xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision ea394cb00fd96864e34d2841b4a22357b621c78f)
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 2010 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 static	void	ill_dlpi_clear_deferred(ill_t *ill);
217 
218 /*
219  * if we go over the memory footprint limit more than once in this msec
220  * interval, we'll start pruning aggressively.
221  */
222 int ip_min_frag_prune_time = 0;
223 
224 static ipft_t	ip_ioctl_ftbl[] = {
225 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
226 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
227 		IPFT_F_NO_REPLY },
228 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
229 	{ 0 }
230 };
231 
232 /* Simple ICMP IP Header Template */
233 static ipha_t icmp_ipha = {
234 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
235 };
236 
237 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
238 
239 static ip_m_t   ip_m_tbl[] = {
240 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
241 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
242 	    ip_nodef_v6intfid },
243 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
259 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
260 	    ip_ipv4_v6destintfid },
261 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
263 	    ip_ipv6_v6destintfid },
264 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_nodef_v6intfid },
267 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
269 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
271 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
272 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
273 	    ip_nodef_v6intfid }
274 };
275 
276 static ill_t	ill_null;		/* Empty ILL for init. */
277 char	ipif_loopback_name[] = "lo0";
278 static char *ipv4_forward_suffix = ":ip_forwarding";
279 static char *ipv6_forward_suffix = ":ip6_forwarding";
280 
281 /* These are used by all IP network modules. */
282 sin6_t	sin6_null;	/* Zero address for quick clears */
283 sin_t	sin_null;	/* Zero address for quick clears */
284 
285 /* When set search for unused ipif_seqid */
286 static ipif_t	ipif_zero;
287 
288 /*
289  * ppa arena is created after these many
290  * interfaces have been plumbed.
291  */
292 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
293 
294 /*
295  * Allocate per-interface mibs.
296  * Returns true if ok. False otherwise.
297  *  ipsq  may not yet be allocated (loopback case ).
298  */
299 static boolean_t
300 ill_allocate_mibs(ill_t *ill)
301 {
302 	/* Already allocated? */
303 	if (ill->ill_ip_mib != NULL) {
304 		if (ill->ill_isv6)
305 			ASSERT(ill->ill_icmp6_mib != NULL);
306 		return (B_TRUE);
307 	}
308 
309 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
310 	    KM_NOSLEEP);
311 	if (ill->ill_ip_mib == NULL) {
312 		return (B_FALSE);
313 	}
314 
315 	/* Setup static information */
316 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
317 	    sizeof (mib2_ipIfStatsEntry_t));
318 	if (ill->ill_isv6) {
319 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
321 		    sizeof (mib2_ipv6AddrEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
323 		    sizeof (mib2_ipv6RouteEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
325 		    sizeof (mib2_ipv6NetToMediaEntry_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
327 		    sizeof (ipv6_member_t));
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
329 		    sizeof (ipv6_grpsrc_t));
330 	} else {
331 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
333 		    sizeof (mib2_ipAddrEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
335 		    sizeof (mib2_ipRouteEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
337 		    sizeof (mib2_ipNetToMediaEntry_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
339 		    sizeof (ip_member_t));
340 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
341 		    sizeof (ip_grpsrc_t));
342 
343 		/*
344 		 * For a v4 ill, we are done at this point, because per ill
345 		 * icmp mibs are only used for v6.
346 		 */
347 		return (B_TRUE);
348 	}
349 
350 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
351 	    KM_NOSLEEP);
352 	if (ill->ill_icmp6_mib == NULL) {
353 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
354 		ill->ill_ip_mib = NULL;
355 		return (B_FALSE);
356 	}
357 	/* static icmp info */
358 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
359 	    sizeof (mib2_ipv6IfIcmpEntry_t);
360 	/*
361 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
362 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
363 	 * -> ill_phyint_reinit
364 	 */
365 	return (B_TRUE);
366 }
367 
368 /*
369  * Completely vaporize a lower level tap and all associated interfaces.
370  * ill_delete is called only out of ip_close when the device control
371  * stream is being closed.
372  */
373 void
374 ill_delete(ill_t *ill)
375 {
376 	ipif_t	*ipif;
377 	ill_t	*prev_ill;
378 	ip_stack_t	*ipst = ill->ill_ipst;
379 
380 	/*
381 	 * ill_delete may be forcibly entering the ipsq. The previous
382 	 * ioctl may not have completed and may need to be aborted.
383 	 * ipsq_flush takes care of it. If we don't need to enter the
384 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
385 	 * ill_delete_tail is sufficient.
386 	 */
387 	ipsq_flush(ill);
388 
389 	/*
390 	 * Nuke all interfaces.  ipif_free will take down the interface,
391 	 * remove it from the list, and free the data structure.
392 	 * Walk down the ipif list and remove the logical interfaces
393 	 * first before removing the main ipif. We can't unplumb
394 	 * zeroth interface first in the case of IPv6 as update_conn_ill
395 	 * -> ip_ll_multireq de-references ill_ipif for checking
396 	 * POINTOPOINT.
397 	 *
398 	 * If ill_ipif was not properly initialized (i.e low on memory),
399 	 * then no interfaces to clean up. In this case just clean up the
400 	 * ill.
401 	 */
402 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
403 		ipif_free(ipif);
404 
405 	/*
406 	 * clean out all the nce_t entries that depend on this
407 	 * ill for the ill_phys_addr.
408 	 */
409 	nce_flush(ill, B_TRUE);
410 
411 	/* Clean up msgs on pending upcalls for mrouted */
412 	reset_mrt_ill(ill);
413 
414 	update_conn_ill(ill, ipst);
415 
416 	/*
417 	 * Remove multicast references added as a result of calls to
418 	 * ip_join_allmulti().
419 	 */
420 	ip_purge_allmulti(ill);
421 
422 	/*
423 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
424 	 */
425 	if (IS_UNDER_IPMP(ill))
426 		ipmp_ill_leave_illgrp(ill);
427 
428 	/*
429 	 * ill_down will arrange to blow off any IRE's dependent on this
430 	 * ILL, and shut down fragmentation reassembly.
431 	 */
432 	ill_down(ill);
433 
434 	/* Let SCTP know, so that it can remove this from its list. */
435 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
436 
437 	/*
438 	 * Walk all CONNs that can have a reference on an ire or nce for this
439 	 * ill (we actually walk all that now have stale references).
440 	 */
441 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
442 
443 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
444 	if (ill->ill_isv6)
445 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
446 
447 	/*
448 	 * If an address on this ILL is being used as a source address then
449 	 * clear out the pointers in other ILLs that point to this ILL.
450 	 */
451 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
452 	if (ill->ill_usesrc_grp_next != NULL) {
453 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
454 			ill_disband_usesrc_group(ill);
455 		} else {	/* consumer of the usesrc ILL */
456 			prev_ill = ill_prev_usesrc(ill);
457 			prev_ill->ill_usesrc_grp_next =
458 			    ill->ill_usesrc_grp_next;
459 		}
460 	}
461 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
462 }
463 
464 static void
465 ipif_non_duplicate(ipif_t *ipif)
466 {
467 	ill_t *ill = ipif->ipif_ill;
468 	mutex_enter(&ill->ill_lock);
469 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
470 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
471 		ASSERT(ill->ill_ipif_dup_count > 0);
472 		ill->ill_ipif_dup_count--;
473 	}
474 	mutex_exit(&ill->ill_lock);
475 }
476 
477 /*
478  * ill_delete_tail is called from ip_modclose after all references
479  * to the closing ill are gone. The wait is done in ip_modclose
480  */
481 void
482 ill_delete_tail(ill_t *ill)
483 {
484 	mblk_t	**mpp;
485 	ipif_t	*ipif;
486 	ip_stack_t	*ipst = ill->ill_ipst;
487 
488 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
489 		ipif_non_duplicate(ipif);
490 		(void) ipif_down_tail(ipif);
491 	}
492 
493 	ASSERT(ill->ill_ipif_dup_count == 0);
494 
495 	/*
496 	 * If polling capability is enabled (which signifies direct
497 	 * upcall into IP and driver has ill saved as a handle),
498 	 * we need to make sure that unbind has completed before we
499 	 * let the ill disappear and driver no longer has any reference
500 	 * to this ill.
501 	 */
502 	mutex_enter(&ill->ill_lock);
503 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
504 		cv_wait(&ill->ill_cv, &ill->ill_lock);
505 	mutex_exit(&ill->ill_lock);
506 	ASSERT(!(ill->ill_capabilities &
507 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
508 
509 	if (ill->ill_net_type != IRE_LOOPBACK)
510 		qprocsoff(ill->ill_rq);
511 
512 	/*
513 	 * We do an ipsq_flush once again now. New messages could have
514 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
515 	 * could also have landed up if an ioctl thread had looked up
516 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
517 	 * enqueued the ioctl when we did the ipsq_flush last time.
518 	 */
519 	ipsq_flush(ill);
520 
521 	/*
522 	 * Free capabilities.
523 	 */
524 	if (ill->ill_hcksum_capab != NULL) {
525 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
526 		ill->ill_hcksum_capab = NULL;
527 	}
528 
529 	if (ill->ill_zerocopy_capab != NULL) {
530 		kmem_free(ill->ill_zerocopy_capab,
531 		    sizeof (ill_zerocopy_capab_t));
532 		ill->ill_zerocopy_capab = NULL;
533 	}
534 
535 	if (ill->ill_lso_capab != NULL) {
536 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
537 		ill->ill_lso_capab = NULL;
538 	}
539 
540 	if (ill->ill_dld_capab != NULL) {
541 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
542 		ill->ill_dld_capab = NULL;
543 	}
544 
545 	while (ill->ill_ipif != NULL)
546 		ipif_free_tail(ill->ill_ipif);
547 
548 	/*
549 	 * We have removed all references to ilm from conn and the ones joined
550 	 * within the kernel.
551 	 *
552 	 * We don't walk conns, mrts and ires because
553 	 *
554 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
555 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
556 	 *    ill references.
557 	 */
558 
559 	/*
560 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
561 	 * is safe to do because the illgrp has already been unlinked from the
562 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
563 	 */
564 	if (IS_IPMP(ill)) {
565 		ipmp_illgrp_destroy(ill->ill_grp);
566 		ill->ill_grp = NULL;
567 	}
568 
569 	/*
570 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
571 	 * could free the phyint. No more reference to the phyint after this
572 	 * point.
573 	 */
574 	(void) ill_glist_delete(ill);
575 
576 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
577 	if (ill->ill_ndd_name != NULL)
578 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
579 	rw_exit(&ipst->ips_ip_g_nd_lock);
580 
581 	if (ill->ill_frag_ptr != NULL) {
582 		uint_t count;
583 
584 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
585 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
586 		}
587 		mi_free(ill->ill_frag_ptr);
588 		ill->ill_frag_ptr = NULL;
589 		ill->ill_frag_hash_tbl = NULL;
590 	}
591 
592 	freemsg(ill->ill_nd_lla_mp);
593 	/* Free all retained control messages. */
594 	mpp = &ill->ill_first_mp_to_free;
595 	do {
596 		while (mpp[0]) {
597 			mblk_t  *mp;
598 			mblk_t  *mp1;
599 
600 			mp = mpp[0];
601 			mpp[0] = mp->b_next;
602 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
603 				mp1->b_next = NULL;
604 				mp1->b_prev = NULL;
605 			}
606 			freemsg(mp);
607 		}
608 	} while (mpp++ != &ill->ill_last_mp_to_free);
609 
610 	ill_free_mib(ill);
611 
612 #ifdef DEBUG
613 	ill_trace_cleanup(ill);
614 #endif
615 
616 	/* The default multicast interface might have changed */
617 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
618 
619 	/* Drop refcnt here */
620 	netstack_rele(ill->ill_ipst->ips_netstack);
621 	ill->ill_ipst = NULL;
622 }
623 
624 static void
625 ill_free_mib(ill_t *ill)
626 {
627 	ip_stack_t *ipst = ill->ill_ipst;
628 
629 	/*
630 	 * MIB statistics must not be lost, so when an interface
631 	 * goes away the counter values will be added to the global
632 	 * MIBs.
633 	 */
634 	if (ill->ill_ip_mib != NULL) {
635 		if (ill->ill_isv6) {
636 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
637 			    ill->ill_ip_mib);
638 		} else {
639 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
640 			    ill->ill_ip_mib);
641 		}
642 
643 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
644 		ill->ill_ip_mib = NULL;
645 	}
646 	if (ill->ill_icmp6_mib != NULL) {
647 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
648 		    ill->ill_icmp6_mib);
649 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
650 		ill->ill_icmp6_mib = NULL;
651 	}
652 }
653 
654 /*
655  * Concatenate together a physical address and a sap.
656  *
657  * Sap_lengths are interpreted as follows:
658  *   sap_length == 0	==>	no sap
659  *   sap_length > 0	==>	sap is at the head of the dlpi address
660  *   sap_length < 0	==>	sap is at the tail of the dlpi address
661  */
662 static void
663 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
664     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
665 {
666 	uint16_t sap_addr = (uint16_t)sap_src;
667 
668 	if (sap_length == 0) {
669 		if (phys_src == NULL)
670 			bzero(dst, phys_length);
671 		else
672 			bcopy(phys_src, dst, phys_length);
673 	} else if (sap_length < 0) {
674 		if (phys_src == NULL)
675 			bzero(dst, phys_length);
676 		else
677 			bcopy(phys_src, dst, phys_length);
678 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
679 	} else {
680 		bcopy(&sap_addr, dst, sizeof (sap_addr));
681 		if (phys_src == NULL)
682 			bzero((char *)dst + sap_length, phys_length);
683 		else
684 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
685 	}
686 }
687 
688 /*
689  * Generate a dl_unitdata_req mblk for the device and address given.
690  * addr_length is the length of the physical portion of the address.
691  * If addr is NULL include an all zero address of the specified length.
692  * TRUE? In any case, addr_length is taken to be the entire length of the
693  * dlpi address, including the absolute value of sap_length.
694  */
695 mblk_t *
696 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
697 		t_scalar_t sap_length)
698 {
699 	dl_unitdata_req_t *dlur;
700 	mblk_t	*mp;
701 	t_scalar_t	abs_sap_length;		/* absolute value */
702 
703 	abs_sap_length = ABS(sap_length);
704 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
705 	    DL_UNITDATA_REQ);
706 	if (mp == NULL)
707 		return (NULL);
708 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
709 	/* HACK: accomodate incompatible DLPI drivers */
710 	if (addr_length == 8)
711 		addr_length = 6;
712 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
713 	dlur->dl_dest_addr_offset = sizeof (*dlur);
714 	dlur->dl_priority.dl_min = 0;
715 	dlur->dl_priority.dl_max = 0;
716 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
717 	    (uchar_t *)&dlur[1]);
718 	return (mp);
719 }
720 
721 /*
722  * Add the pending mp to the list. There can be only 1 pending mp
723  * in the list. Any exclusive ioctl that needs to wait for a response
724  * from another module or driver needs to use this function to set
725  * the ipx_pending_mp to the ioctl mblk and wait for the response from
726  * the other module/driver. This is also used while waiting for the
727  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
728  */
729 boolean_t
730 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
731     int waitfor)
732 {
733 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
734 
735 	ASSERT(IAM_WRITER_IPIF(ipif));
736 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
737 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
738 	ASSERT(ipx->ipx_pending_mp == NULL);
739 	/*
740 	 * The caller may be using a different ipif than the one passed into
741 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
742 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
743 	 * that `ipx_current_ipif == ipif'.
744 	 */
745 	ASSERT(ipx->ipx_current_ipif != NULL);
746 
747 	/*
748 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
749 	 * driver.
750 	 */
751 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
752 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
753 	    (DB_TYPE(add_mp) == M_PCPROTO));
754 
755 	if (connp != NULL) {
756 		ASSERT(MUTEX_HELD(&connp->conn_lock));
757 		/*
758 		 * Return error if the conn has started closing. The conn
759 		 * could have finished cleaning up the pending mp list,
760 		 * If so we should not add another mp to the list negating
761 		 * the cleanup.
762 		 */
763 		if (connp->conn_state_flags & CONN_CLOSING)
764 			return (B_FALSE);
765 	}
766 	mutex_enter(&ipx->ipx_lock);
767 	ipx->ipx_pending_ipif = ipif;
768 	/*
769 	 * Note down the queue in b_queue. This will be returned by
770 	 * ipsq_pending_mp_get. Caller will then use these values to restart
771 	 * the processing
772 	 */
773 	add_mp->b_next = NULL;
774 	add_mp->b_queue = q;
775 	ipx->ipx_pending_mp = add_mp;
776 	ipx->ipx_waitfor = waitfor;
777 	mutex_exit(&ipx->ipx_lock);
778 
779 	if (connp != NULL)
780 		connp->conn_oper_pending_ill = ipif->ipif_ill;
781 
782 	return (B_TRUE);
783 }
784 
785 /*
786  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
787  * queued in the list.
788  */
789 mblk_t *
790 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
791 {
792 	mblk_t	*curr = NULL;
793 	ipxop_t	*ipx = ipsq->ipsq_xop;
794 
795 	*connpp = NULL;
796 	mutex_enter(&ipx->ipx_lock);
797 	if (ipx->ipx_pending_mp == NULL) {
798 		mutex_exit(&ipx->ipx_lock);
799 		return (NULL);
800 	}
801 
802 	/* There can be only 1 such excl message */
803 	curr = ipx->ipx_pending_mp;
804 	ASSERT(curr->b_next == NULL);
805 	ipx->ipx_pending_ipif = NULL;
806 	ipx->ipx_pending_mp = NULL;
807 	ipx->ipx_waitfor = 0;
808 	mutex_exit(&ipx->ipx_lock);
809 
810 	if (CONN_Q(curr->b_queue)) {
811 		/*
812 		 * This mp did a refhold on the conn, at the start of the ioctl.
813 		 * So we can safely return a pointer to the conn to the caller.
814 		 */
815 		*connpp = Q_TO_CONN(curr->b_queue);
816 	} else {
817 		*connpp = NULL;
818 	}
819 	curr->b_next = NULL;
820 	curr->b_prev = NULL;
821 	return (curr);
822 }
823 
824 /*
825  * Cleanup the ioctl mp queued in ipx_pending_mp
826  * - Called in the ill_delete path
827  * - Called in the M_ERROR or M_HANGUP path on the ill.
828  * - Called in the conn close path.
829  *
830  * Returns success on finding the pending mblk associated with the ioctl or
831  * exclusive operation in progress, failure otherwise.
832  */
833 boolean_t
834 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
835 {
836 	mblk_t	*mp;
837 	ipxop_t	*ipx;
838 	queue_t	*q;
839 	ipif_t	*ipif;
840 	int	cmd;
841 
842 	ASSERT(IAM_WRITER_ILL(ill));
843 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
844 
845 	mutex_enter(&ipx->ipx_lock);
846 	mp = ipx->ipx_pending_mp;
847 	if (connp != NULL) {
848 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
849 			/*
850 			 * Nothing to clean since the conn that is closing
851 			 * does not have a matching pending mblk in
852 			 * ipx_pending_mp.
853 			 */
854 			mutex_exit(&ipx->ipx_lock);
855 			return (B_FALSE);
856 		}
857 	} else {
858 		/*
859 		 * A non-zero ill_error signifies we are called in the
860 		 * M_ERROR or M_HANGUP path and we need to unconditionally
861 		 * abort any current ioctl and do the corresponding cleanup.
862 		 * A zero ill_error means we are in the ill_delete path and
863 		 * we do the cleanup only if there is a pending mp.
864 		 */
865 		if (mp == NULL && ill->ill_error == 0) {
866 			mutex_exit(&ipx->ipx_lock);
867 			return (B_FALSE);
868 		}
869 	}
870 
871 	/* Now remove from the ipx_pending_mp */
872 	ipx->ipx_pending_mp = NULL;
873 	ipif = ipx->ipx_pending_ipif;
874 	ipx->ipx_pending_ipif = NULL;
875 	ipx->ipx_waitfor = 0;
876 	ipx->ipx_current_ipif = NULL;
877 	cmd = ipx->ipx_current_ioctl;
878 	ipx->ipx_current_ioctl = 0;
879 	ipx->ipx_current_done = B_TRUE;
880 	mutex_exit(&ipx->ipx_lock);
881 
882 	if (mp == NULL)
883 		return (B_FALSE);
884 
885 	q = mp->b_queue;
886 	mp->b_next = NULL;
887 	mp->b_prev = NULL;
888 	mp->b_queue = NULL;
889 
890 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
891 		DTRACE_PROBE4(ipif__ioctl,
892 		    char *, "ipsq_pending_mp_cleanup",
893 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
894 		    ipif_t *, ipif);
895 		if (connp == NULL) {
896 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
897 		} else {
898 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
899 			mutex_enter(&ipif->ipif_ill->ill_lock);
900 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
901 			mutex_exit(&ipif->ipif_ill->ill_lock);
902 		}
903 	} else {
904 		inet_freemsg(mp);
905 	}
906 	return (B_TRUE);
907 }
908 
909 /*
910  * Called in the conn close path and ill delete path
911  */
912 static void
913 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
914 {
915 	ipsq_t	*ipsq;
916 	mblk_t	*prev;
917 	mblk_t	*curr;
918 	mblk_t	*next;
919 	queue_t	*wq, *rq = NULL;
920 	mblk_t	*tmp_list = NULL;
921 
922 	ASSERT(IAM_WRITER_ILL(ill));
923 	if (connp != NULL)
924 		wq = CONNP_TO_WQ(connp);
925 	else
926 		wq = ill->ill_wq;
927 
928 	/*
929 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
930 	 * against this here.
931 	 */
932 	if (wq != NULL)
933 		rq = RD(wq);
934 
935 	ipsq = ill->ill_phyint->phyint_ipsq;
936 	/*
937 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
938 	 * In the case of ioctl from a conn, there can be only 1 mp
939 	 * queued on the ipsq. If an ill is being unplumbed flush all
940 	 * the messages.
941 	 */
942 	mutex_enter(&ipsq->ipsq_lock);
943 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
944 	    curr = next) {
945 		next = curr->b_next;
946 		if (connp == NULL ||
947 		    (curr->b_queue == wq || curr->b_queue == rq)) {
948 			/* Unlink the mblk from the pending mp list */
949 			if (prev != NULL) {
950 				prev->b_next = curr->b_next;
951 			} else {
952 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
953 				ipsq->ipsq_xopq_mphead = curr->b_next;
954 			}
955 			if (ipsq->ipsq_xopq_mptail == curr)
956 				ipsq->ipsq_xopq_mptail = prev;
957 			/*
958 			 * Create a temporary list and release the ipsq lock
959 			 * New elements are added to the head of the tmp_list
960 			 */
961 			curr->b_next = tmp_list;
962 			tmp_list = curr;
963 		} else {
964 			prev = curr;
965 		}
966 	}
967 	mutex_exit(&ipsq->ipsq_lock);
968 
969 	while (tmp_list != NULL) {
970 		curr = tmp_list;
971 		tmp_list = curr->b_next;
972 		curr->b_next = NULL;
973 		curr->b_prev = NULL;
974 		curr->b_queue = NULL;
975 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
976 			DTRACE_PROBE4(ipif__ioctl,
977 			    char *, "ipsq_xopq_mp_cleanup",
978 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
979 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
980 			    CONN_CLOSE : NO_COPYOUT, NULL);
981 		} else {
982 			/*
983 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
984 			 * this can't be just inet_freemsg. we have to
985 			 * restart it otherwise the thread will be stuck.
986 			 */
987 			inet_freemsg(curr);
988 		}
989 	}
990 }
991 
992 /*
993  * This conn has started closing. Cleanup any pending ioctl from this conn.
994  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
995  */
996 void
997 conn_ioctl_cleanup(conn_t *connp)
998 {
999 	ipsq_t	*ipsq;
1000 	ill_t	*ill;
1001 	boolean_t refheld;
1002 
1003 	/*
1004 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1005 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1006 	 * started the mp could be present in ipx_pending_mp. Note that if
1007 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1008 	 * not yet queued anywhere. In this case, the conn close code will wait
1009 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1010 	 * tcp_close will wait first until all ioctls have completed for this
1011 	 * conn.
1012 	 */
1013 	mutex_enter(&connp->conn_lock);
1014 	ill = connp->conn_oper_pending_ill;
1015 	if (ill == NULL) {
1016 		mutex_exit(&connp->conn_lock);
1017 		return;
1018 	}
1019 
1020 	/*
1021 	 * We may not be able to refhold the ill if the ill/ipif
1022 	 * is changing. But we need to make sure that the ill will
1023 	 * not vanish. So we just bump up the ill_waiter count.
1024 	 */
1025 	refheld = ill_waiter_inc(ill);
1026 	mutex_exit(&connp->conn_lock);
1027 	if (refheld) {
1028 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1029 			ill_waiter_dcr(ill);
1030 			/*
1031 			 * Check whether this ioctl has started and is
1032 			 * pending. If it is not found there then check
1033 			 * whether this ioctl has not even started and is in
1034 			 * the ipsq_xopq list.
1035 			 */
1036 			if (!ipsq_pending_mp_cleanup(ill, connp))
1037 				ipsq_xopq_mp_cleanup(ill, connp);
1038 			ipsq = ill->ill_phyint->phyint_ipsq;
1039 			ipsq_exit(ipsq);
1040 			return;
1041 		}
1042 	}
1043 
1044 	/*
1045 	 * The ill is also closing and we could not bump up the
1046 	 * ill_waiter_count or we could not enter the ipsq. Leave
1047 	 * the cleanup to ill_delete
1048 	 */
1049 	mutex_enter(&connp->conn_lock);
1050 	while (connp->conn_oper_pending_ill != NULL)
1051 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1052 	mutex_exit(&connp->conn_lock);
1053 	if (refheld)
1054 		ill_waiter_dcr(ill);
1055 }
1056 
1057 /*
1058  * ipcl_walk function for cleaning up conn_*_ill fields.
1059  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1060  * conn_bound_if in place. We prefer dropping
1061  * packets instead of sending them out the wrong interface, or accepting
1062  * packets from the wrong ifindex.
1063  */
1064 static void
1065 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1066 {
1067 	ill_t	*ill = (ill_t *)arg;
1068 
1069 	mutex_enter(&connp->conn_lock);
1070 	if (connp->conn_dhcpinit_ill == ill) {
1071 		connp->conn_dhcpinit_ill = NULL;
1072 		ASSERT(ill->ill_dhcpinit != 0);
1073 		atomic_dec_32(&ill->ill_dhcpinit);
1074 		ill_set_inputfn(ill);
1075 	}
1076 	mutex_exit(&connp->conn_lock);
1077 }
1078 
1079 static int
1080 ill_down_ipifs_tail(ill_t *ill)
1081 {
1082 	ipif_t	*ipif;
1083 	int err;
1084 
1085 	ASSERT(IAM_WRITER_ILL(ill));
1086 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1087 		ipif_non_duplicate(ipif);
1088 		/*
1089 		 * ipif_down_tail will call arp_ll_down on the last ipif
1090 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1091 		 */
1092 		if ((err = ipif_down_tail(ipif)) != 0)
1093 			return (err);
1094 	}
1095 	return (0);
1096 }
1097 
1098 /* ARGSUSED */
1099 void
1100 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1101 {
1102 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1103 	(void) ill_down_ipifs_tail(q->q_ptr);
1104 	freemsg(mp);
1105 	ipsq_current_finish(ipsq);
1106 }
1107 
1108 /*
1109  * ill_down_start is called when we want to down this ill and bring it up again
1110  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1111  * all interfaces, but don't tear down any plumbing.
1112  */
1113 boolean_t
1114 ill_down_start(queue_t *q, mblk_t *mp)
1115 {
1116 	ill_t	*ill = q->q_ptr;
1117 	ipif_t	*ipif;
1118 
1119 	ASSERT(IAM_WRITER_ILL(ill));
1120 	mutex_enter(&ill->ill_lock);
1121 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1122 	/* no more nce addition allowed */
1123 	mutex_exit(&ill->ill_lock);
1124 
1125 	/*
1126 	 * It is possible that some ioctl is already in progress while we
1127 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1128 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1129 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1130 	 * the in progress ioctl from ever completing.
1131 	 *
1132 	 * The thread that started the ioctl (if any) must have returned,
1133 	 * since we are now executing as writer. After the 2 calls below,
1134 	 * the state of the ipsq and the ill would reflect no trace of any
1135 	 * pending operation. Subsequently if there is any response to the
1136 	 * original ioctl from the driver, it would be discarded as an
1137 	 * unsolicited message from the driver.
1138 	 */
1139 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1140 	ill_dlpi_clear_deferred(ill);
1141 
1142 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1143 		(void) ipif_down(ipif, NULL, NULL);
1144 
1145 	ill_down(ill);
1146 
1147 	/*
1148 	 * Walk all CONNs that can have a reference on an ire or nce for this
1149 	 * ill (we actually walk all that now have stale references).
1150 	 */
1151 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1152 
1153 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1154 	if (ill->ill_isv6)
1155 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1156 
1157 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1158 
1159 	/*
1160 	 * Atomically test and add the pending mp if references are active.
1161 	 */
1162 	mutex_enter(&ill->ill_lock);
1163 	if (!ill_is_quiescent(ill)) {
1164 		/* call cannot fail since `conn_t *' argument is NULL */
1165 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1166 		    mp, ILL_DOWN);
1167 		mutex_exit(&ill->ill_lock);
1168 		return (B_FALSE);
1169 	}
1170 	mutex_exit(&ill->ill_lock);
1171 	return (B_TRUE);
1172 }
1173 
1174 static void
1175 ill_down(ill_t *ill)
1176 {
1177 	mblk_t	*mp;
1178 	ip_stack_t	*ipst = ill->ill_ipst;
1179 
1180 	/*
1181 	 * Blow off any IREs dependent on this ILL.
1182 	 * The caller needs to handle conn_ixa_cleanup
1183 	 */
1184 	ill_delete_ires(ill);
1185 
1186 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1187 
1188 	/* Remove any conn_*_ill depending on this ill */
1189 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1190 
1191 	/*
1192 	 * Free state for additional IREs.
1193 	 */
1194 	mutex_enter(&ill->ill_saved_ire_lock);
1195 	mp = ill->ill_saved_ire_mp;
1196 	ill->ill_saved_ire_mp = NULL;
1197 	ill->ill_saved_ire_cnt = 0;
1198 	mutex_exit(&ill->ill_saved_ire_lock);
1199 	freemsg(mp);
1200 }
1201 
1202 /*
1203  * ire_walk routine used to delete every IRE that depends on
1204  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1205  *
1206  * Note: since the routes added by the kernel are deleted separately,
1207  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1208  *
1209  * We also remove references on ire_nce_cache entries that refer to the ill.
1210  */
1211 void
1212 ill_downi(ire_t *ire, char *ill_arg)
1213 {
1214 	ill_t	*ill = (ill_t *)ill_arg;
1215 	nce_t	*nce;
1216 
1217 	mutex_enter(&ire->ire_lock);
1218 	nce = ire->ire_nce_cache;
1219 	if (nce != NULL && nce->nce_ill == ill)
1220 		ire->ire_nce_cache = NULL;
1221 	else
1222 		nce = NULL;
1223 	mutex_exit(&ire->ire_lock);
1224 	if (nce != NULL)
1225 		nce_refrele(nce);
1226 	if (ire->ire_ill == ill) {
1227 		/*
1228 		 * The existing interface binding for ire must be
1229 		 * deleted before trying to bind the route to another
1230 		 * interface. However, since we are using the contents of the
1231 		 * ire after ire_delete, the caller has to ensure that
1232 		 * CONDEMNED (deleted) ire's are not removed from the list
1233 		 * when ire_delete() returns. Currently ill_downi() is
1234 		 * only called as part of ire_walk*() routines, so that
1235 		 * the irb_refhold() done by ire_walk*() will ensure that
1236 		 * ire_delete() does not lead to ire_inactive().
1237 		 */
1238 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1239 		ire_delete(ire);
1240 		if (ire->ire_unbound)
1241 			ire_rebind(ire);
1242 	}
1243 }
1244 
1245 /* Remove IRE_IF_CLONE on this ill */
1246 void
1247 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1248 {
1249 	ill_t	*ill = (ill_t *)ill_arg;
1250 
1251 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1252 	if (ire->ire_ill == ill)
1253 		ire_delete(ire);
1254 }
1255 
1256 /* Consume an M_IOCACK of the fastpath probe. */
1257 void
1258 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1259 {
1260 	mblk_t	*mp1 = mp;
1261 
1262 	/*
1263 	 * If this was the first attempt turn on the fastpath probing.
1264 	 */
1265 	mutex_enter(&ill->ill_lock);
1266 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1267 		ill->ill_dlpi_fastpath_state = IDS_OK;
1268 	mutex_exit(&ill->ill_lock);
1269 
1270 	/* Free the M_IOCACK mblk, hold on to the data */
1271 	mp = mp->b_cont;
1272 	freeb(mp1);
1273 	if (mp == NULL)
1274 		return;
1275 	if (mp->b_cont != NULL)
1276 		nce_fastpath_update(ill, mp);
1277 	else
1278 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1279 	freemsg(mp);
1280 }
1281 
1282 /*
1283  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1284  * The data portion of the request is a dl_unitdata_req_t template for
1285  * what we would send downstream in the absence of a fastpath confirmation.
1286  */
1287 int
1288 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1289 {
1290 	struct iocblk	*ioc;
1291 	mblk_t	*mp;
1292 
1293 	if (dlur_mp == NULL)
1294 		return (EINVAL);
1295 
1296 	mutex_enter(&ill->ill_lock);
1297 	switch (ill->ill_dlpi_fastpath_state) {
1298 	case IDS_FAILED:
1299 		/*
1300 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1301 		 * support it.
1302 		 */
1303 		mutex_exit(&ill->ill_lock);
1304 		return (ENOTSUP);
1305 	case IDS_UNKNOWN:
1306 		/* This is the first probe */
1307 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1308 		break;
1309 	default:
1310 		break;
1311 	}
1312 	mutex_exit(&ill->ill_lock);
1313 
1314 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1315 		return (EAGAIN);
1316 
1317 	mp->b_cont = copyb(dlur_mp);
1318 	if (mp->b_cont == NULL) {
1319 		freeb(mp);
1320 		return (EAGAIN);
1321 	}
1322 
1323 	ioc = (struct iocblk *)mp->b_rptr;
1324 	ioc->ioc_count = msgdsize(mp->b_cont);
1325 
1326 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1327 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1328 	putnext(ill->ill_wq, mp);
1329 	return (0);
1330 }
1331 
1332 void
1333 ill_capability_probe(ill_t *ill)
1334 {
1335 	mblk_t	*mp;
1336 
1337 	ASSERT(IAM_WRITER_ILL(ill));
1338 
1339 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1340 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1341 		return;
1342 
1343 	/*
1344 	 * We are starting a new cycle of capability negotiation.
1345 	 * Free up the capab reset messages of any previous incarnation.
1346 	 * We will do a fresh allocation when we get the response to our probe
1347 	 */
1348 	if (ill->ill_capab_reset_mp != NULL) {
1349 		freemsg(ill->ill_capab_reset_mp);
1350 		ill->ill_capab_reset_mp = NULL;
1351 	}
1352 
1353 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1354 
1355 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1356 	if (mp == NULL)
1357 		return;
1358 
1359 	ill_capability_send(ill, mp);
1360 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1361 }
1362 
1363 void
1364 ill_capability_reset(ill_t *ill, boolean_t reneg)
1365 {
1366 	ASSERT(IAM_WRITER_ILL(ill));
1367 
1368 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1369 		return;
1370 
1371 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1372 
1373 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1374 	ill->ill_capab_reset_mp = NULL;
1375 	/*
1376 	 * We turn off all capabilities except those pertaining to
1377 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1378 	 * which will be turned off by the corresponding reset functions.
1379 	 */
1380 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1381 }
1382 
1383 static void
1384 ill_capability_reset_alloc(ill_t *ill)
1385 {
1386 	mblk_t *mp;
1387 	size_t	size = 0;
1388 	int	err;
1389 	dl_capability_req_t	*capb;
1390 
1391 	ASSERT(IAM_WRITER_ILL(ill));
1392 	ASSERT(ill->ill_capab_reset_mp == NULL);
1393 
1394 	if (ILL_HCKSUM_CAPABLE(ill)) {
1395 		size += sizeof (dl_capability_sub_t) +
1396 		    sizeof (dl_capab_hcksum_t);
1397 	}
1398 
1399 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1400 		size += sizeof (dl_capability_sub_t) +
1401 		    sizeof (dl_capab_zerocopy_t);
1402 	}
1403 
1404 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1405 		size += sizeof (dl_capability_sub_t) +
1406 		    sizeof (dl_capab_dld_t);
1407 	}
1408 
1409 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1410 	    STR_NOSIG, &err);
1411 
1412 	mp->b_datap->db_type = M_PROTO;
1413 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1414 
1415 	capb = (dl_capability_req_t *)mp->b_rptr;
1416 	capb->dl_primitive = DL_CAPABILITY_REQ;
1417 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1418 	capb->dl_sub_length = size;
1419 
1420 	mp->b_wptr += sizeof (dl_capability_req_t);
1421 
1422 	/*
1423 	 * Each handler fills in the corresponding dl_capability_sub_t
1424 	 * inside the mblk,
1425 	 */
1426 	ill_capability_hcksum_reset_fill(ill, mp);
1427 	ill_capability_zerocopy_reset_fill(ill, mp);
1428 	ill_capability_dld_reset_fill(ill, mp);
1429 
1430 	ill->ill_capab_reset_mp = mp;
1431 }
1432 
1433 static void
1434 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1435 {
1436 	dl_capab_id_t *id_ic;
1437 	uint_t sub_dl_cap = outers->dl_cap;
1438 	dl_capability_sub_t *inners;
1439 	uint8_t *capend;
1440 
1441 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1442 
1443 	/*
1444 	 * Note: range checks here are not absolutely sufficient to
1445 	 * make us robust against malformed messages sent by drivers;
1446 	 * this is in keeping with the rest of IP's dlpi handling.
1447 	 * (Remember, it's coming from something else in the kernel
1448 	 * address space)
1449 	 */
1450 
1451 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1452 	if (capend > mp->b_wptr) {
1453 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1454 		    "malformed sub-capability too long for mblk");
1455 		return;
1456 	}
1457 
1458 	id_ic = (dl_capab_id_t *)(outers + 1);
1459 
1460 	if (outers->dl_length < sizeof (*id_ic) ||
1461 	    (inners = &id_ic->id_subcap,
1462 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1463 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1464 		    "encapsulated capab type %d too long for mblk",
1465 		    inners->dl_cap);
1466 		return;
1467 	}
1468 
1469 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1470 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1471 		    "isn't as expected; pass-thru module(s) detected, "
1472 		    "discarding capability\n", inners->dl_cap));
1473 		return;
1474 	}
1475 
1476 	/* Process the encapsulated sub-capability */
1477 	ill_capability_dispatch(ill, mp, inners);
1478 }
1479 
1480 static void
1481 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1482 {
1483 	dl_capability_sub_t *dl_subcap;
1484 
1485 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1486 		return;
1487 
1488 	/*
1489 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1490 	 * initialized below since it is not used by DLD.
1491 	 */
1492 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1493 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1494 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1495 
1496 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1497 }
1498 
1499 static void
1500 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1501 {
1502 	/*
1503 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1504 	 * is only to get the VRRP capability.
1505 	 *
1506 	 * Note that we cannot check ill_ipif_up_count here since
1507 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1508 	 * That is done asynchronously, and can race with this function.
1509 	 */
1510 	if (!ill->ill_dl_up) {
1511 		if (subp->dl_cap == DL_CAPAB_VRRP)
1512 			ill_capability_vrrp_ack(ill, mp, subp);
1513 		return;
1514 	}
1515 
1516 	switch (subp->dl_cap) {
1517 	case DL_CAPAB_HCKSUM:
1518 		ill_capability_hcksum_ack(ill, mp, subp);
1519 		break;
1520 	case DL_CAPAB_ZEROCOPY:
1521 		ill_capability_zerocopy_ack(ill, mp, subp);
1522 		break;
1523 	case DL_CAPAB_DLD:
1524 		ill_capability_dld_ack(ill, mp, subp);
1525 		break;
1526 	case DL_CAPAB_VRRP:
1527 		break;
1528 	default:
1529 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1530 		    subp->dl_cap));
1531 	}
1532 }
1533 
1534 /*
1535  * Process the vrrp capability received from a DLS Provider. isub must point
1536  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1537  */
1538 static void
1539 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1540 {
1541 	dl_capab_vrrp_t	*vrrp;
1542 	uint_t		sub_dl_cap = isub->dl_cap;
1543 	uint8_t		*capend;
1544 
1545 	ASSERT(IAM_WRITER_ILL(ill));
1546 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1547 
1548 	/*
1549 	 * Note: range checks here are not absolutely sufficient to
1550 	 * make us robust against malformed messages sent by drivers;
1551 	 * this is in keeping with the rest of IP's dlpi handling.
1552 	 * (Remember, it's coming from something else in the kernel
1553 	 * address space)
1554 	 */
1555 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1556 	if (capend > mp->b_wptr) {
1557 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1558 		    "malformed sub-capability too long for mblk");
1559 		return;
1560 	}
1561 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1562 
1563 	/*
1564 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1565 	 */
1566 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1567 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1568 		ill->ill_flags |= ILLF_VRRP;
1569 	}
1570 }
1571 
1572 /*
1573  * Process a hardware checksum offload capability negotiation ack received
1574  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1575  * of a DL_CAPABILITY_ACK message.
1576  */
1577 static void
1578 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1579 {
1580 	dl_capability_req_t	*ocap;
1581 	dl_capab_hcksum_t	*ihck, *ohck;
1582 	ill_hcksum_capab_t	**ill_hcksum;
1583 	mblk_t			*nmp = NULL;
1584 	uint_t			sub_dl_cap = isub->dl_cap;
1585 	uint8_t			*capend;
1586 
1587 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1588 
1589 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1590 
1591 	/*
1592 	 * Note: range checks here are not absolutely sufficient to
1593 	 * make us robust against malformed messages sent by drivers;
1594 	 * this is in keeping with the rest of IP's dlpi handling.
1595 	 * (Remember, it's coming from something else in the kernel
1596 	 * address space)
1597 	 */
1598 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1599 	if (capend > mp->b_wptr) {
1600 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1601 		    "malformed sub-capability too long for mblk");
1602 		return;
1603 	}
1604 
1605 	/*
1606 	 * There are two types of acks we process here:
1607 	 * 1. acks in reply to a (first form) generic capability req
1608 	 *    (no ENABLE flag set)
1609 	 * 2. acks in reply to a ENABLE capability req.
1610 	 *    (ENABLE flag set)
1611 	 */
1612 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1613 
1614 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1615 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1616 		    "unsupported hardware checksum "
1617 		    "sub-capability (version %d, expected %d)",
1618 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1619 		return;
1620 	}
1621 
1622 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1623 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1624 		    "checksum capability isn't as expected; pass-thru "
1625 		    "module(s) detected, discarding capability\n"));
1626 		return;
1627 	}
1628 
1629 #define	CURR_HCKSUM_CAPAB				\
1630 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1631 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1632 
1633 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1634 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1635 		/* do ENABLE processing */
1636 		if (*ill_hcksum == NULL) {
1637 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1638 			    KM_NOSLEEP);
1639 
1640 			if (*ill_hcksum == NULL) {
1641 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1642 				    "could not enable hcksum version %d "
1643 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1644 				    ill->ill_name);
1645 				return;
1646 			}
1647 		}
1648 
1649 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1650 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1651 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1652 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1653 		    "has enabled hardware checksumming\n ",
1654 		    ill->ill_name));
1655 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1656 		/*
1657 		 * Enabling hardware checksum offload
1658 		 * Currently IP supports {TCP,UDP}/IPv4
1659 		 * partial and full cksum offload and
1660 		 * IPv4 header checksum offload.
1661 		 * Allocate new mblk which will
1662 		 * contain a new capability request
1663 		 * to enable hardware checksum offload.
1664 		 */
1665 		uint_t	size;
1666 		uchar_t	*rptr;
1667 
1668 		size = sizeof (dl_capability_req_t) +
1669 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1670 
1671 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1672 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1673 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1674 			    ill->ill_name);
1675 			return;
1676 		}
1677 
1678 		rptr = nmp->b_rptr;
1679 		/* initialize dl_capability_req_t */
1680 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1681 		ocap->dl_sub_offset =
1682 		    sizeof (dl_capability_req_t);
1683 		ocap->dl_sub_length =
1684 		    sizeof (dl_capability_sub_t) +
1685 		    isub->dl_length;
1686 		nmp->b_rptr += sizeof (dl_capability_req_t);
1687 
1688 		/* initialize dl_capability_sub_t */
1689 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1690 		nmp->b_rptr += sizeof (*isub);
1691 
1692 		/* initialize dl_capab_hcksum_t */
1693 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1694 		bcopy(ihck, ohck, sizeof (*ihck));
1695 
1696 		nmp->b_rptr = rptr;
1697 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1698 
1699 		/* Set ENABLE flag */
1700 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1701 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1702 
1703 		/*
1704 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1705 		 * hardware checksum acceleration.
1706 		 */
1707 		ill_capability_send(ill, nmp);
1708 	} else {
1709 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1710 		    "advertised %x hardware checksum capability flags\n",
1711 		    ill->ill_name, ihck->hcksum_txflags));
1712 	}
1713 }
1714 
1715 static void
1716 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1717 {
1718 	dl_capab_hcksum_t *hck_subcap;
1719 	dl_capability_sub_t *dl_subcap;
1720 
1721 	if (!ILL_HCKSUM_CAPABLE(ill))
1722 		return;
1723 
1724 	ASSERT(ill->ill_hcksum_capab != NULL);
1725 
1726 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1727 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1728 	dl_subcap->dl_length = sizeof (*hck_subcap);
1729 
1730 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1731 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1732 	hck_subcap->hcksum_txflags = 0;
1733 
1734 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1735 }
1736 
1737 static void
1738 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1739 {
1740 	mblk_t *nmp = NULL;
1741 	dl_capability_req_t *oc;
1742 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1743 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1744 	uint_t sub_dl_cap = isub->dl_cap;
1745 	uint8_t *capend;
1746 
1747 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1748 
1749 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1750 
1751 	/*
1752 	 * Note: range checks here are not absolutely sufficient to
1753 	 * make us robust against malformed messages sent by drivers;
1754 	 * this is in keeping with the rest of IP's dlpi handling.
1755 	 * (Remember, it's coming from something else in the kernel
1756 	 * address space)
1757 	 */
1758 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1759 	if (capend > mp->b_wptr) {
1760 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1761 		    "malformed sub-capability too long for mblk");
1762 		return;
1763 	}
1764 
1765 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1766 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1767 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1768 		    "unsupported ZEROCOPY sub-capability (version %d, "
1769 		    "expected %d)", zc_ic->zerocopy_version,
1770 		    ZEROCOPY_VERSION_1);
1771 		return;
1772 	}
1773 
1774 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1775 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1776 		    "capability isn't as expected; pass-thru module(s) "
1777 		    "detected, discarding capability\n"));
1778 		return;
1779 	}
1780 
1781 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1782 		if (*ill_zerocopy_capab == NULL) {
1783 			*ill_zerocopy_capab =
1784 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1785 			    KM_NOSLEEP);
1786 
1787 			if (*ill_zerocopy_capab == NULL) {
1788 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1789 				    "could not enable Zero-copy version %d "
1790 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1791 				    ill->ill_name);
1792 				return;
1793 			}
1794 		}
1795 
1796 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1797 		    "supports Zero-copy version %d\n", ill->ill_name,
1798 		    ZEROCOPY_VERSION_1));
1799 
1800 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1801 		    zc_ic->zerocopy_version;
1802 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1803 		    zc_ic->zerocopy_flags;
1804 
1805 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1806 	} else {
1807 		uint_t size;
1808 		uchar_t *rptr;
1809 
1810 		size = sizeof (dl_capability_req_t) +
1811 		    sizeof (dl_capability_sub_t) +
1812 		    sizeof (dl_capab_zerocopy_t);
1813 
1814 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1815 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1816 			    "could not enable zerocopy for %s (ENOMEM)\n",
1817 			    ill->ill_name);
1818 			return;
1819 		}
1820 
1821 		rptr = nmp->b_rptr;
1822 		/* initialize dl_capability_req_t */
1823 		oc = (dl_capability_req_t *)rptr;
1824 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1825 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1826 		    sizeof (dl_capab_zerocopy_t);
1827 		rptr += sizeof (dl_capability_req_t);
1828 
1829 		/* initialize dl_capability_sub_t */
1830 		bcopy(isub, rptr, sizeof (*isub));
1831 		rptr += sizeof (*isub);
1832 
1833 		/* initialize dl_capab_zerocopy_t */
1834 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1835 		*zc_oc = *zc_ic;
1836 
1837 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1838 		    "to enable zero-copy version %d\n", ill->ill_name,
1839 		    ZEROCOPY_VERSION_1));
1840 
1841 		/* set VMSAFE_MEM flag */
1842 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1843 
1844 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1845 		ill_capability_send(ill, nmp);
1846 	}
1847 }
1848 
1849 static void
1850 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1851 {
1852 	dl_capab_zerocopy_t *zerocopy_subcap;
1853 	dl_capability_sub_t *dl_subcap;
1854 
1855 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1856 		return;
1857 
1858 	ASSERT(ill->ill_zerocopy_capab != NULL);
1859 
1860 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1861 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1862 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1863 
1864 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1865 	zerocopy_subcap->zerocopy_version =
1866 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1867 	zerocopy_subcap->zerocopy_flags = 0;
1868 
1869 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1870 }
1871 
1872 /*
1873  * DLD capability
1874  * Refer to dld.h for more information regarding the purpose and usage
1875  * of this capability.
1876  */
1877 static void
1878 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1879 {
1880 	dl_capab_dld_t		*dld_ic, dld;
1881 	uint_t			sub_dl_cap = isub->dl_cap;
1882 	uint8_t			*capend;
1883 	ill_dld_capab_t		*idc;
1884 
1885 	ASSERT(IAM_WRITER_ILL(ill));
1886 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1887 
1888 	/*
1889 	 * Note: range checks here are not absolutely sufficient to
1890 	 * make us robust against malformed messages sent by drivers;
1891 	 * this is in keeping with the rest of IP's dlpi handling.
1892 	 * (Remember, it's coming from something else in the kernel
1893 	 * address space)
1894 	 */
1895 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1896 	if (capend > mp->b_wptr) {
1897 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1898 		    "malformed sub-capability too long for mblk");
1899 		return;
1900 	}
1901 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1902 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1903 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1904 		    "unsupported DLD sub-capability (version %d, "
1905 		    "expected %d)", dld_ic->dld_version,
1906 		    DLD_CURRENT_VERSION);
1907 		return;
1908 	}
1909 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1910 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1911 		    "capability isn't as expected; pass-thru module(s) "
1912 		    "detected, discarding capability\n"));
1913 		return;
1914 	}
1915 
1916 	/*
1917 	 * Copy locally to ensure alignment.
1918 	 */
1919 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1920 
1921 	if ((idc = ill->ill_dld_capab) == NULL) {
1922 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1923 		if (idc == NULL) {
1924 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1925 			    "could not enable DLD version %d "
1926 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1927 			    ill->ill_name);
1928 			return;
1929 		}
1930 		ill->ill_dld_capab = idc;
1931 	}
1932 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1933 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1934 	ip1dbg(("ill_capability_dld_ack: interface %s "
1935 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1936 
1937 	ill_capability_dld_enable(ill);
1938 }
1939 
1940 /*
1941  * Typically capability negotiation between IP and the driver happens via
1942  * DLPI message exchange. However GLD also offers a direct function call
1943  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1944  * But arbitrary function calls into IP or GLD are not permitted, since both
1945  * of them are protected by their own perimeter mechanism. The perimeter can
1946  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1947  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1948  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1949  * to enter the mac perimeter and then do the direct function calls into
1950  * GLD to enable squeue polling. The ring related callbacks from the mac into
1951  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1952  * protected by the mac perimeter.
1953  */
1954 static void
1955 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1956 {
1957 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1958 	int			err;
1959 
1960 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1961 	    DLD_ENABLE);
1962 	ASSERT(err == 0);
1963 }
1964 
1965 static void
1966 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1967 {
1968 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1969 	int			err;
1970 
1971 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1972 	    DLD_DISABLE);
1973 	ASSERT(err == 0);
1974 }
1975 
1976 boolean_t
1977 ill_mac_perim_held(ill_t *ill)
1978 {
1979 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1980 
1981 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1982 	    DLD_QUERY));
1983 }
1984 
1985 static void
1986 ill_capability_direct_enable(ill_t *ill)
1987 {
1988 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1989 	ill_dld_direct_t	*idd = &idc->idc_direct;
1990 	dld_capab_direct_t	direct;
1991 	int			rc;
1992 
1993 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1994 
1995 	bzero(&direct, sizeof (direct));
1996 	direct.di_rx_cf = (uintptr_t)ip_input;
1997 	direct.di_rx_ch = ill;
1998 
1999 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2000 	    DLD_ENABLE);
2001 	if (rc == 0) {
2002 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2003 		idd->idd_tx_dh = direct.di_tx_dh;
2004 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2005 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2006 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2007 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2008 		ASSERT(idd->idd_tx_cb_df != NULL);
2009 		ASSERT(idd->idd_tx_fctl_df != NULL);
2010 		ASSERT(idd->idd_tx_df != NULL);
2011 		/*
2012 		 * One time registration of flow enable callback function
2013 		 */
2014 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2015 		    ill_flow_enable, ill);
2016 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2017 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2018 	} else {
2019 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2020 		    "capability, rc = %d\n", rc);
2021 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2022 	}
2023 }
2024 
2025 static void
2026 ill_capability_poll_enable(ill_t *ill)
2027 {
2028 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2029 	dld_capab_poll_t	poll;
2030 	int			rc;
2031 
2032 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2033 
2034 	bzero(&poll, sizeof (poll));
2035 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2036 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2037 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2038 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2039 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2040 	poll.poll_ring_ch = ill;
2041 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2042 	    DLD_ENABLE);
2043 	if (rc == 0) {
2044 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2045 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2046 	} else {
2047 		ip1dbg(("warning: could not enable POLL "
2048 		    "capability, rc = %d\n", rc));
2049 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2050 	}
2051 }
2052 
2053 /*
2054  * Enable the LSO capability.
2055  */
2056 static void
2057 ill_capability_lso_enable(ill_t *ill)
2058 {
2059 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2060 	dld_capab_lso_t	lso;
2061 	int rc;
2062 
2063 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2064 
2065 	if (ill->ill_lso_capab == NULL) {
2066 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2067 		    KM_NOSLEEP);
2068 		if (ill->ill_lso_capab == NULL) {
2069 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2070 			    "could not enable LSO for %s (ENOMEM)\n",
2071 			    ill->ill_name);
2072 			return;
2073 		}
2074 	}
2075 
2076 	bzero(&lso, sizeof (lso));
2077 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2078 	    DLD_ENABLE)) == 0) {
2079 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2080 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2081 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2082 		ip1dbg(("ill_capability_lso_enable: interface %s "
2083 		    "has enabled LSO\n ", ill->ill_name));
2084 	} else {
2085 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2086 		ill->ill_lso_capab = NULL;
2087 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2088 	}
2089 }
2090 
2091 static void
2092 ill_capability_dld_enable(ill_t *ill)
2093 {
2094 	mac_perim_handle_t mph;
2095 
2096 	ASSERT(IAM_WRITER_ILL(ill));
2097 
2098 	if (ill->ill_isv6)
2099 		return;
2100 
2101 	ill_mac_perim_enter(ill, &mph);
2102 	if (!ill->ill_isv6) {
2103 		ill_capability_direct_enable(ill);
2104 		ill_capability_poll_enable(ill);
2105 		ill_capability_lso_enable(ill);
2106 	}
2107 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2108 	ill_mac_perim_exit(ill, mph);
2109 }
2110 
2111 static void
2112 ill_capability_dld_disable(ill_t *ill)
2113 {
2114 	ill_dld_capab_t	*idc;
2115 	ill_dld_direct_t *idd;
2116 	mac_perim_handle_t	mph;
2117 
2118 	ASSERT(IAM_WRITER_ILL(ill));
2119 
2120 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2121 		return;
2122 
2123 	ill_mac_perim_enter(ill, &mph);
2124 
2125 	idc = ill->ill_dld_capab;
2126 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2127 		/*
2128 		 * For performance we avoid locks in the transmit data path
2129 		 * and don't maintain a count of the number of threads using
2130 		 * direct calls. Thus some threads could be using direct
2131 		 * transmit calls to GLD, even after the capability mechanism
2132 		 * turns it off. This is still safe since the handles used in
2133 		 * the direct calls continue to be valid until the unplumb is
2134 		 * completed. Remove the callback that was added (1-time) at
2135 		 * capab enable time.
2136 		 */
2137 		mutex_enter(&ill->ill_lock);
2138 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2139 		mutex_exit(&ill->ill_lock);
2140 		if (ill->ill_flownotify_mh != NULL) {
2141 			idd = &idc->idc_direct;
2142 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2143 			    ill->ill_flownotify_mh);
2144 			ill->ill_flownotify_mh = NULL;
2145 		}
2146 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2147 		    NULL, DLD_DISABLE);
2148 	}
2149 
2150 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2151 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2152 		ip_squeue_clean_all(ill);
2153 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2154 		    NULL, DLD_DISABLE);
2155 	}
2156 
2157 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2158 		ASSERT(ill->ill_lso_capab != NULL);
2159 		/*
2160 		 * Clear the capability flag for LSO but retain the
2161 		 * ill_lso_capab structure since it's possible that another
2162 		 * thread is still referring to it.  The structure only gets
2163 		 * deallocated when we destroy the ill.
2164 		 */
2165 
2166 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2167 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2168 		    NULL, DLD_DISABLE);
2169 	}
2170 
2171 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2172 	ill_mac_perim_exit(ill, mph);
2173 }
2174 
2175 /*
2176  * Capability Negotiation protocol
2177  *
2178  * We don't wait for DLPI capability operations to finish during interface
2179  * bringup or teardown. Doing so would introduce more asynchrony and the
2180  * interface up/down operations will need multiple return and restarts.
2181  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2182  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2183  * exclusive operation won't start until the DLPI operations of the previous
2184  * exclusive operation complete.
2185  *
2186  * The capability state machine is shown below.
2187  *
2188  * state		next state		event, action
2189  *
2190  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2191  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2192  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2193  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2194  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2195  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2196  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2197  *						    ill_capability_probe.
2198  */
2199 
2200 /*
2201  * Dedicated thread started from ip_stack_init that handles capability
2202  * disable. This thread ensures the taskq dispatch does not fail by waiting
2203  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2204  * that direct calls to DLD are done in a cv_waitable context.
2205  */
2206 void
2207 ill_taskq_dispatch(ip_stack_t *ipst)
2208 {
2209 	callb_cpr_t cprinfo;
2210 	char 	name[64];
2211 	mblk_t	*mp;
2212 
2213 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2214 	    ipst->ips_netstack->netstack_stackid);
2215 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2216 	    name);
2217 	mutex_enter(&ipst->ips_capab_taskq_lock);
2218 
2219 	for (;;) {
2220 		mp = ipst->ips_capab_taskq_head;
2221 		while (mp != NULL) {
2222 			ipst->ips_capab_taskq_head = mp->b_next;
2223 			if (ipst->ips_capab_taskq_head == NULL)
2224 				ipst->ips_capab_taskq_tail = NULL;
2225 			mutex_exit(&ipst->ips_capab_taskq_lock);
2226 			mp->b_next = NULL;
2227 
2228 			VERIFY(taskq_dispatch(system_taskq,
2229 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2230 			mutex_enter(&ipst->ips_capab_taskq_lock);
2231 			mp = ipst->ips_capab_taskq_head;
2232 		}
2233 
2234 		if (ipst->ips_capab_taskq_quit)
2235 			break;
2236 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2237 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2238 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2239 	}
2240 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2241 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2242 	CALLB_CPR_EXIT(&cprinfo);
2243 	thread_exit();
2244 }
2245 
2246 /*
2247  * Consume a new-style hardware capabilities negotiation ack.
2248  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2249  */
2250 static void
2251 ill_capability_ack_thr(void *arg)
2252 {
2253 	mblk_t	*mp = arg;
2254 	dl_capability_ack_t *capp;
2255 	dl_capability_sub_t *subp, *endp;
2256 	ill_t	*ill;
2257 	boolean_t reneg;
2258 
2259 	ill = (ill_t *)mp->b_prev;
2260 	mp->b_prev = NULL;
2261 
2262 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2263 
2264 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2265 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2266 		/*
2267 		 * We have received the ack for our DL_CAPAB reset request.
2268 		 * There isnt' anything in the message that needs processing.
2269 		 * All message based capabilities have been disabled, now
2270 		 * do the function call based capability disable.
2271 		 */
2272 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2273 		ill_capability_dld_disable(ill);
2274 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2275 		if (reneg)
2276 			ill_capability_probe(ill);
2277 		goto done;
2278 	}
2279 
2280 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2281 		ill->ill_dlpi_capab_state = IDCS_OK;
2282 
2283 	capp = (dl_capability_ack_t *)mp->b_rptr;
2284 
2285 	if (capp->dl_sub_length == 0) {
2286 		/* no new-style capabilities */
2287 		goto done;
2288 	}
2289 
2290 	/* make sure the driver supplied correct dl_sub_length */
2291 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2292 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2293 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2294 		goto done;
2295 	}
2296 
2297 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2298 	/*
2299 	 * There are sub-capabilities. Process the ones we know about.
2300 	 * Loop until we don't have room for another sub-cap header..
2301 	 */
2302 	for (subp = SC(capp, capp->dl_sub_offset),
2303 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2304 	    subp <= endp;
2305 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2306 
2307 		switch (subp->dl_cap) {
2308 		case DL_CAPAB_ID_WRAPPER:
2309 			ill_capability_id_ack(ill, mp, subp);
2310 			break;
2311 		default:
2312 			ill_capability_dispatch(ill, mp, subp);
2313 			break;
2314 		}
2315 	}
2316 #undef SC
2317 done:
2318 	inet_freemsg(mp);
2319 	ill_capability_done(ill);
2320 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2321 }
2322 
2323 /*
2324  * This needs to be started in a taskq thread to provide a cv_waitable
2325  * context.
2326  */
2327 void
2328 ill_capability_ack(ill_t *ill, mblk_t *mp)
2329 {
2330 	ip_stack_t	*ipst = ill->ill_ipst;
2331 
2332 	mp->b_prev = (mblk_t *)ill;
2333 	ASSERT(mp->b_next == NULL);
2334 
2335 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2336 	    TQ_NOSLEEP) != 0)
2337 		return;
2338 
2339 	/*
2340 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2341 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2342 	 */
2343 	mutex_enter(&ipst->ips_capab_taskq_lock);
2344 	if (ipst->ips_capab_taskq_head == NULL) {
2345 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2346 		ipst->ips_capab_taskq_head = mp;
2347 	} else {
2348 		ipst->ips_capab_taskq_tail->b_next = mp;
2349 	}
2350 	ipst->ips_capab_taskq_tail = mp;
2351 
2352 	cv_signal(&ipst->ips_capab_taskq_cv);
2353 	mutex_exit(&ipst->ips_capab_taskq_lock);
2354 }
2355 
2356 /*
2357  * This routine is called to scan the fragmentation reassembly table for
2358  * the specified ILL for any packets that are starting to smell.
2359  * dead_interval is the maximum time in seconds that will be tolerated.  It
2360  * will either be the value specified in ip_g_frag_timeout, or zero if the
2361  * ILL is shutting down and it is time to blow everything off.
2362  *
2363  * It returns the number of seconds (as a time_t) that the next frag timer
2364  * should be scheduled for, 0 meaning that the timer doesn't need to be
2365  * re-started.  Note that the method of calculating next_timeout isn't
2366  * entirely accurate since time will flow between the time we grab
2367  * current_time and the time we schedule the next timeout.  This isn't a
2368  * big problem since this is the timer for sending an ICMP reassembly time
2369  * exceeded messages, and it doesn't have to be exactly accurate.
2370  *
2371  * This function is
2372  * sometimes called as writer, although this is not required.
2373  */
2374 time_t
2375 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2376 {
2377 	ipfb_t	*ipfb;
2378 	ipfb_t	*endp;
2379 	ipf_t	*ipf;
2380 	ipf_t	*ipfnext;
2381 	mblk_t	*mp;
2382 	time_t	current_time = gethrestime_sec();
2383 	time_t	next_timeout = 0;
2384 	uint32_t	hdr_length;
2385 	mblk_t	*send_icmp_head;
2386 	mblk_t	*send_icmp_head_v6;
2387 	ip_stack_t *ipst = ill->ill_ipst;
2388 	ip_recv_attr_t iras;
2389 
2390 	bzero(&iras, sizeof (iras));
2391 	iras.ira_flags = 0;
2392 	iras.ira_ill = iras.ira_rill = ill;
2393 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2394 	iras.ira_rifindex = iras.ira_ruifindex;
2395 
2396 	ipfb = ill->ill_frag_hash_tbl;
2397 	if (ipfb == NULL)
2398 		return (B_FALSE);
2399 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2400 	/* Walk the frag hash table. */
2401 	for (; ipfb < endp; ipfb++) {
2402 		send_icmp_head = NULL;
2403 		send_icmp_head_v6 = NULL;
2404 		mutex_enter(&ipfb->ipfb_lock);
2405 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2406 			time_t frag_time = current_time - ipf->ipf_timestamp;
2407 			time_t frag_timeout;
2408 
2409 			if (frag_time < dead_interval) {
2410 				/*
2411 				 * There are some outstanding fragments
2412 				 * that will timeout later.  Make note of
2413 				 * the time so that we can reschedule the
2414 				 * next timeout appropriately.
2415 				 */
2416 				frag_timeout = dead_interval - frag_time;
2417 				if (next_timeout == 0 ||
2418 				    frag_timeout < next_timeout) {
2419 					next_timeout = frag_timeout;
2420 				}
2421 				break;
2422 			}
2423 			/* Time's up.  Get it out of here. */
2424 			hdr_length = ipf->ipf_nf_hdr_len;
2425 			ipfnext = ipf->ipf_hash_next;
2426 			if (ipfnext)
2427 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2428 			*ipf->ipf_ptphn = ipfnext;
2429 			mp = ipf->ipf_mp->b_cont;
2430 			for (; mp; mp = mp->b_cont) {
2431 				/* Extra points for neatness. */
2432 				IP_REASS_SET_START(mp, 0);
2433 				IP_REASS_SET_END(mp, 0);
2434 			}
2435 			mp = ipf->ipf_mp->b_cont;
2436 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2437 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2438 			ipfb->ipfb_count -= ipf->ipf_count;
2439 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2440 			ipfb->ipfb_frag_pkts--;
2441 			/*
2442 			 * We do not send any icmp message from here because
2443 			 * we currently are holding the ipfb_lock for this
2444 			 * hash chain. If we try and send any icmp messages
2445 			 * from here we may end up via a put back into ip
2446 			 * trying to get the same lock, causing a recursive
2447 			 * mutex panic. Instead we build a list and send all
2448 			 * the icmp messages after we have dropped the lock.
2449 			 */
2450 			if (ill->ill_isv6) {
2451 				if (hdr_length != 0) {
2452 					mp->b_next = send_icmp_head_v6;
2453 					send_icmp_head_v6 = mp;
2454 				} else {
2455 					freemsg(mp);
2456 				}
2457 			} else {
2458 				if (hdr_length != 0) {
2459 					mp->b_next = send_icmp_head;
2460 					send_icmp_head = mp;
2461 				} else {
2462 					freemsg(mp);
2463 				}
2464 			}
2465 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2466 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2467 			freeb(ipf->ipf_mp);
2468 		}
2469 		mutex_exit(&ipfb->ipfb_lock);
2470 		/*
2471 		 * Now need to send any icmp messages that we delayed from
2472 		 * above.
2473 		 */
2474 		while (send_icmp_head_v6 != NULL) {
2475 			ip6_t *ip6h;
2476 
2477 			mp = send_icmp_head_v6;
2478 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2479 			mp->b_next = NULL;
2480 			ip6h = (ip6_t *)mp->b_rptr;
2481 			iras.ira_flags = 0;
2482 			/*
2483 			 * This will result in an incorrect ALL_ZONES zoneid
2484 			 * for multicast packets, but we
2485 			 * don't send ICMP errors for those in any case.
2486 			 */
2487 			iras.ira_zoneid =
2488 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2489 			    ill, ipst);
2490 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2491 			icmp_time_exceeded_v6(mp,
2492 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2493 			    &iras);
2494 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2495 		}
2496 		while (send_icmp_head != NULL) {
2497 			ipaddr_t dst;
2498 
2499 			mp = send_icmp_head;
2500 			send_icmp_head = send_icmp_head->b_next;
2501 			mp->b_next = NULL;
2502 
2503 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2504 
2505 			iras.ira_flags = IRAF_IS_IPV4;
2506 			/*
2507 			 * This will result in an incorrect ALL_ZONES zoneid
2508 			 * for broadcast and multicast packets, but we
2509 			 * don't send ICMP errors for those in any case.
2510 			 */
2511 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2512 			    ill, ipst);
2513 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2514 			icmp_time_exceeded(mp,
2515 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2516 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2517 		}
2518 	}
2519 	/*
2520 	 * A non-dying ILL will use the return value to decide whether to
2521 	 * restart the frag timer, and for how long.
2522 	 */
2523 	return (next_timeout);
2524 }
2525 
2526 /*
2527  * This routine is called when the approximate count of mblk memory used
2528  * for the specified ILL has exceeded max_count.
2529  */
2530 void
2531 ill_frag_prune(ill_t *ill, uint_t max_count)
2532 {
2533 	ipfb_t	*ipfb;
2534 	ipf_t	*ipf;
2535 	size_t	count;
2536 	clock_t now;
2537 
2538 	/*
2539 	 * If we are here within ip_min_frag_prune_time msecs remove
2540 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2541 	 * ill_frag_free_num_pkts.
2542 	 */
2543 	mutex_enter(&ill->ill_lock);
2544 	now = ddi_get_lbolt();
2545 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2546 	    (ip_min_frag_prune_time != 0 ?
2547 	    ip_min_frag_prune_time : msec_per_tick)) {
2548 
2549 		ill->ill_frag_free_num_pkts++;
2550 
2551 	} else {
2552 		ill->ill_frag_free_num_pkts = 0;
2553 	}
2554 	ill->ill_last_frag_clean_time = now;
2555 	mutex_exit(&ill->ill_lock);
2556 
2557 	/*
2558 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2559 	 */
2560 	if (ill->ill_frag_free_num_pkts != 0) {
2561 		int ix;
2562 
2563 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2564 			ipfb = &ill->ill_frag_hash_tbl[ix];
2565 			mutex_enter(&ipfb->ipfb_lock);
2566 			if (ipfb->ipfb_ipf != NULL) {
2567 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2568 				    ill->ill_frag_free_num_pkts);
2569 			}
2570 			mutex_exit(&ipfb->ipfb_lock);
2571 		}
2572 	}
2573 	/*
2574 	 * While the reassembly list for this ILL is too big, prune a fragment
2575 	 * queue by age, oldest first.
2576 	 */
2577 	while (ill->ill_frag_count > max_count) {
2578 		int	ix;
2579 		ipfb_t	*oipfb = NULL;
2580 		uint_t	oldest = UINT_MAX;
2581 
2582 		count = 0;
2583 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2584 			ipfb = &ill->ill_frag_hash_tbl[ix];
2585 			mutex_enter(&ipfb->ipfb_lock);
2586 			ipf = ipfb->ipfb_ipf;
2587 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2588 				oldest = ipf->ipf_gen;
2589 				oipfb = ipfb;
2590 			}
2591 			count += ipfb->ipfb_count;
2592 			mutex_exit(&ipfb->ipfb_lock);
2593 		}
2594 		if (oipfb == NULL)
2595 			break;
2596 
2597 		if (count <= max_count)
2598 			return;	/* Somebody beat us to it, nothing to do */
2599 		mutex_enter(&oipfb->ipfb_lock);
2600 		ipf = oipfb->ipfb_ipf;
2601 		if (ipf != NULL) {
2602 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2603 		}
2604 		mutex_exit(&oipfb->ipfb_lock);
2605 	}
2606 }
2607 
2608 /*
2609  * free 'free_cnt' fragmented packets starting at ipf.
2610  */
2611 void
2612 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2613 {
2614 	size_t	count;
2615 	mblk_t	*mp;
2616 	mblk_t	*tmp;
2617 	ipf_t **ipfp = ipf->ipf_ptphn;
2618 
2619 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2620 	ASSERT(ipfp != NULL);
2621 	ASSERT(ipf != NULL);
2622 
2623 	while (ipf != NULL && free_cnt-- > 0) {
2624 		count = ipf->ipf_count;
2625 		mp = ipf->ipf_mp;
2626 		ipf = ipf->ipf_hash_next;
2627 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2628 			IP_REASS_SET_START(tmp, 0);
2629 			IP_REASS_SET_END(tmp, 0);
2630 		}
2631 		atomic_add_32(&ill->ill_frag_count, -count);
2632 		ASSERT(ipfb->ipfb_count >= count);
2633 		ipfb->ipfb_count -= count;
2634 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2635 		ipfb->ipfb_frag_pkts--;
2636 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2637 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2638 		freemsg(mp);
2639 	}
2640 
2641 	if (ipf)
2642 		ipf->ipf_ptphn = ipfp;
2643 	ipfp[0] = ipf;
2644 }
2645 
2646 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2647 	"obsolete and may be removed in a future release of Solaris.  Use " \
2648 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2649 
2650 /*
2651  * For obsolete per-interface forwarding configuration;
2652  * called in response to ND_GET.
2653  */
2654 /* ARGSUSED */
2655 static int
2656 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2657 {
2658 	ill_t *ill = (ill_t *)cp;
2659 
2660 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2661 
2662 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2663 	return (0);
2664 }
2665 
2666 /*
2667  * For obsolete per-interface forwarding configuration;
2668  * called in response to ND_SET.
2669  */
2670 /* ARGSUSED */
2671 static int
2672 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2673     cred_t *ioc_cr)
2674 {
2675 	long value;
2676 	int retval;
2677 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2678 
2679 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2680 
2681 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2682 	    value < 0 || value > 1) {
2683 		return (EINVAL);
2684 	}
2685 
2686 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2687 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2688 	rw_exit(&ipst->ips_ill_g_lock);
2689 	return (retval);
2690 }
2691 
2692 /*
2693  * Helper function for ill_forward_set().
2694  */
2695 static void
2696 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2697 {
2698 	ip_stack_t	*ipst = ill->ill_ipst;
2699 
2700 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2701 
2702 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2703 	    (enable ? "Enabling" : "Disabling"),
2704 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2705 	mutex_enter(&ill->ill_lock);
2706 	if (enable)
2707 		ill->ill_flags |= ILLF_ROUTER;
2708 	else
2709 		ill->ill_flags &= ~ILLF_ROUTER;
2710 	mutex_exit(&ill->ill_lock);
2711 	if (ill->ill_isv6)
2712 		ill_set_nce_router_flags(ill, enable);
2713 	/* Notify routing socket listeners of this change. */
2714 	if (ill->ill_ipif != NULL)
2715 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2716 }
2717 
2718 /*
2719  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2720  * socket messages for each interface whose flags we change.
2721  */
2722 int
2723 ill_forward_set(ill_t *ill, boolean_t enable)
2724 {
2725 	ipmp_illgrp_t *illg;
2726 	ip_stack_t *ipst = ill->ill_ipst;
2727 
2728 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2729 
2730 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2731 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2732 		return (0);
2733 
2734 	if (IS_LOOPBACK(ill))
2735 		return (EINVAL);
2736 
2737 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2738 		/*
2739 		 * Update all of the interfaces in the group.
2740 		 */
2741 		illg = ill->ill_grp;
2742 		ill = list_head(&illg->ig_if);
2743 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2744 			ill_forward_set_on_ill(ill, enable);
2745 
2746 		/*
2747 		 * Update the IPMP meta-interface.
2748 		 */
2749 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2750 		return (0);
2751 	}
2752 
2753 	ill_forward_set_on_ill(ill, enable);
2754 	return (0);
2755 }
2756 
2757 /*
2758  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2759  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2760  * set or clear.
2761  */
2762 static void
2763 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2764 {
2765 	ipif_t *ipif;
2766 	ncec_t *ncec;
2767 	nce_t *nce;
2768 
2769 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2770 		/*
2771 		 * NOTE: we match across the illgrp because nce's for
2772 		 * addresses on IPMP interfaces have an nce_ill that points to
2773 		 * the bound underlying ill.
2774 		 */
2775 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2776 		if (nce != NULL) {
2777 			ncec = nce->nce_common;
2778 			mutex_enter(&ncec->ncec_lock);
2779 			if (enable)
2780 				ncec->ncec_flags |= NCE_F_ISROUTER;
2781 			else
2782 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2783 			mutex_exit(&ncec->ncec_lock);
2784 			nce_refrele(nce);
2785 		}
2786 	}
2787 }
2788 
2789 /*
2790  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2791  * for this ill.  Make sure the v6/v4 question has been answered about this
2792  * ill.  The creation of this ndd variable is only for backwards compatibility.
2793  * The preferred way to control per-interface IP forwarding is through the
2794  * ILLF_ROUTER interface flag.
2795  */
2796 static int
2797 ill_set_ndd_name(ill_t *ill)
2798 {
2799 	char *suffix;
2800 	ip_stack_t	*ipst = ill->ill_ipst;
2801 
2802 	ASSERT(IAM_WRITER_ILL(ill));
2803 
2804 	if (ill->ill_isv6)
2805 		suffix = ipv6_forward_suffix;
2806 	else
2807 		suffix = ipv4_forward_suffix;
2808 
2809 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2810 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2811 	/*
2812 	 * Copies over the '\0'.
2813 	 * Note that strlen(suffix) is always bounded.
2814 	 */
2815 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2816 	    strlen(suffix) + 1);
2817 
2818 	/*
2819 	 * Use of the nd table requires holding the reader lock.
2820 	 * Modifying the nd table thru nd_load/nd_unload requires
2821 	 * the writer lock.
2822 	 */
2823 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2824 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2825 	    nd_ill_forward_set, (caddr_t)ill)) {
2826 		/*
2827 		 * If the nd_load failed, it only meant that it could not
2828 		 * allocate a new bunch of room for further NDD expansion.
2829 		 * Because of that, the ill_ndd_name will be set to 0, and
2830 		 * this interface is at the mercy of the global ip_forwarding
2831 		 * variable.
2832 		 */
2833 		rw_exit(&ipst->ips_ip_g_nd_lock);
2834 		ill->ill_ndd_name = NULL;
2835 		return (ENOMEM);
2836 	}
2837 	rw_exit(&ipst->ips_ip_g_nd_lock);
2838 	return (0);
2839 }
2840 
2841 /*
2842  * Intializes the context structure and returns the first ill in the list
2843  * cuurently start_list and end_list can have values:
2844  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2845  * IP_V4_G_HEAD		Traverse IPV4 list only.
2846  * IP_V6_G_HEAD		Traverse IPV6 list only.
2847  */
2848 
2849 /*
2850  * We don't check for CONDEMNED ills here. Caller must do that if
2851  * necessary under the ill lock.
2852  */
2853 ill_t *
2854 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2855     ip_stack_t *ipst)
2856 {
2857 	ill_if_t *ifp;
2858 	ill_t *ill;
2859 	avl_tree_t *avl_tree;
2860 
2861 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2862 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2863 
2864 	/*
2865 	 * setup the lists to search
2866 	 */
2867 	if (end_list != MAX_G_HEADS) {
2868 		ctx->ctx_current_list = start_list;
2869 		ctx->ctx_last_list = end_list;
2870 	} else {
2871 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2872 		ctx->ctx_current_list = 0;
2873 	}
2874 
2875 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2876 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2877 		if (ifp != (ill_if_t *)
2878 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2879 			avl_tree = &ifp->illif_avl_by_ppa;
2880 			ill = avl_first(avl_tree);
2881 			/*
2882 			 * ill is guaranteed to be non NULL or ifp should have
2883 			 * not existed.
2884 			 */
2885 			ASSERT(ill != NULL);
2886 			return (ill);
2887 		}
2888 		ctx->ctx_current_list++;
2889 	}
2890 
2891 	return (NULL);
2892 }
2893 
2894 /*
2895  * returns the next ill in the list. ill_first() must have been called
2896  * before calling ill_next() or bad things will happen.
2897  */
2898 
2899 /*
2900  * We don't check for CONDEMNED ills here. Caller must do that if
2901  * necessary under the ill lock.
2902  */
2903 ill_t *
2904 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2905 {
2906 	ill_if_t *ifp;
2907 	ill_t *ill;
2908 	ip_stack_t	*ipst = lastill->ill_ipst;
2909 
2910 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2911 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2912 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2913 	    AVL_AFTER)) != NULL) {
2914 		return (ill);
2915 	}
2916 
2917 	/* goto next ill_ifp in the list. */
2918 	ifp = lastill->ill_ifptr->illif_next;
2919 
2920 	/* make sure not at end of circular list */
2921 	while (ifp ==
2922 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2923 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2924 			return (NULL);
2925 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2926 	}
2927 
2928 	return (avl_first(&ifp->illif_avl_by_ppa));
2929 }
2930 
2931 /*
2932  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2933  * The final number (PPA) must not have any leading zeros.  Upon success, a
2934  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2935  */
2936 static char *
2937 ill_get_ppa_ptr(char *name)
2938 {
2939 	int namelen = strlen(name);
2940 	int end_ndx = namelen - 1;
2941 	int ppa_ndx, i;
2942 
2943 	/*
2944 	 * Check that the first character is [a-zA-Z], and that the last
2945 	 * character is [0-9].
2946 	 */
2947 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2948 		return (NULL);
2949 
2950 	/*
2951 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2952 	 */
2953 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2954 		if (!isdigit(name[ppa_ndx - 1]))
2955 			break;
2956 
2957 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2958 		return (NULL);
2959 
2960 	/*
2961 	 * Check that the intermediate characters are [a-z0-9.]
2962 	 */
2963 	for (i = 1; i < ppa_ndx; i++) {
2964 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2965 		    name[i] != '.' && name[i] != '_') {
2966 			return (NULL);
2967 		}
2968 	}
2969 
2970 	return (name + ppa_ndx);
2971 }
2972 
2973 /*
2974  * use avl tree to locate the ill.
2975  */
2976 static ill_t *
2977 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2978 {
2979 	char *ppa_ptr = NULL;
2980 	int len;
2981 	uint_t ppa;
2982 	ill_t *ill = NULL;
2983 	ill_if_t *ifp;
2984 	int list;
2985 
2986 	/*
2987 	 * get ppa ptr
2988 	 */
2989 	if (isv6)
2990 		list = IP_V6_G_HEAD;
2991 	else
2992 		list = IP_V4_G_HEAD;
2993 
2994 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2995 		return (NULL);
2996 	}
2997 
2998 	len = ppa_ptr - name + 1;
2999 
3000 	ppa = stoi(&ppa_ptr);
3001 
3002 	ifp = IP_VX_ILL_G_LIST(list, ipst);
3003 
3004 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3005 		/*
3006 		 * match is done on len - 1 as the name is not null
3007 		 * terminated it contains ppa in addition to the interface
3008 		 * name.
3009 		 */
3010 		if ((ifp->illif_name_len == len) &&
3011 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3012 			break;
3013 		} else {
3014 			ifp = ifp->illif_next;
3015 		}
3016 	}
3017 
3018 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3019 		/*
3020 		 * Even the interface type does not exist.
3021 		 */
3022 		return (NULL);
3023 	}
3024 
3025 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
3026 	if (ill != NULL) {
3027 		mutex_enter(&ill->ill_lock);
3028 		if (ILL_CAN_LOOKUP(ill)) {
3029 			ill_refhold_locked(ill);
3030 			mutex_exit(&ill->ill_lock);
3031 			return (ill);
3032 		}
3033 		mutex_exit(&ill->ill_lock);
3034 	}
3035 	return (NULL);
3036 }
3037 
3038 /*
3039  * comparison function for use with avl.
3040  */
3041 static int
3042 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3043 {
3044 	uint_t ppa;
3045 	uint_t ill_ppa;
3046 
3047 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3048 
3049 	ppa = *((uint_t *)ppa_ptr);
3050 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3051 	/*
3052 	 * We want the ill with the lowest ppa to be on the
3053 	 * top.
3054 	 */
3055 	if (ill_ppa < ppa)
3056 		return (1);
3057 	if (ill_ppa > ppa)
3058 		return (-1);
3059 	return (0);
3060 }
3061 
3062 /*
3063  * remove an interface type from the global list.
3064  */
3065 static void
3066 ill_delete_interface_type(ill_if_t *interface)
3067 {
3068 	ASSERT(interface != NULL);
3069 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3070 
3071 	avl_destroy(&interface->illif_avl_by_ppa);
3072 	if (interface->illif_ppa_arena != NULL)
3073 		vmem_destroy(interface->illif_ppa_arena);
3074 
3075 	remque(interface);
3076 
3077 	mi_free(interface);
3078 }
3079 
3080 /*
3081  * remove ill from the global list.
3082  */
3083 static void
3084 ill_glist_delete(ill_t *ill)
3085 {
3086 	ip_stack_t	*ipst;
3087 	phyint_t	*phyi;
3088 
3089 	if (ill == NULL)
3090 		return;
3091 	ipst = ill->ill_ipst;
3092 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3093 
3094 	/*
3095 	 * If the ill was never inserted into the AVL tree
3096 	 * we skip the if branch.
3097 	 */
3098 	if (ill->ill_ifptr != NULL) {
3099 		/*
3100 		 * remove from AVL tree and free ppa number
3101 		 */
3102 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3103 
3104 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3105 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3106 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3107 		}
3108 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3109 			ill_delete_interface_type(ill->ill_ifptr);
3110 		}
3111 
3112 		/*
3113 		 * Indicate ill is no longer in the list.
3114 		 */
3115 		ill->ill_ifptr = NULL;
3116 		ill->ill_name_length = 0;
3117 		ill->ill_name[0] = '\0';
3118 		ill->ill_ppa = UINT_MAX;
3119 	}
3120 
3121 	/* Generate one last event for this ill. */
3122 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3123 	    ill->ill_name_length);
3124 
3125 	ASSERT(ill->ill_phyint != NULL);
3126 	phyi = ill->ill_phyint;
3127 	ill->ill_phyint = NULL;
3128 
3129 	/*
3130 	 * ill_init allocates a phyint always to store the copy
3131 	 * of flags relevant to phyint. At that point in time, we could
3132 	 * not assign the name and hence phyint_illv4/v6 could not be
3133 	 * initialized. Later in ipif_set_values, we assign the name to
3134 	 * the ill, at which point in time we assign phyint_illv4/v6.
3135 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3136 	 */
3137 	if (ill->ill_flags & ILLF_IPV6)
3138 		phyi->phyint_illv6 = NULL;
3139 	else
3140 		phyi->phyint_illv4 = NULL;
3141 
3142 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3143 		rw_exit(&ipst->ips_ill_g_lock);
3144 		return;
3145 	}
3146 
3147 	/*
3148 	 * There are no ills left on this phyint; pull it out of the phyint
3149 	 * avl trees, and free it.
3150 	 */
3151 	if (phyi->phyint_ifindex > 0) {
3152 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3153 		    phyi);
3154 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3155 		    phyi);
3156 	}
3157 	rw_exit(&ipst->ips_ill_g_lock);
3158 
3159 	phyint_free(phyi);
3160 }
3161 
3162 /*
3163  * allocate a ppa, if the number of plumbed interfaces of this type are
3164  * less than ill_no_arena do a linear search to find a unused ppa.
3165  * When the number goes beyond ill_no_arena switch to using an arena.
3166  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3167  * is the return value for an error condition, so allocation starts at one
3168  * and is decremented by one.
3169  */
3170 static int
3171 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3172 {
3173 	ill_t *tmp_ill;
3174 	uint_t start, end;
3175 	int ppa;
3176 
3177 	if (ifp->illif_ppa_arena == NULL &&
3178 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3179 		/*
3180 		 * Create an arena.
3181 		 */
3182 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3183 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3184 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3185 			/* allocate what has already been assigned */
3186 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3187 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3188 		    tmp_ill, AVL_AFTER)) {
3189 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3190 			    1,		/* size */
3191 			    1,		/* align/quantum */
3192 			    0,		/* phase */
3193 			    0,		/* nocross */
3194 			    /* minaddr */
3195 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3196 			    /* maxaddr */
3197 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3198 			    VM_NOSLEEP|VM_FIRSTFIT);
3199 			if (ppa == 0) {
3200 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3201 				    " failed while switching"));
3202 				vmem_destroy(ifp->illif_ppa_arena);
3203 				ifp->illif_ppa_arena = NULL;
3204 				break;
3205 			}
3206 		}
3207 	}
3208 
3209 	if (ifp->illif_ppa_arena != NULL) {
3210 		if (ill->ill_ppa == UINT_MAX) {
3211 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3212 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3213 			if (ppa == 0)
3214 				return (EAGAIN);
3215 			ill->ill_ppa = --ppa;
3216 		} else {
3217 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3218 			    1, 		/* size */
3219 			    1, 		/* align/quantum */
3220 			    0, 		/* phase */
3221 			    0, 		/* nocross */
3222 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3223 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3224 			    VM_NOSLEEP|VM_FIRSTFIT);
3225 			/*
3226 			 * Most likely the allocation failed because
3227 			 * the requested ppa was in use.
3228 			 */
3229 			if (ppa == 0)
3230 				return (EEXIST);
3231 		}
3232 		return (0);
3233 	}
3234 
3235 	/*
3236 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3237 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3238 	 */
3239 	if (ill->ill_ppa == UINT_MAX) {
3240 		end = UINT_MAX - 1;
3241 		start = 0;
3242 	} else {
3243 		end = start = ill->ill_ppa;
3244 	}
3245 
3246 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3247 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3248 		if (start++ >= end) {
3249 			if (ill->ill_ppa == UINT_MAX)
3250 				return (EAGAIN);
3251 			else
3252 				return (EEXIST);
3253 		}
3254 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3255 	}
3256 	ill->ill_ppa = start;
3257 	return (0);
3258 }
3259 
3260 /*
3261  * Insert ill into the list of configured ill's. Once this function completes,
3262  * the ill is globally visible and is available through lookups. More precisely
3263  * this happens after the caller drops the ill_g_lock.
3264  */
3265 static int
3266 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3267 {
3268 	ill_if_t *ill_interface;
3269 	avl_index_t where = 0;
3270 	int error;
3271 	int name_length;
3272 	int index;
3273 	boolean_t check_length = B_FALSE;
3274 	ip_stack_t	*ipst = ill->ill_ipst;
3275 
3276 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3277 
3278 	name_length = mi_strlen(name) + 1;
3279 
3280 	if (isv6)
3281 		index = IP_V6_G_HEAD;
3282 	else
3283 		index = IP_V4_G_HEAD;
3284 
3285 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3286 	/*
3287 	 * Search for interface type based on name
3288 	 */
3289 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3290 		if ((ill_interface->illif_name_len == name_length) &&
3291 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3292 			break;
3293 		}
3294 		ill_interface = ill_interface->illif_next;
3295 	}
3296 
3297 	/*
3298 	 * Interface type not found, create one.
3299 	 */
3300 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3301 		ill_g_head_t ghead;
3302 
3303 		/*
3304 		 * allocate ill_if_t structure
3305 		 */
3306 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3307 		if (ill_interface == NULL) {
3308 			return (ENOMEM);
3309 		}
3310 
3311 		(void) strcpy(ill_interface->illif_name, name);
3312 		ill_interface->illif_name_len = name_length;
3313 
3314 		avl_create(&ill_interface->illif_avl_by_ppa,
3315 		    ill_compare_ppa, sizeof (ill_t),
3316 		    offsetof(struct ill_s, ill_avl_byppa));
3317 
3318 		/*
3319 		 * link the structure in the back to maintain order
3320 		 * of configuration for ifconfig output.
3321 		 */
3322 		ghead = ipst->ips_ill_g_heads[index];
3323 		insque(ill_interface, ghead.ill_g_list_tail);
3324 	}
3325 
3326 	if (ill->ill_ppa == UINT_MAX)
3327 		check_length = B_TRUE;
3328 
3329 	error = ill_alloc_ppa(ill_interface, ill);
3330 	if (error != 0) {
3331 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3332 			ill_delete_interface_type(ill->ill_ifptr);
3333 		return (error);
3334 	}
3335 
3336 	/*
3337 	 * When the ppa is choosen by the system, check that there is
3338 	 * enough space to insert ppa. if a specific ppa was passed in this
3339 	 * check is not required as the interface name passed in will have
3340 	 * the right ppa in it.
3341 	 */
3342 	if (check_length) {
3343 		/*
3344 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3345 		 */
3346 		char buf[sizeof (uint_t) * 3];
3347 
3348 		/*
3349 		 * convert ppa to string to calculate the amount of space
3350 		 * required for it in the name.
3351 		 */
3352 		numtos(ill->ill_ppa, buf);
3353 
3354 		/* Do we have enough space to insert ppa ? */
3355 
3356 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3357 			/* Free ppa and interface type struct */
3358 			if (ill_interface->illif_ppa_arena != NULL) {
3359 				vmem_free(ill_interface->illif_ppa_arena,
3360 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3361 			}
3362 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3363 				ill_delete_interface_type(ill->ill_ifptr);
3364 
3365 			return (EINVAL);
3366 		}
3367 	}
3368 
3369 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3370 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3371 
3372 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3373 	    &where);
3374 	ill->ill_ifptr = ill_interface;
3375 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3376 
3377 	ill_phyint_reinit(ill);
3378 	return (0);
3379 }
3380 
3381 /* Initialize the per phyint ipsq used for serialization */
3382 static boolean_t
3383 ipsq_init(ill_t *ill, boolean_t enter)
3384 {
3385 	ipsq_t  *ipsq;
3386 	ipxop_t	*ipx;
3387 
3388 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3389 		return (B_FALSE);
3390 
3391 	ill->ill_phyint->phyint_ipsq = ipsq;
3392 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3393 	ipx->ipx_ipsq = ipsq;
3394 	ipsq->ipsq_next = ipsq;
3395 	ipsq->ipsq_phyint = ill->ill_phyint;
3396 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3397 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3398 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3399 	if (enter) {
3400 		ipx->ipx_writer = curthread;
3401 		ipx->ipx_forced = B_FALSE;
3402 		ipx->ipx_reentry_cnt = 1;
3403 #ifdef DEBUG
3404 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3405 #endif
3406 	}
3407 	return (B_TRUE);
3408 }
3409 
3410 /*
3411  * ill_init is called by ip_open when a device control stream is opened.
3412  * It does a few initializations, and shoots a DL_INFO_REQ message down
3413  * to the driver.  The response is later picked up in ip_rput_dlpi and
3414  * used to set up default mechanisms for talking to the driver.  (Always
3415  * called as writer.)
3416  *
3417  * If this function returns error, ip_open will call ip_close which in
3418  * turn will call ill_delete to clean up any memory allocated here that
3419  * is not yet freed.
3420  */
3421 int
3422 ill_init(queue_t *q, ill_t *ill)
3423 {
3424 	int	count;
3425 	dl_info_req_t	*dlir;
3426 	mblk_t	*info_mp;
3427 	uchar_t *frag_ptr;
3428 
3429 	/*
3430 	 * The ill is initialized to zero by mi_alloc*(). In addition
3431 	 * some fields already contain valid values, initialized in
3432 	 * ip_open(), before we reach here.
3433 	 */
3434 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3435 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3436 	ill->ill_saved_ire_cnt = 0;
3437 
3438 	ill->ill_rq = q;
3439 	ill->ill_wq = WR(q);
3440 
3441 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3442 	    BPRI_HI);
3443 	if (info_mp == NULL)
3444 		return (ENOMEM);
3445 
3446 	/*
3447 	 * Allocate sufficient space to contain our fragment hash table and
3448 	 * the device name.
3449 	 */
3450 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3451 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3452 	if (frag_ptr == NULL) {
3453 		freemsg(info_mp);
3454 		return (ENOMEM);
3455 	}
3456 	ill->ill_frag_ptr = frag_ptr;
3457 	ill->ill_frag_free_num_pkts = 0;
3458 	ill->ill_last_frag_clean_time = 0;
3459 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3460 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3461 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3462 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3463 		    NULL, MUTEX_DEFAULT, NULL);
3464 	}
3465 
3466 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3467 	if (ill->ill_phyint == NULL) {
3468 		freemsg(info_mp);
3469 		mi_free(frag_ptr);
3470 		return (ENOMEM);
3471 	}
3472 
3473 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3474 	/*
3475 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3476 	 * at this point because of the following reason. If we can't
3477 	 * enter the ipsq at some point and cv_wait, the writer that
3478 	 * wakes us up tries to locate us using the list of all phyints
3479 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3480 	 * If we don't set it now, we risk a missed wakeup.
3481 	 */
3482 	ill->ill_phyint->phyint_illv4 = ill;
3483 	ill->ill_ppa = UINT_MAX;
3484 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3485 
3486 	ill_set_inputfn(ill);
3487 
3488 	if (!ipsq_init(ill, B_TRUE)) {
3489 		freemsg(info_mp);
3490 		mi_free(frag_ptr);
3491 		mi_free(ill->ill_phyint);
3492 		return (ENOMEM);
3493 	}
3494 
3495 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3496 
3497 	/* Frag queue limit stuff */
3498 	ill->ill_frag_count = 0;
3499 	ill->ill_ipf_gen = 0;
3500 
3501 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3502 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3503 	ill->ill_global_timer = INFINITY;
3504 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3505 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3506 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3507 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3508 
3509 	/*
3510 	 * Initialize IPv6 configuration variables.  The IP module is always
3511 	 * opened as an IPv4 module.  Instead tracking down the cases where
3512 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3513 	 * here for convenience, this has no effect until the ill is set to do
3514 	 * IPv6.
3515 	 */
3516 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3517 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3518 	ill->ill_max_buf = ND_MAX_Q;
3519 	ill->ill_refcnt = 0;
3520 
3521 	/* Send down the Info Request to the driver. */
3522 	info_mp->b_datap->db_type = M_PCPROTO;
3523 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3524 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3525 	dlir->dl_primitive = DL_INFO_REQ;
3526 
3527 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3528 
3529 	qprocson(q);
3530 	ill_dlpi_send(ill, info_mp);
3531 
3532 	return (0);
3533 }
3534 
3535 /*
3536  * ill_dls_info
3537  * creates datalink socket info from the device.
3538  */
3539 int
3540 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3541 {
3542 	size_t	len;
3543 
3544 	sdl->sdl_family = AF_LINK;
3545 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3546 	sdl->sdl_type = ill->ill_type;
3547 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3548 	len = strlen(sdl->sdl_data);
3549 	ASSERT(len < 256);
3550 	sdl->sdl_nlen = (uchar_t)len;
3551 	sdl->sdl_alen = ill->ill_phys_addr_length;
3552 	sdl->sdl_slen = 0;
3553 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3554 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3555 
3556 	return (sizeof (struct sockaddr_dl));
3557 }
3558 
3559 /*
3560  * ill_xarp_info
3561  * creates xarp info from the device.
3562  */
3563 static int
3564 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3565 {
3566 	sdl->sdl_family = AF_LINK;
3567 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3568 	sdl->sdl_type = ill->ill_type;
3569 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3570 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3571 	sdl->sdl_alen = ill->ill_phys_addr_length;
3572 	sdl->sdl_slen = 0;
3573 	return (sdl->sdl_nlen);
3574 }
3575 
3576 static int
3577 loopback_kstat_update(kstat_t *ksp, int rw)
3578 {
3579 	kstat_named_t *kn;
3580 	netstackid_t	stackid;
3581 	netstack_t	*ns;
3582 	ip_stack_t	*ipst;
3583 
3584 	if (ksp == NULL || ksp->ks_data == NULL)
3585 		return (EIO);
3586 
3587 	if (rw == KSTAT_WRITE)
3588 		return (EACCES);
3589 
3590 	kn = KSTAT_NAMED_PTR(ksp);
3591 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3592 
3593 	ns = netstack_find_by_stackid(stackid);
3594 	if (ns == NULL)
3595 		return (-1);
3596 
3597 	ipst = ns->netstack_ip;
3598 	if (ipst == NULL) {
3599 		netstack_rele(ns);
3600 		return (-1);
3601 	}
3602 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3603 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3604 	netstack_rele(ns);
3605 	return (0);
3606 }
3607 
3608 /*
3609  * Has ifindex been plumbed already?
3610  */
3611 static boolean_t
3612 phyint_exists(uint_t index, ip_stack_t *ipst)
3613 {
3614 	ASSERT(index != 0);
3615 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3616 
3617 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3618 	    &index, NULL) != NULL);
3619 }
3620 
3621 /* Pick a unique ifindex */
3622 boolean_t
3623 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3624 {
3625 	uint_t starting_index;
3626 
3627 	if (!ipst->ips_ill_index_wrap) {
3628 		*indexp = ipst->ips_ill_index++;
3629 		if (ipst->ips_ill_index == 0) {
3630 			/* Reached the uint_t limit Next time wrap  */
3631 			ipst->ips_ill_index_wrap = B_TRUE;
3632 		}
3633 		return (B_TRUE);
3634 	}
3635 
3636 	/*
3637 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3638 	 * at this point and don't want to call any function that attempts
3639 	 * to get the lock again.
3640 	 */
3641 	starting_index = ipst->ips_ill_index++;
3642 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3643 		if (ipst->ips_ill_index != 0 &&
3644 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3645 			/* found unused index - use it */
3646 			*indexp = ipst->ips_ill_index;
3647 			return (B_TRUE);
3648 		}
3649 	}
3650 
3651 	/*
3652 	 * all interface indicies are inuse.
3653 	 */
3654 	return (B_FALSE);
3655 }
3656 
3657 /*
3658  * Assign a unique interface index for the phyint.
3659  */
3660 static boolean_t
3661 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3662 {
3663 	ASSERT(phyi->phyint_ifindex == 0);
3664 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3665 }
3666 
3667 /*
3668  * Initialize the flags on `phyi' as per the provided mactype.
3669  */
3670 static void
3671 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3672 {
3673 	uint64_t flags = 0;
3674 
3675 	/*
3676 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3677 	 * we always presume the underlying hardware is working and set
3678 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3679 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3680 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3681 	 */
3682 	if (mactype == SUNW_DL_IPMP)
3683 		flags |= PHYI_FAILED;
3684 	else
3685 		flags |= PHYI_RUNNING;
3686 
3687 	switch (mactype) {
3688 	case SUNW_DL_VNI:
3689 		flags |= PHYI_VIRTUAL;
3690 		break;
3691 	case SUNW_DL_IPMP:
3692 		flags |= PHYI_IPMP;
3693 		break;
3694 	case DL_LOOP:
3695 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3696 		break;
3697 	}
3698 
3699 	mutex_enter(&phyi->phyint_lock);
3700 	phyi->phyint_flags |= flags;
3701 	mutex_exit(&phyi->phyint_lock);
3702 }
3703 
3704 /*
3705  * Return a pointer to the ill which matches the supplied name.  Note that
3706  * the ill name length includes the null termination character.  (May be
3707  * called as writer.)
3708  * If do_alloc and the interface is "lo0" it will be automatically created.
3709  * Cannot bump up reference on condemned ills. So dup detect can't be done
3710  * using this func.
3711  */
3712 ill_t *
3713 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3714     boolean_t *did_alloc, ip_stack_t *ipst)
3715 {
3716 	ill_t	*ill;
3717 	ipif_t	*ipif;
3718 	ipsq_t	*ipsq;
3719 	kstat_named_t	*kn;
3720 	boolean_t isloopback;
3721 	in6_addr_t ov6addr;
3722 
3723 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3724 
3725 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3726 	ill = ill_find_by_name(name, isv6, ipst);
3727 	rw_exit(&ipst->ips_ill_g_lock);
3728 	if (ill != NULL)
3729 		return (ill);
3730 
3731 	/*
3732 	 * Couldn't find it.  Does this happen to be a lookup for the
3733 	 * loopback device and are we allowed to allocate it?
3734 	 */
3735 	if (!isloopback || !do_alloc)
3736 		return (NULL);
3737 
3738 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3739 	ill = ill_find_by_name(name, isv6, ipst);
3740 	if (ill != NULL) {
3741 		rw_exit(&ipst->ips_ill_g_lock);
3742 		return (ill);
3743 	}
3744 
3745 	/* Create the loopback device on demand */
3746 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3747 	    sizeof (ipif_loopback_name), BPRI_MED));
3748 	if (ill == NULL)
3749 		goto done;
3750 
3751 	*ill = ill_null;
3752 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3753 	ill->ill_ipst = ipst;
3754 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3755 	netstack_hold(ipst->ips_netstack);
3756 	/*
3757 	 * For exclusive stacks we set the zoneid to zero
3758 	 * to make IP operate as if in the global zone.
3759 	 */
3760 	ill->ill_zoneid = GLOBAL_ZONEID;
3761 
3762 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3763 	if (ill->ill_phyint == NULL)
3764 		goto done;
3765 
3766 	if (isv6)
3767 		ill->ill_phyint->phyint_illv6 = ill;
3768 	else
3769 		ill->ill_phyint->phyint_illv4 = ill;
3770 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3771 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3772 
3773 	if (isv6) {
3774 		ill->ill_isv6 = B_TRUE;
3775 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3776 	} else {
3777 		ill->ill_max_frag = ip_loopback_mtuplus;
3778 	}
3779 	if (!ill_allocate_mibs(ill))
3780 		goto done;
3781 	ill->ill_current_frag = ill->ill_max_frag;
3782 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3783 	/*
3784 	 * ipif_loopback_name can't be pointed at directly because its used
3785 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3786 	 * from the glist, ill_glist_delete() sets the first character of
3787 	 * ill_name to '\0'.
3788 	 */
3789 	ill->ill_name = (char *)ill + sizeof (*ill);
3790 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3791 	ill->ill_name_length = sizeof (ipif_loopback_name);
3792 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3793 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3794 
3795 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3796 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3797 	ill->ill_global_timer = INFINITY;
3798 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3799 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3800 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3801 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3802 
3803 	/* No resolver here. */
3804 	ill->ill_net_type = IRE_LOOPBACK;
3805 
3806 	/* Initialize the ipsq */
3807 	if (!ipsq_init(ill, B_FALSE))
3808 		goto done;
3809 
3810 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3811 	if (ipif == NULL)
3812 		goto done;
3813 
3814 	ill->ill_flags = ILLF_MULTICAST;
3815 
3816 	ov6addr = ipif->ipif_v6lcl_addr;
3817 	/* Set up default loopback address and mask. */
3818 	if (!isv6) {
3819 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3820 
3821 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3822 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3823 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3824 		    ipif->ipif_v6subnet);
3825 		ill->ill_flags |= ILLF_IPV4;
3826 	} else {
3827 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3828 		ipif->ipif_v6net_mask = ipv6_all_ones;
3829 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3830 		    ipif->ipif_v6subnet);
3831 		ill->ill_flags |= ILLF_IPV6;
3832 	}
3833 
3834 	/*
3835 	 * Chain us in at the end of the ill list. hold the ill
3836 	 * before we make it globally visible. 1 for the lookup.
3837 	 */
3838 	ill->ill_refcnt = 0;
3839 	ill_refhold(ill);
3840 
3841 	ill->ill_frag_count = 0;
3842 	ill->ill_frag_free_num_pkts = 0;
3843 	ill->ill_last_frag_clean_time = 0;
3844 
3845 	ipsq = ill->ill_phyint->phyint_ipsq;
3846 
3847 	ill_set_inputfn(ill);
3848 
3849 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3850 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3851 
3852 	/* Let SCTP know so that it can add this to its list */
3853 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3854 
3855 	/*
3856 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3857 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3858 	 * requires to be after ill_glist_insert() since we need the
3859 	 * ill_index set. Pass on ipv6_loopback as the old address.
3860 	 */
3861 	sctp_update_ipif_addr(ipif, ov6addr);
3862 
3863 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3864 
3865 	/*
3866 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3867 	 * If so, free our original one.
3868 	 */
3869 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3870 		ipsq_delete(ipsq);
3871 
3872 	if (ipst->ips_loopback_ksp == NULL) {
3873 		/* Export loopback interface statistics */
3874 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3875 		    ipif_loopback_name, "net",
3876 		    KSTAT_TYPE_NAMED, 2, 0,
3877 		    ipst->ips_netstack->netstack_stackid);
3878 		if (ipst->ips_loopback_ksp != NULL) {
3879 			ipst->ips_loopback_ksp->ks_update =
3880 			    loopback_kstat_update;
3881 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3882 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3883 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3884 			ipst->ips_loopback_ksp->ks_private =
3885 			    (void *)(uintptr_t)ipst->ips_netstack->
3886 			    netstack_stackid;
3887 			kstat_install(ipst->ips_loopback_ksp);
3888 		}
3889 	}
3890 
3891 	*did_alloc = B_TRUE;
3892 	rw_exit(&ipst->ips_ill_g_lock);
3893 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3894 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3895 	return (ill);
3896 done:
3897 	if (ill != NULL) {
3898 		if (ill->ill_phyint != NULL) {
3899 			ipsq = ill->ill_phyint->phyint_ipsq;
3900 			if (ipsq != NULL) {
3901 				ipsq->ipsq_phyint = NULL;
3902 				ipsq_delete(ipsq);
3903 			}
3904 			mi_free(ill->ill_phyint);
3905 		}
3906 		ill_free_mib(ill);
3907 		if (ill->ill_ipst != NULL)
3908 			netstack_rele(ill->ill_ipst->ips_netstack);
3909 		mi_free(ill);
3910 	}
3911 	rw_exit(&ipst->ips_ill_g_lock);
3912 	return (NULL);
3913 }
3914 
3915 /*
3916  * For IPP calls - use the ip_stack_t for global stack.
3917  */
3918 ill_t *
3919 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3920 {
3921 	ip_stack_t	*ipst;
3922 	ill_t		*ill;
3923 
3924 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3925 	if (ipst == NULL) {
3926 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3927 		return (NULL);
3928 	}
3929 
3930 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3931 	netstack_rele(ipst->ips_netstack);
3932 	return (ill);
3933 }
3934 
3935 /*
3936  * Return a pointer to the ill which matches the index and IP version type.
3937  */
3938 ill_t *
3939 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3940 {
3941 	ill_t	*ill;
3942 	phyint_t *phyi;
3943 
3944 	/*
3945 	 * Indexes are stored in the phyint - a common structure
3946 	 * to both IPv4 and IPv6.
3947 	 */
3948 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3949 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3950 	    (void *) &index, NULL);
3951 	if (phyi != NULL) {
3952 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3953 		if (ill != NULL) {
3954 			mutex_enter(&ill->ill_lock);
3955 			if (!ILL_IS_CONDEMNED(ill)) {
3956 				ill_refhold_locked(ill);
3957 				mutex_exit(&ill->ill_lock);
3958 				rw_exit(&ipst->ips_ill_g_lock);
3959 				return (ill);
3960 			}
3961 			mutex_exit(&ill->ill_lock);
3962 		}
3963 	}
3964 	rw_exit(&ipst->ips_ill_g_lock);
3965 	return (NULL);
3966 }
3967 
3968 /*
3969  * Verify whether or not an interface index is valid for the specified zoneid
3970  * to transmit packets.
3971  * It can be zero (meaning "reset") or an interface index assigned
3972  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3973  */
3974 boolean_t
3975 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3976     ip_stack_t *ipst)
3977 {
3978 	ill_t		*ill;
3979 
3980 	if (ifindex == 0)
3981 		return (B_TRUE);
3982 
3983 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3984 	if (ill == NULL)
3985 		return (B_FALSE);
3986 	if (IS_VNI(ill)) {
3987 		ill_refrele(ill);
3988 		return (B_FALSE);
3989 	}
3990 	ill_refrele(ill);
3991 	return (B_TRUE);
3992 }
3993 
3994 /*
3995  * Return the ifindex next in sequence after the passed in ifindex.
3996  * If there is no next ifindex for the given protocol, return 0.
3997  */
3998 uint_t
3999 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
4000 {
4001 	phyint_t *phyi;
4002 	phyint_t *phyi_initial;
4003 	uint_t   ifindex;
4004 
4005 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4006 
4007 	if (index == 0) {
4008 		phyi = avl_first(
4009 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4010 	} else {
4011 		phyi = phyi_initial = avl_find(
4012 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4013 		    (void *) &index, NULL);
4014 	}
4015 
4016 	for (; phyi != NULL;
4017 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4018 	    phyi, AVL_AFTER)) {
4019 		/*
4020 		 * If we're not returning the first interface in the tree
4021 		 * and we still haven't moved past the phyint_t that
4022 		 * corresponds to index, avl_walk needs to be called again
4023 		 */
4024 		if (!((index != 0) && (phyi == phyi_initial))) {
4025 			if (isv6) {
4026 				if ((phyi->phyint_illv6) &&
4027 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
4028 				    (phyi->phyint_illv6->ill_isv6 == 1))
4029 					break;
4030 			} else {
4031 				if ((phyi->phyint_illv4) &&
4032 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
4033 				    (phyi->phyint_illv4->ill_isv6 == 0))
4034 					break;
4035 			}
4036 		}
4037 	}
4038 
4039 	rw_exit(&ipst->ips_ill_g_lock);
4040 
4041 	if (phyi != NULL)
4042 		ifindex = phyi->phyint_ifindex;
4043 	else
4044 		ifindex = 0;
4045 
4046 	return (ifindex);
4047 }
4048 
4049 /*
4050  * Return the ifindex for the named interface.
4051  * If there is no next ifindex for the interface, return 0.
4052  */
4053 uint_t
4054 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4055 {
4056 	phyint_t	*phyi;
4057 	avl_index_t	where = 0;
4058 	uint_t		ifindex;
4059 
4060 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4061 
4062 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4063 	    name, &where)) == NULL) {
4064 		rw_exit(&ipst->ips_ill_g_lock);
4065 		return (0);
4066 	}
4067 
4068 	ifindex = phyi->phyint_ifindex;
4069 
4070 	rw_exit(&ipst->ips_ill_g_lock);
4071 
4072 	return (ifindex);
4073 }
4074 
4075 /*
4076  * Return the ifindex to be used by upper layer protocols for instance
4077  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4078  */
4079 uint_t
4080 ill_get_upper_ifindex(const ill_t *ill)
4081 {
4082 	if (IS_UNDER_IPMP(ill))
4083 		return (ipmp_ill_get_ipmp_ifindex(ill));
4084 	else
4085 		return (ill->ill_phyint->phyint_ifindex);
4086 }
4087 
4088 
4089 /*
4090  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4091  * that gives a running thread a reference to the ill. This reference must be
4092  * released by the thread when it is done accessing the ill and related
4093  * objects. ill_refcnt can not be used to account for static references
4094  * such as other structures pointing to an ill. Callers must generally
4095  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4096  * or be sure that the ill is not being deleted or changing state before
4097  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4098  * ill won't change any of its critical state such as address, netmask etc.
4099  */
4100 void
4101 ill_refhold(ill_t *ill)
4102 {
4103 	mutex_enter(&ill->ill_lock);
4104 	ill->ill_refcnt++;
4105 	ILL_TRACE_REF(ill);
4106 	mutex_exit(&ill->ill_lock);
4107 }
4108 
4109 void
4110 ill_refhold_locked(ill_t *ill)
4111 {
4112 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4113 	ill->ill_refcnt++;
4114 	ILL_TRACE_REF(ill);
4115 }
4116 
4117 /* Returns true if we managed to get a refhold */
4118 boolean_t
4119 ill_check_and_refhold(ill_t *ill)
4120 {
4121 	mutex_enter(&ill->ill_lock);
4122 	if (!ILL_IS_CONDEMNED(ill)) {
4123 		ill_refhold_locked(ill);
4124 		mutex_exit(&ill->ill_lock);
4125 		return (B_TRUE);
4126 	}
4127 	mutex_exit(&ill->ill_lock);
4128 	return (B_FALSE);
4129 }
4130 
4131 /*
4132  * Must not be called while holding any locks. Otherwise if this is
4133  * the last reference to be released, there is a chance of recursive mutex
4134  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4135  * to restart an ioctl.
4136  */
4137 void
4138 ill_refrele(ill_t *ill)
4139 {
4140 	mutex_enter(&ill->ill_lock);
4141 	ASSERT(ill->ill_refcnt != 0);
4142 	ill->ill_refcnt--;
4143 	ILL_UNTRACE_REF(ill);
4144 	if (ill->ill_refcnt != 0) {
4145 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4146 		mutex_exit(&ill->ill_lock);
4147 		return;
4148 	}
4149 
4150 	/* Drops the ill_lock */
4151 	ipif_ill_refrele_tail(ill);
4152 }
4153 
4154 /*
4155  * Obtain a weak reference count on the ill. This reference ensures the
4156  * ill won't be freed, but the ill may change any of its critical state
4157  * such as netmask, address etc. Returns an error if the ill has started
4158  * closing.
4159  */
4160 boolean_t
4161 ill_waiter_inc(ill_t *ill)
4162 {
4163 	mutex_enter(&ill->ill_lock);
4164 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4165 		mutex_exit(&ill->ill_lock);
4166 		return (B_FALSE);
4167 	}
4168 	ill->ill_waiters++;
4169 	mutex_exit(&ill->ill_lock);
4170 	return (B_TRUE);
4171 }
4172 
4173 void
4174 ill_waiter_dcr(ill_t *ill)
4175 {
4176 	mutex_enter(&ill->ill_lock);
4177 	ill->ill_waiters--;
4178 	if (ill->ill_waiters == 0)
4179 		cv_broadcast(&ill->ill_cv);
4180 	mutex_exit(&ill->ill_lock);
4181 }
4182 
4183 /*
4184  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4185  * driver.  We construct best guess defaults for lower level information that
4186  * we need.  If an interface is brought up without injection of any overriding
4187  * information from outside, we have to be ready to go with these defaults.
4188  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4189  * we primarely want the dl_provider_style.
4190  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4191  * at which point we assume the other part of the information is valid.
4192  */
4193 void
4194 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4195 {
4196 	uchar_t		*brdcst_addr;
4197 	uint_t		brdcst_addr_length, phys_addr_length;
4198 	t_scalar_t	sap_length;
4199 	dl_info_ack_t	*dlia;
4200 	ip_m_t		*ipm;
4201 	dl_qos_cl_sel1_t *sel1;
4202 	int		min_mtu;
4203 
4204 	ASSERT(IAM_WRITER_ILL(ill));
4205 
4206 	/*
4207 	 * Till the ill is fully up  the ill is not globally visible.
4208 	 * So no need for a lock.
4209 	 */
4210 	dlia = (dl_info_ack_t *)mp->b_rptr;
4211 	ill->ill_mactype = dlia->dl_mac_type;
4212 
4213 	ipm = ip_m_lookup(dlia->dl_mac_type);
4214 	if (ipm == NULL) {
4215 		ipm = ip_m_lookup(DL_OTHER);
4216 		ASSERT(ipm != NULL);
4217 	}
4218 	ill->ill_media = ipm;
4219 
4220 	/*
4221 	 * When the new DLPI stuff is ready we'll pull lengths
4222 	 * from dlia.
4223 	 */
4224 	if (dlia->dl_version == DL_VERSION_2) {
4225 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4226 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4227 		    brdcst_addr_length);
4228 		if (brdcst_addr == NULL) {
4229 			brdcst_addr_length = 0;
4230 		}
4231 		sap_length = dlia->dl_sap_length;
4232 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4233 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4234 		    brdcst_addr_length, sap_length, phys_addr_length));
4235 	} else {
4236 		brdcst_addr_length = 6;
4237 		brdcst_addr = ip_six_byte_all_ones;
4238 		sap_length = -2;
4239 		phys_addr_length = brdcst_addr_length;
4240 	}
4241 
4242 	ill->ill_bcast_addr_length = brdcst_addr_length;
4243 	ill->ill_phys_addr_length = phys_addr_length;
4244 	ill->ill_sap_length = sap_length;
4245 
4246 	/*
4247 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4248 	 * but we must ensure a minimum IP MTU is used since other bits of
4249 	 * IP will fly apart otherwise.
4250 	 */
4251 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4252 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4253 	ill->ill_current_frag = ill->ill_max_frag;
4254 	ill->ill_mtu = ill->ill_max_frag;
4255 
4256 	ill->ill_type = ipm->ip_m_type;
4257 
4258 	if (!ill->ill_dlpi_style_set) {
4259 		if (dlia->dl_provider_style == DL_STYLE2)
4260 			ill->ill_needs_attach = 1;
4261 
4262 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4263 
4264 		/*
4265 		 * Allocate the first ipif on this ill.  We don't delay it
4266 		 * further as ioctl handling assumes at least one ipif exists.
4267 		 *
4268 		 * At this point we don't know whether the ill is v4 or v6.
4269 		 * We will know this whan the SIOCSLIFNAME happens and
4270 		 * the correct value for ill_isv6 will be assigned in
4271 		 * ipif_set_values(). We need to hold the ill lock and
4272 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4273 		 * the wakeup.
4274 		 */
4275 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4276 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4277 		mutex_enter(&ill->ill_lock);
4278 		ASSERT(ill->ill_dlpi_style_set == 0);
4279 		ill->ill_dlpi_style_set = 1;
4280 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4281 		cv_broadcast(&ill->ill_cv);
4282 		mutex_exit(&ill->ill_lock);
4283 		freemsg(mp);
4284 		return;
4285 	}
4286 	ASSERT(ill->ill_ipif != NULL);
4287 	/*
4288 	 * We know whether it is IPv4 or IPv6 now, as this is the
4289 	 * second DL_INFO_ACK we are recieving in response to the
4290 	 * DL_INFO_REQ sent in ipif_set_values.
4291 	 */
4292 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4293 	/*
4294 	 * Clear all the flags that were set based on ill_bcast_addr_length
4295 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4296 	 * changed now and we need to re-evaluate.
4297 	 */
4298 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4299 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4300 
4301 	/*
4302 	 * Free ill_bcast_mp as things could have changed now.
4303 	 *
4304 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4305 	 * with no underlying interfaces (and thus an unknown broadcast
4306 	 * address length), but we enforce that an interface is broadcast-
4307 	 * capable as part of allowing it to join a group.
4308 	 */
4309 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4310 		if (ill->ill_bcast_mp != NULL)
4311 			freemsg(ill->ill_bcast_mp);
4312 		ill->ill_net_type = IRE_IF_NORESOLVER;
4313 
4314 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4315 		    ill->ill_phys_addr_length,
4316 		    ill->ill_sap,
4317 		    ill->ill_sap_length);
4318 
4319 		if (ill->ill_isv6)
4320 			/*
4321 			 * Note: xresolv interfaces will eventually need NOARP
4322 			 * set here as well, but that will require those
4323 			 * external resolvers to have some knowledge of
4324 			 * that flag and act appropriately. Not to be changed
4325 			 * at present.
4326 			 */
4327 			ill->ill_flags |= ILLF_NONUD;
4328 		else
4329 			ill->ill_flags |= ILLF_NOARP;
4330 
4331 		if (ill->ill_mactype == SUNW_DL_VNI) {
4332 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4333 		} else if (ill->ill_phys_addr_length == 0 ||
4334 		    ill->ill_mactype == DL_IPV4 ||
4335 		    ill->ill_mactype == DL_IPV6) {
4336 			/*
4337 			 * The underying link is point-to-point, so mark the
4338 			 * interface as such.  We can do IP multicast over
4339 			 * such a link since it transmits all network-layer
4340 			 * packets to the remote side the same way.
4341 			 */
4342 			ill->ill_flags |= ILLF_MULTICAST;
4343 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4344 		}
4345 	} else {
4346 		ill->ill_net_type = IRE_IF_RESOLVER;
4347 		if (ill->ill_bcast_mp != NULL)
4348 			freemsg(ill->ill_bcast_mp);
4349 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4350 		    ill->ill_bcast_addr_length, ill->ill_sap,
4351 		    ill->ill_sap_length);
4352 		/*
4353 		 * Later detect lack of DLPI driver multicast
4354 		 * capability by catching DL_ENABMULTI errors in
4355 		 * ip_rput_dlpi.
4356 		 */
4357 		ill->ill_flags |= ILLF_MULTICAST;
4358 		if (!ill->ill_isv6)
4359 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4360 	}
4361 
4362 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4363 	if (ill->ill_mactype == SUNW_DL_IPMP)
4364 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4365 
4366 	/* By default an interface does not support any CoS marking */
4367 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4368 
4369 	/*
4370 	 * If we get QoS information in DL_INFO_ACK, the device supports
4371 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4372 	 */
4373 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4374 	    dlia->dl_qos_length);
4375 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4376 		ill->ill_flags |= ILLF_COS_ENABLED;
4377 	}
4378 
4379 	/* Clear any previous error indication. */
4380 	ill->ill_error = 0;
4381 	freemsg(mp);
4382 }
4383 
4384 /*
4385  * Perform various checks to verify that an address would make sense as a
4386  * local, remote, or subnet interface address.
4387  */
4388 static boolean_t
4389 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4390 {
4391 	ipaddr_t	net_mask;
4392 
4393 	/*
4394 	 * Don't allow all zeroes, or all ones, but allow
4395 	 * all ones netmask.
4396 	 */
4397 	if ((net_mask = ip_net_mask(addr)) == 0)
4398 		return (B_FALSE);
4399 	/* A given netmask overrides the "guess" netmask */
4400 	if (subnet_mask != 0)
4401 		net_mask = subnet_mask;
4402 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4403 	    (addr == (addr | ~net_mask)))) {
4404 		return (B_FALSE);
4405 	}
4406 
4407 	/*
4408 	 * Even if the netmask is all ones, we do not allow address to be
4409 	 * 255.255.255.255
4410 	 */
4411 	if (addr == INADDR_BROADCAST)
4412 		return (B_FALSE);
4413 
4414 	if (CLASSD(addr))
4415 		return (B_FALSE);
4416 
4417 	return (B_TRUE);
4418 }
4419 
4420 #define	V6_IPIF_LINKLOCAL(p)	\
4421 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4422 
4423 /*
4424  * Compare two given ipifs and check if the second one is better than
4425  * the first one using the order of preference (not taking deprecated
4426  * into acount) specified in ipif_lookup_multicast().
4427  */
4428 static boolean_t
4429 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4430 {
4431 	/* Check the least preferred first. */
4432 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4433 		/* If both ipifs are the same, use the first one. */
4434 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4435 			return (B_FALSE);
4436 		else
4437 			return (B_TRUE);
4438 	}
4439 
4440 	/* For IPv6, check for link local address. */
4441 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4442 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4443 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4444 			/* The second one is equal or less preferred. */
4445 			return (B_FALSE);
4446 		} else {
4447 			return (B_TRUE);
4448 		}
4449 	}
4450 
4451 	/* Then check for point to point interface. */
4452 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4453 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4454 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4455 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4456 			return (B_FALSE);
4457 		} else {
4458 			return (B_TRUE);
4459 		}
4460 	}
4461 
4462 	/* old_ipif is a normal interface, so no need to use the new one. */
4463 	return (B_FALSE);
4464 }
4465 
4466 /*
4467  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4468  * The ipif must be up, and its ill must multicast-capable, not
4469  * condemned, not an underlying interface in an IPMP group, and
4470  * not a VNI interface.  Order of preference:
4471  *
4472  * 	1a. normal
4473  * 	1b. normal, but deprecated
4474  * 	2a. point to point
4475  * 	2b. point to point, but deprecated
4476  * 	3a. link local
4477  * 	3b. link local, but deprecated
4478  * 	4. loopback.
4479  */
4480 static ipif_t *
4481 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4482 {
4483 	ill_t			*ill;
4484 	ill_walk_context_t	ctx;
4485 	ipif_t			*ipif;
4486 	ipif_t			*saved_ipif = NULL;
4487 	ipif_t			*dep_ipif = NULL;
4488 
4489 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4490 	if (isv6)
4491 		ill = ILL_START_WALK_V6(&ctx, ipst);
4492 	else
4493 		ill = ILL_START_WALK_V4(&ctx, ipst);
4494 
4495 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4496 		mutex_enter(&ill->ill_lock);
4497 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4498 		    ILL_IS_CONDEMNED(ill) ||
4499 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4500 			mutex_exit(&ill->ill_lock);
4501 			continue;
4502 		}
4503 		for (ipif = ill->ill_ipif; ipif != NULL;
4504 		    ipif = ipif->ipif_next) {
4505 			if (zoneid != ipif->ipif_zoneid &&
4506 			    zoneid != ALL_ZONES &&
4507 			    ipif->ipif_zoneid != ALL_ZONES) {
4508 				continue;
4509 			}
4510 			if (!(ipif->ipif_flags & IPIF_UP) ||
4511 			    IPIF_IS_CONDEMNED(ipif)) {
4512 				continue;
4513 			}
4514 
4515 			/*
4516 			 * Found one candidate.  If it is deprecated,
4517 			 * remember it in dep_ipif.  If it is not deprecated,
4518 			 * remember it in saved_ipif.
4519 			 */
4520 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4521 				if (dep_ipif == NULL) {
4522 					dep_ipif = ipif;
4523 				} else if (ipif_comp_multi(dep_ipif, ipif,
4524 				    isv6)) {
4525 					/*
4526 					 * If the previous dep_ipif does not
4527 					 * belong to the same ill, we've done
4528 					 * a ipif_refhold() on it.  So we need
4529 					 * to release it.
4530 					 */
4531 					if (dep_ipif->ipif_ill != ill)
4532 						ipif_refrele(dep_ipif);
4533 					dep_ipif = ipif;
4534 				}
4535 				continue;
4536 			}
4537 			if (saved_ipif == NULL) {
4538 				saved_ipif = ipif;
4539 			} else {
4540 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4541 					if (saved_ipif->ipif_ill != ill)
4542 						ipif_refrele(saved_ipif);
4543 					saved_ipif = ipif;
4544 				}
4545 			}
4546 		}
4547 		/*
4548 		 * Before going to the next ill, do a ipif_refhold() on the
4549 		 * saved ones.
4550 		 */
4551 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4552 			ipif_refhold_locked(saved_ipif);
4553 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4554 			ipif_refhold_locked(dep_ipif);
4555 		mutex_exit(&ill->ill_lock);
4556 	}
4557 	rw_exit(&ipst->ips_ill_g_lock);
4558 
4559 	/*
4560 	 * If we have only the saved_ipif, return it.  But if we have both
4561 	 * saved_ipif and dep_ipif, check to see which one is better.
4562 	 */
4563 	if (saved_ipif != NULL) {
4564 		if (dep_ipif != NULL) {
4565 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4566 				ipif_refrele(saved_ipif);
4567 				return (dep_ipif);
4568 			} else {
4569 				ipif_refrele(dep_ipif);
4570 				return (saved_ipif);
4571 			}
4572 		}
4573 		return (saved_ipif);
4574 	} else {
4575 		return (dep_ipif);
4576 	}
4577 }
4578 
4579 ill_t *
4580 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4581 {
4582 	ipif_t *ipif;
4583 	ill_t *ill;
4584 
4585 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4586 	if (ipif == NULL)
4587 		return (NULL);
4588 
4589 	ill = ipif->ipif_ill;
4590 	ill_refhold(ill);
4591 	ipif_refrele(ipif);
4592 	return (ill);
4593 }
4594 
4595 /*
4596  * This function is called when an application does not specify an interface
4597  * to be used for multicast traffic (joining a group/sending data).  It
4598  * calls ire_lookup_multi() to look for an interface route for the
4599  * specified multicast group.  Doing this allows the administrator to add
4600  * prefix routes for multicast to indicate which interface to be used for
4601  * multicast traffic in the above scenario.  The route could be for all
4602  * multicast (224.0/4), for a single multicast group (a /32 route) or
4603  * anything in between.  If there is no such multicast route, we just find
4604  * any multicast capable interface and return it.  The returned ipif
4605  * is refhold'ed.
4606  *
4607  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4608  * unicast table. This is used by CGTP.
4609  */
4610 ill_t *
4611 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4612     boolean_t *multirtp, ipaddr_t *setsrcp)
4613 {
4614 	ill_t			*ill;
4615 
4616 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4617 	if (ill != NULL)
4618 		return (ill);
4619 
4620 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4621 }
4622 
4623 /*
4624  * Look for an ipif with the specified interface address and destination.
4625  * The destination address is used only for matching point-to-point interfaces.
4626  */
4627 ipif_t *
4628 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4629 {
4630 	ipif_t	*ipif;
4631 	ill_t	*ill;
4632 	ill_walk_context_t ctx;
4633 
4634 	/*
4635 	 * First match all the point-to-point interfaces
4636 	 * before looking at non-point-to-point interfaces.
4637 	 * This is done to avoid returning non-point-to-point
4638 	 * ipif instead of unnumbered point-to-point ipif.
4639 	 */
4640 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4641 	ill = ILL_START_WALK_V4(&ctx, ipst);
4642 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4643 		mutex_enter(&ill->ill_lock);
4644 		for (ipif = ill->ill_ipif; ipif != NULL;
4645 		    ipif = ipif->ipif_next) {
4646 			/* Allow the ipif to be down */
4647 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4648 			    (ipif->ipif_lcl_addr == if_addr) &&
4649 			    (ipif->ipif_pp_dst_addr == dst)) {
4650 				if (!IPIF_IS_CONDEMNED(ipif)) {
4651 					ipif_refhold_locked(ipif);
4652 					mutex_exit(&ill->ill_lock);
4653 					rw_exit(&ipst->ips_ill_g_lock);
4654 					return (ipif);
4655 				}
4656 			}
4657 		}
4658 		mutex_exit(&ill->ill_lock);
4659 	}
4660 	rw_exit(&ipst->ips_ill_g_lock);
4661 
4662 	/* lookup the ipif based on interface address */
4663 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4664 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4665 	return (ipif);
4666 }
4667 
4668 /*
4669  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4670  */
4671 static ipif_t *
4672 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4673     zoneid_t zoneid, ip_stack_t *ipst)
4674 {
4675 	ipif_t  *ipif;
4676 	ill_t   *ill;
4677 	boolean_t ptp = B_FALSE;
4678 	ill_walk_context_t	ctx;
4679 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4680 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4681 
4682 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4683 	/*
4684 	 * Repeat twice, first based on local addresses and
4685 	 * next time for pointopoint.
4686 	 */
4687 repeat:
4688 	ill = ILL_START_WALK_V4(&ctx, ipst);
4689 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4690 		if (match_ill != NULL && ill != match_ill &&
4691 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4692 			continue;
4693 		}
4694 		mutex_enter(&ill->ill_lock);
4695 		for (ipif = ill->ill_ipif; ipif != NULL;
4696 		    ipif = ipif->ipif_next) {
4697 			if (zoneid != ALL_ZONES &&
4698 			    zoneid != ipif->ipif_zoneid &&
4699 			    ipif->ipif_zoneid != ALL_ZONES)
4700 				continue;
4701 
4702 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4703 				continue;
4704 
4705 			/* Allow the ipif to be down */
4706 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4707 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4708 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4709 			    (ipif->ipif_pp_dst_addr == addr))) {
4710 				if (!IPIF_IS_CONDEMNED(ipif)) {
4711 					ipif_refhold_locked(ipif);
4712 					mutex_exit(&ill->ill_lock);
4713 					rw_exit(&ipst->ips_ill_g_lock);
4714 					return (ipif);
4715 				}
4716 			}
4717 		}
4718 		mutex_exit(&ill->ill_lock);
4719 	}
4720 
4721 	/* If we already did the ptp case, then we are done */
4722 	if (ptp) {
4723 		rw_exit(&ipst->ips_ill_g_lock);
4724 		return (NULL);
4725 	}
4726 	ptp = B_TRUE;
4727 	goto repeat;
4728 }
4729 
4730 /*
4731  * Lookup an ipif with the specified address.  For point-to-point links we
4732  * look for matches on either the destination address or the local address,
4733  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4734  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4735  * (or illgrp if `match_ill' is in an IPMP group).
4736  */
4737 ipif_t *
4738 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4739     ip_stack_t *ipst)
4740 {
4741 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4742 	    zoneid, ipst));
4743 }
4744 
4745 /*
4746  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4747  * except that we will only return an address if it is not marked as
4748  * IPIF_DUPLICATE
4749  */
4750 ipif_t *
4751 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4752     ip_stack_t *ipst)
4753 {
4754 	return (ipif_lookup_addr_common(addr, match_ill,
4755 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4756 	    zoneid, ipst));
4757 }
4758 
4759 /*
4760  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4761  * `match_ill' across the IPMP group.  This function is only needed in some
4762  * corner-cases; almost everything should use ipif_lookup_addr().
4763  */
4764 ipif_t *
4765 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4766 {
4767 	ASSERT(match_ill != NULL);
4768 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4769 	    ipst));
4770 }
4771 
4772 /*
4773  * Look for an ipif with the specified address. For point-point links
4774  * we look for matches on either the destination address and the local
4775  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4776  * is set.
4777  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4778  * ill (or illgrp if `match_ill' is in an IPMP group).
4779  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4780  */
4781 zoneid_t
4782 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4783 {
4784 	zoneid_t zoneid;
4785 	ipif_t  *ipif;
4786 	ill_t   *ill;
4787 	boolean_t ptp = B_FALSE;
4788 	ill_walk_context_t	ctx;
4789 
4790 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4791 	/*
4792 	 * Repeat twice, first based on local addresses and
4793 	 * next time for pointopoint.
4794 	 */
4795 repeat:
4796 	ill = ILL_START_WALK_V4(&ctx, ipst);
4797 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4798 		if (match_ill != NULL && ill != match_ill &&
4799 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4800 			continue;
4801 		}
4802 		mutex_enter(&ill->ill_lock);
4803 		for (ipif = ill->ill_ipif; ipif != NULL;
4804 		    ipif = ipif->ipif_next) {
4805 			/* Allow the ipif to be down */
4806 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4807 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4808 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4809 			    (ipif->ipif_pp_dst_addr == addr)) &&
4810 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4811 				zoneid = ipif->ipif_zoneid;
4812 				mutex_exit(&ill->ill_lock);
4813 				rw_exit(&ipst->ips_ill_g_lock);
4814 				/*
4815 				 * If ipif_zoneid was ALL_ZONES then we have
4816 				 * a trusted extensions shared IP address.
4817 				 * In that case GLOBAL_ZONEID works to send.
4818 				 */
4819 				if (zoneid == ALL_ZONES)
4820 					zoneid = GLOBAL_ZONEID;
4821 				return (zoneid);
4822 			}
4823 		}
4824 		mutex_exit(&ill->ill_lock);
4825 	}
4826 
4827 	/* If we already did the ptp case, then we are done */
4828 	if (ptp) {
4829 		rw_exit(&ipst->ips_ill_g_lock);
4830 		return (ALL_ZONES);
4831 	}
4832 	ptp = B_TRUE;
4833 	goto repeat;
4834 }
4835 
4836 /*
4837  * Look for an ipif that matches the specified remote address i.e. the
4838  * ipif that would receive the specified packet.
4839  * First look for directly connected interfaces and then do a recursive
4840  * IRE lookup and pick the first ipif corresponding to the source address in the
4841  * ire.
4842  * Returns: held ipif
4843  *
4844  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4845  */
4846 ipif_t *
4847 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4848 {
4849 	ipif_t	*ipif;
4850 
4851 	ASSERT(!ill->ill_isv6);
4852 
4853 	/*
4854 	 * Someone could be changing this ipif currently or change it
4855 	 * after we return this. Thus  a few packets could use the old
4856 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4857 	 * will atomically be updated or cleaned up with the new value
4858 	 * Thus we don't need a lock to check the flags or other attrs below.
4859 	 */
4860 	mutex_enter(&ill->ill_lock);
4861 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4862 		if (IPIF_IS_CONDEMNED(ipif))
4863 			continue;
4864 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4865 		    ipif->ipif_zoneid != ALL_ZONES)
4866 			continue;
4867 		/* Allow the ipif to be down */
4868 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4869 			if ((ipif->ipif_pp_dst_addr == addr) ||
4870 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4871 			    ipif->ipif_lcl_addr == addr)) {
4872 				ipif_refhold_locked(ipif);
4873 				mutex_exit(&ill->ill_lock);
4874 				return (ipif);
4875 			}
4876 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4877 			ipif_refhold_locked(ipif);
4878 			mutex_exit(&ill->ill_lock);
4879 			return (ipif);
4880 		}
4881 	}
4882 	mutex_exit(&ill->ill_lock);
4883 	/*
4884 	 * For a remote destination it isn't possible to nail down a particular
4885 	 * ipif.
4886 	 */
4887 
4888 	/* Pick the first interface */
4889 	ipif = ipif_get_next_ipif(NULL, ill);
4890 	return (ipif);
4891 }
4892 
4893 /*
4894  * This func does not prevent refcnt from increasing. But if
4895  * the caller has taken steps to that effect, then this func
4896  * can be used to determine whether the ill has become quiescent
4897  */
4898 static boolean_t
4899 ill_is_quiescent(ill_t *ill)
4900 {
4901 	ipif_t	*ipif;
4902 
4903 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4904 
4905 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4906 		if (ipif->ipif_refcnt != 0)
4907 			return (B_FALSE);
4908 	}
4909 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4910 		return (B_FALSE);
4911 	}
4912 	return (B_TRUE);
4913 }
4914 
4915 boolean_t
4916 ill_is_freeable(ill_t *ill)
4917 {
4918 	ipif_t	*ipif;
4919 
4920 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4921 
4922 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4923 		if (ipif->ipif_refcnt != 0) {
4924 			return (B_FALSE);
4925 		}
4926 	}
4927 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4928 		return (B_FALSE);
4929 	}
4930 	return (B_TRUE);
4931 }
4932 
4933 /*
4934  * This func does not prevent refcnt from increasing. But if
4935  * the caller has taken steps to that effect, then this func
4936  * can be used to determine whether the ipif has become quiescent
4937  */
4938 static boolean_t
4939 ipif_is_quiescent(ipif_t *ipif)
4940 {
4941 	ill_t *ill;
4942 
4943 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4944 
4945 	if (ipif->ipif_refcnt != 0)
4946 		return (B_FALSE);
4947 
4948 	ill = ipif->ipif_ill;
4949 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4950 	    ill->ill_logical_down) {
4951 		return (B_TRUE);
4952 	}
4953 
4954 	/* This is the last ipif going down or being deleted on this ill */
4955 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4956 		return (B_FALSE);
4957 	}
4958 
4959 	return (B_TRUE);
4960 }
4961 
4962 /*
4963  * return true if the ipif can be destroyed: the ipif has to be quiescent
4964  * with zero references from ire/ilm to it.
4965  */
4966 static boolean_t
4967 ipif_is_freeable(ipif_t *ipif)
4968 {
4969 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4970 	ASSERT(ipif->ipif_id != 0);
4971 	return (ipif->ipif_refcnt == 0);
4972 }
4973 
4974 /*
4975  * The ipif/ill/ire has been refreled. Do the tail processing.
4976  * Determine if the ipif or ill in question has become quiescent and if so
4977  * wakeup close and/or restart any queued pending ioctl that is waiting
4978  * for the ipif_down (or ill_down)
4979  */
4980 void
4981 ipif_ill_refrele_tail(ill_t *ill)
4982 {
4983 	mblk_t	*mp;
4984 	conn_t	*connp;
4985 	ipsq_t	*ipsq;
4986 	ipxop_t	*ipx;
4987 	ipif_t	*ipif;
4988 	dl_notify_ind_t *dlindp;
4989 
4990 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4991 
4992 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4993 		/* ip_modclose() may be waiting */
4994 		cv_broadcast(&ill->ill_cv);
4995 	}
4996 
4997 	ipsq = ill->ill_phyint->phyint_ipsq;
4998 	mutex_enter(&ipsq->ipsq_lock);
4999 	ipx = ipsq->ipsq_xop;
5000 	mutex_enter(&ipx->ipx_lock);
5001 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
5002 		goto unlock;
5003 
5004 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
5005 
5006 	ipif = ipx->ipx_pending_ipif;
5007 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
5008 		goto unlock;
5009 
5010 	switch (ipx->ipx_waitfor) {
5011 	case IPIF_DOWN:
5012 		if (!ipif_is_quiescent(ipif))
5013 			goto unlock;
5014 		break;
5015 	case IPIF_FREE:
5016 		if (!ipif_is_freeable(ipif))
5017 			goto unlock;
5018 		break;
5019 	case ILL_DOWN:
5020 		if (!ill_is_quiescent(ill))
5021 			goto unlock;
5022 		break;
5023 	case ILL_FREE:
5024 		/*
5025 		 * ILL_FREE is only for loopback; normal ill teardown waits
5026 		 * synchronously in ip_modclose() without using ipx_waitfor,
5027 		 * handled by the cv_broadcast() at the top of this function.
5028 		 */
5029 		if (!ill_is_freeable(ill))
5030 			goto unlock;
5031 		break;
5032 	default:
5033 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
5034 		    (void *)ipsq, ipx->ipx_waitfor);
5035 	}
5036 
5037 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
5038 	mutex_exit(&ipx->ipx_lock);
5039 	mp = ipsq_pending_mp_get(ipsq, &connp);
5040 	mutex_exit(&ipsq->ipsq_lock);
5041 	mutex_exit(&ill->ill_lock);
5042 
5043 	ASSERT(mp != NULL);
5044 	/*
5045 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5046 	 * we can only get here when the current operation decides it
5047 	 * it needs to quiesce via ipsq_pending_mp_add().
5048 	 */
5049 	switch (mp->b_datap->db_type) {
5050 	case M_PCPROTO:
5051 	case M_PROTO:
5052 		/*
5053 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5054 		 */
5055 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5056 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5057 
5058 		switch (dlindp->dl_notification) {
5059 		case DL_NOTE_PHYS_ADDR:
5060 			qwriter_ip(ill, ill->ill_rq, mp,
5061 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5062 			return;
5063 		case DL_NOTE_REPLUMB:
5064 			qwriter_ip(ill, ill->ill_rq, mp,
5065 			    ill_replumb_tail, CUR_OP, B_TRUE);
5066 			return;
5067 		default:
5068 			ASSERT(0);
5069 			ill_refrele(ill);
5070 		}
5071 		break;
5072 
5073 	case M_ERROR:
5074 	case M_HANGUP:
5075 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5076 		    B_TRUE);
5077 		return;
5078 
5079 	case M_IOCTL:
5080 	case M_IOCDATA:
5081 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5082 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5083 		return;
5084 
5085 	default:
5086 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5087 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5088 	}
5089 	return;
5090 unlock:
5091 	mutex_exit(&ipsq->ipsq_lock);
5092 	mutex_exit(&ipx->ipx_lock);
5093 	mutex_exit(&ill->ill_lock);
5094 }
5095 
5096 #ifdef DEBUG
5097 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5098 static void
5099 th_trace_rrecord(th_trace_t *th_trace)
5100 {
5101 	tr_buf_t *tr_buf;
5102 	uint_t lastref;
5103 
5104 	lastref = th_trace->th_trace_lastref;
5105 	lastref++;
5106 	if (lastref == TR_BUF_MAX)
5107 		lastref = 0;
5108 	th_trace->th_trace_lastref = lastref;
5109 	tr_buf = &th_trace->th_trbuf[lastref];
5110 	tr_buf->tr_time = ddi_get_lbolt();
5111 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5112 }
5113 
5114 static void
5115 th_trace_free(void *value)
5116 {
5117 	th_trace_t *th_trace = value;
5118 
5119 	ASSERT(th_trace->th_refcnt == 0);
5120 	kmem_free(th_trace, sizeof (*th_trace));
5121 }
5122 
5123 /*
5124  * Find or create the per-thread hash table used to track object references.
5125  * The ipst argument is NULL if we shouldn't allocate.
5126  *
5127  * Accesses per-thread data, so there's no need to lock here.
5128  */
5129 static mod_hash_t *
5130 th_trace_gethash(ip_stack_t *ipst)
5131 {
5132 	th_hash_t *thh;
5133 
5134 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5135 		mod_hash_t *mh;
5136 		char name[256];
5137 		size_t objsize, rshift;
5138 		int retv;
5139 
5140 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5141 			return (NULL);
5142 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5143 		    (void *)curthread);
5144 
5145 		/*
5146 		 * We use mod_hash_create_extended here rather than the more
5147 		 * obvious mod_hash_create_ptrhash because the latter has a
5148 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5149 		 * block.
5150 		 */
5151 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5152 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5153 		rshift = highbit(objsize);
5154 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5155 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5156 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5157 		if (mh == NULL) {
5158 			kmem_free(thh, sizeof (*thh));
5159 			return (NULL);
5160 		}
5161 		thh->thh_hash = mh;
5162 		thh->thh_ipst = ipst;
5163 		/*
5164 		 * We trace ills, ipifs, ires, and nces.  All of these are
5165 		 * per-IP-stack, so the lock on the thread list is as well.
5166 		 */
5167 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5168 		list_insert_tail(&ip_thread_list, thh);
5169 		rw_exit(&ip_thread_rwlock);
5170 		retv = tsd_set(ip_thread_data, thh);
5171 		ASSERT(retv == 0);
5172 	}
5173 	return (thh != NULL ? thh->thh_hash : NULL);
5174 }
5175 
5176 boolean_t
5177 th_trace_ref(const void *obj, ip_stack_t *ipst)
5178 {
5179 	th_trace_t *th_trace;
5180 	mod_hash_t *mh;
5181 	mod_hash_val_t val;
5182 
5183 	if ((mh = th_trace_gethash(ipst)) == NULL)
5184 		return (B_FALSE);
5185 
5186 	/*
5187 	 * Attempt to locate the trace buffer for this obj and thread.
5188 	 * If it does not exist, then allocate a new trace buffer and
5189 	 * insert into the hash.
5190 	 */
5191 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5192 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5193 		if (th_trace == NULL)
5194 			return (B_FALSE);
5195 
5196 		th_trace->th_id = curthread;
5197 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5198 		    (mod_hash_val_t)th_trace) != 0) {
5199 			kmem_free(th_trace, sizeof (th_trace_t));
5200 			return (B_FALSE);
5201 		}
5202 	} else {
5203 		th_trace = (th_trace_t *)val;
5204 	}
5205 
5206 	ASSERT(th_trace->th_refcnt >= 0 &&
5207 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5208 
5209 	th_trace->th_refcnt++;
5210 	th_trace_rrecord(th_trace);
5211 	return (B_TRUE);
5212 }
5213 
5214 /*
5215  * For the purpose of tracing a reference release, we assume that global
5216  * tracing is always on and that the same thread initiated the reference hold
5217  * is releasing.
5218  */
5219 void
5220 th_trace_unref(const void *obj)
5221 {
5222 	int retv;
5223 	mod_hash_t *mh;
5224 	th_trace_t *th_trace;
5225 	mod_hash_val_t val;
5226 
5227 	mh = th_trace_gethash(NULL);
5228 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5229 	ASSERT(retv == 0);
5230 	th_trace = (th_trace_t *)val;
5231 
5232 	ASSERT(th_trace->th_refcnt > 0);
5233 	th_trace->th_refcnt--;
5234 	th_trace_rrecord(th_trace);
5235 }
5236 
5237 /*
5238  * If tracing has been disabled, then we assume that the reference counts are
5239  * now useless, and we clear them out before destroying the entries.
5240  */
5241 void
5242 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5243 {
5244 	th_hash_t	*thh;
5245 	mod_hash_t	*mh;
5246 	mod_hash_val_t	val;
5247 	th_trace_t	*th_trace;
5248 	int		retv;
5249 
5250 	rw_enter(&ip_thread_rwlock, RW_READER);
5251 	for (thh = list_head(&ip_thread_list); thh != NULL;
5252 	    thh = list_next(&ip_thread_list, thh)) {
5253 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5254 		    &val) == 0) {
5255 			th_trace = (th_trace_t *)val;
5256 			if (trace_disable)
5257 				th_trace->th_refcnt = 0;
5258 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5259 			ASSERT(retv == 0);
5260 		}
5261 	}
5262 	rw_exit(&ip_thread_rwlock);
5263 }
5264 
5265 void
5266 ipif_trace_ref(ipif_t *ipif)
5267 {
5268 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5269 
5270 	if (ipif->ipif_trace_disable)
5271 		return;
5272 
5273 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5274 		ipif->ipif_trace_disable = B_TRUE;
5275 		ipif_trace_cleanup(ipif);
5276 	}
5277 }
5278 
5279 void
5280 ipif_untrace_ref(ipif_t *ipif)
5281 {
5282 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5283 
5284 	if (!ipif->ipif_trace_disable)
5285 		th_trace_unref(ipif);
5286 }
5287 
5288 void
5289 ill_trace_ref(ill_t *ill)
5290 {
5291 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5292 
5293 	if (ill->ill_trace_disable)
5294 		return;
5295 
5296 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5297 		ill->ill_trace_disable = B_TRUE;
5298 		ill_trace_cleanup(ill);
5299 	}
5300 }
5301 
5302 void
5303 ill_untrace_ref(ill_t *ill)
5304 {
5305 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5306 
5307 	if (!ill->ill_trace_disable)
5308 		th_trace_unref(ill);
5309 }
5310 
5311 /*
5312  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5313  * failure, ipif_trace_disable is set.
5314  */
5315 static void
5316 ipif_trace_cleanup(const ipif_t *ipif)
5317 {
5318 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5319 }
5320 
5321 /*
5322  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5323  * failure, ill_trace_disable is set.
5324  */
5325 static void
5326 ill_trace_cleanup(const ill_t *ill)
5327 {
5328 	th_trace_cleanup(ill, ill->ill_trace_disable);
5329 }
5330 #endif /* DEBUG */
5331 
5332 void
5333 ipif_refhold_locked(ipif_t *ipif)
5334 {
5335 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5336 	ipif->ipif_refcnt++;
5337 	IPIF_TRACE_REF(ipif);
5338 }
5339 
5340 void
5341 ipif_refhold(ipif_t *ipif)
5342 {
5343 	ill_t	*ill;
5344 
5345 	ill = ipif->ipif_ill;
5346 	mutex_enter(&ill->ill_lock);
5347 	ipif->ipif_refcnt++;
5348 	IPIF_TRACE_REF(ipif);
5349 	mutex_exit(&ill->ill_lock);
5350 }
5351 
5352 /*
5353  * Must not be called while holding any locks. Otherwise if this is
5354  * the last reference to be released there is a chance of recursive mutex
5355  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5356  * to restart an ioctl.
5357  */
5358 void
5359 ipif_refrele(ipif_t *ipif)
5360 {
5361 	ill_t	*ill;
5362 
5363 	ill = ipif->ipif_ill;
5364 
5365 	mutex_enter(&ill->ill_lock);
5366 	ASSERT(ipif->ipif_refcnt != 0);
5367 	ipif->ipif_refcnt--;
5368 	IPIF_UNTRACE_REF(ipif);
5369 	if (ipif->ipif_refcnt != 0) {
5370 		mutex_exit(&ill->ill_lock);
5371 		return;
5372 	}
5373 
5374 	/* Drops the ill_lock */
5375 	ipif_ill_refrele_tail(ill);
5376 }
5377 
5378 ipif_t *
5379 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5380 {
5381 	ipif_t	*ipif;
5382 
5383 	mutex_enter(&ill->ill_lock);
5384 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5385 	    ipif != NULL; ipif = ipif->ipif_next) {
5386 		if (IPIF_IS_CONDEMNED(ipif))
5387 			continue;
5388 		ipif_refhold_locked(ipif);
5389 		mutex_exit(&ill->ill_lock);
5390 		return (ipif);
5391 	}
5392 	mutex_exit(&ill->ill_lock);
5393 	return (NULL);
5394 }
5395 
5396 /*
5397  * TODO: make this table extendible at run time
5398  * Return a pointer to the mac type info for 'mac_type'
5399  */
5400 static ip_m_t *
5401 ip_m_lookup(t_uscalar_t mac_type)
5402 {
5403 	ip_m_t	*ipm;
5404 
5405 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5406 		if (ipm->ip_m_mac_type == mac_type)
5407 			return (ipm);
5408 	return (NULL);
5409 }
5410 
5411 /*
5412  * Make a link layer address from the multicast IP address *addr.
5413  * To form the link layer address, invoke the ip_m_v*mapping function
5414  * associated with the link-layer type.
5415  */
5416 void
5417 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5418 {
5419 	ip_m_t *ipm;
5420 
5421 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5422 		return;
5423 
5424 	ASSERT(addr != NULL);
5425 
5426 	ipm = ip_m_lookup(ill->ill_mactype);
5427 	if (ipm == NULL ||
5428 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5429 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5430 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5431 		    ill->ill_name, ill->ill_mactype));
5432 		return;
5433 	}
5434 	if (ill->ill_isv6)
5435 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5436 	else
5437 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5438 }
5439 
5440 /*
5441  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5442  * ill is passed in to associate it with the correct interface.
5443  * If ire_arg is set, then we return the held IRE in that location.
5444  */
5445 int
5446 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5447     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5448     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5449 {
5450 	ire_t	*ire, *nire;
5451 	ire_t	*gw_ire = NULL;
5452 	ipif_t	*ipif = NULL;
5453 	uint_t	type;
5454 	int	match_flags = MATCH_IRE_TYPE;
5455 	tsol_gc_t *gc = NULL;
5456 	tsol_gcgrp_t *gcgrp = NULL;
5457 	boolean_t gcgrp_xtraref = B_FALSE;
5458 	boolean_t cgtp_broadcast;
5459 	boolean_t unbound = B_FALSE;
5460 
5461 	ip1dbg(("ip_rt_add:"));
5462 
5463 	if (ire_arg != NULL)
5464 		*ire_arg = NULL;
5465 
5466 	/*
5467 	 * If this is the case of RTF_HOST being set, then we set the netmask
5468 	 * to all ones (regardless if one was supplied).
5469 	 */
5470 	if (flags & RTF_HOST)
5471 		mask = IP_HOST_MASK;
5472 
5473 	/*
5474 	 * Prevent routes with a zero gateway from being created (since
5475 	 * interfaces can currently be plumbed and brought up no assigned
5476 	 * address).
5477 	 */
5478 	if (gw_addr == 0)
5479 		return (ENETUNREACH);
5480 	/*
5481 	 * Get the ipif, if any, corresponding to the gw_addr
5482 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5483 	 * we match on the gatway and destination to handle unnumbered pt-pt
5484 	 * interfaces.
5485 	 */
5486 	if (ill != NULL)
5487 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5488 	else
5489 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5490 	if (ipif != NULL) {
5491 		if (IS_VNI(ipif->ipif_ill)) {
5492 			ipif_refrele(ipif);
5493 			return (EINVAL);
5494 		}
5495 	}
5496 
5497 	/*
5498 	 * GateD will attempt to create routes with a loopback interface
5499 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5500 	 * these routes to be added, but create them as interface routes
5501 	 * since the gateway is an interface address.
5502 	 */
5503 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5504 		flags &= ~RTF_GATEWAY;
5505 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5506 		    mask == IP_HOST_MASK) {
5507 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5508 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5509 			    NULL);
5510 			if (ire != NULL) {
5511 				ire_refrele(ire);
5512 				ipif_refrele(ipif);
5513 				return (EEXIST);
5514 			}
5515 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5516 			    "for 0x%x\n", (void *)ipif,
5517 			    ipif->ipif_ire_type,
5518 			    ntohl(ipif->ipif_lcl_addr)));
5519 			ire = ire_create(
5520 			    (uchar_t *)&dst_addr,	/* dest address */
5521 			    (uchar_t *)&mask,		/* mask */
5522 			    NULL,			/* no gateway */
5523 			    ipif->ipif_ire_type,	/* LOOPBACK */
5524 			    ipif->ipif_ill,
5525 			    zoneid,
5526 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5527 			    NULL,
5528 			    ipst);
5529 
5530 			if (ire == NULL) {
5531 				ipif_refrele(ipif);
5532 				return (ENOMEM);
5533 			}
5534 			/* src address assigned by the caller? */
5535 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5536 				ire->ire_setsrc_addr = src_addr;
5537 
5538 			nire = ire_add(ire);
5539 			if (nire == NULL) {
5540 				/*
5541 				 * In the result of failure, ire_add() will have
5542 				 * already deleted the ire in question, so there
5543 				 * is no need to do that here.
5544 				 */
5545 				ipif_refrele(ipif);
5546 				return (ENOMEM);
5547 			}
5548 			/*
5549 			 * Check if it was a duplicate entry. This handles
5550 			 * the case of two racing route adds for the same route
5551 			 */
5552 			if (nire != ire) {
5553 				ASSERT(nire->ire_identical_ref > 1);
5554 				ire_delete(nire);
5555 				ire_refrele(nire);
5556 				ipif_refrele(ipif);
5557 				return (EEXIST);
5558 			}
5559 			ire = nire;
5560 			goto save_ire;
5561 		}
5562 	}
5563 
5564 	/*
5565 	 * The routes for multicast with CGTP are quite special in that
5566 	 * the gateway is the local interface address, yet RTF_GATEWAY
5567 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5568 	 * this undocumented and unusual use of multicast routes.
5569 	 */
5570 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5571 		flags &= ~RTF_GATEWAY;
5572 
5573 	/*
5574 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5575 	 * and the gateway address provided is one of the system's interface
5576 	 * addresses.  By using the routing socket interface and supplying an
5577 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5578 	 * specifying an interface route to be created is available which uses
5579 	 * the interface index that specifies the outgoing interface rather than
5580 	 * the address of an outgoing interface (which may not be able to
5581 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5582 	 * flag, routes can be specified which not only specify the next-hop to
5583 	 * be used when routing to a certain prefix, but also which outgoing
5584 	 * interface should be used.
5585 	 *
5586 	 * Previously, interfaces would have unique addresses assigned to them
5587 	 * and so the address assigned to a particular interface could be used
5588 	 * to identify a particular interface.  One exception to this was the
5589 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5590 	 *
5591 	 * With the advent of IPv6 and its link-local addresses, this
5592 	 * restriction was relaxed and interfaces could share addresses between
5593 	 * themselves.  In fact, typically all of the link-local interfaces on
5594 	 * an IPv6 node or router will have the same link-local address.  In
5595 	 * order to differentiate between these interfaces, the use of an
5596 	 * interface index is necessary and this index can be carried inside a
5597 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5598 	 * of using the interface index, however, is that all of the ipif's that
5599 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5600 	 * cannot be used to differentiate between ipif's (or logical
5601 	 * interfaces) that belong to the same ill (physical interface).
5602 	 *
5603 	 * For example, in the following case involving IPv4 interfaces and
5604 	 * logical interfaces
5605 	 *
5606 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5607 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5608 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5609 	 *
5610 	 * the ipif's corresponding to each of these interface routes can be
5611 	 * uniquely identified by the "gateway" (actually interface address).
5612 	 *
5613 	 * In this case involving multiple IPv6 default routes to a particular
5614 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5615 	 * default route is of interest:
5616 	 *
5617 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5618 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5619 	 */
5620 
5621 	/* RTF_GATEWAY not set */
5622 	if (!(flags & RTF_GATEWAY)) {
5623 		if (sp != NULL) {
5624 			ip2dbg(("ip_rt_add: gateway security attributes "
5625 			    "cannot be set with interface route\n"));
5626 			if (ipif != NULL)
5627 				ipif_refrele(ipif);
5628 			return (EINVAL);
5629 		}
5630 
5631 		/*
5632 		 * Whether or not ill (RTA_IFP) is set, we require that
5633 		 * the gateway is one of our local addresses.
5634 		 */
5635 		if (ipif == NULL)
5636 			return (ENETUNREACH);
5637 
5638 		/*
5639 		 * We use MATCH_IRE_ILL here. If the caller specified an
5640 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5641 		 * we use the ill derived from the gateway address.
5642 		 * We can always match the gateway address since we record it
5643 		 * in ire_gateway_addr.
5644 		 * We don't allow RTA_IFP to specify a different ill than the
5645 		 * one matching the ipif to make sure we can delete the route.
5646 		 */
5647 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5648 		if (ill == NULL) {
5649 			ill = ipif->ipif_ill;
5650 		} else if (ill != ipif->ipif_ill) {
5651 			ipif_refrele(ipif);
5652 			return (EINVAL);
5653 		}
5654 
5655 		/*
5656 		 * We check for an existing entry at this point.
5657 		 *
5658 		 * Since a netmask isn't passed in via the ioctl interface
5659 		 * (SIOCADDRT), we don't check for a matching netmask in that
5660 		 * case.
5661 		 */
5662 		if (!ioctl_msg)
5663 			match_flags |= MATCH_IRE_MASK;
5664 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5665 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5666 		    NULL);
5667 		if (ire != NULL) {
5668 			ire_refrele(ire);
5669 			ipif_refrele(ipif);
5670 			return (EEXIST);
5671 		}
5672 
5673 		/*
5674 		 * Some software (for example, GateD and Sun Cluster) attempts
5675 		 * to create (what amount to) IRE_PREFIX routes with the
5676 		 * loopback address as the gateway.  This is primarily done to
5677 		 * set up prefixes with the RTF_REJECT flag set (for example,
5678 		 * when generating aggregate routes.)
5679 		 *
5680 		 * If the IRE type (as defined by ill->ill_net_type) would be
5681 		 * IRE_LOOPBACK, then we map the request into a
5682 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5683 		 * these interface routes, by definition, can only be that.
5684 		 *
5685 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5686 		 * routine, but rather using ire_create() directly.
5687 		 *
5688 		 */
5689 		type = ill->ill_net_type;
5690 		if (type == IRE_LOOPBACK) {
5691 			type = IRE_IF_NORESOLVER;
5692 			flags |= RTF_BLACKHOLE;
5693 		}
5694 
5695 		/*
5696 		 * Create a copy of the IRE_IF_NORESOLVER or
5697 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5698 		 * gateway.
5699 		 */
5700 		ire = ire_create(
5701 		    (uchar_t *)&dst_addr,
5702 		    (uint8_t *)&mask,
5703 		    (uint8_t *)&gw_addr,
5704 		    type,
5705 		    ill,
5706 		    zoneid,
5707 		    flags,
5708 		    NULL,
5709 		    ipst);
5710 		if (ire == NULL) {
5711 			ipif_refrele(ipif);
5712 			return (ENOMEM);
5713 		}
5714 
5715 		/* src address assigned by the caller? */
5716 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5717 			ire->ire_setsrc_addr = src_addr;
5718 
5719 		nire = ire_add(ire);
5720 		if (nire == NULL) {
5721 			/*
5722 			 * In the result of failure, ire_add() will have
5723 			 * already deleted the ire in question, so there
5724 			 * is no need to do that here.
5725 			 */
5726 			ipif_refrele(ipif);
5727 			return (ENOMEM);
5728 		}
5729 		/*
5730 		 * Check if it was a duplicate entry. This handles
5731 		 * the case of two racing route adds for the same route
5732 		 */
5733 		if (nire != ire) {
5734 			ire_delete(nire);
5735 			ire_refrele(nire);
5736 			ipif_refrele(ipif);
5737 			return (EEXIST);
5738 		}
5739 		ire = nire;
5740 		goto save_ire;
5741 	}
5742 
5743 	/*
5744 	 * Get an interface IRE for the specified gateway.
5745 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5746 	 * gateway, it is currently unreachable and we fail the request
5747 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5748 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5749 	 * If RTA_IFP was specified we look on that particular ill.
5750 	 */
5751 	if (ill != NULL)
5752 		match_flags |= MATCH_IRE_ILL;
5753 
5754 	/* Check whether the gateway is reachable. */
5755 again:
5756 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5757 	if (flags & RTF_INDIRECT)
5758 		type |= IRE_OFFLINK;
5759 
5760 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5761 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5762 	if (gw_ire == NULL) {
5763 		/*
5764 		 * With IPMP, we allow host routes to influence in.mpathd's
5765 		 * target selection.  However, if the test addresses are on
5766 		 * their own network, the above lookup will fail since the
5767 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5768 		 * hidden test IREs to be found and try again.
5769 		 */
5770 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5771 			match_flags |= MATCH_IRE_TESTHIDDEN;
5772 			goto again;
5773 		}
5774 		if (ipif != NULL)
5775 			ipif_refrele(ipif);
5776 		return (ENETUNREACH);
5777 	}
5778 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5779 		ire_refrele(gw_ire);
5780 		if (ipif != NULL)
5781 			ipif_refrele(ipif);
5782 		return (ENETUNREACH);
5783 	}
5784 
5785 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5786 		unbound = B_TRUE;
5787 		if (ipst->ips_ip_strict_src_multihoming > 0)
5788 			ill = gw_ire->ire_ill;
5789 	}
5790 
5791 	/*
5792 	 * We create one of three types of IREs as a result of this request
5793 	 * based on the netmask.  A netmask of all ones (which is automatically
5794 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5795 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5796 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5797 	 * destination prefix.
5798 	 */
5799 	if (mask == IP_HOST_MASK)
5800 		type = IRE_HOST;
5801 	else if (mask == 0)
5802 		type = IRE_DEFAULT;
5803 	else
5804 		type = IRE_PREFIX;
5805 
5806 	/* check for a duplicate entry */
5807 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5808 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5809 	    0, ipst, NULL);
5810 	if (ire != NULL) {
5811 		if (ipif != NULL)
5812 			ipif_refrele(ipif);
5813 		ire_refrele(gw_ire);
5814 		ire_refrele(ire);
5815 		return (EEXIST);
5816 	}
5817 
5818 	/* Security attribute exists */
5819 	if (sp != NULL) {
5820 		tsol_gcgrp_addr_t ga;
5821 
5822 		/* find or create the gateway credentials group */
5823 		ga.ga_af = AF_INET;
5824 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5825 
5826 		/* we hold reference to it upon success */
5827 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5828 		if (gcgrp == NULL) {
5829 			if (ipif != NULL)
5830 				ipif_refrele(ipif);
5831 			ire_refrele(gw_ire);
5832 			return (ENOMEM);
5833 		}
5834 
5835 		/*
5836 		 * Create and add the security attribute to the group; a
5837 		 * reference to the group is made upon allocating a new
5838 		 * entry successfully.  If it finds an already-existing
5839 		 * entry for the security attribute in the group, it simply
5840 		 * returns it and no new reference is made to the group.
5841 		 */
5842 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5843 		if (gc == NULL) {
5844 			if (ipif != NULL)
5845 				ipif_refrele(ipif);
5846 			/* release reference held by gcgrp_lookup */
5847 			GCGRP_REFRELE(gcgrp);
5848 			ire_refrele(gw_ire);
5849 			return (ENOMEM);
5850 		}
5851 	}
5852 
5853 	/* Create the IRE. */
5854 	ire = ire_create(
5855 	    (uchar_t *)&dst_addr,		/* dest address */
5856 	    (uchar_t *)&mask,			/* mask */
5857 	    (uchar_t *)&gw_addr,		/* gateway address */
5858 	    (ushort_t)type,			/* IRE type */
5859 	    ill,
5860 	    zoneid,
5861 	    flags,
5862 	    gc,					/* security attribute */
5863 	    ipst);
5864 
5865 	/*
5866 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5867 	 * reference to the 'gcgrp'. We can now release the extra reference
5868 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5869 	 */
5870 	if (gcgrp_xtraref)
5871 		GCGRP_REFRELE(gcgrp);
5872 	if (ire == NULL) {
5873 		if (gc != NULL)
5874 			GC_REFRELE(gc);
5875 		if (ipif != NULL)
5876 			ipif_refrele(ipif);
5877 		ire_refrele(gw_ire);
5878 		return (ENOMEM);
5879 	}
5880 
5881 	/* Before we add, check if an extra CGTP broadcast is needed */
5882 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5883 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5884 
5885 	/* src address assigned by the caller? */
5886 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5887 		ire->ire_setsrc_addr = src_addr;
5888 
5889 	ire->ire_unbound = unbound;
5890 
5891 	/*
5892 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5893 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5894 	 */
5895 
5896 	/* Add the new IRE. */
5897 	nire = ire_add(ire);
5898 	if (nire == NULL) {
5899 		/*
5900 		 * In the result of failure, ire_add() will have
5901 		 * already deleted the ire in question, so there
5902 		 * is no need to do that here.
5903 		 */
5904 		if (ipif != NULL)
5905 			ipif_refrele(ipif);
5906 		ire_refrele(gw_ire);
5907 		return (ENOMEM);
5908 	}
5909 	/*
5910 	 * Check if it was a duplicate entry. This handles
5911 	 * the case of two racing route adds for the same route
5912 	 */
5913 	if (nire != ire) {
5914 		ire_delete(nire);
5915 		ire_refrele(nire);
5916 		if (ipif != NULL)
5917 			ipif_refrele(ipif);
5918 		ire_refrele(gw_ire);
5919 		return (EEXIST);
5920 	}
5921 	ire = nire;
5922 
5923 	if (flags & RTF_MULTIRT) {
5924 		/*
5925 		 * Invoke the CGTP (multirouting) filtering module
5926 		 * to add the dst address in the filtering database.
5927 		 * Replicated inbound packets coming from that address
5928 		 * will be filtered to discard the duplicates.
5929 		 * It is not necessary to call the CGTP filter hook
5930 		 * when the dst address is a broadcast or multicast,
5931 		 * because an IP source address cannot be a broadcast
5932 		 * or a multicast.
5933 		 */
5934 		if (cgtp_broadcast) {
5935 			ip_cgtp_bcast_add(ire, ipst);
5936 			goto save_ire;
5937 		}
5938 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5939 		    !CLASSD(ire->ire_addr)) {
5940 			int res;
5941 			ipif_t *src_ipif;
5942 
5943 			/* Find the source address corresponding to gw_ire */
5944 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5945 			    NULL, zoneid, ipst);
5946 			if (src_ipif != NULL) {
5947 				res = ipst->ips_ip_cgtp_filter_ops->
5948 				    cfo_add_dest_v4(
5949 				    ipst->ips_netstack->netstack_stackid,
5950 				    ire->ire_addr,
5951 				    ire->ire_gateway_addr,
5952 				    ire->ire_setsrc_addr,
5953 				    src_ipif->ipif_lcl_addr);
5954 				ipif_refrele(src_ipif);
5955 			} else {
5956 				res = EADDRNOTAVAIL;
5957 			}
5958 			if (res != 0) {
5959 				if (ipif != NULL)
5960 					ipif_refrele(ipif);
5961 				ire_refrele(gw_ire);
5962 				ire_delete(ire);
5963 				ire_refrele(ire);	/* Held in ire_add */
5964 				return (res);
5965 			}
5966 		}
5967 	}
5968 
5969 save_ire:
5970 	if (gw_ire != NULL) {
5971 		ire_refrele(gw_ire);
5972 		gw_ire = NULL;
5973 	}
5974 	if (ill != NULL) {
5975 		/*
5976 		 * Save enough information so that we can recreate the IRE if
5977 		 * the interface goes down and then up.  The metrics associated
5978 		 * with the route will be saved as well when rts_setmetrics() is
5979 		 * called after the IRE has been created.  In the case where
5980 		 * memory cannot be allocated, none of this information will be
5981 		 * saved.
5982 		 */
5983 		ill_save_ire(ill, ire);
5984 	}
5985 	if (ioctl_msg)
5986 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5987 	if (ire_arg != NULL) {
5988 		/*
5989 		 * Store the ire that was successfully added into where ire_arg
5990 		 * points to so that callers don't have to look it up
5991 		 * themselves (but they are responsible for ire_refrele()ing
5992 		 * the ire when they are finished with it).
5993 		 */
5994 		*ire_arg = ire;
5995 	} else {
5996 		ire_refrele(ire);		/* Held in ire_add */
5997 	}
5998 	if (ipif != NULL)
5999 		ipif_refrele(ipif);
6000 	return (0);
6001 }
6002 
6003 /*
6004  * ip_rt_delete is called to delete an IPv4 route.
6005  * ill is passed in to associate it with the correct interface.
6006  */
6007 /* ARGSUSED4 */
6008 int
6009 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6010     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
6011     ip_stack_t *ipst, zoneid_t zoneid)
6012 {
6013 	ire_t	*ire = NULL;
6014 	ipif_t	*ipif;
6015 	uint_t	type;
6016 	uint_t	match_flags = MATCH_IRE_TYPE;
6017 	int	err = 0;
6018 
6019 	ip1dbg(("ip_rt_delete:"));
6020 	/*
6021 	 * If this is the case of RTF_HOST being set, then we set the netmask
6022 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6023 	 */
6024 	if (flags & RTF_HOST) {
6025 		mask = IP_HOST_MASK;
6026 		match_flags |= MATCH_IRE_MASK;
6027 	} else if (rtm_addrs & RTA_NETMASK) {
6028 		match_flags |= MATCH_IRE_MASK;
6029 	}
6030 
6031 	/*
6032 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6033 	 * we check if the gateway address is one of our interfaces first,
6034 	 * and fall back on RTF_GATEWAY routes.
6035 	 *
6036 	 * This makes it possible to delete an original
6037 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6038 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6039 	 * and those can not be deleted here.
6040 	 *
6041 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6042 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6043 	 * otherwise we use the ill derived from the gateway address.
6044 	 * We can always match the gateway address since we record it
6045 	 * in ire_gateway_addr.
6046 	 *
6047 	 * For more detail on specifying routes by gateway address and by
6048 	 * interface index, see the comments in ip_rt_add().
6049 	 */
6050 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6051 	if (ipif != NULL) {
6052 		ill_t	*ill_match;
6053 
6054 		if (ill != NULL)
6055 			ill_match = ill;
6056 		else
6057 			ill_match = ipif->ipif_ill;
6058 
6059 		match_flags |= MATCH_IRE_ILL;
6060 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6061 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6062 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6063 			    match_flags, 0, ipst, NULL);
6064 		}
6065 		if (ire == NULL) {
6066 			match_flags |= MATCH_IRE_GW;
6067 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6068 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6069 			    match_flags, 0, ipst, NULL);
6070 		}
6071 		/* Avoid deleting routes created by kernel from an ipif */
6072 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6073 			ire_refrele(ire);
6074 			ire = NULL;
6075 		}
6076 
6077 		/* Restore in case we didn't find a match */
6078 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6079 	}
6080 
6081 	if (ire == NULL) {
6082 		/*
6083 		 * At this point, the gateway address is not one of our own
6084 		 * addresses or a matching interface route was not found.  We
6085 		 * set the IRE type to lookup based on whether
6086 		 * this is a host route, a default route or just a prefix.
6087 		 *
6088 		 * If an ill was passed in, then the lookup is based on an
6089 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6090 		 */
6091 		match_flags |= MATCH_IRE_GW;
6092 		if (ill != NULL)
6093 			match_flags |= MATCH_IRE_ILL;
6094 		if (mask == IP_HOST_MASK)
6095 			type = IRE_HOST;
6096 		else if (mask == 0)
6097 			type = IRE_DEFAULT;
6098 		else
6099 			type = IRE_PREFIX;
6100 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6101 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6102 	}
6103 
6104 	if (ipif != NULL) {
6105 		ipif_refrele(ipif);
6106 		ipif = NULL;
6107 	}
6108 
6109 	if (ire == NULL)
6110 		return (ESRCH);
6111 
6112 	if (ire->ire_flags & RTF_MULTIRT) {
6113 		/*
6114 		 * Invoke the CGTP (multirouting) filtering module
6115 		 * to remove the dst address from the filtering database.
6116 		 * Packets coming from that address will no longer be
6117 		 * filtered to remove duplicates.
6118 		 */
6119 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6120 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6121 			    ipst->ips_netstack->netstack_stackid,
6122 			    ire->ire_addr, ire->ire_gateway_addr);
6123 		}
6124 		ip_cgtp_bcast_delete(ire, ipst);
6125 	}
6126 
6127 	ill = ire->ire_ill;
6128 	if (ill != NULL)
6129 		ill_remove_saved_ire(ill, ire);
6130 	if (ioctl_msg)
6131 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6132 	ire_delete(ire);
6133 	ire_refrele(ire);
6134 	return (err);
6135 }
6136 
6137 /*
6138  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6139  */
6140 /* ARGSUSED */
6141 int
6142 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6143     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6144 {
6145 	ipaddr_t dst_addr;
6146 	ipaddr_t gw_addr;
6147 	ipaddr_t mask;
6148 	int error = 0;
6149 	mblk_t *mp1;
6150 	struct rtentry *rt;
6151 	ipif_t *ipif = NULL;
6152 	ip_stack_t	*ipst;
6153 
6154 	ASSERT(q->q_next == NULL);
6155 	ipst = CONNQ_TO_IPST(q);
6156 
6157 	ip1dbg(("ip_siocaddrt:"));
6158 	/* Existence of mp1 verified in ip_wput_nondata */
6159 	mp1 = mp->b_cont->b_cont;
6160 	rt = (struct rtentry *)mp1->b_rptr;
6161 
6162 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6163 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6164 
6165 	/*
6166 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6167 	 * to a particular host address.  In this case, we set the netmask to
6168 	 * all ones for the particular destination address.  Otherwise,
6169 	 * determine the netmask to be used based on dst_addr and the interfaces
6170 	 * in use.
6171 	 */
6172 	if (rt->rt_flags & RTF_HOST) {
6173 		mask = IP_HOST_MASK;
6174 	} else {
6175 		/*
6176 		 * Note that ip_subnet_mask returns a zero mask in the case of
6177 		 * default (an all-zeroes address).
6178 		 */
6179 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6180 	}
6181 
6182 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6183 	    B_TRUE, NULL, ipst, ALL_ZONES);
6184 	if (ipif != NULL)
6185 		ipif_refrele(ipif);
6186 	return (error);
6187 }
6188 
6189 /*
6190  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6191  */
6192 /* ARGSUSED */
6193 int
6194 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6195     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6196 {
6197 	ipaddr_t dst_addr;
6198 	ipaddr_t gw_addr;
6199 	ipaddr_t mask;
6200 	int error;
6201 	mblk_t *mp1;
6202 	struct rtentry *rt;
6203 	ipif_t *ipif = NULL;
6204 	ip_stack_t	*ipst;
6205 
6206 	ASSERT(q->q_next == NULL);
6207 	ipst = CONNQ_TO_IPST(q);
6208 
6209 	ip1dbg(("ip_siocdelrt:"));
6210 	/* Existence of mp1 verified in ip_wput_nondata */
6211 	mp1 = mp->b_cont->b_cont;
6212 	rt = (struct rtentry *)mp1->b_rptr;
6213 
6214 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6215 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6216 
6217 	/*
6218 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6219 	 * to a particular host address.  In this case, we set the netmask to
6220 	 * all ones for the particular destination address.  Otherwise,
6221 	 * determine the netmask to be used based on dst_addr and the interfaces
6222 	 * in use.
6223 	 */
6224 	if (rt->rt_flags & RTF_HOST) {
6225 		mask = IP_HOST_MASK;
6226 	} else {
6227 		/*
6228 		 * Note that ip_subnet_mask returns a zero mask in the case of
6229 		 * default (an all-zeroes address).
6230 		 */
6231 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6232 	}
6233 
6234 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6235 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6236 	    ipst, ALL_ZONES);
6237 	if (ipif != NULL)
6238 		ipif_refrele(ipif);
6239 	return (error);
6240 }
6241 
6242 /*
6243  * Enqueue the mp onto the ipsq, chained by b_next.
6244  * b_prev stores the function to be executed later, and b_queue the queue
6245  * where this mp originated.
6246  */
6247 void
6248 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6249     ill_t *pending_ill)
6250 {
6251 	conn_t	*connp;
6252 	ipxop_t *ipx = ipsq->ipsq_xop;
6253 
6254 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6255 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6256 	ASSERT(func != NULL);
6257 
6258 	mp->b_queue = q;
6259 	mp->b_prev = (void *)func;
6260 	mp->b_next = NULL;
6261 
6262 	switch (type) {
6263 	case CUR_OP:
6264 		if (ipx->ipx_mptail != NULL) {
6265 			ASSERT(ipx->ipx_mphead != NULL);
6266 			ipx->ipx_mptail->b_next = mp;
6267 		} else {
6268 			ASSERT(ipx->ipx_mphead == NULL);
6269 			ipx->ipx_mphead = mp;
6270 		}
6271 		ipx->ipx_mptail = mp;
6272 		break;
6273 
6274 	case NEW_OP:
6275 		if (ipsq->ipsq_xopq_mptail != NULL) {
6276 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6277 			ipsq->ipsq_xopq_mptail->b_next = mp;
6278 		} else {
6279 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6280 			ipsq->ipsq_xopq_mphead = mp;
6281 		}
6282 		ipsq->ipsq_xopq_mptail = mp;
6283 		ipx->ipx_ipsq_queued = B_TRUE;
6284 		break;
6285 
6286 	case SWITCH_OP:
6287 		ASSERT(ipsq->ipsq_swxop != NULL);
6288 		/* only one switch operation is currently allowed */
6289 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6290 		ipsq->ipsq_switch_mp = mp;
6291 		ipx->ipx_ipsq_queued = B_TRUE;
6292 		break;
6293 	default:
6294 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6295 	}
6296 
6297 	if (CONN_Q(q) && pending_ill != NULL) {
6298 		connp = Q_TO_CONN(q);
6299 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6300 		connp->conn_oper_pending_ill = pending_ill;
6301 	}
6302 }
6303 
6304 /*
6305  * Dequeue the next message that requested exclusive access to this IPSQ's
6306  * xop.  Specifically:
6307  *
6308  *  1. If we're still processing the current operation on `ipsq', then
6309  *     dequeue the next message for the operation (from ipx_mphead), or
6310  *     return NULL if there are no queued messages for the operation.
6311  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6312  *
6313  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6314  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6315  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6316  *     leaving IPMP groups and require a careful dance -- see the comments
6317  *     in-line below for details.  If we're leaving a group xop or if we're
6318  *     joining a group xop and become writer on it, then we proceed to (3).
6319  *     Otherwise, we return NULL and exit the xop.
6320  *
6321  *  3. For each IPSQ in the xop, return any switch operation stored on
6322  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6323  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6324  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6325  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6326  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6327  *     each phyint in the group, including the IPMP meta-interface phyint.
6328  */
6329 static mblk_t *
6330 ipsq_dq(ipsq_t *ipsq)
6331 {
6332 	ill_t	*illv4, *illv6;
6333 	mblk_t	*mp;
6334 	ipsq_t	*xopipsq;
6335 	ipsq_t	*leftipsq = NULL;
6336 	ipxop_t *ipx;
6337 	phyint_t *phyi = ipsq->ipsq_phyint;
6338 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6339 	boolean_t emptied = B_FALSE;
6340 
6341 	/*
6342 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6343 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6344 	 */
6345 	rw_enter(&ipst->ips_ill_g_lock,
6346 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6347 	mutex_enter(&ipsq->ipsq_lock);
6348 	ipx = ipsq->ipsq_xop;
6349 	mutex_enter(&ipx->ipx_lock);
6350 
6351 	/*
6352 	 * Dequeue the next message associated with the current exclusive
6353 	 * operation, if any.
6354 	 */
6355 	if ((mp = ipx->ipx_mphead) != NULL) {
6356 		ipx->ipx_mphead = mp->b_next;
6357 		if (ipx->ipx_mphead == NULL)
6358 			ipx->ipx_mptail = NULL;
6359 		mp->b_next = (void *)ipsq;
6360 		goto out;
6361 	}
6362 
6363 	if (ipx->ipx_current_ipif != NULL)
6364 		goto empty;
6365 
6366 	if (ipsq->ipsq_swxop != NULL) {
6367 		/*
6368 		 * The exclusive operation that is now being completed has
6369 		 * requested a switch to a different xop.  This happens
6370 		 * when an interface joins or leaves an IPMP group.  Joins
6371 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6372 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6373 		 * (phyint_free()), or interface plumb for an ill type
6374 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6375 		 *
6376 		 * Xop switches are not allowed on the IPMP meta-interface.
6377 		 */
6378 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6379 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6380 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6381 
6382 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6383 			/*
6384 			 * We're switching back to our own xop, so we have two
6385 			 * xop's to drain/exit: our own, and the group xop
6386 			 * that we are leaving.
6387 			 *
6388 			 * First, pull ourselves out of the group ipsq list.
6389 			 * This is safe since we're writer on ill_g_lock.
6390 			 */
6391 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6392 
6393 			xopipsq = ipx->ipx_ipsq;
6394 			while (xopipsq->ipsq_next != ipsq)
6395 				xopipsq = xopipsq->ipsq_next;
6396 
6397 			xopipsq->ipsq_next = ipsq->ipsq_next;
6398 			ipsq->ipsq_next = ipsq;
6399 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6400 			ipsq->ipsq_swxop = NULL;
6401 
6402 			/*
6403 			 * Second, prepare to exit the group xop.  The actual
6404 			 * ipsq_exit() is done at the end of this function
6405 			 * since we cannot hold any locks across ipsq_exit().
6406 			 * Note that although we drop the group's ipx_lock, no
6407 			 * threads can proceed since we're still ipx_writer.
6408 			 */
6409 			leftipsq = xopipsq;
6410 			mutex_exit(&ipx->ipx_lock);
6411 
6412 			/*
6413 			 * Third, set ipx to point to our own xop (which was
6414 			 * inactive and therefore can be entered).
6415 			 */
6416 			ipx = ipsq->ipsq_xop;
6417 			mutex_enter(&ipx->ipx_lock);
6418 			ASSERT(ipx->ipx_writer == NULL);
6419 			ASSERT(ipx->ipx_current_ipif == NULL);
6420 		} else {
6421 			/*
6422 			 * We're switching from our own xop to a group xop.
6423 			 * The requestor of the switch must ensure that the
6424 			 * group xop cannot go away (e.g. by ensuring the
6425 			 * phyint associated with the xop cannot go away).
6426 			 *
6427 			 * If we can become writer on our new xop, then we'll
6428 			 * do the drain.  Otherwise, the current writer of our
6429 			 * new xop will do the drain when it exits.
6430 			 *
6431 			 * First, splice ourselves into the group IPSQ list.
6432 			 * This is safe since we're writer on ill_g_lock.
6433 			 */
6434 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6435 
6436 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6437 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6438 				xopipsq = xopipsq->ipsq_next;
6439 
6440 			xopipsq->ipsq_next = ipsq;
6441 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6442 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6443 			ipsq->ipsq_swxop = NULL;
6444 
6445 			/*
6446 			 * Second, exit our own xop, since it's now unused.
6447 			 * This is safe since we've got the only reference.
6448 			 */
6449 			ASSERT(ipx->ipx_writer == curthread);
6450 			ipx->ipx_writer = NULL;
6451 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6452 			ipx->ipx_ipsq_queued = B_FALSE;
6453 			mutex_exit(&ipx->ipx_lock);
6454 
6455 			/*
6456 			 * Third, set ipx to point to our new xop, and check
6457 			 * if we can become writer on it.  If we cannot, then
6458 			 * the current writer will drain the IPSQ group when
6459 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6460 			 * because we're still holding ipsq_lock.
6461 			 */
6462 			ipx = ipsq->ipsq_xop;
6463 			mutex_enter(&ipx->ipx_lock);
6464 			if (ipx->ipx_writer != NULL ||
6465 			    ipx->ipx_current_ipif != NULL) {
6466 				goto out;
6467 			}
6468 		}
6469 
6470 		/*
6471 		 * Fourth, become writer on our new ipx before we continue
6472 		 * with the drain.  Note that we never dropped ipsq_lock
6473 		 * above, so no other thread could've raced with us to
6474 		 * become writer first.  Also, we're holding ipx_lock, so
6475 		 * no other thread can examine the ipx right now.
6476 		 */
6477 		ASSERT(ipx->ipx_current_ipif == NULL);
6478 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6479 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6480 		ipx->ipx_writer = curthread;
6481 		ipx->ipx_forced = B_FALSE;
6482 #ifdef DEBUG
6483 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6484 #endif
6485 	}
6486 
6487 	xopipsq = ipsq;
6488 	do {
6489 		/*
6490 		 * So that other operations operate on a consistent and
6491 		 * complete phyint, a switch message on an IPSQ must be
6492 		 * handled prior to any other operations on that IPSQ.
6493 		 */
6494 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6495 			xopipsq->ipsq_switch_mp = NULL;
6496 			ASSERT(mp->b_next == NULL);
6497 			mp->b_next = (void *)xopipsq;
6498 			goto out;
6499 		}
6500 
6501 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6502 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6503 			if (xopipsq->ipsq_xopq_mphead == NULL)
6504 				xopipsq->ipsq_xopq_mptail = NULL;
6505 			mp->b_next = (void *)xopipsq;
6506 			goto out;
6507 		}
6508 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6509 empty:
6510 	/*
6511 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6512 	 * new messages can end up on any IPSQ in the xop.
6513 	 */
6514 	ipx->ipx_writer = NULL;
6515 	ipx->ipx_forced = B_FALSE;
6516 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6517 	ipx->ipx_ipsq_queued = B_FALSE;
6518 	emptied = B_TRUE;
6519 #ifdef	DEBUG
6520 	ipx->ipx_depth = 0;
6521 #endif
6522 out:
6523 	mutex_exit(&ipx->ipx_lock);
6524 	mutex_exit(&ipsq->ipsq_lock);
6525 
6526 	/*
6527 	 * If we completely emptied the xop, then wake up any threads waiting
6528 	 * to enter any of the IPSQ's associated with it.
6529 	 */
6530 	if (emptied) {
6531 		xopipsq = ipsq;
6532 		do {
6533 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6534 				continue;
6535 
6536 			illv4 = phyi->phyint_illv4;
6537 			illv6 = phyi->phyint_illv6;
6538 
6539 			GRAB_ILL_LOCKS(illv4, illv6);
6540 			if (illv4 != NULL)
6541 				cv_broadcast(&illv4->ill_cv);
6542 			if (illv6 != NULL)
6543 				cv_broadcast(&illv6->ill_cv);
6544 			RELEASE_ILL_LOCKS(illv4, illv6);
6545 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6546 	}
6547 	rw_exit(&ipst->ips_ill_g_lock);
6548 
6549 	/*
6550 	 * Now that all locks are dropped, exit the IPSQ we left.
6551 	 */
6552 	if (leftipsq != NULL)
6553 		ipsq_exit(leftipsq);
6554 
6555 	return (mp);
6556 }
6557 
6558 /*
6559  * Return completion status of previously initiated DLPI operations on
6560  * ills in the purview of an ipsq.
6561  */
6562 static boolean_t
6563 ipsq_dlpi_done(ipsq_t *ipsq)
6564 {
6565 	ipsq_t		*ipsq_start;
6566 	phyint_t	*phyi;
6567 	ill_t		*ill;
6568 
6569 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6570 	ipsq_start = ipsq;
6571 
6572 	do {
6573 		/*
6574 		 * The only current users of this function are ipsq_try_enter
6575 		 * and ipsq_enter which have made sure that ipsq_writer is
6576 		 * NULL before we reach here. ill_dlpi_pending is modified
6577 		 * only by an ipsq writer
6578 		 */
6579 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6580 		phyi = ipsq->ipsq_phyint;
6581 		/*
6582 		 * phyi could be NULL if a phyint that is part of an
6583 		 * IPMP group is being unplumbed. A more detailed
6584 		 * comment is in ipmp_grp_update_kstats()
6585 		 */
6586 		if (phyi != NULL) {
6587 			ill = phyi->phyint_illv4;
6588 			if (ill != NULL &&
6589 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6590 			    ill->ill_arl_dlpi_pending))
6591 				return (B_FALSE);
6592 
6593 			ill = phyi->phyint_illv6;
6594 			if (ill != NULL &&
6595 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6596 				return (B_FALSE);
6597 		}
6598 
6599 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6600 
6601 	return (B_TRUE);
6602 }
6603 
6604 /*
6605  * Enter the ipsq corresponding to ill, by waiting synchronously till
6606  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6607  * will have to drain completely before ipsq_enter returns success.
6608  * ipx_current_ipif will be set if some exclusive op is in progress,
6609  * and the ipsq_exit logic will start the next enqueued op after
6610  * completion of the current op. If 'force' is used, we don't wait
6611  * for the enqueued ops. This is needed when a conn_close wants to
6612  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6613  * of an ill can also use this option. But we dont' use it currently.
6614  */
6615 #define	ENTER_SQ_WAIT_TICKS 100
6616 boolean_t
6617 ipsq_enter(ill_t *ill, boolean_t force, int type)
6618 {
6619 	ipsq_t	*ipsq;
6620 	ipxop_t *ipx;
6621 	boolean_t waited_enough = B_FALSE;
6622 	ip_stack_t *ipst = ill->ill_ipst;
6623 
6624 	/*
6625 	 * Note that the relationship between ill and ipsq is fixed as long as
6626 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6627 	 * relationship between the IPSQ and xop cannot change.  However,
6628 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6629 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6630 	 * waking up all ills in the xop when it becomes available.
6631 	 */
6632 	for (;;) {
6633 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6634 		mutex_enter(&ill->ill_lock);
6635 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6636 			mutex_exit(&ill->ill_lock);
6637 			rw_exit(&ipst->ips_ill_g_lock);
6638 			return (B_FALSE);
6639 		}
6640 
6641 		ipsq = ill->ill_phyint->phyint_ipsq;
6642 		mutex_enter(&ipsq->ipsq_lock);
6643 		ipx = ipsq->ipsq_xop;
6644 		mutex_enter(&ipx->ipx_lock);
6645 
6646 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6647 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6648 		    waited_enough))
6649 			break;
6650 
6651 		rw_exit(&ipst->ips_ill_g_lock);
6652 
6653 		if (!force || ipx->ipx_writer != NULL) {
6654 			mutex_exit(&ipx->ipx_lock);
6655 			mutex_exit(&ipsq->ipsq_lock);
6656 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6657 		} else {
6658 			mutex_exit(&ipx->ipx_lock);
6659 			mutex_exit(&ipsq->ipsq_lock);
6660 			(void) cv_reltimedwait(&ill->ill_cv,
6661 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6662 			waited_enough = B_TRUE;
6663 		}
6664 		mutex_exit(&ill->ill_lock);
6665 	}
6666 
6667 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6668 	ASSERT(ipx->ipx_reentry_cnt == 0);
6669 	ipx->ipx_writer = curthread;
6670 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6671 	ipx->ipx_reentry_cnt++;
6672 #ifdef DEBUG
6673 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6674 #endif
6675 	mutex_exit(&ipx->ipx_lock);
6676 	mutex_exit(&ipsq->ipsq_lock);
6677 	mutex_exit(&ill->ill_lock);
6678 	rw_exit(&ipst->ips_ill_g_lock);
6679 
6680 	return (B_TRUE);
6681 }
6682 
6683 /*
6684  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6685  * across the call to the core interface ipsq_try_enter() and hence calls this
6686  * function directly. This is explained more fully in ipif_set_values().
6687  * In order to support the above constraint, ipsq_try_enter is implemented as
6688  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6689  */
6690 static ipsq_t *
6691 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6692     int type, boolean_t reentry_ok)
6693 {
6694 	ipsq_t	*ipsq;
6695 	ipxop_t	*ipx;
6696 	ip_stack_t *ipst = ill->ill_ipst;
6697 
6698 	/*
6699 	 * lock ordering:
6700 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6701 	 *
6702 	 * ipx of an ipsq can't change when ipsq_lock is held.
6703 	 */
6704 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6705 	GRAB_CONN_LOCK(q);
6706 	mutex_enter(&ill->ill_lock);
6707 	ipsq = ill->ill_phyint->phyint_ipsq;
6708 	mutex_enter(&ipsq->ipsq_lock);
6709 	ipx = ipsq->ipsq_xop;
6710 	mutex_enter(&ipx->ipx_lock);
6711 
6712 	/*
6713 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6714 	 *    (Note: If the caller does not specify reentry_ok then neither
6715 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6716 	 *    again. Otherwise it can lead to an infinite loop
6717 	 * 2. Enter the ipsq if there is no current writer and this attempted
6718 	 *    entry is part of the current operation
6719 	 * 3. Enter the ipsq if there is no current writer and this is a new
6720 	 *    operation and the operation queue is empty and there is no
6721 	 *    operation currently in progress and if all previously initiated
6722 	 *    DLPI operations have completed.
6723 	 */
6724 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6725 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6726 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6727 	    ipsq_dlpi_done(ipsq))))) {
6728 		/* Success. */
6729 		ipx->ipx_reentry_cnt++;
6730 		ipx->ipx_writer = curthread;
6731 		ipx->ipx_forced = B_FALSE;
6732 		mutex_exit(&ipx->ipx_lock);
6733 		mutex_exit(&ipsq->ipsq_lock);
6734 		mutex_exit(&ill->ill_lock);
6735 		RELEASE_CONN_LOCK(q);
6736 #ifdef DEBUG
6737 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6738 #endif
6739 		return (ipsq);
6740 	}
6741 
6742 	if (func != NULL)
6743 		ipsq_enq(ipsq, q, mp, func, type, ill);
6744 
6745 	mutex_exit(&ipx->ipx_lock);
6746 	mutex_exit(&ipsq->ipsq_lock);
6747 	mutex_exit(&ill->ill_lock);
6748 	RELEASE_CONN_LOCK(q);
6749 	return (NULL);
6750 }
6751 
6752 /*
6753  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6754  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6755  * There is one ipsq per phyint. The ipsq
6756  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6757  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6758  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6759  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6760  * up the interface) and are enqueued in ipx_mphead.
6761  *
6762  * If a thread does not want to reenter the ipsq when it is already writer,
6763  * it must make sure that the specified reentry point to be called later
6764  * when the ipsq is empty, nor any code path starting from the specified reentry
6765  * point must never ever try to enter the ipsq again. Otherwise it can lead
6766  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6767  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6768  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6769  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6770  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6771  * ioctl if the current ioctl has completed. If the current ioctl is still
6772  * in progress it simply returns. The current ioctl could be waiting for
6773  * a response from another module (the driver or could be waiting for
6774  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6775  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6776  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6777  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6778  * all associated DLPI operations have completed.
6779  */
6780 
6781 /*
6782  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6783  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6784  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6785  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6786  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6787  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6788  */
6789 ipsq_t *
6790 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6791     ipsq_func_t func, int type, boolean_t reentry_ok)
6792 {
6793 	ip_stack_t	*ipst;
6794 	ipsq_t		*ipsq;
6795 
6796 	/* Only 1 of ipif or ill can be specified */
6797 	ASSERT((ipif != NULL) ^ (ill != NULL));
6798 
6799 	if (ipif != NULL)
6800 		ill = ipif->ipif_ill;
6801 	ipst = ill->ill_ipst;
6802 
6803 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6804 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6805 	rw_exit(&ipst->ips_ill_g_lock);
6806 
6807 	return (ipsq);
6808 }
6809 
6810 /*
6811  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6812  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6813  * cannot be entered, the mp is queued for completion.
6814  */
6815 void
6816 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6817     boolean_t reentry_ok)
6818 {
6819 	ipsq_t	*ipsq;
6820 
6821 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6822 
6823 	/*
6824 	 * Drop the caller's refhold on the ill.  This is safe since we either
6825 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6826 	 * IPSQ, in which case we return without accessing ill anymore.  This
6827 	 * is needed because func needs to see the correct refcount.
6828 	 * e.g. removeif can work only then.
6829 	 */
6830 	ill_refrele(ill);
6831 	if (ipsq != NULL) {
6832 		(*func)(ipsq, q, mp, NULL);
6833 		ipsq_exit(ipsq);
6834 	}
6835 }
6836 
6837 /*
6838  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6839  * prior to exiting.  Caller must be writer on the specified IPSQ.
6840  */
6841 void
6842 ipsq_exit(ipsq_t *ipsq)
6843 {
6844 	mblk_t *mp;
6845 	ipsq_t *mp_ipsq;
6846 	queue_t	*q;
6847 	phyint_t *phyi;
6848 	ipsq_func_t func;
6849 
6850 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6851 
6852 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6853 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6854 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6855 		return;
6856 	}
6857 
6858 	for (;;) {
6859 		phyi = ipsq->ipsq_phyint;
6860 		mp = ipsq_dq(ipsq);
6861 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6862 
6863 		/*
6864 		 * If we've changed to a new IPSQ, and the phyint associated
6865 		 * with the old one has gone away, free the old IPSQ.  Note
6866 		 * that this cannot happen while the IPSQ is in a group.
6867 		 */
6868 		if (mp_ipsq != ipsq && phyi == NULL) {
6869 			ASSERT(ipsq->ipsq_next == ipsq);
6870 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6871 			ipsq_delete(ipsq);
6872 		}
6873 
6874 		if (mp == NULL)
6875 			break;
6876 
6877 		q = mp->b_queue;
6878 		func = (ipsq_func_t)mp->b_prev;
6879 		ipsq = mp_ipsq;
6880 		mp->b_next = mp->b_prev = NULL;
6881 		mp->b_queue = NULL;
6882 
6883 		/*
6884 		 * If 'q' is an conn queue, it is valid, since we did a
6885 		 * a refhold on the conn at the start of the ioctl.
6886 		 * If 'q' is an ill queue, it is valid, since close of an
6887 		 * ill will clean up its IPSQ.
6888 		 */
6889 		(*func)(ipsq, q, mp, NULL);
6890 	}
6891 }
6892 
6893 /*
6894  * Used to start any igmp or mld timers that could not be started
6895  * while holding ill_mcast_lock. The timers can't be started while holding
6896  * the lock, since mld/igmp_start_timers may need to call untimeout()
6897  * which can't be done while holding the lock which the timeout handler
6898  * acquires. Otherwise
6899  * there could be a deadlock since the timeout handlers
6900  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6901  * ill_mcast_lock.
6902  */
6903 void
6904 ill_mcast_timer_start(ip_stack_t *ipst)
6905 {
6906 	int		next;
6907 
6908 	mutex_enter(&ipst->ips_igmp_timer_lock);
6909 	next = ipst->ips_igmp_deferred_next;
6910 	ipst->ips_igmp_deferred_next = INFINITY;
6911 	mutex_exit(&ipst->ips_igmp_timer_lock);
6912 
6913 	if (next != INFINITY)
6914 		igmp_start_timers(next, ipst);
6915 
6916 	mutex_enter(&ipst->ips_mld_timer_lock);
6917 	next = ipst->ips_mld_deferred_next;
6918 	ipst->ips_mld_deferred_next = INFINITY;
6919 	mutex_exit(&ipst->ips_mld_timer_lock);
6920 
6921 	if (next != INFINITY)
6922 		mld_start_timers(next, ipst);
6923 }
6924 
6925 /*
6926  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6927  * and `ioccmd'.
6928  */
6929 void
6930 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6931 {
6932 	ill_t *ill = ipif->ipif_ill;
6933 	ipxop_t *ipx = ipsq->ipsq_xop;
6934 
6935 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6936 	ASSERT(ipx->ipx_current_ipif == NULL);
6937 	ASSERT(ipx->ipx_current_ioctl == 0);
6938 
6939 	ipx->ipx_current_done = B_FALSE;
6940 	ipx->ipx_current_ioctl = ioccmd;
6941 	mutex_enter(&ipx->ipx_lock);
6942 	ipx->ipx_current_ipif = ipif;
6943 	mutex_exit(&ipx->ipx_lock);
6944 
6945 	/*
6946 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6947 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6948 	 * references to the ipif (so that the references will eventually
6949 	 * drop to zero) and also prevents any "get" operations (e.g.,
6950 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6951 	 * operation has completed and the ipif is again in a stable state.
6952 	 *
6953 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6954 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6955 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6956 	 * ipifs will be affected.
6957 	 *
6958 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6959 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6960 	 * operate on.
6961 	 */
6962 	switch (ioccmd) {
6963 	case SIOCLIFREMOVEIF:
6964 		break;
6965 	case 0:
6966 		mutex_enter(&ill->ill_lock);
6967 		ipif = ipif->ipif_ill->ill_ipif;
6968 		for (; ipif != NULL; ipif = ipif->ipif_next)
6969 			ipif->ipif_state_flags |= IPIF_CHANGING;
6970 		mutex_exit(&ill->ill_lock);
6971 		break;
6972 	default:
6973 		mutex_enter(&ill->ill_lock);
6974 		ipif->ipif_state_flags |= IPIF_CHANGING;
6975 		mutex_exit(&ill->ill_lock);
6976 	}
6977 }
6978 
6979 /*
6980  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6981  * the next exclusive operation to begin once we ipsq_exit().  However, if
6982  * pending DLPI operations remain, then we will wait for the queue to drain
6983  * before allowing the next exclusive operation to begin.  This ensures that
6984  * DLPI operations from one exclusive operation are never improperly processed
6985  * as part of a subsequent exclusive operation.
6986  */
6987 void
6988 ipsq_current_finish(ipsq_t *ipsq)
6989 {
6990 	ipxop_t	*ipx = ipsq->ipsq_xop;
6991 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6992 	ipif_t	*ipif = ipx->ipx_current_ipif;
6993 
6994 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6995 
6996 	/*
6997 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6998 	 * (but in that case, IPIF_CHANGING will already be clear and no
6999 	 * pending DLPI messages can remain).
7000 	 */
7001 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7002 		ill_t *ill = ipif->ipif_ill;
7003 
7004 		mutex_enter(&ill->ill_lock);
7005 		dlpi_pending = ill->ill_dlpi_pending;
7006 		if (ipx->ipx_current_ioctl == 0) {
7007 			ipif = ill->ill_ipif;
7008 			for (; ipif != NULL; ipif = ipif->ipif_next)
7009 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7010 		} else {
7011 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7012 		}
7013 		mutex_exit(&ill->ill_lock);
7014 	}
7015 
7016 	ASSERT(!ipx->ipx_current_done);
7017 	ipx->ipx_current_done = B_TRUE;
7018 	ipx->ipx_current_ioctl = 0;
7019 	if (dlpi_pending == DL_PRIM_INVAL) {
7020 		mutex_enter(&ipx->ipx_lock);
7021 		ipx->ipx_current_ipif = NULL;
7022 		mutex_exit(&ipx->ipx_lock);
7023 	}
7024 }
7025 
7026 /*
7027  * The ill is closing. Flush all messages on the ipsq that originated
7028  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7029  * for this ill since ipsq_enter could not have entered until then.
7030  * New messages can't be queued since the CONDEMNED flag is set.
7031  */
7032 static void
7033 ipsq_flush(ill_t *ill)
7034 {
7035 	queue_t	*q;
7036 	mblk_t	*prev;
7037 	mblk_t	*mp;
7038 	mblk_t	*mp_next;
7039 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7040 
7041 	ASSERT(IAM_WRITER_ILL(ill));
7042 
7043 	/*
7044 	 * Flush any messages sent up by the driver.
7045 	 */
7046 	mutex_enter(&ipx->ipx_lock);
7047 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7048 		mp_next = mp->b_next;
7049 		q = mp->b_queue;
7050 		if (q == ill->ill_rq || q == ill->ill_wq) {
7051 			/* dequeue mp */
7052 			if (prev == NULL)
7053 				ipx->ipx_mphead = mp->b_next;
7054 			else
7055 				prev->b_next = mp->b_next;
7056 			if (ipx->ipx_mptail == mp) {
7057 				ASSERT(mp_next == NULL);
7058 				ipx->ipx_mptail = prev;
7059 			}
7060 			inet_freemsg(mp);
7061 		} else {
7062 			prev = mp;
7063 		}
7064 	}
7065 	mutex_exit(&ipx->ipx_lock);
7066 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7067 	ipsq_xopq_mp_cleanup(ill, NULL);
7068 }
7069 
7070 /*
7071  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7072  * and return the associated ipif.
7073  * Return value:
7074  *	Non zero: An error has occurred. ci may not be filled out.
7075  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7076  *	a held ipif in ci.ci_ipif.
7077  */
7078 int
7079 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7080     cmd_info_t *ci)
7081 {
7082 	char		*name;
7083 	struct ifreq    *ifr;
7084 	struct lifreq    *lifr;
7085 	ipif_t		*ipif = NULL;
7086 	ill_t		*ill;
7087 	conn_t		*connp;
7088 	boolean_t	isv6;
7089 	boolean_t	exists;
7090 	mblk_t		*mp1;
7091 	zoneid_t	zoneid;
7092 	ip_stack_t	*ipst;
7093 
7094 	if (q->q_next != NULL) {
7095 		ill = (ill_t *)q->q_ptr;
7096 		isv6 = ill->ill_isv6;
7097 		connp = NULL;
7098 		zoneid = ALL_ZONES;
7099 		ipst = ill->ill_ipst;
7100 	} else {
7101 		ill = NULL;
7102 		connp = Q_TO_CONN(q);
7103 		isv6 = (connp->conn_family == AF_INET6);
7104 		zoneid = connp->conn_zoneid;
7105 		if (zoneid == GLOBAL_ZONEID) {
7106 			/* global zone can access ipifs in all zones */
7107 			zoneid = ALL_ZONES;
7108 		}
7109 		ipst = connp->conn_netstack->netstack_ip;
7110 	}
7111 
7112 	/* Has been checked in ip_wput_nondata */
7113 	mp1 = mp->b_cont->b_cont;
7114 
7115 	if (ipip->ipi_cmd_type == IF_CMD) {
7116 		/* This a old style SIOC[GS]IF* command */
7117 		ifr = (struct ifreq *)mp1->b_rptr;
7118 		/*
7119 		 * Null terminate the string to protect against buffer
7120 		 * overrun. String was generated by user code and may not
7121 		 * be trusted.
7122 		 */
7123 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7124 		name = ifr->ifr_name;
7125 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7126 		ci->ci_sin6 = NULL;
7127 		ci->ci_lifr = (struct lifreq *)ifr;
7128 	} else {
7129 		/* This a new style SIOC[GS]LIF* command */
7130 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7131 		lifr = (struct lifreq *)mp1->b_rptr;
7132 		/*
7133 		 * Null terminate the string to protect against buffer
7134 		 * overrun. String was generated by user code and may not
7135 		 * be trusted.
7136 		 */
7137 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7138 		name = lifr->lifr_name;
7139 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7140 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7141 		ci->ci_lifr = lifr;
7142 	}
7143 
7144 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7145 		/*
7146 		 * The ioctl will be failed if the ioctl comes down
7147 		 * an conn stream
7148 		 */
7149 		if (ill == NULL) {
7150 			/*
7151 			 * Not an ill queue, return EINVAL same as the
7152 			 * old error code.
7153 			 */
7154 			return (ENXIO);
7155 		}
7156 		ipif = ill->ill_ipif;
7157 		ipif_refhold(ipif);
7158 	} else {
7159 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7160 		    &exists, isv6, zoneid, ipst);
7161 
7162 		/*
7163 		 * Ensure that get ioctls don't see any internal state changes
7164 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7165 		 * set.
7166 		 */
7167 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7168 		    !IAM_WRITER_IPIF(ipif)) {
7169 			ipsq_t	*ipsq;
7170 
7171 			if (connp != NULL)
7172 				mutex_enter(&connp->conn_lock);
7173 			mutex_enter(&ipif->ipif_ill->ill_lock);
7174 			if (IPIF_IS_CHANGING(ipif) &&
7175 			    !IPIF_IS_CONDEMNED(ipif)) {
7176 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7177 				mutex_enter(&ipsq->ipsq_lock);
7178 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7179 				mutex_exit(&ipif->ipif_ill->ill_lock);
7180 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7181 				    NEW_OP, ipif->ipif_ill);
7182 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7183 				mutex_exit(&ipsq->ipsq_lock);
7184 				if (connp != NULL)
7185 					mutex_exit(&connp->conn_lock);
7186 				ipif_refrele(ipif);
7187 				return (EINPROGRESS);
7188 			}
7189 			mutex_exit(&ipif->ipif_ill->ill_lock);
7190 			if (connp != NULL)
7191 				mutex_exit(&connp->conn_lock);
7192 		}
7193 	}
7194 
7195 	/*
7196 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7197 	 */
7198 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7199 		ipif_refrele(ipif);
7200 		return (ENXIO);
7201 	}
7202 
7203 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7204 	    name[0] == '\0') {
7205 		/*
7206 		 * Handle a or a SIOC?IF* with a null name
7207 		 * during plumb (on the ill queue before the I_PLINK).
7208 		 */
7209 		ipif = ill->ill_ipif;
7210 		ipif_refhold(ipif);
7211 	}
7212 
7213 	if (ipif == NULL)
7214 		return (ENXIO);
7215 
7216 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7217 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7218 
7219 	ci->ci_ipif = ipif;
7220 	return (0);
7221 }
7222 
7223 /*
7224  * Return the total number of ipifs.
7225  */
7226 static uint_t
7227 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7228 {
7229 	uint_t numifs = 0;
7230 	ill_t	*ill;
7231 	ill_walk_context_t	ctx;
7232 	ipif_t	*ipif;
7233 
7234 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7235 	ill = ILL_START_WALK_V4(&ctx, ipst);
7236 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7237 		if (IS_UNDER_IPMP(ill))
7238 			continue;
7239 		for (ipif = ill->ill_ipif; ipif != NULL;
7240 		    ipif = ipif->ipif_next) {
7241 			if (ipif->ipif_zoneid == zoneid ||
7242 			    ipif->ipif_zoneid == ALL_ZONES)
7243 				numifs++;
7244 		}
7245 	}
7246 	rw_exit(&ipst->ips_ill_g_lock);
7247 	return (numifs);
7248 }
7249 
7250 /*
7251  * Return the total number of ipifs.
7252  */
7253 static uint_t
7254 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7255 {
7256 	uint_t numifs = 0;
7257 	ill_t	*ill;
7258 	ipif_t	*ipif;
7259 	ill_walk_context_t	ctx;
7260 
7261 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7262 
7263 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7264 	if (family == AF_INET)
7265 		ill = ILL_START_WALK_V4(&ctx, ipst);
7266 	else if (family == AF_INET6)
7267 		ill = ILL_START_WALK_V6(&ctx, ipst);
7268 	else
7269 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7270 
7271 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7272 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7273 			continue;
7274 
7275 		for (ipif = ill->ill_ipif; ipif != NULL;
7276 		    ipif = ipif->ipif_next) {
7277 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7278 			    !(lifn_flags & LIFC_NOXMIT))
7279 				continue;
7280 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7281 			    !(lifn_flags & LIFC_TEMPORARY))
7282 				continue;
7283 			if (((ipif->ipif_flags &
7284 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7285 			    IPIF_DEPRECATED)) ||
7286 			    IS_LOOPBACK(ill) ||
7287 			    !(ipif->ipif_flags & IPIF_UP)) &&
7288 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7289 				continue;
7290 
7291 			if (zoneid != ipif->ipif_zoneid &&
7292 			    ipif->ipif_zoneid != ALL_ZONES &&
7293 			    (zoneid != GLOBAL_ZONEID ||
7294 			    !(lifn_flags & LIFC_ALLZONES)))
7295 				continue;
7296 
7297 			numifs++;
7298 		}
7299 	}
7300 	rw_exit(&ipst->ips_ill_g_lock);
7301 	return (numifs);
7302 }
7303 
7304 uint_t
7305 ip_get_lifsrcofnum(ill_t *ill)
7306 {
7307 	uint_t numifs = 0;
7308 	ill_t	*ill_head = ill;
7309 	ip_stack_t	*ipst = ill->ill_ipst;
7310 
7311 	/*
7312 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7313 	 * other thread may be trying to relink the ILLs in this usesrc group
7314 	 * and adjusting the ill_usesrc_grp_next pointers
7315 	 */
7316 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7317 	if ((ill->ill_usesrc_ifindex == 0) &&
7318 	    (ill->ill_usesrc_grp_next != NULL)) {
7319 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7320 		    ill = ill->ill_usesrc_grp_next)
7321 			numifs++;
7322 	}
7323 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7324 
7325 	return (numifs);
7326 }
7327 
7328 /* Null values are passed in for ipif, sin, and ifreq */
7329 /* ARGSUSED */
7330 int
7331 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7332     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7333 {
7334 	int *nump;
7335 	conn_t *connp = Q_TO_CONN(q);
7336 
7337 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7338 
7339 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7340 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7341 
7342 	*nump = ip_get_numifs(connp->conn_zoneid,
7343 	    connp->conn_netstack->netstack_ip);
7344 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7345 	return (0);
7346 }
7347 
7348 /* Null values are passed in for ipif, sin, and ifreq */
7349 /* ARGSUSED */
7350 int
7351 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7352     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7353 {
7354 	struct lifnum *lifn;
7355 	mblk_t	*mp1;
7356 	conn_t *connp = Q_TO_CONN(q);
7357 
7358 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7359 
7360 	/* Existence checked in ip_wput_nondata */
7361 	mp1 = mp->b_cont->b_cont;
7362 
7363 	lifn = (struct lifnum *)mp1->b_rptr;
7364 	switch (lifn->lifn_family) {
7365 	case AF_UNSPEC:
7366 	case AF_INET:
7367 	case AF_INET6:
7368 		break;
7369 	default:
7370 		return (EAFNOSUPPORT);
7371 	}
7372 
7373 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7374 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7375 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7376 	return (0);
7377 }
7378 
7379 /* ARGSUSED */
7380 int
7381 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7382     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7383 {
7384 	STRUCT_HANDLE(ifconf, ifc);
7385 	mblk_t *mp1;
7386 	struct iocblk *iocp;
7387 	struct ifreq *ifr;
7388 	ill_walk_context_t	ctx;
7389 	ill_t	*ill;
7390 	ipif_t	*ipif;
7391 	struct sockaddr_in *sin;
7392 	int32_t	ifclen;
7393 	zoneid_t zoneid;
7394 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7395 
7396 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7397 
7398 	ip1dbg(("ip_sioctl_get_ifconf"));
7399 	/* Existence verified in ip_wput_nondata */
7400 	mp1 = mp->b_cont->b_cont;
7401 	iocp = (struct iocblk *)mp->b_rptr;
7402 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7403 
7404 	/*
7405 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7406 	 * the user buffer address and length into which the list of struct
7407 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7408 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7409 	 * the SIOCGIFCONF operation was redefined to simply provide
7410 	 * a large output buffer into which we are supposed to jam the ifreq
7411 	 * array.  The same ioctl command code was used, despite the fact that
7412 	 * both the applications and the kernel code had to change, thus making
7413 	 * it impossible to support both interfaces.
7414 	 *
7415 	 * For reasons not good enough to try to explain, the following
7416 	 * algorithm is used for deciding what to do with one of these:
7417 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7418 	 * form with the output buffer coming down as the continuation message.
7419 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7420 	 * and we have to copy in the ifconf structure to find out how big the
7421 	 * output buffer is and where to copy out to.  Sure no problem...
7422 	 *
7423 	 */
7424 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7425 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7426 		int numifs = 0;
7427 		size_t ifc_bufsize;
7428 
7429 		/*
7430 		 * Must be (better be!) continuation of a TRANSPARENT
7431 		 * IOCTL.  We just copied in the ifconf structure.
7432 		 */
7433 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7434 		    (struct ifconf *)mp1->b_rptr);
7435 
7436 		/*
7437 		 * Allocate a buffer to hold requested information.
7438 		 *
7439 		 * If ifc_len is larger than what is needed, we only
7440 		 * allocate what we will use.
7441 		 *
7442 		 * If ifc_len is smaller than what is needed, return
7443 		 * EINVAL.
7444 		 *
7445 		 * XXX: the ill_t structure can hava 2 counters, for
7446 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7447 		 * number of interfaces for a device, so we don't need
7448 		 * to count them here...
7449 		 */
7450 		numifs = ip_get_numifs(zoneid, ipst);
7451 
7452 		ifclen = STRUCT_FGET(ifc, ifc_len);
7453 		ifc_bufsize = numifs * sizeof (struct ifreq);
7454 		if (ifc_bufsize > ifclen) {
7455 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7456 				/* old behaviour */
7457 				return (EINVAL);
7458 			} else {
7459 				ifc_bufsize = ifclen;
7460 			}
7461 		}
7462 
7463 		mp1 = mi_copyout_alloc(q, mp,
7464 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7465 		if (mp1 == NULL)
7466 			return (ENOMEM);
7467 
7468 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7469 	}
7470 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7471 	/*
7472 	 * the SIOCGIFCONF ioctl only knows about
7473 	 * IPv4 addresses, so don't try to tell
7474 	 * it about interfaces with IPv6-only
7475 	 * addresses. (Last parm 'isv6' is B_FALSE)
7476 	 */
7477 
7478 	ifr = (struct ifreq *)mp1->b_rptr;
7479 
7480 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7481 	ill = ILL_START_WALK_V4(&ctx, ipst);
7482 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7483 		if (IS_UNDER_IPMP(ill))
7484 			continue;
7485 		for (ipif = ill->ill_ipif; ipif != NULL;
7486 		    ipif = ipif->ipif_next) {
7487 			if (zoneid != ipif->ipif_zoneid &&
7488 			    ipif->ipif_zoneid != ALL_ZONES)
7489 				continue;
7490 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7491 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7492 					/* old behaviour */
7493 					rw_exit(&ipst->ips_ill_g_lock);
7494 					return (EINVAL);
7495 				} else {
7496 					goto if_copydone;
7497 				}
7498 			}
7499 			ipif_get_name(ipif, ifr->ifr_name,
7500 			    sizeof (ifr->ifr_name));
7501 			sin = (sin_t *)&ifr->ifr_addr;
7502 			*sin = sin_null;
7503 			sin->sin_family = AF_INET;
7504 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7505 			ifr++;
7506 		}
7507 	}
7508 if_copydone:
7509 	rw_exit(&ipst->ips_ill_g_lock);
7510 	mp1->b_wptr = (uchar_t *)ifr;
7511 
7512 	if (STRUCT_BUF(ifc) != NULL) {
7513 		STRUCT_FSET(ifc, ifc_len,
7514 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7515 	}
7516 	return (0);
7517 }
7518 
7519 /*
7520  * Get the interfaces using the address hosted on the interface passed in,
7521  * as a source adddress
7522  */
7523 /* ARGSUSED */
7524 int
7525 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7526     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7527 {
7528 	mblk_t *mp1;
7529 	ill_t	*ill, *ill_head;
7530 	ipif_t	*ipif, *orig_ipif;
7531 	int	numlifs = 0;
7532 	size_t	lifs_bufsize, lifsmaxlen;
7533 	struct	lifreq *lifr;
7534 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7535 	uint_t	ifindex;
7536 	zoneid_t zoneid;
7537 	boolean_t isv6 = B_FALSE;
7538 	struct	sockaddr_in	*sin;
7539 	struct	sockaddr_in6	*sin6;
7540 	STRUCT_HANDLE(lifsrcof, lifs);
7541 	ip_stack_t		*ipst;
7542 
7543 	ipst = CONNQ_TO_IPST(q);
7544 
7545 	ASSERT(q->q_next == NULL);
7546 
7547 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7548 
7549 	/* Existence verified in ip_wput_nondata */
7550 	mp1 = mp->b_cont->b_cont;
7551 
7552 	/*
7553 	 * Must be (better be!) continuation of a TRANSPARENT
7554 	 * IOCTL.  We just copied in the lifsrcof structure.
7555 	 */
7556 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7557 	    (struct lifsrcof *)mp1->b_rptr);
7558 
7559 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7560 		return (EINVAL);
7561 
7562 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7563 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7564 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7565 	if (ipif == NULL) {
7566 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7567 		    ifindex));
7568 		return (ENXIO);
7569 	}
7570 
7571 	/* Allocate a buffer to hold requested information */
7572 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7573 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7574 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7575 	/* The actual size needed is always returned in lifs_len */
7576 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7577 
7578 	/* If the amount we need is more than what is passed in, abort */
7579 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7580 		ipif_refrele(ipif);
7581 		return (0);
7582 	}
7583 
7584 	mp1 = mi_copyout_alloc(q, mp,
7585 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7586 	if (mp1 == NULL) {
7587 		ipif_refrele(ipif);
7588 		return (ENOMEM);
7589 	}
7590 
7591 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7592 	bzero(mp1->b_rptr, lifs_bufsize);
7593 
7594 	lifr = (struct lifreq *)mp1->b_rptr;
7595 
7596 	ill = ill_head = ipif->ipif_ill;
7597 	orig_ipif = ipif;
7598 
7599 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7600 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7601 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7602 
7603 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7604 	for (; (ill != NULL) && (ill != ill_head);
7605 	    ill = ill->ill_usesrc_grp_next) {
7606 
7607 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7608 			break;
7609 
7610 		ipif = ill->ill_ipif;
7611 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7612 		if (ipif->ipif_isv6) {
7613 			sin6 = (sin6_t *)&lifr->lifr_addr;
7614 			*sin6 = sin6_null;
7615 			sin6->sin6_family = AF_INET6;
7616 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7617 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7618 			    &ipif->ipif_v6net_mask);
7619 		} else {
7620 			sin = (sin_t *)&lifr->lifr_addr;
7621 			*sin = sin_null;
7622 			sin->sin_family = AF_INET;
7623 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7624 			lifr->lifr_addrlen = ip_mask_to_plen(
7625 			    ipif->ipif_net_mask);
7626 		}
7627 		lifr++;
7628 	}
7629 	rw_exit(&ipst->ips_ill_g_lock);
7630 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7631 	ipif_refrele(orig_ipif);
7632 	mp1->b_wptr = (uchar_t *)lifr;
7633 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7634 
7635 	return (0);
7636 }
7637 
7638 /* ARGSUSED */
7639 int
7640 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7641     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7642 {
7643 	mblk_t *mp1;
7644 	int	list;
7645 	ill_t	*ill;
7646 	ipif_t	*ipif;
7647 	int	flags;
7648 	int	numlifs = 0;
7649 	size_t	lifc_bufsize;
7650 	struct	lifreq *lifr;
7651 	sa_family_t	family;
7652 	struct	sockaddr_in	*sin;
7653 	struct	sockaddr_in6	*sin6;
7654 	ill_walk_context_t	ctx;
7655 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7656 	int32_t	lifclen;
7657 	zoneid_t zoneid;
7658 	STRUCT_HANDLE(lifconf, lifc);
7659 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7660 
7661 	ip1dbg(("ip_sioctl_get_lifconf"));
7662 
7663 	ASSERT(q->q_next == NULL);
7664 
7665 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7666 
7667 	/* Existence verified in ip_wput_nondata */
7668 	mp1 = mp->b_cont->b_cont;
7669 
7670 	/*
7671 	 * An extended version of SIOCGIFCONF that takes an
7672 	 * additional address family and flags field.
7673 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7674 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7675 	 * interfaces are omitted.
7676 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7677 	 * unless LIFC_TEMPORARY is specified.
7678 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7679 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7680 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7681 	 * has priority over LIFC_NOXMIT.
7682 	 */
7683 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7684 
7685 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7686 		return (EINVAL);
7687 
7688 	/*
7689 	 * Must be (better be!) continuation of a TRANSPARENT
7690 	 * IOCTL.  We just copied in the lifconf structure.
7691 	 */
7692 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7693 
7694 	family = STRUCT_FGET(lifc, lifc_family);
7695 	flags = STRUCT_FGET(lifc, lifc_flags);
7696 
7697 	switch (family) {
7698 	case AF_UNSPEC:
7699 		/*
7700 		 * walk all ILL's.
7701 		 */
7702 		list = MAX_G_HEADS;
7703 		break;
7704 	case AF_INET:
7705 		/*
7706 		 * walk only IPV4 ILL's.
7707 		 */
7708 		list = IP_V4_G_HEAD;
7709 		break;
7710 	case AF_INET6:
7711 		/*
7712 		 * walk only IPV6 ILL's.
7713 		 */
7714 		list = IP_V6_G_HEAD;
7715 		break;
7716 	default:
7717 		return (EAFNOSUPPORT);
7718 	}
7719 
7720 	/*
7721 	 * Allocate a buffer to hold requested information.
7722 	 *
7723 	 * If lifc_len is larger than what is needed, we only
7724 	 * allocate what we will use.
7725 	 *
7726 	 * If lifc_len is smaller than what is needed, return
7727 	 * EINVAL.
7728 	 */
7729 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7730 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7731 	lifclen = STRUCT_FGET(lifc, lifc_len);
7732 	if (lifc_bufsize > lifclen) {
7733 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7734 			return (EINVAL);
7735 		else
7736 			lifc_bufsize = lifclen;
7737 	}
7738 
7739 	mp1 = mi_copyout_alloc(q, mp,
7740 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7741 	if (mp1 == NULL)
7742 		return (ENOMEM);
7743 
7744 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7745 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7746 
7747 	lifr = (struct lifreq *)mp1->b_rptr;
7748 
7749 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7750 	ill = ill_first(list, list, &ctx, ipst);
7751 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7752 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7753 			continue;
7754 
7755 		for (ipif = ill->ill_ipif; ipif != NULL;
7756 		    ipif = ipif->ipif_next) {
7757 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7758 			    !(flags & LIFC_NOXMIT))
7759 				continue;
7760 
7761 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7762 			    !(flags & LIFC_TEMPORARY))
7763 				continue;
7764 
7765 			if (((ipif->ipif_flags &
7766 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7767 			    IPIF_DEPRECATED)) ||
7768 			    IS_LOOPBACK(ill) ||
7769 			    !(ipif->ipif_flags & IPIF_UP)) &&
7770 			    (flags & LIFC_EXTERNAL_SOURCE))
7771 				continue;
7772 
7773 			if (zoneid != ipif->ipif_zoneid &&
7774 			    ipif->ipif_zoneid != ALL_ZONES &&
7775 			    (zoneid != GLOBAL_ZONEID ||
7776 			    !(flags & LIFC_ALLZONES)))
7777 				continue;
7778 
7779 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7780 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7781 					rw_exit(&ipst->ips_ill_g_lock);
7782 					return (EINVAL);
7783 				} else {
7784 					goto lif_copydone;
7785 				}
7786 			}
7787 
7788 			ipif_get_name(ipif, lifr->lifr_name,
7789 			    sizeof (lifr->lifr_name));
7790 			lifr->lifr_type = ill->ill_type;
7791 			if (ipif->ipif_isv6) {
7792 				sin6 = (sin6_t *)&lifr->lifr_addr;
7793 				*sin6 = sin6_null;
7794 				sin6->sin6_family = AF_INET6;
7795 				sin6->sin6_addr =
7796 				    ipif->ipif_v6lcl_addr;
7797 				lifr->lifr_addrlen =
7798 				    ip_mask_to_plen_v6(
7799 				    &ipif->ipif_v6net_mask);
7800 			} else {
7801 				sin = (sin_t *)&lifr->lifr_addr;
7802 				*sin = sin_null;
7803 				sin->sin_family = AF_INET;
7804 				sin->sin_addr.s_addr =
7805 				    ipif->ipif_lcl_addr;
7806 				lifr->lifr_addrlen =
7807 				    ip_mask_to_plen(
7808 				    ipif->ipif_net_mask);
7809 			}
7810 			lifr++;
7811 		}
7812 	}
7813 lif_copydone:
7814 	rw_exit(&ipst->ips_ill_g_lock);
7815 
7816 	mp1->b_wptr = (uchar_t *)lifr;
7817 	if (STRUCT_BUF(lifc) != NULL) {
7818 		STRUCT_FSET(lifc, lifc_len,
7819 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7820 	}
7821 	return (0);
7822 }
7823 
7824 static void
7825 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7826 {
7827 	ip6_asp_t *table;
7828 	size_t table_size;
7829 	mblk_t *data_mp;
7830 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7831 	ip_stack_t	*ipst;
7832 
7833 	if (q->q_next == NULL)
7834 		ipst = CONNQ_TO_IPST(q);
7835 	else
7836 		ipst = ILLQ_TO_IPST(q);
7837 
7838 	/* These two ioctls are I_STR only */
7839 	if (iocp->ioc_count == TRANSPARENT) {
7840 		miocnak(q, mp, 0, EINVAL);
7841 		return;
7842 	}
7843 
7844 	data_mp = mp->b_cont;
7845 	if (data_mp == NULL) {
7846 		/* The user passed us a NULL argument */
7847 		table = NULL;
7848 		table_size = iocp->ioc_count;
7849 	} else {
7850 		/*
7851 		 * The user provided a table.  The stream head
7852 		 * may have copied in the user data in chunks,
7853 		 * so make sure everything is pulled up
7854 		 * properly.
7855 		 */
7856 		if (MBLKL(data_mp) < iocp->ioc_count) {
7857 			mblk_t *new_data_mp;
7858 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7859 			    NULL) {
7860 				miocnak(q, mp, 0, ENOMEM);
7861 				return;
7862 			}
7863 			freemsg(data_mp);
7864 			data_mp = new_data_mp;
7865 			mp->b_cont = data_mp;
7866 		}
7867 		table = (ip6_asp_t *)data_mp->b_rptr;
7868 		table_size = iocp->ioc_count;
7869 	}
7870 
7871 	switch (iocp->ioc_cmd) {
7872 	case SIOCGIP6ADDRPOLICY:
7873 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7874 		if (iocp->ioc_rval == -1)
7875 			iocp->ioc_error = EINVAL;
7876 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7877 		else if (table != NULL &&
7878 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7879 			ip6_asp_t *src = table;
7880 			ip6_asp32_t *dst = (void *)table;
7881 			int count = table_size / sizeof (ip6_asp_t);
7882 			int i;
7883 
7884 			/*
7885 			 * We need to do an in-place shrink of the array
7886 			 * to match the alignment attributes of the
7887 			 * 32-bit ABI looking at it.
7888 			 */
7889 			/* LINTED: logical expression always true: op "||" */
7890 			ASSERT(sizeof (*src) > sizeof (*dst));
7891 			for (i = 1; i < count; i++)
7892 				bcopy(src + i, dst + i, sizeof (*dst));
7893 		}
7894 #endif
7895 		break;
7896 
7897 	case SIOCSIP6ADDRPOLICY:
7898 		ASSERT(mp->b_prev == NULL);
7899 		mp->b_prev = (void *)q;
7900 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7901 		/*
7902 		 * We pass in the datamodel here so that the ip6_asp_replace()
7903 		 * routine can handle converting from 32-bit to native formats
7904 		 * where necessary.
7905 		 *
7906 		 * A better way to handle this might be to convert the inbound
7907 		 * data structure here, and hang it off a new 'mp'; thus the
7908 		 * ip6_asp_replace() logic would always be dealing with native
7909 		 * format data structures..
7910 		 *
7911 		 * (An even simpler way to handle these ioctls is to just
7912 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7913 		 * and just recompile everything that depends on it.)
7914 		 */
7915 #endif
7916 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7917 		    iocp->ioc_flag & IOC_MODELS);
7918 		return;
7919 	}
7920 
7921 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7922 	qreply(q, mp);
7923 }
7924 
7925 static void
7926 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7927 {
7928 	mblk_t 		*data_mp;
7929 	struct dstinforeq	*dir;
7930 	uint8_t		*end, *cur;
7931 	in6_addr_t	*daddr, *saddr;
7932 	ipaddr_t	v4daddr;
7933 	ire_t		*ire;
7934 	ipaddr_t	v4setsrc;
7935 	in6_addr_t	v6setsrc;
7936 	char		*slabel, *dlabel;
7937 	boolean_t	isipv4;
7938 	int		match_ire;
7939 	ill_t		*dst_ill;
7940 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7941 	conn_t		*connp = Q_TO_CONN(q);
7942 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7943 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7944 	uint64_t	ipif_flags;
7945 
7946 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7947 
7948 	/*
7949 	 * This ioctl is I_STR only, and must have a
7950 	 * data mblk following the M_IOCTL mblk.
7951 	 */
7952 	data_mp = mp->b_cont;
7953 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7954 		miocnak(q, mp, 0, EINVAL);
7955 		return;
7956 	}
7957 
7958 	if (MBLKL(data_mp) < iocp->ioc_count) {
7959 		mblk_t *new_data_mp;
7960 
7961 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7962 			miocnak(q, mp, 0, ENOMEM);
7963 			return;
7964 		}
7965 		freemsg(data_mp);
7966 		data_mp = new_data_mp;
7967 		mp->b_cont = data_mp;
7968 	}
7969 	match_ire = MATCH_IRE_DSTONLY;
7970 
7971 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7972 	    end - cur >= sizeof (struct dstinforeq);
7973 	    cur += sizeof (struct dstinforeq)) {
7974 		dir = (struct dstinforeq *)cur;
7975 		daddr = &dir->dir_daddr;
7976 		saddr = &dir->dir_saddr;
7977 
7978 		/*
7979 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7980 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7981 		 * and ip_select_source_v6() do not.
7982 		 */
7983 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7984 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7985 
7986 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7987 		if (isipv4) {
7988 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7989 			v4setsrc = INADDR_ANY;
7990 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7991 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7992 			    NULL, NULL);
7993 		} else {
7994 			v6setsrc = ipv6_all_zeros;
7995 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7996 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7997 			    NULL, NULL);
7998 		}
7999 		ASSERT(ire != NULL);
8000 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
8001 			ire_refrele(ire);
8002 			dir->dir_dreachable = 0;
8003 
8004 			/* move on to next dst addr */
8005 			continue;
8006 		}
8007 		dir->dir_dreachable = 1;
8008 
8009 		dst_ill = ire_nexthop_ill(ire);
8010 		if (dst_ill == NULL) {
8011 			ire_refrele(ire);
8012 			continue;
8013 		}
8014 
8015 		/* With ipmp we most likely look at the ipmp ill here */
8016 		dir->dir_dmactype = dst_ill->ill_mactype;
8017 
8018 		if (isipv4) {
8019 			ipaddr_t v4saddr;
8020 
8021 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
8022 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
8023 			    &v4saddr, NULL, &ipif_flags) != 0) {
8024 				v4saddr = INADDR_ANY;
8025 				ipif_flags = 0;
8026 			}
8027 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
8028 		} else {
8029 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
8030 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8031 			    saddr, NULL, &ipif_flags) != 0) {
8032 				*saddr = ipv6_all_zeros;
8033 				ipif_flags = 0;
8034 			}
8035 		}
8036 
8037 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8038 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
8039 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8040 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8041 		ire_refrele(ire);
8042 		ill_refrele(dst_ill);
8043 	}
8044 	miocack(q, mp, iocp->ioc_count, 0);
8045 }
8046 
8047 /*
8048  * Check if this is an address assigned to this machine.
8049  * Skips interfaces that are down by using ire checks.
8050  * Translates mapped addresses to v4 addresses and then
8051  * treats them as such, returning true if the v4 address
8052  * associated with this mapped address is configured.
8053  * Note: Applications will have to be careful what they do
8054  * with the response; use of mapped addresses limits
8055  * what can be done with the socket, especially with
8056  * respect to socket options and ioctls - neither IPv4
8057  * options nor IPv6 sticky options/ancillary data options
8058  * may be used.
8059  */
8060 /* ARGSUSED */
8061 int
8062 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8063     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8064 {
8065 	struct sioc_addrreq *sia;
8066 	sin_t *sin;
8067 	ire_t *ire;
8068 	mblk_t *mp1;
8069 	zoneid_t zoneid;
8070 	ip_stack_t	*ipst;
8071 
8072 	ip1dbg(("ip_sioctl_tmyaddr"));
8073 
8074 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8075 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8076 	ipst = CONNQ_TO_IPST(q);
8077 
8078 	/* Existence verified in ip_wput_nondata */
8079 	mp1 = mp->b_cont->b_cont;
8080 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8081 	sin = (sin_t *)&sia->sa_addr;
8082 	switch (sin->sin_family) {
8083 	case AF_INET6: {
8084 		sin6_t *sin6 = (sin6_t *)sin;
8085 
8086 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8087 			ipaddr_t v4_addr;
8088 
8089 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8090 			    v4_addr);
8091 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8092 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8093 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8094 		} else {
8095 			in6_addr_t v6addr;
8096 
8097 			v6addr = sin6->sin6_addr;
8098 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8099 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8100 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8101 		}
8102 		break;
8103 	}
8104 	case AF_INET: {
8105 		ipaddr_t v4addr;
8106 
8107 		v4addr = sin->sin_addr.s_addr;
8108 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8109 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8110 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8111 		break;
8112 	}
8113 	default:
8114 		return (EAFNOSUPPORT);
8115 	}
8116 	if (ire != NULL) {
8117 		sia->sa_res = 1;
8118 		ire_refrele(ire);
8119 	} else {
8120 		sia->sa_res = 0;
8121 	}
8122 	return (0);
8123 }
8124 
8125 /*
8126  * Check if this is an address assigned on-link i.e. neighbor,
8127  * and makes sure it's reachable from the current zone.
8128  * Returns true for my addresses as well.
8129  * Translates mapped addresses to v4 addresses and then
8130  * treats them as such, returning true if the v4 address
8131  * associated with this mapped address is configured.
8132  * Note: Applications will have to be careful what they do
8133  * with the response; use of mapped addresses limits
8134  * what can be done with the socket, especially with
8135  * respect to socket options and ioctls - neither IPv4
8136  * options nor IPv6 sticky options/ancillary data options
8137  * may be used.
8138  */
8139 /* ARGSUSED */
8140 int
8141 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8142     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8143 {
8144 	struct sioc_addrreq *sia;
8145 	sin_t *sin;
8146 	mblk_t	*mp1;
8147 	ire_t *ire = NULL;
8148 	zoneid_t zoneid;
8149 	ip_stack_t	*ipst;
8150 
8151 	ip1dbg(("ip_sioctl_tonlink"));
8152 
8153 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8154 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8155 	ipst = CONNQ_TO_IPST(q);
8156 
8157 	/* Existence verified in ip_wput_nondata */
8158 	mp1 = mp->b_cont->b_cont;
8159 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8160 	sin = (sin_t *)&sia->sa_addr;
8161 
8162 	/*
8163 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8164 	 * to make sure we only look at on-link unicast address.
8165 	 */
8166 	switch (sin->sin_family) {
8167 	case AF_INET6: {
8168 		sin6_t *sin6 = (sin6_t *)sin;
8169 
8170 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8171 			ipaddr_t v4_addr;
8172 
8173 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8174 			    v4_addr);
8175 			if (!CLASSD(v4_addr)) {
8176 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8177 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8178 				    0, ipst, NULL);
8179 			}
8180 		} else {
8181 			in6_addr_t v6addr;
8182 
8183 			v6addr = sin6->sin6_addr;
8184 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8185 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8186 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8187 				    ipst, NULL);
8188 			}
8189 		}
8190 		break;
8191 	}
8192 	case AF_INET: {
8193 		ipaddr_t v4addr;
8194 
8195 		v4addr = sin->sin_addr.s_addr;
8196 		if (!CLASSD(v4addr)) {
8197 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8198 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8199 		}
8200 		break;
8201 	}
8202 	default:
8203 		return (EAFNOSUPPORT);
8204 	}
8205 	sia->sa_res = 0;
8206 	if (ire != NULL) {
8207 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8208 
8209 		if ((ire->ire_type & IRE_ONLINK) &&
8210 		    !(ire->ire_type & IRE_BROADCAST))
8211 			sia->sa_res = 1;
8212 		ire_refrele(ire);
8213 	}
8214 	return (0);
8215 }
8216 
8217 /*
8218  * TBD: implement when kernel maintaines a list of site prefixes.
8219  */
8220 /* ARGSUSED */
8221 int
8222 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8223     ip_ioctl_cmd_t *ipip, void *ifreq)
8224 {
8225 	return (ENXIO);
8226 }
8227 
8228 /* ARP IOCTLs. */
8229 /* ARGSUSED */
8230 int
8231 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8232     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8233 {
8234 	int		err;
8235 	ipaddr_t	ipaddr;
8236 	struct iocblk	*iocp;
8237 	conn_t		*connp;
8238 	struct arpreq	*ar;
8239 	struct xarpreq	*xar;
8240 	int		arp_flags, flags, alength;
8241 	uchar_t		*lladdr;
8242 	ip_stack_t	*ipst;
8243 	ill_t		*ill = ipif->ipif_ill;
8244 	ill_t		*proxy_ill = NULL;
8245 	ipmp_arpent_t	*entp = NULL;
8246 	boolean_t	proxyarp = B_FALSE;
8247 	boolean_t	if_arp_ioctl = B_FALSE;
8248 	ncec_t		*ncec = NULL;
8249 	nce_t		*nce;
8250 
8251 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8252 	connp = Q_TO_CONN(q);
8253 	ipst = connp->conn_netstack->netstack_ip;
8254 	iocp = (struct iocblk *)mp->b_rptr;
8255 
8256 	if (ipip->ipi_cmd_type == XARP_CMD) {
8257 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8258 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8259 		ar = NULL;
8260 
8261 		arp_flags = xar->xarp_flags;
8262 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8263 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8264 		/*
8265 		 * Validate against user's link layer address length
8266 		 * input and name and addr length limits.
8267 		 */
8268 		alength = ill->ill_phys_addr_length;
8269 		if (ipip->ipi_cmd == SIOCSXARP) {
8270 			if (alength != xar->xarp_ha.sdl_alen ||
8271 			    (alength + xar->xarp_ha.sdl_nlen >
8272 			    sizeof (xar->xarp_ha.sdl_data)))
8273 				return (EINVAL);
8274 		}
8275 	} else {
8276 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8277 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8278 		xar = NULL;
8279 
8280 		arp_flags = ar->arp_flags;
8281 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8282 		/*
8283 		 * Theoretically, the sa_family could tell us what link
8284 		 * layer type this operation is trying to deal with. By
8285 		 * common usage AF_UNSPEC means ethernet. We'll assume
8286 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8287 		 * for now. Our new SIOC*XARP ioctls can be used more
8288 		 * generally.
8289 		 *
8290 		 * If the underlying media happens to have a non 6 byte
8291 		 * address, arp module will fail set/get, but the del
8292 		 * operation will succeed.
8293 		 */
8294 		alength = 6;
8295 		if ((ipip->ipi_cmd != SIOCDARP) &&
8296 		    (alength != ill->ill_phys_addr_length)) {
8297 			return (EINVAL);
8298 		}
8299 	}
8300 
8301 	/* Translate ATF* flags to NCE* flags */
8302 	flags = 0;
8303 	if (arp_flags & ATF_AUTHORITY)
8304 		flags |= NCE_F_AUTHORITY;
8305 	if (arp_flags & ATF_PERM)
8306 		flags |= NCE_F_NONUD; /* not subject to aging */
8307 	if (arp_flags & ATF_PUBL)
8308 		flags |= NCE_F_PUBLISH;
8309 
8310 	/*
8311 	 * IPMP ARP special handling:
8312 	 *
8313 	 * 1. Since ARP mappings must appear consistent across the group,
8314 	 *    prohibit changing ARP mappings on the underlying interfaces.
8315 	 *
8316 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8317 	 *    IP itself, prohibit changing them.
8318 	 *
8319 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8320 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8321 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8322 	 */
8323 	if (IS_UNDER_IPMP(ill)) {
8324 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8325 			return (EPERM);
8326 	}
8327 	if (IS_IPMP(ill)) {
8328 		ipmp_illgrp_t *illg = ill->ill_grp;
8329 
8330 		switch (ipip->ipi_cmd) {
8331 		case SIOCSARP:
8332 		case SIOCSXARP:
8333 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8334 			if (proxy_ill != NULL) {
8335 				proxyarp = B_TRUE;
8336 				if (!ipmp_ill_is_active(proxy_ill))
8337 					proxy_ill = ipmp_illgrp_next_ill(illg);
8338 				if (proxy_ill != NULL)
8339 					lladdr = proxy_ill->ill_phys_addr;
8340 			}
8341 			/* FALLTHRU */
8342 		}
8343 	}
8344 
8345 	ipaddr = sin->sin_addr.s_addr;
8346 	/*
8347 	 * don't match across illgrp per case (1) and (2).
8348 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8349 	 */
8350 	nce = nce_lookup_v4(ill, &ipaddr);
8351 	if (nce != NULL)
8352 		ncec = nce->nce_common;
8353 
8354 	switch (iocp->ioc_cmd) {
8355 	case SIOCDARP:
8356 	case SIOCDXARP: {
8357 		/*
8358 		 * Delete the NCE if any.
8359 		 */
8360 		if (ncec == NULL) {
8361 			iocp->ioc_error = ENXIO;
8362 			break;
8363 		}
8364 		/* Don't allow changes to arp mappings of local addresses. */
8365 		if (NCE_MYADDR(ncec)) {
8366 			nce_refrele(nce);
8367 			return (ENOTSUP);
8368 		}
8369 		iocp->ioc_error = 0;
8370 
8371 		/*
8372 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8373 		 * This will delete all the nce entries on the under_ills.
8374 		 */
8375 		ncec_delete(ncec);
8376 		/*
8377 		 * Once the NCE has been deleted, then the ire_dep* consistency
8378 		 * mechanism will find any IRE which depended on the now
8379 		 * condemned NCE (as part of sending packets).
8380 		 * That mechanism handles redirects by deleting redirects
8381 		 * that refer to UNREACHABLE nces.
8382 		 */
8383 		break;
8384 	}
8385 	case SIOCGARP:
8386 	case SIOCGXARP:
8387 		if (ncec != NULL) {
8388 			lladdr = ncec->ncec_lladdr;
8389 			flags = ncec->ncec_flags;
8390 			iocp->ioc_error = 0;
8391 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8392 		} else {
8393 			iocp->ioc_error = ENXIO;
8394 		}
8395 		break;
8396 	case SIOCSARP:
8397 	case SIOCSXARP:
8398 		/* Don't allow changes to arp mappings of local addresses. */
8399 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8400 			nce_refrele(nce);
8401 			return (ENOTSUP);
8402 		}
8403 
8404 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8405 		flags |= NCE_F_STATIC;
8406 		if (!if_arp_ioctl) {
8407 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8408 			    lladdr, alength, flags);
8409 		} else {
8410 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8411 			if (ipif != NULL) {
8412 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8413 				    lladdr, alength, flags);
8414 				ipif_refrele(ipif);
8415 			}
8416 		}
8417 		if (nce != NULL) {
8418 			nce_refrele(nce);
8419 			nce = NULL;
8420 		}
8421 		/*
8422 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8423 		 * by nce_add_common()
8424 		 */
8425 		err = nce_lookup_then_add_v4(ill, lladdr,
8426 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8427 		    &nce);
8428 		if (err == EEXIST) {
8429 			ncec = nce->nce_common;
8430 			mutex_enter(&ncec->ncec_lock);
8431 			ncec->ncec_state = ND_REACHABLE;
8432 			ncec->ncec_flags = flags;
8433 			nce_update(ncec, ND_UNCHANGED, lladdr);
8434 			mutex_exit(&ncec->ncec_lock);
8435 			err = 0;
8436 		}
8437 		if (nce != NULL) {
8438 			nce_refrele(nce);
8439 			nce = NULL;
8440 		}
8441 		if (IS_IPMP(ill) && err == 0) {
8442 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8443 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8444 			    flags);
8445 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8446 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8447 				break;
8448 			}
8449 		}
8450 		iocp->ioc_error = err;
8451 	}
8452 
8453 	if (nce != NULL) {
8454 		nce_refrele(nce);
8455 	}
8456 
8457 	/*
8458 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8459 	 */
8460 	if (entp != NULL)
8461 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8462 
8463 	return (iocp->ioc_error);
8464 }
8465 
8466 /*
8467  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8468  * the associated sin and refhold and return the associated ipif via `ci'.
8469  */
8470 int
8471 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8472     cmd_info_t *ci)
8473 {
8474 	mblk_t	*mp1;
8475 	sin_t	*sin;
8476 	conn_t	*connp;
8477 	ipif_t	*ipif;
8478 	ire_t	*ire = NULL;
8479 	ill_t	*ill = NULL;
8480 	boolean_t exists;
8481 	ip_stack_t *ipst;
8482 	struct arpreq *ar;
8483 	struct xarpreq *xar;
8484 	struct sockaddr_dl *sdl;
8485 
8486 	/* ioctl comes down on a conn */
8487 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8488 	connp = Q_TO_CONN(q);
8489 	if (connp->conn_family == AF_INET6)
8490 		return (ENXIO);
8491 
8492 	ipst = connp->conn_netstack->netstack_ip;
8493 
8494 	/* Verified in ip_wput_nondata */
8495 	mp1 = mp->b_cont->b_cont;
8496 
8497 	if (ipip->ipi_cmd_type == XARP_CMD) {
8498 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8499 		xar = (struct xarpreq *)mp1->b_rptr;
8500 		sin = (sin_t *)&xar->xarp_pa;
8501 		sdl = &xar->xarp_ha;
8502 
8503 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8504 			return (ENXIO);
8505 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8506 			return (EINVAL);
8507 	} else {
8508 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8509 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8510 		ar = (struct arpreq *)mp1->b_rptr;
8511 		sin = (sin_t *)&ar->arp_pa;
8512 	}
8513 
8514 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8515 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8516 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8517 		if (ipif == NULL)
8518 			return (ENXIO);
8519 		if (ipif->ipif_id != 0) {
8520 			ipif_refrele(ipif);
8521 			return (ENXIO);
8522 		}
8523 	} else {
8524 		/*
8525 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8526 		 * of 0: use the IP address to find the ipif.  If the IP
8527 		 * address is an IPMP test address, ire_ftable_lookup() will
8528 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8529 		 */
8530 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8531 		    ipst);
8532 		if (ipif == NULL) {
8533 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8534 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8535 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8536 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8537 				if (ire != NULL)
8538 					ire_refrele(ire);
8539 				return (ENXIO);
8540 			}
8541 			ASSERT(ire != NULL && ill != NULL);
8542 			ipif = ill->ill_ipif;
8543 			ipif_refhold(ipif);
8544 			ire_refrele(ire);
8545 		}
8546 	}
8547 
8548 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8549 		ipif_refrele(ipif);
8550 		return (ENXIO);
8551 	}
8552 
8553 	ci->ci_sin = sin;
8554 	ci->ci_ipif = ipif;
8555 	return (0);
8556 }
8557 
8558 /*
8559  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8560  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8561  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8562  * up and thus an ill can join that illgrp.
8563  *
8564  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8565  * open()/close() primarily because close() is not allowed to fail or block
8566  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8567  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8568  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8569  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8570  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8571  * state if I_UNLINK didn't occur.
8572  *
8573  * Note that for each plumb/unplumb operation, we may end up here more than
8574  * once because of the way ifconfig works.  However, it's OK to link the same
8575  * illgrp more than once, or unlink an illgrp that's already unlinked.
8576  */
8577 static int
8578 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8579 {
8580 	int err;
8581 	ip_stack_t *ipst = ill->ill_ipst;
8582 
8583 	ASSERT(IS_IPMP(ill));
8584 	ASSERT(IAM_WRITER_ILL(ill));
8585 
8586 	switch (ioccmd) {
8587 	case I_LINK:
8588 		return (ENOTSUP);
8589 
8590 	case I_PLINK:
8591 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8592 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8593 		rw_exit(&ipst->ips_ipmp_lock);
8594 		break;
8595 
8596 	case I_PUNLINK:
8597 		/*
8598 		 * Require all UP ipifs be brought down prior to unlinking the
8599 		 * illgrp so any associated IREs (and other state) is torched.
8600 		 */
8601 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8602 			return (EBUSY);
8603 
8604 		/*
8605 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8606 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8607 		 * join this group.  Specifically: ills trying to join grab
8608 		 * ipmp_lock and bump a "pending join" counter checked by
8609 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8610 		 * joins can occur (since we have ipmp_lock).  Once we drop
8611 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8612 		 * find the illgrp (since we unlinked it) and will return
8613 		 * EAFNOSUPPORT.  This will then take them back through the
8614 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8615 		 * back through I_PLINK above.
8616 		 */
8617 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8618 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8619 		rw_exit(&ipst->ips_ipmp_lock);
8620 		return (err);
8621 	default:
8622 		break;
8623 	}
8624 	return (0);
8625 }
8626 
8627 /*
8628  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8629  * atomically set/clear the muxids. Also complete the ioctl by acking or
8630  * naking it.  Note that the code is structured such that the link type,
8631  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8632  * its clones use the persistent link, while pppd(1M) and perhaps many
8633  * other daemons may use non-persistent link.  When combined with some
8634  * ill_t states, linking and unlinking lower streams may be used as
8635  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8636  */
8637 /* ARGSUSED */
8638 void
8639 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8640 {
8641 	mblk_t		*mp1;
8642 	struct linkblk	*li;
8643 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8644 	int		err = 0;
8645 
8646 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8647 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8648 
8649 	mp1 = mp->b_cont;	/* This is the linkblk info */
8650 	li = (struct linkblk *)mp1->b_rptr;
8651 
8652 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8653 	if (err == EINPROGRESS)
8654 		return;
8655 done:
8656 	if (err == 0)
8657 		miocack(q, mp, 0, 0);
8658 	else
8659 		miocnak(q, mp, 0, err);
8660 
8661 	/* Conn was refheld in ip_sioctl_copyin_setup */
8662 	if (CONN_Q(q))
8663 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8664 }
8665 
8666 /*
8667  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8668  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8669  * module stream).  If `doconsist' is set, then do the extended consistency
8670  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8671  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8672  * an error code on failure.
8673  */
8674 static int
8675 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8676     struct linkblk *li)
8677 {
8678 	int		err = 0;
8679 	ill_t  		*ill;
8680 	queue_t		*ipwq, *dwq;
8681 	const char	*name;
8682 	struct qinit	*qinfo;
8683 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8684 	boolean_t	entered_ipsq = B_FALSE;
8685 	boolean_t	is_ip = B_FALSE;
8686 	arl_t		*arl;
8687 
8688 	/*
8689 	 * Walk the lower stream to verify it's the IP module stream.
8690 	 * The IP module is identified by its name, wput function,
8691 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8692 	 * (li->l_qbot) will not vanish until this ioctl completes.
8693 	 */
8694 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8695 		qinfo = ipwq->q_qinfo;
8696 		name = qinfo->qi_minfo->mi_idname;
8697 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8698 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8699 			is_ip = B_TRUE;
8700 			break;
8701 		}
8702 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8703 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8704 			break;
8705 		}
8706 	}
8707 
8708 	/*
8709 	 * If this isn't an IP module stream, bail.
8710 	 */
8711 	if (ipwq == NULL)
8712 		return (0);
8713 
8714 	if (!is_ip) {
8715 		arl = (arl_t *)ipwq->q_ptr;
8716 		ill = arl_to_ill(arl);
8717 		if (ill == NULL)
8718 			return (0);
8719 	} else {
8720 		ill = ipwq->q_ptr;
8721 	}
8722 	ASSERT(ill != NULL);
8723 
8724 	if (ipsq == NULL) {
8725 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8726 		    NEW_OP, B_FALSE);
8727 		if (ipsq == NULL) {
8728 			if (!is_ip)
8729 				ill_refrele(ill);
8730 			return (EINPROGRESS);
8731 		}
8732 		entered_ipsq = B_TRUE;
8733 	}
8734 	ASSERT(IAM_WRITER_ILL(ill));
8735 	mutex_enter(&ill->ill_lock);
8736 	if (!is_ip) {
8737 		if (islink && ill->ill_muxid == 0) {
8738 			/*
8739 			 * Plumbing has to be done with IP plumbed first, arp
8740 			 * second, but here we have arp being plumbed first.
8741 			 */
8742 			mutex_exit(&ill->ill_lock);
8743 			ipsq_exit(ipsq);
8744 			ill_refrele(ill);
8745 			return (EINVAL);
8746 		}
8747 	}
8748 	mutex_exit(&ill->ill_lock);
8749 	if (!is_ip) {
8750 		arl->arl_muxid = islink ? li->l_index : 0;
8751 		ill_refrele(ill);
8752 		goto done;
8753 	}
8754 
8755 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8756 		goto done;
8757 
8758 	/*
8759 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8760 	 * the read queue of the module immediately below IP in the ill.
8761 	 * These are used during the capability negotiation below.
8762 	 */
8763 	ill->ill_lmod_rq = NULL;
8764 	ill->ill_lmod_cnt = 0;
8765 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8766 		ill->ill_lmod_rq = RD(dwq);
8767 		for (; dwq != NULL; dwq = dwq->q_next)
8768 			ill->ill_lmod_cnt++;
8769 	}
8770 
8771 	ill->ill_muxid = islink ? li->l_index : 0;
8772 
8773 	/*
8774 	 * Mark the ipsq busy until the capability operations initiated below
8775 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8776 	 * returns, but the capability operation may complete asynchronously
8777 	 * much later.
8778 	 */
8779 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8780 	/*
8781 	 * If there's at least one up ipif on this ill, then we're bound to
8782 	 * the underlying driver via DLPI.  In that case, renegotiate
8783 	 * capabilities to account for any possible change in modules
8784 	 * interposed between IP and the driver.
8785 	 */
8786 	if (ill->ill_ipif_up_count > 0) {
8787 		if (islink)
8788 			ill_capability_probe(ill);
8789 		else
8790 			ill_capability_reset(ill, B_FALSE);
8791 	}
8792 	ipsq_current_finish(ipsq);
8793 done:
8794 	if (entered_ipsq)
8795 		ipsq_exit(ipsq);
8796 
8797 	return (err);
8798 }
8799 
8800 /*
8801  * Search the ioctl command in the ioctl tables and return a pointer
8802  * to the ioctl command information. The ioctl command tables are
8803  * static and fully populated at compile time.
8804  */
8805 ip_ioctl_cmd_t *
8806 ip_sioctl_lookup(int ioc_cmd)
8807 {
8808 	int index;
8809 	ip_ioctl_cmd_t *ipip;
8810 	ip_ioctl_cmd_t *ipip_end;
8811 
8812 	if (ioc_cmd == IPI_DONTCARE)
8813 		return (NULL);
8814 
8815 	/*
8816 	 * Do a 2 step search. First search the indexed table
8817 	 * based on the least significant byte of the ioctl cmd.
8818 	 * If we don't find a match, then search the misc table
8819 	 * serially.
8820 	 */
8821 	index = ioc_cmd & 0xFF;
8822 	if (index < ip_ndx_ioctl_count) {
8823 		ipip = &ip_ndx_ioctl_table[index];
8824 		if (ipip->ipi_cmd == ioc_cmd) {
8825 			/* Found a match in the ndx table */
8826 			return (ipip);
8827 		}
8828 	}
8829 
8830 	/* Search the misc table */
8831 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8832 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8833 		if (ipip->ipi_cmd == ioc_cmd)
8834 			/* Found a match in the misc table */
8835 			return (ipip);
8836 	}
8837 
8838 	return (NULL);
8839 }
8840 
8841 /*
8842  * Wrapper function for resuming deferred ioctl processing
8843  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8844  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8845  */
8846 /* ARGSUSED */
8847 void
8848 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8849     void *dummy_arg)
8850 {
8851 	ip_sioctl_copyin_setup(q, mp);
8852 }
8853 
8854 /*
8855  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8856  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8857  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8858  * We establish here the size of the block to be copied in.  mi_copyin
8859  * arranges for this to happen, an processing continues in ip_wput_nondata with
8860  * an M_IOCDATA message.
8861  */
8862 void
8863 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8864 {
8865 	int	copyin_size;
8866 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8867 	ip_ioctl_cmd_t *ipip;
8868 	cred_t *cr;
8869 	ip_stack_t	*ipst;
8870 
8871 	if (CONN_Q(q))
8872 		ipst = CONNQ_TO_IPST(q);
8873 	else
8874 		ipst = ILLQ_TO_IPST(q);
8875 
8876 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8877 	if (ipip == NULL) {
8878 		/*
8879 		 * The ioctl is not one we understand or own.
8880 		 * Pass it along to be processed down stream,
8881 		 * if this is a module instance of IP, else nak
8882 		 * the ioctl.
8883 		 */
8884 		if (q->q_next == NULL) {
8885 			goto nak;
8886 		} else {
8887 			putnext(q, mp);
8888 			return;
8889 		}
8890 	}
8891 
8892 	/*
8893 	 * If this is deferred, then we will do all the checks when we
8894 	 * come back.
8895 	 */
8896 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8897 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8898 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8899 		return;
8900 	}
8901 
8902 	/*
8903 	 * Only allow a very small subset of IP ioctls on this stream if
8904 	 * IP is a module and not a driver. Allowing ioctls to be processed
8905 	 * in this case may cause assert failures or data corruption.
8906 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8907 	 * ioctls allowed on an IP module stream, after which this stream
8908 	 * normally becomes a multiplexor (at which time the stream head
8909 	 * will fail all ioctls).
8910 	 */
8911 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8912 		goto nak;
8913 	}
8914 
8915 	/* Make sure we have ioctl data to process. */
8916 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8917 		goto nak;
8918 
8919 	/*
8920 	 * Prefer dblk credential over ioctl credential; some synthesized
8921 	 * ioctls have kcred set because there's no way to crhold()
8922 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8923 	 * the framework; the caller of ioctl needs to hold the reference
8924 	 * for the duration of the call).
8925 	 */
8926 	cr = msg_getcred(mp, NULL);
8927 	if (cr == NULL)
8928 		cr = iocp->ioc_cr;
8929 
8930 	/* Make sure normal users don't send down privileged ioctls */
8931 	if ((ipip->ipi_flags & IPI_PRIV) &&
8932 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8933 		/* We checked the privilege earlier but log it here */
8934 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8935 		return;
8936 	}
8937 
8938 	/*
8939 	 * The ioctl command tables can only encode fixed length
8940 	 * ioctl data. If the length is variable, the table will
8941 	 * encode the length as zero. Such special cases are handled
8942 	 * below in the switch.
8943 	 */
8944 	if (ipip->ipi_copyin_size != 0) {
8945 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8946 		return;
8947 	}
8948 
8949 	switch (iocp->ioc_cmd) {
8950 	case O_SIOCGIFCONF:
8951 	case SIOCGIFCONF:
8952 		/*
8953 		 * This IOCTL is hilarious.  See comments in
8954 		 * ip_sioctl_get_ifconf for the story.
8955 		 */
8956 		if (iocp->ioc_count == TRANSPARENT)
8957 			copyin_size = SIZEOF_STRUCT(ifconf,
8958 			    iocp->ioc_flag);
8959 		else
8960 			copyin_size = iocp->ioc_count;
8961 		mi_copyin(q, mp, NULL, copyin_size);
8962 		return;
8963 
8964 	case O_SIOCGLIFCONF:
8965 	case SIOCGLIFCONF:
8966 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8967 		mi_copyin(q, mp, NULL, copyin_size);
8968 		return;
8969 
8970 	case SIOCGLIFSRCOF:
8971 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8972 		mi_copyin(q, mp, NULL, copyin_size);
8973 		return;
8974 	case SIOCGIP6ADDRPOLICY:
8975 		ip_sioctl_ip6addrpolicy(q, mp);
8976 		ip6_asp_table_refrele(ipst);
8977 		return;
8978 
8979 	case SIOCSIP6ADDRPOLICY:
8980 		ip_sioctl_ip6addrpolicy(q, mp);
8981 		return;
8982 
8983 	case SIOCGDSTINFO:
8984 		ip_sioctl_dstinfo(q, mp);
8985 		ip6_asp_table_refrele(ipst);
8986 		return;
8987 
8988 	case I_PLINK:
8989 	case I_PUNLINK:
8990 	case I_LINK:
8991 	case I_UNLINK:
8992 		/*
8993 		 * We treat non-persistent link similarly as the persistent
8994 		 * link case, in terms of plumbing/unplumbing, as well as
8995 		 * dynamic re-plumbing events indicator.  See comments
8996 		 * in ip_sioctl_plink() for more.
8997 		 *
8998 		 * Request can be enqueued in the 'ipsq' while waiting
8999 		 * to become exclusive. So bump up the conn ref.
9000 		 */
9001 		if (CONN_Q(q))
9002 			CONN_INC_REF(Q_TO_CONN(q));
9003 		ip_sioctl_plink(NULL, q, mp, NULL);
9004 		return;
9005 
9006 	case ND_GET:
9007 	case ND_SET:
9008 		/*
9009 		 * Use of the nd table requires holding the reader lock.
9010 		 * Modifying the nd table thru nd_load/nd_unload requires
9011 		 * the writer lock.
9012 		 */
9013 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
9014 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
9015 			rw_exit(&ipst->ips_ip_g_nd_lock);
9016 
9017 			if (iocp->ioc_error)
9018 				iocp->ioc_count = 0;
9019 			mp->b_datap->db_type = M_IOCACK;
9020 			qreply(q, mp);
9021 			return;
9022 		}
9023 		rw_exit(&ipst->ips_ip_g_nd_lock);
9024 		/*
9025 		 * We don't understand this subioctl of ND_GET / ND_SET.
9026 		 * Maybe intended for some driver / module below us
9027 		 */
9028 		if (q->q_next) {
9029 			putnext(q, mp);
9030 		} else {
9031 			iocp->ioc_error = ENOENT;
9032 			mp->b_datap->db_type = M_IOCNAK;
9033 			iocp->ioc_count = 0;
9034 			qreply(q, mp);
9035 		}
9036 		return;
9037 
9038 	case IP_IOCTL:
9039 		ip_wput_ioctl(q, mp);
9040 		return;
9041 
9042 	case SIOCILB:
9043 		/* The ioctl length varies depending on the ILB command. */
9044 		copyin_size = iocp->ioc_count;
9045 		if (copyin_size < sizeof (ilb_cmd_t))
9046 			goto nak;
9047 		mi_copyin(q, mp, NULL, copyin_size);
9048 		return;
9049 
9050 	default:
9051 		cmn_err(CE_PANIC, "should not happen ");
9052 	}
9053 nak:
9054 	if (mp->b_cont != NULL) {
9055 		freemsg(mp->b_cont);
9056 		mp->b_cont = NULL;
9057 	}
9058 	iocp->ioc_error = EINVAL;
9059 	mp->b_datap->db_type = M_IOCNAK;
9060 	iocp->ioc_count = 0;
9061 	qreply(q, mp);
9062 }
9063 
9064 static void
9065 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9066 {
9067 	struct arpreq *ar;
9068 	struct xarpreq *xar;
9069 	mblk_t	*tmp;
9070 	struct iocblk *iocp;
9071 	int x_arp_ioctl = B_FALSE;
9072 	int *flagsp;
9073 	char *storage = NULL;
9074 
9075 	ASSERT(ill != NULL);
9076 
9077 	iocp = (struct iocblk *)mp->b_rptr;
9078 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9079 
9080 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9081 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9082 	    (iocp->ioc_cmd == SIOCSXARP)) {
9083 		x_arp_ioctl = B_TRUE;
9084 		xar = (struct xarpreq *)tmp->b_rptr;
9085 		flagsp = &xar->xarp_flags;
9086 		storage = xar->xarp_ha.sdl_data;
9087 	} else {
9088 		ar = (struct arpreq *)tmp->b_rptr;
9089 		flagsp = &ar->arp_flags;
9090 		storage = ar->arp_ha.sa_data;
9091 	}
9092 
9093 	/*
9094 	 * We're done if this is not an SIOCG{X}ARP
9095 	 */
9096 	if (x_arp_ioctl) {
9097 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9098 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9099 		    sizeof (xar->xarp_ha.sdl_data)) {
9100 			iocp->ioc_error = EINVAL;
9101 			return;
9102 		}
9103 	}
9104 	*flagsp = ATF_INUSE;
9105 	/*
9106 	 * If /sbin/arp told us we are the authority using the "permanent"
9107 	 * flag, or if this is one of my addresses print "permanent"
9108 	 * in the /sbin/arp output.
9109 	 */
9110 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9111 		*flagsp |= ATF_AUTHORITY;
9112 	if (flags & NCE_F_NONUD)
9113 		*flagsp |= ATF_PERM; /* not subject to aging */
9114 	if (flags & NCE_F_PUBLISH)
9115 		*flagsp |= ATF_PUBL;
9116 	if (hwaddr != NULL) {
9117 		*flagsp |= ATF_COM;
9118 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9119 	}
9120 }
9121 
9122 /*
9123  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9124  * interface) create the next available logical interface for this
9125  * physical interface.
9126  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9127  * ipif with the specified name.
9128  *
9129  * If the address family is not AF_UNSPEC then set the address as well.
9130  *
9131  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9132  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9133  *
9134  * Executed as a writer on the ill.
9135  * So no lock is needed to traverse the ipif chain, or examine the
9136  * phyint flags.
9137  */
9138 /* ARGSUSED */
9139 int
9140 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9141     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9142 {
9143 	mblk_t	*mp1;
9144 	struct lifreq *lifr;
9145 	boolean_t	isv6;
9146 	boolean_t	exists;
9147 	char 	*name;
9148 	char	*endp;
9149 	char	*cp;
9150 	int	namelen;
9151 	ipif_t	*ipif;
9152 	long	id;
9153 	ipsq_t	*ipsq;
9154 	ill_t	*ill;
9155 	sin_t	*sin;
9156 	int	err = 0;
9157 	boolean_t found_sep = B_FALSE;
9158 	conn_t	*connp;
9159 	zoneid_t zoneid;
9160 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9161 
9162 	ASSERT(q->q_next == NULL);
9163 	ip1dbg(("ip_sioctl_addif\n"));
9164 	/* Existence of mp1 has been checked in ip_wput_nondata */
9165 	mp1 = mp->b_cont->b_cont;
9166 	/*
9167 	 * Null terminate the string to protect against buffer
9168 	 * overrun. String was generated by user code and may not
9169 	 * be trusted.
9170 	 */
9171 	lifr = (struct lifreq *)mp1->b_rptr;
9172 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9173 	name = lifr->lifr_name;
9174 	ASSERT(CONN_Q(q));
9175 	connp = Q_TO_CONN(q);
9176 	isv6 = (connp->conn_family == AF_INET6);
9177 	zoneid = connp->conn_zoneid;
9178 	namelen = mi_strlen(name);
9179 	if (namelen == 0)
9180 		return (EINVAL);
9181 
9182 	exists = B_FALSE;
9183 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9184 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9185 		/*
9186 		 * Allow creating lo0 using SIOCLIFADDIF.
9187 		 * can't be any other writer thread. So can pass null below
9188 		 * for the last 4 args to ipif_lookup_name.
9189 		 */
9190 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9191 		    &exists, isv6, zoneid, ipst);
9192 		/* Prevent any further action */
9193 		if (ipif == NULL) {
9194 			return (ENOBUFS);
9195 		} else if (!exists) {
9196 			/* We created the ipif now and as writer */
9197 			ipif_refrele(ipif);
9198 			return (0);
9199 		} else {
9200 			ill = ipif->ipif_ill;
9201 			ill_refhold(ill);
9202 			ipif_refrele(ipif);
9203 		}
9204 	} else {
9205 		/* Look for a colon in the name. */
9206 		endp = &name[namelen];
9207 		for (cp = endp; --cp > name; ) {
9208 			if (*cp == IPIF_SEPARATOR_CHAR) {
9209 				found_sep = B_TRUE;
9210 				/*
9211 				 * Reject any non-decimal aliases for plumbing
9212 				 * of logical interfaces. Aliases with leading
9213 				 * zeroes are also rejected as they introduce
9214 				 * ambiguity in the naming of the interfaces.
9215 				 * Comparing with "0" takes care of all such
9216 				 * cases.
9217 				 */
9218 				if ((strncmp("0", cp+1, 1)) == 0)
9219 					return (EINVAL);
9220 
9221 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9222 				    id <= 0 || *endp != '\0') {
9223 					return (EINVAL);
9224 				}
9225 				*cp = '\0';
9226 				break;
9227 			}
9228 		}
9229 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9230 		if (found_sep)
9231 			*cp = IPIF_SEPARATOR_CHAR;
9232 		if (ill == NULL)
9233 			return (ENXIO);
9234 	}
9235 
9236 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9237 	    B_TRUE);
9238 
9239 	/*
9240 	 * Release the refhold due to the lookup, now that we are excl
9241 	 * or we are just returning
9242 	 */
9243 	ill_refrele(ill);
9244 
9245 	if (ipsq == NULL)
9246 		return (EINPROGRESS);
9247 
9248 	/* We are now exclusive on the IPSQ */
9249 	ASSERT(IAM_WRITER_ILL(ill));
9250 
9251 	if (found_sep) {
9252 		/* Now see if there is an IPIF with this unit number. */
9253 		for (ipif = ill->ill_ipif; ipif != NULL;
9254 		    ipif = ipif->ipif_next) {
9255 			if (ipif->ipif_id == id) {
9256 				err = EEXIST;
9257 				goto done;
9258 			}
9259 		}
9260 	}
9261 
9262 	/*
9263 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9264 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9265 	 * instead.
9266 	 */
9267 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9268 	    B_TRUE, B_TRUE, &err)) == NULL) {
9269 		goto done;
9270 	}
9271 
9272 	/* Return created name with ioctl */
9273 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9274 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9275 	ip1dbg(("created %s\n", lifr->lifr_name));
9276 
9277 	/* Set address */
9278 	sin = (sin_t *)&lifr->lifr_addr;
9279 	if (sin->sin_family != AF_UNSPEC) {
9280 		err = ip_sioctl_addr(ipif, sin, q, mp,
9281 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9282 	}
9283 
9284 done:
9285 	ipsq_exit(ipsq);
9286 	return (err);
9287 }
9288 
9289 /*
9290  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9291  * interface) delete it based on the IP address (on this physical interface).
9292  * Otherwise delete it based on the ipif_id.
9293  * Also, special handling to allow a removeif of lo0.
9294  */
9295 /* ARGSUSED */
9296 int
9297 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9298     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9299 {
9300 	conn_t		*connp;
9301 	ill_t		*ill = ipif->ipif_ill;
9302 	boolean_t	 success;
9303 	ip_stack_t	*ipst;
9304 
9305 	ipst = CONNQ_TO_IPST(q);
9306 
9307 	ASSERT(q->q_next == NULL);
9308 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9309 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9310 	ASSERT(IAM_WRITER_IPIF(ipif));
9311 
9312 	connp = Q_TO_CONN(q);
9313 	/*
9314 	 * Special case for unplumbing lo0 (the loopback physical interface).
9315 	 * If unplumbing lo0, the incoming address structure has been
9316 	 * initialized to all zeros. When unplumbing lo0, all its logical
9317 	 * interfaces must be removed too.
9318 	 *
9319 	 * Note that this interface may be called to remove a specific
9320 	 * loopback logical interface (eg, lo0:1). But in that case
9321 	 * ipif->ipif_id != 0 so that the code path for that case is the
9322 	 * same as any other interface (meaning it skips the code directly
9323 	 * below).
9324 	 */
9325 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9326 		if (sin->sin_family == AF_UNSPEC &&
9327 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9328 			/*
9329 			 * Mark it condemned. No new ref. will be made to ill.
9330 			 */
9331 			mutex_enter(&ill->ill_lock);
9332 			ill->ill_state_flags |= ILL_CONDEMNED;
9333 			for (ipif = ill->ill_ipif; ipif != NULL;
9334 			    ipif = ipif->ipif_next) {
9335 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9336 			}
9337 			mutex_exit(&ill->ill_lock);
9338 
9339 			ipif = ill->ill_ipif;
9340 			/* unplumb the loopback interface */
9341 			ill_delete(ill);
9342 			mutex_enter(&connp->conn_lock);
9343 			mutex_enter(&ill->ill_lock);
9344 
9345 			/* Are any references to this ill active */
9346 			if (ill_is_freeable(ill)) {
9347 				mutex_exit(&ill->ill_lock);
9348 				mutex_exit(&connp->conn_lock);
9349 				ill_delete_tail(ill);
9350 				mi_free(ill);
9351 				return (0);
9352 			}
9353 			success = ipsq_pending_mp_add(connp, ipif,
9354 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9355 			mutex_exit(&connp->conn_lock);
9356 			mutex_exit(&ill->ill_lock);
9357 			if (success)
9358 				return (EINPROGRESS);
9359 			else
9360 				return (EINTR);
9361 		}
9362 	}
9363 
9364 	if (ipif->ipif_id == 0) {
9365 		ipsq_t *ipsq;
9366 
9367 		/* Find based on address */
9368 		if (ipif->ipif_isv6) {
9369 			sin6_t *sin6;
9370 
9371 			if (sin->sin_family != AF_INET6)
9372 				return (EAFNOSUPPORT);
9373 
9374 			sin6 = (sin6_t *)sin;
9375 			/* We are a writer, so we should be able to lookup */
9376 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9377 			    ipst);
9378 		} else {
9379 			if (sin->sin_family != AF_INET)
9380 				return (EAFNOSUPPORT);
9381 
9382 			/* We are a writer, so we should be able to lookup */
9383 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9384 			    ipst);
9385 		}
9386 		if (ipif == NULL) {
9387 			return (EADDRNOTAVAIL);
9388 		}
9389 
9390 		/*
9391 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9392 		 * lifr_name of the physical interface but with an ip address
9393 		 * lifr_addr of a logical interface plumbed over it.
9394 		 * So update ipx_current_ipif now that ipif points to the
9395 		 * correct one.
9396 		 */
9397 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9398 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9399 
9400 		/* This is a writer */
9401 		ipif_refrele(ipif);
9402 	}
9403 
9404 	/*
9405 	 * Can not delete instance zero since it is tied to the ill.
9406 	 */
9407 	if (ipif->ipif_id == 0)
9408 		return (EBUSY);
9409 
9410 	mutex_enter(&ill->ill_lock);
9411 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9412 	mutex_exit(&ill->ill_lock);
9413 
9414 	ipif_free(ipif);
9415 
9416 	mutex_enter(&connp->conn_lock);
9417 	mutex_enter(&ill->ill_lock);
9418 
9419 	/* Are any references to this ipif active */
9420 	if (ipif_is_freeable(ipif)) {
9421 		mutex_exit(&ill->ill_lock);
9422 		mutex_exit(&connp->conn_lock);
9423 		ipif_non_duplicate(ipif);
9424 		(void) ipif_down_tail(ipif);
9425 		ipif_free_tail(ipif); /* frees ipif */
9426 		return (0);
9427 	}
9428 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9429 	    IPIF_FREE);
9430 	mutex_exit(&ill->ill_lock);
9431 	mutex_exit(&connp->conn_lock);
9432 	if (success)
9433 		return (EINPROGRESS);
9434 	else
9435 		return (EINTR);
9436 }
9437 
9438 /*
9439  * Restart the removeif ioctl. The refcnt has gone down to 0.
9440  * The ipif is already condemned. So can't find it thru lookups.
9441  */
9442 /* ARGSUSED */
9443 int
9444 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9445     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9446 {
9447 	ill_t *ill = ipif->ipif_ill;
9448 
9449 	ASSERT(IAM_WRITER_IPIF(ipif));
9450 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9451 
9452 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9453 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9454 
9455 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9456 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9457 		ill_delete_tail(ill);
9458 		mi_free(ill);
9459 		return (0);
9460 	}
9461 
9462 	ipif_non_duplicate(ipif);
9463 	(void) ipif_down_tail(ipif);
9464 	ipif_free_tail(ipif);
9465 
9466 	return (0);
9467 }
9468 
9469 /*
9470  * Set the local interface address.
9471  * Allow an address of all zero when the interface is down.
9472  */
9473 /* ARGSUSED */
9474 int
9475 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9476     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9477 {
9478 	int err = 0;
9479 	in6_addr_t v6addr;
9480 	boolean_t need_up = B_FALSE;
9481 
9482 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9483 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9484 
9485 	ASSERT(IAM_WRITER_IPIF(ipif));
9486 
9487 	if (ipif->ipif_isv6) {
9488 		sin6_t *sin6;
9489 		ill_t *ill;
9490 		phyint_t *phyi;
9491 
9492 		if (sin->sin_family != AF_INET6)
9493 			return (EAFNOSUPPORT);
9494 
9495 		sin6 = (sin6_t *)sin;
9496 		v6addr = sin6->sin6_addr;
9497 		ill = ipif->ipif_ill;
9498 		phyi = ill->ill_phyint;
9499 
9500 		/*
9501 		 * Enforce that true multicast interfaces have a link-local
9502 		 * address for logical unit 0.
9503 		 */
9504 		if (ipif->ipif_id == 0 &&
9505 		    (ill->ill_flags & ILLF_MULTICAST) &&
9506 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9507 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9508 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9509 			return (EADDRNOTAVAIL);
9510 		}
9511 
9512 		/*
9513 		 * up interfaces shouldn't have the unspecified address
9514 		 * unless they also have the IPIF_NOLOCAL flags set and
9515 		 * have a subnet assigned.
9516 		 */
9517 		if ((ipif->ipif_flags & IPIF_UP) &&
9518 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9519 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9520 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9521 			return (EADDRNOTAVAIL);
9522 		}
9523 
9524 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9525 			return (EADDRNOTAVAIL);
9526 	} else {
9527 		ipaddr_t addr;
9528 
9529 		if (sin->sin_family != AF_INET)
9530 			return (EAFNOSUPPORT);
9531 
9532 		addr = sin->sin_addr.s_addr;
9533 
9534 		/* Allow 0 as the local address. */
9535 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9536 			return (EADDRNOTAVAIL);
9537 
9538 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9539 	}
9540 
9541 	/*
9542 	 * Even if there is no change we redo things just to rerun
9543 	 * ipif_set_default.
9544 	 */
9545 	if (ipif->ipif_flags & IPIF_UP) {
9546 		/*
9547 		 * Setting a new local address, make sure
9548 		 * we have net and subnet bcast ire's for
9549 		 * the old address if we need them.
9550 		 */
9551 		/*
9552 		 * If the interface is already marked up,
9553 		 * we call ipif_down which will take care
9554 		 * of ditching any IREs that have been set
9555 		 * up based on the old interface address.
9556 		 */
9557 		err = ipif_logical_down(ipif, q, mp);
9558 		if (err == EINPROGRESS)
9559 			return (err);
9560 		(void) ipif_down_tail(ipif);
9561 		need_up = 1;
9562 	}
9563 
9564 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9565 	return (err);
9566 }
9567 
9568 int
9569 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9570     boolean_t need_up)
9571 {
9572 	in6_addr_t v6addr;
9573 	in6_addr_t ov6addr;
9574 	ipaddr_t addr;
9575 	sin6_t	*sin6;
9576 	int	sinlen;
9577 	int	err = 0;
9578 	ill_t	*ill = ipif->ipif_ill;
9579 	boolean_t need_dl_down;
9580 	boolean_t need_arp_down;
9581 	struct iocblk *iocp;
9582 
9583 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9584 
9585 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9586 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9587 	ASSERT(IAM_WRITER_IPIF(ipif));
9588 
9589 	/* Must cancel any pending timer before taking the ill_lock */
9590 	if (ipif->ipif_recovery_id != 0)
9591 		(void) untimeout(ipif->ipif_recovery_id);
9592 	ipif->ipif_recovery_id = 0;
9593 
9594 	if (ipif->ipif_isv6) {
9595 		sin6 = (sin6_t *)sin;
9596 		v6addr = sin6->sin6_addr;
9597 		sinlen = sizeof (struct sockaddr_in6);
9598 	} else {
9599 		addr = sin->sin_addr.s_addr;
9600 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9601 		sinlen = sizeof (struct sockaddr_in);
9602 	}
9603 	mutex_enter(&ill->ill_lock);
9604 	ov6addr = ipif->ipif_v6lcl_addr;
9605 	ipif->ipif_v6lcl_addr = v6addr;
9606 	sctp_update_ipif_addr(ipif, ov6addr);
9607 	ipif->ipif_addr_ready = 0;
9608 
9609 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9610 
9611 	/*
9612 	 * If the interface was previously marked as a duplicate, then since
9613 	 * we've now got a "new" address, it should no longer be considered a
9614 	 * duplicate -- even if the "new" address is the same as the old one.
9615 	 * Note that if all ipifs are down, we may have a pending ARP down
9616 	 * event to handle.  This is because we want to recover from duplicates
9617 	 * and thus delay tearing down ARP until the duplicates have been
9618 	 * removed or disabled.
9619 	 */
9620 	need_dl_down = need_arp_down = B_FALSE;
9621 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9622 		need_arp_down = !need_up;
9623 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9624 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9625 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9626 			need_dl_down = B_TRUE;
9627 		}
9628 	}
9629 
9630 	ipif_set_default(ipif);
9631 
9632 	/*
9633 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9634 	 * tag the ill so that future updates to the interface ID don't result
9635 	 * in this address getting automatically reconfigured from under the
9636 	 * administrator.
9637 	 */
9638 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9639 		ill->ill_manual_linklocal = 1;
9640 
9641 	/*
9642 	 * When publishing an interface address change event, we only notify
9643 	 * the event listeners of the new address.  It is assumed that if they
9644 	 * actively care about the addresses assigned that they will have
9645 	 * already discovered the previous address assigned (if there was one.)
9646 	 *
9647 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9648 	 */
9649 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9650 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9651 		    NE_ADDRESS_CHANGE, sin, sinlen);
9652 	}
9653 
9654 	mutex_exit(&ill->ill_lock);
9655 
9656 	if (need_up) {
9657 		/*
9658 		 * Now bring the interface back up.  If this
9659 		 * is the only IPIF for the ILL, ipif_up
9660 		 * will have to re-bind to the device, so
9661 		 * we may get back EINPROGRESS, in which
9662 		 * case, this IOCTL will get completed in
9663 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9664 		 */
9665 		err = ipif_up(ipif, q, mp);
9666 	} else {
9667 		/* Perhaps ilgs should use this ill */
9668 		update_conn_ill(NULL, ill->ill_ipst);
9669 	}
9670 
9671 	if (need_dl_down)
9672 		ill_dl_down(ill);
9673 
9674 	if (need_arp_down && !ill->ill_isv6)
9675 		(void) ipif_arp_down(ipif);
9676 
9677 	/*
9678 	 * The default multicast interface might have changed (for
9679 	 * instance if the IPv6 scope of the address changed)
9680 	 */
9681 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9682 
9683 	return (err);
9684 }
9685 
9686 /*
9687  * Restart entry point to restart the address set operation after the
9688  * refcounts have dropped to zero.
9689  */
9690 /* ARGSUSED */
9691 int
9692 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9693     ip_ioctl_cmd_t *ipip, void *ifreq)
9694 {
9695 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9696 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9697 	ASSERT(IAM_WRITER_IPIF(ipif));
9698 	(void) ipif_down_tail(ipif);
9699 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9700 }
9701 
9702 /* ARGSUSED */
9703 int
9704 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9705     ip_ioctl_cmd_t *ipip, void *if_req)
9706 {
9707 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9708 	struct lifreq *lifr = (struct lifreq *)if_req;
9709 
9710 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9711 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9712 	/*
9713 	 * The net mask and address can't change since we have a
9714 	 * reference to the ipif. So no lock is necessary.
9715 	 */
9716 	if (ipif->ipif_isv6) {
9717 		*sin6 = sin6_null;
9718 		sin6->sin6_family = AF_INET6;
9719 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9720 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9721 		lifr->lifr_addrlen =
9722 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9723 	} else {
9724 		*sin = sin_null;
9725 		sin->sin_family = AF_INET;
9726 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9727 		if (ipip->ipi_cmd_type == LIF_CMD) {
9728 			lifr->lifr_addrlen =
9729 			    ip_mask_to_plen(ipif->ipif_net_mask);
9730 		}
9731 	}
9732 	return (0);
9733 }
9734 
9735 /*
9736  * Set the destination address for a pt-pt interface.
9737  */
9738 /* ARGSUSED */
9739 int
9740 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9741     ip_ioctl_cmd_t *ipip, void *if_req)
9742 {
9743 	int err = 0;
9744 	in6_addr_t v6addr;
9745 	boolean_t need_up = B_FALSE;
9746 
9747 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9748 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9749 	ASSERT(IAM_WRITER_IPIF(ipif));
9750 
9751 	if (ipif->ipif_isv6) {
9752 		sin6_t *sin6;
9753 
9754 		if (sin->sin_family != AF_INET6)
9755 			return (EAFNOSUPPORT);
9756 
9757 		sin6 = (sin6_t *)sin;
9758 		v6addr = sin6->sin6_addr;
9759 
9760 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9761 			return (EADDRNOTAVAIL);
9762 	} else {
9763 		ipaddr_t addr;
9764 
9765 		if (sin->sin_family != AF_INET)
9766 			return (EAFNOSUPPORT);
9767 
9768 		addr = sin->sin_addr.s_addr;
9769 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9770 			return (EADDRNOTAVAIL);
9771 
9772 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9773 	}
9774 
9775 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9776 		return (0);	/* No change */
9777 
9778 	if (ipif->ipif_flags & IPIF_UP) {
9779 		/*
9780 		 * If the interface is already marked up,
9781 		 * we call ipif_down which will take care
9782 		 * of ditching any IREs that have been set
9783 		 * up based on the old pp dst address.
9784 		 */
9785 		err = ipif_logical_down(ipif, q, mp);
9786 		if (err == EINPROGRESS)
9787 			return (err);
9788 		(void) ipif_down_tail(ipif);
9789 		need_up = B_TRUE;
9790 	}
9791 	/*
9792 	 * could return EINPROGRESS. If so ioctl will complete in
9793 	 * ip_rput_dlpi_writer
9794 	 */
9795 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9796 	return (err);
9797 }
9798 
9799 static int
9800 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9801     boolean_t need_up)
9802 {
9803 	in6_addr_t v6addr;
9804 	ill_t	*ill = ipif->ipif_ill;
9805 	int	err = 0;
9806 	boolean_t need_dl_down;
9807 	boolean_t need_arp_down;
9808 
9809 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9810 	    ipif->ipif_id, (void *)ipif));
9811 
9812 	/* Must cancel any pending timer before taking the ill_lock */
9813 	if (ipif->ipif_recovery_id != 0)
9814 		(void) untimeout(ipif->ipif_recovery_id);
9815 	ipif->ipif_recovery_id = 0;
9816 
9817 	if (ipif->ipif_isv6) {
9818 		sin6_t *sin6;
9819 
9820 		sin6 = (sin6_t *)sin;
9821 		v6addr = sin6->sin6_addr;
9822 	} else {
9823 		ipaddr_t addr;
9824 
9825 		addr = sin->sin_addr.s_addr;
9826 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9827 	}
9828 	mutex_enter(&ill->ill_lock);
9829 	/* Set point to point destination address. */
9830 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9831 		/*
9832 		 * Allow this as a means of creating logical
9833 		 * pt-pt interfaces on top of e.g. an Ethernet.
9834 		 * XXX Undocumented HACK for testing.
9835 		 * pt-pt interfaces are created with NUD disabled.
9836 		 */
9837 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9838 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9839 		if (ipif->ipif_isv6)
9840 			ill->ill_flags |= ILLF_NONUD;
9841 	}
9842 
9843 	/*
9844 	 * If the interface was previously marked as a duplicate, then since
9845 	 * we've now got a "new" address, it should no longer be considered a
9846 	 * duplicate -- even if the "new" address is the same as the old one.
9847 	 * Note that if all ipifs are down, we may have a pending ARP down
9848 	 * event to handle.
9849 	 */
9850 	need_dl_down = need_arp_down = B_FALSE;
9851 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9852 		need_arp_down = !need_up;
9853 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9854 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9855 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9856 			need_dl_down = B_TRUE;
9857 		}
9858 	}
9859 
9860 	/*
9861 	 * If we've just manually set the IPv6 destination link-local address
9862 	 * (0th ipif), tag the ill so that future updates to the destination
9863 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9864 	 * result in this address getting automatically reconfigured from
9865 	 * under the administrator.
9866 	 */
9867 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9868 		ill->ill_manual_dst_linklocal = 1;
9869 
9870 	/* Set the new address. */
9871 	ipif->ipif_v6pp_dst_addr = v6addr;
9872 	/* Make sure subnet tracks pp_dst */
9873 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9874 	mutex_exit(&ill->ill_lock);
9875 
9876 	if (need_up) {
9877 		/*
9878 		 * Now bring the interface back up.  If this
9879 		 * is the only IPIF for the ILL, ipif_up
9880 		 * will have to re-bind to the device, so
9881 		 * we may get back EINPROGRESS, in which
9882 		 * case, this IOCTL will get completed in
9883 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9884 		 */
9885 		err = ipif_up(ipif, q, mp);
9886 	}
9887 
9888 	if (need_dl_down)
9889 		ill_dl_down(ill);
9890 	if (need_arp_down && !ipif->ipif_isv6)
9891 		(void) ipif_arp_down(ipif);
9892 
9893 	return (err);
9894 }
9895 
9896 /*
9897  * Restart entry point to restart the dstaddress set operation after the
9898  * refcounts have dropped to zero.
9899  */
9900 /* ARGSUSED */
9901 int
9902 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9903     ip_ioctl_cmd_t *ipip, void *ifreq)
9904 {
9905 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9906 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9907 	(void) ipif_down_tail(ipif);
9908 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9909 }
9910 
9911 /* ARGSUSED */
9912 int
9913 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9914     ip_ioctl_cmd_t *ipip, void *if_req)
9915 {
9916 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9917 
9918 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9919 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9920 	/*
9921 	 * Get point to point destination address. The addresses can't
9922 	 * change since we hold a reference to the ipif.
9923 	 */
9924 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9925 		return (EADDRNOTAVAIL);
9926 
9927 	if (ipif->ipif_isv6) {
9928 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9929 		*sin6 = sin6_null;
9930 		sin6->sin6_family = AF_INET6;
9931 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9932 	} else {
9933 		*sin = sin_null;
9934 		sin->sin_family = AF_INET;
9935 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9936 	}
9937 	return (0);
9938 }
9939 
9940 /*
9941  * Check which flags will change by the given flags being set
9942  * silently ignore flags which userland is not allowed to control.
9943  * (Because these flags may change between SIOCGLIFFLAGS and
9944  * SIOCSLIFFLAGS, and that's outside of userland's control,
9945  * we need to silently ignore them rather than fail.)
9946  */
9947 static void
9948 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9949     uint64_t *offp)
9950 {
9951 	ill_t		*ill = ipif->ipif_ill;
9952 	phyint_t 	*phyi = ill->ill_phyint;
9953 	uint64_t	cantchange_flags, intf_flags;
9954 	uint64_t	turn_on, turn_off;
9955 
9956 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9957 	cantchange_flags = IFF_CANTCHANGE;
9958 	if (IS_IPMP(ill))
9959 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9960 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9961 	turn_off = intf_flags & turn_on;
9962 	turn_on ^= turn_off;
9963 	*onp = turn_on;
9964 	*offp = turn_off;
9965 }
9966 
9967 /*
9968  * Set interface flags.  Many flags require special handling (e.g.,
9969  * bringing the interface down); see below for details.
9970  *
9971  * NOTE : We really don't enforce that ipif_id zero should be used
9972  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9973  *	  is because applications generally does SICGLIFFLAGS and
9974  *	  ORs in the new flags (that affects the logical) and does a
9975  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9976  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9977  *	  flags that will be turned on is correct with respect to
9978  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9979  */
9980 /* ARGSUSED */
9981 int
9982 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9983     ip_ioctl_cmd_t *ipip, void *if_req)
9984 {
9985 	uint64_t turn_on;
9986 	uint64_t turn_off;
9987 	int	err = 0;
9988 	phyint_t *phyi;
9989 	ill_t *ill;
9990 	conn_t *connp;
9991 	uint64_t intf_flags;
9992 	boolean_t phyint_flags_modified = B_FALSE;
9993 	uint64_t flags;
9994 	struct ifreq *ifr;
9995 	struct lifreq *lifr;
9996 	boolean_t set_linklocal = B_FALSE;
9997 
9998 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9999 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10000 
10001 	ASSERT(IAM_WRITER_IPIF(ipif));
10002 
10003 	ill = ipif->ipif_ill;
10004 	phyi = ill->ill_phyint;
10005 
10006 	if (ipip->ipi_cmd_type == IF_CMD) {
10007 		ifr = (struct ifreq *)if_req;
10008 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10009 	} else {
10010 		lifr = (struct lifreq *)if_req;
10011 		flags = lifr->lifr_flags;
10012 	}
10013 
10014 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10015 
10016 	/*
10017 	 * Have the flags been set correctly until now?
10018 	 */
10019 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10020 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10021 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10022 	/*
10023 	 * Compare the new flags to the old, and partition
10024 	 * into those coming on and those going off.
10025 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10026 	 */
10027 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10028 		flags |= intf_flags & ~0xFFFF;
10029 
10030 	/*
10031 	 * Explicitly fail attempts to change flags that are always invalid on
10032 	 * an IPMP meta-interface.
10033 	 */
10034 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10035 		return (EINVAL);
10036 
10037 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10038 	if ((turn_on|turn_off) == 0)
10039 		return (0);	/* No change */
10040 
10041 	/*
10042 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10043 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10044 	 * allow it to be turned off.
10045 	 */
10046 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10047 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10048 		return (EINVAL);
10049 
10050 	if ((connp = Q_TO_CONN(q)) == NULL)
10051 		return (EINVAL);
10052 
10053 	/*
10054 	 * Only vrrp control socket is allowed to change IFF_UP and
10055 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10056 	 */
10057 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10058 		if (!connp->conn_isvrrp)
10059 			return (EINVAL);
10060 	}
10061 
10062 	/*
10063 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10064 	 * VRRP control socket.
10065 	 */
10066 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10067 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10068 			return (EINVAL);
10069 	}
10070 
10071 	if (turn_on & IFF_NOFAILOVER) {
10072 		turn_on |= IFF_DEPRECATED;
10073 		flags |= IFF_DEPRECATED;
10074 	}
10075 
10076 	/*
10077 	 * On underlying interfaces, only allow applications to manage test
10078 	 * addresses -- otherwise, they may get confused when the address
10079 	 * moves as part of being brought up.  Likewise, prevent an
10080 	 * application-managed test address from being converted to a data
10081 	 * address.  To prevent migration of administratively up addresses in
10082 	 * the kernel, we don't allow them to be converted either.
10083 	 */
10084 	if (IS_UNDER_IPMP(ill)) {
10085 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10086 
10087 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10088 			return (EINVAL);
10089 
10090 		if ((turn_off & IFF_NOFAILOVER) &&
10091 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10092 			return (EINVAL);
10093 	}
10094 
10095 	/*
10096 	 * Only allow IFF_TEMPORARY flag to be set on
10097 	 * IPv6 interfaces.
10098 	 */
10099 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10100 		return (EINVAL);
10101 
10102 	/*
10103 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10104 	 */
10105 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10106 		return (EINVAL);
10107 
10108 	/*
10109 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10110 	 * interfaces.  It makes no sense in that context.
10111 	 */
10112 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10113 		return (EINVAL);
10114 
10115 	/*
10116 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10117 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10118 	 * If the link local address isn't set, and can be set, it will get
10119 	 * set later on in this function.
10120 	 */
10121 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10122 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10123 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10124 		if (ipif_cant_setlinklocal(ipif))
10125 			return (EINVAL);
10126 		set_linklocal = B_TRUE;
10127 	}
10128 
10129 	/*
10130 	 * If we modify physical interface flags, we'll potentially need to
10131 	 * send up two routing socket messages for the changes (one for the
10132 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10133 	 */
10134 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10135 		phyint_flags_modified = B_TRUE;
10136 
10137 	/*
10138 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10139 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10140 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10141 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10142 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10143 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10144 	 * will not be honored.
10145 	 */
10146 	if (turn_on & PHYI_STANDBY) {
10147 		/*
10148 		 * No need to grab ill_g_usesrc_lock here; see the
10149 		 * synchronization notes in ip.c.
10150 		 */
10151 		if (ill->ill_usesrc_grp_next != NULL ||
10152 		    intf_flags & PHYI_INACTIVE)
10153 			return (EINVAL);
10154 		if (!(flags & PHYI_FAILED)) {
10155 			flags |= PHYI_INACTIVE;
10156 			turn_on |= PHYI_INACTIVE;
10157 		}
10158 	}
10159 
10160 	if (turn_off & PHYI_STANDBY) {
10161 		flags &= ~PHYI_INACTIVE;
10162 		turn_off |= PHYI_INACTIVE;
10163 	}
10164 
10165 	/*
10166 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10167 	 * would end up on.
10168 	 */
10169 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10170 	    (PHYI_FAILED | PHYI_INACTIVE))
10171 		return (EINVAL);
10172 
10173 	/*
10174 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10175 	 * status of the interface.
10176 	 */
10177 	if ((turn_on | turn_off) & ILLF_ROUTER)
10178 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10179 
10180 	/*
10181 	 * If the interface is not UP and we are not going to
10182 	 * bring it UP, record the flags and return. When the
10183 	 * interface comes UP later, the right actions will be
10184 	 * taken.
10185 	 */
10186 	if (!(ipif->ipif_flags & IPIF_UP) &&
10187 	    !(turn_on & IPIF_UP)) {
10188 		/* Record new flags in their respective places. */
10189 		mutex_enter(&ill->ill_lock);
10190 		mutex_enter(&ill->ill_phyint->phyint_lock);
10191 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10192 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10193 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10194 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10195 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10196 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10197 		mutex_exit(&ill->ill_lock);
10198 		mutex_exit(&ill->ill_phyint->phyint_lock);
10199 
10200 		/*
10201 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10202 		 * same to the kernel: if any of them has been set by
10203 		 * userland, the interface cannot be used for data traffic.
10204 		 */
10205 		if ((turn_on|turn_off) &
10206 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10207 			ASSERT(!IS_IPMP(ill));
10208 			/*
10209 			 * It's possible the ill is part of an "anonymous"
10210 			 * IPMP group rather than a real group.  In that case,
10211 			 * there are no other interfaces in the group and thus
10212 			 * no need to call ipmp_phyint_refresh_active().
10213 			 */
10214 			if (IS_UNDER_IPMP(ill))
10215 				ipmp_phyint_refresh_active(phyi);
10216 		}
10217 
10218 		if (phyint_flags_modified) {
10219 			if (phyi->phyint_illv4 != NULL) {
10220 				ip_rts_ifmsg(phyi->phyint_illv4->
10221 				    ill_ipif, RTSQ_DEFAULT);
10222 			}
10223 			if (phyi->phyint_illv6 != NULL) {
10224 				ip_rts_ifmsg(phyi->phyint_illv6->
10225 				    ill_ipif, RTSQ_DEFAULT);
10226 			}
10227 		}
10228 		/* The default multicast interface might have changed */
10229 		ire_increment_multicast_generation(ill->ill_ipst,
10230 		    ill->ill_isv6);
10231 
10232 		return (0);
10233 	} else if (set_linklocal) {
10234 		mutex_enter(&ill->ill_lock);
10235 		if (set_linklocal)
10236 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10237 		mutex_exit(&ill->ill_lock);
10238 	}
10239 
10240 	/*
10241 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10242 	 * or point-to-point interfaces with an unspecified destination. We do
10243 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10244 	 * have a subnet assigned, which is how in.ndpd currently manages its
10245 	 * onlink prefix list when no addresses are configured with those
10246 	 * prefixes.
10247 	 */
10248 	if (ipif->ipif_isv6 &&
10249 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10250 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10251 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10252 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10253 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10254 		return (EINVAL);
10255 	}
10256 
10257 	/*
10258 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10259 	 * from being brought up.
10260 	 */
10261 	if (!ipif->ipif_isv6 &&
10262 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10263 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10264 		return (EINVAL);
10265 	}
10266 
10267 	/*
10268 	 * If we are going to change one or more of the flags that are
10269 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10270 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10271 	 * IPIF_NOFAILOVER, we will take special action.  This is
10272 	 * done by bring the ipif down, changing the flags and bringing
10273 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10274 	 * back up will trigger the address to be moved.
10275 	 *
10276 	 * If we are going to change IFF_NOACCEPT, we need to bring
10277 	 * all the ipifs down then bring them up again.	 The act of
10278 	 * bringing all the ipifs back up will trigger the local
10279 	 * ires being recreated with "no_accept" set/cleared.
10280 	 *
10281 	 * Note that ILLF_NOACCEPT is always set separately from the
10282 	 * other flags.
10283 	 */
10284 	if ((turn_on|turn_off) &
10285 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10286 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10287 	    IPIF_NOFAILOVER)) {
10288 		/*
10289 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10290 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10291 		 * entries shared between multiple ipifs on the same subnet.
10292 		 */
10293 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10294 		    !(turn_off & IPIF_UP)) {
10295 			if (ipif->ipif_flags & IPIF_UP)
10296 				ill->ill_logical_down = 1;
10297 			turn_on &= ~IPIF_UP;
10298 		}
10299 		err = ipif_down(ipif, q, mp);
10300 		ip1dbg(("ipif_down returns %d err ", err));
10301 		if (err == EINPROGRESS)
10302 			return (err);
10303 		(void) ipif_down_tail(ipif);
10304 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10305 		/*
10306 		 * If we can quiesce the ill, then continue.  If not, then
10307 		 * ip_sioctl_flags_tail() will be called from
10308 		 * ipif_ill_refrele_tail().
10309 		 */
10310 		ill_down_ipifs(ill, B_TRUE);
10311 
10312 		mutex_enter(&connp->conn_lock);
10313 		mutex_enter(&ill->ill_lock);
10314 		if (!ill_is_quiescent(ill)) {
10315 			boolean_t success;
10316 
10317 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10318 			    q, mp, ILL_DOWN);
10319 			mutex_exit(&ill->ill_lock);
10320 			mutex_exit(&connp->conn_lock);
10321 			return (success ? EINPROGRESS : EINTR);
10322 		}
10323 		mutex_exit(&ill->ill_lock);
10324 		mutex_exit(&connp->conn_lock);
10325 	}
10326 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10327 }
10328 
10329 static int
10330 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10331 {
10332 	ill_t	*ill;
10333 	phyint_t *phyi;
10334 	uint64_t turn_on, turn_off;
10335 	boolean_t phyint_flags_modified = B_FALSE;
10336 	int	err = 0;
10337 	boolean_t set_linklocal = B_FALSE;
10338 
10339 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10340 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10341 
10342 	ASSERT(IAM_WRITER_IPIF(ipif));
10343 
10344 	ill = ipif->ipif_ill;
10345 	phyi = ill->ill_phyint;
10346 
10347 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10348 
10349 	/*
10350 	 * IFF_UP is handled separately.
10351 	 */
10352 	turn_on &= ~IFF_UP;
10353 	turn_off &= ~IFF_UP;
10354 
10355 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10356 		phyint_flags_modified = B_TRUE;
10357 
10358 	/*
10359 	 * Now we change the flags. Track current value of
10360 	 * other flags in their respective places.
10361 	 */
10362 	mutex_enter(&ill->ill_lock);
10363 	mutex_enter(&phyi->phyint_lock);
10364 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10365 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10366 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10367 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10368 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10369 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10370 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10371 		set_linklocal = B_TRUE;
10372 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10373 	}
10374 
10375 	mutex_exit(&ill->ill_lock);
10376 	mutex_exit(&phyi->phyint_lock);
10377 
10378 	if (set_linklocal)
10379 		(void) ipif_setlinklocal(ipif);
10380 
10381 	/*
10382 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10383 	 * the kernel: if any of them has been set by userland, the interface
10384 	 * cannot be used for data traffic.
10385 	 */
10386 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10387 		ASSERT(!IS_IPMP(ill));
10388 		/*
10389 		 * It's possible the ill is part of an "anonymous" IPMP group
10390 		 * rather than a real group.  In that case, there are no other
10391 		 * interfaces in the group and thus no need for us to call
10392 		 * ipmp_phyint_refresh_active().
10393 		 */
10394 		if (IS_UNDER_IPMP(ill))
10395 			ipmp_phyint_refresh_active(phyi);
10396 	}
10397 
10398 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10399 		/*
10400 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10401 		 * ipifs that were brought down.
10402 		 *
10403 		 * The routing sockets messages are sent as the result
10404 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10405 		 * as well.
10406 		 */
10407 		err = ill_up_ipifs(ill, q, mp);
10408 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10409 		/*
10410 		 * XXX ipif_up really does not know whether a phyint flags
10411 		 * was modified or not. So, it sends up information on
10412 		 * only one routing sockets message. As we don't bring up
10413 		 * the interface and also set PHYI_ flags simultaneously
10414 		 * it should be okay.
10415 		 */
10416 		err = ipif_up(ipif, q, mp);
10417 	} else {
10418 		/*
10419 		 * Make sure routing socket sees all changes to the flags.
10420 		 * ipif_up_done* handles this when we use ipif_up.
10421 		 */
10422 		if (phyint_flags_modified) {
10423 			if (phyi->phyint_illv4 != NULL) {
10424 				ip_rts_ifmsg(phyi->phyint_illv4->
10425 				    ill_ipif, RTSQ_DEFAULT);
10426 			}
10427 			if (phyi->phyint_illv6 != NULL) {
10428 				ip_rts_ifmsg(phyi->phyint_illv6->
10429 				    ill_ipif, RTSQ_DEFAULT);
10430 			}
10431 		} else {
10432 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10433 		}
10434 		/*
10435 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10436 		 * this in need_up case.
10437 		 */
10438 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10439 	}
10440 
10441 	/* The default multicast interface might have changed */
10442 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10443 	return (err);
10444 }
10445 
10446 /*
10447  * Restart the flags operation now that the refcounts have dropped to zero.
10448  */
10449 /* ARGSUSED */
10450 int
10451 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10452     ip_ioctl_cmd_t *ipip, void *if_req)
10453 {
10454 	uint64_t flags;
10455 	struct ifreq *ifr = if_req;
10456 	struct lifreq *lifr = if_req;
10457 	uint64_t turn_on, turn_off;
10458 
10459 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10460 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10461 
10462 	if (ipip->ipi_cmd_type == IF_CMD) {
10463 		/* cast to uint16_t prevents unwanted sign extension */
10464 		flags = (uint16_t)ifr->ifr_flags;
10465 	} else {
10466 		flags = lifr->lifr_flags;
10467 	}
10468 
10469 	/*
10470 	 * If this function call is a result of the ILLF_NOACCEPT flag
10471 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10472 	 */
10473 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10474 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10475 		(void) ipif_down_tail(ipif);
10476 
10477 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10478 }
10479 
10480 /*
10481  * Can operate on either a module or a driver queue.
10482  */
10483 /* ARGSUSED */
10484 int
10485 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10486     ip_ioctl_cmd_t *ipip, void *if_req)
10487 {
10488 	/*
10489 	 * Has the flags been set correctly till now ?
10490 	 */
10491 	ill_t *ill = ipif->ipif_ill;
10492 	phyint_t *phyi = ill->ill_phyint;
10493 
10494 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10495 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10496 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10497 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10498 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10499 
10500 	/*
10501 	 * Need a lock since some flags can be set even when there are
10502 	 * references to the ipif.
10503 	 */
10504 	mutex_enter(&ill->ill_lock);
10505 	if (ipip->ipi_cmd_type == IF_CMD) {
10506 		struct ifreq *ifr = (struct ifreq *)if_req;
10507 
10508 		/* Get interface flags (low 16 only). */
10509 		ifr->ifr_flags = ((ipif->ipif_flags |
10510 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10511 	} else {
10512 		struct lifreq *lifr = (struct lifreq *)if_req;
10513 
10514 		/* Get interface flags. */
10515 		lifr->lifr_flags = ipif->ipif_flags |
10516 		    ill->ill_flags | phyi->phyint_flags;
10517 	}
10518 	mutex_exit(&ill->ill_lock);
10519 	return (0);
10520 }
10521 
10522 /*
10523  * We allow the MTU to be set on an ILL, but not have it be different
10524  * for different IPIFs since we don't actually send packets on IPIFs.
10525  */
10526 /* ARGSUSED */
10527 int
10528 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10529     ip_ioctl_cmd_t *ipip, void *if_req)
10530 {
10531 	int mtu;
10532 	int ip_min_mtu;
10533 	struct ifreq	*ifr;
10534 	struct lifreq *lifr;
10535 	ill_t	*ill;
10536 
10537 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10538 	    ipif->ipif_id, (void *)ipif));
10539 	if (ipip->ipi_cmd_type == IF_CMD) {
10540 		ifr = (struct ifreq *)if_req;
10541 		mtu = ifr->ifr_metric;
10542 	} else {
10543 		lifr = (struct lifreq *)if_req;
10544 		mtu = lifr->lifr_mtu;
10545 	}
10546 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10547 	if (ipif->ipif_id != 0)
10548 		return (EINVAL);
10549 
10550 	ill = ipif->ipif_ill;
10551 	if (ipif->ipif_isv6)
10552 		ip_min_mtu = IPV6_MIN_MTU;
10553 	else
10554 		ip_min_mtu = IP_MIN_MTU;
10555 
10556 	mutex_enter(&ill->ill_lock);
10557 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10558 		mutex_exit(&ill->ill_lock);
10559 		return (EINVAL);
10560 	}
10561 	/*
10562 	 * The dce and fragmentation code can handle changes to ill_mtu
10563 	 * concurrent with sending/fragmenting packets.
10564 	 */
10565 	ill->ill_mtu = mtu;
10566 	ill->ill_flags |= ILLF_FIXEDMTU;
10567 	mutex_exit(&ill->ill_lock);
10568 
10569 	/*
10570 	 * Make sure all dce_generation checks find out
10571 	 * that ill_mtu has changed.
10572 	 */
10573 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10574 
10575 	/* Update the MTU in SCTP's list */
10576 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10577 	return (0);
10578 }
10579 
10580 /* Get interface MTU. */
10581 /* ARGSUSED */
10582 int
10583 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10584 	ip_ioctl_cmd_t *ipip, void *if_req)
10585 {
10586 	struct ifreq	*ifr;
10587 	struct lifreq	*lifr;
10588 
10589 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10590 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10591 
10592 	/*
10593 	 * We allow a get on any logical interface even though the set
10594 	 * can only be done on logical unit 0.
10595 	 */
10596 	if (ipip->ipi_cmd_type == IF_CMD) {
10597 		ifr = (struct ifreq *)if_req;
10598 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10599 	} else {
10600 		lifr = (struct lifreq *)if_req;
10601 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10602 	}
10603 	return (0);
10604 }
10605 
10606 /* Set interface broadcast address. */
10607 /* ARGSUSED2 */
10608 int
10609 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10610 	ip_ioctl_cmd_t *ipip, void *if_req)
10611 {
10612 	ipaddr_t addr;
10613 	ire_t	*ire;
10614 	ill_t		*ill = ipif->ipif_ill;
10615 	ip_stack_t	*ipst = ill->ill_ipst;
10616 
10617 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10618 	    ipif->ipif_id));
10619 
10620 	ASSERT(IAM_WRITER_IPIF(ipif));
10621 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10622 		return (EADDRNOTAVAIL);
10623 
10624 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10625 
10626 	if (sin->sin_family != AF_INET)
10627 		return (EAFNOSUPPORT);
10628 
10629 	addr = sin->sin_addr.s_addr;
10630 	if (ipif->ipif_flags & IPIF_UP) {
10631 		/*
10632 		 * If we are already up, make sure the new
10633 		 * broadcast address makes sense.  If it does,
10634 		 * there should be an IRE for it already.
10635 		 */
10636 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10637 		    ill, ipif->ipif_zoneid, NULL,
10638 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10639 		if (ire == NULL) {
10640 			return (EINVAL);
10641 		} else {
10642 			ire_refrele(ire);
10643 		}
10644 	}
10645 	/*
10646 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10647 	 * needs to already exist we never need to change the set of
10648 	 * IRE_BROADCASTs when we are UP.
10649 	 */
10650 	if (addr != ipif->ipif_brd_addr)
10651 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10652 
10653 	return (0);
10654 }
10655 
10656 /* Get interface broadcast address. */
10657 /* ARGSUSED */
10658 int
10659 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10660     ip_ioctl_cmd_t *ipip, void *if_req)
10661 {
10662 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10663 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10664 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10665 		return (EADDRNOTAVAIL);
10666 
10667 	/* IPIF_BROADCAST not possible with IPv6 */
10668 	ASSERT(!ipif->ipif_isv6);
10669 	*sin = sin_null;
10670 	sin->sin_family = AF_INET;
10671 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10672 	return (0);
10673 }
10674 
10675 /*
10676  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10677  */
10678 /* ARGSUSED */
10679 int
10680 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10681     ip_ioctl_cmd_t *ipip, void *if_req)
10682 {
10683 	int err = 0;
10684 	in6_addr_t v6mask;
10685 
10686 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10688 
10689 	ASSERT(IAM_WRITER_IPIF(ipif));
10690 
10691 	if (ipif->ipif_isv6) {
10692 		sin6_t *sin6;
10693 
10694 		if (sin->sin_family != AF_INET6)
10695 			return (EAFNOSUPPORT);
10696 
10697 		sin6 = (sin6_t *)sin;
10698 		v6mask = sin6->sin6_addr;
10699 	} else {
10700 		ipaddr_t mask;
10701 
10702 		if (sin->sin_family != AF_INET)
10703 			return (EAFNOSUPPORT);
10704 
10705 		mask = sin->sin_addr.s_addr;
10706 		V4MASK_TO_V6(mask, v6mask);
10707 	}
10708 
10709 	/*
10710 	 * No big deal if the interface isn't already up, or the mask
10711 	 * isn't really changing, or this is pt-pt.
10712 	 */
10713 	if (!(ipif->ipif_flags & IPIF_UP) ||
10714 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10715 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10716 		ipif->ipif_v6net_mask = v6mask;
10717 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10718 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10719 			    ipif->ipif_v6net_mask,
10720 			    ipif->ipif_v6subnet);
10721 		}
10722 		return (0);
10723 	}
10724 	/*
10725 	 * Make sure we have valid net and subnet broadcast ire's
10726 	 * for the old netmask, if needed by other logical interfaces.
10727 	 */
10728 	err = ipif_logical_down(ipif, q, mp);
10729 	if (err == EINPROGRESS)
10730 		return (err);
10731 	(void) ipif_down_tail(ipif);
10732 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10733 	return (err);
10734 }
10735 
10736 static int
10737 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10738 {
10739 	in6_addr_t v6mask;
10740 	int err = 0;
10741 
10742 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10743 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10744 
10745 	if (ipif->ipif_isv6) {
10746 		sin6_t *sin6;
10747 
10748 		sin6 = (sin6_t *)sin;
10749 		v6mask = sin6->sin6_addr;
10750 	} else {
10751 		ipaddr_t mask;
10752 
10753 		mask = sin->sin_addr.s_addr;
10754 		V4MASK_TO_V6(mask, v6mask);
10755 	}
10756 
10757 	ipif->ipif_v6net_mask = v6mask;
10758 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10759 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10760 		    ipif->ipif_v6subnet);
10761 	}
10762 	err = ipif_up(ipif, q, mp);
10763 
10764 	if (err == 0 || err == EINPROGRESS) {
10765 		/*
10766 		 * The interface must be DL_BOUND if this packet has to
10767 		 * go out on the wire. Since we only go through a logical
10768 		 * down and are bound with the driver during an internal
10769 		 * down/up that is satisfied.
10770 		 */
10771 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10772 			/* Potentially broadcast an address mask reply. */
10773 			ipif_mask_reply(ipif);
10774 		}
10775 	}
10776 	return (err);
10777 }
10778 
10779 /* ARGSUSED */
10780 int
10781 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10782     ip_ioctl_cmd_t *ipip, void *if_req)
10783 {
10784 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10785 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10786 	(void) ipif_down_tail(ipif);
10787 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10788 }
10789 
10790 /* Get interface net mask. */
10791 /* ARGSUSED */
10792 int
10793 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10794     ip_ioctl_cmd_t *ipip, void *if_req)
10795 {
10796 	struct lifreq *lifr = (struct lifreq *)if_req;
10797 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10798 
10799 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10800 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10801 
10802 	/*
10803 	 * net mask can't change since we have a reference to the ipif.
10804 	 */
10805 	if (ipif->ipif_isv6) {
10806 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10807 		*sin6 = sin6_null;
10808 		sin6->sin6_family = AF_INET6;
10809 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10810 		lifr->lifr_addrlen =
10811 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10812 	} else {
10813 		*sin = sin_null;
10814 		sin->sin_family = AF_INET;
10815 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10816 		if (ipip->ipi_cmd_type == LIF_CMD) {
10817 			lifr->lifr_addrlen =
10818 			    ip_mask_to_plen(ipif->ipif_net_mask);
10819 		}
10820 	}
10821 	return (0);
10822 }
10823 
10824 /* ARGSUSED */
10825 int
10826 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10827     ip_ioctl_cmd_t *ipip, void *if_req)
10828 {
10829 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10830 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10831 
10832 	/*
10833 	 * Since no applications should ever be setting metrics on underlying
10834 	 * interfaces, we explicitly fail to smoke 'em out.
10835 	 */
10836 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10837 		return (EINVAL);
10838 
10839 	/*
10840 	 * Set interface metric.  We don't use this for
10841 	 * anything but we keep track of it in case it is
10842 	 * important to routing applications or such.
10843 	 */
10844 	if (ipip->ipi_cmd_type == IF_CMD) {
10845 		struct ifreq    *ifr;
10846 
10847 		ifr = (struct ifreq *)if_req;
10848 		ipif->ipif_metric = ifr->ifr_metric;
10849 	} else {
10850 		struct lifreq   *lifr;
10851 
10852 		lifr = (struct lifreq *)if_req;
10853 		ipif->ipif_metric = lifr->lifr_metric;
10854 	}
10855 	return (0);
10856 }
10857 
10858 /* ARGSUSED */
10859 int
10860 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10861     ip_ioctl_cmd_t *ipip, void *if_req)
10862 {
10863 	/* Get interface metric. */
10864 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10865 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10866 
10867 	if (ipip->ipi_cmd_type == IF_CMD) {
10868 		struct ifreq    *ifr;
10869 
10870 		ifr = (struct ifreq *)if_req;
10871 		ifr->ifr_metric = ipif->ipif_metric;
10872 	} else {
10873 		struct lifreq   *lifr;
10874 
10875 		lifr = (struct lifreq *)if_req;
10876 		lifr->lifr_metric = ipif->ipif_metric;
10877 	}
10878 
10879 	return (0);
10880 }
10881 
10882 /* ARGSUSED */
10883 int
10884 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10885     ip_ioctl_cmd_t *ipip, void *if_req)
10886 {
10887 	int	arp_muxid;
10888 
10889 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10890 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10891 	/*
10892 	 * Set the muxid returned from I_PLINK.
10893 	 */
10894 	if (ipip->ipi_cmd_type == IF_CMD) {
10895 		struct ifreq *ifr = (struct ifreq *)if_req;
10896 
10897 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10898 		arp_muxid = ifr->ifr_arp_muxid;
10899 	} else {
10900 		struct lifreq *lifr = (struct lifreq *)if_req;
10901 
10902 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10903 		arp_muxid = lifr->lifr_arp_muxid;
10904 	}
10905 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10906 	return (0);
10907 }
10908 
10909 /* ARGSUSED */
10910 int
10911 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10912     ip_ioctl_cmd_t *ipip, void *if_req)
10913 {
10914 	int	arp_muxid = 0;
10915 
10916 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10917 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10918 	/*
10919 	 * Get the muxid saved in ill for I_PUNLINK.
10920 	 */
10921 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10922 	if (ipip->ipi_cmd_type == IF_CMD) {
10923 		struct ifreq *ifr = (struct ifreq *)if_req;
10924 
10925 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10926 		ifr->ifr_arp_muxid = arp_muxid;
10927 	} else {
10928 		struct lifreq *lifr = (struct lifreq *)if_req;
10929 
10930 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10931 		lifr->lifr_arp_muxid = arp_muxid;
10932 	}
10933 	return (0);
10934 }
10935 
10936 /*
10937  * Set the subnet prefix. Does not modify the broadcast address.
10938  */
10939 /* ARGSUSED */
10940 int
10941 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10942     ip_ioctl_cmd_t *ipip, void *if_req)
10943 {
10944 	int err = 0;
10945 	in6_addr_t v6addr;
10946 	in6_addr_t v6mask;
10947 	boolean_t need_up = B_FALSE;
10948 	int addrlen;
10949 
10950 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10951 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10952 
10953 	ASSERT(IAM_WRITER_IPIF(ipif));
10954 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10955 
10956 	if (ipif->ipif_isv6) {
10957 		sin6_t *sin6;
10958 
10959 		if (sin->sin_family != AF_INET6)
10960 			return (EAFNOSUPPORT);
10961 
10962 		sin6 = (sin6_t *)sin;
10963 		v6addr = sin6->sin6_addr;
10964 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10965 			return (EADDRNOTAVAIL);
10966 	} else {
10967 		ipaddr_t addr;
10968 
10969 		if (sin->sin_family != AF_INET)
10970 			return (EAFNOSUPPORT);
10971 
10972 		addr = sin->sin_addr.s_addr;
10973 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10974 			return (EADDRNOTAVAIL);
10975 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10976 		/* Add 96 bits */
10977 		addrlen += IPV6_ABITS - IP_ABITS;
10978 	}
10979 
10980 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10981 		return (EINVAL);
10982 
10983 	/* Check if bits in the address is set past the mask */
10984 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10985 		return (EINVAL);
10986 
10987 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10988 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10989 		return (0);	/* No change */
10990 
10991 	if (ipif->ipif_flags & IPIF_UP) {
10992 		/*
10993 		 * If the interface is already marked up,
10994 		 * we call ipif_down which will take care
10995 		 * of ditching any IREs that have been set
10996 		 * up based on the old interface address.
10997 		 */
10998 		err = ipif_logical_down(ipif, q, mp);
10999 		if (err == EINPROGRESS)
11000 			return (err);
11001 		(void) ipif_down_tail(ipif);
11002 		need_up = B_TRUE;
11003 	}
11004 
11005 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11006 	return (err);
11007 }
11008 
11009 static int
11010 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11011     queue_t *q, mblk_t *mp, boolean_t need_up)
11012 {
11013 	ill_t	*ill = ipif->ipif_ill;
11014 	int	err = 0;
11015 
11016 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11017 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11018 
11019 	/* Set the new address. */
11020 	mutex_enter(&ill->ill_lock);
11021 	ipif->ipif_v6net_mask = v6mask;
11022 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11023 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11024 		    ipif->ipif_v6subnet);
11025 	}
11026 	mutex_exit(&ill->ill_lock);
11027 
11028 	if (need_up) {
11029 		/*
11030 		 * Now bring the interface back up.  If this
11031 		 * is the only IPIF for the ILL, ipif_up
11032 		 * will have to re-bind to the device, so
11033 		 * we may get back EINPROGRESS, in which
11034 		 * case, this IOCTL will get completed in
11035 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11036 		 */
11037 		err = ipif_up(ipif, q, mp);
11038 		if (err == EINPROGRESS)
11039 			return (err);
11040 	}
11041 	return (err);
11042 }
11043 
11044 /* ARGSUSED */
11045 int
11046 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11047     ip_ioctl_cmd_t *ipip, void *if_req)
11048 {
11049 	int	addrlen;
11050 	in6_addr_t v6addr;
11051 	in6_addr_t v6mask;
11052 	struct lifreq *lifr = (struct lifreq *)if_req;
11053 
11054 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11055 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11056 	(void) ipif_down_tail(ipif);
11057 
11058 	addrlen = lifr->lifr_addrlen;
11059 	if (ipif->ipif_isv6) {
11060 		sin6_t *sin6;
11061 
11062 		sin6 = (sin6_t *)sin;
11063 		v6addr = sin6->sin6_addr;
11064 	} else {
11065 		ipaddr_t addr;
11066 
11067 		addr = sin->sin_addr.s_addr;
11068 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11069 		addrlen += IPV6_ABITS - IP_ABITS;
11070 	}
11071 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11072 
11073 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11074 }
11075 
11076 /* ARGSUSED */
11077 int
11078 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11079     ip_ioctl_cmd_t *ipip, void *if_req)
11080 {
11081 	struct lifreq *lifr = (struct lifreq *)if_req;
11082 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11083 
11084 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11085 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11086 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11087 
11088 	if (ipif->ipif_isv6) {
11089 		*sin6 = sin6_null;
11090 		sin6->sin6_family = AF_INET6;
11091 		sin6->sin6_addr = ipif->ipif_v6subnet;
11092 		lifr->lifr_addrlen =
11093 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11094 	} else {
11095 		*sin = sin_null;
11096 		sin->sin_family = AF_INET;
11097 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11098 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11099 	}
11100 	return (0);
11101 }
11102 
11103 /*
11104  * Set the IPv6 address token.
11105  */
11106 /* ARGSUSED */
11107 int
11108 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11109     ip_ioctl_cmd_t *ipi, void *if_req)
11110 {
11111 	ill_t *ill = ipif->ipif_ill;
11112 	int err;
11113 	in6_addr_t v6addr;
11114 	in6_addr_t v6mask;
11115 	boolean_t need_up = B_FALSE;
11116 	int i;
11117 	sin6_t *sin6 = (sin6_t *)sin;
11118 	struct lifreq *lifr = (struct lifreq *)if_req;
11119 	int addrlen;
11120 
11121 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11122 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11123 	ASSERT(IAM_WRITER_IPIF(ipif));
11124 
11125 	addrlen = lifr->lifr_addrlen;
11126 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11127 	if (ipif->ipif_id != 0)
11128 		return (EINVAL);
11129 
11130 	if (!ipif->ipif_isv6)
11131 		return (EINVAL);
11132 
11133 	if (addrlen > IPV6_ABITS)
11134 		return (EINVAL);
11135 
11136 	v6addr = sin6->sin6_addr;
11137 
11138 	/*
11139 	 * The length of the token is the length from the end.  To get
11140 	 * the proper mask for this, compute the mask of the bits not
11141 	 * in the token; ie. the prefix, and then xor to get the mask.
11142 	 */
11143 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11144 		return (EINVAL);
11145 	for (i = 0; i < 4; i++) {
11146 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11147 	}
11148 
11149 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11150 	    ill->ill_token_length == addrlen)
11151 		return (0);	/* No change */
11152 
11153 	if (ipif->ipif_flags & IPIF_UP) {
11154 		err = ipif_logical_down(ipif, q, mp);
11155 		if (err == EINPROGRESS)
11156 			return (err);
11157 		(void) ipif_down_tail(ipif);
11158 		need_up = B_TRUE;
11159 	}
11160 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11161 	return (err);
11162 }
11163 
11164 static int
11165 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11166     mblk_t *mp, boolean_t need_up)
11167 {
11168 	in6_addr_t v6addr;
11169 	in6_addr_t v6mask;
11170 	ill_t	*ill = ipif->ipif_ill;
11171 	int	i;
11172 	int	err = 0;
11173 
11174 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11175 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11176 	v6addr = sin6->sin6_addr;
11177 	/*
11178 	 * The length of the token is the length from the end.  To get
11179 	 * the proper mask for this, compute the mask of the bits not
11180 	 * in the token; ie. the prefix, and then xor to get the mask.
11181 	 */
11182 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11183 	for (i = 0; i < 4; i++)
11184 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11185 
11186 	mutex_enter(&ill->ill_lock);
11187 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11188 	ill->ill_token_length = addrlen;
11189 	ill->ill_manual_token = 1;
11190 
11191 	/* Reconfigure the link-local address based on this new token */
11192 	ipif_setlinklocal(ill->ill_ipif);
11193 
11194 	mutex_exit(&ill->ill_lock);
11195 
11196 	if (need_up) {
11197 		/*
11198 		 * Now bring the interface back up.  If this
11199 		 * is the only IPIF for the ILL, ipif_up
11200 		 * will have to re-bind to the device, so
11201 		 * we may get back EINPROGRESS, in which
11202 		 * case, this IOCTL will get completed in
11203 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11204 		 */
11205 		err = ipif_up(ipif, q, mp);
11206 		if (err == EINPROGRESS)
11207 			return (err);
11208 	}
11209 	return (err);
11210 }
11211 
11212 /* ARGSUSED */
11213 int
11214 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11215     ip_ioctl_cmd_t *ipi, void *if_req)
11216 {
11217 	ill_t *ill;
11218 	sin6_t *sin6 = (sin6_t *)sin;
11219 	struct lifreq *lifr = (struct lifreq *)if_req;
11220 
11221 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11222 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11223 	if (ipif->ipif_id != 0)
11224 		return (EINVAL);
11225 
11226 	ill = ipif->ipif_ill;
11227 	if (!ill->ill_isv6)
11228 		return (ENXIO);
11229 
11230 	*sin6 = sin6_null;
11231 	sin6->sin6_family = AF_INET6;
11232 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11233 	sin6->sin6_addr = ill->ill_token;
11234 	lifr->lifr_addrlen = ill->ill_token_length;
11235 	return (0);
11236 }
11237 
11238 /*
11239  * Set (hardware) link specific information that might override
11240  * what was acquired through the DL_INFO_ACK.
11241  */
11242 /* ARGSUSED */
11243 int
11244 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11245     ip_ioctl_cmd_t *ipi, void *if_req)
11246 {
11247 	ill_t		*ill = ipif->ipif_ill;
11248 	int		ip_min_mtu;
11249 	struct lifreq	*lifr = (struct lifreq *)if_req;
11250 	lif_ifinfo_req_t *lir;
11251 
11252 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11253 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11254 	lir = &lifr->lifr_ifinfo;
11255 	ASSERT(IAM_WRITER_IPIF(ipif));
11256 
11257 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11258 	if (ipif->ipif_id != 0)
11259 		return (EINVAL);
11260 
11261 	/* Set interface MTU. */
11262 	if (ipif->ipif_isv6)
11263 		ip_min_mtu = IPV6_MIN_MTU;
11264 	else
11265 		ip_min_mtu = IP_MIN_MTU;
11266 
11267 	/*
11268 	 * Verify values before we set anything. Allow zero to
11269 	 * mean unspecified.
11270 	 *
11271 	 * XXX We should be able to set the user-defined lir_mtu to some value
11272 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11273 	 * ill_max_frag value tells us the max MTU that can be handled by the
11274 	 * datalink, whereas the ill_current_frag is dynamically computed for
11275 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11276 	 * since there is currently no way of distinguishing between
11277 	 * administratively fixed link mtu values (e.g., those set via
11278 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11279 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11280 	 * upper-bound.
11281 	 */
11282 	if (lir->lir_maxmtu != 0 &&
11283 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11284 	    lir->lir_maxmtu < ip_min_mtu))
11285 		return (EINVAL);
11286 	if (lir->lir_reachtime != 0 &&
11287 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11288 		return (EINVAL);
11289 	if (lir->lir_reachretrans != 0 &&
11290 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11291 		return (EINVAL);
11292 
11293 	mutex_enter(&ill->ill_lock);
11294 	/*
11295 	 * The dce and fragmentation code can handle changes to ill_mtu
11296 	 * concurrent with sending/fragmenting packets.
11297 	 */
11298 	if (lir->lir_maxmtu != 0)
11299 		ill->ill_user_mtu = lir->lir_maxmtu;
11300 
11301 	if (lir->lir_reachtime != 0)
11302 		ill->ill_reachable_time = lir->lir_reachtime;
11303 
11304 	if (lir->lir_reachretrans != 0)
11305 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11306 
11307 	ill->ill_max_hops = lir->lir_maxhops;
11308 	ill->ill_max_buf = ND_MAX_Q;
11309 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11310 		/*
11311 		 * ill_mtu is the actual interface MTU, obtained as the min
11312 		 * of user-configured mtu and the value announced by the
11313 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11314 		 * we have already made the choice of requiring
11315 		 * ill_user_mtu < ill_current_frag by the time we get here,
11316 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11317 		 * here.
11318 		 */
11319 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11320 	}
11321 	mutex_exit(&ill->ill_lock);
11322 
11323 	/*
11324 	 * Make sure all dce_generation checks find out
11325 	 * that ill_mtu has changed.
11326 	 */
11327 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11328 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11329 
11330 	/*
11331 	 * Refresh IPMP meta-interface MTU if necessary.
11332 	 */
11333 	if (IS_UNDER_IPMP(ill))
11334 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11335 
11336 	return (0);
11337 }
11338 
11339 /* ARGSUSED */
11340 int
11341 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11342     ip_ioctl_cmd_t *ipi, void *if_req)
11343 {
11344 	struct lif_ifinfo_req *lir;
11345 	ill_t *ill = ipif->ipif_ill;
11346 
11347 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11348 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11349 	if (ipif->ipif_id != 0)
11350 		return (EINVAL);
11351 
11352 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11353 	lir->lir_maxhops = ill->ill_max_hops;
11354 	lir->lir_reachtime = ill->ill_reachable_time;
11355 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11356 	lir->lir_maxmtu = ill->ill_mtu;
11357 
11358 	return (0);
11359 }
11360 
11361 /*
11362  * Return best guess as to the subnet mask for the specified address.
11363  * Based on the subnet masks for all the configured interfaces.
11364  *
11365  * We end up returning a zero mask in the case of default, multicast or
11366  * experimental.
11367  */
11368 static ipaddr_t
11369 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11370 {
11371 	ipaddr_t net_mask;
11372 	ill_t	*ill;
11373 	ipif_t	*ipif;
11374 	ill_walk_context_t ctx;
11375 	ipif_t	*fallback_ipif = NULL;
11376 
11377 	net_mask = ip_net_mask(addr);
11378 	if (net_mask == 0) {
11379 		*ipifp = NULL;
11380 		return (0);
11381 	}
11382 
11383 	/* Let's check to see if this is maybe a local subnet route. */
11384 	/* this function only applies to IPv4 interfaces */
11385 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11386 	ill = ILL_START_WALK_V4(&ctx, ipst);
11387 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11388 		mutex_enter(&ill->ill_lock);
11389 		for (ipif = ill->ill_ipif; ipif != NULL;
11390 		    ipif = ipif->ipif_next) {
11391 			if (IPIF_IS_CONDEMNED(ipif))
11392 				continue;
11393 			if (!(ipif->ipif_flags & IPIF_UP))
11394 				continue;
11395 			if ((ipif->ipif_subnet & net_mask) ==
11396 			    (addr & net_mask)) {
11397 				/*
11398 				 * Don't trust pt-pt interfaces if there are
11399 				 * other interfaces.
11400 				 */
11401 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11402 					if (fallback_ipif == NULL) {
11403 						ipif_refhold_locked(ipif);
11404 						fallback_ipif = ipif;
11405 					}
11406 					continue;
11407 				}
11408 
11409 				/*
11410 				 * Fine. Just assume the same net mask as the
11411 				 * directly attached subnet interface is using.
11412 				 */
11413 				ipif_refhold_locked(ipif);
11414 				mutex_exit(&ill->ill_lock);
11415 				rw_exit(&ipst->ips_ill_g_lock);
11416 				if (fallback_ipif != NULL)
11417 					ipif_refrele(fallback_ipif);
11418 				*ipifp = ipif;
11419 				return (ipif->ipif_net_mask);
11420 			}
11421 		}
11422 		mutex_exit(&ill->ill_lock);
11423 	}
11424 	rw_exit(&ipst->ips_ill_g_lock);
11425 
11426 	*ipifp = fallback_ipif;
11427 	return ((fallback_ipif != NULL) ?
11428 	    fallback_ipif->ipif_net_mask : net_mask);
11429 }
11430 
11431 /*
11432  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11433  */
11434 static void
11435 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11436 {
11437 	IOCP	iocp;
11438 	ipft_t	*ipft;
11439 	ipllc_t	*ipllc;
11440 	mblk_t	*mp1;
11441 	cred_t	*cr;
11442 	int	error = 0;
11443 	conn_t	*connp;
11444 
11445 	ip1dbg(("ip_wput_ioctl"));
11446 	iocp = (IOCP)mp->b_rptr;
11447 	mp1 = mp->b_cont;
11448 	if (mp1 == NULL) {
11449 		iocp->ioc_error = EINVAL;
11450 		mp->b_datap->db_type = M_IOCNAK;
11451 		iocp->ioc_count = 0;
11452 		qreply(q, mp);
11453 		return;
11454 	}
11455 
11456 	/*
11457 	 * These IOCTLs provide various control capabilities to
11458 	 * upstream agents such as ULPs and processes.	There
11459 	 * are currently two such IOCTLs implemented.  They
11460 	 * are used by TCP to provide update information for
11461 	 * existing IREs and to forcibly delete an IRE for a
11462 	 * host that is not responding, thereby forcing an
11463 	 * attempt at a new route.
11464 	 */
11465 	iocp->ioc_error = EINVAL;
11466 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11467 		goto done;
11468 
11469 	ipllc = (ipllc_t *)mp1->b_rptr;
11470 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11471 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11472 			break;
11473 	}
11474 	/*
11475 	 * prefer credential from mblk over ioctl;
11476 	 * see ip_sioctl_copyin_setup
11477 	 */
11478 	cr = msg_getcred(mp, NULL);
11479 	if (cr == NULL)
11480 		cr = iocp->ioc_cr;
11481 
11482 	/*
11483 	 * Refhold the conn in case the request gets queued up in some lookup
11484 	 */
11485 	ASSERT(CONN_Q(q));
11486 	connp = Q_TO_CONN(q);
11487 	CONN_INC_REF(connp);
11488 	if (ipft->ipft_pfi &&
11489 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11490 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11491 		error = (*ipft->ipft_pfi)(q,
11492 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11493 	}
11494 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11495 		/*
11496 		 * CONN_OPER_PENDING_DONE happens in the function called
11497 		 * through ipft_pfi above.
11498 		 */
11499 		return;
11500 	}
11501 
11502 	CONN_OPER_PENDING_DONE(connp);
11503 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11504 		freemsg(mp);
11505 		return;
11506 	}
11507 	iocp->ioc_error = error;
11508 
11509 done:
11510 	mp->b_datap->db_type = M_IOCACK;
11511 	if (iocp->ioc_error)
11512 		iocp->ioc_count = 0;
11513 	qreply(q, mp);
11514 }
11515 
11516 /*
11517  * Assign a unique id for the ipif. This is used by sctp_addr.c
11518  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11519  */
11520 static void
11521 ipif_assign_seqid(ipif_t *ipif)
11522 {
11523 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11524 
11525 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11526 }
11527 
11528 /*
11529  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11530  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11531  * that the clone is complete -- including the seqid -- and the expectation is
11532  * that the caller will either free or overwrite `sipif' before it's unlocked.
11533  */
11534 static void
11535 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11536 {
11537 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11538 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11539 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11540 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11541 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11542 
11543 	dipif->ipif_flags = sipif->ipif_flags;
11544 	dipif->ipif_metric = sipif->ipif_metric;
11545 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11546 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11547 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11548 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11549 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11550 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11551 
11552 	/*
11553 	 * As per the comment atop the function, we assume that these sipif
11554 	 * fields will be changed before sipif is unlocked.
11555 	 */
11556 	dipif->ipif_seqid = sipif->ipif_seqid;
11557 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11558 }
11559 
11560 /*
11561  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11562  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11563  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11564  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11565  * down (i.e., no DAD), of the same type, and unlocked.
11566  */
11567 static void
11568 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11569 {
11570 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11571 	ipxop_t *ipx = ipsq->ipsq_xop;
11572 
11573 	ASSERT(sipif != dipif);
11574 	ASSERT(sipif != virgipif);
11575 
11576 	/*
11577 	 * Grab all of the locks that protect the ipif in a defined order.
11578 	 */
11579 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11580 
11581 	ipif_clone(sipif, dipif);
11582 	if (virgipif != NULL) {
11583 		ipif_clone(virgipif, sipif);
11584 		mi_free(virgipif);
11585 	}
11586 
11587 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11588 
11589 	/*
11590 	 * Transfer ownership of the current xop, if necessary.
11591 	 */
11592 	if (ipx->ipx_current_ipif == sipif) {
11593 		ASSERT(ipx->ipx_pending_ipif == NULL);
11594 		mutex_enter(&ipx->ipx_lock);
11595 		ipx->ipx_current_ipif = dipif;
11596 		mutex_exit(&ipx->ipx_lock);
11597 	}
11598 
11599 	if (virgipif == NULL)
11600 		mi_free(sipif);
11601 }
11602 
11603 /*
11604  * checks if:
11605  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11606  *	- logical interface is within the allowed range
11607  */
11608 static int
11609 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11610 {
11611 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11612 		return (ENAMETOOLONG);
11613 
11614 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11615 		return (ERANGE);
11616 	return (0);
11617 }
11618 
11619 /*
11620  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11621  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11622  * be inserted into the first space available in the list. The value of
11623  * ipif_id will then be set to the appropriate value for its position.
11624  */
11625 static int
11626 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11627 {
11628 	ill_t *ill;
11629 	ipif_t *tipif;
11630 	ipif_t **tipifp;
11631 	int id, err;
11632 	ip_stack_t	*ipst;
11633 
11634 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11635 	    IAM_WRITER_IPIF(ipif));
11636 
11637 	ill = ipif->ipif_ill;
11638 	ASSERT(ill != NULL);
11639 	ipst = ill->ill_ipst;
11640 
11641 	/*
11642 	 * In the case of lo0:0 we already hold the ill_g_lock.
11643 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11644 	 * ipif_insert.
11645 	 */
11646 	if (acquire_g_lock)
11647 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11648 	mutex_enter(&ill->ill_lock);
11649 	id = ipif->ipif_id;
11650 	tipifp = &(ill->ill_ipif);
11651 	if (id == -1) {	/* need to find a real id */
11652 		id = 0;
11653 		while ((tipif = *tipifp) != NULL) {
11654 			ASSERT(tipif->ipif_id >= id);
11655 			if (tipif->ipif_id != id)
11656 				break; /* non-consecutive id */
11657 			id++;
11658 			tipifp = &(tipif->ipif_next);
11659 		}
11660 		if ((err = is_lifname_valid(ill, id)) != 0) {
11661 			mutex_exit(&ill->ill_lock);
11662 			if (acquire_g_lock)
11663 				rw_exit(&ipst->ips_ill_g_lock);
11664 			return (err);
11665 		}
11666 		ipif->ipif_id = id; /* assign new id */
11667 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11668 		/* we have a real id; insert ipif in the right place */
11669 		while ((tipif = *tipifp) != NULL) {
11670 			ASSERT(tipif->ipif_id != id);
11671 			if (tipif->ipif_id > id)
11672 				break; /* found correct location */
11673 			tipifp = &(tipif->ipif_next);
11674 		}
11675 	} else {
11676 		mutex_exit(&ill->ill_lock);
11677 		if (acquire_g_lock)
11678 			rw_exit(&ipst->ips_ill_g_lock);
11679 		return (err);
11680 	}
11681 
11682 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11683 
11684 	ipif->ipif_next = tipif;
11685 	*tipifp = ipif;
11686 	mutex_exit(&ill->ill_lock);
11687 	if (acquire_g_lock)
11688 		rw_exit(&ipst->ips_ill_g_lock);
11689 
11690 	return (0);
11691 }
11692 
11693 static void
11694 ipif_remove(ipif_t *ipif)
11695 {
11696 	ipif_t	**ipifp;
11697 	ill_t	*ill = ipif->ipif_ill;
11698 
11699 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11700 
11701 	mutex_enter(&ill->ill_lock);
11702 	ipifp = &ill->ill_ipif;
11703 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11704 		if (*ipifp == ipif) {
11705 			*ipifp = ipif->ipif_next;
11706 			break;
11707 		}
11708 	}
11709 	mutex_exit(&ill->ill_lock);
11710 }
11711 
11712 /*
11713  * Allocate and initialize a new interface control structure.  (Always
11714  * called as writer.)
11715  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11716  * is not part of the global linked list of ills. ipif_seqid is unique
11717  * in the system and to preserve the uniqueness, it is assigned only
11718  * when ill becomes part of the global list. At that point ill will
11719  * have a name. If it doesn't get assigned here, it will get assigned
11720  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11721  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11722  * the interface flags or any other information from the DL_INFO_ACK for
11723  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11724  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11725  * second DL_INFO_ACK comes in from the driver.
11726  */
11727 static ipif_t *
11728 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11729     boolean_t insert, int *errorp)
11730 {
11731 	int err;
11732 	ipif_t	*ipif;
11733 	ip_stack_t *ipst = ill->ill_ipst;
11734 
11735 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11736 	    ill->ill_name, id, (void *)ill));
11737 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11738 
11739 	if (errorp != NULL)
11740 		*errorp = 0;
11741 
11742 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11743 		if (errorp != NULL)
11744 			*errorp = ENOMEM;
11745 		return (NULL);
11746 	}
11747 	*ipif = ipif_zero;	/* start clean */
11748 
11749 	ipif->ipif_ill = ill;
11750 	ipif->ipif_id = id;	/* could be -1 */
11751 	/*
11752 	 * Inherit the zoneid from the ill; for the shared stack instance
11753 	 * this is always the global zone
11754 	 */
11755 	ipif->ipif_zoneid = ill->ill_zoneid;
11756 
11757 	ipif->ipif_refcnt = 0;
11758 
11759 	if (insert) {
11760 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11761 			mi_free(ipif);
11762 			if (errorp != NULL)
11763 				*errorp = err;
11764 			return (NULL);
11765 		}
11766 		/* -1 id should have been replaced by real id */
11767 		id = ipif->ipif_id;
11768 		ASSERT(id >= 0);
11769 	}
11770 
11771 	if (ill->ill_name[0] != '\0')
11772 		ipif_assign_seqid(ipif);
11773 
11774 	/*
11775 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11776 	 * (which must not exist yet because the zeroth ipif is created once
11777 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11778 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11779 	 */
11780 	if (id == 0 && IS_IPMP(ill)) {
11781 		if (ipmp_illgrp_create(ill) == NULL) {
11782 			if (insert) {
11783 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11784 				ipif_remove(ipif);
11785 				rw_exit(&ipst->ips_ill_g_lock);
11786 			}
11787 			mi_free(ipif);
11788 			if (errorp != NULL)
11789 				*errorp = ENOMEM;
11790 			return (NULL);
11791 		}
11792 	}
11793 
11794 	/*
11795 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11796 	 * not up and can't be looked up until the ioctl completes and the
11797 	 * IPIF_CHANGING flag is cleared.
11798 	 */
11799 	mutex_enter(&ill->ill_lock);
11800 
11801 	ipif->ipif_ire_type = ire_type;
11802 
11803 	if (ipif->ipif_isv6) {
11804 		ill->ill_flags |= ILLF_IPV6;
11805 	} else {
11806 		ipaddr_t inaddr_any = INADDR_ANY;
11807 
11808 		ill->ill_flags |= ILLF_IPV4;
11809 
11810 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11811 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11812 		    &ipif->ipif_v6lcl_addr);
11813 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11814 		    &ipif->ipif_v6subnet);
11815 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11816 		    &ipif->ipif_v6net_mask);
11817 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11818 		    &ipif->ipif_v6brd_addr);
11819 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11820 		    &ipif->ipif_v6pp_dst_addr);
11821 	}
11822 
11823 	/*
11824 	 * Don't set the interface flags etc. now, will do it in
11825 	 * ip_ll_subnet_defaults.
11826 	 */
11827 	if (!initialize)
11828 		goto out;
11829 
11830 	/*
11831 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11832 	 * with no underlying interfaces (and thus an unknown broadcast
11833 	 * address length), but all interfaces that can be placed into an IPMP
11834 	 * group are required to be broadcast-capable.
11835 	 */
11836 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11837 		/*
11838 		 * Later detect lack of DLPI driver multicast capability by
11839 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11840 		 */
11841 		ill->ill_flags |= ILLF_MULTICAST;
11842 		if (!ipif->ipif_isv6)
11843 			ipif->ipif_flags |= IPIF_BROADCAST;
11844 	} else {
11845 		if (ill->ill_net_type != IRE_LOOPBACK) {
11846 			if (ipif->ipif_isv6)
11847 				/*
11848 				 * Note: xresolv interfaces will eventually need
11849 				 * NOARP set here as well, but that will require
11850 				 * those external resolvers to have some
11851 				 * knowledge of that flag and act appropriately.
11852 				 * Not to be changed at present.
11853 				 */
11854 				ill->ill_flags |= ILLF_NONUD;
11855 			else
11856 				ill->ill_flags |= ILLF_NOARP;
11857 		}
11858 		if (ill->ill_phys_addr_length == 0) {
11859 			if (IS_VNI(ill)) {
11860 				ipif->ipif_flags |= IPIF_NOXMIT;
11861 			} else {
11862 				/* pt-pt supports multicast. */
11863 				ill->ill_flags |= ILLF_MULTICAST;
11864 				if (ill->ill_net_type != IRE_LOOPBACK)
11865 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11866 			}
11867 		}
11868 	}
11869 out:
11870 	mutex_exit(&ill->ill_lock);
11871 	return (ipif);
11872 }
11873 
11874 /*
11875  * Remove the neighbor cache entries associated with this logical
11876  * interface.
11877  */
11878 int
11879 ipif_arp_down(ipif_t *ipif)
11880 {
11881 	ill_t	*ill = ipif->ipif_ill;
11882 	int	err = 0;
11883 
11884 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11885 	ASSERT(IAM_WRITER_IPIF(ipif));
11886 
11887 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11888 	    ill_t *, ill, ipif_t *, ipif);
11889 	ipif_nce_down(ipif);
11890 
11891 	/*
11892 	 * If this is the last ipif that is going down and there are no
11893 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11894 	 * clean up ARP completely.
11895 	 */
11896 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11897 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11898 		/*
11899 		 * If this was the last ipif on an IPMP interface, purge any
11900 		 * static ARP entries associated with it.
11901 		 */
11902 		if (IS_IPMP(ill))
11903 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11904 
11905 		/* UNBIND, DETACH */
11906 		err = arp_ll_down(ill);
11907 	}
11908 
11909 	return (err);
11910 }
11911 
11912 /*
11913  * Get the resolver set up for a new IP address.  (Always called as writer.)
11914  * Called both for IPv4 and IPv6 interfaces, though it only does some
11915  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11916  *
11917  * The enumerated value res_act tunes the behavior:
11918  * 	* Res_act_initial: set up all the resolver structures for a new
11919  *	  IP address.
11920  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11921  *	  ARP message in defense of the address.
11922  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11923  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11924  *
11925  * Returns zero on success, or an errno upon failure.
11926  */
11927 int
11928 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11929 {
11930 	ill_t		*ill = ipif->ipif_ill;
11931 	int		err;
11932 	boolean_t	was_dup;
11933 
11934 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11935 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11936 	ASSERT(IAM_WRITER_IPIF(ipif));
11937 
11938 	was_dup = B_FALSE;
11939 	if (res_act == Res_act_initial) {
11940 		ipif->ipif_addr_ready = 0;
11941 		/*
11942 		 * We're bringing an interface up here.  There's no way that we
11943 		 * should need to shut down ARP now.
11944 		 */
11945 		mutex_enter(&ill->ill_lock);
11946 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11947 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11948 			ill->ill_ipif_dup_count--;
11949 			was_dup = B_TRUE;
11950 		}
11951 		mutex_exit(&ill->ill_lock);
11952 	}
11953 	if (ipif->ipif_recovery_id != 0)
11954 		(void) untimeout(ipif->ipif_recovery_id);
11955 	ipif->ipif_recovery_id = 0;
11956 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11957 		ipif->ipif_addr_ready = 1;
11958 		return (0);
11959 	}
11960 	/* NDP will set the ipif_addr_ready flag when it's ready */
11961 	if (ill->ill_isv6)
11962 		return (0);
11963 
11964 	err = ipif_arp_up(ipif, res_act, was_dup);
11965 	return (err);
11966 }
11967 
11968 /*
11969  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11970  * when a link has just gone back up.
11971  */
11972 static void
11973 ipif_nce_start_dad(ipif_t *ipif)
11974 {
11975 	ncec_t *ncec;
11976 	ill_t *ill = ipif->ipif_ill;
11977 	boolean_t isv6 = ill->ill_isv6;
11978 
11979 	if (isv6) {
11980 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11981 		    &ipif->ipif_v6lcl_addr);
11982 	} else {
11983 		ipaddr_t v4addr;
11984 
11985 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11986 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11987 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11988 			/*
11989 			 * If we can't contact ARP for some reason,
11990 			 * that's not really a problem.  Just send
11991 			 * out the routing socket notification that
11992 			 * DAD completion would have done, and continue.
11993 			 */
11994 			ipif_mask_reply(ipif);
11995 			ipif_up_notify(ipif);
11996 			ipif->ipif_addr_ready = 1;
11997 			return;
11998 		}
11999 
12000 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12001 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12002 	}
12003 
12004 	if (ncec == NULL) {
12005 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12006 		    (void *)ipif));
12007 		return;
12008 	}
12009 	if (!nce_restart_dad(ncec)) {
12010 		/*
12011 		 * If we can't restart DAD for some reason, that's not really a
12012 		 * problem.  Just send out the routing socket notification that
12013 		 * DAD completion would have done, and continue.
12014 		 */
12015 		ipif_up_notify(ipif);
12016 		ipif->ipif_addr_ready = 1;
12017 	}
12018 	ncec_refrele(ncec);
12019 }
12020 
12021 /*
12022  * Restart duplicate address detection on all interfaces on the given ill.
12023  *
12024  * This is called when an interface transitions from down to up
12025  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12026  *
12027  * Note that since the underlying physical link has transitioned, we must cause
12028  * at least one routing socket message to be sent here, either via DAD
12029  * completion or just by default on the first ipif.  (If we don't do this, then
12030  * in.mpathd will see long delays when doing link-based failure recovery.)
12031  */
12032 void
12033 ill_restart_dad(ill_t *ill, boolean_t went_up)
12034 {
12035 	ipif_t *ipif;
12036 
12037 	if (ill == NULL)
12038 		return;
12039 
12040 	/*
12041 	 * If layer two doesn't support duplicate address detection, then just
12042 	 * send the routing socket message now and be done with it.
12043 	 */
12044 	if (!ill->ill_isv6 && arp_no_defense) {
12045 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12046 		return;
12047 	}
12048 
12049 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12050 		if (went_up) {
12051 
12052 			if (ipif->ipif_flags & IPIF_UP) {
12053 				ipif_nce_start_dad(ipif);
12054 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12055 				/*
12056 				 * kick off the bring-up process now.
12057 				 */
12058 				ipif_do_recovery(ipif);
12059 			} else {
12060 				/*
12061 				 * Unfortunately, the first ipif is "special"
12062 				 * and represents the underlying ill in the
12063 				 * routing socket messages.  Thus, when this
12064 				 * one ipif is down, we must still notify so
12065 				 * that the user knows the IFF_RUNNING status
12066 				 * change.  (If the first ipif is up, then
12067 				 * we'll handle eventual routing socket
12068 				 * notification via DAD completion.)
12069 				 */
12070 				if (ipif == ill->ill_ipif) {
12071 					ip_rts_ifmsg(ill->ill_ipif,
12072 					    RTSQ_DEFAULT);
12073 				}
12074 			}
12075 		} else {
12076 			/*
12077 			 * After link down, we'll need to send a new routing
12078 			 * message when the link comes back, so clear
12079 			 * ipif_addr_ready.
12080 			 */
12081 			ipif->ipif_addr_ready = 0;
12082 		}
12083 	}
12084 
12085 	/*
12086 	 * If we've torn down links, then notify the user right away.
12087 	 */
12088 	if (!went_up)
12089 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12090 }
12091 
12092 static void
12093 ipsq_delete(ipsq_t *ipsq)
12094 {
12095 	ipxop_t *ipx = ipsq->ipsq_xop;
12096 
12097 	ipsq->ipsq_ipst = NULL;
12098 	ASSERT(ipsq->ipsq_phyint == NULL);
12099 	ASSERT(ipsq->ipsq_xop != NULL);
12100 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12101 	ASSERT(ipx->ipx_pending_mp == NULL);
12102 	kmem_free(ipsq, sizeof (ipsq_t));
12103 }
12104 
12105 static int
12106 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12107 {
12108 	int err = 0;
12109 	ipif_t *ipif;
12110 
12111 	if (ill == NULL)
12112 		return (0);
12113 
12114 	ASSERT(IAM_WRITER_ILL(ill));
12115 	ill->ill_up_ipifs = B_TRUE;
12116 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12117 		if (ipif->ipif_was_up) {
12118 			if (!(ipif->ipif_flags & IPIF_UP))
12119 				err = ipif_up(ipif, q, mp);
12120 			ipif->ipif_was_up = B_FALSE;
12121 			if (err != 0) {
12122 				ASSERT(err == EINPROGRESS);
12123 				return (err);
12124 			}
12125 		}
12126 	}
12127 	ill->ill_up_ipifs = B_FALSE;
12128 	return (0);
12129 }
12130 
12131 /*
12132  * This function is called to bring up all the ipifs that were up before
12133  * bringing the ill down via ill_down_ipifs().
12134  */
12135 int
12136 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12137 {
12138 	int err;
12139 
12140 	ASSERT(IAM_WRITER_ILL(ill));
12141 
12142 	if (ill->ill_replumbing) {
12143 		ill->ill_replumbing = 0;
12144 		/*
12145 		 * Send down REPLUMB_DONE notification followed by the
12146 		 * BIND_REQ on the arp stream.
12147 		 */
12148 		if (!ill->ill_isv6)
12149 			arp_send_replumb_conf(ill);
12150 	}
12151 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12152 	if (err != 0)
12153 		return (err);
12154 
12155 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12156 }
12157 
12158 /*
12159  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12160  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12161  */
12162 static void
12163 ill_down_ipifs(ill_t *ill, boolean_t logical)
12164 {
12165 	ipif_t *ipif;
12166 
12167 	ASSERT(IAM_WRITER_ILL(ill));
12168 
12169 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12170 		/*
12171 		 * We go through the ipif_down logic even if the ipif
12172 		 * is already down, since routes can be added based
12173 		 * on down ipifs. Going through ipif_down once again
12174 		 * will delete any IREs created based on these routes.
12175 		 */
12176 		if (ipif->ipif_flags & IPIF_UP)
12177 			ipif->ipif_was_up = B_TRUE;
12178 
12179 		if (logical) {
12180 			(void) ipif_logical_down(ipif, NULL, NULL);
12181 			ipif_non_duplicate(ipif);
12182 			(void) ipif_down_tail(ipif);
12183 		} else {
12184 			(void) ipif_down(ipif, NULL, NULL);
12185 		}
12186 	}
12187 }
12188 
12189 /*
12190  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12191  * a look again at valid source addresses.
12192  * This should be called each time after the set of source addresses has been
12193  * changed.
12194  */
12195 void
12196 ip_update_source_selection(ip_stack_t *ipst)
12197 {
12198 	/* We skip past SRC_GENERATION_VERIFY */
12199 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12200 	    SRC_GENERATION_VERIFY)
12201 		atomic_add_32(&ipst->ips_src_generation, 1);
12202 }
12203 
12204 /*
12205  * Finish the group join started in ip_sioctl_groupname().
12206  */
12207 /* ARGSUSED */
12208 static void
12209 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12210 {
12211 	ill_t		*ill = q->q_ptr;
12212 	phyint_t	*phyi = ill->ill_phyint;
12213 	ipmp_grp_t	*grp = phyi->phyint_grp;
12214 	ip_stack_t	*ipst = ill->ill_ipst;
12215 
12216 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12217 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12218 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12219 
12220 	if (phyi->phyint_illv4 != NULL) {
12221 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12222 		VERIFY(grp->gr_pendv4-- > 0);
12223 		rw_exit(&ipst->ips_ipmp_lock);
12224 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12225 	}
12226 	if (phyi->phyint_illv6 != NULL) {
12227 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12228 		VERIFY(grp->gr_pendv6-- > 0);
12229 		rw_exit(&ipst->ips_ipmp_lock);
12230 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12231 	}
12232 	freemsg(mp);
12233 }
12234 
12235 /*
12236  * Process an SIOCSLIFGROUPNAME request.
12237  */
12238 /* ARGSUSED */
12239 int
12240 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12241     ip_ioctl_cmd_t *ipip, void *ifreq)
12242 {
12243 	struct lifreq	*lifr = ifreq;
12244 	ill_t		*ill = ipif->ipif_ill;
12245 	ip_stack_t	*ipst = ill->ill_ipst;
12246 	phyint_t	*phyi = ill->ill_phyint;
12247 	ipmp_grp_t	*grp = phyi->phyint_grp;
12248 	mblk_t		*ipsq_mp;
12249 	int		err = 0;
12250 
12251 	/*
12252 	 * Note that phyint_grp can only change here, where we're exclusive.
12253 	 */
12254 	ASSERT(IAM_WRITER_ILL(ill));
12255 
12256 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12257 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12258 		return (EINVAL);
12259 
12260 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12261 
12262 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12263 
12264 	/*
12265 	 * If the name hasn't changed, there's nothing to do.
12266 	 */
12267 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12268 		goto unlock;
12269 
12270 	/*
12271 	 * Handle requests to rename an IPMP meta-interface.
12272 	 *
12273 	 * Note that creation of the IPMP meta-interface is handled in
12274 	 * userland through the standard plumbing sequence.  As part of the
12275 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12276 	 * the name of the interface (see ipif_set_values_tail()).
12277 	 */
12278 	if (IS_IPMP(ill)) {
12279 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12280 		goto unlock;
12281 	}
12282 
12283 	/*
12284 	 * Handle requests to add or remove an IP interface from a group.
12285 	 */
12286 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12287 		/*
12288 		 * Moves are handled by first removing the interface from
12289 		 * its existing group, and then adding it to another group.
12290 		 * So, fail if it's already in a group.
12291 		 */
12292 		if (IS_UNDER_IPMP(ill)) {
12293 			err = EALREADY;
12294 			goto unlock;
12295 		}
12296 
12297 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12298 		if (grp == NULL) {
12299 			err = ENOENT;
12300 			goto unlock;
12301 		}
12302 
12303 		/*
12304 		 * Check if the phyint and its ills are suitable for
12305 		 * inclusion into the group.
12306 		 */
12307 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12308 			goto unlock;
12309 
12310 		/*
12311 		 * Checks pass; join the group, and enqueue the remaining
12312 		 * illgrp joins for when we've become part of the group xop
12313 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12314 		 * requires an mblk_t to scribble on, and since `mp' will be
12315 		 * freed as part of completing the ioctl, allocate another.
12316 		 */
12317 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12318 			err = ENOMEM;
12319 			goto unlock;
12320 		}
12321 
12322 		/*
12323 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12324 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12325 		 * before ip_join_illgrps() is called back.  See the comments
12326 		 * in ip_sioctl_plink_ipmp() for more.
12327 		 */
12328 		if (phyi->phyint_illv4 != NULL)
12329 			grp->gr_pendv4++;
12330 		if (phyi->phyint_illv6 != NULL)
12331 			grp->gr_pendv6++;
12332 
12333 		rw_exit(&ipst->ips_ipmp_lock);
12334 
12335 		ipmp_phyint_join_grp(phyi, grp);
12336 		ill_refhold(ill);
12337 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12338 		    SWITCH_OP, B_FALSE);
12339 		return (0);
12340 	} else {
12341 		/*
12342 		 * Request to remove the interface from a group.  If the
12343 		 * interface is not in a group, this trivially succeeds.
12344 		 */
12345 		rw_exit(&ipst->ips_ipmp_lock);
12346 		if (IS_UNDER_IPMP(ill))
12347 			ipmp_phyint_leave_grp(phyi);
12348 		return (0);
12349 	}
12350 unlock:
12351 	rw_exit(&ipst->ips_ipmp_lock);
12352 	return (err);
12353 }
12354 
12355 /*
12356  * Process an SIOCGLIFBINDING request.
12357  */
12358 /* ARGSUSED */
12359 int
12360 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12361     ip_ioctl_cmd_t *ipip, void *ifreq)
12362 {
12363 	ill_t		*ill;
12364 	struct lifreq	*lifr = ifreq;
12365 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12366 
12367 	if (!IS_IPMP(ipif->ipif_ill))
12368 		return (EINVAL);
12369 
12370 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12371 	if ((ill = ipif->ipif_bound_ill) == NULL)
12372 		lifr->lifr_binding[0] = '\0';
12373 	else
12374 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12375 	rw_exit(&ipst->ips_ipmp_lock);
12376 	return (0);
12377 }
12378 
12379 /*
12380  * Process an SIOCGLIFGROUPNAME request.
12381  */
12382 /* ARGSUSED */
12383 int
12384 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12385     ip_ioctl_cmd_t *ipip, void *ifreq)
12386 {
12387 	ipmp_grp_t	*grp;
12388 	struct lifreq	*lifr = ifreq;
12389 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12390 
12391 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12392 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12393 		lifr->lifr_groupname[0] = '\0';
12394 	else
12395 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12396 	rw_exit(&ipst->ips_ipmp_lock);
12397 	return (0);
12398 }
12399 
12400 /*
12401  * Process an SIOCGLIFGROUPINFO request.
12402  */
12403 /* ARGSUSED */
12404 int
12405 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12406     ip_ioctl_cmd_t *ipip, void *dummy)
12407 {
12408 	ipmp_grp_t	*grp;
12409 	lifgroupinfo_t	*lifgr;
12410 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12411 
12412 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12413 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12414 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12415 
12416 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12417 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12418 		rw_exit(&ipst->ips_ipmp_lock);
12419 		return (ENOENT);
12420 	}
12421 	ipmp_grp_info(grp, lifgr);
12422 	rw_exit(&ipst->ips_ipmp_lock);
12423 	return (0);
12424 }
12425 
12426 static void
12427 ill_dl_down(ill_t *ill)
12428 {
12429 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12430 
12431 	/*
12432 	 * The ill is down; unbind but stay attached since we're still
12433 	 * associated with a PPA. If we have negotiated DLPI capabilites
12434 	 * with the data link service provider (IDS_OK) then reset them.
12435 	 * The interval between unbinding and rebinding is potentially
12436 	 * unbounded hence we cannot assume things will be the same.
12437 	 * The DLPI capabilities will be probed again when the data link
12438 	 * is brought up.
12439 	 */
12440 	mblk_t	*mp = ill->ill_unbind_mp;
12441 
12442 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12443 
12444 	if (!ill->ill_replumbing) {
12445 		/* Free all ilms for this ill */
12446 		update_conn_ill(ill, ill->ill_ipst);
12447 	} else {
12448 		ill_leave_multicast(ill);
12449 	}
12450 
12451 	ill->ill_unbind_mp = NULL;
12452 	if (mp != NULL) {
12453 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12454 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12455 		    ill->ill_name));
12456 		mutex_enter(&ill->ill_lock);
12457 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12458 		mutex_exit(&ill->ill_lock);
12459 		/*
12460 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12461 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12462 		 * ill_capability_dld_disable disable rightaway. If this is not
12463 		 * an unplumb operation then the disable happens on receipt of
12464 		 * the capab ack via ip_rput_dlpi_writer ->
12465 		 * ill_capability_ack_thr. In both cases the order of
12466 		 * the operations seen by DLD is capability disable followed
12467 		 * by DL_UNBIND. Also the DLD capability disable needs a
12468 		 * cv_wait'able context.
12469 		 */
12470 		if (ill->ill_state_flags & ILL_CONDEMNED)
12471 			ill_capability_dld_disable(ill);
12472 		ill_capability_reset(ill, B_FALSE);
12473 		ill_dlpi_send(ill, mp);
12474 	}
12475 	mutex_enter(&ill->ill_lock);
12476 	ill->ill_dl_up = 0;
12477 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12478 	mutex_exit(&ill->ill_lock);
12479 }
12480 
12481 void
12482 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12483 {
12484 	union DL_primitives *dlp;
12485 	t_uscalar_t prim;
12486 	boolean_t waitack = B_FALSE;
12487 
12488 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12489 
12490 	dlp = (union DL_primitives *)mp->b_rptr;
12491 	prim = dlp->dl_primitive;
12492 
12493 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12494 	    dl_primstr(prim), prim, ill->ill_name));
12495 
12496 	switch (prim) {
12497 	case DL_PHYS_ADDR_REQ:
12498 	{
12499 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12500 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12501 		break;
12502 	}
12503 	case DL_BIND_REQ:
12504 		mutex_enter(&ill->ill_lock);
12505 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12506 		mutex_exit(&ill->ill_lock);
12507 		break;
12508 	}
12509 
12510 	/*
12511 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12512 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12513 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12514 	 */
12515 	mutex_enter(&ill->ill_lock);
12516 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12517 	    (prim == DL_UNBIND_REQ)) {
12518 		ill->ill_dlpi_pending = prim;
12519 		waitack = B_TRUE;
12520 	}
12521 
12522 	mutex_exit(&ill->ill_lock);
12523 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12524 	    char *, dl_primstr(prim), ill_t *, ill);
12525 	putnext(ill->ill_wq, mp);
12526 
12527 	/*
12528 	 * There is no ack for DL_NOTIFY_CONF messages
12529 	 */
12530 	if (waitack && prim == DL_NOTIFY_CONF)
12531 		ill_dlpi_done(ill, prim);
12532 }
12533 
12534 /*
12535  * Helper function for ill_dlpi_send().
12536  */
12537 /* ARGSUSED */
12538 static void
12539 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12540 {
12541 	ill_dlpi_send(q->q_ptr, mp);
12542 }
12543 
12544 /*
12545  * Send a DLPI control message to the driver but make sure there
12546  * is only one outstanding message. Uses ill_dlpi_pending to tell
12547  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12548  * when an ACK or a NAK is received to process the next queued message.
12549  */
12550 void
12551 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12552 {
12553 	mblk_t **mpp;
12554 
12555 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12556 
12557 	/*
12558 	 * To ensure that any DLPI requests for current exclusive operation
12559 	 * are always completely sent before any DLPI messages for other
12560 	 * operations, require writer access before enqueuing.
12561 	 */
12562 	if (!IAM_WRITER_ILL(ill)) {
12563 		ill_refhold(ill);
12564 		/* qwriter_ip() does the ill_refrele() */
12565 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12566 		    NEW_OP, B_TRUE);
12567 		return;
12568 	}
12569 
12570 	mutex_enter(&ill->ill_lock);
12571 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12572 		/* Must queue message. Tail insertion */
12573 		mpp = &ill->ill_dlpi_deferred;
12574 		while (*mpp != NULL)
12575 			mpp = &((*mpp)->b_next);
12576 
12577 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12578 		    "while %s pending\n", ill->ill_name,
12579 		    dl_primstr(ill->ill_dlpi_pending)));
12580 
12581 		*mpp = mp;
12582 		mutex_exit(&ill->ill_lock);
12583 		return;
12584 	}
12585 	mutex_exit(&ill->ill_lock);
12586 	ill_dlpi_dispatch(ill, mp);
12587 }
12588 
12589 void
12590 ill_capability_send(ill_t *ill, mblk_t *mp)
12591 {
12592 	ill->ill_capab_pending_cnt++;
12593 	ill_dlpi_send(ill, mp);
12594 }
12595 
12596 void
12597 ill_capability_done(ill_t *ill)
12598 {
12599 	ASSERT(ill->ill_capab_pending_cnt != 0);
12600 
12601 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12602 
12603 	ill->ill_capab_pending_cnt--;
12604 	if (ill->ill_capab_pending_cnt == 0 &&
12605 	    ill->ill_dlpi_capab_state == IDCS_OK)
12606 		ill_capability_reset_alloc(ill);
12607 }
12608 
12609 /*
12610  * Send all deferred DLPI messages without waiting for their ACKs.
12611  */
12612 void
12613 ill_dlpi_send_deferred(ill_t *ill)
12614 {
12615 	mblk_t *mp, *nextmp;
12616 
12617 	/*
12618 	 * Clear ill_dlpi_pending so that the message is not queued in
12619 	 * ill_dlpi_send().
12620 	 */
12621 	mutex_enter(&ill->ill_lock);
12622 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12623 	mp = ill->ill_dlpi_deferred;
12624 	ill->ill_dlpi_deferred = NULL;
12625 	mutex_exit(&ill->ill_lock);
12626 
12627 	for (; mp != NULL; mp = nextmp) {
12628 		nextmp = mp->b_next;
12629 		mp->b_next = NULL;
12630 		ill_dlpi_send(ill, mp);
12631 	}
12632 }
12633 
12634 /*
12635  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12636  * or M_HANGUP
12637  */
12638 static void
12639 ill_dlpi_clear_deferred(ill_t *ill)
12640 {
12641 	mblk_t	*mp, *nextmp;
12642 
12643 	mutex_enter(&ill->ill_lock);
12644 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12645 	mp = ill->ill_dlpi_deferred;
12646 	ill->ill_dlpi_deferred = NULL;
12647 	mutex_exit(&ill->ill_lock);
12648 
12649 	for (; mp != NULL; mp = nextmp) {
12650 		nextmp = mp->b_next;
12651 		inet_freemsg(mp);
12652 	}
12653 }
12654 
12655 /*
12656  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12657  */
12658 boolean_t
12659 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12660 {
12661 	t_uscalar_t pending;
12662 
12663 	mutex_enter(&ill->ill_lock);
12664 	if (ill->ill_dlpi_pending == prim) {
12665 		mutex_exit(&ill->ill_lock);
12666 		return (B_TRUE);
12667 	}
12668 
12669 	/*
12670 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12671 	 * without waiting, so don't print any warnings in that case.
12672 	 */
12673 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12674 		mutex_exit(&ill->ill_lock);
12675 		return (B_FALSE);
12676 	}
12677 	pending = ill->ill_dlpi_pending;
12678 	mutex_exit(&ill->ill_lock);
12679 
12680 	if (pending == DL_PRIM_INVAL) {
12681 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12682 		    "received unsolicited ack for %s on %s\n",
12683 		    dl_primstr(prim), ill->ill_name);
12684 	} else {
12685 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12686 		    "received unexpected ack for %s on %s (expecting %s)\n",
12687 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12688 	}
12689 	return (B_FALSE);
12690 }
12691 
12692 /*
12693  * Complete the current DLPI operation associated with `prim' on `ill' and
12694  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12695  * operations and the ill's current exclusive IPSQ operation has finished
12696  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12697  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12698  * the comments above ipsq_current_finish() for details.
12699  */
12700 void
12701 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12702 {
12703 	mblk_t *mp;
12704 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12705 	ipxop_t *ipx = ipsq->ipsq_xop;
12706 
12707 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12708 	mutex_enter(&ill->ill_lock);
12709 
12710 	ASSERT(prim != DL_PRIM_INVAL);
12711 	ASSERT(ill->ill_dlpi_pending == prim);
12712 
12713 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12714 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12715 
12716 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12717 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12718 		if (ipx->ipx_current_done) {
12719 			mutex_enter(&ipx->ipx_lock);
12720 			ipx->ipx_current_ipif = NULL;
12721 			mutex_exit(&ipx->ipx_lock);
12722 		}
12723 		cv_signal(&ill->ill_cv);
12724 		mutex_exit(&ill->ill_lock);
12725 		return;
12726 	}
12727 
12728 	ill->ill_dlpi_deferred = mp->b_next;
12729 	mp->b_next = NULL;
12730 	mutex_exit(&ill->ill_lock);
12731 
12732 	ill_dlpi_dispatch(ill, mp);
12733 }
12734 
12735 /*
12736  * Queue a (multicast) DLPI control message to be sent to the driver by
12737  * later calling ill_dlpi_send_queued.
12738  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12739  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12740  * for the same group to race.
12741  * We send DLPI control messages in order using ill_lock.
12742  * For IPMP we should be called on the cast_ill.
12743  */
12744 void
12745 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12746 {
12747 	mblk_t **mpp;
12748 
12749 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12750 
12751 	mutex_enter(&ill->ill_lock);
12752 	/* Must queue message. Tail insertion */
12753 	mpp = &ill->ill_dlpi_deferred;
12754 	while (*mpp != NULL)
12755 		mpp = &((*mpp)->b_next);
12756 
12757 	*mpp = mp;
12758 	mutex_exit(&ill->ill_lock);
12759 }
12760 
12761 /*
12762  * Send the messages that were queued. Make sure there is only
12763  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12764  * when an ACK or a NAK is received to process the next queued message.
12765  * For IPMP we are called on the upper ill, but when send what is queued
12766  * on the cast_ill.
12767  */
12768 void
12769 ill_dlpi_send_queued(ill_t *ill)
12770 {
12771 	mblk_t	*mp;
12772 	union DL_primitives *dlp;
12773 	t_uscalar_t prim;
12774 	ill_t *release_ill = NULL;
12775 
12776 	if (IS_IPMP(ill)) {
12777 		/* On the upper IPMP ill. */
12778 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12779 		if (release_ill == NULL) {
12780 			/* Avoid ever sending anything down to the ipmpstub */
12781 			return;
12782 		}
12783 		ill = release_ill;
12784 	}
12785 	mutex_enter(&ill->ill_lock);
12786 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12787 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12788 			/* Can't send. Somebody else will send it */
12789 			mutex_exit(&ill->ill_lock);
12790 			goto done;
12791 		}
12792 		ill->ill_dlpi_deferred = mp->b_next;
12793 		mp->b_next = NULL;
12794 		if (!ill->ill_dl_up) {
12795 			/*
12796 			 * Nobody there. All multicast addresses will be
12797 			 * re-joined when we get the DL_BIND_ACK bringing the
12798 			 * interface up.
12799 			 */
12800 			freemsg(mp);
12801 			continue;
12802 		}
12803 		dlp = (union DL_primitives *)mp->b_rptr;
12804 		prim = dlp->dl_primitive;
12805 
12806 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12807 		    (prim == DL_UNBIND_REQ)) {
12808 			ill->ill_dlpi_pending = prim;
12809 		}
12810 		mutex_exit(&ill->ill_lock);
12811 
12812 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12813 		    char *, dl_primstr(prim), ill_t *, ill);
12814 		putnext(ill->ill_wq, mp);
12815 		mutex_enter(&ill->ill_lock);
12816 	}
12817 	mutex_exit(&ill->ill_lock);
12818 done:
12819 	if (release_ill != NULL)
12820 		ill_refrele(release_ill);
12821 }
12822 
12823 /*
12824  * Queue an IP (IGMP/MLD) message to be sent by IP from
12825  * ill_mcast_send_queued
12826  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12827  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12828  * group to race.
12829  * We send them in order using ill_lock.
12830  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12831  */
12832 void
12833 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12834 {
12835 	mblk_t **mpp;
12836 	ill_t *release_ill = NULL;
12837 
12838 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12839 
12840 	if (IS_IPMP(ill)) {
12841 		/* On the upper IPMP ill. */
12842 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12843 		if (release_ill == NULL) {
12844 			/* Discard instead of queuing for the ipmp interface */
12845 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12846 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12847 			    mp, ill);
12848 			freemsg(mp);
12849 			return;
12850 		}
12851 		ill = release_ill;
12852 	}
12853 
12854 	mutex_enter(&ill->ill_lock);
12855 	/* Must queue message. Tail insertion */
12856 	mpp = &ill->ill_mcast_deferred;
12857 	while (*mpp != NULL)
12858 		mpp = &((*mpp)->b_next);
12859 
12860 	*mpp = mp;
12861 	mutex_exit(&ill->ill_lock);
12862 	if (release_ill != NULL)
12863 		ill_refrele(release_ill);
12864 }
12865 
12866 /*
12867  * Send the IP packets that were queued by ill_mcast_queue.
12868  * These are IGMP/MLD packets.
12869  *
12870  * For IPMP we are called on the upper ill, but when send what is queued
12871  * on the cast_ill.
12872  *
12873  * Request loopback of the report if we are acting as a multicast
12874  * router, so that the process-level routing demon can hear it.
12875  * This will run multiple times for the same group if there are members
12876  * on the same group for multiple ipif's on the same ill. The
12877  * igmp_input/mld_input code will suppress this due to the loopback thus we
12878  * always loopback membership report.
12879  *
12880  * We also need to make sure that this does not get load balanced
12881  * by IPMP. We do this by passing an ill to ip_output_simple.
12882  */
12883 void
12884 ill_mcast_send_queued(ill_t *ill)
12885 {
12886 	mblk_t	*mp;
12887 	ip_xmit_attr_t ixas;
12888 	ill_t *release_ill = NULL;
12889 
12890 	if (IS_IPMP(ill)) {
12891 		/* On the upper IPMP ill. */
12892 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12893 		if (release_ill == NULL) {
12894 			/*
12895 			 * We should have no messages on the ipmp interface
12896 			 * but no point in trying to send them.
12897 			 */
12898 			return;
12899 		}
12900 		ill = release_ill;
12901 	}
12902 	bzero(&ixas, sizeof (ixas));
12903 	ixas.ixa_zoneid = ALL_ZONES;
12904 	ixas.ixa_cred = kcred;
12905 	ixas.ixa_cpid = NOPID;
12906 	ixas.ixa_tsl = NULL;
12907 	/*
12908 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12909 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12910 	 * That is necessary to handle IGMP/MLD snooping switches.
12911 	 */
12912 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12913 	ixas.ixa_ipst = ill->ill_ipst;
12914 
12915 	mutex_enter(&ill->ill_lock);
12916 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12917 		ill->ill_mcast_deferred = mp->b_next;
12918 		mp->b_next = NULL;
12919 		if (!ill->ill_dl_up) {
12920 			/*
12921 			 * Nobody there. Just drop the ip packets.
12922 			 * IGMP/MLD will resend later, if this is a replumb.
12923 			 */
12924 			freemsg(mp);
12925 			continue;
12926 		}
12927 		mutex_enter(&ill->ill_phyint->phyint_lock);
12928 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12929 			/*
12930 			 * When the ill is getting deactivated, we only want to
12931 			 * send the DLPI messages, so drop IGMP/MLD packets.
12932 			 * DLPI messages are handled by ill_dlpi_send_queued()
12933 			 */
12934 			mutex_exit(&ill->ill_phyint->phyint_lock);
12935 			freemsg(mp);
12936 			continue;
12937 		}
12938 		mutex_exit(&ill->ill_phyint->phyint_lock);
12939 		mutex_exit(&ill->ill_lock);
12940 
12941 		/* Check whether we are sending IPv4 or IPv6. */
12942 		if (ill->ill_isv6) {
12943 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12944 
12945 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12946 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12947 		} else {
12948 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12949 
12950 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12951 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12952 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12953 		}
12954 
12955 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12956 		(void) ip_output_simple(mp, &ixas);
12957 		ixa_cleanup(&ixas);
12958 
12959 		mutex_enter(&ill->ill_lock);
12960 	}
12961 	mutex_exit(&ill->ill_lock);
12962 
12963 done:
12964 	if (release_ill != NULL)
12965 		ill_refrele(release_ill);
12966 }
12967 
12968 /*
12969  * Take down a specific interface, but don't lose any information about it.
12970  * (Always called as writer.)
12971  * This function goes through the down sequence even if the interface is
12972  * already down. There are 2 reasons.
12973  * a. Currently we permit interface routes that depend on down interfaces
12974  *    to be added. This behaviour itself is questionable. However it appears
12975  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12976  *    time. We go thru the cleanup in order to remove these routes.
12977  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12978  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12979  *    down, but we need to cleanup i.e. do ill_dl_down and
12980  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12981  *
12982  * IP-MT notes:
12983  *
12984  * Model of reference to interfaces.
12985  *
12986  * The following members in ipif_t track references to the ipif.
12987  *	int     ipif_refcnt;    Active reference count
12988  *
12989  * The following members in ill_t track references to the ill.
12990  *	int             ill_refcnt;     active refcnt
12991  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12992  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12993  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12994  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12995  *
12996  * Reference to an ipif or ill can be obtained in any of the following ways.
12997  *
12998  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12999  * Pointers to ipif / ill from other data structures viz ire and conn.
13000  * Implicit reference to the ipif / ill by holding a reference to the ire.
13001  *
13002  * The ipif/ill lookup functions return a reference held ipif / ill.
13003  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13004  * This is a purely dynamic reference count associated with threads holding
13005  * references to the ipif / ill. Pointers from other structures do not
13006  * count towards this reference count.
13007  *
13008  * ill_ire_cnt is the number of ire's associated with the
13009  * ill. This is incremented whenever a new ire is created referencing the
13010  * ill. This is done atomically inside ire_add_v[46] where the ire is
13011  * actually added to the ire hash table. The count is decremented in
13012  * ire_inactive where the ire is destroyed.
13013  *
13014  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13015  * This is incremented atomically in
13016  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13017  * table. Similarly it is decremented in ncec_inactive() where the ncec
13018  * is destroyed.
13019  *
13020  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13021  * incremented atomically in nce_add() where the nce is actually added to the
13022  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13023  * is destroyed.
13024  *
13025  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13026  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13027  *
13028  * Flow of ioctls involving interface down/up
13029  *
13030  * The following is the sequence of an attempt to set some critical flags on an
13031  * up interface.
13032  * ip_sioctl_flags
13033  * ipif_down
13034  * wait for ipif to be quiescent
13035  * ipif_down_tail
13036  * ip_sioctl_flags_tail
13037  *
13038  * All set ioctls that involve down/up sequence would have a skeleton similar
13039  * to the above. All the *tail functions are called after the refcounts have
13040  * dropped to the appropriate values.
13041  *
13042  * SIOC ioctls during the IPIF_CHANGING interval.
13043  *
13044  * Threads handling SIOC set ioctls serialize on the squeue, but this
13045  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13046  * steps of internal changes to the state, some of which are visible in
13047  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13048  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13049  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13050  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13051  * the current exclusive operation completes. The IPIF_CHANGING check
13052  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13053  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13054  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13055  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13056  * until we release the ipsq_lock, even though the ill/ipif state flags
13057  * can change after we drop the ill_lock.
13058  */
13059 int
13060 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13061 {
13062 	ill_t		*ill = ipif->ipif_ill;
13063 	conn_t		*connp;
13064 	boolean_t	success;
13065 	boolean_t	ipif_was_up = B_FALSE;
13066 	ip_stack_t	*ipst = ill->ill_ipst;
13067 
13068 	ASSERT(IAM_WRITER_IPIF(ipif));
13069 
13070 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13071 
13072 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13073 	    ill_t *, ill, ipif_t *, ipif);
13074 
13075 	if (ipif->ipif_flags & IPIF_UP) {
13076 		mutex_enter(&ill->ill_lock);
13077 		ipif->ipif_flags &= ~IPIF_UP;
13078 		ASSERT(ill->ill_ipif_up_count > 0);
13079 		--ill->ill_ipif_up_count;
13080 		mutex_exit(&ill->ill_lock);
13081 		ipif_was_up = B_TRUE;
13082 		/* Update status in SCTP's list */
13083 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13084 		ill_nic_event_dispatch(ipif->ipif_ill,
13085 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13086 	}
13087 
13088 	/*
13089 	 * Blow away memberships we established in ipif_multicast_up().
13090 	 */
13091 	ipif_multicast_down(ipif);
13092 
13093 	/*
13094 	 * Remove from the mapping for __sin6_src_id. We insert only
13095 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13096 	 * stored as mapped addresses, we need to check for mapped
13097 	 * INADDR_ANY also.
13098 	 */
13099 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13100 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13101 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13102 		int err;
13103 
13104 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13105 		    ipif->ipif_zoneid, ipst);
13106 		if (err != 0) {
13107 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13108 		}
13109 	}
13110 
13111 	if (ipif_was_up) {
13112 		/* only delete if we'd added ire's before */
13113 		if (ipif->ipif_isv6)
13114 			ipif_delete_ires_v6(ipif);
13115 		else
13116 			ipif_delete_ires_v4(ipif);
13117 	}
13118 
13119 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13120 		/*
13121 		 * Since the interface is now down, it may have just become
13122 		 * inactive.  Note that this needs to be done even for a
13123 		 * lll_logical_down(), or ARP entries will not get correctly
13124 		 * restored when the interface comes back up.
13125 		 */
13126 		if (IS_UNDER_IPMP(ill))
13127 			ipmp_ill_refresh_active(ill);
13128 	}
13129 
13130 	/*
13131 	 * neighbor-discovery or arp entries for this interface. The ipif
13132 	 * has to be quiesced, so we walk all the nce's and delete those
13133 	 * that point at the ipif->ipif_ill. At the same time, we also
13134 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13135 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13136 	 * that for ipif_down_tail()
13137 	 */
13138 	ipif_nce_down(ipif);
13139 
13140 	/*
13141 	 * If this is the last ipif on the ill, we also need to remove
13142 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13143 	 * never succeed.
13144 	 */
13145 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13146 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13147 
13148 	/*
13149 	 * Walk all CONNs that can have a reference on an ire for this
13150 	 * ipif (we actually walk all that now have stale references).
13151 	 */
13152 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13153 
13154 	/*
13155 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13156 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13157 	 * and ill_delete -> ipif_free -> ipif_down
13158 	 */
13159 	if (mp == NULL) {
13160 		ASSERT(q == NULL);
13161 		return (0);
13162 	}
13163 
13164 	if (CONN_Q(q)) {
13165 		connp = Q_TO_CONN(q);
13166 		mutex_enter(&connp->conn_lock);
13167 	} else {
13168 		connp = NULL;
13169 	}
13170 	mutex_enter(&ill->ill_lock);
13171 	/*
13172 	 * Are there any ire's pointing to this ipif that are still active ?
13173 	 * If this is the last ipif going down, are there any ire's pointing
13174 	 * to this ill that are still active ?
13175 	 */
13176 	if (ipif_is_quiescent(ipif)) {
13177 		mutex_exit(&ill->ill_lock);
13178 		if (connp != NULL)
13179 			mutex_exit(&connp->conn_lock);
13180 		return (0);
13181 	}
13182 
13183 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13184 	    ill->ill_name, (void *)ill));
13185 	/*
13186 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13187 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13188 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13189 	 */
13190 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13191 	if (!success) {
13192 		/* The conn is closing. So just return */
13193 		ASSERT(connp != NULL);
13194 		mutex_exit(&ill->ill_lock);
13195 		mutex_exit(&connp->conn_lock);
13196 		return (EINTR);
13197 	}
13198 
13199 	mutex_exit(&ill->ill_lock);
13200 	if (connp != NULL)
13201 		mutex_exit(&connp->conn_lock);
13202 	return (EINPROGRESS);
13203 }
13204 
13205 int
13206 ipif_down_tail(ipif_t *ipif)
13207 {
13208 	ill_t	*ill = ipif->ipif_ill;
13209 	int	err = 0;
13210 
13211 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13212 	    ill_t *, ill, ipif_t *, ipif);
13213 
13214 	/*
13215 	 * Skip any loopback interface (null wq).
13216 	 * If this is the last logical interface on the ill
13217 	 * have ill_dl_down tell the driver we are gone (unbind)
13218 	 * Note that lun 0 can ipif_down even though
13219 	 * there are other logical units that are up.
13220 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13221 	 */
13222 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13223 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13224 	    ill->ill_dl_up) {
13225 		ill_dl_down(ill);
13226 	}
13227 	if (!ipif->ipif_isv6)
13228 		err = ipif_arp_down(ipif);
13229 
13230 	ill->ill_logical_down = 0;
13231 
13232 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13233 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13234 	return (err);
13235 }
13236 
13237 /*
13238  * Bring interface logically down without bringing the physical interface
13239  * down e.g. when the netmask is changed. This avoids long lasting link
13240  * negotiations between an ethernet interface and a certain switches.
13241  */
13242 static int
13243 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13244 {
13245 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13246 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13247 
13248 	/*
13249 	 * The ill_logical_down flag is a transient flag. It is set here
13250 	 * and is cleared once the down has completed in ipif_down_tail.
13251 	 * This flag does not indicate whether the ill stream is in the
13252 	 * DL_BOUND state with the driver. Instead this flag is used by
13253 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13254 	 * the driver. The state of the ill stream i.e. whether it is
13255 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13256 	 */
13257 	ipif->ipif_ill->ill_logical_down = 1;
13258 	return (ipif_down(ipif, q, mp));
13259 }
13260 
13261 /*
13262  * Initiate deallocate of an IPIF. Always called as writer. Called by
13263  * ill_delete or ip_sioctl_removeif.
13264  */
13265 static void
13266 ipif_free(ipif_t *ipif)
13267 {
13268 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13269 
13270 	ASSERT(IAM_WRITER_IPIF(ipif));
13271 
13272 	if (ipif->ipif_recovery_id != 0)
13273 		(void) untimeout(ipif->ipif_recovery_id);
13274 	ipif->ipif_recovery_id = 0;
13275 
13276 	/*
13277 	 * Take down the interface. We can be called either from ill_delete
13278 	 * or from ip_sioctl_removeif.
13279 	 */
13280 	(void) ipif_down(ipif, NULL, NULL);
13281 
13282 	/*
13283 	 * Now that the interface is down, there's no chance it can still
13284 	 * become a duplicate.  Cancel any timer that may have been set while
13285 	 * tearing down.
13286 	 */
13287 	if (ipif->ipif_recovery_id != 0)
13288 		(void) untimeout(ipif->ipif_recovery_id);
13289 	ipif->ipif_recovery_id = 0;
13290 
13291 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13292 	/* Remove pointers to this ill in the multicast routing tables */
13293 	reset_mrt_vif_ipif(ipif);
13294 	/* If necessary, clear the cached source ipif rotor. */
13295 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13296 		ipif->ipif_ill->ill_src_ipif = NULL;
13297 	rw_exit(&ipst->ips_ill_g_lock);
13298 }
13299 
13300 static void
13301 ipif_free_tail(ipif_t *ipif)
13302 {
13303 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13304 
13305 	/*
13306 	 * Need to hold both ill_g_lock and ill_lock while
13307 	 * inserting or removing an ipif from the linked list
13308 	 * of ipifs hanging off the ill.
13309 	 */
13310 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13311 
13312 #ifdef DEBUG
13313 	ipif_trace_cleanup(ipif);
13314 #endif
13315 
13316 	/* Ask SCTP to take it out of it list */
13317 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13318 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13319 
13320 	/* Get it out of the ILL interface list. */
13321 	ipif_remove(ipif);
13322 	rw_exit(&ipst->ips_ill_g_lock);
13323 
13324 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13325 	ASSERT(ipif->ipif_recovery_id == 0);
13326 	ASSERT(ipif->ipif_ire_local == NULL);
13327 	ASSERT(ipif->ipif_ire_if == NULL);
13328 
13329 	/* Free the memory. */
13330 	mi_free(ipif);
13331 }
13332 
13333 /*
13334  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13335  * is zero.
13336  */
13337 void
13338 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13339 {
13340 	char	lbuf[LIFNAMSIZ];
13341 	char	*name;
13342 	size_t	name_len;
13343 
13344 	buf[0] = '\0';
13345 	name = ipif->ipif_ill->ill_name;
13346 	name_len = ipif->ipif_ill->ill_name_length;
13347 	if (ipif->ipif_id != 0) {
13348 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13349 		    ipif->ipif_id);
13350 		name = lbuf;
13351 		name_len = mi_strlen(name) + 1;
13352 	}
13353 	len -= 1;
13354 	buf[len] = '\0';
13355 	len = MIN(len, name_len);
13356 	bcopy(name, buf, len);
13357 }
13358 
13359 /*
13360  * Sets `buf' to an ill name.
13361  */
13362 void
13363 ill_get_name(const ill_t *ill, char *buf, int len)
13364 {
13365 	char	*name;
13366 	size_t	name_len;
13367 
13368 	name = ill->ill_name;
13369 	name_len = ill->ill_name_length;
13370 	len -= 1;
13371 	buf[len] = '\0';
13372 	len = MIN(len, name_len);
13373 	bcopy(name, buf, len);
13374 }
13375 
13376 /*
13377  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13378  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13379  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13380  * (May be called as writer.)
13381  */
13382 static ipif_t *
13383 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13384     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13385 {
13386 	char	*cp;
13387 	char	*endp;
13388 	long	id;
13389 	ill_t	*ill;
13390 	ipif_t	*ipif;
13391 	uint_t	ire_type;
13392 	boolean_t did_alloc = B_FALSE;
13393 
13394 	/*
13395 	 * If the caller wants to us to create the ipif, make sure we have a
13396 	 * valid zoneid
13397 	 */
13398 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13399 
13400 	if (namelen == 0) {
13401 		return (NULL);
13402 	}
13403 
13404 	*exists = B_FALSE;
13405 	/* Look for a colon in the name. */
13406 	endp = &name[namelen];
13407 	for (cp = endp; --cp > name; ) {
13408 		if (*cp == IPIF_SEPARATOR_CHAR)
13409 			break;
13410 	}
13411 
13412 	if (*cp == IPIF_SEPARATOR_CHAR) {
13413 		/*
13414 		 * Reject any non-decimal aliases for logical
13415 		 * interfaces. Aliases with leading zeroes
13416 		 * are also rejected as they introduce ambiguity
13417 		 * in the naming of the interfaces.
13418 		 * In order to confirm with existing semantics,
13419 		 * and to not break any programs/script relying
13420 		 * on that behaviour, if<0>:0 is considered to be
13421 		 * a valid interface.
13422 		 *
13423 		 * If alias has two or more digits and the first
13424 		 * is zero, fail.
13425 		 */
13426 		if (&cp[2] < endp && cp[1] == '0') {
13427 			return (NULL);
13428 		}
13429 	}
13430 
13431 	if (cp <= name) {
13432 		cp = endp;
13433 	} else {
13434 		*cp = '\0';
13435 	}
13436 
13437 	/*
13438 	 * Look up the ILL, based on the portion of the name
13439 	 * before the slash. ill_lookup_on_name returns a held ill.
13440 	 * Temporary to check whether ill exists already. If so
13441 	 * ill_lookup_on_name will clear it.
13442 	 */
13443 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13444 	    &did_alloc, ipst);
13445 	if (cp != endp)
13446 		*cp = IPIF_SEPARATOR_CHAR;
13447 	if (ill == NULL)
13448 		return (NULL);
13449 
13450 	/* Establish the unit number in the name. */
13451 	id = 0;
13452 	if (cp < endp && *endp == '\0') {
13453 		/* If there was a colon, the unit number follows. */
13454 		cp++;
13455 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13456 			ill_refrele(ill);
13457 			return (NULL);
13458 		}
13459 	}
13460 
13461 	mutex_enter(&ill->ill_lock);
13462 	/* Now see if there is an IPIF with this unit number. */
13463 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13464 		if (ipif->ipif_id == id) {
13465 			if (zoneid != ALL_ZONES &&
13466 			    zoneid != ipif->ipif_zoneid &&
13467 			    ipif->ipif_zoneid != ALL_ZONES) {
13468 				mutex_exit(&ill->ill_lock);
13469 				ill_refrele(ill);
13470 				return (NULL);
13471 			}
13472 			if (IPIF_CAN_LOOKUP(ipif)) {
13473 				ipif_refhold_locked(ipif);
13474 				mutex_exit(&ill->ill_lock);
13475 				if (!did_alloc)
13476 					*exists = B_TRUE;
13477 				/*
13478 				 * Drop locks before calling ill_refrele
13479 				 * since it can potentially call into
13480 				 * ipif_ill_refrele_tail which can end up
13481 				 * in trying to acquire any lock.
13482 				 */
13483 				ill_refrele(ill);
13484 				return (ipif);
13485 			}
13486 		}
13487 	}
13488 
13489 	if (!do_alloc) {
13490 		mutex_exit(&ill->ill_lock);
13491 		ill_refrele(ill);
13492 		return (NULL);
13493 	}
13494 
13495 	/*
13496 	 * If none found, atomically allocate and return a new one.
13497 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13498 	 * to support "receive only" use of lo0:1 etc. as is still done
13499 	 * below as an initial guess.
13500 	 * However, this is now likely to be overriden later in ipif_up_done()
13501 	 * when we know for sure what address has been configured on the
13502 	 * interface, since we might have more than one loopback interface
13503 	 * with a loopback address, e.g. in the case of zones, and all the
13504 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13505 	 */
13506 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13507 		ire_type = IRE_LOOPBACK;
13508 	else
13509 		ire_type = IRE_LOCAL;
13510 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13511 	if (ipif != NULL)
13512 		ipif_refhold_locked(ipif);
13513 	mutex_exit(&ill->ill_lock);
13514 	ill_refrele(ill);
13515 	return (ipif);
13516 }
13517 
13518 /*
13519  * This routine is called whenever a new address comes up on an ipif.  If
13520  * we are configured to respond to address mask requests, then we are supposed
13521  * to broadcast an address mask reply at this time.  This routine is also
13522  * called if we are already up, but a netmask change is made.  This is legal
13523  * but might not make the system manager very popular.	(May be called
13524  * as writer.)
13525  */
13526 void
13527 ipif_mask_reply(ipif_t *ipif)
13528 {
13529 	icmph_t	*icmph;
13530 	ipha_t	*ipha;
13531 	mblk_t	*mp;
13532 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13533 	ip_xmit_attr_t ixas;
13534 
13535 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13536 
13537 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13538 		return;
13539 
13540 	/* ICMP mask reply is IPv4 only */
13541 	ASSERT(!ipif->ipif_isv6);
13542 	/* ICMP mask reply is not for a loopback interface */
13543 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13544 
13545 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13546 		return;
13547 
13548 	mp = allocb(REPLY_LEN, BPRI_HI);
13549 	if (mp == NULL)
13550 		return;
13551 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13552 
13553 	ipha = (ipha_t *)mp->b_rptr;
13554 	bzero(ipha, REPLY_LEN);
13555 	*ipha = icmp_ipha;
13556 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13557 	ipha->ipha_src = ipif->ipif_lcl_addr;
13558 	ipha->ipha_dst = ipif->ipif_brd_addr;
13559 	ipha->ipha_length = htons(REPLY_LEN);
13560 	ipha->ipha_ident = 0;
13561 
13562 	icmph = (icmph_t *)&ipha[1];
13563 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13564 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13565 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13566 
13567 	bzero(&ixas, sizeof (ixas));
13568 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13569 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13570 	ixas.ixa_zoneid = ALL_ZONES;
13571 	ixas.ixa_ifindex = 0;
13572 	ixas.ixa_ipst = ipst;
13573 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13574 	(void) ip_output_simple(mp, &ixas);
13575 	ixa_cleanup(&ixas);
13576 #undef	REPLY_LEN
13577 }
13578 
13579 /*
13580  * Join the ipif specific multicast groups.
13581  * Must be called after a mapping has been set up in the resolver.  (Always
13582  * called as writer.)
13583  */
13584 void
13585 ipif_multicast_up(ipif_t *ipif)
13586 {
13587 	int err;
13588 	ill_t *ill;
13589 	ilm_t *ilm;
13590 
13591 	ASSERT(IAM_WRITER_IPIF(ipif));
13592 
13593 	ill = ipif->ipif_ill;
13594 
13595 	ip1dbg(("ipif_multicast_up\n"));
13596 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13597 	    ipif->ipif_allhosts_ilm != NULL)
13598 		return;
13599 
13600 	if (ipif->ipif_isv6) {
13601 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13602 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13603 
13604 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13605 
13606 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13607 			return;
13608 
13609 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13610 
13611 		/*
13612 		 * Join the all hosts multicast address.  We skip this for
13613 		 * underlying IPMP interfaces since they should be invisible.
13614 		 */
13615 		if (!IS_UNDER_IPMP(ill)) {
13616 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13617 			    &err);
13618 			if (ilm == NULL) {
13619 				ASSERT(err != 0);
13620 				ip0dbg(("ipif_multicast_up: "
13621 				    "all_hosts_mcast failed %d\n", err));
13622 				return;
13623 			}
13624 			ipif->ipif_allhosts_ilm = ilm;
13625 		}
13626 
13627 		/*
13628 		 * Enable multicast for the solicited node multicast address.
13629 		 * If IPMP we need to put the membership on the upper ill.
13630 		 */
13631 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13632 			ill_t *mcast_ill = NULL;
13633 			boolean_t need_refrele;
13634 
13635 			if (IS_UNDER_IPMP(ill) &&
13636 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13637 				need_refrele = B_TRUE;
13638 			} else {
13639 				mcast_ill = ill;
13640 				need_refrele = B_FALSE;
13641 			}
13642 
13643 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13644 			    ipif->ipif_zoneid, &err);
13645 			if (need_refrele)
13646 				ill_refrele(mcast_ill);
13647 
13648 			if (ilm == NULL) {
13649 				ASSERT(err != 0);
13650 				ip0dbg(("ipif_multicast_up: solicited MC"
13651 				    " failed %d\n", err));
13652 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13653 					ipif->ipif_allhosts_ilm = NULL;
13654 					(void) ip_delmulti(ilm);
13655 				}
13656 				return;
13657 			}
13658 			ipif->ipif_solmulti_ilm = ilm;
13659 		}
13660 	} else {
13661 		in6_addr_t v6group;
13662 
13663 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13664 			return;
13665 
13666 		/* Join the all hosts multicast address */
13667 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13668 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13669 
13670 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13671 		if (ilm == NULL) {
13672 			ASSERT(err != 0);
13673 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13674 			return;
13675 		}
13676 		ipif->ipif_allhosts_ilm = ilm;
13677 	}
13678 }
13679 
13680 /*
13681  * Blow away any multicast groups that we joined in ipif_multicast_up().
13682  * (ilms from explicit memberships are handled in conn_update_ill.)
13683  */
13684 void
13685 ipif_multicast_down(ipif_t *ipif)
13686 {
13687 	ASSERT(IAM_WRITER_IPIF(ipif));
13688 
13689 	ip1dbg(("ipif_multicast_down\n"));
13690 
13691 	if (ipif->ipif_allhosts_ilm != NULL) {
13692 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13693 		ipif->ipif_allhosts_ilm = NULL;
13694 	}
13695 	if (ipif->ipif_solmulti_ilm != NULL) {
13696 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13697 		ipif->ipif_solmulti_ilm = NULL;
13698 	}
13699 }
13700 
13701 /*
13702  * Used when an interface comes up to recreate any extra routes on this
13703  * interface.
13704  */
13705 int
13706 ill_recover_saved_ire(ill_t *ill)
13707 {
13708 	mblk_t		*mp;
13709 	ip_stack_t	*ipst = ill->ill_ipst;
13710 
13711 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13712 
13713 	mutex_enter(&ill->ill_saved_ire_lock);
13714 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13715 		ire_t		*ire, *nire;
13716 		ifrt_t		*ifrt;
13717 
13718 		ifrt = (ifrt_t *)mp->b_rptr;
13719 		/*
13720 		 * Create a copy of the IRE with the saved address and netmask.
13721 		 */
13722 		if (ill->ill_isv6) {
13723 			ire = ire_create_v6(
13724 			    &ifrt->ifrt_v6addr,
13725 			    &ifrt->ifrt_v6mask,
13726 			    &ifrt->ifrt_v6gateway_addr,
13727 			    ifrt->ifrt_type,
13728 			    ill,
13729 			    ifrt->ifrt_zoneid,
13730 			    ifrt->ifrt_flags,
13731 			    NULL,
13732 			    ipst);
13733 		} else {
13734 			ire = ire_create(
13735 			    (uint8_t *)&ifrt->ifrt_addr,
13736 			    (uint8_t *)&ifrt->ifrt_mask,
13737 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13738 			    ifrt->ifrt_type,
13739 			    ill,
13740 			    ifrt->ifrt_zoneid,
13741 			    ifrt->ifrt_flags,
13742 			    NULL,
13743 			    ipst);
13744 		}
13745 		if (ire == NULL) {
13746 			mutex_exit(&ill->ill_saved_ire_lock);
13747 			return (ENOMEM);
13748 		}
13749 
13750 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13751 			if (ill->ill_isv6) {
13752 				ire->ire_setsrc_addr_v6 =
13753 				    ifrt->ifrt_v6setsrc_addr;
13754 			} else {
13755 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13756 			}
13757 		}
13758 
13759 		/*
13760 		 * Some software (for example, GateD and Sun Cluster) attempts
13761 		 * to create (what amount to) IRE_PREFIX routes with the
13762 		 * loopback address as the gateway.  This is primarily done to
13763 		 * set up prefixes with the RTF_REJECT flag set (for example,
13764 		 * when generating aggregate routes.)
13765 		 *
13766 		 * If the IRE type (as defined by ill->ill_net_type) is
13767 		 * IRE_LOOPBACK, then we map the request into a
13768 		 * IRE_IF_NORESOLVER.
13769 		 */
13770 		if (ill->ill_net_type == IRE_LOOPBACK)
13771 			ire->ire_type = IRE_IF_NORESOLVER;
13772 
13773 		/*
13774 		 * ire held by ire_add, will be refreled' towards the
13775 		 * the end of ipif_up_done
13776 		 */
13777 		nire = ire_add(ire);
13778 		/*
13779 		 * Check if it was a duplicate entry. This handles
13780 		 * the case of two racing route adds for the same route
13781 		 */
13782 		if (nire == NULL) {
13783 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13784 		} else if (nire != ire) {
13785 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13786 			    (void *)nire));
13787 			ire_delete(nire);
13788 		} else {
13789 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13790 			    (void *)nire));
13791 		}
13792 		if (nire != NULL)
13793 			ire_refrele(nire);
13794 	}
13795 	mutex_exit(&ill->ill_saved_ire_lock);
13796 	return (0);
13797 }
13798 
13799 /*
13800  * Used to set the netmask and broadcast address to default values when the
13801  * interface is brought up.  (Always called as writer.)
13802  */
13803 static void
13804 ipif_set_default(ipif_t *ipif)
13805 {
13806 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13807 
13808 	if (!ipif->ipif_isv6) {
13809 		/*
13810 		 * Interface holds an IPv4 address. Default
13811 		 * mask is the natural netmask.
13812 		 */
13813 		if (!ipif->ipif_net_mask) {
13814 			ipaddr_t	v4mask;
13815 
13816 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13817 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13818 		}
13819 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13820 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13821 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13822 		} else {
13823 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13824 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13825 		}
13826 		/*
13827 		 * NOTE: SunOS 4.X does this even if the broadcast address
13828 		 * has been already set thus we do the same here.
13829 		 */
13830 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13831 			ipaddr_t	v4addr;
13832 
13833 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13834 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13835 		}
13836 	} else {
13837 		/*
13838 		 * Interface holds an IPv6-only address.  Default
13839 		 * mask is all-ones.
13840 		 */
13841 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13842 			ipif->ipif_v6net_mask = ipv6_all_ones;
13843 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13844 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13845 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13846 		} else {
13847 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13848 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13849 		}
13850 	}
13851 }
13852 
13853 /*
13854  * Return 0 if this address can be used as local address without causing
13855  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13856  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13857  * Note that the same IPv6 link-local address is allowed as long as the ills
13858  * are not on the same link.
13859  */
13860 int
13861 ip_addr_availability_check(ipif_t *new_ipif)
13862 {
13863 	in6_addr_t our_v6addr;
13864 	ill_t *ill;
13865 	ipif_t *ipif;
13866 	ill_walk_context_t ctx;
13867 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13868 
13869 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13870 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13871 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13872 
13873 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13874 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13875 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13876 		return (0);
13877 
13878 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13879 
13880 	if (new_ipif->ipif_isv6)
13881 		ill = ILL_START_WALK_V6(&ctx, ipst);
13882 	else
13883 		ill = ILL_START_WALK_V4(&ctx, ipst);
13884 
13885 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13886 		for (ipif = ill->ill_ipif; ipif != NULL;
13887 		    ipif = ipif->ipif_next) {
13888 			if ((ipif == new_ipif) ||
13889 			    !(ipif->ipif_flags & IPIF_UP) ||
13890 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13891 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13892 			    &our_v6addr))
13893 				continue;
13894 
13895 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13896 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13897 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13898 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13899 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13900 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13901 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13902 				continue;
13903 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13904 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13905 				continue;
13906 			else if (new_ipif->ipif_ill == ill)
13907 				return (EADDRINUSE);
13908 			else
13909 				return (EADDRNOTAVAIL);
13910 		}
13911 	}
13912 
13913 	return (0);
13914 }
13915 
13916 /*
13917  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13918  * IREs for the ipif.
13919  * When the routine returns EINPROGRESS then mp has been consumed and
13920  * the ioctl will be acked from ip_rput_dlpi.
13921  */
13922 int
13923 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13924 {
13925 	ill_t		*ill = ipif->ipif_ill;
13926 	boolean_t 	isv6 = ipif->ipif_isv6;
13927 	int		err = 0;
13928 	boolean_t	success;
13929 	uint_t		ipif_orig_id;
13930 	ip_stack_t	*ipst = ill->ill_ipst;
13931 
13932 	ASSERT(IAM_WRITER_IPIF(ipif));
13933 
13934 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13935 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13936 	    ill_t *, ill, ipif_t *, ipif);
13937 
13938 	/* Shouldn't get here if it is already up. */
13939 	if (ipif->ipif_flags & IPIF_UP)
13940 		return (EALREADY);
13941 
13942 	/*
13943 	 * If this is a request to bring up a data address on an interface
13944 	 * under IPMP, then move the address to its IPMP meta-interface and
13945 	 * try to bring it up.  One complication is that the zeroth ipif for
13946 	 * an ill is special, in that every ill always has one, and that code
13947 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13948 	 */
13949 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13950 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13951 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13952 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13953 
13954 		/*
13955 		 * The ipif being brought up should be quiesced.  If it's not,
13956 		 * something has gone amiss and we need to bail out.  (If it's
13957 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13958 		 */
13959 		mutex_enter(&ill->ill_lock);
13960 		if (!ipif_is_quiescent(ipif)) {
13961 			mutex_exit(&ill->ill_lock);
13962 			return (EINVAL);
13963 		}
13964 		mutex_exit(&ill->ill_lock);
13965 
13966 		/*
13967 		 * If we're going to need to allocate ipifs, do it prior
13968 		 * to starting the move (and grabbing locks).
13969 		 */
13970 		if (ipif->ipif_id == 0) {
13971 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13972 			    B_FALSE, &err)) == NULL) {
13973 				return (err);
13974 			}
13975 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13976 			    B_FALSE, &err)) == NULL) {
13977 				mi_free(moveipif);
13978 				return (err);
13979 			}
13980 		}
13981 
13982 		/*
13983 		 * Grab or transfer the ipif to move.  During the move, keep
13984 		 * ill_g_lock held to prevent any ill walker threads from
13985 		 * seeing things in an inconsistent state.
13986 		 */
13987 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13988 		if (ipif->ipif_id != 0) {
13989 			ipif_remove(ipif);
13990 		} else {
13991 			ipif_transfer(ipif, moveipif, stubipif);
13992 			ipif = moveipif;
13993 		}
13994 
13995 		/*
13996 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13997 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13998 		 * replace that one.  Otherwise, pick the next available slot.
13999 		 */
14000 		ipif->ipif_ill = ipmp_ill;
14001 		ipif_orig_id = ipif->ipif_id;
14002 
14003 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14004 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14005 			ipif = ipmp_ill->ill_ipif;
14006 		} else {
14007 			ipif->ipif_id = -1;
14008 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14009 				/*
14010 				 * No more available ipif_id's -- put it back
14011 				 * on the original ill and fail the operation.
14012 				 * Since we're writer on the ill, we can be
14013 				 * sure our old slot is still available.
14014 				 */
14015 				ipif->ipif_id = ipif_orig_id;
14016 				ipif->ipif_ill = ill;
14017 				if (ipif_orig_id == 0) {
14018 					ipif_transfer(ipif, ill->ill_ipif,
14019 					    NULL);
14020 				} else {
14021 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14022 				}
14023 				rw_exit(&ipst->ips_ill_g_lock);
14024 				return (err);
14025 			}
14026 		}
14027 		rw_exit(&ipst->ips_ill_g_lock);
14028 
14029 		/*
14030 		 * Tell SCTP that the ipif has moved.  Note that even if we
14031 		 * had to allocate a new ipif, the original sequence id was
14032 		 * preserved and therefore SCTP won't know.
14033 		 */
14034 		sctp_move_ipif(ipif, ill, ipmp_ill);
14035 
14036 		/*
14037 		 * If the ipif being brought up was on slot zero, then we
14038 		 * first need to bring up the placeholder we stuck there.  In
14039 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14040 		 * call to ipif_up() itself, if we successfully bring up the
14041 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14042 		 */
14043 		if (ipif_orig_id == 0) {
14044 			ASSERT(ill->ill_move_ipif == NULL);
14045 			ill->ill_move_ipif = ipif;
14046 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14047 				ASSERT(ill->ill_move_ipif == NULL);
14048 			if (err != EINPROGRESS)
14049 				ill->ill_move_ipif = NULL;
14050 			return (err);
14051 		}
14052 
14053 		/*
14054 		 * Bring it up on the IPMP ill.
14055 		 */
14056 		return (ipif_up(ipif, q, mp));
14057 	}
14058 
14059 	/* Skip arp/ndp for any loopback interface. */
14060 	if (ill->ill_wq != NULL) {
14061 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14062 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14063 
14064 		if (!ill->ill_dl_up) {
14065 			/*
14066 			 * ill_dl_up is not yet set. i.e. we are yet to
14067 			 * DL_BIND with the driver and this is the first
14068 			 * logical interface on the ill to become "up".
14069 			 * Tell the driver to get going (via DL_BIND_REQ).
14070 			 * Note that changing "significant" IFF_ flags
14071 			 * address/netmask etc cause a down/up dance, but
14072 			 * does not cause an unbind (DL_UNBIND) with the driver
14073 			 */
14074 			return (ill_dl_up(ill, ipif, mp, q));
14075 		}
14076 
14077 		/*
14078 		 * ipif_resolver_up may end up needeing to bind/attach
14079 		 * the ARP stream, which in turn necessitates a
14080 		 * DLPI message exchange with the driver. ioctls are
14081 		 * serialized and so we cannot send more than one
14082 		 * interface up message at a time. If ipif_resolver_up
14083 		 * does need to wait for the DLPI handshake for the ARP stream,
14084 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14085 		 */
14086 
14087 		ASSERT(connp != NULL || !CONN_Q(q));
14088 		if (connp != NULL)
14089 			mutex_enter(&connp->conn_lock);
14090 		mutex_enter(&ill->ill_lock);
14091 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14092 		mutex_exit(&ill->ill_lock);
14093 		if (connp != NULL)
14094 			mutex_exit(&connp->conn_lock);
14095 		if (!success)
14096 			return (EINTR);
14097 
14098 		/*
14099 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14100 		 * complete when ipif_ndp_up returns.
14101 		 */
14102 		err = ipif_resolver_up(ipif, Res_act_initial);
14103 		if (err == EINPROGRESS) {
14104 			/* We will complete it in arp_bringup_done() */
14105 			return (err);
14106 		}
14107 
14108 		if (isv6 && err == 0)
14109 			err = ipif_ndp_up(ipif, B_TRUE);
14110 
14111 		ASSERT(err != EINPROGRESS);
14112 		mp = ipsq_pending_mp_get(ipsq, &connp);
14113 		ASSERT(mp != NULL);
14114 		if (err != 0)
14115 			return (err);
14116 	} else {
14117 		/*
14118 		 * Interfaces without underlying hardware don't do duplicate
14119 		 * address detection.
14120 		 */
14121 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14122 		ipif->ipif_addr_ready = 1;
14123 		err = ill_add_ires(ill);
14124 		/* allocation failure? */
14125 		if (err != 0)
14126 			return (err);
14127 	}
14128 
14129 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14130 	if (err == 0 && ill->ill_move_ipif != NULL) {
14131 		ipif = ill->ill_move_ipif;
14132 		ill->ill_move_ipif = NULL;
14133 		return (ipif_up(ipif, q, mp));
14134 	}
14135 	return (err);
14136 }
14137 
14138 /*
14139  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14140  * The identical set of IREs need to be removed in ill_delete_ires().
14141  */
14142 int
14143 ill_add_ires(ill_t *ill)
14144 {
14145 	ire_t	*ire;
14146 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14147 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14148 
14149 	if (ill->ill_ire_multicast != NULL)
14150 		return (0);
14151 
14152 	/*
14153 	 * provide some dummy ire_addr for creating the ire.
14154 	 */
14155 	if (ill->ill_isv6) {
14156 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14157 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14158 	} else {
14159 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14160 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14161 	}
14162 	if (ire == NULL)
14163 		return (ENOMEM);
14164 
14165 	ill->ill_ire_multicast = ire;
14166 	return (0);
14167 }
14168 
14169 void
14170 ill_delete_ires(ill_t *ill)
14171 {
14172 	if (ill->ill_ire_multicast != NULL) {
14173 		/*
14174 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14175 		 * which was taken without any th_tracing enabled.
14176 		 * We also mark it as condemned (note that it was never added)
14177 		 * so that caching conn's can move off of it.
14178 		 */
14179 		ire_make_condemned(ill->ill_ire_multicast);
14180 		ire_refrele_notr(ill->ill_ire_multicast);
14181 		ill->ill_ire_multicast = NULL;
14182 	}
14183 }
14184 
14185 /*
14186  * Perform a bind for the physical device.
14187  * When the routine returns EINPROGRESS then mp has been consumed and
14188  * the ioctl will be acked from ip_rput_dlpi.
14189  * Allocate an unbind message and save it until ipif_down.
14190  */
14191 static int
14192 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14193 {
14194 	mblk_t	*bind_mp = NULL;
14195 	mblk_t	*unbind_mp = NULL;
14196 	conn_t	*connp;
14197 	boolean_t success;
14198 	int	err;
14199 
14200 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14201 
14202 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14203 	ASSERT(IAM_WRITER_ILL(ill));
14204 	ASSERT(mp != NULL);
14205 
14206 	/*
14207 	 * Make sure we have an IRE_MULTICAST in case we immediately
14208 	 * start receiving packets.
14209 	 */
14210 	err = ill_add_ires(ill);
14211 	if (err != 0)
14212 		goto bad;
14213 
14214 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14215 	    DL_BIND_REQ);
14216 	if (bind_mp == NULL)
14217 		goto bad;
14218 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14219 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14220 
14221 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14222 	if (unbind_mp == NULL)
14223 		goto bad;
14224 
14225 	/*
14226 	 * Record state needed to complete this operation when the
14227 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14228 	 */
14229 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14230 	ASSERT(connp != NULL || !CONN_Q(q));
14231 	GRAB_CONN_LOCK(q);
14232 	mutex_enter(&ipif->ipif_ill->ill_lock);
14233 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14234 	mutex_exit(&ipif->ipif_ill->ill_lock);
14235 	RELEASE_CONN_LOCK(q);
14236 	if (!success)
14237 		goto bad;
14238 
14239 	/*
14240 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14241 	 * the interface goes down.
14242 	 */
14243 	ASSERT(ill->ill_unbind_mp == NULL);
14244 	ill->ill_unbind_mp = unbind_mp;
14245 
14246 	ill_dlpi_send(ill, bind_mp);
14247 	/* Send down link-layer capabilities probe if not already done. */
14248 	ill_capability_probe(ill);
14249 
14250 	/*
14251 	 * Sysid used to rely on the fact that netboots set domainname
14252 	 * and the like. Now that miniroot boots aren't strictly netboots
14253 	 * and miniroot network configuration is driven from userland
14254 	 * these things still need to be set. This situation can be detected
14255 	 * by comparing the interface being configured here to the one
14256 	 * dhcifname was set to reference by the boot loader. Once sysid is
14257 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14258 	 */
14259 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14260 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14261 	    (strlen(srpc_domain) == 0)) {
14262 		if (dhcpinit() != 0)
14263 			cmn_err(CE_WARN, "no cached dhcp response");
14264 	}
14265 
14266 	/*
14267 	 * This operation will complete in ip_rput_dlpi with either
14268 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14269 	 */
14270 	return (EINPROGRESS);
14271 bad:
14272 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14273 
14274 	freemsg(bind_mp);
14275 	freemsg(unbind_mp);
14276 	return (ENOMEM);
14277 }
14278 
14279 /* Add room for tcp+ip headers */
14280 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14281 
14282 /*
14283  * DLPI and ARP is up.
14284  * Create all the IREs associated with an interface. Bring up multicast.
14285  * Set the interface flag and finish other initialization
14286  * that potentially had to be deferred to after DL_BIND_ACK.
14287  */
14288 int
14289 ipif_up_done(ipif_t *ipif)
14290 {
14291 	ill_t		*ill = ipif->ipif_ill;
14292 	int		err = 0;
14293 	boolean_t	loopback = B_FALSE;
14294 	boolean_t	update_src_selection = B_TRUE;
14295 	ipif_t		*tmp_ipif;
14296 
14297 	ip1dbg(("ipif_up_done(%s:%u)\n",
14298 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14299 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14300 	    ill_t *, ill, ipif_t *, ipif);
14301 
14302 	/* Check if this is a loopback interface */
14303 	if (ipif->ipif_ill->ill_wq == NULL)
14304 		loopback = B_TRUE;
14305 
14306 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14307 
14308 	/*
14309 	 * If all other interfaces for this ill are down or DEPRECATED,
14310 	 * or otherwise unsuitable for source address selection,
14311 	 * reset the src generation numbers to make sure source
14312 	 * address selection gets to take this new ipif into account.
14313 	 * No need to hold ill_lock while traversing the ipif list since
14314 	 * we are writer
14315 	 */
14316 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14317 	    tmp_ipif = tmp_ipif->ipif_next) {
14318 		if (((tmp_ipif->ipif_flags &
14319 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14320 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14321 		    (tmp_ipif == ipif))
14322 			continue;
14323 		/* first useable pre-existing interface */
14324 		update_src_selection = B_FALSE;
14325 		break;
14326 	}
14327 	if (update_src_selection)
14328 		ip_update_source_selection(ill->ill_ipst);
14329 
14330 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14331 		nce_t *loop_nce = NULL;
14332 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14333 
14334 		/*
14335 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14336 		 * ipif_lookup_on_name(), but in the case of zones we can have
14337 		 * several loopback addresses on lo0. So all the interfaces with
14338 		 * loopback addresses need to be marked IRE_LOOPBACK.
14339 		 */
14340 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14341 		    htonl(INADDR_LOOPBACK))
14342 			ipif->ipif_ire_type = IRE_LOOPBACK;
14343 		else
14344 			ipif->ipif_ire_type = IRE_LOCAL;
14345 		if (ill->ill_net_type != IRE_LOOPBACK)
14346 			flags |= NCE_F_PUBLISH;
14347 
14348 		/* add unicast nce for the local addr */
14349 		err = nce_lookup_then_add_v4(ill, NULL,
14350 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14351 		    ND_REACHABLE, &loop_nce);
14352 		/* A shared-IP zone sees EEXIST for lo0:N */
14353 		if (err == 0 || err == EEXIST) {
14354 			ipif->ipif_added_nce = 1;
14355 			loop_nce->nce_ipif_cnt++;
14356 			nce_refrele(loop_nce);
14357 			err = 0;
14358 		} else {
14359 			ASSERT(loop_nce == NULL);
14360 			return (err);
14361 		}
14362 	}
14363 
14364 	/* Create all the IREs associated with this interface */
14365 	err = ipif_add_ires_v4(ipif, loopback);
14366 	if (err != 0) {
14367 		/*
14368 		 * see comments about return value from
14369 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14370 		 */
14371 		if (err != EADDRINUSE) {
14372 			(void) ipif_arp_down(ipif);
14373 		} else {
14374 			/*
14375 			 * Make IPMP aware of the deleted ipif so that
14376 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14377 			 * can be completed. Note that we do not want to
14378 			 * destroy the nce that was created on the ipmp_ill
14379 			 * for the active copy of the duplicate address in
14380 			 * use.
14381 			 */
14382 			if (IS_IPMP(ill))
14383 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14384 			err = EADDRNOTAVAIL;
14385 		}
14386 		return (err);
14387 	}
14388 
14389 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14390 		/* Recover any additional IREs entries for this ill */
14391 		(void) ill_recover_saved_ire(ill);
14392 	}
14393 
14394 	if (ill->ill_need_recover_multicast) {
14395 		/*
14396 		 * Need to recover all multicast memberships in the driver.
14397 		 * This had to be deferred until we had attached.  The same
14398 		 * code exists in ipif_up_done_v6() to recover IPv6
14399 		 * memberships.
14400 		 *
14401 		 * Note that it would be preferable to unconditionally do the
14402 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14403 		 * that since ill_join_allmulti() depends on ill_dl_up being
14404 		 * set, and it is not set until we receive a DL_BIND_ACK after
14405 		 * having called ill_dl_up().
14406 		 */
14407 		ill_recover_multicast(ill);
14408 	}
14409 
14410 	if (ill->ill_ipif_up_count == 1) {
14411 		/*
14412 		 * Since the interface is now up, it may now be active.
14413 		 */
14414 		if (IS_UNDER_IPMP(ill))
14415 			ipmp_ill_refresh_active(ill);
14416 
14417 		/*
14418 		 * If this is an IPMP interface, we may now be able to
14419 		 * establish ARP entries.
14420 		 */
14421 		if (IS_IPMP(ill))
14422 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14423 	}
14424 
14425 	/* Join the allhosts multicast address */
14426 	ipif_multicast_up(ipif);
14427 
14428 	if (!loopback && !update_src_selection &&
14429 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14430 		ip_update_source_selection(ill->ill_ipst);
14431 
14432 	if (!loopback && ipif->ipif_addr_ready) {
14433 		/* Broadcast an address mask reply. */
14434 		ipif_mask_reply(ipif);
14435 	}
14436 	/* Perhaps ilgs should use this ill */
14437 	update_conn_ill(NULL, ill->ill_ipst);
14438 
14439 	/*
14440 	 * This had to be deferred until we had bound.  Tell routing sockets and
14441 	 * others that this interface is up if it looks like the address has
14442 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14443 	 * duplicate address detection to do its thing.
14444 	 */
14445 	if (ipif->ipif_addr_ready)
14446 		ipif_up_notify(ipif);
14447 	return (0);
14448 }
14449 
14450 /*
14451  * Add the IREs associated with the ipif.
14452  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14453  */
14454 static int
14455 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14456 {
14457 	ill_t		*ill = ipif->ipif_ill;
14458 	ip_stack_t	*ipst = ill->ill_ipst;
14459 	ire_t		*ire_array[20];
14460 	ire_t		**irep = ire_array;
14461 	ire_t		**irep1;
14462 	ipaddr_t	net_mask = 0;
14463 	ipaddr_t	subnet_mask, route_mask;
14464 	int		err;
14465 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14466 	ire_t		*ire_if = NULL;
14467 	uchar_t		*gw;
14468 
14469 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14470 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14471 		/*
14472 		 * If we're on a labeled system then make sure that zone-
14473 		 * private addresses have proper remote host database entries.
14474 		 */
14475 		if (is_system_labeled() &&
14476 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14477 		    !tsol_check_interface_address(ipif))
14478 			return (EINVAL);
14479 
14480 		/* Register the source address for __sin6_src_id */
14481 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14482 		    ipif->ipif_zoneid, ipst);
14483 		if (err != 0) {
14484 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14485 			return (err);
14486 		}
14487 
14488 		if (loopback)
14489 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14490 		else
14491 			gw = NULL;
14492 
14493 		/* If the interface address is set, create the local IRE. */
14494 		ire_local = ire_create(
14495 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14496 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14497 		    gw,					/* gateway */
14498 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14499 		    ipif->ipif_ill,
14500 		    ipif->ipif_zoneid,
14501 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14502 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14503 		    NULL,
14504 		    ipst);
14505 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14506 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14507 		    ipif->ipif_ire_type,
14508 		    ntohl(ipif->ipif_lcl_addr)));
14509 		if (ire_local == NULL) {
14510 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14511 			err = ENOMEM;
14512 			goto bad;
14513 		}
14514 	} else {
14515 		ip1dbg((
14516 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14517 		    ipif->ipif_ire_type,
14518 		    ntohl(ipif->ipif_lcl_addr),
14519 		    (uint_t)ipif->ipif_flags));
14520 	}
14521 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14522 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14523 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14524 	} else {
14525 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14526 	}
14527 
14528 	subnet_mask = ipif->ipif_net_mask;
14529 
14530 	/*
14531 	 * If mask was not specified, use natural netmask of
14532 	 * interface address. Also, store this mask back into the
14533 	 * ipif struct.
14534 	 */
14535 	if (subnet_mask == 0) {
14536 		subnet_mask = net_mask;
14537 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14538 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14539 		    ipif->ipif_v6subnet);
14540 	}
14541 
14542 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14543 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14544 	    ipif->ipif_subnet != INADDR_ANY) {
14545 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14546 
14547 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14548 			route_mask = IP_HOST_MASK;
14549 		} else {
14550 			route_mask = subnet_mask;
14551 		}
14552 
14553 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14554 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14555 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14556 		    ntohl(ipif->ipif_subnet)));
14557 		ire_if = ire_create(
14558 		    (uchar_t *)&ipif->ipif_subnet,
14559 		    (uchar_t *)&route_mask,
14560 		    (uchar_t *)&ipif->ipif_lcl_addr,
14561 		    ill->ill_net_type,
14562 		    ill,
14563 		    ipif->ipif_zoneid,
14564 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14565 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14566 		    NULL,
14567 		    ipst);
14568 		if (ire_if == NULL) {
14569 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14570 			err = ENOMEM;
14571 			goto bad;
14572 		}
14573 	}
14574 
14575 	/*
14576 	 * Create any necessary broadcast IREs.
14577 	 */
14578 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14579 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14580 		irep = ipif_create_bcast_ires(ipif, irep);
14581 
14582 	/* If an earlier ire_create failed, get out now */
14583 	for (irep1 = irep; irep1 > ire_array; ) {
14584 		irep1--;
14585 		if (*irep1 == NULL) {
14586 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14587 			err = ENOMEM;
14588 			goto bad;
14589 		}
14590 	}
14591 
14592 	/*
14593 	 * Need to atomically check for IP address availability under
14594 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14595 	 * ills or new ipifs can be added while we are checking availability.
14596 	 */
14597 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14598 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14599 	/* Mark it up, and increment counters. */
14600 	ipif->ipif_flags |= IPIF_UP;
14601 	ill->ill_ipif_up_count++;
14602 	err = ip_addr_availability_check(ipif);
14603 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14604 	rw_exit(&ipst->ips_ill_g_lock);
14605 
14606 	if (err != 0) {
14607 		/*
14608 		 * Our address may already be up on the same ill. In this case,
14609 		 * the ARP entry for our ipif replaced the one for the other
14610 		 * ipif. So we don't want to delete it (otherwise the other ipif
14611 		 * would be unable to send packets).
14612 		 * ip_addr_availability_check() identifies this case for us and
14613 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14614 		 * which is the expected error code.
14615 		 */
14616 		ill->ill_ipif_up_count--;
14617 		ipif->ipif_flags &= ~IPIF_UP;
14618 		goto bad;
14619 	}
14620 
14621 	/*
14622 	 * Add in all newly created IREs.  ire_create_bcast() has
14623 	 * already checked for duplicates of the IRE_BROADCAST type.
14624 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14625 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14626 	 * a /32 route.
14627 	 */
14628 	if (ire_if != NULL) {
14629 		ire_if = ire_add(ire_if);
14630 		if (ire_if == NULL) {
14631 			err = ENOMEM;
14632 			goto bad2;
14633 		}
14634 #ifdef DEBUG
14635 		ire_refhold_notr(ire_if);
14636 		ire_refrele(ire_if);
14637 #endif
14638 	}
14639 	if (ire_local != NULL) {
14640 		ire_local = ire_add(ire_local);
14641 		if (ire_local == NULL) {
14642 			err = ENOMEM;
14643 			goto bad2;
14644 		}
14645 #ifdef DEBUG
14646 		ire_refhold_notr(ire_local);
14647 		ire_refrele(ire_local);
14648 #endif
14649 	}
14650 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14651 	if (ire_local != NULL)
14652 		ipif->ipif_ire_local = ire_local;
14653 	if (ire_if != NULL)
14654 		ipif->ipif_ire_if = ire_if;
14655 	rw_exit(&ipst->ips_ill_g_lock);
14656 	ire_local = NULL;
14657 	ire_if = NULL;
14658 
14659 	/*
14660 	 * We first add all of them, and if that succeeds we refrele the
14661 	 * bunch. That enables us to delete all of them should any of the
14662 	 * ire_adds fail.
14663 	 */
14664 	for (irep1 = irep; irep1 > ire_array; ) {
14665 		irep1--;
14666 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14667 		*irep1 = ire_add(*irep1);
14668 		if (*irep1 == NULL) {
14669 			err = ENOMEM;
14670 			goto bad2;
14671 		}
14672 	}
14673 
14674 	for (irep1 = irep; irep1 > ire_array; ) {
14675 		irep1--;
14676 		/* refheld by ire_add. */
14677 		if (*irep1 != NULL) {
14678 			ire_refrele(*irep1);
14679 			*irep1 = NULL;
14680 		}
14681 	}
14682 
14683 	if (!loopback) {
14684 		/*
14685 		 * If the broadcast address has been set, make sure it makes
14686 		 * sense based on the interface address.
14687 		 * Only match on ill since we are sharing broadcast addresses.
14688 		 */
14689 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14690 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14691 			ire_t	*ire;
14692 
14693 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14694 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14695 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14696 
14697 			if (ire == NULL) {
14698 				/*
14699 				 * If there isn't a matching broadcast IRE,
14700 				 * revert to the default for this netmask.
14701 				 */
14702 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14703 				mutex_enter(&ipif->ipif_ill->ill_lock);
14704 				ipif_set_default(ipif);
14705 				mutex_exit(&ipif->ipif_ill->ill_lock);
14706 			} else {
14707 				ire_refrele(ire);
14708 			}
14709 		}
14710 
14711 	}
14712 	return (0);
14713 
14714 bad2:
14715 	ill->ill_ipif_up_count--;
14716 	ipif->ipif_flags &= ~IPIF_UP;
14717 
14718 bad:
14719 	ip1dbg(("ipif_add_ires: FAILED \n"));
14720 	if (ire_local != NULL)
14721 		ire_delete(ire_local);
14722 	if (ire_if != NULL)
14723 		ire_delete(ire_if);
14724 
14725 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14726 	ire_local = ipif->ipif_ire_local;
14727 	ipif->ipif_ire_local = NULL;
14728 	ire_if = ipif->ipif_ire_if;
14729 	ipif->ipif_ire_if = NULL;
14730 	rw_exit(&ipst->ips_ill_g_lock);
14731 	if (ire_local != NULL) {
14732 		ire_delete(ire_local);
14733 		ire_refrele_notr(ire_local);
14734 	}
14735 	if (ire_if != NULL) {
14736 		ire_delete(ire_if);
14737 		ire_refrele_notr(ire_if);
14738 	}
14739 
14740 	while (irep > ire_array) {
14741 		irep--;
14742 		if (*irep != NULL) {
14743 			ire_delete(*irep);
14744 		}
14745 	}
14746 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14747 
14748 	return (err);
14749 }
14750 
14751 /* Remove all the IREs created by ipif_add_ires_v4 */
14752 void
14753 ipif_delete_ires_v4(ipif_t *ipif)
14754 {
14755 	ill_t		*ill = ipif->ipif_ill;
14756 	ip_stack_t	*ipst = ill->ill_ipst;
14757 	ire_t		*ire;
14758 
14759 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14760 	ire = ipif->ipif_ire_local;
14761 	ipif->ipif_ire_local = NULL;
14762 	rw_exit(&ipst->ips_ill_g_lock);
14763 	if (ire != NULL) {
14764 		/*
14765 		 * Move count to ipif so we don't loose the count due to
14766 		 * a down/up dance.
14767 		 */
14768 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14769 
14770 		ire_delete(ire);
14771 		ire_refrele_notr(ire);
14772 	}
14773 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14774 	ire = ipif->ipif_ire_if;
14775 	ipif->ipif_ire_if = NULL;
14776 	rw_exit(&ipst->ips_ill_g_lock);
14777 	if (ire != NULL) {
14778 		ire_delete(ire);
14779 		ire_refrele_notr(ire);
14780 	}
14781 
14782 	/*
14783 	 * Delete the broadcast IREs.
14784 	 */
14785 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14786 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14787 		ipif_delete_bcast_ires(ipif);
14788 }
14789 
14790 /*
14791  * Checks for availbility of a usable source address (if there is one) when the
14792  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14793  * this selection is done regardless of the destination.
14794  */
14795 boolean_t
14796 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14797     ip_stack_t *ipst)
14798 {
14799 	ipif_t		*ipif = NULL;
14800 	ill_t		*uill;
14801 
14802 	ASSERT(ifindex != 0);
14803 
14804 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14805 	if (uill == NULL)
14806 		return (B_FALSE);
14807 
14808 	mutex_enter(&uill->ill_lock);
14809 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14810 		if (IPIF_IS_CONDEMNED(ipif))
14811 			continue;
14812 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14813 			continue;
14814 		if (!(ipif->ipif_flags & IPIF_UP))
14815 			continue;
14816 		if (ipif->ipif_zoneid != zoneid)
14817 			continue;
14818 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14819 		    ipif->ipif_lcl_addr == INADDR_ANY)
14820 			continue;
14821 		mutex_exit(&uill->ill_lock);
14822 		ill_refrele(uill);
14823 		return (B_TRUE);
14824 	}
14825 	mutex_exit(&uill->ill_lock);
14826 	ill_refrele(uill);
14827 	return (B_FALSE);
14828 }
14829 
14830 /*
14831  * Find an ipif with a good local address on the ill+zoneid.
14832  */
14833 ipif_t *
14834 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14835 {
14836 	ipif_t		*ipif;
14837 
14838 	mutex_enter(&ill->ill_lock);
14839 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14840 		if (IPIF_IS_CONDEMNED(ipif))
14841 			continue;
14842 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14843 			continue;
14844 		if (!(ipif->ipif_flags & IPIF_UP))
14845 			continue;
14846 		if (ipif->ipif_zoneid != zoneid &&
14847 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14848 			continue;
14849 		if (ill->ill_isv6 ?
14850 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14851 		    ipif->ipif_lcl_addr == INADDR_ANY)
14852 			continue;
14853 		ipif_refhold_locked(ipif);
14854 		mutex_exit(&ill->ill_lock);
14855 		return (ipif);
14856 	}
14857 	mutex_exit(&ill->ill_lock);
14858 	return (NULL);
14859 }
14860 
14861 /*
14862  * IP source address type, sorted from worst to best.  For a given type,
14863  * always prefer IP addresses on the same subnet.  All-zones addresses are
14864  * suboptimal because they pose problems with unlabeled destinations.
14865  */
14866 typedef enum {
14867 	IPIF_NONE,
14868 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14869 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14870 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14871 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14872 	IPIF_DIFFNET,			/* normal and different subnet */
14873 	IPIF_SAMENET,			/* normal and same subnet */
14874 	IPIF_LOCALADDR			/* local loopback */
14875 } ipif_type_t;
14876 
14877 /*
14878  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14879  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14880  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14881  * the first one, unless IPMP is used in which case we round-robin among them;
14882  * see below for more.
14883  *
14884  * Returns NULL if there is no suitable source address for the ill.
14885  * This only occurs when there is no valid source address for the ill.
14886  */
14887 ipif_t *
14888 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14889     boolean_t allow_usesrc, boolean_t *notreadyp)
14890 {
14891 	ill_t	*usill = NULL;
14892 	ill_t	*ipmp_ill = NULL;
14893 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14894 	ipif_type_t type, best_type;
14895 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14896 	ip_stack_t *ipst = ill->ill_ipst;
14897 	boolean_t samenet;
14898 
14899 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14900 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14901 		    B_FALSE, ipst);
14902 		if (usill != NULL)
14903 			ill = usill;	/* Select source from usesrc ILL */
14904 		else
14905 			return (NULL);
14906 	}
14907 
14908 	/*
14909 	 * Test addresses should never be used for source address selection,
14910 	 * so if we were passed one, switch to the IPMP meta-interface.
14911 	 */
14912 	if (IS_UNDER_IPMP(ill)) {
14913 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14914 			ill = ipmp_ill;	/* Select source from IPMP ill */
14915 		else
14916 			return (NULL);
14917 	}
14918 
14919 	/*
14920 	 * If we're dealing with an unlabeled destination on a labeled system,
14921 	 * make sure that we ignore source addresses that are incompatible with
14922 	 * the destination's default label.  That destination's default label
14923 	 * must dominate the minimum label on the source address.
14924 	 */
14925 	dst_rhtp = NULL;
14926 	if (is_system_labeled()) {
14927 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14928 		if (dst_rhtp == NULL)
14929 			return (NULL);
14930 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14931 			TPC_RELE(dst_rhtp);
14932 			dst_rhtp = NULL;
14933 		}
14934 	}
14935 
14936 	/*
14937 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14938 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14939 	 * After selecting the right ipif, under ill_lock make sure ipif is
14940 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14941 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14942 	 * but not under a lock.
14943 	 */
14944 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14945 retry:
14946 	/*
14947 	 * For source address selection, we treat the ipif list as circular
14948 	 * and continue until we get back to where we started.  This allows
14949 	 * IPMP to vary source address selection (which improves inbound load
14950 	 * spreading) by caching its last ending point and starting from
14951 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14952 	 * ills since that can't happen on the IPMP ill.
14953 	 */
14954 	start_ipif = ill->ill_ipif;
14955 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14956 		start_ipif = ill->ill_src_ipif;
14957 
14958 	ipif = start_ipif;
14959 	best_ipif = NULL;
14960 	best_type = IPIF_NONE;
14961 	do {
14962 		if ((next_ipif = ipif->ipif_next) == NULL)
14963 			next_ipif = ill->ill_ipif;
14964 
14965 		if (IPIF_IS_CONDEMNED(ipif))
14966 			continue;
14967 		/* Always skip NOLOCAL and ANYCAST interfaces */
14968 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14969 			continue;
14970 		/* Always skip NOACCEPT interfaces */
14971 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14972 			continue;
14973 		if (!(ipif->ipif_flags & IPIF_UP))
14974 			continue;
14975 
14976 		if (!ipif->ipif_addr_ready) {
14977 			if (notreadyp != NULL)
14978 				*notreadyp = B_TRUE;
14979 			continue;
14980 		}
14981 
14982 		if (zoneid != ALL_ZONES &&
14983 		    ipif->ipif_zoneid != zoneid &&
14984 		    ipif->ipif_zoneid != ALL_ZONES)
14985 			continue;
14986 
14987 		/*
14988 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14989 		 * are not valid as source addresses.
14990 		 */
14991 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14992 			continue;
14993 
14994 		/*
14995 		 * Check compatibility of local address for destination's
14996 		 * default label if we're on a labeled system.	Incompatible
14997 		 * addresses can't be used at all.
14998 		 */
14999 		if (dst_rhtp != NULL) {
15000 			boolean_t incompat;
15001 
15002 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15003 			    IPV4_VERSION, B_FALSE);
15004 			if (src_rhtp == NULL)
15005 				continue;
15006 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15007 			    src_rhtp->tpc_tp.tp_doi !=
15008 			    dst_rhtp->tpc_tp.tp_doi ||
15009 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15010 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15011 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15012 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15013 			TPC_RELE(src_rhtp);
15014 			if (incompat)
15015 				continue;
15016 		}
15017 
15018 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15019 
15020 		if (ipif->ipif_lcl_addr == dst) {
15021 			type = IPIF_LOCALADDR;
15022 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15023 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15024 			    IPIF_DIFFNET_DEPRECATED;
15025 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15026 			type = samenet ? IPIF_SAMENET_ALLZONES :
15027 			    IPIF_DIFFNET_ALLZONES;
15028 		} else {
15029 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15030 		}
15031 
15032 		if (type > best_type) {
15033 			best_type = type;
15034 			best_ipif = ipif;
15035 			if (best_type == IPIF_LOCALADDR)
15036 				break; /* can't get better */
15037 		}
15038 	} while ((ipif = next_ipif) != start_ipif);
15039 
15040 	if ((ipif = best_ipif) != NULL) {
15041 		mutex_enter(&ipif->ipif_ill->ill_lock);
15042 		if (IPIF_IS_CONDEMNED(ipif)) {
15043 			mutex_exit(&ipif->ipif_ill->ill_lock);
15044 			goto retry;
15045 		}
15046 		ipif_refhold_locked(ipif);
15047 
15048 		/*
15049 		 * For IPMP, update the source ipif rotor to the next ipif,
15050 		 * provided we can look it up.  (We must not use it if it's
15051 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15052 		 * ipif_free() checked ill_src_ipif.)
15053 		 */
15054 		if (IS_IPMP(ill) && ipif != NULL) {
15055 			next_ipif = ipif->ipif_next;
15056 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15057 				ill->ill_src_ipif = next_ipif;
15058 			else
15059 				ill->ill_src_ipif = NULL;
15060 		}
15061 		mutex_exit(&ipif->ipif_ill->ill_lock);
15062 	}
15063 
15064 	rw_exit(&ipst->ips_ill_g_lock);
15065 	if (usill != NULL)
15066 		ill_refrele(usill);
15067 	if (ipmp_ill != NULL)
15068 		ill_refrele(ipmp_ill);
15069 	if (dst_rhtp != NULL)
15070 		TPC_RELE(dst_rhtp);
15071 
15072 #ifdef DEBUG
15073 	if (ipif == NULL) {
15074 		char buf1[INET6_ADDRSTRLEN];
15075 
15076 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15077 		    ill->ill_name,
15078 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15079 	} else {
15080 		char buf1[INET6_ADDRSTRLEN];
15081 		char buf2[INET6_ADDRSTRLEN];
15082 
15083 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15084 		    ipif->ipif_ill->ill_name,
15085 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15086 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15087 		    buf2, sizeof (buf2))));
15088 	}
15089 #endif /* DEBUG */
15090 	return (ipif);
15091 }
15092 
15093 /*
15094  * Pick a source address based on the destination ill and an optional setsrc
15095  * address.
15096  * The result is stored in srcp. If generation is set, then put the source
15097  * generation number there before we look for the source address (to avoid
15098  * missing changes in the set of source addresses.
15099  * If flagsp is set, then us it to pass back ipif_flags.
15100  *
15101  * If the caller wants to cache the returned source address and detect when
15102  * that might be stale, the caller should pass in a generation argument,
15103  * which the caller can later compare against ips_src_generation
15104  *
15105  * The precedence order for selecting an IPv4 source address is:
15106  *  - RTF_SETSRC on the offlink ire always wins.
15107  *  - If usrsrc is set, swap the ill to be the usesrc one.
15108  *  - If IPMP is used on the ill, select a random address from the most
15109  *    preferred ones below:
15110  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15111  * 2. Not deprecated, not ALL_ZONES
15112  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15113  * 4. Not deprecated, ALL_ZONES
15114  * 5. If onlink destination, same subnet and deprecated
15115  * 6. Deprecated.
15116  *
15117  * We have lower preference for ALL_ZONES IP addresses,
15118  * as they pose problems with unlabeled destinations.
15119  *
15120  * Note that when multiple IP addresses match e.g., #1 we pick
15121  * the first one if IPMP is not in use. With IPMP we randomize.
15122  */
15123 int
15124 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15125     ipaddr_t multicast_ifaddr,
15126     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15127     uint32_t *generation, uint64_t *flagsp)
15128 {
15129 	ipif_t *ipif;
15130 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15131 
15132 	if (flagsp != NULL)
15133 		*flagsp = 0;
15134 
15135 	/*
15136 	 * Need to grab the generation number before we check to
15137 	 * avoid a race with a change to the set of local addresses.
15138 	 * No lock needed since the thread which updates the set of local
15139 	 * addresses use ipif/ill locks and exit those (hence a store memory
15140 	 * barrier) before doing the atomic increase of ips_src_generation.
15141 	 */
15142 	if (generation != NULL) {
15143 		*generation = ipst->ips_src_generation;
15144 	}
15145 
15146 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15147 		*srcp = multicast_ifaddr;
15148 		return (0);
15149 	}
15150 
15151 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15152 	if (setsrc != INADDR_ANY) {
15153 		*srcp = setsrc;
15154 		return (0);
15155 	}
15156 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15157 	if (ipif == NULL) {
15158 		if (notready)
15159 			return (ENETDOWN);
15160 		else
15161 			return (EADDRNOTAVAIL);
15162 	}
15163 	*srcp = ipif->ipif_lcl_addr;
15164 	if (flagsp != NULL)
15165 		*flagsp = ipif->ipif_flags;
15166 	ipif_refrele(ipif);
15167 	return (0);
15168 }
15169 
15170 /* ARGSUSED */
15171 int
15172 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15173 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15174 {
15175 	/*
15176 	 * ill_phyint_reinit merged the v4 and v6 into a single
15177 	 * ipsq.  We might not have been able to complete the
15178 	 * operation in ipif_set_values, if we could not become
15179 	 * exclusive.  If so restart it here.
15180 	 */
15181 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15182 }
15183 
15184 /*
15185  * Can operate on either a module or a driver queue.
15186  * Returns an error if not a module queue.
15187  */
15188 /* ARGSUSED */
15189 int
15190 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15191     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15192 {
15193 	queue_t		*q1 = q;
15194 	char 		*cp;
15195 	char		interf_name[LIFNAMSIZ];
15196 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15197 
15198 	if (q->q_next == NULL) {
15199 		ip1dbg((
15200 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15201 		return (EINVAL);
15202 	}
15203 
15204 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15205 		return (EALREADY);
15206 
15207 	do {
15208 		q1 = q1->q_next;
15209 	} while (q1->q_next);
15210 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15211 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15212 
15213 	/*
15214 	 * Here we are not going to delay the ioack until after
15215 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15216 	 * original ioctl message before sending the requests.
15217 	 */
15218 	return (ipif_set_values(q, mp, interf_name, &ppa));
15219 }
15220 
15221 /* ARGSUSED */
15222 int
15223 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15224     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15225 {
15226 	return (ENXIO);
15227 }
15228 
15229 /*
15230  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15231  * `irep'.  Returns a pointer to the next free `irep' entry
15232  * A mirror exists in ipif_delete_bcast_ires().
15233  *
15234  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15235  * done in ire_add.
15236  */
15237 static ire_t **
15238 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15239 {
15240 	ipaddr_t addr;
15241 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15242 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15243 	ill_t *ill = ipif->ipif_ill;
15244 	zoneid_t zoneid = ipif->ipif_zoneid;
15245 
15246 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15247 
15248 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15249 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15250 
15251 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15252 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15253 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15254 
15255 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15256 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15257 
15258 	/*
15259 	 * For backward compatibility, we create net broadcast IREs based on
15260 	 * the old "IP address class system", since some old machines only
15261 	 * respond to these class derived net broadcast.  However, we must not
15262 	 * create these net broadcast IREs if the subnetmask is shorter than
15263 	 * the IP address class based derived netmask.  Otherwise, we may
15264 	 * create a net broadcast address which is the same as an IP address
15265 	 * on the subnet -- and then TCP will refuse to talk to that address.
15266 	 */
15267 	if (netmask < subnetmask) {
15268 		addr = netmask & ipif->ipif_subnet;
15269 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15270 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15271 	}
15272 
15273 	/*
15274 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15275 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15276 	 * created.  Creating these broadcast IREs will only create confusion
15277 	 * as `addr' will be the same as the IP address.
15278 	 */
15279 	if (subnetmask != 0xFFFFFFFF) {
15280 		addr = ipif->ipif_subnet;
15281 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15282 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15283 	}
15284 
15285 	return (irep);
15286 }
15287 
15288 /*
15289  * Mirror of ipif_create_bcast_ires()
15290  */
15291 static void
15292 ipif_delete_bcast_ires(ipif_t *ipif)
15293 {
15294 	ipaddr_t	addr;
15295 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15296 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15297 	ill_t		*ill = ipif->ipif_ill;
15298 	zoneid_t	zoneid = ipif->ipif_zoneid;
15299 	ire_t		*ire;
15300 
15301 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15302 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15303 
15304 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15305 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15306 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15307 
15308 	ire = ire_lookup_bcast(ill, 0, zoneid);
15309 	ASSERT(ire != NULL);
15310 	ire_delete(ire); ire_refrele(ire);
15311 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15312 	ASSERT(ire != NULL);
15313 	ire_delete(ire); ire_refrele(ire);
15314 
15315 	/*
15316 	 * For backward compatibility, we create net broadcast IREs based on
15317 	 * the old "IP address class system", since some old machines only
15318 	 * respond to these class derived net broadcast.  However, we must not
15319 	 * create these net broadcast IREs if the subnetmask is shorter than
15320 	 * the IP address class based derived netmask.  Otherwise, we may
15321 	 * create a net broadcast address which is the same as an IP address
15322 	 * on the subnet -- and then TCP will refuse to talk to that address.
15323 	 */
15324 	if (netmask < subnetmask) {
15325 		addr = netmask & ipif->ipif_subnet;
15326 		ire = ire_lookup_bcast(ill, addr, zoneid);
15327 		ASSERT(ire != NULL);
15328 		ire_delete(ire); ire_refrele(ire);
15329 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15330 		ASSERT(ire != NULL);
15331 		ire_delete(ire); ire_refrele(ire);
15332 	}
15333 
15334 	/*
15335 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15336 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15337 	 * created.  Creating these broadcast IREs will only create confusion
15338 	 * as `addr' will be the same as the IP address.
15339 	 */
15340 	if (subnetmask != 0xFFFFFFFF) {
15341 		addr = ipif->ipif_subnet;
15342 		ire = ire_lookup_bcast(ill, addr, zoneid);
15343 		ASSERT(ire != NULL);
15344 		ire_delete(ire); ire_refrele(ire);
15345 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15346 		ASSERT(ire != NULL);
15347 		ire_delete(ire); ire_refrele(ire);
15348 	}
15349 }
15350 
15351 /*
15352  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15353  * from lifr_flags and the name from lifr_name.
15354  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15355  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15356  * Returns EINPROGRESS when mp has been consumed by queueing it on
15357  * ipx_pending_mp and the ioctl will complete in ip_rput.
15358  *
15359  * Can operate on either a module or a driver queue.
15360  * Returns an error if not a module queue.
15361  */
15362 /* ARGSUSED */
15363 int
15364 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15365     ip_ioctl_cmd_t *ipip, void *if_req)
15366 {
15367 	ill_t	*ill = q->q_ptr;
15368 	phyint_t *phyi;
15369 	ip_stack_t *ipst;
15370 	struct lifreq *lifr = if_req;
15371 	uint64_t new_flags;
15372 
15373 	ASSERT(ipif != NULL);
15374 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15375 
15376 	if (q->q_next == NULL) {
15377 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15378 		return (EINVAL);
15379 	}
15380 
15381 	/*
15382 	 * If we are not writer on 'q' then this interface exists already
15383 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15384 	 * so return EALREADY.
15385 	 */
15386 	if (ill != ipif->ipif_ill)
15387 		return (EALREADY);
15388 
15389 	if (ill->ill_name[0] != '\0')
15390 		return (EALREADY);
15391 
15392 	/*
15393 	 * If there's another ill already with the requested name, ensure
15394 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15395 	 * fuse together two unrelated ills, which will cause chaos.
15396 	 */
15397 	ipst = ill->ill_ipst;
15398 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15399 	    lifr->lifr_name, NULL);
15400 	if (phyi != NULL) {
15401 		ill_t *ill_mate = phyi->phyint_illv4;
15402 
15403 		if (ill_mate == NULL)
15404 			ill_mate = phyi->phyint_illv6;
15405 		ASSERT(ill_mate != NULL);
15406 
15407 		if (ill_mate->ill_media->ip_m_mac_type !=
15408 		    ill->ill_media->ip_m_mac_type) {
15409 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15410 			    "use the same ill name on differing media\n"));
15411 			return (EINVAL);
15412 		}
15413 	}
15414 
15415 	/*
15416 	 * We start off as IFF_IPV4 in ipif_allocate and become
15417 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15418 	 * The only flags that we read from user space are IFF_IPV4,
15419 	 * IFF_IPV6, and IFF_BROADCAST.
15420 	 *
15421 	 * This ill has not been inserted into the global list.
15422 	 * So we are still single threaded and don't need any lock
15423 	 *
15424 	 * Saniy check the flags.
15425 	 */
15426 
15427 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15428 	    ((lifr->lifr_flags & IFF_IPV6) ||
15429 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15430 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15431 		    "or IPv6 i.e., no broadcast \n"));
15432 		return (EINVAL);
15433 	}
15434 
15435 	new_flags =
15436 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15437 
15438 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15439 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15440 		    "IFF_IPV4 or IFF_IPV6\n"));
15441 		return (EINVAL);
15442 	}
15443 
15444 	/*
15445 	 * We always start off as IPv4, so only need to check for IPv6.
15446 	 */
15447 	if ((new_flags & IFF_IPV6) != 0) {
15448 		ill->ill_flags |= ILLF_IPV6;
15449 		ill->ill_flags &= ~ILLF_IPV4;
15450 	}
15451 
15452 	if ((new_flags & IFF_BROADCAST) != 0)
15453 		ipif->ipif_flags |= IPIF_BROADCAST;
15454 	else
15455 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15456 
15457 	/* We started off as V4. */
15458 	if (ill->ill_flags & ILLF_IPV6) {
15459 		ill->ill_phyint->phyint_illv6 = ill;
15460 		ill->ill_phyint->phyint_illv4 = NULL;
15461 	}
15462 
15463 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15464 }
15465 
15466 /* ARGSUSED */
15467 int
15468 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15469     ip_ioctl_cmd_t *ipip, void *if_req)
15470 {
15471 	/*
15472 	 * ill_phyint_reinit merged the v4 and v6 into a single
15473 	 * ipsq.  We might not have been able to complete the
15474 	 * slifname in ipif_set_values, if we could not become
15475 	 * exclusive.  If so restart it here
15476 	 */
15477 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15478 }
15479 
15480 /*
15481  * Return a pointer to the ipif which matches the index, IP version type and
15482  * zoneid.
15483  */
15484 ipif_t *
15485 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15486     ip_stack_t *ipst)
15487 {
15488 	ill_t	*ill;
15489 	ipif_t	*ipif = NULL;
15490 
15491 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15492 	if (ill != NULL) {
15493 		mutex_enter(&ill->ill_lock);
15494 		for (ipif = ill->ill_ipif; ipif != NULL;
15495 		    ipif = ipif->ipif_next) {
15496 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15497 			    zoneid == ipif->ipif_zoneid ||
15498 			    ipif->ipif_zoneid == ALL_ZONES)) {
15499 				ipif_refhold_locked(ipif);
15500 				break;
15501 			}
15502 		}
15503 		mutex_exit(&ill->ill_lock);
15504 		ill_refrele(ill);
15505 	}
15506 	return (ipif);
15507 }
15508 
15509 /*
15510  * Change an existing physical interface's index. If the new index
15511  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15512  * Finally, we update other systems which may have a dependence on the
15513  * index value.
15514  */
15515 /* ARGSUSED */
15516 int
15517 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15518     ip_ioctl_cmd_t *ipip, void *ifreq)
15519 {
15520 	ill_t		*ill;
15521 	phyint_t	*phyi;
15522 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15523 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15524 	uint_t	old_index, index;
15525 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15526 	avl_index_t	where;
15527 
15528 	if (ipip->ipi_cmd_type == IF_CMD)
15529 		index = ifr->ifr_index;
15530 	else
15531 		index = lifr->lifr_index;
15532 
15533 	/*
15534 	 * Only allow on physical interface. Also, index zero is illegal.
15535 	 */
15536 	ill = ipif->ipif_ill;
15537 	phyi = ill->ill_phyint;
15538 	if (ipif->ipif_id != 0 || index == 0) {
15539 		return (EINVAL);
15540 	}
15541 
15542 	/* If the index is not changing, no work to do */
15543 	if (phyi->phyint_ifindex == index)
15544 		return (0);
15545 
15546 	/*
15547 	 * Use phyint_exists() to determine if the new interface index
15548 	 * is already in use. If the index is unused then we need to
15549 	 * change the phyint's position in the phyint_list_avl_by_index
15550 	 * tree. If we do not do this, subsequent lookups (using the new
15551 	 * index value) will not find the phyint.
15552 	 */
15553 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15554 	if (phyint_exists(index, ipst)) {
15555 		rw_exit(&ipst->ips_ill_g_lock);
15556 		return (EEXIST);
15557 	}
15558 
15559 	/*
15560 	 * The new index is unused. Set it in the phyint. However we must not
15561 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15562 	 * changes. The event must be bound to old ifindex value.
15563 	 */
15564 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15565 	    &index, sizeof (index));
15566 
15567 	old_index = phyi->phyint_ifindex;
15568 	phyi->phyint_ifindex = index;
15569 
15570 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15571 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15572 	    &index, &where);
15573 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15574 	    phyi, where);
15575 	rw_exit(&ipst->ips_ill_g_lock);
15576 
15577 	/* Update SCTP's ILL list */
15578 	sctp_ill_reindex(ill, old_index);
15579 
15580 	/* Send the routing sockets message */
15581 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15582 	if (ILL_OTHER(ill))
15583 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15584 
15585 	/* Perhaps ilgs should use this ill */
15586 	update_conn_ill(NULL, ill->ill_ipst);
15587 	return (0);
15588 }
15589 
15590 /* ARGSUSED */
15591 int
15592 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15593     ip_ioctl_cmd_t *ipip, void *ifreq)
15594 {
15595 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15596 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15597 
15598 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15599 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15600 	/* Get the interface index */
15601 	if (ipip->ipi_cmd_type == IF_CMD) {
15602 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15603 	} else {
15604 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15605 	}
15606 	return (0);
15607 }
15608 
15609 /* ARGSUSED */
15610 int
15611 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15612     ip_ioctl_cmd_t *ipip, void *ifreq)
15613 {
15614 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15615 
15616 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15617 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15618 	/* Get the interface zone */
15619 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15620 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15621 	return (0);
15622 }
15623 
15624 /*
15625  * Set the zoneid of an interface.
15626  */
15627 /* ARGSUSED */
15628 int
15629 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15630     ip_ioctl_cmd_t *ipip, void *ifreq)
15631 {
15632 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15633 	int err = 0;
15634 	boolean_t need_up = B_FALSE;
15635 	zone_t *zptr;
15636 	zone_status_t status;
15637 	zoneid_t zoneid;
15638 
15639 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15640 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15641 		if (!is_system_labeled())
15642 			return (ENOTSUP);
15643 		zoneid = GLOBAL_ZONEID;
15644 	}
15645 
15646 	/* cannot assign instance zero to a non-global zone */
15647 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15648 		return (ENOTSUP);
15649 
15650 	/*
15651 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15652 	 * the event of a race with the zone shutdown processing, since IP
15653 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15654 	 * interface will be cleaned up even if the zone is shut down
15655 	 * immediately after the status check. If the interface can't be brought
15656 	 * down right away, and the zone is shut down before the restart
15657 	 * function is called, we resolve the possible races by rechecking the
15658 	 * zone status in the restart function.
15659 	 */
15660 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15661 		return (EINVAL);
15662 	status = zone_status_get(zptr);
15663 	zone_rele(zptr);
15664 
15665 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15666 		return (EINVAL);
15667 
15668 	if (ipif->ipif_flags & IPIF_UP) {
15669 		/*
15670 		 * If the interface is already marked up,
15671 		 * we call ipif_down which will take care
15672 		 * of ditching any IREs that have been set
15673 		 * up based on the old interface address.
15674 		 */
15675 		err = ipif_logical_down(ipif, q, mp);
15676 		if (err == EINPROGRESS)
15677 			return (err);
15678 		(void) ipif_down_tail(ipif);
15679 		need_up = B_TRUE;
15680 	}
15681 
15682 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15683 	return (err);
15684 }
15685 
15686 static int
15687 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15688     queue_t *q, mblk_t *mp, boolean_t need_up)
15689 {
15690 	int	err = 0;
15691 	ip_stack_t	*ipst;
15692 
15693 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15695 
15696 	if (CONN_Q(q))
15697 		ipst = CONNQ_TO_IPST(q);
15698 	else
15699 		ipst = ILLQ_TO_IPST(q);
15700 
15701 	/*
15702 	 * For exclusive stacks we don't allow a different zoneid than
15703 	 * global.
15704 	 */
15705 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15706 	    zoneid != GLOBAL_ZONEID)
15707 		return (EINVAL);
15708 
15709 	/* Set the new zone id. */
15710 	ipif->ipif_zoneid = zoneid;
15711 
15712 	/* Update sctp list */
15713 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15714 
15715 	/* The default multicast interface might have changed */
15716 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15717 
15718 	if (need_up) {
15719 		/*
15720 		 * Now bring the interface back up.  If this
15721 		 * is the only IPIF for the ILL, ipif_up
15722 		 * will have to re-bind to the device, so
15723 		 * we may get back EINPROGRESS, in which
15724 		 * case, this IOCTL will get completed in
15725 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15726 		 */
15727 		err = ipif_up(ipif, q, mp);
15728 	}
15729 	return (err);
15730 }
15731 
15732 /* ARGSUSED */
15733 int
15734 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15735     ip_ioctl_cmd_t *ipip, void *if_req)
15736 {
15737 	struct lifreq *lifr = (struct lifreq *)if_req;
15738 	zoneid_t zoneid;
15739 	zone_t *zptr;
15740 	zone_status_t status;
15741 
15742 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15743 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15744 		zoneid = GLOBAL_ZONEID;
15745 
15746 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15747 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15748 
15749 	/*
15750 	 * We recheck the zone status to resolve the following race condition:
15751 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15752 	 * 2) hme0:1 is up and can't be brought down right away;
15753 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15754 	 * 3) zone "myzone" is halted; the zone status switches to
15755 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15756 	 * the interfaces to remove - hme0:1 is not returned because it's not
15757 	 * yet in "myzone", so it won't be removed;
15758 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15759 	 * status check here, we would have hme0:1 in "myzone" after it's been
15760 	 * destroyed.
15761 	 * Note that if the status check fails, we need to bring the interface
15762 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15763 	 * ipif_up_done[_v6]().
15764 	 */
15765 	status = ZONE_IS_UNINITIALIZED;
15766 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15767 		status = zone_status_get(zptr);
15768 		zone_rele(zptr);
15769 	}
15770 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15771 		if (ipif->ipif_isv6) {
15772 			(void) ipif_up_done_v6(ipif);
15773 		} else {
15774 			(void) ipif_up_done(ipif);
15775 		}
15776 		return (EINVAL);
15777 	}
15778 
15779 	(void) ipif_down_tail(ipif);
15780 
15781 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15782 	    B_TRUE));
15783 }
15784 
15785 /*
15786  * Return the number of addresses on `ill' with one or more of the values
15787  * in `set' set and all of the values in `clear' clear.
15788  */
15789 static uint_t
15790 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15791 {
15792 	ipif_t	*ipif;
15793 	uint_t	cnt = 0;
15794 
15795 	ASSERT(IAM_WRITER_ILL(ill));
15796 
15797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15798 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15799 			cnt++;
15800 
15801 	return (cnt);
15802 }
15803 
15804 /*
15805  * Return the number of migratable addresses on `ill' that are under
15806  * application control.
15807  */
15808 uint_t
15809 ill_appaddr_cnt(const ill_t *ill)
15810 {
15811 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15812 	    IPIF_NOFAILOVER));
15813 }
15814 
15815 /*
15816  * Return the number of point-to-point addresses on `ill'.
15817  */
15818 uint_t
15819 ill_ptpaddr_cnt(const ill_t *ill)
15820 {
15821 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15822 }
15823 
15824 /* ARGSUSED */
15825 int
15826 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15827 	ip_ioctl_cmd_t *ipip, void *ifreq)
15828 {
15829 	struct lifreq	*lifr = ifreq;
15830 
15831 	ASSERT(q->q_next == NULL);
15832 	ASSERT(CONN_Q(q));
15833 
15834 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15835 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15836 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15837 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15838 
15839 	return (0);
15840 }
15841 
15842 /* Find the previous ILL in this usesrc group */
15843 static ill_t *
15844 ill_prev_usesrc(ill_t *uill)
15845 {
15846 	ill_t *ill;
15847 
15848 	for (ill = uill->ill_usesrc_grp_next;
15849 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15850 	    ill = ill->ill_usesrc_grp_next)
15851 		/* do nothing */;
15852 	return (ill);
15853 }
15854 
15855 /*
15856  * Release all members of the usesrc group. This routine is called
15857  * from ill_delete when the interface being unplumbed is the
15858  * group head.
15859  *
15860  * This silently clears the usesrc that ifconfig setup.
15861  * An alternative would be to keep that ifindex, and drop packets on the floor
15862  * since no source address can be selected.
15863  * Even if we keep the current semantics, don't need a lock and a linked list.
15864  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15865  * the one that is being removed. Issue is how we return the usesrc users
15866  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15867  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15868  * ill walk, but the walker would need to insert in the ioctl response.
15869  */
15870 static void
15871 ill_disband_usesrc_group(ill_t *uill)
15872 {
15873 	ill_t *next_ill, *tmp_ill;
15874 	ip_stack_t	*ipst = uill->ill_ipst;
15875 
15876 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15877 	next_ill = uill->ill_usesrc_grp_next;
15878 
15879 	do {
15880 		ASSERT(next_ill != NULL);
15881 		tmp_ill = next_ill->ill_usesrc_grp_next;
15882 		ASSERT(tmp_ill != NULL);
15883 		next_ill->ill_usesrc_grp_next = NULL;
15884 		next_ill->ill_usesrc_ifindex = 0;
15885 		next_ill = tmp_ill;
15886 	} while (next_ill->ill_usesrc_ifindex != 0);
15887 	uill->ill_usesrc_grp_next = NULL;
15888 }
15889 
15890 /*
15891  * Remove the client usesrc ILL from the list and relink to a new list
15892  */
15893 int
15894 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15895 {
15896 	ill_t *ill, *tmp_ill;
15897 	ip_stack_t	*ipst = ucill->ill_ipst;
15898 
15899 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15900 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15901 
15902 	/*
15903 	 * Check if the usesrc client ILL passed in is not already
15904 	 * in use as a usesrc ILL i.e one whose source address is
15905 	 * in use OR a usesrc ILL is not already in use as a usesrc
15906 	 * client ILL
15907 	 */
15908 	if ((ucill->ill_usesrc_ifindex == 0) ||
15909 	    (uill->ill_usesrc_ifindex != 0)) {
15910 		return (-1);
15911 	}
15912 
15913 	ill = ill_prev_usesrc(ucill);
15914 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15915 
15916 	/* Remove from the current list */
15917 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15918 		/* Only two elements in the list */
15919 		ASSERT(ill->ill_usesrc_ifindex == 0);
15920 		ill->ill_usesrc_grp_next = NULL;
15921 	} else {
15922 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15923 	}
15924 
15925 	if (ifindex == 0) {
15926 		ucill->ill_usesrc_ifindex = 0;
15927 		ucill->ill_usesrc_grp_next = NULL;
15928 		return (0);
15929 	}
15930 
15931 	ucill->ill_usesrc_ifindex = ifindex;
15932 	tmp_ill = uill->ill_usesrc_grp_next;
15933 	uill->ill_usesrc_grp_next = ucill;
15934 	ucill->ill_usesrc_grp_next =
15935 	    (tmp_ill != NULL) ? tmp_ill : uill;
15936 	return (0);
15937 }
15938 
15939 /*
15940  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15941  * ip.c for locking details.
15942  */
15943 /* ARGSUSED */
15944 int
15945 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15946     ip_ioctl_cmd_t *ipip, void *ifreq)
15947 {
15948 	struct lifreq *lifr = (struct lifreq *)ifreq;
15949 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15950 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15951 	int err = 0, ret;
15952 	uint_t ifindex;
15953 	ipsq_t *ipsq = NULL;
15954 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15955 
15956 	ASSERT(IAM_WRITER_IPIF(ipif));
15957 	ASSERT(q->q_next == NULL);
15958 	ASSERT(CONN_Q(q));
15959 
15960 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15961 
15962 	ifindex = lifr->lifr_index;
15963 	if (ifindex == 0) {
15964 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15965 			/* non usesrc group interface, nothing to reset */
15966 			return (0);
15967 		}
15968 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15969 		/* valid reset request */
15970 		reset_flg = B_TRUE;
15971 	}
15972 
15973 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15974 	if (usesrc_ill == NULL) {
15975 		return (ENXIO);
15976 	}
15977 
15978 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15979 	    NEW_OP, B_TRUE);
15980 	if (ipsq == NULL) {
15981 		err = EINPROGRESS;
15982 		/* Operation enqueued on the ipsq of the usesrc ILL */
15983 		goto done;
15984 	}
15985 
15986 	/* USESRC isn't currently supported with IPMP */
15987 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15988 		err = ENOTSUP;
15989 		goto done;
15990 	}
15991 
15992 	/*
15993 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15994 	 * used by IPMP underlying interfaces, but someone might think it's
15995 	 * more general and try to use it independently with VNI.)
15996 	 */
15997 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15998 		err = ENOTSUP;
15999 		goto done;
16000 	}
16001 
16002 	/*
16003 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16004 	 * already a client then return EINVAL
16005 	 */
16006 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16007 		err = EINVAL;
16008 		goto done;
16009 	}
16010 
16011 	/*
16012 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16013 	 * be then this is a duplicate operation.
16014 	 */
16015 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16016 		err = 0;
16017 		goto done;
16018 	}
16019 
16020 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16021 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16022 	    usesrc_ill->ill_isv6));
16023 
16024 	/*
16025 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16026 	 * and the ill_usesrc_ifindex fields
16027 	 */
16028 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16029 
16030 	if (reset_flg) {
16031 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16032 		if (ret != 0) {
16033 			err = EINVAL;
16034 		}
16035 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16036 		goto done;
16037 	}
16038 
16039 	/*
16040 	 * Four possibilities to consider:
16041 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16042 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16043 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16044 	 * 4. Both are part of their respective usesrc groups
16045 	 */
16046 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16047 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16048 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16049 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16050 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16051 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16052 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16053 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16054 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16055 		/* Insert at head of list */
16056 		usesrc_cli_ill->ill_usesrc_grp_next =
16057 		    usesrc_ill->ill_usesrc_grp_next;
16058 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16059 	} else {
16060 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16061 		    ifindex);
16062 		if (ret != 0)
16063 			err = EINVAL;
16064 	}
16065 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16066 
16067 done:
16068 	if (ipsq != NULL)
16069 		ipsq_exit(ipsq);
16070 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16071 	ill_refrele(usesrc_ill);
16072 
16073 	/* Let conn_ixa caching know that source address selection changed */
16074 	ip_update_source_selection(ipst);
16075 
16076 	return (err);
16077 }
16078 
16079 /*
16080  * comparison function used by avl.
16081  */
16082 static int
16083 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16084 {
16085 
16086 	uint_t index;
16087 
16088 	ASSERT(phyip != NULL && index_ptr != NULL);
16089 
16090 	index = *((uint_t *)index_ptr);
16091 	/*
16092 	 * let the phyint with the lowest index be on top.
16093 	 */
16094 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16095 		return (1);
16096 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16097 		return (-1);
16098 	return (0);
16099 }
16100 
16101 /*
16102  * comparison function used by avl.
16103  */
16104 static int
16105 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16106 {
16107 	ill_t *ill;
16108 	int res = 0;
16109 
16110 	ASSERT(phyip != NULL && name_ptr != NULL);
16111 
16112 	if (((phyint_t *)phyip)->phyint_illv4)
16113 		ill = ((phyint_t *)phyip)->phyint_illv4;
16114 	else
16115 		ill = ((phyint_t *)phyip)->phyint_illv6;
16116 	ASSERT(ill != NULL);
16117 
16118 	res = strcmp(ill->ill_name, (char *)name_ptr);
16119 	if (res > 0)
16120 		return (1);
16121 	else if (res < 0)
16122 		return (-1);
16123 	return (0);
16124 }
16125 
16126 /*
16127  * This function is called on the unplumb path via ill_glist_delete() when
16128  * there are no ills left on the phyint and thus the phyint can be freed.
16129  */
16130 static void
16131 phyint_free(phyint_t *phyi)
16132 {
16133 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16134 
16135 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16136 
16137 	/*
16138 	 * If this phyint was an IPMP meta-interface, blow away the group.
16139 	 * This is safe to do because all of the illgrps have already been
16140 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16141 	 * If we're cleaning up as a result of failed initialization,
16142 	 * phyint_grp may be NULL.
16143 	 */
16144 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16145 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16146 		ipmp_grp_destroy(phyi->phyint_grp);
16147 		phyi->phyint_grp = NULL;
16148 		rw_exit(&ipst->ips_ipmp_lock);
16149 	}
16150 
16151 	/*
16152 	 * If this interface was under IPMP, take it out of the group.
16153 	 */
16154 	if (phyi->phyint_grp != NULL)
16155 		ipmp_phyint_leave_grp(phyi);
16156 
16157 	/*
16158 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16159 	 * will be freed in ipsq_exit().
16160 	 */
16161 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16162 	phyi->phyint_name[0] = '\0';
16163 
16164 	mi_free(phyi);
16165 }
16166 
16167 /*
16168  * Attach the ill to the phyint structure which can be shared by both
16169  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16170  * function is called from ipif_set_values and ill_lookup_on_name (for
16171  * loopback) where we know the name of the ill. We lookup the ill and if
16172  * there is one present already with the name use that phyint. Otherwise
16173  * reuse the one allocated by ill_init.
16174  */
16175 static void
16176 ill_phyint_reinit(ill_t *ill)
16177 {
16178 	boolean_t isv6 = ill->ill_isv6;
16179 	phyint_t *phyi_old;
16180 	phyint_t *phyi;
16181 	avl_index_t where = 0;
16182 	ill_t	*ill_other = NULL;
16183 	ip_stack_t	*ipst = ill->ill_ipst;
16184 
16185 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16186 
16187 	phyi_old = ill->ill_phyint;
16188 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16189 	    phyi_old->phyint_illv6 == NULL));
16190 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16191 	    phyi_old->phyint_illv4 == NULL));
16192 	ASSERT(phyi_old->phyint_ifindex == 0);
16193 
16194 	/*
16195 	 * Now that our ill has a name, set it in the phyint.
16196 	 */
16197 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16198 
16199 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16200 	    ill->ill_name, &where);
16201 
16202 	/*
16203 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16204 	 *    the global list of ills. So no other thread could have located
16205 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16206 	 * 2. Now locate the other protocol instance of this ill.
16207 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16208 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16209 	 *    of neither ill can change.
16210 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16211 	 *    other ill.
16212 	 * 5. Release all locks.
16213 	 */
16214 
16215 	/*
16216 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16217 	 * we are initializing IPv4.
16218 	 */
16219 	if (phyi != NULL) {
16220 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16221 		ASSERT(ill_other->ill_phyint != NULL);
16222 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16223 		    (!isv6 && ill_other->ill_isv6));
16224 		GRAB_ILL_LOCKS(ill, ill_other);
16225 		/*
16226 		 * We are potentially throwing away phyint_flags which
16227 		 * could be different from the one that we obtain from
16228 		 * ill_other->ill_phyint. But it is okay as we are assuming
16229 		 * that the state maintained within IP is correct.
16230 		 */
16231 		mutex_enter(&phyi->phyint_lock);
16232 		if (isv6) {
16233 			ASSERT(phyi->phyint_illv6 == NULL);
16234 			phyi->phyint_illv6 = ill;
16235 		} else {
16236 			ASSERT(phyi->phyint_illv4 == NULL);
16237 			phyi->phyint_illv4 = ill;
16238 		}
16239 
16240 		/*
16241 		 * Delete the old phyint and make its ipsq eligible
16242 		 * to be freed in ipsq_exit().
16243 		 */
16244 		phyi_old->phyint_illv4 = NULL;
16245 		phyi_old->phyint_illv6 = NULL;
16246 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16247 		phyi_old->phyint_name[0] = '\0';
16248 		mi_free(phyi_old);
16249 	} else {
16250 		mutex_enter(&ill->ill_lock);
16251 		/*
16252 		 * We don't need to acquire any lock, since
16253 		 * the ill is not yet visible globally  and we
16254 		 * have not yet released the ill_g_lock.
16255 		 */
16256 		phyi = phyi_old;
16257 		mutex_enter(&phyi->phyint_lock);
16258 		/* XXX We need a recovery strategy here. */
16259 		if (!phyint_assign_ifindex(phyi, ipst))
16260 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16261 
16262 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16263 		    (void *)phyi, where);
16264 
16265 		(void) avl_find(&ipst->ips_phyint_g_list->
16266 		    phyint_list_avl_by_index,
16267 		    &phyi->phyint_ifindex, &where);
16268 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16269 		    (void *)phyi, where);
16270 	}
16271 
16272 	/*
16273 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16274 	 * pending mp is not affected because that is per ill basis.
16275 	 */
16276 	ill->ill_phyint = phyi;
16277 
16278 	/*
16279 	 * Now that the phyint's ifindex has been assigned, complete the
16280 	 * remaining
16281 	 */
16282 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16283 	if (ill->ill_isv6) {
16284 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16285 		    ill->ill_phyint->phyint_ifindex;
16286 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16287 	} else {
16288 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16289 	}
16290 
16291 	/*
16292 	 * Generate an event within the hooks framework to indicate that
16293 	 * a new interface has just been added to IP.  For this event to
16294 	 * be generated, the network interface must, at least, have an
16295 	 * ifindex assigned to it.  (We don't generate the event for
16296 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16297 	 *
16298 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16299 	 * that the ordering of delivered events to listeners matches the
16300 	 * order of them in the kernel.
16301 	 */
16302 	if (!IS_LOOPBACK(ill)) {
16303 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16304 		    ill->ill_name_length);
16305 	}
16306 	RELEASE_ILL_LOCKS(ill, ill_other);
16307 	mutex_exit(&phyi->phyint_lock);
16308 }
16309 
16310 /*
16311  * Notify any downstream modules of the name of this interface.
16312  * An M_IOCTL is used even though we don't expect a successful reply.
16313  * Any reply message from the driver (presumably an M_IOCNAK) will
16314  * eventually get discarded somewhere upstream.  The message format is
16315  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16316  * to IP.
16317  */
16318 static void
16319 ip_ifname_notify(ill_t *ill, queue_t *q)
16320 {
16321 	mblk_t *mp1, *mp2;
16322 	struct iocblk *iocp;
16323 	struct lifreq *lifr;
16324 
16325 	mp1 = mkiocb(SIOCSLIFNAME);
16326 	if (mp1 == NULL)
16327 		return;
16328 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16329 	if (mp2 == NULL) {
16330 		freeb(mp1);
16331 		return;
16332 	}
16333 
16334 	mp1->b_cont = mp2;
16335 	iocp = (struct iocblk *)mp1->b_rptr;
16336 	iocp->ioc_count = sizeof (struct lifreq);
16337 
16338 	lifr = (struct lifreq *)mp2->b_rptr;
16339 	mp2->b_wptr += sizeof (struct lifreq);
16340 	bzero(lifr, sizeof (struct lifreq));
16341 
16342 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16343 	lifr->lifr_ppa = ill->ill_ppa;
16344 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16345 
16346 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16347 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16348 	putnext(q, mp1);
16349 }
16350 
16351 static int
16352 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16353 {
16354 	int		err;
16355 	ip_stack_t	*ipst = ill->ill_ipst;
16356 	phyint_t	*phyi = ill->ill_phyint;
16357 
16358 	/* Set the obsolete NDD per-interface forwarding name. */
16359 	err = ill_set_ndd_name(ill);
16360 	if (err != 0) {
16361 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16362 		    err);
16363 	}
16364 
16365 	/*
16366 	 * Now that ill_name is set, the configuration for the IPMP
16367 	 * meta-interface can be performed.
16368 	 */
16369 	if (IS_IPMP(ill)) {
16370 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16371 		/*
16372 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16373 		 * meta-interface and we need to create the IPMP group.
16374 		 */
16375 		if (phyi->phyint_grp == NULL) {
16376 			/*
16377 			 * If someone has renamed another IPMP group to have
16378 			 * the same name as our interface, bail.
16379 			 */
16380 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16381 				rw_exit(&ipst->ips_ipmp_lock);
16382 				return (EEXIST);
16383 			}
16384 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16385 			if (phyi->phyint_grp == NULL) {
16386 				rw_exit(&ipst->ips_ipmp_lock);
16387 				return (ENOMEM);
16388 			}
16389 		}
16390 		rw_exit(&ipst->ips_ipmp_lock);
16391 	}
16392 
16393 	/* Tell downstream modules where they are. */
16394 	ip_ifname_notify(ill, q);
16395 
16396 	/*
16397 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16398 	 * Error cases are ENOMEM ...
16399 	 */
16400 	err = ill_dl_phys(ill, ipif, mp, q);
16401 
16402 	if (ill->ill_isv6) {
16403 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16404 		if (ipst->ips_mld_slowtimeout_id == 0) {
16405 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16406 			    (void *)ipst,
16407 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16408 		}
16409 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16410 	} else {
16411 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16412 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16413 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16414 			    (void *)ipst,
16415 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16416 		}
16417 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16418 	}
16419 
16420 	return (err);
16421 }
16422 
16423 /*
16424  * Common routine for ppa and ifname setting. Should be called exclusive.
16425  *
16426  * Returns EINPROGRESS when mp has been consumed by queueing it on
16427  * ipx_pending_mp and the ioctl will complete in ip_rput.
16428  *
16429  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16430  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16431  * For SLIFNAME, we pass these values back to the userland.
16432  */
16433 static int
16434 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16435 {
16436 	ill_t	*ill;
16437 	ipif_t	*ipif;
16438 	ipsq_t	*ipsq;
16439 	char	*ppa_ptr;
16440 	char	*old_ptr;
16441 	char	old_char;
16442 	int	error;
16443 	ip_stack_t	*ipst;
16444 
16445 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16446 	ASSERT(q->q_next != NULL);
16447 	ASSERT(interf_name != NULL);
16448 
16449 	ill = (ill_t *)q->q_ptr;
16450 	ipst = ill->ill_ipst;
16451 
16452 	ASSERT(ill->ill_ipst != NULL);
16453 	ASSERT(ill->ill_name[0] == '\0');
16454 	ASSERT(IAM_WRITER_ILL(ill));
16455 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16456 	ASSERT(ill->ill_ppa == UINT_MAX);
16457 
16458 	ill->ill_defend_start = ill->ill_defend_count = 0;
16459 	/* The ppa is sent down by ifconfig or is chosen */
16460 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16461 		return (EINVAL);
16462 	}
16463 
16464 	/*
16465 	 * make sure ppa passed in is same as ppa in the name.
16466 	 * This check is not made when ppa == UINT_MAX in that case ppa
16467 	 * in the name could be anything. System will choose a ppa and
16468 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16469 	 */
16470 	if (*new_ppa_ptr != UINT_MAX) {
16471 		/* stoi changes the pointer */
16472 		old_ptr = ppa_ptr;
16473 		/*
16474 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16475 		 * (they don't have an externally visible ppa).  We assign one
16476 		 * here so that we can manage the interface.  Note that in
16477 		 * the past this value was always 0 for DLPI 1 drivers.
16478 		 */
16479 		if (*new_ppa_ptr == 0)
16480 			*new_ppa_ptr = stoi(&old_ptr);
16481 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16482 			return (EINVAL);
16483 	}
16484 	/*
16485 	 * terminate string before ppa
16486 	 * save char at that location.
16487 	 */
16488 	old_char = ppa_ptr[0];
16489 	ppa_ptr[0] = '\0';
16490 
16491 	ill->ill_ppa = *new_ppa_ptr;
16492 	/*
16493 	 * Finish as much work now as possible before calling ill_glist_insert
16494 	 * which makes the ill globally visible and also merges it with the
16495 	 * other protocol instance of this phyint. The remaining work is
16496 	 * done after entering the ipsq which may happen sometime later.
16497 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16498 	 */
16499 	ipif = ill->ill_ipif;
16500 
16501 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16502 	ipif_assign_seqid(ipif);
16503 
16504 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16505 		ill->ill_flags |= ILLF_IPV4;
16506 
16507 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16508 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16509 
16510 	if (ill->ill_flags & ILLF_IPV6) {
16511 
16512 		ill->ill_isv6 = B_TRUE;
16513 		ill_set_inputfn(ill);
16514 		if (ill->ill_rq != NULL) {
16515 			ill->ill_rq->q_qinfo = &iprinitv6;
16516 		}
16517 
16518 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16519 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16520 		ipif->ipif_v6subnet = ipv6_all_zeros;
16521 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16522 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16523 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16524 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16525 		/*
16526 		 * point-to-point or Non-mulicast capable
16527 		 * interfaces won't do NUD unless explicitly
16528 		 * configured to do so.
16529 		 */
16530 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16531 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16532 			ill->ill_flags |= ILLF_NONUD;
16533 		}
16534 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16535 		if (ill->ill_flags & ILLF_NOARP) {
16536 			/*
16537 			 * Note: xresolv interfaces will eventually need
16538 			 * NOARP set here as well, but that will require
16539 			 * those external resolvers to have some
16540 			 * knowledge of that flag and act appropriately.
16541 			 * Not to be changed at present.
16542 			 */
16543 			ill->ill_flags &= ~ILLF_NOARP;
16544 		}
16545 		/*
16546 		 * Set the ILLF_ROUTER flag according to the global
16547 		 * IPv6 forwarding policy.
16548 		 */
16549 		if (ipst->ips_ipv6_forward != 0)
16550 			ill->ill_flags |= ILLF_ROUTER;
16551 	} else if (ill->ill_flags & ILLF_IPV4) {
16552 		ill->ill_isv6 = B_FALSE;
16553 		ill_set_inputfn(ill);
16554 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16555 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16556 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16557 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16558 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16559 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16560 		/*
16561 		 * Set the ILLF_ROUTER flag according to the global
16562 		 * IPv4 forwarding policy.
16563 		 */
16564 		if (ipst->ips_ip_g_forward != 0)
16565 			ill->ill_flags |= ILLF_ROUTER;
16566 	}
16567 
16568 	ASSERT(ill->ill_phyint != NULL);
16569 
16570 	/*
16571 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16572 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16573 	 */
16574 	if (!ill_allocate_mibs(ill))
16575 		return (ENOMEM);
16576 
16577 	/*
16578 	 * Pick a default sap until we get the DL_INFO_ACK back from
16579 	 * the driver.
16580 	 */
16581 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16582 	    ill->ill_media->ip_m_ipv4sap;
16583 
16584 	ill->ill_ifname_pending = 1;
16585 	ill->ill_ifname_pending_err = 0;
16586 
16587 	/*
16588 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16589 	 * that were joined while this ill was not bound to the DLPI link need
16590 	 * to be recovered by ill_recover_multicast().
16591 	 */
16592 	ill->ill_need_recover_multicast = 1;
16593 
16594 	ill_refhold(ill);
16595 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16596 	if ((error = ill_glist_insert(ill, interf_name,
16597 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16598 		ill->ill_ppa = UINT_MAX;
16599 		ill->ill_name[0] = '\0';
16600 		/*
16601 		 * undo null termination done above.
16602 		 */
16603 		ppa_ptr[0] = old_char;
16604 		rw_exit(&ipst->ips_ill_g_lock);
16605 		ill_refrele(ill);
16606 		return (error);
16607 	}
16608 
16609 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16610 
16611 	/*
16612 	 * When we return the buffer pointed to by interf_name should contain
16613 	 * the same name as in ill_name.
16614 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16615 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16616 	 * so copy full name and update the ppa ptr.
16617 	 * When ppa passed in != UINT_MAX all values are correct just undo
16618 	 * null termination, this saves a bcopy.
16619 	 */
16620 	if (*new_ppa_ptr == UINT_MAX) {
16621 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16622 		*new_ppa_ptr = ill->ill_ppa;
16623 	} else {
16624 		/*
16625 		 * undo null termination done above.
16626 		 */
16627 		ppa_ptr[0] = old_char;
16628 	}
16629 
16630 	/* Let SCTP know about this ILL */
16631 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16632 
16633 	/*
16634 	 * ill_glist_insert has made the ill visible globally, and
16635 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16636 	 * we need to hold the ips_ill_g_lock across the call to enter the
16637 	 * ipsq to enforce atomicity and prevent reordering. In the event
16638 	 * the ipsq has changed, and if the new ipsq is currently busy,
16639 	 * we need to make sure that this half-completed ioctl is ahead of
16640 	 * any subsequent ioctl. We achieve this by not dropping the
16641 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16642 	 * ensuring that new ioctls can't start.
16643 	 */
16644 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16645 	    B_TRUE);
16646 
16647 	rw_exit(&ipst->ips_ill_g_lock);
16648 	ill_refrele(ill);
16649 	if (ipsq == NULL)
16650 		return (EINPROGRESS);
16651 
16652 	/*
16653 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16654 	 */
16655 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16656 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16657 	else
16658 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16659 
16660 	error = ipif_set_values_tail(ill, ipif, mp, q);
16661 	ipsq_exit(ipsq);
16662 	if (error != 0 && error != EINPROGRESS) {
16663 		/*
16664 		 * restore previous values
16665 		 */
16666 		ill->ill_isv6 = B_FALSE;
16667 		ill_set_inputfn(ill);
16668 	}
16669 	return (error);
16670 }
16671 
16672 void
16673 ipif_init(ip_stack_t *ipst)
16674 {
16675 	int i;
16676 
16677 	for (i = 0; i < MAX_G_HEADS; i++) {
16678 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16679 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16680 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16681 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16682 	}
16683 
16684 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16685 	    ill_phyint_compare_index,
16686 	    sizeof (phyint_t),
16687 	    offsetof(struct phyint, phyint_avl_by_index));
16688 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16689 	    ill_phyint_compare_name,
16690 	    sizeof (phyint_t),
16691 	    offsetof(struct phyint, phyint_avl_by_name));
16692 }
16693 
16694 /*
16695  * Save enough information so that we can recreate the IRE if
16696  * the interface goes down and then up.
16697  */
16698 void
16699 ill_save_ire(ill_t *ill, ire_t *ire)
16700 {
16701 	mblk_t	*save_mp;
16702 
16703 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16704 	if (save_mp != NULL) {
16705 		ifrt_t	*ifrt;
16706 
16707 		save_mp->b_wptr += sizeof (ifrt_t);
16708 		ifrt = (ifrt_t *)save_mp->b_rptr;
16709 		bzero(ifrt, sizeof (ifrt_t));
16710 		ifrt->ifrt_type = ire->ire_type;
16711 		if (ire->ire_ipversion == IPV4_VERSION) {
16712 			ASSERT(!ill->ill_isv6);
16713 			ifrt->ifrt_addr = ire->ire_addr;
16714 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16715 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16716 			ifrt->ifrt_mask = ire->ire_mask;
16717 		} else {
16718 			ASSERT(ill->ill_isv6);
16719 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16720 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16721 			mutex_enter(&ire->ire_lock);
16722 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16723 			mutex_exit(&ire->ire_lock);
16724 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16725 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16726 		}
16727 		ifrt->ifrt_flags = ire->ire_flags;
16728 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16729 		mutex_enter(&ill->ill_saved_ire_lock);
16730 		save_mp->b_cont = ill->ill_saved_ire_mp;
16731 		ill->ill_saved_ire_mp = save_mp;
16732 		ill->ill_saved_ire_cnt++;
16733 		mutex_exit(&ill->ill_saved_ire_lock);
16734 	}
16735 }
16736 
16737 /*
16738  * Remove one entry from ill_saved_ire_mp.
16739  */
16740 void
16741 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16742 {
16743 	mblk_t	**mpp;
16744 	mblk_t	*mp;
16745 	ifrt_t	*ifrt;
16746 
16747 	/* Remove from ill_saved_ire_mp list if it is there */
16748 	mutex_enter(&ill->ill_saved_ire_lock);
16749 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16750 	    mpp = &(*mpp)->b_cont) {
16751 		in6_addr_t	gw_addr_v6;
16752 
16753 		/*
16754 		 * On a given ill, the tuple of address, gateway, mask,
16755 		 * ire_type, and zoneid is unique for each saved IRE.
16756 		 */
16757 		mp = *mpp;
16758 		ifrt = (ifrt_t *)mp->b_rptr;
16759 		/* ire_gateway_addr_v6 can change - need lock */
16760 		mutex_enter(&ire->ire_lock);
16761 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16762 		mutex_exit(&ire->ire_lock);
16763 
16764 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16765 		    ifrt->ifrt_type != ire->ire_type)
16766 			continue;
16767 
16768 		if (ill->ill_isv6 ?
16769 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16770 		    &ire->ire_addr_v6) &&
16771 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16772 		    &gw_addr_v6) &&
16773 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16774 		    &ire->ire_mask_v6)) :
16775 		    (ifrt->ifrt_addr == ire->ire_addr &&
16776 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16777 		    ifrt->ifrt_mask == ire->ire_mask)) {
16778 			*mpp = mp->b_cont;
16779 			ill->ill_saved_ire_cnt--;
16780 			freeb(mp);
16781 			break;
16782 		}
16783 	}
16784 	mutex_exit(&ill->ill_saved_ire_lock);
16785 }
16786 
16787 /*
16788  * IP multirouting broadcast routes handling
16789  * Append CGTP broadcast IREs to regular ones created
16790  * at ifconfig time.
16791  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16792  * the destination and the gateway are broadcast addresses.
16793  * The caller has verified that the destination is an IRE_BROADCAST and that
16794  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16795  * we create a MULTIRT IRE_BROADCAST.
16796  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16797  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16798  */
16799 static void
16800 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16801 {
16802 	ire_t *ire_prim;
16803 
16804 	ASSERT(ire != NULL);
16805 
16806 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16807 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16808 	    NULL);
16809 	if (ire_prim != NULL) {
16810 		/*
16811 		 * We are in the special case of broadcasts for
16812 		 * CGTP. We add an IRE_BROADCAST that holds
16813 		 * the RTF_MULTIRT flag, the destination
16814 		 * address and the low level
16815 		 * info of ire_prim. In other words, CGTP
16816 		 * broadcast is added to the redundant ipif.
16817 		 */
16818 		ill_t *ill_prim;
16819 		ire_t  *bcast_ire;
16820 
16821 		ill_prim = ire_prim->ire_ill;
16822 
16823 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16824 		    (void *)ire_prim, (void *)ill_prim));
16825 
16826 		bcast_ire = ire_create(
16827 		    (uchar_t *)&ire->ire_addr,
16828 		    (uchar_t *)&ip_g_all_ones,
16829 		    (uchar_t *)&ire->ire_gateway_addr,
16830 		    IRE_BROADCAST,
16831 		    ill_prim,
16832 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16833 		    ire->ire_flags | RTF_KERNEL,
16834 		    NULL,
16835 		    ipst);
16836 
16837 		/*
16838 		 * Here we assume that ire_add does head insertion so that
16839 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16840 		 */
16841 		if (bcast_ire != NULL) {
16842 			if (ire->ire_flags & RTF_SETSRC) {
16843 				bcast_ire->ire_setsrc_addr =
16844 				    ire->ire_setsrc_addr;
16845 			}
16846 			bcast_ire = ire_add(bcast_ire);
16847 			if (bcast_ire != NULL) {
16848 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16849 				    "added bcast_ire %p\n",
16850 				    (void *)bcast_ire));
16851 
16852 				ill_save_ire(ill_prim, bcast_ire);
16853 				ire_refrele(bcast_ire);
16854 			}
16855 		}
16856 		ire_refrele(ire_prim);
16857 	}
16858 }
16859 
16860 /*
16861  * IP multirouting broadcast routes handling
16862  * Remove the broadcast ire.
16863  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16864  * the destination and the gateway are broadcast addresses.
16865  * The caller has only verified that RTF_MULTIRT was set. We check
16866  * that the destination is broadcast and that the gateway is a broadcast
16867  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16868  */
16869 static void
16870 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16871 {
16872 	ASSERT(ire != NULL);
16873 
16874 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16875 		ire_t *ire_prim;
16876 
16877 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16878 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16879 		    ipst, NULL);
16880 		if (ire_prim != NULL) {
16881 			ill_t *ill_prim;
16882 			ire_t  *bcast_ire;
16883 
16884 			ill_prim = ire_prim->ire_ill;
16885 
16886 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16887 			    "ire_prim %p, ill_prim %p\n",
16888 			    (void *)ire_prim, (void *)ill_prim));
16889 
16890 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16891 			    ire->ire_gateway_addr, IRE_BROADCAST,
16892 			    ill_prim, ALL_ZONES, NULL,
16893 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16894 			    MATCH_IRE_MASK, 0, ipst, NULL);
16895 
16896 			if (bcast_ire != NULL) {
16897 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16898 				    "looked up bcast_ire %p\n",
16899 				    (void *)bcast_ire));
16900 				ill_remove_saved_ire(bcast_ire->ire_ill,
16901 				    bcast_ire);
16902 				ire_delete(bcast_ire);
16903 				ire_refrele(bcast_ire);
16904 			}
16905 			ire_refrele(ire_prim);
16906 		}
16907 	}
16908 }
16909 
16910 /*
16911  * Derive an interface id from the link layer address.
16912  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16913  */
16914 static void
16915 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16916 {
16917 	char		*addr;
16918 
16919 	/*
16920 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16921 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16922 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16923 	 * interface ID on IPv6 interfaces above links that actually have real
16924 	 * Ethernet addresses.
16925 	 */
16926 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16927 		/* Form EUI-64 like address */
16928 		addr = (char *)&v6addr->s6_addr32[2];
16929 		bcopy(ill->ill_phys_addr, addr, 3);
16930 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16931 		addr[3] = (char)0xff;
16932 		addr[4] = (char)0xfe;
16933 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16934 	}
16935 }
16936 
16937 /* ARGSUSED */
16938 static void
16939 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16940 {
16941 }
16942 
16943 typedef struct ipmp_ifcookie {
16944 	uint32_t	ic_hostid;
16945 	char		ic_ifname[LIFNAMSIZ];
16946 	char		ic_zonename[ZONENAME_MAX];
16947 } ipmp_ifcookie_t;
16948 
16949 /*
16950  * Construct a pseudo-random interface ID for the IPMP interface that's both
16951  * predictable and (almost) guaranteed to be unique.
16952  */
16953 static void
16954 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16955 {
16956 	zone_t		*zp;
16957 	uint8_t		*addr;
16958 	uchar_t		hash[16];
16959 	ulong_t 	hostid;
16960 	MD5_CTX		ctx;
16961 	ipmp_ifcookie_t	ic = { 0 };
16962 
16963 	ASSERT(IS_IPMP(ill));
16964 
16965 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16966 	ic.ic_hostid = htonl((uint32_t)hostid);
16967 
16968 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16969 
16970 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16971 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16972 		zone_rele(zp);
16973 	}
16974 
16975 	MD5Init(&ctx);
16976 	MD5Update(&ctx, &ic, sizeof (ic));
16977 	MD5Final(hash, &ctx);
16978 
16979 	/*
16980 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16981 	 */
16982 	addr = &v6addr->s6_addr8[8];
16983 	bcopy(hash + 8, addr, sizeof (uint64_t));
16984 	addr[0] &= ~0x2;				/* set local bit */
16985 }
16986 
16987 /*
16988  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16989  */
16990 static void
16991 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16992 {
16993 	phyint_t *phyi = ill->ill_phyint;
16994 
16995 	/*
16996 	 * Check PHYI_MULTI_BCAST and length of physical
16997 	 * address to determine if we use the mapping or the
16998 	 * broadcast address.
16999 	 */
17000 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17001 	    ill->ill_phys_addr_length != ETHERADDRL) {
17002 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17003 		return;
17004 	}
17005 	m_physaddr[0] = 0x33;
17006 	m_physaddr[1] = 0x33;
17007 	m_physaddr[2] = m_ip6addr[12];
17008 	m_physaddr[3] = m_ip6addr[13];
17009 	m_physaddr[4] = m_ip6addr[14];
17010 	m_physaddr[5] = m_ip6addr[15];
17011 }
17012 
17013 /*
17014  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17015  */
17016 static void
17017 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17018 {
17019 	phyint_t *phyi = ill->ill_phyint;
17020 
17021 	/*
17022 	 * Check PHYI_MULTI_BCAST and length of physical
17023 	 * address to determine if we use the mapping or the
17024 	 * broadcast address.
17025 	 */
17026 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17027 	    ill->ill_phys_addr_length != ETHERADDRL) {
17028 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17029 		return;
17030 	}
17031 	m_physaddr[0] = 0x01;
17032 	m_physaddr[1] = 0x00;
17033 	m_physaddr[2] = 0x5e;
17034 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17035 	m_physaddr[4] = m_ipaddr[2];
17036 	m_physaddr[5] = m_ipaddr[3];
17037 }
17038 
17039 /* ARGSUSED */
17040 static void
17041 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17042 {
17043 	/*
17044 	 * for the MULTI_BCAST case and other cases when we want to
17045 	 * use the link-layer broadcast address for multicast.
17046 	 */
17047 	uint8_t	*bphys_addr;
17048 	dl_unitdata_req_t *dlur;
17049 
17050 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17051 	if (ill->ill_sap_length < 0) {
17052 		bphys_addr = (uchar_t *)dlur +
17053 		    dlur->dl_dest_addr_offset;
17054 	} else  {
17055 		bphys_addr = (uchar_t *)dlur +
17056 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17057 	}
17058 
17059 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17060 }
17061 
17062 /*
17063  * Derive IPoIB interface id from the link layer address.
17064  */
17065 static void
17066 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17067 {
17068 	char		*addr;
17069 
17070 	ASSERT(ill->ill_phys_addr_length == 20);
17071 	addr = (char *)&v6addr->s6_addr32[2];
17072 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17073 	/*
17074 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17075 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17076 	 * rules. In these cases, the IBA considers these GUIDs to be in
17077 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17078 	 * required; vendors are required not to assign global EUI-64's
17079 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17080 	 * of the interface identifier. Whether the GUID is in modified
17081 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17082 	 * bit set to 1.
17083 	 */
17084 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17085 }
17086 
17087 /*
17088  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17089  * Note on mapping from multicast IP addresses to IPoIB multicast link
17090  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17091  * The format of an IPoIB multicast address is:
17092  *
17093  *  4 byte QPN      Scope Sign.  Pkey
17094  * +--------------------------------------------+
17095  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17096  * +--------------------------------------------+
17097  *
17098  * The Scope and Pkey components are properties of the IBA port and
17099  * network interface. They can be ascertained from the broadcast address.
17100  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17101  */
17102 static void
17103 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17104 {
17105 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17106 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17107 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17108 	uint8_t	*bphys_addr;
17109 	dl_unitdata_req_t *dlur;
17110 
17111 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17112 
17113 	/*
17114 	 * RFC 4391: IPv4 MGID is 28-bit long.
17115 	 */
17116 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17117 	m_physaddr[17] = m_ipaddr[1];
17118 	m_physaddr[18] = m_ipaddr[2];
17119 	m_physaddr[19] = m_ipaddr[3];
17120 
17121 
17122 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17123 	if (ill->ill_sap_length < 0) {
17124 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17125 	} else  {
17126 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17127 		    ill->ill_sap_length;
17128 	}
17129 	/*
17130 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17131 	 */
17132 	m_physaddr[5] = bphys_addr[5];
17133 	m_physaddr[8] = bphys_addr[8];
17134 	m_physaddr[9] = bphys_addr[9];
17135 }
17136 
17137 static void
17138 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17139 {
17140 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17141 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17142 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17143 	uint8_t	*bphys_addr;
17144 	dl_unitdata_req_t *dlur;
17145 
17146 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17147 
17148 	/*
17149 	 * RFC 4391: IPv4 MGID is 80-bit long.
17150 	 */
17151 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17152 
17153 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17154 	if (ill->ill_sap_length < 0) {
17155 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17156 	} else  {
17157 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17158 		    ill->ill_sap_length;
17159 	}
17160 	/*
17161 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17162 	 */
17163 	m_physaddr[5] = bphys_addr[5];
17164 	m_physaddr[8] = bphys_addr[8];
17165 	m_physaddr[9] = bphys_addr[9];
17166 }
17167 
17168 /*
17169  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17170  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17171  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17172  * of RFC4213.
17173  */
17174 static void
17175 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17176 {
17177 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17178 	v6addr->s6_addr32[2] = 0;
17179 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17180 }
17181 
17182 /*
17183  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17184  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17185  * id.
17186  */
17187 static void
17188 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17189 {
17190 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17191 
17192 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17193 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17194 }
17195 
17196 static void
17197 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17198 {
17199 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17200 }
17201 
17202 static void
17203 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17204 {
17205 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17206 }
17207 
17208 static void
17209 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17210 {
17211 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17212 }
17213 
17214 static void
17215 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17216 {
17217 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17218 }
17219 
17220 /*
17221  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17222  * Returns an held ill, or NULL.
17223  */
17224 ill_t *
17225 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17226     ip_stack_t *ipst)
17227 {
17228 	ill_t	*ill;
17229 	ipif_t	*ipif;
17230 
17231 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17232 	if (ill == NULL)
17233 		return (NULL);
17234 
17235 	mutex_enter(&ill->ill_lock);
17236 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17237 		if (IPIF_IS_CONDEMNED(ipif))
17238 			continue;
17239 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17240 		    ipif->ipif_zoneid != ALL_ZONES)
17241 			continue;
17242 
17243 		mutex_exit(&ill->ill_lock);
17244 		return (ill);
17245 	}
17246 	mutex_exit(&ill->ill_lock);
17247 	ill_refrele(ill);
17248 	return (NULL);
17249 }
17250 
17251 /*
17252  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17253  * If a pointer to an ipif_t is returned then the caller will need to do
17254  * an ill_refrele().
17255  */
17256 ipif_t *
17257 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17258     ip_stack_t *ipst)
17259 {
17260 	ipif_t *ipif;
17261 	ill_t *ill;
17262 
17263 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17264 	if (ill == NULL)
17265 		return (NULL);
17266 
17267 	mutex_enter(&ill->ill_lock);
17268 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17269 		mutex_exit(&ill->ill_lock);
17270 		ill_refrele(ill);
17271 		return (NULL);
17272 	}
17273 
17274 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17275 		if (!IPIF_CAN_LOOKUP(ipif))
17276 			continue;
17277 		if (lifidx == ipif->ipif_id) {
17278 			ipif_refhold_locked(ipif);
17279 			break;
17280 		}
17281 	}
17282 
17283 	mutex_exit(&ill->ill_lock);
17284 	ill_refrele(ill);
17285 	return (ipif);
17286 }
17287 
17288 /*
17289  * Set ill_inputfn based on the current know state.
17290  * This needs to be called when any of the factors taken into
17291  * account changes.
17292  */
17293 void
17294 ill_set_inputfn(ill_t *ill)
17295 {
17296 	ip_stack_t	*ipst = ill->ill_ipst;
17297 
17298 	if (ill->ill_isv6) {
17299 		if (is_system_labeled())
17300 			ill->ill_inputfn = ill_input_full_v6;
17301 		else
17302 			ill->ill_inputfn = ill_input_short_v6;
17303 	} else {
17304 		if (is_system_labeled())
17305 			ill->ill_inputfn = ill_input_full_v4;
17306 		else if (ill->ill_dhcpinit != 0)
17307 			ill->ill_inputfn = ill_input_full_v4;
17308 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17309 		    != NULL)
17310 			ill->ill_inputfn = ill_input_full_v4;
17311 		else if (ipst->ips_ip_cgtp_filter &&
17312 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17313 			ill->ill_inputfn = ill_input_full_v4;
17314 		else
17315 			ill->ill_inputfn = ill_input_short_v4;
17316 	}
17317 }
17318 
17319 /*
17320  * Re-evaluate ill_inputfn for all the IPv4 ills.
17321  * Used when RSVP and CGTP comes and goes.
17322  */
17323 void
17324 ill_set_inputfn_all(ip_stack_t *ipst)
17325 {
17326 	ill_walk_context_t	ctx;
17327 	ill_t			*ill;
17328 
17329 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17330 	ill = ILL_START_WALK_V4(&ctx, ipst);
17331 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17332 		ill_set_inputfn(ill);
17333 
17334 	rw_exit(&ipst->ips_ill_g_lock);
17335 }
17336 
17337 /*
17338  * Set the physical address information for `ill' to the contents of the
17339  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17340  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17341  * EINPROGRESS will be returned.
17342  */
17343 int
17344 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17345 {
17346 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17347 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17348 
17349 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17350 
17351 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17352 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17353 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17354 		/* Changing DL_IPV6_TOKEN is not yet supported */
17355 		return (0);
17356 	}
17357 
17358 	/*
17359 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17360 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17361 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17362 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17363 	 */
17364 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17365 		freemsg(mp);
17366 		return (ENOMEM);
17367 	}
17368 
17369 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17370 	mutex_enter(&ill->ill_lock);
17371 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17372 	/* no more nce addition allowed */
17373 	mutex_exit(&ill->ill_lock);
17374 
17375 	/*
17376 	 * If we can quiesce the ill, then set the address.  If not, then
17377 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17378 	 */
17379 	ill_down_ipifs(ill, B_TRUE);
17380 	mutex_enter(&ill->ill_lock);
17381 	if (!ill_is_quiescent(ill)) {
17382 		/* call cannot fail since `conn_t *' argument is NULL */
17383 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17384 		    mp, ILL_DOWN);
17385 		mutex_exit(&ill->ill_lock);
17386 		return (EINPROGRESS);
17387 	}
17388 	mutex_exit(&ill->ill_lock);
17389 
17390 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17391 	return (0);
17392 }
17393 
17394 /*
17395  * Once the ill associated with `q' has quiesced, set its physical address
17396  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17397  * are passed (linked by b_cont), since we sometimes need to save two distinct
17398  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17399  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17400  * is quiesced, we know any stale nce's with the old address information have
17401  * already been removed, so we don't need to call nce_flush().
17402  */
17403 /* ARGSUSED */
17404 static void
17405 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17406 {
17407 	ill_t		*ill = q->q_ptr;
17408 	mblk_t		*addrmp2 = unlinkb(addrmp);
17409 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17410 	uint_t		addrlen, addroff;
17411 	int		status;
17412 
17413 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17414 
17415 	addroff	= dlindp->dl_addr_offset;
17416 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17417 
17418 	switch (dlindp->dl_data) {
17419 	case DL_IPV6_LINK_LAYER_ADDR:
17420 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17421 		freemsg(addrmp2);
17422 		break;
17423 
17424 	case DL_CURR_DEST_ADDR:
17425 		freemsg(ill->ill_dest_addr_mp);
17426 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17427 		ill->ill_dest_addr_mp = addrmp;
17428 		if (ill->ill_isv6) {
17429 			ill_setdesttoken(ill);
17430 			ipif_setdestlinklocal(ill->ill_ipif);
17431 		}
17432 		freemsg(addrmp2);
17433 		break;
17434 
17435 	case DL_CURR_PHYS_ADDR:
17436 		freemsg(ill->ill_phys_addr_mp);
17437 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17438 		ill->ill_phys_addr_mp = addrmp;
17439 		ill->ill_phys_addr_length = addrlen;
17440 		if (ill->ill_isv6)
17441 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17442 		else
17443 			freemsg(addrmp2);
17444 		if (ill->ill_isv6) {
17445 			ill_setdefaulttoken(ill);
17446 			ipif_setlinklocal(ill->ill_ipif);
17447 		}
17448 		break;
17449 	default:
17450 		ASSERT(0);
17451 	}
17452 
17453 	/*
17454 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17455 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17456 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17457 	 * brought up.
17458 	 */
17459 	status = ill_up_ipifs(ill, q, addrmp);
17460 	mutex_enter(&ill->ill_lock);
17461 	if (ill->ill_dl_up)
17462 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17463 	mutex_exit(&ill->ill_lock);
17464 	if (status != EINPROGRESS)
17465 		ipsq_current_finish(ipsq);
17466 }
17467 
17468 /*
17469  * Helper routine for setting the ill_nd_lla fields.
17470  */
17471 void
17472 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17473 {
17474 	freemsg(ill->ill_nd_lla_mp);
17475 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17476 	ill->ill_nd_lla_mp = ndmp;
17477 	ill->ill_nd_lla_len = addrlen;
17478 }
17479 
17480 /*
17481  * Replumb the ill.
17482  */
17483 int
17484 ill_replumb(ill_t *ill, mblk_t *mp)
17485 {
17486 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17487 
17488 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17489 
17490 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17491 
17492 	mutex_enter(&ill->ill_lock);
17493 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17494 	/* no more nce addition allowed */
17495 	mutex_exit(&ill->ill_lock);
17496 
17497 	/*
17498 	 * If we can quiesce the ill, then continue.  If not, then
17499 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17500 	 */
17501 	ill_down_ipifs(ill, B_FALSE);
17502 
17503 	mutex_enter(&ill->ill_lock);
17504 	if (!ill_is_quiescent(ill)) {
17505 		/* call cannot fail since `conn_t *' argument is NULL */
17506 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17507 		    mp, ILL_DOWN);
17508 		mutex_exit(&ill->ill_lock);
17509 		return (EINPROGRESS);
17510 	}
17511 	mutex_exit(&ill->ill_lock);
17512 
17513 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17514 	return (0);
17515 }
17516 
17517 /* ARGSUSED */
17518 static void
17519 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17520 {
17521 	ill_t *ill = q->q_ptr;
17522 	int err;
17523 	conn_t *connp = NULL;
17524 
17525 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17526 	freemsg(ill->ill_replumb_mp);
17527 	ill->ill_replumb_mp = copyb(mp);
17528 
17529 	if (ill->ill_replumb_mp == NULL) {
17530 		/* out of memory */
17531 		ipsq_current_finish(ipsq);
17532 		return;
17533 	}
17534 
17535 	mutex_enter(&ill->ill_lock);
17536 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17537 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17538 	mutex_exit(&ill->ill_lock);
17539 
17540 	if (!ill->ill_up_ipifs) {
17541 		/* already closing */
17542 		ipsq_current_finish(ipsq);
17543 		return;
17544 	}
17545 	ill->ill_replumbing = 1;
17546 	err = ill_down_ipifs_tail(ill);
17547 
17548 	/*
17549 	 * Successfully quiesced and brought down the interface, now we send
17550 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17551 	 * DL_NOTE_REPLUMB message.
17552 	 */
17553 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17554 	    DL_NOTIFY_CONF);
17555 	ASSERT(mp != NULL);
17556 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17557 	    DL_NOTE_REPLUMB_DONE;
17558 	ill_dlpi_send(ill, mp);
17559 
17560 	/*
17561 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17562 	 * streams have to be unbound. When all the DLPI exchanges are done,
17563 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17564 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17565 	 * arp_bringup_done().
17566 	 */
17567 	ASSERT(ill->ill_replumb_mp != NULL);
17568 	if (err == EINPROGRESS)
17569 		return;
17570 	else
17571 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17572 	ASSERT(connp == NULL);
17573 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17574 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17575 		return;
17576 	}
17577 	ipsq_current_finish(ipsq);
17578 }
17579 
17580 /*
17581  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17582  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17583  * as per the ioctl.  On failure, an errno is returned.
17584  */
17585 static int
17586 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17587 {
17588 	int rval;
17589 	struct strioctl iocb;
17590 
17591 	iocb.ic_cmd = cmd;
17592 	iocb.ic_timout = 15;
17593 	iocb.ic_len = bufsize;
17594 	iocb.ic_dp = buf;
17595 
17596 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17597 }
17598 
17599 /*
17600  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17601  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17602  */
17603 static int
17604 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17605     uint_t *bufsizep, cred_t *cr)
17606 {
17607 	int err;
17608 	struct lifnum lifn;
17609 
17610 	bzero(&lifn, sizeof (lifn));
17611 	lifn.lifn_family = af;
17612 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17613 
17614 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17615 		return (err);
17616 
17617 	/*
17618 	 * Pad the interface count to account for additional interfaces that
17619 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17620 	 */
17621 	lifn.lifn_count += 4;
17622 	bzero(lifcp, sizeof (*lifcp));
17623 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17624 	lifcp->lifc_family = af;
17625 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17626 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17627 
17628 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17629 	if (err != 0) {
17630 		kmem_free(lifcp->lifc_buf, *bufsizep);
17631 		return (err);
17632 	}
17633 
17634 	return (0);
17635 }
17636 
17637 /*
17638  * Helper for ip_interface_cleanup() that removes the loopback interface.
17639  */
17640 static void
17641 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17642 {
17643 	int err;
17644 	struct lifreq lifr;
17645 
17646 	bzero(&lifr, sizeof (lifr));
17647 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17648 
17649 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17650 	if (err != 0) {
17651 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17652 		    "error %d\n", isv6 ? "v6" : "v4", err));
17653 	}
17654 }
17655 
17656 /*
17657  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17658  * groups and that IPMP data addresses are down.  These conditions must be met
17659  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17660  */
17661 static void
17662 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17663 {
17664 	int af = isv6 ? AF_INET6 : AF_INET;
17665 	int i, nifs;
17666 	int err;
17667 	uint_t bufsize;
17668 	uint_t lifrsize = sizeof (struct lifreq);
17669 	struct lifconf lifc;
17670 	struct lifreq *lifrp;
17671 
17672 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17673 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17674 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17675 		return;
17676 	}
17677 
17678 	nifs = lifc.lifc_len / lifrsize;
17679 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17680 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17681 		if (err != 0) {
17682 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17683 			    "flags: error %d", lifrp->lifr_name, err);
17684 			continue;
17685 		}
17686 
17687 		if (lifrp->lifr_flags & IFF_IPMP) {
17688 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17689 				continue;
17690 
17691 			lifrp->lifr_flags &= ~IFF_UP;
17692 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17693 			if (err != 0) {
17694 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17695 				    "bring down (error %d); IPMP interface may "
17696 				    "not be shutdown", lifrp->lifr_name, err);
17697 			}
17698 
17699 			/*
17700 			 * Check if IFF_DUPLICATE is still set -- and if so,
17701 			 * reset the address to clear it.
17702 			 */
17703 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17704 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17705 				continue;
17706 
17707 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17708 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17709 			    lifrp, lifrsize, cr)) != 0) {
17710 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17711 				    "reset DAD (error %d); IPMP interface may "
17712 				    "not be shutdown", lifrp->lifr_name, err);
17713 			}
17714 			continue;
17715 		}
17716 
17717 		lifrp->lifr_groupname[0] = '\0';
17718 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17719 		if (err != 0) {
17720 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17721 			    "IPMP group (error %d); associated IPMP interface "
17722 			    "may not be shutdown", lifrp->lifr_name, err);
17723 			continue;
17724 		}
17725 	}
17726 
17727 	kmem_free(lifc.lifc_buf, bufsize);
17728 }
17729 
17730 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17731 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17732 
17733 /*
17734  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17735  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17736  * when the user-level processes in the zone are killed and the latter are
17737  * cleaned up by str_stack_shutdown().
17738  */
17739 void
17740 ip_interface_cleanup(ip_stack_t *ipst)
17741 {
17742 	ldi_handle_t	lh;
17743 	ldi_ident_t	li;
17744 	cred_t		*cr;
17745 	int		err;
17746 	int		i;
17747 	char		*devs[] = { UDP6DEV, UDPDEV };
17748 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17749 
17750 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17751 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17752 		    " error %d", err);
17753 		return;
17754 	}
17755 
17756 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17757 	ASSERT(cr != NULL);
17758 
17759 	/*
17760 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17761 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17762 	 * the loop.)
17763 	 */
17764 	for (i = 0; i < 2; i++) {
17765 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17766 		if (err != 0) {
17767 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17768 			    " error %d", devs[i], err);
17769 			continue;
17770 		}
17771 
17772 		ip_loopback_removeif(lh, i == 0, cr);
17773 		ip_ipmp_cleanup(lh, i == 0, cr);
17774 
17775 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17776 	}
17777 
17778 	ldi_ident_release(li);
17779 	crfree(cr);
17780 }
17781 
17782 /*
17783  * This needs to be in-sync with nic_event_t definition
17784  */
17785 static const char *
17786 ill_hook_event2str(nic_event_t event)
17787 {
17788 	switch (event) {
17789 	case NE_PLUMB:
17790 		return ("PLUMB");
17791 	case NE_UNPLUMB:
17792 		return ("UNPLUMB");
17793 	case NE_UP:
17794 		return ("UP");
17795 	case NE_DOWN:
17796 		return ("DOWN");
17797 	case NE_ADDRESS_CHANGE:
17798 		return ("ADDRESS_CHANGE");
17799 	case NE_LIF_UP:
17800 		return ("LIF_UP");
17801 	case NE_LIF_DOWN:
17802 		return ("LIF_DOWN");
17803 	case NE_IFINDEX_CHANGE:
17804 		return ("IFINDEX_CHANGE");
17805 	default:
17806 		return ("UNKNOWN");
17807 	}
17808 }
17809 
17810 void
17811 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17812     nic_event_data_t data, size_t datalen)
17813 {
17814 	ip_stack_t		*ipst = ill->ill_ipst;
17815 	hook_nic_event_int_t	*info;
17816 	const char		*str = NULL;
17817 
17818 	/* create a new nic event info */
17819 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17820 		goto fail;
17821 
17822 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17823 	info->hnei_event.hne_lif = lif;
17824 	info->hnei_event.hne_event = event;
17825 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17826 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17827 	info->hnei_event.hne_data = NULL;
17828 	info->hnei_event.hne_datalen = 0;
17829 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17830 
17831 	if (data != NULL && datalen != 0) {
17832 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17833 		if (info->hnei_event.hne_data == NULL)
17834 			goto fail;
17835 		bcopy(data, info->hnei_event.hne_data, datalen);
17836 		info->hnei_event.hne_datalen = datalen;
17837 	}
17838 
17839 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17840 	    DDI_NOSLEEP) == DDI_SUCCESS)
17841 		return;
17842 
17843 fail:
17844 	if (info != NULL) {
17845 		if (info->hnei_event.hne_data != NULL) {
17846 			kmem_free(info->hnei_event.hne_data,
17847 			    info->hnei_event.hne_datalen);
17848 		}
17849 		kmem_free(info, sizeof (hook_nic_event_t));
17850 	}
17851 	str = ill_hook_event2str(event);
17852 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17853 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17854 }
17855 
17856 static int
17857 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17858 {
17859 	int		err = 0;
17860 	const in_addr_t	*addr = NULL;
17861 	nce_t		*nce = NULL;
17862 	ill_t		*ill = ipif->ipif_ill;
17863 	ill_t		*bound_ill;
17864 	boolean_t	added_ipif = B_FALSE;
17865 	uint16_t	state;
17866 	uint16_t	flags;
17867 
17868 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17869 	    ill_t *, ill, ipif_t *, ipif);
17870 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17871 		addr = &ipif->ipif_lcl_addr;
17872 	}
17873 
17874 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17875 		if (res_act != Res_act_initial)
17876 			return (EINVAL);
17877 	}
17878 
17879 	if (addr != NULL) {
17880 		ipmp_illgrp_t	*illg = ill->ill_grp;
17881 
17882 		/* add unicast nce for the local addr */
17883 
17884 		if (IS_IPMP(ill)) {
17885 			/*
17886 			 * If we're here via ipif_up(), then the ipif
17887 			 * won't be bound yet -- add it to the group,
17888 			 * which will bind it if possible. (We would
17889 			 * add it in ipif_up(), but deleting on failure
17890 			 * there is gruesome.)  If we're here via
17891 			 * ipmp_ill_bind_ipif(), then the ipif has
17892 			 * already been added to the group and we
17893 			 * just need to use the binding.
17894 			 */
17895 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17896 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17897 				if (bound_ill == NULL) {
17898 					/*
17899 					 * We couldn't bind the ipif to an ill
17900 					 * yet, so we have nothing to publish.
17901 					 * Mark the address as ready and return.
17902 					 */
17903 					ipif->ipif_addr_ready = 1;
17904 					return (0);
17905 				}
17906 				added_ipif = B_TRUE;
17907 			}
17908 		} else {
17909 			bound_ill = ill;
17910 		}
17911 
17912 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17913 		    NCE_F_NONUD);
17914 		/*
17915 		 * If this is an initial bring-up (or the ipif was never
17916 		 * completely brought up), do DAD.  Otherwise, we're here
17917 		 * because IPMP has rebound an address to this ill: send
17918 		 * unsolicited advertisements (ARP announcements) to
17919 		 * inform others.
17920 		 */
17921 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17922 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17923 		} else {
17924 			state = ND_REACHABLE;
17925 			flags |= NCE_F_UNSOL_ADV;
17926 		}
17927 
17928 retry:
17929 		err = nce_lookup_then_add_v4(ill,
17930 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17931 		    addr, flags, state, &nce);
17932 
17933 		/*
17934 		 * note that we may encounter EEXIST if we are moving
17935 		 * the nce as a result of a rebind operation.
17936 		 */
17937 		switch (err) {
17938 		case 0:
17939 			ipif->ipif_added_nce = 1;
17940 			nce->nce_ipif_cnt++;
17941 			break;
17942 		case EEXIST:
17943 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17944 			    ill->ill_name));
17945 			if (!NCE_MYADDR(nce->nce_common)) {
17946 				/*
17947 				 * A leftover nce from before this address
17948 				 * existed
17949 				 */
17950 				ncec_delete(nce->nce_common);
17951 				nce_refrele(nce);
17952 				nce = NULL;
17953 				goto retry;
17954 			}
17955 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17956 				nce_refrele(nce);
17957 				nce = NULL;
17958 				ip1dbg(("ipif_arp_up: NCE already exists "
17959 				    "for %s:%u\n", ill->ill_name,
17960 				    ipif->ipif_id));
17961 				goto arp_up_done;
17962 			}
17963 			/*
17964 			 * Duplicate local addresses are permissible for
17965 			 * IPIF_POINTOPOINT interfaces which will get marked
17966 			 * IPIF_UNNUMBERED later in
17967 			 * ip_addr_availability_check().
17968 			 *
17969 			 * The nce_ipif_cnt field tracks the number of
17970 			 * ipifs that have nce_addr as their local address.
17971 			 */
17972 			ipif->ipif_addr_ready = 1;
17973 			ipif->ipif_added_nce = 1;
17974 			nce->nce_ipif_cnt++;
17975 			err = 0;
17976 			break;
17977 		default:
17978 			ASSERT(nce == NULL);
17979 			goto arp_up_done;
17980 		}
17981 		if (arp_no_defense) {
17982 			if ((ipif->ipif_flags & IPIF_UP) &&
17983 			    !ipif->ipif_addr_ready)
17984 				ipif_up_notify(ipif);
17985 			ipif->ipif_addr_ready = 1;
17986 		}
17987 	} else {
17988 		/* zero address. nothing to publish */
17989 		ipif->ipif_addr_ready = 1;
17990 	}
17991 	if (nce != NULL)
17992 		nce_refrele(nce);
17993 arp_up_done:
17994 	if (added_ipif && err != 0)
17995 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17996 	return (err);
17997 }
17998 
17999 int
18000 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18001 {
18002 	int 		err = 0;
18003 	ill_t 		*ill = ipif->ipif_ill;
18004 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18005 
18006 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18007 	    ill_t *, ill, ipif_t *, ipif);
18008 
18009 	/*
18010 	 * need to bring up ARP or setup mcast mapping only
18011 	 * when the first interface is coming UP.
18012 	 */
18013 	first_interface = (ill->ill_ipif_up_count == 0 &&
18014 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18015 
18016 	if (res_act == Res_act_initial && first_interface) {
18017 		/*
18018 		 * Send ATTACH + BIND
18019 		 */
18020 		err = arp_ll_up(ill);
18021 		if (err != EINPROGRESS && err != 0)
18022 			return (err);
18023 
18024 		/*
18025 		 * Add NCE for local address. Start DAD.
18026 		 * we'll wait to hear that DAD has finished
18027 		 * before using the interface.
18028 		 */
18029 		if (err == EINPROGRESS)
18030 			wait_for_dlpi = B_TRUE;
18031 	}
18032 
18033 	if (!wait_for_dlpi)
18034 		(void) ipif_arp_up_done_tail(ipif, res_act);
18035 
18036 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18037 }
18038 
18039 /*
18040  * Finish processing of "arp_up" after all the DLPI message
18041  * exchanges have completed between arp and the driver.
18042  */
18043 void
18044 arp_bringup_done(ill_t *ill, int err)
18045 {
18046 	mblk_t	*mp1;
18047 	ipif_t  *ipif;
18048 	conn_t *connp = NULL;
18049 	ipsq_t	*ipsq;
18050 	queue_t *q;
18051 
18052 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18053 
18054 	ASSERT(IAM_WRITER_ILL(ill));
18055 
18056 	ipsq = ill->ill_phyint->phyint_ipsq;
18057 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18058 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18059 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18060 	if (mp1 == NULL) /* bringup was aborted by the user */
18061 		return;
18062 
18063 	/*
18064 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18065 	 * must have an associated conn_t.  Otherwise, we're bringing this
18066 	 * interface back up as part of handling an asynchronous event (e.g.,
18067 	 * physical address change).
18068 	 */
18069 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18070 		ASSERT(connp != NULL);
18071 		q = CONNP_TO_WQ(connp);
18072 	} else {
18073 		ASSERT(connp == NULL);
18074 		q = ill->ill_rq;
18075 	}
18076 	if (err == 0) {
18077 		if (ipif->ipif_isv6) {
18078 			if ((err = ipif_up_done_v6(ipif)) != 0)
18079 				ip0dbg(("arp_bringup_done: init failed\n"));
18080 		} else {
18081 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18082 			if (err != 0 ||
18083 			    (err = ipif_up_done(ipif)) != 0) {
18084 				ip0dbg(("arp_bringup_done: "
18085 				    "init failed err %x\n", err));
18086 				(void) ipif_arp_down(ipif);
18087 			}
18088 
18089 		}
18090 	} else {
18091 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18092 	}
18093 
18094 	if ((err == 0) && (ill->ill_up_ipifs)) {
18095 		err = ill_up_ipifs(ill, q, mp1);
18096 		if (err == EINPROGRESS)
18097 			return;
18098 	}
18099 
18100 	/*
18101 	 * If we have a moved ipif to bring up, and everything has succeeded
18102 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18103 	 * down -- the admin can try to bring it up by hand if need be.
18104 	 */
18105 	if (ill->ill_move_ipif != NULL) {
18106 		ipif = ill->ill_move_ipif;
18107 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18108 		    ipif->ipif_ill->ill_name));
18109 		ill->ill_move_ipif = NULL;
18110 		if (err == 0) {
18111 			err = ipif_up(ipif, q, mp1);
18112 			if (err == EINPROGRESS)
18113 				return;
18114 		}
18115 	}
18116 
18117 	/*
18118 	 * The operation must complete without EINPROGRESS since
18119 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18120 	 * Otherwise, the operation will be stuck forever in the ipsq.
18121 	 */
18122 	ASSERT(err != EINPROGRESS);
18123 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18124 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18125 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18126 		    ill_t *, ill, ipif_t *, ipif);
18127 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18128 	} else {
18129 		ipsq_current_finish(ipsq);
18130 	}
18131 }
18132 
18133 /*
18134  * Finish processing of arp replumb after all the DLPI message
18135  * exchanges have completed between arp and the driver.
18136  */
18137 void
18138 arp_replumb_done(ill_t *ill, int err)
18139 {
18140 	mblk_t	*mp1;
18141 	ipif_t  *ipif;
18142 	conn_t *connp = NULL;
18143 	ipsq_t	*ipsq;
18144 	queue_t *q;
18145 
18146 	ASSERT(IAM_WRITER_ILL(ill));
18147 
18148 	ipsq = ill->ill_phyint->phyint_ipsq;
18149 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18150 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18151 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18152 	if (mp1 == NULL) {
18153 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18154 		    ipsq->ipsq_xop->ipx_current_ioctl));
18155 		/* bringup was aborted by the user */
18156 		return;
18157 	}
18158 	/*
18159 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18160 	 * must have an associated conn_t.  Otherwise, we're bringing this
18161 	 * interface back up as part of handling an asynchronous event (e.g.,
18162 	 * physical address change).
18163 	 */
18164 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18165 		ASSERT(connp != NULL);
18166 		q = CONNP_TO_WQ(connp);
18167 	} else {
18168 		ASSERT(connp == NULL);
18169 		q = ill->ill_rq;
18170 	}
18171 	if ((err == 0) && (ill->ill_up_ipifs)) {
18172 		err = ill_up_ipifs(ill, q, mp1);
18173 		if (err == EINPROGRESS)
18174 			return;
18175 	}
18176 	/*
18177 	 * The operation must complete without EINPROGRESS since
18178 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18179 	 * Otherwise, the operation will be stuck forever in the ipsq.
18180 	 */
18181 	ASSERT(err != EINPROGRESS);
18182 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18183 		DTRACE_PROBE4(ipif__ioctl, char *,
18184 		    "arp_replumb_done finish",
18185 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18186 		    ill_t *, ill, ipif_t *, ipif);
18187 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18188 	} else {
18189 		ipsq_current_finish(ipsq);
18190 	}
18191 }
18192 
18193 void
18194 ipif_up_notify(ipif_t *ipif)
18195 {
18196 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18197 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18198 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18199 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18200 	    NE_LIF_UP, NULL, 0);
18201 }
18202 
18203 /*
18204  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18205  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18206  * TPI end points with STREAMS modules pushed above.  This is assured by not
18207  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18208  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18209  * while unwinding from the ispq and that could be a thread from the bottom.
18210  */
18211 /* ARGSUSED */
18212 int
18213 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18214     ip_ioctl_cmd_t *ipip, void *arg)
18215 {
18216 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18217 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18218 	int ret = 0;
18219 	int i;
18220 	size_t size;
18221 	ip_stack_t *ipst;
18222 	zoneid_t zoneid;
18223 	ilb_stack_t *ilbs;
18224 
18225 	ipst = CONNQ_TO_IPST(q);
18226 	ilbs = ipst->ips_netstack->netstack_ilb;
18227 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18228 
18229 	switch (command) {
18230 	case ILB_CREATE_RULE: {
18231 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18232 
18233 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18234 			ret = EINVAL;
18235 			break;
18236 		}
18237 
18238 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18239 		break;
18240 	}
18241 	case ILB_DESTROY_RULE:
18242 	case ILB_ENABLE_RULE:
18243 	case ILB_DISABLE_RULE: {
18244 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18245 
18246 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18247 			ret = EINVAL;
18248 			break;
18249 		}
18250 
18251 		if (cmd->flags & ILB_RULE_ALLRULES) {
18252 			if (command == ILB_DESTROY_RULE) {
18253 				ilb_rule_del_all(ilbs, zoneid);
18254 				break;
18255 			} else if (command == ILB_ENABLE_RULE) {
18256 				ilb_rule_enable_all(ilbs, zoneid);
18257 				break;
18258 			} else if (command == ILB_DISABLE_RULE) {
18259 				ilb_rule_disable_all(ilbs, zoneid);
18260 				break;
18261 			}
18262 		} else {
18263 			if (command == ILB_DESTROY_RULE) {
18264 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18265 			} else if (command == ILB_ENABLE_RULE) {
18266 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18267 				    NULL);
18268 			} else if (command == ILB_DISABLE_RULE) {
18269 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18270 				    NULL);
18271 			}
18272 		}
18273 		break;
18274 	}
18275 	case ILB_NUM_RULES: {
18276 		ilb_num_rules_cmd_t *cmd;
18277 
18278 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18279 			ret = EINVAL;
18280 			break;
18281 		}
18282 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18283 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18284 		break;
18285 	}
18286 	case ILB_RULE_NAMES: {
18287 		ilb_rule_names_cmd_t *cmd;
18288 
18289 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18290 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18291 		    cmd->num_names == 0) {
18292 			ret = EINVAL;
18293 			break;
18294 		}
18295 		size = cmd->num_names * ILB_RULE_NAMESZ;
18296 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18297 		    size != cmd_mp->b_wptr) {
18298 			ret = EINVAL;
18299 			break;
18300 		}
18301 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18302 		break;
18303 	}
18304 	case ILB_NUM_SERVERS: {
18305 		ilb_num_servers_cmd_t *cmd;
18306 
18307 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18308 			ret = EINVAL;
18309 			break;
18310 		}
18311 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18312 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18313 		    &(cmd->num));
18314 		break;
18315 	}
18316 	case ILB_LIST_RULE: {
18317 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18318 
18319 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18320 			ret = EINVAL;
18321 			break;
18322 		}
18323 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18324 		break;
18325 	}
18326 	case ILB_LIST_SERVERS: {
18327 		ilb_servers_info_cmd_t *cmd;
18328 
18329 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18330 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18331 		    cmd->num_servers == 0) {
18332 			ret = EINVAL;
18333 			break;
18334 		}
18335 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18336 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18337 		    size != cmd_mp->b_wptr) {
18338 			ret = EINVAL;
18339 			break;
18340 		}
18341 
18342 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18343 		    &cmd->num_servers);
18344 		break;
18345 	}
18346 	case ILB_ADD_SERVERS: {
18347 		ilb_servers_info_cmd_t *cmd;
18348 		ilb_rule_t *rule;
18349 
18350 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18351 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18352 			ret = EINVAL;
18353 			break;
18354 		}
18355 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18356 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18357 		    size != cmd_mp->b_wptr) {
18358 			ret = EINVAL;
18359 			break;
18360 		}
18361 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18362 		if (rule == NULL) {
18363 			ASSERT(ret != 0);
18364 			break;
18365 		}
18366 		for (i = 0; i < cmd->num_servers; i++) {
18367 			ilb_server_info_t *s;
18368 
18369 			s = &cmd->servers[i];
18370 			s->err = ilb_server_add(ilbs, rule, s);
18371 		}
18372 		ILB_RULE_REFRELE(rule);
18373 		break;
18374 	}
18375 	case ILB_DEL_SERVERS:
18376 	case ILB_ENABLE_SERVERS:
18377 	case ILB_DISABLE_SERVERS: {
18378 		ilb_servers_cmd_t *cmd;
18379 		ilb_rule_t *rule;
18380 		int (*f)();
18381 
18382 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18383 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18384 			ret = EINVAL;
18385 			break;
18386 		}
18387 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18388 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18389 		    size != cmd_mp->b_wptr) {
18390 			ret = EINVAL;
18391 			break;
18392 		}
18393 
18394 		if (command == ILB_DEL_SERVERS)
18395 			f = ilb_server_del;
18396 		else if (command == ILB_ENABLE_SERVERS)
18397 			f = ilb_server_enable;
18398 		else if (command == ILB_DISABLE_SERVERS)
18399 			f = ilb_server_disable;
18400 
18401 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18402 		if (rule == NULL) {
18403 			ASSERT(ret != 0);
18404 			break;
18405 		}
18406 
18407 		for (i = 0; i < cmd->num_servers; i++) {
18408 			ilb_server_arg_t *s;
18409 
18410 			s = &cmd->servers[i];
18411 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18412 		}
18413 		ILB_RULE_REFRELE(rule);
18414 		break;
18415 	}
18416 	case ILB_LIST_NAT_TABLE: {
18417 		ilb_list_nat_cmd_t *cmd;
18418 
18419 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18420 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18421 			ret = EINVAL;
18422 			break;
18423 		}
18424 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18425 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18426 		    size != cmd_mp->b_wptr) {
18427 			ret = EINVAL;
18428 			break;
18429 		}
18430 
18431 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18432 		    &cmd->flags);
18433 		break;
18434 	}
18435 	case ILB_LIST_STICKY_TABLE: {
18436 		ilb_list_sticky_cmd_t *cmd;
18437 
18438 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18439 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18440 			ret = EINVAL;
18441 			break;
18442 		}
18443 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18444 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18445 		    size != cmd_mp->b_wptr) {
18446 			ret = EINVAL;
18447 			break;
18448 		}
18449 
18450 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18451 		    &cmd->num_sticky, &cmd->flags);
18452 		break;
18453 	}
18454 	default:
18455 		ret = EINVAL;
18456 		break;
18457 	}
18458 done:
18459 	return (ret);
18460 }
18461 
18462 /* Remove all cache entries for this logical interface */
18463 void
18464 ipif_nce_down(ipif_t *ipif)
18465 {
18466 	ill_t *ill = ipif->ipif_ill;
18467 	nce_t *nce;
18468 
18469 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18470 	    ill_t *, ill, ipif_t *, ipif);
18471 	if (ipif->ipif_added_nce) {
18472 		if (ipif->ipif_isv6)
18473 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18474 		else
18475 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18476 		if (nce != NULL) {
18477 			if (--nce->nce_ipif_cnt == 0)
18478 				ncec_delete(nce->nce_common);
18479 			ipif->ipif_added_nce = 0;
18480 			nce_refrele(nce);
18481 		} else {
18482 			/*
18483 			 * nce may already be NULL because it was already
18484 			 * flushed, e.g., due to a call to nce_flush
18485 			 */
18486 			ipif->ipif_added_nce = 0;
18487 		}
18488 	}
18489 	/*
18490 	 * Make IPMP aware of the deleted data address.
18491 	 */
18492 	if (IS_IPMP(ill))
18493 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18494 
18495 	/*
18496 	 * Remove all other nces dependent on this ill when the last ipif
18497 	 * is going away.
18498 	 */
18499 	if (ill->ill_ipif_up_count == 0) {
18500 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18501 		    (uchar_t *)ill, ill->ill_ipst);
18502 		if (IS_UNDER_IPMP(ill))
18503 			nce_flush(ill, B_TRUE);
18504 	}
18505 }
18506 
18507 /*
18508  * find the first interface that uses usill for its source address.
18509  */
18510 ill_t *
18511 ill_lookup_usesrc(ill_t *usill)
18512 {
18513 	ip_stack_t *ipst = usill->ill_ipst;
18514 	ill_t *ill;
18515 
18516 	ASSERT(usill != NULL);
18517 
18518 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18519 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18520 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18521 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18522 	    ill = ill->ill_usesrc_grp_next) {
18523 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18524 		    !ILL_IS_CONDEMNED(ill)) {
18525 			ill_refhold(ill);
18526 			break;
18527 		}
18528 	}
18529 	rw_exit(&ipst->ips_ill_g_lock);
18530 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18531 	return (ill);
18532 }
18533