xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision fb2a9bae0030340ad72b9c26ba1ffee2ee3cafec)
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/tunables.h>
75 #include <inet/arp.h>
76 #include <inet/ip_arp.h>
77 #include <inet/mib2.h>
78 #include <inet/ip.h>
79 #include <inet/ip6.h>
80 #include <inet/ip6_asp.h>
81 #include <inet/tcp.h>
82 #include <inet/ip_multi.h>
83 #include <inet/ip_ire.h>
84 #include <inet/ip_ftable.h>
85 #include <inet/ip_rts.h>
86 #include <inet/ip_ndp.h>
87 #include <inet/ip_if.h>
88 #include <inet/ip_impl.h>
89 #include <inet/sctp_ip.h>
90 #include <inet/ip_netinfo.h>
91 #include <inet/ilb_ip.h>
92 
93 #include <netinet/igmp.h>
94 #include <inet/ip_listutils.h>
95 #include <inet/ipclassifier.h>
96 #include <sys/mac_client.h>
97 #include <sys/dld.h>
98 
99 #include <sys/systeminfo.h>
100 #include <sys/bootconf.h>
101 
102 #include <sys/tsol/tndb.h>
103 #include <sys/tsol/tnet.h>
104 
105 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
106 #include <inet/udp_impl.h> /* needed for udp_stack_t */
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
122 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
123 		    char *value, caddr_t cp, cred_t *ioc_cr);
124 
125 static boolean_t ill_is_quiescent(ill_t *);
126 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
127 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
128 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
129     mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131     mblk_t *mp, boolean_t need_up);
132 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
133     queue_t *q, mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
135     mblk_t *mp);
136 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp);
138 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
139     queue_t *q, mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
141     int ioccmd, struct linkblk *li);
142 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
143 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
144 static void	ipsq_flush(ill_t *ill);
145 
146 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static void	ipsq_delete(ipsq_t *);
149 
150 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
151     boolean_t initialize, boolean_t insert, int *errorp);
152 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
153 static void	ipif_delete_bcast_ires(ipif_t *ipif);
154 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
155 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
156 		    boolean_t isv6);
157 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
158 static void	ipif_free(ipif_t *ipif);
159 static void	ipif_free_tail(ipif_t *ipif);
160 static void	ipif_set_default(ipif_t *ipif);
161 static int	ipif_set_values(queue_t *q, mblk_t *mp,
162     char *interf_name, uint_t *ppa);
163 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
164     queue_t *q);
165 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
166     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
167     ip_stack_t *);
168 
169 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
170 static void	ill_delete_interface_type(ill_if_t *);
171 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
172 static void	ill_dl_down(ill_t *ill);
173 static void	ill_down(ill_t *ill);
174 static void	ill_down_ipifs(ill_t *, boolean_t);
175 static void	ill_free_mib(ill_t *ill);
176 static void	ill_glist_delete(ill_t *);
177 static void	ill_phyint_reinit(ill_t *ill);
178 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
179 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
180 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
181 
182 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
183 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
184 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
185 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
186 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
187 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
188 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
189 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
190 static ip_v4mapinfo_func_t ip_mbcast_mapping;
191 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
192 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
193 static void	phyint_free(phyint_t *);
194 
195 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
196 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
197 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
198 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
199 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
200 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
201     dl_capability_sub_t *);
202 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
203 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
204 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
205 		    dl_capability_sub_t *);
206 static void	ill_capability_dld_enable(ill_t *);
207 static void	ill_capability_ack_thr(void *);
208 static void	ill_capability_lso_enable(ill_t *);
209 
210 static ill_t	*ill_prev_usesrc(ill_t *);
211 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
212 static void	ill_disband_usesrc_group(ill_t *);
213 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
214 
215 #ifdef DEBUG
216 static	void	ill_trace_cleanup(const ill_t *);
217 static	void	ipif_trace_cleanup(const ipif_t *);
218 #endif
219 
220 static	void	ill_dlpi_clear_deferred(ill_t *ill);
221 
222 /*
223  * if we go over the memory footprint limit more than once in this msec
224  * interval, we'll start pruning aggressively.
225  */
226 int ip_min_frag_prune_time = 0;
227 
228 static ipft_t	ip_ioctl_ftbl[] = {
229 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
230 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
231 		IPFT_F_NO_REPLY },
232 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
233 	{ 0 }
234 };
235 
236 /* Simple ICMP IP Header Template */
237 static ipha_t icmp_ipha = {
238 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
239 };
240 
241 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
242 
243 static ip_m_t   ip_m_tbl[] = {
244 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
245 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
246 	    ip_nodef_v6intfid },
247 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
248 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
249 	    ip_nodef_v6intfid },
250 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
251 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
252 	    ip_nodef_v6intfid },
253 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
254 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
255 	    ip_nodef_v6intfid },
256 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
257 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
258 	    ip_nodef_v6intfid },
259 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
260 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
261 	    ip_nodef_v6intfid },
262 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
263 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
264 	    ip_ipv4_v6destintfid },
265 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
266 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
267 	    ip_ipv6_v6destintfid },
268 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
269 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
270 	    ip_nodef_v6intfid },
271 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
272 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
273 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
274 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
275 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
276 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
277 	    ip_nodef_v6intfid }
278 };
279 
280 static ill_t	ill_null;		/* Empty ILL for init. */
281 char	ipif_loopback_name[] = "lo0";
282 
283 /* These are used by all IP network modules. */
284 sin6_t	sin6_null;	/* Zero address for quick clears */
285 sin_t	sin_null;	/* Zero address for quick clears */
286 
287 /* When set search for unused ipif_seqid */
288 static ipif_t	ipif_zero;
289 
290 /*
291  * ppa arena is created after these many
292  * interfaces have been plumbed.
293  */
294 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
295 
296 /*
297  * Allocate per-interface mibs.
298  * Returns true if ok. False otherwise.
299  *  ipsq  may not yet be allocated (loopback case ).
300  */
301 static boolean_t
302 ill_allocate_mibs(ill_t *ill)
303 {
304 	/* Already allocated? */
305 	if (ill->ill_ip_mib != NULL) {
306 		if (ill->ill_isv6)
307 			ASSERT(ill->ill_icmp6_mib != NULL);
308 		return (B_TRUE);
309 	}
310 
311 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
312 	    KM_NOSLEEP);
313 	if (ill->ill_ip_mib == NULL) {
314 		return (B_FALSE);
315 	}
316 
317 	/* Setup static information */
318 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
319 	    sizeof (mib2_ipIfStatsEntry_t));
320 	if (ill->ill_isv6) {
321 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
323 		    sizeof (mib2_ipv6AddrEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
325 		    sizeof (mib2_ipv6RouteEntry_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
327 		    sizeof (mib2_ipv6NetToMediaEntry_t));
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
329 		    sizeof (ipv6_member_t));
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
331 		    sizeof (ipv6_grpsrc_t));
332 	} else {
333 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
335 		    sizeof (mib2_ipAddrEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
337 		    sizeof (mib2_ipRouteEntry_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
339 		    sizeof (mib2_ipNetToMediaEntry_t));
340 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
341 		    sizeof (ip_member_t));
342 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
343 		    sizeof (ip_grpsrc_t));
344 
345 		/*
346 		 * For a v4 ill, we are done at this point, because per ill
347 		 * icmp mibs are only used for v6.
348 		 */
349 		return (B_TRUE);
350 	}
351 
352 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
353 	    KM_NOSLEEP);
354 	if (ill->ill_icmp6_mib == NULL) {
355 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
356 		ill->ill_ip_mib = NULL;
357 		return (B_FALSE);
358 	}
359 	/* static icmp info */
360 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
361 	    sizeof (mib2_ipv6IfIcmpEntry_t);
362 	/*
363 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
364 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
365 	 * -> ill_phyint_reinit
366 	 */
367 	return (B_TRUE);
368 }
369 
370 /*
371  * Completely vaporize a lower level tap and all associated interfaces.
372  * ill_delete is called only out of ip_close when the device control
373  * stream is being closed.
374  */
375 void
376 ill_delete(ill_t *ill)
377 {
378 	ipif_t	*ipif;
379 	ill_t	*prev_ill;
380 	ip_stack_t	*ipst = ill->ill_ipst;
381 
382 	/*
383 	 * ill_delete may be forcibly entering the ipsq. The previous
384 	 * ioctl may not have completed and may need to be aborted.
385 	 * ipsq_flush takes care of it. If we don't need to enter the
386 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
387 	 * ill_delete_tail is sufficient.
388 	 */
389 	ipsq_flush(ill);
390 
391 	/*
392 	 * Nuke all interfaces.  ipif_free will take down the interface,
393 	 * remove it from the list, and free the data structure.
394 	 * Walk down the ipif list and remove the logical interfaces
395 	 * first before removing the main ipif. We can't unplumb
396 	 * zeroth interface first in the case of IPv6 as update_conn_ill
397 	 * -> ip_ll_multireq de-references ill_ipif for checking
398 	 * POINTOPOINT.
399 	 *
400 	 * If ill_ipif was not properly initialized (i.e low on memory),
401 	 * then no interfaces to clean up. In this case just clean up the
402 	 * ill.
403 	 */
404 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
405 		ipif_free(ipif);
406 
407 	/*
408 	 * clean out all the nce_t entries that depend on this
409 	 * ill for the ill_phys_addr.
410 	 */
411 	nce_flush(ill, B_TRUE);
412 
413 	/* Clean up msgs on pending upcalls for mrouted */
414 	reset_mrt_ill(ill);
415 
416 	update_conn_ill(ill, ipst);
417 
418 	/*
419 	 * Remove multicast references added as a result of calls to
420 	 * ip_join_allmulti().
421 	 */
422 	ip_purge_allmulti(ill);
423 
424 	/*
425 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
426 	 */
427 	if (IS_UNDER_IPMP(ill))
428 		ipmp_ill_leave_illgrp(ill);
429 
430 	/*
431 	 * ill_down will arrange to blow off any IRE's dependent on this
432 	 * ILL, and shut down fragmentation reassembly.
433 	 */
434 	ill_down(ill);
435 
436 	/* Let SCTP know, so that it can remove this from its list. */
437 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
438 
439 	/*
440 	 * Walk all CONNs that can have a reference on an ire or nce for this
441 	 * ill (we actually walk all that now have stale references).
442 	 */
443 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
444 
445 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
446 	if (ill->ill_isv6)
447 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
448 
449 	/*
450 	 * If an address on this ILL is being used as a source address then
451 	 * clear out the pointers in other ILLs that point to this ILL.
452 	 */
453 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
454 	if (ill->ill_usesrc_grp_next != NULL) {
455 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
456 			ill_disband_usesrc_group(ill);
457 		} else {	/* consumer of the usesrc ILL */
458 			prev_ill = ill_prev_usesrc(ill);
459 			prev_ill->ill_usesrc_grp_next =
460 			    ill->ill_usesrc_grp_next;
461 		}
462 	}
463 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
464 }
465 
466 static void
467 ipif_non_duplicate(ipif_t *ipif)
468 {
469 	ill_t *ill = ipif->ipif_ill;
470 	mutex_enter(&ill->ill_lock);
471 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
472 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
473 		ASSERT(ill->ill_ipif_dup_count > 0);
474 		ill->ill_ipif_dup_count--;
475 	}
476 	mutex_exit(&ill->ill_lock);
477 }
478 
479 /*
480  * ill_delete_tail is called from ip_modclose after all references
481  * to the closing ill are gone. The wait is done in ip_modclose
482  */
483 void
484 ill_delete_tail(ill_t *ill)
485 {
486 	mblk_t	**mpp;
487 	ipif_t	*ipif;
488 	ip_stack_t *ipst = ill->ill_ipst;
489 
490 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
491 		ipif_non_duplicate(ipif);
492 		(void) ipif_down_tail(ipif);
493 	}
494 
495 	ASSERT(ill->ill_ipif_dup_count == 0);
496 
497 	/*
498 	 * If polling capability is enabled (which signifies direct
499 	 * upcall into IP and driver has ill saved as a handle),
500 	 * we need to make sure that unbind has completed before we
501 	 * let the ill disappear and driver no longer has any reference
502 	 * to this ill.
503 	 */
504 	mutex_enter(&ill->ill_lock);
505 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
506 		cv_wait(&ill->ill_cv, &ill->ill_lock);
507 	mutex_exit(&ill->ill_lock);
508 	ASSERT(!(ill->ill_capabilities &
509 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
510 
511 	if (ill->ill_net_type != IRE_LOOPBACK)
512 		qprocsoff(ill->ill_rq);
513 
514 	/*
515 	 * We do an ipsq_flush once again now. New messages could have
516 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
517 	 * could also have landed up if an ioctl thread had looked up
518 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
519 	 * enqueued the ioctl when we did the ipsq_flush last time.
520 	 */
521 	ipsq_flush(ill);
522 
523 	/*
524 	 * Free capabilities.
525 	 */
526 	if (ill->ill_hcksum_capab != NULL) {
527 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
528 		ill->ill_hcksum_capab = NULL;
529 	}
530 
531 	if (ill->ill_zerocopy_capab != NULL) {
532 		kmem_free(ill->ill_zerocopy_capab,
533 		    sizeof (ill_zerocopy_capab_t));
534 		ill->ill_zerocopy_capab = NULL;
535 	}
536 
537 	if (ill->ill_lso_capab != NULL) {
538 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
539 		ill->ill_lso_capab = NULL;
540 	}
541 
542 	if (ill->ill_dld_capab != NULL) {
543 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
544 		ill->ill_dld_capab = NULL;
545 	}
546 
547 	while (ill->ill_ipif != NULL)
548 		ipif_free_tail(ill->ill_ipif);
549 
550 	/*
551 	 * We have removed all references to ilm from conn and the ones joined
552 	 * within the kernel.
553 	 *
554 	 * We don't walk conns, mrts and ires because
555 	 *
556 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
557 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
558 	 *    ill references.
559 	 */
560 
561 	/*
562 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
563 	 * is safe to do because the illgrp has already been unlinked from the
564 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
565 	 */
566 	if (IS_IPMP(ill)) {
567 		ipmp_illgrp_destroy(ill->ill_grp);
568 		ill->ill_grp = NULL;
569 	}
570 
571 	/*
572 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
573 	 * could free the phyint. No more reference to the phyint after this
574 	 * point.
575 	 */
576 	(void) ill_glist_delete(ill);
577 
578 	if (ill->ill_frag_ptr != NULL) {
579 		uint_t count;
580 
581 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
582 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
583 		}
584 		mi_free(ill->ill_frag_ptr);
585 		ill->ill_frag_ptr = NULL;
586 		ill->ill_frag_hash_tbl = NULL;
587 	}
588 
589 	freemsg(ill->ill_nd_lla_mp);
590 	/* Free all retained control messages. */
591 	mpp = &ill->ill_first_mp_to_free;
592 	do {
593 		while (mpp[0]) {
594 			mblk_t  *mp;
595 			mblk_t  *mp1;
596 
597 			mp = mpp[0];
598 			mpp[0] = mp->b_next;
599 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
600 				mp1->b_next = NULL;
601 				mp1->b_prev = NULL;
602 			}
603 			freemsg(mp);
604 		}
605 	} while (mpp++ != &ill->ill_last_mp_to_free);
606 
607 	ill_free_mib(ill);
608 
609 #ifdef DEBUG
610 	ill_trace_cleanup(ill);
611 #endif
612 
613 	/* The default multicast interface might have changed */
614 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
615 
616 	/* Drop refcnt here */
617 	netstack_rele(ill->ill_ipst->ips_netstack);
618 	ill->ill_ipst = NULL;
619 }
620 
621 static void
622 ill_free_mib(ill_t *ill)
623 {
624 	ip_stack_t *ipst = ill->ill_ipst;
625 
626 	/*
627 	 * MIB statistics must not be lost, so when an interface
628 	 * goes away the counter values will be added to the global
629 	 * MIBs.
630 	 */
631 	if (ill->ill_ip_mib != NULL) {
632 		if (ill->ill_isv6) {
633 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
634 			    ill->ill_ip_mib);
635 		} else {
636 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
637 			    ill->ill_ip_mib);
638 		}
639 
640 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
641 		ill->ill_ip_mib = NULL;
642 	}
643 	if (ill->ill_icmp6_mib != NULL) {
644 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
645 		    ill->ill_icmp6_mib);
646 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
647 		ill->ill_icmp6_mib = NULL;
648 	}
649 }
650 
651 /*
652  * Concatenate together a physical address and a sap.
653  *
654  * Sap_lengths are interpreted as follows:
655  *   sap_length == 0	==>	no sap
656  *   sap_length > 0	==>	sap is at the head of the dlpi address
657  *   sap_length < 0	==>	sap is at the tail of the dlpi address
658  */
659 static void
660 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
661     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
662 {
663 	uint16_t sap_addr = (uint16_t)sap_src;
664 
665 	if (sap_length == 0) {
666 		if (phys_src == NULL)
667 			bzero(dst, phys_length);
668 		else
669 			bcopy(phys_src, dst, phys_length);
670 	} else if (sap_length < 0) {
671 		if (phys_src == NULL)
672 			bzero(dst, phys_length);
673 		else
674 			bcopy(phys_src, dst, phys_length);
675 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
676 	} else {
677 		bcopy(&sap_addr, dst, sizeof (sap_addr));
678 		if (phys_src == NULL)
679 			bzero((char *)dst + sap_length, phys_length);
680 		else
681 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
682 	}
683 }
684 
685 /*
686  * Generate a dl_unitdata_req mblk for the device and address given.
687  * addr_length is the length of the physical portion of the address.
688  * If addr is NULL include an all zero address of the specified length.
689  * TRUE? In any case, addr_length is taken to be the entire length of the
690  * dlpi address, including the absolute value of sap_length.
691  */
692 mblk_t *
693 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
694 		t_scalar_t sap_length)
695 {
696 	dl_unitdata_req_t *dlur;
697 	mblk_t	*mp;
698 	t_scalar_t	abs_sap_length;		/* absolute value */
699 
700 	abs_sap_length = ABS(sap_length);
701 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
702 	    DL_UNITDATA_REQ);
703 	if (mp == NULL)
704 		return (NULL);
705 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
706 	/* HACK: accomodate incompatible DLPI drivers */
707 	if (addr_length == 8)
708 		addr_length = 6;
709 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
710 	dlur->dl_dest_addr_offset = sizeof (*dlur);
711 	dlur->dl_priority.dl_min = 0;
712 	dlur->dl_priority.dl_max = 0;
713 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
714 	    (uchar_t *)&dlur[1]);
715 	return (mp);
716 }
717 
718 /*
719  * Add the pending mp to the list. There can be only 1 pending mp
720  * in the list. Any exclusive ioctl that needs to wait for a response
721  * from another module or driver needs to use this function to set
722  * the ipx_pending_mp to the ioctl mblk and wait for the response from
723  * the other module/driver. This is also used while waiting for the
724  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
725  */
726 boolean_t
727 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
728     int waitfor)
729 {
730 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
731 
732 	ASSERT(IAM_WRITER_IPIF(ipif));
733 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
734 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
735 	ASSERT(ipx->ipx_pending_mp == NULL);
736 	/*
737 	 * The caller may be using a different ipif than the one passed into
738 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
739 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
740 	 * that `ipx_current_ipif == ipif'.
741 	 */
742 	ASSERT(ipx->ipx_current_ipif != NULL);
743 
744 	/*
745 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
746 	 * driver.
747 	 */
748 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
749 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
750 	    (DB_TYPE(add_mp) == M_PCPROTO));
751 
752 	if (connp != NULL) {
753 		ASSERT(MUTEX_HELD(&connp->conn_lock));
754 		/*
755 		 * Return error if the conn has started closing. The conn
756 		 * could have finished cleaning up the pending mp list,
757 		 * If so we should not add another mp to the list negating
758 		 * the cleanup.
759 		 */
760 		if (connp->conn_state_flags & CONN_CLOSING)
761 			return (B_FALSE);
762 	}
763 	mutex_enter(&ipx->ipx_lock);
764 	ipx->ipx_pending_ipif = ipif;
765 	/*
766 	 * Note down the queue in b_queue. This will be returned by
767 	 * ipsq_pending_mp_get. Caller will then use these values to restart
768 	 * the processing
769 	 */
770 	add_mp->b_next = NULL;
771 	add_mp->b_queue = q;
772 	ipx->ipx_pending_mp = add_mp;
773 	ipx->ipx_waitfor = waitfor;
774 	mutex_exit(&ipx->ipx_lock);
775 
776 	if (connp != NULL)
777 		connp->conn_oper_pending_ill = ipif->ipif_ill;
778 
779 	return (B_TRUE);
780 }
781 
782 /*
783  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
784  * queued in the list.
785  */
786 mblk_t *
787 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
788 {
789 	mblk_t	*curr = NULL;
790 	ipxop_t	*ipx = ipsq->ipsq_xop;
791 
792 	*connpp = NULL;
793 	mutex_enter(&ipx->ipx_lock);
794 	if (ipx->ipx_pending_mp == NULL) {
795 		mutex_exit(&ipx->ipx_lock);
796 		return (NULL);
797 	}
798 
799 	/* There can be only 1 such excl message */
800 	curr = ipx->ipx_pending_mp;
801 	ASSERT(curr->b_next == NULL);
802 	ipx->ipx_pending_ipif = NULL;
803 	ipx->ipx_pending_mp = NULL;
804 	ipx->ipx_waitfor = 0;
805 	mutex_exit(&ipx->ipx_lock);
806 
807 	if (CONN_Q(curr->b_queue)) {
808 		/*
809 		 * This mp did a refhold on the conn, at the start of the ioctl.
810 		 * So we can safely return a pointer to the conn to the caller.
811 		 */
812 		*connpp = Q_TO_CONN(curr->b_queue);
813 	} else {
814 		*connpp = NULL;
815 	}
816 	curr->b_next = NULL;
817 	curr->b_prev = NULL;
818 	return (curr);
819 }
820 
821 /*
822  * Cleanup the ioctl mp queued in ipx_pending_mp
823  * - Called in the ill_delete path
824  * - Called in the M_ERROR or M_HANGUP path on the ill.
825  * - Called in the conn close path.
826  *
827  * Returns success on finding the pending mblk associated with the ioctl or
828  * exclusive operation in progress, failure otherwise.
829  */
830 boolean_t
831 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
832 {
833 	mblk_t	*mp;
834 	ipxop_t	*ipx;
835 	queue_t	*q;
836 	ipif_t	*ipif;
837 	int	cmd;
838 
839 	ASSERT(IAM_WRITER_ILL(ill));
840 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
841 
842 	mutex_enter(&ipx->ipx_lock);
843 	mp = ipx->ipx_pending_mp;
844 	if (connp != NULL) {
845 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
846 			/*
847 			 * Nothing to clean since the conn that is closing
848 			 * does not have a matching pending mblk in
849 			 * ipx_pending_mp.
850 			 */
851 			mutex_exit(&ipx->ipx_lock);
852 			return (B_FALSE);
853 		}
854 	} else {
855 		/*
856 		 * A non-zero ill_error signifies we are called in the
857 		 * M_ERROR or M_HANGUP path and we need to unconditionally
858 		 * abort any current ioctl and do the corresponding cleanup.
859 		 * A zero ill_error means we are in the ill_delete path and
860 		 * we do the cleanup only if there is a pending mp.
861 		 */
862 		if (mp == NULL && ill->ill_error == 0) {
863 			mutex_exit(&ipx->ipx_lock);
864 			return (B_FALSE);
865 		}
866 	}
867 
868 	/* Now remove from the ipx_pending_mp */
869 	ipx->ipx_pending_mp = NULL;
870 	ipif = ipx->ipx_pending_ipif;
871 	ipx->ipx_pending_ipif = NULL;
872 	ipx->ipx_waitfor = 0;
873 	ipx->ipx_current_ipif = NULL;
874 	cmd = ipx->ipx_current_ioctl;
875 	ipx->ipx_current_ioctl = 0;
876 	ipx->ipx_current_done = B_TRUE;
877 	mutex_exit(&ipx->ipx_lock);
878 
879 	if (mp == NULL)
880 		return (B_FALSE);
881 
882 	q = mp->b_queue;
883 	mp->b_next = NULL;
884 	mp->b_prev = NULL;
885 	mp->b_queue = NULL;
886 
887 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
888 		DTRACE_PROBE4(ipif__ioctl,
889 		    char *, "ipsq_pending_mp_cleanup",
890 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
891 		    ipif_t *, ipif);
892 		if (connp == NULL) {
893 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
894 		} else {
895 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
896 			mutex_enter(&ipif->ipif_ill->ill_lock);
897 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
898 			mutex_exit(&ipif->ipif_ill->ill_lock);
899 		}
900 	} else {
901 		inet_freemsg(mp);
902 	}
903 	return (B_TRUE);
904 }
905 
906 /*
907  * Called in the conn close path and ill delete path
908  */
909 static void
910 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
911 {
912 	ipsq_t	*ipsq;
913 	mblk_t	*prev;
914 	mblk_t	*curr;
915 	mblk_t	*next;
916 	queue_t	*wq, *rq = NULL;
917 	mblk_t	*tmp_list = NULL;
918 
919 	ASSERT(IAM_WRITER_ILL(ill));
920 	if (connp != NULL)
921 		wq = CONNP_TO_WQ(connp);
922 	else
923 		wq = ill->ill_wq;
924 
925 	/*
926 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
927 	 * against this here.
928 	 */
929 	if (wq != NULL)
930 		rq = RD(wq);
931 
932 	ipsq = ill->ill_phyint->phyint_ipsq;
933 	/*
934 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
935 	 * In the case of ioctl from a conn, there can be only 1 mp
936 	 * queued on the ipsq. If an ill is being unplumbed flush all
937 	 * the messages.
938 	 */
939 	mutex_enter(&ipsq->ipsq_lock);
940 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
941 	    curr = next) {
942 		next = curr->b_next;
943 		if (connp == NULL ||
944 		    (curr->b_queue == wq || curr->b_queue == rq)) {
945 			/* Unlink the mblk from the pending mp list */
946 			if (prev != NULL) {
947 				prev->b_next = curr->b_next;
948 			} else {
949 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
950 				ipsq->ipsq_xopq_mphead = curr->b_next;
951 			}
952 			if (ipsq->ipsq_xopq_mptail == curr)
953 				ipsq->ipsq_xopq_mptail = prev;
954 			/*
955 			 * Create a temporary list and release the ipsq lock
956 			 * New elements are added to the head of the tmp_list
957 			 */
958 			curr->b_next = tmp_list;
959 			tmp_list = curr;
960 		} else {
961 			prev = curr;
962 		}
963 	}
964 	mutex_exit(&ipsq->ipsq_lock);
965 
966 	while (tmp_list != NULL) {
967 		curr = tmp_list;
968 		tmp_list = curr->b_next;
969 		curr->b_next = NULL;
970 		curr->b_prev = NULL;
971 		wq = curr->b_queue;
972 		curr->b_queue = NULL;
973 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
974 			DTRACE_PROBE4(ipif__ioctl,
975 			    char *, "ipsq_xopq_mp_cleanup",
976 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
977 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
978 			    CONN_CLOSE : NO_COPYOUT, NULL);
979 		} else {
980 			/*
981 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
982 			 * this can't be just inet_freemsg. we have to
983 			 * restart it otherwise the thread will be stuck.
984 			 */
985 			inet_freemsg(curr);
986 		}
987 	}
988 }
989 
990 /*
991  * This conn has started closing. Cleanup any pending ioctl from this conn.
992  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
993  */
994 void
995 conn_ioctl_cleanup(conn_t *connp)
996 {
997 	ipsq_t	*ipsq;
998 	ill_t	*ill;
999 	boolean_t refheld;
1000 
1001 	/*
1002 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1003 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1004 	 * started the mp could be present in ipx_pending_mp. Note that if
1005 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1006 	 * not yet queued anywhere. In this case, the conn close code will wait
1007 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1008 	 * tcp_close will wait first until all ioctls have completed for this
1009 	 * conn.
1010 	 */
1011 	mutex_enter(&connp->conn_lock);
1012 	ill = connp->conn_oper_pending_ill;
1013 	if (ill == NULL) {
1014 		mutex_exit(&connp->conn_lock);
1015 		return;
1016 	}
1017 
1018 	/*
1019 	 * We may not be able to refhold the ill if the ill/ipif
1020 	 * is changing. But we need to make sure that the ill will
1021 	 * not vanish. So we just bump up the ill_waiter count.
1022 	 */
1023 	refheld = ill_waiter_inc(ill);
1024 	mutex_exit(&connp->conn_lock);
1025 	if (refheld) {
1026 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1027 			ill_waiter_dcr(ill);
1028 			/*
1029 			 * Check whether this ioctl has started and is
1030 			 * pending. If it is not found there then check
1031 			 * whether this ioctl has not even started and is in
1032 			 * the ipsq_xopq list.
1033 			 */
1034 			if (!ipsq_pending_mp_cleanup(ill, connp))
1035 				ipsq_xopq_mp_cleanup(ill, connp);
1036 			ipsq = ill->ill_phyint->phyint_ipsq;
1037 			ipsq_exit(ipsq);
1038 			return;
1039 		}
1040 	}
1041 
1042 	/*
1043 	 * The ill is also closing and we could not bump up the
1044 	 * ill_waiter_count or we could not enter the ipsq. Leave
1045 	 * the cleanup to ill_delete
1046 	 */
1047 	mutex_enter(&connp->conn_lock);
1048 	while (connp->conn_oper_pending_ill != NULL)
1049 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1050 	mutex_exit(&connp->conn_lock);
1051 	if (refheld)
1052 		ill_waiter_dcr(ill);
1053 }
1054 
1055 /*
1056  * ipcl_walk function for cleaning up conn_*_ill fields.
1057  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1058  * conn_bound_if in place. We prefer dropping
1059  * packets instead of sending them out the wrong interface, or accepting
1060  * packets from the wrong ifindex.
1061  */
1062 static void
1063 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1064 {
1065 	ill_t	*ill = (ill_t *)arg;
1066 
1067 	mutex_enter(&connp->conn_lock);
1068 	if (connp->conn_dhcpinit_ill == ill) {
1069 		connp->conn_dhcpinit_ill = NULL;
1070 		ASSERT(ill->ill_dhcpinit != 0);
1071 		atomic_dec_32(&ill->ill_dhcpinit);
1072 		ill_set_inputfn(ill);
1073 	}
1074 	mutex_exit(&connp->conn_lock);
1075 }
1076 
1077 static int
1078 ill_down_ipifs_tail(ill_t *ill)
1079 {
1080 	ipif_t	*ipif;
1081 	int err;
1082 
1083 	ASSERT(IAM_WRITER_ILL(ill));
1084 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1085 		ipif_non_duplicate(ipif);
1086 		/*
1087 		 * ipif_down_tail will call arp_ll_down on the last ipif
1088 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1089 		 */
1090 		if ((err = ipif_down_tail(ipif)) != 0)
1091 			return (err);
1092 	}
1093 	return (0);
1094 }
1095 
1096 /* ARGSUSED */
1097 void
1098 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1099 {
1100 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1101 	(void) ill_down_ipifs_tail(q->q_ptr);
1102 	freemsg(mp);
1103 	ipsq_current_finish(ipsq);
1104 }
1105 
1106 /*
1107  * ill_down_start is called when we want to down this ill and bring it up again
1108  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1109  * all interfaces, but don't tear down any plumbing.
1110  */
1111 boolean_t
1112 ill_down_start(queue_t *q, mblk_t *mp)
1113 {
1114 	ill_t	*ill = q->q_ptr;
1115 	ipif_t	*ipif;
1116 
1117 	ASSERT(IAM_WRITER_ILL(ill));
1118 	mutex_enter(&ill->ill_lock);
1119 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1120 	/* no more nce addition allowed */
1121 	mutex_exit(&ill->ill_lock);
1122 
1123 	/*
1124 	 * It is possible that some ioctl is already in progress while we
1125 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1126 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1127 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1128 	 * the in progress ioctl from ever completing.
1129 	 *
1130 	 * The thread that started the ioctl (if any) must have returned,
1131 	 * since we are now executing as writer. After the 2 calls below,
1132 	 * the state of the ipsq and the ill would reflect no trace of any
1133 	 * pending operation. Subsequently if there is any response to the
1134 	 * original ioctl from the driver, it would be discarded as an
1135 	 * unsolicited message from the driver.
1136 	 */
1137 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1138 	ill_dlpi_clear_deferred(ill);
1139 
1140 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1141 		(void) ipif_down(ipif, NULL, NULL);
1142 
1143 	ill_down(ill);
1144 
1145 	/*
1146 	 * Walk all CONNs that can have a reference on an ire or nce for this
1147 	 * ill (we actually walk all that now have stale references).
1148 	 */
1149 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1150 
1151 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1152 	if (ill->ill_isv6)
1153 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1154 
1155 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1156 
1157 	/*
1158 	 * Atomically test and add the pending mp if references are active.
1159 	 */
1160 	mutex_enter(&ill->ill_lock);
1161 	if (!ill_is_quiescent(ill)) {
1162 		/* call cannot fail since `conn_t *' argument is NULL */
1163 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1164 		    mp, ILL_DOWN);
1165 		mutex_exit(&ill->ill_lock);
1166 		return (B_FALSE);
1167 	}
1168 	mutex_exit(&ill->ill_lock);
1169 	return (B_TRUE);
1170 }
1171 
1172 static void
1173 ill_down(ill_t *ill)
1174 {
1175 	mblk_t	*mp;
1176 	ip_stack_t	*ipst = ill->ill_ipst;
1177 
1178 	/*
1179 	 * Blow off any IREs dependent on this ILL.
1180 	 * The caller needs to handle conn_ixa_cleanup
1181 	 */
1182 	ill_delete_ires(ill);
1183 
1184 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1185 
1186 	/* Remove any conn_*_ill depending on this ill */
1187 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1188 
1189 	/*
1190 	 * Free state for additional IREs.
1191 	 */
1192 	mutex_enter(&ill->ill_saved_ire_lock);
1193 	mp = ill->ill_saved_ire_mp;
1194 	ill->ill_saved_ire_mp = NULL;
1195 	ill->ill_saved_ire_cnt = 0;
1196 	mutex_exit(&ill->ill_saved_ire_lock);
1197 	freemsg(mp);
1198 }
1199 
1200 /*
1201  * ire_walk routine used to delete every IRE that depends on
1202  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1203  *
1204  * Note: since the routes added by the kernel are deleted separately,
1205  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1206  *
1207  * We also remove references on ire_nce_cache entries that refer to the ill.
1208  */
1209 void
1210 ill_downi(ire_t *ire, char *ill_arg)
1211 {
1212 	ill_t	*ill = (ill_t *)ill_arg;
1213 	nce_t	*nce;
1214 
1215 	mutex_enter(&ire->ire_lock);
1216 	nce = ire->ire_nce_cache;
1217 	if (nce != NULL && nce->nce_ill == ill)
1218 		ire->ire_nce_cache = NULL;
1219 	else
1220 		nce = NULL;
1221 	mutex_exit(&ire->ire_lock);
1222 	if (nce != NULL)
1223 		nce_refrele(nce);
1224 	if (ire->ire_ill == ill) {
1225 		/*
1226 		 * The existing interface binding for ire must be
1227 		 * deleted before trying to bind the route to another
1228 		 * interface. However, since we are using the contents of the
1229 		 * ire after ire_delete, the caller has to ensure that
1230 		 * CONDEMNED (deleted) ire's are not removed from the list
1231 		 * when ire_delete() returns. Currently ill_downi() is
1232 		 * only called as part of ire_walk*() routines, so that
1233 		 * the irb_refhold() done by ire_walk*() will ensure that
1234 		 * ire_delete() does not lead to ire_inactive().
1235 		 */
1236 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1237 		ire_delete(ire);
1238 		if (ire->ire_unbound)
1239 			ire_rebind(ire);
1240 	}
1241 }
1242 
1243 /* Remove IRE_IF_CLONE on this ill */
1244 void
1245 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1246 {
1247 	ill_t	*ill = (ill_t *)ill_arg;
1248 
1249 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1250 	if (ire->ire_ill == ill)
1251 		ire_delete(ire);
1252 }
1253 
1254 /* Consume an M_IOCACK of the fastpath probe. */
1255 void
1256 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1257 {
1258 	mblk_t	*mp1 = mp;
1259 
1260 	/*
1261 	 * If this was the first attempt turn on the fastpath probing.
1262 	 */
1263 	mutex_enter(&ill->ill_lock);
1264 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1265 		ill->ill_dlpi_fastpath_state = IDS_OK;
1266 	mutex_exit(&ill->ill_lock);
1267 
1268 	/* Free the M_IOCACK mblk, hold on to the data */
1269 	mp = mp->b_cont;
1270 	freeb(mp1);
1271 	if (mp == NULL)
1272 		return;
1273 	if (mp->b_cont != NULL)
1274 		nce_fastpath_update(ill, mp);
1275 	else
1276 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1277 	freemsg(mp);
1278 }
1279 
1280 /*
1281  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1282  * The data portion of the request is a dl_unitdata_req_t template for
1283  * what we would send downstream in the absence of a fastpath confirmation.
1284  */
1285 int
1286 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1287 {
1288 	struct iocblk	*ioc;
1289 	mblk_t	*mp;
1290 
1291 	if (dlur_mp == NULL)
1292 		return (EINVAL);
1293 
1294 	mutex_enter(&ill->ill_lock);
1295 	switch (ill->ill_dlpi_fastpath_state) {
1296 	case IDS_FAILED:
1297 		/*
1298 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1299 		 * support it.
1300 		 */
1301 		mutex_exit(&ill->ill_lock);
1302 		return (ENOTSUP);
1303 	case IDS_UNKNOWN:
1304 		/* This is the first probe */
1305 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1306 		break;
1307 	default:
1308 		break;
1309 	}
1310 	mutex_exit(&ill->ill_lock);
1311 
1312 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1313 		return (EAGAIN);
1314 
1315 	mp->b_cont = copyb(dlur_mp);
1316 	if (mp->b_cont == NULL) {
1317 		freeb(mp);
1318 		return (EAGAIN);
1319 	}
1320 
1321 	ioc = (struct iocblk *)mp->b_rptr;
1322 	ioc->ioc_count = msgdsize(mp->b_cont);
1323 
1324 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1325 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1326 	putnext(ill->ill_wq, mp);
1327 	return (0);
1328 }
1329 
1330 void
1331 ill_capability_probe(ill_t *ill)
1332 {
1333 	mblk_t	*mp;
1334 
1335 	ASSERT(IAM_WRITER_ILL(ill));
1336 
1337 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1338 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1339 		return;
1340 
1341 	/*
1342 	 * We are starting a new cycle of capability negotiation.
1343 	 * Free up the capab reset messages of any previous incarnation.
1344 	 * We will do a fresh allocation when we get the response to our probe
1345 	 */
1346 	if (ill->ill_capab_reset_mp != NULL) {
1347 		freemsg(ill->ill_capab_reset_mp);
1348 		ill->ill_capab_reset_mp = NULL;
1349 	}
1350 
1351 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1352 
1353 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1354 	if (mp == NULL)
1355 		return;
1356 
1357 	ill_capability_send(ill, mp);
1358 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1359 }
1360 
1361 void
1362 ill_capability_reset(ill_t *ill, boolean_t reneg)
1363 {
1364 	ASSERT(IAM_WRITER_ILL(ill));
1365 
1366 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1367 		return;
1368 
1369 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1370 
1371 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1372 	ill->ill_capab_reset_mp = NULL;
1373 	/*
1374 	 * We turn off all capabilities except those pertaining to
1375 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1376 	 * which will be turned off by the corresponding reset functions.
1377 	 */
1378 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1379 }
1380 
1381 static void
1382 ill_capability_reset_alloc(ill_t *ill)
1383 {
1384 	mblk_t *mp;
1385 	size_t	size = 0;
1386 	int	err;
1387 	dl_capability_req_t	*capb;
1388 
1389 	ASSERT(IAM_WRITER_ILL(ill));
1390 	ASSERT(ill->ill_capab_reset_mp == NULL);
1391 
1392 	if (ILL_HCKSUM_CAPABLE(ill)) {
1393 		size += sizeof (dl_capability_sub_t) +
1394 		    sizeof (dl_capab_hcksum_t);
1395 	}
1396 
1397 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1398 		size += sizeof (dl_capability_sub_t) +
1399 		    sizeof (dl_capab_zerocopy_t);
1400 	}
1401 
1402 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1403 		size += sizeof (dl_capability_sub_t) +
1404 		    sizeof (dl_capab_dld_t);
1405 	}
1406 
1407 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1408 	    STR_NOSIG, &err);
1409 
1410 	mp->b_datap->db_type = M_PROTO;
1411 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1412 
1413 	capb = (dl_capability_req_t *)mp->b_rptr;
1414 	capb->dl_primitive = DL_CAPABILITY_REQ;
1415 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1416 	capb->dl_sub_length = size;
1417 
1418 	mp->b_wptr += sizeof (dl_capability_req_t);
1419 
1420 	/*
1421 	 * Each handler fills in the corresponding dl_capability_sub_t
1422 	 * inside the mblk,
1423 	 */
1424 	ill_capability_hcksum_reset_fill(ill, mp);
1425 	ill_capability_zerocopy_reset_fill(ill, mp);
1426 	ill_capability_dld_reset_fill(ill, mp);
1427 
1428 	ill->ill_capab_reset_mp = mp;
1429 }
1430 
1431 static void
1432 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1433 {
1434 	dl_capab_id_t *id_ic;
1435 	uint_t sub_dl_cap = outers->dl_cap;
1436 	dl_capability_sub_t *inners;
1437 	uint8_t *capend;
1438 
1439 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1440 
1441 	/*
1442 	 * Note: range checks here are not absolutely sufficient to
1443 	 * make us robust against malformed messages sent by drivers;
1444 	 * this is in keeping with the rest of IP's dlpi handling.
1445 	 * (Remember, it's coming from something else in the kernel
1446 	 * address space)
1447 	 */
1448 
1449 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1450 	if (capend > mp->b_wptr) {
1451 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1452 		    "malformed sub-capability too long for mblk");
1453 		return;
1454 	}
1455 
1456 	id_ic = (dl_capab_id_t *)(outers + 1);
1457 
1458 	if (outers->dl_length < sizeof (*id_ic) ||
1459 	    (inners = &id_ic->id_subcap,
1460 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1461 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1462 		    "encapsulated capab type %d too long for mblk",
1463 		    inners->dl_cap);
1464 		return;
1465 	}
1466 
1467 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1468 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1469 		    "isn't as expected; pass-thru module(s) detected, "
1470 		    "discarding capability\n", inners->dl_cap));
1471 		return;
1472 	}
1473 
1474 	/* Process the encapsulated sub-capability */
1475 	ill_capability_dispatch(ill, mp, inners);
1476 }
1477 
1478 static void
1479 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1480 {
1481 	dl_capability_sub_t *dl_subcap;
1482 
1483 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1484 		return;
1485 
1486 	/*
1487 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1488 	 * initialized below since it is not used by DLD.
1489 	 */
1490 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1491 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1492 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1493 
1494 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1495 }
1496 
1497 static void
1498 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1499 {
1500 	/*
1501 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1502 	 * is only to get the VRRP capability.
1503 	 *
1504 	 * Note that we cannot check ill_ipif_up_count here since
1505 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1506 	 * That is done asynchronously, and can race with this function.
1507 	 */
1508 	if (!ill->ill_dl_up) {
1509 		if (subp->dl_cap == DL_CAPAB_VRRP)
1510 			ill_capability_vrrp_ack(ill, mp, subp);
1511 		return;
1512 	}
1513 
1514 	switch (subp->dl_cap) {
1515 	case DL_CAPAB_HCKSUM:
1516 		ill_capability_hcksum_ack(ill, mp, subp);
1517 		break;
1518 	case DL_CAPAB_ZEROCOPY:
1519 		ill_capability_zerocopy_ack(ill, mp, subp);
1520 		break;
1521 	case DL_CAPAB_DLD:
1522 		ill_capability_dld_ack(ill, mp, subp);
1523 		break;
1524 	case DL_CAPAB_VRRP:
1525 		break;
1526 	default:
1527 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1528 		    subp->dl_cap));
1529 	}
1530 }
1531 
1532 /*
1533  * Process the vrrp capability received from a DLS Provider. isub must point
1534  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1535  */
1536 static void
1537 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1538 {
1539 	dl_capab_vrrp_t	*vrrp;
1540 	uint_t		sub_dl_cap = isub->dl_cap;
1541 	uint8_t		*capend;
1542 
1543 	ASSERT(IAM_WRITER_ILL(ill));
1544 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1545 
1546 	/*
1547 	 * Note: range checks here are not absolutely sufficient to
1548 	 * make us robust against malformed messages sent by drivers;
1549 	 * this is in keeping with the rest of IP's dlpi handling.
1550 	 * (Remember, it's coming from something else in the kernel
1551 	 * address space)
1552 	 */
1553 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1554 	if (capend > mp->b_wptr) {
1555 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1556 		    "malformed sub-capability too long for mblk");
1557 		return;
1558 	}
1559 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1560 
1561 	/*
1562 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1563 	 */
1564 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1565 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1566 		ill->ill_flags |= ILLF_VRRP;
1567 	}
1568 }
1569 
1570 /*
1571  * Process a hardware checksum offload capability negotiation ack received
1572  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1573  * of a DL_CAPABILITY_ACK message.
1574  */
1575 static void
1576 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1577 {
1578 	dl_capability_req_t	*ocap;
1579 	dl_capab_hcksum_t	*ihck, *ohck;
1580 	ill_hcksum_capab_t	**ill_hcksum;
1581 	mblk_t			*nmp = NULL;
1582 	uint_t			sub_dl_cap = isub->dl_cap;
1583 	uint8_t			*capend;
1584 
1585 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1586 
1587 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1588 
1589 	/*
1590 	 * Note: range checks here are not absolutely sufficient to
1591 	 * make us robust against malformed messages sent by drivers;
1592 	 * this is in keeping with the rest of IP's dlpi handling.
1593 	 * (Remember, it's coming from something else in the kernel
1594 	 * address space)
1595 	 */
1596 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1597 	if (capend > mp->b_wptr) {
1598 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1599 		    "malformed sub-capability too long for mblk");
1600 		return;
1601 	}
1602 
1603 	/*
1604 	 * There are two types of acks we process here:
1605 	 * 1. acks in reply to a (first form) generic capability req
1606 	 *    (no ENABLE flag set)
1607 	 * 2. acks in reply to a ENABLE capability req.
1608 	 *    (ENABLE flag set)
1609 	 */
1610 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1611 
1612 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1613 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1614 		    "unsupported hardware checksum "
1615 		    "sub-capability (version %d, expected %d)",
1616 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1617 		return;
1618 	}
1619 
1620 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1621 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1622 		    "checksum capability isn't as expected; pass-thru "
1623 		    "module(s) detected, discarding capability\n"));
1624 		return;
1625 	}
1626 
1627 #define	CURR_HCKSUM_CAPAB				\
1628 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1629 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1630 
1631 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1632 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1633 		/* do ENABLE processing */
1634 		if (*ill_hcksum == NULL) {
1635 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1636 			    KM_NOSLEEP);
1637 
1638 			if (*ill_hcksum == NULL) {
1639 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1640 				    "could not enable hcksum version %d "
1641 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1642 				    ill->ill_name);
1643 				return;
1644 			}
1645 		}
1646 
1647 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1648 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1649 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1650 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1651 		    "has enabled hardware checksumming\n ",
1652 		    ill->ill_name));
1653 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1654 		/*
1655 		 * Enabling hardware checksum offload
1656 		 * Currently IP supports {TCP,UDP}/IPv4
1657 		 * partial and full cksum offload and
1658 		 * IPv4 header checksum offload.
1659 		 * Allocate new mblk which will
1660 		 * contain a new capability request
1661 		 * to enable hardware checksum offload.
1662 		 */
1663 		uint_t	size;
1664 		uchar_t	*rptr;
1665 
1666 		size = sizeof (dl_capability_req_t) +
1667 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1668 
1669 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1670 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1671 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1672 			    ill->ill_name);
1673 			return;
1674 		}
1675 
1676 		rptr = nmp->b_rptr;
1677 		/* initialize dl_capability_req_t */
1678 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1679 		ocap->dl_sub_offset =
1680 		    sizeof (dl_capability_req_t);
1681 		ocap->dl_sub_length =
1682 		    sizeof (dl_capability_sub_t) +
1683 		    isub->dl_length;
1684 		nmp->b_rptr += sizeof (dl_capability_req_t);
1685 
1686 		/* initialize dl_capability_sub_t */
1687 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1688 		nmp->b_rptr += sizeof (*isub);
1689 
1690 		/* initialize dl_capab_hcksum_t */
1691 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1692 		bcopy(ihck, ohck, sizeof (*ihck));
1693 
1694 		nmp->b_rptr = rptr;
1695 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1696 
1697 		/* Set ENABLE flag */
1698 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1699 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1700 
1701 		/*
1702 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1703 		 * hardware checksum acceleration.
1704 		 */
1705 		ill_capability_send(ill, nmp);
1706 	} else {
1707 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1708 		    "advertised %x hardware checksum capability flags\n",
1709 		    ill->ill_name, ihck->hcksum_txflags));
1710 	}
1711 }
1712 
1713 static void
1714 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1715 {
1716 	dl_capab_hcksum_t *hck_subcap;
1717 	dl_capability_sub_t *dl_subcap;
1718 
1719 	if (!ILL_HCKSUM_CAPABLE(ill))
1720 		return;
1721 
1722 	ASSERT(ill->ill_hcksum_capab != NULL);
1723 
1724 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1725 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1726 	dl_subcap->dl_length = sizeof (*hck_subcap);
1727 
1728 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1729 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1730 	hck_subcap->hcksum_txflags = 0;
1731 
1732 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1733 }
1734 
1735 static void
1736 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1737 {
1738 	mblk_t *nmp = NULL;
1739 	dl_capability_req_t *oc;
1740 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1741 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1742 	uint_t sub_dl_cap = isub->dl_cap;
1743 	uint8_t *capend;
1744 
1745 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1746 
1747 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1748 
1749 	/*
1750 	 * Note: range checks here are not absolutely sufficient to
1751 	 * make us robust against malformed messages sent by drivers;
1752 	 * this is in keeping with the rest of IP's dlpi handling.
1753 	 * (Remember, it's coming from something else in the kernel
1754 	 * address space)
1755 	 */
1756 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1757 	if (capend > mp->b_wptr) {
1758 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1759 		    "malformed sub-capability too long for mblk");
1760 		return;
1761 	}
1762 
1763 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1764 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1765 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1766 		    "unsupported ZEROCOPY sub-capability (version %d, "
1767 		    "expected %d)", zc_ic->zerocopy_version,
1768 		    ZEROCOPY_VERSION_1);
1769 		return;
1770 	}
1771 
1772 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1773 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1774 		    "capability isn't as expected; pass-thru module(s) "
1775 		    "detected, discarding capability\n"));
1776 		return;
1777 	}
1778 
1779 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1780 		if (*ill_zerocopy_capab == NULL) {
1781 			*ill_zerocopy_capab =
1782 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1783 			    KM_NOSLEEP);
1784 
1785 			if (*ill_zerocopy_capab == NULL) {
1786 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1787 				    "could not enable Zero-copy version %d "
1788 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1789 				    ill->ill_name);
1790 				return;
1791 			}
1792 		}
1793 
1794 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1795 		    "supports Zero-copy version %d\n", ill->ill_name,
1796 		    ZEROCOPY_VERSION_1));
1797 
1798 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1799 		    zc_ic->zerocopy_version;
1800 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1801 		    zc_ic->zerocopy_flags;
1802 
1803 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1804 	} else {
1805 		uint_t size;
1806 		uchar_t *rptr;
1807 
1808 		size = sizeof (dl_capability_req_t) +
1809 		    sizeof (dl_capability_sub_t) +
1810 		    sizeof (dl_capab_zerocopy_t);
1811 
1812 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1813 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1814 			    "could not enable zerocopy for %s (ENOMEM)\n",
1815 			    ill->ill_name);
1816 			return;
1817 		}
1818 
1819 		rptr = nmp->b_rptr;
1820 		/* initialize dl_capability_req_t */
1821 		oc = (dl_capability_req_t *)rptr;
1822 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1823 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1824 		    sizeof (dl_capab_zerocopy_t);
1825 		rptr += sizeof (dl_capability_req_t);
1826 
1827 		/* initialize dl_capability_sub_t */
1828 		bcopy(isub, rptr, sizeof (*isub));
1829 		rptr += sizeof (*isub);
1830 
1831 		/* initialize dl_capab_zerocopy_t */
1832 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1833 		*zc_oc = *zc_ic;
1834 
1835 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1836 		    "to enable zero-copy version %d\n", ill->ill_name,
1837 		    ZEROCOPY_VERSION_1));
1838 
1839 		/* set VMSAFE_MEM flag */
1840 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1841 
1842 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1843 		ill_capability_send(ill, nmp);
1844 	}
1845 }
1846 
1847 static void
1848 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1849 {
1850 	dl_capab_zerocopy_t *zerocopy_subcap;
1851 	dl_capability_sub_t *dl_subcap;
1852 
1853 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1854 		return;
1855 
1856 	ASSERT(ill->ill_zerocopy_capab != NULL);
1857 
1858 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1859 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1860 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1861 
1862 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1863 	zerocopy_subcap->zerocopy_version =
1864 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1865 	zerocopy_subcap->zerocopy_flags = 0;
1866 
1867 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1868 }
1869 
1870 /*
1871  * DLD capability
1872  * Refer to dld.h for more information regarding the purpose and usage
1873  * of this capability.
1874  */
1875 static void
1876 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1877 {
1878 	dl_capab_dld_t		*dld_ic, dld;
1879 	uint_t			sub_dl_cap = isub->dl_cap;
1880 	uint8_t			*capend;
1881 	ill_dld_capab_t		*idc;
1882 
1883 	ASSERT(IAM_WRITER_ILL(ill));
1884 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1885 
1886 	/*
1887 	 * Note: range checks here are not absolutely sufficient to
1888 	 * make us robust against malformed messages sent by drivers;
1889 	 * this is in keeping with the rest of IP's dlpi handling.
1890 	 * (Remember, it's coming from something else in the kernel
1891 	 * address space)
1892 	 */
1893 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1894 	if (capend > mp->b_wptr) {
1895 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1896 		    "malformed sub-capability too long for mblk");
1897 		return;
1898 	}
1899 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1900 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1901 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1902 		    "unsupported DLD sub-capability (version %d, "
1903 		    "expected %d)", dld_ic->dld_version,
1904 		    DLD_CURRENT_VERSION);
1905 		return;
1906 	}
1907 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1908 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1909 		    "capability isn't as expected; pass-thru module(s) "
1910 		    "detected, discarding capability\n"));
1911 		return;
1912 	}
1913 
1914 	/*
1915 	 * Copy locally to ensure alignment.
1916 	 */
1917 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1918 
1919 	if ((idc = ill->ill_dld_capab) == NULL) {
1920 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1921 		if (idc == NULL) {
1922 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1923 			    "could not enable DLD version %d "
1924 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1925 			    ill->ill_name);
1926 			return;
1927 		}
1928 		ill->ill_dld_capab = idc;
1929 	}
1930 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1931 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1932 	ip1dbg(("ill_capability_dld_ack: interface %s "
1933 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1934 
1935 	ill_capability_dld_enable(ill);
1936 }
1937 
1938 /*
1939  * Typically capability negotiation between IP and the driver happens via
1940  * DLPI message exchange. However GLD also offers a direct function call
1941  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1942  * But arbitrary function calls into IP or GLD are not permitted, since both
1943  * of them are protected by their own perimeter mechanism. The perimeter can
1944  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1945  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1946  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1947  * to enter the mac perimeter and then do the direct function calls into
1948  * GLD to enable squeue polling. The ring related callbacks from the mac into
1949  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1950  * protected by the mac perimeter.
1951  */
1952 static void
1953 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1954 {
1955 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1956 	int			err;
1957 
1958 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1959 	    DLD_ENABLE);
1960 	ASSERT(err == 0);
1961 }
1962 
1963 static void
1964 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1965 {
1966 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1967 	int			err;
1968 
1969 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1970 	    DLD_DISABLE);
1971 	ASSERT(err == 0);
1972 }
1973 
1974 boolean_t
1975 ill_mac_perim_held(ill_t *ill)
1976 {
1977 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1978 
1979 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1980 	    DLD_QUERY));
1981 }
1982 
1983 static void
1984 ill_capability_direct_enable(ill_t *ill)
1985 {
1986 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1987 	ill_dld_direct_t	*idd = &idc->idc_direct;
1988 	dld_capab_direct_t	direct;
1989 	int			rc;
1990 
1991 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1992 
1993 	bzero(&direct, sizeof (direct));
1994 	direct.di_rx_cf = (uintptr_t)ip_input;
1995 	direct.di_rx_ch = ill;
1996 
1997 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1998 	    DLD_ENABLE);
1999 	if (rc == 0) {
2000 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2001 		idd->idd_tx_dh = direct.di_tx_dh;
2002 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2003 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2004 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2005 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2006 		ASSERT(idd->idd_tx_cb_df != NULL);
2007 		ASSERT(idd->idd_tx_fctl_df != NULL);
2008 		ASSERT(idd->idd_tx_df != NULL);
2009 		/*
2010 		 * One time registration of flow enable callback function
2011 		 */
2012 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2013 		    ill_flow_enable, ill);
2014 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2015 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2016 	} else {
2017 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2018 		    "capability, rc = %d\n", rc);
2019 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2020 	}
2021 }
2022 
2023 static void
2024 ill_capability_poll_enable(ill_t *ill)
2025 {
2026 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2027 	dld_capab_poll_t	poll;
2028 	int			rc;
2029 
2030 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2031 
2032 	bzero(&poll, sizeof (poll));
2033 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2034 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2035 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2036 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2037 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2038 	poll.poll_ring_ch = ill;
2039 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2040 	    DLD_ENABLE);
2041 	if (rc == 0) {
2042 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2043 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2044 	} else {
2045 		ip1dbg(("warning: could not enable POLL "
2046 		    "capability, rc = %d\n", rc));
2047 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2048 	}
2049 }
2050 
2051 /*
2052  * Enable the LSO capability.
2053  */
2054 static void
2055 ill_capability_lso_enable(ill_t *ill)
2056 {
2057 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2058 	dld_capab_lso_t	lso;
2059 	int rc;
2060 
2061 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2062 
2063 	if (ill->ill_lso_capab == NULL) {
2064 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2065 		    KM_NOSLEEP);
2066 		if (ill->ill_lso_capab == NULL) {
2067 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2068 			    "could not enable LSO for %s (ENOMEM)\n",
2069 			    ill->ill_name);
2070 			return;
2071 		}
2072 	}
2073 
2074 	bzero(&lso, sizeof (lso));
2075 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2076 	    DLD_ENABLE)) == 0) {
2077 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2078 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2079 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2080 		ip1dbg(("ill_capability_lso_enable: interface %s "
2081 		    "has enabled LSO\n ", ill->ill_name));
2082 	} else {
2083 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2084 		ill->ill_lso_capab = NULL;
2085 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2086 	}
2087 }
2088 
2089 static void
2090 ill_capability_dld_enable(ill_t *ill)
2091 {
2092 	mac_perim_handle_t mph;
2093 
2094 	ASSERT(IAM_WRITER_ILL(ill));
2095 
2096 	if (ill->ill_isv6)
2097 		return;
2098 
2099 	ill_mac_perim_enter(ill, &mph);
2100 	if (!ill->ill_isv6) {
2101 		ill_capability_direct_enable(ill);
2102 		ill_capability_poll_enable(ill);
2103 		ill_capability_lso_enable(ill);
2104 	}
2105 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2106 	ill_mac_perim_exit(ill, mph);
2107 }
2108 
2109 static void
2110 ill_capability_dld_disable(ill_t *ill)
2111 {
2112 	ill_dld_capab_t	*idc;
2113 	ill_dld_direct_t *idd;
2114 	mac_perim_handle_t	mph;
2115 
2116 	ASSERT(IAM_WRITER_ILL(ill));
2117 
2118 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2119 		return;
2120 
2121 	ill_mac_perim_enter(ill, &mph);
2122 
2123 	idc = ill->ill_dld_capab;
2124 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2125 		/*
2126 		 * For performance we avoid locks in the transmit data path
2127 		 * and don't maintain a count of the number of threads using
2128 		 * direct calls. Thus some threads could be using direct
2129 		 * transmit calls to GLD, even after the capability mechanism
2130 		 * turns it off. This is still safe since the handles used in
2131 		 * the direct calls continue to be valid until the unplumb is
2132 		 * completed. Remove the callback that was added (1-time) at
2133 		 * capab enable time.
2134 		 */
2135 		mutex_enter(&ill->ill_lock);
2136 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2137 		mutex_exit(&ill->ill_lock);
2138 		if (ill->ill_flownotify_mh != NULL) {
2139 			idd = &idc->idc_direct;
2140 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2141 			    ill->ill_flownotify_mh);
2142 			ill->ill_flownotify_mh = NULL;
2143 		}
2144 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2145 		    NULL, DLD_DISABLE);
2146 	}
2147 
2148 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2149 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2150 		ip_squeue_clean_all(ill);
2151 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2152 		    NULL, DLD_DISABLE);
2153 	}
2154 
2155 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2156 		ASSERT(ill->ill_lso_capab != NULL);
2157 		/*
2158 		 * Clear the capability flag for LSO but retain the
2159 		 * ill_lso_capab structure since it's possible that another
2160 		 * thread is still referring to it.  The structure only gets
2161 		 * deallocated when we destroy the ill.
2162 		 */
2163 
2164 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2165 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2166 		    NULL, DLD_DISABLE);
2167 	}
2168 
2169 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2170 	ill_mac_perim_exit(ill, mph);
2171 }
2172 
2173 /*
2174  * Capability Negotiation protocol
2175  *
2176  * We don't wait for DLPI capability operations to finish during interface
2177  * bringup or teardown. Doing so would introduce more asynchrony and the
2178  * interface up/down operations will need multiple return and restarts.
2179  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2180  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2181  * exclusive operation won't start until the DLPI operations of the previous
2182  * exclusive operation complete.
2183  *
2184  * The capability state machine is shown below.
2185  *
2186  * state		next state		event, action
2187  *
2188  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2189  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2190  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2191  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2192  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2193  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2194  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2195  *						    ill_capability_probe.
2196  */
2197 
2198 /*
2199  * Dedicated thread started from ip_stack_init that handles capability
2200  * disable. This thread ensures the taskq dispatch does not fail by waiting
2201  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2202  * that direct calls to DLD are done in a cv_waitable context.
2203  */
2204 void
2205 ill_taskq_dispatch(ip_stack_t *ipst)
2206 {
2207 	callb_cpr_t cprinfo;
2208 	char 	name[64];
2209 	mblk_t	*mp;
2210 
2211 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2212 	    ipst->ips_netstack->netstack_stackid);
2213 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2214 	    name);
2215 	mutex_enter(&ipst->ips_capab_taskq_lock);
2216 
2217 	for (;;) {
2218 		mp = ipst->ips_capab_taskq_head;
2219 		while (mp != NULL) {
2220 			ipst->ips_capab_taskq_head = mp->b_next;
2221 			if (ipst->ips_capab_taskq_head == NULL)
2222 				ipst->ips_capab_taskq_tail = NULL;
2223 			mutex_exit(&ipst->ips_capab_taskq_lock);
2224 			mp->b_next = NULL;
2225 
2226 			VERIFY(taskq_dispatch(system_taskq,
2227 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2228 			mutex_enter(&ipst->ips_capab_taskq_lock);
2229 			mp = ipst->ips_capab_taskq_head;
2230 		}
2231 
2232 		if (ipst->ips_capab_taskq_quit)
2233 			break;
2234 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2235 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2236 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2237 	}
2238 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2239 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2240 	CALLB_CPR_EXIT(&cprinfo);
2241 	thread_exit();
2242 }
2243 
2244 /*
2245  * Consume a new-style hardware capabilities negotiation ack.
2246  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2247  */
2248 static void
2249 ill_capability_ack_thr(void *arg)
2250 {
2251 	mblk_t	*mp = arg;
2252 	dl_capability_ack_t *capp;
2253 	dl_capability_sub_t *subp, *endp;
2254 	ill_t	*ill;
2255 	boolean_t reneg;
2256 
2257 	ill = (ill_t *)mp->b_prev;
2258 	mp->b_prev = NULL;
2259 
2260 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2261 
2262 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2263 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2264 		/*
2265 		 * We have received the ack for our DL_CAPAB reset request.
2266 		 * There isnt' anything in the message that needs processing.
2267 		 * All message based capabilities have been disabled, now
2268 		 * do the function call based capability disable.
2269 		 */
2270 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2271 		ill_capability_dld_disable(ill);
2272 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2273 		if (reneg)
2274 			ill_capability_probe(ill);
2275 		goto done;
2276 	}
2277 
2278 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2279 		ill->ill_dlpi_capab_state = IDCS_OK;
2280 
2281 	capp = (dl_capability_ack_t *)mp->b_rptr;
2282 
2283 	if (capp->dl_sub_length == 0) {
2284 		/* no new-style capabilities */
2285 		goto done;
2286 	}
2287 
2288 	/* make sure the driver supplied correct dl_sub_length */
2289 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2290 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2291 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2292 		goto done;
2293 	}
2294 
2295 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2296 	/*
2297 	 * There are sub-capabilities. Process the ones we know about.
2298 	 * Loop until we don't have room for another sub-cap header..
2299 	 */
2300 	for (subp = SC(capp, capp->dl_sub_offset),
2301 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2302 	    subp <= endp;
2303 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2304 
2305 		switch (subp->dl_cap) {
2306 		case DL_CAPAB_ID_WRAPPER:
2307 			ill_capability_id_ack(ill, mp, subp);
2308 			break;
2309 		default:
2310 			ill_capability_dispatch(ill, mp, subp);
2311 			break;
2312 		}
2313 	}
2314 #undef SC
2315 done:
2316 	inet_freemsg(mp);
2317 	ill_capability_done(ill);
2318 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2319 }
2320 
2321 /*
2322  * This needs to be started in a taskq thread to provide a cv_waitable
2323  * context.
2324  */
2325 void
2326 ill_capability_ack(ill_t *ill, mblk_t *mp)
2327 {
2328 	ip_stack_t	*ipst = ill->ill_ipst;
2329 
2330 	mp->b_prev = (mblk_t *)ill;
2331 	ASSERT(mp->b_next == NULL);
2332 
2333 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2334 	    TQ_NOSLEEP) != 0)
2335 		return;
2336 
2337 	/*
2338 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2339 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2340 	 */
2341 	mutex_enter(&ipst->ips_capab_taskq_lock);
2342 	if (ipst->ips_capab_taskq_head == NULL) {
2343 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2344 		ipst->ips_capab_taskq_head = mp;
2345 	} else {
2346 		ipst->ips_capab_taskq_tail->b_next = mp;
2347 	}
2348 	ipst->ips_capab_taskq_tail = mp;
2349 
2350 	cv_signal(&ipst->ips_capab_taskq_cv);
2351 	mutex_exit(&ipst->ips_capab_taskq_lock);
2352 }
2353 
2354 /*
2355  * This routine is called to scan the fragmentation reassembly table for
2356  * the specified ILL for any packets that are starting to smell.
2357  * dead_interval is the maximum time in seconds that will be tolerated.  It
2358  * will either be the value specified in ip_g_frag_timeout, or zero if the
2359  * ILL is shutting down and it is time to blow everything off.
2360  *
2361  * It returns the number of seconds (as a time_t) that the next frag timer
2362  * should be scheduled for, 0 meaning that the timer doesn't need to be
2363  * re-started.  Note that the method of calculating next_timeout isn't
2364  * entirely accurate since time will flow between the time we grab
2365  * current_time and the time we schedule the next timeout.  This isn't a
2366  * big problem since this is the timer for sending an ICMP reassembly time
2367  * exceeded messages, and it doesn't have to be exactly accurate.
2368  *
2369  * This function is
2370  * sometimes called as writer, although this is not required.
2371  */
2372 time_t
2373 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2374 {
2375 	ipfb_t	*ipfb;
2376 	ipfb_t	*endp;
2377 	ipf_t	*ipf;
2378 	ipf_t	*ipfnext;
2379 	mblk_t	*mp;
2380 	time_t	current_time = gethrestime_sec();
2381 	time_t	next_timeout = 0;
2382 	uint32_t	hdr_length;
2383 	mblk_t	*send_icmp_head;
2384 	mblk_t	*send_icmp_head_v6;
2385 	ip_stack_t *ipst = ill->ill_ipst;
2386 	ip_recv_attr_t iras;
2387 
2388 	bzero(&iras, sizeof (iras));
2389 	iras.ira_flags = 0;
2390 	iras.ira_ill = iras.ira_rill = ill;
2391 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2392 	iras.ira_rifindex = iras.ira_ruifindex;
2393 
2394 	ipfb = ill->ill_frag_hash_tbl;
2395 	if (ipfb == NULL)
2396 		return (B_FALSE);
2397 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2398 	/* Walk the frag hash table. */
2399 	for (; ipfb < endp; ipfb++) {
2400 		send_icmp_head = NULL;
2401 		send_icmp_head_v6 = NULL;
2402 		mutex_enter(&ipfb->ipfb_lock);
2403 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2404 			time_t frag_time = current_time - ipf->ipf_timestamp;
2405 			time_t frag_timeout;
2406 
2407 			if (frag_time < dead_interval) {
2408 				/*
2409 				 * There are some outstanding fragments
2410 				 * that will timeout later.  Make note of
2411 				 * the time so that we can reschedule the
2412 				 * next timeout appropriately.
2413 				 */
2414 				frag_timeout = dead_interval - frag_time;
2415 				if (next_timeout == 0 ||
2416 				    frag_timeout < next_timeout) {
2417 					next_timeout = frag_timeout;
2418 				}
2419 				break;
2420 			}
2421 			/* Time's up.  Get it out of here. */
2422 			hdr_length = ipf->ipf_nf_hdr_len;
2423 			ipfnext = ipf->ipf_hash_next;
2424 			if (ipfnext)
2425 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2426 			*ipf->ipf_ptphn = ipfnext;
2427 			mp = ipf->ipf_mp->b_cont;
2428 			for (; mp; mp = mp->b_cont) {
2429 				/* Extra points for neatness. */
2430 				IP_REASS_SET_START(mp, 0);
2431 				IP_REASS_SET_END(mp, 0);
2432 			}
2433 			mp = ipf->ipf_mp->b_cont;
2434 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2435 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2436 			ipfb->ipfb_count -= ipf->ipf_count;
2437 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2438 			ipfb->ipfb_frag_pkts--;
2439 			/*
2440 			 * We do not send any icmp message from here because
2441 			 * we currently are holding the ipfb_lock for this
2442 			 * hash chain. If we try and send any icmp messages
2443 			 * from here we may end up via a put back into ip
2444 			 * trying to get the same lock, causing a recursive
2445 			 * mutex panic. Instead we build a list and send all
2446 			 * the icmp messages after we have dropped the lock.
2447 			 */
2448 			if (ill->ill_isv6) {
2449 				if (hdr_length != 0) {
2450 					mp->b_next = send_icmp_head_v6;
2451 					send_icmp_head_v6 = mp;
2452 				} else {
2453 					freemsg(mp);
2454 				}
2455 			} else {
2456 				if (hdr_length != 0) {
2457 					mp->b_next = send_icmp_head;
2458 					send_icmp_head = mp;
2459 				} else {
2460 					freemsg(mp);
2461 				}
2462 			}
2463 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2464 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2465 			freeb(ipf->ipf_mp);
2466 		}
2467 		mutex_exit(&ipfb->ipfb_lock);
2468 		/*
2469 		 * Now need to send any icmp messages that we delayed from
2470 		 * above.
2471 		 */
2472 		while (send_icmp_head_v6 != NULL) {
2473 			ip6_t *ip6h;
2474 
2475 			mp = send_icmp_head_v6;
2476 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2477 			mp->b_next = NULL;
2478 			ip6h = (ip6_t *)mp->b_rptr;
2479 			iras.ira_flags = 0;
2480 			/*
2481 			 * This will result in an incorrect ALL_ZONES zoneid
2482 			 * for multicast packets, but we
2483 			 * don't send ICMP errors for those in any case.
2484 			 */
2485 			iras.ira_zoneid =
2486 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2487 			    ill, ipst);
2488 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2489 			icmp_time_exceeded_v6(mp,
2490 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2491 			    &iras);
2492 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2493 		}
2494 		while (send_icmp_head != NULL) {
2495 			ipaddr_t dst;
2496 
2497 			mp = send_icmp_head;
2498 			send_icmp_head = send_icmp_head->b_next;
2499 			mp->b_next = NULL;
2500 
2501 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2502 
2503 			iras.ira_flags = IRAF_IS_IPV4;
2504 			/*
2505 			 * This will result in an incorrect ALL_ZONES zoneid
2506 			 * for broadcast and multicast packets, but we
2507 			 * don't send ICMP errors for those in any case.
2508 			 */
2509 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2510 			    ill, ipst);
2511 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2512 			icmp_time_exceeded(mp,
2513 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2514 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2515 		}
2516 	}
2517 	/*
2518 	 * A non-dying ILL will use the return value to decide whether to
2519 	 * restart the frag timer, and for how long.
2520 	 */
2521 	return (next_timeout);
2522 }
2523 
2524 /*
2525  * This routine is called when the approximate count of mblk memory used
2526  * for the specified ILL has exceeded max_count.
2527  */
2528 void
2529 ill_frag_prune(ill_t *ill, uint_t max_count)
2530 {
2531 	ipfb_t	*ipfb;
2532 	ipf_t	*ipf;
2533 	size_t	count;
2534 	clock_t now;
2535 
2536 	/*
2537 	 * If we are here within ip_min_frag_prune_time msecs remove
2538 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2539 	 * ill_frag_free_num_pkts.
2540 	 */
2541 	mutex_enter(&ill->ill_lock);
2542 	now = ddi_get_lbolt();
2543 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2544 	    (ip_min_frag_prune_time != 0 ?
2545 	    ip_min_frag_prune_time : msec_per_tick)) {
2546 
2547 		ill->ill_frag_free_num_pkts++;
2548 
2549 	} else {
2550 		ill->ill_frag_free_num_pkts = 0;
2551 	}
2552 	ill->ill_last_frag_clean_time = now;
2553 	mutex_exit(&ill->ill_lock);
2554 
2555 	/*
2556 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2557 	 */
2558 	if (ill->ill_frag_free_num_pkts != 0) {
2559 		int ix;
2560 
2561 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2562 			ipfb = &ill->ill_frag_hash_tbl[ix];
2563 			mutex_enter(&ipfb->ipfb_lock);
2564 			if (ipfb->ipfb_ipf != NULL) {
2565 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2566 				    ill->ill_frag_free_num_pkts);
2567 			}
2568 			mutex_exit(&ipfb->ipfb_lock);
2569 		}
2570 	}
2571 	/*
2572 	 * While the reassembly list for this ILL is too big, prune a fragment
2573 	 * queue by age, oldest first.
2574 	 */
2575 	while (ill->ill_frag_count > max_count) {
2576 		int	ix;
2577 		ipfb_t	*oipfb = NULL;
2578 		uint_t	oldest = UINT_MAX;
2579 
2580 		count = 0;
2581 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2582 			ipfb = &ill->ill_frag_hash_tbl[ix];
2583 			mutex_enter(&ipfb->ipfb_lock);
2584 			ipf = ipfb->ipfb_ipf;
2585 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2586 				oldest = ipf->ipf_gen;
2587 				oipfb = ipfb;
2588 			}
2589 			count += ipfb->ipfb_count;
2590 			mutex_exit(&ipfb->ipfb_lock);
2591 		}
2592 		if (oipfb == NULL)
2593 			break;
2594 
2595 		if (count <= max_count)
2596 			return;	/* Somebody beat us to it, nothing to do */
2597 		mutex_enter(&oipfb->ipfb_lock);
2598 		ipf = oipfb->ipfb_ipf;
2599 		if (ipf != NULL) {
2600 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2601 		}
2602 		mutex_exit(&oipfb->ipfb_lock);
2603 	}
2604 }
2605 
2606 /*
2607  * free 'free_cnt' fragmented packets starting at ipf.
2608  */
2609 void
2610 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2611 {
2612 	size_t	count;
2613 	mblk_t	*mp;
2614 	mblk_t	*tmp;
2615 	ipf_t **ipfp = ipf->ipf_ptphn;
2616 
2617 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2618 	ASSERT(ipfp != NULL);
2619 	ASSERT(ipf != NULL);
2620 
2621 	while (ipf != NULL && free_cnt-- > 0) {
2622 		count = ipf->ipf_count;
2623 		mp = ipf->ipf_mp;
2624 		ipf = ipf->ipf_hash_next;
2625 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2626 			IP_REASS_SET_START(tmp, 0);
2627 			IP_REASS_SET_END(tmp, 0);
2628 		}
2629 		atomic_add_32(&ill->ill_frag_count, -count);
2630 		ASSERT(ipfb->ipfb_count >= count);
2631 		ipfb->ipfb_count -= count;
2632 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2633 		ipfb->ipfb_frag_pkts--;
2634 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2635 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2636 		freemsg(mp);
2637 	}
2638 
2639 	if (ipf)
2640 		ipf->ipf_ptphn = ipfp;
2641 	ipfp[0] = ipf;
2642 }
2643 
2644 /*
2645  * Helper function for ill_forward_set().
2646  */
2647 static void
2648 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2649 {
2650 	ip_stack_t	*ipst = ill->ill_ipst;
2651 
2652 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2653 
2654 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2655 	    (enable ? "Enabling" : "Disabling"),
2656 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2657 	mutex_enter(&ill->ill_lock);
2658 	if (enable)
2659 		ill->ill_flags |= ILLF_ROUTER;
2660 	else
2661 		ill->ill_flags &= ~ILLF_ROUTER;
2662 	mutex_exit(&ill->ill_lock);
2663 	if (ill->ill_isv6)
2664 		ill_set_nce_router_flags(ill, enable);
2665 	/* Notify routing socket listeners of this change. */
2666 	if (ill->ill_ipif != NULL)
2667 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2668 }
2669 
2670 /*
2671  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2672  * socket messages for each interface whose flags we change.
2673  */
2674 int
2675 ill_forward_set(ill_t *ill, boolean_t enable)
2676 {
2677 	ipmp_illgrp_t *illg;
2678 	ip_stack_t *ipst = ill->ill_ipst;
2679 
2680 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2681 
2682 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2683 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2684 		return (0);
2685 
2686 	if (IS_LOOPBACK(ill))
2687 		return (EINVAL);
2688 
2689 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2690 		/*
2691 		 * Update all of the interfaces in the group.
2692 		 */
2693 		illg = ill->ill_grp;
2694 		ill = list_head(&illg->ig_if);
2695 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2696 			ill_forward_set_on_ill(ill, enable);
2697 
2698 		/*
2699 		 * Update the IPMP meta-interface.
2700 		 */
2701 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2702 		return (0);
2703 	}
2704 
2705 	ill_forward_set_on_ill(ill, enable);
2706 	return (0);
2707 }
2708 
2709 /*
2710  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2711  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2712  * set or clear.
2713  */
2714 static void
2715 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2716 {
2717 	ipif_t *ipif;
2718 	ncec_t *ncec;
2719 	nce_t *nce;
2720 
2721 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2722 		/*
2723 		 * NOTE: we match across the illgrp because nce's for
2724 		 * addresses on IPMP interfaces have an nce_ill that points to
2725 		 * the bound underlying ill.
2726 		 */
2727 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2728 		if (nce != NULL) {
2729 			ncec = nce->nce_common;
2730 			mutex_enter(&ncec->ncec_lock);
2731 			if (enable)
2732 				ncec->ncec_flags |= NCE_F_ISROUTER;
2733 			else
2734 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2735 			mutex_exit(&ncec->ncec_lock);
2736 			nce_refrele(nce);
2737 		}
2738 	}
2739 }
2740 
2741 /*
2742  * Intializes the context structure and returns the first ill in the list
2743  * cuurently start_list and end_list can have values:
2744  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2745  * IP_V4_G_HEAD		Traverse IPV4 list only.
2746  * IP_V6_G_HEAD		Traverse IPV6 list only.
2747  */
2748 
2749 /*
2750  * We don't check for CONDEMNED ills here. Caller must do that if
2751  * necessary under the ill lock.
2752  */
2753 ill_t *
2754 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2755     ip_stack_t *ipst)
2756 {
2757 	ill_if_t *ifp;
2758 	ill_t *ill;
2759 	avl_tree_t *avl_tree;
2760 
2761 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2762 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2763 
2764 	/*
2765 	 * setup the lists to search
2766 	 */
2767 	if (end_list != MAX_G_HEADS) {
2768 		ctx->ctx_current_list = start_list;
2769 		ctx->ctx_last_list = end_list;
2770 	} else {
2771 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2772 		ctx->ctx_current_list = 0;
2773 	}
2774 
2775 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2776 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2777 		if (ifp != (ill_if_t *)
2778 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2779 			avl_tree = &ifp->illif_avl_by_ppa;
2780 			ill = avl_first(avl_tree);
2781 			/*
2782 			 * ill is guaranteed to be non NULL or ifp should have
2783 			 * not existed.
2784 			 */
2785 			ASSERT(ill != NULL);
2786 			return (ill);
2787 		}
2788 		ctx->ctx_current_list++;
2789 	}
2790 
2791 	return (NULL);
2792 }
2793 
2794 /*
2795  * returns the next ill in the list. ill_first() must have been called
2796  * before calling ill_next() or bad things will happen.
2797  */
2798 
2799 /*
2800  * We don't check for CONDEMNED ills here. Caller must do that if
2801  * necessary under the ill lock.
2802  */
2803 ill_t *
2804 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2805 {
2806 	ill_if_t *ifp;
2807 	ill_t *ill;
2808 	ip_stack_t	*ipst = lastill->ill_ipst;
2809 
2810 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2811 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2812 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2813 	    AVL_AFTER)) != NULL) {
2814 		return (ill);
2815 	}
2816 
2817 	/* goto next ill_ifp in the list. */
2818 	ifp = lastill->ill_ifptr->illif_next;
2819 
2820 	/* make sure not at end of circular list */
2821 	while (ifp ==
2822 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2823 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2824 			return (NULL);
2825 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2826 	}
2827 
2828 	return (avl_first(&ifp->illif_avl_by_ppa));
2829 }
2830 
2831 /*
2832  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2833  * The final number (PPA) must not have any leading zeros.  Upon success, a
2834  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2835  */
2836 static char *
2837 ill_get_ppa_ptr(char *name)
2838 {
2839 	int namelen = strlen(name);
2840 	int end_ndx = namelen - 1;
2841 	int ppa_ndx, i;
2842 
2843 	/*
2844 	 * Check that the first character is [a-zA-Z], and that the last
2845 	 * character is [0-9].
2846 	 */
2847 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2848 		return (NULL);
2849 
2850 	/*
2851 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2852 	 */
2853 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2854 		if (!isdigit(name[ppa_ndx - 1]))
2855 			break;
2856 
2857 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2858 		return (NULL);
2859 
2860 	/*
2861 	 * Check that the intermediate characters are [a-z0-9.]
2862 	 */
2863 	for (i = 1; i < ppa_ndx; i++) {
2864 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2865 		    name[i] != '.' && name[i] != '_') {
2866 			return (NULL);
2867 		}
2868 	}
2869 
2870 	return (name + ppa_ndx);
2871 }
2872 
2873 /*
2874  * use avl tree to locate the ill.
2875  */
2876 static ill_t *
2877 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2878 {
2879 	char *ppa_ptr = NULL;
2880 	int len;
2881 	uint_t ppa;
2882 	ill_t *ill = NULL;
2883 	ill_if_t *ifp;
2884 	int list;
2885 
2886 	/*
2887 	 * get ppa ptr
2888 	 */
2889 	if (isv6)
2890 		list = IP_V6_G_HEAD;
2891 	else
2892 		list = IP_V4_G_HEAD;
2893 
2894 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2895 		return (NULL);
2896 	}
2897 
2898 	len = ppa_ptr - name + 1;
2899 
2900 	ppa = stoi(&ppa_ptr);
2901 
2902 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2903 
2904 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2905 		/*
2906 		 * match is done on len - 1 as the name is not null
2907 		 * terminated it contains ppa in addition to the interface
2908 		 * name.
2909 		 */
2910 		if ((ifp->illif_name_len == len) &&
2911 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2912 			break;
2913 		} else {
2914 			ifp = ifp->illif_next;
2915 		}
2916 	}
2917 
2918 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2919 		/*
2920 		 * Even the interface type does not exist.
2921 		 */
2922 		return (NULL);
2923 	}
2924 
2925 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2926 	if (ill != NULL) {
2927 		mutex_enter(&ill->ill_lock);
2928 		if (ILL_CAN_LOOKUP(ill)) {
2929 			ill_refhold_locked(ill);
2930 			mutex_exit(&ill->ill_lock);
2931 			return (ill);
2932 		}
2933 		mutex_exit(&ill->ill_lock);
2934 	}
2935 	return (NULL);
2936 }
2937 
2938 /*
2939  * comparison function for use with avl.
2940  */
2941 static int
2942 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2943 {
2944 	uint_t ppa;
2945 	uint_t ill_ppa;
2946 
2947 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2948 
2949 	ppa = *((uint_t *)ppa_ptr);
2950 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2951 	/*
2952 	 * We want the ill with the lowest ppa to be on the
2953 	 * top.
2954 	 */
2955 	if (ill_ppa < ppa)
2956 		return (1);
2957 	if (ill_ppa > ppa)
2958 		return (-1);
2959 	return (0);
2960 }
2961 
2962 /*
2963  * remove an interface type from the global list.
2964  */
2965 static void
2966 ill_delete_interface_type(ill_if_t *interface)
2967 {
2968 	ASSERT(interface != NULL);
2969 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2970 
2971 	avl_destroy(&interface->illif_avl_by_ppa);
2972 	if (interface->illif_ppa_arena != NULL)
2973 		vmem_destroy(interface->illif_ppa_arena);
2974 
2975 	remque(interface);
2976 
2977 	mi_free(interface);
2978 }
2979 
2980 /*
2981  * remove ill from the global list.
2982  */
2983 static void
2984 ill_glist_delete(ill_t *ill)
2985 {
2986 	ip_stack_t	*ipst;
2987 	phyint_t	*phyi;
2988 
2989 	if (ill == NULL)
2990 		return;
2991 	ipst = ill->ill_ipst;
2992 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2993 
2994 	/*
2995 	 * If the ill was never inserted into the AVL tree
2996 	 * we skip the if branch.
2997 	 */
2998 	if (ill->ill_ifptr != NULL) {
2999 		/*
3000 		 * remove from AVL tree and free ppa number
3001 		 */
3002 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3003 
3004 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3005 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3006 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3007 		}
3008 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3009 			ill_delete_interface_type(ill->ill_ifptr);
3010 		}
3011 
3012 		/*
3013 		 * Indicate ill is no longer in the list.
3014 		 */
3015 		ill->ill_ifptr = NULL;
3016 		ill->ill_name_length = 0;
3017 		ill->ill_name[0] = '\0';
3018 		ill->ill_ppa = UINT_MAX;
3019 	}
3020 
3021 	/* Generate one last event for this ill. */
3022 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3023 	    ill->ill_name_length);
3024 
3025 	ASSERT(ill->ill_phyint != NULL);
3026 	phyi = ill->ill_phyint;
3027 	ill->ill_phyint = NULL;
3028 
3029 	/*
3030 	 * ill_init allocates a phyint always to store the copy
3031 	 * of flags relevant to phyint. At that point in time, we could
3032 	 * not assign the name and hence phyint_illv4/v6 could not be
3033 	 * initialized. Later in ipif_set_values, we assign the name to
3034 	 * the ill, at which point in time we assign phyint_illv4/v6.
3035 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3036 	 */
3037 	if (ill->ill_flags & ILLF_IPV6)
3038 		phyi->phyint_illv6 = NULL;
3039 	else
3040 		phyi->phyint_illv4 = NULL;
3041 
3042 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3043 		rw_exit(&ipst->ips_ill_g_lock);
3044 		return;
3045 	}
3046 
3047 	/*
3048 	 * There are no ills left on this phyint; pull it out of the phyint
3049 	 * avl trees, and free it.
3050 	 */
3051 	if (phyi->phyint_ifindex > 0) {
3052 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3053 		    phyi);
3054 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3055 		    phyi);
3056 	}
3057 	rw_exit(&ipst->ips_ill_g_lock);
3058 
3059 	phyint_free(phyi);
3060 }
3061 
3062 /*
3063  * allocate a ppa, if the number of plumbed interfaces of this type are
3064  * less than ill_no_arena do a linear search to find a unused ppa.
3065  * When the number goes beyond ill_no_arena switch to using an arena.
3066  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3067  * is the return value for an error condition, so allocation starts at one
3068  * and is decremented by one.
3069  */
3070 static int
3071 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3072 {
3073 	ill_t *tmp_ill;
3074 	uint_t start, end;
3075 	int ppa;
3076 
3077 	if (ifp->illif_ppa_arena == NULL &&
3078 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3079 		/*
3080 		 * Create an arena.
3081 		 */
3082 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3083 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3084 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3085 			/* allocate what has already been assigned */
3086 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3087 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3088 		    tmp_ill, AVL_AFTER)) {
3089 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3090 			    1,		/* size */
3091 			    1,		/* align/quantum */
3092 			    0,		/* phase */
3093 			    0,		/* nocross */
3094 			    /* minaddr */
3095 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3096 			    /* maxaddr */
3097 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3098 			    VM_NOSLEEP|VM_FIRSTFIT);
3099 			if (ppa == 0) {
3100 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3101 				    " failed while switching"));
3102 				vmem_destroy(ifp->illif_ppa_arena);
3103 				ifp->illif_ppa_arena = NULL;
3104 				break;
3105 			}
3106 		}
3107 	}
3108 
3109 	if (ifp->illif_ppa_arena != NULL) {
3110 		if (ill->ill_ppa == UINT_MAX) {
3111 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3112 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3113 			if (ppa == 0)
3114 				return (EAGAIN);
3115 			ill->ill_ppa = --ppa;
3116 		} else {
3117 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3118 			    1, 		/* size */
3119 			    1, 		/* align/quantum */
3120 			    0, 		/* phase */
3121 			    0, 		/* nocross */
3122 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3123 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3124 			    VM_NOSLEEP|VM_FIRSTFIT);
3125 			/*
3126 			 * Most likely the allocation failed because
3127 			 * the requested ppa was in use.
3128 			 */
3129 			if (ppa == 0)
3130 				return (EEXIST);
3131 		}
3132 		return (0);
3133 	}
3134 
3135 	/*
3136 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3137 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3138 	 */
3139 	if (ill->ill_ppa == UINT_MAX) {
3140 		end = UINT_MAX - 1;
3141 		start = 0;
3142 	} else {
3143 		end = start = ill->ill_ppa;
3144 	}
3145 
3146 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3147 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3148 		if (start++ >= end) {
3149 			if (ill->ill_ppa == UINT_MAX)
3150 				return (EAGAIN);
3151 			else
3152 				return (EEXIST);
3153 		}
3154 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3155 	}
3156 	ill->ill_ppa = start;
3157 	return (0);
3158 }
3159 
3160 /*
3161  * Insert ill into the list of configured ill's. Once this function completes,
3162  * the ill is globally visible and is available through lookups. More precisely
3163  * this happens after the caller drops the ill_g_lock.
3164  */
3165 static int
3166 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3167 {
3168 	ill_if_t *ill_interface;
3169 	avl_index_t where = 0;
3170 	int error;
3171 	int name_length;
3172 	int index;
3173 	boolean_t check_length = B_FALSE;
3174 	ip_stack_t	*ipst = ill->ill_ipst;
3175 
3176 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3177 
3178 	name_length = mi_strlen(name) + 1;
3179 
3180 	if (isv6)
3181 		index = IP_V6_G_HEAD;
3182 	else
3183 		index = IP_V4_G_HEAD;
3184 
3185 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3186 	/*
3187 	 * Search for interface type based on name
3188 	 */
3189 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3190 		if ((ill_interface->illif_name_len == name_length) &&
3191 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3192 			break;
3193 		}
3194 		ill_interface = ill_interface->illif_next;
3195 	}
3196 
3197 	/*
3198 	 * Interface type not found, create one.
3199 	 */
3200 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3201 		ill_g_head_t ghead;
3202 
3203 		/*
3204 		 * allocate ill_if_t structure
3205 		 */
3206 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3207 		if (ill_interface == NULL) {
3208 			return (ENOMEM);
3209 		}
3210 
3211 		(void) strcpy(ill_interface->illif_name, name);
3212 		ill_interface->illif_name_len = name_length;
3213 
3214 		avl_create(&ill_interface->illif_avl_by_ppa,
3215 		    ill_compare_ppa, sizeof (ill_t),
3216 		    offsetof(struct ill_s, ill_avl_byppa));
3217 
3218 		/*
3219 		 * link the structure in the back to maintain order
3220 		 * of configuration for ifconfig output.
3221 		 */
3222 		ghead = ipst->ips_ill_g_heads[index];
3223 		insque(ill_interface, ghead.ill_g_list_tail);
3224 	}
3225 
3226 	if (ill->ill_ppa == UINT_MAX)
3227 		check_length = B_TRUE;
3228 
3229 	error = ill_alloc_ppa(ill_interface, ill);
3230 	if (error != 0) {
3231 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3232 			ill_delete_interface_type(ill->ill_ifptr);
3233 		return (error);
3234 	}
3235 
3236 	/*
3237 	 * When the ppa is choosen by the system, check that there is
3238 	 * enough space to insert ppa. if a specific ppa was passed in this
3239 	 * check is not required as the interface name passed in will have
3240 	 * the right ppa in it.
3241 	 */
3242 	if (check_length) {
3243 		/*
3244 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3245 		 */
3246 		char buf[sizeof (uint_t) * 3];
3247 
3248 		/*
3249 		 * convert ppa to string to calculate the amount of space
3250 		 * required for it in the name.
3251 		 */
3252 		numtos(ill->ill_ppa, buf);
3253 
3254 		/* Do we have enough space to insert ppa ? */
3255 
3256 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3257 			/* Free ppa and interface type struct */
3258 			if (ill_interface->illif_ppa_arena != NULL) {
3259 				vmem_free(ill_interface->illif_ppa_arena,
3260 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3261 			}
3262 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3263 				ill_delete_interface_type(ill->ill_ifptr);
3264 
3265 			return (EINVAL);
3266 		}
3267 	}
3268 
3269 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3270 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3271 
3272 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3273 	    &where);
3274 	ill->ill_ifptr = ill_interface;
3275 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3276 
3277 	ill_phyint_reinit(ill);
3278 	return (0);
3279 }
3280 
3281 /* Initialize the per phyint ipsq used for serialization */
3282 static boolean_t
3283 ipsq_init(ill_t *ill, boolean_t enter)
3284 {
3285 	ipsq_t  *ipsq;
3286 	ipxop_t	*ipx;
3287 
3288 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3289 		return (B_FALSE);
3290 
3291 	ill->ill_phyint->phyint_ipsq = ipsq;
3292 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3293 	ipx->ipx_ipsq = ipsq;
3294 	ipsq->ipsq_next = ipsq;
3295 	ipsq->ipsq_phyint = ill->ill_phyint;
3296 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3297 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3298 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3299 	if (enter) {
3300 		ipx->ipx_writer = curthread;
3301 		ipx->ipx_forced = B_FALSE;
3302 		ipx->ipx_reentry_cnt = 1;
3303 #ifdef DEBUG
3304 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3305 #endif
3306 	}
3307 	return (B_TRUE);
3308 }
3309 
3310 /*
3311  * ill_init is called by ip_open when a device control stream is opened.
3312  * It does a few initializations, and shoots a DL_INFO_REQ message down
3313  * to the driver.  The response is later picked up in ip_rput_dlpi and
3314  * used to set up default mechanisms for talking to the driver.  (Always
3315  * called as writer.)
3316  *
3317  * If this function returns error, ip_open will call ip_close which in
3318  * turn will call ill_delete to clean up any memory allocated here that
3319  * is not yet freed.
3320  */
3321 int
3322 ill_init(queue_t *q, ill_t *ill)
3323 {
3324 	int	count;
3325 	dl_info_req_t	*dlir;
3326 	mblk_t	*info_mp;
3327 	uchar_t *frag_ptr;
3328 
3329 	/*
3330 	 * The ill is initialized to zero by mi_alloc*(). In addition
3331 	 * some fields already contain valid values, initialized in
3332 	 * ip_open(), before we reach here.
3333 	 */
3334 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3335 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3336 	ill->ill_saved_ire_cnt = 0;
3337 
3338 	ill->ill_rq = q;
3339 	ill->ill_wq = WR(q);
3340 
3341 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3342 	    BPRI_HI);
3343 	if (info_mp == NULL)
3344 		return (ENOMEM);
3345 
3346 	/*
3347 	 * Allocate sufficient space to contain our fragment hash table and
3348 	 * the device name.
3349 	 */
3350 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3351 	if (frag_ptr == NULL) {
3352 		freemsg(info_mp);
3353 		return (ENOMEM);
3354 	}
3355 	ill->ill_frag_ptr = frag_ptr;
3356 	ill->ill_frag_free_num_pkts = 0;
3357 	ill->ill_last_frag_clean_time = 0;
3358 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3359 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3360 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3361 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3362 		    NULL, MUTEX_DEFAULT, NULL);
3363 	}
3364 
3365 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3366 	if (ill->ill_phyint == NULL) {
3367 		freemsg(info_mp);
3368 		mi_free(frag_ptr);
3369 		return (ENOMEM);
3370 	}
3371 
3372 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3373 	/*
3374 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3375 	 * at this point because of the following reason. If we can't
3376 	 * enter the ipsq at some point and cv_wait, the writer that
3377 	 * wakes us up tries to locate us using the list of all phyints
3378 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3379 	 * If we don't set it now, we risk a missed wakeup.
3380 	 */
3381 	ill->ill_phyint->phyint_illv4 = ill;
3382 	ill->ill_ppa = UINT_MAX;
3383 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3384 
3385 	ill_set_inputfn(ill);
3386 
3387 	if (!ipsq_init(ill, B_TRUE)) {
3388 		freemsg(info_mp);
3389 		mi_free(frag_ptr);
3390 		mi_free(ill->ill_phyint);
3391 		return (ENOMEM);
3392 	}
3393 
3394 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3395 
3396 	/* Frag queue limit stuff */
3397 	ill->ill_frag_count = 0;
3398 	ill->ill_ipf_gen = 0;
3399 
3400 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3401 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3402 	ill->ill_global_timer = INFINITY;
3403 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3404 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3405 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3406 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3407 
3408 	/*
3409 	 * Initialize IPv6 configuration variables.  The IP module is always
3410 	 * opened as an IPv4 module.  Instead tracking down the cases where
3411 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3412 	 * here for convenience, this has no effect until the ill is set to do
3413 	 * IPv6.
3414 	 */
3415 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3416 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3417 	ill->ill_max_buf = ND_MAX_Q;
3418 	ill->ill_refcnt = 0;
3419 
3420 	/* Send down the Info Request to the driver. */
3421 	info_mp->b_datap->db_type = M_PCPROTO;
3422 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3423 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3424 	dlir->dl_primitive = DL_INFO_REQ;
3425 
3426 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3427 
3428 	qprocson(q);
3429 	ill_dlpi_send(ill, info_mp);
3430 
3431 	return (0);
3432 }
3433 
3434 /*
3435  * ill_dls_info
3436  * creates datalink socket info from the device.
3437  */
3438 int
3439 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3440 {
3441 	size_t	len;
3442 
3443 	sdl->sdl_family = AF_LINK;
3444 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3445 	sdl->sdl_type = ill->ill_type;
3446 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3447 	len = strlen(sdl->sdl_data);
3448 	ASSERT(len < 256);
3449 	sdl->sdl_nlen = (uchar_t)len;
3450 	sdl->sdl_alen = ill->ill_phys_addr_length;
3451 	sdl->sdl_slen = 0;
3452 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3453 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3454 
3455 	return (sizeof (struct sockaddr_dl));
3456 }
3457 
3458 /*
3459  * ill_xarp_info
3460  * creates xarp info from the device.
3461  */
3462 static int
3463 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3464 {
3465 	sdl->sdl_family = AF_LINK;
3466 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3467 	sdl->sdl_type = ill->ill_type;
3468 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3469 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3470 	sdl->sdl_alen = ill->ill_phys_addr_length;
3471 	sdl->sdl_slen = 0;
3472 	return (sdl->sdl_nlen);
3473 }
3474 
3475 static int
3476 loopback_kstat_update(kstat_t *ksp, int rw)
3477 {
3478 	kstat_named_t *kn;
3479 	netstackid_t	stackid;
3480 	netstack_t	*ns;
3481 	ip_stack_t	*ipst;
3482 
3483 	if (ksp == NULL || ksp->ks_data == NULL)
3484 		return (EIO);
3485 
3486 	if (rw == KSTAT_WRITE)
3487 		return (EACCES);
3488 
3489 	kn = KSTAT_NAMED_PTR(ksp);
3490 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3491 
3492 	ns = netstack_find_by_stackid(stackid);
3493 	if (ns == NULL)
3494 		return (-1);
3495 
3496 	ipst = ns->netstack_ip;
3497 	if (ipst == NULL) {
3498 		netstack_rele(ns);
3499 		return (-1);
3500 	}
3501 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3502 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3503 	netstack_rele(ns);
3504 	return (0);
3505 }
3506 
3507 /*
3508  * Has ifindex been plumbed already?
3509  */
3510 static boolean_t
3511 phyint_exists(uint_t index, ip_stack_t *ipst)
3512 {
3513 	ASSERT(index != 0);
3514 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3515 
3516 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3517 	    &index, NULL) != NULL);
3518 }
3519 
3520 /* Pick a unique ifindex */
3521 boolean_t
3522 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3523 {
3524 	uint_t starting_index;
3525 
3526 	if (!ipst->ips_ill_index_wrap) {
3527 		*indexp = ipst->ips_ill_index++;
3528 		if (ipst->ips_ill_index == 0) {
3529 			/* Reached the uint_t limit Next time wrap  */
3530 			ipst->ips_ill_index_wrap = B_TRUE;
3531 		}
3532 		return (B_TRUE);
3533 	}
3534 
3535 	/*
3536 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3537 	 * at this point and don't want to call any function that attempts
3538 	 * to get the lock again.
3539 	 */
3540 	starting_index = ipst->ips_ill_index++;
3541 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3542 		if (ipst->ips_ill_index != 0 &&
3543 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3544 			/* found unused index - use it */
3545 			*indexp = ipst->ips_ill_index;
3546 			return (B_TRUE);
3547 		}
3548 	}
3549 
3550 	/*
3551 	 * all interface indicies are inuse.
3552 	 */
3553 	return (B_FALSE);
3554 }
3555 
3556 /*
3557  * Assign a unique interface index for the phyint.
3558  */
3559 static boolean_t
3560 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3561 {
3562 	ASSERT(phyi->phyint_ifindex == 0);
3563 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3564 }
3565 
3566 /*
3567  * Initialize the flags on `phyi' as per the provided mactype.
3568  */
3569 static void
3570 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3571 {
3572 	uint64_t flags = 0;
3573 
3574 	/*
3575 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3576 	 * we always presume the underlying hardware is working and set
3577 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3578 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3579 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3580 	 */
3581 	if (mactype == SUNW_DL_IPMP)
3582 		flags |= PHYI_FAILED;
3583 	else
3584 		flags |= PHYI_RUNNING;
3585 
3586 	switch (mactype) {
3587 	case SUNW_DL_VNI:
3588 		flags |= PHYI_VIRTUAL;
3589 		break;
3590 	case SUNW_DL_IPMP:
3591 		flags |= PHYI_IPMP;
3592 		break;
3593 	case DL_LOOP:
3594 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3595 		break;
3596 	}
3597 
3598 	mutex_enter(&phyi->phyint_lock);
3599 	phyi->phyint_flags |= flags;
3600 	mutex_exit(&phyi->phyint_lock);
3601 }
3602 
3603 /*
3604  * Return a pointer to the ill which matches the supplied name.  Note that
3605  * the ill name length includes the null termination character.  (May be
3606  * called as writer.)
3607  * If do_alloc and the interface is "lo0" it will be automatically created.
3608  * Cannot bump up reference on condemned ills. So dup detect can't be done
3609  * using this func.
3610  */
3611 ill_t *
3612 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3613     boolean_t *did_alloc, ip_stack_t *ipst)
3614 {
3615 	ill_t	*ill;
3616 	ipif_t	*ipif;
3617 	ipsq_t	*ipsq;
3618 	kstat_named_t	*kn;
3619 	boolean_t isloopback;
3620 	in6_addr_t ov6addr;
3621 
3622 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3623 
3624 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3625 	ill = ill_find_by_name(name, isv6, ipst);
3626 	rw_exit(&ipst->ips_ill_g_lock);
3627 	if (ill != NULL)
3628 		return (ill);
3629 
3630 	/*
3631 	 * Couldn't find it.  Does this happen to be a lookup for the
3632 	 * loopback device and are we allowed to allocate it?
3633 	 */
3634 	if (!isloopback || !do_alloc)
3635 		return (NULL);
3636 
3637 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3638 	ill = ill_find_by_name(name, isv6, ipst);
3639 	if (ill != NULL) {
3640 		rw_exit(&ipst->ips_ill_g_lock);
3641 		return (ill);
3642 	}
3643 
3644 	/* Create the loopback device on demand */
3645 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3646 	    sizeof (ipif_loopback_name), BPRI_MED));
3647 	if (ill == NULL)
3648 		goto done;
3649 
3650 	*ill = ill_null;
3651 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3652 	ill->ill_ipst = ipst;
3653 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3654 	netstack_hold(ipst->ips_netstack);
3655 	/*
3656 	 * For exclusive stacks we set the zoneid to zero
3657 	 * to make IP operate as if in the global zone.
3658 	 */
3659 	ill->ill_zoneid = GLOBAL_ZONEID;
3660 
3661 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3662 	if (ill->ill_phyint == NULL)
3663 		goto done;
3664 
3665 	if (isv6)
3666 		ill->ill_phyint->phyint_illv6 = ill;
3667 	else
3668 		ill->ill_phyint->phyint_illv4 = ill;
3669 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3670 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3671 
3672 	if (isv6) {
3673 		ill->ill_isv6 = B_TRUE;
3674 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3675 	} else {
3676 		ill->ill_max_frag = ip_loopback_mtuplus;
3677 	}
3678 	if (!ill_allocate_mibs(ill))
3679 		goto done;
3680 	ill->ill_current_frag = ill->ill_max_frag;
3681 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3682 	/*
3683 	 * ipif_loopback_name can't be pointed at directly because its used
3684 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3685 	 * from the glist, ill_glist_delete() sets the first character of
3686 	 * ill_name to '\0'.
3687 	 */
3688 	ill->ill_name = (char *)ill + sizeof (*ill);
3689 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3690 	ill->ill_name_length = sizeof (ipif_loopback_name);
3691 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3692 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3693 
3694 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3695 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3696 	ill->ill_global_timer = INFINITY;
3697 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3698 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3699 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3700 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3701 
3702 	/* No resolver here. */
3703 	ill->ill_net_type = IRE_LOOPBACK;
3704 
3705 	/* Initialize the ipsq */
3706 	if (!ipsq_init(ill, B_FALSE))
3707 		goto done;
3708 
3709 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3710 	if (ipif == NULL)
3711 		goto done;
3712 
3713 	ill->ill_flags = ILLF_MULTICAST;
3714 
3715 	ov6addr = ipif->ipif_v6lcl_addr;
3716 	/* Set up default loopback address and mask. */
3717 	if (!isv6) {
3718 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3719 
3720 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3721 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3722 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3723 		    ipif->ipif_v6subnet);
3724 		ill->ill_flags |= ILLF_IPV4;
3725 	} else {
3726 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3727 		ipif->ipif_v6net_mask = ipv6_all_ones;
3728 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3729 		    ipif->ipif_v6subnet);
3730 		ill->ill_flags |= ILLF_IPV6;
3731 	}
3732 
3733 	/*
3734 	 * Chain us in at the end of the ill list. hold the ill
3735 	 * before we make it globally visible. 1 for the lookup.
3736 	 */
3737 	ill->ill_refcnt = 0;
3738 	ill_refhold(ill);
3739 
3740 	ill->ill_frag_count = 0;
3741 	ill->ill_frag_free_num_pkts = 0;
3742 	ill->ill_last_frag_clean_time = 0;
3743 
3744 	ipsq = ill->ill_phyint->phyint_ipsq;
3745 
3746 	ill_set_inputfn(ill);
3747 
3748 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3749 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3750 
3751 	/* Let SCTP know so that it can add this to its list */
3752 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3753 
3754 	/*
3755 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3756 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3757 	 * requires to be after ill_glist_insert() since we need the
3758 	 * ill_index set. Pass on ipv6_loopback as the old address.
3759 	 */
3760 	sctp_update_ipif_addr(ipif, ov6addr);
3761 
3762 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3763 
3764 	/*
3765 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3766 	 * If so, free our original one.
3767 	 */
3768 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3769 		ipsq_delete(ipsq);
3770 
3771 	if (ipst->ips_loopback_ksp == NULL) {
3772 		/* Export loopback interface statistics */
3773 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3774 		    ipif_loopback_name, "net",
3775 		    KSTAT_TYPE_NAMED, 2, 0,
3776 		    ipst->ips_netstack->netstack_stackid);
3777 		if (ipst->ips_loopback_ksp != NULL) {
3778 			ipst->ips_loopback_ksp->ks_update =
3779 			    loopback_kstat_update;
3780 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3781 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3782 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3783 			ipst->ips_loopback_ksp->ks_private =
3784 			    (void *)(uintptr_t)ipst->ips_netstack->
3785 			    netstack_stackid;
3786 			kstat_install(ipst->ips_loopback_ksp);
3787 		}
3788 	}
3789 
3790 	*did_alloc = B_TRUE;
3791 	rw_exit(&ipst->ips_ill_g_lock);
3792 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3793 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3794 	return (ill);
3795 done:
3796 	if (ill != NULL) {
3797 		if (ill->ill_phyint != NULL) {
3798 			ipsq = ill->ill_phyint->phyint_ipsq;
3799 			if (ipsq != NULL) {
3800 				ipsq->ipsq_phyint = NULL;
3801 				ipsq_delete(ipsq);
3802 			}
3803 			mi_free(ill->ill_phyint);
3804 		}
3805 		ill_free_mib(ill);
3806 		if (ill->ill_ipst != NULL)
3807 			netstack_rele(ill->ill_ipst->ips_netstack);
3808 		mi_free(ill);
3809 	}
3810 	rw_exit(&ipst->ips_ill_g_lock);
3811 	return (NULL);
3812 }
3813 
3814 /*
3815  * For IPP calls - use the ip_stack_t for global stack.
3816  */
3817 ill_t *
3818 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3819 {
3820 	ip_stack_t	*ipst;
3821 	ill_t		*ill;
3822 
3823 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3824 	if (ipst == NULL) {
3825 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3826 		return (NULL);
3827 	}
3828 
3829 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3830 	netstack_rele(ipst->ips_netstack);
3831 	return (ill);
3832 }
3833 
3834 /*
3835  * Return a pointer to the ill which matches the index and IP version type.
3836  */
3837 ill_t *
3838 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3839 {
3840 	ill_t	*ill;
3841 	phyint_t *phyi;
3842 
3843 	/*
3844 	 * Indexes are stored in the phyint - a common structure
3845 	 * to both IPv4 and IPv6.
3846 	 */
3847 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3848 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3849 	    (void *) &index, NULL);
3850 	if (phyi != NULL) {
3851 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3852 		if (ill != NULL) {
3853 			mutex_enter(&ill->ill_lock);
3854 			if (!ILL_IS_CONDEMNED(ill)) {
3855 				ill_refhold_locked(ill);
3856 				mutex_exit(&ill->ill_lock);
3857 				rw_exit(&ipst->ips_ill_g_lock);
3858 				return (ill);
3859 			}
3860 			mutex_exit(&ill->ill_lock);
3861 		}
3862 	}
3863 	rw_exit(&ipst->ips_ill_g_lock);
3864 	return (NULL);
3865 }
3866 
3867 /*
3868  * Verify whether or not an interface index is valid for the specified zoneid
3869  * to transmit packets.
3870  * It can be zero (meaning "reset") or an interface index assigned
3871  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3872  */
3873 boolean_t
3874 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3875     ip_stack_t *ipst)
3876 {
3877 	ill_t		*ill;
3878 
3879 	if (ifindex == 0)
3880 		return (B_TRUE);
3881 
3882 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3883 	if (ill == NULL)
3884 		return (B_FALSE);
3885 	if (IS_VNI(ill)) {
3886 		ill_refrele(ill);
3887 		return (B_FALSE);
3888 	}
3889 	ill_refrele(ill);
3890 	return (B_TRUE);
3891 }
3892 
3893 /*
3894  * Return the ifindex next in sequence after the passed in ifindex.
3895  * If there is no next ifindex for the given protocol, return 0.
3896  */
3897 uint_t
3898 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3899 {
3900 	phyint_t *phyi;
3901 	phyint_t *phyi_initial;
3902 	uint_t   ifindex;
3903 
3904 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3905 
3906 	if (index == 0) {
3907 		phyi = avl_first(
3908 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3909 	} else {
3910 		phyi = phyi_initial = avl_find(
3911 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3912 		    (void *) &index, NULL);
3913 	}
3914 
3915 	for (; phyi != NULL;
3916 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3917 	    phyi, AVL_AFTER)) {
3918 		/*
3919 		 * If we're not returning the first interface in the tree
3920 		 * and we still haven't moved past the phyint_t that
3921 		 * corresponds to index, avl_walk needs to be called again
3922 		 */
3923 		if (!((index != 0) && (phyi == phyi_initial))) {
3924 			if (isv6) {
3925 				if ((phyi->phyint_illv6) &&
3926 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3927 				    (phyi->phyint_illv6->ill_isv6 == 1))
3928 					break;
3929 			} else {
3930 				if ((phyi->phyint_illv4) &&
3931 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3932 				    (phyi->phyint_illv4->ill_isv6 == 0))
3933 					break;
3934 			}
3935 		}
3936 	}
3937 
3938 	rw_exit(&ipst->ips_ill_g_lock);
3939 
3940 	if (phyi != NULL)
3941 		ifindex = phyi->phyint_ifindex;
3942 	else
3943 		ifindex = 0;
3944 
3945 	return (ifindex);
3946 }
3947 
3948 /*
3949  * Return the ifindex for the named interface.
3950  * If there is no next ifindex for the interface, return 0.
3951  */
3952 uint_t
3953 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3954 {
3955 	phyint_t	*phyi;
3956 	avl_index_t	where = 0;
3957 	uint_t		ifindex;
3958 
3959 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3960 
3961 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3962 	    name, &where)) == NULL) {
3963 		rw_exit(&ipst->ips_ill_g_lock);
3964 		return (0);
3965 	}
3966 
3967 	ifindex = phyi->phyint_ifindex;
3968 
3969 	rw_exit(&ipst->ips_ill_g_lock);
3970 
3971 	return (ifindex);
3972 }
3973 
3974 /*
3975  * Return the ifindex to be used by upper layer protocols for instance
3976  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3977  */
3978 uint_t
3979 ill_get_upper_ifindex(const ill_t *ill)
3980 {
3981 	if (IS_UNDER_IPMP(ill))
3982 		return (ipmp_ill_get_ipmp_ifindex(ill));
3983 	else
3984 		return (ill->ill_phyint->phyint_ifindex);
3985 }
3986 
3987 
3988 /*
3989  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3990  * that gives a running thread a reference to the ill. This reference must be
3991  * released by the thread when it is done accessing the ill and related
3992  * objects. ill_refcnt can not be used to account for static references
3993  * such as other structures pointing to an ill. Callers must generally
3994  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3995  * or be sure that the ill is not being deleted or changing state before
3996  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3997  * ill won't change any of its critical state such as address, netmask etc.
3998  */
3999 void
4000 ill_refhold(ill_t *ill)
4001 {
4002 	mutex_enter(&ill->ill_lock);
4003 	ill->ill_refcnt++;
4004 	ILL_TRACE_REF(ill);
4005 	mutex_exit(&ill->ill_lock);
4006 }
4007 
4008 void
4009 ill_refhold_locked(ill_t *ill)
4010 {
4011 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4012 	ill->ill_refcnt++;
4013 	ILL_TRACE_REF(ill);
4014 }
4015 
4016 /* Returns true if we managed to get a refhold */
4017 boolean_t
4018 ill_check_and_refhold(ill_t *ill)
4019 {
4020 	mutex_enter(&ill->ill_lock);
4021 	if (!ILL_IS_CONDEMNED(ill)) {
4022 		ill_refhold_locked(ill);
4023 		mutex_exit(&ill->ill_lock);
4024 		return (B_TRUE);
4025 	}
4026 	mutex_exit(&ill->ill_lock);
4027 	return (B_FALSE);
4028 }
4029 
4030 /*
4031  * Must not be called while holding any locks. Otherwise if this is
4032  * the last reference to be released, there is a chance of recursive mutex
4033  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4034  * to restart an ioctl.
4035  */
4036 void
4037 ill_refrele(ill_t *ill)
4038 {
4039 	mutex_enter(&ill->ill_lock);
4040 	ASSERT(ill->ill_refcnt != 0);
4041 	ill->ill_refcnt--;
4042 	ILL_UNTRACE_REF(ill);
4043 	if (ill->ill_refcnt != 0) {
4044 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4045 		mutex_exit(&ill->ill_lock);
4046 		return;
4047 	}
4048 
4049 	/* Drops the ill_lock */
4050 	ipif_ill_refrele_tail(ill);
4051 }
4052 
4053 /*
4054  * Obtain a weak reference count on the ill. This reference ensures the
4055  * ill won't be freed, but the ill may change any of its critical state
4056  * such as netmask, address etc. Returns an error if the ill has started
4057  * closing.
4058  */
4059 boolean_t
4060 ill_waiter_inc(ill_t *ill)
4061 {
4062 	mutex_enter(&ill->ill_lock);
4063 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4064 		mutex_exit(&ill->ill_lock);
4065 		return (B_FALSE);
4066 	}
4067 	ill->ill_waiters++;
4068 	mutex_exit(&ill->ill_lock);
4069 	return (B_TRUE);
4070 }
4071 
4072 void
4073 ill_waiter_dcr(ill_t *ill)
4074 {
4075 	mutex_enter(&ill->ill_lock);
4076 	ill->ill_waiters--;
4077 	if (ill->ill_waiters == 0)
4078 		cv_broadcast(&ill->ill_cv);
4079 	mutex_exit(&ill->ill_lock);
4080 }
4081 
4082 /*
4083  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4084  * driver.  We construct best guess defaults for lower level information that
4085  * we need.  If an interface is brought up without injection of any overriding
4086  * information from outside, we have to be ready to go with these defaults.
4087  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4088  * we primarely want the dl_provider_style.
4089  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4090  * at which point we assume the other part of the information is valid.
4091  */
4092 void
4093 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4094 {
4095 	uchar_t		*brdcst_addr;
4096 	uint_t		brdcst_addr_length, phys_addr_length;
4097 	t_scalar_t	sap_length;
4098 	dl_info_ack_t	*dlia;
4099 	ip_m_t		*ipm;
4100 	dl_qos_cl_sel1_t *sel1;
4101 	int		min_mtu;
4102 
4103 	ASSERT(IAM_WRITER_ILL(ill));
4104 
4105 	/*
4106 	 * Till the ill is fully up  the ill is not globally visible.
4107 	 * So no need for a lock.
4108 	 */
4109 	dlia = (dl_info_ack_t *)mp->b_rptr;
4110 	ill->ill_mactype = dlia->dl_mac_type;
4111 
4112 	ipm = ip_m_lookup(dlia->dl_mac_type);
4113 	if (ipm == NULL) {
4114 		ipm = ip_m_lookup(DL_OTHER);
4115 		ASSERT(ipm != NULL);
4116 	}
4117 	ill->ill_media = ipm;
4118 
4119 	/*
4120 	 * When the new DLPI stuff is ready we'll pull lengths
4121 	 * from dlia.
4122 	 */
4123 	if (dlia->dl_version == DL_VERSION_2) {
4124 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4125 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4126 		    brdcst_addr_length);
4127 		if (brdcst_addr == NULL) {
4128 			brdcst_addr_length = 0;
4129 		}
4130 		sap_length = dlia->dl_sap_length;
4131 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4132 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4133 		    brdcst_addr_length, sap_length, phys_addr_length));
4134 	} else {
4135 		brdcst_addr_length = 6;
4136 		brdcst_addr = ip_six_byte_all_ones;
4137 		sap_length = -2;
4138 		phys_addr_length = brdcst_addr_length;
4139 	}
4140 
4141 	ill->ill_bcast_addr_length = brdcst_addr_length;
4142 	ill->ill_phys_addr_length = phys_addr_length;
4143 	ill->ill_sap_length = sap_length;
4144 
4145 	/*
4146 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4147 	 * but we must ensure a minimum IP MTU is used since other bits of
4148 	 * IP will fly apart otherwise.
4149 	 */
4150 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4151 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4152 	ill->ill_current_frag = ill->ill_max_frag;
4153 	ill->ill_mtu = ill->ill_max_frag;
4154 
4155 	ill->ill_type = ipm->ip_m_type;
4156 
4157 	if (!ill->ill_dlpi_style_set) {
4158 		if (dlia->dl_provider_style == DL_STYLE2)
4159 			ill->ill_needs_attach = 1;
4160 
4161 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4162 
4163 		/*
4164 		 * Allocate the first ipif on this ill.  We don't delay it
4165 		 * further as ioctl handling assumes at least one ipif exists.
4166 		 *
4167 		 * At this point we don't know whether the ill is v4 or v6.
4168 		 * We will know this whan the SIOCSLIFNAME happens and
4169 		 * the correct value for ill_isv6 will be assigned in
4170 		 * ipif_set_values(). We need to hold the ill lock and
4171 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4172 		 * the wakeup.
4173 		 */
4174 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4175 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4176 		mutex_enter(&ill->ill_lock);
4177 		ASSERT(ill->ill_dlpi_style_set == 0);
4178 		ill->ill_dlpi_style_set = 1;
4179 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4180 		cv_broadcast(&ill->ill_cv);
4181 		mutex_exit(&ill->ill_lock);
4182 		freemsg(mp);
4183 		return;
4184 	}
4185 	ASSERT(ill->ill_ipif != NULL);
4186 	/*
4187 	 * We know whether it is IPv4 or IPv6 now, as this is the
4188 	 * second DL_INFO_ACK we are recieving in response to the
4189 	 * DL_INFO_REQ sent in ipif_set_values.
4190 	 */
4191 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4192 	/*
4193 	 * Clear all the flags that were set based on ill_bcast_addr_length
4194 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4195 	 * changed now and we need to re-evaluate.
4196 	 */
4197 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4198 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4199 
4200 	/*
4201 	 * Free ill_bcast_mp as things could have changed now.
4202 	 *
4203 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4204 	 * with no underlying interfaces (and thus an unknown broadcast
4205 	 * address length), but we enforce that an interface is broadcast-
4206 	 * capable as part of allowing it to join a group.
4207 	 */
4208 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4209 		if (ill->ill_bcast_mp != NULL)
4210 			freemsg(ill->ill_bcast_mp);
4211 		ill->ill_net_type = IRE_IF_NORESOLVER;
4212 
4213 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4214 		    ill->ill_phys_addr_length,
4215 		    ill->ill_sap,
4216 		    ill->ill_sap_length);
4217 
4218 		if (ill->ill_isv6)
4219 			/*
4220 			 * Note: xresolv interfaces will eventually need NOARP
4221 			 * set here as well, but that will require those
4222 			 * external resolvers to have some knowledge of
4223 			 * that flag and act appropriately. Not to be changed
4224 			 * at present.
4225 			 */
4226 			ill->ill_flags |= ILLF_NONUD;
4227 		else
4228 			ill->ill_flags |= ILLF_NOARP;
4229 
4230 		if (ill->ill_mactype == SUNW_DL_VNI) {
4231 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4232 		} else if (ill->ill_phys_addr_length == 0 ||
4233 		    ill->ill_mactype == DL_IPV4 ||
4234 		    ill->ill_mactype == DL_IPV6) {
4235 			/*
4236 			 * The underying link is point-to-point, so mark the
4237 			 * interface as such.  We can do IP multicast over
4238 			 * such a link since it transmits all network-layer
4239 			 * packets to the remote side the same way.
4240 			 */
4241 			ill->ill_flags |= ILLF_MULTICAST;
4242 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4243 		}
4244 	} else {
4245 		ill->ill_net_type = IRE_IF_RESOLVER;
4246 		if (ill->ill_bcast_mp != NULL)
4247 			freemsg(ill->ill_bcast_mp);
4248 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4249 		    ill->ill_bcast_addr_length, ill->ill_sap,
4250 		    ill->ill_sap_length);
4251 		/*
4252 		 * Later detect lack of DLPI driver multicast
4253 		 * capability by catching DL_ENABMULTI errors in
4254 		 * ip_rput_dlpi.
4255 		 */
4256 		ill->ill_flags |= ILLF_MULTICAST;
4257 		if (!ill->ill_isv6)
4258 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4259 	}
4260 
4261 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4262 	if (ill->ill_mactype == SUNW_DL_IPMP)
4263 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4264 
4265 	/* By default an interface does not support any CoS marking */
4266 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4267 
4268 	/*
4269 	 * If we get QoS information in DL_INFO_ACK, the device supports
4270 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4271 	 */
4272 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4273 	    dlia->dl_qos_length);
4274 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4275 		ill->ill_flags |= ILLF_COS_ENABLED;
4276 	}
4277 
4278 	/* Clear any previous error indication. */
4279 	ill->ill_error = 0;
4280 	freemsg(mp);
4281 }
4282 
4283 /*
4284  * Perform various checks to verify that an address would make sense as a
4285  * local, remote, or subnet interface address.
4286  */
4287 static boolean_t
4288 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4289 {
4290 	ipaddr_t	net_mask;
4291 
4292 	/*
4293 	 * Don't allow all zeroes, or all ones, but allow
4294 	 * all ones netmask.
4295 	 */
4296 	if ((net_mask = ip_net_mask(addr)) == 0)
4297 		return (B_FALSE);
4298 	/* A given netmask overrides the "guess" netmask */
4299 	if (subnet_mask != 0)
4300 		net_mask = subnet_mask;
4301 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4302 	    (addr == (addr | ~net_mask)))) {
4303 		return (B_FALSE);
4304 	}
4305 
4306 	/*
4307 	 * Even if the netmask is all ones, we do not allow address to be
4308 	 * 255.255.255.255
4309 	 */
4310 	if (addr == INADDR_BROADCAST)
4311 		return (B_FALSE);
4312 
4313 	if (CLASSD(addr))
4314 		return (B_FALSE);
4315 
4316 	return (B_TRUE);
4317 }
4318 
4319 #define	V6_IPIF_LINKLOCAL(p)	\
4320 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4321 
4322 /*
4323  * Compare two given ipifs and check if the second one is better than
4324  * the first one using the order of preference (not taking deprecated
4325  * into acount) specified in ipif_lookup_multicast().
4326  */
4327 static boolean_t
4328 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4329 {
4330 	/* Check the least preferred first. */
4331 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4332 		/* If both ipifs are the same, use the first one. */
4333 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4334 			return (B_FALSE);
4335 		else
4336 			return (B_TRUE);
4337 	}
4338 
4339 	/* For IPv6, check for link local address. */
4340 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4341 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4342 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4343 			/* The second one is equal or less preferred. */
4344 			return (B_FALSE);
4345 		} else {
4346 			return (B_TRUE);
4347 		}
4348 	}
4349 
4350 	/* Then check for point to point interface. */
4351 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4352 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4353 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4354 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4355 			return (B_FALSE);
4356 		} else {
4357 			return (B_TRUE);
4358 		}
4359 	}
4360 
4361 	/* old_ipif is a normal interface, so no need to use the new one. */
4362 	return (B_FALSE);
4363 }
4364 
4365 /*
4366  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4367  * The ipif must be up, and its ill must multicast-capable, not
4368  * condemned, not an underlying interface in an IPMP group, and
4369  * not a VNI interface.  Order of preference:
4370  *
4371  * 	1a. normal
4372  * 	1b. normal, but deprecated
4373  * 	2a. point to point
4374  * 	2b. point to point, but deprecated
4375  * 	3a. link local
4376  * 	3b. link local, but deprecated
4377  * 	4. loopback.
4378  */
4379 static ipif_t *
4380 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4381 {
4382 	ill_t			*ill;
4383 	ill_walk_context_t	ctx;
4384 	ipif_t			*ipif;
4385 	ipif_t			*saved_ipif = NULL;
4386 	ipif_t			*dep_ipif = NULL;
4387 
4388 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4389 	if (isv6)
4390 		ill = ILL_START_WALK_V6(&ctx, ipst);
4391 	else
4392 		ill = ILL_START_WALK_V4(&ctx, ipst);
4393 
4394 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4395 		mutex_enter(&ill->ill_lock);
4396 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4397 		    ILL_IS_CONDEMNED(ill) ||
4398 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4399 			mutex_exit(&ill->ill_lock);
4400 			continue;
4401 		}
4402 		for (ipif = ill->ill_ipif; ipif != NULL;
4403 		    ipif = ipif->ipif_next) {
4404 			if (zoneid != ipif->ipif_zoneid &&
4405 			    zoneid != ALL_ZONES &&
4406 			    ipif->ipif_zoneid != ALL_ZONES) {
4407 				continue;
4408 			}
4409 			if (!(ipif->ipif_flags & IPIF_UP) ||
4410 			    IPIF_IS_CONDEMNED(ipif)) {
4411 				continue;
4412 			}
4413 
4414 			/*
4415 			 * Found one candidate.  If it is deprecated,
4416 			 * remember it in dep_ipif.  If it is not deprecated,
4417 			 * remember it in saved_ipif.
4418 			 */
4419 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4420 				if (dep_ipif == NULL) {
4421 					dep_ipif = ipif;
4422 				} else if (ipif_comp_multi(dep_ipif, ipif,
4423 				    isv6)) {
4424 					/*
4425 					 * If the previous dep_ipif does not
4426 					 * belong to the same ill, we've done
4427 					 * a ipif_refhold() on it.  So we need
4428 					 * to release it.
4429 					 */
4430 					if (dep_ipif->ipif_ill != ill)
4431 						ipif_refrele(dep_ipif);
4432 					dep_ipif = ipif;
4433 				}
4434 				continue;
4435 			}
4436 			if (saved_ipif == NULL) {
4437 				saved_ipif = ipif;
4438 			} else {
4439 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4440 					if (saved_ipif->ipif_ill != ill)
4441 						ipif_refrele(saved_ipif);
4442 					saved_ipif = ipif;
4443 				}
4444 			}
4445 		}
4446 		/*
4447 		 * Before going to the next ill, do a ipif_refhold() on the
4448 		 * saved ones.
4449 		 */
4450 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4451 			ipif_refhold_locked(saved_ipif);
4452 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4453 			ipif_refhold_locked(dep_ipif);
4454 		mutex_exit(&ill->ill_lock);
4455 	}
4456 	rw_exit(&ipst->ips_ill_g_lock);
4457 
4458 	/*
4459 	 * If we have only the saved_ipif, return it.  But if we have both
4460 	 * saved_ipif and dep_ipif, check to see which one is better.
4461 	 */
4462 	if (saved_ipif != NULL) {
4463 		if (dep_ipif != NULL) {
4464 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4465 				ipif_refrele(saved_ipif);
4466 				return (dep_ipif);
4467 			} else {
4468 				ipif_refrele(dep_ipif);
4469 				return (saved_ipif);
4470 			}
4471 		}
4472 		return (saved_ipif);
4473 	} else {
4474 		return (dep_ipif);
4475 	}
4476 }
4477 
4478 ill_t *
4479 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4480 {
4481 	ipif_t *ipif;
4482 	ill_t *ill;
4483 
4484 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4485 	if (ipif == NULL)
4486 		return (NULL);
4487 
4488 	ill = ipif->ipif_ill;
4489 	ill_refhold(ill);
4490 	ipif_refrele(ipif);
4491 	return (ill);
4492 }
4493 
4494 /*
4495  * This function is called when an application does not specify an interface
4496  * to be used for multicast traffic (joining a group/sending data).  It
4497  * calls ire_lookup_multi() to look for an interface route for the
4498  * specified multicast group.  Doing this allows the administrator to add
4499  * prefix routes for multicast to indicate which interface to be used for
4500  * multicast traffic in the above scenario.  The route could be for all
4501  * multicast (224.0/4), for a single multicast group (a /32 route) or
4502  * anything in between.  If there is no such multicast route, we just find
4503  * any multicast capable interface and return it.  The returned ipif
4504  * is refhold'ed.
4505  *
4506  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4507  * unicast table. This is used by CGTP.
4508  */
4509 ill_t *
4510 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4511     boolean_t *multirtp, ipaddr_t *setsrcp)
4512 {
4513 	ill_t			*ill;
4514 
4515 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4516 	if (ill != NULL)
4517 		return (ill);
4518 
4519 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4520 }
4521 
4522 /*
4523  * Look for an ipif with the specified interface address and destination.
4524  * The destination address is used only for matching point-to-point interfaces.
4525  */
4526 ipif_t *
4527 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4528 {
4529 	ipif_t	*ipif;
4530 	ill_t	*ill;
4531 	ill_walk_context_t ctx;
4532 
4533 	/*
4534 	 * First match all the point-to-point interfaces
4535 	 * before looking at non-point-to-point interfaces.
4536 	 * This is done to avoid returning non-point-to-point
4537 	 * ipif instead of unnumbered point-to-point ipif.
4538 	 */
4539 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4540 	ill = ILL_START_WALK_V4(&ctx, ipst);
4541 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4542 		mutex_enter(&ill->ill_lock);
4543 		for (ipif = ill->ill_ipif; ipif != NULL;
4544 		    ipif = ipif->ipif_next) {
4545 			/* Allow the ipif to be down */
4546 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4547 			    (ipif->ipif_lcl_addr == if_addr) &&
4548 			    (ipif->ipif_pp_dst_addr == dst)) {
4549 				if (!IPIF_IS_CONDEMNED(ipif)) {
4550 					ipif_refhold_locked(ipif);
4551 					mutex_exit(&ill->ill_lock);
4552 					rw_exit(&ipst->ips_ill_g_lock);
4553 					return (ipif);
4554 				}
4555 			}
4556 		}
4557 		mutex_exit(&ill->ill_lock);
4558 	}
4559 	rw_exit(&ipst->ips_ill_g_lock);
4560 
4561 	/* lookup the ipif based on interface address */
4562 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4563 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4564 	return (ipif);
4565 }
4566 
4567 /*
4568  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4569  */
4570 static ipif_t *
4571 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4572     zoneid_t zoneid, ip_stack_t *ipst)
4573 {
4574 	ipif_t  *ipif;
4575 	ill_t   *ill;
4576 	boolean_t ptp = B_FALSE;
4577 	ill_walk_context_t	ctx;
4578 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4579 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4580 
4581 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4582 	/*
4583 	 * Repeat twice, first based on local addresses and
4584 	 * next time for pointopoint.
4585 	 */
4586 repeat:
4587 	ill = ILL_START_WALK_V4(&ctx, ipst);
4588 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4589 		if (match_ill != NULL && ill != match_ill &&
4590 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4591 			continue;
4592 		}
4593 		mutex_enter(&ill->ill_lock);
4594 		for (ipif = ill->ill_ipif; ipif != NULL;
4595 		    ipif = ipif->ipif_next) {
4596 			if (zoneid != ALL_ZONES &&
4597 			    zoneid != ipif->ipif_zoneid &&
4598 			    ipif->ipif_zoneid != ALL_ZONES)
4599 				continue;
4600 
4601 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4602 				continue;
4603 
4604 			/* Allow the ipif to be down */
4605 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4606 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4607 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4608 			    (ipif->ipif_pp_dst_addr == addr))) {
4609 				if (!IPIF_IS_CONDEMNED(ipif)) {
4610 					ipif_refhold_locked(ipif);
4611 					mutex_exit(&ill->ill_lock);
4612 					rw_exit(&ipst->ips_ill_g_lock);
4613 					return (ipif);
4614 				}
4615 			}
4616 		}
4617 		mutex_exit(&ill->ill_lock);
4618 	}
4619 
4620 	/* If we already did the ptp case, then we are done */
4621 	if (ptp) {
4622 		rw_exit(&ipst->ips_ill_g_lock);
4623 		return (NULL);
4624 	}
4625 	ptp = B_TRUE;
4626 	goto repeat;
4627 }
4628 
4629 /*
4630  * Lookup an ipif with the specified address.  For point-to-point links we
4631  * look for matches on either the destination address or the local address,
4632  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4633  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4634  * (or illgrp if `match_ill' is in an IPMP group).
4635  */
4636 ipif_t *
4637 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4638     ip_stack_t *ipst)
4639 {
4640 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4641 	    zoneid, ipst));
4642 }
4643 
4644 /*
4645  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4646  * except that we will only return an address if it is not marked as
4647  * IPIF_DUPLICATE
4648  */
4649 ipif_t *
4650 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4651     ip_stack_t *ipst)
4652 {
4653 	return (ipif_lookup_addr_common(addr, match_ill,
4654 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4655 	    zoneid, ipst));
4656 }
4657 
4658 /*
4659  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4660  * `match_ill' across the IPMP group.  This function is only needed in some
4661  * corner-cases; almost everything should use ipif_lookup_addr().
4662  */
4663 ipif_t *
4664 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4665 {
4666 	ASSERT(match_ill != NULL);
4667 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4668 	    ipst));
4669 }
4670 
4671 /*
4672  * Look for an ipif with the specified address. For point-point links
4673  * we look for matches on either the destination address and the local
4674  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4675  * is set.
4676  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4677  * ill (or illgrp if `match_ill' is in an IPMP group).
4678  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4679  */
4680 zoneid_t
4681 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4682 {
4683 	zoneid_t zoneid;
4684 	ipif_t  *ipif;
4685 	ill_t   *ill;
4686 	boolean_t ptp = B_FALSE;
4687 	ill_walk_context_t	ctx;
4688 
4689 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4690 	/*
4691 	 * Repeat twice, first based on local addresses and
4692 	 * next time for pointopoint.
4693 	 */
4694 repeat:
4695 	ill = ILL_START_WALK_V4(&ctx, ipst);
4696 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4697 		if (match_ill != NULL && ill != match_ill &&
4698 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4699 			continue;
4700 		}
4701 		mutex_enter(&ill->ill_lock);
4702 		for (ipif = ill->ill_ipif; ipif != NULL;
4703 		    ipif = ipif->ipif_next) {
4704 			/* Allow the ipif to be down */
4705 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4706 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4707 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4708 			    (ipif->ipif_pp_dst_addr == addr)) &&
4709 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4710 				zoneid = ipif->ipif_zoneid;
4711 				mutex_exit(&ill->ill_lock);
4712 				rw_exit(&ipst->ips_ill_g_lock);
4713 				/*
4714 				 * If ipif_zoneid was ALL_ZONES then we have
4715 				 * a trusted extensions shared IP address.
4716 				 * In that case GLOBAL_ZONEID works to send.
4717 				 */
4718 				if (zoneid == ALL_ZONES)
4719 					zoneid = GLOBAL_ZONEID;
4720 				return (zoneid);
4721 			}
4722 		}
4723 		mutex_exit(&ill->ill_lock);
4724 	}
4725 
4726 	/* If we already did the ptp case, then we are done */
4727 	if (ptp) {
4728 		rw_exit(&ipst->ips_ill_g_lock);
4729 		return (ALL_ZONES);
4730 	}
4731 	ptp = B_TRUE;
4732 	goto repeat;
4733 }
4734 
4735 /*
4736  * Look for an ipif that matches the specified remote address i.e. the
4737  * ipif that would receive the specified packet.
4738  * First look for directly connected interfaces and then do a recursive
4739  * IRE lookup and pick the first ipif corresponding to the source address in the
4740  * ire.
4741  * Returns: held ipif
4742  *
4743  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4744  */
4745 ipif_t *
4746 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4747 {
4748 	ipif_t	*ipif;
4749 
4750 	ASSERT(!ill->ill_isv6);
4751 
4752 	/*
4753 	 * Someone could be changing this ipif currently or change it
4754 	 * after we return this. Thus  a few packets could use the old
4755 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4756 	 * will atomically be updated or cleaned up with the new value
4757 	 * Thus we don't need a lock to check the flags or other attrs below.
4758 	 */
4759 	mutex_enter(&ill->ill_lock);
4760 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4761 		if (IPIF_IS_CONDEMNED(ipif))
4762 			continue;
4763 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4764 		    ipif->ipif_zoneid != ALL_ZONES)
4765 			continue;
4766 		/* Allow the ipif to be down */
4767 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4768 			if ((ipif->ipif_pp_dst_addr == addr) ||
4769 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4770 			    ipif->ipif_lcl_addr == addr)) {
4771 				ipif_refhold_locked(ipif);
4772 				mutex_exit(&ill->ill_lock);
4773 				return (ipif);
4774 			}
4775 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4776 			ipif_refhold_locked(ipif);
4777 			mutex_exit(&ill->ill_lock);
4778 			return (ipif);
4779 		}
4780 	}
4781 	mutex_exit(&ill->ill_lock);
4782 	/*
4783 	 * For a remote destination it isn't possible to nail down a particular
4784 	 * ipif.
4785 	 */
4786 
4787 	/* Pick the first interface */
4788 	ipif = ipif_get_next_ipif(NULL, ill);
4789 	return (ipif);
4790 }
4791 
4792 /*
4793  * This func does not prevent refcnt from increasing. But if
4794  * the caller has taken steps to that effect, then this func
4795  * can be used to determine whether the ill has become quiescent
4796  */
4797 static boolean_t
4798 ill_is_quiescent(ill_t *ill)
4799 {
4800 	ipif_t	*ipif;
4801 
4802 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4803 
4804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4805 		if (ipif->ipif_refcnt != 0)
4806 			return (B_FALSE);
4807 	}
4808 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4809 		return (B_FALSE);
4810 	}
4811 	return (B_TRUE);
4812 }
4813 
4814 boolean_t
4815 ill_is_freeable(ill_t *ill)
4816 {
4817 	ipif_t	*ipif;
4818 
4819 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4820 
4821 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4822 		if (ipif->ipif_refcnt != 0) {
4823 			return (B_FALSE);
4824 		}
4825 	}
4826 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4827 		return (B_FALSE);
4828 	}
4829 	return (B_TRUE);
4830 }
4831 
4832 /*
4833  * This func does not prevent refcnt from increasing. But if
4834  * the caller has taken steps to that effect, then this func
4835  * can be used to determine whether the ipif has become quiescent
4836  */
4837 static boolean_t
4838 ipif_is_quiescent(ipif_t *ipif)
4839 {
4840 	ill_t *ill;
4841 
4842 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4843 
4844 	if (ipif->ipif_refcnt != 0)
4845 		return (B_FALSE);
4846 
4847 	ill = ipif->ipif_ill;
4848 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4849 	    ill->ill_logical_down) {
4850 		return (B_TRUE);
4851 	}
4852 
4853 	/* This is the last ipif going down or being deleted on this ill */
4854 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4855 		return (B_FALSE);
4856 	}
4857 
4858 	return (B_TRUE);
4859 }
4860 
4861 /*
4862  * return true if the ipif can be destroyed: the ipif has to be quiescent
4863  * with zero references from ire/ilm to it.
4864  */
4865 static boolean_t
4866 ipif_is_freeable(ipif_t *ipif)
4867 {
4868 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4869 	ASSERT(ipif->ipif_id != 0);
4870 	return (ipif->ipif_refcnt == 0);
4871 }
4872 
4873 /*
4874  * The ipif/ill/ire has been refreled. Do the tail processing.
4875  * Determine if the ipif or ill in question has become quiescent and if so
4876  * wakeup close and/or restart any queued pending ioctl that is waiting
4877  * for the ipif_down (or ill_down)
4878  */
4879 void
4880 ipif_ill_refrele_tail(ill_t *ill)
4881 {
4882 	mblk_t	*mp;
4883 	conn_t	*connp;
4884 	ipsq_t	*ipsq;
4885 	ipxop_t	*ipx;
4886 	ipif_t	*ipif;
4887 	dl_notify_ind_t *dlindp;
4888 
4889 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4890 
4891 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4892 		/* ip_modclose() may be waiting */
4893 		cv_broadcast(&ill->ill_cv);
4894 	}
4895 
4896 	ipsq = ill->ill_phyint->phyint_ipsq;
4897 	mutex_enter(&ipsq->ipsq_lock);
4898 	ipx = ipsq->ipsq_xop;
4899 	mutex_enter(&ipx->ipx_lock);
4900 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4901 		goto unlock;
4902 
4903 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4904 
4905 	ipif = ipx->ipx_pending_ipif;
4906 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4907 		goto unlock;
4908 
4909 	switch (ipx->ipx_waitfor) {
4910 	case IPIF_DOWN:
4911 		if (!ipif_is_quiescent(ipif))
4912 			goto unlock;
4913 		break;
4914 	case IPIF_FREE:
4915 		if (!ipif_is_freeable(ipif))
4916 			goto unlock;
4917 		break;
4918 	case ILL_DOWN:
4919 		if (!ill_is_quiescent(ill))
4920 			goto unlock;
4921 		break;
4922 	case ILL_FREE:
4923 		/*
4924 		 * ILL_FREE is only for loopback; normal ill teardown waits
4925 		 * synchronously in ip_modclose() without using ipx_waitfor,
4926 		 * handled by the cv_broadcast() at the top of this function.
4927 		 */
4928 		if (!ill_is_freeable(ill))
4929 			goto unlock;
4930 		break;
4931 	default:
4932 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4933 		    (void *)ipsq, ipx->ipx_waitfor);
4934 	}
4935 
4936 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4937 	mutex_exit(&ipx->ipx_lock);
4938 	mp = ipsq_pending_mp_get(ipsq, &connp);
4939 	mutex_exit(&ipsq->ipsq_lock);
4940 	mutex_exit(&ill->ill_lock);
4941 
4942 	ASSERT(mp != NULL);
4943 	/*
4944 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4945 	 * we can only get here when the current operation decides it
4946 	 * it needs to quiesce via ipsq_pending_mp_add().
4947 	 */
4948 	switch (mp->b_datap->db_type) {
4949 	case M_PCPROTO:
4950 	case M_PROTO:
4951 		/*
4952 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4953 		 */
4954 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4955 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4956 
4957 		switch (dlindp->dl_notification) {
4958 		case DL_NOTE_PHYS_ADDR:
4959 			qwriter_ip(ill, ill->ill_rq, mp,
4960 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4961 			return;
4962 		case DL_NOTE_REPLUMB:
4963 			qwriter_ip(ill, ill->ill_rq, mp,
4964 			    ill_replumb_tail, CUR_OP, B_TRUE);
4965 			return;
4966 		default:
4967 			ASSERT(0);
4968 			ill_refrele(ill);
4969 		}
4970 		break;
4971 
4972 	case M_ERROR:
4973 	case M_HANGUP:
4974 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4975 		    B_TRUE);
4976 		return;
4977 
4978 	case M_IOCTL:
4979 	case M_IOCDATA:
4980 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4981 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4982 		return;
4983 
4984 	default:
4985 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4986 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4987 	}
4988 	return;
4989 unlock:
4990 	mutex_exit(&ipsq->ipsq_lock);
4991 	mutex_exit(&ipx->ipx_lock);
4992 	mutex_exit(&ill->ill_lock);
4993 }
4994 
4995 #ifdef DEBUG
4996 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4997 static void
4998 th_trace_rrecord(th_trace_t *th_trace)
4999 {
5000 	tr_buf_t *tr_buf;
5001 	uint_t lastref;
5002 
5003 	lastref = th_trace->th_trace_lastref;
5004 	lastref++;
5005 	if (lastref == TR_BUF_MAX)
5006 		lastref = 0;
5007 	th_trace->th_trace_lastref = lastref;
5008 	tr_buf = &th_trace->th_trbuf[lastref];
5009 	tr_buf->tr_time = ddi_get_lbolt();
5010 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5011 }
5012 
5013 static void
5014 th_trace_free(void *value)
5015 {
5016 	th_trace_t *th_trace = value;
5017 
5018 	ASSERT(th_trace->th_refcnt == 0);
5019 	kmem_free(th_trace, sizeof (*th_trace));
5020 }
5021 
5022 /*
5023  * Find or create the per-thread hash table used to track object references.
5024  * The ipst argument is NULL if we shouldn't allocate.
5025  *
5026  * Accesses per-thread data, so there's no need to lock here.
5027  */
5028 static mod_hash_t *
5029 th_trace_gethash(ip_stack_t *ipst)
5030 {
5031 	th_hash_t *thh;
5032 
5033 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5034 		mod_hash_t *mh;
5035 		char name[256];
5036 		size_t objsize, rshift;
5037 		int retv;
5038 
5039 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5040 			return (NULL);
5041 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5042 		    (void *)curthread);
5043 
5044 		/*
5045 		 * We use mod_hash_create_extended here rather than the more
5046 		 * obvious mod_hash_create_ptrhash because the latter has a
5047 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5048 		 * block.
5049 		 */
5050 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5051 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5052 		rshift = highbit(objsize);
5053 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5054 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5055 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5056 		if (mh == NULL) {
5057 			kmem_free(thh, sizeof (*thh));
5058 			return (NULL);
5059 		}
5060 		thh->thh_hash = mh;
5061 		thh->thh_ipst = ipst;
5062 		/*
5063 		 * We trace ills, ipifs, ires, and nces.  All of these are
5064 		 * per-IP-stack, so the lock on the thread list is as well.
5065 		 */
5066 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5067 		list_insert_tail(&ip_thread_list, thh);
5068 		rw_exit(&ip_thread_rwlock);
5069 		retv = tsd_set(ip_thread_data, thh);
5070 		ASSERT(retv == 0);
5071 	}
5072 	return (thh != NULL ? thh->thh_hash : NULL);
5073 }
5074 
5075 boolean_t
5076 th_trace_ref(const void *obj, ip_stack_t *ipst)
5077 {
5078 	th_trace_t *th_trace;
5079 	mod_hash_t *mh;
5080 	mod_hash_val_t val;
5081 
5082 	if ((mh = th_trace_gethash(ipst)) == NULL)
5083 		return (B_FALSE);
5084 
5085 	/*
5086 	 * Attempt to locate the trace buffer for this obj and thread.
5087 	 * If it does not exist, then allocate a new trace buffer and
5088 	 * insert into the hash.
5089 	 */
5090 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5091 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5092 		if (th_trace == NULL)
5093 			return (B_FALSE);
5094 
5095 		th_trace->th_id = curthread;
5096 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5097 		    (mod_hash_val_t)th_trace) != 0) {
5098 			kmem_free(th_trace, sizeof (th_trace_t));
5099 			return (B_FALSE);
5100 		}
5101 	} else {
5102 		th_trace = (th_trace_t *)val;
5103 	}
5104 
5105 	ASSERT(th_trace->th_refcnt >= 0 &&
5106 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5107 
5108 	th_trace->th_refcnt++;
5109 	th_trace_rrecord(th_trace);
5110 	return (B_TRUE);
5111 }
5112 
5113 /*
5114  * For the purpose of tracing a reference release, we assume that global
5115  * tracing is always on and that the same thread initiated the reference hold
5116  * is releasing.
5117  */
5118 void
5119 th_trace_unref(const void *obj)
5120 {
5121 	int retv;
5122 	mod_hash_t *mh;
5123 	th_trace_t *th_trace;
5124 	mod_hash_val_t val;
5125 
5126 	mh = th_trace_gethash(NULL);
5127 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5128 	ASSERT(retv == 0);
5129 	th_trace = (th_trace_t *)val;
5130 
5131 	ASSERT(th_trace->th_refcnt > 0);
5132 	th_trace->th_refcnt--;
5133 	th_trace_rrecord(th_trace);
5134 }
5135 
5136 /*
5137  * If tracing has been disabled, then we assume that the reference counts are
5138  * now useless, and we clear them out before destroying the entries.
5139  */
5140 void
5141 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5142 {
5143 	th_hash_t	*thh;
5144 	mod_hash_t	*mh;
5145 	mod_hash_val_t	val;
5146 	th_trace_t	*th_trace;
5147 	int		retv;
5148 
5149 	rw_enter(&ip_thread_rwlock, RW_READER);
5150 	for (thh = list_head(&ip_thread_list); thh != NULL;
5151 	    thh = list_next(&ip_thread_list, thh)) {
5152 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5153 		    &val) == 0) {
5154 			th_trace = (th_trace_t *)val;
5155 			if (trace_disable)
5156 				th_trace->th_refcnt = 0;
5157 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5158 			ASSERT(retv == 0);
5159 		}
5160 	}
5161 	rw_exit(&ip_thread_rwlock);
5162 }
5163 
5164 void
5165 ipif_trace_ref(ipif_t *ipif)
5166 {
5167 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5168 
5169 	if (ipif->ipif_trace_disable)
5170 		return;
5171 
5172 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5173 		ipif->ipif_trace_disable = B_TRUE;
5174 		ipif_trace_cleanup(ipif);
5175 	}
5176 }
5177 
5178 void
5179 ipif_untrace_ref(ipif_t *ipif)
5180 {
5181 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5182 
5183 	if (!ipif->ipif_trace_disable)
5184 		th_trace_unref(ipif);
5185 }
5186 
5187 void
5188 ill_trace_ref(ill_t *ill)
5189 {
5190 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5191 
5192 	if (ill->ill_trace_disable)
5193 		return;
5194 
5195 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5196 		ill->ill_trace_disable = B_TRUE;
5197 		ill_trace_cleanup(ill);
5198 	}
5199 }
5200 
5201 void
5202 ill_untrace_ref(ill_t *ill)
5203 {
5204 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5205 
5206 	if (!ill->ill_trace_disable)
5207 		th_trace_unref(ill);
5208 }
5209 
5210 /*
5211  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5212  * failure, ipif_trace_disable is set.
5213  */
5214 static void
5215 ipif_trace_cleanup(const ipif_t *ipif)
5216 {
5217 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5218 }
5219 
5220 /*
5221  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5222  * failure, ill_trace_disable is set.
5223  */
5224 static void
5225 ill_trace_cleanup(const ill_t *ill)
5226 {
5227 	th_trace_cleanup(ill, ill->ill_trace_disable);
5228 }
5229 #endif /* DEBUG */
5230 
5231 void
5232 ipif_refhold_locked(ipif_t *ipif)
5233 {
5234 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5235 	ipif->ipif_refcnt++;
5236 	IPIF_TRACE_REF(ipif);
5237 }
5238 
5239 void
5240 ipif_refhold(ipif_t *ipif)
5241 {
5242 	ill_t	*ill;
5243 
5244 	ill = ipif->ipif_ill;
5245 	mutex_enter(&ill->ill_lock);
5246 	ipif->ipif_refcnt++;
5247 	IPIF_TRACE_REF(ipif);
5248 	mutex_exit(&ill->ill_lock);
5249 }
5250 
5251 /*
5252  * Must not be called while holding any locks. Otherwise if this is
5253  * the last reference to be released there is a chance of recursive mutex
5254  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5255  * to restart an ioctl.
5256  */
5257 void
5258 ipif_refrele(ipif_t *ipif)
5259 {
5260 	ill_t	*ill;
5261 
5262 	ill = ipif->ipif_ill;
5263 
5264 	mutex_enter(&ill->ill_lock);
5265 	ASSERT(ipif->ipif_refcnt != 0);
5266 	ipif->ipif_refcnt--;
5267 	IPIF_UNTRACE_REF(ipif);
5268 	if (ipif->ipif_refcnt != 0) {
5269 		mutex_exit(&ill->ill_lock);
5270 		return;
5271 	}
5272 
5273 	/* Drops the ill_lock */
5274 	ipif_ill_refrele_tail(ill);
5275 }
5276 
5277 ipif_t *
5278 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5279 {
5280 	ipif_t	*ipif;
5281 
5282 	mutex_enter(&ill->ill_lock);
5283 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5284 	    ipif != NULL; ipif = ipif->ipif_next) {
5285 		if (IPIF_IS_CONDEMNED(ipif))
5286 			continue;
5287 		ipif_refhold_locked(ipif);
5288 		mutex_exit(&ill->ill_lock);
5289 		return (ipif);
5290 	}
5291 	mutex_exit(&ill->ill_lock);
5292 	return (NULL);
5293 }
5294 
5295 /*
5296  * TODO: make this table extendible at run time
5297  * Return a pointer to the mac type info for 'mac_type'
5298  */
5299 static ip_m_t *
5300 ip_m_lookup(t_uscalar_t mac_type)
5301 {
5302 	ip_m_t	*ipm;
5303 
5304 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5305 		if (ipm->ip_m_mac_type == mac_type)
5306 			return (ipm);
5307 	return (NULL);
5308 }
5309 
5310 /*
5311  * Make a link layer address from the multicast IP address *addr.
5312  * To form the link layer address, invoke the ip_m_v*mapping function
5313  * associated with the link-layer type.
5314  */
5315 void
5316 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5317 {
5318 	ip_m_t *ipm;
5319 
5320 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5321 		return;
5322 
5323 	ASSERT(addr != NULL);
5324 
5325 	ipm = ip_m_lookup(ill->ill_mactype);
5326 	if (ipm == NULL ||
5327 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5328 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5329 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5330 		    ill->ill_name, ill->ill_mactype));
5331 		return;
5332 	}
5333 	if (ill->ill_isv6)
5334 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5335 	else
5336 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5337 }
5338 
5339 /*
5340  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5341  * Otherwise returns B_TRUE.
5342  *
5343  * The netmask can be verified to be contiguous with 32 shifts and or
5344  * operations. Take the contiguous mask (in host byte order) and compute
5345  * 	mask | mask << 1 | mask << 2 | ... | mask << 31
5346  * the result will be the same as the 'mask' for contiguous mask.
5347  */
5348 static boolean_t
5349 ip_contiguous_mask(uint32_t mask)
5350 {
5351 	uint32_t	m = mask;
5352 	int		i;
5353 
5354 	for (i = 1; i < 32; i++)
5355 		m |= (mask << i);
5356 
5357 	return (m == mask);
5358 }
5359 
5360 /*
5361  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5362  * ill is passed in to associate it with the correct interface.
5363  * If ire_arg is set, then we return the held IRE in that location.
5364  */
5365 int
5366 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5367     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5368     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5369 {
5370 	ire_t	*ire, *nire;
5371 	ire_t	*gw_ire = NULL;
5372 	ipif_t	*ipif = NULL;
5373 	uint_t	type;
5374 	int	match_flags = MATCH_IRE_TYPE;
5375 	tsol_gc_t *gc = NULL;
5376 	tsol_gcgrp_t *gcgrp = NULL;
5377 	boolean_t gcgrp_xtraref = B_FALSE;
5378 	boolean_t cgtp_broadcast;
5379 	boolean_t unbound = B_FALSE;
5380 
5381 	ip1dbg(("ip_rt_add:"));
5382 
5383 	if (ire_arg != NULL)
5384 		*ire_arg = NULL;
5385 
5386 	/* disallow non-contiguous netmasks */
5387 	if (!ip_contiguous_mask(ntohl(mask)))
5388 		return (ENOTSUP);
5389 
5390 	/*
5391 	 * If this is the case of RTF_HOST being set, then we set the netmask
5392 	 * to all ones (regardless if one was supplied).
5393 	 */
5394 	if (flags & RTF_HOST)
5395 		mask = IP_HOST_MASK;
5396 
5397 	/*
5398 	 * Prevent routes with a zero gateway from being created (since
5399 	 * interfaces can currently be plumbed and brought up no assigned
5400 	 * address).
5401 	 */
5402 	if (gw_addr == 0)
5403 		return (ENETUNREACH);
5404 	/*
5405 	 * Get the ipif, if any, corresponding to the gw_addr
5406 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5407 	 * we match on the gatway and destination to handle unnumbered pt-pt
5408 	 * interfaces.
5409 	 */
5410 	if (ill != NULL)
5411 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5412 	else
5413 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5414 	if (ipif != NULL) {
5415 		if (IS_VNI(ipif->ipif_ill)) {
5416 			ipif_refrele(ipif);
5417 			return (EINVAL);
5418 		}
5419 	}
5420 
5421 	/*
5422 	 * GateD will attempt to create routes with a loopback interface
5423 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5424 	 * these routes to be added, but create them as interface routes
5425 	 * since the gateway is an interface address.
5426 	 */
5427 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5428 		flags &= ~RTF_GATEWAY;
5429 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5430 		    mask == IP_HOST_MASK) {
5431 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5432 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5433 			    NULL);
5434 			if (ire != NULL) {
5435 				ire_refrele(ire);
5436 				ipif_refrele(ipif);
5437 				return (EEXIST);
5438 			}
5439 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5440 			    "for 0x%x\n", (void *)ipif,
5441 			    ipif->ipif_ire_type,
5442 			    ntohl(ipif->ipif_lcl_addr)));
5443 			ire = ire_create(
5444 			    (uchar_t *)&dst_addr,	/* dest address */
5445 			    (uchar_t *)&mask,		/* mask */
5446 			    NULL,			/* no gateway */
5447 			    ipif->ipif_ire_type,	/* LOOPBACK */
5448 			    ipif->ipif_ill,
5449 			    zoneid,
5450 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5451 			    NULL,
5452 			    ipst);
5453 
5454 			if (ire == NULL) {
5455 				ipif_refrele(ipif);
5456 				return (ENOMEM);
5457 			}
5458 			/* src address assigned by the caller? */
5459 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5460 				ire->ire_setsrc_addr = src_addr;
5461 
5462 			nire = ire_add(ire);
5463 			if (nire == NULL) {
5464 				/*
5465 				 * In the result of failure, ire_add() will have
5466 				 * already deleted the ire in question, so there
5467 				 * is no need to do that here.
5468 				 */
5469 				ipif_refrele(ipif);
5470 				return (ENOMEM);
5471 			}
5472 			/*
5473 			 * Check if it was a duplicate entry. This handles
5474 			 * the case of two racing route adds for the same route
5475 			 */
5476 			if (nire != ire) {
5477 				ASSERT(nire->ire_identical_ref > 1);
5478 				ire_delete(nire);
5479 				ire_refrele(nire);
5480 				ipif_refrele(ipif);
5481 				return (EEXIST);
5482 			}
5483 			ire = nire;
5484 			goto save_ire;
5485 		}
5486 	}
5487 
5488 	/*
5489 	 * The routes for multicast with CGTP are quite special in that
5490 	 * the gateway is the local interface address, yet RTF_GATEWAY
5491 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5492 	 * this undocumented and unusual use of multicast routes.
5493 	 */
5494 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5495 		flags &= ~RTF_GATEWAY;
5496 
5497 	/*
5498 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5499 	 * and the gateway address provided is one of the system's interface
5500 	 * addresses.  By using the routing socket interface and supplying an
5501 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5502 	 * specifying an interface route to be created is available which uses
5503 	 * the interface index that specifies the outgoing interface rather than
5504 	 * the address of an outgoing interface (which may not be able to
5505 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5506 	 * flag, routes can be specified which not only specify the next-hop to
5507 	 * be used when routing to a certain prefix, but also which outgoing
5508 	 * interface should be used.
5509 	 *
5510 	 * Previously, interfaces would have unique addresses assigned to them
5511 	 * and so the address assigned to a particular interface could be used
5512 	 * to identify a particular interface.  One exception to this was the
5513 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5514 	 *
5515 	 * With the advent of IPv6 and its link-local addresses, this
5516 	 * restriction was relaxed and interfaces could share addresses between
5517 	 * themselves.  In fact, typically all of the link-local interfaces on
5518 	 * an IPv6 node or router will have the same link-local address.  In
5519 	 * order to differentiate between these interfaces, the use of an
5520 	 * interface index is necessary and this index can be carried inside a
5521 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5522 	 * of using the interface index, however, is that all of the ipif's that
5523 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5524 	 * cannot be used to differentiate between ipif's (or logical
5525 	 * interfaces) that belong to the same ill (physical interface).
5526 	 *
5527 	 * For example, in the following case involving IPv4 interfaces and
5528 	 * logical interfaces
5529 	 *
5530 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5531 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5532 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5533 	 *
5534 	 * the ipif's corresponding to each of these interface routes can be
5535 	 * uniquely identified by the "gateway" (actually interface address).
5536 	 *
5537 	 * In this case involving multiple IPv6 default routes to a particular
5538 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5539 	 * default route is of interest:
5540 	 *
5541 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5542 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5543 	 */
5544 
5545 	/* RTF_GATEWAY not set */
5546 	if (!(flags & RTF_GATEWAY)) {
5547 		if (sp != NULL) {
5548 			ip2dbg(("ip_rt_add: gateway security attributes "
5549 			    "cannot be set with interface route\n"));
5550 			if (ipif != NULL)
5551 				ipif_refrele(ipif);
5552 			return (EINVAL);
5553 		}
5554 
5555 		/*
5556 		 * Whether or not ill (RTA_IFP) is set, we require that
5557 		 * the gateway is one of our local addresses.
5558 		 */
5559 		if (ipif == NULL)
5560 			return (ENETUNREACH);
5561 
5562 		/*
5563 		 * We use MATCH_IRE_ILL here. If the caller specified an
5564 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5565 		 * we use the ill derived from the gateway address.
5566 		 * We can always match the gateway address since we record it
5567 		 * in ire_gateway_addr.
5568 		 * We don't allow RTA_IFP to specify a different ill than the
5569 		 * one matching the ipif to make sure we can delete the route.
5570 		 */
5571 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5572 		if (ill == NULL) {
5573 			ill = ipif->ipif_ill;
5574 		} else if (ill != ipif->ipif_ill) {
5575 			ipif_refrele(ipif);
5576 			return (EINVAL);
5577 		}
5578 
5579 		/*
5580 		 * We check for an existing entry at this point.
5581 		 *
5582 		 * Since a netmask isn't passed in via the ioctl interface
5583 		 * (SIOCADDRT), we don't check for a matching netmask in that
5584 		 * case.
5585 		 */
5586 		if (!ioctl_msg)
5587 			match_flags |= MATCH_IRE_MASK;
5588 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5589 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5590 		    NULL);
5591 		if (ire != NULL) {
5592 			ire_refrele(ire);
5593 			ipif_refrele(ipif);
5594 			return (EEXIST);
5595 		}
5596 
5597 		/*
5598 		 * Some software (for example, GateD and Sun Cluster) attempts
5599 		 * to create (what amount to) IRE_PREFIX routes with the
5600 		 * loopback address as the gateway.  This is primarily done to
5601 		 * set up prefixes with the RTF_REJECT flag set (for example,
5602 		 * when generating aggregate routes.)
5603 		 *
5604 		 * If the IRE type (as defined by ill->ill_net_type) would be
5605 		 * IRE_LOOPBACK, then we map the request into a
5606 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5607 		 * these interface routes, by definition, can only be that.
5608 		 *
5609 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5610 		 * routine, but rather using ire_create() directly.
5611 		 *
5612 		 */
5613 		type = ill->ill_net_type;
5614 		if (type == IRE_LOOPBACK) {
5615 			type = IRE_IF_NORESOLVER;
5616 			flags |= RTF_BLACKHOLE;
5617 		}
5618 
5619 		/*
5620 		 * Create a copy of the IRE_IF_NORESOLVER or
5621 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5622 		 * gateway.
5623 		 */
5624 		ire = ire_create(
5625 		    (uchar_t *)&dst_addr,
5626 		    (uint8_t *)&mask,
5627 		    (uint8_t *)&gw_addr,
5628 		    type,
5629 		    ill,
5630 		    zoneid,
5631 		    flags,
5632 		    NULL,
5633 		    ipst);
5634 		if (ire == NULL) {
5635 			ipif_refrele(ipif);
5636 			return (ENOMEM);
5637 		}
5638 
5639 		/* src address assigned by the caller? */
5640 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5641 			ire->ire_setsrc_addr = src_addr;
5642 
5643 		nire = ire_add(ire);
5644 		if (nire == NULL) {
5645 			/*
5646 			 * In the result of failure, ire_add() will have
5647 			 * already deleted the ire in question, so there
5648 			 * is no need to do that here.
5649 			 */
5650 			ipif_refrele(ipif);
5651 			return (ENOMEM);
5652 		}
5653 		/*
5654 		 * Check if it was a duplicate entry. This handles
5655 		 * the case of two racing route adds for the same route
5656 		 */
5657 		if (nire != ire) {
5658 			ire_delete(nire);
5659 			ire_refrele(nire);
5660 			ipif_refrele(ipif);
5661 			return (EEXIST);
5662 		}
5663 		ire = nire;
5664 		goto save_ire;
5665 	}
5666 
5667 	/*
5668 	 * Get an interface IRE for the specified gateway.
5669 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5670 	 * gateway, it is currently unreachable and we fail the request
5671 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5672 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5673 	 * If RTA_IFP was specified we look on that particular ill.
5674 	 */
5675 	if (ill != NULL)
5676 		match_flags |= MATCH_IRE_ILL;
5677 
5678 	/* Check whether the gateway is reachable. */
5679 again:
5680 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5681 	if (flags & RTF_INDIRECT)
5682 		type |= IRE_OFFLINK;
5683 
5684 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5685 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5686 	if (gw_ire == NULL) {
5687 		/*
5688 		 * With IPMP, we allow host routes to influence in.mpathd's
5689 		 * target selection.  However, if the test addresses are on
5690 		 * their own network, the above lookup will fail since the
5691 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5692 		 * hidden test IREs to be found and try again.
5693 		 */
5694 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5695 			match_flags |= MATCH_IRE_TESTHIDDEN;
5696 			goto again;
5697 		}
5698 		if (ipif != NULL)
5699 			ipif_refrele(ipif);
5700 		return (ENETUNREACH);
5701 	}
5702 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5703 		ire_refrele(gw_ire);
5704 		if (ipif != NULL)
5705 			ipif_refrele(ipif);
5706 		return (ENETUNREACH);
5707 	}
5708 
5709 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5710 		unbound = B_TRUE;
5711 		if (ipst->ips_ip_strict_src_multihoming > 0)
5712 			ill = gw_ire->ire_ill;
5713 	}
5714 
5715 	/*
5716 	 * We create one of three types of IREs as a result of this request
5717 	 * based on the netmask.  A netmask of all ones (which is automatically
5718 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5719 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5720 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5721 	 * destination prefix.
5722 	 */
5723 	if (mask == IP_HOST_MASK)
5724 		type = IRE_HOST;
5725 	else if (mask == 0)
5726 		type = IRE_DEFAULT;
5727 	else
5728 		type = IRE_PREFIX;
5729 
5730 	/* check for a duplicate entry */
5731 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5732 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5733 	    0, ipst, NULL);
5734 	if (ire != NULL) {
5735 		if (ipif != NULL)
5736 			ipif_refrele(ipif);
5737 		ire_refrele(gw_ire);
5738 		ire_refrele(ire);
5739 		return (EEXIST);
5740 	}
5741 
5742 	/* Security attribute exists */
5743 	if (sp != NULL) {
5744 		tsol_gcgrp_addr_t ga;
5745 
5746 		/* find or create the gateway credentials group */
5747 		ga.ga_af = AF_INET;
5748 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5749 
5750 		/* we hold reference to it upon success */
5751 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5752 		if (gcgrp == NULL) {
5753 			if (ipif != NULL)
5754 				ipif_refrele(ipif);
5755 			ire_refrele(gw_ire);
5756 			return (ENOMEM);
5757 		}
5758 
5759 		/*
5760 		 * Create and add the security attribute to the group; a
5761 		 * reference to the group is made upon allocating a new
5762 		 * entry successfully.  If it finds an already-existing
5763 		 * entry for the security attribute in the group, it simply
5764 		 * returns it and no new reference is made to the group.
5765 		 */
5766 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5767 		if (gc == NULL) {
5768 			if (ipif != NULL)
5769 				ipif_refrele(ipif);
5770 			/* release reference held by gcgrp_lookup */
5771 			GCGRP_REFRELE(gcgrp);
5772 			ire_refrele(gw_ire);
5773 			return (ENOMEM);
5774 		}
5775 	}
5776 
5777 	/* Create the IRE. */
5778 	ire = ire_create(
5779 	    (uchar_t *)&dst_addr,		/* dest address */
5780 	    (uchar_t *)&mask,			/* mask */
5781 	    (uchar_t *)&gw_addr,		/* gateway address */
5782 	    (ushort_t)type,			/* IRE type */
5783 	    ill,
5784 	    zoneid,
5785 	    flags,
5786 	    gc,					/* security attribute */
5787 	    ipst);
5788 
5789 	/*
5790 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5791 	 * reference to the 'gcgrp'. We can now release the extra reference
5792 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5793 	 */
5794 	if (gcgrp_xtraref)
5795 		GCGRP_REFRELE(gcgrp);
5796 	if (ire == NULL) {
5797 		if (gc != NULL)
5798 			GC_REFRELE(gc);
5799 		if (ipif != NULL)
5800 			ipif_refrele(ipif);
5801 		ire_refrele(gw_ire);
5802 		return (ENOMEM);
5803 	}
5804 
5805 	/* Before we add, check if an extra CGTP broadcast is needed */
5806 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5807 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5808 
5809 	/* src address assigned by the caller? */
5810 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5811 		ire->ire_setsrc_addr = src_addr;
5812 
5813 	ire->ire_unbound = unbound;
5814 
5815 	/*
5816 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5817 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5818 	 */
5819 
5820 	/* Add the new IRE. */
5821 	nire = ire_add(ire);
5822 	if (nire == NULL) {
5823 		/*
5824 		 * In the result of failure, ire_add() will have
5825 		 * already deleted the ire in question, so there
5826 		 * is no need to do that here.
5827 		 */
5828 		if (ipif != NULL)
5829 			ipif_refrele(ipif);
5830 		ire_refrele(gw_ire);
5831 		return (ENOMEM);
5832 	}
5833 	/*
5834 	 * Check if it was a duplicate entry. This handles
5835 	 * the case of two racing route adds for the same route
5836 	 */
5837 	if (nire != ire) {
5838 		ire_delete(nire);
5839 		ire_refrele(nire);
5840 		if (ipif != NULL)
5841 			ipif_refrele(ipif);
5842 		ire_refrele(gw_ire);
5843 		return (EEXIST);
5844 	}
5845 	ire = nire;
5846 
5847 	if (flags & RTF_MULTIRT) {
5848 		/*
5849 		 * Invoke the CGTP (multirouting) filtering module
5850 		 * to add the dst address in the filtering database.
5851 		 * Replicated inbound packets coming from that address
5852 		 * will be filtered to discard the duplicates.
5853 		 * It is not necessary to call the CGTP filter hook
5854 		 * when the dst address is a broadcast or multicast,
5855 		 * because an IP source address cannot be a broadcast
5856 		 * or a multicast.
5857 		 */
5858 		if (cgtp_broadcast) {
5859 			ip_cgtp_bcast_add(ire, ipst);
5860 			goto save_ire;
5861 		}
5862 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5863 		    !CLASSD(ire->ire_addr)) {
5864 			int res;
5865 			ipif_t *src_ipif;
5866 
5867 			/* Find the source address corresponding to gw_ire */
5868 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5869 			    NULL, zoneid, ipst);
5870 			if (src_ipif != NULL) {
5871 				res = ipst->ips_ip_cgtp_filter_ops->
5872 				    cfo_add_dest_v4(
5873 				    ipst->ips_netstack->netstack_stackid,
5874 				    ire->ire_addr,
5875 				    ire->ire_gateway_addr,
5876 				    ire->ire_setsrc_addr,
5877 				    src_ipif->ipif_lcl_addr);
5878 				ipif_refrele(src_ipif);
5879 			} else {
5880 				res = EADDRNOTAVAIL;
5881 			}
5882 			if (res != 0) {
5883 				if (ipif != NULL)
5884 					ipif_refrele(ipif);
5885 				ire_refrele(gw_ire);
5886 				ire_delete(ire);
5887 				ire_refrele(ire);	/* Held in ire_add */
5888 				return (res);
5889 			}
5890 		}
5891 	}
5892 
5893 save_ire:
5894 	if (gw_ire != NULL) {
5895 		ire_refrele(gw_ire);
5896 		gw_ire = NULL;
5897 	}
5898 	if (ill != NULL) {
5899 		/*
5900 		 * Save enough information so that we can recreate the IRE if
5901 		 * the interface goes down and then up.  The metrics associated
5902 		 * with the route will be saved as well when rts_setmetrics() is
5903 		 * called after the IRE has been created.  In the case where
5904 		 * memory cannot be allocated, none of this information will be
5905 		 * saved.
5906 		 */
5907 		ill_save_ire(ill, ire);
5908 	}
5909 	if (ioctl_msg)
5910 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5911 	if (ire_arg != NULL) {
5912 		/*
5913 		 * Store the ire that was successfully added into where ire_arg
5914 		 * points to so that callers don't have to look it up
5915 		 * themselves (but they are responsible for ire_refrele()ing
5916 		 * the ire when they are finished with it).
5917 		 */
5918 		*ire_arg = ire;
5919 	} else {
5920 		ire_refrele(ire);		/* Held in ire_add */
5921 	}
5922 	if (ipif != NULL)
5923 		ipif_refrele(ipif);
5924 	return (0);
5925 }
5926 
5927 /*
5928  * ip_rt_delete is called to delete an IPv4 route.
5929  * ill is passed in to associate it with the correct interface.
5930  */
5931 /* ARGSUSED4 */
5932 int
5933 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5934     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5935     ip_stack_t *ipst, zoneid_t zoneid)
5936 {
5937 	ire_t	*ire = NULL;
5938 	ipif_t	*ipif;
5939 	uint_t	type;
5940 	uint_t	match_flags = MATCH_IRE_TYPE;
5941 	int	err = 0;
5942 
5943 	ip1dbg(("ip_rt_delete:"));
5944 	/*
5945 	 * If this is the case of RTF_HOST being set, then we set the netmask
5946 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5947 	 */
5948 	if (flags & RTF_HOST) {
5949 		mask = IP_HOST_MASK;
5950 		match_flags |= MATCH_IRE_MASK;
5951 	} else if (rtm_addrs & RTA_NETMASK) {
5952 		match_flags |= MATCH_IRE_MASK;
5953 	}
5954 
5955 	/*
5956 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5957 	 * we check if the gateway address is one of our interfaces first,
5958 	 * and fall back on RTF_GATEWAY routes.
5959 	 *
5960 	 * This makes it possible to delete an original
5961 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5962 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5963 	 * and those can not be deleted here.
5964 	 *
5965 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5966 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5967 	 * otherwise we use the ill derived from the gateway address.
5968 	 * We can always match the gateway address since we record it
5969 	 * in ire_gateway_addr.
5970 	 *
5971 	 * For more detail on specifying routes by gateway address and by
5972 	 * interface index, see the comments in ip_rt_add().
5973 	 */
5974 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5975 	if (ipif != NULL) {
5976 		ill_t	*ill_match;
5977 
5978 		if (ill != NULL)
5979 			ill_match = ill;
5980 		else
5981 			ill_match = ipif->ipif_ill;
5982 
5983 		match_flags |= MATCH_IRE_ILL;
5984 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5985 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
5986 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
5987 			    match_flags, 0, ipst, NULL);
5988 		}
5989 		if (ire == NULL) {
5990 			match_flags |= MATCH_IRE_GW;
5991 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5992 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5993 			    match_flags, 0, ipst, NULL);
5994 		}
5995 		/* Avoid deleting routes created by kernel from an ipif */
5996 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5997 			ire_refrele(ire);
5998 			ire = NULL;
5999 		}
6000 
6001 		/* Restore in case we didn't find a match */
6002 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6003 	}
6004 
6005 	if (ire == NULL) {
6006 		/*
6007 		 * At this point, the gateway address is not one of our own
6008 		 * addresses or a matching interface route was not found.  We
6009 		 * set the IRE type to lookup based on whether
6010 		 * this is a host route, a default route or just a prefix.
6011 		 *
6012 		 * If an ill was passed in, then the lookup is based on an
6013 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6014 		 */
6015 		match_flags |= MATCH_IRE_GW;
6016 		if (ill != NULL)
6017 			match_flags |= MATCH_IRE_ILL;
6018 		if (mask == IP_HOST_MASK)
6019 			type = IRE_HOST;
6020 		else if (mask == 0)
6021 			type = IRE_DEFAULT;
6022 		else
6023 			type = IRE_PREFIX;
6024 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6025 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6026 	}
6027 
6028 	if (ipif != NULL) {
6029 		ipif_refrele(ipif);
6030 		ipif = NULL;
6031 	}
6032 
6033 	if (ire == NULL)
6034 		return (ESRCH);
6035 
6036 	if (ire->ire_flags & RTF_MULTIRT) {
6037 		/*
6038 		 * Invoke the CGTP (multirouting) filtering module
6039 		 * to remove the dst address from the filtering database.
6040 		 * Packets coming from that address will no longer be
6041 		 * filtered to remove duplicates.
6042 		 */
6043 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6044 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6045 			    ipst->ips_netstack->netstack_stackid,
6046 			    ire->ire_addr, ire->ire_gateway_addr);
6047 		}
6048 		ip_cgtp_bcast_delete(ire, ipst);
6049 	}
6050 
6051 	ill = ire->ire_ill;
6052 	if (ill != NULL)
6053 		ill_remove_saved_ire(ill, ire);
6054 	if (ioctl_msg)
6055 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6056 	ire_delete(ire);
6057 	ire_refrele(ire);
6058 	return (err);
6059 }
6060 
6061 /*
6062  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6063  */
6064 /* ARGSUSED */
6065 int
6066 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6067     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6068 {
6069 	ipaddr_t dst_addr;
6070 	ipaddr_t gw_addr;
6071 	ipaddr_t mask;
6072 	int error = 0;
6073 	mblk_t *mp1;
6074 	struct rtentry *rt;
6075 	ipif_t *ipif = NULL;
6076 	ip_stack_t	*ipst;
6077 
6078 	ASSERT(q->q_next == NULL);
6079 	ipst = CONNQ_TO_IPST(q);
6080 
6081 	ip1dbg(("ip_siocaddrt:"));
6082 	/* Existence of mp1 verified in ip_wput_nondata */
6083 	mp1 = mp->b_cont->b_cont;
6084 	rt = (struct rtentry *)mp1->b_rptr;
6085 
6086 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6087 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6088 
6089 	/*
6090 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6091 	 * to a particular host address.  In this case, we set the netmask to
6092 	 * all ones for the particular destination address.  Otherwise,
6093 	 * determine the netmask to be used based on dst_addr and the interfaces
6094 	 * in use.
6095 	 */
6096 	if (rt->rt_flags & RTF_HOST) {
6097 		mask = IP_HOST_MASK;
6098 	} else {
6099 		/*
6100 		 * Note that ip_subnet_mask returns a zero mask in the case of
6101 		 * default (an all-zeroes address).
6102 		 */
6103 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6104 	}
6105 
6106 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6107 	    B_TRUE, NULL, ipst, ALL_ZONES);
6108 	if (ipif != NULL)
6109 		ipif_refrele(ipif);
6110 	return (error);
6111 }
6112 
6113 /*
6114  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6115  */
6116 /* ARGSUSED */
6117 int
6118 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6119     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6120 {
6121 	ipaddr_t dst_addr;
6122 	ipaddr_t gw_addr;
6123 	ipaddr_t mask;
6124 	int error;
6125 	mblk_t *mp1;
6126 	struct rtentry *rt;
6127 	ipif_t *ipif = NULL;
6128 	ip_stack_t	*ipst;
6129 
6130 	ASSERT(q->q_next == NULL);
6131 	ipst = CONNQ_TO_IPST(q);
6132 
6133 	ip1dbg(("ip_siocdelrt:"));
6134 	/* Existence of mp1 verified in ip_wput_nondata */
6135 	mp1 = mp->b_cont->b_cont;
6136 	rt = (struct rtentry *)mp1->b_rptr;
6137 
6138 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6139 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6140 
6141 	/*
6142 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6143 	 * to a particular host address.  In this case, we set the netmask to
6144 	 * all ones for the particular destination address.  Otherwise,
6145 	 * determine the netmask to be used based on dst_addr and the interfaces
6146 	 * in use.
6147 	 */
6148 	if (rt->rt_flags & RTF_HOST) {
6149 		mask = IP_HOST_MASK;
6150 	} else {
6151 		/*
6152 		 * Note that ip_subnet_mask returns a zero mask in the case of
6153 		 * default (an all-zeroes address).
6154 		 */
6155 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6156 	}
6157 
6158 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6159 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6160 	    ipst, ALL_ZONES);
6161 	if (ipif != NULL)
6162 		ipif_refrele(ipif);
6163 	return (error);
6164 }
6165 
6166 /*
6167  * Enqueue the mp onto the ipsq, chained by b_next.
6168  * b_prev stores the function to be executed later, and b_queue the queue
6169  * where this mp originated.
6170  */
6171 void
6172 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6173     ill_t *pending_ill)
6174 {
6175 	conn_t	*connp;
6176 	ipxop_t *ipx = ipsq->ipsq_xop;
6177 
6178 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6179 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6180 	ASSERT(func != NULL);
6181 
6182 	mp->b_queue = q;
6183 	mp->b_prev = (void *)func;
6184 	mp->b_next = NULL;
6185 
6186 	switch (type) {
6187 	case CUR_OP:
6188 		if (ipx->ipx_mptail != NULL) {
6189 			ASSERT(ipx->ipx_mphead != NULL);
6190 			ipx->ipx_mptail->b_next = mp;
6191 		} else {
6192 			ASSERT(ipx->ipx_mphead == NULL);
6193 			ipx->ipx_mphead = mp;
6194 		}
6195 		ipx->ipx_mptail = mp;
6196 		break;
6197 
6198 	case NEW_OP:
6199 		if (ipsq->ipsq_xopq_mptail != NULL) {
6200 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6201 			ipsq->ipsq_xopq_mptail->b_next = mp;
6202 		} else {
6203 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6204 			ipsq->ipsq_xopq_mphead = mp;
6205 		}
6206 		ipsq->ipsq_xopq_mptail = mp;
6207 		ipx->ipx_ipsq_queued = B_TRUE;
6208 		break;
6209 
6210 	case SWITCH_OP:
6211 		ASSERT(ipsq->ipsq_swxop != NULL);
6212 		/* only one switch operation is currently allowed */
6213 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6214 		ipsq->ipsq_switch_mp = mp;
6215 		ipx->ipx_ipsq_queued = B_TRUE;
6216 		break;
6217 	default:
6218 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6219 	}
6220 
6221 	if (CONN_Q(q) && pending_ill != NULL) {
6222 		connp = Q_TO_CONN(q);
6223 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6224 		connp->conn_oper_pending_ill = pending_ill;
6225 	}
6226 }
6227 
6228 /*
6229  * Dequeue the next message that requested exclusive access to this IPSQ's
6230  * xop.  Specifically:
6231  *
6232  *  1. If we're still processing the current operation on `ipsq', then
6233  *     dequeue the next message for the operation (from ipx_mphead), or
6234  *     return NULL if there are no queued messages for the operation.
6235  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6236  *
6237  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6238  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6239  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6240  *     leaving IPMP groups and require a careful dance -- see the comments
6241  *     in-line below for details.  If we're leaving a group xop or if we're
6242  *     joining a group xop and become writer on it, then we proceed to (3).
6243  *     Otherwise, we return NULL and exit the xop.
6244  *
6245  *  3. For each IPSQ in the xop, return any switch operation stored on
6246  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6247  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6248  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6249  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6250  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6251  *     each phyint in the group, including the IPMP meta-interface phyint.
6252  */
6253 static mblk_t *
6254 ipsq_dq(ipsq_t *ipsq)
6255 {
6256 	ill_t	*illv4, *illv6;
6257 	mblk_t	*mp;
6258 	ipsq_t	*xopipsq;
6259 	ipsq_t	*leftipsq = NULL;
6260 	ipxop_t *ipx;
6261 	phyint_t *phyi = ipsq->ipsq_phyint;
6262 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6263 	boolean_t emptied = B_FALSE;
6264 
6265 	/*
6266 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6267 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6268 	 */
6269 	rw_enter(&ipst->ips_ill_g_lock,
6270 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6271 	mutex_enter(&ipsq->ipsq_lock);
6272 	ipx = ipsq->ipsq_xop;
6273 	mutex_enter(&ipx->ipx_lock);
6274 
6275 	/*
6276 	 * Dequeue the next message associated with the current exclusive
6277 	 * operation, if any.
6278 	 */
6279 	if ((mp = ipx->ipx_mphead) != NULL) {
6280 		ipx->ipx_mphead = mp->b_next;
6281 		if (ipx->ipx_mphead == NULL)
6282 			ipx->ipx_mptail = NULL;
6283 		mp->b_next = (void *)ipsq;
6284 		goto out;
6285 	}
6286 
6287 	if (ipx->ipx_current_ipif != NULL)
6288 		goto empty;
6289 
6290 	if (ipsq->ipsq_swxop != NULL) {
6291 		/*
6292 		 * The exclusive operation that is now being completed has
6293 		 * requested a switch to a different xop.  This happens
6294 		 * when an interface joins or leaves an IPMP group.  Joins
6295 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6296 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6297 		 * (phyint_free()), or interface plumb for an ill type
6298 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6299 		 *
6300 		 * Xop switches are not allowed on the IPMP meta-interface.
6301 		 */
6302 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6303 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6304 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6305 
6306 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6307 			/*
6308 			 * We're switching back to our own xop, so we have two
6309 			 * xop's to drain/exit: our own, and the group xop
6310 			 * that we are leaving.
6311 			 *
6312 			 * First, pull ourselves out of the group ipsq list.
6313 			 * This is safe since we're writer on ill_g_lock.
6314 			 */
6315 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6316 
6317 			xopipsq = ipx->ipx_ipsq;
6318 			while (xopipsq->ipsq_next != ipsq)
6319 				xopipsq = xopipsq->ipsq_next;
6320 
6321 			xopipsq->ipsq_next = ipsq->ipsq_next;
6322 			ipsq->ipsq_next = ipsq;
6323 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6324 			ipsq->ipsq_swxop = NULL;
6325 
6326 			/*
6327 			 * Second, prepare to exit the group xop.  The actual
6328 			 * ipsq_exit() is done at the end of this function
6329 			 * since we cannot hold any locks across ipsq_exit().
6330 			 * Note that although we drop the group's ipx_lock, no
6331 			 * threads can proceed since we're still ipx_writer.
6332 			 */
6333 			leftipsq = xopipsq;
6334 			mutex_exit(&ipx->ipx_lock);
6335 
6336 			/*
6337 			 * Third, set ipx to point to our own xop (which was
6338 			 * inactive and therefore can be entered).
6339 			 */
6340 			ipx = ipsq->ipsq_xop;
6341 			mutex_enter(&ipx->ipx_lock);
6342 			ASSERT(ipx->ipx_writer == NULL);
6343 			ASSERT(ipx->ipx_current_ipif == NULL);
6344 		} else {
6345 			/*
6346 			 * We're switching from our own xop to a group xop.
6347 			 * The requestor of the switch must ensure that the
6348 			 * group xop cannot go away (e.g. by ensuring the
6349 			 * phyint associated with the xop cannot go away).
6350 			 *
6351 			 * If we can become writer on our new xop, then we'll
6352 			 * do the drain.  Otherwise, the current writer of our
6353 			 * new xop will do the drain when it exits.
6354 			 *
6355 			 * First, splice ourselves into the group IPSQ list.
6356 			 * This is safe since we're writer on ill_g_lock.
6357 			 */
6358 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6359 
6360 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6361 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6362 				xopipsq = xopipsq->ipsq_next;
6363 
6364 			xopipsq->ipsq_next = ipsq;
6365 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6366 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6367 			ipsq->ipsq_swxop = NULL;
6368 
6369 			/*
6370 			 * Second, exit our own xop, since it's now unused.
6371 			 * This is safe since we've got the only reference.
6372 			 */
6373 			ASSERT(ipx->ipx_writer == curthread);
6374 			ipx->ipx_writer = NULL;
6375 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6376 			ipx->ipx_ipsq_queued = B_FALSE;
6377 			mutex_exit(&ipx->ipx_lock);
6378 
6379 			/*
6380 			 * Third, set ipx to point to our new xop, and check
6381 			 * if we can become writer on it.  If we cannot, then
6382 			 * the current writer will drain the IPSQ group when
6383 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6384 			 * because we're still holding ipsq_lock.
6385 			 */
6386 			ipx = ipsq->ipsq_xop;
6387 			mutex_enter(&ipx->ipx_lock);
6388 			if (ipx->ipx_writer != NULL ||
6389 			    ipx->ipx_current_ipif != NULL) {
6390 				goto out;
6391 			}
6392 		}
6393 
6394 		/*
6395 		 * Fourth, become writer on our new ipx before we continue
6396 		 * with the drain.  Note that we never dropped ipsq_lock
6397 		 * above, so no other thread could've raced with us to
6398 		 * become writer first.  Also, we're holding ipx_lock, so
6399 		 * no other thread can examine the ipx right now.
6400 		 */
6401 		ASSERT(ipx->ipx_current_ipif == NULL);
6402 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6403 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6404 		ipx->ipx_writer = curthread;
6405 		ipx->ipx_forced = B_FALSE;
6406 #ifdef DEBUG
6407 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6408 #endif
6409 	}
6410 
6411 	xopipsq = ipsq;
6412 	do {
6413 		/*
6414 		 * So that other operations operate on a consistent and
6415 		 * complete phyint, a switch message on an IPSQ must be
6416 		 * handled prior to any other operations on that IPSQ.
6417 		 */
6418 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6419 			xopipsq->ipsq_switch_mp = NULL;
6420 			ASSERT(mp->b_next == NULL);
6421 			mp->b_next = (void *)xopipsq;
6422 			goto out;
6423 		}
6424 
6425 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6426 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6427 			if (xopipsq->ipsq_xopq_mphead == NULL)
6428 				xopipsq->ipsq_xopq_mptail = NULL;
6429 			mp->b_next = (void *)xopipsq;
6430 			goto out;
6431 		}
6432 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6433 empty:
6434 	/*
6435 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6436 	 * new messages can end up on any IPSQ in the xop.
6437 	 */
6438 	ipx->ipx_writer = NULL;
6439 	ipx->ipx_forced = B_FALSE;
6440 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6441 	ipx->ipx_ipsq_queued = B_FALSE;
6442 	emptied = B_TRUE;
6443 #ifdef	DEBUG
6444 	ipx->ipx_depth = 0;
6445 #endif
6446 out:
6447 	mutex_exit(&ipx->ipx_lock);
6448 	mutex_exit(&ipsq->ipsq_lock);
6449 
6450 	/*
6451 	 * If we completely emptied the xop, then wake up any threads waiting
6452 	 * to enter any of the IPSQ's associated with it.
6453 	 */
6454 	if (emptied) {
6455 		xopipsq = ipsq;
6456 		do {
6457 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6458 				continue;
6459 
6460 			illv4 = phyi->phyint_illv4;
6461 			illv6 = phyi->phyint_illv6;
6462 
6463 			GRAB_ILL_LOCKS(illv4, illv6);
6464 			if (illv4 != NULL)
6465 				cv_broadcast(&illv4->ill_cv);
6466 			if (illv6 != NULL)
6467 				cv_broadcast(&illv6->ill_cv);
6468 			RELEASE_ILL_LOCKS(illv4, illv6);
6469 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6470 	}
6471 	rw_exit(&ipst->ips_ill_g_lock);
6472 
6473 	/*
6474 	 * Now that all locks are dropped, exit the IPSQ we left.
6475 	 */
6476 	if (leftipsq != NULL)
6477 		ipsq_exit(leftipsq);
6478 
6479 	return (mp);
6480 }
6481 
6482 /*
6483  * Return completion status of previously initiated DLPI operations on
6484  * ills in the purview of an ipsq.
6485  */
6486 static boolean_t
6487 ipsq_dlpi_done(ipsq_t *ipsq)
6488 {
6489 	ipsq_t		*ipsq_start;
6490 	phyint_t	*phyi;
6491 	ill_t		*ill;
6492 
6493 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6494 	ipsq_start = ipsq;
6495 
6496 	do {
6497 		/*
6498 		 * The only current users of this function are ipsq_try_enter
6499 		 * and ipsq_enter which have made sure that ipsq_writer is
6500 		 * NULL before we reach here. ill_dlpi_pending is modified
6501 		 * only by an ipsq writer
6502 		 */
6503 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6504 		phyi = ipsq->ipsq_phyint;
6505 		/*
6506 		 * phyi could be NULL if a phyint that is part of an
6507 		 * IPMP group is being unplumbed. A more detailed
6508 		 * comment is in ipmp_grp_update_kstats()
6509 		 */
6510 		if (phyi != NULL) {
6511 			ill = phyi->phyint_illv4;
6512 			if (ill != NULL &&
6513 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6514 			    ill->ill_arl_dlpi_pending))
6515 				return (B_FALSE);
6516 
6517 			ill = phyi->phyint_illv6;
6518 			if (ill != NULL &&
6519 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6520 				return (B_FALSE);
6521 		}
6522 
6523 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6524 
6525 	return (B_TRUE);
6526 }
6527 
6528 /*
6529  * Enter the ipsq corresponding to ill, by waiting synchronously till
6530  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6531  * will have to drain completely before ipsq_enter returns success.
6532  * ipx_current_ipif will be set if some exclusive op is in progress,
6533  * and the ipsq_exit logic will start the next enqueued op after
6534  * completion of the current op. If 'force' is used, we don't wait
6535  * for the enqueued ops. This is needed when a conn_close wants to
6536  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6537  * of an ill can also use this option. But we dont' use it currently.
6538  */
6539 #define	ENTER_SQ_WAIT_TICKS 100
6540 boolean_t
6541 ipsq_enter(ill_t *ill, boolean_t force, int type)
6542 {
6543 	ipsq_t	*ipsq;
6544 	ipxop_t *ipx;
6545 	boolean_t waited_enough = B_FALSE;
6546 	ip_stack_t *ipst = ill->ill_ipst;
6547 
6548 	/*
6549 	 * Note that the relationship between ill and ipsq is fixed as long as
6550 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6551 	 * relationship between the IPSQ and xop cannot change.  However,
6552 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6553 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6554 	 * waking up all ills in the xop when it becomes available.
6555 	 */
6556 	for (;;) {
6557 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6558 		mutex_enter(&ill->ill_lock);
6559 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6560 			mutex_exit(&ill->ill_lock);
6561 			rw_exit(&ipst->ips_ill_g_lock);
6562 			return (B_FALSE);
6563 		}
6564 
6565 		ipsq = ill->ill_phyint->phyint_ipsq;
6566 		mutex_enter(&ipsq->ipsq_lock);
6567 		ipx = ipsq->ipsq_xop;
6568 		mutex_enter(&ipx->ipx_lock);
6569 
6570 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6571 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6572 		    waited_enough))
6573 			break;
6574 
6575 		rw_exit(&ipst->ips_ill_g_lock);
6576 
6577 		if (!force || ipx->ipx_writer != NULL) {
6578 			mutex_exit(&ipx->ipx_lock);
6579 			mutex_exit(&ipsq->ipsq_lock);
6580 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6581 		} else {
6582 			mutex_exit(&ipx->ipx_lock);
6583 			mutex_exit(&ipsq->ipsq_lock);
6584 			(void) cv_reltimedwait(&ill->ill_cv,
6585 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6586 			waited_enough = B_TRUE;
6587 		}
6588 		mutex_exit(&ill->ill_lock);
6589 	}
6590 
6591 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6592 	ASSERT(ipx->ipx_reentry_cnt == 0);
6593 	ipx->ipx_writer = curthread;
6594 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6595 	ipx->ipx_reentry_cnt++;
6596 #ifdef DEBUG
6597 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6598 #endif
6599 	mutex_exit(&ipx->ipx_lock);
6600 	mutex_exit(&ipsq->ipsq_lock);
6601 	mutex_exit(&ill->ill_lock);
6602 	rw_exit(&ipst->ips_ill_g_lock);
6603 
6604 	return (B_TRUE);
6605 }
6606 
6607 /*
6608  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6609  * across the call to the core interface ipsq_try_enter() and hence calls this
6610  * function directly. This is explained more fully in ipif_set_values().
6611  * In order to support the above constraint, ipsq_try_enter is implemented as
6612  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6613  */
6614 static ipsq_t *
6615 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6616     int type, boolean_t reentry_ok)
6617 {
6618 	ipsq_t	*ipsq;
6619 	ipxop_t	*ipx;
6620 	ip_stack_t *ipst = ill->ill_ipst;
6621 
6622 	/*
6623 	 * lock ordering:
6624 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6625 	 *
6626 	 * ipx of an ipsq can't change when ipsq_lock is held.
6627 	 */
6628 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6629 	GRAB_CONN_LOCK(q);
6630 	mutex_enter(&ill->ill_lock);
6631 	ipsq = ill->ill_phyint->phyint_ipsq;
6632 	mutex_enter(&ipsq->ipsq_lock);
6633 	ipx = ipsq->ipsq_xop;
6634 	mutex_enter(&ipx->ipx_lock);
6635 
6636 	/*
6637 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6638 	 *    (Note: If the caller does not specify reentry_ok then neither
6639 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6640 	 *    again. Otherwise it can lead to an infinite loop
6641 	 * 2. Enter the ipsq if there is no current writer and this attempted
6642 	 *    entry is part of the current operation
6643 	 * 3. Enter the ipsq if there is no current writer and this is a new
6644 	 *    operation and the operation queue is empty and there is no
6645 	 *    operation currently in progress and if all previously initiated
6646 	 *    DLPI operations have completed.
6647 	 */
6648 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6649 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6650 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6651 	    ipsq_dlpi_done(ipsq))))) {
6652 		/* Success. */
6653 		ipx->ipx_reentry_cnt++;
6654 		ipx->ipx_writer = curthread;
6655 		ipx->ipx_forced = B_FALSE;
6656 		mutex_exit(&ipx->ipx_lock);
6657 		mutex_exit(&ipsq->ipsq_lock);
6658 		mutex_exit(&ill->ill_lock);
6659 		RELEASE_CONN_LOCK(q);
6660 #ifdef DEBUG
6661 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6662 #endif
6663 		return (ipsq);
6664 	}
6665 
6666 	if (func != NULL)
6667 		ipsq_enq(ipsq, q, mp, func, type, ill);
6668 
6669 	mutex_exit(&ipx->ipx_lock);
6670 	mutex_exit(&ipsq->ipsq_lock);
6671 	mutex_exit(&ill->ill_lock);
6672 	RELEASE_CONN_LOCK(q);
6673 	return (NULL);
6674 }
6675 
6676 /*
6677  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6678  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6679  * There is one ipsq per phyint. The ipsq
6680  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6681  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6682  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6683  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6684  * up the interface) and are enqueued in ipx_mphead.
6685  *
6686  * If a thread does not want to reenter the ipsq when it is already writer,
6687  * it must make sure that the specified reentry point to be called later
6688  * when the ipsq is empty, nor any code path starting from the specified reentry
6689  * point must never ever try to enter the ipsq again. Otherwise it can lead
6690  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6691  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6692  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6693  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6694  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6695  * ioctl if the current ioctl has completed. If the current ioctl is still
6696  * in progress it simply returns. The current ioctl could be waiting for
6697  * a response from another module (the driver or could be waiting for
6698  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6699  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6700  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6701  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6702  * all associated DLPI operations have completed.
6703  */
6704 
6705 /*
6706  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6707  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6708  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6709  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6710  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6711  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6712  */
6713 ipsq_t *
6714 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6715     ipsq_func_t func, int type, boolean_t reentry_ok)
6716 {
6717 	ip_stack_t	*ipst;
6718 	ipsq_t		*ipsq;
6719 
6720 	/* Only 1 of ipif or ill can be specified */
6721 	ASSERT((ipif != NULL) ^ (ill != NULL));
6722 
6723 	if (ipif != NULL)
6724 		ill = ipif->ipif_ill;
6725 	ipst = ill->ill_ipst;
6726 
6727 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6728 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6729 	rw_exit(&ipst->ips_ill_g_lock);
6730 
6731 	return (ipsq);
6732 }
6733 
6734 /*
6735  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6736  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6737  * cannot be entered, the mp is queued for completion.
6738  */
6739 void
6740 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6741     boolean_t reentry_ok)
6742 {
6743 	ipsq_t	*ipsq;
6744 
6745 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6746 
6747 	/*
6748 	 * Drop the caller's refhold on the ill.  This is safe since we either
6749 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6750 	 * IPSQ, in which case we return without accessing ill anymore.  This
6751 	 * is needed because func needs to see the correct refcount.
6752 	 * e.g. removeif can work only then.
6753 	 */
6754 	ill_refrele(ill);
6755 	if (ipsq != NULL) {
6756 		(*func)(ipsq, q, mp, NULL);
6757 		ipsq_exit(ipsq);
6758 	}
6759 }
6760 
6761 /*
6762  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6763  * prior to exiting.  Caller must be writer on the specified IPSQ.
6764  */
6765 void
6766 ipsq_exit(ipsq_t *ipsq)
6767 {
6768 	mblk_t *mp;
6769 	ipsq_t *mp_ipsq;
6770 	queue_t	*q;
6771 	phyint_t *phyi;
6772 	ipsq_func_t func;
6773 
6774 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6775 
6776 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6777 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6778 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6779 		return;
6780 	}
6781 
6782 	for (;;) {
6783 		phyi = ipsq->ipsq_phyint;
6784 		mp = ipsq_dq(ipsq);
6785 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6786 
6787 		/*
6788 		 * If we've changed to a new IPSQ, and the phyint associated
6789 		 * with the old one has gone away, free the old IPSQ.  Note
6790 		 * that this cannot happen while the IPSQ is in a group.
6791 		 */
6792 		if (mp_ipsq != ipsq && phyi == NULL) {
6793 			ASSERT(ipsq->ipsq_next == ipsq);
6794 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6795 			ipsq_delete(ipsq);
6796 		}
6797 
6798 		if (mp == NULL)
6799 			break;
6800 
6801 		q = mp->b_queue;
6802 		func = (ipsq_func_t)mp->b_prev;
6803 		ipsq = mp_ipsq;
6804 		mp->b_next = mp->b_prev = NULL;
6805 		mp->b_queue = NULL;
6806 
6807 		/*
6808 		 * If 'q' is an conn queue, it is valid, since we did a
6809 		 * a refhold on the conn at the start of the ioctl.
6810 		 * If 'q' is an ill queue, it is valid, since close of an
6811 		 * ill will clean up its IPSQ.
6812 		 */
6813 		(*func)(ipsq, q, mp, NULL);
6814 	}
6815 }
6816 
6817 /*
6818  * Used to start any igmp or mld timers that could not be started
6819  * while holding ill_mcast_lock. The timers can't be started while holding
6820  * the lock, since mld/igmp_start_timers may need to call untimeout()
6821  * which can't be done while holding the lock which the timeout handler
6822  * acquires. Otherwise
6823  * there could be a deadlock since the timeout handlers
6824  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6825  * ill_mcast_lock.
6826  */
6827 void
6828 ill_mcast_timer_start(ip_stack_t *ipst)
6829 {
6830 	int		next;
6831 
6832 	mutex_enter(&ipst->ips_igmp_timer_lock);
6833 	next = ipst->ips_igmp_deferred_next;
6834 	ipst->ips_igmp_deferred_next = INFINITY;
6835 	mutex_exit(&ipst->ips_igmp_timer_lock);
6836 
6837 	if (next != INFINITY)
6838 		igmp_start_timers(next, ipst);
6839 
6840 	mutex_enter(&ipst->ips_mld_timer_lock);
6841 	next = ipst->ips_mld_deferred_next;
6842 	ipst->ips_mld_deferred_next = INFINITY;
6843 	mutex_exit(&ipst->ips_mld_timer_lock);
6844 
6845 	if (next != INFINITY)
6846 		mld_start_timers(next, ipst);
6847 }
6848 
6849 /*
6850  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6851  * and `ioccmd'.
6852  */
6853 void
6854 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6855 {
6856 	ill_t *ill = ipif->ipif_ill;
6857 	ipxop_t *ipx = ipsq->ipsq_xop;
6858 
6859 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6860 	ASSERT(ipx->ipx_current_ipif == NULL);
6861 	ASSERT(ipx->ipx_current_ioctl == 0);
6862 
6863 	ipx->ipx_current_done = B_FALSE;
6864 	ipx->ipx_current_ioctl = ioccmd;
6865 	mutex_enter(&ipx->ipx_lock);
6866 	ipx->ipx_current_ipif = ipif;
6867 	mutex_exit(&ipx->ipx_lock);
6868 
6869 	/*
6870 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6871 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6872 	 * references to the ipif (so that the references will eventually
6873 	 * drop to zero) and also prevents any "get" operations (e.g.,
6874 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6875 	 * operation has completed and the ipif is again in a stable state.
6876 	 *
6877 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6878 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6879 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6880 	 * ipifs will be affected.
6881 	 *
6882 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6883 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6884 	 * operate on.
6885 	 */
6886 	switch (ioccmd) {
6887 	case SIOCLIFREMOVEIF:
6888 		break;
6889 	case 0:
6890 		mutex_enter(&ill->ill_lock);
6891 		ipif = ipif->ipif_ill->ill_ipif;
6892 		for (; ipif != NULL; ipif = ipif->ipif_next)
6893 			ipif->ipif_state_flags |= IPIF_CHANGING;
6894 		mutex_exit(&ill->ill_lock);
6895 		break;
6896 	default:
6897 		mutex_enter(&ill->ill_lock);
6898 		ipif->ipif_state_flags |= IPIF_CHANGING;
6899 		mutex_exit(&ill->ill_lock);
6900 	}
6901 }
6902 
6903 /*
6904  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6905  * the next exclusive operation to begin once we ipsq_exit().  However, if
6906  * pending DLPI operations remain, then we will wait for the queue to drain
6907  * before allowing the next exclusive operation to begin.  This ensures that
6908  * DLPI operations from one exclusive operation are never improperly processed
6909  * as part of a subsequent exclusive operation.
6910  */
6911 void
6912 ipsq_current_finish(ipsq_t *ipsq)
6913 {
6914 	ipxop_t	*ipx = ipsq->ipsq_xop;
6915 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6916 	ipif_t	*ipif = ipx->ipx_current_ipif;
6917 
6918 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6919 
6920 	/*
6921 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6922 	 * (but in that case, IPIF_CHANGING will already be clear and no
6923 	 * pending DLPI messages can remain).
6924 	 */
6925 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6926 		ill_t *ill = ipif->ipif_ill;
6927 
6928 		mutex_enter(&ill->ill_lock);
6929 		dlpi_pending = ill->ill_dlpi_pending;
6930 		if (ipx->ipx_current_ioctl == 0) {
6931 			ipif = ill->ill_ipif;
6932 			for (; ipif != NULL; ipif = ipif->ipif_next)
6933 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6934 		} else {
6935 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6936 		}
6937 		mutex_exit(&ill->ill_lock);
6938 	}
6939 
6940 	ASSERT(!ipx->ipx_current_done);
6941 	ipx->ipx_current_done = B_TRUE;
6942 	ipx->ipx_current_ioctl = 0;
6943 	if (dlpi_pending == DL_PRIM_INVAL) {
6944 		mutex_enter(&ipx->ipx_lock);
6945 		ipx->ipx_current_ipif = NULL;
6946 		mutex_exit(&ipx->ipx_lock);
6947 	}
6948 }
6949 
6950 /*
6951  * The ill is closing. Flush all messages on the ipsq that originated
6952  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6953  * for this ill since ipsq_enter could not have entered until then.
6954  * New messages can't be queued since the CONDEMNED flag is set.
6955  */
6956 static void
6957 ipsq_flush(ill_t *ill)
6958 {
6959 	queue_t	*q;
6960 	mblk_t	*prev;
6961 	mblk_t	*mp;
6962 	mblk_t	*mp_next;
6963 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6964 
6965 	ASSERT(IAM_WRITER_ILL(ill));
6966 
6967 	/*
6968 	 * Flush any messages sent up by the driver.
6969 	 */
6970 	mutex_enter(&ipx->ipx_lock);
6971 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6972 		mp_next = mp->b_next;
6973 		q = mp->b_queue;
6974 		if (q == ill->ill_rq || q == ill->ill_wq) {
6975 			/* dequeue mp */
6976 			if (prev == NULL)
6977 				ipx->ipx_mphead = mp->b_next;
6978 			else
6979 				prev->b_next = mp->b_next;
6980 			if (ipx->ipx_mptail == mp) {
6981 				ASSERT(mp_next == NULL);
6982 				ipx->ipx_mptail = prev;
6983 			}
6984 			inet_freemsg(mp);
6985 		} else {
6986 			prev = mp;
6987 		}
6988 	}
6989 	mutex_exit(&ipx->ipx_lock);
6990 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6991 	ipsq_xopq_mp_cleanup(ill, NULL);
6992 }
6993 
6994 /*
6995  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6996  * and return the associated ipif.
6997  * Return value:
6998  *	Non zero: An error has occurred. ci may not be filled out.
6999  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7000  *	a held ipif in ci.ci_ipif.
7001  */
7002 int
7003 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7004     cmd_info_t *ci)
7005 {
7006 	char		*name;
7007 	struct ifreq    *ifr;
7008 	struct lifreq    *lifr;
7009 	ipif_t		*ipif = NULL;
7010 	ill_t		*ill;
7011 	conn_t		*connp;
7012 	boolean_t	isv6;
7013 	boolean_t	exists;
7014 	mblk_t		*mp1;
7015 	zoneid_t	zoneid;
7016 	ip_stack_t	*ipst;
7017 
7018 	if (q->q_next != NULL) {
7019 		ill = (ill_t *)q->q_ptr;
7020 		isv6 = ill->ill_isv6;
7021 		connp = NULL;
7022 		zoneid = ALL_ZONES;
7023 		ipst = ill->ill_ipst;
7024 	} else {
7025 		ill = NULL;
7026 		connp = Q_TO_CONN(q);
7027 		isv6 = (connp->conn_family == AF_INET6);
7028 		zoneid = connp->conn_zoneid;
7029 		if (zoneid == GLOBAL_ZONEID) {
7030 			/* global zone can access ipifs in all zones */
7031 			zoneid = ALL_ZONES;
7032 		}
7033 		ipst = connp->conn_netstack->netstack_ip;
7034 	}
7035 
7036 	/* Has been checked in ip_wput_nondata */
7037 	mp1 = mp->b_cont->b_cont;
7038 
7039 	if (ipip->ipi_cmd_type == IF_CMD) {
7040 		/* This a old style SIOC[GS]IF* command */
7041 		ifr = (struct ifreq *)mp1->b_rptr;
7042 		/*
7043 		 * Null terminate the string to protect against buffer
7044 		 * overrun. String was generated by user code and may not
7045 		 * be trusted.
7046 		 */
7047 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7048 		name = ifr->ifr_name;
7049 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7050 		ci->ci_sin6 = NULL;
7051 		ci->ci_lifr = (struct lifreq *)ifr;
7052 	} else {
7053 		/* This a new style SIOC[GS]LIF* command */
7054 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7055 		lifr = (struct lifreq *)mp1->b_rptr;
7056 		/*
7057 		 * Null terminate the string to protect against buffer
7058 		 * overrun. String was generated by user code and may not
7059 		 * be trusted.
7060 		 */
7061 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7062 		name = lifr->lifr_name;
7063 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7064 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7065 		ci->ci_lifr = lifr;
7066 	}
7067 
7068 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7069 		/*
7070 		 * The ioctl will be failed if the ioctl comes down
7071 		 * an conn stream
7072 		 */
7073 		if (ill == NULL) {
7074 			/*
7075 			 * Not an ill queue, return EINVAL same as the
7076 			 * old error code.
7077 			 */
7078 			return (ENXIO);
7079 		}
7080 		ipif = ill->ill_ipif;
7081 		ipif_refhold(ipif);
7082 	} else {
7083 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7084 		    &exists, isv6, zoneid, ipst);
7085 
7086 		/*
7087 		 * Ensure that get ioctls don't see any internal state changes
7088 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7089 		 * set.
7090 		 */
7091 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7092 		    !IAM_WRITER_IPIF(ipif)) {
7093 			ipsq_t	*ipsq;
7094 
7095 			if (connp != NULL)
7096 				mutex_enter(&connp->conn_lock);
7097 			mutex_enter(&ipif->ipif_ill->ill_lock);
7098 			if (IPIF_IS_CHANGING(ipif) &&
7099 			    !IPIF_IS_CONDEMNED(ipif)) {
7100 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7101 				mutex_enter(&ipsq->ipsq_lock);
7102 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7103 				mutex_exit(&ipif->ipif_ill->ill_lock);
7104 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7105 				    NEW_OP, ipif->ipif_ill);
7106 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7107 				mutex_exit(&ipsq->ipsq_lock);
7108 				if (connp != NULL)
7109 					mutex_exit(&connp->conn_lock);
7110 				ipif_refrele(ipif);
7111 				return (EINPROGRESS);
7112 			}
7113 			mutex_exit(&ipif->ipif_ill->ill_lock);
7114 			if (connp != NULL)
7115 				mutex_exit(&connp->conn_lock);
7116 		}
7117 	}
7118 
7119 	/*
7120 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7121 	 */
7122 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7123 		ipif_refrele(ipif);
7124 		return (ENXIO);
7125 	}
7126 
7127 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7128 	    name[0] == '\0') {
7129 		/*
7130 		 * Handle a or a SIOC?IF* with a null name
7131 		 * during plumb (on the ill queue before the I_PLINK).
7132 		 */
7133 		ipif = ill->ill_ipif;
7134 		ipif_refhold(ipif);
7135 	}
7136 
7137 	if (ipif == NULL)
7138 		return (ENXIO);
7139 
7140 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7141 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7142 
7143 	ci->ci_ipif = ipif;
7144 	return (0);
7145 }
7146 
7147 /*
7148  * Return the total number of ipifs.
7149  */
7150 static uint_t
7151 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7152 {
7153 	uint_t numifs = 0;
7154 	ill_t	*ill;
7155 	ill_walk_context_t	ctx;
7156 	ipif_t	*ipif;
7157 
7158 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7159 	ill = ILL_START_WALK_V4(&ctx, ipst);
7160 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7161 		if (IS_UNDER_IPMP(ill))
7162 			continue;
7163 		for (ipif = ill->ill_ipif; ipif != NULL;
7164 		    ipif = ipif->ipif_next) {
7165 			if (ipif->ipif_zoneid == zoneid ||
7166 			    ipif->ipif_zoneid == ALL_ZONES)
7167 				numifs++;
7168 		}
7169 	}
7170 	rw_exit(&ipst->ips_ill_g_lock);
7171 	return (numifs);
7172 }
7173 
7174 /*
7175  * Return the total number of ipifs.
7176  */
7177 static uint_t
7178 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7179 {
7180 	uint_t numifs = 0;
7181 	ill_t	*ill;
7182 	ipif_t	*ipif;
7183 	ill_walk_context_t	ctx;
7184 
7185 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7186 
7187 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7188 	if (family == AF_INET)
7189 		ill = ILL_START_WALK_V4(&ctx, ipst);
7190 	else if (family == AF_INET6)
7191 		ill = ILL_START_WALK_V6(&ctx, ipst);
7192 	else
7193 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7194 
7195 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7196 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7197 			continue;
7198 
7199 		for (ipif = ill->ill_ipif; ipif != NULL;
7200 		    ipif = ipif->ipif_next) {
7201 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7202 			    !(lifn_flags & LIFC_NOXMIT))
7203 				continue;
7204 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7205 			    !(lifn_flags & LIFC_TEMPORARY))
7206 				continue;
7207 			if (((ipif->ipif_flags &
7208 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7209 			    IPIF_DEPRECATED)) ||
7210 			    IS_LOOPBACK(ill) ||
7211 			    !(ipif->ipif_flags & IPIF_UP)) &&
7212 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7213 				continue;
7214 
7215 			if (zoneid != ipif->ipif_zoneid &&
7216 			    ipif->ipif_zoneid != ALL_ZONES &&
7217 			    (zoneid != GLOBAL_ZONEID ||
7218 			    !(lifn_flags & LIFC_ALLZONES)))
7219 				continue;
7220 
7221 			numifs++;
7222 		}
7223 	}
7224 	rw_exit(&ipst->ips_ill_g_lock);
7225 	return (numifs);
7226 }
7227 
7228 uint_t
7229 ip_get_lifsrcofnum(ill_t *ill)
7230 {
7231 	uint_t numifs = 0;
7232 	ill_t	*ill_head = ill;
7233 	ip_stack_t	*ipst = ill->ill_ipst;
7234 
7235 	/*
7236 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7237 	 * other thread may be trying to relink the ILLs in this usesrc group
7238 	 * and adjusting the ill_usesrc_grp_next pointers
7239 	 */
7240 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7241 	if ((ill->ill_usesrc_ifindex == 0) &&
7242 	    (ill->ill_usesrc_grp_next != NULL)) {
7243 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7244 		    ill = ill->ill_usesrc_grp_next)
7245 			numifs++;
7246 	}
7247 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7248 
7249 	return (numifs);
7250 }
7251 
7252 /* Null values are passed in for ipif, sin, and ifreq */
7253 /* ARGSUSED */
7254 int
7255 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7256     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7257 {
7258 	int *nump;
7259 	conn_t *connp = Q_TO_CONN(q);
7260 
7261 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7262 
7263 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7264 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7265 
7266 	*nump = ip_get_numifs(connp->conn_zoneid,
7267 	    connp->conn_netstack->netstack_ip);
7268 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7269 	return (0);
7270 }
7271 
7272 /* Null values are passed in for ipif, sin, and ifreq */
7273 /* ARGSUSED */
7274 int
7275 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7276     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7277 {
7278 	struct lifnum *lifn;
7279 	mblk_t	*mp1;
7280 	conn_t *connp = Q_TO_CONN(q);
7281 
7282 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7283 
7284 	/* Existence checked in ip_wput_nondata */
7285 	mp1 = mp->b_cont->b_cont;
7286 
7287 	lifn = (struct lifnum *)mp1->b_rptr;
7288 	switch (lifn->lifn_family) {
7289 	case AF_UNSPEC:
7290 	case AF_INET:
7291 	case AF_INET6:
7292 		break;
7293 	default:
7294 		return (EAFNOSUPPORT);
7295 	}
7296 
7297 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7298 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7299 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7300 	return (0);
7301 }
7302 
7303 /* ARGSUSED */
7304 int
7305 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7306     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7307 {
7308 	STRUCT_HANDLE(ifconf, ifc);
7309 	mblk_t *mp1;
7310 	struct iocblk *iocp;
7311 	struct ifreq *ifr;
7312 	ill_walk_context_t	ctx;
7313 	ill_t	*ill;
7314 	ipif_t	*ipif;
7315 	struct sockaddr_in *sin;
7316 	int32_t	ifclen;
7317 	zoneid_t zoneid;
7318 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7319 
7320 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7321 
7322 	ip1dbg(("ip_sioctl_get_ifconf"));
7323 	/* Existence verified in ip_wput_nondata */
7324 	mp1 = mp->b_cont->b_cont;
7325 	iocp = (struct iocblk *)mp->b_rptr;
7326 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7327 
7328 	/*
7329 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7330 	 * the user buffer address and length into which the list of struct
7331 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7332 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7333 	 * the SIOCGIFCONF operation was redefined to simply provide
7334 	 * a large output buffer into which we are supposed to jam the ifreq
7335 	 * array.  The same ioctl command code was used, despite the fact that
7336 	 * both the applications and the kernel code had to change, thus making
7337 	 * it impossible to support both interfaces.
7338 	 *
7339 	 * For reasons not good enough to try to explain, the following
7340 	 * algorithm is used for deciding what to do with one of these:
7341 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7342 	 * form with the output buffer coming down as the continuation message.
7343 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7344 	 * and we have to copy in the ifconf structure to find out how big the
7345 	 * output buffer is and where to copy out to.  Sure no problem...
7346 	 *
7347 	 */
7348 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7349 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7350 		int numifs = 0;
7351 		size_t ifc_bufsize;
7352 
7353 		/*
7354 		 * Must be (better be!) continuation of a TRANSPARENT
7355 		 * IOCTL.  We just copied in the ifconf structure.
7356 		 */
7357 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7358 		    (struct ifconf *)mp1->b_rptr);
7359 
7360 		/*
7361 		 * Allocate a buffer to hold requested information.
7362 		 *
7363 		 * If ifc_len is larger than what is needed, we only
7364 		 * allocate what we will use.
7365 		 *
7366 		 * If ifc_len is smaller than what is needed, return
7367 		 * EINVAL.
7368 		 *
7369 		 * XXX: the ill_t structure can hava 2 counters, for
7370 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7371 		 * number of interfaces for a device, so we don't need
7372 		 * to count them here...
7373 		 */
7374 		numifs = ip_get_numifs(zoneid, ipst);
7375 
7376 		ifclen = STRUCT_FGET(ifc, ifc_len);
7377 		ifc_bufsize = numifs * sizeof (struct ifreq);
7378 		if (ifc_bufsize > ifclen) {
7379 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7380 				/* old behaviour */
7381 				return (EINVAL);
7382 			} else {
7383 				ifc_bufsize = ifclen;
7384 			}
7385 		}
7386 
7387 		mp1 = mi_copyout_alloc(q, mp,
7388 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7389 		if (mp1 == NULL)
7390 			return (ENOMEM);
7391 
7392 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7393 	}
7394 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7395 	/*
7396 	 * the SIOCGIFCONF ioctl only knows about
7397 	 * IPv4 addresses, so don't try to tell
7398 	 * it about interfaces with IPv6-only
7399 	 * addresses. (Last parm 'isv6' is B_FALSE)
7400 	 */
7401 
7402 	ifr = (struct ifreq *)mp1->b_rptr;
7403 
7404 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7405 	ill = ILL_START_WALK_V4(&ctx, ipst);
7406 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7407 		if (IS_UNDER_IPMP(ill))
7408 			continue;
7409 		for (ipif = ill->ill_ipif; ipif != NULL;
7410 		    ipif = ipif->ipif_next) {
7411 			if (zoneid != ipif->ipif_zoneid &&
7412 			    ipif->ipif_zoneid != ALL_ZONES)
7413 				continue;
7414 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7415 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7416 					/* old behaviour */
7417 					rw_exit(&ipst->ips_ill_g_lock);
7418 					return (EINVAL);
7419 				} else {
7420 					goto if_copydone;
7421 				}
7422 			}
7423 			ipif_get_name(ipif, ifr->ifr_name,
7424 			    sizeof (ifr->ifr_name));
7425 			sin = (sin_t *)&ifr->ifr_addr;
7426 			*sin = sin_null;
7427 			sin->sin_family = AF_INET;
7428 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7429 			ifr++;
7430 		}
7431 	}
7432 if_copydone:
7433 	rw_exit(&ipst->ips_ill_g_lock);
7434 	mp1->b_wptr = (uchar_t *)ifr;
7435 
7436 	if (STRUCT_BUF(ifc) != NULL) {
7437 		STRUCT_FSET(ifc, ifc_len,
7438 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7439 	}
7440 	return (0);
7441 }
7442 
7443 /*
7444  * Get the interfaces using the address hosted on the interface passed in,
7445  * as a source adddress
7446  */
7447 /* ARGSUSED */
7448 int
7449 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7450     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7451 {
7452 	mblk_t *mp1;
7453 	ill_t	*ill, *ill_head;
7454 	ipif_t	*ipif, *orig_ipif;
7455 	int	numlifs = 0;
7456 	size_t	lifs_bufsize, lifsmaxlen;
7457 	struct	lifreq *lifr;
7458 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7459 	uint_t	ifindex;
7460 	zoneid_t zoneid;
7461 	boolean_t isv6 = B_FALSE;
7462 	struct	sockaddr_in	*sin;
7463 	struct	sockaddr_in6	*sin6;
7464 	STRUCT_HANDLE(lifsrcof, lifs);
7465 	ip_stack_t		*ipst;
7466 
7467 	ipst = CONNQ_TO_IPST(q);
7468 
7469 	ASSERT(q->q_next == NULL);
7470 
7471 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7472 
7473 	/* Existence verified in ip_wput_nondata */
7474 	mp1 = mp->b_cont->b_cont;
7475 
7476 	/*
7477 	 * Must be (better be!) continuation of a TRANSPARENT
7478 	 * IOCTL.  We just copied in the lifsrcof structure.
7479 	 */
7480 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7481 	    (struct lifsrcof *)mp1->b_rptr);
7482 
7483 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7484 		return (EINVAL);
7485 
7486 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7487 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7488 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7489 	if (ipif == NULL) {
7490 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7491 		    ifindex));
7492 		return (ENXIO);
7493 	}
7494 
7495 	/* Allocate a buffer to hold requested information */
7496 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7497 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7498 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7499 	/* The actual size needed is always returned in lifs_len */
7500 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7501 
7502 	/* If the amount we need is more than what is passed in, abort */
7503 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7504 		ipif_refrele(ipif);
7505 		return (0);
7506 	}
7507 
7508 	mp1 = mi_copyout_alloc(q, mp,
7509 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7510 	if (mp1 == NULL) {
7511 		ipif_refrele(ipif);
7512 		return (ENOMEM);
7513 	}
7514 
7515 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7516 	bzero(mp1->b_rptr, lifs_bufsize);
7517 
7518 	lifr = (struct lifreq *)mp1->b_rptr;
7519 
7520 	ill = ill_head = ipif->ipif_ill;
7521 	orig_ipif = ipif;
7522 
7523 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7524 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7525 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7526 
7527 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7528 	for (; (ill != NULL) && (ill != ill_head);
7529 	    ill = ill->ill_usesrc_grp_next) {
7530 
7531 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7532 			break;
7533 
7534 		ipif = ill->ill_ipif;
7535 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7536 		if (ipif->ipif_isv6) {
7537 			sin6 = (sin6_t *)&lifr->lifr_addr;
7538 			*sin6 = sin6_null;
7539 			sin6->sin6_family = AF_INET6;
7540 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7541 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7542 			    &ipif->ipif_v6net_mask);
7543 		} else {
7544 			sin = (sin_t *)&lifr->lifr_addr;
7545 			*sin = sin_null;
7546 			sin->sin_family = AF_INET;
7547 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7548 			lifr->lifr_addrlen = ip_mask_to_plen(
7549 			    ipif->ipif_net_mask);
7550 		}
7551 		lifr++;
7552 	}
7553 	rw_exit(&ipst->ips_ill_g_lock);
7554 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7555 	ipif_refrele(orig_ipif);
7556 	mp1->b_wptr = (uchar_t *)lifr;
7557 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7558 
7559 	return (0);
7560 }
7561 
7562 /* ARGSUSED */
7563 int
7564 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7565     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7566 {
7567 	mblk_t *mp1;
7568 	int	list;
7569 	ill_t	*ill;
7570 	ipif_t	*ipif;
7571 	int	flags;
7572 	int	numlifs = 0;
7573 	size_t	lifc_bufsize;
7574 	struct	lifreq *lifr;
7575 	sa_family_t	family;
7576 	struct	sockaddr_in	*sin;
7577 	struct	sockaddr_in6	*sin6;
7578 	ill_walk_context_t	ctx;
7579 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7580 	int32_t	lifclen;
7581 	zoneid_t zoneid;
7582 	STRUCT_HANDLE(lifconf, lifc);
7583 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7584 
7585 	ip1dbg(("ip_sioctl_get_lifconf"));
7586 
7587 	ASSERT(q->q_next == NULL);
7588 
7589 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7590 
7591 	/* Existence verified in ip_wput_nondata */
7592 	mp1 = mp->b_cont->b_cont;
7593 
7594 	/*
7595 	 * An extended version of SIOCGIFCONF that takes an
7596 	 * additional address family and flags field.
7597 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7598 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7599 	 * interfaces are omitted.
7600 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7601 	 * unless LIFC_TEMPORARY is specified.
7602 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7603 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7604 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7605 	 * has priority over LIFC_NOXMIT.
7606 	 */
7607 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7608 
7609 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7610 		return (EINVAL);
7611 
7612 	/*
7613 	 * Must be (better be!) continuation of a TRANSPARENT
7614 	 * IOCTL.  We just copied in the lifconf structure.
7615 	 */
7616 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7617 
7618 	family = STRUCT_FGET(lifc, lifc_family);
7619 	flags = STRUCT_FGET(lifc, lifc_flags);
7620 
7621 	switch (family) {
7622 	case AF_UNSPEC:
7623 		/*
7624 		 * walk all ILL's.
7625 		 */
7626 		list = MAX_G_HEADS;
7627 		break;
7628 	case AF_INET:
7629 		/*
7630 		 * walk only IPV4 ILL's.
7631 		 */
7632 		list = IP_V4_G_HEAD;
7633 		break;
7634 	case AF_INET6:
7635 		/*
7636 		 * walk only IPV6 ILL's.
7637 		 */
7638 		list = IP_V6_G_HEAD;
7639 		break;
7640 	default:
7641 		return (EAFNOSUPPORT);
7642 	}
7643 
7644 	/*
7645 	 * Allocate a buffer to hold requested information.
7646 	 *
7647 	 * If lifc_len is larger than what is needed, we only
7648 	 * allocate what we will use.
7649 	 *
7650 	 * If lifc_len is smaller than what is needed, return
7651 	 * EINVAL.
7652 	 */
7653 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7654 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7655 	lifclen = STRUCT_FGET(lifc, lifc_len);
7656 	if (lifc_bufsize > lifclen) {
7657 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7658 			return (EINVAL);
7659 		else
7660 			lifc_bufsize = lifclen;
7661 	}
7662 
7663 	mp1 = mi_copyout_alloc(q, mp,
7664 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7665 	if (mp1 == NULL)
7666 		return (ENOMEM);
7667 
7668 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7669 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7670 
7671 	lifr = (struct lifreq *)mp1->b_rptr;
7672 
7673 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7674 	ill = ill_first(list, list, &ctx, ipst);
7675 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7676 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7677 			continue;
7678 
7679 		for (ipif = ill->ill_ipif; ipif != NULL;
7680 		    ipif = ipif->ipif_next) {
7681 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7682 			    !(flags & LIFC_NOXMIT))
7683 				continue;
7684 
7685 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7686 			    !(flags & LIFC_TEMPORARY))
7687 				continue;
7688 
7689 			if (((ipif->ipif_flags &
7690 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7691 			    IPIF_DEPRECATED)) ||
7692 			    IS_LOOPBACK(ill) ||
7693 			    !(ipif->ipif_flags & IPIF_UP)) &&
7694 			    (flags & LIFC_EXTERNAL_SOURCE))
7695 				continue;
7696 
7697 			if (zoneid != ipif->ipif_zoneid &&
7698 			    ipif->ipif_zoneid != ALL_ZONES &&
7699 			    (zoneid != GLOBAL_ZONEID ||
7700 			    !(flags & LIFC_ALLZONES)))
7701 				continue;
7702 
7703 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7704 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7705 					rw_exit(&ipst->ips_ill_g_lock);
7706 					return (EINVAL);
7707 				} else {
7708 					goto lif_copydone;
7709 				}
7710 			}
7711 
7712 			ipif_get_name(ipif, lifr->lifr_name,
7713 			    sizeof (lifr->lifr_name));
7714 			lifr->lifr_type = ill->ill_type;
7715 			if (ipif->ipif_isv6) {
7716 				sin6 = (sin6_t *)&lifr->lifr_addr;
7717 				*sin6 = sin6_null;
7718 				sin6->sin6_family = AF_INET6;
7719 				sin6->sin6_addr =
7720 				    ipif->ipif_v6lcl_addr;
7721 				lifr->lifr_addrlen =
7722 				    ip_mask_to_plen_v6(
7723 				    &ipif->ipif_v6net_mask);
7724 			} else {
7725 				sin = (sin_t *)&lifr->lifr_addr;
7726 				*sin = sin_null;
7727 				sin->sin_family = AF_INET;
7728 				sin->sin_addr.s_addr =
7729 				    ipif->ipif_lcl_addr;
7730 				lifr->lifr_addrlen =
7731 				    ip_mask_to_plen(
7732 				    ipif->ipif_net_mask);
7733 			}
7734 			lifr++;
7735 		}
7736 	}
7737 lif_copydone:
7738 	rw_exit(&ipst->ips_ill_g_lock);
7739 
7740 	mp1->b_wptr = (uchar_t *)lifr;
7741 	if (STRUCT_BUF(lifc) != NULL) {
7742 		STRUCT_FSET(lifc, lifc_len,
7743 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7744 	}
7745 	return (0);
7746 }
7747 
7748 static void
7749 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7750 {
7751 	ip6_asp_t *table;
7752 	size_t table_size;
7753 	mblk_t *data_mp;
7754 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7755 	ip_stack_t	*ipst;
7756 
7757 	if (q->q_next == NULL)
7758 		ipst = CONNQ_TO_IPST(q);
7759 	else
7760 		ipst = ILLQ_TO_IPST(q);
7761 
7762 	/* These two ioctls are I_STR only */
7763 	if (iocp->ioc_count == TRANSPARENT) {
7764 		miocnak(q, mp, 0, EINVAL);
7765 		return;
7766 	}
7767 
7768 	data_mp = mp->b_cont;
7769 	if (data_mp == NULL) {
7770 		/* The user passed us a NULL argument */
7771 		table = NULL;
7772 		table_size = iocp->ioc_count;
7773 	} else {
7774 		/*
7775 		 * The user provided a table.  The stream head
7776 		 * may have copied in the user data in chunks,
7777 		 * so make sure everything is pulled up
7778 		 * properly.
7779 		 */
7780 		if (MBLKL(data_mp) < iocp->ioc_count) {
7781 			mblk_t *new_data_mp;
7782 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7783 			    NULL) {
7784 				miocnak(q, mp, 0, ENOMEM);
7785 				return;
7786 			}
7787 			freemsg(data_mp);
7788 			data_mp = new_data_mp;
7789 			mp->b_cont = data_mp;
7790 		}
7791 		table = (ip6_asp_t *)data_mp->b_rptr;
7792 		table_size = iocp->ioc_count;
7793 	}
7794 
7795 	switch (iocp->ioc_cmd) {
7796 	case SIOCGIP6ADDRPOLICY:
7797 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7798 		if (iocp->ioc_rval == -1)
7799 			iocp->ioc_error = EINVAL;
7800 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7801 		else if (table != NULL &&
7802 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7803 			ip6_asp_t *src = table;
7804 			ip6_asp32_t *dst = (void *)table;
7805 			int count = table_size / sizeof (ip6_asp_t);
7806 			int i;
7807 
7808 			/*
7809 			 * We need to do an in-place shrink of the array
7810 			 * to match the alignment attributes of the
7811 			 * 32-bit ABI looking at it.
7812 			 */
7813 			/* LINTED: logical expression always true: op "||" */
7814 			ASSERT(sizeof (*src) > sizeof (*dst));
7815 			for (i = 1; i < count; i++)
7816 				bcopy(src + i, dst + i, sizeof (*dst));
7817 		}
7818 #endif
7819 		break;
7820 
7821 	case SIOCSIP6ADDRPOLICY:
7822 		ASSERT(mp->b_prev == NULL);
7823 		mp->b_prev = (void *)q;
7824 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7825 		/*
7826 		 * We pass in the datamodel here so that the ip6_asp_replace()
7827 		 * routine can handle converting from 32-bit to native formats
7828 		 * where necessary.
7829 		 *
7830 		 * A better way to handle this might be to convert the inbound
7831 		 * data structure here, and hang it off a new 'mp'; thus the
7832 		 * ip6_asp_replace() logic would always be dealing with native
7833 		 * format data structures..
7834 		 *
7835 		 * (An even simpler way to handle these ioctls is to just
7836 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7837 		 * and just recompile everything that depends on it.)
7838 		 */
7839 #endif
7840 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7841 		    iocp->ioc_flag & IOC_MODELS);
7842 		return;
7843 	}
7844 
7845 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7846 	qreply(q, mp);
7847 }
7848 
7849 static void
7850 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7851 {
7852 	mblk_t 		*data_mp;
7853 	struct dstinforeq	*dir;
7854 	uint8_t		*end, *cur;
7855 	in6_addr_t	*daddr, *saddr;
7856 	ipaddr_t	v4daddr;
7857 	ire_t		*ire;
7858 	ipaddr_t	v4setsrc;
7859 	in6_addr_t	v6setsrc;
7860 	char		*slabel, *dlabel;
7861 	boolean_t	isipv4;
7862 	int		match_ire;
7863 	ill_t		*dst_ill;
7864 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7865 	conn_t		*connp = Q_TO_CONN(q);
7866 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7867 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7868 	uint64_t	ipif_flags;
7869 
7870 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7871 
7872 	/*
7873 	 * This ioctl is I_STR only, and must have a
7874 	 * data mblk following the M_IOCTL mblk.
7875 	 */
7876 	data_mp = mp->b_cont;
7877 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7878 		miocnak(q, mp, 0, EINVAL);
7879 		return;
7880 	}
7881 
7882 	if (MBLKL(data_mp) < iocp->ioc_count) {
7883 		mblk_t *new_data_mp;
7884 
7885 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7886 			miocnak(q, mp, 0, ENOMEM);
7887 			return;
7888 		}
7889 		freemsg(data_mp);
7890 		data_mp = new_data_mp;
7891 		mp->b_cont = data_mp;
7892 	}
7893 	match_ire = MATCH_IRE_DSTONLY;
7894 
7895 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7896 	    end - cur >= sizeof (struct dstinforeq);
7897 	    cur += sizeof (struct dstinforeq)) {
7898 		dir = (struct dstinforeq *)cur;
7899 		daddr = &dir->dir_daddr;
7900 		saddr = &dir->dir_saddr;
7901 
7902 		/*
7903 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7904 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7905 		 * and ip_select_source_v6() do not.
7906 		 */
7907 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7908 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7909 
7910 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7911 		if (isipv4) {
7912 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7913 			v4setsrc = INADDR_ANY;
7914 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7915 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7916 			    NULL, NULL);
7917 		} else {
7918 			v6setsrc = ipv6_all_zeros;
7919 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7920 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7921 			    NULL, NULL);
7922 		}
7923 		ASSERT(ire != NULL);
7924 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7925 			ire_refrele(ire);
7926 			dir->dir_dreachable = 0;
7927 
7928 			/* move on to next dst addr */
7929 			continue;
7930 		}
7931 		dir->dir_dreachable = 1;
7932 
7933 		dst_ill = ire_nexthop_ill(ire);
7934 		if (dst_ill == NULL) {
7935 			ire_refrele(ire);
7936 			continue;
7937 		}
7938 
7939 		/* With ipmp we most likely look at the ipmp ill here */
7940 		dir->dir_dmactype = dst_ill->ill_mactype;
7941 
7942 		if (isipv4) {
7943 			ipaddr_t v4saddr;
7944 
7945 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7946 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7947 			    &v4saddr, NULL, &ipif_flags) != 0) {
7948 				v4saddr = INADDR_ANY;
7949 				ipif_flags = 0;
7950 			}
7951 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7952 		} else {
7953 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7954 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7955 			    saddr, NULL, &ipif_flags) != 0) {
7956 				*saddr = ipv6_all_zeros;
7957 				ipif_flags = 0;
7958 			}
7959 		}
7960 
7961 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7962 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7963 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7964 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7965 		ire_refrele(ire);
7966 		ill_refrele(dst_ill);
7967 	}
7968 	miocack(q, mp, iocp->ioc_count, 0);
7969 }
7970 
7971 /*
7972  * Check if this is an address assigned to this machine.
7973  * Skips interfaces that are down by using ire checks.
7974  * Translates mapped addresses to v4 addresses and then
7975  * treats them as such, returning true if the v4 address
7976  * associated with this mapped address is configured.
7977  * Note: Applications will have to be careful what they do
7978  * with the response; use of mapped addresses limits
7979  * what can be done with the socket, especially with
7980  * respect to socket options and ioctls - neither IPv4
7981  * options nor IPv6 sticky options/ancillary data options
7982  * may be used.
7983  */
7984 /* ARGSUSED */
7985 int
7986 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7987     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7988 {
7989 	struct sioc_addrreq *sia;
7990 	sin_t *sin;
7991 	ire_t *ire;
7992 	mblk_t *mp1;
7993 	zoneid_t zoneid;
7994 	ip_stack_t	*ipst;
7995 
7996 	ip1dbg(("ip_sioctl_tmyaddr"));
7997 
7998 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7999 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8000 	ipst = CONNQ_TO_IPST(q);
8001 
8002 	/* Existence verified in ip_wput_nondata */
8003 	mp1 = mp->b_cont->b_cont;
8004 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8005 	sin = (sin_t *)&sia->sa_addr;
8006 	switch (sin->sin_family) {
8007 	case AF_INET6: {
8008 		sin6_t *sin6 = (sin6_t *)sin;
8009 
8010 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8011 			ipaddr_t v4_addr;
8012 
8013 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8014 			    v4_addr);
8015 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8016 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8017 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8018 		} else {
8019 			in6_addr_t v6addr;
8020 
8021 			v6addr = sin6->sin6_addr;
8022 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8023 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8024 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8025 		}
8026 		break;
8027 	}
8028 	case AF_INET: {
8029 		ipaddr_t v4addr;
8030 
8031 		v4addr = sin->sin_addr.s_addr;
8032 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8033 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8034 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8035 		break;
8036 	}
8037 	default:
8038 		return (EAFNOSUPPORT);
8039 	}
8040 	if (ire != NULL) {
8041 		sia->sa_res = 1;
8042 		ire_refrele(ire);
8043 	} else {
8044 		sia->sa_res = 0;
8045 	}
8046 	return (0);
8047 }
8048 
8049 /*
8050  * Check if this is an address assigned on-link i.e. neighbor,
8051  * and makes sure it's reachable from the current zone.
8052  * Returns true for my addresses as well.
8053  * Translates mapped addresses to v4 addresses and then
8054  * treats them as such, returning true if the v4 address
8055  * associated with this mapped address is configured.
8056  * Note: Applications will have to be careful what they do
8057  * with the response; use of mapped addresses limits
8058  * what can be done with the socket, especially with
8059  * respect to socket options and ioctls - neither IPv4
8060  * options nor IPv6 sticky options/ancillary data options
8061  * may be used.
8062  */
8063 /* ARGSUSED */
8064 int
8065 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8066     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8067 {
8068 	struct sioc_addrreq *sia;
8069 	sin_t *sin;
8070 	mblk_t	*mp1;
8071 	ire_t *ire = NULL;
8072 	zoneid_t zoneid;
8073 	ip_stack_t	*ipst;
8074 
8075 	ip1dbg(("ip_sioctl_tonlink"));
8076 
8077 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8078 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8079 	ipst = CONNQ_TO_IPST(q);
8080 
8081 	/* Existence verified in ip_wput_nondata */
8082 	mp1 = mp->b_cont->b_cont;
8083 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8084 	sin = (sin_t *)&sia->sa_addr;
8085 
8086 	/*
8087 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8088 	 * to make sure we only look at on-link unicast address.
8089 	 */
8090 	switch (sin->sin_family) {
8091 	case AF_INET6: {
8092 		sin6_t *sin6 = (sin6_t *)sin;
8093 
8094 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8095 			ipaddr_t v4_addr;
8096 
8097 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8098 			    v4_addr);
8099 			if (!CLASSD(v4_addr)) {
8100 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8101 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8102 				    0, ipst, NULL);
8103 			}
8104 		} else {
8105 			in6_addr_t v6addr;
8106 
8107 			v6addr = sin6->sin6_addr;
8108 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8109 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8110 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8111 				    ipst, NULL);
8112 			}
8113 		}
8114 		break;
8115 	}
8116 	case AF_INET: {
8117 		ipaddr_t v4addr;
8118 
8119 		v4addr = sin->sin_addr.s_addr;
8120 		if (!CLASSD(v4addr)) {
8121 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8122 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8123 		}
8124 		break;
8125 	}
8126 	default:
8127 		return (EAFNOSUPPORT);
8128 	}
8129 	sia->sa_res = 0;
8130 	if (ire != NULL) {
8131 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8132 
8133 		if ((ire->ire_type & IRE_ONLINK) &&
8134 		    !(ire->ire_type & IRE_BROADCAST))
8135 			sia->sa_res = 1;
8136 		ire_refrele(ire);
8137 	}
8138 	return (0);
8139 }
8140 
8141 /*
8142  * TBD: implement when kernel maintaines a list of site prefixes.
8143  */
8144 /* ARGSUSED */
8145 int
8146 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8147     ip_ioctl_cmd_t *ipip, void *ifreq)
8148 {
8149 	return (ENXIO);
8150 }
8151 
8152 /* ARP IOCTLs. */
8153 /* ARGSUSED */
8154 int
8155 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8156     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8157 {
8158 	int		err;
8159 	ipaddr_t	ipaddr;
8160 	struct iocblk	*iocp;
8161 	conn_t		*connp;
8162 	struct arpreq	*ar;
8163 	struct xarpreq	*xar;
8164 	int		arp_flags, flags, alength;
8165 	uchar_t		*lladdr;
8166 	ip_stack_t	*ipst;
8167 	ill_t		*ill = ipif->ipif_ill;
8168 	ill_t		*proxy_ill = NULL;
8169 	ipmp_arpent_t	*entp = NULL;
8170 	boolean_t	proxyarp = B_FALSE;
8171 	boolean_t	if_arp_ioctl = B_FALSE;
8172 	ncec_t		*ncec = NULL;
8173 	nce_t		*nce;
8174 
8175 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8176 	connp = Q_TO_CONN(q);
8177 	ipst = connp->conn_netstack->netstack_ip;
8178 	iocp = (struct iocblk *)mp->b_rptr;
8179 
8180 	if (ipip->ipi_cmd_type == XARP_CMD) {
8181 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8182 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8183 		ar = NULL;
8184 
8185 		arp_flags = xar->xarp_flags;
8186 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8187 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8188 		/*
8189 		 * Validate against user's link layer address length
8190 		 * input and name and addr length limits.
8191 		 */
8192 		alength = ill->ill_phys_addr_length;
8193 		if (ipip->ipi_cmd == SIOCSXARP) {
8194 			if (alength != xar->xarp_ha.sdl_alen ||
8195 			    (alength + xar->xarp_ha.sdl_nlen >
8196 			    sizeof (xar->xarp_ha.sdl_data)))
8197 				return (EINVAL);
8198 		}
8199 	} else {
8200 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8201 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8202 		xar = NULL;
8203 
8204 		arp_flags = ar->arp_flags;
8205 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8206 		/*
8207 		 * Theoretically, the sa_family could tell us what link
8208 		 * layer type this operation is trying to deal with. By
8209 		 * common usage AF_UNSPEC means ethernet. We'll assume
8210 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8211 		 * for now. Our new SIOC*XARP ioctls can be used more
8212 		 * generally.
8213 		 *
8214 		 * If the underlying media happens to have a non 6 byte
8215 		 * address, arp module will fail set/get, but the del
8216 		 * operation will succeed.
8217 		 */
8218 		alength = 6;
8219 		if ((ipip->ipi_cmd != SIOCDARP) &&
8220 		    (alength != ill->ill_phys_addr_length)) {
8221 			return (EINVAL);
8222 		}
8223 	}
8224 
8225 	/* Translate ATF* flags to NCE* flags */
8226 	flags = 0;
8227 	if (arp_flags & ATF_AUTHORITY)
8228 		flags |= NCE_F_AUTHORITY;
8229 	if (arp_flags & ATF_PERM)
8230 		flags |= NCE_F_NONUD; /* not subject to aging */
8231 	if (arp_flags & ATF_PUBL)
8232 		flags |= NCE_F_PUBLISH;
8233 
8234 	/*
8235 	 * IPMP ARP special handling:
8236 	 *
8237 	 * 1. Since ARP mappings must appear consistent across the group,
8238 	 *    prohibit changing ARP mappings on the underlying interfaces.
8239 	 *
8240 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8241 	 *    IP itself, prohibit changing them.
8242 	 *
8243 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8244 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8245 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8246 	 */
8247 	if (IS_UNDER_IPMP(ill)) {
8248 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8249 			return (EPERM);
8250 	}
8251 	if (IS_IPMP(ill)) {
8252 		ipmp_illgrp_t *illg = ill->ill_grp;
8253 
8254 		switch (ipip->ipi_cmd) {
8255 		case SIOCSARP:
8256 		case SIOCSXARP:
8257 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8258 			if (proxy_ill != NULL) {
8259 				proxyarp = B_TRUE;
8260 				if (!ipmp_ill_is_active(proxy_ill))
8261 					proxy_ill = ipmp_illgrp_next_ill(illg);
8262 				if (proxy_ill != NULL)
8263 					lladdr = proxy_ill->ill_phys_addr;
8264 			}
8265 			/* FALLTHRU */
8266 		}
8267 	}
8268 
8269 	ipaddr = sin->sin_addr.s_addr;
8270 	/*
8271 	 * don't match across illgrp per case (1) and (2).
8272 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8273 	 */
8274 	nce = nce_lookup_v4(ill, &ipaddr);
8275 	if (nce != NULL)
8276 		ncec = nce->nce_common;
8277 
8278 	switch (iocp->ioc_cmd) {
8279 	case SIOCDARP:
8280 	case SIOCDXARP: {
8281 		/*
8282 		 * Delete the NCE if any.
8283 		 */
8284 		if (ncec == NULL) {
8285 			iocp->ioc_error = ENXIO;
8286 			break;
8287 		}
8288 		/* Don't allow changes to arp mappings of local addresses. */
8289 		if (NCE_MYADDR(ncec)) {
8290 			nce_refrele(nce);
8291 			return (ENOTSUP);
8292 		}
8293 		iocp->ioc_error = 0;
8294 
8295 		/*
8296 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8297 		 * This will delete all the nce entries on the under_ills.
8298 		 */
8299 		ncec_delete(ncec);
8300 		/*
8301 		 * Once the NCE has been deleted, then the ire_dep* consistency
8302 		 * mechanism will find any IRE which depended on the now
8303 		 * condemned NCE (as part of sending packets).
8304 		 * That mechanism handles redirects by deleting redirects
8305 		 * that refer to UNREACHABLE nces.
8306 		 */
8307 		break;
8308 	}
8309 	case SIOCGARP:
8310 	case SIOCGXARP:
8311 		if (ncec != NULL) {
8312 			lladdr = ncec->ncec_lladdr;
8313 			flags = ncec->ncec_flags;
8314 			iocp->ioc_error = 0;
8315 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8316 		} else {
8317 			iocp->ioc_error = ENXIO;
8318 		}
8319 		break;
8320 	case SIOCSARP:
8321 	case SIOCSXARP:
8322 		/* Don't allow changes to arp mappings of local addresses. */
8323 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8324 			nce_refrele(nce);
8325 			return (ENOTSUP);
8326 		}
8327 
8328 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8329 		flags |= NCE_F_STATIC;
8330 		if (!if_arp_ioctl) {
8331 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8332 			    lladdr, alength, flags);
8333 		} else {
8334 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8335 			if (ipif != NULL) {
8336 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8337 				    lladdr, alength, flags);
8338 				ipif_refrele(ipif);
8339 			}
8340 		}
8341 		if (nce != NULL) {
8342 			nce_refrele(nce);
8343 			nce = NULL;
8344 		}
8345 		/*
8346 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8347 		 * by nce_add_common()
8348 		 */
8349 		err = nce_lookup_then_add_v4(ill, lladdr,
8350 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8351 		    &nce);
8352 		if (err == EEXIST) {
8353 			ncec = nce->nce_common;
8354 			mutex_enter(&ncec->ncec_lock);
8355 			ncec->ncec_state = ND_REACHABLE;
8356 			ncec->ncec_flags = flags;
8357 			nce_update(ncec, ND_UNCHANGED, lladdr);
8358 			mutex_exit(&ncec->ncec_lock);
8359 			err = 0;
8360 		}
8361 		if (nce != NULL) {
8362 			nce_refrele(nce);
8363 			nce = NULL;
8364 		}
8365 		if (IS_IPMP(ill) && err == 0) {
8366 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8367 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8368 			    flags);
8369 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8370 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8371 				break;
8372 			}
8373 		}
8374 		iocp->ioc_error = err;
8375 	}
8376 
8377 	if (nce != NULL) {
8378 		nce_refrele(nce);
8379 	}
8380 
8381 	/*
8382 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8383 	 */
8384 	if (entp != NULL)
8385 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8386 
8387 	return (iocp->ioc_error);
8388 }
8389 
8390 /*
8391  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8392  * the associated sin and refhold and return the associated ipif via `ci'.
8393  */
8394 int
8395 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8396     cmd_info_t *ci)
8397 {
8398 	mblk_t	*mp1;
8399 	sin_t	*sin;
8400 	conn_t	*connp;
8401 	ipif_t	*ipif;
8402 	ire_t	*ire = NULL;
8403 	ill_t	*ill = NULL;
8404 	boolean_t exists;
8405 	ip_stack_t *ipst;
8406 	struct arpreq *ar;
8407 	struct xarpreq *xar;
8408 	struct sockaddr_dl *sdl;
8409 
8410 	/* ioctl comes down on a conn */
8411 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8412 	connp = Q_TO_CONN(q);
8413 	if (connp->conn_family == AF_INET6)
8414 		return (ENXIO);
8415 
8416 	ipst = connp->conn_netstack->netstack_ip;
8417 
8418 	/* Verified in ip_wput_nondata */
8419 	mp1 = mp->b_cont->b_cont;
8420 
8421 	if (ipip->ipi_cmd_type == XARP_CMD) {
8422 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8423 		xar = (struct xarpreq *)mp1->b_rptr;
8424 		sin = (sin_t *)&xar->xarp_pa;
8425 		sdl = &xar->xarp_ha;
8426 
8427 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8428 			return (ENXIO);
8429 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8430 			return (EINVAL);
8431 	} else {
8432 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8433 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8434 		ar = (struct arpreq *)mp1->b_rptr;
8435 		sin = (sin_t *)&ar->arp_pa;
8436 	}
8437 
8438 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8439 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8440 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8441 		if (ipif == NULL)
8442 			return (ENXIO);
8443 		if (ipif->ipif_id != 0) {
8444 			ipif_refrele(ipif);
8445 			return (ENXIO);
8446 		}
8447 	} else {
8448 		/*
8449 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8450 		 * of 0: use the IP address to find the ipif.  If the IP
8451 		 * address is an IPMP test address, ire_ftable_lookup() will
8452 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8453 		 */
8454 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8455 		    ipst);
8456 		if (ipif == NULL) {
8457 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8458 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8459 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8460 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8461 				if (ire != NULL)
8462 					ire_refrele(ire);
8463 				return (ENXIO);
8464 			}
8465 			ASSERT(ire != NULL && ill != NULL);
8466 			ipif = ill->ill_ipif;
8467 			ipif_refhold(ipif);
8468 			ire_refrele(ire);
8469 		}
8470 	}
8471 
8472 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8473 		ipif_refrele(ipif);
8474 		return (ENXIO);
8475 	}
8476 
8477 	ci->ci_sin = sin;
8478 	ci->ci_ipif = ipif;
8479 	return (0);
8480 }
8481 
8482 /*
8483  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8484  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8485  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8486  * up and thus an ill can join that illgrp.
8487  *
8488  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8489  * open()/close() primarily because close() is not allowed to fail or block
8490  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8491  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8492  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8493  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8494  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8495  * state if I_UNLINK didn't occur.
8496  *
8497  * Note that for each plumb/unplumb operation, we may end up here more than
8498  * once because of the way ifconfig works.  However, it's OK to link the same
8499  * illgrp more than once, or unlink an illgrp that's already unlinked.
8500  */
8501 static int
8502 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8503 {
8504 	int err;
8505 	ip_stack_t *ipst = ill->ill_ipst;
8506 
8507 	ASSERT(IS_IPMP(ill));
8508 	ASSERT(IAM_WRITER_ILL(ill));
8509 
8510 	switch (ioccmd) {
8511 	case I_LINK:
8512 		return (ENOTSUP);
8513 
8514 	case I_PLINK:
8515 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8516 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8517 		rw_exit(&ipst->ips_ipmp_lock);
8518 		break;
8519 
8520 	case I_PUNLINK:
8521 		/*
8522 		 * Require all UP ipifs be brought down prior to unlinking the
8523 		 * illgrp so any associated IREs (and other state) is torched.
8524 		 */
8525 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8526 			return (EBUSY);
8527 
8528 		/*
8529 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8530 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8531 		 * join this group.  Specifically: ills trying to join grab
8532 		 * ipmp_lock and bump a "pending join" counter checked by
8533 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8534 		 * joins can occur (since we have ipmp_lock).  Once we drop
8535 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8536 		 * find the illgrp (since we unlinked it) and will return
8537 		 * EAFNOSUPPORT.  This will then take them back through the
8538 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8539 		 * back through I_PLINK above.
8540 		 */
8541 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8542 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8543 		rw_exit(&ipst->ips_ipmp_lock);
8544 		return (err);
8545 	default:
8546 		break;
8547 	}
8548 	return (0);
8549 }
8550 
8551 /*
8552  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8553  * atomically set/clear the muxids. Also complete the ioctl by acking or
8554  * naking it.  Note that the code is structured such that the link type,
8555  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8556  * its clones use the persistent link, while pppd(1M) and perhaps many
8557  * other daemons may use non-persistent link.  When combined with some
8558  * ill_t states, linking and unlinking lower streams may be used as
8559  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8560  */
8561 /* ARGSUSED */
8562 void
8563 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8564 {
8565 	mblk_t		*mp1;
8566 	struct linkblk	*li;
8567 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8568 	int		err = 0;
8569 
8570 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8571 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8572 
8573 	mp1 = mp->b_cont;	/* This is the linkblk info */
8574 	li = (struct linkblk *)mp1->b_rptr;
8575 
8576 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8577 	if (err == EINPROGRESS)
8578 		return;
8579 done:
8580 	if (err == 0)
8581 		miocack(q, mp, 0, 0);
8582 	else
8583 		miocnak(q, mp, 0, err);
8584 
8585 	/* Conn was refheld in ip_sioctl_copyin_setup */
8586 	if (CONN_Q(q))
8587 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8588 }
8589 
8590 /*
8591  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8592  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8593  * module stream).  If `doconsist' is set, then do the extended consistency
8594  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8595  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8596  * an error code on failure.
8597  */
8598 static int
8599 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8600     struct linkblk *li)
8601 {
8602 	int		err = 0;
8603 	ill_t  		*ill;
8604 	queue_t		*ipwq, *dwq;
8605 	const char	*name;
8606 	struct qinit	*qinfo;
8607 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8608 	boolean_t	entered_ipsq = B_FALSE;
8609 	boolean_t	is_ip = B_FALSE;
8610 	arl_t		*arl;
8611 
8612 	/*
8613 	 * Walk the lower stream to verify it's the IP module stream.
8614 	 * The IP module is identified by its name, wput function,
8615 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8616 	 * (li->l_qbot) will not vanish until this ioctl completes.
8617 	 */
8618 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8619 		qinfo = ipwq->q_qinfo;
8620 		name = qinfo->qi_minfo->mi_idname;
8621 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8622 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8623 			is_ip = B_TRUE;
8624 			break;
8625 		}
8626 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8627 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8628 			break;
8629 		}
8630 	}
8631 
8632 	/*
8633 	 * If this isn't an IP module stream, bail.
8634 	 */
8635 	if (ipwq == NULL)
8636 		return (0);
8637 
8638 	if (!is_ip) {
8639 		arl = (arl_t *)ipwq->q_ptr;
8640 		ill = arl_to_ill(arl);
8641 		if (ill == NULL)
8642 			return (0);
8643 	} else {
8644 		ill = ipwq->q_ptr;
8645 	}
8646 	ASSERT(ill != NULL);
8647 
8648 	if (ipsq == NULL) {
8649 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8650 		    NEW_OP, B_FALSE);
8651 		if (ipsq == NULL) {
8652 			if (!is_ip)
8653 				ill_refrele(ill);
8654 			return (EINPROGRESS);
8655 		}
8656 		entered_ipsq = B_TRUE;
8657 	}
8658 	ASSERT(IAM_WRITER_ILL(ill));
8659 	mutex_enter(&ill->ill_lock);
8660 	if (!is_ip) {
8661 		if (islink && ill->ill_muxid == 0) {
8662 			/*
8663 			 * Plumbing has to be done with IP plumbed first, arp
8664 			 * second, but here we have arp being plumbed first.
8665 			 */
8666 			mutex_exit(&ill->ill_lock);
8667 			ipsq_exit(ipsq);
8668 			ill_refrele(ill);
8669 			return (EINVAL);
8670 		}
8671 	}
8672 	mutex_exit(&ill->ill_lock);
8673 	if (!is_ip) {
8674 		arl->arl_muxid = islink ? li->l_index : 0;
8675 		ill_refrele(ill);
8676 		goto done;
8677 	}
8678 
8679 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8680 		goto done;
8681 
8682 	/*
8683 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8684 	 * the read queue of the module immediately below IP in the ill.
8685 	 * These are used during the capability negotiation below.
8686 	 */
8687 	ill->ill_lmod_rq = NULL;
8688 	ill->ill_lmod_cnt = 0;
8689 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8690 		ill->ill_lmod_rq = RD(dwq);
8691 		for (; dwq != NULL; dwq = dwq->q_next)
8692 			ill->ill_lmod_cnt++;
8693 	}
8694 
8695 	ill->ill_muxid = islink ? li->l_index : 0;
8696 
8697 	/*
8698 	 * Mark the ipsq busy until the capability operations initiated below
8699 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8700 	 * returns, but the capability operation may complete asynchronously
8701 	 * much later.
8702 	 */
8703 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8704 	/*
8705 	 * If there's at least one up ipif on this ill, then we're bound to
8706 	 * the underlying driver via DLPI.  In that case, renegotiate
8707 	 * capabilities to account for any possible change in modules
8708 	 * interposed between IP and the driver.
8709 	 */
8710 	if (ill->ill_ipif_up_count > 0) {
8711 		if (islink)
8712 			ill_capability_probe(ill);
8713 		else
8714 			ill_capability_reset(ill, B_FALSE);
8715 	}
8716 	ipsq_current_finish(ipsq);
8717 done:
8718 	if (entered_ipsq)
8719 		ipsq_exit(ipsq);
8720 
8721 	return (err);
8722 }
8723 
8724 /*
8725  * Search the ioctl command in the ioctl tables and return a pointer
8726  * to the ioctl command information. The ioctl command tables are
8727  * static and fully populated at compile time.
8728  */
8729 ip_ioctl_cmd_t *
8730 ip_sioctl_lookup(int ioc_cmd)
8731 {
8732 	int index;
8733 	ip_ioctl_cmd_t *ipip;
8734 	ip_ioctl_cmd_t *ipip_end;
8735 
8736 	if (ioc_cmd == IPI_DONTCARE)
8737 		return (NULL);
8738 
8739 	/*
8740 	 * Do a 2 step search. First search the indexed table
8741 	 * based on the least significant byte of the ioctl cmd.
8742 	 * If we don't find a match, then search the misc table
8743 	 * serially.
8744 	 */
8745 	index = ioc_cmd & 0xFF;
8746 	if (index < ip_ndx_ioctl_count) {
8747 		ipip = &ip_ndx_ioctl_table[index];
8748 		if (ipip->ipi_cmd == ioc_cmd) {
8749 			/* Found a match in the ndx table */
8750 			return (ipip);
8751 		}
8752 	}
8753 
8754 	/* Search the misc table */
8755 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8756 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8757 		if (ipip->ipi_cmd == ioc_cmd)
8758 			/* Found a match in the misc table */
8759 			return (ipip);
8760 	}
8761 
8762 	return (NULL);
8763 }
8764 
8765 /*
8766  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8767  */
8768 static boolean_t
8769 getset_ioctl_checks(mblk_t *mp)
8770 {
8771 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8772 	mblk_t		*mp1 = mp->b_cont;
8773 	mod_ioc_prop_t	*pioc;
8774 	uint_t		flags;
8775 	uint_t		pioc_size;
8776 
8777 	/* do sanity checks on various arguments */
8778 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8779 	    iocp->ioc_count == TRANSPARENT) {
8780 		return (B_FALSE);
8781 	}
8782 	if (msgdsize(mp1) < iocp->ioc_count) {
8783 		if (!pullupmsg(mp1, iocp->ioc_count))
8784 			return (B_FALSE);
8785 	}
8786 
8787 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8788 
8789 	/* sanity checks on mpr_valsize */
8790 	pioc_size = sizeof (mod_ioc_prop_t);
8791 	if (pioc->mpr_valsize != 0)
8792 		pioc_size += pioc->mpr_valsize - 1;
8793 
8794 	if (iocp->ioc_count != pioc_size)
8795 		return (B_FALSE);
8796 
8797 	flags = pioc->mpr_flags;
8798 	if (iocp->ioc_cmd == SIOCSETPROP) {
8799 		/*
8800 		 * One can either reset the value to it's default value or
8801 		 * change the current value or append/remove the value from
8802 		 * a multi-valued properties.
8803 		 */
8804 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8805 		    flags != MOD_PROP_ACTIVE &&
8806 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8807 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8808 			return (B_FALSE);
8809 	} else {
8810 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8811 
8812 		/*
8813 		 * One can retrieve only one kind of property information
8814 		 * at a time.
8815 		 */
8816 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8817 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8818 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8819 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8820 			return (B_FALSE);
8821 	}
8822 
8823 	return (B_TRUE);
8824 }
8825 
8826 /*
8827  * process the SIOC{SET|GET}PROP ioctl's
8828  */
8829 /* ARGSUSED */
8830 static void
8831 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8832 {
8833 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8834 	mblk_t		*mp1 = mp->b_cont;
8835 	mod_ioc_prop_t	*pioc;
8836 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8837 	ip_stack_t	*ipst;
8838 	icmp_stack_t	*is;
8839 	tcp_stack_t	*tcps;
8840 	sctp_stack_t	*sctps;
8841 	udp_stack_t	*us;
8842 	netstack_t	*stack;
8843 	void		*cbarg;
8844 	cred_t		*cr;
8845 	boolean_t 	set;
8846 	int		err;
8847 
8848 	ASSERT(q->q_next == NULL);
8849 	ASSERT(CONN_Q(q));
8850 
8851 	if (!getset_ioctl_checks(mp)) {
8852 		miocnak(q, mp, 0, EINVAL);
8853 		return;
8854 	}
8855 	ipst = CONNQ_TO_IPST(q);
8856 	stack = ipst->ips_netstack;
8857 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8858 
8859 	switch (pioc->mpr_proto) {
8860 	case MOD_PROTO_IP:
8861 	case MOD_PROTO_IPV4:
8862 	case MOD_PROTO_IPV6:
8863 		ptbl = ipst->ips_propinfo_tbl;
8864 		cbarg = ipst;
8865 		break;
8866 	case MOD_PROTO_RAWIP:
8867 		is = stack->netstack_icmp;
8868 		ptbl = is->is_propinfo_tbl;
8869 		cbarg = is;
8870 		break;
8871 	case MOD_PROTO_TCP:
8872 		tcps = stack->netstack_tcp;
8873 		ptbl = tcps->tcps_propinfo_tbl;
8874 		cbarg = tcps;
8875 		break;
8876 	case MOD_PROTO_UDP:
8877 		us = stack->netstack_udp;
8878 		ptbl = us->us_propinfo_tbl;
8879 		cbarg = us;
8880 		break;
8881 	case MOD_PROTO_SCTP:
8882 		sctps = stack->netstack_sctp;
8883 		ptbl = sctps->sctps_propinfo_tbl;
8884 		cbarg = sctps;
8885 		break;
8886 	default:
8887 		miocnak(q, mp, 0, EINVAL);
8888 		return;
8889 	}
8890 
8891 	/* search for given property in respective protocol propinfo table */
8892 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8893 		if (strcmp(pinfo->mpi_name, pioc->mpr_name) == 0 &&
8894 		    pinfo->mpi_proto == pioc->mpr_proto)
8895 			break;
8896 	}
8897 	if (pinfo->mpi_name == NULL) {
8898 		miocnak(q, mp, 0, ENOENT);
8899 		return;
8900 	}
8901 
8902 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8903 	if (set && pinfo->mpi_setf != NULL) {
8904 		cr = msg_getcred(mp, NULL);
8905 		if (cr == NULL)
8906 			cr = iocp->ioc_cr;
8907 		err = pinfo->mpi_setf(cbarg, cr, pinfo, pioc->mpr_ifname,
8908 		    pioc->mpr_val, pioc->mpr_flags);
8909 	} else if (!set && pinfo->mpi_getf != NULL) {
8910 		err = pinfo->mpi_getf(cbarg, pinfo, pioc->mpr_ifname,
8911 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8912 	} else {
8913 		err = EPERM;
8914 	}
8915 
8916 	if (err != 0) {
8917 		miocnak(q, mp, 0, err);
8918 	} else {
8919 		if (set)
8920 			miocack(q, mp, 0, 0);
8921 		else    /* For get, we need to return back the data */
8922 			miocack(q, mp, iocp->ioc_count, 0);
8923 	}
8924 }
8925 
8926 /*
8927  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8928  * as several routing daemons have unfortunately used this 'unpublished'
8929  * but well-known ioctls.
8930  */
8931 /* ARGSUSED */
8932 static void
8933 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8934 {
8935 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8936 	mblk_t		*mp1 = mp->b_cont;
8937 	char		*pname, *pval, *buf;
8938 	uint_t		bufsize, proto;
8939 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8940 	ip_stack_t	*ipst;
8941 	int		err = 0;
8942 
8943 	ASSERT(CONN_Q(q));
8944 	ipst = CONNQ_TO_IPST(q);
8945 
8946 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8947 		miocnak(q, mp, 0, EINVAL);
8948 		return;
8949 	}
8950 
8951 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8952 	pval = buf = pname = (char *)mp1->b_rptr;
8953 	bufsize = MBLKL(mp1);
8954 
8955 	if (strcmp(pname, "ip_forwarding") == 0) {
8956 		pname = "forwarding";
8957 		proto = MOD_PROTO_IPV4;
8958 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8959 		pname = "forwarding";
8960 		proto = MOD_PROTO_IPV6;
8961 	} else {
8962 		miocnak(q, mp, 0, EINVAL);
8963 		return;
8964 	}
8965 
8966 	ptbl = ipst->ips_propinfo_tbl;
8967 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8968 		if (strcmp(pinfo->mpi_name, pname) == 0 &&
8969 		    pinfo->mpi_proto == proto)
8970 			break;
8971 	}
8972 
8973 	ASSERT(pinfo->mpi_name != NULL);
8974 
8975 	switch (iocp->ioc_cmd) {
8976 	case ND_GET:
8977 		if ((err = pinfo->mpi_getf(ipst, pinfo, NULL, buf, bufsize,
8978 		    0)) == 0) {
8979 			miocack(q, mp, iocp->ioc_count, 0);
8980 			return;
8981 		}
8982 		break;
8983 	case ND_SET:
8984 		/*
8985 		 * buffer will have property name and value in the following
8986 		 * format,
8987 		 * <property name>'\0'<property value>'\0', extract them;
8988 		 */
8989 		while (*pval++)
8990 			noop;
8991 
8992 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8993 			err = EINVAL;
8994 		} else if ((err = pinfo->mpi_setf(ipst, NULL, pinfo, NULL,
8995 		    pval, 0)) == 0) {
8996 			miocack(q, mp, 0, 0);
8997 			return;
8998 		}
8999 		break;
9000 	default:
9001 		err = EINVAL;
9002 		break;
9003 	}
9004 	miocnak(q, mp, 0, err);
9005 }
9006 
9007 /*
9008  * Wrapper function for resuming deferred ioctl processing
9009  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9010  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9011  */
9012 /* ARGSUSED */
9013 void
9014 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9015     void *dummy_arg)
9016 {
9017 	ip_sioctl_copyin_setup(q, mp);
9018 }
9019 
9020 /*
9021  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9022  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9023  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9024  * We establish here the size of the block to be copied in.  mi_copyin
9025  * arranges for this to happen, an processing continues in ip_wput_nondata with
9026  * an M_IOCDATA message.
9027  */
9028 void
9029 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9030 {
9031 	int	copyin_size;
9032 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9033 	ip_ioctl_cmd_t *ipip;
9034 	cred_t *cr;
9035 	ip_stack_t	*ipst;
9036 
9037 	if (CONN_Q(q))
9038 		ipst = CONNQ_TO_IPST(q);
9039 	else
9040 		ipst = ILLQ_TO_IPST(q);
9041 
9042 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9043 	if (ipip == NULL) {
9044 		/*
9045 		 * The ioctl is not one we understand or own.
9046 		 * Pass it along to be processed down stream,
9047 		 * if this is a module instance of IP, else nak
9048 		 * the ioctl.
9049 		 */
9050 		if (q->q_next == NULL) {
9051 			goto nak;
9052 		} else {
9053 			putnext(q, mp);
9054 			return;
9055 		}
9056 	}
9057 
9058 	/*
9059 	 * If this is deferred, then we will do all the checks when we
9060 	 * come back.
9061 	 */
9062 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9063 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9064 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9065 		return;
9066 	}
9067 
9068 	/*
9069 	 * Only allow a very small subset of IP ioctls on this stream if
9070 	 * IP is a module and not a driver. Allowing ioctls to be processed
9071 	 * in this case may cause assert failures or data corruption.
9072 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9073 	 * ioctls allowed on an IP module stream, after which this stream
9074 	 * normally becomes a multiplexor (at which time the stream head
9075 	 * will fail all ioctls).
9076 	 */
9077 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9078 		goto nak;
9079 	}
9080 
9081 	/* Make sure we have ioctl data to process. */
9082 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9083 		goto nak;
9084 
9085 	/*
9086 	 * Prefer dblk credential over ioctl credential; some synthesized
9087 	 * ioctls have kcred set because there's no way to crhold()
9088 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9089 	 * the framework; the caller of ioctl needs to hold the reference
9090 	 * for the duration of the call).
9091 	 */
9092 	cr = msg_getcred(mp, NULL);
9093 	if (cr == NULL)
9094 		cr = iocp->ioc_cr;
9095 
9096 	/* Make sure normal users don't send down privileged ioctls */
9097 	if ((ipip->ipi_flags & IPI_PRIV) &&
9098 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9099 		/* We checked the privilege earlier but log it here */
9100 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9101 		return;
9102 	}
9103 
9104 	/*
9105 	 * The ioctl command tables can only encode fixed length
9106 	 * ioctl data. If the length is variable, the table will
9107 	 * encode the length as zero. Such special cases are handled
9108 	 * below in the switch.
9109 	 */
9110 	if (ipip->ipi_copyin_size != 0) {
9111 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9112 		return;
9113 	}
9114 
9115 	switch (iocp->ioc_cmd) {
9116 	case O_SIOCGIFCONF:
9117 	case SIOCGIFCONF:
9118 		/*
9119 		 * This IOCTL is hilarious.  See comments in
9120 		 * ip_sioctl_get_ifconf for the story.
9121 		 */
9122 		if (iocp->ioc_count == TRANSPARENT)
9123 			copyin_size = SIZEOF_STRUCT(ifconf,
9124 			    iocp->ioc_flag);
9125 		else
9126 			copyin_size = iocp->ioc_count;
9127 		mi_copyin(q, mp, NULL, copyin_size);
9128 		return;
9129 
9130 	case O_SIOCGLIFCONF:
9131 	case SIOCGLIFCONF:
9132 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9133 		mi_copyin(q, mp, NULL, copyin_size);
9134 		return;
9135 
9136 	case SIOCGLIFSRCOF:
9137 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9138 		mi_copyin(q, mp, NULL, copyin_size);
9139 		return;
9140 
9141 	case SIOCGIP6ADDRPOLICY:
9142 		ip_sioctl_ip6addrpolicy(q, mp);
9143 		ip6_asp_table_refrele(ipst);
9144 		return;
9145 
9146 	case SIOCSIP6ADDRPOLICY:
9147 		ip_sioctl_ip6addrpolicy(q, mp);
9148 		return;
9149 
9150 	case SIOCGDSTINFO:
9151 		ip_sioctl_dstinfo(q, mp);
9152 		ip6_asp_table_refrele(ipst);
9153 		return;
9154 
9155 	case ND_SET:
9156 	case ND_GET:
9157 		ip_process_legacy_nddprop(q, mp);
9158 		return;
9159 
9160 	case SIOCSETPROP:
9161 	case SIOCGETPROP:
9162 		ip_sioctl_getsetprop(q, mp);
9163 		return;
9164 
9165 	case I_PLINK:
9166 	case I_PUNLINK:
9167 	case I_LINK:
9168 	case I_UNLINK:
9169 		/*
9170 		 * We treat non-persistent link similarly as the persistent
9171 		 * link case, in terms of plumbing/unplumbing, as well as
9172 		 * dynamic re-plumbing events indicator.  See comments
9173 		 * in ip_sioctl_plink() for more.
9174 		 *
9175 		 * Request can be enqueued in the 'ipsq' while waiting
9176 		 * to become exclusive. So bump up the conn ref.
9177 		 */
9178 		if (CONN_Q(q))
9179 			CONN_INC_REF(Q_TO_CONN(q));
9180 		ip_sioctl_plink(NULL, q, mp, NULL);
9181 		return;
9182 
9183 	case IP_IOCTL:
9184 		ip_wput_ioctl(q, mp);
9185 		return;
9186 
9187 	case SIOCILB:
9188 		/* The ioctl length varies depending on the ILB command. */
9189 		copyin_size = iocp->ioc_count;
9190 		if (copyin_size < sizeof (ilb_cmd_t))
9191 			goto nak;
9192 		mi_copyin(q, mp, NULL, copyin_size);
9193 		return;
9194 
9195 	default:
9196 		cmn_err(CE_PANIC, "should not happen ");
9197 	}
9198 nak:
9199 	if (mp->b_cont != NULL) {
9200 		freemsg(mp->b_cont);
9201 		mp->b_cont = NULL;
9202 	}
9203 	iocp->ioc_error = EINVAL;
9204 	mp->b_datap->db_type = M_IOCNAK;
9205 	iocp->ioc_count = 0;
9206 	qreply(q, mp);
9207 }
9208 
9209 static void
9210 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9211 {
9212 	struct arpreq *ar;
9213 	struct xarpreq *xar;
9214 	mblk_t	*tmp;
9215 	struct iocblk *iocp;
9216 	int x_arp_ioctl = B_FALSE;
9217 	int *flagsp;
9218 	char *storage = NULL;
9219 
9220 	ASSERT(ill != NULL);
9221 
9222 	iocp = (struct iocblk *)mp->b_rptr;
9223 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9224 
9225 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9226 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9227 	    (iocp->ioc_cmd == SIOCSXARP)) {
9228 		x_arp_ioctl = B_TRUE;
9229 		xar = (struct xarpreq *)tmp->b_rptr;
9230 		flagsp = &xar->xarp_flags;
9231 		storage = xar->xarp_ha.sdl_data;
9232 	} else {
9233 		ar = (struct arpreq *)tmp->b_rptr;
9234 		flagsp = &ar->arp_flags;
9235 		storage = ar->arp_ha.sa_data;
9236 	}
9237 
9238 	/*
9239 	 * We're done if this is not an SIOCG{X}ARP
9240 	 */
9241 	if (x_arp_ioctl) {
9242 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9243 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9244 		    sizeof (xar->xarp_ha.sdl_data)) {
9245 			iocp->ioc_error = EINVAL;
9246 			return;
9247 		}
9248 	}
9249 	*flagsp = ATF_INUSE;
9250 	/*
9251 	 * If /sbin/arp told us we are the authority using the "permanent"
9252 	 * flag, or if this is one of my addresses print "permanent"
9253 	 * in the /sbin/arp output.
9254 	 */
9255 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9256 		*flagsp |= ATF_AUTHORITY;
9257 	if (flags & NCE_F_NONUD)
9258 		*flagsp |= ATF_PERM; /* not subject to aging */
9259 	if (flags & NCE_F_PUBLISH)
9260 		*flagsp |= ATF_PUBL;
9261 	if (hwaddr != NULL) {
9262 		*flagsp |= ATF_COM;
9263 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9264 	}
9265 }
9266 
9267 /*
9268  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9269  * interface) create the next available logical interface for this
9270  * physical interface.
9271  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9272  * ipif with the specified name.
9273  *
9274  * If the address family is not AF_UNSPEC then set the address as well.
9275  *
9276  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9277  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9278  *
9279  * Executed as a writer on the ill.
9280  * So no lock is needed to traverse the ipif chain, or examine the
9281  * phyint flags.
9282  */
9283 /* ARGSUSED */
9284 int
9285 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9286     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9287 {
9288 	mblk_t	*mp1;
9289 	struct lifreq *lifr;
9290 	boolean_t	isv6;
9291 	boolean_t	exists;
9292 	char 	*name;
9293 	char	*endp;
9294 	char	*cp;
9295 	int	namelen;
9296 	ipif_t	*ipif;
9297 	long	id;
9298 	ipsq_t	*ipsq;
9299 	ill_t	*ill;
9300 	sin_t	*sin;
9301 	int	err = 0;
9302 	boolean_t found_sep = B_FALSE;
9303 	conn_t	*connp;
9304 	zoneid_t zoneid;
9305 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9306 
9307 	ASSERT(q->q_next == NULL);
9308 	ip1dbg(("ip_sioctl_addif\n"));
9309 	/* Existence of mp1 has been checked in ip_wput_nondata */
9310 	mp1 = mp->b_cont->b_cont;
9311 	/*
9312 	 * Null terminate the string to protect against buffer
9313 	 * overrun. String was generated by user code and may not
9314 	 * be trusted.
9315 	 */
9316 	lifr = (struct lifreq *)mp1->b_rptr;
9317 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9318 	name = lifr->lifr_name;
9319 	ASSERT(CONN_Q(q));
9320 	connp = Q_TO_CONN(q);
9321 	isv6 = (connp->conn_family == AF_INET6);
9322 	zoneid = connp->conn_zoneid;
9323 	namelen = mi_strlen(name);
9324 	if (namelen == 0)
9325 		return (EINVAL);
9326 
9327 	exists = B_FALSE;
9328 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9329 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9330 		/*
9331 		 * Allow creating lo0 using SIOCLIFADDIF.
9332 		 * can't be any other writer thread. So can pass null below
9333 		 * for the last 4 args to ipif_lookup_name.
9334 		 */
9335 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9336 		    &exists, isv6, zoneid, ipst);
9337 		/* Prevent any further action */
9338 		if (ipif == NULL) {
9339 			return (ENOBUFS);
9340 		} else if (!exists) {
9341 			/* We created the ipif now and as writer */
9342 			ipif_refrele(ipif);
9343 			return (0);
9344 		} else {
9345 			ill = ipif->ipif_ill;
9346 			ill_refhold(ill);
9347 			ipif_refrele(ipif);
9348 		}
9349 	} else {
9350 		/* Look for a colon in the name. */
9351 		endp = &name[namelen];
9352 		for (cp = endp; --cp > name; ) {
9353 			if (*cp == IPIF_SEPARATOR_CHAR) {
9354 				found_sep = B_TRUE;
9355 				/*
9356 				 * Reject any non-decimal aliases for plumbing
9357 				 * of logical interfaces. Aliases with leading
9358 				 * zeroes are also rejected as they introduce
9359 				 * ambiguity in the naming of the interfaces.
9360 				 * Comparing with "0" takes care of all such
9361 				 * cases.
9362 				 */
9363 				if ((strncmp("0", cp+1, 1)) == 0)
9364 					return (EINVAL);
9365 
9366 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9367 				    id <= 0 || *endp != '\0') {
9368 					return (EINVAL);
9369 				}
9370 				*cp = '\0';
9371 				break;
9372 			}
9373 		}
9374 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9375 		if (found_sep)
9376 			*cp = IPIF_SEPARATOR_CHAR;
9377 		if (ill == NULL)
9378 			return (ENXIO);
9379 	}
9380 
9381 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9382 	    B_TRUE);
9383 
9384 	/*
9385 	 * Release the refhold due to the lookup, now that we are excl
9386 	 * or we are just returning
9387 	 */
9388 	ill_refrele(ill);
9389 
9390 	if (ipsq == NULL)
9391 		return (EINPROGRESS);
9392 
9393 	/* We are now exclusive on the IPSQ */
9394 	ASSERT(IAM_WRITER_ILL(ill));
9395 
9396 	if (found_sep) {
9397 		/* Now see if there is an IPIF with this unit number. */
9398 		for (ipif = ill->ill_ipif; ipif != NULL;
9399 		    ipif = ipif->ipif_next) {
9400 			if (ipif->ipif_id == id) {
9401 				err = EEXIST;
9402 				goto done;
9403 			}
9404 		}
9405 	}
9406 
9407 	/*
9408 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9409 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9410 	 * instead.
9411 	 */
9412 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9413 	    B_TRUE, B_TRUE, &err)) == NULL) {
9414 		goto done;
9415 	}
9416 
9417 	/* Return created name with ioctl */
9418 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9419 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9420 	ip1dbg(("created %s\n", lifr->lifr_name));
9421 
9422 	/* Set address */
9423 	sin = (sin_t *)&lifr->lifr_addr;
9424 	if (sin->sin_family != AF_UNSPEC) {
9425 		err = ip_sioctl_addr(ipif, sin, q, mp,
9426 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9427 	}
9428 
9429 done:
9430 	ipsq_exit(ipsq);
9431 	return (err);
9432 }
9433 
9434 /*
9435  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9436  * interface) delete it based on the IP address (on this physical interface).
9437  * Otherwise delete it based on the ipif_id.
9438  * Also, special handling to allow a removeif of lo0.
9439  */
9440 /* ARGSUSED */
9441 int
9442 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9443     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9444 {
9445 	conn_t		*connp;
9446 	ill_t		*ill = ipif->ipif_ill;
9447 	boolean_t	 success;
9448 	ip_stack_t	*ipst;
9449 
9450 	ipst = CONNQ_TO_IPST(q);
9451 
9452 	ASSERT(q->q_next == NULL);
9453 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9454 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9455 	ASSERT(IAM_WRITER_IPIF(ipif));
9456 
9457 	connp = Q_TO_CONN(q);
9458 	/*
9459 	 * Special case for unplumbing lo0 (the loopback physical interface).
9460 	 * If unplumbing lo0, the incoming address structure has been
9461 	 * initialized to all zeros. When unplumbing lo0, all its logical
9462 	 * interfaces must be removed too.
9463 	 *
9464 	 * Note that this interface may be called to remove a specific
9465 	 * loopback logical interface (eg, lo0:1). But in that case
9466 	 * ipif->ipif_id != 0 so that the code path for that case is the
9467 	 * same as any other interface (meaning it skips the code directly
9468 	 * below).
9469 	 */
9470 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9471 		if (sin->sin_family == AF_UNSPEC &&
9472 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9473 			/*
9474 			 * Mark it condemned. No new ref. will be made to ill.
9475 			 */
9476 			mutex_enter(&ill->ill_lock);
9477 			ill->ill_state_flags |= ILL_CONDEMNED;
9478 			for (ipif = ill->ill_ipif; ipif != NULL;
9479 			    ipif = ipif->ipif_next) {
9480 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9481 			}
9482 			mutex_exit(&ill->ill_lock);
9483 
9484 			ipif = ill->ill_ipif;
9485 			/* unplumb the loopback interface */
9486 			ill_delete(ill);
9487 			mutex_enter(&connp->conn_lock);
9488 			mutex_enter(&ill->ill_lock);
9489 
9490 			/* Are any references to this ill active */
9491 			if (ill_is_freeable(ill)) {
9492 				mutex_exit(&ill->ill_lock);
9493 				mutex_exit(&connp->conn_lock);
9494 				ill_delete_tail(ill);
9495 				mi_free(ill);
9496 				return (0);
9497 			}
9498 			success = ipsq_pending_mp_add(connp, ipif,
9499 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9500 			mutex_exit(&connp->conn_lock);
9501 			mutex_exit(&ill->ill_lock);
9502 			if (success)
9503 				return (EINPROGRESS);
9504 			else
9505 				return (EINTR);
9506 		}
9507 	}
9508 
9509 	if (ipif->ipif_id == 0) {
9510 		ipsq_t *ipsq;
9511 
9512 		/* Find based on address */
9513 		if (ipif->ipif_isv6) {
9514 			sin6_t *sin6;
9515 
9516 			if (sin->sin_family != AF_INET6)
9517 				return (EAFNOSUPPORT);
9518 
9519 			sin6 = (sin6_t *)sin;
9520 			/* We are a writer, so we should be able to lookup */
9521 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9522 			    ipst);
9523 		} else {
9524 			if (sin->sin_family != AF_INET)
9525 				return (EAFNOSUPPORT);
9526 
9527 			/* We are a writer, so we should be able to lookup */
9528 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9529 			    ipst);
9530 		}
9531 		if (ipif == NULL) {
9532 			return (EADDRNOTAVAIL);
9533 		}
9534 
9535 		/*
9536 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9537 		 * lifr_name of the physical interface but with an ip address
9538 		 * lifr_addr of a logical interface plumbed over it.
9539 		 * So update ipx_current_ipif now that ipif points to the
9540 		 * correct one.
9541 		 */
9542 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9543 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9544 
9545 		/* This is a writer */
9546 		ipif_refrele(ipif);
9547 	}
9548 
9549 	/*
9550 	 * Can not delete instance zero since it is tied to the ill.
9551 	 */
9552 	if (ipif->ipif_id == 0)
9553 		return (EBUSY);
9554 
9555 	mutex_enter(&ill->ill_lock);
9556 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9557 	mutex_exit(&ill->ill_lock);
9558 
9559 	ipif_free(ipif);
9560 
9561 	mutex_enter(&connp->conn_lock);
9562 	mutex_enter(&ill->ill_lock);
9563 
9564 	/* Are any references to this ipif active */
9565 	if (ipif_is_freeable(ipif)) {
9566 		mutex_exit(&ill->ill_lock);
9567 		mutex_exit(&connp->conn_lock);
9568 		ipif_non_duplicate(ipif);
9569 		(void) ipif_down_tail(ipif);
9570 		ipif_free_tail(ipif); /* frees ipif */
9571 		return (0);
9572 	}
9573 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9574 	    IPIF_FREE);
9575 	mutex_exit(&ill->ill_lock);
9576 	mutex_exit(&connp->conn_lock);
9577 	if (success)
9578 		return (EINPROGRESS);
9579 	else
9580 		return (EINTR);
9581 }
9582 
9583 /*
9584  * Restart the removeif ioctl. The refcnt has gone down to 0.
9585  * The ipif is already condemned. So can't find it thru lookups.
9586  */
9587 /* ARGSUSED */
9588 int
9589 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9590     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9591 {
9592 	ill_t *ill = ipif->ipif_ill;
9593 
9594 	ASSERT(IAM_WRITER_IPIF(ipif));
9595 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9596 
9597 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9598 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9599 
9600 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9601 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9602 		ill_delete_tail(ill);
9603 		mi_free(ill);
9604 		return (0);
9605 	}
9606 
9607 	ipif_non_duplicate(ipif);
9608 	(void) ipif_down_tail(ipif);
9609 	ipif_free_tail(ipif);
9610 
9611 	return (0);
9612 }
9613 
9614 /*
9615  * Set the local interface address using the given prefix and ill_token.
9616  */
9617 /* ARGSUSED */
9618 int
9619 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9620     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9621 {
9622 	int err;
9623 	in6_addr_t v6addr;
9624 	sin6_t *sin6;
9625 	ill_t *ill;
9626 	int i;
9627 
9628 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9629 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9630 
9631 	ASSERT(IAM_WRITER_IPIF(ipif));
9632 
9633 	if (!ipif->ipif_isv6)
9634 		return (EINVAL);
9635 
9636 	if (sin->sin_family != AF_INET6)
9637 		return (EAFNOSUPPORT);
9638 
9639 	sin6 = (sin6_t *)sin;
9640 	v6addr = sin6->sin6_addr;
9641 	ill = ipif->ipif_ill;
9642 
9643 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9644 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9645 		return (EADDRNOTAVAIL);
9646 
9647 	for (i = 0; i < 4; i++)
9648 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9649 
9650 	err = ip_sioctl_addr(ipif, sin, q, mp,
9651 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9652 	return (err);
9653 }
9654 
9655 /*
9656  * Restart entry point to restart the address set operation after the
9657  * refcounts have dropped to zero.
9658  */
9659 /* ARGSUSED */
9660 int
9661 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9662     ip_ioctl_cmd_t *ipip, void *ifreq)
9663 {
9664 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9665 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9666 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9667 }
9668 
9669 /*
9670  * Set the local interface address.
9671  * Allow an address of all zero when the interface is down.
9672  */
9673 /* ARGSUSED */
9674 int
9675 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9676     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9677 {
9678 	int err = 0;
9679 	in6_addr_t v6addr;
9680 	boolean_t need_up = B_FALSE;
9681 
9682 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9683 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9684 
9685 	ASSERT(IAM_WRITER_IPIF(ipif));
9686 
9687 	if (ipif->ipif_isv6) {
9688 		sin6_t *sin6;
9689 		ill_t *ill;
9690 		phyint_t *phyi;
9691 
9692 		if (sin->sin_family != AF_INET6)
9693 			return (EAFNOSUPPORT);
9694 
9695 		sin6 = (sin6_t *)sin;
9696 		v6addr = sin6->sin6_addr;
9697 		ill = ipif->ipif_ill;
9698 		phyi = ill->ill_phyint;
9699 
9700 		/*
9701 		 * Enforce that true multicast interfaces have a link-local
9702 		 * address for logical unit 0.
9703 		 *
9704 		 * However for those ipif's for which link-local address was
9705 		 * not created by default, also allow setting :: as the address.
9706 		 * This scenario would arise, when we delete an address on ipif
9707 		 * with logical unit 0, we would want to set :: as the address.
9708 		 */
9709 		if (ipif->ipif_id == 0 &&
9710 		    (ill->ill_flags & ILLF_MULTICAST) &&
9711 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9712 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9713 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9714 
9715 			/*
9716 			 * if default link-local was not created by kernel for
9717 			 * this ill, allow setting :: as the address on ipif:0.
9718 			 */
9719 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9720 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9721 					return (EADDRNOTAVAIL);
9722 			} else {
9723 				return (EADDRNOTAVAIL);
9724 			}
9725 		}
9726 
9727 		/*
9728 		 * up interfaces shouldn't have the unspecified address
9729 		 * unless they also have the IPIF_NOLOCAL flags set and
9730 		 * have a subnet assigned.
9731 		 */
9732 		if ((ipif->ipif_flags & IPIF_UP) &&
9733 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9734 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9735 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9736 			return (EADDRNOTAVAIL);
9737 		}
9738 
9739 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9740 			return (EADDRNOTAVAIL);
9741 	} else {
9742 		ipaddr_t addr;
9743 
9744 		if (sin->sin_family != AF_INET)
9745 			return (EAFNOSUPPORT);
9746 
9747 		addr = sin->sin_addr.s_addr;
9748 
9749 		/* Allow INADDR_ANY as the local address. */
9750 		if (addr != INADDR_ANY &&
9751 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9752 			return (EADDRNOTAVAIL);
9753 
9754 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9755 	}
9756 
9757 	/*
9758 	 * Even if there is no change we redo things just to rerun
9759 	 * ipif_set_default.
9760 	 */
9761 	if (ipif->ipif_flags & IPIF_UP) {
9762 		/*
9763 		 * Setting a new local address, make sure
9764 		 * we have net and subnet bcast ire's for
9765 		 * the old address if we need them.
9766 		 */
9767 		/*
9768 		 * If the interface is already marked up,
9769 		 * we call ipif_down which will take care
9770 		 * of ditching any IREs that have been set
9771 		 * up based on the old interface address.
9772 		 */
9773 		err = ipif_logical_down(ipif, q, mp);
9774 		if (err == EINPROGRESS)
9775 			return (err);
9776 		(void) ipif_down_tail(ipif);
9777 		need_up = 1;
9778 	}
9779 
9780 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9781 	return (err);
9782 }
9783 
9784 int
9785 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9786     boolean_t need_up)
9787 {
9788 	in6_addr_t v6addr;
9789 	in6_addr_t ov6addr;
9790 	ipaddr_t addr;
9791 	sin6_t	*sin6;
9792 	int	sinlen;
9793 	int	err = 0;
9794 	ill_t	*ill = ipif->ipif_ill;
9795 	boolean_t need_dl_down;
9796 	boolean_t need_arp_down;
9797 	struct iocblk *iocp;
9798 
9799 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9800 
9801 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9802 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9803 	ASSERT(IAM_WRITER_IPIF(ipif));
9804 
9805 	/* Must cancel any pending timer before taking the ill_lock */
9806 	if (ipif->ipif_recovery_id != 0)
9807 		(void) untimeout(ipif->ipif_recovery_id);
9808 	ipif->ipif_recovery_id = 0;
9809 
9810 	if (ipif->ipif_isv6) {
9811 		sin6 = (sin6_t *)sin;
9812 		v6addr = sin6->sin6_addr;
9813 		sinlen = sizeof (struct sockaddr_in6);
9814 	} else {
9815 		addr = sin->sin_addr.s_addr;
9816 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9817 		sinlen = sizeof (struct sockaddr_in);
9818 	}
9819 	mutex_enter(&ill->ill_lock);
9820 	ov6addr = ipif->ipif_v6lcl_addr;
9821 	ipif->ipif_v6lcl_addr = v6addr;
9822 	sctp_update_ipif_addr(ipif, ov6addr);
9823 	ipif->ipif_addr_ready = 0;
9824 
9825 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9826 
9827 	/*
9828 	 * If the interface was previously marked as a duplicate, then since
9829 	 * we've now got a "new" address, it should no longer be considered a
9830 	 * duplicate -- even if the "new" address is the same as the old one.
9831 	 * Note that if all ipifs are down, we may have a pending ARP down
9832 	 * event to handle.  This is because we want to recover from duplicates
9833 	 * and thus delay tearing down ARP until the duplicates have been
9834 	 * removed or disabled.
9835 	 */
9836 	need_dl_down = need_arp_down = B_FALSE;
9837 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9838 		need_arp_down = !need_up;
9839 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9840 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9841 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9842 			need_dl_down = B_TRUE;
9843 		}
9844 	}
9845 
9846 	ipif_set_default(ipif);
9847 
9848 	/*
9849 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9850 	 * tag the ill so that future updates to the interface ID don't result
9851 	 * in this address getting automatically reconfigured from under the
9852 	 * administrator.
9853 	 */
9854 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9855 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9856 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9857 			ill->ill_manual_linklocal = 1;
9858 	}
9859 
9860 	/*
9861 	 * When publishing an interface address change event, we only notify
9862 	 * the event listeners of the new address.  It is assumed that if they
9863 	 * actively care about the addresses assigned that they will have
9864 	 * already discovered the previous address assigned (if there was one.)
9865 	 *
9866 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9867 	 */
9868 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9869 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9870 		    NE_ADDRESS_CHANGE, sin, sinlen);
9871 	}
9872 
9873 	mutex_exit(&ill->ill_lock);
9874 
9875 	if (need_up) {
9876 		/*
9877 		 * Now bring the interface back up.  If this
9878 		 * is the only IPIF for the ILL, ipif_up
9879 		 * will have to re-bind to the device, so
9880 		 * we may get back EINPROGRESS, in which
9881 		 * case, this IOCTL will get completed in
9882 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9883 		 */
9884 		err = ipif_up(ipif, q, mp);
9885 	} else {
9886 		/* Perhaps ilgs should use this ill */
9887 		update_conn_ill(NULL, ill->ill_ipst);
9888 	}
9889 
9890 	if (need_dl_down)
9891 		ill_dl_down(ill);
9892 
9893 	if (need_arp_down && !ill->ill_isv6)
9894 		(void) ipif_arp_down(ipif);
9895 
9896 	/*
9897 	 * The default multicast interface might have changed (for
9898 	 * instance if the IPv6 scope of the address changed)
9899 	 */
9900 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9901 
9902 	return (err);
9903 }
9904 
9905 /*
9906  * Restart entry point to restart the address set operation after the
9907  * refcounts have dropped to zero.
9908  */
9909 /* ARGSUSED */
9910 int
9911 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9912     ip_ioctl_cmd_t *ipip, void *ifreq)
9913 {
9914 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9915 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9916 	ASSERT(IAM_WRITER_IPIF(ipif));
9917 	(void) ipif_down_tail(ipif);
9918 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9919 }
9920 
9921 /* ARGSUSED */
9922 int
9923 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9924     ip_ioctl_cmd_t *ipip, void *if_req)
9925 {
9926 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9927 	struct lifreq *lifr = (struct lifreq *)if_req;
9928 
9929 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9930 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9931 	/*
9932 	 * The net mask and address can't change since we have a
9933 	 * reference to the ipif. So no lock is necessary.
9934 	 */
9935 	if (ipif->ipif_isv6) {
9936 		*sin6 = sin6_null;
9937 		sin6->sin6_family = AF_INET6;
9938 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9939 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9940 		lifr->lifr_addrlen =
9941 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9942 	} else {
9943 		*sin = sin_null;
9944 		sin->sin_family = AF_INET;
9945 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9946 		if (ipip->ipi_cmd_type == LIF_CMD) {
9947 			lifr->lifr_addrlen =
9948 			    ip_mask_to_plen(ipif->ipif_net_mask);
9949 		}
9950 	}
9951 	return (0);
9952 }
9953 
9954 /*
9955  * Set the destination address for a pt-pt interface.
9956  */
9957 /* ARGSUSED */
9958 int
9959 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9960     ip_ioctl_cmd_t *ipip, void *if_req)
9961 {
9962 	int err = 0;
9963 	in6_addr_t v6addr;
9964 	boolean_t need_up = B_FALSE;
9965 
9966 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9967 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9968 	ASSERT(IAM_WRITER_IPIF(ipif));
9969 
9970 	if (ipif->ipif_isv6) {
9971 		sin6_t *sin6;
9972 
9973 		if (sin->sin_family != AF_INET6)
9974 			return (EAFNOSUPPORT);
9975 
9976 		sin6 = (sin6_t *)sin;
9977 		v6addr = sin6->sin6_addr;
9978 
9979 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9980 			return (EADDRNOTAVAIL);
9981 	} else {
9982 		ipaddr_t addr;
9983 
9984 		if (sin->sin_family != AF_INET)
9985 			return (EAFNOSUPPORT);
9986 
9987 		addr = sin->sin_addr.s_addr;
9988 		if (addr != INADDR_ANY &&
9989 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
9990 			return (EADDRNOTAVAIL);
9991 		}
9992 
9993 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9994 	}
9995 
9996 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9997 		return (0);	/* No change */
9998 
9999 	if (ipif->ipif_flags & IPIF_UP) {
10000 		/*
10001 		 * If the interface is already marked up,
10002 		 * we call ipif_down which will take care
10003 		 * of ditching any IREs that have been set
10004 		 * up based on the old pp dst address.
10005 		 */
10006 		err = ipif_logical_down(ipif, q, mp);
10007 		if (err == EINPROGRESS)
10008 			return (err);
10009 		(void) ipif_down_tail(ipif);
10010 		need_up = B_TRUE;
10011 	}
10012 	/*
10013 	 * could return EINPROGRESS. If so ioctl will complete in
10014 	 * ip_rput_dlpi_writer
10015 	 */
10016 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10017 	return (err);
10018 }
10019 
10020 static int
10021 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10022     boolean_t need_up)
10023 {
10024 	in6_addr_t v6addr;
10025 	ill_t	*ill = ipif->ipif_ill;
10026 	int	err = 0;
10027 	boolean_t need_dl_down;
10028 	boolean_t need_arp_down;
10029 
10030 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10031 	    ipif->ipif_id, (void *)ipif));
10032 
10033 	/* Must cancel any pending timer before taking the ill_lock */
10034 	if (ipif->ipif_recovery_id != 0)
10035 		(void) untimeout(ipif->ipif_recovery_id);
10036 	ipif->ipif_recovery_id = 0;
10037 
10038 	if (ipif->ipif_isv6) {
10039 		sin6_t *sin6;
10040 
10041 		sin6 = (sin6_t *)sin;
10042 		v6addr = sin6->sin6_addr;
10043 	} else {
10044 		ipaddr_t addr;
10045 
10046 		addr = sin->sin_addr.s_addr;
10047 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10048 	}
10049 	mutex_enter(&ill->ill_lock);
10050 	/* Set point to point destination address. */
10051 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10052 		/*
10053 		 * Allow this as a means of creating logical
10054 		 * pt-pt interfaces on top of e.g. an Ethernet.
10055 		 * XXX Undocumented HACK for testing.
10056 		 * pt-pt interfaces are created with NUD disabled.
10057 		 */
10058 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10059 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10060 		if (ipif->ipif_isv6)
10061 			ill->ill_flags |= ILLF_NONUD;
10062 	}
10063 
10064 	/*
10065 	 * If the interface was previously marked as a duplicate, then since
10066 	 * we've now got a "new" address, it should no longer be considered a
10067 	 * duplicate -- even if the "new" address is the same as the old one.
10068 	 * Note that if all ipifs are down, we may have a pending ARP down
10069 	 * event to handle.
10070 	 */
10071 	need_dl_down = need_arp_down = B_FALSE;
10072 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10073 		need_arp_down = !need_up;
10074 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10075 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10076 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10077 			need_dl_down = B_TRUE;
10078 		}
10079 	}
10080 
10081 	/*
10082 	 * If we've just manually set the IPv6 destination link-local address
10083 	 * (0th ipif), tag the ill so that future updates to the destination
10084 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10085 	 * result in this address getting automatically reconfigured from
10086 	 * under the administrator.
10087 	 */
10088 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10089 		ill->ill_manual_dst_linklocal = 1;
10090 
10091 	/* Set the new address. */
10092 	ipif->ipif_v6pp_dst_addr = v6addr;
10093 	/* Make sure subnet tracks pp_dst */
10094 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10095 	mutex_exit(&ill->ill_lock);
10096 
10097 	if (need_up) {
10098 		/*
10099 		 * Now bring the interface back up.  If this
10100 		 * is the only IPIF for the ILL, ipif_up
10101 		 * will have to re-bind to the device, so
10102 		 * we may get back EINPROGRESS, in which
10103 		 * case, this IOCTL will get completed in
10104 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10105 		 */
10106 		err = ipif_up(ipif, q, mp);
10107 	}
10108 
10109 	if (need_dl_down)
10110 		ill_dl_down(ill);
10111 	if (need_arp_down && !ipif->ipif_isv6)
10112 		(void) ipif_arp_down(ipif);
10113 
10114 	return (err);
10115 }
10116 
10117 /*
10118  * Restart entry point to restart the dstaddress set operation after the
10119  * refcounts have dropped to zero.
10120  */
10121 /* ARGSUSED */
10122 int
10123 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10124     ip_ioctl_cmd_t *ipip, void *ifreq)
10125 {
10126 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10128 	(void) ipif_down_tail(ipif);
10129 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10130 }
10131 
10132 /* ARGSUSED */
10133 int
10134 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10135     ip_ioctl_cmd_t *ipip, void *if_req)
10136 {
10137 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10138 
10139 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10140 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10141 	/*
10142 	 * Get point to point destination address. The addresses can't
10143 	 * change since we hold a reference to the ipif.
10144 	 */
10145 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10146 		return (EADDRNOTAVAIL);
10147 
10148 	if (ipif->ipif_isv6) {
10149 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10150 		*sin6 = sin6_null;
10151 		sin6->sin6_family = AF_INET6;
10152 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10153 	} else {
10154 		*sin = sin_null;
10155 		sin->sin_family = AF_INET;
10156 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10157 	}
10158 	return (0);
10159 }
10160 
10161 /*
10162  * Check which flags will change by the given flags being set
10163  * silently ignore flags which userland is not allowed to control.
10164  * (Because these flags may change between SIOCGLIFFLAGS and
10165  * SIOCSLIFFLAGS, and that's outside of userland's control,
10166  * we need to silently ignore them rather than fail.)
10167  */
10168 static void
10169 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10170     uint64_t *offp)
10171 {
10172 	ill_t		*ill = ipif->ipif_ill;
10173 	phyint_t 	*phyi = ill->ill_phyint;
10174 	uint64_t	cantchange_flags, intf_flags;
10175 	uint64_t	turn_on, turn_off;
10176 
10177 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10178 	cantchange_flags = IFF_CANTCHANGE;
10179 	if (IS_IPMP(ill))
10180 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10181 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10182 	turn_off = intf_flags & turn_on;
10183 	turn_on ^= turn_off;
10184 	*onp = turn_on;
10185 	*offp = turn_off;
10186 }
10187 
10188 /*
10189  * Set interface flags.  Many flags require special handling (e.g.,
10190  * bringing the interface down); see below for details.
10191  *
10192  * NOTE : We really don't enforce that ipif_id zero should be used
10193  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10194  *	  is because applications generally does SICGLIFFLAGS and
10195  *	  ORs in the new flags (that affects the logical) and does a
10196  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10197  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10198  *	  flags that will be turned on is correct with respect to
10199  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10200  */
10201 /* ARGSUSED */
10202 int
10203 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10204     ip_ioctl_cmd_t *ipip, void *if_req)
10205 {
10206 	uint64_t turn_on;
10207 	uint64_t turn_off;
10208 	int	err = 0;
10209 	phyint_t *phyi;
10210 	ill_t *ill;
10211 	conn_t *connp;
10212 	uint64_t intf_flags;
10213 	boolean_t phyint_flags_modified = B_FALSE;
10214 	uint64_t flags;
10215 	struct ifreq *ifr;
10216 	struct lifreq *lifr;
10217 	boolean_t set_linklocal = B_FALSE;
10218 
10219 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10220 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10221 
10222 	ASSERT(IAM_WRITER_IPIF(ipif));
10223 
10224 	ill = ipif->ipif_ill;
10225 	phyi = ill->ill_phyint;
10226 
10227 	if (ipip->ipi_cmd_type == IF_CMD) {
10228 		ifr = (struct ifreq *)if_req;
10229 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10230 	} else {
10231 		lifr = (struct lifreq *)if_req;
10232 		flags = lifr->lifr_flags;
10233 	}
10234 
10235 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10236 
10237 	/*
10238 	 * Have the flags been set correctly until now?
10239 	 */
10240 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10241 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10242 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10243 	/*
10244 	 * Compare the new flags to the old, and partition
10245 	 * into those coming on and those going off.
10246 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10247 	 */
10248 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10249 		flags |= intf_flags & ~0xFFFF;
10250 
10251 	/*
10252 	 * Explicitly fail attempts to change flags that are always invalid on
10253 	 * an IPMP meta-interface.
10254 	 */
10255 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10256 		return (EINVAL);
10257 
10258 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10259 	if ((turn_on|turn_off) == 0)
10260 		return (0);	/* No change */
10261 
10262 	/*
10263 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10264 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10265 	 * allow it to be turned off.
10266 	 */
10267 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10268 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10269 		return (EINVAL);
10270 
10271 	if ((connp = Q_TO_CONN(q)) == NULL)
10272 		return (EINVAL);
10273 
10274 	/*
10275 	 * Only vrrp control socket is allowed to change IFF_UP and
10276 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10277 	 */
10278 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10279 		if (!connp->conn_isvrrp)
10280 			return (EINVAL);
10281 	}
10282 
10283 	/*
10284 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10285 	 * VRRP control socket.
10286 	 */
10287 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10288 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10289 			return (EINVAL);
10290 	}
10291 
10292 	if (turn_on & IFF_NOFAILOVER) {
10293 		turn_on |= IFF_DEPRECATED;
10294 		flags |= IFF_DEPRECATED;
10295 	}
10296 
10297 	/*
10298 	 * On underlying interfaces, only allow applications to manage test
10299 	 * addresses -- otherwise, they may get confused when the address
10300 	 * moves as part of being brought up.  Likewise, prevent an
10301 	 * application-managed test address from being converted to a data
10302 	 * address.  To prevent migration of administratively up addresses in
10303 	 * the kernel, we don't allow them to be converted either.
10304 	 */
10305 	if (IS_UNDER_IPMP(ill)) {
10306 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10307 
10308 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10309 			return (EINVAL);
10310 
10311 		if ((turn_off & IFF_NOFAILOVER) &&
10312 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10313 			return (EINVAL);
10314 	}
10315 
10316 	/*
10317 	 * Only allow IFF_TEMPORARY flag to be set on
10318 	 * IPv6 interfaces.
10319 	 */
10320 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10321 		return (EINVAL);
10322 
10323 	/*
10324 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10325 	 */
10326 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10327 		return (EINVAL);
10328 
10329 	/*
10330 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10331 	 * interfaces.  It makes no sense in that context.
10332 	 */
10333 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10334 		return (EINVAL);
10335 
10336 	/*
10337 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10338 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10339 	 * If the link local address isn't set, and can be set, it will get
10340 	 * set later on in this function.
10341 	 */
10342 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10343 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10344 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10345 		if (ipif_cant_setlinklocal(ipif))
10346 			return (EINVAL);
10347 		set_linklocal = B_TRUE;
10348 	}
10349 
10350 	/*
10351 	 * If we modify physical interface flags, we'll potentially need to
10352 	 * send up two routing socket messages for the changes (one for the
10353 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10354 	 */
10355 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10356 		phyint_flags_modified = B_TRUE;
10357 
10358 	/*
10359 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10360 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10361 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10362 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10363 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10364 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10365 	 * will not be honored.
10366 	 */
10367 	if (turn_on & PHYI_STANDBY) {
10368 		/*
10369 		 * No need to grab ill_g_usesrc_lock here; see the
10370 		 * synchronization notes in ip.c.
10371 		 */
10372 		if (ill->ill_usesrc_grp_next != NULL ||
10373 		    intf_flags & PHYI_INACTIVE)
10374 			return (EINVAL);
10375 		if (!(flags & PHYI_FAILED)) {
10376 			flags |= PHYI_INACTIVE;
10377 			turn_on |= PHYI_INACTIVE;
10378 		}
10379 	}
10380 
10381 	if (turn_off & PHYI_STANDBY) {
10382 		flags &= ~PHYI_INACTIVE;
10383 		turn_off |= PHYI_INACTIVE;
10384 	}
10385 
10386 	/*
10387 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10388 	 * would end up on.
10389 	 */
10390 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10391 	    (PHYI_FAILED | PHYI_INACTIVE))
10392 		return (EINVAL);
10393 
10394 	/*
10395 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10396 	 * status of the interface.
10397 	 */
10398 	if ((turn_on | turn_off) & ILLF_ROUTER)
10399 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10400 
10401 	/*
10402 	 * If the interface is not UP and we are not going to
10403 	 * bring it UP, record the flags and return. When the
10404 	 * interface comes UP later, the right actions will be
10405 	 * taken.
10406 	 */
10407 	if (!(ipif->ipif_flags & IPIF_UP) &&
10408 	    !(turn_on & IPIF_UP)) {
10409 		/* Record new flags in their respective places. */
10410 		mutex_enter(&ill->ill_lock);
10411 		mutex_enter(&ill->ill_phyint->phyint_lock);
10412 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10413 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10414 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10415 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10416 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10417 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10418 		mutex_exit(&ill->ill_lock);
10419 		mutex_exit(&ill->ill_phyint->phyint_lock);
10420 
10421 		/*
10422 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10423 		 * same to the kernel: if any of them has been set by
10424 		 * userland, the interface cannot be used for data traffic.
10425 		 */
10426 		if ((turn_on|turn_off) &
10427 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10428 			ASSERT(!IS_IPMP(ill));
10429 			/*
10430 			 * It's possible the ill is part of an "anonymous"
10431 			 * IPMP group rather than a real group.  In that case,
10432 			 * there are no other interfaces in the group and thus
10433 			 * no need to call ipmp_phyint_refresh_active().
10434 			 */
10435 			if (IS_UNDER_IPMP(ill))
10436 				ipmp_phyint_refresh_active(phyi);
10437 		}
10438 
10439 		if (phyint_flags_modified) {
10440 			if (phyi->phyint_illv4 != NULL) {
10441 				ip_rts_ifmsg(phyi->phyint_illv4->
10442 				    ill_ipif, RTSQ_DEFAULT);
10443 			}
10444 			if (phyi->phyint_illv6 != NULL) {
10445 				ip_rts_ifmsg(phyi->phyint_illv6->
10446 				    ill_ipif, RTSQ_DEFAULT);
10447 			}
10448 		}
10449 		/* The default multicast interface might have changed */
10450 		ire_increment_multicast_generation(ill->ill_ipst,
10451 		    ill->ill_isv6);
10452 
10453 		return (0);
10454 	} else if (set_linklocal) {
10455 		mutex_enter(&ill->ill_lock);
10456 		if (set_linklocal)
10457 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10458 		mutex_exit(&ill->ill_lock);
10459 	}
10460 
10461 	/*
10462 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10463 	 * or point-to-point interfaces with an unspecified destination. We do
10464 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10465 	 * have a subnet assigned, which is how in.ndpd currently manages its
10466 	 * onlink prefix list when no addresses are configured with those
10467 	 * prefixes.
10468 	 */
10469 	if (ipif->ipif_isv6 &&
10470 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10471 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10472 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10473 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10474 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10475 		return (EINVAL);
10476 	}
10477 
10478 	/*
10479 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10480 	 * from being brought up.
10481 	 */
10482 	if (!ipif->ipif_isv6 &&
10483 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10484 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10485 		return (EINVAL);
10486 	}
10487 
10488 	/*
10489 	 * If we are going to change one or more of the flags that are
10490 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10491 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10492 	 * IPIF_NOFAILOVER, we will take special action.  This is
10493 	 * done by bring the ipif down, changing the flags and bringing
10494 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10495 	 * back up will trigger the address to be moved.
10496 	 *
10497 	 * If we are going to change IFF_NOACCEPT, we need to bring
10498 	 * all the ipifs down then bring them up again.	 The act of
10499 	 * bringing all the ipifs back up will trigger the local
10500 	 * ires being recreated with "no_accept" set/cleared.
10501 	 *
10502 	 * Note that ILLF_NOACCEPT is always set separately from the
10503 	 * other flags.
10504 	 */
10505 	if ((turn_on|turn_off) &
10506 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10507 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10508 	    IPIF_NOFAILOVER)) {
10509 		/*
10510 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10511 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10512 		 * entries shared between multiple ipifs on the same subnet.
10513 		 */
10514 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10515 		    !(turn_off & IPIF_UP)) {
10516 			if (ipif->ipif_flags & IPIF_UP)
10517 				ill->ill_logical_down = 1;
10518 			turn_on &= ~IPIF_UP;
10519 		}
10520 		err = ipif_down(ipif, q, mp);
10521 		ip1dbg(("ipif_down returns %d err ", err));
10522 		if (err == EINPROGRESS)
10523 			return (err);
10524 		(void) ipif_down_tail(ipif);
10525 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10526 		/*
10527 		 * If we can quiesce the ill, then continue.  If not, then
10528 		 * ip_sioctl_flags_tail() will be called from
10529 		 * ipif_ill_refrele_tail().
10530 		 */
10531 		ill_down_ipifs(ill, B_TRUE);
10532 
10533 		mutex_enter(&connp->conn_lock);
10534 		mutex_enter(&ill->ill_lock);
10535 		if (!ill_is_quiescent(ill)) {
10536 			boolean_t success;
10537 
10538 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10539 			    q, mp, ILL_DOWN);
10540 			mutex_exit(&ill->ill_lock);
10541 			mutex_exit(&connp->conn_lock);
10542 			return (success ? EINPROGRESS : EINTR);
10543 		}
10544 		mutex_exit(&ill->ill_lock);
10545 		mutex_exit(&connp->conn_lock);
10546 	}
10547 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10548 }
10549 
10550 static int
10551 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10552 {
10553 	ill_t	*ill;
10554 	phyint_t *phyi;
10555 	uint64_t turn_on, turn_off;
10556 	boolean_t phyint_flags_modified = B_FALSE;
10557 	int	err = 0;
10558 	boolean_t set_linklocal = B_FALSE;
10559 
10560 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10561 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10562 
10563 	ASSERT(IAM_WRITER_IPIF(ipif));
10564 
10565 	ill = ipif->ipif_ill;
10566 	phyi = ill->ill_phyint;
10567 
10568 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10569 
10570 	/*
10571 	 * IFF_UP is handled separately.
10572 	 */
10573 	turn_on &= ~IFF_UP;
10574 	turn_off &= ~IFF_UP;
10575 
10576 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10577 		phyint_flags_modified = B_TRUE;
10578 
10579 	/*
10580 	 * Now we change the flags. Track current value of
10581 	 * other flags in their respective places.
10582 	 */
10583 	mutex_enter(&ill->ill_lock);
10584 	mutex_enter(&phyi->phyint_lock);
10585 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10586 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10587 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10588 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10589 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10590 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10591 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10592 		set_linklocal = B_TRUE;
10593 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10594 	}
10595 
10596 	mutex_exit(&ill->ill_lock);
10597 	mutex_exit(&phyi->phyint_lock);
10598 
10599 	if (set_linklocal)
10600 		(void) ipif_setlinklocal(ipif);
10601 
10602 	/*
10603 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10604 	 * the kernel: if any of them has been set by userland, the interface
10605 	 * cannot be used for data traffic.
10606 	 */
10607 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10608 		ASSERT(!IS_IPMP(ill));
10609 		/*
10610 		 * It's possible the ill is part of an "anonymous" IPMP group
10611 		 * rather than a real group.  In that case, there are no other
10612 		 * interfaces in the group and thus no need for us to call
10613 		 * ipmp_phyint_refresh_active().
10614 		 */
10615 		if (IS_UNDER_IPMP(ill))
10616 			ipmp_phyint_refresh_active(phyi);
10617 	}
10618 
10619 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10620 		/*
10621 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10622 		 * ipifs that were brought down.
10623 		 *
10624 		 * The routing sockets messages are sent as the result
10625 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10626 		 * as well.
10627 		 */
10628 		err = ill_up_ipifs(ill, q, mp);
10629 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10630 		/*
10631 		 * XXX ipif_up really does not know whether a phyint flags
10632 		 * was modified or not. So, it sends up information on
10633 		 * only one routing sockets message. As we don't bring up
10634 		 * the interface and also set PHYI_ flags simultaneously
10635 		 * it should be okay.
10636 		 */
10637 		err = ipif_up(ipif, q, mp);
10638 	} else {
10639 		/*
10640 		 * Make sure routing socket sees all changes to the flags.
10641 		 * ipif_up_done* handles this when we use ipif_up.
10642 		 */
10643 		if (phyint_flags_modified) {
10644 			if (phyi->phyint_illv4 != NULL) {
10645 				ip_rts_ifmsg(phyi->phyint_illv4->
10646 				    ill_ipif, RTSQ_DEFAULT);
10647 			}
10648 			if (phyi->phyint_illv6 != NULL) {
10649 				ip_rts_ifmsg(phyi->phyint_illv6->
10650 				    ill_ipif, RTSQ_DEFAULT);
10651 			}
10652 		} else {
10653 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10654 		}
10655 		/*
10656 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10657 		 * this in need_up case.
10658 		 */
10659 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10660 	}
10661 
10662 	/* The default multicast interface might have changed */
10663 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10664 	return (err);
10665 }
10666 
10667 /*
10668  * Restart the flags operation now that the refcounts have dropped to zero.
10669  */
10670 /* ARGSUSED */
10671 int
10672 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10673     ip_ioctl_cmd_t *ipip, void *if_req)
10674 {
10675 	uint64_t flags;
10676 	struct ifreq *ifr = if_req;
10677 	struct lifreq *lifr = if_req;
10678 	uint64_t turn_on, turn_off;
10679 
10680 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10681 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10682 
10683 	if (ipip->ipi_cmd_type == IF_CMD) {
10684 		/* cast to uint16_t prevents unwanted sign extension */
10685 		flags = (uint16_t)ifr->ifr_flags;
10686 	} else {
10687 		flags = lifr->lifr_flags;
10688 	}
10689 
10690 	/*
10691 	 * If this function call is a result of the ILLF_NOACCEPT flag
10692 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10693 	 */
10694 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10695 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10696 		(void) ipif_down_tail(ipif);
10697 
10698 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10699 }
10700 
10701 /*
10702  * Can operate on either a module or a driver queue.
10703  */
10704 /* ARGSUSED */
10705 int
10706 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10707     ip_ioctl_cmd_t *ipip, void *if_req)
10708 {
10709 	/*
10710 	 * Has the flags been set correctly till now ?
10711 	 */
10712 	ill_t *ill = ipif->ipif_ill;
10713 	phyint_t *phyi = ill->ill_phyint;
10714 
10715 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10716 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10717 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10718 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10719 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10720 
10721 	/*
10722 	 * Need a lock since some flags can be set even when there are
10723 	 * references to the ipif.
10724 	 */
10725 	mutex_enter(&ill->ill_lock);
10726 	if (ipip->ipi_cmd_type == IF_CMD) {
10727 		struct ifreq *ifr = (struct ifreq *)if_req;
10728 
10729 		/* Get interface flags (low 16 only). */
10730 		ifr->ifr_flags = ((ipif->ipif_flags |
10731 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10732 	} else {
10733 		struct lifreq *lifr = (struct lifreq *)if_req;
10734 
10735 		/* Get interface flags. */
10736 		lifr->lifr_flags = ipif->ipif_flags |
10737 		    ill->ill_flags | phyi->phyint_flags;
10738 	}
10739 	mutex_exit(&ill->ill_lock);
10740 	return (0);
10741 }
10742 
10743 /*
10744  * We allow the MTU to be set on an ILL, but not have it be different
10745  * for different IPIFs since we don't actually send packets on IPIFs.
10746  */
10747 /* ARGSUSED */
10748 int
10749 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10750     ip_ioctl_cmd_t *ipip, void *if_req)
10751 {
10752 	int mtu;
10753 	int ip_min_mtu;
10754 	struct ifreq	*ifr;
10755 	struct lifreq *lifr;
10756 	ill_t	*ill;
10757 
10758 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10759 	    ipif->ipif_id, (void *)ipif));
10760 	if (ipip->ipi_cmd_type == IF_CMD) {
10761 		ifr = (struct ifreq *)if_req;
10762 		mtu = ifr->ifr_metric;
10763 	} else {
10764 		lifr = (struct lifreq *)if_req;
10765 		mtu = lifr->lifr_mtu;
10766 	}
10767 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10768 	if (ipif->ipif_id != 0)
10769 		return (EINVAL);
10770 
10771 	ill = ipif->ipif_ill;
10772 	if (ipif->ipif_isv6)
10773 		ip_min_mtu = IPV6_MIN_MTU;
10774 	else
10775 		ip_min_mtu = IP_MIN_MTU;
10776 
10777 	mutex_enter(&ill->ill_lock);
10778 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10779 		mutex_exit(&ill->ill_lock);
10780 		return (EINVAL);
10781 	}
10782 	/*
10783 	 * The dce and fragmentation code can handle changes to ill_mtu
10784 	 * concurrent with sending/fragmenting packets.
10785 	 */
10786 	ill->ill_mtu = mtu;
10787 	ill->ill_flags |= ILLF_FIXEDMTU;
10788 	mutex_exit(&ill->ill_lock);
10789 
10790 	/*
10791 	 * Make sure all dce_generation checks find out
10792 	 * that ill_mtu has changed.
10793 	 */
10794 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10795 
10796 	/* Update the MTU in SCTP's list */
10797 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10798 	return (0);
10799 }
10800 
10801 /* Get interface MTU. */
10802 /* ARGSUSED */
10803 int
10804 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10805 	ip_ioctl_cmd_t *ipip, void *if_req)
10806 {
10807 	struct ifreq	*ifr;
10808 	struct lifreq	*lifr;
10809 
10810 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10811 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10812 
10813 	/*
10814 	 * We allow a get on any logical interface even though the set
10815 	 * can only be done on logical unit 0.
10816 	 */
10817 	if (ipip->ipi_cmd_type == IF_CMD) {
10818 		ifr = (struct ifreq *)if_req;
10819 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10820 	} else {
10821 		lifr = (struct lifreq *)if_req;
10822 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10823 	}
10824 	return (0);
10825 }
10826 
10827 /* Set interface broadcast address. */
10828 /* ARGSUSED2 */
10829 int
10830 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10831 	ip_ioctl_cmd_t *ipip, void *if_req)
10832 {
10833 	ipaddr_t addr;
10834 	ire_t	*ire;
10835 	ill_t		*ill = ipif->ipif_ill;
10836 	ip_stack_t	*ipst = ill->ill_ipst;
10837 
10838 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10839 	    ipif->ipif_id));
10840 
10841 	ASSERT(IAM_WRITER_IPIF(ipif));
10842 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10843 		return (EADDRNOTAVAIL);
10844 
10845 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10846 
10847 	if (sin->sin_family != AF_INET)
10848 		return (EAFNOSUPPORT);
10849 
10850 	addr = sin->sin_addr.s_addr;
10851 
10852 	if (ipif->ipif_flags & IPIF_UP) {
10853 		/*
10854 		 * If we are already up, make sure the new
10855 		 * broadcast address makes sense.  If it does,
10856 		 * there should be an IRE for it already.
10857 		 */
10858 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10859 		    ill, ipif->ipif_zoneid, NULL,
10860 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10861 		if (ire == NULL) {
10862 			return (EINVAL);
10863 		} else {
10864 			ire_refrele(ire);
10865 		}
10866 	}
10867 	/*
10868 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10869 	 * needs to already exist we never need to change the set of
10870 	 * IRE_BROADCASTs when we are UP.
10871 	 */
10872 	if (addr != ipif->ipif_brd_addr)
10873 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10874 
10875 	return (0);
10876 }
10877 
10878 /* Get interface broadcast address. */
10879 /* ARGSUSED */
10880 int
10881 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10882     ip_ioctl_cmd_t *ipip, void *if_req)
10883 {
10884 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10885 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10886 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10887 		return (EADDRNOTAVAIL);
10888 
10889 	/* IPIF_BROADCAST not possible with IPv6 */
10890 	ASSERT(!ipif->ipif_isv6);
10891 	*sin = sin_null;
10892 	sin->sin_family = AF_INET;
10893 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10894 	return (0);
10895 }
10896 
10897 /*
10898  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10899  */
10900 /* ARGSUSED */
10901 int
10902 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10903     ip_ioctl_cmd_t *ipip, void *if_req)
10904 {
10905 	int err = 0;
10906 	in6_addr_t v6mask;
10907 
10908 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10909 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10910 
10911 	ASSERT(IAM_WRITER_IPIF(ipif));
10912 
10913 	if (ipif->ipif_isv6) {
10914 		sin6_t *sin6;
10915 
10916 		if (sin->sin_family != AF_INET6)
10917 			return (EAFNOSUPPORT);
10918 
10919 		sin6 = (sin6_t *)sin;
10920 		v6mask = sin6->sin6_addr;
10921 	} else {
10922 		ipaddr_t mask;
10923 
10924 		if (sin->sin_family != AF_INET)
10925 			return (EAFNOSUPPORT);
10926 
10927 		mask = sin->sin_addr.s_addr;
10928 		if (!ip_contiguous_mask(ntohl(mask)))
10929 			return (ENOTSUP);
10930 		V4MASK_TO_V6(mask, v6mask);
10931 	}
10932 
10933 	/*
10934 	 * No big deal if the interface isn't already up, or the mask
10935 	 * isn't really changing, or this is pt-pt.
10936 	 */
10937 	if (!(ipif->ipif_flags & IPIF_UP) ||
10938 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10939 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10940 		ipif->ipif_v6net_mask = v6mask;
10941 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10942 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10943 			    ipif->ipif_v6net_mask,
10944 			    ipif->ipif_v6subnet);
10945 		}
10946 		return (0);
10947 	}
10948 	/*
10949 	 * Make sure we have valid net and subnet broadcast ire's
10950 	 * for the old netmask, if needed by other logical interfaces.
10951 	 */
10952 	err = ipif_logical_down(ipif, q, mp);
10953 	if (err == EINPROGRESS)
10954 		return (err);
10955 	(void) ipif_down_tail(ipif);
10956 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10957 	return (err);
10958 }
10959 
10960 static int
10961 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10962 {
10963 	in6_addr_t v6mask;
10964 	int err = 0;
10965 
10966 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10967 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10968 
10969 	if (ipif->ipif_isv6) {
10970 		sin6_t *sin6;
10971 
10972 		sin6 = (sin6_t *)sin;
10973 		v6mask = sin6->sin6_addr;
10974 	} else {
10975 		ipaddr_t mask;
10976 
10977 		mask = sin->sin_addr.s_addr;
10978 		V4MASK_TO_V6(mask, v6mask);
10979 	}
10980 
10981 	ipif->ipif_v6net_mask = v6mask;
10982 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10983 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10984 		    ipif->ipif_v6subnet);
10985 	}
10986 	err = ipif_up(ipif, q, mp);
10987 
10988 	if (err == 0 || err == EINPROGRESS) {
10989 		/*
10990 		 * The interface must be DL_BOUND if this packet has to
10991 		 * go out on the wire. Since we only go through a logical
10992 		 * down and are bound with the driver during an internal
10993 		 * down/up that is satisfied.
10994 		 */
10995 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10996 			/* Potentially broadcast an address mask reply. */
10997 			ipif_mask_reply(ipif);
10998 		}
10999 	}
11000 	return (err);
11001 }
11002 
11003 /* ARGSUSED */
11004 int
11005 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11006     ip_ioctl_cmd_t *ipip, void *if_req)
11007 {
11008 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11009 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11010 	(void) ipif_down_tail(ipif);
11011 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11012 }
11013 
11014 /* Get interface net mask. */
11015 /* ARGSUSED */
11016 int
11017 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11018     ip_ioctl_cmd_t *ipip, void *if_req)
11019 {
11020 	struct lifreq *lifr = (struct lifreq *)if_req;
11021 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11022 
11023 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11024 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11025 
11026 	/*
11027 	 * net mask can't change since we have a reference to the ipif.
11028 	 */
11029 	if (ipif->ipif_isv6) {
11030 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11031 		*sin6 = sin6_null;
11032 		sin6->sin6_family = AF_INET6;
11033 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11034 		lifr->lifr_addrlen =
11035 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11036 	} else {
11037 		*sin = sin_null;
11038 		sin->sin_family = AF_INET;
11039 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11040 		if (ipip->ipi_cmd_type == LIF_CMD) {
11041 			lifr->lifr_addrlen =
11042 			    ip_mask_to_plen(ipif->ipif_net_mask);
11043 		}
11044 	}
11045 	return (0);
11046 }
11047 
11048 /* ARGSUSED */
11049 int
11050 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11051     ip_ioctl_cmd_t *ipip, void *if_req)
11052 {
11053 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11054 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11055 
11056 	/*
11057 	 * Since no applications should ever be setting metrics on underlying
11058 	 * interfaces, we explicitly fail to smoke 'em out.
11059 	 */
11060 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11061 		return (EINVAL);
11062 
11063 	/*
11064 	 * Set interface metric.  We don't use this for
11065 	 * anything but we keep track of it in case it is
11066 	 * important to routing applications or such.
11067 	 */
11068 	if (ipip->ipi_cmd_type == IF_CMD) {
11069 		struct ifreq    *ifr;
11070 
11071 		ifr = (struct ifreq *)if_req;
11072 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11073 	} else {
11074 		struct lifreq   *lifr;
11075 
11076 		lifr = (struct lifreq *)if_req;
11077 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11078 	}
11079 	return (0);
11080 }
11081 
11082 /* ARGSUSED */
11083 int
11084 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11085     ip_ioctl_cmd_t *ipip, void *if_req)
11086 {
11087 	/* Get interface metric. */
11088 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11089 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11090 
11091 	if (ipip->ipi_cmd_type == IF_CMD) {
11092 		struct ifreq    *ifr;
11093 
11094 		ifr = (struct ifreq *)if_req;
11095 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11096 	} else {
11097 		struct lifreq   *lifr;
11098 
11099 		lifr = (struct lifreq *)if_req;
11100 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11101 	}
11102 
11103 	return (0);
11104 }
11105 
11106 /* ARGSUSED */
11107 int
11108 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11109     ip_ioctl_cmd_t *ipip, void *if_req)
11110 {
11111 	int	arp_muxid;
11112 
11113 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11114 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11115 	/*
11116 	 * Set the muxid returned from I_PLINK.
11117 	 */
11118 	if (ipip->ipi_cmd_type == IF_CMD) {
11119 		struct ifreq *ifr = (struct ifreq *)if_req;
11120 
11121 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11122 		arp_muxid = ifr->ifr_arp_muxid;
11123 	} else {
11124 		struct lifreq *lifr = (struct lifreq *)if_req;
11125 
11126 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11127 		arp_muxid = lifr->lifr_arp_muxid;
11128 	}
11129 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11130 	return (0);
11131 }
11132 
11133 /* ARGSUSED */
11134 int
11135 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11136     ip_ioctl_cmd_t *ipip, void *if_req)
11137 {
11138 	int	arp_muxid = 0;
11139 
11140 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11141 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11142 	/*
11143 	 * Get the muxid saved in ill for I_PUNLINK.
11144 	 */
11145 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11146 	if (ipip->ipi_cmd_type == IF_CMD) {
11147 		struct ifreq *ifr = (struct ifreq *)if_req;
11148 
11149 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11150 		ifr->ifr_arp_muxid = arp_muxid;
11151 	} else {
11152 		struct lifreq *lifr = (struct lifreq *)if_req;
11153 
11154 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11155 		lifr->lifr_arp_muxid = arp_muxid;
11156 	}
11157 	return (0);
11158 }
11159 
11160 /*
11161  * Set the subnet prefix. Does not modify the broadcast address.
11162  */
11163 /* ARGSUSED */
11164 int
11165 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11166     ip_ioctl_cmd_t *ipip, void *if_req)
11167 {
11168 	int err = 0;
11169 	in6_addr_t v6addr;
11170 	in6_addr_t v6mask;
11171 	boolean_t need_up = B_FALSE;
11172 	int addrlen;
11173 
11174 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11175 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11176 
11177 	ASSERT(IAM_WRITER_IPIF(ipif));
11178 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11179 
11180 	if (ipif->ipif_isv6) {
11181 		sin6_t *sin6;
11182 
11183 		if (sin->sin_family != AF_INET6)
11184 			return (EAFNOSUPPORT);
11185 
11186 		sin6 = (sin6_t *)sin;
11187 		v6addr = sin6->sin6_addr;
11188 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11189 			return (EADDRNOTAVAIL);
11190 	} else {
11191 		ipaddr_t addr;
11192 
11193 		if (sin->sin_family != AF_INET)
11194 			return (EAFNOSUPPORT);
11195 
11196 		addr = sin->sin_addr.s_addr;
11197 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11198 			return (EADDRNOTAVAIL);
11199 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11200 		/* Add 96 bits */
11201 		addrlen += IPV6_ABITS - IP_ABITS;
11202 	}
11203 
11204 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11205 		return (EINVAL);
11206 
11207 	/* Check if bits in the address is set past the mask */
11208 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11209 		return (EINVAL);
11210 
11211 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11212 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11213 		return (0);	/* No change */
11214 
11215 	if (ipif->ipif_flags & IPIF_UP) {
11216 		/*
11217 		 * If the interface is already marked up,
11218 		 * we call ipif_down which will take care
11219 		 * of ditching any IREs that have been set
11220 		 * up based on the old interface address.
11221 		 */
11222 		err = ipif_logical_down(ipif, q, mp);
11223 		if (err == EINPROGRESS)
11224 			return (err);
11225 		(void) ipif_down_tail(ipif);
11226 		need_up = B_TRUE;
11227 	}
11228 
11229 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11230 	return (err);
11231 }
11232 
11233 static int
11234 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11235     queue_t *q, mblk_t *mp, boolean_t need_up)
11236 {
11237 	ill_t	*ill = ipif->ipif_ill;
11238 	int	err = 0;
11239 
11240 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11241 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11242 
11243 	/* Set the new address. */
11244 	mutex_enter(&ill->ill_lock);
11245 	ipif->ipif_v6net_mask = v6mask;
11246 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11247 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11248 		    ipif->ipif_v6subnet);
11249 	}
11250 	mutex_exit(&ill->ill_lock);
11251 
11252 	if (need_up) {
11253 		/*
11254 		 * Now bring the interface back up.  If this
11255 		 * is the only IPIF for the ILL, ipif_up
11256 		 * will have to re-bind to the device, so
11257 		 * we may get back EINPROGRESS, in which
11258 		 * case, this IOCTL will get completed in
11259 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11260 		 */
11261 		err = ipif_up(ipif, q, mp);
11262 		if (err == EINPROGRESS)
11263 			return (err);
11264 	}
11265 	return (err);
11266 }
11267 
11268 /* ARGSUSED */
11269 int
11270 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11271     ip_ioctl_cmd_t *ipip, void *if_req)
11272 {
11273 	int	addrlen;
11274 	in6_addr_t v6addr;
11275 	in6_addr_t v6mask;
11276 	struct lifreq *lifr = (struct lifreq *)if_req;
11277 
11278 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11279 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11280 	(void) ipif_down_tail(ipif);
11281 
11282 	addrlen = lifr->lifr_addrlen;
11283 	if (ipif->ipif_isv6) {
11284 		sin6_t *sin6;
11285 
11286 		sin6 = (sin6_t *)sin;
11287 		v6addr = sin6->sin6_addr;
11288 	} else {
11289 		ipaddr_t addr;
11290 
11291 		addr = sin->sin_addr.s_addr;
11292 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11293 		addrlen += IPV6_ABITS - IP_ABITS;
11294 	}
11295 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11296 
11297 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11298 }
11299 
11300 /* ARGSUSED */
11301 int
11302 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11303     ip_ioctl_cmd_t *ipip, void *if_req)
11304 {
11305 	struct lifreq *lifr = (struct lifreq *)if_req;
11306 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11307 
11308 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11309 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11310 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11311 
11312 	if (ipif->ipif_isv6) {
11313 		*sin6 = sin6_null;
11314 		sin6->sin6_family = AF_INET6;
11315 		sin6->sin6_addr = ipif->ipif_v6subnet;
11316 		lifr->lifr_addrlen =
11317 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11318 	} else {
11319 		*sin = sin_null;
11320 		sin->sin_family = AF_INET;
11321 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11322 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11323 	}
11324 	return (0);
11325 }
11326 
11327 /*
11328  * Set the IPv6 address token.
11329  */
11330 /* ARGSUSED */
11331 int
11332 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11333     ip_ioctl_cmd_t *ipi, void *if_req)
11334 {
11335 	ill_t *ill = ipif->ipif_ill;
11336 	int err;
11337 	in6_addr_t v6addr;
11338 	in6_addr_t v6mask;
11339 	boolean_t need_up = B_FALSE;
11340 	int i;
11341 	sin6_t *sin6 = (sin6_t *)sin;
11342 	struct lifreq *lifr = (struct lifreq *)if_req;
11343 	int addrlen;
11344 
11345 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11346 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11347 	ASSERT(IAM_WRITER_IPIF(ipif));
11348 
11349 	addrlen = lifr->lifr_addrlen;
11350 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11351 	if (ipif->ipif_id != 0)
11352 		return (EINVAL);
11353 
11354 	if (!ipif->ipif_isv6)
11355 		return (EINVAL);
11356 
11357 	if (addrlen > IPV6_ABITS)
11358 		return (EINVAL);
11359 
11360 	v6addr = sin6->sin6_addr;
11361 
11362 	/*
11363 	 * The length of the token is the length from the end.  To get
11364 	 * the proper mask for this, compute the mask of the bits not
11365 	 * in the token; ie. the prefix, and then xor to get the mask.
11366 	 */
11367 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11368 		return (EINVAL);
11369 	for (i = 0; i < 4; i++) {
11370 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11371 	}
11372 
11373 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11374 	    ill->ill_token_length == addrlen)
11375 		return (0);	/* No change */
11376 
11377 	if (ipif->ipif_flags & IPIF_UP) {
11378 		err = ipif_logical_down(ipif, q, mp);
11379 		if (err == EINPROGRESS)
11380 			return (err);
11381 		(void) ipif_down_tail(ipif);
11382 		need_up = B_TRUE;
11383 	}
11384 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11385 	return (err);
11386 }
11387 
11388 static int
11389 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11390     mblk_t *mp, boolean_t need_up)
11391 {
11392 	in6_addr_t v6addr;
11393 	in6_addr_t v6mask;
11394 	ill_t	*ill = ipif->ipif_ill;
11395 	int	i;
11396 	int	err = 0;
11397 
11398 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11399 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11400 	v6addr = sin6->sin6_addr;
11401 	/*
11402 	 * The length of the token is the length from the end.  To get
11403 	 * the proper mask for this, compute the mask of the bits not
11404 	 * in the token; ie. the prefix, and then xor to get the mask.
11405 	 */
11406 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11407 	for (i = 0; i < 4; i++)
11408 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11409 
11410 	mutex_enter(&ill->ill_lock);
11411 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11412 	ill->ill_token_length = addrlen;
11413 	ill->ill_manual_token = 1;
11414 
11415 	/* Reconfigure the link-local address based on this new token */
11416 	ipif_setlinklocal(ill->ill_ipif);
11417 
11418 	mutex_exit(&ill->ill_lock);
11419 
11420 	if (need_up) {
11421 		/*
11422 		 * Now bring the interface back up.  If this
11423 		 * is the only IPIF for the ILL, ipif_up
11424 		 * will have to re-bind to the device, so
11425 		 * we may get back EINPROGRESS, in which
11426 		 * case, this IOCTL will get completed in
11427 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11428 		 */
11429 		err = ipif_up(ipif, q, mp);
11430 		if (err == EINPROGRESS)
11431 			return (err);
11432 	}
11433 	return (err);
11434 }
11435 
11436 /* ARGSUSED */
11437 int
11438 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11439     ip_ioctl_cmd_t *ipi, void *if_req)
11440 {
11441 	ill_t *ill;
11442 	sin6_t *sin6 = (sin6_t *)sin;
11443 	struct lifreq *lifr = (struct lifreq *)if_req;
11444 
11445 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11446 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11447 	if (ipif->ipif_id != 0)
11448 		return (EINVAL);
11449 
11450 	ill = ipif->ipif_ill;
11451 	if (!ill->ill_isv6)
11452 		return (ENXIO);
11453 
11454 	*sin6 = sin6_null;
11455 	sin6->sin6_family = AF_INET6;
11456 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11457 	sin6->sin6_addr = ill->ill_token;
11458 	lifr->lifr_addrlen = ill->ill_token_length;
11459 	return (0);
11460 }
11461 
11462 /*
11463  * Set (hardware) link specific information that might override
11464  * what was acquired through the DL_INFO_ACK.
11465  */
11466 /* ARGSUSED */
11467 int
11468 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11469     ip_ioctl_cmd_t *ipi, void *if_req)
11470 {
11471 	ill_t		*ill = ipif->ipif_ill;
11472 	int		ip_min_mtu;
11473 	struct lifreq	*lifr = (struct lifreq *)if_req;
11474 	lif_ifinfo_req_t *lir;
11475 
11476 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11477 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11478 	lir = &lifr->lifr_ifinfo;
11479 	ASSERT(IAM_WRITER_IPIF(ipif));
11480 
11481 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11482 	if (ipif->ipif_id != 0)
11483 		return (EINVAL);
11484 
11485 	/* Set interface MTU. */
11486 	if (ipif->ipif_isv6)
11487 		ip_min_mtu = IPV6_MIN_MTU;
11488 	else
11489 		ip_min_mtu = IP_MIN_MTU;
11490 
11491 	/*
11492 	 * Verify values before we set anything. Allow zero to
11493 	 * mean unspecified.
11494 	 *
11495 	 * XXX We should be able to set the user-defined lir_mtu to some value
11496 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11497 	 * ill_max_frag value tells us the max MTU that can be handled by the
11498 	 * datalink, whereas the ill_current_frag is dynamically computed for
11499 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11500 	 * since there is currently no way of distinguishing between
11501 	 * administratively fixed link mtu values (e.g., those set via
11502 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11503 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11504 	 * upper-bound.
11505 	 */
11506 	if (lir->lir_maxmtu != 0 &&
11507 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11508 	    lir->lir_maxmtu < ip_min_mtu))
11509 		return (EINVAL);
11510 	if (lir->lir_reachtime != 0 &&
11511 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11512 		return (EINVAL);
11513 	if (lir->lir_reachretrans != 0 &&
11514 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11515 		return (EINVAL);
11516 
11517 	mutex_enter(&ill->ill_lock);
11518 	/*
11519 	 * The dce and fragmentation code can handle changes to ill_mtu
11520 	 * concurrent with sending/fragmenting packets.
11521 	 */
11522 	if (lir->lir_maxmtu != 0)
11523 		ill->ill_user_mtu = lir->lir_maxmtu;
11524 
11525 	if (lir->lir_reachtime != 0)
11526 		ill->ill_reachable_time = lir->lir_reachtime;
11527 
11528 	if (lir->lir_reachretrans != 0)
11529 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11530 
11531 	ill->ill_max_hops = lir->lir_maxhops;
11532 	ill->ill_max_buf = ND_MAX_Q;
11533 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11534 		/*
11535 		 * ill_mtu is the actual interface MTU, obtained as the min
11536 		 * of user-configured mtu and the value announced by the
11537 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11538 		 * we have already made the choice of requiring
11539 		 * ill_user_mtu < ill_current_frag by the time we get here,
11540 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11541 		 * here.
11542 		 */
11543 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11544 	}
11545 	mutex_exit(&ill->ill_lock);
11546 
11547 	/*
11548 	 * Make sure all dce_generation checks find out
11549 	 * that ill_mtu has changed.
11550 	 */
11551 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11552 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11553 
11554 	/*
11555 	 * Refresh IPMP meta-interface MTU if necessary.
11556 	 */
11557 	if (IS_UNDER_IPMP(ill))
11558 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11559 
11560 	return (0);
11561 }
11562 
11563 /* ARGSUSED */
11564 int
11565 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11566     ip_ioctl_cmd_t *ipi, void *if_req)
11567 {
11568 	struct lif_ifinfo_req *lir;
11569 	ill_t *ill = ipif->ipif_ill;
11570 
11571 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11572 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11573 	if (ipif->ipif_id != 0)
11574 		return (EINVAL);
11575 
11576 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11577 	lir->lir_maxhops = ill->ill_max_hops;
11578 	lir->lir_reachtime = ill->ill_reachable_time;
11579 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11580 	lir->lir_maxmtu = ill->ill_mtu;
11581 
11582 	return (0);
11583 }
11584 
11585 /*
11586  * Return best guess as to the subnet mask for the specified address.
11587  * Based on the subnet masks for all the configured interfaces.
11588  *
11589  * We end up returning a zero mask in the case of default, multicast or
11590  * experimental.
11591  */
11592 static ipaddr_t
11593 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11594 {
11595 	ipaddr_t net_mask;
11596 	ill_t	*ill;
11597 	ipif_t	*ipif;
11598 	ill_walk_context_t ctx;
11599 	ipif_t	*fallback_ipif = NULL;
11600 
11601 	net_mask = ip_net_mask(addr);
11602 	if (net_mask == 0) {
11603 		*ipifp = NULL;
11604 		return (0);
11605 	}
11606 
11607 	/* Let's check to see if this is maybe a local subnet route. */
11608 	/* this function only applies to IPv4 interfaces */
11609 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11610 	ill = ILL_START_WALK_V4(&ctx, ipst);
11611 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11612 		mutex_enter(&ill->ill_lock);
11613 		for (ipif = ill->ill_ipif; ipif != NULL;
11614 		    ipif = ipif->ipif_next) {
11615 			if (IPIF_IS_CONDEMNED(ipif))
11616 				continue;
11617 			if (!(ipif->ipif_flags & IPIF_UP))
11618 				continue;
11619 			if ((ipif->ipif_subnet & net_mask) ==
11620 			    (addr & net_mask)) {
11621 				/*
11622 				 * Don't trust pt-pt interfaces if there are
11623 				 * other interfaces.
11624 				 */
11625 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11626 					if (fallback_ipif == NULL) {
11627 						ipif_refhold_locked(ipif);
11628 						fallback_ipif = ipif;
11629 					}
11630 					continue;
11631 				}
11632 
11633 				/*
11634 				 * Fine. Just assume the same net mask as the
11635 				 * directly attached subnet interface is using.
11636 				 */
11637 				ipif_refhold_locked(ipif);
11638 				mutex_exit(&ill->ill_lock);
11639 				rw_exit(&ipst->ips_ill_g_lock);
11640 				if (fallback_ipif != NULL)
11641 					ipif_refrele(fallback_ipif);
11642 				*ipifp = ipif;
11643 				return (ipif->ipif_net_mask);
11644 			}
11645 		}
11646 		mutex_exit(&ill->ill_lock);
11647 	}
11648 	rw_exit(&ipst->ips_ill_g_lock);
11649 
11650 	*ipifp = fallback_ipif;
11651 	return ((fallback_ipif != NULL) ?
11652 	    fallback_ipif->ipif_net_mask : net_mask);
11653 }
11654 
11655 /*
11656  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11657  */
11658 static void
11659 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11660 {
11661 	IOCP	iocp;
11662 	ipft_t	*ipft;
11663 	ipllc_t	*ipllc;
11664 	mblk_t	*mp1;
11665 	cred_t	*cr;
11666 	int	error = 0;
11667 	conn_t	*connp;
11668 
11669 	ip1dbg(("ip_wput_ioctl"));
11670 	iocp = (IOCP)mp->b_rptr;
11671 	mp1 = mp->b_cont;
11672 	if (mp1 == NULL) {
11673 		iocp->ioc_error = EINVAL;
11674 		mp->b_datap->db_type = M_IOCNAK;
11675 		iocp->ioc_count = 0;
11676 		qreply(q, mp);
11677 		return;
11678 	}
11679 
11680 	/*
11681 	 * These IOCTLs provide various control capabilities to
11682 	 * upstream agents such as ULPs and processes.	There
11683 	 * are currently two such IOCTLs implemented.  They
11684 	 * are used by TCP to provide update information for
11685 	 * existing IREs and to forcibly delete an IRE for a
11686 	 * host that is not responding, thereby forcing an
11687 	 * attempt at a new route.
11688 	 */
11689 	iocp->ioc_error = EINVAL;
11690 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11691 		goto done;
11692 
11693 	ipllc = (ipllc_t *)mp1->b_rptr;
11694 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11695 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11696 			break;
11697 	}
11698 	/*
11699 	 * prefer credential from mblk over ioctl;
11700 	 * see ip_sioctl_copyin_setup
11701 	 */
11702 	cr = msg_getcred(mp, NULL);
11703 	if (cr == NULL)
11704 		cr = iocp->ioc_cr;
11705 
11706 	/*
11707 	 * Refhold the conn in case the request gets queued up in some lookup
11708 	 */
11709 	ASSERT(CONN_Q(q));
11710 	connp = Q_TO_CONN(q);
11711 	CONN_INC_REF(connp);
11712 	if (ipft->ipft_pfi &&
11713 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11714 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11715 		error = (*ipft->ipft_pfi)(q,
11716 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11717 	}
11718 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11719 		/*
11720 		 * CONN_OPER_PENDING_DONE happens in the function called
11721 		 * through ipft_pfi above.
11722 		 */
11723 		return;
11724 	}
11725 
11726 	CONN_OPER_PENDING_DONE(connp);
11727 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11728 		freemsg(mp);
11729 		return;
11730 	}
11731 	iocp->ioc_error = error;
11732 
11733 done:
11734 	mp->b_datap->db_type = M_IOCACK;
11735 	if (iocp->ioc_error)
11736 		iocp->ioc_count = 0;
11737 	qreply(q, mp);
11738 }
11739 
11740 /*
11741  * Assign a unique id for the ipif. This is used by sctp_addr.c
11742  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11743  */
11744 static void
11745 ipif_assign_seqid(ipif_t *ipif)
11746 {
11747 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11748 
11749 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11750 }
11751 
11752 /*
11753  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11754  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11755  * that the clone is complete -- including the seqid -- and the expectation is
11756  * that the caller will either free or overwrite `sipif' before it's unlocked.
11757  */
11758 static void
11759 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11760 {
11761 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11762 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11763 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11764 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11765 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11766 
11767 	dipif->ipif_flags = sipif->ipif_flags;
11768 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11769 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11770 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11771 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11772 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11773 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11774 
11775 	/*
11776 	 * As per the comment atop the function, we assume that these sipif
11777 	 * fields will be changed before sipif is unlocked.
11778 	 */
11779 	dipif->ipif_seqid = sipif->ipif_seqid;
11780 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11781 }
11782 
11783 /*
11784  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11785  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11786  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11787  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11788  * down (i.e., no DAD), of the same type, and unlocked.
11789  */
11790 static void
11791 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11792 {
11793 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11794 	ipxop_t *ipx = ipsq->ipsq_xop;
11795 
11796 	ASSERT(sipif != dipif);
11797 	ASSERT(sipif != virgipif);
11798 
11799 	/*
11800 	 * Grab all of the locks that protect the ipif in a defined order.
11801 	 */
11802 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11803 
11804 	ipif_clone(sipif, dipif);
11805 	if (virgipif != NULL) {
11806 		ipif_clone(virgipif, sipif);
11807 		mi_free(virgipif);
11808 	}
11809 
11810 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11811 
11812 	/*
11813 	 * Transfer ownership of the current xop, if necessary.
11814 	 */
11815 	if (ipx->ipx_current_ipif == sipif) {
11816 		ASSERT(ipx->ipx_pending_ipif == NULL);
11817 		mutex_enter(&ipx->ipx_lock);
11818 		ipx->ipx_current_ipif = dipif;
11819 		mutex_exit(&ipx->ipx_lock);
11820 	}
11821 
11822 	if (virgipif == NULL)
11823 		mi_free(sipif);
11824 }
11825 
11826 /*
11827  * checks if:
11828  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11829  *	- logical interface is within the allowed range
11830  */
11831 static int
11832 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11833 {
11834 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11835 		return (ENAMETOOLONG);
11836 
11837 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11838 		return (ERANGE);
11839 	return (0);
11840 }
11841 
11842 /*
11843  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11844  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11845  * be inserted into the first space available in the list. The value of
11846  * ipif_id will then be set to the appropriate value for its position.
11847  */
11848 static int
11849 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11850 {
11851 	ill_t *ill;
11852 	ipif_t *tipif;
11853 	ipif_t **tipifp;
11854 	int id, err;
11855 	ip_stack_t	*ipst;
11856 
11857 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11858 	    IAM_WRITER_IPIF(ipif));
11859 
11860 	ill = ipif->ipif_ill;
11861 	ASSERT(ill != NULL);
11862 	ipst = ill->ill_ipst;
11863 
11864 	/*
11865 	 * In the case of lo0:0 we already hold the ill_g_lock.
11866 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11867 	 * ipif_insert.
11868 	 */
11869 	if (acquire_g_lock)
11870 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11871 	mutex_enter(&ill->ill_lock);
11872 	id = ipif->ipif_id;
11873 	tipifp = &(ill->ill_ipif);
11874 	if (id == -1) {	/* need to find a real id */
11875 		id = 0;
11876 		while ((tipif = *tipifp) != NULL) {
11877 			ASSERT(tipif->ipif_id >= id);
11878 			if (tipif->ipif_id != id)
11879 				break; /* non-consecutive id */
11880 			id++;
11881 			tipifp = &(tipif->ipif_next);
11882 		}
11883 		if ((err = is_lifname_valid(ill, id)) != 0) {
11884 			mutex_exit(&ill->ill_lock);
11885 			if (acquire_g_lock)
11886 				rw_exit(&ipst->ips_ill_g_lock);
11887 			return (err);
11888 		}
11889 		ipif->ipif_id = id; /* assign new id */
11890 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11891 		/* we have a real id; insert ipif in the right place */
11892 		while ((tipif = *tipifp) != NULL) {
11893 			ASSERT(tipif->ipif_id != id);
11894 			if (tipif->ipif_id > id)
11895 				break; /* found correct location */
11896 			tipifp = &(tipif->ipif_next);
11897 		}
11898 	} else {
11899 		mutex_exit(&ill->ill_lock);
11900 		if (acquire_g_lock)
11901 			rw_exit(&ipst->ips_ill_g_lock);
11902 		return (err);
11903 	}
11904 
11905 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11906 
11907 	ipif->ipif_next = tipif;
11908 	*tipifp = ipif;
11909 	mutex_exit(&ill->ill_lock);
11910 	if (acquire_g_lock)
11911 		rw_exit(&ipst->ips_ill_g_lock);
11912 
11913 	return (0);
11914 }
11915 
11916 static void
11917 ipif_remove(ipif_t *ipif)
11918 {
11919 	ipif_t	**ipifp;
11920 	ill_t	*ill = ipif->ipif_ill;
11921 
11922 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11923 
11924 	mutex_enter(&ill->ill_lock);
11925 	ipifp = &ill->ill_ipif;
11926 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11927 		if (*ipifp == ipif) {
11928 			*ipifp = ipif->ipif_next;
11929 			break;
11930 		}
11931 	}
11932 	mutex_exit(&ill->ill_lock);
11933 }
11934 
11935 /*
11936  * Allocate and initialize a new interface control structure.  (Always
11937  * called as writer.)
11938  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11939  * is not part of the global linked list of ills. ipif_seqid is unique
11940  * in the system and to preserve the uniqueness, it is assigned only
11941  * when ill becomes part of the global list. At that point ill will
11942  * have a name. If it doesn't get assigned here, it will get assigned
11943  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11944  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11945  * the interface flags or any other information from the DL_INFO_ACK for
11946  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11947  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11948  * second DL_INFO_ACK comes in from the driver.
11949  */
11950 static ipif_t *
11951 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11952     boolean_t insert, int *errorp)
11953 {
11954 	int err;
11955 	ipif_t	*ipif;
11956 	ip_stack_t *ipst = ill->ill_ipst;
11957 
11958 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11959 	    ill->ill_name, id, (void *)ill));
11960 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11961 
11962 	if (errorp != NULL)
11963 		*errorp = 0;
11964 
11965 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11966 		if (errorp != NULL)
11967 			*errorp = ENOMEM;
11968 		return (NULL);
11969 	}
11970 	*ipif = ipif_zero;	/* start clean */
11971 
11972 	ipif->ipif_ill = ill;
11973 	ipif->ipif_id = id;	/* could be -1 */
11974 	/*
11975 	 * Inherit the zoneid from the ill; for the shared stack instance
11976 	 * this is always the global zone
11977 	 */
11978 	ipif->ipif_zoneid = ill->ill_zoneid;
11979 
11980 	ipif->ipif_refcnt = 0;
11981 
11982 	if (insert) {
11983 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11984 			mi_free(ipif);
11985 			if (errorp != NULL)
11986 				*errorp = err;
11987 			return (NULL);
11988 		}
11989 		/* -1 id should have been replaced by real id */
11990 		id = ipif->ipif_id;
11991 		ASSERT(id >= 0);
11992 	}
11993 
11994 	if (ill->ill_name[0] != '\0')
11995 		ipif_assign_seqid(ipif);
11996 
11997 	/*
11998 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11999 	 * (which must not exist yet because the zeroth ipif is created once
12000 	 * per ill).  However, do not not link it to the ipmp_grp_t until
12001 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12002 	 */
12003 	if (id == 0 && IS_IPMP(ill)) {
12004 		if (ipmp_illgrp_create(ill) == NULL) {
12005 			if (insert) {
12006 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12007 				ipif_remove(ipif);
12008 				rw_exit(&ipst->ips_ill_g_lock);
12009 			}
12010 			mi_free(ipif);
12011 			if (errorp != NULL)
12012 				*errorp = ENOMEM;
12013 			return (NULL);
12014 		}
12015 	}
12016 
12017 	/*
12018 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12019 	 * not up and can't be looked up until the ioctl completes and the
12020 	 * IPIF_CHANGING flag is cleared.
12021 	 */
12022 	mutex_enter(&ill->ill_lock);
12023 
12024 	ipif->ipif_ire_type = ire_type;
12025 
12026 	if (ipif->ipif_isv6) {
12027 		ill->ill_flags |= ILLF_IPV6;
12028 	} else {
12029 		ipaddr_t inaddr_any = INADDR_ANY;
12030 
12031 		ill->ill_flags |= ILLF_IPV4;
12032 
12033 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12034 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12035 		    &ipif->ipif_v6lcl_addr);
12036 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12037 		    &ipif->ipif_v6subnet);
12038 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12039 		    &ipif->ipif_v6net_mask);
12040 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12041 		    &ipif->ipif_v6brd_addr);
12042 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12043 		    &ipif->ipif_v6pp_dst_addr);
12044 	}
12045 
12046 	/*
12047 	 * Don't set the interface flags etc. now, will do it in
12048 	 * ip_ll_subnet_defaults.
12049 	 */
12050 	if (!initialize)
12051 		goto out;
12052 
12053 	/*
12054 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12055 	 * with no underlying interfaces (and thus an unknown broadcast
12056 	 * address length), but all interfaces that can be placed into an IPMP
12057 	 * group are required to be broadcast-capable.
12058 	 */
12059 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12060 		/*
12061 		 * Later detect lack of DLPI driver multicast capability by
12062 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12063 		 */
12064 		ill->ill_flags |= ILLF_MULTICAST;
12065 		if (!ipif->ipif_isv6)
12066 			ipif->ipif_flags |= IPIF_BROADCAST;
12067 	} else {
12068 		if (ill->ill_net_type != IRE_LOOPBACK) {
12069 			if (ipif->ipif_isv6)
12070 				/*
12071 				 * Note: xresolv interfaces will eventually need
12072 				 * NOARP set here as well, but that will require
12073 				 * those external resolvers to have some
12074 				 * knowledge of that flag and act appropriately.
12075 				 * Not to be changed at present.
12076 				 */
12077 				ill->ill_flags |= ILLF_NONUD;
12078 			else
12079 				ill->ill_flags |= ILLF_NOARP;
12080 		}
12081 		if (ill->ill_phys_addr_length == 0) {
12082 			if (IS_VNI(ill)) {
12083 				ipif->ipif_flags |= IPIF_NOXMIT;
12084 			} else {
12085 				/* pt-pt supports multicast. */
12086 				ill->ill_flags |= ILLF_MULTICAST;
12087 				if (ill->ill_net_type != IRE_LOOPBACK)
12088 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12089 			}
12090 		}
12091 	}
12092 out:
12093 	mutex_exit(&ill->ill_lock);
12094 	return (ipif);
12095 }
12096 
12097 /*
12098  * Remove the neighbor cache entries associated with this logical
12099  * interface.
12100  */
12101 int
12102 ipif_arp_down(ipif_t *ipif)
12103 {
12104 	ill_t	*ill = ipif->ipif_ill;
12105 	int	err = 0;
12106 
12107 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12108 	ASSERT(IAM_WRITER_IPIF(ipif));
12109 
12110 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12111 	    ill_t *, ill, ipif_t *, ipif);
12112 	ipif_nce_down(ipif);
12113 
12114 	/*
12115 	 * If this is the last ipif that is going down and there are no
12116 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12117 	 * clean up ARP completely.
12118 	 */
12119 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12120 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12121 		/*
12122 		 * If this was the last ipif on an IPMP interface, purge any
12123 		 * static ARP entries associated with it.
12124 		 */
12125 		if (IS_IPMP(ill))
12126 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12127 
12128 		/* UNBIND, DETACH */
12129 		err = arp_ll_down(ill);
12130 	}
12131 
12132 	return (err);
12133 }
12134 
12135 /*
12136  * Get the resolver set up for a new IP address.  (Always called as writer.)
12137  * Called both for IPv4 and IPv6 interfaces, though it only does some
12138  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12139  *
12140  * The enumerated value res_act tunes the behavior:
12141  * 	* Res_act_initial: set up all the resolver structures for a new
12142  *	  IP address.
12143  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12144  *	  ARP message in defense of the address.
12145  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12146  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12147  *
12148  * Returns zero on success, or an errno upon failure.
12149  */
12150 int
12151 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12152 {
12153 	ill_t		*ill = ipif->ipif_ill;
12154 	int		err;
12155 	boolean_t	was_dup;
12156 
12157 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12158 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12159 	ASSERT(IAM_WRITER_IPIF(ipif));
12160 
12161 	was_dup = B_FALSE;
12162 	if (res_act == Res_act_initial) {
12163 		ipif->ipif_addr_ready = 0;
12164 		/*
12165 		 * We're bringing an interface up here.  There's no way that we
12166 		 * should need to shut down ARP now.
12167 		 */
12168 		mutex_enter(&ill->ill_lock);
12169 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12170 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12171 			ill->ill_ipif_dup_count--;
12172 			was_dup = B_TRUE;
12173 		}
12174 		mutex_exit(&ill->ill_lock);
12175 	}
12176 	if (ipif->ipif_recovery_id != 0)
12177 		(void) untimeout(ipif->ipif_recovery_id);
12178 	ipif->ipif_recovery_id = 0;
12179 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12180 		ipif->ipif_addr_ready = 1;
12181 		return (0);
12182 	}
12183 	/* NDP will set the ipif_addr_ready flag when it's ready */
12184 	if (ill->ill_isv6)
12185 		return (0);
12186 
12187 	err = ipif_arp_up(ipif, res_act, was_dup);
12188 	return (err);
12189 }
12190 
12191 /*
12192  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12193  * when a link has just gone back up.
12194  */
12195 static void
12196 ipif_nce_start_dad(ipif_t *ipif)
12197 {
12198 	ncec_t *ncec;
12199 	ill_t *ill = ipif->ipif_ill;
12200 	boolean_t isv6 = ill->ill_isv6;
12201 
12202 	if (isv6) {
12203 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12204 		    &ipif->ipif_v6lcl_addr);
12205 	} else {
12206 		ipaddr_t v4addr;
12207 
12208 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12209 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12210 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12211 			/*
12212 			 * If we can't contact ARP for some reason,
12213 			 * that's not really a problem.  Just send
12214 			 * out the routing socket notification that
12215 			 * DAD completion would have done, and continue.
12216 			 */
12217 			ipif_mask_reply(ipif);
12218 			ipif_up_notify(ipif);
12219 			ipif->ipif_addr_ready = 1;
12220 			return;
12221 		}
12222 
12223 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12224 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12225 	}
12226 
12227 	if (ncec == NULL) {
12228 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12229 		    (void *)ipif));
12230 		return;
12231 	}
12232 	if (!nce_restart_dad(ncec)) {
12233 		/*
12234 		 * If we can't restart DAD for some reason, that's not really a
12235 		 * problem.  Just send out the routing socket notification that
12236 		 * DAD completion would have done, and continue.
12237 		 */
12238 		ipif_up_notify(ipif);
12239 		ipif->ipif_addr_ready = 1;
12240 	}
12241 	ncec_refrele(ncec);
12242 }
12243 
12244 /*
12245  * Restart duplicate address detection on all interfaces on the given ill.
12246  *
12247  * This is called when an interface transitions from down to up
12248  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12249  *
12250  * Note that since the underlying physical link has transitioned, we must cause
12251  * at least one routing socket message to be sent here, either via DAD
12252  * completion or just by default on the first ipif.  (If we don't do this, then
12253  * in.mpathd will see long delays when doing link-based failure recovery.)
12254  */
12255 void
12256 ill_restart_dad(ill_t *ill, boolean_t went_up)
12257 {
12258 	ipif_t *ipif;
12259 
12260 	if (ill == NULL)
12261 		return;
12262 
12263 	/*
12264 	 * If layer two doesn't support duplicate address detection, then just
12265 	 * send the routing socket message now and be done with it.
12266 	 */
12267 	if (!ill->ill_isv6 && arp_no_defense) {
12268 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12269 		return;
12270 	}
12271 
12272 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12273 		if (went_up) {
12274 
12275 			if (ipif->ipif_flags & IPIF_UP) {
12276 				ipif_nce_start_dad(ipif);
12277 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12278 				/*
12279 				 * kick off the bring-up process now.
12280 				 */
12281 				ipif_do_recovery(ipif);
12282 			} else {
12283 				/*
12284 				 * Unfortunately, the first ipif is "special"
12285 				 * and represents the underlying ill in the
12286 				 * routing socket messages.  Thus, when this
12287 				 * one ipif is down, we must still notify so
12288 				 * that the user knows the IFF_RUNNING status
12289 				 * change.  (If the first ipif is up, then
12290 				 * we'll handle eventual routing socket
12291 				 * notification via DAD completion.)
12292 				 */
12293 				if (ipif == ill->ill_ipif) {
12294 					ip_rts_ifmsg(ill->ill_ipif,
12295 					    RTSQ_DEFAULT);
12296 				}
12297 			}
12298 		} else {
12299 			/*
12300 			 * After link down, we'll need to send a new routing
12301 			 * message when the link comes back, so clear
12302 			 * ipif_addr_ready.
12303 			 */
12304 			ipif->ipif_addr_ready = 0;
12305 		}
12306 	}
12307 
12308 	/*
12309 	 * If we've torn down links, then notify the user right away.
12310 	 */
12311 	if (!went_up)
12312 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12313 }
12314 
12315 static void
12316 ipsq_delete(ipsq_t *ipsq)
12317 {
12318 	ipxop_t *ipx = ipsq->ipsq_xop;
12319 
12320 	ipsq->ipsq_ipst = NULL;
12321 	ASSERT(ipsq->ipsq_phyint == NULL);
12322 	ASSERT(ipsq->ipsq_xop != NULL);
12323 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12324 	ASSERT(ipx->ipx_pending_mp == NULL);
12325 	kmem_free(ipsq, sizeof (ipsq_t));
12326 }
12327 
12328 static int
12329 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12330 {
12331 	int err = 0;
12332 	ipif_t *ipif;
12333 
12334 	if (ill == NULL)
12335 		return (0);
12336 
12337 	ASSERT(IAM_WRITER_ILL(ill));
12338 	ill->ill_up_ipifs = B_TRUE;
12339 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12340 		if (ipif->ipif_was_up) {
12341 			if (!(ipif->ipif_flags & IPIF_UP))
12342 				err = ipif_up(ipif, q, mp);
12343 			ipif->ipif_was_up = B_FALSE;
12344 			if (err != 0) {
12345 				ASSERT(err == EINPROGRESS);
12346 				return (err);
12347 			}
12348 		}
12349 	}
12350 	ill->ill_up_ipifs = B_FALSE;
12351 	return (0);
12352 }
12353 
12354 /*
12355  * This function is called to bring up all the ipifs that were up before
12356  * bringing the ill down via ill_down_ipifs().
12357  */
12358 int
12359 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12360 {
12361 	int err;
12362 
12363 	ASSERT(IAM_WRITER_ILL(ill));
12364 
12365 	if (ill->ill_replumbing) {
12366 		ill->ill_replumbing = 0;
12367 		/*
12368 		 * Send down REPLUMB_DONE notification followed by the
12369 		 * BIND_REQ on the arp stream.
12370 		 */
12371 		if (!ill->ill_isv6)
12372 			arp_send_replumb_conf(ill);
12373 	}
12374 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12375 	if (err != 0)
12376 		return (err);
12377 
12378 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12379 }
12380 
12381 /*
12382  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12383  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12384  */
12385 static void
12386 ill_down_ipifs(ill_t *ill, boolean_t logical)
12387 {
12388 	ipif_t *ipif;
12389 
12390 	ASSERT(IAM_WRITER_ILL(ill));
12391 
12392 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12393 		/*
12394 		 * We go through the ipif_down logic even if the ipif
12395 		 * is already down, since routes can be added based
12396 		 * on down ipifs. Going through ipif_down once again
12397 		 * will delete any IREs created based on these routes.
12398 		 */
12399 		if (ipif->ipif_flags & IPIF_UP)
12400 			ipif->ipif_was_up = B_TRUE;
12401 
12402 		if (logical) {
12403 			(void) ipif_logical_down(ipif, NULL, NULL);
12404 			ipif_non_duplicate(ipif);
12405 			(void) ipif_down_tail(ipif);
12406 		} else {
12407 			(void) ipif_down(ipif, NULL, NULL);
12408 		}
12409 	}
12410 }
12411 
12412 /*
12413  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12414  * a look again at valid source addresses.
12415  * This should be called each time after the set of source addresses has been
12416  * changed.
12417  */
12418 void
12419 ip_update_source_selection(ip_stack_t *ipst)
12420 {
12421 	/* We skip past SRC_GENERATION_VERIFY */
12422 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12423 	    SRC_GENERATION_VERIFY)
12424 		atomic_add_32(&ipst->ips_src_generation, 1);
12425 }
12426 
12427 /*
12428  * Finish the group join started in ip_sioctl_groupname().
12429  */
12430 /* ARGSUSED */
12431 static void
12432 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12433 {
12434 	ill_t		*ill = q->q_ptr;
12435 	phyint_t	*phyi = ill->ill_phyint;
12436 	ipmp_grp_t	*grp = phyi->phyint_grp;
12437 	ip_stack_t	*ipst = ill->ill_ipst;
12438 
12439 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12440 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12441 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12442 
12443 	if (phyi->phyint_illv4 != NULL) {
12444 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12445 		VERIFY(grp->gr_pendv4-- > 0);
12446 		rw_exit(&ipst->ips_ipmp_lock);
12447 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12448 	}
12449 	if (phyi->phyint_illv6 != NULL) {
12450 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12451 		VERIFY(grp->gr_pendv6-- > 0);
12452 		rw_exit(&ipst->ips_ipmp_lock);
12453 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12454 	}
12455 	freemsg(mp);
12456 }
12457 
12458 /*
12459  * Process an SIOCSLIFGROUPNAME request.
12460  */
12461 /* ARGSUSED */
12462 int
12463 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12464     ip_ioctl_cmd_t *ipip, void *ifreq)
12465 {
12466 	struct lifreq	*lifr = ifreq;
12467 	ill_t		*ill = ipif->ipif_ill;
12468 	ip_stack_t	*ipst = ill->ill_ipst;
12469 	phyint_t	*phyi = ill->ill_phyint;
12470 	ipmp_grp_t	*grp = phyi->phyint_grp;
12471 	mblk_t		*ipsq_mp;
12472 	int		err = 0;
12473 
12474 	/*
12475 	 * Note that phyint_grp can only change here, where we're exclusive.
12476 	 */
12477 	ASSERT(IAM_WRITER_ILL(ill));
12478 
12479 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12480 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12481 		return (EINVAL);
12482 
12483 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12484 
12485 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12486 
12487 	/*
12488 	 * If the name hasn't changed, there's nothing to do.
12489 	 */
12490 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12491 		goto unlock;
12492 
12493 	/*
12494 	 * Handle requests to rename an IPMP meta-interface.
12495 	 *
12496 	 * Note that creation of the IPMP meta-interface is handled in
12497 	 * userland through the standard plumbing sequence.  As part of the
12498 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12499 	 * the name of the interface (see ipif_set_values_tail()).
12500 	 */
12501 	if (IS_IPMP(ill)) {
12502 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12503 		goto unlock;
12504 	}
12505 
12506 	/*
12507 	 * Handle requests to add or remove an IP interface from a group.
12508 	 */
12509 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12510 		/*
12511 		 * Moves are handled by first removing the interface from
12512 		 * its existing group, and then adding it to another group.
12513 		 * So, fail if it's already in a group.
12514 		 */
12515 		if (IS_UNDER_IPMP(ill)) {
12516 			err = EALREADY;
12517 			goto unlock;
12518 		}
12519 
12520 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12521 		if (grp == NULL) {
12522 			err = ENOENT;
12523 			goto unlock;
12524 		}
12525 
12526 		/*
12527 		 * Check if the phyint and its ills are suitable for
12528 		 * inclusion into the group.
12529 		 */
12530 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12531 			goto unlock;
12532 
12533 		/*
12534 		 * Checks pass; join the group, and enqueue the remaining
12535 		 * illgrp joins for when we've become part of the group xop
12536 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12537 		 * requires an mblk_t to scribble on, and since `mp' will be
12538 		 * freed as part of completing the ioctl, allocate another.
12539 		 */
12540 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12541 			err = ENOMEM;
12542 			goto unlock;
12543 		}
12544 
12545 		/*
12546 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12547 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12548 		 * before ip_join_illgrps() is called back.  See the comments
12549 		 * in ip_sioctl_plink_ipmp() for more.
12550 		 */
12551 		if (phyi->phyint_illv4 != NULL)
12552 			grp->gr_pendv4++;
12553 		if (phyi->phyint_illv6 != NULL)
12554 			grp->gr_pendv6++;
12555 
12556 		rw_exit(&ipst->ips_ipmp_lock);
12557 
12558 		ipmp_phyint_join_grp(phyi, grp);
12559 		ill_refhold(ill);
12560 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12561 		    SWITCH_OP, B_FALSE);
12562 		return (0);
12563 	} else {
12564 		/*
12565 		 * Request to remove the interface from a group.  If the
12566 		 * interface is not in a group, this trivially succeeds.
12567 		 */
12568 		rw_exit(&ipst->ips_ipmp_lock);
12569 		if (IS_UNDER_IPMP(ill))
12570 			ipmp_phyint_leave_grp(phyi);
12571 		return (0);
12572 	}
12573 unlock:
12574 	rw_exit(&ipst->ips_ipmp_lock);
12575 	return (err);
12576 }
12577 
12578 /*
12579  * Process an SIOCGLIFBINDING request.
12580  */
12581 /* ARGSUSED */
12582 int
12583 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12584     ip_ioctl_cmd_t *ipip, void *ifreq)
12585 {
12586 	ill_t		*ill;
12587 	struct lifreq	*lifr = ifreq;
12588 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12589 
12590 	if (!IS_IPMP(ipif->ipif_ill))
12591 		return (EINVAL);
12592 
12593 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12594 	if ((ill = ipif->ipif_bound_ill) == NULL)
12595 		lifr->lifr_binding[0] = '\0';
12596 	else
12597 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12598 	rw_exit(&ipst->ips_ipmp_lock);
12599 	return (0);
12600 }
12601 
12602 /*
12603  * Process an SIOCGLIFGROUPNAME request.
12604  */
12605 /* ARGSUSED */
12606 int
12607 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12608     ip_ioctl_cmd_t *ipip, void *ifreq)
12609 {
12610 	ipmp_grp_t	*grp;
12611 	struct lifreq	*lifr = ifreq;
12612 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12613 
12614 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12615 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12616 		lifr->lifr_groupname[0] = '\0';
12617 	else
12618 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12619 	rw_exit(&ipst->ips_ipmp_lock);
12620 	return (0);
12621 }
12622 
12623 /*
12624  * Process an SIOCGLIFGROUPINFO request.
12625  */
12626 /* ARGSUSED */
12627 int
12628 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12629     ip_ioctl_cmd_t *ipip, void *dummy)
12630 {
12631 	ipmp_grp_t	*grp;
12632 	lifgroupinfo_t	*lifgr;
12633 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12634 
12635 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12636 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12637 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12638 
12639 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12640 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12641 		rw_exit(&ipst->ips_ipmp_lock);
12642 		return (ENOENT);
12643 	}
12644 	ipmp_grp_info(grp, lifgr);
12645 	rw_exit(&ipst->ips_ipmp_lock);
12646 	return (0);
12647 }
12648 
12649 static void
12650 ill_dl_down(ill_t *ill)
12651 {
12652 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12653 
12654 	/*
12655 	 * The ill is down; unbind but stay attached since we're still
12656 	 * associated with a PPA. If we have negotiated DLPI capabilites
12657 	 * with the data link service provider (IDS_OK) then reset them.
12658 	 * The interval between unbinding and rebinding is potentially
12659 	 * unbounded hence we cannot assume things will be the same.
12660 	 * The DLPI capabilities will be probed again when the data link
12661 	 * is brought up.
12662 	 */
12663 	mblk_t	*mp = ill->ill_unbind_mp;
12664 
12665 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12666 
12667 	if (!ill->ill_replumbing) {
12668 		/* Free all ilms for this ill */
12669 		update_conn_ill(ill, ill->ill_ipst);
12670 	} else {
12671 		ill_leave_multicast(ill);
12672 	}
12673 
12674 	ill->ill_unbind_mp = NULL;
12675 	if (mp != NULL) {
12676 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12677 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12678 		    ill->ill_name));
12679 		mutex_enter(&ill->ill_lock);
12680 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12681 		mutex_exit(&ill->ill_lock);
12682 		/*
12683 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12684 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12685 		 * ill_capability_dld_disable disable rightaway. If this is not
12686 		 * an unplumb operation then the disable happens on receipt of
12687 		 * the capab ack via ip_rput_dlpi_writer ->
12688 		 * ill_capability_ack_thr. In both cases the order of
12689 		 * the operations seen by DLD is capability disable followed
12690 		 * by DL_UNBIND. Also the DLD capability disable needs a
12691 		 * cv_wait'able context.
12692 		 */
12693 		if (ill->ill_state_flags & ILL_CONDEMNED)
12694 			ill_capability_dld_disable(ill);
12695 		ill_capability_reset(ill, B_FALSE);
12696 		ill_dlpi_send(ill, mp);
12697 	}
12698 	mutex_enter(&ill->ill_lock);
12699 	ill->ill_dl_up = 0;
12700 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12701 	mutex_exit(&ill->ill_lock);
12702 }
12703 
12704 void
12705 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12706 {
12707 	union DL_primitives *dlp;
12708 	t_uscalar_t prim;
12709 	boolean_t waitack = B_FALSE;
12710 
12711 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12712 
12713 	dlp = (union DL_primitives *)mp->b_rptr;
12714 	prim = dlp->dl_primitive;
12715 
12716 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12717 	    dl_primstr(prim), prim, ill->ill_name));
12718 
12719 	switch (prim) {
12720 	case DL_PHYS_ADDR_REQ:
12721 	{
12722 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12723 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12724 		break;
12725 	}
12726 	case DL_BIND_REQ:
12727 		mutex_enter(&ill->ill_lock);
12728 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12729 		mutex_exit(&ill->ill_lock);
12730 		break;
12731 	}
12732 
12733 	/*
12734 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12735 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12736 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12737 	 */
12738 	mutex_enter(&ill->ill_lock);
12739 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12740 	    (prim == DL_UNBIND_REQ)) {
12741 		ill->ill_dlpi_pending = prim;
12742 		waitack = B_TRUE;
12743 	}
12744 
12745 	mutex_exit(&ill->ill_lock);
12746 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12747 	    char *, dl_primstr(prim), ill_t *, ill);
12748 	putnext(ill->ill_wq, mp);
12749 
12750 	/*
12751 	 * There is no ack for DL_NOTIFY_CONF messages
12752 	 */
12753 	if (waitack && prim == DL_NOTIFY_CONF)
12754 		ill_dlpi_done(ill, prim);
12755 }
12756 
12757 /*
12758  * Helper function for ill_dlpi_send().
12759  */
12760 /* ARGSUSED */
12761 static void
12762 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12763 {
12764 	ill_dlpi_send(q->q_ptr, mp);
12765 }
12766 
12767 /*
12768  * Send a DLPI control message to the driver but make sure there
12769  * is only one outstanding message. Uses ill_dlpi_pending to tell
12770  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12771  * when an ACK or a NAK is received to process the next queued message.
12772  */
12773 void
12774 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12775 {
12776 	mblk_t **mpp;
12777 
12778 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12779 
12780 	/*
12781 	 * To ensure that any DLPI requests for current exclusive operation
12782 	 * are always completely sent before any DLPI messages for other
12783 	 * operations, require writer access before enqueuing.
12784 	 */
12785 	if (!IAM_WRITER_ILL(ill)) {
12786 		ill_refhold(ill);
12787 		/* qwriter_ip() does the ill_refrele() */
12788 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12789 		    NEW_OP, B_TRUE);
12790 		return;
12791 	}
12792 
12793 	mutex_enter(&ill->ill_lock);
12794 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12795 		/* Must queue message. Tail insertion */
12796 		mpp = &ill->ill_dlpi_deferred;
12797 		while (*mpp != NULL)
12798 			mpp = &((*mpp)->b_next);
12799 
12800 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12801 		    "while %s pending\n", ill->ill_name,
12802 		    dl_primstr(ill->ill_dlpi_pending)));
12803 
12804 		*mpp = mp;
12805 		mutex_exit(&ill->ill_lock);
12806 		return;
12807 	}
12808 	mutex_exit(&ill->ill_lock);
12809 	ill_dlpi_dispatch(ill, mp);
12810 }
12811 
12812 void
12813 ill_capability_send(ill_t *ill, mblk_t *mp)
12814 {
12815 	ill->ill_capab_pending_cnt++;
12816 	ill_dlpi_send(ill, mp);
12817 }
12818 
12819 void
12820 ill_capability_done(ill_t *ill)
12821 {
12822 	ASSERT(ill->ill_capab_pending_cnt != 0);
12823 
12824 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12825 
12826 	ill->ill_capab_pending_cnt--;
12827 	if (ill->ill_capab_pending_cnt == 0 &&
12828 	    ill->ill_dlpi_capab_state == IDCS_OK)
12829 		ill_capability_reset_alloc(ill);
12830 }
12831 
12832 /*
12833  * Send all deferred DLPI messages without waiting for their ACKs.
12834  */
12835 void
12836 ill_dlpi_send_deferred(ill_t *ill)
12837 {
12838 	mblk_t *mp, *nextmp;
12839 
12840 	/*
12841 	 * Clear ill_dlpi_pending so that the message is not queued in
12842 	 * ill_dlpi_send().
12843 	 */
12844 	mutex_enter(&ill->ill_lock);
12845 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12846 	mp = ill->ill_dlpi_deferred;
12847 	ill->ill_dlpi_deferred = NULL;
12848 	mutex_exit(&ill->ill_lock);
12849 
12850 	for (; mp != NULL; mp = nextmp) {
12851 		nextmp = mp->b_next;
12852 		mp->b_next = NULL;
12853 		ill_dlpi_send(ill, mp);
12854 	}
12855 }
12856 
12857 /*
12858  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12859  * or M_HANGUP
12860  */
12861 static void
12862 ill_dlpi_clear_deferred(ill_t *ill)
12863 {
12864 	mblk_t	*mp, *nextmp;
12865 
12866 	mutex_enter(&ill->ill_lock);
12867 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12868 	mp = ill->ill_dlpi_deferred;
12869 	ill->ill_dlpi_deferred = NULL;
12870 	mutex_exit(&ill->ill_lock);
12871 
12872 	for (; mp != NULL; mp = nextmp) {
12873 		nextmp = mp->b_next;
12874 		inet_freemsg(mp);
12875 	}
12876 }
12877 
12878 /*
12879  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12880  */
12881 boolean_t
12882 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12883 {
12884 	t_uscalar_t pending;
12885 
12886 	mutex_enter(&ill->ill_lock);
12887 	if (ill->ill_dlpi_pending == prim) {
12888 		mutex_exit(&ill->ill_lock);
12889 		return (B_TRUE);
12890 	}
12891 
12892 	/*
12893 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12894 	 * without waiting, so don't print any warnings in that case.
12895 	 */
12896 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12897 		mutex_exit(&ill->ill_lock);
12898 		return (B_FALSE);
12899 	}
12900 	pending = ill->ill_dlpi_pending;
12901 	mutex_exit(&ill->ill_lock);
12902 
12903 	if (pending == DL_PRIM_INVAL) {
12904 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12905 		    "received unsolicited ack for %s on %s\n",
12906 		    dl_primstr(prim), ill->ill_name);
12907 	} else {
12908 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12909 		    "received unexpected ack for %s on %s (expecting %s)\n",
12910 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12911 	}
12912 	return (B_FALSE);
12913 }
12914 
12915 /*
12916  * Complete the current DLPI operation associated with `prim' on `ill' and
12917  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12918  * operations and the ill's current exclusive IPSQ operation has finished
12919  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12920  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12921  * the comments above ipsq_current_finish() for details.
12922  */
12923 void
12924 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12925 {
12926 	mblk_t *mp;
12927 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12928 	ipxop_t *ipx = ipsq->ipsq_xop;
12929 
12930 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12931 	mutex_enter(&ill->ill_lock);
12932 
12933 	ASSERT(prim != DL_PRIM_INVAL);
12934 	ASSERT(ill->ill_dlpi_pending == prim);
12935 
12936 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12937 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12938 
12939 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12940 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12941 		if (ipx->ipx_current_done) {
12942 			mutex_enter(&ipx->ipx_lock);
12943 			ipx->ipx_current_ipif = NULL;
12944 			mutex_exit(&ipx->ipx_lock);
12945 		}
12946 		cv_signal(&ill->ill_cv);
12947 		mutex_exit(&ill->ill_lock);
12948 		return;
12949 	}
12950 
12951 	ill->ill_dlpi_deferred = mp->b_next;
12952 	mp->b_next = NULL;
12953 	mutex_exit(&ill->ill_lock);
12954 
12955 	ill_dlpi_dispatch(ill, mp);
12956 }
12957 
12958 /*
12959  * Queue a (multicast) DLPI control message to be sent to the driver by
12960  * later calling ill_dlpi_send_queued.
12961  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12962  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12963  * for the same group to race.
12964  * We send DLPI control messages in order using ill_lock.
12965  * For IPMP we should be called on the cast_ill.
12966  */
12967 void
12968 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12969 {
12970 	mblk_t **mpp;
12971 
12972 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12973 
12974 	mutex_enter(&ill->ill_lock);
12975 	/* Must queue message. Tail insertion */
12976 	mpp = &ill->ill_dlpi_deferred;
12977 	while (*mpp != NULL)
12978 		mpp = &((*mpp)->b_next);
12979 
12980 	*mpp = mp;
12981 	mutex_exit(&ill->ill_lock);
12982 }
12983 
12984 /*
12985  * Send the messages that were queued. Make sure there is only
12986  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12987  * when an ACK or a NAK is received to process the next queued message.
12988  * For IPMP we are called on the upper ill, but when send what is queued
12989  * on the cast_ill.
12990  */
12991 void
12992 ill_dlpi_send_queued(ill_t *ill)
12993 {
12994 	mblk_t	*mp;
12995 	union DL_primitives *dlp;
12996 	t_uscalar_t prim;
12997 	ill_t *release_ill = NULL;
12998 
12999 	if (IS_IPMP(ill)) {
13000 		/* On the upper IPMP ill. */
13001 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13002 		if (release_ill == NULL) {
13003 			/* Avoid ever sending anything down to the ipmpstub */
13004 			return;
13005 		}
13006 		ill = release_ill;
13007 	}
13008 	mutex_enter(&ill->ill_lock);
13009 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
13010 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13011 			/* Can't send. Somebody else will send it */
13012 			mutex_exit(&ill->ill_lock);
13013 			goto done;
13014 		}
13015 		ill->ill_dlpi_deferred = mp->b_next;
13016 		mp->b_next = NULL;
13017 		if (!ill->ill_dl_up) {
13018 			/*
13019 			 * Nobody there. All multicast addresses will be
13020 			 * re-joined when we get the DL_BIND_ACK bringing the
13021 			 * interface up.
13022 			 */
13023 			freemsg(mp);
13024 			continue;
13025 		}
13026 		dlp = (union DL_primitives *)mp->b_rptr;
13027 		prim = dlp->dl_primitive;
13028 
13029 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13030 		    (prim == DL_UNBIND_REQ)) {
13031 			ill->ill_dlpi_pending = prim;
13032 		}
13033 		mutex_exit(&ill->ill_lock);
13034 
13035 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13036 		    char *, dl_primstr(prim), ill_t *, ill);
13037 		putnext(ill->ill_wq, mp);
13038 		mutex_enter(&ill->ill_lock);
13039 	}
13040 	mutex_exit(&ill->ill_lock);
13041 done:
13042 	if (release_ill != NULL)
13043 		ill_refrele(release_ill);
13044 }
13045 
13046 /*
13047  * Queue an IP (IGMP/MLD) message to be sent by IP from
13048  * ill_mcast_send_queued
13049  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13050  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13051  * group to race.
13052  * We send them in order using ill_lock.
13053  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13054  */
13055 void
13056 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13057 {
13058 	mblk_t **mpp;
13059 	ill_t *release_ill = NULL;
13060 
13061 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13062 
13063 	if (IS_IPMP(ill)) {
13064 		/* On the upper IPMP ill. */
13065 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13066 		if (release_ill == NULL) {
13067 			/* Discard instead of queuing for the ipmp interface */
13068 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13069 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13070 			    mp, ill);
13071 			freemsg(mp);
13072 			return;
13073 		}
13074 		ill = release_ill;
13075 	}
13076 
13077 	mutex_enter(&ill->ill_lock);
13078 	/* Must queue message. Tail insertion */
13079 	mpp = &ill->ill_mcast_deferred;
13080 	while (*mpp != NULL)
13081 		mpp = &((*mpp)->b_next);
13082 
13083 	*mpp = mp;
13084 	mutex_exit(&ill->ill_lock);
13085 	if (release_ill != NULL)
13086 		ill_refrele(release_ill);
13087 }
13088 
13089 /*
13090  * Send the IP packets that were queued by ill_mcast_queue.
13091  * These are IGMP/MLD packets.
13092  *
13093  * For IPMP we are called on the upper ill, but when send what is queued
13094  * on the cast_ill.
13095  *
13096  * Request loopback of the report if we are acting as a multicast
13097  * router, so that the process-level routing demon can hear it.
13098  * This will run multiple times for the same group if there are members
13099  * on the same group for multiple ipif's on the same ill. The
13100  * igmp_input/mld_input code will suppress this due to the loopback thus we
13101  * always loopback membership report.
13102  *
13103  * We also need to make sure that this does not get load balanced
13104  * by IPMP. We do this by passing an ill to ip_output_simple.
13105  */
13106 void
13107 ill_mcast_send_queued(ill_t *ill)
13108 {
13109 	mblk_t	*mp;
13110 	ip_xmit_attr_t ixas;
13111 	ill_t *release_ill = NULL;
13112 
13113 	if (IS_IPMP(ill)) {
13114 		/* On the upper IPMP ill. */
13115 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13116 		if (release_ill == NULL) {
13117 			/*
13118 			 * We should have no messages on the ipmp interface
13119 			 * but no point in trying to send them.
13120 			 */
13121 			return;
13122 		}
13123 		ill = release_ill;
13124 	}
13125 	bzero(&ixas, sizeof (ixas));
13126 	ixas.ixa_zoneid = ALL_ZONES;
13127 	ixas.ixa_cred = kcred;
13128 	ixas.ixa_cpid = NOPID;
13129 	ixas.ixa_tsl = NULL;
13130 	/*
13131 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13132 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13133 	 * That is necessary to handle IGMP/MLD snooping switches.
13134 	 */
13135 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13136 	ixas.ixa_ipst = ill->ill_ipst;
13137 
13138 	mutex_enter(&ill->ill_lock);
13139 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13140 		ill->ill_mcast_deferred = mp->b_next;
13141 		mp->b_next = NULL;
13142 		if (!ill->ill_dl_up) {
13143 			/*
13144 			 * Nobody there. Just drop the ip packets.
13145 			 * IGMP/MLD will resend later, if this is a replumb.
13146 			 */
13147 			freemsg(mp);
13148 			continue;
13149 		}
13150 		mutex_enter(&ill->ill_phyint->phyint_lock);
13151 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13152 			/*
13153 			 * When the ill is getting deactivated, we only want to
13154 			 * send the DLPI messages, so drop IGMP/MLD packets.
13155 			 * DLPI messages are handled by ill_dlpi_send_queued()
13156 			 */
13157 			mutex_exit(&ill->ill_phyint->phyint_lock);
13158 			freemsg(mp);
13159 			continue;
13160 		}
13161 		mutex_exit(&ill->ill_phyint->phyint_lock);
13162 		mutex_exit(&ill->ill_lock);
13163 
13164 		/* Check whether we are sending IPv4 or IPv6. */
13165 		if (ill->ill_isv6) {
13166 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13167 
13168 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13169 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13170 		} else {
13171 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13172 
13173 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13174 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13175 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13176 		}
13177 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13178 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13179 		(void) ip_output_simple(mp, &ixas);
13180 		ixa_cleanup(&ixas);
13181 
13182 		mutex_enter(&ill->ill_lock);
13183 	}
13184 	mutex_exit(&ill->ill_lock);
13185 
13186 done:
13187 	if (release_ill != NULL)
13188 		ill_refrele(release_ill);
13189 }
13190 
13191 /*
13192  * Take down a specific interface, but don't lose any information about it.
13193  * (Always called as writer.)
13194  * This function goes through the down sequence even if the interface is
13195  * already down. There are 2 reasons.
13196  * a. Currently we permit interface routes that depend on down interfaces
13197  *    to be added. This behaviour itself is questionable. However it appears
13198  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13199  *    time. We go thru the cleanup in order to remove these routes.
13200  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13201  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13202  *    down, but we need to cleanup i.e. do ill_dl_down and
13203  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13204  *
13205  * IP-MT notes:
13206  *
13207  * Model of reference to interfaces.
13208  *
13209  * The following members in ipif_t track references to the ipif.
13210  *	int     ipif_refcnt;    Active reference count
13211  *
13212  * The following members in ill_t track references to the ill.
13213  *	int             ill_refcnt;     active refcnt
13214  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13215  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13216  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13217  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13218  *
13219  * Reference to an ipif or ill can be obtained in any of the following ways.
13220  *
13221  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13222  * Pointers to ipif / ill from other data structures viz ire and conn.
13223  * Implicit reference to the ipif / ill by holding a reference to the ire.
13224  *
13225  * The ipif/ill lookup functions return a reference held ipif / ill.
13226  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13227  * This is a purely dynamic reference count associated with threads holding
13228  * references to the ipif / ill. Pointers from other structures do not
13229  * count towards this reference count.
13230  *
13231  * ill_ire_cnt is the number of ire's associated with the
13232  * ill. This is incremented whenever a new ire is created referencing the
13233  * ill. This is done atomically inside ire_add_v[46] where the ire is
13234  * actually added to the ire hash table. The count is decremented in
13235  * ire_inactive where the ire is destroyed.
13236  *
13237  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13238  * This is incremented atomically in
13239  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13240  * table. Similarly it is decremented in ncec_inactive() where the ncec
13241  * is destroyed.
13242  *
13243  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13244  * incremented atomically in nce_add() where the nce is actually added to the
13245  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13246  * is destroyed.
13247  *
13248  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13249  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13250  *
13251  * Flow of ioctls involving interface down/up
13252  *
13253  * The following is the sequence of an attempt to set some critical flags on an
13254  * up interface.
13255  * ip_sioctl_flags
13256  * ipif_down
13257  * wait for ipif to be quiescent
13258  * ipif_down_tail
13259  * ip_sioctl_flags_tail
13260  *
13261  * All set ioctls that involve down/up sequence would have a skeleton similar
13262  * to the above. All the *tail functions are called after the refcounts have
13263  * dropped to the appropriate values.
13264  *
13265  * SIOC ioctls during the IPIF_CHANGING interval.
13266  *
13267  * Threads handling SIOC set ioctls serialize on the squeue, but this
13268  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13269  * steps of internal changes to the state, some of which are visible in
13270  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13271  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13272  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13273  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13274  * the current exclusive operation completes. The IPIF_CHANGING check
13275  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13276  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13277  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13278  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13279  * until we release the ipsq_lock, even though the ill/ipif state flags
13280  * can change after we drop the ill_lock.
13281  */
13282 int
13283 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13284 {
13285 	ill_t		*ill = ipif->ipif_ill;
13286 	conn_t		*connp;
13287 	boolean_t	success;
13288 	boolean_t	ipif_was_up = B_FALSE;
13289 	ip_stack_t	*ipst = ill->ill_ipst;
13290 
13291 	ASSERT(IAM_WRITER_IPIF(ipif));
13292 
13293 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13294 
13295 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13296 	    ill_t *, ill, ipif_t *, ipif);
13297 
13298 	if (ipif->ipif_flags & IPIF_UP) {
13299 		mutex_enter(&ill->ill_lock);
13300 		ipif->ipif_flags &= ~IPIF_UP;
13301 		ASSERT(ill->ill_ipif_up_count > 0);
13302 		--ill->ill_ipif_up_count;
13303 		mutex_exit(&ill->ill_lock);
13304 		ipif_was_up = B_TRUE;
13305 		/* Update status in SCTP's list */
13306 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13307 		ill_nic_event_dispatch(ipif->ipif_ill,
13308 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13309 	}
13310 
13311 	/*
13312 	 * Blow away memberships we established in ipif_multicast_up().
13313 	 */
13314 	ipif_multicast_down(ipif);
13315 
13316 	/*
13317 	 * Remove from the mapping for __sin6_src_id. We insert only
13318 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13319 	 * stored as mapped addresses, we need to check for mapped
13320 	 * INADDR_ANY also.
13321 	 */
13322 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13323 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13324 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13325 		int err;
13326 
13327 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13328 		    ipif->ipif_zoneid, ipst);
13329 		if (err != 0) {
13330 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13331 		}
13332 	}
13333 
13334 	if (ipif_was_up) {
13335 		/* only delete if we'd added ire's before */
13336 		if (ipif->ipif_isv6)
13337 			ipif_delete_ires_v6(ipif);
13338 		else
13339 			ipif_delete_ires_v4(ipif);
13340 	}
13341 
13342 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13343 		/*
13344 		 * Since the interface is now down, it may have just become
13345 		 * inactive.  Note that this needs to be done even for a
13346 		 * lll_logical_down(), or ARP entries will not get correctly
13347 		 * restored when the interface comes back up.
13348 		 */
13349 		if (IS_UNDER_IPMP(ill))
13350 			ipmp_ill_refresh_active(ill);
13351 	}
13352 
13353 	/*
13354 	 * neighbor-discovery or arp entries for this interface. The ipif
13355 	 * has to be quiesced, so we walk all the nce's and delete those
13356 	 * that point at the ipif->ipif_ill. At the same time, we also
13357 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13358 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13359 	 * that for ipif_down_tail()
13360 	 */
13361 	ipif_nce_down(ipif);
13362 
13363 	/*
13364 	 * If this is the last ipif on the ill, we also need to remove
13365 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13366 	 * never succeed.
13367 	 */
13368 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13369 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13370 
13371 	/*
13372 	 * Walk all CONNs that can have a reference on an ire for this
13373 	 * ipif (we actually walk all that now have stale references).
13374 	 */
13375 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13376 
13377 	/*
13378 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13379 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13380 	 * and ill_delete -> ipif_free -> ipif_down
13381 	 */
13382 	if (mp == NULL) {
13383 		ASSERT(q == NULL);
13384 		return (0);
13385 	}
13386 
13387 	if (CONN_Q(q)) {
13388 		connp = Q_TO_CONN(q);
13389 		mutex_enter(&connp->conn_lock);
13390 	} else {
13391 		connp = NULL;
13392 	}
13393 	mutex_enter(&ill->ill_lock);
13394 	/*
13395 	 * Are there any ire's pointing to this ipif that are still active ?
13396 	 * If this is the last ipif going down, are there any ire's pointing
13397 	 * to this ill that are still active ?
13398 	 */
13399 	if (ipif_is_quiescent(ipif)) {
13400 		mutex_exit(&ill->ill_lock);
13401 		if (connp != NULL)
13402 			mutex_exit(&connp->conn_lock);
13403 		return (0);
13404 	}
13405 
13406 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13407 	    ill->ill_name, (void *)ill));
13408 	/*
13409 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13410 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13411 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13412 	 */
13413 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13414 	if (!success) {
13415 		/* The conn is closing. So just return */
13416 		ASSERT(connp != NULL);
13417 		mutex_exit(&ill->ill_lock);
13418 		mutex_exit(&connp->conn_lock);
13419 		return (EINTR);
13420 	}
13421 
13422 	mutex_exit(&ill->ill_lock);
13423 	if (connp != NULL)
13424 		mutex_exit(&connp->conn_lock);
13425 	return (EINPROGRESS);
13426 }
13427 
13428 int
13429 ipif_down_tail(ipif_t *ipif)
13430 {
13431 	ill_t	*ill = ipif->ipif_ill;
13432 	int	err = 0;
13433 
13434 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13435 	    ill_t *, ill, ipif_t *, ipif);
13436 
13437 	/*
13438 	 * Skip any loopback interface (null wq).
13439 	 * If this is the last logical interface on the ill
13440 	 * have ill_dl_down tell the driver we are gone (unbind)
13441 	 * Note that lun 0 can ipif_down even though
13442 	 * there are other logical units that are up.
13443 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13444 	 */
13445 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13446 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13447 	    ill->ill_dl_up) {
13448 		ill_dl_down(ill);
13449 	}
13450 	if (!ipif->ipif_isv6)
13451 		err = ipif_arp_down(ipif);
13452 
13453 	ill->ill_logical_down = 0;
13454 
13455 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13456 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13457 	return (err);
13458 }
13459 
13460 /*
13461  * Bring interface logically down without bringing the physical interface
13462  * down e.g. when the netmask is changed. This avoids long lasting link
13463  * negotiations between an ethernet interface and a certain switches.
13464  */
13465 static int
13466 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13467 {
13468 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13469 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13470 
13471 	/*
13472 	 * The ill_logical_down flag is a transient flag. It is set here
13473 	 * and is cleared once the down has completed in ipif_down_tail.
13474 	 * This flag does not indicate whether the ill stream is in the
13475 	 * DL_BOUND state with the driver. Instead this flag is used by
13476 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13477 	 * the driver. The state of the ill stream i.e. whether it is
13478 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13479 	 */
13480 	ipif->ipif_ill->ill_logical_down = 1;
13481 	return (ipif_down(ipif, q, mp));
13482 }
13483 
13484 /*
13485  * Initiate deallocate of an IPIF. Always called as writer. Called by
13486  * ill_delete or ip_sioctl_removeif.
13487  */
13488 static void
13489 ipif_free(ipif_t *ipif)
13490 {
13491 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13492 
13493 	ASSERT(IAM_WRITER_IPIF(ipif));
13494 
13495 	if (ipif->ipif_recovery_id != 0)
13496 		(void) untimeout(ipif->ipif_recovery_id);
13497 	ipif->ipif_recovery_id = 0;
13498 
13499 	/*
13500 	 * Take down the interface. We can be called either from ill_delete
13501 	 * or from ip_sioctl_removeif.
13502 	 */
13503 	(void) ipif_down(ipif, NULL, NULL);
13504 
13505 	/*
13506 	 * Now that the interface is down, there's no chance it can still
13507 	 * become a duplicate.  Cancel any timer that may have been set while
13508 	 * tearing down.
13509 	 */
13510 	if (ipif->ipif_recovery_id != 0)
13511 		(void) untimeout(ipif->ipif_recovery_id);
13512 	ipif->ipif_recovery_id = 0;
13513 
13514 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13515 	/* Remove pointers to this ill in the multicast routing tables */
13516 	reset_mrt_vif_ipif(ipif);
13517 	/* If necessary, clear the cached source ipif rotor. */
13518 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13519 		ipif->ipif_ill->ill_src_ipif = NULL;
13520 	rw_exit(&ipst->ips_ill_g_lock);
13521 }
13522 
13523 static void
13524 ipif_free_tail(ipif_t *ipif)
13525 {
13526 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13527 
13528 	/*
13529 	 * Need to hold both ill_g_lock and ill_lock while
13530 	 * inserting or removing an ipif from the linked list
13531 	 * of ipifs hanging off the ill.
13532 	 */
13533 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13534 
13535 #ifdef DEBUG
13536 	ipif_trace_cleanup(ipif);
13537 #endif
13538 
13539 	/* Ask SCTP to take it out of it list */
13540 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13541 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13542 
13543 	/* Get it out of the ILL interface list. */
13544 	ipif_remove(ipif);
13545 	rw_exit(&ipst->ips_ill_g_lock);
13546 
13547 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13548 	ASSERT(ipif->ipif_recovery_id == 0);
13549 	ASSERT(ipif->ipif_ire_local == NULL);
13550 	ASSERT(ipif->ipif_ire_if == NULL);
13551 
13552 	/* Free the memory. */
13553 	mi_free(ipif);
13554 }
13555 
13556 /*
13557  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13558  * is zero.
13559  */
13560 void
13561 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13562 {
13563 	char	lbuf[LIFNAMSIZ];
13564 	char	*name;
13565 	size_t	name_len;
13566 
13567 	buf[0] = '\0';
13568 	name = ipif->ipif_ill->ill_name;
13569 	name_len = ipif->ipif_ill->ill_name_length;
13570 	if (ipif->ipif_id != 0) {
13571 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13572 		    ipif->ipif_id);
13573 		name = lbuf;
13574 		name_len = mi_strlen(name) + 1;
13575 	}
13576 	len -= 1;
13577 	buf[len] = '\0';
13578 	len = MIN(len, name_len);
13579 	bcopy(name, buf, len);
13580 }
13581 
13582 /*
13583  * Sets `buf' to an ill name.
13584  */
13585 void
13586 ill_get_name(const ill_t *ill, char *buf, int len)
13587 {
13588 	char	*name;
13589 	size_t	name_len;
13590 
13591 	name = ill->ill_name;
13592 	name_len = ill->ill_name_length;
13593 	len -= 1;
13594 	buf[len] = '\0';
13595 	len = MIN(len, name_len);
13596 	bcopy(name, buf, len);
13597 }
13598 
13599 /*
13600  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13601  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13602  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13603  * (May be called as writer.)
13604  */
13605 static ipif_t *
13606 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13607     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13608 {
13609 	char	*cp;
13610 	char	*endp;
13611 	long	id;
13612 	ill_t	*ill;
13613 	ipif_t	*ipif;
13614 	uint_t	ire_type;
13615 	boolean_t did_alloc = B_FALSE;
13616 
13617 	/*
13618 	 * If the caller wants to us to create the ipif, make sure we have a
13619 	 * valid zoneid
13620 	 */
13621 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13622 
13623 	if (namelen == 0) {
13624 		return (NULL);
13625 	}
13626 
13627 	*exists = B_FALSE;
13628 	/* Look for a colon in the name. */
13629 	endp = &name[namelen];
13630 	for (cp = endp; --cp > name; ) {
13631 		if (*cp == IPIF_SEPARATOR_CHAR)
13632 			break;
13633 	}
13634 
13635 	if (*cp == IPIF_SEPARATOR_CHAR) {
13636 		/*
13637 		 * Reject any non-decimal aliases for logical
13638 		 * interfaces. Aliases with leading zeroes
13639 		 * are also rejected as they introduce ambiguity
13640 		 * in the naming of the interfaces.
13641 		 * In order to confirm with existing semantics,
13642 		 * and to not break any programs/script relying
13643 		 * on that behaviour, if<0>:0 is considered to be
13644 		 * a valid interface.
13645 		 *
13646 		 * If alias has two or more digits and the first
13647 		 * is zero, fail.
13648 		 */
13649 		if (&cp[2] < endp && cp[1] == '0') {
13650 			return (NULL);
13651 		}
13652 	}
13653 
13654 	if (cp <= name) {
13655 		cp = endp;
13656 	} else {
13657 		*cp = '\0';
13658 	}
13659 
13660 	/*
13661 	 * Look up the ILL, based on the portion of the name
13662 	 * before the slash. ill_lookup_on_name returns a held ill.
13663 	 * Temporary to check whether ill exists already. If so
13664 	 * ill_lookup_on_name will clear it.
13665 	 */
13666 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13667 	    &did_alloc, ipst);
13668 	if (cp != endp)
13669 		*cp = IPIF_SEPARATOR_CHAR;
13670 	if (ill == NULL)
13671 		return (NULL);
13672 
13673 	/* Establish the unit number in the name. */
13674 	id = 0;
13675 	if (cp < endp && *endp == '\0') {
13676 		/* If there was a colon, the unit number follows. */
13677 		cp++;
13678 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13679 			ill_refrele(ill);
13680 			return (NULL);
13681 		}
13682 	}
13683 
13684 	mutex_enter(&ill->ill_lock);
13685 	/* Now see if there is an IPIF with this unit number. */
13686 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13687 		if (ipif->ipif_id == id) {
13688 			if (zoneid != ALL_ZONES &&
13689 			    zoneid != ipif->ipif_zoneid &&
13690 			    ipif->ipif_zoneid != ALL_ZONES) {
13691 				mutex_exit(&ill->ill_lock);
13692 				ill_refrele(ill);
13693 				return (NULL);
13694 			}
13695 			if (IPIF_CAN_LOOKUP(ipif)) {
13696 				ipif_refhold_locked(ipif);
13697 				mutex_exit(&ill->ill_lock);
13698 				if (!did_alloc)
13699 					*exists = B_TRUE;
13700 				/*
13701 				 * Drop locks before calling ill_refrele
13702 				 * since it can potentially call into
13703 				 * ipif_ill_refrele_tail which can end up
13704 				 * in trying to acquire any lock.
13705 				 */
13706 				ill_refrele(ill);
13707 				return (ipif);
13708 			}
13709 		}
13710 	}
13711 
13712 	if (!do_alloc) {
13713 		mutex_exit(&ill->ill_lock);
13714 		ill_refrele(ill);
13715 		return (NULL);
13716 	}
13717 
13718 	/*
13719 	 * If none found, atomically allocate and return a new one.
13720 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13721 	 * to support "receive only" use of lo0:1 etc. as is still done
13722 	 * below as an initial guess.
13723 	 * However, this is now likely to be overriden later in ipif_up_done()
13724 	 * when we know for sure what address has been configured on the
13725 	 * interface, since we might have more than one loopback interface
13726 	 * with a loopback address, e.g. in the case of zones, and all the
13727 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13728 	 */
13729 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13730 		ire_type = IRE_LOOPBACK;
13731 	else
13732 		ire_type = IRE_LOCAL;
13733 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13734 	if (ipif != NULL)
13735 		ipif_refhold_locked(ipif);
13736 	mutex_exit(&ill->ill_lock);
13737 	ill_refrele(ill);
13738 	return (ipif);
13739 }
13740 
13741 /*
13742  * This routine is called whenever a new address comes up on an ipif.  If
13743  * we are configured to respond to address mask requests, then we are supposed
13744  * to broadcast an address mask reply at this time.  This routine is also
13745  * called if we are already up, but a netmask change is made.  This is legal
13746  * but might not make the system manager very popular.	(May be called
13747  * as writer.)
13748  */
13749 void
13750 ipif_mask_reply(ipif_t *ipif)
13751 {
13752 	icmph_t	*icmph;
13753 	ipha_t	*ipha;
13754 	mblk_t	*mp;
13755 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13756 	ip_xmit_attr_t ixas;
13757 
13758 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13759 
13760 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13761 		return;
13762 
13763 	/* ICMP mask reply is IPv4 only */
13764 	ASSERT(!ipif->ipif_isv6);
13765 	/* ICMP mask reply is not for a loopback interface */
13766 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13767 
13768 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13769 		return;
13770 
13771 	mp = allocb(REPLY_LEN, BPRI_HI);
13772 	if (mp == NULL)
13773 		return;
13774 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13775 
13776 	ipha = (ipha_t *)mp->b_rptr;
13777 	bzero(ipha, REPLY_LEN);
13778 	*ipha = icmp_ipha;
13779 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13780 	ipha->ipha_src = ipif->ipif_lcl_addr;
13781 	ipha->ipha_dst = ipif->ipif_brd_addr;
13782 	ipha->ipha_length = htons(REPLY_LEN);
13783 	ipha->ipha_ident = 0;
13784 
13785 	icmph = (icmph_t *)&ipha[1];
13786 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13787 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13788 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13789 
13790 	bzero(&ixas, sizeof (ixas));
13791 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13792 	ixas.ixa_zoneid = ALL_ZONES;
13793 	ixas.ixa_ifindex = 0;
13794 	ixas.ixa_ipst = ipst;
13795 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13796 	(void) ip_output_simple(mp, &ixas);
13797 	ixa_cleanup(&ixas);
13798 #undef	REPLY_LEN
13799 }
13800 
13801 /*
13802  * Join the ipif specific multicast groups.
13803  * Must be called after a mapping has been set up in the resolver.  (Always
13804  * called as writer.)
13805  */
13806 void
13807 ipif_multicast_up(ipif_t *ipif)
13808 {
13809 	int err;
13810 	ill_t *ill;
13811 	ilm_t *ilm;
13812 
13813 	ASSERT(IAM_WRITER_IPIF(ipif));
13814 
13815 	ill = ipif->ipif_ill;
13816 
13817 	ip1dbg(("ipif_multicast_up\n"));
13818 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13819 	    ipif->ipif_allhosts_ilm != NULL)
13820 		return;
13821 
13822 	if (ipif->ipif_isv6) {
13823 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13824 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13825 
13826 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13827 
13828 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13829 			return;
13830 
13831 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13832 
13833 		/*
13834 		 * Join the all hosts multicast address.  We skip this for
13835 		 * underlying IPMP interfaces since they should be invisible.
13836 		 */
13837 		if (!IS_UNDER_IPMP(ill)) {
13838 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13839 			    &err);
13840 			if (ilm == NULL) {
13841 				ASSERT(err != 0);
13842 				ip0dbg(("ipif_multicast_up: "
13843 				    "all_hosts_mcast failed %d\n", err));
13844 				return;
13845 			}
13846 			ipif->ipif_allhosts_ilm = ilm;
13847 		}
13848 
13849 		/*
13850 		 * Enable multicast for the solicited node multicast address.
13851 		 * If IPMP we need to put the membership on the upper ill.
13852 		 */
13853 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13854 			ill_t *mcast_ill = NULL;
13855 			boolean_t need_refrele;
13856 
13857 			if (IS_UNDER_IPMP(ill) &&
13858 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13859 				need_refrele = B_TRUE;
13860 			} else {
13861 				mcast_ill = ill;
13862 				need_refrele = B_FALSE;
13863 			}
13864 
13865 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13866 			    ipif->ipif_zoneid, &err);
13867 			if (need_refrele)
13868 				ill_refrele(mcast_ill);
13869 
13870 			if (ilm == NULL) {
13871 				ASSERT(err != 0);
13872 				ip0dbg(("ipif_multicast_up: solicited MC"
13873 				    " failed %d\n", err));
13874 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13875 					ipif->ipif_allhosts_ilm = NULL;
13876 					(void) ip_delmulti(ilm);
13877 				}
13878 				return;
13879 			}
13880 			ipif->ipif_solmulti_ilm = ilm;
13881 		}
13882 	} else {
13883 		in6_addr_t v6group;
13884 
13885 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13886 			return;
13887 
13888 		/* Join the all hosts multicast address */
13889 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13890 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13891 
13892 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13893 		if (ilm == NULL) {
13894 			ASSERT(err != 0);
13895 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13896 			return;
13897 		}
13898 		ipif->ipif_allhosts_ilm = ilm;
13899 	}
13900 }
13901 
13902 /*
13903  * Blow away any multicast groups that we joined in ipif_multicast_up().
13904  * (ilms from explicit memberships are handled in conn_update_ill.)
13905  */
13906 void
13907 ipif_multicast_down(ipif_t *ipif)
13908 {
13909 	ASSERT(IAM_WRITER_IPIF(ipif));
13910 
13911 	ip1dbg(("ipif_multicast_down\n"));
13912 
13913 	if (ipif->ipif_allhosts_ilm != NULL) {
13914 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13915 		ipif->ipif_allhosts_ilm = NULL;
13916 	}
13917 	if (ipif->ipif_solmulti_ilm != NULL) {
13918 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13919 		ipif->ipif_solmulti_ilm = NULL;
13920 	}
13921 }
13922 
13923 /*
13924  * Used when an interface comes up to recreate any extra routes on this
13925  * interface.
13926  */
13927 int
13928 ill_recover_saved_ire(ill_t *ill)
13929 {
13930 	mblk_t		*mp;
13931 	ip_stack_t	*ipst = ill->ill_ipst;
13932 
13933 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13934 
13935 	mutex_enter(&ill->ill_saved_ire_lock);
13936 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13937 		ire_t		*ire, *nire;
13938 		ifrt_t		*ifrt;
13939 
13940 		ifrt = (ifrt_t *)mp->b_rptr;
13941 		/*
13942 		 * Create a copy of the IRE with the saved address and netmask.
13943 		 */
13944 		if (ill->ill_isv6) {
13945 			ire = ire_create_v6(
13946 			    &ifrt->ifrt_v6addr,
13947 			    &ifrt->ifrt_v6mask,
13948 			    &ifrt->ifrt_v6gateway_addr,
13949 			    ifrt->ifrt_type,
13950 			    ill,
13951 			    ifrt->ifrt_zoneid,
13952 			    ifrt->ifrt_flags,
13953 			    NULL,
13954 			    ipst);
13955 		} else {
13956 			ire = ire_create(
13957 			    (uint8_t *)&ifrt->ifrt_addr,
13958 			    (uint8_t *)&ifrt->ifrt_mask,
13959 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13960 			    ifrt->ifrt_type,
13961 			    ill,
13962 			    ifrt->ifrt_zoneid,
13963 			    ifrt->ifrt_flags,
13964 			    NULL,
13965 			    ipst);
13966 		}
13967 		if (ire == NULL) {
13968 			mutex_exit(&ill->ill_saved_ire_lock);
13969 			return (ENOMEM);
13970 		}
13971 
13972 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13973 			if (ill->ill_isv6) {
13974 				ire->ire_setsrc_addr_v6 =
13975 				    ifrt->ifrt_v6setsrc_addr;
13976 			} else {
13977 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13978 			}
13979 		}
13980 
13981 		/*
13982 		 * Some software (for example, GateD and Sun Cluster) attempts
13983 		 * to create (what amount to) IRE_PREFIX routes with the
13984 		 * loopback address as the gateway.  This is primarily done to
13985 		 * set up prefixes with the RTF_REJECT flag set (for example,
13986 		 * when generating aggregate routes.)
13987 		 *
13988 		 * If the IRE type (as defined by ill->ill_net_type) is
13989 		 * IRE_LOOPBACK, then we map the request into a
13990 		 * IRE_IF_NORESOLVER.
13991 		 */
13992 		if (ill->ill_net_type == IRE_LOOPBACK)
13993 			ire->ire_type = IRE_IF_NORESOLVER;
13994 
13995 		/*
13996 		 * ire held by ire_add, will be refreled' towards the
13997 		 * the end of ipif_up_done
13998 		 */
13999 		nire = ire_add(ire);
14000 		/*
14001 		 * Check if it was a duplicate entry. This handles
14002 		 * the case of two racing route adds for the same route
14003 		 */
14004 		if (nire == NULL) {
14005 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14006 		} else if (nire != ire) {
14007 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14008 			    (void *)nire));
14009 			ire_delete(nire);
14010 		} else {
14011 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14012 			    (void *)nire));
14013 		}
14014 		if (nire != NULL)
14015 			ire_refrele(nire);
14016 	}
14017 	mutex_exit(&ill->ill_saved_ire_lock);
14018 	return (0);
14019 }
14020 
14021 /*
14022  * Used to set the netmask and broadcast address to default values when the
14023  * interface is brought up.  (Always called as writer.)
14024  */
14025 static void
14026 ipif_set_default(ipif_t *ipif)
14027 {
14028 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14029 
14030 	if (!ipif->ipif_isv6) {
14031 		/*
14032 		 * Interface holds an IPv4 address. Default
14033 		 * mask is the natural netmask.
14034 		 */
14035 		if (!ipif->ipif_net_mask) {
14036 			ipaddr_t	v4mask;
14037 
14038 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14039 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14040 		}
14041 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14042 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14043 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14044 		} else {
14045 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14046 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14047 		}
14048 		/*
14049 		 * NOTE: SunOS 4.X does this even if the broadcast address
14050 		 * has been already set thus we do the same here.
14051 		 */
14052 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14053 			ipaddr_t	v4addr;
14054 
14055 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14056 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14057 		}
14058 	} else {
14059 		/*
14060 		 * Interface holds an IPv6-only address.  Default
14061 		 * mask is all-ones.
14062 		 */
14063 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14064 			ipif->ipif_v6net_mask = ipv6_all_ones;
14065 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14066 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14067 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14068 		} else {
14069 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14070 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14071 		}
14072 	}
14073 }
14074 
14075 /*
14076  * Return 0 if this address can be used as local address without causing
14077  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14078  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14079  * Note that the same IPv6 link-local address is allowed as long as the ills
14080  * are not on the same link.
14081  */
14082 int
14083 ip_addr_availability_check(ipif_t *new_ipif)
14084 {
14085 	in6_addr_t our_v6addr;
14086 	ill_t *ill;
14087 	ipif_t *ipif;
14088 	ill_walk_context_t ctx;
14089 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14090 
14091 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14092 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14093 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14094 
14095 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14096 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14097 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14098 		return (0);
14099 
14100 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14101 
14102 	if (new_ipif->ipif_isv6)
14103 		ill = ILL_START_WALK_V6(&ctx, ipst);
14104 	else
14105 		ill = ILL_START_WALK_V4(&ctx, ipst);
14106 
14107 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14108 		for (ipif = ill->ill_ipif; ipif != NULL;
14109 		    ipif = ipif->ipif_next) {
14110 			if ((ipif == new_ipif) ||
14111 			    !(ipif->ipif_flags & IPIF_UP) ||
14112 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14113 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14114 			    &our_v6addr))
14115 				continue;
14116 
14117 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14118 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14119 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14120 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14121 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14122 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14123 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14124 				continue;
14125 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14126 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14127 				continue;
14128 			else if (new_ipif->ipif_ill == ill)
14129 				return (EADDRINUSE);
14130 			else
14131 				return (EADDRNOTAVAIL);
14132 		}
14133 	}
14134 
14135 	return (0);
14136 }
14137 
14138 /*
14139  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14140  * IREs for the ipif.
14141  * When the routine returns EINPROGRESS then mp has been consumed and
14142  * the ioctl will be acked from ip_rput_dlpi.
14143  */
14144 int
14145 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14146 {
14147 	ill_t		*ill = ipif->ipif_ill;
14148 	boolean_t 	isv6 = ipif->ipif_isv6;
14149 	int		err = 0;
14150 	boolean_t	success;
14151 	uint_t		ipif_orig_id;
14152 	ip_stack_t	*ipst = ill->ill_ipst;
14153 
14154 	ASSERT(IAM_WRITER_IPIF(ipif));
14155 
14156 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14157 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14158 	    ill_t *, ill, ipif_t *, ipif);
14159 
14160 	/* Shouldn't get here if it is already up. */
14161 	if (ipif->ipif_flags & IPIF_UP)
14162 		return (EALREADY);
14163 
14164 	/*
14165 	 * If this is a request to bring up a data address on an interface
14166 	 * under IPMP, then move the address to its IPMP meta-interface and
14167 	 * try to bring it up.  One complication is that the zeroth ipif for
14168 	 * an ill is special, in that every ill always has one, and that code
14169 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14170 	 */
14171 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14172 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14173 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14174 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14175 
14176 		/*
14177 		 * The ipif being brought up should be quiesced.  If it's not,
14178 		 * something has gone amiss and we need to bail out.  (If it's
14179 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14180 		 */
14181 		mutex_enter(&ill->ill_lock);
14182 		if (!ipif_is_quiescent(ipif)) {
14183 			mutex_exit(&ill->ill_lock);
14184 			return (EINVAL);
14185 		}
14186 		mutex_exit(&ill->ill_lock);
14187 
14188 		/*
14189 		 * If we're going to need to allocate ipifs, do it prior
14190 		 * to starting the move (and grabbing locks).
14191 		 */
14192 		if (ipif->ipif_id == 0) {
14193 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14194 			    B_FALSE, &err)) == NULL) {
14195 				return (err);
14196 			}
14197 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14198 			    B_FALSE, &err)) == NULL) {
14199 				mi_free(moveipif);
14200 				return (err);
14201 			}
14202 		}
14203 
14204 		/*
14205 		 * Grab or transfer the ipif to move.  During the move, keep
14206 		 * ill_g_lock held to prevent any ill walker threads from
14207 		 * seeing things in an inconsistent state.
14208 		 */
14209 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14210 		if (ipif->ipif_id != 0) {
14211 			ipif_remove(ipif);
14212 		} else {
14213 			ipif_transfer(ipif, moveipif, stubipif);
14214 			ipif = moveipif;
14215 		}
14216 
14217 		/*
14218 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14219 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14220 		 * replace that one.  Otherwise, pick the next available slot.
14221 		 */
14222 		ipif->ipif_ill = ipmp_ill;
14223 		ipif_orig_id = ipif->ipif_id;
14224 
14225 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14226 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14227 			ipif = ipmp_ill->ill_ipif;
14228 		} else {
14229 			ipif->ipif_id = -1;
14230 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14231 				/*
14232 				 * No more available ipif_id's -- put it back
14233 				 * on the original ill and fail the operation.
14234 				 * Since we're writer on the ill, we can be
14235 				 * sure our old slot is still available.
14236 				 */
14237 				ipif->ipif_id = ipif_orig_id;
14238 				ipif->ipif_ill = ill;
14239 				if (ipif_orig_id == 0) {
14240 					ipif_transfer(ipif, ill->ill_ipif,
14241 					    NULL);
14242 				} else {
14243 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14244 				}
14245 				rw_exit(&ipst->ips_ill_g_lock);
14246 				return (err);
14247 			}
14248 		}
14249 		rw_exit(&ipst->ips_ill_g_lock);
14250 
14251 		/*
14252 		 * Tell SCTP that the ipif has moved.  Note that even if we
14253 		 * had to allocate a new ipif, the original sequence id was
14254 		 * preserved and therefore SCTP won't know.
14255 		 */
14256 		sctp_move_ipif(ipif, ill, ipmp_ill);
14257 
14258 		/*
14259 		 * If the ipif being brought up was on slot zero, then we
14260 		 * first need to bring up the placeholder we stuck there.  In
14261 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14262 		 * call to ipif_up() itself, if we successfully bring up the
14263 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14264 		 */
14265 		if (ipif_orig_id == 0) {
14266 			ASSERT(ill->ill_move_ipif == NULL);
14267 			ill->ill_move_ipif = ipif;
14268 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14269 				ASSERT(ill->ill_move_ipif == NULL);
14270 			if (err != EINPROGRESS)
14271 				ill->ill_move_ipif = NULL;
14272 			return (err);
14273 		}
14274 
14275 		/*
14276 		 * Bring it up on the IPMP ill.
14277 		 */
14278 		return (ipif_up(ipif, q, mp));
14279 	}
14280 
14281 	/* Skip arp/ndp for any loopback interface. */
14282 	if (ill->ill_wq != NULL) {
14283 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14284 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14285 
14286 		if (!ill->ill_dl_up) {
14287 			/*
14288 			 * ill_dl_up is not yet set. i.e. we are yet to
14289 			 * DL_BIND with the driver and this is the first
14290 			 * logical interface on the ill to become "up".
14291 			 * Tell the driver to get going (via DL_BIND_REQ).
14292 			 * Note that changing "significant" IFF_ flags
14293 			 * address/netmask etc cause a down/up dance, but
14294 			 * does not cause an unbind (DL_UNBIND) with the driver
14295 			 */
14296 			return (ill_dl_up(ill, ipif, mp, q));
14297 		}
14298 
14299 		/*
14300 		 * ipif_resolver_up may end up needeing to bind/attach
14301 		 * the ARP stream, which in turn necessitates a
14302 		 * DLPI message exchange with the driver. ioctls are
14303 		 * serialized and so we cannot send more than one
14304 		 * interface up message at a time. If ipif_resolver_up
14305 		 * does need to wait for the DLPI handshake for the ARP stream,
14306 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14307 		 */
14308 
14309 		ASSERT(connp != NULL || !CONN_Q(q));
14310 		if (connp != NULL)
14311 			mutex_enter(&connp->conn_lock);
14312 		mutex_enter(&ill->ill_lock);
14313 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14314 		mutex_exit(&ill->ill_lock);
14315 		if (connp != NULL)
14316 			mutex_exit(&connp->conn_lock);
14317 		if (!success)
14318 			return (EINTR);
14319 
14320 		/*
14321 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14322 		 * complete when ipif_ndp_up returns.
14323 		 */
14324 		err = ipif_resolver_up(ipif, Res_act_initial);
14325 		if (err == EINPROGRESS) {
14326 			/* We will complete it in arp_bringup_done() */
14327 			return (err);
14328 		}
14329 
14330 		if (isv6 && err == 0)
14331 			err = ipif_ndp_up(ipif, B_TRUE);
14332 
14333 		ASSERT(err != EINPROGRESS);
14334 		mp = ipsq_pending_mp_get(ipsq, &connp);
14335 		ASSERT(mp != NULL);
14336 		if (err != 0)
14337 			return (err);
14338 	} else {
14339 		/*
14340 		 * Interfaces without underlying hardware don't do duplicate
14341 		 * address detection.
14342 		 */
14343 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14344 		ipif->ipif_addr_ready = 1;
14345 		err = ill_add_ires(ill);
14346 		/* allocation failure? */
14347 		if (err != 0)
14348 			return (err);
14349 	}
14350 
14351 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14352 	if (err == 0 && ill->ill_move_ipif != NULL) {
14353 		ipif = ill->ill_move_ipif;
14354 		ill->ill_move_ipif = NULL;
14355 		return (ipif_up(ipif, q, mp));
14356 	}
14357 	return (err);
14358 }
14359 
14360 /*
14361  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14362  * The identical set of IREs need to be removed in ill_delete_ires().
14363  */
14364 int
14365 ill_add_ires(ill_t *ill)
14366 {
14367 	ire_t	*ire;
14368 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14369 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14370 
14371 	if (ill->ill_ire_multicast != NULL)
14372 		return (0);
14373 
14374 	/*
14375 	 * provide some dummy ire_addr for creating the ire.
14376 	 */
14377 	if (ill->ill_isv6) {
14378 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14379 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14380 	} else {
14381 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14382 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14383 	}
14384 	if (ire == NULL)
14385 		return (ENOMEM);
14386 
14387 	ill->ill_ire_multicast = ire;
14388 	return (0);
14389 }
14390 
14391 void
14392 ill_delete_ires(ill_t *ill)
14393 {
14394 	if (ill->ill_ire_multicast != NULL) {
14395 		/*
14396 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14397 		 * which was taken without any th_tracing enabled.
14398 		 * We also mark it as condemned (note that it was never added)
14399 		 * so that caching conn's can move off of it.
14400 		 */
14401 		ire_make_condemned(ill->ill_ire_multicast);
14402 		ire_refrele_notr(ill->ill_ire_multicast);
14403 		ill->ill_ire_multicast = NULL;
14404 	}
14405 }
14406 
14407 /*
14408  * Perform a bind for the physical device.
14409  * When the routine returns EINPROGRESS then mp has been consumed and
14410  * the ioctl will be acked from ip_rput_dlpi.
14411  * Allocate an unbind message and save it until ipif_down.
14412  */
14413 static int
14414 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14415 {
14416 	mblk_t	*bind_mp = NULL;
14417 	mblk_t	*unbind_mp = NULL;
14418 	conn_t	*connp;
14419 	boolean_t success;
14420 	int	err;
14421 
14422 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14423 
14424 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14425 	ASSERT(IAM_WRITER_ILL(ill));
14426 	ASSERT(mp != NULL);
14427 
14428 	/*
14429 	 * Make sure we have an IRE_MULTICAST in case we immediately
14430 	 * start receiving packets.
14431 	 */
14432 	err = ill_add_ires(ill);
14433 	if (err != 0)
14434 		goto bad;
14435 
14436 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14437 	    DL_BIND_REQ);
14438 	if (bind_mp == NULL)
14439 		goto bad;
14440 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14441 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14442 
14443 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14444 	if (unbind_mp == NULL)
14445 		goto bad;
14446 
14447 	/*
14448 	 * Record state needed to complete this operation when the
14449 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14450 	 */
14451 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14452 	ASSERT(connp != NULL || !CONN_Q(q));
14453 	GRAB_CONN_LOCK(q);
14454 	mutex_enter(&ipif->ipif_ill->ill_lock);
14455 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14456 	mutex_exit(&ipif->ipif_ill->ill_lock);
14457 	RELEASE_CONN_LOCK(q);
14458 	if (!success)
14459 		goto bad;
14460 
14461 	/*
14462 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14463 	 * the interface goes down.
14464 	 */
14465 	ASSERT(ill->ill_unbind_mp == NULL);
14466 	ill->ill_unbind_mp = unbind_mp;
14467 
14468 	ill_dlpi_send(ill, bind_mp);
14469 	/* Send down link-layer capabilities probe if not already done. */
14470 	ill_capability_probe(ill);
14471 
14472 	/*
14473 	 * Sysid used to rely on the fact that netboots set domainname
14474 	 * and the like. Now that miniroot boots aren't strictly netboots
14475 	 * and miniroot network configuration is driven from userland
14476 	 * these things still need to be set. This situation can be detected
14477 	 * by comparing the interface being configured here to the one
14478 	 * dhcifname was set to reference by the boot loader. Once sysid is
14479 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14480 	 */
14481 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14482 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14483 	    (strlen(srpc_domain) == 0)) {
14484 		if (dhcpinit() != 0)
14485 			cmn_err(CE_WARN, "no cached dhcp response");
14486 	}
14487 
14488 	/*
14489 	 * This operation will complete in ip_rput_dlpi with either
14490 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14491 	 */
14492 	return (EINPROGRESS);
14493 bad:
14494 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14495 
14496 	freemsg(bind_mp);
14497 	freemsg(unbind_mp);
14498 	return (ENOMEM);
14499 }
14500 
14501 /* Add room for tcp+ip headers */
14502 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14503 
14504 /*
14505  * DLPI and ARP is up.
14506  * Create all the IREs associated with an interface. Bring up multicast.
14507  * Set the interface flag and finish other initialization
14508  * that potentially had to be deferred to after DL_BIND_ACK.
14509  */
14510 int
14511 ipif_up_done(ipif_t *ipif)
14512 {
14513 	ill_t		*ill = ipif->ipif_ill;
14514 	int		err = 0;
14515 	boolean_t	loopback = B_FALSE;
14516 	boolean_t	update_src_selection = B_TRUE;
14517 	ipif_t		*tmp_ipif;
14518 
14519 	ip1dbg(("ipif_up_done(%s:%u)\n",
14520 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14521 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14522 	    ill_t *, ill, ipif_t *, ipif);
14523 
14524 	/* Check if this is a loopback interface */
14525 	if (ipif->ipif_ill->ill_wq == NULL)
14526 		loopback = B_TRUE;
14527 
14528 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14529 
14530 	/*
14531 	 * If all other interfaces for this ill are down or DEPRECATED,
14532 	 * or otherwise unsuitable for source address selection,
14533 	 * reset the src generation numbers to make sure source
14534 	 * address selection gets to take this new ipif into account.
14535 	 * No need to hold ill_lock while traversing the ipif list since
14536 	 * we are writer
14537 	 */
14538 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14539 	    tmp_ipif = tmp_ipif->ipif_next) {
14540 		if (((tmp_ipif->ipif_flags &
14541 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14542 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14543 		    (tmp_ipif == ipif))
14544 			continue;
14545 		/* first useable pre-existing interface */
14546 		update_src_selection = B_FALSE;
14547 		break;
14548 	}
14549 	if (update_src_selection)
14550 		ip_update_source_selection(ill->ill_ipst);
14551 
14552 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14553 		nce_t *loop_nce = NULL;
14554 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14555 
14556 		/*
14557 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14558 		 * ipif_lookup_on_name(), but in the case of zones we can have
14559 		 * several loopback addresses on lo0. So all the interfaces with
14560 		 * loopback addresses need to be marked IRE_LOOPBACK.
14561 		 */
14562 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14563 		    htonl(INADDR_LOOPBACK))
14564 			ipif->ipif_ire_type = IRE_LOOPBACK;
14565 		else
14566 			ipif->ipif_ire_type = IRE_LOCAL;
14567 		if (ill->ill_net_type != IRE_LOOPBACK)
14568 			flags |= NCE_F_PUBLISH;
14569 
14570 		/* add unicast nce for the local addr */
14571 		err = nce_lookup_then_add_v4(ill, NULL,
14572 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14573 		    ND_REACHABLE, &loop_nce);
14574 		/* A shared-IP zone sees EEXIST for lo0:N */
14575 		if (err == 0 || err == EEXIST) {
14576 			ipif->ipif_added_nce = 1;
14577 			loop_nce->nce_ipif_cnt++;
14578 			nce_refrele(loop_nce);
14579 			err = 0;
14580 		} else {
14581 			ASSERT(loop_nce == NULL);
14582 			return (err);
14583 		}
14584 	}
14585 
14586 	/* Create all the IREs associated with this interface */
14587 	err = ipif_add_ires_v4(ipif, loopback);
14588 	if (err != 0) {
14589 		/*
14590 		 * see comments about return value from
14591 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14592 		 */
14593 		if (err != EADDRINUSE) {
14594 			(void) ipif_arp_down(ipif);
14595 		} else {
14596 			/*
14597 			 * Make IPMP aware of the deleted ipif so that
14598 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14599 			 * can be completed. Note that we do not want to
14600 			 * destroy the nce that was created on the ipmp_ill
14601 			 * for the active copy of the duplicate address in
14602 			 * use.
14603 			 */
14604 			if (IS_IPMP(ill))
14605 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14606 			err = EADDRNOTAVAIL;
14607 		}
14608 		return (err);
14609 	}
14610 
14611 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14612 		/* Recover any additional IREs entries for this ill */
14613 		(void) ill_recover_saved_ire(ill);
14614 	}
14615 
14616 	if (ill->ill_need_recover_multicast) {
14617 		/*
14618 		 * Need to recover all multicast memberships in the driver.
14619 		 * This had to be deferred until we had attached.  The same
14620 		 * code exists in ipif_up_done_v6() to recover IPv6
14621 		 * memberships.
14622 		 *
14623 		 * Note that it would be preferable to unconditionally do the
14624 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14625 		 * that since ill_join_allmulti() depends on ill_dl_up being
14626 		 * set, and it is not set until we receive a DL_BIND_ACK after
14627 		 * having called ill_dl_up().
14628 		 */
14629 		ill_recover_multicast(ill);
14630 	}
14631 
14632 	if (ill->ill_ipif_up_count == 1) {
14633 		/*
14634 		 * Since the interface is now up, it may now be active.
14635 		 */
14636 		if (IS_UNDER_IPMP(ill))
14637 			ipmp_ill_refresh_active(ill);
14638 
14639 		/*
14640 		 * If this is an IPMP interface, we may now be able to
14641 		 * establish ARP entries.
14642 		 */
14643 		if (IS_IPMP(ill))
14644 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14645 	}
14646 
14647 	/* Join the allhosts multicast address */
14648 	ipif_multicast_up(ipif);
14649 
14650 	if (!loopback && !update_src_selection &&
14651 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14652 		ip_update_source_selection(ill->ill_ipst);
14653 
14654 	if (!loopback && ipif->ipif_addr_ready) {
14655 		/* Broadcast an address mask reply. */
14656 		ipif_mask_reply(ipif);
14657 	}
14658 	/* Perhaps ilgs should use this ill */
14659 	update_conn_ill(NULL, ill->ill_ipst);
14660 
14661 	/*
14662 	 * This had to be deferred until we had bound.  Tell routing sockets and
14663 	 * others that this interface is up if it looks like the address has
14664 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14665 	 * duplicate address detection to do its thing.
14666 	 */
14667 	if (ipif->ipif_addr_ready)
14668 		ipif_up_notify(ipif);
14669 	return (0);
14670 }
14671 
14672 /*
14673  * Add the IREs associated with the ipif.
14674  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14675  */
14676 static int
14677 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14678 {
14679 	ill_t		*ill = ipif->ipif_ill;
14680 	ip_stack_t	*ipst = ill->ill_ipst;
14681 	ire_t		*ire_array[20];
14682 	ire_t		**irep = ire_array;
14683 	ire_t		**irep1;
14684 	ipaddr_t	net_mask = 0;
14685 	ipaddr_t	subnet_mask, route_mask;
14686 	int		err;
14687 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14688 	ire_t		*ire_if = NULL;
14689 	uchar_t		*gw;
14690 
14691 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14692 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14693 		/*
14694 		 * If we're on a labeled system then make sure that zone-
14695 		 * private addresses have proper remote host database entries.
14696 		 */
14697 		if (is_system_labeled() &&
14698 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14699 		    !tsol_check_interface_address(ipif))
14700 			return (EINVAL);
14701 
14702 		/* Register the source address for __sin6_src_id */
14703 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14704 		    ipif->ipif_zoneid, ipst);
14705 		if (err != 0) {
14706 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14707 			return (err);
14708 		}
14709 
14710 		if (loopback)
14711 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14712 		else
14713 			gw = NULL;
14714 
14715 		/* If the interface address is set, create the local IRE. */
14716 		ire_local = ire_create(
14717 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14718 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14719 		    gw,					/* gateway */
14720 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14721 		    ipif->ipif_ill,
14722 		    ipif->ipif_zoneid,
14723 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14724 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14725 		    NULL,
14726 		    ipst);
14727 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14728 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14729 		    ipif->ipif_ire_type,
14730 		    ntohl(ipif->ipif_lcl_addr)));
14731 		if (ire_local == NULL) {
14732 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14733 			err = ENOMEM;
14734 			goto bad;
14735 		}
14736 	} else {
14737 		ip1dbg((
14738 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14739 		    ipif->ipif_ire_type,
14740 		    ntohl(ipif->ipif_lcl_addr),
14741 		    (uint_t)ipif->ipif_flags));
14742 	}
14743 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14744 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14745 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14746 	} else {
14747 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14748 	}
14749 
14750 	subnet_mask = ipif->ipif_net_mask;
14751 
14752 	/*
14753 	 * If mask was not specified, use natural netmask of
14754 	 * interface address. Also, store this mask back into the
14755 	 * ipif struct.
14756 	 */
14757 	if (subnet_mask == 0) {
14758 		subnet_mask = net_mask;
14759 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14760 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14761 		    ipif->ipif_v6subnet);
14762 	}
14763 
14764 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14765 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14766 	    ipif->ipif_subnet != INADDR_ANY) {
14767 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14768 
14769 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14770 			route_mask = IP_HOST_MASK;
14771 		} else {
14772 			route_mask = subnet_mask;
14773 		}
14774 
14775 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14776 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14777 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14778 		    ntohl(ipif->ipif_subnet)));
14779 		ire_if = ire_create(
14780 		    (uchar_t *)&ipif->ipif_subnet,
14781 		    (uchar_t *)&route_mask,
14782 		    (uchar_t *)&ipif->ipif_lcl_addr,
14783 		    ill->ill_net_type,
14784 		    ill,
14785 		    ipif->ipif_zoneid,
14786 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14787 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14788 		    NULL,
14789 		    ipst);
14790 		if (ire_if == NULL) {
14791 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14792 			err = ENOMEM;
14793 			goto bad;
14794 		}
14795 	}
14796 
14797 	/*
14798 	 * Create any necessary broadcast IREs.
14799 	 */
14800 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14801 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14802 		irep = ipif_create_bcast_ires(ipif, irep);
14803 
14804 	/* If an earlier ire_create failed, get out now */
14805 	for (irep1 = irep; irep1 > ire_array; ) {
14806 		irep1--;
14807 		if (*irep1 == NULL) {
14808 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14809 			err = ENOMEM;
14810 			goto bad;
14811 		}
14812 	}
14813 
14814 	/*
14815 	 * Need to atomically check for IP address availability under
14816 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14817 	 * ills or new ipifs can be added while we are checking availability.
14818 	 */
14819 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14820 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14821 	/* Mark it up, and increment counters. */
14822 	ipif->ipif_flags |= IPIF_UP;
14823 	ill->ill_ipif_up_count++;
14824 	err = ip_addr_availability_check(ipif);
14825 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14826 	rw_exit(&ipst->ips_ill_g_lock);
14827 
14828 	if (err != 0) {
14829 		/*
14830 		 * Our address may already be up on the same ill. In this case,
14831 		 * the ARP entry for our ipif replaced the one for the other
14832 		 * ipif. So we don't want to delete it (otherwise the other ipif
14833 		 * would be unable to send packets).
14834 		 * ip_addr_availability_check() identifies this case for us and
14835 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14836 		 * which is the expected error code.
14837 		 */
14838 		ill->ill_ipif_up_count--;
14839 		ipif->ipif_flags &= ~IPIF_UP;
14840 		goto bad;
14841 	}
14842 
14843 	/*
14844 	 * Add in all newly created IREs.  ire_create_bcast() has
14845 	 * already checked for duplicates of the IRE_BROADCAST type.
14846 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14847 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14848 	 * a /32 route.
14849 	 */
14850 	if (ire_if != NULL) {
14851 		ire_if = ire_add(ire_if);
14852 		if (ire_if == NULL) {
14853 			err = ENOMEM;
14854 			goto bad2;
14855 		}
14856 #ifdef DEBUG
14857 		ire_refhold_notr(ire_if);
14858 		ire_refrele(ire_if);
14859 #endif
14860 	}
14861 	if (ire_local != NULL) {
14862 		ire_local = ire_add(ire_local);
14863 		if (ire_local == NULL) {
14864 			err = ENOMEM;
14865 			goto bad2;
14866 		}
14867 #ifdef DEBUG
14868 		ire_refhold_notr(ire_local);
14869 		ire_refrele(ire_local);
14870 #endif
14871 	}
14872 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14873 	if (ire_local != NULL)
14874 		ipif->ipif_ire_local = ire_local;
14875 	if (ire_if != NULL)
14876 		ipif->ipif_ire_if = ire_if;
14877 	rw_exit(&ipst->ips_ill_g_lock);
14878 	ire_local = NULL;
14879 	ire_if = NULL;
14880 
14881 	/*
14882 	 * We first add all of them, and if that succeeds we refrele the
14883 	 * bunch. That enables us to delete all of them should any of the
14884 	 * ire_adds fail.
14885 	 */
14886 	for (irep1 = irep; irep1 > ire_array; ) {
14887 		irep1--;
14888 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14889 		*irep1 = ire_add(*irep1);
14890 		if (*irep1 == NULL) {
14891 			err = ENOMEM;
14892 			goto bad2;
14893 		}
14894 	}
14895 
14896 	for (irep1 = irep; irep1 > ire_array; ) {
14897 		irep1--;
14898 		/* refheld by ire_add. */
14899 		if (*irep1 != NULL) {
14900 			ire_refrele(*irep1);
14901 			*irep1 = NULL;
14902 		}
14903 	}
14904 
14905 	if (!loopback) {
14906 		/*
14907 		 * If the broadcast address has been set, make sure it makes
14908 		 * sense based on the interface address.
14909 		 * Only match on ill since we are sharing broadcast addresses.
14910 		 */
14911 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14912 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14913 			ire_t	*ire;
14914 
14915 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14916 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14917 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14918 
14919 			if (ire == NULL) {
14920 				/*
14921 				 * If there isn't a matching broadcast IRE,
14922 				 * revert to the default for this netmask.
14923 				 */
14924 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14925 				mutex_enter(&ipif->ipif_ill->ill_lock);
14926 				ipif_set_default(ipif);
14927 				mutex_exit(&ipif->ipif_ill->ill_lock);
14928 			} else {
14929 				ire_refrele(ire);
14930 			}
14931 		}
14932 
14933 	}
14934 	return (0);
14935 
14936 bad2:
14937 	ill->ill_ipif_up_count--;
14938 	ipif->ipif_flags &= ~IPIF_UP;
14939 
14940 bad:
14941 	ip1dbg(("ipif_add_ires: FAILED \n"));
14942 	if (ire_local != NULL)
14943 		ire_delete(ire_local);
14944 	if (ire_if != NULL)
14945 		ire_delete(ire_if);
14946 
14947 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14948 	ire_local = ipif->ipif_ire_local;
14949 	ipif->ipif_ire_local = NULL;
14950 	ire_if = ipif->ipif_ire_if;
14951 	ipif->ipif_ire_if = NULL;
14952 	rw_exit(&ipst->ips_ill_g_lock);
14953 	if (ire_local != NULL) {
14954 		ire_delete(ire_local);
14955 		ire_refrele_notr(ire_local);
14956 	}
14957 	if (ire_if != NULL) {
14958 		ire_delete(ire_if);
14959 		ire_refrele_notr(ire_if);
14960 	}
14961 
14962 	while (irep > ire_array) {
14963 		irep--;
14964 		if (*irep != NULL) {
14965 			ire_delete(*irep);
14966 		}
14967 	}
14968 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14969 
14970 	return (err);
14971 }
14972 
14973 /* Remove all the IREs created by ipif_add_ires_v4 */
14974 void
14975 ipif_delete_ires_v4(ipif_t *ipif)
14976 {
14977 	ill_t		*ill = ipif->ipif_ill;
14978 	ip_stack_t	*ipst = ill->ill_ipst;
14979 	ire_t		*ire;
14980 
14981 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14982 	ire = ipif->ipif_ire_local;
14983 	ipif->ipif_ire_local = NULL;
14984 	rw_exit(&ipst->ips_ill_g_lock);
14985 	if (ire != NULL) {
14986 		/*
14987 		 * Move count to ipif so we don't loose the count due to
14988 		 * a down/up dance.
14989 		 */
14990 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14991 
14992 		ire_delete(ire);
14993 		ire_refrele_notr(ire);
14994 	}
14995 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14996 	ire = ipif->ipif_ire_if;
14997 	ipif->ipif_ire_if = NULL;
14998 	rw_exit(&ipst->ips_ill_g_lock);
14999 	if (ire != NULL) {
15000 		ire_delete(ire);
15001 		ire_refrele_notr(ire);
15002 	}
15003 
15004 	/*
15005 	 * Delete the broadcast IREs.
15006 	 */
15007 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15008 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15009 		ipif_delete_bcast_ires(ipif);
15010 }
15011 
15012 /*
15013  * Checks for availbility of a usable source address (if there is one) when the
15014  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15015  * this selection is done regardless of the destination.
15016  */
15017 boolean_t
15018 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15019     ip_stack_t *ipst)
15020 {
15021 	ipif_t		*ipif = NULL;
15022 	ill_t		*uill;
15023 
15024 	ASSERT(ifindex != 0);
15025 
15026 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15027 	if (uill == NULL)
15028 		return (B_FALSE);
15029 
15030 	mutex_enter(&uill->ill_lock);
15031 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15032 		if (IPIF_IS_CONDEMNED(ipif))
15033 			continue;
15034 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15035 			continue;
15036 		if (!(ipif->ipif_flags & IPIF_UP))
15037 			continue;
15038 		if (ipif->ipif_zoneid != zoneid)
15039 			continue;
15040 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15041 		    ipif->ipif_lcl_addr == INADDR_ANY)
15042 			continue;
15043 		mutex_exit(&uill->ill_lock);
15044 		ill_refrele(uill);
15045 		return (B_TRUE);
15046 	}
15047 	mutex_exit(&uill->ill_lock);
15048 	ill_refrele(uill);
15049 	return (B_FALSE);
15050 }
15051 
15052 /*
15053  * Find an ipif with a good local address on the ill+zoneid.
15054  */
15055 ipif_t *
15056 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15057 {
15058 	ipif_t		*ipif;
15059 
15060 	mutex_enter(&ill->ill_lock);
15061 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15062 		if (IPIF_IS_CONDEMNED(ipif))
15063 			continue;
15064 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15065 			continue;
15066 		if (!(ipif->ipif_flags & IPIF_UP))
15067 			continue;
15068 		if (ipif->ipif_zoneid != zoneid &&
15069 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15070 			continue;
15071 		if (ill->ill_isv6 ?
15072 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15073 		    ipif->ipif_lcl_addr == INADDR_ANY)
15074 			continue;
15075 		ipif_refhold_locked(ipif);
15076 		mutex_exit(&ill->ill_lock);
15077 		return (ipif);
15078 	}
15079 	mutex_exit(&ill->ill_lock);
15080 	return (NULL);
15081 }
15082 
15083 /*
15084  * IP source address type, sorted from worst to best.  For a given type,
15085  * always prefer IP addresses on the same subnet.  All-zones addresses are
15086  * suboptimal because they pose problems with unlabeled destinations.
15087  */
15088 typedef enum {
15089 	IPIF_NONE,
15090 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
15091 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
15092 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15093 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15094 	IPIF_DIFFNET,			/* normal and different subnet */
15095 	IPIF_SAMENET,			/* normal and same subnet */
15096 	IPIF_LOCALADDR			/* local loopback */
15097 } ipif_type_t;
15098 
15099 /*
15100  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15101  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15102  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15103  * the first one, unless IPMP is used in which case we round-robin among them;
15104  * see below for more.
15105  *
15106  * Returns NULL if there is no suitable source address for the ill.
15107  * This only occurs when there is no valid source address for the ill.
15108  */
15109 ipif_t *
15110 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15111     boolean_t allow_usesrc, boolean_t *notreadyp)
15112 {
15113 	ill_t	*usill = NULL;
15114 	ill_t	*ipmp_ill = NULL;
15115 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15116 	ipif_type_t type, best_type;
15117 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15118 	ip_stack_t *ipst = ill->ill_ipst;
15119 	boolean_t samenet;
15120 
15121 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15122 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15123 		    B_FALSE, ipst);
15124 		if (usill != NULL)
15125 			ill = usill;	/* Select source from usesrc ILL */
15126 		else
15127 			return (NULL);
15128 	}
15129 
15130 	/*
15131 	 * Test addresses should never be used for source address selection,
15132 	 * so if we were passed one, switch to the IPMP meta-interface.
15133 	 */
15134 	if (IS_UNDER_IPMP(ill)) {
15135 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15136 			ill = ipmp_ill;	/* Select source from IPMP ill */
15137 		else
15138 			return (NULL);
15139 	}
15140 
15141 	/*
15142 	 * If we're dealing with an unlabeled destination on a labeled system,
15143 	 * make sure that we ignore source addresses that are incompatible with
15144 	 * the destination's default label.  That destination's default label
15145 	 * must dominate the minimum label on the source address.
15146 	 */
15147 	dst_rhtp = NULL;
15148 	if (is_system_labeled()) {
15149 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15150 		if (dst_rhtp == NULL)
15151 			return (NULL);
15152 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15153 			TPC_RELE(dst_rhtp);
15154 			dst_rhtp = NULL;
15155 		}
15156 	}
15157 
15158 	/*
15159 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15160 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15161 	 * After selecting the right ipif, under ill_lock make sure ipif is
15162 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15163 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15164 	 * but not under a lock.
15165 	 */
15166 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15167 retry:
15168 	/*
15169 	 * For source address selection, we treat the ipif list as circular
15170 	 * and continue until we get back to where we started.  This allows
15171 	 * IPMP to vary source address selection (which improves inbound load
15172 	 * spreading) by caching its last ending point and starting from
15173 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15174 	 * ills since that can't happen on the IPMP ill.
15175 	 */
15176 	start_ipif = ill->ill_ipif;
15177 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15178 		start_ipif = ill->ill_src_ipif;
15179 
15180 	ipif = start_ipif;
15181 	best_ipif = NULL;
15182 	best_type = IPIF_NONE;
15183 	do {
15184 		if ((next_ipif = ipif->ipif_next) == NULL)
15185 			next_ipif = ill->ill_ipif;
15186 
15187 		if (IPIF_IS_CONDEMNED(ipif))
15188 			continue;
15189 		/* Always skip NOLOCAL and ANYCAST interfaces */
15190 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15191 			continue;
15192 		/* Always skip NOACCEPT interfaces */
15193 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15194 			continue;
15195 		if (!(ipif->ipif_flags & IPIF_UP))
15196 			continue;
15197 
15198 		if (!ipif->ipif_addr_ready) {
15199 			if (notreadyp != NULL)
15200 				*notreadyp = B_TRUE;
15201 			continue;
15202 		}
15203 
15204 		if (zoneid != ALL_ZONES &&
15205 		    ipif->ipif_zoneid != zoneid &&
15206 		    ipif->ipif_zoneid != ALL_ZONES)
15207 			continue;
15208 
15209 		/*
15210 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15211 		 * are not valid as source addresses.
15212 		 */
15213 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15214 			continue;
15215 
15216 		/*
15217 		 * Check compatibility of local address for destination's
15218 		 * default label if we're on a labeled system.	Incompatible
15219 		 * addresses can't be used at all.
15220 		 */
15221 		if (dst_rhtp != NULL) {
15222 			boolean_t incompat;
15223 
15224 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15225 			    IPV4_VERSION, B_FALSE);
15226 			if (src_rhtp == NULL)
15227 				continue;
15228 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15229 			    src_rhtp->tpc_tp.tp_doi !=
15230 			    dst_rhtp->tpc_tp.tp_doi ||
15231 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15232 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15233 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15234 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15235 			TPC_RELE(src_rhtp);
15236 			if (incompat)
15237 				continue;
15238 		}
15239 
15240 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15241 
15242 		if (ipif->ipif_lcl_addr == dst) {
15243 			type = IPIF_LOCALADDR;
15244 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15245 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15246 			    IPIF_DIFFNET_DEPRECATED;
15247 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15248 			type = samenet ? IPIF_SAMENET_ALLZONES :
15249 			    IPIF_DIFFNET_ALLZONES;
15250 		} else {
15251 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15252 		}
15253 
15254 		if (type > best_type) {
15255 			best_type = type;
15256 			best_ipif = ipif;
15257 			if (best_type == IPIF_LOCALADDR)
15258 				break; /* can't get better */
15259 		}
15260 	} while ((ipif = next_ipif) != start_ipif);
15261 
15262 	if ((ipif = best_ipif) != NULL) {
15263 		mutex_enter(&ipif->ipif_ill->ill_lock);
15264 		if (IPIF_IS_CONDEMNED(ipif)) {
15265 			mutex_exit(&ipif->ipif_ill->ill_lock);
15266 			goto retry;
15267 		}
15268 		ipif_refhold_locked(ipif);
15269 
15270 		/*
15271 		 * For IPMP, update the source ipif rotor to the next ipif,
15272 		 * provided we can look it up.  (We must not use it if it's
15273 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15274 		 * ipif_free() checked ill_src_ipif.)
15275 		 */
15276 		if (IS_IPMP(ill) && ipif != NULL) {
15277 			next_ipif = ipif->ipif_next;
15278 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15279 				ill->ill_src_ipif = next_ipif;
15280 			else
15281 				ill->ill_src_ipif = NULL;
15282 		}
15283 		mutex_exit(&ipif->ipif_ill->ill_lock);
15284 	}
15285 
15286 	rw_exit(&ipst->ips_ill_g_lock);
15287 	if (usill != NULL)
15288 		ill_refrele(usill);
15289 	if (ipmp_ill != NULL)
15290 		ill_refrele(ipmp_ill);
15291 	if (dst_rhtp != NULL)
15292 		TPC_RELE(dst_rhtp);
15293 
15294 #ifdef DEBUG
15295 	if (ipif == NULL) {
15296 		char buf1[INET6_ADDRSTRLEN];
15297 
15298 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15299 		    ill->ill_name,
15300 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15301 	} else {
15302 		char buf1[INET6_ADDRSTRLEN];
15303 		char buf2[INET6_ADDRSTRLEN];
15304 
15305 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15306 		    ipif->ipif_ill->ill_name,
15307 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15308 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15309 		    buf2, sizeof (buf2))));
15310 	}
15311 #endif /* DEBUG */
15312 	return (ipif);
15313 }
15314 
15315 /*
15316  * Pick a source address based on the destination ill and an optional setsrc
15317  * address.
15318  * The result is stored in srcp. If generation is set, then put the source
15319  * generation number there before we look for the source address (to avoid
15320  * missing changes in the set of source addresses.
15321  * If flagsp is set, then us it to pass back ipif_flags.
15322  *
15323  * If the caller wants to cache the returned source address and detect when
15324  * that might be stale, the caller should pass in a generation argument,
15325  * which the caller can later compare against ips_src_generation
15326  *
15327  * The precedence order for selecting an IPv4 source address is:
15328  *  - RTF_SETSRC on the offlink ire always wins.
15329  *  - If usrsrc is set, swap the ill to be the usesrc one.
15330  *  - If IPMP is used on the ill, select a random address from the most
15331  *    preferred ones below:
15332  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15333  * 2. Not deprecated, not ALL_ZONES
15334  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15335  * 4. Not deprecated, ALL_ZONES
15336  * 5. If onlink destination, same subnet and deprecated
15337  * 6. Deprecated.
15338  *
15339  * We have lower preference for ALL_ZONES IP addresses,
15340  * as they pose problems with unlabeled destinations.
15341  *
15342  * Note that when multiple IP addresses match e.g., #1 we pick
15343  * the first one if IPMP is not in use. With IPMP we randomize.
15344  */
15345 int
15346 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15347     ipaddr_t multicast_ifaddr,
15348     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15349     uint32_t *generation, uint64_t *flagsp)
15350 {
15351 	ipif_t *ipif;
15352 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15353 
15354 	if (flagsp != NULL)
15355 		*flagsp = 0;
15356 
15357 	/*
15358 	 * Need to grab the generation number before we check to
15359 	 * avoid a race with a change to the set of local addresses.
15360 	 * No lock needed since the thread which updates the set of local
15361 	 * addresses use ipif/ill locks and exit those (hence a store memory
15362 	 * barrier) before doing the atomic increase of ips_src_generation.
15363 	 */
15364 	if (generation != NULL) {
15365 		*generation = ipst->ips_src_generation;
15366 	}
15367 
15368 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15369 		*srcp = multicast_ifaddr;
15370 		return (0);
15371 	}
15372 
15373 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15374 	if (setsrc != INADDR_ANY) {
15375 		*srcp = setsrc;
15376 		return (0);
15377 	}
15378 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15379 	if (ipif == NULL) {
15380 		if (notready)
15381 			return (ENETDOWN);
15382 		else
15383 			return (EADDRNOTAVAIL);
15384 	}
15385 	*srcp = ipif->ipif_lcl_addr;
15386 	if (flagsp != NULL)
15387 		*flagsp = ipif->ipif_flags;
15388 	ipif_refrele(ipif);
15389 	return (0);
15390 }
15391 
15392 /* ARGSUSED */
15393 int
15394 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15395 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15396 {
15397 	/*
15398 	 * ill_phyint_reinit merged the v4 and v6 into a single
15399 	 * ipsq.  We might not have been able to complete the
15400 	 * operation in ipif_set_values, if we could not become
15401 	 * exclusive.  If so restart it here.
15402 	 */
15403 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15404 }
15405 
15406 /*
15407  * Can operate on either a module or a driver queue.
15408  * Returns an error if not a module queue.
15409  */
15410 /* ARGSUSED */
15411 int
15412 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15413     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15414 {
15415 	queue_t		*q1 = q;
15416 	char 		*cp;
15417 	char		interf_name[LIFNAMSIZ];
15418 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15419 
15420 	if (q->q_next == NULL) {
15421 		ip1dbg((
15422 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15423 		return (EINVAL);
15424 	}
15425 
15426 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15427 		return (EALREADY);
15428 
15429 	do {
15430 		q1 = q1->q_next;
15431 	} while (q1->q_next);
15432 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15433 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15434 
15435 	/*
15436 	 * Here we are not going to delay the ioack until after
15437 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15438 	 * original ioctl message before sending the requests.
15439 	 */
15440 	return (ipif_set_values(q, mp, interf_name, &ppa));
15441 }
15442 
15443 /* ARGSUSED */
15444 int
15445 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15446     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15447 {
15448 	return (ENXIO);
15449 }
15450 
15451 /*
15452  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15453  * `irep'.  Returns a pointer to the next free `irep' entry
15454  * A mirror exists in ipif_delete_bcast_ires().
15455  *
15456  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15457  * done in ire_add.
15458  */
15459 static ire_t **
15460 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15461 {
15462 	ipaddr_t addr;
15463 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15464 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15465 	ill_t *ill = ipif->ipif_ill;
15466 	zoneid_t zoneid = ipif->ipif_zoneid;
15467 
15468 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15469 
15470 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15471 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15472 
15473 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15474 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15475 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15476 
15477 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15478 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15479 
15480 	/*
15481 	 * For backward compatibility, we create net broadcast IREs based on
15482 	 * the old "IP address class system", since some old machines only
15483 	 * respond to these class derived net broadcast.  However, we must not
15484 	 * create these net broadcast IREs if the subnetmask is shorter than
15485 	 * the IP address class based derived netmask.  Otherwise, we may
15486 	 * create a net broadcast address which is the same as an IP address
15487 	 * on the subnet -- and then TCP will refuse to talk to that address.
15488 	 */
15489 	if (netmask < subnetmask) {
15490 		addr = netmask & ipif->ipif_subnet;
15491 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15492 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15493 	}
15494 
15495 	/*
15496 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15497 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15498 	 * created.  Creating these broadcast IREs will only create confusion
15499 	 * as `addr' will be the same as the IP address.
15500 	 */
15501 	if (subnetmask != 0xFFFFFFFF) {
15502 		addr = ipif->ipif_subnet;
15503 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15504 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15505 	}
15506 
15507 	return (irep);
15508 }
15509 
15510 /*
15511  * Mirror of ipif_create_bcast_ires()
15512  */
15513 static void
15514 ipif_delete_bcast_ires(ipif_t *ipif)
15515 {
15516 	ipaddr_t	addr;
15517 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15518 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15519 	ill_t		*ill = ipif->ipif_ill;
15520 	zoneid_t	zoneid = ipif->ipif_zoneid;
15521 	ire_t		*ire;
15522 
15523 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15524 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15525 
15526 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15527 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15528 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15529 
15530 	ire = ire_lookup_bcast(ill, 0, zoneid);
15531 	ASSERT(ire != NULL);
15532 	ire_delete(ire); ire_refrele(ire);
15533 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15534 	ASSERT(ire != NULL);
15535 	ire_delete(ire); ire_refrele(ire);
15536 
15537 	/*
15538 	 * For backward compatibility, we create net broadcast IREs based on
15539 	 * the old "IP address class system", since some old machines only
15540 	 * respond to these class derived net broadcast.  However, we must not
15541 	 * create these net broadcast IREs if the subnetmask is shorter than
15542 	 * the IP address class based derived netmask.  Otherwise, we may
15543 	 * create a net broadcast address which is the same as an IP address
15544 	 * on the subnet -- and then TCP will refuse to talk to that address.
15545 	 */
15546 	if (netmask < subnetmask) {
15547 		addr = netmask & ipif->ipif_subnet;
15548 		ire = ire_lookup_bcast(ill, addr, zoneid);
15549 		ASSERT(ire != NULL);
15550 		ire_delete(ire); ire_refrele(ire);
15551 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15552 		ASSERT(ire != NULL);
15553 		ire_delete(ire); ire_refrele(ire);
15554 	}
15555 
15556 	/*
15557 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15558 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15559 	 * created.  Creating these broadcast IREs will only create confusion
15560 	 * as `addr' will be the same as the IP address.
15561 	 */
15562 	if (subnetmask != 0xFFFFFFFF) {
15563 		addr = ipif->ipif_subnet;
15564 		ire = ire_lookup_bcast(ill, addr, zoneid);
15565 		ASSERT(ire != NULL);
15566 		ire_delete(ire); ire_refrele(ire);
15567 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15568 		ASSERT(ire != NULL);
15569 		ire_delete(ire); ire_refrele(ire);
15570 	}
15571 }
15572 
15573 /*
15574  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15575  * from lifr_flags and the name from lifr_name.
15576  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15577  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15578  * Returns EINPROGRESS when mp has been consumed by queueing it on
15579  * ipx_pending_mp and the ioctl will complete in ip_rput.
15580  *
15581  * Can operate on either a module or a driver queue.
15582  * Returns an error if not a module queue.
15583  */
15584 /* ARGSUSED */
15585 int
15586 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15587     ip_ioctl_cmd_t *ipip, void *if_req)
15588 {
15589 	ill_t	*ill = q->q_ptr;
15590 	phyint_t *phyi;
15591 	ip_stack_t *ipst;
15592 	struct lifreq *lifr = if_req;
15593 	uint64_t new_flags;
15594 
15595 	ASSERT(ipif != NULL);
15596 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15597 
15598 	if (q->q_next == NULL) {
15599 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15600 		return (EINVAL);
15601 	}
15602 
15603 	/*
15604 	 * If we are not writer on 'q' then this interface exists already
15605 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15606 	 * so return EALREADY.
15607 	 */
15608 	if (ill != ipif->ipif_ill)
15609 		return (EALREADY);
15610 
15611 	if (ill->ill_name[0] != '\0')
15612 		return (EALREADY);
15613 
15614 	/*
15615 	 * If there's another ill already with the requested name, ensure
15616 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15617 	 * fuse together two unrelated ills, which will cause chaos.
15618 	 */
15619 	ipst = ill->ill_ipst;
15620 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15621 	    lifr->lifr_name, NULL);
15622 	if (phyi != NULL) {
15623 		ill_t *ill_mate = phyi->phyint_illv4;
15624 
15625 		if (ill_mate == NULL)
15626 			ill_mate = phyi->phyint_illv6;
15627 		ASSERT(ill_mate != NULL);
15628 
15629 		if (ill_mate->ill_media->ip_m_mac_type !=
15630 		    ill->ill_media->ip_m_mac_type) {
15631 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15632 			    "use the same ill name on differing media\n"));
15633 			return (EINVAL);
15634 		}
15635 	}
15636 
15637 	/*
15638 	 * We start off as IFF_IPV4 in ipif_allocate and become
15639 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15640 	 * The only flags that we read from user space are IFF_IPV4,
15641 	 * IFF_IPV6, and IFF_BROADCAST.
15642 	 *
15643 	 * This ill has not been inserted into the global list.
15644 	 * So we are still single threaded and don't need any lock
15645 	 *
15646 	 * Saniy check the flags.
15647 	 */
15648 
15649 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15650 	    ((lifr->lifr_flags & IFF_IPV6) ||
15651 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15652 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15653 		    "or IPv6 i.e., no broadcast \n"));
15654 		return (EINVAL);
15655 	}
15656 
15657 	new_flags =
15658 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15659 
15660 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15661 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15662 		    "IFF_IPV4 or IFF_IPV6\n"));
15663 		return (EINVAL);
15664 	}
15665 
15666 	/*
15667 	 * We always start off as IPv4, so only need to check for IPv6.
15668 	 */
15669 	if ((new_flags & IFF_IPV6) != 0) {
15670 		ill->ill_flags |= ILLF_IPV6;
15671 		ill->ill_flags &= ~ILLF_IPV4;
15672 
15673 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15674 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15675 	}
15676 
15677 	if ((new_flags & IFF_BROADCAST) != 0)
15678 		ipif->ipif_flags |= IPIF_BROADCAST;
15679 	else
15680 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15681 
15682 	/* We started off as V4. */
15683 	if (ill->ill_flags & ILLF_IPV6) {
15684 		ill->ill_phyint->phyint_illv6 = ill;
15685 		ill->ill_phyint->phyint_illv4 = NULL;
15686 	}
15687 
15688 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15689 }
15690 
15691 /* ARGSUSED */
15692 int
15693 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15694     ip_ioctl_cmd_t *ipip, void *if_req)
15695 {
15696 	/*
15697 	 * ill_phyint_reinit merged the v4 and v6 into a single
15698 	 * ipsq.  We might not have been able to complete the
15699 	 * slifname in ipif_set_values, if we could not become
15700 	 * exclusive.  If so restart it here
15701 	 */
15702 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15703 }
15704 
15705 /*
15706  * Return a pointer to the ipif which matches the index, IP version type and
15707  * zoneid.
15708  */
15709 ipif_t *
15710 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15711     ip_stack_t *ipst)
15712 {
15713 	ill_t	*ill;
15714 	ipif_t	*ipif = NULL;
15715 
15716 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15717 	if (ill != NULL) {
15718 		mutex_enter(&ill->ill_lock);
15719 		for (ipif = ill->ill_ipif; ipif != NULL;
15720 		    ipif = ipif->ipif_next) {
15721 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15722 			    zoneid == ipif->ipif_zoneid ||
15723 			    ipif->ipif_zoneid == ALL_ZONES)) {
15724 				ipif_refhold_locked(ipif);
15725 				break;
15726 			}
15727 		}
15728 		mutex_exit(&ill->ill_lock);
15729 		ill_refrele(ill);
15730 	}
15731 	return (ipif);
15732 }
15733 
15734 /*
15735  * Change an existing physical interface's index. If the new index
15736  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15737  * Finally, we update other systems which may have a dependence on the
15738  * index value.
15739  */
15740 /* ARGSUSED */
15741 int
15742 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15743     ip_ioctl_cmd_t *ipip, void *ifreq)
15744 {
15745 	ill_t		*ill;
15746 	phyint_t	*phyi;
15747 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15748 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15749 	uint_t	old_index, index;
15750 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15751 	avl_index_t	where;
15752 
15753 	if (ipip->ipi_cmd_type == IF_CMD)
15754 		index = ifr->ifr_index;
15755 	else
15756 		index = lifr->lifr_index;
15757 
15758 	/*
15759 	 * Only allow on physical interface. Also, index zero is illegal.
15760 	 */
15761 	ill = ipif->ipif_ill;
15762 	phyi = ill->ill_phyint;
15763 	if (ipif->ipif_id != 0 || index == 0) {
15764 		return (EINVAL);
15765 	}
15766 
15767 	/* If the index is not changing, no work to do */
15768 	if (phyi->phyint_ifindex == index)
15769 		return (0);
15770 
15771 	/*
15772 	 * Use phyint_exists() to determine if the new interface index
15773 	 * is already in use. If the index is unused then we need to
15774 	 * change the phyint's position in the phyint_list_avl_by_index
15775 	 * tree. If we do not do this, subsequent lookups (using the new
15776 	 * index value) will not find the phyint.
15777 	 */
15778 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15779 	if (phyint_exists(index, ipst)) {
15780 		rw_exit(&ipst->ips_ill_g_lock);
15781 		return (EEXIST);
15782 	}
15783 
15784 	/*
15785 	 * The new index is unused. Set it in the phyint. However we must not
15786 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15787 	 * changes. The event must be bound to old ifindex value.
15788 	 */
15789 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15790 	    &index, sizeof (index));
15791 
15792 	old_index = phyi->phyint_ifindex;
15793 	phyi->phyint_ifindex = index;
15794 
15795 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15796 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15797 	    &index, &where);
15798 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15799 	    phyi, where);
15800 	rw_exit(&ipst->ips_ill_g_lock);
15801 
15802 	/* Update SCTP's ILL list */
15803 	sctp_ill_reindex(ill, old_index);
15804 
15805 	/* Send the routing sockets message */
15806 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15807 	if (ILL_OTHER(ill))
15808 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15809 
15810 	/* Perhaps ilgs should use this ill */
15811 	update_conn_ill(NULL, ill->ill_ipst);
15812 	return (0);
15813 }
15814 
15815 /* ARGSUSED */
15816 int
15817 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15818     ip_ioctl_cmd_t *ipip, void *ifreq)
15819 {
15820 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15821 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15822 
15823 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15824 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15825 	/* Get the interface index */
15826 	if (ipip->ipi_cmd_type == IF_CMD) {
15827 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15828 	} else {
15829 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15830 	}
15831 	return (0);
15832 }
15833 
15834 /* ARGSUSED */
15835 int
15836 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15837     ip_ioctl_cmd_t *ipip, void *ifreq)
15838 {
15839 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15840 
15841 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15842 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15843 	/* Get the interface zone */
15844 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15845 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15846 	return (0);
15847 }
15848 
15849 /*
15850  * Set the zoneid of an interface.
15851  */
15852 /* ARGSUSED */
15853 int
15854 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15855     ip_ioctl_cmd_t *ipip, void *ifreq)
15856 {
15857 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15858 	int err = 0;
15859 	boolean_t need_up = B_FALSE;
15860 	zone_t *zptr;
15861 	zone_status_t status;
15862 	zoneid_t zoneid;
15863 
15864 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15865 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15866 		if (!is_system_labeled())
15867 			return (ENOTSUP);
15868 		zoneid = GLOBAL_ZONEID;
15869 	}
15870 
15871 	/* cannot assign instance zero to a non-global zone */
15872 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15873 		return (ENOTSUP);
15874 
15875 	/*
15876 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15877 	 * the event of a race with the zone shutdown processing, since IP
15878 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15879 	 * interface will be cleaned up even if the zone is shut down
15880 	 * immediately after the status check. If the interface can't be brought
15881 	 * down right away, and the zone is shut down before the restart
15882 	 * function is called, we resolve the possible races by rechecking the
15883 	 * zone status in the restart function.
15884 	 */
15885 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15886 		return (EINVAL);
15887 	status = zone_status_get(zptr);
15888 	zone_rele(zptr);
15889 
15890 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15891 		return (EINVAL);
15892 
15893 	if (ipif->ipif_flags & IPIF_UP) {
15894 		/*
15895 		 * If the interface is already marked up,
15896 		 * we call ipif_down which will take care
15897 		 * of ditching any IREs that have been set
15898 		 * up based on the old interface address.
15899 		 */
15900 		err = ipif_logical_down(ipif, q, mp);
15901 		if (err == EINPROGRESS)
15902 			return (err);
15903 		(void) ipif_down_tail(ipif);
15904 		need_up = B_TRUE;
15905 	}
15906 
15907 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15908 	return (err);
15909 }
15910 
15911 static int
15912 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15913     queue_t *q, mblk_t *mp, boolean_t need_up)
15914 {
15915 	int	err = 0;
15916 	ip_stack_t	*ipst;
15917 
15918 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15919 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15920 
15921 	if (CONN_Q(q))
15922 		ipst = CONNQ_TO_IPST(q);
15923 	else
15924 		ipst = ILLQ_TO_IPST(q);
15925 
15926 	/*
15927 	 * For exclusive stacks we don't allow a different zoneid than
15928 	 * global.
15929 	 */
15930 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15931 	    zoneid != GLOBAL_ZONEID)
15932 		return (EINVAL);
15933 
15934 	/* Set the new zone id. */
15935 	ipif->ipif_zoneid = zoneid;
15936 
15937 	/* Update sctp list */
15938 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15939 
15940 	/* The default multicast interface might have changed */
15941 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15942 
15943 	if (need_up) {
15944 		/*
15945 		 * Now bring the interface back up.  If this
15946 		 * is the only IPIF for the ILL, ipif_up
15947 		 * will have to re-bind to the device, so
15948 		 * we may get back EINPROGRESS, in which
15949 		 * case, this IOCTL will get completed in
15950 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15951 		 */
15952 		err = ipif_up(ipif, q, mp);
15953 	}
15954 	return (err);
15955 }
15956 
15957 /* ARGSUSED */
15958 int
15959 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15960     ip_ioctl_cmd_t *ipip, void *if_req)
15961 {
15962 	struct lifreq *lifr = (struct lifreq *)if_req;
15963 	zoneid_t zoneid;
15964 	zone_t *zptr;
15965 	zone_status_t status;
15966 
15967 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15968 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15969 		zoneid = GLOBAL_ZONEID;
15970 
15971 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15973 
15974 	/*
15975 	 * We recheck the zone status to resolve the following race condition:
15976 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15977 	 * 2) hme0:1 is up and can't be brought down right away;
15978 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15979 	 * 3) zone "myzone" is halted; the zone status switches to
15980 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15981 	 * the interfaces to remove - hme0:1 is not returned because it's not
15982 	 * yet in "myzone", so it won't be removed;
15983 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15984 	 * status check here, we would have hme0:1 in "myzone" after it's been
15985 	 * destroyed.
15986 	 * Note that if the status check fails, we need to bring the interface
15987 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15988 	 * ipif_up_done[_v6]().
15989 	 */
15990 	status = ZONE_IS_UNINITIALIZED;
15991 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15992 		status = zone_status_get(zptr);
15993 		zone_rele(zptr);
15994 	}
15995 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15996 		if (ipif->ipif_isv6) {
15997 			(void) ipif_up_done_v6(ipif);
15998 		} else {
15999 			(void) ipif_up_done(ipif);
16000 		}
16001 		return (EINVAL);
16002 	}
16003 
16004 	(void) ipif_down_tail(ipif);
16005 
16006 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16007 	    B_TRUE));
16008 }
16009 
16010 /*
16011  * Return the number of addresses on `ill' with one or more of the values
16012  * in `set' set and all of the values in `clear' clear.
16013  */
16014 static uint_t
16015 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16016 {
16017 	ipif_t	*ipif;
16018 	uint_t	cnt = 0;
16019 
16020 	ASSERT(IAM_WRITER_ILL(ill));
16021 
16022 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16023 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16024 			cnt++;
16025 
16026 	return (cnt);
16027 }
16028 
16029 /*
16030  * Return the number of migratable addresses on `ill' that are under
16031  * application control.
16032  */
16033 uint_t
16034 ill_appaddr_cnt(const ill_t *ill)
16035 {
16036 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16037 	    IPIF_NOFAILOVER));
16038 }
16039 
16040 /*
16041  * Return the number of point-to-point addresses on `ill'.
16042  */
16043 uint_t
16044 ill_ptpaddr_cnt(const ill_t *ill)
16045 {
16046 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16047 }
16048 
16049 /* ARGSUSED */
16050 int
16051 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16052 	ip_ioctl_cmd_t *ipip, void *ifreq)
16053 {
16054 	struct lifreq	*lifr = ifreq;
16055 
16056 	ASSERT(q->q_next == NULL);
16057 	ASSERT(CONN_Q(q));
16058 
16059 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16061 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16062 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16063 
16064 	return (0);
16065 }
16066 
16067 /* Find the previous ILL in this usesrc group */
16068 static ill_t *
16069 ill_prev_usesrc(ill_t *uill)
16070 {
16071 	ill_t *ill;
16072 
16073 	for (ill = uill->ill_usesrc_grp_next;
16074 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16075 	    ill = ill->ill_usesrc_grp_next)
16076 		/* do nothing */;
16077 	return (ill);
16078 }
16079 
16080 /*
16081  * Release all members of the usesrc group. This routine is called
16082  * from ill_delete when the interface being unplumbed is the
16083  * group head.
16084  *
16085  * This silently clears the usesrc that ifconfig setup.
16086  * An alternative would be to keep that ifindex, and drop packets on the floor
16087  * since no source address can be selected.
16088  * Even if we keep the current semantics, don't need a lock and a linked list.
16089  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16090  * the one that is being removed. Issue is how we return the usesrc users
16091  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16092  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16093  * ill walk, but the walker would need to insert in the ioctl response.
16094  */
16095 static void
16096 ill_disband_usesrc_group(ill_t *uill)
16097 {
16098 	ill_t *next_ill, *tmp_ill;
16099 	ip_stack_t	*ipst = uill->ill_ipst;
16100 
16101 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16102 	next_ill = uill->ill_usesrc_grp_next;
16103 
16104 	do {
16105 		ASSERT(next_ill != NULL);
16106 		tmp_ill = next_ill->ill_usesrc_grp_next;
16107 		ASSERT(tmp_ill != NULL);
16108 		next_ill->ill_usesrc_grp_next = NULL;
16109 		next_ill->ill_usesrc_ifindex = 0;
16110 		next_ill = tmp_ill;
16111 	} while (next_ill->ill_usesrc_ifindex != 0);
16112 	uill->ill_usesrc_grp_next = NULL;
16113 }
16114 
16115 /*
16116  * Remove the client usesrc ILL from the list and relink to a new list
16117  */
16118 int
16119 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16120 {
16121 	ill_t *ill, *tmp_ill;
16122 	ip_stack_t	*ipst = ucill->ill_ipst;
16123 
16124 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16125 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16126 
16127 	/*
16128 	 * Check if the usesrc client ILL passed in is not already
16129 	 * in use as a usesrc ILL i.e one whose source address is
16130 	 * in use OR a usesrc ILL is not already in use as a usesrc
16131 	 * client ILL
16132 	 */
16133 	if ((ucill->ill_usesrc_ifindex == 0) ||
16134 	    (uill->ill_usesrc_ifindex != 0)) {
16135 		return (-1);
16136 	}
16137 
16138 	ill = ill_prev_usesrc(ucill);
16139 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16140 
16141 	/* Remove from the current list */
16142 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16143 		/* Only two elements in the list */
16144 		ASSERT(ill->ill_usesrc_ifindex == 0);
16145 		ill->ill_usesrc_grp_next = NULL;
16146 	} else {
16147 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16148 	}
16149 
16150 	if (ifindex == 0) {
16151 		ucill->ill_usesrc_ifindex = 0;
16152 		ucill->ill_usesrc_grp_next = NULL;
16153 		return (0);
16154 	}
16155 
16156 	ucill->ill_usesrc_ifindex = ifindex;
16157 	tmp_ill = uill->ill_usesrc_grp_next;
16158 	uill->ill_usesrc_grp_next = ucill;
16159 	ucill->ill_usesrc_grp_next =
16160 	    (tmp_ill != NULL) ? tmp_ill : uill;
16161 	return (0);
16162 }
16163 
16164 /*
16165  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16166  * ip.c for locking details.
16167  */
16168 /* ARGSUSED */
16169 int
16170 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16171     ip_ioctl_cmd_t *ipip, void *ifreq)
16172 {
16173 	struct lifreq *lifr = (struct lifreq *)ifreq;
16174 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16175 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16176 	int err = 0, ret;
16177 	uint_t ifindex;
16178 	ipsq_t *ipsq = NULL;
16179 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16180 
16181 	ASSERT(IAM_WRITER_IPIF(ipif));
16182 	ASSERT(q->q_next == NULL);
16183 	ASSERT(CONN_Q(q));
16184 
16185 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16186 
16187 	ifindex = lifr->lifr_index;
16188 	if (ifindex == 0) {
16189 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16190 			/* non usesrc group interface, nothing to reset */
16191 			return (0);
16192 		}
16193 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16194 		/* valid reset request */
16195 		reset_flg = B_TRUE;
16196 	}
16197 
16198 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16199 	if (usesrc_ill == NULL)
16200 		return (ENXIO);
16201 	if (usesrc_ill == ipif->ipif_ill) {
16202 		ill_refrele(usesrc_ill);
16203 		return (EINVAL);
16204 	}
16205 
16206 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16207 	    NEW_OP, B_TRUE);
16208 	if (ipsq == NULL) {
16209 		err = EINPROGRESS;
16210 		/* Operation enqueued on the ipsq of the usesrc ILL */
16211 		goto done;
16212 	}
16213 
16214 	/* USESRC isn't currently supported with IPMP */
16215 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16216 		err = ENOTSUP;
16217 		goto done;
16218 	}
16219 
16220 	/*
16221 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16222 	 * used by IPMP underlying interfaces, but someone might think it's
16223 	 * more general and try to use it independently with VNI.)
16224 	 */
16225 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16226 		err = ENOTSUP;
16227 		goto done;
16228 	}
16229 
16230 	/*
16231 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16232 	 * already a client then return EINVAL
16233 	 */
16234 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16235 		err = EINVAL;
16236 		goto done;
16237 	}
16238 
16239 	/*
16240 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16241 	 * be then this is a duplicate operation.
16242 	 */
16243 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16244 		err = 0;
16245 		goto done;
16246 	}
16247 
16248 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16249 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16250 	    usesrc_ill->ill_isv6));
16251 
16252 	/*
16253 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16254 	 * and the ill_usesrc_ifindex fields
16255 	 */
16256 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16257 
16258 	if (reset_flg) {
16259 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16260 		if (ret != 0) {
16261 			err = EINVAL;
16262 		}
16263 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16264 		goto done;
16265 	}
16266 
16267 	/*
16268 	 * Four possibilities to consider:
16269 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16270 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16271 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16272 	 * 4. Both are part of their respective usesrc groups
16273 	 */
16274 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16275 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16276 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16277 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16278 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16279 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16280 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16281 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16282 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16283 		/* Insert at head of list */
16284 		usesrc_cli_ill->ill_usesrc_grp_next =
16285 		    usesrc_ill->ill_usesrc_grp_next;
16286 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16287 	} else {
16288 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16289 		    ifindex);
16290 		if (ret != 0)
16291 			err = EINVAL;
16292 	}
16293 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16294 
16295 done:
16296 	if (ipsq != NULL)
16297 		ipsq_exit(ipsq);
16298 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16299 	ill_refrele(usesrc_ill);
16300 
16301 	/* Let conn_ixa caching know that source address selection changed */
16302 	ip_update_source_selection(ipst);
16303 
16304 	return (err);
16305 }
16306 
16307 /* ARGSUSED */
16308 int
16309 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16310     ip_ioctl_cmd_t *ipip, void *if_req)
16311 {
16312 	struct lifreq	*lifr = (struct lifreq *)if_req;
16313 	ill_t		*ill = ipif->ipif_ill;
16314 
16315 	/*
16316 	 * Need a lock since IFF_UP can be set even when there are
16317 	 * references to the ipif.
16318 	 */
16319 	mutex_enter(&ill->ill_lock);
16320 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16321 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16322 	else
16323 		lifr->lifr_dadstate = DAD_DONE;
16324 	mutex_exit(&ill->ill_lock);
16325 	return (0);
16326 }
16327 
16328 /*
16329  * comparison function used by avl.
16330  */
16331 static int
16332 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16333 {
16334 
16335 	uint_t index;
16336 
16337 	ASSERT(phyip != NULL && index_ptr != NULL);
16338 
16339 	index = *((uint_t *)index_ptr);
16340 	/*
16341 	 * let the phyint with the lowest index be on top.
16342 	 */
16343 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16344 		return (1);
16345 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16346 		return (-1);
16347 	return (0);
16348 }
16349 
16350 /*
16351  * comparison function used by avl.
16352  */
16353 static int
16354 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16355 {
16356 	ill_t *ill;
16357 	int res = 0;
16358 
16359 	ASSERT(phyip != NULL && name_ptr != NULL);
16360 
16361 	if (((phyint_t *)phyip)->phyint_illv4)
16362 		ill = ((phyint_t *)phyip)->phyint_illv4;
16363 	else
16364 		ill = ((phyint_t *)phyip)->phyint_illv6;
16365 	ASSERT(ill != NULL);
16366 
16367 	res = strcmp(ill->ill_name, (char *)name_ptr);
16368 	if (res > 0)
16369 		return (1);
16370 	else if (res < 0)
16371 		return (-1);
16372 	return (0);
16373 }
16374 
16375 /*
16376  * This function is called on the unplumb path via ill_glist_delete() when
16377  * there are no ills left on the phyint and thus the phyint can be freed.
16378  */
16379 static void
16380 phyint_free(phyint_t *phyi)
16381 {
16382 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16383 
16384 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16385 
16386 	/*
16387 	 * If this phyint was an IPMP meta-interface, blow away the group.
16388 	 * This is safe to do because all of the illgrps have already been
16389 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16390 	 * If we're cleaning up as a result of failed initialization,
16391 	 * phyint_grp may be NULL.
16392 	 */
16393 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16394 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16395 		ipmp_grp_destroy(phyi->phyint_grp);
16396 		phyi->phyint_grp = NULL;
16397 		rw_exit(&ipst->ips_ipmp_lock);
16398 	}
16399 
16400 	/*
16401 	 * If this interface was under IPMP, take it out of the group.
16402 	 */
16403 	if (phyi->phyint_grp != NULL)
16404 		ipmp_phyint_leave_grp(phyi);
16405 
16406 	/*
16407 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16408 	 * will be freed in ipsq_exit().
16409 	 */
16410 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16411 	phyi->phyint_name[0] = '\0';
16412 
16413 	mi_free(phyi);
16414 }
16415 
16416 /*
16417  * Attach the ill to the phyint structure which can be shared by both
16418  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16419  * function is called from ipif_set_values and ill_lookup_on_name (for
16420  * loopback) where we know the name of the ill. We lookup the ill and if
16421  * there is one present already with the name use that phyint. Otherwise
16422  * reuse the one allocated by ill_init.
16423  */
16424 static void
16425 ill_phyint_reinit(ill_t *ill)
16426 {
16427 	boolean_t isv6 = ill->ill_isv6;
16428 	phyint_t *phyi_old;
16429 	phyint_t *phyi;
16430 	avl_index_t where = 0;
16431 	ill_t	*ill_other = NULL;
16432 	ip_stack_t	*ipst = ill->ill_ipst;
16433 
16434 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16435 
16436 	phyi_old = ill->ill_phyint;
16437 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16438 	    phyi_old->phyint_illv6 == NULL));
16439 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16440 	    phyi_old->phyint_illv4 == NULL));
16441 	ASSERT(phyi_old->phyint_ifindex == 0);
16442 
16443 	/*
16444 	 * Now that our ill has a name, set it in the phyint.
16445 	 */
16446 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16447 
16448 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16449 	    ill->ill_name, &where);
16450 
16451 	/*
16452 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16453 	 *    the global list of ills. So no other thread could have located
16454 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16455 	 * 2. Now locate the other protocol instance of this ill.
16456 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16457 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16458 	 *    of neither ill can change.
16459 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16460 	 *    other ill.
16461 	 * 5. Release all locks.
16462 	 */
16463 
16464 	/*
16465 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16466 	 * we are initializing IPv4.
16467 	 */
16468 	if (phyi != NULL) {
16469 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16470 		ASSERT(ill_other->ill_phyint != NULL);
16471 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16472 		    (!isv6 && ill_other->ill_isv6));
16473 		GRAB_ILL_LOCKS(ill, ill_other);
16474 		/*
16475 		 * We are potentially throwing away phyint_flags which
16476 		 * could be different from the one that we obtain from
16477 		 * ill_other->ill_phyint. But it is okay as we are assuming
16478 		 * that the state maintained within IP is correct.
16479 		 */
16480 		mutex_enter(&phyi->phyint_lock);
16481 		if (isv6) {
16482 			ASSERT(phyi->phyint_illv6 == NULL);
16483 			phyi->phyint_illv6 = ill;
16484 		} else {
16485 			ASSERT(phyi->phyint_illv4 == NULL);
16486 			phyi->phyint_illv4 = ill;
16487 		}
16488 
16489 		/*
16490 		 * Delete the old phyint and make its ipsq eligible
16491 		 * to be freed in ipsq_exit().
16492 		 */
16493 		phyi_old->phyint_illv4 = NULL;
16494 		phyi_old->phyint_illv6 = NULL;
16495 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16496 		phyi_old->phyint_name[0] = '\0';
16497 		mi_free(phyi_old);
16498 	} else {
16499 		mutex_enter(&ill->ill_lock);
16500 		/*
16501 		 * We don't need to acquire any lock, since
16502 		 * the ill is not yet visible globally  and we
16503 		 * have not yet released the ill_g_lock.
16504 		 */
16505 		phyi = phyi_old;
16506 		mutex_enter(&phyi->phyint_lock);
16507 		/* XXX We need a recovery strategy here. */
16508 		if (!phyint_assign_ifindex(phyi, ipst))
16509 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16510 
16511 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16512 		    (void *)phyi, where);
16513 
16514 		(void) avl_find(&ipst->ips_phyint_g_list->
16515 		    phyint_list_avl_by_index,
16516 		    &phyi->phyint_ifindex, &where);
16517 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16518 		    (void *)phyi, where);
16519 	}
16520 
16521 	/*
16522 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16523 	 * pending mp is not affected because that is per ill basis.
16524 	 */
16525 	ill->ill_phyint = phyi;
16526 
16527 	/*
16528 	 * Now that the phyint's ifindex has been assigned, complete the
16529 	 * remaining
16530 	 */
16531 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16532 	if (ill->ill_isv6) {
16533 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16534 		    ill->ill_phyint->phyint_ifindex;
16535 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16536 	} else {
16537 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16538 	}
16539 
16540 	/*
16541 	 * Generate an event within the hooks framework to indicate that
16542 	 * a new interface has just been added to IP.  For this event to
16543 	 * be generated, the network interface must, at least, have an
16544 	 * ifindex assigned to it.  (We don't generate the event for
16545 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16546 	 *
16547 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16548 	 * that the ordering of delivered events to listeners matches the
16549 	 * order of them in the kernel.
16550 	 */
16551 	if (!IS_LOOPBACK(ill)) {
16552 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16553 		    ill->ill_name_length);
16554 	}
16555 	RELEASE_ILL_LOCKS(ill, ill_other);
16556 	mutex_exit(&phyi->phyint_lock);
16557 }
16558 
16559 /*
16560  * Notify any downstream modules of the name of this interface.
16561  * An M_IOCTL is used even though we don't expect a successful reply.
16562  * Any reply message from the driver (presumably an M_IOCNAK) will
16563  * eventually get discarded somewhere upstream.  The message format is
16564  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16565  * to IP.
16566  */
16567 static void
16568 ip_ifname_notify(ill_t *ill, queue_t *q)
16569 {
16570 	mblk_t *mp1, *mp2;
16571 	struct iocblk *iocp;
16572 	struct lifreq *lifr;
16573 
16574 	mp1 = mkiocb(SIOCSLIFNAME);
16575 	if (mp1 == NULL)
16576 		return;
16577 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16578 	if (mp2 == NULL) {
16579 		freeb(mp1);
16580 		return;
16581 	}
16582 
16583 	mp1->b_cont = mp2;
16584 	iocp = (struct iocblk *)mp1->b_rptr;
16585 	iocp->ioc_count = sizeof (struct lifreq);
16586 
16587 	lifr = (struct lifreq *)mp2->b_rptr;
16588 	mp2->b_wptr += sizeof (struct lifreq);
16589 	bzero(lifr, sizeof (struct lifreq));
16590 
16591 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16592 	lifr->lifr_ppa = ill->ill_ppa;
16593 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16594 
16595 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16596 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16597 	putnext(q, mp1);
16598 }
16599 
16600 static int
16601 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16602 {
16603 	int		err;
16604 	ip_stack_t	*ipst = ill->ill_ipst;
16605 	phyint_t	*phyi = ill->ill_phyint;
16606 
16607 	/*
16608 	 * Now that ill_name is set, the configuration for the IPMP
16609 	 * meta-interface can be performed.
16610 	 */
16611 	if (IS_IPMP(ill)) {
16612 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16613 		/*
16614 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16615 		 * meta-interface and we need to create the IPMP group.
16616 		 */
16617 		if (phyi->phyint_grp == NULL) {
16618 			/*
16619 			 * If someone has renamed another IPMP group to have
16620 			 * the same name as our interface, bail.
16621 			 */
16622 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16623 				rw_exit(&ipst->ips_ipmp_lock);
16624 				return (EEXIST);
16625 			}
16626 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16627 			if (phyi->phyint_grp == NULL) {
16628 				rw_exit(&ipst->ips_ipmp_lock);
16629 				return (ENOMEM);
16630 			}
16631 		}
16632 		rw_exit(&ipst->ips_ipmp_lock);
16633 	}
16634 
16635 	/* Tell downstream modules where they are. */
16636 	ip_ifname_notify(ill, q);
16637 
16638 	/*
16639 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16640 	 * Error cases are ENOMEM ...
16641 	 */
16642 	err = ill_dl_phys(ill, ipif, mp, q);
16643 
16644 	if (ill->ill_isv6) {
16645 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16646 		if (ipst->ips_mld_slowtimeout_id == 0) {
16647 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16648 			    (void *)ipst,
16649 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16650 		}
16651 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16652 	} else {
16653 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16654 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16655 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16656 			    (void *)ipst,
16657 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16658 		}
16659 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16660 	}
16661 
16662 	return (err);
16663 }
16664 
16665 /*
16666  * Common routine for ppa and ifname setting. Should be called exclusive.
16667  *
16668  * Returns EINPROGRESS when mp has been consumed by queueing it on
16669  * ipx_pending_mp and the ioctl will complete in ip_rput.
16670  *
16671  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16672  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16673  * For SLIFNAME, we pass these values back to the userland.
16674  */
16675 static int
16676 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16677 {
16678 	ill_t	*ill;
16679 	ipif_t	*ipif;
16680 	ipsq_t	*ipsq;
16681 	char	*ppa_ptr;
16682 	char	*old_ptr;
16683 	char	old_char;
16684 	int	error;
16685 	ip_stack_t	*ipst;
16686 
16687 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16688 	ASSERT(q->q_next != NULL);
16689 	ASSERT(interf_name != NULL);
16690 
16691 	ill = (ill_t *)q->q_ptr;
16692 	ipst = ill->ill_ipst;
16693 
16694 	ASSERT(ill->ill_ipst != NULL);
16695 	ASSERT(ill->ill_name[0] == '\0');
16696 	ASSERT(IAM_WRITER_ILL(ill));
16697 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16698 	ASSERT(ill->ill_ppa == UINT_MAX);
16699 
16700 	ill->ill_defend_start = ill->ill_defend_count = 0;
16701 	/* The ppa is sent down by ifconfig or is chosen */
16702 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16703 		return (EINVAL);
16704 	}
16705 
16706 	/*
16707 	 * make sure ppa passed in is same as ppa in the name.
16708 	 * This check is not made when ppa == UINT_MAX in that case ppa
16709 	 * in the name could be anything. System will choose a ppa and
16710 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16711 	 */
16712 	if (*new_ppa_ptr != UINT_MAX) {
16713 		/* stoi changes the pointer */
16714 		old_ptr = ppa_ptr;
16715 		/*
16716 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16717 		 * (they don't have an externally visible ppa).  We assign one
16718 		 * here so that we can manage the interface.  Note that in
16719 		 * the past this value was always 0 for DLPI 1 drivers.
16720 		 */
16721 		if (*new_ppa_ptr == 0)
16722 			*new_ppa_ptr = stoi(&old_ptr);
16723 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16724 			return (EINVAL);
16725 	}
16726 	/*
16727 	 * terminate string before ppa
16728 	 * save char at that location.
16729 	 */
16730 	old_char = ppa_ptr[0];
16731 	ppa_ptr[0] = '\0';
16732 
16733 	ill->ill_ppa = *new_ppa_ptr;
16734 	/*
16735 	 * Finish as much work now as possible before calling ill_glist_insert
16736 	 * which makes the ill globally visible and also merges it with the
16737 	 * other protocol instance of this phyint. The remaining work is
16738 	 * done after entering the ipsq which may happen sometime later.
16739 	 */
16740 	ipif = ill->ill_ipif;
16741 
16742 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16743 	ipif_assign_seqid(ipif);
16744 
16745 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16746 		ill->ill_flags |= ILLF_IPV4;
16747 
16748 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16749 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16750 
16751 	if (ill->ill_flags & ILLF_IPV6) {
16752 
16753 		ill->ill_isv6 = B_TRUE;
16754 		ill_set_inputfn(ill);
16755 		if (ill->ill_rq != NULL) {
16756 			ill->ill_rq->q_qinfo = &iprinitv6;
16757 		}
16758 
16759 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16760 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16761 		ipif->ipif_v6subnet = ipv6_all_zeros;
16762 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16763 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16764 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16765 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16766 		/*
16767 		 * point-to-point or Non-mulicast capable
16768 		 * interfaces won't do NUD unless explicitly
16769 		 * configured to do so.
16770 		 */
16771 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16772 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16773 			ill->ill_flags |= ILLF_NONUD;
16774 		}
16775 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16776 		if (ill->ill_flags & ILLF_NOARP) {
16777 			/*
16778 			 * Note: xresolv interfaces will eventually need
16779 			 * NOARP set here as well, but that will require
16780 			 * those external resolvers to have some
16781 			 * knowledge of that flag and act appropriately.
16782 			 * Not to be changed at present.
16783 			 */
16784 			ill->ill_flags &= ~ILLF_NOARP;
16785 		}
16786 		/*
16787 		 * Set the ILLF_ROUTER flag according to the global
16788 		 * IPv6 forwarding policy.
16789 		 */
16790 		if (ipst->ips_ipv6_forwarding != 0)
16791 			ill->ill_flags |= ILLF_ROUTER;
16792 	} else if (ill->ill_flags & ILLF_IPV4) {
16793 		ill->ill_isv6 = B_FALSE;
16794 		ill_set_inputfn(ill);
16795 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16796 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16797 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16798 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16799 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16800 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16801 		/*
16802 		 * Set the ILLF_ROUTER flag according to the global
16803 		 * IPv4 forwarding policy.
16804 		 */
16805 		if (ipst->ips_ip_forwarding != 0)
16806 			ill->ill_flags |= ILLF_ROUTER;
16807 	}
16808 
16809 	ASSERT(ill->ill_phyint != NULL);
16810 
16811 	/*
16812 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16813 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16814 	 */
16815 	if (!ill_allocate_mibs(ill))
16816 		return (ENOMEM);
16817 
16818 	/*
16819 	 * Pick a default sap until we get the DL_INFO_ACK back from
16820 	 * the driver.
16821 	 */
16822 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16823 	    ill->ill_media->ip_m_ipv4sap;
16824 
16825 	ill->ill_ifname_pending = 1;
16826 	ill->ill_ifname_pending_err = 0;
16827 
16828 	/*
16829 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16830 	 * that were joined while this ill was not bound to the DLPI link need
16831 	 * to be recovered by ill_recover_multicast().
16832 	 */
16833 	ill->ill_need_recover_multicast = 1;
16834 
16835 	ill_refhold(ill);
16836 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16837 	if ((error = ill_glist_insert(ill, interf_name,
16838 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16839 		ill->ill_ppa = UINT_MAX;
16840 		ill->ill_name[0] = '\0';
16841 		/*
16842 		 * undo null termination done above.
16843 		 */
16844 		ppa_ptr[0] = old_char;
16845 		rw_exit(&ipst->ips_ill_g_lock);
16846 		ill_refrele(ill);
16847 		return (error);
16848 	}
16849 
16850 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16851 
16852 	/*
16853 	 * When we return the buffer pointed to by interf_name should contain
16854 	 * the same name as in ill_name.
16855 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16856 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16857 	 * so copy full name and update the ppa ptr.
16858 	 * When ppa passed in != UINT_MAX all values are correct just undo
16859 	 * null termination, this saves a bcopy.
16860 	 */
16861 	if (*new_ppa_ptr == UINT_MAX) {
16862 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16863 		*new_ppa_ptr = ill->ill_ppa;
16864 	} else {
16865 		/*
16866 		 * undo null termination done above.
16867 		 */
16868 		ppa_ptr[0] = old_char;
16869 	}
16870 
16871 	/* Let SCTP know about this ILL */
16872 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16873 
16874 	/*
16875 	 * ill_glist_insert has made the ill visible globally, and
16876 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16877 	 * we need to hold the ips_ill_g_lock across the call to enter the
16878 	 * ipsq to enforce atomicity and prevent reordering. In the event
16879 	 * the ipsq has changed, and if the new ipsq is currently busy,
16880 	 * we need to make sure that this half-completed ioctl is ahead of
16881 	 * any subsequent ioctl. We achieve this by not dropping the
16882 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16883 	 * ensuring that new ioctls can't start.
16884 	 */
16885 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16886 	    B_TRUE);
16887 
16888 	rw_exit(&ipst->ips_ill_g_lock);
16889 	ill_refrele(ill);
16890 	if (ipsq == NULL)
16891 		return (EINPROGRESS);
16892 
16893 	/*
16894 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16895 	 */
16896 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16897 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16898 	else
16899 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16900 
16901 	error = ipif_set_values_tail(ill, ipif, mp, q);
16902 	ipsq_exit(ipsq);
16903 	if (error != 0 && error != EINPROGRESS) {
16904 		/*
16905 		 * restore previous values
16906 		 */
16907 		ill->ill_isv6 = B_FALSE;
16908 		ill_set_inputfn(ill);
16909 	}
16910 	return (error);
16911 }
16912 
16913 void
16914 ipif_init(ip_stack_t *ipst)
16915 {
16916 	int i;
16917 
16918 	for (i = 0; i < MAX_G_HEADS; i++) {
16919 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16920 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16921 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16922 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16923 	}
16924 
16925 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16926 	    ill_phyint_compare_index,
16927 	    sizeof (phyint_t),
16928 	    offsetof(struct phyint, phyint_avl_by_index));
16929 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16930 	    ill_phyint_compare_name,
16931 	    sizeof (phyint_t),
16932 	    offsetof(struct phyint, phyint_avl_by_name));
16933 }
16934 
16935 /*
16936  * Save enough information so that we can recreate the IRE if
16937  * the interface goes down and then up.
16938  */
16939 void
16940 ill_save_ire(ill_t *ill, ire_t *ire)
16941 {
16942 	mblk_t	*save_mp;
16943 
16944 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16945 	if (save_mp != NULL) {
16946 		ifrt_t	*ifrt;
16947 
16948 		save_mp->b_wptr += sizeof (ifrt_t);
16949 		ifrt = (ifrt_t *)save_mp->b_rptr;
16950 		bzero(ifrt, sizeof (ifrt_t));
16951 		ifrt->ifrt_type = ire->ire_type;
16952 		if (ire->ire_ipversion == IPV4_VERSION) {
16953 			ASSERT(!ill->ill_isv6);
16954 			ifrt->ifrt_addr = ire->ire_addr;
16955 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16956 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16957 			ifrt->ifrt_mask = ire->ire_mask;
16958 		} else {
16959 			ASSERT(ill->ill_isv6);
16960 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16961 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16962 			mutex_enter(&ire->ire_lock);
16963 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16964 			mutex_exit(&ire->ire_lock);
16965 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16966 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16967 		}
16968 		ifrt->ifrt_flags = ire->ire_flags;
16969 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16970 		mutex_enter(&ill->ill_saved_ire_lock);
16971 		save_mp->b_cont = ill->ill_saved_ire_mp;
16972 		ill->ill_saved_ire_mp = save_mp;
16973 		ill->ill_saved_ire_cnt++;
16974 		mutex_exit(&ill->ill_saved_ire_lock);
16975 	}
16976 }
16977 
16978 /*
16979  * Remove one entry from ill_saved_ire_mp.
16980  */
16981 void
16982 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16983 {
16984 	mblk_t	**mpp;
16985 	mblk_t	*mp;
16986 	ifrt_t	*ifrt;
16987 
16988 	/* Remove from ill_saved_ire_mp list if it is there */
16989 	mutex_enter(&ill->ill_saved_ire_lock);
16990 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16991 	    mpp = &(*mpp)->b_cont) {
16992 		in6_addr_t	gw_addr_v6;
16993 
16994 		/*
16995 		 * On a given ill, the tuple of address, gateway, mask,
16996 		 * ire_type, and zoneid is unique for each saved IRE.
16997 		 */
16998 		mp = *mpp;
16999 		ifrt = (ifrt_t *)mp->b_rptr;
17000 		/* ire_gateway_addr_v6 can change - need lock */
17001 		mutex_enter(&ire->ire_lock);
17002 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17003 		mutex_exit(&ire->ire_lock);
17004 
17005 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17006 		    ifrt->ifrt_type != ire->ire_type)
17007 			continue;
17008 
17009 		if (ill->ill_isv6 ?
17010 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17011 		    &ire->ire_addr_v6) &&
17012 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17013 		    &gw_addr_v6) &&
17014 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17015 		    &ire->ire_mask_v6)) :
17016 		    (ifrt->ifrt_addr == ire->ire_addr &&
17017 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17018 		    ifrt->ifrt_mask == ire->ire_mask)) {
17019 			*mpp = mp->b_cont;
17020 			ill->ill_saved_ire_cnt--;
17021 			freeb(mp);
17022 			break;
17023 		}
17024 	}
17025 	mutex_exit(&ill->ill_saved_ire_lock);
17026 }
17027 
17028 /*
17029  * IP multirouting broadcast routes handling
17030  * Append CGTP broadcast IREs to regular ones created
17031  * at ifconfig time.
17032  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17033  * the destination and the gateway are broadcast addresses.
17034  * The caller has verified that the destination is an IRE_BROADCAST and that
17035  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17036  * we create a MULTIRT IRE_BROADCAST.
17037  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17038  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17039  */
17040 static void
17041 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17042 {
17043 	ire_t *ire_prim;
17044 
17045 	ASSERT(ire != NULL);
17046 
17047 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17048 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17049 	    NULL);
17050 	if (ire_prim != NULL) {
17051 		/*
17052 		 * We are in the special case of broadcasts for
17053 		 * CGTP. We add an IRE_BROADCAST that holds
17054 		 * the RTF_MULTIRT flag, the destination
17055 		 * address and the low level
17056 		 * info of ire_prim. In other words, CGTP
17057 		 * broadcast is added to the redundant ipif.
17058 		 */
17059 		ill_t *ill_prim;
17060 		ire_t  *bcast_ire;
17061 
17062 		ill_prim = ire_prim->ire_ill;
17063 
17064 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17065 		    (void *)ire_prim, (void *)ill_prim));
17066 
17067 		bcast_ire = ire_create(
17068 		    (uchar_t *)&ire->ire_addr,
17069 		    (uchar_t *)&ip_g_all_ones,
17070 		    (uchar_t *)&ire->ire_gateway_addr,
17071 		    IRE_BROADCAST,
17072 		    ill_prim,
17073 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17074 		    ire->ire_flags | RTF_KERNEL,
17075 		    NULL,
17076 		    ipst);
17077 
17078 		/*
17079 		 * Here we assume that ire_add does head insertion so that
17080 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17081 		 */
17082 		if (bcast_ire != NULL) {
17083 			if (ire->ire_flags & RTF_SETSRC) {
17084 				bcast_ire->ire_setsrc_addr =
17085 				    ire->ire_setsrc_addr;
17086 			}
17087 			bcast_ire = ire_add(bcast_ire);
17088 			if (bcast_ire != NULL) {
17089 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17090 				    "added bcast_ire %p\n",
17091 				    (void *)bcast_ire));
17092 
17093 				ill_save_ire(ill_prim, bcast_ire);
17094 				ire_refrele(bcast_ire);
17095 			}
17096 		}
17097 		ire_refrele(ire_prim);
17098 	}
17099 }
17100 
17101 /*
17102  * IP multirouting broadcast routes handling
17103  * Remove the broadcast ire.
17104  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17105  * the destination and the gateway are broadcast addresses.
17106  * The caller has only verified that RTF_MULTIRT was set. We check
17107  * that the destination is broadcast and that the gateway is a broadcast
17108  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17109  */
17110 static void
17111 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17112 {
17113 	ASSERT(ire != NULL);
17114 
17115 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17116 		ire_t *ire_prim;
17117 
17118 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17119 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17120 		    ipst, NULL);
17121 		if (ire_prim != NULL) {
17122 			ill_t *ill_prim;
17123 			ire_t  *bcast_ire;
17124 
17125 			ill_prim = ire_prim->ire_ill;
17126 
17127 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17128 			    "ire_prim %p, ill_prim %p\n",
17129 			    (void *)ire_prim, (void *)ill_prim));
17130 
17131 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17132 			    ire->ire_gateway_addr, IRE_BROADCAST,
17133 			    ill_prim, ALL_ZONES, NULL,
17134 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17135 			    MATCH_IRE_MASK, 0, ipst, NULL);
17136 
17137 			if (bcast_ire != NULL) {
17138 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17139 				    "looked up bcast_ire %p\n",
17140 				    (void *)bcast_ire));
17141 				ill_remove_saved_ire(bcast_ire->ire_ill,
17142 				    bcast_ire);
17143 				ire_delete(bcast_ire);
17144 				ire_refrele(bcast_ire);
17145 			}
17146 			ire_refrele(ire_prim);
17147 		}
17148 	}
17149 }
17150 
17151 /*
17152  * Derive an interface id from the link layer address.
17153  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17154  */
17155 static void
17156 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17157 {
17158 	char		*addr;
17159 
17160 	/*
17161 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17162 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17163 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17164 	 * interface ID on IPv6 interfaces above links that actually have real
17165 	 * Ethernet addresses.
17166 	 */
17167 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17168 		/* Form EUI-64 like address */
17169 		addr = (char *)&v6addr->s6_addr32[2];
17170 		bcopy(ill->ill_phys_addr, addr, 3);
17171 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17172 		addr[3] = (char)0xff;
17173 		addr[4] = (char)0xfe;
17174 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17175 	}
17176 }
17177 
17178 /* ARGSUSED */
17179 static void
17180 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17181 {
17182 }
17183 
17184 typedef struct ipmp_ifcookie {
17185 	uint32_t	ic_hostid;
17186 	char		ic_ifname[LIFNAMSIZ];
17187 	char		ic_zonename[ZONENAME_MAX];
17188 } ipmp_ifcookie_t;
17189 
17190 /*
17191  * Construct a pseudo-random interface ID for the IPMP interface that's both
17192  * predictable and (almost) guaranteed to be unique.
17193  */
17194 static void
17195 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17196 {
17197 	zone_t		*zp;
17198 	uint8_t		*addr;
17199 	uchar_t		hash[16];
17200 	ulong_t 	hostid;
17201 	MD5_CTX		ctx;
17202 	ipmp_ifcookie_t	ic = { 0 };
17203 
17204 	ASSERT(IS_IPMP(ill));
17205 
17206 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17207 	ic.ic_hostid = htonl((uint32_t)hostid);
17208 
17209 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17210 
17211 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17212 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17213 		zone_rele(zp);
17214 	}
17215 
17216 	MD5Init(&ctx);
17217 	MD5Update(&ctx, &ic, sizeof (ic));
17218 	MD5Final(hash, &ctx);
17219 
17220 	/*
17221 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17222 	 */
17223 	addr = &v6addr->s6_addr8[8];
17224 	bcopy(hash + 8, addr, sizeof (uint64_t));
17225 	addr[0] &= ~0x2;				/* set local bit */
17226 }
17227 
17228 /*
17229  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17230  */
17231 static void
17232 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17233 {
17234 	phyint_t *phyi = ill->ill_phyint;
17235 
17236 	/*
17237 	 * Check PHYI_MULTI_BCAST and length of physical
17238 	 * address to determine if we use the mapping or the
17239 	 * broadcast address.
17240 	 */
17241 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17242 	    ill->ill_phys_addr_length != ETHERADDRL) {
17243 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17244 		return;
17245 	}
17246 	m_physaddr[0] = 0x33;
17247 	m_physaddr[1] = 0x33;
17248 	m_physaddr[2] = m_ip6addr[12];
17249 	m_physaddr[3] = m_ip6addr[13];
17250 	m_physaddr[4] = m_ip6addr[14];
17251 	m_physaddr[5] = m_ip6addr[15];
17252 }
17253 
17254 /*
17255  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17256  */
17257 static void
17258 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17259 {
17260 	phyint_t *phyi = ill->ill_phyint;
17261 
17262 	/*
17263 	 * Check PHYI_MULTI_BCAST and length of physical
17264 	 * address to determine if we use the mapping or the
17265 	 * broadcast address.
17266 	 */
17267 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17268 	    ill->ill_phys_addr_length != ETHERADDRL) {
17269 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17270 		return;
17271 	}
17272 	m_physaddr[0] = 0x01;
17273 	m_physaddr[1] = 0x00;
17274 	m_physaddr[2] = 0x5e;
17275 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17276 	m_physaddr[4] = m_ipaddr[2];
17277 	m_physaddr[5] = m_ipaddr[3];
17278 }
17279 
17280 /* ARGSUSED */
17281 static void
17282 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17283 {
17284 	/*
17285 	 * for the MULTI_BCAST case and other cases when we want to
17286 	 * use the link-layer broadcast address for multicast.
17287 	 */
17288 	uint8_t	*bphys_addr;
17289 	dl_unitdata_req_t *dlur;
17290 
17291 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17292 	if (ill->ill_sap_length < 0) {
17293 		bphys_addr = (uchar_t *)dlur +
17294 		    dlur->dl_dest_addr_offset;
17295 	} else  {
17296 		bphys_addr = (uchar_t *)dlur +
17297 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17298 	}
17299 
17300 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17301 }
17302 
17303 /*
17304  * Derive IPoIB interface id from the link layer address.
17305  */
17306 static void
17307 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17308 {
17309 	char		*addr;
17310 
17311 	ASSERT(ill->ill_phys_addr_length == 20);
17312 	addr = (char *)&v6addr->s6_addr32[2];
17313 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17314 	/*
17315 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17316 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17317 	 * rules. In these cases, the IBA considers these GUIDs to be in
17318 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17319 	 * required; vendors are required not to assign global EUI-64's
17320 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17321 	 * of the interface identifier. Whether the GUID is in modified
17322 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17323 	 * bit set to 1.
17324 	 */
17325 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17326 }
17327 
17328 /*
17329  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17330  * Note on mapping from multicast IP addresses to IPoIB multicast link
17331  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17332  * The format of an IPoIB multicast address is:
17333  *
17334  *  4 byte QPN      Scope Sign.  Pkey
17335  * +--------------------------------------------+
17336  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17337  * +--------------------------------------------+
17338  *
17339  * The Scope and Pkey components are properties of the IBA port and
17340  * network interface. They can be ascertained from the broadcast address.
17341  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17342  */
17343 static void
17344 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17345 {
17346 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17347 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17348 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17349 	uint8_t	*bphys_addr;
17350 	dl_unitdata_req_t *dlur;
17351 
17352 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17353 
17354 	/*
17355 	 * RFC 4391: IPv4 MGID is 28-bit long.
17356 	 */
17357 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17358 	m_physaddr[17] = m_ipaddr[1];
17359 	m_physaddr[18] = m_ipaddr[2];
17360 	m_physaddr[19] = m_ipaddr[3];
17361 
17362 
17363 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17364 	if (ill->ill_sap_length < 0) {
17365 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17366 	} else  {
17367 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17368 		    ill->ill_sap_length;
17369 	}
17370 	/*
17371 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17372 	 */
17373 	m_physaddr[5] = bphys_addr[5];
17374 	m_physaddr[8] = bphys_addr[8];
17375 	m_physaddr[9] = bphys_addr[9];
17376 }
17377 
17378 static void
17379 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17380 {
17381 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17382 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17383 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17384 	uint8_t	*bphys_addr;
17385 	dl_unitdata_req_t *dlur;
17386 
17387 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17388 
17389 	/*
17390 	 * RFC 4391: IPv4 MGID is 80-bit long.
17391 	 */
17392 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17393 
17394 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17395 	if (ill->ill_sap_length < 0) {
17396 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17397 	} else  {
17398 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17399 		    ill->ill_sap_length;
17400 	}
17401 	/*
17402 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17403 	 */
17404 	m_physaddr[5] = bphys_addr[5];
17405 	m_physaddr[8] = bphys_addr[8];
17406 	m_physaddr[9] = bphys_addr[9];
17407 }
17408 
17409 /*
17410  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17411  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17412  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17413  * of RFC4213.
17414  */
17415 static void
17416 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17417 {
17418 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17419 	v6addr->s6_addr32[2] = 0;
17420 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17421 }
17422 
17423 /*
17424  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17425  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17426  * id.
17427  */
17428 static void
17429 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17430 {
17431 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17432 
17433 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17434 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17435 }
17436 
17437 static void
17438 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17439 {
17440 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17441 }
17442 
17443 static void
17444 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17445 {
17446 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17447 }
17448 
17449 static void
17450 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17451 {
17452 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17453 }
17454 
17455 static void
17456 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17457 {
17458 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17459 }
17460 
17461 /*
17462  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17463  * Returns an held ill, or NULL.
17464  */
17465 ill_t *
17466 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17467     ip_stack_t *ipst)
17468 {
17469 	ill_t	*ill;
17470 	ipif_t	*ipif;
17471 
17472 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17473 	if (ill == NULL)
17474 		return (NULL);
17475 
17476 	mutex_enter(&ill->ill_lock);
17477 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17478 		if (IPIF_IS_CONDEMNED(ipif))
17479 			continue;
17480 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17481 		    ipif->ipif_zoneid != ALL_ZONES)
17482 			continue;
17483 
17484 		mutex_exit(&ill->ill_lock);
17485 		return (ill);
17486 	}
17487 	mutex_exit(&ill->ill_lock);
17488 	ill_refrele(ill);
17489 	return (NULL);
17490 }
17491 
17492 /*
17493  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17494  * If a pointer to an ipif_t is returned then the caller will need to do
17495  * an ill_refrele().
17496  */
17497 ipif_t *
17498 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17499     ip_stack_t *ipst)
17500 {
17501 	ipif_t *ipif;
17502 	ill_t *ill;
17503 
17504 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17505 	if (ill == NULL)
17506 		return (NULL);
17507 
17508 	mutex_enter(&ill->ill_lock);
17509 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17510 		mutex_exit(&ill->ill_lock);
17511 		ill_refrele(ill);
17512 		return (NULL);
17513 	}
17514 
17515 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17516 		if (!IPIF_CAN_LOOKUP(ipif))
17517 			continue;
17518 		if (lifidx == ipif->ipif_id) {
17519 			ipif_refhold_locked(ipif);
17520 			break;
17521 		}
17522 	}
17523 
17524 	mutex_exit(&ill->ill_lock);
17525 	ill_refrele(ill);
17526 	return (ipif);
17527 }
17528 
17529 /*
17530  * Set ill_inputfn based on the current know state.
17531  * This needs to be called when any of the factors taken into
17532  * account changes.
17533  */
17534 void
17535 ill_set_inputfn(ill_t *ill)
17536 {
17537 	ip_stack_t	*ipst = ill->ill_ipst;
17538 
17539 	if (ill->ill_isv6) {
17540 		if (is_system_labeled())
17541 			ill->ill_inputfn = ill_input_full_v6;
17542 		else
17543 			ill->ill_inputfn = ill_input_short_v6;
17544 	} else {
17545 		if (is_system_labeled())
17546 			ill->ill_inputfn = ill_input_full_v4;
17547 		else if (ill->ill_dhcpinit != 0)
17548 			ill->ill_inputfn = ill_input_full_v4;
17549 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17550 		    != NULL)
17551 			ill->ill_inputfn = ill_input_full_v4;
17552 		else if (ipst->ips_ip_cgtp_filter &&
17553 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17554 			ill->ill_inputfn = ill_input_full_v4;
17555 		else
17556 			ill->ill_inputfn = ill_input_short_v4;
17557 	}
17558 }
17559 
17560 /*
17561  * Re-evaluate ill_inputfn for all the IPv4 ills.
17562  * Used when RSVP and CGTP comes and goes.
17563  */
17564 void
17565 ill_set_inputfn_all(ip_stack_t *ipst)
17566 {
17567 	ill_walk_context_t	ctx;
17568 	ill_t			*ill;
17569 
17570 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17571 	ill = ILL_START_WALK_V4(&ctx, ipst);
17572 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17573 		ill_set_inputfn(ill);
17574 
17575 	rw_exit(&ipst->ips_ill_g_lock);
17576 }
17577 
17578 /*
17579  * Set the physical address information for `ill' to the contents of the
17580  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17581  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17582  * EINPROGRESS will be returned.
17583  */
17584 int
17585 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17586 {
17587 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17588 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17589 
17590 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17591 
17592 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17593 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17594 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17595 		/* Changing DL_IPV6_TOKEN is not yet supported */
17596 		return (0);
17597 	}
17598 
17599 	/*
17600 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17601 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17602 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17603 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17604 	 */
17605 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17606 		freemsg(mp);
17607 		return (ENOMEM);
17608 	}
17609 
17610 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17611 	mutex_enter(&ill->ill_lock);
17612 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17613 	/* no more nce addition allowed */
17614 	mutex_exit(&ill->ill_lock);
17615 
17616 	/*
17617 	 * If we can quiesce the ill, then set the address.  If not, then
17618 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17619 	 */
17620 	ill_down_ipifs(ill, B_TRUE);
17621 	mutex_enter(&ill->ill_lock);
17622 	if (!ill_is_quiescent(ill)) {
17623 		/* call cannot fail since `conn_t *' argument is NULL */
17624 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17625 		    mp, ILL_DOWN);
17626 		mutex_exit(&ill->ill_lock);
17627 		return (EINPROGRESS);
17628 	}
17629 	mutex_exit(&ill->ill_lock);
17630 
17631 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17632 	return (0);
17633 }
17634 
17635 /*
17636  * Once the ill associated with `q' has quiesced, set its physical address
17637  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17638  * are passed (linked by b_cont), since we sometimes need to save two distinct
17639  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17640  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17641  * is quiesced, we know any stale nce's with the old address information have
17642  * already been removed, so we don't need to call nce_flush().
17643  */
17644 /* ARGSUSED */
17645 static void
17646 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17647 {
17648 	ill_t		*ill = q->q_ptr;
17649 	mblk_t		*addrmp2 = unlinkb(addrmp);
17650 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17651 	uint_t		addrlen, addroff;
17652 	int		status;
17653 
17654 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17655 
17656 	addroff	= dlindp->dl_addr_offset;
17657 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17658 
17659 	switch (dlindp->dl_data) {
17660 	case DL_IPV6_LINK_LAYER_ADDR:
17661 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17662 		freemsg(addrmp2);
17663 		break;
17664 
17665 	case DL_CURR_DEST_ADDR:
17666 		freemsg(ill->ill_dest_addr_mp);
17667 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17668 		ill->ill_dest_addr_mp = addrmp;
17669 		if (ill->ill_isv6) {
17670 			ill_setdesttoken(ill);
17671 			ipif_setdestlinklocal(ill->ill_ipif);
17672 		}
17673 		freemsg(addrmp2);
17674 		break;
17675 
17676 	case DL_CURR_PHYS_ADDR:
17677 		freemsg(ill->ill_phys_addr_mp);
17678 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17679 		ill->ill_phys_addr_mp = addrmp;
17680 		ill->ill_phys_addr_length = addrlen;
17681 		if (ill->ill_isv6)
17682 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17683 		else
17684 			freemsg(addrmp2);
17685 		if (ill->ill_isv6) {
17686 			ill_setdefaulttoken(ill);
17687 			ipif_setlinklocal(ill->ill_ipif);
17688 		}
17689 		break;
17690 	default:
17691 		ASSERT(0);
17692 	}
17693 
17694 	/*
17695 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17696 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17697 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17698 	 * brought up.
17699 	 */
17700 	status = ill_up_ipifs(ill, q, addrmp);
17701 	mutex_enter(&ill->ill_lock);
17702 	if (ill->ill_dl_up)
17703 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17704 	mutex_exit(&ill->ill_lock);
17705 	if (status != EINPROGRESS)
17706 		ipsq_current_finish(ipsq);
17707 }
17708 
17709 /*
17710  * Helper routine for setting the ill_nd_lla fields.
17711  */
17712 void
17713 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17714 {
17715 	freemsg(ill->ill_nd_lla_mp);
17716 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17717 	ill->ill_nd_lla_mp = ndmp;
17718 	ill->ill_nd_lla_len = addrlen;
17719 }
17720 
17721 /*
17722  * Replumb the ill.
17723  */
17724 int
17725 ill_replumb(ill_t *ill, mblk_t *mp)
17726 {
17727 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17728 
17729 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17730 
17731 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17732 
17733 	mutex_enter(&ill->ill_lock);
17734 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17735 	/* no more nce addition allowed */
17736 	mutex_exit(&ill->ill_lock);
17737 
17738 	/*
17739 	 * If we can quiesce the ill, then continue.  If not, then
17740 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17741 	 */
17742 	ill_down_ipifs(ill, B_FALSE);
17743 
17744 	mutex_enter(&ill->ill_lock);
17745 	if (!ill_is_quiescent(ill)) {
17746 		/* call cannot fail since `conn_t *' argument is NULL */
17747 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17748 		    mp, ILL_DOWN);
17749 		mutex_exit(&ill->ill_lock);
17750 		return (EINPROGRESS);
17751 	}
17752 	mutex_exit(&ill->ill_lock);
17753 
17754 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17755 	return (0);
17756 }
17757 
17758 /* ARGSUSED */
17759 static void
17760 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17761 {
17762 	ill_t *ill = q->q_ptr;
17763 	int err;
17764 	conn_t *connp = NULL;
17765 
17766 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17767 	freemsg(ill->ill_replumb_mp);
17768 	ill->ill_replumb_mp = copyb(mp);
17769 
17770 	if (ill->ill_replumb_mp == NULL) {
17771 		/* out of memory */
17772 		ipsq_current_finish(ipsq);
17773 		return;
17774 	}
17775 
17776 	mutex_enter(&ill->ill_lock);
17777 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17778 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17779 	mutex_exit(&ill->ill_lock);
17780 
17781 	if (!ill->ill_up_ipifs) {
17782 		/* already closing */
17783 		ipsq_current_finish(ipsq);
17784 		return;
17785 	}
17786 	ill->ill_replumbing = 1;
17787 	err = ill_down_ipifs_tail(ill);
17788 
17789 	/*
17790 	 * Successfully quiesced and brought down the interface, now we send
17791 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17792 	 * DL_NOTE_REPLUMB message.
17793 	 */
17794 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17795 	    DL_NOTIFY_CONF);
17796 	ASSERT(mp != NULL);
17797 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17798 	    DL_NOTE_REPLUMB_DONE;
17799 	ill_dlpi_send(ill, mp);
17800 
17801 	/*
17802 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17803 	 * streams have to be unbound. When all the DLPI exchanges are done,
17804 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17805 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17806 	 * arp_bringup_done().
17807 	 */
17808 	ASSERT(ill->ill_replumb_mp != NULL);
17809 	if (err == EINPROGRESS)
17810 		return;
17811 	else
17812 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17813 	ASSERT(connp == NULL);
17814 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17815 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17816 		return;
17817 	}
17818 	ipsq_current_finish(ipsq);
17819 }
17820 
17821 /*
17822  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17823  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17824  * as per the ioctl.  On failure, an errno is returned.
17825  */
17826 static int
17827 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17828 {
17829 	int rval;
17830 	struct strioctl iocb;
17831 
17832 	iocb.ic_cmd = cmd;
17833 	iocb.ic_timout = 15;
17834 	iocb.ic_len = bufsize;
17835 	iocb.ic_dp = buf;
17836 
17837 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17838 }
17839 
17840 /*
17841  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17842  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17843  */
17844 static int
17845 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17846     uint_t *bufsizep, cred_t *cr)
17847 {
17848 	int err;
17849 	struct lifnum lifn;
17850 
17851 	bzero(&lifn, sizeof (lifn));
17852 	lifn.lifn_family = af;
17853 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17854 
17855 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17856 		return (err);
17857 
17858 	/*
17859 	 * Pad the interface count to account for additional interfaces that
17860 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17861 	 */
17862 	lifn.lifn_count += 4;
17863 	bzero(lifcp, sizeof (*lifcp));
17864 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17865 	lifcp->lifc_family = af;
17866 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17867 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17868 
17869 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17870 	if (err != 0) {
17871 		kmem_free(lifcp->lifc_buf, *bufsizep);
17872 		return (err);
17873 	}
17874 
17875 	return (0);
17876 }
17877 
17878 /*
17879  * Helper for ip_interface_cleanup() that removes the loopback interface.
17880  */
17881 static void
17882 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17883 {
17884 	int err;
17885 	struct lifreq lifr;
17886 
17887 	bzero(&lifr, sizeof (lifr));
17888 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17889 
17890 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17891 	if (err != 0) {
17892 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17893 		    "error %d\n", isv6 ? "v6" : "v4", err));
17894 	}
17895 }
17896 
17897 /*
17898  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17899  * groups and that IPMP data addresses are down.  These conditions must be met
17900  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17901  */
17902 static void
17903 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17904 {
17905 	int af = isv6 ? AF_INET6 : AF_INET;
17906 	int i, nifs;
17907 	int err;
17908 	uint_t bufsize;
17909 	uint_t lifrsize = sizeof (struct lifreq);
17910 	struct lifconf lifc;
17911 	struct lifreq *lifrp;
17912 
17913 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17914 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17915 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17916 		return;
17917 	}
17918 
17919 	nifs = lifc.lifc_len / lifrsize;
17920 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17921 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17922 		if (err != 0) {
17923 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17924 			    "flags: error %d", lifrp->lifr_name, err);
17925 			continue;
17926 		}
17927 
17928 		if (lifrp->lifr_flags & IFF_IPMP) {
17929 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17930 				continue;
17931 
17932 			lifrp->lifr_flags &= ~IFF_UP;
17933 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17934 			if (err != 0) {
17935 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17936 				    "bring down (error %d); IPMP interface may "
17937 				    "not be shutdown", lifrp->lifr_name, err);
17938 			}
17939 
17940 			/*
17941 			 * Check if IFF_DUPLICATE is still set -- and if so,
17942 			 * reset the address to clear it.
17943 			 */
17944 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17945 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17946 				continue;
17947 
17948 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17949 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17950 			    lifrp, lifrsize, cr)) != 0) {
17951 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17952 				    "reset DAD (error %d); IPMP interface may "
17953 				    "not be shutdown", lifrp->lifr_name, err);
17954 			}
17955 			continue;
17956 		}
17957 
17958 		lifrp->lifr_groupname[0] = '\0';
17959 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17960 		if (err != 0) {
17961 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17962 			    "IPMP group (error %d); associated IPMP interface "
17963 			    "may not be shutdown", lifrp->lifr_name, err);
17964 			continue;
17965 		}
17966 	}
17967 
17968 	kmem_free(lifc.lifc_buf, bufsize);
17969 }
17970 
17971 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17972 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17973 
17974 /*
17975  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17976  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17977  * when the user-level processes in the zone are killed and the latter are
17978  * cleaned up by str_stack_shutdown().
17979  */
17980 void
17981 ip_interface_cleanup(ip_stack_t *ipst)
17982 {
17983 	ldi_handle_t	lh;
17984 	ldi_ident_t	li;
17985 	cred_t		*cr;
17986 	int		err;
17987 	int		i;
17988 	char		*devs[] = { UDP6DEV, UDPDEV };
17989 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17990 
17991 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17992 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17993 		    " error %d", err);
17994 		return;
17995 	}
17996 
17997 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17998 	ASSERT(cr != NULL);
17999 
18000 	/*
18001 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18002 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18003 	 * the loop.)
18004 	 */
18005 	for (i = 0; i < 2; i++) {
18006 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18007 		if (err != 0) {
18008 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18009 			    " error %d", devs[i], err);
18010 			continue;
18011 		}
18012 
18013 		ip_loopback_removeif(lh, i == 0, cr);
18014 		ip_ipmp_cleanup(lh, i == 0, cr);
18015 
18016 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18017 	}
18018 
18019 	ldi_ident_release(li);
18020 	crfree(cr);
18021 }
18022 
18023 /*
18024  * This needs to be in-sync with nic_event_t definition
18025  */
18026 static const char *
18027 ill_hook_event2str(nic_event_t event)
18028 {
18029 	switch (event) {
18030 	case NE_PLUMB:
18031 		return ("PLUMB");
18032 	case NE_UNPLUMB:
18033 		return ("UNPLUMB");
18034 	case NE_UP:
18035 		return ("UP");
18036 	case NE_DOWN:
18037 		return ("DOWN");
18038 	case NE_ADDRESS_CHANGE:
18039 		return ("ADDRESS_CHANGE");
18040 	case NE_LIF_UP:
18041 		return ("LIF_UP");
18042 	case NE_LIF_DOWN:
18043 		return ("LIF_DOWN");
18044 	case NE_IFINDEX_CHANGE:
18045 		return ("IFINDEX_CHANGE");
18046 	default:
18047 		return ("UNKNOWN");
18048 	}
18049 }
18050 
18051 void
18052 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18053     nic_event_data_t data, size_t datalen)
18054 {
18055 	ip_stack_t		*ipst = ill->ill_ipst;
18056 	hook_nic_event_int_t	*info;
18057 	const char		*str = NULL;
18058 
18059 	/* create a new nic event info */
18060 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18061 		goto fail;
18062 
18063 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18064 	info->hnei_event.hne_lif = lif;
18065 	info->hnei_event.hne_event = event;
18066 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18067 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18068 	info->hnei_event.hne_data = NULL;
18069 	info->hnei_event.hne_datalen = 0;
18070 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18071 
18072 	if (data != NULL && datalen != 0) {
18073 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18074 		if (info->hnei_event.hne_data == NULL)
18075 			goto fail;
18076 		bcopy(data, info->hnei_event.hne_data, datalen);
18077 		info->hnei_event.hne_datalen = datalen;
18078 	}
18079 
18080 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18081 	    DDI_NOSLEEP) == DDI_SUCCESS)
18082 		return;
18083 
18084 fail:
18085 	if (info != NULL) {
18086 		if (info->hnei_event.hne_data != NULL) {
18087 			kmem_free(info->hnei_event.hne_data,
18088 			    info->hnei_event.hne_datalen);
18089 		}
18090 		kmem_free(info, sizeof (hook_nic_event_t));
18091 	}
18092 	str = ill_hook_event2str(event);
18093 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18094 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18095 }
18096 
18097 static int
18098 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18099 {
18100 	int		err = 0;
18101 	const in_addr_t	*addr = NULL;
18102 	nce_t		*nce = NULL;
18103 	ill_t		*ill = ipif->ipif_ill;
18104 	ill_t		*bound_ill;
18105 	boolean_t	added_ipif = B_FALSE;
18106 	uint16_t	state;
18107 	uint16_t	flags;
18108 
18109 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18110 	    ill_t *, ill, ipif_t *, ipif);
18111 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18112 		addr = &ipif->ipif_lcl_addr;
18113 	}
18114 
18115 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18116 		if (res_act != Res_act_initial)
18117 			return (EINVAL);
18118 	}
18119 
18120 	if (addr != NULL) {
18121 		ipmp_illgrp_t	*illg = ill->ill_grp;
18122 
18123 		/* add unicast nce for the local addr */
18124 
18125 		if (IS_IPMP(ill)) {
18126 			/*
18127 			 * If we're here via ipif_up(), then the ipif
18128 			 * won't be bound yet -- add it to the group,
18129 			 * which will bind it if possible. (We would
18130 			 * add it in ipif_up(), but deleting on failure
18131 			 * there is gruesome.)  If we're here via
18132 			 * ipmp_ill_bind_ipif(), then the ipif has
18133 			 * already been added to the group and we
18134 			 * just need to use the binding.
18135 			 */
18136 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18137 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18138 				if (bound_ill == NULL) {
18139 					/*
18140 					 * We couldn't bind the ipif to an ill
18141 					 * yet, so we have nothing to publish.
18142 					 * Mark the address as ready and return.
18143 					 */
18144 					ipif->ipif_addr_ready = 1;
18145 					return (0);
18146 				}
18147 				added_ipif = B_TRUE;
18148 			}
18149 		} else {
18150 			bound_ill = ill;
18151 		}
18152 
18153 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18154 		    NCE_F_NONUD);
18155 		/*
18156 		 * If this is an initial bring-up (or the ipif was never
18157 		 * completely brought up), do DAD.  Otherwise, we're here
18158 		 * because IPMP has rebound an address to this ill: send
18159 		 * unsolicited advertisements (ARP announcements) to
18160 		 * inform others.
18161 		 */
18162 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18163 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18164 		} else {
18165 			state = ND_REACHABLE;
18166 			flags |= NCE_F_UNSOL_ADV;
18167 		}
18168 
18169 retry:
18170 		err = nce_lookup_then_add_v4(ill,
18171 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18172 		    addr, flags, state, &nce);
18173 
18174 		/*
18175 		 * note that we may encounter EEXIST if we are moving
18176 		 * the nce as a result of a rebind operation.
18177 		 */
18178 		switch (err) {
18179 		case 0:
18180 			ipif->ipif_added_nce = 1;
18181 			nce->nce_ipif_cnt++;
18182 			break;
18183 		case EEXIST:
18184 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18185 			    ill->ill_name));
18186 			if (!NCE_MYADDR(nce->nce_common)) {
18187 				/*
18188 				 * A leftover nce from before this address
18189 				 * existed
18190 				 */
18191 				ncec_delete(nce->nce_common);
18192 				nce_refrele(nce);
18193 				nce = NULL;
18194 				goto retry;
18195 			}
18196 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18197 				nce_refrele(nce);
18198 				nce = NULL;
18199 				ip1dbg(("ipif_arp_up: NCE already exists "
18200 				    "for %s:%u\n", ill->ill_name,
18201 				    ipif->ipif_id));
18202 				goto arp_up_done;
18203 			}
18204 			/*
18205 			 * Duplicate local addresses are permissible for
18206 			 * IPIF_POINTOPOINT interfaces which will get marked
18207 			 * IPIF_UNNUMBERED later in
18208 			 * ip_addr_availability_check().
18209 			 *
18210 			 * The nce_ipif_cnt field tracks the number of
18211 			 * ipifs that have nce_addr as their local address.
18212 			 */
18213 			ipif->ipif_addr_ready = 1;
18214 			ipif->ipif_added_nce = 1;
18215 			nce->nce_ipif_cnt++;
18216 			err = 0;
18217 			break;
18218 		default:
18219 			ASSERT(nce == NULL);
18220 			goto arp_up_done;
18221 		}
18222 		if (arp_no_defense) {
18223 			if ((ipif->ipif_flags & IPIF_UP) &&
18224 			    !ipif->ipif_addr_ready)
18225 				ipif_up_notify(ipif);
18226 			ipif->ipif_addr_ready = 1;
18227 		}
18228 	} else {
18229 		/* zero address. nothing to publish */
18230 		ipif->ipif_addr_ready = 1;
18231 	}
18232 	if (nce != NULL)
18233 		nce_refrele(nce);
18234 arp_up_done:
18235 	if (added_ipif && err != 0)
18236 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18237 	return (err);
18238 }
18239 
18240 int
18241 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18242 {
18243 	int 		err = 0;
18244 	ill_t 		*ill = ipif->ipif_ill;
18245 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18246 
18247 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18248 	    ill_t *, ill, ipif_t *, ipif);
18249 
18250 	/*
18251 	 * need to bring up ARP or setup mcast mapping only
18252 	 * when the first interface is coming UP.
18253 	 */
18254 	first_interface = (ill->ill_ipif_up_count == 0 &&
18255 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18256 
18257 	if (res_act == Res_act_initial && first_interface) {
18258 		/*
18259 		 * Send ATTACH + BIND
18260 		 */
18261 		err = arp_ll_up(ill);
18262 		if (err != EINPROGRESS && err != 0)
18263 			return (err);
18264 
18265 		/*
18266 		 * Add NCE for local address. Start DAD.
18267 		 * we'll wait to hear that DAD has finished
18268 		 * before using the interface.
18269 		 */
18270 		if (err == EINPROGRESS)
18271 			wait_for_dlpi = B_TRUE;
18272 	}
18273 
18274 	if (!wait_for_dlpi)
18275 		(void) ipif_arp_up_done_tail(ipif, res_act);
18276 
18277 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18278 }
18279 
18280 /*
18281  * Finish processing of "arp_up" after all the DLPI message
18282  * exchanges have completed between arp and the driver.
18283  */
18284 void
18285 arp_bringup_done(ill_t *ill, int err)
18286 {
18287 	mblk_t	*mp1;
18288 	ipif_t  *ipif;
18289 	conn_t *connp = NULL;
18290 	ipsq_t	*ipsq;
18291 	queue_t *q;
18292 
18293 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18294 
18295 	ASSERT(IAM_WRITER_ILL(ill));
18296 
18297 	ipsq = ill->ill_phyint->phyint_ipsq;
18298 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18299 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18300 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18301 	if (mp1 == NULL) /* bringup was aborted by the user */
18302 		return;
18303 
18304 	/*
18305 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18306 	 * must have an associated conn_t.  Otherwise, we're bringing this
18307 	 * interface back up as part of handling an asynchronous event (e.g.,
18308 	 * physical address change).
18309 	 */
18310 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18311 		ASSERT(connp != NULL);
18312 		q = CONNP_TO_WQ(connp);
18313 	} else {
18314 		ASSERT(connp == NULL);
18315 		q = ill->ill_rq;
18316 	}
18317 	if (err == 0) {
18318 		if (ipif->ipif_isv6) {
18319 			if ((err = ipif_up_done_v6(ipif)) != 0)
18320 				ip0dbg(("arp_bringup_done: init failed\n"));
18321 		} else {
18322 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18323 			if (err != 0 ||
18324 			    (err = ipif_up_done(ipif)) != 0) {
18325 				ip0dbg(("arp_bringup_done: "
18326 				    "init failed err %x\n", err));
18327 				(void) ipif_arp_down(ipif);
18328 			}
18329 
18330 		}
18331 	} else {
18332 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18333 	}
18334 
18335 	if ((err == 0) && (ill->ill_up_ipifs)) {
18336 		err = ill_up_ipifs(ill, q, mp1);
18337 		if (err == EINPROGRESS)
18338 			return;
18339 	}
18340 
18341 	/*
18342 	 * If we have a moved ipif to bring up, and everything has succeeded
18343 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18344 	 * down -- the admin can try to bring it up by hand if need be.
18345 	 */
18346 	if (ill->ill_move_ipif != NULL) {
18347 		ipif = ill->ill_move_ipif;
18348 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18349 		    ipif->ipif_ill->ill_name));
18350 		ill->ill_move_ipif = NULL;
18351 		if (err == 0) {
18352 			err = ipif_up(ipif, q, mp1);
18353 			if (err == EINPROGRESS)
18354 				return;
18355 		}
18356 	}
18357 
18358 	/*
18359 	 * The operation must complete without EINPROGRESS since
18360 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18361 	 * Otherwise, the operation will be stuck forever in the ipsq.
18362 	 */
18363 	ASSERT(err != EINPROGRESS);
18364 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18365 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18366 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18367 		    ill_t *, ill, ipif_t *, ipif);
18368 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18369 	} else {
18370 		ipsq_current_finish(ipsq);
18371 	}
18372 }
18373 
18374 /*
18375  * Finish processing of arp replumb after all the DLPI message
18376  * exchanges have completed between arp and the driver.
18377  */
18378 void
18379 arp_replumb_done(ill_t *ill, int err)
18380 {
18381 	mblk_t	*mp1;
18382 	ipif_t  *ipif;
18383 	conn_t *connp = NULL;
18384 	ipsq_t	*ipsq;
18385 	queue_t *q;
18386 
18387 	ASSERT(IAM_WRITER_ILL(ill));
18388 
18389 	ipsq = ill->ill_phyint->phyint_ipsq;
18390 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18391 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18392 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18393 	if (mp1 == NULL) {
18394 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18395 		    ipsq->ipsq_xop->ipx_current_ioctl));
18396 		/* bringup was aborted by the user */
18397 		return;
18398 	}
18399 	/*
18400 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18401 	 * must have an associated conn_t.  Otherwise, we're bringing this
18402 	 * interface back up as part of handling an asynchronous event (e.g.,
18403 	 * physical address change).
18404 	 */
18405 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18406 		ASSERT(connp != NULL);
18407 		q = CONNP_TO_WQ(connp);
18408 	} else {
18409 		ASSERT(connp == NULL);
18410 		q = ill->ill_rq;
18411 	}
18412 	if ((err == 0) && (ill->ill_up_ipifs)) {
18413 		err = ill_up_ipifs(ill, q, mp1);
18414 		if (err == EINPROGRESS)
18415 			return;
18416 	}
18417 	/*
18418 	 * The operation must complete without EINPROGRESS since
18419 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18420 	 * Otherwise, the operation will be stuck forever in the ipsq.
18421 	 */
18422 	ASSERT(err != EINPROGRESS);
18423 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18424 		DTRACE_PROBE4(ipif__ioctl, char *,
18425 		    "arp_replumb_done finish",
18426 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18427 		    ill_t *, ill, ipif_t *, ipif);
18428 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18429 	} else {
18430 		ipsq_current_finish(ipsq);
18431 	}
18432 }
18433 
18434 void
18435 ipif_up_notify(ipif_t *ipif)
18436 {
18437 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18438 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18439 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18440 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18441 	    NE_LIF_UP, NULL, 0);
18442 }
18443 
18444 /*
18445  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18446  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18447  * TPI end points with STREAMS modules pushed above.  This is assured by not
18448  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18449  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18450  * while unwinding from the ispq and that could be a thread from the bottom.
18451  */
18452 /* ARGSUSED */
18453 int
18454 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18455     ip_ioctl_cmd_t *ipip, void *arg)
18456 {
18457 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18458 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18459 	int ret = 0;
18460 	int i;
18461 	size_t size;
18462 	ip_stack_t *ipst;
18463 	zoneid_t zoneid;
18464 	ilb_stack_t *ilbs;
18465 
18466 	ipst = CONNQ_TO_IPST(q);
18467 	ilbs = ipst->ips_netstack->netstack_ilb;
18468 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18469 
18470 	switch (command) {
18471 	case ILB_CREATE_RULE: {
18472 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18473 
18474 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18475 			ret = EINVAL;
18476 			break;
18477 		}
18478 
18479 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18480 		break;
18481 	}
18482 	case ILB_DESTROY_RULE:
18483 	case ILB_ENABLE_RULE:
18484 	case ILB_DISABLE_RULE: {
18485 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18486 
18487 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18488 			ret = EINVAL;
18489 			break;
18490 		}
18491 
18492 		if (cmd->flags & ILB_RULE_ALLRULES) {
18493 			if (command == ILB_DESTROY_RULE) {
18494 				ilb_rule_del_all(ilbs, zoneid);
18495 				break;
18496 			} else if (command == ILB_ENABLE_RULE) {
18497 				ilb_rule_enable_all(ilbs, zoneid);
18498 				break;
18499 			} else if (command == ILB_DISABLE_RULE) {
18500 				ilb_rule_disable_all(ilbs, zoneid);
18501 				break;
18502 			}
18503 		} else {
18504 			if (command == ILB_DESTROY_RULE) {
18505 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18506 			} else if (command == ILB_ENABLE_RULE) {
18507 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18508 				    NULL);
18509 			} else if (command == ILB_DISABLE_RULE) {
18510 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18511 				    NULL);
18512 			}
18513 		}
18514 		break;
18515 	}
18516 	case ILB_NUM_RULES: {
18517 		ilb_num_rules_cmd_t *cmd;
18518 
18519 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18520 			ret = EINVAL;
18521 			break;
18522 		}
18523 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18524 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18525 		break;
18526 	}
18527 	case ILB_RULE_NAMES: {
18528 		ilb_rule_names_cmd_t *cmd;
18529 
18530 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18531 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18532 		    cmd->num_names == 0) {
18533 			ret = EINVAL;
18534 			break;
18535 		}
18536 		size = cmd->num_names * ILB_RULE_NAMESZ;
18537 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18538 		    size != cmd_mp->b_wptr) {
18539 			ret = EINVAL;
18540 			break;
18541 		}
18542 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18543 		break;
18544 	}
18545 	case ILB_NUM_SERVERS: {
18546 		ilb_num_servers_cmd_t *cmd;
18547 
18548 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18549 			ret = EINVAL;
18550 			break;
18551 		}
18552 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18553 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18554 		    &(cmd->num));
18555 		break;
18556 	}
18557 	case ILB_LIST_RULE: {
18558 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18559 
18560 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18561 			ret = EINVAL;
18562 			break;
18563 		}
18564 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18565 		break;
18566 	}
18567 	case ILB_LIST_SERVERS: {
18568 		ilb_servers_info_cmd_t *cmd;
18569 
18570 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18571 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18572 		    cmd->num_servers == 0) {
18573 			ret = EINVAL;
18574 			break;
18575 		}
18576 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18577 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18578 		    size != cmd_mp->b_wptr) {
18579 			ret = EINVAL;
18580 			break;
18581 		}
18582 
18583 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18584 		    &cmd->num_servers);
18585 		break;
18586 	}
18587 	case ILB_ADD_SERVERS: {
18588 		ilb_servers_info_cmd_t *cmd;
18589 		ilb_rule_t *rule;
18590 
18591 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18592 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18593 			ret = EINVAL;
18594 			break;
18595 		}
18596 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18597 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18598 		    size != cmd_mp->b_wptr) {
18599 			ret = EINVAL;
18600 			break;
18601 		}
18602 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18603 		if (rule == NULL) {
18604 			ASSERT(ret != 0);
18605 			break;
18606 		}
18607 		for (i = 0; i < cmd->num_servers; i++) {
18608 			ilb_server_info_t *s;
18609 
18610 			s = &cmd->servers[i];
18611 			s->err = ilb_server_add(ilbs, rule, s);
18612 		}
18613 		ILB_RULE_REFRELE(rule);
18614 		break;
18615 	}
18616 	case ILB_DEL_SERVERS:
18617 	case ILB_ENABLE_SERVERS:
18618 	case ILB_DISABLE_SERVERS: {
18619 		ilb_servers_cmd_t *cmd;
18620 		ilb_rule_t *rule;
18621 		int (*f)();
18622 
18623 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18624 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18625 			ret = EINVAL;
18626 			break;
18627 		}
18628 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18629 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18630 		    size != cmd_mp->b_wptr) {
18631 			ret = EINVAL;
18632 			break;
18633 		}
18634 
18635 		if (command == ILB_DEL_SERVERS)
18636 			f = ilb_server_del;
18637 		else if (command == ILB_ENABLE_SERVERS)
18638 			f = ilb_server_enable;
18639 		else if (command == ILB_DISABLE_SERVERS)
18640 			f = ilb_server_disable;
18641 
18642 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18643 		if (rule == NULL) {
18644 			ASSERT(ret != 0);
18645 			break;
18646 		}
18647 
18648 		for (i = 0; i < cmd->num_servers; i++) {
18649 			ilb_server_arg_t *s;
18650 
18651 			s = &cmd->servers[i];
18652 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18653 		}
18654 		ILB_RULE_REFRELE(rule);
18655 		break;
18656 	}
18657 	case ILB_LIST_NAT_TABLE: {
18658 		ilb_list_nat_cmd_t *cmd;
18659 
18660 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18661 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18662 			ret = EINVAL;
18663 			break;
18664 		}
18665 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18666 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18667 		    size != cmd_mp->b_wptr) {
18668 			ret = EINVAL;
18669 			break;
18670 		}
18671 
18672 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18673 		    &cmd->flags);
18674 		break;
18675 	}
18676 	case ILB_LIST_STICKY_TABLE: {
18677 		ilb_list_sticky_cmd_t *cmd;
18678 
18679 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18680 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18681 			ret = EINVAL;
18682 			break;
18683 		}
18684 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18685 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18686 		    size != cmd_mp->b_wptr) {
18687 			ret = EINVAL;
18688 			break;
18689 		}
18690 
18691 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18692 		    &cmd->num_sticky, &cmd->flags);
18693 		break;
18694 	}
18695 	default:
18696 		ret = EINVAL;
18697 		break;
18698 	}
18699 done:
18700 	return (ret);
18701 }
18702 
18703 /* Remove all cache entries for this logical interface */
18704 void
18705 ipif_nce_down(ipif_t *ipif)
18706 {
18707 	ill_t *ill = ipif->ipif_ill;
18708 	nce_t *nce;
18709 
18710 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18711 	    ill_t *, ill, ipif_t *, ipif);
18712 	if (ipif->ipif_added_nce) {
18713 		if (ipif->ipif_isv6)
18714 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18715 		else
18716 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18717 		if (nce != NULL) {
18718 			if (--nce->nce_ipif_cnt == 0)
18719 				ncec_delete(nce->nce_common);
18720 			ipif->ipif_added_nce = 0;
18721 			nce_refrele(nce);
18722 		} else {
18723 			/*
18724 			 * nce may already be NULL because it was already
18725 			 * flushed, e.g., due to a call to nce_flush
18726 			 */
18727 			ipif->ipif_added_nce = 0;
18728 		}
18729 	}
18730 	/*
18731 	 * Make IPMP aware of the deleted data address.
18732 	 */
18733 	if (IS_IPMP(ill))
18734 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18735 
18736 	/*
18737 	 * Remove all other nces dependent on this ill when the last ipif
18738 	 * is going away.
18739 	 */
18740 	if (ill->ill_ipif_up_count == 0) {
18741 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18742 		    (uchar_t *)ill, ill->ill_ipst);
18743 		if (IS_UNDER_IPMP(ill))
18744 			nce_flush(ill, B_TRUE);
18745 	}
18746 }
18747 
18748 /*
18749  * find the first interface that uses usill for its source address.
18750  */
18751 ill_t *
18752 ill_lookup_usesrc(ill_t *usill)
18753 {
18754 	ip_stack_t *ipst = usill->ill_ipst;
18755 	ill_t *ill;
18756 
18757 	ASSERT(usill != NULL);
18758 
18759 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18760 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18761 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18762 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18763 	    ill = ill->ill_usesrc_grp_next) {
18764 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18765 		    !ILL_IS_CONDEMNED(ill)) {
18766 			ill_refhold(ill);
18767 			break;
18768 		}
18769 	}
18770 	rw_exit(&ipst->ips_ill_g_lock);
18771 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18772 	return (ill);
18773 }
18774