xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 7e3e5701)
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 static	sin6_t	sin6_null;	/* Zero address for quick clears */
281 static	sin_t	sin_null;	/* Zero address for quick clears */
282 
283 /* When set search for unused ipif_seqid */
284 static ipif_t	ipif_zero;
285 
286 /*
287  * ppa arena is created after these many
288  * interfaces have been plumbed.
289  */
290 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
291 
292 /*
293  * Allocate per-interface mibs.
294  * Returns true if ok. False otherwise.
295  *  ipsq  may not yet be allocated (loopback case ).
296  */
297 static boolean_t
298 ill_allocate_mibs(ill_t *ill)
299 {
300 	/* Already allocated? */
301 	if (ill->ill_ip_mib != NULL) {
302 		if (ill->ill_isv6)
303 			ASSERT(ill->ill_icmp6_mib != NULL);
304 		return (B_TRUE);
305 	}
306 
307 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
308 	    KM_NOSLEEP);
309 	if (ill->ill_ip_mib == NULL) {
310 		return (B_FALSE);
311 	}
312 
313 	/* Setup static information */
314 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
315 	    sizeof (mib2_ipIfStatsEntry_t));
316 	if (ill->ill_isv6) {
317 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
318 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
319 		    sizeof (mib2_ipv6AddrEntry_t));
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
321 		    sizeof (mib2_ipv6RouteEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
323 		    sizeof (mib2_ipv6NetToMediaEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
325 		    sizeof (ipv6_member_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
327 		    sizeof (ipv6_grpsrc_t));
328 	} else {
329 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
331 		    sizeof (mib2_ipAddrEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
333 		    sizeof (mib2_ipRouteEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
335 		    sizeof (mib2_ipNetToMediaEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
337 		    sizeof (ip_member_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
339 		    sizeof (ip_grpsrc_t));
340 
341 		/*
342 		 * For a v4 ill, we are done at this point, because per ill
343 		 * icmp mibs are only used for v6.
344 		 */
345 		return (B_TRUE);
346 	}
347 
348 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
349 	    KM_NOSLEEP);
350 	if (ill->ill_icmp6_mib == NULL) {
351 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
352 		ill->ill_ip_mib = NULL;
353 		return (B_FALSE);
354 	}
355 	/* static icmp info */
356 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
357 	    sizeof (mib2_ipv6IfIcmpEntry_t);
358 	/*
359 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
360 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
361 	 * -> ill_phyint_reinit
362 	 */
363 	return (B_TRUE);
364 }
365 
366 /*
367  * Completely vaporize a lower level tap and all associated interfaces.
368  * ill_delete is called only out of ip_close when the device control
369  * stream is being closed.
370  */
371 void
372 ill_delete(ill_t *ill)
373 {
374 	ipif_t	*ipif;
375 	ill_t	*prev_ill;
376 	ip_stack_t	*ipst = ill->ill_ipst;
377 
378 	/*
379 	 * ill_delete may be forcibly entering the ipsq. The previous
380 	 * ioctl may not have completed and may need to be aborted.
381 	 * ipsq_flush takes care of it. If we don't need to enter the
382 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
383 	 * ill_delete_tail is sufficient.
384 	 */
385 	ipsq_flush(ill);
386 
387 	/*
388 	 * Nuke all interfaces.  ipif_free will take down the interface,
389 	 * remove it from the list, and free the data structure.
390 	 * Walk down the ipif list and remove the logical interfaces
391 	 * first before removing the main ipif. We can't unplumb
392 	 * zeroth interface first in the case of IPv6 as update_conn_ill
393 	 * -> ip_ll_multireq de-references ill_ipif for checking
394 	 * POINTOPOINT.
395 	 *
396 	 * If ill_ipif was not properly initialized (i.e low on memory),
397 	 * then no interfaces to clean up. In this case just clean up the
398 	 * ill.
399 	 */
400 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
401 		ipif_free(ipif);
402 
403 	/*
404 	 * clean out all the nce_t entries that depend on this
405 	 * ill for the ill_phys_addr.
406 	 */
407 	nce_flush(ill, B_TRUE);
408 
409 	/* Clean up msgs on pending upcalls for mrouted */
410 	reset_mrt_ill(ill);
411 
412 	update_conn_ill(ill, ipst);
413 
414 	/*
415 	 * Remove multicast references added as a result of calls to
416 	 * ip_join_allmulti().
417 	 */
418 	ip_purge_allmulti(ill);
419 
420 	/*
421 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
422 	 */
423 	if (IS_UNDER_IPMP(ill))
424 		ipmp_ill_leave_illgrp(ill);
425 
426 	/*
427 	 * ill_down will arrange to blow off any IRE's dependent on this
428 	 * ILL, and shut down fragmentation reassembly.
429 	 */
430 	ill_down(ill);
431 
432 	/* Let SCTP know, so that it can remove this from its list. */
433 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
434 
435 	/*
436 	 * Walk all CONNs that can have a reference on an ire or nce for this
437 	 * ill (we actually walk all that now have stale references).
438 	 */
439 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
440 
441 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
442 	if (ill->ill_isv6)
443 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
444 
445 	/*
446 	 * If an address on this ILL is being used as a source address then
447 	 * clear out the pointers in other ILLs that point to this ILL.
448 	 */
449 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
450 	if (ill->ill_usesrc_grp_next != NULL) {
451 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
452 			ill_disband_usesrc_group(ill);
453 		} else {	/* consumer of the usesrc ILL */
454 			prev_ill = ill_prev_usesrc(ill);
455 			prev_ill->ill_usesrc_grp_next =
456 			    ill->ill_usesrc_grp_next;
457 		}
458 	}
459 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
460 }
461 
462 static void
463 ipif_non_duplicate(ipif_t *ipif)
464 {
465 	ill_t *ill = ipif->ipif_ill;
466 	mutex_enter(&ill->ill_lock);
467 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
468 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
469 		ASSERT(ill->ill_ipif_dup_count > 0);
470 		ill->ill_ipif_dup_count--;
471 	}
472 	mutex_exit(&ill->ill_lock);
473 }
474 
475 /*
476  * ill_delete_tail is called from ip_modclose after all references
477  * to the closing ill are gone. The wait is done in ip_modclose
478  */
479 void
480 ill_delete_tail(ill_t *ill)
481 {
482 	mblk_t	**mpp;
483 	ipif_t	*ipif;
484 	ip_stack_t	*ipst = ill->ill_ipst;
485 
486 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
487 		ipif_non_duplicate(ipif);
488 		(void) ipif_down_tail(ipif);
489 	}
490 
491 	ASSERT(ill->ill_ipif_dup_count == 0);
492 
493 	/*
494 	 * If polling capability is enabled (which signifies direct
495 	 * upcall into IP and driver has ill saved as a handle),
496 	 * we need to make sure that unbind has completed before we
497 	 * let the ill disappear and driver no longer has any reference
498 	 * to this ill.
499 	 */
500 	mutex_enter(&ill->ill_lock);
501 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
502 		cv_wait(&ill->ill_cv, &ill->ill_lock);
503 	mutex_exit(&ill->ill_lock);
504 	ASSERT(!(ill->ill_capabilities &
505 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
506 
507 	if (ill->ill_net_type != IRE_LOOPBACK)
508 		qprocsoff(ill->ill_rq);
509 
510 	/*
511 	 * We do an ipsq_flush once again now. New messages could have
512 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
513 	 * could also have landed up if an ioctl thread had looked up
514 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
515 	 * enqueued the ioctl when we did the ipsq_flush last time.
516 	 */
517 	ipsq_flush(ill);
518 
519 	/*
520 	 * Free capabilities.
521 	 */
522 	if (ill->ill_hcksum_capab != NULL) {
523 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
524 		ill->ill_hcksum_capab = NULL;
525 	}
526 
527 	if (ill->ill_zerocopy_capab != NULL) {
528 		kmem_free(ill->ill_zerocopy_capab,
529 		    sizeof (ill_zerocopy_capab_t));
530 		ill->ill_zerocopy_capab = NULL;
531 	}
532 
533 	if (ill->ill_lso_capab != NULL) {
534 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
535 		ill->ill_lso_capab = NULL;
536 	}
537 
538 	if (ill->ill_dld_capab != NULL) {
539 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
540 		ill->ill_dld_capab = NULL;
541 	}
542 
543 	while (ill->ill_ipif != NULL)
544 		ipif_free_tail(ill->ill_ipif);
545 
546 	/*
547 	 * We have removed all references to ilm from conn and the ones joined
548 	 * within the kernel.
549 	 *
550 	 * We don't walk conns, mrts and ires because
551 	 *
552 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
553 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
554 	 *    ill references.
555 	 */
556 
557 	/*
558 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
559 	 * is safe to do because the illgrp has already been unlinked from the
560 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
561 	 */
562 	if (IS_IPMP(ill)) {
563 		ipmp_illgrp_destroy(ill->ill_grp);
564 		ill->ill_grp = NULL;
565 	}
566 
567 	/*
568 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
569 	 * could free the phyint. No more reference to the phyint after this
570 	 * point.
571 	 */
572 	(void) ill_glist_delete(ill);
573 
574 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
575 	if (ill->ill_ndd_name != NULL)
576 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
577 	rw_exit(&ipst->ips_ip_g_nd_lock);
578 
579 	if (ill->ill_frag_ptr != NULL) {
580 		uint_t count;
581 
582 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
583 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
584 		}
585 		mi_free(ill->ill_frag_ptr);
586 		ill->ill_frag_ptr = NULL;
587 		ill->ill_frag_hash_tbl = NULL;
588 	}
589 
590 	freemsg(ill->ill_nd_lla_mp);
591 	/* Free all retained control messages. */
592 	mpp = &ill->ill_first_mp_to_free;
593 	do {
594 		while (mpp[0]) {
595 			mblk_t  *mp;
596 			mblk_t  *mp1;
597 
598 			mp = mpp[0];
599 			mpp[0] = mp->b_next;
600 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
601 				mp1->b_next = NULL;
602 				mp1->b_prev = NULL;
603 			}
604 			freemsg(mp);
605 		}
606 	} while (mpp++ != &ill->ill_last_mp_to_free);
607 
608 	ill_free_mib(ill);
609 
610 #ifdef DEBUG
611 	ill_trace_cleanup(ill);
612 #endif
613 
614 	/* The default multicast interface might have changed */
615 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
616 
617 	/* Drop refcnt here */
618 	netstack_rele(ill->ill_ipst->ips_netstack);
619 	ill->ill_ipst = NULL;
620 }
621 
622 static void
623 ill_free_mib(ill_t *ill)
624 {
625 	ip_stack_t *ipst = ill->ill_ipst;
626 
627 	/*
628 	 * MIB statistics must not be lost, so when an interface
629 	 * goes away the counter values will be added to the global
630 	 * MIBs.
631 	 */
632 	if (ill->ill_ip_mib != NULL) {
633 		if (ill->ill_isv6) {
634 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
635 			    ill->ill_ip_mib);
636 		} else {
637 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
638 			    ill->ill_ip_mib);
639 		}
640 
641 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
642 		ill->ill_ip_mib = NULL;
643 	}
644 	if (ill->ill_icmp6_mib != NULL) {
645 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
646 		    ill->ill_icmp6_mib);
647 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
648 		ill->ill_icmp6_mib = NULL;
649 	}
650 }
651 
652 /*
653  * Concatenate together a physical address and a sap.
654  *
655  * Sap_lengths are interpreted as follows:
656  *   sap_length == 0	==>	no sap
657  *   sap_length > 0	==>	sap is at the head of the dlpi address
658  *   sap_length < 0	==>	sap is at the tail of the dlpi address
659  */
660 static void
661 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
662     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
663 {
664 	uint16_t sap_addr = (uint16_t)sap_src;
665 
666 	if (sap_length == 0) {
667 		if (phys_src == NULL)
668 			bzero(dst, phys_length);
669 		else
670 			bcopy(phys_src, dst, phys_length);
671 	} else if (sap_length < 0) {
672 		if (phys_src == NULL)
673 			bzero(dst, phys_length);
674 		else
675 			bcopy(phys_src, dst, phys_length);
676 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
677 	} else {
678 		bcopy(&sap_addr, dst, sizeof (sap_addr));
679 		if (phys_src == NULL)
680 			bzero((char *)dst + sap_length, phys_length);
681 		else
682 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
683 	}
684 }
685 
686 /*
687  * Generate a dl_unitdata_req mblk for the device and address given.
688  * addr_length is the length of the physical portion of the address.
689  * If addr is NULL include an all zero address of the specified length.
690  * TRUE? In any case, addr_length is taken to be the entire length of the
691  * dlpi address, including the absolute value of sap_length.
692  */
693 mblk_t *
694 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
695 		t_scalar_t sap_length)
696 {
697 	dl_unitdata_req_t *dlur;
698 	mblk_t	*mp;
699 	t_scalar_t	abs_sap_length;		/* absolute value */
700 
701 	abs_sap_length = ABS(sap_length);
702 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
703 	    DL_UNITDATA_REQ);
704 	if (mp == NULL)
705 		return (NULL);
706 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
707 	/* HACK: accomodate incompatible DLPI drivers */
708 	if (addr_length == 8)
709 		addr_length = 6;
710 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
711 	dlur->dl_dest_addr_offset = sizeof (*dlur);
712 	dlur->dl_priority.dl_min = 0;
713 	dlur->dl_priority.dl_max = 0;
714 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
715 	    (uchar_t *)&dlur[1]);
716 	return (mp);
717 }
718 
719 /*
720  * Add the pending mp to the list. There can be only 1 pending mp
721  * in the list. Any exclusive ioctl that needs to wait for a response
722  * from another module or driver needs to use this function to set
723  * the ipx_pending_mp to the ioctl mblk and wait for the response from
724  * the other module/driver. This is also used while waiting for the
725  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
726  */
727 boolean_t
728 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
729     int waitfor)
730 {
731 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
732 
733 	ASSERT(IAM_WRITER_IPIF(ipif));
734 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
735 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
736 	ASSERT(ipx->ipx_pending_mp == NULL);
737 	/*
738 	 * The caller may be using a different ipif than the one passed into
739 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
740 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
741 	 * that `ipx_current_ipif == ipif'.
742 	 */
743 	ASSERT(ipx->ipx_current_ipif != NULL);
744 
745 	/*
746 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
747 	 * driver.
748 	 */
749 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
750 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
751 	    (DB_TYPE(add_mp) == M_PCPROTO));
752 
753 	if (connp != NULL) {
754 		ASSERT(MUTEX_HELD(&connp->conn_lock));
755 		/*
756 		 * Return error if the conn has started closing. The conn
757 		 * could have finished cleaning up the pending mp list,
758 		 * If so we should not add another mp to the list negating
759 		 * the cleanup.
760 		 */
761 		if (connp->conn_state_flags & CONN_CLOSING)
762 			return (B_FALSE);
763 	}
764 	mutex_enter(&ipx->ipx_lock);
765 	ipx->ipx_pending_ipif = ipif;
766 	/*
767 	 * Note down the queue in b_queue. This will be returned by
768 	 * ipsq_pending_mp_get. Caller will then use these values to restart
769 	 * the processing
770 	 */
771 	add_mp->b_next = NULL;
772 	add_mp->b_queue = q;
773 	ipx->ipx_pending_mp = add_mp;
774 	ipx->ipx_waitfor = waitfor;
775 	mutex_exit(&ipx->ipx_lock);
776 
777 	if (connp != NULL)
778 		connp->conn_oper_pending_ill = ipif->ipif_ill;
779 
780 	return (B_TRUE);
781 }
782 
783 /*
784  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
785  * queued in the list.
786  */
787 mblk_t *
788 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
789 {
790 	mblk_t	*curr = NULL;
791 	ipxop_t	*ipx = ipsq->ipsq_xop;
792 
793 	*connpp = NULL;
794 	mutex_enter(&ipx->ipx_lock);
795 	if (ipx->ipx_pending_mp == NULL) {
796 		mutex_exit(&ipx->ipx_lock);
797 		return (NULL);
798 	}
799 
800 	/* There can be only 1 such excl message */
801 	curr = ipx->ipx_pending_mp;
802 	ASSERT(curr->b_next == NULL);
803 	ipx->ipx_pending_ipif = NULL;
804 	ipx->ipx_pending_mp = NULL;
805 	ipx->ipx_waitfor = 0;
806 	mutex_exit(&ipx->ipx_lock);
807 
808 	if (CONN_Q(curr->b_queue)) {
809 		/*
810 		 * This mp did a refhold on the conn, at the start of the ioctl.
811 		 * So we can safely return a pointer to the conn to the caller.
812 		 */
813 		*connpp = Q_TO_CONN(curr->b_queue);
814 	} else {
815 		*connpp = NULL;
816 	}
817 	curr->b_next = NULL;
818 	curr->b_prev = NULL;
819 	return (curr);
820 }
821 
822 /*
823  * Cleanup the ioctl mp queued in ipx_pending_mp
824  * - Called in the ill_delete path
825  * - Called in the M_ERROR or M_HANGUP path on the ill.
826  * - Called in the conn close path.
827  *
828  * Returns success on finding the pending mblk associated with the ioctl or
829  * exclusive operation in progress, failure otherwise.
830  */
831 boolean_t
832 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
833 {
834 	mblk_t	*mp;
835 	ipxop_t	*ipx;
836 	queue_t	*q;
837 	ipif_t	*ipif;
838 	int	cmd;
839 
840 	ASSERT(IAM_WRITER_ILL(ill));
841 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
842 
843 	/*
844 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
845 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
846 	 * even if it is meant for another ill, since we have to enqueue
847 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
848 	 * If connp is non-null we are called from the conn close path.
849 	 */
850 	mutex_enter(&ipx->ipx_lock);
851 	mp = ipx->ipx_pending_mp;
852 	if ((connp != NULL) &&
853 	    (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp))) {
854 		mutex_exit(&ipx->ipx_lock);
855 		return (B_FALSE);
856 	}
857 
858 	/* Now remove from the ipx_pending_mp */
859 	ipx->ipx_pending_mp = NULL;
860 	ipif = ipx->ipx_pending_ipif;
861 	ipx->ipx_pending_ipif = NULL;
862 	ipx->ipx_waitfor = 0;
863 	ipx->ipx_current_ipif = NULL;
864 	cmd = ipx->ipx_current_ioctl;
865 	ipx->ipx_current_ioctl = 0;
866 	ipx->ipx_current_done = B_TRUE;
867 	mutex_exit(&ipx->ipx_lock);
868 
869 	if (mp == NULL)
870 		return (B_FALSE);
871 
872 	q = mp->b_queue;
873 	mp->b_next = NULL;
874 	mp->b_prev = NULL;
875 	mp->b_queue = NULL;
876 
877 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
878 		DTRACE_PROBE4(ipif__ioctl,
879 		    char *, "ipsq_pending_mp_cleanup",
880 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
881 		    ipif_t *, ipif);
882 		if (connp == NULL) {
883 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
884 		} else {
885 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
886 			mutex_enter(&ipif->ipif_ill->ill_lock);
887 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
888 			mutex_exit(&ipif->ipif_ill->ill_lock);
889 		}
890 	} else {
891 		inet_freemsg(mp);
892 	}
893 	return (B_TRUE);
894 }
895 
896 /*
897  * Called in the conn close path and ill delete path
898  */
899 static void
900 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
901 {
902 	ipsq_t	*ipsq;
903 	mblk_t	*prev;
904 	mblk_t	*curr;
905 	mblk_t	*next;
906 	queue_t	*rq, *wq;
907 	mblk_t	*tmp_list = NULL;
908 
909 	ASSERT(IAM_WRITER_ILL(ill));
910 	if (connp != NULL)
911 		wq = CONNP_TO_WQ(connp);
912 	else
913 		wq = ill->ill_wq;
914 	rq = RD(wq);
915 
916 	ipsq = ill->ill_phyint->phyint_ipsq;
917 	/*
918 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
919 	 * In the case of ioctl from a conn, there can be only 1 mp
920 	 * queued on the ipsq. If an ill is being unplumbed, only messages
921 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
922 	 * ioctls meant for this ill form conn's are not flushed. They will
923 	 * be processed during ipsq_exit and will not find the ill and will
924 	 * return error.
925 	 */
926 	mutex_enter(&ipsq->ipsq_lock);
927 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
928 	    curr = next) {
929 		next = curr->b_next;
930 		if (curr->b_queue == wq || curr->b_queue == rq) {
931 			/* Unlink the mblk from the pending mp list */
932 			if (prev != NULL) {
933 				prev->b_next = curr->b_next;
934 			} else {
935 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
936 				ipsq->ipsq_xopq_mphead = curr->b_next;
937 			}
938 			if (ipsq->ipsq_xopq_mptail == curr)
939 				ipsq->ipsq_xopq_mptail = prev;
940 			/*
941 			 * Create a temporary list and release the ipsq lock
942 			 * New elements are added to the head of the tmp_list
943 			 */
944 			curr->b_next = tmp_list;
945 			tmp_list = curr;
946 		} else {
947 			prev = curr;
948 		}
949 	}
950 	mutex_exit(&ipsq->ipsq_lock);
951 
952 	while (tmp_list != NULL) {
953 		curr = tmp_list;
954 		tmp_list = curr->b_next;
955 		curr->b_next = NULL;
956 		curr->b_prev = NULL;
957 		curr->b_queue = NULL;
958 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
959 			DTRACE_PROBE4(ipif__ioctl,
960 			    char *, "ipsq_xopq_mp_cleanup",
961 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
962 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
963 			    CONN_CLOSE : NO_COPYOUT, NULL);
964 		} else {
965 			/*
966 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
967 			 * this can't be just inet_freemsg. we have to
968 			 * restart it otherwise the thread will be stuck.
969 			 */
970 			inet_freemsg(curr);
971 		}
972 	}
973 }
974 
975 /*
976  * This conn has started closing. Cleanup any pending ioctl from this conn.
977  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
978  */
979 void
980 conn_ioctl_cleanup(conn_t *connp)
981 {
982 	ipsq_t	*ipsq;
983 	ill_t	*ill;
984 	boolean_t refheld;
985 
986 	/*
987 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
988 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
989 	 * started the mp could be present in ipx_pending_mp. Note that if
990 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
991 	 * not yet queued anywhere. In this case, the conn close code will wait
992 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
993 	 * tcp_close will wait first until all ioctls have completed for this
994 	 * conn.
995 	 */
996 	mutex_enter(&connp->conn_lock);
997 	ill = connp->conn_oper_pending_ill;
998 	if (ill == NULL) {
999 		mutex_exit(&connp->conn_lock);
1000 		return;
1001 	}
1002 
1003 	/*
1004 	 * We may not be able to refhold the ill if the ill/ipif
1005 	 * is changing. But we need to make sure that the ill will
1006 	 * not vanish. So we just bump up the ill_waiter count.
1007 	 */
1008 	refheld = ill_waiter_inc(ill);
1009 	mutex_exit(&connp->conn_lock);
1010 	if (refheld) {
1011 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1012 			ill_waiter_dcr(ill);
1013 			/*
1014 			 * Check whether this ioctl has started and is
1015 			 * pending. If it is not found there then check
1016 			 * whether this ioctl has not even started and is in
1017 			 * the ipsq_xopq list.
1018 			 */
1019 			if (!ipsq_pending_mp_cleanup(ill, connp))
1020 				ipsq_xopq_mp_cleanup(ill, connp);
1021 			ipsq = ill->ill_phyint->phyint_ipsq;
1022 			ipsq_exit(ipsq);
1023 			return;
1024 		}
1025 	}
1026 
1027 	/*
1028 	 * The ill is also closing and we could not bump up the
1029 	 * ill_waiter_count or we could not enter the ipsq. Leave
1030 	 * the cleanup to ill_delete
1031 	 */
1032 	mutex_enter(&connp->conn_lock);
1033 	while (connp->conn_oper_pending_ill != NULL)
1034 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1035 	mutex_exit(&connp->conn_lock);
1036 	if (refheld)
1037 		ill_waiter_dcr(ill);
1038 }
1039 
1040 /*
1041  * ipcl_walk function for cleaning up conn_*_ill fields.
1042  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1043  * conn_bound_if in place. We prefer dropping
1044  * packets instead of sending them out the wrong interface, or accepting
1045  * packets from the wrong ifindex.
1046  */
1047 static void
1048 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1049 {
1050 	ill_t	*ill = (ill_t *)arg;
1051 
1052 	mutex_enter(&connp->conn_lock);
1053 	if (connp->conn_dhcpinit_ill == ill) {
1054 		connp->conn_dhcpinit_ill = NULL;
1055 		ASSERT(ill->ill_dhcpinit != 0);
1056 		atomic_dec_32(&ill->ill_dhcpinit);
1057 		ill_set_inputfn(ill);
1058 	}
1059 	mutex_exit(&connp->conn_lock);
1060 }
1061 
1062 static int
1063 ill_down_ipifs_tail(ill_t *ill)
1064 {
1065 	ipif_t	*ipif;
1066 	int err;
1067 
1068 	ASSERT(IAM_WRITER_ILL(ill));
1069 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1070 		ipif_non_duplicate(ipif);
1071 		/*
1072 		 * ipif_down_tail will call arp_ll_down on the last ipif
1073 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1074 		 */
1075 		if ((err = ipif_down_tail(ipif)) != 0)
1076 			return (err);
1077 	}
1078 	return (0);
1079 }
1080 
1081 /* ARGSUSED */
1082 void
1083 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1084 {
1085 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1086 	(void) ill_down_ipifs_tail(q->q_ptr);
1087 	freemsg(mp);
1088 	ipsq_current_finish(ipsq);
1089 }
1090 
1091 /*
1092  * ill_down_start is called when we want to down this ill and bring it up again
1093  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1094  * all interfaces, but don't tear down any plumbing.
1095  */
1096 boolean_t
1097 ill_down_start(queue_t *q, mblk_t *mp)
1098 {
1099 	ill_t	*ill = q->q_ptr;
1100 	ipif_t	*ipif;
1101 
1102 	ASSERT(IAM_WRITER_ILL(ill));
1103 	mutex_enter(&ill->ill_lock);
1104 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1105 	/* no more nce addition allowed */
1106 	mutex_exit(&ill->ill_lock);
1107 
1108 	/*
1109 	 * It is possible that some ioctl is already in progress while we
1110 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1111 	 * the ioctl. (ill_down_start() is being processed as CUR_OP since
1112 	 * the cause of the M_ERROR / M_HANGUP may prevent the in progress
1113 	 * ioctl from completion.)
1114 	 */
1115 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1116 	ill_dlpi_clear_deferred(ill);
1117 
1118 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1119 		(void) ipif_down(ipif, NULL, NULL);
1120 
1121 	ill_down(ill);
1122 
1123 	/*
1124 	 * Walk all CONNs that can have a reference on an ire or nce for this
1125 	 * ill (we actually walk all that now have stale references).
1126 	 */
1127 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1128 
1129 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1130 	if (ill->ill_isv6)
1131 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1132 
1133 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1134 
1135 	/*
1136 	 * Atomically test and add the pending mp if references are active.
1137 	 */
1138 	mutex_enter(&ill->ill_lock);
1139 	if (!ill_is_quiescent(ill)) {
1140 		/* call cannot fail since `conn_t *' argument is NULL */
1141 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1142 		    mp, ILL_DOWN);
1143 		mutex_exit(&ill->ill_lock);
1144 		return (B_FALSE);
1145 	}
1146 	mutex_exit(&ill->ill_lock);
1147 	return (B_TRUE);
1148 }
1149 
1150 static void
1151 ill_down(ill_t *ill)
1152 {
1153 	mblk_t	*mp;
1154 	ip_stack_t	*ipst = ill->ill_ipst;
1155 
1156 	/*
1157 	 * Blow off any IREs dependent on this ILL.
1158 	 * The caller needs to handle conn_ixa_cleanup
1159 	 */
1160 	ill_delete_ires(ill);
1161 
1162 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1163 
1164 	/* Remove any conn_*_ill depending on this ill */
1165 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1166 
1167 	/*
1168 	 * Free state for additional IREs.
1169 	 */
1170 	mutex_enter(&ill->ill_saved_ire_lock);
1171 	mp = ill->ill_saved_ire_mp;
1172 	ill->ill_saved_ire_mp = NULL;
1173 	ill->ill_saved_ire_cnt = 0;
1174 	mutex_exit(&ill->ill_saved_ire_lock);
1175 	freemsg(mp);
1176 }
1177 
1178 /*
1179  * ire_walk routine used to delete every IRE that depends on
1180  * 'ill'.  (Always called as writer.)
1181  *
1182  * Note: since the routes added by the kernel are deleted separately,
1183  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1184  *
1185  * We also remove references on ire_nce_cache entries that refer to the ill.
1186  */
1187 void
1188 ill_downi(ire_t *ire, char *ill_arg)
1189 {
1190 	ill_t	*ill = (ill_t *)ill_arg;
1191 	nce_t	*nce;
1192 
1193 	mutex_enter(&ire->ire_lock);
1194 	nce = ire->ire_nce_cache;
1195 	if (nce != NULL && nce->nce_ill == ill)
1196 		ire->ire_nce_cache = NULL;
1197 	else
1198 		nce = NULL;
1199 	mutex_exit(&ire->ire_lock);
1200 	if (nce != NULL)
1201 		nce_refrele(nce);
1202 	if (ire->ire_ill == ill)
1203 		ire_delete(ire);
1204 }
1205 
1206 /* Remove IRE_IF_CLONE on this ill */
1207 void
1208 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1209 {
1210 	ill_t	*ill = (ill_t *)ill_arg;
1211 
1212 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1213 	if (ire->ire_ill == ill)
1214 		ire_delete(ire);
1215 }
1216 
1217 /* Consume an M_IOCACK of the fastpath probe. */
1218 void
1219 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1220 {
1221 	mblk_t	*mp1 = mp;
1222 
1223 	/*
1224 	 * If this was the first attempt turn on the fastpath probing.
1225 	 */
1226 	mutex_enter(&ill->ill_lock);
1227 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1228 		ill->ill_dlpi_fastpath_state = IDS_OK;
1229 	mutex_exit(&ill->ill_lock);
1230 
1231 	/* Free the M_IOCACK mblk, hold on to the data */
1232 	mp = mp->b_cont;
1233 	freeb(mp1);
1234 	if (mp == NULL)
1235 		return;
1236 	if (mp->b_cont != NULL)
1237 		nce_fastpath_update(ill, mp);
1238 	else
1239 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1240 	freemsg(mp);
1241 }
1242 
1243 /*
1244  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1245  * The data portion of the request is a dl_unitdata_req_t template for
1246  * what we would send downstream in the absence of a fastpath confirmation.
1247  */
1248 int
1249 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1250 {
1251 	struct iocblk	*ioc;
1252 	mblk_t	*mp;
1253 
1254 	if (dlur_mp == NULL)
1255 		return (EINVAL);
1256 
1257 	mutex_enter(&ill->ill_lock);
1258 	switch (ill->ill_dlpi_fastpath_state) {
1259 	case IDS_FAILED:
1260 		/*
1261 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1262 		 * support it.
1263 		 */
1264 		mutex_exit(&ill->ill_lock);
1265 		return (ENOTSUP);
1266 	case IDS_UNKNOWN:
1267 		/* This is the first probe */
1268 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1269 		break;
1270 	default:
1271 		break;
1272 	}
1273 	mutex_exit(&ill->ill_lock);
1274 
1275 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1276 		return (EAGAIN);
1277 
1278 	mp->b_cont = copyb(dlur_mp);
1279 	if (mp->b_cont == NULL) {
1280 		freeb(mp);
1281 		return (EAGAIN);
1282 	}
1283 
1284 	ioc = (struct iocblk *)mp->b_rptr;
1285 	ioc->ioc_count = msgdsize(mp->b_cont);
1286 
1287 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1288 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1289 	putnext(ill->ill_wq, mp);
1290 	return (0);
1291 }
1292 
1293 void
1294 ill_capability_probe(ill_t *ill)
1295 {
1296 	mblk_t	*mp;
1297 
1298 	ASSERT(IAM_WRITER_ILL(ill));
1299 
1300 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1301 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1302 		return;
1303 
1304 	/*
1305 	 * We are starting a new cycle of capability negotiation.
1306 	 * Free up the capab reset messages of any previous incarnation.
1307 	 * We will do a fresh allocation when we get the response to our probe
1308 	 */
1309 	if (ill->ill_capab_reset_mp != NULL) {
1310 		freemsg(ill->ill_capab_reset_mp);
1311 		ill->ill_capab_reset_mp = NULL;
1312 	}
1313 
1314 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1315 
1316 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1317 	if (mp == NULL)
1318 		return;
1319 
1320 	ill_capability_send(ill, mp);
1321 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1322 }
1323 
1324 void
1325 ill_capability_reset(ill_t *ill, boolean_t reneg)
1326 {
1327 	ASSERT(IAM_WRITER_ILL(ill));
1328 
1329 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1330 		return;
1331 
1332 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1333 
1334 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1335 	ill->ill_capab_reset_mp = NULL;
1336 	/*
1337 	 * We turn off all capabilities except those pertaining to
1338 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1339 	 * which will be turned off by the corresponding reset functions.
1340 	 */
1341 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1342 }
1343 
1344 static void
1345 ill_capability_reset_alloc(ill_t *ill)
1346 {
1347 	mblk_t *mp;
1348 	size_t	size = 0;
1349 	int	err;
1350 	dl_capability_req_t	*capb;
1351 
1352 	ASSERT(IAM_WRITER_ILL(ill));
1353 	ASSERT(ill->ill_capab_reset_mp == NULL);
1354 
1355 	if (ILL_HCKSUM_CAPABLE(ill)) {
1356 		size += sizeof (dl_capability_sub_t) +
1357 		    sizeof (dl_capab_hcksum_t);
1358 	}
1359 
1360 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1361 		size += sizeof (dl_capability_sub_t) +
1362 		    sizeof (dl_capab_zerocopy_t);
1363 	}
1364 
1365 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1366 		size += sizeof (dl_capability_sub_t) +
1367 		    sizeof (dl_capab_dld_t);
1368 	}
1369 
1370 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1371 	    STR_NOSIG, &err);
1372 
1373 	mp->b_datap->db_type = M_PROTO;
1374 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1375 
1376 	capb = (dl_capability_req_t *)mp->b_rptr;
1377 	capb->dl_primitive = DL_CAPABILITY_REQ;
1378 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1379 	capb->dl_sub_length = size;
1380 
1381 	mp->b_wptr += sizeof (dl_capability_req_t);
1382 
1383 	/*
1384 	 * Each handler fills in the corresponding dl_capability_sub_t
1385 	 * inside the mblk,
1386 	 */
1387 	ill_capability_hcksum_reset_fill(ill, mp);
1388 	ill_capability_zerocopy_reset_fill(ill, mp);
1389 	ill_capability_dld_reset_fill(ill, mp);
1390 
1391 	ill->ill_capab_reset_mp = mp;
1392 }
1393 
1394 static void
1395 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1396 {
1397 	dl_capab_id_t *id_ic;
1398 	uint_t sub_dl_cap = outers->dl_cap;
1399 	dl_capability_sub_t *inners;
1400 	uint8_t *capend;
1401 
1402 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1403 
1404 	/*
1405 	 * Note: range checks here are not absolutely sufficient to
1406 	 * make us robust against malformed messages sent by drivers;
1407 	 * this is in keeping with the rest of IP's dlpi handling.
1408 	 * (Remember, it's coming from something else in the kernel
1409 	 * address space)
1410 	 */
1411 
1412 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1413 	if (capend > mp->b_wptr) {
1414 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1415 		    "malformed sub-capability too long for mblk");
1416 		return;
1417 	}
1418 
1419 	id_ic = (dl_capab_id_t *)(outers + 1);
1420 
1421 	if (outers->dl_length < sizeof (*id_ic) ||
1422 	    (inners = &id_ic->id_subcap,
1423 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1424 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1425 		    "encapsulated capab type %d too long for mblk",
1426 		    inners->dl_cap);
1427 		return;
1428 	}
1429 
1430 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1431 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1432 		    "isn't as expected; pass-thru module(s) detected, "
1433 		    "discarding capability\n", inners->dl_cap));
1434 		return;
1435 	}
1436 
1437 	/* Process the encapsulated sub-capability */
1438 	ill_capability_dispatch(ill, mp, inners);
1439 }
1440 
1441 static void
1442 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1443 {
1444 	dl_capability_sub_t *dl_subcap;
1445 
1446 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1447 		return;
1448 
1449 	/*
1450 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1451 	 * initialized below since it is not used by DLD.
1452 	 */
1453 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1454 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1455 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1456 
1457 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1458 }
1459 
1460 static void
1461 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1462 {
1463 	/*
1464 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1465 	 * is only to get the VRRP capability.
1466 	 *
1467 	 * Note that we cannot check ill_ipif_up_count here since
1468 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1469 	 * That is done asynchronously, and can race with this function.
1470 	 */
1471 	if (!ill->ill_dl_up) {
1472 		if (subp->dl_cap == DL_CAPAB_VRRP)
1473 			ill_capability_vrrp_ack(ill, mp, subp);
1474 		return;
1475 	}
1476 
1477 	switch (subp->dl_cap) {
1478 	case DL_CAPAB_HCKSUM:
1479 		ill_capability_hcksum_ack(ill, mp, subp);
1480 		break;
1481 	case DL_CAPAB_ZEROCOPY:
1482 		ill_capability_zerocopy_ack(ill, mp, subp);
1483 		break;
1484 	case DL_CAPAB_DLD:
1485 		ill_capability_dld_ack(ill, mp, subp);
1486 		break;
1487 	case DL_CAPAB_VRRP:
1488 		break;
1489 	default:
1490 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1491 		    subp->dl_cap));
1492 	}
1493 }
1494 
1495 /*
1496  * Process the vrrp capability received from a DLS Provider. isub must point
1497  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1498  */
1499 static void
1500 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1501 {
1502 	dl_capab_vrrp_t	*vrrp;
1503 	uint_t		sub_dl_cap = isub->dl_cap;
1504 	uint8_t		*capend;
1505 
1506 	ASSERT(IAM_WRITER_ILL(ill));
1507 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1508 
1509 	/*
1510 	 * Note: range checks here are not absolutely sufficient to
1511 	 * make us robust against malformed messages sent by drivers;
1512 	 * this is in keeping with the rest of IP's dlpi handling.
1513 	 * (Remember, it's coming from something else in the kernel
1514 	 * address space)
1515 	 */
1516 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1517 	if (capend > mp->b_wptr) {
1518 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1519 		    "malformed sub-capability too long for mblk");
1520 		return;
1521 	}
1522 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1523 
1524 	/*
1525 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1526 	 */
1527 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1528 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1529 		ill->ill_flags |= ILLF_VRRP;
1530 	}
1531 }
1532 
1533 /*
1534  * Process a hardware checksum offload capability negotiation ack received
1535  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1536  * of a DL_CAPABILITY_ACK message.
1537  */
1538 static void
1539 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1540 {
1541 	dl_capability_req_t	*ocap;
1542 	dl_capab_hcksum_t	*ihck, *ohck;
1543 	ill_hcksum_capab_t	**ill_hcksum;
1544 	mblk_t			*nmp = NULL;
1545 	uint_t			sub_dl_cap = isub->dl_cap;
1546 	uint8_t			*capend;
1547 
1548 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1549 
1550 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1551 
1552 	/*
1553 	 * Note: range checks here are not absolutely sufficient to
1554 	 * make us robust against malformed messages sent by drivers;
1555 	 * this is in keeping with the rest of IP's dlpi handling.
1556 	 * (Remember, it's coming from something else in the kernel
1557 	 * address space)
1558 	 */
1559 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1560 	if (capend > mp->b_wptr) {
1561 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1562 		    "malformed sub-capability too long for mblk");
1563 		return;
1564 	}
1565 
1566 	/*
1567 	 * There are two types of acks we process here:
1568 	 * 1. acks in reply to a (first form) generic capability req
1569 	 *    (no ENABLE flag set)
1570 	 * 2. acks in reply to a ENABLE capability req.
1571 	 *    (ENABLE flag set)
1572 	 */
1573 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1574 
1575 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1576 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1577 		    "unsupported hardware checksum "
1578 		    "sub-capability (version %d, expected %d)",
1579 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1580 		return;
1581 	}
1582 
1583 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1584 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1585 		    "checksum capability isn't as expected; pass-thru "
1586 		    "module(s) detected, discarding capability\n"));
1587 		return;
1588 	}
1589 
1590 #define	CURR_HCKSUM_CAPAB				\
1591 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1592 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1593 
1594 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1595 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1596 		/* do ENABLE processing */
1597 		if (*ill_hcksum == NULL) {
1598 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1599 			    KM_NOSLEEP);
1600 
1601 			if (*ill_hcksum == NULL) {
1602 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1603 				    "could not enable hcksum version %d "
1604 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1605 				    ill->ill_name);
1606 				return;
1607 			}
1608 		}
1609 
1610 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1611 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1612 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1613 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1614 		    "has enabled hardware checksumming\n ",
1615 		    ill->ill_name));
1616 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1617 		/*
1618 		 * Enabling hardware checksum offload
1619 		 * Currently IP supports {TCP,UDP}/IPv4
1620 		 * partial and full cksum offload and
1621 		 * IPv4 header checksum offload.
1622 		 * Allocate new mblk which will
1623 		 * contain a new capability request
1624 		 * to enable hardware checksum offload.
1625 		 */
1626 		uint_t	size;
1627 		uchar_t	*rptr;
1628 
1629 		size = sizeof (dl_capability_req_t) +
1630 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1631 
1632 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1633 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1634 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1635 			    ill->ill_name);
1636 			return;
1637 		}
1638 
1639 		rptr = nmp->b_rptr;
1640 		/* initialize dl_capability_req_t */
1641 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1642 		ocap->dl_sub_offset =
1643 		    sizeof (dl_capability_req_t);
1644 		ocap->dl_sub_length =
1645 		    sizeof (dl_capability_sub_t) +
1646 		    isub->dl_length;
1647 		nmp->b_rptr += sizeof (dl_capability_req_t);
1648 
1649 		/* initialize dl_capability_sub_t */
1650 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1651 		nmp->b_rptr += sizeof (*isub);
1652 
1653 		/* initialize dl_capab_hcksum_t */
1654 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1655 		bcopy(ihck, ohck, sizeof (*ihck));
1656 
1657 		nmp->b_rptr = rptr;
1658 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1659 
1660 		/* Set ENABLE flag */
1661 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1662 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1663 
1664 		/*
1665 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1666 		 * hardware checksum acceleration.
1667 		 */
1668 		ill_capability_send(ill, nmp);
1669 	} else {
1670 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1671 		    "advertised %x hardware checksum capability flags\n",
1672 		    ill->ill_name, ihck->hcksum_txflags));
1673 	}
1674 }
1675 
1676 static void
1677 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1678 {
1679 	dl_capab_hcksum_t *hck_subcap;
1680 	dl_capability_sub_t *dl_subcap;
1681 
1682 	if (!ILL_HCKSUM_CAPABLE(ill))
1683 		return;
1684 
1685 	ASSERT(ill->ill_hcksum_capab != NULL);
1686 
1687 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1688 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1689 	dl_subcap->dl_length = sizeof (*hck_subcap);
1690 
1691 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1692 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1693 	hck_subcap->hcksum_txflags = 0;
1694 
1695 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1696 }
1697 
1698 static void
1699 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1700 {
1701 	mblk_t *nmp = NULL;
1702 	dl_capability_req_t *oc;
1703 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1704 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1705 	uint_t sub_dl_cap = isub->dl_cap;
1706 	uint8_t *capend;
1707 
1708 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1709 
1710 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1711 
1712 	/*
1713 	 * Note: range checks here are not absolutely sufficient to
1714 	 * make us robust against malformed messages sent by drivers;
1715 	 * this is in keeping with the rest of IP's dlpi handling.
1716 	 * (Remember, it's coming from something else in the kernel
1717 	 * address space)
1718 	 */
1719 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1720 	if (capend > mp->b_wptr) {
1721 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1722 		    "malformed sub-capability too long for mblk");
1723 		return;
1724 	}
1725 
1726 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1727 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1728 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1729 		    "unsupported ZEROCOPY sub-capability (version %d, "
1730 		    "expected %d)", zc_ic->zerocopy_version,
1731 		    ZEROCOPY_VERSION_1);
1732 		return;
1733 	}
1734 
1735 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1736 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1737 		    "capability isn't as expected; pass-thru module(s) "
1738 		    "detected, discarding capability\n"));
1739 		return;
1740 	}
1741 
1742 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1743 		if (*ill_zerocopy_capab == NULL) {
1744 			*ill_zerocopy_capab =
1745 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1746 			    KM_NOSLEEP);
1747 
1748 			if (*ill_zerocopy_capab == NULL) {
1749 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1750 				    "could not enable Zero-copy version %d "
1751 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1752 				    ill->ill_name);
1753 				return;
1754 			}
1755 		}
1756 
1757 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1758 		    "supports Zero-copy version %d\n", ill->ill_name,
1759 		    ZEROCOPY_VERSION_1));
1760 
1761 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1762 		    zc_ic->zerocopy_version;
1763 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1764 		    zc_ic->zerocopy_flags;
1765 
1766 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1767 	} else {
1768 		uint_t size;
1769 		uchar_t *rptr;
1770 
1771 		size = sizeof (dl_capability_req_t) +
1772 		    sizeof (dl_capability_sub_t) +
1773 		    sizeof (dl_capab_zerocopy_t);
1774 
1775 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1776 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1777 			    "could not enable zerocopy for %s (ENOMEM)\n",
1778 			    ill->ill_name);
1779 			return;
1780 		}
1781 
1782 		rptr = nmp->b_rptr;
1783 		/* initialize dl_capability_req_t */
1784 		oc = (dl_capability_req_t *)rptr;
1785 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1786 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1787 		    sizeof (dl_capab_zerocopy_t);
1788 		rptr += sizeof (dl_capability_req_t);
1789 
1790 		/* initialize dl_capability_sub_t */
1791 		bcopy(isub, rptr, sizeof (*isub));
1792 		rptr += sizeof (*isub);
1793 
1794 		/* initialize dl_capab_zerocopy_t */
1795 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1796 		*zc_oc = *zc_ic;
1797 
1798 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1799 		    "to enable zero-copy version %d\n", ill->ill_name,
1800 		    ZEROCOPY_VERSION_1));
1801 
1802 		/* set VMSAFE_MEM flag */
1803 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1804 
1805 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1806 		ill_capability_send(ill, nmp);
1807 	}
1808 }
1809 
1810 static void
1811 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1812 {
1813 	dl_capab_zerocopy_t *zerocopy_subcap;
1814 	dl_capability_sub_t *dl_subcap;
1815 
1816 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1817 		return;
1818 
1819 	ASSERT(ill->ill_zerocopy_capab != NULL);
1820 
1821 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1822 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1823 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1824 
1825 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1826 	zerocopy_subcap->zerocopy_version =
1827 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1828 	zerocopy_subcap->zerocopy_flags = 0;
1829 
1830 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1831 }
1832 
1833 /*
1834  * DLD capability
1835  * Refer to dld.h for more information regarding the purpose and usage
1836  * of this capability.
1837  */
1838 static void
1839 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1840 {
1841 	dl_capab_dld_t		*dld_ic, dld;
1842 	uint_t			sub_dl_cap = isub->dl_cap;
1843 	uint8_t			*capend;
1844 	ill_dld_capab_t		*idc;
1845 
1846 	ASSERT(IAM_WRITER_ILL(ill));
1847 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1848 
1849 	/*
1850 	 * Note: range checks here are not absolutely sufficient to
1851 	 * make us robust against malformed messages sent by drivers;
1852 	 * this is in keeping with the rest of IP's dlpi handling.
1853 	 * (Remember, it's coming from something else in the kernel
1854 	 * address space)
1855 	 */
1856 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1857 	if (capend > mp->b_wptr) {
1858 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1859 		    "malformed sub-capability too long for mblk");
1860 		return;
1861 	}
1862 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1863 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1864 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1865 		    "unsupported DLD sub-capability (version %d, "
1866 		    "expected %d)", dld_ic->dld_version,
1867 		    DLD_CURRENT_VERSION);
1868 		return;
1869 	}
1870 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1871 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1872 		    "capability isn't as expected; pass-thru module(s) "
1873 		    "detected, discarding capability\n"));
1874 		return;
1875 	}
1876 
1877 	/*
1878 	 * Copy locally to ensure alignment.
1879 	 */
1880 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1881 
1882 	if ((idc = ill->ill_dld_capab) == NULL) {
1883 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1884 		if (idc == NULL) {
1885 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1886 			    "could not enable DLD version %d "
1887 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1888 			    ill->ill_name);
1889 			return;
1890 		}
1891 		ill->ill_dld_capab = idc;
1892 	}
1893 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1894 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1895 	ip1dbg(("ill_capability_dld_ack: interface %s "
1896 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1897 
1898 	ill_capability_dld_enable(ill);
1899 }
1900 
1901 /*
1902  * Typically capability negotiation between IP and the driver happens via
1903  * DLPI message exchange. However GLD also offers a direct function call
1904  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1905  * But arbitrary function calls into IP or GLD are not permitted, since both
1906  * of them are protected by their own perimeter mechanism. The perimeter can
1907  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1908  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1909  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1910  * to enter the mac perimeter and then do the direct function calls into
1911  * GLD to enable squeue polling. The ring related callbacks from the mac into
1912  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1913  * protected by the mac perimeter.
1914  */
1915 static void
1916 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1917 {
1918 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1919 	int			err;
1920 
1921 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1922 	    DLD_ENABLE);
1923 	ASSERT(err == 0);
1924 }
1925 
1926 static void
1927 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1928 {
1929 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1930 	int			err;
1931 
1932 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1933 	    DLD_DISABLE);
1934 	ASSERT(err == 0);
1935 }
1936 
1937 boolean_t
1938 ill_mac_perim_held(ill_t *ill)
1939 {
1940 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1941 
1942 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1943 	    DLD_QUERY));
1944 }
1945 
1946 static void
1947 ill_capability_direct_enable(ill_t *ill)
1948 {
1949 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1950 	ill_dld_direct_t	*idd = &idc->idc_direct;
1951 	dld_capab_direct_t	direct;
1952 	int			rc;
1953 
1954 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1955 
1956 	bzero(&direct, sizeof (direct));
1957 	direct.di_rx_cf = (uintptr_t)ip_input;
1958 	direct.di_rx_ch = ill;
1959 
1960 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1961 	    DLD_ENABLE);
1962 	if (rc == 0) {
1963 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1964 		idd->idd_tx_dh = direct.di_tx_dh;
1965 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1966 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1967 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1968 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1969 		ASSERT(idd->idd_tx_cb_df != NULL);
1970 		ASSERT(idd->idd_tx_fctl_df != NULL);
1971 		ASSERT(idd->idd_tx_df != NULL);
1972 		/*
1973 		 * One time registration of flow enable callback function
1974 		 */
1975 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
1976 		    ill_flow_enable, ill);
1977 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
1978 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
1979 	} else {
1980 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
1981 		    "capability, rc = %d\n", rc);
1982 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
1983 	}
1984 }
1985 
1986 static void
1987 ill_capability_poll_enable(ill_t *ill)
1988 {
1989 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1990 	dld_capab_poll_t	poll;
1991 	int			rc;
1992 
1993 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1994 
1995 	bzero(&poll, sizeof (poll));
1996 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
1997 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
1998 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
1999 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2000 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2001 	poll.poll_ring_ch = ill;
2002 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2003 	    DLD_ENABLE);
2004 	if (rc == 0) {
2005 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2006 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2007 	} else {
2008 		ip1dbg(("warning: could not enable POLL "
2009 		    "capability, rc = %d\n", rc));
2010 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2011 	}
2012 }
2013 
2014 /*
2015  * Enable the LSO capability.
2016  */
2017 static void
2018 ill_capability_lso_enable(ill_t *ill)
2019 {
2020 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2021 	dld_capab_lso_t	lso;
2022 	int rc;
2023 
2024 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2025 
2026 	if (ill->ill_lso_capab == NULL) {
2027 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2028 		    KM_NOSLEEP);
2029 		if (ill->ill_lso_capab == NULL) {
2030 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2031 			    "could not enable LSO for %s (ENOMEM)\n",
2032 			    ill->ill_name);
2033 			return;
2034 		}
2035 	}
2036 
2037 	bzero(&lso, sizeof (lso));
2038 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2039 	    DLD_ENABLE)) == 0) {
2040 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2041 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2042 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2043 		ip1dbg(("ill_capability_lso_enable: interface %s "
2044 		    "has enabled LSO\n ", ill->ill_name));
2045 	} else {
2046 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2047 		ill->ill_lso_capab = NULL;
2048 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2049 	}
2050 }
2051 
2052 static void
2053 ill_capability_dld_enable(ill_t *ill)
2054 {
2055 	mac_perim_handle_t mph;
2056 
2057 	ASSERT(IAM_WRITER_ILL(ill));
2058 
2059 	if (ill->ill_isv6)
2060 		return;
2061 
2062 	ill_mac_perim_enter(ill, &mph);
2063 	if (!ill->ill_isv6) {
2064 		ill_capability_direct_enable(ill);
2065 		ill_capability_poll_enable(ill);
2066 		ill_capability_lso_enable(ill);
2067 	}
2068 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2069 	ill_mac_perim_exit(ill, mph);
2070 }
2071 
2072 static void
2073 ill_capability_dld_disable(ill_t *ill)
2074 {
2075 	ill_dld_capab_t	*idc;
2076 	ill_dld_direct_t *idd;
2077 	mac_perim_handle_t	mph;
2078 
2079 	ASSERT(IAM_WRITER_ILL(ill));
2080 
2081 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2082 		return;
2083 
2084 	ill_mac_perim_enter(ill, &mph);
2085 
2086 	idc = ill->ill_dld_capab;
2087 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2088 		/*
2089 		 * For performance we avoid locks in the transmit data path
2090 		 * and don't maintain a count of the number of threads using
2091 		 * direct calls. Thus some threads could be using direct
2092 		 * transmit calls to GLD, even after the capability mechanism
2093 		 * turns it off. This is still safe since the handles used in
2094 		 * the direct calls continue to be valid until the unplumb is
2095 		 * completed. Remove the callback that was added (1-time) at
2096 		 * capab enable time.
2097 		 */
2098 		mutex_enter(&ill->ill_lock);
2099 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2100 		mutex_exit(&ill->ill_lock);
2101 		if (ill->ill_flownotify_mh != NULL) {
2102 			idd = &idc->idc_direct;
2103 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2104 			    ill->ill_flownotify_mh);
2105 			ill->ill_flownotify_mh = NULL;
2106 		}
2107 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2108 		    NULL, DLD_DISABLE);
2109 	}
2110 
2111 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2112 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2113 		ip_squeue_clean_all(ill);
2114 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2115 		    NULL, DLD_DISABLE);
2116 	}
2117 
2118 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2119 		ASSERT(ill->ill_lso_capab != NULL);
2120 		/*
2121 		 * Clear the capability flag for LSO but retain the
2122 		 * ill_lso_capab structure since it's possible that another
2123 		 * thread is still referring to it.  The structure only gets
2124 		 * deallocated when we destroy the ill.
2125 		 */
2126 
2127 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2128 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2129 		    NULL, DLD_DISABLE);
2130 	}
2131 
2132 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2133 	ill_mac_perim_exit(ill, mph);
2134 }
2135 
2136 /*
2137  * Capability Negotiation protocol
2138  *
2139  * We don't wait for DLPI capability operations to finish during interface
2140  * bringup or teardown. Doing so would introduce more asynchrony and the
2141  * interface up/down operations will need multiple return and restarts.
2142  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2143  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2144  * exclusive operation won't start until the DLPI operations of the previous
2145  * exclusive operation complete.
2146  *
2147  * The capability state machine is shown below.
2148  *
2149  * state		next state		event, action
2150  *
2151  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2152  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2153  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2154  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2155  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2156  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2157  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2158  *						    ill_capability_probe.
2159  */
2160 
2161 /*
2162  * Dedicated thread started from ip_stack_init that handles capability
2163  * disable. This thread ensures the taskq dispatch does not fail by waiting
2164  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2165  * that direct calls to DLD are done in a cv_waitable context.
2166  */
2167 void
2168 ill_taskq_dispatch(ip_stack_t *ipst)
2169 {
2170 	callb_cpr_t cprinfo;
2171 	char 	name[64];
2172 	mblk_t	*mp;
2173 
2174 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2175 	    ipst->ips_netstack->netstack_stackid);
2176 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2177 	    name);
2178 	mutex_enter(&ipst->ips_capab_taskq_lock);
2179 
2180 	for (;;) {
2181 		mp = ipst->ips_capab_taskq_head;
2182 		while (mp != NULL) {
2183 			ipst->ips_capab_taskq_head = mp->b_next;
2184 			if (ipst->ips_capab_taskq_head == NULL)
2185 				ipst->ips_capab_taskq_tail = NULL;
2186 			mutex_exit(&ipst->ips_capab_taskq_lock);
2187 			mp->b_next = NULL;
2188 
2189 			VERIFY(taskq_dispatch(system_taskq,
2190 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2191 			mutex_enter(&ipst->ips_capab_taskq_lock);
2192 			mp = ipst->ips_capab_taskq_head;
2193 		}
2194 
2195 		if (ipst->ips_capab_taskq_quit)
2196 			break;
2197 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2198 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2199 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2200 	}
2201 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2202 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2203 	CALLB_CPR_EXIT(&cprinfo);
2204 	thread_exit();
2205 }
2206 
2207 /*
2208  * Consume a new-style hardware capabilities negotiation ack.
2209  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2210  */
2211 static void
2212 ill_capability_ack_thr(void *arg)
2213 {
2214 	mblk_t	*mp = arg;
2215 	dl_capability_ack_t *capp;
2216 	dl_capability_sub_t *subp, *endp;
2217 	ill_t	*ill;
2218 	boolean_t reneg;
2219 
2220 	ill = (ill_t *)mp->b_prev;
2221 	mp->b_prev = NULL;
2222 
2223 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2224 
2225 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2226 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2227 		/*
2228 		 * We have received the ack for our DL_CAPAB reset request.
2229 		 * There isnt' anything in the message that needs processing.
2230 		 * All message based capabilities have been disabled, now
2231 		 * do the function call based capability disable.
2232 		 */
2233 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2234 		ill_capability_dld_disable(ill);
2235 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2236 		if (reneg)
2237 			ill_capability_probe(ill);
2238 		goto done;
2239 	}
2240 
2241 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2242 		ill->ill_dlpi_capab_state = IDCS_OK;
2243 
2244 	capp = (dl_capability_ack_t *)mp->b_rptr;
2245 
2246 	if (capp->dl_sub_length == 0) {
2247 		/* no new-style capabilities */
2248 		goto done;
2249 	}
2250 
2251 	/* make sure the driver supplied correct dl_sub_length */
2252 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2253 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2254 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2255 		goto done;
2256 	}
2257 
2258 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2259 	/*
2260 	 * There are sub-capabilities. Process the ones we know about.
2261 	 * Loop until we don't have room for another sub-cap header..
2262 	 */
2263 	for (subp = SC(capp, capp->dl_sub_offset),
2264 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2265 	    subp <= endp;
2266 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2267 
2268 		switch (subp->dl_cap) {
2269 		case DL_CAPAB_ID_WRAPPER:
2270 			ill_capability_id_ack(ill, mp, subp);
2271 			break;
2272 		default:
2273 			ill_capability_dispatch(ill, mp, subp);
2274 			break;
2275 		}
2276 	}
2277 #undef SC
2278 done:
2279 	inet_freemsg(mp);
2280 	ill_capability_done(ill);
2281 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2282 }
2283 
2284 /*
2285  * This needs to be started in a taskq thread to provide a cv_waitable
2286  * context.
2287  */
2288 void
2289 ill_capability_ack(ill_t *ill, mblk_t *mp)
2290 {
2291 	ip_stack_t	*ipst = ill->ill_ipst;
2292 
2293 	mp->b_prev = (mblk_t *)ill;
2294 	ASSERT(mp->b_next == NULL);
2295 
2296 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2297 	    TQ_NOSLEEP) != 0)
2298 		return;
2299 
2300 	/*
2301 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2302 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2303 	 */
2304 	mutex_enter(&ipst->ips_capab_taskq_lock);
2305 	if (ipst->ips_capab_taskq_head == NULL) {
2306 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2307 		ipst->ips_capab_taskq_head = mp;
2308 	} else {
2309 		ipst->ips_capab_taskq_tail->b_next = mp;
2310 	}
2311 	ipst->ips_capab_taskq_tail = mp;
2312 
2313 	cv_signal(&ipst->ips_capab_taskq_cv);
2314 	mutex_exit(&ipst->ips_capab_taskq_lock);
2315 }
2316 
2317 /*
2318  * This routine is called to scan the fragmentation reassembly table for
2319  * the specified ILL for any packets that are starting to smell.
2320  * dead_interval is the maximum time in seconds that will be tolerated.  It
2321  * will either be the value specified in ip_g_frag_timeout, or zero if the
2322  * ILL is shutting down and it is time to blow everything off.
2323  *
2324  * It returns the number of seconds (as a time_t) that the next frag timer
2325  * should be scheduled for, 0 meaning that the timer doesn't need to be
2326  * re-started.  Note that the method of calculating next_timeout isn't
2327  * entirely accurate since time will flow between the time we grab
2328  * current_time and the time we schedule the next timeout.  This isn't a
2329  * big problem since this is the timer for sending an ICMP reassembly time
2330  * exceeded messages, and it doesn't have to be exactly accurate.
2331  *
2332  * This function is
2333  * sometimes called as writer, although this is not required.
2334  */
2335 time_t
2336 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2337 {
2338 	ipfb_t	*ipfb;
2339 	ipfb_t	*endp;
2340 	ipf_t	*ipf;
2341 	ipf_t	*ipfnext;
2342 	mblk_t	*mp;
2343 	time_t	current_time = gethrestime_sec();
2344 	time_t	next_timeout = 0;
2345 	uint32_t	hdr_length;
2346 	mblk_t	*send_icmp_head;
2347 	mblk_t	*send_icmp_head_v6;
2348 	ip_stack_t *ipst = ill->ill_ipst;
2349 	ip_recv_attr_t iras;
2350 
2351 	bzero(&iras, sizeof (iras));
2352 	iras.ira_flags = 0;
2353 	iras.ira_ill = iras.ira_rill = ill;
2354 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2355 	iras.ira_rifindex = iras.ira_ruifindex;
2356 
2357 	ipfb = ill->ill_frag_hash_tbl;
2358 	if (ipfb == NULL)
2359 		return (B_FALSE);
2360 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2361 	/* Walk the frag hash table. */
2362 	for (; ipfb < endp; ipfb++) {
2363 		send_icmp_head = NULL;
2364 		send_icmp_head_v6 = NULL;
2365 		mutex_enter(&ipfb->ipfb_lock);
2366 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2367 			time_t frag_time = current_time - ipf->ipf_timestamp;
2368 			time_t frag_timeout;
2369 
2370 			if (frag_time < dead_interval) {
2371 				/*
2372 				 * There are some outstanding fragments
2373 				 * that will timeout later.  Make note of
2374 				 * the time so that we can reschedule the
2375 				 * next timeout appropriately.
2376 				 */
2377 				frag_timeout = dead_interval - frag_time;
2378 				if (next_timeout == 0 ||
2379 				    frag_timeout < next_timeout) {
2380 					next_timeout = frag_timeout;
2381 				}
2382 				break;
2383 			}
2384 			/* Time's up.  Get it out of here. */
2385 			hdr_length = ipf->ipf_nf_hdr_len;
2386 			ipfnext = ipf->ipf_hash_next;
2387 			if (ipfnext)
2388 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2389 			*ipf->ipf_ptphn = ipfnext;
2390 			mp = ipf->ipf_mp->b_cont;
2391 			for (; mp; mp = mp->b_cont) {
2392 				/* Extra points for neatness. */
2393 				IP_REASS_SET_START(mp, 0);
2394 				IP_REASS_SET_END(mp, 0);
2395 			}
2396 			mp = ipf->ipf_mp->b_cont;
2397 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2398 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2399 			ipfb->ipfb_count -= ipf->ipf_count;
2400 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2401 			ipfb->ipfb_frag_pkts--;
2402 			/*
2403 			 * We do not send any icmp message from here because
2404 			 * we currently are holding the ipfb_lock for this
2405 			 * hash chain. If we try and send any icmp messages
2406 			 * from here we may end up via a put back into ip
2407 			 * trying to get the same lock, causing a recursive
2408 			 * mutex panic. Instead we build a list and send all
2409 			 * the icmp messages after we have dropped the lock.
2410 			 */
2411 			if (ill->ill_isv6) {
2412 				if (hdr_length != 0) {
2413 					mp->b_next = send_icmp_head_v6;
2414 					send_icmp_head_v6 = mp;
2415 				} else {
2416 					freemsg(mp);
2417 				}
2418 			} else {
2419 				if (hdr_length != 0) {
2420 					mp->b_next = send_icmp_head;
2421 					send_icmp_head = mp;
2422 				} else {
2423 					freemsg(mp);
2424 				}
2425 			}
2426 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2427 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2428 			freeb(ipf->ipf_mp);
2429 		}
2430 		mutex_exit(&ipfb->ipfb_lock);
2431 		/*
2432 		 * Now need to send any icmp messages that we delayed from
2433 		 * above.
2434 		 */
2435 		while (send_icmp_head_v6 != NULL) {
2436 			ip6_t *ip6h;
2437 
2438 			mp = send_icmp_head_v6;
2439 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2440 			mp->b_next = NULL;
2441 			ip6h = (ip6_t *)mp->b_rptr;
2442 			iras.ira_flags = 0;
2443 			/*
2444 			 * This will result in an incorrect ALL_ZONES zoneid
2445 			 * for multicast packets, but we
2446 			 * don't send ICMP errors for those in any case.
2447 			 */
2448 			iras.ira_zoneid =
2449 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2450 			    ill, ipst);
2451 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2452 			icmp_time_exceeded_v6(mp,
2453 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2454 			    &iras);
2455 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2456 		}
2457 		while (send_icmp_head != NULL) {
2458 			ipaddr_t dst;
2459 
2460 			mp = send_icmp_head;
2461 			send_icmp_head = send_icmp_head->b_next;
2462 			mp->b_next = NULL;
2463 
2464 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2465 
2466 			iras.ira_flags = IRAF_IS_IPV4;
2467 			/*
2468 			 * This will result in an incorrect ALL_ZONES zoneid
2469 			 * for broadcast and multicast packets, but we
2470 			 * don't send ICMP errors for those in any case.
2471 			 */
2472 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2473 			    ill, ipst);
2474 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2475 			icmp_time_exceeded(mp,
2476 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2477 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2478 		}
2479 	}
2480 	/*
2481 	 * A non-dying ILL will use the return value to decide whether to
2482 	 * restart the frag timer, and for how long.
2483 	 */
2484 	return (next_timeout);
2485 }
2486 
2487 /*
2488  * This routine is called when the approximate count of mblk memory used
2489  * for the specified ILL has exceeded max_count.
2490  */
2491 void
2492 ill_frag_prune(ill_t *ill, uint_t max_count)
2493 {
2494 	ipfb_t	*ipfb;
2495 	ipf_t	*ipf;
2496 	size_t	count;
2497 	clock_t now;
2498 
2499 	/*
2500 	 * If we are here within ip_min_frag_prune_time msecs remove
2501 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2502 	 * ill_frag_free_num_pkts.
2503 	 */
2504 	mutex_enter(&ill->ill_lock);
2505 	now = ddi_get_lbolt();
2506 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2507 	    (ip_min_frag_prune_time != 0 ?
2508 	    ip_min_frag_prune_time : msec_per_tick)) {
2509 
2510 		ill->ill_frag_free_num_pkts++;
2511 
2512 	} else {
2513 		ill->ill_frag_free_num_pkts = 0;
2514 	}
2515 	ill->ill_last_frag_clean_time = now;
2516 	mutex_exit(&ill->ill_lock);
2517 
2518 	/*
2519 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2520 	 */
2521 	if (ill->ill_frag_free_num_pkts != 0) {
2522 		int ix;
2523 
2524 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2525 			ipfb = &ill->ill_frag_hash_tbl[ix];
2526 			mutex_enter(&ipfb->ipfb_lock);
2527 			if (ipfb->ipfb_ipf != NULL) {
2528 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2529 				    ill->ill_frag_free_num_pkts);
2530 			}
2531 			mutex_exit(&ipfb->ipfb_lock);
2532 		}
2533 	}
2534 	/*
2535 	 * While the reassembly list for this ILL is too big, prune a fragment
2536 	 * queue by age, oldest first.
2537 	 */
2538 	while (ill->ill_frag_count > max_count) {
2539 		int	ix;
2540 		ipfb_t	*oipfb = NULL;
2541 		uint_t	oldest = UINT_MAX;
2542 
2543 		count = 0;
2544 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2545 			ipfb = &ill->ill_frag_hash_tbl[ix];
2546 			mutex_enter(&ipfb->ipfb_lock);
2547 			ipf = ipfb->ipfb_ipf;
2548 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2549 				oldest = ipf->ipf_gen;
2550 				oipfb = ipfb;
2551 			}
2552 			count += ipfb->ipfb_count;
2553 			mutex_exit(&ipfb->ipfb_lock);
2554 		}
2555 		if (oipfb == NULL)
2556 			break;
2557 
2558 		if (count <= max_count)
2559 			return;	/* Somebody beat us to it, nothing to do */
2560 		mutex_enter(&oipfb->ipfb_lock);
2561 		ipf = oipfb->ipfb_ipf;
2562 		if (ipf != NULL) {
2563 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2564 		}
2565 		mutex_exit(&oipfb->ipfb_lock);
2566 	}
2567 }
2568 
2569 /*
2570  * free 'free_cnt' fragmented packets starting at ipf.
2571  */
2572 void
2573 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2574 {
2575 	size_t	count;
2576 	mblk_t	*mp;
2577 	mblk_t	*tmp;
2578 	ipf_t **ipfp = ipf->ipf_ptphn;
2579 
2580 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2581 	ASSERT(ipfp != NULL);
2582 	ASSERT(ipf != NULL);
2583 
2584 	while (ipf != NULL && free_cnt-- > 0) {
2585 		count = ipf->ipf_count;
2586 		mp = ipf->ipf_mp;
2587 		ipf = ipf->ipf_hash_next;
2588 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2589 			IP_REASS_SET_START(tmp, 0);
2590 			IP_REASS_SET_END(tmp, 0);
2591 		}
2592 		atomic_add_32(&ill->ill_frag_count, -count);
2593 		ASSERT(ipfb->ipfb_count >= count);
2594 		ipfb->ipfb_count -= count;
2595 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2596 		ipfb->ipfb_frag_pkts--;
2597 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2598 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2599 		freemsg(mp);
2600 	}
2601 
2602 	if (ipf)
2603 		ipf->ipf_ptphn = ipfp;
2604 	ipfp[0] = ipf;
2605 }
2606 
2607 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2608 	"obsolete and may be removed in a future release of Solaris.  Use " \
2609 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2610 
2611 /*
2612  * For obsolete per-interface forwarding configuration;
2613  * called in response to ND_GET.
2614  */
2615 /* ARGSUSED */
2616 static int
2617 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2618 {
2619 	ill_t *ill = (ill_t *)cp;
2620 
2621 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2622 
2623 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2624 	return (0);
2625 }
2626 
2627 /*
2628  * For obsolete per-interface forwarding configuration;
2629  * called in response to ND_SET.
2630  */
2631 /* ARGSUSED */
2632 static int
2633 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2634     cred_t *ioc_cr)
2635 {
2636 	long value;
2637 	int retval;
2638 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2639 
2640 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2641 
2642 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2643 	    value < 0 || value > 1) {
2644 		return (EINVAL);
2645 	}
2646 
2647 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2648 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2649 	rw_exit(&ipst->ips_ill_g_lock);
2650 	return (retval);
2651 }
2652 
2653 /*
2654  * Helper function for ill_forward_set().
2655  */
2656 static void
2657 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2658 {
2659 	ip_stack_t	*ipst = ill->ill_ipst;
2660 
2661 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2662 
2663 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2664 	    (enable ? "Enabling" : "Disabling"),
2665 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2666 	mutex_enter(&ill->ill_lock);
2667 	if (enable)
2668 		ill->ill_flags |= ILLF_ROUTER;
2669 	else
2670 		ill->ill_flags &= ~ILLF_ROUTER;
2671 	mutex_exit(&ill->ill_lock);
2672 	if (ill->ill_isv6)
2673 		ill_set_nce_router_flags(ill, enable);
2674 	/* Notify routing socket listeners of this change. */
2675 	if (ill->ill_ipif != NULL)
2676 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2677 }
2678 
2679 /*
2680  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2681  * socket messages for each interface whose flags we change.
2682  */
2683 int
2684 ill_forward_set(ill_t *ill, boolean_t enable)
2685 {
2686 	ipmp_illgrp_t *illg;
2687 	ip_stack_t *ipst = ill->ill_ipst;
2688 
2689 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2690 
2691 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2692 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2693 		return (0);
2694 
2695 	if (IS_LOOPBACK(ill))
2696 		return (EINVAL);
2697 
2698 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2699 		/*
2700 		 * Update all of the interfaces in the group.
2701 		 */
2702 		illg = ill->ill_grp;
2703 		ill = list_head(&illg->ig_if);
2704 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2705 			ill_forward_set_on_ill(ill, enable);
2706 
2707 		/*
2708 		 * Update the IPMP meta-interface.
2709 		 */
2710 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2711 		return (0);
2712 	}
2713 
2714 	ill_forward_set_on_ill(ill, enable);
2715 	return (0);
2716 }
2717 
2718 /*
2719  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2720  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2721  * set or clear.
2722  */
2723 static void
2724 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2725 {
2726 	ipif_t *ipif;
2727 	ncec_t *ncec;
2728 	nce_t *nce;
2729 
2730 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2731 		/*
2732 		 * NOTE: we match across the illgrp because nce's for
2733 		 * addresses on IPMP interfaces have an nce_ill that points to
2734 		 * the bound underlying ill.
2735 		 */
2736 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2737 		if (nce != NULL) {
2738 			ncec = nce->nce_common;
2739 			mutex_enter(&ncec->ncec_lock);
2740 			if (enable)
2741 				ncec->ncec_flags |= NCE_F_ISROUTER;
2742 			else
2743 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2744 			mutex_exit(&ncec->ncec_lock);
2745 			nce_refrele(nce);
2746 		}
2747 	}
2748 }
2749 
2750 /*
2751  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2752  * for this ill.  Make sure the v6/v4 question has been answered about this
2753  * ill.  The creation of this ndd variable is only for backwards compatibility.
2754  * The preferred way to control per-interface IP forwarding is through the
2755  * ILLF_ROUTER interface flag.
2756  */
2757 static int
2758 ill_set_ndd_name(ill_t *ill)
2759 {
2760 	char *suffix;
2761 	ip_stack_t	*ipst = ill->ill_ipst;
2762 
2763 	ASSERT(IAM_WRITER_ILL(ill));
2764 
2765 	if (ill->ill_isv6)
2766 		suffix = ipv6_forward_suffix;
2767 	else
2768 		suffix = ipv4_forward_suffix;
2769 
2770 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2771 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2772 	/*
2773 	 * Copies over the '\0'.
2774 	 * Note that strlen(suffix) is always bounded.
2775 	 */
2776 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2777 	    strlen(suffix) + 1);
2778 
2779 	/*
2780 	 * Use of the nd table requires holding the reader lock.
2781 	 * Modifying the nd table thru nd_load/nd_unload requires
2782 	 * the writer lock.
2783 	 */
2784 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2785 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2786 	    nd_ill_forward_set, (caddr_t)ill)) {
2787 		/*
2788 		 * If the nd_load failed, it only meant that it could not
2789 		 * allocate a new bunch of room for further NDD expansion.
2790 		 * Because of that, the ill_ndd_name will be set to 0, and
2791 		 * this interface is at the mercy of the global ip_forwarding
2792 		 * variable.
2793 		 */
2794 		rw_exit(&ipst->ips_ip_g_nd_lock);
2795 		ill->ill_ndd_name = NULL;
2796 		return (ENOMEM);
2797 	}
2798 	rw_exit(&ipst->ips_ip_g_nd_lock);
2799 	return (0);
2800 }
2801 
2802 /*
2803  * Intializes the context structure and returns the first ill in the list
2804  * cuurently start_list and end_list can have values:
2805  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2806  * IP_V4_G_HEAD		Traverse IPV4 list only.
2807  * IP_V6_G_HEAD		Traverse IPV6 list only.
2808  */
2809 
2810 /*
2811  * We don't check for CONDEMNED ills here. Caller must do that if
2812  * necessary under the ill lock.
2813  */
2814 ill_t *
2815 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2816     ip_stack_t *ipst)
2817 {
2818 	ill_if_t *ifp;
2819 	ill_t *ill;
2820 	avl_tree_t *avl_tree;
2821 
2822 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2823 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2824 
2825 	/*
2826 	 * setup the lists to search
2827 	 */
2828 	if (end_list != MAX_G_HEADS) {
2829 		ctx->ctx_current_list = start_list;
2830 		ctx->ctx_last_list = end_list;
2831 	} else {
2832 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2833 		ctx->ctx_current_list = 0;
2834 	}
2835 
2836 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2837 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2838 		if (ifp != (ill_if_t *)
2839 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2840 			avl_tree = &ifp->illif_avl_by_ppa;
2841 			ill = avl_first(avl_tree);
2842 			/*
2843 			 * ill is guaranteed to be non NULL or ifp should have
2844 			 * not existed.
2845 			 */
2846 			ASSERT(ill != NULL);
2847 			return (ill);
2848 		}
2849 		ctx->ctx_current_list++;
2850 	}
2851 
2852 	return (NULL);
2853 }
2854 
2855 /*
2856  * returns the next ill in the list. ill_first() must have been called
2857  * before calling ill_next() or bad things will happen.
2858  */
2859 
2860 /*
2861  * We don't check for CONDEMNED ills here. Caller must do that if
2862  * necessary under the ill lock.
2863  */
2864 ill_t *
2865 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2866 {
2867 	ill_if_t *ifp;
2868 	ill_t *ill;
2869 	ip_stack_t	*ipst = lastill->ill_ipst;
2870 
2871 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2872 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2873 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2874 	    AVL_AFTER)) != NULL) {
2875 		return (ill);
2876 	}
2877 
2878 	/* goto next ill_ifp in the list. */
2879 	ifp = lastill->ill_ifptr->illif_next;
2880 
2881 	/* make sure not at end of circular list */
2882 	while (ifp ==
2883 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2884 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2885 			return (NULL);
2886 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2887 	}
2888 
2889 	return (avl_first(&ifp->illif_avl_by_ppa));
2890 }
2891 
2892 /*
2893  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2894  * The final number (PPA) must not have any leading zeros.  Upon success, a
2895  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2896  */
2897 static char *
2898 ill_get_ppa_ptr(char *name)
2899 {
2900 	int namelen = strlen(name);
2901 	int end_ndx = namelen - 1;
2902 	int ppa_ndx, i;
2903 
2904 	/*
2905 	 * Check that the first character is [a-zA-Z], and that the last
2906 	 * character is [0-9].
2907 	 */
2908 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2909 		return (NULL);
2910 
2911 	/*
2912 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2913 	 */
2914 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2915 		if (!isdigit(name[ppa_ndx - 1]))
2916 			break;
2917 
2918 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2919 		return (NULL);
2920 
2921 	/*
2922 	 * Check that the intermediate characters are [a-z0-9.]
2923 	 */
2924 	for (i = 1; i < ppa_ndx; i++) {
2925 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2926 		    name[i] != '.' && name[i] != '_') {
2927 			return (NULL);
2928 		}
2929 	}
2930 
2931 	return (name + ppa_ndx);
2932 }
2933 
2934 /*
2935  * use avl tree to locate the ill.
2936  */
2937 static ill_t *
2938 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2939 {
2940 	char *ppa_ptr = NULL;
2941 	int len;
2942 	uint_t ppa;
2943 	ill_t *ill = NULL;
2944 	ill_if_t *ifp;
2945 	int list;
2946 
2947 	/*
2948 	 * get ppa ptr
2949 	 */
2950 	if (isv6)
2951 		list = IP_V6_G_HEAD;
2952 	else
2953 		list = IP_V4_G_HEAD;
2954 
2955 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2956 		return (NULL);
2957 	}
2958 
2959 	len = ppa_ptr - name + 1;
2960 
2961 	ppa = stoi(&ppa_ptr);
2962 
2963 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2964 
2965 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2966 		/*
2967 		 * match is done on len - 1 as the name is not null
2968 		 * terminated it contains ppa in addition to the interface
2969 		 * name.
2970 		 */
2971 		if ((ifp->illif_name_len == len) &&
2972 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2973 			break;
2974 		} else {
2975 			ifp = ifp->illif_next;
2976 		}
2977 	}
2978 
2979 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2980 		/*
2981 		 * Even the interface type does not exist.
2982 		 */
2983 		return (NULL);
2984 	}
2985 
2986 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2987 	if (ill != NULL) {
2988 		mutex_enter(&ill->ill_lock);
2989 		if (ILL_CAN_LOOKUP(ill)) {
2990 			ill_refhold_locked(ill);
2991 			mutex_exit(&ill->ill_lock);
2992 			return (ill);
2993 		}
2994 		mutex_exit(&ill->ill_lock);
2995 	}
2996 	return (NULL);
2997 }
2998 
2999 /*
3000  * comparison function for use with avl.
3001  */
3002 static int
3003 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3004 {
3005 	uint_t ppa;
3006 	uint_t ill_ppa;
3007 
3008 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3009 
3010 	ppa = *((uint_t *)ppa_ptr);
3011 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3012 	/*
3013 	 * We want the ill with the lowest ppa to be on the
3014 	 * top.
3015 	 */
3016 	if (ill_ppa < ppa)
3017 		return (1);
3018 	if (ill_ppa > ppa)
3019 		return (-1);
3020 	return (0);
3021 }
3022 
3023 /*
3024  * remove an interface type from the global list.
3025  */
3026 static void
3027 ill_delete_interface_type(ill_if_t *interface)
3028 {
3029 	ASSERT(interface != NULL);
3030 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3031 
3032 	avl_destroy(&interface->illif_avl_by_ppa);
3033 	if (interface->illif_ppa_arena != NULL)
3034 		vmem_destroy(interface->illif_ppa_arena);
3035 
3036 	remque(interface);
3037 
3038 	mi_free(interface);
3039 }
3040 
3041 /*
3042  * remove ill from the global list.
3043  */
3044 static void
3045 ill_glist_delete(ill_t *ill)
3046 {
3047 	ip_stack_t	*ipst;
3048 	phyint_t	*phyi;
3049 
3050 	if (ill == NULL)
3051 		return;
3052 	ipst = ill->ill_ipst;
3053 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3054 
3055 	/*
3056 	 * If the ill was never inserted into the AVL tree
3057 	 * we skip the if branch.
3058 	 */
3059 	if (ill->ill_ifptr != NULL) {
3060 		/*
3061 		 * remove from AVL tree and free ppa number
3062 		 */
3063 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3064 
3065 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3066 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3067 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3068 		}
3069 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3070 			ill_delete_interface_type(ill->ill_ifptr);
3071 		}
3072 
3073 		/*
3074 		 * Indicate ill is no longer in the list.
3075 		 */
3076 		ill->ill_ifptr = NULL;
3077 		ill->ill_name_length = 0;
3078 		ill->ill_name[0] = '\0';
3079 		ill->ill_ppa = UINT_MAX;
3080 	}
3081 
3082 	/* Generate one last event for this ill. */
3083 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3084 	    ill->ill_name_length);
3085 
3086 	ASSERT(ill->ill_phyint != NULL);
3087 	phyi = ill->ill_phyint;
3088 	ill->ill_phyint = NULL;
3089 
3090 	/*
3091 	 * ill_init allocates a phyint always to store the copy
3092 	 * of flags relevant to phyint. At that point in time, we could
3093 	 * not assign the name and hence phyint_illv4/v6 could not be
3094 	 * initialized. Later in ipif_set_values, we assign the name to
3095 	 * the ill, at which point in time we assign phyint_illv4/v6.
3096 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3097 	 */
3098 	if (ill->ill_flags & ILLF_IPV6)
3099 		phyi->phyint_illv6 = NULL;
3100 	else
3101 		phyi->phyint_illv4 = NULL;
3102 
3103 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3104 		rw_exit(&ipst->ips_ill_g_lock);
3105 		return;
3106 	}
3107 
3108 	/*
3109 	 * There are no ills left on this phyint; pull it out of the phyint
3110 	 * avl trees, and free it.
3111 	 */
3112 	if (phyi->phyint_ifindex > 0) {
3113 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3114 		    phyi);
3115 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3116 		    phyi);
3117 	}
3118 	rw_exit(&ipst->ips_ill_g_lock);
3119 
3120 	phyint_free(phyi);
3121 }
3122 
3123 /*
3124  * allocate a ppa, if the number of plumbed interfaces of this type are
3125  * less than ill_no_arena do a linear search to find a unused ppa.
3126  * When the number goes beyond ill_no_arena switch to using an arena.
3127  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3128  * is the return value for an error condition, so allocation starts at one
3129  * and is decremented by one.
3130  */
3131 static int
3132 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3133 {
3134 	ill_t *tmp_ill;
3135 	uint_t start, end;
3136 	int ppa;
3137 
3138 	if (ifp->illif_ppa_arena == NULL &&
3139 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3140 		/*
3141 		 * Create an arena.
3142 		 */
3143 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3144 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3145 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3146 			/* allocate what has already been assigned */
3147 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3148 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3149 		    tmp_ill, AVL_AFTER)) {
3150 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3151 			    1,		/* size */
3152 			    1,		/* align/quantum */
3153 			    0,		/* phase */
3154 			    0,		/* nocross */
3155 			    /* minaddr */
3156 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3157 			    /* maxaddr */
3158 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3159 			    VM_NOSLEEP|VM_FIRSTFIT);
3160 			if (ppa == 0) {
3161 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3162 				    " failed while switching"));
3163 				vmem_destroy(ifp->illif_ppa_arena);
3164 				ifp->illif_ppa_arena = NULL;
3165 				break;
3166 			}
3167 		}
3168 	}
3169 
3170 	if (ifp->illif_ppa_arena != NULL) {
3171 		if (ill->ill_ppa == UINT_MAX) {
3172 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3173 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3174 			if (ppa == 0)
3175 				return (EAGAIN);
3176 			ill->ill_ppa = --ppa;
3177 		} else {
3178 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3179 			    1, 		/* size */
3180 			    1, 		/* align/quantum */
3181 			    0, 		/* phase */
3182 			    0, 		/* nocross */
3183 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3184 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3185 			    VM_NOSLEEP|VM_FIRSTFIT);
3186 			/*
3187 			 * Most likely the allocation failed because
3188 			 * the requested ppa was in use.
3189 			 */
3190 			if (ppa == 0)
3191 				return (EEXIST);
3192 		}
3193 		return (0);
3194 	}
3195 
3196 	/*
3197 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3198 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3199 	 */
3200 	if (ill->ill_ppa == UINT_MAX) {
3201 		end = UINT_MAX - 1;
3202 		start = 0;
3203 	} else {
3204 		end = start = ill->ill_ppa;
3205 	}
3206 
3207 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3208 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3209 		if (start++ >= end) {
3210 			if (ill->ill_ppa == UINT_MAX)
3211 				return (EAGAIN);
3212 			else
3213 				return (EEXIST);
3214 		}
3215 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3216 	}
3217 	ill->ill_ppa = start;
3218 	return (0);
3219 }
3220 
3221 /*
3222  * Insert ill into the list of configured ill's. Once this function completes,
3223  * the ill is globally visible and is available through lookups. More precisely
3224  * this happens after the caller drops the ill_g_lock.
3225  */
3226 static int
3227 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3228 {
3229 	ill_if_t *ill_interface;
3230 	avl_index_t where = 0;
3231 	int error;
3232 	int name_length;
3233 	int index;
3234 	boolean_t check_length = B_FALSE;
3235 	ip_stack_t	*ipst = ill->ill_ipst;
3236 
3237 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3238 
3239 	name_length = mi_strlen(name) + 1;
3240 
3241 	if (isv6)
3242 		index = IP_V6_G_HEAD;
3243 	else
3244 		index = IP_V4_G_HEAD;
3245 
3246 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3247 	/*
3248 	 * Search for interface type based on name
3249 	 */
3250 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3251 		if ((ill_interface->illif_name_len == name_length) &&
3252 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3253 			break;
3254 		}
3255 		ill_interface = ill_interface->illif_next;
3256 	}
3257 
3258 	/*
3259 	 * Interface type not found, create one.
3260 	 */
3261 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3262 		ill_g_head_t ghead;
3263 
3264 		/*
3265 		 * allocate ill_if_t structure
3266 		 */
3267 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3268 		if (ill_interface == NULL) {
3269 			return (ENOMEM);
3270 		}
3271 
3272 		(void) strcpy(ill_interface->illif_name, name);
3273 		ill_interface->illif_name_len = name_length;
3274 
3275 		avl_create(&ill_interface->illif_avl_by_ppa,
3276 		    ill_compare_ppa, sizeof (ill_t),
3277 		    offsetof(struct ill_s, ill_avl_byppa));
3278 
3279 		/*
3280 		 * link the structure in the back to maintain order
3281 		 * of configuration for ifconfig output.
3282 		 */
3283 		ghead = ipst->ips_ill_g_heads[index];
3284 		insque(ill_interface, ghead.ill_g_list_tail);
3285 	}
3286 
3287 	if (ill->ill_ppa == UINT_MAX)
3288 		check_length = B_TRUE;
3289 
3290 	error = ill_alloc_ppa(ill_interface, ill);
3291 	if (error != 0) {
3292 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3293 			ill_delete_interface_type(ill->ill_ifptr);
3294 		return (error);
3295 	}
3296 
3297 	/*
3298 	 * When the ppa is choosen by the system, check that there is
3299 	 * enough space to insert ppa. if a specific ppa was passed in this
3300 	 * check is not required as the interface name passed in will have
3301 	 * the right ppa in it.
3302 	 */
3303 	if (check_length) {
3304 		/*
3305 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3306 		 */
3307 		char buf[sizeof (uint_t) * 3];
3308 
3309 		/*
3310 		 * convert ppa to string to calculate the amount of space
3311 		 * required for it in the name.
3312 		 */
3313 		numtos(ill->ill_ppa, buf);
3314 
3315 		/* Do we have enough space to insert ppa ? */
3316 
3317 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3318 			/* Free ppa and interface type struct */
3319 			if (ill_interface->illif_ppa_arena != NULL) {
3320 				vmem_free(ill_interface->illif_ppa_arena,
3321 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3322 			}
3323 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3324 				ill_delete_interface_type(ill->ill_ifptr);
3325 
3326 			return (EINVAL);
3327 		}
3328 	}
3329 
3330 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3331 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3332 
3333 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3334 	    &where);
3335 	ill->ill_ifptr = ill_interface;
3336 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3337 
3338 	ill_phyint_reinit(ill);
3339 	return (0);
3340 }
3341 
3342 /* Initialize the per phyint ipsq used for serialization */
3343 static boolean_t
3344 ipsq_init(ill_t *ill, boolean_t enter)
3345 {
3346 	ipsq_t  *ipsq;
3347 	ipxop_t	*ipx;
3348 
3349 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3350 		return (B_FALSE);
3351 
3352 	ill->ill_phyint->phyint_ipsq = ipsq;
3353 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3354 	ipx->ipx_ipsq = ipsq;
3355 	ipsq->ipsq_next = ipsq;
3356 	ipsq->ipsq_phyint = ill->ill_phyint;
3357 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3358 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3359 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3360 	if (enter) {
3361 		ipx->ipx_writer = curthread;
3362 		ipx->ipx_forced = B_FALSE;
3363 		ipx->ipx_reentry_cnt = 1;
3364 #ifdef DEBUG
3365 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3366 #endif
3367 	}
3368 	return (B_TRUE);
3369 }
3370 
3371 /*
3372  * ill_init is called by ip_open when a device control stream is opened.
3373  * It does a few initializations, and shoots a DL_INFO_REQ message down
3374  * to the driver.  The response is later picked up in ip_rput_dlpi and
3375  * used to set up default mechanisms for talking to the driver.  (Always
3376  * called as writer.)
3377  *
3378  * If this function returns error, ip_open will call ip_close which in
3379  * turn will call ill_delete to clean up any memory allocated here that
3380  * is not yet freed.
3381  */
3382 int
3383 ill_init(queue_t *q, ill_t *ill)
3384 {
3385 	int	count;
3386 	dl_info_req_t	*dlir;
3387 	mblk_t	*info_mp;
3388 	uchar_t *frag_ptr;
3389 
3390 	/*
3391 	 * The ill is initialized to zero by mi_alloc*(). In addition
3392 	 * some fields already contain valid values, initialized in
3393 	 * ip_open(), before we reach here.
3394 	 */
3395 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3396 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3397 	ill->ill_saved_ire_cnt = 0;
3398 
3399 	ill->ill_rq = q;
3400 	ill->ill_wq = WR(q);
3401 
3402 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3403 	    BPRI_HI);
3404 	if (info_mp == NULL)
3405 		return (ENOMEM);
3406 
3407 	/*
3408 	 * Allocate sufficient space to contain our fragment hash table and
3409 	 * the device name.
3410 	 */
3411 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3412 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3413 	if (frag_ptr == NULL) {
3414 		freemsg(info_mp);
3415 		return (ENOMEM);
3416 	}
3417 	ill->ill_frag_ptr = frag_ptr;
3418 	ill->ill_frag_free_num_pkts = 0;
3419 	ill->ill_last_frag_clean_time = 0;
3420 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3421 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3422 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3423 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3424 		    NULL, MUTEX_DEFAULT, NULL);
3425 	}
3426 
3427 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3428 	if (ill->ill_phyint == NULL) {
3429 		freemsg(info_mp);
3430 		mi_free(frag_ptr);
3431 		return (ENOMEM);
3432 	}
3433 
3434 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3435 	/*
3436 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3437 	 * at this point because of the following reason. If we can't
3438 	 * enter the ipsq at some point and cv_wait, the writer that
3439 	 * wakes us up tries to locate us using the list of all phyints
3440 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3441 	 * If we don't set it now, we risk a missed wakeup.
3442 	 */
3443 	ill->ill_phyint->phyint_illv4 = ill;
3444 	ill->ill_ppa = UINT_MAX;
3445 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3446 
3447 	ill_set_inputfn(ill);
3448 
3449 	if (!ipsq_init(ill, B_TRUE)) {
3450 		freemsg(info_mp);
3451 		mi_free(frag_ptr);
3452 		mi_free(ill->ill_phyint);
3453 		return (ENOMEM);
3454 	}
3455 
3456 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3457 
3458 	/* Frag queue limit stuff */
3459 	ill->ill_frag_count = 0;
3460 	ill->ill_ipf_gen = 0;
3461 
3462 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3463 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3464 	ill->ill_global_timer = INFINITY;
3465 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3466 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3467 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3468 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3469 
3470 	/*
3471 	 * Initialize IPv6 configuration variables.  The IP module is always
3472 	 * opened as an IPv4 module.  Instead tracking down the cases where
3473 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3474 	 * here for convenience, this has no effect until the ill is set to do
3475 	 * IPv6.
3476 	 */
3477 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3478 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3479 	ill->ill_max_buf = ND_MAX_Q;
3480 	ill->ill_refcnt = 0;
3481 
3482 	/* Send down the Info Request to the driver. */
3483 	info_mp->b_datap->db_type = M_PCPROTO;
3484 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3485 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3486 	dlir->dl_primitive = DL_INFO_REQ;
3487 
3488 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3489 
3490 	qprocson(q);
3491 	ill_dlpi_send(ill, info_mp);
3492 
3493 	return (0);
3494 }
3495 
3496 /*
3497  * ill_dls_info
3498  * creates datalink socket info from the device.
3499  */
3500 int
3501 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3502 {
3503 	size_t	len;
3504 
3505 	sdl->sdl_family = AF_LINK;
3506 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3507 	sdl->sdl_type = ill->ill_type;
3508 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3509 	len = strlen(sdl->sdl_data);
3510 	ASSERT(len < 256);
3511 	sdl->sdl_nlen = (uchar_t)len;
3512 	sdl->sdl_alen = ill->ill_phys_addr_length;
3513 	sdl->sdl_slen = 0;
3514 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3515 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3516 
3517 	return (sizeof (struct sockaddr_dl));
3518 }
3519 
3520 /*
3521  * ill_xarp_info
3522  * creates xarp info from the device.
3523  */
3524 static int
3525 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3526 {
3527 	sdl->sdl_family = AF_LINK;
3528 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3529 	sdl->sdl_type = ill->ill_type;
3530 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3531 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3532 	sdl->sdl_alen = ill->ill_phys_addr_length;
3533 	sdl->sdl_slen = 0;
3534 	return (sdl->sdl_nlen);
3535 }
3536 
3537 static int
3538 loopback_kstat_update(kstat_t *ksp, int rw)
3539 {
3540 	kstat_named_t *kn;
3541 	netstackid_t	stackid;
3542 	netstack_t	*ns;
3543 	ip_stack_t	*ipst;
3544 
3545 	if (ksp == NULL || ksp->ks_data == NULL)
3546 		return (EIO);
3547 
3548 	if (rw == KSTAT_WRITE)
3549 		return (EACCES);
3550 
3551 	kn = KSTAT_NAMED_PTR(ksp);
3552 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3553 
3554 	ns = netstack_find_by_stackid(stackid);
3555 	if (ns == NULL)
3556 		return (-1);
3557 
3558 	ipst = ns->netstack_ip;
3559 	if (ipst == NULL) {
3560 		netstack_rele(ns);
3561 		return (-1);
3562 	}
3563 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3564 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3565 	netstack_rele(ns);
3566 	return (0);
3567 }
3568 
3569 /*
3570  * Has ifindex been plumbed already?
3571  */
3572 static boolean_t
3573 phyint_exists(uint_t index, ip_stack_t *ipst)
3574 {
3575 	ASSERT(index != 0);
3576 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3577 
3578 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3579 	    &index, NULL) != NULL);
3580 }
3581 
3582 /* Pick a unique ifindex */
3583 boolean_t
3584 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3585 {
3586 	uint_t starting_index;
3587 
3588 	if (!ipst->ips_ill_index_wrap) {
3589 		*indexp = ipst->ips_ill_index++;
3590 		if (ipst->ips_ill_index == 0) {
3591 			/* Reached the uint_t limit Next time wrap  */
3592 			ipst->ips_ill_index_wrap = B_TRUE;
3593 		}
3594 		return (B_TRUE);
3595 	}
3596 
3597 	/*
3598 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3599 	 * at this point and don't want to call any function that attempts
3600 	 * to get the lock again.
3601 	 */
3602 	starting_index = ipst->ips_ill_index++;
3603 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3604 		if (ipst->ips_ill_index != 0 &&
3605 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3606 			/* found unused index - use it */
3607 			*indexp = ipst->ips_ill_index;
3608 			return (B_TRUE);
3609 		}
3610 	}
3611 
3612 	/*
3613 	 * all interface indicies are inuse.
3614 	 */
3615 	return (B_FALSE);
3616 }
3617 
3618 /*
3619  * Assign a unique interface index for the phyint.
3620  */
3621 static boolean_t
3622 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3623 {
3624 	ASSERT(phyi->phyint_ifindex == 0);
3625 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3626 }
3627 
3628 /*
3629  * Initialize the flags on `phyi' as per the provided mactype.
3630  */
3631 static void
3632 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3633 {
3634 	uint64_t flags = 0;
3635 
3636 	/*
3637 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3638 	 * we always presume the underlying hardware is working and set
3639 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3640 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3641 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3642 	 */
3643 	if (mactype == SUNW_DL_IPMP)
3644 		flags |= PHYI_FAILED;
3645 	else
3646 		flags |= PHYI_RUNNING;
3647 
3648 	switch (mactype) {
3649 	case SUNW_DL_VNI:
3650 		flags |= PHYI_VIRTUAL;
3651 		break;
3652 	case SUNW_DL_IPMP:
3653 		flags |= PHYI_IPMP;
3654 		break;
3655 	case DL_LOOP:
3656 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3657 		break;
3658 	}
3659 
3660 	mutex_enter(&phyi->phyint_lock);
3661 	phyi->phyint_flags |= flags;
3662 	mutex_exit(&phyi->phyint_lock);
3663 }
3664 
3665 /*
3666  * Return a pointer to the ill which matches the supplied name.  Note that
3667  * the ill name length includes the null termination character.  (May be
3668  * called as writer.)
3669  * If do_alloc and the interface is "lo0" it will be automatically created.
3670  * Cannot bump up reference on condemned ills. So dup detect can't be done
3671  * using this func.
3672  */
3673 ill_t *
3674 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3675     boolean_t *did_alloc, ip_stack_t *ipst)
3676 {
3677 	ill_t	*ill;
3678 	ipif_t	*ipif;
3679 	ipsq_t	*ipsq;
3680 	kstat_named_t	*kn;
3681 	boolean_t isloopback;
3682 	in6_addr_t ov6addr;
3683 
3684 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3685 
3686 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3687 	ill = ill_find_by_name(name, isv6, ipst);
3688 	rw_exit(&ipst->ips_ill_g_lock);
3689 	if (ill != NULL)
3690 		return (ill);
3691 
3692 	/*
3693 	 * Couldn't find it.  Does this happen to be a lookup for the
3694 	 * loopback device and are we allowed to allocate it?
3695 	 */
3696 	if (!isloopback || !do_alloc)
3697 		return (NULL);
3698 
3699 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3700 	ill = ill_find_by_name(name, isv6, ipst);
3701 	if (ill != NULL) {
3702 		rw_exit(&ipst->ips_ill_g_lock);
3703 		return (ill);
3704 	}
3705 
3706 	/* Create the loopback device on demand */
3707 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3708 	    sizeof (ipif_loopback_name), BPRI_MED));
3709 	if (ill == NULL)
3710 		goto done;
3711 
3712 	*ill = ill_null;
3713 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3714 	ill->ill_ipst = ipst;
3715 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3716 	netstack_hold(ipst->ips_netstack);
3717 	/*
3718 	 * For exclusive stacks we set the zoneid to zero
3719 	 * to make IP operate as if in the global zone.
3720 	 */
3721 	ill->ill_zoneid = GLOBAL_ZONEID;
3722 
3723 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3724 	if (ill->ill_phyint == NULL)
3725 		goto done;
3726 
3727 	if (isv6)
3728 		ill->ill_phyint->phyint_illv6 = ill;
3729 	else
3730 		ill->ill_phyint->phyint_illv4 = ill;
3731 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3732 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3733 
3734 	if (isv6) {
3735 		ill->ill_isv6 = B_TRUE;
3736 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3737 	} else {
3738 		ill->ill_max_frag = ip_loopback_mtuplus;
3739 	}
3740 	if (!ill_allocate_mibs(ill))
3741 		goto done;
3742 	ill->ill_current_frag = ill->ill_max_frag;
3743 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3744 	/*
3745 	 * ipif_loopback_name can't be pointed at directly because its used
3746 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3747 	 * from the glist, ill_glist_delete() sets the first character of
3748 	 * ill_name to '\0'.
3749 	 */
3750 	ill->ill_name = (char *)ill + sizeof (*ill);
3751 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3752 	ill->ill_name_length = sizeof (ipif_loopback_name);
3753 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3754 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3755 
3756 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3757 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3758 	ill->ill_global_timer = INFINITY;
3759 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3760 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3761 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3762 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3763 
3764 	/* No resolver here. */
3765 	ill->ill_net_type = IRE_LOOPBACK;
3766 
3767 	/* Initialize the ipsq */
3768 	if (!ipsq_init(ill, B_FALSE))
3769 		goto done;
3770 
3771 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3772 	if (ipif == NULL)
3773 		goto done;
3774 
3775 	ill->ill_flags = ILLF_MULTICAST;
3776 
3777 	ov6addr = ipif->ipif_v6lcl_addr;
3778 	/* Set up default loopback address and mask. */
3779 	if (!isv6) {
3780 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3781 
3782 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3783 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3784 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3785 		    ipif->ipif_v6subnet);
3786 		ill->ill_flags |= ILLF_IPV4;
3787 	} else {
3788 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3789 		ipif->ipif_v6net_mask = ipv6_all_ones;
3790 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3791 		    ipif->ipif_v6subnet);
3792 		ill->ill_flags |= ILLF_IPV6;
3793 	}
3794 
3795 	/*
3796 	 * Chain us in at the end of the ill list. hold the ill
3797 	 * before we make it globally visible. 1 for the lookup.
3798 	 */
3799 	ill->ill_refcnt = 0;
3800 	ill_refhold(ill);
3801 
3802 	ill->ill_frag_count = 0;
3803 	ill->ill_frag_free_num_pkts = 0;
3804 	ill->ill_last_frag_clean_time = 0;
3805 
3806 	ipsq = ill->ill_phyint->phyint_ipsq;
3807 
3808 	ill_set_inputfn(ill);
3809 
3810 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3811 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3812 
3813 	/* Let SCTP know so that it can add this to its list */
3814 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3815 
3816 	/*
3817 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3818 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3819 	 * requires to be after ill_glist_insert() since we need the
3820 	 * ill_index set. Pass on ipv6_loopback as the old address.
3821 	 */
3822 	sctp_update_ipif_addr(ipif, ov6addr);
3823 
3824 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3825 
3826 	/*
3827 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3828 	 * If so, free our original one.
3829 	 */
3830 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3831 		ipsq_delete(ipsq);
3832 
3833 	if (ipst->ips_loopback_ksp == NULL) {
3834 		/* Export loopback interface statistics */
3835 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3836 		    ipif_loopback_name, "net",
3837 		    KSTAT_TYPE_NAMED, 2, 0,
3838 		    ipst->ips_netstack->netstack_stackid);
3839 		if (ipst->ips_loopback_ksp != NULL) {
3840 			ipst->ips_loopback_ksp->ks_update =
3841 			    loopback_kstat_update;
3842 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3843 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3844 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3845 			ipst->ips_loopback_ksp->ks_private =
3846 			    (void *)(uintptr_t)ipst->ips_netstack->
3847 			    netstack_stackid;
3848 			kstat_install(ipst->ips_loopback_ksp);
3849 		}
3850 	}
3851 
3852 	*did_alloc = B_TRUE;
3853 	rw_exit(&ipst->ips_ill_g_lock);
3854 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3855 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3856 	return (ill);
3857 done:
3858 	if (ill != NULL) {
3859 		if (ill->ill_phyint != NULL) {
3860 			ipsq = ill->ill_phyint->phyint_ipsq;
3861 			if (ipsq != NULL) {
3862 				ipsq->ipsq_phyint = NULL;
3863 				ipsq_delete(ipsq);
3864 			}
3865 			mi_free(ill->ill_phyint);
3866 		}
3867 		ill_free_mib(ill);
3868 		if (ill->ill_ipst != NULL)
3869 			netstack_rele(ill->ill_ipst->ips_netstack);
3870 		mi_free(ill);
3871 	}
3872 	rw_exit(&ipst->ips_ill_g_lock);
3873 	return (NULL);
3874 }
3875 
3876 /*
3877  * For IPP calls - use the ip_stack_t for global stack.
3878  */
3879 ill_t *
3880 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3881 {
3882 	ip_stack_t	*ipst;
3883 	ill_t		*ill;
3884 
3885 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3886 	if (ipst == NULL) {
3887 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3888 		return (NULL);
3889 	}
3890 
3891 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3892 	netstack_rele(ipst->ips_netstack);
3893 	return (ill);
3894 }
3895 
3896 /*
3897  * Return a pointer to the ill which matches the index and IP version type.
3898  */
3899 ill_t *
3900 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3901 {
3902 	ill_t	*ill;
3903 	phyint_t *phyi;
3904 
3905 	/*
3906 	 * Indexes are stored in the phyint - a common structure
3907 	 * to both IPv4 and IPv6.
3908 	 */
3909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3910 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3911 	    (void *) &index, NULL);
3912 	if (phyi != NULL) {
3913 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3914 		if (ill != NULL) {
3915 			mutex_enter(&ill->ill_lock);
3916 			if (!ILL_IS_CONDEMNED(ill)) {
3917 				ill_refhold_locked(ill);
3918 				mutex_exit(&ill->ill_lock);
3919 				rw_exit(&ipst->ips_ill_g_lock);
3920 				return (ill);
3921 			}
3922 			mutex_exit(&ill->ill_lock);
3923 		}
3924 	}
3925 	rw_exit(&ipst->ips_ill_g_lock);
3926 	return (NULL);
3927 }
3928 
3929 /*
3930  * Verify whether or not an interface index is valid for the specified zoneid
3931  * to transmit packets.
3932  * It can be zero (meaning "reset") or an interface index assigned
3933  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3934  */
3935 boolean_t
3936 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3937     ip_stack_t *ipst)
3938 {
3939 	ill_t		*ill;
3940 
3941 	if (ifindex == 0)
3942 		return (B_TRUE);
3943 
3944 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3945 	if (ill == NULL)
3946 		return (B_FALSE);
3947 	if (IS_VNI(ill)) {
3948 		ill_refrele(ill);
3949 		return (B_FALSE);
3950 	}
3951 	ill_refrele(ill);
3952 	return (B_TRUE);
3953 }
3954 
3955 /*
3956  * Return the ifindex next in sequence after the passed in ifindex.
3957  * If there is no next ifindex for the given protocol, return 0.
3958  */
3959 uint_t
3960 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3961 {
3962 	phyint_t *phyi;
3963 	phyint_t *phyi_initial;
3964 	uint_t   ifindex;
3965 
3966 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3967 
3968 	if (index == 0) {
3969 		phyi = avl_first(
3970 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3971 	} else {
3972 		phyi = phyi_initial = avl_find(
3973 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3974 		    (void *) &index, NULL);
3975 	}
3976 
3977 	for (; phyi != NULL;
3978 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3979 	    phyi, AVL_AFTER)) {
3980 		/*
3981 		 * If we're not returning the first interface in the tree
3982 		 * and we still haven't moved past the phyint_t that
3983 		 * corresponds to index, avl_walk needs to be called again
3984 		 */
3985 		if (!((index != 0) && (phyi == phyi_initial))) {
3986 			if (isv6) {
3987 				if ((phyi->phyint_illv6) &&
3988 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3989 				    (phyi->phyint_illv6->ill_isv6 == 1))
3990 					break;
3991 			} else {
3992 				if ((phyi->phyint_illv4) &&
3993 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3994 				    (phyi->phyint_illv4->ill_isv6 == 0))
3995 					break;
3996 			}
3997 		}
3998 	}
3999 
4000 	rw_exit(&ipst->ips_ill_g_lock);
4001 
4002 	if (phyi != NULL)
4003 		ifindex = phyi->phyint_ifindex;
4004 	else
4005 		ifindex = 0;
4006 
4007 	return (ifindex);
4008 }
4009 
4010 /*
4011  * Return the ifindex for the named interface.
4012  * If there is no next ifindex for the interface, return 0.
4013  */
4014 uint_t
4015 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4016 {
4017 	phyint_t	*phyi;
4018 	avl_index_t	where = 0;
4019 	uint_t		ifindex;
4020 
4021 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4022 
4023 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4024 	    name, &where)) == NULL) {
4025 		rw_exit(&ipst->ips_ill_g_lock);
4026 		return (0);
4027 	}
4028 
4029 	ifindex = phyi->phyint_ifindex;
4030 
4031 	rw_exit(&ipst->ips_ill_g_lock);
4032 
4033 	return (ifindex);
4034 }
4035 
4036 /*
4037  * Return the ifindex to be used by upper layer protocols for instance
4038  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4039  */
4040 uint_t
4041 ill_get_upper_ifindex(const ill_t *ill)
4042 {
4043 	if (IS_UNDER_IPMP(ill))
4044 		return (ipmp_ill_get_ipmp_ifindex(ill));
4045 	else
4046 		return (ill->ill_phyint->phyint_ifindex);
4047 }
4048 
4049 
4050 /*
4051  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4052  * that gives a running thread a reference to the ill. This reference must be
4053  * released by the thread when it is done accessing the ill and related
4054  * objects. ill_refcnt can not be used to account for static references
4055  * such as other structures pointing to an ill. Callers must generally
4056  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4057  * or be sure that the ill is not being deleted or changing state before
4058  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4059  * ill won't change any of its critical state such as address, netmask etc.
4060  */
4061 void
4062 ill_refhold(ill_t *ill)
4063 {
4064 	mutex_enter(&ill->ill_lock);
4065 	ill->ill_refcnt++;
4066 	ILL_TRACE_REF(ill);
4067 	mutex_exit(&ill->ill_lock);
4068 }
4069 
4070 void
4071 ill_refhold_locked(ill_t *ill)
4072 {
4073 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4074 	ill->ill_refcnt++;
4075 	ILL_TRACE_REF(ill);
4076 }
4077 
4078 /* Returns true if we managed to get a refhold */
4079 boolean_t
4080 ill_check_and_refhold(ill_t *ill)
4081 {
4082 	mutex_enter(&ill->ill_lock);
4083 	if (!ILL_IS_CONDEMNED(ill)) {
4084 		ill_refhold_locked(ill);
4085 		mutex_exit(&ill->ill_lock);
4086 		return (B_TRUE);
4087 	}
4088 	mutex_exit(&ill->ill_lock);
4089 	return (B_FALSE);
4090 }
4091 
4092 /*
4093  * Must not be called while holding any locks. Otherwise if this is
4094  * the last reference to be released, there is a chance of recursive mutex
4095  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4096  * to restart an ioctl.
4097  */
4098 void
4099 ill_refrele(ill_t *ill)
4100 {
4101 	mutex_enter(&ill->ill_lock);
4102 	ASSERT(ill->ill_refcnt != 0);
4103 	ill->ill_refcnt--;
4104 	ILL_UNTRACE_REF(ill);
4105 	if (ill->ill_refcnt != 0) {
4106 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4107 		mutex_exit(&ill->ill_lock);
4108 		return;
4109 	}
4110 
4111 	/* Drops the ill_lock */
4112 	ipif_ill_refrele_tail(ill);
4113 }
4114 
4115 /*
4116  * Obtain a weak reference count on the ill. This reference ensures the
4117  * ill won't be freed, but the ill may change any of its critical state
4118  * such as netmask, address etc. Returns an error if the ill has started
4119  * closing.
4120  */
4121 boolean_t
4122 ill_waiter_inc(ill_t *ill)
4123 {
4124 	mutex_enter(&ill->ill_lock);
4125 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4126 		mutex_exit(&ill->ill_lock);
4127 		return (B_FALSE);
4128 	}
4129 	ill->ill_waiters++;
4130 	mutex_exit(&ill->ill_lock);
4131 	return (B_TRUE);
4132 }
4133 
4134 void
4135 ill_waiter_dcr(ill_t *ill)
4136 {
4137 	mutex_enter(&ill->ill_lock);
4138 	ill->ill_waiters--;
4139 	if (ill->ill_waiters == 0)
4140 		cv_broadcast(&ill->ill_cv);
4141 	mutex_exit(&ill->ill_lock);
4142 }
4143 
4144 /*
4145  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4146  * driver.  We construct best guess defaults for lower level information that
4147  * we need.  If an interface is brought up without injection of any overriding
4148  * information from outside, we have to be ready to go with these defaults.
4149  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4150  * we primarely want the dl_provider_style.
4151  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4152  * at which point we assume the other part of the information is valid.
4153  */
4154 void
4155 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4156 {
4157 	uchar_t		*brdcst_addr;
4158 	uint_t		brdcst_addr_length, phys_addr_length;
4159 	t_scalar_t	sap_length;
4160 	dl_info_ack_t	*dlia;
4161 	ip_m_t		*ipm;
4162 	dl_qos_cl_sel1_t *sel1;
4163 	int		min_mtu;
4164 
4165 	ASSERT(IAM_WRITER_ILL(ill));
4166 
4167 	/*
4168 	 * Till the ill is fully up  the ill is not globally visible.
4169 	 * So no need for a lock.
4170 	 */
4171 	dlia = (dl_info_ack_t *)mp->b_rptr;
4172 	ill->ill_mactype = dlia->dl_mac_type;
4173 
4174 	ipm = ip_m_lookup(dlia->dl_mac_type);
4175 	if (ipm == NULL) {
4176 		ipm = ip_m_lookup(DL_OTHER);
4177 		ASSERT(ipm != NULL);
4178 	}
4179 	ill->ill_media = ipm;
4180 
4181 	/*
4182 	 * When the new DLPI stuff is ready we'll pull lengths
4183 	 * from dlia.
4184 	 */
4185 	if (dlia->dl_version == DL_VERSION_2) {
4186 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4187 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4188 		    brdcst_addr_length);
4189 		if (brdcst_addr == NULL) {
4190 			brdcst_addr_length = 0;
4191 		}
4192 		sap_length = dlia->dl_sap_length;
4193 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4194 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4195 		    brdcst_addr_length, sap_length, phys_addr_length));
4196 	} else {
4197 		brdcst_addr_length = 6;
4198 		brdcst_addr = ip_six_byte_all_ones;
4199 		sap_length = -2;
4200 		phys_addr_length = brdcst_addr_length;
4201 	}
4202 
4203 	ill->ill_bcast_addr_length = brdcst_addr_length;
4204 	ill->ill_phys_addr_length = phys_addr_length;
4205 	ill->ill_sap_length = sap_length;
4206 
4207 	/*
4208 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4209 	 * but we must ensure a minimum IP MTU is used since other bits of
4210 	 * IP will fly apart otherwise.
4211 	 */
4212 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4213 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4214 	ill->ill_current_frag = ill->ill_max_frag;
4215 	ill->ill_mtu = ill->ill_max_frag;
4216 
4217 	ill->ill_type = ipm->ip_m_type;
4218 
4219 	if (!ill->ill_dlpi_style_set) {
4220 		if (dlia->dl_provider_style == DL_STYLE2)
4221 			ill->ill_needs_attach = 1;
4222 
4223 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4224 
4225 		/*
4226 		 * Allocate the first ipif on this ill.  We don't delay it
4227 		 * further as ioctl handling assumes at least one ipif exists.
4228 		 *
4229 		 * At this point we don't know whether the ill is v4 or v6.
4230 		 * We will know this whan the SIOCSLIFNAME happens and
4231 		 * the correct value for ill_isv6 will be assigned in
4232 		 * ipif_set_values(). We need to hold the ill lock and
4233 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4234 		 * the wakeup.
4235 		 */
4236 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4237 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4238 		mutex_enter(&ill->ill_lock);
4239 		ASSERT(ill->ill_dlpi_style_set == 0);
4240 		ill->ill_dlpi_style_set = 1;
4241 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4242 		cv_broadcast(&ill->ill_cv);
4243 		mutex_exit(&ill->ill_lock);
4244 		freemsg(mp);
4245 		return;
4246 	}
4247 	ASSERT(ill->ill_ipif != NULL);
4248 	/*
4249 	 * We know whether it is IPv4 or IPv6 now, as this is the
4250 	 * second DL_INFO_ACK we are recieving in response to the
4251 	 * DL_INFO_REQ sent in ipif_set_values.
4252 	 */
4253 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4254 	/*
4255 	 * Clear all the flags that were set based on ill_bcast_addr_length
4256 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4257 	 * changed now and we need to re-evaluate.
4258 	 */
4259 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4260 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4261 
4262 	/*
4263 	 * Free ill_bcast_mp as things could have changed now.
4264 	 *
4265 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4266 	 * with no underlying interfaces (and thus an unknown broadcast
4267 	 * address length), but we enforce that an interface is broadcast-
4268 	 * capable as part of allowing it to join a group.
4269 	 */
4270 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4271 		if (ill->ill_bcast_mp != NULL)
4272 			freemsg(ill->ill_bcast_mp);
4273 		ill->ill_net_type = IRE_IF_NORESOLVER;
4274 
4275 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4276 		    ill->ill_phys_addr_length,
4277 		    ill->ill_sap,
4278 		    ill->ill_sap_length);
4279 
4280 		if (ill->ill_isv6)
4281 			/*
4282 			 * Note: xresolv interfaces will eventually need NOARP
4283 			 * set here as well, but that will require those
4284 			 * external resolvers to have some knowledge of
4285 			 * that flag and act appropriately. Not to be changed
4286 			 * at present.
4287 			 */
4288 			ill->ill_flags |= ILLF_NONUD;
4289 		else
4290 			ill->ill_flags |= ILLF_NOARP;
4291 
4292 		if (ill->ill_mactype == SUNW_DL_VNI) {
4293 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4294 		} else if (ill->ill_phys_addr_length == 0 ||
4295 		    ill->ill_mactype == DL_IPV4 ||
4296 		    ill->ill_mactype == DL_IPV6) {
4297 			/*
4298 			 * The underying link is point-to-point, so mark the
4299 			 * interface as such.  We can do IP multicast over
4300 			 * such a link since it transmits all network-layer
4301 			 * packets to the remote side the same way.
4302 			 */
4303 			ill->ill_flags |= ILLF_MULTICAST;
4304 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4305 		}
4306 	} else {
4307 		ill->ill_net_type = IRE_IF_RESOLVER;
4308 		if (ill->ill_bcast_mp != NULL)
4309 			freemsg(ill->ill_bcast_mp);
4310 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4311 		    ill->ill_bcast_addr_length, ill->ill_sap,
4312 		    ill->ill_sap_length);
4313 		/*
4314 		 * Later detect lack of DLPI driver multicast
4315 		 * capability by catching DL_ENABMULTI errors in
4316 		 * ip_rput_dlpi.
4317 		 */
4318 		ill->ill_flags |= ILLF_MULTICAST;
4319 		if (!ill->ill_isv6)
4320 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4321 	}
4322 
4323 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4324 	if (ill->ill_mactype == SUNW_DL_IPMP)
4325 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4326 
4327 	/* By default an interface does not support any CoS marking */
4328 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4329 
4330 	/*
4331 	 * If we get QoS information in DL_INFO_ACK, the device supports
4332 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4333 	 */
4334 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4335 	    dlia->dl_qos_length);
4336 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4337 		ill->ill_flags |= ILLF_COS_ENABLED;
4338 	}
4339 
4340 	/* Clear any previous error indication. */
4341 	ill->ill_error = 0;
4342 	freemsg(mp);
4343 }
4344 
4345 /*
4346  * Perform various checks to verify that an address would make sense as a
4347  * local, remote, or subnet interface address.
4348  */
4349 static boolean_t
4350 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4351 {
4352 	ipaddr_t	net_mask;
4353 
4354 	/*
4355 	 * Don't allow all zeroes, or all ones, but allow
4356 	 * all ones netmask.
4357 	 */
4358 	if ((net_mask = ip_net_mask(addr)) == 0)
4359 		return (B_FALSE);
4360 	/* A given netmask overrides the "guess" netmask */
4361 	if (subnet_mask != 0)
4362 		net_mask = subnet_mask;
4363 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4364 	    (addr == (addr | ~net_mask)))) {
4365 		return (B_FALSE);
4366 	}
4367 
4368 	/*
4369 	 * Even if the netmask is all ones, we do not allow address to be
4370 	 * 255.255.255.255
4371 	 */
4372 	if (addr == INADDR_BROADCAST)
4373 		return (B_FALSE);
4374 
4375 	if (CLASSD(addr))
4376 		return (B_FALSE);
4377 
4378 	return (B_TRUE);
4379 }
4380 
4381 #define	V6_IPIF_LINKLOCAL(p)	\
4382 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4383 
4384 /*
4385  * Compare two given ipifs and check if the second one is better than
4386  * the first one using the order of preference (not taking deprecated
4387  * into acount) specified in ipif_lookup_multicast().
4388  */
4389 static boolean_t
4390 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4391 {
4392 	/* Check the least preferred first. */
4393 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4394 		/* If both ipifs are the same, use the first one. */
4395 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4396 			return (B_FALSE);
4397 		else
4398 			return (B_TRUE);
4399 	}
4400 
4401 	/* For IPv6, check for link local address. */
4402 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4403 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4404 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4405 			/* The second one is equal or less preferred. */
4406 			return (B_FALSE);
4407 		} else {
4408 			return (B_TRUE);
4409 		}
4410 	}
4411 
4412 	/* Then check for point to point interface. */
4413 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4414 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4415 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4416 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4417 			return (B_FALSE);
4418 		} else {
4419 			return (B_TRUE);
4420 		}
4421 	}
4422 
4423 	/* old_ipif is a normal interface, so no need to use the new one. */
4424 	return (B_FALSE);
4425 }
4426 
4427 /*
4428  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4429  * The ipif must be up, and its ill must multicast-capable, not
4430  * condemned, not an underlying interface in an IPMP group, and
4431  * not a VNI interface.  Order of preference:
4432  *
4433  * 	1a. normal
4434  * 	1b. normal, but deprecated
4435  * 	2a. point to point
4436  * 	2b. point to point, but deprecated
4437  * 	3a. link local
4438  * 	3b. link local, but deprecated
4439  * 	4. loopback.
4440  */
4441 static ipif_t *
4442 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4443 {
4444 	ill_t			*ill;
4445 	ill_walk_context_t	ctx;
4446 	ipif_t			*ipif;
4447 	ipif_t			*saved_ipif = NULL;
4448 	ipif_t			*dep_ipif = NULL;
4449 
4450 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4451 	if (isv6)
4452 		ill = ILL_START_WALK_V6(&ctx, ipst);
4453 	else
4454 		ill = ILL_START_WALK_V4(&ctx, ipst);
4455 
4456 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4457 		mutex_enter(&ill->ill_lock);
4458 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4459 		    ILL_IS_CONDEMNED(ill) ||
4460 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4461 			mutex_exit(&ill->ill_lock);
4462 			continue;
4463 		}
4464 		for (ipif = ill->ill_ipif; ipif != NULL;
4465 		    ipif = ipif->ipif_next) {
4466 			if (zoneid != ipif->ipif_zoneid &&
4467 			    zoneid != ALL_ZONES &&
4468 			    ipif->ipif_zoneid != ALL_ZONES) {
4469 				continue;
4470 			}
4471 			if (!(ipif->ipif_flags & IPIF_UP) ||
4472 			    IPIF_IS_CONDEMNED(ipif)) {
4473 				continue;
4474 			}
4475 
4476 			/*
4477 			 * Found one candidate.  If it is deprecated,
4478 			 * remember it in dep_ipif.  If it is not deprecated,
4479 			 * remember it in saved_ipif.
4480 			 */
4481 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4482 				if (dep_ipif == NULL) {
4483 					dep_ipif = ipif;
4484 				} else if (ipif_comp_multi(dep_ipif, ipif,
4485 				    isv6)) {
4486 					/*
4487 					 * If the previous dep_ipif does not
4488 					 * belong to the same ill, we've done
4489 					 * a ipif_refhold() on it.  So we need
4490 					 * to release it.
4491 					 */
4492 					if (dep_ipif->ipif_ill != ill)
4493 						ipif_refrele(dep_ipif);
4494 					dep_ipif = ipif;
4495 				}
4496 				continue;
4497 			}
4498 			if (saved_ipif == NULL) {
4499 				saved_ipif = ipif;
4500 			} else {
4501 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4502 					if (saved_ipif->ipif_ill != ill)
4503 						ipif_refrele(saved_ipif);
4504 					saved_ipif = ipif;
4505 				}
4506 			}
4507 		}
4508 		/*
4509 		 * Before going to the next ill, do a ipif_refhold() on the
4510 		 * saved ones.
4511 		 */
4512 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4513 			ipif_refhold_locked(saved_ipif);
4514 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4515 			ipif_refhold_locked(dep_ipif);
4516 		mutex_exit(&ill->ill_lock);
4517 	}
4518 	rw_exit(&ipst->ips_ill_g_lock);
4519 
4520 	/*
4521 	 * If we have only the saved_ipif, return it.  But if we have both
4522 	 * saved_ipif and dep_ipif, check to see which one is better.
4523 	 */
4524 	if (saved_ipif != NULL) {
4525 		if (dep_ipif != NULL) {
4526 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4527 				ipif_refrele(saved_ipif);
4528 				return (dep_ipif);
4529 			} else {
4530 				ipif_refrele(dep_ipif);
4531 				return (saved_ipif);
4532 			}
4533 		}
4534 		return (saved_ipif);
4535 	} else {
4536 		return (dep_ipif);
4537 	}
4538 }
4539 
4540 ill_t *
4541 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4542 {
4543 	ipif_t *ipif;
4544 	ill_t *ill;
4545 
4546 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4547 	if (ipif == NULL)
4548 		return (NULL);
4549 
4550 	ill = ipif->ipif_ill;
4551 	ill_refhold(ill);
4552 	ipif_refrele(ipif);
4553 	return (ill);
4554 }
4555 
4556 /*
4557  * This function is called when an application does not specify an interface
4558  * to be used for multicast traffic (joining a group/sending data).  It
4559  * calls ire_lookup_multi() to look for an interface route for the
4560  * specified multicast group.  Doing this allows the administrator to add
4561  * prefix routes for multicast to indicate which interface to be used for
4562  * multicast traffic in the above scenario.  The route could be for all
4563  * multicast (224.0/4), for a single multicast group (a /32 route) or
4564  * anything in between.  If there is no such multicast route, we just find
4565  * any multicast capable interface and return it.  The returned ipif
4566  * is refhold'ed.
4567  *
4568  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4569  * unicast table. This is used by CGTP.
4570  */
4571 ill_t *
4572 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4573     boolean_t *multirtp, ipaddr_t *setsrcp)
4574 {
4575 	ill_t			*ill;
4576 
4577 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4578 	if (ill != NULL)
4579 		return (ill);
4580 
4581 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4582 }
4583 
4584 /*
4585  * Look for an ipif with the specified interface address and destination.
4586  * The destination address is used only for matching point-to-point interfaces.
4587  */
4588 ipif_t *
4589 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4590 {
4591 	ipif_t	*ipif;
4592 	ill_t	*ill;
4593 	ill_walk_context_t ctx;
4594 
4595 	/*
4596 	 * First match all the point-to-point interfaces
4597 	 * before looking at non-point-to-point interfaces.
4598 	 * This is done to avoid returning non-point-to-point
4599 	 * ipif instead of unnumbered point-to-point ipif.
4600 	 */
4601 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4602 	ill = ILL_START_WALK_V4(&ctx, ipst);
4603 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4604 		mutex_enter(&ill->ill_lock);
4605 		for (ipif = ill->ill_ipif; ipif != NULL;
4606 		    ipif = ipif->ipif_next) {
4607 			/* Allow the ipif to be down */
4608 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4609 			    (ipif->ipif_lcl_addr == if_addr) &&
4610 			    (ipif->ipif_pp_dst_addr == dst)) {
4611 				if (!IPIF_IS_CONDEMNED(ipif)) {
4612 					ipif_refhold_locked(ipif);
4613 					mutex_exit(&ill->ill_lock);
4614 					rw_exit(&ipst->ips_ill_g_lock);
4615 					return (ipif);
4616 				}
4617 			}
4618 		}
4619 		mutex_exit(&ill->ill_lock);
4620 	}
4621 	rw_exit(&ipst->ips_ill_g_lock);
4622 
4623 	/* lookup the ipif based on interface address */
4624 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4625 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4626 	return (ipif);
4627 }
4628 
4629 /*
4630  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4631  */
4632 static ipif_t *
4633 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4634     zoneid_t zoneid, ip_stack_t *ipst)
4635 {
4636 	ipif_t  *ipif;
4637 	ill_t   *ill;
4638 	boolean_t ptp = B_FALSE;
4639 	ill_walk_context_t	ctx;
4640 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4641 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4642 
4643 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4644 	/*
4645 	 * Repeat twice, first based on local addresses and
4646 	 * next time for pointopoint.
4647 	 */
4648 repeat:
4649 	ill = ILL_START_WALK_V4(&ctx, ipst);
4650 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4651 		if (match_ill != NULL && ill != match_ill &&
4652 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4653 			continue;
4654 		}
4655 		mutex_enter(&ill->ill_lock);
4656 		for (ipif = ill->ill_ipif; ipif != NULL;
4657 		    ipif = ipif->ipif_next) {
4658 			if (zoneid != ALL_ZONES &&
4659 			    zoneid != ipif->ipif_zoneid &&
4660 			    ipif->ipif_zoneid != ALL_ZONES)
4661 				continue;
4662 
4663 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4664 				continue;
4665 
4666 			/* Allow the ipif to be down */
4667 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4668 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4669 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4670 			    (ipif->ipif_pp_dst_addr == addr))) {
4671 				if (!IPIF_IS_CONDEMNED(ipif)) {
4672 					ipif_refhold_locked(ipif);
4673 					mutex_exit(&ill->ill_lock);
4674 					rw_exit(&ipst->ips_ill_g_lock);
4675 					return (ipif);
4676 				}
4677 			}
4678 		}
4679 		mutex_exit(&ill->ill_lock);
4680 	}
4681 
4682 	/* If we already did the ptp case, then we are done */
4683 	if (ptp) {
4684 		rw_exit(&ipst->ips_ill_g_lock);
4685 		return (NULL);
4686 	}
4687 	ptp = B_TRUE;
4688 	goto repeat;
4689 }
4690 
4691 /*
4692  * Lookup an ipif with the specified address.  For point-to-point links we
4693  * look for matches on either the destination address or the local address,
4694  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4695  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4696  * (or illgrp if `match_ill' is in an IPMP group).
4697  */
4698 ipif_t *
4699 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4700     ip_stack_t *ipst)
4701 {
4702 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4703 	    zoneid, ipst));
4704 }
4705 
4706 /*
4707  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4708  * except that we will only return an address if it is not marked as
4709  * IPIF_DUPLICATE
4710  */
4711 ipif_t *
4712 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4713     ip_stack_t *ipst)
4714 {
4715 	return (ipif_lookup_addr_common(addr, match_ill,
4716 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4717 	    zoneid, ipst));
4718 }
4719 
4720 /*
4721  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4722  * `match_ill' across the IPMP group.  This function is only needed in some
4723  * corner-cases; almost everything should use ipif_lookup_addr().
4724  */
4725 ipif_t *
4726 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4727 {
4728 	ASSERT(match_ill != NULL);
4729 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4730 	    ipst));
4731 }
4732 
4733 /*
4734  * Look for an ipif with the specified address. For point-point links
4735  * we look for matches on either the destination address and the local
4736  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4737  * is set.
4738  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4739  * ill (or illgrp if `match_ill' is in an IPMP group).
4740  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4741  */
4742 zoneid_t
4743 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4744 {
4745 	zoneid_t zoneid;
4746 	ipif_t  *ipif;
4747 	ill_t   *ill;
4748 	boolean_t ptp = B_FALSE;
4749 	ill_walk_context_t	ctx;
4750 
4751 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4752 	/*
4753 	 * Repeat twice, first based on local addresses and
4754 	 * next time for pointopoint.
4755 	 */
4756 repeat:
4757 	ill = ILL_START_WALK_V4(&ctx, ipst);
4758 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4759 		if (match_ill != NULL && ill != match_ill &&
4760 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4761 			continue;
4762 		}
4763 		mutex_enter(&ill->ill_lock);
4764 		for (ipif = ill->ill_ipif; ipif != NULL;
4765 		    ipif = ipif->ipif_next) {
4766 			/* Allow the ipif to be down */
4767 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4768 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4769 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4770 			    (ipif->ipif_pp_dst_addr == addr)) &&
4771 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4772 				zoneid = ipif->ipif_zoneid;
4773 				mutex_exit(&ill->ill_lock);
4774 				rw_exit(&ipst->ips_ill_g_lock);
4775 				/*
4776 				 * If ipif_zoneid was ALL_ZONES then we have
4777 				 * a trusted extensions shared IP address.
4778 				 * In that case GLOBAL_ZONEID works to send.
4779 				 */
4780 				if (zoneid == ALL_ZONES)
4781 					zoneid = GLOBAL_ZONEID;
4782 				return (zoneid);
4783 			}
4784 		}
4785 		mutex_exit(&ill->ill_lock);
4786 	}
4787 
4788 	/* If we already did the ptp case, then we are done */
4789 	if (ptp) {
4790 		rw_exit(&ipst->ips_ill_g_lock);
4791 		return (ALL_ZONES);
4792 	}
4793 	ptp = B_TRUE;
4794 	goto repeat;
4795 }
4796 
4797 /*
4798  * Look for an ipif that matches the specified remote address i.e. the
4799  * ipif that would receive the specified packet.
4800  * First look for directly connected interfaces and then do a recursive
4801  * IRE lookup and pick the first ipif corresponding to the source address in the
4802  * ire.
4803  * Returns: held ipif
4804  *
4805  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4806  */
4807 ipif_t *
4808 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4809 {
4810 	ipif_t	*ipif;
4811 
4812 	ASSERT(!ill->ill_isv6);
4813 
4814 	/*
4815 	 * Someone could be changing this ipif currently or change it
4816 	 * after we return this. Thus  a few packets could use the old
4817 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4818 	 * will atomically be updated or cleaned up with the new value
4819 	 * Thus we don't need a lock to check the flags or other attrs below.
4820 	 */
4821 	mutex_enter(&ill->ill_lock);
4822 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4823 		if (IPIF_IS_CONDEMNED(ipif))
4824 			continue;
4825 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4826 		    ipif->ipif_zoneid != ALL_ZONES)
4827 			continue;
4828 		/* Allow the ipif to be down */
4829 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4830 			if ((ipif->ipif_pp_dst_addr == addr) ||
4831 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4832 			    ipif->ipif_lcl_addr == addr)) {
4833 				ipif_refhold_locked(ipif);
4834 				mutex_exit(&ill->ill_lock);
4835 				return (ipif);
4836 			}
4837 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4838 			ipif_refhold_locked(ipif);
4839 			mutex_exit(&ill->ill_lock);
4840 			return (ipif);
4841 		}
4842 	}
4843 	mutex_exit(&ill->ill_lock);
4844 	/*
4845 	 * For a remote destination it isn't possible to nail down a particular
4846 	 * ipif.
4847 	 */
4848 
4849 	/* Pick the first interface */
4850 	ipif = ipif_get_next_ipif(NULL, ill);
4851 	return (ipif);
4852 }
4853 
4854 /*
4855  * This func does not prevent refcnt from increasing. But if
4856  * the caller has taken steps to that effect, then this func
4857  * can be used to determine whether the ill has become quiescent
4858  */
4859 static boolean_t
4860 ill_is_quiescent(ill_t *ill)
4861 {
4862 	ipif_t	*ipif;
4863 
4864 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4865 
4866 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4867 		if (ipif->ipif_refcnt != 0)
4868 			return (B_FALSE);
4869 	}
4870 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4871 		return (B_FALSE);
4872 	}
4873 	return (B_TRUE);
4874 }
4875 
4876 boolean_t
4877 ill_is_freeable(ill_t *ill)
4878 {
4879 	ipif_t	*ipif;
4880 
4881 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4882 
4883 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4884 		if (ipif->ipif_refcnt != 0) {
4885 			return (B_FALSE);
4886 		}
4887 	}
4888 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4889 		return (B_FALSE);
4890 	}
4891 	return (B_TRUE);
4892 }
4893 
4894 /*
4895  * This func does not prevent refcnt from increasing. But if
4896  * the caller has taken steps to that effect, then this func
4897  * can be used to determine whether the ipif has become quiescent
4898  */
4899 static boolean_t
4900 ipif_is_quiescent(ipif_t *ipif)
4901 {
4902 	ill_t *ill;
4903 
4904 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4905 
4906 	if (ipif->ipif_refcnt != 0)
4907 		return (B_FALSE);
4908 
4909 	ill = ipif->ipif_ill;
4910 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4911 	    ill->ill_logical_down) {
4912 		return (B_TRUE);
4913 	}
4914 
4915 	/* This is the last ipif going down or being deleted on this ill */
4916 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4917 		return (B_FALSE);
4918 	}
4919 
4920 	return (B_TRUE);
4921 }
4922 
4923 /*
4924  * return true if the ipif can be destroyed: the ipif has to be quiescent
4925  * with zero references from ire/ilm to it.
4926  */
4927 static boolean_t
4928 ipif_is_freeable(ipif_t *ipif)
4929 {
4930 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4931 	ASSERT(ipif->ipif_id != 0);
4932 	return (ipif->ipif_refcnt == 0);
4933 }
4934 
4935 /*
4936  * The ipif/ill/ire has been refreled. Do the tail processing.
4937  * Determine if the ipif or ill in question has become quiescent and if so
4938  * wakeup close and/or restart any queued pending ioctl that is waiting
4939  * for the ipif_down (or ill_down)
4940  */
4941 void
4942 ipif_ill_refrele_tail(ill_t *ill)
4943 {
4944 	mblk_t	*mp;
4945 	conn_t	*connp;
4946 	ipsq_t	*ipsq;
4947 	ipxop_t	*ipx;
4948 	ipif_t	*ipif;
4949 	dl_notify_ind_t *dlindp;
4950 
4951 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4952 
4953 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4954 		/* ip_modclose() may be waiting */
4955 		cv_broadcast(&ill->ill_cv);
4956 	}
4957 
4958 	ipsq = ill->ill_phyint->phyint_ipsq;
4959 	mutex_enter(&ipsq->ipsq_lock);
4960 	ipx = ipsq->ipsq_xop;
4961 	mutex_enter(&ipx->ipx_lock);
4962 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4963 		goto unlock;
4964 
4965 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4966 
4967 	ipif = ipx->ipx_pending_ipif;
4968 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4969 		goto unlock;
4970 
4971 	switch (ipx->ipx_waitfor) {
4972 	case IPIF_DOWN:
4973 		if (!ipif_is_quiescent(ipif))
4974 			goto unlock;
4975 		break;
4976 	case IPIF_FREE:
4977 		if (!ipif_is_freeable(ipif))
4978 			goto unlock;
4979 		break;
4980 	case ILL_DOWN:
4981 		if (!ill_is_quiescent(ill))
4982 			goto unlock;
4983 		break;
4984 	case ILL_FREE:
4985 		/*
4986 		 * ILL_FREE is only for loopback; normal ill teardown waits
4987 		 * synchronously in ip_modclose() without using ipx_waitfor,
4988 		 * handled by the cv_broadcast() at the top of this function.
4989 		 */
4990 		if (!ill_is_freeable(ill))
4991 			goto unlock;
4992 		break;
4993 	default:
4994 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4995 		    (void *)ipsq, ipx->ipx_waitfor);
4996 	}
4997 
4998 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4999 	mutex_exit(&ipx->ipx_lock);
5000 	mp = ipsq_pending_mp_get(ipsq, &connp);
5001 	mutex_exit(&ipsq->ipsq_lock);
5002 	mutex_exit(&ill->ill_lock);
5003 
5004 	ASSERT(mp != NULL);
5005 	/*
5006 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5007 	 * we can only get here when the current operation decides it
5008 	 * it needs to quiesce via ipsq_pending_mp_add().
5009 	 */
5010 	switch (mp->b_datap->db_type) {
5011 	case M_PCPROTO:
5012 	case M_PROTO:
5013 		/*
5014 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5015 		 */
5016 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5017 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5018 
5019 		switch (dlindp->dl_notification) {
5020 		case DL_NOTE_PHYS_ADDR:
5021 			qwriter_ip(ill, ill->ill_rq, mp,
5022 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5023 			return;
5024 		case DL_NOTE_REPLUMB:
5025 			qwriter_ip(ill, ill->ill_rq, mp,
5026 			    ill_replumb_tail, CUR_OP, B_TRUE);
5027 			return;
5028 		default:
5029 			ASSERT(0);
5030 			ill_refrele(ill);
5031 		}
5032 		break;
5033 
5034 	case M_ERROR:
5035 	case M_HANGUP:
5036 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5037 		    B_TRUE);
5038 		return;
5039 
5040 	case M_IOCTL:
5041 	case M_IOCDATA:
5042 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5043 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5044 		return;
5045 
5046 	default:
5047 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5048 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5049 	}
5050 	return;
5051 unlock:
5052 	mutex_exit(&ipsq->ipsq_lock);
5053 	mutex_exit(&ipx->ipx_lock);
5054 	mutex_exit(&ill->ill_lock);
5055 }
5056 
5057 #ifdef DEBUG
5058 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5059 static void
5060 th_trace_rrecord(th_trace_t *th_trace)
5061 {
5062 	tr_buf_t *tr_buf;
5063 	uint_t lastref;
5064 
5065 	lastref = th_trace->th_trace_lastref;
5066 	lastref++;
5067 	if (lastref == TR_BUF_MAX)
5068 		lastref = 0;
5069 	th_trace->th_trace_lastref = lastref;
5070 	tr_buf = &th_trace->th_trbuf[lastref];
5071 	tr_buf->tr_time = ddi_get_lbolt();
5072 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5073 }
5074 
5075 static void
5076 th_trace_free(void *value)
5077 {
5078 	th_trace_t *th_trace = value;
5079 
5080 	ASSERT(th_trace->th_refcnt == 0);
5081 	kmem_free(th_trace, sizeof (*th_trace));
5082 }
5083 
5084 /*
5085  * Find or create the per-thread hash table used to track object references.
5086  * The ipst argument is NULL if we shouldn't allocate.
5087  *
5088  * Accesses per-thread data, so there's no need to lock here.
5089  */
5090 static mod_hash_t *
5091 th_trace_gethash(ip_stack_t *ipst)
5092 {
5093 	th_hash_t *thh;
5094 
5095 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5096 		mod_hash_t *mh;
5097 		char name[256];
5098 		size_t objsize, rshift;
5099 		int retv;
5100 
5101 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5102 			return (NULL);
5103 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5104 		    (void *)curthread);
5105 
5106 		/*
5107 		 * We use mod_hash_create_extended here rather than the more
5108 		 * obvious mod_hash_create_ptrhash because the latter has a
5109 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5110 		 * block.
5111 		 */
5112 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5113 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5114 		rshift = highbit(objsize);
5115 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5116 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5117 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5118 		if (mh == NULL) {
5119 			kmem_free(thh, sizeof (*thh));
5120 			return (NULL);
5121 		}
5122 		thh->thh_hash = mh;
5123 		thh->thh_ipst = ipst;
5124 		/*
5125 		 * We trace ills, ipifs, ires, and nces.  All of these are
5126 		 * per-IP-stack, so the lock on the thread list is as well.
5127 		 */
5128 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5129 		list_insert_tail(&ip_thread_list, thh);
5130 		rw_exit(&ip_thread_rwlock);
5131 		retv = tsd_set(ip_thread_data, thh);
5132 		ASSERT(retv == 0);
5133 	}
5134 	return (thh != NULL ? thh->thh_hash : NULL);
5135 }
5136 
5137 boolean_t
5138 th_trace_ref(const void *obj, ip_stack_t *ipst)
5139 {
5140 	th_trace_t *th_trace;
5141 	mod_hash_t *mh;
5142 	mod_hash_val_t val;
5143 
5144 	if ((mh = th_trace_gethash(ipst)) == NULL)
5145 		return (B_FALSE);
5146 
5147 	/*
5148 	 * Attempt to locate the trace buffer for this obj and thread.
5149 	 * If it does not exist, then allocate a new trace buffer and
5150 	 * insert into the hash.
5151 	 */
5152 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5153 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5154 		if (th_trace == NULL)
5155 			return (B_FALSE);
5156 
5157 		th_trace->th_id = curthread;
5158 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5159 		    (mod_hash_val_t)th_trace) != 0) {
5160 			kmem_free(th_trace, sizeof (th_trace_t));
5161 			return (B_FALSE);
5162 		}
5163 	} else {
5164 		th_trace = (th_trace_t *)val;
5165 	}
5166 
5167 	ASSERT(th_trace->th_refcnt >= 0 &&
5168 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5169 
5170 	th_trace->th_refcnt++;
5171 	th_trace_rrecord(th_trace);
5172 	return (B_TRUE);
5173 }
5174 
5175 /*
5176  * For the purpose of tracing a reference release, we assume that global
5177  * tracing is always on and that the same thread initiated the reference hold
5178  * is releasing.
5179  */
5180 void
5181 th_trace_unref(const void *obj)
5182 {
5183 	int retv;
5184 	mod_hash_t *mh;
5185 	th_trace_t *th_trace;
5186 	mod_hash_val_t val;
5187 
5188 	mh = th_trace_gethash(NULL);
5189 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5190 	ASSERT(retv == 0);
5191 	th_trace = (th_trace_t *)val;
5192 
5193 	ASSERT(th_trace->th_refcnt > 0);
5194 	th_trace->th_refcnt--;
5195 	th_trace_rrecord(th_trace);
5196 }
5197 
5198 /*
5199  * If tracing has been disabled, then we assume that the reference counts are
5200  * now useless, and we clear them out before destroying the entries.
5201  */
5202 void
5203 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5204 {
5205 	th_hash_t	*thh;
5206 	mod_hash_t	*mh;
5207 	mod_hash_val_t	val;
5208 	th_trace_t	*th_trace;
5209 	int		retv;
5210 
5211 	rw_enter(&ip_thread_rwlock, RW_READER);
5212 	for (thh = list_head(&ip_thread_list); thh != NULL;
5213 	    thh = list_next(&ip_thread_list, thh)) {
5214 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5215 		    &val) == 0) {
5216 			th_trace = (th_trace_t *)val;
5217 			if (trace_disable)
5218 				th_trace->th_refcnt = 0;
5219 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5220 			ASSERT(retv == 0);
5221 		}
5222 	}
5223 	rw_exit(&ip_thread_rwlock);
5224 }
5225 
5226 void
5227 ipif_trace_ref(ipif_t *ipif)
5228 {
5229 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5230 
5231 	if (ipif->ipif_trace_disable)
5232 		return;
5233 
5234 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5235 		ipif->ipif_trace_disable = B_TRUE;
5236 		ipif_trace_cleanup(ipif);
5237 	}
5238 }
5239 
5240 void
5241 ipif_untrace_ref(ipif_t *ipif)
5242 {
5243 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5244 
5245 	if (!ipif->ipif_trace_disable)
5246 		th_trace_unref(ipif);
5247 }
5248 
5249 void
5250 ill_trace_ref(ill_t *ill)
5251 {
5252 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5253 
5254 	if (ill->ill_trace_disable)
5255 		return;
5256 
5257 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5258 		ill->ill_trace_disable = B_TRUE;
5259 		ill_trace_cleanup(ill);
5260 	}
5261 }
5262 
5263 void
5264 ill_untrace_ref(ill_t *ill)
5265 {
5266 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5267 
5268 	if (!ill->ill_trace_disable)
5269 		th_trace_unref(ill);
5270 }
5271 
5272 /*
5273  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5274  * failure, ipif_trace_disable is set.
5275  */
5276 static void
5277 ipif_trace_cleanup(const ipif_t *ipif)
5278 {
5279 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5280 }
5281 
5282 /*
5283  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5284  * failure, ill_trace_disable is set.
5285  */
5286 static void
5287 ill_trace_cleanup(const ill_t *ill)
5288 {
5289 	th_trace_cleanup(ill, ill->ill_trace_disable);
5290 }
5291 #endif /* DEBUG */
5292 
5293 void
5294 ipif_refhold_locked(ipif_t *ipif)
5295 {
5296 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5297 	ipif->ipif_refcnt++;
5298 	IPIF_TRACE_REF(ipif);
5299 }
5300 
5301 void
5302 ipif_refhold(ipif_t *ipif)
5303 {
5304 	ill_t	*ill;
5305 
5306 	ill = ipif->ipif_ill;
5307 	mutex_enter(&ill->ill_lock);
5308 	ipif->ipif_refcnt++;
5309 	IPIF_TRACE_REF(ipif);
5310 	mutex_exit(&ill->ill_lock);
5311 }
5312 
5313 /*
5314  * Must not be called while holding any locks. Otherwise if this is
5315  * the last reference to be released there is a chance of recursive mutex
5316  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5317  * to restart an ioctl.
5318  */
5319 void
5320 ipif_refrele(ipif_t *ipif)
5321 {
5322 	ill_t	*ill;
5323 
5324 	ill = ipif->ipif_ill;
5325 
5326 	mutex_enter(&ill->ill_lock);
5327 	ASSERT(ipif->ipif_refcnt != 0);
5328 	ipif->ipif_refcnt--;
5329 	IPIF_UNTRACE_REF(ipif);
5330 	if (ipif->ipif_refcnt != 0) {
5331 		mutex_exit(&ill->ill_lock);
5332 		return;
5333 	}
5334 
5335 	/* Drops the ill_lock */
5336 	ipif_ill_refrele_tail(ill);
5337 }
5338 
5339 ipif_t *
5340 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5341 {
5342 	ipif_t	*ipif;
5343 
5344 	mutex_enter(&ill->ill_lock);
5345 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5346 	    ipif != NULL; ipif = ipif->ipif_next) {
5347 		if (IPIF_IS_CONDEMNED(ipif))
5348 			continue;
5349 		ipif_refhold_locked(ipif);
5350 		mutex_exit(&ill->ill_lock);
5351 		return (ipif);
5352 	}
5353 	mutex_exit(&ill->ill_lock);
5354 	return (NULL);
5355 }
5356 
5357 /*
5358  * TODO: make this table extendible at run time
5359  * Return a pointer to the mac type info for 'mac_type'
5360  */
5361 static ip_m_t *
5362 ip_m_lookup(t_uscalar_t mac_type)
5363 {
5364 	ip_m_t	*ipm;
5365 
5366 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5367 		if (ipm->ip_m_mac_type == mac_type)
5368 			return (ipm);
5369 	return (NULL);
5370 }
5371 
5372 /*
5373  * Make a link layer address from the multicast IP address *addr.
5374  * To form the link layer address, invoke the ip_m_v*mapping function
5375  * associated with the link-layer type.
5376  */
5377 void
5378 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5379 {
5380 	ip_m_t *ipm;
5381 
5382 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5383 		return;
5384 
5385 	ASSERT(addr != NULL);
5386 
5387 	ipm = ip_m_lookup(ill->ill_mactype);
5388 	if (ipm == NULL ||
5389 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5390 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5391 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5392 		    ill->ill_name, ill->ill_mactype));
5393 		return;
5394 	}
5395 	if (ill->ill_isv6)
5396 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5397 	else
5398 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5399 }
5400 
5401 /*
5402  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5403  * ill is passed in to associate it with the correct interface.
5404  * If ire_arg is set, then we return the held IRE in that location.
5405  */
5406 int
5407 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5408     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5409     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5410 {
5411 	ire_t	*ire, *nire;
5412 	ire_t	*gw_ire = NULL;
5413 	ipif_t	*ipif = NULL;
5414 	uint_t	type;
5415 	int	match_flags = MATCH_IRE_TYPE;
5416 	tsol_gc_t *gc = NULL;
5417 	tsol_gcgrp_t *gcgrp = NULL;
5418 	boolean_t gcgrp_xtraref = B_FALSE;
5419 	boolean_t cgtp_broadcast;
5420 
5421 	ip1dbg(("ip_rt_add:"));
5422 
5423 	if (ire_arg != NULL)
5424 		*ire_arg = NULL;
5425 
5426 	/*
5427 	 * If this is the case of RTF_HOST being set, then we set the netmask
5428 	 * to all ones (regardless if one was supplied).
5429 	 */
5430 	if (flags & RTF_HOST)
5431 		mask = IP_HOST_MASK;
5432 
5433 	/*
5434 	 * Prevent routes with a zero gateway from being created (since
5435 	 * interfaces can currently be plumbed and brought up no assigned
5436 	 * address).
5437 	 */
5438 	if (gw_addr == 0)
5439 		return (ENETUNREACH);
5440 	/*
5441 	 * Get the ipif, if any, corresponding to the gw_addr
5442 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5443 	 * we match on the gatway and destination to handle unnumbered pt-pt
5444 	 * interfaces.
5445 	 */
5446 	if (ill != NULL)
5447 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5448 	else
5449 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5450 	if (ipif != NULL) {
5451 		if (IS_VNI(ipif->ipif_ill)) {
5452 			ipif_refrele(ipif);
5453 			return (EINVAL);
5454 		}
5455 	}
5456 
5457 	/*
5458 	 * GateD will attempt to create routes with a loopback interface
5459 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5460 	 * these routes to be added, but create them as interface routes
5461 	 * since the gateway is an interface address.
5462 	 */
5463 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5464 		flags &= ~RTF_GATEWAY;
5465 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5466 		    mask == IP_HOST_MASK) {
5467 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5468 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5469 			    NULL);
5470 			if (ire != NULL) {
5471 				ire_refrele(ire);
5472 				ipif_refrele(ipif);
5473 				return (EEXIST);
5474 			}
5475 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5476 			    "for 0x%x\n", (void *)ipif,
5477 			    ipif->ipif_ire_type,
5478 			    ntohl(ipif->ipif_lcl_addr)));
5479 			ire = ire_create(
5480 			    (uchar_t *)&dst_addr,	/* dest address */
5481 			    (uchar_t *)&mask,		/* mask */
5482 			    NULL,			/* no gateway */
5483 			    ipif->ipif_ire_type,	/* LOOPBACK */
5484 			    ipif->ipif_ill,
5485 			    zoneid,
5486 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5487 			    NULL,
5488 			    ipst);
5489 
5490 			if (ire == NULL) {
5491 				ipif_refrele(ipif);
5492 				return (ENOMEM);
5493 			}
5494 			/* src address assigned by the caller? */
5495 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5496 				ire->ire_setsrc_addr = src_addr;
5497 
5498 			nire = ire_add(ire);
5499 			if (nire == NULL) {
5500 				/*
5501 				 * In the result of failure, ire_add() will have
5502 				 * already deleted the ire in question, so there
5503 				 * is no need to do that here.
5504 				 */
5505 				ipif_refrele(ipif);
5506 				return (ENOMEM);
5507 			}
5508 			/*
5509 			 * Check if it was a duplicate entry. This handles
5510 			 * the case of two racing route adds for the same route
5511 			 */
5512 			if (nire != ire) {
5513 				ASSERT(nire->ire_identical_ref > 1);
5514 				ire_delete(nire);
5515 				ire_refrele(nire);
5516 				ipif_refrele(ipif);
5517 				return (EEXIST);
5518 			}
5519 			ire = nire;
5520 			goto save_ire;
5521 		}
5522 	}
5523 
5524 	/*
5525 	 * The routes for multicast with CGTP are quite special in that
5526 	 * the gateway is the local interface address, yet RTF_GATEWAY
5527 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5528 	 * this undocumented and unusual use of multicast routes.
5529 	 */
5530 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5531 		flags &= ~RTF_GATEWAY;
5532 
5533 	/*
5534 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5535 	 * and the gateway address provided is one of the system's interface
5536 	 * addresses.  By using the routing socket interface and supplying an
5537 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5538 	 * specifying an interface route to be created is available which uses
5539 	 * the interface index that specifies the outgoing interface rather than
5540 	 * the address of an outgoing interface (which may not be able to
5541 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5542 	 * flag, routes can be specified which not only specify the next-hop to
5543 	 * be used when routing to a certain prefix, but also which outgoing
5544 	 * interface should be used.
5545 	 *
5546 	 * Previously, interfaces would have unique addresses assigned to them
5547 	 * and so the address assigned to a particular interface could be used
5548 	 * to identify a particular interface.  One exception to this was the
5549 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5550 	 *
5551 	 * With the advent of IPv6 and its link-local addresses, this
5552 	 * restriction was relaxed and interfaces could share addresses between
5553 	 * themselves.  In fact, typically all of the link-local interfaces on
5554 	 * an IPv6 node or router will have the same link-local address.  In
5555 	 * order to differentiate between these interfaces, the use of an
5556 	 * interface index is necessary and this index can be carried inside a
5557 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5558 	 * of using the interface index, however, is that all of the ipif's that
5559 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5560 	 * cannot be used to differentiate between ipif's (or logical
5561 	 * interfaces) that belong to the same ill (physical interface).
5562 	 *
5563 	 * For example, in the following case involving IPv4 interfaces and
5564 	 * logical interfaces
5565 	 *
5566 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5567 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5568 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5569 	 *
5570 	 * the ipif's corresponding to each of these interface routes can be
5571 	 * uniquely identified by the "gateway" (actually interface address).
5572 	 *
5573 	 * In this case involving multiple IPv6 default routes to a particular
5574 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5575 	 * default route is of interest:
5576 	 *
5577 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5578 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5579 	 */
5580 
5581 	/* RTF_GATEWAY not set */
5582 	if (!(flags & RTF_GATEWAY)) {
5583 		if (sp != NULL) {
5584 			ip2dbg(("ip_rt_add: gateway security attributes "
5585 			    "cannot be set with interface route\n"));
5586 			if (ipif != NULL)
5587 				ipif_refrele(ipif);
5588 			return (EINVAL);
5589 		}
5590 
5591 		/*
5592 		 * Whether or not ill (RTA_IFP) is set, we require that
5593 		 * the gateway is one of our local addresses.
5594 		 */
5595 		if (ipif == NULL)
5596 			return (ENETUNREACH);
5597 
5598 		/*
5599 		 * We use MATCH_IRE_ILL here. If the caller specified an
5600 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5601 		 * we use the ill derived from the gateway address.
5602 		 * We can always match the gateway address since we record it
5603 		 * in ire_gateway_addr.
5604 		 * We don't allow RTA_IFP to specify a different ill than the
5605 		 * one matching the ipif to make sure we can delete the route.
5606 		 */
5607 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5608 		if (ill == NULL) {
5609 			ill = ipif->ipif_ill;
5610 		} else if (ill != ipif->ipif_ill) {
5611 			ipif_refrele(ipif);
5612 			return (EINVAL);
5613 		}
5614 
5615 		/*
5616 		 * We check for an existing entry at this point.
5617 		 *
5618 		 * Since a netmask isn't passed in via the ioctl interface
5619 		 * (SIOCADDRT), we don't check for a matching netmask in that
5620 		 * case.
5621 		 */
5622 		if (!ioctl_msg)
5623 			match_flags |= MATCH_IRE_MASK;
5624 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5625 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5626 		    NULL);
5627 		if (ire != NULL) {
5628 			ire_refrele(ire);
5629 			ipif_refrele(ipif);
5630 			return (EEXIST);
5631 		}
5632 
5633 		/*
5634 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5635 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5636 		 * gateway.
5637 		 */
5638 		ire = ire_create(
5639 		    (uchar_t *)&dst_addr,
5640 		    (uint8_t *)&mask,
5641 		    (uint8_t *)&gw_addr,
5642 		    ill->ill_net_type,
5643 		    ill,
5644 		    zoneid,
5645 		    flags,
5646 		    NULL,
5647 		    ipst);
5648 		if (ire == NULL) {
5649 			ipif_refrele(ipif);
5650 			return (ENOMEM);
5651 		}
5652 
5653 		/*
5654 		 * Some software (for example, GateD and Sun Cluster) attempts
5655 		 * to create (what amount to) IRE_PREFIX routes with the
5656 		 * loopback address as the gateway.  This is primarily done to
5657 		 * set up prefixes with the RTF_REJECT flag set (for example,
5658 		 * when generating aggregate routes.)
5659 		 *
5660 		 * If the IRE type (as defined by ill->ill_net_type) is
5661 		 * IRE_LOOPBACK, then we map the request into a
5662 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5663 		 * these interface routes, by definition, can only be that.
5664 		 *
5665 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5666 		 * routine, but rather using ire_create() directly.
5667 		 *
5668 		 */
5669 		if (ill->ill_net_type == IRE_LOOPBACK) {
5670 			ire->ire_type = IRE_IF_NORESOLVER;
5671 			ire->ire_flags |= RTF_BLACKHOLE;
5672 		}
5673 
5674 		/* src address assigned by the caller? */
5675 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5676 			ire->ire_setsrc_addr = src_addr;
5677 
5678 		nire = ire_add(ire);
5679 		if (nire == NULL) {
5680 			/*
5681 			 * In the result of failure, ire_add() will have
5682 			 * already deleted the ire in question, so there
5683 			 * is no need to do that here.
5684 			 */
5685 			ipif_refrele(ipif);
5686 			return (ENOMEM);
5687 		}
5688 		/*
5689 		 * Check if it was a duplicate entry. This handles
5690 		 * the case of two racing route adds for the same route
5691 		 */
5692 		if (nire != ire) {
5693 			ire_delete(nire);
5694 			ire_refrele(nire);
5695 			ipif_refrele(ipif);
5696 			return (EEXIST);
5697 		}
5698 		ire = nire;
5699 		goto save_ire;
5700 	}
5701 
5702 	/*
5703 	 * Get an interface IRE for the specified gateway.
5704 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5705 	 * gateway, it is currently unreachable and we fail the request
5706 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5707 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5708 	 * If RTA_IFP was specified we look on that particular ill.
5709 	 */
5710 	if (ill != NULL)
5711 		match_flags |= MATCH_IRE_ILL;
5712 
5713 	/* Check whether the gateway is reachable. */
5714 again:
5715 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5716 	if (flags & RTF_INDIRECT)
5717 		type |= IRE_OFFLINK;
5718 
5719 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5720 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5721 	if (gw_ire == NULL) {
5722 		/*
5723 		 * With IPMP, we allow host routes to influence in.mpathd's
5724 		 * target selection.  However, if the test addresses are on
5725 		 * their own network, the above lookup will fail since the
5726 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5727 		 * hidden test IREs to be found and try again.
5728 		 */
5729 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5730 			match_flags |= MATCH_IRE_TESTHIDDEN;
5731 			goto again;
5732 		}
5733 		if (ipif != NULL)
5734 			ipif_refrele(ipif);
5735 		return (ENETUNREACH);
5736 	}
5737 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5738 		ire_refrele(gw_ire);
5739 		if (ipif != NULL)
5740 			ipif_refrele(ipif);
5741 		return (ENETUNREACH);
5742 	}
5743 
5744 	/*
5745 	 * We create one of three types of IREs as a result of this request
5746 	 * based on the netmask.  A netmask of all ones (which is automatically
5747 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5748 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5749 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5750 	 * destination prefix.
5751 	 */
5752 	if (mask == IP_HOST_MASK)
5753 		type = IRE_HOST;
5754 	else if (mask == 0)
5755 		type = IRE_DEFAULT;
5756 	else
5757 		type = IRE_PREFIX;
5758 
5759 	/* check for a duplicate entry */
5760 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5761 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5762 	    0, ipst, NULL);
5763 	if (ire != NULL) {
5764 		if (ipif != NULL)
5765 			ipif_refrele(ipif);
5766 		ire_refrele(gw_ire);
5767 		ire_refrele(ire);
5768 		return (EEXIST);
5769 	}
5770 
5771 	/* Security attribute exists */
5772 	if (sp != NULL) {
5773 		tsol_gcgrp_addr_t ga;
5774 
5775 		/* find or create the gateway credentials group */
5776 		ga.ga_af = AF_INET;
5777 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5778 
5779 		/* we hold reference to it upon success */
5780 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5781 		if (gcgrp == NULL) {
5782 			if (ipif != NULL)
5783 				ipif_refrele(ipif);
5784 			ire_refrele(gw_ire);
5785 			return (ENOMEM);
5786 		}
5787 
5788 		/*
5789 		 * Create and add the security attribute to the group; a
5790 		 * reference to the group is made upon allocating a new
5791 		 * entry successfully.  If it finds an already-existing
5792 		 * entry for the security attribute in the group, it simply
5793 		 * returns it and no new reference is made to the group.
5794 		 */
5795 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5796 		if (gc == NULL) {
5797 			if (ipif != NULL)
5798 				ipif_refrele(ipif);
5799 			/* release reference held by gcgrp_lookup */
5800 			GCGRP_REFRELE(gcgrp);
5801 			ire_refrele(gw_ire);
5802 			return (ENOMEM);
5803 		}
5804 	}
5805 
5806 	/* Create the IRE. */
5807 	ire = ire_create(
5808 	    (uchar_t *)&dst_addr,		/* dest address */
5809 	    (uchar_t *)&mask,			/* mask */
5810 	    (uchar_t *)&gw_addr,		/* gateway address */
5811 	    (ushort_t)type,			/* IRE type */
5812 	    ill,
5813 	    zoneid,
5814 	    flags,
5815 	    gc,					/* security attribute */
5816 	    ipst);
5817 
5818 	/*
5819 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5820 	 * reference to the 'gcgrp'. We can now release the extra reference
5821 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5822 	 */
5823 	if (gcgrp_xtraref)
5824 		GCGRP_REFRELE(gcgrp);
5825 	if (ire == NULL) {
5826 		if (gc != NULL)
5827 			GC_REFRELE(gc);
5828 		if (ipif != NULL)
5829 			ipif_refrele(ipif);
5830 		ire_refrele(gw_ire);
5831 		return (ENOMEM);
5832 	}
5833 
5834 	/* Before we add, check if an extra CGTP broadcast is needed */
5835 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5836 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5837 
5838 	/* src address assigned by the caller? */
5839 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5840 		ire->ire_setsrc_addr = src_addr;
5841 
5842 	/*
5843 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5844 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5845 	 */
5846 
5847 	/* Add the new IRE. */
5848 	nire = ire_add(ire);
5849 	if (nire == NULL) {
5850 		/*
5851 		 * In the result of failure, ire_add() will have
5852 		 * already deleted the ire in question, so there
5853 		 * is no need to do that here.
5854 		 */
5855 		if (ipif != NULL)
5856 			ipif_refrele(ipif);
5857 		ire_refrele(gw_ire);
5858 		return (ENOMEM);
5859 	}
5860 	/*
5861 	 * Check if it was a duplicate entry. This handles
5862 	 * the case of two racing route adds for the same route
5863 	 */
5864 	if (nire != ire) {
5865 		ire_delete(nire);
5866 		ire_refrele(nire);
5867 		if (ipif != NULL)
5868 			ipif_refrele(ipif);
5869 		ire_refrele(gw_ire);
5870 		return (EEXIST);
5871 	}
5872 	ire = nire;
5873 
5874 	if (flags & RTF_MULTIRT) {
5875 		/*
5876 		 * Invoke the CGTP (multirouting) filtering module
5877 		 * to add the dst address in the filtering database.
5878 		 * Replicated inbound packets coming from that address
5879 		 * will be filtered to discard the duplicates.
5880 		 * It is not necessary to call the CGTP filter hook
5881 		 * when the dst address is a broadcast or multicast,
5882 		 * because an IP source address cannot be a broadcast
5883 		 * or a multicast.
5884 		 */
5885 		if (cgtp_broadcast) {
5886 			ip_cgtp_bcast_add(ire, ipst);
5887 			goto save_ire;
5888 		}
5889 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5890 		    !CLASSD(ire->ire_addr)) {
5891 			int res;
5892 			ipif_t *src_ipif;
5893 
5894 			/* Find the source address corresponding to gw_ire */
5895 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5896 			    NULL, zoneid, ipst);
5897 			if (src_ipif != NULL) {
5898 				res = ipst->ips_ip_cgtp_filter_ops->
5899 				    cfo_add_dest_v4(
5900 				    ipst->ips_netstack->netstack_stackid,
5901 				    ire->ire_addr,
5902 				    ire->ire_gateway_addr,
5903 				    ire->ire_setsrc_addr,
5904 				    src_ipif->ipif_lcl_addr);
5905 				ipif_refrele(src_ipif);
5906 			} else {
5907 				res = EADDRNOTAVAIL;
5908 			}
5909 			if (res != 0) {
5910 				if (ipif != NULL)
5911 					ipif_refrele(ipif);
5912 				ire_refrele(gw_ire);
5913 				ire_delete(ire);
5914 				ire_refrele(ire);	/* Held in ire_add */
5915 				return (res);
5916 			}
5917 		}
5918 	}
5919 
5920 save_ire:
5921 	if (gw_ire != NULL) {
5922 		ire_refrele(gw_ire);
5923 		gw_ire = NULL;
5924 	}
5925 	if (ill != NULL) {
5926 		/*
5927 		 * Save enough information so that we can recreate the IRE if
5928 		 * the interface goes down and then up.  The metrics associated
5929 		 * with the route will be saved as well when rts_setmetrics() is
5930 		 * called after the IRE has been created.  In the case where
5931 		 * memory cannot be allocated, none of this information will be
5932 		 * saved.
5933 		 */
5934 		ill_save_ire(ill, ire);
5935 	}
5936 	if (ioctl_msg)
5937 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5938 	if (ire_arg != NULL) {
5939 		/*
5940 		 * Store the ire that was successfully added into where ire_arg
5941 		 * points to so that callers don't have to look it up
5942 		 * themselves (but they are responsible for ire_refrele()ing
5943 		 * the ire when they are finished with it).
5944 		 */
5945 		*ire_arg = ire;
5946 	} else {
5947 		ire_refrele(ire);		/* Held in ire_add */
5948 	}
5949 	if (ipif != NULL)
5950 		ipif_refrele(ipif);
5951 	return (0);
5952 }
5953 
5954 /*
5955  * ip_rt_delete is called to delete an IPv4 route.
5956  * ill is passed in to associate it with the correct interface.
5957  */
5958 /* ARGSUSED4 */
5959 int
5960 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5961     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5962     ip_stack_t *ipst, zoneid_t zoneid)
5963 {
5964 	ire_t	*ire = NULL;
5965 	ipif_t	*ipif;
5966 	uint_t	type;
5967 	uint_t	match_flags = MATCH_IRE_TYPE;
5968 	int	err = 0;
5969 
5970 	ip1dbg(("ip_rt_delete:"));
5971 	/*
5972 	 * If this is the case of RTF_HOST being set, then we set the netmask
5973 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5974 	 */
5975 	if (flags & RTF_HOST) {
5976 		mask = IP_HOST_MASK;
5977 		match_flags |= MATCH_IRE_MASK;
5978 	} else if (rtm_addrs & RTA_NETMASK) {
5979 		match_flags |= MATCH_IRE_MASK;
5980 	}
5981 
5982 	/*
5983 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5984 	 * we check if the gateway address is one of our interfaces first,
5985 	 * and fall back on RTF_GATEWAY routes.
5986 	 *
5987 	 * This makes it possible to delete an original
5988 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5989 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5990 	 * and those can not be deleted here.
5991 	 *
5992 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5993 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5994 	 * otherwise we use the ill derived from the gateway address.
5995 	 * We can always match the gateway address since we record it
5996 	 * in ire_gateway_addr.
5997 	 *
5998 	 * For more detail on specifying routes by gateway address and by
5999 	 * interface index, see the comments in ip_rt_add().
6000 	 */
6001 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6002 	if (ipif != NULL) {
6003 		ill_t	*ill_match;
6004 
6005 		if (ill != NULL)
6006 			ill_match = ill;
6007 		else
6008 			ill_match = ipif->ipif_ill;
6009 
6010 		match_flags |= MATCH_IRE_ILL;
6011 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6012 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
6013 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
6014 			    NULL);
6015 		}
6016 		if (ire == NULL) {
6017 			match_flags |= MATCH_IRE_GW;
6018 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6019 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6020 			    match_flags, 0, ipst, NULL);
6021 		}
6022 		/* Avoid deleting routes created by kernel from an ipif */
6023 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6024 			ire_refrele(ire);
6025 			ire = NULL;
6026 		}
6027 
6028 		/* Restore in case we didn't find a match */
6029 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6030 	}
6031 
6032 	if (ire == NULL) {
6033 		/*
6034 		 * At this point, the gateway address is not one of our own
6035 		 * addresses or a matching interface route was not found.  We
6036 		 * set the IRE type to lookup based on whether
6037 		 * this is a host route, a default route or just a prefix.
6038 		 *
6039 		 * If an ill was passed in, then the lookup is based on an
6040 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6041 		 */
6042 		match_flags |= MATCH_IRE_GW;
6043 		if (ill != NULL)
6044 			match_flags |= MATCH_IRE_ILL;
6045 		if (mask == IP_HOST_MASK)
6046 			type = IRE_HOST;
6047 		else if (mask == 0)
6048 			type = IRE_DEFAULT;
6049 		else
6050 			type = IRE_PREFIX;
6051 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6052 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6053 	}
6054 
6055 	if (ipif != NULL) {
6056 		ipif_refrele(ipif);
6057 		ipif = NULL;
6058 	}
6059 
6060 	if (ire == NULL)
6061 		return (ESRCH);
6062 
6063 	if (ire->ire_flags & RTF_MULTIRT) {
6064 		/*
6065 		 * Invoke the CGTP (multirouting) filtering module
6066 		 * to remove the dst address from the filtering database.
6067 		 * Packets coming from that address will no longer be
6068 		 * filtered to remove duplicates.
6069 		 */
6070 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6071 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6072 			    ipst->ips_netstack->netstack_stackid,
6073 			    ire->ire_addr, ire->ire_gateway_addr);
6074 		}
6075 		ip_cgtp_bcast_delete(ire, ipst);
6076 	}
6077 
6078 	ill = ire->ire_ill;
6079 	if (ill != NULL)
6080 		ill_remove_saved_ire(ill, ire);
6081 	if (ioctl_msg)
6082 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6083 	ire_delete(ire);
6084 	ire_refrele(ire);
6085 	return (err);
6086 }
6087 
6088 /*
6089  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6090  */
6091 /* ARGSUSED */
6092 int
6093 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6094     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6095 {
6096 	ipaddr_t dst_addr;
6097 	ipaddr_t gw_addr;
6098 	ipaddr_t mask;
6099 	int error = 0;
6100 	mblk_t *mp1;
6101 	struct rtentry *rt;
6102 	ipif_t *ipif = NULL;
6103 	ip_stack_t	*ipst;
6104 
6105 	ASSERT(q->q_next == NULL);
6106 	ipst = CONNQ_TO_IPST(q);
6107 
6108 	ip1dbg(("ip_siocaddrt:"));
6109 	/* Existence of mp1 verified in ip_wput_nondata */
6110 	mp1 = mp->b_cont->b_cont;
6111 	rt = (struct rtentry *)mp1->b_rptr;
6112 
6113 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6114 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6115 
6116 	/*
6117 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6118 	 * to a particular host address.  In this case, we set the netmask to
6119 	 * all ones for the particular destination address.  Otherwise,
6120 	 * determine the netmask to be used based on dst_addr and the interfaces
6121 	 * in use.
6122 	 */
6123 	if (rt->rt_flags & RTF_HOST) {
6124 		mask = IP_HOST_MASK;
6125 	} else {
6126 		/*
6127 		 * Note that ip_subnet_mask returns a zero mask in the case of
6128 		 * default (an all-zeroes address).
6129 		 */
6130 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6131 	}
6132 
6133 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6134 	    B_TRUE, NULL, ipst, ALL_ZONES);
6135 	if (ipif != NULL)
6136 		ipif_refrele(ipif);
6137 	return (error);
6138 }
6139 
6140 /*
6141  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6142  */
6143 /* ARGSUSED */
6144 int
6145 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6146     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6147 {
6148 	ipaddr_t dst_addr;
6149 	ipaddr_t gw_addr;
6150 	ipaddr_t mask;
6151 	int error;
6152 	mblk_t *mp1;
6153 	struct rtentry *rt;
6154 	ipif_t *ipif = NULL;
6155 	ip_stack_t	*ipst;
6156 
6157 	ASSERT(q->q_next == NULL);
6158 	ipst = CONNQ_TO_IPST(q);
6159 
6160 	ip1dbg(("ip_siocdelrt:"));
6161 	/* Existence of mp1 verified in ip_wput_nondata */
6162 	mp1 = mp->b_cont->b_cont;
6163 	rt = (struct rtentry *)mp1->b_rptr;
6164 
6165 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6166 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6167 
6168 	/*
6169 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6170 	 * to a particular host address.  In this case, we set the netmask to
6171 	 * all ones for the particular destination address.  Otherwise,
6172 	 * determine the netmask to be used based on dst_addr and the interfaces
6173 	 * in use.
6174 	 */
6175 	if (rt->rt_flags & RTF_HOST) {
6176 		mask = IP_HOST_MASK;
6177 	} else {
6178 		/*
6179 		 * Note that ip_subnet_mask returns a zero mask in the case of
6180 		 * default (an all-zeroes address).
6181 		 */
6182 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6183 	}
6184 
6185 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6186 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6187 	    ipst, ALL_ZONES);
6188 	if (ipif != NULL)
6189 		ipif_refrele(ipif);
6190 	return (error);
6191 }
6192 
6193 /*
6194  * Enqueue the mp onto the ipsq, chained by b_next.
6195  * b_prev stores the function to be executed later, and b_queue the queue
6196  * where this mp originated.
6197  */
6198 void
6199 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6200     ill_t *pending_ill)
6201 {
6202 	conn_t	*connp;
6203 	ipxop_t *ipx = ipsq->ipsq_xop;
6204 
6205 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6206 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6207 	ASSERT(func != NULL);
6208 
6209 	mp->b_queue = q;
6210 	mp->b_prev = (void *)func;
6211 	mp->b_next = NULL;
6212 
6213 	switch (type) {
6214 	case CUR_OP:
6215 		if (ipx->ipx_mptail != NULL) {
6216 			ASSERT(ipx->ipx_mphead != NULL);
6217 			ipx->ipx_mptail->b_next = mp;
6218 		} else {
6219 			ASSERT(ipx->ipx_mphead == NULL);
6220 			ipx->ipx_mphead = mp;
6221 		}
6222 		ipx->ipx_mptail = mp;
6223 		break;
6224 
6225 	case NEW_OP:
6226 		if (ipsq->ipsq_xopq_mptail != NULL) {
6227 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6228 			ipsq->ipsq_xopq_mptail->b_next = mp;
6229 		} else {
6230 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6231 			ipsq->ipsq_xopq_mphead = mp;
6232 		}
6233 		ipsq->ipsq_xopq_mptail = mp;
6234 		ipx->ipx_ipsq_queued = B_TRUE;
6235 		break;
6236 
6237 	case SWITCH_OP:
6238 		ASSERT(ipsq->ipsq_swxop != NULL);
6239 		/* only one switch operation is currently allowed */
6240 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6241 		ipsq->ipsq_switch_mp = mp;
6242 		ipx->ipx_ipsq_queued = B_TRUE;
6243 		break;
6244 	default:
6245 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6246 	}
6247 
6248 	if (CONN_Q(q) && pending_ill != NULL) {
6249 		connp = Q_TO_CONN(q);
6250 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6251 		connp->conn_oper_pending_ill = pending_ill;
6252 	}
6253 }
6254 
6255 /*
6256  * Dequeue the next message that requested exclusive access to this IPSQ's
6257  * xop.  Specifically:
6258  *
6259  *  1. If we're still processing the current operation on `ipsq', then
6260  *     dequeue the next message for the operation (from ipx_mphead), or
6261  *     return NULL if there are no queued messages for the operation.
6262  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6263  *
6264  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6265  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6266  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6267  *     leaving IPMP groups and require a careful dance -- see the comments
6268  *     in-line below for details.  If we're leaving a group xop or if we're
6269  *     joining a group xop and become writer on it, then we proceed to (3).
6270  *     Otherwise, we return NULL and exit the xop.
6271  *
6272  *  3. For each IPSQ in the xop, return any switch operation stored on
6273  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6274  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6275  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6276  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6277  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6278  *     each phyint in the group, including the IPMP meta-interface phyint.
6279  */
6280 static mblk_t *
6281 ipsq_dq(ipsq_t *ipsq)
6282 {
6283 	ill_t	*illv4, *illv6;
6284 	mblk_t	*mp;
6285 	ipsq_t	*xopipsq;
6286 	ipsq_t	*leftipsq = NULL;
6287 	ipxop_t *ipx;
6288 	phyint_t *phyi = ipsq->ipsq_phyint;
6289 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6290 	boolean_t emptied = B_FALSE;
6291 
6292 	/*
6293 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6294 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6295 	 */
6296 	rw_enter(&ipst->ips_ill_g_lock,
6297 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6298 	mutex_enter(&ipsq->ipsq_lock);
6299 	ipx = ipsq->ipsq_xop;
6300 	mutex_enter(&ipx->ipx_lock);
6301 
6302 	/*
6303 	 * Dequeue the next message associated with the current exclusive
6304 	 * operation, if any.
6305 	 */
6306 	if ((mp = ipx->ipx_mphead) != NULL) {
6307 		ipx->ipx_mphead = mp->b_next;
6308 		if (ipx->ipx_mphead == NULL)
6309 			ipx->ipx_mptail = NULL;
6310 		mp->b_next = (void *)ipsq;
6311 		goto out;
6312 	}
6313 
6314 	if (ipx->ipx_current_ipif != NULL)
6315 		goto empty;
6316 
6317 	if (ipsq->ipsq_swxop != NULL) {
6318 		/*
6319 		 * The exclusive operation that is now being completed has
6320 		 * requested a switch to a different xop.  This happens
6321 		 * when an interface joins or leaves an IPMP group.  Joins
6322 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6323 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6324 		 * (phyint_free()), or interface plumb for an ill type
6325 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6326 		 *
6327 		 * Xop switches are not allowed on the IPMP meta-interface.
6328 		 */
6329 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6330 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6331 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6332 
6333 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6334 			/*
6335 			 * We're switching back to our own xop, so we have two
6336 			 * xop's to drain/exit: our own, and the group xop
6337 			 * that we are leaving.
6338 			 *
6339 			 * First, pull ourselves out of the group ipsq list.
6340 			 * This is safe since we're writer on ill_g_lock.
6341 			 */
6342 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6343 
6344 			xopipsq = ipx->ipx_ipsq;
6345 			while (xopipsq->ipsq_next != ipsq)
6346 				xopipsq = xopipsq->ipsq_next;
6347 
6348 			xopipsq->ipsq_next = ipsq->ipsq_next;
6349 			ipsq->ipsq_next = ipsq;
6350 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6351 			ipsq->ipsq_swxop = NULL;
6352 
6353 			/*
6354 			 * Second, prepare to exit the group xop.  The actual
6355 			 * ipsq_exit() is done at the end of this function
6356 			 * since we cannot hold any locks across ipsq_exit().
6357 			 * Note that although we drop the group's ipx_lock, no
6358 			 * threads can proceed since we're still ipx_writer.
6359 			 */
6360 			leftipsq = xopipsq;
6361 			mutex_exit(&ipx->ipx_lock);
6362 
6363 			/*
6364 			 * Third, set ipx to point to our own xop (which was
6365 			 * inactive and therefore can be entered).
6366 			 */
6367 			ipx = ipsq->ipsq_xop;
6368 			mutex_enter(&ipx->ipx_lock);
6369 			ASSERT(ipx->ipx_writer == NULL);
6370 			ASSERT(ipx->ipx_current_ipif == NULL);
6371 		} else {
6372 			/*
6373 			 * We're switching from our own xop to a group xop.
6374 			 * The requestor of the switch must ensure that the
6375 			 * group xop cannot go away (e.g. by ensuring the
6376 			 * phyint associated with the xop cannot go away).
6377 			 *
6378 			 * If we can become writer on our new xop, then we'll
6379 			 * do the drain.  Otherwise, the current writer of our
6380 			 * new xop will do the drain when it exits.
6381 			 *
6382 			 * First, splice ourselves into the group IPSQ list.
6383 			 * This is safe since we're writer on ill_g_lock.
6384 			 */
6385 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6386 
6387 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6388 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6389 				xopipsq = xopipsq->ipsq_next;
6390 
6391 			xopipsq->ipsq_next = ipsq;
6392 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6393 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6394 			ipsq->ipsq_swxop = NULL;
6395 
6396 			/*
6397 			 * Second, exit our own xop, since it's now unused.
6398 			 * This is safe since we've got the only reference.
6399 			 */
6400 			ASSERT(ipx->ipx_writer == curthread);
6401 			ipx->ipx_writer = NULL;
6402 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6403 			ipx->ipx_ipsq_queued = B_FALSE;
6404 			mutex_exit(&ipx->ipx_lock);
6405 
6406 			/*
6407 			 * Third, set ipx to point to our new xop, and check
6408 			 * if we can become writer on it.  If we cannot, then
6409 			 * the current writer will drain the IPSQ group when
6410 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6411 			 * because we're still holding ipsq_lock.
6412 			 */
6413 			ipx = ipsq->ipsq_xop;
6414 			mutex_enter(&ipx->ipx_lock);
6415 			if (ipx->ipx_writer != NULL ||
6416 			    ipx->ipx_current_ipif != NULL) {
6417 				goto out;
6418 			}
6419 		}
6420 
6421 		/*
6422 		 * Fourth, become writer on our new ipx before we continue
6423 		 * with the drain.  Note that we never dropped ipsq_lock
6424 		 * above, so no other thread could've raced with us to
6425 		 * become writer first.  Also, we're holding ipx_lock, so
6426 		 * no other thread can examine the ipx right now.
6427 		 */
6428 		ASSERT(ipx->ipx_current_ipif == NULL);
6429 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6430 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6431 		ipx->ipx_writer = curthread;
6432 		ipx->ipx_forced = B_FALSE;
6433 #ifdef DEBUG
6434 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6435 #endif
6436 	}
6437 
6438 	xopipsq = ipsq;
6439 	do {
6440 		/*
6441 		 * So that other operations operate on a consistent and
6442 		 * complete phyint, a switch message on an IPSQ must be
6443 		 * handled prior to any other operations on that IPSQ.
6444 		 */
6445 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6446 			xopipsq->ipsq_switch_mp = NULL;
6447 			ASSERT(mp->b_next == NULL);
6448 			mp->b_next = (void *)xopipsq;
6449 			goto out;
6450 		}
6451 
6452 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6453 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6454 			if (xopipsq->ipsq_xopq_mphead == NULL)
6455 				xopipsq->ipsq_xopq_mptail = NULL;
6456 			mp->b_next = (void *)xopipsq;
6457 			goto out;
6458 		}
6459 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6460 empty:
6461 	/*
6462 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6463 	 * new messages can end up on any IPSQ in the xop.
6464 	 */
6465 	ipx->ipx_writer = NULL;
6466 	ipx->ipx_forced = B_FALSE;
6467 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6468 	ipx->ipx_ipsq_queued = B_FALSE;
6469 	emptied = B_TRUE;
6470 #ifdef	DEBUG
6471 	ipx->ipx_depth = 0;
6472 #endif
6473 out:
6474 	mutex_exit(&ipx->ipx_lock);
6475 	mutex_exit(&ipsq->ipsq_lock);
6476 
6477 	/*
6478 	 * If we completely emptied the xop, then wake up any threads waiting
6479 	 * to enter any of the IPSQ's associated with it.
6480 	 */
6481 	if (emptied) {
6482 		xopipsq = ipsq;
6483 		do {
6484 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6485 				continue;
6486 
6487 			illv4 = phyi->phyint_illv4;
6488 			illv6 = phyi->phyint_illv6;
6489 
6490 			GRAB_ILL_LOCKS(illv4, illv6);
6491 			if (illv4 != NULL)
6492 				cv_broadcast(&illv4->ill_cv);
6493 			if (illv6 != NULL)
6494 				cv_broadcast(&illv6->ill_cv);
6495 			RELEASE_ILL_LOCKS(illv4, illv6);
6496 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6497 	}
6498 	rw_exit(&ipst->ips_ill_g_lock);
6499 
6500 	/*
6501 	 * Now that all locks are dropped, exit the IPSQ we left.
6502 	 */
6503 	if (leftipsq != NULL)
6504 		ipsq_exit(leftipsq);
6505 
6506 	return (mp);
6507 }
6508 
6509 /*
6510  * Return completion status of previously initiated DLPI operations on
6511  * ills in the purview of an ipsq.
6512  */
6513 static boolean_t
6514 ipsq_dlpi_done(ipsq_t *ipsq)
6515 {
6516 	ipsq_t		*ipsq_start;
6517 	phyint_t	*phyi;
6518 	ill_t		*ill;
6519 
6520 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6521 	ipsq_start = ipsq;
6522 
6523 	do {
6524 		/*
6525 		 * The only current users of this function are ipsq_try_enter
6526 		 * and ipsq_enter which have made sure that ipsq_writer is
6527 		 * NULL before we reach here. ill_dlpi_pending is modified
6528 		 * only by an ipsq writer
6529 		 */
6530 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6531 		phyi = ipsq->ipsq_phyint;
6532 		/*
6533 		 * phyi could be NULL if a phyint that is part of an
6534 		 * IPMP group is being unplumbed. A more detailed
6535 		 * comment is in ipmp_grp_update_kstats()
6536 		 */
6537 		if (phyi != NULL) {
6538 			ill = phyi->phyint_illv4;
6539 			if (ill != NULL &&
6540 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6541 			    ill->ill_arl_dlpi_pending))
6542 				return (B_FALSE);
6543 
6544 			ill = phyi->phyint_illv6;
6545 			if (ill != NULL &&
6546 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6547 				return (B_FALSE);
6548 		}
6549 
6550 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6551 
6552 	return (B_TRUE);
6553 }
6554 
6555 /*
6556  * Enter the ipsq corresponding to ill, by waiting synchronously till
6557  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6558  * will have to drain completely before ipsq_enter returns success.
6559  * ipx_current_ipif will be set if some exclusive op is in progress,
6560  * and the ipsq_exit logic will start the next enqueued op after
6561  * completion of the current op. If 'force' is used, we don't wait
6562  * for the enqueued ops. This is needed when a conn_close wants to
6563  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6564  * of an ill can also use this option. But we dont' use it currently.
6565  */
6566 #define	ENTER_SQ_WAIT_TICKS 100
6567 boolean_t
6568 ipsq_enter(ill_t *ill, boolean_t force, int type)
6569 {
6570 	ipsq_t	*ipsq;
6571 	ipxop_t *ipx;
6572 	boolean_t waited_enough = B_FALSE;
6573 	ip_stack_t *ipst = ill->ill_ipst;
6574 
6575 	/*
6576 	 * Note that the relationship between ill and ipsq is fixed as long as
6577 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6578 	 * relationship between the IPSQ and xop cannot change.  However,
6579 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6580 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6581 	 * waking up all ills in the xop when it becomes available.
6582 	 */
6583 	for (;;) {
6584 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6585 		mutex_enter(&ill->ill_lock);
6586 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6587 			mutex_exit(&ill->ill_lock);
6588 			rw_exit(&ipst->ips_ill_g_lock);
6589 			return (B_FALSE);
6590 		}
6591 
6592 		ipsq = ill->ill_phyint->phyint_ipsq;
6593 		mutex_enter(&ipsq->ipsq_lock);
6594 		ipx = ipsq->ipsq_xop;
6595 		mutex_enter(&ipx->ipx_lock);
6596 
6597 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6598 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6599 		    waited_enough))
6600 			break;
6601 
6602 		rw_exit(&ipst->ips_ill_g_lock);
6603 
6604 		if (!force || ipx->ipx_writer != NULL) {
6605 			mutex_exit(&ipx->ipx_lock);
6606 			mutex_exit(&ipsq->ipsq_lock);
6607 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6608 		} else {
6609 			mutex_exit(&ipx->ipx_lock);
6610 			mutex_exit(&ipsq->ipsq_lock);
6611 			(void) cv_reltimedwait(&ill->ill_cv,
6612 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6613 			waited_enough = B_TRUE;
6614 		}
6615 		mutex_exit(&ill->ill_lock);
6616 	}
6617 
6618 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6619 	ASSERT(ipx->ipx_reentry_cnt == 0);
6620 	ipx->ipx_writer = curthread;
6621 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6622 	ipx->ipx_reentry_cnt++;
6623 #ifdef DEBUG
6624 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6625 #endif
6626 	mutex_exit(&ipx->ipx_lock);
6627 	mutex_exit(&ipsq->ipsq_lock);
6628 	mutex_exit(&ill->ill_lock);
6629 	rw_exit(&ipst->ips_ill_g_lock);
6630 
6631 	return (B_TRUE);
6632 }
6633 
6634 /*
6635  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6636  * across the call to the core interface ipsq_try_enter() and hence calls this
6637  * function directly. This is explained more fully in ipif_set_values().
6638  * In order to support the above constraint, ipsq_try_enter is implemented as
6639  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6640  */
6641 static ipsq_t *
6642 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6643     int type, boolean_t reentry_ok)
6644 {
6645 	ipsq_t	*ipsq;
6646 	ipxop_t	*ipx;
6647 	ip_stack_t *ipst = ill->ill_ipst;
6648 
6649 	/*
6650 	 * lock ordering:
6651 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6652 	 *
6653 	 * ipx of an ipsq can't change when ipsq_lock is held.
6654 	 */
6655 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6656 	GRAB_CONN_LOCK(q);
6657 	mutex_enter(&ill->ill_lock);
6658 	ipsq = ill->ill_phyint->phyint_ipsq;
6659 	mutex_enter(&ipsq->ipsq_lock);
6660 	ipx = ipsq->ipsq_xop;
6661 	mutex_enter(&ipx->ipx_lock);
6662 
6663 	/*
6664 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6665 	 *    (Note: If the caller does not specify reentry_ok then neither
6666 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6667 	 *    again. Otherwise it can lead to an infinite loop
6668 	 * 2. Enter the ipsq if there is no current writer and this attempted
6669 	 *    entry is part of the current operation
6670 	 * 3. Enter the ipsq if there is no current writer and this is a new
6671 	 *    operation and the operation queue is empty and there is no
6672 	 *    operation currently in progress and if all previously initiated
6673 	 *    DLPI operations have completed.
6674 	 */
6675 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6676 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6677 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6678 	    ipsq_dlpi_done(ipsq))))) {
6679 		/* Success. */
6680 		ipx->ipx_reentry_cnt++;
6681 		ipx->ipx_writer = curthread;
6682 		ipx->ipx_forced = B_FALSE;
6683 		mutex_exit(&ipx->ipx_lock);
6684 		mutex_exit(&ipsq->ipsq_lock);
6685 		mutex_exit(&ill->ill_lock);
6686 		RELEASE_CONN_LOCK(q);
6687 #ifdef DEBUG
6688 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6689 #endif
6690 		return (ipsq);
6691 	}
6692 
6693 	if (func != NULL)
6694 		ipsq_enq(ipsq, q, mp, func, type, ill);
6695 
6696 	mutex_exit(&ipx->ipx_lock);
6697 	mutex_exit(&ipsq->ipsq_lock);
6698 	mutex_exit(&ill->ill_lock);
6699 	RELEASE_CONN_LOCK(q);
6700 	return (NULL);
6701 }
6702 
6703 /*
6704  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6705  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6706  * There is one ipsq per phyint. The ipsq
6707  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6708  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6709  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6710  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6711  * up the interface) and are enqueued in ipx_mphead.
6712  *
6713  * If a thread does not want to reenter the ipsq when it is already writer,
6714  * it must make sure that the specified reentry point to be called later
6715  * when the ipsq is empty, nor any code path starting from the specified reentry
6716  * point must never ever try to enter the ipsq again. Otherwise it can lead
6717  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6718  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6719  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6720  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6721  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6722  * ioctl if the current ioctl has completed. If the current ioctl is still
6723  * in progress it simply returns. The current ioctl could be waiting for
6724  * a response from another module (the driver or could be waiting for
6725  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6726  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6727  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6728  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6729  * all associated DLPI operations have completed.
6730  */
6731 
6732 /*
6733  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6734  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6735  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6736  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6737  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6738  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6739  */
6740 ipsq_t *
6741 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6742     ipsq_func_t func, int type, boolean_t reentry_ok)
6743 {
6744 	ip_stack_t	*ipst;
6745 	ipsq_t		*ipsq;
6746 
6747 	/* Only 1 of ipif or ill can be specified */
6748 	ASSERT((ipif != NULL) ^ (ill != NULL));
6749 
6750 	if (ipif != NULL)
6751 		ill = ipif->ipif_ill;
6752 	ipst = ill->ill_ipst;
6753 
6754 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6755 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6756 	rw_exit(&ipst->ips_ill_g_lock);
6757 
6758 	return (ipsq);
6759 }
6760 
6761 /*
6762  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6763  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6764  * cannot be entered, the mp is queued for completion.
6765  */
6766 void
6767 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6768     boolean_t reentry_ok)
6769 {
6770 	ipsq_t	*ipsq;
6771 
6772 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6773 
6774 	/*
6775 	 * Drop the caller's refhold on the ill.  This is safe since we either
6776 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6777 	 * IPSQ, in which case we return without accessing ill anymore.  This
6778 	 * is needed because func needs to see the correct refcount.
6779 	 * e.g. removeif can work only then.
6780 	 */
6781 	ill_refrele(ill);
6782 	if (ipsq != NULL) {
6783 		(*func)(ipsq, q, mp, NULL);
6784 		ipsq_exit(ipsq);
6785 	}
6786 }
6787 
6788 /*
6789  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6790  * prior to exiting.  Caller must be writer on the specified IPSQ.
6791  */
6792 void
6793 ipsq_exit(ipsq_t *ipsq)
6794 {
6795 	mblk_t *mp;
6796 	ipsq_t *mp_ipsq;
6797 	queue_t	*q;
6798 	phyint_t *phyi;
6799 	ipsq_func_t func;
6800 
6801 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6802 
6803 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6804 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6805 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6806 		return;
6807 	}
6808 
6809 	for (;;) {
6810 		phyi = ipsq->ipsq_phyint;
6811 		mp = ipsq_dq(ipsq);
6812 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6813 
6814 		/*
6815 		 * If we've changed to a new IPSQ, and the phyint associated
6816 		 * with the old one has gone away, free the old IPSQ.  Note
6817 		 * that this cannot happen while the IPSQ is in a group.
6818 		 */
6819 		if (mp_ipsq != ipsq && phyi == NULL) {
6820 			ASSERT(ipsq->ipsq_next == ipsq);
6821 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6822 			ipsq_delete(ipsq);
6823 		}
6824 
6825 		if (mp == NULL)
6826 			break;
6827 
6828 		q = mp->b_queue;
6829 		func = (ipsq_func_t)mp->b_prev;
6830 		ipsq = mp_ipsq;
6831 		mp->b_next = mp->b_prev = NULL;
6832 		mp->b_queue = NULL;
6833 
6834 		/*
6835 		 * If 'q' is an conn queue, it is valid, since we did a
6836 		 * a refhold on the conn at the start of the ioctl.
6837 		 * If 'q' is an ill queue, it is valid, since close of an
6838 		 * ill will clean up its IPSQ.
6839 		 */
6840 		(*func)(ipsq, q, mp, NULL);
6841 	}
6842 }
6843 
6844 /*
6845  * Used to start any igmp or mld timers that could not be started
6846  * while holding ill_mcast_lock. The timers can't be started while holding
6847  * the lock, since mld/igmp_start_timers may need to call untimeout()
6848  * which can't be done while holding the lock which the timeout handler
6849  * acquires. Otherwise
6850  * there could be a deadlock since the timeout handlers
6851  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6852  * ill_mcast_lock.
6853  */
6854 void
6855 ill_mcast_timer_start(ip_stack_t *ipst)
6856 {
6857 	int		next;
6858 
6859 	mutex_enter(&ipst->ips_igmp_timer_lock);
6860 	next = ipst->ips_igmp_deferred_next;
6861 	ipst->ips_igmp_deferred_next = INFINITY;
6862 	mutex_exit(&ipst->ips_igmp_timer_lock);
6863 
6864 	if (next != INFINITY)
6865 		igmp_start_timers(next, ipst);
6866 
6867 	mutex_enter(&ipst->ips_mld_timer_lock);
6868 	next = ipst->ips_mld_deferred_next;
6869 	ipst->ips_mld_deferred_next = INFINITY;
6870 	mutex_exit(&ipst->ips_mld_timer_lock);
6871 
6872 	if (next != INFINITY)
6873 		mld_start_timers(next, ipst);
6874 }
6875 
6876 /*
6877  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6878  * and `ioccmd'.
6879  */
6880 void
6881 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6882 {
6883 	ill_t *ill = ipif->ipif_ill;
6884 	ipxop_t *ipx = ipsq->ipsq_xop;
6885 
6886 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6887 	ASSERT(ipx->ipx_current_ipif == NULL);
6888 	ASSERT(ipx->ipx_current_ioctl == 0);
6889 
6890 	ipx->ipx_current_done = B_FALSE;
6891 	ipx->ipx_current_ioctl = ioccmd;
6892 	mutex_enter(&ipx->ipx_lock);
6893 	ipx->ipx_current_ipif = ipif;
6894 	mutex_exit(&ipx->ipx_lock);
6895 
6896 	/*
6897 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6898 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6899 	 * references to the ipif (so that the references will eventually
6900 	 * drop to zero) and also prevents any "get" operations (e.g.,
6901 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6902 	 * operation has completed and the ipif is again in a stable state.
6903 	 *
6904 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6905 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6906 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6907 	 * ipifs will be affected.
6908 	 *
6909 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6910 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6911 	 * operate on.
6912 	 */
6913 	switch (ioccmd) {
6914 	case SIOCLIFREMOVEIF:
6915 		break;
6916 	case 0:
6917 		mutex_enter(&ill->ill_lock);
6918 		ipif = ipif->ipif_ill->ill_ipif;
6919 		for (; ipif != NULL; ipif = ipif->ipif_next)
6920 			ipif->ipif_state_flags |= IPIF_CHANGING;
6921 		mutex_exit(&ill->ill_lock);
6922 		break;
6923 	default:
6924 		mutex_enter(&ill->ill_lock);
6925 		ipif->ipif_state_flags |= IPIF_CHANGING;
6926 		mutex_exit(&ill->ill_lock);
6927 	}
6928 }
6929 
6930 /*
6931  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6932  * the next exclusive operation to begin once we ipsq_exit().  However, if
6933  * pending DLPI operations remain, then we will wait for the queue to drain
6934  * before allowing the next exclusive operation to begin.  This ensures that
6935  * DLPI operations from one exclusive operation are never improperly processed
6936  * as part of a subsequent exclusive operation.
6937  */
6938 void
6939 ipsq_current_finish(ipsq_t *ipsq)
6940 {
6941 	ipxop_t	*ipx = ipsq->ipsq_xop;
6942 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6943 	ipif_t	*ipif = ipx->ipx_current_ipif;
6944 
6945 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6946 
6947 	/*
6948 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6949 	 * (but in that case, IPIF_CHANGING will already be clear and no
6950 	 * pending DLPI messages can remain).
6951 	 */
6952 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6953 		ill_t *ill = ipif->ipif_ill;
6954 
6955 		mutex_enter(&ill->ill_lock);
6956 		dlpi_pending = ill->ill_dlpi_pending;
6957 		if (ipx->ipx_current_ioctl == 0) {
6958 			ipif = ill->ill_ipif;
6959 			for (; ipif != NULL; ipif = ipif->ipif_next)
6960 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6961 		} else {
6962 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6963 		}
6964 		mutex_exit(&ill->ill_lock);
6965 	}
6966 
6967 	ASSERT(!ipx->ipx_current_done);
6968 	ipx->ipx_current_done = B_TRUE;
6969 	ipx->ipx_current_ioctl = 0;
6970 	if (dlpi_pending == DL_PRIM_INVAL) {
6971 		mutex_enter(&ipx->ipx_lock);
6972 		ipx->ipx_current_ipif = NULL;
6973 		mutex_exit(&ipx->ipx_lock);
6974 	}
6975 }
6976 
6977 /*
6978  * The ill is closing. Flush all messages on the ipsq that originated
6979  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6980  * for this ill since ipsq_enter could not have entered until then.
6981  * New messages can't be queued since the CONDEMNED flag is set.
6982  */
6983 static void
6984 ipsq_flush(ill_t *ill)
6985 {
6986 	queue_t	*q;
6987 	mblk_t	*prev;
6988 	mblk_t	*mp;
6989 	mblk_t	*mp_next;
6990 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6991 
6992 	ASSERT(IAM_WRITER_ILL(ill));
6993 
6994 	/*
6995 	 * Flush any messages sent up by the driver.
6996 	 */
6997 	mutex_enter(&ipx->ipx_lock);
6998 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6999 		mp_next = mp->b_next;
7000 		q = mp->b_queue;
7001 		if (q == ill->ill_rq || q == ill->ill_wq) {
7002 			/* dequeue mp */
7003 			if (prev == NULL)
7004 				ipx->ipx_mphead = mp->b_next;
7005 			else
7006 				prev->b_next = mp->b_next;
7007 			if (ipx->ipx_mptail == mp) {
7008 				ASSERT(mp_next == NULL);
7009 				ipx->ipx_mptail = prev;
7010 			}
7011 			inet_freemsg(mp);
7012 		} else {
7013 			prev = mp;
7014 		}
7015 	}
7016 	mutex_exit(&ipx->ipx_lock);
7017 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7018 	ipsq_xopq_mp_cleanup(ill, NULL);
7019 }
7020 
7021 /*
7022  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7023  * and return the associated ipif.
7024  * Return value:
7025  *	Non zero: An error has occurred. ci may not be filled out.
7026  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7027  *	a held ipif in ci.ci_ipif.
7028  */
7029 int
7030 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7031     cmd_info_t *ci)
7032 {
7033 	char		*name;
7034 	struct ifreq    *ifr;
7035 	struct lifreq    *lifr;
7036 	ipif_t		*ipif = NULL;
7037 	ill_t		*ill;
7038 	conn_t		*connp;
7039 	boolean_t	isv6;
7040 	boolean_t	exists;
7041 	mblk_t		*mp1;
7042 	zoneid_t	zoneid;
7043 	ip_stack_t	*ipst;
7044 
7045 	if (q->q_next != NULL) {
7046 		ill = (ill_t *)q->q_ptr;
7047 		isv6 = ill->ill_isv6;
7048 		connp = NULL;
7049 		zoneid = ALL_ZONES;
7050 		ipst = ill->ill_ipst;
7051 	} else {
7052 		ill = NULL;
7053 		connp = Q_TO_CONN(q);
7054 		isv6 = (connp->conn_family == AF_INET6);
7055 		zoneid = connp->conn_zoneid;
7056 		if (zoneid == GLOBAL_ZONEID) {
7057 			/* global zone can access ipifs in all zones */
7058 			zoneid = ALL_ZONES;
7059 		}
7060 		ipst = connp->conn_netstack->netstack_ip;
7061 	}
7062 
7063 	/* Has been checked in ip_wput_nondata */
7064 	mp1 = mp->b_cont->b_cont;
7065 
7066 	if (ipip->ipi_cmd_type == IF_CMD) {
7067 		/* This a old style SIOC[GS]IF* command */
7068 		ifr = (struct ifreq *)mp1->b_rptr;
7069 		/*
7070 		 * Null terminate the string to protect against buffer
7071 		 * overrun. String was generated by user code and may not
7072 		 * be trusted.
7073 		 */
7074 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7075 		name = ifr->ifr_name;
7076 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7077 		ci->ci_sin6 = NULL;
7078 		ci->ci_lifr = (struct lifreq *)ifr;
7079 	} else {
7080 		/* This a new style SIOC[GS]LIF* command */
7081 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7082 		lifr = (struct lifreq *)mp1->b_rptr;
7083 		/*
7084 		 * Null terminate the string to protect against buffer
7085 		 * overrun. String was generated by user code and may not
7086 		 * be trusted.
7087 		 */
7088 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7089 		name = lifr->lifr_name;
7090 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7091 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7092 		ci->ci_lifr = lifr;
7093 	}
7094 
7095 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7096 		/*
7097 		 * The ioctl will be failed if the ioctl comes down
7098 		 * an conn stream
7099 		 */
7100 		if (ill == NULL) {
7101 			/*
7102 			 * Not an ill queue, return EINVAL same as the
7103 			 * old error code.
7104 			 */
7105 			return (ENXIO);
7106 		}
7107 		ipif = ill->ill_ipif;
7108 		ipif_refhold(ipif);
7109 	} else {
7110 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7111 		    &exists, isv6, zoneid, ipst);
7112 
7113 		/*
7114 		 * Ensure that get ioctls don't see any internal state changes
7115 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7116 		 * set.
7117 		 */
7118 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7119 		    !IAM_WRITER_IPIF(ipif)) {
7120 			ipsq_t	*ipsq;
7121 
7122 			if (connp != NULL)
7123 				mutex_enter(&connp->conn_lock);
7124 			mutex_enter(&ipif->ipif_ill->ill_lock);
7125 			if (IPIF_IS_CHANGING(ipif) &&
7126 			    !IPIF_IS_CONDEMNED(ipif)) {
7127 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7128 				mutex_enter(&ipsq->ipsq_lock);
7129 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7130 				mutex_exit(&ipif->ipif_ill->ill_lock);
7131 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7132 				    NEW_OP, ipif->ipif_ill);
7133 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7134 				mutex_exit(&ipsq->ipsq_lock);
7135 				if (connp != NULL)
7136 					mutex_exit(&connp->conn_lock);
7137 				ipif_refrele(ipif);
7138 				return (EINPROGRESS);
7139 			}
7140 			mutex_exit(&ipif->ipif_ill->ill_lock);
7141 			if (connp != NULL)
7142 				mutex_exit(&connp->conn_lock);
7143 		}
7144 	}
7145 
7146 	/*
7147 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7148 	 */
7149 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7150 		ipif_refrele(ipif);
7151 		return (ENXIO);
7152 	}
7153 
7154 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7155 	    name[0] == '\0') {
7156 		/*
7157 		 * Handle a or a SIOC?IF* with a null name
7158 		 * during plumb (on the ill queue before the I_PLINK).
7159 		 */
7160 		ipif = ill->ill_ipif;
7161 		ipif_refhold(ipif);
7162 	}
7163 
7164 	if (ipif == NULL)
7165 		return (ENXIO);
7166 
7167 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7168 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7169 
7170 	ci->ci_ipif = ipif;
7171 	return (0);
7172 }
7173 
7174 /*
7175  * Return the total number of ipifs.
7176  */
7177 static uint_t
7178 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7179 {
7180 	uint_t numifs = 0;
7181 	ill_t	*ill;
7182 	ill_walk_context_t	ctx;
7183 	ipif_t	*ipif;
7184 
7185 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7186 	ill = ILL_START_WALK_V4(&ctx, ipst);
7187 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7188 		if (IS_UNDER_IPMP(ill))
7189 			continue;
7190 		for (ipif = ill->ill_ipif; ipif != NULL;
7191 		    ipif = ipif->ipif_next) {
7192 			if (ipif->ipif_zoneid == zoneid ||
7193 			    ipif->ipif_zoneid == ALL_ZONES)
7194 				numifs++;
7195 		}
7196 	}
7197 	rw_exit(&ipst->ips_ill_g_lock);
7198 	return (numifs);
7199 }
7200 
7201 /*
7202  * Return the total number of ipifs.
7203  */
7204 static uint_t
7205 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7206 {
7207 	uint_t numifs = 0;
7208 	ill_t	*ill;
7209 	ipif_t	*ipif;
7210 	ill_walk_context_t	ctx;
7211 
7212 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7213 
7214 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7215 	if (family == AF_INET)
7216 		ill = ILL_START_WALK_V4(&ctx, ipst);
7217 	else if (family == AF_INET6)
7218 		ill = ILL_START_WALK_V6(&ctx, ipst);
7219 	else
7220 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7221 
7222 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7223 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7224 			continue;
7225 
7226 		for (ipif = ill->ill_ipif; ipif != NULL;
7227 		    ipif = ipif->ipif_next) {
7228 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7229 			    !(lifn_flags & LIFC_NOXMIT))
7230 				continue;
7231 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7232 			    !(lifn_flags & LIFC_TEMPORARY))
7233 				continue;
7234 			if (((ipif->ipif_flags &
7235 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7236 			    IPIF_DEPRECATED)) ||
7237 			    IS_LOOPBACK(ill) ||
7238 			    !(ipif->ipif_flags & IPIF_UP)) &&
7239 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7240 				continue;
7241 
7242 			if (zoneid != ipif->ipif_zoneid &&
7243 			    ipif->ipif_zoneid != ALL_ZONES &&
7244 			    (zoneid != GLOBAL_ZONEID ||
7245 			    !(lifn_flags & LIFC_ALLZONES)))
7246 				continue;
7247 
7248 			numifs++;
7249 		}
7250 	}
7251 	rw_exit(&ipst->ips_ill_g_lock);
7252 	return (numifs);
7253 }
7254 
7255 uint_t
7256 ip_get_lifsrcofnum(ill_t *ill)
7257 {
7258 	uint_t numifs = 0;
7259 	ill_t	*ill_head = ill;
7260 	ip_stack_t	*ipst = ill->ill_ipst;
7261 
7262 	/*
7263 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7264 	 * other thread may be trying to relink the ILLs in this usesrc group
7265 	 * and adjusting the ill_usesrc_grp_next pointers
7266 	 */
7267 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7268 	if ((ill->ill_usesrc_ifindex == 0) &&
7269 	    (ill->ill_usesrc_grp_next != NULL)) {
7270 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7271 		    ill = ill->ill_usesrc_grp_next)
7272 			numifs++;
7273 	}
7274 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7275 
7276 	return (numifs);
7277 }
7278 
7279 /* Null values are passed in for ipif, sin, and ifreq */
7280 /* ARGSUSED */
7281 int
7282 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7283     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7284 {
7285 	int *nump;
7286 	conn_t *connp = Q_TO_CONN(q);
7287 
7288 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7289 
7290 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7291 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7292 
7293 	*nump = ip_get_numifs(connp->conn_zoneid,
7294 	    connp->conn_netstack->netstack_ip);
7295 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7296 	return (0);
7297 }
7298 
7299 /* Null values are passed in for ipif, sin, and ifreq */
7300 /* ARGSUSED */
7301 int
7302 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7303     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7304 {
7305 	struct lifnum *lifn;
7306 	mblk_t	*mp1;
7307 	conn_t *connp = Q_TO_CONN(q);
7308 
7309 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7310 
7311 	/* Existence checked in ip_wput_nondata */
7312 	mp1 = mp->b_cont->b_cont;
7313 
7314 	lifn = (struct lifnum *)mp1->b_rptr;
7315 	switch (lifn->lifn_family) {
7316 	case AF_UNSPEC:
7317 	case AF_INET:
7318 	case AF_INET6:
7319 		break;
7320 	default:
7321 		return (EAFNOSUPPORT);
7322 	}
7323 
7324 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7325 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7326 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7327 	return (0);
7328 }
7329 
7330 /* ARGSUSED */
7331 int
7332 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7333     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7334 {
7335 	STRUCT_HANDLE(ifconf, ifc);
7336 	mblk_t *mp1;
7337 	struct iocblk *iocp;
7338 	struct ifreq *ifr;
7339 	ill_walk_context_t	ctx;
7340 	ill_t	*ill;
7341 	ipif_t	*ipif;
7342 	struct sockaddr_in *sin;
7343 	int32_t	ifclen;
7344 	zoneid_t zoneid;
7345 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7346 
7347 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7348 
7349 	ip1dbg(("ip_sioctl_get_ifconf"));
7350 	/* Existence verified in ip_wput_nondata */
7351 	mp1 = mp->b_cont->b_cont;
7352 	iocp = (struct iocblk *)mp->b_rptr;
7353 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7354 
7355 	/*
7356 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7357 	 * the user buffer address and length into which the list of struct
7358 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7359 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7360 	 * the SIOCGIFCONF operation was redefined to simply provide
7361 	 * a large output buffer into which we are supposed to jam the ifreq
7362 	 * array.  The same ioctl command code was used, despite the fact that
7363 	 * both the applications and the kernel code had to change, thus making
7364 	 * it impossible to support both interfaces.
7365 	 *
7366 	 * For reasons not good enough to try to explain, the following
7367 	 * algorithm is used for deciding what to do with one of these:
7368 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7369 	 * form with the output buffer coming down as the continuation message.
7370 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7371 	 * and we have to copy in the ifconf structure to find out how big the
7372 	 * output buffer is and where to copy out to.  Sure no problem...
7373 	 *
7374 	 */
7375 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7376 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7377 		int numifs = 0;
7378 		size_t ifc_bufsize;
7379 
7380 		/*
7381 		 * Must be (better be!) continuation of a TRANSPARENT
7382 		 * IOCTL.  We just copied in the ifconf structure.
7383 		 */
7384 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7385 		    (struct ifconf *)mp1->b_rptr);
7386 
7387 		/*
7388 		 * Allocate a buffer to hold requested information.
7389 		 *
7390 		 * If ifc_len is larger than what is needed, we only
7391 		 * allocate what we will use.
7392 		 *
7393 		 * If ifc_len is smaller than what is needed, return
7394 		 * EINVAL.
7395 		 *
7396 		 * XXX: the ill_t structure can hava 2 counters, for
7397 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7398 		 * number of interfaces for a device, so we don't need
7399 		 * to count them here...
7400 		 */
7401 		numifs = ip_get_numifs(zoneid, ipst);
7402 
7403 		ifclen = STRUCT_FGET(ifc, ifc_len);
7404 		ifc_bufsize = numifs * sizeof (struct ifreq);
7405 		if (ifc_bufsize > ifclen) {
7406 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7407 				/* old behaviour */
7408 				return (EINVAL);
7409 			} else {
7410 				ifc_bufsize = ifclen;
7411 			}
7412 		}
7413 
7414 		mp1 = mi_copyout_alloc(q, mp,
7415 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7416 		if (mp1 == NULL)
7417 			return (ENOMEM);
7418 
7419 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7420 	}
7421 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7422 	/*
7423 	 * the SIOCGIFCONF ioctl only knows about
7424 	 * IPv4 addresses, so don't try to tell
7425 	 * it about interfaces with IPv6-only
7426 	 * addresses. (Last parm 'isv6' is B_FALSE)
7427 	 */
7428 
7429 	ifr = (struct ifreq *)mp1->b_rptr;
7430 
7431 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7432 	ill = ILL_START_WALK_V4(&ctx, ipst);
7433 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7434 		if (IS_UNDER_IPMP(ill))
7435 			continue;
7436 		for (ipif = ill->ill_ipif; ipif != NULL;
7437 		    ipif = ipif->ipif_next) {
7438 			if (zoneid != ipif->ipif_zoneid &&
7439 			    ipif->ipif_zoneid != ALL_ZONES)
7440 				continue;
7441 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7442 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7443 					/* old behaviour */
7444 					rw_exit(&ipst->ips_ill_g_lock);
7445 					return (EINVAL);
7446 				} else {
7447 					goto if_copydone;
7448 				}
7449 			}
7450 			ipif_get_name(ipif, ifr->ifr_name,
7451 			    sizeof (ifr->ifr_name));
7452 			sin = (sin_t *)&ifr->ifr_addr;
7453 			*sin = sin_null;
7454 			sin->sin_family = AF_INET;
7455 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7456 			ifr++;
7457 		}
7458 	}
7459 if_copydone:
7460 	rw_exit(&ipst->ips_ill_g_lock);
7461 	mp1->b_wptr = (uchar_t *)ifr;
7462 
7463 	if (STRUCT_BUF(ifc) != NULL) {
7464 		STRUCT_FSET(ifc, ifc_len,
7465 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7466 	}
7467 	return (0);
7468 }
7469 
7470 /*
7471  * Get the interfaces using the address hosted on the interface passed in,
7472  * as a source adddress
7473  */
7474 /* ARGSUSED */
7475 int
7476 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7477     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7478 {
7479 	mblk_t *mp1;
7480 	ill_t	*ill, *ill_head;
7481 	ipif_t	*ipif, *orig_ipif;
7482 	int	numlifs = 0;
7483 	size_t	lifs_bufsize, lifsmaxlen;
7484 	struct	lifreq *lifr;
7485 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7486 	uint_t	ifindex;
7487 	zoneid_t zoneid;
7488 	boolean_t isv6 = B_FALSE;
7489 	struct	sockaddr_in	*sin;
7490 	struct	sockaddr_in6	*sin6;
7491 	STRUCT_HANDLE(lifsrcof, lifs);
7492 	ip_stack_t		*ipst;
7493 
7494 	ipst = CONNQ_TO_IPST(q);
7495 
7496 	ASSERT(q->q_next == NULL);
7497 
7498 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7499 
7500 	/* Existence verified in ip_wput_nondata */
7501 	mp1 = mp->b_cont->b_cont;
7502 
7503 	/*
7504 	 * Must be (better be!) continuation of a TRANSPARENT
7505 	 * IOCTL.  We just copied in the lifsrcof structure.
7506 	 */
7507 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7508 	    (struct lifsrcof *)mp1->b_rptr);
7509 
7510 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7511 		return (EINVAL);
7512 
7513 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7514 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7515 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7516 	if (ipif == NULL) {
7517 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7518 		    ifindex));
7519 		return (ENXIO);
7520 	}
7521 
7522 	/* Allocate a buffer to hold requested information */
7523 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7524 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7525 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7526 	/* The actual size needed is always returned in lifs_len */
7527 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7528 
7529 	/* If the amount we need is more than what is passed in, abort */
7530 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7531 		ipif_refrele(ipif);
7532 		return (0);
7533 	}
7534 
7535 	mp1 = mi_copyout_alloc(q, mp,
7536 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7537 	if (mp1 == NULL) {
7538 		ipif_refrele(ipif);
7539 		return (ENOMEM);
7540 	}
7541 
7542 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7543 	bzero(mp1->b_rptr, lifs_bufsize);
7544 
7545 	lifr = (struct lifreq *)mp1->b_rptr;
7546 
7547 	ill = ill_head = ipif->ipif_ill;
7548 	orig_ipif = ipif;
7549 
7550 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7551 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7552 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7553 
7554 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7555 	for (; (ill != NULL) && (ill != ill_head);
7556 	    ill = ill->ill_usesrc_grp_next) {
7557 
7558 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7559 			break;
7560 
7561 		ipif = ill->ill_ipif;
7562 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7563 		if (ipif->ipif_isv6) {
7564 			sin6 = (sin6_t *)&lifr->lifr_addr;
7565 			*sin6 = sin6_null;
7566 			sin6->sin6_family = AF_INET6;
7567 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7568 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7569 			    &ipif->ipif_v6net_mask);
7570 		} else {
7571 			sin = (sin_t *)&lifr->lifr_addr;
7572 			*sin = sin_null;
7573 			sin->sin_family = AF_INET;
7574 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7575 			lifr->lifr_addrlen = ip_mask_to_plen(
7576 			    ipif->ipif_net_mask);
7577 		}
7578 		lifr++;
7579 	}
7580 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7581 	rw_exit(&ipst->ips_ill_g_lock);
7582 	ipif_refrele(orig_ipif);
7583 	mp1->b_wptr = (uchar_t *)lifr;
7584 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7585 
7586 	return (0);
7587 }
7588 
7589 /* ARGSUSED */
7590 int
7591 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7592     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7593 {
7594 	mblk_t *mp1;
7595 	int	list;
7596 	ill_t	*ill;
7597 	ipif_t	*ipif;
7598 	int	flags;
7599 	int	numlifs = 0;
7600 	size_t	lifc_bufsize;
7601 	struct	lifreq *lifr;
7602 	sa_family_t	family;
7603 	struct	sockaddr_in	*sin;
7604 	struct	sockaddr_in6	*sin6;
7605 	ill_walk_context_t	ctx;
7606 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7607 	int32_t	lifclen;
7608 	zoneid_t zoneid;
7609 	STRUCT_HANDLE(lifconf, lifc);
7610 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7611 
7612 	ip1dbg(("ip_sioctl_get_lifconf"));
7613 
7614 	ASSERT(q->q_next == NULL);
7615 
7616 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7617 
7618 	/* Existence verified in ip_wput_nondata */
7619 	mp1 = mp->b_cont->b_cont;
7620 
7621 	/*
7622 	 * An extended version of SIOCGIFCONF that takes an
7623 	 * additional address family and flags field.
7624 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7625 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7626 	 * interfaces are omitted.
7627 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7628 	 * unless LIFC_TEMPORARY is specified.
7629 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7630 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7631 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7632 	 * has priority over LIFC_NOXMIT.
7633 	 */
7634 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7635 
7636 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7637 		return (EINVAL);
7638 
7639 	/*
7640 	 * Must be (better be!) continuation of a TRANSPARENT
7641 	 * IOCTL.  We just copied in the lifconf structure.
7642 	 */
7643 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7644 
7645 	family = STRUCT_FGET(lifc, lifc_family);
7646 	flags = STRUCT_FGET(lifc, lifc_flags);
7647 
7648 	switch (family) {
7649 	case AF_UNSPEC:
7650 		/*
7651 		 * walk all ILL's.
7652 		 */
7653 		list = MAX_G_HEADS;
7654 		break;
7655 	case AF_INET:
7656 		/*
7657 		 * walk only IPV4 ILL's.
7658 		 */
7659 		list = IP_V4_G_HEAD;
7660 		break;
7661 	case AF_INET6:
7662 		/*
7663 		 * walk only IPV6 ILL's.
7664 		 */
7665 		list = IP_V6_G_HEAD;
7666 		break;
7667 	default:
7668 		return (EAFNOSUPPORT);
7669 	}
7670 
7671 	/*
7672 	 * Allocate a buffer to hold requested information.
7673 	 *
7674 	 * If lifc_len is larger than what is needed, we only
7675 	 * allocate what we will use.
7676 	 *
7677 	 * If lifc_len is smaller than what is needed, return
7678 	 * EINVAL.
7679 	 */
7680 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7681 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7682 	lifclen = STRUCT_FGET(lifc, lifc_len);
7683 	if (lifc_bufsize > lifclen) {
7684 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7685 			return (EINVAL);
7686 		else
7687 			lifc_bufsize = lifclen;
7688 	}
7689 
7690 	mp1 = mi_copyout_alloc(q, mp,
7691 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7692 	if (mp1 == NULL)
7693 		return (ENOMEM);
7694 
7695 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7696 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7697 
7698 	lifr = (struct lifreq *)mp1->b_rptr;
7699 
7700 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7701 	ill = ill_first(list, list, &ctx, ipst);
7702 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7703 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7704 			continue;
7705 
7706 		for (ipif = ill->ill_ipif; ipif != NULL;
7707 		    ipif = ipif->ipif_next) {
7708 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7709 			    !(flags & LIFC_NOXMIT))
7710 				continue;
7711 
7712 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7713 			    !(flags & LIFC_TEMPORARY))
7714 				continue;
7715 
7716 			if (((ipif->ipif_flags &
7717 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7718 			    IPIF_DEPRECATED)) ||
7719 			    IS_LOOPBACK(ill) ||
7720 			    !(ipif->ipif_flags & IPIF_UP)) &&
7721 			    (flags & LIFC_EXTERNAL_SOURCE))
7722 				continue;
7723 
7724 			if (zoneid != ipif->ipif_zoneid &&
7725 			    ipif->ipif_zoneid != ALL_ZONES &&
7726 			    (zoneid != GLOBAL_ZONEID ||
7727 			    !(flags & LIFC_ALLZONES)))
7728 				continue;
7729 
7730 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7731 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7732 					rw_exit(&ipst->ips_ill_g_lock);
7733 					return (EINVAL);
7734 				} else {
7735 					goto lif_copydone;
7736 				}
7737 			}
7738 
7739 			ipif_get_name(ipif, lifr->lifr_name,
7740 			    sizeof (lifr->lifr_name));
7741 			lifr->lifr_type = ill->ill_type;
7742 			if (ipif->ipif_isv6) {
7743 				sin6 = (sin6_t *)&lifr->lifr_addr;
7744 				*sin6 = sin6_null;
7745 				sin6->sin6_family = AF_INET6;
7746 				sin6->sin6_addr =
7747 				    ipif->ipif_v6lcl_addr;
7748 				lifr->lifr_addrlen =
7749 				    ip_mask_to_plen_v6(
7750 				    &ipif->ipif_v6net_mask);
7751 			} else {
7752 				sin = (sin_t *)&lifr->lifr_addr;
7753 				*sin = sin_null;
7754 				sin->sin_family = AF_INET;
7755 				sin->sin_addr.s_addr =
7756 				    ipif->ipif_lcl_addr;
7757 				lifr->lifr_addrlen =
7758 				    ip_mask_to_plen(
7759 				    ipif->ipif_net_mask);
7760 			}
7761 			lifr++;
7762 		}
7763 	}
7764 lif_copydone:
7765 	rw_exit(&ipst->ips_ill_g_lock);
7766 
7767 	mp1->b_wptr = (uchar_t *)lifr;
7768 	if (STRUCT_BUF(lifc) != NULL) {
7769 		STRUCT_FSET(lifc, lifc_len,
7770 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7771 	}
7772 	return (0);
7773 }
7774 
7775 static void
7776 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7777 {
7778 	ip6_asp_t *table;
7779 	size_t table_size;
7780 	mblk_t *data_mp;
7781 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7782 	ip_stack_t	*ipst;
7783 
7784 	if (q->q_next == NULL)
7785 		ipst = CONNQ_TO_IPST(q);
7786 	else
7787 		ipst = ILLQ_TO_IPST(q);
7788 
7789 	/* These two ioctls are I_STR only */
7790 	if (iocp->ioc_count == TRANSPARENT) {
7791 		miocnak(q, mp, 0, EINVAL);
7792 		return;
7793 	}
7794 
7795 	data_mp = mp->b_cont;
7796 	if (data_mp == NULL) {
7797 		/* The user passed us a NULL argument */
7798 		table = NULL;
7799 		table_size = iocp->ioc_count;
7800 	} else {
7801 		/*
7802 		 * The user provided a table.  The stream head
7803 		 * may have copied in the user data in chunks,
7804 		 * so make sure everything is pulled up
7805 		 * properly.
7806 		 */
7807 		if (MBLKL(data_mp) < iocp->ioc_count) {
7808 			mblk_t *new_data_mp;
7809 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7810 			    NULL) {
7811 				miocnak(q, mp, 0, ENOMEM);
7812 				return;
7813 			}
7814 			freemsg(data_mp);
7815 			data_mp = new_data_mp;
7816 			mp->b_cont = data_mp;
7817 		}
7818 		table = (ip6_asp_t *)data_mp->b_rptr;
7819 		table_size = iocp->ioc_count;
7820 	}
7821 
7822 	switch (iocp->ioc_cmd) {
7823 	case SIOCGIP6ADDRPOLICY:
7824 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7825 		if (iocp->ioc_rval == -1)
7826 			iocp->ioc_error = EINVAL;
7827 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7828 		else if (table != NULL &&
7829 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7830 			ip6_asp_t *src = table;
7831 			ip6_asp32_t *dst = (void *)table;
7832 			int count = table_size / sizeof (ip6_asp_t);
7833 			int i;
7834 
7835 			/*
7836 			 * We need to do an in-place shrink of the array
7837 			 * to match the alignment attributes of the
7838 			 * 32-bit ABI looking at it.
7839 			 */
7840 			/* LINTED: logical expression always true: op "||" */
7841 			ASSERT(sizeof (*src) > sizeof (*dst));
7842 			for (i = 1; i < count; i++)
7843 				bcopy(src + i, dst + i, sizeof (*dst));
7844 		}
7845 #endif
7846 		break;
7847 
7848 	case SIOCSIP6ADDRPOLICY:
7849 		ASSERT(mp->b_prev == NULL);
7850 		mp->b_prev = (void *)q;
7851 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7852 		/*
7853 		 * We pass in the datamodel here so that the ip6_asp_replace()
7854 		 * routine can handle converting from 32-bit to native formats
7855 		 * where necessary.
7856 		 *
7857 		 * A better way to handle this might be to convert the inbound
7858 		 * data structure here, and hang it off a new 'mp'; thus the
7859 		 * ip6_asp_replace() logic would always be dealing with native
7860 		 * format data structures..
7861 		 *
7862 		 * (An even simpler way to handle these ioctls is to just
7863 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7864 		 * and just recompile everything that depends on it.)
7865 		 */
7866 #endif
7867 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7868 		    iocp->ioc_flag & IOC_MODELS);
7869 		return;
7870 	}
7871 
7872 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7873 	qreply(q, mp);
7874 }
7875 
7876 static void
7877 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7878 {
7879 	mblk_t 		*data_mp;
7880 	struct dstinforeq	*dir;
7881 	uint8_t		*end, *cur;
7882 	in6_addr_t	*daddr, *saddr;
7883 	ipaddr_t	v4daddr;
7884 	ire_t		*ire;
7885 	ipaddr_t	v4setsrc;
7886 	in6_addr_t	v6setsrc;
7887 	char		*slabel, *dlabel;
7888 	boolean_t	isipv4;
7889 	int		match_ire;
7890 	ill_t		*dst_ill;
7891 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7892 	conn_t		*connp = Q_TO_CONN(q);
7893 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7894 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7895 	uint64_t	ipif_flags;
7896 
7897 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7898 
7899 	/*
7900 	 * This ioctl is I_STR only, and must have a
7901 	 * data mblk following the M_IOCTL mblk.
7902 	 */
7903 	data_mp = mp->b_cont;
7904 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7905 		miocnak(q, mp, 0, EINVAL);
7906 		return;
7907 	}
7908 
7909 	if (MBLKL(data_mp) < iocp->ioc_count) {
7910 		mblk_t *new_data_mp;
7911 
7912 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7913 			miocnak(q, mp, 0, ENOMEM);
7914 			return;
7915 		}
7916 		freemsg(data_mp);
7917 		data_mp = new_data_mp;
7918 		mp->b_cont = data_mp;
7919 	}
7920 	match_ire = MATCH_IRE_DSTONLY;
7921 
7922 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7923 	    end - cur >= sizeof (struct dstinforeq);
7924 	    cur += sizeof (struct dstinforeq)) {
7925 		dir = (struct dstinforeq *)cur;
7926 		daddr = &dir->dir_daddr;
7927 		saddr = &dir->dir_saddr;
7928 
7929 		/*
7930 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7931 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7932 		 * and ip_select_source_v6() do not.
7933 		 */
7934 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7935 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7936 
7937 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7938 		if (isipv4) {
7939 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7940 			v4setsrc = INADDR_ANY;
7941 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7942 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7943 			    NULL, NULL);
7944 		} else {
7945 			v6setsrc = ipv6_all_zeros;
7946 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7947 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7948 			    NULL, NULL);
7949 		}
7950 		ASSERT(ire != NULL);
7951 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7952 			ire_refrele(ire);
7953 			dir->dir_dreachable = 0;
7954 
7955 			/* move on to next dst addr */
7956 			continue;
7957 		}
7958 		dir->dir_dreachable = 1;
7959 
7960 		dst_ill = ire_nexthop_ill(ire);
7961 		if (dst_ill == NULL) {
7962 			ire_refrele(ire);
7963 			continue;
7964 		}
7965 
7966 		/* With ipmp we most likely look at the ipmp ill here */
7967 		dir->dir_dmactype = dst_ill->ill_mactype;
7968 
7969 		if (isipv4) {
7970 			ipaddr_t v4saddr;
7971 
7972 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7973 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7974 			    &v4saddr, NULL, &ipif_flags) != 0) {
7975 				v4saddr = INADDR_ANY;
7976 				ipif_flags = 0;
7977 			}
7978 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7979 		} else {
7980 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7981 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7982 			    saddr, NULL, &ipif_flags) != 0) {
7983 				*saddr = ipv6_all_zeros;
7984 				ipif_flags = 0;
7985 			}
7986 		}
7987 
7988 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7989 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7990 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7991 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7992 		ire_refrele(ire);
7993 		ill_refrele(dst_ill);
7994 	}
7995 	miocack(q, mp, iocp->ioc_count, 0);
7996 }
7997 
7998 /*
7999  * Check if this is an address assigned to this machine.
8000  * Skips interfaces that are down by using ire checks.
8001  * Translates mapped addresses to v4 addresses and then
8002  * treats them as such, returning true if the v4 address
8003  * associated with this mapped address is configured.
8004  * Note: Applications will have to be careful what they do
8005  * with the response; use of mapped addresses limits
8006  * what can be done with the socket, especially with
8007  * respect to socket options and ioctls - neither IPv4
8008  * options nor IPv6 sticky options/ancillary data options
8009  * may be used.
8010  */
8011 /* ARGSUSED */
8012 int
8013 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8014     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8015 {
8016 	struct sioc_addrreq *sia;
8017 	sin_t *sin;
8018 	ire_t *ire;
8019 	mblk_t *mp1;
8020 	zoneid_t zoneid;
8021 	ip_stack_t	*ipst;
8022 
8023 	ip1dbg(("ip_sioctl_tmyaddr"));
8024 
8025 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8026 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8027 	ipst = CONNQ_TO_IPST(q);
8028 
8029 	/* Existence verified in ip_wput_nondata */
8030 	mp1 = mp->b_cont->b_cont;
8031 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8032 	sin = (sin_t *)&sia->sa_addr;
8033 	switch (sin->sin_family) {
8034 	case AF_INET6: {
8035 		sin6_t *sin6 = (sin6_t *)sin;
8036 
8037 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8038 			ipaddr_t v4_addr;
8039 
8040 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8041 			    v4_addr);
8042 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8043 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8044 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8045 		} else {
8046 			in6_addr_t v6addr;
8047 
8048 			v6addr = sin6->sin6_addr;
8049 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8050 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8051 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8052 		}
8053 		break;
8054 	}
8055 	case AF_INET: {
8056 		ipaddr_t v4addr;
8057 
8058 		v4addr = sin->sin_addr.s_addr;
8059 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8060 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8061 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8062 		break;
8063 	}
8064 	default:
8065 		return (EAFNOSUPPORT);
8066 	}
8067 	if (ire != NULL) {
8068 		sia->sa_res = 1;
8069 		ire_refrele(ire);
8070 	} else {
8071 		sia->sa_res = 0;
8072 	}
8073 	return (0);
8074 }
8075 
8076 /*
8077  * Check if this is an address assigned on-link i.e. neighbor,
8078  * and makes sure it's reachable from the current zone.
8079  * Returns true for my addresses as well.
8080  * Translates mapped addresses to v4 addresses and then
8081  * treats them as such, returning true if the v4 address
8082  * associated with this mapped address is configured.
8083  * Note: Applications will have to be careful what they do
8084  * with the response; use of mapped addresses limits
8085  * what can be done with the socket, especially with
8086  * respect to socket options and ioctls - neither IPv4
8087  * options nor IPv6 sticky options/ancillary data options
8088  * may be used.
8089  */
8090 /* ARGSUSED */
8091 int
8092 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8093     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8094 {
8095 	struct sioc_addrreq *sia;
8096 	sin_t *sin;
8097 	mblk_t	*mp1;
8098 	ire_t *ire = NULL;
8099 	zoneid_t zoneid;
8100 	ip_stack_t	*ipst;
8101 
8102 	ip1dbg(("ip_sioctl_tonlink"));
8103 
8104 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8105 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8106 	ipst = CONNQ_TO_IPST(q);
8107 
8108 	/* Existence verified in ip_wput_nondata */
8109 	mp1 = mp->b_cont->b_cont;
8110 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8111 	sin = (sin_t *)&sia->sa_addr;
8112 
8113 	/*
8114 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8115 	 * to make sure we only look at on-link unicast address.
8116 	 */
8117 	switch (sin->sin_family) {
8118 	case AF_INET6: {
8119 		sin6_t *sin6 = (sin6_t *)sin;
8120 
8121 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8122 			ipaddr_t v4_addr;
8123 
8124 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8125 			    v4_addr);
8126 			if (!CLASSD(v4_addr)) {
8127 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8128 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8129 				    0, ipst, NULL);
8130 			}
8131 		} else {
8132 			in6_addr_t v6addr;
8133 
8134 			v6addr = sin6->sin6_addr;
8135 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8136 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8137 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8138 				    ipst, NULL);
8139 			}
8140 		}
8141 		break;
8142 	}
8143 	case AF_INET: {
8144 		ipaddr_t v4addr;
8145 
8146 		v4addr = sin->sin_addr.s_addr;
8147 		if (!CLASSD(v4addr)) {
8148 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8149 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8150 		}
8151 		break;
8152 	}
8153 	default:
8154 		return (EAFNOSUPPORT);
8155 	}
8156 	sia->sa_res = 0;
8157 	if (ire != NULL) {
8158 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8159 
8160 		if ((ire->ire_type & IRE_ONLINK) &&
8161 		    !(ire->ire_type & IRE_BROADCAST))
8162 			sia->sa_res = 1;
8163 		ire_refrele(ire);
8164 	}
8165 	return (0);
8166 }
8167 
8168 /*
8169  * TBD: implement when kernel maintaines a list of site prefixes.
8170  */
8171 /* ARGSUSED */
8172 int
8173 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8174     ip_ioctl_cmd_t *ipip, void *ifreq)
8175 {
8176 	return (ENXIO);
8177 }
8178 
8179 /* ARP IOCTLs. */
8180 /* ARGSUSED */
8181 int
8182 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8183     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8184 {
8185 	int		err;
8186 	ipaddr_t	ipaddr;
8187 	struct iocblk	*iocp;
8188 	conn_t		*connp;
8189 	struct arpreq	*ar;
8190 	struct xarpreq	*xar;
8191 	int		arp_flags, flags, alength;
8192 	uchar_t		*lladdr;
8193 	ip_stack_t	*ipst;
8194 	ill_t		*ill = ipif->ipif_ill;
8195 	ill_t		*proxy_ill = NULL;
8196 	ipmp_arpent_t	*entp = NULL;
8197 	boolean_t	proxyarp = B_FALSE;
8198 	boolean_t	if_arp_ioctl = B_FALSE;
8199 	ncec_t		*ncec = NULL;
8200 	nce_t		*nce;
8201 
8202 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8203 	connp = Q_TO_CONN(q);
8204 	ipst = connp->conn_netstack->netstack_ip;
8205 	iocp = (struct iocblk *)mp->b_rptr;
8206 
8207 	if (ipip->ipi_cmd_type == XARP_CMD) {
8208 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8209 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8210 		ar = NULL;
8211 
8212 		arp_flags = xar->xarp_flags;
8213 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8214 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8215 		/*
8216 		 * Validate against user's link layer address length
8217 		 * input and name and addr length limits.
8218 		 */
8219 		alength = ill->ill_phys_addr_length;
8220 		if (ipip->ipi_cmd == SIOCSXARP) {
8221 			if (alength != xar->xarp_ha.sdl_alen ||
8222 			    (alength + xar->xarp_ha.sdl_nlen >
8223 			    sizeof (xar->xarp_ha.sdl_data)))
8224 				return (EINVAL);
8225 		}
8226 	} else {
8227 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8228 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8229 		xar = NULL;
8230 
8231 		arp_flags = ar->arp_flags;
8232 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8233 		/*
8234 		 * Theoretically, the sa_family could tell us what link
8235 		 * layer type this operation is trying to deal with. By
8236 		 * common usage AF_UNSPEC means ethernet. We'll assume
8237 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8238 		 * for now. Our new SIOC*XARP ioctls can be used more
8239 		 * generally.
8240 		 *
8241 		 * If the underlying media happens to have a non 6 byte
8242 		 * address, arp module will fail set/get, but the del
8243 		 * operation will succeed.
8244 		 */
8245 		alength = 6;
8246 		if ((ipip->ipi_cmd != SIOCDARP) &&
8247 		    (alength != ill->ill_phys_addr_length)) {
8248 			return (EINVAL);
8249 		}
8250 	}
8251 
8252 	/* Translate ATF* flags to NCE* flags */
8253 	flags = 0;
8254 	if (arp_flags & ATF_AUTHORITY)
8255 		flags |= NCE_F_AUTHORITY;
8256 	if (arp_flags & ATF_PERM)
8257 		flags |= NCE_F_NONUD; /* not subject to aging */
8258 	if (arp_flags & ATF_PUBL)
8259 		flags |= NCE_F_PUBLISH;
8260 
8261 	/*
8262 	 * IPMP ARP special handling:
8263 	 *
8264 	 * 1. Since ARP mappings must appear consistent across the group,
8265 	 *    prohibit changing ARP mappings on the underlying interfaces.
8266 	 *
8267 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8268 	 *    IP itself, prohibit changing them.
8269 	 *
8270 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8271 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8272 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8273 	 */
8274 	if (IS_UNDER_IPMP(ill)) {
8275 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8276 			return (EPERM);
8277 	}
8278 	if (IS_IPMP(ill)) {
8279 		ipmp_illgrp_t *illg = ill->ill_grp;
8280 
8281 		switch (ipip->ipi_cmd) {
8282 		case SIOCSARP:
8283 		case SIOCSXARP:
8284 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8285 			if (proxy_ill != NULL) {
8286 				proxyarp = B_TRUE;
8287 				if (!ipmp_ill_is_active(proxy_ill))
8288 					proxy_ill = ipmp_illgrp_next_ill(illg);
8289 				if (proxy_ill != NULL)
8290 					lladdr = proxy_ill->ill_phys_addr;
8291 			}
8292 			/* FALLTHRU */
8293 		}
8294 	}
8295 
8296 	ipaddr = sin->sin_addr.s_addr;
8297 	/*
8298 	 * don't match across illgrp per case (1) and (2).
8299 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8300 	 */
8301 	nce = nce_lookup_v4(ill, &ipaddr);
8302 	if (nce != NULL)
8303 		ncec = nce->nce_common;
8304 
8305 	switch (iocp->ioc_cmd) {
8306 	case SIOCDARP:
8307 	case SIOCDXARP: {
8308 		/*
8309 		 * Delete the NCE if any.
8310 		 */
8311 		if (ncec == NULL) {
8312 			iocp->ioc_error = ENXIO;
8313 			break;
8314 		}
8315 		/* Don't allow changes to arp mappings of local addresses. */
8316 		if (NCE_MYADDR(ncec)) {
8317 			nce_refrele(nce);
8318 			return (ENOTSUP);
8319 		}
8320 		iocp->ioc_error = 0;
8321 
8322 		/*
8323 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8324 		 * This will delete all the nce entries on the under_ills.
8325 		 */
8326 		ncec_delete(ncec);
8327 		/*
8328 		 * Once the NCE has been deleted, then the ire_dep* consistency
8329 		 * mechanism will find any IRE which depended on the now
8330 		 * condemned NCE (as part of sending packets).
8331 		 * That mechanism handles redirects by deleting redirects
8332 		 * that refer to UNREACHABLE nces.
8333 		 */
8334 		break;
8335 	}
8336 	case SIOCGARP:
8337 	case SIOCGXARP:
8338 		if (ncec != NULL) {
8339 			lladdr = ncec->ncec_lladdr;
8340 			flags = ncec->ncec_flags;
8341 			iocp->ioc_error = 0;
8342 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8343 		} else {
8344 			iocp->ioc_error = ENXIO;
8345 		}
8346 		break;
8347 	case SIOCSARP:
8348 	case SIOCSXARP:
8349 		/* Don't allow changes to arp mappings of local addresses. */
8350 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8351 			nce_refrele(nce);
8352 			return (ENOTSUP);
8353 		}
8354 
8355 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8356 		flags |= NCE_F_STATIC;
8357 		if (!if_arp_ioctl) {
8358 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8359 			    lladdr, alength, flags);
8360 		} else {
8361 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8362 			if (ipif != NULL) {
8363 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8364 				    lladdr, alength, flags);
8365 				ipif_refrele(ipif);
8366 			}
8367 		}
8368 		if (nce != NULL) {
8369 			nce_refrele(nce);
8370 			nce = NULL;
8371 		}
8372 		/*
8373 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8374 		 * by nce_add_common()
8375 		 */
8376 		err = nce_lookup_then_add_v4(ill, lladdr,
8377 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8378 		    &nce);
8379 		if (err == EEXIST) {
8380 			ncec = nce->nce_common;
8381 			mutex_enter(&ncec->ncec_lock);
8382 			ncec->ncec_state = ND_REACHABLE;
8383 			ncec->ncec_flags = flags;
8384 			nce_update(ncec, ND_UNCHANGED, lladdr);
8385 			mutex_exit(&ncec->ncec_lock);
8386 			err = 0;
8387 		}
8388 		if (nce != NULL) {
8389 			nce_refrele(nce);
8390 			nce = NULL;
8391 		}
8392 		if (IS_IPMP(ill) && err == 0) {
8393 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8394 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8395 			    flags);
8396 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8397 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8398 				break;
8399 			}
8400 		}
8401 		iocp->ioc_error = err;
8402 	}
8403 
8404 	if (nce != NULL) {
8405 		nce_refrele(nce);
8406 	}
8407 
8408 	/*
8409 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8410 	 */
8411 	if (entp != NULL)
8412 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8413 
8414 	return (iocp->ioc_error);
8415 }
8416 
8417 /*
8418  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8419  * the associated sin and refhold and return the associated ipif via `ci'.
8420  */
8421 int
8422 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8423     cmd_info_t *ci)
8424 {
8425 	mblk_t	*mp1;
8426 	sin_t	*sin;
8427 	conn_t	*connp;
8428 	ipif_t	*ipif;
8429 	ire_t	*ire = NULL;
8430 	ill_t	*ill = NULL;
8431 	boolean_t exists;
8432 	ip_stack_t *ipst;
8433 	struct arpreq *ar;
8434 	struct xarpreq *xar;
8435 	struct sockaddr_dl *sdl;
8436 
8437 	/* ioctl comes down on a conn */
8438 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8439 	connp = Q_TO_CONN(q);
8440 	if (connp->conn_family == AF_INET6)
8441 		return (ENXIO);
8442 
8443 	ipst = connp->conn_netstack->netstack_ip;
8444 
8445 	/* Verified in ip_wput_nondata */
8446 	mp1 = mp->b_cont->b_cont;
8447 
8448 	if (ipip->ipi_cmd_type == XARP_CMD) {
8449 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8450 		xar = (struct xarpreq *)mp1->b_rptr;
8451 		sin = (sin_t *)&xar->xarp_pa;
8452 		sdl = &xar->xarp_ha;
8453 
8454 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8455 			return (ENXIO);
8456 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8457 			return (EINVAL);
8458 	} else {
8459 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8460 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8461 		ar = (struct arpreq *)mp1->b_rptr;
8462 		sin = (sin_t *)&ar->arp_pa;
8463 	}
8464 
8465 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8466 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8467 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8468 		if (ipif == NULL)
8469 			return (ENXIO);
8470 		if (ipif->ipif_id != 0) {
8471 			ipif_refrele(ipif);
8472 			return (ENXIO);
8473 		}
8474 	} else {
8475 		/*
8476 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8477 		 * of 0: use the IP address to find the ipif.  If the IP
8478 		 * address is an IPMP test address, ire_ftable_lookup() will
8479 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8480 		 */
8481 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8482 		    ipst);
8483 		if (ipif == NULL) {
8484 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8485 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8486 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8487 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8488 				if (ire != NULL)
8489 					ire_refrele(ire);
8490 				return (ENXIO);
8491 			}
8492 			ASSERT(ire != NULL && ill != NULL);
8493 			ipif = ill->ill_ipif;
8494 			ipif_refhold(ipif);
8495 			ire_refrele(ire);
8496 		}
8497 	}
8498 
8499 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8500 		ipif_refrele(ipif);
8501 		return (ENXIO);
8502 	}
8503 
8504 	ci->ci_sin = sin;
8505 	ci->ci_ipif = ipif;
8506 	return (0);
8507 }
8508 
8509 /*
8510  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8511  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8512  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8513  * up and thus an ill can join that illgrp.
8514  *
8515  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8516  * open()/close() primarily because close() is not allowed to fail or block
8517  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8518  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8519  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8520  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8521  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8522  * state if I_UNLINK didn't occur.
8523  *
8524  * Note that for each plumb/unplumb operation, we may end up here more than
8525  * once because of the way ifconfig works.  However, it's OK to link the same
8526  * illgrp more than once, or unlink an illgrp that's already unlinked.
8527  */
8528 static int
8529 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8530 {
8531 	int err;
8532 	ip_stack_t *ipst = ill->ill_ipst;
8533 
8534 	ASSERT(IS_IPMP(ill));
8535 	ASSERT(IAM_WRITER_ILL(ill));
8536 
8537 	switch (ioccmd) {
8538 	case I_LINK:
8539 		return (ENOTSUP);
8540 
8541 	case I_PLINK:
8542 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8543 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8544 		rw_exit(&ipst->ips_ipmp_lock);
8545 		break;
8546 
8547 	case I_PUNLINK:
8548 		/*
8549 		 * Require all UP ipifs be brought down prior to unlinking the
8550 		 * illgrp so any associated IREs (and other state) is torched.
8551 		 */
8552 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8553 			return (EBUSY);
8554 
8555 		/*
8556 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8557 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8558 		 * join this group.  Specifically: ills trying to join grab
8559 		 * ipmp_lock and bump a "pending join" counter checked by
8560 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8561 		 * joins can occur (since we have ipmp_lock).  Once we drop
8562 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8563 		 * find the illgrp (since we unlinked it) and will return
8564 		 * EAFNOSUPPORT.  This will then take them back through the
8565 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8566 		 * back through I_PLINK above.
8567 		 */
8568 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8569 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8570 		rw_exit(&ipst->ips_ipmp_lock);
8571 		return (err);
8572 	default:
8573 		break;
8574 	}
8575 	return (0);
8576 }
8577 
8578 /*
8579  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8580  * atomically set/clear the muxids. Also complete the ioctl by acking or
8581  * naking it.  Note that the code is structured such that the link type,
8582  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8583  * its clones use the persistent link, while pppd(1M) and perhaps many
8584  * other daemons may use non-persistent link.  When combined with some
8585  * ill_t states, linking and unlinking lower streams may be used as
8586  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8587  */
8588 /* ARGSUSED */
8589 void
8590 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8591 {
8592 	mblk_t		*mp1;
8593 	struct linkblk	*li;
8594 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8595 	int		err = 0;
8596 
8597 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8598 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8599 
8600 	mp1 = mp->b_cont;	/* This is the linkblk info */
8601 	li = (struct linkblk *)mp1->b_rptr;
8602 
8603 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8604 	if (err == EINPROGRESS)
8605 		return;
8606 done:
8607 	if (err == 0)
8608 		miocack(q, mp, 0, 0);
8609 	else
8610 		miocnak(q, mp, 0, err);
8611 
8612 	/* Conn was refheld in ip_sioctl_copyin_setup */
8613 	if (CONN_Q(q))
8614 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8615 }
8616 
8617 /*
8618  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8619  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8620  * module stream).  If `doconsist' is set, then do the extended consistency
8621  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8622  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8623  * an error code on failure.
8624  */
8625 static int
8626 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8627     struct linkblk *li)
8628 {
8629 	int		err = 0;
8630 	ill_t  		*ill;
8631 	queue_t		*ipwq, *dwq;
8632 	const char	*name;
8633 	struct qinit	*qinfo;
8634 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8635 	boolean_t	entered_ipsq = B_FALSE;
8636 	boolean_t	is_ip = B_FALSE;
8637 	arl_t		*arl;
8638 
8639 	/*
8640 	 * Walk the lower stream to verify it's the IP module stream.
8641 	 * The IP module is identified by its name, wput function,
8642 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8643 	 * (li->l_qbot) will not vanish until this ioctl completes.
8644 	 */
8645 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8646 		qinfo = ipwq->q_qinfo;
8647 		name = qinfo->qi_minfo->mi_idname;
8648 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8649 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8650 			is_ip = B_TRUE;
8651 			break;
8652 		}
8653 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8654 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8655 			break;
8656 		}
8657 	}
8658 
8659 	/*
8660 	 * If this isn't an IP module stream, bail.
8661 	 */
8662 	if (ipwq == NULL)
8663 		return (0);
8664 
8665 	if (!is_ip) {
8666 		arl = (arl_t *)ipwq->q_ptr;
8667 		ill = arl_to_ill(arl);
8668 		if (ill == NULL)
8669 			return (0);
8670 	} else {
8671 		ill = ipwq->q_ptr;
8672 	}
8673 	ASSERT(ill != NULL);
8674 
8675 	if (ipsq == NULL) {
8676 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8677 		    NEW_OP, B_FALSE);
8678 		if (ipsq == NULL) {
8679 			if (!is_ip)
8680 				ill_refrele(ill);
8681 			return (EINPROGRESS);
8682 		}
8683 		entered_ipsq = B_TRUE;
8684 	}
8685 	ASSERT(IAM_WRITER_ILL(ill));
8686 	mutex_enter(&ill->ill_lock);
8687 	if (!is_ip) {
8688 		if (islink && ill->ill_muxid == 0) {
8689 			/*
8690 			 * Plumbing has to be done with IP plumbed first, arp
8691 			 * second, but here we have arp being plumbed first.
8692 			 */
8693 			mutex_exit(&ill->ill_lock);
8694 			ipsq_exit(ipsq);
8695 			ill_refrele(ill);
8696 			return (EINVAL);
8697 		}
8698 	}
8699 	mutex_exit(&ill->ill_lock);
8700 	if (!is_ip) {
8701 		arl->arl_muxid = islink ? li->l_index : 0;
8702 		ill_refrele(ill);
8703 		goto done;
8704 	}
8705 
8706 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8707 		goto done;
8708 
8709 	/*
8710 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8711 	 * the read queue of the module immediately below IP in the ill.
8712 	 * These are used during the capability negotiation below.
8713 	 */
8714 	ill->ill_lmod_rq = NULL;
8715 	ill->ill_lmod_cnt = 0;
8716 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8717 		ill->ill_lmod_rq = RD(dwq);
8718 		for (; dwq != NULL; dwq = dwq->q_next)
8719 			ill->ill_lmod_cnt++;
8720 	}
8721 
8722 	ill->ill_muxid = islink ? li->l_index : 0;
8723 
8724 	/*
8725 	 * Mark the ipsq busy until the capability operations initiated below
8726 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8727 	 * returns, but the capability operation may complete asynchronously
8728 	 * much later.
8729 	 */
8730 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8731 	/*
8732 	 * If there's at least one up ipif on this ill, then we're bound to
8733 	 * the underlying driver via DLPI.  In that case, renegotiate
8734 	 * capabilities to account for any possible change in modules
8735 	 * interposed between IP and the driver.
8736 	 */
8737 	if (ill->ill_ipif_up_count > 0) {
8738 		if (islink)
8739 			ill_capability_probe(ill);
8740 		else
8741 			ill_capability_reset(ill, B_FALSE);
8742 	}
8743 	ipsq_current_finish(ipsq);
8744 done:
8745 	if (entered_ipsq)
8746 		ipsq_exit(ipsq);
8747 
8748 	return (err);
8749 }
8750 
8751 /*
8752  * Search the ioctl command in the ioctl tables and return a pointer
8753  * to the ioctl command information. The ioctl command tables are
8754  * static and fully populated at compile time.
8755  */
8756 ip_ioctl_cmd_t *
8757 ip_sioctl_lookup(int ioc_cmd)
8758 {
8759 	int index;
8760 	ip_ioctl_cmd_t *ipip;
8761 	ip_ioctl_cmd_t *ipip_end;
8762 
8763 	if (ioc_cmd == IPI_DONTCARE)
8764 		return (NULL);
8765 
8766 	/*
8767 	 * Do a 2 step search. First search the indexed table
8768 	 * based on the least significant byte of the ioctl cmd.
8769 	 * If we don't find a match, then search the misc table
8770 	 * serially.
8771 	 */
8772 	index = ioc_cmd & 0xFF;
8773 	if (index < ip_ndx_ioctl_count) {
8774 		ipip = &ip_ndx_ioctl_table[index];
8775 		if (ipip->ipi_cmd == ioc_cmd) {
8776 			/* Found a match in the ndx table */
8777 			return (ipip);
8778 		}
8779 	}
8780 
8781 	/* Search the misc table */
8782 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8783 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8784 		if (ipip->ipi_cmd == ioc_cmd)
8785 			/* Found a match in the misc table */
8786 			return (ipip);
8787 	}
8788 
8789 	return (NULL);
8790 }
8791 
8792 /*
8793  * Wrapper function for resuming deferred ioctl processing
8794  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8795  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8796  */
8797 /* ARGSUSED */
8798 void
8799 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8800     void *dummy_arg)
8801 {
8802 	ip_sioctl_copyin_setup(q, mp);
8803 }
8804 
8805 /*
8806  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8807  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8808  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8809  * We establish here the size of the block to be copied in.  mi_copyin
8810  * arranges for this to happen, an processing continues in ip_wput_nondata with
8811  * an M_IOCDATA message.
8812  */
8813 void
8814 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8815 {
8816 	int	copyin_size;
8817 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8818 	ip_ioctl_cmd_t *ipip;
8819 	cred_t *cr;
8820 	ip_stack_t	*ipst;
8821 
8822 	if (CONN_Q(q))
8823 		ipst = CONNQ_TO_IPST(q);
8824 	else
8825 		ipst = ILLQ_TO_IPST(q);
8826 
8827 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8828 	if (ipip == NULL) {
8829 		/*
8830 		 * The ioctl is not one we understand or own.
8831 		 * Pass it along to be processed down stream,
8832 		 * if this is a module instance of IP, else nak
8833 		 * the ioctl.
8834 		 */
8835 		if (q->q_next == NULL) {
8836 			goto nak;
8837 		} else {
8838 			putnext(q, mp);
8839 			return;
8840 		}
8841 	}
8842 
8843 	/*
8844 	 * If this is deferred, then we will do all the checks when we
8845 	 * come back.
8846 	 */
8847 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8848 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8849 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8850 		return;
8851 	}
8852 
8853 	/*
8854 	 * Only allow a very small subset of IP ioctls on this stream if
8855 	 * IP is a module and not a driver. Allowing ioctls to be processed
8856 	 * in this case may cause assert failures or data corruption.
8857 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8858 	 * ioctls allowed on an IP module stream, after which this stream
8859 	 * normally becomes a multiplexor (at which time the stream head
8860 	 * will fail all ioctls).
8861 	 */
8862 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8863 		goto nak;
8864 	}
8865 
8866 	/* Make sure we have ioctl data to process. */
8867 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8868 		goto nak;
8869 
8870 	/*
8871 	 * Prefer dblk credential over ioctl credential; some synthesized
8872 	 * ioctls have kcred set because there's no way to crhold()
8873 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8874 	 * the framework; the caller of ioctl needs to hold the reference
8875 	 * for the duration of the call).
8876 	 */
8877 	cr = msg_getcred(mp, NULL);
8878 	if (cr == NULL)
8879 		cr = iocp->ioc_cr;
8880 
8881 	/* Make sure normal users don't send down privileged ioctls */
8882 	if ((ipip->ipi_flags & IPI_PRIV) &&
8883 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8884 		/* We checked the privilege earlier but log it here */
8885 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8886 		return;
8887 	}
8888 
8889 	/*
8890 	 * The ioctl command tables can only encode fixed length
8891 	 * ioctl data. If the length is variable, the table will
8892 	 * encode the length as zero. Such special cases are handled
8893 	 * below in the switch.
8894 	 */
8895 	if (ipip->ipi_copyin_size != 0) {
8896 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8897 		return;
8898 	}
8899 
8900 	switch (iocp->ioc_cmd) {
8901 	case O_SIOCGIFCONF:
8902 	case SIOCGIFCONF:
8903 		/*
8904 		 * This IOCTL is hilarious.  See comments in
8905 		 * ip_sioctl_get_ifconf for the story.
8906 		 */
8907 		if (iocp->ioc_count == TRANSPARENT)
8908 			copyin_size = SIZEOF_STRUCT(ifconf,
8909 			    iocp->ioc_flag);
8910 		else
8911 			copyin_size = iocp->ioc_count;
8912 		mi_copyin(q, mp, NULL, copyin_size);
8913 		return;
8914 
8915 	case O_SIOCGLIFCONF:
8916 	case SIOCGLIFCONF:
8917 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8918 		mi_copyin(q, mp, NULL, copyin_size);
8919 		return;
8920 
8921 	case SIOCGLIFSRCOF:
8922 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8923 		mi_copyin(q, mp, NULL, copyin_size);
8924 		return;
8925 	case SIOCGIP6ADDRPOLICY:
8926 		ip_sioctl_ip6addrpolicy(q, mp);
8927 		ip6_asp_table_refrele(ipst);
8928 		return;
8929 
8930 	case SIOCSIP6ADDRPOLICY:
8931 		ip_sioctl_ip6addrpolicy(q, mp);
8932 		return;
8933 
8934 	case SIOCGDSTINFO:
8935 		ip_sioctl_dstinfo(q, mp);
8936 		ip6_asp_table_refrele(ipst);
8937 		return;
8938 
8939 	case I_PLINK:
8940 	case I_PUNLINK:
8941 	case I_LINK:
8942 	case I_UNLINK:
8943 		/*
8944 		 * We treat non-persistent link similarly as the persistent
8945 		 * link case, in terms of plumbing/unplumbing, as well as
8946 		 * dynamic re-plumbing events indicator.  See comments
8947 		 * in ip_sioctl_plink() for more.
8948 		 *
8949 		 * Request can be enqueued in the 'ipsq' while waiting
8950 		 * to become exclusive. So bump up the conn ref.
8951 		 */
8952 		if (CONN_Q(q))
8953 			CONN_INC_REF(Q_TO_CONN(q));
8954 		ip_sioctl_plink(NULL, q, mp, NULL);
8955 		return;
8956 
8957 	case ND_GET:
8958 	case ND_SET:
8959 		/*
8960 		 * Use of the nd table requires holding the reader lock.
8961 		 * Modifying the nd table thru nd_load/nd_unload requires
8962 		 * the writer lock.
8963 		 */
8964 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8965 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8966 			rw_exit(&ipst->ips_ip_g_nd_lock);
8967 
8968 			if (iocp->ioc_error)
8969 				iocp->ioc_count = 0;
8970 			mp->b_datap->db_type = M_IOCACK;
8971 			qreply(q, mp);
8972 			return;
8973 		}
8974 		rw_exit(&ipst->ips_ip_g_nd_lock);
8975 		/*
8976 		 * We don't understand this subioctl of ND_GET / ND_SET.
8977 		 * Maybe intended for some driver / module below us
8978 		 */
8979 		if (q->q_next) {
8980 			putnext(q, mp);
8981 		} else {
8982 			iocp->ioc_error = ENOENT;
8983 			mp->b_datap->db_type = M_IOCNAK;
8984 			iocp->ioc_count = 0;
8985 			qreply(q, mp);
8986 		}
8987 		return;
8988 
8989 	case IP_IOCTL:
8990 		ip_wput_ioctl(q, mp);
8991 		return;
8992 
8993 	case SIOCILB:
8994 		/* The ioctl length varies depending on the ILB command. */
8995 		copyin_size = iocp->ioc_count;
8996 		if (copyin_size < sizeof (ilb_cmd_t))
8997 			goto nak;
8998 		mi_copyin(q, mp, NULL, copyin_size);
8999 		return;
9000 
9001 	default:
9002 		cmn_err(CE_PANIC, "should not happen ");
9003 	}
9004 nak:
9005 	if (mp->b_cont != NULL) {
9006 		freemsg(mp->b_cont);
9007 		mp->b_cont = NULL;
9008 	}
9009 	iocp->ioc_error = EINVAL;
9010 	mp->b_datap->db_type = M_IOCNAK;
9011 	iocp->ioc_count = 0;
9012 	qreply(q, mp);
9013 }
9014 
9015 static void
9016 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9017 {
9018 	struct arpreq *ar;
9019 	struct xarpreq *xar;
9020 	mblk_t	*tmp;
9021 	struct iocblk *iocp;
9022 	int x_arp_ioctl = B_FALSE;
9023 	int *flagsp;
9024 	char *storage = NULL;
9025 
9026 	ASSERT(ill != NULL);
9027 
9028 	iocp = (struct iocblk *)mp->b_rptr;
9029 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9030 
9031 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9032 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9033 	    (iocp->ioc_cmd == SIOCSXARP)) {
9034 		x_arp_ioctl = B_TRUE;
9035 		xar = (struct xarpreq *)tmp->b_rptr;
9036 		flagsp = &xar->xarp_flags;
9037 		storage = xar->xarp_ha.sdl_data;
9038 	} else {
9039 		ar = (struct arpreq *)tmp->b_rptr;
9040 		flagsp = &ar->arp_flags;
9041 		storage = ar->arp_ha.sa_data;
9042 	}
9043 
9044 	/*
9045 	 * We're done if this is not an SIOCG{X}ARP
9046 	 */
9047 	if (x_arp_ioctl) {
9048 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9049 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9050 		    sizeof (xar->xarp_ha.sdl_data)) {
9051 			iocp->ioc_error = EINVAL;
9052 			return;
9053 		}
9054 	}
9055 	*flagsp = ATF_INUSE;
9056 	/*
9057 	 * If /sbin/arp told us we are the authority using the "permanent"
9058 	 * flag, or if this is one of my addresses print "permanent"
9059 	 * in the /sbin/arp output.
9060 	 */
9061 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9062 		*flagsp |= ATF_AUTHORITY;
9063 	if (flags & NCE_F_NONUD)
9064 		*flagsp |= ATF_PERM; /* not subject to aging */
9065 	if (flags & NCE_F_PUBLISH)
9066 		*flagsp |= ATF_PUBL;
9067 	if (hwaddr != NULL) {
9068 		*flagsp |= ATF_COM;
9069 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9070 	}
9071 }
9072 
9073 /*
9074  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9075  * interface) create the next available logical interface for this
9076  * physical interface.
9077  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9078  * ipif with the specified name.
9079  *
9080  * If the address family is not AF_UNSPEC then set the address as well.
9081  *
9082  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9083  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9084  *
9085  * Executed as a writer on the ill.
9086  * So no lock is needed to traverse the ipif chain, or examine the
9087  * phyint flags.
9088  */
9089 /* ARGSUSED */
9090 int
9091 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9092     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9093 {
9094 	mblk_t	*mp1;
9095 	struct lifreq *lifr;
9096 	boolean_t	isv6;
9097 	boolean_t	exists;
9098 	char 	*name;
9099 	char	*endp;
9100 	char	*cp;
9101 	int	namelen;
9102 	ipif_t	*ipif;
9103 	long	id;
9104 	ipsq_t	*ipsq;
9105 	ill_t	*ill;
9106 	sin_t	*sin;
9107 	int	err = 0;
9108 	boolean_t found_sep = B_FALSE;
9109 	conn_t	*connp;
9110 	zoneid_t zoneid;
9111 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9112 
9113 	ASSERT(q->q_next == NULL);
9114 	ip1dbg(("ip_sioctl_addif\n"));
9115 	/* Existence of mp1 has been checked in ip_wput_nondata */
9116 	mp1 = mp->b_cont->b_cont;
9117 	/*
9118 	 * Null terminate the string to protect against buffer
9119 	 * overrun. String was generated by user code and may not
9120 	 * be trusted.
9121 	 */
9122 	lifr = (struct lifreq *)mp1->b_rptr;
9123 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9124 	name = lifr->lifr_name;
9125 	ASSERT(CONN_Q(q));
9126 	connp = Q_TO_CONN(q);
9127 	isv6 = (connp->conn_family == AF_INET6);
9128 	zoneid = connp->conn_zoneid;
9129 	namelen = mi_strlen(name);
9130 	if (namelen == 0)
9131 		return (EINVAL);
9132 
9133 	exists = B_FALSE;
9134 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9135 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9136 		/*
9137 		 * Allow creating lo0 using SIOCLIFADDIF.
9138 		 * can't be any other writer thread. So can pass null below
9139 		 * for the last 4 args to ipif_lookup_name.
9140 		 */
9141 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9142 		    &exists, isv6, zoneid, ipst);
9143 		/* Prevent any further action */
9144 		if (ipif == NULL) {
9145 			return (ENOBUFS);
9146 		} else if (!exists) {
9147 			/* We created the ipif now and as writer */
9148 			ipif_refrele(ipif);
9149 			return (0);
9150 		} else {
9151 			ill = ipif->ipif_ill;
9152 			ill_refhold(ill);
9153 			ipif_refrele(ipif);
9154 		}
9155 	} else {
9156 		/* Look for a colon in the name. */
9157 		endp = &name[namelen];
9158 		for (cp = endp; --cp > name; ) {
9159 			if (*cp == IPIF_SEPARATOR_CHAR) {
9160 				found_sep = B_TRUE;
9161 				/*
9162 				 * Reject any non-decimal aliases for plumbing
9163 				 * of logical interfaces. Aliases with leading
9164 				 * zeroes are also rejected as they introduce
9165 				 * ambiguity in the naming of the interfaces.
9166 				 * Comparing with "0" takes care of all such
9167 				 * cases.
9168 				 */
9169 				if ((strncmp("0", cp+1, 1)) == 0)
9170 					return (EINVAL);
9171 
9172 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9173 				    id <= 0 || *endp != '\0') {
9174 					return (EINVAL);
9175 				}
9176 				*cp = '\0';
9177 				break;
9178 			}
9179 		}
9180 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9181 		if (found_sep)
9182 			*cp = IPIF_SEPARATOR_CHAR;
9183 		if (ill == NULL)
9184 			return (ENXIO);
9185 	}
9186 
9187 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9188 	    B_TRUE);
9189 
9190 	/*
9191 	 * Release the refhold due to the lookup, now that we are excl
9192 	 * or we are just returning
9193 	 */
9194 	ill_refrele(ill);
9195 
9196 	if (ipsq == NULL)
9197 		return (EINPROGRESS);
9198 
9199 	/* We are now exclusive on the IPSQ */
9200 	ASSERT(IAM_WRITER_ILL(ill));
9201 
9202 	if (found_sep) {
9203 		/* Now see if there is an IPIF with this unit number. */
9204 		for (ipif = ill->ill_ipif; ipif != NULL;
9205 		    ipif = ipif->ipif_next) {
9206 			if (ipif->ipif_id == id) {
9207 				err = EEXIST;
9208 				goto done;
9209 			}
9210 		}
9211 	}
9212 
9213 	/*
9214 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9215 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9216 	 * instead.
9217 	 */
9218 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9219 	    B_TRUE, B_TRUE, &err)) == NULL) {
9220 		goto done;
9221 	}
9222 
9223 	/* Return created name with ioctl */
9224 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9225 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9226 	ip1dbg(("created %s\n", lifr->lifr_name));
9227 
9228 	/* Set address */
9229 	sin = (sin_t *)&lifr->lifr_addr;
9230 	if (sin->sin_family != AF_UNSPEC) {
9231 		err = ip_sioctl_addr(ipif, sin, q, mp,
9232 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9233 	}
9234 
9235 done:
9236 	ipsq_exit(ipsq);
9237 	return (err);
9238 }
9239 
9240 /*
9241  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9242  * interface) delete it based on the IP address (on this physical interface).
9243  * Otherwise delete it based on the ipif_id.
9244  * Also, special handling to allow a removeif of lo0.
9245  */
9246 /* ARGSUSED */
9247 int
9248 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9249     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9250 {
9251 	conn_t		*connp;
9252 	ill_t		*ill = ipif->ipif_ill;
9253 	boolean_t	 success;
9254 	ip_stack_t	*ipst;
9255 
9256 	ipst = CONNQ_TO_IPST(q);
9257 
9258 	ASSERT(q->q_next == NULL);
9259 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9260 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9261 	ASSERT(IAM_WRITER_IPIF(ipif));
9262 
9263 	connp = Q_TO_CONN(q);
9264 	/*
9265 	 * Special case for unplumbing lo0 (the loopback physical interface).
9266 	 * If unplumbing lo0, the incoming address structure has been
9267 	 * initialized to all zeros. When unplumbing lo0, all its logical
9268 	 * interfaces must be removed too.
9269 	 *
9270 	 * Note that this interface may be called to remove a specific
9271 	 * loopback logical interface (eg, lo0:1). But in that case
9272 	 * ipif->ipif_id != 0 so that the code path for that case is the
9273 	 * same as any other interface (meaning it skips the code directly
9274 	 * below).
9275 	 */
9276 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9277 		if (sin->sin_family == AF_UNSPEC &&
9278 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9279 			/*
9280 			 * Mark it condemned. No new ref. will be made to ill.
9281 			 */
9282 			mutex_enter(&ill->ill_lock);
9283 			ill->ill_state_flags |= ILL_CONDEMNED;
9284 			for (ipif = ill->ill_ipif; ipif != NULL;
9285 			    ipif = ipif->ipif_next) {
9286 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9287 			}
9288 			mutex_exit(&ill->ill_lock);
9289 
9290 			ipif = ill->ill_ipif;
9291 			/* unplumb the loopback interface */
9292 			ill_delete(ill);
9293 			mutex_enter(&connp->conn_lock);
9294 			mutex_enter(&ill->ill_lock);
9295 
9296 			/* Are any references to this ill active */
9297 			if (ill_is_freeable(ill)) {
9298 				mutex_exit(&ill->ill_lock);
9299 				mutex_exit(&connp->conn_lock);
9300 				ill_delete_tail(ill);
9301 				mi_free(ill);
9302 				return (0);
9303 			}
9304 			success = ipsq_pending_mp_add(connp, ipif,
9305 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9306 			mutex_exit(&connp->conn_lock);
9307 			mutex_exit(&ill->ill_lock);
9308 			if (success)
9309 				return (EINPROGRESS);
9310 			else
9311 				return (EINTR);
9312 		}
9313 	}
9314 
9315 	if (ipif->ipif_id == 0) {
9316 		ipsq_t *ipsq;
9317 
9318 		/* Find based on address */
9319 		if (ipif->ipif_isv6) {
9320 			sin6_t *sin6;
9321 
9322 			if (sin->sin_family != AF_INET6)
9323 				return (EAFNOSUPPORT);
9324 
9325 			sin6 = (sin6_t *)sin;
9326 			/* We are a writer, so we should be able to lookup */
9327 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9328 			    ipst);
9329 		} else {
9330 			if (sin->sin_family != AF_INET)
9331 				return (EAFNOSUPPORT);
9332 
9333 			/* We are a writer, so we should be able to lookup */
9334 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9335 			    ipst);
9336 		}
9337 		if (ipif == NULL) {
9338 			return (EADDRNOTAVAIL);
9339 		}
9340 
9341 		/*
9342 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9343 		 * lifr_name of the physical interface but with an ip address
9344 		 * lifr_addr of a logical interface plumbed over it.
9345 		 * So update ipx_current_ipif now that ipif points to the
9346 		 * correct one.
9347 		 */
9348 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9349 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9350 
9351 		/* This is a writer */
9352 		ipif_refrele(ipif);
9353 	}
9354 
9355 	/*
9356 	 * Can not delete instance zero since it is tied to the ill.
9357 	 */
9358 	if (ipif->ipif_id == 0)
9359 		return (EBUSY);
9360 
9361 	mutex_enter(&ill->ill_lock);
9362 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9363 	mutex_exit(&ill->ill_lock);
9364 
9365 	ipif_free(ipif);
9366 
9367 	mutex_enter(&connp->conn_lock);
9368 	mutex_enter(&ill->ill_lock);
9369 
9370 	/* Are any references to this ipif active */
9371 	if (ipif_is_freeable(ipif)) {
9372 		mutex_exit(&ill->ill_lock);
9373 		mutex_exit(&connp->conn_lock);
9374 		ipif_non_duplicate(ipif);
9375 		(void) ipif_down_tail(ipif);
9376 		ipif_free_tail(ipif); /* frees ipif */
9377 		return (0);
9378 	}
9379 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9380 	    IPIF_FREE);
9381 	mutex_exit(&ill->ill_lock);
9382 	mutex_exit(&connp->conn_lock);
9383 	if (success)
9384 		return (EINPROGRESS);
9385 	else
9386 		return (EINTR);
9387 }
9388 
9389 /*
9390  * Restart the removeif ioctl. The refcnt has gone down to 0.
9391  * The ipif is already condemned. So can't find it thru lookups.
9392  */
9393 /* ARGSUSED */
9394 int
9395 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9396     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9397 {
9398 	ill_t *ill = ipif->ipif_ill;
9399 
9400 	ASSERT(IAM_WRITER_IPIF(ipif));
9401 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9402 
9403 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9404 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9405 
9406 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9407 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9408 		ill_delete_tail(ill);
9409 		mi_free(ill);
9410 		return (0);
9411 	}
9412 
9413 	ipif_non_duplicate(ipif);
9414 	(void) ipif_down_tail(ipif);
9415 	ipif_free_tail(ipif);
9416 
9417 	return (0);
9418 }
9419 
9420 /*
9421  * Set the local interface address.
9422  * Allow an address of all zero when the interface is down.
9423  */
9424 /* ARGSUSED */
9425 int
9426 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9427     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9428 {
9429 	int err = 0;
9430 	in6_addr_t v6addr;
9431 	boolean_t need_up = B_FALSE;
9432 
9433 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9434 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9435 
9436 	ASSERT(IAM_WRITER_IPIF(ipif));
9437 
9438 	if (ipif->ipif_isv6) {
9439 		sin6_t *sin6;
9440 		ill_t *ill;
9441 		phyint_t *phyi;
9442 
9443 		if (sin->sin_family != AF_INET6)
9444 			return (EAFNOSUPPORT);
9445 
9446 		sin6 = (sin6_t *)sin;
9447 		v6addr = sin6->sin6_addr;
9448 		ill = ipif->ipif_ill;
9449 		phyi = ill->ill_phyint;
9450 
9451 		/*
9452 		 * Enforce that true multicast interfaces have a link-local
9453 		 * address for logical unit 0.
9454 		 */
9455 		if (ipif->ipif_id == 0 &&
9456 		    (ill->ill_flags & ILLF_MULTICAST) &&
9457 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9458 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9459 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9460 			return (EADDRNOTAVAIL);
9461 		}
9462 
9463 		/*
9464 		 * up interfaces shouldn't have the unspecified address
9465 		 * unless they also have the IPIF_NOLOCAL flags set and
9466 		 * have a subnet assigned.
9467 		 */
9468 		if ((ipif->ipif_flags & IPIF_UP) &&
9469 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9470 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9471 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9472 			return (EADDRNOTAVAIL);
9473 		}
9474 
9475 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9476 			return (EADDRNOTAVAIL);
9477 	} else {
9478 		ipaddr_t addr;
9479 
9480 		if (sin->sin_family != AF_INET)
9481 			return (EAFNOSUPPORT);
9482 
9483 		addr = sin->sin_addr.s_addr;
9484 
9485 		/* Allow 0 as the local address. */
9486 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9487 			return (EADDRNOTAVAIL);
9488 
9489 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9490 	}
9491 
9492 	/*
9493 	 * Even if there is no change we redo things just to rerun
9494 	 * ipif_set_default.
9495 	 */
9496 	if (ipif->ipif_flags & IPIF_UP) {
9497 		/*
9498 		 * Setting a new local address, make sure
9499 		 * we have net and subnet bcast ire's for
9500 		 * the old address if we need them.
9501 		 */
9502 		/*
9503 		 * If the interface is already marked up,
9504 		 * we call ipif_down which will take care
9505 		 * of ditching any IREs that have been set
9506 		 * up based on the old interface address.
9507 		 */
9508 		err = ipif_logical_down(ipif, q, mp);
9509 		if (err == EINPROGRESS)
9510 			return (err);
9511 		(void) ipif_down_tail(ipif);
9512 		need_up = 1;
9513 	}
9514 
9515 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9516 	return (err);
9517 }
9518 
9519 int
9520 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9521     boolean_t need_up)
9522 {
9523 	in6_addr_t v6addr;
9524 	in6_addr_t ov6addr;
9525 	ipaddr_t addr;
9526 	sin6_t	*sin6;
9527 	int	sinlen;
9528 	int	err = 0;
9529 	ill_t	*ill = ipif->ipif_ill;
9530 	boolean_t need_dl_down;
9531 	boolean_t need_arp_down;
9532 	struct iocblk *iocp;
9533 
9534 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9535 
9536 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9537 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9538 	ASSERT(IAM_WRITER_IPIF(ipif));
9539 
9540 	/* Must cancel any pending timer before taking the ill_lock */
9541 	if (ipif->ipif_recovery_id != 0)
9542 		(void) untimeout(ipif->ipif_recovery_id);
9543 	ipif->ipif_recovery_id = 0;
9544 
9545 	if (ipif->ipif_isv6) {
9546 		sin6 = (sin6_t *)sin;
9547 		v6addr = sin6->sin6_addr;
9548 		sinlen = sizeof (struct sockaddr_in6);
9549 	} else {
9550 		addr = sin->sin_addr.s_addr;
9551 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9552 		sinlen = sizeof (struct sockaddr_in);
9553 	}
9554 	mutex_enter(&ill->ill_lock);
9555 	ov6addr = ipif->ipif_v6lcl_addr;
9556 	ipif->ipif_v6lcl_addr = v6addr;
9557 	sctp_update_ipif_addr(ipif, ov6addr);
9558 	ipif->ipif_addr_ready = 0;
9559 
9560 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9561 
9562 	/*
9563 	 * If the interface was previously marked as a duplicate, then since
9564 	 * we've now got a "new" address, it should no longer be considered a
9565 	 * duplicate -- even if the "new" address is the same as the old one.
9566 	 * Note that if all ipifs are down, we may have a pending ARP down
9567 	 * event to handle.  This is because we want to recover from duplicates
9568 	 * and thus delay tearing down ARP until the duplicates have been
9569 	 * removed or disabled.
9570 	 */
9571 	need_dl_down = need_arp_down = B_FALSE;
9572 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9573 		need_arp_down = !need_up;
9574 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9575 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9576 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9577 			need_dl_down = B_TRUE;
9578 		}
9579 	}
9580 
9581 	ipif_set_default(ipif);
9582 
9583 	/*
9584 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9585 	 * tag the ill so that future updates to the interface ID don't result
9586 	 * in this address getting automatically reconfigured from under the
9587 	 * administrator.
9588 	 */
9589 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9590 		ill->ill_manual_linklocal = 1;
9591 
9592 	/*
9593 	 * When publishing an interface address change event, we only notify
9594 	 * the event listeners of the new address.  It is assumed that if they
9595 	 * actively care about the addresses assigned that they will have
9596 	 * already discovered the previous address assigned (if there was one.)
9597 	 *
9598 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9599 	 */
9600 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9601 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9602 		    NE_ADDRESS_CHANGE, sin, sinlen);
9603 	}
9604 
9605 	mutex_exit(&ill->ill_lock);
9606 
9607 	if (need_up) {
9608 		/*
9609 		 * Now bring the interface back up.  If this
9610 		 * is the only IPIF for the ILL, ipif_up
9611 		 * will have to re-bind to the device, so
9612 		 * we may get back EINPROGRESS, in which
9613 		 * case, this IOCTL will get completed in
9614 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9615 		 */
9616 		err = ipif_up(ipif, q, mp);
9617 	} else {
9618 		/* Perhaps ilgs should use this ill */
9619 		update_conn_ill(NULL, ill->ill_ipst);
9620 	}
9621 
9622 	if (need_dl_down)
9623 		ill_dl_down(ill);
9624 
9625 	if (need_arp_down && !ill->ill_isv6)
9626 		(void) ipif_arp_down(ipif);
9627 
9628 	/*
9629 	 * The default multicast interface might have changed (for
9630 	 * instance if the IPv6 scope of the address changed)
9631 	 */
9632 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9633 
9634 	return (err);
9635 }
9636 
9637 /*
9638  * Restart entry point to restart the address set operation after the
9639  * refcounts have dropped to zero.
9640  */
9641 /* ARGSUSED */
9642 int
9643 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9644     ip_ioctl_cmd_t *ipip, void *ifreq)
9645 {
9646 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9647 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9648 	ASSERT(IAM_WRITER_IPIF(ipif));
9649 	(void) ipif_down_tail(ipif);
9650 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9651 }
9652 
9653 /* ARGSUSED */
9654 int
9655 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9656     ip_ioctl_cmd_t *ipip, void *if_req)
9657 {
9658 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9659 	struct lifreq *lifr = (struct lifreq *)if_req;
9660 
9661 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9662 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9663 	/*
9664 	 * The net mask and address can't change since we have a
9665 	 * reference to the ipif. So no lock is necessary.
9666 	 */
9667 	if (ipif->ipif_isv6) {
9668 		*sin6 = sin6_null;
9669 		sin6->sin6_family = AF_INET6;
9670 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9671 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9672 		lifr->lifr_addrlen =
9673 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9674 	} else {
9675 		*sin = sin_null;
9676 		sin->sin_family = AF_INET;
9677 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9678 		if (ipip->ipi_cmd_type == LIF_CMD) {
9679 			lifr->lifr_addrlen =
9680 			    ip_mask_to_plen(ipif->ipif_net_mask);
9681 		}
9682 	}
9683 	return (0);
9684 }
9685 
9686 /*
9687  * Set the destination address for a pt-pt interface.
9688  */
9689 /* ARGSUSED */
9690 int
9691 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9692     ip_ioctl_cmd_t *ipip, void *if_req)
9693 {
9694 	int err = 0;
9695 	in6_addr_t v6addr;
9696 	boolean_t need_up = B_FALSE;
9697 
9698 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9699 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9700 	ASSERT(IAM_WRITER_IPIF(ipif));
9701 
9702 	if (ipif->ipif_isv6) {
9703 		sin6_t *sin6;
9704 
9705 		if (sin->sin_family != AF_INET6)
9706 			return (EAFNOSUPPORT);
9707 
9708 		sin6 = (sin6_t *)sin;
9709 		v6addr = sin6->sin6_addr;
9710 
9711 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9712 			return (EADDRNOTAVAIL);
9713 	} else {
9714 		ipaddr_t addr;
9715 
9716 		if (sin->sin_family != AF_INET)
9717 			return (EAFNOSUPPORT);
9718 
9719 		addr = sin->sin_addr.s_addr;
9720 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9721 			return (EADDRNOTAVAIL);
9722 
9723 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9724 	}
9725 
9726 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9727 		return (0);	/* No change */
9728 
9729 	if (ipif->ipif_flags & IPIF_UP) {
9730 		/*
9731 		 * If the interface is already marked up,
9732 		 * we call ipif_down which will take care
9733 		 * of ditching any IREs that have been set
9734 		 * up based on the old pp dst address.
9735 		 */
9736 		err = ipif_logical_down(ipif, q, mp);
9737 		if (err == EINPROGRESS)
9738 			return (err);
9739 		(void) ipif_down_tail(ipif);
9740 		need_up = B_TRUE;
9741 	}
9742 	/*
9743 	 * could return EINPROGRESS. If so ioctl will complete in
9744 	 * ip_rput_dlpi_writer
9745 	 */
9746 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9747 	return (err);
9748 }
9749 
9750 static int
9751 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9752     boolean_t need_up)
9753 {
9754 	in6_addr_t v6addr;
9755 	ill_t	*ill = ipif->ipif_ill;
9756 	int	err = 0;
9757 	boolean_t need_dl_down;
9758 	boolean_t need_arp_down;
9759 
9760 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9761 	    ipif->ipif_id, (void *)ipif));
9762 
9763 	/* Must cancel any pending timer before taking the ill_lock */
9764 	if (ipif->ipif_recovery_id != 0)
9765 		(void) untimeout(ipif->ipif_recovery_id);
9766 	ipif->ipif_recovery_id = 0;
9767 
9768 	if (ipif->ipif_isv6) {
9769 		sin6_t *sin6;
9770 
9771 		sin6 = (sin6_t *)sin;
9772 		v6addr = sin6->sin6_addr;
9773 	} else {
9774 		ipaddr_t addr;
9775 
9776 		addr = sin->sin_addr.s_addr;
9777 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9778 	}
9779 	mutex_enter(&ill->ill_lock);
9780 	/* Set point to point destination address. */
9781 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9782 		/*
9783 		 * Allow this as a means of creating logical
9784 		 * pt-pt interfaces on top of e.g. an Ethernet.
9785 		 * XXX Undocumented HACK for testing.
9786 		 * pt-pt interfaces are created with NUD disabled.
9787 		 */
9788 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9789 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9790 		if (ipif->ipif_isv6)
9791 			ill->ill_flags |= ILLF_NONUD;
9792 	}
9793 
9794 	/*
9795 	 * If the interface was previously marked as a duplicate, then since
9796 	 * we've now got a "new" address, it should no longer be considered a
9797 	 * duplicate -- even if the "new" address is the same as the old one.
9798 	 * Note that if all ipifs are down, we may have a pending ARP down
9799 	 * event to handle.
9800 	 */
9801 	need_dl_down = need_arp_down = B_FALSE;
9802 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9803 		need_arp_down = !need_up;
9804 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9805 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9806 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9807 			need_dl_down = B_TRUE;
9808 		}
9809 	}
9810 
9811 	/*
9812 	 * If we've just manually set the IPv6 destination link-local address
9813 	 * (0th ipif), tag the ill so that future updates to the destination
9814 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9815 	 * result in this address getting automatically reconfigured from
9816 	 * under the administrator.
9817 	 */
9818 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9819 		ill->ill_manual_dst_linklocal = 1;
9820 
9821 	/* Set the new address. */
9822 	ipif->ipif_v6pp_dst_addr = v6addr;
9823 	/* Make sure subnet tracks pp_dst */
9824 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9825 	mutex_exit(&ill->ill_lock);
9826 
9827 	if (need_up) {
9828 		/*
9829 		 * Now bring the interface back up.  If this
9830 		 * is the only IPIF for the ILL, ipif_up
9831 		 * will have to re-bind to the device, so
9832 		 * we may get back EINPROGRESS, in which
9833 		 * case, this IOCTL will get completed in
9834 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9835 		 */
9836 		err = ipif_up(ipif, q, mp);
9837 	}
9838 
9839 	if (need_dl_down)
9840 		ill_dl_down(ill);
9841 	if (need_arp_down && !ipif->ipif_isv6)
9842 		(void) ipif_arp_down(ipif);
9843 
9844 	return (err);
9845 }
9846 
9847 /*
9848  * Restart entry point to restart the dstaddress set operation after the
9849  * refcounts have dropped to zero.
9850  */
9851 /* ARGSUSED */
9852 int
9853 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9854     ip_ioctl_cmd_t *ipip, void *ifreq)
9855 {
9856 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9857 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9858 	(void) ipif_down_tail(ipif);
9859 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9860 }
9861 
9862 /* ARGSUSED */
9863 int
9864 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9865     ip_ioctl_cmd_t *ipip, void *if_req)
9866 {
9867 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9868 
9869 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9870 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9871 	/*
9872 	 * Get point to point destination address. The addresses can't
9873 	 * change since we hold a reference to the ipif.
9874 	 */
9875 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9876 		return (EADDRNOTAVAIL);
9877 
9878 	if (ipif->ipif_isv6) {
9879 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9880 		*sin6 = sin6_null;
9881 		sin6->sin6_family = AF_INET6;
9882 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9883 	} else {
9884 		*sin = sin_null;
9885 		sin->sin_family = AF_INET;
9886 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9887 	}
9888 	return (0);
9889 }
9890 
9891 /*
9892  * Check which flags will change by the given flags being set
9893  * silently ignore flags which userland is not allowed to control.
9894  * (Because these flags may change between SIOCGLIFFLAGS and
9895  * SIOCSLIFFLAGS, and that's outside of userland's control,
9896  * we need to silently ignore them rather than fail.)
9897  */
9898 static void
9899 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9900     uint64_t *offp)
9901 {
9902 	ill_t		*ill = ipif->ipif_ill;
9903 	phyint_t 	*phyi = ill->ill_phyint;
9904 	uint64_t	cantchange_flags, intf_flags;
9905 	uint64_t	turn_on, turn_off;
9906 
9907 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9908 	cantchange_flags = IFF_CANTCHANGE;
9909 	if (IS_IPMP(ill))
9910 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9911 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9912 	turn_off = intf_flags & turn_on;
9913 	turn_on ^= turn_off;
9914 	*onp = turn_on;
9915 	*offp = turn_off;
9916 }
9917 
9918 /*
9919  * Set interface flags.  Many flags require special handling (e.g.,
9920  * bringing the interface down); see below for details.
9921  *
9922  * NOTE : We really don't enforce that ipif_id zero should be used
9923  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9924  *	  is because applications generally does SICGLIFFLAGS and
9925  *	  ORs in the new flags (that affects the logical) and does a
9926  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9927  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9928  *	  flags that will be turned on is correct with respect to
9929  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9930  */
9931 /* ARGSUSED */
9932 int
9933 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9934     ip_ioctl_cmd_t *ipip, void *if_req)
9935 {
9936 	uint64_t turn_on;
9937 	uint64_t turn_off;
9938 	int	err = 0;
9939 	phyint_t *phyi;
9940 	ill_t *ill;
9941 	conn_t *connp;
9942 	uint64_t intf_flags;
9943 	boolean_t phyint_flags_modified = B_FALSE;
9944 	uint64_t flags;
9945 	struct ifreq *ifr;
9946 	struct lifreq *lifr;
9947 	boolean_t set_linklocal = B_FALSE;
9948 
9949 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9950 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9951 
9952 	ASSERT(IAM_WRITER_IPIF(ipif));
9953 
9954 	ill = ipif->ipif_ill;
9955 	phyi = ill->ill_phyint;
9956 
9957 	if (ipip->ipi_cmd_type == IF_CMD) {
9958 		ifr = (struct ifreq *)if_req;
9959 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9960 	} else {
9961 		lifr = (struct lifreq *)if_req;
9962 		flags = lifr->lifr_flags;
9963 	}
9964 
9965 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9966 
9967 	/*
9968 	 * Have the flags been set correctly until now?
9969 	 */
9970 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9971 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9972 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9973 	/*
9974 	 * Compare the new flags to the old, and partition
9975 	 * into those coming on and those going off.
9976 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9977 	 */
9978 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9979 		flags |= intf_flags & ~0xFFFF;
9980 
9981 	/*
9982 	 * Explicitly fail attempts to change flags that are always invalid on
9983 	 * an IPMP meta-interface.
9984 	 */
9985 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9986 		return (EINVAL);
9987 
9988 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
9989 	if ((turn_on|turn_off) == 0)
9990 		return (0);	/* No change */
9991 
9992 	/*
9993 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
9994 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
9995 	 * allow it to be turned off.
9996 	 */
9997 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
9998 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
9999 		return (EINVAL);
10000 
10001 	if ((connp = Q_TO_CONN(q)) == NULL)
10002 		return (EINVAL);
10003 
10004 	/*
10005 	 * Only vrrp control socket is allowed to change IFF_UP and
10006 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10007 	 */
10008 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10009 		if (!connp->conn_isvrrp)
10010 			return (EINVAL);
10011 	}
10012 
10013 	/*
10014 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10015 	 * VRRP control socket.
10016 	 */
10017 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10018 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10019 			return (EINVAL);
10020 	}
10021 
10022 	if (turn_on & IFF_NOFAILOVER) {
10023 		turn_on |= IFF_DEPRECATED;
10024 		flags |= IFF_DEPRECATED;
10025 	}
10026 
10027 	/*
10028 	 * On underlying interfaces, only allow applications to manage test
10029 	 * addresses -- otherwise, they may get confused when the address
10030 	 * moves as part of being brought up.  Likewise, prevent an
10031 	 * application-managed test address from being converted to a data
10032 	 * address.  To prevent migration of administratively up addresses in
10033 	 * the kernel, we don't allow them to be converted either.
10034 	 */
10035 	if (IS_UNDER_IPMP(ill)) {
10036 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10037 
10038 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10039 			return (EINVAL);
10040 
10041 		if ((turn_off & IFF_NOFAILOVER) &&
10042 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10043 			return (EINVAL);
10044 	}
10045 
10046 	/*
10047 	 * Only allow IFF_TEMPORARY flag to be set on
10048 	 * IPv6 interfaces.
10049 	 */
10050 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10051 		return (EINVAL);
10052 
10053 	/*
10054 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10055 	 */
10056 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10057 		return (EINVAL);
10058 
10059 	/*
10060 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10061 	 * interfaces.  It makes no sense in that context.
10062 	 */
10063 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10064 		return (EINVAL);
10065 
10066 	/*
10067 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10068 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10069 	 * If the link local address isn't set, and can be set, it will get
10070 	 * set later on in this function.
10071 	 */
10072 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10073 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10074 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10075 		if (ipif_cant_setlinklocal(ipif))
10076 			return (EINVAL);
10077 		set_linklocal = B_TRUE;
10078 	}
10079 
10080 	/*
10081 	 * If we modify physical interface flags, we'll potentially need to
10082 	 * send up two routing socket messages for the changes (one for the
10083 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10084 	 */
10085 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10086 		phyint_flags_modified = B_TRUE;
10087 
10088 	/*
10089 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10090 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10091 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10092 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10093 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10094 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10095 	 * will not be honored.
10096 	 */
10097 	if (turn_on & PHYI_STANDBY) {
10098 		/*
10099 		 * No need to grab ill_g_usesrc_lock here; see the
10100 		 * synchronization notes in ip.c.
10101 		 */
10102 		if (ill->ill_usesrc_grp_next != NULL ||
10103 		    intf_flags & PHYI_INACTIVE)
10104 			return (EINVAL);
10105 		if (!(flags & PHYI_FAILED)) {
10106 			flags |= PHYI_INACTIVE;
10107 			turn_on |= PHYI_INACTIVE;
10108 		}
10109 	}
10110 
10111 	if (turn_off & PHYI_STANDBY) {
10112 		flags &= ~PHYI_INACTIVE;
10113 		turn_off |= PHYI_INACTIVE;
10114 	}
10115 
10116 	/*
10117 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10118 	 * would end up on.
10119 	 */
10120 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10121 	    (PHYI_FAILED | PHYI_INACTIVE))
10122 		return (EINVAL);
10123 
10124 	/*
10125 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10126 	 * status of the interface.
10127 	 */
10128 	if ((turn_on | turn_off) & ILLF_ROUTER)
10129 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10130 
10131 	/*
10132 	 * If the interface is not UP and we are not going to
10133 	 * bring it UP, record the flags and return. When the
10134 	 * interface comes UP later, the right actions will be
10135 	 * taken.
10136 	 */
10137 	if (!(ipif->ipif_flags & IPIF_UP) &&
10138 	    !(turn_on & IPIF_UP)) {
10139 		/* Record new flags in their respective places. */
10140 		mutex_enter(&ill->ill_lock);
10141 		mutex_enter(&ill->ill_phyint->phyint_lock);
10142 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10143 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10144 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10145 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10146 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10147 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10148 		mutex_exit(&ill->ill_lock);
10149 		mutex_exit(&ill->ill_phyint->phyint_lock);
10150 
10151 		/*
10152 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10153 		 * same to the kernel: if any of them has been set by
10154 		 * userland, the interface cannot be used for data traffic.
10155 		 */
10156 		if ((turn_on|turn_off) &
10157 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10158 			ASSERT(!IS_IPMP(ill));
10159 			/*
10160 			 * It's possible the ill is part of an "anonymous"
10161 			 * IPMP group rather than a real group.  In that case,
10162 			 * there are no other interfaces in the group and thus
10163 			 * no need to call ipmp_phyint_refresh_active().
10164 			 */
10165 			if (IS_UNDER_IPMP(ill))
10166 				ipmp_phyint_refresh_active(phyi);
10167 		}
10168 
10169 		if (phyint_flags_modified) {
10170 			if (phyi->phyint_illv4 != NULL) {
10171 				ip_rts_ifmsg(phyi->phyint_illv4->
10172 				    ill_ipif, RTSQ_DEFAULT);
10173 			}
10174 			if (phyi->phyint_illv6 != NULL) {
10175 				ip_rts_ifmsg(phyi->phyint_illv6->
10176 				    ill_ipif, RTSQ_DEFAULT);
10177 			}
10178 		}
10179 		/* The default multicast interface might have changed */
10180 		ire_increment_multicast_generation(ill->ill_ipst,
10181 		    ill->ill_isv6);
10182 
10183 		return (0);
10184 	} else if (set_linklocal) {
10185 		mutex_enter(&ill->ill_lock);
10186 		if (set_linklocal)
10187 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10188 		mutex_exit(&ill->ill_lock);
10189 	}
10190 
10191 	/*
10192 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10193 	 * or point-to-point interfaces with an unspecified destination. We do
10194 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10195 	 * have a subnet assigned, which is how in.ndpd currently manages its
10196 	 * onlink prefix list when no addresses are configured with those
10197 	 * prefixes.
10198 	 */
10199 	if (ipif->ipif_isv6 &&
10200 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10201 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10202 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10203 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10204 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10205 		return (EINVAL);
10206 	}
10207 
10208 	/*
10209 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10210 	 * from being brought up.
10211 	 */
10212 	if (!ipif->ipif_isv6 &&
10213 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10214 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10215 		return (EINVAL);
10216 	}
10217 
10218 	/*
10219 	 * If we are going to change one or more of the flags that are
10220 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10221 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10222 	 * IPIF_NOFAILOVER, we will take special action.  This is
10223 	 * done by bring the ipif down, changing the flags and bringing
10224 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10225 	 * back up will trigger the address to be moved.
10226 	 *
10227 	 * If we are going to change IFF_NOACCEPT, we need to bring
10228 	 * all the ipifs down then bring them up again.	 The act of
10229 	 * bringing all the ipifs back up will trigger the local
10230 	 * ires being recreated with "no_accept" set/cleared.
10231 	 *
10232 	 * Note that ILLF_NOACCEPT is always set separately from the
10233 	 * other flags.
10234 	 */
10235 	if ((turn_on|turn_off) &
10236 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10237 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10238 	    IPIF_NOFAILOVER)) {
10239 		/*
10240 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10241 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10242 		 * entries shared between multiple ipifs on the same subnet.
10243 		 */
10244 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10245 		    !(turn_off & IPIF_UP)) {
10246 			if (ipif->ipif_flags & IPIF_UP)
10247 				ill->ill_logical_down = 1;
10248 			turn_on &= ~IPIF_UP;
10249 		}
10250 		err = ipif_down(ipif, q, mp);
10251 		ip1dbg(("ipif_down returns %d err ", err));
10252 		if (err == EINPROGRESS)
10253 			return (err);
10254 		(void) ipif_down_tail(ipif);
10255 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10256 		/*
10257 		 * If we can quiesce the ill, then continue.  If not, then
10258 		 * ip_sioctl_flags_tail() will be called from
10259 		 * ipif_ill_refrele_tail().
10260 		 */
10261 		ill_down_ipifs(ill, B_TRUE);
10262 
10263 		mutex_enter(&connp->conn_lock);
10264 		mutex_enter(&ill->ill_lock);
10265 		if (!ill_is_quiescent(ill)) {
10266 			boolean_t success;
10267 
10268 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10269 			    q, mp, ILL_DOWN);
10270 			mutex_exit(&ill->ill_lock);
10271 			mutex_exit(&connp->conn_lock);
10272 			return (success ? EINPROGRESS : EINTR);
10273 		}
10274 		mutex_exit(&ill->ill_lock);
10275 		mutex_exit(&connp->conn_lock);
10276 	}
10277 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10278 }
10279 
10280 static int
10281 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10282 {
10283 	ill_t	*ill;
10284 	phyint_t *phyi;
10285 	uint64_t turn_on, turn_off;
10286 	boolean_t phyint_flags_modified = B_FALSE;
10287 	int	err = 0;
10288 	boolean_t set_linklocal = B_FALSE;
10289 
10290 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10291 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10292 
10293 	ASSERT(IAM_WRITER_IPIF(ipif));
10294 
10295 	ill = ipif->ipif_ill;
10296 	phyi = ill->ill_phyint;
10297 
10298 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10299 
10300 	/*
10301 	 * IFF_UP is handled separately.
10302 	 */
10303 	turn_on &= ~IFF_UP;
10304 	turn_off &= ~IFF_UP;
10305 
10306 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10307 		phyint_flags_modified = B_TRUE;
10308 
10309 	/*
10310 	 * Now we change the flags. Track current value of
10311 	 * other flags in their respective places.
10312 	 */
10313 	mutex_enter(&ill->ill_lock);
10314 	mutex_enter(&phyi->phyint_lock);
10315 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10316 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10317 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10318 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10319 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10320 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10321 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10322 		set_linklocal = B_TRUE;
10323 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10324 	}
10325 
10326 	mutex_exit(&ill->ill_lock);
10327 	mutex_exit(&phyi->phyint_lock);
10328 
10329 	if (set_linklocal)
10330 		(void) ipif_setlinklocal(ipif);
10331 
10332 	/*
10333 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10334 	 * the kernel: if any of them has been set by userland, the interface
10335 	 * cannot be used for data traffic.
10336 	 */
10337 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10338 		ASSERT(!IS_IPMP(ill));
10339 		/*
10340 		 * It's possible the ill is part of an "anonymous" IPMP group
10341 		 * rather than a real group.  In that case, there are no other
10342 		 * interfaces in the group and thus no need for us to call
10343 		 * ipmp_phyint_refresh_active().
10344 		 */
10345 		if (IS_UNDER_IPMP(ill))
10346 			ipmp_phyint_refresh_active(phyi);
10347 	}
10348 
10349 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10350 		/*
10351 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10352 		 * ipifs that were brought down.
10353 		 *
10354 		 * The routing sockets messages are sent as the result
10355 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10356 		 * as well.
10357 		 */
10358 		err = ill_up_ipifs(ill, q, mp);
10359 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10360 		/*
10361 		 * XXX ipif_up really does not know whether a phyint flags
10362 		 * was modified or not. So, it sends up information on
10363 		 * only one routing sockets message. As we don't bring up
10364 		 * the interface and also set PHYI_ flags simultaneously
10365 		 * it should be okay.
10366 		 */
10367 		err = ipif_up(ipif, q, mp);
10368 	} else {
10369 		/*
10370 		 * Make sure routing socket sees all changes to the flags.
10371 		 * ipif_up_done* handles this when we use ipif_up.
10372 		 */
10373 		if (phyint_flags_modified) {
10374 			if (phyi->phyint_illv4 != NULL) {
10375 				ip_rts_ifmsg(phyi->phyint_illv4->
10376 				    ill_ipif, RTSQ_DEFAULT);
10377 			}
10378 			if (phyi->phyint_illv6 != NULL) {
10379 				ip_rts_ifmsg(phyi->phyint_illv6->
10380 				    ill_ipif, RTSQ_DEFAULT);
10381 			}
10382 		} else {
10383 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10384 		}
10385 		/*
10386 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10387 		 * this in need_up case.
10388 		 */
10389 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10390 	}
10391 
10392 	/* The default multicast interface might have changed */
10393 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10394 	return (err);
10395 }
10396 
10397 /*
10398  * Restart the flags operation now that the refcounts have dropped to zero.
10399  */
10400 /* ARGSUSED */
10401 int
10402 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10403     ip_ioctl_cmd_t *ipip, void *if_req)
10404 {
10405 	uint64_t flags;
10406 	struct ifreq *ifr = if_req;
10407 	struct lifreq *lifr = if_req;
10408 	uint64_t turn_on, turn_off;
10409 
10410 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10411 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10412 
10413 	if (ipip->ipi_cmd_type == IF_CMD) {
10414 		/* cast to uint16_t prevents unwanted sign extension */
10415 		flags = (uint16_t)ifr->ifr_flags;
10416 	} else {
10417 		flags = lifr->lifr_flags;
10418 	}
10419 
10420 	/*
10421 	 * If this function call is a result of the ILLF_NOACCEPT flag
10422 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10423 	 */
10424 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10425 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10426 		(void) ipif_down_tail(ipif);
10427 
10428 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10429 }
10430 
10431 /*
10432  * Can operate on either a module or a driver queue.
10433  */
10434 /* ARGSUSED */
10435 int
10436 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10437     ip_ioctl_cmd_t *ipip, void *if_req)
10438 {
10439 	/*
10440 	 * Has the flags been set correctly till now ?
10441 	 */
10442 	ill_t *ill = ipif->ipif_ill;
10443 	phyint_t *phyi = ill->ill_phyint;
10444 
10445 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10446 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10447 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10448 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10449 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10450 
10451 	/*
10452 	 * Need a lock since some flags can be set even when there are
10453 	 * references to the ipif.
10454 	 */
10455 	mutex_enter(&ill->ill_lock);
10456 	if (ipip->ipi_cmd_type == IF_CMD) {
10457 		struct ifreq *ifr = (struct ifreq *)if_req;
10458 
10459 		/* Get interface flags (low 16 only). */
10460 		ifr->ifr_flags = ((ipif->ipif_flags |
10461 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10462 	} else {
10463 		struct lifreq *lifr = (struct lifreq *)if_req;
10464 
10465 		/* Get interface flags. */
10466 		lifr->lifr_flags = ipif->ipif_flags |
10467 		    ill->ill_flags | phyi->phyint_flags;
10468 	}
10469 	mutex_exit(&ill->ill_lock);
10470 	return (0);
10471 }
10472 
10473 /*
10474  * We allow the MTU to be set on an ILL, but not have it be different
10475  * for different IPIFs since we don't actually send packets on IPIFs.
10476  */
10477 /* ARGSUSED */
10478 int
10479 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10480     ip_ioctl_cmd_t *ipip, void *if_req)
10481 {
10482 	int mtu;
10483 	int ip_min_mtu;
10484 	struct ifreq	*ifr;
10485 	struct lifreq *lifr;
10486 	ill_t	*ill;
10487 
10488 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10489 	    ipif->ipif_id, (void *)ipif));
10490 	if (ipip->ipi_cmd_type == IF_CMD) {
10491 		ifr = (struct ifreq *)if_req;
10492 		mtu = ifr->ifr_metric;
10493 	} else {
10494 		lifr = (struct lifreq *)if_req;
10495 		mtu = lifr->lifr_mtu;
10496 	}
10497 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10498 	if (ipif->ipif_id != 0)
10499 		return (EINVAL);
10500 
10501 	ill = ipif->ipif_ill;
10502 	if (ipif->ipif_isv6)
10503 		ip_min_mtu = IPV6_MIN_MTU;
10504 	else
10505 		ip_min_mtu = IP_MIN_MTU;
10506 
10507 	mutex_enter(&ill->ill_lock);
10508 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10509 		mutex_exit(&ill->ill_lock);
10510 		return (EINVAL);
10511 	}
10512 	/*
10513 	 * The dce and fragmentation code can handle changes to ill_mtu
10514 	 * concurrent with sending/fragmenting packets.
10515 	 */
10516 	ill->ill_mtu = mtu;
10517 	ill->ill_flags |= ILLF_FIXEDMTU;
10518 	mutex_exit(&ill->ill_lock);
10519 
10520 	/*
10521 	 * Make sure all dce_generation checks find out
10522 	 * that ill_mtu has changed.
10523 	 */
10524 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10525 
10526 	/* Update the MTU in SCTP's list */
10527 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10528 	return (0);
10529 }
10530 
10531 /* Get interface MTU. */
10532 /* ARGSUSED */
10533 int
10534 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10535 	ip_ioctl_cmd_t *ipip, void *if_req)
10536 {
10537 	struct ifreq	*ifr;
10538 	struct lifreq	*lifr;
10539 
10540 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10541 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10542 
10543 	/*
10544 	 * We allow a get on any logical interface even though the set
10545 	 * can only be done on logical unit 0.
10546 	 */
10547 	if (ipip->ipi_cmd_type == IF_CMD) {
10548 		ifr = (struct ifreq *)if_req;
10549 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10550 	} else {
10551 		lifr = (struct lifreq *)if_req;
10552 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10553 	}
10554 	return (0);
10555 }
10556 
10557 /* Set interface broadcast address. */
10558 /* ARGSUSED2 */
10559 int
10560 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10561 	ip_ioctl_cmd_t *ipip, void *if_req)
10562 {
10563 	ipaddr_t addr;
10564 	ire_t	*ire;
10565 	ill_t		*ill = ipif->ipif_ill;
10566 	ip_stack_t	*ipst = ill->ill_ipst;
10567 
10568 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10569 	    ipif->ipif_id));
10570 
10571 	ASSERT(IAM_WRITER_IPIF(ipif));
10572 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10573 		return (EADDRNOTAVAIL);
10574 
10575 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10576 
10577 	if (sin->sin_family != AF_INET)
10578 		return (EAFNOSUPPORT);
10579 
10580 	addr = sin->sin_addr.s_addr;
10581 	if (ipif->ipif_flags & IPIF_UP) {
10582 		/*
10583 		 * If we are already up, make sure the new
10584 		 * broadcast address makes sense.  If it does,
10585 		 * there should be an IRE for it already.
10586 		 */
10587 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10588 		    ill, ipif->ipif_zoneid, NULL,
10589 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10590 		if (ire == NULL) {
10591 			return (EINVAL);
10592 		} else {
10593 			ire_refrele(ire);
10594 		}
10595 	}
10596 	/*
10597 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10598 	 * needs to already exist we never need to change the set of
10599 	 * IRE_BROADCASTs when we are UP.
10600 	 */
10601 	if (addr != ipif->ipif_brd_addr)
10602 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10603 
10604 	return (0);
10605 }
10606 
10607 /* Get interface broadcast address. */
10608 /* ARGSUSED */
10609 int
10610 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10611     ip_ioctl_cmd_t *ipip, void *if_req)
10612 {
10613 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10614 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10615 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10616 		return (EADDRNOTAVAIL);
10617 
10618 	/* IPIF_BROADCAST not possible with IPv6 */
10619 	ASSERT(!ipif->ipif_isv6);
10620 	*sin = sin_null;
10621 	sin->sin_family = AF_INET;
10622 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10623 	return (0);
10624 }
10625 
10626 /*
10627  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10628  */
10629 /* ARGSUSED */
10630 int
10631 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10632     ip_ioctl_cmd_t *ipip, void *if_req)
10633 {
10634 	int err = 0;
10635 	in6_addr_t v6mask;
10636 
10637 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10639 
10640 	ASSERT(IAM_WRITER_IPIF(ipif));
10641 
10642 	if (ipif->ipif_isv6) {
10643 		sin6_t *sin6;
10644 
10645 		if (sin->sin_family != AF_INET6)
10646 			return (EAFNOSUPPORT);
10647 
10648 		sin6 = (sin6_t *)sin;
10649 		v6mask = sin6->sin6_addr;
10650 	} else {
10651 		ipaddr_t mask;
10652 
10653 		if (sin->sin_family != AF_INET)
10654 			return (EAFNOSUPPORT);
10655 
10656 		mask = sin->sin_addr.s_addr;
10657 		V4MASK_TO_V6(mask, v6mask);
10658 	}
10659 
10660 	/*
10661 	 * No big deal if the interface isn't already up, or the mask
10662 	 * isn't really changing, or this is pt-pt.
10663 	 */
10664 	if (!(ipif->ipif_flags & IPIF_UP) ||
10665 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10666 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10667 		ipif->ipif_v6net_mask = v6mask;
10668 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10669 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10670 			    ipif->ipif_v6net_mask,
10671 			    ipif->ipif_v6subnet);
10672 		}
10673 		return (0);
10674 	}
10675 	/*
10676 	 * Make sure we have valid net and subnet broadcast ire's
10677 	 * for the old netmask, if needed by other logical interfaces.
10678 	 */
10679 	err = ipif_logical_down(ipif, q, mp);
10680 	if (err == EINPROGRESS)
10681 		return (err);
10682 	(void) ipif_down_tail(ipif);
10683 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10684 	return (err);
10685 }
10686 
10687 static int
10688 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10689 {
10690 	in6_addr_t v6mask;
10691 	int err = 0;
10692 
10693 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10695 
10696 	if (ipif->ipif_isv6) {
10697 		sin6_t *sin6;
10698 
10699 		sin6 = (sin6_t *)sin;
10700 		v6mask = sin6->sin6_addr;
10701 	} else {
10702 		ipaddr_t mask;
10703 
10704 		mask = sin->sin_addr.s_addr;
10705 		V4MASK_TO_V6(mask, v6mask);
10706 	}
10707 
10708 	ipif->ipif_v6net_mask = v6mask;
10709 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10710 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10711 		    ipif->ipif_v6subnet);
10712 	}
10713 	err = ipif_up(ipif, q, mp);
10714 
10715 	if (err == 0 || err == EINPROGRESS) {
10716 		/*
10717 		 * The interface must be DL_BOUND if this packet has to
10718 		 * go out on the wire. Since we only go through a logical
10719 		 * down and are bound with the driver during an internal
10720 		 * down/up that is satisfied.
10721 		 */
10722 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10723 			/* Potentially broadcast an address mask reply. */
10724 			ipif_mask_reply(ipif);
10725 		}
10726 	}
10727 	return (err);
10728 }
10729 
10730 /* ARGSUSED */
10731 int
10732 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10733     ip_ioctl_cmd_t *ipip, void *if_req)
10734 {
10735 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10736 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10737 	(void) ipif_down_tail(ipif);
10738 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10739 }
10740 
10741 /* Get interface net mask. */
10742 /* ARGSUSED */
10743 int
10744 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10745     ip_ioctl_cmd_t *ipip, void *if_req)
10746 {
10747 	struct lifreq *lifr = (struct lifreq *)if_req;
10748 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10749 
10750 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10751 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10752 
10753 	/*
10754 	 * net mask can't change since we have a reference to the ipif.
10755 	 */
10756 	if (ipif->ipif_isv6) {
10757 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10758 		*sin6 = sin6_null;
10759 		sin6->sin6_family = AF_INET6;
10760 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10761 		lifr->lifr_addrlen =
10762 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10763 	} else {
10764 		*sin = sin_null;
10765 		sin->sin_family = AF_INET;
10766 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10767 		if (ipip->ipi_cmd_type == LIF_CMD) {
10768 			lifr->lifr_addrlen =
10769 			    ip_mask_to_plen(ipif->ipif_net_mask);
10770 		}
10771 	}
10772 	return (0);
10773 }
10774 
10775 /* ARGSUSED */
10776 int
10777 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10778     ip_ioctl_cmd_t *ipip, void *if_req)
10779 {
10780 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10782 
10783 	/*
10784 	 * Since no applications should ever be setting metrics on underlying
10785 	 * interfaces, we explicitly fail to smoke 'em out.
10786 	 */
10787 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10788 		return (EINVAL);
10789 
10790 	/*
10791 	 * Set interface metric.  We don't use this for
10792 	 * anything but we keep track of it in case it is
10793 	 * important to routing applications or such.
10794 	 */
10795 	if (ipip->ipi_cmd_type == IF_CMD) {
10796 		struct ifreq    *ifr;
10797 
10798 		ifr = (struct ifreq *)if_req;
10799 		ipif->ipif_metric = ifr->ifr_metric;
10800 	} else {
10801 		struct lifreq   *lifr;
10802 
10803 		lifr = (struct lifreq *)if_req;
10804 		ipif->ipif_metric = lifr->lifr_metric;
10805 	}
10806 	return (0);
10807 }
10808 
10809 /* ARGSUSED */
10810 int
10811 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10812     ip_ioctl_cmd_t *ipip, void *if_req)
10813 {
10814 	/* Get interface metric. */
10815 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10817 
10818 	if (ipip->ipi_cmd_type == IF_CMD) {
10819 		struct ifreq    *ifr;
10820 
10821 		ifr = (struct ifreq *)if_req;
10822 		ifr->ifr_metric = ipif->ipif_metric;
10823 	} else {
10824 		struct lifreq   *lifr;
10825 
10826 		lifr = (struct lifreq *)if_req;
10827 		lifr->lifr_metric = ipif->ipif_metric;
10828 	}
10829 
10830 	return (0);
10831 }
10832 
10833 /* ARGSUSED */
10834 int
10835 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10836     ip_ioctl_cmd_t *ipip, void *if_req)
10837 {
10838 	int	arp_muxid;
10839 
10840 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10841 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10842 	/*
10843 	 * Set the muxid returned from I_PLINK.
10844 	 */
10845 	if (ipip->ipi_cmd_type == IF_CMD) {
10846 		struct ifreq *ifr = (struct ifreq *)if_req;
10847 
10848 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10849 		arp_muxid = ifr->ifr_arp_muxid;
10850 	} else {
10851 		struct lifreq *lifr = (struct lifreq *)if_req;
10852 
10853 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10854 		arp_muxid = lifr->lifr_arp_muxid;
10855 	}
10856 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10857 	return (0);
10858 }
10859 
10860 /* ARGSUSED */
10861 int
10862 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10863     ip_ioctl_cmd_t *ipip, void *if_req)
10864 {
10865 	int	arp_muxid = 0;
10866 
10867 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10868 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10869 	/*
10870 	 * Get the muxid saved in ill for I_PUNLINK.
10871 	 */
10872 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10873 	if (ipip->ipi_cmd_type == IF_CMD) {
10874 		struct ifreq *ifr = (struct ifreq *)if_req;
10875 
10876 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10877 		ifr->ifr_arp_muxid = arp_muxid;
10878 	} else {
10879 		struct lifreq *lifr = (struct lifreq *)if_req;
10880 
10881 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10882 		lifr->lifr_arp_muxid = arp_muxid;
10883 	}
10884 	return (0);
10885 }
10886 
10887 /*
10888  * Set the subnet prefix. Does not modify the broadcast address.
10889  */
10890 /* ARGSUSED */
10891 int
10892 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10893     ip_ioctl_cmd_t *ipip, void *if_req)
10894 {
10895 	int err = 0;
10896 	in6_addr_t v6addr;
10897 	in6_addr_t v6mask;
10898 	boolean_t need_up = B_FALSE;
10899 	int addrlen;
10900 
10901 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10902 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10903 
10904 	ASSERT(IAM_WRITER_IPIF(ipif));
10905 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10906 
10907 	if (ipif->ipif_isv6) {
10908 		sin6_t *sin6;
10909 
10910 		if (sin->sin_family != AF_INET6)
10911 			return (EAFNOSUPPORT);
10912 
10913 		sin6 = (sin6_t *)sin;
10914 		v6addr = sin6->sin6_addr;
10915 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10916 			return (EADDRNOTAVAIL);
10917 	} else {
10918 		ipaddr_t addr;
10919 
10920 		if (sin->sin_family != AF_INET)
10921 			return (EAFNOSUPPORT);
10922 
10923 		addr = sin->sin_addr.s_addr;
10924 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10925 			return (EADDRNOTAVAIL);
10926 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10927 		/* Add 96 bits */
10928 		addrlen += IPV6_ABITS - IP_ABITS;
10929 	}
10930 
10931 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10932 		return (EINVAL);
10933 
10934 	/* Check if bits in the address is set past the mask */
10935 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10936 		return (EINVAL);
10937 
10938 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10939 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10940 		return (0);	/* No change */
10941 
10942 	if (ipif->ipif_flags & IPIF_UP) {
10943 		/*
10944 		 * If the interface is already marked up,
10945 		 * we call ipif_down which will take care
10946 		 * of ditching any IREs that have been set
10947 		 * up based on the old interface address.
10948 		 */
10949 		err = ipif_logical_down(ipif, q, mp);
10950 		if (err == EINPROGRESS)
10951 			return (err);
10952 		(void) ipif_down_tail(ipif);
10953 		need_up = B_TRUE;
10954 	}
10955 
10956 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10957 	return (err);
10958 }
10959 
10960 static int
10961 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10962     queue_t *q, mblk_t *mp, boolean_t need_up)
10963 {
10964 	ill_t	*ill = ipif->ipif_ill;
10965 	int	err = 0;
10966 
10967 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10968 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10969 
10970 	/* Set the new address. */
10971 	mutex_enter(&ill->ill_lock);
10972 	ipif->ipif_v6net_mask = v6mask;
10973 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10974 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10975 		    ipif->ipif_v6subnet);
10976 	}
10977 	mutex_exit(&ill->ill_lock);
10978 
10979 	if (need_up) {
10980 		/*
10981 		 * Now bring the interface back up.  If this
10982 		 * is the only IPIF for the ILL, ipif_up
10983 		 * will have to re-bind to the device, so
10984 		 * we may get back EINPROGRESS, in which
10985 		 * case, this IOCTL will get completed in
10986 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10987 		 */
10988 		err = ipif_up(ipif, q, mp);
10989 		if (err == EINPROGRESS)
10990 			return (err);
10991 	}
10992 	return (err);
10993 }
10994 
10995 /* ARGSUSED */
10996 int
10997 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10998     ip_ioctl_cmd_t *ipip, void *if_req)
10999 {
11000 	int	addrlen;
11001 	in6_addr_t v6addr;
11002 	in6_addr_t v6mask;
11003 	struct lifreq *lifr = (struct lifreq *)if_req;
11004 
11005 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11006 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11007 	(void) ipif_down_tail(ipif);
11008 
11009 	addrlen = lifr->lifr_addrlen;
11010 	if (ipif->ipif_isv6) {
11011 		sin6_t *sin6;
11012 
11013 		sin6 = (sin6_t *)sin;
11014 		v6addr = sin6->sin6_addr;
11015 	} else {
11016 		ipaddr_t addr;
11017 
11018 		addr = sin->sin_addr.s_addr;
11019 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11020 		addrlen += IPV6_ABITS - IP_ABITS;
11021 	}
11022 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11023 
11024 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11025 }
11026 
11027 /* ARGSUSED */
11028 int
11029 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11030     ip_ioctl_cmd_t *ipip, void *if_req)
11031 {
11032 	struct lifreq *lifr = (struct lifreq *)if_req;
11033 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11034 
11035 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11036 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11037 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11038 
11039 	if (ipif->ipif_isv6) {
11040 		*sin6 = sin6_null;
11041 		sin6->sin6_family = AF_INET6;
11042 		sin6->sin6_addr = ipif->ipif_v6subnet;
11043 		lifr->lifr_addrlen =
11044 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11045 	} else {
11046 		*sin = sin_null;
11047 		sin->sin_family = AF_INET;
11048 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11049 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11050 	}
11051 	return (0);
11052 }
11053 
11054 /*
11055  * Set the IPv6 address token.
11056  */
11057 /* ARGSUSED */
11058 int
11059 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11060     ip_ioctl_cmd_t *ipi, void *if_req)
11061 {
11062 	ill_t *ill = ipif->ipif_ill;
11063 	int err;
11064 	in6_addr_t v6addr;
11065 	in6_addr_t v6mask;
11066 	boolean_t need_up = B_FALSE;
11067 	int i;
11068 	sin6_t *sin6 = (sin6_t *)sin;
11069 	struct lifreq *lifr = (struct lifreq *)if_req;
11070 	int addrlen;
11071 
11072 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11073 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11074 	ASSERT(IAM_WRITER_IPIF(ipif));
11075 
11076 	addrlen = lifr->lifr_addrlen;
11077 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11078 	if (ipif->ipif_id != 0)
11079 		return (EINVAL);
11080 
11081 	if (!ipif->ipif_isv6)
11082 		return (EINVAL);
11083 
11084 	if (addrlen > IPV6_ABITS)
11085 		return (EINVAL);
11086 
11087 	v6addr = sin6->sin6_addr;
11088 
11089 	/*
11090 	 * The length of the token is the length from the end.  To get
11091 	 * the proper mask for this, compute the mask of the bits not
11092 	 * in the token; ie. the prefix, and then xor to get the mask.
11093 	 */
11094 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11095 		return (EINVAL);
11096 	for (i = 0; i < 4; i++) {
11097 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11098 	}
11099 
11100 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11101 	    ill->ill_token_length == addrlen)
11102 		return (0);	/* No change */
11103 
11104 	if (ipif->ipif_flags & IPIF_UP) {
11105 		err = ipif_logical_down(ipif, q, mp);
11106 		if (err == EINPROGRESS)
11107 			return (err);
11108 		(void) ipif_down_tail(ipif);
11109 		need_up = B_TRUE;
11110 	}
11111 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11112 	return (err);
11113 }
11114 
11115 static int
11116 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11117     mblk_t *mp, boolean_t need_up)
11118 {
11119 	in6_addr_t v6addr;
11120 	in6_addr_t v6mask;
11121 	ill_t	*ill = ipif->ipif_ill;
11122 	int	i;
11123 	int	err = 0;
11124 
11125 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11127 	v6addr = sin6->sin6_addr;
11128 	/*
11129 	 * The length of the token is the length from the end.  To get
11130 	 * the proper mask for this, compute the mask of the bits not
11131 	 * in the token; ie. the prefix, and then xor to get the mask.
11132 	 */
11133 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11134 	for (i = 0; i < 4; i++)
11135 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11136 
11137 	mutex_enter(&ill->ill_lock);
11138 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11139 	ill->ill_token_length = addrlen;
11140 	ill->ill_manual_token = 1;
11141 
11142 	/* Reconfigure the link-local address based on this new token */
11143 	ipif_setlinklocal(ill->ill_ipif);
11144 
11145 	mutex_exit(&ill->ill_lock);
11146 
11147 	if (need_up) {
11148 		/*
11149 		 * Now bring the interface back up.  If this
11150 		 * is the only IPIF for the ILL, ipif_up
11151 		 * will have to re-bind to the device, so
11152 		 * we may get back EINPROGRESS, in which
11153 		 * case, this IOCTL will get completed in
11154 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11155 		 */
11156 		err = ipif_up(ipif, q, mp);
11157 		if (err == EINPROGRESS)
11158 			return (err);
11159 	}
11160 	return (err);
11161 }
11162 
11163 /* ARGSUSED */
11164 int
11165 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11166     ip_ioctl_cmd_t *ipi, void *if_req)
11167 {
11168 	ill_t *ill;
11169 	sin6_t *sin6 = (sin6_t *)sin;
11170 	struct lifreq *lifr = (struct lifreq *)if_req;
11171 
11172 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11173 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11174 	if (ipif->ipif_id != 0)
11175 		return (EINVAL);
11176 
11177 	ill = ipif->ipif_ill;
11178 	if (!ill->ill_isv6)
11179 		return (ENXIO);
11180 
11181 	*sin6 = sin6_null;
11182 	sin6->sin6_family = AF_INET6;
11183 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11184 	sin6->sin6_addr = ill->ill_token;
11185 	lifr->lifr_addrlen = ill->ill_token_length;
11186 	return (0);
11187 }
11188 
11189 /*
11190  * Set (hardware) link specific information that might override
11191  * what was acquired through the DL_INFO_ACK.
11192  */
11193 /* ARGSUSED */
11194 int
11195 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11196     ip_ioctl_cmd_t *ipi, void *if_req)
11197 {
11198 	ill_t		*ill = ipif->ipif_ill;
11199 	int		ip_min_mtu;
11200 	struct lifreq	*lifr = (struct lifreq *)if_req;
11201 	lif_ifinfo_req_t *lir;
11202 
11203 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11204 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11205 	lir = &lifr->lifr_ifinfo;
11206 	ASSERT(IAM_WRITER_IPIF(ipif));
11207 
11208 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11209 	if (ipif->ipif_id != 0)
11210 		return (EINVAL);
11211 
11212 	/* Set interface MTU. */
11213 	if (ipif->ipif_isv6)
11214 		ip_min_mtu = IPV6_MIN_MTU;
11215 	else
11216 		ip_min_mtu = IP_MIN_MTU;
11217 
11218 	/*
11219 	 * Verify values before we set anything. Allow zero to
11220 	 * mean unspecified.
11221 	 *
11222 	 * XXX We should be able to set the user-defined lir_mtu to some value
11223 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11224 	 * ill_max_frag value tells us the max MTU that can be handled by the
11225 	 * datalink, whereas the ill_current_frag is dynamically computed for
11226 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11227 	 * since there is currently no way of distinguishing between
11228 	 * administratively fixed link mtu values (e.g., those set via
11229 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11230 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11231 	 * upper-bound.
11232 	 */
11233 	if (lir->lir_maxmtu != 0 &&
11234 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11235 	    lir->lir_maxmtu < ip_min_mtu))
11236 		return (EINVAL);
11237 	if (lir->lir_reachtime != 0 &&
11238 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11239 		return (EINVAL);
11240 	if (lir->lir_reachretrans != 0 &&
11241 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11242 		return (EINVAL);
11243 
11244 	mutex_enter(&ill->ill_lock);
11245 	/*
11246 	 * The dce and fragmentation code can handle changes to ill_mtu
11247 	 * concurrent with sending/fragmenting packets.
11248 	 */
11249 	if (lir->lir_maxmtu != 0)
11250 		ill->ill_user_mtu = lir->lir_maxmtu;
11251 
11252 	if (lir->lir_reachtime != 0)
11253 		ill->ill_reachable_time = lir->lir_reachtime;
11254 
11255 	if (lir->lir_reachretrans != 0)
11256 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11257 
11258 	ill->ill_max_hops = lir->lir_maxhops;
11259 	ill->ill_max_buf = ND_MAX_Q;
11260 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11261 		/*
11262 		 * ill_mtu is the actual interface MTU, obtained as the min
11263 		 * of user-configured mtu and the value announced by the
11264 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11265 		 * we have already made the choice of requiring
11266 		 * ill_user_mtu < ill_current_frag by the time we get here,
11267 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11268 		 * here.
11269 		 */
11270 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11271 	}
11272 	mutex_exit(&ill->ill_lock);
11273 
11274 	/*
11275 	 * Make sure all dce_generation checks find out
11276 	 * that ill_mtu has changed.
11277 	 */
11278 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11279 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11280 
11281 	/*
11282 	 * Refresh IPMP meta-interface MTU if necessary.
11283 	 */
11284 	if (IS_UNDER_IPMP(ill))
11285 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11286 
11287 	return (0);
11288 }
11289 
11290 /* ARGSUSED */
11291 int
11292 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11293     ip_ioctl_cmd_t *ipi, void *if_req)
11294 {
11295 	struct lif_ifinfo_req *lir;
11296 	ill_t *ill = ipif->ipif_ill;
11297 
11298 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11299 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11300 	if (ipif->ipif_id != 0)
11301 		return (EINVAL);
11302 
11303 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11304 	lir->lir_maxhops = ill->ill_max_hops;
11305 	lir->lir_reachtime = ill->ill_reachable_time;
11306 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11307 	lir->lir_maxmtu = ill->ill_mtu;
11308 
11309 	return (0);
11310 }
11311 
11312 /*
11313  * Return best guess as to the subnet mask for the specified address.
11314  * Based on the subnet masks for all the configured interfaces.
11315  *
11316  * We end up returning a zero mask in the case of default, multicast or
11317  * experimental.
11318  */
11319 static ipaddr_t
11320 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11321 {
11322 	ipaddr_t net_mask;
11323 	ill_t	*ill;
11324 	ipif_t	*ipif;
11325 	ill_walk_context_t ctx;
11326 	ipif_t	*fallback_ipif = NULL;
11327 
11328 	net_mask = ip_net_mask(addr);
11329 	if (net_mask == 0) {
11330 		*ipifp = NULL;
11331 		return (0);
11332 	}
11333 
11334 	/* Let's check to see if this is maybe a local subnet route. */
11335 	/* this function only applies to IPv4 interfaces */
11336 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11337 	ill = ILL_START_WALK_V4(&ctx, ipst);
11338 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11339 		mutex_enter(&ill->ill_lock);
11340 		for (ipif = ill->ill_ipif; ipif != NULL;
11341 		    ipif = ipif->ipif_next) {
11342 			if (IPIF_IS_CONDEMNED(ipif))
11343 				continue;
11344 			if (!(ipif->ipif_flags & IPIF_UP))
11345 				continue;
11346 			if ((ipif->ipif_subnet & net_mask) ==
11347 			    (addr & net_mask)) {
11348 				/*
11349 				 * Don't trust pt-pt interfaces if there are
11350 				 * other interfaces.
11351 				 */
11352 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11353 					if (fallback_ipif == NULL) {
11354 						ipif_refhold_locked(ipif);
11355 						fallback_ipif = ipif;
11356 					}
11357 					continue;
11358 				}
11359 
11360 				/*
11361 				 * Fine. Just assume the same net mask as the
11362 				 * directly attached subnet interface is using.
11363 				 */
11364 				ipif_refhold_locked(ipif);
11365 				mutex_exit(&ill->ill_lock);
11366 				rw_exit(&ipst->ips_ill_g_lock);
11367 				if (fallback_ipif != NULL)
11368 					ipif_refrele(fallback_ipif);
11369 				*ipifp = ipif;
11370 				return (ipif->ipif_net_mask);
11371 			}
11372 		}
11373 		mutex_exit(&ill->ill_lock);
11374 	}
11375 	rw_exit(&ipst->ips_ill_g_lock);
11376 
11377 	*ipifp = fallback_ipif;
11378 	return ((fallback_ipif != NULL) ?
11379 	    fallback_ipif->ipif_net_mask : net_mask);
11380 }
11381 
11382 /*
11383  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11384  */
11385 static void
11386 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11387 {
11388 	IOCP	iocp;
11389 	ipft_t	*ipft;
11390 	ipllc_t	*ipllc;
11391 	mblk_t	*mp1;
11392 	cred_t	*cr;
11393 	int	error = 0;
11394 	conn_t	*connp;
11395 
11396 	ip1dbg(("ip_wput_ioctl"));
11397 	iocp = (IOCP)mp->b_rptr;
11398 	mp1 = mp->b_cont;
11399 	if (mp1 == NULL) {
11400 		iocp->ioc_error = EINVAL;
11401 		mp->b_datap->db_type = M_IOCNAK;
11402 		iocp->ioc_count = 0;
11403 		qreply(q, mp);
11404 		return;
11405 	}
11406 
11407 	/*
11408 	 * These IOCTLs provide various control capabilities to
11409 	 * upstream agents such as ULPs and processes.	There
11410 	 * are currently two such IOCTLs implemented.  They
11411 	 * are used by TCP to provide update information for
11412 	 * existing IREs and to forcibly delete an IRE for a
11413 	 * host that is not responding, thereby forcing an
11414 	 * attempt at a new route.
11415 	 */
11416 	iocp->ioc_error = EINVAL;
11417 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11418 		goto done;
11419 
11420 	ipllc = (ipllc_t *)mp1->b_rptr;
11421 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11422 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11423 			break;
11424 	}
11425 	/*
11426 	 * prefer credential from mblk over ioctl;
11427 	 * see ip_sioctl_copyin_setup
11428 	 */
11429 	cr = msg_getcred(mp, NULL);
11430 	if (cr == NULL)
11431 		cr = iocp->ioc_cr;
11432 
11433 	/*
11434 	 * Refhold the conn in case the request gets queued up in some lookup
11435 	 */
11436 	ASSERT(CONN_Q(q));
11437 	connp = Q_TO_CONN(q);
11438 	CONN_INC_REF(connp);
11439 	if (ipft->ipft_pfi &&
11440 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11441 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11442 		error = (*ipft->ipft_pfi)(q,
11443 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11444 	}
11445 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11446 		/*
11447 		 * CONN_OPER_PENDING_DONE happens in the function called
11448 		 * through ipft_pfi above.
11449 		 */
11450 		return;
11451 	}
11452 
11453 	CONN_OPER_PENDING_DONE(connp);
11454 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11455 		freemsg(mp);
11456 		return;
11457 	}
11458 	iocp->ioc_error = error;
11459 
11460 done:
11461 	mp->b_datap->db_type = M_IOCACK;
11462 	if (iocp->ioc_error)
11463 		iocp->ioc_count = 0;
11464 	qreply(q, mp);
11465 }
11466 
11467 /*
11468  * Assign a unique id for the ipif. This is used by sctp_addr.c
11469  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11470  */
11471 static void
11472 ipif_assign_seqid(ipif_t *ipif)
11473 {
11474 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11475 
11476 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11477 }
11478 
11479 /*
11480  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11481  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11482  * that the clone is complete -- including the seqid -- and the expectation is
11483  * that the caller will either free or overwrite `sipif' before it's unlocked.
11484  */
11485 static void
11486 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11487 {
11488 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11489 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11490 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11491 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11492 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11493 
11494 	dipif->ipif_flags = sipif->ipif_flags;
11495 	dipif->ipif_metric = sipif->ipif_metric;
11496 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11497 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11498 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11499 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11500 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11501 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11502 
11503 	/*
11504 	 * As per the comment atop the function, we assume that these sipif
11505 	 * fields will be changed before sipif is unlocked.
11506 	 */
11507 	dipif->ipif_seqid = sipif->ipif_seqid;
11508 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11509 }
11510 
11511 /*
11512  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11513  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11514  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11515  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11516  * down (i.e., no DAD), of the same type, and unlocked.
11517  */
11518 static void
11519 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11520 {
11521 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11522 	ipxop_t *ipx = ipsq->ipsq_xop;
11523 
11524 	ASSERT(sipif != dipif);
11525 	ASSERT(sipif != virgipif);
11526 
11527 	/*
11528 	 * Grab all of the locks that protect the ipif in a defined order.
11529 	 */
11530 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11531 
11532 	ipif_clone(sipif, dipif);
11533 	if (virgipif != NULL) {
11534 		ipif_clone(virgipif, sipif);
11535 		mi_free(virgipif);
11536 	}
11537 
11538 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11539 
11540 	/*
11541 	 * Transfer ownership of the current xop, if necessary.
11542 	 */
11543 	if (ipx->ipx_current_ipif == sipif) {
11544 		ASSERT(ipx->ipx_pending_ipif == NULL);
11545 		mutex_enter(&ipx->ipx_lock);
11546 		ipx->ipx_current_ipif = dipif;
11547 		mutex_exit(&ipx->ipx_lock);
11548 	}
11549 
11550 	if (virgipif == NULL)
11551 		mi_free(sipif);
11552 }
11553 
11554 /*
11555  * checks if:
11556  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11557  *	- logical interface is within the allowed range
11558  */
11559 static int
11560 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11561 {
11562 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11563 		return (ENAMETOOLONG);
11564 
11565 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11566 		return (ERANGE);
11567 	return (0);
11568 }
11569 
11570 /*
11571  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11572  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11573  * be inserted into the first space available in the list. The value of
11574  * ipif_id will then be set to the appropriate value for its position.
11575  */
11576 static int
11577 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11578 {
11579 	ill_t *ill;
11580 	ipif_t *tipif;
11581 	ipif_t **tipifp;
11582 	int id, err;
11583 	ip_stack_t	*ipst;
11584 
11585 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11586 	    IAM_WRITER_IPIF(ipif));
11587 
11588 	ill = ipif->ipif_ill;
11589 	ASSERT(ill != NULL);
11590 	ipst = ill->ill_ipst;
11591 
11592 	/*
11593 	 * In the case of lo0:0 we already hold the ill_g_lock.
11594 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11595 	 * ipif_insert.
11596 	 */
11597 	if (acquire_g_lock)
11598 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11599 	mutex_enter(&ill->ill_lock);
11600 	id = ipif->ipif_id;
11601 	tipifp = &(ill->ill_ipif);
11602 	if (id == -1) {	/* need to find a real id */
11603 		id = 0;
11604 		while ((tipif = *tipifp) != NULL) {
11605 			ASSERT(tipif->ipif_id >= id);
11606 			if (tipif->ipif_id != id)
11607 				break; /* non-consecutive id */
11608 			id++;
11609 			tipifp = &(tipif->ipif_next);
11610 		}
11611 		if ((err = is_lifname_valid(ill, id)) != 0) {
11612 			mutex_exit(&ill->ill_lock);
11613 			if (acquire_g_lock)
11614 				rw_exit(&ipst->ips_ill_g_lock);
11615 			return (err);
11616 		}
11617 		ipif->ipif_id = id; /* assign new id */
11618 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11619 		/* we have a real id; insert ipif in the right place */
11620 		while ((tipif = *tipifp) != NULL) {
11621 			ASSERT(tipif->ipif_id != id);
11622 			if (tipif->ipif_id > id)
11623 				break; /* found correct location */
11624 			tipifp = &(tipif->ipif_next);
11625 		}
11626 	} else {
11627 		mutex_exit(&ill->ill_lock);
11628 		if (acquire_g_lock)
11629 			rw_exit(&ipst->ips_ill_g_lock);
11630 		return (err);
11631 	}
11632 
11633 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11634 
11635 	ipif->ipif_next = tipif;
11636 	*tipifp = ipif;
11637 	mutex_exit(&ill->ill_lock);
11638 	if (acquire_g_lock)
11639 		rw_exit(&ipst->ips_ill_g_lock);
11640 
11641 	return (0);
11642 }
11643 
11644 static void
11645 ipif_remove(ipif_t *ipif)
11646 {
11647 	ipif_t	**ipifp;
11648 	ill_t	*ill = ipif->ipif_ill;
11649 
11650 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11651 
11652 	mutex_enter(&ill->ill_lock);
11653 	ipifp = &ill->ill_ipif;
11654 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11655 		if (*ipifp == ipif) {
11656 			*ipifp = ipif->ipif_next;
11657 			break;
11658 		}
11659 	}
11660 	mutex_exit(&ill->ill_lock);
11661 }
11662 
11663 /*
11664  * Allocate and initialize a new interface control structure.  (Always
11665  * called as writer.)
11666  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11667  * is not part of the global linked list of ills. ipif_seqid is unique
11668  * in the system and to preserve the uniqueness, it is assigned only
11669  * when ill becomes part of the global list. At that point ill will
11670  * have a name. If it doesn't get assigned here, it will get assigned
11671  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11672  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11673  * the interface flags or any other information from the DL_INFO_ACK for
11674  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11675  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11676  * second DL_INFO_ACK comes in from the driver.
11677  */
11678 static ipif_t *
11679 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11680     boolean_t insert, int *errorp)
11681 {
11682 	int err;
11683 	ipif_t	*ipif;
11684 	ip_stack_t *ipst = ill->ill_ipst;
11685 
11686 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11687 	    ill->ill_name, id, (void *)ill));
11688 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11689 
11690 	if (errorp != NULL)
11691 		*errorp = 0;
11692 
11693 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11694 		if (errorp != NULL)
11695 			*errorp = ENOMEM;
11696 		return (NULL);
11697 	}
11698 	*ipif = ipif_zero;	/* start clean */
11699 
11700 	ipif->ipif_ill = ill;
11701 	ipif->ipif_id = id;	/* could be -1 */
11702 	/*
11703 	 * Inherit the zoneid from the ill; for the shared stack instance
11704 	 * this is always the global zone
11705 	 */
11706 	ipif->ipif_zoneid = ill->ill_zoneid;
11707 
11708 	ipif->ipif_refcnt = 0;
11709 
11710 	if (insert) {
11711 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11712 			mi_free(ipif);
11713 			if (errorp != NULL)
11714 				*errorp = err;
11715 			return (NULL);
11716 		}
11717 		/* -1 id should have been replaced by real id */
11718 		id = ipif->ipif_id;
11719 		ASSERT(id >= 0);
11720 	}
11721 
11722 	if (ill->ill_name[0] != '\0')
11723 		ipif_assign_seqid(ipif);
11724 
11725 	/*
11726 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11727 	 * (which must not exist yet because the zeroth ipif is created once
11728 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11729 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11730 	 */
11731 	if (id == 0 && IS_IPMP(ill)) {
11732 		if (ipmp_illgrp_create(ill) == NULL) {
11733 			if (insert) {
11734 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11735 				ipif_remove(ipif);
11736 				rw_exit(&ipst->ips_ill_g_lock);
11737 			}
11738 			mi_free(ipif);
11739 			if (errorp != NULL)
11740 				*errorp = ENOMEM;
11741 			return (NULL);
11742 		}
11743 	}
11744 
11745 	/*
11746 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11747 	 * not up and can't be looked up until the ioctl completes and the
11748 	 * IPIF_CHANGING flag is cleared.
11749 	 */
11750 	mutex_enter(&ill->ill_lock);
11751 
11752 	ipif->ipif_ire_type = ire_type;
11753 
11754 	if (ipif->ipif_isv6) {
11755 		ill->ill_flags |= ILLF_IPV6;
11756 	} else {
11757 		ipaddr_t inaddr_any = INADDR_ANY;
11758 
11759 		ill->ill_flags |= ILLF_IPV4;
11760 
11761 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11762 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11763 		    &ipif->ipif_v6lcl_addr);
11764 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11765 		    &ipif->ipif_v6subnet);
11766 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11767 		    &ipif->ipif_v6net_mask);
11768 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11769 		    &ipif->ipif_v6brd_addr);
11770 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11771 		    &ipif->ipif_v6pp_dst_addr);
11772 	}
11773 
11774 	/*
11775 	 * Don't set the interface flags etc. now, will do it in
11776 	 * ip_ll_subnet_defaults.
11777 	 */
11778 	if (!initialize)
11779 		goto out;
11780 
11781 	/*
11782 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11783 	 * with no underlying interfaces (and thus an unknown broadcast
11784 	 * address length), but all interfaces that can be placed into an IPMP
11785 	 * group are required to be broadcast-capable.
11786 	 */
11787 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11788 		/*
11789 		 * Later detect lack of DLPI driver multicast capability by
11790 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11791 		 */
11792 		ill->ill_flags |= ILLF_MULTICAST;
11793 		if (!ipif->ipif_isv6)
11794 			ipif->ipif_flags |= IPIF_BROADCAST;
11795 	} else {
11796 		if (ill->ill_net_type != IRE_LOOPBACK) {
11797 			if (ipif->ipif_isv6)
11798 				/*
11799 				 * Note: xresolv interfaces will eventually need
11800 				 * NOARP set here as well, but that will require
11801 				 * those external resolvers to have some
11802 				 * knowledge of that flag and act appropriately.
11803 				 * Not to be changed at present.
11804 				 */
11805 				ill->ill_flags |= ILLF_NONUD;
11806 			else
11807 				ill->ill_flags |= ILLF_NOARP;
11808 		}
11809 		if (ill->ill_phys_addr_length == 0) {
11810 			if (IS_VNI(ill)) {
11811 				ipif->ipif_flags |= IPIF_NOXMIT;
11812 			} else {
11813 				/* pt-pt supports multicast. */
11814 				ill->ill_flags |= ILLF_MULTICAST;
11815 				if (ill->ill_net_type != IRE_LOOPBACK)
11816 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11817 			}
11818 		}
11819 	}
11820 out:
11821 	mutex_exit(&ill->ill_lock);
11822 	return (ipif);
11823 }
11824 
11825 /*
11826  * Remove the neighbor cache entries associated with this logical
11827  * interface.
11828  */
11829 int
11830 ipif_arp_down(ipif_t *ipif)
11831 {
11832 	ill_t	*ill = ipif->ipif_ill;
11833 	int	err = 0;
11834 
11835 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11836 	ASSERT(IAM_WRITER_IPIF(ipif));
11837 
11838 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11839 	    ill_t *, ill, ipif_t *, ipif);
11840 	ipif_nce_down(ipif);
11841 
11842 	/*
11843 	 * If this is the last ipif that is going down and there are no
11844 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11845 	 * clean up ARP completely.
11846 	 */
11847 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11848 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11849 		/*
11850 		 * If this was the last ipif on an IPMP interface, purge any
11851 		 * static ARP entries associated with it.
11852 		 */
11853 		if (IS_IPMP(ill))
11854 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11855 
11856 		/* UNBIND, DETACH */
11857 		err = arp_ll_down(ill);
11858 	}
11859 
11860 	return (err);
11861 }
11862 
11863 /*
11864  * Get the resolver set up for a new IP address.  (Always called as writer.)
11865  * Called both for IPv4 and IPv6 interfaces, though it only does some
11866  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11867  *
11868  * The enumerated value res_act tunes the behavior:
11869  * 	* Res_act_initial: set up all the resolver structures for a new
11870  *	  IP address.
11871  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11872  *	  ARP message in defense of the address.
11873  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11874  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11875  *
11876  * Returns zero on success, or an errno upon failure.
11877  */
11878 int
11879 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11880 {
11881 	ill_t		*ill = ipif->ipif_ill;
11882 	int		err;
11883 	boolean_t	was_dup;
11884 
11885 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11886 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11887 	ASSERT(IAM_WRITER_IPIF(ipif));
11888 
11889 	was_dup = B_FALSE;
11890 	if (res_act == Res_act_initial) {
11891 		ipif->ipif_addr_ready = 0;
11892 		/*
11893 		 * We're bringing an interface up here.  There's no way that we
11894 		 * should need to shut down ARP now.
11895 		 */
11896 		mutex_enter(&ill->ill_lock);
11897 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11898 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11899 			ill->ill_ipif_dup_count--;
11900 			was_dup = B_TRUE;
11901 		}
11902 		mutex_exit(&ill->ill_lock);
11903 	}
11904 	if (ipif->ipif_recovery_id != 0)
11905 		(void) untimeout(ipif->ipif_recovery_id);
11906 	ipif->ipif_recovery_id = 0;
11907 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11908 		ipif->ipif_addr_ready = 1;
11909 		return (0);
11910 	}
11911 	/* NDP will set the ipif_addr_ready flag when it's ready */
11912 	if (ill->ill_isv6)
11913 		return (0);
11914 
11915 	err = ipif_arp_up(ipif, res_act, was_dup);
11916 	return (err);
11917 }
11918 
11919 /*
11920  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11921  * when a link has just gone back up.
11922  */
11923 static void
11924 ipif_nce_start_dad(ipif_t *ipif)
11925 {
11926 	ncec_t *ncec;
11927 	ill_t *ill = ipif->ipif_ill;
11928 	boolean_t isv6 = ill->ill_isv6;
11929 
11930 	if (isv6) {
11931 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11932 		    &ipif->ipif_v6lcl_addr);
11933 	} else {
11934 		ipaddr_t v4addr;
11935 
11936 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11937 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11938 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11939 			/*
11940 			 * If we can't contact ARP for some reason,
11941 			 * that's not really a problem.  Just send
11942 			 * out the routing socket notification that
11943 			 * DAD completion would have done, and continue.
11944 			 */
11945 			ipif_mask_reply(ipif);
11946 			ipif_up_notify(ipif);
11947 			ipif->ipif_addr_ready = 1;
11948 			return;
11949 		}
11950 
11951 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11952 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11953 	}
11954 
11955 	if (ncec == NULL) {
11956 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11957 		    (void *)ipif));
11958 		return;
11959 	}
11960 	if (!nce_restart_dad(ncec)) {
11961 		/*
11962 		 * If we can't restart DAD for some reason, that's not really a
11963 		 * problem.  Just send out the routing socket notification that
11964 		 * DAD completion would have done, and continue.
11965 		 */
11966 		ipif_up_notify(ipif);
11967 		ipif->ipif_addr_ready = 1;
11968 	}
11969 	ncec_refrele(ncec);
11970 }
11971 
11972 /*
11973  * Restart duplicate address detection on all interfaces on the given ill.
11974  *
11975  * This is called when an interface transitions from down to up
11976  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11977  *
11978  * Note that since the underlying physical link has transitioned, we must cause
11979  * at least one routing socket message to be sent here, either via DAD
11980  * completion or just by default on the first ipif.  (If we don't do this, then
11981  * in.mpathd will see long delays when doing link-based failure recovery.)
11982  */
11983 void
11984 ill_restart_dad(ill_t *ill, boolean_t went_up)
11985 {
11986 	ipif_t *ipif;
11987 
11988 	if (ill == NULL)
11989 		return;
11990 
11991 	/*
11992 	 * If layer two doesn't support duplicate address detection, then just
11993 	 * send the routing socket message now and be done with it.
11994 	 */
11995 	if (!ill->ill_isv6 && arp_no_defense) {
11996 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11997 		return;
11998 	}
11999 
12000 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12001 		if (went_up) {
12002 
12003 			if (ipif->ipif_flags & IPIF_UP) {
12004 				ipif_nce_start_dad(ipif);
12005 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12006 				/*
12007 				 * kick off the bring-up process now.
12008 				 */
12009 				ipif_do_recovery(ipif);
12010 			} else {
12011 				/*
12012 				 * Unfortunately, the first ipif is "special"
12013 				 * and represents the underlying ill in the
12014 				 * routing socket messages.  Thus, when this
12015 				 * one ipif is down, we must still notify so
12016 				 * that the user knows the IFF_RUNNING status
12017 				 * change.  (If the first ipif is up, then
12018 				 * we'll handle eventual routing socket
12019 				 * notification via DAD completion.)
12020 				 */
12021 				if (ipif == ill->ill_ipif) {
12022 					ip_rts_ifmsg(ill->ill_ipif,
12023 					    RTSQ_DEFAULT);
12024 				}
12025 			}
12026 		} else {
12027 			/*
12028 			 * After link down, we'll need to send a new routing
12029 			 * message when the link comes back, so clear
12030 			 * ipif_addr_ready.
12031 			 */
12032 			ipif->ipif_addr_ready = 0;
12033 		}
12034 	}
12035 
12036 	/*
12037 	 * If we've torn down links, then notify the user right away.
12038 	 */
12039 	if (!went_up)
12040 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12041 }
12042 
12043 static void
12044 ipsq_delete(ipsq_t *ipsq)
12045 {
12046 	ipxop_t *ipx = ipsq->ipsq_xop;
12047 
12048 	ipsq->ipsq_ipst = NULL;
12049 	ASSERT(ipsq->ipsq_phyint == NULL);
12050 	ASSERT(ipsq->ipsq_xop != NULL);
12051 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12052 	ASSERT(ipx->ipx_pending_mp == NULL);
12053 	kmem_free(ipsq, sizeof (ipsq_t));
12054 }
12055 
12056 static int
12057 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12058 {
12059 	int err = 0;
12060 	ipif_t *ipif;
12061 
12062 	if (ill == NULL)
12063 		return (0);
12064 
12065 	ASSERT(IAM_WRITER_ILL(ill));
12066 	ill->ill_up_ipifs = B_TRUE;
12067 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12068 		if (ipif->ipif_was_up) {
12069 			if (!(ipif->ipif_flags & IPIF_UP))
12070 				err = ipif_up(ipif, q, mp);
12071 			ipif->ipif_was_up = B_FALSE;
12072 			if (err != 0) {
12073 				ASSERT(err == EINPROGRESS);
12074 				return (err);
12075 			}
12076 		}
12077 	}
12078 	ill->ill_up_ipifs = B_FALSE;
12079 	return (0);
12080 }
12081 
12082 /*
12083  * This function is called to bring up all the ipifs that were up before
12084  * bringing the ill down via ill_down_ipifs().
12085  */
12086 int
12087 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12088 {
12089 	int err;
12090 
12091 	ASSERT(IAM_WRITER_ILL(ill));
12092 
12093 	if (ill->ill_replumbing) {
12094 		ill->ill_replumbing = 0;
12095 		/*
12096 		 * Send down REPLUMB_DONE notification followed by the
12097 		 * BIND_REQ on the arp stream.
12098 		 */
12099 		if (!ill->ill_isv6)
12100 			arp_send_replumb_conf(ill);
12101 	}
12102 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12103 	if (err != 0)
12104 		return (err);
12105 
12106 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12107 }
12108 
12109 /*
12110  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12111  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12112  */
12113 static void
12114 ill_down_ipifs(ill_t *ill, boolean_t logical)
12115 {
12116 	ipif_t *ipif;
12117 
12118 	ASSERT(IAM_WRITER_ILL(ill));
12119 
12120 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12121 		/*
12122 		 * We go through the ipif_down logic even if the ipif
12123 		 * is already down, since routes can be added based
12124 		 * on down ipifs. Going through ipif_down once again
12125 		 * will delete any IREs created based on these routes.
12126 		 */
12127 		if (ipif->ipif_flags & IPIF_UP)
12128 			ipif->ipif_was_up = B_TRUE;
12129 
12130 		if (logical) {
12131 			(void) ipif_logical_down(ipif, NULL, NULL);
12132 			ipif_non_duplicate(ipif);
12133 			(void) ipif_down_tail(ipif);
12134 		} else {
12135 			(void) ipif_down(ipif, NULL, NULL);
12136 		}
12137 	}
12138 }
12139 
12140 /*
12141  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12142  * a look again at valid source addresses.
12143  * This should be called each time after the set of source addresses has been
12144  * changed.
12145  */
12146 void
12147 ip_update_source_selection(ip_stack_t *ipst)
12148 {
12149 	/* We skip past SRC_GENERATION_VERIFY */
12150 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12151 	    SRC_GENERATION_VERIFY)
12152 		atomic_add_32(&ipst->ips_src_generation, 1);
12153 }
12154 
12155 /*
12156  * Finish the group join started in ip_sioctl_groupname().
12157  */
12158 /* ARGSUSED */
12159 static void
12160 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12161 {
12162 	ill_t		*ill = q->q_ptr;
12163 	phyint_t	*phyi = ill->ill_phyint;
12164 	ipmp_grp_t	*grp = phyi->phyint_grp;
12165 	ip_stack_t	*ipst = ill->ill_ipst;
12166 
12167 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12168 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12169 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12170 
12171 	if (phyi->phyint_illv4 != NULL) {
12172 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12173 		VERIFY(grp->gr_pendv4-- > 0);
12174 		rw_exit(&ipst->ips_ipmp_lock);
12175 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12176 	}
12177 	if (phyi->phyint_illv6 != NULL) {
12178 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12179 		VERIFY(grp->gr_pendv6-- > 0);
12180 		rw_exit(&ipst->ips_ipmp_lock);
12181 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12182 	}
12183 	freemsg(mp);
12184 }
12185 
12186 /*
12187  * Process an SIOCSLIFGROUPNAME request.
12188  */
12189 /* ARGSUSED */
12190 int
12191 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12192     ip_ioctl_cmd_t *ipip, void *ifreq)
12193 {
12194 	struct lifreq	*lifr = ifreq;
12195 	ill_t		*ill = ipif->ipif_ill;
12196 	ip_stack_t	*ipst = ill->ill_ipst;
12197 	phyint_t	*phyi = ill->ill_phyint;
12198 	ipmp_grp_t	*grp = phyi->phyint_grp;
12199 	mblk_t		*ipsq_mp;
12200 	int		err = 0;
12201 
12202 	/*
12203 	 * Note that phyint_grp can only change here, where we're exclusive.
12204 	 */
12205 	ASSERT(IAM_WRITER_ILL(ill));
12206 
12207 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12208 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12209 		return (EINVAL);
12210 
12211 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12212 
12213 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12214 
12215 	/*
12216 	 * If the name hasn't changed, there's nothing to do.
12217 	 */
12218 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12219 		goto unlock;
12220 
12221 	/*
12222 	 * Handle requests to rename an IPMP meta-interface.
12223 	 *
12224 	 * Note that creation of the IPMP meta-interface is handled in
12225 	 * userland through the standard plumbing sequence.  As part of the
12226 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12227 	 * the name of the interface (see ipif_set_values_tail()).
12228 	 */
12229 	if (IS_IPMP(ill)) {
12230 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12231 		goto unlock;
12232 	}
12233 
12234 	/*
12235 	 * Handle requests to add or remove an IP interface from a group.
12236 	 */
12237 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12238 		/*
12239 		 * Moves are handled by first removing the interface from
12240 		 * its existing group, and then adding it to another group.
12241 		 * So, fail if it's already in a group.
12242 		 */
12243 		if (IS_UNDER_IPMP(ill)) {
12244 			err = EALREADY;
12245 			goto unlock;
12246 		}
12247 
12248 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12249 		if (grp == NULL) {
12250 			err = ENOENT;
12251 			goto unlock;
12252 		}
12253 
12254 		/*
12255 		 * Check if the phyint and its ills are suitable for
12256 		 * inclusion into the group.
12257 		 */
12258 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12259 			goto unlock;
12260 
12261 		/*
12262 		 * Checks pass; join the group, and enqueue the remaining
12263 		 * illgrp joins for when we've become part of the group xop
12264 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12265 		 * requires an mblk_t to scribble on, and since `mp' will be
12266 		 * freed as part of completing the ioctl, allocate another.
12267 		 */
12268 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12269 			err = ENOMEM;
12270 			goto unlock;
12271 		}
12272 
12273 		/*
12274 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12275 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12276 		 * before ip_join_illgrps() is called back.  See the comments
12277 		 * in ip_sioctl_plink_ipmp() for more.
12278 		 */
12279 		if (phyi->phyint_illv4 != NULL)
12280 			grp->gr_pendv4++;
12281 		if (phyi->phyint_illv6 != NULL)
12282 			grp->gr_pendv6++;
12283 
12284 		rw_exit(&ipst->ips_ipmp_lock);
12285 
12286 		ipmp_phyint_join_grp(phyi, grp);
12287 		ill_refhold(ill);
12288 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12289 		    SWITCH_OP, B_FALSE);
12290 		return (0);
12291 	} else {
12292 		/*
12293 		 * Request to remove the interface from a group.  If the
12294 		 * interface is not in a group, this trivially succeeds.
12295 		 */
12296 		rw_exit(&ipst->ips_ipmp_lock);
12297 		if (IS_UNDER_IPMP(ill))
12298 			ipmp_phyint_leave_grp(phyi);
12299 		return (0);
12300 	}
12301 unlock:
12302 	rw_exit(&ipst->ips_ipmp_lock);
12303 	return (err);
12304 }
12305 
12306 /*
12307  * Process an SIOCGLIFBINDING request.
12308  */
12309 /* ARGSUSED */
12310 int
12311 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12312     ip_ioctl_cmd_t *ipip, void *ifreq)
12313 {
12314 	ill_t		*ill;
12315 	struct lifreq	*lifr = ifreq;
12316 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12317 
12318 	if (!IS_IPMP(ipif->ipif_ill))
12319 		return (EINVAL);
12320 
12321 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12322 	if ((ill = ipif->ipif_bound_ill) == NULL)
12323 		lifr->lifr_binding[0] = '\0';
12324 	else
12325 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12326 	rw_exit(&ipst->ips_ipmp_lock);
12327 	return (0);
12328 }
12329 
12330 /*
12331  * Process an SIOCGLIFGROUPNAME request.
12332  */
12333 /* ARGSUSED */
12334 int
12335 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12336     ip_ioctl_cmd_t *ipip, void *ifreq)
12337 {
12338 	ipmp_grp_t	*grp;
12339 	struct lifreq	*lifr = ifreq;
12340 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12341 
12342 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12343 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12344 		lifr->lifr_groupname[0] = '\0';
12345 	else
12346 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12347 	rw_exit(&ipst->ips_ipmp_lock);
12348 	return (0);
12349 }
12350 
12351 /*
12352  * Process an SIOCGLIFGROUPINFO request.
12353  */
12354 /* ARGSUSED */
12355 int
12356 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12357     ip_ioctl_cmd_t *ipip, void *dummy)
12358 {
12359 	ipmp_grp_t	*grp;
12360 	lifgroupinfo_t	*lifgr;
12361 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12362 
12363 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12364 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12365 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12366 
12367 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12368 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12369 		rw_exit(&ipst->ips_ipmp_lock);
12370 		return (ENOENT);
12371 	}
12372 	ipmp_grp_info(grp, lifgr);
12373 	rw_exit(&ipst->ips_ipmp_lock);
12374 	return (0);
12375 }
12376 
12377 static void
12378 ill_dl_down(ill_t *ill)
12379 {
12380 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12381 
12382 	/*
12383 	 * The ill is down; unbind but stay attached since we're still
12384 	 * associated with a PPA. If we have negotiated DLPI capabilites
12385 	 * with the data link service provider (IDS_OK) then reset them.
12386 	 * The interval between unbinding and rebinding is potentially
12387 	 * unbounded hence we cannot assume things will be the same.
12388 	 * The DLPI capabilities will be probed again when the data link
12389 	 * is brought up.
12390 	 */
12391 	mblk_t	*mp = ill->ill_unbind_mp;
12392 
12393 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12394 
12395 	if (!ill->ill_replumbing) {
12396 		/* Free all ilms for this ill */
12397 		update_conn_ill(ill, ill->ill_ipst);
12398 	} else {
12399 		ill_leave_multicast(ill);
12400 	}
12401 
12402 	ill->ill_unbind_mp = NULL;
12403 	if (mp != NULL) {
12404 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12405 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12406 		    ill->ill_name));
12407 		mutex_enter(&ill->ill_lock);
12408 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12409 		mutex_exit(&ill->ill_lock);
12410 		/*
12411 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12412 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12413 		 * ill_capability_dld_disable disable rightaway. If this is not
12414 		 * an unplumb operation then the disable happens on receipt of
12415 		 * the capab ack via ip_rput_dlpi_writer ->
12416 		 * ill_capability_ack_thr. In both cases the order of
12417 		 * the operations seen by DLD is capability disable followed
12418 		 * by DL_UNBIND. Also the DLD capability disable needs a
12419 		 * cv_wait'able context.
12420 		 */
12421 		if (ill->ill_state_flags & ILL_CONDEMNED)
12422 			ill_capability_dld_disable(ill);
12423 		ill_capability_reset(ill, B_FALSE);
12424 		ill_dlpi_send(ill, mp);
12425 	}
12426 	mutex_enter(&ill->ill_lock);
12427 	ill->ill_dl_up = 0;
12428 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12429 	mutex_exit(&ill->ill_lock);
12430 }
12431 
12432 void
12433 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12434 {
12435 	union DL_primitives *dlp;
12436 	t_uscalar_t prim;
12437 	boolean_t waitack = B_FALSE;
12438 
12439 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12440 
12441 	dlp = (union DL_primitives *)mp->b_rptr;
12442 	prim = dlp->dl_primitive;
12443 
12444 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12445 	    dl_primstr(prim), prim, ill->ill_name));
12446 
12447 	switch (prim) {
12448 	case DL_PHYS_ADDR_REQ:
12449 	{
12450 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12451 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12452 		break;
12453 	}
12454 	case DL_BIND_REQ:
12455 		mutex_enter(&ill->ill_lock);
12456 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12457 		mutex_exit(&ill->ill_lock);
12458 		break;
12459 	}
12460 
12461 	/*
12462 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12463 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12464 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12465 	 */
12466 	mutex_enter(&ill->ill_lock);
12467 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12468 	    (prim == DL_UNBIND_REQ)) {
12469 		ill->ill_dlpi_pending = prim;
12470 		waitack = B_TRUE;
12471 	}
12472 
12473 	mutex_exit(&ill->ill_lock);
12474 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12475 	    char *, dl_primstr(prim), ill_t *, ill);
12476 	putnext(ill->ill_wq, mp);
12477 
12478 	/*
12479 	 * There is no ack for DL_NOTIFY_CONF messages
12480 	 */
12481 	if (waitack && prim == DL_NOTIFY_CONF)
12482 		ill_dlpi_done(ill, prim);
12483 }
12484 
12485 /*
12486  * Helper function for ill_dlpi_send().
12487  */
12488 /* ARGSUSED */
12489 static void
12490 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12491 {
12492 	ill_dlpi_send(q->q_ptr, mp);
12493 }
12494 
12495 /*
12496  * Send a DLPI control message to the driver but make sure there
12497  * is only one outstanding message. Uses ill_dlpi_pending to tell
12498  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12499  * when an ACK or a NAK is received to process the next queued message.
12500  */
12501 void
12502 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12503 {
12504 	mblk_t **mpp;
12505 
12506 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12507 
12508 	/*
12509 	 * To ensure that any DLPI requests for current exclusive operation
12510 	 * are always completely sent before any DLPI messages for other
12511 	 * operations, require writer access before enqueuing.
12512 	 */
12513 	if (!IAM_WRITER_ILL(ill)) {
12514 		ill_refhold(ill);
12515 		/* qwriter_ip() does the ill_refrele() */
12516 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12517 		    NEW_OP, B_TRUE);
12518 		return;
12519 	}
12520 
12521 	mutex_enter(&ill->ill_lock);
12522 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12523 		/* Must queue message. Tail insertion */
12524 		mpp = &ill->ill_dlpi_deferred;
12525 		while (*mpp != NULL)
12526 			mpp = &((*mpp)->b_next);
12527 
12528 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12529 		    "while %s pending\n", ill->ill_name,
12530 		    dl_primstr(ill->ill_dlpi_pending)));
12531 
12532 		*mpp = mp;
12533 		mutex_exit(&ill->ill_lock);
12534 		return;
12535 	}
12536 	mutex_exit(&ill->ill_lock);
12537 	ill_dlpi_dispatch(ill, mp);
12538 }
12539 
12540 void
12541 ill_capability_send(ill_t *ill, mblk_t *mp)
12542 {
12543 	ill->ill_capab_pending_cnt++;
12544 	ill_dlpi_send(ill, mp);
12545 }
12546 
12547 void
12548 ill_capability_done(ill_t *ill)
12549 {
12550 	ASSERT(ill->ill_capab_pending_cnt != 0);
12551 
12552 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12553 
12554 	ill->ill_capab_pending_cnt--;
12555 	if (ill->ill_capab_pending_cnt == 0 &&
12556 	    ill->ill_dlpi_capab_state == IDCS_OK)
12557 		ill_capability_reset_alloc(ill);
12558 }
12559 
12560 /*
12561  * Send all deferred DLPI messages without waiting for their ACKs.
12562  */
12563 void
12564 ill_dlpi_send_deferred(ill_t *ill)
12565 {
12566 	mblk_t *mp, *nextmp;
12567 
12568 	/*
12569 	 * Clear ill_dlpi_pending so that the message is not queued in
12570 	 * ill_dlpi_send().
12571 	 */
12572 	mutex_enter(&ill->ill_lock);
12573 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12574 	mp = ill->ill_dlpi_deferred;
12575 	ill->ill_dlpi_deferred = NULL;
12576 	mutex_exit(&ill->ill_lock);
12577 
12578 	for (; mp != NULL; mp = nextmp) {
12579 		nextmp = mp->b_next;
12580 		mp->b_next = NULL;
12581 		ill_dlpi_send(ill, mp);
12582 	}
12583 }
12584 
12585 /*
12586  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12587  * or M_HANGUP
12588  */
12589 static void
12590 ill_dlpi_clear_deferred(ill_t *ill)
12591 {
12592 	mblk_t	*mp, *nextmp;
12593 
12594 	mutex_enter(&ill->ill_lock);
12595 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12596 	mp = ill->ill_dlpi_deferred;
12597 	ill->ill_dlpi_deferred = NULL;
12598 	mutex_exit(&ill->ill_lock);
12599 
12600 	for (; mp != NULL; mp = nextmp) {
12601 		nextmp = mp->b_next;
12602 		inet_freemsg(mp);
12603 	}
12604 }
12605 
12606 /*
12607  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12608  */
12609 boolean_t
12610 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12611 {
12612 	t_uscalar_t pending;
12613 
12614 	mutex_enter(&ill->ill_lock);
12615 	if (ill->ill_dlpi_pending == prim) {
12616 		mutex_exit(&ill->ill_lock);
12617 		return (B_TRUE);
12618 	}
12619 
12620 	/*
12621 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12622 	 * without waiting, so don't print any warnings in that case.
12623 	 */
12624 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12625 		mutex_exit(&ill->ill_lock);
12626 		return (B_FALSE);
12627 	}
12628 	pending = ill->ill_dlpi_pending;
12629 	mutex_exit(&ill->ill_lock);
12630 
12631 	if (pending == DL_PRIM_INVAL) {
12632 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12633 		    "received unsolicited ack for %s on %s\n",
12634 		    dl_primstr(prim), ill->ill_name);
12635 	} else {
12636 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12637 		    "received unexpected ack for %s on %s (expecting %s)\n",
12638 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12639 	}
12640 	return (B_FALSE);
12641 }
12642 
12643 /*
12644  * Complete the current DLPI operation associated with `prim' on `ill' and
12645  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12646  * operations and the ill's current exclusive IPSQ operation has finished
12647  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12648  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12649  * the comments above ipsq_current_finish() for details.
12650  */
12651 void
12652 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12653 {
12654 	mblk_t *mp;
12655 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12656 	ipxop_t *ipx = ipsq->ipsq_xop;
12657 
12658 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12659 	mutex_enter(&ill->ill_lock);
12660 
12661 	ASSERT(prim != DL_PRIM_INVAL);
12662 	ASSERT(ill->ill_dlpi_pending == prim);
12663 
12664 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12665 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12666 
12667 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12668 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12669 		if (ipx->ipx_current_done) {
12670 			mutex_enter(&ipx->ipx_lock);
12671 			ipx->ipx_current_ipif = NULL;
12672 			mutex_exit(&ipx->ipx_lock);
12673 		}
12674 		cv_signal(&ill->ill_cv);
12675 		mutex_exit(&ill->ill_lock);
12676 		return;
12677 	}
12678 
12679 	ill->ill_dlpi_deferred = mp->b_next;
12680 	mp->b_next = NULL;
12681 	mutex_exit(&ill->ill_lock);
12682 
12683 	ill_dlpi_dispatch(ill, mp);
12684 }
12685 
12686 /*
12687  * Queue a (multicast) DLPI control message to be sent to the driver by
12688  * later calling ill_dlpi_send_queued.
12689  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12690  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12691  * for the same group to race.
12692  * We send DLPI control messages in order using ill_lock.
12693  * For IPMP we should be called on the cast_ill.
12694  */
12695 void
12696 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12697 {
12698 	mblk_t **mpp;
12699 
12700 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12701 
12702 	mutex_enter(&ill->ill_lock);
12703 	/* Must queue message. Tail insertion */
12704 	mpp = &ill->ill_dlpi_deferred;
12705 	while (*mpp != NULL)
12706 		mpp = &((*mpp)->b_next);
12707 
12708 	*mpp = mp;
12709 	mutex_exit(&ill->ill_lock);
12710 }
12711 
12712 /*
12713  * Send the messages that were queued. Make sure there is only
12714  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12715  * when an ACK or a NAK is received to process the next queued message.
12716  * For IPMP we are called on the upper ill, but when send what is queued
12717  * on the cast_ill.
12718  */
12719 void
12720 ill_dlpi_send_queued(ill_t *ill)
12721 {
12722 	mblk_t	*mp;
12723 	union DL_primitives *dlp;
12724 	t_uscalar_t prim;
12725 	ill_t *release_ill = NULL;
12726 
12727 	if (IS_IPMP(ill)) {
12728 		/* On the upper IPMP ill. */
12729 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12730 		if (release_ill == NULL) {
12731 			/* Avoid ever sending anything down to the ipmpstub */
12732 			return;
12733 		}
12734 		ill = release_ill;
12735 	}
12736 	mutex_enter(&ill->ill_lock);
12737 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12738 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12739 			/* Can't send. Somebody else will send it */
12740 			mutex_exit(&ill->ill_lock);
12741 			goto done;
12742 		}
12743 		ill->ill_dlpi_deferred = mp->b_next;
12744 		mp->b_next = NULL;
12745 		if (!ill->ill_dl_up) {
12746 			/*
12747 			 * Nobody there. All multicast addresses will be
12748 			 * re-joined when we get the DL_BIND_ACK bringing the
12749 			 * interface up.
12750 			 */
12751 			freemsg(mp);
12752 			continue;
12753 		}
12754 		dlp = (union DL_primitives *)mp->b_rptr;
12755 		prim = dlp->dl_primitive;
12756 
12757 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12758 		    (prim == DL_UNBIND_REQ)) {
12759 			ill->ill_dlpi_pending = prim;
12760 		}
12761 		mutex_exit(&ill->ill_lock);
12762 
12763 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12764 		    char *, dl_primstr(prim), ill_t *, ill);
12765 		putnext(ill->ill_wq, mp);
12766 		mutex_enter(&ill->ill_lock);
12767 	}
12768 	mutex_exit(&ill->ill_lock);
12769 done:
12770 	if (release_ill != NULL)
12771 		ill_refrele(release_ill);
12772 }
12773 
12774 /*
12775  * Queue an IP (IGMP/MLD) message to be sent by IP from
12776  * ill_mcast_send_queued
12777  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12778  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12779  * group to race.
12780  * We send them in order using ill_lock.
12781  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12782  */
12783 void
12784 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12785 {
12786 	mblk_t **mpp;
12787 	ill_t *release_ill = NULL;
12788 
12789 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12790 
12791 	if (IS_IPMP(ill)) {
12792 		/* On the upper IPMP ill. */
12793 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12794 		if (release_ill == NULL) {
12795 			/* Discard instead of queuing for the ipmp interface */
12796 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12797 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12798 			    mp, ill);
12799 			freemsg(mp);
12800 			return;
12801 		}
12802 		ill = release_ill;
12803 	}
12804 
12805 	mutex_enter(&ill->ill_lock);
12806 	/* Must queue message. Tail insertion */
12807 	mpp = &ill->ill_mcast_deferred;
12808 	while (*mpp != NULL)
12809 		mpp = &((*mpp)->b_next);
12810 
12811 	*mpp = mp;
12812 	mutex_exit(&ill->ill_lock);
12813 	if (release_ill != NULL)
12814 		ill_refrele(release_ill);
12815 }
12816 
12817 /*
12818  * Send the IP packets that were queued by ill_mcast_queue.
12819  * These are IGMP/MLD packets.
12820  *
12821  * For IPMP we are called on the upper ill, but when send what is queued
12822  * on the cast_ill.
12823  *
12824  * Request loopback of the report if we are acting as a multicast
12825  * router, so that the process-level routing demon can hear it.
12826  * This will run multiple times for the same group if there are members
12827  * on the same group for multiple ipif's on the same ill. The
12828  * igmp_input/mld_input code will suppress this due to the loopback thus we
12829  * always loopback membership report.
12830  *
12831  * We also need to make sure that this does not get load balanced
12832  * by IPMP. We do this by passing an ill to ip_output_simple.
12833  */
12834 void
12835 ill_mcast_send_queued(ill_t *ill)
12836 {
12837 	mblk_t	*mp;
12838 	ip_xmit_attr_t ixas;
12839 	ill_t *release_ill = NULL;
12840 
12841 	if (IS_IPMP(ill)) {
12842 		/* On the upper IPMP ill. */
12843 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12844 		if (release_ill == NULL) {
12845 			/*
12846 			 * We should have no messages on the ipmp interface
12847 			 * but no point in trying to send them.
12848 			 */
12849 			return;
12850 		}
12851 		ill = release_ill;
12852 	}
12853 	bzero(&ixas, sizeof (ixas));
12854 	ixas.ixa_zoneid = ALL_ZONES;
12855 	ixas.ixa_cred = kcred;
12856 	ixas.ixa_cpid = NOPID;
12857 	ixas.ixa_tsl = NULL;
12858 	/*
12859 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12860 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12861 	 * That is necessary to handle IGMP/MLD snooping switches.
12862 	 */
12863 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12864 	ixas.ixa_ipst = ill->ill_ipst;
12865 
12866 	mutex_enter(&ill->ill_lock);
12867 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12868 		ill->ill_mcast_deferred = mp->b_next;
12869 		mp->b_next = NULL;
12870 		if (!ill->ill_dl_up) {
12871 			/*
12872 			 * Nobody there. Just drop the ip packets.
12873 			 * IGMP/MLD will resend later, if this is a replumb.
12874 			 */
12875 			freemsg(mp);
12876 			continue;
12877 		}
12878 		mutex_enter(&ill->ill_phyint->phyint_lock);
12879 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12880 			/*
12881 			 * When the ill is getting deactivated, we only want to
12882 			 * send the DLPI messages, so drop IGMP/MLD packets.
12883 			 * DLPI messages are handled by ill_dlpi_send_queued()
12884 			 */
12885 			mutex_exit(&ill->ill_phyint->phyint_lock);
12886 			freemsg(mp);
12887 			continue;
12888 		}
12889 		mutex_exit(&ill->ill_phyint->phyint_lock);
12890 		mutex_exit(&ill->ill_lock);
12891 
12892 		/* Check whether we are sending IPv4 or IPv6. */
12893 		if (ill->ill_isv6) {
12894 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12895 
12896 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12897 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12898 		} else {
12899 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12900 
12901 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12902 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12903 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12904 		}
12905 
12906 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12907 		(void) ip_output_simple(mp, &ixas);
12908 		ixa_cleanup(&ixas);
12909 
12910 		mutex_enter(&ill->ill_lock);
12911 	}
12912 	mutex_exit(&ill->ill_lock);
12913 
12914 done:
12915 	if (release_ill != NULL)
12916 		ill_refrele(release_ill);
12917 }
12918 
12919 /*
12920  * Take down a specific interface, but don't lose any information about it.
12921  * (Always called as writer.)
12922  * This function goes through the down sequence even if the interface is
12923  * already down. There are 2 reasons.
12924  * a. Currently we permit interface routes that depend on down interfaces
12925  *    to be added. This behaviour itself is questionable. However it appears
12926  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12927  *    time. We go thru the cleanup in order to remove these routes.
12928  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12929  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12930  *    down, but we need to cleanup i.e. do ill_dl_down and
12931  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12932  *
12933  * IP-MT notes:
12934  *
12935  * Model of reference to interfaces.
12936  *
12937  * The following members in ipif_t track references to the ipif.
12938  *	int     ipif_refcnt;    Active reference count
12939  *
12940  * The following members in ill_t track references to the ill.
12941  *	int             ill_refcnt;     active refcnt
12942  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12943  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12944  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12945  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12946  *
12947  * Reference to an ipif or ill can be obtained in any of the following ways.
12948  *
12949  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12950  * Pointers to ipif / ill from other data structures viz ire and conn.
12951  * Implicit reference to the ipif / ill by holding a reference to the ire.
12952  *
12953  * The ipif/ill lookup functions return a reference held ipif / ill.
12954  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12955  * This is a purely dynamic reference count associated with threads holding
12956  * references to the ipif / ill. Pointers from other structures do not
12957  * count towards this reference count.
12958  *
12959  * ill_ire_cnt is the number of ire's associated with the
12960  * ill. This is incremented whenever a new ire is created referencing the
12961  * ill. This is done atomically inside ire_add_v[46] where the ire is
12962  * actually added to the ire hash table. The count is decremented in
12963  * ire_inactive where the ire is destroyed.
12964  *
12965  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12966  * This is incremented atomically in
12967  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12968  * table. Similarly it is decremented in ncec_inactive() where the ncec
12969  * is destroyed.
12970  *
12971  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12972  * incremented atomically in nce_add() where the nce is actually added to the
12973  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12974  * is destroyed.
12975  *
12976  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12977  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12978  *
12979  * Flow of ioctls involving interface down/up
12980  *
12981  * The following is the sequence of an attempt to set some critical flags on an
12982  * up interface.
12983  * ip_sioctl_flags
12984  * ipif_down
12985  * wait for ipif to be quiescent
12986  * ipif_down_tail
12987  * ip_sioctl_flags_tail
12988  *
12989  * All set ioctls that involve down/up sequence would have a skeleton similar
12990  * to the above. All the *tail functions are called after the refcounts have
12991  * dropped to the appropriate values.
12992  *
12993  * SIOC ioctls during the IPIF_CHANGING interval.
12994  *
12995  * Threads handling SIOC set ioctls serialize on the squeue, but this
12996  * is not done for SIOC get ioctls. Since a set ioctl can cause several
12997  * steps of internal changes to the state, some of which are visible in
12998  * ipif_flags (such as IFF_UP being cleared and later set), and we want
12999  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13000  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13001  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13002  * the current exclusive operation completes. The IPIF_CHANGING check
13003  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13004  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13005  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13006  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13007  * until we release the ipsq_lock, even though the ill/ipif state flags
13008  * can change after we drop the ill_lock.
13009  */
13010 int
13011 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13012 {
13013 	ill_t		*ill = ipif->ipif_ill;
13014 	conn_t		*connp;
13015 	boolean_t	success;
13016 	boolean_t	ipif_was_up = B_FALSE;
13017 	ip_stack_t	*ipst = ill->ill_ipst;
13018 
13019 	ASSERT(IAM_WRITER_IPIF(ipif));
13020 
13021 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13022 
13023 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13024 	    ill_t *, ill, ipif_t *, ipif);
13025 
13026 	if (ipif->ipif_flags & IPIF_UP) {
13027 		mutex_enter(&ill->ill_lock);
13028 		ipif->ipif_flags &= ~IPIF_UP;
13029 		ASSERT(ill->ill_ipif_up_count > 0);
13030 		--ill->ill_ipif_up_count;
13031 		mutex_exit(&ill->ill_lock);
13032 		ipif_was_up = B_TRUE;
13033 		/* Update status in SCTP's list */
13034 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13035 		ill_nic_event_dispatch(ipif->ipif_ill,
13036 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13037 	}
13038 
13039 	/*
13040 	 * Blow away memberships we established in ipif_multicast_up().
13041 	 */
13042 	ipif_multicast_down(ipif);
13043 
13044 	/*
13045 	 * Remove from the mapping for __sin6_src_id. We insert only
13046 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13047 	 * stored as mapped addresses, we need to check for mapped
13048 	 * INADDR_ANY also.
13049 	 */
13050 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13051 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13052 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13053 		int err;
13054 
13055 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13056 		    ipif->ipif_zoneid, ipst);
13057 		if (err != 0) {
13058 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13059 		}
13060 	}
13061 
13062 	if (ipif_was_up) {
13063 		/* only delete if we'd added ire's before */
13064 		if (ipif->ipif_isv6)
13065 			ipif_delete_ires_v6(ipif);
13066 		else
13067 			ipif_delete_ires_v4(ipif);
13068 	}
13069 
13070 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13071 		/*
13072 		 * Since the interface is now down, it may have just become
13073 		 * inactive.  Note that this needs to be done even for a
13074 		 * lll_logical_down(), or ARP entries will not get correctly
13075 		 * restored when the interface comes back up.
13076 		 */
13077 		if (IS_UNDER_IPMP(ill))
13078 			ipmp_ill_refresh_active(ill);
13079 	}
13080 
13081 	/*
13082 	 * neighbor-discovery or arp entries for this interface. The ipif
13083 	 * has to be quiesced, so we walk all the nce's and delete those
13084 	 * that point at the ipif->ipif_ill. At the same time, we also
13085 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13086 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13087 	 * that for ipif_down_tail()
13088 	 */
13089 	ipif_nce_down(ipif);
13090 
13091 	/*
13092 	 * If this is the last ipif on the ill, we also need to remove
13093 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13094 	 * never succeed.
13095 	 */
13096 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13097 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13098 
13099 	/*
13100 	 * Walk all CONNs that can have a reference on an ire for this
13101 	 * ipif (we actually walk all that now have stale references).
13102 	 */
13103 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13104 
13105 	/*
13106 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13107 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13108 	 * and ill_delete -> ipif_free -> ipif_down
13109 	 */
13110 	if (mp == NULL) {
13111 		ASSERT(q == NULL);
13112 		return (0);
13113 	}
13114 
13115 	if (CONN_Q(q)) {
13116 		connp = Q_TO_CONN(q);
13117 		mutex_enter(&connp->conn_lock);
13118 	} else {
13119 		connp = NULL;
13120 	}
13121 	mutex_enter(&ill->ill_lock);
13122 	/*
13123 	 * Are there any ire's pointing to this ipif that are still active ?
13124 	 * If this is the last ipif going down, are there any ire's pointing
13125 	 * to this ill that are still active ?
13126 	 */
13127 	if (ipif_is_quiescent(ipif)) {
13128 		mutex_exit(&ill->ill_lock);
13129 		if (connp != NULL)
13130 			mutex_exit(&connp->conn_lock);
13131 		return (0);
13132 	}
13133 
13134 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13135 	    ill->ill_name, (void *)ill));
13136 	/*
13137 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13138 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13139 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13140 	 */
13141 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13142 	if (!success) {
13143 		/* The conn is closing. So just return */
13144 		ASSERT(connp != NULL);
13145 		mutex_exit(&ill->ill_lock);
13146 		mutex_exit(&connp->conn_lock);
13147 		return (EINTR);
13148 	}
13149 
13150 	mutex_exit(&ill->ill_lock);
13151 	if (connp != NULL)
13152 		mutex_exit(&connp->conn_lock);
13153 	return (EINPROGRESS);
13154 }
13155 
13156 int
13157 ipif_down_tail(ipif_t *ipif)
13158 {
13159 	ill_t	*ill = ipif->ipif_ill;
13160 	int	err = 0;
13161 
13162 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13163 	    ill_t *, ill, ipif_t *, ipif);
13164 
13165 	/*
13166 	 * Skip any loopback interface (null wq).
13167 	 * If this is the last logical interface on the ill
13168 	 * have ill_dl_down tell the driver we are gone (unbind)
13169 	 * Note that lun 0 can ipif_down even though
13170 	 * there are other logical units that are up.
13171 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13172 	 */
13173 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13174 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13175 	    ill->ill_dl_up) {
13176 		ill_dl_down(ill);
13177 	}
13178 	if (!ipif->ipif_isv6)
13179 		err = ipif_arp_down(ipif);
13180 
13181 	ill->ill_logical_down = 0;
13182 
13183 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13184 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13185 	return (err);
13186 }
13187 
13188 /*
13189  * Bring interface logically down without bringing the physical interface
13190  * down e.g. when the netmask is changed. This avoids long lasting link
13191  * negotiations between an ethernet interface and a certain switches.
13192  */
13193 static int
13194 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13195 {
13196 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13197 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13198 
13199 	/*
13200 	 * The ill_logical_down flag is a transient flag. It is set here
13201 	 * and is cleared once the down has completed in ipif_down_tail.
13202 	 * This flag does not indicate whether the ill stream is in the
13203 	 * DL_BOUND state with the driver. Instead this flag is used by
13204 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13205 	 * the driver. The state of the ill stream i.e. whether it is
13206 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13207 	 */
13208 	ipif->ipif_ill->ill_logical_down = 1;
13209 	return (ipif_down(ipif, q, mp));
13210 }
13211 
13212 /*
13213  * Initiate deallocate of an IPIF. Always called as writer. Called by
13214  * ill_delete or ip_sioctl_removeif.
13215  */
13216 static void
13217 ipif_free(ipif_t *ipif)
13218 {
13219 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13220 
13221 	ASSERT(IAM_WRITER_IPIF(ipif));
13222 
13223 	if (ipif->ipif_recovery_id != 0)
13224 		(void) untimeout(ipif->ipif_recovery_id);
13225 	ipif->ipif_recovery_id = 0;
13226 
13227 	/*
13228 	 * Take down the interface. We can be called either from ill_delete
13229 	 * or from ip_sioctl_removeif.
13230 	 */
13231 	(void) ipif_down(ipif, NULL, NULL);
13232 
13233 	/*
13234 	 * Now that the interface is down, there's no chance it can still
13235 	 * become a duplicate.  Cancel any timer that may have been set while
13236 	 * tearing down.
13237 	 */
13238 	if (ipif->ipif_recovery_id != 0)
13239 		(void) untimeout(ipif->ipif_recovery_id);
13240 	ipif->ipif_recovery_id = 0;
13241 
13242 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13243 	/* Remove pointers to this ill in the multicast routing tables */
13244 	reset_mrt_vif_ipif(ipif);
13245 	/* If necessary, clear the cached source ipif rotor. */
13246 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13247 		ipif->ipif_ill->ill_src_ipif = NULL;
13248 	rw_exit(&ipst->ips_ill_g_lock);
13249 }
13250 
13251 static void
13252 ipif_free_tail(ipif_t *ipif)
13253 {
13254 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13255 
13256 	/*
13257 	 * Need to hold both ill_g_lock and ill_lock while
13258 	 * inserting or removing an ipif from the linked list
13259 	 * of ipifs hanging off the ill.
13260 	 */
13261 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13262 
13263 #ifdef DEBUG
13264 	ipif_trace_cleanup(ipif);
13265 #endif
13266 
13267 	/* Ask SCTP to take it out of it list */
13268 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13269 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13270 
13271 	/* Get it out of the ILL interface list. */
13272 	ipif_remove(ipif);
13273 	rw_exit(&ipst->ips_ill_g_lock);
13274 
13275 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13276 	ASSERT(ipif->ipif_recovery_id == 0);
13277 	ASSERT(ipif->ipif_ire_local == NULL);
13278 	ASSERT(ipif->ipif_ire_if == NULL);
13279 
13280 	/* Free the memory. */
13281 	mi_free(ipif);
13282 }
13283 
13284 /*
13285  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13286  * is zero.
13287  */
13288 void
13289 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13290 {
13291 	char	lbuf[LIFNAMSIZ];
13292 	char	*name;
13293 	size_t	name_len;
13294 
13295 	buf[0] = '\0';
13296 	name = ipif->ipif_ill->ill_name;
13297 	name_len = ipif->ipif_ill->ill_name_length;
13298 	if (ipif->ipif_id != 0) {
13299 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13300 		    ipif->ipif_id);
13301 		name = lbuf;
13302 		name_len = mi_strlen(name) + 1;
13303 	}
13304 	len -= 1;
13305 	buf[len] = '\0';
13306 	len = MIN(len, name_len);
13307 	bcopy(name, buf, len);
13308 }
13309 
13310 /*
13311  * Sets `buf' to an ill name.
13312  */
13313 void
13314 ill_get_name(const ill_t *ill, char *buf, int len)
13315 {
13316 	char	*name;
13317 	size_t	name_len;
13318 
13319 	name = ill->ill_name;
13320 	name_len = ill->ill_name_length;
13321 	len -= 1;
13322 	buf[len] = '\0';
13323 	len = MIN(len, name_len);
13324 	bcopy(name, buf, len);
13325 }
13326 
13327 /*
13328  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13329  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13330  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13331  * (May be called as writer.)
13332  */
13333 static ipif_t *
13334 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13335     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13336 {
13337 	char	*cp;
13338 	char	*endp;
13339 	long	id;
13340 	ill_t	*ill;
13341 	ipif_t	*ipif;
13342 	uint_t	ire_type;
13343 	boolean_t did_alloc = B_FALSE;
13344 
13345 	/*
13346 	 * If the caller wants to us to create the ipif, make sure we have a
13347 	 * valid zoneid
13348 	 */
13349 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13350 
13351 	if (namelen == 0) {
13352 		return (NULL);
13353 	}
13354 
13355 	*exists = B_FALSE;
13356 	/* Look for a colon in the name. */
13357 	endp = &name[namelen];
13358 	for (cp = endp; --cp > name; ) {
13359 		if (*cp == IPIF_SEPARATOR_CHAR)
13360 			break;
13361 	}
13362 
13363 	if (*cp == IPIF_SEPARATOR_CHAR) {
13364 		/*
13365 		 * Reject any non-decimal aliases for logical
13366 		 * interfaces. Aliases with leading zeroes
13367 		 * are also rejected as they introduce ambiguity
13368 		 * in the naming of the interfaces.
13369 		 * In order to confirm with existing semantics,
13370 		 * and to not break any programs/script relying
13371 		 * on that behaviour, if<0>:0 is considered to be
13372 		 * a valid interface.
13373 		 *
13374 		 * If alias has two or more digits and the first
13375 		 * is zero, fail.
13376 		 */
13377 		if (&cp[2] < endp && cp[1] == '0') {
13378 			return (NULL);
13379 		}
13380 	}
13381 
13382 	if (cp <= name) {
13383 		cp = endp;
13384 	} else {
13385 		*cp = '\0';
13386 	}
13387 
13388 	/*
13389 	 * Look up the ILL, based on the portion of the name
13390 	 * before the slash. ill_lookup_on_name returns a held ill.
13391 	 * Temporary to check whether ill exists already. If so
13392 	 * ill_lookup_on_name will clear it.
13393 	 */
13394 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13395 	    &did_alloc, ipst);
13396 	if (cp != endp)
13397 		*cp = IPIF_SEPARATOR_CHAR;
13398 	if (ill == NULL)
13399 		return (NULL);
13400 
13401 	/* Establish the unit number in the name. */
13402 	id = 0;
13403 	if (cp < endp && *endp == '\0') {
13404 		/* If there was a colon, the unit number follows. */
13405 		cp++;
13406 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13407 			ill_refrele(ill);
13408 			return (NULL);
13409 		}
13410 	}
13411 
13412 	mutex_enter(&ill->ill_lock);
13413 	/* Now see if there is an IPIF with this unit number. */
13414 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13415 		if (ipif->ipif_id == id) {
13416 			if (zoneid != ALL_ZONES &&
13417 			    zoneid != ipif->ipif_zoneid &&
13418 			    ipif->ipif_zoneid != ALL_ZONES) {
13419 				mutex_exit(&ill->ill_lock);
13420 				ill_refrele(ill);
13421 				return (NULL);
13422 			}
13423 			if (IPIF_CAN_LOOKUP(ipif)) {
13424 				ipif_refhold_locked(ipif);
13425 				mutex_exit(&ill->ill_lock);
13426 				if (!did_alloc)
13427 					*exists = B_TRUE;
13428 				/*
13429 				 * Drop locks before calling ill_refrele
13430 				 * since it can potentially call into
13431 				 * ipif_ill_refrele_tail which can end up
13432 				 * in trying to acquire any lock.
13433 				 */
13434 				ill_refrele(ill);
13435 				return (ipif);
13436 			}
13437 		}
13438 	}
13439 
13440 	if (!do_alloc) {
13441 		mutex_exit(&ill->ill_lock);
13442 		ill_refrele(ill);
13443 		return (NULL);
13444 	}
13445 
13446 	/*
13447 	 * If none found, atomically allocate and return a new one.
13448 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13449 	 * to support "receive only" use of lo0:1 etc. as is still done
13450 	 * below as an initial guess.
13451 	 * However, this is now likely to be overriden later in ipif_up_done()
13452 	 * when we know for sure what address has been configured on the
13453 	 * interface, since we might have more than one loopback interface
13454 	 * with a loopback address, e.g. in the case of zones, and all the
13455 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13456 	 */
13457 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13458 		ire_type = IRE_LOOPBACK;
13459 	else
13460 		ire_type = IRE_LOCAL;
13461 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13462 	if (ipif != NULL)
13463 		ipif_refhold_locked(ipif);
13464 	mutex_exit(&ill->ill_lock);
13465 	ill_refrele(ill);
13466 	return (ipif);
13467 }
13468 
13469 /*
13470  * This routine is called whenever a new address comes up on an ipif.  If
13471  * we are configured to respond to address mask requests, then we are supposed
13472  * to broadcast an address mask reply at this time.  This routine is also
13473  * called if we are already up, but a netmask change is made.  This is legal
13474  * but might not make the system manager very popular.	(May be called
13475  * as writer.)
13476  */
13477 void
13478 ipif_mask_reply(ipif_t *ipif)
13479 {
13480 	icmph_t	*icmph;
13481 	ipha_t	*ipha;
13482 	mblk_t	*mp;
13483 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13484 	ip_xmit_attr_t ixas;
13485 
13486 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13487 
13488 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13489 		return;
13490 
13491 	/* ICMP mask reply is IPv4 only */
13492 	ASSERT(!ipif->ipif_isv6);
13493 	/* ICMP mask reply is not for a loopback interface */
13494 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13495 
13496 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13497 		return;
13498 
13499 	mp = allocb(REPLY_LEN, BPRI_HI);
13500 	if (mp == NULL)
13501 		return;
13502 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13503 
13504 	ipha = (ipha_t *)mp->b_rptr;
13505 	bzero(ipha, REPLY_LEN);
13506 	*ipha = icmp_ipha;
13507 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13508 	ipha->ipha_src = ipif->ipif_lcl_addr;
13509 	ipha->ipha_dst = ipif->ipif_brd_addr;
13510 	ipha->ipha_length = htons(REPLY_LEN);
13511 	ipha->ipha_ident = 0;
13512 
13513 	icmph = (icmph_t *)&ipha[1];
13514 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13515 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13516 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13517 
13518 	bzero(&ixas, sizeof (ixas));
13519 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13520 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13521 	ixas.ixa_zoneid = ALL_ZONES;
13522 	ixas.ixa_ifindex = 0;
13523 	ixas.ixa_ipst = ipst;
13524 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13525 	(void) ip_output_simple(mp, &ixas);
13526 	ixa_cleanup(&ixas);
13527 #undef	REPLY_LEN
13528 }
13529 
13530 /*
13531  * Join the ipif specific multicast groups.
13532  * Must be called after a mapping has been set up in the resolver.  (Always
13533  * called as writer.)
13534  */
13535 void
13536 ipif_multicast_up(ipif_t *ipif)
13537 {
13538 	int err;
13539 	ill_t *ill;
13540 	ilm_t *ilm;
13541 
13542 	ASSERT(IAM_WRITER_IPIF(ipif));
13543 
13544 	ill = ipif->ipif_ill;
13545 
13546 	ip1dbg(("ipif_multicast_up\n"));
13547 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13548 	    ipif->ipif_allhosts_ilm != NULL)
13549 		return;
13550 
13551 	if (ipif->ipif_isv6) {
13552 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13553 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13554 
13555 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13556 
13557 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13558 			return;
13559 
13560 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13561 
13562 		/*
13563 		 * Join the all hosts multicast address.  We skip this for
13564 		 * underlying IPMP interfaces since they should be invisible.
13565 		 */
13566 		if (!IS_UNDER_IPMP(ill)) {
13567 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13568 			    &err);
13569 			if (ilm == NULL) {
13570 				ASSERT(err != 0);
13571 				ip0dbg(("ipif_multicast_up: "
13572 				    "all_hosts_mcast failed %d\n", err));
13573 				return;
13574 			}
13575 			ipif->ipif_allhosts_ilm = ilm;
13576 		}
13577 
13578 		/*
13579 		 * Enable multicast for the solicited node multicast address.
13580 		 * If IPMP we need to put the membership on the upper ill.
13581 		 */
13582 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13583 			ill_t *mcast_ill = NULL;
13584 			boolean_t need_refrele;
13585 
13586 			if (IS_UNDER_IPMP(ill) &&
13587 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13588 				need_refrele = B_TRUE;
13589 			} else {
13590 				mcast_ill = ill;
13591 				need_refrele = B_FALSE;
13592 			}
13593 
13594 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13595 			    ipif->ipif_zoneid, &err);
13596 			if (need_refrele)
13597 				ill_refrele(mcast_ill);
13598 
13599 			if (ilm == NULL) {
13600 				ASSERT(err != 0);
13601 				ip0dbg(("ipif_multicast_up: solicited MC"
13602 				    " failed %d\n", err));
13603 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13604 					ipif->ipif_allhosts_ilm = NULL;
13605 					(void) ip_delmulti(ilm);
13606 				}
13607 				return;
13608 			}
13609 			ipif->ipif_solmulti_ilm = ilm;
13610 		}
13611 	} else {
13612 		in6_addr_t v6group;
13613 
13614 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13615 			return;
13616 
13617 		/* Join the all hosts multicast address */
13618 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13619 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13620 
13621 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13622 		if (ilm == NULL) {
13623 			ASSERT(err != 0);
13624 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13625 			return;
13626 		}
13627 		ipif->ipif_allhosts_ilm = ilm;
13628 	}
13629 }
13630 
13631 /*
13632  * Blow away any multicast groups that we joined in ipif_multicast_up().
13633  * (ilms from explicit memberships are handled in conn_update_ill.)
13634  */
13635 void
13636 ipif_multicast_down(ipif_t *ipif)
13637 {
13638 	ASSERT(IAM_WRITER_IPIF(ipif));
13639 
13640 	ip1dbg(("ipif_multicast_down\n"));
13641 
13642 	if (ipif->ipif_allhosts_ilm != NULL) {
13643 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13644 		ipif->ipif_allhosts_ilm = NULL;
13645 	}
13646 	if (ipif->ipif_solmulti_ilm != NULL) {
13647 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13648 		ipif->ipif_solmulti_ilm = NULL;
13649 	}
13650 }
13651 
13652 /*
13653  * Used when an interface comes up to recreate any extra routes on this
13654  * interface.
13655  */
13656 int
13657 ill_recover_saved_ire(ill_t *ill)
13658 {
13659 	mblk_t		*mp;
13660 	ip_stack_t	*ipst = ill->ill_ipst;
13661 
13662 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13663 
13664 	mutex_enter(&ill->ill_saved_ire_lock);
13665 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13666 		ire_t		*ire, *nire;
13667 		ifrt_t		*ifrt;
13668 
13669 		ifrt = (ifrt_t *)mp->b_rptr;
13670 		/*
13671 		 * Create a copy of the IRE with the saved address and netmask.
13672 		 */
13673 		if (ill->ill_isv6) {
13674 			ire = ire_create_v6(
13675 			    &ifrt->ifrt_v6addr,
13676 			    &ifrt->ifrt_v6mask,
13677 			    &ifrt->ifrt_v6gateway_addr,
13678 			    ifrt->ifrt_type,
13679 			    ill,
13680 			    ifrt->ifrt_zoneid,
13681 			    ifrt->ifrt_flags,
13682 			    NULL,
13683 			    ipst);
13684 		} else {
13685 			ire = ire_create(
13686 			    (uint8_t *)&ifrt->ifrt_addr,
13687 			    (uint8_t *)&ifrt->ifrt_mask,
13688 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13689 			    ifrt->ifrt_type,
13690 			    ill,
13691 			    ifrt->ifrt_zoneid,
13692 			    ifrt->ifrt_flags,
13693 			    NULL,
13694 			    ipst);
13695 		}
13696 		if (ire == NULL) {
13697 			mutex_exit(&ill->ill_saved_ire_lock);
13698 			return (ENOMEM);
13699 		}
13700 
13701 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13702 			if (ill->ill_isv6) {
13703 				ire->ire_setsrc_addr_v6 =
13704 				    ifrt->ifrt_v6setsrc_addr;
13705 			} else {
13706 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13707 			}
13708 		}
13709 
13710 		/*
13711 		 * Some software (for example, GateD and Sun Cluster) attempts
13712 		 * to create (what amount to) IRE_PREFIX routes with the
13713 		 * loopback address as the gateway.  This is primarily done to
13714 		 * set up prefixes with the RTF_REJECT flag set (for example,
13715 		 * when generating aggregate routes.)
13716 		 *
13717 		 * If the IRE type (as defined by ill->ill_net_type) is
13718 		 * IRE_LOOPBACK, then we map the request into a
13719 		 * IRE_IF_NORESOLVER.
13720 		 */
13721 		if (ill->ill_net_type == IRE_LOOPBACK)
13722 			ire->ire_type = IRE_IF_NORESOLVER;
13723 
13724 		/*
13725 		 * ire held by ire_add, will be refreled' towards the
13726 		 * the end of ipif_up_done
13727 		 */
13728 		nire = ire_add(ire);
13729 		/*
13730 		 * Check if it was a duplicate entry. This handles
13731 		 * the case of two racing route adds for the same route
13732 		 */
13733 		if (nire == NULL) {
13734 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13735 		} else if (nire != ire) {
13736 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13737 			    (void *)nire));
13738 			ire_delete(nire);
13739 		} else {
13740 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13741 			    (void *)nire));
13742 		}
13743 		if (nire != NULL)
13744 			ire_refrele(nire);
13745 	}
13746 	mutex_exit(&ill->ill_saved_ire_lock);
13747 	return (0);
13748 }
13749 
13750 /*
13751  * Used to set the netmask and broadcast address to default values when the
13752  * interface is brought up.  (Always called as writer.)
13753  */
13754 static void
13755 ipif_set_default(ipif_t *ipif)
13756 {
13757 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13758 
13759 	if (!ipif->ipif_isv6) {
13760 		/*
13761 		 * Interface holds an IPv4 address. Default
13762 		 * mask is the natural netmask.
13763 		 */
13764 		if (!ipif->ipif_net_mask) {
13765 			ipaddr_t	v4mask;
13766 
13767 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13768 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13769 		}
13770 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13771 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13772 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13773 		} else {
13774 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13775 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13776 		}
13777 		/*
13778 		 * NOTE: SunOS 4.X does this even if the broadcast address
13779 		 * has been already set thus we do the same here.
13780 		 */
13781 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13782 			ipaddr_t	v4addr;
13783 
13784 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13785 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13786 		}
13787 	} else {
13788 		/*
13789 		 * Interface holds an IPv6-only address.  Default
13790 		 * mask is all-ones.
13791 		 */
13792 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13793 			ipif->ipif_v6net_mask = ipv6_all_ones;
13794 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13795 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13796 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13797 		} else {
13798 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13799 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13800 		}
13801 	}
13802 }
13803 
13804 /*
13805  * Return 0 if this address can be used as local address without causing
13806  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13807  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13808  * Note that the same IPv6 link-local address is allowed as long as the ills
13809  * are not on the same link.
13810  */
13811 int
13812 ip_addr_availability_check(ipif_t *new_ipif)
13813 {
13814 	in6_addr_t our_v6addr;
13815 	ill_t *ill;
13816 	ipif_t *ipif;
13817 	ill_walk_context_t ctx;
13818 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13819 
13820 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13821 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13822 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13823 
13824 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13825 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13826 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13827 		return (0);
13828 
13829 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13830 
13831 	if (new_ipif->ipif_isv6)
13832 		ill = ILL_START_WALK_V6(&ctx, ipst);
13833 	else
13834 		ill = ILL_START_WALK_V4(&ctx, ipst);
13835 
13836 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13837 		for (ipif = ill->ill_ipif; ipif != NULL;
13838 		    ipif = ipif->ipif_next) {
13839 			if ((ipif == new_ipif) ||
13840 			    !(ipif->ipif_flags & IPIF_UP) ||
13841 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13842 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13843 			    &our_v6addr))
13844 				continue;
13845 
13846 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13847 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13848 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13849 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13850 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13851 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13852 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13853 				continue;
13854 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13855 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13856 				continue;
13857 			else if (new_ipif->ipif_ill == ill)
13858 				return (EADDRINUSE);
13859 			else
13860 				return (EADDRNOTAVAIL);
13861 		}
13862 	}
13863 
13864 	return (0);
13865 }
13866 
13867 /*
13868  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13869  * IREs for the ipif.
13870  * When the routine returns EINPROGRESS then mp has been consumed and
13871  * the ioctl will be acked from ip_rput_dlpi.
13872  */
13873 int
13874 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13875 {
13876 	ill_t		*ill = ipif->ipif_ill;
13877 	boolean_t 	isv6 = ipif->ipif_isv6;
13878 	int		err = 0;
13879 	boolean_t	success;
13880 	uint_t		ipif_orig_id;
13881 	ip_stack_t	*ipst = ill->ill_ipst;
13882 
13883 	ASSERT(IAM_WRITER_IPIF(ipif));
13884 
13885 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13886 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13887 	    ill_t *, ill, ipif_t *, ipif);
13888 
13889 	/* Shouldn't get here if it is already up. */
13890 	if (ipif->ipif_flags & IPIF_UP)
13891 		return (EALREADY);
13892 
13893 	/*
13894 	 * If this is a request to bring up a data address on an interface
13895 	 * under IPMP, then move the address to its IPMP meta-interface and
13896 	 * try to bring it up.  One complication is that the zeroth ipif for
13897 	 * an ill is special, in that every ill always has one, and that code
13898 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13899 	 */
13900 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13901 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13902 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13903 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13904 
13905 		/*
13906 		 * The ipif being brought up should be quiesced.  If it's not,
13907 		 * something has gone amiss and we need to bail out.  (If it's
13908 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13909 		 */
13910 		mutex_enter(&ill->ill_lock);
13911 		if (!ipif_is_quiescent(ipif)) {
13912 			mutex_exit(&ill->ill_lock);
13913 			return (EINVAL);
13914 		}
13915 		mutex_exit(&ill->ill_lock);
13916 
13917 		/*
13918 		 * If we're going to need to allocate ipifs, do it prior
13919 		 * to starting the move (and grabbing locks).
13920 		 */
13921 		if (ipif->ipif_id == 0) {
13922 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13923 			    B_FALSE, &err)) == NULL) {
13924 				return (err);
13925 			}
13926 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13927 			    B_FALSE, &err)) == NULL) {
13928 				mi_free(moveipif);
13929 				return (err);
13930 			}
13931 		}
13932 
13933 		/*
13934 		 * Grab or transfer the ipif to move.  During the move, keep
13935 		 * ill_g_lock held to prevent any ill walker threads from
13936 		 * seeing things in an inconsistent state.
13937 		 */
13938 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13939 		if (ipif->ipif_id != 0) {
13940 			ipif_remove(ipif);
13941 		} else {
13942 			ipif_transfer(ipif, moveipif, stubipif);
13943 			ipif = moveipif;
13944 		}
13945 
13946 		/*
13947 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13948 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13949 		 * replace that one.  Otherwise, pick the next available slot.
13950 		 */
13951 		ipif->ipif_ill = ipmp_ill;
13952 		ipif_orig_id = ipif->ipif_id;
13953 
13954 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13955 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13956 			ipif = ipmp_ill->ill_ipif;
13957 		} else {
13958 			ipif->ipif_id = -1;
13959 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
13960 				/*
13961 				 * No more available ipif_id's -- put it back
13962 				 * on the original ill and fail the operation.
13963 				 * Since we're writer on the ill, we can be
13964 				 * sure our old slot is still available.
13965 				 */
13966 				ipif->ipif_id = ipif_orig_id;
13967 				ipif->ipif_ill = ill;
13968 				if (ipif_orig_id == 0) {
13969 					ipif_transfer(ipif, ill->ill_ipif,
13970 					    NULL);
13971 				} else {
13972 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13973 				}
13974 				rw_exit(&ipst->ips_ill_g_lock);
13975 				return (err);
13976 			}
13977 		}
13978 		rw_exit(&ipst->ips_ill_g_lock);
13979 
13980 		/*
13981 		 * Tell SCTP that the ipif has moved.  Note that even if we
13982 		 * had to allocate a new ipif, the original sequence id was
13983 		 * preserved and therefore SCTP won't know.
13984 		 */
13985 		sctp_move_ipif(ipif, ill, ipmp_ill);
13986 
13987 		/*
13988 		 * If the ipif being brought up was on slot zero, then we
13989 		 * first need to bring up the placeholder we stuck there.  In
13990 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13991 		 * call to ipif_up() itself, if we successfully bring up the
13992 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13993 		 */
13994 		if (ipif_orig_id == 0) {
13995 			ASSERT(ill->ill_move_ipif == NULL);
13996 			ill->ill_move_ipif = ipif;
13997 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
13998 				ASSERT(ill->ill_move_ipif == NULL);
13999 			if (err != EINPROGRESS)
14000 				ill->ill_move_ipif = NULL;
14001 			return (err);
14002 		}
14003 
14004 		/*
14005 		 * Bring it up on the IPMP ill.
14006 		 */
14007 		return (ipif_up(ipif, q, mp));
14008 	}
14009 
14010 	/* Skip arp/ndp for any loopback interface. */
14011 	if (ill->ill_wq != NULL) {
14012 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14013 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14014 
14015 		if (!ill->ill_dl_up) {
14016 			/*
14017 			 * ill_dl_up is not yet set. i.e. we are yet to
14018 			 * DL_BIND with the driver and this is the first
14019 			 * logical interface on the ill to become "up".
14020 			 * Tell the driver to get going (via DL_BIND_REQ).
14021 			 * Note that changing "significant" IFF_ flags
14022 			 * address/netmask etc cause a down/up dance, but
14023 			 * does not cause an unbind (DL_UNBIND) with the driver
14024 			 */
14025 			return (ill_dl_up(ill, ipif, mp, q));
14026 		}
14027 
14028 		/*
14029 		 * ipif_resolver_up may end up needeing to bind/attach
14030 		 * the ARP stream, which in turn necessitates a
14031 		 * DLPI message exchange with the driver. ioctls are
14032 		 * serialized and so we cannot send more than one
14033 		 * interface up message at a time. If ipif_resolver_up
14034 		 * does need to wait for the DLPI handshake for the ARP stream,
14035 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14036 		 */
14037 
14038 		ASSERT(connp != NULL || !CONN_Q(q));
14039 		if (connp != NULL)
14040 			mutex_enter(&connp->conn_lock);
14041 		mutex_enter(&ill->ill_lock);
14042 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14043 		mutex_exit(&ill->ill_lock);
14044 		if (connp != NULL)
14045 			mutex_exit(&connp->conn_lock);
14046 		if (!success)
14047 			return (EINTR);
14048 
14049 		/*
14050 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14051 		 * complete when ipif_ndp_up returns.
14052 		 */
14053 		err = ipif_resolver_up(ipif, Res_act_initial);
14054 		if (err == EINPROGRESS) {
14055 			/* We will complete it in arp_bringup_done() */
14056 			return (err);
14057 		}
14058 
14059 		if (isv6 && err == 0)
14060 			err = ipif_ndp_up(ipif, B_TRUE);
14061 
14062 		ASSERT(err != EINPROGRESS);
14063 		mp = ipsq_pending_mp_get(ipsq, &connp);
14064 		ASSERT(mp != NULL);
14065 		if (err != 0)
14066 			return (err);
14067 	} else {
14068 		/*
14069 		 * Interfaces without underlying hardware don't do duplicate
14070 		 * address detection.
14071 		 */
14072 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14073 		ipif->ipif_addr_ready = 1;
14074 		err = ill_add_ires(ill);
14075 		/* allocation failure? */
14076 		if (err != 0)
14077 			return (err);
14078 	}
14079 
14080 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14081 	if (err == 0 && ill->ill_move_ipif != NULL) {
14082 		ipif = ill->ill_move_ipif;
14083 		ill->ill_move_ipif = NULL;
14084 		return (ipif_up(ipif, q, mp));
14085 	}
14086 	return (err);
14087 }
14088 
14089 /*
14090  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14091  * The identical set of IREs need to be removed in ill_delete_ires().
14092  */
14093 int
14094 ill_add_ires(ill_t *ill)
14095 {
14096 	ire_t	*ire;
14097 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14098 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14099 
14100 	if (ill->ill_ire_multicast != NULL)
14101 		return (0);
14102 
14103 	/*
14104 	 * provide some dummy ire_addr for creating the ire.
14105 	 */
14106 	if (ill->ill_isv6) {
14107 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14108 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14109 	} else {
14110 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14111 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14112 	}
14113 	if (ire == NULL)
14114 		return (ENOMEM);
14115 
14116 	ill->ill_ire_multicast = ire;
14117 	return (0);
14118 }
14119 
14120 void
14121 ill_delete_ires(ill_t *ill)
14122 {
14123 	if (ill->ill_ire_multicast != NULL) {
14124 		/*
14125 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14126 		 * which was taken without any th_tracing enabled.
14127 		 * We also mark it as condemned (note that it was never added)
14128 		 * so that caching conn's can move off of it.
14129 		 */
14130 		ire_make_condemned(ill->ill_ire_multicast);
14131 		ire_refrele_notr(ill->ill_ire_multicast);
14132 		ill->ill_ire_multicast = NULL;
14133 	}
14134 }
14135 
14136 /*
14137  * Perform a bind for the physical device.
14138  * When the routine returns EINPROGRESS then mp has been consumed and
14139  * the ioctl will be acked from ip_rput_dlpi.
14140  * Allocate an unbind message and save it until ipif_down.
14141  */
14142 static int
14143 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14144 {
14145 	mblk_t	*bind_mp = NULL;
14146 	mblk_t	*unbind_mp = NULL;
14147 	conn_t	*connp;
14148 	boolean_t success;
14149 	int	err;
14150 
14151 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14152 
14153 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14154 	ASSERT(IAM_WRITER_ILL(ill));
14155 	ASSERT(mp != NULL);
14156 
14157 	/*
14158 	 * Make sure we have an IRE_MULTICAST in case we immediately
14159 	 * start receiving packets.
14160 	 */
14161 	err = ill_add_ires(ill);
14162 	if (err != 0)
14163 		goto bad;
14164 
14165 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14166 	    DL_BIND_REQ);
14167 	if (bind_mp == NULL)
14168 		goto bad;
14169 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14170 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14171 
14172 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14173 	if (unbind_mp == NULL)
14174 		goto bad;
14175 
14176 	/*
14177 	 * Record state needed to complete this operation when the
14178 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14179 	 */
14180 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14181 	ASSERT(connp != NULL || !CONN_Q(q));
14182 	GRAB_CONN_LOCK(q);
14183 	mutex_enter(&ipif->ipif_ill->ill_lock);
14184 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14185 	mutex_exit(&ipif->ipif_ill->ill_lock);
14186 	RELEASE_CONN_LOCK(q);
14187 	if (!success)
14188 		goto bad;
14189 
14190 	/*
14191 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14192 	 * the interface goes down.
14193 	 */
14194 	ASSERT(ill->ill_unbind_mp == NULL);
14195 	ill->ill_unbind_mp = unbind_mp;
14196 
14197 	ill_dlpi_send(ill, bind_mp);
14198 	/* Send down link-layer capabilities probe if not already done. */
14199 	ill_capability_probe(ill);
14200 
14201 	/*
14202 	 * Sysid used to rely on the fact that netboots set domainname
14203 	 * and the like. Now that miniroot boots aren't strictly netboots
14204 	 * and miniroot network configuration is driven from userland
14205 	 * these things still need to be set. This situation can be detected
14206 	 * by comparing the interface being configured here to the one
14207 	 * dhcifname was set to reference by the boot loader. Once sysid is
14208 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14209 	 */
14210 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14211 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14212 	    (strlen(srpc_domain) == 0)) {
14213 		if (dhcpinit() != 0)
14214 			cmn_err(CE_WARN, "no cached dhcp response");
14215 	}
14216 
14217 	/*
14218 	 * This operation will complete in ip_rput_dlpi with either
14219 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14220 	 */
14221 	return (EINPROGRESS);
14222 bad:
14223 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14224 
14225 	freemsg(bind_mp);
14226 	freemsg(unbind_mp);
14227 	return (ENOMEM);
14228 }
14229 
14230 /* Add room for tcp+ip headers */
14231 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14232 
14233 /*
14234  * DLPI and ARP is up.
14235  * Create all the IREs associated with an interface. Bring up multicast.
14236  * Set the interface flag and finish other initialization
14237  * that potentially had to be deferred to after DL_BIND_ACK.
14238  */
14239 int
14240 ipif_up_done(ipif_t *ipif)
14241 {
14242 	ill_t		*ill = ipif->ipif_ill;
14243 	int		err = 0;
14244 	boolean_t	loopback = B_FALSE;
14245 	boolean_t	update_src_selection = B_TRUE;
14246 	ipif_t		*tmp_ipif;
14247 
14248 	ip1dbg(("ipif_up_done(%s:%u)\n",
14249 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14250 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14251 	    ill_t *, ill, ipif_t *, ipif);
14252 
14253 	/* Check if this is a loopback interface */
14254 	if (ipif->ipif_ill->ill_wq == NULL)
14255 		loopback = B_TRUE;
14256 
14257 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14258 
14259 	/*
14260 	 * If all other interfaces for this ill are down or DEPRECATED,
14261 	 * or otherwise unsuitable for source address selection,
14262 	 * reset the src generation numbers to make sure source
14263 	 * address selection gets to take this new ipif into account.
14264 	 * No need to hold ill_lock while traversing the ipif list since
14265 	 * we are writer
14266 	 */
14267 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14268 	    tmp_ipif = tmp_ipif->ipif_next) {
14269 		if (((tmp_ipif->ipif_flags &
14270 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14271 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14272 		    (tmp_ipif == ipif))
14273 			continue;
14274 		/* first useable pre-existing interface */
14275 		update_src_selection = B_FALSE;
14276 		break;
14277 	}
14278 	if (update_src_selection)
14279 		ip_update_source_selection(ill->ill_ipst);
14280 
14281 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14282 		nce_t *loop_nce = NULL;
14283 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14284 
14285 		/*
14286 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14287 		 * ipif_lookup_on_name(), but in the case of zones we can have
14288 		 * several loopback addresses on lo0. So all the interfaces with
14289 		 * loopback addresses need to be marked IRE_LOOPBACK.
14290 		 */
14291 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14292 		    htonl(INADDR_LOOPBACK))
14293 			ipif->ipif_ire_type = IRE_LOOPBACK;
14294 		else
14295 			ipif->ipif_ire_type = IRE_LOCAL;
14296 		if (ill->ill_net_type != IRE_LOOPBACK)
14297 			flags |= NCE_F_PUBLISH;
14298 
14299 		/* add unicast nce for the local addr */
14300 		err = nce_lookup_then_add_v4(ill, NULL,
14301 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14302 		    ND_REACHABLE, &loop_nce);
14303 		/* A shared-IP zone sees EEXIST for lo0:N */
14304 		if (err == 0 || err == EEXIST) {
14305 			ipif->ipif_added_nce = 1;
14306 			loop_nce->nce_ipif_cnt++;
14307 			nce_refrele(loop_nce);
14308 			err = 0;
14309 		} else {
14310 			ASSERT(loop_nce == NULL);
14311 			return (err);
14312 		}
14313 	}
14314 
14315 	/* Create all the IREs associated with this interface */
14316 	err = ipif_add_ires_v4(ipif, loopback);
14317 	if (err != 0) {
14318 		/*
14319 		 * see comments about return value from
14320 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14321 		 */
14322 		if (err != EADDRINUSE) {
14323 			(void) ipif_arp_down(ipif);
14324 		} else {
14325 			/*
14326 			 * Make IPMP aware of the deleted ipif so that
14327 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14328 			 * can be completed. Note that we do not want to
14329 			 * destroy the nce that was created on the ipmp_ill
14330 			 * for the active copy of the duplicate address in
14331 			 * use.
14332 			 */
14333 			if (IS_IPMP(ill))
14334 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14335 			err = EADDRNOTAVAIL;
14336 		}
14337 		return (err);
14338 	}
14339 
14340 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14341 		/* Recover any additional IREs entries for this ill */
14342 		(void) ill_recover_saved_ire(ill);
14343 	}
14344 
14345 	if (ill->ill_need_recover_multicast) {
14346 		/*
14347 		 * Need to recover all multicast memberships in the driver.
14348 		 * This had to be deferred until we had attached.  The same
14349 		 * code exists in ipif_up_done_v6() to recover IPv6
14350 		 * memberships.
14351 		 *
14352 		 * Note that it would be preferable to unconditionally do the
14353 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14354 		 * that since ill_join_allmulti() depends on ill_dl_up being
14355 		 * set, and it is not set until we receive a DL_BIND_ACK after
14356 		 * having called ill_dl_up().
14357 		 */
14358 		ill_recover_multicast(ill);
14359 	}
14360 
14361 	if (ill->ill_ipif_up_count == 1) {
14362 		/*
14363 		 * Since the interface is now up, it may now be active.
14364 		 */
14365 		if (IS_UNDER_IPMP(ill))
14366 			ipmp_ill_refresh_active(ill);
14367 
14368 		/*
14369 		 * If this is an IPMP interface, we may now be able to
14370 		 * establish ARP entries.
14371 		 */
14372 		if (IS_IPMP(ill))
14373 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14374 	}
14375 
14376 	/* Join the allhosts multicast address */
14377 	ipif_multicast_up(ipif);
14378 
14379 	if (!loopback && !update_src_selection &&
14380 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14381 		ip_update_source_selection(ill->ill_ipst);
14382 
14383 	if (!loopback && ipif->ipif_addr_ready) {
14384 		/* Broadcast an address mask reply. */
14385 		ipif_mask_reply(ipif);
14386 	}
14387 	/* Perhaps ilgs should use this ill */
14388 	update_conn_ill(NULL, ill->ill_ipst);
14389 
14390 	/*
14391 	 * This had to be deferred until we had bound.  Tell routing sockets and
14392 	 * others that this interface is up if it looks like the address has
14393 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14394 	 * duplicate address detection to do its thing.
14395 	 */
14396 	if (ipif->ipif_addr_ready)
14397 		ipif_up_notify(ipif);
14398 	return (0);
14399 }
14400 
14401 /*
14402  * Add the IREs associated with the ipif.
14403  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14404  */
14405 static int
14406 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14407 {
14408 	ill_t		*ill = ipif->ipif_ill;
14409 	ip_stack_t	*ipst = ill->ill_ipst;
14410 	ire_t		*ire_array[20];
14411 	ire_t		**irep = ire_array;
14412 	ire_t		**irep1;
14413 	ipaddr_t	net_mask = 0;
14414 	ipaddr_t	subnet_mask, route_mask;
14415 	int		err;
14416 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14417 	ire_t		*ire_if = NULL;
14418 
14419 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14420 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14421 		/*
14422 		 * If we're on a labeled system then make sure that zone-
14423 		 * private addresses have proper remote host database entries.
14424 		 */
14425 		if (is_system_labeled() &&
14426 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14427 		    !tsol_check_interface_address(ipif))
14428 			return (EINVAL);
14429 
14430 		/* Register the source address for __sin6_src_id */
14431 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14432 		    ipif->ipif_zoneid, ipst);
14433 		if (err != 0) {
14434 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14435 			return (err);
14436 		}
14437 
14438 		/* If the interface address is set, create the local IRE. */
14439 		ire_local = ire_create(
14440 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14441 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14442 		    NULL,				/* no gateway */
14443 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14444 		    ipif->ipif_ill,
14445 		    ipif->ipif_zoneid,
14446 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14447 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14448 		    NULL,
14449 		    ipst);
14450 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14451 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14452 		    ipif->ipif_ire_type,
14453 		    ntohl(ipif->ipif_lcl_addr)));
14454 		if (ire_local == NULL) {
14455 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14456 			err = ENOMEM;
14457 			goto bad;
14458 		}
14459 	} else {
14460 		ip1dbg((
14461 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14462 		    ipif->ipif_ire_type,
14463 		    ntohl(ipif->ipif_lcl_addr),
14464 		    (uint_t)ipif->ipif_flags));
14465 	}
14466 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14467 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14468 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14469 	} else {
14470 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14471 	}
14472 
14473 	subnet_mask = ipif->ipif_net_mask;
14474 
14475 	/*
14476 	 * If mask was not specified, use natural netmask of
14477 	 * interface address. Also, store this mask back into the
14478 	 * ipif struct.
14479 	 */
14480 	if (subnet_mask == 0) {
14481 		subnet_mask = net_mask;
14482 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14483 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14484 		    ipif->ipif_v6subnet);
14485 	}
14486 
14487 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14488 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14489 	    ipif->ipif_subnet != INADDR_ANY) {
14490 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14491 
14492 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14493 			route_mask = IP_HOST_MASK;
14494 		} else {
14495 			route_mask = subnet_mask;
14496 		}
14497 
14498 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14499 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14500 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14501 		    ntohl(ipif->ipif_subnet)));
14502 		ire_if = ire_create(
14503 		    (uchar_t *)&ipif->ipif_subnet,
14504 		    (uchar_t *)&route_mask,
14505 		    (uchar_t *)&ipif->ipif_lcl_addr,
14506 		    ill->ill_net_type,
14507 		    ill,
14508 		    ipif->ipif_zoneid,
14509 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14510 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14511 		    NULL,
14512 		    ipst);
14513 		if (ire_if == NULL) {
14514 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14515 			err = ENOMEM;
14516 			goto bad;
14517 		}
14518 	}
14519 
14520 	/*
14521 	 * Create any necessary broadcast IREs.
14522 	 */
14523 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14524 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14525 		irep = ipif_create_bcast_ires(ipif, irep);
14526 
14527 	/* If an earlier ire_create failed, get out now */
14528 	for (irep1 = irep; irep1 > ire_array; ) {
14529 		irep1--;
14530 		if (*irep1 == NULL) {
14531 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14532 			err = ENOMEM;
14533 			goto bad;
14534 		}
14535 	}
14536 
14537 	/*
14538 	 * Need to atomically check for IP address availability under
14539 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14540 	 * ills or new ipifs can be added while we are checking availability.
14541 	 */
14542 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14543 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14544 	/* Mark it up, and increment counters. */
14545 	ipif->ipif_flags |= IPIF_UP;
14546 	ill->ill_ipif_up_count++;
14547 	err = ip_addr_availability_check(ipif);
14548 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14549 	rw_exit(&ipst->ips_ill_g_lock);
14550 
14551 	if (err != 0) {
14552 		/*
14553 		 * Our address may already be up on the same ill. In this case,
14554 		 * the ARP entry for our ipif replaced the one for the other
14555 		 * ipif. So we don't want to delete it (otherwise the other ipif
14556 		 * would be unable to send packets).
14557 		 * ip_addr_availability_check() identifies this case for us and
14558 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14559 		 * which is the expected error code.
14560 		 */
14561 		ill->ill_ipif_up_count--;
14562 		ipif->ipif_flags &= ~IPIF_UP;
14563 		goto bad;
14564 	}
14565 
14566 	/*
14567 	 * Add in all newly created IREs.  ire_create_bcast() has
14568 	 * already checked for duplicates of the IRE_BROADCAST type.
14569 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14570 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14571 	 * a /32 route.
14572 	 */
14573 	if (ire_if != NULL) {
14574 		ire_if = ire_add(ire_if);
14575 		if (ire_if == NULL) {
14576 			err = ENOMEM;
14577 			goto bad2;
14578 		}
14579 #ifdef DEBUG
14580 		ire_refhold_notr(ire_if);
14581 		ire_refrele(ire_if);
14582 #endif
14583 	}
14584 	if (ire_local != NULL) {
14585 		ire_local = ire_add(ire_local);
14586 		if (ire_local == NULL) {
14587 			err = ENOMEM;
14588 			goto bad2;
14589 		}
14590 #ifdef DEBUG
14591 		ire_refhold_notr(ire_local);
14592 		ire_refrele(ire_local);
14593 #endif
14594 	}
14595 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14596 	if (ire_local != NULL)
14597 		ipif->ipif_ire_local = ire_local;
14598 	if (ire_if != NULL)
14599 		ipif->ipif_ire_if = ire_if;
14600 	rw_exit(&ipst->ips_ill_g_lock);
14601 	ire_local = NULL;
14602 	ire_if = NULL;
14603 
14604 	/*
14605 	 * We first add all of them, and if that succeeds we refrele the
14606 	 * bunch. That enables us to delete all of them should any of the
14607 	 * ire_adds fail.
14608 	 */
14609 	for (irep1 = irep; irep1 > ire_array; ) {
14610 		irep1--;
14611 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14612 		*irep1 = ire_add(*irep1);
14613 		if (*irep1 == NULL) {
14614 			err = ENOMEM;
14615 			goto bad2;
14616 		}
14617 	}
14618 
14619 	for (irep1 = irep; irep1 > ire_array; ) {
14620 		irep1--;
14621 		/* refheld by ire_add. */
14622 		if (*irep1 != NULL) {
14623 			ire_refrele(*irep1);
14624 			*irep1 = NULL;
14625 		}
14626 	}
14627 
14628 	if (!loopback) {
14629 		/*
14630 		 * If the broadcast address has been set, make sure it makes
14631 		 * sense based on the interface address.
14632 		 * Only match on ill since we are sharing broadcast addresses.
14633 		 */
14634 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14635 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14636 			ire_t	*ire;
14637 
14638 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14639 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14640 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14641 
14642 			if (ire == NULL) {
14643 				/*
14644 				 * If there isn't a matching broadcast IRE,
14645 				 * revert to the default for this netmask.
14646 				 */
14647 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14648 				mutex_enter(&ipif->ipif_ill->ill_lock);
14649 				ipif_set_default(ipif);
14650 				mutex_exit(&ipif->ipif_ill->ill_lock);
14651 			} else {
14652 				ire_refrele(ire);
14653 			}
14654 		}
14655 
14656 	}
14657 	return (0);
14658 
14659 bad2:
14660 	ill->ill_ipif_up_count--;
14661 	ipif->ipif_flags &= ~IPIF_UP;
14662 
14663 bad:
14664 	ip1dbg(("ipif_add_ires: FAILED \n"));
14665 	if (ire_local != NULL)
14666 		ire_delete(ire_local);
14667 	if (ire_if != NULL)
14668 		ire_delete(ire_if);
14669 
14670 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14671 	ire_local = ipif->ipif_ire_local;
14672 	ipif->ipif_ire_local = NULL;
14673 	ire_if = ipif->ipif_ire_if;
14674 	ipif->ipif_ire_if = NULL;
14675 	rw_exit(&ipst->ips_ill_g_lock);
14676 	if (ire_local != NULL) {
14677 		ire_delete(ire_local);
14678 		ire_refrele_notr(ire_local);
14679 	}
14680 	if (ire_if != NULL) {
14681 		ire_delete(ire_if);
14682 		ire_refrele_notr(ire_if);
14683 	}
14684 
14685 	while (irep > ire_array) {
14686 		irep--;
14687 		if (*irep != NULL) {
14688 			ire_delete(*irep);
14689 		}
14690 	}
14691 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14692 
14693 	return (err);
14694 }
14695 
14696 /* Remove all the IREs created by ipif_add_ires_v4 */
14697 void
14698 ipif_delete_ires_v4(ipif_t *ipif)
14699 {
14700 	ill_t		*ill = ipif->ipif_ill;
14701 	ip_stack_t	*ipst = ill->ill_ipst;
14702 	ire_t		*ire;
14703 
14704 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14705 	ire = ipif->ipif_ire_local;
14706 	ipif->ipif_ire_local = NULL;
14707 	rw_exit(&ipst->ips_ill_g_lock);
14708 	if (ire != NULL) {
14709 		/*
14710 		 * Move count to ipif so we don't loose the count due to
14711 		 * a down/up dance.
14712 		 */
14713 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14714 
14715 		ire_delete(ire);
14716 		ire_refrele_notr(ire);
14717 	}
14718 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14719 	ire = ipif->ipif_ire_if;
14720 	ipif->ipif_ire_if = NULL;
14721 	rw_exit(&ipst->ips_ill_g_lock);
14722 	if (ire != NULL) {
14723 		ire_delete(ire);
14724 		ire_refrele_notr(ire);
14725 	}
14726 
14727 	/*
14728 	 * Delete the broadcast IREs.
14729 	 */
14730 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14731 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14732 		ipif_delete_bcast_ires(ipif);
14733 }
14734 
14735 /*
14736  * Checks for availbility of a usable source address (if there is one) when the
14737  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14738  * this selection is done regardless of the destination.
14739  */
14740 boolean_t
14741 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14742     ip_stack_t *ipst)
14743 {
14744 	ipif_t		*ipif = NULL;
14745 	ill_t		*uill;
14746 
14747 	ASSERT(ifindex != 0);
14748 
14749 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14750 	if (uill == NULL)
14751 		return (B_FALSE);
14752 
14753 	mutex_enter(&uill->ill_lock);
14754 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14755 		if (IPIF_IS_CONDEMNED(ipif))
14756 			continue;
14757 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14758 			continue;
14759 		if (!(ipif->ipif_flags & IPIF_UP))
14760 			continue;
14761 		if (ipif->ipif_zoneid != zoneid)
14762 			continue;
14763 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14764 		    ipif->ipif_lcl_addr == INADDR_ANY)
14765 			continue;
14766 		mutex_exit(&uill->ill_lock);
14767 		ill_refrele(uill);
14768 		return (B_TRUE);
14769 	}
14770 	mutex_exit(&uill->ill_lock);
14771 	ill_refrele(uill);
14772 	return (B_FALSE);
14773 }
14774 
14775 /*
14776  * Find an ipif with a good local address on the ill+zoneid.
14777  */
14778 ipif_t *
14779 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14780 {
14781 	ipif_t		*ipif;
14782 
14783 	mutex_enter(&ill->ill_lock);
14784 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14785 		if (IPIF_IS_CONDEMNED(ipif))
14786 			continue;
14787 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14788 			continue;
14789 		if (!(ipif->ipif_flags & IPIF_UP))
14790 			continue;
14791 		if (ipif->ipif_zoneid != zoneid &&
14792 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14793 			continue;
14794 		if (ill->ill_isv6 ?
14795 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14796 		    ipif->ipif_lcl_addr == INADDR_ANY)
14797 			continue;
14798 		ipif_refhold_locked(ipif);
14799 		mutex_exit(&ill->ill_lock);
14800 		return (ipif);
14801 	}
14802 	mutex_exit(&ill->ill_lock);
14803 	return (NULL);
14804 }
14805 
14806 /*
14807  * IP source address type, sorted from worst to best.  For a given type,
14808  * always prefer IP addresses on the same subnet.  All-zones addresses are
14809  * suboptimal because they pose problems with unlabeled destinations.
14810  */
14811 typedef enum {
14812 	IPIF_NONE,
14813 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14814 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14815 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14816 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14817 	IPIF_DIFFNET,			/* normal and different subnet */
14818 	IPIF_SAMENET,			/* normal and same subnet */
14819 	IPIF_LOCALADDR			/* local loopback */
14820 } ipif_type_t;
14821 
14822 /*
14823  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14824  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14825  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14826  * the first one, unless IPMP is used in which case we round-robin among them;
14827  * see below for more.
14828  *
14829  * Returns NULL if there is no suitable source address for the ill.
14830  * This only occurs when there is no valid source address for the ill.
14831  */
14832 ipif_t *
14833 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14834     boolean_t allow_usesrc, boolean_t *notreadyp)
14835 {
14836 	ill_t	*usill = NULL;
14837 	ill_t	*ipmp_ill = NULL;
14838 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14839 	ipif_type_t type, best_type;
14840 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14841 	ip_stack_t *ipst = ill->ill_ipst;
14842 	boolean_t samenet;
14843 
14844 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14845 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14846 		    B_FALSE, ipst);
14847 		if (usill != NULL)
14848 			ill = usill;	/* Select source from usesrc ILL */
14849 		else
14850 			return (NULL);
14851 	}
14852 
14853 	/*
14854 	 * Test addresses should never be used for source address selection,
14855 	 * so if we were passed one, switch to the IPMP meta-interface.
14856 	 */
14857 	if (IS_UNDER_IPMP(ill)) {
14858 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14859 			ill = ipmp_ill;	/* Select source from IPMP ill */
14860 		else
14861 			return (NULL);
14862 	}
14863 
14864 	/*
14865 	 * If we're dealing with an unlabeled destination on a labeled system,
14866 	 * make sure that we ignore source addresses that are incompatible with
14867 	 * the destination's default label.  That destination's default label
14868 	 * must dominate the minimum label on the source address.
14869 	 */
14870 	dst_rhtp = NULL;
14871 	if (is_system_labeled()) {
14872 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14873 		if (dst_rhtp == NULL)
14874 			return (NULL);
14875 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14876 			TPC_RELE(dst_rhtp);
14877 			dst_rhtp = NULL;
14878 		}
14879 	}
14880 
14881 	/*
14882 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14883 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14884 	 * After selecting the right ipif, under ill_lock make sure ipif is
14885 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14886 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14887 	 * but not under a lock.
14888 	 */
14889 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14890 retry:
14891 	/*
14892 	 * For source address selection, we treat the ipif list as circular
14893 	 * and continue until we get back to where we started.  This allows
14894 	 * IPMP to vary source address selection (which improves inbound load
14895 	 * spreading) by caching its last ending point and starting from
14896 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14897 	 * ills since that can't happen on the IPMP ill.
14898 	 */
14899 	start_ipif = ill->ill_ipif;
14900 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14901 		start_ipif = ill->ill_src_ipif;
14902 
14903 	ipif = start_ipif;
14904 	best_ipif = NULL;
14905 	best_type = IPIF_NONE;
14906 	do {
14907 		if ((next_ipif = ipif->ipif_next) == NULL)
14908 			next_ipif = ill->ill_ipif;
14909 
14910 		if (IPIF_IS_CONDEMNED(ipif))
14911 			continue;
14912 		/* Always skip NOLOCAL and ANYCAST interfaces */
14913 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14914 			continue;
14915 		/* Always skip NOACCEPT interfaces */
14916 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14917 			continue;
14918 		if (!(ipif->ipif_flags & IPIF_UP))
14919 			continue;
14920 
14921 		if (!ipif->ipif_addr_ready) {
14922 			if (notreadyp != NULL)
14923 				*notreadyp = B_TRUE;
14924 			continue;
14925 		}
14926 
14927 		if (zoneid != ALL_ZONES &&
14928 		    ipif->ipif_zoneid != zoneid &&
14929 		    ipif->ipif_zoneid != ALL_ZONES)
14930 			continue;
14931 
14932 		/*
14933 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14934 		 * are not valid as source addresses.
14935 		 */
14936 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14937 			continue;
14938 
14939 		/*
14940 		 * Check compatibility of local address for destination's
14941 		 * default label if we're on a labeled system.	Incompatible
14942 		 * addresses can't be used at all.
14943 		 */
14944 		if (dst_rhtp != NULL) {
14945 			boolean_t incompat;
14946 
14947 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14948 			    IPV4_VERSION, B_FALSE);
14949 			if (src_rhtp == NULL)
14950 				continue;
14951 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14952 			    src_rhtp->tpc_tp.tp_doi !=
14953 			    dst_rhtp->tpc_tp.tp_doi ||
14954 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14955 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14956 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14957 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14958 			TPC_RELE(src_rhtp);
14959 			if (incompat)
14960 				continue;
14961 		}
14962 
14963 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14964 
14965 		if (ipif->ipif_lcl_addr == dst) {
14966 			type = IPIF_LOCALADDR;
14967 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14968 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14969 			    IPIF_DIFFNET_DEPRECATED;
14970 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14971 			type = samenet ? IPIF_SAMENET_ALLZONES :
14972 			    IPIF_DIFFNET_ALLZONES;
14973 		} else {
14974 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14975 		}
14976 
14977 		if (type > best_type) {
14978 			best_type = type;
14979 			best_ipif = ipif;
14980 			if (best_type == IPIF_LOCALADDR)
14981 				break; /* can't get better */
14982 		}
14983 	} while ((ipif = next_ipif) != start_ipif);
14984 
14985 	if ((ipif = best_ipif) != NULL) {
14986 		mutex_enter(&ipif->ipif_ill->ill_lock);
14987 		if (IPIF_IS_CONDEMNED(ipif)) {
14988 			mutex_exit(&ipif->ipif_ill->ill_lock);
14989 			goto retry;
14990 		}
14991 		ipif_refhold_locked(ipif);
14992 
14993 		/*
14994 		 * For IPMP, update the source ipif rotor to the next ipif,
14995 		 * provided we can look it up.  (We must not use it if it's
14996 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
14997 		 * ipif_free() checked ill_src_ipif.)
14998 		 */
14999 		if (IS_IPMP(ill) && ipif != NULL) {
15000 			next_ipif = ipif->ipif_next;
15001 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15002 				ill->ill_src_ipif = next_ipif;
15003 			else
15004 				ill->ill_src_ipif = NULL;
15005 		}
15006 		mutex_exit(&ipif->ipif_ill->ill_lock);
15007 	}
15008 
15009 	rw_exit(&ipst->ips_ill_g_lock);
15010 	if (usill != NULL)
15011 		ill_refrele(usill);
15012 	if (ipmp_ill != NULL)
15013 		ill_refrele(ipmp_ill);
15014 	if (dst_rhtp != NULL)
15015 		TPC_RELE(dst_rhtp);
15016 
15017 #ifdef DEBUG
15018 	if (ipif == NULL) {
15019 		char buf1[INET6_ADDRSTRLEN];
15020 
15021 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15022 		    ill->ill_name,
15023 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15024 	} else {
15025 		char buf1[INET6_ADDRSTRLEN];
15026 		char buf2[INET6_ADDRSTRLEN];
15027 
15028 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15029 		    ipif->ipif_ill->ill_name,
15030 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15031 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15032 		    buf2, sizeof (buf2))));
15033 	}
15034 #endif /* DEBUG */
15035 	return (ipif);
15036 }
15037 
15038 /*
15039  * Pick a source address based on the destination ill and an optional setsrc
15040  * address.
15041  * The result is stored in srcp. If generation is set, then put the source
15042  * generation number there before we look for the source address (to avoid
15043  * missing changes in the set of source addresses.
15044  * If flagsp is set, then us it to pass back ipif_flags.
15045  *
15046  * If the caller wants to cache the returned source address and detect when
15047  * that might be stale, the caller should pass in a generation argument,
15048  * which the caller can later compare against ips_src_generation
15049  *
15050  * The precedence order for selecting an IPv4 source address is:
15051  *  - RTF_SETSRC on the offlink ire always wins.
15052  *  - If usrsrc is set, swap the ill to be the usesrc one.
15053  *  - If IPMP is used on the ill, select a random address from the most
15054  *    preferred ones below:
15055  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15056  * 2. Not deprecated, not ALL_ZONES
15057  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15058  * 4. Not deprecated, ALL_ZONES
15059  * 5. If onlink destination, same subnet and deprecated
15060  * 6. Deprecated.
15061  *
15062  * We have lower preference for ALL_ZONES IP addresses,
15063  * as they pose problems with unlabeled destinations.
15064  *
15065  * Note that when multiple IP addresses match e.g., #1 we pick
15066  * the first one if IPMP is not in use. With IPMP we randomize.
15067  */
15068 int
15069 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15070     ipaddr_t multicast_ifaddr,
15071     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15072     uint32_t *generation, uint64_t *flagsp)
15073 {
15074 	ipif_t *ipif;
15075 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15076 
15077 	if (flagsp != NULL)
15078 		*flagsp = 0;
15079 
15080 	/*
15081 	 * Need to grab the generation number before we check to
15082 	 * avoid a race with a change to the set of local addresses.
15083 	 * No lock needed since the thread which updates the set of local
15084 	 * addresses use ipif/ill locks and exit those (hence a store memory
15085 	 * barrier) before doing the atomic increase of ips_src_generation.
15086 	 */
15087 	if (generation != NULL) {
15088 		*generation = ipst->ips_src_generation;
15089 	}
15090 
15091 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15092 		*srcp = multicast_ifaddr;
15093 		return (0);
15094 	}
15095 
15096 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15097 	if (setsrc != INADDR_ANY) {
15098 		*srcp = setsrc;
15099 		return (0);
15100 	}
15101 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15102 	if (ipif == NULL) {
15103 		if (notready)
15104 			return (ENETDOWN);
15105 		else
15106 			return (EADDRNOTAVAIL);
15107 	}
15108 	*srcp = ipif->ipif_lcl_addr;
15109 	if (flagsp != NULL)
15110 		*flagsp = ipif->ipif_flags;
15111 	ipif_refrele(ipif);
15112 	return (0);
15113 }
15114 
15115 /* ARGSUSED */
15116 int
15117 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15118 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15119 {
15120 	/*
15121 	 * ill_phyint_reinit merged the v4 and v6 into a single
15122 	 * ipsq.  We might not have been able to complete the
15123 	 * operation in ipif_set_values, if we could not become
15124 	 * exclusive.  If so restart it here.
15125 	 */
15126 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15127 }
15128 
15129 /*
15130  * Can operate on either a module or a driver queue.
15131  * Returns an error if not a module queue.
15132  */
15133 /* ARGSUSED */
15134 int
15135 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15136     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15137 {
15138 	queue_t		*q1 = q;
15139 	char 		*cp;
15140 	char		interf_name[LIFNAMSIZ];
15141 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15142 
15143 	if (q->q_next == NULL) {
15144 		ip1dbg((
15145 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15146 		return (EINVAL);
15147 	}
15148 
15149 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15150 		return (EALREADY);
15151 
15152 	do {
15153 		q1 = q1->q_next;
15154 	} while (q1->q_next);
15155 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15156 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15157 
15158 	/*
15159 	 * Here we are not going to delay the ioack until after
15160 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15161 	 * original ioctl message before sending the requests.
15162 	 */
15163 	return (ipif_set_values(q, mp, interf_name, &ppa));
15164 }
15165 
15166 /* ARGSUSED */
15167 int
15168 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15169     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15170 {
15171 	return (ENXIO);
15172 }
15173 
15174 /*
15175  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15176  * `irep'.  Returns a pointer to the next free `irep' entry
15177  * A mirror exists in ipif_delete_bcast_ires().
15178  *
15179  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15180  * done in ire_add.
15181  */
15182 static ire_t **
15183 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15184 {
15185 	ipaddr_t addr;
15186 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15187 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15188 	ill_t *ill = ipif->ipif_ill;
15189 	zoneid_t zoneid = ipif->ipif_zoneid;
15190 
15191 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15192 
15193 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15194 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15195 
15196 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15197 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15198 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15199 
15200 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15201 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15202 
15203 	/*
15204 	 * For backward compatibility, we create net broadcast IREs based on
15205 	 * the old "IP address class system", since some old machines only
15206 	 * respond to these class derived net broadcast.  However, we must not
15207 	 * create these net broadcast IREs if the subnetmask is shorter than
15208 	 * the IP address class based derived netmask.  Otherwise, we may
15209 	 * create a net broadcast address which is the same as an IP address
15210 	 * on the subnet -- and then TCP will refuse to talk to that address.
15211 	 */
15212 	if (netmask < subnetmask) {
15213 		addr = netmask & ipif->ipif_subnet;
15214 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15215 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15216 	}
15217 
15218 	/*
15219 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15220 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15221 	 * created.  Creating these broadcast IREs will only create confusion
15222 	 * as `addr' will be the same as the IP address.
15223 	 */
15224 	if (subnetmask != 0xFFFFFFFF) {
15225 		addr = ipif->ipif_subnet;
15226 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15227 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15228 	}
15229 
15230 	return (irep);
15231 }
15232 
15233 /*
15234  * Mirror of ipif_create_bcast_ires()
15235  */
15236 static void
15237 ipif_delete_bcast_ires(ipif_t *ipif)
15238 {
15239 	ipaddr_t	addr;
15240 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15241 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15242 	ill_t		*ill = ipif->ipif_ill;
15243 	zoneid_t	zoneid = ipif->ipif_zoneid;
15244 	ire_t		*ire;
15245 
15246 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15247 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15248 
15249 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15250 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15251 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15252 
15253 	ire = ire_lookup_bcast(ill, 0, zoneid);
15254 	ASSERT(ire != NULL);
15255 	ire_delete(ire); ire_refrele(ire);
15256 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15257 	ASSERT(ire != NULL);
15258 	ire_delete(ire); ire_refrele(ire);
15259 
15260 	/*
15261 	 * For backward compatibility, we create net broadcast IREs based on
15262 	 * the old "IP address class system", since some old machines only
15263 	 * respond to these class derived net broadcast.  However, we must not
15264 	 * create these net broadcast IREs if the subnetmask is shorter than
15265 	 * the IP address class based derived netmask.  Otherwise, we may
15266 	 * create a net broadcast address which is the same as an IP address
15267 	 * on the subnet -- and then TCP will refuse to talk to that address.
15268 	 */
15269 	if (netmask < subnetmask) {
15270 		addr = netmask & ipif->ipif_subnet;
15271 		ire = ire_lookup_bcast(ill, addr, zoneid);
15272 		ASSERT(ire != NULL);
15273 		ire_delete(ire); ire_refrele(ire);
15274 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15275 		ASSERT(ire != NULL);
15276 		ire_delete(ire); ire_refrele(ire);
15277 	}
15278 
15279 	/*
15280 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15281 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15282 	 * created.  Creating these broadcast IREs will only create confusion
15283 	 * as `addr' will be the same as the IP address.
15284 	 */
15285 	if (subnetmask != 0xFFFFFFFF) {
15286 		addr = ipif->ipif_subnet;
15287 		ire = ire_lookup_bcast(ill, addr, zoneid);
15288 		ASSERT(ire != NULL);
15289 		ire_delete(ire); ire_refrele(ire);
15290 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15291 		ASSERT(ire != NULL);
15292 		ire_delete(ire); ire_refrele(ire);
15293 	}
15294 }
15295 
15296 /*
15297  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15298  * from lifr_flags and the name from lifr_name.
15299  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15300  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15301  * Returns EINPROGRESS when mp has been consumed by queueing it on
15302  * ipx_pending_mp and the ioctl will complete in ip_rput.
15303  *
15304  * Can operate on either a module or a driver queue.
15305  * Returns an error if not a module queue.
15306  */
15307 /* ARGSUSED */
15308 int
15309 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15310     ip_ioctl_cmd_t *ipip, void *if_req)
15311 {
15312 	ill_t	*ill = q->q_ptr;
15313 	phyint_t *phyi;
15314 	ip_stack_t *ipst;
15315 	struct lifreq *lifr = if_req;
15316 	uint64_t new_flags;
15317 
15318 	ASSERT(ipif != NULL);
15319 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15320 
15321 	if (q->q_next == NULL) {
15322 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15323 		return (EINVAL);
15324 	}
15325 
15326 	/*
15327 	 * If we are not writer on 'q' then this interface exists already
15328 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15329 	 * so return EALREADY.
15330 	 */
15331 	if (ill != ipif->ipif_ill)
15332 		return (EALREADY);
15333 
15334 	if (ill->ill_name[0] != '\0')
15335 		return (EALREADY);
15336 
15337 	/*
15338 	 * If there's another ill already with the requested name, ensure
15339 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15340 	 * fuse together two unrelated ills, which will cause chaos.
15341 	 */
15342 	ipst = ill->ill_ipst;
15343 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15344 	    lifr->lifr_name, NULL);
15345 	if (phyi != NULL) {
15346 		ill_t *ill_mate = phyi->phyint_illv4;
15347 
15348 		if (ill_mate == NULL)
15349 			ill_mate = phyi->phyint_illv6;
15350 		ASSERT(ill_mate != NULL);
15351 
15352 		if (ill_mate->ill_media->ip_m_mac_type !=
15353 		    ill->ill_media->ip_m_mac_type) {
15354 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15355 			    "use the same ill name on differing media\n"));
15356 			return (EINVAL);
15357 		}
15358 	}
15359 
15360 	/*
15361 	 * We start off as IFF_IPV4 in ipif_allocate and become
15362 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15363 	 * The only flags that we read from user space are IFF_IPV4,
15364 	 * IFF_IPV6, and IFF_BROADCAST.
15365 	 *
15366 	 * This ill has not been inserted into the global list.
15367 	 * So we are still single threaded and don't need any lock
15368 	 *
15369 	 * Saniy check the flags.
15370 	 */
15371 
15372 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15373 	    ((lifr->lifr_flags & IFF_IPV6) ||
15374 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15375 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15376 		    "or IPv6 i.e., no broadcast \n"));
15377 		return (EINVAL);
15378 	}
15379 
15380 	new_flags =
15381 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15382 
15383 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15384 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15385 		    "IFF_IPV4 or IFF_IPV6\n"));
15386 		return (EINVAL);
15387 	}
15388 
15389 	/*
15390 	 * We always start off as IPv4, so only need to check for IPv6.
15391 	 */
15392 	if ((new_flags & IFF_IPV6) != 0) {
15393 		ill->ill_flags |= ILLF_IPV6;
15394 		ill->ill_flags &= ~ILLF_IPV4;
15395 	}
15396 
15397 	if ((new_flags & IFF_BROADCAST) != 0)
15398 		ipif->ipif_flags |= IPIF_BROADCAST;
15399 	else
15400 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15401 
15402 	/* We started off as V4. */
15403 	if (ill->ill_flags & ILLF_IPV6) {
15404 		ill->ill_phyint->phyint_illv6 = ill;
15405 		ill->ill_phyint->phyint_illv4 = NULL;
15406 	}
15407 
15408 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15409 }
15410 
15411 /* ARGSUSED */
15412 int
15413 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15414     ip_ioctl_cmd_t *ipip, void *if_req)
15415 {
15416 	/*
15417 	 * ill_phyint_reinit merged the v4 and v6 into a single
15418 	 * ipsq.  We might not have been able to complete the
15419 	 * slifname in ipif_set_values, if we could not become
15420 	 * exclusive.  If so restart it here
15421 	 */
15422 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15423 }
15424 
15425 /*
15426  * Return a pointer to the ipif which matches the index, IP version type and
15427  * zoneid.
15428  */
15429 ipif_t *
15430 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15431     ip_stack_t *ipst)
15432 {
15433 	ill_t	*ill;
15434 	ipif_t	*ipif = NULL;
15435 
15436 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15437 	if (ill != NULL) {
15438 		mutex_enter(&ill->ill_lock);
15439 		for (ipif = ill->ill_ipif; ipif != NULL;
15440 		    ipif = ipif->ipif_next) {
15441 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15442 			    zoneid == ipif->ipif_zoneid ||
15443 			    ipif->ipif_zoneid == ALL_ZONES)) {
15444 				ipif_refhold_locked(ipif);
15445 				break;
15446 			}
15447 		}
15448 		mutex_exit(&ill->ill_lock);
15449 		ill_refrele(ill);
15450 	}
15451 	return (ipif);
15452 }
15453 
15454 /*
15455  * Change an existing physical interface's index. If the new index
15456  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15457  * Finally, we update other systems which may have a dependence on the
15458  * index value.
15459  */
15460 /* ARGSUSED */
15461 int
15462 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15463     ip_ioctl_cmd_t *ipip, void *ifreq)
15464 {
15465 	ill_t		*ill;
15466 	phyint_t	*phyi;
15467 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15468 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15469 	uint_t	old_index, index;
15470 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15471 	avl_index_t	where;
15472 
15473 	if (ipip->ipi_cmd_type == IF_CMD)
15474 		index = ifr->ifr_index;
15475 	else
15476 		index = lifr->lifr_index;
15477 
15478 	/*
15479 	 * Only allow on physical interface. Also, index zero is illegal.
15480 	 */
15481 	ill = ipif->ipif_ill;
15482 	phyi = ill->ill_phyint;
15483 	if (ipif->ipif_id != 0 || index == 0) {
15484 		return (EINVAL);
15485 	}
15486 
15487 	/* If the index is not changing, no work to do */
15488 	if (phyi->phyint_ifindex == index)
15489 		return (0);
15490 
15491 	/*
15492 	 * Use phyint_exists() to determine if the new interface index
15493 	 * is already in use. If the index is unused then we need to
15494 	 * change the phyint's position in the phyint_list_avl_by_index
15495 	 * tree. If we do not do this, subsequent lookups (using the new
15496 	 * index value) will not find the phyint.
15497 	 */
15498 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15499 	if (phyint_exists(index, ipst)) {
15500 		rw_exit(&ipst->ips_ill_g_lock);
15501 		return (EEXIST);
15502 	}
15503 
15504 	/*
15505 	 * The new index is unused. Set it in the phyint. However we must not
15506 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15507 	 * changes. The event must be bound to old ifindex value.
15508 	 */
15509 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15510 	    &index, sizeof (index));
15511 
15512 	old_index = phyi->phyint_ifindex;
15513 	phyi->phyint_ifindex = index;
15514 
15515 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15516 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15517 	    &index, &where);
15518 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15519 	    phyi, where);
15520 	rw_exit(&ipst->ips_ill_g_lock);
15521 
15522 	/* Update SCTP's ILL list */
15523 	sctp_ill_reindex(ill, old_index);
15524 
15525 	/* Send the routing sockets message */
15526 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15527 	if (ILL_OTHER(ill))
15528 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15529 
15530 	/* Perhaps ilgs should use this ill */
15531 	update_conn_ill(NULL, ill->ill_ipst);
15532 	return (0);
15533 }
15534 
15535 /* ARGSUSED */
15536 int
15537 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15538     ip_ioctl_cmd_t *ipip, void *ifreq)
15539 {
15540 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15541 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15542 
15543 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15544 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15545 	/* Get the interface index */
15546 	if (ipip->ipi_cmd_type == IF_CMD) {
15547 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15548 	} else {
15549 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15550 	}
15551 	return (0);
15552 }
15553 
15554 /* ARGSUSED */
15555 int
15556 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15557     ip_ioctl_cmd_t *ipip, void *ifreq)
15558 {
15559 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15560 
15561 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15562 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15563 	/* Get the interface zone */
15564 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15565 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15566 	return (0);
15567 }
15568 
15569 /*
15570  * Set the zoneid of an interface.
15571  */
15572 /* ARGSUSED */
15573 int
15574 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15575     ip_ioctl_cmd_t *ipip, void *ifreq)
15576 {
15577 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15578 	int err = 0;
15579 	boolean_t need_up = B_FALSE;
15580 	zone_t *zptr;
15581 	zone_status_t status;
15582 	zoneid_t zoneid;
15583 
15584 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15585 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15586 		if (!is_system_labeled())
15587 			return (ENOTSUP);
15588 		zoneid = GLOBAL_ZONEID;
15589 	}
15590 
15591 	/* cannot assign instance zero to a non-global zone */
15592 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15593 		return (ENOTSUP);
15594 
15595 	/*
15596 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15597 	 * the event of a race with the zone shutdown processing, since IP
15598 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15599 	 * interface will be cleaned up even if the zone is shut down
15600 	 * immediately after the status check. If the interface can't be brought
15601 	 * down right away, and the zone is shut down before the restart
15602 	 * function is called, we resolve the possible races by rechecking the
15603 	 * zone status in the restart function.
15604 	 */
15605 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15606 		return (EINVAL);
15607 	status = zone_status_get(zptr);
15608 	zone_rele(zptr);
15609 
15610 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15611 		return (EINVAL);
15612 
15613 	if (ipif->ipif_flags & IPIF_UP) {
15614 		/*
15615 		 * If the interface is already marked up,
15616 		 * we call ipif_down which will take care
15617 		 * of ditching any IREs that have been set
15618 		 * up based on the old interface address.
15619 		 */
15620 		err = ipif_logical_down(ipif, q, mp);
15621 		if (err == EINPROGRESS)
15622 			return (err);
15623 		(void) ipif_down_tail(ipif);
15624 		need_up = B_TRUE;
15625 	}
15626 
15627 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15628 	return (err);
15629 }
15630 
15631 static int
15632 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15633     queue_t *q, mblk_t *mp, boolean_t need_up)
15634 {
15635 	int	err = 0;
15636 	ip_stack_t	*ipst;
15637 
15638 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15639 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15640 
15641 	if (CONN_Q(q))
15642 		ipst = CONNQ_TO_IPST(q);
15643 	else
15644 		ipst = ILLQ_TO_IPST(q);
15645 
15646 	/*
15647 	 * For exclusive stacks we don't allow a different zoneid than
15648 	 * global.
15649 	 */
15650 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15651 	    zoneid != GLOBAL_ZONEID)
15652 		return (EINVAL);
15653 
15654 	/* Set the new zone id. */
15655 	ipif->ipif_zoneid = zoneid;
15656 
15657 	/* Update sctp list */
15658 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15659 
15660 	/* The default multicast interface might have changed */
15661 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15662 
15663 	if (need_up) {
15664 		/*
15665 		 * Now bring the interface back up.  If this
15666 		 * is the only IPIF for the ILL, ipif_up
15667 		 * will have to re-bind to the device, so
15668 		 * we may get back EINPROGRESS, in which
15669 		 * case, this IOCTL will get completed in
15670 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15671 		 */
15672 		err = ipif_up(ipif, q, mp);
15673 	}
15674 	return (err);
15675 }
15676 
15677 /* ARGSUSED */
15678 int
15679 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15680     ip_ioctl_cmd_t *ipip, void *if_req)
15681 {
15682 	struct lifreq *lifr = (struct lifreq *)if_req;
15683 	zoneid_t zoneid;
15684 	zone_t *zptr;
15685 	zone_status_t status;
15686 
15687 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15688 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15689 		zoneid = GLOBAL_ZONEID;
15690 
15691 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15692 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15693 
15694 	/*
15695 	 * We recheck the zone status to resolve the following race condition:
15696 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15697 	 * 2) hme0:1 is up and can't be brought down right away;
15698 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15699 	 * 3) zone "myzone" is halted; the zone status switches to
15700 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15701 	 * the interfaces to remove - hme0:1 is not returned because it's not
15702 	 * yet in "myzone", so it won't be removed;
15703 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15704 	 * status check here, we would have hme0:1 in "myzone" after it's been
15705 	 * destroyed.
15706 	 * Note that if the status check fails, we need to bring the interface
15707 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15708 	 * ipif_up_done[_v6]().
15709 	 */
15710 	status = ZONE_IS_UNINITIALIZED;
15711 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15712 		status = zone_status_get(zptr);
15713 		zone_rele(zptr);
15714 	}
15715 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15716 		if (ipif->ipif_isv6) {
15717 			(void) ipif_up_done_v6(ipif);
15718 		} else {
15719 			(void) ipif_up_done(ipif);
15720 		}
15721 		return (EINVAL);
15722 	}
15723 
15724 	(void) ipif_down_tail(ipif);
15725 
15726 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15727 	    B_TRUE));
15728 }
15729 
15730 /*
15731  * Return the number of addresses on `ill' with one or more of the values
15732  * in `set' set and all of the values in `clear' clear.
15733  */
15734 static uint_t
15735 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15736 {
15737 	ipif_t	*ipif;
15738 	uint_t	cnt = 0;
15739 
15740 	ASSERT(IAM_WRITER_ILL(ill));
15741 
15742 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15743 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15744 			cnt++;
15745 
15746 	return (cnt);
15747 }
15748 
15749 /*
15750  * Return the number of migratable addresses on `ill' that are under
15751  * application control.
15752  */
15753 uint_t
15754 ill_appaddr_cnt(const ill_t *ill)
15755 {
15756 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15757 	    IPIF_NOFAILOVER));
15758 }
15759 
15760 /*
15761  * Return the number of point-to-point addresses on `ill'.
15762  */
15763 uint_t
15764 ill_ptpaddr_cnt(const ill_t *ill)
15765 {
15766 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15767 }
15768 
15769 /* ARGSUSED */
15770 int
15771 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15772 	ip_ioctl_cmd_t *ipip, void *ifreq)
15773 {
15774 	struct lifreq	*lifr = ifreq;
15775 
15776 	ASSERT(q->q_next == NULL);
15777 	ASSERT(CONN_Q(q));
15778 
15779 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15780 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15781 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15782 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15783 
15784 	return (0);
15785 }
15786 
15787 /* Find the previous ILL in this usesrc group */
15788 static ill_t *
15789 ill_prev_usesrc(ill_t *uill)
15790 {
15791 	ill_t *ill;
15792 
15793 	for (ill = uill->ill_usesrc_grp_next;
15794 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15795 	    ill = ill->ill_usesrc_grp_next)
15796 		/* do nothing */;
15797 	return (ill);
15798 }
15799 
15800 /*
15801  * Release all members of the usesrc group. This routine is called
15802  * from ill_delete when the interface being unplumbed is the
15803  * group head.
15804  *
15805  * This silently clears the usesrc that ifconfig setup.
15806  * An alternative would be to keep that ifindex, and drop packets on the floor
15807  * since no source address can be selected.
15808  * Even if we keep the current semantics, don't need a lock and a linked list.
15809  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15810  * the one that is being removed. Issue is how we return the usesrc users
15811  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15812  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15813  * ill walk, but the walker would need to insert in the ioctl response.
15814  */
15815 static void
15816 ill_disband_usesrc_group(ill_t *uill)
15817 {
15818 	ill_t *next_ill, *tmp_ill;
15819 	ip_stack_t	*ipst = uill->ill_ipst;
15820 
15821 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15822 	next_ill = uill->ill_usesrc_grp_next;
15823 
15824 	do {
15825 		ASSERT(next_ill != NULL);
15826 		tmp_ill = next_ill->ill_usesrc_grp_next;
15827 		ASSERT(tmp_ill != NULL);
15828 		next_ill->ill_usesrc_grp_next = NULL;
15829 		next_ill->ill_usesrc_ifindex = 0;
15830 		next_ill = tmp_ill;
15831 	} while (next_ill->ill_usesrc_ifindex != 0);
15832 	uill->ill_usesrc_grp_next = NULL;
15833 }
15834 
15835 /*
15836  * Remove the client usesrc ILL from the list and relink to a new list
15837  */
15838 int
15839 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15840 {
15841 	ill_t *ill, *tmp_ill;
15842 	ip_stack_t	*ipst = ucill->ill_ipst;
15843 
15844 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15845 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15846 
15847 	/*
15848 	 * Check if the usesrc client ILL passed in is not already
15849 	 * in use as a usesrc ILL i.e one whose source address is
15850 	 * in use OR a usesrc ILL is not already in use as a usesrc
15851 	 * client ILL
15852 	 */
15853 	if ((ucill->ill_usesrc_ifindex == 0) ||
15854 	    (uill->ill_usesrc_ifindex != 0)) {
15855 		return (-1);
15856 	}
15857 
15858 	ill = ill_prev_usesrc(ucill);
15859 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15860 
15861 	/* Remove from the current list */
15862 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15863 		/* Only two elements in the list */
15864 		ASSERT(ill->ill_usesrc_ifindex == 0);
15865 		ill->ill_usesrc_grp_next = NULL;
15866 	} else {
15867 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15868 	}
15869 
15870 	if (ifindex == 0) {
15871 		ucill->ill_usesrc_ifindex = 0;
15872 		ucill->ill_usesrc_grp_next = NULL;
15873 		return (0);
15874 	}
15875 
15876 	ucill->ill_usesrc_ifindex = ifindex;
15877 	tmp_ill = uill->ill_usesrc_grp_next;
15878 	uill->ill_usesrc_grp_next = ucill;
15879 	ucill->ill_usesrc_grp_next =
15880 	    (tmp_ill != NULL) ? tmp_ill : uill;
15881 	return (0);
15882 }
15883 
15884 /*
15885  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15886  * ip.c for locking details.
15887  */
15888 /* ARGSUSED */
15889 int
15890 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15891     ip_ioctl_cmd_t *ipip, void *ifreq)
15892 {
15893 	struct lifreq *lifr = (struct lifreq *)ifreq;
15894 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15895 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15896 	int err = 0, ret;
15897 	uint_t ifindex;
15898 	ipsq_t *ipsq = NULL;
15899 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15900 
15901 	ASSERT(IAM_WRITER_IPIF(ipif));
15902 	ASSERT(q->q_next == NULL);
15903 	ASSERT(CONN_Q(q));
15904 
15905 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15906 
15907 	ifindex = lifr->lifr_index;
15908 	if (ifindex == 0) {
15909 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15910 			/* non usesrc group interface, nothing to reset */
15911 			return (0);
15912 		}
15913 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15914 		/* valid reset request */
15915 		reset_flg = B_TRUE;
15916 	}
15917 
15918 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15919 	if (usesrc_ill == NULL) {
15920 		return (ENXIO);
15921 	}
15922 
15923 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15924 	    NEW_OP, B_TRUE);
15925 	if (ipsq == NULL) {
15926 		err = EINPROGRESS;
15927 		/* Operation enqueued on the ipsq of the usesrc ILL */
15928 		goto done;
15929 	}
15930 
15931 	/* USESRC isn't currently supported with IPMP */
15932 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15933 		err = ENOTSUP;
15934 		goto done;
15935 	}
15936 
15937 	/*
15938 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15939 	 * used by IPMP underlying interfaces, but someone might think it's
15940 	 * more general and try to use it independently with VNI.)
15941 	 */
15942 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15943 		err = ENOTSUP;
15944 		goto done;
15945 	}
15946 
15947 	/*
15948 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15949 	 * already a client then return EINVAL
15950 	 */
15951 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15952 		err = EINVAL;
15953 		goto done;
15954 	}
15955 
15956 	/*
15957 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15958 	 * be then this is a duplicate operation.
15959 	 */
15960 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15961 		err = 0;
15962 		goto done;
15963 	}
15964 
15965 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15966 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15967 	    usesrc_ill->ill_isv6));
15968 
15969 	/*
15970 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15971 	 * and the ill_usesrc_ifindex fields
15972 	 */
15973 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15974 
15975 	if (reset_flg) {
15976 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15977 		if (ret != 0) {
15978 			err = EINVAL;
15979 		}
15980 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15981 		goto done;
15982 	}
15983 
15984 	/*
15985 	 * Four possibilities to consider:
15986 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15987 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15988 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15989 	 * 4. Both are part of their respective usesrc groups
15990 	 */
15991 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15992 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15993 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
15994 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15995 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15996 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
15997 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
15998 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15999 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16000 		/* Insert at head of list */
16001 		usesrc_cli_ill->ill_usesrc_grp_next =
16002 		    usesrc_ill->ill_usesrc_grp_next;
16003 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16004 	} else {
16005 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16006 		    ifindex);
16007 		if (ret != 0)
16008 			err = EINVAL;
16009 	}
16010 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16011 
16012 done:
16013 	if (ipsq != NULL)
16014 		ipsq_exit(ipsq);
16015 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16016 	ill_refrele(usesrc_ill);
16017 
16018 	/* Let conn_ixa caching know that source address selection changed */
16019 	ip_update_source_selection(ipst);
16020 
16021 	return (err);
16022 }
16023 
16024 /*
16025  * comparison function used by avl.
16026  */
16027 static int
16028 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16029 {
16030 
16031 	uint_t index;
16032 
16033 	ASSERT(phyip != NULL && index_ptr != NULL);
16034 
16035 	index = *((uint_t *)index_ptr);
16036 	/*
16037 	 * let the phyint with the lowest index be on top.
16038 	 */
16039 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16040 		return (1);
16041 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16042 		return (-1);
16043 	return (0);
16044 }
16045 
16046 /*
16047  * comparison function used by avl.
16048  */
16049 static int
16050 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16051 {
16052 	ill_t *ill;
16053 	int res = 0;
16054 
16055 	ASSERT(phyip != NULL && name_ptr != NULL);
16056 
16057 	if (((phyint_t *)phyip)->phyint_illv4)
16058 		ill = ((phyint_t *)phyip)->phyint_illv4;
16059 	else
16060 		ill = ((phyint_t *)phyip)->phyint_illv6;
16061 	ASSERT(ill != NULL);
16062 
16063 	res = strcmp(ill->ill_name, (char *)name_ptr);
16064 	if (res > 0)
16065 		return (1);
16066 	else if (res < 0)
16067 		return (-1);
16068 	return (0);
16069 }
16070 
16071 /*
16072  * This function is called on the unplumb path via ill_glist_delete() when
16073  * there are no ills left on the phyint and thus the phyint can be freed.
16074  */
16075 static void
16076 phyint_free(phyint_t *phyi)
16077 {
16078 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16079 
16080 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16081 
16082 	/*
16083 	 * If this phyint was an IPMP meta-interface, blow away the group.
16084 	 * This is safe to do because all of the illgrps have already been
16085 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16086 	 * If we're cleaning up as a result of failed initialization,
16087 	 * phyint_grp may be NULL.
16088 	 */
16089 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16090 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16091 		ipmp_grp_destroy(phyi->phyint_grp);
16092 		phyi->phyint_grp = NULL;
16093 		rw_exit(&ipst->ips_ipmp_lock);
16094 	}
16095 
16096 	/*
16097 	 * If this interface was under IPMP, take it out of the group.
16098 	 */
16099 	if (phyi->phyint_grp != NULL)
16100 		ipmp_phyint_leave_grp(phyi);
16101 
16102 	/*
16103 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16104 	 * will be freed in ipsq_exit().
16105 	 */
16106 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16107 	phyi->phyint_name[0] = '\0';
16108 
16109 	mi_free(phyi);
16110 }
16111 
16112 /*
16113  * Attach the ill to the phyint structure which can be shared by both
16114  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16115  * function is called from ipif_set_values and ill_lookup_on_name (for
16116  * loopback) where we know the name of the ill. We lookup the ill and if
16117  * there is one present already with the name use that phyint. Otherwise
16118  * reuse the one allocated by ill_init.
16119  */
16120 static void
16121 ill_phyint_reinit(ill_t *ill)
16122 {
16123 	boolean_t isv6 = ill->ill_isv6;
16124 	phyint_t *phyi_old;
16125 	phyint_t *phyi;
16126 	avl_index_t where = 0;
16127 	ill_t	*ill_other = NULL;
16128 	ip_stack_t	*ipst = ill->ill_ipst;
16129 
16130 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16131 
16132 	phyi_old = ill->ill_phyint;
16133 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16134 	    phyi_old->phyint_illv6 == NULL));
16135 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16136 	    phyi_old->phyint_illv4 == NULL));
16137 	ASSERT(phyi_old->phyint_ifindex == 0);
16138 
16139 	/*
16140 	 * Now that our ill has a name, set it in the phyint.
16141 	 */
16142 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16143 
16144 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16145 	    ill->ill_name, &where);
16146 
16147 	/*
16148 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16149 	 *    the global list of ills. So no other thread could have located
16150 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16151 	 * 2. Now locate the other protocol instance of this ill.
16152 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16153 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16154 	 *    of neither ill can change.
16155 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16156 	 *    other ill.
16157 	 * 5. Release all locks.
16158 	 */
16159 
16160 	/*
16161 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16162 	 * we are initializing IPv4.
16163 	 */
16164 	if (phyi != NULL) {
16165 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16166 		ASSERT(ill_other->ill_phyint != NULL);
16167 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16168 		    (!isv6 && ill_other->ill_isv6));
16169 		GRAB_ILL_LOCKS(ill, ill_other);
16170 		/*
16171 		 * We are potentially throwing away phyint_flags which
16172 		 * could be different from the one that we obtain from
16173 		 * ill_other->ill_phyint. But it is okay as we are assuming
16174 		 * that the state maintained within IP is correct.
16175 		 */
16176 		mutex_enter(&phyi->phyint_lock);
16177 		if (isv6) {
16178 			ASSERT(phyi->phyint_illv6 == NULL);
16179 			phyi->phyint_illv6 = ill;
16180 		} else {
16181 			ASSERT(phyi->phyint_illv4 == NULL);
16182 			phyi->phyint_illv4 = ill;
16183 		}
16184 
16185 		/*
16186 		 * Delete the old phyint and make its ipsq eligible
16187 		 * to be freed in ipsq_exit().
16188 		 */
16189 		phyi_old->phyint_illv4 = NULL;
16190 		phyi_old->phyint_illv6 = NULL;
16191 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16192 		phyi_old->phyint_name[0] = '\0';
16193 		mi_free(phyi_old);
16194 	} else {
16195 		mutex_enter(&ill->ill_lock);
16196 		/*
16197 		 * We don't need to acquire any lock, since
16198 		 * the ill is not yet visible globally  and we
16199 		 * have not yet released the ill_g_lock.
16200 		 */
16201 		phyi = phyi_old;
16202 		mutex_enter(&phyi->phyint_lock);
16203 		/* XXX We need a recovery strategy here. */
16204 		if (!phyint_assign_ifindex(phyi, ipst))
16205 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16206 
16207 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16208 		    (void *)phyi, where);
16209 
16210 		(void) avl_find(&ipst->ips_phyint_g_list->
16211 		    phyint_list_avl_by_index,
16212 		    &phyi->phyint_ifindex, &where);
16213 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16214 		    (void *)phyi, where);
16215 	}
16216 
16217 	/*
16218 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16219 	 * pending mp is not affected because that is per ill basis.
16220 	 */
16221 	ill->ill_phyint = phyi;
16222 
16223 	/*
16224 	 * Now that the phyint's ifindex has been assigned, complete the
16225 	 * remaining
16226 	 */
16227 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16228 	if (ill->ill_isv6) {
16229 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16230 		    ill->ill_phyint->phyint_ifindex;
16231 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16232 	} else {
16233 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16234 	}
16235 
16236 	/*
16237 	 * Generate an event within the hooks framework to indicate that
16238 	 * a new interface has just been added to IP.  For this event to
16239 	 * be generated, the network interface must, at least, have an
16240 	 * ifindex assigned to it.  (We don't generate the event for
16241 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16242 	 *
16243 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16244 	 * that the ordering of delivered events to listeners matches the
16245 	 * order of them in the kernel.
16246 	 */
16247 	if (!IS_LOOPBACK(ill)) {
16248 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16249 		    ill->ill_name_length);
16250 	}
16251 	RELEASE_ILL_LOCKS(ill, ill_other);
16252 	mutex_exit(&phyi->phyint_lock);
16253 }
16254 
16255 /*
16256  * Notify any downstream modules of the name of this interface.
16257  * An M_IOCTL is used even though we don't expect a successful reply.
16258  * Any reply message from the driver (presumably an M_IOCNAK) will
16259  * eventually get discarded somewhere upstream.  The message format is
16260  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16261  * to IP.
16262  */
16263 static void
16264 ip_ifname_notify(ill_t *ill, queue_t *q)
16265 {
16266 	mblk_t *mp1, *mp2;
16267 	struct iocblk *iocp;
16268 	struct lifreq *lifr;
16269 
16270 	mp1 = mkiocb(SIOCSLIFNAME);
16271 	if (mp1 == NULL)
16272 		return;
16273 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16274 	if (mp2 == NULL) {
16275 		freeb(mp1);
16276 		return;
16277 	}
16278 
16279 	mp1->b_cont = mp2;
16280 	iocp = (struct iocblk *)mp1->b_rptr;
16281 	iocp->ioc_count = sizeof (struct lifreq);
16282 
16283 	lifr = (struct lifreq *)mp2->b_rptr;
16284 	mp2->b_wptr += sizeof (struct lifreq);
16285 	bzero(lifr, sizeof (struct lifreq));
16286 
16287 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16288 	lifr->lifr_ppa = ill->ill_ppa;
16289 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16290 
16291 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16292 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16293 	putnext(q, mp1);
16294 }
16295 
16296 static int
16297 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16298 {
16299 	int		err;
16300 	ip_stack_t	*ipst = ill->ill_ipst;
16301 	phyint_t	*phyi = ill->ill_phyint;
16302 
16303 	/* Set the obsolete NDD per-interface forwarding name. */
16304 	err = ill_set_ndd_name(ill);
16305 	if (err != 0) {
16306 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16307 		    err);
16308 	}
16309 
16310 	/*
16311 	 * Now that ill_name is set, the configuration for the IPMP
16312 	 * meta-interface can be performed.
16313 	 */
16314 	if (IS_IPMP(ill)) {
16315 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16316 		/*
16317 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16318 		 * meta-interface and we need to create the IPMP group.
16319 		 */
16320 		if (phyi->phyint_grp == NULL) {
16321 			/*
16322 			 * If someone has renamed another IPMP group to have
16323 			 * the same name as our interface, bail.
16324 			 */
16325 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16326 				rw_exit(&ipst->ips_ipmp_lock);
16327 				return (EEXIST);
16328 			}
16329 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16330 			if (phyi->phyint_grp == NULL) {
16331 				rw_exit(&ipst->ips_ipmp_lock);
16332 				return (ENOMEM);
16333 			}
16334 		}
16335 		rw_exit(&ipst->ips_ipmp_lock);
16336 	}
16337 
16338 	/* Tell downstream modules where they are. */
16339 	ip_ifname_notify(ill, q);
16340 
16341 	/*
16342 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16343 	 * Error cases are ENOMEM ...
16344 	 */
16345 	err = ill_dl_phys(ill, ipif, mp, q);
16346 
16347 	if (ill->ill_isv6) {
16348 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16349 		if (ipst->ips_mld_slowtimeout_id == 0) {
16350 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16351 			    (void *)ipst,
16352 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16353 		}
16354 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16355 	} else {
16356 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16357 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16358 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16359 			    (void *)ipst,
16360 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16361 		}
16362 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16363 	}
16364 
16365 	return (err);
16366 }
16367 
16368 /*
16369  * Common routine for ppa and ifname setting. Should be called exclusive.
16370  *
16371  * Returns EINPROGRESS when mp has been consumed by queueing it on
16372  * ipx_pending_mp and the ioctl will complete in ip_rput.
16373  *
16374  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16375  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16376  * For SLIFNAME, we pass these values back to the userland.
16377  */
16378 static int
16379 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16380 {
16381 	ill_t	*ill;
16382 	ipif_t	*ipif;
16383 	ipsq_t	*ipsq;
16384 	char	*ppa_ptr;
16385 	char	*old_ptr;
16386 	char	old_char;
16387 	int	error;
16388 	ip_stack_t	*ipst;
16389 
16390 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16391 	ASSERT(q->q_next != NULL);
16392 	ASSERT(interf_name != NULL);
16393 
16394 	ill = (ill_t *)q->q_ptr;
16395 	ipst = ill->ill_ipst;
16396 
16397 	ASSERT(ill->ill_ipst != NULL);
16398 	ASSERT(ill->ill_name[0] == '\0');
16399 	ASSERT(IAM_WRITER_ILL(ill));
16400 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16401 	ASSERT(ill->ill_ppa == UINT_MAX);
16402 
16403 	ill->ill_defend_start = ill->ill_defend_count = 0;
16404 	/* The ppa is sent down by ifconfig or is chosen */
16405 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16406 		return (EINVAL);
16407 	}
16408 
16409 	/*
16410 	 * make sure ppa passed in is same as ppa in the name.
16411 	 * This check is not made when ppa == UINT_MAX in that case ppa
16412 	 * in the name could be anything. System will choose a ppa and
16413 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16414 	 */
16415 	if (*new_ppa_ptr != UINT_MAX) {
16416 		/* stoi changes the pointer */
16417 		old_ptr = ppa_ptr;
16418 		/*
16419 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16420 		 * (they don't have an externally visible ppa).  We assign one
16421 		 * here so that we can manage the interface.  Note that in
16422 		 * the past this value was always 0 for DLPI 1 drivers.
16423 		 */
16424 		if (*new_ppa_ptr == 0)
16425 			*new_ppa_ptr = stoi(&old_ptr);
16426 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16427 			return (EINVAL);
16428 	}
16429 	/*
16430 	 * terminate string before ppa
16431 	 * save char at that location.
16432 	 */
16433 	old_char = ppa_ptr[0];
16434 	ppa_ptr[0] = '\0';
16435 
16436 	ill->ill_ppa = *new_ppa_ptr;
16437 	/*
16438 	 * Finish as much work now as possible before calling ill_glist_insert
16439 	 * which makes the ill globally visible and also merges it with the
16440 	 * other protocol instance of this phyint. The remaining work is
16441 	 * done after entering the ipsq which may happen sometime later.
16442 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16443 	 */
16444 	ipif = ill->ill_ipif;
16445 
16446 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16447 	ipif_assign_seqid(ipif);
16448 
16449 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16450 		ill->ill_flags |= ILLF_IPV4;
16451 
16452 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16453 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16454 
16455 	if (ill->ill_flags & ILLF_IPV6) {
16456 
16457 		ill->ill_isv6 = B_TRUE;
16458 		ill_set_inputfn(ill);
16459 		if (ill->ill_rq != NULL) {
16460 			ill->ill_rq->q_qinfo = &iprinitv6;
16461 		}
16462 
16463 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16464 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16465 		ipif->ipif_v6subnet = ipv6_all_zeros;
16466 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16467 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16468 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16469 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16470 		/*
16471 		 * point-to-point or Non-mulicast capable
16472 		 * interfaces won't do NUD unless explicitly
16473 		 * configured to do so.
16474 		 */
16475 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16476 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16477 			ill->ill_flags |= ILLF_NONUD;
16478 		}
16479 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16480 		if (ill->ill_flags & ILLF_NOARP) {
16481 			/*
16482 			 * Note: xresolv interfaces will eventually need
16483 			 * NOARP set here as well, but that will require
16484 			 * those external resolvers to have some
16485 			 * knowledge of that flag and act appropriately.
16486 			 * Not to be changed at present.
16487 			 */
16488 			ill->ill_flags &= ~ILLF_NOARP;
16489 		}
16490 		/*
16491 		 * Set the ILLF_ROUTER flag according to the global
16492 		 * IPv6 forwarding policy.
16493 		 */
16494 		if (ipst->ips_ipv6_forward != 0)
16495 			ill->ill_flags |= ILLF_ROUTER;
16496 	} else if (ill->ill_flags & ILLF_IPV4) {
16497 		ill->ill_isv6 = B_FALSE;
16498 		ill_set_inputfn(ill);
16499 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16500 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16501 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16502 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16503 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16504 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16505 		/*
16506 		 * Set the ILLF_ROUTER flag according to the global
16507 		 * IPv4 forwarding policy.
16508 		 */
16509 		if (ipst->ips_ip_g_forward != 0)
16510 			ill->ill_flags |= ILLF_ROUTER;
16511 	}
16512 
16513 	ASSERT(ill->ill_phyint != NULL);
16514 
16515 	/*
16516 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16517 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16518 	 */
16519 	if (!ill_allocate_mibs(ill))
16520 		return (ENOMEM);
16521 
16522 	/*
16523 	 * Pick a default sap until we get the DL_INFO_ACK back from
16524 	 * the driver.
16525 	 */
16526 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16527 	    ill->ill_media->ip_m_ipv4sap;
16528 
16529 	ill->ill_ifname_pending = 1;
16530 	ill->ill_ifname_pending_err = 0;
16531 
16532 	/*
16533 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16534 	 * that were joined while this ill was not bound to the DLPI link need
16535 	 * to be recovered by ill_recover_multicast().
16536 	 */
16537 	ill->ill_need_recover_multicast = 1;
16538 
16539 	ill_refhold(ill);
16540 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16541 	if ((error = ill_glist_insert(ill, interf_name,
16542 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16543 		ill->ill_ppa = UINT_MAX;
16544 		ill->ill_name[0] = '\0';
16545 		/*
16546 		 * undo null termination done above.
16547 		 */
16548 		ppa_ptr[0] = old_char;
16549 		rw_exit(&ipst->ips_ill_g_lock);
16550 		ill_refrele(ill);
16551 		return (error);
16552 	}
16553 
16554 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16555 
16556 	/*
16557 	 * When we return the buffer pointed to by interf_name should contain
16558 	 * the same name as in ill_name.
16559 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16560 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16561 	 * so copy full name and update the ppa ptr.
16562 	 * When ppa passed in != UINT_MAX all values are correct just undo
16563 	 * null termination, this saves a bcopy.
16564 	 */
16565 	if (*new_ppa_ptr == UINT_MAX) {
16566 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16567 		*new_ppa_ptr = ill->ill_ppa;
16568 	} else {
16569 		/*
16570 		 * undo null termination done above.
16571 		 */
16572 		ppa_ptr[0] = old_char;
16573 	}
16574 
16575 	/* Let SCTP know about this ILL */
16576 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16577 
16578 	/*
16579 	 * ill_glist_insert has made the ill visible globally, and
16580 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16581 	 * we need to hold the ips_ill_g_lock across the call to enter the
16582 	 * ipsq to enforce atomicity and prevent reordering. In the event
16583 	 * the ipsq has changed, and if the new ipsq is currently busy,
16584 	 * we need to make sure that this half-completed ioctl is ahead of
16585 	 * any subsequent ioctl. We achieve this by not dropping the
16586 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16587 	 * ensuring that new ioctls can't start.
16588 	 */
16589 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16590 	    B_TRUE);
16591 
16592 	rw_exit(&ipst->ips_ill_g_lock);
16593 	ill_refrele(ill);
16594 	if (ipsq == NULL)
16595 		return (EINPROGRESS);
16596 
16597 	/*
16598 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16599 	 */
16600 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16601 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16602 	else
16603 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16604 
16605 	error = ipif_set_values_tail(ill, ipif, mp, q);
16606 	ipsq_exit(ipsq);
16607 	if (error != 0 && error != EINPROGRESS) {
16608 		/*
16609 		 * restore previous values
16610 		 */
16611 		ill->ill_isv6 = B_FALSE;
16612 		ill_set_inputfn(ill);
16613 	}
16614 	return (error);
16615 }
16616 
16617 void
16618 ipif_init(ip_stack_t *ipst)
16619 {
16620 	int i;
16621 
16622 	for (i = 0; i < MAX_G_HEADS; i++) {
16623 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16624 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16625 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16626 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16627 	}
16628 
16629 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16630 	    ill_phyint_compare_index,
16631 	    sizeof (phyint_t),
16632 	    offsetof(struct phyint, phyint_avl_by_index));
16633 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16634 	    ill_phyint_compare_name,
16635 	    sizeof (phyint_t),
16636 	    offsetof(struct phyint, phyint_avl_by_name));
16637 }
16638 
16639 /*
16640  * Save enough information so that we can recreate the IRE if
16641  * the interface goes down and then up.
16642  */
16643 void
16644 ill_save_ire(ill_t *ill, ire_t *ire)
16645 {
16646 	mblk_t	*save_mp;
16647 
16648 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16649 	if (save_mp != NULL) {
16650 		ifrt_t	*ifrt;
16651 
16652 		save_mp->b_wptr += sizeof (ifrt_t);
16653 		ifrt = (ifrt_t *)save_mp->b_rptr;
16654 		bzero(ifrt, sizeof (ifrt_t));
16655 		ifrt->ifrt_type = ire->ire_type;
16656 		if (ire->ire_ipversion == IPV4_VERSION) {
16657 			ASSERT(!ill->ill_isv6);
16658 			ifrt->ifrt_addr = ire->ire_addr;
16659 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16660 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16661 			ifrt->ifrt_mask = ire->ire_mask;
16662 		} else {
16663 			ASSERT(ill->ill_isv6);
16664 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16665 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16666 			mutex_enter(&ire->ire_lock);
16667 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16668 			mutex_exit(&ire->ire_lock);
16669 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16670 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16671 		}
16672 		ifrt->ifrt_flags = ire->ire_flags;
16673 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16674 		mutex_enter(&ill->ill_saved_ire_lock);
16675 		save_mp->b_cont = ill->ill_saved_ire_mp;
16676 		ill->ill_saved_ire_mp = save_mp;
16677 		ill->ill_saved_ire_cnt++;
16678 		mutex_exit(&ill->ill_saved_ire_lock);
16679 	}
16680 }
16681 
16682 /*
16683  * Remove one entry from ill_saved_ire_mp.
16684  */
16685 void
16686 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16687 {
16688 	mblk_t	**mpp;
16689 	mblk_t	*mp;
16690 	ifrt_t	*ifrt;
16691 
16692 	/* Remove from ill_saved_ire_mp list if it is there */
16693 	mutex_enter(&ill->ill_saved_ire_lock);
16694 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16695 	    mpp = &(*mpp)->b_cont) {
16696 		in6_addr_t	gw_addr_v6;
16697 
16698 		/*
16699 		 * On a given ill, the tuple of address, gateway, mask,
16700 		 * ire_type, and zoneid is unique for each saved IRE.
16701 		 */
16702 		mp = *mpp;
16703 		ifrt = (ifrt_t *)mp->b_rptr;
16704 		/* ire_gateway_addr_v6 can change - need lock */
16705 		mutex_enter(&ire->ire_lock);
16706 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16707 		mutex_exit(&ire->ire_lock);
16708 
16709 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16710 		    ifrt->ifrt_type != ire->ire_type)
16711 			continue;
16712 
16713 		if (ill->ill_isv6 ?
16714 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16715 		    &ire->ire_addr_v6) &&
16716 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16717 		    &gw_addr_v6) &&
16718 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16719 		    &ire->ire_mask_v6)) :
16720 		    (ifrt->ifrt_addr == ire->ire_addr &&
16721 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16722 		    ifrt->ifrt_mask == ire->ire_mask)) {
16723 			*mpp = mp->b_cont;
16724 			ill->ill_saved_ire_cnt--;
16725 			freeb(mp);
16726 			break;
16727 		}
16728 	}
16729 	mutex_exit(&ill->ill_saved_ire_lock);
16730 }
16731 
16732 /*
16733  * IP multirouting broadcast routes handling
16734  * Append CGTP broadcast IREs to regular ones created
16735  * at ifconfig time.
16736  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16737  * the destination and the gateway are broadcast addresses.
16738  * The caller has verified that the destination is an IRE_BROADCAST and that
16739  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16740  * we create a MULTIRT IRE_BROADCAST.
16741  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16742  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16743  */
16744 static void
16745 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16746 {
16747 	ire_t *ire_prim;
16748 
16749 	ASSERT(ire != NULL);
16750 
16751 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16752 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16753 	    NULL);
16754 	if (ire_prim != NULL) {
16755 		/*
16756 		 * We are in the special case of broadcasts for
16757 		 * CGTP. We add an IRE_BROADCAST that holds
16758 		 * the RTF_MULTIRT flag, the destination
16759 		 * address and the low level
16760 		 * info of ire_prim. In other words, CGTP
16761 		 * broadcast is added to the redundant ipif.
16762 		 */
16763 		ill_t *ill_prim;
16764 		ire_t  *bcast_ire;
16765 
16766 		ill_prim = ire_prim->ire_ill;
16767 
16768 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16769 		    (void *)ire_prim, (void *)ill_prim));
16770 
16771 		bcast_ire = ire_create(
16772 		    (uchar_t *)&ire->ire_addr,
16773 		    (uchar_t *)&ip_g_all_ones,
16774 		    (uchar_t *)&ire->ire_gateway_addr,
16775 		    IRE_BROADCAST,
16776 		    ill_prim,
16777 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16778 		    ire->ire_flags | RTF_KERNEL,
16779 		    NULL,
16780 		    ipst);
16781 
16782 		/*
16783 		 * Here we assume that ire_add does head insertion so that
16784 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16785 		 */
16786 		if (bcast_ire != NULL) {
16787 			if (ire->ire_flags & RTF_SETSRC) {
16788 				bcast_ire->ire_setsrc_addr =
16789 				    ire->ire_setsrc_addr;
16790 			}
16791 			bcast_ire = ire_add(bcast_ire);
16792 			if (bcast_ire != NULL) {
16793 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16794 				    "added bcast_ire %p\n",
16795 				    (void *)bcast_ire));
16796 
16797 				ill_save_ire(ill_prim, bcast_ire);
16798 				ire_refrele(bcast_ire);
16799 			}
16800 		}
16801 		ire_refrele(ire_prim);
16802 	}
16803 }
16804 
16805 /*
16806  * IP multirouting broadcast routes handling
16807  * Remove the broadcast ire.
16808  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16809  * the destination and the gateway are broadcast addresses.
16810  * The caller has only verified that RTF_MULTIRT was set. We check
16811  * that the destination is broadcast and that the gateway is a broadcast
16812  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16813  */
16814 static void
16815 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16816 {
16817 	ASSERT(ire != NULL);
16818 
16819 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16820 		ire_t *ire_prim;
16821 
16822 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16823 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16824 		    ipst, NULL);
16825 		if (ire_prim != NULL) {
16826 			ill_t *ill_prim;
16827 			ire_t  *bcast_ire;
16828 
16829 			ill_prim = ire_prim->ire_ill;
16830 
16831 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16832 			    "ire_prim %p, ill_prim %p\n",
16833 			    (void *)ire_prim, (void *)ill_prim));
16834 
16835 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16836 			    ire->ire_gateway_addr, IRE_BROADCAST,
16837 			    ill_prim, ALL_ZONES, NULL,
16838 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16839 			    MATCH_IRE_MASK, 0, ipst, NULL);
16840 
16841 			if (bcast_ire != NULL) {
16842 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16843 				    "looked up bcast_ire %p\n",
16844 				    (void *)bcast_ire));
16845 				ill_remove_saved_ire(bcast_ire->ire_ill,
16846 				    bcast_ire);
16847 				ire_delete(bcast_ire);
16848 				ire_refrele(bcast_ire);
16849 			}
16850 			ire_refrele(ire_prim);
16851 		}
16852 	}
16853 }
16854 
16855 /*
16856  * Derive an interface id from the link layer address.
16857  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16858  */
16859 static void
16860 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16861 {
16862 	char		*addr;
16863 
16864 	/*
16865 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16866 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16867 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16868 	 * interface ID on IPv6 interfaces above links that actually have real
16869 	 * Ethernet addresses.
16870 	 */
16871 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16872 		/* Form EUI-64 like address */
16873 		addr = (char *)&v6addr->s6_addr32[2];
16874 		bcopy(ill->ill_phys_addr, addr, 3);
16875 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16876 		addr[3] = (char)0xff;
16877 		addr[4] = (char)0xfe;
16878 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16879 	}
16880 }
16881 
16882 /* ARGSUSED */
16883 static void
16884 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16885 {
16886 }
16887 
16888 typedef struct ipmp_ifcookie {
16889 	uint32_t	ic_hostid;
16890 	char		ic_ifname[LIFNAMSIZ];
16891 	char		ic_zonename[ZONENAME_MAX];
16892 } ipmp_ifcookie_t;
16893 
16894 /*
16895  * Construct a pseudo-random interface ID for the IPMP interface that's both
16896  * predictable and (almost) guaranteed to be unique.
16897  */
16898 static void
16899 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16900 {
16901 	zone_t		*zp;
16902 	uint8_t		*addr;
16903 	uchar_t		hash[16];
16904 	ulong_t 	hostid;
16905 	MD5_CTX		ctx;
16906 	ipmp_ifcookie_t	ic = { 0 };
16907 
16908 	ASSERT(IS_IPMP(ill));
16909 
16910 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16911 	ic.ic_hostid = htonl((uint32_t)hostid);
16912 
16913 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16914 
16915 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16916 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16917 		zone_rele(zp);
16918 	}
16919 
16920 	MD5Init(&ctx);
16921 	MD5Update(&ctx, &ic, sizeof (ic));
16922 	MD5Final(hash, &ctx);
16923 
16924 	/*
16925 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16926 	 */
16927 	addr = &v6addr->s6_addr8[8];
16928 	bcopy(hash + 8, addr, sizeof (uint64_t));
16929 	addr[0] &= ~0x2;				/* set local bit */
16930 }
16931 
16932 /*
16933  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16934  */
16935 static void
16936 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16937 {
16938 	phyint_t *phyi = ill->ill_phyint;
16939 
16940 	/*
16941 	 * Check PHYI_MULTI_BCAST and length of physical
16942 	 * address to determine if we use the mapping or the
16943 	 * broadcast address.
16944 	 */
16945 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16946 	    ill->ill_phys_addr_length != ETHERADDRL) {
16947 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16948 		return;
16949 	}
16950 	m_physaddr[0] = 0x33;
16951 	m_physaddr[1] = 0x33;
16952 	m_physaddr[2] = m_ip6addr[12];
16953 	m_physaddr[3] = m_ip6addr[13];
16954 	m_physaddr[4] = m_ip6addr[14];
16955 	m_physaddr[5] = m_ip6addr[15];
16956 }
16957 
16958 /*
16959  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16960  */
16961 static void
16962 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16963 {
16964 	phyint_t *phyi = ill->ill_phyint;
16965 
16966 	/*
16967 	 * Check PHYI_MULTI_BCAST and length of physical
16968 	 * address to determine if we use the mapping or the
16969 	 * broadcast address.
16970 	 */
16971 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16972 	    ill->ill_phys_addr_length != ETHERADDRL) {
16973 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16974 		return;
16975 	}
16976 	m_physaddr[0] = 0x01;
16977 	m_physaddr[1] = 0x00;
16978 	m_physaddr[2] = 0x5e;
16979 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16980 	m_physaddr[4] = m_ipaddr[2];
16981 	m_physaddr[5] = m_ipaddr[3];
16982 }
16983 
16984 /* ARGSUSED */
16985 static void
16986 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16987 {
16988 	/*
16989 	 * for the MULTI_BCAST case and other cases when we want to
16990 	 * use the link-layer broadcast address for multicast.
16991 	 */
16992 	uint8_t	*bphys_addr;
16993 	dl_unitdata_req_t *dlur;
16994 
16995 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16996 	if (ill->ill_sap_length < 0) {
16997 		bphys_addr = (uchar_t *)dlur +
16998 		    dlur->dl_dest_addr_offset;
16999 	} else  {
17000 		bphys_addr = (uchar_t *)dlur +
17001 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17002 	}
17003 
17004 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17005 }
17006 
17007 /*
17008  * Derive IPoIB interface id from the link layer address.
17009  */
17010 static void
17011 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17012 {
17013 	char		*addr;
17014 
17015 	ASSERT(ill->ill_phys_addr_length == 20);
17016 	addr = (char *)&v6addr->s6_addr32[2];
17017 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17018 	/*
17019 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17020 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17021 	 * rules. In these cases, the IBA considers these GUIDs to be in
17022 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17023 	 * required; vendors are required not to assign global EUI-64's
17024 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17025 	 * of the interface identifier. Whether the GUID is in modified
17026 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17027 	 * bit set to 1.
17028 	 */
17029 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17030 }
17031 
17032 /*
17033  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17034  * Note on mapping from multicast IP addresses to IPoIB multicast link
17035  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17036  * The format of an IPoIB multicast address is:
17037  *
17038  *  4 byte QPN      Scope Sign.  Pkey
17039  * +--------------------------------------------+
17040  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17041  * +--------------------------------------------+
17042  *
17043  * The Scope and Pkey components are properties of the IBA port and
17044  * network interface. They can be ascertained from the broadcast address.
17045  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17046  */
17047 static void
17048 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17049 {
17050 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17051 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17052 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17053 	uint8_t	*bphys_addr;
17054 	dl_unitdata_req_t *dlur;
17055 
17056 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17057 
17058 	/*
17059 	 * RFC 4391: IPv4 MGID is 28-bit long.
17060 	 */
17061 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17062 	m_physaddr[17] = m_ipaddr[1];
17063 	m_physaddr[18] = m_ipaddr[2];
17064 	m_physaddr[19] = m_ipaddr[3];
17065 
17066 
17067 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17068 	if (ill->ill_sap_length < 0) {
17069 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17070 	} else  {
17071 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17072 		    ill->ill_sap_length;
17073 	}
17074 	/*
17075 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17076 	 */
17077 	m_physaddr[5] = bphys_addr[5];
17078 	m_physaddr[8] = bphys_addr[8];
17079 	m_physaddr[9] = bphys_addr[9];
17080 }
17081 
17082 static void
17083 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17084 {
17085 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17086 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17087 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17088 	uint8_t	*bphys_addr;
17089 	dl_unitdata_req_t *dlur;
17090 
17091 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17092 
17093 	/*
17094 	 * RFC 4391: IPv4 MGID is 80-bit long.
17095 	 */
17096 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17097 
17098 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17099 	if (ill->ill_sap_length < 0) {
17100 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17101 	} else  {
17102 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17103 		    ill->ill_sap_length;
17104 	}
17105 	/*
17106 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17107 	 */
17108 	m_physaddr[5] = bphys_addr[5];
17109 	m_physaddr[8] = bphys_addr[8];
17110 	m_physaddr[9] = bphys_addr[9];
17111 }
17112 
17113 /*
17114  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17115  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17116  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17117  * of RFC4213.
17118  */
17119 static void
17120 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17121 {
17122 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17123 	v6addr->s6_addr32[2] = 0;
17124 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17125 }
17126 
17127 /*
17128  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17129  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17130  * id.
17131  */
17132 static void
17133 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17134 {
17135 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17136 
17137 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17138 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17139 }
17140 
17141 static void
17142 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17143 {
17144 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17145 }
17146 
17147 static void
17148 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17149 {
17150 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17151 }
17152 
17153 static void
17154 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17155 {
17156 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17157 }
17158 
17159 static void
17160 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17161 {
17162 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17163 }
17164 
17165 /*
17166  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17167  * Returns an held ill, or NULL.
17168  */
17169 ill_t *
17170 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17171     ip_stack_t *ipst)
17172 {
17173 	ill_t	*ill;
17174 	ipif_t	*ipif;
17175 
17176 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17177 	if (ill == NULL)
17178 		return (NULL);
17179 
17180 	mutex_enter(&ill->ill_lock);
17181 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17182 		if (IPIF_IS_CONDEMNED(ipif))
17183 			continue;
17184 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17185 		    ipif->ipif_zoneid != ALL_ZONES)
17186 			continue;
17187 
17188 		mutex_exit(&ill->ill_lock);
17189 		return (ill);
17190 	}
17191 	mutex_exit(&ill->ill_lock);
17192 	ill_refrele(ill);
17193 	return (NULL);
17194 }
17195 
17196 /*
17197  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17198  * If a pointer to an ipif_t is returned then the caller will need to do
17199  * an ill_refrele().
17200  */
17201 ipif_t *
17202 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17203     ip_stack_t *ipst)
17204 {
17205 	ipif_t *ipif;
17206 	ill_t *ill;
17207 
17208 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17209 	if (ill == NULL)
17210 		return (NULL);
17211 
17212 	mutex_enter(&ill->ill_lock);
17213 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17214 		mutex_exit(&ill->ill_lock);
17215 		ill_refrele(ill);
17216 		return (NULL);
17217 	}
17218 
17219 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17220 		if (!IPIF_CAN_LOOKUP(ipif))
17221 			continue;
17222 		if (lifidx == ipif->ipif_id) {
17223 			ipif_refhold_locked(ipif);
17224 			break;
17225 		}
17226 	}
17227 
17228 	mutex_exit(&ill->ill_lock);
17229 	ill_refrele(ill);
17230 	return (ipif);
17231 }
17232 
17233 /*
17234  * Set ill_inputfn based on the current know state.
17235  * This needs to be called when any of the factors taken into
17236  * account changes.
17237  */
17238 void
17239 ill_set_inputfn(ill_t *ill)
17240 {
17241 	ip_stack_t	*ipst = ill->ill_ipst;
17242 
17243 	if (ill->ill_isv6) {
17244 		if (is_system_labeled())
17245 			ill->ill_inputfn = ill_input_full_v6;
17246 		else
17247 			ill->ill_inputfn = ill_input_short_v6;
17248 	} else {
17249 		if (is_system_labeled())
17250 			ill->ill_inputfn = ill_input_full_v4;
17251 		else if (ill->ill_dhcpinit != 0)
17252 			ill->ill_inputfn = ill_input_full_v4;
17253 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17254 		    != NULL)
17255 			ill->ill_inputfn = ill_input_full_v4;
17256 		else if (ipst->ips_ip_cgtp_filter &&
17257 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17258 			ill->ill_inputfn = ill_input_full_v4;
17259 		else
17260 			ill->ill_inputfn = ill_input_short_v4;
17261 	}
17262 }
17263 
17264 /*
17265  * Re-evaluate ill_inputfn for all the IPv4 ills.
17266  * Used when RSVP and CGTP comes and goes.
17267  */
17268 void
17269 ill_set_inputfn_all(ip_stack_t *ipst)
17270 {
17271 	ill_walk_context_t	ctx;
17272 	ill_t			*ill;
17273 
17274 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17275 	ill = ILL_START_WALK_V4(&ctx, ipst);
17276 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17277 		ill_set_inputfn(ill);
17278 
17279 	rw_exit(&ipst->ips_ill_g_lock);
17280 }
17281 
17282 /*
17283  * Set the physical address information for `ill' to the contents of the
17284  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17285  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17286  * EINPROGRESS will be returned.
17287  */
17288 int
17289 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17290 {
17291 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17292 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17293 
17294 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17295 
17296 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17297 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17298 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17299 		/* Changing DL_IPV6_TOKEN is not yet supported */
17300 		return (0);
17301 	}
17302 
17303 	/*
17304 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17305 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17306 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17307 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17308 	 */
17309 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17310 		freemsg(mp);
17311 		return (ENOMEM);
17312 	}
17313 
17314 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17315 	mutex_enter(&ill->ill_lock);
17316 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17317 	/* no more nce addition allowed */
17318 	mutex_exit(&ill->ill_lock);
17319 
17320 	/*
17321 	 * If we can quiesce the ill, then set the address.  If not, then
17322 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17323 	 */
17324 	ill_down_ipifs(ill, B_TRUE);
17325 	mutex_enter(&ill->ill_lock);
17326 	if (!ill_is_quiescent(ill)) {
17327 		/* call cannot fail since `conn_t *' argument is NULL */
17328 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17329 		    mp, ILL_DOWN);
17330 		mutex_exit(&ill->ill_lock);
17331 		return (EINPROGRESS);
17332 	}
17333 	mutex_exit(&ill->ill_lock);
17334 
17335 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17336 	return (0);
17337 }
17338 
17339 /*
17340  * Once the ill associated with `q' has quiesced, set its physical address
17341  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17342  * are passed (linked by b_cont), since we sometimes need to save two distinct
17343  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17344  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17345  * is quiesced, we know any stale nce's with the old address information have
17346  * already been removed, so we don't need to call nce_flush().
17347  */
17348 /* ARGSUSED */
17349 static void
17350 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17351 {
17352 	ill_t		*ill = q->q_ptr;
17353 	mblk_t		*addrmp2 = unlinkb(addrmp);
17354 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17355 	uint_t		addrlen, addroff;
17356 	int		status;
17357 
17358 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17359 
17360 	addroff	= dlindp->dl_addr_offset;
17361 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17362 
17363 	switch (dlindp->dl_data) {
17364 	case DL_IPV6_LINK_LAYER_ADDR:
17365 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17366 		freemsg(addrmp2);
17367 		break;
17368 
17369 	case DL_CURR_DEST_ADDR:
17370 		freemsg(ill->ill_dest_addr_mp);
17371 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17372 		ill->ill_dest_addr_mp = addrmp;
17373 		if (ill->ill_isv6) {
17374 			ill_setdesttoken(ill);
17375 			ipif_setdestlinklocal(ill->ill_ipif);
17376 		}
17377 		freemsg(addrmp2);
17378 		break;
17379 
17380 	case DL_CURR_PHYS_ADDR:
17381 		freemsg(ill->ill_phys_addr_mp);
17382 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17383 		ill->ill_phys_addr_mp = addrmp;
17384 		ill->ill_phys_addr_length = addrlen;
17385 		if (ill->ill_isv6)
17386 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17387 		else
17388 			freemsg(addrmp2);
17389 		if (ill->ill_isv6) {
17390 			ill_setdefaulttoken(ill);
17391 			ipif_setlinklocal(ill->ill_ipif);
17392 		}
17393 		break;
17394 	default:
17395 		ASSERT(0);
17396 	}
17397 
17398 	/*
17399 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17400 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17401 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17402 	 * brought up.
17403 	 */
17404 	status = ill_up_ipifs(ill, q, addrmp);
17405 	mutex_enter(&ill->ill_lock);
17406 	if (ill->ill_dl_up)
17407 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17408 	mutex_exit(&ill->ill_lock);
17409 	if (status != EINPROGRESS)
17410 		ipsq_current_finish(ipsq);
17411 }
17412 
17413 /*
17414  * Helper routine for setting the ill_nd_lla fields.
17415  */
17416 void
17417 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17418 {
17419 	freemsg(ill->ill_nd_lla_mp);
17420 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17421 	ill->ill_nd_lla_mp = ndmp;
17422 	ill->ill_nd_lla_len = addrlen;
17423 }
17424 
17425 /*
17426  * Replumb the ill.
17427  */
17428 int
17429 ill_replumb(ill_t *ill, mblk_t *mp)
17430 {
17431 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17432 
17433 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17434 
17435 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17436 
17437 	mutex_enter(&ill->ill_lock);
17438 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17439 	/* no more nce addition allowed */
17440 	mutex_exit(&ill->ill_lock);
17441 
17442 	/*
17443 	 * If we can quiesce the ill, then continue.  If not, then
17444 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17445 	 */
17446 	ill_down_ipifs(ill, B_FALSE);
17447 
17448 	mutex_enter(&ill->ill_lock);
17449 	if (!ill_is_quiescent(ill)) {
17450 		/* call cannot fail since `conn_t *' argument is NULL */
17451 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17452 		    mp, ILL_DOWN);
17453 		mutex_exit(&ill->ill_lock);
17454 		return (EINPROGRESS);
17455 	}
17456 	mutex_exit(&ill->ill_lock);
17457 
17458 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17459 	return (0);
17460 }
17461 
17462 /* ARGSUSED */
17463 static void
17464 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17465 {
17466 	ill_t *ill = q->q_ptr;
17467 	int err;
17468 	conn_t *connp = NULL;
17469 
17470 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17471 	freemsg(ill->ill_replumb_mp);
17472 	ill->ill_replumb_mp = copyb(mp);
17473 
17474 	if (ill->ill_replumb_mp == NULL) {
17475 		/* out of memory */
17476 		ipsq_current_finish(ipsq);
17477 		return;
17478 	}
17479 
17480 	mutex_enter(&ill->ill_lock);
17481 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17482 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17483 	mutex_exit(&ill->ill_lock);
17484 
17485 	if (!ill->ill_up_ipifs) {
17486 		/* already closing */
17487 		ipsq_current_finish(ipsq);
17488 		return;
17489 	}
17490 	ill->ill_replumbing = 1;
17491 	err = ill_down_ipifs_tail(ill);
17492 
17493 	/*
17494 	 * Successfully quiesced and brought down the interface, now we send
17495 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17496 	 * DL_NOTE_REPLUMB message.
17497 	 */
17498 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17499 	    DL_NOTIFY_CONF);
17500 	ASSERT(mp != NULL);
17501 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17502 	    DL_NOTE_REPLUMB_DONE;
17503 	ill_dlpi_send(ill, mp);
17504 
17505 	/*
17506 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17507 	 * streams have to be unbound. When all the DLPI exchanges are done,
17508 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17509 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17510 	 * arp_bringup_done().
17511 	 */
17512 	ASSERT(ill->ill_replumb_mp != NULL);
17513 	if (err == EINPROGRESS)
17514 		return;
17515 	else
17516 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17517 	ASSERT(connp == NULL);
17518 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17519 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17520 		return;
17521 	}
17522 	ipsq_current_finish(ipsq);
17523 }
17524 
17525 /*
17526  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17527  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17528  * as per the ioctl.  On failure, an errno is returned.
17529  */
17530 static int
17531 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17532 {
17533 	int rval;
17534 	struct strioctl iocb;
17535 
17536 	iocb.ic_cmd = cmd;
17537 	iocb.ic_timout = 15;
17538 	iocb.ic_len = bufsize;
17539 	iocb.ic_dp = buf;
17540 
17541 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17542 }
17543 
17544 /*
17545  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17546  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17547  */
17548 static int
17549 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17550     uint_t *bufsizep, cred_t *cr)
17551 {
17552 	int err;
17553 	struct lifnum lifn;
17554 
17555 	bzero(&lifn, sizeof (lifn));
17556 	lifn.lifn_family = af;
17557 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17558 
17559 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17560 		return (err);
17561 
17562 	/*
17563 	 * Pad the interface count to account for additional interfaces that
17564 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17565 	 */
17566 	lifn.lifn_count += 4;
17567 	bzero(lifcp, sizeof (*lifcp));
17568 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17569 	lifcp->lifc_family = af;
17570 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17571 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17572 
17573 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17574 	if (err != 0) {
17575 		kmem_free(lifcp->lifc_buf, *bufsizep);
17576 		return (err);
17577 	}
17578 
17579 	return (0);
17580 }
17581 
17582 /*
17583  * Helper for ip_interface_cleanup() that removes the loopback interface.
17584  */
17585 static void
17586 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17587 {
17588 	int err;
17589 	struct lifreq lifr;
17590 
17591 	bzero(&lifr, sizeof (lifr));
17592 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17593 
17594 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17595 	if (err != 0) {
17596 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17597 		    "error %d\n", isv6 ? "v6" : "v4", err));
17598 	}
17599 }
17600 
17601 /*
17602  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17603  * groups and that IPMP data addresses are down.  These conditions must be met
17604  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17605  */
17606 static void
17607 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17608 {
17609 	int af = isv6 ? AF_INET6 : AF_INET;
17610 	int i, nifs;
17611 	int err;
17612 	uint_t bufsize;
17613 	uint_t lifrsize = sizeof (struct lifreq);
17614 	struct lifconf lifc;
17615 	struct lifreq *lifrp;
17616 
17617 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17618 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17619 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17620 		return;
17621 	}
17622 
17623 	nifs = lifc.lifc_len / lifrsize;
17624 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17625 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17626 		if (err != 0) {
17627 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17628 			    "flags: error %d", lifrp->lifr_name, err);
17629 			continue;
17630 		}
17631 
17632 		if (lifrp->lifr_flags & IFF_IPMP) {
17633 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17634 				continue;
17635 
17636 			lifrp->lifr_flags &= ~IFF_UP;
17637 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17638 			if (err != 0) {
17639 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17640 				    "bring down (error %d); IPMP interface may "
17641 				    "not be shutdown", lifrp->lifr_name, err);
17642 			}
17643 
17644 			/*
17645 			 * Check if IFF_DUPLICATE is still set -- and if so,
17646 			 * reset the address to clear it.
17647 			 */
17648 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17649 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17650 				continue;
17651 
17652 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17653 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17654 			    lifrp, lifrsize, cr)) != 0) {
17655 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17656 				    "reset DAD (error %d); IPMP interface may "
17657 				    "not be shutdown", lifrp->lifr_name, err);
17658 			}
17659 			continue;
17660 		}
17661 
17662 		lifrp->lifr_groupname[0] = '\0';
17663 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17664 		if (err != 0) {
17665 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17666 			    "IPMP group (error %d); associated IPMP interface "
17667 			    "may not be shutdown", lifrp->lifr_name, err);
17668 			continue;
17669 		}
17670 	}
17671 
17672 	kmem_free(lifc.lifc_buf, bufsize);
17673 }
17674 
17675 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17676 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17677 
17678 /*
17679  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17680  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17681  * when the user-level processes in the zone are killed and the latter are
17682  * cleaned up by str_stack_shutdown().
17683  */
17684 void
17685 ip_interface_cleanup(ip_stack_t *ipst)
17686 {
17687 	ldi_handle_t	lh;
17688 	ldi_ident_t	li;
17689 	cred_t		*cr;
17690 	int		err;
17691 	int		i;
17692 	char		*devs[] = { UDP6DEV, UDPDEV };
17693 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17694 
17695 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17696 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17697 		    " error %d", err);
17698 		return;
17699 	}
17700 
17701 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17702 	ASSERT(cr != NULL);
17703 
17704 	/*
17705 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17706 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17707 	 * the loop.)
17708 	 */
17709 	for (i = 0; i < 2; i++) {
17710 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17711 		if (err != 0) {
17712 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17713 			    " error %d", devs[i], err);
17714 			continue;
17715 		}
17716 
17717 		ip_loopback_removeif(lh, i == 0, cr);
17718 		ip_ipmp_cleanup(lh, i == 0, cr);
17719 
17720 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17721 	}
17722 
17723 	ldi_ident_release(li);
17724 	crfree(cr);
17725 }
17726 
17727 /*
17728  * This needs to be in-sync with nic_event_t definition
17729  */
17730 static const char *
17731 ill_hook_event2str(nic_event_t event)
17732 {
17733 	switch (event) {
17734 	case NE_PLUMB:
17735 		return ("PLUMB");
17736 	case NE_UNPLUMB:
17737 		return ("UNPLUMB");
17738 	case NE_UP:
17739 		return ("UP");
17740 	case NE_DOWN:
17741 		return ("DOWN");
17742 	case NE_ADDRESS_CHANGE:
17743 		return ("ADDRESS_CHANGE");
17744 	case NE_LIF_UP:
17745 		return ("LIF_UP");
17746 	case NE_LIF_DOWN:
17747 		return ("LIF_DOWN");
17748 	case NE_IFINDEX_CHANGE:
17749 		return ("IFINDEX_CHANGE");
17750 	default:
17751 		return ("UNKNOWN");
17752 	}
17753 }
17754 
17755 void
17756 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17757     nic_event_data_t data, size_t datalen)
17758 {
17759 	ip_stack_t		*ipst = ill->ill_ipst;
17760 	hook_nic_event_int_t	*info;
17761 	const char		*str = NULL;
17762 
17763 	/* create a new nic event info */
17764 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17765 		goto fail;
17766 
17767 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17768 	info->hnei_event.hne_lif = lif;
17769 	info->hnei_event.hne_event = event;
17770 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17771 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17772 	info->hnei_event.hne_data = NULL;
17773 	info->hnei_event.hne_datalen = 0;
17774 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17775 
17776 	if (data != NULL && datalen != 0) {
17777 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17778 		if (info->hnei_event.hne_data == NULL)
17779 			goto fail;
17780 		bcopy(data, info->hnei_event.hne_data, datalen);
17781 		info->hnei_event.hne_datalen = datalen;
17782 	}
17783 
17784 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17785 	    DDI_NOSLEEP) == DDI_SUCCESS)
17786 		return;
17787 
17788 fail:
17789 	if (info != NULL) {
17790 		if (info->hnei_event.hne_data != NULL) {
17791 			kmem_free(info->hnei_event.hne_data,
17792 			    info->hnei_event.hne_datalen);
17793 		}
17794 		kmem_free(info, sizeof (hook_nic_event_t));
17795 	}
17796 	str = ill_hook_event2str(event);
17797 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17798 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17799 }
17800 
17801 static int
17802 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17803 {
17804 	int		err = 0;
17805 	const in_addr_t	*addr = NULL;
17806 	nce_t		*nce = NULL;
17807 	ill_t		*ill = ipif->ipif_ill;
17808 	ill_t		*bound_ill;
17809 	boolean_t	added_ipif = B_FALSE;
17810 	uint16_t	state;
17811 	uint16_t	flags;
17812 
17813 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17814 	    ill_t *, ill, ipif_t *, ipif);
17815 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17816 		addr = &ipif->ipif_lcl_addr;
17817 	}
17818 
17819 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17820 		if (res_act != Res_act_initial)
17821 			return (EINVAL);
17822 	}
17823 
17824 	if (addr != NULL) {
17825 		ipmp_illgrp_t	*illg = ill->ill_grp;
17826 
17827 		/* add unicast nce for the local addr */
17828 
17829 		if (IS_IPMP(ill)) {
17830 			/*
17831 			 * If we're here via ipif_up(), then the ipif
17832 			 * won't be bound yet -- add it to the group,
17833 			 * which will bind it if possible. (We would
17834 			 * add it in ipif_up(), but deleting on failure
17835 			 * there is gruesome.)  If we're here via
17836 			 * ipmp_ill_bind_ipif(), then the ipif has
17837 			 * already been added to the group and we
17838 			 * just need to use the binding.
17839 			 */
17840 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17841 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17842 				if (bound_ill == NULL) {
17843 					/*
17844 					 * We couldn't bind the ipif to an ill
17845 					 * yet, so we have nothing to publish.
17846 					 * Mark the address as ready and return.
17847 					 */
17848 					ipif->ipif_addr_ready = 1;
17849 					return (0);
17850 				}
17851 				added_ipif = B_TRUE;
17852 			}
17853 		} else {
17854 			bound_ill = ill;
17855 		}
17856 
17857 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17858 		    NCE_F_NONUD);
17859 		/*
17860 		 * If this is an initial bring-up (or the ipif was never
17861 		 * completely brought up), do DAD.  Otherwise, we're here
17862 		 * because IPMP has rebound an address to this ill: send
17863 		 * unsolicited advertisements (ARP announcements) to
17864 		 * inform others.
17865 		 */
17866 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17867 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17868 		} else {
17869 			state = ND_REACHABLE;
17870 			flags |= NCE_F_UNSOL_ADV;
17871 		}
17872 
17873 retry:
17874 		err = nce_lookup_then_add_v4(ill,
17875 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17876 		    addr, flags, state, &nce);
17877 
17878 		/*
17879 		 * note that we may encounter EEXIST if we are moving
17880 		 * the nce as a result of a rebind operation.
17881 		 */
17882 		switch (err) {
17883 		case 0:
17884 			ipif->ipif_added_nce = 1;
17885 			nce->nce_ipif_cnt++;
17886 			break;
17887 		case EEXIST:
17888 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17889 			    ill->ill_name));
17890 			if (!NCE_MYADDR(nce->nce_common)) {
17891 				/*
17892 				 * A leftover nce from before this address
17893 				 * existed
17894 				 */
17895 				ncec_delete(nce->nce_common);
17896 				nce_refrele(nce);
17897 				nce = NULL;
17898 				goto retry;
17899 			}
17900 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17901 				nce_refrele(nce);
17902 				nce = NULL;
17903 				ip1dbg(("ipif_arp_up: NCE already exists "
17904 				    "for %s:%u\n", ill->ill_name,
17905 				    ipif->ipif_id));
17906 				goto arp_up_done;
17907 			}
17908 			/*
17909 			 * Duplicate local addresses are permissible for
17910 			 * IPIF_POINTOPOINT interfaces which will get marked
17911 			 * IPIF_UNNUMBERED later in
17912 			 * ip_addr_availability_check().
17913 			 *
17914 			 * The nce_ipif_cnt field tracks the number of
17915 			 * ipifs that have nce_addr as their local address.
17916 			 */
17917 			ipif->ipif_addr_ready = 1;
17918 			ipif->ipif_added_nce = 1;
17919 			nce->nce_ipif_cnt++;
17920 			err = 0;
17921 			break;
17922 		default:
17923 			ASSERT(nce == NULL);
17924 			goto arp_up_done;
17925 		}
17926 		if (arp_no_defense) {
17927 			if ((ipif->ipif_flags & IPIF_UP) &&
17928 			    !ipif->ipif_addr_ready)
17929 				ipif_up_notify(ipif);
17930 			ipif->ipif_addr_ready = 1;
17931 		}
17932 	} else {
17933 		/* zero address. nothing to publish */
17934 		ipif->ipif_addr_ready = 1;
17935 	}
17936 	if (nce != NULL)
17937 		nce_refrele(nce);
17938 arp_up_done:
17939 	if (added_ipif && err != 0)
17940 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17941 	return (err);
17942 }
17943 
17944 int
17945 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17946 {
17947 	int 		err = 0;
17948 	ill_t 		*ill = ipif->ipif_ill;
17949 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17950 
17951 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17952 	    ill_t *, ill, ipif_t *, ipif);
17953 
17954 	/*
17955 	 * need to bring up ARP or setup mcast mapping only
17956 	 * when the first interface is coming UP.
17957 	 */
17958 	first_interface = (ill->ill_ipif_up_count == 0 &&
17959 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17960 
17961 	if (res_act == Res_act_initial && first_interface) {
17962 		/*
17963 		 * Send ATTACH + BIND
17964 		 */
17965 		err = arp_ll_up(ill);
17966 		if (err != EINPROGRESS && err != 0)
17967 			return (err);
17968 
17969 		/*
17970 		 * Add NCE for local address. Start DAD.
17971 		 * we'll wait to hear that DAD has finished
17972 		 * before using the interface.
17973 		 */
17974 		if (err == EINPROGRESS)
17975 			wait_for_dlpi = B_TRUE;
17976 	}
17977 
17978 	if (!wait_for_dlpi)
17979 		(void) ipif_arp_up_done_tail(ipif, res_act);
17980 
17981 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17982 }
17983 
17984 /*
17985  * Finish processing of "arp_up" after all the DLPI message
17986  * exchanges have completed between arp and the driver.
17987  */
17988 void
17989 arp_bringup_done(ill_t *ill, int err)
17990 {
17991 	mblk_t	*mp1;
17992 	ipif_t  *ipif;
17993 	conn_t *connp = NULL;
17994 	ipsq_t	*ipsq;
17995 	queue_t *q;
17996 
17997 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
17998 
17999 	ASSERT(IAM_WRITER_ILL(ill));
18000 
18001 	ipsq = ill->ill_phyint->phyint_ipsq;
18002 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18003 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18004 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18005 	if (mp1 == NULL) /* bringup was aborted by the user */
18006 		return;
18007 
18008 	/*
18009 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18010 	 * must have an associated conn_t.  Otherwise, we're bringing this
18011 	 * interface back up as part of handling an asynchronous event (e.g.,
18012 	 * physical address change).
18013 	 */
18014 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18015 		ASSERT(connp != NULL);
18016 		q = CONNP_TO_WQ(connp);
18017 	} else {
18018 		ASSERT(connp == NULL);
18019 		q = ill->ill_rq;
18020 	}
18021 	if (err == 0) {
18022 		if (ipif->ipif_isv6) {
18023 			if ((err = ipif_up_done_v6(ipif)) != 0)
18024 				ip0dbg(("arp_bringup_done: init failed\n"));
18025 		} else {
18026 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18027 			if (err != 0 ||
18028 			    (err = ipif_up_done(ipif)) != 0) {
18029 				ip0dbg(("arp_bringup_done: "
18030 				    "init failed err %x\n", err));
18031 				(void) ipif_arp_down(ipif);
18032 			}
18033 
18034 		}
18035 	} else {
18036 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18037 	}
18038 
18039 	if ((err == 0) && (ill->ill_up_ipifs)) {
18040 		err = ill_up_ipifs(ill, q, mp1);
18041 		if (err == EINPROGRESS)
18042 			return;
18043 	}
18044 
18045 	/*
18046 	 * If we have a moved ipif to bring up, and everything has succeeded
18047 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18048 	 * down -- the admin can try to bring it up by hand if need be.
18049 	 */
18050 	if (ill->ill_move_ipif != NULL) {
18051 		ipif = ill->ill_move_ipif;
18052 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18053 		    ipif->ipif_ill->ill_name));
18054 		ill->ill_move_ipif = NULL;
18055 		if (err == 0) {
18056 			err = ipif_up(ipif, q, mp1);
18057 			if (err == EINPROGRESS)
18058 				return;
18059 		}
18060 	}
18061 
18062 	/*
18063 	 * The operation must complete without EINPROGRESS since
18064 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18065 	 * Otherwise, the operation will be stuck forever in the ipsq.
18066 	 */
18067 	ASSERT(err != EINPROGRESS);
18068 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18069 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18070 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18071 		    ill_t *, ill, ipif_t *, ipif);
18072 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18073 	} else {
18074 		ipsq_current_finish(ipsq);
18075 	}
18076 }
18077 
18078 /*
18079  * Finish processing of arp replumb after all the DLPI message
18080  * exchanges have completed between arp and the driver.
18081  */
18082 void
18083 arp_replumb_done(ill_t *ill, int err)
18084 {
18085 	mblk_t	*mp1;
18086 	ipif_t  *ipif;
18087 	conn_t *connp = NULL;
18088 	ipsq_t	*ipsq;
18089 	queue_t *q;
18090 
18091 	ASSERT(IAM_WRITER_ILL(ill));
18092 
18093 	ipsq = ill->ill_phyint->phyint_ipsq;
18094 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18095 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18096 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18097 	if (mp1 == NULL) {
18098 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18099 		    ipsq->ipsq_xop->ipx_current_ioctl));
18100 		/* bringup was aborted by the user */
18101 		return;
18102 	}
18103 	/*
18104 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18105 	 * must have an associated conn_t.  Otherwise, we're bringing this
18106 	 * interface back up as part of handling an asynchronous event (e.g.,
18107 	 * physical address change).
18108 	 */
18109 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18110 		ASSERT(connp != NULL);
18111 		q = CONNP_TO_WQ(connp);
18112 	} else {
18113 		ASSERT(connp == NULL);
18114 		q = ill->ill_rq;
18115 	}
18116 	if ((err == 0) && (ill->ill_up_ipifs)) {
18117 		err = ill_up_ipifs(ill, q, mp1);
18118 		if (err == EINPROGRESS)
18119 			return;
18120 	}
18121 	/*
18122 	 * The operation must complete without EINPROGRESS since
18123 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18124 	 * Otherwise, the operation will be stuck forever in the ipsq.
18125 	 */
18126 	ASSERT(err != EINPROGRESS);
18127 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18128 		DTRACE_PROBE4(ipif__ioctl, char *,
18129 		    "arp_replumb_done finish",
18130 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18131 		    ill_t *, ill, ipif_t *, ipif);
18132 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18133 	} else {
18134 		ipsq_current_finish(ipsq);
18135 	}
18136 }
18137 
18138 void
18139 ipif_up_notify(ipif_t *ipif)
18140 {
18141 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18142 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18143 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18144 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18145 	    NE_LIF_UP, NULL, 0);
18146 }
18147 
18148 /*
18149  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18150  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18151  * TPI end points with STREAMS modules pushed above.  This is assured by not
18152  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18153  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18154  * while unwinding from the ispq and that could be a thread from the bottom.
18155  */
18156 /* ARGSUSED */
18157 int
18158 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18159     ip_ioctl_cmd_t *ipip, void *arg)
18160 {
18161 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18162 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18163 	int ret = 0;
18164 	int i;
18165 	size_t size;
18166 	ip_stack_t *ipst;
18167 	zoneid_t zoneid;
18168 	ilb_stack_t *ilbs;
18169 
18170 	ipst = CONNQ_TO_IPST(q);
18171 	ilbs = ipst->ips_netstack->netstack_ilb;
18172 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18173 
18174 	switch (command) {
18175 	case ILB_CREATE_RULE: {
18176 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18177 
18178 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18179 			ret = EINVAL;
18180 			break;
18181 		}
18182 
18183 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18184 		break;
18185 	}
18186 	case ILB_DESTROY_RULE:
18187 	case ILB_ENABLE_RULE:
18188 	case ILB_DISABLE_RULE: {
18189 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18190 
18191 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18192 			ret = EINVAL;
18193 			break;
18194 		}
18195 
18196 		if (cmd->flags & ILB_RULE_ALLRULES) {
18197 			if (command == ILB_DESTROY_RULE) {
18198 				ilb_rule_del_all(ilbs, zoneid);
18199 				break;
18200 			} else if (command == ILB_ENABLE_RULE) {
18201 				ilb_rule_enable_all(ilbs, zoneid);
18202 				break;
18203 			} else if (command == ILB_DISABLE_RULE) {
18204 				ilb_rule_disable_all(ilbs, zoneid);
18205 				break;
18206 			}
18207 		} else {
18208 			if (command == ILB_DESTROY_RULE) {
18209 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18210 			} else if (command == ILB_ENABLE_RULE) {
18211 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18212 				    NULL);
18213 			} else if (command == ILB_DISABLE_RULE) {
18214 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18215 				    NULL);
18216 			}
18217 		}
18218 		break;
18219 	}
18220 	case ILB_NUM_RULES: {
18221 		ilb_num_rules_cmd_t *cmd;
18222 
18223 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18224 			ret = EINVAL;
18225 			break;
18226 		}
18227 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18228 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18229 		break;
18230 	}
18231 	case ILB_RULE_NAMES: {
18232 		ilb_rule_names_cmd_t *cmd;
18233 
18234 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18235 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18236 		    cmd->num_names == 0) {
18237 			ret = EINVAL;
18238 			break;
18239 		}
18240 		size = cmd->num_names * ILB_RULE_NAMESZ;
18241 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18242 		    size != cmd_mp->b_wptr) {
18243 			ret = EINVAL;
18244 			break;
18245 		}
18246 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18247 		break;
18248 	}
18249 	case ILB_NUM_SERVERS: {
18250 		ilb_num_servers_cmd_t *cmd;
18251 
18252 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18253 			ret = EINVAL;
18254 			break;
18255 		}
18256 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18257 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18258 		    &(cmd->num));
18259 		break;
18260 	}
18261 	case ILB_LIST_RULE: {
18262 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18263 
18264 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18265 			ret = EINVAL;
18266 			break;
18267 		}
18268 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18269 		break;
18270 	}
18271 	case ILB_LIST_SERVERS: {
18272 		ilb_servers_info_cmd_t *cmd;
18273 
18274 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18275 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18276 		    cmd->num_servers == 0) {
18277 			ret = EINVAL;
18278 			break;
18279 		}
18280 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18281 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18282 		    size != cmd_mp->b_wptr) {
18283 			ret = EINVAL;
18284 			break;
18285 		}
18286 
18287 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18288 		    &cmd->num_servers);
18289 		break;
18290 	}
18291 	case ILB_ADD_SERVERS: {
18292 		ilb_servers_info_cmd_t *cmd;
18293 		ilb_rule_t *rule;
18294 
18295 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18296 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18297 			ret = EINVAL;
18298 			break;
18299 		}
18300 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18301 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18302 		    size != cmd_mp->b_wptr) {
18303 			ret = EINVAL;
18304 			break;
18305 		}
18306 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18307 		if (rule == NULL) {
18308 			ASSERT(ret != 0);
18309 			break;
18310 		}
18311 		for (i = 0; i < cmd->num_servers; i++) {
18312 			ilb_server_info_t *s;
18313 
18314 			s = &cmd->servers[i];
18315 			s->err = ilb_server_add(ilbs, rule, s);
18316 		}
18317 		ILB_RULE_REFRELE(rule);
18318 		break;
18319 	}
18320 	case ILB_DEL_SERVERS:
18321 	case ILB_ENABLE_SERVERS:
18322 	case ILB_DISABLE_SERVERS: {
18323 		ilb_servers_cmd_t *cmd;
18324 		ilb_rule_t *rule;
18325 		int (*f)();
18326 
18327 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18328 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18329 			ret = EINVAL;
18330 			break;
18331 		}
18332 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18333 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18334 		    size != cmd_mp->b_wptr) {
18335 			ret = EINVAL;
18336 			break;
18337 		}
18338 
18339 		if (command == ILB_DEL_SERVERS)
18340 			f = ilb_server_del;
18341 		else if (command == ILB_ENABLE_SERVERS)
18342 			f = ilb_server_enable;
18343 		else if (command == ILB_DISABLE_SERVERS)
18344 			f = ilb_server_disable;
18345 
18346 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18347 		if (rule == NULL) {
18348 			ASSERT(ret != 0);
18349 			break;
18350 		}
18351 
18352 		for (i = 0; i < cmd->num_servers; i++) {
18353 			ilb_server_arg_t *s;
18354 
18355 			s = &cmd->servers[i];
18356 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18357 		}
18358 		ILB_RULE_REFRELE(rule);
18359 		break;
18360 	}
18361 	case ILB_LIST_NAT_TABLE: {
18362 		ilb_list_nat_cmd_t *cmd;
18363 
18364 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18365 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18366 			ret = EINVAL;
18367 			break;
18368 		}
18369 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18370 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18371 		    size != cmd_mp->b_wptr) {
18372 			ret = EINVAL;
18373 			break;
18374 		}
18375 
18376 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18377 		    &cmd->flags);
18378 		break;
18379 	}
18380 	case ILB_LIST_STICKY_TABLE: {
18381 		ilb_list_sticky_cmd_t *cmd;
18382 
18383 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18384 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18385 			ret = EINVAL;
18386 			break;
18387 		}
18388 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18389 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18390 		    size != cmd_mp->b_wptr) {
18391 			ret = EINVAL;
18392 			break;
18393 		}
18394 
18395 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18396 		    &cmd->num_sticky, &cmd->flags);
18397 		break;
18398 	}
18399 	default:
18400 		ret = EINVAL;
18401 		break;
18402 	}
18403 done:
18404 	return (ret);
18405 }
18406 
18407 /* Remove all cache entries for this logical interface */
18408 void
18409 ipif_nce_down(ipif_t *ipif)
18410 {
18411 	ill_t *ill = ipif->ipif_ill;
18412 	nce_t *nce;
18413 
18414 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18415 	    ill_t *, ill, ipif_t *, ipif);
18416 	if (ipif->ipif_added_nce) {
18417 		if (ipif->ipif_isv6)
18418 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18419 		else
18420 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18421 		if (nce != NULL) {
18422 			if (--nce->nce_ipif_cnt == 0)
18423 				ncec_delete(nce->nce_common);
18424 			ipif->ipif_added_nce = 0;
18425 			nce_refrele(nce);
18426 		} else {
18427 			/*
18428 			 * nce may already be NULL because it was already
18429 			 * flushed, e.g., due to a call to nce_flush
18430 			 */
18431 			ipif->ipif_added_nce = 0;
18432 		}
18433 	}
18434 	/*
18435 	 * Make IPMP aware of the deleted data address.
18436 	 */
18437 	if (IS_IPMP(ill))
18438 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18439 
18440 	/*
18441 	 * Remove all other nces dependent on this ill when the last ipif
18442 	 * is going away.
18443 	 */
18444 	if (ill->ill_ipif_up_count == 0) {
18445 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18446 		    (uchar_t *)ill, ill->ill_ipst);
18447 		if (IS_UNDER_IPMP(ill))
18448 			nce_flush(ill, B_TRUE);
18449 	}
18450 }
18451