xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1)
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 #include <sys/mac_flow.h>
98 
99 #include <sys/systeminfo.h>
100 #include <sys/bootconf.h>
101 
102 #include <sys/tsol/tndb.h>
103 #include <sys/tsol/tnet.h>
104 
105 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
106 #include <inet/udp_impl.h> /* needed for udp_stack_t */
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
122 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
123 		    char *value, caddr_t cp, cred_t *ioc_cr);
124 
125 static boolean_t ill_is_quiescent(ill_t *);
126 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
127 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
128 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
129     mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131     mblk_t *mp, boolean_t need_up);
132 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
133     queue_t *q, mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
135     mblk_t *mp);
136 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp);
138 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
139     queue_t *q, mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
141     int ioccmd, struct linkblk *li);
142 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
143 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
144 static void	ipsq_flush(ill_t *ill);
145 
146 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static void	ipsq_delete(ipsq_t *);
149 
150 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
151     boolean_t initialize, boolean_t insert, int *errorp);
152 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
153 static void	ipif_delete_bcast_ires(ipif_t *ipif);
154 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
155 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
156 		    boolean_t isv6);
157 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
158 static void	ipif_free(ipif_t *ipif);
159 static void	ipif_free_tail(ipif_t *ipif);
160 static void	ipif_set_default(ipif_t *ipif);
161 static int	ipif_set_values(queue_t *q, mblk_t *mp,
162     char *interf_name, uint_t *ppa);
163 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
164     queue_t *q);
165 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
166     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
167     ip_stack_t *);
168 static ipif_t	*ipif_lookup_on_name_async(char *name, size_t namelen,
169     boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
170     int *error, ip_stack_t *);
171 
172 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
173 static void	ill_delete_interface_type(ill_if_t *);
174 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
175 static void	ill_dl_down(ill_t *ill);
176 static void	ill_down(ill_t *ill);
177 static void	ill_down_ipifs(ill_t *, boolean_t);
178 static void	ill_free_mib(ill_t *ill);
179 static void	ill_glist_delete(ill_t *);
180 static void	ill_phyint_reinit(ill_t *ill);
181 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
182 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
183 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
184 
185 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
186 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
187 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
188 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
189 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
190 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
191 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
192 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
193 static ip_v4mapinfo_func_t ip_mbcast_mapping;
194 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
195 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
196 static void	phyint_free(phyint_t *);
197 
198 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
199 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
200 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
201 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
202 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
203 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
204     dl_capability_sub_t *);
205 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
206 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
207 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
208 		    dl_capability_sub_t *);
209 static void	ill_capability_dld_enable(ill_t *);
210 static void	ill_capability_ack_thr(void *);
211 static void	ill_capability_lso_enable(ill_t *);
212 
213 static ill_t	*ill_prev_usesrc(ill_t *);
214 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
215 static void	ill_disband_usesrc_group(ill_t *);
216 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
217 
218 #ifdef DEBUG
219 static	void	ill_trace_cleanup(const ill_t *);
220 static	void	ipif_trace_cleanup(const ipif_t *);
221 #endif
222 
223 static	void	ill_dlpi_clear_deferred(ill_t *ill);
224 
225 /*
226  * if we go over the memory footprint limit more than once in this msec
227  * interval, we'll start pruning aggressively.
228  */
229 int ip_min_frag_prune_time = 0;
230 
231 static ipft_t	ip_ioctl_ftbl[] = {
232 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
233 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
234 		IPFT_F_NO_REPLY },
235 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
236 	{ 0 }
237 };
238 
239 /* Simple ICMP IP Header Template */
240 static ipha_t icmp_ipha = {
241 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
242 };
243 
244 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
245 
246 static ip_m_t   ip_m_tbl[] = {
247 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
248 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
249 	    ip_nodef_v6intfid },
250 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
251 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
252 	    ip_nodef_v6intfid },
253 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
254 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
255 	    ip_nodef_v6intfid },
256 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
257 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
258 	    ip_nodef_v6intfid },
259 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
260 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
261 	    ip_nodef_v6intfid },
262 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
263 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
264 	    ip_nodef_v6intfid },
265 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
266 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
267 	    ip_ipv4_v6destintfid },
268 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
269 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
270 	    ip_ipv6_v6destintfid },
271 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
272 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
273 	    ip_nodef_v6intfid },
274 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
275 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
276 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
277 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
278 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
279 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
280 	    ip_nodef_v6intfid }
281 };
282 
283 static ill_t	ill_null;		/* Empty ILL for init. */
284 char	ipif_loopback_name[] = "lo0";
285 
286 /* These are used by all IP network modules. */
287 sin6_t	sin6_null;	/* Zero address for quick clears */
288 sin_t	sin_null;	/* Zero address for quick clears */
289 
290 /* When set search for unused ipif_seqid */
291 static ipif_t	ipif_zero;
292 
293 /*
294  * ppa arena is created after these many
295  * interfaces have been plumbed.
296  */
297 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
298 
299 /*
300  * Allocate per-interface mibs.
301  * Returns true if ok. False otherwise.
302  *  ipsq  may not yet be allocated (loopback case ).
303  */
304 static boolean_t
305 ill_allocate_mibs(ill_t *ill)
306 {
307 	/* Already allocated? */
308 	if (ill->ill_ip_mib != NULL) {
309 		if (ill->ill_isv6)
310 			ASSERT(ill->ill_icmp6_mib != NULL);
311 		return (B_TRUE);
312 	}
313 
314 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
315 	    KM_NOSLEEP);
316 	if (ill->ill_ip_mib == NULL) {
317 		return (B_FALSE);
318 	}
319 
320 	/* Setup static information */
321 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
322 	    sizeof (mib2_ipIfStatsEntry_t));
323 	if (ill->ill_isv6) {
324 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
325 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
326 		    sizeof (mib2_ipv6AddrEntry_t));
327 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
328 		    sizeof (mib2_ipv6RouteEntry_t));
329 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
330 		    sizeof (mib2_ipv6NetToMediaEntry_t));
331 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
332 		    sizeof (ipv6_member_t));
333 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
334 		    sizeof (ipv6_grpsrc_t));
335 	} else {
336 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
337 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
338 		    sizeof (mib2_ipAddrEntry_t));
339 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
340 		    sizeof (mib2_ipRouteEntry_t));
341 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
342 		    sizeof (mib2_ipNetToMediaEntry_t));
343 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
344 		    sizeof (ip_member_t));
345 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
346 		    sizeof (ip_grpsrc_t));
347 
348 		/*
349 		 * For a v4 ill, we are done at this point, because per ill
350 		 * icmp mibs are only used for v6.
351 		 */
352 		return (B_TRUE);
353 	}
354 
355 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
356 	    KM_NOSLEEP);
357 	if (ill->ill_icmp6_mib == NULL) {
358 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
359 		ill->ill_ip_mib = NULL;
360 		return (B_FALSE);
361 	}
362 	/* static icmp info */
363 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
364 	    sizeof (mib2_ipv6IfIcmpEntry_t);
365 	/*
366 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
367 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
368 	 * -> ill_phyint_reinit
369 	 */
370 	return (B_TRUE);
371 }
372 
373 /*
374  * Completely vaporize a lower level tap and all associated interfaces.
375  * ill_delete is called only out of ip_close when the device control
376  * stream is being closed.
377  */
378 void
379 ill_delete(ill_t *ill)
380 {
381 	ipif_t	*ipif;
382 	ill_t	*prev_ill;
383 	ip_stack_t	*ipst = ill->ill_ipst;
384 
385 	/*
386 	 * ill_delete may be forcibly entering the ipsq. The previous
387 	 * ioctl may not have completed and may need to be aborted.
388 	 * ipsq_flush takes care of it. If we don't need to enter the
389 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
390 	 * ill_delete_tail is sufficient.
391 	 */
392 	ipsq_flush(ill);
393 
394 	/*
395 	 * Nuke all interfaces.  ipif_free will take down the interface,
396 	 * remove it from the list, and free the data structure.
397 	 * Walk down the ipif list and remove the logical interfaces
398 	 * first before removing the main ipif. We can't unplumb
399 	 * zeroth interface first in the case of IPv6 as update_conn_ill
400 	 * -> ip_ll_multireq de-references ill_ipif for checking
401 	 * POINTOPOINT.
402 	 *
403 	 * If ill_ipif was not properly initialized (i.e low on memory),
404 	 * then no interfaces to clean up. In this case just clean up the
405 	 * ill.
406 	 */
407 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
408 		ipif_free(ipif);
409 
410 	/*
411 	 * clean out all the nce_t entries that depend on this
412 	 * ill for the ill_phys_addr.
413 	 */
414 	nce_flush(ill, B_TRUE);
415 
416 	/* Clean up msgs on pending upcalls for mrouted */
417 	reset_mrt_ill(ill);
418 
419 	update_conn_ill(ill, ipst);
420 
421 	/*
422 	 * Remove multicast references added as a result of calls to
423 	 * ip_join_allmulti().
424 	 */
425 	ip_purge_allmulti(ill);
426 
427 	/*
428 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
429 	 */
430 	if (IS_UNDER_IPMP(ill))
431 		ipmp_ill_leave_illgrp(ill);
432 
433 	/*
434 	 * ill_down will arrange to blow off any IRE's dependent on this
435 	 * ILL, and shut down fragmentation reassembly.
436 	 */
437 	ill_down(ill);
438 
439 	/* Let SCTP know, so that it can remove this from its list. */
440 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
441 
442 	/*
443 	 * Walk all CONNs that can have a reference on an ire or nce for this
444 	 * ill (we actually walk all that now have stale references).
445 	 */
446 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
447 
448 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
449 	if (ill->ill_isv6)
450 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
451 
452 	/*
453 	 * If an address on this ILL is being used as a source address then
454 	 * clear out the pointers in other ILLs that point to this ILL.
455 	 */
456 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
457 	if (ill->ill_usesrc_grp_next != NULL) {
458 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
459 			ill_disband_usesrc_group(ill);
460 		} else {	/* consumer of the usesrc ILL */
461 			prev_ill = ill_prev_usesrc(ill);
462 			prev_ill->ill_usesrc_grp_next =
463 			    ill->ill_usesrc_grp_next;
464 		}
465 	}
466 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
467 }
468 
469 static void
470 ipif_non_duplicate(ipif_t *ipif)
471 {
472 	ill_t *ill = ipif->ipif_ill;
473 	mutex_enter(&ill->ill_lock);
474 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
475 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
476 		ASSERT(ill->ill_ipif_dup_count > 0);
477 		ill->ill_ipif_dup_count--;
478 	}
479 	mutex_exit(&ill->ill_lock);
480 }
481 
482 /*
483  * ill_delete_tail is called from ip_modclose after all references
484  * to the closing ill are gone. The wait is done in ip_modclose
485  */
486 void
487 ill_delete_tail(ill_t *ill)
488 {
489 	mblk_t	**mpp;
490 	ipif_t	*ipif;
491 	ip_stack_t *ipst = ill->ill_ipst;
492 
493 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
494 		ipif_non_duplicate(ipif);
495 		(void) ipif_down_tail(ipif);
496 	}
497 
498 	ASSERT(ill->ill_ipif_dup_count == 0);
499 
500 	/*
501 	 * If polling capability is enabled (which signifies direct
502 	 * upcall into IP and driver has ill saved as a handle),
503 	 * we need to make sure that unbind has completed before we
504 	 * let the ill disappear and driver no longer has any reference
505 	 * to this ill.
506 	 */
507 	mutex_enter(&ill->ill_lock);
508 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
509 		cv_wait(&ill->ill_cv, &ill->ill_lock);
510 	mutex_exit(&ill->ill_lock);
511 	ASSERT(!(ill->ill_capabilities &
512 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
513 
514 	if (ill->ill_net_type != IRE_LOOPBACK)
515 		qprocsoff(ill->ill_rq);
516 
517 	/*
518 	 * We do an ipsq_flush once again now. New messages could have
519 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
520 	 * could also have landed up if an ioctl thread had looked up
521 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
522 	 * enqueued the ioctl when we did the ipsq_flush last time.
523 	 */
524 	ipsq_flush(ill);
525 
526 	/*
527 	 * Free capabilities.
528 	 */
529 	if (ill->ill_hcksum_capab != NULL) {
530 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
531 		ill->ill_hcksum_capab = NULL;
532 	}
533 
534 	if (ill->ill_zerocopy_capab != NULL) {
535 		kmem_free(ill->ill_zerocopy_capab,
536 		    sizeof (ill_zerocopy_capab_t));
537 		ill->ill_zerocopy_capab = NULL;
538 	}
539 
540 	if (ill->ill_lso_capab != NULL) {
541 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
542 		ill->ill_lso_capab = NULL;
543 	}
544 
545 	if (ill->ill_dld_capab != NULL) {
546 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
547 		ill->ill_dld_capab = NULL;
548 	}
549 
550 	/* Clean up ill_allowed_ips* related state */
551 	if (ill->ill_allowed_ips != NULL) {
552 		ASSERT(ill->ill_allowed_ips_cnt > 0);
553 		kmem_free(ill->ill_allowed_ips,
554 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
555 		ill->ill_allowed_ips = NULL;
556 		ill->ill_allowed_ips_cnt = 0;
557 	}
558 
559 	while (ill->ill_ipif != NULL)
560 		ipif_free_tail(ill->ill_ipif);
561 
562 	/*
563 	 * We have removed all references to ilm from conn and the ones joined
564 	 * within the kernel.
565 	 *
566 	 * We don't walk conns, mrts and ires because
567 	 *
568 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
569 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
570 	 *    ill references.
571 	 */
572 
573 	/*
574 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
575 	 * is safe to do because the illgrp has already been unlinked from the
576 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
577 	 */
578 	if (IS_IPMP(ill)) {
579 		ipmp_illgrp_destroy(ill->ill_grp);
580 		ill->ill_grp = NULL;
581 	}
582 
583 	if (ill->ill_mphysaddr_list != NULL) {
584 		multiphysaddr_t *mpa, *tmpa;
585 
586 		mpa = ill->ill_mphysaddr_list;
587 		ill->ill_mphysaddr_list = NULL;
588 		while (mpa) {
589 			tmpa = mpa->mpa_next;
590 			kmem_free(mpa, sizeof (*mpa));
591 			mpa = tmpa;
592 		}
593 	}
594 	/*
595 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
596 	 * could free the phyint. No more reference to the phyint after this
597 	 * point.
598 	 */
599 	(void) ill_glist_delete(ill);
600 
601 	if (ill->ill_frag_ptr != NULL) {
602 		uint_t count;
603 
604 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
605 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
606 		}
607 		mi_free(ill->ill_frag_ptr);
608 		ill->ill_frag_ptr = NULL;
609 		ill->ill_frag_hash_tbl = NULL;
610 	}
611 
612 	freemsg(ill->ill_nd_lla_mp);
613 	/* Free all retained control messages. */
614 	mpp = &ill->ill_first_mp_to_free;
615 	do {
616 		while (mpp[0]) {
617 			mblk_t  *mp;
618 			mblk_t  *mp1;
619 
620 			mp = mpp[0];
621 			mpp[0] = mp->b_next;
622 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
623 				mp1->b_next = NULL;
624 				mp1->b_prev = NULL;
625 			}
626 			freemsg(mp);
627 		}
628 	} while (mpp++ != &ill->ill_last_mp_to_free);
629 
630 	ill_free_mib(ill);
631 
632 #ifdef DEBUG
633 	ill_trace_cleanup(ill);
634 #endif
635 
636 	/* The default multicast interface might have changed */
637 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
638 
639 	/* Drop refcnt here */
640 	netstack_rele(ill->ill_ipst->ips_netstack);
641 	ill->ill_ipst = NULL;
642 }
643 
644 static void
645 ill_free_mib(ill_t *ill)
646 {
647 	ip_stack_t *ipst = ill->ill_ipst;
648 
649 	/*
650 	 * MIB statistics must not be lost, so when an interface
651 	 * goes away the counter values will be added to the global
652 	 * MIBs.
653 	 */
654 	if (ill->ill_ip_mib != NULL) {
655 		if (ill->ill_isv6) {
656 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
657 			    ill->ill_ip_mib);
658 		} else {
659 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
660 			    ill->ill_ip_mib);
661 		}
662 
663 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
664 		ill->ill_ip_mib = NULL;
665 	}
666 	if (ill->ill_icmp6_mib != NULL) {
667 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
668 		    ill->ill_icmp6_mib);
669 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
670 		ill->ill_icmp6_mib = NULL;
671 	}
672 }
673 
674 /*
675  * Concatenate together a physical address and a sap.
676  *
677  * Sap_lengths are interpreted as follows:
678  *   sap_length == 0	==>	no sap
679  *   sap_length > 0	==>	sap is at the head of the dlpi address
680  *   sap_length < 0	==>	sap is at the tail of the dlpi address
681  */
682 static void
683 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
684     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
685 {
686 	uint16_t sap_addr = (uint16_t)sap_src;
687 
688 	if (sap_length == 0) {
689 		if (phys_src == NULL)
690 			bzero(dst, phys_length);
691 		else
692 			bcopy(phys_src, dst, phys_length);
693 	} else if (sap_length < 0) {
694 		if (phys_src == NULL)
695 			bzero(dst, phys_length);
696 		else
697 			bcopy(phys_src, dst, phys_length);
698 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
699 	} else {
700 		bcopy(&sap_addr, dst, sizeof (sap_addr));
701 		if (phys_src == NULL)
702 			bzero((char *)dst + sap_length, phys_length);
703 		else
704 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
705 	}
706 }
707 
708 /*
709  * Generate a dl_unitdata_req mblk for the device and address given.
710  * addr_length is the length of the physical portion of the address.
711  * If addr is NULL include an all zero address of the specified length.
712  * TRUE? In any case, addr_length is taken to be the entire length of the
713  * dlpi address, including the absolute value of sap_length.
714  */
715 mblk_t *
716 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
717 		t_scalar_t sap_length)
718 {
719 	dl_unitdata_req_t *dlur;
720 	mblk_t	*mp;
721 	t_scalar_t	abs_sap_length;		/* absolute value */
722 
723 	abs_sap_length = ABS(sap_length);
724 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
725 	    DL_UNITDATA_REQ);
726 	if (mp == NULL)
727 		return (NULL);
728 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
729 	/* HACK: accomodate incompatible DLPI drivers */
730 	if (addr_length == 8)
731 		addr_length = 6;
732 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
733 	dlur->dl_dest_addr_offset = sizeof (*dlur);
734 	dlur->dl_priority.dl_min = 0;
735 	dlur->dl_priority.dl_max = 0;
736 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
737 	    (uchar_t *)&dlur[1]);
738 	return (mp);
739 }
740 
741 /*
742  * Add the pending mp to the list. There can be only 1 pending mp
743  * in the list. Any exclusive ioctl that needs to wait for a response
744  * from another module or driver needs to use this function to set
745  * the ipx_pending_mp to the ioctl mblk and wait for the response from
746  * the other module/driver. This is also used while waiting for the
747  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
748  */
749 boolean_t
750 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
751     int waitfor)
752 {
753 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
754 
755 	ASSERT(IAM_WRITER_IPIF(ipif));
756 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
757 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
758 	ASSERT(ipx->ipx_pending_mp == NULL);
759 	/*
760 	 * The caller may be using a different ipif than the one passed into
761 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
762 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
763 	 * that `ipx_current_ipif == ipif'.
764 	 */
765 	ASSERT(ipx->ipx_current_ipif != NULL);
766 
767 	/*
768 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
769 	 * driver.
770 	 */
771 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
772 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
773 	    (DB_TYPE(add_mp) == M_PCPROTO));
774 
775 	if (connp != NULL) {
776 		ASSERT(MUTEX_HELD(&connp->conn_lock));
777 		/*
778 		 * Return error if the conn has started closing. The conn
779 		 * could have finished cleaning up the pending mp list,
780 		 * If so we should not add another mp to the list negating
781 		 * the cleanup.
782 		 */
783 		if (connp->conn_state_flags & CONN_CLOSING)
784 			return (B_FALSE);
785 	}
786 	mutex_enter(&ipx->ipx_lock);
787 	ipx->ipx_pending_ipif = ipif;
788 	/*
789 	 * Note down the queue in b_queue. This will be returned by
790 	 * ipsq_pending_mp_get. Caller will then use these values to restart
791 	 * the processing
792 	 */
793 	add_mp->b_next = NULL;
794 	add_mp->b_queue = q;
795 	ipx->ipx_pending_mp = add_mp;
796 	ipx->ipx_waitfor = waitfor;
797 	mutex_exit(&ipx->ipx_lock);
798 
799 	if (connp != NULL)
800 		connp->conn_oper_pending_ill = ipif->ipif_ill;
801 
802 	return (B_TRUE);
803 }
804 
805 /*
806  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
807  * queued in the list.
808  */
809 mblk_t *
810 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
811 {
812 	mblk_t	*curr = NULL;
813 	ipxop_t	*ipx = ipsq->ipsq_xop;
814 
815 	*connpp = NULL;
816 	mutex_enter(&ipx->ipx_lock);
817 	if (ipx->ipx_pending_mp == NULL) {
818 		mutex_exit(&ipx->ipx_lock);
819 		return (NULL);
820 	}
821 
822 	/* There can be only 1 such excl message */
823 	curr = ipx->ipx_pending_mp;
824 	ASSERT(curr->b_next == NULL);
825 	ipx->ipx_pending_ipif = NULL;
826 	ipx->ipx_pending_mp = NULL;
827 	ipx->ipx_waitfor = 0;
828 	mutex_exit(&ipx->ipx_lock);
829 
830 	if (CONN_Q(curr->b_queue)) {
831 		/*
832 		 * This mp did a refhold on the conn, at the start of the ioctl.
833 		 * So we can safely return a pointer to the conn to the caller.
834 		 */
835 		*connpp = Q_TO_CONN(curr->b_queue);
836 	} else {
837 		*connpp = NULL;
838 	}
839 	curr->b_next = NULL;
840 	curr->b_prev = NULL;
841 	return (curr);
842 }
843 
844 /*
845  * Cleanup the ioctl mp queued in ipx_pending_mp
846  * - Called in the ill_delete path
847  * - Called in the M_ERROR or M_HANGUP path on the ill.
848  * - Called in the conn close path.
849  *
850  * Returns success on finding the pending mblk associated with the ioctl or
851  * exclusive operation in progress, failure otherwise.
852  */
853 boolean_t
854 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
855 {
856 	mblk_t	*mp;
857 	ipxop_t	*ipx;
858 	queue_t	*q;
859 	ipif_t	*ipif;
860 	int	cmd;
861 
862 	ASSERT(IAM_WRITER_ILL(ill));
863 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
864 
865 	mutex_enter(&ipx->ipx_lock);
866 	mp = ipx->ipx_pending_mp;
867 	if (connp != NULL) {
868 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
869 			/*
870 			 * Nothing to clean since the conn that is closing
871 			 * does not have a matching pending mblk in
872 			 * ipx_pending_mp.
873 			 */
874 			mutex_exit(&ipx->ipx_lock);
875 			return (B_FALSE);
876 		}
877 	} else {
878 		/*
879 		 * A non-zero ill_error signifies we are called in the
880 		 * M_ERROR or M_HANGUP path and we need to unconditionally
881 		 * abort any current ioctl and do the corresponding cleanup.
882 		 * A zero ill_error means we are in the ill_delete path and
883 		 * we do the cleanup only if there is a pending mp.
884 		 */
885 		if (mp == NULL && ill->ill_error == 0) {
886 			mutex_exit(&ipx->ipx_lock);
887 			return (B_FALSE);
888 		}
889 	}
890 
891 	/* Now remove from the ipx_pending_mp */
892 	ipx->ipx_pending_mp = NULL;
893 	ipif = ipx->ipx_pending_ipif;
894 	ipx->ipx_pending_ipif = NULL;
895 	ipx->ipx_waitfor = 0;
896 	ipx->ipx_current_ipif = NULL;
897 	cmd = ipx->ipx_current_ioctl;
898 	ipx->ipx_current_ioctl = 0;
899 	ipx->ipx_current_done = B_TRUE;
900 	mutex_exit(&ipx->ipx_lock);
901 
902 	if (mp == NULL)
903 		return (B_FALSE);
904 
905 	q = mp->b_queue;
906 	mp->b_next = NULL;
907 	mp->b_prev = NULL;
908 	mp->b_queue = NULL;
909 
910 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
911 		DTRACE_PROBE4(ipif__ioctl,
912 		    char *, "ipsq_pending_mp_cleanup",
913 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
914 		    ipif_t *, ipif);
915 		if (connp == NULL) {
916 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
917 		} else {
918 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
919 			mutex_enter(&ipif->ipif_ill->ill_lock);
920 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
921 			mutex_exit(&ipif->ipif_ill->ill_lock);
922 		}
923 	} else {
924 		inet_freemsg(mp);
925 	}
926 	return (B_TRUE);
927 }
928 
929 /*
930  * Called in the conn close path and ill delete path
931  */
932 static void
933 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
934 {
935 	ipsq_t	*ipsq;
936 	mblk_t	*prev;
937 	mblk_t	*curr;
938 	mblk_t	*next;
939 	queue_t	*wq, *rq = NULL;
940 	mblk_t	*tmp_list = NULL;
941 
942 	ASSERT(IAM_WRITER_ILL(ill));
943 	if (connp != NULL)
944 		wq = CONNP_TO_WQ(connp);
945 	else
946 		wq = ill->ill_wq;
947 
948 	/*
949 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
950 	 * against this here.
951 	 */
952 	if (wq != NULL)
953 		rq = RD(wq);
954 
955 	ipsq = ill->ill_phyint->phyint_ipsq;
956 	/*
957 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
958 	 * In the case of ioctl from a conn, there can be only 1 mp
959 	 * queued on the ipsq. If an ill is being unplumbed flush all
960 	 * the messages.
961 	 */
962 	mutex_enter(&ipsq->ipsq_lock);
963 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
964 	    curr = next) {
965 		next = curr->b_next;
966 		if (connp == NULL ||
967 		    (curr->b_queue == wq || curr->b_queue == rq)) {
968 			/* Unlink the mblk from the pending mp list */
969 			if (prev != NULL) {
970 				prev->b_next = curr->b_next;
971 			} else {
972 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
973 				ipsq->ipsq_xopq_mphead = curr->b_next;
974 			}
975 			if (ipsq->ipsq_xopq_mptail == curr)
976 				ipsq->ipsq_xopq_mptail = prev;
977 			/*
978 			 * Create a temporary list and release the ipsq lock
979 			 * New elements are added to the head of the tmp_list
980 			 */
981 			curr->b_next = tmp_list;
982 			tmp_list = curr;
983 		} else {
984 			prev = curr;
985 		}
986 	}
987 	mutex_exit(&ipsq->ipsq_lock);
988 
989 	while (tmp_list != NULL) {
990 		curr = tmp_list;
991 		tmp_list = curr->b_next;
992 		curr->b_next = NULL;
993 		curr->b_prev = NULL;
994 		wq = curr->b_queue;
995 		curr->b_queue = NULL;
996 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
997 			DTRACE_PROBE4(ipif__ioctl,
998 			    char *, "ipsq_xopq_mp_cleanup",
999 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
1000 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
1001 			    CONN_CLOSE : NO_COPYOUT, NULL);
1002 		} else {
1003 			/*
1004 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1005 			 * this can't be just inet_freemsg. we have to
1006 			 * restart it otherwise the thread will be stuck.
1007 			 */
1008 			inet_freemsg(curr);
1009 		}
1010 	}
1011 }
1012 
1013 /*
1014  * This conn has started closing. Cleanup any pending ioctl from this conn.
1015  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
1016  */
1017 void
1018 conn_ioctl_cleanup(conn_t *connp)
1019 {
1020 	ipsq_t	*ipsq;
1021 	ill_t	*ill;
1022 	boolean_t refheld;
1023 
1024 	/*
1025 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1026 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1027 	 * started the mp could be present in ipx_pending_mp. Note that if
1028 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1029 	 * not yet queued anywhere. In this case, the conn close code will wait
1030 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1031 	 * tcp_close will wait first until all ioctls have completed for this
1032 	 * conn.
1033 	 */
1034 	mutex_enter(&connp->conn_lock);
1035 	ill = connp->conn_oper_pending_ill;
1036 	if (ill == NULL) {
1037 		mutex_exit(&connp->conn_lock);
1038 		return;
1039 	}
1040 
1041 	/*
1042 	 * We may not be able to refhold the ill if the ill/ipif
1043 	 * is changing. But we need to make sure that the ill will
1044 	 * not vanish. So we just bump up the ill_waiter count.
1045 	 */
1046 	refheld = ill_waiter_inc(ill);
1047 	mutex_exit(&connp->conn_lock);
1048 	if (refheld) {
1049 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1050 			ill_waiter_dcr(ill);
1051 			/*
1052 			 * Check whether this ioctl has started and is
1053 			 * pending. If it is not found there then check
1054 			 * whether this ioctl has not even started and is in
1055 			 * the ipsq_xopq list.
1056 			 */
1057 			if (!ipsq_pending_mp_cleanup(ill, connp))
1058 				ipsq_xopq_mp_cleanup(ill, connp);
1059 			ipsq = ill->ill_phyint->phyint_ipsq;
1060 			ipsq_exit(ipsq);
1061 			return;
1062 		}
1063 	}
1064 
1065 	/*
1066 	 * The ill is also closing and we could not bump up the
1067 	 * ill_waiter_count or we could not enter the ipsq. Leave
1068 	 * the cleanup to ill_delete
1069 	 */
1070 	mutex_enter(&connp->conn_lock);
1071 	while (connp->conn_oper_pending_ill != NULL)
1072 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1073 	mutex_exit(&connp->conn_lock);
1074 	if (refheld)
1075 		ill_waiter_dcr(ill);
1076 }
1077 
1078 /*
1079  * ipcl_walk function for cleaning up conn_*_ill fields.
1080  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1081  * conn_bound_if in place. We prefer dropping
1082  * packets instead of sending them out the wrong interface, or accepting
1083  * packets from the wrong ifindex.
1084  */
1085 static void
1086 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1087 {
1088 	ill_t	*ill = (ill_t *)arg;
1089 
1090 	mutex_enter(&connp->conn_lock);
1091 	if (connp->conn_dhcpinit_ill == ill) {
1092 		connp->conn_dhcpinit_ill = NULL;
1093 		ASSERT(ill->ill_dhcpinit != 0);
1094 		atomic_dec_32(&ill->ill_dhcpinit);
1095 		ill_set_inputfn(ill);
1096 	}
1097 	mutex_exit(&connp->conn_lock);
1098 }
1099 
1100 static int
1101 ill_down_ipifs_tail(ill_t *ill)
1102 {
1103 	ipif_t	*ipif;
1104 	int err;
1105 
1106 	ASSERT(IAM_WRITER_ILL(ill));
1107 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1108 		ipif_non_duplicate(ipif);
1109 		/*
1110 		 * ipif_down_tail will call arp_ll_down on the last ipif
1111 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1112 		 */
1113 		if ((err = ipif_down_tail(ipif)) != 0)
1114 			return (err);
1115 	}
1116 	return (0);
1117 }
1118 
1119 /* ARGSUSED */
1120 void
1121 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1122 {
1123 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1124 	(void) ill_down_ipifs_tail(q->q_ptr);
1125 	freemsg(mp);
1126 	ipsq_current_finish(ipsq);
1127 }
1128 
1129 /*
1130  * ill_down_start is called when we want to down this ill and bring it up again
1131  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1132  * all interfaces, but don't tear down any plumbing.
1133  */
1134 boolean_t
1135 ill_down_start(queue_t *q, mblk_t *mp)
1136 {
1137 	ill_t	*ill = q->q_ptr;
1138 	ipif_t	*ipif;
1139 
1140 	ASSERT(IAM_WRITER_ILL(ill));
1141 	/*
1142 	 * It is possible that some ioctl is already in progress while we
1143 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1144 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1145 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1146 	 * the in progress ioctl from ever completing.
1147 	 *
1148 	 * The thread that started the ioctl (if any) must have returned,
1149 	 * since we are now executing as writer. After the 2 calls below,
1150 	 * the state of the ipsq and the ill would reflect no trace of any
1151 	 * pending operation. Subsequently if there is any response to the
1152 	 * original ioctl from the driver, it would be discarded as an
1153 	 * unsolicited message from the driver.
1154 	 */
1155 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1156 	ill_dlpi_clear_deferred(ill);
1157 
1158 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1159 		(void) ipif_down(ipif, NULL, NULL);
1160 
1161 	ill_down(ill);
1162 
1163 	/*
1164 	 * Walk all CONNs that can have a reference on an ire or nce for this
1165 	 * ill (we actually walk all that now have stale references).
1166 	 */
1167 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1168 
1169 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1170 	if (ill->ill_isv6)
1171 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1172 
1173 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1174 
1175 	/*
1176 	 * Atomically test and add the pending mp if references are active.
1177 	 */
1178 	mutex_enter(&ill->ill_lock);
1179 	if (!ill_is_quiescent(ill)) {
1180 		/* call cannot fail since `conn_t *' argument is NULL */
1181 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1182 		    mp, ILL_DOWN);
1183 		mutex_exit(&ill->ill_lock);
1184 		return (B_FALSE);
1185 	}
1186 	mutex_exit(&ill->ill_lock);
1187 	return (B_TRUE);
1188 }
1189 
1190 static void
1191 ill_down(ill_t *ill)
1192 {
1193 	mblk_t	*mp;
1194 	ip_stack_t	*ipst = ill->ill_ipst;
1195 
1196 	/*
1197 	 * Blow off any IREs dependent on this ILL.
1198 	 * The caller needs to handle conn_ixa_cleanup
1199 	 */
1200 	ill_delete_ires(ill);
1201 
1202 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1203 
1204 	/* Remove any conn_*_ill depending on this ill */
1205 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1206 
1207 	/*
1208 	 * Free state for additional IREs.
1209 	 */
1210 	mutex_enter(&ill->ill_saved_ire_lock);
1211 	mp = ill->ill_saved_ire_mp;
1212 	ill->ill_saved_ire_mp = NULL;
1213 	ill->ill_saved_ire_cnt = 0;
1214 	mutex_exit(&ill->ill_saved_ire_lock);
1215 	freemsg(mp);
1216 }
1217 
1218 /*
1219  * ire_walk routine used to delete every IRE that depends on
1220  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1221  *
1222  * Note: since the routes added by the kernel are deleted separately,
1223  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1224  *
1225  * We also remove references on ire_nce_cache entries that refer to the ill.
1226  */
1227 void
1228 ill_downi(ire_t *ire, char *ill_arg)
1229 {
1230 	ill_t	*ill = (ill_t *)ill_arg;
1231 	nce_t	*nce;
1232 
1233 	mutex_enter(&ire->ire_lock);
1234 	nce = ire->ire_nce_cache;
1235 	if (nce != NULL && nce->nce_ill == ill)
1236 		ire->ire_nce_cache = NULL;
1237 	else
1238 		nce = NULL;
1239 	mutex_exit(&ire->ire_lock);
1240 	if (nce != NULL)
1241 		nce_refrele(nce);
1242 	if (ire->ire_ill == ill) {
1243 		/*
1244 		 * The existing interface binding for ire must be
1245 		 * deleted before trying to bind the route to another
1246 		 * interface. However, since we are using the contents of the
1247 		 * ire after ire_delete, the caller has to ensure that
1248 		 * CONDEMNED (deleted) ire's are not removed from the list
1249 		 * when ire_delete() returns. Currently ill_downi() is
1250 		 * only called as part of ire_walk*() routines, so that
1251 		 * the irb_refhold() done by ire_walk*() will ensure that
1252 		 * ire_delete() does not lead to ire_inactive().
1253 		 */
1254 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1255 		ire_delete(ire);
1256 		if (ire->ire_unbound)
1257 			ire_rebind(ire);
1258 	}
1259 }
1260 
1261 /* Remove IRE_IF_CLONE on this ill */
1262 void
1263 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1264 {
1265 	ill_t	*ill = (ill_t *)ill_arg;
1266 
1267 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1268 	if (ire->ire_ill == ill)
1269 		ire_delete(ire);
1270 }
1271 
1272 /* Consume an M_IOCACK of the fastpath probe. */
1273 void
1274 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1275 {
1276 	mblk_t	*mp1 = mp;
1277 
1278 	/*
1279 	 * If this was the first attempt turn on the fastpath probing.
1280 	 */
1281 	mutex_enter(&ill->ill_lock);
1282 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1283 		ill->ill_dlpi_fastpath_state = IDS_OK;
1284 	mutex_exit(&ill->ill_lock);
1285 
1286 	/* Free the M_IOCACK mblk, hold on to the data */
1287 	mp = mp->b_cont;
1288 	freeb(mp1);
1289 	if (mp == NULL)
1290 		return;
1291 	if (mp->b_cont != NULL)
1292 		nce_fastpath_update(ill, mp);
1293 	else
1294 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1295 	freemsg(mp);
1296 }
1297 
1298 /*
1299  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1300  * The data portion of the request is a dl_unitdata_req_t template for
1301  * what we would send downstream in the absence of a fastpath confirmation.
1302  */
1303 int
1304 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1305 {
1306 	struct iocblk	*ioc;
1307 	mblk_t	*mp;
1308 
1309 	if (dlur_mp == NULL)
1310 		return (EINVAL);
1311 
1312 	mutex_enter(&ill->ill_lock);
1313 	switch (ill->ill_dlpi_fastpath_state) {
1314 	case IDS_FAILED:
1315 		/*
1316 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1317 		 * support it.
1318 		 */
1319 		mutex_exit(&ill->ill_lock);
1320 		return (ENOTSUP);
1321 	case IDS_UNKNOWN:
1322 		/* This is the first probe */
1323 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1324 		break;
1325 	default:
1326 		break;
1327 	}
1328 	mutex_exit(&ill->ill_lock);
1329 
1330 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1331 		return (EAGAIN);
1332 
1333 	mp->b_cont = copyb(dlur_mp);
1334 	if (mp->b_cont == NULL) {
1335 		freeb(mp);
1336 		return (EAGAIN);
1337 	}
1338 
1339 	ioc = (struct iocblk *)mp->b_rptr;
1340 	ioc->ioc_count = msgdsize(mp->b_cont);
1341 
1342 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1343 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1344 	putnext(ill->ill_wq, mp);
1345 	return (0);
1346 }
1347 
1348 void
1349 ill_capability_probe(ill_t *ill)
1350 {
1351 	mblk_t	*mp;
1352 
1353 	ASSERT(IAM_WRITER_ILL(ill));
1354 
1355 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1356 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1357 		return;
1358 
1359 	/*
1360 	 * We are starting a new cycle of capability negotiation.
1361 	 * Free up the capab reset messages of any previous incarnation.
1362 	 * We will do a fresh allocation when we get the response to our probe
1363 	 */
1364 	if (ill->ill_capab_reset_mp != NULL) {
1365 		freemsg(ill->ill_capab_reset_mp);
1366 		ill->ill_capab_reset_mp = NULL;
1367 	}
1368 
1369 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1370 
1371 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1372 	if (mp == NULL)
1373 		return;
1374 
1375 	ill_capability_send(ill, mp);
1376 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1377 }
1378 
1379 void
1380 ill_capability_reset(ill_t *ill, boolean_t reneg)
1381 {
1382 	ASSERT(IAM_WRITER_ILL(ill));
1383 
1384 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1385 		return;
1386 
1387 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1388 
1389 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1390 	ill->ill_capab_reset_mp = NULL;
1391 	/*
1392 	 * We turn off all capabilities except those pertaining to
1393 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1394 	 * which will be turned off by the corresponding reset functions.
1395 	 */
1396 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1397 }
1398 
1399 static void
1400 ill_capability_reset_alloc(ill_t *ill)
1401 {
1402 	mblk_t *mp;
1403 	size_t	size = 0;
1404 	int	err;
1405 	dl_capability_req_t	*capb;
1406 
1407 	ASSERT(IAM_WRITER_ILL(ill));
1408 	ASSERT(ill->ill_capab_reset_mp == NULL);
1409 
1410 	if (ILL_HCKSUM_CAPABLE(ill)) {
1411 		size += sizeof (dl_capability_sub_t) +
1412 		    sizeof (dl_capab_hcksum_t);
1413 	}
1414 
1415 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1416 		size += sizeof (dl_capability_sub_t) +
1417 		    sizeof (dl_capab_zerocopy_t);
1418 	}
1419 
1420 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1421 		size += sizeof (dl_capability_sub_t) +
1422 		    sizeof (dl_capab_dld_t);
1423 	}
1424 
1425 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1426 	    STR_NOSIG, &err);
1427 
1428 	mp->b_datap->db_type = M_PROTO;
1429 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1430 
1431 	capb = (dl_capability_req_t *)mp->b_rptr;
1432 	capb->dl_primitive = DL_CAPABILITY_REQ;
1433 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1434 	capb->dl_sub_length = size;
1435 
1436 	mp->b_wptr += sizeof (dl_capability_req_t);
1437 
1438 	/*
1439 	 * Each handler fills in the corresponding dl_capability_sub_t
1440 	 * inside the mblk,
1441 	 */
1442 	ill_capability_hcksum_reset_fill(ill, mp);
1443 	ill_capability_zerocopy_reset_fill(ill, mp);
1444 	ill_capability_dld_reset_fill(ill, mp);
1445 
1446 	ill->ill_capab_reset_mp = mp;
1447 }
1448 
1449 static void
1450 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1451 {
1452 	dl_capab_id_t *id_ic;
1453 	uint_t sub_dl_cap = outers->dl_cap;
1454 	dl_capability_sub_t *inners;
1455 	uint8_t *capend;
1456 
1457 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1458 
1459 	/*
1460 	 * Note: range checks here are not absolutely sufficient to
1461 	 * make us robust against malformed messages sent by drivers;
1462 	 * this is in keeping with the rest of IP's dlpi handling.
1463 	 * (Remember, it's coming from something else in the kernel
1464 	 * address space)
1465 	 */
1466 
1467 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1468 	if (capend > mp->b_wptr) {
1469 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1470 		    "malformed sub-capability too long for mblk");
1471 		return;
1472 	}
1473 
1474 	id_ic = (dl_capab_id_t *)(outers + 1);
1475 
1476 	if (outers->dl_length < sizeof (*id_ic) ||
1477 	    (inners = &id_ic->id_subcap,
1478 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1479 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1480 		    "encapsulated capab type %d too long for mblk",
1481 		    inners->dl_cap);
1482 		return;
1483 	}
1484 
1485 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1486 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1487 		    "isn't as expected; pass-thru module(s) detected, "
1488 		    "discarding capability\n", inners->dl_cap));
1489 		return;
1490 	}
1491 
1492 	/* Process the encapsulated sub-capability */
1493 	ill_capability_dispatch(ill, mp, inners);
1494 }
1495 
1496 static void
1497 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1498 {
1499 	dl_capability_sub_t *dl_subcap;
1500 
1501 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1502 		return;
1503 
1504 	/*
1505 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1506 	 * initialized below since it is not used by DLD.
1507 	 */
1508 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1509 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1510 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1511 
1512 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1513 }
1514 
1515 static void
1516 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1517 {
1518 	/*
1519 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1520 	 * is only to get the VRRP capability.
1521 	 *
1522 	 * Note that we cannot check ill_ipif_up_count here since
1523 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1524 	 * That is done asynchronously, and can race with this function.
1525 	 */
1526 	if (!ill->ill_dl_up) {
1527 		if (subp->dl_cap == DL_CAPAB_VRRP)
1528 			ill_capability_vrrp_ack(ill, mp, subp);
1529 		return;
1530 	}
1531 
1532 	switch (subp->dl_cap) {
1533 	case DL_CAPAB_HCKSUM:
1534 		ill_capability_hcksum_ack(ill, mp, subp);
1535 		break;
1536 	case DL_CAPAB_ZEROCOPY:
1537 		ill_capability_zerocopy_ack(ill, mp, subp);
1538 		break;
1539 	case DL_CAPAB_DLD:
1540 		ill_capability_dld_ack(ill, mp, subp);
1541 		break;
1542 	case DL_CAPAB_VRRP:
1543 		break;
1544 	default:
1545 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1546 		    subp->dl_cap));
1547 	}
1548 }
1549 
1550 /*
1551  * Process the vrrp capability received from a DLS Provider. isub must point
1552  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1553  */
1554 static void
1555 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1556 {
1557 	dl_capab_vrrp_t	*vrrp;
1558 	uint_t		sub_dl_cap = isub->dl_cap;
1559 	uint8_t		*capend;
1560 
1561 	ASSERT(IAM_WRITER_ILL(ill));
1562 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1563 
1564 	/*
1565 	 * Note: range checks here are not absolutely sufficient to
1566 	 * make us robust against malformed messages sent by drivers;
1567 	 * this is in keeping with the rest of IP's dlpi handling.
1568 	 * (Remember, it's coming from something else in the kernel
1569 	 * address space)
1570 	 */
1571 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1572 	if (capend > mp->b_wptr) {
1573 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1574 		    "malformed sub-capability too long for mblk");
1575 		return;
1576 	}
1577 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1578 
1579 	/*
1580 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1581 	 */
1582 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1583 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1584 		ill->ill_flags |= ILLF_VRRP;
1585 	}
1586 }
1587 
1588 /*
1589  * Process a hardware checksum offload capability negotiation ack received
1590  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1591  * of a DL_CAPABILITY_ACK message.
1592  */
1593 static void
1594 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1595 {
1596 	dl_capability_req_t	*ocap;
1597 	dl_capab_hcksum_t	*ihck, *ohck;
1598 	ill_hcksum_capab_t	**ill_hcksum;
1599 	mblk_t			*nmp = NULL;
1600 	uint_t			sub_dl_cap = isub->dl_cap;
1601 	uint8_t			*capend;
1602 
1603 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1604 
1605 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1606 
1607 	/*
1608 	 * Note: range checks here are not absolutely sufficient to
1609 	 * make us robust against malformed messages sent by drivers;
1610 	 * this is in keeping with the rest of IP's dlpi handling.
1611 	 * (Remember, it's coming from something else in the kernel
1612 	 * address space)
1613 	 */
1614 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1615 	if (capend > mp->b_wptr) {
1616 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1617 		    "malformed sub-capability too long for mblk");
1618 		return;
1619 	}
1620 
1621 	/*
1622 	 * There are two types of acks we process here:
1623 	 * 1. acks in reply to a (first form) generic capability req
1624 	 *    (no ENABLE flag set)
1625 	 * 2. acks in reply to a ENABLE capability req.
1626 	 *    (ENABLE flag set)
1627 	 */
1628 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1629 
1630 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1631 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1632 		    "unsupported hardware checksum "
1633 		    "sub-capability (version %d, expected %d)",
1634 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1635 		return;
1636 	}
1637 
1638 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1639 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1640 		    "checksum capability isn't as expected; pass-thru "
1641 		    "module(s) detected, discarding capability\n"));
1642 		return;
1643 	}
1644 
1645 #define	CURR_HCKSUM_CAPAB				\
1646 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1647 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1648 
1649 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1650 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1651 		/* do ENABLE processing */
1652 		if (*ill_hcksum == NULL) {
1653 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1654 			    KM_NOSLEEP);
1655 
1656 			if (*ill_hcksum == NULL) {
1657 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1658 				    "could not enable hcksum version %d "
1659 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1660 				    ill->ill_name);
1661 				return;
1662 			}
1663 		}
1664 
1665 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1666 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1667 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1668 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1669 		    "has enabled hardware checksumming\n ",
1670 		    ill->ill_name));
1671 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1672 		/*
1673 		 * Enabling hardware checksum offload
1674 		 * Currently IP supports {TCP,UDP}/IPv4
1675 		 * partial and full cksum offload and
1676 		 * IPv4 header checksum offload.
1677 		 * Allocate new mblk which will
1678 		 * contain a new capability request
1679 		 * to enable hardware checksum offload.
1680 		 */
1681 		uint_t	size;
1682 		uchar_t	*rptr;
1683 
1684 		size = sizeof (dl_capability_req_t) +
1685 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1686 
1687 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1688 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1689 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1690 			    ill->ill_name);
1691 			return;
1692 		}
1693 
1694 		rptr = nmp->b_rptr;
1695 		/* initialize dl_capability_req_t */
1696 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1697 		ocap->dl_sub_offset =
1698 		    sizeof (dl_capability_req_t);
1699 		ocap->dl_sub_length =
1700 		    sizeof (dl_capability_sub_t) +
1701 		    isub->dl_length;
1702 		nmp->b_rptr += sizeof (dl_capability_req_t);
1703 
1704 		/* initialize dl_capability_sub_t */
1705 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1706 		nmp->b_rptr += sizeof (*isub);
1707 
1708 		/* initialize dl_capab_hcksum_t */
1709 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1710 		bcopy(ihck, ohck, sizeof (*ihck));
1711 
1712 		nmp->b_rptr = rptr;
1713 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1714 
1715 		/* Set ENABLE flag */
1716 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1717 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1718 
1719 		/*
1720 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1721 		 * hardware checksum acceleration.
1722 		 */
1723 		ill_capability_send(ill, nmp);
1724 	} else {
1725 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1726 		    "advertised %x hardware checksum capability flags\n",
1727 		    ill->ill_name, ihck->hcksum_txflags));
1728 	}
1729 }
1730 
1731 static void
1732 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1733 {
1734 	dl_capab_hcksum_t *hck_subcap;
1735 	dl_capability_sub_t *dl_subcap;
1736 
1737 	if (!ILL_HCKSUM_CAPABLE(ill))
1738 		return;
1739 
1740 	ASSERT(ill->ill_hcksum_capab != NULL);
1741 
1742 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1743 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1744 	dl_subcap->dl_length = sizeof (*hck_subcap);
1745 
1746 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1747 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1748 	hck_subcap->hcksum_txflags = 0;
1749 
1750 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1751 }
1752 
1753 static void
1754 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1755 {
1756 	mblk_t *nmp = NULL;
1757 	dl_capability_req_t *oc;
1758 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1759 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1760 	uint_t sub_dl_cap = isub->dl_cap;
1761 	uint8_t *capend;
1762 
1763 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1764 
1765 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1766 
1767 	/*
1768 	 * Note: range checks here are not absolutely sufficient to
1769 	 * make us robust against malformed messages sent by drivers;
1770 	 * this is in keeping with the rest of IP's dlpi handling.
1771 	 * (Remember, it's coming from something else in the kernel
1772 	 * address space)
1773 	 */
1774 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1775 	if (capend > mp->b_wptr) {
1776 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1777 		    "malformed sub-capability too long for mblk");
1778 		return;
1779 	}
1780 
1781 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1782 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1783 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1784 		    "unsupported ZEROCOPY sub-capability (version %d, "
1785 		    "expected %d)", zc_ic->zerocopy_version,
1786 		    ZEROCOPY_VERSION_1);
1787 		return;
1788 	}
1789 
1790 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1791 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1792 		    "capability isn't as expected; pass-thru module(s) "
1793 		    "detected, discarding capability\n"));
1794 		return;
1795 	}
1796 
1797 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1798 		if (*ill_zerocopy_capab == NULL) {
1799 			*ill_zerocopy_capab =
1800 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1801 			    KM_NOSLEEP);
1802 
1803 			if (*ill_zerocopy_capab == NULL) {
1804 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1805 				    "could not enable Zero-copy version %d "
1806 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1807 				    ill->ill_name);
1808 				return;
1809 			}
1810 		}
1811 
1812 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1813 		    "supports Zero-copy version %d\n", ill->ill_name,
1814 		    ZEROCOPY_VERSION_1));
1815 
1816 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1817 		    zc_ic->zerocopy_version;
1818 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1819 		    zc_ic->zerocopy_flags;
1820 
1821 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1822 	} else {
1823 		uint_t size;
1824 		uchar_t *rptr;
1825 
1826 		size = sizeof (dl_capability_req_t) +
1827 		    sizeof (dl_capability_sub_t) +
1828 		    sizeof (dl_capab_zerocopy_t);
1829 
1830 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1831 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1832 			    "could not enable zerocopy for %s (ENOMEM)\n",
1833 			    ill->ill_name);
1834 			return;
1835 		}
1836 
1837 		rptr = nmp->b_rptr;
1838 		/* initialize dl_capability_req_t */
1839 		oc = (dl_capability_req_t *)rptr;
1840 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1841 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1842 		    sizeof (dl_capab_zerocopy_t);
1843 		rptr += sizeof (dl_capability_req_t);
1844 
1845 		/* initialize dl_capability_sub_t */
1846 		bcopy(isub, rptr, sizeof (*isub));
1847 		rptr += sizeof (*isub);
1848 
1849 		/* initialize dl_capab_zerocopy_t */
1850 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1851 		*zc_oc = *zc_ic;
1852 
1853 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1854 		    "to enable zero-copy version %d\n", ill->ill_name,
1855 		    ZEROCOPY_VERSION_1));
1856 
1857 		/* set VMSAFE_MEM flag */
1858 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1859 
1860 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1861 		ill_capability_send(ill, nmp);
1862 	}
1863 }
1864 
1865 static void
1866 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1867 {
1868 	dl_capab_zerocopy_t *zerocopy_subcap;
1869 	dl_capability_sub_t *dl_subcap;
1870 
1871 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1872 		return;
1873 
1874 	ASSERT(ill->ill_zerocopy_capab != NULL);
1875 
1876 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1877 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1878 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1879 
1880 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1881 	zerocopy_subcap->zerocopy_version =
1882 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1883 	zerocopy_subcap->zerocopy_flags = 0;
1884 
1885 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1886 }
1887 
1888 /*
1889  * DLD capability
1890  * Refer to dld.h for more information regarding the purpose and usage
1891  * of this capability.
1892  */
1893 static void
1894 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	dl_capab_dld_t		*dld_ic, dld;
1897 	uint_t			sub_dl_cap = isub->dl_cap;
1898 	uint8_t			*capend;
1899 	ill_dld_capab_t		*idc;
1900 
1901 	ASSERT(IAM_WRITER_ILL(ill));
1902 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1903 
1904 	/*
1905 	 * Note: range checks here are not absolutely sufficient to
1906 	 * make us robust against malformed messages sent by drivers;
1907 	 * this is in keeping with the rest of IP's dlpi handling.
1908 	 * (Remember, it's coming from something else in the kernel
1909 	 * address space)
1910 	 */
1911 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1912 	if (capend > mp->b_wptr) {
1913 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1914 		    "malformed sub-capability too long for mblk");
1915 		return;
1916 	}
1917 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1918 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1919 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1920 		    "unsupported DLD sub-capability (version %d, "
1921 		    "expected %d)", dld_ic->dld_version,
1922 		    DLD_CURRENT_VERSION);
1923 		return;
1924 	}
1925 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1926 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1927 		    "capability isn't as expected; pass-thru module(s) "
1928 		    "detected, discarding capability\n"));
1929 		return;
1930 	}
1931 
1932 	/*
1933 	 * Copy locally to ensure alignment.
1934 	 */
1935 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1936 
1937 	if ((idc = ill->ill_dld_capab) == NULL) {
1938 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1939 		if (idc == NULL) {
1940 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1941 			    "could not enable DLD version %d "
1942 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1943 			    ill->ill_name);
1944 			return;
1945 		}
1946 		ill->ill_dld_capab = idc;
1947 	}
1948 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1949 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1950 	ip1dbg(("ill_capability_dld_ack: interface %s "
1951 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1952 
1953 	ill_capability_dld_enable(ill);
1954 }
1955 
1956 /*
1957  * Typically capability negotiation between IP and the driver happens via
1958  * DLPI message exchange. However GLD also offers a direct function call
1959  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1960  * But arbitrary function calls into IP or GLD are not permitted, since both
1961  * of them are protected by their own perimeter mechanism. The perimeter can
1962  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1963  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1964  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1965  * to enter the mac perimeter and then do the direct function calls into
1966  * GLD to enable squeue polling. The ring related callbacks from the mac into
1967  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1968  * protected by the mac perimeter.
1969  */
1970 static void
1971 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1972 {
1973 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1974 	int			err;
1975 
1976 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1977 	    DLD_ENABLE);
1978 	ASSERT(err == 0);
1979 }
1980 
1981 static void
1982 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1983 {
1984 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1985 	int			err;
1986 
1987 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1988 	    DLD_DISABLE);
1989 	ASSERT(err == 0);
1990 }
1991 
1992 boolean_t
1993 ill_mac_perim_held(ill_t *ill)
1994 {
1995 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1996 
1997 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1998 	    DLD_QUERY));
1999 }
2000 
2001 static void
2002 ill_capability_direct_enable(ill_t *ill)
2003 {
2004 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2005 	ill_dld_direct_t	*idd = &idc->idc_direct;
2006 	dld_capab_direct_t	direct;
2007 	int			rc;
2008 
2009 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2010 
2011 	bzero(&direct, sizeof (direct));
2012 	direct.di_rx_cf = (uintptr_t)ip_input;
2013 	direct.di_rx_ch = ill;
2014 
2015 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2016 	    DLD_ENABLE);
2017 	if (rc == 0) {
2018 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2019 		idd->idd_tx_dh = direct.di_tx_dh;
2020 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2021 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2022 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2023 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2024 		ASSERT(idd->idd_tx_cb_df != NULL);
2025 		ASSERT(idd->idd_tx_fctl_df != NULL);
2026 		ASSERT(idd->idd_tx_df != NULL);
2027 		/*
2028 		 * One time registration of flow enable callback function
2029 		 */
2030 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2031 		    ill_flow_enable, ill);
2032 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2033 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2034 	} else {
2035 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2036 		    "capability, rc = %d\n", rc);
2037 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2038 	}
2039 }
2040 
2041 static void
2042 ill_capability_poll_enable(ill_t *ill)
2043 {
2044 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2045 	dld_capab_poll_t	poll;
2046 	int			rc;
2047 
2048 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2049 
2050 	bzero(&poll, sizeof (poll));
2051 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2052 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2053 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2054 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2055 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2056 	poll.poll_ring_ch = ill;
2057 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2058 	    DLD_ENABLE);
2059 	if (rc == 0) {
2060 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2061 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2062 	} else {
2063 		ip1dbg(("warning: could not enable POLL "
2064 		    "capability, rc = %d\n", rc));
2065 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2066 	}
2067 }
2068 
2069 /*
2070  * Enable the LSO capability.
2071  */
2072 static void
2073 ill_capability_lso_enable(ill_t *ill)
2074 {
2075 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2076 	dld_capab_lso_t	lso;
2077 	int rc;
2078 
2079 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2080 
2081 	if (ill->ill_lso_capab == NULL) {
2082 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2083 		    KM_NOSLEEP);
2084 		if (ill->ill_lso_capab == NULL) {
2085 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2086 			    "could not enable LSO for %s (ENOMEM)\n",
2087 			    ill->ill_name);
2088 			return;
2089 		}
2090 	}
2091 
2092 	bzero(&lso, sizeof (lso));
2093 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2094 	    DLD_ENABLE)) == 0) {
2095 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2096 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2097 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2098 		ip1dbg(("ill_capability_lso_enable: interface %s "
2099 		    "has enabled LSO\n ", ill->ill_name));
2100 	} else {
2101 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2102 		ill->ill_lso_capab = NULL;
2103 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2104 	}
2105 }
2106 
2107 static void
2108 ill_capability_dld_enable(ill_t *ill)
2109 {
2110 	mac_perim_handle_t mph;
2111 
2112 	ASSERT(IAM_WRITER_ILL(ill));
2113 
2114 	if (ill->ill_isv6)
2115 		return;
2116 
2117 	ill_mac_perim_enter(ill, &mph);
2118 	if (!ill->ill_isv6) {
2119 		ill_capability_direct_enable(ill);
2120 		ill_capability_poll_enable(ill);
2121 		ill_capability_lso_enable(ill);
2122 	}
2123 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2124 	ill_mac_perim_exit(ill, mph);
2125 }
2126 
2127 static void
2128 ill_capability_dld_disable(ill_t *ill)
2129 {
2130 	ill_dld_capab_t	*idc;
2131 	ill_dld_direct_t *idd;
2132 	mac_perim_handle_t	mph;
2133 
2134 	ASSERT(IAM_WRITER_ILL(ill));
2135 
2136 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2137 		return;
2138 
2139 	ill_mac_perim_enter(ill, &mph);
2140 
2141 	idc = ill->ill_dld_capab;
2142 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2143 		/*
2144 		 * For performance we avoid locks in the transmit data path
2145 		 * and don't maintain a count of the number of threads using
2146 		 * direct calls. Thus some threads could be using direct
2147 		 * transmit calls to GLD, even after the capability mechanism
2148 		 * turns it off. This is still safe since the handles used in
2149 		 * the direct calls continue to be valid until the unplumb is
2150 		 * completed. Remove the callback that was added (1-time) at
2151 		 * capab enable time.
2152 		 */
2153 		mutex_enter(&ill->ill_lock);
2154 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2155 		mutex_exit(&ill->ill_lock);
2156 		if (ill->ill_flownotify_mh != NULL) {
2157 			idd = &idc->idc_direct;
2158 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2159 			    ill->ill_flownotify_mh);
2160 			ill->ill_flownotify_mh = NULL;
2161 		}
2162 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2163 		    NULL, DLD_DISABLE);
2164 	}
2165 
2166 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2167 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2168 		ip_squeue_clean_all(ill);
2169 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2170 		    NULL, DLD_DISABLE);
2171 	}
2172 
2173 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2174 		ASSERT(ill->ill_lso_capab != NULL);
2175 		/*
2176 		 * Clear the capability flag for LSO but retain the
2177 		 * ill_lso_capab structure since it's possible that another
2178 		 * thread is still referring to it.  The structure only gets
2179 		 * deallocated when we destroy the ill.
2180 		 */
2181 
2182 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2183 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2184 		    NULL, DLD_DISABLE);
2185 	}
2186 
2187 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2188 	ill_mac_perim_exit(ill, mph);
2189 }
2190 
2191 /*
2192  * Capability Negotiation protocol
2193  *
2194  * We don't wait for DLPI capability operations to finish during interface
2195  * bringup or teardown. Doing so would introduce more asynchrony and the
2196  * interface up/down operations will need multiple return and restarts.
2197  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2198  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2199  * exclusive operation won't start until the DLPI operations of the previous
2200  * exclusive operation complete.
2201  *
2202  * The capability state machine is shown below.
2203  *
2204  * state		next state		event, action
2205  *
2206  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2207  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2208  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2209  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2210  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2211  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2212  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2213  *						    ill_capability_probe.
2214  */
2215 
2216 /*
2217  * Dedicated thread started from ip_stack_init that handles capability
2218  * disable. This thread ensures the taskq dispatch does not fail by waiting
2219  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2220  * that direct calls to DLD are done in a cv_waitable context.
2221  */
2222 void
2223 ill_taskq_dispatch(ip_stack_t *ipst)
2224 {
2225 	callb_cpr_t cprinfo;
2226 	char 	name[64];
2227 	mblk_t	*mp;
2228 
2229 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2230 	    ipst->ips_netstack->netstack_stackid);
2231 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2232 	    name);
2233 	mutex_enter(&ipst->ips_capab_taskq_lock);
2234 
2235 	for (;;) {
2236 		mp = ipst->ips_capab_taskq_head;
2237 		while (mp != NULL) {
2238 			ipst->ips_capab_taskq_head = mp->b_next;
2239 			if (ipst->ips_capab_taskq_head == NULL)
2240 				ipst->ips_capab_taskq_tail = NULL;
2241 			mutex_exit(&ipst->ips_capab_taskq_lock);
2242 			mp->b_next = NULL;
2243 
2244 			VERIFY(taskq_dispatch(system_taskq,
2245 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2246 			mutex_enter(&ipst->ips_capab_taskq_lock);
2247 			mp = ipst->ips_capab_taskq_head;
2248 		}
2249 
2250 		if (ipst->ips_capab_taskq_quit)
2251 			break;
2252 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2253 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2254 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2255 	}
2256 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2257 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2258 	CALLB_CPR_EXIT(&cprinfo);
2259 	thread_exit();
2260 }
2261 
2262 /*
2263  * Consume a new-style hardware capabilities negotiation ack.
2264  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2265  */
2266 static void
2267 ill_capability_ack_thr(void *arg)
2268 {
2269 	mblk_t	*mp = arg;
2270 	dl_capability_ack_t *capp;
2271 	dl_capability_sub_t *subp, *endp;
2272 	ill_t	*ill;
2273 	boolean_t reneg;
2274 
2275 	ill = (ill_t *)mp->b_prev;
2276 	mp->b_prev = NULL;
2277 
2278 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2279 
2280 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2281 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2282 		/*
2283 		 * We have received the ack for our DL_CAPAB reset request.
2284 		 * There isnt' anything in the message that needs processing.
2285 		 * All message based capabilities have been disabled, now
2286 		 * do the function call based capability disable.
2287 		 */
2288 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2289 		ill_capability_dld_disable(ill);
2290 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2291 		if (reneg)
2292 			ill_capability_probe(ill);
2293 		goto done;
2294 	}
2295 
2296 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2297 		ill->ill_dlpi_capab_state = IDCS_OK;
2298 
2299 	capp = (dl_capability_ack_t *)mp->b_rptr;
2300 
2301 	if (capp->dl_sub_length == 0) {
2302 		/* no new-style capabilities */
2303 		goto done;
2304 	}
2305 
2306 	/* make sure the driver supplied correct dl_sub_length */
2307 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2308 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2309 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2310 		goto done;
2311 	}
2312 
2313 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2314 	/*
2315 	 * There are sub-capabilities. Process the ones we know about.
2316 	 * Loop until we don't have room for another sub-cap header..
2317 	 */
2318 	for (subp = SC(capp, capp->dl_sub_offset),
2319 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2320 	    subp <= endp;
2321 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2322 
2323 		switch (subp->dl_cap) {
2324 		case DL_CAPAB_ID_WRAPPER:
2325 			ill_capability_id_ack(ill, mp, subp);
2326 			break;
2327 		default:
2328 			ill_capability_dispatch(ill, mp, subp);
2329 			break;
2330 		}
2331 	}
2332 #undef SC
2333 done:
2334 	inet_freemsg(mp);
2335 	ill_capability_done(ill);
2336 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2337 }
2338 
2339 /*
2340  * This needs to be started in a taskq thread to provide a cv_waitable
2341  * context.
2342  */
2343 void
2344 ill_capability_ack(ill_t *ill, mblk_t *mp)
2345 {
2346 	ip_stack_t	*ipst = ill->ill_ipst;
2347 
2348 	mp->b_prev = (mblk_t *)ill;
2349 	ASSERT(mp->b_next == NULL);
2350 
2351 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2352 	    TQ_NOSLEEP) != 0)
2353 		return;
2354 
2355 	/*
2356 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2357 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2358 	 */
2359 	mutex_enter(&ipst->ips_capab_taskq_lock);
2360 	if (ipst->ips_capab_taskq_head == NULL) {
2361 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2362 		ipst->ips_capab_taskq_head = mp;
2363 	} else {
2364 		ipst->ips_capab_taskq_tail->b_next = mp;
2365 	}
2366 	ipst->ips_capab_taskq_tail = mp;
2367 
2368 	cv_signal(&ipst->ips_capab_taskq_cv);
2369 	mutex_exit(&ipst->ips_capab_taskq_lock);
2370 }
2371 
2372 /*
2373  * This routine is called to scan the fragmentation reassembly table for
2374  * the specified ILL for any packets that are starting to smell.
2375  * dead_interval is the maximum time in seconds that will be tolerated.  It
2376  * will either be the value specified in ip_g_frag_timeout, or zero if the
2377  * ILL is shutting down and it is time to blow everything off.
2378  *
2379  * It returns the number of seconds (as a time_t) that the next frag timer
2380  * should be scheduled for, 0 meaning that the timer doesn't need to be
2381  * re-started.  Note that the method of calculating next_timeout isn't
2382  * entirely accurate since time will flow between the time we grab
2383  * current_time and the time we schedule the next timeout.  This isn't a
2384  * big problem since this is the timer for sending an ICMP reassembly time
2385  * exceeded messages, and it doesn't have to be exactly accurate.
2386  *
2387  * This function is
2388  * sometimes called as writer, although this is not required.
2389  */
2390 time_t
2391 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2392 {
2393 	ipfb_t	*ipfb;
2394 	ipfb_t	*endp;
2395 	ipf_t	*ipf;
2396 	ipf_t	*ipfnext;
2397 	mblk_t	*mp;
2398 	time_t	current_time = gethrestime_sec();
2399 	time_t	next_timeout = 0;
2400 	uint32_t	hdr_length;
2401 	mblk_t	*send_icmp_head;
2402 	mblk_t	*send_icmp_head_v6;
2403 	ip_stack_t *ipst = ill->ill_ipst;
2404 	ip_recv_attr_t iras;
2405 
2406 	bzero(&iras, sizeof (iras));
2407 	iras.ira_flags = 0;
2408 	iras.ira_ill = iras.ira_rill = ill;
2409 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2410 	iras.ira_rifindex = iras.ira_ruifindex;
2411 
2412 	ipfb = ill->ill_frag_hash_tbl;
2413 	if (ipfb == NULL)
2414 		return (B_FALSE);
2415 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2416 	/* Walk the frag hash table. */
2417 	for (; ipfb < endp; ipfb++) {
2418 		send_icmp_head = NULL;
2419 		send_icmp_head_v6 = NULL;
2420 		mutex_enter(&ipfb->ipfb_lock);
2421 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2422 			time_t frag_time = current_time - ipf->ipf_timestamp;
2423 			time_t frag_timeout;
2424 
2425 			if (frag_time < dead_interval) {
2426 				/*
2427 				 * There are some outstanding fragments
2428 				 * that will timeout later.  Make note of
2429 				 * the time so that we can reschedule the
2430 				 * next timeout appropriately.
2431 				 */
2432 				frag_timeout = dead_interval - frag_time;
2433 				if (next_timeout == 0 ||
2434 				    frag_timeout < next_timeout) {
2435 					next_timeout = frag_timeout;
2436 				}
2437 				break;
2438 			}
2439 			/* Time's up.  Get it out of here. */
2440 			hdr_length = ipf->ipf_nf_hdr_len;
2441 			ipfnext = ipf->ipf_hash_next;
2442 			if (ipfnext)
2443 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2444 			*ipf->ipf_ptphn = ipfnext;
2445 			mp = ipf->ipf_mp->b_cont;
2446 			for (; mp; mp = mp->b_cont) {
2447 				/* Extra points for neatness. */
2448 				IP_REASS_SET_START(mp, 0);
2449 				IP_REASS_SET_END(mp, 0);
2450 			}
2451 			mp = ipf->ipf_mp->b_cont;
2452 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2453 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2454 			ipfb->ipfb_count -= ipf->ipf_count;
2455 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2456 			ipfb->ipfb_frag_pkts--;
2457 			/*
2458 			 * We do not send any icmp message from here because
2459 			 * we currently are holding the ipfb_lock for this
2460 			 * hash chain. If we try and send any icmp messages
2461 			 * from here we may end up via a put back into ip
2462 			 * trying to get the same lock, causing a recursive
2463 			 * mutex panic. Instead we build a list and send all
2464 			 * the icmp messages after we have dropped the lock.
2465 			 */
2466 			if (ill->ill_isv6) {
2467 				if (hdr_length != 0) {
2468 					mp->b_next = send_icmp_head_v6;
2469 					send_icmp_head_v6 = mp;
2470 				} else {
2471 					freemsg(mp);
2472 				}
2473 			} else {
2474 				if (hdr_length != 0) {
2475 					mp->b_next = send_icmp_head;
2476 					send_icmp_head = mp;
2477 				} else {
2478 					freemsg(mp);
2479 				}
2480 			}
2481 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2482 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2483 			freeb(ipf->ipf_mp);
2484 		}
2485 		mutex_exit(&ipfb->ipfb_lock);
2486 		/*
2487 		 * Now need to send any icmp messages that we delayed from
2488 		 * above.
2489 		 */
2490 		while (send_icmp_head_v6 != NULL) {
2491 			ip6_t *ip6h;
2492 
2493 			mp = send_icmp_head_v6;
2494 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2495 			mp->b_next = NULL;
2496 			ip6h = (ip6_t *)mp->b_rptr;
2497 			iras.ira_flags = 0;
2498 			/*
2499 			 * This will result in an incorrect ALL_ZONES zoneid
2500 			 * for multicast packets, but we
2501 			 * don't send ICMP errors for those in any case.
2502 			 */
2503 			iras.ira_zoneid =
2504 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2505 			    ill, ipst);
2506 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2507 			icmp_time_exceeded_v6(mp,
2508 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2509 			    &iras);
2510 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2511 		}
2512 		while (send_icmp_head != NULL) {
2513 			ipaddr_t dst;
2514 
2515 			mp = send_icmp_head;
2516 			send_icmp_head = send_icmp_head->b_next;
2517 			mp->b_next = NULL;
2518 
2519 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2520 
2521 			iras.ira_flags = IRAF_IS_IPV4;
2522 			/*
2523 			 * This will result in an incorrect ALL_ZONES zoneid
2524 			 * for broadcast and multicast packets, but we
2525 			 * don't send ICMP errors for those in any case.
2526 			 */
2527 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2528 			    ill, ipst);
2529 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2530 			icmp_time_exceeded(mp,
2531 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2532 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2533 		}
2534 	}
2535 	/*
2536 	 * A non-dying ILL will use the return value to decide whether to
2537 	 * restart the frag timer, and for how long.
2538 	 */
2539 	return (next_timeout);
2540 }
2541 
2542 /*
2543  * This routine is called when the approximate count of mblk memory used
2544  * for the specified ILL has exceeded max_count.
2545  */
2546 void
2547 ill_frag_prune(ill_t *ill, uint_t max_count)
2548 {
2549 	ipfb_t	*ipfb;
2550 	ipf_t	*ipf;
2551 	size_t	count;
2552 	clock_t now;
2553 
2554 	/*
2555 	 * If we are here within ip_min_frag_prune_time msecs remove
2556 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2557 	 * ill_frag_free_num_pkts.
2558 	 */
2559 	mutex_enter(&ill->ill_lock);
2560 	now = ddi_get_lbolt();
2561 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2562 	    (ip_min_frag_prune_time != 0 ?
2563 	    ip_min_frag_prune_time : msec_per_tick)) {
2564 
2565 		ill->ill_frag_free_num_pkts++;
2566 
2567 	} else {
2568 		ill->ill_frag_free_num_pkts = 0;
2569 	}
2570 	ill->ill_last_frag_clean_time = now;
2571 	mutex_exit(&ill->ill_lock);
2572 
2573 	/*
2574 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2575 	 */
2576 	if (ill->ill_frag_free_num_pkts != 0) {
2577 		int ix;
2578 
2579 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2580 			ipfb = &ill->ill_frag_hash_tbl[ix];
2581 			mutex_enter(&ipfb->ipfb_lock);
2582 			if (ipfb->ipfb_ipf != NULL) {
2583 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2584 				    ill->ill_frag_free_num_pkts);
2585 			}
2586 			mutex_exit(&ipfb->ipfb_lock);
2587 		}
2588 	}
2589 	/*
2590 	 * While the reassembly list for this ILL is too big, prune a fragment
2591 	 * queue by age, oldest first.
2592 	 */
2593 	while (ill->ill_frag_count > max_count) {
2594 		int	ix;
2595 		ipfb_t	*oipfb = NULL;
2596 		uint_t	oldest = UINT_MAX;
2597 
2598 		count = 0;
2599 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2600 			ipfb = &ill->ill_frag_hash_tbl[ix];
2601 			mutex_enter(&ipfb->ipfb_lock);
2602 			ipf = ipfb->ipfb_ipf;
2603 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2604 				oldest = ipf->ipf_gen;
2605 				oipfb = ipfb;
2606 			}
2607 			count += ipfb->ipfb_count;
2608 			mutex_exit(&ipfb->ipfb_lock);
2609 		}
2610 		if (oipfb == NULL)
2611 			break;
2612 
2613 		if (count <= max_count)
2614 			return;	/* Somebody beat us to it, nothing to do */
2615 		mutex_enter(&oipfb->ipfb_lock);
2616 		ipf = oipfb->ipfb_ipf;
2617 		if (ipf != NULL) {
2618 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2619 		}
2620 		mutex_exit(&oipfb->ipfb_lock);
2621 	}
2622 }
2623 
2624 /*
2625  * free 'free_cnt' fragmented packets starting at ipf.
2626  */
2627 void
2628 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2629 {
2630 	size_t	count;
2631 	mblk_t	*mp;
2632 	mblk_t	*tmp;
2633 	ipf_t **ipfp = ipf->ipf_ptphn;
2634 
2635 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2636 	ASSERT(ipfp != NULL);
2637 	ASSERT(ipf != NULL);
2638 
2639 	while (ipf != NULL && free_cnt-- > 0) {
2640 		count = ipf->ipf_count;
2641 		mp = ipf->ipf_mp;
2642 		ipf = ipf->ipf_hash_next;
2643 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2644 			IP_REASS_SET_START(tmp, 0);
2645 			IP_REASS_SET_END(tmp, 0);
2646 		}
2647 		atomic_add_32(&ill->ill_frag_count, -count);
2648 		ASSERT(ipfb->ipfb_count >= count);
2649 		ipfb->ipfb_count -= count;
2650 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2651 		ipfb->ipfb_frag_pkts--;
2652 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2653 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2654 		freemsg(mp);
2655 	}
2656 
2657 	if (ipf)
2658 		ipf->ipf_ptphn = ipfp;
2659 	ipfp[0] = ipf;
2660 }
2661 
2662 /*
2663  * Helper function for ill_forward_set().
2664  */
2665 static void
2666 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2667 {
2668 	ip_stack_t	*ipst = ill->ill_ipst;
2669 
2670 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2671 
2672 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2673 	    (enable ? "Enabling" : "Disabling"),
2674 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2675 	mutex_enter(&ill->ill_lock);
2676 	if (enable)
2677 		ill->ill_flags |= ILLF_ROUTER;
2678 	else
2679 		ill->ill_flags &= ~ILLF_ROUTER;
2680 	mutex_exit(&ill->ill_lock);
2681 	if (ill->ill_isv6)
2682 		ill_set_nce_router_flags(ill, enable);
2683 	/* Notify routing socket listeners of this change. */
2684 	if (ill->ill_ipif != NULL)
2685 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2686 }
2687 
2688 /*
2689  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2690  * socket messages for each interface whose flags we change.
2691  */
2692 int
2693 ill_forward_set(ill_t *ill, boolean_t enable)
2694 {
2695 	ipmp_illgrp_t *illg;
2696 	ip_stack_t *ipst = ill->ill_ipst;
2697 
2698 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2699 
2700 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2701 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2702 		return (0);
2703 
2704 	if (IS_LOOPBACK(ill))
2705 		return (EINVAL);
2706 
2707 	if (enable && ill->ill_allowed_ips_cnt > 0)
2708 		return (EPERM);
2709 
2710 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2711 		/*
2712 		 * Update all of the interfaces in the group.
2713 		 */
2714 		illg = ill->ill_grp;
2715 		ill = list_head(&illg->ig_if);
2716 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2717 			ill_forward_set_on_ill(ill, enable);
2718 
2719 		/*
2720 		 * Update the IPMP meta-interface.
2721 		 */
2722 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2723 		return (0);
2724 	}
2725 
2726 	ill_forward_set_on_ill(ill, enable);
2727 	return (0);
2728 }
2729 
2730 /*
2731  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2732  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2733  * set or clear.
2734  */
2735 static void
2736 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2737 {
2738 	ipif_t *ipif;
2739 	ncec_t *ncec;
2740 	nce_t *nce;
2741 
2742 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2743 		/*
2744 		 * NOTE: we match across the illgrp because nce's for
2745 		 * addresses on IPMP interfaces have an nce_ill that points to
2746 		 * the bound underlying ill.
2747 		 */
2748 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2749 		if (nce != NULL) {
2750 			ncec = nce->nce_common;
2751 			mutex_enter(&ncec->ncec_lock);
2752 			if (enable)
2753 				ncec->ncec_flags |= NCE_F_ISROUTER;
2754 			else
2755 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2756 			mutex_exit(&ncec->ncec_lock);
2757 			nce_refrele(nce);
2758 		}
2759 	}
2760 }
2761 
2762 /*
2763  * Intializes the context structure and returns the first ill in the list
2764  * cuurently start_list and end_list can have values:
2765  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2766  * IP_V4_G_HEAD		Traverse IPV4 list only.
2767  * IP_V6_G_HEAD		Traverse IPV6 list only.
2768  */
2769 
2770 /*
2771  * We don't check for CONDEMNED ills here. Caller must do that if
2772  * necessary under the ill lock.
2773  */
2774 ill_t *
2775 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2776     ip_stack_t *ipst)
2777 {
2778 	ill_if_t *ifp;
2779 	ill_t *ill;
2780 	avl_tree_t *avl_tree;
2781 
2782 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2783 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2784 
2785 	/*
2786 	 * setup the lists to search
2787 	 */
2788 	if (end_list != MAX_G_HEADS) {
2789 		ctx->ctx_current_list = start_list;
2790 		ctx->ctx_last_list = end_list;
2791 	} else {
2792 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2793 		ctx->ctx_current_list = 0;
2794 	}
2795 
2796 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2797 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2798 		if (ifp != (ill_if_t *)
2799 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2800 			avl_tree = &ifp->illif_avl_by_ppa;
2801 			ill = avl_first(avl_tree);
2802 			/*
2803 			 * ill is guaranteed to be non NULL or ifp should have
2804 			 * not existed.
2805 			 */
2806 			ASSERT(ill != NULL);
2807 			return (ill);
2808 		}
2809 		ctx->ctx_current_list++;
2810 	}
2811 
2812 	return (NULL);
2813 }
2814 
2815 /*
2816  * returns the next ill in the list. ill_first() must have been called
2817  * before calling ill_next() or bad things will happen.
2818  */
2819 
2820 /*
2821  * We don't check for CONDEMNED ills here. Caller must do that if
2822  * necessary under the ill lock.
2823  */
2824 ill_t *
2825 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2826 {
2827 	ill_if_t *ifp;
2828 	ill_t *ill;
2829 	ip_stack_t	*ipst = lastill->ill_ipst;
2830 
2831 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2832 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2833 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2834 	    AVL_AFTER)) != NULL) {
2835 		return (ill);
2836 	}
2837 
2838 	/* goto next ill_ifp in the list. */
2839 	ifp = lastill->ill_ifptr->illif_next;
2840 
2841 	/* make sure not at end of circular list */
2842 	while (ifp ==
2843 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2844 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2845 			return (NULL);
2846 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2847 	}
2848 
2849 	return (avl_first(&ifp->illif_avl_by_ppa));
2850 }
2851 
2852 /*
2853  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2854  * The final number (PPA) must not have any leading zeros.  Upon success, a
2855  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2856  */
2857 static char *
2858 ill_get_ppa_ptr(char *name)
2859 {
2860 	int namelen = strlen(name);
2861 	int end_ndx = namelen - 1;
2862 	int ppa_ndx, i;
2863 
2864 	/*
2865 	 * Check that the first character is [a-zA-Z], and that the last
2866 	 * character is [0-9].
2867 	 */
2868 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2869 		return (NULL);
2870 
2871 	/*
2872 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2873 	 */
2874 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2875 		if (!isdigit(name[ppa_ndx - 1]))
2876 			break;
2877 
2878 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2879 		return (NULL);
2880 
2881 	/*
2882 	 * Check that the intermediate characters are [a-z0-9.]
2883 	 */
2884 	for (i = 1; i < ppa_ndx; i++) {
2885 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2886 		    name[i] != '.' && name[i] != '_') {
2887 			return (NULL);
2888 		}
2889 	}
2890 
2891 	return (name + ppa_ndx);
2892 }
2893 
2894 /*
2895  * use avl tree to locate the ill.
2896  */
2897 static ill_t *
2898 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2899 {
2900 	char *ppa_ptr = NULL;
2901 	int len;
2902 	uint_t ppa;
2903 	ill_t *ill = NULL;
2904 	ill_if_t *ifp;
2905 	int list;
2906 
2907 	/*
2908 	 * get ppa ptr
2909 	 */
2910 	if (isv6)
2911 		list = IP_V6_G_HEAD;
2912 	else
2913 		list = IP_V4_G_HEAD;
2914 
2915 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2916 		return (NULL);
2917 	}
2918 
2919 	len = ppa_ptr - name + 1;
2920 
2921 	ppa = stoi(&ppa_ptr);
2922 
2923 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2924 
2925 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2926 		/*
2927 		 * match is done on len - 1 as the name is not null
2928 		 * terminated it contains ppa in addition to the interface
2929 		 * name.
2930 		 */
2931 		if ((ifp->illif_name_len == len) &&
2932 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2933 			break;
2934 		} else {
2935 			ifp = ifp->illif_next;
2936 		}
2937 	}
2938 
2939 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2940 		/*
2941 		 * Even the interface type does not exist.
2942 		 */
2943 		return (NULL);
2944 	}
2945 
2946 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2947 	if (ill != NULL) {
2948 		mutex_enter(&ill->ill_lock);
2949 		if (ILL_CAN_LOOKUP(ill)) {
2950 			ill_refhold_locked(ill);
2951 			mutex_exit(&ill->ill_lock);
2952 			return (ill);
2953 		}
2954 		mutex_exit(&ill->ill_lock);
2955 	}
2956 	return (NULL);
2957 }
2958 
2959 /*
2960  * comparison function for use with avl.
2961  */
2962 static int
2963 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2964 {
2965 	uint_t ppa;
2966 	uint_t ill_ppa;
2967 
2968 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2969 
2970 	ppa = *((uint_t *)ppa_ptr);
2971 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2972 	/*
2973 	 * We want the ill with the lowest ppa to be on the
2974 	 * top.
2975 	 */
2976 	if (ill_ppa < ppa)
2977 		return (1);
2978 	if (ill_ppa > ppa)
2979 		return (-1);
2980 	return (0);
2981 }
2982 
2983 /*
2984  * remove an interface type from the global list.
2985  */
2986 static void
2987 ill_delete_interface_type(ill_if_t *interface)
2988 {
2989 	ASSERT(interface != NULL);
2990 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2991 
2992 	avl_destroy(&interface->illif_avl_by_ppa);
2993 	if (interface->illif_ppa_arena != NULL)
2994 		vmem_destroy(interface->illif_ppa_arena);
2995 
2996 	remque(interface);
2997 
2998 	mi_free(interface);
2999 }
3000 
3001 /*
3002  * remove ill from the global list.
3003  */
3004 static void
3005 ill_glist_delete(ill_t *ill)
3006 {
3007 	ip_stack_t	*ipst;
3008 	phyint_t	*phyi;
3009 
3010 	if (ill == NULL)
3011 		return;
3012 	ipst = ill->ill_ipst;
3013 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3014 
3015 	/*
3016 	 * If the ill was never inserted into the AVL tree
3017 	 * we skip the if branch.
3018 	 */
3019 	if (ill->ill_ifptr != NULL) {
3020 		/*
3021 		 * remove from AVL tree and free ppa number
3022 		 */
3023 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3024 
3025 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3026 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3027 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3028 		}
3029 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3030 			ill_delete_interface_type(ill->ill_ifptr);
3031 		}
3032 
3033 		/*
3034 		 * Indicate ill is no longer in the list.
3035 		 */
3036 		ill->ill_ifptr = NULL;
3037 		ill->ill_name_length = 0;
3038 		ill->ill_name[0] = '\0';
3039 		ill->ill_ppa = UINT_MAX;
3040 	}
3041 
3042 	/* Generate one last event for this ill. */
3043 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3044 	    ill->ill_name_length);
3045 
3046 	ASSERT(ill->ill_phyint != NULL);
3047 	phyi = ill->ill_phyint;
3048 	ill->ill_phyint = NULL;
3049 
3050 	/*
3051 	 * ill_init allocates a phyint always to store the copy
3052 	 * of flags relevant to phyint. At that point in time, we could
3053 	 * not assign the name and hence phyint_illv4/v6 could not be
3054 	 * initialized. Later in ipif_set_values, we assign the name to
3055 	 * the ill, at which point in time we assign phyint_illv4/v6.
3056 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3057 	 */
3058 	if (ill->ill_flags & ILLF_IPV6)
3059 		phyi->phyint_illv6 = NULL;
3060 	else
3061 		phyi->phyint_illv4 = NULL;
3062 
3063 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3064 		rw_exit(&ipst->ips_ill_g_lock);
3065 		return;
3066 	}
3067 
3068 	/*
3069 	 * There are no ills left on this phyint; pull it out of the phyint
3070 	 * avl trees, and free it.
3071 	 */
3072 	if (phyi->phyint_ifindex > 0) {
3073 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3074 		    phyi);
3075 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3076 		    phyi);
3077 	}
3078 	rw_exit(&ipst->ips_ill_g_lock);
3079 
3080 	phyint_free(phyi);
3081 }
3082 
3083 /*
3084  * allocate a ppa, if the number of plumbed interfaces of this type are
3085  * less than ill_no_arena do a linear search to find a unused ppa.
3086  * When the number goes beyond ill_no_arena switch to using an arena.
3087  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3088  * is the return value for an error condition, so allocation starts at one
3089  * and is decremented by one.
3090  */
3091 static int
3092 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3093 {
3094 	ill_t *tmp_ill;
3095 	uint_t start, end;
3096 	int ppa;
3097 
3098 	if (ifp->illif_ppa_arena == NULL &&
3099 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3100 		/*
3101 		 * Create an arena.
3102 		 */
3103 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3104 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3105 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3106 			/* allocate what has already been assigned */
3107 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3108 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3109 		    tmp_ill, AVL_AFTER)) {
3110 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3111 			    1,		/* size */
3112 			    1,		/* align/quantum */
3113 			    0,		/* phase */
3114 			    0,		/* nocross */
3115 			    /* minaddr */
3116 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3117 			    /* maxaddr */
3118 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3119 			    VM_NOSLEEP|VM_FIRSTFIT);
3120 			if (ppa == 0) {
3121 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3122 				    " failed while switching"));
3123 				vmem_destroy(ifp->illif_ppa_arena);
3124 				ifp->illif_ppa_arena = NULL;
3125 				break;
3126 			}
3127 		}
3128 	}
3129 
3130 	if (ifp->illif_ppa_arena != NULL) {
3131 		if (ill->ill_ppa == UINT_MAX) {
3132 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3133 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3134 			if (ppa == 0)
3135 				return (EAGAIN);
3136 			ill->ill_ppa = --ppa;
3137 		} else {
3138 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3139 			    1, 		/* size */
3140 			    1, 		/* align/quantum */
3141 			    0, 		/* phase */
3142 			    0, 		/* nocross */
3143 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3144 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3145 			    VM_NOSLEEP|VM_FIRSTFIT);
3146 			/*
3147 			 * Most likely the allocation failed because
3148 			 * the requested ppa was in use.
3149 			 */
3150 			if (ppa == 0)
3151 				return (EEXIST);
3152 		}
3153 		return (0);
3154 	}
3155 
3156 	/*
3157 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3158 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3159 	 */
3160 	if (ill->ill_ppa == UINT_MAX) {
3161 		end = UINT_MAX - 1;
3162 		start = 0;
3163 	} else {
3164 		end = start = ill->ill_ppa;
3165 	}
3166 
3167 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3168 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3169 		if (start++ >= end) {
3170 			if (ill->ill_ppa == UINT_MAX)
3171 				return (EAGAIN);
3172 			else
3173 				return (EEXIST);
3174 		}
3175 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3176 	}
3177 	ill->ill_ppa = start;
3178 	return (0);
3179 }
3180 
3181 /*
3182  * Insert ill into the list of configured ill's. Once this function completes,
3183  * the ill is globally visible and is available through lookups. More precisely
3184  * this happens after the caller drops the ill_g_lock.
3185  */
3186 static int
3187 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3188 {
3189 	ill_if_t *ill_interface;
3190 	avl_index_t where = 0;
3191 	int error;
3192 	int name_length;
3193 	int index;
3194 	boolean_t check_length = B_FALSE;
3195 	ip_stack_t	*ipst = ill->ill_ipst;
3196 
3197 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3198 
3199 	name_length = mi_strlen(name) + 1;
3200 
3201 	if (isv6)
3202 		index = IP_V6_G_HEAD;
3203 	else
3204 		index = IP_V4_G_HEAD;
3205 
3206 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3207 	/*
3208 	 * Search for interface type based on name
3209 	 */
3210 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3211 		if ((ill_interface->illif_name_len == name_length) &&
3212 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3213 			break;
3214 		}
3215 		ill_interface = ill_interface->illif_next;
3216 	}
3217 
3218 	/*
3219 	 * Interface type not found, create one.
3220 	 */
3221 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3222 		ill_g_head_t ghead;
3223 
3224 		/*
3225 		 * allocate ill_if_t structure
3226 		 */
3227 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3228 		if (ill_interface == NULL) {
3229 			return (ENOMEM);
3230 		}
3231 
3232 		(void) strcpy(ill_interface->illif_name, name);
3233 		ill_interface->illif_name_len = name_length;
3234 
3235 		avl_create(&ill_interface->illif_avl_by_ppa,
3236 		    ill_compare_ppa, sizeof (ill_t),
3237 		    offsetof(struct ill_s, ill_avl_byppa));
3238 
3239 		/*
3240 		 * link the structure in the back to maintain order
3241 		 * of configuration for ifconfig output.
3242 		 */
3243 		ghead = ipst->ips_ill_g_heads[index];
3244 		insque(ill_interface, ghead.ill_g_list_tail);
3245 	}
3246 
3247 	if (ill->ill_ppa == UINT_MAX)
3248 		check_length = B_TRUE;
3249 
3250 	error = ill_alloc_ppa(ill_interface, ill);
3251 	if (error != 0) {
3252 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3253 			ill_delete_interface_type(ill->ill_ifptr);
3254 		return (error);
3255 	}
3256 
3257 	/*
3258 	 * When the ppa is choosen by the system, check that there is
3259 	 * enough space to insert ppa. if a specific ppa was passed in this
3260 	 * check is not required as the interface name passed in will have
3261 	 * the right ppa in it.
3262 	 */
3263 	if (check_length) {
3264 		/*
3265 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3266 		 */
3267 		char buf[sizeof (uint_t) * 3];
3268 
3269 		/*
3270 		 * convert ppa to string to calculate the amount of space
3271 		 * required for it in the name.
3272 		 */
3273 		numtos(ill->ill_ppa, buf);
3274 
3275 		/* Do we have enough space to insert ppa ? */
3276 
3277 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3278 			/* Free ppa and interface type struct */
3279 			if (ill_interface->illif_ppa_arena != NULL) {
3280 				vmem_free(ill_interface->illif_ppa_arena,
3281 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3282 			}
3283 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3284 				ill_delete_interface_type(ill->ill_ifptr);
3285 
3286 			return (EINVAL);
3287 		}
3288 	}
3289 
3290 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3291 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3292 
3293 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3294 	    &where);
3295 	ill->ill_ifptr = ill_interface;
3296 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3297 
3298 	ill_phyint_reinit(ill);
3299 	return (0);
3300 }
3301 
3302 /* Initialize the per phyint ipsq used for serialization */
3303 static boolean_t
3304 ipsq_init(ill_t *ill, boolean_t enter)
3305 {
3306 	ipsq_t  *ipsq;
3307 	ipxop_t	*ipx;
3308 
3309 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3310 		return (B_FALSE);
3311 
3312 	ill->ill_phyint->phyint_ipsq = ipsq;
3313 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3314 	ipx->ipx_ipsq = ipsq;
3315 	ipsq->ipsq_next = ipsq;
3316 	ipsq->ipsq_phyint = ill->ill_phyint;
3317 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3318 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3319 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3320 	if (enter) {
3321 		ipx->ipx_writer = curthread;
3322 		ipx->ipx_forced = B_FALSE;
3323 		ipx->ipx_reentry_cnt = 1;
3324 #ifdef DEBUG
3325 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3326 #endif
3327 	}
3328 	return (B_TRUE);
3329 }
3330 
3331 /*
3332  * ill_init is called by ip_open when a device control stream is opened.
3333  * It does a few initializations, and shoots a DL_INFO_REQ message down
3334  * to the driver.  The response is later picked up in ip_rput_dlpi and
3335  * used to set up default mechanisms for talking to the driver.  (Always
3336  * called as writer.)
3337  *
3338  * If this function returns error, ip_open will call ip_close which in
3339  * turn will call ill_delete to clean up any memory allocated here that
3340  * is not yet freed.
3341  */
3342 int
3343 ill_init(queue_t *q, ill_t *ill)
3344 {
3345 	int	count;
3346 	dl_info_req_t	*dlir;
3347 	mblk_t	*info_mp;
3348 	uchar_t *frag_ptr;
3349 
3350 	/*
3351 	 * The ill is initialized to zero by mi_alloc*(). In addition
3352 	 * some fields already contain valid values, initialized in
3353 	 * ip_open(), before we reach here.
3354 	 */
3355 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3356 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3357 	ill->ill_saved_ire_cnt = 0;
3358 
3359 	ill->ill_rq = q;
3360 	ill->ill_wq = WR(q);
3361 
3362 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3363 	    BPRI_HI);
3364 	if (info_mp == NULL)
3365 		return (ENOMEM);
3366 
3367 	/*
3368 	 * Allocate sufficient space to contain our fragment hash table and
3369 	 * the device name.
3370 	 */
3371 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3372 	if (frag_ptr == NULL) {
3373 		freemsg(info_mp);
3374 		return (ENOMEM);
3375 	}
3376 	ill->ill_frag_ptr = frag_ptr;
3377 	ill->ill_frag_free_num_pkts = 0;
3378 	ill->ill_last_frag_clean_time = 0;
3379 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3380 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3381 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3382 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3383 		    NULL, MUTEX_DEFAULT, NULL);
3384 	}
3385 
3386 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3387 	if (ill->ill_phyint == NULL) {
3388 		freemsg(info_mp);
3389 		mi_free(frag_ptr);
3390 		return (ENOMEM);
3391 	}
3392 
3393 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3394 	/*
3395 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3396 	 * at this point because of the following reason. If we can't
3397 	 * enter the ipsq at some point and cv_wait, the writer that
3398 	 * wakes us up tries to locate us using the list of all phyints
3399 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3400 	 * If we don't set it now, we risk a missed wakeup.
3401 	 */
3402 	ill->ill_phyint->phyint_illv4 = ill;
3403 	ill->ill_ppa = UINT_MAX;
3404 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3405 
3406 	ill_set_inputfn(ill);
3407 
3408 	if (!ipsq_init(ill, B_TRUE)) {
3409 		freemsg(info_mp);
3410 		mi_free(frag_ptr);
3411 		mi_free(ill->ill_phyint);
3412 		return (ENOMEM);
3413 	}
3414 
3415 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3416 
3417 	/* Frag queue limit stuff */
3418 	ill->ill_frag_count = 0;
3419 	ill->ill_ipf_gen = 0;
3420 
3421 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3422 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3423 	ill->ill_global_timer = INFINITY;
3424 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3425 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3426 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3427 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3428 
3429 	/*
3430 	 * Initialize IPv6 configuration variables.  The IP module is always
3431 	 * opened as an IPv4 module.  Instead tracking down the cases where
3432 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3433 	 * here for convenience, this has no effect until the ill is set to do
3434 	 * IPv6.
3435 	 */
3436 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3437 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3438 	ill->ill_max_buf = ND_MAX_Q;
3439 	ill->ill_refcnt = 0;
3440 
3441 	/* Send down the Info Request to the driver. */
3442 	info_mp->b_datap->db_type = M_PCPROTO;
3443 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3444 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3445 	dlir->dl_primitive = DL_INFO_REQ;
3446 
3447 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3448 
3449 	qprocson(q);
3450 	ill_dlpi_send(ill, info_mp);
3451 
3452 	return (0);
3453 }
3454 
3455 /*
3456  * ill_dls_info
3457  * creates datalink socket info from the device.
3458  */
3459 int
3460 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3461 {
3462 	size_t	len;
3463 
3464 	sdl->sdl_family = AF_LINK;
3465 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3466 	sdl->sdl_type = ill->ill_type;
3467 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3468 	len = strlen(sdl->sdl_data);
3469 	ASSERT(len < 256);
3470 	sdl->sdl_nlen = (uchar_t)len;
3471 	sdl->sdl_alen = ill->ill_phys_addr_length;
3472 	sdl->sdl_slen = 0;
3473 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3474 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3475 
3476 	return (sizeof (struct sockaddr_dl));
3477 }
3478 
3479 /*
3480  * ill_xarp_info
3481  * creates xarp info from the device.
3482  */
3483 static int
3484 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3485 {
3486 	sdl->sdl_family = AF_LINK;
3487 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3488 	sdl->sdl_type = ill->ill_type;
3489 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3490 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3491 	sdl->sdl_alen = ill->ill_phys_addr_length;
3492 	sdl->sdl_slen = 0;
3493 	return (sdl->sdl_nlen);
3494 }
3495 
3496 static int
3497 loopback_kstat_update(kstat_t *ksp, int rw)
3498 {
3499 	kstat_named_t *kn;
3500 	netstackid_t	stackid;
3501 	netstack_t	*ns;
3502 	ip_stack_t	*ipst;
3503 
3504 	if (ksp == NULL || ksp->ks_data == NULL)
3505 		return (EIO);
3506 
3507 	if (rw == KSTAT_WRITE)
3508 		return (EACCES);
3509 
3510 	kn = KSTAT_NAMED_PTR(ksp);
3511 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3512 
3513 	ns = netstack_find_by_stackid(stackid);
3514 	if (ns == NULL)
3515 		return (-1);
3516 
3517 	ipst = ns->netstack_ip;
3518 	if (ipst == NULL) {
3519 		netstack_rele(ns);
3520 		return (-1);
3521 	}
3522 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3523 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3524 	netstack_rele(ns);
3525 	return (0);
3526 }
3527 
3528 /*
3529  * Has ifindex been plumbed already?
3530  */
3531 static boolean_t
3532 phyint_exists(uint_t index, ip_stack_t *ipst)
3533 {
3534 	ASSERT(index != 0);
3535 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3536 
3537 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3538 	    &index, NULL) != NULL);
3539 }
3540 
3541 /* Pick a unique ifindex */
3542 boolean_t
3543 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3544 {
3545 	uint_t starting_index;
3546 
3547 	if (!ipst->ips_ill_index_wrap) {
3548 		*indexp = ipst->ips_ill_index++;
3549 		if (ipst->ips_ill_index == 0) {
3550 			/* Reached the uint_t limit Next time wrap  */
3551 			ipst->ips_ill_index_wrap = B_TRUE;
3552 		}
3553 		return (B_TRUE);
3554 	}
3555 
3556 	/*
3557 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3558 	 * at this point and don't want to call any function that attempts
3559 	 * to get the lock again.
3560 	 */
3561 	starting_index = ipst->ips_ill_index++;
3562 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3563 		if (ipst->ips_ill_index != 0 &&
3564 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3565 			/* found unused index - use it */
3566 			*indexp = ipst->ips_ill_index;
3567 			return (B_TRUE);
3568 		}
3569 	}
3570 
3571 	/*
3572 	 * all interface indicies are inuse.
3573 	 */
3574 	return (B_FALSE);
3575 }
3576 
3577 /*
3578  * Assign a unique interface index for the phyint.
3579  */
3580 static boolean_t
3581 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3582 {
3583 	ASSERT(phyi->phyint_ifindex == 0);
3584 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3585 }
3586 
3587 /*
3588  * Initialize the flags on `phyi' as per the provided mactype.
3589  */
3590 static void
3591 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3592 {
3593 	uint64_t flags = 0;
3594 
3595 	/*
3596 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3597 	 * we always presume the underlying hardware is working and set
3598 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3599 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3600 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3601 	 */
3602 	if (mactype == SUNW_DL_IPMP)
3603 		flags |= PHYI_FAILED;
3604 	else
3605 		flags |= PHYI_RUNNING;
3606 
3607 	switch (mactype) {
3608 	case SUNW_DL_VNI:
3609 		flags |= PHYI_VIRTUAL;
3610 		break;
3611 	case SUNW_DL_IPMP:
3612 		flags |= PHYI_IPMP;
3613 		break;
3614 	case DL_LOOP:
3615 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3616 		break;
3617 	}
3618 
3619 	mutex_enter(&phyi->phyint_lock);
3620 	phyi->phyint_flags |= flags;
3621 	mutex_exit(&phyi->phyint_lock);
3622 }
3623 
3624 /*
3625  * Return a pointer to the ill which matches the supplied name.  Note that
3626  * the ill name length includes the null termination character.  (May be
3627  * called as writer.)
3628  * If do_alloc and the interface is "lo0" it will be automatically created.
3629  * Cannot bump up reference on condemned ills. So dup detect can't be done
3630  * using this func.
3631  */
3632 ill_t *
3633 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3634     boolean_t *did_alloc, ip_stack_t *ipst)
3635 {
3636 	ill_t	*ill;
3637 	ipif_t	*ipif;
3638 	ipsq_t	*ipsq;
3639 	kstat_named_t	*kn;
3640 	boolean_t isloopback;
3641 	in6_addr_t ov6addr;
3642 
3643 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3644 
3645 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3646 	ill = ill_find_by_name(name, isv6, ipst);
3647 	rw_exit(&ipst->ips_ill_g_lock);
3648 	if (ill != NULL)
3649 		return (ill);
3650 
3651 	/*
3652 	 * Couldn't find it.  Does this happen to be a lookup for the
3653 	 * loopback device and are we allowed to allocate it?
3654 	 */
3655 	if (!isloopback || !do_alloc)
3656 		return (NULL);
3657 
3658 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3659 	ill = ill_find_by_name(name, isv6, ipst);
3660 	if (ill != NULL) {
3661 		rw_exit(&ipst->ips_ill_g_lock);
3662 		return (ill);
3663 	}
3664 
3665 	/* Create the loopback device on demand */
3666 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3667 	    sizeof (ipif_loopback_name), BPRI_MED));
3668 	if (ill == NULL)
3669 		goto done;
3670 
3671 	*ill = ill_null;
3672 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3673 	ill->ill_ipst = ipst;
3674 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3675 	netstack_hold(ipst->ips_netstack);
3676 	/*
3677 	 * For exclusive stacks we set the zoneid to zero
3678 	 * to make IP operate as if in the global zone.
3679 	 */
3680 	ill->ill_zoneid = GLOBAL_ZONEID;
3681 
3682 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3683 	if (ill->ill_phyint == NULL)
3684 		goto done;
3685 
3686 	if (isv6)
3687 		ill->ill_phyint->phyint_illv6 = ill;
3688 	else
3689 		ill->ill_phyint->phyint_illv4 = ill;
3690 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3691 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3692 
3693 	if (isv6) {
3694 		ill->ill_isv6 = B_TRUE;
3695 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3696 	} else {
3697 		ill->ill_max_frag = ip_loopback_mtuplus;
3698 	}
3699 	if (!ill_allocate_mibs(ill))
3700 		goto done;
3701 	ill->ill_current_frag = ill->ill_max_frag;
3702 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3703 	/*
3704 	 * ipif_loopback_name can't be pointed at directly because its used
3705 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3706 	 * from the glist, ill_glist_delete() sets the first character of
3707 	 * ill_name to '\0'.
3708 	 */
3709 	ill->ill_name = (char *)ill + sizeof (*ill);
3710 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3711 	ill->ill_name_length = sizeof (ipif_loopback_name);
3712 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3713 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3714 
3715 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3716 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3717 	ill->ill_global_timer = INFINITY;
3718 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3719 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3720 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3721 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3722 
3723 	/* No resolver here. */
3724 	ill->ill_net_type = IRE_LOOPBACK;
3725 
3726 	/* Initialize the ipsq */
3727 	if (!ipsq_init(ill, B_FALSE))
3728 		goto done;
3729 
3730 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3731 	if (ipif == NULL)
3732 		goto done;
3733 
3734 	ill->ill_flags = ILLF_MULTICAST;
3735 
3736 	ov6addr = ipif->ipif_v6lcl_addr;
3737 	/* Set up default loopback address and mask. */
3738 	if (!isv6) {
3739 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3740 
3741 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3742 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3743 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3744 		    ipif->ipif_v6subnet);
3745 		ill->ill_flags |= ILLF_IPV4;
3746 	} else {
3747 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3748 		ipif->ipif_v6net_mask = ipv6_all_ones;
3749 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3750 		    ipif->ipif_v6subnet);
3751 		ill->ill_flags |= ILLF_IPV6;
3752 	}
3753 
3754 	/*
3755 	 * Chain us in at the end of the ill list. hold the ill
3756 	 * before we make it globally visible. 1 for the lookup.
3757 	 */
3758 	ill->ill_refcnt = 0;
3759 	ill_refhold(ill);
3760 
3761 	ill->ill_frag_count = 0;
3762 	ill->ill_frag_free_num_pkts = 0;
3763 	ill->ill_last_frag_clean_time = 0;
3764 
3765 	ipsq = ill->ill_phyint->phyint_ipsq;
3766 
3767 	ill_set_inputfn(ill);
3768 
3769 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3770 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3771 
3772 	/* Let SCTP know so that it can add this to its list */
3773 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3774 
3775 	/*
3776 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3777 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3778 	 * requires to be after ill_glist_insert() since we need the
3779 	 * ill_index set. Pass on ipv6_loopback as the old address.
3780 	 */
3781 	sctp_update_ipif_addr(ipif, ov6addr);
3782 
3783 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3784 
3785 	/*
3786 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3787 	 * If so, free our original one.
3788 	 */
3789 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3790 		ipsq_delete(ipsq);
3791 
3792 	if (ipst->ips_loopback_ksp == NULL) {
3793 		/* Export loopback interface statistics */
3794 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3795 		    ipif_loopback_name, "net",
3796 		    KSTAT_TYPE_NAMED, 2, 0,
3797 		    ipst->ips_netstack->netstack_stackid);
3798 		if (ipst->ips_loopback_ksp != NULL) {
3799 			ipst->ips_loopback_ksp->ks_update =
3800 			    loopback_kstat_update;
3801 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3802 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3803 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3804 			ipst->ips_loopback_ksp->ks_private =
3805 			    (void *)(uintptr_t)ipst->ips_netstack->
3806 			    netstack_stackid;
3807 			kstat_install(ipst->ips_loopback_ksp);
3808 		}
3809 	}
3810 
3811 	*did_alloc = B_TRUE;
3812 	rw_exit(&ipst->ips_ill_g_lock);
3813 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3814 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3815 	return (ill);
3816 done:
3817 	if (ill != NULL) {
3818 		if (ill->ill_phyint != NULL) {
3819 			ipsq = ill->ill_phyint->phyint_ipsq;
3820 			if (ipsq != NULL) {
3821 				ipsq->ipsq_phyint = NULL;
3822 				ipsq_delete(ipsq);
3823 			}
3824 			mi_free(ill->ill_phyint);
3825 		}
3826 		ill_free_mib(ill);
3827 		if (ill->ill_ipst != NULL)
3828 			netstack_rele(ill->ill_ipst->ips_netstack);
3829 		mi_free(ill);
3830 	}
3831 	rw_exit(&ipst->ips_ill_g_lock);
3832 	return (NULL);
3833 }
3834 
3835 /*
3836  * For IPP calls - use the ip_stack_t for global stack.
3837  */
3838 ill_t *
3839 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3840 {
3841 	ip_stack_t	*ipst;
3842 	ill_t		*ill;
3843 
3844 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3845 	if (ipst == NULL) {
3846 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3847 		return (NULL);
3848 	}
3849 
3850 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3851 	netstack_rele(ipst->ips_netstack);
3852 	return (ill);
3853 }
3854 
3855 /*
3856  * Return a pointer to the ill which matches the index and IP version type.
3857  */
3858 ill_t *
3859 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3860 {
3861 	ill_t	*ill;
3862 	phyint_t *phyi;
3863 
3864 	/*
3865 	 * Indexes are stored in the phyint - a common structure
3866 	 * to both IPv4 and IPv6.
3867 	 */
3868 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3869 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3870 	    (void *) &index, NULL);
3871 	if (phyi != NULL) {
3872 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3873 		if (ill != NULL) {
3874 			mutex_enter(&ill->ill_lock);
3875 			if (!ILL_IS_CONDEMNED(ill)) {
3876 				ill_refhold_locked(ill);
3877 				mutex_exit(&ill->ill_lock);
3878 				rw_exit(&ipst->ips_ill_g_lock);
3879 				return (ill);
3880 			}
3881 			mutex_exit(&ill->ill_lock);
3882 		}
3883 	}
3884 	rw_exit(&ipst->ips_ill_g_lock);
3885 	return (NULL);
3886 }
3887 
3888 /*
3889  * Verify whether or not an interface index is valid for the specified zoneid
3890  * to transmit packets.
3891  * It can be zero (meaning "reset") or an interface index assigned
3892  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3893  */
3894 boolean_t
3895 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3896     ip_stack_t *ipst)
3897 {
3898 	ill_t		*ill;
3899 
3900 	if (ifindex == 0)
3901 		return (B_TRUE);
3902 
3903 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3904 	if (ill == NULL)
3905 		return (B_FALSE);
3906 	if (IS_VNI(ill)) {
3907 		ill_refrele(ill);
3908 		return (B_FALSE);
3909 	}
3910 	ill_refrele(ill);
3911 	return (B_TRUE);
3912 }
3913 
3914 /*
3915  * Return the ifindex next in sequence after the passed in ifindex.
3916  * If there is no next ifindex for the given protocol, return 0.
3917  */
3918 uint_t
3919 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3920 {
3921 	phyint_t *phyi;
3922 	phyint_t *phyi_initial;
3923 	uint_t   ifindex;
3924 
3925 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3926 
3927 	if (index == 0) {
3928 		phyi = avl_first(
3929 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3930 	} else {
3931 		phyi = phyi_initial = avl_find(
3932 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3933 		    (void *) &index, NULL);
3934 	}
3935 
3936 	for (; phyi != NULL;
3937 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3938 	    phyi, AVL_AFTER)) {
3939 		/*
3940 		 * If we're not returning the first interface in the tree
3941 		 * and we still haven't moved past the phyint_t that
3942 		 * corresponds to index, avl_walk needs to be called again
3943 		 */
3944 		if (!((index != 0) && (phyi == phyi_initial))) {
3945 			if (isv6) {
3946 				if ((phyi->phyint_illv6) &&
3947 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3948 				    (phyi->phyint_illv6->ill_isv6 == 1))
3949 					break;
3950 			} else {
3951 				if ((phyi->phyint_illv4) &&
3952 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3953 				    (phyi->phyint_illv4->ill_isv6 == 0))
3954 					break;
3955 			}
3956 		}
3957 	}
3958 
3959 	rw_exit(&ipst->ips_ill_g_lock);
3960 
3961 	if (phyi != NULL)
3962 		ifindex = phyi->phyint_ifindex;
3963 	else
3964 		ifindex = 0;
3965 
3966 	return (ifindex);
3967 }
3968 
3969 /*
3970  * Return the ifindex for the named interface.
3971  * If there is no next ifindex for the interface, return 0.
3972  */
3973 uint_t
3974 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3975 {
3976 	phyint_t	*phyi;
3977 	avl_index_t	where = 0;
3978 	uint_t		ifindex;
3979 
3980 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3981 
3982 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3983 	    name, &where)) == NULL) {
3984 		rw_exit(&ipst->ips_ill_g_lock);
3985 		return (0);
3986 	}
3987 
3988 	ifindex = phyi->phyint_ifindex;
3989 
3990 	rw_exit(&ipst->ips_ill_g_lock);
3991 
3992 	return (ifindex);
3993 }
3994 
3995 /*
3996  * Return the ifindex to be used by upper layer protocols for instance
3997  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3998  */
3999 uint_t
4000 ill_get_upper_ifindex(const ill_t *ill)
4001 {
4002 	if (IS_UNDER_IPMP(ill))
4003 		return (ipmp_ill_get_ipmp_ifindex(ill));
4004 	else
4005 		return (ill->ill_phyint->phyint_ifindex);
4006 }
4007 
4008 
4009 /*
4010  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4011  * that gives a running thread a reference to the ill. This reference must be
4012  * released by the thread when it is done accessing the ill and related
4013  * objects. ill_refcnt can not be used to account for static references
4014  * such as other structures pointing to an ill. Callers must generally
4015  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4016  * or be sure that the ill is not being deleted or changing state before
4017  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4018  * ill won't change any of its critical state such as address, netmask etc.
4019  */
4020 void
4021 ill_refhold(ill_t *ill)
4022 {
4023 	mutex_enter(&ill->ill_lock);
4024 	ill->ill_refcnt++;
4025 	ILL_TRACE_REF(ill);
4026 	mutex_exit(&ill->ill_lock);
4027 }
4028 
4029 void
4030 ill_refhold_locked(ill_t *ill)
4031 {
4032 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4033 	ill->ill_refcnt++;
4034 	ILL_TRACE_REF(ill);
4035 }
4036 
4037 /* Returns true if we managed to get a refhold */
4038 boolean_t
4039 ill_check_and_refhold(ill_t *ill)
4040 {
4041 	mutex_enter(&ill->ill_lock);
4042 	if (!ILL_IS_CONDEMNED(ill)) {
4043 		ill_refhold_locked(ill);
4044 		mutex_exit(&ill->ill_lock);
4045 		return (B_TRUE);
4046 	}
4047 	mutex_exit(&ill->ill_lock);
4048 	return (B_FALSE);
4049 }
4050 
4051 /*
4052  * Must not be called while holding any locks. Otherwise if this is
4053  * the last reference to be released, there is a chance of recursive mutex
4054  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4055  * to restart an ioctl.
4056  */
4057 void
4058 ill_refrele(ill_t *ill)
4059 {
4060 	mutex_enter(&ill->ill_lock);
4061 	ASSERT(ill->ill_refcnt != 0);
4062 	ill->ill_refcnt--;
4063 	ILL_UNTRACE_REF(ill);
4064 	if (ill->ill_refcnt != 0) {
4065 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4066 		mutex_exit(&ill->ill_lock);
4067 		return;
4068 	}
4069 
4070 	/* Drops the ill_lock */
4071 	ipif_ill_refrele_tail(ill);
4072 }
4073 
4074 /*
4075  * Obtain a weak reference count on the ill. This reference ensures the
4076  * ill won't be freed, but the ill may change any of its critical state
4077  * such as netmask, address etc. Returns an error if the ill has started
4078  * closing.
4079  */
4080 boolean_t
4081 ill_waiter_inc(ill_t *ill)
4082 {
4083 	mutex_enter(&ill->ill_lock);
4084 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4085 		mutex_exit(&ill->ill_lock);
4086 		return (B_FALSE);
4087 	}
4088 	ill->ill_waiters++;
4089 	mutex_exit(&ill->ill_lock);
4090 	return (B_TRUE);
4091 }
4092 
4093 void
4094 ill_waiter_dcr(ill_t *ill)
4095 {
4096 	mutex_enter(&ill->ill_lock);
4097 	ill->ill_waiters--;
4098 	if (ill->ill_waiters == 0)
4099 		cv_broadcast(&ill->ill_cv);
4100 	mutex_exit(&ill->ill_lock);
4101 }
4102 
4103 /*
4104  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4105  * driver.  We construct best guess defaults for lower level information that
4106  * we need.  If an interface is brought up without injection of any overriding
4107  * information from outside, we have to be ready to go with these defaults.
4108  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4109  * we primarely want the dl_provider_style.
4110  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4111  * at which point we assume the other part of the information is valid.
4112  */
4113 void
4114 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4115 {
4116 	uchar_t		*brdcst_addr;
4117 	uint_t		brdcst_addr_length, phys_addr_length;
4118 	t_scalar_t	sap_length;
4119 	dl_info_ack_t	*dlia;
4120 	ip_m_t		*ipm;
4121 	dl_qos_cl_sel1_t *sel1;
4122 	int		min_mtu;
4123 
4124 	ASSERT(IAM_WRITER_ILL(ill));
4125 
4126 	/*
4127 	 * Till the ill is fully up  the ill is not globally visible.
4128 	 * So no need for a lock.
4129 	 */
4130 	dlia = (dl_info_ack_t *)mp->b_rptr;
4131 	ill->ill_mactype = dlia->dl_mac_type;
4132 
4133 	ipm = ip_m_lookup(dlia->dl_mac_type);
4134 	if (ipm == NULL) {
4135 		ipm = ip_m_lookup(DL_OTHER);
4136 		ASSERT(ipm != NULL);
4137 	}
4138 	ill->ill_media = ipm;
4139 
4140 	/*
4141 	 * When the new DLPI stuff is ready we'll pull lengths
4142 	 * from dlia.
4143 	 */
4144 	if (dlia->dl_version == DL_VERSION_2) {
4145 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4146 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4147 		    brdcst_addr_length);
4148 		if (brdcst_addr == NULL) {
4149 			brdcst_addr_length = 0;
4150 		}
4151 		sap_length = dlia->dl_sap_length;
4152 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4153 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4154 		    brdcst_addr_length, sap_length, phys_addr_length));
4155 	} else {
4156 		brdcst_addr_length = 6;
4157 		brdcst_addr = ip_six_byte_all_ones;
4158 		sap_length = -2;
4159 		phys_addr_length = brdcst_addr_length;
4160 	}
4161 
4162 	ill->ill_bcast_addr_length = brdcst_addr_length;
4163 	ill->ill_phys_addr_length = phys_addr_length;
4164 	ill->ill_sap_length = sap_length;
4165 
4166 	/*
4167 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4168 	 * but we must ensure a minimum IP MTU is used since other bits of
4169 	 * IP will fly apart otherwise.
4170 	 */
4171 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4172 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4173 	ill->ill_current_frag = ill->ill_max_frag;
4174 	ill->ill_mtu = ill->ill_max_frag;
4175 
4176 	ill->ill_type = ipm->ip_m_type;
4177 
4178 	if (!ill->ill_dlpi_style_set) {
4179 		if (dlia->dl_provider_style == DL_STYLE2)
4180 			ill->ill_needs_attach = 1;
4181 
4182 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4183 
4184 		/*
4185 		 * Allocate the first ipif on this ill.  We don't delay it
4186 		 * further as ioctl handling assumes at least one ipif exists.
4187 		 *
4188 		 * At this point we don't know whether the ill is v4 or v6.
4189 		 * We will know this whan the SIOCSLIFNAME happens and
4190 		 * the correct value for ill_isv6 will be assigned in
4191 		 * ipif_set_values(). We need to hold the ill lock and
4192 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4193 		 * the wakeup.
4194 		 */
4195 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4196 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4197 		mutex_enter(&ill->ill_lock);
4198 		ASSERT(ill->ill_dlpi_style_set == 0);
4199 		ill->ill_dlpi_style_set = 1;
4200 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4201 		cv_broadcast(&ill->ill_cv);
4202 		mutex_exit(&ill->ill_lock);
4203 		freemsg(mp);
4204 		return;
4205 	}
4206 	ASSERT(ill->ill_ipif != NULL);
4207 	/*
4208 	 * We know whether it is IPv4 or IPv6 now, as this is the
4209 	 * second DL_INFO_ACK we are recieving in response to the
4210 	 * DL_INFO_REQ sent in ipif_set_values.
4211 	 */
4212 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4213 	/*
4214 	 * Clear all the flags that were set based on ill_bcast_addr_length
4215 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4216 	 * changed now and we need to re-evaluate.
4217 	 */
4218 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4219 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4220 
4221 	/*
4222 	 * Free ill_bcast_mp as things could have changed now.
4223 	 *
4224 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4225 	 * with no underlying interfaces (and thus an unknown broadcast
4226 	 * address length), but we enforce that an interface is broadcast-
4227 	 * capable as part of allowing it to join a group.
4228 	 */
4229 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4230 		if (ill->ill_bcast_mp != NULL)
4231 			freemsg(ill->ill_bcast_mp);
4232 		ill->ill_net_type = IRE_IF_NORESOLVER;
4233 
4234 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4235 		    ill->ill_phys_addr_length,
4236 		    ill->ill_sap,
4237 		    ill->ill_sap_length);
4238 
4239 		if (ill->ill_isv6)
4240 			/*
4241 			 * Note: xresolv interfaces will eventually need NOARP
4242 			 * set here as well, but that will require those
4243 			 * external resolvers to have some knowledge of
4244 			 * that flag and act appropriately. Not to be changed
4245 			 * at present.
4246 			 */
4247 			ill->ill_flags |= ILLF_NONUD;
4248 		else
4249 			ill->ill_flags |= ILLF_NOARP;
4250 
4251 		if (ill->ill_mactype == SUNW_DL_VNI) {
4252 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4253 		} else if (ill->ill_phys_addr_length == 0 ||
4254 		    ill->ill_mactype == DL_IPV4 ||
4255 		    ill->ill_mactype == DL_IPV6) {
4256 			/*
4257 			 * The underying link is point-to-point, so mark the
4258 			 * interface as such.  We can do IP multicast over
4259 			 * such a link since it transmits all network-layer
4260 			 * packets to the remote side the same way.
4261 			 */
4262 			ill->ill_flags |= ILLF_MULTICAST;
4263 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4264 		}
4265 	} else {
4266 		ill->ill_net_type = IRE_IF_RESOLVER;
4267 		if (ill->ill_bcast_mp != NULL)
4268 			freemsg(ill->ill_bcast_mp);
4269 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4270 		    ill->ill_bcast_addr_length, ill->ill_sap,
4271 		    ill->ill_sap_length);
4272 		/*
4273 		 * Later detect lack of DLPI driver multicast
4274 		 * capability by catching DL_ENABMULTI errors in
4275 		 * ip_rput_dlpi.
4276 		 */
4277 		ill->ill_flags |= ILLF_MULTICAST;
4278 		if (!ill->ill_isv6)
4279 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4280 	}
4281 
4282 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4283 	if (ill->ill_mactype == SUNW_DL_IPMP)
4284 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4285 
4286 	/* By default an interface does not support any CoS marking */
4287 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4288 
4289 	/*
4290 	 * If we get QoS information in DL_INFO_ACK, the device supports
4291 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4292 	 */
4293 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4294 	    dlia->dl_qos_length);
4295 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4296 		ill->ill_flags |= ILLF_COS_ENABLED;
4297 	}
4298 
4299 	/* Clear any previous error indication. */
4300 	ill->ill_error = 0;
4301 	freemsg(mp);
4302 }
4303 
4304 /*
4305  * Perform various checks to verify that an address would make sense as a
4306  * local, remote, or subnet interface address.
4307  */
4308 static boolean_t
4309 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4310 {
4311 	ipaddr_t	net_mask;
4312 
4313 	/*
4314 	 * Don't allow all zeroes, or all ones, but allow
4315 	 * all ones netmask.
4316 	 */
4317 	if ((net_mask = ip_net_mask(addr)) == 0)
4318 		return (B_FALSE);
4319 	/* A given netmask overrides the "guess" netmask */
4320 	if (subnet_mask != 0)
4321 		net_mask = subnet_mask;
4322 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4323 	    (addr == (addr | ~net_mask)))) {
4324 		return (B_FALSE);
4325 	}
4326 
4327 	/*
4328 	 * Even if the netmask is all ones, we do not allow address to be
4329 	 * 255.255.255.255
4330 	 */
4331 	if (addr == INADDR_BROADCAST)
4332 		return (B_FALSE);
4333 
4334 	if (CLASSD(addr))
4335 		return (B_FALSE);
4336 
4337 	return (B_TRUE);
4338 }
4339 
4340 #define	V6_IPIF_LINKLOCAL(p)	\
4341 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4342 
4343 /*
4344  * Compare two given ipifs and check if the second one is better than
4345  * the first one using the order of preference (not taking deprecated
4346  * into acount) specified in ipif_lookup_multicast().
4347  */
4348 static boolean_t
4349 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4350 {
4351 	/* Check the least preferred first. */
4352 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4353 		/* If both ipifs are the same, use the first one. */
4354 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4355 			return (B_FALSE);
4356 		else
4357 			return (B_TRUE);
4358 	}
4359 
4360 	/* For IPv6, check for link local address. */
4361 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4362 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4363 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4364 			/* The second one is equal or less preferred. */
4365 			return (B_FALSE);
4366 		} else {
4367 			return (B_TRUE);
4368 		}
4369 	}
4370 
4371 	/* Then check for point to point interface. */
4372 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4373 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4374 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4375 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4376 			return (B_FALSE);
4377 		} else {
4378 			return (B_TRUE);
4379 		}
4380 	}
4381 
4382 	/* old_ipif is a normal interface, so no need to use the new one. */
4383 	return (B_FALSE);
4384 }
4385 
4386 /*
4387  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4388  * The ipif must be up, and its ill must multicast-capable, not
4389  * condemned, not an underlying interface in an IPMP group, and
4390  * not a VNI interface.  Order of preference:
4391  *
4392  * 	1a. normal
4393  * 	1b. normal, but deprecated
4394  * 	2a. point to point
4395  * 	2b. point to point, but deprecated
4396  * 	3a. link local
4397  * 	3b. link local, but deprecated
4398  * 	4. loopback.
4399  */
4400 static ipif_t *
4401 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4402 {
4403 	ill_t			*ill;
4404 	ill_walk_context_t	ctx;
4405 	ipif_t			*ipif;
4406 	ipif_t			*saved_ipif = NULL;
4407 	ipif_t			*dep_ipif = NULL;
4408 
4409 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4410 	if (isv6)
4411 		ill = ILL_START_WALK_V6(&ctx, ipst);
4412 	else
4413 		ill = ILL_START_WALK_V4(&ctx, ipst);
4414 
4415 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4416 		mutex_enter(&ill->ill_lock);
4417 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4418 		    ILL_IS_CONDEMNED(ill) ||
4419 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4420 			mutex_exit(&ill->ill_lock);
4421 			continue;
4422 		}
4423 		for (ipif = ill->ill_ipif; ipif != NULL;
4424 		    ipif = ipif->ipif_next) {
4425 			if (zoneid != ipif->ipif_zoneid &&
4426 			    zoneid != ALL_ZONES &&
4427 			    ipif->ipif_zoneid != ALL_ZONES) {
4428 				continue;
4429 			}
4430 			if (!(ipif->ipif_flags & IPIF_UP) ||
4431 			    IPIF_IS_CONDEMNED(ipif)) {
4432 				continue;
4433 			}
4434 
4435 			/*
4436 			 * Found one candidate.  If it is deprecated,
4437 			 * remember it in dep_ipif.  If it is not deprecated,
4438 			 * remember it in saved_ipif.
4439 			 */
4440 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4441 				if (dep_ipif == NULL) {
4442 					dep_ipif = ipif;
4443 				} else if (ipif_comp_multi(dep_ipif, ipif,
4444 				    isv6)) {
4445 					/*
4446 					 * If the previous dep_ipif does not
4447 					 * belong to the same ill, we've done
4448 					 * a ipif_refhold() on it.  So we need
4449 					 * to release it.
4450 					 */
4451 					if (dep_ipif->ipif_ill != ill)
4452 						ipif_refrele(dep_ipif);
4453 					dep_ipif = ipif;
4454 				}
4455 				continue;
4456 			}
4457 			if (saved_ipif == NULL) {
4458 				saved_ipif = ipif;
4459 			} else {
4460 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4461 					if (saved_ipif->ipif_ill != ill)
4462 						ipif_refrele(saved_ipif);
4463 					saved_ipif = ipif;
4464 				}
4465 			}
4466 		}
4467 		/*
4468 		 * Before going to the next ill, do a ipif_refhold() on the
4469 		 * saved ones.
4470 		 */
4471 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4472 			ipif_refhold_locked(saved_ipif);
4473 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4474 			ipif_refhold_locked(dep_ipif);
4475 		mutex_exit(&ill->ill_lock);
4476 	}
4477 	rw_exit(&ipst->ips_ill_g_lock);
4478 
4479 	/*
4480 	 * If we have only the saved_ipif, return it.  But if we have both
4481 	 * saved_ipif and dep_ipif, check to see which one is better.
4482 	 */
4483 	if (saved_ipif != NULL) {
4484 		if (dep_ipif != NULL) {
4485 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4486 				ipif_refrele(saved_ipif);
4487 				return (dep_ipif);
4488 			} else {
4489 				ipif_refrele(dep_ipif);
4490 				return (saved_ipif);
4491 			}
4492 		}
4493 		return (saved_ipif);
4494 	} else {
4495 		return (dep_ipif);
4496 	}
4497 }
4498 
4499 ill_t *
4500 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4501 {
4502 	ipif_t *ipif;
4503 	ill_t *ill;
4504 
4505 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4506 	if (ipif == NULL)
4507 		return (NULL);
4508 
4509 	ill = ipif->ipif_ill;
4510 	ill_refhold(ill);
4511 	ipif_refrele(ipif);
4512 	return (ill);
4513 }
4514 
4515 /*
4516  * This function is called when an application does not specify an interface
4517  * to be used for multicast traffic (joining a group/sending data).  It
4518  * calls ire_lookup_multi() to look for an interface route for the
4519  * specified multicast group.  Doing this allows the administrator to add
4520  * prefix routes for multicast to indicate which interface to be used for
4521  * multicast traffic in the above scenario.  The route could be for all
4522  * multicast (224.0/4), for a single multicast group (a /32 route) or
4523  * anything in between.  If there is no such multicast route, we just find
4524  * any multicast capable interface and return it.  The returned ipif
4525  * is refhold'ed.
4526  *
4527  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4528  * unicast table. This is used by CGTP.
4529  */
4530 ill_t *
4531 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4532     boolean_t *multirtp, ipaddr_t *setsrcp)
4533 {
4534 	ill_t			*ill;
4535 
4536 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4537 	if (ill != NULL)
4538 		return (ill);
4539 
4540 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4541 }
4542 
4543 /*
4544  * Look for an ipif with the specified interface address and destination.
4545  * The destination address is used only for matching point-to-point interfaces.
4546  */
4547 ipif_t *
4548 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4549 {
4550 	ipif_t	*ipif;
4551 	ill_t	*ill;
4552 	ill_walk_context_t ctx;
4553 
4554 	/*
4555 	 * First match all the point-to-point interfaces
4556 	 * before looking at non-point-to-point interfaces.
4557 	 * This is done to avoid returning non-point-to-point
4558 	 * ipif instead of unnumbered point-to-point ipif.
4559 	 */
4560 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4561 	ill = ILL_START_WALK_V4(&ctx, ipst);
4562 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4563 		mutex_enter(&ill->ill_lock);
4564 		for (ipif = ill->ill_ipif; ipif != NULL;
4565 		    ipif = ipif->ipif_next) {
4566 			/* Allow the ipif to be down */
4567 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4568 			    (ipif->ipif_lcl_addr == if_addr) &&
4569 			    (ipif->ipif_pp_dst_addr == dst)) {
4570 				if (!IPIF_IS_CONDEMNED(ipif)) {
4571 					ipif_refhold_locked(ipif);
4572 					mutex_exit(&ill->ill_lock);
4573 					rw_exit(&ipst->ips_ill_g_lock);
4574 					return (ipif);
4575 				}
4576 			}
4577 		}
4578 		mutex_exit(&ill->ill_lock);
4579 	}
4580 	rw_exit(&ipst->ips_ill_g_lock);
4581 
4582 	/* lookup the ipif based on interface address */
4583 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4584 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4585 	return (ipif);
4586 }
4587 
4588 /*
4589  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4590  */
4591 static ipif_t *
4592 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4593     zoneid_t zoneid, ip_stack_t *ipst)
4594 {
4595 	ipif_t  *ipif;
4596 	ill_t   *ill;
4597 	boolean_t ptp = B_FALSE;
4598 	ill_walk_context_t	ctx;
4599 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4600 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4601 
4602 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4603 	/*
4604 	 * Repeat twice, first based on local addresses and
4605 	 * next time for pointopoint.
4606 	 */
4607 repeat:
4608 	ill = ILL_START_WALK_V4(&ctx, ipst);
4609 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4610 		if (match_ill != NULL && ill != match_ill &&
4611 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4612 			continue;
4613 		}
4614 		mutex_enter(&ill->ill_lock);
4615 		for (ipif = ill->ill_ipif; ipif != NULL;
4616 		    ipif = ipif->ipif_next) {
4617 			if (zoneid != ALL_ZONES &&
4618 			    zoneid != ipif->ipif_zoneid &&
4619 			    ipif->ipif_zoneid != ALL_ZONES)
4620 				continue;
4621 
4622 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4623 				continue;
4624 
4625 			/* Allow the ipif to be down */
4626 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4627 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4628 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4629 			    (ipif->ipif_pp_dst_addr == addr))) {
4630 				if (!IPIF_IS_CONDEMNED(ipif)) {
4631 					ipif_refhold_locked(ipif);
4632 					mutex_exit(&ill->ill_lock);
4633 					rw_exit(&ipst->ips_ill_g_lock);
4634 					return (ipif);
4635 				}
4636 			}
4637 		}
4638 		mutex_exit(&ill->ill_lock);
4639 	}
4640 
4641 	/* If we already did the ptp case, then we are done */
4642 	if (ptp) {
4643 		rw_exit(&ipst->ips_ill_g_lock);
4644 		return (NULL);
4645 	}
4646 	ptp = B_TRUE;
4647 	goto repeat;
4648 }
4649 
4650 /*
4651  * Lookup an ipif with the specified address.  For point-to-point links we
4652  * look for matches on either the destination address or the local address,
4653  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4654  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4655  * (or illgrp if `match_ill' is in an IPMP group).
4656  */
4657 ipif_t *
4658 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4659     ip_stack_t *ipst)
4660 {
4661 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4662 	    zoneid, ipst));
4663 }
4664 
4665 /*
4666  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4667  * except that we will only return an address if it is not marked as
4668  * IPIF_DUPLICATE
4669  */
4670 ipif_t *
4671 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4672     ip_stack_t *ipst)
4673 {
4674 	return (ipif_lookup_addr_common(addr, match_ill,
4675 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4676 	    zoneid, ipst));
4677 }
4678 
4679 /*
4680  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4681  * `match_ill' across the IPMP group.  This function is only needed in some
4682  * corner-cases; almost everything should use ipif_lookup_addr().
4683  */
4684 ipif_t *
4685 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4686 {
4687 	ASSERT(match_ill != NULL);
4688 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4689 	    ipst));
4690 }
4691 
4692 /*
4693  * Look for an ipif with the specified address. For point-point links
4694  * we look for matches on either the destination address and the local
4695  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4696  * is set.
4697  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4698  * ill (or illgrp if `match_ill' is in an IPMP group).
4699  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4700  */
4701 zoneid_t
4702 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4703 {
4704 	zoneid_t zoneid;
4705 	ipif_t  *ipif;
4706 	ill_t   *ill;
4707 	boolean_t ptp = B_FALSE;
4708 	ill_walk_context_t	ctx;
4709 
4710 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4711 	/*
4712 	 * Repeat twice, first based on local addresses and
4713 	 * next time for pointopoint.
4714 	 */
4715 repeat:
4716 	ill = ILL_START_WALK_V4(&ctx, ipst);
4717 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4718 		if (match_ill != NULL && ill != match_ill &&
4719 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4720 			continue;
4721 		}
4722 		mutex_enter(&ill->ill_lock);
4723 		for (ipif = ill->ill_ipif; ipif != NULL;
4724 		    ipif = ipif->ipif_next) {
4725 			/* Allow the ipif to be down */
4726 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4727 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4728 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4729 			    (ipif->ipif_pp_dst_addr == addr)) &&
4730 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4731 				zoneid = ipif->ipif_zoneid;
4732 				mutex_exit(&ill->ill_lock);
4733 				rw_exit(&ipst->ips_ill_g_lock);
4734 				/*
4735 				 * If ipif_zoneid was ALL_ZONES then we have
4736 				 * a trusted extensions shared IP address.
4737 				 * In that case GLOBAL_ZONEID works to send.
4738 				 */
4739 				if (zoneid == ALL_ZONES)
4740 					zoneid = GLOBAL_ZONEID;
4741 				return (zoneid);
4742 			}
4743 		}
4744 		mutex_exit(&ill->ill_lock);
4745 	}
4746 
4747 	/* If we already did the ptp case, then we are done */
4748 	if (ptp) {
4749 		rw_exit(&ipst->ips_ill_g_lock);
4750 		return (ALL_ZONES);
4751 	}
4752 	ptp = B_TRUE;
4753 	goto repeat;
4754 }
4755 
4756 /*
4757  * Look for an ipif that matches the specified remote address i.e. the
4758  * ipif that would receive the specified packet.
4759  * First look for directly connected interfaces and then do a recursive
4760  * IRE lookup and pick the first ipif corresponding to the source address in the
4761  * ire.
4762  * Returns: held ipif
4763  *
4764  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4765  */
4766 ipif_t *
4767 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4768 {
4769 	ipif_t	*ipif;
4770 
4771 	ASSERT(!ill->ill_isv6);
4772 
4773 	/*
4774 	 * Someone could be changing this ipif currently or change it
4775 	 * after we return this. Thus  a few packets could use the old
4776 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4777 	 * will atomically be updated or cleaned up with the new value
4778 	 * Thus we don't need a lock to check the flags or other attrs below.
4779 	 */
4780 	mutex_enter(&ill->ill_lock);
4781 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4782 		if (IPIF_IS_CONDEMNED(ipif))
4783 			continue;
4784 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4785 		    ipif->ipif_zoneid != ALL_ZONES)
4786 			continue;
4787 		/* Allow the ipif to be down */
4788 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4789 			if ((ipif->ipif_pp_dst_addr == addr) ||
4790 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4791 			    ipif->ipif_lcl_addr == addr)) {
4792 				ipif_refhold_locked(ipif);
4793 				mutex_exit(&ill->ill_lock);
4794 				return (ipif);
4795 			}
4796 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4797 			ipif_refhold_locked(ipif);
4798 			mutex_exit(&ill->ill_lock);
4799 			return (ipif);
4800 		}
4801 	}
4802 	mutex_exit(&ill->ill_lock);
4803 	/*
4804 	 * For a remote destination it isn't possible to nail down a particular
4805 	 * ipif.
4806 	 */
4807 
4808 	/* Pick the first interface */
4809 	ipif = ipif_get_next_ipif(NULL, ill);
4810 	return (ipif);
4811 }
4812 
4813 /*
4814  * This func does not prevent refcnt from increasing. But if
4815  * the caller has taken steps to that effect, then this func
4816  * can be used to determine whether the ill has become quiescent
4817  */
4818 static boolean_t
4819 ill_is_quiescent(ill_t *ill)
4820 {
4821 	ipif_t	*ipif;
4822 
4823 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4824 
4825 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4826 		if (ipif->ipif_refcnt != 0)
4827 			return (B_FALSE);
4828 	}
4829 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4830 		return (B_FALSE);
4831 	}
4832 	return (B_TRUE);
4833 }
4834 
4835 boolean_t
4836 ill_is_freeable(ill_t *ill)
4837 {
4838 	ipif_t	*ipif;
4839 
4840 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4841 
4842 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4843 		if (ipif->ipif_refcnt != 0) {
4844 			return (B_FALSE);
4845 		}
4846 	}
4847 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4848 		return (B_FALSE);
4849 	}
4850 	return (B_TRUE);
4851 }
4852 
4853 /*
4854  * This func does not prevent refcnt from increasing. But if
4855  * the caller has taken steps to that effect, then this func
4856  * can be used to determine whether the ipif has become quiescent
4857  */
4858 static boolean_t
4859 ipif_is_quiescent(ipif_t *ipif)
4860 {
4861 	ill_t *ill;
4862 
4863 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4864 
4865 	if (ipif->ipif_refcnt != 0)
4866 		return (B_FALSE);
4867 
4868 	ill = ipif->ipif_ill;
4869 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4870 	    ill->ill_logical_down) {
4871 		return (B_TRUE);
4872 	}
4873 
4874 	/* This is the last ipif going down or being deleted on this ill */
4875 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4876 		return (B_FALSE);
4877 	}
4878 
4879 	return (B_TRUE);
4880 }
4881 
4882 /*
4883  * return true if the ipif can be destroyed: the ipif has to be quiescent
4884  * with zero references from ire/ilm to it.
4885  */
4886 static boolean_t
4887 ipif_is_freeable(ipif_t *ipif)
4888 {
4889 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4890 	ASSERT(ipif->ipif_id != 0);
4891 	return (ipif->ipif_refcnt == 0);
4892 }
4893 
4894 /*
4895  * The ipif/ill/ire has been refreled. Do the tail processing.
4896  * Determine if the ipif or ill in question has become quiescent and if so
4897  * wakeup close and/or restart any queued pending ioctl that is waiting
4898  * for the ipif_down (or ill_down)
4899  */
4900 void
4901 ipif_ill_refrele_tail(ill_t *ill)
4902 {
4903 	mblk_t	*mp;
4904 	conn_t	*connp;
4905 	ipsq_t	*ipsq;
4906 	ipxop_t	*ipx;
4907 	ipif_t	*ipif;
4908 	dl_notify_ind_t *dlindp;
4909 
4910 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4911 
4912 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4913 		/* ip_modclose() may be waiting */
4914 		cv_broadcast(&ill->ill_cv);
4915 	}
4916 
4917 	ipsq = ill->ill_phyint->phyint_ipsq;
4918 	mutex_enter(&ipsq->ipsq_lock);
4919 	ipx = ipsq->ipsq_xop;
4920 	mutex_enter(&ipx->ipx_lock);
4921 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4922 		goto unlock;
4923 
4924 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4925 
4926 	ipif = ipx->ipx_pending_ipif;
4927 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4928 		goto unlock;
4929 
4930 	switch (ipx->ipx_waitfor) {
4931 	case IPIF_DOWN:
4932 		if (!ipif_is_quiescent(ipif))
4933 			goto unlock;
4934 		break;
4935 	case IPIF_FREE:
4936 		if (!ipif_is_freeable(ipif))
4937 			goto unlock;
4938 		break;
4939 	case ILL_DOWN:
4940 		if (!ill_is_quiescent(ill))
4941 			goto unlock;
4942 		break;
4943 	case ILL_FREE:
4944 		/*
4945 		 * ILL_FREE is only for loopback; normal ill teardown waits
4946 		 * synchronously in ip_modclose() without using ipx_waitfor,
4947 		 * handled by the cv_broadcast() at the top of this function.
4948 		 */
4949 		if (!ill_is_freeable(ill))
4950 			goto unlock;
4951 		break;
4952 	default:
4953 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4954 		    (void *)ipsq, ipx->ipx_waitfor);
4955 	}
4956 
4957 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4958 	mutex_exit(&ipx->ipx_lock);
4959 	mp = ipsq_pending_mp_get(ipsq, &connp);
4960 	mutex_exit(&ipsq->ipsq_lock);
4961 	mutex_exit(&ill->ill_lock);
4962 
4963 	ASSERT(mp != NULL);
4964 	/*
4965 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4966 	 * we can only get here when the current operation decides it
4967 	 * it needs to quiesce via ipsq_pending_mp_add().
4968 	 */
4969 	switch (mp->b_datap->db_type) {
4970 	case M_PCPROTO:
4971 	case M_PROTO:
4972 		/*
4973 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4974 		 */
4975 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4976 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4977 
4978 		switch (dlindp->dl_notification) {
4979 		case DL_NOTE_PHYS_ADDR:
4980 			qwriter_ip(ill, ill->ill_rq, mp,
4981 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4982 			return;
4983 		case DL_NOTE_REPLUMB:
4984 			qwriter_ip(ill, ill->ill_rq, mp,
4985 			    ill_replumb_tail, CUR_OP, B_TRUE);
4986 			return;
4987 		default:
4988 			ASSERT(0);
4989 			ill_refrele(ill);
4990 		}
4991 		break;
4992 
4993 	case M_ERROR:
4994 	case M_HANGUP:
4995 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4996 		    B_TRUE);
4997 		return;
4998 
4999 	case M_IOCTL:
5000 	case M_IOCDATA:
5001 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5002 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5003 		return;
5004 
5005 	default:
5006 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5007 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5008 	}
5009 	return;
5010 unlock:
5011 	mutex_exit(&ipsq->ipsq_lock);
5012 	mutex_exit(&ipx->ipx_lock);
5013 	mutex_exit(&ill->ill_lock);
5014 }
5015 
5016 #ifdef DEBUG
5017 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5018 static void
5019 th_trace_rrecord(th_trace_t *th_trace)
5020 {
5021 	tr_buf_t *tr_buf;
5022 	uint_t lastref;
5023 
5024 	lastref = th_trace->th_trace_lastref;
5025 	lastref++;
5026 	if (lastref == TR_BUF_MAX)
5027 		lastref = 0;
5028 	th_trace->th_trace_lastref = lastref;
5029 	tr_buf = &th_trace->th_trbuf[lastref];
5030 	tr_buf->tr_time = ddi_get_lbolt();
5031 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5032 }
5033 
5034 static void
5035 th_trace_free(void *value)
5036 {
5037 	th_trace_t *th_trace = value;
5038 
5039 	ASSERT(th_trace->th_refcnt == 0);
5040 	kmem_free(th_trace, sizeof (*th_trace));
5041 }
5042 
5043 /*
5044  * Find or create the per-thread hash table used to track object references.
5045  * The ipst argument is NULL if we shouldn't allocate.
5046  *
5047  * Accesses per-thread data, so there's no need to lock here.
5048  */
5049 static mod_hash_t *
5050 th_trace_gethash(ip_stack_t *ipst)
5051 {
5052 	th_hash_t *thh;
5053 
5054 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5055 		mod_hash_t *mh;
5056 		char name[256];
5057 		size_t objsize, rshift;
5058 		int retv;
5059 
5060 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5061 			return (NULL);
5062 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5063 		    (void *)curthread);
5064 
5065 		/*
5066 		 * We use mod_hash_create_extended here rather than the more
5067 		 * obvious mod_hash_create_ptrhash because the latter has a
5068 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5069 		 * block.
5070 		 */
5071 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5072 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5073 		rshift = highbit(objsize);
5074 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5075 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5076 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5077 		if (mh == NULL) {
5078 			kmem_free(thh, sizeof (*thh));
5079 			return (NULL);
5080 		}
5081 		thh->thh_hash = mh;
5082 		thh->thh_ipst = ipst;
5083 		/*
5084 		 * We trace ills, ipifs, ires, and nces.  All of these are
5085 		 * per-IP-stack, so the lock on the thread list is as well.
5086 		 */
5087 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5088 		list_insert_tail(&ip_thread_list, thh);
5089 		rw_exit(&ip_thread_rwlock);
5090 		retv = tsd_set(ip_thread_data, thh);
5091 		ASSERT(retv == 0);
5092 	}
5093 	return (thh != NULL ? thh->thh_hash : NULL);
5094 }
5095 
5096 boolean_t
5097 th_trace_ref(const void *obj, ip_stack_t *ipst)
5098 {
5099 	th_trace_t *th_trace;
5100 	mod_hash_t *mh;
5101 	mod_hash_val_t val;
5102 
5103 	if ((mh = th_trace_gethash(ipst)) == NULL)
5104 		return (B_FALSE);
5105 
5106 	/*
5107 	 * Attempt to locate the trace buffer for this obj and thread.
5108 	 * If it does not exist, then allocate a new trace buffer and
5109 	 * insert into the hash.
5110 	 */
5111 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5112 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5113 		if (th_trace == NULL)
5114 			return (B_FALSE);
5115 
5116 		th_trace->th_id = curthread;
5117 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5118 		    (mod_hash_val_t)th_trace) != 0) {
5119 			kmem_free(th_trace, sizeof (th_trace_t));
5120 			return (B_FALSE);
5121 		}
5122 	} else {
5123 		th_trace = (th_trace_t *)val;
5124 	}
5125 
5126 	ASSERT(th_trace->th_refcnt >= 0 &&
5127 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5128 
5129 	th_trace->th_refcnt++;
5130 	th_trace_rrecord(th_trace);
5131 	return (B_TRUE);
5132 }
5133 
5134 /*
5135  * For the purpose of tracing a reference release, we assume that global
5136  * tracing is always on and that the same thread initiated the reference hold
5137  * is releasing.
5138  */
5139 void
5140 th_trace_unref(const void *obj)
5141 {
5142 	int retv;
5143 	mod_hash_t *mh;
5144 	th_trace_t *th_trace;
5145 	mod_hash_val_t val;
5146 
5147 	mh = th_trace_gethash(NULL);
5148 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5149 	ASSERT(retv == 0);
5150 	th_trace = (th_trace_t *)val;
5151 
5152 	ASSERT(th_trace->th_refcnt > 0);
5153 	th_trace->th_refcnt--;
5154 	th_trace_rrecord(th_trace);
5155 }
5156 
5157 /*
5158  * If tracing has been disabled, then we assume that the reference counts are
5159  * now useless, and we clear them out before destroying the entries.
5160  */
5161 void
5162 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5163 {
5164 	th_hash_t	*thh;
5165 	mod_hash_t	*mh;
5166 	mod_hash_val_t	val;
5167 	th_trace_t	*th_trace;
5168 	int		retv;
5169 
5170 	rw_enter(&ip_thread_rwlock, RW_READER);
5171 	for (thh = list_head(&ip_thread_list); thh != NULL;
5172 	    thh = list_next(&ip_thread_list, thh)) {
5173 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5174 		    &val) == 0) {
5175 			th_trace = (th_trace_t *)val;
5176 			if (trace_disable)
5177 				th_trace->th_refcnt = 0;
5178 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5179 			ASSERT(retv == 0);
5180 		}
5181 	}
5182 	rw_exit(&ip_thread_rwlock);
5183 }
5184 
5185 void
5186 ipif_trace_ref(ipif_t *ipif)
5187 {
5188 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5189 
5190 	if (ipif->ipif_trace_disable)
5191 		return;
5192 
5193 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5194 		ipif->ipif_trace_disable = B_TRUE;
5195 		ipif_trace_cleanup(ipif);
5196 	}
5197 }
5198 
5199 void
5200 ipif_untrace_ref(ipif_t *ipif)
5201 {
5202 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5203 
5204 	if (!ipif->ipif_trace_disable)
5205 		th_trace_unref(ipif);
5206 }
5207 
5208 void
5209 ill_trace_ref(ill_t *ill)
5210 {
5211 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5212 
5213 	if (ill->ill_trace_disable)
5214 		return;
5215 
5216 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5217 		ill->ill_trace_disable = B_TRUE;
5218 		ill_trace_cleanup(ill);
5219 	}
5220 }
5221 
5222 void
5223 ill_untrace_ref(ill_t *ill)
5224 {
5225 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5226 
5227 	if (!ill->ill_trace_disable)
5228 		th_trace_unref(ill);
5229 }
5230 
5231 /*
5232  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5233  * failure, ipif_trace_disable is set.
5234  */
5235 static void
5236 ipif_trace_cleanup(const ipif_t *ipif)
5237 {
5238 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5239 }
5240 
5241 /*
5242  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5243  * failure, ill_trace_disable is set.
5244  */
5245 static void
5246 ill_trace_cleanup(const ill_t *ill)
5247 {
5248 	th_trace_cleanup(ill, ill->ill_trace_disable);
5249 }
5250 #endif /* DEBUG */
5251 
5252 void
5253 ipif_refhold_locked(ipif_t *ipif)
5254 {
5255 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5256 	ipif->ipif_refcnt++;
5257 	IPIF_TRACE_REF(ipif);
5258 }
5259 
5260 void
5261 ipif_refhold(ipif_t *ipif)
5262 {
5263 	ill_t	*ill;
5264 
5265 	ill = ipif->ipif_ill;
5266 	mutex_enter(&ill->ill_lock);
5267 	ipif->ipif_refcnt++;
5268 	IPIF_TRACE_REF(ipif);
5269 	mutex_exit(&ill->ill_lock);
5270 }
5271 
5272 /*
5273  * Must not be called while holding any locks. Otherwise if this is
5274  * the last reference to be released there is a chance of recursive mutex
5275  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5276  * to restart an ioctl.
5277  */
5278 void
5279 ipif_refrele(ipif_t *ipif)
5280 {
5281 	ill_t	*ill;
5282 
5283 	ill = ipif->ipif_ill;
5284 
5285 	mutex_enter(&ill->ill_lock);
5286 	ASSERT(ipif->ipif_refcnt != 0);
5287 	ipif->ipif_refcnt--;
5288 	IPIF_UNTRACE_REF(ipif);
5289 	if (ipif->ipif_refcnt != 0) {
5290 		mutex_exit(&ill->ill_lock);
5291 		return;
5292 	}
5293 
5294 	/* Drops the ill_lock */
5295 	ipif_ill_refrele_tail(ill);
5296 }
5297 
5298 ipif_t *
5299 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5300 {
5301 	ipif_t	*ipif;
5302 
5303 	mutex_enter(&ill->ill_lock);
5304 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5305 	    ipif != NULL; ipif = ipif->ipif_next) {
5306 		if (IPIF_IS_CONDEMNED(ipif))
5307 			continue;
5308 		ipif_refhold_locked(ipif);
5309 		mutex_exit(&ill->ill_lock);
5310 		return (ipif);
5311 	}
5312 	mutex_exit(&ill->ill_lock);
5313 	return (NULL);
5314 }
5315 
5316 /*
5317  * TODO: make this table extendible at run time
5318  * Return a pointer to the mac type info for 'mac_type'
5319  */
5320 static ip_m_t *
5321 ip_m_lookup(t_uscalar_t mac_type)
5322 {
5323 	ip_m_t	*ipm;
5324 
5325 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5326 		if (ipm->ip_m_mac_type == mac_type)
5327 			return (ipm);
5328 	return (NULL);
5329 }
5330 
5331 /*
5332  * Make a link layer address from the multicast IP address *addr.
5333  * To form the link layer address, invoke the ip_m_v*mapping function
5334  * associated with the link-layer type.
5335  */
5336 void
5337 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5338 {
5339 	ip_m_t *ipm;
5340 
5341 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5342 		return;
5343 
5344 	ASSERT(addr != NULL);
5345 
5346 	ipm = ip_m_lookup(ill->ill_mactype);
5347 	if (ipm == NULL ||
5348 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5349 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5350 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5351 		    ill->ill_name, ill->ill_mactype));
5352 		return;
5353 	}
5354 	if (ill->ill_isv6)
5355 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5356 	else
5357 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5358 }
5359 
5360 /*
5361  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5362  * Otherwise returns B_TRUE.
5363  *
5364  * The netmask can be verified to be contiguous with 32 shifts and or
5365  * operations. Take the contiguous mask (in host byte order) and compute
5366  * 	mask | mask << 1 | mask << 2 | ... | mask << 31
5367  * the result will be the same as the 'mask' for contiguous mask.
5368  */
5369 static boolean_t
5370 ip_contiguous_mask(uint32_t mask)
5371 {
5372 	uint32_t	m = mask;
5373 	int		i;
5374 
5375 	for (i = 1; i < 32; i++)
5376 		m |= (mask << i);
5377 
5378 	return (m == mask);
5379 }
5380 
5381 /*
5382  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5383  * ill is passed in to associate it with the correct interface.
5384  * If ire_arg is set, then we return the held IRE in that location.
5385  */
5386 int
5387 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5388     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5389     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5390 {
5391 	ire_t	*ire, *nire;
5392 	ire_t	*gw_ire = NULL;
5393 	ipif_t	*ipif = NULL;
5394 	uint_t	type;
5395 	int	match_flags = MATCH_IRE_TYPE;
5396 	tsol_gc_t *gc = NULL;
5397 	tsol_gcgrp_t *gcgrp = NULL;
5398 	boolean_t gcgrp_xtraref = B_FALSE;
5399 	boolean_t cgtp_broadcast;
5400 	boolean_t unbound = B_FALSE;
5401 
5402 	ip1dbg(("ip_rt_add:"));
5403 
5404 	if (ire_arg != NULL)
5405 		*ire_arg = NULL;
5406 
5407 	/* disallow non-contiguous netmasks */
5408 	if (!ip_contiguous_mask(ntohl(mask)))
5409 		return (ENOTSUP);
5410 
5411 	/*
5412 	 * If this is the case of RTF_HOST being set, then we set the netmask
5413 	 * to all ones (regardless if one was supplied).
5414 	 */
5415 	if (flags & RTF_HOST)
5416 		mask = IP_HOST_MASK;
5417 
5418 	/*
5419 	 * Prevent routes with a zero gateway from being created (since
5420 	 * interfaces can currently be plumbed and brought up no assigned
5421 	 * address).
5422 	 */
5423 	if (gw_addr == 0)
5424 		return (ENETUNREACH);
5425 	/*
5426 	 * Get the ipif, if any, corresponding to the gw_addr
5427 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5428 	 * we match on the gatway and destination to handle unnumbered pt-pt
5429 	 * interfaces.
5430 	 */
5431 	if (ill != NULL)
5432 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5433 	else
5434 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5435 	if (ipif != NULL) {
5436 		if (IS_VNI(ipif->ipif_ill)) {
5437 			ipif_refrele(ipif);
5438 			return (EINVAL);
5439 		}
5440 	}
5441 
5442 	/*
5443 	 * GateD will attempt to create routes with a loopback interface
5444 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5445 	 * these routes to be added, but create them as interface routes
5446 	 * since the gateway is an interface address.
5447 	 */
5448 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5449 		flags &= ~RTF_GATEWAY;
5450 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5451 		    mask == IP_HOST_MASK) {
5452 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5453 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5454 			    NULL);
5455 			if (ire != NULL) {
5456 				ire_refrele(ire);
5457 				ipif_refrele(ipif);
5458 				return (EEXIST);
5459 			}
5460 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5461 			    "for 0x%x\n", (void *)ipif,
5462 			    ipif->ipif_ire_type,
5463 			    ntohl(ipif->ipif_lcl_addr)));
5464 			ire = ire_create(
5465 			    (uchar_t *)&dst_addr,	/* dest address */
5466 			    (uchar_t *)&mask,		/* mask */
5467 			    NULL,			/* no gateway */
5468 			    ipif->ipif_ire_type,	/* LOOPBACK */
5469 			    ipif->ipif_ill,
5470 			    zoneid,
5471 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5472 			    NULL,
5473 			    ipst);
5474 
5475 			if (ire == NULL) {
5476 				ipif_refrele(ipif);
5477 				return (ENOMEM);
5478 			}
5479 			/* src address assigned by the caller? */
5480 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5481 				ire->ire_setsrc_addr = src_addr;
5482 
5483 			nire = ire_add(ire);
5484 			if (nire == NULL) {
5485 				/*
5486 				 * In the result of failure, ire_add() will have
5487 				 * already deleted the ire in question, so there
5488 				 * is no need to do that here.
5489 				 */
5490 				ipif_refrele(ipif);
5491 				return (ENOMEM);
5492 			}
5493 			/*
5494 			 * Check if it was a duplicate entry. This handles
5495 			 * the case of two racing route adds for the same route
5496 			 */
5497 			if (nire != ire) {
5498 				ASSERT(nire->ire_identical_ref > 1);
5499 				ire_delete(nire);
5500 				ire_refrele(nire);
5501 				ipif_refrele(ipif);
5502 				return (EEXIST);
5503 			}
5504 			ire = nire;
5505 			goto save_ire;
5506 		}
5507 	}
5508 
5509 	/*
5510 	 * The routes for multicast with CGTP are quite special in that
5511 	 * the gateway is the local interface address, yet RTF_GATEWAY
5512 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5513 	 * this undocumented and unusual use of multicast routes.
5514 	 */
5515 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5516 		flags &= ~RTF_GATEWAY;
5517 
5518 	/*
5519 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5520 	 * and the gateway address provided is one of the system's interface
5521 	 * addresses.  By using the routing socket interface and supplying an
5522 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5523 	 * specifying an interface route to be created is available which uses
5524 	 * the interface index that specifies the outgoing interface rather than
5525 	 * the address of an outgoing interface (which may not be able to
5526 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5527 	 * flag, routes can be specified which not only specify the next-hop to
5528 	 * be used when routing to a certain prefix, but also which outgoing
5529 	 * interface should be used.
5530 	 *
5531 	 * Previously, interfaces would have unique addresses assigned to them
5532 	 * and so the address assigned to a particular interface could be used
5533 	 * to identify a particular interface.  One exception to this was the
5534 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5535 	 *
5536 	 * With the advent of IPv6 and its link-local addresses, this
5537 	 * restriction was relaxed and interfaces could share addresses between
5538 	 * themselves.  In fact, typically all of the link-local interfaces on
5539 	 * an IPv6 node or router will have the same link-local address.  In
5540 	 * order to differentiate between these interfaces, the use of an
5541 	 * interface index is necessary and this index can be carried inside a
5542 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5543 	 * of using the interface index, however, is that all of the ipif's that
5544 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5545 	 * cannot be used to differentiate between ipif's (or logical
5546 	 * interfaces) that belong to the same ill (physical interface).
5547 	 *
5548 	 * For example, in the following case involving IPv4 interfaces and
5549 	 * logical interfaces
5550 	 *
5551 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5552 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5553 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5554 	 *
5555 	 * the ipif's corresponding to each of these interface routes can be
5556 	 * uniquely identified by the "gateway" (actually interface address).
5557 	 *
5558 	 * In this case involving multiple IPv6 default routes to a particular
5559 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5560 	 * default route is of interest:
5561 	 *
5562 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5563 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5564 	 */
5565 
5566 	/* RTF_GATEWAY not set */
5567 	if (!(flags & RTF_GATEWAY)) {
5568 		if (sp != NULL) {
5569 			ip2dbg(("ip_rt_add: gateway security attributes "
5570 			    "cannot be set with interface route\n"));
5571 			if (ipif != NULL)
5572 				ipif_refrele(ipif);
5573 			return (EINVAL);
5574 		}
5575 
5576 		/*
5577 		 * Whether or not ill (RTA_IFP) is set, we require that
5578 		 * the gateway is one of our local addresses.
5579 		 */
5580 		if (ipif == NULL)
5581 			return (ENETUNREACH);
5582 
5583 		/*
5584 		 * We use MATCH_IRE_ILL here. If the caller specified an
5585 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5586 		 * we use the ill derived from the gateway address.
5587 		 * We can always match the gateway address since we record it
5588 		 * in ire_gateway_addr.
5589 		 * We don't allow RTA_IFP to specify a different ill than the
5590 		 * one matching the ipif to make sure we can delete the route.
5591 		 */
5592 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5593 		if (ill == NULL) {
5594 			ill = ipif->ipif_ill;
5595 		} else if (ill != ipif->ipif_ill) {
5596 			ipif_refrele(ipif);
5597 			return (EINVAL);
5598 		}
5599 
5600 		/*
5601 		 * We check for an existing entry at this point.
5602 		 *
5603 		 * Since a netmask isn't passed in via the ioctl interface
5604 		 * (SIOCADDRT), we don't check for a matching netmask in that
5605 		 * case.
5606 		 */
5607 		if (!ioctl_msg)
5608 			match_flags |= MATCH_IRE_MASK;
5609 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5610 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5611 		    NULL);
5612 		if (ire != NULL) {
5613 			ire_refrele(ire);
5614 			ipif_refrele(ipif);
5615 			return (EEXIST);
5616 		}
5617 
5618 		/*
5619 		 * Some software (for example, GateD and Sun Cluster) attempts
5620 		 * to create (what amount to) IRE_PREFIX routes with the
5621 		 * loopback address as the gateway.  This is primarily done to
5622 		 * set up prefixes with the RTF_REJECT flag set (for example,
5623 		 * when generating aggregate routes.)
5624 		 *
5625 		 * If the IRE type (as defined by ill->ill_net_type) would be
5626 		 * IRE_LOOPBACK, then we map the request into a
5627 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5628 		 * these interface routes, by definition, can only be that.
5629 		 *
5630 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5631 		 * routine, but rather using ire_create() directly.
5632 		 *
5633 		 */
5634 		type = ill->ill_net_type;
5635 		if (type == IRE_LOOPBACK) {
5636 			type = IRE_IF_NORESOLVER;
5637 			flags |= RTF_BLACKHOLE;
5638 		}
5639 
5640 		/*
5641 		 * Create a copy of the IRE_IF_NORESOLVER or
5642 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5643 		 * gateway.
5644 		 */
5645 		ire = ire_create(
5646 		    (uchar_t *)&dst_addr,
5647 		    (uint8_t *)&mask,
5648 		    (uint8_t *)&gw_addr,
5649 		    type,
5650 		    ill,
5651 		    zoneid,
5652 		    flags,
5653 		    NULL,
5654 		    ipst);
5655 		if (ire == NULL) {
5656 			ipif_refrele(ipif);
5657 			return (ENOMEM);
5658 		}
5659 
5660 		/* src address assigned by the caller? */
5661 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5662 			ire->ire_setsrc_addr = src_addr;
5663 
5664 		nire = ire_add(ire);
5665 		if (nire == NULL) {
5666 			/*
5667 			 * In the result of failure, ire_add() will have
5668 			 * already deleted the ire in question, so there
5669 			 * is no need to do that here.
5670 			 */
5671 			ipif_refrele(ipif);
5672 			return (ENOMEM);
5673 		}
5674 		/*
5675 		 * Check if it was a duplicate entry. This handles
5676 		 * the case of two racing route adds for the same route
5677 		 */
5678 		if (nire != ire) {
5679 			ire_delete(nire);
5680 			ire_refrele(nire);
5681 			ipif_refrele(ipif);
5682 			return (EEXIST);
5683 		}
5684 		ire = nire;
5685 		goto save_ire;
5686 	}
5687 
5688 	/*
5689 	 * Get an interface IRE for the specified gateway.
5690 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5691 	 * gateway, it is currently unreachable and we fail the request
5692 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5693 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5694 	 * If RTA_IFP was specified we look on that particular ill.
5695 	 */
5696 	if (ill != NULL)
5697 		match_flags |= MATCH_IRE_ILL;
5698 
5699 	/* Check whether the gateway is reachable. */
5700 again:
5701 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5702 	if (flags & RTF_INDIRECT)
5703 		type |= IRE_OFFLINK;
5704 
5705 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5706 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5707 	if (gw_ire == NULL) {
5708 		/*
5709 		 * With IPMP, we allow host routes to influence in.mpathd's
5710 		 * target selection.  However, if the test addresses are on
5711 		 * their own network, the above lookup will fail since the
5712 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5713 		 * hidden test IREs to be found and try again.
5714 		 */
5715 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5716 			match_flags |= MATCH_IRE_TESTHIDDEN;
5717 			goto again;
5718 		}
5719 		if (ipif != NULL)
5720 			ipif_refrele(ipif);
5721 		return (ENETUNREACH);
5722 	}
5723 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5724 		ire_refrele(gw_ire);
5725 		if (ipif != NULL)
5726 			ipif_refrele(ipif);
5727 		return (ENETUNREACH);
5728 	}
5729 
5730 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5731 		unbound = B_TRUE;
5732 		if (ipst->ips_ip_strict_src_multihoming > 0)
5733 			ill = gw_ire->ire_ill;
5734 	}
5735 
5736 	/*
5737 	 * We create one of three types of IREs as a result of this request
5738 	 * based on the netmask.  A netmask of all ones (which is automatically
5739 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5740 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5741 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5742 	 * destination prefix.
5743 	 */
5744 	if (mask == IP_HOST_MASK)
5745 		type = IRE_HOST;
5746 	else if (mask == 0)
5747 		type = IRE_DEFAULT;
5748 	else
5749 		type = IRE_PREFIX;
5750 
5751 	/* check for a duplicate entry */
5752 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5753 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5754 	    0, ipst, NULL);
5755 	if (ire != NULL) {
5756 		if (ipif != NULL)
5757 			ipif_refrele(ipif);
5758 		ire_refrele(gw_ire);
5759 		ire_refrele(ire);
5760 		return (EEXIST);
5761 	}
5762 
5763 	/* Security attribute exists */
5764 	if (sp != NULL) {
5765 		tsol_gcgrp_addr_t ga;
5766 
5767 		/* find or create the gateway credentials group */
5768 		ga.ga_af = AF_INET;
5769 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5770 
5771 		/* we hold reference to it upon success */
5772 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5773 		if (gcgrp == NULL) {
5774 			if (ipif != NULL)
5775 				ipif_refrele(ipif);
5776 			ire_refrele(gw_ire);
5777 			return (ENOMEM);
5778 		}
5779 
5780 		/*
5781 		 * Create and add the security attribute to the group; a
5782 		 * reference to the group is made upon allocating a new
5783 		 * entry successfully.  If it finds an already-existing
5784 		 * entry for the security attribute in the group, it simply
5785 		 * returns it and no new reference is made to the group.
5786 		 */
5787 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5788 		if (gc == NULL) {
5789 			if (ipif != NULL)
5790 				ipif_refrele(ipif);
5791 			/* release reference held by gcgrp_lookup */
5792 			GCGRP_REFRELE(gcgrp);
5793 			ire_refrele(gw_ire);
5794 			return (ENOMEM);
5795 		}
5796 	}
5797 
5798 	/* Create the IRE. */
5799 	ire = ire_create(
5800 	    (uchar_t *)&dst_addr,		/* dest address */
5801 	    (uchar_t *)&mask,			/* mask */
5802 	    (uchar_t *)&gw_addr,		/* gateway address */
5803 	    (ushort_t)type,			/* IRE type */
5804 	    ill,
5805 	    zoneid,
5806 	    flags,
5807 	    gc,					/* security attribute */
5808 	    ipst);
5809 
5810 	/*
5811 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5812 	 * reference to the 'gcgrp'. We can now release the extra reference
5813 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5814 	 */
5815 	if (gcgrp_xtraref)
5816 		GCGRP_REFRELE(gcgrp);
5817 	if (ire == NULL) {
5818 		if (gc != NULL)
5819 			GC_REFRELE(gc);
5820 		if (ipif != NULL)
5821 			ipif_refrele(ipif);
5822 		ire_refrele(gw_ire);
5823 		return (ENOMEM);
5824 	}
5825 
5826 	/* Before we add, check if an extra CGTP broadcast is needed */
5827 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5828 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5829 
5830 	/* src address assigned by the caller? */
5831 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5832 		ire->ire_setsrc_addr = src_addr;
5833 
5834 	ire->ire_unbound = unbound;
5835 
5836 	/*
5837 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5838 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5839 	 */
5840 
5841 	/* Add the new IRE. */
5842 	nire = ire_add(ire);
5843 	if (nire == NULL) {
5844 		/*
5845 		 * In the result of failure, ire_add() will have
5846 		 * already deleted the ire in question, so there
5847 		 * is no need to do that here.
5848 		 */
5849 		if (ipif != NULL)
5850 			ipif_refrele(ipif);
5851 		ire_refrele(gw_ire);
5852 		return (ENOMEM);
5853 	}
5854 	/*
5855 	 * Check if it was a duplicate entry. This handles
5856 	 * the case of two racing route adds for the same route
5857 	 */
5858 	if (nire != ire) {
5859 		ire_delete(nire);
5860 		ire_refrele(nire);
5861 		if (ipif != NULL)
5862 			ipif_refrele(ipif);
5863 		ire_refrele(gw_ire);
5864 		return (EEXIST);
5865 	}
5866 	ire = nire;
5867 
5868 	if (flags & RTF_MULTIRT) {
5869 		/*
5870 		 * Invoke the CGTP (multirouting) filtering module
5871 		 * to add the dst address in the filtering database.
5872 		 * Replicated inbound packets coming from that address
5873 		 * will be filtered to discard the duplicates.
5874 		 * It is not necessary to call the CGTP filter hook
5875 		 * when the dst address is a broadcast or multicast,
5876 		 * because an IP source address cannot be a broadcast
5877 		 * or a multicast.
5878 		 */
5879 		if (cgtp_broadcast) {
5880 			ip_cgtp_bcast_add(ire, ipst);
5881 			goto save_ire;
5882 		}
5883 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5884 		    !CLASSD(ire->ire_addr)) {
5885 			int res;
5886 			ipif_t *src_ipif;
5887 
5888 			/* Find the source address corresponding to gw_ire */
5889 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5890 			    NULL, zoneid, ipst);
5891 			if (src_ipif != NULL) {
5892 				res = ipst->ips_ip_cgtp_filter_ops->
5893 				    cfo_add_dest_v4(
5894 				    ipst->ips_netstack->netstack_stackid,
5895 				    ire->ire_addr,
5896 				    ire->ire_gateway_addr,
5897 				    ire->ire_setsrc_addr,
5898 				    src_ipif->ipif_lcl_addr);
5899 				ipif_refrele(src_ipif);
5900 			} else {
5901 				res = EADDRNOTAVAIL;
5902 			}
5903 			if (res != 0) {
5904 				if (ipif != NULL)
5905 					ipif_refrele(ipif);
5906 				ire_refrele(gw_ire);
5907 				ire_delete(ire);
5908 				ire_refrele(ire);	/* Held in ire_add */
5909 				return (res);
5910 			}
5911 		}
5912 	}
5913 
5914 save_ire:
5915 	if (gw_ire != NULL) {
5916 		ire_refrele(gw_ire);
5917 		gw_ire = NULL;
5918 	}
5919 	if (ill != NULL) {
5920 		/*
5921 		 * Save enough information so that we can recreate the IRE if
5922 		 * the interface goes down and then up.  The metrics associated
5923 		 * with the route will be saved as well when rts_setmetrics() is
5924 		 * called after the IRE has been created.  In the case where
5925 		 * memory cannot be allocated, none of this information will be
5926 		 * saved.
5927 		 */
5928 		ill_save_ire(ill, ire);
5929 	}
5930 	if (ioctl_msg)
5931 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5932 	if (ire_arg != NULL) {
5933 		/*
5934 		 * Store the ire that was successfully added into where ire_arg
5935 		 * points to so that callers don't have to look it up
5936 		 * themselves (but they are responsible for ire_refrele()ing
5937 		 * the ire when they are finished with it).
5938 		 */
5939 		*ire_arg = ire;
5940 	} else {
5941 		ire_refrele(ire);		/* Held in ire_add */
5942 	}
5943 	if (ipif != NULL)
5944 		ipif_refrele(ipif);
5945 	return (0);
5946 }
5947 
5948 /*
5949  * ip_rt_delete is called to delete an IPv4 route.
5950  * ill is passed in to associate it with the correct interface.
5951  */
5952 /* ARGSUSED4 */
5953 int
5954 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5955     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5956     ip_stack_t *ipst, zoneid_t zoneid)
5957 {
5958 	ire_t	*ire = NULL;
5959 	ipif_t	*ipif;
5960 	uint_t	type;
5961 	uint_t	match_flags = MATCH_IRE_TYPE;
5962 	int	err = 0;
5963 
5964 	ip1dbg(("ip_rt_delete:"));
5965 	/*
5966 	 * If this is the case of RTF_HOST being set, then we set the netmask
5967 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5968 	 */
5969 	if (flags & RTF_HOST) {
5970 		mask = IP_HOST_MASK;
5971 		match_flags |= MATCH_IRE_MASK;
5972 	} else if (rtm_addrs & RTA_NETMASK) {
5973 		match_flags |= MATCH_IRE_MASK;
5974 	}
5975 
5976 	/*
5977 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5978 	 * we check if the gateway address is one of our interfaces first,
5979 	 * and fall back on RTF_GATEWAY routes.
5980 	 *
5981 	 * This makes it possible to delete an original
5982 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5983 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5984 	 * and those can not be deleted here.
5985 	 *
5986 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5987 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5988 	 * otherwise we use the ill derived from the gateway address.
5989 	 * We can always match the gateway address since we record it
5990 	 * in ire_gateway_addr.
5991 	 *
5992 	 * For more detail on specifying routes by gateway address and by
5993 	 * interface index, see the comments in ip_rt_add().
5994 	 */
5995 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5996 	if (ipif != NULL) {
5997 		ill_t	*ill_match;
5998 
5999 		if (ill != NULL)
6000 			ill_match = ill;
6001 		else
6002 			ill_match = ipif->ipif_ill;
6003 
6004 		match_flags |= MATCH_IRE_ILL;
6005 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6006 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6007 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6008 			    match_flags, 0, ipst, NULL);
6009 		}
6010 		if (ire == NULL) {
6011 			match_flags |= MATCH_IRE_GW;
6012 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6013 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6014 			    match_flags, 0, ipst, NULL);
6015 		}
6016 		/* Avoid deleting routes created by kernel from an ipif */
6017 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6018 			ire_refrele(ire);
6019 			ire = NULL;
6020 		}
6021 
6022 		/* Restore in case we didn't find a match */
6023 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6024 	}
6025 
6026 	if (ire == NULL) {
6027 		/*
6028 		 * At this point, the gateway address is not one of our own
6029 		 * addresses or a matching interface route was not found.  We
6030 		 * set the IRE type to lookup based on whether
6031 		 * this is a host route, a default route or just a prefix.
6032 		 *
6033 		 * If an ill was passed in, then the lookup is based on an
6034 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6035 		 */
6036 		match_flags |= MATCH_IRE_GW;
6037 		if (ill != NULL)
6038 			match_flags |= MATCH_IRE_ILL;
6039 		if (mask == IP_HOST_MASK)
6040 			type = IRE_HOST;
6041 		else if (mask == 0)
6042 			type = IRE_DEFAULT;
6043 		else
6044 			type = IRE_PREFIX;
6045 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6046 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6047 	}
6048 
6049 	if (ipif != NULL) {
6050 		ipif_refrele(ipif);
6051 		ipif = NULL;
6052 	}
6053 
6054 	if (ire == NULL)
6055 		return (ESRCH);
6056 
6057 	if (ire->ire_flags & RTF_MULTIRT) {
6058 		/*
6059 		 * Invoke the CGTP (multirouting) filtering module
6060 		 * to remove the dst address from the filtering database.
6061 		 * Packets coming from that address will no longer be
6062 		 * filtered to remove duplicates.
6063 		 */
6064 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6065 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6066 			    ipst->ips_netstack->netstack_stackid,
6067 			    ire->ire_addr, ire->ire_gateway_addr);
6068 		}
6069 		ip_cgtp_bcast_delete(ire, ipst);
6070 	}
6071 
6072 	ill = ire->ire_ill;
6073 	if (ill != NULL)
6074 		ill_remove_saved_ire(ill, ire);
6075 	if (ioctl_msg)
6076 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6077 	ire_delete(ire);
6078 	ire_refrele(ire);
6079 	return (err);
6080 }
6081 
6082 /*
6083  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6084  */
6085 /* ARGSUSED */
6086 int
6087 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6088     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6089 {
6090 	ipaddr_t dst_addr;
6091 	ipaddr_t gw_addr;
6092 	ipaddr_t mask;
6093 	int error = 0;
6094 	mblk_t *mp1;
6095 	struct rtentry *rt;
6096 	ipif_t *ipif = NULL;
6097 	ip_stack_t	*ipst;
6098 
6099 	ASSERT(q->q_next == NULL);
6100 	ipst = CONNQ_TO_IPST(q);
6101 
6102 	ip1dbg(("ip_siocaddrt:"));
6103 	/* Existence of mp1 verified in ip_wput_nondata */
6104 	mp1 = mp->b_cont->b_cont;
6105 	rt = (struct rtentry *)mp1->b_rptr;
6106 
6107 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6108 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6109 
6110 	/*
6111 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6112 	 * to a particular host address.  In this case, we set the netmask to
6113 	 * all ones for the particular destination address.  Otherwise,
6114 	 * determine the netmask to be used based on dst_addr and the interfaces
6115 	 * in use.
6116 	 */
6117 	if (rt->rt_flags & RTF_HOST) {
6118 		mask = IP_HOST_MASK;
6119 	} else {
6120 		/*
6121 		 * Note that ip_subnet_mask returns a zero mask in the case of
6122 		 * default (an all-zeroes address).
6123 		 */
6124 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6125 	}
6126 
6127 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6128 	    B_TRUE, NULL, ipst, ALL_ZONES);
6129 	if (ipif != NULL)
6130 		ipif_refrele(ipif);
6131 	return (error);
6132 }
6133 
6134 /*
6135  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6136  */
6137 /* ARGSUSED */
6138 int
6139 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6140     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6141 {
6142 	ipaddr_t dst_addr;
6143 	ipaddr_t gw_addr;
6144 	ipaddr_t mask;
6145 	int error;
6146 	mblk_t *mp1;
6147 	struct rtentry *rt;
6148 	ipif_t *ipif = NULL;
6149 	ip_stack_t	*ipst;
6150 
6151 	ASSERT(q->q_next == NULL);
6152 	ipst = CONNQ_TO_IPST(q);
6153 
6154 	ip1dbg(("ip_siocdelrt:"));
6155 	/* Existence of mp1 verified in ip_wput_nondata */
6156 	mp1 = mp->b_cont->b_cont;
6157 	rt = (struct rtentry *)mp1->b_rptr;
6158 
6159 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6160 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6161 
6162 	/*
6163 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6164 	 * to a particular host address.  In this case, we set the netmask to
6165 	 * all ones for the particular destination address.  Otherwise,
6166 	 * determine the netmask to be used based on dst_addr and the interfaces
6167 	 * in use.
6168 	 */
6169 	if (rt->rt_flags & RTF_HOST) {
6170 		mask = IP_HOST_MASK;
6171 	} else {
6172 		/*
6173 		 * Note that ip_subnet_mask returns a zero mask in the case of
6174 		 * default (an all-zeroes address).
6175 		 */
6176 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6177 	}
6178 
6179 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6180 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6181 	    ipst, ALL_ZONES);
6182 	if (ipif != NULL)
6183 		ipif_refrele(ipif);
6184 	return (error);
6185 }
6186 
6187 /*
6188  * Enqueue the mp onto the ipsq, chained by b_next.
6189  * b_prev stores the function to be executed later, and b_queue the queue
6190  * where this mp originated.
6191  */
6192 void
6193 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6194     ill_t *pending_ill)
6195 {
6196 	conn_t	*connp;
6197 	ipxop_t *ipx = ipsq->ipsq_xop;
6198 
6199 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6200 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6201 	ASSERT(func != NULL);
6202 
6203 	mp->b_queue = q;
6204 	mp->b_prev = (void *)func;
6205 	mp->b_next = NULL;
6206 
6207 	switch (type) {
6208 	case CUR_OP:
6209 		if (ipx->ipx_mptail != NULL) {
6210 			ASSERT(ipx->ipx_mphead != NULL);
6211 			ipx->ipx_mptail->b_next = mp;
6212 		} else {
6213 			ASSERT(ipx->ipx_mphead == NULL);
6214 			ipx->ipx_mphead = mp;
6215 		}
6216 		ipx->ipx_mptail = mp;
6217 		break;
6218 
6219 	case NEW_OP:
6220 		if (ipsq->ipsq_xopq_mptail != NULL) {
6221 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6222 			ipsq->ipsq_xopq_mptail->b_next = mp;
6223 		} else {
6224 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6225 			ipsq->ipsq_xopq_mphead = mp;
6226 		}
6227 		ipsq->ipsq_xopq_mptail = mp;
6228 		ipx->ipx_ipsq_queued = B_TRUE;
6229 		break;
6230 
6231 	case SWITCH_OP:
6232 		ASSERT(ipsq->ipsq_swxop != NULL);
6233 		/* only one switch operation is currently allowed */
6234 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6235 		ipsq->ipsq_switch_mp = mp;
6236 		ipx->ipx_ipsq_queued = B_TRUE;
6237 		break;
6238 	default:
6239 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6240 	}
6241 
6242 	if (CONN_Q(q) && pending_ill != NULL) {
6243 		connp = Q_TO_CONN(q);
6244 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6245 		connp->conn_oper_pending_ill = pending_ill;
6246 	}
6247 }
6248 
6249 /*
6250  * Dequeue the next message that requested exclusive access to this IPSQ's
6251  * xop.  Specifically:
6252  *
6253  *  1. If we're still processing the current operation on `ipsq', then
6254  *     dequeue the next message for the operation (from ipx_mphead), or
6255  *     return NULL if there are no queued messages for the operation.
6256  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6257  *
6258  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6259  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6260  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6261  *     leaving IPMP groups and require a careful dance -- see the comments
6262  *     in-line below for details.  If we're leaving a group xop or if we're
6263  *     joining a group xop and become writer on it, then we proceed to (3).
6264  *     Otherwise, we return NULL and exit the xop.
6265  *
6266  *  3. For each IPSQ in the xop, return any switch operation stored on
6267  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6268  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6269  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6270  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6271  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6272  *     each phyint in the group, including the IPMP meta-interface phyint.
6273  */
6274 static mblk_t *
6275 ipsq_dq(ipsq_t *ipsq)
6276 {
6277 	ill_t	*illv4, *illv6;
6278 	mblk_t	*mp;
6279 	ipsq_t	*xopipsq;
6280 	ipsq_t	*leftipsq = NULL;
6281 	ipxop_t *ipx;
6282 	phyint_t *phyi = ipsq->ipsq_phyint;
6283 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6284 	boolean_t emptied = B_FALSE;
6285 
6286 	/*
6287 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6288 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6289 	 */
6290 	rw_enter(&ipst->ips_ill_g_lock,
6291 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6292 	mutex_enter(&ipsq->ipsq_lock);
6293 	ipx = ipsq->ipsq_xop;
6294 	mutex_enter(&ipx->ipx_lock);
6295 
6296 	/*
6297 	 * Dequeue the next message associated with the current exclusive
6298 	 * operation, if any.
6299 	 */
6300 	if ((mp = ipx->ipx_mphead) != NULL) {
6301 		ipx->ipx_mphead = mp->b_next;
6302 		if (ipx->ipx_mphead == NULL)
6303 			ipx->ipx_mptail = NULL;
6304 		mp->b_next = (void *)ipsq;
6305 		goto out;
6306 	}
6307 
6308 	if (ipx->ipx_current_ipif != NULL)
6309 		goto empty;
6310 
6311 	if (ipsq->ipsq_swxop != NULL) {
6312 		/*
6313 		 * The exclusive operation that is now being completed has
6314 		 * requested a switch to a different xop.  This happens
6315 		 * when an interface joins or leaves an IPMP group.  Joins
6316 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6317 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6318 		 * (phyint_free()), or interface plumb for an ill type
6319 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6320 		 *
6321 		 * Xop switches are not allowed on the IPMP meta-interface.
6322 		 */
6323 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6324 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6325 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6326 
6327 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6328 			/*
6329 			 * We're switching back to our own xop, so we have two
6330 			 * xop's to drain/exit: our own, and the group xop
6331 			 * that we are leaving.
6332 			 *
6333 			 * First, pull ourselves out of the group ipsq list.
6334 			 * This is safe since we're writer on ill_g_lock.
6335 			 */
6336 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6337 
6338 			xopipsq = ipx->ipx_ipsq;
6339 			while (xopipsq->ipsq_next != ipsq)
6340 				xopipsq = xopipsq->ipsq_next;
6341 
6342 			xopipsq->ipsq_next = ipsq->ipsq_next;
6343 			ipsq->ipsq_next = ipsq;
6344 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6345 			ipsq->ipsq_swxop = NULL;
6346 
6347 			/*
6348 			 * Second, prepare to exit the group xop.  The actual
6349 			 * ipsq_exit() is done at the end of this function
6350 			 * since we cannot hold any locks across ipsq_exit().
6351 			 * Note that although we drop the group's ipx_lock, no
6352 			 * threads can proceed since we're still ipx_writer.
6353 			 */
6354 			leftipsq = xopipsq;
6355 			mutex_exit(&ipx->ipx_lock);
6356 
6357 			/*
6358 			 * Third, set ipx to point to our own xop (which was
6359 			 * inactive and therefore can be entered).
6360 			 */
6361 			ipx = ipsq->ipsq_xop;
6362 			mutex_enter(&ipx->ipx_lock);
6363 			ASSERT(ipx->ipx_writer == NULL);
6364 			ASSERT(ipx->ipx_current_ipif == NULL);
6365 		} else {
6366 			/*
6367 			 * We're switching from our own xop to a group xop.
6368 			 * The requestor of the switch must ensure that the
6369 			 * group xop cannot go away (e.g. by ensuring the
6370 			 * phyint associated with the xop cannot go away).
6371 			 *
6372 			 * If we can become writer on our new xop, then we'll
6373 			 * do the drain.  Otherwise, the current writer of our
6374 			 * new xop will do the drain when it exits.
6375 			 *
6376 			 * First, splice ourselves into the group IPSQ list.
6377 			 * This is safe since we're writer on ill_g_lock.
6378 			 */
6379 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6380 
6381 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6382 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6383 				xopipsq = xopipsq->ipsq_next;
6384 
6385 			xopipsq->ipsq_next = ipsq;
6386 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6387 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6388 			ipsq->ipsq_swxop = NULL;
6389 
6390 			/*
6391 			 * Second, exit our own xop, since it's now unused.
6392 			 * This is safe since we've got the only reference.
6393 			 */
6394 			ASSERT(ipx->ipx_writer == curthread);
6395 			ipx->ipx_writer = NULL;
6396 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6397 			ipx->ipx_ipsq_queued = B_FALSE;
6398 			mutex_exit(&ipx->ipx_lock);
6399 
6400 			/*
6401 			 * Third, set ipx to point to our new xop, and check
6402 			 * if we can become writer on it.  If we cannot, then
6403 			 * the current writer will drain the IPSQ group when
6404 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6405 			 * because we're still holding ipsq_lock.
6406 			 */
6407 			ipx = ipsq->ipsq_xop;
6408 			mutex_enter(&ipx->ipx_lock);
6409 			if (ipx->ipx_writer != NULL ||
6410 			    ipx->ipx_current_ipif != NULL) {
6411 				goto out;
6412 			}
6413 		}
6414 
6415 		/*
6416 		 * Fourth, become writer on our new ipx before we continue
6417 		 * with the drain.  Note that we never dropped ipsq_lock
6418 		 * above, so no other thread could've raced with us to
6419 		 * become writer first.  Also, we're holding ipx_lock, so
6420 		 * no other thread can examine the ipx right now.
6421 		 */
6422 		ASSERT(ipx->ipx_current_ipif == NULL);
6423 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6424 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6425 		ipx->ipx_writer = curthread;
6426 		ipx->ipx_forced = B_FALSE;
6427 #ifdef DEBUG
6428 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6429 #endif
6430 	}
6431 
6432 	xopipsq = ipsq;
6433 	do {
6434 		/*
6435 		 * So that other operations operate on a consistent and
6436 		 * complete phyint, a switch message on an IPSQ must be
6437 		 * handled prior to any other operations on that IPSQ.
6438 		 */
6439 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6440 			xopipsq->ipsq_switch_mp = NULL;
6441 			ASSERT(mp->b_next == NULL);
6442 			mp->b_next = (void *)xopipsq;
6443 			goto out;
6444 		}
6445 
6446 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6447 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6448 			if (xopipsq->ipsq_xopq_mphead == NULL)
6449 				xopipsq->ipsq_xopq_mptail = NULL;
6450 			mp->b_next = (void *)xopipsq;
6451 			goto out;
6452 		}
6453 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6454 empty:
6455 	/*
6456 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6457 	 * new messages can end up on any IPSQ in the xop.
6458 	 */
6459 	ipx->ipx_writer = NULL;
6460 	ipx->ipx_forced = B_FALSE;
6461 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6462 	ipx->ipx_ipsq_queued = B_FALSE;
6463 	emptied = B_TRUE;
6464 #ifdef	DEBUG
6465 	ipx->ipx_depth = 0;
6466 #endif
6467 out:
6468 	mutex_exit(&ipx->ipx_lock);
6469 	mutex_exit(&ipsq->ipsq_lock);
6470 
6471 	/*
6472 	 * If we completely emptied the xop, then wake up any threads waiting
6473 	 * to enter any of the IPSQ's associated with it.
6474 	 */
6475 	if (emptied) {
6476 		xopipsq = ipsq;
6477 		do {
6478 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6479 				continue;
6480 
6481 			illv4 = phyi->phyint_illv4;
6482 			illv6 = phyi->phyint_illv6;
6483 
6484 			GRAB_ILL_LOCKS(illv4, illv6);
6485 			if (illv4 != NULL)
6486 				cv_broadcast(&illv4->ill_cv);
6487 			if (illv6 != NULL)
6488 				cv_broadcast(&illv6->ill_cv);
6489 			RELEASE_ILL_LOCKS(illv4, illv6);
6490 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6491 	}
6492 	rw_exit(&ipst->ips_ill_g_lock);
6493 
6494 	/*
6495 	 * Now that all locks are dropped, exit the IPSQ we left.
6496 	 */
6497 	if (leftipsq != NULL)
6498 		ipsq_exit(leftipsq);
6499 
6500 	return (mp);
6501 }
6502 
6503 /*
6504  * Return completion status of previously initiated DLPI operations on
6505  * ills in the purview of an ipsq.
6506  */
6507 static boolean_t
6508 ipsq_dlpi_done(ipsq_t *ipsq)
6509 {
6510 	ipsq_t		*ipsq_start;
6511 	phyint_t	*phyi;
6512 	ill_t		*ill;
6513 
6514 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6515 	ipsq_start = ipsq;
6516 
6517 	do {
6518 		/*
6519 		 * The only current users of this function are ipsq_try_enter
6520 		 * and ipsq_enter which have made sure that ipsq_writer is
6521 		 * NULL before we reach here. ill_dlpi_pending is modified
6522 		 * only by an ipsq writer
6523 		 */
6524 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6525 		phyi = ipsq->ipsq_phyint;
6526 		/*
6527 		 * phyi could be NULL if a phyint that is part of an
6528 		 * IPMP group is being unplumbed. A more detailed
6529 		 * comment is in ipmp_grp_update_kstats()
6530 		 */
6531 		if (phyi != NULL) {
6532 			ill = phyi->phyint_illv4;
6533 			if (ill != NULL &&
6534 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6535 			    ill->ill_arl_dlpi_pending))
6536 				return (B_FALSE);
6537 
6538 			ill = phyi->phyint_illv6;
6539 			if (ill != NULL &&
6540 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6541 				return (B_FALSE);
6542 		}
6543 
6544 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6545 
6546 	return (B_TRUE);
6547 }
6548 
6549 /*
6550  * Enter the ipsq corresponding to ill, by waiting synchronously till
6551  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6552  * will have to drain completely before ipsq_enter returns success.
6553  * ipx_current_ipif will be set if some exclusive op is in progress,
6554  * and the ipsq_exit logic will start the next enqueued op after
6555  * completion of the current op. If 'force' is used, we don't wait
6556  * for the enqueued ops. This is needed when a conn_close wants to
6557  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6558  * of an ill can also use this option. But we dont' use it currently.
6559  */
6560 #define	ENTER_SQ_WAIT_TICKS 100
6561 boolean_t
6562 ipsq_enter(ill_t *ill, boolean_t force, int type)
6563 {
6564 	ipsq_t	*ipsq;
6565 	ipxop_t *ipx;
6566 	boolean_t waited_enough = B_FALSE;
6567 	ip_stack_t *ipst = ill->ill_ipst;
6568 
6569 	/*
6570 	 * Note that the relationship between ill and ipsq is fixed as long as
6571 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6572 	 * relationship between the IPSQ and xop cannot change.  However,
6573 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6574 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6575 	 * waking up all ills in the xop when it becomes available.
6576 	 */
6577 	for (;;) {
6578 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6579 		mutex_enter(&ill->ill_lock);
6580 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6581 			mutex_exit(&ill->ill_lock);
6582 			rw_exit(&ipst->ips_ill_g_lock);
6583 			return (B_FALSE);
6584 		}
6585 
6586 		ipsq = ill->ill_phyint->phyint_ipsq;
6587 		mutex_enter(&ipsq->ipsq_lock);
6588 		ipx = ipsq->ipsq_xop;
6589 		mutex_enter(&ipx->ipx_lock);
6590 
6591 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6592 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6593 		    waited_enough))
6594 			break;
6595 
6596 		rw_exit(&ipst->ips_ill_g_lock);
6597 
6598 		if (!force || ipx->ipx_writer != NULL) {
6599 			mutex_exit(&ipx->ipx_lock);
6600 			mutex_exit(&ipsq->ipsq_lock);
6601 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6602 		} else {
6603 			mutex_exit(&ipx->ipx_lock);
6604 			mutex_exit(&ipsq->ipsq_lock);
6605 			(void) cv_reltimedwait(&ill->ill_cv,
6606 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6607 			waited_enough = B_TRUE;
6608 		}
6609 		mutex_exit(&ill->ill_lock);
6610 	}
6611 
6612 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6613 	ASSERT(ipx->ipx_reentry_cnt == 0);
6614 	ipx->ipx_writer = curthread;
6615 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6616 	ipx->ipx_reentry_cnt++;
6617 #ifdef DEBUG
6618 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6619 #endif
6620 	mutex_exit(&ipx->ipx_lock);
6621 	mutex_exit(&ipsq->ipsq_lock);
6622 	mutex_exit(&ill->ill_lock);
6623 	rw_exit(&ipst->ips_ill_g_lock);
6624 
6625 	return (B_TRUE);
6626 }
6627 
6628 /*
6629  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6630  * across the call to the core interface ipsq_try_enter() and hence calls this
6631  * function directly. This is explained more fully in ipif_set_values().
6632  * In order to support the above constraint, ipsq_try_enter is implemented as
6633  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6634  */
6635 static ipsq_t *
6636 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6637     int type, boolean_t reentry_ok)
6638 {
6639 	ipsq_t	*ipsq;
6640 	ipxop_t	*ipx;
6641 	ip_stack_t *ipst = ill->ill_ipst;
6642 
6643 	/*
6644 	 * lock ordering:
6645 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6646 	 *
6647 	 * ipx of an ipsq can't change when ipsq_lock is held.
6648 	 */
6649 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6650 	GRAB_CONN_LOCK(q);
6651 	mutex_enter(&ill->ill_lock);
6652 	ipsq = ill->ill_phyint->phyint_ipsq;
6653 	mutex_enter(&ipsq->ipsq_lock);
6654 	ipx = ipsq->ipsq_xop;
6655 	mutex_enter(&ipx->ipx_lock);
6656 
6657 	/*
6658 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6659 	 *    (Note: If the caller does not specify reentry_ok then neither
6660 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6661 	 *    again. Otherwise it can lead to an infinite loop
6662 	 * 2. Enter the ipsq if there is no current writer and this attempted
6663 	 *    entry is part of the current operation
6664 	 * 3. Enter the ipsq if there is no current writer and this is a new
6665 	 *    operation and the operation queue is empty and there is no
6666 	 *    operation currently in progress and if all previously initiated
6667 	 *    DLPI operations have completed.
6668 	 */
6669 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6670 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6671 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6672 	    ipsq_dlpi_done(ipsq))))) {
6673 		/* Success. */
6674 		ipx->ipx_reentry_cnt++;
6675 		ipx->ipx_writer = curthread;
6676 		ipx->ipx_forced = B_FALSE;
6677 		mutex_exit(&ipx->ipx_lock);
6678 		mutex_exit(&ipsq->ipsq_lock);
6679 		mutex_exit(&ill->ill_lock);
6680 		RELEASE_CONN_LOCK(q);
6681 #ifdef DEBUG
6682 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6683 #endif
6684 		return (ipsq);
6685 	}
6686 
6687 	if (func != NULL)
6688 		ipsq_enq(ipsq, q, mp, func, type, ill);
6689 
6690 	mutex_exit(&ipx->ipx_lock);
6691 	mutex_exit(&ipsq->ipsq_lock);
6692 	mutex_exit(&ill->ill_lock);
6693 	RELEASE_CONN_LOCK(q);
6694 	return (NULL);
6695 }
6696 
6697 /*
6698  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6699  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6700  * There is one ipsq per phyint. The ipsq
6701  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6702  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6703  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6704  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6705  * up the interface) and are enqueued in ipx_mphead.
6706  *
6707  * If a thread does not want to reenter the ipsq when it is already writer,
6708  * it must make sure that the specified reentry point to be called later
6709  * when the ipsq is empty, nor any code path starting from the specified reentry
6710  * point must never ever try to enter the ipsq again. Otherwise it can lead
6711  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6712  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6713  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6714  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6715  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6716  * ioctl if the current ioctl has completed. If the current ioctl is still
6717  * in progress it simply returns. The current ioctl could be waiting for
6718  * a response from another module (the driver or could be waiting for
6719  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6720  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6721  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6722  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6723  * all associated DLPI operations have completed.
6724  */
6725 
6726 /*
6727  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6728  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6729  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6730  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6731  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6732  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6733  */
6734 ipsq_t *
6735 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6736     ipsq_func_t func, int type, boolean_t reentry_ok)
6737 {
6738 	ip_stack_t	*ipst;
6739 	ipsq_t		*ipsq;
6740 
6741 	/* Only 1 of ipif or ill can be specified */
6742 	ASSERT((ipif != NULL) ^ (ill != NULL));
6743 
6744 	if (ipif != NULL)
6745 		ill = ipif->ipif_ill;
6746 	ipst = ill->ill_ipst;
6747 
6748 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6749 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6750 	rw_exit(&ipst->ips_ill_g_lock);
6751 
6752 	return (ipsq);
6753 }
6754 
6755 /*
6756  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6757  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6758  * cannot be entered, the mp is queued for completion.
6759  */
6760 void
6761 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6762     boolean_t reentry_ok)
6763 {
6764 	ipsq_t	*ipsq;
6765 
6766 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6767 
6768 	/*
6769 	 * Drop the caller's refhold on the ill.  This is safe since we either
6770 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6771 	 * IPSQ, in which case we return without accessing ill anymore.  This
6772 	 * is needed because func needs to see the correct refcount.
6773 	 * e.g. removeif can work only then.
6774 	 */
6775 	ill_refrele(ill);
6776 	if (ipsq != NULL) {
6777 		(*func)(ipsq, q, mp, NULL);
6778 		ipsq_exit(ipsq);
6779 	}
6780 }
6781 
6782 /*
6783  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6784  * prior to exiting.  Caller must be writer on the specified IPSQ.
6785  */
6786 void
6787 ipsq_exit(ipsq_t *ipsq)
6788 {
6789 	mblk_t *mp;
6790 	ipsq_t *mp_ipsq;
6791 	queue_t	*q;
6792 	phyint_t *phyi;
6793 	ipsq_func_t func;
6794 
6795 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6796 
6797 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6798 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6799 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6800 		return;
6801 	}
6802 
6803 	for (;;) {
6804 		phyi = ipsq->ipsq_phyint;
6805 		mp = ipsq_dq(ipsq);
6806 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6807 
6808 		/*
6809 		 * If we've changed to a new IPSQ, and the phyint associated
6810 		 * with the old one has gone away, free the old IPSQ.  Note
6811 		 * that this cannot happen while the IPSQ is in a group.
6812 		 */
6813 		if (mp_ipsq != ipsq && phyi == NULL) {
6814 			ASSERT(ipsq->ipsq_next == ipsq);
6815 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6816 			ipsq_delete(ipsq);
6817 		}
6818 
6819 		if (mp == NULL)
6820 			break;
6821 
6822 		q = mp->b_queue;
6823 		func = (ipsq_func_t)mp->b_prev;
6824 		ipsq = mp_ipsq;
6825 		mp->b_next = mp->b_prev = NULL;
6826 		mp->b_queue = NULL;
6827 
6828 		/*
6829 		 * If 'q' is an conn queue, it is valid, since we did a
6830 		 * a refhold on the conn at the start of the ioctl.
6831 		 * If 'q' is an ill queue, it is valid, since close of an
6832 		 * ill will clean up its IPSQ.
6833 		 */
6834 		(*func)(ipsq, q, mp, NULL);
6835 	}
6836 }
6837 
6838 /*
6839  * Used to start any igmp or mld timers that could not be started
6840  * while holding ill_mcast_lock. The timers can't be started while holding
6841  * the lock, since mld/igmp_start_timers may need to call untimeout()
6842  * which can't be done while holding the lock which the timeout handler
6843  * acquires. Otherwise
6844  * there could be a deadlock since the timeout handlers
6845  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6846  * ill_mcast_lock.
6847  */
6848 void
6849 ill_mcast_timer_start(ip_stack_t *ipst)
6850 {
6851 	int		next;
6852 
6853 	mutex_enter(&ipst->ips_igmp_timer_lock);
6854 	next = ipst->ips_igmp_deferred_next;
6855 	ipst->ips_igmp_deferred_next = INFINITY;
6856 	mutex_exit(&ipst->ips_igmp_timer_lock);
6857 
6858 	if (next != INFINITY)
6859 		igmp_start_timers(next, ipst);
6860 
6861 	mutex_enter(&ipst->ips_mld_timer_lock);
6862 	next = ipst->ips_mld_deferred_next;
6863 	ipst->ips_mld_deferred_next = INFINITY;
6864 	mutex_exit(&ipst->ips_mld_timer_lock);
6865 
6866 	if (next != INFINITY)
6867 		mld_start_timers(next, ipst);
6868 }
6869 
6870 /*
6871  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6872  * and `ioccmd'.
6873  */
6874 void
6875 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6876 {
6877 	ill_t *ill = ipif->ipif_ill;
6878 	ipxop_t *ipx = ipsq->ipsq_xop;
6879 
6880 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6881 	ASSERT(ipx->ipx_current_ipif == NULL);
6882 	ASSERT(ipx->ipx_current_ioctl == 0);
6883 
6884 	ipx->ipx_current_done = B_FALSE;
6885 	ipx->ipx_current_ioctl = ioccmd;
6886 	mutex_enter(&ipx->ipx_lock);
6887 	ipx->ipx_current_ipif = ipif;
6888 	mutex_exit(&ipx->ipx_lock);
6889 
6890 	/*
6891 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6892 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6893 	 * references to the ipif (so that the references will eventually
6894 	 * drop to zero) and also prevents any "get" operations (e.g.,
6895 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6896 	 * operation has completed and the ipif is again in a stable state.
6897 	 *
6898 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6899 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6900 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6901 	 * ipifs will be affected.
6902 	 *
6903 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6904 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6905 	 * operate on.
6906 	 */
6907 	switch (ioccmd) {
6908 	case SIOCLIFREMOVEIF:
6909 		break;
6910 	case 0:
6911 		mutex_enter(&ill->ill_lock);
6912 		ipif = ipif->ipif_ill->ill_ipif;
6913 		for (; ipif != NULL; ipif = ipif->ipif_next)
6914 			ipif->ipif_state_flags |= IPIF_CHANGING;
6915 		mutex_exit(&ill->ill_lock);
6916 		break;
6917 	default:
6918 		mutex_enter(&ill->ill_lock);
6919 		ipif->ipif_state_flags |= IPIF_CHANGING;
6920 		mutex_exit(&ill->ill_lock);
6921 	}
6922 }
6923 
6924 /*
6925  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6926  * the next exclusive operation to begin once we ipsq_exit().  However, if
6927  * pending DLPI operations remain, then we will wait for the queue to drain
6928  * before allowing the next exclusive operation to begin.  This ensures that
6929  * DLPI operations from one exclusive operation are never improperly processed
6930  * as part of a subsequent exclusive operation.
6931  */
6932 void
6933 ipsq_current_finish(ipsq_t *ipsq)
6934 {
6935 	ipxop_t	*ipx = ipsq->ipsq_xop;
6936 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6937 	ipif_t	*ipif = ipx->ipx_current_ipif;
6938 
6939 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6940 
6941 	/*
6942 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6943 	 * (but in that case, IPIF_CHANGING will already be clear and no
6944 	 * pending DLPI messages can remain).
6945 	 */
6946 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6947 		ill_t *ill = ipif->ipif_ill;
6948 
6949 		mutex_enter(&ill->ill_lock);
6950 		dlpi_pending = ill->ill_dlpi_pending;
6951 		if (ipx->ipx_current_ioctl == 0) {
6952 			ipif = ill->ill_ipif;
6953 			for (; ipif != NULL; ipif = ipif->ipif_next)
6954 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6955 		} else {
6956 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6957 		}
6958 		mutex_exit(&ill->ill_lock);
6959 	}
6960 
6961 	ASSERT(!ipx->ipx_current_done);
6962 	ipx->ipx_current_done = B_TRUE;
6963 	ipx->ipx_current_ioctl = 0;
6964 	if (dlpi_pending == DL_PRIM_INVAL) {
6965 		mutex_enter(&ipx->ipx_lock);
6966 		ipx->ipx_current_ipif = NULL;
6967 		mutex_exit(&ipx->ipx_lock);
6968 	}
6969 }
6970 
6971 /*
6972  * The ill is closing. Flush all messages on the ipsq that originated
6973  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6974  * for this ill since ipsq_enter could not have entered until then.
6975  * New messages can't be queued since the CONDEMNED flag is set.
6976  */
6977 static void
6978 ipsq_flush(ill_t *ill)
6979 {
6980 	queue_t	*q;
6981 	mblk_t	*prev;
6982 	mblk_t	*mp;
6983 	mblk_t	*mp_next;
6984 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6985 
6986 	ASSERT(IAM_WRITER_ILL(ill));
6987 
6988 	/*
6989 	 * Flush any messages sent up by the driver.
6990 	 */
6991 	mutex_enter(&ipx->ipx_lock);
6992 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6993 		mp_next = mp->b_next;
6994 		q = mp->b_queue;
6995 		if (q == ill->ill_rq || q == ill->ill_wq) {
6996 			/* dequeue mp */
6997 			if (prev == NULL)
6998 				ipx->ipx_mphead = mp->b_next;
6999 			else
7000 				prev->b_next = mp->b_next;
7001 			if (ipx->ipx_mptail == mp) {
7002 				ASSERT(mp_next == NULL);
7003 				ipx->ipx_mptail = prev;
7004 			}
7005 			inet_freemsg(mp);
7006 		} else {
7007 			prev = mp;
7008 		}
7009 	}
7010 	mutex_exit(&ipx->ipx_lock);
7011 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7012 	ipsq_xopq_mp_cleanup(ill, NULL);
7013 }
7014 
7015 /*
7016  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7017  * and return the associated ipif.
7018  * Return value:
7019  *	Non zero: An error has occurred. ci may not be filled out.
7020  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7021  *	a held ipif in ci.ci_ipif.
7022  */
7023 int
7024 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7025     cmd_info_t *ci)
7026 {
7027 	char		*name;
7028 	struct ifreq    *ifr;
7029 	struct lifreq    *lifr;
7030 	ipif_t		*ipif = NULL;
7031 	ill_t		*ill;
7032 	conn_t		*connp;
7033 	boolean_t	isv6;
7034 	int		err;
7035 	mblk_t		*mp1;
7036 	zoneid_t	zoneid;
7037 	ip_stack_t	*ipst;
7038 
7039 	if (q->q_next != NULL) {
7040 		ill = (ill_t *)q->q_ptr;
7041 		isv6 = ill->ill_isv6;
7042 		connp = NULL;
7043 		zoneid = ALL_ZONES;
7044 		ipst = ill->ill_ipst;
7045 	} else {
7046 		ill = NULL;
7047 		connp = Q_TO_CONN(q);
7048 		isv6 = (connp->conn_family == AF_INET6);
7049 		zoneid = connp->conn_zoneid;
7050 		if (zoneid == GLOBAL_ZONEID) {
7051 			/* global zone can access ipifs in all zones */
7052 			zoneid = ALL_ZONES;
7053 		}
7054 		ipst = connp->conn_netstack->netstack_ip;
7055 	}
7056 
7057 	/* Has been checked in ip_wput_nondata */
7058 	mp1 = mp->b_cont->b_cont;
7059 
7060 	if (ipip->ipi_cmd_type == IF_CMD) {
7061 		/* This a old style SIOC[GS]IF* command */
7062 		ifr = (struct ifreq *)mp1->b_rptr;
7063 		/*
7064 		 * Null terminate the string to protect against buffer
7065 		 * overrun. String was generated by user code and may not
7066 		 * be trusted.
7067 		 */
7068 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7069 		name = ifr->ifr_name;
7070 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7071 		ci->ci_sin6 = NULL;
7072 		ci->ci_lifr = (struct lifreq *)ifr;
7073 	} else {
7074 		/* This a new style SIOC[GS]LIF* command */
7075 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7076 		lifr = (struct lifreq *)mp1->b_rptr;
7077 		/*
7078 		 * Null terminate the string to protect against buffer
7079 		 * overrun. String was generated by user code and may not
7080 		 * be trusted.
7081 		 */
7082 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7083 		name = lifr->lifr_name;
7084 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7085 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7086 		ci->ci_lifr = lifr;
7087 	}
7088 
7089 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7090 		/*
7091 		 * The ioctl will be failed if the ioctl comes down
7092 		 * an conn stream
7093 		 */
7094 		if (ill == NULL) {
7095 			/*
7096 			 * Not an ill queue, return EINVAL same as the
7097 			 * old error code.
7098 			 */
7099 			return (ENXIO);
7100 		}
7101 		ipif = ill->ill_ipif;
7102 		ipif_refhold(ipif);
7103 	} else {
7104 		/*
7105 		 * Ensure that ioctls don't see any internal state changes
7106 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7107 		 * set.
7108 		 */
7109 		ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7110 		    isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7111 		if (ipif == NULL) {
7112 			if (err == EINPROGRESS)
7113 				return (err);
7114 			err = 0;	/* Ensure we don't use it below */
7115 		}
7116 	}
7117 
7118 	/*
7119 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7120 	 */
7121 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7122 		ipif_refrele(ipif);
7123 		return (ENXIO);
7124 	}
7125 
7126 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7127 	    name[0] == '\0') {
7128 		/*
7129 		 * Handle a or a SIOC?IF* with a null name
7130 		 * during plumb (on the ill queue before the I_PLINK).
7131 		 */
7132 		ipif = ill->ill_ipif;
7133 		ipif_refhold(ipif);
7134 	}
7135 
7136 	if (ipif == NULL)
7137 		return (ENXIO);
7138 
7139 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7140 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7141 
7142 	ci->ci_ipif = ipif;
7143 	return (0);
7144 }
7145 
7146 /*
7147  * Return the total number of ipifs.
7148  */
7149 static uint_t
7150 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7151 {
7152 	uint_t numifs = 0;
7153 	ill_t	*ill;
7154 	ill_walk_context_t	ctx;
7155 	ipif_t	*ipif;
7156 
7157 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7158 	ill = ILL_START_WALK_V4(&ctx, ipst);
7159 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7160 		if (IS_UNDER_IPMP(ill))
7161 			continue;
7162 		for (ipif = ill->ill_ipif; ipif != NULL;
7163 		    ipif = ipif->ipif_next) {
7164 			if (ipif->ipif_zoneid == zoneid ||
7165 			    ipif->ipif_zoneid == ALL_ZONES)
7166 				numifs++;
7167 		}
7168 	}
7169 	rw_exit(&ipst->ips_ill_g_lock);
7170 	return (numifs);
7171 }
7172 
7173 /*
7174  * Return the total number of ipifs.
7175  */
7176 static uint_t
7177 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7178 {
7179 	uint_t numifs = 0;
7180 	ill_t	*ill;
7181 	ipif_t	*ipif;
7182 	ill_walk_context_t	ctx;
7183 
7184 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7185 
7186 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7187 	if (family == AF_INET)
7188 		ill = ILL_START_WALK_V4(&ctx, ipst);
7189 	else if (family == AF_INET6)
7190 		ill = ILL_START_WALK_V6(&ctx, ipst);
7191 	else
7192 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7193 
7194 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7195 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7196 			continue;
7197 
7198 		for (ipif = ill->ill_ipif; ipif != NULL;
7199 		    ipif = ipif->ipif_next) {
7200 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7201 			    !(lifn_flags & LIFC_NOXMIT))
7202 				continue;
7203 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7204 			    !(lifn_flags & LIFC_TEMPORARY))
7205 				continue;
7206 			if (((ipif->ipif_flags &
7207 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7208 			    IPIF_DEPRECATED)) ||
7209 			    IS_LOOPBACK(ill) ||
7210 			    !(ipif->ipif_flags & IPIF_UP)) &&
7211 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7212 				continue;
7213 
7214 			if (zoneid != ipif->ipif_zoneid &&
7215 			    ipif->ipif_zoneid != ALL_ZONES &&
7216 			    (zoneid != GLOBAL_ZONEID ||
7217 			    !(lifn_flags & LIFC_ALLZONES)))
7218 				continue;
7219 
7220 			numifs++;
7221 		}
7222 	}
7223 	rw_exit(&ipst->ips_ill_g_lock);
7224 	return (numifs);
7225 }
7226 
7227 uint_t
7228 ip_get_lifsrcofnum(ill_t *ill)
7229 {
7230 	uint_t numifs = 0;
7231 	ill_t	*ill_head = ill;
7232 	ip_stack_t	*ipst = ill->ill_ipst;
7233 
7234 	/*
7235 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7236 	 * other thread may be trying to relink the ILLs in this usesrc group
7237 	 * and adjusting the ill_usesrc_grp_next pointers
7238 	 */
7239 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7240 	if ((ill->ill_usesrc_ifindex == 0) &&
7241 	    (ill->ill_usesrc_grp_next != NULL)) {
7242 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7243 		    ill = ill->ill_usesrc_grp_next)
7244 			numifs++;
7245 	}
7246 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7247 
7248 	return (numifs);
7249 }
7250 
7251 /* Null values are passed in for ipif, sin, and ifreq */
7252 /* ARGSUSED */
7253 int
7254 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7255     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7256 {
7257 	int *nump;
7258 	conn_t *connp = Q_TO_CONN(q);
7259 
7260 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7261 
7262 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7263 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7264 
7265 	*nump = ip_get_numifs(connp->conn_zoneid,
7266 	    connp->conn_netstack->netstack_ip);
7267 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7268 	return (0);
7269 }
7270 
7271 /* Null values are passed in for ipif, sin, and ifreq */
7272 /* ARGSUSED */
7273 int
7274 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7275     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7276 {
7277 	struct lifnum *lifn;
7278 	mblk_t	*mp1;
7279 	conn_t *connp = Q_TO_CONN(q);
7280 
7281 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7282 
7283 	/* Existence checked in ip_wput_nondata */
7284 	mp1 = mp->b_cont->b_cont;
7285 
7286 	lifn = (struct lifnum *)mp1->b_rptr;
7287 	switch (lifn->lifn_family) {
7288 	case AF_UNSPEC:
7289 	case AF_INET:
7290 	case AF_INET6:
7291 		break;
7292 	default:
7293 		return (EAFNOSUPPORT);
7294 	}
7295 
7296 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7297 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7298 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7299 	return (0);
7300 }
7301 
7302 /* ARGSUSED */
7303 int
7304 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7305     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7306 {
7307 	STRUCT_HANDLE(ifconf, ifc);
7308 	mblk_t *mp1;
7309 	struct iocblk *iocp;
7310 	struct ifreq *ifr;
7311 	ill_walk_context_t	ctx;
7312 	ill_t	*ill;
7313 	ipif_t	*ipif;
7314 	struct sockaddr_in *sin;
7315 	int32_t	ifclen;
7316 	zoneid_t zoneid;
7317 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7318 
7319 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7320 
7321 	ip1dbg(("ip_sioctl_get_ifconf"));
7322 	/* Existence verified in ip_wput_nondata */
7323 	mp1 = mp->b_cont->b_cont;
7324 	iocp = (struct iocblk *)mp->b_rptr;
7325 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7326 
7327 	/*
7328 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7329 	 * the user buffer address and length into which the list of struct
7330 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7331 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7332 	 * the SIOCGIFCONF operation was redefined to simply provide
7333 	 * a large output buffer into which we are supposed to jam the ifreq
7334 	 * array.  The same ioctl command code was used, despite the fact that
7335 	 * both the applications and the kernel code had to change, thus making
7336 	 * it impossible to support both interfaces.
7337 	 *
7338 	 * For reasons not good enough to try to explain, the following
7339 	 * algorithm is used for deciding what to do with one of these:
7340 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7341 	 * form with the output buffer coming down as the continuation message.
7342 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7343 	 * and we have to copy in the ifconf structure to find out how big the
7344 	 * output buffer is and where to copy out to.  Sure no problem...
7345 	 *
7346 	 */
7347 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7348 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7349 		int numifs = 0;
7350 		size_t ifc_bufsize;
7351 
7352 		/*
7353 		 * Must be (better be!) continuation of a TRANSPARENT
7354 		 * IOCTL.  We just copied in the ifconf structure.
7355 		 */
7356 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7357 		    (struct ifconf *)mp1->b_rptr);
7358 
7359 		/*
7360 		 * Allocate a buffer to hold requested information.
7361 		 *
7362 		 * If ifc_len is larger than what is needed, we only
7363 		 * allocate what we will use.
7364 		 *
7365 		 * If ifc_len is smaller than what is needed, return
7366 		 * EINVAL.
7367 		 *
7368 		 * XXX: the ill_t structure can hava 2 counters, for
7369 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7370 		 * number of interfaces for a device, so we don't need
7371 		 * to count them here...
7372 		 */
7373 		numifs = ip_get_numifs(zoneid, ipst);
7374 
7375 		ifclen = STRUCT_FGET(ifc, ifc_len);
7376 		ifc_bufsize = numifs * sizeof (struct ifreq);
7377 		if (ifc_bufsize > ifclen) {
7378 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7379 				/* old behaviour */
7380 				return (EINVAL);
7381 			} else {
7382 				ifc_bufsize = ifclen;
7383 			}
7384 		}
7385 
7386 		mp1 = mi_copyout_alloc(q, mp,
7387 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7388 		if (mp1 == NULL)
7389 			return (ENOMEM);
7390 
7391 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7392 	}
7393 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7394 	/*
7395 	 * the SIOCGIFCONF ioctl only knows about
7396 	 * IPv4 addresses, so don't try to tell
7397 	 * it about interfaces with IPv6-only
7398 	 * addresses. (Last parm 'isv6' is B_FALSE)
7399 	 */
7400 
7401 	ifr = (struct ifreq *)mp1->b_rptr;
7402 
7403 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7404 	ill = ILL_START_WALK_V4(&ctx, ipst);
7405 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7406 		if (IS_UNDER_IPMP(ill))
7407 			continue;
7408 		for (ipif = ill->ill_ipif; ipif != NULL;
7409 		    ipif = ipif->ipif_next) {
7410 			if (zoneid != ipif->ipif_zoneid &&
7411 			    ipif->ipif_zoneid != ALL_ZONES)
7412 				continue;
7413 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7414 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7415 					/* old behaviour */
7416 					rw_exit(&ipst->ips_ill_g_lock);
7417 					return (EINVAL);
7418 				} else {
7419 					goto if_copydone;
7420 				}
7421 			}
7422 			ipif_get_name(ipif, ifr->ifr_name,
7423 			    sizeof (ifr->ifr_name));
7424 			sin = (sin_t *)&ifr->ifr_addr;
7425 			*sin = sin_null;
7426 			sin->sin_family = AF_INET;
7427 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7428 			ifr++;
7429 		}
7430 	}
7431 if_copydone:
7432 	rw_exit(&ipst->ips_ill_g_lock);
7433 	mp1->b_wptr = (uchar_t *)ifr;
7434 
7435 	if (STRUCT_BUF(ifc) != NULL) {
7436 		STRUCT_FSET(ifc, ifc_len,
7437 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7438 	}
7439 	return (0);
7440 }
7441 
7442 /*
7443  * Get the interfaces using the address hosted on the interface passed in,
7444  * as a source adddress
7445  */
7446 /* ARGSUSED */
7447 int
7448 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7449     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7450 {
7451 	mblk_t *mp1;
7452 	ill_t	*ill, *ill_head;
7453 	ipif_t	*ipif, *orig_ipif;
7454 	int	numlifs = 0;
7455 	size_t	lifs_bufsize, lifsmaxlen;
7456 	struct	lifreq *lifr;
7457 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7458 	uint_t	ifindex;
7459 	zoneid_t zoneid;
7460 	boolean_t isv6 = B_FALSE;
7461 	struct	sockaddr_in	*sin;
7462 	struct	sockaddr_in6	*sin6;
7463 	STRUCT_HANDLE(lifsrcof, lifs);
7464 	ip_stack_t		*ipst;
7465 
7466 	ipst = CONNQ_TO_IPST(q);
7467 
7468 	ASSERT(q->q_next == NULL);
7469 
7470 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7471 
7472 	/* Existence verified in ip_wput_nondata */
7473 	mp1 = mp->b_cont->b_cont;
7474 
7475 	/*
7476 	 * Must be (better be!) continuation of a TRANSPARENT
7477 	 * IOCTL.  We just copied in the lifsrcof structure.
7478 	 */
7479 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7480 	    (struct lifsrcof *)mp1->b_rptr);
7481 
7482 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7483 		return (EINVAL);
7484 
7485 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7486 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7487 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7488 	if (ipif == NULL) {
7489 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7490 		    ifindex));
7491 		return (ENXIO);
7492 	}
7493 
7494 	/* Allocate a buffer to hold requested information */
7495 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7496 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7497 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7498 	/* The actual size needed is always returned in lifs_len */
7499 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7500 
7501 	/* If the amount we need is more than what is passed in, abort */
7502 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7503 		ipif_refrele(ipif);
7504 		return (0);
7505 	}
7506 
7507 	mp1 = mi_copyout_alloc(q, mp,
7508 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7509 	if (mp1 == NULL) {
7510 		ipif_refrele(ipif);
7511 		return (ENOMEM);
7512 	}
7513 
7514 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7515 	bzero(mp1->b_rptr, lifs_bufsize);
7516 
7517 	lifr = (struct lifreq *)mp1->b_rptr;
7518 
7519 	ill = ill_head = ipif->ipif_ill;
7520 	orig_ipif = ipif;
7521 
7522 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7523 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7524 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7525 
7526 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7527 	for (; (ill != NULL) && (ill != ill_head);
7528 	    ill = ill->ill_usesrc_grp_next) {
7529 
7530 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7531 			break;
7532 
7533 		ipif = ill->ill_ipif;
7534 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7535 		if (ipif->ipif_isv6) {
7536 			sin6 = (sin6_t *)&lifr->lifr_addr;
7537 			*sin6 = sin6_null;
7538 			sin6->sin6_family = AF_INET6;
7539 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7540 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7541 			    &ipif->ipif_v6net_mask);
7542 		} else {
7543 			sin = (sin_t *)&lifr->lifr_addr;
7544 			*sin = sin_null;
7545 			sin->sin_family = AF_INET;
7546 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7547 			lifr->lifr_addrlen = ip_mask_to_plen(
7548 			    ipif->ipif_net_mask);
7549 		}
7550 		lifr++;
7551 	}
7552 	rw_exit(&ipst->ips_ill_g_lock);
7553 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7554 	ipif_refrele(orig_ipif);
7555 	mp1->b_wptr = (uchar_t *)lifr;
7556 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7557 
7558 	return (0);
7559 }
7560 
7561 /* ARGSUSED */
7562 int
7563 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7564     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7565 {
7566 	mblk_t *mp1;
7567 	int	list;
7568 	ill_t	*ill;
7569 	ipif_t	*ipif;
7570 	int	flags;
7571 	int	numlifs = 0;
7572 	size_t	lifc_bufsize;
7573 	struct	lifreq *lifr;
7574 	sa_family_t	family;
7575 	struct	sockaddr_in	*sin;
7576 	struct	sockaddr_in6	*sin6;
7577 	ill_walk_context_t	ctx;
7578 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7579 	int32_t	lifclen;
7580 	zoneid_t zoneid;
7581 	STRUCT_HANDLE(lifconf, lifc);
7582 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7583 
7584 	ip1dbg(("ip_sioctl_get_lifconf"));
7585 
7586 	ASSERT(q->q_next == NULL);
7587 
7588 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7589 
7590 	/* Existence verified in ip_wput_nondata */
7591 	mp1 = mp->b_cont->b_cont;
7592 
7593 	/*
7594 	 * An extended version of SIOCGIFCONF that takes an
7595 	 * additional address family and flags field.
7596 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7597 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7598 	 * interfaces are omitted.
7599 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7600 	 * unless LIFC_TEMPORARY is specified.
7601 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7602 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7603 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7604 	 * has priority over LIFC_NOXMIT.
7605 	 */
7606 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7607 
7608 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7609 		return (EINVAL);
7610 
7611 	/*
7612 	 * Must be (better be!) continuation of a TRANSPARENT
7613 	 * IOCTL.  We just copied in the lifconf structure.
7614 	 */
7615 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7616 
7617 	family = STRUCT_FGET(lifc, lifc_family);
7618 	flags = STRUCT_FGET(lifc, lifc_flags);
7619 
7620 	switch (family) {
7621 	case AF_UNSPEC:
7622 		/*
7623 		 * walk all ILL's.
7624 		 */
7625 		list = MAX_G_HEADS;
7626 		break;
7627 	case AF_INET:
7628 		/*
7629 		 * walk only IPV4 ILL's.
7630 		 */
7631 		list = IP_V4_G_HEAD;
7632 		break;
7633 	case AF_INET6:
7634 		/*
7635 		 * walk only IPV6 ILL's.
7636 		 */
7637 		list = IP_V6_G_HEAD;
7638 		break;
7639 	default:
7640 		return (EAFNOSUPPORT);
7641 	}
7642 
7643 	/*
7644 	 * Allocate a buffer to hold requested information.
7645 	 *
7646 	 * If lifc_len is larger than what is needed, we only
7647 	 * allocate what we will use.
7648 	 *
7649 	 * If lifc_len is smaller than what is needed, return
7650 	 * EINVAL.
7651 	 */
7652 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7653 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7654 	lifclen = STRUCT_FGET(lifc, lifc_len);
7655 	if (lifc_bufsize > lifclen) {
7656 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7657 			return (EINVAL);
7658 		else
7659 			lifc_bufsize = lifclen;
7660 	}
7661 
7662 	mp1 = mi_copyout_alloc(q, mp,
7663 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7664 	if (mp1 == NULL)
7665 		return (ENOMEM);
7666 
7667 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7668 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7669 
7670 	lifr = (struct lifreq *)mp1->b_rptr;
7671 
7672 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7673 	ill = ill_first(list, list, &ctx, ipst);
7674 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7675 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7676 			continue;
7677 
7678 		for (ipif = ill->ill_ipif; ipif != NULL;
7679 		    ipif = ipif->ipif_next) {
7680 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7681 			    !(flags & LIFC_NOXMIT))
7682 				continue;
7683 
7684 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7685 			    !(flags & LIFC_TEMPORARY))
7686 				continue;
7687 
7688 			if (((ipif->ipif_flags &
7689 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7690 			    IPIF_DEPRECATED)) ||
7691 			    IS_LOOPBACK(ill) ||
7692 			    !(ipif->ipif_flags & IPIF_UP)) &&
7693 			    (flags & LIFC_EXTERNAL_SOURCE))
7694 				continue;
7695 
7696 			if (zoneid != ipif->ipif_zoneid &&
7697 			    ipif->ipif_zoneid != ALL_ZONES &&
7698 			    (zoneid != GLOBAL_ZONEID ||
7699 			    !(flags & LIFC_ALLZONES)))
7700 				continue;
7701 
7702 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7703 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7704 					rw_exit(&ipst->ips_ill_g_lock);
7705 					return (EINVAL);
7706 				} else {
7707 					goto lif_copydone;
7708 				}
7709 			}
7710 
7711 			ipif_get_name(ipif, lifr->lifr_name,
7712 			    sizeof (lifr->lifr_name));
7713 			lifr->lifr_type = ill->ill_type;
7714 			if (ipif->ipif_isv6) {
7715 				sin6 = (sin6_t *)&lifr->lifr_addr;
7716 				*sin6 = sin6_null;
7717 				sin6->sin6_family = AF_INET6;
7718 				sin6->sin6_addr =
7719 				    ipif->ipif_v6lcl_addr;
7720 				lifr->lifr_addrlen =
7721 				    ip_mask_to_plen_v6(
7722 				    &ipif->ipif_v6net_mask);
7723 			} else {
7724 				sin = (sin_t *)&lifr->lifr_addr;
7725 				*sin = sin_null;
7726 				sin->sin_family = AF_INET;
7727 				sin->sin_addr.s_addr =
7728 				    ipif->ipif_lcl_addr;
7729 				lifr->lifr_addrlen =
7730 				    ip_mask_to_plen(
7731 				    ipif->ipif_net_mask);
7732 			}
7733 			lifr++;
7734 		}
7735 	}
7736 lif_copydone:
7737 	rw_exit(&ipst->ips_ill_g_lock);
7738 
7739 	mp1->b_wptr = (uchar_t *)lifr;
7740 	if (STRUCT_BUF(lifc) != NULL) {
7741 		STRUCT_FSET(lifc, lifc_len,
7742 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7743 	}
7744 	return (0);
7745 }
7746 
7747 static void
7748 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7749 {
7750 	ip6_asp_t *table;
7751 	size_t table_size;
7752 	mblk_t *data_mp;
7753 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7754 	ip_stack_t	*ipst;
7755 
7756 	if (q->q_next == NULL)
7757 		ipst = CONNQ_TO_IPST(q);
7758 	else
7759 		ipst = ILLQ_TO_IPST(q);
7760 
7761 	/* These two ioctls are I_STR only */
7762 	if (iocp->ioc_count == TRANSPARENT) {
7763 		miocnak(q, mp, 0, EINVAL);
7764 		return;
7765 	}
7766 
7767 	data_mp = mp->b_cont;
7768 	if (data_mp == NULL) {
7769 		/* The user passed us a NULL argument */
7770 		table = NULL;
7771 		table_size = iocp->ioc_count;
7772 	} else {
7773 		/*
7774 		 * The user provided a table.  The stream head
7775 		 * may have copied in the user data in chunks,
7776 		 * so make sure everything is pulled up
7777 		 * properly.
7778 		 */
7779 		if (MBLKL(data_mp) < iocp->ioc_count) {
7780 			mblk_t *new_data_mp;
7781 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7782 			    NULL) {
7783 				miocnak(q, mp, 0, ENOMEM);
7784 				return;
7785 			}
7786 			freemsg(data_mp);
7787 			data_mp = new_data_mp;
7788 			mp->b_cont = data_mp;
7789 		}
7790 		table = (ip6_asp_t *)data_mp->b_rptr;
7791 		table_size = iocp->ioc_count;
7792 	}
7793 
7794 	switch (iocp->ioc_cmd) {
7795 	case SIOCGIP6ADDRPOLICY:
7796 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7797 		if (iocp->ioc_rval == -1)
7798 			iocp->ioc_error = EINVAL;
7799 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7800 		else if (table != NULL &&
7801 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7802 			ip6_asp_t *src = table;
7803 			ip6_asp32_t *dst = (void *)table;
7804 			int count = table_size / sizeof (ip6_asp_t);
7805 			int i;
7806 
7807 			/*
7808 			 * We need to do an in-place shrink of the array
7809 			 * to match the alignment attributes of the
7810 			 * 32-bit ABI looking at it.
7811 			 */
7812 			/* LINTED: logical expression always true: op "||" */
7813 			ASSERT(sizeof (*src) > sizeof (*dst));
7814 			for (i = 1; i < count; i++)
7815 				bcopy(src + i, dst + i, sizeof (*dst));
7816 		}
7817 #endif
7818 		break;
7819 
7820 	case SIOCSIP6ADDRPOLICY:
7821 		ASSERT(mp->b_prev == NULL);
7822 		mp->b_prev = (void *)q;
7823 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7824 		/*
7825 		 * We pass in the datamodel here so that the ip6_asp_replace()
7826 		 * routine can handle converting from 32-bit to native formats
7827 		 * where necessary.
7828 		 *
7829 		 * A better way to handle this might be to convert the inbound
7830 		 * data structure here, and hang it off a new 'mp'; thus the
7831 		 * ip6_asp_replace() logic would always be dealing with native
7832 		 * format data structures..
7833 		 *
7834 		 * (An even simpler way to handle these ioctls is to just
7835 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7836 		 * and just recompile everything that depends on it.)
7837 		 */
7838 #endif
7839 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7840 		    iocp->ioc_flag & IOC_MODELS);
7841 		return;
7842 	}
7843 
7844 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7845 	qreply(q, mp);
7846 }
7847 
7848 static void
7849 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7850 {
7851 	mblk_t 		*data_mp;
7852 	struct dstinforeq	*dir;
7853 	uint8_t		*end, *cur;
7854 	in6_addr_t	*daddr, *saddr;
7855 	ipaddr_t	v4daddr;
7856 	ire_t		*ire;
7857 	ipaddr_t	v4setsrc;
7858 	in6_addr_t	v6setsrc;
7859 	char		*slabel, *dlabel;
7860 	boolean_t	isipv4;
7861 	int		match_ire;
7862 	ill_t		*dst_ill;
7863 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7864 	conn_t		*connp = Q_TO_CONN(q);
7865 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7866 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7867 	uint64_t	ipif_flags;
7868 
7869 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7870 
7871 	/*
7872 	 * This ioctl is I_STR only, and must have a
7873 	 * data mblk following the M_IOCTL mblk.
7874 	 */
7875 	data_mp = mp->b_cont;
7876 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7877 		miocnak(q, mp, 0, EINVAL);
7878 		return;
7879 	}
7880 
7881 	if (MBLKL(data_mp) < iocp->ioc_count) {
7882 		mblk_t *new_data_mp;
7883 
7884 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7885 			miocnak(q, mp, 0, ENOMEM);
7886 			return;
7887 		}
7888 		freemsg(data_mp);
7889 		data_mp = new_data_mp;
7890 		mp->b_cont = data_mp;
7891 	}
7892 	match_ire = MATCH_IRE_DSTONLY;
7893 
7894 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7895 	    end - cur >= sizeof (struct dstinforeq);
7896 	    cur += sizeof (struct dstinforeq)) {
7897 		dir = (struct dstinforeq *)cur;
7898 		daddr = &dir->dir_daddr;
7899 		saddr = &dir->dir_saddr;
7900 
7901 		/*
7902 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7903 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7904 		 * and ip_select_source_v6() do not.
7905 		 */
7906 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7907 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7908 
7909 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7910 		if (isipv4) {
7911 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7912 			v4setsrc = INADDR_ANY;
7913 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7914 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7915 			    NULL, NULL);
7916 		} else {
7917 			v6setsrc = ipv6_all_zeros;
7918 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7919 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7920 			    NULL, NULL);
7921 		}
7922 		ASSERT(ire != NULL);
7923 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7924 			ire_refrele(ire);
7925 			dir->dir_dreachable = 0;
7926 
7927 			/* move on to next dst addr */
7928 			continue;
7929 		}
7930 		dir->dir_dreachable = 1;
7931 
7932 		dst_ill = ire_nexthop_ill(ire);
7933 		if (dst_ill == NULL) {
7934 			ire_refrele(ire);
7935 			continue;
7936 		}
7937 
7938 		/* With ipmp we most likely look at the ipmp ill here */
7939 		dir->dir_dmactype = dst_ill->ill_mactype;
7940 
7941 		if (isipv4) {
7942 			ipaddr_t v4saddr;
7943 
7944 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7945 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7946 			    &v4saddr, NULL, &ipif_flags) != 0) {
7947 				v4saddr = INADDR_ANY;
7948 				ipif_flags = 0;
7949 			}
7950 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7951 		} else {
7952 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7953 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7954 			    saddr, NULL, &ipif_flags) != 0) {
7955 				*saddr = ipv6_all_zeros;
7956 				ipif_flags = 0;
7957 			}
7958 		}
7959 
7960 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7961 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7962 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7963 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7964 		ire_refrele(ire);
7965 		ill_refrele(dst_ill);
7966 	}
7967 	miocack(q, mp, iocp->ioc_count, 0);
7968 }
7969 
7970 /*
7971  * Check if this is an address assigned to this machine.
7972  * Skips interfaces that are down by using ire checks.
7973  * Translates mapped addresses to v4 addresses and then
7974  * treats them as such, returning true if the v4 address
7975  * associated with this mapped address is configured.
7976  * Note: Applications will have to be careful what they do
7977  * with the response; use of mapped addresses limits
7978  * what can be done with the socket, especially with
7979  * respect to socket options and ioctls - neither IPv4
7980  * options nor IPv6 sticky options/ancillary data options
7981  * may be used.
7982  */
7983 /* ARGSUSED */
7984 int
7985 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7986     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7987 {
7988 	struct sioc_addrreq *sia;
7989 	sin_t *sin;
7990 	ire_t *ire;
7991 	mblk_t *mp1;
7992 	zoneid_t zoneid;
7993 	ip_stack_t	*ipst;
7994 
7995 	ip1dbg(("ip_sioctl_tmyaddr"));
7996 
7997 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7998 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7999 	ipst = CONNQ_TO_IPST(q);
8000 
8001 	/* Existence verified in ip_wput_nondata */
8002 	mp1 = mp->b_cont->b_cont;
8003 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8004 	sin = (sin_t *)&sia->sa_addr;
8005 	switch (sin->sin_family) {
8006 	case AF_INET6: {
8007 		sin6_t *sin6 = (sin6_t *)sin;
8008 
8009 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8010 			ipaddr_t v4_addr;
8011 
8012 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8013 			    v4_addr);
8014 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8015 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8016 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8017 		} else {
8018 			in6_addr_t v6addr;
8019 
8020 			v6addr = sin6->sin6_addr;
8021 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8022 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8023 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8024 		}
8025 		break;
8026 	}
8027 	case AF_INET: {
8028 		ipaddr_t v4addr;
8029 
8030 		v4addr = sin->sin_addr.s_addr;
8031 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8032 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8033 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8034 		break;
8035 	}
8036 	default:
8037 		return (EAFNOSUPPORT);
8038 	}
8039 	if (ire != NULL) {
8040 		sia->sa_res = 1;
8041 		ire_refrele(ire);
8042 	} else {
8043 		sia->sa_res = 0;
8044 	}
8045 	return (0);
8046 }
8047 
8048 /*
8049  * Check if this is an address assigned on-link i.e. neighbor,
8050  * and makes sure it's reachable from the current zone.
8051  * Returns true for my addresses as well.
8052  * Translates mapped addresses to v4 addresses and then
8053  * treats them as such, returning true if the v4 address
8054  * associated with this mapped address is configured.
8055  * Note: Applications will have to be careful what they do
8056  * with the response; use of mapped addresses limits
8057  * what can be done with the socket, especially with
8058  * respect to socket options and ioctls - neither IPv4
8059  * options nor IPv6 sticky options/ancillary data options
8060  * may be used.
8061  */
8062 /* ARGSUSED */
8063 int
8064 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8065     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8066 {
8067 	struct sioc_addrreq *sia;
8068 	sin_t *sin;
8069 	mblk_t	*mp1;
8070 	ire_t *ire = NULL;
8071 	zoneid_t zoneid;
8072 	ip_stack_t	*ipst;
8073 
8074 	ip1dbg(("ip_sioctl_tonlink"));
8075 
8076 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8077 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8078 	ipst = CONNQ_TO_IPST(q);
8079 
8080 	/* Existence verified in ip_wput_nondata */
8081 	mp1 = mp->b_cont->b_cont;
8082 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8083 	sin = (sin_t *)&sia->sa_addr;
8084 
8085 	/*
8086 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8087 	 * to make sure we only look at on-link unicast address.
8088 	 */
8089 	switch (sin->sin_family) {
8090 	case AF_INET6: {
8091 		sin6_t *sin6 = (sin6_t *)sin;
8092 
8093 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8094 			ipaddr_t v4_addr;
8095 
8096 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8097 			    v4_addr);
8098 			if (!CLASSD(v4_addr)) {
8099 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8100 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8101 				    0, ipst, NULL);
8102 			}
8103 		} else {
8104 			in6_addr_t v6addr;
8105 
8106 			v6addr = sin6->sin6_addr;
8107 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8108 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8109 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8110 				    ipst, NULL);
8111 			}
8112 		}
8113 		break;
8114 	}
8115 	case AF_INET: {
8116 		ipaddr_t v4addr;
8117 
8118 		v4addr = sin->sin_addr.s_addr;
8119 		if (!CLASSD(v4addr)) {
8120 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8121 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8122 		}
8123 		break;
8124 	}
8125 	default:
8126 		return (EAFNOSUPPORT);
8127 	}
8128 	sia->sa_res = 0;
8129 	if (ire != NULL) {
8130 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8131 
8132 		if ((ire->ire_type & IRE_ONLINK) &&
8133 		    !(ire->ire_type & IRE_BROADCAST))
8134 			sia->sa_res = 1;
8135 		ire_refrele(ire);
8136 	}
8137 	return (0);
8138 }
8139 
8140 /*
8141  * TBD: implement when kernel maintaines a list of site prefixes.
8142  */
8143 /* ARGSUSED */
8144 int
8145 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8146     ip_ioctl_cmd_t *ipip, void *ifreq)
8147 {
8148 	return (ENXIO);
8149 }
8150 
8151 /* ARP IOCTLs. */
8152 /* ARGSUSED */
8153 int
8154 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8155     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8156 {
8157 	int		err;
8158 	ipaddr_t	ipaddr;
8159 	struct iocblk	*iocp;
8160 	conn_t		*connp;
8161 	struct arpreq	*ar;
8162 	struct xarpreq	*xar;
8163 	int		arp_flags, flags, alength;
8164 	uchar_t		*lladdr;
8165 	ip_stack_t	*ipst;
8166 	ill_t		*ill = ipif->ipif_ill;
8167 	ill_t		*proxy_ill = NULL;
8168 	ipmp_arpent_t	*entp = NULL;
8169 	boolean_t	proxyarp = B_FALSE;
8170 	boolean_t	if_arp_ioctl = B_FALSE;
8171 	ncec_t		*ncec = NULL;
8172 	nce_t		*nce;
8173 
8174 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8175 	connp = Q_TO_CONN(q);
8176 	ipst = connp->conn_netstack->netstack_ip;
8177 	iocp = (struct iocblk *)mp->b_rptr;
8178 
8179 	if (ipip->ipi_cmd_type == XARP_CMD) {
8180 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8181 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8182 		ar = NULL;
8183 
8184 		arp_flags = xar->xarp_flags;
8185 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8186 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8187 		/*
8188 		 * Validate against user's link layer address length
8189 		 * input and name and addr length limits.
8190 		 */
8191 		alength = ill->ill_phys_addr_length;
8192 		if (ipip->ipi_cmd == SIOCSXARP) {
8193 			if (alength != xar->xarp_ha.sdl_alen ||
8194 			    (alength + xar->xarp_ha.sdl_nlen >
8195 			    sizeof (xar->xarp_ha.sdl_data)))
8196 				return (EINVAL);
8197 		}
8198 	} else {
8199 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8200 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8201 		xar = NULL;
8202 
8203 		arp_flags = ar->arp_flags;
8204 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8205 		/*
8206 		 * Theoretically, the sa_family could tell us what link
8207 		 * layer type this operation is trying to deal with. By
8208 		 * common usage AF_UNSPEC means ethernet. We'll assume
8209 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8210 		 * for now. Our new SIOC*XARP ioctls can be used more
8211 		 * generally.
8212 		 *
8213 		 * If the underlying media happens to have a non 6 byte
8214 		 * address, arp module will fail set/get, but the del
8215 		 * operation will succeed.
8216 		 */
8217 		alength = 6;
8218 		if ((ipip->ipi_cmd != SIOCDARP) &&
8219 		    (alength != ill->ill_phys_addr_length)) {
8220 			return (EINVAL);
8221 		}
8222 	}
8223 
8224 	/* Translate ATF* flags to NCE* flags */
8225 	flags = 0;
8226 	if (arp_flags & ATF_AUTHORITY)
8227 		flags |= NCE_F_AUTHORITY;
8228 	if (arp_flags & ATF_PERM)
8229 		flags |= NCE_F_NONUD; /* not subject to aging */
8230 	if (arp_flags & ATF_PUBL)
8231 		flags |= NCE_F_PUBLISH;
8232 
8233 	/*
8234 	 * IPMP ARP special handling:
8235 	 *
8236 	 * 1. Since ARP mappings must appear consistent across the group,
8237 	 *    prohibit changing ARP mappings on the underlying interfaces.
8238 	 *
8239 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8240 	 *    IP itself, prohibit changing them.
8241 	 *
8242 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8243 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8244 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8245 	 */
8246 	if (IS_UNDER_IPMP(ill)) {
8247 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8248 			return (EPERM);
8249 	}
8250 	if (IS_IPMP(ill)) {
8251 		ipmp_illgrp_t *illg = ill->ill_grp;
8252 
8253 		switch (ipip->ipi_cmd) {
8254 		case SIOCSARP:
8255 		case SIOCSXARP:
8256 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8257 			if (proxy_ill != NULL) {
8258 				proxyarp = B_TRUE;
8259 				if (!ipmp_ill_is_active(proxy_ill))
8260 					proxy_ill = ipmp_illgrp_next_ill(illg);
8261 				if (proxy_ill != NULL)
8262 					lladdr = proxy_ill->ill_phys_addr;
8263 			}
8264 			/* FALLTHRU */
8265 		}
8266 	}
8267 
8268 	ipaddr = sin->sin_addr.s_addr;
8269 	/*
8270 	 * don't match across illgrp per case (1) and (2).
8271 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8272 	 */
8273 	nce = nce_lookup_v4(ill, &ipaddr);
8274 	if (nce != NULL)
8275 		ncec = nce->nce_common;
8276 
8277 	switch (iocp->ioc_cmd) {
8278 	case SIOCDARP:
8279 	case SIOCDXARP: {
8280 		/*
8281 		 * Delete the NCE if any.
8282 		 */
8283 		if (ncec == NULL) {
8284 			iocp->ioc_error = ENXIO;
8285 			break;
8286 		}
8287 		/* Don't allow changes to arp mappings of local addresses. */
8288 		if (NCE_MYADDR(ncec)) {
8289 			nce_refrele(nce);
8290 			return (ENOTSUP);
8291 		}
8292 		iocp->ioc_error = 0;
8293 
8294 		/*
8295 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8296 		 * This will delete all the nce entries on the under_ills.
8297 		 */
8298 		ncec_delete(ncec);
8299 		/*
8300 		 * Once the NCE has been deleted, then the ire_dep* consistency
8301 		 * mechanism will find any IRE which depended on the now
8302 		 * condemned NCE (as part of sending packets).
8303 		 * That mechanism handles redirects by deleting redirects
8304 		 * that refer to UNREACHABLE nces.
8305 		 */
8306 		break;
8307 	}
8308 	case SIOCGARP:
8309 	case SIOCGXARP:
8310 		if (ncec != NULL) {
8311 			lladdr = ncec->ncec_lladdr;
8312 			flags = ncec->ncec_flags;
8313 			iocp->ioc_error = 0;
8314 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8315 		} else {
8316 			iocp->ioc_error = ENXIO;
8317 		}
8318 		break;
8319 	case SIOCSARP:
8320 	case SIOCSXARP:
8321 		/* Don't allow changes to arp mappings of local addresses. */
8322 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8323 			nce_refrele(nce);
8324 			return (ENOTSUP);
8325 		}
8326 
8327 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8328 		flags |= NCE_F_STATIC;
8329 		if (!if_arp_ioctl) {
8330 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8331 			    lladdr, alength, flags);
8332 		} else {
8333 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8334 			if (ipif != NULL) {
8335 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8336 				    lladdr, alength, flags);
8337 				ipif_refrele(ipif);
8338 			}
8339 		}
8340 		if (nce != NULL) {
8341 			nce_refrele(nce);
8342 			nce = NULL;
8343 		}
8344 		/*
8345 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8346 		 * by nce_add_common()
8347 		 */
8348 		err = nce_lookup_then_add_v4(ill, lladdr,
8349 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8350 		    &nce);
8351 		if (err == EEXIST) {
8352 			ncec = nce->nce_common;
8353 			mutex_enter(&ncec->ncec_lock);
8354 			ncec->ncec_state = ND_REACHABLE;
8355 			ncec->ncec_flags = flags;
8356 			nce_update(ncec, ND_UNCHANGED, lladdr);
8357 			mutex_exit(&ncec->ncec_lock);
8358 			err = 0;
8359 		}
8360 		if (nce != NULL) {
8361 			nce_refrele(nce);
8362 			nce = NULL;
8363 		}
8364 		if (IS_IPMP(ill) && err == 0) {
8365 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8366 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8367 			    flags);
8368 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8369 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8370 				break;
8371 			}
8372 		}
8373 		iocp->ioc_error = err;
8374 	}
8375 
8376 	if (nce != NULL) {
8377 		nce_refrele(nce);
8378 	}
8379 
8380 	/*
8381 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8382 	 */
8383 	if (entp != NULL)
8384 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8385 
8386 	return (iocp->ioc_error);
8387 }
8388 
8389 /*
8390  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8391  * the associated sin and refhold and return the associated ipif via `ci'.
8392  */
8393 int
8394 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8395     cmd_info_t *ci)
8396 {
8397 	mblk_t	*mp1;
8398 	sin_t	*sin;
8399 	conn_t	*connp;
8400 	ipif_t	*ipif;
8401 	ire_t	*ire = NULL;
8402 	ill_t	*ill = NULL;
8403 	boolean_t exists;
8404 	ip_stack_t *ipst;
8405 	struct arpreq *ar;
8406 	struct xarpreq *xar;
8407 	struct sockaddr_dl *sdl;
8408 
8409 	/* ioctl comes down on a conn */
8410 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8411 	connp = Q_TO_CONN(q);
8412 	if (connp->conn_family == AF_INET6)
8413 		return (ENXIO);
8414 
8415 	ipst = connp->conn_netstack->netstack_ip;
8416 
8417 	/* Verified in ip_wput_nondata */
8418 	mp1 = mp->b_cont->b_cont;
8419 
8420 	if (ipip->ipi_cmd_type == XARP_CMD) {
8421 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8422 		xar = (struct xarpreq *)mp1->b_rptr;
8423 		sin = (sin_t *)&xar->xarp_pa;
8424 		sdl = &xar->xarp_ha;
8425 
8426 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8427 			return (ENXIO);
8428 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8429 			return (EINVAL);
8430 	} else {
8431 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8432 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8433 		ar = (struct arpreq *)mp1->b_rptr;
8434 		sin = (sin_t *)&ar->arp_pa;
8435 	}
8436 
8437 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8438 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8439 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8440 		if (ipif == NULL)
8441 			return (ENXIO);
8442 		if (ipif->ipif_id != 0) {
8443 			ipif_refrele(ipif);
8444 			return (ENXIO);
8445 		}
8446 	} else {
8447 		/*
8448 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8449 		 * of 0: use the IP address to find the ipif.  If the IP
8450 		 * address is an IPMP test address, ire_ftable_lookup() will
8451 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8452 		 */
8453 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8454 		    ipst);
8455 		if (ipif == NULL) {
8456 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8457 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8458 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8459 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8460 				if (ire != NULL)
8461 					ire_refrele(ire);
8462 				return (ENXIO);
8463 			}
8464 			ASSERT(ire != NULL && ill != NULL);
8465 			ipif = ill->ill_ipif;
8466 			ipif_refhold(ipif);
8467 			ire_refrele(ire);
8468 		}
8469 	}
8470 
8471 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8472 		ipif_refrele(ipif);
8473 		return (ENXIO);
8474 	}
8475 
8476 	ci->ci_sin = sin;
8477 	ci->ci_ipif = ipif;
8478 	return (0);
8479 }
8480 
8481 /*
8482  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8483  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8484  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8485  * up and thus an ill can join that illgrp.
8486  *
8487  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8488  * open()/close() primarily because close() is not allowed to fail or block
8489  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8490  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8491  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8492  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8493  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8494  * state if I_UNLINK didn't occur.
8495  *
8496  * Note that for each plumb/unplumb operation, we may end up here more than
8497  * once because of the way ifconfig works.  However, it's OK to link the same
8498  * illgrp more than once, or unlink an illgrp that's already unlinked.
8499  */
8500 static int
8501 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8502 {
8503 	int err;
8504 	ip_stack_t *ipst = ill->ill_ipst;
8505 
8506 	ASSERT(IS_IPMP(ill));
8507 	ASSERT(IAM_WRITER_ILL(ill));
8508 
8509 	switch (ioccmd) {
8510 	case I_LINK:
8511 		return (ENOTSUP);
8512 
8513 	case I_PLINK:
8514 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8515 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8516 		rw_exit(&ipst->ips_ipmp_lock);
8517 		break;
8518 
8519 	case I_PUNLINK:
8520 		/*
8521 		 * Require all UP ipifs be brought down prior to unlinking the
8522 		 * illgrp so any associated IREs (and other state) is torched.
8523 		 */
8524 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8525 			return (EBUSY);
8526 
8527 		/*
8528 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8529 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8530 		 * join this group.  Specifically: ills trying to join grab
8531 		 * ipmp_lock and bump a "pending join" counter checked by
8532 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8533 		 * joins can occur (since we have ipmp_lock).  Once we drop
8534 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8535 		 * find the illgrp (since we unlinked it) and will return
8536 		 * EAFNOSUPPORT.  This will then take them back through the
8537 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8538 		 * back through I_PLINK above.
8539 		 */
8540 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8541 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8542 		rw_exit(&ipst->ips_ipmp_lock);
8543 		return (err);
8544 	default:
8545 		break;
8546 	}
8547 	return (0);
8548 }
8549 
8550 /*
8551  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8552  * atomically set/clear the muxids. Also complete the ioctl by acking or
8553  * naking it.  Note that the code is structured such that the link type,
8554  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8555  * its clones use the persistent link, while pppd(1M) and perhaps many
8556  * other daemons may use non-persistent link.  When combined with some
8557  * ill_t states, linking and unlinking lower streams may be used as
8558  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8559  */
8560 /* ARGSUSED */
8561 void
8562 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8563 {
8564 	mblk_t		*mp1;
8565 	struct linkblk	*li;
8566 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8567 	int		err = 0;
8568 
8569 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8570 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8571 
8572 	mp1 = mp->b_cont;	/* This is the linkblk info */
8573 	li = (struct linkblk *)mp1->b_rptr;
8574 
8575 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8576 	if (err == EINPROGRESS)
8577 		return;
8578 	if (err == 0)
8579 		miocack(q, mp, 0, 0);
8580 	else
8581 		miocnak(q, mp, 0, err);
8582 
8583 	/* Conn was refheld in ip_sioctl_copyin_setup */
8584 	if (CONN_Q(q)) {
8585 		CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8586 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8587 	}
8588 }
8589 
8590 /*
8591  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8592  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8593  * module stream).
8594  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8595  * an error code on failure.
8596  */
8597 static int
8598 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8599     struct linkblk *li)
8600 {
8601 	int		err = 0;
8602 	ill_t  		*ill;
8603 	queue_t		*ipwq, *dwq;
8604 	const char	*name;
8605 	struct qinit	*qinfo;
8606 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8607 	boolean_t	entered_ipsq = B_FALSE;
8608 	boolean_t	is_ip = B_FALSE;
8609 	arl_t		*arl;
8610 
8611 	/*
8612 	 * Walk the lower stream to verify it's the IP module stream.
8613 	 * The IP module is identified by its name, wput function,
8614 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8615 	 * (li->l_qbot) will not vanish until this ioctl completes.
8616 	 */
8617 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8618 		qinfo = ipwq->q_qinfo;
8619 		name = qinfo->qi_minfo->mi_idname;
8620 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8621 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8622 			is_ip = B_TRUE;
8623 			break;
8624 		}
8625 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8626 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8627 			break;
8628 		}
8629 	}
8630 
8631 	/*
8632 	 * If this isn't an IP module stream, bail.
8633 	 */
8634 	if (ipwq == NULL)
8635 		return (0);
8636 
8637 	if (!is_ip) {
8638 		arl = (arl_t *)ipwq->q_ptr;
8639 		ill = arl_to_ill(arl);
8640 		if (ill == NULL)
8641 			return (0);
8642 	} else {
8643 		ill = ipwq->q_ptr;
8644 	}
8645 	ASSERT(ill != NULL);
8646 
8647 	if (ipsq == NULL) {
8648 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8649 		    NEW_OP, B_FALSE);
8650 		if (ipsq == NULL) {
8651 			if (!is_ip)
8652 				ill_refrele(ill);
8653 			return (EINPROGRESS);
8654 		}
8655 		entered_ipsq = B_TRUE;
8656 	}
8657 	ASSERT(IAM_WRITER_ILL(ill));
8658 	mutex_enter(&ill->ill_lock);
8659 	if (!is_ip) {
8660 		if (islink && ill->ill_muxid == 0) {
8661 			/*
8662 			 * Plumbing has to be done with IP plumbed first, arp
8663 			 * second, but here we have arp being plumbed first.
8664 			 */
8665 			mutex_exit(&ill->ill_lock);
8666 			if (entered_ipsq)
8667 				ipsq_exit(ipsq);
8668 			ill_refrele(ill);
8669 			return (EINVAL);
8670 		}
8671 	}
8672 	mutex_exit(&ill->ill_lock);
8673 	if (!is_ip) {
8674 		arl->arl_muxid = islink ? li->l_index : 0;
8675 		ill_refrele(ill);
8676 		goto done;
8677 	}
8678 
8679 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8680 		goto done;
8681 
8682 	/*
8683 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8684 	 * the read queue of the module immediately below IP in the ill.
8685 	 * These are used during the capability negotiation below.
8686 	 */
8687 	ill->ill_lmod_rq = NULL;
8688 	ill->ill_lmod_cnt = 0;
8689 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8690 		ill->ill_lmod_rq = RD(dwq);
8691 		for (; dwq != NULL; dwq = dwq->q_next)
8692 			ill->ill_lmod_cnt++;
8693 	}
8694 
8695 	ill->ill_muxid = islink ? li->l_index : 0;
8696 
8697 	/*
8698 	 * Mark the ipsq busy until the capability operations initiated below
8699 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8700 	 * returns, but the capability operation may complete asynchronously
8701 	 * much later.
8702 	 */
8703 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8704 	/*
8705 	 * If there's at least one up ipif on this ill, then we're bound to
8706 	 * the underlying driver via DLPI.  In that case, renegotiate
8707 	 * capabilities to account for any possible change in modules
8708 	 * interposed between IP and the driver.
8709 	 */
8710 	if (ill->ill_ipif_up_count > 0) {
8711 		if (islink)
8712 			ill_capability_probe(ill);
8713 		else
8714 			ill_capability_reset(ill, B_FALSE);
8715 	}
8716 	ipsq_current_finish(ipsq);
8717 done:
8718 	if (entered_ipsq)
8719 		ipsq_exit(ipsq);
8720 
8721 	return (err);
8722 }
8723 
8724 /*
8725  * Search the ioctl command in the ioctl tables and return a pointer
8726  * to the ioctl command information. The ioctl command tables are
8727  * static and fully populated at compile time.
8728  */
8729 ip_ioctl_cmd_t *
8730 ip_sioctl_lookup(int ioc_cmd)
8731 {
8732 	int index;
8733 	ip_ioctl_cmd_t *ipip;
8734 	ip_ioctl_cmd_t *ipip_end;
8735 
8736 	if (ioc_cmd == IPI_DONTCARE)
8737 		return (NULL);
8738 
8739 	/*
8740 	 * Do a 2 step search. First search the indexed table
8741 	 * based on the least significant byte of the ioctl cmd.
8742 	 * If we don't find a match, then search the misc table
8743 	 * serially.
8744 	 */
8745 	index = ioc_cmd & 0xFF;
8746 	if (index < ip_ndx_ioctl_count) {
8747 		ipip = &ip_ndx_ioctl_table[index];
8748 		if (ipip->ipi_cmd == ioc_cmd) {
8749 			/* Found a match in the ndx table */
8750 			return (ipip);
8751 		}
8752 	}
8753 
8754 	/* Search the misc table */
8755 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8756 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8757 		if (ipip->ipi_cmd == ioc_cmd)
8758 			/* Found a match in the misc table */
8759 			return (ipip);
8760 	}
8761 
8762 	return (NULL);
8763 }
8764 
8765 /*
8766  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8767  */
8768 static boolean_t
8769 getset_ioctl_checks(mblk_t *mp)
8770 {
8771 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8772 	mblk_t		*mp1 = mp->b_cont;
8773 	mod_ioc_prop_t	*pioc;
8774 	uint_t		flags;
8775 	uint_t		pioc_size;
8776 
8777 	/* do sanity checks on various arguments */
8778 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8779 	    iocp->ioc_count == TRANSPARENT) {
8780 		return (B_FALSE);
8781 	}
8782 	if (msgdsize(mp1) < iocp->ioc_count) {
8783 		if (!pullupmsg(mp1, iocp->ioc_count))
8784 			return (B_FALSE);
8785 	}
8786 
8787 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8788 
8789 	/* sanity checks on mpr_valsize */
8790 	pioc_size = sizeof (mod_ioc_prop_t);
8791 	if (pioc->mpr_valsize != 0)
8792 		pioc_size += pioc->mpr_valsize - 1;
8793 
8794 	if (iocp->ioc_count != pioc_size)
8795 		return (B_FALSE);
8796 
8797 	flags = pioc->mpr_flags;
8798 	if (iocp->ioc_cmd == SIOCSETPROP) {
8799 		/*
8800 		 * One can either reset the value to it's default value or
8801 		 * change the current value or append/remove the value from
8802 		 * a multi-valued properties.
8803 		 */
8804 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8805 		    flags != MOD_PROP_ACTIVE &&
8806 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8807 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8808 			return (B_FALSE);
8809 	} else {
8810 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8811 
8812 		/*
8813 		 * One can retrieve only one kind of property information
8814 		 * at a time.
8815 		 */
8816 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8817 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8818 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8819 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8820 			return (B_FALSE);
8821 	}
8822 
8823 	return (B_TRUE);
8824 }
8825 
8826 /*
8827  * process the SIOC{SET|GET}PROP ioctl's
8828  */
8829 /* ARGSUSED */
8830 static void
8831 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8832 {
8833 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8834 	mblk_t		*mp1 = mp->b_cont;
8835 	mod_ioc_prop_t	*pioc;
8836 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8837 	ip_stack_t	*ipst;
8838 	icmp_stack_t	*is;
8839 	tcp_stack_t	*tcps;
8840 	sctp_stack_t	*sctps;
8841 	udp_stack_t	*us;
8842 	netstack_t	*stack;
8843 	void		*cbarg;
8844 	cred_t		*cr;
8845 	boolean_t 	set;
8846 	int		err;
8847 
8848 	ASSERT(q->q_next == NULL);
8849 	ASSERT(CONN_Q(q));
8850 
8851 	if (!getset_ioctl_checks(mp)) {
8852 		miocnak(q, mp, 0, EINVAL);
8853 		return;
8854 	}
8855 	ipst = CONNQ_TO_IPST(q);
8856 	stack = ipst->ips_netstack;
8857 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8858 
8859 	switch (pioc->mpr_proto) {
8860 	case MOD_PROTO_IP:
8861 	case MOD_PROTO_IPV4:
8862 	case MOD_PROTO_IPV6:
8863 		ptbl = ipst->ips_propinfo_tbl;
8864 		cbarg = ipst;
8865 		break;
8866 	case MOD_PROTO_RAWIP:
8867 		is = stack->netstack_icmp;
8868 		ptbl = is->is_propinfo_tbl;
8869 		cbarg = is;
8870 		break;
8871 	case MOD_PROTO_TCP:
8872 		tcps = stack->netstack_tcp;
8873 		ptbl = tcps->tcps_propinfo_tbl;
8874 		cbarg = tcps;
8875 		break;
8876 	case MOD_PROTO_UDP:
8877 		us = stack->netstack_udp;
8878 		ptbl = us->us_propinfo_tbl;
8879 		cbarg = us;
8880 		break;
8881 	case MOD_PROTO_SCTP:
8882 		sctps = stack->netstack_sctp;
8883 		ptbl = sctps->sctps_propinfo_tbl;
8884 		cbarg = sctps;
8885 		break;
8886 	default:
8887 		miocnak(q, mp, 0, EINVAL);
8888 		return;
8889 	}
8890 
8891 	/* search for given property in respective protocol propinfo table */
8892 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8893 		if (strcmp(pinfo->mpi_name, pioc->mpr_name) == 0 &&
8894 		    pinfo->mpi_proto == pioc->mpr_proto)
8895 			break;
8896 	}
8897 	if (pinfo->mpi_name == NULL) {
8898 		miocnak(q, mp, 0, ENOENT);
8899 		return;
8900 	}
8901 
8902 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8903 	if (set && pinfo->mpi_setf != NULL) {
8904 		cr = msg_getcred(mp, NULL);
8905 		if (cr == NULL)
8906 			cr = iocp->ioc_cr;
8907 		err = pinfo->mpi_setf(cbarg, cr, pinfo, pioc->mpr_ifname,
8908 		    pioc->mpr_val, pioc->mpr_flags);
8909 	} else if (!set && pinfo->mpi_getf != NULL) {
8910 		err = pinfo->mpi_getf(cbarg, pinfo, pioc->mpr_ifname,
8911 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8912 	} else {
8913 		err = EPERM;
8914 	}
8915 
8916 	if (err != 0) {
8917 		miocnak(q, mp, 0, err);
8918 	} else {
8919 		if (set)
8920 			miocack(q, mp, 0, 0);
8921 		else    /* For get, we need to return back the data */
8922 			miocack(q, mp, iocp->ioc_count, 0);
8923 	}
8924 }
8925 
8926 /*
8927  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8928  * as several routing daemons have unfortunately used this 'unpublished'
8929  * but well-known ioctls.
8930  */
8931 /* ARGSUSED */
8932 static void
8933 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8934 {
8935 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8936 	mblk_t		*mp1 = mp->b_cont;
8937 	char		*pname, *pval, *buf;
8938 	uint_t		bufsize, proto;
8939 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8940 	ip_stack_t	*ipst;
8941 	int		err = 0;
8942 
8943 	ASSERT(CONN_Q(q));
8944 	ipst = CONNQ_TO_IPST(q);
8945 
8946 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8947 		miocnak(q, mp, 0, EINVAL);
8948 		return;
8949 	}
8950 
8951 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8952 	pval = buf = pname = (char *)mp1->b_rptr;
8953 	bufsize = MBLKL(mp1);
8954 
8955 	if (strcmp(pname, "ip_forwarding") == 0) {
8956 		pname = "forwarding";
8957 		proto = MOD_PROTO_IPV4;
8958 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8959 		pname = "forwarding";
8960 		proto = MOD_PROTO_IPV6;
8961 	} else {
8962 		miocnak(q, mp, 0, EINVAL);
8963 		return;
8964 	}
8965 
8966 	ptbl = ipst->ips_propinfo_tbl;
8967 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8968 		if (strcmp(pinfo->mpi_name, pname) == 0 &&
8969 		    pinfo->mpi_proto == proto)
8970 			break;
8971 	}
8972 
8973 	ASSERT(pinfo->mpi_name != NULL);
8974 
8975 	switch (iocp->ioc_cmd) {
8976 	case ND_GET:
8977 		if ((err = pinfo->mpi_getf(ipst, pinfo, NULL, buf, bufsize,
8978 		    0)) == 0) {
8979 			miocack(q, mp, iocp->ioc_count, 0);
8980 			return;
8981 		}
8982 		break;
8983 	case ND_SET:
8984 		/*
8985 		 * buffer will have property name and value in the following
8986 		 * format,
8987 		 * <property name>'\0'<property value>'\0', extract them;
8988 		 */
8989 		while (*pval++)
8990 			noop;
8991 
8992 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8993 			err = EINVAL;
8994 		} else if ((err = pinfo->mpi_setf(ipst, NULL, pinfo, NULL,
8995 		    pval, 0)) == 0) {
8996 			miocack(q, mp, 0, 0);
8997 			return;
8998 		}
8999 		break;
9000 	default:
9001 		err = EINVAL;
9002 		break;
9003 	}
9004 	miocnak(q, mp, 0, err);
9005 }
9006 
9007 /*
9008  * Wrapper function for resuming deferred ioctl processing
9009  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9010  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9011  */
9012 /* ARGSUSED */
9013 void
9014 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9015     void *dummy_arg)
9016 {
9017 	ip_sioctl_copyin_setup(q, mp);
9018 }
9019 
9020 /*
9021  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9022  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9023  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9024  * We establish here the size of the block to be copied in.  mi_copyin
9025  * arranges for this to happen, an processing continues in ip_wput_nondata with
9026  * an M_IOCDATA message.
9027  */
9028 void
9029 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9030 {
9031 	int	copyin_size;
9032 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9033 	ip_ioctl_cmd_t *ipip;
9034 	cred_t *cr;
9035 	ip_stack_t	*ipst;
9036 
9037 	if (CONN_Q(q))
9038 		ipst = CONNQ_TO_IPST(q);
9039 	else
9040 		ipst = ILLQ_TO_IPST(q);
9041 
9042 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9043 	if (ipip == NULL) {
9044 		/*
9045 		 * The ioctl is not one we understand or own.
9046 		 * Pass it along to be processed down stream,
9047 		 * if this is a module instance of IP, else nak
9048 		 * the ioctl.
9049 		 */
9050 		if (q->q_next == NULL) {
9051 			goto nak;
9052 		} else {
9053 			putnext(q, mp);
9054 			return;
9055 		}
9056 	}
9057 
9058 	/*
9059 	 * If this is deferred, then we will do all the checks when we
9060 	 * come back.
9061 	 */
9062 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9063 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9064 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9065 		return;
9066 	}
9067 
9068 	/*
9069 	 * Only allow a very small subset of IP ioctls on this stream if
9070 	 * IP is a module and not a driver. Allowing ioctls to be processed
9071 	 * in this case may cause assert failures or data corruption.
9072 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9073 	 * ioctls allowed on an IP module stream, after which this stream
9074 	 * normally becomes a multiplexor (at which time the stream head
9075 	 * will fail all ioctls).
9076 	 */
9077 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9078 		goto nak;
9079 	}
9080 
9081 	/* Make sure we have ioctl data to process. */
9082 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9083 		goto nak;
9084 
9085 	/*
9086 	 * Prefer dblk credential over ioctl credential; some synthesized
9087 	 * ioctls have kcred set because there's no way to crhold()
9088 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9089 	 * the framework; the caller of ioctl needs to hold the reference
9090 	 * for the duration of the call).
9091 	 */
9092 	cr = msg_getcred(mp, NULL);
9093 	if (cr == NULL)
9094 		cr = iocp->ioc_cr;
9095 
9096 	/* Make sure normal users don't send down privileged ioctls */
9097 	if ((ipip->ipi_flags & IPI_PRIV) &&
9098 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9099 		/* We checked the privilege earlier but log it here */
9100 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9101 		return;
9102 	}
9103 
9104 	/*
9105 	 * The ioctl command tables can only encode fixed length
9106 	 * ioctl data. If the length is variable, the table will
9107 	 * encode the length as zero. Such special cases are handled
9108 	 * below in the switch.
9109 	 */
9110 	if (ipip->ipi_copyin_size != 0) {
9111 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9112 		return;
9113 	}
9114 
9115 	switch (iocp->ioc_cmd) {
9116 	case O_SIOCGIFCONF:
9117 	case SIOCGIFCONF:
9118 		/*
9119 		 * This IOCTL is hilarious.  See comments in
9120 		 * ip_sioctl_get_ifconf for the story.
9121 		 */
9122 		if (iocp->ioc_count == TRANSPARENT)
9123 			copyin_size = SIZEOF_STRUCT(ifconf,
9124 			    iocp->ioc_flag);
9125 		else
9126 			copyin_size = iocp->ioc_count;
9127 		mi_copyin(q, mp, NULL, copyin_size);
9128 		return;
9129 
9130 	case O_SIOCGLIFCONF:
9131 	case SIOCGLIFCONF:
9132 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9133 		mi_copyin(q, mp, NULL, copyin_size);
9134 		return;
9135 
9136 	case SIOCGLIFSRCOF:
9137 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9138 		mi_copyin(q, mp, NULL, copyin_size);
9139 		return;
9140 
9141 	case SIOCGIP6ADDRPOLICY:
9142 		ip_sioctl_ip6addrpolicy(q, mp);
9143 		ip6_asp_table_refrele(ipst);
9144 		return;
9145 
9146 	case SIOCSIP6ADDRPOLICY:
9147 		ip_sioctl_ip6addrpolicy(q, mp);
9148 		return;
9149 
9150 	case SIOCGDSTINFO:
9151 		ip_sioctl_dstinfo(q, mp);
9152 		ip6_asp_table_refrele(ipst);
9153 		return;
9154 
9155 	case ND_SET:
9156 	case ND_GET:
9157 		ip_process_legacy_nddprop(q, mp);
9158 		return;
9159 
9160 	case SIOCSETPROP:
9161 	case SIOCGETPROP:
9162 		ip_sioctl_getsetprop(q, mp);
9163 		return;
9164 
9165 	case I_PLINK:
9166 	case I_PUNLINK:
9167 	case I_LINK:
9168 	case I_UNLINK:
9169 		/*
9170 		 * We treat non-persistent link similarly as the persistent
9171 		 * link case, in terms of plumbing/unplumbing, as well as
9172 		 * dynamic re-plumbing events indicator.  See comments
9173 		 * in ip_sioctl_plink() for more.
9174 		 *
9175 		 * Request can be enqueued in the 'ipsq' while waiting
9176 		 * to become exclusive. So bump up the conn ref.
9177 		 */
9178 		if (CONN_Q(q)) {
9179 			CONN_INC_REF(Q_TO_CONN(q));
9180 			CONN_INC_IOCTLREF(Q_TO_CONN(q))
9181 		}
9182 		ip_sioctl_plink(NULL, q, mp, NULL);
9183 		return;
9184 
9185 	case IP_IOCTL:
9186 		ip_wput_ioctl(q, mp);
9187 		return;
9188 
9189 	case SIOCILB:
9190 		/* The ioctl length varies depending on the ILB command. */
9191 		copyin_size = iocp->ioc_count;
9192 		if (copyin_size < sizeof (ilb_cmd_t))
9193 			goto nak;
9194 		mi_copyin(q, mp, NULL, copyin_size);
9195 		return;
9196 
9197 	default:
9198 		cmn_err(CE_PANIC, "should not happen ");
9199 	}
9200 nak:
9201 	if (mp->b_cont != NULL) {
9202 		freemsg(mp->b_cont);
9203 		mp->b_cont = NULL;
9204 	}
9205 	iocp->ioc_error = EINVAL;
9206 	mp->b_datap->db_type = M_IOCNAK;
9207 	iocp->ioc_count = 0;
9208 	qreply(q, mp);
9209 }
9210 
9211 static void
9212 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9213 {
9214 	struct arpreq *ar;
9215 	struct xarpreq *xar;
9216 	mblk_t	*tmp;
9217 	struct iocblk *iocp;
9218 	int x_arp_ioctl = B_FALSE;
9219 	int *flagsp;
9220 	char *storage = NULL;
9221 
9222 	ASSERT(ill != NULL);
9223 
9224 	iocp = (struct iocblk *)mp->b_rptr;
9225 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9226 
9227 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9228 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9229 	    (iocp->ioc_cmd == SIOCSXARP)) {
9230 		x_arp_ioctl = B_TRUE;
9231 		xar = (struct xarpreq *)tmp->b_rptr;
9232 		flagsp = &xar->xarp_flags;
9233 		storage = xar->xarp_ha.sdl_data;
9234 	} else {
9235 		ar = (struct arpreq *)tmp->b_rptr;
9236 		flagsp = &ar->arp_flags;
9237 		storage = ar->arp_ha.sa_data;
9238 	}
9239 
9240 	/*
9241 	 * We're done if this is not an SIOCG{X}ARP
9242 	 */
9243 	if (x_arp_ioctl) {
9244 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9245 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9246 		    sizeof (xar->xarp_ha.sdl_data)) {
9247 			iocp->ioc_error = EINVAL;
9248 			return;
9249 		}
9250 	}
9251 	*flagsp = ATF_INUSE;
9252 	/*
9253 	 * If /sbin/arp told us we are the authority using the "permanent"
9254 	 * flag, or if this is one of my addresses print "permanent"
9255 	 * in the /sbin/arp output.
9256 	 */
9257 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9258 		*flagsp |= ATF_AUTHORITY;
9259 	if (flags & NCE_F_NONUD)
9260 		*flagsp |= ATF_PERM; /* not subject to aging */
9261 	if (flags & NCE_F_PUBLISH)
9262 		*flagsp |= ATF_PUBL;
9263 	if (hwaddr != NULL) {
9264 		*flagsp |= ATF_COM;
9265 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9266 	}
9267 }
9268 
9269 /*
9270  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9271  * interface) create the next available logical interface for this
9272  * physical interface.
9273  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9274  * ipif with the specified name.
9275  *
9276  * If the address family is not AF_UNSPEC then set the address as well.
9277  *
9278  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9279  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9280  *
9281  * Executed as a writer on the ill.
9282  * So no lock is needed to traverse the ipif chain, or examine the
9283  * phyint flags.
9284  */
9285 /* ARGSUSED */
9286 int
9287 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9288     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9289 {
9290 	mblk_t	*mp1;
9291 	struct lifreq *lifr;
9292 	boolean_t	isv6;
9293 	boolean_t	exists;
9294 	char 	*name;
9295 	char	*endp;
9296 	char	*cp;
9297 	int	namelen;
9298 	ipif_t	*ipif;
9299 	long	id;
9300 	ipsq_t	*ipsq;
9301 	ill_t	*ill;
9302 	sin_t	*sin;
9303 	int	err = 0;
9304 	boolean_t found_sep = B_FALSE;
9305 	conn_t	*connp;
9306 	zoneid_t zoneid;
9307 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9308 
9309 	ASSERT(q->q_next == NULL);
9310 	ip1dbg(("ip_sioctl_addif\n"));
9311 	/* Existence of mp1 has been checked in ip_wput_nondata */
9312 	mp1 = mp->b_cont->b_cont;
9313 	/*
9314 	 * Null terminate the string to protect against buffer
9315 	 * overrun. String was generated by user code and may not
9316 	 * be trusted.
9317 	 */
9318 	lifr = (struct lifreq *)mp1->b_rptr;
9319 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9320 	name = lifr->lifr_name;
9321 	ASSERT(CONN_Q(q));
9322 	connp = Q_TO_CONN(q);
9323 	isv6 = (connp->conn_family == AF_INET6);
9324 	zoneid = connp->conn_zoneid;
9325 	namelen = mi_strlen(name);
9326 	if (namelen == 0)
9327 		return (EINVAL);
9328 
9329 	exists = B_FALSE;
9330 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9331 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9332 		/*
9333 		 * Allow creating lo0 using SIOCLIFADDIF.
9334 		 * can't be any other writer thread. So can pass null below
9335 		 * for the last 4 args to ipif_lookup_name.
9336 		 */
9337 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9338 		    &exists, isv6, zoneid, ipst);
9339 		/* Prevent any further action */
9340 		if (ipif == NULL) {
9341 			return (ENOBUFS);
9342 		} else if (!exists) {
9343 			/* We created the ipif now and as writer */
9344 			ipif_refrele(ipif);
9345 			return (0);
9346 		} else {
9347 			ill = ipif->ipif_ill;
9348 			ill_refhold(ill);
9349 			ipif_refrele(ipif);
9350 		}
9351 	} else {
9352 		/* Look for a colon in the name. */
9353 		endp = &name[namelen];
9354 		for (cp = endp; --cp > name; ) {
9355 			if (*cp == IPIF_SEPARATOR_CHAR) {
9356 				found_sep = B_TRUE;
9357 				/*
9358 				 * Reject any non-decimal aliases for plumbing
9359 				 * of logical interfaces. Aliases with leading
9360 				 * zeroes are also rejected as they introduce
9361 				 * ambiguity in the naming of the interfaces.
9362 				 * Comparing with "0" takes care of all such
9363 				 * cases.
9364 				 */
9365 				if ((strncmp("0", cp+1, 1)) == 0)
9366 					return (EINVAL);
9367 
9368 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9369 				    id <= 0 || *endp != '\0') {
9370 					return (EINVAL);
9371 				}
9372 				*cp = '\0';
9373 				break;
9374 			}
9375 		}
9376 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9377 		if (found_sep)
9378 			*cp = IPIF_SEPARATOR_CHAR;
9379 		if (ill == NULL)
9380 			return (ENXIO);
9381 	}
9382 
9383 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9384 	    B_TRUE);
9385 
9386 	/*
9387 	 * Release the refhold due to the lookup, now that we are excl
9388 	 * or we are just returning
9389 	 */
9390 	ill_refrele(ill);
9391 
9392 	if (ipsq == NULL)
9393 		return (EINPROGRESS);
9394 
9395 	/* We are now exclusive on the IPSQ */
9396 	ASSERT(IAM_WRITER_ILL(ill));
9397 
9398 	if (found_sep) {
9399 		/* Now see if there is an IPIF with this unit number. */
9400 		for (ipif = ill->ill_ipif; ipif != NULL;
9401 		    ipif = ipif->ipif_next) {
9402 			if (ipif->ipif_id == id) {
9403 				err = EEXIST;
9404 				goto done;
9405 			}
9406 		}
9407 	}
9408 
9409 	/*
9410 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9411 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9412 	 * instead.
9413 	 */
9414 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9415 	    B_TRUE, B_TRUE, &err)) == NULL) {
9416 		goto done;
9417 	}
9418 
9419 	/* Return created name with ioctl */
9420 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9421 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9422 	ip1dbg(("created %s\n", lifr->lifr_name));
9423 
9424 	/* Set address */
9425 	sin = (sin_t *)&lifr->lifr_addr;
9426 	if (sin->sin_family != AF_UNSPEC) {
9427 		err = ip_sioctl_addr(ipif, sin, q, mp,
9428 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9429 	}
9430 
9431 done:
9432 	ipsq_exit(ipsq);
9433 	return (err);
9434 }
9435 
9436 /*
9437  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9438  * interface) delete it based on the IP address (on this physical interface).
9439  * Otherwise delete it based on the ipif_id.
9440  * Also, special handling to allow a removeif of lo0.
9441  */
9442 /* ARGSUSED */
9443 int
9444 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9445     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9446 {
9447 	conn_t		*connp;
9448 	ill_t		*ill = ipif->ipif_ill;
9449 	boolean_t	 success;
9450 	ip_stack_t	*ipst;
9451 
9452 	ipst = CONNQ_TO_IPST(q);
9453 
9454 	ASSERT(q->q_next == NULL);
9455 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9456 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9457 	ASSERT(IAM_WRITER_IPIF(ipif));
9458 
9459 	connp = Q_TO_CONN(q);
9460 	/*
9461 	 * Special case for unplumbing lo0 (the loopback physical interface).
9462 	 * If unplumbing lo0, the incoming address structure has been
9463 	 * initialized to all zeros. When unplumbing lo0, all its logical
9464 	 * interfaces must be removed too.
9465 	 *
9466 	 * Note that this interface may be called to remove a specific
9467 	 * loopback logical interface (eg, lo0:1). But in that case
9468 	 * ipif->ipif_id != 0 so that the code path for that case is the
9469 	 * same as any other interface (meaning it skips the code directly
9470 	 * below).
9471 	 */
9472 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9473 		if (sin->sin_family == AF_UNSPEC &&
9474 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9475 			/*
9476 			 * Mark it condemned. No new ref. will be made to ill.
9477 			 */
9478 			mutex_enter(&ill->ill_lock);
9479 			ill->ill_state_flags |= ILL_CONDEMNED;
9480 			for (ipif = ill->ill_ipif; ipif != NULL;
9481 			    ipif = ipif->ipif_next) {
9482 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9483 			}
9484 			mutex_exit(&ill->ill_lock);
9485 
9486 			ipif = ill->ill_ipif;
9487 			/* unplumb the loopback interface */
9488 			ill_delete(ill);
9489 			mutex_enter(&connp->conn_lock);
9490 			mutex_enter(&ill->ill_lock);
9491 
9492 			/* Are any references to this ill active */
9493 			if (ill_is_freeable(ill)) {
9494 				mutex_exit(&ill->ill_lock);
9495 				mutex_exit(&connp->conn_lock);
9496 				ill_delete_tail(ill);
9497 				mi_free(ill);
9498 				return (0);
9499 			}
9500 			success = ipsq_pending_mp_add(connp, ipif,
9501 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9502 			mutex_exit(&connp->conn_lock);
9503 			mutex_exit(&ill->ill_lock);
9504 			if (success)
9505 				return (EINPROGRESS);
9506 			else
9507 				return (EINTR);
9508 		}
9509 	}
9510 
9511 	if (ipif->ipif_id == 0) {
9512 		ipsq_t *ipsq;
9513 
9514 		/* Find based on address */
9515 		if (ipif->ipif_isv6) {
9516 			sin6_t *sin6;
9517 
9518 			if (sin->sin_family != AF_INET6)
9519 				return (EAFNOSUPPORT);
9520 
9521 			sin6 = (sin6_t *)sin;
9522 			/* We are a writer, so we should be able to lookup */
9523 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9524 			    ipst);
9525 		} else {
9526 			if (sin->sin_family != AF_INET)
9527 				return (EAFNOSUPPORT);
9528 
9529 			/* We are a writer, so we should be able to lookup */
9530 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9531 			    ipst);
9532 		}
9533 		if (ipif == NULL) {
9534 			return (EADDRNOTAVAIL);
9535 		}
9536 
9537 		/*
9538 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9539 		 * lifr_name of the physical interface but with an ip address
9540 		 * lifr_addr of a logical interface plumbed over it.
9541 		 * So update ipx_current_ipif now that ipif points to the
9542 		 * correct one.
9543 		 */
9544 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9545 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9546 
9547 		/* This is a writer */
9548 		ipif_refrele(ipif);
9549 	}
9550 
9551 	/*
9552 	 * Can not delete instance zero since it is tied to the ill.
9553 	 */
9554 	if (ipif->ipif_id == 0)
9555 		return (EBUSY);
9556 
9557 	mutex_enter(&ill->ill_lock);
9558 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9559 	mutex_exit(&ill->ill_lock);
9560 
9561 	ipif_free(ipif);
9562 
9563 	mutex_enter(&connp->conn_lock);
9564 	mutex_enter(&ill->ill_lock);
9565 
9566 	/* Are any references to this ipif active */
9567 	if (ipif_is_freeable(ipif)) {
9568 		mutex_exit(&ill->ill_lock);
9569 		mutex_exit(&connp->conn_lock);
9570 		ipif_non_duplicate(ipif);
9571 		(void) ipif_down_tail(ipif);
9572 		ipif_free_tail(ipif); /* frees ipif */
9573 		return (0);
9574 	}
9575 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9576 	    IPIF_FREE);
9577 	mutex_exit(&ill->ill_lock);
9578 	mutex_exit(&connp->conn_lock);
9579 	if (success)
9580 		return (EINPROGRESS);
9581 	else
9582 		return (EINTR);
9583 }
9584 
9585 /*
9586  * Restart the removeif ioctl. The refcnt has gone down to 0.
9587  * The ipif is already condemned. So can't find it thru lookups.
9588  */
9589 /* ARGSUSED */
9590 int
9591 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9592     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9593 {
9594 	ill_t *ill = ipif->ipif_ill;
9595 
9596 	ASSERT(IAM_WRITER_IPIF(ipif));
9597 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9598 
9599 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9600 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9601 
9602 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9603 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9604 		ill_delete_tail(ill);
9605 		mi_free(ill);
9606 		return (0);
9607 	}
9608 
9609 	ipif_non_duplicate(ipif);
9610 	(void) ipif_down_tail(ipif);
9611 	ipif_free_tail(ipif);
9612 
9613 	return (0);
9614 }
9615 
9616 /*
9617  * Set the local interface address using the given prefix and ill_token.
9618  */
9619 /* ARGSUSED */
9620 int
9621 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9622     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9623 {
9624 	int err;
9625 	in6_addr_t v6addr;
9626 	sin6_t *sin6;
9627 	ill_t *ill;
9628 	int i;
9629 
9630 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9631 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9632 
9633 	ASSERT(IAM_WRITER_IPIF(ipif));
9634 
9635 	if (!ipif->ipif_isv6)
9636 		return (EINVAL);
9637 
9638 	if (sin->sin_family != AF_INET6)
9639 		return (EAFNOSUPPORT);
9640 
9641 	sin6 = (sin6_t *)sin;
9642 	v6addr = sin6->sin6_addr;
9643 	ill = ipif->ipif_ill;
9644 
9645 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9646 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9647 		return (EADDRNOTAVAIL);
9648 
9649 	for (i = 0; i < 4; i++)
9650 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9651 
9652 	err = ip_sioctl_addr(ipif, sin, q, mp,
9653 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9654 	return (err);
9655 }
9656 
9657 /*
9658  * Restart entry point to restart the address set operation after the
9659  * refcounts have dropped to zero.
9660  */
9661 /* ARGSUSED */
9662 int
9663 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9664     ip_ioctl_cmd_t *ipip, void *ifreq)
9665 {
9666 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9668 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9669 }
9670 
9671 /*
9672  * Set the local interface address.
9673  * Allow an address of all zero when the interface is down.
9674  */
9675 /* ARGSUSED */
9676 int
9677 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9678     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9679 {
9680 	int err = 0;
9681 	in6_addr_t v6addr;
9682 	boolean_t need_up = B_FALSE;
9683 	ill_t *ill;
9684 	int i;
9685 
9686 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9688 
9689 	ASSERT(IAM_WRITER_IPIF(ipif));
9690 
9691 	ill = ipif->ipif_ill;
9692 	if (ipif->ipif_isv6) {
9693 		sin6_t *sin6;
9694 		phyint_t *phyi;
9695 
9696 		if (sin->sin_family != AF_INET6)
9697 			return (EAFNOSUPPORT);
9698 
9699 		sin6 = (sin6_t *)sin;
9700 		v6addr = sin6->sin6_addr;
9701 		phyi = ill->ill_phyint;
9702 
9703 		/*
9704 		 * Enforce that true multicast interfaces have a link-local
9705 		 * address for logical unit 0.
9706 		 *
9707 		 * However for those ipif's for which link-local address was
9708 		 * not created by default, also allow setting :: as the address.
9709 		 * This scenario would arise, when we delete an address on ipif
9710 		 * with logical unit 0, we would want to set :: as the address.
9711 		 */
9712 		if (ipif->ipif_id == 0 &&
9713 		    (ill->ill_flags & ILLF_MULTICAST) &&
9714 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9715 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9716 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9717 
9718 			/*
9719 			 * if default link-local was not created by kernel for
9720 			 * this ill, allow setting :: as the address on ipif:0.
9721 			 */
9722 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9723 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9724 					return (EADDRNOTAVAIL);
9725 			} else {
9726 				return (EADDRNOTAVAIL);
9727 			}
9728 		}
9729 
9730 		/*
9731 		 * up interfaces shouldn't have the unspecified address
9732 		 * unless they also have the IPIF_NOLOCAL flags set and
9733 		 * have a subnet assigned.
9734 		 */
9735 		if ((ipif->ipif_flags & IPIF_UP) &&
9736 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9737 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9738 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9739 			return (EADDRNOTAVAIL);
9740 		}
9741 
9742 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9743 			return (EADDRNOTAVAIL);
9744 	} else {
9745 		ipaddr_t addr;
9746 
9747 		if (sin->sin_family != AF_INET)
9748 			return (EAFNOSUPPORT);
9749 
9750 		addr = sin->sin_addr.s_addr;
9751 
9752 		/* Allow INADDR_ANY as the local address. */
9753 		if (addr != INADDR_ANY &&
9754 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9755 			return (EADDRNOTAVAIL);
9756 
9757 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9758 	}
9759 	/*
9760 	 * verify that the address being configured is permitted by the
9761 	 * ill_allowed_ips[] for the interface.
9762 	 */
9763 	if (ill->ill_allowed_ips_cnt > 0) {
9764 		for (i = 0; i < ill->ill_allowed_ips_cnt; i++) {
9765 			if (IN6_ARE_ADDR_EQUAL(&ill->ill_allowed_ips[i],
9766 			    &v6addr))
9767 				break;
9768 		}
9769 		if (i == ill->ill_allowed_ips_cnt) {
9770 			pr_addr_dbg("!allowed addr %s\n", AF_INET6, &v6addr);
9771 			return (EPERM);
9772 		}
9773 	}
9774 	/*
9775 	 * Even if there is no change we redo things just to rerun
9776 	 * ipif_set_default.
9777 	 */
9778 	if (ipif->ipif_flags & IPIF_UP) {
9779 		/*
9780 		 * Setting a new local address, make sure
9781 		 * we have net and subnet bcast ire's for
9782 		 * the old address if we need them.
9783 		 */
9784 		/*
9785 		 * If the interface is already marked up,
9786 		 * we call ipif_down which will take care
9787 		 * of ditching any IREs that have been set
9788 		 * up based on the old interface address.
9789 		 */
9790 		err = ipif_logical_down(ipif, q, mp);
9791 		if (err == EINPROGRESS)
9792 			return (err);
9793 		(void) ipif_down_tail(ipif);
9794 		need_up = 1;
9795 	}
9796 
9797 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9798 	return (err);
9799 }
9800 
9801 int
9802 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9803     boolean_t need_up)
9804 {
9805 	in6_addr_t v6addr;
9806 	in6_addr_t ov6addr;
9807 	ipaddr_t addr;
9808 	sin6_t	*sin6;
9809 	int	sinlen;
9810 	int	err = 0;
9811 	ill_t	*ill = ipif->ipif_ill;
9812 	boolean_t need_dl_down;
9813 	boolean_t need_arp_down;
9814 	struct iocblk *iocp;
9815 
9816 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9817 
9818 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9819 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9820 	ASSERT(IAM_WRITER_IPIF(ipif));
9821 
9822 	/* Must cancel any pending timer before taking the ill_lock */
9823 	if (ipif->ipif_recovery_id != 0)
9824 		(void) untimeout(ipif->ipif_recovery_id);
9825 	ipif->ipif_recovery_id = 0;
9826 
9827 	if (ipif->ipif_isv6) {
9828 		sin6 = (sin6_t *)sin;
9829 		v6addr = sin6->sin6_addr;
9830 		sinlen = sizeof (struct sockaddr_in6);
9831 	} else {
9832 		addr = sin->sin_addr.s_addr;
9833 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9834 		sinlen = sizeof (struct sockaddr_in);
9835 	}
9836 	mutex_enter(&ill->ill_lock);
9837 	ov6addr = ipif->ipif_v6lcl_addr;
9838 	ipif->ipif_v6lcl_addr = v6addr;
9839 	sctp_update_ipif_addr(ipif, ov6addr);
9840 	ipif->ipif_addr_ready = 0;
9841 
9842 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9843 
9844 	/*
9845 	 * If the interface was previously marked as a duplicate, then since
9846 	 * we've now got a "new" address, it should no longer be considered a
9847 	 * duplicate -- even if the "new" address is the same as the old one.
9848 	 * Note that if all ipifs are down, we may have a pending ARP down
9849 	 * event to handle.  This is because we want to recover from duplicates
9850 	 * and thus delay tearing down ARP until the duplicates have been
9851 	 * removed or disabled.
9852 	 */
9853 	need_dl_down = need_arp_down = B_FALSE;
9854 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9855 		need_arp_down = !need_up;
9856 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9857 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9858 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9859 			need_dl_down = B_TRUE;
9860 		}
9861 	}
9862 
9863 	ipif_set_default(ipif);
9864 
9865 	/*
9866 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9867 	 * tag the ill so that future updates to the interface ID don't result
9868 	 * in this address getting automatically reconfigured from under the
9869 	 * administrator.
9870 	 */
9871 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9872 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9873 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9874 			ill->ill_manual_linklocal = 1;
9875 	}
9876 
9877 	/*
9878 	 * When publishing an interface address change event, we only notify
9879 	 * the event listeners of the new address.  It is assumed that if they
9880 	 * actively care about the addresses assigned that they will have
9881 	 * already discovered the previous address assigned (if there was one.)
9882 	 *
9883 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9884 	 */
9885 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9886 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9887 		    NE_ADDRESS_CHANGE, sin, sinlen);
9888 	}
9889 
9890 	mutex_exit(&ill->ill_lock);
9891 
9892 	if (need_up) {
9893 		/*
9894 		 * Now bring the interface back up.  If this
9895 		 * is the only IPIF for the ILL, ipif_up
9896 		 * will have to re-bind to the device, so
9897 		 * we may get back EINPROGRESS, in which
9898 		 * case, this IOCTL will get completed in
9899 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9900 		 */
9901 		err = ipif_up(ipif, q, mp);
9902 	} else {
9903 		/* Perhaps ilgs should use this ill */
9904 		update_conn_ill(NULL, ill->ill_ipst);
9905 	}
9906 
9907 	if (need_dl_down)
9908 		ill_dl_down(ill);
9909 
9910 	if (need_arp_down && !ill->ill_isv6)
9911 		(void) ipif_arp_down(ipif);
9912 
9913 	/*
9914 	 * The default multicast interface might have changed (for
9915 	 * instance if the IPv6 scope of the address changed)
9916 	 */
9917 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9918 
9919 	return (err);
9920 }
9921 
9922 /*
9923  * Restart entry point to restart the address set operation after the
9924  * refcounts have dropped to zero.
9925  */
9926 /* ARGSUSED */
9927 int
9928 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9929     ip_ioctl_cmd_t *ipip, void *ifreq)
9930 {
9931 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9932 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9933 	ASSERT(IAM_WRITER_IPIF(ipif));
9934 	(void) ipif_down_tail(ipif);
9935 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9936 }
9937 
9938 /* ARGSUSED */
9939 int
9940 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9941     ip_ioctl_cmd_t *ipip, void *if_req)
9942 {
9943 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9944 	struct lifreq *lifr = (struct lifreq *)if_req;
9945 
9946 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9947 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9948 	/*
9949 	 * The net mask and address can't change since we have a
9950 	 * reference to the ipif. So no lock is necessary.
9951 	 */
9952 	if (ipif->ipif_isv6) {
9953 		*sin6 = sin6_null;
9954 		sin6->sin6_family = AF_INET6;
9955 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9956 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9957 		lifr->lifr_addrlen =
9958 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9959 	} else {
9960 		*sin = sin_null;
9961 		sin->sin_family = AF_INET;
9962 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9963 		if (ipip->ipi_cmd_type == LIF_CMD) {
9964 			lifr->lifr_addrlen =
9965 			    ip_mask_to_plen(ipif->ipif_net_mask);
9966 		}
9967 	}
9968 	return (0);
9969 }
9970 
9971 /*
9972  * Set the destination address for a pt-pt interface.
9973  */
9974 /* ARGSUSED */
9975 int
9976 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9977     ip_ioctl_cmd_t *ipip, void *if_req)
9978 {
9979 	int err = 0;
9980 	in6_addr_t v6addr;
9981 	boolean_t need_up = B_FALSE;
9982 
9983 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9984 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9985 	ASSERT(IAM_WRITER_IPIF(ipif));
9986 
9987 	if (ipif->ipif_isv6) {
9988 		sin6_t *sin6;
9989 
9990 		if (sin->sin_family != AF_INET6)
9991 			return (EAFNOSUPPORT);
9992 
9993 		sin6 = (sin6_t *)sin;
9994 		v6addr = sin6->sin6_addr;
9995 
9996 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9997 			return (EADDRNOTAVAIL);
9998 	} else {
9999 		ipaddr_t addr;
10000 
10001 		if (sin->sin_family != AF_INET)
10002 			return (EAFNOSUPPORT);
10003 
10004 		addr = sin->sin_addr.s_addr;
10005 		if (addr != INADDR_ANY &&
10006 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
10007 			return (EADDRNOTAVAIL);
10008 		}
10009 
10010 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10011 	}
10012 
10013 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10014 		return (0);	/* No change */
10015 
10016 	if (ipif->ipif_flags & IPIF_UP) {
10017 		/*
10018 		 * If the interface is already marked up,
10019 		 * we call ipif_down which will take care
10020 		 * of ditching any IREs that have been set
10021 		 * up based on the old pp dst address.
10022 		 */
10023 		err = ipif_logical_down(ipif, q, mp);
10024 		if (err == EINPROGRESS)
10025 			return (err);
10026 		(void) ipif_down_tail(ipif);
10027 		need_up = B_TRUE;
10028 	}
10029 	/*
10030 	 * could return EINPROGRESS. If so ioctl will complete in
10031 	 * ip_rput_dlpi_writer
10032 	 */
10033 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10034 	return (err);
10035 }
10036 
10037 static int
10038 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10039     boolean_t need_up)
10040 {
10041 	in6_addr_t v6addr;
10042 	ill_t	*ill = ipif->ipif_ill;
10043 	int	err = 0;
10044 	boolean_t need_dl_down;
10045 	boolean_t need_arp_down;
10046 
10047 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10048 	    ipif->ipif_id, (void *)ipif));
10049 
10050 	/* Must cancel any pending timer before taking the ill_lock */
10051 	if (ipif->ipif_recovery_id != 0)
10052 		(void) untimeout(ipif->ipif_recovery_id);
10053 	ipif->ipif_recovery_id = 0;
10054 
10055 	if (ipif->ipif_isv6) {
10056 		sin6_t *sin6;
10057 
10058 		sin6 = (sin6_t *)sin;
10059 		v6addr = sin6->sin6_addr;
10060 	} else {
10061 		ipaddr_t addr;
10062 
10063 		addr = sin->sin_addr.s_addr;
10064 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10065 	}
10066 	mutex_enter(&ill->ill_lock);
10067 	/* Set point to point destination address. */
10068 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10069 		/*
10070 		 * Allow this as a means of creating logical
10071 		 * pt-pt interfaces on top of e.g. an Ethernet.
10072 		 * XXX Undocumented HACK for testing.
10073 		 * pt-pt interfaces are created with NUD disabled.
10074 		 */
10075 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10076 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10077 		if (ipif->ipif_isv6)
10078 			ill->ill_flags |= ILLF_NONUD;
10079 	}
10080 
10081 	/*
10082 	 * If the interface was previously marked as a duplicate, then since
10083 	 * we've now got a "new" address, it should no longer be considered a
10084 	 * duplicate -- even if the "new" address is the same as the old one.
10085 	 * Note that if all ipifs are down, we may have a pending ARP down
10086 	 * event to handle.
10087 	 */
10088 	need_dl_down = need_arp_down = B_FALSE;
10089 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10090 		need_arp_down = !need_up;
10091 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10092 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10093 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10094 			need_dl_down = B_TRUE;
10095 		}
10096 	}
10097 
10098 	/*
10099 	 * If we've just manually set the IPv6 destination link-local address
10100 	 * (0th ipif), tag the ill so that future updates to the destination
10101 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10102 	 * result in this address getting automatically reconfigured from
10103 	 * under the administrator.
10104 	 */
10105 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10106 		ill->ill_manual_dst_linklocal = 1;
10107 
10108 	/* Set the new address. */
10109 	ipif->ipif_v6pp_dst_addr = v6addr;
10110 	/* Make sure subnet tracks pp_dst */
10111 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10112 	mutex_exit(&ill->ill_lock);
10113 
10114 	if (need_up) {
10115 		/*
10116 		 * Now bring the interface back up.  If this
10117 		 * is the only IPIF for the ILL, ipif_up
10118 		 * will have to re-bind to the device, so
10119 		 * we may get back EINPROGRESS, in which
10120 		 * case, this IOCTL will get completed in
10121 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10122 		 */
10123 		err = ipif_up(ipif, q, mp);
10124 	}
10125 
10126 	if (need_dl_down)
10127 		ill_dl_down(ill);
10128 	if (need_arp_down && !ipif->ipif_isv6)
10129 		(void) ipif_arp_down(ipif);
10130 
10131 	return (err);
10132 }
10133 
10134 /*
10135  * Restart entry point to restart the dstaddress set operation after the
10136  * refcounts have dropped to zero.
10137  */
10138 /* ARGSUSED */
10139 int
10140 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10141     ip_ioctl_cmd_t *ipip, void *ifreq)
10142 {
10143 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10144 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10145 	(void) ipif_down_tail(ipif);
10146 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10147 }
10148 
10149 /* ARGSUSED */
10150 int
10151 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10152     ip_ioctl_cmd_t *ipip, void *if_req)
10153 {
10154 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10155 
10156 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10157 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10158 	/*
10159 	 * Get point to point destination address. The addresses can't
10160 	 * change since we hold a reference to the ipif.
10161 	 */
10162 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10163 		return (EADDRNOTAVAIL);
10164 
10165 	if (ipif->ipif_isv6) {
10166 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10167 		*sin6 = sin6_null;
10168 		sin6->sin6_family = AF_INET6;
10169 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10170 	} else {
10171 		*sin = sin_null;
10172 		sin->sin_family = AF_INET;
10173 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10174 	}
10175 	return (0);
10176 }
10177 
10178 /*
10179  * Check which flags will change by the given flags being set
10180  * silently ignore flags which userland is not allowed to control.
10181  * (Because these flags may change between SIOCGLIFFLAGS and
10182  * SIOCSLIFFLAGS, and that's outside of userland's control,
10183  * we need to silently ignore them rather than fail.)
10184  */
10185 static void
10186 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10187     uint64_t *offp)
10188 {
10189 	ill_t		*ill = ipif->ipif_ill;
10190 	phyint_t 	*phyi = ill->ill_phyint;
10191 	uint64_t	cantchange_flags, intf_flags;
10192 	uint64_t	turn_on, turn_off;
10193 
10194 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10195 	cantchange_flags = IFF_CANTCHANGE;
10196 	if (IS_IPMP(ill))
10197 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10198 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10199 	turn_off = intf_flags & turn_on;
10200 	turn_on ^= turn_off;
10201 	*onp = turn_on;
10202 	*offp = turn_off;
10203 }
10204 
10205 /*
10206  * Set interface flags.  Many flags require special handling (e.g.,
10207  * bringing the interface down); see below for details.
10208  *
10209  * NOTE : We really don't enforce that ipif_id zero should be used
10210  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10211  *	  is because applications generally does SICGLIFFLAGS and
10212  *	  ORs in the new flags (that affects the logical) and does a
10213  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10214  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10215  *	  flags that will be turned on is correct with respect to
10216  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10217  */
10218 /* ARGSUSED */
10219 int
10220 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10221     ip_ioctl_cmd_t *ipip, void *if_req)
10222 {
10223 	uint64_t turn_on;
10224 	uint64_t turn_off;
10225 	int	err = 0;
10226 	phyint_t *phyi;
10227 	ill_t *ill;
10228 	conn_t *connp;
10229 	uint64_t intf_flags;
10230 	boolean_t phyint_flags_modified = B_FALSE;
10231 	uint64_t flags;
10232 	struct ifreq *ifr;
10233 	struct lifreq *lifr;
10234 	boolean_t set_linklocal = B_FALSE;
10235 
10236 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10237 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10238 
10239 	ASSERT(IAM_WRITER_IPIF(ipif));
10240 
10241 	ill = ipif->ipif_ill;
10242 	phyi = ill->ill_phyint;
10243 
10244 	if (ipip->ipi_cmd_type == IF_CMD) {
10245 		ifr = (struct ifreq *)if_req;
10246 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10247 	} else {
10248 		lifr = (struct lifreq *)if_req;
10249 		flags = lifr->lifr_flags;
10250 	}
10251 
10252 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10253 
10254 	/*
10255 	 * Have the flags been set correctly until now?
10256 	 */
10257 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10258 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10259 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10260 	/*
10261 	 * Compare the new flags to the old, and partition
10262 	 * into those coming on and those going off.
10263 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10264 	 */
10265 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10266 		flags |= intf_flags & ~0xFFFF;
10267 
10268 	/*
10269 	 * Explicitly fail attempts to change flags that are always invalid on
10270 	 * an IPMP meta-interface.
10271 	 */
10272 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10273 		return (EINVAL);
10274 
10275 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10276 	if ((turn_on|turn_off) == 0)
10277 		return (0);	/* No change */
10278 
10279 	/*
10280 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10281 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10282 	 * allow it to be turned off.
10283 	 */
10284 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10285 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10286 		return (EINVAL);
10287 
10288 	if ((connp = Q_TO_CONN(q)) == NULL)
10289 		return (EINVAL);
10290 
10291 	/*
10292 	 * Only vrrp control socket is allowed to change IFF_UP and
10293 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10294 	 */
10295 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10296 		if (!connp->conn_isvrrp)
10297 			return (EINVAL);
10298 	}
10299 
10300 	/*
10301 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10302 	 * VRRP control socket.
10303 	 */
10304 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10305 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10306 			return (EINVAL);
10307 	}
10308 
10309 	if (turn_on & IFF_NOFAILOVER) {
10310 		turn_on |= IFF_DEPRECATED;
10311 		flags |= IFF_DEPRECATED;
10312 	}
10313 
10314 	/*
10315 	 * On underlying interfaces, only allow applications to manage test
10316 	 * addresses -- otherwise, they may get confused when the address
10317 	 * moves as part of being brought up.  Likewise, prevent an
10318 	 * application-managed test address from being converted to a data
10319 	 * address.  To prevent migration of administratively up addresses in
10320 	 * the kernel, we don't allow them to be converted either.
10321 	 */
10322 	if (IS_UNDER_IPMP(ill)) {
10323 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10324 
10325 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10326 			return (EINVAL);
10327 
10328 		if ((turn_off & IFF_NOFAILOVER) &&
10329 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10330 			return (EINVAL);
10331 	}
10332 
10333 	/*
10334 	 * Only allow IFF_TEMPORARY flag to be set on
10335 	 * IPv6 interfaces.
10336 	 */
10337 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10338 		return (EINVAL);
10339 
10340 	/*
10341 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10342 	 */
10343 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10344 		return (EINVAL);
10345 
10346 	/*
10347 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10348 	 * interfaces.  It makes no sense in that context.
10349 	 */
10350 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10351 		return (EINVAL);
10352 
10353 	/*
10354 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10355 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10356 	 * If the link local address isn't set, and can be set, it will get
10357 	 * set later on in this function.
10358 	 */
10359 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10360 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10361 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10362 		if (ipif_cant_setlinklocal(ipif))
10363 			return (EINVAL);
10364 		set_linklocal = B_TRUE;
10365 	}
10366 
10367 	/*
10368 	 * If we modify physical interface flags, we'll potentially need to
10369 	 * send up two routing socket messages for the changes (one for the
10370 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10371 	 */
10372 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10373 		phyint_flags_modified = B_TRUE;
10374 
10375 	/*
10376 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10377 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10378 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10379 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10380 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10381 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10382 	 * will not be honored.
10383 	 */
10384 	if (turn_on & PHYI_STANDBY) {
10385 		/*
10386 		 * No need to grab ill_g_usesrc_lock here; see the
10387 		 * synchronization notes in ip.c.
10388 		 */
10389 		if (ill->ill_usesrc_grp_next != NULL ||
10390 		    intf_flags & PHYI_INACTIVE)
10391 			return (EINVAL);
10392 		if (!(flags & PHYI_FAILED)) {
10393 			flags |= PHYI_INACTIVE;
10394 			turn_on |= PHYI_INACTIVE;
10395 		}
10396 	}
10397 
10398 	if (turn_off & PHYI_STANDBY) {
10399 		flags &= ~PHYI_INACTIVE;
10400 		turn_off |= PHYI_INACTIVE;
10401 	}
10402 
10403 	/*
10404 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10405 	 * would end up on.
10406 	 */
10407 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10408 	    (PHYI_FAILED | PHYI_INACTIVE))
10409 		return (EINVAL);
10410 
10411 	/*
10412 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10413 	 * status of the interface.
10414 	 */
10415 	if ((turn_on | turn_off) & ILLF_ROUTER) {
10416 		err = ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10417 		if (err != 0)
10418 			return (err);
10419 	}
10420 
10421 	/*
10422 	 * If the interface is not UP and we are not going to
10423 	 * bring it UP, record the flags and return. When the
10424 	 * interface comes UP later, the right actions will be
10425 	 * taken.
10426 	 */
10427 	if (!(ipif->ipif_flags & IPIF_UP) &&
10428 	    !(turn_on & IPIF_UP)) {
10429 		/* Record new flags in their respective places. */
10430 		mutex_enter(&ill->ill_lock);
10431 		mutex_enter(&ill->ill_phyint->phyint_lock);
10432 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10433 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10434 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10435 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10436 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10437 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10438 		mutex_exit(&ill->ill_lock);
10439 		mutex_exit(&ill->ill_phyint->phyint_lock);
10440 
10441 		/*
10442 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10443 		 * same to the kernel: if any of them has been set by
10444 		 * userland, the interface cannot be used for data traffic.
10445 		 */
10446 		if ((turn_on|turn_off) &
10447 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10448 			ASSERT(!IS_IPMP(ill));
10449 			/*
10450 			 * It's possible the ill is part of an "anonymous"
10451 			 * IPMP group rather than a real group.  In that case,
10452 			 * there are no other interfaces in the group and thus
10453 			 * no need to call ipmp_phyint_refresh_active().
10454 			 */
10455 			if (IS_UNDER_IPMP(ill))
10456 				ipmp_phyint_refresh_active(phyi);
10457 		}
10458 
10459 		if (phyint_flags_modified) {
10460 			if (phyi->phyint_illv4 != NULL) {
10461 				ip_rts_ifmsg(phyi->phyint_illv4->
10462 				    ill_ipif, RTSQ_DEFAULT);
10463 			}
10464 			if (phyi->phyint_illv6 != NULL) {
10465 				ip_rts_ifmsg(phyi->phyint_illv6->
10466 				    ill_ipif, RTSQ_DEFAULT);
10467 			}
10468 		}
10469 		/* The default multicast interface might have changed */
10470 		ire_increment_multicast_generation(ill->ill_ipst,
10471 		    ill->ill_isv6);
10472 
10473 		return (0);
10474 	} else if (set_linklocal) {
10475 		mutex_enter(&ill->ill_lock);
10476 		if (set_linklocal)
10477 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10478 		mutex_exit(&ill->ill_lock);
10479 	}
10480 
10481 	/*
10482 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10483 	 * or point-to-point interfaces with an unspecified destination. We do
10484 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10485 	 * have a subnet assigned, which is how in.ndpd currently manages its
10486 	 * onlink prefix list when no addresses are configured with those
10487 	 * prefixes.
10488 	 */
10489 	if (ipif->ipif_isv6 &&
10490 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10491 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10492 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10493 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10494 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10495 		return (EINVAL);
10496 	}
10497 
10498 	/*
10499 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10500 	 * from being brought up.
10501 	 */
10502 	if (!ipif->ipif_isv6 &&
10503 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10504 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10505 		return (EINVAL);
10506 	}
10507 
10508 	/*
10509 	 * If we are going to change one or more of the flags that are
10510 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10511 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10512 	 * IPIF_NOFAILOVER, we will take special action.  This is
10513 	 * done by bring the ipif down, changing the flags and bringing
10514 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10515 	 * back up will trigger the address to be moved.
10516 	 *
10517 	 * If we are going to change IFF_NOACCEPT, we need to bring
10518 	 * all the ipifs down then bring them up again.	 The act of
10519 	 * bringing all the ipifs back up will trigger the local
10520 	 * ires being recreated with "no_accept" set/cleared.
10521 	 *
10522 	 * Note that ILLF_NOACCEPT is always set separately from the
10523 	 * other flags.
10524 	 */
10525 	if ((turn_on|turn_off) &
10526 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10527 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10528 	    IPIF_NOFAILOVER)) {
10529 		/*
10530 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10531 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10532 		 * entries shared between multiple ipifs on the same subnet.
10533 		 */
10534 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10535 		    !(turn_off & IPIF_UP)) {
10536 			if (ipif->ipif_flags & IPIF_UP)
10537 				ill->ill_logical_down = 1;
10538 			turn_on &= ~IPIF_UP;
10539 		}
10540 		err = ipif_down(ipif, q, mp);
10541 		ip1dbg(("ipif_down returns %d err ", err));
10542 		if (err == EINPROGRESS)
10543 			return (err);
10544 		(void) ipif_down_tail(ipif);
10545 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10546 		/*
10547 		 * If we can quiesce the ill, then continue.  If not, then
10548 		 * ip_sioctl_flags_tail() will be called from
10549 		 * ipif_ill_refrele_tail().
10550 		 */
10551 		ill_down_ipifs(ill, B_TRUE);
10552 
10553 		mutex_enter(&connp->conn_lock);
10554 		mutex_enter(&ill->ill_lock);
10555 		if (!ill_is_quiescent(ill)) {
10556 			boolean_t success;
10557 
10558 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10559 			    q, mp, ILL_DOWN);
10560 			mutex_exit(&ill->ill_lock);
10561 			mutex_exit(&connp->conn_lock);
10562 			return (success ? EINPROGRESS : EINTR);
10563 		}
10564 		mutex_exit(&ill->ill_lock);
10565 		mutex_exit(&connp->conn_lock);
10566 	}
10567 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10568 }
10569 
10570 static int
10571 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10572 {
10573 	ill_t	*ill;
10574 	phyint_t *phyi;
10575 	uint64_t turn_on, turn_off;
10576 	boolean_t phyint_flags_modified = B_FALSE;
10577 	int	err = 0;
10578 	boolean_t set_linklocal = B_FALSE;
10579 
10580 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10581 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10582 
10583 	ASSERT(IAM_WRITER_IPIF(ipif));
10584 
10585 	ill = ipif->ipif_ill;
10586 	phyi = ill->ill_phyint;
10587 
10588 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10589 
10590 	/*
10591 	 * IFF_UP is handled separately.
10592 	 */
10593 	turn_on &= ~IFF_UP;
10594 	turn_off &= ~IFF_UP;
10595 
10596 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10597 		phyint_flags_modified = B_TRUE;
10598 
10599 	/*
10600 	 * Now we change the flags. Track current value of
10601 	 * other flags in their respective places.
10602 	 */
10603 	mutex_enter(&ill->ill_lock);
10604 	mutex_enter(&phyi->phyint_lock);
10605 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10606 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10607 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10608 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10609 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10610 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10611 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10612 		set_linklocal = B_TRUE;
10613 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10614 	}
10615 
10616 	mutex_exit(&ill->ill_lock);
10617 	mutex_exit(&phyi->phyint_lock);
10618 
10619 	if (set_linklocal)
10620 		(void) ipif_setlinklocal(ipif);
10621 
10622 	/*
10623 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10624 	 * the kernel: if any of them has been set by userland, the interface
10625 	 * cannot be used for data traffic.
10626 	 */
10627 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10628 		ASSERT(!IS_IPMP(ill));
10629 		/*
10630 		 * It's possible the ill is part of an "anonymous" IPMP group
10631 		 * rather than a real group.  In that case, there are no other
10632 		 * interfaces in the group and thus no need for us to call
10633 		 * ipmp_phyint_refresh_active().
10634 		 */
10635 		if (IS_UNDER_IPMP(ill))
10636 			ipmp_phyint_refresh_active(phyi);
10637 	}
10638 
10639 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10640 		/*
10641 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10642 		 * ipifs that were brought down.
10643 		 *
10644 		 * The routing sockets messages are sent as the result
10645 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10646 		 * as well.
10647 		 */
10648 		err = ill_up_ipifs(ill, q, mp);
10649 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10650 		/*
10651 		 * XXX ipif_up really does not know whether a phyint flags
10652 		 * was modified or not. So, it sends up information on
10653 		 * only one routing sockets message. As we don't bring up
10654 		 * the interface and also set PHYI_ flags simultaneously
10655 		 * it should be okay.
10656 		 */
10657 		err = ipif_up(ipif, q, mp);
10658 	} else {
10659 		/*
10660 		 * Make sure routing socket sees all changes to the flags.
10661 		 * ipif_up_done* handles this when we use ipif_up.
10662 		 */
10663 		if (phyint_flags_modified) {
10664 			if (phyi->phyint_illv4 != NULL) {
10665 				ip_rts_ifmsg(phyi->phyint_illv4->
10666 				    ill_ipif, RTSQ_DEFAULT);
10667 			}
10668 			if (phyi->phyint_illv6 != NULL) {
10669 				ip_rts_ifmsg(phyi->phyint_illv6->
10670 				    ill_ipif, RTSQ_DEFAULT);
10671 			}
10672 		} else {
10673 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10674 		}
10675 		/*
10676 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10677 		 * this in need_up case.
10678 		 */
10679 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10680 	}
10681 
10682 	/* The default multicast interface might have changed */
10683 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10684 	return (err);
10685 }
10686 
10687 /*
10688  * Restart the flags operation now that the refcounts have dropped to zero.
10689  */
10690 /* ARGSUSED */
10691 int
10692 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10693     ip_ioctl_cmd_t *ipip, void *if_req)
10694 {
10695 	uint64_t flags;
10696 	struct ifreq *ifr = if_req;
10697 	struct lifreq *lifr = if_req;
10698 	uint64_t turn_on, turn_off;
10699 
10700 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10701 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10702 
10703 	if (ipip->ipi_cmd_type == IF_CMD) {
10704 		/* cast to uint16_t prevents unwanted sign extension */
10705 		flags = (uint16_t)ifr->ifr_flags;
10706 	} else {
10707 		flags = lifr->lifr_flags;
10708 	}
10709 
10710 	/*
10711 	 * If this function call is a result of the ILLF_NOACCEPT flag
10712 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10713 	 */
10714 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10715 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10716 		(void) ipif_down_tail(ipif);
10717 
10718 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10719 }
10720 
10721 /*
10722  * Can operate on either a module or a driver queue.
10723  */
10724 /* ARGSUSED */
10725 int
10726 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10727     ip_ioctl_cmd_t *ipip, void *if_req)
10728 {
10729 	/*
10730 	 * Has the flags been set correctly till now ?
10731 	 */
10732 	ill_t *ill = ipif->ipif_ill;
10733 	phyint_t *phyi = ill->ill_phyint;
10734 
10735 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10736 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10737 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10738 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10739 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10740 
10741 	/*
10742 	 * Need a lock since some flags can be set even when there are
10743 	 * references to the ipif.
10744 	 */
10745 	mutex_enter(&ill->ill_lock);
10746 	if (ipip->ipi_cmd_type == IF_CMD) {
10747 		struct ifreq *ifr = (struct ifreq *)if_req;
10748 
10749 		/* Get interface flags (low 16 only). */
10750 		ifr->ifr_flags = ((ipif->ipif_flags |
10751 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10752 	} else {
10753 		struct lifreq *lifr = (struct lifreq *)if_req;
10754 
10755 		/* Get interface flags. */
10756 		lifr->lifr_flags = ipif->ipif_flags |
10757 		    ill->ill_flags | phyi->phyint_flags;
10758 	}
10759 	mutex_exit(&ill->ill_lock);
10760 	return (0);
10761 }
10762 
10763 /*
10764  * We allow the MTU to be set on an ILL, but not have it be different
10765  * for different IPIFs since we don't actually send packets on IPIFs.
10766  */
10767 /* ARGSUSED */
10768 int
10769 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10770     ip_ioctl_cmd_t *ipip, void *if_req)
10771 {
10772 	int mtu;
10773 	int ip_min_mtu;
10774 	struct ifreq	*ifr;
10775 	struct lifreq *lifr;
10776 	ill_t	*ill;
10777 
10778 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10779 	    ipif->ipif_id, (void *)ipif));
10780 	if (ipip->ipi_cmd_type == IF_CMD) {
10781 		ifr = (struct ifreq *)if_req;
10782 		mtu = ifr->ifr_metric;
10783 	} else {
10784 		lifr = (struct lifreq *)if_req;
10785 		mtu = lifr->lifr_mtu;
10786 	}
10787 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10788 	if (ipif->ipif_id != 0)
10789 		return (EINVAL);
10790 
10791 	ill = ipif->ipif_ill;
10792 	if (ipif->ipif_isv6)
10793 		ip_min_mtu = IPV6_MIN_MTU;
10794 	else
10795 		ip_min_mtu = IP_MIN_MTU;
10796 
10797 	mutex_enter(&ill->ill_lock);
10798 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10799 		mutex_exit(&ill->ill_lock);
10800 		return (EINVAL);
10801 	}
10802 	/*
10803 	 * The dce and fragmentation code can handle changes to ill_mtu
10804 	 * concurrent with sending/fragmenting packets.
10805 	 */
10806 	ill->ill_mtu = mtu;
10807 	ill->ill_flags |= ILLF_FIXEDMTU;
10808 	mutex_exit(&ill->ill_lock);
10809 
10810 	/*
10811 	 * Make sure all dce_generation checks find out
10812 	 * that ill_mtu has changed.
10813 	 */
10814 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10815 
10816 	/*
10817 	 * Refresh IPMP meta-interface MTU if necessary.
10818 	 */
10819 	if (IS_UNDER_IPMP(ill))
10820 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10821 
10822 	/* Update the MTU in SCTP's list */
10823 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10824 	return (0);
10825 }
10826 
10827 /* Get interface MTU. */
10828 /* ARGSUSED */
10829 int
10830 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10831 	ip_ioctl_cmd_t *ipip, void *if_req)
10832 {
10833 	struct ifreq	*ifr;
10834 	struct lifreq	*lifr;
10835 
10836 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10837 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10838 
10839 	/*
10840 	 * We allow a get on any logical interface even though the set
10841 	 * can only be done on logical unit 0.
10842 	 */
10843 	if (ipip->ipi_cmd_type == IF_CMD) {
10844 		ifr = (struct ifreq *)if_req;
10845 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10846 	} else {
10847 		lifr = (struct lifreq *)if_req;
10848 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10849 	}
10850 	return (0);
10851 }
10852 
10853 /* Set interface broadcast address. */
10854 /* ARGSUSED2 */
10855 int
10856 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10857 	ip_ioctl_cmd_t *ipip, void *if_req)
10858 {
10859 	ipaddr_t addr;
10860 	ire_t	*ire;
10861 	ill_t		*ill = ipif->ipif_ill;
10862 	ip_stack_t	*ipst = ill->ill_ipst;
10863 
10864 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10865 	    ipif->ipif_id));
10866 
10867 	ASSERT(IAM_WRITER_IPIF(ipif));
10868 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10869 		return (EADDRNOTAVAIL);
10870 
10871 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10872 
10873 	if (sin->sin_family != AF_INET)
10874 		return (EAFNOSUPPORT);
10875 
10876 	addr = sin->sin_addr.s_addr;
10877 
10878 	if (ipif->ipif_flags & IPIF_UP) {
10879 		/*
10880 		 * If we are already up, make sure the new
10881 		 * broadcast address makes sense.  If it does,
10882 		 * there should be an IRE for it already.
10883 		 */
10884 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10885 		    ill, ipif->ipif_zoneid, NULL,
10886 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10887 		if (ire == NULL) {
10888 			return (EINVAL);
10889 		} else {
10890 			ire_refrele(ire);
10891 		}
10892 	}
10893 	/*
10894 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10895 	 * needs to already exist we never need to change the set of
10896 	 * IRE_BROADCASTs when we are UP.
10897 	 */
10898 	if (addr != ipif->ipif_brd_addr)
10899 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10900 
10901 	return (0);
10902 }
10903 
10904 /* Get interface broadcast address. */
10905 /* ARGSUSED */
10906 int
10907 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10908     ip_ioctl_cmd_t *ipip, void *if_req)
10909 {
10910 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10911 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10912 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10913 		return (EADDRNOTAVAIL);
10914 
10915 	/* IPIF_BROADCAST not possible with IPv6 */
10916 	ASSERT(!ipif->ipif_isv6);
10917 	*sin = sin_null;
10918 	sin->sin_family = AF_INET;
10919 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10920 	return (0);
10921 }
10922 
10923 /*
10924  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10925  */
10926 /* ARGSUSED */
10927 int
10928 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10929     ip_ioctl_cmd_t *ipip, void *if_req)
10930 {
10931 	int err = 0;
10932 	in6_addr_t v6mask;
10933 
10934 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10935 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10936 
10937 	ASSERT(IAM_WRITER_IPIF(ipif));
10938 
10939 	if (ipif->ipif_isv6) {
10940 		sin6_t *sin6;
10941 
10942 		if (sin->sin_family != AF_INET6)
10943 			return (EAFNOSUPPORT);
10944 
10945 		sin6 = (sin6_t *)sin;
10946 		v6mask = sin6->sin6_addr;
10947 	} else {
10948 		ipaddr_t mask;
10949 
10950 		if (sin->sin_family != AF_INET)
10951 			return (EAFNOSUPPORT);
10952 
10953 		mask = sin->sin_addr.s_addr;
10954 		if (!ip_contiguous_mask(ntohl(mask)))
10955 			return (ENOTSUP);
10956 		V4MASK_TO_V6(mask, v6mask);
10957 	}
10958 
10959 	/*
10960 	 * No big deal if the interface isn't already up, or the mask
10961 	 * isn't really changing, or this is pt-pt.
10962 	 */
10963 	if (!(ipif->ipif_flags & IPIF_UP) ||
10964 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10965 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10966 		ipif->ipif_v6net_mask = v6mask;
10967 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10968 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10969 			    ipif->ipif_v6net_mask,
10970 			    ipif->ipif_v6subnet);
10971 		}
10972 		return (0);
10973 	}
10974 	/*
10975 	 * Make sure we have valid net and subnet broadcast ire's
10976 	 * for the old netmask, if needed by other logical interfaces.
10977 	 */
10978 	err = ipif_logical_down(ipif, q, mp);
10979 	if (err == EINPROGRESS)
10980 		return (err);
10981 	(void) ipif_down_tail(ipif);
10982 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10983 	return (err);
10984 }
10985 
10986 static int
10987 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10988 {
10989 	in6_addr_t v6mask;
10990 	int err = 0;
10991 
10992 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10993 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10994 
10995 	if (ipif->ipif_isv6) {
10996 		sin6_t *sin6;
10997 
10998 		sin6 = (sin6_t *)sin;
10999 		v6mask = sin6->sin6_addr;
11000 	} else {
11001 		ipaddr_t mask;
11002 
11003 		mask = sin->sin_addr.s_addr;
11004 		V4MASK_TO_V6(mask, v6mask);
11005 	}
11006 
11007 	ipif->ipif_v6net_mask = v6mask;
11008 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11009 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11010 		    ipif->ipif_v6subnet);
11011 	}
11012 	err = ipif_up(ipif, q, mp);
11013 
11014 	if (err == 0 || err == EINPROGRESS) {
11015 		/*
11016 		 * The interface must be DL_BOUND if this packet has to
11017 		 * go out on the wire. Since we only go through a logical
11018 		 * down and are bound with the driver during an internal
11019 		 * down/up that is satisfied.
11020 		 */
11021 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11022 			/* Potentially broadcast an address mask reply. */
11023 			ipif_mask_reply(ipif);
11024 		}
11025 	}
11026 	return (err);
11027 }
11028 
11029 /* ARGSUSED */
11030 int
11031 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11032     ip_ioctl_cmd_t *ipip, void *if_req)
11033 {
11034 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11035 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11036 	(void) ipif_down_tail(ipif);
11037 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11038 }
11039 
11040 /* Get interface net mask. */
11041 /* ARGSUSED */
11042 int
11043 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11044     ip_ioctl_cmd_t *ipip, void *if_req)
11045 {
11046 	struct lifreq *lifr = (struct lifreq *)if_req;
11047 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11048 
11049 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11050 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11051 
11052 	/*
11053 	 * net mask can't change since we have a reference to the ipif.
11054 	 */
11055 	if (ipif->ipif_isv6) {
11056 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11057 		*sin6 = sin6_null;
11058 		sin6->sin6_family = AF_INET6;
11059 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11060 		lifr->lifr_addrlen =
11061 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11062 	} else {
11063 		*sin = sin_null;
11064 		sin->sin_family = AF_INET;
11065 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11066 		if (ipip->ipi_cmd_type == LIF_CMD) {
11067 			lifr->lifr_addrlen =
11068 			    ip_mask_to_plen(ipif->ipif_net_mask);
11069 		}
11070 	}
11071 	return (0);
11072 }
11073 
11074 /* ARGSUSED */
11075 int
11076 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11077     ip_ioctl_cmd_t *ipip, void *if_req)
11078 {
11079 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11080 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11081 
11082 	/*
11083 	 * Since no applications should ever be setting metrics on underlying
11084 	 * interfaces, we explicitly fail to smoke 'em out.
11085 	 */
11086 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11087 		return (EINVAL);
11088 
11089 	/*
11090 	 * Set interface metric.  We don't use this for
11091 	 * anything but we keep track of it in case it is
11092 	 * important to routing applications or such.
11093 	 */
11094 	if (ipip->ipi_cmd_type == IF_CMD) {
11095 		struct ifreq    *ifr;
11096 
11097 		ifr = (struct ifreq *)if_req;
11098 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11099 	} else {
11100 		struct lifreq   *lifr;
11101 
11102 		lifr = (struct lifreq *)if_req;
11103 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11104 	}
11105 	return (0);
11106 }
11107 
11108 /* ARGSUSED */
11109 int
11110 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11111     ip_ioctl_cmd_t *ipip, void *if_req)
11112 {
11113 	/* Get interface metric. */
11114 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11115 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11116 
11117 	if (ipip->ipi_cmd_type == IF_CMD) {
11118 		struct ifreq    *ifr;
11119 
11120 		ifr = (struct ifreq *)if_req;
11121 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11122 	} else {
11123 		struct lifreq   *lifr;
11124 
11125 		lifr = (struct lifreq *)if_req;
11126 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11127 	}
11128 
11129 	return (0);
11130 }
11131 
11132 /* ARGSUSED */
11133 int
11134 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11135     ip_ioctl_cmd_t *ipip, void *if_req)
11136 {
11137 	int	arp_muxid;
11138 
11139 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11140 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11141 	/*
11142 	 * Set the muxid returned from I_PLINK.
11143 	 */
11144 	if (ipip->ipi_cmd_type == IF_CMD) {
11145 		struct ifreq *ifr = (struct ifreq *)if_req;
11146 
11147 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11148 		arp_muxid = ifr->ifr_arp_muxid;
11149 	} else {
11150 		struct lifreq *lifr = (struct lifreq *)if_req;
11151 
11152 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11153 		arp_muxid = lifr->lifr_arp_muxid;
11154 	}
11155 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11156 	return (0);
11157 }
11158 
11159 /* ARGSUSED */
11160 int
11161 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11162     ip_ioctl_cmd_t *ipip, void *if_req)
11163 {
11164 	int	arp_muxid = 0;
11165 
11166 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11167 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11168 	/*
11169 	 * Get the muxid saved in ill for I_PUNLINK.
11170 	 */
11171 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11172 	if (ipip->ipi_cmd_type == IF_CMD) {
11173 		struct ifreq *ifr = (struct ifreq *)if_req;
11174 
11175 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11176 		ifr->ifr_arp_muxid = arp_muxid;
11177 	} else {
11178 		struct lifreq *lifr = (struct lifreq *)if_req;
11179 
11180 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11181 		lifr->lifr_arp_muxid = arp_muxid;
11182 	}
11183 	return (0);
11184 }
11185 
11186 /*
11187  * Set the subnet prefix. Does not modify the broadcast address.
11188  */
11189 /* ARGSUSED */
11190 int
11191 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11192     ip_ioctl_cmd_t *ipip, void *if_req)
11193 {
11194 	int err = 0;
11195 	in6_addr_t v6addr;
11196 	in6_addr_t v6mask;
11197 	boolean_t need_up = B_FALSE;
11198 	int addrlen;
11199 
11200 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11201 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11202 
11203 	ASSERT(IAM_WRITER_IPIF(ipif));
11204 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11205 
11206 	if (ipif->ipif_isv6) {
11207 		sin6_t *sin6;
11208 
11209 		if (sin->sin_family != AF_INET6)
11210 			return (EAFNOSUPPORT);
11211 
11212 		sin6 = (sin6_t *)sin;
11213 		v6addr = sin6->sin6_addr;
11214 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11215 			return (EADDRNOTAVAIL);
11216 	} else {
11217 		ipaddr_t addr;
11218 
11219 		if (sin->sin_family != AF_INET)
11220 			return (EAFNOSUPPORT);
11221 
11222 		addr = sin->sin_addr.s_addr;
11223 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11224 			return (EADDRNOTAVAIL);
11225 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11226 		/* Add 96 bits */
11227 		addrlen += IPV6_ABITS - IP_ABITS;
11228 	}
11229 
11230 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11231 		return (EINVAL);
11232 
11233 	/* Check if bits in the address is set past the mask */
11234 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11235 		return (EINVAL);
11236 
11237 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11238 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11239 		return (0);	/* No change */
11240 
11241 	if (ipif->ipif_flags & IPIF_UP) {
11242 		/*
11243 		 * If the interface is already marked up,
11244 		 * we call ipif_down which will take care
11245 		 * of ditching any IREs that have been set
11246 		 * up based on the old interface address.
11247 		 */
11248 		err = ipif_logical_down(ipif, q, mp);
11249 		if (err == EINPROGRESS)
11250 			return (err);
11251 		(void) ipif_down_tail(ipif);
11252 		need_up = B_TRUE;
11253 	}
11254 
11255 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11256 	return (err);
11257 }
11258 
11259 static int
11260 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11261     queue_t *q, mblk_t *mp, boolean_t need_up)
11262 {
11263 	ill_t	*ill = ipif->ipif_ill;
11264 	int	err = 0;
11265 
11266 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11267 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11268 
11269 	/* Set the new address. */
11270 	mutex_enter(&ill->ill_lock);
11271 	ipif->ipif_v6net_mask = v6mask;
11272 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11273 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11274 		    ipif->ipif_v6subnet);
11275 	}
11276 	mutex_exit(&ill->ill_lock);
11277 
11278 	if (need_up) {
11279 		/*
11280 		 * Now bring the interface back up.  If this
11281 		 * is the only IPIF for the ILL, ipif_up
11282 		 * will have to re-bind to the device, so
11283 		 * we may get back EINPROGRESS, in which
11284 		 * case, this IOCTL will get completed in
11285 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11286 		 */
11287 		err = ipif_up(ipif, q, mp);
11288 		if (err == EINPROGRESS)
11289 			return (err);
11290 	}
11291 	return (err);
11292 }
11293 
11294 /* ARGSUSED */
11295 int
11296 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11297     ip_ioctl_cmd_t *ipip, void *if_req)
11298 {
11299 	int	addrlen;
11300 	in6_addr_t v6addr;
11301 	in6_addr_t v6mask;
11302 	struct lifreq *lifr = (struct lifreq *)if_req;
11303 
11304 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11305 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11306 	(void) ipif_down_tail(ipif);
11307 
11308 	addrlen = lifr->lifr_addrlen;
11309 	if (ipif->ipif_isv6) {
11310 		sin6_t *sin6;
11311 
11312 		sin6 = (sin6_t *)sin;
11313 		v6addr = sin6->sin6_addr;
11314 	} else {
11315 		ipaddr_t addr;
11316 
11317 		addr = sin->sin_addr.s_addr;
11318 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11319 		addrlen += IPV6_ABITS - IP_ABITS;
11320 	}
11321 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11322 
11323 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11324 }
11325 
11326 /* ARGSUSED */
11327 int
11328 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11329     ip_ioctl_cmd_t *ipip, void *if_req)
11330 {
11331 	struct lifreq *lifr = (struct lifreq *)if_req;
11332 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11333 
11334 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11335 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11336 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11337 
11338 	if (ipif->ipif_isv6) {
11339 		*sin6 = sin6_null;
11340 		sin6->sin6_family = AF_INET6;
11341 		sin6->sin6_addr = ipif->ipif_v6subnet;
11342 		lifr->lifr_addrlen =
11343 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11344 	} else {
11345 		*sin = sin_null;
11346 		sin->sin_family = AF_INET;
11347 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11348 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11349 	}
11350 	return (0);
11351 }
11352 
11353 /*
11354  * Set the IPv6 address token.
11355  */
11356 /* ARGSUSED */
11357 int
11358 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11359     ip_ioctl_cmd_t *ipi, void *if_req)
11360 {
11361 	ill_t *ill = ipif->ipif_ill;
11362 	int err;
11363 	in6_addr_t v6addr;
11364 	in6_addr_t v6mask;
11365 	boolean_t need_up = B_FALSE;
11366 	int i;
11367 	sin6_t *sin6 = (sin6_t *)sin;
11368 	struct lifreq *lifr = (struct lifreq *)if_req;
11369 	int addrlen;
11370 
11371 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11372 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11373 	ASSERT(IAM_WRITER_IPIF(ipif));
11374 
11375 	addrlen = lifr->lifr_addrlen;
11376 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11377 	if (ipif->ipif_id != 0)
11378 		return (EINVAL);
11379 
11380 	if (!ipif->ipif_isv6)
11381 		return (EINVAL);
11382 
11383 	if (addrlen > IPV6_ABITS)
11384 		return (EINVAL);
11385 
11386 	v6addr = sin6->sin6_addr;
11387 
11388 	/*
11389 	 * The length of the token is the length from the end.  To get
11390 	 * the proper mask for this, compute the mask of the bits not
11391 	 * in the token; ie. the prefix, and then xor to get the mask.
11392 	 */
11393 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11394 		return (EINVAL);
11395 	for (i = 0; i < 4; i++) {
11396 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11397 	}
11398 
11399 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11400 	    ill->ill_token_length == addrlen)
11401 		return (0);	/* No change */
11402 
11403 	if (ipif->ipif_flags & IPIF_UP) {
11404 		err = ipif_logical_down(ipif, q, mp);
11405 		if (err == EINPROGRESS)
11406 			return (err);
11407 		(void) ipif_down_tail(ipif);
11408 		need_up = B_TRUE;
11409 	}
11410 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11411 	return (err);
11412 }
11413 
11414 static int
11415 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11416     mblk_t *mp, boolean_t need_up)
11417 {
11418 	in6_addr_t v6addr;
11419 	in6_addr_t v6mask;
11420 	ill_t	*ill = ipif->ipif_ill;
11421 	int	i;
11422 	int	err = 0;
11423 
11424 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11425 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11426 	v6addr = sin6->sin6_addr;
11427 	/*
11428 	 * The length of the token is the length from the end.  To get
11429 	 * the proper mask for this, compute the mask of the bits not
11430 	 * in the token; ie. the prefix, and then xor to get the mask.
11431 	 */
11432 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11433 	for (i = 0; i < 4; i++)
11434 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11435 
11436 	mutex_enter(&ill->ill_lock);
11437 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11438 	ill->ill_token_length = addrlen;
11439 	ill->ill_manual_token = 1;
11440 
11441 	/* Reconfigure the link-local address based on this new token */
11442 	ipif_setlinklocal(ill->ill_ipif);
11443 
11444 	mutex_exit(&ill->ill_lock);
11445 
11446 	if (need_up) {
11447 		/*
11448 		 * Now bring the interface back up.  If this
11449 		 * is the only IPIF for the ILL, ipif_up
11450 		 * will have to re-bind to the device, so
11451 		 * we may get back EINPROGRESS, in which
11452 		 * case, this IOCTL will get completed in
11453 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11454 		 */
11455 		err = ipif_up(ipif, q, mp);
11456 		if (err == EINPROGRESS)
11457 			return (err);
11458 	}
11459 	return (err);
11460 }
11461 
11462 /* ARGSUSED */
11463 int
11464 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11465     ip_ioctl_cmd_t *ipi, void *if_req)
11466 {
11467 	ill_t *ill;
11468 	sin6_t *sin6 = (sin6_t *)sin;
11469 	struct lifreq *lifr = (struct lifreq *)if_req;
11470 
11471 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11472 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11473 	if (ipif->ipif_id != 0)
11474 		return (EINVAL);
11475 
11476 	ill = ipif->ipif_ill;
11477 	if (!ill->ill_isv6)
11478 		return (ENXIO);
11479 
11480 	*sin6 = sin6_null;
11481 	sin6->sin6_family = AF_INET6;
11482 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11483 	sin6->sin6_addr = ill->ill_token;
11484 	lifr->lifr_addrlen = ill->ill_token_length;
11485 	return (0);
11486 }
11487 
11488 /*
11489  * Set (hardware) link specific information that might override
11490  * what was acquired through the DL_INFO_ACK.
11491  */
11492 /* ARGSUSED */
11493 int
11494 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11495     ip_ioctl_cmd_t *ipi, void *if_req)
11496 {
11497 	ill_t		*ill = ipif->ipif_ill;
11498 	int		ip_min_mtu;
11499 	struct lifreq	*lifr = (struct lifreq *)if_req;
11500 	lif_ifinfo_req_t *lir;
11501 
11502 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11503 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11504 	lir = &lifr->lifr_ifinfo;
11505 	ASSERT(IAM_WRITER_IPIF(ipif));
11506 
11507 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11508 	if (ipif->ipif_id != 0)
11509 		return (EINVAL);
11510 
11511 	/* Set interface MTU. */
11512 	if (ipif->ipif_isv6)
11513 		ip_min_mtu = IPV6_MIN_MTU;
11514 	else
11515 		ip_min_mtu = IP_MIN_MTU;
11516 
11517 	/*
11518 	 * Verify values before we set anything. Allow zero to
11519 	 * mean unspecified.
11520 	 *
11521 	 * XXX We should be able to set the user-defined lir_mtu to some value
11522 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11523 	 * ill_max_frag value tells us the max MTU that can be handled by the
11524 	 * datalink, whereas the ill_current_frag is dynamically computed for
11525 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11526 	 * since there is currently no way of distinguishing between
11527 	 * administratively fixed link mtu values (e.g., those set via
11528 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11529 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11530 	 * upper-bound.
11531 	 */
11532 	if (lir->lir_maxmtu != 0 &&
11533 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11534 	    lir->lir_maxmtu < ip_min_mtu))
11535 		return (EINVAL);
11536 	if (lir->lir_reachtime != 0 &&
11537 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11538 		return (EINVAL);
11539 	if (lir->lir_reachretrans != 0 &&
11540 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11541 		return (EINVAL);
11542 
11543 	mutex_enter(&ill->ill_lock);
11544 	/*
11545 	 * The dce and fragmentation code can handle changes to ill_mtu
11546 	 * concurrent with sending/fragmenting packets.
11547 	 */
11548 	if (lir->lir_maxmtu != 0)
11549 		ill->ill_user_mtu = lir->lir_maxmtu;
11550 
11551 	if (lir->lir_reachtime != 0)
11552 		ill->ill_reachable_time = lir->lir_reachtime;
11553 
11554 	if (lir->lir_reachretrans != 0)
11555 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11556 
11557 	ill->ill_max_hops = lir->lir_maxhops;
11558 	ill->ill_max_buf = ND_MAX_Q;
11559 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11560 		/*
11561 		 * ill_mtu is the actual interface MTU, obtained as the min
11562 		 * of user-configured mtu and the value announced by the
11563 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11564 		 * we have already made the choice of requiring
11565 		 * ill_user_mtu < ill_current_frag by the time we get here,
11566 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11567 		 * here.
11568 		 */
11569 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11570 	}
11571 	mutex_exit(&ill->ill_lock);
11572 
11573 	/*
11574 	 * Make sure all dce_generation checks find out
11575 	 * that ill_mtu has changed.
11576 	 */
11577 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11578 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11579 
11580 	/*
11581 	 * Refresh IPMP meta-interface MTU if necessary.
11582 	 */
11583 	if (IS_UNDER_IPMP(ill))
11584 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11585 
11586 	return (0);
11587 }
11588 
11589 /* ARGSUSED */
11590 int
11591 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11592     ip_ioctl_cmd_t *ipi, void *if_req)
11593 {
11594 	struct lif_ifinfo_req *lir;
11595 	ill_t *ill = ipif->ipif_ill;
11596 
11597 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11598 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11599 	if (ipif->ipif_id != 0)
11600 		return (EINVAL);
11601 
11602 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11603 	lir->lir_maxhops = ill->ill_max_hops;
11604 	lir->lir_reachtime = ill->ill_reachable_time;
11605 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11606 	lir->lir_maxmtu = ill->ill_mtu;
11607 
11608 	return (0);
11609 }
11610 
11611 /*
11612  * Return best guess as to the subnet mask for the specified address.
11613  * Based on the subnet masks for all the configured interfaces.
11614  *
11615  * We end up returning a zero mask in the case of default, multicast or
11616  * experimental.
11617  */
11618 static ipaddr_t
11619 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11620 {
11621 	ipaddr_t net_mask;
11622 	ill_t	*ill;
11623 	ipif_t	*ipif;
11624 	ill_walk_context_t ctx;
11625 	ipif_t	*fallback_ipif = NULL;
11626 
11627 	net_mask = ip_net_mask(addr);
11628 	if (net_mask == 0) {
11629 		*ipifp = NULL;
11630 		return (0);
11631 	}
11632 
11633 	/* Let's check to see if this is maybe a local subnet route. */
11634 	/* this function only applies to IPv4 interfaces */
11635 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11636 	ill = ILL_START_WALK_V4(&ctx, ipst);
11637 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11638 		mutex_enter(&ill->ill_lock);
11639 		for (ipif = ill->ill_ipif; ipif != NULL;
11640 		    ipif = ipif->ipif_next) {
11641 			if (IPIF_IS_CONDEMNED(ipif))
11642 				continue;
11643 			if (!(ipif->ipif_flags & IPIF_UP))
11644 				continue;
11645 			if ((ipif->ipif_subnet & net_mask) ==
11646 			    (addr & net_mask)) {
11647 				/*
11648 				 * Don't trust pt-pt interfaces if there are
11649 				 * other interfaces.
11650 				 */
11651 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11652 					if (fallback_ipif == NULL) {
11653 						ipif_refhold_locked(ipif);
11654 						fallback_ipif = ipif;
11655 					}
11656 					continue;
11657 				}
11658 
11659 				/*
11660 				 * Fine. Just assume the same net mask as the
11661 				 * directly attached subnet interface is using.
11662 				 */
11663 				ipif_refhold_locked(ipif);
11664 				mutex_exit(&ill->ill_lock);
11665 				rw_exit(&ipst->ips_ill_g_lock);
11666 				if (fallback_ipif != NULL)
11667 					ipif_refrele(fallback_ipif);
11668 				*ipifp = ipif;
11669 				return (ipif->ipif_net_mask);
11670 			}
11671 		}
11672 		mutex_exit(&ill->ill_lock);
11673 	}
11674 	rw_exit(&ipst->ips_ill_g_lock);
11675 
11676 	*ipifp = fallback_ipif;
11677 	return ((fallback_ipif != NULL) ?
11678 	    fallback_ipif->ipif_net_mask : net_mask);
11679 }
11680 
11681 /*
11682  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11683  */
11684 static void
11685 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11686 {
11687 	IOCP	iocp;
11688 	ipft_t	*ipft;
11689 	ipllc_t	*ipllc;
11690 	mblk_t	*mp1;
11691 	cred_t	*cr;
11692 	int	error = 0;
11693 	conn_t	*connp;
11694 
11695 	ip1dbg(("ip_wput_ioctl"));
11696 	iocp = (IOCP)mp->b_rptr;
11697 	mp1 = mp->b_cont;
11698 	if (mp1 == NULL) {
11699 		iocp->ioc_error = EINVAL;
11700 		mp->b_datap->db_type = M_IOCNAK;
11701 		iocp->ioc_count = 0;
11702 		qreply(q, mp);
11703 		return;
11704 	}
11705 
11706 	/*
11707 	 * These IOCTLs provide various control capabilities to
11708 	 * upstream agents such as ULPs and processes.	There
11709 	 * are currently two such IOCTLs implemented.  They
11710 	 * are used by TCP to provide update information for
11711 	 * existing IREs and to forcibly delete an IRE for a
11712 	 * host that is not responding, thereby forcing an
11713 	 * attempt at a new route.
11714 	 */
11715 	iocp->ioc_error = EINVAL;
11716 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11717 		goto done;
11718 
11719 	ipllc = (ipllc_t *)mp1->b_rptr;
11720 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11721 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11722 			break;
11723 	}
11724 	/*
11725 	 * prefer credential from mblk over ioctl;
11726 	 * see ip_sioctl_copyin_setup
11727 	 */
11728 	cr = msg_getcred(mp, NULL);
11729 	if (cr == NULL)
11730 		cr = iocp->ioc_cr;
11731 
11732 	/*
11733 	 * Refhold the conn in case the request gets queued up in some lookup
11734 	 */
11735 	ASSERT(CONN_Q(q));
11736 	connp = Q_TO_CONN(q);
11737 	CONN_INC_REF(connp);
11738 	CONN_INC_IOCTLREF(connp);
11739 	if (ipft->ipft_pfi &&
11740 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11741 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11742 		error = (*ipft->ipft_pfi)(q,
11743 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11744 	}
11745 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11746 		/*
11747 		 * CONN_OPER_PENDING_DONE happens in the function called
11748 		 * through ipft_pfi above.
11749 		 */
11750 		return;
11751 	}
11752 
11753 	CONN_DEC_IOCTLREF(connp);
11754 	CONN_OPER_PENDING_DONE(connp);
11755 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11756 		freemsg(mp);
11757 		return;
11758 	}
11759 	iocp->ioc_error = error;
11760 
11761 done:
11762 	mp->b_datap->db_type = M_IOCACK;
11763 	if (iocp->ioc_error)
11764 		iocp->ioc_count = 0;
11765 	qreply(q, mp);
11766 }
11767 
11768 /*
11769  * Assign a unique id for the ipif. This is used by sctp_addr.c
11770  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11771  */
11772 static void
11773 ipif_assign_seqid(ipif_t *ipif)
11774 {
11775 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11776 
11777 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11778 }
11779 
11780 /*
11781  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11782  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11783  * that the clone is complete -- including the seqid -- and the expectation is
11784  * that the caller will either free or overwrite `sipif' before it's unlocked.
11785  */
11786 static void
11787 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11788 {
11789 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11790 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11791 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11792 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11793 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11794 
11795 	dipif->ipif_flags = sipif->ipif_flags;
11796 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11797 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11798 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11799 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11800 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11801 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11802 
11803 	/*
11804 	 * As per the comment atop the function, we assume that these sipif
11805 	 * fields will be changed before sipif is unlocked.
11806 	 */
11807 	dipif->ipif_seqid = sipif->ipif_seqid;
11808 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11809 }
11810 
11811 /*
11812  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11813  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11814  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11815  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11816  * down (i.e., no DAD), of the same type, and unlocked.
11817  */
11818 static void
11819 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11820 {
11821 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11822 	ipxop_t *ipx = ipsq->ipsq_xop;
11823 
11824 	ASSERT(sipif != dipif);
11825 	ASSERT(sipif != virgipif);
11826 
11827 	/*
11828 	 * Grab all of the locks that protect the ipif in a defined order.
11829 	 */
11830 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11831 
11832 	ipif_clone(sipif, dipif);
11833 	if (virgipif != NULL) {
11834 		ipif_clone(virgipif, sipif);
11835 		mi_free(virgipif);
11836 	}
11837 
11838 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11839 
11840 	/*
11841 	 * Transfer ownership of the current xop, if necessary.
11842 	 */
11843 	if (ipx->ipx_current_ipif == sipif) {
11844 		ASSERT(ipx->ipx_pending_ipif == NULL);
11845 		mutex_enter(&ipx->ipx_lock);
11846 		ipx->ipx_current_ipif = dipif;
11847 		mutex_exit(&ipx->ipx_lock);
11848 	}
11849 
11850 	if (virgipif == NULL)
11851 		mi_free(sipif);
11852 }
11853 
11854 /*
11855  * checks if:
11856  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11857  *	- logical interface is within the allowed range
11858  */
11859 static int
11860 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11861 {
11862 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11863 		return (ENAMETOOLONG);
11864 
11865 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11866 		return (ERANGE);
11867 	return (0);
11868 }
11869 
11870 /*
11871  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11872  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11873  * be inserted into the first space available in the list. The value of
11874  * ipif_id will then be set to the appropriate value for its position.
11875  */
11876 static int
11877 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11878 {
11879 	ill_t *ill;
11880 	ipif_t *tipif;
11881 	ipif_t **tipifp;
11882 	int id, err;
11883 	ip_stack_t	*ipst;
11884 
11885 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11886 	    IAM_WRITER_IPIF(ipif));
11887 
11888 	ill = ipif->ipif_ill;
11889 	ASSERT(ill != NULL);
11890 	ipst = ill->ill_ipst;
11891 
11892 	/*
11893 	 * In the case of lo0:0 we already hold the ill_g_lock.
11894 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11895 	 * ipif_insert.
11896 	 */
11897 	if (acquire_g_lock)
11898 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11899 	mutex_enter(&ill->ill_lock);
11900 	id = ipif->ipif_id;
11901 	tipifp = &(ill->ill_ipif);
11902 	if (id == -1) {	/* need to find a real id */
11903 		id = 0;
11904 		while ((tipif = *tipifp) != NULL) {
11905 			ASSERT(tipif->ipif_id >= id);
11906 			if (tipif->ipif_id != id)
11907 				break; /* non-consecutive id */
11908 			id++;
11909 			tipifp = &(tipif->ipif_next);
11910 		}
11911 		if ((err = is_lifname_valid(ill, id)) != 0) {
11912 			mutex_exit(&ill->ill_lock);
11913 			if (acquire_g_lock)
11914 				rw_exit(&ipst->ips_ill_g_lock);
11915 			return (err);
11916 		}
11917 		ipif->ipif_id = id; /* assign new id */
11918 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11919 		/* we have a real id; insert ipif in the right place */
11920 		while ((tipif = *tipifp) != NULL) {
11921 			ASSERT(tipif->ipif_id != id);
11922 			if (tipif->ipif_id > id)
11923 				break; /* found correct location */
11924 			tipifp = &(tipif->ipif_next);
11925 		}
11926 	} else {
11927 		mutex_exit(&ill->ill_lock);
11928 		if (acquire_g_lock)
11929 			rw_exit(&ipst->ips_ill_g_lock);
11930 		return (err);
11931 	}
11932 
11933 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11934 
11935 	ipif->ipif_next = tipif;
11936 	*tipifp = ipif;
11937 	mutex_exit(&ill->ill_lock);
11938 	if (acquire_g_lock)
11939 		rw_exit(&ipst->ips_ill_g_lock);
11940 
11941 	return (0);
11942 }
11943 
11944 static void
11945 ipif_remove(ipif_t *ipif)
11946 {
11947 	ipif_t	**ipifp;
11948 	ill_t	*ill = ipif->ipif_ill;
11949 
11950 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11951 
11952 	mutex_enter(&ill->ill_lock);
11953 	ipifp = &ill->ill_ipif;
11954 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11955 		if (*ipifp == ipif) {
11956 			*ipifp = ipif->ipif_next;
11957 			break;
11958 		}
11959 	}
11960 	mutex_exit(&ill->ill_lock);
11961 }
11962 
11963 /*
11964  * Allocate and initialize a new interface control structure.  (Always
11965  * called as writer.)
11966  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11967  * is not part of the global linked list of ills. ipif_seqid is unique
11968  * in the system and to preserve the uniqueness, it is assigned only
11969  * when ill becomes part of the global list. At that point ill will
11970  * have a name. If it doesn't get assigned here, it will get assigned
11971  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11972  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11973  * the interface flags or any other information from the DL_INFO_ACK for
11974  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11975  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11976  * second DL_INFO_ACK comes in from the driver.
11977  */
11978 static ipif_t *
11979 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11980     boolean_t insert, int *errorp)
11981 {
11982 	int err;
11983 	ipif_t	*ipif;
11984 	ip_stack_t *ipst = ill->ill_ipst;
11985 
11986 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11987 	    ill->ill_name, id, (void *)ill));
11988 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11989 
11990 	if (errorp != NULL)
11991 		*errorp = 0;
11992 
11993 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11994 		if (errorp != NULL)
11995 			*errorp = ENOMEM;
11996 		return (NULL);
11997 	}
11998 	*ipif = ipif_zero;	/* start clean */
11999 
12000 	ipif->ipif_ill = ill;
12001 	ipif->ipif_id = id;	/* could be -1 */
12002 	/*
12003 	 * Inherit the zoneid from the ill; for the shared stack instance
12004 	 * this is always the global zone
12005 	 */
12006 	ipif->ipif_zoneid = ill->ill_zoneid;
12007 
12008 	ipif->ipif_refcnt = 0;
12009 
12010 	if (insert) {
12011 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
12012 			mi_free(ipif);
12013 			if (errorp != NULL)
12014 				*errorp = err;
12015 			return (NULL);
12016 		}
12017 		/* -1 id should have been replaced by real id */
12018 		id = ipif->ipif_id;
12019 		ASSERT(id >= 0);
12020 	}
12021 
12022 	if (ill->ill_name[0] != '\0')
12023 		ipif_assign_seqid(ipif);
12024 
12025 	/*
12026 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
12027 	 * (which must not exist yet because the zeroth ipif is created once
12028 	 * per ill).  However, do not not link it to the ipmp_grp_t until
12029 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12030 	 */
12031 	if (id == 0 && IS_IPMP(ill)) {
12032 		if (ipmp_illgrp_create(ill) == NULL) {
12033 			if (insert) {
12034 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12035 				ipif_remove(ipif);
12036 				rw_exit(&ipst->ips_ill_g_lock);
12037 			}
12038 			mi_free(ipif);
12039 			if (errorp != NULL)
12040 				*errorp = ENOMEM;
12041 			return (NULL);
12042 		}
12043 	}
12044 
12045 	/*
12046 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12047 	 * not up and can't be looked up until the ioctl completes and the
12048 	 * IPIF_CHANGING flag is cleared.
12049 	 */
12050 	mutex_enter(&ill->ill_lock);
12051 
12052 	ipif->ipif_ire_type = ire_type;
12053 
12054 	if (ipif->ipif_isv6) {
12055 		ill->ill_flags |= ILLF_IPV6;
12056 	} else {
12057 		ipaddr_t inaddr_any = INADDR_ANY;
12058 
12059 		ill->ill_flags |= ILLF_IPV4;
12060 
12061 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12062 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12063 		    &ipif->ipif_v6lcl_addr);
12064 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12065 		    &ipif->ipif_v6subnet);
12066 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12067 		    &ipif->ipif_v6net_mask);
12068 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12069 		    &ipif->ipif_v6brd_addr);
12070 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12071 		    &ipif->ipif_v6pp_dst_addr);
12072 	}
12073 
12074 	/*
12075 	 * Don't set the interface flags etc. now, will do it in
12076 	 * ip_ll_subnet_defaults.
12077 	 */
12078 	if (!initialize)
12079 		goto out;
12080 
12081 	/*
12082 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12083 	 * with no underlying interfaces (and thus an unknown broadcast
12084 	 * address length), but all interfaces that can be placed into an IPMP
12085 	 * group are required to be broadcast-capable.
12086 	 */
12087 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12088 		/*
12089 		 * Later detect lack of DLPI driver multicast capability by
12090 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12091 		 */
12092 		ill->ill_flags |= ILLF_MULTICAST;
12093 		if (!ipif->ipif_isv6)
12094 			ipif->ipif_flags |= IPIF_BROADCAST;
12095 	} else {
12096 		if (ill->ill_net_type != IRE_LOOPBACK) {
12097 			if (ipif->ipif_isv6)
12098 				/*
12099 				 * Note: xresolv interfaces will eventually need
12100 				 * NOARP set here as well, but that will require
12101 				 * those external resolvers to have some
12102 				 * knowledge of that flag and act appropriately.
12103 				 * Not to be changed at present.
12104 				 */
12105 				ill->ill_flags |= ILLF_NONUD;
12106 			else
12107 				ill->ill_flags |= ILLF_NOARP;
12108 		}
12109 		if (ill->ill_phys_addr_length == 0) {
12110 			if (IS_VNI(ill)) {
12111 				ipif->ipif_flags |= IPIF_NOXMIT;
12112 			} else {
12113 				/* pt-pt supports multicast. */
12114 				ill->ill_flags |= ILLF_MULTICAST;
12115 				if (ill->ill_net_type != IRE_LOOPBACK)
12116 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12117 			}
12118 		}
12119 	}
12120 out:
12121 	mutex_exit(&ill->ill_lock);
12122 	return (ipif);
12123 }
12124 
12125 /*
12126  * Remove the neighbor cache entries associated with this logical
12127  * interface.
12128  */
12129 int
12130 ipif_arp_down(ipif_t *ipif)
12131 {
12132 	ill_t	*ill = ipif->ipif_ill;
12133 	int	err = 0;
12134 
12135 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12136 	ASSERT(IAM_WRITER_IPIF(ipif));
12137 
12138 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12139 	    ill_t *, ill, ipif_t *, ipif);
12140 	ipif_nce_down(ipif);
12141 
12142 	/*
12143 	 * If this is the last ipif that is going down and there are no
12144 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12145 	 * clean up ARP completely.
12146 	 */
12147 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12148 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12149 		/*
12150 		 * If this was the last ipif on an IPMP interface, purge any
12151 		 * static ARP entries associated with it.
12152 		 */
12153 		if (IS_IPMP(ill))
12154 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12155 
12156 		/* UNBIND, DETACH */
12157 		err = arp_ll_down(ill);
12158 	}
12159 
12160 	return (err);
12161 }
12162 
12163 /*
12164  * Get the resolver set up for a new IP address.  (Always called as writer.)
12165  * Called both for IPv4 and IPv6 interfaces, though it only does some
12166  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12167  *
12168  * The enumerated value res_act tunes the behavior:
12169  * 	* Res_act_initial: set up all the resolver structures for a new
12170  *	  IP address.
12171  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12172  *	  ARP message in defense of the address.
12173  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12174  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12175  *
12176  * Returns zero on success, or an errno upon failure.
12177  */
12178 int
12179 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12180 {
12181 	ill_t		*ill = ipif->ipif_ill;
12182 	int		err;
12183 	boolean_t	was_dup;
12184 
12185 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12186 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12187 	ASSERT(IAM_WRITER_IPIF(ipif));
12188 
12189 	was_dup = B_FALSE;
12190 	if (res_act == Res_act_initial) {
12191 		ipif->ipif_addr_ready = 0;
12192 		/*
12193 		 * We're bringing an interface up here.  There's no way that we
12194 		 * should need to shut down ARP now.
12195 		 */
12196 		mutex_enter(&ill->ill_lock);
12197 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12198 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12199 			ill->ill_ipif_dup_count--;
12200 			was_dup = B_TRUE;
12201 		}
12202 		mutex_exit(&ill->ill_lock);
12203 	}
12204 	if (ipif->ipif_recovery_id != 0)
12205 		(void) untimeout(ipif->ipif_recovery_id);
12206 	ipif->ipif_recovery_id = 0;
12207 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12208 		ipif->ipif_addr_ready = 1;
12209 		return (0);
12210 	}
12211 	/* NDP will set the ipif_addr_ready flag when it's ready */
12212 	if (ill->ill_isv6)
12213 		return (0);
12214 
12215 	err = ipif_arp_up(ipif, res_act, was_dup);
12216 	return (err);
12217 }
12218 
12219 /*
12220  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12221  * when a link has just gone back up.
12222  */
12223 static void
12224 ipif_nce_start_dad(ipif_t *ipif)
12225 {
12226 	ncec_t *ncec;
12227 	ill_t *ill = ipif->ipif_ill;
12228 	boolean_t isv6 = ill->ill_isv6;
12229 
12230 	if (isv6) {
12231 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12232 		    &ipif->ipif_v6lcl_addr);
12233 	} else {
12234 		ipaddr_t v4addr;
12235 
12236 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12237 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12238 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12239 			/*
12240 			 * If we can't contact ARP for some reason,
12241 			 * that's not really a problem.  Just send
12242 			 * out the routing socket notification that
12243 			 * DAD completion would have done, and continue.
12244 			 */
12245 			ipif_mask_reply(ipif);
12246 			ipif_up_notify(ipif);
12247 			ipif->ipif_addr_ready = 1;
12248 			return;
12249 		}
12250 
12251 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12252 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12253 	}
12254 
12255 	if (ncec == NULL) {
12256 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12257 		    (void *)ipif));
12258 		return;
12259 	}
12260 	if (!nce_restart_dad(ncec)) {
12261 		/*
12262 		 * If we can't restart DAD for some reason, that's not really a
12263 		 * problem.  Just send out the routing socket notification that
12264 		 * DAD completion would have done, and continue.
12265 		 */
12266 		ipif_up_notify(ipif);
12267 		ipif->ipif_addr_ready = 1;
12268 	}
12269 	ncec_refrele(ncec);
12270 }
12271 
12272 /*
12273  * Restart duplicate address detection on all interfaces on the given ill.
12274  *
12275  * This is called when an interface transitions from down to up
12276  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12277  *
12278  * Note that since the underlying physical link has transitioned, we must cause
12279  * at least one routing socket message to be sent here, either via DAD
12280  * completion or just by default on the first ipif.  (If we don't do this, then
12281  * in.mpathd will see long delays when doing link-based failure recovery.)
12282  */
12283 void
12284 ill_restart_dad(ill_t *ill, boolean_t went_up)
12285 {
12286 	ipif_t *ipif;
12287 
12288 	if (ill == NULL)
12289 		return;
12290 
12291 	/*
12292 	 * If layer two doesn't support duplicate address detection, then just
12293 	 * send the routing socket message now and be done with it.
12294 	 */
12295 	if (!ill->ill_isv6 && arp_no_defense) {
12296 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12297 		return;
12298 	}
12299 
12300 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12301 		if (went_up) {
12302 
12303 			if (ipif->ipif_flags & IPIF_UP) {
12304 				ipif_nce_start_dad(ipif);
12305 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12306 				/*
12307 				 * kick off the bring-up process now.
12308 				 */
12309 				ipif_do_recovery(ipif);
12310 			} else {
12311 				/*
12312 				 * Unfortunately, the first ipif is "special"
12313 				 * and represents the underlying ill in the
12314 				 * routing socket messages.  Thus, when this
12315 				 * one ipif is down, we must still notify so
12316 				 * that the user knows the IFF_RUNNING status
12317 				 * change.  (If the first ipif is up, then
12318 				 * we'll handle eventual routing socket
12319 				 * notification via DAD completion.)
12320 				 */
12321 				if (ipif == ill->ill_ipif) {
12322 					ip_rts_ifmsg(ill->ill_ipif,
12323 					    RTSQ_DEFAULT);
12324 				}
12325 			}
12326 		} else {
12327 			/*
12328 			 * After link down, we'll need to send a new routing
12329 			 * message when the link comes back, so clear
12330 			 * ipif_addr_ready.
12331 			 */
12332 			ipif->ipif_addr_ready = 0;
12333 		}
12334 	}
12335 
12336 	/*
12337 	 * If we've torn down links, then notify the user right away.
12338 	 */
12339 	if (!went_up)
12340 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12341 }
12342 
12343 static void
12344 ipsq_delete(ipsq_t *ipsq)
12345 {
12346 	ipxop_t *ipx = ipsq->ipsq_xop;
12347 
12348 	ipsq->ipsq_ipst = NULL;
12349 	ASSERT(ipsq->ipsq_phyint == NULL);
12350 	ASSERT(ipsq->ipsq_xop != NULL);
12351 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12352 	ASSERT(ipx->ipx_pending_mp == NULL);
12353 	kmem_free(ipsq, sizeof (ipsq_t));
12354 }
12355 
12356 static int
12357 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12358 {
12359 	int err = 0;
12360 	ipif_t *ipif;
12361 
12362 	if (ill == NULL)
12363 		return (0);
12364 
12365 	ASSERT(IAM_WRITER_ILL(ill));
12366 	ill->ill_up_ipifs = B_TRUE;
12367 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12368 		if (ipif->ipif_was_up) {
12369 			if (!(ipif->ipif_flags & IPIF_UP))
12370 				err = ipif_up(ipif, q, mp);
12371 			ipif->ipif_was_up = B_FALSE;
12372 			if (err != 0) {
12373 				ASSERT(err == EINPROGRESS);
12374 				return (err);
12375 			}
12376 		}
12377 	}
12378 	ill->ill_up_ipifs = B_FALSE;
12379 	return (0);
12380 }
12381 
12382 /*
12383  * This function is called to bring up all the ipifs that were up before
12384  * bringing the ill down via ill_down_ipifs().
12385  */
12386 int
12387 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12388 {
12389 	int err;
12390 
12391 	ASSERT(IAM_WRITER_ILL(ill));
12392 
12393 	if (ill->ill_replumbing) {
12394 		ill->ill_replumbing = 0;
12395 		/*
12396 		 * Send down REPLUMB_DONE notification followed by the
12397 		 * BIND_REQ on the arp stream.
12398 		 */
12399 		if (!ill->ill_isv6)
12400 			arp_send_replumb_conf(ill);
12401 	}
12402 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12403 	if (err != 0)
12404 		return (err);
12405 
12406 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12407 }
12408 
12409 /*
12410  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12411  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12412  */
12413 static void
12414 ill_down_ipifs(ill_t *ill, boolean_t logical)
12415 {
12416 	ipif_t *ipif;
12417 
12418 	ASSERT(IAM_WRITER_ILL(ill));
12419 
12420 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12421 		/*
12422 		 * We go through the ipif_down logic even if the ipif
12423 		 * is already down, since routes can be added based
12424 		 * on down ipifs. Going through ipif_down once again
12425 		 * will delete any IREs created based on these routes.
12426 		 */
12427 		if (ipif->ipif_flags & IPIF_UP)
12428 			ipif->ipif_was_up = B_TRUE;
12429 
12430 		if (logical) {
12431 			(void) ipif_logical_down(ipif, NULL, NULL);
12432 			ipif_non_duplicate(ipif);
12433 			(void) ipif_down_tail(ipif);
12434 		} else {
12435 			(void) ipif_down(ipif, NULL, NULL);
12436 		}
12437 	}
12438 }
12439 
12440 /*
12441  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12442  * a look again at valid source addresses.
12443  * This should be called each time after the set of source addresses has been
12444  * changed.
12445  */
12446 void
12447 ip_update_source_selection(ip_stack_t *ipst)
12448 {
12449 	/* We skip past SRC_GENERATION_VERIFY */
12450 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12451 	    SRC_GENERATION_VERIFY)
12452 		atomic_add_32(&ipst->ips_src_generation, 1);
12453 }
12454 
12455 /*
12456  * Finish the group join started in ip_sioctl_groupname().
12457  */
12458 /* ARGSUSED */
12459 static void
12460 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12461 {
12462 	ill_t		*ill = q->q_ptr;
12463 	phyint_t	*phyi = ill->ill_phyint;
12464 	ipmp_grp_t	*grp = phyi->phyint_grp;
12465 	ip_stack_t	*ipst = ill->ill_ipst;
12466 
12467 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12468 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12469 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12470 
12471 	if (phyi->phyint_illv4 != NULL) {
12472 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12473 		VERIFY(grp->gr_pendv4-- > 0);
12474 		rw_exit(&ipst->ips_ipmp_lock);
12475 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12476 	}
12477 	if (phyi->phyint_illv6 != NULL) {
12478 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12479 		VERIFY(grp->gr_pendv6-- > 0);
12480 		rw_exit(&ipst->ips_ipmp_lock);
12481 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12482 	}
12483 	freemsg(mp);
12484 }
12485 
12486 /*
12487  * Process an SIOCSLIFGROUPNAME request.
12488  */
12489 /* ARGSUSED */
12490 int
12491 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12492     ip_ioctl_cmd_t *ipip, void *ifreq)
12493 {
12494 	struct lifreq	*lifr = ifreq;
12495 	ill_t		*ill = ipif->ipif_ill;
12496 	ip_stack_t	*ipst = ill->ill_ipst;
12497 	phyint_t	*phyi = ill->ill_phyint;
12498 	ipmp_grp_t	*grp = phyi->phyint_grp;
12499 	mblk_t		*ipsq_mp;
12500 	int		err = 0;
12501 
12502 	/*
12503 	 * Note that phyint_grp can only change here, where we're exclusive.
12504 	 */
12505 	ASSERT(IAM_WRITER_ILL(ill));
12506 
12507 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12508 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12509 		return (EINVAL);
12510 
12511 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12512 
12513 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12514 
12515 	/*
12516 	 * If the name hasn't changed, there's nothing to do.
12517 	 */
12518 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12519 		goto unlock;
12520 
12521 	/*
12522 	 * Handle requests to rename an IPMP meta-interface.
12523 	 *
12524 	 * Note that creation of the IPMP meta-interface is handled in
12525 	 * userland through the standard plumbing sequence.  As part of the
12526 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12527 	 * the name of the interface (see ipif_set_values_tail()).
12528 	 */
12529 	if (IS_IPMP(ill)) {
12530 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12531 		goto unlock;
12532 	}
12533 
12534 	/*
12535 	 * Handle requests to add or remove an IP interface from a group.
12536 	 */
12537 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12538 		/*
12539 		 * Moves are handled by first removing the interface from
12540 		 * its existing group, and then adding it to another group.
12541 		 * So, fail if it's already in a group.
12542 		 */
12543 		if (IS_UNDER_IPMP(ill)) {
12544 			err = EALREADY;
12545 			goto unlock;
12546 		}
12547 
12548 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12549 		if (grp == NULL) {
12550 			err = ENOENT;
12551 			goto unlock;
12552 		}
12553 
12554 		/*
12555 		 * Check if the phyint and its ills are suitable for
12556 		 * inclusion into the group.
12557 		 */
12558 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12559 			goto unlock;
12560 
12561 		/*
12562 		 * Checks pass; join the group, and enqueue the remaining
12563 		 * illgrp joins for when we've become part of the group xop
12564 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12565 		 * requires an mblk_t to scribble on, and since `mp' will be
12566 		 * freed as part of completing the ioctl, allocate another.
12567 		 */
12568 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12569 			err = ENOMEM;
12570 			goto unlock;
12571 		}
12572 
12573 		/*
12574 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12575 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12576 		 * before ip_join_illgrps() is called back.  See the comments
12577 		 * in ip_sioctl_plink_ipmp() for more.
12578 		 */
12579 		if (phyi->phyint_illv4 != NULL)
12580 			grp->gr_pendv4++;
12581 		if (phyi->phyint_illv6 != NULL)
12582 			grp->gr_pendv6++;
12583 
12584 		rw_exit(&ipst->ips_ipmp_lock);
12585 
12586 		ipmp_phyint_join_grp(phyi, grp);
12587 		ill_refhold(ill);
12588 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12589 		    SWITCH_OP, B_FALSE);
12590 		return (0);
12591 	} else {
12592 		/*
12593 		 * Request to remove the interface from a group.  If the
12594 		 * interface is not in a group, this trivially succeeds.
12595 		 */
12596 		rw_exit(&ipst->ips_ipmp_lock);
12597 		if (IS_UNDER_IPMP(ill))
12598 			ipmp_phyint_leave_grp(phyi);
12599 		return (0);
12600 	}
12601 unlock:
12602 	rw_exit(&ipst->ips_ipmp_lock);
12603 	return (err);
12604 }
12605 
12606 /*
12607  * Process an SIOCGLIFBINDING request.
12608  */
12609 /* ARGSUSED */
12610 int
12611 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12612     ip_ioctl_cmd_t *ipip, void *ifreq)
12613 {
12614 	ill_t		*ill;
12615 	struct lifreq	*lifr = ifreq;
12616 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12617 
12618 	if (!IS_IPMP(ipif->ipif_ill))
12619 		return (EINVAL);
12620 
12621 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12622 	if ((ill = ipif->ipif_bound_ill) == NULL)
12623 		lifr->lifr_binding[0] = '\0';
12624 	else
12625 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12626 	rw_exit(&ipst->ips_ipmp_lock);
12627 	return (0);
12628 }
12629 
12630 /*
12631  * Process an SIOCGLIFGROUPNAME request.
12632  */
12633 /* ARGSUSED */
12634 int
12635 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12636     ip_ioctl_cmd_t *ipip, void *ifreq)
12637 {
12638 	ipmp_grp_t	*grp;
12639 	struct lifreq	*lifr = ifreq;
12640 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12641 
12642 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12643 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12644 		lifr->lifr_groupname[0] = '\0';
12645 	else
12646 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12647 	rw_exit(&ipst->ips_ipmp_lock);
12648 	return (0);
12649 }
12650 
12651 /*
12652  * Process an SIOCGLIFGROUPINFO request.
12653  */
12654 /* ARGSUSED */
12655 int
12656 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12657     ip_ioctl_cmd_t *ipip, void *dummy)
12658 {
12659 	ipmp_grp_t	*grp;
12660 	lifgroupinfo_t	*lifgr;
12661 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12662 
12663 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12664 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12665 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12666 
12667 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12668 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12669 		rw_exit(&ipst->ips_ipmp_lock);
12670 		return (ENOENT);
12671 	}
12672 	ipmp_grp_info(grp, lifgr);
12673 	rw_exit(&ipst->ips_ipmp_lock);
12674 	return (0);
12675 }
12676 
12677 static void
12678 ill_dl_down(ill_t *ill)
12679 {
12680 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12681 
12682 	/*
12683 	 * The ill is down; unbind but stay attached since we're still
12684 	 * associated with a PPA. If we have negotiated DLPI capabilites
12685 	 * with the data link service provider (IDS_OK) then reset them.
12686 	 * The interval between unbinding and rebinding is potentially
12687 	 * unbounded hence we cannot assume things will be the same.
12688 	 * The DLPI capabilities will be probed again when the data link
12689 	 * is brought up.
12690 	 */
12691 	mblk_t	*mp = ill->ill_unbind_mp;
12692 
12693 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12694 
12695 	if (!ill->ill_replumbing) {
12696 		/* Free all ilms for this ill */
12697 		update_conn_ill(ill, ill->ill_ipst);
12698 	} else {
12699 		ill_leave_multicast(ill);
12700 	}
12701 
12702 	ill->ill_unbind_mp = NULL;
12703 	if (mp != NULL) {
12704 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12705 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12706 		    ill->ill_name));
12707 		mutex_enter(&ill->ill_lock);
12708 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12709 		mutex_exit(&ill->ill_lock);
12710 		/*
12711 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12712 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12713 		 * ill_capability_dld_disable disable rightaway. If this is not
12714 		 * an unplumb operation then the disable happens on receipt of
12715 		 * the capab ack via ip_rput_dlpi_writer ->
12716 		 * ill_capability_ack_thr. In both cases the order of
12717 		 * the operations seen by DLD is capability disable followed
12718 		 * by DL_UNBIND. Also the DLD capability disable needs a
12719 		 * cv_wait'able context.
12720 		 */
12721 		if (ill->ill_state_flags & ILL_CONDEMNED)
12722 			ill_capability_dld_disable(ill);
12723 		ill_capability_reset(ill, B_FALSE);
12724 		ill_dlpi_send(ill, mp);
12725 	}
12726 	mutex_enter(&ill->ill_lock);
12727 	ill->ill_dl_up = 0;
12728 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12729 	mutex_exit(&ill->ill_lock);
12730 }
12731 
12732 void
12733 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12734 {
12735 	union DL_primitives *dlp;
12736 	t_uscalar_t prim;
12737 	boolean_t waitack = B_FALSE;
12738 
12739 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12740 
12741 	dlp = (union DL_primitives *)mp->b_rptr;
12742 	prim = dlp->dl_primitive;
12743 
12744 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12745 	    dl_primstr(prim), prim, ill->ill_name));
12746 
12747 	switch (prim) {
12748 	case DL_PHYS_ADDR_REQ:
12749 	{
12750 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12751 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12752 		break;
12753 	}
12754 	case DL_BIND_REQ:
12755 		mutex_enter(&ill->ill_lock);
12756 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12757 		mutex_exit(&ill->ill_lock);
12758 		break;
12759 	}
12760 
12761 	/*
12762 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12763 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12764 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12765 	 */
12766 	mutex_enter(&ill->ill_lock);
12767 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12768 	    (prim == DL_UNBIND_REQ)) {
12769 		ill->ill_dlpi_pending = prim;
12770 		waitack = B_TRUE;
12771 	}
12772 
12773 	mutex_exit(&ill->ill_lock);
12774 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12775 	    char *, dl_primstr(prim), ill_t *, ill);
12776 	putnext(ill->ill_wq, mp);
12777 
12778 	/*
12779 	 * There is no ack for DL_NOTIFY_CONF messages
12780 	 */
12781 	if (waitack && prim == DL_NOTIFY_CONF)
12782 		ill_dlpi_done(ill, prim);
12783 }
12784 
12785 /*
12786  * Helper function for ill_dlpi_send().
12787  */
12788 /* ARGSUSED */
12789 static void
12790 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12791 {
12792 	ill_dlpi_send(q->q_ptr, mp);
12793 }
12794 
12795 /*
12796  * Send a DLPI control message to the driver but make sure there
12797  * is only one outstanding message. Uses ill_dlpi_pending to tell
12798  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12799  * when an ACK or a NAK is received to process the next queued message.
12800  */
12801 void
12802 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12803 {
12804 	mblk_t **mpp;
12805 
12806 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12807 
12808 	/*
12809 	 * To ensure that any DLPI requests for current exclusive operation
12810 	 * are always completely sent before any DLPI messages for other
12811 	 * operations, require writer access before enqueuing.
12812 	 */
12813 	if (!IAM_WRITER_ILL(ill)) {
12814 		ill_refhold(ill);
12815 		/* qwriter_ip() does the ill_refrele() */
12816 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12817 		    NEW_OP, B_TRUE);
12818 		return;
12819 	}
12820 
12821 	mutex_enter(&ill->ill_lock);
12822 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12823 		/* Must queue message. Tail insertion */
12824 		mpp = &ill->ill_dlpi_deferred;
12825 		while (*mpp != NULL)
12826 			mpp = &((*mpp)->b_next);
12827 
12828 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12829 		    "while %s pending\n", ill->ill_name,
12830 		    dl_primstr(ill->ill_dlpi_pending)));
12831 
12832 		*mpp = mp;
12833 		mutex_exit(&ill->ill_lock);
12834 		return;
12835 	}
12836 	mutex_exit(&ill->ill_lock);
12837 	ill_dlpi_dispatch(ill, mp);
12838 }
12839 
12840 void
12841 ill_capability_send(ill_t *ill, mblk_t *mp)
12842 {
12843 	ill->ill_capab_pending_cnt++;
12844 	ill_dlpi_send(ill, mp);
12845 }
12846 
12847 void
12848 ill_capability_done(ill_t *ill)
12849 {
12850 	ASSERT(ill->ill_capab_pending_cnt != 0);
12851 
12852 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12853 
12854 	ill->ill_capab_pending_cnt--;
12855 	if (ill->ill_capab_pending_cnt == 0 &&
12856 	    ill->ill_dlpi_capab_state == IDCS_OK)
12857 		ill_capability_reset_alloc(ill);
12858 }
12859 
12860 /*
12861  * Send all deferred DLPI messages without waiting for their ACKs.
12862  */
12863 void
12864 ill_dlpi_send_deferred(ill_t *ill)
12865 {
12866 	mblk_t *mp, *nextmp;
12867 
12868 	/*
12869 	 * Clear ill_dlpi_pending so that the message is not queued in
12870 	 * ill_dlpi_send().
12871 	 */
12872 	mutex_enter(&ill->ill_lock);
12873 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12874 	mp = ill->ill_dlpi_deferred;
12875 	ill->ill_dlpi_deferred = NULL;
12876 	mutex_exit(&ill->ill_lock);
12877 
12878 	for (; mp != NULL; mp = nextmp) {
12879 		nextmp = mp->b_next;
12880 		mp->b_next = NULL;
12881 		ill_dlpi_send(ill, mp);
12882 	}
12883 }
12884 
12885 /*
12886  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12887  * or M_HANGUP
12888  */
12889 static void
12890 ill_dlpi_clear_deferred(ill_t *ill)
12891 {
12892 	mblk_t	*mp, *nextmp;
12893 
12894 	mutex_enter(&ill->ill_lock);
12895 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12896 	mp = ill->ill_dlpi_deferred;
12897 	ill->ill_dlpi_deferred = NULL;
12898 	mutex_exit(&ill->ill_lock);
12899 
12900 	for (; mp != NULL; mp = nextmp) {
12901 		nextmp = mp->b_next;
12902 		inet_freemsg(mp);
12903 	}
12904 }
12905 
12906 /*
12907  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12908  */
12909 boolean_t
12910 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12911 {
12912 	t_uscalar_t pending;
12913 
12914 	mutex_enter(&ill->ill_lock);
12915 	if (ill->ill_dlpi_pending == prim) {
12916 		mutex_exit(&ill->ill_lock);
12917 		return (B_TRUE);
12918 	}
12919 
12920 	/*
12921 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12922 	 * without waiting, so don't print any warnings in that case.
12923 	 */
12924 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12925 		mutex_exit(&ill->ill_lock);
12926 		return (B_FALSE);
12927 	}
12928 	pending = ill->ill_dlpi_pending;
12929 	mutex_exit(&ill->ill_lock);
12930 
12931 	if (pending == DL_PRIM_INVAL) {
12932 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12933 		    "received unsolicited ack for %s on %s\n",
12934 		    dl_primstr(prim), ill->ill_name);
12935 	} else {
12936 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12937 		    "received unexpected ack for %s on %s (expecting %s)\n",
12938 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12939 	}
12940 	return (B_FALSE);
12941 }
12942 
12943 /*
12944  * Complete the current DLPI operation associated with `prim' on `ill' and
12945  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12946  * operations and the ill's current exclusive IPSQ operation has finished
12947  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12948  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12949  * the comments above ipsq_current_finish() for details.
12950  */
12951 void
12952 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12953 {
12954 	mblk_t *mp;
12955 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12956 	ipxop_t *ipx = ipsq->ipsq_xop;
12957 
12958 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12959 	mutex_enter(&ill->ill_lock);
12960 
12961 	ASSERT(prim != DL_PRIM_INVAL);
12962 	ASSERT(ill->ill_dlpi_pending == prim);
12963 
12964 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12965 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12966 
12967 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12968 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12969 		if (ipx->ipx_current_done) {
12970 			mutex_enter(&ipx->ipx_lock);
12971 			ipx->ipx_current_ipif = NULL;
12972 			mutex_exit(&ipx->ipx_lock);
12973 		}
12974 		cv_signal(&ill->ill_cv);
12975 		mutex_exit(&ill->ill_lock);
12976 		return;
12977 	}
12978 
12979 	ill->ill_dlpi_deferred = mp->b_next;
12980 	mp->b_next = NULL;
12981 	mutex_exit(&ill->ill_lock);
12982 
12983 	ill_dlpi_dispatch(ill, mp);
12984 }
12985 
12986 /*
12987  * Queue a (multicast) DLPI control message to be sent to the driver by
12988  * later calling ill_dlpi_send_queued.
12989  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12990  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12991  * for the same group to race.
12992  * We send DLPI control messages in order using ill_lock.
12993  * For IPMP we should be called on the cast_ill.
12994  */
12995 void
12996 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12997 {
12998 	mblk_t **mpp;
12999 
13000 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
13001 
13002 	mutex_enter(&ill->ill_lock);
13003 	/* Must queue message. Tail insertion */
13004 	mpp = &ill->ill_dlpi_deferred;
13005 	while (*mpp != NULL)
13006 		mpp = &((*mpp)->b_next);
13007 
13008 	*mpp = mp;
13009 	mutex_exit(&ill->ill_lock);
13010 }
13011 
13012 /*
13013  * Send the messages that were queued. Make sure there is only
13014  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
13015  * when an ACK or a NAK is received to process the next queued message.
13016  * For IPMP we are called on the upper ill, but when send what is queued
13017  * on the cast_ill.
13018  */
13019 void
13020 ill_dlpi_send_queued(ill_t *ill)
13021 {
13022 	mblk_t	*mp;
13023 	union DL_primitives *dlp;
13024 	t_uscalar_t prim;
13025 	ill_t *release_ill = NULL;
13026 
13027 	if (IS_IPMP(ill)) {
13028 		/* On the upper IPMP ill. */
13029 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13030 		if (release_ill == NULL) {
13031 			/* Avoid ever sending anything down to the ipmpstub */
13032 			return;
13033 		}
13034 		ill = release_ill;
13035 	}
13036 	mutex_enter(&ill->ill_lock);
13037 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
13038 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13039 			/* Can't send. Somebody else will send it */
13040 			mutex_exit(&ill->ill_lock);
13041 			goto done;
13042 		}
13043 		ill->ill_dlpi_deferred = mp->b_next;
13044 		mp->b_next = NULL;
13045 		if (!ill->ill_dl_up) {
13046 			/*
13047 			 * Nobody there. All multicast addresses will be
13048 			 * re-joined when we get the DL_BIND_ACK bringing the
13049 			 * interface up.
13050 			 */
13051 			freemsg(mp);
13052 			continue;
13053 		}
13054 		dlp = (union DL_primitives *)mp->b_rptr;
13055 		prim = dlp->dl_primitive;
13056 
13057 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13058 		    (prim == DL_UNBIND_REQ)) {
13059 			ill->ill_dlpi_pending = prim;
13060 		}
13061 		mutex_exit(&ill->ill_lock);
13062 
13063 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13064 		    char *, dl_primstr(prim), ill_t *, ill);
13065 		putnext(ill->ill_wq, mp);
13066 		mutex_enter(&ill->ill_lock);
13067 	}
13068 	mutex_exit(&ill->ill_lock);
13069 done:
13070 	if (release_ill != NULL)
13071 		ill_refrele(release_ill);
13072 }
13073 
13074 /*
13075  * Queue an IP (IGMP/MLD) message to be sent by IP from
13076  * ill_mcast_send_queued
13077  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13078  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13079  * group to race.
13080  * We send them in order using ill_lock.
13081  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13082  */
13083 void
13084 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13085 {
13086 	mblk_t **mpp;
13087 	ill_t *release_ill = NULL;
13088 
13089 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13090 
13091 	if (IS_IPMP(ill)) {
13092 		/* On the upper IPMP ill. */
13093 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13094 		if (release_ill == NULL) {
13095 			/* Discard instead of queuing for the ipmp interface */
13096 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13097 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13098 			    mp, ill);
13099 			freemsg(mp);
13100 			return;
13101 		}
13102 		ill = release_ill;
13103 	}
13104 
13105 	mutex_enter(&ill->ill_lock);
13106 	/* Must queue message. Tail insertion */
13107 	mpp = &ill->ill_mcast_deferred;
13108 	while (*mpp != NULL)
13109 		mpp = &((*mpp)->b_next);
13110 
13111 	*mpp = mp;
13112 	mutex_exit(&ill->ill_lock);
13113 	if (release_ill != NULL)
13114 		ill_refrele(release_ill);
13115 }
13116 
13117 /*
13118  * Send the IP packets that were queued by ill_mcast_queue.
13119  * These are IGMP/MLD packets.
13120  *
13121  * For IPMP we are called on the upper ill, but when send what is queued
13122  * on the cast_ill.
13123  *
13124  * Request loopback of the report if we are acting as a multicast
13125  * router, so that the process-level routing demon can hear it.
13126  * This will run multiple times for the same group if there are members
13127  * on the same group for multiple ipif's on the same ill. The
13128  * igmp_input/mld_input code will suppress this due to the loopback thus we
13129  * always loopback membership report.
13130  *
13131  * We also need to make sure that this does not get load balanced
13132  * by IPMP. We do this by passing an ill to ip_output_simple.
13133  */
13134 void
13135 ill_mcast_send_queued(ill_t *ill)
13136 {
13137 	mblk_t	*mp;
13138 	ip_xmit_attr_t ixas;
13139 	ill_t *release_ill = NULL;
13140 
13141 	if (IS_IPMP(ill)) {
13142 		/* On the upper IPMP ill. */
13143 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13144 		if (release_ill == NULL) {
13145 			/*
13146 			 * We should have no messages on the ipmp interface
13147 			 * but no point in trying to send them.
13148 			 */
13149 			return;
13150 		}
13151 		ill = release_ill;
13152 	}
13153 	bzero(&ixas, sizeof (ixas));
13154 	ixas.ixa_zoneid = ALL_ZONES;
13155 	ixas.ixa_cred = kcred;
13156 	ixas.ixa_cpid = NOPID;
13157 	ixas.ixa_tsl = NULL;
13158 	/*
13159 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13160 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13161 	 * That is necessary to handle IGMP/MLD snooping switches.
13162 	 */
13163 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13164 	ixas.ixa_ipst = ill->ill_ipst;
13165 
13166 	mutex_enter(&ill->ill_lock);
13167 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13168 		ill->ill_mcast_deferred = mp->b_next;
13169 		mp->b_next = NULL;
13170 		if (!ill->ill_dl_up) {
13171 			/*
13172 			 * Nobody there. Just drop the ip packets.
13173 			 * IGMP/MLD will resend later, if this is a replumb.
13174 			 */
13175 			freemsg(mp);
13176 			continue;
13177 		}
13178 		mutex_enter(&ill->ill_phyint->phyint_lock);
13179 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13180 			/*
13181 			 * When the ill is getting deactivated, we only want to
13182 			 * send the DLPI messages, so drop IGMP/MLD packets.
13183 			 * DLPI messages are handled by ill_dlpi_send_queued()
13184 			 */
13185 			mutex_exit(&ill->ill_phyint->phyint_lock);
13186 			freemsg(mp);
13187 			continue;
13188 		}
13189 		mutex_exit(&ill->ill_phyint->phyint_lock);
13190 		mutex_exit(&ill->ill_lock);
13191 
13192 		/* Check whether we are sending IPv4 or IPv6. */
13193 		if (ill->ill_isv6) {
13194 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13195 
13196 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13197 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13198 		} else {
13199 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13200 
13201 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13202 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13203 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13204 		}
13205 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13206 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13207 		(void) ip_output_simple(mp, &ixas);
13208 		ixa_cleanup(&ixas);
13209 
13210 		mutex_enter(&ill->ill_lock);
13211 	}
13212 	mutex_exit(&ill->ill_lock);
13213 
13214 done:
13215 	if (release_ill != NULL)
13216 		ill_refrele(release_ill);
13217 }
13218 
13219 /*
13220  * Take down a specific interface, but don't lose any information about it.
13221  * (Always called as writer.)
13222  * This function goes through the down sequence even if the interface is
13223  * already down. There are 2 reasons.
13224  * a. Currently we permit interface routes that depend on down interfaces
13225  *    to be added. This behaviour itself is questionable. However it appears
13226  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13227  *    time. We go thru the cleanup in order to remove these routes.
13228  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13229  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13230  *    down, but we need to cleanup i.e. do ill_dl_down and
13231  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13232  *
13233  * IP-MT notes:
13234  *
13235  * Model of reference to interfaces.
13236  *
13237  * The following members in ipif_t track references to the ipif.
13238  *	int     ipif_refcnt;    Active reference count
13239  *
13240  * The following members in ill_t track references to the ill.
13241  *	int             ill_refcnt;     active refcnt
13242  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13243  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13244  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13245  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13246  *
13247  * Reference to an ipif or ill can be obtained in any of the following ways.
13248  *
13249  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13250  * Pointers to ipif / ill from other data structures viz ire and conn.
13251  * Implicit reference to the ipif / ill by holding a reference to the ire.
13252  *
13253  * The ipif/ill lookup functions return a reference held ipif / ill.
13254  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13255  * This is a purely dynamic reference count associated with threads holding
13256  * references to the ipif / ill. Pointers from other structures do not
13257  * count towards this reference count.
13258  *
13259  * ill_ire_cnt is the number of ire's associated with the
13260  * ill. This is incremented whenever a new ire is created referencing the
13261  * ill. This is done atomically inside ire_add_v[46] where the ire is
13262  * actually added to the ire hash table. The count is decremented in
13263  * ire_inactive where the ire is destroyed.
13264  *
13265  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13266  * This is incremented atomically in
13267  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13268  * table. Similarly it is decremented in ncec_inactive() where the ncec
13269  * is destroyed.
13270  *
13271  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13272  * incremented atomically in nce_add() where the nce is actually added to the
13273  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13274  * is destroyed.
13275  *
13276  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13277  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13278  *
13279  * Flow of ioctls involving interface down/up
13280  *
13281  * The following is the sequence of an attempt to set some critical flags on an
13282  * up interface.
13283  * ip_sioctl_flags
13284  * ipif_down
13285  * wait for ipif to be quiescent
13286  * ipif_down_tail
13287  * ip_sioctl_flags_tail
13288  *
13289  * All set ioctls that involve down/up sequence would have a skeleton similar
13290  * to the above. All the *tail functions are called after the refcounts have
13291  * dropped to the appropriate values.
13292  *
13293  * SIOC ioctls during the IPIF_CHANGING interval.
13294  *
13295  * Threads handling SIOC set ioctls serialize on the squeue, but this
13296  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13297  * steps of internal changes to the state, some of which are visible in
13298  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13299  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13300  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13301  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13302  * the current exclusive operation completes. The IPIF_CHANGING check
13303  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13304  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13305  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13306  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13307  * until we release the ipsq_lock, even though the ill/ipif state flags
13308  * can change after we drop the ill_lock.
13309  */
13310 int
13311 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13312 {
13313 	ill_t		*ill = ipif->ipif_ill;
13314 	conn_t		*connp;
13315 	boolean_t	success;
13316 	boolean_t	ipif_was_up = B_FALSE;
13317 	ip_stack_t	*ipst = ill->ill_ipst;
13318 
13319 	ASSERT(IAM_WRITER_IPIF(ipif));
13320 
13321 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13322 
13323 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13324 	    ill_t *, ill, ipif_t *, ipif);
13325 
13326 	if (ipif->ipif_flags & IPIF_UP) {
13327 		mutex_enter(&ill->ill_lock);
13328 		ipif->ipif_flags &= ~IPIF_UP;
13329 		ASSERT(ill->ill_ipif_up_count > 0);
13330 		--ill->ill_ipif_up_count;
13331 		mutex_exit(&ill->ill_lock);
13332 		ipif_was_up = B_TRUE;
13333 		/* Update status in SCTP's list */
13334 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13335 		ill_nic_event_dispatch(ipif->ipif_ill,
13336 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13337 	}
13338 
13339 	/*
13340 	 * Removal of the last ipif from an ill may result in a DL_UNBIND
13341 	 * being sent to the driver, and we must not send any data packets to
13342 	 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13343 	 * ire and nce entries used in the data path will be cleaned
13344 	 * up, and we also set  the ILL_DOWN_IN_PROGRESS bit to make
13345 	 * sure on new entries will be added until the ill is bound
13346 	 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13347 	 * receipt of a DL_BIND_ACK.
13348 	 */
13349 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13350 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13351 	    ill->ill_dl_up) {
13352 		ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13353 	}
13354 
13355 	/*
13356 	 * Blow away memberships we established in ipif_multicast_up().
13357 	 */
13358 	ipif_multicast_down(ipif);
13359 
13360 	/*
13361 	 * Remove from the mapping for __sin6_src_id. We insert only
13362 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13363 	 * stored as mapped addresses, we need to check for mapped
13364 	 * INADDR_ANY also.
13365 	 */
13366 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13367 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13368 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13369 		int err;
13370 
13371 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13372 		    ipif->ipif_zoneid, ipst);
13373 		if (err != 0) {
13374 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13375 		}
13376 	}
13377 
13378 	if (ipif_was_up) {
13379 		/* only delete if we'd added ire's before */
13380 		if (ipif->ipif_isv6)
13381 			ipif_delete_ires_v6(ipif);
13382 		else
13383 			ipif_delete_ires_v4(ipif);
13384 	}
13385 
13386 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13387 		/*
13388 		 * Since the interface is now down, it may have just become
13389 		 * inactive.  Note that this needs to be done even for a
13390 		 * lll_logical_down(), or ARP entries will not get correctly
13391 		 * restored when the interface comes back up.
13392 		 */
13393 		if (IS_UNDER_IPMP(ill))
13394 			ipmp_ill_refresh_active(ill);
13395 	}
13396 
13397 	/*
13398 	 * neighbor-discovery or arp entries for this interface. The ipif
13399 	 * has to be quiesced, so we walk all the nce's and delete those
13400 	 * that point at the ipif->ipif_ill. At the same time, we also
13401 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13402 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13403 	 * that for ipif_down_tail()
13404 	 */
13405 	ipif_nce_down(ipif);
13406 
13407 	/*
13408 	 * If this is the last ipif on the ill, we also need to remove
13409 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13410 	 * never succeed.
13411 	 */
13412 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13413 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13414 
13415 	/*
13416 	 * Walk all CONNs that can have a reference on an ire for this
13417 	 * ipif (we actually walk all that now have stale references).
13418 	 */
13419 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13420 
13421 	/*
13422 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13423 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13424 	 * and ill_delete -> ipif_free -> ipif_down
13425 	 */
13426 	if (mp == NULL) {
13427 		ASSERT(q == NULL);
13428 		return (0);
13429 	}
13430 
13431 	if (CONN_Q(q)) {
13432 		connp = Q_TO_CONN(q);
13433 		mutex_enter(&connp->conn_lock);
13434 	} else {
13435 		connp = NULL;
13436 	}
13437 	mutex_enter(&ill->ill_lock);
13438 	/*
13439 	 * Are there any ire's pointing to this ipif that are still active ?
13440 	 * If this is the last ipif going down, are there any ire's pointing
13441 	 * to this ill that are still active ?
13442 	 */
13443 	if (ipif_is_quiescent(ipif)) {
13444 		mutex_exit(&ill->ill_lock);
13445 		if (connp != NULL)
13446 			mutex_exit(&connp->conn_lock);
13447 		return (0);
13448 	}
13449 
13450 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13451 	    ill->ill_name, (void *)ill));
13452 	/*
13453 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13454 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13455 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13456 	 */
13457 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13458 	if (!success) {
13459 		/* The conn is closing. So just return */
13460 		ASSERT(connp != NULL);
13461 		mutex_exit(&ill->ill_lock);
13462 		mutex_exit(&connp->conn_lock);
13463 		return (EINTR);
13464 	}
13465 
13466 	mutex_exit(&ill->ill_lock);
13467 	if (connp != NULL)
13468 		mutex_exit(&connp->conn_lock);
13469 	return (EINPROGRESS);
13470 }
13471 
13472 int
13473 ipif_down_tail(ipif_t *ipif)
13474 {
13475 	ill_t	*ill = ipif->ipif_ill;
13476 	int	err = 0;
13477 
13478 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13479 	    ill_t *, ill, ipif_t *, ipif);
13480 
13481 	/*
13482 	 * Skip any loopback interface (null wq).
13483 	 * If this is the last logical interface on the ill
13484 	 * have ill_dl_down tell the driver we are gone (unbind)
13485 	 * Note that lun 0 can ipif_down even though
13486 	 * there are other logical units that are up.
13487 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13488 	 */
13489 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13490 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13491 	    ill->ill_dl_up) {
13492 		ill_dl_down(ill);
13493 	}
13494 	if (!ipif->ipif_isv6)
13495 		err = ipif_arp_down(ipif);
13496 
13497 	ill->ill_logical_down = 0;
13498 
13499 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13500 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13501 	return (err);
13502 }
13503 
13504 /*
13505  * Bring interface logically down without bringing the physical interface
13506  * down e.g. when the netmask is changed. This avoids long lasting link
13507  * negotiations between an ethernet interface and a certain switches.
13508  */
13509 static int
13510 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13511 {
13512 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13513 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13514 
13515 	/*
13516 	 * The ill_logical_down flag is a transient flag. It is set here
13517 	 * and is cleared once the down has completed in ipif_down_tail.
13518 	 * This flag does not indicate whether the ill stream is in the
13519 	 * DL_BOUND state with the driver. Instead this flag is used by
13520 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13521 	 * the driver. The state of the ill stream i.e. whether it is
13522 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13523 	 */
13524 	ipif->ipif_ill->ill_logical_down = 1;
13525 	return (ipif_down(ipif, q, mp));
13526 }
13527 
13528 /*
13529  * Initiate deallocate of an IPIF. Always called as writer. Called by
13530  * ill_delete or ip_sioctl_removeif.
13531  */
13532 static void
13533 ipif_free(ipif_t *ipif)
13534 {
13535 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13536 
13537 	ASSERT(IAM_WRITER_IPIF(ipif));
13538 
13539 	if (ipif->ipif_recovery_id != 0)
13540 		(void) untimeout(ipif->ipif_recovery_id);
13541 	ipif->ipif_recovery_id = 0;
13542 
13543 	/*
13544 	 * Take down the interface. We can be called either from ill_delete
13545 	 * or from ip_sioctl_removeif.
13546 	 */
13547 	(void) ipif_down(ipif, NULL, NULL);
13548 
13549 	/*
13550 	 * Now that the interface is down, there's no chance it can still
13551 	 * become a duplicate.  Cancel any timer that may have been set while
13552 	 * tearing down.
13553 	 */
13554 	if (ipif->ipif_recovery_id != 0)
13555 		(void) untimeout(ipif->ipif_recovery_id);
13556 	ipif->ipif_recovery_id = 0;
13557 
13558 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13559 	/* Remove pointers to this ill in the multicast routing tables */
13560 	reset_mrt_vif_ipif(ipif);
13561 	/* If necessary, clear the cached source ipif rotor. */
13562 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13563 		ipif->ipif_ill->ill_src_ipif = NULL;
13564 	rw_exit(&ipst->ips_ill_g_lock);
13565 }
13566 
13567 static void
13568 ipif_free_tail(ipif_t *ipif)
13569 {
13570 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13571 
13572 	/*
13573 	 * Need to hold both ill_g_lock and ill_lock while
13574 	 * inserting or removing an ipif from the linked list
13575 	 * of ipifs hanging off the ill.
13576 	 */
13577 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13578 
13579 #ifdef DEBUG
13580 	ipif_trace_cleanup(ipif);
13581 #endif
13582 
13583 	/* Ask SCTP to take it out of it list */
13584 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13585 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13586 
13587 	/* Get it out of the ILL interface list. */
13588 	ipif_remove(ipif);
13589 	rw_exit(&ipst->ips_ill_g_lock);
13590 
13591 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13592 	ASSERT(ipif->ipif_recovery_id == 0);
13593 	ASSERT(ipif->ipif_ire_local == NULL);
13594 	ASSERT(ipif->ipif_ire_if == NULL);
13595 
13596 	/* Free the memory. */
13597 	mi_free(ipif);
13598 }
13599 
13600 /*
13601  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13602  * is zero.
13603  */
13604 void
13605 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13606 {
13607 	char	lbuf[LIFNAMSIZ];
13608 	char	*name;
13609 	size_t	name_len;
13610 
13611 	buf[0] = '\0';
13612 	name = ipif->ipif_ill->ill_name;
13613 	name_len = ipif->ipif_ill->ill_name_length;
13614 	if (ipif->ipif_id != 0) {
13615 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13616 		    ipif->ipif_id);
13617 		name = lbuf;
13618 		name_len = mi_strlen(name) + 1;
13619 	}
13620 	len -= 1;
13621 	buf[len] = '\0';
13622 	len = MIN(len, name_len);
13623 	bcopy(name, buf, len);
13624 }
13625 
13626 /*
13627  * Sets `buf' to an ill name.
13628  */
13629 void
13630 ill_get_name(const ill_t *ill, char *buf, int len)
13631 {
13632 	char	*name;
13633 	size_t	name_len;
13634 
13635 	name = ill->ill_name;
13636 	name_len = ill->ill_name_length;
13637 	len -= 1;
13638 	buf[len] = '\0';
13639 	len = MIN(len, name_len);
13640 	bcopy(name, buf, len);
13641 }
13642 
13643 /*
13644  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13645  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13646  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13647  * (May be called as writer.)
13648  */
13649 static ipif_t *
13650 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13651     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13652 {
13653 	char	*cp;
13654 	char	*endp;
13655 	long	id;
13656 	ill_t	*ill;
13657 	ipif_t	*ipif;
13658 	uint_t	ire_type;
13659 	boolean_t did_alloc = B_FALSE;
13660 	char	last;
13661 
13662 	/*
13663 	 * If the caller wants to us to create the ipif, make sure we have a
13664 	 * valid zoneid
13665 	 */
13666 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13667 
13668 	if (namelen == 0) {
13669 		return (NULL);
13670 	}
13671 
13672 	*exists = B_FALSE;
13673 	/* Look for a colon in the name. */
13674 	endp = &name[namelen];
13675 	for (cp = endp; --cp > name; ) {
13676 		if (*cp == IPIF_SEPARATOR_CHAR)
13677 			break;
13678 	}
13679 
13680 	if (*cp == IPIF_SEPARATOR_CHAR) {
13681 		/*
13682 		 * Reject any non-decimal aliases for logical
13683 		 * interfaces. Aliases with leading zeroes
13684 		 * are also rejected as they introduce ambiguity
13685 		 * in the naming of the interfaces.
13686 		 * In order to confirm with existing semantics,
13687 		 * and to not break any programs/script relying
13688 		 * on that behaviour, if<0>:0 is considered to be
13689 		 * a valid interface.
13690 		 *
13691 		 * If alias has two or more digits and the first
13692 		 * is zero, fail.
13693 		 */
13694 		if (&cp[2] < endp && cp[1] == '0') {
13695 			return (NULL);
13696 		}
13697 	}
13698 
13699 	if (cp <= name) {
13700 		cp = endp;
13701 	}
13702 	last = *cp;
13703 	*cp = '\0';
13704 
13705 	/*
13706 	 * Look up the ILL, based on the portion of the name
13707 	 * before the slash. ill_lookup_on_name returns a held ill.
13708 	 * Temporary to check whether ill exists already. If so
13709 	 * ill_lookup_on_name will clear it.
13710 	 */
13711 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13712 	    &did_alloc, ipst);
13713 	*cp = last;
13714 	if (ill == NULL)
13715 		return (NULL);
13716 
13717 	/* Establish the unit number in the name. */
13718 	id = 0;
13719 	if (cp < endp && *endp == '\0') {
13720 		/* If there was a colon, the unit number follows. */
13721 		cp++;
13722 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13723 			ill_refrele(ill);
13724 			return (NULL);
13725 		}
13726 	}
13727 
13728 	mutex_enter(&ill->ill_lock);
13729 	/* Now see if there is an IPIF with this unit number. */
13730 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13731 		if (ipif->ipif_id == id) {
13732 			if (zoneid != ALL_ZONES &&
13733 			    zoneid != ipif->ipif_zoneid &&
13734 			    ipif->ipif_zoneid != ALL_ZONES) {
13735 				mutex_exit(&ill->ill_lock);
13736 				ill_refrele(ill);
13737 				return (NULL);
13738 			}
13739 			if (IPIF_CAN_LOOKUP(ipif)) {
13740 				ipif_refhold_locked(ipif);
13741 				mutex_exit(&ill->ill_lock);
13742 				if (!did_alloc)
13743 					*exists = B_TRUE;
13744 				/*
13745 				 * Drop locks before calling ill_refrele
13746 				 * since it can potentially call into
13747 				 * ipif_ill_refrele_tail which can end up
13748 				 * in trying to acquire any lock.
13749 				 */
13750 				ill_refrele(ill);
13751 				return (ipif);
13752 			}
13753 		}
13754 	}
13755 
13756 	if (!do_alloc) {
13757 		mutex_exit(&ill->ill_lock);
13758 		ill_refrele(ill);
13759 		return (NULL);
13760 	}
13761 
13762 	/*
13763 	 * If none found, atomically allocate and return a new one.
13764 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13765 	 * to support "receive only" use of lo0:1 etc. as is still done
13766 	 * below as an initial guess.
13767 	 * However, this is now likely to be overriden later in ipif_up_done()
13768 	 * when we know for sure what address has been configured on the
13769 	 * interface, since we might have more than one loopback interface
13770 	 * with a loopback address, e.g. in the case of zones, and all the
13771 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13772 	 */
13773 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13774 		ire_type = IRE_LOOPBACK;
13775 	else
13776 		ire_type = IRE_LOCAL;
13777 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13778 	if (ipif != NULL)
13779 		ipif_refhold_locked(ipif);
13780 	mutex_exit(&ill->ill_lock);
13781 	ill_refrele(ill);
13782 	return (ipif);
13783 }
13784 
13785 /*
13786  * Variant of the above that queues the request on the ipsq when
13787  * IPIF_CHANGING is set.
13788  */
13789 static ipif_t *
13790 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13791     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13792     ip_stack_t *ipst)
13793 {
13794 	char	*cp;
13795 	char	*endp;
13796 	long	id;
13797 	ill_t	*ill;
13798 	ipif_t	*ipif;
13799 	boolean_t did_alloc = B_FALSE;
13800 	ipsq_t	*ipsq;
13801 
13802 	if (error != NULL)
13803 		*error = 0;
13804 
13805 	if (namelen == 0) {
13806 		if (error != NULL)
13807 			*error = ENXIO;
13808 		return (NULL);
13809 	}
13810 
13811 	/* Look for a colon in the name. */
13812 	endp = &name[namelen];
13813 	for (cp = endp; --cp > name; ) {
13814 		if (*cp == IPIF_SEPARATOR_CHAR)
13815 			break;
13816 	}
13817 
13818 	if (*cp == IPIF_SEPARATOR_CHAR) {
13819 		/*
13820 		 * Reject any non-decimal aliases for logical
13821 		 * interfaces. Aliases with leading zeroes
13822 		 * are also rejected as they introduce ambiguity
13823 		 * in the naming of the interfaces.
13824 		 * In order to confirm with existing semantics,
13825 		 * and to not break any programs/script relying
13826 		 * on that behaviour, if<0>:0 is considered to be
13827 		 * a valid interface.
13828 		 *
13829 		 * If alias has two or more digits and the first
13830 		 * is zero, fail.
13831 		 */
13832 		if (&cp[2] < endp && cp[1] == '0') {
13833 			if (error != NULL)
13834 				*error = EINVAL;
13835 			return (NULL);
13836 		}
13837 	}
13838 
13839 	if (cp <= name) {
13840 		cp = endp;
13841 	} else {
13842 		*cp = '\0';
13843 	}
13844 
13845 	/*
13846 	 * Look up the ILL, based on the portion of the name
13847 	 * before the slash. ill_lookup_on_name returns a held ill.
13848 	 * Temporary to check whether ill exists already. If so
13849 	 * ill_lookup_on_name will clear it.
13850 	 */
13851 	ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13852 	if (cp != endp)
13853 		*cp = IPIF_SEPARATOR_CHAR;
13854 	if (ill == NULL)
13855 		return (NULL);
13856 
13857 	/* Establish the unit number in the name. */
13858 	id = 0;
13859 	if (cp < endp && *endp == '\0') {
13860 		/* If there was a colon, the unit number follows. */
13861 		cp++;
13862 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13863 			ill_refrele(ill);
13864 			if (error != NULL)
13865 				*error = ENXIO;
13866 			return (NULL);
13867 		}
13868 	}
13869 
13870 	GRAB_CONN_LOCK(q);
13871 	mutex_enter(&ill->ill_lock);
13872 	/* Now see if there is an IPIF with this unit number. */
13873 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13874 		if (ipif->ipif_id == id) {
13875 			if (zoneid != ALL_ZONES &&
13876 			    zoneid != ipif->ipif_zoneid &&
13877 			    ipif->ipif_zoneid != ALL_ZONES) {
13878 				mutex_exit(&ill->ill_lock);
13879 				RELEASE_CONN_LOCK(q);
13880 				ill_refrele(ill);
13881 				if (error != NULL)
13882 					*error = ENXIO;
13883 				return (NULL);
13884 			}
13885 
13886 			if (!(IPIF_IS_CHANGING(ipif) ||
13887 			    IPIF_IS_CONDEMNED(ipif)) ||
13888 			    IAM_WRITER_IPIF(ipif)) {
13889 				ipif_refhold_locked(ipif);
13890 				mutex_exit(&ill->ill_lock);
13891 				/*
13892 				 * Drop locks before calling ill_refrele
13893 				 * since it can potentially call into
13894 				 * ipif_ill_refrele_tail which can end up
13895 				 * in trying to acquire any lock.
13896 				 */
13897 				RELEASE_CONN_LOCK(q);
13898 				ill_refrele(ill);
13899 				return (ipif);
13900 			} else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13901 				ipsq = ill->ill_phyint->phyint_ipsq;
13902 				mutex_enter(&ipsq->ipsq_lock);
13903 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13904 				mutex_exit(&ill->ill_lock);
13905 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13906 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13907 				mutex_exit(&ipsq->ipsq_lock);
13908 				RELEASE_CONN_LOCK(q);
13909 				ill_refrele(ill);
13910 				if (error != NULL)
13911 					*error = EINPROGRESS;
13912 				return (NULL);
13913 			}
13914 		}
13915 	}
13916 	RELEASE_CONN_LOCK(q);
13917 	mutex_exit(&ill->ill_lock);
13918 	ill_refrele(ill);
13919 	if (error != NULL)
13920 		*error = ENXIO;
13921 	return (NULL);
13922 }
13923 
13924 /*
13925  * This routine is called whenever a new address comes up on an ipif.  If
13926  * we are configured to respond to address mask requests, then we are supposed
13927  * to broadcast an address mask reply at this time.  This routine is also
13928  * called if we are already up, but a netmask change is made.  This is legal
13929  * but might not make the system manager very popular.	(May be called
13930  * as writer.)
13931  */
13932 void
13933 ipif_mask_reply(ipif_t *ipif)
13934 {
13935 	icmph_t	*icmph;
13936 	ipha_t	*ipha;
13937 	mblk_t	*mp;
13938 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13939 	ip_xmit_attr_t ixas;
13940 
13941 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13942 
13943 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13944 		return;
13945 
13946 	/* ICMP mask reply is IPv4 only */
13947 	ASSERT(!ipif->ipif_isv6);
13948 	/* ICMP mask reply is not for a loopback interface */
13949 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13950 
13951 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13952 		return;
13953 
13954 	mp = allocb(REPLY_LEN, BPRI_HI);
13955 	if (mp == NULL)
13956 		return;
13957 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13958 
13959 	ipha = (ipha_t *)mp->b_rptr;
13960 	bzero(ipha, REPLY_LEN);
13961 	*ipha = icmp_ipha;
13962 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13963 	ipha->ipha_src = ipif->ipif_lcl_addr;
13964 	ipha->ipha_dst = ipif->ipif_brd_addr;
13965 	ipha->ipha_length = htons(REPLY_LEN);
13966 	ipha->ipha_ident = 0;
13967 
13968 	icmph = (icmph_t *)&ipha[1];
13969 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13970 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13971 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13972 
13973 	bzero(&ixas, sizeof (ixas));
13974 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13975 	ixas.ixa_zoneid = ALL_ZONES;
13976 	ixas.ixa_ifindex = 0;
13977 	ixas.ixa_ipst = ipst;
13978 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13979 	(void) ip_output_simple(mp, &ixas);
13980 	ixa_cleanup(&ixas);
13981 #undef	REPLY_LEN
13982 }
13983 
13984 /*
13985  * Join the ipif specific multicast groups.
13986  * Must be called after a mapping has been set up in the resolver.  (Always
13987  * called as writer.)
13988  */
13989 void
13990 ipif_multicast_up(ipif_t *ipif)
13991 {
13992 	int err;
13993 	ill_t *ill;
13994 	ilm_t *ilm;
13995 
13996 	ASSERT(IAM_WRITER_IPIF(ipif));
13997 
13998 	ill = ipif->ipif_ill;
13999 
14000 	ip1dbg(("ipif_multicast_up\n"));
14001 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
14002 	    ipif->ipif_allhosts_ilm != NULL)
14003 		return;
14004 
14005 	if (ipif->ipif_isv6) {
14006 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
14007 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
14008 
14009 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
14010 
14011 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
14012 			return;
14013 
14014 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14015 
14016 		/*
14017 		 * Join the all hosts multicast address.  We skip this for
14018 		 * underlying IPMP interfaces since they should be invisible.
14019 		 */
14020 		if (!IS_UNDER_IPMP(ill)) {
14021 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
14022 			    &err);
14023 			if (ilm == NULL) {
14024 				ASSERT(err != 0);
14025 				ip0dbg(("ipif_multicast_up: "
14026 				    "all_hosts_mcast failed %d\n", err));
14027 				return;
14028 			}
14029 			ipif->ipif_allhosts_ilm = ilm;
14030 		}
14031 
14032 		/*
14033 		 * Enable multicast for the solicited node multicast address.
14034 		 * If IPMP we need to put the membership on the upper ill.
14035 		 */
14036 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
14037 			ill_t *mcast_ill = NULL;
14038 			boolean_t need_refrele;
14039 
14040 			if (IS_UNDER_IPMP(ill) &&
14041 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
14042 				need_refrele = B_TRUE;
14043 			} else {
14044 				mcast_ill = ill;
14045 				need_refrele = B_FALSE;
14046 			}
14047 
14048 			ilm = ip_addmulti(&v6solmc, mcast_ill,
14049 			    ipif->ipif_zoneid, &err);
14050 			if (need_refrele)
14051 				ill_refrele(mcast_ill);
14052 
14053 			if (ilm == NULL) {
14054 				ASSERT(err != 0);
14055 				ip0dbg(("ipif_multicast_up: solicited MC"
14056 				    " failed %d\n", err));
14057 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14058 					ipif->ipif_allhosts_ilm = NULL;
14059 					(void) ip_delmulti(ilm);
14060 				}
14061 				return;
14062 			}
14063 			ipif->ipif_solmulti_ilm = ilm;
14064 		}
14065 	} else {
14066 		in6_addr_t v6group;
14067 
14068 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14069 			return;
14070 
14071 		/* Join the all hosts multicast address */
14072 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14073 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14074 
14075 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14076 		if (ilm == NULL) {
14077 			ASSERT(err != 0);
14078 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
14079 			return;
14080 		}
14081 		ipif->ipif_allhosts_ilm = ilm;
14082 	}
14083 }
14084 
14085 /*
14086  * Blow away any multicast groups that we joined in ipif_multicast_up().
14087  * (ilms from explicit memberships are handled in conn_update_ill.)
14088  */
14089 void
14090 ipif_multicast_down(ipif_t *ipif)
14091 {
14092 	ASSERT(IAM_WRITER_IPIF(ipif));
14093 
14094 	ip1dbg(("ipif_multicast_down\n"));
14095 
14096 	if (ipif->ipif_allhosts_ilm != NULL) {
14097 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
14098 		ipif->ipif_allhosts_ilm = NULL;
14099 	}
14100 	if (ipif->ipif_solmulti_ilm != NULL) {
14101 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
14102 		ipif->ipif_solmulti_ilm = NULL;
14103 	}
14104 }
14105 
14106 /*
14107  * Used when an interface comes up to recreate any extra routes on this
14108  * interface.
14109  */
14110 int
14111 ill_recover_saved_ire(ill_t *ill)
14112 {
14113 	mblk_t		*mp;
14114 	ip_stack_t	*ipst = ill->ill_ipst;
14115 
14116 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14117 
14118 	mutex_enter(&ill->ill_saved_ire_lock);
14119 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14120 		ire_t		*ire, *nire;
14121 		ifrt_t		*ifrt;
14122 
14123 		ifrt = (ifrt_t *)mp->b_rptr;
14124 		/*
14125 		 * Create a copy of the IRE with the saved address and netmask.
14126 		 */
14127 		if (ill->ill_isv6) {
14128 			ire = ire_create_v6(
14129 			    &ifrt->ifrt_v6addr,
14130 			    &ifrt->ifrt_v6mask,
14131 			    &ifrt->ifrt_v6gateway_addr,
14132 			    ifrt->ifrt_type,
14133 			    ill,
14134 			    ifrt->ifrt_zoneid,
14135 			    ifrt->ifrt_flags,
14136 			    NULL,
14137 			    ipst);
14138 		} else {
14139 			ire = ire_create(
14140 			    (uint8_t *)&ifrt->ifrt_addr,
14141 			    (uint8_t *)&ifrt->ifrt_mask,
14142 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
14143 			    ifrt->ifrt_type,
14144 			    ill,
14145 			    ifrt->ifrt_zoneid,
14146 			    ifrt->ifrt_flags,
14147 			    NULL,
14148 			    ipst);
14149 		}
14150 		if (ire == NULL) {
14151 			mutex_exit(&ill->ill_saved_ire_lock);
14152 			return (ENOMEM);
14153 		}
14154 
14155 		if (ifrt->ifrt_flags & RTF_SETSRC) {
14156 			if (ill->ill_isv6) {
14157 				ire->ire_setsrc_addr_v6 =
14158 				    ifrt->ifrt_v6setsrc_addr;
14159 			} else {
14160 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14161 			}
14162 		}
14163 
14164 		/*
14165 		 * Some software (for example, GateD and Sun Cluster) attempts
14166 		 * to create (what amount to) IRE_PREFIX routes with the
14167 		 * loopback address as the gateway.  This is primarily done to
14168 		 * set up prefixes with the RTF_REJECT flag set (for example,
14169 		 * when generating aggregate routes.)
14170 		 *
14171 		 * If the IRE type (as defined by ill->ill_net_type) is
14172 		 * IRE_LOOPBACK, then we map the request into a
14173 		 * IRE_IF_NORESOLVER.
14174 		 */
14175 		if (ill->ill_net_type == IRE_LOOPBACK)
14176 			ire->ire_type = IRE_IF_NORESOLVER;
14177 
14178 		/*
14179 		 * ire held by ire_add, will be refreled' towards the
14180 		 * the end of ipif_up_done
14181 		 */
14182 		nire = ire_add(ire);
14183 		/*
14184 		 * Check if it was a duplicate entry. This handles
14185 		 * the case of two racing route adds for the same route
14186 		 */
14187 		if (nire == NULL) {
14188 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14189 		} else if (nire != ire) {
14190 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14191 			    (void *)nire));
14192 			ire_delete(nire);
14193 		} else {
14194 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14195 			    (void *)nire));
14196 		}
14197 		if (nire != NULL)
14198 			ire_refrele(nire);
14199 	}
14200 	mutex_exit(&ill->ill_saved_ire_lock);
14201 	return (0);
14202 }
14203 
14204 /*
14205  * Used to set the netmask and broadcast address to default values when the
14206  * interface is brought up.  (Always called as writer.)
14207  */
14208 static void
14209 ipif_set_default(ipif_t *ipif)
14210 {
14211 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14212 
14213 	if (!ipif->ipif_isv6) {
14214 		/*
14215 		 * Interface holds an IPv4 address. Default
14216 		 * mask is the natural netmask.
14217 		 */
14218 		if (!ipif->ipif_net_mask) {
14219 			ipaddr_t	v4mask;
14220 
14221 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14222 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14223 		}
14224 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14225 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14226 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14227 		} else {
14228 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14229 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14230 		}
14231 		/*
14232 		 * NOTE: SunOS 4.X does this even if the broadcast address
14233 		 * has been already set thus we do the same here.
14234 		 */
14235 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14236 			ipaddr_t	v4addr;
14237 
14238 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14239 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14240 		}
14241 	} else {
14242 		/*
14243 		 * Interface holds an IPv6-only address.  Default
14244 		 * mask is all-ones.
14245 		 */
14246 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14247 			ipif->ipif_v6net_mask = ipv6_all_ones;
14248 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14249 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14250 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14251 		} else {
14252 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14253 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14254 		}
14255 	}
14256 }
14257 
14258 /*
14259  * Return 0 if this address can be used as local address without causing
14260  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14261  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14262  * Note that the same IPv6 link-local address is allowed as long as the ills
14263  * are not on the same link.
14264  */
14265 int
14266 ip_addr_availability_check(ipif_t *new_ipif)
14267 {
14268 	in6_addr_t our_v6addr;
14269 	ill_t *ill;
14270 	ipif_t *ipif;
14271 	ill_walk_context_t ctx;
14272 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14273 
14274 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14275 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14276 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14277 
14278 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14279 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14280 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14281 		return (0);
14282 
14283 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14284 
14285 	if (new_ipif->ipif_isv6)
14286 		ill = ILL_START_WALK_V6(&ctx, ipst);
14287 	else
14288 		ill = ILL_START_WALK_V4(&ctx, ipst);
14289 
14290 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14291 		for (ipif = ill->ill_ipif; ipif != NULL;
14292 		    ipif = ipif->ipif_next) {
14293 			if ((ipif == new_ipif) ||
14294 			    !(ipif->ipif_flags & IPIF_UP) ||
14295 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14296 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14297 			    &our_v6addr))
14298 				continue;
14299 
14300 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14301 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14302 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14303 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14304 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14305 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14306 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14307 				continue;
14308 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14309 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14310 				continue;
14311 			else if (new_ipif->ipif_ill == ill)
14312 				return (EADDRINUSE);
14313 			else
14314 				return (EADDRNOTAVAIL);
14315 		}
14316 	}
14317 
14318 	return (0);
14319 }
14320 
14321 /*
14322  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14323  * IREs for the ipif.
14324  * When the routine returns EINPROGRESS then mp has been consumed and
14325  * the ioctl will be acked from ip_rput_dlpi.
14326  */
14327 int
14328 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14329 {
14330 	ill_t		*ill = ipif->ipif_ill;
14331 	boolean_t 	isv6 = ipif->ipif_isv6;
14332 	int		err = 0;
14333 	boolean_t	success;
14334 	uint_t		ipif_orig_id;
14335 	ip_stack_t	*ipst = ill->ill_ipst;
14336 
14337 	ASSERT(IAM_WRITER_IPIF(ipif));
14338 
14339 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14340 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14341 	    ill_t *, ill, ipif_t *, ipif);
14342 
14343 	/* Shouldn't get here if it is already up. */
14344 	if (ipif->ipif_flags & IPIF_UP)
14345 		return (EALREADY);
14346 
14347 	/*
14348 	 * If this is a request to bring up a data address on an interface
14349 	 * under IPMP, then move the address to its IPMP meta-interface and
14350 	 * try to bring it up.  One complication is that the zeroth ipif for
14351 	 * an ill is special, in that every ill always has one, and that code
14352 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14353 	 */
14354 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14355 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14356 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14357 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14358 
14359 		/*
14360 		 * The ipif being brought up should be quiesced.  If it's not,
14361 		 * something has gone amiss and we need to bail out.  (If it's
14362 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14363 		 */
14364 		mutex_enter(&ill->ill_lock);
14365 		if (!ipif_is_quiescent(ipif)) {
14366 			mutex_exit(&ill->ill_lock);
14367 			return (EINVAL);
14368 		}
14369 		mutex_exit(&ill->ill_lock);
14370 
14371 		/*
14372 		 * If we're going to need to allocate ipifs, do it prior
14373 		 * to starting the move (and grabbing locks).
14374 		 */
14375 		if (ipif->ipif_id == 0) {
14376 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14377 			    B_FALSE, &err)) == NULL) {
14378 				return (err);
14379 			}
14380 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14381 			    B_FALSE, &err)) == NULL) {
14382 				mi_free(moveipif);
14383 				return (err);
14384 			}
14385 		}
14386 
14387 		/*
14388 		 * Grab or transfer the ipif to move.  During the move, keep
14389 		 * ill_g_lock held to prevent any ill walker threads from
14390 		 * seeing things in an inconsistent state.
14391 		 */
14392 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14393 		if (ipif->ipif_id != 0) {
14394 			ipif_remove(ipif);
14395 		} else {
14396 			ipif_transfer(ipif, moveipif, stubipif);
14397 			ipif = moveipif;
14398 		}
14399 
14400 		/*
14401 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14402 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14403 		 * replace that one.  Otherwise, pick the next available slot.
14404 		 */
14405 		ipif->ipif_ill = ipmp_ill;
14406 		ipif_orig_id = ipif->ipif_id;
14407 
14408 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14409 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14410 			ipif = ipmp_ill->ill_ipif;
14411 		} else {
14412 			ipif->ipif_id = -1;
14413 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14414 				/*
14415 				 * No more available ipif_id's -- put it back
14416 				 * on the original ill and fail the operation.
14417 				 * Since we're writer on the ill, we can be
14418 				 * sure our old slot is still available.
14419 				 */
14420 				ipif->ipif_id = ipif_orig_id;
14421 				ipif->ipif_ill = ill;
14422 				if (ipif_orig_id == 0) {
14423 					ipif_transfer(ipif, ill->ill_ipif,
14424 					    NULL);
14425 				} else {
14426 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14427 				}
14428 				rw_exit(&ipst->ips_ill_g_lock);
14429 				return (err);
14430 			}
14431 		}
14432 		rw_exit(&ipst->ips_ill_g_lock);
14433 
14434 		/*
14435 		 * Tell SCTP that the ipif has moved.  Note that even if we
14436 		 * had to allocate a new ipif, the original sequence id was
14437 		 * preserved and therefore SCTP won't know.
14438 		 */
14439 		sctp_move_ipif(ipif, ill, ipmp_ill);
14440 
14441 		/*
14442 		 * If the ipif being brought up was on slot zero, then we
14443 		 * first need to bring up the placeholder we stuck there.  In
14444 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14445 		 * call to ipif_up() itself, if we successfully bring up the
14446 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14447 		 */
14448 		if (ipif_orig_id == 0) {
14449 			ASSERT(ill->ill_move_ipif == NULL);
14450 			ill->ill_move_ipif = ipif;
14451 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14452 				ASSERT(ill->ill_move_ipif == NULL);
14453 			if (err != EINPROGRESS)
14454 				ill->ill_move_ipif = NULL;
14455 			return (err);
14456 		}
14457 
14458 		/*
14459 		 * Bring it up on the IPMP ill.
14460 		 */
14461 		return (ipif_up(ipif, q, mp));
14462 	}
14463 
14464 	/* Skip arp/ndp for any loopback interface. */
14465 	if (ill->ill_wq != NULL) {
14466 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14467 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14468 
14469 		if (!ill->ill_dl_up) {
14470 			/*
14471 			 * ill_dl_up is not yet set. i.e. we are yet to
14472 			 * DL_BIND with the driver and this is the first
14473 			 * logical interface on the ill to become "up".
14474 			 * Tell the driver to get going (via DL_BIND_REQ).
14475 			 * Note that changing "significant" IFF_ flags
14476 			 * address/netmask etc cause a down/up dance, but
14477 			 * does not cause an unbind (DL_UNBIND) with the driver
14478 			 */
14479 			return (ill_dl_up(ill, ipif, mp, q));
14480 		}
14481 
14482 		/*
14483 		 * ipif_resolver_up may end up needeing to bind/attach
14484 		 * the ARP stream, which in turn necessitates a
14485 		 * DLPI message exchange with the driver. ioctls are
14486 		 * serialized and so we cannot send more than one
14487 		 * interface up message at a time. If ipif_resolver_up
14488 		 * does need to wait for the DLPI handshake for the ARP stream,
14489 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14490 		 */
14491 
14492 		ASSERT(connp != NULL || !CONN_Q(q));
14493 		if (connp != NULL)
14494 			mutex_enter(&connp->conn_lock);
14495 		mutex_enter(&ill->ill_lock);
14496 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14497 		mutex_exit(&ill->ill_lock);
14498 		if (connp != NULL)
14499 			mutex_exit(&connp->conn_lock);
14500 		if (!success)
14501 			return (EINTR);
14502 
14503 		/*
14504 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14505 		 * complete when ipif_ndp_up returns.
14506 		 */
14507 		err = ipif_resolver_up(ipif, Res_act_initial);
14508 		if (err == EINPROGRESS) {
14509 			/* We will complete it in arp_bringup_done() */
14510 			return (err);
14511 		}
14512 
14513 		if (isv6 && err == 0)
14514 			err = ipif_ndp_up(ipif, B_TRUE);
14515 
14516 		ASSERT(err != EINPROGRESS);
14517 		mp = ipsq_pending_mp_get(ipsq, &connp);
14518 		ASSERT(mp != NULL);
14519 		if (err != 0)
14520 			return (err);
14521 	} else {
14522 		/*
14523 		 * Interfaces without underlying hardware don't do duplicate
14524 		 * address detection.
14525 		 */
14526 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14527 		ipif->ipif_addr_ready = 1;
14528 		err = ill_add_ires(ill);
14529 		/* allocation failure? */
14530 		if (err != 0)
14531 			return (err);
14532 	}
14533 
14534 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14535 	if (err == 0 && ill->ill_move_ipif != NULL) {
14536 		ipif = ill->ill_move_ipif;
14537 		ill->ill_move_ipif = NULL;
14538 		return (ipif_up(ipif, q, mp));
14539 	}
14540 	return (err);
14541 }
14542 
14543 /*
14544  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14545  * The identical set of IREs need to be removed in ill_delete_ires().
14546  */
14547 int
14548 ill_add_ires(ill_t *ill)
14549 {
14550 	ire_t	*ire;
14551 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14552 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14553 
14554 	if (ill->ill_ire_multicast != NULL)
14555 		return (0);
14556 
14557 	/*
14558 	 * provide some dummy ire_addr for creating the ire.
14559 	 */
14560 	if (ill->ill_isv6) {
14561 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14562 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14563 	} else {
14564 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14565 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14566 	}
14567 	if (ire == NULL)
14568 		return (ENOMEM);
14569 
14570 	ill->ill_ire_multicast = ire;
14571 	return (0);
14572 }
14573 
14574 void
14575 ill_delete_ires(ill_t *ill)
14576 {
14577 	if (ill->ill_ire_multicast != NULL) {
14578 		/*
14579 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14580 		 * which was taken without any th_tracing enabled.
14581 		 * We also mark it as condemned (note that it was never added)
14582 		 * so that caching conn's can move off of it.
14583 		 */
14584 		ire_make_condemned(ill->ill_ire_multicast);
14585 		ire_refrele_notr(ill->ill_ire_multicast);
14586 		ill->ill_ire_multicast = NULL;
14587 	}
14588 }
14589 
14590 /*
14591  * Perform a bind for the physical device.
14592  * When the routine returns EINPROGRESS then mp has been consumed and
14593  * the ioctl will be acked from ip_rput_dlpi.
14594  * Allocate an unbind message and save it until ipif_down.
14595  */
14596 static int
14597 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14598 {
14599 	mblk_t	*bind_mp = NULL;
14600 	mblk_t	*unbind_mp = NULL;
14601 	conn_t	*connp;
14602 	boolean_t success;
14603 	int	err;
14604 
14605 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14606 
14607 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14608 	ASSERT(IAM_WRITER_ILL(ill));
14609 	ASSERT(mp != NULL);
14610 
14611 	/*
14612 	 * Make sure we have an IRE_MULTICAST in case we immediately
14613 	 * start receiving packets.
14614 	 */
14615 	err = ill_add_ires(ill);
14616 	if (err != 0)
14617 		goto bad;
14618 
14619 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14620 	    DL_BIND_REQ);
14621 	if (bind_mp == NULL)
14622 		goto bad;
14623 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14624 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14625 
14626 	/*
14627 	 * ill_unbind_mp would be non-null if the following sequence had
14628 	 * happened:
14629 	 * - send DL_BIND_REQ to driver, wait for response
14630 	 * - multiple ioctls that need to bring the ipif up are encountered,
14631 	 *   but they cannot enter the ipsq due to the outstanding DL_BIND_REQ.
14632 	 *   These ioctls will then be enqueued on the ipsq
14633 	 * - a DL_ERROR_ACK is returned for the DL_BIND_REQ
14634 	 * At this point, the pending ioctls in the ipsq will be drained, and
14635 	 * since ill->ill_dl_up was not set, ill_dl_up would be invoked with
14636 	 * a non-null ill->ill_unbind_mp
14637 	 */
14638 	if (ill->ill_unbind_mp == NULL) {
14639 		unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t),
14640 		    DL_UNBIND_REQ);
14641 		if (unbind_mp == NULL)
14642 			goto bad;
14643 	}
14644 	/*
14645 	 * Record state needed to complete this operation when the
14646 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14647 	 */
14648 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14649 	ASSERT(connp != NULL || !CONN_Q(q));
14650 	GRAB_CONN_LOCK(q);
14651 	mutex_enter(&ipif->ipif_ill->ill_lock);
14652 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14653 	mutex_exit(&ipif->ipif_ill->ill_lock);
14654 	RELEASE_CONN_LOCK(q);
14655 	if (!success)
14656 		goto bad;
14657 
14658 	/*
14659 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14660 	 * the interface goes down.
14661 	 */
14662 	if (ill->ill_unbind_mp == NULL)
14663 		ill->ill_unbind_mp = unbind_mp;
14664 
14665 	ill_dlpi_send(ill, bind_mp);
14666 	/* Send down link-layer capabilities probe if not already done. */
14667 	ill_capability_probe(ill);
14668 
14669 	/*
14670 	 * Sysid used to rely on the fact that netboots set domainname
14671 	 * and the like. Now that miniroot boots aren't strictly netboots
14672 	 * and miniroot network configuration is driven from userland
14673 	 * these things still need to be set. This situation can be detected
14674 	 * by comparing the interface being configured here to the one
14675 	 * dhcifname was set to reference by the boot loader. Once sysid is
14676 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14677 	 */
14678 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14679 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14680 	    (strlen(srpc_domain) == 0)) {
14681 		if (dhcpinit() != 0)
14682 			cmn_err(CE_WARN, "no cached dhcp response");
14683 	}
14684 
14685 	/*
14686 	 * This operation will complete in ip_rput_dlpi with either
14687 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14688 	 */
14689 	return (EINPROGRESS);
14690 bad:
14691 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14692 
14693 	freemsg(bind_mp);
14694 	freemsg(unbind_mp);
14695 	return (ENOMEM);
14696 }
14697 
14698 /* Add room for tcp+ip headers */
14699 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14700 
14701 /*
14702  * DLPI and ARP is up.
14703  * Create all the IREs associated with an interface. Bring up multicast.
14704  * Set the interface flag and finish other initialization
14705  * that potentially had to be deferred to after DL_BIND_ACK.
14706  */
14707 int
14708 ipif_up_done(ipif_t *ipif)
14709 {
14710 	ill_t		*ill = ipif->ipif_ill;
14711 	int		err = 0;
14712 	boolean_t	loopback = B_FALSE;
14713 	boolean_t	update_src_selection = B_TRUE;
14714 	ipif_t		*tmp_ipif;
14715 
14716 	ip1dbg(("ipif_up_done(%s:%u)\n",
14717 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14718 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14719 	    ill_t *, ill, ipif_t *, ipif);
14720 
14721 	/* Check if this is a loopback interface */
14722 	if (ipif->ipif_ill->ill_wq == NULL)
14723 		loopback = B_TRUE;
14724 
14725 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14726 
14727 	/*
14728 	 * If all other interfaces for this ill are down or DEPRECATED,
14729 	 * or otherwise unsuitable for source address selection,
14730 	 * reset the src generation numbers to make sure source
14731 	 * address selection gets to take this new ipif into account.
14732 	 * No need to hold ill_lock while traversing the ipif list since
14733 	 * we are writer
14734 	 */
14735 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14736 	    tmp_ipif = tmp_ipif->ipif_next) {
14737 		if (((tmp_ipif->ipif_flags &
14738 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14739 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14740 		    (tmp_ipif == ipif))
14741 			continue;
14742 		/* first useable pre-existing interface */
14743 		update_src_selection = B_FALSE;
14744 		break;
14745 	}
14746 	if (update_src_selection)
14747 		ip_update_source_selection(ill->ill_ipst);
14748 
14749 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14750 		nce_t *loop_nce = NULL;
14751 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14752 
14753 		/*
14754 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14755 		 * ipif_lookup_on_name(), but in the case of zones we can have
14756 		 * several loopback addresses on lo0. So all the interfaces with
14757 		 * loopback addresses need to be marked IRE_LOOPBACK.
14758 		 */
14759 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14760 		    htonl(INADDR_LOOPBACK))
14761 			ipif->ipif_ire_type = IRE_LOOPBACK;
14762 		else
14763 			ipif->ipif_ire_type = IRE_LOCAL;
14764 		if (ill->ill_net_type != IRE_LOOPBACK)
14765 			flags |= NCE_F_PUBLISH;
14766 
14767 		/* add unicast nce for the local addr */
14768 		err = nce_lookup_then_add_v4(ill, NULL,
14769 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14770 		    ND_REACHABLE, &loop_nce);
14771 		/* A shared-IP zone sees EEXIST for lo0:N */
14772 		if (err == 0 || err == EEXIST) {
14773 			ipif->ipif_added_nce = 1;
14774 			loop_nce->nce_ipif_cnt++;
14775 			nce_refrele(loop_nce);
14776 			err = 0;
14777 		} else {
14778 			ASSERT(loop_nce == NULL);
14779 			return (err);
14780 		}
14781 	}
14782 
14783 	/* Create all the IREs associated with this interface */
14784 	err = ipif_add_ires_v4(ipif, loopback);
14785 	if (err != 0) {
14786 		/*
14787 		 * see comments about return value from
14788 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14789 		 */
14790 		if (err != EADDRINUSE) {
14791 			(void) ipif_arp_down(ipif);
14792 		} else {
14793 			/*
14794 			 * Make IPMP aware of the deleted ipif so that
14795 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14796 			 * can be completed. Note that we do not want to
14797 			 * destroy the nce that was created on the ipmp_ill
14798 			 * for the active copy of the duplicate address in
14799 			 * use.
14800 			 */
14801 			if (IS_IPMP(ill))
14802 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14803 			err = EADDRNOTAVAIL;
14804 		}
14805 		return (err);
14806 	}
14807 
14808 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14809 		/* Recover any additional IREs entries for this ill */
14810 		(void) ill_recover_saved_ire(ill);
14811 	}
14812 
14813 	if (ill->ill_need_recover_multicast) {
14814 		/*
14815 		 * Need to recover all multicast memberships in the driver.
14816 		 * This had to be deferred until we had attached.  The same
14817 		 * code exists in ipif_up_done_v6() to recover IPv6
14818 		 * memberships.
14819 		 *
14820 		 * Note that it would be preferable to unconditionally do the
14821 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14822 		 * that since ill_join_allmulti() depends on ill_dl_up being
14823 		 * set, and it is not set until we receive a DL_BIND_ACK after
14824 		 * having called ill_dl_up().
14825 		 */
14826 		ill_recover_multicast(ill);
14827 	}
14828 
14829 	if (ill->ill_ipif_up_count == 1) {
14830 		/*
14831 		 * Since the interface is now up, it may now be active.
14832 		 */
14833 		if (IS_UNDER_IPMP(ill))
14834 			ipmp_ill_refresh_active(ill);
14835 
14836 		/*
14837 		 * If this is an IPMP interface, we may now be able to
14838 		 * establish ARP entries.
14839 		 */
14840 		if (IS_IPMP(ill))
14841 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14842 	}
14843 
14844 	/* Join the allhosts multicast address */
14845 	ipif_multicast_up(ipif);
14846 
14847 	if (!loopback && !update_src_selection &&
14848 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14849 		ip_update_source_selection(ill->ill_ipst);
14850 
14851 	if (!loopback && ipif->ipif_addr_ready) {
14852 		/* Broadcast an address mask reply. */
14853 		ipif_mask_reply(ipif);
14854 	}
14855 	/* Perhaps ilgs should use this ill */
14856 	update_conn_ill(NULL, ill->ill_ipst);
14857 
14858 	/*
14859 	 * This had to be deferred until we had bound.  Tell routing sockets and
14860 	 * others that this interface is up if it looks like the address has
14861 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14862 	 * duplicate address detection to do its thing.
14863 	 */
14864 	if (ipif->ipif_addr_ready)
14865 		ipif_up_notify(ipif);
14866 	return (0);
14867 }
14868 
14869 /*
14870  * Add the IREs associated with the ipif.
14871  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14872  */
14873 static int
14874 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14875 {
14876 	ill_t		*ill = ipif->ipif_ill;
14877 	ip_stack_t	*ipst = ill->ill_ipst;
14878 	ire_t		*ire_array[20];
14879 	ire_t		**irep = ire_array;
14880 	ire_t		**irep1;
14881 	ipaddr_t	net_mask = 0;
14882 	ipaddr_t	subnet_mask, route_mask;
14883 	int		err;
14884 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14885 	ire_t		*ire_if = NULL;
14886 	uchar_t		*gw;
14887 
14888 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14889 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14890 		/*
14891 		 * If we're on a labeled system then make sure that zone-
14892 		 * private addresses have proper remote host database entries.
14893 		 */
14894 		if (is_system_labeled() &&
14895 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14896 		    !tsol_check_interface_address(ipif))
14897 			return (EINVAL);
14898 
14899 		/* Register the source address for __sin6_src_id */
14900 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14901 		    ipif->ipif_zoneid, ipst);
14902 		if (err != 0) {
14903 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14904 			return (err);
14905 		}
14906 
14907 		if (loopback)
14908 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14909 		else
14910 			gw = NULL;
14911 
14912 		/* If the interface address is set, create the local IRE. */
14913 		ire_local = ire_create(
14914 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14915 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14916 		    gw,					/* gateway */
14917 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14918 		    ipif->ipif_ill,
14919 		    ipif->ipif_zoneid,
14920 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14921 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14922 		    NULL,
14923 		    ipst);
14924 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14925 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14926 		    ipif->ipif_ire_type,
14927 		    ntohl(ipif->ipif_lcl_addr)));
14928 		if (ire_local == NULL) {
14929 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14930 			err = ENOMEM;
14931 			goto bad;
14932 		}
14933 	} else {
14934 		ip1dbg((
14935 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14936 		    ipif->ipif_ire_type,
14937 		    ntohl(ipif->ipif_lcl_addr),
14938 		    (uint_t)ipif->ipif_flags));
14939 	}
14940 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14941 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14942 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14943 	} else {
14944 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14945 	}
14946 
14947 	subnet_mask = ipif->ipif_net_mask;
14948 
14949 	/*
14950 	 * If mask was not specified, use natural netmask of
14951 	 * interface address. Also, store this mask back into the
14952 	 * ipif struct.
14953 	 */
14954 	if (subnet_mask == 0) {
14955 		subnet_mask = net_mask;
14956 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14957 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14958 		    ipif->ipif_v6subnet);
14959 	}
14960 
14961 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14962 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14963 	    ipif->ipif_subnet != INADDR_ANY) {
14964 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14965 
14966 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14967 			route_mask = IP_HOST_MASK;
14968 		} else {
14969 			route_mask = subnet_mask;
14970 		}
14971 
14972 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14973 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14974 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14975 		    ntohl(ipif->ipif_subnet)));
14976 		ire_if = ire_create(
14977 		    (uchar_t *)&ipif->ipif_subnet,
14978 		    (uchar_t *)&route_mask,
14979 		    (uchar_t *)&ipif->ipif_lcl_addr,
14980 		    ill->ill_net_type,
14981 		    ill,
14982 		    ipif->ipif_zoneid,
14983 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14984 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14985 		    NULL,
14986 		    ipst);
14987 		if (ire_if == NULL) {
14988 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14989 			err = ENOMEM;
14990 			goto bad;
14991 		}
14992 	}
14993 
14994 	/*
14995 	 * Create any necessary broadcast IREs.
14996 	 */
14997 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14998 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14999 		irep = ipif_create_bcast_ires(ipif, irep);
15000 
15001 	/* If an earlier ire_create failed, get out now */
15002 	for (irep1 = irep; irep1 > ire_array; ) {
15003 		irep1--;
15004 		if (*irep1 == NULL) {
15005 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
15006 			err = ENOMEM;
15007 			goto bad;
15008 		}
15009 	}
15010 
15011 	/*
15012 	 * Need to atomically check for IP address availability under
15013 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
15014 	 * ills or new ipifs can be added while we are checking availability.
15015 	 */
15016 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15017 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
15018 	/* Mark it up, and increment counters. */
15019 	ipif->ipif_flags |= IPIF_UP;
15020 	ill->ill_ipif_up_count++;
15021 	err = ip_addr_availability_check(ipif);
15022 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
15023 	rw_exit(&ipst->ips_ill_g_lock);
15024 
15025 	if (err != 0) {
15026 		/*
15027 		 * Our address may already be up on the same ill. In this case,
15028 		 * the ARP entry for our ipif replaced the one for the other
15029 		 * ipif. So we don't want to delete it (otherwise the other ipif
15030 		 * would be unable to send packets).
15031 		 * ip_addr_availability_check() identifies this case for us and
15032 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
15033 		 * which is the expected error code.
15034 		 */
15035 		ill->ill_ipif_up_count--;
15036 		ipif->ipif_flags &= ~IPIF_UP;
15037 		goto bad;
15038 	}
15039 
15040 	/*
15041 	 * Add in all newly created IREs.  ire_create_bcast() has
15042 	 * already checked for duplicates of the IRE_BROADCAST type.
15043 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
15044 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
15045 	 * a /32 route.
15046 	 */
15047 	if (ire_if != NULL) {
15048 		ire_if = ire_add(ire_if);
15049 		if (ire_if == NULL) {
15050 			err = ENOMEM;
15051 			goto bad2;
15052 		}
15053 #ifdef DEBUG
15054 		ire_refhold_notr(ire_if);
15055 		ire_refrele(ire_if);
15056 #endif
15057 	}
15058 	if (ire_local != NULL) {
15059 		ire_local = ire_add(ire_local);
15060 		if (ire_local == NULL) {
15061 			err = ENOMEM;
15062 			goto bad2;
15063 		}
15064 #ifdef DEBUG
15065 		ire_refhold_notr(ire_local);
15066 		ire_refrele(ire_local);
15067 #endif
15068 	}
15069 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15070 	if (ire_local != NULL)
15071 		ipif->ipif_ire_local = ire_local;
15072 	if (ire_if != NULL)
15073 		ipif->ipif_ire_if = ire_if;
15074 	rw_exit(&ipst->ips_ill_g_lock);
15075 	ire_local = NULL;
15076 	ire_if = NULL;
15077 
15078 	/*
15079 	 * We first add all of them, and if that succeeds we refrele the
15080 	 * bunch. That enables us to delete all of them should any of the
15081 	 * ire_adds fail.
15082 	 */
15083 	for (irep1 = irep; irep1 > ire_array; ) {
15084 		irep1--;
15085 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15086 		*irep1 = ire_add(*irep1);
15087 		if (*irep1 == NULL) {
15088 			err = ENOMEM;
15089 			goto bad2;
15090 		}
15091 	}
15092 
15093 	for (irep1 = irep; irep1 > ire_array; ) {
15094 		irep1--;
15095 		/* refheld by ire_add. */
15096 		if (*irep1 != NULL) {
15097 			ire_refrele(*irep1);
15098 			*irep1 = NULL;
15099 		}
15100 	}
15101 
15102 	if (!loopback) {
15103 		/*
15104 		 * If the broadcast address has been set, make sure it makes
15105 		 * sense based on the interface address.
15106 		 * Only match on ill since we are sharing broadcast addresses.
15107 		 */
15108 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15109 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
15110 			ire_t	*ire;
15111 
15112 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15113 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15114 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15115 
15116 			if (ire == NULL) {
15117 				/*
15118 				 * If there isn't a matching broadcast IRE,
15119 				 * revert to the default for this netmask.
15120 				 */
15121 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
15122 				mutex_enter(&ipif->ipif_ill->ill_lock);
15123 				ipif_set_default(ipif);
15124 				mutex_exit(&ipif->ipif_ill->ill_lock);
15125 			} else {
15126 				ire_refrele(ire);
15127 			}
15128 		}
15129 
15130 	}
15131 	return (0);
15132 
15133 bad2:
15134 	ill->ill_ipif_up_count--;
15135 	ipif->ipif_flags &= ~IPIF_UP;
15136 
15137 bad:
15138 	ip1dbg(("ipif_add_ires: FAILED \n"));
15139 	if (ire_local != NULL)
15140 		ire_delete(ire_local);
15141 	if (ire_if != NULL)
15142 		ire_delete(ire_if);
15143 
15144 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15145 	ire_local = ipif->ipif_ire_local;
15146 	ipif->ipif_ire_local = NULL;
15147 	ire_if = ipif->ipif_ire_if;
15148 	ipif->ipif_ire_if = NULL;
15149 	rw_exit(&ipst->ips_ill_g_lock);
15150 	if (ire_local != NULL) {
15151 		ire_delete(ire_local);
15152 		ire_refrele_notr(ire_local);
15153 	}
15154 	if (ire_if != NULL) {
15155 		ire_delete(ire_if);
15156 		ire_refrele_notr(ire_if);
15157 	}
15158 
15159 	while (irep > ire_array) {
15160 		irep--;
15161 		if (*irep != NULL) {
15162 			ire_delete(*irep);
15163 		}
15164 	}
15165 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15166 
15167 	return (err);
15168 }
15169 
15170 /* Remove all the IREs created by ipif_add_ires_v4 */
15171 void
15172 ipif_delete_ires_v4(ipif_t *ipif)
15173 {
15174 	ill_t		*ill = ipif->ipif_ill;
15175 	ip_stack_t	*ipst = ill->ill_ipst;
15176 	ire_t		*ire;
15177 
15178 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15179 	ire = ipif->ipif_ire_local;
15180 	ipif->ipif_ire_local = NULL;
15181 	rw_exit(&ipst->ips_ill_g_lock);
15182 	if (ire != NULL) {
15183 		/*
15184 		 * Move count to ipif so we don't loose the count due to
15185 		 * a down/up dance.
15186 		 */
15187 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15188 
15189 		ire_delete(ire);
15190 		ire_refrele_notr(ire);
15191 	}
15192 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15193 	ire = ipif->ipif_ire_if;
15194 	ipif->ipif_ire_if = NULL;
15195 	rw_exit(&ipst->ips_ill_g_lock);
15196 	if (ire != NULL) {
15197 		ire_delete(ire);
15198 		ire_refrele_notr(ire);
15199 	}
15200 
15201 	/*
15202 	 * Delete the broadcast IREs.
15203 	 */
15204 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15205 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15206 		ipif_delete_bcast_ires(ipif);
15207 }
15208 
15209 /*
15210  * Checks for availbility of a usable source address (if there is one) when the
15211  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15212  * this selection is done regardless of the destination.
15213  */
15214 boolean_t
15215 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15216     ip_stack_t *ipst)
15217 {
15218 	ipif_t		*ipif = NULL;
15219 	ill_t		*uill;
15220 
15221 	ASSERT(ifindex != 0);
15222 
15223 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15224 	if (uill == NULL)
15225 		return (B_FALSE);
15226 
15227 	mutex_enter(&uill->ill_lock);
15228 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15229 		if (IPIF_IS_CONDEMNED(ipif))
15230 			continue;
15231 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15232 			continue;
15233 		if (!(ipif->ipif_flags & IPIF_UP))
15234 			continue;
15235 		if (ipif->ipif_zoneid != zoneid)
15236 			continue;
15237 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15238 		    ipif->ipif_lcl_addr == INADDR_ANY)
15239 			continue;
15240 		mutex_exit(&uill->ill_lock);
15241 		ill_refrele(uill);
15242 		return (B_TRUE);
15243 	}
15244 	mutex_exit(&uill->ill_lock);
15245 	ill_refrele(uill);
15246 	return (B_FALSE);
15247 }
15248 
15249 /*
15250  * Find an ipif with a good local address on the ill+zoneid.
15251  */
15252 ipif_t *
15253 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15254 {
15255 	ipif_t		*ipif;
15256 
15257 	mutex_enter(&ill->ill_lock);
15258 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15259 		if (IPIF_IS_CONDEMNED(ipif))
15260 			continue;
15261 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15262 			continue;
15263 		if (!(ipif->ipif_flags & IPIF_UP))
15264 			continue;
15265 		if (ipif->ipif_zoneid != zoneid &&
15266 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15267 			continue;
15268 		if (ill->ill_isv6 ?
15269 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15270 		    ipif->ipif_lcl_addr == INADDR_ANY)
15271 			continue;
15272 		ipif_refhold_locked(ipif);
15273 		mutex_exit(&ill->ill_lock);
15274 		return (ipif);
15275 	}
15276 	mutex_exit(&ill->ill_lock);
15277 	return (NULL);
15278 }
15279 
15280 /*
15281  * IP source address type, sorted from worst to best.  For a given type,
15282  * always prefer IP addresses on the same subnet.  All-zones addresses are
15283  * suboptimal because they pose problems with unlabeled destinations.
15284  */
15285 typedef enum {
15286 	IPIF_NONE,
15287 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
15288 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
15289 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15290 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15291 	IPIF_DIFFNET,			/* normal and different subnet */
15292 	IPIF_SAMENET,			/* normal and same subnet */
15293 	IPIF_LOCALADDR			/* local loopback */
15294 } ipif_type_t;
15295 
15296 /*
15297  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15298  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15299  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15300  * the first one, unless IPMP is used in which case we round-robin among them;
15301  * see below for more.
15302  *
15303  * Returns NULL if there is no suitable source address for the ill.
15304  * This only occurs when there is no valid source address for the ill.
15305  */
15306 ipif_t *
15307 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15308     boolean_t allow_usesrc, boolean_t *notreadyp)
15309 {
15310 	ill_t	*usill = NULL;
15311 	ill_t	*ipmp_ill = NULL;
15312 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15313 	ipif_type_t type, best_type;
15314 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15315 	ip_stack_t *ipst = ill->ill_ipst;
15316 	boolean_t samenet;
15317 
15318 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15319 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15320 		    B_FALSE, ipst);
15321 		if (usill != NULL)
15322 			ill = usill;	/* Select source from usesrc ILL */
15323 		else
15324 			return (NULL);
15325 	}
15326 
15327 	/*
15328 	 * Test addresses should never be used for source address selection,
15329 	 * so if we were passed one, switch to the IPMP meta-interface.
15330 	 */
15331 	if (IS_UNDER_IPMP(ill)) {
15332 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15333 			ill = ipmp_ill;	/* Select source from IPMP ill */
15334 		else
15335 			return (NULL);
15336 	}
15337 
15338 	/*
15339 	 * If we're dealing with an unlabeled destination on a labeled system,
15340 	 * make sure that we ignore source addresses that are incompatible with
15341 	 * the destination's default label.  That destination's default label
15342 	 * must dominate the minimum label on the source address.
15343 	 */
15344 	dst_rhtp = NULL;
15345 	if (is_system_labeled()) {
15346 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15347 		if (dst_rhtp == NULL)
15348 			return (NULL);
15349 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15350 			TPC_RELE(dst_rhtp);
15351 			dst_rhtp = NULL;
15352 		}
15353 	}
15354 
15355 	/*
15356 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15357 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15358 	 * After selecting the right ipif, under ill_lock make sure ipif is
15359 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15360 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15361 	 * but not under a lock.
15362 	 */
15363 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15364 retry:
15365 	/*
15366 	 * For source address selection, we treat the ipif list as circular
15367 	 * and continue until we get back to where we started.  This allows
15368 	 * IPMP to vary source address selection (which improves inbound load
15369 	 * spreading) by caching its last ending point and starting from
15370 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15371 	 * ills since that can't happen on the IPMP ill.
15372 	 */
15373 	start_ipif = ill->ill_ipif;
15374 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15375 		start_ipif = ill->ill_src_ipif;
15376 
15377 	ipif = start_ipif;
15378 	best_ipif = NULL;
15379 	best_type = IPIF_NONE;
15380 	do {
15381 		if ((next_ipif = ipif->ipif_next) == NULL)
15382 			next_ipif = ill->ill_ipif;
15383 
15384 		if (IPIF_IS_CONDEMNED(ipif))
15385 			continue;
15386 		/* Always skip NOLOCAL and ANYCAST interfaces */
15387 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15388 			continue;
15389 		/* Always skip NOACCEPT interfaces */
15390 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15391 			continue;
15392 		if (!(ipif->ipif_flags & IPIF_UP))
15393 			continue;
15394 
15395 		if (!ipif->ipif_addr_ready) {
15396 			if (notreadyp != NULL)
15397 				*notreadyp = B_TRUE;
15398 			continue;
15399 		}
15400 
15401 		if (zoneid != ALL_ZONES &&
15402 		    ipif->ipif_zoneid != zoneid &&
15403 		    ipif->ipif_zoneid != ALL_ZONES)
15404 			continue;
15405 
15406 		/*
15407 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15408 		 * are not valid as source addresses.
15409 		 */
15410 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15411 			continue;
15412 
15413 		/*
15414 		 * Check compatibility of local address for destination's
15415 		 * default label if we're on a labeled system.	Incompatible
15416 		 * addresses can't be used at all.
15417 		 */
15418 		if (dst_rhtp != NULL) {
15419 			boolean_t incompat;
15420 
15421 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15422 			    IPV4_VERSION, B_FALSE);
15423 			if (src_rhtp == NULL)
15424 				continue;
15425 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15426 			    src_rhtp->tpc_tp.tp_doi !=
15427 			    dst_rhtp->tpc_tp.tp_doi ||
15428 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15429 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15430 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15431 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15432 			TPC_RELE(src_rhtp);
15433 			if (incompat)
15434 				continue;
15435 		}
15436 
15437 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15438 
15439 		if (ipif->ipif_lcl_addr == dst) {
15440 			type = IPIF_LOCALADDR;
15441 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15442 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15443 			    IPIF_DIFFNET_DEPRECATED;
15444 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15445 			type = samenet ? IPIF_SAMENET_ALLZONES :
15446 			    IPIF_DIFFNET_ALLZONES;
15447 		} else {
15448 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15449 		}
15450 
15451 		if (type > best_type) {
15452 			best_type = type;
15453 			best_ipif = ipif;
15454 			if (best_type == IPIF_LOCALADDR)
15455 				break; /* can't get better */
15456 		}
15457 	} while ((ipif = next_ipif) != start_ipif);
15458 
15459 	if ((ipif = best_ipif) != NULL) {
15460 		mutex_enter(&ipif->ipif_ill->ill_lock);
15461 		if (IPIF_IS_CONDEMNED(ipif)) {
15462 			mutex_exit(&ipif->ipif_ill->ill_lock);
15463 			goto retry;
15464 		}
15465 		ipif_refhold_locked(ipif);
15466 
15467 		/*
15468 		 * For IPMP, update the source ipif rotor to the next ipif,
15469 		 * provided we can look it up.  (We must not use it if it's
15470 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15471 		 * ipif_free() checked ill_src_ipif.)
15472 		 */
15473 		if (IS_IPMP(ill) && ipif != NULL) {
15474 			next_ipif = ipif->ipif_next;
15475 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15476 				ill->ill_src_ipif = next_ipif;
15477 			else
15478 				ill->ill_src_ipif = NULL;
15479 		}
15480 		mutex_exit(&ipif->ipif_ill->ill_lock);
15481 	}
15482 
15483 	rw_exit(&ipst->ips_ill_g_lock);
15484 	if (usill != NULL)
15485 		ill_refrele(usill);
15486 	if (ipmp_ill != NULL)
15487 		ill_refrele(ipmp_ill);
15488 	if (dst_rhtp != NULL)
15489 		TPC_RELE(dst_rhtp);
15490 
15491 #ifdef DEBUG
15492 	if (ipif == NULL) {
15493 		char buf1[INET6_ADDRSTRLEN];
15494 
15495 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15496 		    ill->ill_name,
15497 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15498 	} else {
15499 		char buf1[INET6_ADDRSTRLEN];
15500 		char buf2[INET6_ADDRSTRLEN];
15501 
15502 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15503 		    ipif->ipif_ill->ill_name,
15504 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15505 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15506 		    buf2, sizeof (buf2))));
15507 	}
15508 #endif /* DEBUG */
15509 	return (ipif);
15510 }
15511 
15512 /*
15513  * Pick a source address based on the destination ill and an optional setsrc
15514  * address.
15515  * The result is stored in srcp. If generation is set, then put the source
15516  * generation number there before we look for the source address (to avoid
15517  * missing changes in the set of source addresses.
15518  * If flagsp is set, then us it to pass back ipif_flags.
15519  *
15520  * If the caller wants to cache the returned source address and detect when
15521  * that might be stale, the caller should pass in a generation argument,
15522  * which the caller can later compare against ips_src_generation
15523  *
15524  * The precedence order for selecting an IPv4 source address is:
15525  *  - RTF_SETSRC on the offlink ire always wins.
15526  *  - If usrsrc is set, swap the ill to be the usesrc one.
15527  *  - If IPMP is used on the ill, select a random address from the most
15528  *    preferred ones below:
15529  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15530  * 2. Not deprecated, not ALL_ZONES
15531  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15532  * 4. Not deprecated, ALL_ZONES
15533  * 5. If onlink destination, same subnet and deprecated
15534  * 6. Deprecated.
15535  *
15536  * We have lower preference for ALL_ZONES IP addresses,
15537  * as they pose problems with unlabeled destinations.
15538  *
15539  * Note that when multiple IP addresses match e.g., #1 we pick
15540  * the first one if IPMP is not in use. With IPMP we randomize.
15541  */
15542 int
15543 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15544     ipaddr_t multicast_ifaddr,
15545     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15546     uint32_t *generation, uint64_t *flagsp)
15547 {
15548 	ipif_t *ipif;
15549 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15550 
15551 	if (flagsp != NULL)
15552 		*flagsp = 0;
15553 
15554 	/*
15555 	 * Need to grab the generation number before we check to
15556 	 * avoid a race with a change to the set of local addresses.
15557 	 * No lock needed since the thread which updates the set of local
15558 	 * addresses use ipif/ill locks and exit those (hence a store memory
15559 	 * barrier) before doing the atomic increase of ips_src_generation.
15560 	 */
15561 	if (generation != NULL) {
15562 		*generation = ipst->ips_src_generation;
15563 	}
15564 
15565 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15566 		*srcp = multicast_ifaddr;
15567 		return (0);
15568 	}
15569 
15570 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15571 	if (setsrc != INADDR_ANY) {
15572 		*srcp = setsrc;
15573 		return (0);
15574 	}
15575 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15576 	if (ipif == NULL) {
15577 		if (notready)
15578 			return (ENETDOWN);
15579 		else
15580 			return (EADDRNOTAVAIL);
15581 	}
15582 	*srcp = ipif->ipif_lcl_addr;
15583 	if (flagsp != NULL)
15584 		*flagsp = ipif->ipif_flags;
15585 	ipif_refrele(ipif);
15586 	return (0);
15587 }
15588 
15589 /* ARGSUSED */
15590 int
15591 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15592 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15593 {
15594 	/*
15595 	 * ill_phyint_reinit merged the v4 and v6 into a single
15596 	 * ipsq.  We might not have been able to complete the
15597 	 * operation in ipif_set_values, if we could not become
15598 	 * exclusive.  If so restart it here.
15599 	 */
15600 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15601 }
15602 
15603 /*
15604  * Can operate on either a module or a driver queue.
15605  * Returns an error if not a module queue.
15606  */
15607 /* ARGSUSED */
15608 int
15609 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15610     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15611 {
15612 	queue_t		*q1 = q;
15613 	char 		*cp;
15614 	char		interf_name[LIFNAMSIZ];
15615 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15616 
15617 	if (q->q_next == NULL) {
15618 		ip1dbg((
15619 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15620 		return (EINVAL);
15621 	}
15622 
15623 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15624 		return (EALREADY);
15625 
15626 	do {
15627 		q1 = q1->q_next;
15628 	} while (q1->q_next);
15629 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15630 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15631 
15632 	/*
15633 	 * Here we are not going to delay the ioack until after
15634 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15635 	 * original ioctl message before sending the requests.
15636 	 */
15637 	return (ipif_set_values(q, mp, interf_name, &ppa));
15638 }
15639 
15640 /* ARGSUSED */
15641 int
15642 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15643     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15644 {
15645 	return (ENXIO);
15646 }
15647 
15648 /*
15649  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15650  * `irep'.  Returns a pointer to the next free `irep' entry
15651  * A mirror exists in ipif_delete_bcast_ires().
15652  *
15653  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15654  * done in ire_add.
15655  */
15656 static ire_t **
15657 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15658 {
15659 	ipaddr_t addr;
15660 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15661 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15662 	ill_t *ill = ipif->ipif_ill;
15663 	zoneid_t zoneid = ipif->ipif_zoneid;
15664 
15665 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15666 
15667 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15668 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15669 
15670 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15671 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15672 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15673 
15674 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15675 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15676 
15677 	/*
15678 	 * For backward compatibility, we create net broadcast IREs based on
15679 	 * the old "IP address class system", since some old machines only
15680 	 * respond to these class derived net broadcast.  However, we must not
15681 	 * create these net broadcast IREs if the subnetmask is shorter than
15682 	 * the IP address class based derived netmask.  Otherwise, we may
15683 	 * create a net broadcast address which is the same as an IP address
15684 	 * on the subnet -- and then TCP will refuse to talk to that address.
15685 	 */
15686 	if (netmask < subnetmask) {
15687 		addr = netmask & ipif->ipif_subnet;
15688 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15689 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15690 	}
15691 
15692 	/*
15693 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15694 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15695 	 * created.  Creating these broadcast IREs will only create confusion
15696 	 * as `addr' will be the same as the IP address.
15697 	 */
15698 	if (subnetmask != 0xFFFFFFFF) {
15699 		addr = ipif->ipif_subnet;
15700 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15701 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15702 	}
15703 
15704 	return (irep);
15705 }
15706 
15707 /*
15708  * Mirror of ipif_create_bcast_ires()
15709  */
15710 static void
15711 ipif_delete_bcast_ires(ipif_t *ipif)
15712 {
15713 	ipaddr_t	addr;
15714 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15715 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15716 	ill_t		*ill = ipif->ipif_ill;
15717 	zoneid_t	zoneid = ipif->ipif_zoneid;
15718 	ire_t		*ire;
15719 
15720 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15721 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15722 
15723 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15724 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15725 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15726 
15727 	ire = ire_lookup_bcast(ill, 0, zoneid);
15728 	ASSERT(ire != NULL);
15729 	ire_delete(ire); ire_refrele(ire);
15730 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15731 	ASSERT(ire != NULL);
15732 	ire_delete(ire); ire_refrele(ire);
15733 
15734 	/*
15735 	 * For backward compatibility, we create net broadcast IREs based on
15736 	 * the old "IP address class system", since some old machines only
15737 	 * respond to these class derived net broadcast.  However, we must not
15738 	 * create these net broadcast IREs if the subnetmask is shorter than
15739 	 * the IP address class based derived netmask.  Otherwise, we may
15740 	 * create a net broadcast address which is the same as an IP address
15741 	 * on the subnet -- and then TCP will refuse to talk to that address.
15742 	 */
15743 	if (netmask < subnetmask) {
15744 		addr = netmask & ipif->ipif_subnet;
15745 		ire = ire_lookup_bcast(ill, addr, zoneid);
15746 		ASSERT(ire != NULL);
15747 		ire_delete(ire); ire_refrele(ire);
15748 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15749 		ASSERT(ire != NULL);
15750 		ire_delete(ire); ire_refrele(ire);
15751 	}
15752 
15753 	/*
15754 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15755 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15756 	 * created.  Creating these broadcast IREs will only create confusion
15757 	 * as `addr' will be the same as the IP address.
15758 	 */
15759 	if (subnetmask != 0xFFFFFFFF) {
15760 		addr = ipif->ipif_subnet;
15761 		ire = ire_lookup_bcast(ill, addr, zoneid);
15762 		ASSERT(ire != NULL);
15763 		ire_delete(ire); ire_refrele(ire);
15764 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15765 		ASSERT(ire != NULL);
15766 		ire_delete(ire); ire_refrele(ire);
15767 	}
15768 }
15769 
15770 /*
15771  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15772  * from lifr_flags and the name from lifr_name.
15773  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15774  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15775  * Returns EINPROGRESS when mp has been consumed by queueing it on
15776  * ipx_pending_mp and the ioctl will complete in ip_rput.
15777  *
15778  * Can operate on either a module or a driver queue.
15779  * Returns an error if not a module queue.
15780  */
15781 /* ARGSUSED */
15782 int
15783 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15784     ip_ioctl_cmd_t *ipip, void *if_req)
15785 {
15786 	ill_t	*ill = q->q_ptr;
15787 	phyint_t *phyi;
15788 	ip_stack_t *ipst;
15789 	struct lifreq *lifr = if_req;
15790 	uint64_t new_flags;
15791 
15792 	ASSERT(ipif != NULL);
15793 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15794 
15795 	if (q->q_next == NULL) {
15796 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15797 		return (EINVAL);
15798 	}
15799 
15800 	/*
15801 	 * If we are not writer on 'q' then this interface exists already
15802 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15803 	 * so return EALREADY.
15804 	 */
15805 	if (ill != ipif->ipif_ill)
15806 		return (EALREADY);
15807 
15808 	if (ill->ill_name[0] != '\0')
15809 		return (EALREADY);
15810 
15811 	/*
15812 	 * If there's another ill already with the requested name, ensure
15813 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15814 	 * fuse together two unrelated ills, which will cause chaos.
15815 	 */
15816 	ipst = ill->ill_ipst;
15817 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15818 	    lifr->lifr_name, NULL);
15819 	if (phyi != NULL) {
15820 		ill_t *ill_mate = phyi->phyint_illv4;
15821 
15822 		if (ill_mate == NULL)
15823 			ill_mate = phyi->phyint_illv6;
15824 		ASSERT(ill_mate != NULL);
15825 
15826 		if (ill_mate->ill_media->ip_m_mac_type !=
15827 		    ill->ill_media->ip_m_mac_type) {
15828 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15829 			    "use the same ill name on differing media\n"));
15830 			return (EINVAL);
15831 		}
15832 	}
15833 
15834 	/*
15835 	 * We start off as IFF_IPV4 in ipif_allocate and become
15836 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15837 	 * The only flags that we read from user space are IFF_IPV4,
15838 	 * IFF_IPV6, and IFF_BROADCAST.
15839 	 *
15840 	 * This ill has not been inserted into the global list.
15841 	 * So we are still single threaded and don't need any lock
15842 	 *
15843 	 * Saniy check the flags.
15844 	 */
15845 
15846 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15847 	    ((lifr->lifr_flags & IFF_IPV6) ||
15848 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15849 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15850 		    "or IPv6 i.e., no broadcast \n"));
15851 		return (EINVAL);
15852 	}
15853 
15854 	new_flags =
15855 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15856 
15857 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15858 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15859 		    "IFF_IPV4 or IFF_IPV6\n"));
15860 		return (EINVAL);
15861 	}
15862 
15863 	/*
15864 	 * We always start off as IPv4, so only need to check for IPv6.
15865 	 */
15866 	if ((new_flags & IFF_IPV6) != 0) {
15867 		ill->ill_flags |= ILLF_IPV6;
15868 		ill->ill_flags &= ~ILLF_IPV4;
15869 
15870 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15871 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15872 	}
15873 
15874 	if ((new_flags & IFF_BROADCAST) != 0)
15875 		ipif->ipif_flags |= IPIF_BROADCAST;
15876 	else
15877 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15878 
15879 	/* We started off as V4. */
15880 	if (ill->ill_flags & ILLF_IPV6) {
15881 		ill->ill_phyint->phyint_illv6 = ill;
15882 		ill->ill_phyint->phyint_illv4 = NULL;
15883 	}
15884 
15885 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15886 }
15887 
15888 /* ARGSUSED */
15889 int
15890 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15891     ip_ioctl_cmd_t *ipip, void *if_req)
15892 {
15893 	/*
15894 	 * ill_phyint_reinit merged the v4 and v6 into a single
15895 	 * ipsq.  We might not have been able to complete the
15896 	 * slifname in ipif_set_values, if we could not become
15897 	 * exclusive.  If so restart it here
15898 	 */
15899 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15900 }
15901 
15902 /*
15903  * Return a pointer to the ipif which matches the index, IP version type and
15904  * zoneid.
15905  */
15906 ipif_t *
15907 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15908     ip_stack_t *ipst)
15909 {
15910 	ill_t	*ill;
15911 	ipif_t	*ipif = NULL;
15912 
15913 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15914 	if (ill != NULL) {
15915 		mutex_enter(&ill->ill_lock);
15916 		for (ipif = ill->ill_ipif; ipif != NULL;
15917 		    ipif = ipif->ipif_next) {
15918 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15919 			    zoneid == ipif->ipif_zoneid ||
15920 			    ipif->ipif_zoneid == ALL_ZONES)) {
15921 				ipif_refhold_locked(ipif);
15922 				break;
15923 			}
15924 		}
15925 		mutex_exit(&ill->ill_lock);
15926 		ill_refrele(ill);
15927 	}
15928 	return (ipif);
15929 }
15930 
15931 /*
15932  * Change an existing physical interface's index. If the new index
15933  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15934  * Finally, we update other systems which may have a dependence on the
15935  * index value.
15936  */
15937 /* ARGSUSED */
15938 int
15939 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15940     ip_ioctl_cmd_t *ipip, void *ifreq)
15941 {
15942 	ill_t		*ill;
15943 	phyint_t	*phyi;
15944 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15945 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15946 	uint_t	old_index, index;
15947 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15948 	avl_index_t	where;
15949 
15950 	if (ipip->ipi_cmd_type == IF_CMD)
15951 		index = ifr->ifr_index;
15952 	else
15953 		index = lifr->lifr_index;
15954 
15955 	/*
15956 	 * Only allow on physical interface. Also, index zero is illegal.
15957 	 */
15958 	ill = ipif->ipif_ill;
15959 	phyi = ill->ill_phyint;
15960 	if (ipif->ipif_id != 0 || index == 0) {
15961 		return (EINVAL);
15962 	}
15963 
15964 	/* If the index is not changing, no work to do */
15965 	if (phyi->phyint_ifindex == index)
15966 		return (0);
15967 
15968 	/*
15969 	 * Use phyint_exists() to determine if the new interface index
15970 	 * is already in use. If the index is unused then we need to
15971 	 * change the phyint's position in the phyint_list_avl_by_index
15972 	 * tree. If we do not do this, subsequent lookups (using the new
15973 	 * index value) will not find the phyint.
15974 	 */
15975 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15976 	if (phyint_exists(index, ipst)) {
15977 		rw_exit(&ipst->ips_ill_g_lock);
15978 		return (EEXIST);
15979 	}
15980 
15981 	/*
15982 	 * The new index is unused. Set it in the phyint. However we must not
15983 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15984 	 * changes. The event must be bound to old ifindex value.
15985 	 */
15986 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15987 	    &index, sizeof (index));
15988 
15989 	old_index = phyi->phyint_ifindex;
15990 	phyi->phyint_ifindex = index;
15991 
15992 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15993 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15994 	    &index, &where);
15995 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15996 	    phyi, where);
15997 	rw_exit(&ipst->ips_ill_g_lock);
15998 
15999 	/* Update SCTP's ILL list */
16000 	sctp_ill_reindex(ill, old_index);
16001 
16002 	/* Send the routing sockets message */
16003 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
16004 	if (ILL_OTHER(ill))
16005 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
16006 
16007 	/* Perhaps ilgs should use this ill */
16008 	update_conn_ill(NULL, ill->ill_ipst);
16009 	return (0);
16010 }
16011 
16012 /* ARGSUSED */
16013 int
16014 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16015     ip_ioctl_cmd_t *ipip, void *ifreq)
16016 {
16017 	struct ifreq	*ifr = (struct ifreq *)ifreq;
16018 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16019 
16020 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
16021 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16022 	/* Get the interface index */
16023 	if (ipip->ipi_cmd_type == IF_CMD) {
16024 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16025 	} else {
16026 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16027 	}
16028 	return (0);
16029 }
16030 
16031 /* ARGSUSED */
16032 int
16033 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16034     ip_ioctl_cmd_t *ipip, void *ifreq)
16035 {
16036 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16037 
16038 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
16039 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16040 	/* Get the interface zone */
16041 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16042 	lifr->lifr_zoneid = ipif->ipif_zoneid;
16043 	return (0);
16044 }
16045 
16046 /*
16047  * Set the zoneid of an interface.
16048  */
16049 /* ARGSUSED */
16050 int
16051 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16052     ip_ioctl_cmd_t *ipip, void *ifreq)
16053 {
16054 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16055 	int err = 0;
16056 	boolean_t need_up = B_FALSE;
16057 	zone_t *zptr;
16058 	zone_status_t status;
16059 	zoneid_t zoneid;
16060 
16061 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16062 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16063 		if (!is_system_labeled())
16064 			return (ENOTSUP);
16065 		zoneid = GLOBAL_ZONEID;
16066 	}
16067 
16068 	/* cannot assign instance zero to a non-global zone */
16069 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16070 		return (ENOTSUP);
16071 
16072 	/*
16073 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
16074 	 * the event of a race with the zone shutdown processing, since IP
16075 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16076 	 * interface will be cleaned up even if the zone is shut down
16077 	 * immediately after the status check. If the interface can't be brought
16078 	 * down right away, and the zone is shut down before the restart
16079 	 * function is called, we resolve the possible races by rechecking the
16080 	 * zone status in the restart function.
16081 	 */
16082 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
16083 		return (EINVAL);
16084 	status = zone_status_get(zptr);
16085 	zone_rele(zptr);
16086 
16087 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16088 		return (EINVAL);
16089 
16090 	if (ipif->ipif_flags & IPIF_UP) {
16091 		/*
16092 		 * If the interface is already marked up,
16093 		 * we call ipif_down which will take care
16094 		 * of ditching any IREs that have been set
16095 		 * up based on the old interface address.
16096 		 */
16097 		err = ipif_logical_down(ipif, q, mp);
16098 		if (err == EINPROGRESS)
16099 			return (err);
16100 		(void) ipif_down_tail(ipif);
16101 		need_up = B_TRUE;
16102 	}
16103 
16104 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16105 	return (err);
16106 }
16107 
16108 static int
16109 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16110     queue_t *q, mblk_t *mp, boolean_t need_up)
16111 {
16112 	int	err = 0;
16113 	ip_stack_t	*ipst;
16114 
16115 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16116 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16117 
16118 	if (CONN_Q(q))
16119 		ipst = CONNQ_TO_IPST(q);
16120 	else
16121 		ipst = ILLQ_TO_IPST(q);
16122 
16123 	/*
16124 	 * For exclusive stacks we don't allow a different zoneid than
16125 	 * global.
16126 	 */
16127 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16128 	    zoneid != GLOBAL_ZONEID)
16129 		return (EINVAL);
16130 
16131 	/* Set the new zone id. */
16132 	ipif->ipif_zoneid = zoneid;
16133 
16134 	/* Update sctp list */
16135 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16136 
16137 	/* The default multicast interface might have changed */
16138 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16139 
16140 	if (need_up) {
16141 		/*
16142 		 * Now bring the interface back up.  If this
16143 		 * is the only IPIF for the ILL, ipif_up
16144 		 * will have to re-bind to the device, so
16145 		 * we may get back EINPROGRESS, in which
16146 		 * case, this IOCTL will get completed in
16147 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
16148 		 */
16149 		err = ipif_up(ipif, q, mp);
16150 	}
16151 	return (err);
16152 }
16153 
16154 /* ARGSUSED */
16155 int
16156 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16157     ip_ioctl_cmd_t *ipip, void *if_req)
16158 {
16159 	struct lifreq *lifr = (struct lifreq *)if_req;
16160 	zoneid_t zoneid;
16161 	zone_t *zptr;
16162 	zone_status_t status;
16163 
16164 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16165 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16166 		zoneid = GLOBAL_ZONEID;
16167 
16168 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16169 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16170 
16171 	/*
16172 	 * We recheck the zone status to resolve the following race condition:
16173 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16174 	 * 2) hme0:1 is up and can't be brought down right away;
16175 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16176 	 * 3) zone "myzone" is halted; the zone status switches to
16177 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16178 	 * the interfaces to remove - hme0:1 is not returned because it's not
16179 	 * yet in "myzone", so it won't be removed;
16180 	 * 4) the restart function for SIOCSLIFZONE is called; without the
16181 	 * status check here, we would have hme0:1 in "myzone" after it's been
16182 	 * destroyed.
16183 	 * Note that if the status check fails, we need to bring the interface
16184 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16185 	 * ipif_up_done[_v6]().
16186 	 */
16187 	status = ZONE_IS_UNINITIALIZED;
16188 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16189 		status = zone_status_get(zptr);
16190 		zone_rele(zptr);
16191 	}
16192 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16193 		if (ipif->ipif_isv6) {
16194 			(void) ipif_up_done_v6(ipif);
16195 		} else {
16196 			(void) ipif_up_done(ipif);
16197 		}
16198 		return (EINVAL);
16199 	}
16200 
16201 	(void) ipif_down_tail(ipif);
16202 
16203 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16204 	    B_TRUE));
16205 }
16206 
16207 /*
16208  * Return the number of addresses on `ill' with one or more of the values
16209  * in `set' set and all of the values in `clear' clear.
16210  */
16211 static uint_t
16212 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16213 {
16214 	ipif_t	*ipif;
16215 	uint_t	cnt = 0;
16216 
16217 	ASSERT(IAM_WRITER_ILL(ill));
16218 
16219 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16220 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16221 			cnt++;
16222 
16223 	return (cnt);
16224 }
16225 
16226 /*
16227  * Return the number of migratable addresses on `ill' that are under
16228  * application control.
16229  */
16230 uint_t
16231 ill_appaddr_cnt(const ill_t *ill)
16232 {
16233 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16234 	    IPIF_NOFAILOVER));
16235 }
16236 
16237 /*
16238  * Return the number of point-to-point addresses on `ill'.
16239  */
16240 uint_t
16241 ill_ptpaddr_cnt(const ill_t *ill)
16242 {
16243 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16244 }
16245 
16246 /* ARGSUSED */
16247 int
16248 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16249 	ip_ioctl_cmd_t *ipip, void *ifreq)
16250 {
16251 	struct lifreq	*lifr = ifreq;
16252 
16253 	ASSERT(q->q_next == NULL);
16254 	ASSERT(CONN_Q(q));
16255 
16256 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16257 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16258 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16259 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16260 
16261 	return (0);
16262 }
16263 
16264 /* Find the previous ILL in this usesrc group */
16265 static ill_t *
16266 ill_prev_usesrc(ill_t *uill)
16267 {
16268 	ill_t *ill;
16269 
16270 	for (ill = uill->ill_usesrc_grp_next;
16271 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16272 	    ill = ill->ill_usesrc_grp_next)
16273 		/* do nothing */;
16274 	return (ill);
16275 }
16276 
16277 /*
16278  * Release all members of the usesrc group. This routine is called
16279  * from ill_delete when the interface being unplumbed is the
16280  * group head.
16281  *
16282  * This silently clears the usesrc that ifconfig setup.
16283  * An alternative would be to keep that ifindex, and drop packets on the floor
16284  * since no source address can be selected.
16285  * Even if we keep the current semantics, don't need a lock and a linked list.
16286  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16287  * the one that is being removed. Issue is how we return the usesrc users
16288  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16289  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16290  * ill walk, but the walker would need to insert in the ioctl response.
16291  */
16292 static void
16293 ill_disband_usesrc_group(ill_t *uill)
16294 {
16295 	ill_t *next_ill, *tmp_ill;
16296 	ip_stack_t	*ipst = uill->ill_ipst;
16297 
16298 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16299 	next_ill = uill->ill_usesrc_grp_next;
16300 
16301 	do {
16302 		ASSERT(next_ill != NULL);
16303 		tmp_ill = next_ill->ill_usesrc_grp_next;
16304 		ASSERT(tmp_ill != NULL);
16305 		next_ill->ill_usesrc_grp_next = NULL;
16306 		next_ill->ill_usesrc_ifindex = 0;
16307 		next_ill = tmp_ill;
16308 	} while (next_ill->ill_usesrc_ifindex != 0);
16309 	uill->ill_usesrc_grp_next = NULL;
16310 }
16311 
16312 /*
16313  * Remove the client usesrc ILL from the list and relink to a new list
16314  */
16315 int
16316 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16317 {
16318 	ill_t *ill, *tmp_ill;
16319 	ip_stack_t	*ipst = ucill->ill_ipst;
16320 
16321 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16322 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16323 
16324 	/*
16325 	 * Check if the usesrc client ILL passed in is not already
16326 	 * in use as a usesrc ILL i.e one whose source address is
16327 	 * in use OR a usesrc ILL is not already in use as a usesrc
16328 	 * client ILL
16329 	 */
16330 	if ((ucill->ill_usesrc_ifindex == 0) ||
16331 	    (uill->ill_usesrc_ifindex != 0)) {
16332 		return (-1);
16333 	}
16334 
16335 	ill = ill_prev_usesrc(ucill);
16336 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16337 
16338 	/* Remove from the current list */
16339 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16340 		/* Only two elements in the list */
16341 		ASSERT(ill->ill_usesrc_ifindex == 0);
16342 		ill->ill_usesrc_grp_next = NULL;
16343 	} else {
16344 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16345 	}
16346 
16347 	if (ifindex == 0) {
16348 		ucill->ill_usesrc_ifindex = 0;
16349 		ucill->ill_usesrc_grp_next = NULL;
16350 		return (0);
16351 	}
16352 
16353 	ucill->ill_usesrc_ifindex = ifindex;
16354 	tmp_ill = uill->ill_usesrc_grp_next;
16355 	uill->ill_usesrc_grp_next = ucill;
16356 	ucill->ill_usesrc_grp_next =
16357 	    (tmp_ill != NULL) ? tmp_ill : uill;
16358 	return (0);
16359 }
16360 
16361 /*
16362  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16363  * ip.c for locking details.
16364  */
16365 /* ARGSUSED */
16366 int
16367 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16368     ip_ioctl_cmd_t *ipip, void *ifreq)
16369 {
16370 	struct lifreq *lifr = (struct lifreq *)ifreq;
16371 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16372 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16373 	int err = 0, ret;
16374 	uint_t ifindex;
16375 	ipsq_t *ipsq = NULL;
16376 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16377 
16378 	ASSERT(IAM_WRITER_IPIF(ipif));
16379 	ASSERT(q->q_next == NULL);
16380 	ASSERT(CONN_Q(q));
16381 
16382 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16383 
16384 	ifindex = lifr->lifr_index;
16385 	if (ifindex == 0) {
16386 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16387 			/* non usesrc group interface, nothing to reset */
16388 			return (0);
16389 		}
16390 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16391 		/* valid reset request */
16392 		reset_flg = B_TRUE;
16393 	}
16394 
16395 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16396 	if (usesrc_ill == NULL)
16397 		return (ENXIO);
16398 	if (usesrc_ill == ipif->ipif_ill) {
16399 		ill_refrele(usesrc_ill);
16400 		return (EINVAL);
16401 	}
16402 
16403 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16404 	    NEW_OP, B_TRUE);
16405 	if (ipsq == NULL) {
16406 		err = EINPROGRESS;
16407 		/* Operation enqueued on the ipsq of the usesrc ILL */
16408 		goto done;
16409 	}
16410 
16411 	/* USESRC isn't currently supported with IPMP */
16412 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16413 		err = ENOTSUP;
16414 		goto done;
16415 	}
16416 
16417 	/*
16418 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16419 	 * used by IPMP underlying interfaces, but someone might think it's
16420 	 * more general and try to use it independently with VNI.)
16421 	 */
16422 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16423 		err = ENOTSUP;
16424 		goto done;
16425 	}
16426 
16427 	/*
16428 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16429 	 * already a client then return EINVAL
16430 	 */
16431 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16432 		err = EINVAL;
16433 		goto done;
16434 	}
16435 
16436 	/*
16437 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16438 	 * be then this is a duplicate operation.
16439 	 */
16440 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16441 		err = 0;
16442 		goto done;
16443 	}
16444 
16445 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16446 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16447 	    usesrc_ill->ill_isv6));
16448 
16449 	/*
16450 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16451 	 * and the ill_usesrc_ifindex fields
16452 	 */
16453 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16454 
16455 	if (reset_flg) {
16456 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16457 		if (ret != 0) {
16458 			err = EINVAL;
16459 		}
16460 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16461 		goto done;
16462 	}
16463 
16464 	/*
16465 	 * Four possibilities to consider:
16466 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16467 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16468 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16469 	 * 4. Both are part of their respective usesrc groups
16470 	 */
16471 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16472 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16473 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16474 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16475 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16476 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16477 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16478 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16479 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16480 		/* Insert at head of list */
16481 		usesrc_cli_ill->ill_usesrc_grp_next =
16482 		    usesrc_ill->ill_usesrc_grp_next;
16483 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16484 	} else {
16485 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16486 		    ifindex);
16487 		if (ret != 0)
16488 			err = EINVAL;
16489 	}
16490 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16491 
16492 done:
16493 	if (ipsq != NULL)
16494 		ipsq_exit(ipsq);
16495 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16496 	ill_refrele(usesrc_ill);
16497 
16498 	/* Let conn_ixa caching know that source address selection changed */
16499 	ip_update_source_selection(ipst);
16500 
16501 	return (err);
16502 }
16503 
16504 /* ARGSUSED */
16505 int
16506 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16507     ip_ioctl_cmd_t *ipip, void *if_req)
16508 {
16509 	struct lifreq	*lifr = (struct lifreq *)if_req;
16510 	ill_t		*ill = ipif->ipif_ill;
16511 
16512 	/*
16513 	 * Need a lock since IFF_UP can be set even when there are
16514 	 * references to the ipif.
16515 	 */
16516 	mutex_enter(&ill->ill_lock);
16517 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16518 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16519 	else
16520 		lifr->lifr_dadstate = DAD_DONE;
16521 	mutex_exit(&ill->ill_lock);
16522 	return (0);
16523 }
16524 
16525 /*
16526  * comparison function used by avl.
16527  */
16528 static int
16529 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16530 {
16531 
16532 	uint_t index;
16533 
16534 	ASSERT(phyip != NULL && index_ptr != NULL);
16535 
16536 	index = *((uint_t *)index_ptr);
16537 	/*
16538 	 * let the phyint with the lowest index be on top.
16539 	 */
16540 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16541 		return (1);
16542 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16543 		return (-1);
16544 	return (0);
16545 }
16546 
16547 /*
16548  * comparison function used by avl.
16549  */
16550 static int
16551 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16552 {
16553 	ill_t *ill;
16554 	int res = 0;
16555 
16556 	ASSERT(phyip != NULL && name_ptr != NULL);
16557 
16558 	if (((phyint_t *)phyip)->phyint_illv4)
16559 		ill = ((phyint_t *)phyip)->phyint_illv4;
16560 	else
16561 		ill = ((phyint_t *)phyip)->phyint_illv6;
16562 	ASSERT(ill != NULL);
16563 
16564 	res = strcmp(ill->ill_name, (char *)name_ptr);
16565 	if (res > 0)
16566 		return (1);
16567 	else if (res < 0)
16568 		return (-1);
16569 	return (0);
16570 }
16571 
16572 /*
16573  * This function is called on the unplumb path via ill_glist_delete() when
16574  * there are no ills left on the phyint and thus the phyint can be freed.
16575  */
16576 static void
16577 phyint_free(phyint_t *phyi)
16578 {
16579 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16580 
16581 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16582 
16583 	/*
16584 	 * If this phyint was an IPMP meta-interface, blow away the group.
16585 	 * This is safe to do because all of the illgrps have already been
16586 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16587 	 * If we're cleaning up as a result of failed initialization,
16588 	 * phyint_grp may be NULL.
16589 	 */
16590 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16591 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16592 		ipmp_grp_destroy(phyi->phyint_grp);
16593 		phyi->phyint_grp = NULL;
16594 		rw_exit(&ipst->ips_ipmp_lock);
16595 	}
16596 
16597 	/*
16598 	 * If this interface was under IPMP, take it out of the group.
16599 	 */
16600 	if (phyi->phyint_grp != NULL)
16601 		ipmp_phyint_leave_grp(phyi);
16602 
16603 	/*
16604 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16605 	 * will be freed in ipsq_exit().
16606 	 */
16607 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16608 	phyi->phyint_name[0] = '\0';
16609 
16610 	mi_free(phyi);
16611 }
16612 
16613 /*
16614  * Attach the ill to the phyint structure which can be shared by both
16615  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16616  * function is called from ipif_set_values and ill_lookup_on_name (for
16617  * loopback) where we know the name of the ill. We lookup the ill and if
16618  * there is one present already with the name use that phyint. Otherwise
16619  * reuse the one allocated by ill_init.
16620  */
16621 static void
16622 ill_phyint_reinit(ill_t *ill)
16623 {
16624 	boolean_t isv6 = ill->ill_isv6;
16625 	phyint_t *phyi_old;
16626 	phyint_t *phyi;
16627 	avl_index_t where = 0;
16628 	ill_t	*ill_other = NULL;
16629 	ip_stack_t	*ipst = ill->ill_ipst;
16630 
16631 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16632 
16633 	phyi_old = ill->ill_phyint;
16634 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16635 	    phyi_old->phyint_illv6 == NULL));
16636 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16637 	    phyi_old->phyint_illv4 == NULL));
16638 	ASSERT(phyi_old->phyint_ifindex == 0);
16639 
16640 	/*
16641 	 * Now that our ill has a name, set it in the phyint.
16642 	 */
16643 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16644 
16645 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16646 	    ill->ill_name, &where);
16647 
16648 	/*
16649 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16650 	 *    the global list of ills. So no other thread could have located
16651 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16652 	 * 2. Now locate the other protocol instance of this ill.
16653 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16654 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16655 	 *    of neither ill can change.
16656 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16657 	 *    other ill.
16658 	 * 5. Release all locks.
16659 	 */
16660 
16661 	/*
16662 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16663 	 * we are initializing IPv4.
16664 	 */
16665 	if (phyi != NULL) {
16666 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16667 		ASSERT(ill_other->ill_phyint != NULL);
16668 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16669 		    (!isv6 && ill_other->ill_isv6));
16670 		GRAB_ILL_LOCKS(ill, ill_other);
16671 		/*
16672 		 * We are potentially throwing away phyint_flags which
16673 		 * could be different from the one that we obtain from
16674 		 * ill_other->ill_phyint. But it is okay as we are assuming
16675 		 * that the state maintained within IP is correct.
16676 		 */
16677 		mutex_enter(&phyi->phyint_lock);
16678 		if (isv6) {
16679 			ASSERT(phyi->phyint_illv6 == NULL);
16680 			phyi->phyint_illv6 = ill;
16681 		} else {
16682 			ASSERT(phyi->phyint_illv4 == NULL);
16683 			phyi->phyint_illv4 = ill;
16684 		}
16685 
16686 		/*
16687 		 * Delete the old phyint and make its ipsq eligible
16688 		 * to be freed in ipsq_exit().
16689 		 */
16690 		phyi_old->phyint_illv4 = NULL;
16691 		phyi_old->phyint_illv6 = NULL;
16692 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16693 		phyi_old->phyint_name[0] = '\0';
16694 		mi_free(phyi_old);
16695 	} else {
16696 		mutex_enter(&ill->ill_lock);
16697 		/*
16698 		 * We don't need to acquire any lock, since
16699 		 * the ill is not yet visible globally  and we
16700 		 * have not yet released the ill_g_lock.
16701 		 */
16702 		phyi = phyi_old;
16703 		mutex_enter(&phyi->phyint_lock);
16704 		/* XXX We need a recovery strategy here. */
16705 		if (!phyint_assign_ifindex(phyi, ipst))
16706 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16707 
16708 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16709 		    (void *)phyi, where);
16710 
16711 		(void) avl_find(&ipst->ips_phyint_g_list->
16712 		    phyint_list_avl_by_index,
16713 		    &phyi->phyint_ifindex, &where);
16714 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16715 		    (void *)phyi, where);
16716 	}
16717 
16718 	/*
16719 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16720 	 * pending mp is not affected because that is per ill basis.
16721 	 */
16722 	ill->ill_phyint = phyi;
16723 
16724 	/*
16725 	 * Now that the phyint's ifindex has been assigned, complete the
16726 	 * remaining
16727 	 */
16728 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16729 	if (ill->ill_isv6) {
16730 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16731 		    ill->ill_phyint->phyint_ifindex;
16732 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16733 	} else {
16734 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16735 	}
16736 
16737 	/*
16738 	 * Generate an event within the hooks framework to indicate that
16739 	 * a new interface has just been added to IP.  For this event to
16740 	 * be generated, the network interface must, at least, have an
16741 	 * ifindex assigned to it.  (We don't generate the event for
16742 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16743 	 *
16744 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16745 	 * that the ordering of delivered events to listeners matches the
16746 	 * order of them in the kernel.
16747 	 */
16748 	if (!IS_LOOPBACK(ill)) {
16749 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16750 		    ill->ill_name_length);
16751 	}
16752 	RELEASE_ILL_LOCKS(ill, ill_other);
16753 	mutex_exit(&phyi->phyint_lock);
16754 }
16755 
16756 /*
16757  * Notify any downstream modules of the name of this interface.
16758  * An M_IOCTL is used even though we don't expect a successful reply.
16759  * Any reply message from the driver (presumably an M_IOCNAK) will
16760  * eventually get discarded somewhere upstream.  The message format is
16761  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16762  * to IP.
16763  */
16764 static void
16765 ip_ifname_notify(ill_t *ill, queue_t *q)
16766 {
16767 	mblk_t *mp1, *mp2;
16768 	struct iocblk *iocp;
16769 	struct lifreq *lifr;
16770 
16771 	mp1 = mkiocb(SIOCSLIFNAME);
16772 	if (mp1 == NULL)
16773 		return;
16774 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16775 	if (mp2 == NULL) {
16776 		freeb(mp1);
16777 		return;
16778 	}
16779 
16780 	mp1->b_cont = mp2;
16781 	iocp = (struct iocblk *)mp1->b_rptr;
16782 	iocp->ioc_count = sizeof (struct lifreq);
16783 
16784 	lifr = (struct lifreq *)mp2->b_rptr;
16785 	mp2->b_wptr += sizeof (struct lifreq);
16786 	bzero(lifr, sizeof (struct lifreq));
16787 
16788 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16789 	lifr->lifr_ppa = ill->ill_ppa;
16790 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16791 
16792 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16793 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16794 	putnext(q, mp1);
16795 }
16796 
16797 static int
16798 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16799 {
16800 	int		err;
16801 	ip_stack_t	*ipst = ill->ill_ipst;
16802 	phyint_t	*phyi = ill->ill_phyint;
16803 
16804 	/*
16805 	 * Now that ill_name is set, the configuration for the IPMP
16806 	 * meta-interface can be performed.
16807 	 */
16808 	if (IS_IPMP(ill)) {
16809 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16810 		/*
16811 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16812 		 * meta-interface and we need to create the IPMP group.
16813 		 */
16814 		if (phyi->phyint_grp == NULL) {
16815 			/*
16816 			 * If someone has renamed another IPMP group to have
16817 			 * the same name as our interface, bail.
16818 			 */
16819 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16820 				rw_exit(&ipst->ips_ipmp_lock);
16821 				return (EEXIST);
16822 			}
16823 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16824 			if (phyi->phyint_grp == NULL) {
16825 				rw_exit(&ipst->ips_ipmp_lock);
16826 				return (ENOMEM);
16827 			}
16828 		}
16829 		rw_exit(&ipst->ips_ipmp_lock);
16830 	}
16831 
16832 	/* Tell downstream modules where they are. */
16833 	ip_ifname_notify(ill, q);
16834 
16835 	/*
16836 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16837 	 * Error cases are ENOMEM ...
16838 	 */
16839 	err = ill_dl_phys(ill, ipif, mp, q);
16840 
16841 	if (ill->ill_isv6) {
16842 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16843 		if (ipst->ips_mld_slowtimeout_id == 0) {
16844 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16845 			    (void *)ipst,
16846 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16847 		}
16848 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16849 	} else {
16850 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16851 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16852 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16853 			    (void *)ipst,
16854 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16855 		}
16856 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16857 	}
16858 
16859 	return (err);
16860 }
16861 
16862 /*
16863  * Common routine for ppa and ifname setting. Should be called exclusive.
16864  *
16865  * Returns EINPROGRESS when mp has been consumed by queueing it on
16866  * ipx_pending_mp and the ioctl will complete in ip_rput.
16867  *
16868  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16869  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16870  * For SLIFNAME, we pass these values back to the userland.
16871  */
16872 static int
16873 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16874 {
16875 	ill_t	*ill;
16876 	ipif_t	*ipif;
16877 	ipsq_t	*ipsq;
16878 	char	*ppa_ptr;
16879 	char	*old_ptr;
16880 	char	old_char;
16881 	int	error;
16882 	ip_stack_t	*ipst;
16883 
16884 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16885 	ASSERT(q->q_next != NULL);
16886 	ASSERT(interf_name != NULL);
16887 
16888 	ill = (ill_t *)q->q_ptr;
16889 	ipst = ill->ill_ipst;
16890 
16891 	ASSERT(ill->ill_ipst != NULL);
16892 	ASSERT(ill->ill_name[0] == '\0');
16893 	ASSERT(IAM_WRITER_ILL(ill));
16894 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16895 	ASSERT(ill->ill_ppa == UINT_MAX);
16896 
16897 	ill->ill_defend_start = ill->ill_defend_count = 0;
16898 	/* The ppa is sent down by ifconfig or is chosen */
16899 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16900 		return (EINVAL);
16901 	}
16902 
16903 	/*
16904 	 * make sure ppa passed in is same as ppa in the name.
16905 	 * This check is not made when ppa == UINT_MAX in that case ppa
16906 	 * in the name could be anything. System will choose a ppa and
16907 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16908 	 */
16909 	if (*new_ppa_ptr != UINT_MAX) {
16910 		/* stoi changes the pointer */
16911 		old_ptr = ppa_ptr;
16912 		/*
16913 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16914 		 * (they don't have an externally visible ppa).  We assign one
16915 		 * here so that we can manage the interface.  Note that in
16916 		 * the past this value was always 0 for DLPI 1 drivers.
16917 		 */
16918 		if (*new_ppa_ptr == 0)
16919 			*new_ppa_ptr = stoi(&old_ptr);
16920 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16921 			return (EINVAL);
16922 	}
16923 	/*
16924 	 * terminate string before ppa
16925 	 * save char at that location.
16926 	 */
16927 	old_char = ppa_ptr[0];
16928 	ppa_ptr[0] = '\0';
16929 
16930 	ill->ill_ppa = *new_ppa_ptr;
16931 	/*
16932 	 * Finish as much work now as possible before calling ill_glist_insert
16933 	 * which makes the ill globally visible and also merges it with the
16934 	 * other protocol instance of this phyint. The remaining work is
16935 	 * done after entering the ipsq which may happen sometime later.
16936 	 */
16937 	ipif = ill->ill_ipif;
16938 
16939 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16940 	ipif_assign_seqid(ipif);
16941 
16942 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16943 		ill->ill_flags |= ILLF_IPV4;
16944 
16945 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16946 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16947 
16948 	if (ill->ill_flags & ILLF_IPV6) {
16949 
16950 		ill->ill_isv6 = B_TRUE;
16951 		ill_set_inputfn(ill);
16952 		if (ill->ill_rq != NULL) {
16953 			ill->ill_rq->q_qinfo = &iprinitv6;
16954 		}
16955 
16956 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16957 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16958 		ipif->ipif_v6subnet = ipv6_all_zeros;
16959 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16960 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16961 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16962 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16963 		/*
16964 		 * point-to-point or Non-mulicast capable
16965 		 * interfaces won't do NUD unless explicitly
16966 		 * configured to do so.
16967 		 */
16968 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16969 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16970 			ill->ill_flags |= ILLF_NONUD;
16971 		}
16972 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16973 		if (ill->ill_flags & ILLF_NOARP) {
16974 			/*
16975 			 * Note: xresolv interfaces will eventually need
16976 			 * NOARP set here as well, but that will require
16977 			 * those external resolvers to have some
16978 			 * knowledge of that flag and act appropriately.
16979 			 * Not to be changed at present.
16980 			 */
16981 			ill->ill_flags &= ~ILLF_NOARP;
16982 		}
16983 		/*
16984 		 * Set the ILLF_ROUTER flag according to the global
16985 		 * IPv6 forwarding policy.
16986 		 */
16987 		if (ipst->ips_ipv6_forwarding != 0)
16988 			ill->ill_flags |= ILLF_ROUTER;
16989 	} else if (ill->ill_flags & ILLF_IPV4) {
16990 		ill->ill_isv6 = B_FALSE;
16991 		ill_set_inputfn(ill);
16992 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16993 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16994 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16995 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16996 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16997 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16998 		/*
16999 		 * Set the ILLF_ROUTER flag according to the global
17000 		 * IPv4 forwarding policy.
17001 		 */
17002 		if (ipst->ips_ip_forwarding != 0)
17003 			ill->ill_flags |= ILLF_ROUTER;
17004 	}
17005 
17006 	ASSERT(ill->ill_phyint != NULL);
17007 
17008 	/*
17009 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
17010 	 * be completed in ill_glist_insert -> ill_phyint_reinit
17011 	 */
17012 	if (!ill_allocate_mibs(ill))
17013 		return (ENOMEM);
17014 
17015 	/*
17016 	 * Pick a default sap until we get the DL_INFO_ACK back from
17017 	 * the driver.
17018 	 */
17019 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
17020 	    ill->ill_media->ip_m_ipv4sap;
17021 
17022 	ill->ill_ifname_pending = 1;
17023 	ill->ill_ifname_pending_err = 0;
17024 
17025 	/*
17026 	 * When the first ipif comes up in ipif_up_done(), multicast groups
17027 	 * that were joined while this ill was not bound to the DLPI link need
17028 	 * to be recovered by ill_recover_multicast().
17029 	 */
17030 	ill->ill_need_recover_multicast = 1;
17031 
17032 	ill_refhold(ill);
17033 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17034 	if ((error = ill_glist_insert(ill, interf_name,
17035 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
17036 		ill->ill_ppa = UINT_MAX;
17037 		ill->ill_name[0] = '\0';
17038 		/*
17039 		 * undo null termination done above.
17040 		 */
17041 		ppa_ptr[0] = old_char;
17042 		rw_exit(&ipst->ips_ill_g_lock);
17043 		ill_refrele(ill);
17044 		return (error);
17045 	}
17046 
17047 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
17048 
17049 	/*
17050 	 * When we return the buffer pointed to by interf_name should contain
17051 	 * the same name as in ill_name.
17052 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
17053 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
17054 	 * so copy full name and update the ppa ptr.
17055 	 * When ppa passed in != UINT_MAX all values are correct just undo
17056 	 * null termination, this saves a bcopy.
17057 	 */
17058 	if (*new_ppa_ptr == UINT_MAX) {
17059 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
17060 		*new_ppa_ptr = ill->ill_ppa;
17061 	} else {
17062 		/*
17063 		 * undo null termination done above.
17064 		 */
17065 		ppa_ptr[0] = old_char;
17066 	}
17067 
17068 	/* Let SCTP know about this ILL */
17069 	sctp_update_ill(ill, SCTP_ILL_INSERT);
17070 
17071 	/*
17072 	 * ill_glist_insert has made the ill visible globally, and
17073 	 * ill_phyint_reinit could have changed the ipsq. At this point,
17074 	 * we need to hold the ips_ill_g_lock across the call to enter the
17075 	 * ipsq to enforce atomicity and prevent reordering. In the event
17076 	 * the ipsq has changed, and if the new ipsq is currently busy,
17077 	 * we need to make sure that this half-completed ioctl is ahead of
17078 	 * any subsequent ioctl. We achieve this by not dropping the
17079 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
17080 	 * ensuring that new ioctls can't start.
17081 	 */
17082 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17083 	    B_TRUE);
17084 
17085 	rw_exit(&ipst->ips_ill_g_lock);
17086 	ill_refrele(ill);
17087 	if (ipsq == NULL)
17088 		return (EINPROGRESS);
17089 
17090 	/*
17091 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17092 	 */
17093 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17094 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17095 	else
17096 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17097 
17098 	error = ipif_set_values_tail(ill, ipif, mp, q);
17099 	ipsq_exit(ipsq);
17100 	if (error != 0 && error != EINPROGRESS) {
17101 		/*
17102 		 * restore previous values
17103 		 */
17104 		ill->ill_isv6 = B_FALSE;
17105 		ill_set_inputfn(ill);
17106 	}
17107 	return (error);
17108 }
17109 
17110 void
17111 ipif_init(ip_stack_t *ipst)
17112 {
17113 	int i;
17114 
17115 	for (i = 0; i < MAX_G_HEADS; i++) {
17116 		ipst->ips_ill_g_heads[i].ill_g_list_head =
17117 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17118 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
17119 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17120 	}
17121 
17122 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17123 	    ill_phyint_compare_index,
17124 	    sizeof (phyint_t),
17125 	    offsetof(struct phyint, phyint_avl_by_index));
17126 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17127 	    ill_phyint_compare_name,
17128 	    sizeof (phyint_t),
17129 	    offsetof(struct phyint, phyint_avl_by_name));
17130 }
17131 
17132 /*
17133  * Save enough information so that we can recreate the IRE if
17134  * the interface goes down and then up.
17135  */
17136 void
17137 ill_save_ire(ill_t *ill, ire_t *ire)
17138 {
17139 	mblk_t	*save_mp;
17140 
17141 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17142 	if (save_mp != NULL) {
17143 		ifrt_t	*ifrt;
17144 
17145 		save_mp->b_wptr += sizeof (ifrt_t);
17146 		ifrt = (ifrt_t *)save_mp->b_rptr;
17147 		bzero(ifrt, sizeof (ifrt_t));
17148 		ifrt->ifrt_type = ire->ire_type;
17149 		if (ire->ire_ipversion == IPV4_VERSION) {
17150 			ASSERT(!ill->ill_isv6);
17151 			ifrt->ifrt_addr = ire->ire_addr;
17152 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17153 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17154 			ifrt->ifrt_mask = ire->ire_mask;
17155 		} else {
17156 			ASSERT(ill->ill_isv6);
17157 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
17158 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17159 			mutex_enter(&ire->ire_lock);
17160 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17161 			mutex_exit(&ire->ire_lock);
17162 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17163 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
17164 		}
17165 		ifrt->ifrt_flags = ire->ire_flags;
17166 		ifrt->ifrt_zoneid = ire->ire_zoneid;
17167 		mutex_enter(&ill->ill_saved_ire_lock);
17168 		save_mp->b_cont = ill->ill_saved_ire_mp;
17169 		ill->ill_saved_ire_mp = save_mp;
17170 		ill->ill_saved_ire_cnt++;
17171 		mutex_exit(&ill->ill_saved_ire_lock);
17172 	}
17173 }
17174 
17175 /*
17176  * Remove one entry from ill_saved_ire_mp.
17177  */
17178 void
17179 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17180 {
17181 	mblk_t	**mpp;
17182 	mblk_t	*mp;
17183 	ifrt_t	*ifrt;
17184 
17185 	/* Remove from ill_saved_ire_mp list if it is there */
17186 	mutex_enter(&ill->ill_saved_ire_lock);
17187 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17188 	    mpp = &(*mpp)->b_cont) {
17189 		in6_addr_t	gw_addr_v6;
17190 
17191 		/*
17192 		 * On a given ill, the tuple of address, gateway, mask,
17193 		 * ire_type, and zoneid is unique for each saved IRE.
17194 		 */
17195 		mp = *mpp;
17196 		ifrt = (ifrt_t *)mp->b_rptr;
17197 		/* ire_gateway_addr_v6 can change - need lock */
17198 		mutex_enter(&ire->ire_lock);
17199 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17200 		mutex_exit(&ire->ire_lock);
17201 
17202 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17203 		    ifrt->ifrt_type != ire->ire_type)
17204 			continue;
17205 
17206 		if (ill->ill_isv6 ?
17207 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17208 		    &ire->ire_addr_v6) &&
17209 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17210 		    &gw_addr_v6) &&
17211 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17212 		    &ire->ire_mask_v6)) :
17213 		    (ifrt->ifrt_addr == ire->ire_addr &&
17214 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17215 		    ifrt->ifrt_mask == ire->ire_mask)) {
17216 			*mpp = mp->b_cont;
17217 			ill->ill_saved_ire_cnt--;
17218 			freeb(mp);
17219 			break;
17220 		}
17221 	}
17222 	mutex_exit(&ill->ill_saved_ire_lock);
17223 }
17224 
17225 /*
17226  * IP multirouting broadcast routes handling
17227  * Append CGTP broadcast IREs to regular ones created
17228  * at ifconfig time.
17229  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17230  * the destination and the gateway are broadcast addresses.
17231  * The caller has verified that the destination is an IRE_BROADCAST and that
17232  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17233  * we create a MULTIRT IRE_BROADCAST.
17234  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17235  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17236  */
17237 static void
17238 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17239 {
17240 	ire_t *ire_prim;
17241 
17242 	ASSERT(ire != NULL);
17243 
17244 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17245 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17246 	    NULL);
17247 	if (ire_prim != NULL) {
17248 		/*
17249 		 * We are in the special case of broadcasts for
17250 		 * CGTP. We add an IRE_BROADCAST that holds
17251 		 * the RTF_MULTIRT flag, the destination
17252 		 * address and the low level
17253 		 * info of ire_prim. In other words, CGTP
17254 		 * broadcast is added to the redundant ipif.
17255 		 */
17256 		ill_t *ill_prim;
17257 		ire_t  *bcast_ire;
17258 
17259 		ill_prim = ire_prim->ire_ill;
17260 
17261 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17262 		    (void *)ire_prim, (void *)ill_prim));
17263 
17264 		bcast_ire = ire_create(
17265 		    (uchar_t *)&ire->ire_addr,
17266 		    (uchar_t *)&ip_g_all_ones,
17267 		    (uchar_t *)&ire->ire_gateway_addr,
17268 		    IRE_BROADCAST,
17269 		    ill_prim,
17270 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17271 		    ire->ire_flags | RTF_KERNEL,
17272 		    NULL,
17273 		    ipst);
17274 
17275 		/*
17276 		 * Here we assume that ire_add does head insertion so that
17277 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17278 		 */
17279 		if (bcast_ire != NULL) {
17280 			if (ire->ire_flags & RTF_SETSRC) {
17281 				bcast_ire->ire_setsrc_addr =
17282 				    ire->ire_setsrc_addr;
17283 			}
17284 			bcast_ire = ire_add(bcast_ire);
17285 			if (bcast_ire != NULL) {
17286 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17287 				    "added bcast_ire %p\n",
17288 				    (void *)bcast_ire));
17289 
17290 				ill_save_ire(ill_prim, bcast_ire);
17291 				ire_refrele(bcast_ire);
17292 			}
17293 		}
17294 		ire_refrele(ire_prim);
17295 	}
17296 }
17297 
17298 /*
17299  * IP multirouting broadcast routes handling
17300  * Remove the broadcast ire.
17301  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17302  * the destination and the gateway are broadcast addresses.
17303  * The caller has only verified that RTF_MULTIRT was set. We check
17304  * that the destination is broadcast and that the gateway is a broadcast
17305  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17306  */
17307 static void
17308 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17309 {
17310 	ASSERT(ire != NULL);
17311 
17312 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17313 		ire_t *ire_prim;
17314 
17315 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17316 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17317 		    ipst, NULL);
17318 		if (ire_prim != NULL) {
17319 			ill_t *ill_prim;
17320 			ire_t  *bcast_ire;
17321 
17322 			ill_prim = ire_prim->ire_ill;
17323 
17324 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17325 			    "ire_prim %p, ill_prim %p\n",
17326 			    (void *)ire_prim, (void *)ill_prim));
17327 
17328 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17329 			    ire->ire_gateway_addr, IRE_BROADCAST,
17330 			    ill_prim, ALL_ZONES, NULL,
17331 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17332 			    MATCH_IRE_MASK, 0, ipst, NULL);
17333 
17334 			if (bcast_ire != NULL) {
17335 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17336 				    "looked up bcast_ire %p\n",
17337 				    (void *)bcast_ire));
17338 				ill_remove_saved_ire(bcast_ire->ire_ill,
17339 				    bcast_ire);
17340 				ire_delete(bcast_ire);
17341 				ire_refrele(bcast_ire);
17342 			}
17343 			ire_refrele(ire_prim);
17344 		}
17345 	}
17346 }
17347 
17348 /*
17349  * Derive an interface id from the link layer address.
17350  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17351  */
17352 static void
17353 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17354 {
17355 	char		*addr;
17356 
17357 	/*
17358 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17359 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17360 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17361 	 * interface ID on IPv6 interfaces above links that actually have real
17362 	 * Ethernet addresses.
17363 	 */
17364 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17365 		/* Form EUI-64 like address */
17366 		addr = (char *)&v6addr->s6_addr32[2];
17367 		bcopy(ill->ill_phys_addr, addr, 3);
17368 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17369 		addr[3] = (char)0xff;
17370 		addr[4] = (char)0xfe;
17371 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17372 	}
17373 }
17374 
17375 /* ARGSUSED */
17376 static void
17377 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17378 {
17379 }
17380 
17381 typedef struct ipmp_ifcookie {
17382 	uint32_t	ic_hostid;
17383 	char		ic_ifname[LIFNAMSIZ];
17384 	char		ic_zonename[ZONENAME_MAX];
17385 } ipmp_ifcookie_t;
17386 
17387 /*
17388  * Construct a pseudo-random interface ID for the IPMP interface that's both
17389  * predictable and (almost) guaranteed to be unique.
17390  */
17391 static void
17392 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17393 {
17394 	zone_t		*zp;
17395 	uint8_t		*addr;
17396 	uchar_t		hash[16];
17397 	ulong_t 	hostid;
17398 	MD5_CTX		ctx;
17399 	ipmp_ifcookie_t	ic = { 0 };
17400 
17401 	ASSERT(IS_IPMP(ill));
17402 
17403 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17404 	ic.ic_hostid = htonl((uint32_t)hostid);
17405 
17406 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17407 
17408 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17409 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17410 		zone_rele(zp);
17411 	}
17412 
17413 	MD5Init(&ctx);
17414 	MD5Update(&ctx, &ic, sizeof (ic));
17415 	MD5Final(hash, &ctx);
17416 
17417 	/*
17418 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17419 	 */
17420 	addr = &v6addr->s6_addr8[8];
17421 	bcopy(hash + 8, addr, sizeof (uint64_t));
17422 	addr[0] &= ~0x2;				/* set local bit */
17423 }
17424 
17425 /*
17426  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17427  */
17428 static void
17429 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17430 {
17431 	phyint_t *phyi = ill->ill_phyint;
17432 
17433 	/*
17434 	 * Check PHYI_MULTI_BCAST and length of physical
17435 	 * address to determine if we use the mapping or the
17436 	 * broadcast address.
17437 	 */
17438 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17439 	    ill->ill_phys_addr_length != ETHERADDRL) {
17440 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17441 		return;
17442 	}
17443 	m_physaddr[0] = 0x33;
17444 	m_physaddr[1] = 0x33;
17445 	m_physaddr[2] = m_ip6addr[12];
17446 	m_physaddr[3] = m_ip6addr[13];
17447 	m_physaddr[4] = m_ip6addr[14];
17448 	m_physaddr[5] = m_ip6addr[15];
17449 }
17450 
17451 /*
17452  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17453  */
17454 static void
17455 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17456 {
17457 	phyint_t *phyi = ill->ill_phyint;
17458 
17459 	/*
17460 	 * Check PHYI_MULTI_BCAST and length of physical
17461 	 * address to determine if we use the mapping or the
17462 	 * broadcast address.
17463 	 */
17464 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17465 	    ill->ill_phys_addr_length != ETHERADDRL) {
17466 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17467 		return;
17468 	}
17469 	m_physaddr[0] = 0x01;
17470 	m_physaddr[1] = 0x00;
17471 	m_physaddr[2] = 0x5e;
17472 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17473 	m_physaddr[4] = m_ipaddr[2];
17474 	m_physaddr[5] = m_ipaddr[3];
17475 }
17476 
17477 /* ARGSUSED */
17478 static void
17479 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17480 {
17481 	/*
17482 	 * for the MULTI_BCAST case and other cases when we want to
17483 	 * use the link-layer broadcast address for multicast.
17484 	 */
17485 	uint8_t	*bphys_addr;
17486 	dl_unitdata_req_t *dlur;
17487 
17488 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17489 	if (ill->ill_sap_length < 0) {
17490 		bphys_addr = (uchar_t *)dlur +
17491 		    dlur->dl_dest_addr_offset;
17492 	} else  {
17493 		bphys_addr = (uchar_t *)dlur +
17494 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17495 	}
17496 
17497 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17498 }
17499 
17500 /*
17501  * Derive IPoIB interface id from the link layer address.
17502  */
17503 static void
17504 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17505 {
17506 	char		*addr;
17507 
17508 	ASSERT(ill->ill_phys_addr_length == 20);
17509 	addr = (char *)&v6addr->s6_addr32[2];
17510 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17511 	/*
17512 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17513 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17514 	 * rules. In these cases, the IBA considers these GUIDs to be in
17515 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17516 	 * required; vendors are required not to assign global EUI-64's
17517 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17518 	 * of the interface identifier. Whether the GUID is in modified
17519 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17520 	 * bit set to 1.
17521 	 */
17522 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17523 }
17524 
17525 /*
17526  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17527  * Note on mapping from multicast IP addresses to IPoIB multicast link
17528  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17529  * The format of an IPoIB multicast address is:
17530  *
17531  *  4 byte QPN      Scope Sign.  Pkey
17532  * +--------------------------------------------+
17533  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17534  * +--------------------------------------------+
17535  *
17536  * The Scope and Pkey components are properties of the IBA port and
17537  * network interface. They can be ascertained from the broadcast address.
17538  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17539  */
17540 static void
17541 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17542 {
17543 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17544 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17545 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17546 	uint8_t	*bphys_addr;
17547 	dl_unitdata_req_t *dlur;
17548 
17549 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17550 
17551 	/*
17552 	 * RFC 4391: IPv4 MGID is 28-bit long.
17553 	 */
17554 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17555 	m_physaddr[17] = m_ipaddr[1];
17556 	m_physaddr[18] = m_ipaddr[2];
17557 	m_physaddr[19] = m_ipaddr[3];
17558 
17559 
17560 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17561 	if (ill->ill_sap_length < 0) {
17562 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17563 	} else  {
17564 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17565 		    ill->ill_sap_length;
17566 	}
17567 	/*
17568 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17569 	 */
17570 	m_physaddr[5] = bphys_addr[5];
17571 	m_physaddr[8] = bphys_addr[8];
17572 	m_physaddr[9] = bphys_addr[9];
17573 }
17574 
17575 static void
17576 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17577 {
17578 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17579 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17580 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17581 	uint8_t	*bphys_addr;
17582 	dl_unitdata_req_t *dlur;
17583 
17584 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17585 
17586 	/*
17587 	 * RFC 4391: IPv4 MGID is 80-bit long.
17588 	 */
17589 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17590 
17591 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17592 	if (ill->ill_sap_length < 0) {
17593 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17594 	} else  {
17595 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17596 		    ill->ill_sap_length;
17597 	}
17598 	/*
17599 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17600 	 */
17601 	m_physaddr[5] = bphys_addr[5];
17602 	m_physaddr[8] = bphys_addr[8];
17603 	m_physaddr[9] = bphys_addr[9];
17604 }
17605 
17606 /*
17607  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17608  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17609  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17610  * of RFC4213.
17611  */
17612 static void
17613 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17614 {
17615 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17616 	v6addr->s6_addr32[2] = 0;
17617 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17618 }
17619 
17620 /*
17621  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17622  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17623  * id.
17624  */
17625 static void
17626 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17627 {
17628 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17629 
17630 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17631 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17632 }
17633 
17634 static void
17635 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17636 {
17637 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17638 }
17639 
17640 static void
17641 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17642 {
17643 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17644 }
17645 
17646 static void
17647 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17648 {
17649 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17650 }
17651 
17652 static void
17653 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17654 {
17655 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17656 }
17657 
17658 /*
17659  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17660  * Returns an held ill, or NULL.
17661  */
17662 ill_t *
17663 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17664     ip_stack_t *ipst)
17665 {
17666 	ill_t	*ill;
17667 	ipif_t	*ipif;
17668 
17669 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17670 	if (ill == NULL)
17671 		return (NULL);
17672 
17673 	mutex_enter(&ill->ill_lock);
17674 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17675 		if (IPIF_IS_CONDEMNED(ipif))
17676 			continue;
17677 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17678 		    ipif->ipif_zoneid != ALL_ZONES)
17679 			continue;
17680 
17681 		mutex_exit(&ill->ill_lock);
17682 		return (ill);
17683 	}
17684 	mutex_exit(&ill->ill_lock);
17685 	ill_refrele(ill);
17686 	return (NULL);
17687 }
17688 
17689 /*
17690  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17691  * If a pointer to an ipif_t is returned then the caller will need to do
17692  * an ill_refrele().
17693  */
17694 ipif_t *
17695 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17696     ip_stack_t *ipst)
17697 {
17698 	ipif_t *ipif;
17699 	ill_t *ill;
17700 
17701 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17702 	if (ill == NULL)
17703 		return (NULL);
17704 
17705 	mutex_enter(&ill->ill_lock);
17706 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17707 		mutex_exit(&ill->ill_lock);
17708 		ill_refrele(ill);
17709 		return (NULL);
17710 	}
17711 
17712 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17713 		if (!IPIF_CAN_LOOKUP(ipif))
17714 			continue;
17715 		if (lifidx == ipif->ipif_id) {
17716 			ipif_refhold_locked(ipif);
17717 			break;
17718 		}
17719 	}
17720 
17721 	mutex_exit(&ill->ill_lock);
17722 	ill_refrele(ill);
17723 	return (ipif);
17724 }
17725 
17726 /*
17727  * Set ill_inputfn based on the current know state.
17728  * This needs to be called when any of the factors taken into
17729  * account changes.
17730  */
17731 void
17732 ill_set_inputfn(ill_t *ill)
17733 {
17734 	ip_stack_t	*ipst = ill->ill_ipst;
17735 
17736 	if (ill->ill_isv6) {
17737 		if (is_system_labeled())
17738 			ill->ill_inputfn = ill_input_full_v6;
17739 		else
17740 			ill->ill_inputfn = ill_input_short_v6;
17741 	} else {
17742 		if (is_system_labeled())
17743 			ill->ill_inputfn = ill_input_full_v4;
17744 		else if (ill->ill_dhcpinit != 0)
17745 			ill->ill_inputfn = ill_input_full_v4;
17746 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17747 		    != NULL)
17748 			ill->ill_inputfn = ill_input_full_v4;
17749 		else if (ipst->ips_ip_cgtp_filter &&
17750 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17751 			ill->ill_inputfn = ill_input_full_v4;
17752 		else
17753 			ill->ill_inputfn = ill_input_short_v4;
17754 	}
17755 }
17756 
17757 /*
17758  * Re-evaluate ill_inputfn for all the IPv4 ills.
17759  * Used when RSVP and CGTP comes and goes.
17760  */
17761 void
17762 ill_set_inputfn_all(ip_stack_t *ipst)
17763 {
17764 	ill_walk_context_t	ctx;
17765 	ill_t			*ill;
17766 
17767 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17768 	ill = ILL_START_WALK_V4(&ctx, ipst);
17769 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17770 		ill_set_inputfn(ill);
17771 
17772 	rw_exit(&ipst->ips_ill_g_lock);
17773 }
17774 
17775 /*
17776  * Set the physical address information for `ill' to the contents of the
17777  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17778  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17779  * EINPROGRESS will be returned.
17780  */
17781 int
17782 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17783 {
17784 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17785 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17786 
17787 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17788 
17789 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17790 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17791 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17792 		/* Changing DL_IPV6_TOKEN is not yet supported */
17793 		return (0);
17794 	}
17795 
17796 	/*
17797 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17798 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17799 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17800 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17801 	 */
17802 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17803 		freemsg(mp);
17804 		return (ENOMEM);
17805 	}
17806 
17807 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17808 
17809 	/*
17810 	 * Since we'll only do a logical down, we can't rely on ipif_down
17811 	 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17812 	 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17813 	 * case, to quiesce ire's and nce's for ill_is_quiescent.
17814 	 */
17815 	mutex_enter(&ill->ill_lock);
17816 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17817 	/* no more ire/nce addition allowed */
17818 	mutex_exit(&ill->ill_lock);
17819 
17820 	/*
17821 	 * If we can quiesce the ill, then set the address.  If not, then
17822 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17823 	 */
17824 	ill_down_ipifs(ill, B_TRUE);
17825 	mutex_enter(&ill->ill_lock);
17826 	if (!ill_is_quiescent(ill)) {
17827 		/* call cannot fail since `conn_t *' argument is NULL */
17828 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17829 		    mp, ILL_DOWN);
17830 		mutex_exit(&ill->ill_lock);
17831 		return (EINPROGRESS);
17832 	}
17833 	mutex_exit(&ill->ill_lock);
17834 
17835 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17836 	return (0);
17837 }
17838 
17839 /*
17840  * When the allowed-ips link property is set on the datalink, IP receives a
17841  * DL_NOTE_ALLOWED_IPS notification that is processed in ill_set_allowed_ips()
17842  * to initialize the ill_allowed_ips[] array in the ill_t. This array is then
17843  * used to vet addresses passed to ip_sioctl_addr() and to ensure that the
17844  * only IP addresses configured on the ill_t are those in the ill_allowed_ips[]
17845  * array.
17846  */
17847 void
17848 ill_set_allowed_ips(ill_t *ill, mblk_t *mp)
17849 {
17850 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17851 	dl_notify_ind_t	*dlip = (dl_notify_ind_t *)mp->b_rptr;
17852 	mac_protect_t *mrp;
17853 	int i;
17854 
17855 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17856 	mrp = (mac_protect_t *)&dlip[1];
17857 
17858 	if (mrp->mp_ipaddrcnt == 0) { /* reset allowed-ips */
17859 		kmem_free(ill->ill_allowed_ips,
17860 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17861 		ill->ill_allowed_ips_cnt = 0;
17862 		ill->ill_allowed_ips = NULL;
17863 		mutex_enter(&ill->ill_phyint->phyint_lock);
17864 		ill->ill_phyint->phyint_flags &= ~PHYI_L3PROTECT;
17865 		mutex_exit(&ill->ill_phyint->phyint_lock);
17866 		return;
17867 	}
17868 
17869 	if (ill->ill_allowed_ips != NULL) {
17870 		kmem_free(ill->ill_allowed_ips,
17871 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17872 	}
17873 	ill->ill_allowed_ips_cnt = mrp->mp_ipaddrcnt;
17874 	ill->ill_allowed_ips = kmem_alloc(
17875 	    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t), KM_SLEEP);
17876 	for (i = 0; i < mrp->mp_ipaddrcnt;  i++)
17877 		ill->ill_allowed_ips[i] = mrp->mp_ipaddrs[i].ip_addr;
17878 
17879 	mutex_enter(&ill->ill_phyint->phyint_lock);
17880 	ill->ill_phyint->phyint_flags |= PHYI_L3PROTECT;
17881 	mutex_exit(&ill->ill_phyint->phyint_lock);
17882 }
17883 
17884 /*
17885  * Once the ill associated with `q' has quiesced, set its physical address
17886  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17887  * are passed (linked by b_cont), since we sometimes need to save two distinct
17888  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17889  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17890  * is quiesced, we know any stale nce's with the old address information have
17891  * already been removed, so we don't need to call nce_flush().
17892  */
17893 /* ARGSUSED */
17894 static void
17895 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17896 {
17897 	ill_t		*ill = q->q_ptr;
17898 	mblk_t		*addrmp2 = unlinkb(addrmp);
17899 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17900 	uint_t		addrlen, addroff;
17901 	int		status;
17902 
17903 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17904 
17905 	addroff	= dlindp->dl_addr_offset;
17906 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17907 
17908 	switch (dlindp->dl_data) {
17909 	case DL_IPV6_LINK_LAYER_ADDR:
17910 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17911 		freemsg(addrmp2);
17912 		break;
17913 
17914 	case DL_CURR_DEST_ADDR:
17915 		freemsg(ill->ill_dest_addr_mp);
17916 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17917 		ill->ill_dest_addr_mp = addrmp;
17918 		if (ill->ill_isv6) {
17919 			ill_setdesttoken(ill);
17920 			ipif_setdestlinklocal(ill->ill_ipif);
17921 		}
17922 		freemsg(addrmp2);
17923 		break;
17924 
17925 	case DL_CURR_PHYS_ADDR:
17926 		freemsg(ill->ill_phys_addr_mp);
17927 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17928 		ill->ill_phys_addr_mp = addrmp;
17929 		ill->ill_phys_addr_length = addrlen;
17930 		if (ill->ill_isv6)
17931 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17932 		else
17933 			freemsg(addrmp2);
17934 		if (ill->ill_isv6) {
17935 			ill_setdefaulttoken(ill);
17936 			ipif_setlinklocal(ill->ill_ipif);
17937 		}
17938 		break;
17939 	default:
17940 		ASSERT(0);
17941 	}
17942 
17943 	/*
17944 	 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17945 	 * as we bring the ipifs up again.
17946 	 */
17947 	mutex_enter(&ill->ill_lock);
17948 	ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17949 	mutex_exit(&ill->ill_lock);
17950 	/*
17951 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17952 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17953 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17954 	 * brought up.
17955 	 */
17956 	status = ill_up_ipifs(ill, q, addrmp);
17957 	if (status != EINPROGRESS)
17958 		ipsq_current_finish(ipsq);
17959 }
17960 
17961 /*
17962  * Helper routine for setting the ill_nd_lla fields.
17963  */
17964 void
17965 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17966 {
17967 	freemsg(ill->ill_nd_lla_mp);
17968 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17969 	ill->ill_nd_lla_mp = ndmp;
17970 	ill->ill_nd_lla_len = addrlen;
17971 }
17972 
17973 /*
17974  * Replumb the ill.
17975  */
17976 int
17977 ill_replumb(ill_t *ill, mblk_t *mp)
17978 {
17979 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17980 
17981 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17982 
17983 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17984 
17985 	/*
17986 	 * If we can quiesce the ill, then continue.  If not, then
17987 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17988 	 */
17989 	ill_down_ipifs(ill, B_FALSE);
17990 
17991 	mutex_enter(&ill->ill_lock);
17992 	if (!ill_is_quiescent(ill)) {
17993 		/* call cannot fail since `conn_t *' argument is NULL */
17994 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17995 		    mp, ILL_DOWN);
17996 		mutex_exit(&ill->ill_lock);
17997 		return (EINPROGRESS);
17998 	}
17999 	mutex_exit(&ill->ill_lock);
18000 
18001 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
18002 	return (0);
18003 }
18004 
18005 /* ARGSUSED */
18006 static void
18007 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
18008 {
18009 	ill_t *ill = q->q_ptr;
18010 	int err;
18011 	conn_t *connp = NULL;
18012 
18013 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18014 	freemsg(ill->ill_replumb_mp);
18015 	ill->ill_replumb_mp = copyb(mp);
18016 
18017 	if (ill->ill_replumb_mp == NULL) {
18018 		/* out of memory */
18019 		ipsq_current_finish(ipsq);
18020 		return;
18021 	}
18022 
18023 	mutex_enter(&ill->ill_lock);
18024 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
18025 	    ill->ill_rq, ill->ill_replumb_mp, 0);
18026 	mutex_exit(&ill->ill_lock);
18027 
18028 	if (!ill->ill_up_ipifs) {
18029 		/* already closing */
18030 		ipsq_current_finish(ipsq);
18031 		return;
18032 	}
18033 	ill->ill_replumbing = 1;
18034 	err = ill_down_ipifs_tail(ill);
18035 
18036 	/*
18037 	 * Successfully quiesced and brought down the interface, now we send
18038 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
18039 	 * DL_NOTE_REPLUMB message.
18040 	 */
18041 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
18042 	    DL_NOTIFY_CONF);
18043 	ASSERT(mp != NULL);
18044 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
18045 	    DL_NOTE_REPLUMB_DONE;
18046 	ill_dlpi_send(ill, mp);
18047 
18048 	/*
18049 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
18050 	 * streams have to be unbound. When all the DLPI exchanges are done,
18051 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
18052 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
18053 	 * arp_bringup_done().
18054 	 */
18055 	ASSERT(ill->ill_replumb_mp != NULL);
18056 	if (err == EINPROGRESS)
18057 		return;
18058 	else
18059 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
18060 	ASSERT(connp == NULL);
18061 	if (err == 0 && ill->ill_replumb_mp != NULL &&
18062 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
18063 		return;
18064 	}
18065 	ipsq_current_finish(ipsq);
18066 }
18067 
18068 /*
18069  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
18070  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
18071  * as per the ioctl.  On failure, an errno is returned.
18072  */
18073 static int
18074 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
18075 {
18076 	int rval;
18077 	struct strioctl iocb;
18078 
18079 	iocb.ic_cmd = cmd;
18080 	iocb.ic_timout = 15;
18081 	iocb.ic_len = bufsize;
18082 	iocb.ic_dp = buf;
18083 
18084 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
18085 }
18086 
18087 /*
18088  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
18089  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
18090  */
18091 static int
18092 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
18093     uint_t *bufsizep, cred_t *cr)
18094 {
18095 	int err;
18096 	struct lifnum lifn;
18097 
18098 	bzero(&lifn, sizeof (lifn));
18099 	lifn.lifn_family = af;
18100 	lifn.lifn_flags = LIFC_UNDER_IPMP;
18101 
18102 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
18103 		return (err);
18104 
18105 	/*
18106 	 * Pad the interface count to account for additional interfaces that
18107 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18108 	 */
18109 	lifn.lifn_count += 4;
18110 	bzero(lifcp, sizeof (*lifcp));
18111 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
18112 	lifcp->lifc_family = af;
18113 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18114 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18115 
18116 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18117 	if (err != 0) {
18118 		kmem_free(lifcp->lifc_buf, *bufsizep);
18119 		return (err);
18120 	}
18121 
18122 	return (0);
18123 }
18124 
18125 /*
18126  * Helper for ip_interface_cleanup() that removes the loopback interface.
18127  */
18128 static void
18129 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18130 {
18131 	int err;
18132 	struct lifreq lifr;
18133 
18134 	bzero(&lifr, sizeof (lifr));
18135 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
18136 
18137 	/*
18138 	 * Attempt to remove the interface.  It may legitimately not exist
18139 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18140 	 */
18141 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18142 	if (err != 0 && err != ENXIO) {
18143 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18144 		    "error %d\n", isv6 ? "v6" : "v4", err));
18145 	}
18146 }
18147 
18148 /*
18149  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18150  * groups and that IPMP data addresses are down.  These conditions must be met
18151  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18152  */
18153 static void
18154 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18155 {
18156 	int af = isv6 ? AF_INET6 : AF_INET;
18157 	int i, nifs;
18158 	int err;
18159 	uint_t bufsize;
18160 	uint_t lifrsize = sizeof (struct lifreq);
18161 	struct lifconf lifc;
18162 	struct lifreq *lifrp;
18163 
18164 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18165 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18166 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
18167 		return;
18168 	}
18169 
18170 	nifs = lifc.lifc_len / lifrsize;
18171 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18172 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18173 		if (err != 0) {
18174 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18175 			    "flags: error %d", lifrp->lifr_name, err);
18176 			continue;
18177 		}
18178 
18179 		if (lifrp->lifr_flags & IFF_IPMP) {
18180 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18181 				continue;
18182 
18183 			lifrp->lifr_flags &= ~IFF_UP;
18184 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18185 			if (err != 0) {
18186 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18187 				    "bring down (error %d); IPMP interface may "
18188 				    "not be shutdown", lifrp->lifr_name, err);
18189 			}
18190 
18191 			/*
18192 			 * Check if IFF_DUPLICATE is still set -- and if so,
18193 			 * reset the address to clear it.
18194 			 */
18195 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18196 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18197 				continue;
18198 
18199 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18200 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18201 			    lifrp, lifrsize, cr)) != 0) {
18202 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18203 				    "reset DAD (error %d); IPMP interface may "
18204 				    "not be shutdown", lifrp->lifr_name, err);
18205 			}
18206 			continue;
18207 		}
18208 
18209 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18210 			lifrp->lifr_groupname[0] = '\0';
18211 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18212 			    lifrsize, cr)) != 0) {
18213 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18214 				    "leave IPMP group (error %d); associated "
18215 				    "IPMP interface may not be shutdown",
18216 				    lifrp->lifr_name, err);
18217 				continue;
18218 			}
18219 		}
18220 	}
18221 
18222 	kmem_free(lifc.lifc_buf, bufsize);
18223 }
18224 
18225 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
18226 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
18227 
18228 /*
18229  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18230  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18231  * when the user-level processes in the zone are killed and the latter are
18232  * cleaned up by str_stack_shutdown().
18233  */
18234 void
18235 ip_interface_cleanup(ip_stack_t *ipst)
18236 {
18237 	ldi_handle_t	lh;
18238 	ldi_ident_t	li;
18239 	cred_t		*cr;
18240 	int		err;
18241 	int		i;
18242 	char		*devs[] = { UDP6DEV, UDPDEV };
18243 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18244 
18245 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18246 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18247 		    " error %d", err);
18248 		return;
18249 	}
18250 
18251 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18252 	ASSERT(cr != NULL);
18253 
18254 	/*
18255 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18256 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18257 	 * the loop.)
18258 	 */
18259 	for (i = 0; i < 2; i++) {
18260 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18261 		if (err != 0) {
18262 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18263 			    " error %d", devs[i], err);
18264 			continue;
18265 		}
18266 
18267 		ip_loopback_removeif(lh, i == 0, cr);
18268 		ip_ipmp_cleanup(lh, i == 0, cr);
18269 
18270 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18271 	}
18272 
18273 	ldi_ident_release(li);
18274 	crfree(cr);
18275 }
18276 
18277 /*
18278  * This needs to be in-sync with nic_event_t definition
18279  */
18280 static const char *
18281 ill_hook_event2str(nic_event_t event)
18282 {
18283 	switch (event) {
18284 	case NE_PLUMB:
18285 		return ("PLUMB");
18286 	case NE_UNPLUMB:
18287 		return ("UNPLUMB");
18288 	case NE_UP:
18289 		return ("UP");
18290 	case NE_DOWN:
18291 		return ("DOWN");
18292 	case NE_ADDRESS_CHANGE:
18293 		return ("ADDRESS_CHANGE");
18294 	case NE_LIF_UP:
18295 		return ("LIF_UP");
18296 	case NE_LIF_DOWN:
18297 		return ("LIF_DOWN");
18298 	case NE_IFINDEX_CHANGE:
18299 		return ("IFINDEX_CHANGE");
18300 	default:
18301 		return ("UNKNOWN");
18302 	}
18303 }
18304 
18305 void
18306 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18307     nic_event_data_t data, size_t datalen)
18308 {
18309 	ip_stack_t		*ipst = ill->ill_ipst;
18310 	hook_nic_event_int_t	*info;
18311 	const char		*str = NULL;
18312 
18313 	/* create a new nic event info */
18314 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18315 		goto fail;
18316 
18317 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18318 	info->hnei_event.hne_lif = lif;
18319 	info->hnei_event.hne_event = event;
18320 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18321 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18322 	info->hnei_event.hne_data = NULL;
18323 	info->hnei_event.hne_datalen = 0;
18324 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18325 
18326 	if (data != NULL && datalen != 0) {
18327 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18328 		if (info->hnei_event.hne_data == NULL)
18329 			goto fail;
18330 		bcopy(data, info->hnei_event.hne_data, datalen);
18331 		info->hnei_event.hne_datalen = datalen;
18332 	}
18333 
18334 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18335 	    DDI_NOSLEEP) == DDI_SUCCESS)
18336 		return;
18337 
18338 fail:
18339 	if (info != NULL) {
18340 		if (info->hnei_event.hne_data != NULL) {
18341 			kmem_free(info->hnei_event.hne_data,
18342 			    info->hnei_event.hne_datalen);
18343 		}
18344 		kmem_free(info, sizeof (hook_nic_event_t));
18345 	}
18346 	str = ill_hook_event2str(event);
18347 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18348 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18349 }
18350 
18351 static int
18352 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18353 {
18354 	int		err = 0;
18355 	const in_addr_t	*addr = NULL;
18356 	nce_t		*nce = NULL;
18357 	ill_t		*ill = ipif->ipif_ill;
18358 	ill_t		*bound_ill;
18359 	boolean_t	added_ipif = B_FALSE;
18360 	uint16_t	state;
18361 	uint16_t	flags;
18362 
18363 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18364 	    ill_t *, ill, ipif_t *, ipif);
18365 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18366 		addr = &ipif->ipif_lcl_addr;
18367 	}
18368 
18369 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18370 		if (res_act != Res_act_initial)
18371 			return (EINVAL);
18372 	}
18373 
18374 	if (addr != NULL) {
18375 		ipmp_illgrp_t	*illg = ill->ill_grp;
18376 
18377 		/* add unicast nce for the local addr */
18378 
18379 		if (IS_IPMP(ill)) {
18380 			/*
18381 			 * If we're here via ipif_up(), then the ipif
18382 			 * won't be bound yet -- add it to the group,
18383 			 * which will bind it if possible. (We would
18384 			 * add it in ipif_up(), but deleting on failure
18385 			 * there is gruesome.)  If we're here via
18386 			 * ipmp_ill_bind_ipif(), then the ipif has
18387 			 * already been added to the group and we
18388 			 * just need to use the binding.
18389 			 */
18390 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18391 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18392 				if (bound_ill == NULL) {
18393 					/*
18394 					 * We couldn't bind the ipif to an ill
18395 					 * yet, so we have nothing to publish.
18396 					 * Mark the address as ready and return.
18397 					 */
18398 					ipif->ipif_addr_ready = 1;
18399 					return (0);
18400 				}
18401 				added_ipif = B_TRUE;
18402 			}
18403 		} else {
18404 			bound_ill = ill;
18405 		}
18406 
18407 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18408 		    NCE_F_NONUD);
18409 		/*
18410 		 * If this is an initial bring-up (or the ipif was never
18411 		 * completely brought up), do DAD.  Otherwise, we're here
18412 		 * because IPMP has rebound an address to this ill: send
18413 		 * unsolicited advertisements (ARP announcements) to
18414 		 * inform others.
18415 		 */
18416 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18417 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18418 		} else {
18419 			state = ND_REACHABLE;
18420 			flags |= NCE_F_UNSOL_ADV;
18421 		}
18422 
18423 retry:
18424 		err = nce_lookup_then_add_v4(ill,
18425 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18426 		    addr, flags, state, &nce);
18427 
18428 		/*
18429 		 * note that we may encounter EEXIST if we are moving
18430 		 * the nce as a result of a rebind operation.
18431 		 */
18432 		switch (err) {
18433 		case 0:
18434 			ipif->ipif_added_nce = 1;
18435 			nce->nce_ipif_cnt++;
18436 			break;
18437 		case EEXIST:
18438 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18439 			    ill->ill_name));
18440 			if (!NCE_MYADDR(nce->nce_common)) {
18441 				/*
18442 				 * A leftover nce from before this address
18443 				 * existed
18444 				 */
18445 				ncec_delete(nce->nce_common);
18446 				nce_refrele(nce);
18447 				nce = NULL;
18448 				goto retry;
18449 			}
18450 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18451 				nce_refrele(nce);
18452 				nce = NULL;
18453 				ip1dbg(("ipif_arp_up: NCE already exists "
18454 				    "for %s:%u\n", ill->ill_name,
18455 				    ipif->ipif_id));
18456 				goto arp_up_done;
18457 			}
18458 			/*
18459 			 * Duplicate local addresses are permissible for
18460 			 * IPIF_POINTOPOINT interfaces which will get marked
18461 			 * IPIF_UNNUMBERED later in
18462 			 * ip_addr_availability_check().
18463 			 *
18464 			 * The nce_ipif_cnt field tracks the number of
18465 			 * ipifs that have nce_addr as their local address.
18466 			 */
18467 			ipif->ipif_addr_ready = 1;
18468 			ipif->ipif_added_nce = 1;
18469 			nce->nce_ipif_cnt++;
18470 			err = 0;
18471 			break;
18472 		default:
18473 			ASSERT(nce == NULL);
18474 			goto arp_up_done;
18475 		}
18476 		if (arp_no_defense) {
18477 			if ((ipif->ipif_flags & IPIF_UP) &&
18478 			    !ipif->ipif_addr_ready)
18479 				ipif_up_notify(ipif);
18480 			ipif->ipif_addr_ready = 1;
18481 		}
18482 	} else {
18483 		/* zero address. nothing to publish */
18484 		ipif->ipif_addr_ready = 1;
18485 	}
18486 	if (nce != NULL)
18487 		nce_refrele(nce);
18488 arp_up_done:
18489 	if (added_ipif && err != 0)
18490 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18491 	return (err);
18492 }
18493 
18494 int
18495 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18496 {
18497 	int 		err = 0;
18498 	ill_t 		*ill = ipif->ipif_ill;
18499 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18500 
18501 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18502 	    ill_t *, ill, ipif_t *, ipif);
18503 
18504 	/*
18505 	 * need to bring up ARP or setup mcast mapping only
18506 	 * when the first interface is coming UP.
18507 	 */
18508 	first_interface = (ill->ill_ipif_up_count == 0 &&
18509 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18510 
18511 	if (res_act == Res_act_initial && first_interface) {
18512 		/*
18513 		 * Send ATTACH + BIND
18514 		 */
18515 		err = arp_ll_up(ill);
18516 		if (err != EINPROGRESS && err != 0)
18517 			return (err);
18518 
18519 		/*
18520 		 * Add NCE for local address. Start DAD.
18521 		 * we'll wait to hear that DAD has finished
18522 		 * before using the interface.
18523 		 */
18524 		if (err == EINPROGRESS)
18525 			wait_for_dlpi = B_TRUE;
18526 	}
18527 
18528 	if (!wait_for_dlpi)
18529 		(void) ipif_arp_up_done_tail(ipif, res_act);
18530 
18531 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18532 }
18533 
18534 /*
18535  * Finish processing of "arp_up" after all the DLPI message
18536  * exchanges have completed between arp and the driver.
18537  */
18538 void
18539 arp_bringup_done(ill_t *ill, int err)
18540 {
18541 	mblk_t	*mp1;
18542 	ipif_t  *ipif;
18543 	conn_t *connp = NULL;
18544 	ipsq_t	*ipsq;
18545 	queue_t *q;
18546 
18547 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18548 
18549 	ASSERT(IAM_WRITER_ILL(ill));
18550 
18551 	ipsq = ill->ill_phyint->phyint_ipsq;
18552 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18553 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18554 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18555 	if (mp1 == NULL) /* bringup was aborted by the user */
18556 		return;
18557 
18558 	/*
18559 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18560 	 * must have an associated conn_t.  Otherwise, we're bringing this
18561 	 * interface back up as part of handling an asynchronous event (e.g.,
18562 	 * physical address change).
18563 	 */
18564 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18565 		ASSERT(connp != NULL);
18566 		q = CONNP_TO_WQ(connp);
18567 	} else {
18568 		ASSERT(connp == NULL);
18569 		q = ill->ill_rq;
18570 	}
18571 	if (err == 0) {
18572 		if (ipif->ipif_isv6) {
18573 			if ((err = ipif_up_done_v6(ipif)) != 0)
18574 				ip0dbg(("arp_bringup_done: init failed\n"));
18575 		} else {
18576 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18577 			if (err != 0 ||
18578 			    (err = ipif_up_done(ipif)) != 0) {
18579 				ip0dbg(("arp_bringup_done: "
18580 				    "init failed err %x\n", err));
18581 				(void) ipif_arp_down(ipif);
18582 			}
18583 
18584 		}
18585 	} else {
18586 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18587 	}
18588 
18589 	if ((err == 0) && (ill->ill_up_ipifs)) {
18590 		err = ill_up_ipifs(ill, q, mp1);
18591 		if (err == EINPROGRESS)
18592 			return;
18593 	}
18594 
18595 	/*
18596 	 * If we have a moved ipif to bring up, and everything has succeeded
18597 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18598 	 * down -- the admin can try to bring it up by hand if need be.
18599 	 */
18600 	if (ill->ill_move_ipif != NULL) {
18601 		ipif = ill->ill_move_ipif;
18602 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18603 		    ipif->ipif_ill->ill_name));
18604 		ill->ill_move_ipif = NULL;
18605 		if (err == 0) {
18606 			err = ipif_up(ipif, q, mp1);
18607 			if (err == EINPROGRESS)
18608 				return;
18609 		}
18610 	}
18611 
18612 	/*
18613 	 * The operation must complete without EINPROGRESS since
18614 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18615 	 * Otherwise, the operation will be stuck forever in the ipsq.
18616 	 */
18617 	ASSERT(err != EINPROGRESS);
18618 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18619 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18620 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18621 		    ill_t *, ill, ipif_t *, ipif);
18622 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18623 	} else {
18624 		ipsq_current_finish(ipsq);
18625 	}
18626 }
18627 
18628 /*
18629  * Finish processing of arp replumb after all the DLPI message
18630  * exchanges have completed between arp and the driver.
18631  */
18632 void
18633 arp_replumb_done(ill_t *ill, int err)
18634 {
18635 	mblk_t	*mp1;
18636 	ipif_t  *ipif;
18637 	conn_t *connp = NULL;
18638 	ipsq_t	*ipsq;
18639 	queue_t *q;
18640 
18641 	ASSERT(IAM_WRITER_ILL(ill));
18642 
18643 	ipsq = ill->ill_phyint->phyint_ipsq;
18644 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18645 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18646 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18647 	if (mp1 == NULL) {
18648 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18649 		    ipsq->ipsq_xop->ipx_current_ioctl));
18650 		/* bringup was aborted by the user */
18651 		return;
18652 	}
18653 	/*
18654 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18655 	 * must have an associated conn_t.  Otherwise, we're bringing this
18656 	 * interface back up as part of handling an asynchronous event (e.g.,
18657 	 * physical address change).
18658 	 */
18659 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18660 		ASSERT(connp != NULL);
18661 		q = CONNP_TO_WQ(connp);
18662 	} else {
18663 		ASSERT(connp == NULL);
18664 		q = ill->ill_rq;
18665 	}
18666 	if ((err == 0) && (ill->ill_up_ipifs)) {
18667 		err = ill_up_ipifs(ill, q, mp1);
18668 		if (err == EINPROGRESS)
18669 			return;
18670 	}
18671 	/*
18672 	 * The operation must complete without EINPROGRESS since
18673 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18674 	 * Otherwise, the operation will be stuck forever in the ipsq.
18675 	 */
18676 	ASSERT(err != EINPROGRESS);
18677 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18678 		DTRACE_PROBE4(ipif__ioctl, char *,
18679 		    "arp_replumb_done finish",
18680 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18681 		    ill_t *, ill, ipif_t *, ipif);
18682 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18683 	} else {
18684 		ipsq_current_finish(ipsq);
18685 	}
18686 }
18687 
18688 void
18689 ipif_up_notify(ipif_t *ipif)
18690 {
18691 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18692 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18693 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18694 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18695 	    NE_LIF_UP, NULL, 0);
18696 }
18697 
18698 /*
18699  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18700  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18701  * TPI end points with STREAMS modules pushed above.  This is assured by not
18702  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18703  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18704  * while unwinding from the ispq and that could be a thread from the bottom.
18705  */
18706 /* ARGSUSED */
18707 int
18708 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18709     ip_ioctl_cmd_t *ipip, void *arg)
18710 {
18711 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18712 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18713 	int ret = 0;
18714 	int i;
18715 	size_t size;
18716 	ip_stack_t *ipst;
18717 	zoneid_t zoneid;
18718 	ilb_stack_t *ilbs;
18719 
18720 	ipst = CONNQ_TO_IPST(q);
18721 	ilbs = ipst->ips_netstack->netstack_ilb;
18722 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18723 
18724 	switch (command) {
18725 	case ILB_CREATE_RULE: {
18726 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18727 
18728 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18729 			ret = EINVAL;
18730 			break;
18731 		}
18732 
18733 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18734 		break;
18735 	}
18736 	case ILB_DESTROY_RULE:
18737 	case ILB_ENABLE_RULE:
18738 	case ILB_DISABLE_RULE: {
18739 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18740 
18741 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18742 			ret = EINVAL;
18743 			break;
18744 		}
18745 
18746 		if (cmd->flags & ILB_RULE_ALLRULES) {
18747 			if (command == ILB_DESTROY_RULE) {
18748 				ilb_rule_del_all(ilbs, zoneid);
18749 				break;
18750 			} else if (command == ILB_ENABLE_RULE) {
18751 				ilb_rule_enable_all(ilbs, zoneid);
18752 				break;
18753 			} else if (command == ILB_DISABLE_RULE) {
18754 				ilb_rule_disable_all(ilbs, zoneid);
18755 				break;
18756 			}
18757 		} else {
18758 			if (command == ILB_DESTROY_RULE) {
18759 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18760 			} else if (command == ILB_ENABLE_RULE) {
18761 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18762 				    NULL);
18763 			} else if (command == ILB_DISABLE_RULE) {
18764 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18765 				    NULL);
18766 			}
18767 		}
18768 		break;
18769 	}
18770 	case ILB_NUM_RULES: {
18771 		ilb_num_rules_cmd_t *cmd;
18772 
18773 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18774 			ret = EINVAL;
18775 			break;
18776 		}
18777 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18778 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18779 		break;
18780 	}
18781 	case ILB_RULE_NAMES: {
18782 		ilb_rule_names_cmd_t *cmd;
18783 
18784 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18785 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18786 		    cmd->num_names == 0) {
18787 			ret = EINVAL;
18788 			break;
18789 		}
18790 		size = cmd->num_names * ILB_RULE_NAMESZ;
18791 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18792 		    size != cmd_mp->b_wptr) {
18793 			ret = EINVAL;
18794 			break;
18795 		}
18796 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18797 		break;
18798 	}
18799 	case ILB_NUM_SERVERS: {
18800 		ilb_num_servers_cmd_t *cmd;
18801 
18802 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18803 			ret = EINVAL;
18804 			break;
18805 		}
18806 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18807 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18808 		    &(cmd->num));
18809 		break;
18810 	}
18811 	case ILB_LIST_RULE: {
18812 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18813 
18814 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18815 			ret = EINVAL;
18816 			break;
18817 		}
18818 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18819 		break;
18820 	}
18821 	case ILB_LIST_SERVERS: {
18822 		ilb_servers_info_cmd_t *cmd;
18823 
18824 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18825 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18826 		    cmd->num_servers == 0) {
18827 			ret = EINVAL;
18828 			break;
18829 		}
18830 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18831 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18832 		    size != cmd_mp->b_wptr) {
18833 			ret = EINVAL;
18834 			break;
18835 		}
18836 
18837 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18838 		    &cmd->num_servers);
18839 		break;
18840 	}
18841 	case ILB_ADD_SERVERS: {
18842 		ilb_servers_info_cmd_t *cmd;
18843 		ilb_rule_t *rule;
18844 
18845 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18846 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18847 			ret = EINVAL;
18848 			break;
18849 		}
18850 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18851 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18852 		    size != cmd_mp->b_wptr) {
18853 			ret = EINVAL;
18854 			break;
18855 		}
18856 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18857 		if (rule == NULL) {
18858 			ASSERT(ret != 0);
18859 			break;
18860 		}
18861 		for (i = 0; i < cmd->num_servers; i++) {
18862 			ilb_server_info_t *s;
18863 
18864 			s = &cmd->servers[i];
18865 			s->err = ilb_server_add(ilbs, rule, s);
18866 		}
18867 		ILB_RULE_REFRELE(rule);
18868 		break;
18869 	}
18870 	case ILB_DEL_SERVERS:
18871 	case ILB_ENABLE_SERVERS:
18872 	case ILB_DISABLE_SERVERS: {
18873 		ilb_servers_cmd_t *cmd;
18874 		ilb_rule_t *rule;
18875 		int (*f)();
18876 
18877 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18878 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18879 			ret = EINVAL;
18880 			break;
18881 		}
18882 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18883 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18884 		    size != cmd_mp->b_wptr) {
18885 			ret = EINVAL;
18886 			break;
18887 		}
18888 
18889 		if (command == ILB_DEL_SERVERS)
18890 			f = ilb_server_del;
18891 		else if (command == ILB_ENABLE_SERVERS)
18892 			f = ilb_server_enable;
18893 		else if (command == ILB_DISABLE_SERVERS)
18894 			f = ilb_server_disable;
18895 
18896 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18897 		if (rule == NULL) {
18898 			ASSERT(ret != 0);
18899 			break;
18900 		}
18901 
18902 		for (i = 0; i < cmd->num_servers; i++) {
18903 			ilb_server_arg_t *s;
18904 
18905 			s = &cmd->servers[i];
18906 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18907 		}
18908 		ILB_RULE_REFRELE(rule);
18909 		break;
18910 	}
18911 	case ILB_LIST_NAT_TABLE: {
18912 		ilb_list_nat_cmd_t *cmd;
18913 
18914 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18915 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18916 			ret = EINVAL;
18917 			break;
18918 		}
18919 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18920 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18921 		    size != cmd_mp->b_wptr) {
18922 			ret = EINVAL;
18923 			break;
18924 		}
18925 
18926 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18927 		    &cmd->flags);
18928 		break;
18929 	}
18930 	case ILB_LIST_STICKY_TABLE: {
18931 		ilb_list_sticky_cmd_t *cmd;
18932 
18933 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18934 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18935 			ret = EINVAL;
18936 			break;
18937 		}
18938 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18939 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18940 		    size != cmd_mp->b_wptr) {
18941 			ret = EINVAL;
18942 			break;
18943 		}
18944 
18945 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18946 		    &cmd->num_sticky, &cmd->flags);
18947 		break;
18948 	}
18949 	default:
18950 		ret = EINVAL;
18951 		break;
18952 	}
18953 done:
18954 	return (ret);
18955 }
18956 
18957 /* Remove all cache entries for this logical interface */
18958 void
18959 ipif_nce_down(ipif_t *ipif)
18960 {
18961 	ill_t *ill = ipif->ipif_ill;
18962 	nce_t *nce;
18963 
18964 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18965 	    ill_t *, ill, ipif_t *, ipif);
18966 	if (ipif->ipif_added_nce) {
18967 		if (ipif->ipif_isv6)
18968 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18969 		else
18970 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18971 		if (nce != NULL) {
18972 			if (--nce->nce_ipif_cnt == 0)
18973 				ncec_delete(nce->nce_common);
18974 			ipif->ipif_added_nce = 0;
18975 			nce_refrele(nce);
18976 		} else {
18977 			/*
18978 			 * nce may already be NULL because it was already
18979 			 * flushed, e.g., due to a call to nce_flush
18980 			 */
18981 			ipif->ipif_added_nce = 0;
18982 		}
18983 	}
18984 	/*
18985 	 * Make IPMP aware of the deleted data address.
18986 	 */
18987 	if (IS_IPMP(ill))
18988 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18989 
18990 	/*
18991 	 * Remove all other nces dependent on this ill when the last ipif
18992 	 * is going away.
18993 	 */
18994 	if (ill->ill_ipif_up_count == 0) {
18995 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18996 		    (uchar_t *)ill, ill->ill_ipst);
18997 		if (IS_UNDER_IPMP(ill))
18998 			nce_flush(ill, B_TRUE);
18999 	}
19000 }
19001 
19002 /*
19003  * find the first interface that uses usill for its source address.
19004  */
19005 ill_t *
19006 ill_lookup_usesrc(ill_t *usill)
19007 {
19008 	ip_stack_t *ipst = usill->ill_ipst;
19009 	ill_t *ill;
19010 
19011 	ASSERT(usill != NULL);
19012 
19013 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
19014 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
19015 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19016 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
19017 	    ill = ill->ill_usesrc_grp_next) {
19018 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
19019 		    !ILL_IS_CONDEMNED(ill)) {
19020 			ill_refhold(ill);
19021 			break;
19022 		}
19023 	}
19024 	rw_exit(&ipst->ips_ill_g_lock);
19025 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
19026 	return (ill);
19027 }
19028