xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision 72b703890acc1682901e7ab4df40758e3c4399d8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 1990 Mentat Inc.
24  */
25 
26 /*
27  * This file contains the interface control functions for IP.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/dlpi.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/sysmacros.h>
36 #include <sys/strsubr.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/ctype.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.h>
68 #include <sys/md5.h>
69 
70 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
71 #include <inet/mi.h>
72 #include <inet/nd.h>
73 #include <inet/tunables.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
105 #include <inet/udp_impl.h> /* needed for udp_stack_t */
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
121 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
122 		    char *value, caddr_t cp, cred_t *ioc_cr);
123 
124 static boolean_t ill_is_quiescent(ill_t *);
125 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
126 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
127 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
128     mblk_t *mp, boolean_t need_up);
129 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
130     mblk_t *mp, boolean_t need_up);
131 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
132     queue_t *q, mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
134     mblk_t *mp);
135 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp);
137 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
138     queue_t *q, mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
140     int ioccmd, struct linkblk *li);
141 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
142 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
143 static void	ipsq_flush(ill_t *ill);
144 
145 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static void	ipsq_delete(ipsq_t *);
148 
149 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
150     boolean_t initialize, boolean_t insert, int *errorp);
151 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
152 static void	ipif_delete_bcast_ires(ipif_t *ipif);
153 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
154 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
155 		    boolean_t isv6);
156 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
157 static void	ipif_free(ipif_t *ipif);
158 static void	ipif_free_tail(ipif_t *ipif);
159 static void	ipif_set_default(ipif_t *ipif);
160 static int	ipif_set_values(queue_t *q, mblk_t *mp,
161     char *interf_name, uint_t *ppa);
162 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
163     queue_t *q);
164 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
165     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
166     ip_stack_t *);
167 static ipif_t	*ipif_lookup_on_name_async(char *name, size_t namelen,
168     boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
169     int *error, ip_stack_t *);
170 
171 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
172 static void	ill_delete_interface_type(ill_if_t *);
173 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
174 static void	ill_dl_down(ill_t *ill);
175 static void	ill_down(ill_t *ill);
176 static void	ill_down_ipifs(ill_t *, boolean_t);
177 static void	ill_free_mib(ill_t *ill);
178 static void	ill_glist_delete(ill_t *);
179 static void	ill_phyint_reinit(ill_t *ill);
180 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
181 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
182 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
183 
184 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
185 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
186 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
187 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
188 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
189 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
190 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
191 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
192 static ip_v4mapinfo_func_t ip_mbcast_mapping;
193 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
194 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
195 static void	phyint_free(phyint_t *);
196 
197 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
198 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
199 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
200 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
201 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
202 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
203     dl_capability_sub_t *);
204 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
205 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
206 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
207 		    dl_capability_sub_t *);
208 static void	ill_capability_dld_enable(ill_t *);
209 static void	ill_capability_ack_thr(void *);
210 static void	ill_capability_lso_enable(ill_t *);
211 
212 static ill_t	*ill_prev_usesrc(ill_t *);
213 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
214 static void	ill_disband_usesrc_group(ill_t *);
215 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
216 
217 #ifdef DEBUG
218 static	void	ill_trace_cleanup(const ill_t *);
219 static	void	ipif_trace_cleanup(const ipif_t *);
220 #endif
221 
222 static	void	ill_dlpi_clear_deferred(ill_t *ill);
223 
224 /*
225  * if we go over the memory footprint limit more than once in this msec
226  * interval, we'll start pruning aggressively.
227  */
228 int ip_min_frag_prune_time = 0;
229 
230 static ipft_t	ip_ioctl_ftbl[] = {
231 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
232 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
233 		IPFT_F_NO_REPLY },
234 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
235 	{ 0 }
236 };
237 
238 /* Simple ICMP IP Header Template */
239 static ipha_t icmp_ipha = {
240 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
241 };
242 
243 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
244 
245 static ip_m_t   ip_m_tbl[] = {
246 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
260 	    ip_nodef_v6intfid },
261 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
262 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
263 	    ip_nodef_v6intfid },
264 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_ipv4_v6destintfid },
267 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
268 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
269 	    ip_ipv6_v6destintfid },
270 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
271 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
272 	    ip_nodef_v6intfid },
273 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
274 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
275 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
276 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
277 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
278 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
279 	    ip_nodef_v6intfid }
280 };
281 
282 static ill_t	ill_null;		/* Empty ILL for init. */
283 char	ipif_loopback_name[] = "lo0";
284 
285 /* These are used by all IP network modules. */
286 sin6_t	sin6_null;	/* Zero address for quick clears */
287 sin_t	sin_null;	/* Zero address for quick clears */
288 
289 /* When set search for unused ipif_seqid */
290 static ipif_t	ipif_zero;
291 
292 /*
293  * ppa arena is created after these many
294  * interfaces have been plumbed.
295  */
296 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
297 
298 /*
299  * Allocate per-interface mibs.
300  * Returns true if ok. False otherwise.
301  *  ipsq  may not yet be allocated (loopback case ).
302  */
303 static boolean_t
304 ill_allocate_mibs(ill_t *ill)
305 {
306 	/* Already allocated? */
307 	if (ill->ill_ip_mib != NULL) {
308 		if (ill->ill_isv6)
309 			ASSERT(ill->ill_icmp6_mib != NULL);
310 		return (B_TRUE);
311 	}
312 
313 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
314 	    KM_NOSLEEP);
315 	if (ill->ill_ip_mib == NULL) {
316 		return (B_FALSE);
317 	}
318 
319 	/* Setup static information */
320 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
321 	    sizeof (mib2_ipIfStatsEntry_t));
322 	if (ill->ill_isv6) {
323 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
325 		    sizeof (mib2_ipv6AddrEntry_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
327 		    sizeof (mib2_ipv6RouteEntry_t));
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
329 		    sizeof (mib2_ipv6NetToMediaEntry_t));
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
331 		    sizeof (ipv6_member_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
333 		    sizeof (ipv6_grpsrc_t));
334 	} else {
335 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
337 		    sizeof (mib2_ipAddrEntry_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
339 		    sizeof (mib2_ipRouteEntry_t));
340 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
341 		    sizeof (mib2_ipNetToMediaEntry_t));
342 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
343 		    sizeof (ip_member_t));
344 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
345 		    sizeof (ip_grpsrc_t));
346 
347 		/*
348 		 * For a v4 ill, we are done at this point, because per ill
349 		 * icmp mibs are only used for v6.
350 		 */
351 		return (B_TRUE);
352 	}
353 
354 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
355 	    KM_NOSLEEP);
356 	if (ill->ill_icmp6_mib == NULL) {
357 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
358 		ill->ill_ip_mib = NULL;
359 		return (B_FALSE);
360 	}
361 	/* static icmp info */
362 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
363 	    sizeof (mib2_ipv6IfIcmpEntry_t);
364 	/*
365 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
366 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
367 	 * -> ill_phyint_reinit
368 	 */
369 	return (B_TRUE);
370 }
371 
372 /*
373  * Completely vaporize a lower level tap and all associated interfaces.
374  * ill_delete is called only out of ip_close when the device control
375  * stream is being closed.
376  */
377 void
378 ill_delete(ill_t *ill)
379 {
380 	ipif_t	*ipif;
381 	ill_t	*prev_ill;
382 	ip_stack_t	*ipst = ill->ill_ipst;
383 
384 	/*
385 	 * ill_delete may be forcibly entering the ipsq. The previous
386 	 * ioctl may not have completed and may need to be aborted.
387 	 * ipsq_flush takes care of it. If we don't need to enter the
388 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
389 	 * ill_delete_tail is sufficient.
390 	 */
391 	ipsq_flush(ill);
392 
393 	/*
394 	 * Nuke all interfaces.  ipif_free will take down the interface,
395 	 * remove it from the list, and free the data structure.
396 	 * Walk down the ipif list and remove the logical interfaces
397 	 * first before removing the main ipif. We can't unplumb
398 	 * zeroth interface first in the case of IPv6 as update_conn_ill
399 	 * -> ip_ll_multireq de-references ill_ipif for checking
400 	 * POINTOPOINT.
401 	 *
402 	 * If ill_ipif was not properly initialized (i.e low on memory),
403 	 * then no interfaces to clean up. In this case just clean up the
404 	 * ill.
405 	 */
406 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
407 		ipif_free(ipif);
408 
409 	/*
410 	 * clean out all the nce_t entries that depend on this
411 	 * ill for the ill_phys_addr.
412 	 */
413 	nce_flush(ill, B_TRUE);
414 
415 	/* Clean up msgs on pending upcalls for mrouted */
416 	reset_mrt_ill(ill);
417 
418 	update_conn_ill(ill, ipst);
419 
420 	/*
421 	 * Remove multicast references added as a result of calls to
422 	 * ip_join_allmulti().
423 	 */
424 	ip_purge_allmulti(ill);
425 
426 	/*
427 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
428 	 */
429 	if (IS_UNDER_IPMP(ill))
430 		ipmp_ill_leave_illgrp(ill);
431 
432 	/*
433 	 * ill_down will arrange to blow off any IRE's dependent on this
434 	 * ILL, and shut down fragmentation reassembly.
435 	 */
436 	ill_down(ill);
437 
438 	/* Let SCTP know, so that it can remove this from its list. */
439 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
440 
441 	/*
442 	 * Walk all CONNs that can have a reference on an ire or nce for this
443 	 * ill (we actually walk all that now have stale references).
444 	 */
445 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
446 
447 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
448 	if (ill->ill_isv6)
449 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
450 
451 	/*
452 	 * If an address on this ILL is being used as a source address then
453 	 * clear out the pointers in other ILLs that point to this ILL.
454 	 */
455 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
456 	if (ill->ill_usesrc_grp_next != NULL) {
457 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
458 			ill_disband_usesrc_group(ill);
459 		} else {	/* consumer of the usesrc ILL */
460 			prev_ill = ill_prev_usesrc(ill);
461 			prev_ill->ill_usesrc_grp_next =
462 			    ill->ill_usesrc_grp_next;
463 		}
464 	}
465 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
466 }
467 
468 static void
469 ipif_non_duplicate(ipif_t *ipif)
470 {
471 	ill_t *ill = ipif->ipif_ill;
472 	mutex_enter(&ill->ill_lock);
473 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
474 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
475 		ASSERT(ill->ill_ipif_dup_count > 0);
476 		ill->ill_ipif_dup_count--;
477 	}
478 	mutex_exit(&ill->ill_lock);
479 }
480 
481 /*
482  * ill_delete_tail is called from ip_modclose after all references
483  * to the closing ill are gone. The wait is done in ip_modclose
484  */
485 void
486 ill_delete_tail(ill_t *ill)
487 {
488 	mblk_t	**mpp;
489 	ipif_t	*ipif;
490 	ip_stack_t *ipst = ill->ill_ipst;
491 
492 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
493 		ipif_non_duplicate(ipif);
494 		(void) ipif_down_tail(ipif);
495 	}
496 
497 	ASSERT(ill->ill_ipif_dup_count == 0);
498 
499 	/*
500 	 * If polling capability is enabled (which signifies direct
501 	 * upcall into IP and driver has ill saved as a handle),
502 	 * we need to make sure that unbind has completed before we
503 	 * let the ill disappear and driver no longer has any reference
504 	 * to this ill.
505 	 */
506 	mutex_enter(&ill->ill_lock);
507 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
508 		cv_wait(&ill->ill_cv, &ill->ill_lock);
509 	mutex_exit(&ill->ill_lock);
510 	ASSERT(!(ill->ill_capabilities &
511 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
512 
513 	if (ill->ill_net_type != IRE_LOOPBACK)
514 		qprocsoff(ill->ill_rq);
515 
516 	/*
517 	 * We do an ipsq_flush once again now. New messages could have
518 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
519 	 * could also have landed up if an ioctl thread had looked up
520 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
521 	 * enqueued the ioctl when we did the ipsq_flush last time.
522 	 */
523 	ipsq_flush(ill);
524 
525 	/*
526 	 * Free capabilities.
527 	 */
528 	if (ill->ill_hcksum_capab != NULL) {
529 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
530 		ill->ill_hcksum_capab = NULL;
531 	}
532 
533 	if (ill->ill_zerocopy_capab != NULL) {
534 		kmem_free(ill->ill_zerocopy_capab,
535 		    sizeof (ill_zerocopy_capab_t));
536 		ill->ill_zerocopy_capab = NULL;
537 	}
538 
539 	if (ill->ill_lso_capab != NULL) {
540 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
541 		ill->ill_lso_capab = NULL;
542 	}
543 
544 	if (ill->ill_dld_capab != NULL) {
545 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
546 		ill->ill_dld_capab = NULL;
547 	}
548 
549 	while (ill->ill_ipif != NULL)
550 		ipif_free_tail(ill->ill_ipif);
551 
552 	/*
553 	 * We have removed all references to ilm from conn and the ones joined
554 	 * within the kernel.
555 	 *
556 	 * We don't walk conns, mrts and ires because
557 	 *
558 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
559 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
560 	 *    ill references.
561 	 */
562 
563 	/*
564 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
565 	 * is safe to do because the illgrp has already been unlinked from the
566 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
567 	 */
568 	if (IS_IPMP(ill)) {
569 		ipmp_illgrp_destroy(ill->ill_grp);
570 		ill->ill_grp = NULL;
571 	}
572 
573 	/*
574 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
575 	 * could free the phyint. No more reference to the phyint after this
576 	 * point.
577 	 */
578 	(void) ill_glist_delete(ill);
579 
580 	if (ill->ill_frag_ptr != NULL) {
581 		uint_t count;
582 
583 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
584 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
585 		}
586 		mi_free(ill->ill_frag_ptr);
587 		ill->ill_frag_ptr = NULL;
588 		ill->ill_frag_hash_tbl = NULL;
589 	}
590 
591 	freemsg(ill->ill_nd_lla_mp);
592 	/* Free all retained control messages. */
593 	mpp = &ill->ill_first_mp_to_free;
594 	do {
595 		while (mpp[0]) {
596 			mblk_t  *mp;
597 			mblk_t  *mp1;
598 
599 			mp = mpp[0];
600 			mpp[0] = mp->b_next;
601 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
602 				mp1->b_next = NULL;
603 				mp1->b_prev = NULL;
604 			}
605 			freemsg(mp);
606 		}
607 	} while (mpp++ != &ill->ill_last_mp_to_free);
608 
609 	ill_free_mib(ill);
610 
611 #ifdef DEBUG
612 	ill_trace_cleanup(ill);
613 #endif
614 
615 	/* The default multicast interface might have changed */
616 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
617 
618 	/* Drop refcnt here */
619 	netstack_rele(ill->ill_ipst->ips_netstack);
620 	ill->ill_ipst = NULL;
621 }
622 
623 static void
624 ill_free_mib(ill_t *ill)
625 {
626 	ip_stack_t *ipst = ill->ill_ipst;
627 
628 	/*
629 	 * MIB statistics must not be lost, so when an interface
630 	 * goes away the counter values will be added to the global
631 	 * MIBs.
632 	 */
633 	if (ill->ill_ip_mib != NULL) {
634 		if (ill->ill_isv6) {
635 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
636 			    ill->ill_ip_mib);
637 		} else {
638 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
639 			    ill->ill_ip_mib);
640 		}
641 
642 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
643 		ill->ill_ip_mib = NULL;
644 	}
645 	if (ill->ill_icmp6_mib != NULL) {
646 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
647 		    ill->ill_icmp6_mib);
648 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
649 		ill->ill_icmp6_mib = NULL;
650 	}
651 }
652 
653 /*
654  * Concatenate together a physical address and a sap.
655  *
656  * Sap_lengths are interpreted as follows:
657  *   sap_length == 0	==>	no sap
658  *   sap_length > 0	==>	sap is at the head of the dlpi address
659  *   sap_length < 0	==>	sap is at the tail of the dlpi address
660  */
661 static void
662 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
663     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
664 {
665 	uint16_t sap_addr = (uint16_t)sap_src;
666 
667 	if (sap_length == 0) {
668 		if (phys_src == NULL)
669 			bzero(dst, phys_length);
670 		else
671 			bcopy(phys_src, dst, phys_length);
672 	} else if (sap_length < 0) {
673 		if (phys_src == NULL)
674 			bzero(dst, phys_length);
675 		else
676 			bcopy(phys_src, dst, phys_length);
677 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
678 	} else {
679 		bcopy(&sap_addr, dst, sizeof (sap_addr));
680 		if (phys_src == NULL)
681 			bzero((char *)dst + sap_length, phys_length);
682 		else
683 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
684 	}
685 }
686 
687 /*
688  * Generate a dl_unitdata_req mblk for the device and address given.
689  * addr_length is the length of the physical portion of the address.
690  * If addr is NULL include an all zero address of the specified length.
691  * TRUE? In any case, addr_length is taken to be the entire length of the
692  * dlpi address, including the absolute value of sap_length.
693  */
694 mblk_t *
695 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
696 		t_scalar_t sap_length)
697 {
698 	dl_unitdata_req_t *dlur;
699 	mblk_t	*mp;
700 	t_scalar_t	abs_sap_length;		/* absolute value */
701 
702 	abs_sap_length = ABS(sap_length);
703 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
704 	    DL_UNITDATA_REQ);
705 	if (mp == NULL)
706 		return (NULL);
707 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
708 	/* HACK: accomodate incompatible DLPI drivers */
709 	if (addr_length == 8)
710 		addr_length = 6;
711 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
712 	dlur->dl_dest_addr_offset = sizeof (*dlur);
713 	dlur->dl_priority.dl_min = 0;
714 	dlur->dl_priority.dl_max = 0;
715 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
716 	    (uchar_t *)&dlur[1]);
717 	return (mp);
718 }
719 
720 /*
721  * Add the pending mp to the list. There can be only 1 pending mp
722  * in the list. Any exclusive ioctl that needs to wait for a response
723  * from another module or driver needs to use this function to set
724  * the ipx_pending_mp to the ioctl mblk and wait for the response from
725  * the other module/driver. This is also used while waiting for the
726  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
727  */
728 boolean_t
729 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
730     int waitfor)
731 {
732 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
733 
734 	ASSERT(IAM_WRITER_IPIF(ipif));
735 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
736 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
737 	ASSERT(ipx->ipx_pending_mp == NULL);
738 	/*
739 	 * The caller may be using a different ipif than the one passed into
740 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
741 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
742 	 * that `ipx_current_ipif == ipif'.
743 	 */
744 	ASSERT(ipx->ipx_current_ipif != NULL);
745 
746 	/*
747 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
748 	 * driver.
749 	 */
750 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
751 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
752 	    (DB_TYPE(add_mp) == M_PCPROTO));
753 
754 	if (connp != NULL) {
755 		ASSERT(MUTEX_HELD(&connp->conn_lock));
756 		/*
757 		 * Return error if the conn has started closing. The conn
758 		 * could have finished cleaning up the pending mp list,
759 		 * If so we should not add another mp to the list negating
760 		 * the cleanup.
761 		 */
762 		if (connp->conn_state_flags & CONN_CLOSING)
763 			return (B_FALSE);
764 	}
765 	mutex_enter(&ipx->ipx_lock);
766 	ipx->ipx_pending_ipif = ipif;
767 	/*
768 	 * Note down the queue in b_queue. This will be returned by
769 	 * ipsq_pending_mp_get. Caller will then use these values to restart
770 	 * the processing
771 	 */
772 	add_mp->b_next = NULL;
773 	add_mp->b_queue = q;
774 	ipx->ipx_pending_mp = add_mp;
775 	ipx->ipx_waitfor = waitfor;
776 	mutex_exit(&ipx->ipx_lock);
777 
778 	if (connp != NULL)
779 		connp->conn_oper_pending_ill = ipif->ipif_ill;
780 
781 	return (B_TRUE);
782 }
783 
784 /*
785  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
786  * queued in the list.
787  */
788 mblk_t *
789 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
790 {
791 	mblk_t	*curr = NULL;
792 	ipxop_t	*ipx = ipsq->ipsq_xop;
793 
794 	*connpp = NULL;
795 	mutex_enter(&ipx->ipx_lock);
796 	if (ipx->ipx_pending_mp == NULL) {
797 		mutex_exit(&ipx->ipx_lock);
798 		return (NULL);
799 	}
800 
801 	/* There can be only 1 such excl message */
802 	curr = ipx->ipx_pending_mp;
803 	ASSERT(curr->b_next == NULL);
804 	ipx->ipx_pending_ipif = NULL;
805 	ipx->ipx_pending_mp = NULL;
806 	ipx->ipx_waitfor = 0;
807 	mutex_exit(&ipx->ipx_lock);
808 
809 	if (CONN_Q(curr->b_queue)) {
810 		/*
811 		 * This mp did a refhold on the conn, at the start of the ioctl.
812 		 * So we can safely return a pointer to the conn to the caller.
813 		 */
814 		*connpp = Q_TO_CONN(curr->b_queue);
815 	} else {
816 		*connpp = NULL;
817 	}
818 	curr->b_next = NULL;
819 	curr->b_prev = NULL;
820 	return (curr);
821 }
822 
823 /*
824  * Cleanup the ioctl mp queued in ipx_pending_mp
825  * - Called in the ill_delete path
826  * - Called in the M_ERROR or M_HANGUP path on the ill.
827  * - Called in the conn close path.
828  *
829  * Returns success on finding the pending mblk associated with the ioctl or
830  * exclusive operation in progress, failure otherwise.
831  */
832 boolean_t
833 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
834 {
835 	mblk_t	*mp;
836 	ipxop_t	*ipx;
837 	queue_t	*q;
838 	ipif_t	*ipif;
839 	int	cmd;
840 
841 	ASSERT(IAM_WRITER_ILL(ill));
842 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
843 
844 	mutex_enter(&ipx->ipx_lock);
845 	mp = ipx->ipx_pending_mp;
846 	if (connp != NULL) {
847 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
848 			/*
849 			 * Nothing to clean since the conn that is closing
850 			 * does not have a matching pending mblk in
851 			 * ipx_pending_mp.
852 			 */
853 			mutex_exit(&ipx->ipx_lock);
854 			return (B_FALSE);
855 		}
856 	} else {
857 		/*
858 		 * A non-zero ill_error signifies we are called in the
859 		 * M_ERROR or M_HANGUP path and we need to unconditionally
860 		 * abort any current ioctl and do the corresponding cleanup.
861 		 * A zero ill_error means we are in the ill_delete path and
862 		 * we do the cleanup only if there is a pending mp.
863 		 */
864 		if (mp == NULL && ill->ill_error == 0) {
865 			mutex_exit(&ipx->ipx_lock);
866 			return (B_FALSE);
867 		}
868 	}
869 
870 	/* Now remove from the ipx_pending_mp */
871 	ipx->ipx_pending_mp = NULL;
872 	ipif = ipx->ipx_pending_ipif;
873 	ipx->ipx_pending_ipif = NULL;
874 	ipx->ipx_waitfor = 0;
875 	ipx->ipx_current_ipif = NULL;
876 	cmd = ipx->ipx_current_ioctl;
877 	ipx->ipx_current_ioctl = 0;
878 	ipx->ipx_current_done = B_TRUE;
879 	mutex_exit(&ipx->ipx_lock);
880 
881 	if (mp == NULL)
882 		return (B_FALSE);
883 
884 	q = mp->b_queue;
885 	mp->b_next = NULL;
886 	mp->b_prev = NULL;
887 	mp->b_queue = NULL;
888 
889 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
890 		DTRACE_PROBE4(ipif__ioctl,
891 		    char *, "ipsq_pending_mp_cleanup",
892 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
893 		    ipif_t *, ipif);
894 		if (connp == NULL) {
895 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
896 		} else {
897 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
898 			mutex_enter(&ipif->ipif_ill->ill_lock);
899 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
900 			mutex_exit(&ipif->ipif_ill->ill_lock);
901 		}
902 	} else {
903 		inet_freemsg(mp);
904 	}
905 	return (B_TRUE);
906 }
907 
908 /*
909  * Called in the conn close path and ill delete path
910  */
911 static void
912 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
913 {
914 	ipsq_t	*ipsq;
915 	mblk_t	*prev;
916 	mblk_t	*curr;
917 	mblk_t	*next;
918 	queue_t	*wq, *rq = NULL;
919 	mblk_t	*tmp_list = NULL;
920 
921 	ASSERT(IAM_WRITER_ILL(ill));
922 	if (connp != NULL)
923 		wq = CONNP_TO_WQ(connp);
924 	else
925 		wq = ill->ill_wq;
926 
927 	/*
928 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
929 	 * against this here.
930 	 */
931 	if (wq != NULL)
932 		rq = RD(wq);
933 
934 	ipsq = ill->ill_phyint->phyint_ipsq;
935 	/*
936 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
937 	 * In the case of ioctl from a conn, there can be only 1 mp
938 	 * queued on the ipsq. If an ill is being unplumbed flush all
939 	 * the messages.
940 	 */
941 	mutex_enter(&ipsq->ipsq_lock);
942 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
943 	    curr = next) {
944 		next = curr->b_next;
945 		if (connp == NULL ||
946 		    (curr->b_queue == wq || curr->b_queue == rq)) {
947 			/* Unlink the mblk from the pending mp list */
948 			if (prev != NULL) {
949 				prev->b_next = curr->b_next;
950 			} else {
951 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
952 				ipsq->ipsq_xopq_mphead = curr->b_next;
953 			}
954 			if (ipsq->ipsq_xopq_mptail == curr)
955 				ipsq->ipsq_xopq_mptail = prev;
956 			/*
957 			 * Create a temporary list and release the ipsq lock
958 			 * New elements are added to the head of the tmp_list
959 			 */
960 			curr->b_next = tmp_list;
961 			tmp_list = curr;
962 		} else {
963 			prev = curr;
964 		}
965 	}
966 	mutex_exit(&ipsq->ipsq_lock);
967 
968 	while (tmp_list != NULL) {
969 		curr = tmp_list;
970 		tmp_list = curr->b_next;
971 		curr->b_next = NULL;
972 		curr->b_prev = NULL;
973 		wq = curr->b_queue;
974 		curr->b_queue = NULL;
975 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
976 			DTRACE_PROBE4(ipif__ioctl,
977 			    char *, "ipsq_xopq_mp_cleanup",
978 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
979 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
980 			    CONN_CLOSE : NO_COPYOUT, NULL);
981 		} else {
982 			/*
983 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
984 			 * this can't be just inet_freemsg. we have to
985 			 * restart it otherwise the thread will be stuck.
986 			 */
987 			inet_freemsg(curr);
988 		}
989 	}
990 }
991 
992 /*
993  * This conn has started closing. Cleanup any pending ioctl from this conn.
994  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
995  */
996 void
997 conn_ioctl_cleanup(conn_t *connp)
998 {
999 	ipsq_t	*ipsq;
1000 	ill_t	*ill;
1001 	boolean_t refheld;
1002 
1003 	/*
1004 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1005 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1006 	 * started the mp could be present in ipx_pending_mp. Note that if
1007 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1008 	 * not yet queued anywhere. In this case, the conn close code will wait
1009 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1010 	 * tcp_close will wait first until all ioctls have completed for this
1011 	 * conn.
1012 	 */
1013 	mutex_enter(&connp->conn_lock);
1014 	ill = connp->conn_oper_pending_ill;
1015 	if (ill == NULL) {
1016 		mutex_exit(&connp->conn_lock);
1017 		return;
1018 	}
1019 
1020 	/*
1021 	 * We may not be able to refhold the ill if the ill/ipif
1022 	 * is changing. But we need to make sure that the ill will
1023 	 * not vanish. So we just bump up the ill_waiter count.
1024 	 */
1025 	refheld = ill_waiter_inc(ill);
1026 	mutex_exit(&connp->conn_lock);
1027 	if (refheld) {
1028 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1029 			ill_waiter_dcr(ill);
1030 			/*
1031 			 * Check whether this ioctl has started and is
1032 			 * pending. If it is not found there then check
1033 			 * whether this ioctl has not even started and is in
1034 			 * the ipsq_xopq list.
1035 			 */
1036 			if (!ipsq_pending_mp_cleanup(ill, connp))
1037 				ipsq_xopq_mp_cleanup(ill, connp);
1038 			ipsq = ill->ill_phyint->phyint_ipsq;
1039 			ipsq_exit(ipsq);
1040 			return;
1041 		}
1042 	}
1043 
1044 	/*
1045 	 * The ill is also closing and we could not bump up the
1046 	 * ill_waiter_count or we could not enter the ipsq. Leave
1047 	 * the cleanup to ill_delete
1048 	 */
1049 	mutex_enter(&connp->conn_lock);
1050 	while (connp->conn_oper_pending_ill != NULL)
1051 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1052 	mutex_exit(&connp->conn_lock);
1053 	if (refheld)
1054 		ill_waiter_dcr(ill);
1055 }
1056 
1057 /*
1058  * ipcl_walk function for cleaning up conn_*_ill fields.
1059  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1060  * conn_bound_if in place. We prefer dropping
1061  * packets instead of sending them out the wrong interface, or accepting
1062  * packets from the wrong ifindex.
1063  */
1064 static void
1065 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1066 {
1067 	ill_t	*ill = (ill_t *)arg;
1068 
1069 	mutex_enter(&connp->conn_lock);
1070 	if (connp->conn_dhcpinit_ill == ill) {
1071 		connp->conn_dhcpinit_ill = NULL;
1072 		ASSERT(ill->ill_dhcpinit != 0);
1073 		atomic_dec_32(&ill->ill_dhcpinit);
1074 		ill_set_inputfn(ill);
1075 	}
1076 	mutex_exit(&connp->conn_lock);
1077 }
1078 
1079 static int
1080 ill_down_ipifs_tail(ill_t *ill)
1081 {
1082 	ipif_t	*ipif;
1083 	int err;
1084 
1085 	ASSERT(IAM_WRITER_ILL(ill));
1086 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1087 		ipif_non_duplicate(ipif);
1088 		/*
1089 		 * ipif_down_tail will call arp_ll_down on the last ipif
1090 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1091 		 */
1092 		if ((err = ipif_down_tail(ipif)) != 0)
1093 			return (err);
1094 	}
1095 	return (0);
1096 }
1097 
1098 /* ARGSUSED */
1099 void
1100 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1101 {
1102 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1103 	(void) ill_down_ipifs_tail(q->q_ptr);
1104 	freemsg(mp);
1105 	ipsq_current_finish(ipsq);
1106 }
1107 
1108 /*
1109  * ill_down_start is called when we want to down this ill and bring it up again
1110  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1111  * all interfaces, but don't tear down any plumbing.
1112  */
1113 boolean_t
1114 ill_down_start(queue_t *q, mblk_t *mp)
1115 {
1116 	ill_t	*ill = q->q_ptr;
1117 	ipif_t	*ipif;
1118 
1119 	ASSERT(IAM_WRITER_ILL(ill));
1120 	/*
1121 	 * It is possible that some ioctl is already in progress while we
1122 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1123 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1124 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1125 	 * the in progress ioctl from ever completing.
1126 	 *
1127 	 * The thread that started the ioctl (if any) must have returned,
1128 	 * since we are now executing as writer. After the 2 calls below,
1129 	 * the state of the ipsq and the ill would reflect no trace of any
1130 	 * pending operation. Subsequently if there is any response to the
1131 	 * original ioctl from the driver, it would be discarded as an
1132 	 * unsolicited message from the driver.
1133 	 */
1134 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1135 	ill_dlpi_clear_deferred(ill);
1136 
1137 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1138 		(void) ipif_down(ipif, NULL, NULL);
1139 
1140 	ill_down(ill);
1141 
1142 	/*
1143 	 * Walk all CONNs that can have a reference on an ire or nce for this
1144 	 * ill (we actually walk all that now have stale references).
1145 	 */
1146 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1147 
1148 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1149 	if (ill->ill_isv6)
1150 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1151 
1152 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1153 
1154 	/*
1155 	 * Atomically test and add the pending mp if references are active.
1156 	 */
1157 	mutex_enter(&ill->ill_lock);
1158 	if (!ill_is_quiescent(ill)) {
1159 		/* call cannot fail since `conn_t *' argument is NULL */
1160 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1161 		    mp, ILL_DOWN);
1162 		mutex_exit(&ill->ill_lock);
1163 		return (B_FALSE);
1164 	}
1165 	mutex_exit(&ill->ill_lock);
1166 	return (B_TRUE);
1167 }
1168 
1169 static void
1170 ill_down(ill_t *ill)
1171 {
1172 	mblk_t	*mp;
1173 	ip_stack_t	*ipst = ill->ill_ipst;
1174 
1175 	/*
1176 	 * Blow off any IREs dependent on this ILL.
1177 	 * The caller needs to handle conn_ixa_cleanup
1178 	 */
1179 	ill_delete_ires(ill);
1180 
1181 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1182 
1183 	/* Remove any conn_*_ill depending on this ill */
1184 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1185 
1186 	/*
1187 	 * Free state for additional IREs.
1188 	 */
1189 	mutex_enter(&ill->ill_saved_ire_lock);
1190 	mp = ill->ill_saved_ire_mp;
1191 	ill->ill_saved_ire_mp = NULL;
1192 	ill->ill_saved_ire_cnt = 0;
1193 	mutex_exit(&ill->ill_saved_ire_lock);
1194 	freemsg(mp);
1195 }
1196 
1197 /*
1198  * ire_walk routine used to delete every IRE that depends on
1199  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1200  *
1201  * Note: since the routes added by the kernel are deleted separately,
1202  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1203  *
1204  * We also remove references on ire_nce_cache entries that refer to the ill.
1205  */
1206 void
1207 ill_downi(ire_t *ire, char *ill_arg)
1208 {
1209 	ill_t	*ill = (ill_t *)ill_arg;
1210 	nce_t	*nce;
1211 
1212 	mutex_enter(&ire->ire_lock);
1213 	nce = ire->ire_nce_cache;
1214 	if (nce != NULL && nce->nce_ill == ill)
1215 		ire->ire_nce_cache = NULL;
1216 	else
1217 		nce = NULL;
1218 	mutex_exit(&ire->ire_lock);
1219 	if (nce != NULL)
1220 		nce_refrele(nce);
1221 	if (ire->ire_ill == ill) {
1222 		/*
1223 		 * The existing interface binding for ire must be
1224 		 * deleted before trying to bind the route to another
1225 		 * interface. However, since we are using the contents of the
1226 		 * ire after ire_delete, the caller has to ensure that
1227 		 * CONDEMNED (deleted) ire's are not removed from the list
1228 		 * when ire_delete() returns. Currently ill_downi() is
1229 		 * only called as part of ire_walk*() routines, so that
1230 		 * the irb_refhold() done by ire_walk*() will ensure that
1231 		 * ire_delete() does not lead to ire_inactive().
1232 		 */
1233 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1234 		ire_delete(ire);
1235 		if (ire->ire_unbound)
1236 			ire_rebind(ire);
1237 	}
1238 }
1239 
1240 /* Remove IRE_IF_CLONE on this ill */
1241 void
1242 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1243 {
1244 	ill_t	*ill = (ill_t *)ill_arg;
1245 
1246 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1247 	if (ire->ire_ill == ill)
1248 		ire_delete(ire);
1249 }
1250 
1251 /* Consume an M_IOCACK of the fastpath probe. */
1252 void
1253 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1254 {
1255 	mblk_t	*mp1 = mp;
1256 
1257 	/*
1258 	 * If this was the first attempt turn on the fastpath probing.
1259 	 */
1260 	mutex_enter(&ill->ill_lock);
1261 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1262 		ill->ill_dlpi_fastpath_state = IDS_OK;
1263 	mutex_exit(&ill->ill_lock);
1264 
1265 	/* Free the M_IOCACK mblk, hold on to the data */
1266 	mp = mp->b_cont;
1267 	freeb(mp1);
1268 	if (mp == NULL)
1269 		return;
1270 	if (mp->b_cont != NULL)
1271 		nce_fastpath_update(ill, mp);
1272 	else
1273 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1274 	freemsg(mp);
1275 }
1276 
1277 /*
1278  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1279  * The data portion of the request is a dl_unitdata_req_t template for
1280  * what we would send downstream in the absence of a fastpath confirmation.
1281  */
1282 int
1283 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1284 {
1285 	struct iocblk	*ioc;
1286 	mblk_t	*mp;
1287 
1288 	if (dlur_mp == NULL)
1289 		return (EINVAL);
1290 
1291 	mutex_enter(&ill->ill_lock);
1292 	switch (ill->ill_dlpi_fastpath_state) {
1293 	case IDS_FAILED:
1294 		/*
1295 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1296 		 * support it.
1297 		 */
1298 		mutex_exit(&ill->ill_lock);
1299 		return (ENOTSUP);
1300 	case IDS_UNKNOWN:
1301 		/* This is the first probe */
1302 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1303 		break;
1304 	default:
1305 		break;
1306 	}
1307 	mutex_exit(&ill->ill_lock);
1308 
1309 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1310 		return (EAGAIN);
1311 
1312 	mp->b_cont = copyb(dlur_mp);
1313 	if (mp->b_cont == NULL) {
1314 		freeb(mp);
1315 		return (EAGAIN);
1316 	}
1317 
1318 	ioc = (struct iocblk *)mp->b_rptr;
1319 	ioc->ioc_count = msgdsize(mp->b_cont);
1320 
1321 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1322 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1323 	putnext(ill->ill_wq, mp);
1324 	return (0);
1325 }
1326 
1327 void
1328 ill_capability_probe(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 
1332 	ASSERT(IAM_WRITER_ILL(ill));
1333 
1334 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1335 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1336 		return;
1337 
1338 	/*
1339 	 * We are starting a new cycle of capability negotiation.
1340 	 * Free up the capab reset messages of any previous incarnation.
1341 	 * We will do a fresh allocation when we get the response to our probe
1342 	 */
1343 	if (ill->ill_capab_reset_mp != NULL) {
1344 		freemsg(ill->ill_capab_reset_mp);
1345 		ill->ill_capab_reset_mp = NULL;
1346 	}
1347 
1348 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1349 
1350 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1351 	if (mp == NULL)
1352 		return;
1353 
1354 	ill_capability_send(ill, mp);
1355 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1356 }
1357 
1358 void
1359 ill_capability_reset(ill_t *ill, boolean_t reneg)
1360 {
1361 	ASSERT(IAM_WRITER_ILL(ill));
1362 
1363 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1364 		return;
1365 
1366 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1367 
1368 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1369 	ill->ill_capab_reset_mp = NULL;
1370 	/*
1371 	 * We turn off all capabilities except those pertaining to
1372 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1373 	 * which will be turned off by the corresponding reset functions.
1374 	 */
1375 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1376 }
1377 
1378 static void
1379 ill_capability_reset_alloc(ill_t *ill)
1380 {
1381 	mblk_t *mp;
1382 	size_t	size = 0;
1383 	int	err;
1384 	dl_capability_req_t	*capb;
1385 
1386 	ASSERT(IAM_WRITER_ILL(ill));
1387 	ASSERT(ill->ill_capab_reset_mp == NULL);
1388 
1389 	if (ILL_HCKSUM_CAPABLE(ill)) {
1390 		size += sizeof (dl_capability_sub_t) +
1391 		    sizeof (dl_capab_hcksum_t);
1392 	}
1393 
1394 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1395 		size += sizeof (dl_capability_sub_t) +
1396 		    sizeof (dl_capab_zerocopy_t);
1397 	}
1398 
1399 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1400 		size += sizeof (dl_capability_sub_t) +
1401 		    sizeof (dl_capab_dld_t);
1402 	}
1403 
1404 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1405 	    STR_NOSIG, &err);
1406 
1407 	mp->b_datap->db_type = M_PROTO;
1408 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1409 
1410 	capb = (dl_capability_req_t *)mp->b_rptr;
1411 	capb->dl_primitive = DL_CAPABILITY_REQ;
1412 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1413 	capb->dl_sub_length = size;
1414 
1415 	mp->b_wptr += sizeof (dl_capability_req_t);
1416 
1417 	/*
1418 	 * Each handler fills in the corresponding dl_capability_sub_t
1419 	 * inside the mblk,
1420 	 */
1421 	ill_capability_hcksum_reset_fill(ill, mp);
1422 	ill_capability_zerocopy_reset_fill(ill, mp);
1423 	ill_capability_dld_reset_fill(ill, mp);
1424 
1425 	ill->ill_capab_reset_mp = mp;
1426 }
1427 
1428 static void
1429 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1430 {
1431 	dl_capab_id_t *id_ic;
1432 	uint_t sub_dl_cap = outers->dl_cap;
1433 	dl_capability_sub_t *inners;
1434 	uint8_t *capend;
1435 
1436 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1437 
1438 	/*
1439 	 * Note: range checks here are not absolutely sufficient to
1440 	 * make us robust against malformed messages sent by drivers;
1441 	 * this is in keeping with the rest of IP's dlpi handling.
1442 	 * (Remember, it's coming from something else in the kernel
1443 	 * address space)
1444 	 */
1445 
1446 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1447 	if (capend > mp->b_wptr) {
1448 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1449 		    "malformed sub-capability too long for mblk");
1450 		return;
1451 	}
1452 
1453 	id_ic = (dl_capab_id_t *)(outers + 1);
1454 
1455 	if (outers->dl_length < sizeof (*id_ic) ||
1456 	    (inners = &id_ic->id_subcap,
1457 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1458 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1459 		    "encapsulated capab type %d too long for mblk",
1460 		    inners->dl_cap);
1461 		return;
1462 	}
1463 
1464 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1465 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1466 		    "isn't as expected; pass-thru module(s) detected, "
1467 		    "discarding capability\n", inners->dl_cap));
1468 		return;
1469 	}
1470 
1471 	/* Process the encapsulated sub-capability */
1472 	ill_capability_dispatch(ill, mp, inners);
1473 }
1474 
1475 static void
1476 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1477 {
1478 	dl_capability_sub_t *dl_subcap;
1479 
1480 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1481 		return;
1482 
1483 	/*
1484 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1485 	 * initialized below since it is not used by DLD.
1486 	 */
1487 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1488 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1489 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1490 
1491 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1492 }
1493 
1494 static void
1495 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1496 {
1497 	/*
1498 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1499 	 * is only to get the VRRP capability.
1500 	 *
1501 	 * Note that we cannot check ill_ipif_up_count here since
1502 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1503 	 * That is done asynchronously, and can race with this function.
1504 	 */
1505 	if (!ill->ill_dl_up) {
1506 		if (subp->dl_cap == DL_CAPAB_VRRP)
1507 			ill_capability_vrrp_ack(ill, mp, subp);
1508 		return;
1509 	}
1510 
1511 	switch (subp->dl_cap) {
1512 	case DL_CAPAB_HCKSUM:
1513 		ill_capability_hcksum_ack(ill, mp, subp);
1514 		break;
1515 	case DL_CAPAB_ZEROCOPY:
1516 		ill_capability_zerocopy_ack(ill, mp, subp);
1517 		break;
1518 	case DL_CAPAB_DLD:
1519 		ill_capability_dld_ack(ill, mp, subp);
1520 		break;
1521 	case DL_CAPAB_VRRP:
1522 		break;
1523 	default:
1524 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1525 		    subp->dl_cap));
1526 	}
1527 }
1528 
1529 /*
1530  * Process the vrrp capability received from a DLS Provider. isub must point
1531  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1532  */
1533 static void
1534 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1535 {
1536 	dl_capab_vrrp_t	*vrrp;
1537 	uint_t		sub_dl_cap = isub->dl_cap;
1538 	uint8_t		*capend;
1539 
1540 	ASSERT(IAM_WRITER_ILL(ill));
1541 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1542 
1543 	/*
1544 	 * Note: range checks here are not absolutely sufficient to
1545 	 * make us robust against malformed messages sent by drivers;
1546 	 * this is in keeping with the rest of IP's dlpi handling.
1547 	 * (Remember, it's coming from something else in the kernel
1548 	 * address space)
1549 	 */
1550 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1551 	if (capend > mp->b_wptr) {
1552 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1553 		    "malformed sub-capability too long for mblk");
1554 		return;
1555 	}
1556 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1557 
1558 	/*
1559 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1560 	 */
1561 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1562 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1563 		ill->ill_flags |= ILLF_VRRP;
1564 	}
1565 }
1566 
1567 /*
1568  * Process a hardware checksum offload capability negotiation ack received
1569  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1570  * of a DL_CAPABILITY_ACK message.
1571  */
1572 static void
1573 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1574 {
1575 	dl_capability_req_t	*ocap;
1576 	dl_capab_hcksum_t	*ihck, *ohck;
1577 	ill_hcksum_capab_t	**ill_hcksum;
1578 	mblk_t			*nmp = NULL;
1579 	uint_t			sub_dl_cap = isub->dl_cap;
1580 	uint8_t			*capend;
1581 
1582 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1583 
1584 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1585 
1586 	/*
1587 	 * Note: range checks here are not absolutely sufficient to
1588 	 * make us robust against malformed messages sent by drivers;
1589 	 * this is in keeping with the rest of IP's dlpi handling.
1590 	 * (Remember, it's coming from something else in the kernel
1591 	 * address space)
1592 	 */
1593 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1594 	if (capend > mp->b_wptr) {
1595 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1596 		    "malformed sub-capability too long for mblk");
1597 		return;
1598 	}
1599 
1600 	/*
1601 	 * There are two types of acks we process here:
1602 	 * 1. acks in reply to a (first form) generic capability req
1603 	 *    (no ENABLE flag set)
1604 	 * 2. acks in reply to a ENABLE capability req.
1605 	 *    (ENABLE flag set)
1606 	 */
1607 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1608 
1609 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1610 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1611 		    "unsupported hardware checksum "
1612 		    "sub-capability (version %d, expected %d)",
1613 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1614 		return;
1615 	}
1616 
1617 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1618 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1619 		    "checksum capability isn't as expected; pass-thru "
1620 		    "module(s) detected, discarding capability\n"));
1621 		return;
1622 	}
1623 
1624 #define	CURR_HCKSUM_CAPAB				\
1625 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1626 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1627 
1628 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1629 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1630 		/* do ENABLE processing */
1631 		if (*ill_hcksum == NULL) {
1632 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1633 			    KM_NOSLEEP);
1634 
1635 			if (*ill_hcksum == NULL) {
1636 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1637 				    "could not enable hcksum version %d "
1638 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1639 				    ill->ill_name);
1640 				return;
1641 			}
1642 		}
1643 
1644 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1645 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1646 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1647 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1648 		    "has enabled hardware checksumming\n ",
1649 		    ill->ill_name));
1650 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1651 		/*
1652 		 * Enabling hardware checksum offload
1653 		 * Currently IP supports {TCP,UDP}/IPv4
1654 		 * partial and full cksum offload and
1655 		 * IPv4 header checksum offload.
1656 		 * Allocate new mblk which will
1657 		 * contain a new capability request
1658 		 * to enable hardware checksum offload.
1659 		 */
1660 		uint_t	size;
1661 		uchar_t	*rptr;
1662 
1663 		size = sizeof (dl_capability_req_t) +
1664 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1665 
1666 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1667 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1668 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1669 			    ill->ill_name);
1670 			return;
1671 		}
1672 
1673 		rptr = nmp->b_rptr;
1674 		/* initialize dl_capability_req_t */
1675 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1676 		ocap->dl_sub_offset =
1677 		    sizeof (dl_capability_req_t);
1678 		ocap->dl_sub_length =
1679 		    sizeof (dl_capability_sub_t) +
1680 		    isub->dl_length;
1681 		nmp->b_rptr += sizeof (dl_capability_req_t);
1682 
1683 		/* initialize dl_capability_sub_t */
1684 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1685 		nmp->b_rptr += sizeof (*isub);
1686 
1687 		/* initialize dl_capab_hcksum_t */
1688 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1689 		bcopy(ihck, ohck, sizeof (*ihck));
1690 
1691 		nmp->b_rptr = rptr;
1692 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1693 
1694 		/* Set ENABLE flag */
1695 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1696 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1697 
1698 		/*
1699 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1700 		 * hardware checksum acceleration.
1701 		 */
1702 		ill_capability_send(ill, nmp);
1703 	} else {
1704 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1705 		    "advertised %x hardware checksum capability flags\n",
1706 		    ill->ill_name, ihck->hcksum_txflags));
1707 	}
1708 }
1709 
1710 static void
1711 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1712 {
1713 	dl_capab_hcksum_t *hck_subcap;
1714 	dl_capability_sub_t *dl_subcap;
1715 
1716 	if (!ILL_HCKSUM_CAPABLE(ill))
1717 		return;
1718 
1719 	ASSERT(ill->ill_hcksum_capab != NULL);
1720 
1721 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1722 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1723 	dl_subcap->dl_length = sizeof (*hck_subcap);
1724 
1725 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1726 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1727 	hck_subcap->hcksum_txflags = 0;
1728 
1729 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1730 }
1731 
1732 static void
1733 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1734 {
1735 	mblk_t *nmp = NULL;
1736 	dl_capability_req_t *oc;
1737 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1738 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1739 	uint_t sub_dl_cap = isub->dl_cap;
1740 	uint8_t *capend;
1741 
1742 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1743 
1744 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1745 
1746 	/*
1747 	 * Note: range checks here are not absolutely sufficient to
1748 	 * make us robust against malformed messages sent by drivers;
1749 	 * this is in keeping with the rest of IP's dlpi handling.
1750 	 * (Remember, it's coming from something else in the kernel
1751 	 * address space)
1752 	 */
1753 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1754 	if (capend > mp->b_wptr) {
1755 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1756 		    "malformed sub-capability too long for mblk");
1757 		return;
1758 	}
1759 
1760 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1761 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1762 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1763 		    "unsupported ZEROCOPY sub-capability (version %d, "
1764 		    "expected %d)", zc_ic->zerocopy_version,
1765 		    ZEROCOPY_VERSION_1);
1766 		return;
1767 	}
1768 
1769 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1770 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1771 		    "capability isn't as expected; pass-thru module(s) "
1772 		    "detected, discarding capability\n"));
1773 		return;
1774 	}
1775 
1776 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1777 		if (*ill_zerocopy_capab == NULL) {
1778 			*ill_zerocopy_capab =
1779 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1780 			    KM_NOSLEEP);
1781 
1782 			if (*ill_zerocopy_capab == NULL) {
1783 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1784 				    "could not enable Zero-copy version %d "
1785 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1786 				    ill->ill_name);
1787 				return;
1788 			}
1789 		}
1790 
1791 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1792 		    "supports Zero-copy version %d\n", ill->ill_name,
1793 		    ZEROCOPY_VERSION_1));
1794 
1795 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1796 		    zc_ic->zerocopy_version;
1797 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1798 		    zc_ic->zerocopy_flags;
1799 
1800 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1801 	} else {
1802 		uint_t size;
1803 		uchar_t *rptr;
1804 
1805 		size = sizeof (dl_capability_req_t) +
1806 		    sizeof (dl_capability_sub_t) +
1807 		    sizeof (dl_capab_zerocopy_t);
1808 
1809 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1810 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1811 			    "could not enable zerocopy for %s (ENOMEM)\n",
1812 			    ill->ill_name);
1813 			return;
1814 		}
1815 
1816 		rptr = nmp->b_rptr;
1817 		/* initialize dl_capability_req_t */
1818 		oc = (dl_capability_req_t *)rptr;
1819 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1820 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1821 		    sizeof (dl_capab_zerocopy_t);
1822 		rptr += sizeof (dl_capability_req_t);
1823 
1824 		/* initialize dl_capability_sub_t */
1825 		bcopy(isub, rptr, sizeof (*isub));
1826 		rptr += sizeof (*isub);
1827 
1828 		/* initialize dl_capab_zerocopy_t */
1829 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1830 		*zc_oc = *zc_ic;
1831 
1832 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1833 		    "to enable zero-copy version %d\n", ill->ill_name,
1834 		    ZEROCOPY_VERSION_1));
1835 
1836 		/* set VMSAFE_MEM flag */
1837 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1838 
1839 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1840 		ill_capability_send(ill, nmp);
1841 	}
1842 }
1843 
1844 static void
1845 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1846 {
1847 	dl_capab_zerocopy_t *zerocopy_subcap;
1848 	dl_capability_sub_t *dl_subcap;
1849 
1850 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1851 		return;
1852 
1853 	ASSERT(ill->ill_zerocopy_capab != NULL);
1854 
1855 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1856 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1857 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1858 
1859 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1860 	zerocopy_subcap->zerocopy_version =
1861 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1862 	zerocopy_subcap->zerocopy_flags = 0;
1863 
1864 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1865 }
1866 
1867 /*
1868  * DLD capability
1869  * Refer to dld.h for more information regarding the purpose and usage
1870  * of this capability.
1871  */
1872 static void
1873 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1874 {
1875 	dl_capab_dld_t		*dld_ic, dld;
1876 	uint_t			sub_dl_cap = isub->dl_cap;
1877 	uint8_t			*capend;
1878 	ill_dld_capab_t		*idc;
1879 
1880 	ASSERT(IAM_WRITER_ILL(ill));
1881 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1882 
1883 	/*
1884 	 * Note: range checks here are not absolutely sufficient to
1885 	 * make us robust against malformed messages sent by drivers;
1886 	 * this is in keeping with the rest of IP's dlpi handling.
1887 	 * (Remember, it's coming from something else in the kernel
1888 	 * address space)
1889 	 */
1890 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1891 	if (capend > mp->b_wptr) {
1892 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1893 		    "malformed sub-capability too long for mblk");
1894 		return;
1895 	}
1896 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1897 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1898 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1899 		    "unsupported DLD sub-capability (version %d, "
1900 		    "expected %d)", dld_ic->dld_version,
1901 		    DLD_CURRENT_VERSION);
1902 		return;
1903 	}
1904 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1905 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1906 		    "capability isn't as expected; pass-thru module(s) "
1907 		    "detected, discarding capability\n"));
1908 		return;
1909 	}
1910 
1911 	/*
1912 	 * Copy locally to ensure alignment.
1913 	 */
1914 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1915 
1916 	if ((idc = ill->ill_dld_capab) == NULL) {
1917 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1918 		if (idc == NULL) {
1919 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1920 			    "could not enable DLD version %d "
1921 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1922 			    ill->ill_name);
1923 			return;
1924 		}
1925 		ill->ill_dld_capab = idc;
1926 	}
1927 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1928 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1929 	ip1dbg(("ill_capability_dld_ack: interface %s "
1930 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1931 
1932 	ill_capability_dld_enable(ill);
1933 }
1934 
1935 /*
1936  * Typically capability negotiation between IP and the driver happens via
1937  * DLPI message exchange. However GLD also offers a direct function call
1938  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1939  * But arbitrary function calls into IP or GLD are not permitted, since both
1940  * of them are protected by their own perimeter mechanism. The perimeter can
1941  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1942  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1943  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1944  * to enter the mac perimeter and then do the direct function calls into
1945  * GLD to enable squeue polling. The ring related callbacks from the mac into
1946  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1947  * protected by the mac perimeter.
1948  */
1949 static void
1950 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1951 {
1952 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1953 	int			err;
1954 
1955 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1956 	    DLD_ENABLE);
1957 	ASSERT(err == 0);
1958 }
1959 
1960 static void
1961 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1962 {
1963 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1964 	int			err;
1965 
1966 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1967 	    DLD_DISABLE);
1968 	ASSERT(err == 0);
1969 }
1970 
1971 boolean_t
1972 ill_mac_perim_held(ill_t *ill)
1973 {
1974 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1975 
1976 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1977 	    DLD_QUERY));
1978 }
1979 
1980 static void
1981 ill_capability_direct_enable(ill_t *ill)
1982 {
1983 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1984 	ill_dld_direct_t	*idd = &idc->idc_direct;
1985 	dld_capab_direct_t	direct;
1986 	int			rc;
1987 
1988 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1989 
1990 	bzero(&direct, sizeof (direct));
1991 	direct.di_rx_cf = (uintptr_t)ip_input;
1992 	direct.di_rx_ch = ill;
1993 
1994 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1995 	    DLD_ENABLE);
1996 	if (rc == 0) {
1997 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1998 		idd->idd_tx_dh = direct.di_tx_dh;
1999 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2000 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2001 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2002 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2003 		ASSERT(idd->idd_tx_cb_df != NULL);
2004 		ASSERT(idd->idd_tx_fctl_df != NULL);
2005 		ASSERT(idd->idd_tx_df != NULL);
2006 		/*
2007 		 * One time registration of flow enable callback function
2008 		 */
2009 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2010 		    ill_flow_enable, ill);
2011 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2012 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2013 	} else {
2014 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2015 		    "capability, rc = %d\n", rc);
2016 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_poll_enable(ill_t *ill)
2022 {
2023 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2024 	dld_capab_poll_t	poll;
2025 	int			rc;
2026 
2027 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2028 
2029 	bzero(&poll, sizeof (poll));
2030 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2031 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2032 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2033 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2034 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2035 	poll.poll_ring_ch = ill;
2036 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2037 	    DLD_ENABLE);
2038 	if (rc == 0) {
2039 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2040 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2041 	} else {
2042 		ip1dbg(("warning: could not enable POLL "
2043 		    "capability, rc = %d\n", rc));
2044 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2045 	}
2046 }
2047 
2048 /*
2049  * Enable the LSO capability.
2050  */
2051 static void
2052 ill_capability_lso_enable(ill_t *ill)
2053 {
2054 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2055 	dld_capab_lso_t	lso;
2056 	int rc;
2057 
2058 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2059 
2060 	if (ill->ill_lso_capab == NULL) {
2061 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2062 		    KM_NOSLEEP);
2063 		if (ill->ill_lso_capab == NULL) {
2064 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2065 			    "could not enable LSO for %s (ENOMEM)\n",
2066 			    ill->ill_name);
2067 			return;
2068 		}
2069 	}
2070 
2071 	bzero(&lso, sizeof (lso));
2072 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2073 	    DLD_ENABLE)) == 0) {
2074 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2075 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2076 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2077 		ip1dbg(("ill_capability_lso_enable: interface %s "
2078 		    "has enabled LSO\n ", ill->ill_name));
2079 	} else {
2080 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2081 		ill->ill_lso_capab = NULL;
2082 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2083 	}
2084 }
2085 
2086 static void
2087 ill_capability_dld_enable(ill_t *ill)
2088 {
2089 	mac_perim_handle_t mph;
2090 
2091 	ASSERT(IAM_WRITER_ILL(ill));
2092 
2093 	if (ill->ill_isv6)
2094 		return;
2095 
2096 	ill_mac_perim_enter(ill, &mph);
2097 	if (!ill->ill_isv6) {
2098 		ill_capability_direct_enable(ill);
2099 		ill_capability_poll_enable(ill);
2100 		ill_capability_lso_enable(ill);
2101 	}
2102 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2103 	ill_mac_perim_exit(ill, mph);
2104 }
2105 
2106 static void
2107 ill_capability_dld_disable(ill_t *ill)
2108 {
2109 	ill_dld_capab_t	*idc;
2110 	ill_dld_direct_t *idd;
2111 	mac_perim_handle_t	mph;
2112 
2113 	ASSERT(IAM_WRITER_ILL(ill));
2114 
2115 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2116 		return;
2117 
2118 	ill_mac_perim_enter(ill, &mph);
2119 
2120 	idc = ill->ill_dld_capab;
2121 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2122 		/*
2123 		 * For performance we avoid locks in the transmit data path
2124 		 * and don't maintain a count of the number of threads using
2125 		 * direct calls. Thus some threads could be using direct
2126 		 * transmit calls to GLD, even after the capability mechanism
2127 		 * turns it off. This is still safe since the handles used in
2128 		 * the direct calls continue to be valid until the unplumb is
2129 		 * completed. Remove the callback that was added (1-time) at
2130 		 * capab enable time.
2131 		 */
2132 		mutex_enter(&ill->ill_lock);
2133 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2134 		mutex_exit(&ill->ill_lock);
2135 		if (ill->ill_flownotify_mh != NULL) {
2136 			idd = &idc->idc_direct;
2137 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2138 			    ill->ill_flownotify_mh);
2139 			ill->ill_flownotify_mh = NULL;
2140 		}
2141 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2142 		    NULL, DLD_DISABLE);
2143 	}
2144 
2145 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2146 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2147 		ip_squeue_clean_all(ill);
2148 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2149 		    NULL, DLD_DISABLE);
2150 	}
2151 
2152 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2153 		ASSERT(ill->ill_lso_capab != NULL);
2154 		/*
2155 		 * Clear the capability flag for LSO but retain the
2156 		 * ill_lso_capab structure since it's possible that another
2157 		 * thread is still referring to it.  The structure only gets
2158 		 * deallocated when we destroy the ill.
2159 		 */
2160 
2161 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2162 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2163 		    NULL, DLD_DISABLE);
2164 	}
2165 
2166 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2167 	ill_mac_perim_exit(ill, mph);
2168 }
2169 
2170 /*
2171  * Capability Negotiation protocol
2172  *
2173  * We don't wait for DLPI capability operations to finish during interface
2174  * bringup or teardown. Doing so would introduce more asynchrony and the
2175  * interface up/down operations will need multiple return and restarts.
2176  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2177  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2178  * exclusive operation won't start until the DLPI operations of the previous
2179  * exclusive operation complete.
2180  *
2181  * The capability state machine is shown below.
2182  *
2183  * state		next state		event, action
2184  *
2185  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2186  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2187  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2188  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2189  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2190  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2191  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2192  *						    ill_capability_probe.
2193  */
2194 
2195 /*
2196  * Dedicated thread started from ip_stack_init that handles capability
2197  * disable. This thread ensures the taskq dispatch does not fail by waiting
2198  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2199  * that direct calls to DLD are done in a cv_waitable context.
2200  */
2201 void
2202 ill_taskq_dispatch(ip_stack_t *ipst)
2203 {
2204 	callb_cpr_t cprinfo;
2205 	char 	name[64];
2206 	mblk_t	*mp;
2207 
2208 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2209 	    ipst->ips_netstack->netstack_stackid);
2210 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2211 	    name);
2212 	mutex_enter(&ipst->ips_capab_taskq_lock);
2213 
2214 	for (;;) {
2215 		mp = ipst->ips_capab_taskq_head;
2216 		while (mp != NULL) {
2217 			ipst->ips_capab_taskq_head = mp->b_next;
2218 			if (ipst->ips_capab_taskq_head == NULL)
2219 				ipst->ips_capab_taskq_tail = NULL;
2220 			mutex_exit(&ipst->ips_capab_taskq_lock);
2221 			mp->b_next = NULL;
2222 
2223 			VERIFY(taskq_dispatch(system_taskq,
2224 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2225 			mutex_enter(&ipst->ips_capab_taskq_lock);
2226 			mp = ipst->ips_capab_taskq_head;
2227 		}
2228 
2229 		if (ipst->ips_capab_taskq_quit)
2230 			break;
2231 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2232 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2233 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2234 	}
2235 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2236 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2237 	CALLB_CPR_EXIT(&cprinfo);
2238 	thread_exit();
2239 }
2240 
2241 /*
2242  * Consume a new-style hardware capabilities negotiation ack.
2243  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2244  */
2245 static void
2246 ill_capability_ack_thr(void *arg)
2247 {
2248 	mblk_t	*mp = arg;
2249 	dl_capability_ack_t *capp;
2250 	dl_capability_sub_t *subp, *endp;
2251 	ill_t	*ill;
2252 	boolean_t reneg;
2253 
2254 	ill = (ill_t *)mp->b_prev;
2255 	mp->b_prev = NULL;
2256 
2257 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2258 
2259 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2260 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2261 		/*
2262 		 * We have received the ack for our DL_CAPAB reset request.
2263 		 * There isnt' anything in the message that needs processing.
2264 		 * All message based capabilities have been disabled, now
2265 		 * do the function call based capability disable.
2266 		 */
2267 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2268 		ill_capability_dld_disable(ill);
2269 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2270 		if (reneg)
2271 			ill_capability_probe(ill);
2272 		goto done;
2273 	}
2274 
2275 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2276 		ill->ill_dlpi_capab_state = IDCS_OK;
2277 
2278 	capp = (dl_capability_ack_t *)mp->b_rptr;
2279 
2280 	if (capp->dl_sub_length == 0) {
2281 		/* no new-style capabilities */
2282 		goto done;
2283 	}
2284 
2285 	/* make sure the driver supplied correct dl_sub_length */
2286 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2287 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2288 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2289 		goto done;
2290 	}
2291 
2292 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2293 	/*
2294 	 * There are sub-capabilities. Process the ones we know about.
2295 	 * Loop until we don't have room for another sub-cap header..
2296 	 */
2297 	for (subp = SC(capp, capp->dl_sub_offset),
2298 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2299 	    subp <= endp;
2300 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2301 
2302 		switch (subp->dl_cap) {
2303 		case DL_CAPAB_ID_WRAPPER:
2304 			ill_capability_id_ack(ill, mp, subp);
2305 			break;
2306 		default:
2307 			ill_capability_dispatch(ill, mp, subp);
2308 			break;
2309 		}
2310 	}
2311 #undef SC
2312 done:
2313 	inet_freemsg(mp);
2314 	ill_capability_done(ill);
2315 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2316 }
2317 
2318 /*
2319  * This needs to be started in a taskq thread to provide a cv_waitable
2320  * context.
2321  */
2322 void
2323 ill_capability_ack(ill_t *ill, mblk_t *mp)
2324 {
2325 	ip_stack_t	*ipst = ill->ill_ipst;
2326 
2327 	mp->b_prev = (mblk_t *)ill;
2328 	ASSERT(mp->b_next == NULL);
2329 
2330 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2331 	    TQ_NOSLEEP) != 0)
2332 		return;
2333 
2334 	/*
2335 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2336 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2337 	 */
2338 	mutex_enter(&ipst->ips_capab_taskq_lock);
2339 	if (ipst->ips_capab_taskq_head == NULL) {
2340 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2341 		ipst->ips_capab_taskq_head = mp;
2342 	} else {
2343 		ipst->ips_capab_taskq_tail->b_next = mp;
2344 	}
2345 	ipst->ips_capab_taskq_tail = mp;
2346 
2347 	cv_signal(&ipst->ips_capab_taskq_cv);
2348 	mutex_exit(&ipst->ips_capab_taskq_lock);
2349 }
2350 
2351 /*
2352  * This routine is called to scan the fragmentation reassembly table for
2353  * the specified ILL for any packets that are starting to smell.
2354  * dead_interval is the maximum time in seconds that will be tolerated.  It
2355  * will either be the value specified in ip_g_frag_timeout, or zero if the
2356  * ILL is shutting down and it is time to blow everything off.
2357  *
2358  * It returns the number of seconds (as a time_t) that the next frag timer
2359  * should be scheduled for, 0 meaning that the timer doesn't need to be
2360  * re-started.  Note that the method of calculating next_timeout isn't
2361  * entirely accurate since time will flow between the time we grab
2362  * current_time and the time we schedule the next timeout.  This isn't a
2363  * big problem since this is the timer for sending an ICMP reassembly time
2364  * exceeded messages, and it doesn't have to be exactly accurate.
2365  *
2366  * This function is
2367  * sometimes called as writer, although this is not required.
2368  */
2369 time_t
2370 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2371 {
2372 	ipfb_t	*ipfb;
2373 	ipfb_t	*endp;
2374 	ipf_t	*ipf;
2375 	ipf_t	*ipfnext;
2376 	mblk_t	*mp;
2377 	time_t	current_time = gethrestime_sec();
2378 	time_t	next_timeout = 0;
2379 	uint32_t	hdr_length;
2380 	mblk_t	*send_icmp_head;
2381 	mblk_t	*send_icmp_head_v6;
2382 	ip_stack_t *ipst = ill->ill_ipst;
2383 	ip_recv_attr_t iras;
2384 
2385 	bzero(&iras, sizeof (iras));
2386 	iras.ira_flags = 0;
2387 	iras.ira_ill = iras.ira_rill = ill;
2388 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2389 	iras.ira_rifindex = iras.ira_ruifindex;
2390 
2391 	ipfb = ill->ill_frag_hash_tbl;
2392 	if (ipfb == NULL)
2393 		return (B_FALSE);
2394 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2395 	/* Walk the frag hash table. */
2396 	for (; ipfb < endp; ipfb++) {
2397 		send_icmp_head = NULL;
2398 		send_icmp_head_v6 = NULL;
2399 		mutex_enter(&ipfb->ipfb_lock);
2400 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2401 			time_t frag_time = current_time - ipf->ipf_timestamp;
2402 			time_t frag_timeout;
2403 
2404 			if (frag_time < dead_interval) {
2405 				/*
2406 				 * There are some outstanding fragments
2407 				 * that will timeout later.  Make note of
2408 				 * the time so that we can reschedule the
2409 				 * next timeout appropriately.
2410 				 */
2411 				frag_timeout = dead_interval - frag_time;
2412 				if (next_timeout == 0 ||
2413 				    frag_timeout < next_timeout) {
2414 					next_timeout = frag_timeout;
2415 				}
2416 				break;
2417 			}
2418 			/* Time's up.  Get it out of here. */
2419 			hdr_length = ipf->ipf_nf_hdr_len;
2420 			ipfnext = ipf->ipf_hash_next;
2421 			if (ipfnext)
2422 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2423 			*ipf->ipf_ptphn = ipfnext;
2424 			mp = ipf->ipf_mp->b_cont;
2425 			for (; mp; mp = mp->b_cont) {
2426 				/* Extra points for neatness. */
2427 				IP_REASS_SET_START(mp, 0);
2428 				IP_REASS_SET_END(mp, 0);
2429 			}
2430 			mp = ipf->ipf_mp->b_cont;
2431 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2432 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2433 			ipfb->ipfb_count -= ipf->ipf_count;
2434 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2435 			ipfb->ipfb_frag_pkts--;
2436 			/*
2437 			 * We do not send any icmp message from here because
2438 			 * we currently are holding the ipfb_lock for this
2439 			 * hash chain. If we try and send any icmp messages
2440 			 * from here we may end up via a put back into ip
2441 			 * trying to get the same lock, causing a recursive
2442 			 * mutex panic. Instead we build a list and send all
2443 			 * the icmp messages after we have dropped the lock.
2444 			 */
2445 			if (ill->ill_isv6) {
2446 				if (hdr_length != 0) {
2447 					mp->b_next = send_icmp_head_v6;
2448 					send_icmp_head_v6 = mp;
2449 				} else {
2450 					freemsg(mp);
2451 				}
2452 			} else {
2453 				if (hdr_length != 0) {
2454 					mp->b_next = send_icmp_head;
2455 					send_icmp_head = mp;
2456 				} else {
2457 					freemsg(mp);
2458 				}
2459 			}
2460 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2461 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2462 			freeb(ipf->ipf_mp);
2463 		}
2464 		mutex_exit(&ipfb->ipfb_lock);
2465 		/*
2466 		 * Now need to send any icmp messages that we delayed from
2467 		 * above.
2468 		 */
2469 		while (send_icmp_head_v6 != NULL) {
2470 			ip6_t *ip6h;
2471 
2472 			mp = send_icmp_head_v6;
2473 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2474 			mp->b_next = NULL;
2475 			ip6h = (ip6_t *)mp->b_rptr;
2476 			iras.ira_flags = 0;
2477 			/*
2478 			 * This will result in an incorrect ALL_ZONES zoneid
2479 			 * for multicast packets, but we
2480 			 * don't send ICMP errors for those in any case.
2481 			 */
2482 			iras.ira_zoneid =
2483 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2484 			    ill, ipst);
2485 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2486 			icmp_time_exceeded_v6(mp,
2487 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2488 			    &iras);
2489 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2490 		}
2491 		while (send_icmp_head != NULL) {
2492 			ipaddr_t dst;
2493 
2494 			mp = send_icmp_head;
2495 			send_icmp_head = send_icmp_head->b_next;
2496 			mp->b_next = NULL;
2497 
2498 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2499 
2500 			iras.ira_flags = IRAF_IS_IPV4;
2501 			/*
2502 			 * This will result in an incorrect ALL_ZONES zoneid
2503 			 * for broadcast and multicast packets, but we
2504 			 * don't send ICMP errors for those in any case.
2505 			 */
2506 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2507 			    ill, ipst);
2508 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2509 			icmp_time_exceeded(mp,
2510 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2511 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2512 		}
2513 	}
2514 	/*
2515 	 * A non-dying ILL will use the return value to decide whether to
2516 	 * restart the frag timer, and for how long.
2517 	 */
2518 	return (next_timeout);
2519 }
2520 
2521 /*
2522  * This routine is called when the approximate count of mblk memory used
2523  * for the specified ILL has exceeded max_count.
2524  */
2525 void
2526 ill_frag_prune(ill_t *ill, uint_t max_count)
2527 {
2528 	ipfb_t	*ipfb;
2529 	ipf_t	*ipf;
2530 	size_t	count;
2531 	clock_t now;
2532 
2533 	/*
2534 	 * If we are here within ip_min_frag_prune_time msecs remove
2535 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2536 	 * ill_frag_free_num_pkts.
2537 	 */
2538 	mutex_enter(&ill->ill_lock);
2539 	now = ddi_get_lbolt();
2540 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2541 	    (ip_min_frag_prune_time != 0 ?
2542 	    ip_min_frag_prune_time : msec_per_tick)) {
2543 
2544 		ill->ill_frag_free_num_pkts++;
2545 
2546 	} else {
2547 		ill->ill_frag_free_num_pkts = 0;
2548 	}
2549 	ill->ill_last_frag_clean_time = now;
2550 	mutex_exit(&ill->ill_lock);
2551 
2552 	/*
2553 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2554 	 */
2555 	if (ill->ill_frag_free_num_pkts != 0) {
2556 		int ix;
2557 
2558 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2559 			ipfb = &ill->ill_frag_hash_tbl[ix];
2560 			mutex_enter(&ipfb->ipfb_lock);
2561 			if (ipfb->ipfb_ipf != NULL) {
2562 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2563 				    ill->ill_frag_free_num_pkts);
2564 			}
2565 			mutex_exit(&ipfb->ipfb_lock);
2566 		}
2567 	}
2568 	/*
2569 	 * While the reassembly list for this ILL is too big, prune a fragment
2570 	 * queue by age, oldest first.
2571 	 */
2572 	while (ill->ill_frag_count > max_count) {
2573 		int	ix;
2574 		ipfb_t	*oipfb = NULL;
2575 		uint_t	oldest = UINT_MAX;
2576 
2577 		count = 0;
2578 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2579 			ipfb = &ill->ill_frag_hash_tbl[ix];
2580 			mutex_enter(&ipfb->ipfb_lock);
2581 			ipf = ipfb->ipfb_ipf;
2582 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2583 				oldest = ipf->ipf_gen;
2584 				oipfb = ipfb;
2585 			}
2586 			count += ipfb->ipfb_count;
2587 			mutex_exit(&ipfb->ipfb_lock);
2588 		}
2589 		if (oipfb == NULL)
2590 			break;
2591 
2592 		if (count <= max_count)
2593 			return;	/* Somebody beat us to it, nothing to do */
2594 		mutex_enter(&oipfb->ipfb_lock);
2595 		ipf = oipfb->ipfb_ipf;
2596 		if (ipf != NULL) {
2597 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2598 		}
2599 		mutex_exit(&oipfb->ipfb_lock);
2600 	}
2601 }
2602 
2603 /*
2604  * free 'free_cnt' fragmented packets starting at ipf.
2605  */
2606 void
2607 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2608 {
2609 	size_t	count;
2610 	mblk_t	*mp;
2611 	mblk_t	*tmp;
2612 	ipf_t **ipfp = ipf->ipf_ptphn;
2613 
2614 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2615 	ASSERT(ipfp != NULL);
2616 	ASSERT(ipf != NULL);
2617 
2618 	while (ipf != NULL && free_cnt-- > 0) {
2619 		count = ipf->ipf_count;
2620 		mp = ipf->ipf_mp;
2621 		ipf = ipf->ipf_hash_next;
2622 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2623 			IP_REASS_SET_START(tmp, 0);
2624 			IP_REASS_SET_END(tmp, 0);
2625 		}
2626 		atomic_add_32(&ill->ill_frag_count, -count);
2627 		ASSERT(ipfb->ipfb_count >= count);
2628 		ipfb->ipfb_count -= count;
2629 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2630 		ipfb->ipfb_frag_pkts--;
2631 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2632 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2633 		freemsg(mp);
2634 	}
2635 
2636 	if (ipf)
2637 		ipf->ipf_ptphn = ipfp;
2638 	ipfp[0] = ipf;
2639 }
2640 
2641 /*
2642  * Helper function for ill_forward_set().
2643  */
2644 static void
2645 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2646 {
2647 	ip_stack_t	*ipst = ill->ill_ipst;
2648 
2649 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2650 
2651 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2652 	    (enable ? "Enabling" : "Disabling"),
2653 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2654 	mutex_enter(&ill->ill_lock);
2655 	if (enable)
2656 		ill->ill_flags |= ILLF_ROUTER;
2657 	else
2658 		ill->ill_flags &= ~ILLF_ROUTER;
2659 	mutex_exit(&ill->ill_lock);
2660 	if (ill->ill_isv6)
2661 		ill_set_nce_router_flags(ill, enable);
2662 	/* Notify routing socket listeners of this change. */
2663 	if (ill->ill_ipif != NULL)
2664 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2665 }
2666 
2667 /*
2668  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2669  * socket messages for each interface whose flags we change.
2670  */
2671 int
2672 ill_forward_set(ill_t *ill, boolean_t enable)
2673 {
2674 	ipmp_illgrp_t *illg;
2675 	ip_stack_t *ipst = ill->ill_ipst;
2676 
2677 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2678 
2679 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2680 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2681 		return (0);
2682 
2683 	if (IS_LOOPBACK(ill))
2684 		return (EINVAL);
2685 
2686 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2687 		/*
2688 		 * Update all of the interfaces in the group.
2689 		 */
2690 		illg = ill->ill_grp;
2691 		ill = list_head(&illg->ig_if);
2692 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2693 			ill_forward_set_on_ill(ill, enable);
2694 
2695 		/*
2696 		 * Update the IPMP meta-interface.
2697 		 */
2698 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2699 		return (0);
2700 	}
2701 
2702 	ill_forward_set_on_ill(ill, enable);
2703 	return (0);
2704 }
2705 
2706 /*
2707  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2708  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2709  * set or clear.
2710  */
2711 static void
2712 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2713 {
2714 	ipif_t *ipif;
2715 	ncec_t *ncec;
2716 	nce_t *nce;
2717 
2718 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2719 		/*
2720 		 * NOTE: we match across the illgrp because nce's for
2721 		 * addresses on IPMP interfaces have an nce_ill that points to
2722 		 * the bound underlying ill.
2723 		 */
2724 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2725 		if (nce != NULL) {
2726 			ncec = nce->nce_common;
2727 			mutex_enter(&ncec->ncec_lock);
2728 			if (enable)
2729 				ncec->ncec_flags |= NCE_F_ISROUTER;
2730 			else
2731 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2732 			mutex_exit(&ncec->ncec_lock);
2733 			nce_refrele(nce);
2734 		}
2735 	}
2736 }
2737 
2738 /*
2739  * Intializes the context structure and returns the first ill in the list
2740  * cuurently start_list and end_list can have values:
2741  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2742  * IP_V4_G_HEAD		Traverse IPV4 list only.
2743  * IP_V6_G_HEAD		Traverse IPV6 list only.
2744  */
2745 
2746 /*
2747  * We don't check for CONDEMNED ills here. Caller must do that if
2748  * necessary under the ill lock.
2749  */
2750 ill_t *
2751 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2752     ip_stack_t *ipst)
2753 {
2754 	ill_if_t *ifp;
2755 	ill_t *ill;
2756 	avl_tree_t *avl_tree;
2757 
2758 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2759 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2760 
2761 	/*
2762 	 * setup the lists to search
2763 	 */
2764 	if (end_list != MAX_G_HEADS) {
2765 		ctx->ctx_current_list = start_list;
2766 		ctx->ctx_last_list = end_list;
2767 	} else {
2768 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2769 		ctx->ctx_current_list = 0;
2770 	}
2771 
2772 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2773 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2774 		if (ifp != (ill_if_t *)
2775 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2776 			avl_tree = &ifp->illif_avl_by_ppa;
2777 			ill = avl_first(avl_tree);
2778 			/*
2779 			 * ill is guaranteed to be non NULL or ifp should have
2780 			 * not existed.
2781 			 */
2782 			ASSERT(ill != NULL);
2783 			return (ill);
2784 		}
2785 		ctx->ctx_current_list++;
2786 	}
2787 
2788 	return (NULL);
2789 }
2790 
2791 /*
2792  * returns the next ill in the list. ill_first() must have been called
2793  * before calling ill_next() or bad things will happen.
2794  */
2795 
2796 /*
2797  * We don't check for CONDEMNED ills here. Caller must do that if
2798  * necessary under the ill lock.
2799  */
2800 ill_t *
2801 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2802 {
2803 	ill_if_t *ifp;
2804 	ill_t *ill;
2805 	ip_stack_t	*ipst = lastill->ill_ipst;
2806 
2807 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2808 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2809 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2810 	    AVL_AFTER)) != NULL) {
2811 		return (ill);
2812 	}
2813 
2814 	/* goto next ill_ifp in the list. */
2815 	ifp = lastill->ill_ifptr->illif_next;
2816 
2817 	/* make sure not at end of circular list */
2818 	while (ifp ==
2819 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2820 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2821 			return (NULL);
2822 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2823 	}
2824 
2825 	return (avl_first(&ifp->illif_avl_by_ppa));
2826 }
2827 
2828 /*
2829  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2830  * The final number (PPA) must not have any leading zeros.  Upon success, a
2831  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2832  */
2833 static char *
2834 ill_get_ppa_ptr(char *name)
2835 {
2836 	int namelen = strlen(name);
2837 	int end_ndx = namelen - 1;
2838 	int ppa_ndx, i;
2839 
2840 	/*
2841 	 * Check that the first character is [a-zA-Z], and that the last
2842 	 * character is [0-9].
2843 	 */
2844 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2845 		return (NULL);
2846 
2847 	/*
2848 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2849 	 */
2850 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2851 		if (!isdigit(name[ppa_ndx - 1]))
2852 			break;
2853 
2854 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2855 		return (NULL);
2856 
2857 	/*
2858 	 * Check that the intermediate characters are [a-z0-9.]
2859 	 */
2860 	for (i = 1; i < ppa_ndx; i++) {
2861 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2862 		    name[i] != '.' && name[i] != '_') {
2863 			return (NULL);
2864 		}
2865 	}
2866 
2867 	return (name + ppa_ndx);
2868 }
2869 
2870 /*
2871  * use avl tree to locate the ill.
2872  */
2873 static ill_t *
2874 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2875 {
2876 	char *ppa_ptr = NULL;
2877 	int len;
2878 	uint_t ppa;
2879 	ill_t *ill = NULL;
2880 	ill_if_t *ifp;
2881 	int list;
2882 
2883 	/*
2884 	 * get ppa ptr
2885 	 */
2886 	if (isv6)
2887 		list = IP_V6_G_HEAD;
2888 	else
2889 		list = IP_V4_G_HEAD;
2890 
2891 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2892 		return (NULL);
2893 	}
2894 
2895 	len = ppa_ptr - name + 1;
2896 
2897 	ppa = stoi(&ppa_ptr);
2898 
2899 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2900 
2901 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2902 		/*
2903 		 * match is done on len - 1 as the name is not null
2904 		 * terminated it contains ppa in addition to the interface
2905 		 * name.
2906 		 */
2907 		if ((ifp->illif_name_len == len) &&
2908 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2909 			break;
2910 		} else {
2911 			ifp = ifp->illif_next;
2912 		}
2913 	}
2914 
2915 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2916 		/*
2917 		 * Even the interface type does not exist.
2918 		 */
2919 		return (NULL);
2920 	}
2921 
2922 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2923 	if (ill != NULL) {
2924 		mutex_enter(&ill->ill_lock);
2925 		if (ILL_CAN_LOOKUP(ill)) {
2926 			ill_refhold_locked(ill);
2927 			mutex_exit(&ill->ill_lock);
2928 			return (ill);
2929 		}
2930 		mutex_exit(&ill->ill_lock);
2931 	}
2932 	return (NULL);
2933 }
2934 
2935 /*
2936  * comparison function for use with avl.
2937  */
2938 static int
2939 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2940 {
2941 	uint_t ppa;
2942 	uint_t ill_ppa;
2943 
2944 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2945 
2946 	ppa = *((uint_t *)ppa_ptr);
2947 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2948 	/*
2949 	 * We want the ill with the lowest ppa to be on the
2950 	 * top.
2951 	 */
2952 	if (ill_ppa < ppa)
2953 		return (1);
2954 	if (ill_ppa > ppa)
2955 		return (-1);
2956 	return (0);
2957 }
2958 
2959 /*
2960  * remove an interface type from the global list.
2961  */
2962 static void
2963 ill_delete_interface_type(ill_if_t *interface)
2964 {
2965 	ASSERT(interface != NULL);
2966 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2967 
2968 	avl_destroy(&interface->illif_avl_by_ppa);
2969 	if (interface->illif_ppa_arena != NULL)
2970 		vmem_destroy(interface->illif_ppa_arena);
2971 
2972 	remque(interface);
2973 
2974 	mi_free(interface);
2975 }
2976 
2977 /*
2978  * remove ill from the global list.
2979  */
2980 static void
2981 ill_glist_delete(ill_t *ill)
2982 {
2983 	ip_stack_t	*ipst;
2984 	phyint_t	*phyi;
2985 
2986 	if (ill == NULL)
2987 		return;
2988 	ipst = ill->ill_ipst;
2989 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2990 
2991 	/*
2992 	 * If the ill was never inserted into the AVL tree
2993 	 * we skip the if branch.
2994 	 */
2995 	if (ill->ill_ifptr != NULL) {
2996 		/*
2997 		 * remove from AVL tree and free ppa number
2998 		 */
2999 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3000 
3001 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3002 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3003 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3004 		}
3005 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3006 			ill_delete_interface_type(ill->ill_ifptr);
3007 		}
3008 
3009 		/*
3010 		 * Indicate ill is no longer in the list.
3011 		 */
3012 		ill->ill_ifptr = NULL;
3013 		ill->ill_name_length = 0;
3014 		ill->ill_name[0] = '\0';
3015 		ill->ill_ppa = UINT_MAX;
3016 	}
3017 
3018 	/* Generate one last event for this ill. */
3019 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3020 	    ill->ill_name_length);
3021 
3022 	ASSERT(ill->ill_phyint != NULL);
3023 	phyi = ill->ill_phyint;
3024 	ill->ill_phyint = NULL;
3025 
3026 	/*
3027 	 * ill_init allocates a phyint always to store the copy
3028 	 * of flags relevant to phyint. At that point in time, we could
3029 	 * not assign the name and hence phyint_illv4/v6 could not be
3030 	 * initialized. Later in ipif_set_values, we assign the name to
3031 	 * the ill, at which point in time we assign phyint_illv4/v6.
3032 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3033 	 */
3034 	if (ill->ill_flags & ILLF_IPV6)
3035 		phyi->phyint_illv6 = NULL;
3036 	else
3037 		phyi->phyint_illv4 = NULL;
3038 
3039 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3040 		rw_exit(&ipst->ips_ill_g_lock);
3041 		return;
3042 	}
3043 
3044 	/*
3045 	 * There are no ills left on this phyint; pull it out of the phyint
3046 	 * avl trees, and free it.
3047 	 */
3048 	if (phyi->phyint_ifindex > 0) {
3049 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3050 		    phyi);
3051 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3052 		    phyi);
3053 	}
3054 	rw_exit(&ipst->ips_ill_g_lock);
3055 
3056 	phyint_free(phyi);
3057 }
3058 
3059 /*
3060  * allocate a ppa, if the number of plumbed interfaces of this type are
3061  * less than ill_no_arena do a linear search to find a unused ppa.
3062  * When the number goes beyond ill_no_arena switch to using an arena.
3063  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3064  * is the return value for an error condition, so allocation starts at one
3065  * and is decremented by one.
3066  */
3067 static int
3068 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3069 {
3070 	ill_t *tmp_ill;
3071 	uint_t start, end;
3072 	int ppa;
3073 
3074 	if (ifp->illif_ppa_arena == NULL &&
3075 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3076 		/*
3077 		 * Create an arena.
3078 		 */
3079 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3080 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3081 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3082 			/* allocate what has already been assigned */
3083 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3084 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3085 		    tmp_ill, AVL_AFTER)) {
3086 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3087 			    1,		/* size */
3088 			    1,		/* align/quantum */
3089 			    0,		/* phase */
3090 			    0,		/* nocross */
3091 			    /* minaddr */
3092 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3093 			    /* maxaddr */
3094 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3095 			    VM_NOSLEEP|VM_FIRSTFIT);
3096 			if (ppa == 0) {
3097 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3098 				    " failed while switching"));
3099 				vmem_destroy(ifp->illif_ppa_arena);
3100 				ifp->illif_ppa_arena = NULL;
3101 				break;
3102 			}
3103 		}
3104 	}
3105 
3106 	if (ifp->illif_ppa_arena != NULL) {
3107 		if (ill->ill_ppa == UINT_MAX) {
3108 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3109 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3110 			if (ppa == 0)
3111 				return (EAGAIN);
3112 			ill->ill_ppa = --ppa;
3113 		} else {
3114 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3115 			    1, 		/* size */
3116 			    1, 		/* align/quantum */
3117 			    0, 		/* phase */
3118 			    0, 		/* nocross */
3119 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3120 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3121 			    VM_NOSLEEP|VM_FIRSTFIT);
3122 			/*
3123 			 * Most likely the allocation failed because
3124 			 * the requested ppa was in use.
3125 			 */
3126 			if (ppa == 0)
3127 				return (EEXIST);
3128 		}
3129 		return (0);
3130 	}
3131 
3132 	/*
3133 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3134 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3135 	 */
3136 	if (ill->ill_ppa == UINT_MAX) {
3137 		end = UINT_MAX - 1;
3138 		start = 0;
3139 	} else {
3140 		end = start = ill->ill_ppa;
3141 	}
3142 
3143 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3144 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3145 		if (start++ >= end) {
3146 			if (ill->ill_ppa == UINT_MAX)
3147 				return (EAGAIN);
3148 			else
3149 				return (EEXIST);
3150 		}
3151 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3152 	}
3153 	ill->ill_ppa = start;
3154 	return (0);
3155 }
3156 
3157 /*
3158  * Insert ill into the list of configured ill's. Once this function completes,
3159  * the ill is globally visible and is available through lookups. More precisely
3160  * this happens after the caller drops the ill_g_lock.
3161  */
3162 static int
3163 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3164 {
3165 	ill_if_t *ill_interface;
3166 	avl_index_t where = 0;
3167 	int error;
3168 	int name_length;
3169 	int index;
3170 	boolean_t check_length = B_FALSE;
3171 	ip_stack_t	*ipst = ill->ill_ipst;
3172 
3173 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3174 
3175 	name_length = mi_strlen(name) + 1;
3176 
3177 	if (isv6)
3178 		index = IP_V6_G_HEAD;
3179 	else
3180 		index = IP_V4_G_HEAD;
3181 
3182 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3183 	/*
3184 	 * Search for interface type based on name
3185 	 */
3186 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3187 		if ((ill_interface->illif_name_len == name_length) &&
3188 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3189 			break;
3190 		}
3191 		ill_interface = ill_interface->illif_next;
3192 	}
3193 
3194 	/*
3195 	 * Interface type not found, create one.
3196 	 */
3197 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3198 		ill_g_head_t ghead;
3199 
3200 		/*
3201 		 * allocate ill_if_t structure
3202 		 */
3203 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3204 		if (ill_interface == NULL) {
3205 			return (ENOMEM);
3206 		}
3207 
3208 		(void) strcpy(ill_interface->illif_name, name);
3209 		ill_interface->illif_name_len = name_length;
3210 
3211 		avl_create(&ill_interface->illif_avl_by_ppa,
3212 		    ill_compare_ppa, sizeof (ill_t),
3213 		    offsetof(struct ill_s, ill_avl_byppa));
3214 
3215 		/*
3216 		 * link the structure in the back to maintain order
3217 		 * of configuration for ifconfig output.
3218 		 */
3219 		ghead = ipst->ips_ill_g_heads[index];
3220 		insque(ill_interface, ghead.ill_g_list_tail);
3221 	}
3222 
3223 	if (ill->ill_ppa == UINT_MAX)
3224 		check_length = B_TRUE;
3225 
3226 	error = ill_alloc_ppa(ill_interface, ill);
3227 	if (error != 0) {
3228 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3229 			ill_delete_interface_type(ill->ill_ifptr);
3230 		return (error);
3231 	}
3232 
3233 	/*
3234 	 * When the ppa is choosen by the system, check that there is
3235 	 * enough space to insert ppa. if a specific ppa was passed in this
3236 	 * check is not required as the interface name passed in will have
3237 	 * the right ppa in it.
3238 	 */
3239 	if (check_length) {
3240 		/*
3241 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3242 		 */
3243 		char buf[sizeof (uint_t) * 3];
3244 
3245 		/*
3246 		 * convert ppa to string to calculate the amount of space
3247 		 * required for it in the name.
3248 		 */
3249 		numtos(ill->ill_ppa, buf);
3250 
3251 		/* Do we have enough space to insert ppa ? */
3252 
3253 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3254 			/* Free ppa and interface type struct */
3255 			if (ill_interface->illif_ppa_arena != NULL) {
3256 				vmem_free(ill_interface->illif_ppa_arena,
3257 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3258 			}
3259 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3260 				ill_delete_interface_type(ill->ill_ifptr);
3261 
3262 			return (EINVAL);
3263 		}
3264 	}
3265 
3266 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3267 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3268 
3269 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3270 	    &where);
3271 	ill->ill_ifptr = ill_interface;
3272 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3273 
3274 	ill_phyint_reinit(ill);
3275 	return (0);
3276 }
3277 
3278 /* Initialize the per phyint ipsq used for serialization */
3279 static boolean_t
3280 ipsq_init(ill_t *ill, boolean_t enter)
3281 {
3282 	ipsq_t  *ipsq;
3283 	ipxop_t	*ipx;
3284 
3285 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3286 		return (B_FALSE);
3287 
3288 	ill->ill_phyint->phyint_ipsq = ipsq;
3289 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3290 	ipx->ipx_ipsq = ipsq;
3291 	ipsq->ipsq_next = ipsq;
3292 	ipsq->ipsq_phyint = ill->ill_phyint;
3293 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3294 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3295 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3296 	if (enter) {
3297 		ipx->ipx_writer = curthread;
3298 		ipx->ipx_forced = B_FALSE;
3299 		ipx->ipx_reentry_cnt = 1;
3300 #ifdef DEBUG
3301 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3302 #endif
3303 	}
3304 	return (B_TRUE);
3305 }
3306 
3307 /*
3308  * ill_init is called by ip_open when a device control stream is opened.
3309  * It does a few initializations, and shoots a DL_INFO_REQ message down
3310  * to the driver.  The response is later picked up in ip_rput_dlpi and
3311  * used to set up default mechanisms for talking to the driver.  (Always
3312  * called as writer.)
3313  *
3314  * If this function returns error, ip_open will call ip_close which in
3315  * turn will call ill_delete to clean up any memory allocated here that
3316  * is not yet freed.
3317  */
3318 int
3319 ill_init(queue_t *q, ill_t *ill)
3320 {
3321 	int	count;
3322 	dl_info_req_t	*dlir;
3323 	mblk_t	*info_mp;
3324 	uchar_t *frag_ptr;
3325 
3326 	/*
3327 	 * The ill is initialized to zero by mi_alloc*(). In addition
3328 	 * some fields already contain valid values, initialized in
3329 	 * ip_open(), before we reach here.
3330 	 */
3331 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3332 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3333 	ill->ill_saved_ire_cnt = 0;
3334 
3335 	ill->ill_rq = q;
3336 	ill->ill_wq = WR(q);
3337 
3338 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3339 	    BPRI_HI);
3340 	if (info_mp == NULL)
3341 		return (ENOMEM);
3342 
3343 	/*
3344 	 * Allocate sufficient space to contain our fragment hash table and
3345 	 * the device name.
3346 	 */
3347 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3348 	if (frag_ptr == NULL) {
3349 		freemsg(info_mp);
3350 		return (ENOMEM);
3351 	}
3352 	ill->ill_frag_ptr = frag_ptr;
3353 	ill->ill_frag_free_num_pkts = 0;
3354 	ill->ill_last_frag_clean_time = 0;
3355 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3356 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3357 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3358 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3359 		    NULL, MUTEX_DEFAULT, NULL);
3360 	}
3361 
3362 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3363 	if (ill->ill_phyint == NULL) {
3364 		freemsg(info_mp);
3365 		mi_free(frag_ptr);
3366 		return (ENOMEM);
3367 	}
3368 
3369 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3370 	/*
3371 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3372 	 * at this point because of the following reason. If we can't
3373 	 * enter the ipsq at some point and cv_wait, the writer that
3374 	 * wakes us up tries to locate us using the list of all phyints
3375 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3376 	 * If we don't set it now, we risk a missed wakeup.
3377 	 */
3378 	ill->ill_phyint->phyint_illv4 = ill;
3379 	ill->ill_ppa = UINT_MAX;
3380 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3381 
3382 	ill_set_inputfn(ill);
3383 
3384 	if (!ipsq_init(ill, B_TRUE)) {
3385 		freemsg(info_mp);
3386 		mi_free(frag_ptr);
3387 		mi_free(ill->ill_phyint);
3388 		return (ENOMEM);
3389 	}
3390 
3391 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3392 
3393 	/* Frag queue limit stuff */
3394 	ill->ill_frag_count = 0;
3395 	ill->ill_ipf_gen = 0;
3396 
3397 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3398 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3399 	ill->ill_global_timer = INFINITY;
3400 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3401 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3402 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3403 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3404 
3405 	/*
3406 	 * Initialize IPv6 configuration variables.  The IP module is always
3407 	 * opened as an IPv4 module.  Instead tracking down the cases where
3408 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3409 	 * here for convenience, this has no effect until the ill is set to do
3410 	 * IPv6.
3411 	 */
3412 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3413 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3414 	ill->ill_max_buf = ND_MAX_Q;
3415 	ill->ill_refcnt = 0;
3416 
3417 	/* Send down the Info Request to the driver. */
3418 	info_mp->b_datap->db_type = M_PCPROTO;
3419 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3420 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3421 	dlir->dl_primitive = DL_INFO_REQ;
3422 
3423 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3424 
3425 	qprocson(q);
3426 	ill_dlpi_send(ill, info_mp);
3427 
3428 	return (0);
3429 }
3430 
3431 /*
3432  * ill_dls_info
3433  * creates datalink socket info from the device.
3434  */
3435 int
3436 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3437 {
3438 	size_t	len;
3439 
3440 	sdl->sdl_family = AF_LINK;
3441 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3442 	sdl->sdl_type = ill->ill_type;
3443 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3444 	len = strlen(sdl->sdl_data);
3445 	ASSERT(len < 256);
3446 	sdl->sdl_nlen = (uchar_t)len;
3447 	sdl->sdl_alen = ill->ill_phys_addr_length;
3448 	sdl->sdl_slen = 0;
3449 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3450 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3451 
3452 	return (sizeof (struct sockaddr_dl));
3453 }
3454 
3455 /*
3456  * ill_xarp_info
3457  * creates xarp info from the device.
3458  */
3459 static int
3460 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3461 {
3462 	sdl->sdl_family = AF_LINK;
3463 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3464 	sdl->sdl_type = ill->ill_type;
3465 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3466 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3467 	sdl->sdl_alen = ill->ill_phys_addr_length;
3468 	sdl->sdl_slen = 0;
3469 	return (sdl->sdl_nlen);
3470 }
3471 
3472 static int
3473 loopback_kstat_update(kstat_t *ksp, int rw)
3474 {
3475 	kstat_named_t *kn;
3476 	netstackid_t	stackid;
3477 	netstack_t	*ns;
3478 	ip_stack_t	*ipst;
3479 
3480 	if (ksp == NULL || ksp->ks_data == NULL)
3481 		return (EIO);
3482 
3483 	if (rw == KSTAT_WRITE)
3484 		return (EACCES);
3485 
3486 	kn = KSTAT_NAMED_PTR(ksp);
3487 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3488 
3489 	ns = netstack_find_by_stackid(stackid);
3490 	if (ns == NULL)
3491 		return (-1);
3492 
3493 	ipst = ns->netstack_ip;
3494 	if (ipst == NULL) {
3495 		netstack_rele(ns);
3496 		return (-1);
3497 	}
3498 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3499 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3500 	netstack_rele(ns);
3501 	return (0);
3502 }
3503 
3504 /*
3505  * Has ifindex been plumbed already?
3506  */
3507 static boolean_t
3508 phyint_exists(uint_t index, ip_stack_t *ipst)
3509 {
3510 	ASSERT(index != 0);
3511 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3512 
3513 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3514 	    &index, NULL) != NULL);
3515 }
3516 
3517 /* Pick a unique ifindex */
3518 boolean_t
3519 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3520 {
3521 	uint_t starting_index;
3522 
3523 	if (!ipst->ips_ill_index_wrap) {
3524 		*indexp = ipst->ips_ill_index++;
3525 		if (ipst->ips_ill_index == 0) {
3526 			/* Reached the uint_t limit Next time wrap  */
3527 			ipst->ips_ill_index_wrap = B_TRUE;
3528 		}
3529 		return (B_TRUE);
3530 	}
3531 
3532 	/*
3533 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3534 	 * at this point and don't want to call any function that attempts
3535 	 * to get the lock again.
3536 	 */
3537 	starting_index = ipst->ips_ill_index++;
3538 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3539 		if (ipst->ips_ill_index != 0 &&
3540 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3541 			/* found unused index - use it */
3542 			*indexp = ipst->ips_ill_index;
3543 			return (B_TRUE);
3544 		}
3545 	}
3546 
3547 	/*
3548 	 * all interface indicies are inuse.
3549 	 */
3550 	return (B_FALSE);
3551 }
3552 
3553 /*
3554  * Assign a unique interface index for the phyint.
3555  */
3556 static boolean_t
3557 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3558 {
3559 	ASSERT(phyi->phyint_ifindex == 0);
3560 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3561 }
3562 
3563 /*
3564  * Initialize the flags on `phyi' as per the provided mactype.
3565  */
3566 static void
3567 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3568 {
3569 	uint64_t flags = 0;
3570 
3571 	/*
3572 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3573 	 * we always presume the underlying hardware is working and set
3574 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3575 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3576 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3577 	 */
3578 	if (mactype == SUNW_DL_IPMP)
3579 		flags |= PHYI_FAILED;
3580 	else
3581 		flags |= PHYI_RUNNING;
3582 
3583 	switch (mactype) {
3584 	case SUNW_DL_VNI:
3585 		flags |= PHYI_VIRTUAL;
3586 		break;
3587 	case SUNW_DL_IPMP:
3588 		flags |= PHYI_IPMP;
3589 		break;
3590 	case DL_LOOP:
3591 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3592 		break;
3593 	}
3594 
3595 	mutex_enter(&phyi->phyint_lock);
3596 	phyi->phyint_flags |= flags;
3597 	mutex_exit(&phyi->phyint_lock);
3598 }
3599 
3600 /*
3601  * Return a pointer to the ill which matches the supplied name.  Note that
3602  * the ill name length includes the null termination character.  (May be
3603  * called as writer.)
3604  * If do_alloc and the interface is "lo0" it will be automatically created.
3605  * Cannot bump up reference on condemned ills. So dup detect can't be done
3606  * using this func.
3607  */
3608 ill_t *
3609 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3610     boolean_t *did_alloc, ip_stack_t *ipst)
3611 {
3612 	ill_t	*ill;
3613 	ipif_t	*ipif;
3614 	ipsq_t	*ipsq;
3615 	kstat_named_t	*kn;
3616 	boolean_t isloopback;
3617 	in6_addr_t ov6addr;
3618 
3619 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3620 
3621 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3622 	ill = ill_find_by_name(name, isv6, ipst);
3623 	rw_exit(&ipst->ips_ill_g_lock);
3624 	if (ill != NULL)
3625 		return (ill);
3626 
3627 	/*
3628 	 * Couldn't find it.  Does this happen to be a lookup for the
3629 	 * loopback device and are we allowed to allocate it?
3630 	 */
3631 	if (!isloopback || !do_alloc)
3632 		return (NULL);
3633 
3634 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3635 	ill = ill_find_by_name(name, isv6, ipst);
3636 	if (ill != NULL) {
3637 		rw_exit(&ipst->ips_ill_g_lock);
3638 		return (ill);
3639 	}
3640 
3641 	/* Create the loopback device on demand */
3642 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3643 	    sizeof (ipif_loopback_name), BPRI_MED));
3644 	if (ill == NULL)
3645 		goto done;
3646 
3647 	*ill = ill_null;
3648 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3649 	ill->ill_ipst = ipst;
3650 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3651 	netstack_hold(ipst->ips_netstack);
3652 	/*
3653 	 * For exclusive stacks we set the zoneid to zero
3654 	 * to make IP operate as if in the global zone.
3655 	 */
3656 	ill->ill_zoneid = GLOBAL_ZONEID;
3657 
3658 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3659 	if (ill->ill_phyint == NULL)
3660 		goto done;
3661 
3662 	if (isv6)
3663 		ill->ill_phyint->phyint_illv6 = ill;
3664 	else
3665 		ill->ill_phyint->phyint_illv4 = ill;
3666 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3667 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3668 
3669 	if (isv6) {
3670 		ill->ill_isv6 = B_TRUE;
3671 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3672 	} else {
3673 		ill->ill_max_frag = ip_loopback_mtuplus;
3674 	}
3675 	if (!ill_allocate_mibs(ill))
3676 		goto done;
3677 	ill->ill_current_frag = ill->ill_max_frag;
3678 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3679 	/*
3680 	 * ipif_loopback_name can't be pointed at directly because its used
3681 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3682 	 * from the glist, ill_glist_delete() sets the first character of
3683 	 * ill_name to '\0'.
3684 	 */
3685 	ill->ill_name = (char *)ill + sizeof (*ill);
3686 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3687 	ill->ill_name_length = sizeof (ipif_loopback_name);
3688 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3689 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3690 
3691 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3692 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3693 	ill->ill_global_timer = INFINITY;
3694 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3695 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3696 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3697 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3698 
3699 	/* No resolver here. */
3700 	ill->ill_net_type = IRE_LOOPBACK;
3701 
3702 	/* Initialize the ipsq */
3703 	if (!ipsq_init(ill, B_FALSE))
3704 		goto done;
3705 
3706 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3707 	if (ipif == NULL)
3708 		goto done;
3709 
3710 	ill->ill_flags = ILLF_MULTICAST;
3711 
3712 	ov6addr = ipif->ipif_v6lcl_addr;
3713 	/* Set up default loopback address and mask. */
3714 	if (!isv6) {
3715 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3716 
3717 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3718 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3719 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3720 		    ipif->ipif_v6subnet);
3721 		ill->ill_flags |= ILLF_IPV4;
3722 	} else {
3723 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3724 		ipif->ipif_v6net_mask = ipv6_all_ones;
3725 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3726 		    ipif->ipif_v6subnet);
3727 		ill->ill_flags |= ILLF_IPV6;
3728 	}
3729 
3730 	/*
3731 	 * Chain us in at the end of the ill list. hold the ill
3732 	 * before we make it globally visible. 1 for the lookup.
3733 	 */
3734 	ill->ill_refcnt = 0;
3735 	ill_refhold(ill);
3736 
3737 	ill->ill_frag_count = 0;
3738 	ill->ill_frag_free_num_pkts = 0;
3739 	ill->ill_last_frag_clean_time = 0;
3740 
3741 	ipsq = ill->ill_phyint->phyint_ipsq;
3742 
3743 	ill_set_inputfn(ill);
3744 
3745 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3746 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3747 
3748 	/* Let SCTP know so that it can add this to its list */
3749 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3750 
3751 	/*
3752 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3753 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3754 	 * requires to be after ill_glist_insert() since we need the
3755 	 * ill_index set. Pass on ipv6_loopback as the old address.
3756 	 */
3757 	sctp_update_ipif_addr(ipif, ov6addr);
3758 
3759 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3760 
3761 	/*
3762 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3763 	 * If so, free our original one.
3764 	 */
3765 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3766 		ipsq_delete(ipsq);
3767 
3768 	if (ipst->ips_loopback_ksp == NULL) {
3769 		/* Export loopback interface statistics */
3770 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3771 		    ipif_loopback_name, "net",
3772 		    KSTAT_TYPE_NAMED, 2, 0,
3773 		    ipst->ips_netstack->netstack_stackid);
3774 		if (ipst->ips_loopback_ksp != NULL) {
3775 			ipst->ips_loopback_ksp->ks_update =
3776 			    loopback_kstat_update;
3777 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3778 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3779 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3780 			ipst->ips_loopback_ksp->ks_private =
3781 			    (void *)(uintptr_t)ipst->ips_netstack->
3782 			    netstack_stackid;
3783 			kstat_install(ipst->ips_loopback_ksp);
3784 		}
3785 	}
3786 
3787 	*did_alloc = B_TRUE;
3788 	rw_exit(&ipst->ips_ill_g_lock);
3789 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3790 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3791 	return (ill);
3792 done:
3793 	if (ill != NULL) {
3794 		if (ill->ill_phyint != NULL) {
3795 			ipsq = ill->ill_phyint->phyint_ipsq;
3796 			if (ipsq != NULL) {
3797 				ipsq->ipsq_phyint = NULL;
3798 				ipsq_delete(ipsq);
3799 			}
3800 			mi_free(ill->ill_phyint);
3801 		}
3802 		ill_free_mib(ill);
3803 		if (ill->ill_ipst != NULL)
3804 			netstack_rele(ill->ill_ipst->ips_netstack);
3805 		mi_free(ill);
3806 	}
3807 	rw_exit(&ipst->ips_ill_g_lock);
3808 	return (NULL);
3809 }
3810 
3811 /*
3812  * For IPP calls - use the ip_stack_t for global stack.
3813  */
3814 ill_t *
3815 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3816 {
3817 	ip_stack_t	*ipst;
3818 	ill_t		*ill;
3819 
3820 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3821 	if (ipst == NULL) {
3822 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3823 		return (NULL);
3824 	}
3825 
3826 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3827 	netstack_rele(ipst->ips_netstack);
3828 	return (ill);
3829 }
3830 
3831 /*
3832  * Return a pointer to the ill which matches the index and IP version type.
3833  */
3834 ill_t *
3835 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3836 {
3837 	ill_t	*ill;
3838 	phyint_t *phyi;
3839 
3840 	/*
3841 	 * Indexes are stored in the phyint - a common structure
3842 	 * to both IPv4 and IPv6.
3843 	 */
3844 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3845 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3846 	    (void *) &index, NULL);
3847 	if (phyi != NULL) {
3848 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3849 		if (ill != NULL) {
3850 			mutex_enter(&ill->ill_lock);
3851 			if (!ILL_IS_CONDEMNED(ill)) {
3852 				ill_refhold_locked(ill);
3853 				mutex_exit(&ill->ill_lock);
3854 				rw_exit(&ipst->ips_ill_g_lock);
3855 				return (ill);
3856 			}
3857 			mutex_exit(&ill->ill_lock);
3858 		}
3859 	}
3860 	rw_exit(&ipst->ips_ill_g_lock);
3861 	return (NULL);
3862 }
3863 
3864 /*
3865  * Verify whether or not an interface index is valid for the specified zoneid
3866  * to transmit packets.
3867  * It can be zero (meaning "reset") or an interface index assigned
3868  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3869  */
3870 boolean_t
3871 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3872     ip_stack_t *ipst)
3873 {
3874 	ill_t		*ill;
3875 
3876 	if (ifindex == 0)
3877 		return (B_TRUE);
3878 
3879 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3880 	if (ill == NULL)
3881 		return (B_FALSE);
3882 	if (IS_VNI(ill)) {
3883 		ill_refrele(ill);
3884 		return (B_FALSE);
3885 	}
3886 	ill_refrele(ill);
3887 	return (B_TRUE);
3888 }
3889 
3890 /*
3891  * Return the ifindex next in sequence after the passed in ifindex.
3892  * If there is no next ifindex for the given protocol, return 0.
3893  */
3894 uint_t
3895 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3896 {
3897 	phyint_t *phyi;
3898 	phyint_t *phyi_initial;
3899 	uint_t   ifindex;
3900 
3901 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3902 
3903 	if (index == 0) {
3904 		phyi = avl_first(
3905 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3906 	} else {
3907 		phyi = phyi_initial = avl_find(
3908 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3909 		    (void *) &index, NULL);
3910 	}
3911 
3912 	for (; phyi != NULL;
3913 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3914 	    phyi, AVL_AFTER)) {
3915 		/*
3916 		 * If we're not returning the first interface in the tree
3917 		 * and we still haven't moved past the phyint_t that
3918 		 * corresponds to index, avl_walk needs to be called again
3919 		 */
3920 		if (!((index != 0) && (phyi == phyi_initial))) {
3921 			if (isv6) {
3922 				if ((phyi->phyint_illv6) &&
3923 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3924 				    (phyi->phyint_illv6->ill_isv6 == 1))
3925 					break;
3926 			} else {
3927 				if ((phyi->phyint_illv4) &&
3928 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3929 				    (phyi->phyint_illv4->ill_isv6 == 0))
3930 					break;
3931 			}
3932 		}
3933 	}
3934 
3935 	rw_exit(&ipst->ips_ill_g_lock);
3936 
3937 	if (phyi != NULL)
3938 		ifindex = phyi->phyint_ifindex;
3939 	else
3940 		ifindex = 0;
3941 
3942 	return (ifindex);
3943 }
3944 
3945 /*
3946  * Return the ifindex for the named interface.
3947  * If there is no next ifindex for the interface, return 0.
3948  */
3949 uint_t
3950 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3951 {
3952 	phyint_t	*phyi;
3953 	avl_index_t	where = 0;
3954 	uint_t		ifindex;
3955 
3956 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3957 
3958 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3959 	    name, &where)) == NULL) {
3960 		rw_exit(&ipst->ips_ill_g_lock);
3961 		return (0);
3962 	}
3963 
3964 	ifindex = phyi->phyint_ifindex;
3965 
3966 	rw_exit(&ipst->ips_ill_g_lock);
3967 
3968 	return (ifindex);
3969 }
3970 
3971 /*
3972  * Return the ifindex to be used by upper layer protocols for instance
3973  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3974  */
3975 uint_t
3976 ill_get_upper_ifindex(const ill_t *ill)
3977 {
3978 	if (IS_UNDER_IPMP(ill))
3979 		return (ipmp_ill_get_ipmp_ifindex(ill));
3980 	else
3981 		return (ill->ill_phyint->phyint_ifindex);
3982 }
3983 
3984 
3985 /*
3986  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3987  * that gives a running thread a reference to the ill. This reference must be
3988  * released by the thread when it is done accessing the ill and related
3989  * objects. ill_refcnt can not be used to account for static references
3990  * such as other structures pointing to an ill. Callers must generally
3991  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3992  * or be sure that the ill is not being deleted or changing state before
3993  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3994  * ill won't change any of its critical state such as address, netmask etc.
3995  */
3996 void
3997 ill_refhold(ill_t *ill)
3998 {
3999 	mutex_enter(&ill->ill_lock);
4000 	ill->ill_refcnt++;
4001 	ILL_TRACE_REF(ill);
4002 	mutex_exit(&ill->ill_lock);
4003 }
4004 
4005 void
4006 ill_refhold_locked(ill_t *ill)
4007 {
4008 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4009 	ill->ill_refcnt++;
4010 	ILL_TRACE_REF(ill);
4011 }
4012 
4013 /* Returns true if we managed to get a refhold */
4014 boolean_t
4015 ill_check_and_refhold(ill_t *ill)
4016 {
4017 	mutex_enter(&ill->ill_lock);
4018 	if (!ILL_IS_CONDEMNED(ill)) {
4019 		ill_refhold_locked(ill);
4020 		mutex_exit(&ill->ill_lock);
4021 		return (B_TRUE);
4022 	}
4023 	mutex_exit(&ill->ill_lock);
4024 	return (B_FALSE);
4025 }
4026 
4027 /*
4028  * Must not be called while holding any locks. Otherwise if this is
4029  * the last reference to be released, there is a chance of recursive mutex
4030  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4031  * to restart an ioctl.
4032  */
4033 void
4034 ill_refrele(ill_t *ill)
4035 {
4036 	mutex_enter(&ill->ill_lock);
4037 	ASSERT(ill->ill_refcnt != 0);
4038 	ill->ill_refcnt--;
4039 	ILL_UNTRACE_REF(ill);
4040 	if (ill->ill_refcnt != 0) {
4041 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4042 		mutex_exit(&ill->ill_lock);
4043 		return;
4044 	}
4045 
4046 	/* Drops the ill_lock */
4047 	ipif_ill_refrele_tail(ill);
4048 }
4049 
4050 /*
4051  * Obtain a weak reference count on the ill. This reference ensures the
4052  * ill won't be freed, but the ill may change any of its critical state
4053  * such as netmask, address etc. Returns an error if the ill has started
4054  * closing.
4055  */
4056 boolean_t
4057 ill_waiter_inc(ill_t *ill)
4058 {
4059 	mutex_enter(&ill->ill_lock);
4060 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4061 		mutex_exit(&ill->ill_lock);
4062 		return (B_FALSE);
4063 	}
4064 	ill->ill_waiters++;
4065 	mutex_exit(&ill->ill_lock);
4066 	return (B_TRUE);
4067 }
4068 
4069 void
4070 ill_waiter_dcr(ill_t *ill)
4071 {
4072 	mutex_enter(&ill->ill_lock);
4073 	ill->ill_waiters--;
4074 	if (ill->ill_waiters == 0)
4075 		cv_broadcast(&ill->ill_cv);
4076 	mutex_exit(&ill->ill_lock);
4077 }
4078 
4079 /*
4080  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4081  * driver.  We construct best guess defaults for lower level information that
4082  * we need.  If an interface is brought up without injection of any overriding
4083  * information from outside, we have to be ready to go with these defaults.
4084  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4085  * we primarely want the dl_provider_style.
4086  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4087  * at which point we assume the other part of the information is valid.
4088  */
4089 void
4090 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4091 {
4092 	uchar_t		*brdcst_addr;
4093 	uint_t		brdcst_addr_length, phys_addr_length;
4094 	t_scalar_t	sap_length;
4095 	dl_info_ack_t	*dlia;
4096 	ip_m_t		*ipm;
4097 	dl_qos_cl_sel1_t *sel1;
4098 	int		min_mtu;
4099 
4100 	ASSERT(IAM_WRITER_ILL(ill));
4101 
4102 	/*
4103 	 * Till the ill is fully up  the ill is not globally visible.
4104 	 * So no need for a lock.
4105 	 */
4106 	dlia = (dl_info_ack_t *)mp->b_rptr;
4107 	ill->ill_mactype = dlia->dl_mac_type;
4108 
4109 	ipm = ip_m_lookup(dlia->dl_mac_type);
4110 	if (ipm == NULL) {
4111 		ipm = ip_m_lookup(DL_OTHER);
4112 		ASSERT(ipm != NULL);
4113 	}
4114 	ill->ill_media = ipm;
4115 
4116 	/*
4117 	 * When the new DLPI stuff is ready we'll pull lengths
4118 	 * from dlia.
4119 	 */
4120 	if (dlia->dl_version == DL_VERSION_2) {
4121 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4122 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4123 		    brdcst_addr_length);
4124 		if (brdcst_addr == NULL) {
4125 			brdcst_addr_length = 0;
4126 		}
4127 		sap_length = dlia->dl_sap_length;
4128 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4129 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4130 		    brdcst_addr_length, sap_length, phys_addr_length));
4131 	} else {
4132 		brdcst_addr_length = 6;
4133 		brdcst_addr = ip_six_byte_all_ones;
4134 		sap_length = -2;
4135 		phys_addr_length = brdcst_addr_length;
4136 	}
4137 
4138 	ill->ill_bcast_addr_length = brdcst_addr_length;
4139 	ill->ill_phys_addr_length = phys_addr_length;
4140 	ill->ill_sap_length = sap_length;
4141 
4142 	/*
4143 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4144 	 * but we must ensure a minimum IP MTU is used since other bits of
4145 	 * IP will fly apart otherwise.
4146 	 */
4147 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4148 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4149 	ill->ill_current_frag = ill->ill_max_frag;
4150 	ill->ill_mtu = ill->ill_max_frag;
4151 
4152 	ill->ill_type = ipm->ip_m_type;
4153 
4154 	if (!ill->ill_dlpi_style_set) {
4155 		if (dlia->dl_provider_style == DL_STYLE2)
4156 			ill->ill_needs_attach = 1;
4157 
4158 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4159 
4160 		/*
4161 		 * Allocate the first ipif on this ill.  We don't delay it
4162 		 * further as ioctl handling assumes at least one ipif exists.
4163 		 *
4164 		 * At this point we don't know whether the ill is v4 or v6.
4165 		 * We will know this whan the SIOCSLIFNAME happens and
4166 		 * the correct value for ill_isv6 will be assigned in
4167 		 * ipif_set_values(). We need to hold the ill lock and
4168 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4169 		 * the wakeup.
4170 		 */
4171 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4172 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4173 		mutex_enter(&ill->ill_lock);
4174 		ASSERT(ill->ill_dlpi_style_set == 0);
4175 		ill->ill_dlpi_style_set = 1;
4176 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4177 		cv_broadcast(&ill->ill_cv);
4178 		mutex_exit(&ill->ill_lock);
4179 		freemsg(mp);
4180 		return;
4181 	}
4182 	ASSERT(ill->ill_ipif != NULL);
4183 	/*
4184 	 * We know whether it is IPv4 or IPv6 now, as this is the
4185 	 * second DL_INFO_ACK we are recieving in response to the
4186 	 * DL_INFO_REQ sent in ipif_set_values.
4187 	 */
4188 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4189 	/*
4190 	 * Clear all the flags that were set based on ill_bcast_addr_length
4191 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4192 	 * changed now and we need to re-evaluate.
4193 	 */
4194 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4195 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4196 
4197 	/*
4198 	 * Free ill_bcast_mp as things could have changed now.
4199 	 *
4200 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4201 	 * with no underlying interfaces (and thus an unknown broadcast
4202 	 * address length), but we enforce that an interface is broadcast-
4203 	 * capable as part of allowing it to join a group.
4204 	 */
4205 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4206 		if (ill->ill_bcast_mp != NULL)
4207 			freemsg(ill->ill_bcast_mp);
4208 		ill->ill_net_type = IRE_IF_NORESOLVER;
4209 
4210 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4211 		    ill->ill_phys_addr_length,
4212 		    ill->ill_sap,
4213 		    ill->ill_sap_length);
4214 
4215 		if (ill->ill_isv6)
4216 			/*
4217 			 * Note: xresolv interfaces will eventually need NOARP
4218 			 * set here as well, but that will require those
4219 			 * external resolvers to have some knowledge of
4220 			 * that flag and act appropriately. Not to be changed
4221 			 * at present.
4222 			 */
4223 			ill->ill_flags |= ILLF_NONUD;
4224 		else
4225 			ill->ill_flags |= ILLF_NOARP;
4226 
4227 		if (ill->ill_mactype == SUNW_DL_VNI) {
4228 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4229 		} else if (ill->ill_phys_addr_length == 0 ||
4230 		    ill->ill_mactype == DL_IPV4 ||
4231 		    ill->ill_mactype == DL_IPV6) {
4232 			/*
4233 			 * The underying link is point-to-point, so mark the
4234 			 * interface as such.  We can do IP multicast over
4235 			 * such a link since it transmits all network-layer
4236 			 * packets to the remote side the same way.
4237 			 */
4238 			ill->ill_flags |= ILLF_MULTICAST;
4239 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4240 		}
4241 	} else {
4242 		ill->ill_net_type = IRE_IF_RESOLVER;
4243 		if (ill->ill_bcast_mp != NULL)
4244 			freemsg(ill->ill_bcast_mp);
4245 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4246 		    ill->ill_bcast_addr_length, ill->ill_sap,
4247 		    ill->ill_sap_length);
4248 		/*
4249 		 * Later detect lack of DLPI driver multicast
4250 		 * capability by catching DL_ENABMULTI errors in
4251 		 * ip_rput_dlpi.
4252 		 */
4253 		ill->ill_flags |= ILLF_MULTICAST;
4254 		if (!ill->ill_isv6)
4255 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4256 	}
4257 
4258 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4259 	if (ill->ill_mactype == SUNW_DL_IPMP)
4260 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4261 
4262 	/* By default an interface does not support any CoS marking */
4263 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4264 
4265 	/*
4266 	 * If we get QoS information in DL_INFO_ACK, the device supports
4267 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4268 	 */
4269 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4270 	    dlia->dl_qos_length);
4271 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4272 		ill->ill_flags |= ILLF_COS_ENABLED;
4273 	}
4274 
4275 	/* Clear any previous error indication. */
4276 	ill->ill_error = 0;
4277 	freemsg(mp);
4278 }
4279 
4280 /*
4281  * Perform various checks to verify that an address would make sense as a
4282  * local, remote, or subnet interface address.
4283  */
4284 static boolean_t
4285 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4286 {
4287 	ipaddr_t	net_mask;
4288 
4289 	/*
4290 	 * Don't allow all zeroes, or all ones, but allow
4291 	 * all ones netmask.
4292 	 */
4293 	if ((net_mask = ip_net_mask(addr)) == 0)
4294 		return (B_FALSE);
4295 	/* A given netmask overrides the "guess" netmask */
4296 	if (subnet_mask != 0)
4297 		net_mask = subnet_mask;
4298 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4299 	    (addr == (addr | ~net_mask)))) {
4300 		return (B_FALSE);
4301 	}
4302 
4303 	/*
4304 	 * Even if the netmask is all ones, we do not allow address to be
4305 	 * 255.255.255.255
4306 	 */
4307 	if (addr == INADDR_BROADCAST)
4308 		return (B_FALSE);
4309 
4310 	if (CLASSD(addr))
4311 		return (B_FALSE);
4312 
4313 	return (B_TRUE);
4314 }
4315 
4316 #define	V6_IPIF_LINKLOCAL(p)	\
4317 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4318 
4319 /*
4320  * Compare two given ipifs and check if the second one is better than
4321  * the first one using the order of preference (not taking deprecated
4322  * into acount) specified in ipif_lookup_multicast().
4323  */
4324 static boolean_t
4325 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4326 {
4327 	/* Check the least preferred first. */
4328 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4329 		/* If both ipifs are the same, use the first one. */
4330 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4331 			return (B_FALSE);
4332 		else
4333 			return (B_TRUE);
4334 	}
4335 
4336 	/* For IPv6, check for link local address. */
4337 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4338 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4339 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4340 			/* The second one is equal or less preferred. */
4341 			return (B_FALSE);
4342 		} else {
4343 			return (B_TRUE);
4344 		}
4345 	}
4346 
4347 	/* Then check for point to point interface. */
4348 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4349 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4350 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4351 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4352 			return (B_FALSE);
4353 		} else {
4354 			return (B_TRUE);
4355 		}
4356 	}
4357 
4358 	/* old_ipif is a normal interface, so no need to use the new one. */
4359 	return (B_FALSE);
4360 }
4361 
4362 /*
4363  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4364  * The ipif must be up, and its ill must multicast-capable, not
4365  * condemned, not an underlying interface in an IPMP group, and
4366  * not a VNI interface.  Order of preference:
4367  *
4368  * 	1a. normal
4369  * 	1b. normal, but deprecated
4370  * 	2a. point to point
4371  * 	2b. point to point, but deprecated
4372  * 	3a. link local
4373  * 	3b. link local, but deprecated
4374  * 	4. loopback.
4375  */
4376 static ipif_t *
4377 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4378 {
4379 	ill_t			*ill;
4380 	ill_walk_context_t	ctx;
4381 	ipif_t			*ipif;
4382 	ipif_t			*saved_ipif = NULL;
4383 	ipif_t			*dep_ipif = NULL;
4384 
4385 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4386 	if (isv6)
4387 		ill = ILL_START_WALK_V6(&ctx, ipst);
4388 	else
4389 		ill = ILL_START_WALK_V4(&ctx, ipst);
4390 
4391 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4392 		mutex_enter(&ill->ill_lock);
4393 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4394 		    ILL_IS_CONDEMNED(ill) ||
4395 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4396 			mutex_exit(&ill->ill_lock);
4397 			continue;
4398 		}
4399 		for (ipif = ill->ill_ipif; ipif != NULL;
4400 		    ipif = ipif->ipif_next) {
4401 			if (zoneid != ipif->ipif_zoneid &&
4402 			    zoneid != ALL_ZONES &&
4403 			    ipif->ipif_zoneid != ALL_ZONES) {
4404 				continue;
4405 			}
4406 			if (!(ipif->ipif_flags & IPIF_UP) ||
4407 			    IPIF_IS_CONDEMNED(ipif)) {
4408 				continue;
4409 			}
4410 
4411 			/*
4412 			 * Found one candidate.  If it is deprecated,
4413 			 * remember it in dep_ipif.  If it is not deprecated,
4414 			 * remember it in saved_ipif.
4415 			 */
4416 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4417 				if (dep_ipif == NULL) {
4418 					dep_ipif = ipif;
4419 				} else if (ipif_comp_multi(dep_ipif, ipif,
4420 				    isv6)) {
4421 					/*
4422 					 * If the previous dep_ipif does not
4423 					 * belong to the same ill, we've done
4424 					 * a ipif_refhold() on it.  So we need
4425 					 * to release it.
4426 					 */
4427 					if (dep_ipif->ipif_ill != ill)
4428 						ipif_refrele(dep_ipif);
4429 					dep_ipif = ipif;
4430 				}
4431 				continue;
4432 			}
4433 			if (saved_ipif == NULL) {
4434 				saved_ipif = ipif;
4435 			} else {
4436 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4437 					if (saved_ipif->ipif_ill != ill)
4438 						ipif_refrele(saved_ipif);
4439 					saved_ipif = ipif;
4440 				}
4441 			}
4442 		}
4443 		/*
4444 		 * Before going to the next ill, do a ipif_refhold() on the
4445 		 * saved ones.
4446 		 */
4447 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4448 			ipif_refhold_locked(saved_ipif);
4449 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4450 			ipif_refhold_locked(dep_ipif);
4451 		mutex_exit(&ill->ill_lock);
4452 	}
4453 	rw_exit(&ipst->ips_ill_g_lock);
4454 
4455 	/*
4456 	 * If we have only the saved_ipif, return it.  But if we have both
4457 	 * saved_ipif and dep_ipif, check to see which one is better.
4458 	 */
4459 	if (saved_ipif != NULL) {
4460 		if (dep_ipif != NULL) {
4461 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4462 				ipif_refrele(saved_ipif);
4463 				return (dep_ipif);
4464 			} else {
4465 				ipif_refrele(dep_ipif);
4466 				return (saved_ipif);
4467 			}
4468 		}
4469 		return (saved_ipif);
4470 	} else {
4471 		return (dep_ipif);
4472 	}
4473 }
4474 
4475 ill_t *
4476 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4477 {
4478 	ipif_t *ipif;
4479 	ill_t *ill;
4480 
4481 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4482 	if (ipif == NULL)
4483 		return (NULL);
4484 
4485 	ill = ipif->ipif_ill;
4486 	ill_refhold(ill);
4487 	ipif_refrele(ipif);
4488 	return (ill);
4489 }
4490 
4491 /*
4492  * This function is called when an application does not specify an interface
4493  * to be used for multicast traffic (joining a group/sending data).  It
4494  * calls ire_lookup_multi() to look for an interface route for the
4495  * specified multicast group.  Doing this allows the administrator to add
4496  * prefix routes for multicast to indicate which interface to be used for
4497  * multicast traffic in the above scenario.  The route could be for all
4498  * multicast (224.0/4), for a single multicast group (a /32 route) or
4499  * anything in between.  If there is no such multicast route, we just find
4500  * any multicast capable interface and return it.  The returned ipif
4501  * is refhold'ed.
4502  *
4503  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4504  * unicast table. This is used by CGTP.
4505  */
4506 ill_t *
4507 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4508     boolean_t *multirtp, ipaddr_t *setsrcp)
4509 {
4510 	ill_t			*ill;
4511 
4512 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4513 	if (ill != NULL)
4514 		return (ill);
4515 
4516 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4517 }
4518 
4519 /*
4520  * Look for an ipif with the specified interface address and destination.
4521  * The destination address is used only for matching point-to-point interfaces.
4522  */
4523 ipif_t *
4524 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4525 {
4526 	ipif_t	*ipif;
4527 	ill_t	*ill;
4528 	ill_walk_context_t ctx;
4529 
4530 	/*
4531 	 * First match all the point-to-point interfaces
4532 	 * before looking at non-point-to-point interfaces.
4533 	 * This is done to avoid returning non-point-to-point
4534 	 * ipif instead of unnumbered point-to-point ipif.
4535 	 */
4536 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4537 	ill = ILL_START_WALK_V4(&ctx, ipst);
4538 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4539 		mutex_enter(&ill->ill_lock);
4540 		for (ipif = ill->ill_ipif; ipif != NULL;
4541 		    ipif = ipif->ipif_next) {
4542 			/* Allow the ipif to be down */
4543 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4544 			    (ipif->ipif_lcl_addr == if_addr) &&
4545 			    (ipif->ipif_pp_dst_addr == dst)) {
4546 				if (!IPIF_IS_CONDEMNED(ipif)) {
4547 					ipif_refhold_locked(ipif);
4548 					mutex_exit(&ill->ill_lock);
4549 					rw_exit(&ipst->ips_ill_g_lock);
4550 					return (ipif);
4551 				}
4552 			}
4553 		}
4554 		mutex_exit(&ill->ill_lock);
4555 	}
4556 	rw_exit(&ipst->ips_ill_g_lock);
4557 
4558 	/* lookup the ipif based on interface address */
4559 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4560 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4561 	return (ipif);
4562 }
4563 
4564 /*
4565  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4566  */
4567 static ipif_t *
4568 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4569     zoneid_t zoneid, ip_stack_t *ipst)
4570 {
4571 	ipif_t  *ipif;
4572 	ill_t   *ill;
4573 	boolean_t ptp = B_FALSE;
4574 	ill_walk_context_t	ctx;
4575 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4576 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4577 
4578 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4579 	/*
4580 	 * Repeat twice, first based on local addresses and
4581 	 * next time for pointopoint.
4582 	 */
4583 repeat:
4584 	ill = ILL_START_WALK_V4(&ctx, ipst);
4585 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4586 		if (match_ill != NULL && ill != match_ill &&
4587 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4588 			continue;
4589 		}
4590 		mutex_enter(&ill->ill_lock);
4591 		for (ipif = ill->ill_ipif; ipif != NULL;
4592 		    ipif = ipif->ipif_next) {
4593 			if (zoneid != ALL_ZONES &&
4594 			    zoneid != ipif->ipif_zoneid &&
4595 			    ipif->ipif_zoneid != ALL_ZONES)
4596 				continue;
4597 
4598 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4599 				continue;
4600 
4601 			/* Allow the ipif to be down */
4602 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4603 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4604 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4605 			    (ipif->ipif_pp_dst_addr == addr))) {
4606 				if (!IPIF_IS_CONDEMNED(ipif)) {
4607 					ipif_refhold_locked(ipif);
4608 					mutex_exit(&ill->ill_lock);
4609 					rw_exit(&ipst->ips_ill_g_lock);
4610 					return (ipif);
4611 				}
4612 			}
4613 		}
4614 		mutex_exit(&ill->ill_lock);
4615 	}
4616 
4617 	/* If we already did the ptp case, then we are done */
4618 	if (ptp) {
4619 		rw_exit(&ipst->ips_ill_g_lock);
4620 		return (NULL);
4621 	}
4622 	ptp = B_TRUE;
4623 	goto repeat;
4624 }
4625 
4626 /*
4627  * Lookup an ipif with the specified address.  For point-to-point links we
4628  * look for matches on either the destination address or the local address,
4629  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4630  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4631  * (or illgrp if `match_ill' is in an IPMP group).
4632  */
4633 ipif_t *
4634 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4635     ip_stack_t *ipst)
4636 {
4637 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4638 	    zoneid, ipst));
4639 }
4640 
4641 /*
4642  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4643  * except that we will only return an address if it is not marked as
4644  * IPIF_DUPLICATE
4645  */
4646 ipif_t *
4647 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4648     ip_stack_t *ipst)
4649 {
4650 	return (ipif_lookup_addr_common(addr, match_ill,
4651 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4652 	    zoneid, ipst));
4653 }
4654 
4655 /*
4656  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4657  * `match_ill' across the IPMP group.  This function is only needed in some
4658  * corner-cases; almost everything should use ipif_lookup_addr().
4659  */
4660 ipif_t *
4661 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4662 {
4663 	ASSERT(match_ill != NULL);
4664 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4665 	    ipst));
4666 }
4667 
4668 /*
4669  * Look for an ipif with the specified address. For point-point links
4670  * we look for matches on either the destination address and the local
4671  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4672  * is set.
4673  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4674  * ill (or illgrp if `match_ill' is in an IPMP group).
4675  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4676  */
4677 zoneid_t
4678 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4679 {
4680 	zoneid_t zoneid;
4681 	ipif_t  *ipif;
4682 	ill_t   *ill;
4683 	boolean_t ptp = B_FALSE;
4684 	ill_walk_context_t	ctx;
4685 
4686 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4687 	/*
4688 	 * Repeat twice, first based on local addresses and
4689 	 * next time for pointopoint.
4690 	 */
4691 repeat:
4692 	ill = ILL_START_WALK_V4(&ctx, ipst);
4693 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4694 		if (match_ill != NULL && ill != match_ill &&
4695 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4696 			continue;
4697 		}
4698 		mutex_enter(&ill->ill_lock);
4699 		for (ipif = ill->ill_ipif; ipif != NULL;
4700 		    ipif = ipif->ipif_next) {
4701 			/* Allow the ipif to be down */
4702 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4703 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4704 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4705 			    (ipif->ipif_pp_dst_addr == addr)) &&
4706 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4707 				zoneid = ipif->ipif_zoneid;
4708 				mutex_exit(&ill->ill_lock);
4709 				rw_exit(&ipst->ips_ill_g_lock);
4710 				/*
4711 				 * If ipif_zoneid was ALL_ZONES then we have
4712 				 * a trusted extensions shared IP address.
4713 				 * In that case GLOBAL_ZONEID works to send.
4714 				 */
4715 				if (zoneid == ALL_ZONES)
4716 					zoneid = GLOBAL_ZONEID;
4717 				return (zoneid);
4718 			}
4719 		}
4720 		mutex_exit(&ill->ill_lock);
4721 	}
4722 
4723 	/* If we already did the ptp case, then we are done */
4724 	if (ptp) {
4725 		rw_exit(&ipst->ips_ill_g_lock);
4726 		return (ALL_ZONES);
4727 	}
4728 	ptp = B_TRUE;
4729 	goto repeat;
4730 }
4731 
4732 /*
4733  * Look for an ipif that matches the specified remote address i.e. the
4734  * ipif that would receive the specified packet.
4735  * First look for directly connected interfaces and then do a recursive
4736  * IRE lookup and pick the first ipif corresponding to the source address in the
4737  * ire.
4738  * Returns: held ipif
4739  *
4740  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4741  */
4742 ipif_t *
4743 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4744 {
4745 	ipif_t	*ipif;
4746 
4747 	ASSERT(!ill->ill_isv6);
4748 
4749 	/*
4750 	 * Someone could be changing this ipif currently or change it
4751 	 * after we return this. Thus  a few packets could use the old
4752 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4753 	 * will atomically be updated or cleaned up with the new value
4754 	 * Thus we don't need a lock to check the flags or other attrs below.
4755 	 */
4756 	mutex_enter(&ill->ill_lock);
4757 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4758 		if (IPIF_IS_CONDEMNED(ipif))
4759 			continue;
4760 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4761 		    ipif->ipif_zoneid != ALL_ZONES)
4762 			continue;
4763 		/* Allow the ipif to be down */
4764 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4765 			if ((ipif->ipif_pp_dst_addr == addr) ||
4766 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4767 			    ipif->ipif_lcl_addr == addr)) {
4768 				ipif_refhold_locked(ipif);
4769 				mutex_exit(&ill->ill_lock);
4770 				return (ipif);
4771 			}
4772 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4773 			ipif_refhold_locked(ipif);
4774 			mutex_exit(&ill->ill_lock);
4775 			return (ipif);
4776 		}
4777 	}
4778 	mutex_exit(&ill->ill_lock);
4779 	/*
4780 	 * For a remote destination it isn't possible to nail down a particular
4781 	 * ipif.
4782 	 */
4783 
4784 	/* Pick the first interface */
4785 	ipif = ipif_get_next_ipif(NULL, ill);
4786 	return (ipif);
4787 }
4788 
4789 /*
4790  * This func does not prevent refcnt from increasing. But if
4791  * the caller has taken steps to that effect, then this func
4792  * can be used to determine whether the ill has become quiescent
4793  */
4794 static boolean_t
4795 ill_is_quiescent(ill_t *ill)
4796 {
4797 	ipif_t	*ipif;
4798 
4799 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4800 
4801 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4802 		if (ipif->ipif_refcnt != 0)
4803 			return (B_FALSE);
4804 	}
4805 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4806 		return (B_FALSE);
4807 	}
4808 	return (B_TRUE);
4809 }
4810 
4811 boolean_t
4812 ill_is_freeable(ill_t *ill)
4813 {
4814 	ipif_t	*ipif;
4815 
4816 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4817 
4818 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4819 		if (ipif->ipif_refcnt != 0) {
4820 			return (B_FALSE);
4821 		}
4822 	}
4823 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4824 		return (B_FALSE);
4825 	}
4826 	return (B_TRUE);
4827 }
4828 
4829 /*
4830  * This func does not prevent refcnt from increasing. But if
4831  * the caller has taken steps to that effect, then this func
4832  * can be used to determine whether the ipif has become quiescent
4833  */
4834 static boolean_t
4835 ipif_is_quiescent(ipif_t *ipif)
4836 {
4837 	ill_t *ill;
4838 
4839 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4840 
4841 	if (ipif->ipif_refcnt != 0)
4842 		return (B_FALSE);
4843 
4844 	ill = ipif->ipif_ill;
4845 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4846 	    ill->ill_logical_down) {
4847 		return (B_TRUE);
4848 	}
4849 
4850 	/* This is the last ipif going down or being deleted on this ill */
4851 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4852 		return (B_FALSE);
4853 	}
4854 
4855 	return (B_TRUE);
4856 }
4857 
4858 /*
4859  * return true if the ipif can be destroyed: the ipif has to be quiescent
4860  * with zero references from ire/ilm to it.
4861  */
4862 static boolean_t
4863 ipif_is_freeable(ipif_t *ipif)
4864 {
4865 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4866 	ASSERT(ipif->ipif_id != 0);
4867 	return (ipif->ipif_refcnt == 0);
4868 }
4869 
4870 /*
4871  * The ipif/ill/ire has been refreled. Do the tail processing.
4872  * Determine if the ipif or ill in question has become quiescent and if so
4873  * wakeup close and/or restart any queued pending ioctl that is waiting
4874  * for the ipif_down (or ill_down)
4875  */
4876 void
4877 ipif_ill_refrele_tail(ill_t *ill)
4878 {
4879 	mblk_t	*mp;
4880 	conn_t	*connp;
4881 	ipsq_t	*ipsq;
4882 	ipxop_t	*ipx;
4883 	ipif_t	*ipif;
4884 	dl_notify_ind_t *dlindp;
4885 
4886 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4887 
4888 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4889 		/* ip_modclose() may be waiting */
4890 		cv_broadcast(&ill->ill_cv);
4891 	}
4892 
4893 	ipsq = ill->ill_phyint->phyint_ipsq;
4894 	mutex_enter(&ipsq->ipsq_lock);
4895 	ipx = ipsq->ipsq_xop;
4896 	mutex_enter(&ipx->ipx_lock);
4897 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4898 		goto unlock;
4899 
4900 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4901 
4902 	ipif = ipx->ipx_pending_ipif;
4903 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4904 		goto unlock;
4905 
4906 	switch (ipx->ipx_waitfor) {
4907 	case IPIF_DOWN:
4908 		if (!ipif_is_quiescent(ipif))
4909 			goto unlock;
4910 		break;
4911 	case IPIF_FREE:
4912 		if (!ipif_is_freeable(ipif))
4913 			goto unlock;
4914 		break;
4915 	case ILL_DOWN:
4916 		if (!ill_is_quiescent(ill))
4917 			goto unlock;
4918 		break;
4919 	case ILL_FREE:
4920 		/*
4921 		 * ILL_FREE is only for loopback; normal ill teardown waits
4922 		 * synchronously in ip_modclose() without using ipx_waitfor,
4923 		 * handled by the cv_broadcast() at the top of this function.
4924 		 */
4925 		if (!ill_is_freeable(ill))
4926 			goto unlock;
4927 		break;
4928 	default:
4929 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4930 		    (void *)ipsq, ipx->ipx_waitfor);
4931 	}
4932 
4933 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4934 	mutex_exit(&ipx->ipx_lock);
4935 	mp = ipsq_pending_mp_get(ipsq, &connp);
4936 	mutex_exit(&ipsq->ipsq_lock);
4937 	mutex_exit(&ill->ill_lock);
4938 
4939 	ASSERT(mp != NULL);
4940 	/*
4941 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4942 	 * we can only get here when the current operation decides it
4943 	 * it needs to quiesce via ipsq_pending_mp_add().
4944 	 */
4945 	switch (mp->b_datap->db_type) {
4946 	case M_PCPROTO:
4947 	case M_PROTO:
4948 		/*
4949 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4950 		 */
4951 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4952 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4953 
4954 		switch (dlindp->dl_notification) {
4955 		case DL_NOTE_PHYS_ADDR:
4956 			qwriter_ip(ill, ill->ill_rq, mp,
4957 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4958 			return;
4959 		case DL_NOTE_REPLUMB:
4960 			qwriter_ip(ill, ill->ill_rq, mp,
4961 			    ill_replumb_tail, CUR_OP, B_TRUE);
4962 			return;
4963 		default:
4964 			ASSERT(0);
4965 			ill_refrele(ill);
4966 		}
4967 		break;
4968 
4969 	case M_ERROR:
4970 	case M_HANGUP:
4971 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4972 		    B_TRUE);
4973 		return;
4974 
4975 	case M_IOCTL:
4976 	case M_IOCDATA:
4977 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4978 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4979 		return;
4980 
4981 	default:
4982 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4983 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4984 	}
4985 	return;
4986 unlock:
4987 	mutex_exit(&ipsq->ipsq_lock);
4988 	mutex_exit(&ipx->ipx_lock);
4989 	mutex_exit(&ill->ill_lock);
4990 }
4991 
4992 #ifdef DEBUG
4993 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4994 static void
4995 th_trace_rrecord(th_trace_t *th_trace)
4996 {
4997 	tr_buf_t *tr_buf;
4998 	uint_t lastref;
4999 
5000 	lastref = th_trace->th_trace_lastref;
5001 	lastref++;
5002 	if (lastref == TR_BUF_MAX)
5003 		lastref = 0;
5004 	th_trace->th_trace_lastref = lastref;
5005 	tr_buf = &th_trace->th_trbuf[lastref];
5006 	tr_buf->tr_time = ddi_get_lbolt();
5007 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5008 }
5009 
5010 static void
5011 th_trace_free(void *value)
5012 {
5013 	th_trace_t *th_trace = value;
5014 
5015 	ASSERT(th_trace->th_refcnt == 0);
5016 	kmem_free(th_trace, sizeof (*th_trace));
5017 }
5018 
5019 /*
5020  * Find or create the per-thread hash table used to track object references.
5021  * The ipst argument is NULL if we shouldn't allocate.
5022  *
5023  * Accesses per-thread data, so there's no need to lock here.
5024  */
5025 static mod_hash_t *
5026 th_trace_gethash(ip_stack_t *ipst)
5027 {
5028 	th_hash_t *thh;
5029 
5030 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5031 		mod_hash_t *mh;
5032 		char name[256];
5033 		size_t objsize, rshift;
5034 		int retv;
5035 
5036 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5037 			return (NULL);
5038 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5039 		    (void *)curthread);
5040 
5041 		/*
5042 		 * We use mod_hash_create_extended here rather than the more
5043 		 * obvious mod_hash_create_ptrhash because the latter has a
5044 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5045 		 * block.
5046 		 */
5047 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5048 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5049 		rshift = highbit(objsize);
5050 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5051 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5052 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5053 		if (mh == NULL) {
5054 			kmem_free(thh, sizeof (*thh));
5055 			return (NULL);
5056 		}
5057 		thh->thh_hash = mh;
5058 		thh->thh_ipst = ipst;
5059 		/*
5060 		 * We trace ills, ipifs, ires, and nces.  All of these are
5061 		 * per-IP-stack, so the lock on the thread list is as well.
5062 		 */
5063 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5064 		list_insert_tail(&ip_thread_list, thh);
5065 		rw_exit(&ip_thread_rwlock);
5066 		retv = tsd_set(ip_thread_data, thh);
5067 		ASSERT(retv == 0);
5068 	}
5069 	return (thh != NULL ? thh->thh_hash : NULL);
5070 }
5071 
5072 boolean_t
5073 th_trace_ref(const void *obj, ip_stack_t *ipst)
5074 {
5075 	th_trace_t *th_trace;
5076 	mod_hash_t *mh;
5077 	mod_hash_val_t val;
5078 
5079 	if ((mh = th_trace_gethash(ipst)) == NULL)
5080 		return (B_FALSE);
5081 
5082 	/*
5083 	 * Attempt to locate the trace buffer for this obj and thread.
5084 	 * If it does not exist, then allocate a new trace buffer and
5085 	 * insert into the hash.
5086 	 */
5087 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5088 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5089 		if (th_trace == NULL)
5090 			return (B_FALSE);
5091 
5092 		th_trace->th_id = curthread;
5093 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5094 		    (mod_hash_val_t)th_trace) != 0) {
5095 			kmem_free(th_trace, sizeof (th_trace_t));
5096 			return (B_FALSE);
5097 		}
5098 	} else {
5099 		th_trace = (th_trace_t *)val;
5100 	}
5101 
5102 	ASSERT(th_trace->th_refcnt >= 0 &&
5103 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5104 
5105 	th_trace->th_refcnt++;
5106 	th_trace_rrecord(th_trace);
5107 	return (B_TRUE);
5108 }
5109 
5110 /*
5111  * For the purpose of tracing a reference release, we assume that global
5112  * tracing is always on and that the same thread initiated the reference hold
5113  * is releasing.
5114  */
5115 void
5116 th_trace_unref(const void *obj)
5117 {
5118 	int retv;
5119 	mod_hash_t *mh;
5120 	th_trace_t *th_trace;
5121 	mod_hash_val_t val;
5122 
5123 	mh = th_trace_gethash(NULL);
5124 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5125 	ASSERT(retv == 0);
5126 	th_trace = (th_trace_t *)val;
5127 
5128 	ASSERT(th_trace->th_refcnt > 0);
5129 	th_trace->th_refcnt--;
5130 	th_trace_rrecord(th_trace);
5131 }
5132 
5133 /*
5134  * If tracing has been disabled, then we assume that the reference counts are
5135  * now useless, and we clear them out before destroying the entries.
5136  */
5137 void
5138 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5139 {
5140 	th_hash_t	*thh;
5141 	mod_hash_t	*mh;
5142 	mod_hash_val_t	val;
5143 	th_trace_t	*th_trace;
5144 	int		retv;
5145 
5146 	rw_enter(&ip_thread_rwlock, RW_READER);
5147 	for (thh = list_head(&ip_thread_list); thh != NULL;
5148 	    thh = list_next(&ip_thread_list, thh)) {
5149 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5150 		    &val) == 0) {
5151 			th_trace = (th_trace_t *)val;
5152 			if (trace_disable)
5153 				th_trace->th_refcnt = 0;
5154 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5155 			ASSERT(retv == 0);
5156 		}
5157 	}
5158 	rw_exit(&ip_thread_rwlock);
5159 }
5160 
5161 void
5162 ipif_trace_ref(ipif_t *ipif)
5163 {
5164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5165 
5166 	if (ipif->ipif_trace_disable)
5167 		return;
5168 
5169 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5170 		ipif->ipif_trace_disable = B_TRUE;
5171 		ipif_trace_cleanup(ipif);
5172 	}
5173 }
5174 
5175 void
5176 ipif_untrace_ref(ipif_t *ipif)
5177 {
5178 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5179 
5180 	if (!ipif->ipif_trace_disable)
5181 		th_trace_unref(ipif);
5182 }
5183 
5184 void
5185 ill_trace_ref(ill_t *ill)
5186 {
5187 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5188 
5189 	if (ill->ill_trace_disable)
5190 		return;
5191 
5192 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5193 		ill->ill_trace_disable = B_TRUE;
5194 		ill_trace_cleanup(ill);
5195 	}
5196 }
5197 
5198 void
5199 ill_untrace_ref(ill_t *ill)
5200 {
5201 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5202 
5203 	if (!ill->ill_trace_disable)
5204 		th_trace_unref(ill);
5205 }
5206 
5207 /*
5208  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5209  * failure, ipif_trace_disable is set.
5210  */
5211 static void
5212 ipif_trace_cleanup(const ipif_t *ipif)
5213 {
5214 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5215 }
5216 
5217 /*
5218  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5219  * failure, ill_trace_disable is set.
5220  */
5221 static void
5222 ill_trace_cleanup(const ill_t *ill)
5223 {
5224 	th_trace_cleanup(ill, ill->ill_trace_disable);
5225 }
5226 #endif /* DEBUG */
5227 
5228 void
5229 ipif_refhold_locked(ipif_t *ipif)
5230 {
5231 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5232 	ipif->ipif_refcnt++;
5233 	IPIF_TRACE_REF(ipif);
5234 }
5235 
5236 void
5237 ipif_refhold(ipif_t *ipif)
5238 {
5239 	ill_t	*ill;
5240 
5241 	ill = ipif->ipif_ill;
5242 	mutex_enter(&ill->ill_lock);
5243 	ipif->ipif_refcnt++;
5244 	IPIF_TRACE_REF(ipif);
5245 	mutex_exit(&ill->ill_lock);
5246 }
5247 
5248 /*
5249  * Must not be called while holding any locks. Otherwise if this is
5250  * the last reference to be released there is a chance of recursive mutex
5251  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5252  * to restart an ioctl.
5253  */
5254 void
5255 ipif_refrele(ipif_t *ipif)
5256 {
5257 	ill_t	*ill;
5258 
5259 	ill = ipif->ipif_ill;
5260 
5261 	mutex_enter(&ill->ill_lock);
5262 	ASSERT(ipif->ipif_refcnt != 0);
5263 	ipif->ipif_refcnt--;
5264 	IPIF_UNTRACE_REF(ipif);
5265 	if (ipif->ipif_refcnt != 0) {
5266 		mutex_exit(&ill->ill_lock);
5267 		return;
5268 	}
5269 
5270 	/* Drops the ill_lock */
5271 	ipif_ill_refrele_tail(ill);
5272 }
5273 
5274 ipif_t *
5275 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5276 {
5277 	ipif_t	*ipif;
5278 
5279 	mutex_enter(&ill->ill_lock);
5280 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5281 	    ipif != NULL; ipif = ipif->ipif_next) {
5282 		if (IPIF_IS_CONDEMNED(ipif))
5283 			continue;
5284 		ipif_refhold_locked(ipif);
5285 		mutex_exit(&ill->ill_lock);
5286 		return (ipif);
5287 	}
5288 	mutex_exit(&ill->ill_lock);
5289 	return (NULL);
5290 }
5291 
5292 /*
5293  * TODO: make this table extendible at run time
5294  * Return a pointer to the mac type info for 'mac_type'
5295  */
5296 static ip_m_t *
5297 ip_m_lookup(t_uscalar_t mac_type)
5298 {
5299 	ip_m_t	*ipm;
5300 
5301 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5302 		if (ipm->ip_m_mac_type == mac_type)
5303 			return (ipm);
5304 	return (NULL);
5305 }
5306 
5307 /*
5308  * Make a link layer address from the multicast IP address *addr.
5309  * To form the link layer address, invoke the ip_m_v*mapping function
5310  * associated with the link-layer type.
5311  */
5312 void
5313 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5314 {
5315 	ip_m_t *ipm;
5316 
5317 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5318 		return;
5319 
5320 	ASSERT(addr != NULL);
5321 
5322 	ipm = ip_m_lookup(ill->ill_mactype);
5323 	if (ipm == NULL ||
5324 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5325 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5326 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5327 		    ill->ill_name, ill->ill_mactype));
5328 		return;
5329 	}
5330 	if (ill->ill_isv6)
5331 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5332 	else
5333 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5334 }
5335 
5336 /*
5337  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5338  * Otherwise returns B_TRUE.
5339  *
5340  * The netmask can be verified to be contiguous with 32 shifts and or
5341  * operations. Take the contiguous mask (in host byte order) and compute
5342  * 	mask | mask << 1 | mask << 2 | ... | mask << 31
5343  * the result will be the same as the 'mask' for contiguous mask.
5344  */
5345 static boolean_t
5346 ip_contiguous_mask(uint32_t mask)
5347 {
5348 	uint32_t	m = mask;
5349 	int		i;
5350 
5351 	for (i = 1; i < 32; i++)
5352 		m |= (mask << i);
5353 
5354 	return (m == mask);
5355 }
5356 
5357 /*
5358  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5359  * ill is passed in to associate it with the correct interface.
5360  * If ire_arg is set, then we return the held IRE in that location.
5361  */
5362 int
5363 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5364     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5365     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5366 {
5367 	ire_t	*ire, *nire;
5368 	ire_t	*gw_ire = NULL;
5369 	ipif_t	*ipif = NULL;
5370 	uint_t	type;
5371 	int	match_flags = MATCH_IRE_TYPE;
5372 	tsol_gc_t *gc = NULL;
5373 	tsol_gcgrp_t *gcgrp = NULL;
5374 	boolean_t gcgrp_xtraref = B_FALSE;
5375 	boolean_t cgtp_broadcast;
5376 	boolean_t unbound = B_FALSE;
5377 
5378 	ip1dbg(("ip_rt_add:"));
5379 
5380 	if (ire_arg != NULL)
5381 		*ire_arg = NULL;
5382 
5383 	/* disallow non-contiguous netmasks */
5384 	if (!ip_contiguous_mask(ntohl(mask)))
5385 		return (ENOTSUP);
5386 
5387 	/*
5388 	 * If this is the case of RTF_HOST being set, then we set the netmask
5389 	 * to all ones (regardless if one was supplied).
5390 	 */
5391 	if (flags & RTF_HOST)
5392 		mask = IP_HOST_MASK;
5393 
5394 	/*
5395 	 * Prevent routes with a zero gateway from being created (since
5396 	 * interfaces can currently be plumbed and brought up no assigned
5397 	 * address).
5398 	 */
5399 	if (gw_addr == 0)
5400 		return (ENETUNREACH);
5401 	/*
5402 	 * Get the ipif, if any, corresponding to the gw_addr
5403 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5404 	 * we match on the gatway and destination to handle unnumbered pt-pt
5405 	 * interfaces.
5406 	 */
5407 	if (ill != NULL)
5408 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5409 	else
5410 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5411 	if (ipif != NULL) {
5412 		if (IS_VNI(ipif->ipif_ill)) {
5413 			ipif_refrele(ipif);
5414 			return (EINVAL);
5415 		}
5416 	}
5417 
5418 	/*
5419 	 * GateD will attempt to create routes with a loopback interface
5420 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5421 	 * these routes to be added, but create them as interface routes
5422 	 * since the gateway is an interface address.
5423 	 */
5424 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5425 		flags &= ~RTF_GATEWAY;
5426 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5427 		    mask == IP_HOST_MASK) {
5428 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5429 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5430 			    NULL);
5431 			if (ire != NULL) {
5432 				ire_refrele(ire);
5433 				ipif_refrele(ipif);
5434 				return (EEXIST);
5435 			}
5436 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5437 			    "for 0x%x\n", (void *)ipif,
5438 			    ipif->ipif_ire_type,
5439 			    ntohl(ipif->ipif_lcl_addr)));
5440 			ire = ire_create(
5441 			    (uchar_t *)&dst_addr,	/* dest address */
5442 			    (uchar_t *)&mask,		/* mask */
5443 			    NULL,			/* no gateway */
5444 			    ipif->ipif_ire_type,	/* LOOPBACK */
5445 			    ipif->ipif_ill,
5446 			    zoneid,
5447 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5448 			    NULL,
5449 			    ipst);
5450 
5451 			if (ire == NULL) {
5452 				ipif_refrele(ipif);
5453 				return (ENOMEM);
5454 			}
5455 			/* src address assigned by the caller? */
5456 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5457 				ire->ire_setsrc_addr = src_addr;
5458 
5459 			nire = ire_add(ire);
5460 			if (nire == NULL) {
5461 				/*
5462 				 * In the result of failure, ire_add() will have
5463 				 * already deleted the ire in question, so there
5464 				 * is no need to do that here.
5465 				 */
5466 				ipif_refrele(ipif);
5467 				return (ENOMEM);
5468 			}
5469 			/*
5470 			 * Check if it was a duplicate entry. This handles
5471 			 * the case of two racing route adds for the same route
5472 			 */
5473 			if (nire != ire) {
5474 				ASSERT(nire->ire_identical_ref > 1);
5475 				ire_delete(nire);
5476 				ire_refrele(nire);
5477 				ipif_refrele(ipif);
5478 				return (EEXIST);
5479 			}
5480 			ire = nire;
5481 			goto save_ire;
5482 		}
5483 	}
5484 
5485 	/*
5486 	 * The routes for multicast with CGTP are quite special in that
5487 	 * the gateway is the local interface address, yet RTF_GATEWAY
5488 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5489 	 * this undocumented and unusual use of multicast routes.
5490 	 */
5491 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5492 		flags &= ~RTF_GATEWAY;
5493 
5494 	/*
5495 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5496 	 * and the gateway address provided is one of the system's interface
5497 	 * addresses.  By using the routing socket interface and supplying an
5498 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5499 	 * specifying an interface route to be created is available which uses
5500 	 * the interface index that specifies the outgoing interface rather than
5501 	 * the address of an outgoing interface (which may not be able to
5502 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5503 	 * flag, routes can be specified which not only specify the next-hop to
5504 	 * be used when routing to a certain prefix, but also which outgoing
5505 	 * interface should be used.
5506 	 *
5507 	 * Previously, interfaces would have unique addresses assigned to them
5508 	 * and so the address assigned to a particular interface could be used
5509 	 * to identify a particular interface.  One exception to this was the
5510 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5511 	 *
5512 	 * With the advent of IPv6 and its link-local addresses, this
5513 	 * restriction was relaxed and interfaces could share addresses between
5514 	 * themselves.  In fact, typically all of the link-local interfaces on
5515 	 * an IPv6 node or router will have the same link-local address.  In
5516 	 * order to differentiate between these interfaces, the use of an
5517 	 * interface index is necessary and this index can be carried inside a
5518 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5519 	 * of using the interface index, however, is that all of the ipif's that
5520 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5521 	 * cannot be used to differentiate between ipif's (or logical
5522 	 * interfaces) that belong to the same ill (physical interface).
5523 	 *
5524 	 * For example, in the following case involving IPv4 interfaces and
5525 	 * logical interfaces
5526 	 *
5527 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5528 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5529 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5530 	 *
5531 	 * the ipif's corresponding to each of these interface routes can be
5532 	 * uniquely identified by the "gateway" (actually interface address).
5533 	 *
5534 	 * In this case involving multiple IPv6 default routes to a particular
5535 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5536 	 * default route is of interest:
5537 	 *
5538 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5539 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5540 	 */
5541 
5542 	/* RTF_GATEWAY not set */
5543 	if (!(flags & RTF_GATEWAY)) {
5544 		if (sp != NULL) {
5545 			ip2dbg(("ip_rt_add: gateway security attributes "
5546 			    "cannot be set with interface route\n"));
5547 			if (ipif != NULL)
5548 				ipif_refrele(ipif);
5549 			return (EINVAL);
5550 		}
5551 
5552 		/*
5553 		 * Whether or not ill (RTA_IFP) is set, we require that
5554 		 * the gateway is one of our local addresses.
5555 		 */
5556 		if (ipif == NULL)
5557 			return (ENETUNREACH);
5558 
5559 		/*
5560 		 * We use MATCH_IRE_ILL here. If the caller specified an
5561 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5562 		 * we use the ill derived from the gateway address.
5563 		 * We can always match the gateway address since we record it
5564 		 * in ire_gateway_addr.
5565 		 * We don't allow RTA_IFP to specify a different ill than the
5566 		 * one matching the ipif to make sure we can delete the route.
5567 		 */
5568 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5569 		if (ill == NULL) {
5570 			ill = ipif->ipif_ill;
5571 		} else if (ill != ipif->ipif_ill) {
5572 			ipif_refrele(ipif);
5573 			return (EINVAL);
5574 		}
5575 
5576 		/*
5577 		 * We check for an existing entry at this point.
5578 		 *
5579 		 * Since a netmask isn't passed in via the ioctl interface
5580 		 * (SIOCADDRT), we don't check for a matching netmask in that
5581 		 * case.
5582 		 */
5583 		if (!ioctl_msg)
5584 			match_flags |= MATCH_IRE_MASK;
5585 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5586 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5587 		    NULL);
5588 		if (ire != NULL) {
5589 			ire_refrele(ire);
5590 			ipif_refrele(ipif);
5591 			return (EEXIST);
5592 		}
5593 
5594 		/*
5595 		 * Some software (for example, GateD and Sun Cluster) attempts
5596 		 * to create (what amount to) IRE_PREFIX routes with the
5597 		 * loopback address as the gateway.  This is primarily done to
5598 		 * set up prefixes with the RTF_REJECT flag set (for example,
5599 		 * when generating aggregate routes.)
5600 		 *
5601 		 * If the IRE type (as defined by ill->ill_net_type) would be
5602 		 * IRE_LOOPBACK, then we map the request into a
5603 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5604 		 * these interface routes, by definition, can only be that.
5605 		 *
5606 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5607 		 * routine, but rather using ire_create() directly.
5608 		 *
5609 		 */
5610 		type = ill->ill_net_type;
5611 		if (type == IRE_LOOPBACK) {
5612 			type = IRE_IF_NORESOLVER;
5613 			flags |= RTF_BLACKHOLE;
5614 		}
5615 
5616 		/*
5617 		 * Create a copy of the IRE_IF_NORESOLVER or
5618 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5619 		 * gateway.
5620 		 */
5621 		ire = ire_create(
5622 		    (uchar_t *)&dst_addr,
5623 		    (uint8_t *)&mask,
5624 		    (uint8_t *)&gw_addr,
5625 		    type,
5626 		    ill,
5627 		    zoneid,
5628 		    flags,
5629 		    NULL,
5630 		    ipst);
5631 		if (ire == NULL) {
5632 			ipif_refrele(ipif);
5633 			return (ENOMEM);
5634 		}
5635 
5636 		/* src address assigned by the caller? */
5637 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5638 			ire->ire_setsrc_addr = src_addr;
5639 
5640 		nire = ire_add(ire);
5641 		if (nire == NULL) {
5642 			/*
5643 			 * In the result of failure, ire_add() will have
5644 			 * already deleted the ire in question, so there
5645 			 * is no need to do that here.
5646 			 */
5647 			ipif_refrele(ipif);
5648 			return (ENOMEM);
5649 		}
5650 		/*
5651 		 * Check if it was a duplicate entry. This handles
5652 		 * the case of two racing route adds for the same route
5653 		 */
5654 		if (nire != ire) {
5655 			ire_delete(nire);
5656 			ire_refrele(nire);
5657 			ipif_refrele(ipif);
5658 			return (EEXIST);
5659 		}
5660 		ire = nire;
5661 		goto save_ire;
5662 	}
5663 
5664 	/*
5665 	 * Get an interface IRE for the specified gateway.
5666 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5667 	 * gateway, it is currently unreachable and we fail the request
5668 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5669 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5670 	 * If RTA_IFP was specified we look on that particular ill.
5671 	 */
5672 	if (ill != NULL)
5673 		match_flags |= MATCH_IRE_ILL;
5674 
5675 	/* Check whether the gateway is reachable. */
5676 again:
5677 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5678 	if (flags & RTF_INDIRECT)
5679 		type |= IRE_OFFLINK;
5680 
5681 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5682 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5683 	if (gw_ire == NULL) {
5684 		/*
5685 		 * With IPMP, we allow host routes to influence in.mpathd's
5686 		 * target selection.  However, if the test addresses are on
5687 		 * their own network, the above lookup will fail since the
5688 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5689 		 * hidden test IREs to be found and try again.
5690 		 */
5691 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5692 			match_flags |= MATCH_IRE_TESTHIDDEN;
5693 			goto again;
5694 		}
5695 		if (ipif != NULL)
5696 			ipif_refrele(ipif);
5697 		return (ENETUNREACH);
5698 	}
5699 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5700 		ire_refrele(gw_ire);
5701 		if (ipif != NULL)
5702 			ipif_refrele(ipif);
5703 		return (ENETUNREACH);
5704 	}
5705 
5706 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5707 		unbound = B_TRUE;
5708 		if (ipst->ips_ip_strict_src_multihoming > 0)
5709 			ill = gw_ire->ire_ill;
5710 	}
5711 
5712 	/*
5713 	 * We create one of three types of IREs as a result of this request
5714 	 * based on the netmask.  A netmask of all ones (which is automatically
5715 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5716 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5717 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5718 	 * destination prefix.
5719 	 */
5720 	if (mask == IP_HOST_MASK)
5721 		type = IRE_HOST;
5722 	else if (mask == 0)
5723 		type = IRE_DEFAULT;
5724 	else
5725 		type = IRE_PREFIX;
5726 
5727 	/* check for a duplicate entry */
5728 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5729 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5730 	    0, ipst, NULL);
5731 	if (ire != NULL) {
5732 		if (ipif != NULL)
5733 			ipif_refrele(ipif);
5734 		ire_refrele(gw_ire);
5735 		ire_refrele(ire);
5736 		return (EEXIST);
5737 	}
5738 
5739 	/* Security attribute exists */
5740 	if (sp != NULL) {
5741 		tsol_gcgrp_addr_t ga;
5742 
5743 		/* find or create the gateway credentials group */
5744 		ga.ga_af = AF_INET;
5745 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5746 
5747 		/* we hold reference to it upon success */
5748 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5749 		if (gcgrp == NULL) {
5750 			if (ipif != NULL)
5751 				ipif_refrele(ipif);
5752 			ire_refrele(gw_ire);
5753 			return (ENOMEM);
5754 		}
5755 
5756 		/*
5757 		 * Create and add the security attribute to the group; a
5758 		 * reference to the group is made upon allocating a new
5759 		 * entry successfully.  If it finds an already-existing
5760 		 * entry for the security attribute in the group, it simply
5761 		 * returns it and no new reference is made to the group.
5762 		 */
5763 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5764 		if (gc == NULL) {
5765 			if (ipif != NULL)
5766 				ipif_refrele(ipif);
5767 			/* release reference held by gcgrp_lookup */
5768 			GCGRP_REFRELE(gcgrp);
5769 			ire_refrele(gw_ire);
5770 			return (ENOMEM);
5771 		}
5772 	}
5773 
5774 	/* Create the IRE. */
5775 	ire = ire_create(
5776 	    (uchar_t *)&dst_addr,		/* dest address */
5777 	    (uchar_t *)&mask,			/* mask */
5778 	    (uchar_t *)&gw_addr,		/* gateway address */
5779 	    (ushort_t)type,			/* IRE type */
5780 	    ill,
5781 	    zoneid,
5782 	    flags,
5783 	    gc,					/* security attribute */
5784 	    ipst);
5785 
5786 	/*
5787 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5788 	 * reference to the 'gcgrp'. We can now release the extra reference
5789 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5790 	 */
5791 	if (gcgrp_xtraref)
5792 		GCGRP_REFRELE(gcgrp);
5793 	if (ire == NULL) {
5794 		if (gc != NULL)
5795 			GC_REFRELE(gc);
5796 		if (ipif != NULL)
5797 			ipif_refrele(ipif);
5798 		ire_refrele(gw_ire);
5799 		return (ENOMEM);
5800 	}
5801 
5802 	/* Before we add, check if an extra CGTP broadcast is needed */
5803 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5804 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5805 
5806 	/* src address assigned by the caller? */
5807 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5808 		ire->ire_setsrc_addr = src_addr;
5809 
5810 	ire->ire_unbound = unbound;
5811 
5812 	/*
5813 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5814 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5815 	 */
5816 
5817 	/* Add the new IRE. */
5818 	nire = ire_add(ire);
5819 	if (nire == NULL) {
5820 		/*
5821 		 * In the result of failure, ire_add() will have
5822 		 * already deleted the ire in question, so there
5823 		 * is no need to do that here.
5824 		 */
5825 		if (ipif != NULL)
5826 			ipif_refrele(ipif);
5827 		ire_refrele(gw_ire);
5828 		return (ENOMEM);
5829 	}
5830 	/*
5831 	 * Check if it was a duplicate entry. This handles
5832 	 * the case of two racing route adds for the same route
5833 	 */
5834 	if (nire != ire) {
5835 		ire_delete(nire);
5836 		ire_refrele(nire);
5837 		if (ipif != NULL)
5838 			ipif_refrele(ipif);
5839 		ire_refrele(gw_ire);
5840 		return (EEXIST);
5841 	}
5842 	ire = nire;
5843 
5844 	if (flags & RTF_MULTIRT) {
5845 		/*
5846 		 * Invoke the CGTP (multirouting) filtering module
5847 		 * to add the dst address in the filtering database.
5848 		 * Replicated inbound packets coming from that address
5849 		 * will be filtered to discard the duplicates.
5850 		 * It is not necessary to call the CGTP filter hook
5851 		 * when the dst address is a broadcast or multicast,
5852 		 * because an IP source address cannot be a broadcast
5853 		 * or a multicast.
5854 		 */
5855 		if (cgtp_broadcast) {
5856 			ip_cgtp_bcast_add(ire, ipst);
5857 			goto save_ire;
5858 		}
5859 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5860 		    !CLASSD(ire->ire_addr)) {
5861 			int res;
5862 			ipif_t *src_ipif;
5863 
5864 			/* Find the source address corresponding to gw_ire */
5865 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5866 			    NULL, zoneid, ipst);
5867 			if (src_ipif != NULL) {
5868 				res = ipst->ips_ip_cgtp_filter_ops->
5869 				    cfo_add_dest_v4(
5870 				    ipst->ips_netstack->netstack_stackid,
5871 				    ire->ire_addr,
5872 				    ire->ire_gateway_addr,
5873 				    ire->ire_setsrc_addr,
5874 				    src_ipif->ipif_lcl_addr);
5875 				ipif_refrele(src_ipif);
5876 			} else {
5877 				res = EADDRNOTAVAIL;
5878 			}
5879 			if (res != 0) {
5880 				if (ipif != NULL)
5881 					ipif_refrele(ipif);
5882 				ire_refrele(gw_ire);
5883 				ire_delete(ire);
5884 				ire_refrele(ire);	/* Held in ire_add */
5885 				return (res);
5886 			}
5887 		}
5888 	}
5889 
5890 save_ire:
5891 	if (gw_ire != NULL) {
5892 		ire_refrele(gw_ire);
5893 		gw_ire = NULL;
5894 	}
5895 	if (ill != NULL) {
5896 		/*
5897 		 * Save enough information so that we can recreate the IRE if
5898 		 * the interface goes down and then up.  The metrics associated
5899 		 * with the route will be saved as well when rts_setmetrics() is
5900 		 * called after the IRE has been created.  In the case where
5901 		 * memory cannot be allocated, none of this information will be
5902 		 * saved.
5903 		 */
5904 		ill_save_ire(ill, ire);
5905 	}
5906 	if (ioctl_msg)
5907 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5908 	if (ire_arg != NULL) {
5909 		/*
5910 		 * Store the ire that was successfully added into where ire_arg
5911 		 * points to so that callers don't have to look it up
5912 		 * themselves (but they are responsible for ire_refrele()ing
5913 		 * the ire when they are finished with it).
5914 		 */
5915 		*ire_arg = ire;
5916 	} else {
5917 		ire_refrele(ire);		/* Held in ire_add */
5918 	}
5919 	if (ipif != NULL)
5920 		ipif_refrele(ipif);
5921 	return (0);
5922 }
5923 
5924 /*
5925  * ip_rt_delete is called to delete an IPv4 route.
5926  * ill is passed in to associate it with the correct interface.
5927  */
5928 /* ARGSUSED4 */
5929 int
5930 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5931     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5932     ip_stack_t *ipst, zoneid_t zoneid)
5933 {
5934 	ire_t	*ire = NULL;
5935 	ipif_t	*ipif;
5936 	uint_t	type;
5937 	uint_t	match_flags = MATCH_IRE_TYPE;
5938 	int	err = 0;
5939 
5940 	ip1dbg(("ip_rt_delete:"));
5941 	/*
5942 	 * If this is the case of RTF_HOST being set, then we set the netmask
5943 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5944 	 */
5945 	if (flags & RTF_HOST) {
5946 		mask = IP_HOST_MASK;
5947 		match_flags |= MATCH_IRE_MASK;
5948 	} else if (rtm_addrs & RTA_NETMASK) {
5949 		match_flags |= MATCH_IRE_MASK;
5950 	}
5951 
5952 	/*
5953 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5954 	 * we check if the gateway address is one of our interfaces first,
5955 	 * and fall back on RTF_GATEWAY routes.
5956 	 *
5957 	 * This makes it possible to delete an original
5958 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5959 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5960 	 * and those can not be deleted here.
5961 	 *
5962 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5963 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5964 	 * otherwise we use the ill derived from the gateway address.
5965 	 * We can always match the gateway address since we record it
5966 	 * in ire_gateway_addr.
5967 	 *
5968 	 * For more detail on specifying routes by gateway address and by
5969 	 * interface index, see the comments in ip_rt_add().
5970 	 */
5971 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5972 	if (ipif != NULL) {
5973 		ill_t	*ill_match;
5974 
5975 		if (ill != NULL)
5976 			ill_match = ill;
5977 		else
5978 			ill_match = ipif->ipif_ill;
5979 
5980 		match_flags |= MATCH_IRE_ILL;
5981 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5982 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
5983 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
5984 			    match_flags, 0, ipst, NULL);
5985 		}
5986 		if (ire == NULL) {
5987 			match_flags |= MATCH_IRE_GW;
5988 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5989 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5990 			    match_flags, 0, ipst, NULL);
5991 		}
5992 		/* Avoid deleting routes created by kernel from an ipif */
5993 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5994 			ire_refrele(ire);
5995 			ire = NULL;
5996 		}
5997 
5998 		/* Restore in case we didn't find a match */
5999 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6000 	}
6001 
6002 	if (ire == NULL) {
6003 		/*
6004 		 * At this point, the gateway address is not one of our own
6005 		 * addresses or a matching interface route was not found.  We
6006 		 * set the IRE type to lookup based on whether
6007 		 * this is a host route, a default route or just a prefix.
6008 		 *
6009 		 * If an ill was passed in, then the lookup is based on an
6010 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6011 		 */
6012 		match_flags |= MATCH_IRE_GW;
6013 		if (ill != NULL)
6014 			match_flags |= MATCH_IRE_ILL;
6015 		if (mask == IP_HOST_MASK)
6016 			type = IRE_HOST;
6017 		else if (mask == 0)
6018 			type = IRE_DEFAULT;
6019 		else
6020 			type = IRE_PREFIX;
6021 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6022 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6023 	}
6024 
6025 	if (ipif != NULL) {
6026 		ipif_refrele(ipif);
6027 		ipif = NULL;
6028 	}
6029 
6030 	if (ire == NULL)
6031 		return (ESRCH);
6032 
6033 	if (ire->ire_flags & RTF_MULTIRT) {
6034 		/*
6035 		 * Invoke the CGTP (multirouting) filtering module
6036 		 * to remove the dst address from the filtering database.
6037 		 * Packets coming from that address will no longer be
6038 		 * filtered to remove duplicates.
6039 		 */
6040 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6041 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6042 			    ipst->ips_netstack->netstack_stackid,
6043 			    ire->ire_addr, ire->ire_gateway_addr);
6044 		}
6045 		ip_cgtp_bcast_delete(ire, ipst);
6046 	}
6047 
6048 	ill = ire->ire_ill;
6049 	if (ill != NULL)
6050 		ill_remove_saved_ire(ill, ire);
6051 	if (ioctl_msg)
6052 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6053 	ire_delete(ire);
6054 	ire_refrele(ire);
6055 	return (err);
6056 }
6057 
6058 /*
6059  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6060  */
6061 /* ARGSUSED */
6062 int
6063 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6064     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6065 {
6066 	ipaddr_t dst_addr;
6067 	ipaddr_t gw_addr;
6068 	ipaddr_t mask;
6069 	int error = 0;
6070 	mblk_t *mp1;
6071 	struct rtentry *rt;
6072 	ipif_t *ipif = NULL;
6073 	ip_stack_t	*ipst;
6074 
6075 	ASSERT(q->q_next == NULL);
6076 	ipst = CONNQ_TO_IPST(q);
6077 
6078 	ip1dbg(("ip_siocaddrt:"));
6079 	/* Existence of mp1 verified in ip_wput_nondata */
6080 	mp1 = mp->b_cont->b_cont;
6081 	rt = (struct rtentry *)mp1->b_rptr;
6082 
6083 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6084 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6085 
6086 	/*
6087 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6088 	 * to a particular host address.  In this case, we set the netmask to
6089 	 * all ones for the particular destination address.  Otherwise,
6090 	 * determine the netmask to be used based on dst_addr and the interfaces
6091 	 * in use.
6092 	 */
6093 	if (rt->rt_flags & RTF_HOST) {
6094 		mask = IP_HOST_MASK;
6095 	} else {
6096 		/*
6097 		 * Note that ip_subnet_mask returns a zero mask in the case of
6098 		 * default (an all-zeroes address).
6099 		 */
6100 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6101 	}
6102 
6103 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6104 	    B_TRUE, NULL, ipst, ALL_ZONES);
6105 	if (ipif != NULL)
6106 		ipif_refrele(ipif);
6107 	return (error);
6108 }
6109 
6110 /*
6111  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6112  */
6113 /* ARGSUSED */
6114 int
6115 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6116     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6117 {
6118 	ipaddr_t dst_addr;
6119 	ipaddr_t gw_addr;
6120 	ipaddr_t mask;
6121 	int error;
6122 	mblk_t *mp1;
6123 	struct rtentry *rt;
6124 	ipif_t *ipif = NULL;
6125 	ip_stack_t	*ipst;
6126 
6127 	ASSERT(q->q_next == NULL);
6128 	ipst = CONNQ_TO_IPST(q);
6129 
6130 	ip1dbg(("ip_siocdelrt:"));
6131 	/* Existence of mp1 verified in ip_wput_nondata */
6132 	mp1 = mp->b_cont->b_cont;
6133 	rt = (struct rtentry *)mp1->b_rptr;
6134 
6135 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6136 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6137 
6138 	/*
6139 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6140 	 * to a particular host address.  In this case, we set the netmask to
6141 	 * all ones for the particular destination address.  Otherwise,
6142 	 * determine the netmask to be used based on dst_addr and the interfaces
6143 	 * in use.
6144 	 */
6145 	if (rt->rt_flags & RTF_HOST) {
6146 		mask = IP_HOST_MASK;
6147 	} else {
6148 		/*
6149 		 * Note that ip_subnet_mask returns a zero mask in the case of
6150 		 * default (an all-zeroes address).
6151 		 */
6152 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6153 	}
6154 
6155 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6156 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6157 	    ipst, ALL_ZONES);
6158 	if (ipif != NULL)
6159 		ipif_refrele(ipif);
6160 	return (error);
6161 }
6162 
6163 /*
6164  * Enqueue the mp onto the ipsq, chained by b_next.
6165  * b_prev stores the function to be executed later, and b_queue the queue
6166  * where this mp originated.
6167  */
6168 void
6169 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6170     ill_t *pending_ill)
6171 {
6172 	conn_t	*connp;
6173 	ipxop_t *ipx = ipsq->ipsq_xop;
6174 
6175 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6176 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6177 	ASSERT(func != NULL);
6178 
6179 	mp->b_queue = q;
6180 	mp->b_prev = (void *)func;
6181 	mp->b_next = NULL;
6182 
6183 	switch (type) {
6184 	case CUR_OP:
6185 		if (ipx->ipx_mptail != NULL) {
6186 			ASSERT(ipx->ipx_mphead != NULL);
6187 			ipx->ipx_mptail->b_next = mp;
6188 		} else {
6189 			ASSERT(ipx->ipx_mphead == NULL);
6190 			ipx->ipx_mphead = mp;
6191 		}
6192 		ipx->ipx_mptail = mp;
6193 		break;
6194 
6195 	case NEW_OP:
6196 		if (ipsq->ipsq_xopq_mptail != NULL) {
6197 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6198 			ipsq->ipsq_xopq_mptail->b_next = mp;
6199 		} else {
6200 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6201 			ipsq->ipsq_xopq_mphead = mp;
6202 		}
6203 		ipsq->ipsq_xopq_mptail = mp;
6204 		ipx->ipx_ipsq_queued = B_TRUE;
6205 		break;
6206 
6207 	case SWITCH_OP:
6208 		ASSERT(ipsq->ipsq_swxop != NULL);
6209 		/* only one switch operation is currently allowed */
6210 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6211 		ipsq->ipsq_switch_mp = mp;
6212 		ipx->ipx_ipsq_queued = B_TRUE;
6213 		break;
6214 	default:
6215 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6216 	}
6217 
6218 	if (CONN_Q(q) && pending_ill != NULL) {
6219 		connp = Q_TO_CONN(q);
6220 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6221 		connp->conn_oper_pending_ill = pending_ill;
6222 	}
6223 }
6224 
6225 /*
6226  * Dequeue the next message that requested exclusive access to this IPSQ's
6227  * xop.  Specifically:
6228  *
6229  *  1. If we're still processing the current operation on `ipsq', then
6230  *     dequeue the next message for the operation (from ipx_mphead), or
6231  *     return NULL if there are no queued messages for the operation.
6232  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6233  *
6234  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6235  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6236  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6237  *     leaving IPMP groups and require a careful dance -- see the comments
6238  *     in-line below for details.  If we're leaving a group xop or if we're
6239  *     joining a group xop and become writer on it, then we proceed to (3).
6240  *     Otherwise, we return NULL and exit the xop.
6241  *
6242  *  3. For each IPSQ in the xop, return any switch operation stored on
6243  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6244  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6245  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6246  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6247  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6248  *     each phyint in the group, including the IPMP meta-interface phyint.
6249  */
6250 static mblk_t *
6251 ipsq_dq(ipsq_t *ipsq)
6252 {
6253 	ill_t	*illv4, *illv6;
6254 	mblk_t	*mp;
6255 	ipsq_t	*xopipsq;
6256 	ipsq_t	*leftipsq = NULL;
6257 	ipxop_t *ipx;
6258 	phyint_t *phyi = ipsq->ipsq_phyint;
6259 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6260 	boolean_t emptied = B_FALSE;
6261 
6262 	/*
6263 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6264 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6265 	 */
6266 	rw_enter(&ipst->ips_ill_g_lock,
6267 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6268 	mutex_enter(&ipsq->ipsq_lock);
6269 	ipx = ipsq->ipsq_xop;
6270 	mutex_enter(&ipx->ipx_lock);
6271 
6272 	/*
6273 	 * Dequeue the next message associated with the current exclusive
6274 	 * operation, if any.
6275 	 */
6276 	if ((mp = ipx->ipx_mphead) != NULL) {
6277 		ipx->ipx_mphead = mp->b_next;
6278 		if (ipx->ipx_mphead == NULL)
6279 			ipx->ipx_mptail = NULL;
6280 		mp->b_next = (void *)ipsq;
6281 		goto out;
6282 	}
6283 
6284 	if (ipx->ipx_current_ipif != NULL)
6285 		goto empty;
6286 
6287 	if (ipsq->ipsq_swxop != NULL) {
6288 		/*
6289 		 * The exclusive operation that is now being completed has
6290 		 * requested a switch to a different xop.  This happens
6291 		 * when an interface joins or leaves an IPMP group.  Joins
6292 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6293 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6294 		 * (phyint_free()), or interface plumb for an ill type
6295 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6296 		 *
6297 		 * Xop switches are not allowed on the IPMP meta-interface.
6298 		 */
6299 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6300 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6301 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6302 
6303 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6304 			/*
6305 			 * We're switching back to our own xop, so we have two
6306 			 * xop's to drain/exit: our own, and the group xop
6307 			 * that we are leaving.
6308 			 *
6309 			 * First, pull ourselves out of the group ipsq list.
6310 			 * This is safe since we're writer on ill_g_lock.
6311 			 */
6312 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6313 
6314 			xopipsq = ipx->ipx_ipsq;
6315 			while (xopipsq->ipsq_next != ipsq)
6316 				xopipsq = xopipsq->ipsq_next;
6317 
6318 			xopipsq->ipsq_next = ipsq->ipsq_next;
6319 			ipsq->ipsq_next = ipsq;
6320 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6321 			ipsq->ipsq_swxop = NULL;
6322 
6323 			/*
6324 			 * Second, prepare to exit the group xop.  The actual
6325 			 * ipsq_exit() is done at the end of this function
6326 			 * since we cannot hold any locks across ipsq_exit().
6327 			 * Note that although we drop the group's ipx_lock, no
6328 			 * threads can proceed since we're still ipx_writer.
6329 			 */
6330 			leftipsq = xopipsq;
6331 			mutex_exit(&ipx->ipx_lock);
6332 
6333 			/*
6334 			 * Third, set ipx to point to our own xop (which was
6335 			 * inactive and therefore can be entered).
6336 			 */
6337 			ipx = ipsq->ipsq_xop;
6338 			mutex_enter(&ipx->ipx_lock);
6339 			ASSERT(ipx->ipx_writer == NULL);
6340 			ASSERT(ipx->ipx_current_ipif == NULL);
6341 		} else {
6342 			/*
6343 			 * We're switching from our own xop to a group xop.
6344 			 * The requestor of the switch must ensure that the
6345 			 * group xop cannot go away (e.g. by ensuring the
6346 			 * phyint associated with the xop cannot go away).
6347 			 *
6348 			 * If we can become writer on our new xop, then we'll
6349 			 * do the drain.  Otherwise, the current writer of our
6350 			 * new xop will do the drain when it exits.
6351 			 *
6352 			 * First, splice ourselves into the group IPSQ list.
6353 			 * This is safe since we're writer on ill_g_lock.
6354 			 */
6355 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6356 
6357 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6358 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6359 				xopipsq = xopipsq->ipsq_next;
6360 
6361 			xopipsq->ipsq_next = ipsq;
6362 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6363 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6364 			ipsq->ipsq_swxop = NULL;
6365 
6366 			/*
6367 			 * Second, exit our own xop, since it's now unused.
6368 			 * This is safe since we've got the only reference.
6369 			 */
6370 			ASSERT(ipx->ipx_writer == curthread);
6371 			ipx->ipx_writer = NULL;
6372 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6373 			ipx->ipx_ipsq_queued = B_FALSE;
6374 			mutex_exit(&ipx->ipx_lock);
6375 
6376 			/*
6377 			 * Third, set ipx to point to our new xop, and check
6378 			 * if we can become writer on it.  If we cannot, then
6379 			 * the current writer will drain the IPSQ group when
6380 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6381 			 * because we're still holding ipsq_lock.
6382 			 */
6383 			ipx = ipsq->ipsq_xop;
6384 			mutex_enter(&ipx->ipx_lock);
6385 			if (ipx->ipx_writer != NULL ||
6386 			    ipx->ipx_current_ipif != NULL) {
6387 				goto out;
6388 			}
6389 		}
6390 
6391 		/*
6392 		 * Fourth, become writer on our new ipx before we continue
6393 		 * with the drain.  Note that we never dropped ipsq_lock
6394 		 * above, so no other thread could've raced with us to
6395 		 * become writer first.  Also, we're holding ipx_lock, so
6396 		 * no other thread can examine the ipx right now.
6397 		 */
6398 		ASSERT(ipx->ipx_current_ipif == NULL);
6399 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6400 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6401 		ipx->ipx_writer = curthread;
6402 		ipx->ipx_forced = B_FALSE;
6403 #ifdef DEBUG
6404 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6405 #endif
6406 	}
6407 
6408 	xopipsq = ipsq;
6409 	do {
6410 		/*
6411 		 * So that other operations operate on a consistent and
6412 		 * complete phyint, a switch message on an IPSQ must be
6413 		 * handled prior to any other operations on that IPSQ.
6414 		 */
6415 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6416 			xopipsq->ipsq_switch_mp = NULL;
6417 			ASSERT(mp->b_next == NULL);
6418 			mp->b_next = (void *)xopipsq;
6419 			goto out;
6420 		}
6421 
6422 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6423 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6424 			if (xopipsq->ipsq_xopq_mphead == NULL)
6425 				xopipsq->ipsq_xopq_mptail = NULL;
6426 			mp->b_next = (void *)xopipsq;
6427 			goto out;
6428 		}
6429 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6430 empty:
6431 	/*
6432 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6433 	 * new messages can end up on any IPSQ in the xop.
6434 	 */
6435 	ipx->ipx_writer = NULL;
6436 	ipx->ipx_forced = B_FALSE;
6437 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6438 	ipx->ipx_ipsq_queued = B_FALSE;
6439 	emptied = B_TRUE;
6440 #ifdef	DEBUG
6441 	ipx->ipx_depth = 0;
6442 #endif
6443 out:
6444 	mutex_exit(&ipx->ipx_lock);
6445 	mutex_exit(&ipsq->ipsq_lock);
6446 
6447 	/*
6448 	 * If we completely emptied the xop, then wake up any threads waiting
6449 	 * to enter any of the IPSQ's associated with it.
6450 	 */
6451 	if (emptied) {
6452 		xopipsq = ipsq;
6453 		do {
6454 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6455 				continue;
6456 
6457 			illv4 = phyi->phyint_illv4;
6458 			illv6 = phyi->phyint_illv6;
6459 
6460 			GRAB_ILL_LOCKS(illv4, illv6);
6461 			if (illv4 != NULL)
6462 				cv_broadcast(&illv4->ill_cv);
6463 			if (illv6 != NULL)
6464 				cv_broadcast(&illv6->ill_cv);
6465 			RELEASE_ILL_LOCKS(illv4, illv6);
6466 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6467 	}
6468 	rw_exit(&ipst->ips_ill_g_lock);
6469 
6470 	/*
6471 	 * Now that all locks are dropped, exit the IPSQ we left.
6472 	 */
6473 	if (leftipsq != NULL)
6474 		ipsq_exit(leftipsq);
6475 
6476 	return (mp);
6477 }
6478 
6479 /*
6480  * Return completion status of previously initiated DLPI operations on
6481  * ills in the purview of an ipsq.
6482  */
6483 static boolean_t
6484 ipsq_dlpi_done(ipsq_t *ipsq)
6485 {
6486 	ipsq_t		*ipsq_start;
6487 	phyint_t	*phyi;
6488 	ill_t		*ill;
6489 
6490 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6491 	ipsq_start = ipsq;
6492 
6493 	do {
6494 		/*
6495 		 * The only current users of this function are ipsq_try_enter
6496 		 * and ipsq_enter which have made sure that ipsq_writer is
6497 		 * NULL before we reach here. ill_dlpi_pending is modified
6498 		 * only by an ipsq writer
6499 		 */
6500 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6501 		phyi = ipsq->ipsq_phyint;
6502 		/*
6503 		 * phyi could be NULL if a phyint that is part of an
6504 		 * IPMP group is being unplumbed. A more detailed
6505 		 * comment is in ipmp_grp_update_kstats()
6506 		 */
6507 		if (phyi != NULL) {
6508 			ill = phyi->phyint_illv4;
6509 			if (ill != NULL &&
6510 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6511 			    ill->ill_arl_dlpi_pending))
6512 				return (B_FALSE);
6513 
6514 			ill = phyi->phyint_illv6;
6515 			if (ill != NULL &&
6516 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6517 				return (B_FALSE);
6518 		}
6519 
6520 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6521 
6522 	return (B_TRUE);
6523 }
6524 
6525 /*
6526  * Enter the ipsq corresponding to ill, by waiting synchronously till
6527  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6528  * will have to drain completely before ipsq_enter returns success.
6529  * ipx_current_ipif will be set if some exclusive op is in progress,
6530  * and the ipsq_exit logic will start the next enqueued op after
6531  * completion of the current op. If 'force' is used, we don't wait
6532  * for the enqueued ops. This is needed when a conn_close wants to
6533  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6534  * of an ill can also use this option. But we dont' use it currently.
6535  */
6536 #define	ENTER_SQ_WAIT_TICKS 100
6537 boolean_t
6538 ipsq_enter(ill_t *ill, boolean_t force, int type)
6539 {
6540 	ipsq_t	*ipsq;
6541 	ipxop_t *ipx;
6542 	boolean_t waited_enough = B_FALSE;
6543 	ip_stack_t *ipst = ill->ill_ipst;
6544 
6545 	/*
6546 	 * Note that the relationship between ill and ipsq is fixed as long as
6547 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6548 	 * relationship between the IPSQ and xop cannot change.  However,
6549 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6550 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6551 	 * waking up all ills in the xop when it becomes available.
6552 	 */
6553 	for (;;) {
6554 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6555 		mutex_enter(&ill->ill_lock);
6556 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6557 			mutex_exit(&ill->ill_lock);
6558 			rw_exit(&ipst->ips_ill_g_lock);
6559 			return (B_FALSE);
6560 		}
6561 
6562 		ipsq = ill->ill_phyint->phyint_ipsq;
6563 		mutex_enter(&ipsq->ipsq_lock);
6564 		ipx = ipsq->ipsq_xop;
6565 		mutex_enter(&ipx->ipx_lock);
6566 
6567 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6568 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6569 		    waited_enough))
6570 			break;
6571 
6572 		rw_exit(&ipst->ips_ill_g_lock);
6573 
6574 		if (!force || ipx->ipx_writer != NULL) {
6575 			mutex_exit(&ipx->ipx_lock);
6576 			mutex_exit(&ipsq->ipsq_lock);
6577 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6578 		} else {
6579 			mutex_exit(&ipx->ipx_lock);
6580 			mutex_exit(&ipsq->ipsq_lock);
6581 			(void) cv_reltimedwait(&ill->ill_cv,
6582 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6583 			waited_enough = B_TRUE;
6584 		}
6585 		mutex_exit(&ill->ill_lock);
6586 	}
6587 
6588 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6589 	ASSERT(ipx->ipx_reentry_cnt == 0);
6590 	ipx->ipx_writer = curthread;
6591 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6592 	ipx->ipx_reentry_cnt++;
6593 #ifdef DEBUG
6594 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6595 #endif
6596 	mutex_exit(&ipx->ipx_lock);
6597 	mutex_exit(&ipsq->ipsq_lock);
6598 	mutex_exit(&ill->ill_lock);
6599 	rw_exit(&ipst->ips_ill_g_lock);
6600 
6601 	return (B_TRUE);
6602 }
6603 
6604 /*
6605  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6606  * across the call to the core interface ipsq_try_enter() and hence calls this
6607  * function directly. This is explained more fully in ipif_set_values().
6608  * In order to support the above constraint, ipsq_try_enter is implemented as
6609  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6610  */
6611 static ipsq_t *
6612 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6613     int type, boolean_t reentry_ok)
6614 {
6615 	ipsq_t	*ipsq;
6616 	ipxop_t	*ipx;
6617 	ip_stack_t *ipst = ill->ill_ipst;
6618 
6619 	/*
6620 	 * lock ordering:
6621 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6622 	 *
6623 	 * ipx of an ipsq can't change when ipsq_lock is held.
6624 	 */
6625 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6626 	GRAB_CONN_LOCK(q);
6627 	mutex_enter(&ill->ill_lock);
6628 	ipsq = ill->ill_phyint->phyint_ipsq;
6629 	mutex_enter(&ipsq->ipsq_lock);
6630 	ipx = ipsq->ipsq_xop;
6631 	mutex_enter(&ipx->ipx_lock);
6632 
6633 	/*
6634 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6635 	 *    (Note: If the caller does not specify reentry_ok then neither
6636 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6637 	 *    again. Otherwise it can lead to an infinite loop
6638 	 * 2. Enter the ipsq if there is no current writer and this attempted
6639 	 *    entry is part of the current operation
6640 	 * 3. Enter the ipsq if there is no current writer and this is a new
6641 	 *    operation and the operation queue is empty and there is no
6642 	 *    operation currently in progress and if all previously initiated
6643 	 *    DLPI operations have completed.
6644 	 */
6645 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6646 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6647 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6648 	    ipsq_dlpi_done(ipsq))))) {
6649 		/* Success. */
6650 		ipx->ipx_reentry_cnt++;
6651 		ipx->ipx_writer = curthread;
6652 		ipx->ipx_forced = B_FALSE;
6653 		mutex_exit(&ipx->ipx_lock);
6654 		mutex_exit(&ipsq->ipsq_lock);
6655 		mutex_exit(&ill->ill_lock);
6656 		RELEASE_CONN_LOCK(q);
6657 #ifdef DEBUG
6658 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6659 #endif
6660 		return (ipsq);
6661 	}
6662 
6663 	if (func != NULL)
6664 		ipsq_enq(ipsq, q, mp, func, type, ill);
6665 
6666 	mutex_exit(&ipx->ipx_lock);
6667 	mutex_exit(&ipsq->ipsq_lock);
6668 	mutex_exit(&ill->ill_lock);
6669 	RELEASE_CONN_LOCK(q);
6670 	return (NULL);
6671 }
6672 
6673 /*
6674  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6675  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6676  * There is one ipsq per phyint. The ipsq
6677  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6678  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6679  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6680  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6681  * up the interface) and are enqueued in ipx_mphead.
6682  *
6683  * If a thread does not want to reenter the ipsq when it is already writer,
6684  * it must make sure that the specified reentry point to be called later
6685  * when the ipsq is empty, nor any code path starting from the specified reentry
6686  * point must never ever try to enter the ipsq again. Otherwise it can lead
6687  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6688  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6689  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6690  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6691  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6692  * ioctl if the current ioctl has completed. If the current ioctl is still
6693  * in progress it simply returns. The current ioctl could be waiting for
6694  * a response from another module (the driver or could be waiting for
6695  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6696  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6697  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6698  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6699  * all associated DLPI operations have completed.
6700  */
6701 
6702 /*
6703  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6704  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6705  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6706  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6707  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6708  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6709  */
6710 ipsq_t *
6711 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6712     ipsq_func_t func, int type, boolean_t reentry_ok)
6713 {
6714 	ip_stack_t	*ipst;
6715 	ipsq_t		*ipsq;
6716 
6717 	/* Only 1 of ipif or ill can be specified */
6718 	ASSERT((ipif != NULL) ^ (ill != NULL));
6719 
6720 	if (ipif != NULL)
6721 		ill = ipif->ipif_ill;
6722 	ipst = ill->ill_ipst;
6723 
6724 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6725 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6726 	rw_exit(&ipst->ips_ill_g_lock);
6727 
6728 	return (ipsq);
6729 }
6730 
6731 /*
6732  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6733  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6734  * cannot be entered, the mp is queued for completion.
6735  */
6736 void
6737 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6738     boolean_t reentry_ok)
6739 {
6740 	ipsq_t	*ipsq;
6741 
6742 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6743 
6744 	/*
6745 	 * Drop the caller's refhold on the ill.  This is safe since we either
6746 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6747 	 * IPSQ, in which case we return without accessing ill anymore.  This
6748 	 * is needed because func needs to see the correct refcount.
6749 	 * e.g. removeif can work only then.
6750 	 */
6751 	ill_refrele(ill);
6752 	if (ipsq != NULL) {
6753 		(*func)(ipsq, q, mp, NULL);
6754 		ipsq_exit(ipsq);
6755 	}
6756 }
6757 
6758 /*
6759  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6760  * prior to exiting.  Caller must be writer on the specified IPSQ.
6761  */
6762 void
6763 ipsq_exit(ipsq_t *ipsq)
6764 {
6765 	mblk_t *mp;
6766 	ipsq_t *mp_ipsq;
6767 	queue_t	*q;
6768 	phyint_t *phyi;
6769 	ipsq_func_t func;
6770 
6771 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6772 
6773 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6774 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6775 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6776 		return;
6777 	}
6778 
6779 	for (;;) {
6780 		phyi = ipsq->ipsq_phyint;
6781 		mp = ipsq_dq(ipsq);
6782 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6783 
6784 		/*
6785 		 * If we've changed to a new IPSQ, and the phyint associated
6786 		 * with the old one has gone away, free the old IPSQ.  Note
6787 		 * that this cannot happen while the IPSQ is in a group.
6788 		 */
6789 		if (mp_ipsq != ipsq && phyi == NULL) {
6790 			ASSERT(ipsq->ipsq_next == ipsq);
6791 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6792 			ipsq_delete(ipsq);
6793 		}
6794 
6795 		if (mp == NULL)
6796 			break;
6797 
6798 		q = mp->b_queue;
6799 		func = (ipsq_func_t)mp->b_prev;
6800 		ipsq = mp_ipsq;
6801 		mp->b_next = mp->b_prev = NULL;
6802 		mp->b_queue = NULL;
6803 
6804 		/*
6805 		 * If 'q' is an conn queue, it is valid, since we did a
6806 		 * a refhold on the conn at the start of the ioctl.
6807 		 * If 'q' is an ill queue, it is valid, since close of an
6808 		 * ill will clean up its IPSQ.
6809 		 */
6810 		(*func)(ipsq, q, mp, NULL);
6811 	}
6812 }
6813 
6814 /*
6815  * Used to start any igmp or mld timers that could not be started
6816  * while holding ill_mcast_lock. The timers can't be started while holding
6817  * the lock, since mld/igmp_start_timers may need to call untimeout()
6818  * which can't be done while holding the lock which the timeout handler
6819  * acquires. Otherwise
6820  * there could be a deadlock since the timeout handlers
6821  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6822  * ill_mcast_lock.
6823  */
6824 void
6825 ill_mcast_timer_start(ip_stack_t *ipst)
6826 {
6827 	int		next;
6828 
6829 	mutex_enter(&ipst->ips_igmp_timer_lock);
6830 	next = ipst->ips_igmp_deferred_next;
6831 	ipst->ips_igmp_deferred_next = INFINITY;
6832 	mutex_exit(&ipst->ips_igmp_timer_lock);
6833 
6834 	if (next != INFINITY)
6835 		igmp_start_timers(next, ipst);
6836 
6837 	mutex_enter(&ipst->ips_mld_timer_lock);
6838 	next = ipst->ips_mld_deferred_next;
6839 	ipst->ips_mld_deferred_next = INFINITY;
6840 	mutex_exit(&ipst->ips_mld_timer_lock);
6841 
6842 	if (next != INFINITY)
6843 		mld_start_timers(next, ipst);
6844 }
6845 
6846 /*
6847  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6848  * and `ioccmd'.
6849  */
6850 void
6851 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6852 {
6853 	ill_t *ill = ipif->ipif_ill;
6854 	ipxop_t *ipx = ipsq->ipsq_xop;
6855 
6856 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6857 	ASSERT(ipx->ipx_current_ipif == NULL);
6858 	ASSERT(ipx->ipx_current_ioctl == 0);
6859 
6860 	ipx->ipx_current_done = B_FALSE;
6861 	ipx->ipx_current_ioctl = ioccmd;
6862 	mutex_enter(&ipx->ipx_lock);
6863 	ipx->ipx_current_ipif = ipif;
6864 	mutex_exit(&ipx->ipx_lock);
6865 
6866 	/*
6867 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6868 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6869 	 * references to the ipif (so that the references will eventually
6870 	 * drop to zero) and also prevents any "get" operations (e.g.,
6871 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6872 	 * operation has completed and the ipif is again in a stable state.
6873 	 *
6874 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6875 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6876 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6877 	 * ipifs will be affected.
6878 	 *
6879 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6880 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6881 	 * operate on.
6882 	 */
6883 	switch (ioccmd) {
6884 	case SIOCLIFREMOVEIF:
6885 		break;
6886 	case 0:
6887 		mutex_enter(&ill->ill_lock);
6888 		ipif = ipif->ipif_ill->ill_ipif;
6889 		for (; ipif != NULL; ipif = ipif->ipif_next)
6890 			ipif->ipif_state_flags |= IPIF_CHANGING;
6891 		mutex_exit(&ill->ill_lock);
6892 		break;
6893 	default:
6894 		mutex_enter(&ill->ill_lock);
6895 		ipif->ipif_state_flags |= IPIF_CHANGING;
6896 		mutex_exit(&ill->ill_lock);
6897 	}
6898 }
6899 
6900 /*
6901  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6902  * the next exclusive operation to begin once we ipsq_exit().  However, if
6903  * pending DLPI operations remain, then we will wait for the queue to drain
6904  * before allowing the next exclusive operation to begin.  This ensures that
6905  * DLPI operations from one exclusive operation are never improperly processed
6906  * as part of a subsequent exclusive operation.
6907  */
6908 void
6909 ipsq_current_finish(ipsq_t *ipsq)
6910 {
6911 	ipxop_t	*ipx = ipsq->ipsq_xop;
6912 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6913 	ipif_t	*ipif = ipx->ipx_current_ipif;
6914 
6915 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6916 
6917 	/*
6918 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6919 	 * (but in that case, IPIF_CHANGING will already be clear and no
6920 	 * pending DLPI messages can remain).
6921 	 */
6922 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6923 		ill_t *ill = ipif->ipif_ill;
6924 
6925 		mutex_enter(&ill->ill_lock);
6926 		dlpi_pending = ill->ill_dlpi_pending;
6927 		if (ipx->ipx_current_ioctl == 0) {
6928 			ipif = ill->ill_ipif;
6929 			for (; ipif != NULL; ipif = ipif->ipif_next)
6930 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6931 		} else {
6932 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6933 		}
6934 		mutex_exit(&ill->ill_lock);
6935 	}
6936 
6937 	ASSERT(!ipx->ipx_current_done);
6938 	ipx->ipx_current_done = B_TRUE;
6939 	ipx->ipx_current_ioctl = 0;
6940 	if (dlpi_pending == DL_PRIM_INVAL) {
6941 		mutex_enter(&ipx->ipx_lock);
6942 		ipx->ipx_current_ipif = NULL;
6943 		mutex_exit(&ipx->ipx_lock);
6944 	}
6945 }
6946 
6947 /*
6948  * The ill is closing. Flush all messages on the ipsq that originated
6949  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6950  * for this ill since ipsq_enter could not have entered until then.
6951  * New messages can't be queued since the CONDEMNED flag is set.
6952  */
6953 static void
6954 ipsq_flush(ill_t *ill)
6955 {
6956 	queue_t	*q;
6957 	mblk_t	*prev;
6958 	mblk_t	*mp;
6959 	mblk_t	*mp_next;
6960 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6961 
6962 	ASSERT(IAM_WRITER_ILL(ill));
6963 
6964 	/*
6965 	 * Flush any messages sent up by the driver.
6966 	 */
6967 	mutex_enter(&ipx->ipx_lock);
6968 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6969 		mp_next = mp->b_next;
6970 		q = mp->b_queue;
6971 		if (q == ill->ill_rq || q == ill->ill_wq) {
6972 			/* dequeue mp */
6973 			if (prev == NULL)
6974 				ipx->ipx_mphead = mp->b_next;
6975 			else
6976 				prev->b_next = mp->b_next;
6977 			if (ipx->ipx_mptail == mp) {
6978 				ASSERT(mp_next == NULL);
6979 				ipx->ipx_mptail = prev;
6980 			}
6981 			inet_freemsg(mp);
6982 		} else {
6983 			prev = mp;
6984 		}
6985 	}
6986 	mutex_exit(&ipx->ipx_lock);
6987 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6988 	ipsq_xopq_mp_cleanup(ill, NULL);
6989 }
6990 
6991 /*
6992  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6993  * and return the associated ipif.
6994  * Return value:
6995  *	Non zero: An error has occurred. ci may not be filled out.
6996  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6997  *	a held ipif in ci.ci_ipif.
6998  */
6999 int
7000 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7001     cmd_info_t *ci)
7002 {
7003 	char		*name;
7004 	struct ifreq    *ifr;
7005 	struct lifreq    *lifr;
7006 	ipif_t		*ipif = NULL;
7007 	ill_t		*ill;
7008 	conn_t		*connp;
7009 	boolean_t	isv6;
7010 	int		err;
7011 	mblk_t		*mp1;
7012 	zoneid_t	zoneid;
7013 	ip_stack_t	*ipst;
7014 
7015 	if (q->q_next != NULL) {
7016 		ill = (ill_t *)q->q_ptr;
7017 		isv6 = ill->ill_isv6;
7018 		connp = NULL;
7019 		zoneid = ALL_ZONES;
7020 		ipst = ill->ill_ipst;
7021 	} else {
7022 		ill = NULL;
7023 		connp = Q_TO_CONN(q);
7024 		isv6 = (connp->conn_family == AF_INET6);
7025 		zoneid = connp->conn_zoneid;
7026 		if (zoneid == GLOBAL_ZONEID) {
7027 			/* global zone can access ipifs in all zones */
7028 			zoneid = ALL_ZONES;
7029 		}
7030 		ipst = connp->conn_netstack->netstack_ip;
7031 	}
7032 
7033 	/* Has been checked in ip_wput_nondata */
7034 	mp1 = mp->b_cont->b_cont;
7035 
7036 	if (ipip->ipi_cmd_type == IF_CMD) {
7037 		/* This a old style SIOC[GS]IF* command */
7038 		ifr = (struct ifreq *)mp1->b_rptr;
7039 		/*
7040 		 * Null terminate the string to protect against buffer
7041 		 * overrun. String was generated by user code and may not
7042 		 * be trusted.
7043 		 */
7044 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7045 		name = ifr->ifr_name;
7046 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7047 		ci->ci_sin6 = NULL;
7048 		ci->ci_lifr = (struct lifreq *)ifr;
7049 	} else {
7050 		/* This a new style SIOC[GS]LIF* command */
7051 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7052 		lifr = (struct lifreq *)mp1->b_rptr;
7053 		/*
7054 		 * Null terminate the string to protect against buffer
7055 		 * overrun. String was generated by user code and may not
7056 		 * be trusted.
7057 		 */
7058 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7059 		name = lifr->lifr_name;
7060 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7061 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7062 		ci->ci_lifr = lifr;
7063 	}
7064 
7065 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7066 		/*
7067 		 * The ioctl will be failed if the ioctl comes down
7068 		 * an conn stream
7069 		 */
7070 		if (ill == NULL) {
7071 			/*
7072 			 * Not an ill queue, return EINVAL same as the
7073 			 * old error code.
7074 			 */
7075 			return (ENXIO);
7076 		}
7077 		ipif = ill->ill_ipif;
7078 		ipif_refhold(ipif);
7079 	} else {
7080 		/*
7081 		 * Ensure that ioctls don't see any internal state changes
7082 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7083 		 * set.
7084 		 */
7085 		ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7086 		    isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7087 		if (ipif == NULL) {
7088 			if (err == EINPROGRESS)
7089 				return (err);
7090 			err = 0;	/* Ensure we don't use it below */
7091 		}
7092 	}
7093 
7094 	/*
7095 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7096 	 */
7097 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7098 		ipif_refrele(ipif);
7099 		return (ENXIO);
7100 	}
7101 
7102 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7103 	    name[0] == '\0') {
7104 		/*
7105 		 * Handle a or a SIOC?IF* with a null name
7106 		 * during plumb (on the ill queue before the I_PLINK).
7107 		 */
7108 		ipif = ill->ill_ipif;
7109 		ipif_refhold(ipif);
7110 	}
7111 
7112 	if (ipif == NULL)
7113 		return (ENXIO);
7114 
7115 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7116 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7117 
7118 	ci->ci_ipif = ipif;
7119 	return (0);
7120 }
7121 
7122 /*
7123  * Return the total number of ipifs.
7124  */
7125 static uint_t
7126 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7127 {
7128 	uint_t numifs = 0;
7129 	ill_t	*ill;
7130 	ill_walk_context_t	ctx;
7131 	ipif_t	*ipif;
7132 
7133 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7134 	ill = ILL_START_WALK_V4(&ctx, ipst);
7135 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7136 		if (IS_UNDER_IPMP(ill))
7137 			continue;
7138 		for (ipif = ill->ill_ipif; ipif != NULL;
7139 		    ipif = ipif->ipif_next) {
7140 			if (ipif->ipif_zoneid == zoneid ||
7141 			    ipif->ipif_zoneid == ALL_ZONES)
7142 				numifs++;
7143 		}
7144 	}
7145 	rw_exit(&ipst->ips_ill_g_lock);
7146 	return (numifs);
7147 }
7148 
7149 /*
7150  * Return the total number of ipifs.
7151  */
7152 static uint_t
7153 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7154 {
7155 	uint_t numifs = 0;
7156 	ill_t	*ill;
7157 	ipif_t	*ipif;
7158 	ill_walk_context_t	ctx;
7159 
7160 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7161 
7162 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7163 	if (family == AF_INET)
7164 		ill = ILL_START_WALK_V4(&ctx, ipst);
7165 	else if (family == AF_INET6)
7166 		ill = ILL_START_WALK_V6(&ctx, ipst);
7167 	else
7168 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7169 
7170 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7171 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7172 			continue;
7173 
7174 		for (ipif = ill->ill_ipif; ipif != NULL;
7175 		    ipif = ipif->ipif_next) {
7176 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7177 			    !(lifn_flags & LIFC_NOXMIT))
7178 				continue;
7179 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7180 			    !(lifn_flags & LIFC_TEMPORARY))
7181 				continue;
7182 			if (((ipif->ipif_flags &
7183 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7184 			    IPIF_DEPRECATED)) ||
7185 			    IS_LOOPBACK(ill) ||
7186 			    !(ipif->ipif_flags & IPIF_UP)) &&
7187 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7188 				continue;
7189 
7190 			if (zoneid != ipif->ipif_zoneid &&
7191 			    ipif->ipif_zoneid != ALL_ZONES &&
7192 			    (zoneid != GLOBAL_ZONEID ||
7193 			    !(lifn_flags & LIFC_ALLZONES)))
7194 				continue;
7195 
7196 			numifs++;
7197 		}
7198 	}
7199 	rw_exit(&ipst->ips_ill_g_lock);
7200 	return (numifs);
7201 }
7202 
7203 uint_t
7204 ip_get_lifsrcofnum(ill_t *ill)
7205 {
7206 	uint_t numifs = 0;
7207 	ill_t	*ill_head = ill;
7208 	ip_stack_t	*ipst = ill->ill_ipst;
7209 
7210 	/*
7211 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7212 	 * other thread may be trying to relink the ILLs in this usesrc group
7213 	 * and adjusting the ill_usesrc_grp_next pointers
7214 	 */
7215 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7216 	if ((ill->ill_usesrc_ifindex == 0) &&
7217 	    (ill->ill_usesrc_grp_next != NULL)) {
7218 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7219 		    ill = ill->ill_usesrc_grp_next)
7220 			numifs++;
7221 	}
7222 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7223 
7224 	return (numifs);
7225 }
7226 
7227 /* Null values are passed in for ipif, sin, and ifreq */
7228 /* ARGSUSED */
7229 int
7230 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7231     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7232 {
7233 	int *nump;
7234 	conn_t *connp = Q_TO_CONN(q);
7235 
7236 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7237 
7238 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7239 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7240 
7241 	*nump = ip_get_numifs(connp->conn_zoneid,
7242 	    connp->conn_netstack->netstack_ip);
7243 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7244 	return (0);
7245 }
7246 
7247 /* Null values are passed in for ipif, sin, and ifreq */
7248 /* ARGSUSED */
7249 int
7250 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7251     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7252 {
7253 	struct lifnum *lifn;
7254 	mblk_t	*mp1;
7255 	conn_t *connp = Q_TO_CONN(q);
7256 
7257 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7258 
7259 	/* Existence checked in ip_wput_nondata */
7260 	mp1 = mp->b_cont->b_cont;
7261 
7262 	lifn = (struct lifnum *)mp1->b_rptr;
7263 	switch (lifn->lifn_family) {
7264 	case AF_UNSPEC:
7265 	case AF_INET:
7266 	case AF_INET6:
7267 		break;
7268 	default:
7269 		return (EAFNOSUPPORT);
7270 	}
7271 
7272 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7273 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7274 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7275 	return (0);
7276 }
7277 
7278 /* ARGSUSED */
7279 int
7280 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7281     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7282 {
7283 	STRUCT_HANDLE(ifconf, ifc);
7284 	mblk_t *mp1;
7285 	struct iocblk *iocp;
7286 	struct ifreq *ifr;
7287 	ill_walk_context_t	ctx;
7288 	ill_t	*ill;
7289 	ipif_t	*ipif;
7290 	struct sockaddr_in *sin;
7291 	int32_t	ifclen;
7292 	zoneid_t zoneid;
7293 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7294 
7295 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7296 
7297 	ip1dbg(("ip_sioctl_get_ifconf"));
7298 	/* Existence verified in ip_wput_nondata */
7299 	mp1 = mp->b_cont->b_cont;
7300 	iocp = (struct iocblk *)mp->b_rptr;
7301 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7302 
7303 	/*
7304 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7305 	 * the user buffer address and length into which the list of struct
7306 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7307 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7308 	 * the SIOCGIFCONF operation was redefined to simply provide
7309 	 * a large output buffer into which we are supposed to jam the ifreq
7310 	 * array.  The same ioctl command code was used, despite the fact that
7311 	 * both the applications and the kernel code had to change, thus making
7312 	 * it impossible to support both interfaces.
7313 	 *
7314 	 * For reasons not good enough to try to explain, the following
7315 	 * algorithm is used for deciding what to do with one of these:
7316 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7317 	 * form with the output buffer coming down as the continuation message.
7318 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7319 	 * and we have to copy in the ifconf structure to find out how big the
7320 	 * output buffer is and where to copy out to.  Sure no problem...
7321 	 *
7322 	 */
7323 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7324 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7325 		int numifs = 0;
7326 		size_t ifc_bufsize;
7327 
7328 		/*
7329 		 * Must be (better be!) continuation of a TRANSPARENT
7330 		 * IOCTL.  We just copied in the ifconf structure.
7331 		 */
7332 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7333 		    (struct ifconf *)mp1->b_rptr);
7334 
7335 		/*
7336 		 * Allocate a buffer to hold requested information.
7337 		 *
7338 		 * If ifc_len is larger than what is needed, we only
7339 		 * allocate what we will use.
7340 		 *
7341 		 * If ifc_len is smaller than what is needed, return
7342 		 * EINVAL.
7343 		 *
7344 		 * XXX: the ill_t structure can hava 2 counters, for
7345 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7346 		 * number of interfaces for a device, so we don't need
7347 		 * to count them here...
7348 		 */
7349 		numifs = ip_get_numifs(zoneid, ipst);
7350 
7351 		ifclen = STRUCT_FGET(ifc, ifc_len);
7352 		ifc_bufsize = numifs * sizeof (struct ifreq);
7353 		if (ifc_bufsize > ifclen) {
7354 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7355 				/* old behaviour */
7356 				return (EINVAL);
7357 			} else {
7358 				ifc_bufsize = ifclen;
7359 			}
7360 		}
7361 
7362 		mp1 = mi_copyout_alloc(q, mp,
7363 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7364 		if (mp1 == NULL)
7365 			return (ENOMEM);
7366 
7367 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7368 	}
7369 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7370 	/*
7371 	 * the SIOCGIFCONF ioctl only knows about
7372 	 * IPv4 addresses, so don't try to tell
7373 	 * it about interfaces with IPv6-only
7374 	 * addresses. (Last parm 'isv6' is B_FALSE)
7375 	 */
7376 
7377 	ifr = (struct ifreq *)mp1->b_rptr;
7378 
7379 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7380 	ill = ILL_START_WALK_V4(&ctx, ipst);
7381 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7382 		if (IS_UNDER_IPMP(ill))
7383 			continue;
7384 		for (ipif = ill->ill_ipif; ipif != NULL;
7385 		    ipif = ipif->ipif_next) {
7386 			if (zoneid != ipif->ipif_zoneid &&
7387 			    ipif->ipif_zoneid != ALL_ZONES)
7388 				continue;
7389 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7390 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7391 					/* old behaviour */
7392 					rw_exit(&ipst->ips_ill_g_lock);
7393 					return (EINVAL);
7394 				} else {
7395 					goto if_copydone;
7396 				}
7397 			}
7398 			ipif_get_name(ipif, ifr->ifr_name,
7399 			    sizeof (ifr->ifr_name));
7400 			sin = (sin_t *)&ifr->ifr_addr;
7401 			*sin = sin_null;
7402 			sin->sin_family = AF_INET;
7403 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7404 			ifr++;
7405 		}
7406 	}
7407 if_copydone:
7408 	rw_exit(&ipst->ips_ill_g_lock);
7409 	mp1->b_wptr = (uchar_t *)ifr;
7410 
7411 	if (STRUCT_BUF(ifc) != NULL) {
7412 		STRUCT_FSET(ifc, ifc_len,
7413 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7414 	}
7415 	return (0);
7416 }
7417 
7418 /*
7419  * Get the interfaces using the address hosted on the interface passed in,
7420  * as a source adddress
7421  */
7422 /* ARGSUSED */
7423 int
7424 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7425     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7426 {
7427 	mblk_t *mp1;
7428 	ill_t	*ill, *ill_head;
7429 	ipif_t	*ipif, *orig_ipif;
7430 	int	numlifs = 0;
7431 	size_t	lifs_bufsize, lifsmaxlen;
7432 	struct	lifreq *lifr;
7433 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7434 	uint_t	ifindex;
7435 	zoneid_t zoneid;
7436 	boolean_t isv6 = B_FALSE;
7437 	struct	sockaddr_in	*sin;
7438 	struct	sockaddr_in6	*sin6;
7439 	STRUCT_HANDLE(lifsrcof, lifs);
7440 	ip_stack_t		*ipst;
7441 
7442 	ipst = CONNQ_TO_IPST(q);
7443 
7444 	ASSERT(q->q_next == NULL);
7445 
7446 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7447 
7448 	/* Existence verified in ip_wput_nondata */
7449 	mp1 = mp->b_cont->b_cont;
7450 
7451 	/*
7452 	 * Must be (better be!) continuation of a TRANSPARENT
7453 	 * IOCTL.  We just copied in the lifsrcof structure.
7454 	 */
7455 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7456 	    (struct lifsrcof *)mp1->b_rptr);
7457 
7458 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7459 		return (EINVAL);
7460 
7461 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7462 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7463 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7464 	if (ipif == NULL) {
7465 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7466 		    ifindex));
7467 		return (ENXIO);
7468 	}
7469 
7470 	/* Allocate a buffer to hold requested information */
7471 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7472 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7473 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7474 	/* The actual size needed is always returned in lifs_len */
7475 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7476 
7477 	/* If the amount we need is more than what is passed in, abort */
7478 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7479 		ipif_refrele(ipif);
7480 		return (0);
7481 	}
7482 
7483 	mp1 = mi_copyout_alloc(q, mp,
7484 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7485 	if (mp1 == NULL) {
7486 		ipif_refrele(ipif);
7487 		return (ENOMEM);
7488 	}
7489 
7490 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7491 	bzero(mp1->b_rptr, lifs_bufsize);
7492 
7493 	lifr = (struct lifreq *)mp1->b_rptr;
7494 
7495 	ill = ill_head = ipif->ipif_ill;
7496 	orig_ipif = ipif;
7497 
7498 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7499 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7500 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7501 
7502 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7503 	for (; (ill != NULL) && (ill != ill_head);
7504 	    ill = ill->ill_usesrc_grp_next) {
7505 
7506 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7507 			break;
7508 
7509 		ipif = ill->ill_ipif;
7510 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7511 		if (ipif->ipif_isv6) {
7512 			sin6 = (sin6_t *)&lifr->lifr_addr;
7513 			*sin6 = sin6_null;
7514 			sin6->sin6_family = AF_INET6;
7515 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7516 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7517 			    &ipif->ipif_v6net_mask);
7518 		} else {
7519 			sin = (sin_t *)&lifr->lifr_addr;
7520 			*sin = sin_null;
7521 			sin->sin_family = AF_INET;
7522 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7523 			lifr->lifr_addrlen = ip_mask_to_plen(
7524 			    ipif->ipif_net_mask);
7525 		}
7526 		lifr++;
7527 	}
7528 	rw_exit(&ipst->ips_ill_g_lock);
7529 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7530 	ipif_refrele(orig_ipif);
7531 	mp1->b_wptr = (uchar_t *)lifr;
7532 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7533 
7534 	return (0);
7535 }
7536 
7537 /* ARGSUSED */
7538 int
7539 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7540     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7541 {
7542 	mblk_t *mp1;
7543 	int	list;
7544 	ill_t	*ill;
7545 	ipif_t	*ipif;
7546 	int	flags;
7547 	int	numlifs = 0;
7548 	size_t	lifc_bufsize;
7549 	struct	lifreq *lifr;
7550 	sa_family_t	family;
7551 	struct	sockaddr_in	*sin;
7552 	struct	sockaddr_in6	*sin6;
7553 	ill_walk_context_t	ctx;
7554 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7555 	int32_t	lifclen;
7556 	zoneid_t zoneid;
7557 	STRUCT_HANDLE(lifconf, lifc);
7558 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7559 
7560 	ip1dbg(("ip_sioctl_get_lifconf"));
7561 
7562 	ASSERT(q->q_next == NULL);
7563 
7564 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7565 
7566 	/* Existence verified in ip_wput_nondata */
7567 	mp1 = mp->b_cont->b_cont;
7568 
7569 	/*
7570 	 * An extended version of SIOCGIFCONF that takes an
7571 	 * additional address family and flags field.
7572 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7573 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7574 	 * interfaces are omitted.
7575 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7576 	 * unless LIFC_TEMPORARY is specified.
7577 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7578 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7579 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7580 	 * has priority over LIFC_NOXMIT.
7581 	 */
7582 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7583 
7584 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7585 		return (EINVAL);
7586 
7587 	/*
7588 	 * Must be (better be!) continuation of a TRANSPARENT
7589 	 * IOCTL.  We just copied in the lifconf structure.
7590 	 */
7591 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7592 
7593 	family = STRUCT_FGET(lifc, lifc_family);
7594 	flags = STRUCT_FGET(lifc, lifc_flags);
7595 
7596 	switch (family) {
7597 	case AF_UNSPEC:
7598 		/*
7599 		 * walk all ILL's.
7600 		 */
7601 		list = MAX_G_HEADS;
7602 		break;
7603 	case AF_INET:
7604 		/*
7605 		 * walk only IPV4 ILL's.
7606 		 */
7607 		list = IP_V4_G_HEAD;
7608 		break;
7609 	case AF_INET6:
7610 		/*
7611 		 * walk only IPV6 ILL's.
7612 		 */
7613 		list = IP_V6_G_HEAD;
7614 		break;
7615 	default:
7616 		return (EAFNOSUPPORT);
7617 	}
7618 
7619 	/*
7620 	 * Allocate a buffer to hold requested information.
7621 	 *
7622 	 * If lifc_len is larger than what is needed, we only
7623 	 * allocate what we will use.
7624 	 *
7625 	 * If lifc_len is smaller than what is needed, return
7626 	 * EINVAL.
7627 	 */
7628 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7629 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7630 	lifclen = STRUCT_FGET(lifc, lifc_len);
7631 	if (lifc_bufsize > lifclen) {
7632 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7633 			return (EINVAL);
7634 		else
7635 			lifc_bufsize = lifclen;
7636 	}
7637 
7638 	mp1 = mi_copyout_alloc(q, mp,
7639 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7640 	if (mp1 == NULL)
7641 		return (ENOMEM);
7642 
7643 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7644 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7645 
7646 	lifr = (struct lifreq *)mp1->b_rptr;
7647 
7648 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7649 	ill = ill_first(list, list, &ctx, ipst);
7650 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7651 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7652 			continue;
7653 
7654 		for (ipif = ill->ill_ipif; ipif != NULL;
7655 		    ipif = ipif->ipif_next) {
7656 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7657 			    !(flags & LIFC_NOXMIT))
7658 				continue;
7659 
7660 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7661 			    !(flags & LIFC_TEMPORARY))
7662 				continue;
7663 
7664 			if (((ipif->ipif_flags &
7665 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7666 			    IPIF_DEPRECATED)) ||
7667 			    IS_LOOPBACK(ill) ||
7668 			    !(ipif->ipif_flags & IPIF_UP)) &&
7669 			    (flags & LIFC_EXTERNAL_SOURCE))
7670 				continue;
7671 
7672 			if (zoneid != ipif->ipif_zoneid &&
7673 			    ipif->ipif_zoneid != ALL_ZONES &&
7674 			    (zoneid != GLOBAL_ZONEID ||
7675 			    !(flags & LIFC_ALLZONES)))
7676 				continue;
7677 
7678 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7679 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7680 					rw_exit(&ipst->ips_ill_g_lock);
7681 					return (EINVAL);
7682 				} else {
7683 					goto lif_copydone;
7684 				}
7685 			}
7686 
7687 			ipif_get_name(ipif, lifr->lifr_name,
7688 			    sizeof (lifr->lifr_name));
7689 			lifr->lifr_type = ill->ill_type;
7690 			if (ipif->ipif_isv6) {
7691 				sin6 = (sin6_t *)&lifr->lifr_addr;
7692 				*sin6 = sin6_null;
7693 				sin6->sin6_family = AF_INET6;
7694 				sin6->sin6_addr =
7695 				    ipif->ipif_v6lcl_addr;
7696 				lifr->lifr_addrlen =
7697 				    ip_mask_to_plen_v6(
7698 				    &ipif->ipif_v6net_mask);
7699 			} else {
7700 				sin = (sin_t *)&lifr->lifr_addr;
7701 				*sin = sin_null;
7702 				sin->sin_family = AF_INET;
7703 				sin->sin_addr.s_addr =
7704 				    ipif->ipif_lcl_addr;
7705 				lifr->lifr_addrlen =
7706 				    ip_mask_to_plen(
7707 				    ipif->ipif_net_mask);
7708 			}
7709 			lifr++;
7710 		}
7711 	}
7712 lif_copydone:
7713 	rw_exit(&ipst->ips_ill_g_lock);
7714 
7715 	mp1->b_wptr = (uchar_t *)lifr;
7716 	if (STRUCT_BUF(lifc) != NULL) {
7717 		STRUCT_FSET(lifc, lifc_len,
7718 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7719 	}
7720 	return (0);
7721 }
7722 
7723 static void
7724 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7725 {
7726 	ip6_asp_t *table;
7727 	size_t table_size;
7728 	mblk_t *data_mp;
7729 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7730 	ip_stack_t	*ipst;
7731 
7732 	if (q->q_next == NULL)
7733 		ipst = CONNQ_TO_IPST(q);
7734 	else
7735 		ipst = ILLQ_TO_IPST(q);
7736 
7737 	/* These two ioctls are I_STR only */
7738 	if (iocp->ioc_count == TRANSPARENT) {
7739 		miocnak(q, mp, 0, EINVAL);
7740 		return;
7741 	}
7742 
7743 	data_mp = mp->b_cont;
7744 	if (data_mp == NULL) {
7745 		/* The user passed us a NULL argument */
7746 		table = NULL;
7747 		table_size = iocp->ioc_count;
7748 	} else {
7749 		/*
7750 		 * The user provided a table.  The stream head
7751 		 * may have copied in the user data in chunks,
7752 		 * so make sure everything is pulled up
7753 		 * properly.
7754 		 */
7755 		if (MBLKL(data_mp) < iocp->ioc_count) {
7756 			mblk_t *new_data_mp;
7757 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7758 			    NULL) {
7759 				miocnak(q, mp, 0, ENOMEM);
7760 				return;
7761 			}
7762 			freemsg(data_mp);
7763 			data_mp = new_data_mp;
7764 			mp->b_cont = data_mp;
7765 		}
7766 		table = (ip6_asp_t *)data_mp->b_rptr;
7767 		table_size = iocp->ioc_count;
7768 	}
7769 
7770 	switch (iocp->ioc_cmd) {
7771 	case SIOCGIP6ADDRPOLICY:
7772 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7773 		if (iocp->ioc_rval == -1)
7774 			iocp->ioc_error = EINVAL;
7775 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7776 		else if (table != NULL &&
7777 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7778 			ip6_asp_t *src = table;
7779 			ip6_asp32_t *dst = (void *)table;
7780 			int count = table_size / sizeof (ip6_asp_t);
7781 			int i;
7782 
7783 			/*
7784 			 * We need to do an in-place shrink of the array
7785 			 * to match the alignment attributes of the
7786 			 * 32-bit ABI looking at it.
7787 			 */
7788 			/* LINTED: logical expression always true: op "||" */
7789 			ASSERT(sizeof (*src) > sizeof (*dst));
7790 			for (i = 1; i < count; i++)
7791 				bcopy(src + i, dst + i, sizeof (*dst));
7792 		}
7793 #endif
7794 		break;
7795 
7796 	case SIOCSIP6ADDRPOLICY:
7797 		ASSERT(mp->b_prev == NULL);
7798 		mp->b_prev = (void *)q;
7799 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7800 		/*
7801 		 * We pass in the datamodel here so that the ip6_asp_replace()
7802 		 * routine can handle converting from 32-bit to native formats
7803 		 * where necessary.
7804 		 *
7805 		 * A better way to handle this might be to convert the inbound
7806 		 * data structure here, and hang it off a new 'mp'; thus the
7807 		 * ip6_asp_replace() logic would always be dealing with native
7808 		 * format data structures..
7809 		 *
7810 		 * (An even simpler way to handle these ioctls is to just
7811 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7812 		 * and just recompile everything that depends on it.)
7813 		 */
7814 #endif
7815 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7816 		    iocp->ioc_flag & IOC_MODELS);
7817 		return;
7818 	}
7819 
7820 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7821 	qreply(q, mp);
7822 }
7823 
7824 static void
7825 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7826 {
7827 	mblk_t 		*data_mp;
7828 	struct dstinforeq	*dir;
7829 	uint8_t		*end, *cur;
7830 	in6_addr_t	*daddr, *saddr;
7831 	ipaddr_t	v4daddr;
7832 	ire_t		*ire;
7833 	ipaddr_t	v4setsrc;
7834 	in6_addr_t	v6setsrc;
7835 	char		*slabel, *dlabel;
7836 	boolean_t	isipv4;
7837 	int		match_ire;
7838 	ill_t		*dst_ill;
7839 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7840 	conn_t		*connp = Q_TO_CONN(q);
7841 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7842 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7843 	uint64_t	ipif_flags;
7844 
7845 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7846 
7847 	/*
7848 	 * This ioctl is I_STR only, and must have a
7849 	 * data mblk following the M_IOCTL mblk.
7850 	 */
7851 	data_mp = mp->b_cont;
7852 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7853 		miocnak(q, mp, 0, EINVAL);
7854 		return;
7855 	}
7856 
7857 	if (MBLKL(data_mp) < iocp->ioc_count) {
7858 		mblk_t *new_data_mp;
7859 
7860 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7861 			miocnak(q, mp, 0, ENOMEM);
7862 			return;
7863 		}
7864 		freemsg(data_mp);
7865 		data_mp = new_data_mp;
7866 		mp->b_cont = data_mp;
7867 	}
7868 	match_ire = MATCH_IRE_DSTONLY;
7869 
7870 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7871 	    end - cur >= sizeof (struct dstinforeq);
7872 	    cur += sizeof (struct dstinforeq)) {
7873 		dir = (struct dstinforeq *)cur;
7874 		daddr = &dir->dir_daddr;
7875 		saddr = &dir->dir_saddr;
7876 
7877 		/*
7878 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7879 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7880 		 * and ip_select_source_v6() do not.
7881 		 */
7882 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7883 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7884 
7885 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7886 		if (isipv4) {
7887 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7888 			v4setsrc = INADDR_ANY;
7889 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7890 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7891 			    NULL, NULL);
7892 		} else {
7893 			v6setsrc = ipv6_all_zeros;
7894 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7895 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7896 			    NULL, NULL);
7897 		}
7898 		ASSERT(ire != NULL);
7899 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7900 			ire_refrele(ire);
7901 			dir->dir_dreachable = 0;
7902 
7903 			/* move on to next dst addr */
7904 			continue;
7905 		}
7906 		dir->dir_dreachable = 1;
7907 
7908 		dst_ill = ire_nexthop_ill(ire);
7909 		if (dst_ill == NULL) {
7910 			ire_refrele(ire);
7911 			continue;
7912 		}
7913 
7914 		/* With ipmp we most likely look at the ipmp ill here */
7915 		dir->dir_dmactype = dst_ill->ill_mactype;
7916 
7917 		if (isipv4) {
7918 			ipaddr_t v4saddr;
7919 
7920 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7921 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7922 			    &v4saddr, NULL, &ipif_flags) != 0) {
7923 				v4saddr = INADDR_ANY;
7924 				ipif_flags = 0;
7925 			}
7926 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7927 		} else {
7928 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7929 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7930 			    saddr, NULL, &ipif_flags) != 0) {
7931 				*saddr = ipv6_all_zeros;
7932 				ipif_flags = 0;
7933 			}
7934 		}
7935 
7936 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7937 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7938 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7939 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7940 		ire_refrele(ire);
7941 		ill_refrele(dst_ill);
7942 	}
7943 	miocack(q, mp, iocp->ioc_count, 0);
7944 }
7945 
7946 /*
7947  * Check if this is an address assigned to this machine.
7948  * Skips interfaces that are down by using ire checks.
7949  * Translates mapped addresses to v4 addresses and then
7950  * treats them as such, returning true if the v4 address
7951  * associated with this mapped address is configured.
7952  * Note: Applications will have to be careful what they do
7953  * with the response; use of mapped addresses limits
7954  * what can be done with the socket, especially with
7955  * respect to socket options and ioctls - neither IPv4
7956  * options nor IPv6 sticky options/ancillary data options
7957  * may be used.
7958  */
7959 /* ARGSUSED */
7960 int
7961 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7962     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7963 {
7964 	struct sioc_addrreq *sia;
7965 	sin_t *sin;
7966 	ire_t *ire;
7967 	mblk_t *mp1;
7968 	zoneid_t zoneid;
7969 	ip_stack_t	*ipst;
7970 
7971 	ip1dbg(("ip_sioctl_tmyaddr"));
7972 
7973 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7974 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7975 	ipst = CONNQ_TO_IPST(q);
7976 
7977 	/* Existence verified in ip_wput_nondata */
7978 	mp1 = mp->b_cont->b_cont;
7979 	sia = (struct sioc_addrreq *)mp1->b_rptr;
7980 	sin = (sin_t *)&sia->sa_addr;
7981 	switch (sin->sin_family) {
7982 	case AF_INET6: {
7983 		sin6_t *sin6 = (sin6_t *)sin;
7984 
7985 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7986 			ipaddr_t v4_addr;
7987 
7988 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
7989 			    v4_addr);
7990 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
7991 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7992 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7993 		} else {
7994 			in6_addr_t v6addr;
7995 
7996 			v6addr = sin6->sin6_addr;
7997 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
7998 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7999 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8000 		}
8001 		break;
8002 	}
8003 	case AF_INET: {
8004 		ipaddr_t v4addr;
8005 
8006 		v4addr = sin->sin_addr.s_addr;
8007 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8008 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8009 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8010 		break;
8011 	}
8012 	default:
8013 		return (EAFNOSUPPORT);
8014 	}
8015 	if (ire != NULL) {
8016 		sia->sa_res = 1;
8017 		ire_refrele(ire);
8018 	} else {
8019 		sia->sa_res = 0;
8020 	}
8021 	return (0);
8022 }
8023 
8024 /*
8025  * Check if this is an address assigned on-link i.e. neighbor,
8026  * and makes sure it's reachable from the current zone.
8027  * Returns true for my addresses as well.
8028  * Translates mapped addresses to v4 addresses and then
8029  * treats them as such, returning true if the v4 address
8030  * associated with this mapped address is configured.
8031  * Note: Applications will have to be careful what they do
8032  * with the response; use of mapped addresses limits
8033  * what can be done with the socket, especially with
8034  * respect to socket options and ioctls - neither IPv4
8035  * options nor IPv6 sticky options/ancillary data options
8036  * may be used.
8037  */
8038 /* ARGSUSED */
8039 int
8040 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8041     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8042 {
8043 	struct sioc_addrreq *sia;
8044 	sin_t *sin;
8045 	mblk_t	*mp1;
8046 	ire_t *ire = NULL;
8047 	zoneid_t zoneid;
8048 	ip_stack_t	*ipst;
8049 
8050 	ip1dbg(("ip_sioctl_tonlink"));
8051 
8052 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8053 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8054 	ipst = CONNQ_TO_IPST(q);
8055 
8056 	/* Existence verified in ip_wput_nondata */
8057 	mp1 = mp->b_cont->b_cont;
8058 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8059 	sin = (sin_t *)&sia->sa_addr;
8060 
8061 	/*
8062 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8063 	 * to make sure we only look at on-link unicast address.
8064 	 */
8065 	switch (sin->sin_family) {
8066 	case AF_INET6: {
8067 		sin6_t *sin6 = (sin6_t *)sin;
8068 
8069 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8070 			ipaddr_t v4_addr;
8071 
8072 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8073 			    v4_addr);
8074 			if (!CLASSD(v4_addr)) {
8075 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8076 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8077 				    0, ipst, NULL);
8078 			}
8079 		} else {
8080 			in6_addr_t v6addr;
8081 
8082 			v6addr = sin6->sin6_addr;
8083 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8084 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8085 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8086 				    ipst, NULL);
8087 			}
8088 		}
8089 		break;
8090 	}
8091 	case AF_INET: {
8092 		ipaddr_t v4addr;
8093 
8094 		v4addr = sin->sin_addr.s_addr;
8095 		if (!CLASSD(v4addr)) {
8096 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8097 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8098 		}
8099 		break;
8100 	}
8101 	default:
8102 		return (EAFNOSUPPORT);
8103 	}
8104 	sia->sa_res = 0;
8105 	if (ire != NULL) {
8106 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8107 
8108 		if ((ire->ire_type & IRE_ONLINK) &&
8109 		    !(ire->ire_type & IRE_BROADCAST))
8110 			sia->sa_res = 1;
8111 		ire_refrele(ire);
8112 	}
8113 	return (0);
8114 }
8115 
8116 /*
8117  * TBD: implement when kernel maintaines a list of site prefixes.
8118  */
8119 /* ARGSUSED */
8120 int
8121 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8122     ip_ioctl_cmd_t *ipip, void *ifreq)
8123 {
8124 	return (ENXIO);
8125 }
8126 
8127 /* ARP IOCTLs. */
8128 /* ARGSUSED */
8129 int
8130 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8131     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8132 {
8133 	int		err;
8134 	ipaddr_t	ipaddr;
8135 	struct iocblk	*iocp;
8136 	conn_t		*connp;
8137 	struct arpreq	*ar;
8138 	struct xarpreq	*xar;
8139 	int		arp_flags, flags, alength;
8140 	uchar_t		*lladdr;
8141 	ip_stack_t	*ipst;
8142 	ill_t		*ill = ipif->ipif_ill;
8143 	ill_t		*proxy_ill = NULL;
8144 	ipmp_arpent_t	*entp = NULL;
8145 	boolean_t	proxyarp = B_FALSE;
8146 	boolean_t	if_arp_ioctl = B_FALSE;
8147 	ncec_t		*ncec = NULL;
8148 	nce_t		*nce;
8149 
8150 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8151 	connp = Q_TO_CONN(q);
8152 	ipst = connp->conn_netstack->netstack_ip;
8153 	iocp = (struct iocblk *)mp->b_rptr;
8154 
8155 	if (ipip->ipi_cmd_type == XARP_CMD) {
8156 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8157 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8158 		ar = NULL;
8159 
8160 		arp_flags = xar->xarp_flags;
8161 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8162 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8163 		/*
8164 		 * Validate against user's link layer address length
8165 		 * input and name and addr length limits.
8166 		 */
8167 		alength = ill->ill_phys_addr_length;
8168 		if (ipip->ipi_cmd == SIOCSXARP) {
8169 			if (alength != xar->xarp_ha.sdl_alen ||
8170 			    (alength + xar->xarp_ha.sdl_nlen >
8171 			    sizeof (xar->xarp_ha.sdl_data)))
8172 				return (EINVAL);
8173 		}
8174 	} else {
8175 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8176 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8177 		xar = NULL;
8178 
8179 		arp_flags = ar->arp_flags;
8180 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8181 		/*
8182 		 * Theoretically, the sa_family could tell us what link
8183 		 * layer type this operation is trying to deal with. By
8184 		 * common usage AF_UNSPEC means ethernet. We'll assume
8185 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8186 		 * for now. Our new SIOC*XARP ioctls can be used more
8187 		 * generally.
8188 		 *
8189 		 * If the underlying media happens to have a non 6 byte
8190 		 * address, arp module will fail set/get, but the del
8191 		 * operation will succeed.
8192 		 */
8193 		alength = 6;
8194 		if ((ipip->ipi_cmd != SIOCDARP) &&
8195 		    (alength != ill->ill_phys_addr_length)) {
8196 			return (EINVAL);
8197 		}
8198 	}
8199 
8200 	/* Translate ATF* flags to NCE* flags */
8201 	flags = 0;
8202 	if (arp_flags & ATF_AUTHORITY)
8203 		flags |= NCE_F_AUTHORITY;
8204 	if (arp_flags & ATF_PERM)
8205 		flags |= NCE_F_NONUD; /* not subject to aging */
8206 	if (arp_flags & ATF_PUBL)
8207 		flags |= NCE_F_PUBLISH;
8208 
8209 	/*
8210 	 * IPMP ARP special handling:
8211 	 *
8212 	 * 1. Since ARP mappings must appear consistent across the group,
8213 	 *    prohibit changing ARP mappings on the underlying interfaces.
8214 	 *
8215 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8216 	 *    IP itself, prohibit changing them.
8217 	 *
8218 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8219 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8220 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8221 	 */
8222 	if (IS_UNDER_IPMP(ill)) {
8223 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8224 			return (EPERM);
8225 	}
8226 	if (IS_IPMP(ill)) {
8227 		ipmp_illgrp_t *illg = ill->ill_grp;
8228 
8229 		switch (ipip->ipi_cmd) {
8230 		case SIOCSARP:
8231 		case SIOCSXARP:
8232 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8233 			if (proxy_ill != NULL) {
8234 				proxyarp = B_TRUE;
8235 				if (!ipmp_ill_is_active(proxy_ill))
8236 					proxy_ill = ipmp_illgrp_next_ill(illg);
8237 				if (proxy_ill != NULL)
8238 					lladdr = proxy_ill->ill_phys_addr;
8239 			}
8240 			/* FALLTHRU */
8241 		}
8242 	}
8243 
8244 	ipaddr = sin->sin_addr.s_addr;
8245 	/*
8246 	 * don't match across illgrp per case (1) and (2).
8247 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8248 	 */
8249 	nce = nce_lookup_v4(ill, &ipaddr);
8250 	if (nce != NULL)
8251 		ncec = nce->nce_common;
8252 
8253 	switch (iocp->ioc_cmd) {
8254 	case SIOCDARP:
8255 	case SIOCDXARP: {
8256 		/*
8257 		 * Delete the NCE if any.
8258 		 */
8259 		if (ncec == NULL) {
8260 			iocp->ioc_error = ENXIO;
8261 			break;
8262 		}
8263 		/* Don't allow changes to arp mappings of local addresses. */
8264 		if (NCE_MYADDR(ncec)) {
8265 			nce_refrele(nce);
8266 			return (ENOTSUP);
8267 		}
8268 		iocp->ioc_error = 0;
8269 
8270 		/*
8271 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8272 		 * This will delete all the nce entries on the under_ills.
8273 		 */
8274 		ncec_delete(ncec);
8275 		/*
8276 		 * Once the NCE has been deleted, then the ire_dep* consistency
8277 		 * mechanism will find any IRE which depended on the now
8278 		 * condemned NCE (as part of sending packets).
8279 		 * That mechanism handles redirects by deleting redirects
8280 		 * that refer to UNREACHABLE nces.
8281 		 */
8282 		break;
8283 	}
8284 	case SIOCGARP:
8285 	case SIOCGXARP:
8286 		if (ncec != NULL) {
8287 			lladdr = ncec->ncec_lladdr;
8288 			flags = ncec->ncec_flags;
8289 			iocp->ioc_error = 0;
8290 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8291 		} else {
8292 			iocp->ioc_error = ENXIO;
8293 		}
8294 		break;
8295 	case SIOCSARP:
8296 	case SIOCSXARP:
8297 		/* Don't allow changes to arp mappings of local addresses. */
8298 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8299 			nce_refrele(nce);
8300 			return (ENOTSUP);
8301 		}
8302 
8303 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8304 		flags |= NCE_F_STATIC;
8305 		if (!if_arp_ioctl) {
8306 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8307 			    lladdr, alength, flags);
8308 		} else {
8309 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8310 			if (ipif != NULL) {
8311 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8312 				    lladdr, alength, flags);
8313 				ipif_refrele(ipif);
8314 			}
8315 		}
8316 		if (nce != NULL) {
8317 			nce_refrele(nce);
8318 			nce = NULL;
8319 		}
8320 		/*
8321 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8322 		 * by nce_add_common()
8323 		 */
8324 		err = nce_lookup_then_add_v4(ill, lladdr,
8325 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8326 		    &nce);
8327 		if (err == EEXIST) {
8328 			ncec = nce->nce_common;
8329 			mutex_enter(&ncec->ncec_lock);
8330 			ncec->ncec_state = ND_REACHABLE;
8331 			ncec->ncec_flags = flags;
8332 			nce_update(ncec, ND_UNCHANGED, lladdr);
8333 			mutex_exit(&ncec->ncec_lock);
8334 			err = 0;
8335 		}
8336 		if (nce != NULL) {
8337 			nce_refrele(nce);
8338 			nce = NULL;
8339 		}
8340 		if (IS_IPMP(ill) && err == 0) {
8341 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8342 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8343 			    flags);
8344 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8345 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8346 				break;
8347 			}
8348 		}
8349 		iocp->ioc_error = err;
8350 	}
8351 
8352 	if (nce != NULL) {
8353 		nce_refrele(nce);
8354 	}
8355 
8356 	/*
8357 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8358 	 */
8359 	if (entp != NULL)
8360 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8361 
8362 	return (iocp->ioc_error);
8363 }
8364 
8365 /*
8366  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8367  * the associated sin and refhold and return the associated ipif via `ci'.
8368  */
8369 int
8370 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8371     cmd_info_t *ci)
8372 {
8373 	mblk_t	*mp1;
8374 	sin_t	*sin;
8375 	conn_t	*connp;
8376 	ipif_t	*ipif;
8377 	ire_t	*ire = NULL;
8378 	ill_t	*ill = NULL;
8379 	boolean_t exists;
8380 	ip_stack_t *ipst;
8381 	struct arpreq *ar;
8382 	struct xarpreq *xar;
8383 	struct sockaddr_dl *sdl;
8384 
8385 	/* ioctl comes down on a conn */
8386 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8387 	connp = Q_TO_CONN(q);
8388 	if (connp->conn_family == AF_INET6)
8389 		return (ENXIO);
8390 
8391 	ipst = connp->conn_netstack->netstack_ip;
8392 
8393 	/* Verified in ip_wput_nondata */
8394 	mp1 = mp->b_cont->b_cont;
8395 
8396 	if (ipip->ipi_cmd_type == XARP_CMD) {
8397 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8398 		xar = (struct xarpreq *)mp1->b_rptr;
8399 		sin = (sin_t *)&xar->xarp_pa;
8400 		sdl = &xar->xarp_ha;
8401 
8402 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8403 			return (ENXIO);
8404 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8405 			return (EINVAL);
8406 	} else {
8407 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8408 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8409 		ar = (struct arpreq *)mp1->b_rptr;
8410 		sin = (sin_t *)&ar->arp_pa;
8411 	}
8412 
8413 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8414 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8415 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8416 		if (ipif == NULL)
8417 			return (ENXIO);
8418 		if (ipif->ipif_id != 0) {
8419 			ipif_refrele(ipif);
8420 			return (ENXIO);
8421 		}
8422 	} else {
8423 		/*
8424 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8425 		 * of 0: use the IP address to find the ipif.  If the IP
8426 		 * address is an IPMP test address, ire_ftable_lookup() will
8427 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8428 		 */
8429 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8430 		    ipst);
8431 		if (ipif == NULL) {
8432 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8433 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8434 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8435 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8436 				if (ire != NULL)
8437 					ire_refrele(ire);
8438 				return (ENXIO);
8439 			}
8440 			ASSERT(ire != NULL && ill != NULL);
8441 			ipif = ill->ill_ipif;
8442 			ipif_refhold(ipif);
8443 			ire_refrele(ire);
8444 		}
8445 	}
8446 
8447 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8448 		ipif_refrele(ipif);
8449 		return (ENXIO);
8450 	}
8451 
8452 	ci->ci_sin = sin;
8453 	ci->ci_ipif = ipif;
8454 	return (0);
8455 }
8456 
8457 /*
8458  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8459  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8460  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8461  * up and thus an ill can join that illgrp.
8462  *
8463  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8464  * open()/close() primarily because close() is not allowed to fail or block
8465  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8466  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8467  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8468  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8469  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8470  * state if I_UNLINK didn't occur.
8471  *
8472  * Note that for each plumb/unplumb operation, we may end up here more than
8473  * once because of the way ifconfig works.  However, it's OK to link the same
8474  * illgrp more than once, or unlink an illgrp that's already unlinked.
8475  */
8476 static int
8477 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8478 {
8479 	int err;
8480 	ip_stack_t *ipst = ill->ill_ipst;
8481 
8482 	ASSERT(IS_IPMP(ill));
8483 	ASSERT(IAM_WRITER_ILL(ill));
8484 
8485 	switch (ioccmd) {
8486 	case I_LINK:
8487 		return (ENOTSUP);
8488 
8489 	case I_PLINK:
8490 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8491 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8492 		rw_exit(&ipst->ips_ipmp_lock);
8493 		break;
8494 
8495 	case I_PUNLINK:
8496 		/*
8497 		 * Require all UP ipifs be brought down prior to unlinking the
8498 		 * illgrp so any associated IREs (and other state) is torched.
8499 		 */
8500 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8501 			return (EBUSY);
8502 
8503 		/*
8504 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8505 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8506 		 * join this group.  Specifically: ills trying to join grab
8507 		 * ipmp_lock and bump a "pending join" counter checked by
8508 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8509 		 * joins can occur (since we have ipmp_lock).  Once we drop
8510 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8511 		 * find the illgrp (since we unlinked it) and will return
8512 		 * EAFNOSUPPORT.  This will then take them back through the
8513 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8514 		 * back through I_PLINK above.
8515 		 */
8516 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8517 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8518 		rw_exit(&ipst->ips_ipmp_lock);
8519 		return (err);
8520 	default:
8521 		break;
8522 	}
8523 	return (0);
8524 }
8525 
8526 /*
8527  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8528  * atomically set/clear the muxids. Also complete the ioctl by acking or
8529  * naking it.  Note that the code is structured such that the link type,
8530  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8531  * its clones use the persistent link, while pppd(1M) and perhaps many
8532  * other daemons may use non-persistent link.  When combined with some
8533  * ill_t states, linking and unlinking lower streams may be used as
8534  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8535  */
8536 /* ARGSUSED */
8537 void
8538 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8539 {
8540 	mblk_t		*mp1;
8541 	struct linkblk	*li;
8542 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8543 	int		err = 0;
8544 
8545 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8546 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8547 
8548 	mp1 = mp->b_cont;	/* This is the linkblk info */
8549 	li = (struct linkblk *)mp1->b_rptr;
8550 
8551 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8552 	if (err == EINPROGRESS)
8553 		return;
8554 	if (err == 0)
8555 		miocack(q, mp, 0, 0);
8556 	else
8557 		miocnak(q, mp, 0, err);
8558 
8559 	/* Conn was refheld in ip_sioctl_copyin_setup */
8560 	if (CONN_Q(q)) {
8561 		CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8562 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8563 	}
8564 }
8565 
8566 /*
8567  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8568  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8569  * module stream).
8570  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8571  * an error code on failure.
8572  */
8573 static int
8574 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8575     struct linkblk *li)
8576 {
8577 	int		err = 0;
8578 	ill_t  		*ill;
8579 	queue_t		*ipwq, *dwq;
8580 	const char	*name;
8581 	struct qinit	*qinfo;
8582 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8583 	boolean_t	entered_ipsq = B_FALSE;
8584 	boolean_t	is_ip = B_FALSE;
8585 	arl_t		*arl;
8586 
8587 	/*
8588 	 * Walk the lower stream to verify it's the IP module stream.
8589 	 * The IP module is identified by its name, wput function,
8590 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8591 	 * (li->l_qbot) will not vanish until this ioctl completes.
8592 	 */
8593 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8594 		qinfo = ipwq->q_qinfo;
8595 		name = qinfo->qi_minfo->mi_idname;
8596 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8597 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8598 			is_ip = B_TRUE;
8599 			break;
8600 		}
8601 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8602 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8603 			break;
8604 		}
8605 	}
8606 
8607 	/*
8608 	 * If this isn't an IP module stream, bail.
8609 	 */
8610 	if (ipwq == NULL)
8611 		return (0);
8612 
8613 	if (!is_ip) {
8614 		arl = (arl_t *)ipwq->q_ptr;
8615 		ill = arl_to_ill(arl);
8616 		if (ill == NULL)
8617 			return (0);
8618 	} else {
8619 		ill = ipwq->q_ptr;
8620 	}
8621 	ASSERT(ill != NULL);
8622 
8623 	if (ipsq == NULL) {
8624 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8625 		    NEW_OP, B_FALSE);
8626 		if (ipsq == NULL) {
8627 			if (!is_ip)
8628 				ill_refrele(ill);
8629 			return (EINPROGRESS);
8630 		}
8631 		entered_ipsq = B_TRUE;
8632 	}
8633 	ASSERT(IAM_WRITER_ILL(ill));
8634 	mutex_enter(&ill->ill_lock);
8635 	if (!is_ip) {
8636 		if (islink && ill->ill_muxid == 0) {
8637 			/*
8638 			 * Plumbing has to be done with IP plumbed first, arp
8639 			 * second, but here we have arp being plumbed first.
8640 			 */
8641 			mutex_exit(&ill->ill_lock);
8642 			if (entered_ipsq)
8643 				ipsq_exit(ipsq);
8644 			ill_refrele(ill);
8645 			return (EINVAL);
8646 		}
8647 	}
8648 	mutex_exit(&ill->ill_lock);
8649 	if (!is_ip) {
8650 		arl->arl_muxid = islink ? li->l_index : 0;
8651 		ill_refrele(ill);
8652 		goto done;
8653 	}
8654 
8655 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8656 		goto done;
8657 
8658 	/*
8659 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8660 	 * the read queue of the module immediately below IP in the ill.
8661 	 * These are used during the capability negotiation below.
8662 	 */
8663 	ill->ill_lmod_rq = NULL;
8664 	ill->ill_lmod_cnt = 0;
8665 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8666 		ill->ill_lmod_rq = RD(dwq);
8667 		for (; dwq != NULL; dwq = dwq->q_next)
8668 			ill->ill_lmod_cnt++;
8669 	}
8670 
8671 	ill->ill_muxid = islink ? li->l_index : 0;
8672 
8673 	/*
8674 	 * Mark the ipsq busy until the capability operations initiated below
8675 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8676 	 * returns, but the capability operation may complete asynchronously
8677 	 * much later.
8678 	 */
8679 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8680 	/*
8681 	 * If there's at least one up ipif on this ill, then we're bound to
8682 	 * the underlying driver via DLPI.  In that case, renegotiate
8683 	 * capabilities to account for any possible change in modules
8684 	 * interposed between IP and the driver.
8685 	 */
8686 	if (ill->ill_ipif_up_count > 0) {
8687 		if (islink)
8688 			ill_capability_probe(ill);
8689 		else
8690 			ill_capability_reset(ill, B_FALSE);
8691 	}
8692 	ipsq_current_finish(ipsq);
8693 done:
8694 	if (entered_ipsq)
8695 		ipsq_exit(ipsq);
8696 
8697 	return (err);
8698 }
8699 
8700 /*
8701  * Search the ioctl command in the ioctl tables and return a pointer
8702  * to the ioctl command information. The ioctl command tables are
8703  * static and fully populated at compile time.
8704  */
8705 ip_ioctl_cmd_t *
8706 ip_sioctl_lookup(int ioc_cmd)
8707 {
8708 	int index;
8709 	ip_ioctl_cmd_t *ipip;
8710 	ip_ioctl_cmd_t *ipip_end;
8711 
8712 	if (ioc_cmd == IPI_DONTCARE)
8713 		return (NULL);
8714 
8715 	/*
8716 	 * Do a 2 step search. First search the indexed table
8717 	 * based on the least significant byte of the ioctl cmd.
8718 	 * If we don't find a match, then search the misc table
8719 	 * serially.
8720 	 */
8721 	index = ioc_cmd & 0xFF;
8722 	if (index < ip_ndx_ioctl_count) {
8723 		ipip = &ip_ndx_ioctl_table[index];
8724 		if (ipip->ipi_cmd == ioc_cmd) {
8725 			/* Found a match in the ndx table */
8726 			return (ipip);
8727 		}
8728 	}
8729 
8730 	/* Search the misc table */
8731 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8732 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8733 		if (ipip->ipi_cmd == ioc_cmd)
8734 			/* Found a match in the misc table */
8735 			return (ipip);
8736 	}
8737 
8738 	return (NULL);
8739 }
8740 
8741 /*
8742  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8743  */
8744 static boolean_t
8745 getset_ioctl_checks(mblk_t *mp)
8746 {
8747 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8748 	mblk_t		*mp1 = mp->b_cont;
8749 	mod_ioc_prop_t	*pioc;
8750 	uint_t		flags;
8751 	uint_t		pioc_size;
8752 
8753 	/* do sanity checks on various arguments */
8754 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8755 	    iocp->ioc_count == TRANSPARENT) {
8756 		return (B_FALSE);
8757 	}
8758 	if (msgdsize(mp1) < iocp->ioc_count) {
8759 		if (!pullupmsg(mp1, iocp->ioc_count))
8760 			return (B_FALSE);
8761 	}
8762 
8763 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8764 
8765 	/* sanity checks on mpr_valsize */
8766 	pioc_size = sizeof (mod_ioc_prop_t);
8767 	if (pioc->mpr_valsize != 0)
8768 		pioc_size += pioc->mpr_valsize - 1;
8769 
8770 	if (iocp->ioc_count != pioc_size)
8771 		return (B_FALSE);
8772 
8773 	flags = pioc->mpr_flags;
8774 	if (iocp->ioc_cmd == SIOCSETPROP) {
8775 		/*
8776 		 * One can either reset the value to it's default value or
8777 		 * change the current value or append/remove the value from
8778 		 * a multi-valued properties.
8779 		 */
8780 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8781 		    flags != MOD_PROP_ACTIVE &&
8782 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8783 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8784 			return (B_FALSE);
8785 	} else {
8786 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8787 
8788 		/*
8789 		 * One can retrieve only one kind of property information
8790 		 * at a time.
8791 		 */
8792 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8793 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8794 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8795 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8796 			return (B_FALSE);
8797 	}
8798 
8799 	return (B_TRUE);
8800 }
8801 
8802 /*
8803  * process the SIOC{SET|GET}PROP ioctl's
8804  */
8805 /* ARGSUSED */
8806 static void
8807 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8808 {
8809 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8810 	mblk_t		*mp1 = mp->b_cont;
8811 	mod_ioc_prop_t	*pioc;
8812 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8813 	ip_stack_t	*ipst;
8814 	icmp_stack_t	*is;
8815 	tcp_stack_t	*tcps;
8816 	sctp_stack_t	*sctps;
8817 	udp_stack_t	*us;
8818 	netstack_t	*stack;
8819 	void		*cbarg;
8820 	cred_t		*cr;
8821 	boolean_t 	set;
8822 	int		err;
8823 
8824 	ASSERT(q->q_next == NULL);
8825 	ASSERT(CONN_Q(q));
8826 
8827 	if (!getset_ioctl_checks(mp)) {
8828 		miocnak(q, mp, 0, EINVAL);
8829 		return;
8830 	}
8831 	ipst = CONNQ_TO_IPST(q);
8832 	stack = ipst->ips_netstack;
8833 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8834 
8835 	switch (pioc->mpr_proto) {
8836 	case MOD_PROTO_IP:
8837 	case MOD_PROTO_IPV4:
8838 	case MOD_PROTO_IPV6:
8839 		ptbl = ipst->ips_propinfo_tbl;
8840 		cbarg = ipst;
8841 		break;
8842 	case MOD_PROTO_RAWIP:
8843 		is = stack->netstack_icmp;
8844 		ptbl = is->is_propinfo_tbl;
8845 		cbarg = is;
8846 		break;
8847 	case MOD_PROTO_TCP:
8848 		tcps = stack->netstack_tcp;
8849 		ptbl = tcps->tcps_propinfo_tbl;
8850 		cbarg = tcps;
8851 		break;
8852 	case MOD_PROTO_UDP:
8853 		us = stack->netstack_udp;
8854 		ptbl = us->us_propinfo_tbl;
8855 		cbarg = us;
8856 		break;
8857 	case MOD_PROTO_SCTP:
8858 		sctps = stack->netstack_sctp;
8859 		ptbl = sctps->sctps_propinfo_tbl;
8860 		cbarg = sctps;
8861 		break;
8862 	default:
8863 		miocnak(q, mp, 0, EINVAL);
8864 		return;
8865 	}
8866 
8867 	/* search for given property in respective protocol propinfo table */
8868 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8869 		if (strcmp(pinfo->mpi_name, pioc->mpr_name) == 0 &&
8870 		    pinfo->mpi_proto == pioc->mpr_proto)
8871 			break;
8872 	}
8873 	if (pinfo->mpi_name == NULL) {
8874 		miocnak(q, mp, 0, ENOENT);
8875 		return;
8876 	}
8877 
8878 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8879 	if (set && pinfo->mpi_setf != NULL) {
8880 		cr = msg_getcred(mp, NULL);
8881 		if (cr == NULL)
8882 			cr = iocp->ioc_cr;
8883 		err = pinfo->mpi_setf(cbarg, cr, pinfo, pioc->mpr_ifname,
8884 		    pioc->mpr_val, pioc->mpr_flags);
8885 	} else if (!set && pinfo->mpi_getf != NULL) {
8886 		err = pinfo->mpi_getf(cbarg, pinfo, pioc->mpr_ifname,
8887 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8888 	} else {
8889 		err = EPERM;
8890 	}
8891 
8892 	if (err != 0) {
8893 		miocnak(q, mp, 0, err);
8894 	} else {
8895 		if (set)
8896 			miocack(q, mp, 0, 0);
8897 		else    /* For get, we need to return back the data */
8898 			miocack(q, mp, iocp->ioc_count, 0);
8899 	}
8900 }
8901 
8902 /*
8903  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8904  * as several routing daemons have unfortunately used this 'unpublished'
8905  * but well-known ioctls.
8906  */
8907 /* ARGSUSED */
8908 static void
8909 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8910 {
8911 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8912 	mblk_t		*mp1 = mp->b_cont;
8913 	char		*pname, *pval, *buf;
8914 	uint_t		bufsize, proto;
8915 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8916 	ip_stack_t	*ipst;
8917 	int		err = 0;
8918 
8919 	ASSERT(CONN_Q(q));
8920 	ipst = CONNQ_TO_IPST(q);
8921 
8922 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8923 		miocnak(q, mp, 0, EINVAL);
8924 		return;
8925 	}
8926 
8927 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8928 	pval = buf = pname = (char *)mp1->b_rptr;
8929 	bufsize = MBLKL(mp1);
8930 
8931 	if (strcmp(pname, "ip_forwarding") == 0) {
8932 		pname = "forwarding";
8933 		proto = MOD_PROTO_IPV4;
8934 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8935 		pname = "forwarding";
8936 		proto = MOD_PROTO_IPV6;
8937 	} else {
8938 		miocnak(q, mp, 0, EINVAL);
8939 		return;
8940 	}
8941 
8942 	ptbl = ipst->ips_propinfo_tbl;
8943 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8944 		if (strcmp(pinfo->mpi_name, pname) == 0 &&
8945 		    pinfo->mpi_proto == proto)
8946 			break;
8947 	}
8948 
8949 	ASSERT(pinfo->mpi_name != NULL);
8950 
8951 	switch (iocp->ioc_cmd) {
8952 	case ND_GET:
8953 		if ((err = pinfo->mpi_getf(ipst, pinfo, NULL, buf, bufsize,
8954 		    0)) == 0) {
8955 			miocack(q, mp, iocp->ioc_count, 0);
8956 			return;
8957 		}
8958 		break;
8959 	case ND_SET:
8960 		/*
8961 		 * buffer will have property name and value in the following
8962 		 * format,
8963 		 * <property name>'\0'<property value>'\0', extract them;
8964 		 */
8965 		while (*pval++)
8966 			noop;
8967 
8968 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8969 			err = EINVAL;
8970 		} else if ((err = pinfo->mpi_setf(ipst, NULL, pinfo, NULL,
8971 		    pval, 0)) == 0) {
8972 			miocack(q, mp, 0, 0);
8973 			return;
8974 		}
8975 		break;
8976 	default:
8977 		err = EINVAL;
8978 		break;
8979 	}
8980 	miocnak(q, mp, 0, err);
8981 }
8982 
8983 /*
8984  * Wrapper function for resuming deferred ioctl processing
8985  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8986  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8987  */
8988 /* ARGSUSED */
8989 void
8990 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8991     void *dummy_arg)
8992 {
8993 	ip_sioctl_copyin_setup(q, mp);
8994 }
8995 
8996 /*
8997  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8998  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8999  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9000  * We establish here the size of the block to be copied in.  mi_copyin
9001  * arranges for this to happen, an processing continues in ip_wput_nondata with
9002  * an M_IOCDATA message.
9003  */
9004 void
9005 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9006 {
9007 	int	copyin_size;
9008 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9009 	ip_ioctl_cmd_t *ipip;
9010 	cred_t *cr;
9011 	ip_stack_t	*ipst;
9012 
9013 	if (CONN_Q(q))
9014 		ipst = CONNQ_TO_IPST(q);
9015 	else
9016 		ipst = ILLQ_TO_IPST(q);
9017 
9018 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9019 	if (ipip == NULL) {
9020 		/*
9021 		 * The ioctl is not one we understand or own.
9022 		 * Pass it along to be processed down stream,
9023 		 * if this is a module instance of IP, else nak
9024 		 * the ioctl.
9025 		 */
9026 		if (q->q_next == NULL) {
9027 			goto nak;
9028 		} else {
9029 			putnext(q, mp);
9030 			return;
9031 		}
9032 	}
9033 
9034 	/*
9035 	 * If this is deferred, then we will do all the checks when we
9036 	 * come back.
9037 	 */
9038 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9039 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9040 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9041 		return;
9042 	}
9043 
9044 	/*
9045 	 * Only allow a very small subset of IP ioctls on this stream if
9046 	 * IP is a module and not a driver. Allowing ioctls to be processed
9047 	 * in this case may cause assert failures or data corruption.
9048 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9049 	 * ioctls allowed on an IP module stream, after which this stream
9050 	 * normally becomes a multiplexor (at which time the stream head
9051 	 * will fail all ioctls).
9052 	 */
9053 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9054 		goto nak;
9055 	}
9056 
9057 	/* Make sure we have ioctl data to process. */
9058 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9059 		goto nak;
9060 
9061 	/*
9062 	 * Prefer dblk credential over ioctl credential; some synthesized
9063 	 * ioctls have kcred set because there's no way to crhold()
9064 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9065 	 * the framework; the caller of ioctl needs to hold the reference
9066 	 * for the duration of the call).
9067 	 */
9068 	cr = msg_getcred(mp, NULL);
9069 	if (cr == NULL)
9070 		cr = iocp->ioc_cr;
9071 
9072 	/* Make sure normal users don't send down privileged ioctls */
9073 	if ((ipip->ipi_flags & IPI_PRIV) &&
9074 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9075 		/* We checked the privilege earlier but log it here */
9076 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9077 		return;
9078 	}
9079 
9080 	/*
9081 	 * The ioctl command tables can only encode fixed length
9082 	 * ioctl data. If the length is variable, the table will
9083 	 * encode the length as zero. Such special cases are handled
9084 	 * below in the switch.
9085 	 */
9086 	if (ipip->ipi_copyin_size != 0) {
9087 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9088 		return;
9089 	}
9090 
9091 	switch (iocp->ioc_cmd) {
9092 	case O_SIOCGIFCONF:
9093 	case SIOCGIFCONF:
9094 		/*
9095 		 * This IOCTL is hilarious.  See comments in
9096 		 * ip_sioctl_get_ifconf for the story.
9097 		 */
9098 		if (iocp->ioc_count == TRANSPARENT)
9099 			copyin_size = SIZEOF_STRUCT(ifconf,
9100 			    iocp->ioc_flag);
9101 		else
9102 			copyin_size = iocp->ioc_count;
9103 		mi_copyin(q, mp, NULL, copyin_size);
9104 		return;
9105 
9106 	case O_SIOCGLIFCONF:
9107 	case SIOCGLIFCONF:
9108 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9109 		mi_copyin(q, mp, NULL, copyin_size);
9110 		return;
9111 
9112 	case SIOCGLIFSRCOF:
9113 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9114 		mi_copyin(q, mp, NULL, copyin_size);
9115 		return;
9116 
9117 	case SIOCGIP6ADDRPOLICY:
9118 		ip_sioctl_ip6addrpolicy(q, mp);
9119 		ip6_asp_table_refrele(ipst);
9120 		return;
9121 
9122 	case SIOCSIP6ADDRPOLICY:
9123 		ip_sioctl_ip6addrpolicy(q, mp);
9124 		return;
9125 
9126 	case SIOCGDSTINFO:
9127 		ip_sioctl_dstinfo(q, mp);
9128 		ip6_asp_table_refrele(ipst);
9129 		return;
9130 
9131 	case ND_SET:
9132 	case ND_GET:
9133 		ip_process_legacy_nddprop(q, mp);
9134 		return;
9135 
9136 	case SIOCSETPROP:
9137 	case SIOCGETPROP:
9138 		ip_sioctl_getsetprop(q, mp);
9139 		return;
9140 
9141 	case I_PLINK:
9142 	case I_PUNLINK:
9143 	case I_LINK:
9144 	case I_UNLINK:
9145 		/*
9146 		 * We treat non-persistent link similarly as the persistent
9147 		 * link case, in terms of plumbing/unplumbing, as well as
9148 		 * dynamic re-plumbing events indicator.  See comments
9149 		 * in ip_sioctl_plink() for more.
9150 		 *
9151 		 * Request can be enqueued in the 'ipsq' while waiting
9152 		 * to become exclusive. So bump up the conn ref.
9153 		 */
9154 		if (CONN_Q(q)) {
9155 			CONN_INC_REF(Q_TO_CONN(q));
9156 			CONN_INC_IOCTLREF(Q_TO_CONN(q))
9157 		}
9158 		ip_sioctl_plink(NULL, q, mp, NULL);
9159 		return;
9160 
9161 	case IP_IOCTL:
9162 		ip_wput_ioctl(q, mp);
9163 		return;
9164 
9165 	case SIOCILB:
9166 		/* The ioctl length varies depending on the ILB command. */
9167 		copyin_size = iocp->ioc_count;
9168 		if (copyin_size < sizeof (ilb_cmd_t))
9169 			goto nak;
9170 		mi_copyin(q, mp, NULL, copyin_size);
9171 		return;
9172 
9173 	default:
9174 		cmn_err(CE_PANIC, "should not happen ");
9175 	}
9176 nak:
9177 	if (mp->b_cont != NULL) {
9178 		freemsg(mp->b_cont);
9179 		mp->b_cont = NULL;
9180 	}
9181 	iocp->ioc_error = EINVAL;
9182 	mp->b_datap->db_type = M_IOCNAK;
9183 	iocp->ioc_count = 0;
9184 	qreply(q, mp);
9185 }
9186 
9187 static void
9188 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9189 {
9190 	struct arpreq *ar;
9191 	struct xarpreq *xar;
9192 	mblk_t	*tmp;
9193 	struct iocblk *iocp;
9194 	int x_arp_ioctl = B_FALSE;
9195 	int *flagsp;
9196 	char *storage = NULL;
9197 
9198 	ASSERT(ill != NULL);
9199 
9200 	iocp = (struct iocblk *)mp->b_rptr;
9201 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9202 
9203 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9204 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9205 	    (iocp->ioc_cmd == SIOCSXARP)) {
9206 		x_arp_ioctl = B_TRUE;
9207 		xar = (struct xarpreq *)tmp->b_rptr;
9208 		flagsp = &xar->xarp_flags;
9209 		storage = xar->xarp_ha.sdl_data;
9210 	} else {
9211 		ar = (struct arpreq *)tmp->b_rptr;
9212 		flagsp = &ar->arp_flags;
9213 		storage = ar->arp_ha.sa_data;
9214 	}
9215 
9216 	/*
9217 	 * We're done if this is not an SIOCG{X}ARP
9218 	 */
9219 	if (x_arp_ioctl) {
9220 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9221 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9222 		    sizeof (xar->xarp_ha.sdl_data)) {
9223 			iocp->ioc_error = EINVAL;
9224 			return;
9225 		}
9226 	}
9227 	*flagsp = ATF_INUSE;
9228 	/*
9229 	 * If /sbin/arp told us we are the authority using the "permanent"
9230 	 * flag, or if this is one of my addresses print "permanent"
9231 	 * in the /sbin/arp output.
9232 	 */
9233 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9234 		*flagsp |= ATF_AUTHORITY;
9235 	if (flags & NCE_F_NONUD)
9236 		*flagsp |= ATF_PERM; /* not subject to aging */
9237 	if (flags & NCE_F_PUBLISH)
9238 		*flagsp |= ATF_PUBL;
9239 	if (hwaddr != NULL) {
9240 		*flagsp |= ATF_COM;
9241 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9242 	}
9243 }
9244 
9245 /*
9246  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9247  * interface) create the next available logical interface for this
9248  * physical interface.
9249  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9250  * ipif with the specified name.
9251  *
9252  * If the address family is not AF_UNSPEC then set the address as well.
9253  *
9254  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9255  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9256  *
9257  * Executed as a writer on the ill.
9258  * So no lock is needed to traverse the ipif chain, or examine the
9259  * phyint flags.
9260  */
9261 /* ARGSUSED */
9262 int
9263 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9264     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9265 {
9266 	mblk_t	*mp1;
9267 	struct lifreq *lifr;
9268 	boolean_t	isv6;
9269 	boolean_t	exists;
9270 	char 	*name;
9271 	char	*endp;
9272 	char	*cp;
9273 	int	namelen;
9274 	ipif_t	*ipif;
9275 	long	id;
9276 	ipsq_t	*ipsq;
9277 	ill_t	*ill;
9278 	sin_t	*sin;
9279 	int	err = 0;
9280 	boolean_t found_sep = B_FALSE;
9281 	conn_t	*connp;
9282 	zoneid_t zoneid;
9283 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9284 
9285 	ASSERT(q->q_next == NULL);
9286 	ip1dbg(("ip_sioctl_addif\n"));
9287 	/* Existence of mp1 has been checked in ip_wput_nondata */
9288 	mp1 = mp->b_cont->b_cont;
9289 	/*
9290 	 * Null terminate the string to protect against buffer
9291 	 * overrun. String was generated by user code and may not
9292 	 * be trusted.
9293 	 */
9294 	lifr = (struct lifreq *)mp1->b_rptr;
9295 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9296 	name = lifr->lifr_name;
9297 	ASSERT(CONN_Q(q));
9298 	connp = Q_TO_CONN(q);
9299 	isv6 = (connp->conn_family == AF_INET6);
9300 	zoneid = connp->conn_zoneid;
9301 	namelen = mi_strlen(name);
9302 	if (namelen == 0)
9303 		return (EINVAL);
9304 
9305 	exists = B_FALSE;
9306 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9307 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9308 		/*
9309 		 * Allow creating lo0 using SIOCLIFADDIF.
9310 		 * can't be any other writer thread. So can pass null below
9311 		 * for the last 4 args to ipif_lookup_name.
9312 		 */
9313 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9314 		    &exists, isv6, zoneid, ipst);
9315 		/* Prevent any further action */
9316 		if (ipif == NULL) {
9317 			return (ENOBUFS);
9318 		} else if (!exists) {
9319 			/* We created the ipif now and as writer */
9320 			ipif_refrele(ipif);
9321 			return (0);
9322 		} else {
9323 			ill = ipif->ipif_ill;
9324 			ill_refhold(ill);
9325 			ipif_refrele(ipif);
9326 		}
9327 	} else {
9328 		/* Look for a colon in the name. */
9329 		endp = &name[namelen];
9330 		for (cp = endp; --cp > name; ) {
9331 			if (*cp == IPIF_SEPARATOR_CHAR) {
9332 				found_sep = B_TRUE;
9333 				/*
9334 				 * Reject any non-decimal aliases for plumbing
9335 				 * of logical interfaces. Aliases with leading
9336 				 * zeroes are also rejected as they introduce
9337 				 * ambiguity in the naming of the interfaces.
9338 				 * Comparing with "0" takes care of all such
9339 				 * cases.
9340 				 */
9341 				if ((strncmp("0", cp+1, 1)) == 0)
9342 					return (EINVAL);
9343 
9344 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9345 				    id <= 0 || *endp != '\0') {
9346 					return (EINVAL);
9347 				}
9348 				*cp = '\0';
9349 				break;
9350 			}
9351 		}
9352 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9353 		if (found_sep)
9354 			*cp = IPIF_SEPARATOR_CHAR;
9355 		if (ill == NULL)
9356 			return (ENXIO);
9357 	}
9358 
9359 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9360 	    B_TRUE);
9361 
9362 	/*
9363 	 * Release the refhold due to the lookup, now that we are excl
9364 	 * or we are just returning
9365 	 */
9366 	ill_refrele(ill);
9367 
9368 	if (ipsq == NULL)
9369 		return (EINPROGRESS);
9370 
9371 	/* We are now exclusive on the IPSQ */
9372 	ASSERT(IAM_WRITER_ILL(ill));
9373 
9374 	if (found_sep) {
9375 		/* Now see if there is an IPIF with this unit number. */
9376 		for (ipif = ill->ill_ipif; ipif != NULL;
9377 		    ipif = ipif->ipif_next) {
9378 			if (ipif->ipif_id == id) {
9379 				err = EEXIST;
9380 				goto done;
9381 			}
9382 		}
9383 	}
9384 
9385 	/*
9386 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9387 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9388 	 * instead.
9389 	 */
9390 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9391 	    B_TRUE, B_TRUE, &err)) == NULL) {
9392 		goto done;
9393 	}
9394 
9395 	/* Return created name with ioctl */
9396 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9397 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9398 	ip1dbg(("created %s\n", lifr->lifr_name));
9399 
9400 	/* Set address */
9401 	sin = (sin_t *)&lifr->lifr_addr;
9402 	if (sin->sin_family != AF_UNSPEC) {
9403 		err = ip_sioctl_addr(ipif, sin, q, mp,
9404 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9405 	}
9406 
9407 done:
9408 	ipsq_exit(ipsq);
9409 	return (err);
9410 }
9411 
9412 /*
9413  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9414  * interface) delete it based on the IP address (on this physical interface).
9415  * Otherwise delete it based on the ipif_id.
9416  * Also, special handling to allow a removeif of lo0.
9417  */
9418 /* ARGSUSED */
9419 int
9420 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9421     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9422 {
9423 	conn_t		*connp;
9424 	ill_t		*ill = ipif->ipif_ill;
9425 	boolean_t	 success;
9426 	ip_stack_t	*ipst;
9427 
9428 	ipst = CONNQ_TO_IPST(q);
9429 
9430 	ASSERT(q->q_next == NULL);
9431 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9432 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9433 	ASSERT(IAM_WRITER_IPIF(ipif));
9434 
9435 	connp = Q_TO_CONN(q);
9436 	/*
9437 	 * Special case for unplumbing lo0 (the loopback physical interface).
9438 	 * If unplumbing lo0, the incoming address structure has been
9439 	 * initialized to all zeros. When unplumbing lo0, all its logical
9440 	 * interfaces must be removed too.
9441 	 *
9442 	 * Note that this interface may be called to remove a specific
9443 	 * loopback logical interface (eg, lo0:1). But in that case
9444 	 * ipif->ipif_id != 0 so that the code path for that case is the
9445 	 * same as any other interface (meaning it skips the code directly
9446 	 * below).
9447 	 */
9448 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9449 		if (sin->sin_family == AF_UNSPEC &&
9450 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9451 			/*
9452 			 * Mark it condemned. No new ref. will be made to ill.
9453 			 */
9454 			mutex_enter(&ill->ill_lock);
9455 			ill->ill_state_flags |= ILL_CONDEMNED;
9456 			for (ipif = ill->ill_ipif; ipif != NULL;
9457 			    ipif = ipif->ipif_next) {
9458 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9459 			}
9460 			mutex_exit(&ill->ill_lock);
9461 
9462 			ipif = ill->ill_ipif;
9463 			/* unplumb the loopback interface */
9464 			ill_delete(ill);
9465 			mutex_enter(&connp->conn_lock);
9466 			mutex_enter(&ill->ill_lock);
9467 
9468 			/* Are any references to this ill active */
9469 			if (ill_is_freeable(ill)) {
9470 				mutex_exit(&ill->ill_lock);
9471 				mutex_exit(&connp->conn_lock);
9472 				ill_delete_tail(ill);
9473 				mi_free(ill);
9474 				return (0);
9475 			}
9476 			success = ipsq_pending_mp_add(connp, ipif,
9477 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9478 			mutex_exit(&connp->conn_lock);
9479 			mutex_exit(&ill->ill_lock);
9480 			if (success)
9481 				return (EINPROGRESS);
9482 			else
9483 				return (EINTR);
9484 		}
9485 	}
9486 
9487 	if (ipif->ipif_id == 0) {
9488 		ipsq_t *ipsq;
9489 
9490 		/* Find based on address */
9491 		if (ipif->ipif_isv6) {
9492 			sin6_t *sin6;
9493 
9494 			if (sin->sin_family != AF_INET6)
9495 				return (EAFNOSUPPORT);
9496 
9497 			sin6 = (sin6_t *)sin;
9498 			/* We are a writer, so we should be able to lookup */
9499 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9500 			    ipst);
9501 		} else {
9502 			if (sin->sin_family != AF_INET)
9503 				return (EAFNOSUPPORT);
9504 
9505 			/* We are a writer, so we should be able to lookup */
9506 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9507 			    ipst);
9508 		}
9509 		if (ipif == NULL) {
9510 			return (EADDRNOTAVAIL);
9511 		}
9512 
9513 		/*
9514 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9515 		 * lifr_name of the physical interface but with an ip address
9516 		 * lifr_addr of a logical interface plumbed over it.
9517 		 * So update ipx_current_ipif now that ipif points to the
9518 		 * correct one.
9519 		 */
9520 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9521 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9522 
9523 		/* This is a writer */
9524 		ipif_refrele(ipif);
9525 	}
9526 
9527 	/*
9528 	 * Can not delete instance zero since it is tied to the ill.
9529 	 */
9530 	if (ipif->ipif_id == 0)
9531 		return (EBUSY);
9532 
9533 	mutex_enter(&ill->ill_lock);
9534 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9535 	mutex_exit(&ill->ill_lock);
9536 
9537 	ipif_free(ipif);
9538 
9539 	mutex_enter(&connp->conn_lock);
9540 	mutex_enter(&ill->ill_lock);
9541 
9542 	/* Are any references to this ipif active */
9543 	if (ipif_is_freeable(ipif)) {
9544 		mutex_exit(&ill->ill_lock);
9545 		mutex_exit(&connp->conn_lock);
9546 		ipif_non_duplicate(ipif);
9547 		(void) ipif_down_tail(ipif);
9548 		ipif_free_tail(ipif); /* frees ipif */
9549 		return (0);
9550 	}
9551 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9552 	    IPIF_FREE);
9553 	mutex_exit(&ill->ill_lock);
9554 	mutex_exit(&connp->conn_lock);
9555 	if (success)
9556 		return (EINPROGRESS);
9557 	else
9558 		return (EINTR);
9559 }
9560 
9561 /*
9562  * Restart the removeif ioctl. The refcnt has gone down to 0.
9563  * The ipif is already condemned. So can't find it thru lookups.
9564  */
9565 /* ARGSUSED */
9566 int
9567 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9568     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9569 {
9570 	ill_t *ill = ipif->ipif_ill;
9571 
9572 	ASSERT(IAM_WRITER_IPIF(ipif));
9573 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9574 
9575 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9576 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9577 
9578 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9579 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9580 		ill_delete_tail(ill);
9581 		mi_free(ill);
9582 		return (0);
9583 	}
9584 
9585 	ipif_non_duplicate(ipif);
9586 	(void) ipif_down_tail(ipif);
9587 	ipif_free_tail(ipif);
9588 
9589 	return (0);
9590 }
9591 
9592 /*
9593  * Set the local interface address using the given prefix and ill_token.
9594  */
9595 /* ARGSUSED */
9596 int
9597 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9598     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9599 {
9600 	int err;
9601 	in6_addr_t v6addr;
9602 	sin6_t *sin6;
9603 	ill_t *ill;
9604 	int i;
9605 
9606 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9607 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9608 
9609 	ASSERT(IAM_WRITER_IPIF(ipif));
9610 
9611 	if (!ipif->ipif_isv6)
9612 		return (EINVAL);
9613 
9614 	if (sin->sin_family != AF_INET6)
9615 		return (EAFNOSUPPORT);
9616 
9617 	sin6 = (sin6_t *)sin;
9618 	v6addr = sin6->sin6_addr;
9619 	ill = ipif->ipif_ill;
9620 
9621 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9622 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9623 		return (EADDRNOTAVAIL);
9624 
9625 	for (i = 0; i < 4; i++)
9626 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9627 
9628 	err = ip_sioctl_addr(ipif, sin, q, mp,
9629 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9630 	return (err);
9631 }
9632 
9633 /*
9634  * Restart entry point to restart the address set operation after the
9635  * refcounts have dropped to zero.
9636  */
9637 /* ARGSUSED */
9638 int
9639 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9640     ip_ioctl_cmd_t *ipip, void *ifreq)
9641 {
9642 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9643 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9644 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9645 }
9646 
9647 /*
9648  * Set the local interface address.
9649  * Allow an address of all zero when the interface is down.
9650  */
9651 /* ARGSUSED */
9652 int
9653 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9654     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9655 {
9656 	int err = 0;
9657 	in6_addr_t v6addr;
9658 	boolean_t need_up = B_FALSE;
9659 
9660 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9661 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9662 
9663 	ASSERT(IAM_WRITER_IPIF(ipif));
9664 
9665 	if (ipif->ipif_isv6) {
9666 		sin6_t *sin6;
9667 		ill_t *ill;
9668 		phyint_t *phyi;
9669 
9670 		if (sin->sin_family != AF_INET6)
9671 			return (EAFNOSUPPORT);
9672 
9673 		sin6 = (sin6_t *)sin;
9674 		v6addr = sin6->sin6_addr;
9675 		ill = ipif->ipif_ill;
9676 		phyi = ill->ill_phyint;
9677 
9678 		/*
9679 		 * Enforce that true multicast interfaces have a link-local
9680 		 * address for logical unit 0.
9681 		 *
9682 		 * However for those ipif's for which link-local address was
9683 		 * not created by default, also allow setting :: as the address.
9684 		 * This scenario would arise, when we delete an address on ipif
9685 		 * with logical unit 0, we would want to set :: as the address.
9686 		 */
9687 		if (ipif->ipif_id == 0 &&
9688 		    (ill->ill_flags & ILLF_MULTICAST) &&
9689 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9690 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9691 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9692 
9693 			/*
9694 			 * if default link-local was not created by kernel for
9695 			 * this ill, allow setting :: as the address on ipif:0.
9696 			 */
9697 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9698 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9699 					return (EADDRNOTAVAIL);
9700 			} else {
9701 				return (EADDRNOTAVAIL);
9702 			}
9703 		}
9704 
9705 		/*
9706 		 * up interfaces shouldn't have the unspecified address
9707 		 * unless they also have the IPIF_NOLOCAL flags set and
9708 		 * have a subnet assigned.
9709 		 */
9710 		if ((ipif->ipif_flags & IPIF_UP) &&
9711 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9712 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9713 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9714 			return (EADDRNOTAVAIL);
9715 		}
9716 
9717 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9718 			return (EADDRNOTAVAIL);
9719 	} else {
9720 		ipaddr_t addr;
9721 
9722 		if (sin->sin_family != AF_INET)
9723 			return (EAFNOSUPPORT);
9724 
9725 		addr = sin->sin_addr.s_addr;
9726 
9727 		/* Allow INADDR_ANY as the local address. */
9728 		if (addr != INADDR_ANY &&
9729 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9730 			return (EADDRNOTAVAIL);
9731 
9732 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9733 	}
9734 
9735 	/*
9736 	 * Even if there is no change we redo things just to rerun
9737 	 * ipif_set_default.
9738 	 */
9739 	if (ipif->ipif_flags & IPIF_UP) {
9740 		/*
9741 		 * Setting a new local address, make sure
9742 		 * we have net and subnet bcast ire's for
9743 		 * the old address if we need them.
9744 		 */
9745 		/*
9746 		 * If the interface is already marked up,
9747 		 * we call ipif_down which will take care
9748 		 * of ditching any IREs that have been set
9749 		 * up based on the old interface address.
9750 		 */
9751 		err = ipif_logical_down(ipif, q, mp);
9752 		if (err == EINPROGRESS)
9753 			return (err);
9754 		(void) ipif_down_tail(ipif);
9755 		need_up = 1;
9756 	}
9757 
9758 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9759 	return (err);
9760 }
9761 
9762 int
9763 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9764     boolean_t need_up)
9765 {
9766 	in6_addr_t v6addr;
9767 	in6_addr_t ov6addr;
9768 	ipaddr_t addr;
9769 	sin6_t	*sin6;
9770 	int	sinlen;
9771 	int	err = 0;
9772 	ill_t	*ill = ipif->ipif_ill;
9773 	boolean_t need_dl_down;
9774 	boolean_t need_arp_down;
9775 	struct iocblk *iocp;
9776 
9777 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9778 
9779 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9780 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9781 	ASSERT(IAM_WRITER_IPIF(ipif));
9782 
9783 	/* Must cancel any pending timer before taking the ill_lock */
9784 	if (ipif->ipif_recovery_id != 0)
9785 		(void) untimeout(ipif->ipif_recovery_id);
9786 	ipif->ipif_recovery_id = 0;
9787 
9788 	if (ipif->ipif_isv6) {
9789 		sin6 = (sin6_t *)sin;
9790 		v6addr = sin6->sin6_addr;
9791 		sinlen = sizeof (struct sockaddr_in6);
9792 	} else {
9793 		addr = sin->sin_addr.s_addr;
9794 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9795 		sinlen = sizeof (struct sockaddr_in);
9796 	}
9797 	mutex_enter(&ill->ill_lock);
9798 	ov6addr = ipif->ipif_v6lcl_addr;
9799 	ipif->ipif_v6lcl_addr = v6addr;
9800 	sctp_update_ipif_addr(ipif, ov6addr);
9801 	ipif->ipif_addr_ready = 0;
9802 
9803 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9804 
9805 	/*
9806 	 * If the interface was previously marked as a duplicate, then since
9807 	 * we've now got a "new" address, it should no longer be considered a
9808 	 * duplicate -- even if the "new" address is the same as the old one.
9809 	 * Note that if all ipifs are down, we may have a pending ARP down
9810 	 * event to handle.  This is because we want to recover from duplicates
9811 	 * and thus delay tearing down ARP until the duplicates have been
9812 	 * removed or disabled.
9813 	 */
9814 	need_dl_down = need_arp_down = B_FALSE;
9815 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9816 		need_arp_down = !need_up;
9817 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9818 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9819 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9820 			need_dl_down = B_TRUE;
9821 		}
9822 	}
9823 
9824 	ipif_set_default(ipif);
9825 
9826 	/*
9827 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9828 	 * tag the ill so that future updates to the interface ID don't result
9829 	 * in this address getting automatically reconfigured from under the
9830 	 * administrator.
9831 	 */
9832 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9833 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9834 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9835 			ill->ill_manual_linklocal = 1;
9836 	}
9837 
9838 	/*
9839 	 * When publishing an interface address change event, we only notify
9840 	 * the event listeners of the new address.  It is assumed that if they
9841 	 * actively care about the addresses assigned that they will have
9842 	 * already discovered the previous address assigned (if there was one.)
9843 	 *
9844 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9845 	 */
9846 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9847 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9848 		    NE_ADDRESS_CHANGE, sin, sinlen);
9849 	}
9850 
9851 	mutex_exit(&ill->ill_lock);
9852 
9853 	if (need_up) {
9854 		/*
9855 		 * Now bring the interface back up.  If this
9856 		 * is the only IPIF for the ILL, ipif_up
9857 		 * will have to re-bind to the device, so
9858 		 * we may get back EINPROGRESS, in which
9859 		 * case, this IOCTL will get completed in
9860 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9861 		 */
9862 		err = ipif_up(ipif, q, mp);
9863 	} else {
9864 		/* Perhaps ilgs should use this ill */
9865 		update_conn_ill(NULL, ill->ill_ipst);
9866 	}
9867 
9868 	if (need_dl_down)
9869 		ill_dl_down(ill);
9870 
9871 	if (need_arp_down && !ill->ill_isv6)
9872 		(void) ipif_arp_down(ipif);
9873 
9874 	/*
9875 	 * The default multicast interface might have changed (for
9876 	 * instance if the IPv6 scope of the address changed)
9877 	 */
9878 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9879 
9880 	return (err);
9881 }
9882 
9883 /*
9884  * Restart entry point to restart the address set operation after the
9885  * refcounts have dropped to zero.
9886  */
9887 /* ARGSUSED */
9888 int
9889 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9890     ip_ioctl_cmd_t *ipip, void *ifreq)
9891 {
9892 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9893 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9894 	ASSERT(IAM_WRITER_IPIF(ipif));
9895 	(void) ipif_down_tail(ipif);
9896 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9897 }
9898 
9899 /* ARGSUSED */
9900 int
9901 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9902     ip_ioctl_cmd_t *ipip, void *if_req)
9903 {
9904 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9905 	struct lifreq *lifr = (struct lifreq *)if_req;
9906 
9907 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9908 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9909 	/*
9910 	 * The net mask and address can't change since we have a
9911 	 * reference to the ipif. So no lock is necessary.
9912 	 */
9913 	if (ipif->ipif_isv6) {
9914 		*sin6 = sin6_null;
9915 		sin6->sin6_family = AF_INET6;
9916 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9917 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9918 		lifr->lifr_addrlen =
9919 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9920 	} else {
9921 		*sin = sin_null;
9922 		sin->sin_family = AF_INET;
9923 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9924 		if (ipip->ipi_cmd_type == LIF_CMD) {
9925 			lifr->lifr_addrlen =
9926 			    ip_mask_to_plen(ipif->ipif_net_mask);
9927 		}
9928 	}
9929 	return (0);
9930 }
9931 
9932 /*
9933  * Set the destination address for a pt-pt interface.
9934  */
9935 /* ARGSUSED */
9936 int
9937 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9938     ip_ioctl_cmd_t *ipip, void *if_req)
9939 {
9940 	int err = 0;
9941 	in6_addr_t v6addr;
9942 	boolean_t need_up = B_FALSE;
9943 
9944 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9945 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9946 	ASSERT(IAM_WRITER_IPIF(ipif));
9947 
9948 	if (ipif->ipif_isv6) {
9949 		sin6_t *sin6;
9950 
9951 		if (sin->sin_family != AF_INET6)
9952 			return (EAFNOSUPPORT);
9953 
9954 		sin6 = (sin6_t *)sin;
9955 		v6addr = sin6->sin6_addr;
9956 
9957 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9958 			return (EADDRNOTAVAIL);
9959 	} else {
9960 		ipaddr_t addr;
9961 
9962 		if (sin->sin_family != AF_INET)
9963 			return (EAFNOSUPPORT);
9964 
9965 		addr = sin->sin_addr.s_addr;
9966 		if (addr != INADDR_ANY &&
9967 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
9968 			return (EADDRNOTAVAIL);
9969 		}
9970 
9971 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9972 	}
9973 
9974 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9975 		return (0);	/* No change */
9976 
9977 	if (ipif->ipif_flags & IPIF_UP) {
9978 		/*
9979 		 * If the interface is already marked up,
9980 		 * we call ipif_down which will take care
9981 		 * of ditching any IREs that have been set
9982 		 * up based on the old pp dst address.
9983 		 */
9984 		err = ipif_logical_down(ipif, q, mp);
9985 		if (err == EINPROGRESS)
9986 			return (err);
9987 		(void) ipif_down_tail(ipif);
9988 		need_up = B_TRUE;
9989 	}
9990 	/*
9991 	 * could return EINPROGRESS. If so ioctl will complete in
9992 	 * ip_rput_dlpi_writer
9993 	 */
9994 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9995 	return (err);
9996 }
9997 
9998 static int
9999 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10000     boolean_t need_up)
10001 {
10002 	in6_addr_t v6addr;
10003 	ill_t	*ill = ipif->ipif_ill;
10004 	int	err = 0;
10005 	boolean_t need_dl_down;
10006 	boolean_t need_arp_down;
10007 
10008 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10009 	    ipif->ipif_id, (void *)ipif));
10010 
10011 	/* Must cancel any pending timer before taking the ill_lock */
10012 	if (ipif->ipif_recovery_id != 0)
10013 		(void) untimeout(ipif->ipif_recovery_id);
10014 	ipif->ipif_recovery_id = 0;
10015 
10016 	if (ipif->ipif_isv6) {
10017 		sin6_t *sin6;
10018 
10019 		sin6 = (sin6_t *)sin;
10020 		v6addr = sin6->sin6_addr;
10021 	} else {
10022 		ipaddr_t addr;
10023 
10024 		addr = sin->sin_addr.s_addr;
10025 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10026 	}
10027 	mutex_enter(&ill->ill_lock);
10028 	/* Set point to point destination address. */
10029 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10030 		/*
10031 		 * Allow this as a means of creating logical
10032 		 * pt-pt interfaces on top of e.g. an Ethernet.
10033 		 * XXX Undocumented HACK for testing.
10034 		 * pt-pt interfaces are created with NUD disabled.
10035 		 */
10036 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10037 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10038 		if (ipif->ipif_isv6)
10039 			ill->ill_flags |= ILLF_NONUD;
10040 	}
10041 
10042 	/*
10043 	 * If the interface was previously marked as a duplicate, then since
10044 	 * we've now got a "new" address, it should no longer be considered a
10045 	 * duplicate -- even if the "new" address is the same as the old one.
10046 	 * Note that if all ipifs are down, we may have a pending ARP down
10047 	 * event to handle.
10048 	 */
10049 	need_dl_down = need_arp_down = B_FALSE;
10050 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10051 		need_arp_down = !need_up;
10052 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10053 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10054 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10055 			need_dl_down = B_TRUE;
10056 		}
10057 	}
10058 
10059 	/*
10060 	 * If we've just manually set the IPv6 destination link-local address
10061 	 * (0th ipif), tag the ill so that future updates to the destination
10062 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10063 	 * result in this address getting automatically reconfigured from
10064 	 * under the administrator.
10065 	 */
10066 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10067 		ill->ill_manual_dst_linklocal = 1;
10068 
10069 	/* Set the new address. */
10070 	ipif->ipif_v6pp_dst_addr = v6addr;
10071 	/* Make sure subnet tracks pp_dst */
10072 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10073 	mutex_exit(&ill->ill_lock);
10074 
10075 	if (need_up) {
10076 		/*
10077 		 * Now bring the interface back up.  If this
10078 		 * is the only IPIF for the ILL, ipif_up
10079 		 * will have to re-bind to the device, so
10080 		 * we may get back EINPROGRESS, in which
10081 		 * case, this IOCTL will get completed in
10082 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10083 		 */
10084 		err = ipif_up(ipif, q, mp);
10085 	}
10086 
10087 	if (need_dl_down)
10088 		ill_dl_down(ill);
10089 	if (need_arp_down && !ipif->ipif_isv6)
10090 		(void) ipif_arp_down(ipif);
10091 
10092 	return (err);
10093 }
10094 
10095 /*
10096  * Restart entry point to restart the dstaddress set operation after the
10097  * refcounts have dropped to zero.
10098  */
10099 /* ARGSUSED */
10100 int
10101 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10102     ip_ioctl_cmd_t *ipip, void *ifreq)
10103 {
10104 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10105 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10106 	(void) ipif_down_tail(ipif);
10107 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10108 }
10109 
10110 /* ARGSUSED */
10111 int
10112 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10113     ip_ioctl_cmd_t *ipip, void *if_req)
10114 {
10115 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10116 
10117 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10118 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10119 	/*
10120 	 * Get point to point destination address. The addresses can't
10121 	 * change since we hold a reference to the ipif.
10122 	 */
10123 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10124 		return (EADDRNOTAVAIL);
10125 
10126 	if (ipif->ipif_isv6) {
10127 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10128 		*sin6 = sin6_null;
10129 		sin6->sin6_family = AF_INET6;
10130 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10131 	} else {
10132 		*sin = sin_null;
10133 		sin->sin_family = AF_INET;
10134 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10135 	}
10136 	return (0);
10137 }
10138 
10139 /*
10140  * Check which flags will change by the given flags being set
10141  * silently ignore flags which userland is not allowed to control.
10142  * (Because these flags may change between SIOCGLIFFLAGS and
10143  * SIOCSLIFFLAGS, and that's outside of userland's control,
10144  * we need to silently ignore them rather than fail.)
10145  */
10146 static void
10147 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10148     uint64_t *offp)
10149 {
10150 	ill_t		*ill = ipif->ipif_ill;
10151 	phyint_t 	*phyi = ill->ill_phyint;
10152 	uint64_t	cantchange_flags, intf_flags;
10153 	uint64_t	turn_on, turn_off;
10154 
10155 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10156 	cantchange_flags = IFF_CANTCHANGE;
10157 	if (IS_IPMP(ill))
10158 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10159 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10160 	turn_off = intf_flags & turn_on;
10161 	turn_on ^= turn_off;
10162 	*onp = turn_on;
10163 	*offp = turn_off;
10164 }
10165 
10166 /*
10167  * Set interface flags.  Many flags require special handling (e.g.,
10168  * bringing the interface down); see below for details.
10169  *
10170  * NOTE : We really don't enforce that ipif_id zero should be used
10171  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10172  *	  is because applications generally does SICGLIFFLAGS and
10173  *	  ORs in the new flags (that affects the logical) and does a
10174  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10175  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10176  *	  flags that will be turned on is correct with respect to
10177  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10178  */
10179 /* ARGSUSED */
10180 int
10181 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10182     ip_ioctl_cmd_t *ipip, void *if_req)
10183 {
10184 	uint64_t turn_on;
10185 	uint64_t turn_off;
10186 	int	err = 0;
10187 	phyint_t *phyi;
10188 	ill_t *ill;
10189 	conn_t *connp;
10190 	uint64_t intf_flags;
10191 	boolean_t phyint_flags_modified = B_FALSE;
10192 	uint64_t flags;
10193 	struct ifreq *ifr;
10194 	struct lifreq *lifr;
10195 	boolean_t set_linklocal = B_FALSE;
10196 
10197 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10198 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10199 
10200 	ASSERT(IAM_WRITER_IPIF(ipif));
10201 
10202 	ill = ipif->ipif_ill;
10203 	phyi = ill->ill_phyint;
10204 
10205 	if (ipip->ipi_cmd_type == IF_CMD) {
10206 		ifr = (struct ifreq *)if_req;
10207 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10208 	} else {
10209 		lifr = (struct lifreq *)if_req;
10210 		flags = lifr->lifr_flags;
10211 	}
10212 
10213 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10214 
10215 	/*
10216 	 * Have the flags been set correctly until now?
10217 	 */
10218 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10219 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10220 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10221 	/*
10222 	 * Compare the new flags to the old, and partition
10223 	 * into those coming on and those going off.
10224 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10225 	 */
10226 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10227 		flags |= intf_flags & ~0xFFFF;
10228 
10229 	/*
10230 	 * Explicitly fail attempts to change flags that are always invalid on
10231 	 * an IPMP meta-interface.
10232 	 */
10233 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10234 		return (EINVAL);
10235 
10236 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10237 	if ((turn_on|turn_off) == 0)
10238 		return (0);	/* No change */
10239 
10240 	/*
10241 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10242 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10243 	 * allow it to be turned off.
10244 	 */
10245 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10246 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10247 		return (EINVAL);
10248 
10249 	if ((connp = Q_TO_CONN(q)) == NULL)
10250 		return (EINVAL);
10251 
10252 	/*
10253 	 * Only vrrp control socket is allowed to change IFF_UP and
10254 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10255 	 */
10256 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10257 		if (!connp->conn_isvrrp)
10258 			return (EINVAL);
10259 	}
10260 
10261 	/*
10262 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10263 	 * VRRP control socket.
10264 	 */
10265 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10266 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10267 			return (EINVAL);
10268 	}
10269 
10270 	if (turn_on & IFF_NOFAILOVER) {
10271 		turn_on |= IFF_DEPRECATED;
10272 		flags |= IFF_DEPRECATED;
10273 	}
10274 
10275 	/*
10276 	 * On underlying interfaces, only allow applications to manage test
10277 	 * addresses -- otherwise, they may get confused when the address
10278 	 * moves as part of being brought up.  Likewise, prevent an
10279 	 * application-managed test address from being converted to a data
10280 	 * address.  To prevent migration of administratively up addresses in
10281 	 * the kernel, we don't allow them to be converted either.
10282 	 */
10283 	if (IS_UNDER_IPMP(ill)) {
10284 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10285 
10286 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10287 			return (EINVAL);
10288 
10289 		if ((turn_off & IFF_NOFAILOVER) &&
10290 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10291 			return (EINVAL);
10292 	}
10293 
10294 	/*
10295 	 * Only allow IFF_TEMPORARY flag to be set on
10296 	 * IPv6 interfaces.
10297 	 */
10298 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10299 		return (EINVAL);
10300 
10301 	/*
10302 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10303 	 */
10304 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10305 		return (EINVAL);
10306 
10307 	/*
10308 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10309 	 * interfaces.  It makes no sense in that context.
10310 	 */
10311 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10312 		return (EINVAL);
10313 
10314 	/*
10315 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10316 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10317 	 * If the link local address isn't set, and can be set, it will get
10318 	 * set later on in this function.
10319 	 */
10320 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10321 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10322 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10323 		if (ipif_cant_setlinklocal(ipif))
10324 			return (EINVAL);
10325 		set_linklocal = B_TRUE;
10326 	}
10327 
10328 	/*
10329 	 * If we modify physical interface flags, we'll potentially need to
10330 	 * send up two routing socket messages for the changes (one for the
10331 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10332 	 */
10333 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10334 		phyint_flags_modified = B_TRUE;
10335 
10336 	/*
10337 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10338 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10339 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10340 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10341 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10342 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10343 	 * will not be honored.
10344 	 */
10345 	if (turn_on & PHYI_STANDBY) {
10346 		/*
10347 		 * No need to grab ill_g_usesrc_lock here; see the
10348 		 * synchronization notes in ip.c.
10349 		 */
10350 		if (ill->ill_usesrc_grp_next != NULL ||
10351 		    intf_flags & PHYI_INACTIVE)
10352 			return (EINVAL);
10353 		if (!(flags & PHYI_FAILED)) {
10354 			flags |= PHYI_INACTIVE;
10355 			turn_on |= PHYI_INACTIVE;
10356 		}
10357 	}
10358 
10359 	if (turn_off & PHYI_STANDBY) {
10360 		flags &= ~PHYI_INACTIVE;
10361 		turn_off |= PHYI_INACTIVE;
10362 	}
10363 
10364 	/*
10365 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10366 	 * would end up on.
10367 	 */
10368 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10369 	    (PHYI_FAILED | PHYI_INACTIVE))
10370 		return (EINVAL);
10371 
10372 	/*
10373 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10374 	 * status of the interface.
10375 	 */
10376 	if ((turn_on | turn_off) & ILLF_ROUTER)
10377 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10378 
10379 	/*
10380 	 * If the interface is not UP and we are not going to
10381 	 * bring it UP, record the flags and return. When the
10382 	 * interface comes UP later, the right actions will be
10383 	 * taken.
10384 	 */
10385 	if (!(ipif->ipif_flags & IPIF_UP) &&
10386 	    !(turn_on & IPIF_UP)) {
10387 		/* Record new flags in their respective places. */
10388 		mutex_enter(&ill->ill_lock);
10389 		mutex_enter(&ill->ill_phyint->phyint_lock);
10390 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10391 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10392 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10393 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10394 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10395 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10396 		mutex_exit(&ill->ill_lock);
10397 		mutex_exit(&ill->ill_phyint->phyint_lock);
10398 
10399 		/*
10400 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10401 		 * same to the kernel: if any of them has been set by
10402 		 * userland, the interface cannot be used for data traffic.
10403 		 */
10404 		if ((turn_on|turn_off) &
10405 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10406 			ASSERT(!IS_IPMP(ill));
10407 			/*
10408 			 * It's possible the ill is part of an "anonymous"
10409 			 * IPMP group rather than a real group.  In that case,
10410 			 * there are no other interfaces in the group and thus
10411 			 * no need to call ipmp_phyint_refresh_active().
10412 			 */
10413 			if (IS_UNDER_IPMP(ill))
10414 				ipmp_phyint_refresh_active(phyi);
10415 		}
10416 
10417 		if (phyint_flags_modified) {
10418 			if (phyi->phyint_illv4 != NULL) {
10419 				ip_rts_ifmsg(phyi->phyint_illv4->
10420 				    ill_ipif, RTSQ_DEFAULT);
10421 			}
10422 			if (phyi->phyint_illv6 != NULL) {
10423 				ip_rts_ifmsg(phyi->phyint_illv6->
10424 				    ill_ipif, RTSQ_DEFAULT);
10425 			}
10426 		}
10427 		/* The default multicast interface might have changed */
10428 		ire_increment_multicast_generation(ill->ill_ipst,
10429 		    ill->ill_isv6);
10430 
10431 		return (0);
10432 	} else if (set_linklocal) {
10433 		mutex_enter(&ill->ill_lock);
10434 		if (set_linklocal)
10435 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10436 		mutex_exit(&ill->ill_lock);
10437 	}
10438 
10439 	/*
10440 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10441 	 * or point-to-point interfaces with an unspecified destination. We do
10442 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10443 	 * have a subnet assigned, which is how in.ndpd currently manages its
10444 	 * onlink prefix list when no addresses are configured with those
10445 	 * prefixes.
10446 	 */
10447 	if (ipif->ipif_isv6 &&
10448 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10449 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10450 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10451 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10452 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10453 		return (EINVAL);
10454 	}
10455 
10456 	/*
10457 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10458 	 * from being brought up.
10459 	 */
10460 	if (!ipif->ipif_isv6 &&
10461 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10462 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10463 		return (EINVAL);
10464 	}
10465 
10466 	/*
10467 	 * If we are going to change one or more of the flags that are
10468 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10469 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10470 	 * IPIF_NOFAILOVER, we will take special action.  This is
10471 	 * done by bring the ipif down, changing the flags and bringing
10472 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10473 	 * back up will trigger the address to be moved.
10474 	 *
10475 	 * If we are going to change IFF_NOACCEPT, we need to bring
10476 	 * all the ipifs down then bring them up again.	 The act of
10477 	 * bringing all the ipifs back up will trigger the local
10478 	 * ires being recreated with "no_accept" set/cleared.
10479 	 *
10480 	 * Note that ILLF_NOACCEPT is always set separately from the
10481 	 * other flags.
10482 	 */
10483 	if ((turn_on|turn_off) &
10484 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10485 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10486 	    IPIF_NOFAILOVER)) {
10487 		/*
10488 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10489 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10490 		 * entries shared between multiple ipifs on the same subnet.
10491 		 */
10492 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10493 		    !(turn_off & IPIF_UP)) {
10494 			if (ipif->ipif_flags & IPIF_UP)
10495 				ill->ill_logical_down = 1;
10496 			turn_on &= ~IPIF_UP;
10497 		}
10498 		err = ipif_down(ipif, q, mp);
10499 		ip1dbg(("ipif_down returns %d err ", err));
10500 		if (err == EINPROGRESS)
10501 			return (err);
10502 		(void) ipif_down_tail(ipif);
10503 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10504 		/*
10505 		 * If we can quiesce the ill, then continue.  If not, then
10506 		 * ip_sioctl_flags_tail() will be called from
10507 		 * ipif_ill_refrele_tail().
10508 		 */
10509 		ill_down_ipifs(ill, B_TRUE);
10510 
10511 		mutex_enter(&connp->conn_lock);
10512 		mutex_enter(&ill->ill_lock);
10513 		if (!ill_is_quiescent(ill)) {
10514 			boolean_t success;
10515 
10516 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10517 			    q, mp, ILL_DOWN);
10518 			mutex_exit(&ill->ill_lock);
10519 			mutex_exit(&connp->conn_lock);
10520 			return (success ? EINPROGRESS : EINTR);
10521 		}
10522 		mutex_exit(&ill->ill_lock);
10523 		mutex_exit(&connp->conn_lock);
10524 	}
10525 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10526 }
10527 
10528 static int
10529 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10530 {
10531 	ill_t	*ill;
10532 	phyint_t *phyi;
10533 	uint64_t turn_on, turn_off;
10534 	boolean_t phyint_flags_modified = B_FALSE;
10535 	int	err = 0;
10536 	boolean_t set_linklocal = B_FALSE;
10537 
10538 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10539 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10540 
10541 	ASSERT(IAM_WRITER_IPIF(ipif));
10542 
10543 	ill = ipif->ipif_ill;
10544 	phyi = ill->ill_phyint;
10545 
10546 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10547 
10548 	/*
10549 	 * IFF_UP is handled separately.
10550 	 */
10551 	turn_on &= ~IFF_UP;
10552 	turn_off &= ~IFF_UP;
10553 
10554 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10555 		phyint_flags_modified = B_TRUE;
10556 
10557 	/*
10558 	 * Now we change the flags. Track current value of
10559 	 * other flags in their respective places.
10560 	 */
10561 	mutex_enter(&ill->ill_lock);
10562 	mutex_enter(&phyi->phyint_lock);
10563 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10564 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10565 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10566 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10567 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10568 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10569 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10570 		set_linklocal = B_TRUE;
10571 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10572 	}
10573 
10574 	mutex_exit(&ill->ill_lock);
10575 	mutex_exit(&phyi->phyint_lock);
10576 
10577 	if (set_linklocal)
10578 		(void) ipif_setlinklocal(ipif);
10579 
10580 	/*
10581 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10582 	 * the kernel: if any of them has been set by userland, the interface
10583 	 * cannot be used for data traffic.
10584 	 */
10585 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10586 		ASSERT(!IS_IPMP(ill));
10587 		/*
10588 		 * It's possible the ill is part of an "anonymous" IPMP group
10589 		 * rather than a real group.  In that case, there are no other
10590 		 * interfaces in the group and thus no need for us to call
10591 		 * ipmp_phyint_refresh_active().
10592 		 */
10593 		if (IS_UNDER_IPMP(ill))
10594 			ipmp_phyint_refresh_active(phyi);
10595 	}
10596 
10597 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10598 		/*
10599 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10600 		 * ipifs that were brought down.
10601 		 *
10602 		 * The routing sockets messages are sent as the result
10603 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10604 		 * as well.
10605 		 */
10606 		err = ill_up_ipifs(ill, q, mp);
10607 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10608 		/*
10609 		 * XXX ipif_up really does not know whether a phyint flags
10610 		 * was modified or not. So, it sends up information on
10611 		 * only one routing sockets message. As we don't bring up
10612 		 * the interface and also set PHYI_ flags simultaneously
10613 		 * it should be okay.
10614 		 */
10615 		err = ipif_up(ipif, q, mp);
10616 	} else {
10617 		/*
10618 		 * Make sure routing socket sees all changes to the flags.
10619 		 * ipif_up_done* handles this when we use ipif_up.
10620 		 */
10621 		if (phyint_flags_modified) {
10622 			if (phyi->phyint_illv4 != NULL) {
10623 				ip_rts_ifmsg(phyi->phyint_illv4->
10624 				    ill_ipif, RTSQ_DEFAULT);
10625 			}
10626 			if (phyi->phyint_illv6 != NULL) {
10627 				ip_rts_ifmsg(phyi->phyint_illv6->
10628 				    ill_ipif, RTSQ_DEFAULT);
10629 			}
10630 		} else {
10631 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10632 		}
10633 		/*
10634 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10635 		 * this in need_up case.
10636 		 */
10637 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10638 	}
10639 
10640 	/* The default multicast interface might have changed */
10641 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10642 	return (err);
10643 }
10644 
10645 /*
10646  * Restart the flags operation now that the refcounts have dropped to zero.
10647  */
10648 /* ARGSUSED */
10649 int
10650 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10651     ip_ioctl_cmd_t *ipip, void *if_req)
10652 {
10653 	uint64_t flags;
10654 	struct ifreq *ifr = if_req;
10655 	struct lifreq *lifr = if_req;
10656 	uint64_t turn_on, turn_off;
10657 
10658 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10659 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10660 
10661 	if (ipip->ipi_cmd_type == IF_CMD) {
10662 		/* cast to uint16_t prevents unwanted sign extension */
10663 		flags = (uint16_t)ifr->ifr_flags;
10664 	} else {
10665 		flags = lifr->lifr_flags;
10666 	}
10667 
10668 	/*
10669 	 * If this function call is a result of the ILLF_NOACCEPT flag
10670 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10671 	 */
10672 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10673 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10674 		(void) ipif_down_tail(ipif);
10675 
10676 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10677 }
10678 
10679 /*
10680  * Can operate on either a module or a driver queue.
10681  */
10682 /* ARGSUSED */
10683 int
10684 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10685     ip_ioctl_cmd_t *ipip, void *if_req)
10686 {
10687 	/*
10688 	 * Has the flags been set correctly till now ?
10689 	 */
10690 	ill_t *ill = ipif->ipif_ill;
10691 	phyint_t *phyi = ill->ill_phyint;
10692 
10693 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10695 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10696 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10697 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10698 
10699 	/*
10700 	 * Need a lock since some flags can be set even when there are
10701 	 * references to the ipif.
10702 	 */
10703 	mutex_enter(&ill->ill_lock);
10704 	if (ipip->ipi_cmd_type == IF_CMD) {
10705 		struct ifreq *ifr = (struct ifreq *)if_req;
10706 
10707 		/* Get interface flags (low 16 only). */
10708 		ifr->ifr_flags = ((ipif->ipif_flags |
10709 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10710 	} else {
10711 		struct lifreq *lifr = (struct lifreq *)if_req;
10712 
10713 		/* Get interface flags. */
10714 		lifr->lifr_flags = ipif->ipif_flags |
10715 		    ill->ill_flags | phyi->phyint_flags;
10716 	}
10717 	mutex_exit(&ill->ill_lock);
10718 	return (0);
10719 }
10720 
10721 /*
10722  * We allow the MTU to be set on an ILL, but not have it be different
10723  * for different IPIFs since we don't actually send packets on IPIFs.
10724  */
10725 /* ARGSUSED */
10726 int
10727 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10728     ip_ioctl_cmd_t *ipip, void *if_req)
10729 {
10730 	int mtu;
10731 	int ip_min_mtu;
10732 	struct ifreq	*ifr;
10733 	struct lifreq *lifr;
10734 	ill_t	*ill;
10735 
10736 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10737 	    ipif->ipif_id, (void *)ipif));
10738 	if (ipip->ipi_cmd_type == IF_CMD) {
10739 		ifr = (struct ifreq *)if_req;
10740 		mtu = ifr->ifr_metric;
10741 	} else {
10742 		lifr = (struct lifreq *)if_req;
10743 		mtu = lifr->lifr_mtu;
10744 	}
10745 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10746 	if (ipif->ipif_id != 0)
10747 		return (EINVAL);
10748 
10749 	ill = ipif->ipif_ill;
10750 	if (ipif->ipif_isv6)
10751 		ip_min_mtu = IPV6_MIN_MTU;
10752 	else
10753 		ip_min_mtu = IP_MIN_MTU;
10754 
10755 	mutex_enter(&ill->ill_lock);
10756 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10757 		mutex_exit(&ill->ill_lock);
10758 		return (EINVAL);
10759 	}
10760 	/*
10761 	 * The dce and fragmentation code can handle changes to ill_mtu
10762 	 * concurrent with sending/fragmenting packets.
10763 	 */
10764 	ill->ill_mtu = mtu;
10765 	ill->ill_flags |= ILLF_FIXEDMTU;
10766 	mutex_exit(&ill->ill_lock);
10767 
10768 	/*
10769 	 * Make sure all dce_generation checks find out
10770 	 * that ill_mtu has changed.
10771 	 */
10772 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10773 
10774 	/*
10775 	 * Refresh IPMP meta-interface MTU if necessary.
10776 	 */
10777 	if (IS_UNDER_IPMP(ill))
10778 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10779 
10780 	/* Update the MTU in SCTP's list */
10781 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10782 	return (0);
10783 }
10784 
10785 /* Get interface MTU. */
10786 /* ARGSUSED */
10787 int
10788 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10789 	ip_ioctl_cmd_t *ipip, void *if_req)
10790 {
10791 	struct ifreq	*ifr;
10792 	struct lifreq	*lifr;
10793 
10794 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10795 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10796 
10797 	/*
10798 	 * We allow a get on any logical interface even though the set
10799 	 * can only be done on logical unit 0.
10800 	 */
10801 	if (ipip->ipi_cmd_type == IF_CMD) {
10802 		ifr = (struct ifreq *)if_req;
10803 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10804 	} else {
10805 		lifr = (struct lifreq *)if_req;
10806 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10807 	}
10808 	return (0);
10809 }
10810 
10811 /* Set interface broadcast address. */
10812 /* ARGSUSED2 */
10813 int
10814 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10815 	ip_ioctl_cmd_t *ipip, void *if_req)
10816 {
10817 	ipaddr_t addr;
10818 	ire_t	*ire;
10819 	ill_t		*ill = ipif->ipif_ill;
10820 	ip_stack_t	*ipst = ill->ill_ipst;
10821 
10822 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10823 	    ipif->ipif_id));
10824 
10825 	ASSERT(IAM_WRITER_IPIF(ipif));
10826 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10827 		return (EADDRNOTAVAIL);
10828 
10829 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10830 
10831 	if (sin->sin_family != AF_INET)
10832 		return (EAFNOSUPPORT);
10833 
10834 	addr = sin->sin_addr.s_addr;
10835 
10836 	if (ipif->ipif_flags & IPIF_UP) {
10837 		/*
10838 		 * If we are already up, make sure the new
10839 		 * broadcast address makes sense.  If it does,
10840 		 * there should be an IRE for it already.
10841 		 */
10842 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10843 		    ill, ipif->ipif_zoneid, NULL,
10844 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10845 		if (ire == NULL) {
10846 			return (EINVAL);
10847 		} else {
10848 			ire_refrele(ire);
10849 		}
10850 	}
10851 	/*
10852 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10853 	 * needs to already exist we never need to change the set of
10854 	 * IRE_BROADCASTs when we are UP.
10855 	 */
10856 	if (addr != ipif->ipif_brd_addr)
10857 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10858 
10859 	return (0);
10860 }
10861 
10862 /* Get interface broadcast address. */
10863 /* ARGSUSED */
10864 int
10865 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10866     ip_ioctl_cmd_t *ipip, void *if_req)
10867 {
10868 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10869 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10870 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10871 		return (EADDRNOTAVAIL);
10872 
10873 	/* IPIF_BROADCAST not possible with IPv6 */
10874 	ASSERT(!ipif->ipif_isv6);
10875 	*sin = sin_null;
10876 	sin->sin_family = AF_INET;
10877 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10878 	return (0);
10879 }
10880 
10881 /*
10882  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10883  */
10884 /* ARGSUSED */
10885 int
10886 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10887     ip_ioctl_cmd_t *ipip, void *if_req)
10888 {
10889 	int err = 0;
10890 	in6_addr_t v6mask;
10891 
10892 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10893 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10894 
10895 	ASSERT(IAM_WRITER_IPIF(ipif));
10896 
10897 	if (ipif->ipif_isv6) {
10898 		sin6_t *sin6;
10899 
10900 		if (sin->sin_family != AF_INET6)
10901 			return (EAFNOSUPPORT);
10902 
10903 		sin6 = (sin6_t *)sin;
10904 		v6mask = sin6->sin6_addr;
10905 	} else {
10906 		ipaddr_t mask;
10907 
10908 		if (sin->sin_family != AF_INET)
10909 			return (EAFNOSUPPORT);
10910 
10911 		mask = sin->sin_addr.s_addr;
10912 		if (!ip_contiguous_mask(ntohl(mask)))
10913 			return (ENOTSUP);
10914 		V4MASK_TO_V6(mask, v6mask);
10915 	}
10916 
10917 	/*
10918 	 * No big deal if the interface isn't already up, or the mask
10919 	 * isn't really changing, or this is pt-pt.
10920 	 */
10921 	if (!(ipif->ipif_flags & IPIF_UP) ||
10922 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10923 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10924 		ipif->ipif_v6net_mask = v6mask;
10925 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10926 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10927 			    ipif->ipif_v6net_mask,
10928 			    ipif->ipif_v6subnet);
10929 		}
10930 		return (0);
10931 	}
10932 	/*
10933 	 * Make sure we have valid net and subnet broadcast ire's
10934 	 * for the old netmask, if needed by other logical interfaces.
10935 	 */
10936 	err = ipif_logical_down(ipif, q, mp);
10937 	if (err == EINPROGRESS)
10938 		return (err);
10939 	(void) ipif_down_tail(ipif);
10940 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10941 	return (err);
10942 }
10943 
10944 static int
10945 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10946 {
10947 	in6_addr_t v6mask;
10948 	int err = 0;
10949 
10950 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10951 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10952 
10953 	if (ipif->ipif_isv6) {
10954 		sin6_t *sin6;
10955 
10956 		sin6 = (sin6_t *)sin;
10957 		v6mask = sin6->sin6_addr;
10958 	} else {
10959 		ipaddr_t mask;
10960 
10961 		mask = sin->sin_addr.s_addr;
10962 		V4MASK_TO_V6(mask, v6mask);
10963 	}
10964 
10965 	ipif->ipif_v6net_mask = v6mask;
10966 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10967 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10968 		    ipif->ipif_v6subnet);
10969 	}
10970 	err = ipif_up(ipif, q, mp);
10971 
10972 	if (err == 0 || err == EINPROGRESS) {
10973 		/*
10974 		 * The interface must be DL_BOUND if this packet has to
10975 		 * go out on the wire. Since we only go through a logical
10976 		 * down and are bound with the driver during an internal
10977 		 * down/up that is satisfied.
10978 		 */
10979 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10980 			/* Potentially broadcast an address mask reply. */
10981 			ipif_mask_reply(ipif);
10982 		}
10983 	}
10984 	return (err);
10985 }
10986 
10987 /* ARGSUSED */
10988 int
10989 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10990     ip_ioctl_cmd_t *ipip, void *if_req)
10991 {
10992 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10993 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10994 	(void) ipif_down_tail(ipif);
10995 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10996 }
10997 
10998 /* Get interface net mask. */
10999 /* ARGSUSED */
11000 int
11001 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11002     ip_ioctl_cmd_t *ipip, void *if_req)
11003 {
11004 	struct lifreq *lifr = (struct lifreq *)if_req;
11005 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11006 
11007 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11008 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11009 
11010 	/*
11011 	 * net mask can't change since we have a reference to the ipif.
11012 	 */
11013 	if (ipif->ipif_isv6) {
11014 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11015 		*sin6 = sin6_null;
11016 		sin6->sin6_family = AF_INET6;
11017 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11018 		lifr->lifr_addrlen =
11019 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11020 	} else {
11021 		*sin = sin_null;
11022 		sin->sin_family = AF_INET;
11023 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11024 		if (ipip->ipi_cmd_type == LIF_CMD) {
11025 			lifr->lifr_addrlen =
11026 			    ip_mask_to_plen(ipif->ipif_net_mask);
11027 		}
11028 	}
11029 	return (0);
11030 }
11031 
11032 /* ARGSUSED */
11033 int
11034 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11035     ip_ioctl_cmd_t *ipip, void *if_req)
11036 {
11037 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11039 
11040 	/*
11041 	 * Since no applications should ever be setting metrics on underlying
11042 	 * interfaces, we explicitly fail to smoke 'em out.
11043 	 */
11044 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11045 		return (EINVAL);
11046 
11047 	/*
11048 	 * Set interface metric.  We don't use this for
11049 	 * anything but we keep track of it in case it is
11050 	 * important to routing applications or such.
11051 	 */
11052 	if (ipip->ipi_cmd_type == IF_CMD) {
11053 		struct ifreq    *ifr;
11054 
11055 		ifr = (struct ifreq *)if_req;
11056 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11057 	} else {
11058 		struct lifreq   *lifr;
11059 
11060 		lifr = (struct lifreq *)if_req;
11061 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11062 	}
11063 	return (0);
11064 }
11065 
11066 /* ARGSUSED */
11067 int
11068 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11069     ip_ioctl_cmd_t *ipip, void *if_req)
11070 {
11071 	/* Get interface metric. */
11072 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11073 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11074 
11075 	if (ipip->ipi_cmd_type == IF_CMD) {
11076 		struct ifreq    *ifr;
11077 
11078 		ifr = (struct ifreq *)if_req;
11079 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11080 	} else {
11081 		struct lifreq   *lifr;
11082 
11083 		lifr = (struct lifreq *)if_req;
11084 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11085 	}
11086 
11087 	return (0);
11088 }
11089 
11090 /* ARGSUSED */
11091 int
11092 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11093     ip_ioctl_cmd_t *ipip, void *if_req)
11094 {
11095 	int	arp_muxid;
11096 
11097 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11098 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11099 	/*
11100 	 * Set the muxid returned from I_PLINK.
11101 	 */
11102 	if (ipip->ipi_cmd_type == IF_CMD) {
11103 		struct ifreq *ifr = (struct ifreq *)if_req;
11104 
11105 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11106 		arp_muxid = ifr->ifr_arp_muxid;
11107 	} else {
11108 		struct lifreq *lifr = (struct lifreq *)if_req;
11109 
11110 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11111 		arp_muxid = lifr->lifr_arp_muxid;
11112 	}
11113 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11114 	return (0);
11115 }
11116 
11117 /* ARGSUSED */
11118 int
11119 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11120     ip_ioctl_cmd_t *ipip, void *if_req)
11121 {
11122 	int	arp_muxid = 0;
11123 
11124 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11125 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11126 	/*
11127 	 * Get the muxid saved in ill for I_PUNLINK.
11128 	 */
11129 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11130 	if (ipip->ipi_cmd_type == IF_CMD) {
11131 		struct ifreq *ifr = (struct ifreq *)if_req;
11132 
11133 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11134 		ifr->ifr_arp_muxid = arp_muxid;
11135 	} else {
11136 		struct lifreq *lifr = (struct lifreq *)if_req;
11137 
11138 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11139 		lifr->lifr_arp_muxid = arp_muxid;
11140 	}
11141 	return (0);
11142 }
11143 
11144 /*
11145  * Set the subnet prefix. Does not modify the broadcast address.
11146  */
11147 /* ARGSUSED */
11148 int
11149 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11150     ip_ioctl_cmd_t *ipip, void *if_req)
11151 {
11152 	int err = 0;
11153 	in6_addr_t v6addr;
11154 	in6_addr_t v6mask;
11155 	boolean_t need_up = B_FALSE;
11156 	int addrlen;
11157 
11158 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11159 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11160 
11161 	ASSERT(IAM_WRITER_IPIF(ipif));
11162 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11163 
11164 	if (ipif->ipif_isv6) {
11165 		sin6_t *sin6;
11166 
11167 		if (sin->sin_family != AF_INET6)
11168 			return (EAFNOSUPPORT);
11169 
11170 		sin6 = (sin6_t *)sin;
11171 		v6addr = sin6->sin6_addr;
11172 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11173 			return (EADDRNOTAVAIL);
11174 	} else {
11175 		ipaddr_t addr;
11176 
11177 		if (sin->sin_family != AF_INET)
11178 			return (EAFNOSUPPORT);
11179 
11180 		addr = sin->sin_addr.s_addr;
11181 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11182 			return (EADDRNOTAVAIL);
11183 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11184 		/* Add 96 bits */
11185 		addrlen += IPV6_ABITS - IP_ABITS;
11186 	}
11187 
11188 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11189 		return (EINVAL);
11190 
11191 	/* Check if bits in the address is set past the mask */
11192 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11193 		return (EINVAL);
11194 
11195 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11196 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11197 		return (0);	/* No change */
11198 
11199 	if (ipif->ipif_flags & IPIF_UP) {
11200 		/*
11201 		 * If the interface is already marked up,
11202 		 * we call ipif_down which will take care
11203 		 * of ditching any IREs that have been set
11204 		 * up based on the old interface address.
11205 		 */
11206 		err = ipif_logical_down(ipif, q, mp);
11207 		if (err == EINPROGRESS)
11208 			return (err);
11209 		(void) ipif_down_tail(ipif);
11210 		need_up = B_TRUE;
11211 	}
11212 
11213 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11214 	return (err);
11215 }
11216 
11217 static int
11218 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11219     queue_t *q, mblk_t *mp, boolean_t need_up)
11220 {
11221 	ill_t	*ill = ipif->ipif_ill;
11222 	int	err = 0;
11223 
11224 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11225 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11226 
11227 	/* Set the new address. */
11228 	mutex_enter(&ill->ill_lock);
11229 	ipif->ipif_v6net_mask = v6mask;
11230 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11231 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11232 		    ipif->ipif_v6subnet);
11233 	}
11234 	mutex_exit(&ill->ill_lock);
11235 
11236 	if (need_up) {
11237 		/*
11238 		 * Now bring the interface back up.  If this
11239 		 * is the only IPIF for the ILL, ipif_up
11240 		 * will have to re-bind to the device, so
11241 		 * we may get back EINPROGRESS, in which
11242 		 * case, this IOCTL will get completed in
11243 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11244 		 */
11245 		err = ipif_up(ipif, q, mp);
11246 		if (err == EINPROGRESS)
11247 			return (err);
11248 	}
11249 	return (err);
11250 }
11251 
11252 /* ARGSUSED */
11253 int
11254 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11255     ip_ioctl_cmd_t *ipip, void *if_req)
11256 {
11257 	int	addrlen;
11258 	in6_addr_t v6addr;
11259 	in6_addr_t v6mask;
11260 	struct lifreq *lifr = (struct lifreq *)if_req;
11261 
11262 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11263 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11264 	(void) ipif_down_tail(ipif);
11265 
11266 	addrlen = lifr->lifr_addrlen;
11267 	if (ipif->ipif_isv6) {
11268 		sin6_t *sin6;
11269 
11270 		sin6 = (sin6_t *)sin;
11271 		v6addr = sin6->sin6_addr;
11272 	} else {
11273 		ipaddr_t addr;
11274 
11275 		addr = sin->sin_addr.s_addr;
11276 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11277 		addrlen += IPV6_ABITS - IP_ABITS;
11278 	}
11279 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11280 
11281 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11282 }
11283 
11284 /* ARGSUSED */
11285 int
11286 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11287     ip_ioctl_cmd_t *ipip, void *if_req)
11288 {
11289 	struct lifreq *lifr = (struct lifreq *)if_req;
11290 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11291 
11292 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11293 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11294 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11295 
11296 	if (ipif->ipif_isv6) {
11297 		*sin6 = sin6_null;
11298 		sin6->sin6_family = AF_INET6;
11299 		sin6->sin6_addr = ipif->ipif_v6subnet;
11300 		lifr->lifr_addrlen =
11301 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11302 	} else {
11303 		*sin = sin_null;
11304 		sin->sin_family = AF_INET;
11305 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11306 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11307 	}
11308 	return (0);
11309 }
11310 
11311 /*
11312  * Set the IPv6 address token.
11313  */
11314 /* ARGSUSED */
11315 int
11316 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11317     ip_ioctl_cmd_t *ipi, void *if_req)
11318 {
11319 	ill_t *ill = ipif->ipif_ill;
11320 	int err;
11321 	in6_addr_t v6addr;
11322 	in6_addr_t v6mask;
11323 	boolean_t need_up = B_FALSE;
11324 	int i;
11325 	sin6_t *sin6 = (sin6_t *)sin;
11326 	struct lifreq *lifr = (struct lifreq *)if_req;
11327 	int addrlen;
11328 
11329 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11330 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11331 	ASSERT(IAM_WRITER_IPIF(ipif));
11332 
11333 	addrlen = lifr->lifr_addrlen;
11334 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11335 	if (ipif->ipif_id != 0)
11336 		return (EINVAL);
11337 
11338 	if (!ipif->ipif_isv6)
11339 		return (EINVAL);
11340 
11341 	if (addrlen > IPV6_ABITS)
11342 		return (EINVAL);
11343 
11344 	v6addr = sin6->sin6_addr;
11345 
11346 	/*
11347 	 * The length of the token is the length from the end.  To get
11348 	 * the proper mask for this, compute the mask of the bits not
11349 	 * in the token; ie. the prefix, and then xor to get the mask.
11350 	 */
11351 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11352 		return (EINVAL);
11353 	for (i = 0; i < 4; i++) {
11354 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11355 	}
11356 
11357 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11358 	    ill->ill_token_length == addrlen)
11359 		return (0);	/* No change */
11360 
11361 	if (ipif->ipif_flags & IPIF_UP) {
11362 		err = ipif_logical_down(ipif, q, mp);
11363 		if (err == EINPROGRESS)
11364 			return (err);
11365 		(void) ipif_down_tail(ipif);
11366 		need_up = B_TRUE;
11367 	}
11368 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11369 	return (err);
11370 }
11371 
11372 static int
11373 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11374     mblk_t *mp, boolean_t need_up)
11375 {
11376 	in6_addr_t v6addr;
11377 	in6_addr_t v6mask;
11378 	ill_t	*ill = ipif->ipif_ill;
11379 	int	i;
11380 	int	err = 0;
11381 
11382 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11383 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11384 	v6addr = sin6->sin6_addr;
11385 	/*
11386 	 * The length of the token is the length from the end.  To get
11387 	 * the proper mask for this, compute the mask of the bits not
11388 	 * in the token; ie. the prefix, and then xor to get the mask.
11389 	 */
11390 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11391 	for (i = 0; i < 4; i++)
11392 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11393 
11394 	mutex_enter(&ill->ill_lock);
11395 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11396 	ill->ill_token_length = addrlen;
11397 	ill->ill_manual_token = 1;
11398 
11399 	/* Reconfigure the link-local address based on this new token */
11400 	ipif_setlinklocal(ill->ill_ipif);
11401 
11402 	mutex_exit(&ill->ill_lock);
11403 
11404 	if (need_up) {
11405 		/*
11406 		 * Now bring the interface back up.  If this
11407 		 * is the only IPIF for the ILL, ipif_up
11408 		 * will have to re-bind to the device, so
11409 		 * we may get back EINPROGRESS, in which
11410 		 * case, this IOCTL will get completed in
11411 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11412 		 */
11413 		err = ipif_up(ipif, q, mp);
11414 		if (err == EINPROGRESS)
11415 			return (err);
11416 	}
11417 	return (err);
11418 }
11419 
11420 /* ARGSUSED */
11421 int
11422 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11423     ip_ioctl_cmd_t *ipi, void *if_req)
11424 {
11425 	ill_t *ill;
11426 	sin6_t *sin6 = (sin6_t *)sin;
11427 	struct lifreq *lifr = (struct lifreq *)if_req;
11428 
11429 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11430 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11431 	if (ipif->ipif_id != 0)
11432 		return (EINVAL);
11433 
11434 	ill = ipif->ipif_ill;
11435 	if (!ill->ill_isv6)
11436 		return (ENXIO);
11437 
11438 	*sin6 = sin6_null;
11439 	sin6->sin6_family = AF_INET6;
11440 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11441 	sin6->sin6_addr = ill->ill_token;
11442 	lifr->lifr_addrlen = ill->ill_token_length;
11443 	return (0);
11444 }
11445 
11446 /*
11447  * Set (hardware) link specific information that might override
11448  * what was acquired through the DL_INFO_ACK.
11449  */
11450 /* ARGSUSED */
11451 int
11452 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11453     ip_ioctl_cmd_t *ipi, void *if_req)
11454 {
11455 	ill_t		*ill = ipif->ipif_ill;
11456 	int		ip_min_mtu;
11457 	struct lifreq	*lifr = (struct lifreq *)if_req;
11458 	lif_ifinfo_req_t *lir;
11459 
11460 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11461 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11462 	lir = &lifr->lifr_ifinfo;
11463 	ASSERT(IAM_WRITER_IPIF(ipif));
11464 
11465 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11466 	if (ipif->ipif_id != 0)
11467 		return (EINVAL);
11468 
11469 	/* Set interface MTU. */
11470 	if (ipif->ipif_isv6)
11471 		ip_min_mtu = IPV6_MIN_MTU;
11472 	else
11473 		ip_min_mtu = IP_MIN_MTU;
11474 
11475 	/*
11476 	 * Verify values before we set anything. Allow zero to
11477 	 * mean unspecified.
11478 	 *
11479 	 * XXX We should be able to set the user-defined lir_mtu to some value
11480 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11481 	 * ill_max_frag value tells us the max MTU that can be handled by the
11482 	 * datalink, whereas the ill_current_frag is dynamically computed for
11483 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11484 	 * since there is currently no way of distinguishing between
11485 	 * administratively fixed link mtu values (e.g., those set via
11486 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11487 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11488 	 * upper-bound.
11489 	 */
11490 	if (lir->lir_maxmtu != 0 &&
11491 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11492 	    lir->lir_maxmtu < ip_min_mtu))
11493 		return (EINVAL);
11494 	if (lir->lir_reachtime != 0 &&
11495 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11496 		return (EINVAL);
11497 	if (lir->lir_reachretrans != 0 &&
11498 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11499 		return (EINVAL);
11500 
11501 	mutex_enter(&ill->ill_lock);
11502 	/*
11503 	 * The dce and fragmentation code can handle changes to ill_mtu
11504 	 * concurrent with sending/fragmenting packets.
11505 	 */
11506 	if (lir->lir_maxmtu != 0)
11507 		ill->ill_user_mtu = lir->lir_maxmtu;
11508 
11509 	if (lir->lir_reachtime != 0)
11510 		ill->ill_reachable_time = lir->lir_reachtime;
11511 
11512 	if (lir->lir_reachretrans != 0)
11513 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11514 
11515 	ill->ill_max_hops = lir->lir_maxhops;
11516 	ill->ill_max_buf = ND_MAX_Q;
11517 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11518 		/*
11519 		 * ill_mtu is the actual interface MTU, obtained as the min
11520 		 * of user-configured mtu and the value announced by the
11521 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11522 		 * we have already made the choice of requiring
11523 		 * ill_user_mtu < ill_current_frag by the time we get here,
11524 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11525 		 * here.
11526 		 */
11527 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11528 	}
11529 	mutex_exit(&ill->ill_lock);
11530 
11531 	/*
11532 	 * Make sure all dce_generation checks find out
11533 	 * that ill_mtu has changed.
11534 	 */
11535 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11536 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11537 
11538 	/*
11539 	 * Refresh IPMP meta-interface MTU if necessary.
11540 	 */
11541 	if (IS_UNDER_IPMP(ill))
11542 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11543 
11544 	return (0);
11545 }
11546 
11547 /* ARGSUSED */
11548 int
11549 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11550     ip_ioctl_cmd_t *ipi, void *if_req)
11551 {
11552 	struct lif_ifinfo_req *lir;
11553 	ill_t *ill = ipif->ipif_ill;
11554 
11555 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11556 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11557 	if (ipif->ipif_id != 0)
11558 		return (EINVAL);
11559 
11560 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11561 	lir->lir_maxhops = ill->ill_max_hops;
11562 	lir->lir_reachtime = ill->ill_reachable_time;
11563 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11564 	lir->lir_maxmtu = ill->ill_mtu;
11565 
11566 	return (0);
11567 }
11568 
11569 /*
11570  * Return best guess as to the subnet mask for the specified address.
11571  * Based on the subnet masks for all the configured interfaces.
11572  *
11573  * We end up returning a zero mask in the case of default, multicast or
11574  * experimental.
11575  */
11576 static ipaddr_t
11577 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11578 {
11579 	ipaddr_t net_mask;
11580 	ill_t	*ill;
11581 	ipif_t	*ipif;
11582 	ill_walk_context_t ctx;
11583 	ipif_t	*fallback_ipif = NULL;
11584 
11585 	net_mask = ip_net_mask(addr);
11586 	if (net_mask == 0) {
11587 		*ipifp = NULL;
11588 		return (0);
11589 	}
11590 
11591 	/* Let's check to see if this is maybe a local subnet route. */
11592 	/* this function only applies to IPv4 interfaces */
11593 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11594 	ill = ILL_START_WALK_V4(&ctx, ipst);
11595 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11596 		mutex_enter(&ill->ill_lock);
11597 		for (ipif = ill->ill_ipif; ipif != NULL;
11598 		    ipif = ipif->ipif_next) {
11599 			if (IPIF_IS_CONDEMNED(ipif))
11600 				continue;
11601 			if (!(ipif->ipif_flags & IPIF_UP))
11602 				continue;
11603 			if ((ipif->ipif_subnet & net_mask) ==
11604 			    (addr & net_mask)) {
11605 				/*
11606 				 * Don't trust pt-pt interfaces if there are
11607 				 * other interfaces.
11608 				 */
11609 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11610 					if (fallback_ipif == NULL) {
11611 						ipif_refhold_locked(ipif);
11612 						fallback_ipif = ipif;
11613 					}
11614 					continue;
11615 				}
11616 
11617 				/*
11618 				 * Fine. Just assume the same net mask as the
11619 				 * directly attached subnet interface is using.
11620 				 */
11621 				ipif_refhold_locked(ipif);
11622 				mutex_exit(&ill->ill_lock);
11623 				rw_exit(&ipst->ips_ill_g_lock);
11624 				if (fallback_ipif != NULL)
11625 					ipif_refrele(fallback_ipif);
11626 				*ipifp = ipif;
11627 				return (ipif->ipif_net_mask);
11628 			}
11629 		}
11630 		mutex_exit(&ill->ill_lock);
11631 	}
11632 	rw_exit(&ipst->ips_ill_g_lock);
11633 
11634 	*ipifp = fallback_ipif;
11635 	return ((fallback_ipif != NULL) ?
11636 	    fallback_ipif->ipif_net_mask : net_mask);
11637 }
11638 
11639 /*
11640  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11641  */
11642 static void
11643 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11644 {
11645 	IOCP	iocp;
11646 	ipft_t	*ipft;
11647 	ipllc_t	*ipllc;
11648 	mblk_t	*mp1;
11649 	cred_t	*cr;
11650 	int	error = 0;
11651 	conn_t	*connp;
11652 
11653 	ip1dbg(("ip_wput_ioctl"));
11654 	iocp = (IOCP)mp->b_rptr;
11655 	mp1 = mp->b_cont;
11656 	if (mp1 == NULL) {
11657 		iocp->ioc_error = EINVAL;
11658 		mp->b_datap->db_type = M_IOCNAK;
11659 		iocp->ioc_count = 0;
11660 		qreply(q, mp);
11661 		return;
11662 	}
11663 
11664 	/*
11665 	 * These IOCTLs provide various control capabilities to
11666 	 * upstream agents such as ULPs and processes.	There
11667 	 * are currently two such IOCTLs implemented.  They
11668 	 * are used by TCP to provide update information for
11669 	 * existing IREs and to forcibly delete an IRE for a
11670 	 * host that is not responding, thereby forcing an
11671 	 * attempt at a new route.
11672 	 */
11673 	iocp->ioc_error = EINVAL;
11674 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11675 		goto done;
11676 
11677 	ipllc = (ipllc_t *)mp1->b_rptr;
11678 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11679 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11680 			break;
11681 	}
11682 	/*
11683 	 * prefer credential from mblk over ioctl;
11684 	 * see ip_sioctl_copyin_setup
11685 	 */
11686 	cr = msg_getcred(mp, NULL);
11687 	if (cr == NULL)
11688 		cr = iocp->ioc_cr;
11689 
11690 	/*
11691 	 * Refhold the conn in case the request gets queued up in some lookup
11692 	 */
11693 	ASSERT(CONN_Q(q));
11694 	connp = Q_TO_CONN(q);
11695 	CONN_INC_REF(connp);
11696 	CONN_INC_IOCTLREF(connp);
11697 	if (ipft->ipft_pfi &&
11698 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11699 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11700 		error = (*ipft->ipft_pfi)(q,
11701 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11702 	}
11703 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11704 		/*
11705 		 * CONN_OPER_PENDING_DONE happens in the function called
11706 		 * through ipft_pfi above.
11707 		 */
11708 		return;
11709 	}
11710 
11711 	CONN_DEC_IOCTLREF(connp);
11712 	CONN_OPER_PENDING_DONE(connp);
11713 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11714 		freemsg(mp);
11715 		return;
11716 	}
11717 	iocp->ioc_error = error;
11718 
11719 done:
11720 	mp->b_datap->db_type = M_IOCACK;
11721 	if (iocp->ioc_error)
11722 		iocp->ioc_count = 0;
11723 	qreply(q, mp);
11724 }
11725 
11726 /*
11727  * Assign a unique id for the ipif. This is used by sctp_addr.c
11728  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11729  */
11730 static void
11731 ipif_assign_seqid(ipif_t *ipif)
11732 {
11733 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11734 
11735 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11736 }
11737 
11738 /*
11739  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11740  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11741  * that the clone is complete -- including the seqid -- and the expectation is
11742  * that the caller will either free or overwrite `sipif' before it's unlocked.
11743  */
11744 static void
11745 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11746 {
11747 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11748 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11749 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11750 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11751 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11752 
11753 	dipif->ipif_flags = sipif->ipif_flags;
11754 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11755 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11756 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11757 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11758 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11759 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11760 
11761 	/*
11762 	 * As per the comment atop the function, we assume that these sipif
11763 	 * fields will be changed before sipif is unlocked.
11764 	 */
11765 	dipif->ipif_seqid = sipif->ipif_seqid;
11766 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11767 }
11768 
11769 /*
11770  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11771  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11772  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11773  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11774  * down (i.e., no DAD), of the same type, and unlocked.
11775  */
11776 static void
11777 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11778 {
11779 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11780 	ipxop_t *ipx = ipsq->ipsq_xop;
11781 
11782 	ASSERT(sipif != dipif);
11783 	ASSERT(sipif != virgipif);
11784 
11785 	/*
11786 	 * Grab all of the locks that protect the ipif in a defined order.
11787 	 */
11788 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11789 
11790 	ipif_clone(sipif, dipif);
11791 	if (virgipif != NULL) {
11792 		ipif_clone(virgipif, sipif);
11793 		mi_free(virgipif);
11794 	}
11795 
11796 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11797 
11798 	/*
11799 	 * Transfer ownership of the current xop, if necessary.
11800 	 */
11801 	if (ipx->ipx_current_ipif == sipif) {
11802 		ASSERT(ipx->ipx_pending_ipif == NULL);
11803 		mutex_enter(&ipx->ipx_lock);
11804 		ipx->ipx_current_ipif = dipif;
11805 		mutex_exit(&ipx->ipx_lock);
11806 	}
11807 
11808 	if (virgipif == NULL)
11809 		mi_free(sipif);
11810 }
11811 
11812 /*
11813  * checks if:
11814  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11815  *	- logical interface is within the allowed range
11816  */
11817 static int
11818 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11819 {
11820 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11821 		return (ENAMETOOLONG);
11822 
11823 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11824 		return (ERANGE);
11825 	return (0);
11826 }
11827 
11828 /*
11829  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11830  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11831  * be inserted into the first space available in the list. The value of
11832  * ipif_id will then be set to the appropriate value for its position.
11833  */
11834 static int
11835 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11836 {
11837 	ill_t *ill;
11838 	ipif_t *tipif;
11839 	ipif_t **tipifp;
11840 	int id, err;
11841 	ip_stack_t	*ipst;
11842 
11843 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11844 	    IAM_WRITER_IPIF(ipif));
11845 
11846 	ill = ipif->ipif_ill;
11847 	ASSERT(ill != NULL);
11848 	ipst = ill->ill_ipst;
11849 
11850 	/*
11851 	 * In the case of lo0:0 we already hold the ill_g_lock.
11852 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11853 	 * ipif_insert.
11854 	 */
11855 	if (acquire_g_lock)
11856 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11857 	mutex_enter(&ill->ill_lock);
11858 	id = ipif->ipif_id;
11859 	tipifp = &(ill->ill_ipif);
11860 	if (id == -1) {	/* need to find a real id */
11861 		id = 0;
11862 		while ((tipif = *tipifp) != NULL) {
11863 			ASSERT(tipif->ipif_id >= id);
11864 			if (tipif->ipif_id != id)
11865 				break; /* non-consecutive id */
11866 			id++;
11867 			tipifp = &(tipif->ipif_next);
11868 		}
11869 		if ((err = is_lifname_valid(ill, id)) != 0) {
11870 			mutex_exit(&ill->ill_lock);
11871 			if (acquire_g_lock)
11872 				rw_exit(&ipst->ips_ill_g_lock);
11873 			return (err);
11874 		}
11875 		ipif->ipif_id = id; /* assign new id */
11876 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11877 		/* we have a real id; insert ipif in the right place */
11878 		while ((tipif = *tipifp) != NULL) {
11879 			ASSERT(tipif->ipif_id != id);
11880 			if (tipif->ipif_id > id)
11881 				break; /* found correct location */
11882 			tipifp = &(tipif->ipif_next);
11883 		}
11884 	} else {
11885 		mutex_exit(&ill->ill_lock);
11886 		if (acquire_g_lock)
11887 			rw_exit(&ipst->ips_ill_g_lock);
11888 		return (err);
11889 	}
11890 
11891 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11892 
11893 	ipif->ipif_next = tipif;
11894 	*tipifp = ipif;
11895 	mutex_exit(&ill->ill_lock);
11896 	if (acquire_g_lock)
11897 		rw_exit(&ipst->ips_ill_g_lock);
11898 
11899 	return (0);
11900 }
11901 
11902 static void
11903 ipif_remove(ipif_t *ipif)
11904 {
11905 	ipif_t	**ipifp;
11906 	ill_t	*ill = ipif->ipif_ill;
11907 
11908 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11909 
11910 	mutex_enter(&ill->ill_lock);
11911 	ipifp = &ill->ill_ipif;
11912 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11913 		if (*ipifp == ipif) {
11914 			*ipifp = ipif->ipif_next;
11915 			break;
11916 		}
11917 	}
11918 	mutex_exit(&ill->ill_lock);
11919 }
11920 
11921 /*
11922  * Allocate and initialize a new interface control structure.  (Always
11923  * called as writer.)
11924  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11925  * is not part of the global linked list of ills. ipif_seqid is unique
11926  * in the system and to preserve the uniqueness, it is assigned only
11927  * when ill becomes part of the global list. At that point ill will
11928  * have a name. If it doesn't get assigned here, it will get assigned
11929  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11930  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11931  * the interface flags or any other information from the DL_INFO_ACK for
11932  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11933  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11934  * second DL_INFO_ACK comes in from the driver.
11935  */
11936 static ipif_t *
11937 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11938     boolean_t insert, int *errorp)
11939 {
11940 	int err;
11941 	ipif_t	*ipif;
11942 	ip_stack_t *ipst = ill->ill_ipst;
11943 
11944 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11945 	    ill->ill_name, id, (void *)ill));
11946 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11947 
11948 	if (errorp != NULL)
11949 		*errorp = 0;
11950 
11951 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11952 		if (errorp != NULL)
11953 			*errorp = ENOMEM;
11954 		return (NULL);
11955 	}
11956 	*ipif = ipif_zero;	/* start clean */
11957 
11958 	ipif->ipif_ill = ill;
11959 	ipif->ipif_id = id;	/* could be -1 */
11960 	/*
11961 	 * Inherit the zoneid from the ill; for the shared stack instance
11962 	 * this is always the global zone
11963 	 */
11964 	ipif->ipif_zoneid = ill->ill_zoneid;
11965 
11966 	ipif->ipif_refcnt = 0;
11967 
11968 	if (insert) {
11969 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11970 			mi_free(ipif);
11971 			if (errorp != NULL)
11972 				*errorp = err;
11973 			return (NULL);
11974 		}
11975 		/* -1 id should have been replaced by real id */
11976 		id = ipif->ipif_id;
11977 		ASSERT(id >= 0);
11978 	}
11979 
11980 	if (ill->ill_name[0] != '\0')
11981 		ipif_assign_seqid(ipif);
11982 
11983 	/*
11984 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11985 	 * (which must not exist yet because the zeroth ipif is created once
11986 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11987 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11988 	 */
11989 	if (id == 0 && IS_IPMP(ill)) {
11990 		if (ipmp_illgrp_create(ill) == NULL) {
11991 			if (insert) {
11992 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11993 				ipif_remove(ipif);
11994 				rw_exit(&ipst->ips_ill_g_lock);
11995 			}
11996 			mi_free(ipif);
11997 			if (errorp != NULL)
11998 				*errorp = ENOMEM;
11999 			return (NULL);
12000 		}
12001 	}
12002 
12003 	/*
12004 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12005 	 * not up and can't be looked up until the ioctl completes and the
12006 	 * IPIF_CHANGING flag is cleared.
12007 	 */
12008 	mutex_enter(&ill->ill_lock);
12009 
12010 	ipif->ipif_ire_type = ire_type;
12011 
12012 	if (ipif->ipif_isv6) {
12013 		ill->ill_flags |= ILLF_IPV6;
12014 	} else {
12015 		ipaddr_t inaddr_any = INADDR_ANY;
12016 
12017 		ill->ill_flags |= ILLF_IPV4;
12018 
12019 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12020 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12021 		    &ipif->ipif_v6lcl_addr);
12022 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12023 		    &ipif->ipif_v6subnet);
12024 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12025 		    &ipif->ipif_v6net_mask);
12026 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12027 		    &ipif->ipif_v6brd_addr);
12028 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12029 		    &ipif->ipif_v6pp_dst_addr);
12030 	}
12031 
12032 	/*
12033 	 * Don't set the interface flags etc. now, will do it in
12034 	 * ip_ll_subnet_defaults.
12035 	 */
12036 	if (!initialize)
12037 		goto out;
12038 
12039 	/*
12040 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12041 	 * with no underlying interfaces (and thus an unknown broadcast
12042 	 * address length), but all interfaces that can be placed into an IPMP
12043 	 * group are required to be broadcast-capable.
12044 	 */
12045 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12046 		/*
12047 		 * Later detect lack of DLPI driver multicast capability by
12048 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12049 		 */
12050 		ill->ill_flags |= ILLF_MULTICAST;
12051 		if (!ipif->ipif_isv6)
12052 			ipif->ipif_flags |= IPIF_BROADCAST;
12053 	} else {
12054 		if (ill->ill_net_type != IRE_LOOPBACK) {
12055 			if (ipif->ipif_isv6)
12056 				/*
12057 				 * Note: xresolv interfaces will eventually need
12058 				 * NOARP set here as well, but that will require
12059 				 * those external resolvers to have some
12060 				 * knowledge of that flag and act appropriately.
12061 				 * Not to be changed at present.
12062 				 */
12063 				ill->ill_flags |= ILLF_NONUD;
12064 			else
12065 				ill->ill_flags |= ILLF_NOARP;
12066 		}
12067 		if (ill->ill_phys_addr_length == 0) {
12068 			if (IS_VNI(ill)) {
12069 				ipif->ipif_flags |= IPIF_NOXMIT;
12070 			} else {
12071 				/* pt-pt supports multicast. */
12072 				ill->ill_flags |= ILLF_MULTICAST;
12073 				if (ill->ill_net_type != IRE_LOOPBACK)
12074 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12075 			}
12076 		}
12077 	}
12078 out:
12079 	mutex_exit(&ill->ill_lock);
12080 	return (ipif);
12081 }
12082 
12083 /*
12084  * Remove the neighbor cache entries associated with this logical
12085  * interface.
12086  */
12087 int
12088 ipif_arp_down(ipif_t *ipif)
12089 {
12090 	ill_t	*ill = ipif->ipif_ill;
12091 	int	err = 0;
12092 
12093 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12094 	ASSERT(IAM_WRITER_IPIF(ipif));
12095 
12096 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12097 	    ill_t *, ill, ipif_t *, ipif);
12098 	ipif_nce_down(ipif);
12099 
12100 	/*
12101 	 * If this is the last ipif that is going down and there are no
12102 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12103 	 * clean up ARP completely.
12104 	 */
12105 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12106 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12107 		/*
12108 		 * If this was the last ipif on an IPMP interface, purge any
12109 		 * static ARP entries associated with it.
12110 		 */
12111 		if (IS_IPMP(ill))
12112 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12113 
12114 		/* UNBIND, DETACH */
12115 		err = arp_ll_down(ill);
12116 	}
12117 
12118 	return (err);
12119 }
12120 
12121 /*
12122  * Get the resolver set up for a new IP address.  (Always called as writer.)
12123  * Called both for IPv4 and IPv6 interfaces, though it only does some
12124  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12125  *
12126  * The enumerated value res_act tunes the behavior:
12127  * 	* Res_act_initial: set up all the resolver structures for a new
12128  *	  IP address.
12129  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12130  *	  ARP message in defense of the address.
12131  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12132  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12133  *
12134  * Returns zero on success, or an errno upon failure.
12135  */
12136 int
12137 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12138 {
12139 	ill_t		*ill = ipif->ipif_ill;
12140 	int		err;
12141 	boolean_t	was_dup;
12142 
12143 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12144 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12145 	ASSERT(IAM_WRITER_IPIF(ipif));
12146 
12147 	was_dup = B_FALSE;
12148 	if (res_act == Res_act_initial) {
12149 		ipif->ipif_addr_ready = 0;
12150 		/*
12151 		 * We're bringing an interface up here.  There's no way that we
12152 		 * should need to shut down ARP now.
12153 		 */
12154 		mutex_enter(&ill->ill_lock);
12155 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12156 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12157 			ill->ill_ipif_dup_count--;
12158 			was_dup = B_TRUE;
12159 		}
12160 		mutex_exit(&ill->ill_lock);
12161 	}
12162 	if (ipif->ipif_recovery_id != 0)
12163 		(void) untimeout(ipif->ipif_recovery_id);
12164 	ipif->ipif_recovery_id = 0;
12165 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12166 		ipif->ipif_addr_ready = 1;
12167 		return (0);
12168 	}
12169 	/* NDP will set the ipif_addr_ready flag when it's ready */
12170 	if (ill->ill_isv6)
12171 		return (0);
12172 
12173 	err = ipif_arp_up(ipif, res_act, was_dup);
12174 	return (err);
12175 }
12176 
12177 /*
12178  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12179  * when a link has just gone back up.
12180  */
12181 static void
12182 ipif_nce_start_dad(ipif_t *ipif)
12183 {
12184 	ncec_t *ncec;
12185 	ill_t *ill = ipif->ipif_ill;
12186 	boolean_t isv6 = ill->ill_isv6;
12187 
12188 	if (isv6) {
12189 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12190 		    &ipif->ipif_v6lcl_addr);
12191 	} else {
12192 		ipaddr_t v4addr;
12193 
12194 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12195 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12196 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12197 			/*
12198 			 * If we can't contact ARP for some reason,
12199 			 * that's not really a problem.  Just send
12200 			 * out the routing socket notification that
12201 			 * DAD completion would have done, and continue.
12202 			 */
12203 			ipif_mask_reply(ipif);
12204 			ipif_up_notify(ipif);
12205 			ipif->ipif_addr_ready = 1;
12206 			return;
12207 		}
12208 
12209 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12210 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12211 	}
12212 
12213 	if (ncec == NULL) {
12214 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12215 		    (void *)ipif));
12216 		return;
12217 	}
12218 	if (!nce_restart_dad(ncec)) {
12219 		/*
12220 		 * If we can't restart DAD for some reason, that's not really a
12221 		 * problem.  Just send out the routing socket notification that
12222 		 * DAD completion would have done, and continue.
12223 		 */
12224 		ipif_up_notify(ipif);
12225 		ipif->ipif_addr_ready = 1;
12226 	}
12227 	ncec_refrele(ncec);
12228 }
12229 
12230 /*
12231  * Restart duplicate address detection on all interfaces on the given ill.
12232  *
12233  * This is called when an interface transitions from down to up
12234  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12235  *
12236  * Note that since the underlying physical link has transitioned, we must cause
12237  * at least one routing socket message to be sent here, either via DAD
12238  * completion or just by default on the first ipif.  (If we don't do this, then
12239  * in.mpathd will see long delays when doing link-based failure recovery.)
12240  */
12241 void
12242 ill_restart_dad(ill_t *ill, boolean_t went_up)
12243 {
12244 	ipif_t *ipif;
12245 
12246 	if (ill == NULL)
12247 		return;
12248 
12249 	/*
12250 	 * If layer two doesn't support duplicate address detection, then just
12251 	 * send the routing socket message now and be done with it.
12252 	 */
12253 	if (!ill->ill_isv6 && arp_no_defense) {
12254 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12255 		return;
12256 	}
12257 
12258 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12259 		if (went_up) {
12260 
12261 			if (ipif->ipif_flags & IPIF_UP) {
12262 				ipif_nce_start_dad(ipif);
12263 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12264 				/*
12265 				 * kick off the bring-up process now.
12266 				 */
12267 				ipif_do_recovery(ipif);
12268 			} else {
12269 				/*
12270 				 * Unfortunately, the first ipif is "special"
12271 				 * and represents the underlying ill in the
12272 				 * routing socket messages.  Thus, when this
12273 				 * one ipif is down, we must still notify so
12274 				 * that the user knows the IFF_RUNNING status
12275 				 * change.  (If the first ipif is up, then
12276 				 * we'll handle eventual routing socket
12277 				 * notification via DAD completion.)
12278 				 */
12279 				if (ipif == ill->ill_ipif) {
12280 					ip_rts_ifmsg(ill->ill_ipif,
12281 					    RTSQ_DEFAULT);
12282 				}
12283 			}
12284 		} else {
12285 			/*
12286 			 * After link down, we'll need to send a new routing
12287 			 * message when the link comes back, so clear
12288 			 * ipif_addr_ready.
12289 			 */
12290 			ipif->ipif_addr_ready = 0;
12291 		}
12292 	}
12293 
12294 	/*
12295 	 * If we've torn down links, then notify the user right away.
12296 	 */
12297 	if (!went_up)
12298 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12299 }
12300 
12301 static void
12302 ipsq_delete(ipsq_t *ipsq)
12303 {
12304 	ipxop_t *ipx = ipsq->ipsq_xop;
12305 
12306 	ipsq->ipsq_ipst = NULL;
12307 	ASSERT(ipsq->ipsq_phyint == NULL);
12308 	ASSERT(ipsq->ipsq_xop != NULL);
12309 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12310 	ASSERT(ipx->ipx_pending_mp == NULL);
12311 	kmem_free(ipsq, sizeof (ipsq_t));
12312 }
12313 
12314 static int
12315 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12316 {
12317 	int err = 0;
12318 	ipif_t *ipif;
12319 
12320 	if (ill == NULL)
12321 		return (0);
12322 
12323 	ASSERT(IAM_WRITER_ILL(ill));
12324 	ill->ill_up_ipifs = B_TRUE;
12325 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12326 		if (ipif->ipif_was_up) {
12327 			if (!(ipif->ipif_flags & IPIF_UP))
12328 				err = ipif_up(ipif, q, mp);
12329 			ipif->ipif_was_up = B_FALSE;
12330 			if (err != 0) {
12331 				ASSERT(err == EINPROGRESS);
12332 				return (err);
12333 			}
12334 		}
12335 	}
12336 	ill->ill_up_ipifs = B_FALSE;
12337 	return (0);
12338 }
12339 
12340 /*
12341  * This function is called to bring up all the ipifs that were up before
12342  * bringing the ill down via ill_down_ipifs().
12343  */
12344 int
12345 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12346 {
12347 	int err;
12348 
12349 	ASSERT(IAM_WRITER_ILL(ill));
12350 
12351 	if (ill->ill_replumbing) {
12352 		ill->ill_replumbing = 0;
12353 		/*
12354 		 * Send down REPLUMB_DONE notification followed by the
12355 		 * BIND_REQ on the arp stream.
12356 		 */
12357 		if (!ill->ill_isv6)
12358 			arp_send_replumb_conf(ill);
12359 	}
12360 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12361 	if (err != 0)
12362 		return (err);
12363 
12364 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12365 }
12366 
12367 /*
12368  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12369  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12370  */
12371 static void
12372 ill_down_ipifs(ill_t *ill, boolean_t logical)
12373 {
12374 	ipif_t *ipif;
12375 
12376 	ASSERT(IAM_WRITER_ILL(ill));
12377 
12378 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12379 		/*
12380 		 * We go through the ipif_down logic even if the ipif
12381 		 * is already down, since routes can be added based
12382 		 * on down ipifs. Going through ipif_down once again
12383 		 * will delete any IREs created based on these routes.
12384 		 */
12385 		if (ipif->ipif_flags & IPIF_UP)
12386 			ipif->ipif_was_up = B_TRUE;
12387 
12388 		if (logical) {
12389 			(void) ipif_logical_down(ipif, NULL, NULL);
12390 			ipif_non_duplicate(ipif);
12391 			(void) ipif_down_tail(ipif);
12392 		} else {
12393 			(void) ipif_down(ipif, NULL, NULL);
12394 		}
12395 	}
12396 }
12397 
12398 /*
12399  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12400  * a look again at valid source addresses.
12401  * This should be called each time after the set of source addresses has been
12402  * changed.
12403  */
12404 void
12405 ip_update_source_selection(ip_stack_t *ipst)
12406 {
12407 	/* We skip past SRC_GENERATION_VERIFY */
12408 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12409 	    SRC_GENERATION_VERIFY)
12410 		atomic_add_32(&ipst->ips_src_generation, 1);
12411 }
12412 
12413 /*
12414  * Finish the group join started in ip_sioctl_groupname().
12415  */
12416 /* ARGSUSED */
12417 static void
12418 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12419 {
12420 	ill_t		*ill = q->q_ptr;
12421 	phyint_t	*phyi = ill->ill_phyint;
12422 	ipmp_grp_t	*grp = phyi->phyint_grp;
12423 	ip_stack_t	*ipst = ill->ill_ipst;
12424 
12425 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12426 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12427 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12428 
12429 	if (phyi->phyint_illv4 != NULL) {
12430 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12431 		VERIFY(grp->gr_pendv4-- > 0);
12432 		rw_exit(&ipst->ips_ipmp_lock);
12433 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12434 	}
12435 	if (phyi->phyint_illv6 != NULL) {
12436 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12437 		VERIFY(grp->gr_pendv6-- > 0);
12438 		rw_exit(&ipst->ips_ipmp_lock);
12439 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12440 	}
12441 	freemsg(mp);
12442 }
12443 
12444 /*
12445  * Process an SIOCSLIFGROUPNAME request.
12446  */
12447 /* ARGSUSED */
12448 int
12449 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12450     ip_ioctl_cmd_t *ipip, void *ifreq)
12451 {
12452 	struct lifreq	*lifr = ifreq;
12453 	ill_t		*ill = ipif->ipif_ill;
12454 	ip_stack_t	*ipst = ill->ill_ipst;
12455 	phyint_t	*phyi = ill->ill_phyint;
12456 	ipmp_grp_t	*grp = phyi->phyint_grp;
12457 	mblk_t		*ipsq_mp;
12458 	int		err = 0;
12459 
12460 	/*
12461 	 * Note that phyint_grp can only change here, where we're exclusive.
12462 	 */
12463 	ASSERT(IAM_WRITER_ILL(ill));
12464 
12465 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12466 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12467 		return (EINVAL);
12468 
12469 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12470 
12471 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12472 
12473 	/*
12474 	 * If the name hasn't changed, there's nothing to do.
12475 	 */
12476 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12477 		goto unlock;
12478 
12479 	/*
12480 	 * Handle requests to rename an IPMP meta-interface.
12481 	 *
12482 	 * Note that creation of the IPMP meta-interface is handled in
12483 	 * userland through the standard plumbing sequence.  As part of the
12484 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12485 	 * the name of the interface (see ipif_set_values_tail()).
12486 	 */
12487 	if (IS_IPMP(ill)) {
12488 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12489 		goto unlock;
12490 	}
12491 
12492 	/*
12493 	 * Handle requests to add or remove an IP interface from a group.
12494 	 */
12495 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12496 		/*
12497 		 * Moves are handled by first removing the interface from
12498 		 * its existing group, and then adding it to another group.
12499 		 * So, fail if it's already in a group.
12500 		 */
12501 		if (IS_UNDER_IPMP(ill)) {
12502 			err = EALREADY;
12503 			goto unlock;
12504 		}
12505 
12506 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12507 		if (grp == NULL) {
12508 			err = ENOENT;
12509 			goto unlock;
12510 		}
12511 
12512 		/*
12513 		 * Check if the phyint and its ills are suitable for
12514 		 * inclusion into the group.
12515 		 */
12516 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12517 			goto unlock;
12518 
12519 		/*
12520 		 * Checks pass; join the group, and enqueue the remaining
12521 		 * illgrp joins for when we've become part of the group xop
12522 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12523 		 * requires an mblk_t to scribble on, and since `mp' will be
12524 		 * freed as part of completing the ioctl, allocate another.
12525 		 */
12526 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12527 			err = ENOMEM;
12528 			goto unlock;
12529 		}
12530 
12531 		/*
12532 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12533 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12534 		 * before ip_join_illgrps() is called back.  See the comments
12535 		 * in ip_sioctl_plink_ipmp() for more.
12536 		 */
12537 		if (phyi->phyint_illv4 != NULL)
12538 			grp->gr_pendv4++;
12539 		if (phyi->phyint_illv6 != NULL)
12540 			grp->gr_pendv6++;
12541 
12542 		rw_exit(&ipst->ips_ipmp_lock);
12543 
12544 		ipmp_phyint_join_grp(phyi, grp);
12545 		ill_refhold(ill);
12546 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12547 		    SWITCH_OP, B_FALSE);
12548 		return (0);
12549 	} else {
12550 		/*
12551 		 * Request to remove the interface from a group.  If the
12552 		 * interface is not in a group, this trivially succeeds.
12553 		 */
12554 		rw_exit(&ipst->ips_ipmp_lock);
12555 		if (IS_UNDER_IPMP(ill))
12556 			ipmp_phyint_leave_grp(phyi);
12557 		return (0);
12558 	}
12559 unlock:
12560 	rw_exit(&ipst->ips_ipmp_lock);
12561 	return (err);
12562 }
12563 
12564 /*
12565  * Process an SIOCGLIFBINDING request.
12566  */
12567 /* ARGSUSED */
12568 int
12569 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12570     ip_ioctl_cmd_t *ipip, void *ifreq)
12571 {
12572 	ill_t		*ill;
12573 	struct lifreq	*lifr = ifreq;
12574 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12575 
12576 	if (!IS_IPMP(ipif->ipif_ill))
12577 		return (EINVAL);
12578 
12579 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12580 	if ((ill = ipif->ipif_bound_ill) == NULL)
12581 		lifr->lifr_binding[0] = '\0';
12582 	else
12583 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12584 	rw_exit(&ipst->ips_ipmp_lock);
12585 	return (0);
12586 }
12587 
12588 /*
12589  * Process an SIOCGLIFGROUPNAME request.
12590  */
12591 /* ARGSUSED */
12592 int
12593 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12594     ip_ioctl_cmd_t *ipip, void *ifreq)
12595 {
12596 	ipmp_grp_t	*grp;
12597 	struct lifreq	*lifr = ifreq;
12598 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12599 
12600 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12601 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12602 		lifr->lifr_groupname[0] = '\0';
12603 	else
12604 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12605 	rw_exit(&ipst->ips_ipmp_lock);
12606 	return (0);
12607 }
12608 
12609 /*
12610  * Process an SIOCGLIFGROUPINFO request.
12611  */
12612 /* ARGSUSED */
12613 int
12614 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12615     ip_ioctl_cmd_t *ipip, void *dummy)
12616 {
12617 	ipmp_grp_t	*grp;
12618 	lifgroupinfo_t	*lifgr;
12619 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12620 
12621 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12622 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12623 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12624 
12625 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12626 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12627 		rw_exit(&ipst->ips_ipmp_lock);
12628 		return (ENOENT);
12629 	}
12630 	ipmp_grp_info(grp, lifgr);
12631 	rw_exit(&ipst->ips_ipmp_lock);
12632 	return (0);
12633 }
12634 
12635 static void
12636 ill_dl_down(ill_t *ill)
12637 {
12638 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12639 
12640 	/*
12641 	 * The ill is down; unbind but stay attached since we're still
12642 	 * associated with a PPA. If we have negotiated DLPI capabilites
12643 	 * with the data link service provider (IDS_OK) then reset them.
12644 	 * The interval between unbinding and rebinding is potentially
12645 	 * unbounded hence we cannot assume things will be the same.
12646 	 * The DLPI capabilities will be probed again when the data link
12647 	 * is brought up.
12648 	 */
12649 	mblk_t	*mp = ill->ill_unbind_mp;
12650 
12651 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12652 
12653 	if (!ill->ill_replumbing) {
12654 		/* Free all ilms for this ill */
12655 		update_conn_ill(ill, ill->ill_ipst);
12656 	} else {
12657 		ill_leave_multicast(ill);
12658 	}
12659 
12660 	ill->ill_unbind_mp = NULL;
12661 	if (mp != NULL) {
12662 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12663 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12664 		    ill->ill_name));
12665 		mutex_enter(&ill->ill_lock);
12666 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12667 		mutex_exit(&ill->ill_lock);
12668 		/*
12669 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12670 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12671 		 * ill_capability_dld_disable disable rightaway. If this is not
12672 		 * an unplumb operation then the disable happens on receipt of
12673 		 * the capab ack via ip_rput_dlpi_writer ->
12674 		 * ill_capability_ack_thr. In both cases the order of
12675 		 * the operations seen by DLD is capability disable followed
12676 		 * by DL_UNBIND. Also the DLD capability disable needs a
12677 		 * cv_wait'able context.
12678 		 */
12679 		if (ill->ill_state_flags & ILL_CONDEMNED)
12680 			ill_capability_dld_disable(ill);
12681 		ill_capability_reset(ill, B_FALSE);
12682 		ill_dlpi_send(ill, mp);
12683 	}
12684 	mutex_enter(&ill->ill_lock);
12685 	ill->ill_dl_up = 0;
12686 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12687 	mutex_exit(&ill->ill_lock);
12688 }
12689 
12690 void
12691 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12692 {
12693 	union DL_primitives *dlp;
12694 	t_uscalar_t prim;
12695 	boolean_t waitack = B_FALSE;
12696 
12697 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12698 
12699 	dlp = (union DL_primitives *)mp->b_rptr;
12700 	prim = dlp->dl_primitive;
12701 
12702 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12703 	    dl_primstr(prim), prim, ill->ill_name));
12704 
12705 	switch (prim) {
12706 	case DL_PHYS_ADDR_REQ:
12707 	{
12708 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12709 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12710 		break;
12711 	}
12712 	case DL_BIND_REQ:
12713 		mutex_enter(&ill->ill_lock);
12714 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12715 		mutex_exit(&ill->ill_lock);
12716 		break;
12717 	}
12718 
12719 	/*
12720 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12721 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12722 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12723 	 */
12724 	mutex_enter(&ill->ill_lock);
12725 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12726 	    (prim == DL_UNBIND_REQ)) {
12727 		ill->ill_dlpi_pending = prim;
12728 		waitack = B_TRUE;
12729 	}
12730 
12731 	mutex_exit(&ill->ill_lock);
12732 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12733 	    char *, dl_primstr(prim), ill_t *, ill);
12734 	putnext(ill->ill_wq, mp);
12735 
12736 	/*
12737 	 * There is no ack for DL_NOTIFY_CONF messages
12738 	 */
12739 	if (waitack && prim == DL_NOTIFY_CONF)
12740 		ill_dlpi_done(ill, prim);
12741 }
12742 
12743 /*
12744  * Helper function for ill_dlpi_send().
12745  */
12746 /* ARGSUSED */
12747 static void
12748 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12749 {
12750 	ill_dlpi_send(q->q_ptr, mp);
12751 }
12752 
12753 /*
12754  * Send a DLPI control message to the driver but make sure there
12755  * is only one outstanding message. Uses ill_dlpi_pending to tell
12756  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12757  * when an ACK or a NAK is received to process the next queued message.
12758  */
12759 void
12760 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12761 {
12762 	mblk_t **mpp;
12763 
12764 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12765 
12766 	/*
12767 	 * To ensure that any DLPI requests for current exclusive operation
12768 	 * are always completely sent before any DLPI messages for other
12769 	 * operations, require writer access before enqueuing.
12770 	 */
12771 	if (!IAM_WRITER_ILL(ill)) {
12772 		ill_refhold(ill);
12773 		/* qwriter_ip() does the ill_refrele() */
12774 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12775 		    NEW_OP, B_TRUE);
12776 		return;
12777 	}
12778 
12779 	mutex_enter(&ill->ill_lock);
12780 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12781 		/* Must queue message. Tail insertion */
12782 		mpp = &ill->ill_dlpi_deferred;
12783 		while (*mpp != NULL)
12784 			mpp = &((*mpp)->b_next);
12785 
12786 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12787 		    "while %s pending\n", ill->ill_name,
12788 		    dl_primstr(ill->ill_dlpi_pending)));
12789 
12790 		*mpp = mp;
12791 		mutex_exit(&ill->ill_lock);
12792 		return;
12793 	}
12794 	mutex_exit(&ill->ill_lock);
12795 	ill_dlpi_dispatch(ill, mp);
12796 }
12797 
12798 void
12799 ill_capability_send(ill_t *ill, mblk_t *mp)
12800 {
12801 	ill->ill_capab_pending_cnt++;
12802 	ill_dlpi_send(ill, mp);
12803 }
12804 
12805 void
12806 ill_capability_done(ill_t *ill)
12807 {
12808 	ASSERT(ill->ill_capab_pending_cnt != 0);
12809 
12810 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12811 
12812 	ill->ill_capab_pending_cnt--;
12813 	if (ill->ill_capab_pending_cnt == 0 &&
12814 	    ill->ill_dlpi_capab_state == IDCS_OK)
12815 		ill_capability_reset_alloc(ill);
12816 }
12817 
12818 /*
12819  * Send all deferred DLPI messages without waiting for their ACKs.
12820  */
12821 void
12822 ill_dlpi_send_deferred(ill_t *ill)
12823 {
12824 	mblk_t *mp, *nextmp;
12825 
12826 	/*
12827 	 * Clear ill_dlpi_pending so that the message is not queued in
12828 	 * ill_dlpi_send().
12829 	 */
12830 	mutex_enter(&ill->ill_lock);
12831 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12832 	mp = ill->ill_dlpi_deferred;
12833 	ill->ill_dlpi_deferred = NULL;
12834 	mutex_exit(&ill->ill_lock);
12835 
12836 	for (; mp != NULL; mp = nextmp) {
12837 		nextmp = mp->b_next;
12838 		mp->b_next = NULL;
12839 		ill_dlpi_send(ill, mp);
12840 	}
12841 }
12842 
12843 /*
12844  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12845  * or M_HANGUP
12846  */
12847 static void
12848 ill_dlpi_clear_deferred(ill_t *ill)
12849 {
12850 	mblk_t	*mp, *nextmp;
12851 
12852 	mutex_enter(&ill->ill_lock);
12853 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12854 	mp = ill->ill_dlpi_deferred;
12855 	ill->ill_dlpi_deferred = NULL;
12856 	mutex_exit(&ill->ill_lock);
12857 
12858 	for (; mp != NULL; mp = nextmp) {
12859 		nextmp = mp->b_next;
12860 		inet_freemsg(mp);
12861 	}
12862 }
12863 
12864 /*
12865  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12866  */
12867 boolean_t
12868 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12869 {
12870 	t_uscalar_t pending;
12871 
12872 	mutex_enter(&ill->ill_lock);
12873 	if (ill->ill_dlpi_pending == prim) {
12874 		mutex_exit(&ill->ill_lock);
12875 		return (B_TRUE);
12876 	}
12877 
12878 	/*
12879 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12880 	 * without waiting, so don't print any warnings in that case.
12881 	 */
12882 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12883 		mutex_exit(&ill->ill_lock);
12884 		return (B_FALSE);
12885 	}
12886 	pending = ill->ill_dlpi_pending;
12887 	mutex_exit(&ill->ill_lock);
12888 
12889 	if (pending == DL_PRIM_INVAL) {
12890 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12891 		    "received unsolicited ack for %s on %s\n",
12892 		    dl_primstr(prim), ill->ill_name);
12893 	} else {
12894 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12895 		    "received unexpected ack for %s on %s (expecting %s)\n",
12896 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12897 	}
12898 	return (B_FALSE);
12899 }
12900 
12901 /*
12902  * Complete the current DLPI operation associated with `prim' on `ill' and
12903  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12904  * operations and the ill's current exclusive IPSQ operation has finished
12905  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12906  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12907  * the comments above ipsq_current_finish() for details.
12908  */
12909 void
12910 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12911 {
12912 	mblk_t *mp;
12913 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12914 	ipxop_t *ipx = ipsq->ipsq_xop;
12915 
12916 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12917 	mutex_enter(&ill->ill_lock);
12918 
12919 	ASSERT(prim != DL_PRIM_INVAL);
12920 	ASSERT(ill->ill_dlpi_pending == prim);
12921 
12922 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12923 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12924 
12925 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12926 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12927 		if (ipx->ipx_current_done) {
12928 			mutex_enter(&ipx->ipx_lock);
12929 			ipx->ipx_current_ipif = NULL;
12930 			mutex_exit(&ipx->ipx_lock);
12931 		}
12932 		cv_signal(&ill->ill_cv);
12933 		mutex_exit(&ill->ill_lock);
12934 		return;
12935 	}
12936 
12937 	ill->ill_dlpi_deferred = mp->b_next;
12938 	mp->b_next = NULL;
12939 	mutex_exit(&ill->ill_lock);
12940 
12941 	ill_dlpi_dispatch(ill, mp);
12942 }
12943 
12944 /*
12945  * Queue a (multicast) DLPI control message to be sent to the driver by
12946  * later calling ill_dlpi_send_queued.
12947  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12948  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12949  * for the same group to race.
12950  * We send DLPI control messages in order using ill_lock.
12951  * For IPMP we should be called on the cast_ill.
12952  */
12953 void
12954 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12955 {
12956 	mblk_t **mpp;
12957 
12958 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12959 
12960 	mutex_enter(&ill->ill_lock);
12961 	/* Must queue message. Tail insertion */
12962 	mpp = &ill->ill_dlpi_deferred;
12963 	while (*mpp != NULL)
12964 		mpp = &((*mpp)->b_next);
12965 
12966 	*mpp = mp;
12967 	mutex_exit(&ill->ill_lock);
12968 }
12969 
12970 /*
12971  * Send the messages that were queued. Make sure there is only
12972  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12973  * when an ACK or a NAK is received to process the next queued message.
12974  * For IPMP we are called on the upper ill, but when send what is queued
12975  * on the cast_ill.
12976  */
12977 void
12978 ill_dlpi_send_queued(ill_t *ill)
12979 {
12980 	mblk_t	*mp;
12981 	union DL_primitives *dlp;
12982 	t_uscalar_t prim;
12983 	ill_t *release_ill = NULL;
12984 
12985 	if (IS_IPMP(ill)) {
12986 		/* On the upper IPMP ill. */
12987 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12988 		if (release_ill == NULL) {
12989 			/* Avoid ever sending anything down to the ipmpstub */
12990 			return;
12991 		}
12992 		ill = release_ill;
12993 	}
12994 	mutex_enter(&ill->ill_lock);
12995 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12996 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12997 			/* Can't send. Somebody else will send it */
12998 			mutex_exit(&ill->ill_lock);
12999 			goto done;
13000 		}
13001 		ill->ill_dlpi_deferred = mp->b_next;
13002 		mp->b_next = NULL;
13003 		if (!ill->ill_dl_up) {
13004 			/*
13005 			 * Nobody there. All multicast addresses will be
13006 			 * re-joined when we get the DL_BIND_ACK bringing the
13007 			 * interface up.
13008 			 */
13009 			freemsg(mp);
13010 			continue;
13011 		}
13012 		dlp = (union DL_primitives *)mp->b_rptr;
13013 		prim = dlp->dl_primitive;
13014 
13015 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13016 		    (prim == DL_UNBIND_REQ)) {
13017 			ill->ill_dlpi_pending = prim;
13018 		}
13019 		mutex_exit(&ill->ill_lock);
13020 
13021 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13022 		    char *, dl_primstr(prim), ill_t *, ill);
13023 		putnext(ill->ill_wq, mp);
13024 		mutex_enter(&ill->ill_lock);
13025 	}
13026 	mutex_exit(&ill->ill_lock);
13027 done:
13028 	if (release_ill != NULL)
13029 		ill_refrele(release_ill);
13030 }
13031 
13032 /*
13033  * Queue an IP (IGMP/MLD) message to be sent by IP from
13034  * ill_mcast_send_queued
13035  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13036  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13037  * group to race.
13038  * We send them in order using ill_lock.
13039  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13040  */
13041 void
13042 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13043 {
13044 	mblk_t **mpp;
13045 	ill_t *release_ill = NULL;
13046 
13047 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13048 
13049 	if (IS_IPMP(ill)) {
13050 		/* On the upper IPMP ill. */
13051 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13052 		if (release_ill == NULL) {
13053 			/* Discard instead of queuing for the ipmp interface */
13054 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13055 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13056 			    mp, ill);
13057 			freemsg(mp);
13058 			return;
13059 		}
13060 		ill = release_ill;
13061 	}
13062 
13063 	mutex_enter(&ill->ill_lock);
13064 	/* Must queue message. Tail insertion */
13065 	mpp = &ill->ill_mcast_deferred;
13066 	while (*mpp != NULL)
13067 		mpp = &((*mpp)->b_next);
13068 
13069 	*mpp = mp;
13070 	mutex_exit(&ill->ill_lock);
13071 	if (release_ill != NULL)
13072 		ill_refrele(release_ill);
13073 }
13074 
13075 /*
13076  * Send the IP packets that were queued by ill_mcast_queue.
13077  * These are IGMP/MLD packets.
13078  *
13079  * For IPMP we are called on the upper ill, but when send what is queued
13080  * on the cast_ill.
13081  *
13082  * Request loopback of the report if we are acting as a multicast
13083  * router, so that the process-level routing demon can hear it.
13084  * This will run multiple times for the same group if there are members
13085  * on the same group for multiple ipif's on the same ill. The
13086  * igmp_input/mld_input code will suppress this due to the loopback thus we
13087  * always loopback membership report.
13088  *
13089  * We also need to make sure that this does not get load balanced
13090  * by IPMP. We do this by passing an ill to ip_output_simple.
13091  */
13092 void
13093 ill_mcast_send_queued(ill_t *ill)
13094 {
13095 	mblk_t	*mp;
13096 	ip_xmit_attr_t ixas;
13097 	ill_t *release_ill = NULL;
13098 
13099 	if (IS_IPMP(ill)) {
13100 		/* On the upper IPMP ill. */
13101 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13102 		if (release_ill == NULL) {
13103 			/*
13104 			 * We should have no messages on the ipmp interface
13105 			 * but no point in trying to send them.
13106 			 */
13107 			return;
13108 		}
13109 		ill = release_ill;
13110 	}
13111 	bzero(&ixas, sizeof (ixas));
13112 	ixas.ixa_zoneid = ALL_ZONES;
13113 	ixas.ixa_cred = kcred;
13114 	ixas.ixa_cpid = NOPID;
13115 	ixas.ixa_tsl = NULL;
13116 	/*
13117 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13118 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13119 	 * That is necessary to handle IGMP/MLD snooping switches.
13120 	 */
13121 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13122 	ixas.ixa_ipst = ill->ill_ipst;
13123 
13124 	mutex_enter(&ill->ill_lock);
13125 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13126 		ill->ill_mcast_deferred = mp->b_next;
13127 		mp->b_next = NULL;
13128 		if (!ill->ill_dl_up) {
13129 			/*
13130 			 * Nobody there. Just drop the ip packets.
13131 			 * IGMP/MLD will resend later, if this is a replumb.
13132 			 */
13133 			freemsg(mp);
13134 			continue;
13135 		}
13136 		mutex_enter(&ill->ill_phyint->phyint_lock);
13137 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13138 			/*
13139 			 * When the ill is getting deactivated, we only want to
13140 			 * send the DLPI messages, so drop IGMP/MLD packets.
13141 			 * DLPI messages are handled by ill_dlpi_send_queued()
13142 			 */
13143 			mutex_exit(&ill->ill_phyint->phyint_lock);
13144 			freemsg(mp);
13145 			continue;
13146 		}
13147 		mutex_exit(&ill->ill_phyint->phyint_lock);
13148 		mutex_exit(&ill->ill_lock);
13149 
13150 		/* Check whether we are sending IPv4 or IPv6. */
13151 		if (ill->ill_isv6) {
13152 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13153 
13154 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13155 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13156 		} else {
13157 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13158 
13159 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13160 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13161 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13162 		}
13163 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13164 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13165 		(void) ip_output_simple(mp, &ixas);
13166 		ixa_cleanup(&ixas);
13167 
13168 		mutex_enter(&ill->ill_lock);
13169 	}
13170 	mutex_exit(&ill->ill_lock);
13171 
13172 done:
13173 	if (release_ill != NULL)
13174 		ill_refrele(release_ill);
13175 }
13176 
13177 /*
13178  * Take down a specific interface, but don't lose any information about it.
13179  * (Always called as writer.)
13180  * This function goes through the down sequence even if the interface is
13181  * already down. There are 2 reasons.
13182  * a. Currently we permit interface routes that depend on down interfaces
13183  *    to be added. This behaviour itself is questionable. However it appears
13184  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13185  *    time. We go thru the cleanup in order to remove these routes.
13186  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13187  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13188  *    down, but we need to cleanup i.e. do ill_dl_down and
13189  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13190  *
13191  * IP-MT notes:
13192  *
13193  * Model of reference to interfaces.
13194  *
13195  * The following members in ipif_t track references to the ipif.
13196  *	int     ipif_refcnt;    Active reference count
13197  *
13198  * The following members in ill_t track references to the ill.
13199  *	int             ill_refcnt;     active refcnt
13200  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13201  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13202  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13203  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13204  *
13205  * Reference to an ipif or ill can be obtained in any of the following ways.
13206  *
13207  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13208  * Pointers to ipif / ill from other data structures viz ire and conn.
13209  * Implicit reference to the ipif / ill by holding a reference to the ire.
13210  *
13211  * The ipif/ill lookup functions return a reference held ipif / ill.
13212  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13213  * This is a purely dynamic reference count associated with threads holding
13214  * references to the ipif / ill. Pointers from other structures do not
13215  * count towards this reference count.
13216  *
13217  * ill_ire_cnt is the number of ire's associated with the
13218  * ill. This is incremented whenever a new ire is created referencing the
13219  * ill. This is done atomically inside ire_add_v[46] where the ire is
13220  * actually added to the ire hash table. The count is decremented in
13221  * ire_inactive where the ire is destroyed.
13222  *
13223  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13224  * This is incremented atomically in
13225  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13226  * table. Similarly it is decremented in ncec_inactive() where the ncec
13227  * is destroyed.
13228  *
13229  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13230  * incremented atomically in nce_add() where the nce is actually added to the
13231  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13232  * is destroyed.
13233  *
13234  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13235  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13236  *
13237  * Flow of ioctls involving interface down/up
13238  *
13239  * The following is the sequence of an attempt to set some critical flags on an
13240  * up interface.
13241  * ip_sioctl_flags
13242  * ipif_down
13243  * wait for ipif to be quiescent
13244  * ipif_down_tail
13245  * ip_sioctl_flags_tail
13246  *
13247  * All set ioctls that involve down/up sequence would have a skeleton similar
13248  * to the above. All the *tail functions are called after the refcounts have
13249  * dropped to the appropriate values.
13250  *
13251  * SIOC ioctls during the IPIF_CHANGING interval.
13252  *
13253  * Threads handling SIOC set ioctls serialize on the squeue, but this
13254  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13255  * steps of internal changes to the state, some of which are visible in
13256  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13257  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13258  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13259  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13260  * the current exclusive operation completes. The IPIF_CHANGING check
13261  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13262  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13263  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13264  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13265  * until we release the ipsq_lock, even though the ill/ipif state flags
13266  * can change after we drop the ill_lock.
13267  */
13268 int
13269 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13270 {
13271 	ill_t		*ill = ipif->ipif_ill;
13272 	conn_t		*connp;
13273 	boolean_t	success;
13274 	boolean_t	ipif_was_up = B_FALSE;
13275 	ip_stack_t	*ipst = ill->ill_ipst;
13276 
13277 	ASSERT(IAM_WRITER_IPIF(ipif));
13278 
13279 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13280 
13281 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13282 	    ill_t *, ill, ipif_t *, ipif);
13283 
13284 	if (ipif->ipif_flags & IPIF_UP) {
13285 		mutex_enter(&ill->ill_lock);
13286 		ipif->ipif_flags &= ~IPIF_UP;
13287 		ASSERT(ill->ill_ipif_up_count > 0);
13288 		--ill->ill_ipif_up_count;
13289 		mutex_exit(&ill->ill_lock);
13290 		ipif_was_up = B_TRUE;
13291 		/* Update status in SCTP's list */
13292 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13293 		ill_nic_event_dispatch(ipif->ipif_ill,
13294 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13295 	}
13296 
13297 	/*
13298 	 * Removal of the last ipif from an ill may result in a DL_UNBIND
13299 	 * being sent to the driver, and we must not send any data packets to
13300 	 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13301 	 * ire and nce entries used in the data path will be cleaned
13302 	 * up, and we also set  the ILL_DOWN_IN_PROGRESS bit to make
13303 	 * sure on new entries will be added until the ill is bound
13304 	 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13305 	 * receipt of a DL_BIND_ACK.
13306 	 */
13307 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13308 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13309 	    ill->ill_dl_up) {
13310 		ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13311 	}
13312 
13313 	/*
13314 	 * Blow away memberships we established in ipif_multicast_up().
13315 	 */
13316 	ipif_multicast_down(ipif);
13317 
13318 	/*
13319 	 * Remove from the mapping for __sin6_src_id. We insert only
13320 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13321 	 * stored as mapped addresses, we need to check for mapped
13322 	 * INADDR_ANY also.
13323 	 */
13324 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13325 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13326 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13327 		int err;
13328 
13329 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13330 		    ipif->ipif_zoneid, ipst);
13331 		if (err != 0) {
13332 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13333 		}
13334 	}
13335 
13336 	if (ipif_was_up) {
13337 		/* only delete if we'd added ire's before */
13338 		if (ipif->ipif_isv6)
13339 			ipif_delete_ires_v6(ipif);
13340 		else
13341 			ipif_delete_ires_v4(ipif);
13342 	}
13343 
13344 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13345 		/*
13346 		 * Since the interface is now down, it may have just become
13347 		 * inactive.  Note that this needs to be done even for a
13348 		 * lll_logical_down(), or ARP entries will not get correctly
13349 		 * restored when the interface comes back up.
13350 		 */
13351 		if (IS_UNDER_IPMP(ill))
13352 			ipmp_ill_refresh_active(ill);
13353 	}
13354 
13355 	/*
13356 	 * neighbor-discovery or arp entries for this interface. The ipif
13357 	 * has to be quiesced, so we walk all the nce's and delete those
13358 	 * that point at the ipif->ipif_ill. At the same time, we also
13359 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13360 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13361 	 * that for ipif_down_tail()
13362 	 */
13363 	ipif_nce_down(ipif);
13364 
13365 	/*
13366 	 * If this is the last ipif on the ill, we also need to remove
13367 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13368 	 * never succeed.
13369 	 */
13370 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13371 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13372 
13373 	/*
13374 	 * Walk all CONNs that can have a reference on an ire for this
13375 	 * ipif (we actually walk all that now have stale references).
13376 	 */
13377 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13378 
13379 	/*
13380 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13381 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13382 	 * and ill_delete -> ipif_free -> ipif_down
13383 	 */
13384 	if (mp == NULL) {
13385 		ASSERT(q == NULL);
13386 		return (0);
13387 	}
13388 
13389 	if (CONN_Q(q)) {
13390 		connp = Q_TO_CONN(q);
13391 		mutex_enter(&connp->conn_lock);
13392 	} else {
13393 		connp = NULL;
13394 	}
13395 	mutex_enter(&ill->ill_lock);
13396 	/*
13397 	 * Are there any ire's pointing to this ipif that are still active ?
13398 	 * If this is the last ipif going down, are there any ire's pointing
13399 	 * to this ill that are still active ?
13400 	 */
13401 	if (ipif_is_quiescent(ipif)) {
13402 		mutex_exit(&ill->ill_lock);
13403 		if (connp != NULL)
13404 			mutex_exit(&connp->conn_lock);
13405 		return (0);
13406 	}
13407 
13408 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13409 	    ill->ill_name, (void *)ill));
13410 	/*
13411 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13412 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13413 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13414 	 */
13415 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13416 	if (!success) {
13417 		/* The conn is closing. So just return */
13418 		ASSERT(connp != NULL);
13419 		mutex_exit(&ill->ill_lock);
13420 		mutex_exit(&connp->conn_lock);
13421 		return (EINTR);
13422 	}
13423 
13424 	mutex_exit(&ill->ill_lock);
13425 	if (connp != NULL)
13426 		mutex_exit(&connp->conn_lock);
13427 	return (EINPROGRESS);
13428 }
13429 
13430 int
13431 ipif_down_tail(ipif_t *ipif)
13432 {
13433 	ill_t	*ill = ipif->ipif_ill;
13434 	int	err = 0;
13435 
13436 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13437 	    ill_t *, ill, ipif_t *, ipif);
13438 
13439 	/*
13440 	 * Skip any loopback interface (null wq).
13441 	 * If this is the last logical interface on the ill
13442 	 * have ill_dl_down tell the driver we are gone (unbind)
13443 	 * Note that lun 0 can ipif_down even though
13444 	 * there are other logical units that are up.
13445 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13446 	 */
13447 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13448 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13449 	    ill->ill_dl_up) {
13450 		ill_dl_down(ill);
13451 	}
13452 	if (!ipif->ipif_isv6)
13453 		err = ipif_arp_down(ipif);
13454 
13455 	ill->ill_logical_down = 0;
13456 
13457 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13458 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13459 	return (err);
13460 }
13461 
13462 /*
13463  * Bring interface logically down without bringing the physical interface
13464  * down e.g. when the netmask is changed. This avoids long lasting link
13465  * negotiations between an ethernet interface and a certain switches.
13466  */
13467 static int
13468 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13469 {
13470 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13471 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13472 
13473 	/*
13474 	 * The ill_logical_down flag is a transient flag. It is set here
13475 	 * and is cleared once the down has completed in ipif_down_tail.
13476 	 * This flag does not indicate whether the ill stream is in the
13477 	 * DL_BOUND state with the driver. Instead this flag is used by
13478 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13479 	 * the driver. The state of the ill stream i.e. whether it is
13480 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13481 	 */
13482 	ipif->ipif_ill->ill_logical_down = 1;
13483 	return (ipif_down(ipif, q, mp));
13484 }
13485 
13486 /*
13487  * Initiate deallocate of an IPIF. Always called as writer. Called by
13488  * ill_delete or ip_sioctl_removeif.
13489  */
13490 static void
13491 ipif_free(ipif_t *ipif)
13492 {
13493 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13494 
13495 	ASSERT(IAM_WRITER_IPIF(ipif));
13496 
13497 	if (ipif->ipif_recovery_id != 0)
13498 		(void) untimeout(ipif->ipif_recovery_id);
13499 	ipif->ipif_recovery_id = 0;
13500 
13501 	/*
13502 	 * Take down the interface. We can be called either from ill_delete
13503 	 * or from ip_sioctl_removeif.
13504 	 */
13505 	(void) ipif_down(ipif, NULL, NULL);
13506 
13507 	/*
13508 	 * Now that the interface is down, there's no chance it can still
13509 	 * become a duplicate.  Cancel any timer that may have been set while
13510 	 * tearing down.
13511 	 */
13512 	if (ipif->ipif_recovery_id != 0)
13513 		(void) untimeout(ipif->ipif_recovery_id);
13514 	ipif->ipif_recovery_id = 0;
13515 
13516 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13517 	/* Remove pointers to this ill in the multicast routing tables */
13518 	reset_mrt_vif_ipif(ipif);
13519 	/* If necessary, clear the cached source ipif rotor. */
13520 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13521 		ipif->ipif_ill->ill_src_ipif = NULL;
13522 	rw_exit(&ipst->ips_ill_g_lock);
13523 }
13524 
13525 static void
13526 ipif_free_tail(ipif_t *ipif)
13527 {
13528 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13529 
13530 	/*
13531 	 * Need to hold both ill_g_lock and ill_lock while
13532 	 * inserting or removing an ipif from the linked list
13533 	 * of ipifs hanging off the ill.
13534 	 */
13535 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13536 
13537 #ifdef DEBUG
13538 	ipif_trace_cleanup(ipif);
13539 #endif
13540 
13541 	/* Ask SCTP to take it out of it list */
13542 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13543 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13544 
13545 	/* Get it out of the ILL interface list. */
13546 	ipif_remove(ipif);
13547 	rw_exit(&ipst->ips_ill_g_lock);
13548 
13549 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13550 	ASSERT(ipif->ipif_recovery_id == 0);
13551 	ASSERT(ipif->ipif_ire_local == NULL);
13552 	ASSERT(ipif->ipif_ire_if == NULL);
13553 
13554 	/* Free the memory. */
13555 	mi_free(ipif);
13556 }
13557 
13558 /*
13559  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13560  * is zero.
13561  */
13562 void
13563 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13564 {
13565 	char	lbuf[LIFNAMSIZ];
13566 	char	*name;
13567 	size_t	name_len;
13568 
13569 	buf[0] = '\0';
13570 	name = ipif->ipif_ill->ill_name;
13571 	name_len = ipif->ipif_ill->ill_name_length;
13572 	if (ipif->ipif_id != 0) {
13573 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13574 		    ipif->ipif_id);
13575 		name = lbuf;
13576 		name_len = mi_strlen(name) + 1;
13577 	}
13578 	len -= 1;
13579 	buf[len] = '\0';
13580 	len = MIN(len, name_len);
13581 	bcopy(name, buf, len);
13582 }
13583 
13584 /*
13585  * Sets `buf' to an ill name.
13586  */
13587 void
13588 ill_get_name(const ill_t *ill, char *buf, int len)
13589 {
13590 	char	*name;
13591 	size_t	name_len;
13592 
13593 	name = ill->ill_name;
13594 	name_len = ill->ill_name_length;
13595 	len -= 1;
13596 	buf[len] = '\0';
13597 	len = MIN(len, name_len);
13598 	bcopy(name, buf, len);
13599 }
13600 
13601 /*
13602  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13603  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13604  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13605  * (May be called as writer.)
13606  */
13607 static ipif_t *
13608 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13609     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13610 {
13611 	char	*cp;
13612 	char	*endp;
13613 	long	id;
13614 	ill_t	*ill;
13615 	ipif_t	*ipif;
13616 	uint_t	ire_type;
13617 	boolean_t did_alloc = B_FALSE;
13618 	char	last;
13619 
13620 	/*
13621 	 * If the caller wants to us to create the ipif, make sure we have a
13622 	 * valid zoneid
13623 	 */
13624 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13625 
13626 	if (namelen == 0) {
13627 		return (NULL);
13628 	}
13629 
13630 	*exists = B_FALSE;
13631 	/* Look for a colon in the name. */
13632 	endp = &name[namelen];
13633 	for (cp = endp; --cp > name; ) {
13634 		if (*cp == IPIF_SEPARATOR_CHAR)
13635 			break;
13636 	}
13637 
13638 	if (*cp == IPIF_SEPARATOR_CHAR) {
13639 		/*
13640 		 * Reject any non-decimal aliases for logical
13641 		 * interfaces. Aliases with leading zeroes
13642 		 * are also rejected as they introduce ambiguity
13643 		 * in the naming of the interfaces.
13644 		 * In order to confirm with existing semantics,
13645 		 * and to not break any programs/script relying
13646 		 * on that behaviour, if<0>:0 is considered to be
13647 		 * a valid interface.
13648 		 *
13649 		 * If alias has two or more digits and the first
13650 		 * is zero, fail.
13651 		 */
13652 		if (&cp[2] < endp && cp[1] == '0') {
13653 			return (NULL);
13654 		}
13655 	}
13656 
13657 	if (cp <= name) {
13658 		cp = endp;
13659 	}
13660 	last = *cp;
13661 	*cp = '\0';
13662 
13663 	/*
13664 	 * Look up the ILL, based on the portion of the name
13665 	 * before the slash. ill_lookup_on_name returns a held ill.
13666 	 * Temporary to check whether ill exists already. If so
13667 	 * ill_lookup_on_name will clear it.
13668 	 */
13669 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13670 	    &did_alloc, ipst);
13671 	*cp = last;
13672 	if (ill == NULL)
13673 		return (NULL);
13674 
13675 	/* Establish the unit number in the name. */
13676 	id = 0;
13677 	if (cp < endp && *endp == '\0') {
13678 		/* If there was a colon, the unit number follows. */
13679 		cp++;
13680 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13681 			ill_refrele(ill);
13682 			return (NULL);
13683 		}
13684 	}
13685 
13686 	mutex_enter(&ill->ill_lock);
13687 	/* Now see if there is an IPIF with this unit number. */
13688 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13689 		if (ipif->ipif_id == id) {
13690 			if (zoneid != ALL_ZONES &&
13691 			    zoneid != ipif->ipif_zoneid &&
13692 			    ipif->ipif_zoneid != ALL_ZONES) {
13693 				mutex_exit(&ill->ill_lock);
13694 				ill_refrele(ill);
13695 				return (NULL);
13696 			}
13697 			if (IPIF_CAN_LOOKUP(ipif)) {
13698 				ipif_refhold_locked(ipif);
13699 				mutex_exit(&ill->ill_lock);
13700 				if (!did_alloc)
13701 					*exists = B_TRUE;
13702 				/*
13703 				 * Drop locks before calling ill_refrele
13704 				 * since it can potentially call into
13705 				 * ipif_ill_refrele_tail which can end up
13706 				 * in trying to acquire any lock.
13707 				 */
13708 				ill_refrele(ill);
13709 				return (ipif);
13710 			}
13711 		}
13712 	}
13713 
13714 	if (!do_alloc) {
13715 		mutex_exit(&ill->ill_lock);
13716 		ill_refrele(ill);
13717 		return (NULL);
13718 	}
13719 
13720 	/*
13721 	 * If none found, atomically allocate and return a new one.
13722 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13723 	 * to support "receive only" use of lo0:1 etc. as is still done
13724 	 * below as an initial guess.
13725 	 * However, this is now likely to be overriden later in ipif_up_done()
13726 	 * when we know for sure what address has been configured on the
13727 	 * interface, since we might have more than one loopback interface
13728 	 * with a loopback address, e.g. in the case of zones, and all the
13729 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13730 	 */
13731 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13732 		ire_type = IRE_LOOPBACK;
13733 	else
13734 		ire_type = IRE_LOCAL;
13735 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13736 	if (ipif != NULL)
13737 		ipif_refhold_locked(ipif);
13738 	mutex_exit(&ill->ill_lock);
13739 	ill_refrele(ill);
13740 	return (ipif);
13741 }
13742 
13743 /*
13744  * Variant of the above that queues the request on the ipsq when
13745  * IPIF_CHANGING is set.
13746  */
13747 static ipif_t *
13748 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13749     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13750     ip_stack_t *ipst)
13751 {
13752 	char	*cp;
13753 	char	*endp;
13754 	long	id;
13755 	ill_t	*ill;
13756 	ipif_t	*ipif;
13757 	boolean_t did_alloc = B_FALSE;
13758 	ipsq_t	*ipsq;
13759 
13760 	if (error != NULL)
13761 		*error = 0;
13762 
13763 	if (namelen == 0) {
13764 		if (error != NULL)
13765 			*error = ENXIO;
13766 		return (NULL);
13767 	}
13768 
13769 	/* Look for a colon in the name. */
13770 	endp = &name[namelen];
13771 	for (cp = endp; --cp > name; ) {
13772 		if (*cp == IPIF_SEPARATOR_CHAR)
13773 			break;
13774 	}
13775 
13776 	if (*cp == IPIF_SEPARATOR_CHAR) {
13777 		/*
13778 		 * Reject any non-decimal aliases for logical
13779 		 * interfaces. Aliases with leading zeroes
13780 		 * are also rejected as they introduce ambiguity
13781 		 * in the naming of the interfaces.
13782 		 * In order to confirm with existing semantics,
13783 		 * and to not break any programs/script relying
13784 		 * on that behaviour, if<0>:0 is considered to be
13785 		 * a valid interface.
13786 		 *
13787 		 * If alias has two or more digits and the first
13788 		 * is zero, fail.
13789 		 */
13790 		if (&cp[2] < endp && cp[1] == '0') {
13791 			if (error != NULL)
13792 				*error = EINVAL;
13793 			return (NULL);
13794 		}
13795 	}
13796 
13797 	if (cp <= name) {
13798 		cp = endp;
13799 	} else {
13800 		*cp = '\0';
13801 	}
13802 
13803 	/*
13804 	 * Look up the ILL, based on the portion of the name
13805 	 * before the slash. ill_lookup_on_name returns a held ill.
13806 	 * Temporary to check whether ill exists already. If so
13807 	 * ill_lookup_on_name will clear it.
13808 	 */
13809 	ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13810 	if (cp != endp)
13811 		*cp = IPIF_SEPARATOR_CHAR;
13812 	if (ill == NULL)
13813 		return (NULL);
13814 
13815 	/* Establish the unit number in the name. */
13816 	id = 0;
13817 	if (cp < endp && *endp == '\0') {
13818 		/* If there was a colon, the unit number follows. */
13819 		cp++;
13820 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13821 			ill_refrele(ill);
13822 			if (error != NULL)
13823 				*error = ENXIO;
13824 			return (NULL);
13825 		}
13826 	}
13827 
13828 	GRAB_CONN_LOCK(q);
13829 	mutex_enter(&ill->ill_lock);
13830 	/* Now see if there is an IPIF with this unit number. */
13831 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13832 		if (ipif->ipif_id == id) {
13833 			if (zoneid != ALL_ZONES &&
13834 			    zoneid != ipif->ipif_zoneid &&
13835 			    ipif->ipif_zoneid != ALL_ZONES) {
13836 				mutex_exit(&ill->ill_lock);
13837 				RELEASE_CONN_LOCK(q);
13838 				ill_refrele(ill);
13839 				if (error != NULL)
13840 					*error = ENXIO;
13841 				return (NULL);
13842 			}
13843 
13844 			if (!(IPIF_IS_CHANGING(ipif) ||
13845 			    IPIF_IS_CONDEMNED(ipif)) ||
13846 			    IAM_WRITER_IPIF(ipif)) {
13847 				ipif_refhold_locked(ipif);
13848 				mutex_exit(&ill->ill_lock);
13849 				/*
13850 				 * Drop locks before calling ill_refrele
13851 				 * since it can potentially call into
13852 				 * ipif_ill_refrele_tail which can end up
13853 				 * in trying to acquire any lock.
13854 				 */
13855 				RELEASE_CONN_LOCK(q);
13856 				ill_refrele(ill);
13857 				return (ipif);
13858 			} else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13859 				ipsq = ill->ill_phyint->phyint_ipsq;
13860 				mutex_enter(&ipsq->ipsq_lock);
13861 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13862 				mutex_exit(&ill->ill_lock);
13863 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13864 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13865 				mutex_exit(&ipsq->ipsq_lock);
13866 				RELEASE_CONN_LOCK(q);
13867 				ill_refrele(ill);
13868 				if (error != NULL)
13869 					*error = EINPROGRESS;
13870 				return (NULL);
13871 			}
13872 		}
13873 	}
13874 	RELEASE_CONN_LOCK(q);
13875 	mutex_exit(&ill->ill_lock);
13876 	ill_refrele(ill);
13877 	if (error != NULL)
13878 		*error = ENXIO;
13879 	return (NULL);
13880 }
13881 
13882 /*
13883  * This routine is called whenever a new address comes up on an ipif.  If
13884  * we are configured to respond to address mask requests, then we are supposed
13885  * to broadcast an address mask reply at this time.  This routine is also
13886  * called if we are already up, but a netmask change is made.  This is legal
13887  * but might not make the system manager very popular.	(May be called
13888  * as writer.)
13889  */
13890 void
13891 ipif_mask_reply(ipif_t *ipif)
13892 {
13893 	icmph_t	*icmph;
13894 	ipha_t	*ipha;
13895 	mblk_t	*mp;
13896 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13897 	ip_xmit_attr_t ixas;
13898 
13899 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13900 
13901 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13902 		return;
13903 
13904 	/* ICMP mask reply is IPv4 only */
13905 	ASSERT(!ipif->ipif_isv6);
13906 	/* ICMP mask reply is not for a loopback interface */
13907 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13908 
13909 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13910 		return;
13911 
13912 	mp = allocb(REPLY_LEN, BPRI_HI);
13913 	if (mp == NULL)
13914 		return;
13915 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13916 
13917 	ipha = (ipha_t *)mp->b_rptr;
13918 	bzero(ipha, REPLY_LEN);
13919 	*ipha = icmp_ipha;
13920 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13921 	ipha->ipha_src = ipif->ipif_lcl_addr;
13922 	ipha->ipha_dst = ipif->ipif_brd_addr;
13923 	ipha->ipha_length = htons(REPLY_LEN);
13924 	ipha->ipha_ident = 0;
13925 
13926 	icmph = (icmph_t *)&ipha[1];
13927 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13928 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13929 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13930 
13931 	bzero(&ixas, sizeof (ixas));
13932 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13933 	ixas.ixa_zoneid = ALL_ZONES;
13934 	ixas.ixa_ifindex = 0;
13935 	ixas.ixa_ipst = ipst;
13936 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13937 	(void) ip_output_simple(mp, &ixas);
13938 	ixa_cleanup(&ixas);
13939 #undef	REPLY_LEN
13940 }
13941 
13942 /*
13943  * Join the ipif specific multicast groups.
13944  * Must be called after a mapping has been set up in the resolver.  (Always
13945  * called as writer.)
13946  */
13947 void
13948 ipif_multicast_up(ipif_t *ipif)
13949 {
13950 	int err;
13951 	ill_t *ill;
13952 	ilm_t *ilm;
13953 
13954 	ASSERT(IAM_WRITER_IPIF(ipif));
13955 
13956 	ill = ipif->ipif_ill;
13957 
13958 	ip1dbg(("ipif_multicast_up\n"));
13959 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13960 	    ipif->ipif_allhosts_ilm != NULL)
13961 		return;
13962 
13963 	if (ipif->ipif_isv6) {
13964 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13965 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13966 
13967 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13968 
13969 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13970 			return;
13971 
13972 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13973 
13974 		/*
13975 		 * Join the all hosts multicast address.  We skip this for
13976 		 * underlying IPMP interfaces since they should be invisible.
13977 		 */
13978 		if (!IS_UNDER_IPMP(ill)) {
13979 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13980 			    &err);
13981 			if (ilm == NULL) {
13982 				ASSERT(err != 0);
13983 				ip0dbg(("ipif_multicast_up: "
13984 				    "all_hosts_mcast failed %d\n", err));
13985 				return;
13986 			}
13987 			ipif->ipif_allhosts_ilm = ilm;
13988 		}
13989 
13990 		/*
13991 		 * Enable multicast for the solicited node multicast address.
13992 		 * If IPMP we need to put the membership on the upper ill.
13993 		 */
13994 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13995 			ill_t *mcast_ill = NULL;
13996 			boolean_t need_refrele;
13997 
13998 			if (IS_UNDER_IPMP(ill) &&
13999 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
14000 				need_refrele = B_TRUE;
14001 			} else {
14002 				mcast_ill = ill;
14003 				need_refrele = B_FALSE;
14004 			}
14005 
14006 			ilm = ip_addmulti(&v6solmc, mcast_ill,
14007 			    ipif->ipif_zoneid, &err);
14008 			if (need_refrele)
14009 				ill_refrele(mcast_ill);
14010 
14011 			if (ilm == NULL) {
14012 				ASSERT(err != 0);
14013 				ip0dbg(("ipif_multicast_up: solicited MC"
14014 				    " failed %d\n", err));
14015 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14016 					ipif->ipif_allhosts_ilm = NULL;
14017 					(void) ip_delmulti(ilm);
14018 				}
14019 				return;
14020 			}
14021 			ipif->ipif_solmulti_ilm = ilm;
14022 		}
14023 	} else {
14024 		in6_addr_t v6group;
14025 
14026 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14027 			return;
14028 
14029 		/* Join the all hosts multicast address */
14030 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14031 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14032 
14033 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14034 		if (ilm == NULL) {
14035 			ASSERT(err != 0);
14036 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
14037 			return;
14038 		}
14039 		ipif->ipif_allhosts_ilm = ilm;
14040 	}
14041 }
14042 
14043 /*
14044  * Blow away any multicast groups that we joined in ipif_multicast_up().
14045  * (ilms from explicit memberships are handled in conn_update_ill.)
14046  */
14047 void
14048 ipif_multicast_down(ipif_t *ipif)
14049 {
14050 	ASSERT(IAM_WRITER_IPIF(ipif));
14051 
14052 	ip1dbg(("ipif_multicast_down\n"));
14053 
14054 	if (ipif->ipif_allhosts_ilm != NULL) {
14055 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
14056 		ipif->ipif_allhosts_ilm = NULL;
14057 	}
14058 	if (ipif->ipif_solmulti_ilm != NULL) {
14059 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
14060 		ipif->ipif_solmulti_ilm = NULL;
14061 	}
14062 }
14063 
14064 /*
14065  * Used when an interface comes up to recreate any extra routes on this
14066  * interface.
14067  */
14068 int
14069 ill_recover_saved_ire(ill_t *ill)
14070 {
14071 	mblk_t		*mp;
14072 	ip_stack_t	*ipst = ill->ill_ipst;
14073 
14074 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14075 
14076 	mutex_enter(&ill->ill_saved_ire_lock);
14077 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14078 		ire_t		*ire, *nire;
14079 		ifrt_t		*ifrt;
14080 
14081 		ifrt = (ifrt_t *)mp->b_rptr;
14082 		/*
14083 		 * Create a copy of the IRE with the saved address and netmask.
14084 		 */
14085 		if (ill->ill_isv6) {
14086 			ire = ire_create_v6(
14087 			    &ifrt->ifrt_v6addr,
14088 			    &ifrt->ifrt_v6mask,
14089 			    &ifrt->ifrt_v6gateway_addr,
14090 			    ifrt->ifrt_type,
14091 			    ill,
14092 			    ifrt->ifrt_zoneid,
14093 			    ifrt->ifrt_flags,
14094 			    NULL,
14095 			    ipst);
14096 		} else {
14097 			ire = ire_create(
14098 			    (uint8_t *)&ifrt->ifrt_addr,
14099 			    (uint8_t *)&ifrt->ifrt_mask,
14100 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
14101 			    ifrt->ifrt_type,
14102 			    ill,
14103 			    ifrt->ifrt_zoneid,
14104 			    ifrt->ifrt_flags,
14105 			    NULL,
14106 			    ipst);
14107 		}
14108 		if (ire == NULL) {
14109 			mutex_exit(&ill->ill_saved_ire_lock);
14110 			return (ENOMEM);
14111 		}
14112 
14113 		if (ifrt->ifrt_flags & RTF_SETSRC) {
14114 			if (ill->ill_isv6) {
14115 				ire->ire_setsrc_addr_v6 =
14116 				    ifrt->ifrt_v6setsrc_addr;
14117 			} else {
14118 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14119 			}
14120 		}
14121 
14122 		/*
14123 		 * Some software (for example, GateD and Sun Cluster) attempts
14124 		 * to create (what amount to) IRE_PREFIX routes with the
14125 		 * loopback address as the gateway.  This is primarily done to
14126 		 * set up prefixes with the RTF_REJECT flag set (for example,
14127 		 * when generating aggregate routes.)
14128 		 *
14129 		 * If the IRE type (as defined by ill->ill_net_type) is
14130 		 * IRE_LOOPBACK, then we map the request into a
14131 		 * IRE_IF_NORESOLVER.
14132 		 */
14133 		if (ill->ill_net_type == IRE_LOOPBACK)
14134 			ire->ire_type = IRE_IF_NORESOLVER;
14135 
14136 		/*
14137 		 * ire held by ire_add, will be refreled' towards the
14138 		 * the end of ipif_up_done
14139 		 */
14140 		nire = ire_add(ire);
14141 		/*
14142 		 * Check if it was a duplicate entry. This handles
14143 		 * the case of two racing route adds for the same route
14144 		 */
14145 		if (nire == NULL) {
14146 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14147 		} else if (nire != ire) {
14148 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14149 			    (void *)nire));
14150 			ire_delete(nire);
14151 		} else {
14152 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14153 			    (void *)nire));
14154 		}
14155 		if (nire != NULL)
14156 			ire_refrele(nire);
14157 	}
14158 	mutex_exit(&ill->ill_saved_ire_lock);
14159 	return (0);
14160 }
14161 
14162 /*
14163  * Used to set the netmask and broadcast address to default values when the
14164  * interface is brought up.  (Always called as writer.)
14165  */
14166 static void
14167 ipif_set_default(ipif_t *ipif)
14168 {
14169 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14170 
14171 	if (!ipif->ipif_isv6) {
14172 		/*
14173 		 * Interface holds an IPv4 address. Default
14174 		 * mask is the natural netmask.
14175 		 */
14176 		if (!ipif->ipif_net_mask) {
14177 			ipaddr_t	v4mask;
14178 
14179 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14180 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14181 		}
14182 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14183 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14184 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14185 		} else {
14186 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14187 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14188 		}
14189 		/*
14190 		 * NOTE: SunOS 4.X does this even if the broadcast address
14191 		 * has been already set thus we do the same here.
14192 		 */
14193 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14194 			ipaddr_t	v4addr;
14195 
14196 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14197 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14198 		}
14199 	} else {
14200 		/*
14201 		 * Interface holds an IPv6-only address.  Default
14202 		 * mask is all-ones.
14203 		 */
14204 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14205 			ipif->ipif_v6net_mask = ipv6_all_ones;
14206 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14207 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14208 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14209 		} else {
14210 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14211 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14212 		}
14213 	}
14214 }
14215 
14216 /*
14217  * Return 0 if this address can be used as local address without causing
14218  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14219  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14220  * Note that the same IPv6 link-local address is allowed as long as the ills
14221  * are not on the same link.
14222  */
14223 int
14224 ip_addr_availability_check(ipif_t *new_ipif)
14225 {
14226 	in6_addr_t our_v6addr;
14227 	ill_t *ill;
14228 	ipif_t *ipif;
14229 	ill_walk_context_t ctx;
14230 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14231 
14232 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14233 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14234 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14235 
14236 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14237 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14238 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14239 		return (0);
14240 
14241 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14242 
14243 	if (new_ipif->ipif_isv6)
14244 		ill = ILL_START_WALK_V6(&ctx, ipst);
14245 	else
14246 		ill = ILL_START_WALK_V4(&ctx, ipst);
14247 
14248 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14249 		for (ipif = ill->ill_ipif; ipif != NULL;
14250 		    ipif = ipif->ipif_next) {
14251 			if ((ipif == new_ipif) ||
14252 			    !(ipif->ipif_flags & IPIF_UP) ||
14253 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14254 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14255 			    &our_v6addr))
14256 				continue;
14257 
14258 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14259 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14260 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14261 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14262 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14263 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14264 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14265 				continue;
14266 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14267 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14268 				continue;
14269 			else if (new_ipif->ipif_ill == ill)
14270 				return (EADDRINUSE);
14271 			else
14272 				return (EADDRNOTAVAIL);
14273 		}
14274 	}
14275 
14276 	return (0);
14277 }
14278 
14279 /*
14280  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14281  * IREs for the ipif.
14282  * When the routine returns EINPROGRESS then mp has been consumed and
14283  * the ioctl will be acked from ip_rput_dlpi.
14284  */
14285 int
14286 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14287 {
14288 	ill_t		*ill = ipif->ipif_ill;
14289 	boolean_t 	isv6 = ipif->ipif_isv6;
14290 	int		err = 0;
14291 	boolean_t	success;
14292 	uint_t		ipif_orig_id;
14293 	ip_stack_t	*ipst = ill->ill_ipst;
14294 
14295 	ASSERT(IAM_WRITER_IPIF(ipif));
14296 
14297 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14298 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14299 	    ill_t *, ill, ipif_t *, ipif);
14300 
14301 	/* Shouldn't get here if it is already up. */
14302 	if (ipif->ipif_flags & IPIF_UP)
14303 		return (EALREADY);
14304 
14305 	/*
14306 	 * If this is a request to bring up a data address on an interface
14307 	 * under IPMP, then move the address to its IPMP meta-interface and
14308 	 * try to bring it up.  One complication is that the zeroth ipif for
14309 	 * an ill is special, in that every ill always has one, and that code
14310 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14311 	 */
14312 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14313 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14314 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14315 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14316 
14317 		/*
14318 		 * The ipif being brought up should be quiesced.  If it's not,
14319 		 * something has gone amiss and we need to bail out.  (If it's
14320 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14321 		 */
14322 		mutex_enter(&ill->ill_lock);
14323 		if (!ipif_is_quiescent(ipif)) {
14324 			mutex_exit(&ill->ill_lock);
14325 			return (EINVAL);
14326 		}
14327 		mutex_exit(&ill->ill_lock);
14328 
14329 		/*
14330 		 * If we're going to need to allocate ipifs, do it prior
14331 		 * to starting the move (and grabbing locks).
14332 		 */
14333 		if (ipif->ipif_id == 0) {
14334 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14335 			    B_FALSE, &err)) == NULL) {
14336 				return (err);
14337 			}
14338 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14339 			    B_FALSE, &err)) == NULL) {
14340 				mi_free(moveipif);
14341 				return (err);
14342 			}
14343 		}
14344 
14345 		/*
14346 		 * Grab or transfer the ipif to move.  During the move, keep
14347 		 * ill_g_lock held to prevent any ill walker threads from
14348 		 * seeing things in an inconsistent state.
14349 		 */
14350 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14351 		if (ipif->ipif_id != 0) {
14352 			ipif_remove(ipif);
14353 		} else {
14354 			ipif_transfer(ipif, moveipif, stubipif);
14355 			ipif = moveipif;
14356 		}
14357 
14358 		/*
14359 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14360 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14361 		 * replace that one.  Otherwise, pick the next available slot.
14362 		 */
14363 		ipif->ipif_ill = ipmp_ill;
14364 		ipif_orig_id = ipif->ipif_id;
14365 
14366 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14367 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14368 			ipif = ipmp_ill->ill_ipif;
14369 		} else {
14370 			ipif->ipif_id = -1;
14371 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14372 				/*
14373 				 * No more available ipif_id's -- put it back
14374 				 * on the original ill and fail the operation.
14375 				 * Since we're writer on the ill, we can be
14376 				 * sure our old slot is still available.
14377 				 */
14378 				ipif->ipif_id = ipif_orig_id;
14379 				ipif->ipif_ill = ill;
14380 				if (ipif_orig_id == 0) {
14381 					ipif_transfer(ipif, ill->ill_ipif,
14382 					    NULL);
14383 				} else {
14384 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14385 				}
14386 				rw_exit(&ipst->ips_ill_g_lock);
14387 				return (err);
14388 			}
14389 		}
14390 		rw_exit(&ipst->ips_ill_g_lock);
14391 
14392 		/*
14393 		 * Tell SCTP that the ipif has moved.  Note that even if we
14394 		 * had to allocate a new ipif, the original sequence id was
14395 		 * preserved and therefore SCTP won't know.
14396 		 */
14397 		sctp_move_ipif(ipif, ill, ipmp_ill);
14398 
14399 		/*
14400 		 * If the ipif being brought up was on slot zero, then we
14401 		 * first need to bring up the placeholder we stuck there.  In
14402 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14403 		 * call to ipif_up() itself, if we successfully bring up the
14404 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14405 		 */
14406 		if (ipif_orig_id == 0) {
14407 			ASSERT(ill->ill_move_ipif == NULL);
14408 			ill->ill_move_ipif = ipif;
14409 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14410 				ASSERT(ill->ill_move_ipif == NULL);
14411 			if (err != EINPROGRESS)
14412 				ill->ill_move_ipif = NULL;
14413 			return (err);
14414 		}
14415 
14416 		/*
14417 		 * Bring it up on the IPMP ill.
14418 		 */
14419 		return (ipif_up(ipif, q, mp));
14420 	}
14421 
14422 	/* Skip arp/ndp for any loopback interface. */
14423 	if (ill->ill_wq != NULL) {
14424 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14425 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14426 
14427 		if (!ill->ill_dl_up) {
14428 			/*
14429 			 * ill_dl_up is not yet set. i.e. we are yet to
14430 			 * DL_BIND with the driver and this is the first
14431 			 * logical interface on the ill to become "up".
14432 			 * Tell the driver to get going (via DL_BIND_REQ).
14433 			 * Note that changing "significant" IFF_ flags
14434 			 * address/netmask etc cause a down/up dance, but
14435 			 * does not cause an unbind (DL_UNBIND) with the driver
14436 			 */
14437 			return (ill_dl_up(ill, ipif, mp, q));
14438 		}
14439 
14440 		/*
14441 		 * ipif_resolver_up may end up needeing to bind/attach
14442 		 * the ARP stream, which in turn necessitates a
14443 		 * DLPI message exchange with the driver. ioctls are
14444 		 * serialized and so we cannot send more than one
14445 		 * interface up message at a time. If ipif_resolver_up
14446 		 * does need to wait for the DLPI handshake for the ARP stream,
14447 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14448 		 */
14449 
14450 		ASSERT(connp != NULL || !CONN_Q(q));
14451 		if (connp != NULL)
14452 			mutex_enter(&connp->conn_lock);
14453 		mutex_enter(&ill->ill_lock);
14454 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14455 		mutex_exit(&ill->ill_lock);
14456 		if (connp != NULL)
14457 			mutex_exit(&connp->conn_lock);
14458 		if (!success)
14459 			return (EINTR);
14460 
14461 		/*
14462 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14463 		 * complete when ipif_ndp_up returns.
14464 		 */
14465 		err = ipif_resolver_up(ipif, Res_act_initial);
14466 		if (err == EINPROGRESS) {
14467 			/* We will complete it in arp_bringup_done() */
14468 			return (err);
14469 		}
14470 
14471 		if (isv6 && err == 0)
14472 			err = ipif_ndp_up(ipif, B_TRUE);
14473 
14474 		ASSERT(err != EINPROGRESS);
14475 		mp = ipsq_pending_mp_get(ipsq, &connp);
14476 		ASSERT(mp != NULL);
14477 		if (err != 0)
14478 			return (err);
14479 	} else {
14480 		/*
14481 		 * Interfaces without underlying hardware don't do duplicate
14482 		 * address detection.
14483 		 */
14484 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14485 		ipif->ipif_addr_ready = 1;
14486 		err = ill_add_ires(ill);
14487 		/* allocation failure? */
14488 		if (err != 0)
14489 			return (err);
14490 	}
14491 
14492 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14493 	if (err == 0 && ill->ill_move_ipif != NULL) {
14494 		ipif = ill->ill_move_ipif;
14495 		ill->ill_move_ipif = NULL;
14496 		return (ipif_up(ipif, q, mp));
14497 	}
14498 	return (err);
14499 }
14500 
14501 /*
14502  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14503  * The identical set of IREs need to be removed in ill_delete_ires().
14504  */
14505 int
14506 ill_add_ires(ill_t *ill)
14507 {
14508 	ire_t	*ire;
14509 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14510 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14511 
14512 	if (ill->ill_ire_multicast != NULL)
14513 		return (0);
14514 
14515 	/*
14516 	 * provide some dummy ire_addr for creating the ire.
14517 	 */
14518 	if (ill->ill_isv6) {
14519 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14520 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14521 	} else {
14522 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14523 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14524 	}
14525 	if (ire == NULL)
14526 		return (ENOMEM);
14527 
14528 	ill->ill_ire_multicast = ire;
14529 	return (0);
14530 }
14531 
14532 void
14533 ill_delete_ires(ill_t *ill)
14534 {
14535 	if (ill->ill_ire_multicast != NULL) {
14536 		/*
14537 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14538 		 * which was taken without any th_tracing enabled.
14539 		 * We also mark it as condemned (note that it was never added)
14540 		 * so that caching conn's can move off of it.
14541 		 */
14542 		ire_make_condemned(ill->ill_ire_multicast);
14543 		ire_refrele_notr(ill->ill_ire_multicast);
14544 		ill->ill_ire_multicast = NULL;
14545 	}
14546 }
14547 
14548 /*
14549  * Perform a bind for the physical device.
14550  * When the routine returns EINPROGRESS then mp has been consumed and
14551  * the ioctl will be acked from ip_rput_dlpi.
14552  * Allocate an unbind message and save it until ipif_down.
14553  */
14554 static int
14555 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14556 {
14557 	mblk_t	*bind_mp = NULL;
14558 	mblk_t	*unbind_mp = NULL;
14559 	conn_t	*connp;
14560 	boolean_t success;
14561 	int	err;
14562 
14563 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14564 
14565 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14566 	ASSERT(IAM_WRITER_ILL(ill));
14567 	ASSERT(mp != NULL);
14568 
14569 	/*
14570 	 * Make sure we have an IRE_MULTICAST in case we immediately
14571 	 * start receiving packets.
14572 	 */
14573 	err = ill_add_ires(ill);
14574 	if (err != 0)
14575 		goto bad;
14576 
14577 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14578 	    DL_BIND_REQ);
14579 	if (bind_mp == NULL)
14580 		goto bad;
14581 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14582 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14583 
14584 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14585 	if (unbind_mp == NULL)
14586 		goto bad;
14587 
14588 	/*
14589 	 * Record state needed to complete this operation when the
14590 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14591 	 */
14592 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14593 	ASSERT(connp != NULL || !CONN_Q(q));
14594 	GRAB_CONN_LOCK(q);
14595 	mutex_enter(&ipif->ipif_ill->ill_lock);
14596 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14597 	mutex_exit(&ipif->ipif_ill->ill_lock);
14598 	RELEASE_CONN_LOCK(q);
14599 	if (!success)
14600 		goto bad;
14601 
14602 	/*
14603 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14604 	 * the interface goes down.
14605 	 */
14606 	ASSERT(ill->ill_unbind_mp == NULL);
14607 	ill->ill_unbind_mp = unbind_mp;
14608 
14609 	ill_dlpi_send(ill, bind_mp);
14610 	/* Send down link-layer capabilities probe if not already done. */
14611 	ill_capability_probe(ill);
14612 
14613 	/*
14614 	 * Sysid used to rely on the fact that netboots set domainname
14615 	 * and the like. Now that miniroot boots aren't strictly netboots
14616 	 * and miniroot network configuration is driven from userland
14617 	 * these things still need to be set. This situation can be detected
14618 	 * by comparing the interface being configured here to the one
14619 	 * dhcifname was set to reference by the boot loader. Once sysid is
14620 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14621 	 */
14622 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14623 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14624 	    (strlen(srpc_domain) == 0)) {
14625 		if (dhcpinit() != 0)
14626 			cmn_err(CE_WARN, "no cached dhcp response");
14627 	}
14628 
14629 	/*
14630 	 * This operation will complete in ip_rput_dlpi with either
14631 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14632 	 */
14633 	return (EINPROGRESS);
14634 bad:
14635 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14636 
14637 	freemsg(bind_mp);
14638 	freemsg(unbind_mp);
14639 	return (ENOMEM);
14640 }
14641 
14642 /* Add room for tcp+ip headers */
14643 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14644 
14645 /*
14646  * DLPI and ARP is up.
14647  * Create all the IREs associated with an interface. Bring up multicast.
14648  * Set the interface flag and finish other initialization
14649  * that potentially had to be deferred to after DL_BIND_ACK.
14650  */
14651 int
14652 ipif_up_done(ipif_t *ipif)
14653 {
14654 	ill_t		*ill = ipif->ipif_ill;
14655 	int		err = 0;
14656 	boolean_t	loopback = B_FALSE;
14657 	boolean_t	update_src_selection = B_TRUE;
14658 	ipif_t		*tmp_ipif;
14659 
14660 	ip1dbg(("ipif_up_done(%s:%u)\n",
14661 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14662 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14663 	    ill_t *, ill, ipif_t *, ipif);
14664 
14665 	/* Check if this is a loopback interface */
14666 	if (ipif->ipif_ill->ill_wq == NULL)
14667 		loopback = B_TRUE;
14668 
14669 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14670 
14671 	/*
14672 	 * If all other interfaces for this ill are down or DEPRECATED,
14673 	 * or otherwise unsuitable for source address selection,
14674 	 * reset the src generation numbers to make sure source
14675 	 * address selection gets to take this new ipif into account.
14676 	 * No need to hold ill_lock while traversing the ipif list since
14677 	 * we are writer
14678 	 */
14679 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14680 	    tmp_ipif = tmp_ipif->ipif_next) {
14681 		if (((tmp_ipif->ipif_flags &
14682 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14683 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14684 		    (tmp_ipif == ipif))
14685 			continue;
14686 		/* first useable pre-existing interface */
14687 		update_src_selection = B_FALSE;
14688 		break;
14689 	}
14690 	if (update_src_selection)
14691 		ip_update_source_selection(ill->ill_ipst);
14692 
14693 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14694 		nce_t *loop_nce = NULL;
14695 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14696 
14697 		/*
14698 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14699 		 * ipif_lookup_on_name(), but in the case of zones we can have
14700 		 * several loopback addresses on lo0. So all the interfaces with
14701 		 * loopback addresses need to be marked IRE_LOOPBACK.
14702 		 */
14703 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14704 		    htonl(INADDR_LOOPBACK))
14705 			ipif->ipif_ire_type = IRE_LOOPBACK;
14706 		else
14707 			ipif->ipif_ire_type = IRE_LOCAL;
14708 		if (ill->ill_net_type != IRE_LOOPBACK)
14709 			flags |= NCE_F_PUBLISH;
14710 
14711 		/* add unicast nce for the local addr */
14712 		err = nce_lookup_then_add_v4(ill, NULL,
14713 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14714 		    ND_REACHABLE, &loop_nce);
14715 		/* A shared-IP zone sees EEXIST for lo0:N */
14716 		if (err == 0 || err == EEXIST) {
14717 			ipif->ipif_added_nce = 1;
14718 			loop_nce->nce_ipif_cnt++;
14719 			nce_refrele(loop_nce);
14720 			err = 0;
14721 		} else {
14722 			ASSERT(loop_nce == NULL);
14723 			return (err);
14724 		}
14725 	}
14726 
14727 	/* Create all the IREs associated with this interface */
14728 	err = ipif_add_ires_v4(ipif, loopback);
14729 	if (err != 0) {
14730 		/*
14731 		 * see comments about return value from
14732 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14733 		 */
14734 		if (err != EADDRINUSE) {
14735 			(void) ipif_arp_down(ipif);
14736 		} else {
14737 			/*
14738 			 * Make IPMP aware of the deleted ipif so that
14739 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14740 			 * can be completed. Note that we do not want to
14741 			 * destroy the nce that was created on the ipmp_ill
14742 			 * for the active copy of the duplicate address in
14743 			 * use.
14744 			 */
14745 			if (IS_IPMP(ill))
14746 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14747 			err = EADDRNOTAVAIL;
14748 		}
14749 		return (err);
14750 	}
14751 
14752 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14753 		/* Recover any additional IREs entries for this ill */
14754 		(void) ill_recover_saved_ire(ill);
14755 	}
14756 
14757 	if (ill->ill_need_recover_multicast) {
14758 		/*
14759 		 * Need to recover all multicast memberships in the driver.
14760 		 * This had to be deferred until we had attached.  The same
14761 		 * code exists in ipif_up_done_v6() to recover IPv6
14762 		 * memberships.
14763 		 *
14764 		 * Note that it would be preferable to unconditionally do the
14765 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14766 		 * that since ill_join_allmulti() depends on ill_dl_up being
14767 		 * set, and it is not set until we receive a DL_BIND_ACK after
14768 		 * having called ill_dl_up().
14769 		 */
14770 		ill_recover_multicast(ill);
14771 	}
14772 
14773 	if (ill->ill_ipif_up_count == 1) {
14774 		/*
14775 		 * Since the interface is now up, it may now be active.
14776 		 */
14777 		if (IS_UNDER_IPMP(ill))
14778 			ipmp_ill_refresh_active(ill);
14779 
14780 		/*
14781 		 * If this is an IPMP interface, we may now be able to
14782 		 * establish ARP entries.
14783 		 */
14784 		if (IS_IPMP(ill))
14785 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14786 	}
14787 
14788 	/* Join the allhosts multicast address */
14789 	ipif_multicast_up(ipif);
14790 
14791 	if (!loopback && !update_src_selection &&
14792 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14793 		ip_update_source_selection(ill->ill_ipst);
14794 
14795 	if (!loopback && ipif->ipif_addr_ready) {
14796 		/* Broadcast an address mask reply. */
14797 		ipif_mask_reply(ipif);
14798 	}
14799 	/* Perhaps ilgs should use this ill */
14800 	update_conn_ill(NULL, ill->ill_ipst);
14801 
14802 	/*
14803 	 * This had to be deferred until we had bound.  Tell routing sockets and
14804 	 * others that this interface is up if it looks like the address has
14805 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14806 	 * duplicate address detection to do its thing.
14807 	 */
14808 	if (ipif->ipif_addr_ready)
14809 		ipif_up_notify(ipif);
14810 	return (0);
14811 }
14812 
14813 /*
14814  * Add the IREs associated with the ipif.
14815  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14816  */
14817 static int
14818 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14819 {
14820 	ill_t		*ill = ipif->ipif_ill;
14821 	ip_stack_t	*ipst = ill->ill_ipst;
14822 	ire_t		*ire_array[20];
14823 	ire_t		**irep = ire_array;
14824 	ire_t		**irep1;
14825 	ipaddr_t	net_mask = 0;
14826 	ipaddr_t	subnet_mask, route_mask;
14827 	int		err;
14828 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14829 	ire_t		*ire_if = NULL;
14830 	uchar_t		*gw;
14831 
14832 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14833 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14834 		/*
14835 		 * If we're on a labeled system then make sure that zone-
14836 		 * private addresses have proper remote host database entries.
14837 		 */
14838 		if (is_system_labeled() &&
14839 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14840 		    !tsol_check_interface_address(ipif))
14841 			return (EINVAL);
14842 
14843 		/* Register the source address for __sin6_src_id */
14844 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14845 		    ipif->ipif_zoneid, ipst);
14846 		if (err != 0) {
14847 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14848 			return (err);
14849 		}
14850 
14851 		if (loopback)
14852 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14853 		else
14854 			gw = NULL;
14855 
14856 		/* If the interface address is set, create the local IRE. */
14857 		ire_local = ire_create(
14858 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14859 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14860 		    gw,					/* gateway */
14861 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14862 		    ipif->ipif_ill,
14863 		    ipif->ipif_zoneid,
14864 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14865 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14866 		    NULL,
14867 		    ipst);
14868 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14869 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14870 		    ipif->ipif_ire_type,
14871 		    ntohl(ipif->ipif_lcl_addr)));
14872 		if (ire_local == NULL) {
14873 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14874 			err = ENOMEM;
14875 			goto bad;
14876 		}
14877 	} else {
14878 		ip1dbg((
14879 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14880 		    ipif->ipif_ire_type,
14881 		    ntohl(ipif->ipif_lcl_addr),
14882 		    (uint_t)ipif->ipif_flags));
14883 	}
14884 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14885 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14886 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14887 	} else {
14888 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14889 	}
14890 
14891 	subnet_mask = ipif->ipif_net_mask;
14892 
14893 	/*
14894 	 * If mask was not specified, use natural netmask of
14895 	 * interface address. Also, store this mask back into the
14896 	 * ipif struct.
14897 	 */
14898 	if (subnet_mask == 0) {
14899 		subnet_mask = net_mask;
14900 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14901 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14902 		    ipif->ipif_v6subnet);
14903 	}
14904 
14905 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14906 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14907 	    ipif->ipif_subnet != INADDR_ANY) {
14908 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14909 
14910 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14911 			route_mask = IP_HOST_MASK;
14912 		} else {
14913 			route_mask = subnet_mask;
14914 		}
14915 
14916 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14917 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14918 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14919 		    ntohl(ipif->ipif_subnet)));
14920 		ire_if = ire_create(
14921 		    (uchar_t *)&ipif->ipif_subnet,
14922 		    (uchar_t *)&route_mask,
14923 		    (uchar_t *)&ipif->ipif_lcl_addr,
14924 		    ill->ill_net_type,
14925 		    ill,
14926 		    ipif->ipif_zoneid,
14927 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14928 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14929 		    NULL,
14930 		    ipst);
14931 		if (ire_if == NULL) {
14932 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14933 			err = ENOMEM;
14934 			goto bad;
14935 		}
14936 	}
14937 
14938 	/*
14939 	 * Create any necessary broadcast IREs.
14940 	 */
14941 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14942 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14943 		irep = ipif_create_bcast_ires(ipif, irep);
14944 
14945 	/* If an earlier ire_create failed, get out now */
14946 	for (irep1 = irep; irep1 > ire_array; ) {
14947 		irep1--;
14948 		if (*irep1 == NULL) {
14949 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14950 			err = ENOMEM;
14951 			goto bad;
14952 		}
14953 	}
14954 
14955 	/*
14956 	 * Need to atomically check for IP address availability under
14957 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14958 	 * ills or new ipifs can be added while we are checking availability.
14959 	 */
14960 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14961 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14962 	/* Mark it up, and increment counters. */
14963 	ipif->ipif_flags |= IPIF_UP;
14964 	ill->ill_ipif_up_count++;
14965 	err = ip_addr_availability_check(ipif);
14966 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14967 	rw_exit(&ipst->ips_ill_g_lock);
14968 
14969 	if (err != 0) {
14970 		/*
14971 		 * Our address may already be up on the same ill. In this case,
14972 		 * the ARP entry for our ipif replaced the one for the other
14973 		 * ipif. So we don't want to delete it (otherwise the other ipif
14974 		 * would be unable to send packets).
14975 		 * ip_addr_availability_check() identifies this case for us and
14976 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14977 		 * which is the expected error code.
14978 		 */
14979 		ill->ill_ipif_up_count--;
14980 		ipif->ipif_flags &= ~IPIF_UP;
14981 		goto bad;
14982 	}
14983 
14984 	/*
14985 	 * Add in all newly created IREs.  ire_create_bcast() has
14986 	 * already checked for duplicates of the IRE_BROADCAST type.
14987 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14988 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14989 	 * a /32 route.
14990 	 */
14991 	if (ire_if != NULL) {
14992 		ire_if = ire_add(ire_if);
14993 		if (ire_if == NULL) {
14994 			err = ENOMEM;
14995 			goto bad2;
14996 		}
14997 #ifdef DEBUG
14998 		ire_refhold_notr(ire_if);
14999 		ire_refrele(ire_if);
15000 #endif
15001 	}
15002 	if (ire_local != NULL) {
15003 		ire_local = ire_add(ire_local);
15004 		if (ire_local == NULL) {
15005 			err = ENOMEM;
15006 			goto bad2;
15007 		}
15008 #ifdef DEBUG
15009 		ire_refhold_notr(ire_local);
15010 		ire_refrele(ire_local);
15011 #endif
15012 	}
15013 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15014 	if (ire_local != NULL)
15015 		ipif->ipif_ire_local = ire_local;
15016 	if (ire_if != NULL)
15017 		ipif->ipif_ire_if = ire_if;
15018 	rw_exit(&ipst->ips_ill_g_lock);
15019 	ire_local = NULL;
15020 	ire_if = NULL;
15021 
15022 	/*
15023 	 * We first add all of them, and if that succeeds we refrele the
15024 	 * bunch. That enables us to delete all of them should any of the
15025 	 * ire_adds fail.
15026 	 */
15027 	for (irep1 = irep; irep1 > ire_array; ) {
15028 		irep1--;
15029 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15030 		*irep1 = ire_add(*irep1);
15031 		if (*irep1 == NULL) {
15032 			err = ENOMEM;
15033 			goto bad2;
15034 		}
15035 	}
15036 
15037 	for (irep1 = irep; irep1 > ire_array; ) {
15038 		irep1--;
15039 		/* refheld by ire_add. */
15040 		if (*irep1 != NULL) {
15041 			ire_refrele(*irep1);
15042 			*irep1 = NULL;
15043 		}
15044 	}
15045 
15046 	if (!loopback) {
15047 		/*
15048 		 * If the broadcast address has been set, make sure it makes
15049 		 * sense based on the interface address.
15050 		 * Only match on ill since we are sharing broadcast addresses.
15051 		 */
15052 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15053 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
15054 			ire_t	*ire;
15055 
15056 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15057 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15058 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15059 
15060 			if (ire == NULL) {
15061 				/*
15062 				 * If there isn't a matching broadcast IRE,
15063 				 * revert to the default for this netmask.
15064 				 */
15065 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
15066 				mutex_enter(&ipif->ipif_ill->ill_lock);
15067 				ipif_set_default(ipif);
15068 				mutex_exit(&ipif->ipif_ill->ill_lock);
15069 			} else {
15070 				ire_refrele(ire);
15071 			}
15072 		}
15073 
15074 	}
15075 	return (0);
15076 
15077 bad2:
15078 	ill->ill_ipif_up_count--;
15079 	ipif->ipif_flags &= ~IPIF_UP;
15080 
15081 bad:
15082 	ip1dbg(("ipif_add_ires: FAILED \n"));
15083 	if (ire_local != NULL)
15084 		ire_delete(ire_local);
15085 	if (ire_if != NULL)
15086 		ire_delete(ire_if);
15087 
15088 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15089 	ire_local = ipif->ipif_ire_local;
15090 	ipif->ipif_ire_local = NULL;
15091 	ire_if = ipif->ipif_ire_if;
15092 	ipif->ipif_ire_if = NULL;
15093 	rw_exit(&ipst->ips_ill_g_lock);
15094 	if (ire_local != NULL) {
15095 		ire_delete(ire_local);
15096 		ire_refrele_notr(ire_local);
15097 	}
15098 	if (ire_if != NULL) {
15099 		ire_delete(ire_if);
15100 		ire_refrele_notr(ire_if);
15101 	}
15102 
15103 	while (irep > ire_array) {
15104 		irep--;
15105 		if (*irep != NULL) {
15106 			ire_delete(*irep);
15107 		}
15108 	}
15109 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15110 
15111 	return (err);
15112 }
15113 
15114 /* Remove all the IREs created by ipif_add_ires_v4 */
15115 void
15116 ipif_delete_ires_v4(ipif_t *ipif)
15117 {
15118 	ill_t		*ill = ipif->ipif_ill;
15119 	ip_stack_t	*ipst = ill->ill_ipst;
15120 	ire_t		*ire;
15121 
15122 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15123 	ire = ipif->ipif_ire_local;
15124 	ipif->ipif_ire_local = NULL;
15125 	rw_exit(&ipst->ips_ill_g_lock);
15126 	if (ire != NULL) {
15127 		/*
15128 		 * Move count to ipif so we don't loose the count due to
15129 		 * a down/up dance.
15130 		 */
15131 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15132 
15133 		ire_delete(ire);
15134 		ire_refrele_notr(ire);
15135 	}
15136 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15137 	ire = ipif->ipif_ire_if;
15138 	ipif->ipif_ire_if = NULL;
15139 	rw_exit(&ipst->ips_ill_g_lock);
15140 	if (ire != NULL) {
15141 		ire_delete(ire);
15142 		ire_refrele_notr(ire);
15143 	}
15144 
15145 	/*
15146 	 * Delete the broadcast IREs.
15147 	 */
15148 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15149 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15150 		ipif_delete_bcast_ires(ipif);
15151 }
15152 
15153 /*
15154  * Checks for availbility of a usable source address (if there is one) when the
15155  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15156  * this selection is done regardless of the destination.
15157  */
15158 boolean_t
15159 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15160     ip_stack_t *ipst)
15161 {
15162 	ipif_t		*ipif = NULL;
15163 	ill_t		*uill;
15164 
15165 	ASSERT(ifindex != 0);
15166 
15167 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15168 	if (uill == NULL)
15169 		return (B_FALSE);
15170 
15171 	mutex_enter(&uill->ill_lock);
15172 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15173 		if (IPIF_IS_CONDEMNED(ipif))
15174 			continue;
15175 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15176 			continue;
15177 		if (!(ipif->ipif_flags & IPIF_UP))
15178 			continue;
15179 		if (ipif->ipif_zoneid != zoneid)
15180 			continue;
15181 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15182 		    ipif->ipif_lcl_addr == INADDR_ANY)
15183 			continue;
15184 		mutex_exit(&uill->ill_lock);
15185 		ill_refrele(uill);
15186 		return (B_TRUE);
15187 	}
15188 	mutex_exit(&uill->ill_lock);
15189 	ill_refrele(uill);
15190 	return (B_FALSE);
15191 }
15192 
15193 /*
15194  * Find an ipif with a good local address on the ill+zoneid.
15195  */
15196 ipif_t *
15197 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15198 {
15199 	ipif_t		*ipif;
15200 
15201 	mutex_enter(&ill->ill_lock);
15202 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15203 		if (IPIF_IS_CONDEMNED(ipif))
15204 			continue;
15205 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15206 			continue;
15207 		if (!(ipif->ipif_flags & IPIF_UP))
15208 			continue;
15209 		if (ipif->ipif_zoneid != zoneid &&
15210 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15211 			continue;
15212 		if (ill->ill_isv6 ?
15213 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15214 		    ipif->ipif_lcl_addr == INADDR_ANY)
15215 			continue;
15216 		ipif_refhold_locked(ipif);
15217 		mutex_exit(&ill->ill_lock);
15218 		return (ipif);
15219 	}
15220 	mutex_exit(&ill->ill_lock);
15221 	return (NULL);
15222 }
15223 
15224 /*
15225  * IP source address type, sorted from worst to best.  For a given type,
15226  * always prefer IP addresses on the same subnet.  All-zones addresses are
15227  * suboptimal because they pose problems with unlabeled destinations.
15228  */
15229 typedef enum {
15230 	IPIF_NONE,
15231 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
15232 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
15233 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15234 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15235 	IPIF_DIFFNET,			/* normal and different subnet */
15236 	IPIF_SAMENET,			/* normal and same subnet */
15237 	IPIF_LOCALADDR			/* local loopback */
15238 } ipif_type_t;
15239 
15240 /*
15241  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15242  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15243  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15244  * the first one, unless IPMP is used in which case we round-robin among them;
15245  * see below for more.
15246  *
15247  * Returns NULL if there is no suitable source address for the ill.
15248  * This only occurs when there is no valid source address for the ill.
15249  */
15250 ipif_t *
15251 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15252     boolean_t allow_usesrc, boolean_t *notreadyp)
15253 {
15254 	ill_t	*usill = NULL;
15255 	ill_t	*ipmp_ill = NULL;
15256 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15257 	ipif_type_t type, best_type;
15258 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15259 	ip_stack_t *ipst = ill->ill_ipst;
15260 	boolean_t samenet;
15261 
15262 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15263 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15264 		    B_FALSE, ipst);
15265 		if (usill != NULL)
15266 			ill = usill;	/* Select source from usesrc ILL */
15267 		else
15268 			return (NULL);
15269 	}
15270 
15271 	/*
15272 	 * Test addresses should never be used for source address selection,
15273 	 * so if we were passed one, switch to the IPMP meta-interface.
15274 	 */
15275 	if (IS_UNDER_IPMP(ill)) {
15276 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15277 			ill = ipmp_ill;	/* Select source from IPMP ill */
15278 		else
15279 			return (NULL);
15280 	}
15281 
15282 	/*
15283 	 * If we're dealing with an unlabeled destination on a labeled system,
15284 	 * make sure that we ignore source addresses that are incompatible with
15285 	 * the destination's default label.  That destination's default label
15286 	 * must dominate the minimum label on the source address.
15287 	 */
15288 	dst_rhtp = NULL;
15289 	if (is_system_labeled()) {
15290 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15291 		if (dst_rhtp == NULL)
15292 			return (NULL);
15293 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15294 			TPC_RELE(dst_rhtp);
15295 			dst_rhtp = NULL;
15296 		}
15297 	}
15298 
15299 	/*
15300 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15301 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15302 	 * After selecting the right ipif, under ill_lock make sure ipif is
15303 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15304 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15305 	 * but not under a lock.
15306 	 */
15307 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15308 retry:
15309 	/*
15310 	 * For source address selection, we treat the ipif list as circular
15311 	 * and continue until we get back to where we started.  This allows
15312 	 * IPMP to vary source address selection (which improves inbound load
15313 	 * spreading) by caching its last ending point and starting from
15314 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15315 	 * ills since that can't happen on the IPMP ill.
15316 	 */
15317 	start_ipif = ill->ill_ipif;
15318 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15319 		start_ipif = ill->ill_src_ipif;
15320 
15321 	ipif = start_ipif;
15322 	best_ipif = NULL;
15323 	best_type = IPIF_NONE;
15324 	do {
15325 		if ((next_ipif = ipif->ipif_next) == NULL)
15326 			next_ipif = ill->ill_ipif;
15327 
15328 		if (IPIF_IS_CONDEMNED(ipif))
15329 			continue;
15330 		/* Always skip NOLOCAL and ANYCAST interfaces */
15331 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15332 			continue;
15333 		/* Always skip NOACCEPT interfaces */
15334 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15335 			continue;
15336 		if (!(ipif->ipif_flags & IPIF_UP))
15337 			continue;
15338 
15339 		if (!ipif->ipif_addr_ready) {
15340 			if (notreadyp != NULL)
15341 				*notreadyp = B_TRUE;
15342 			continue;
15343 		}
15344 
15345 		if (zoneid != ALL_ZONES &&
15346 		    ipif->ipif_zoneid != zoneid &&
15347 		    ipif->ipif_zoneid != ALL_ZONES)
15348 			continue;
15349 
15350 		/*
15351 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15352 		 * are not valid as source addresses.
15353 		 */
15354 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15355 			continue;
15356 
15357 		/*
15358 		 * Check compatibility of local address for destination's
15359 		 * default label if we're on a labeled system.	Incompatible
15360 		 * addresses can't be used at all.
15361 		 */
15362 		if (dst_rhtp != NULL) {
15363 			boolean_t incompat;
15364 
15365 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15366 			    IPV4_VERSION, B_FALSE);
15367 			if (src_rhtp == NULL)
15368 				continue;
15369 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15370 			    src_rhtp->tpc_tp.tp_doi !=
15371 			    dst_rhtp->tpc_tp.tp_doi ||
15372 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15373 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15374 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15375 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15376 			TPC_RELE(src_rhtp);
15377 			if (incompat)
15378 				continue;
15379 		}
15380 
15381 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15382 
15383 		if (ipif->ipif_lcl_addr == dst) {
15384 			type = IPIF_LOCALADDR;
15385 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15386 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15387 			    IPIF_DIFFNET_DEPRECATED;
15388 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15389 			type = samenet ? IPIF_SAMENET_ALLZONES :
15390 			    IPIF_DIFFNET_ALLZONES;
15391 		} else {
15392 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15393 		}
15394 
15395 		if (type > best_type) {
15396 			best_type = type;
15397 			best_ipif = ipif;
15398 			if (best_type == IPIF_LOCALADDR)
15399 				break; /* can't get better */
15400 		}
15401 	} while ((ipif = next_ipif) != start_ipif);
15402 
15403 	if ((ipif = best_ipif) != NULL) {
15404 		mutex_enter(&ipif->ipif_ill->ill_lock);
15405 		if (IPIF_IS_CONDEMNED(ipif)) {
15406 			mutex_exit(&ipif->ipif_ill->ill_lock);
15407 			goto retry;
15408 		}
15409 		ipif_refhold_locked(ipif);
15410 
15411 		/*
15412 		 * For IPMP, update the source ipif rotor to the next ipif,
15413 		 * provided we can look it up.  (We must not use it if it's
15414 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15415 		 * ipif_free() checked ill_src_ipif.)
15416 		 */
15417 		if (IS_IPMP(ill) && ipif != NULL) {
15418 			next_ipif = ipif->ipif_next;
15419 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15420 				ill->ill_src_ipif = next_ipif;
15421 			else
15422 				ill->ill_src_ipif = NULL;
15423 		}
15424 		mutex_exit(&ipif->ipif_ill->ill_lock);
15425 	}
15426 
15427 	rw_exit(&ipst->ips_ill_g_lock);
15428 	if (usill != NULL)
15429 		ill_refrele(usill);
15430 	if (ipmp_ill != NULL)
15431 		ill_refrele(ipmp_ill);
15432 	if (dst_rhtp != NULL)
15433 		TPC_RELE(dst_rhtp);
15434 
15435 #ifdef DEBUG
15436 	if (ipif == NULL) {
15437 		char buf1[INET6_ADDRSTRLEN];
15438 
15439 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15440 		    ill->ill_name,
15441 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15442 	} else {
15443 		char buf1[INET6_ADDRSTRLEN];
15444 		char buf2[INET6_ADDRSTRLEN];
15445 
15446 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15447 		    ipif->ipif_ill->ill_name,
15448 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15449 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15450 		    buf2, sizeof (buf2))));
15451 	}
15452 #endif /* DEBUG */
15453 	return (ipif);
15454 }
15455 
15456 /*
15457  * Pick a source address based on the destination ill and an optional setsrc
15458  * address.
15459  * The result is stored in srcp. If generation is set, then put the source
15460  * generation number there before we look for the source address (to avoid
15461  * missing changes in the set of source addresses.
15462  * If flagsp is set, then us it to pass back ipif_flags.
15463  *
15464  * If the caller wants to cache the returned source address and detect when
15465  * that might be stale, the caller should pass in a generation argument,
15466  * which the caller can later compare against ips_src_generation
15467  *
15468  * The precedence order for selecting an IPv4 source address is:
15469  *  - RTF_SETSRC on the offlink ire always wins.
15470  *  - If usrsrc is set, swap the ill to be the usesrc one.
15471  *  - If IPMP is used on the ill, select a random address from the most
15472  *    preferred ones below:
15473  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15474  * 2. Not deprecated, not ALL_ZONES
15475  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15476  * 4. Not deprecated, ALL_ZONES
15477  * 5. If onlink destination, same subnet and deprecated
15478  * 6. Deprecated.
15479  *
15480  * We have lower preference for ALL_ZONES IP addresses,
15481  * as they pose problems with unlabeled destinations.
15482  *
15483  * Note that when multiple IP addresses match e.g., #1 we pick
15484  * the first one if IPMP is not in use. With IPMP we randomize.
15485  */
15486 int
15487 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15488     ipaddr_t multicast_ifaddr,
15489     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15490     uint32_t *generation, uint64_t *flagsp)
15491 {
15492 	ipif_t *ipif;
15493 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15494 
15495 	if (flagsp != NULL)
15496 		*flagsp = 0;
15497 
15498 	/*
15499 	 * Need to grab the generation number before we check to
15500 	 * avoid a race with a change to the set of local addresses.
15501 	 * No lock needed since the thread which updates the set of local
15502 	 * addresses use ipif/ill locks and exit those (hence a store memory
15503 	 * barrier) before doing the atomic increase of ips_src_generation.
15504 	 */
15505 	if (generation != NULL) {
15506 		*generation = ipst->ips_src_generation;
15507 	}
15508 
15509 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15510 		*srcp = multicast_ifaddr;
15511 		return (0);
15512 	}
15513 
15514 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15515 	if (setsrc != INADDR_ANY) {
15516 		*srcp = setsrc;
15517 		return (0);
15518 	}
15519 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15520 	if (ipif == NULL) {
15521 		if (notready)
15522 			return (ENETDOWN);
15523 		else
15524 			return (EADDRNOTAVAIL);
15525 	}
15526 	*srcp = ipif->ipif_lcl_addr;
15527 	if (flagsp != NULL)
15528 		*flagsp = ipif->ipif_flags;
15529 	ipif_refrele(ipif);
15530 	return (0);
15531 }
15532 
15533 /* ARGSUSED */
15534 int
15535 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15536 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15537 {
15538 	/*
15539 	 * ill_phyint_reinit merged the v4 and v6 into a single
15540 	 * ipsq.  We might not have been able to complete the
15541 	 * operation in ipif_set_values, if we could not become
15542 	 * exclusive.  If so restart it here.
15543 	 */
15544 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15545 }
15546 
15547 /*
15548  * Can operate on either a module or a driver queue.
15549  * Returns an error if not a module queue.
15550  */
15551 /* ARGSUSED */
15552 int
15553 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15554     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15555 {
15556 	queue_t		*q1 = q;
15557 	char 		*cp;
15558 	char		interf_name[LIFNAMSIZ];
15559 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15560 
15561 	if (q->q_next == NULL) {
15562 		ip1dbg((
15563 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15564 		return (EINVAL);
15565 	}
15566 
15567 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15568 		return (EALREADY);
15569 
15570 	do {
15571 		q1 = q1->q_next;
15572 	} while (q1->q_next);
15573 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15574 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15575 
15576 	/*
15577 	 * Here we are not going to delay the ioack until after
15578 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15579 	 * original ioctl message before sending the requests.
15580 	 */
15581 	return (ipif_set_values(q, mp, interf_name, &ppa));
15582 }
15583 
15584 /* ARGSUSED */
15585 int
15586 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15587     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15588 {
15589 	return (ENXIO);
15590 }
15591 
15592 /*
15593  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15594  * `irep'.  Returns a pointer to the next free `irep' entry
15595  * A mirror exists in ipif_delete_bcast_ires().
15596  *
15597  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15598  * done in ire_add.
15599  */
15600 static ire_t **
15601 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15602 {
15603 	ipaddr_t addr;
15604 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15605 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15606 	ill_t *ill = ipif->ipif_ill;
15607 	zoneid_t zoneid = ipif->ipif_zoneid;
15608 
15609 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15610 
15611 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15612 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15613 
15614 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15615 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15616 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15617 
15618 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15619 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15620 
15621 	/*
15622 	 * For backward compatibility, we create net broadcast IREs based on
15623 	 * the old "IP address class system", since some old machines only
15624 	 * respond to these class derived net broadcast.  However, we must not
15625 	 * create these net broadcast IREs if the subnetmask is shorter than
15626 	 * the IP address class based derived netmask.  Otherwise, we may
15627 	 * create a net broadcast address which is the same as an IP address
15628 	 * on the subnet -- and then TCP will refuse to talk to that address.
15629 	 */
15630 	if (netmask < subnetmask) {
15631 		addr = netmask & ipif->ipif_subnet;
15632 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15633 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15634 	}
15635 
15636 	/*
15637 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15638 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15639 	 * created.  Creating these broadcast IREs will only create confusion
15640 	 * as `addr' will be the same as the IP address.
15641 	 */
15642 	if (subnetmask != 0xFFFFFFFF) {
15643 		addr = ipif->ipif_subnet;
15644 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15645 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15646 	}
15647 
15648 	return (irep);
15649 }
15650 
15651 /*
15652  * Mirror of ipif_create_bcast_ires()
15653  */
15654 static void
15655 ipif_delete_bcast_ires(ipif_t *ipif)
15656 {
15657 	ipaddr_t	addr;
15658 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15659 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15660 	ill_t		*ill = ipif->ipif_ill;
15661 	zoneid_t	zoneid = ipif->ipif_zoneid;
15662 	ire_t		*ire;
15663 
15664 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15665 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15666 
15667 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15668 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15669 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15670 
15671 	ire = ire_lookup_bcast(ill, 0, zoneid);
15672 	ASSERT(ire != NULL);
15673 	ire_delete(ire); ire_refrele(ire);
15674 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15675 	ASSERT(ire != NULL);
15676 	ire_delete(ire); ire_refrele(ire);
15677 
15678 	/*
15679 	 * For backward compatibility, we create net broadcast IREs based on
15680 	 * the old "IP address class system", since some old machines only
15681 	 * respond to these class derived net broadcast.  However, we must not
15682 	 * create these net broadcast IREs if the subnetmask is shorter than
15683 	 * the IP address class based derived netmask.  Otherwise, we may
15684 	 * create a net broadcast address which is the same as an IP address
15685 	 * on the subnet -- and then TCP will refuse to talk to that address.
15686 	 */
15687 	if (netmask < subnetmask) {
15688 		addr = netmask & ipif->ipif_subnet;
15689 		ire = ire_lookup_bcast(ill, addr, zoneid);
15690 		ASSERT(ire != NULL);
15691 		ire_delete(ire); ire_refrele(ire);
15692 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15693 		ASSERT(ire != NULL);
15694 		ire_delete(ire); ire_refrele(ire);
15695 	}
15696 
15697 	/*
15698 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15699 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15700 	 * created.  Creating these broadcast IREs will only create confusion
15701 	 * as `addr' will be the same as the IP address.
15702 	 */
15703 	if (subnetmask != 0xFFFFFFFF) {
15704 		addr = ipif->ipif_subnet;
15705 		ire = ire_lookup_bcast(ill, addr, zoneid);
15706 		ASSERT(ire != NULL);
15707 		ire_delete(ire); ire_refrele(ire);
15708 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15709 		ASSERT(ire != NULL);
15710 		ire_delete(ire); ire_refrele(ire);
15711 	}
15712 }
15713 
15714 /*
15715  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15716  * from lifr_flags and the name from lifr_name.
15717  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15718  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15719  * Returns EINPROGRESS when mp has been consumed by queueing it on
15720  * ipx_pending_mp and the ioctl will complete in ip_rput.
15721  *
15722  * Can operate on either a module or a driver queue.
15723  * Returns an error if not a module queue.
15724  */
15725 /* ARGSUSED */
15726 int
15727 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15728     ip_ioctl_cmd_t *ipip, void *if_req)
15729 {
15730 	ill_t	*ill = q->q_ptr;
15731 	phyint_t *phyi;
15732 	ip_stack_t *ipst;
15733 	struct lifreq *lifr = if_req;
15734 	uint64_t new_flags;
15735 
15736 	ASSERT(ipif != NULL);
15737 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15738 
15739 	if (q->q_next == NULL) {
15740 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15741 		return (EINVAL);
15742 	}
15743 
15744 	/*
15745 	 * If we are not writer on 'q' then this interface exists already
15746 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15747 	 * so return EALREADY.
15748 	 */
15749 	if (ill != ipif->ipif_ill)
15750 		return (EALREADY);
15751 
15752 	if (ill->ill_name[0] != '\0')
15753 		return (EALREADY);
15754 
15755 	/*
15756 	 * If there's another ill already with the requested name, ensure
15757 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15758 	 * fuse together two unrelated ills, which will cause chaos.
15759 	 */
15760 	ipst = ill->ill_ipst;
15761 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15762 	    lifr->lifr_name, NULL);
15763 	if (phyi != NULL) {
15764 		ill_t *ill_mate = phyi->phyint_illv4;
15765 
15766 		if (ill_mate == NULL)
15767 			ill_mate = phyi->phyint_illv6;
15768 		ASSERT(ill_mate != NULL);
15769 
15770 		if (ill_mate->ill_media->ip_m_mac_type !=
15771 		    ill->ill_media->ip_m_mac_type) {
15772 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15773 			    "use the same ill name on differing media\n"));
15774 			return (EINVAL);
15775 		}
15776 	}
15777 
15778 	/*
15779 	 * We start off as IFF_IPV4 in ipif_allocate and become
15780 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15781 	 * The only flags that we read from user space are IFF_IPV4,
15782 	 * IFF_IPV6, and IFF_BROADCAST.
15783 	 *
15784 	 * This ill has not been inserted into the global list.
15785 	 * So we are still single threaded and don't need any lock
15786 	 *
15787 	 * Saniy check the flags.
15788 	 */
15789 
15790 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15791 	    ((lifr->lifr_flags & IFF_IPV6) ||
15792 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15793 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15794 		    "or IPv6 i.e., no broadcast \n"));
15795 		return (EINVAL);
15796 	}
15797 
15798 	new_flags =
15799 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15800 
15801 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15802 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15803 		    "IFF_IPV4 or IFF_IPV6\n"));
15804 		return (EINVAL);
15805 	}
15806 
15807 	/*
15808 	 * We always start off as IPv4, so only need to check for IPv6.
15809 	 */
15810 	if ((new_flags & IFF_IPV6) != 0) {
15811 		ill->ill_flags |= ILLF_IPV6;
15812 		ill->ill_flags &= ~ILLF_IPV4;
15813 
15814 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15815 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15816 	}
15817 
15818 	if ((new_flags & IFF_BROADCAST) != 0)
15819 		ipif->ipif_flags |= IPIF_BROADCAST;
15820 	else
15821 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15822 
15823 	/* We started off as V4. */
15824 	if (ill->ill_flags & ILLF_IPV6) {
15825 		ill->ill_phyint->phyint_illv6 = ill;
15826 		ill->ill_phyint->phyint_illv4 = NULL;
15827 	}
15828 
15829 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15830 }
15831 
15832 /* ARGSUSED */
15833 int
15834 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15835     ip_ioctl_cmd_t *ipip, void *if_req)
15836 {
15837 	/*
15838 	 * ill_phyint_reinit merged the v4 and v6 into a single
15839 	 * ipsq.  We might not have been able to complete the
15840 	 * slifname in ipif_set_values, if we could not become
15841 	 * exclusive.  If so restart it here
15842 	 */
15843 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15844 }
15845 
15846 /*
15847  * Return a pointer to the ipif which matches the index, IP version type and
15848  * zoneid.
15849  */
15850 ipif_t *
15851 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15852     ip_stack_t *ipst)
15853 {
15854 	ill_t	*ill;
15855 	ipif_t	*ipif = NULL;
15856 
15857 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15858 	if (ill != NULL) {
15859 		mutex_enter(&ill->ill_lock);
15860 		for (ipif = ill->ill_ipif; ipif != NULL;
15861 		    ipif = ipif->ipif_next) {
15862 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15863 			    zoneid == ipif->ipif_zoneid ||
15864 			    ipif->ipif_zoneid == ALL_ZONES)) {
15865 				ipif_refhold_locked(ipif);
15866 				break;
15867 			}
15868 		}
15869 		mutex_exit(&ill->ill_lock);
15870 		ill_refrele(ill);
15871 	}
15872 	return (ipif);
15873 }
15874 
15875 /*
15876  * Change an existing physical interface's index. If the new index
15877  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15878  * Finally, we update other systems which may have a dependence on the
15879  * index value.
15880  */
15881 /* ARGSUSED */
15882 int
15883 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15884     ip_ioctl_cmd_t *ipip, void *ifreq)
15885 {
15886 	ill_t		*ill;
15887 	phyint_t	*phyi;
15888 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15889 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15890 	uint_t	old_index, index;
15891 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15892 	avl_index_t	where;
15893 
15894 	if (ipip->ipi_cmd_type == IF_CMD)
15895 		index = ifr->ifr_index;
15896 	else
15897 		index = lifr->lifr_index;
15898 
15899 	/*
15900 	 * Only allow on physical interface. Also, index zero is illegal.
15901 	 */
15902 	ill = ipif->ipif_ill;
15903 	phyi = ill->ill_phyint;
15904 	if (ipif->ipif_id != 0 || index == 0) {
15905 		return (EINVAL);
15906 	}
15907 
15908 	/* If the index is not changing, no work to do */
15909 	if (phyi->phyint_ifindex == index)
15910 		return (0);
15911 
15912 	/*
15913 	 * Use phyint_exists() to determine if the new interface index
15914 	 * is already in use. If the index is unused then we need to
15915 	 * change the phyint's position in the phyint_list_avl_by_index
15916 	 * tree. If we do not do this, subsequent lookups (using the new
15917 	 * index value) will not find the phyint.
15918 	 */
15919 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15920 	if (phyint_exists(index, ipst)) {
15921 		rw_exit(&ipst->ips_ill_g_lock);
15922 		return (EEXIST);
15923 	}
15924 
15925 	/*
15926 	 * The new index is unused. Set it in the phyint. However we must not
15927 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15928 	 * changes. The event must be bound to old ifindex value.
15929 	 */
15930 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15931 	    &index, sizeof (index));
15932 
15933 	old_index = phyi->phyint_ifindex;
15934 	phyi->phyint_ifindex = index;
15935 
15936 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15937 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15938 	    &index, &where);
15939 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15940 	    phyi, where);
15941 	rw_exit(&ipst->ips_ill_g_lock);
15942 
15943 	/* Update SCTP's ILL list */
15944 	sctp_ill_reindex(ill, old_index);
15945 
15946 	/* Send the routing sockets message */
15947 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15948 	if (ILL_OTHER(ill))
15949 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15950 
15951 	/* Perhaps ilgs should use this ill */
15952 	update_conn_ill(NULL, ill->ill_ipst);
15953 	return (0);
15954 }
15955 
15956 /* ARGSUSED */
15957 int
15958 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15959     ip_ioctl_cmd_t *ipip, void *ifreq)
15960 {
15961 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15962 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15963 
15964 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15965 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15966 	/* Get the interface index */
15967 	if (ipip->ipi_cmd_type == IF_CMD) {
15968 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15969 	} else {
15970 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15971 	}
15972 	return (0);
15973 }
15974 
15975 /* ARGSUSED */
15976 int
15977 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15978     ip_ioctl_cmd_t *ipip, void *ifreq)
15979 {
15980 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15981 
15982 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15983 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15984 	/* Get the interface zone */
15985 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15986 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15987 	return (0);
15988 }
15989 
15990 /*
15991  * Set the zoneid of an interface.
15992  */
15993 /* ARGSUSED */
15994 int
15995 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15996     ip_ioctl_cmd_t *ipip, void *ifreq)
15997 {
15998 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15999 	int err = 0;
16000 	boolean_t need_up = B_FALSE;
16001 	zone_t *zptr;
16002 	zone_status_t status;
16003 	zoneid_t zoneid;
16004 
16005 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16006 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16007 		if (!is_system_labeled())
16008 			return (ENOTSUP);
16009 		zoneid = GLOBAL_ZONEID;
16010 	}
16011 
16012 	/* cannot assign instance zero to a non-global zone */
16013 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16014 		return (ENOTSUP);
16015 
16016 	/*
16017 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
16018 	 * the event of a race with the zone shutdown processing, since IP
16019 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16020 	 * interface will be cleaned up even if the zone is shut down
16021 	 * immediately after the status check. If the interface can't be brought
16022 	 * down right away, and the zone is shut down before the restart
16023 	 * function is called, we resolve the possible races by rechecking the
16024 	 * zone status in the restart function.
16025 	 */
16026 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
16027 		return (EINVAL);
16028 	status = zone_status_get(zptr);
16029 	zone_rele(zptr);
16030 
16031 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16032 		return (EINVAL);
16033 
16034 	if (ipif->ipif_flags & IPIF_UP) {
16035 		/*
16036 		 * If the interface is already marked up,
16037 		 * we call ipif_down which will take care
16038 		 * of ditching any IREs that have been set
16039 		 * up based on the old interface address.
16040 		 */
16041 		err = ipif_logical_down(ipif, q, mp);
16042 		if (err == EINPROGRESS)
16043 			return (err);
16044 		(void) ipif_down_tail(ipif);
16045 		need_up = B_TRUE;
16046 	}
16047 
16048 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16049 	return (err);
16050 }
16051 
16052 static int
16053 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16054     queue_t *q, mblk_t *mp, boolean_t need_up)
16055 {
16056 	int	err = 0;
16057 	ip_stack_t	*ipst;
16058 
16059 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16061 
16062 	if (CONN_Q(q))
16063 		ipst = CONNQ_TO_IPST(q);
16064 	else
16065 		ipst = ILLQ_TO_IPST(q);
16066 
16067 	/*
16068 	 * For exclusive stacks we don't allow a different zoneid than
16069 	 * global.
16070 	 */
16071 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16072 	    zoneid != GLOBAL_ZONEID)
16073 		return (EINVAL);
16074 
16075 	/* Set the new zone id. */
16076 	ipif->ipif_zoneid = zoneid;
16077 
16078 	/* Update sctp list */
16079 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16080 
16081 	/* The default multicast interface might have changed */
16082 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16083 
16084 	if (need_up) {
16085 		/*
16086 		 * Now bring the interface back up.  If this
16087 		 * is the only IPIF for the ILL, ipif_up
16088 		 * will have to re-bind to the device, so
16089 		 * we may get back EINPROGRESS, in which
16090 		 * case, this IOCTL will get completed in
16091 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
16092 		 */
16093 		err = ipif_up(ipif, q, mp);
16094 	}
16095 	return (err);
16096 }
16097 
16098 /* ARGSUSED */
16099 int
16100 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16101     ip_ioctl_cmd_t *ipip, void *if_req)
16102 {
16103 	struct lifreq *lifr = (struct lifreq *)if_req;
16104 	zoneid_t zoneid;
16105 	zone_t *zptr;
16106 	zone_status_t status;
16107 
16108 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16109 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16110 		zoneid = GLOBAL_ZONEID;
16111 
16112 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16113 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16114 
16115 	/*
16116 	 * We recheck the zone status to resolve the following race condition:
16117 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16118 	 * 2) hme0:1 is up and can't be brought down right away;
16119 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16120 	 * 3) zone "myzone" is halted; the zone status switches to
16121 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16122 	 * the interfaces to remove - hme0:1 is not returned because it's not
16123 	 * yet in "myzone", so it won't be removed;
16124 	 * 4) the restart function for SIOCSLIFZONE is called; without the
16125 	 * status check here, we would have hme0:1 in "myzone" after it's been
16126 	 * destroyed.
16127 	 * Note that if the status check fails, we need to bring the interface
16128 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16129 	 * ipif_up_done[_v6]().
16130 	 */
16131 	status = ZONE_IS_UNINITIALIZED;
16132 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16133 		status = zone_status_get(zptr);
16134 		zone_rele(zptr);
16135 	}
16136 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16137 		if (ipif->ipif_isv6) {
16138 			(void) ipif_up_done_v6(ipif);
16139 		} else {
16140 			(void) ipif_up_done(ipif);
16141 		}
16142 		return (EINVAL);
16143 	}
16144 
16145 	(void) ipif_down_tail(ipif);
16146 
16147 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16148 	    B_TRUE));
16149 }
16150 
16151 /*
16152  * Return the number of addresses on `ill' with one or more of the values
16153  * in `set' set and all of the values in `clear' clear.
16154  */
16155 static uint_t
16156 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16157 {
16158 	ipif_t	*ipif;
16159 	uint_t	cnt = 0;
16160 
16161 	ASSERT(IAM_WRITER_ILL(ill));
16162 
16163 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16164 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16165 			cnt++;
16166 
16167 	return (cnt);
16168 }
16169 
16170 /*
16171  * Return the number of migratable addresses on `ill' that are under
16172  * application control.
16173  */
16174 uint_t
16175 ill_appaddr_cnt(const ill_t *ill)
16176 {
16177 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16178 	    IPIF_NOFAILOVER));
16179 }
16180 
16181 /*
16182  * Return the number of point-to-point addresses on `ill'.
16183  */
16184 uint_t
16185 ill_ptpaddr_cnt(const ill_t *ill)
16186 {
16187 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16188 }
16189 
16190 /* ARGSUSED */
16191 int
16192 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16193 	ip_ioctl_cmd_t *ipip, void *ifreq)
16194 {
16195 	struct lifreq	*lifr = ifreq;
16196 
16197 	ASSERT(q->q_next == NULL);
16198 	ASSERT(CONN_Q(q));
16199 
16200 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16201 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16202 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16203 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16204 
16205 	return (0);
16206 }
16207 
16208 /* Find the previous ILL in this usesrc group */
16209 static ill_t *
16210 ill_prev_usesrc(ill_t *uill)
16211 {
16212 	ill_t *ill;
16213 
16214 	for (ill = uill->ill_usesrc_grp_next;
16215 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16216 	    ill = ill->ill_usesrc_grp_next)
16217 		/* do nothing */;
16218 	return (ill);
16219 }
16220 
16221 /*
16222  * Release all members of the usesrc group. This routine is called
16223  * from ill_delete when the interface being unplumbed is the
16224  * group head.
16225  *
16226  * This silently clears the usesrc that ifconfig setup.
16227  * An alternative would be to keep that ifindex, and drop packets on the floor
16228  * since no source address can be selected.
16229  * Even if we keep the current semantics, don't need a lock and a linked list.
16230  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16231  * the one that is being removed. Issue is how we return the usesrc users
16232  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16233  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16234  * ill walk, but the walker would need to insert in the ioctl response.
16235  */
16236 static void
16237 ill_disband_usesrc_group(ill_t *uill)
16238 {
16239 	ill_t *next_ill, *tmp_ill;
16240 	ip_stack_t	*ipst = uill->ill_ipst;
16241 
16242 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16243 	next_ill = uill->ill_usesrc_grp_next;
16244 
16245 	do {
16246 		ASSERT(next_ill != NULL);
16247 		tmp_ill = next_ill->ill_usesrc_grp_next;
16248 		ASSERT(tmp_ill != NULL);
16249 		next_ill->ill_usesrc_grp_next = NULL;
16250 		next_ill->ill_usesrc_ifindex = 0;
16251 		next_ill = tmp_ill;
16252 	} while (next_ill->ill_usesrc_ifindex != 0);
16253 	uill->ill_usesrc_grp_next = NULL;
16254 }
16255 
16256 /*
16257  * Remove the client usesrc ILL from the list and relink to a new list
16258  */
16259 int
16260 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16261 {
16262 	ill_t *ill, *tmp_ill;
16263 	ip_stack_t	*ipst = ucill->ill_ipst;
16264 
16265 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16266 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16267 
16268 	/*
16269 	 * Check if the usesrc client ILL passed in is not already
16270 	 * in use as a usesrc ILL i.e one whose source address is
16271 	 * in use OR a usesrc ILL is not already in use as a usesrc
16272 	 * client ILL
16273 	 */
16274 	if ((ucill->ill_usesrc_ifindex == 0) ||
16275 	    (uill->ill_usesrc_ifindex != 0)) {
16276 		return (-1);
16277 	}
16278 
16279 	ill = ill_prev_usesrc(ucill);
16280 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16281 
16282 	/* Remove from the current list */
16283 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16284 		/* Only two elements in the list */
16285 		ASSERT(ill->ill_usesrc_ifindex == 0);
16286 		ill->ill_usesrc_grp_next = NULL;
16287 	} else {
16288 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16289 	}
16290 
16291 	if (ifindex == 0) {
16292 		ucill->ill_usesrc_ifindex = 0;
16293 		ucill->ill_usesrc_grp_next = NULL;
16294 		return (0);
16295 	}
16296 
16297 	ucill->ill_usesrc_ifindex = ifindex;
16298 	tmp_ill = uill->ill_usesrc_grp_next;
16299 	uill->ill_usesrc_grp_next = ucill;
16300 	ucill->ill_usesrc_grp_next =
16301 	    (tmp_ill != NULL) ? tmp_ill : uill;
16302 	return (0);
16303 }
16304 
16305 /*
16306  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16307  * ip.c for locking details.
16308  */
16309 /* ARGSUSED */
16310 int
16311 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16312     ip_ioctl_cmd_t *ipip, void *ifreq)
16313 {
16314 	struct lifreq *lifr = (struct lifreq *)ifreq;
16315 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16316 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16317 	int err = 0, ret;
16318 	uint_t ifindex;
16319 	ipsq_t *ipsq = NULL;
16320 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16321 
16322 	ASSERT(IAM_WRITER_IPIF(ipif));
16323 	ASSERT(q->q_next == NULL);
16324 	ASSERT(CONN_Q(q));
16325 
16326 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16327 
16328 	ifindex = lifr->lifr_index;
16329 	if (ifindex == 0) {
16330 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16331 			/* non usesrc group interface, nothing to reset */
16332 			return (0);
16333 		}
16334 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16335 		/* valid reset request */
16336 		reset_flg = B_TRUE;
16337 	}
16338 
16339 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16340 	if (usesrc_ill == NULL)
16341 		return (ENXIO);
16342 	if (usesrc_ill == ipif->ipif_ill) {
16343 		ill_refrele(usesrc_ill);
16344 		return (EINVAL);
16345 	}
16346 
16347 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16348 	    NEW_OP, B_TRUE);
16349 	if (ipsq == NULL) {
16350 		err = EINPROGRESS;
16351 		/* Operation enqueued on the ipsq of the usesrc ILL */
16352 		goto done;
16353 	}
16354 
16355 	/* USESRC isn't currently supported with IPMP */
16356 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16357 		err = ENOTSUP;
16358 		goto done;
16359 	}
16360 
16361 	/*
16362 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16363 	 * used by IPMP underlying interfaces, but someone might think it's
16364 	 * more general and try to use it independently with VNI.)
16365 	 */
16366 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16367 		err = ENOTSUP;
16368 		goto done;
16369 	}
16370 
16371 	/*
16372 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16373 	 * already a client then return EINVAL
16374 	 */
16375 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16376 		err = EINVAL;
16377 		goto done;
16378 	}
16379 
16380 	/*
16381 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16382 	 * be then this is a duplicate operation.
16383 	 */
16384 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16385 		err = 0;
16386 		goto done;
16387 	}
16388 
16389 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16390 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16391 	    usesrc_ill->ill_isv6));
16392 
16393 	/*
16394 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16395 	 * and the ill_usesrc_ifindex fields
16396 	 */
16397 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16398 
16399 	if (reset_flg) {
16400 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16401 		if (ret != 0) {
16402 			err = EINVAL;
16403 		}
16404 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16405 		goto done;
16406 	}
16407 
16408 	/*
16409 	 * Four possibilities to consider:
16410 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16411 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16412 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16413 	 * 4. Both are part of their respective usesrc groups
16414 	 */
16415 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16416 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16417 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16418 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16419 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16420 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16421 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16422 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16423 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16424 		/* Insert at head of list */
16425 		usesrc_cli_ill->ill_usesrc_grp_next =
16426 		    usesrc_ill->ill_usesrc_grp_next;
16427 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16428 	} else {
16429 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16430 		    ifindex);
16431 		if (ret != 0)
16432 			err = EINVAL;
16433 	}
16434 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16435 
16436 done:
16437 	if (ipsq != NULL)
16438 		ipsq_exit(ipsq);
16439 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16440 	ill_refrele(usesrc_ill);
16441 
16442 	/* Let conn_ixa caching know that source address selection changed */
16443 	ip_update_source_selection(ipst);
16444 
16445 	return (err);
16446 }
16447 
16448 /* ARGSUSED */
16449 int
16450 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16451     ip_ioctl_cmd_t *ipip, void *if_req)
16452 {
16453 	struct lifreq	*lifr = (struct lifreq *)if_req;
16454 	ill_t		*ill = ipif->ipif_ill;
16455 
16456 	/*
16457 	 * Need a lock since IFF_UP can be set even when there are
16458 	 * references to the ipif.
16459 	 */
16460 	mutex_enter(&ill->ill_lock);
16461 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16462 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16463 	else
16464 		lifr->lifr_dadstate = DAD_DONE;
16465 	mutex_exit(&ill->ill_lock);
16466 	return (0);
16467 }
16468 
16469 /*
16470  * comparison function used by avl.
16471  */
16472 static int
16473 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16474 {
16475 
16476 	uint_t index;
16477 
16478 	ASSERT(phyip != NULL && index_ptr != NULL);
16479 
16480 	index = *((uint_t *)index_ptr);
16481 	/*
16482 	 * let the phyint with the lowest index be on top.
16483 	 */
16484 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16485 		return (1);
16486 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16487 		return (-1);
16488 	return (0);
16489 }
16490 
16491 /*
16492  * comparison function used by avl.
16493  */
16494 static int
16495 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16496 {
16497 	ill_t *ill;
16498 	int res = 0;
16499 
16500 	ASSERT(phyip != NULL && name_ptr != NULL);
16501 
16502 	if (((phyint_t *)phyip)->phyint_illv4)
16503 		ill = ((phyint_t *)phyip)->phyint_illv4;
16504 	else
16505 		ill = ((phyint_t *)phyip)->phyint_illv6;
16506 	ASSERT(ill != NULL);
16507 
16508 	res = strcmp(ill->ill_name, (char *)name_ptr);
16509 	if (res > 0)
16510 		return (1);
16511 	else if (res < 0)
16512 		return (-1);
16513 	return (0);
16514 }
16515 
16516 /*
16517  * This function is called on the unplumb path via ill_glist_delete() when
16518  * there are no ills left on the phyint and thus the phyint can be freed.
16519  */
16520 static void
16521 phyint_free(phyint_t *phyi)
16522 {
16523 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16524 
16525 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16526 
16527 	/*
16528 	 * If this phyint was an IPMP meta-interface, blow away the group.
16529 	 * This is safe to do because all of the illgrps have already been
16530 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16531 	 * If we're cleaning up as a result of failed initialization,
16532 	 * phyint_grp may be NULL.
16533 	 */
16534 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16535 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16536 		ipmp_grp_destroy(phyi->phyint_grp);
16537 		phyi->phyint_grp = NULL;
16538 		rw_exit(&ipst->ips_ipmp_lock);
16539 	}
16540 
16541 	/*
16542 	 * If this interface was under IPMP, take it out of the group.
16543 	 */
16544 	if (phyi->phyint_grp != NULL)
16545 		ipmp_phyint_leave_grp(phyi);
16546 
16547 	/*
16548 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16549 	 * will be freed in ipsq_exit().
16550 	 */
16551 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16552 	phyi->phyint_name[0] = '\0';
16553 
16554 	mi_free(phyi);
16555 }
16556 
16557 /*
16558  * Attach the ill to the phyint structure which can be shared by both
16559  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16560  * function is called from ipif_set_values and ill_lookup_on_name (for
16561  * loopback) where we know the name of the ill. We lookup the ill and if
16562  * there is one present already with the name use that phyint. Otherwise
16563  * reuse the one allocated by ill_init.
16564  */
16565 static void
16566 ill_phyint_reinit(ill_t *ill)
16567 {
16568 	boolean_t isv6 = ill->ill_isv6;
16569 	phyint_t *phyi_old;
16570 	phyint_t *phyi;
16571 	avl_index_t where = 0;
16572 	ill_t	*ill_other = NULL;
16573 	ip_stack_t	*ipst = ill->ill_ipst;
16574 
16575 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16576 
16577 	phyi_old = ill->ill_phyint;
16578 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16579 	    phyi_old->phyint_illv6 == NULL));
16580 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16581 	    phyi_old->phyint_illv4 == NULL));
16582 	ASSERT(phyi_old->phyint_ifindex == 0);
16583 
16584 	/*
16585 	 * Now that our ill has a name, set it in the phyint.
16586 	 */
16587 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16588 
16589 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16590 	    ill->ill_name, &where);
16591 
16592 	/*
16593 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16594 	 *    the global list of ills. So no other thread could have located
16595 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16596 	 * 2. Now locate the other protocol instance of this ill.
16597 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16598 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16599 	 *    of neither ill can change.
16600 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16601 	 *    other ill.
16602 	 * 5. Release all locks.
16603 	 */
16604 
16605 	/*
16606 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16607 	 * we are initializing IPv4.
16608 	 */
16609 	if (phyi != NULL) {
16610 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16611 		ASSERT(ill_other->ill_phyint != NULL);
16612 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16613 		    (!isv6 && ill_other->ill_isv6));
16614 		GRAB_ILL_LOCKS(ill, ill_other);
16615 		/*
16616 		 * We are potentially throwing away phyint_flags which
16617 		 * could be different from the one that we obtain from
16618 		 * ill_other->ill_phyint. But it is okay as we are assuming
16619 		 * that the state maintained within IP is correct.
16620 		 */
16621 		mutex_enter(&phyi->phyint_lock);
16622 		if (isv6) {
16623 			ASSERT(phyi->phyint_illv6 == NULL);
16624 			phyi->phyint_illv6 = ill;
16625 		} else {
16626 			ASSERT(phyi->phyint_illv4 == NULL);
16627 			phyi->phyint_illv4 = ill;
16628 		}
16629 
16630 		/*
16631 		 * Delete the old phyint and make its ipsq eligible
16632 		 * to be freed in ipsq_exit().
16633 		 */
16634 		phyi_old->phyint_illv4 = NULL;
16635 		phyi_old->phyint_illv6 = NULL;
16636 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16637 		phyi_old->phyint_name[0] = '\0';
16638 		mi_free(phyi_old);
16639 	} else {
16640 		mutex_enter(&ill->ill_lock);
16641 		/*
16642 		 * We don't need to acquire any lock, since
16643 		 * the ill is not yet visible globally  and we
16644 		 * have not yet released the ill_g_lock.
16645 		 */
16646 		phyi = phyi_old;
16647 		mutex_enter(&phyi->phyint_lock);
16648 		/* XXX We need a recovery strategy here. */
16649 		if (!phyint_assign_ifindex(phyi, ipst))
16650 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16651 
16652 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16653 		    (void *)phyi, where);
16654 
16655 		(void) avl_find(&ipst->ips_phyint_g_list->
16656 		    phyint_list_avl_by_index,
16657 		    &phyi->phyint_ifindex, &where);
16658 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16659 		    (void *)phyi, where);
16660 	}
16661 
16662 	/*
16663 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16664 	 * pending mp is not affected because that is per ill basis.
16665 	 */
16666 	ill->ill_phyint = phyi;
16667 
16668 	/*
16669 	 * Now that the phyint's ifindex has been assigned, complete the
16670 	 * remaining
16671 	 */
16672 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16673 	if (ill->ill_isv6) {
16674 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16675 		    ill->ill_phyint->phyint_ifindex;
16676 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16677 	} else {
16678 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16679 	}
16680 
16681 	/*
16682 	 * Generate an event within the hooks framework to indicate that
16683 	 * a new interface has just been added to IP.  For this event to
16684 	 * be generated, the network interface must, at least, have an
16685 	 * ifindex assigned to it.  (We don't generate the event for
16686 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16687 	 *
16688 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16689 	 * that the ordering of delivered events to listeners matches the
16690 	 * order of them in the kernel.
16691 	 */
16692 	if (!IS_LOOPBACK(ill)) {
16693 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16694 		    ill->ill_name_length);
16695 	}
16696 	RELEASE_ILL_LOCKS(ill, ill_other);
16697 	mutex_exit(&phyi->phyint_lock);
16698 }
16699 
16700 /*
16701  * Notify any downstream modules of the name of this interface.
16702  * An M_IOCTL is used even though we don't expect a successful reply.
16703  * Any reply message from the driver (presumably an M_IOCNAK) will
16704  * eventually get discarded somewhere upstream.  The message format is
16705  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16706  * to IP.
16707  */
16708 static void
16709 ip_ifname_notify(ill_t *ill, queue_t *q)
16710 {
16711 	mblk_t *mp1, *mp2;
16712 	struct iocblk *iocp;
16713 	struct lifreq *lifr;
16714 
16715 	mp1 = mkiocb(SIOCSLIFNAME);
16716 	if (mp1 == NULL)
16717 		return;
16718 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16719 	if (mp2 == NULL) {
16720 		freeb(mp1);
16721 		return;
16722 	}
16723 
16724 	mp1->b_cont = mp2;
16725 	iocp = (struct iocblk *)mp1->b_rptr;
16726 	iocp->ioc_count = sizeof (struct lifreq);
16727 
16728 	lifr = (struct lifreq *)mp2->b_rptr;
16729 	mp2->b_wptr += sizeof (struct lifreq);
16730 	bzero(lifr, sizeof (struct lifreq));
16731 
16732 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16733 	lifr->lifr_ppa = ill->ill_ppa;
16734 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16735 
16736 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16737 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16738 	putnext(q, mp1);
16739 }
16740 
16741 static int
16742 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16743 {
16744 	int		err;
16745 	ip_stack_t	*ipst = ill->ill_ipst;
16746 	phyint_t	*phyi = ill->ill_phyint;
16747 
16748 	/*
16749 	 * Now that ill_name is set, the configuration for the IPMP
16750 	 * meta-interface can be performed.
16751 	 */
16752 	if (IS_IPMP(ill)) {
16753 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16754 		/*
16755 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16756 		 * meta-interface and we need to create the IPMP group.
16757 		 */
16758 		if (phyi->phyint_grp == NULL) {
16759 			/*
16760 			 * If someone has renamed another IPMP group to have
16761 			 * the same name as our interface, bail.
16762 			 */
16763 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16764 				rw_exit(&ipst->ips_ipmp_lock);
16765 				return (EEXIST);
16766 			}
16767 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16768 			if (phyi->phyint_grp == NULL) {
16769 				rw_exit(&ipst->ips_ipmp_lock);
16770 				return (ENOMEM);
16771 			}
16772 		}
16773 		rw_exit(&ipst->ips_ipmp_lock);
16774 	}
16775 
16776 	/* Tell downstream modules where they are. */
16777 	ip_ifname_notify(ill, q);
16778 
16779 	/*
16780 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16781 	 * Error cases are ENOMEM ...
16782 	 */
16783 	err = ill_dl_phys(ill, ipif, mp, q);
16784 
16785 	if (ill->ill_isv6) {
16786 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16787 		if (ipst->ips_mld_slowtimeout_id == 0) {
16788 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16789 			    (void *)ipst,
16790 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16791 		}
16792 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16793 	} else {
16794 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16795 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16796 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16797 			    (void *)ipst,
16798 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16799 		}
16800 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16801 	}
16802 
16803 	return (err);
16804 }
16805 
16806 /*
16807  * Common routine for ppa and ifname setting. Should be called exclusive.
16808  *
16809  * Returns EINPROGRESS when mp has been consumed by queueing it on
16810  * ipx_pending_mp and the ioctl will complete in ip_rput.
16811  *
16812  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16813  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16814  * For SLIFNAME, we pass these values back to the userland.
16815  */
16816 static int
16817 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16818 {
16819 	ill_t	*ill;
16820 	ipif_t	*ipif;
16821 	ipsq_t	*ipsq;
16822 	char	*ppa_ptr;
16823 	char	*old_ptr;
16824 	char	old_char;
16825 	int	error;
16826 	ip_stack_t	*ipst;
16827 
16828 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16829 	ASSERT(q->q_next != NULL);
16830 	ASSERT(interf_name != NULL);
16831 
16832 	ill = (ill_t *)q->q_ptr;
16833 	ipst = ill->ill_ipst;
16834 
16835 	ASSERT(ill->ill_ipst != NULL);
16836 	ASSERT(ill->ill_name[0] == '\0');
16837 	ASSERT(IAM_WRITER_ILL(ill));
16838 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16839 	ASSERT(ill->ill_ppa == UINT_MAX);
16840 
16841 	ill->ill_defend_start = ill->ill_defend_count = 0;
16842 	/* The ppa is sent down by ifconfig or is chosen */
16843 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16844 		return (EINVAL);
16845 	}
16846 
16847 	/*
16848 	 * make sure ppa passed in is same as ppa in the name.
16849 	 * This check is not made when ppa == UINT_MAX in that case ppa
16850 	 * in the name could be anything. System will choose a ppa and
16851 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16852 	 */
16853 	if (*new_ppa_ptr != UINT_MAX) {
16854 		/* stoi changes the pointer */
16855 		old_ptr = ppa_ptr;
16856 		/*
16857 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16858 		 * (they don't have an externally visible ppa).  We assign one
16859 		 * here so that we can manage the interface.  Note that in
16860 		 * the past this value was always 0 for DLPI 1 drivers.
16861 		 */
16862 		if (*new_ppa_ptr == 0)
16863 			*new_ppa_ptr = stoi(&old_ptr);
16864 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16865 			return (EINVAL);
16866 	}
16867 	/*
16868 	 * terminate string before ppa
16869 	 * save char at that location.
16870 	 */
16871 	old_char = ppa_ptr[0];
16872 	ppa_ptr[0] = '\0';
16873 
16874 	ill->ill_ppa = *new_ppa_ptr;
16875 	/*
16876 	 * Finish as much work now as possible before calling ill_glist_insert
16877 	 * which makes the ill globally visible and also merges it with the
16878 	 * other protocol instance of this phyint. The remaining work is
16879 	 * done after entering the ipsq which may happen sometime later.
16880 	 */
16881 	ipif = ill->ill_ipif;
16882 
16883 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16884 	ipif_assign_seqid(ipif);
16885 
16886 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16887 		ill->ill_flags |= ILLF_IPV4;
16888 
16889 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16890 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16891 
16892 	if (ill->ill_flags & ILLF_IPV6) {
16893 
16894 		ill->ill_isv6 = B_TRUE;
16895 		ill_set_inputfn(ill);
16896 		if (ill->ill_rq != NULL) {
16897 			ill->ill_rq->q_qinfo = &iprinitv6;
16898 		}
16899 
16900 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16901 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16902 		ipif->ipif_v6subnet = ipv6_all_zeros;
16903 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16904 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16905 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16906 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16907 		/*
16908 		 * point-to-point or Non-mulicast capable
16909 		 * interfaces won't do NUD unless explicitly
16910 		 * configured to do so.
16911 		 */
16912 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16913 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16914 			ill->ill_flags |= ILLF_NONUD;
16915 		}
16916 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16917 		if (ill->ill_flags & ILLF_NOARP) {
16918 			/*
16919 			 * Note: xresolv interfaces will eventually need
16920 			 * NOARP set here as well, but that will require
16921 			 * those external resolvers to have some
16922 			 * knowledge of that flag and act appropriately.
16923 			 * Not to be changed at present.
16924 			 */
16925 			ill->ill_flags &= ~ILLF_NOARP;
16926 		}
16927 		/*
16928 		 * Set the ILLF_ROUTER flag according to the global
16929 		 * IPv6 forwarding policy.
16930 		 */
16931 		if (ipst->ips_ipv6_forwarding != 0)
16932 			ill->ill_flags |= ILLF_ROUTER;
16933 	} else if (ill->ill_flags & ILLF_IPV4) {
16934 		ill->ill_isv6 = B_FALSE;
16935 		ill_set_inputfn(ill);
16936 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16937 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16938 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16939 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16940 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16941 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16942 		/*
16943 		 * Set the ILLF_ROUTER flag according to the global
16944 		 * IPv4 forwarding policy.
16945 		 */
16946 		if (ipst->ips_ip_forwarding != 0)
16947 			ill->ill_flags |= ILLF_ROUTER;
16948 	}
16949 
16950 	ASSERT(ill->ill_phyint != NULL);
16951 
16952 	/*
16953 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16954 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16955 	 */
16956 	if (!ill_allocate_mibs(ill))
16957 		return (ENOMEM);
16958 
16959 	/*
16960 	 * Pick a default sap until we get the DL_INFO_ACK back from
16961 	 * the driver.
16962 	 */
16963 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16964 	    ill->ill_media->ip_m_ipv4sap;
16965 
16966 	ill->ill_ifname_pending = 1;
16967 	ill->ill_ifname_pending_err = 0;
16968 
16969 	/*
16970 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16971 	 * that were joined while this ill was not bound to the DLPI link need
16972 	 * to be recovered by ill_recover_multicast().
16973 	 */
16974 	ill->ill_need_recover_multicast = 1;
16975 
16976 	ill_refhold(ill);
16977 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16978 	if ((error = ill_glist_insert(ill, interf_name,
16979 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16980 		ill->ill_ppa = UINT_MAX;
16981 		ill->ill_name[0] = '\0';
16982 		/*
16983 		 * undo null termination done above.
16984 		 */
16985 		ppa_ptr[0] = old_char;
16986 		rw_exit(&ipst->ips_ill_g_lock);
16987 		ill_refrele(ill);
16988 		return (error);
16989 	}
16990 
16991 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16992 
16993 	/*
16994 	 * When we return the buffer pointed to by interf_name should contain
16995 	 * the same name as in ill_name.
16996 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16997 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16998 	 * so copy full name and update the ppa ptr.
16999 	 * When ppa passed in != UINT_MAX all values are correct just undo
17000 	 * null termination, this saves a bcopy.
17001 	 */
17002 	if (*new_ppa_ptr == UINT_MAX) {
17003 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
17004 		*new_ppa_ptr = ill->ill_ppa;
17005 	} else {
17006 		/*
17007 		 * undo null termination done above.
17008 		 */
17009 		ppa_ptr[0] = old_char;
17010 	}
17011 
17012 	/* Let SCTP know about this ILL */
17013 	sctp_update_ill(ill, SCTP_ILL_INSERT);
17014 
17015 	/*
17016 	 * ill_glist_insert has made the ill visible globally, and
17017 	 * ill_phyint_reinit could have changed the ipsq. At this point,
17018 	 * we need to hold the ips_ill_g_lock across the call to enter the
17019 	 * ipsq to enforce atomicity and prevent reordering. In the event
17020 	 * the ipsq has changed, and if the new ipsq is currently busy,
17021 	 * we need to make sure that this half-completed ioctl is ahead of
17022 	 * any subsequent ioctl. We achieve this by not dropping the
17023 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
17024 	 * ensuring that new ioctls can't start.
17025 	 */
17026 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17027 	    B_TRUE);
17028 
17029 	rw_exit(&ipst->ips_ill_g_lock);
17030 	ill_refrele(ill);
17031 	if (ipsq == NULL)
17032 		return (EINPROGRESS);
17033 
17034 	/*
17035 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17036 	 */
17037 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17038 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17039 	else
17040 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17041 
17042 	error = ipif_set_values_tail(ill, ipif, mp, q);
17043 	ipsq_exit(ipsq);
17044 	if (error != 0 && error != EINPROGRESS) {
17045 		/*
17046 		 * restore previous values
17047 		 */
17048 		ill->ill_isv6 = B_FALSE;
17049 		ill_set_inputfn(ill);
17050 	}
17051 	return (error);
17052 }
17053 
17054 void
17055 ipif_init(ip_stack_t *ipst)
17056 {
17057 	int i;
17058 
17059 	for (i = 0; i < MAX_G_HEADS; i++) {
17060 		ipst->ips_ill_g_heads[i].ill_g_list_head =
17061 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17062 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
17063 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17064 	}
17065 
17066 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17067 	    ill_phyint_compare_index,
17068 	    sizeof (phyint_t),
17069 	    offsetof(struct phyint, phyint_avl_by_index));
17070 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17071 	    ill_phyint_compare_name,
17072 	    sizeof (phyint_t),
17073 	    offsetof(struct phyint, phyint_avl_by_name));
17074 }
17075 
17076 /*
17077  * Save enough information so that we can recreate the IRE if
17078  * the interface goes down and then up.
17079  */
17080 void
17081 ill_save_ire(ill_t *ill, ire_t *ire)
17082 {
17083 	mblk_t	*save_mp;
17084 
17085 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17086 	if (save_mp != NULL) {
17087 		ifrt_t	*ifrt;
17088 
17089 		save_mp->b_wptr += sizeof (ifrt_t);
17090 		ifrt = (ifrt_t *)save_mp->b_rptr;
17091 		bzero(ifrt, sizeof (ifrt_t));
17092 		ifrt->ifrt_type = ire->ire_type;
17093 		if (ire->ire_ipversion == IPV4_VERSION) {
17094 			ASSERT(!ill->ill_isv6);
17095 			ifrt->ifrt_addr = ire->ire_addr;
17096 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17097 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17098 			ifrt->ifrt_mask = ire->ire_mask;
17099 		} else {
17100 			ASSERT(ill->ill_isv6);
17101 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
17102 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17103 			mutex_enter(&ire->ire_lock);
17104 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17105 			mutex_exit(&ire->ire_lock);
17106 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17107 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
17108 		}
17109 		ifrt->ifrt_flags = ire->ire_flags;
17110 		ifrt->ifrt_zoneid = ire->ire_zoneid;
17111 		mutex_enter(&ill->ill_saved_ire_lock);
17112 		save_mp->b_cont = ill->ill_saved_ire_mp;
17113 		ill->ill_saved_ire_mp = save_mp;
17114 		ill->ill_saved_ire_cnt++;
17115 		mutex_exit(&ill->ill_saved_ire_lock);
17116 	}
17117 }
17118 
17119 /*
17120  * Remove one entry from ill_saved_ire_mp.
17121  */
17122 void
17123 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17124 {
17125 	mblk_t	**mpp;
17126 	mblk_t	*mp;
17127 	ifrt_t	*ifrt;
17128 
17129 	/* Remove from ill_saved_ire_mp list if it is there */
17130 	mutex_enter(&ill->ill_saved_ire_lock);
17131 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17132 	    mpp = &(*mpp)->b_cont) {
17133 		in6_addr_t	gw_addr_v6;
17134 
17135 		/*
17136 		 * On a given ill, the tuple of address, gateway, mask,
17137 		 * ire_type, and zoneid is unique for each saved IRE.
17138 		 */
17139 		mp = *mpp;
17140 		ifrt = (ifrt_t *)mp->b_rptr;
17141 		/* ire_gateway_addr_v6 can change - need lock */
17142 		mutex_enter(&ire->ire_lock);
17143 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17144 		mutex_exit(&ire->ire_lock);
17145 
17146 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17147 		    ifrt->ifrt_type != ire->ire_type)
17148 			continue;
17149 
17150 		if (ill->ill_isv6 ?
17151 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17152 		    &ire->ire_addr_v6) &&
17153 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17154 		    &gw_addr_v6) &&
17155 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17156 		    &ire->ire_mask_v6)) :
17157 		    (ifrt->ifrt_addr == ire->ire_addr &&
17158 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17159 		    ifrt->ifrt_mask == ire->ire_mask)) {
17160 			*mpp = mp->b_cont;
17161 			ill->ill_saved_ire_cnt--;
17162 			freeb(mp);
17163 			break;
17164 		}
17165 	}
17166 	mutex_exit(&ill->ill_saved_ire_lock);
17167 }
17168 
17169 /*
17170  * IP multirouting broadcast routes handling
17171  * Append CGTP broadcast IREs to regular ones created
17172  * at ifconfig time.
17173  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17174  * the destination and the gateway are broadcast addresses.
17175  * The caller has verified that the destination is an IRE_BROADCAST and that
17176  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17177  * we create a MULTIRT IRE_BROADCAST.
17178  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17179  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17180  */
17181 static void
17182 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17183 {
17184 	ire_t *ire_prim;
17185 
17186 	ASSERT(ire != NULL);
17187 
17188 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17189 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17190 	    NULL);
17191 	if (ire_prim != NULL) {
17192 		/*
17193 		 * We are in the special case of broadcasts for
17194 		 * CGTP. We add an IRE_BROADCAST that holds
17195 		 * the RTF_MULTIRT flag, the destination
17196 		 * address and the low level
17197 		 * info of ire_prim. In other words, CGTP
17198 		 * broadcast is added to the redundant ipif.
17199 		 */
17200 		ill_t *ill_prim;
17201 		ire_t  *bcast_ire;
17202 
17203 		ill_prim = ire_prim->ire_ill;
17204 
17205 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17206 		    (void *)ire_prim, (void *)ill_prim));
17207 
17208 		bcast_ire = ire_create(
17209 		    (uchar_t *)&ire->ire_addr,
17210 		    (uchar_t *)&ip_g_all_ones,
17211 		    (uchar_t *)&ire->ire_gateway_addr,
17212 		    IRE_BROADCAST,
17213 		    ill_prim,
17214 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17215 		    ire->ire_flags | RTF_KERNEL,
17216 		    NULL,
17217 		    ipst);
17218 
17219 		/*
17220 		 * Here we assume that ire_add does head insertion so that
17221 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17222 		 */
17223 		if (bcast_ire != NULL) {
17224 			if (ire->ire_flags & RTF_SETSRC) {
17225 				bcast_ire->ire_setsrc_addr =
17226 				    ire->ire_setsrc_addr;
17227 			}
17228 			bcast_ire = ire_add(bcast_ire);
17229 			if (bcast_ire != NULL) {
17230 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17231 				    "added bcast_ire %p\n",
17232 				    (void *)bcast_ire));
17233 
17234 				ill_save_ire(ill_prim, bcast_ire);
17235 				ire_refrele(bcast_ire);
17236 			}
17237 		}
17238 		ire_refrele(ire_prim);
17239 	}
17240 }
17241 
17242 /*
17243  * IP multirouting broadcast routes handling
17244  * Remove the broadcast ire.
17245  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17246  * the destination and the gateway are broadcast addresses.
17247  * The caller has only verified that RTF_MULTIRT was set. We check
17248  * that the destination is broadcast and that the gateway is a broadcast
17249  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17250  */
17251 static void
17252 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17253 {
17254 	ASSERT(ire != NULL);
17255 
17256 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17257 		ire_t *ire_prim;
17258 
17259 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17260 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17261 		    ipst, NULL);
17262 		if (ire_prim != NULL) {
17263 			ill_t *ill_prim;
17264 			ire_t  *bcast_ire;
17265 
17266 			ill_prim = ire_prim->ire_ill;
17267 
17268 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17269 			    "ire_prim %p, ill_prim %p\n",
17270 			    (void *)ire_prim, (void *)ill_prim));
17271 
17272 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17273 			    ire->ire_gateway_addr, IRE_BROADCAST,
17274 			    ill_prim, ALL_ZONES, NULL,
17275 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17276 			    MATCH_IRE_MASK, 0, ipst, NULL);
17277 
17278 			if (bcast_ire != NULL) {
17279 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17280 				    "looked up bcast_ire %p\n",
17281 				    (void *)bcast_ire));
17282 				ill_remove_saved_ire(bcast_ire->ire_ill,
17283 				    bcast_ire);
17284 				ire_delete(bcast_ire);
17285 				ire_refrele(bcast_ire);
17286 			}
17287 			ire_refrele(ire_prim);
17288 		}
17289 	}
17290 }
17291 
17292 /*
17293  * Derive an interface id from the link layer address.
17294  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17295  */
17296 static void
17297 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17298 {
17299 	char		*addr;
17300 
17301 	/*
17302 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17303 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17304 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17305 	 * interface ID on IPv6 interfaces above links that actually have real
17306 	 * Ethernet addresses.
17307 	 */
17308 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17309 		/* Form EUI-64 like address */
17310 		addr = (char *)&v6addr->s6_addr32[2];
17311 		bcopy(ill->ill_phys_addr, addr, 3);
17312 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17313 		addr[3] = (char)0xff;
17314 		addr[4] = (char)0xfe;
17315 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17316 	}
17317 }
17318 
17319 /* ARGSUSED */
17320 static void
17321 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17322 {
17323 }
17324 
17325 typedef struct ipmp_ifcookie {
17326 	uint32_t	ic_hostid;
17327 	char		ic_ifname[LIFNAMSIZ];
17328 	char		ic_zonename[ZONENAME_MAX];
17329 } ipmp_ifcookie_t;
17330 
17331 /*
17332  * Construct a pseudo-random interface ID for the IPMP interface that's both
17333  * predictable and (almost) guaranteed to be unique.
17334  */
17335 static void
17336 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17337 {
17338 	zone_t		*zp;
17339 	uint8_t		*addr;
17340 	uchar_t		hash[16];
17341 	ulong_t 	hostid;
17342 	MD5_CTX		ctx;
17343 	ipmp_ifcookie_t	ic = { 0 };
17344 
17345 	ASSERT(IS_IPMP(ill));
17346 
17347 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17348 	ic.ic_hostid = htonl((uint32_t)hostid);
17349 
17350 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17351 
17352 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17353 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17354 		zone_rele(zp);
17355 	}
17356 
17357 	MD5Init(&ctx);
17358 	MD5Update(&ctx, &ic, sizeof (ic));
17359 	MD5Final(hash, &ctx);
17360 
17361 	/*
17362 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17363 	 */
17364 	addr = &v6addr->s6_addr8[8];
17365 	bcopy(hash + 8, addr, sizeof (uint64_t));
17366 	addr[0] &= ~0x2;				/* set local bit */
17367 }
17368 
17369 /*
17370  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17371  */
17372 static void
17373 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17374 {
17375 	phyint_t *phyi = ill->ill_phyint;
17376 
17377 	/*
17378 	 * Check PHYI_MULTI_BCAST and length of physical
17379 	 * address to determine if we use the mapping or the
17380 	 * broadcast address.
17381 	 */
17382 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17383 	    ill->ill_phys_addr_length != ETHERADDRL) {
17384 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17385 		return;
17386 	}
17387 	m_physaddr[0] = 0x33;
17388 	m_physaddr[1] = 0x33;
17389 	m_physaddr[2] = m_ip6addr[12];
17390 	m_physaddr[3] = m_ip6addr[13];
17391 	m_physaddr[4] = m_ip6addr[14];
17392 	m_physaddr[5] = m_ip6addr[15];
17393 }
17394 
17395 /*
17396  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17397  */
17398 static void
17399 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17400 {
17401 	phyint_t *phyi = ill->ill_phyint;
17402 
17403 	/*
17404 	 * Check PHYI_MULTI_BCAST and length of physical
17405 	 * address to determine if we use the mapping or the
17406 	 * broadcast address.
17407 	 */
17408 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17409 	    ill->ill_phys_addr_length != ETHERADDRL) {
17410 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17411 		return;
17412 	}
17413 	m_physaddr[0] = 0x01;
17414 	m_physaddr[1] = 0x00;
17415 	m_physaddr[2] = 0x5e;
17416 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17417 	m_physaddr[4] = m_ipaddr[2];
17418 	m_physaddr[5] = m_ipaddr[3];
17419 }
17420 
17421 /* ARGSUSED */
17422 static void
17423 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17424 {
17425 	/*
17426 	 * for the MULTI_BCAST case and other cases when we want to
17427 	 * use the link-layer broadcast address for multicast.
17428 	 */
17429 	uint8_t	*bphys_addr;
17430 	dl_unitdata_req_t *dlur;
17431 
17432 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17433 	if (ill->ill_sap_length < 0) {
17434 		bphys_addr = (uchar_t *)dlur +
17435 		    dlur->dl_dest_addr_offset;
17436 	} else  {
17437 		bphys_addr = (uchar_t *)dlur +
17438 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17439 	}
17440 
17441 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17442 }
17443 
17444 /*
17445  * Derive IPoIB interface id from the link layer address.
17446  */
17447 static void
17448 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17449 {
17450 	char		*addr;
17451 
17452 	ASSERT(ill->ill_phys_addr_length == 20);
17453 	addr = (char *)&v6addr->s6_addr32[2];
17454 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17455 	/*
17456 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17457 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17458 	 * rules. In these cases, the IBA considers these GUIDs to be in
17459 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17460 	 * required; vendors are required not to assign global EUI-64's
17461 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17462 	 * of the interface identifier. Whether the GUID is in modified
17463 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17464 	 * bit set to 1.
17465 	 */
17466 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17467 }
17468 
17469 /*
17470  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17471  * Note on mapping from multicast IP addresses to IPoIB multicast link
17472  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17473  * The format of an IPoIB multicast address is:
17474  *
17475  *  4 byte QPN      Scope Sign.  Pkey
17476  * +--------------------------------------------+
17477  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17478  * +--------------------------------------------+
17479  *
17480  * The Scope and Pkey components are properties of the IBA port and
17481  * network interface. They can be ascertained from the broadcast address.
17482  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17483  */
17484 static void
17485 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17486 {
17487 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17488 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17489 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17490 	uint8_t	*bphys_addr;
17491 	dl_unitdata_req_t *dlur;
17492 
17493 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17494 
17495 	/*
17496 	 * RFC 4391: IPv4 MGID is 28-bit long.
17497 	 */
17498 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17499 	m_physaddr[17] = m_ipaddr[1];
17500 	m_physaddr[18] = m_ipaddr[2];
17501 	m_physaddr[19] = m_ipaddr[3];
17502 
17503 
17504 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17505 	if (ill->ill_sap_length < 0) {
17506 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17507 	} else  {
17508 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17509 		    ill->ill_sap_length;
17510 	}
17511 	/*
17512 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17513 	 */
17514 	m_physaddr[5] = bphys_addr[5];
17515 	m_physaddr[8] = bphys_addr[8];
17516 	m_physaddr[9] = bphys_addr[9];
17517 }
17518 
17519 static void
17520 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17521 {
17522 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17523 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17524 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17525 	uint8_t	*bphys_addr;
17526 	dl_unitdata_req_t *dlur;
17527 
17528 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17529 
17530 	/*
17531 	 * RFC 4391: IPv4 MGID is 80-bit long.
17532 	 */
17533 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17534 
17535 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17536 	if (ill->ill_sap_length < 0) {
17537 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17538 	} else  {
17539 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17540 		    ill->ill_sap_length;
17541 	}
17542 	/*
17543 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17544 	 */
17545 	m_physaddr[5] = bphys_addr[5];
17546 	m_physaddr[8] = bphys_addr[8];
17547 	m_physaddr[9] = bphys_addr[9];
17548 }
17549 
17550 /*
17551  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17552  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17553  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17554  * of RFC4213.
17555  */
17556 static void
17557 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17558 {
17559 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17560 	v6addr->s6_addr32[2] = 0;
17561 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17562 }
17563 
17564 /*
17565  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17566  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17567  * id.
17568  */
17569 static void
17570 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17571 {
17572 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17573 
17574 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17575 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17576 }
17577 
17578 static void
17579 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17580 {
17581 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17582 }
17583 
17584 static void
17585 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17586 {
17587 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17588 }
17589 
17590 static void
17591 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17592 {
17593 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17594 }
17595 
17596 static void
17597 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17598 {
17599 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17600 }
17601 
17602 /*
17603  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17604  * Returns an held ill, or NULL.
17605  */
17606 ill_t *
17607 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17608     ip_stack_t *ipst)
17609 {
17610 	ill_t	*ill;
17611 	ipif_t	*ipif;
17612 
17613 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17614 	if (ill == NULL)
17615 		return (NULL);
17616 
17617 	mutex_enter(&ill->ill_lock);
17618 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17619 		if (IPIF_IS_CONDEMNED(ipif))
17620 			continue;
17621 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17622 		    ipif->ipif_zoneid != ALL_ZONES)
17623 			continue;
17624 
17625 		mutex_exit(&ill->ill_lock);
17626 		return (ill);
17627 	}
17628 	mutex_exit(&ill->ill_lock);
17629 	ill_refrele(ill);
17630 	return (NULL);
17631 }
17632 
17633 /*
17634  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17635  * If a pointer to an ipif_t is returned then the caller will need to do
17636  * an ill_refrele().
17637  */
17638 ipif_t *
17639 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17640     ip_stack_t *ipst)
17641 {
17642 	ipif_t *ipif;
17643 	ill_t *ill;
17644 
17645 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17646 	if (ill == NULL)
17647 		return (NULL);
17648 
17649 	mutex_enter(&ill->ill_lock);
17650 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17651 		mutex_exit(&ill->ill_lock);
17652 		ill_refrele(ill);
17653 		return (NULL);
17654 	}
17655 
17656 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17657 		if (!IPIF_CAN_LOOKUP(ipif))
17658 			continue;
17659 		if (lifidx == ipif->ipif_id) {
17660 			ipif_refhold_locked(ipif);
17661 			break;
17662 		}
17663 	}
17664 
17665 	mutex_exit(&ill->ill_lock);
17666 	ill_refrele(ill);
17667 	return (ipif);
17668 }
17669 
17670 /*
17671  * Set ill_inputfn based on the current know state.
17672  * This needs to be called when any of the factors taken into
17673  * account changes.
17674  */
17675 void
17676 ill_set_inputfn(ill_t *ill)
17677 {
17678 	ip_stack_t	*ipst = ill->ill_ipst;
17679 
17680 	if (ill->ill_isv6) {
17681 		if (is_system_labeled())
17682 			ill->ill_inputfn = ill_input_full_v6;
17683 		else
17684 			ill->ill_inputfn = ill_input_short_v6;
17685 	} else {
17686 		if (is_system_labeled())
17687 			ill->ill_inputfn = ill_input_full_v4;
17688 		else if (ill->ill_dhcpinit != 0)
17689 			ill->ill_inputfn = ill_input_full_v4;
17690 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17691 		    != NULL)
17692 			ill->ill_inputfn = ill_input_full_v4;
17693 		else if (ipst->ips_ip_cgtp_filter &&
17694 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17695 			ill->ill_inputfn = ill_input_full_v4;
17696 		else
17697 			ill->ill_inputfn = ill_input_short_v4;
17698 	}
17699 }
17700 
17701 /*
17702  * Re-evaluate ill_inputfn for all the IPv4 ills.
17703  * Used when RSVP and CGTP comes and goes.
17704  */
17705 void
17706 ill_set_inputfn_all(ip_stack_t *ipst)
17707 {
17708 	ill_walk_context_t	ctx;
17709 	ill_t			*ill;
17710 
17711 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17712 	ill = ILL_START_WALK_V4(&ctx, ipst);
17713 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17714 		ill_set_inputfn(ill);
17715 
17716 	rw_exit(&ipst->ips_ill_g_lock);
17717 }
17718 
17719 /*
17720  * Set the physical address information for `ill' to the contents of the
17721  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17722  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17723  * EINPROGRESS will be returned.
17724  */
17725 int
17726 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17727 {
17728 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17729 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17730 
17731 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17732 
17733 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17734 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17735 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17736 		/* Changing DL_IPV6_TOKEN is not yet supported */
17737 		return (0);
17738 	}
17739 
17740 	/*
17741 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17742 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17743 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17744 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17745 	 */
17746 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17747 		freemsg(mp);
17748 		return (ENOMEM);
17749 	}
17750 
17751 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17752 
17753 	/*
17754 	 * Since we'll only do a logical down, we can't rely on ipif_down
17755 	 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17756 	 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17757 	 * case, to quiesce ire's and nce's for ill_is_quiescent.
17758 	 */
17759 	mutex_enter(&ill->ill_lock);
17760 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17761 	/* no more ire/nce addition allowed */
17762 	mutex_exit(&ill->ill_lock);
17763 
17764 	/*
17765 	 * If we can quiesce the ill, then set the address.  If not, then
17766 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17767 	 */
17768 	ill_down_ipifs(ill, B_TRUE);
17769 	mutex_enter(&ill->ill_lock);
17770 	if (!ill_is_quiescent(ill)) {
17771 		/* call cannot fail since `conn_t *' argument is NULL */
17772 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17773 		    mp, ILL_DOWN);
17774 		mutex_exit(&ill->ill_lock);
17775 		return (EINPROGRESS);
17776 	}
17777 	mutex_exit(&ill->ill_lock);
17778 
17779 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17780 	return (0);
17781 }
17782 
17783 /*
17784  * Once the ill associated with `q' has quiesced, set its physical address
17785  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17786  * are passed (linked by b_cont), since we sometimes need to save two distinct
17787  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17788  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17789  * is quiesced, we know any stale nce's with the old address information have
17790  * already been removed, so we don't need to call nce_flush().
17791  */
17792 /* ARGSUSED */
17793 static void
17794 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17795 {
17796 	ill_t		*ill = q->q_ptr;
17797 	mblk_t		*addrmp2 = unlinkb(addrmp);
17798 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17799 	uint_t		addrlen, addroff;
17800 	int		status;
17801 
17802 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17803 
17804 	addroff	= dlindp->dl_addr_offset;
17805 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17806 
17807 	switch (dlindp->dl_data) {
17808 	case DL_IPV6_LINK_LAYER_ADDR:
17809 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17810 		freemsg(addrmp2);
17811 		break;
17812 
17813 	case DL_CURR_DEST_ADDR:
17814 		freemsg(ill->ill_dest_addr_mp);
17815 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17816 		ill->ill_dest_addr_mp = addrmp;
17817 		if (ill->ill_isv6) {
17818 			ill_setdesttoken(ill);
17819 			ipif_setdestlinklocal(ill->ill_ipif);
17820 		}
17821 		freemsg(addrmp2);
17822 		break;
17823 
17824 	case DL_CURR_PHYS_ADDR:
17825 		freemsg(ill->ill_phys_addr_mp);
17826 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17827 		ill->ill_phys_addr_mp = addrmp;
17828 		ill->ill_phys_addr_length = addrlen;
17829 		if (ill->ill_isv6)
17830 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17831 		else
17832 			freemsg(addrmp2);
17833 		if (ill->ill_isv6) {
17834 			ill_setdefaulttoken(ill);
17835 			ipif_setlinklocal(ill->ill_ipif);
17836 		}
17837 		break;
17838 	default:
17839 		ASSERT(0);
17840 	}
17841 
17842 	/*
17843 	 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17844 	 * as we bring the ipifs up again.
17845 	 */
17846 	mutex_enter(&ill->ill_lock);
17847 	ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17848 	mutex_exit(&ill->ill_lock);
17849 	/*
17850 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17851 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17852 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17853 	 * brought up.
17854 	 */
17855 	status = ill_up_ipifs(ill, q, addrmp);
17856 	if (status != EINPROGRESS)
17857 		ipsq_current_finish(ipsq);
17858 }
17859 
17860 /*
17861  * Helper routine for setting the ill_nd_lla fields.
17862  */
17863 void
17864 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17865 {
17866 	freemsg(ill->ill_nd_lla_mp);
17867 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17868 	ill->ill_nd_lla_mp = ndmp;
17869 	ill->ill_nd_lla_len = addrlen;
17870 }
17871 
17872 /*
17873  * Replumb the ill.
17874  */
17875 int
17876 ill_replumb(ill_t *ill, mblk_t *mp)
17877 {
17878 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17879 
17880 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17881 
17882 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17883 
17884 	/*
17885 	 * If we can quiesce the ill, then continue.  If not, then
17886 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17887 	 */
17888 	ill_down_ipifs(ill, B_FALSE);
17889 
17890 	mutex_enter(&ill->ill_lock);
17891 	if (!ill_is_quiescent(ill)) {
17892 		/* call cannot fail since `conn_t *' argument is NULL */
17893 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17894 		    mp, ILL_DOWN);
17895 		mutex_exit(&ill->ill_lock);
17896 		return (EINPROGRESS);
17897 	}
17898 	mutex_exit(&ill->ill_lock);
17899 
17900 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17901 	return (0);
17902 }
17903 
17904 /* ARGSUSED */
17905 static void
17906 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17907 {
17908 	ill_t *ill = q->q_ptr;
17909 	int err;
17910 	conn_t *connp = NULL;
17911 
17912 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17913 	freemsg(ill->ill_replumb_mp);
17914 	ill->ill_replumb_mp = copyb(mp);
17915 
17916 	if (ill->ill_replumb_mp == NULL) {
17917 		/* out of memory */
17918 		ipsq_current_finish(ipsq);
17919 		return;
17920 	}
17921 
17922 	mutex_enter(&ill->ill_lock);
17923 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17924 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17925 	mutex_exit(&ill->ill_lock);
17926 
17927 	if (!ill->ill_up_ipifs) {
17928 		/* already closing */
17929 		ipsq_current_finish(ipsq);
17930 		return;
17931 	}
17932 	ill->ill_replumbing = 1;
17933 	err = ill_down_ipifs_tail(ill);
17934 
17935 	/*
17936 	 * Successfully quiesced and brought down the interface, now we send
17937 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17938 	 * DL_NOTE_REPLUMB message.
17939 	 */
17940 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17941 	    DL_NOTIFY_CONF);
17942 	ASSERT(mp != NULL);
17943 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17944 	    DL_NOTE_REPLUMB_DONE;
17945 	ill_dlpi_send(ill, mp);
17946 
17947 	/*
17948 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17949 	 * streams have to be unbound. When all the DLPI exchanges are done,
17950 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17951 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17952 	 * arp_bringup_done().
17953 	 */
17954 	ASSERT(ill->ill_replumb_mp != NULL);
17955 	if (err == EINPROGRESS)
17956 		return;
17957 	else
17958 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17959 	ASSERT(connp == NULL);
17960 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17961 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17962 		return;
17963 	}
17964 	ipsq_current_finish(ipsq);
17965 }
17966 
17967 /*
17968  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17969  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17970  * as per the ioctl.  On failure, an errno is returned.
17971  */
17972 static int
17973 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17974 {
17975 	int rval;
17976 	struct strioctl iocb;
17977 
17978 	iocb.ic_cmd = cmd;
17979 	iocb.ic_timout = 15;
17980 	iocb.ic_len = bufsize;
17981 	iocb.ic_dp = buf;
17982 
17983 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17984 }
17985 
17986 /*
17987  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17988  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17989  */
17990 static int
17991 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17992     uint_t *bufsizep, cred_t *cr)
17993 {
17994 	int err;
17995 	struct lifnum lifn;
17996 
17997 	bzero(&lifn, sizeof (lifn));
17998 	lifn.lifn_family = af;
17999 	lifn.lifn_flags = LIFC_UNDER_IPMP;
18000 
18001 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
18002 		return (err);
18003 
18004 	/*
18005 	 * Pad the interface count to account for additional interfaces that
18006 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18007 	 */
18008 	lifn.lifn_count += 4;
18009 	bzero(lifcp, sizeof (*lifcp));
18010 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
18011 	lifcp->lifc_family = af;
18012 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18013 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18014 
18015 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18016 	if (err != 0) {
18017 		kmem_free(lifcp->lifc_buf, *bufsizep);
18018 		return (err);
18019 	}
18020 
18021 	return (0);
18022 }
18023 
18024 /*
18025  * Helper for ip_interface_cleanup() that removes the loopback interface.
18026  */
18027 static void
18028 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18029 {
18030 	int err;
18031 	struct lifreq lifr;
18032 
18033 	bzero(&lifr, sizeof (lifr));
18034 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
18035 
18036 	/*
18037 	 * Attempt to remove the interface.  It may legitimately not exist
18038 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18039 	 */
18040 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18041 	if (err != 0 && err != ENXIO) {
18042 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18043 		    "error %d\n", isv6 ? "v6" : "v4", err));
18044 	}
18045 }
18046 
18047 /*
18048  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18049  * groups and that IPMP data addresses are down.  These conditions must be met
18050  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18051  */
18052 static void
18053 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18054 {
18055 	int af = isv6 ? AF_INET6 : AF_INET;
18056 	int i, nifs;
18057 	int err;
18058 	uint_t bufsize;
18059 	uint_t lifrsize = sizeof (struct lifreq);
18060 	struct lifconf lifc;
18061 	struct lifreq *lifrp;
18062 
18063 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18064 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18065 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
18066 		return;
18067 	}
18068 
18069 	nifs = lifc.lifc_len / lifrsize;
18070 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18071 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18072 		if (err != 0) {
18073 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18074 			    "flags: error %d", lifrp->lifr_name, err);
18075 			continue;
18076 		}
18077 
18078 		if (lifrp->lifr_flags & IFF_IPMP) {
18079 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18080 				continue;
18081 
18082 			lifrp->lifr_flags &= ~IFF_UP;
18083 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18084 			if (err != 0) {
18085 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18086 				    "bring down (error %d); IPMP interface may "
18087 				    "not be shutdown", lifrp->lifr_name, err);
18088 			}
18089 
18090 			/*
18091 			 * Check if IFF_DUPLICATE is still set -- and if so,
18092 			 * reset the address to clear it.
18093 			 */
18094 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18095 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18096 				continue;
18097 
18098 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18099 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18100 			    lifrp, lifrsize, cr)) != 0) {
18101 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18102 				    "reset DAD (error %d); IPMP interface may "
18103 				    "not be shutdown", lifrp->lifr_name, err);
18104 			}
18105 			continue;
18106 		}
18107 
18108 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18109 			lifrp->lifr_groupname[0] = '\0';
18110 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18111 			    lifrsize, cr)) != 0) {
18112 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18113 				    "leave IPMP group (error %d); associated "
18114 				    "IPMP interface may not be shutdown",
18115 				    lifrp->lifr_name, err);
18116 				continue;
18117 			}
18118 		}
18119 	}
18120 
18121 	kmem_free(lifc.lifc_buf, bufsize);
18122 }
18123 
18124 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
18125 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
18126 
18127 /*
18128  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18129  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18130  * when the user-level processes in the zone are killed and the latter are
18131  * cleaned up by str_stack_shutdown().
18132  */
18133 void
18134 ip_interface_cleanup(ip_stack_t *ipst)
18135 {
18136 	ldi_handle_t	lh;
18137 	ldi_ident_t	li;
18138 	cred_t		*cr;
18139 	int		err;
18140 	int		i;
18141 	char		*devs[] = { UDP6DEV, UDPDEV };
18142 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18143 
18144 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18145 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18146 		    " error %d", err);
18147 		return;
18148 	}
18149 
18150 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18151 	ASSERT(cr != NULL);
18152 
18153 	/*
18154 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18155 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18156 	 * the loop.)
18157 	 */
18158 	for (i = 0; i < 2; i++) {
18159 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18160 		if (err != 0) {
18161 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18162 			    " error %d", devs[i], err);
18163 			continue;
18164 		}
18165 
18166 		ip_loopback_removeif(lh, i == 0, cr);
18167 		ip_ipmp_cleanup(lh, i == 0, cr);
18168 
18169 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18170 	}
18171 
18172 	ldi_ident_release(li);
18173 	crfree(cr);
18174 }
18175 
18176 /*
18177  * This needs to be in-sync with nic_event_t definition
18178  */
18179 static const char *
18180 ill_hook_event2str(nic_event_t event)
18181 {
18182 	switch (event) {
18183 	case NE_PLUMB:
18184 		return ("PLUMB");
18185 	case NE_UNPLUMB:
18186 		return ("UNPLUMB");
18187 	case NE_UP:
18188 		return ("UP");
18189 	case NE_DOWN:
18190 		return ("DOWN");
18191 	case NE_ADDRESS_CHANGE:
18192 		return ("ADDRESS_CHANGE");
18193 	case NE_LIF_UP:
18194 		return ("LIF_UP");
18195 	case NE_LIF_DOWN:
18196 		return ("LIF_DOWN");
18197 	case NE_IFINDEX_CHANGE:
18198 		return ("IFINDEX_CHANGE");
18199 	default:
18200 		return ("UNKNOWN");
18201 	}
18202 }
18203 
18204 void
18205 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18206     nic_event_data_t data, size_t datalen)
18207 {
18208 	ip_stack_t		*ipst = ill->ill_ipst;
18209 	hook_nic_event_int_t	*info;
18210 	const char		*str = NULL;
18211 
18212 	/* create a new nic event info */
18213 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18214 		goto fail;
18215 
18216 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18217 	info->hnei_event.hne_lif = lif;
18218 	info->hnei_event.hne_event = event;
18219 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18220 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18221 	info->hnei_event.hne_data = NULL;
18222 	info->hnei_event.hne_datalen = 0;
18223 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18224 
18225 	if (data != NULL && datalen != 0) {
18226 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18227 		if (info->hnei_event.hne_data == NULL)
18228 			goto fail;
18229 		bcopy(data, info->hnei_event.hne_data, datalen);
18230 		info->hnei_event.hne_datalen = datalen;
18231 	}
18232 
18233 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18234 	    DDI_NOSLEEP) == DDI_SUCCESS)
18235 		return;
18236 
18237 fail:
18238 	if (info != NULL) {
18239 		if (info->hnei_event.hne_data != NULL) {
18240 			kmem_free(info->hnei_event.hne_data,
18241 			    info->hnei_event.hne_datalen);
18242 		}
18243 		kmem_free(info, sizeof (hook_nic_event_t));
18244 	}
18245 	str = ill_hook_event2str(event);
18246 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18247 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18248 }
18249 
18250 static int
18251 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18252 {
18253 	int		err = 0;
18254 	const in_addr_t	*addr = NULL;
18255 	nce_t		*nce = NULL;
18256 	ill_t		*ill = ipif->ipif_ill;
18257 	ill_t		*bound_ill;
18258 	boolean_t	added_ipif = B_FALSE;
18259 	uint16_t	state;
18260 	uint16_t	flags;
18261 
18262 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18263 	    ill_t *, ill, ipif_t *, ipif);
18264 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18265 		addr = &ipif->ipif_lcl_addr;
18266 	}
18267 
18268 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18269 		if (res_act != Res_act_initial)
18270 			return (EINVAL);
18271 	}
18272 
18273 	if (addr != NULL) {
18274 		ipmp_illgrp_t	*illg = ill->ill_grp;
18275 
18276 		/* add unicast nce for the local addr */
18277 
18278 		if (IS_IPMP(ill)) {
18279 			/*
18280 			 * If we're here via ipif_up(), then the ipif
18281 			 * won't be bound yet -- add it to the group,
18282 			 * which will bind it if possible. (We would
18283 			 * add it in ipif_up(), but deleting on failure
18284 			 * there is gruesome.)  If we're here via
18285 			 * ipmp_ill_bind_ipif(), then the ipif has
18286 			 * already been added to the group and we
18287 			 * just need to use the binding.
18288 			 */
18289 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18290 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18291 				if (bound_ill == NULL) {
18292 					/*
18293 					 * We couldn't bind the ipif to an ill
18294 					 * yet, so we have nothing to publish.
18295 					 * Mark the address as ready and return.
18296 					 */
18297 					ipif->ipif_addr_ready = 1;
18298 					return (0);
18299 				}
18300 				added_ipif = B_TRUE;
18301 			}
18302 		} else {
18303 			bound_ill = ill;
18304 		}
18305 
18306 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18307 		    NCE_F_NONUD);
18308 		/*
18309 		 * If this is an initial bring-up (or the ipif was never
18310 		 * completely brought up), do DAD.  Otherwise, we're here
18311 		 * because IPMP has rebound an address to this ill: send
18312 		 * unsolicited advertisements (ARP announcements) to
18313 		 * inform others.
18314 		 */
18315 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18316 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18317 		} else {
18318 			state = ND_REACHABLE;
18319 			flags |= NCE_F_UNSOL_ADV;
18320 		}
18321 
18322 retry:
18323 		err = nce_lookup_then_add_v4(ill,
18324 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18325 		    addr, flags, state, &nce);
18326 
18327 		/*
18328 		 * note that we may encounter EEXIST if we are moving
18329 		 * the nce as a result of a rebind operation.
18330 		 */
18331 		switch (err) {
18332 		case 0:
18333 			ipif->ipif_added_nce = 1;
18334 			nce->nce_ipif_cnt++;
18335 			break;
18336 		case EEXIST:
18337 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18338 			    ill->ill_name));
18339 			if (!NCE_MYADDR(nce->nce_common)) {
18340 				/*
18341 				 * A leftover nce from before this address
18342 				 * existed
18343 				 */
18344 				ncec_delete(nce->nce_common);
18345 				nce_refrele(nce);
18346 				nce = NULL;
18347 				goto retry;
18348 			}
18349 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18350 				nce_refrele(nce);
18351 				nce = NULL;
18352 				ip1dbg(("ipif_arp_up: NCE already exists "
18353 				    "for %s:%u\n", ill->ill_name,
18354 				    ipif->ipif_id));
18355 				goto arp_up_done;
18356 			}
18357 			/*
18358 			 * Duplicate local addresses are permissible for
18359 			 * IPIF_POINTOPOINT interfaces which will get marked
18360 			 * IPIF_UNNUMBERED later in
18361 			 * ip_addr_availability_check().
18362 			 *
18363 			 * The nce_ipif_cnt field tracks the number of
18364 			 * ipifs that have nce_addr as their local address.
18365 			 */
18366 			ipif->ipif_addr_ready = 1;
18367 			ipif->ipif_added_nce = 1;
18368 			nce->nce_ipif_cnt++;
18369 			err = 0;
18370 			break;
18371 		default:
18372 			ASSERT(nce == NULL);
18373 			goto arp_up_done;
18374 		}
18375 		if (arp_no_defense) {
18376 			if ((ipif->ipif_flags & IPIF_UP) &&
18377 			    !ipif->ipif_addr_ready)
18378 				ipif_up_notify(ipif);
18379 			ipif->ipif_addr_ready = 1;
18380 		}
18381 	} else {
18382 		/* zero address. nothing to publish */
18383 		ipif->ipif_addr_ready = 1;
18384 	}
18385 	if (nce != NULL)
18386 		nce_refrele(nce);
18387 arp_up_done:
18388 	if (added_ipif && err != 0)
18389 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18390 	return (err);
18391 }
18392 
18393 int
18394 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18395 {
18396 	int 		err = 0;
18397 	ill_t 		*ill = ipif->ipif_ill;
18398 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18399 
18400 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18401 	    ill_t *, ill, ipif_t *, ipif);
18402 
18403 	/*
18404 	 * need to bring up ARP or setup mcast mapping only
18405 	 * when the first interface is coming UP.
18406 	 */
18407 	first_interface = (ill->ill_ipif_up_count == 0 &&
18408 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18409 
18410 	if (res_act == Res_act_initial && first_interface) {
18411 		/*
18412 		 * Send ATTACH + BIND
18413 		 */
18414 		err = arp_ll_up(ill);
18415 		if (err != EINPROGRESS && err != 0)
18416 			return (err);
18417 
18418 		/*
18419 		 * Add NCE for local address. Start DAD.
18420 		 * we'll wait to hear that DAD has finished
18421 		 * before using the interface.
18422 		 */
18423 		if (err == EINPROGRESS)
18424 			wait_for_dlpi = B_TRUE;
18425 	}
18426 
18427 	if (!wait_for_dlpi)
18428 		(void) ipif_arp_up_done_tail(ipif, res_act);
18429 
18430 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18431 }
18432 
18433 /*
18434  * Finish processing of "arp_up" after all the DLPI message
18435  * exchanges have completed between arp and the driver.
18436  */
18437 void
18438 arp_bringup_done(ill_t *ill, int err)
18439 {
18440 	mblk_t	*mp1;
18441 	ipif_t  *ipif;
18442 	conn_t *connp = NULL;
18443 	ipsq_t	*ipsq;
18444 	queue_t *q;
18445 
18446 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18447 
18448 	ASSERT(IAM_WRITER_ILL(ill));
18449 
18450 	ipsq = ill->ill_phyint->phyint_ipsq;
18451 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18452 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18453 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18454 	if (mp1 == NULL) /* bringup was aborted by the user */
18455 		return;
18456 
18457 	/*
18458 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18459 	 * must have an associated conn_t.  Otherwise, we're bringing this
18460 	 * interface back up as part of handling an asynchronous event (e.g.,
18461 	 * physical address change).
18462 	 */
18463 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18464 		ASSERT(connp != NULL);
18465 		q = CONNP_TO_WQ(connp);
18466 	} else {
18467 		ASSERT(connp == NULL);
18468 		q = ill->ill_rq;
18469 	}
18470 	if (err == 0) {
18471 		if (ipif->ipif_isv6) {
18472 			if ((err = ipif_up_done_v6(ipif)) != 0)
18473 				ip0dbg(("arp_bringup_done: init failed\n"));
18474 		} else {
18475 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18476 			if (err != 0 ||
18477 			    (err = ipif_up_done(ipif)) != 0) {
18478 				ip0dbg(("arp_bringup_done: "
18479 				    "init failed err %x\n", err));
18480 				(void) ipif_arp_down(ipif);
18481 			}
18482 
18483 		}
18484 	} else {
18485 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18486 	}
18487 
18488 	if ((err == 0) && (ill->ill_up_ipifs)) {
18489 		err = ill_up_ipifs(ill, q, mp1);
18490 		if (err == EINPROGRESS)
18491 			return;
18492 	}
18493 
18494 	/*
18495 	 * If we have a moved ipif to bring up, and everything has succeeded
18496 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18497 	 * down -- the admin can try to bring it up by hand if need be.
18498 	 */
18499 	if (ill->ill_move_ipif != NULL) {
18500 		ipif = ill->ill_move_ipif;
18501 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18502 		    ipif->ipif_ill->ill_name));
18503 		ill->ill_move_ipif = NULL;
18504 		if (err == 0) {
18505 			err = ipif_up(ipif, q, mp1);
18506 			if (err == EINPROGRESS)
18507 				return;
18508 		}
18509 	}
18510 
18511 	/*
18512 	 * The operation must complete without EINPROGRESS since
18513 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18514 	 * Otherwise, the operation will be stuck forever in the ipsq.
18515 	 */
18516 	ASSERT(err != EINPROGRESS);
18517 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18518 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18519 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18520 		    ill_t *, ill, ipif_t *, ipif);
18521 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18522 	} else {
18523 		ipsq_current_finish(ipsq);
18524 	}
18525 }
18526 
18527 /*
18528  * Finish processing of arp replumb after all the DLPI message
18529  * exchanges have completed between arp and the driver.
18530  */
18531 void
18532 arp_replumb_done(ill_t *ill, int err)
18533 {
18534 	mblk_t	*mp1;
18535 	ipif_t  *ipif;
18536 	conn_t *connp = NULL;
18537 	ipsq_t	*ipsq;
18538 	queue_t *q;
18539 
18540 	ASSERT(IAM_WRITER_ILL(ill));
18541 
18542 	ipsq = ill->ill_phyint->phyint_ipsq;
18543 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18544 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18545 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18546 	if (mp1 == NULL) {
18547 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18548 		    ipsq->ipsq_xop->ipx_current_ioctl));
18549 		/* bringup was aborted by the user */
18550 		return;
18551 	}
18552 	/*
18553 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18554 	 * must have an associated conn_t.  Otherwise, we're bringing this
18555 	 * interface back up as part of handling an asynchronous event (e.g.,
18556 	 * physical address change).
18557 	 */
18558 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18559 		ASSERT(connp != NULL);
18560 		q = CONNP_TO_WQ(connp);
18561 	} else {
18562 		ASSERT(connp == NULL);
18563 		q = ill->ill_rq;
18564 	}
18565 	if ((err == 0) && (ill->ill_up_ipifs)) {
18566 		err = ill_up_ipifs(ill, q, mp1);
18567 		if (err == EINPROGRESS)
18568 			return;
18569 	}
18570 	/*
18571 	 * The operation must complete without EINPROGRESS since
18572 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18573 	 * Otherwise, the operation will be stuck forever in the ipsq.
18574 	 */
18575 	ASSERT(err != EINPROGRESS);
18576 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18577 		DTRACE_PROBE4(ipif__ioctl, char *,
18578 		    "arp_replumb_done finish",
18579 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18580 		    ill_t *, ill, ipif_t *, ipif);
18581 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18582 	} else {
18583 		ipsq_current_finish(ipsq);
18584 	}
18585 }
18586 
18587 void
18588 ipif_up_notify(ipif_t *ipif)
18589 {
18590 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18591 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18592 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18593 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18594 	    NE_LIF_UP, NULL, 0);
18595 }
18596 
18597 /*
18598  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18599  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18600  * TPI end points with STREAMS modules pushed above.  This is assured by not
18601  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18602  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18603  * while unwinding from the ispq and that could be a thread from the bottom.
18604  */
18605 /* ARGSUSED */
18606 int
18607 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18608     ip_ioctl_cmd_t *ipip, void *arg)
18609 {
18610 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18611 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18612 	int ret = 0;
18613 	int i;
18614 	size_t size;
18615 	ip_stack_t *ipst;
18616 	zoneid_t zoneid;
18617 	ilb_stack_t *ilbs;
18618 
18619 	ipst = CONNQ_TO_IPST(q);
18620 	ilbs = ipst->ips_netstack->netstack_ilb;
18621 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18622 
18623 	switch (command) {
18624 	case ILB_CREATE_RULE: {
18625 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18626 
18627 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18628 			ret = EINVAL;
18629 			break;
18630 		}
18631 
18632 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18633 		break;
18634 	}
18635 	case ILB_DESTROY_RULE:
18636 	case ILB_ENABLE_RULE:
18637 	case ILB_DISABLE_RULE: {
18638 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18639 
18640 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18641 			ret = EINVAL;
18642 			break;
18643 		}
18644 
18645 		if (cmd->flags & ILB_RULE_ALLRULES) {
18646 			if (command == ILB_DESTROY_RULE) {
18647 				ilb_rule_del_all(ilbs, zoneid);
18648 				break;
18649 			} else if (command == ILB_ENABLE_RULE) {
18650 				ilb_rule_enable_all(ilbs, zoneid);
18651 				break;
18652 			} else if (command == ILB_DISABLE_RULE) {
18653 				ilb_rule_disable_all(ilbs, zoneid);
18654 				break;
18655 			}
18656 		} else {
18657 			if (command == ILB_DESTROY_RULE) {
18658 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18659 			} else if (command == ILB_ENABLE_RULE) {
18660 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18661 				    NULL);
18662 			} else if (command == ILB_DISABLE_RULE) {
18663 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18664 				    NULL);
18665 			}
18666 		}
18667 		break;
18668 	}
18669 	case ILB_NUM_RULES: {
18670 		ilb_num_rules_cmd_t *cmd;
18671 
18672 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18673 			ret = EINVAL;
18674 			break;
18675 		}
18676 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18677 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18678 		break;
18679 	}
18680 	case ILB_RULE_NAMES: {
18681 		ilb_rule_names_cmd_t *cmd;
18682 
18683 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18684 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18685 		    cmd->num_names == 0) {
18686 			ret = EINVAL;
18687 			break;
18688 		}
18689 		size = cmd->num_names * ILB_RULE_NAMESZ;
18690 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18691 		    size != cmd_mp->b_wptr) {
18692 			ret = EINVAL;
18693 			break;
18694 		}
18695 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18696 		break;
18697 	}
18698 	case ILB_NUM_SERVERS: {
18699 		ilb_num_servers_cmd_t *cmd;
18700 
18701 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18702 			ret = EINVAL;
18703 			break;
18704 		}
18705 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18706 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18707 		    &(cmd->num));
18708 		break;
18709 	}
18710 	case ILB_LIST_RULE: {
18711 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18712 
18713 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18714 			ret = EINVAL;
18715 			break;
18716 		}
18717 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18718 		break;
18719 	}
18720 	case ILB_LIST_SERVERS: {
18721 		ilb_servers_info_cmd_t *cmd;
18722 
18723 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18724 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18725 		    cmd->num_servers == 0) {
18726 			ret = EINVAL;
18727 			break;
18728 		}
18729 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18730 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18731 		    size != cmd_mp->b_wptr) {
18732 			ret = EINVAL;
18733 			break;
18734 		}
18735 
18736 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18737 		    &cmd->num_servers);
18738 		break;
18739 	}
18740 	case ILB_ADD_SERVERS: {
18741 		ilb_servers_info_cmd_t *cmd;
18742 		ilb_rule_t *rule;
18743 
18744 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18745 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18746 			ret = EINVAL;
18747 			break;
18748 		}
18749 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18750 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18751 		    size != cmd_mp->b_wptr) {
18752 			ret = EINVAL;
18753 			break;
18754 		}
18755 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18756 		if (rule == NULL) {
18757 			ASSERT(ret != 0);
18758 			break;
18759 		}
18760 		for (i = 0; i < cmd->num_servers; i++) {
18761 			ilb_server_info_t *s;
18762 
18763 			s = &cmd->servers[i];
18764 			s->err = ilb_server_add(ilbs, rule, s);
18765 		}
18766 		ILB_RULE_REFRELE(rule);
18767 		break;
18768 	}
18769 	case ILB_DEL_SERVERS:
18770 	case ILB_ENABLE_SERVERS:
18771 	case ILB_DISABLE_SERVERS: {
18772 		ilb_servers_cmd_t *cmd;
18773 		ilb_rule_t *rule;
18774 		int (*f)();
18775 
18776 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18777 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18778 			ret = EINVAL;
18779 			break;
18780 		}
18781 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18782 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18783 		    size != cmd_mp->b_wptr) {
18784 			ret = EINVAL;
18785 			break;
18786 		}
18787 
18788 		if (command == ILB_DEL_SERVERS)
18789 			f = ilb_server_del;
18790 		else if (command == ILB_ENABLE_SERVERS)
18791 			f = ilb_server_enable;
18792 		else if (command == ILB_DISABLE_SERVERS)
18793 			f = ilb_server_disable;
18794 
18795 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18796 		if (rule == NULL) {
18797 			ASSERT(ret != 0);
18798 			break;
18799 		}
18800 
18801 		for (i = 0; i < cmd->num_servers; i++) {
18802 			ilb_server_arg_t *s;
18803 
18804 			s = &cmd->servers[i];
18805 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18806 		}
18807 		ILB_RULE_REFRELE(rule);
18808 		break;
18809 	}
18810 	case ILB_LIST_NAT_TABLE: {
18811 		ilb_list_nat_cmd_t *cmd;
18812 
18813 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18814 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18815 			ret = EINVAL;
18816 			break;
18817 		}
18818 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18819 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18820 		    size != cmd_mp->b_wptr) {
18821 			ret = EINVAL;
18822 			break;
18823 		}
18824 
18825 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18826 		    &cmd->flags);
18827 		break;
18828 	}
18829 	case ILB_LIST_STICKY_TABLE: {
18830 		ilb_list_sticky_cmd_t *cmd;
18831 
18832 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18833 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18834 			ret = EINVAL;
18835 			break;
18836 		}
18837 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18838 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18839 		    size != cmd_mp->b_wptr) {
18840 			ret = EINVAL;
18841 			break;
18842 		}
18843 
18844 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18845 		    &cmd->num_sticky, &cmd->flags);
18846 		break;
18847 	}
18848 	default:
18849 		ret = EINVAL;
18850 		break;
18851 	}
18852 done:
18853 	return (ret);
18854 }
18855 
18856 /* Remove all cache entries for this logical interface */
18857 void
18858 ipif_nce_down(ipif_t *ipif)
18859 {
18860 	ill_t *ill = ipif->ipif_ill;
18861 	nce_t *nce;
18862 
18863 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18864 	    ill_t *, ill, ipif_t *, ipif);
18865 	if (ipif->ipif_added_nce) {
18866 		if (ipif->ipif_isv6)
18867 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18868 		else
18869 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18870 		if (nce != NULL) {
18871 			if (--nce->nce_ipif_cnt == 0)
18872 				ncec_delete(nce->nce_common);
18873 			ipif->ipif_added_nce = 0;
18874 			nce_refrele(nce);
18875 		} else {
18876 			/*
18877 			 * nce may already be NULL because it was already
18878 			 * flushed, e.g., due to a call to nce_flush
18879 			 */
18880 			ipif->ipif_added_nce = 0;
18881 		}
18882 	}
18883 	/*
18884 	 * Make IPMP aware of the deleted data address.
18885 	 */
18886 	if (IS_IPMP(ill))
18887 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18888 
18889 	/*
18890 	 * Remove all other nces dependent on this ill when the last ipif
18891 	 * is going away.
18892 	 */
18893 	if (ill->ill_ipif_up_count == 0) {
18894 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18895 		    (uchar_t *)ill, ill->ill_ipst);
18896 		if (IS_UNDER_IPMP(ill))
18897 			nce_flush(ill, B_TRUE);
18898 	}
18899 }
18900 
18901 /*
18902  * find the first interface that uses usill for its source address.
18903  */
18904 ill_t *
18905 ill_lookup_usesrc(ill_t *usill)
18906 {
18907 	ip_stack_t *ipst = usill->ill_ipst;
18908 	ill_t *ill;
18909 
18910 	ASSERT(usill != NULL);
18911 
18912 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18913 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18914 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18915 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18916 	    ill = ill->ill_usesrc_grp_next) {
18917 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18918 		    !ILL_IS_CONDEMNED(ill)) {
18919 			ill_refhold(ill);
18920 			break;
18921 		}
18922 	}
18923 	rw_exit(&ipst->ips_ill_g_lock);
18924 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18925 	return (ill);
18926 }
18927