xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
118 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
119 		    char *value, caddr_t cp, cred_t *ioc_cr);
120 
121 static boolean_t ill_is_quiescent(ill_t *);
122 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
123 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
124 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
125     mblk_t *mp, boolean_t need_up);
126 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
127     mblk_t *mp, boolean_t need_up);
128 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
129     queue_t *q, mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
131     mblk_t *mp);
132 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp);
134 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
137     int ioccmd, struct linkblk *li);
138 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
139 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
140 static void	ipsq_flush(ill_t *ill);
141 
142 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static void	ipsq_delete(ipsq_t *);
145 
146 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
147     boolean_t initialize, boolean_t insert);
148 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
149 static void	ipif_delete_bcast_ires(ipif_t *ipif);
150 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
151 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
152 		    boolean_t isv6);
153 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
154 static void	ipif_free(ipif_t *ipif);
155 static void	ipif_free_tail(ipif_t *ipif);
156 static void	ipif_set_default(ipif_t *ipif);
157 static int	ipif_set_values(queue_t *q, mblk_t *mp,
158     char *interf_name, uint_t *ppa);
159 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
160     queue_t *q);
161 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
162     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
163     ip_stack_t *);
164 
165 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
166 static void	ill_delete_interface_type(ill_if_t *);
167 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
168 static void	ill_dl_down(ill_t *ill);
169 static void	ill_down(ill_t *ill);
170 static void	ill_down_ipifs(ill_t *, boolean_t);
171 static void	ill_free_mib(ill_t *ill);
172 static void	ill_glist_delete(ill_t *);
173 static void	ill_phyint_reinit(ill_t *ill);
174 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
175 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
177 
178 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
179 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
180 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
181 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
182 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
183 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
184 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
185 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
186 static ip_v4mapinfo_func_t ip_mbcast_mapping;
187 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
188 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
189 static void	phyint_free(phyint_t *);
190 
191 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
194 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
196 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
197     dl_capability_sub_t *);
198 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
199 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
200 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
201 		    dl_capability_sub_t *);
202 static void	ill_capability_dld_enable(ill_t *);
203 static void	ill_capability_ack_thr(void *);
204 static void	ill_capability_lso_enable(ill_t *);
205 
206 static ill_t	*ill_prev_usesrc(ill_t *);
207 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
208 static void	ill_disband_usesrc_group(ill_t *);
209 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
210 
211 #ifdef DEBUG
212 static	void	ill_trace_cleanup(const ill_t *);
213 static	void	ipif_trace_cleanup(const ipif_t *);
214 #endif
215 
216 /*
217  * if we go over the memory footprint limit more than once in this msec
218  * interval, we'll start pruning aggressively.
219  */
220 int ip_min_frag_prune_time = 0;
221 
222 static ipft_t	ip_ioctl_ftbl[] = {
223 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
224 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
225 		IPFT_F_NO_REPLY },
226 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
227 	{ 0 }
228 };
229 
230 /* Simple ICMP IP Header Template */
231 static ipha_t icmp_ipha = {
232 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
233 };
234 
235 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
236 
237 static ip_m_t   ip_m_tbl[] = {
238 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
239 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
240 	    ip_nodef_v6intfid },
241 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
242 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
243 	    ip_nodef_v6intfid },
244 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
245 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
246 	    ip_nodef_v6intfid },
247 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
248 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
249 	    ip_nodef_v6intfid },
250 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
251 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
252 	    ip_nodef_v6intfid },
253 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
254 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
255 	    ip_nodef_v6intfid },
256 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
257 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
258 	    ip_ipv4_v6destintfid },
259 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
260 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
261 	    ip_ipv6_v6destintfid },
262 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
263 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
264 	    ip_nodef_v6intfid },
265 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
266 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
267 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
269 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
271 	    ip_nodef_v6intfid }
272 };
273 
274 static ill_t	ill_null;		/* Empty ILL for init. */
275 char	ipif_loopback_name[] = "lo0";
276 static char *ipv4_forward_suffix = ":ip_forwarding";
277 static char *ipv6_forward_suffix = ":ip6_forwarding";
278 static	sin6_t	sin6_null;	/* Zero address for quick clears */
279 static	sin_t	sin_null;	/* Zero address for quick clears */
280 
281 /* When set search for unused ipif_seqid */
282 static ipif_t	ipif_zero;
283 
284 /*
285  * ppa arena is created after these many
286  * interfaces have been plumbed.
287  */
288 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
289 
290 /*
291  * Allocate per-interface mibs.
292  * Returns true if ok. False otherwise.
293  *  ipsq  may not yet be allocated (loopback case ).
294  */
295 static boolean_t
296 ill_allocate_mibs(ill_t *ill)
297 {
298 	/* Already allocated? */
299 	if (ill->ill_ip_mib != NULL) {
300 		if (ill->ill_isv6)
301 			ASSERT(ill->ill_icmp6_mib != NULL);
302 		return (B_TRUE);
303 	}
304 
305 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
306 	    KM_NOSLEEP);
307 	if (ill->ill_ip_mib == NULL) {
308 		return (B_FALSE);
309 	}
310 
311 	/* Setup static information */
312 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
313 	    sizeof (mib2_ipIfStatsEntry_t));
314 	if (ill->ill_isv6) {
315 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
316 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
317 		    sizeof (mib2_ipv6AddrEntry_t));
318 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
319 		    sizeof (mib2_ipv6RouteEntry_t));
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
321 		    sizeof (mib2_ipv6NetToMediaEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
323 		    sizeof (ipv6_member_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
325 		    sizeof (ipv6_grpsrc_t));
326 	} else {
327 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
329 		    sizeof (mib2_ipAddrEntry_t));
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
331 		    sizeof (mib2_ipRouteEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
333 		    sizeof (mib2_ipNetToMediaEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
335 		    sizeof (ip_member_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
337 		    sizeof (ip_grpsrc_t));
338 
339 		/*
340 		 * For a v4 ill, we are done at this point, because per ill
341 		 * icmp mibs are only used for v6.
342 		 */
343 		return (B_TRUE);
344 	}
345 
346 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
347 	    KM_NOSLEEP);
348 	if (ill->ill_icmp6_mib == NULL) {
349 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
350 		ill->ill_ip_mib = NULL;
351 		return (B_FALSE);
352 	}
353 	/* static icmp info */
354 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
355 	    sizeof (mib2_ipv6IfIcmpEntry_t);
356 	/*
357 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
358 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
359 	 * -> ill_phyint_reinit
360 	 */
361 	return (B_TRUE);
362 }
363 
364 /*
365  * Completely vaporize a lower level tap and all associated interfaces.
366  * ill_delete is called only out of ip_close when the device control
367  * stream is being closed.
368  */
369 void
370 ill_delete(ill_t *ill)
371 {
372 	ipif_t	*ipif;
373 	ill_t	*prev_ill;
374 	ip_stack_t	*ipst = ill->ill_ipst;
375 
376 	/*
377 	 * ill_delete may be forcibly entering the ipsq. The previous
378 	 * ioctl may not have completed and may need to be aborted.
379 	 * ipsq_flush takes care of it. If we don't need to enter the
380 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
381 	 * ill_delete_tail is sufficient.
382 	 */
383 	ipsq_flush(ill);
384 
385 	/*
386 	 * Nuke all interfaces.  ipif_free will take down the interface,
387 	 * remove it from the list, and free the data structure.
388 	 * Walk down the ipif list and remove the logical interfaces
389 	 * first before removing the main ipif. We can't unplumb
390 	 * zeroth interface first in the case of IPv6 as update_conn_ill
391 	 * -> ip_ll_multireq de-references ill_ipif for checking
392 	 * POINTOPOINT.
393 	 *
394 	 * If ill_ipif was not properly initialized (i.e low on memory),
395 	 * then no interfaces to clean up. In this case just clean up the
396 	 * ill.
397 	 */
398 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
399 		ipif_free(ipif);
400 
401 	/*
402 	 * clean out all the nce_t entries that depend on this
403 	 * ill for the ill_phys_addr.
404 	 */
405 	nce_flush(ill, B_TRUE);
406 
407 	/* Clean up msgs on pending upcalls for mrouted */
408 	reset_mrt_ill(ill);
409 
410 	update_conn_ill(ill, ipst);
411 
412 	/*
413 	 * Remove multicast references added as a result of calls to
414 	 * ip_join_allmulti().
415 	 */
416 	ip_purge_allmulti(ill);
417 
418 	/*
419 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
420 	 */
421 	if (IS_UNDER_IPMP(ill))
422 		ipmp_ill_leave_illgrp(ill);
423 
424 	/*
425 	 * ill_down will arrange to blow off any IRE's dependent on this
426 	 * ILL, and shut down fragmentation reassembly.
427 	 */
428 	ill_down(ill);
429 
430 	/* Let SCTP know, so that it can remove this from its list. */
431 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
432 
433 	/*
434 	 * Walk all CONNs that can have a reference on an ire or nce for this
435 	 * ill (we actually walk all that now have stale references).
436 	 */
437 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
438 
439 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
440 	if (ill->ill_isv6)
441 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
442 
443 	/*
444 	 * If an address on this ILL is being used as a source address then
445 	 * clear out the pointers in other ILLs that point to this ILL.
446 	 */
447 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
448 	if (ill->ill_usesrc_grp_next != NULL) {
449 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
450 			ill_disband_usesrc_group(ill);
451 		} else {	/* consumer of the usesrc ILL */
452 			prev_ill = ill_prev_usesrc(ill);
453 			prev_ill->ill_usesrc_grp_next =
454 			    ill->ill_usesrc_grp_next;
455 		}
456 	}
457 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
458 }
459 
460 static void
461 ipif_non_duplicate(ipif_t *ipif)
462 {
463 	ill_t *ill = ipif->ipif_ill;
464 	mutex_enter(&ill->ill_lock);
465 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
466 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
467 		ASSERT(ill->ill_ipif_dup_count > 0);
468 		ill->ill_ipif_dup_count--;
469 	}
470 	mutex_exit(&ill->ill_lock);
471 }
472 
473 /*
474  * ill_delete_tail is called from ip_modclose after all references
475  * to the closing ill are gone. The wait is done in ip_modclose
476  */
477 void
478 ill_delete_tail(ill_t *ill)
479 {
480 	mblk_t	**mpp;
481 	ipif_t	*ipif;
482 	ip_stack_t	*ipst = ill->ill_ipst;
483 
484 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
485 		ipif_non_duplicate(ipif);
486 		(void) ipif_down_tail(ipif);
487 	}
488 
489 	ASSERT(ill->ill_ipif_dup_count == 0);
490 
491 	/*
492 	 * If polling capability is enabled (which signifies direct
493 	 * upcall into IP and driver has ill saved as a handle),
494 	 * we need to make sure that unbind has completed before we
495 	 * let the ill disappear and driver no longer has any reference
496 	 * to this ill.
497 	 */
498 	mutex_enter(&ill->ill_lock);
499 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
500 		cv_wait(&ill->ill_cv, &ill->ill_lock);
501 	mutex_exit(&ill->ill_lock);
502 	ASSERT(!(ill->ill_capabilities &
503 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
504 
505 	if (ill->ill_net_type != IRE_LOOPBACK)
506 		qprocsoff(ill->ill_rq);
507 
508 	/*
509 	 * We do an ipsq_flush once again now. New messages could have
510 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
511 	 * could also have landed up if an ioctl thread had looked up
512 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
513 	 * enqueued the ioctl when we did the ipsq_flush last time.
514 	 */
515 	ipsq_flush(ill);
516 
517 	/*
518 	 * Free capabilities.
519 	 */
520 	if (ill->ill_hcksum_capab != NULL) {
521 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
522 		ill->ill_hcksum_capab = NULL;
523 	}
524 
525 	if (ill->ill_zerocopy_capab != NULL) {
526 		kmem_free(ill->ill_zerocopy_capab,
527 		    sizeof (ill_zerocopy_capab_t));
528 		ill->ill_zerocopy_capab = NULL;
529 	}
530 
531 	if (ill->ill_lso_capab != NULL) {
532 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
533 		ill->ill_lso_capab = NULL;
534 	}
535 
536 	if (ill->ill_dld_capab != NULL) {
537 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
538 		ill->ill_dld_capab = NULL;
539 	}
540 
541 	while (ill->ill_ipif != NULL)
542 		ipif_free_tail(ill->ill_ipif);
543 
544 	/*
545 	 * We have removed all references to ilm from conn and the ones joined
546 	 * within the kernel.
547 	 *
548 	 * We don't walk conns, mrts and ires because
549 	 *
550 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
551 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
552 	 *    ill references.
553 	 */
554 
555 	/*
556 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
557 	 * is safe to do because the illgrp has already been unlinked from the
558 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
559 	 */
560 	if (IS_IPMP(ill)) {
561 		ipmp_illgrp_destroy(ill->ill_grp);
562 		ill->ill_grp = NULL;
563 	}
564 
565 	/*
566 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
567 	 * could free the phyint. No more reference to the phyint after this
568 	 * point.
569 	 */
570 	(void) ill_glist_delete(ill);
571 
572 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
573 	if (ill->ill_ndd_name != NULL)
574 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
575 	rw_exit(&ipst->ips_ip_g_nd_lock);
576 
577 	if (ill->ill_frag_ptr != NULL) {
578 		uint_t count;
579 
580 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
581 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
582 		}
583 		mi_free(ill->ill_frag_ptr);
584 		ill->ill_frag_ptr = NULL;
585 		ill->ill_frag_hash_tbl = NULL;
586 	}
587 
588 	freemsg(ill->ill_nd_lla_mp);
589 	/* Free all retained control messages. */
590 	mpp = &ill->ill_first_mp_to_free;
591 	do {
592 		while (mpp[0]) {
593 			mblk_t  *mp;
594 			mblk_t  *mp1;
595 
596 			mp = mpp[0];
597 			mpp[0] = mp->b_next;
598 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
599 				mp1->b_next = NULL;
600 				mp1->b_prev = NULL;
601 			}
602 			freemsg(mp);
603 		}
604 	} while (mpp++ != &ill->ill_last_mp_to_free);
605 
606 	ill_free_mib(ill);
607 
608 #ifdef DEBUG
609 	ill_trace_cleanup(ill);
610 #endif
611 
612 	/* The default multicast interface might have changed */
613 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
614 
615 	/* Drop refcnt here */
616 	netstack_rele(ill->ill_ipst->ips_netstack);
617 	ill->ill_ipst = NULL;
618 }
619 
620 static void
621 ill_free_mib(ill_t *ill)
622 {
623 	ip_stack_t *ipst = ill->ill_ipst;
624 
625 	/*
626 	 * MIB statistics must not be lost, so when an interface
627 	 * goes away the counter values will be added to the global
628 	 * MIBs.
629 	 */
630 	if (ill->ill_ip_mib != NULL) {
631 		if (ill->ill_isv6) {
632 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
633 			    ill->ill_ip_mib);
634 		} else {
635 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
636 			    ill->ill_ip_mib);
637 		}
638 
639 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
640 		ill->ill_ip_mib = NULL;
641 	}
642 	if (ill->ill_icmp6_mib != NULL) {
643 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
644 		    ill->ill_icmp6_mib);
645 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
646 		ill->ill_icmp6_mib = NULL;
647 	}
648 }
649 
650 /*
651  * Concatenate together a physical address and a sap.
652  *
653  * Sap_lengths are interpreted as follows:
654  *   sap_length == 0	==>	no sap
655  *   sap_length > 0	==>	sap is at the head of the dlpi address
656  *   sap_length < 0	==>	sap is at the tail of the dlpi address
657  */
658 static void
659 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
660     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
661 {
662 	uint16_t sap_addr = (uint16_t)sap_src;
663 
664 	if (sap_length == 0) {
665 		if (phys_src == NULL)
666 			bzero(dst, phys_length);
667 		else
668 			bcopy(phys_src, dst, phys_length);
669 	} else if (sap_length < 0) {
670 		if (phys_src == NULL)
671 			bzero(dst, phys_length);
672 		else
673 			bcopy(phys_src, dst, phys_length);
674 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
675 	} else {
676 		bcopy(&sap_addr, dst, sizeof (sap_addr));
677 		if (phys_src == NULL)
678 			bzero((char *)dst + sap_length, phys_length);
679 		else
680 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
681 	}
682 }
683 
684 /*
685  * Generate a dl_unitdata_req mblk for the device and address given.
686  * addr_length is the length of the physical portion of the address.
687  * If addr is NULL include an all zero address of the specified length.
688  * TRUE? In any case, addr_length is taken to be the entire length of the
689  * dlpi address, including the absolute value of sap_length.
690  */
691 mblk_t *
692 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
693 		t_scalar_t sap_length)
694 {
695 	dl_unitdata_req_t *dlur;
696 	mblk_t	*mp;
697 	t_scalar_t	abs_sap_length;		/* absolute value */
698 
699 	abs_sap_length = ABS(sap_length);
700 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
701 	    DL_UNITDATA_REQ);
702 	if (mp == NULL)
703 		return (NULL);
704 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
705 	/* HACK: accomodate incompatible DLPI drivers */
706 	if (addr_length == 8)
707 		addr_length = 6;
708 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
709 	dlur->dl_dest_addr_offset = sizeof (*dlur);
710 	dlur->dl_priority.dl_min = 0;
711 	dlur->dl_priority.dl_max = 0;
712 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
713 	    (uchar_t *)&dlur[1]);
714 	return (mp);
715 }
716 
717 /*
718  * Add the pending mp to the list. There can be only 1 pending mp
719  * in the list. Any exclusive ioctl that needs to wait for a response
720  * from another module or driver needs to use this function to set
721  * the ipx_pending_mp to the ioctl mblk and wait for the response from
722  * the other module/driver. This is also used while waiting for the
723  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
724  */
725 boolean_t
726 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
727     int waitfor)
728 {
729 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
730 
731 	ASSERT(IAM_WRITER_IPIF(ipif));
732 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
733 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
734 	ASSERT(ipx->ipx_pending_mp == NULL);
735 	/*
736 	 * The caller may be using a different ipif than the one passed into
737 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
738 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
739 	 * that `ipx_current_ipif == ipif'.
740 	 */
741 	ASSERT(ipx->ipx_current_ipif != NULL);
742 
743 	/*
744 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
745 	 * driver.
746 	 */
747 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
748 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
749 	    (DB_TYPE(add_mp) == M_PCPROTO));
750 
751 	if (connp != NULL) {
752 		ASSERT(MUTEX_HELD(&connp->conn_lock));
753 		/*
754 		 * Return error if the conn has started closing. The conn
755 		 * could have finished cleaning up the pending mp list,
756 		 * If so we should not add another mp to the list negating
757 		 * the cleanup.
758 		 */
759 		if (connp->conn_state_flags & CONN_CLOSING)
760 			return (B_FALSE);
761 	}
762 	mutex_enter(&ipx->ipx_lock);
763 	ipx->ipx_pending_ipif = ipif;
764 	/*
765 	 * Note down the queue in b_queue. This will be returned by
766 	 * ipsq_pending_mp_get. Caller will then use these values to restart
767 	 * the processing
768 	 */
769 	add_mp->b_next = NULL;
770 	add_mp->b_queue = q;
771 	ipx->ipx_pending_mp = add_mp;
772 	ipx->ipx_waitfor = waitfor;
773 	mutex_exit(&ipx->ipx_lock);
774 
775 	if (connp != NULL)
776 		connp->conn_oper_pending_ill = ipif->ipif_ill;
777 
778 	return (B_TRUE);
779 }
780 
781 /*
782  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
783  * queued in the list.
784  */
785 mblk_t *
786 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
787 {
788 	mblk_t	*curr = NULL;
789 	ipxop_t	*ipx = ipsq->ipsq_xop;
790 
791 	*connpp = NULL;
792 	mutex_enter(&ipx->ipx_lock);
793 	if (ipx->ipx_pending_mp == NULL) {
794 		mutex_exit(&ipx->ipx_lock);
795 		return (NULL);
796 	}
797 
798 	/* There can be only 1 such excl message */
799 	curr = ipx->ipx_pending_mp;
800 	ASSERT(curr->b_next == NULL);
801 	ipx->ipx_pending_ipif = NULL;
802 	ipx->ipx_pending_mp = NULL;
803 	ipx->ipx_waitfor = 0;
804 	mutex_exit(&ipx->ipx_lock);
805 
806 	if (CONN_Q(curr->b_queue)) {
807 		/*
808 		 * This mp did a refhold on the conn, at the start of the ioctl.
809 		 * So we can safely return a pointer to the conn to the caller.
810 		 */
811 		*connpp = Q_TO_CONN(curr->b_queue);
812 	} else {
813 		*connpp = NULL;
814 	}
815 	curr->b_next = NULL;
816 	curr->b_prev = NULL;
817 	return (curr);
818 }
819 
820 /*
821  * Cleanup the ioctl mp queued in ipx_pending_mp
822  * - Called in the ill_delete path
823  * - Called in the M_ERROR or M_HANGUP path on the ill.
824  * - Called in the conn close path.
825  */
826 boolean_t
827 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
828 {
829 	mblk_t	*mp;
830 	ipxop_t	*ipx;
831 	queue_t	*q;
832 	ipif_t	*ipif;
833 	int	cmd;
834 
835 	ASSERT(IAM_WRITER_ILL(ill));
836 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
837 
838 	/*
839 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
840 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
841 	 * even if it is meant for another ill, since we have to enqueue
842 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
843 	 * If connp is non-null we are called from the conn close path.
844 	 */
845 	mutex_enter(&ipx->ipx_lock);
846 	mp = ipx->ipx_pending_mp;
847 	if (mp == NULL || (connp != NULL &&
848 	    mp->b_queue != CONNP_TO_WQ(connp))) {
849 		mutex_exit(&ipx->ipx_lock);
850 		return (B_FALSE);
851 	}
852 	/* Now remove from the ipx_pending_mp */
853 	ipx->ipx_pending_mp = NULL;
854 	q = mp->b_queue;
855 	mp->b_next = NULL;
856 	mp->b_prev = NULL;
857 	mp->b_queue = NULL;
858 
859 	ipif = ipx->ipx_pending_ipif;
860 	ipx->ipx_pending_ipif = NULL;
861 	ipx->ipx_waitfor = 0;
862 	ipx->ipx_current_ipif = NULL;
863 	cmd = ipx->ipx_current_ioctl;
864 	ipx->ipx_current_ioctl = 0;
865 	ipx->ipx_current_done = B_TRUE;
866 	mutex_exit(&ipx->ipx_lock);
867 
868 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
869 		DTRACE_PROBE4(ipif__ioctl,
870 		    char *, "ipsq_pending_mp_cleanup",
871 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
872 		    ipif_t *, ipif);
873 		if (connp == NULL) {
874 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
875 		} else {
876 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
877 			mutex_enter(&ipif->ipif_ill->ill_lock);
878 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
879 			mutex_exit(&ipif->ipif_ill->ill_lock);
880 		}
881 	} else {
882 		/*
883 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
884 		 * be just inet_freemsg. we have to restart it
885 		 * otherwise the thread will be stuck.
886 		 */
887 		inet_freemsg(mp);
888 	}
889 	return (B_TRUE);
890 }
891 
892 /*
893  * Called in the conn close path and ill delete path
894  */
895 static void
896 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
897 {
898 	ipsq_t	*ipsq;
899 	mblk_t	*prev;
900 	mblk_t	*curr;
901 	mblk_t	*next;
902 	queue_t	*q;
903 	mblk_t	*tmp_list = NULL;
904 
905 	ASSERT(IAM_WRITER_ILL(ill));
906 	if (connp != NULL)
907 		q = CONNP_TO_WQ(connp);
908 	else
909 		q = ill->ill_wq;
910 
911 	ipsq = ill->ill_phyint->phyint_ipsq;
912 	/*
913 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
914 	 * In the case of ioctl from a conn, there can be only 1 mp
915 	 * queued on the ipsq. If an ill is being unplumbed, only messages
916 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
917 	 * ioctls meant for this ill form conn's are not flushed. They will
918 	 * be processed during ipsq_exit and will not find the ill and will
919 	 * return error.
920 	 */
921 	mutex_enter(&ipsq->ipsq_lock);
922 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
923 	    curr = next) {
924 		next = curr->b_next;
925 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
926 			/* Unlink the mblk from the pending mp list */
927 			if (prev != NULL) {
928 				prev->b_next = curr->b_next;
929 			} else {
930 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
931 				ipsq->ipsq_xopq_mphead = curr->b_next;
932 			}
933 			if (ipsq->ipsq_xopq_mptail == curr)
934 				ipsq->ipsq_xopq_mptail = prev;
935 			/*
936 			 * Create a temporary list and release the ipsq lock
937 			 * New elements are added to the head of the tmp_list
938 			 */
939 			curr->b_next = tmp_list;
940 			tmp_list = curr;
941 		} else {
942 			prev = curr;
943 		}
944 	}
945 	mutex_exit(&ipsq->ipsq_lock);
946 
947 	while (tmp_list != NULL) {
948 		curr = tmp_list;
949 		tmp_list = curr->b_next;
950 		curr->b_next = NULL;
951 		curr->b_prev = NULL;
952 		curr->b_queue = NULL;
953 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
954 			DTRACE_PROBE4(ipif__ioctl,
955 			    char *, "ipsq_xopq_mp_cleanup",
956 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
957 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
958 			    CONN_CLOSE : NO_COPYOUT, NULL);
959 		} else {
960 			/*
961 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
962 			 * this can't be just inet_freemsg. we have to
963 			 * restart it otherwise the thread will be stuck.
964 			 */
965 			inet_freemsg(curr);
966 		}
967 	}
968 }
969 
970 /*
971  * This conn has started closing. Cleanup any pending ioctl from this conn.
972  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
973  */
974 void
975 conn_ioctl_cleanup(conn_t *connp)
976 {
977 	ipsq_t	*ipsq;
978 	ill_t	*ill;
979 	boolean_t refheld;
980 
981 	/*
982 	 * Is any exclusive ioctl pending ? If so clean it up. If the
983 	 * ioctl has not yet started, the mp is pending in the list headed by
984 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
985 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
986 	 * is currently executing now the mp is not queued anywhere but
987 	 * conn_oper_pending_ill is null. The conn close will wait
988 	 * till the conn_ref drops to zero.
989 	 */
990 	mutex_enter(&connp->conn_lock);
991 	ill = connp->conn_oper_pending_ill;
992 	if (ill == NULL) {
993 		mutex_exit(&connp->conn_lock);
994 		return;
995 	}
996 
997 	/*
998 	 * We may not be able to refhold the ill if the ill/ipif
999 	 * is changing. But we need to make sure that the ill will
1000 	 * not vanish. So we just bump up the ill_waiter count.
1001 	 */
1002 	refheld = ill_waiter_inc(ill);
1003 	mutex_exit(&connp->conn_lock);
1004 	if (refheld) {
1005 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1006 			ill_waiter_dcr(ill);
1007 			/*
1008 			 * Check whether this ioctl has started and is
1009 			 * pending. If it is not found there then check
1010 			 * whether this ioctl has not even started and is in
1011 			 * the ipsq_xopq list.
1012 			 */
1013 			if (!ipsq_pending_mp_cleanup(ill, connp))
1014 				ipsq_xopq_mp_cleanup(ill, connp);
1015 			ipsq = ill->ill_phyint->phyint_ipsq;
1016 			ipsq_exit(ipsq);
1017 			return;
1018 		}
1019 	}
1020 
1021 	/*
1022 	 * The ill is also closing and we could not bump up the
1023 	 * ill_waiter_count or we could not enter the ipsq. Leave
1024 	 * the cleanup to ill_delete
1025 	 */
1026 	mutex_enter(&connp->conn_lock);
1027 	while (connp->conn_oper_pending_ill != NULL)
1028 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1029 	mutex_exit(&connp->conn_lock);
1030 	if (refheld)
1031 		ill_waiter_dcr(ill);
1032 }
1033 
1034 /*
1035  * ipcl_walk function for cleaning up conn_*_ill fields.
1036  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1037  * conn_bound_if in place. We prefer dropping
1038  * packets instead of sending them out the wrong interface, or accepting
1039  * packets from the wrong ifindex.
1040  */
1041 static void
1042 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1043 {
1044 	ill_t	*ill = (ill_t *)arg;
1045 
1046 	mutex_enter(&connp->conn_lock);
1047 	if (connp->conn_dhcpinit_ill == ill) {
1048 		connp->conn_dhcpinit_ill = NULL;
1049 		ASSERT(ill->ill_dhcpinit != 0);
1050 		atomic_dec_32(&ill->ill_dhcpinit);
1051 		ill_set_inputfn(ill);
1052 	}
1053 	mutex_exit(&connp->conn_lock);
1054 }
1055 
1056 static int
1057 ill_down_ipifs_tail(ill_t *ill)
1058 {
1059 	ipif_t	*ipif;
1060 	int err;
1061 
1062 	ASSERT(IAM_WRITER_ILL(ill));
1063 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1064 		ipif_non_duplicate(ipif);
1065 		/*
1066 		 * ipif_down_tail will call arp_ll_down on the last ipif
1067 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1068 		 */
1069 		if ((err = ipif_down_tail(ipif)) != 0)
1070 			return (err);
1071 	}
1072 	return (0);
1073 }
1074 
1075 /* ARGSUSED */
1076 void
1077 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1078 {
1079 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1080 	(void) ill_down_ipifs_tail(q->q_ptr);
1081 	freemsg(mp);
1082 	ipsq_current_finish(ipsq);
1083 }
1084 
1085 /*
1086  * ill_down_start is called when we want to down this ill and bring it up again
1087  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1088  * all interfaces, but don't tear down any plumbing.
1089  */
1090 boolean_t
1091 ill_down_start(queue_t *q, mblk_t *mp)
1092 {
1093 	ill_t	*ill = q->q_ptr;
1094 	ipif_t	*ipif;
1095 
1096 	ASSERT(IAM_WRITER_ILL(ill));
1097 	mutex_enter(&ill->ill_lock);
1098 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1099 	/* no more nce addition allowed */
1100 	mutex_exit(&ill->ill_lock);
1101 
1102 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1103 		(void) ipif_down(ipif, NULL, NULL);
1104 
1105 	ill_down(ill);
1106 
1107 	/*
1108 	 * Walk all CONNs that can have a reference on an ire or nce for this
1109 	 * ill (we actually walk all that now have stale references).
1110 	 */
1111 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1112 
1113 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1114 	if (ill->ill_isv6)
1115 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1116 
1117 
1118 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1119 
1120 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1121 
1122 	/*
1123 	 * Atomically test and add the pending mp if references are active.
1124 	 */
1125 	mutex_enter(&ill->ill_lock);
1126 	if (!ill_is_quiescent(ill)) {
1127 		/* call cannot fail since `conn_t *' argument is NULL */
1128 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1129 		    mp, ILL_DOWN);
1130 		mutex_exit(&ill->ill_lock);
1131 		return (B_FALSE);
1132 	}
1133 	mutex_exit(&ill->ill_lock);
1134 	return (B_TRUE);
1135 }
1136 
1137 static void
1138 ill_down(ill_t *ill)
1139 {
1140 	mblk_t	*mp;
1141 	ip_stack_t	*ipst = ill->ill_ipst;
1142 
1143 	/*
1144 	 * Blow off any IREs dependent on this ILL.
1145 	 * The caller needs to handle conn_ixa_cleanup
1146 	 */
1147 	ill_delete_ires(ill);
1148 
1149 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1150 
1151 	/* Remove any conn_*_ill depending on this ill */
1152 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1153 
1154 	/*
1155 	 * Free state for additional IREs.
1156 	 */
1157 	mutex_enter(&ill->ill_saved_ire_lock);
1158 	mp = ill->ill_saved_ire_mp;
1159 	ill->ill_saved_ire_mp = NULL;
1160 	ill->ill_saved_ire_cnt = 0;
1161 	mutex_exit(&ill->ill_saved_ire_lock);
1162 	freemsg(mp);
1163 }
1164 
1165 /*
1166  * ire_walk routine used to delete every IRE that depends on
1167  * 'ill'.  (Always called as writer.)
1168  *
1169  * Note: since the routes added by the kernel are deleted separately,
1170  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1171  *
1172  * We also remove references on ire_nce_cache entries that refer to the ill.
1173  */
1174 void
1175 ill_downi(ire_t *ire, char *ill_arg)
1176 {
1177 	ill_t	*ill = (ill_t *)ill_arg;
1178 	nce_t	*nce;
1179 
1180 	mutex_enter(&ire->ire_lock);
1181 	nce = ire->ire_nce_cache;
1182 	if (nce != NULL && nce->nce_ill == ill)
1183 		ire->ire_nce_cache = NULL;
1184 	else
1185 		nce = NULL;
1186 	mutex_exit(&ire->ire_lock);
1187 	if (nce != NULL)
1188 		nce_refrele(nce);
1189 	if (ire->ire_ill == ill)
1190 		ire_delete(ire);
1191 }
1192 
1193 /* Remove IRE_IF_CLONE on this ill */
1194 void
1195 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1196 {
1197 	ill_t	*ill = (ill_t *)ill_arg;
1198 
1199 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1200 	if (ire->ire_ill == ill)
1201 		ire_delete(ire);
1202 }
1203 
1204 /* Consume an M_IOCACK of the fastpath probe. */
1205 void
1206 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1207 {
1208 	mblk_t	*mp1 = mp;
1209 
1210 	/*
1211 	 * If this was the first attempt turn on the fastpath probing.
1212 	 */
1213 	mutex_enter(&ill->ill_lock);
1214 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1215 		ill->ill_dlpi_fastpath_state = IDS_OK;
1216 	mutex_exit(&ill->ill_lock);
1217 
1218 	/* Free the M_IOCACK mblk, hold on to the data */
1219 	mp = mp->b_cont;
1220 	freeb(mp1);
1221 	if (mp == NULL)
1222 		return;
1223 	if (mp->b_cont != NULL)
1224 		nce_fastpath_update(ill, mp);
1225 	else
1226 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1227 	freemsg(mp);
1228 }
1229 
1230 /*
1231  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1232  * The data portion of the request is a dl_unitdata_req_t template for
1233  * what we would send downstream in the absence of a fastpath confirmation.
1234  */
1235 int
1236 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1237 {
1238 	struct iocblk	*ioc;
1239 	mblk_t	*mp;
1240 
1241 	if (dlur_mp == NULL)
1242 		return (EINVAL);
1243 
1244 	mutex_enter(&ill->ill_lock);
1245 	switch (ill->ill_dlpi_fastpath_state) {
1246 	case IDS_FAILED:
1247 		/*
1248 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1249 		 * support it.
1250 		 */
1251 		mutex_exit(&ill->ill_lock);
1252 		return (ENOTSUP);
1253 	case IDS_UNKNOWN:
1254 		/* This is the first probe */
1255 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1256 		break;
1257 	default:
1258 		break;
1259 	}
1260 	mutex_exit(&ill->ill_lock);
1261 
1262 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1263 		return (EAGAIN);
1264 
1265 	mp->b_cont = copyb(dlur_mp);
1266 	if (mp->b_cont == NULL) {
1267 		freeb(mp);
1268 		return (EAGAIN);
1269 	}
1270 
1271 	ioc = (struct iocblk *)mp->b_rptr;
1272 	ioc->ioc_count = msgdsize(mp->b_cont);
1273 
1274 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1275 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1276 	putnext(ill->ill_wq, mp);
1277 	return (0);
1278 }
1279 
1280 void
1281 ill_capability_probe(ill_t *ill)
1282 {
1283 	mblk_t	*mp;
1284 
1285 	ASSERT(IAM_WRITER_ILL(ill));
1286 
1287 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1288 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1289 		return;
1290 
1291 	/*
1292 	 * We are starting a new cycle of capability negotiation.
1293 	 * Free up the capab reset messages of any previous incarnation.
1294 	 * We will do a fresh allocation when we get the response to our probe
1295 	 */
1296 	if (ill->ill_capab_reset_mp != NULL) {
1297 		freemsg(ill->ill_capab_reset_mp);
1298 		ill->ill_capab_reset_mp = NULL;
1299 	}
1300 
1301 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1302 
1303 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1304 	if (mp == NULL)
1305 		return;
1306 
1307 	ill_capability_send(ill, mp);
1308 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1309 }
1310 
1311 void
1312 ill_capability_reset(ill_t *ill, boolean_t reneg)
1313 {
1314 	ASSERT(IAM_WRITER_ILL(ill));
1315 
1316 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1317 		return;
1318 
1319 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1320 
1321 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1322 	ill->ill_capab_reset_mp = NULL;
1323 	/*
1324 	 * We turn off all capabilities except those pertaining to
1325 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1326 	 * which will be turned off by the corresponding reset functions.
1327 	 */
1328 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1329 }
1330 
1331 static void
1332 ill_capability_reset_alloc(ill_t *ill)
1333 {
1334 	mblk_t *mp;
1335 	size_t	size = 0;
1336 	int	err;
1337 	dl_capability_req_t	*capb;
1338 
1339 	ASSERT(IAM_WRITER_ILL(ill));
1340 	ASSERT(ill->ill_capab_reset_mp == NULL);
1341 
1342 	if (ILL_HCKSUM_CAPABLE(ill)) {
1343 		size += sizeof (dl_capability_sub_t) +
1344 		    sizeof (dl_capab_hcksum_t);
1345 	}
1346 
1347 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1348 		size += sizeof (dl_capability_sub_t) +
1349 		    sizeof (dl_capab_zerocopy_t);
1350 	}
1351 
1352 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1353 		size += sizeof (dl_capability_sub_t) +
1354 		    sizeof (dl_capab_dld_t);
1355 	}
1356 
1357 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1358 	    STR_NOSIG, &err);
1359 
1360 	mp->b_datap->db_type = M_PROTO;
1361 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1362 
1363 	capb = (dl_capability_req_t *)mp->b_rptr;
1364 	capb->dl_primitive = DL_CAPABILITY_REQ;
1365 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1366 	capb->dl_sub_length = size;
1367 
1368 	mp->b_wptr += sizeof (dl_capability_req_t);
1369 
1370 	/*
1371 	 * Each handler fills in the corresponding dl_capability_sub_t
1372 	 * inside the mblk,
1373 	 */
1374 	ill_capability_hcksum_reset_fill(ill, mp);
1375 	ill_capability_zerocopy_reset_fill(ill, mp);
1376 	ill_capability_dld_reset_fill(ill, mp);
1377 
1378 	ill->ill_capab_reset_mp = mp;
1379 }
1380 
1381 static void
1382 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1383 {
1384 	dl_capab_id_t *id_ic;
1385 	uint_t sub_dl_cap = outers->dl_cap;
1386 	dl_capability_sub_t *inners;
1387 	uint8_t *capend;
1388 
1389 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1390 
1391 	/*
1392 	 * Note: range checks here are not absolutely sufficient to
1393 	 * make us robust against malformed messages sent by drivers;
1394 	 * this is in keeping with the rest of IP's dlpi handling.
1395 	 * (Remember, it's coming from something else in the kernel
1396 	 * address space)
1397 	 */
1398 
1399 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1400 	if (capend > mp->b_wptr) {
1401 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1402 		    "malformed sub-capability too long for mblk");
1403 		return;
1404 	}
1405 
1406 	id_ic = (dl_capab_id_t *)(outers + 1);
1407 
1408 	if (outers->dl_length < sizeof (*id_ic) ||
1409 	    (inners = &id_ic->id_subcap,
1410 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1411 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1412 		    "encapsulated capab type %d too long for mblk",
1413 		    inners->dl_cap);
1414 		return;
1415 	}
1416 
1417 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1418 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1419 		    "isn't as expected; pass-thru module(s) detected, "
1420 		    "discarding capability\n", inners->dl_cap));
1421 		return;
1422 	}
1423 
1424 	/* Process the encapsulated sub-capability */
1425 	ill_capability_dispatch(ill, mp, inners);
1426 }
1427 
1428 static void
1429 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1430 {
1431 	dl_capability_sub_t *dl_subcap;
1432 
1433 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1434 		return;
1435 
1436 	/*
1437 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1438 	 * initialized below since it is not used by DLD.
1439 	 */
1440 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1441 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1442 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1443 
1444 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1445 }
1446 
1447 static void
1448 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1449 {
1450 	/*
1451 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1452 	 * is only to get the VRRP capability.
1453 	 */
1454 	if (ill->ill_ipif_up_count == 0) {
1455 		if (subp->dl_cap == DL_CAPAB_VRRP)
1456 			ill_capability_vrrp_ack(ill, mp, subp);
1457 		return;
1458 	}
1459 
1460 	switch (subp->dl_cap) {
1461 	case DL_CAPAB_HCKSUM:
1462 		ill_capability_hcksum_ack(ill, mp, subp);
1463 		break;
1464 	case DL_CAPAB_ZEROCOPY:
1465 		ill_capability_zerocopy_ack(ill, mp, subp);
1466 		break;
1467 	case DL_CAPAB_DLD:
1468 		ill_capability_dld_ack(ill, mp, subp);
1469 		break;
1470 	case DL_CAPAB_VRRP:
1471 		break;
1472 	default:
1473 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1474 		    subp->dl_cap));
1475 	}
1476 }
1477 
1478 /*
1479  * Process the vrrp capability received from a DLS Provider. isub must point
1480  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1481  */
1482 static void
1483 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1484 {
1485 	dl_capab_vrrp_t	*vrrp;
1486 	uint_t		sub_dl_cap = isub->dl_cap;
1487 	uint8_t		*capend;
1488 
1489 	ASSERT(IAM_WRITER_ILL(ill));
1490 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1491 
1492 	/*
1493 	 * Note: range checks here are not absolutely sufficient to
1494 	 * make us robust against malformed messages sent by drivers;
1495 	 * this is in keeping with the rest of IP's dlpi handling.
1496 	 * (Remember, it's coming from something else in the kernel
1497 	 * address space)
1498 	 */
1499 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1500 	if (capend > mp->b_wptr) {
1501 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1502 		    "malformed sub-capability too long for mblk");
1503 		return;
1504 	}
1505 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1506 
1507 	/*
1508 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1509 	 */
1510 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1511 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1512 		ill->ill_flags |= ILLF_VRRP;
1513 	}
1514 }
1515 
1516 /*
1517  * Process a hardware checksum offload capability negotiation ack received
1518  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1519  * of a DL_CAPABILITY_ACK message.
1520  */
1521 static void
1522 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1523 {
1524 	dl_capability_req_t	*ocap;
1525 	dl_capab_hcksum_t	*ihck, *ohck;
1526 	ill_hcksum_capab_t	**ill_hcksum;
1527 	mblk_t			*nmp = NULL;
1528 	uint_t			sub_dl_cap = isub->dl_cap;
1529 	uint8_t			*capend;
1530 
1531 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1532 
1533 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1534 
1535 	/*
1536 	 * Note: range checks here are not absolutely sufficient to
1537 	 * make us robust against malformed messages sent by drivers;
1538 	 * this is in keeping with the rest of IP's dlpi handling.
1539 	 * (Remember, it's coming from something else in the kernel
1540 	 * address space)
1541 	 */
1542 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1543 	if (capend > mp->b_wptr) {
1544 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1545 		    "malformed sub-capability too long for mblk");
1546 		return;
1547 	}
1548 
1549 	/*
1550 	 * There are two types of acks we process here:
1551 	 * 1. acks in reply to a (first form) generic capability req
1552 	 *    (no ENABLE flag set)
1553 	 * 2. acks in reply to a ENABLE capability req.
1554 	 *    (ENABLE flag set)
1555 	 */
1556 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1557 
1558 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1559 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1560 		    "unsupported hardware checksum "
1561 		    "sub-capability (version %d, expected %d)",
1562 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1563 		return;
1564 	}
1565 
1566 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1567 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1568 		    "checksum capability isn't as expected; pass-thru "
1569 		    "module(s) detected, discarding capability\n"));
1570 		return;
1571 	}
1572 
1573 #define	CURR_HCKSUM_CAPAB				\
1574 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1575 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1576 
1577 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1578 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1579 		/* do ENABLE processing */
1580 		if (*ill_hcksum == NULL) {
1581 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1582 			    KM_NOSLEEP);
1583 
1584 			if (*ill_hcksum == NULL) {
1585 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1586 				    "could not enable hcksum version %d "
1587 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1588 				    ill->ill_name);
1589 				return;
1590 			}
1591 		}
1592 
1593 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1594 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1595 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1596 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1597 		    "has enabled hardware checksumming\n ",
1598 		    ill->ill_name));
1599 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1600 		/*
1601 		 * Enabling hardware checksum offload
1602 		 * Currently IP supports {TCP,UDP}/IPv4
1603 		 * partial and full cksum offload and
1604 		 * IPv4 header checksum offload.
1605 		 * Allocate new mblk which will
1606 		 * contain a new capability request
1607 		 * to enable hardware checksum offload.
1608 		 */
1609 		uint_t	size;
1610 		uchar_t	*rptr;
1611 
1612 		size = sizeof (dl_capability_req_t) +
1613 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1614 
1615 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1616 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1617 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1618 			    ill->ill_name);
1619 			return;
1620 		}
1621 
1622 		rptr = nmp->b_rptr;
1623 		/* initialize dl_capability_req_t */
1624 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1625 		ocap->dl_sub_offset =
1626 		    sizeof (dl_capability_req_t);
1627 		ocap->dl_sub_length =
1628 		    sizeof (dl_capability_sub_t) +
1629 		    isub->dl_length;
1630 		nmp->b_rptr += sizeof (dl_capability_req_t);
1631 
1632 		/* initialize dl_capability_sub_t */
1633 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1634 		nmp->b_rptr += sizeof (*isub);
1635 
1636 		/* initialize dl_capab_hcksum_t */
1637 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1638 		bcopy(ihck, ohck, sizeof (*ihck));
1639 
1640 		nmp->b_rptr = rptr;
1641 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1642 
1643 		/* Set ENABLE flag */
1644 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1645 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1646 
1647 		/*
1648 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1649 		 * hardware checksum acceleration.
1650 		 */
1651 		ill_capability_send(ill, nmp);
1652 	} else {
1653 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1654 		    "advertised %x hardware checksum capability flags\n",
1655 		    ill->ill_name, ihck->hcksum_txflags));
1656 	}
1657 }
1658 
1659 static void
1660 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1661 {
1662 	dl_capab_hcksum_t *hck_subcap;
1663 	dl_capability_sub_t *dl_subcap;
1664 
1665 	if (!ILL_HCKSUM_CAPABLE(ill))
1666 		return;
1667 
1668 	ASSERT(ill->ill_hcksum_capab != NULL);
1669 
1670 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1671 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1672 	dl_subcap->dl_length = sizeof (*hck_subcap);
1673 
1674 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1675 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1676 	hck_subcap->hcksum_txflags = 0;
1677 
1678 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1679 }
1680 
1681 static void
1682 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1683 {
1684 	mblk_t *nmp = NULL;
1685 	dl_capability_req_t *oc;
1686 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1687 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1688 	uint_t sub_dl_cap = isub->dl_cap;
1689 	uint8_t *capend;
1690 
1691 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1692 
1693 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1694 
1695 	/*
1696 	 * Note: range checks here are not absolutely sufficient to
1697 	 * make us robust against malformed messages sent by drivers;
1698 	 * this is in keeping with the rest of IP's dlpi handling.
1699 	 * (Remember, it's coming from something else in the kernel
1700 	 * address space)
1701 	 */
1702 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1703 	if (capend > mp->b_wptr) {
1704 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1705 		    "malformed sub-capability too long for mblk");
1706 		return;
1707 	}
1708 
1709 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1710 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1711 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1712 		    "unsupported ZEROCOPY sub-capability (version %d, "
1713 		    "expected %d)", zc_ic->zerocopy_version,
1714 		    ZEROCOPY_VERSION_1);
1715 		return;
1716 	}
1717 
1718 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1719 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1720 		    "capability isn't as expected; pass-thru module(s) "
1721 		    "detected, discarding capability\n"));
1722 		return;
1723 	}
1724 
1725 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1726 		if (*ill_zerocopy_capab == NULL) {
1727 			*ill_zerocopy_capab =
1728 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1729 			    KM_NOSLEEP);
1730 
1731 			if (*ill_zerocopy_capab == NULL) {
1732 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1733 				    "could not enable Zero-copy version %d "
1734 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1735 				    ill->ill_name);
1736 				return;
1737 			}
1738 		}
1739 
1740 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1741 		    "supports Zero-copy version %d\n", ill->ill_name,
1742 		    ZEROCOPY_VERSION_1));
1743 
1744 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1745 		    zc_ic->zerocopy_version;
1746 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1747 		    zc_ic->zerocopy_flags;
1748 
1749 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1750 	} else {
1751 		uint_t size;
1752 		uchar_t *rptr;
1753 
1754 		size = sizeof (dl_capability_req_t) +
1755 		    sizeof (dl_capability_sub_t) +
1756 		    sizeof (dl_capab_zerocopy_t);
1757 
1758 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1759 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1760 			    "could not enable zerocopy for %s (ENOMEM)\n",
1761 			    ill->ill_name);
1762 			return;
1763 		}
1764 
1765 		rptr = nmp->b_rptr;
1766 		/* initialize dl_capability_req_t */
1767 		oc = (dl_capability_req_t *)rptr;
1768 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1769 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1770 		    sizeof (dl_capab_zerocopy_t);
1771 		rptr += sizeof (dl_capability_req_t);
1772 
1773 		/* initialize dl_capability_sub_t */
1774 		bcopy(isub, rptr, sizeof (*isub));
1775 		rptr += sizeof (*isub);
1776 
1777 		/* initialize dl_capab_zerocopy_t */
1778 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1779 		*zc_oc = *zc_ic;
1780 
1781 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1782 		    "to enable zero-copy version %d\n", ill->ill_name,
1783 		    ZEROCOPY_VERSION_1));
1784 
1785 		/* set VMSAFE_MEM flag */
1786 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1787 
1788 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1789 		ill_capability_send(ill, nmp);
1790 	}
1791 }
1792 
1793 static void
1794 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1795 {
1796 	dl_capab_zerocopy_t *zerocopy_subcap;
1797 	dl_capability_sub_t *dl_subcap;
1798 
1799 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1800 		return;
1801 
1802 	ASSERT(ill->ill_zerocopy_capab != NULL);
1803 
1804 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1805 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1806 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1807 
1808 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1809 	zerocopy_subcap->zerocopy_version =
1810 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1811 	zerocopy_subcap->zerocopy_flags = 0;
1812 
1813 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1814 }
1815 
1816 /*
1817  * DLD capability
1818  * Refer to dld.h for more information regarding the purpose and usage
1819  * of this capability.
1820  */
1821 static void
1822 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1823 {
1824 	dl_capab_dld_t		*dld_ic, dld;
1825 	uint_t			sub_dl_cap = isub->dl_cap;
1826 	uint8_t			*capend;
1827 	ill_dld_capab_t		*idc;
1828 
1829 	ASSERT(IAM_WRITER_ILL(ill));
1830 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1831 
1832 	/*
1833 	 * Note: range checks here are not absolutely sufficient to
1834 	 * make us robust against malformed messages sent by drivers;
1835 	 * this is in keeping with the rest of IP's dlpi handling.
1836 	 * (Remember, it's coming from something else in the kernel
1837 	 * address space)
1838 	 */
1839 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1840 	if (capend > mp->b_wptr) {
1841 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1842 		    "malformed sub-capability too long for mblk");
1843 		return;
1844 	}
1845 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1846 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1847 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1848 		    "unsupported DLD sub-capability (version %d, "
1849 		    "expected %d)", dld_ic->dld_version,
1850 		    DLD_CURRENT_VERSION);
1851 		return;
1852 	}
1853 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1854 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1855 		    "capability isn't as expected; pass-thru module(s) "
1856 		    "detected, discarding capability\n"));
1857 		return;
1858 	}
1859 
1860 	/*
1861 	 * Copy locally to ensure alignment.
1862 	 */
1863 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1864 
1865 	if ((idc = ill->ill_dld_capab) == NULL) {
1866 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1867 		if (idc == NULL) {
1868 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1869 			    "could not enable DLD version %d "
1870 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1871 			    ill->ill_name);
1872 			return;
1873 		}
1874 		ill->ill_dld_capab = idc;
1875 	}
1876 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1877 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1878 	ip1dbg(("ill_capability_dld_ack: interface %s "
1879 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1880 
1881 	ill_capability_dld_enable(ill);
1882 }
1883 
1884 /*
1885  * Typically capability negotiation between IP and the driver happens via
1886  * DLPI message exchange. However GLD also offers a direct function call
1887  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1888  * But arbitrary function calls into IP or GLD are not permitted, since both
1889  * of them are protected by their own perimeter mechanism. The perimeter can
1890  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1891  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1892  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1893  * to enter the mac perimeter and then do the direct function calls into
1894  * GLD to enable squeue polling. The ring related callbacks from the mac into
1895  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1896  * protected by the mac perimeter.
1897  */
1898 static void
1899 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1900 {
1901 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1902 	int			err;
1903 
1904 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1905 	    DLD_ENABLE);
1906 	ASSERT(err == 0);
1907 }
1908 
1909 static void
1910 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1911 {
1912 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1913 	int			err;
1914 
1915 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1916 	    DLD_DISABLE);
1917 	ASSERT(err == 0);
1918 }
1919 
1920 boolean_t
1921 ill_mac_perim_held(ill_t *ill)
1922 {
1923 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1924 
1925 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1926 	    DLD_QUERY));
1927 }
1928 
1929 static void
1930 ill_capability_direct_enable(ill_t *ill)
1931 {
1932 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1933 	ill_dld_direct_t	*idd = &idc->idc_direct;
1934 	dld_capab_direct_t	direct;
1935 	int			rc;
1936 
1937 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1938 
1939 	bzero(&direct, sizeof (direct));
1940 	direct.di_rx_cf = (uintptr_t)ip_input;
1941 	direct.di_rx_ch = ill;
1942 
1943 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1944 	    DLD_ENABLE);
1945 	if (rc == 0) {
1946 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1947 		idd->idd_tx_dh = direct.di_tx_dh;
1948 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1949 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1950 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1951 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1952 		ASSERT(idd->idd_tx_cb_df != NULL);
1953 		ASSERT(idd->idd_tx_fctl_df != NULL);
1954 		ASSERT(idd->idd_tx_df != NULL);
1955 		/*
1956 		 * One time registration of flow enable callback function
1957 		 */
1958 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
1959 		    ill_flow_enable, ill);
1960 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
1961 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
1962 	} else {
1963 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
1964 		    "capability, rc = %d\n", rc);
1965 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
1966 	}
1967 }
1968 
1969 static void
1970 ill_capability_poll_enable(ill_t *ill)
1971 {
1972 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1973 	dld_capab_poll_t	poll;
1974 	int			rc;
1975 
1976 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1977 
1978 	bzero(&poll, sizeof (poll));
1979 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
1980 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
1981 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
1982 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
1983 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
1984 	poll.poll_ring_ch = ill;
1985 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
1986 	    DLD_ENABLE);
1987 	if (rc == 0) {
1988 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
1989 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
1990 	} else {
1991 		ip1dbg(("warning: could not enable POLL "
1992 		    "capability, rc = %d\n", rc));
1993 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
1994 	}
1995 }
1996 
1997 /*
1998  * Enable the LSO capability.
1999  */
2000 static void
2001 ill_capability_lso_enable(ill_t *ill)
2002 {
2003 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2004 	dld_capab_lso_t	lso;
2005 	int rc;
2006 
2007 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2008 
2009 	if (ill->ill_lso_capab == NULL) {
2010 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2011 		    KM_NOSLEEP);
2012 		if (ill->ill_lso_capab == NULL) {
2013 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2014 			    "could not enable LSO for %s (ENOMEM)\n",
2015 			    ill->ill_name);
2016 			return;
2017 		}
2018 	}
2019 
2020 	bzero(&lso, sizeof (lso));
2021 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2022 	    DLD_ENABLE)) == 0) {
2023 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2024 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2025 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2026 		ip1dbg(("ill_capability_lso_enable: interface %s "
2027 		    "has enabled LSO\n ", ill->ill_name));
2028 	} else {
2029 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2030 		ill->ill_lso_capab = NULL;
2031 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2032 	}
2033 }
2034 
2035 static void
2036 ill_capability_dld_enable(ill_t *ill)
2037 {
2038 	mac_perim_handle_t mph;
2039 
2040 	ASSERT(IAM_WRITER_ILL(ill));
2041 
2042 	if (ill->ill_isv6)
2043 		return;
2044 
2045 	ill_mac_perim_enter(ill, &mph);
2046 	if (!ill->ill_isv6) {
2047 		ill_capability_direct_enable(ill);
2048 		ill_capability_poll_enable(ill);
2049 		ill_capability_lso_enable(ill);
2050 	}
2051 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2052 	ill_mac_perim_exit(ill, mph);
2053 }
2054 
2055 static void
2056 ill_capability_dld_disable(ill_t *ill)
2057 {
2058 	ill_dld_capab_t	*idc;
2059 	ill_dld_direct_t *idd;
2060 	mac_perim_handle_t	mph;
2061 
2062 	ASSERT(IAM_WRITER_ILL(ill));
2063 
2064 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2065 		return;
2066 
2067 	ill_mac_perim_enter(ill, &mph);
2068 
2069 	idc = ill->ill_dld_capab;
2070 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2071 		/*
2072 		 * For performance we avoid locks in the transmit data path
2073 		 * and don't maintain a count of the number of threads using
2074 		 * direct calls. Thus some threads could be using direct
2075 		 * transmit calls to GLD, even after the capability mechanism
2076 		 * turns it off. This is still safe since the handles used in
2077 		 * the direct calls continue to be valid until the unplumb is
2078 		 * completed. Remove the callback that was added (1-time) at
2079 		 * capab enable time.
2080 		 */
2081 		mutex_enter(&ill->ill_lock);
2082 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2083 		mutex_exit(&ill->ill_lock);
2084 		if (ill->ill_flownotify_mh != NULL) {
2085 			idd = &idc->idc_direct;
2086 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2087 			    ill->ill_flownotify_mh);
2088 			ill->ill_flownotify_mh = NULL;
2089 		}
2090 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2091 		    NULL, DLD_DISABLE);
2092 	}
2093 
2094 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2095 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2096 		ip_squeue_clean_all(ill);
2097 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2098 		    NULL, DLD_DISABLE);
2099 	}
2100 
2101 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2102 		ASSERT(ill->ill_lso_capab != NULL);
2103 		/*
2104 		 * Clear the capability flag for LSO but retain the
2105 		 * ill_lso_capab structure since it's possible that another
2106 		 * thread is still referring to it.  The structure only gets
2107 		 * deallocated when we destroy the ill.
2108 		 */
2109 
2110 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2111 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2112 		    NULL, DLD_DISABLE);
2113 	}
2114 
2115 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2116 	ill_mac_perim_exit(ill, mph);
2117 }
2118 
2119 /*
2120  * Capability Negotiation protocol
2121  *
2122  * We don't wait for DLPI capability operations to finish during interface
2123  * bringup or teardown. Doing so would introduce more asynchrony and the
2124  * interface up/down operations will need multiple return and restarts.
2125  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2126  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2127  * exclusive operation won't start until the DLPI operations of the previous
2128  * exclusive operation complete.
2129  *
2130  * The capability state machine is shown below.
2131  *
2132  * state		next state		event, action
2133  *
2134  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2135  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2136  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2137  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2138  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2139  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2140  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2141  *						    ill_capability_probe.
2142  */
2143 
2144 /*
2145  * Dedicated thread started from ip_stack_init that handles capability
2146  * disable. This thread ensures the taskq dispatch does not fail by waiting
2147  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2148  * that direct calls to DLD are done in a cv_waitable context.
2149  */
2150 void
2151 ill_taskq_dispatch(ip_stack_t *ipst)
2152 {
2153 	callb_cpr_t cprinfo;
2154 	char 	name[64];
2155 	mblk_t	*mp;
2156 
2157 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2158 	    ipst->ips_netstack->netstack_stackid);
2159 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2160 	    name);
2161 	mutex_enter(&ipst->ips_capab_taskq_lock);
2162 
2163 	for (;;) {
2164 		mp = ipst->ips_capab_taskq_head;
2165 		while (mp != NULL) {
2166 			ipst->ips_capab_taskq_head = mp->b_next;
2167 			if (ipst->ips_capab_taskq_head == NULL)
2168 				ipst->ips_capab_taskq_tail = NULL;
2169 			mutex_exit(&ipst->ips_capab_taskq_lock);
2170 			mp->b_next = NULL;
2171 
2172 			VERIFY(taskq_dispatch(system_taskq,
2173 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2174 			mutex_enter(&ipst->ips_capab_taskq_lock);
2175 			mp = ipst->ips_capab_taskq_head;
2176 		}
2177 
2178 		if (ipst->ips_capab_taskq_quit)
2179 			break;
2180 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2181 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2182 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2183 	}
2184 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2185 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2186 	CALLB_CPR_EXIT(&cprinfo);
2187 	thread_exit();
2188 }
2189 
2190 /*
2191  * Consume a new-style hardware capabilities negotiation ack.
2192  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2193  */
2194 static void
2195 ill_capability_ack_thr(void *arg)
2196 {
2197 	mblk_t	*mp = arg;
2198 	dl_capability_ack_t *capp;
2199 	dl_capability_sub_t *subp, *endp;
2200 	ill_t	*ill;
2201 	boolean_t reneg;
2202 
2203 	ill = (ill_t *)mp->b_prev;
2204 	mp->b_prev = NULL;
2205 
2206 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2207 
2208 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2209 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2210 		/*
2211 		 * We have received the ack for our DL_CAPAB reset request.
2212 		 * There isnt' anything in the message that needs processing.
2213 		 * All message based capabilities have been disabled, now
2214 		 * do the function call based capability disable.
2215 		 */
2216 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2217 		ill_capability_dld_disable(ill);
2218 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2219 		if (reneg)
2220 			ill_capability_probe(ill);
2221 		goto done;
2222 	}
2223 
2224 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2225 		ill->ill_dlpi_capab_state = IDCS_OK;
2226 
2227 	capp = (dl_capability_ack_t *)mp->b_rptr;
2228 
2229 	if (capp->dl_sub_length == 0) {
2230 		/* no new-style capabilities */
2231 		goto done;
2232 	}
2233 
2234 	/* make sure the driver supplied correct dl_sub_length */
2235 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2236 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2237 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2238 		goto done;
2239 	}
2240 
2241 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2242 	/*
2243 	 * There are sub-capabilities. Process the ones we know about.
2244 	 * Loop until we don't have room for another sub-cap header..
2245 	 */
2246 	for (subp = SC(capp, capp->dl_sub_offset),
2247 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2248 	    subp <= endp;
2249 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2250 
2251 		switch (subp->dl_cap) {
2252 		case DL_CAPAB_ID_WRAPPER:
2253 			ill_capability_id_ack(ill, mp, subp);
2254 			break;
2255 		default:
2256 			ill_capability_dispatch(ill, mp, subp);
2257 			break;
2258 		}
2259 	}
2260 #undef SC
2261 done:
2262 	inet_freemsg(mp);
2263 	ill_capability_done(ill);
2264 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2265 }
2266 
2267 /*
2268  * This needs to be started in a taskq thread to provide a cv_waitable
2269  * context.
2270  */
2271 void
2272 ill_capability_ack(ill_t *ill, mblk_t *mp)
2273 {
2274 	ip_stack_t	*ipst = ill->ill_ipst;
2275 
2276 	mp->b_prev = (mblk_t *)ill;
2277 	ASSERT(mp->b_next == NULL);
2278 
2279 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2280 	    TQ_NOSLEEP) != 0)
2281 		return;
2282 
2283 	/*
2284 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2285 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2286 	 */
2287 	mutex_enter(&ipst->ips_capab_taskq_lock);
2288 	if (ipst->ips_capab_taskq_head == NULL) {
2289 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2290 		ipst->ips_capab_taskq_head = mp;
2291 	} else {
2292 		ipst->ips_capab_taskq_tail->b_next = mp;
2293 	}
2294 	ipst->ips_capab_taskq_tail = mp;
2295 
2296 	cv_signal(&ipst->ips_capab_taskq_cv);
2297 	mutex_exit(&ipst->ips_capab_taskq_lock);
2298 }
2299 
2300 /*
2301  * This routine is called to scan the fragmentation reassembly table for
2302  * the specified ILL for any packets that are starting to smell.
2303  * dead_interval is the maximum time in seconds that will be tolerated.  It
2304  * will either be the value specified in ip_g_frag_timeout, or zero if the
2305  * ILL is shutting down and it is time to blow everything off.
2306  *
2307  * It returns the number of seconds (as a time_t) that the next frag timer
2308  * should be scheduled for, 0 meaning that the timer doesn't need to be
2309  * re-started.  Note that the method of calculating next_timeout isn't
2310  * entirely accurate since time will flow between the time we grab
2311  * current_time and the time we schedule the next timeout.  This isn't a
2312  * big problem since this is the timer for sending an ICMP reassembly time
2313  * exceeded messages, and it doesn't have to be exactly accurate.
2314  *
2315  * This function is
2316  * sometimes called as writer, although this is not required.
2317  */
2318 time_t
2319 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2320 {
2321 	ipfb_t	*ipfb;
2322 	ipfb_t	*endp;
2323 	ipf_t	*ipf;
2324 	ipf_t	*ipfnext;
2325 	mblk_t	*mp;
2326 	time_t	current_time = gethrestime_sec();
2327 	time_t	next_timeout = 0;
2328 	uint32_t	hdr_length;
2329 	mblk_t	*send_icmp_head;
2330 	mblk_t	*send_icmp_head_v6;
2331 	ip_stack_t *ipst = ill->ill_ipst;
2332 	ip_recv_attr_t iras;
2333 
2334 	bzero(&iras, sizeof (iras));
2335 	iras.ira_flags = 0;
2336 	iras.ira_ill = iras.ira_rill = ill;
2337 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2338 	iras.ira_rifindex = iras.ira_ruifindex;
2339 
2340 	ipfb = ill->ill_frag_hash_tbl;
2341 	if (ipfb == NULL)
2342 		return (B_FALSE);
2343 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2344 	/* Walk the frag hash table. */
2345 	for (; ipfb < endp; ipfb++) {
2346 		send_icmp_head = NULL;
2347 		send_icmp_head_v6 = NULL;
2348 		mutex_enter(&ipfb->ipfb_lock);
2349 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2350 			time_t frag_time = current_time - ipf->ipf_timestamp;
2351 			time_t frag_timeout;
2352 
2353 			if (frag_time < dead_interval) {
2354 				/*
2355 				 * There are some outstanding fragments
2356 				 * that will timeout later.  Make note of
2357 				 * the time so that we can reschedule the
2358 				 * next timeout appropriately.
2359 				 */
2360 				frag_timeout = dead_interval - frag_time;
2361 				if (next_timeout == 0 ||
2362 				    frag_timeout < next_timeout) {
2363 					next_timeout = frag_timeout;
2364 				}
2365 				break;
2366 			}
2367 			/* Time's up.  Get it out of here. */
2368 			hdr_length = ipf->ipf_nf_hdr_len;
2369 			ipfnext = ipf->ipf_hash_next;
2370 			if (ipfnext)
2371 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2372 			*ipf->ipf_ptphn = ipfnext;
2373 			mp = ipf->ipf_mp->b_cont;
2374 			for (; mp; mp = mp->b_cont) {
2375 				/* Extra points for neatness. */
2376 				IP_REASS_SET_START(mp, 0);
2377 				IP_REASS_SET_END(mp, 0);
2378 			}
2379 			mp = ipf->ipf_mp->b_cont;
2380 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2381 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2382 			ipfb->ipfb_count -= ipf->ipf_count;
2383 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2384 			ipfb->ipfb_frag_pkts--;
2385 			/*
2386 			 * We do not send any icmp message from here because
2387 			 * we currently are holding the ipfb_lock for this
2388 			 * hash chain. If we try and send any icmp messages
2389 			 * from here we may end up via a put back into ip
2390 			 * trying to get the same lock, causing a recursive
2391 			 * mutex panic. Instead we build a list and send all
2392 			 * the icmp messages after we have dropped the lock.
2393 			 */
2394 			if (ill->ill_isv6) {
2395 				if (hdr_length != 0) {
2396 					mp->b_next = send_icmp_head_v6;
2397 					send_icmp_head_v6 = mp;
2398 				} else {
2399 					freemsg(mp);
2400 				}
2401 			} else {
2402 				if (hdr_length != 0) {
2403 					mp->b_next = send_icmp_head;
2404 					send_icmp_head = mp;
2405 				} else {
2406 					freemsg(mp);
2407 				}
2408 			}
2409 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2410 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2411 			freeb(ipf->ipf_mp);
2412 		}
2413 		mutex_exit(&ipfb->ipfb_lock);
2414 		/*
2415 		 * Now need to send any icmp messages that we delayed from
2416 		 * above.
2417 		 */
2418 		while (send_icmp_head_v6 != NULL) {
2419 			ip6_t *ip6h;
2420 
2421 			mp = send_icmp_head_v6;
2422 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2423 			mp->b_next = NULL;
2424 			ip6h = (ip6_t *)mp->b_rptr;
2425 			iras.ira_flags = 0;
2426 			/*
2427 			 * This will result in an incorrect ALL_ZONES zoneid
2428 			 * for multicast packets, but we
2429 			 * don't send ICMP errors for those in any case.
2430 			 */
2431 			iras.ira_zoneid =
2432 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2433 			    ill, ipst);
2434 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2435 			icmp_time_exceeded_v6(mp,
2436 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2437 			    &iras);
2438 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2439 		}
2440 		while (send_icmp_head != NULL) {
2441 			ipaddr_t dst;
2442 
2443 			mp = send_icmp_head;
2444 			send_icmp_head = send_icmp_head->b_next;
2445 			mp->b_next = NULL;
2446 
2447 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2448 
2449 			iras.ira_flags = IRAF_IS_IPV4;
2450 			/*
2451 			 * This will result in an incorrect ALL_ZONES zoneid
2452 			 * for broadcast and multicast packets, but we
2453 			 * don't send ICMP errors for those in any case.
2454 			 */
2455 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2456 			    ill, ipst);
2457 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2458 			icmp_time_exceeded(mp,
2459 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2460 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2461 		}
2462 	}
2463 	/*
2464 	 * A non-dying ILL will use the return value to decide whether to
2465 	 * restart the frag timer, and for how long.
2466 	 */
2467 	return (next_timeout);
2468 }
2469 
2470 /*
2471  * This routine is called when the approximate count of mblk memory used
2472  * for the specified ILL has exceeded max_count.
2473  */
2474 void
2475 ill_frag_prune(ill_t *ill, uint_t max_count)
2476 {
2477 	ipfb_t	*ipfb;
2478 	ipf_t	*ipf;
2479 	size_t	count;
2480 	clock_t now;
2481 
2482 	/*
2483 	 * If we are here within ip_min_frag_prune_time msecs remove
2484 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2485 	 * ill_frag_free_num_pkts.
2486 	 */
2487 	mutex_enter(&ill->ill_lock);
2488 	now = ddi_get_lbolt();
2489 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2490 	    (ip_min_frag_prune_time != 0 ?
2491 	    ip_min_frag_prune_time : msec_per_tick)) {
2492 
2493 		ill->ill_frag_free_num_pkts++;
2494 
2495 	} else {
2496 		ill->ill_frag_free_num_pkts = 0;
2497 	}
2498 	ill->ill_last_frag_clean_time = now;
2499 	mutex_exit(&ill->ill_lock);
2500 
2501 	/*
2502 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2503 	 */
2504 	if (ill->ill_frag_free_num_pkts != 0) {
2505 		int ix;
2506 
2507 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2508 			ipfb = &ill->ill_frag_hash_tbl[ix];
2509 			mutex_enter(&ipfb->ipfb_lock);
2510 			if (ipfb->ipfb_ipf != NULL) {
2511 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2512 				    ill->ill_frag_free_num_pkts);
2513 			}
2514 			mutex_exit(&ipfb->ipfb_lock);
2515 		}
2516 	}
2517 	/*
2518 	 * While the reassembly list for this ILL is too big, prune a fragment
2519 	 * queue by age, oldest first.
2520 	 */
2521 	while (ill->ill_frag_count > max_count) {
2522 		int	ix;
2523 		ipfb_t	*oipfb = NULL;
2524 		uint_t	oldest = UINT_MAX;
2525 
2526 		count = 0;
2527 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2528 			ipfb = &ill->ill_frag_hash_tbl[ix];
2529 			mutex_enter(&ipfb->ipfb_lock);
2530 			ipf = ipfb->ipfb_ipf;
2531 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2532 				oldest = ipf->ipf_gen;
2533 				oipfb = ipfb;
2534 			}
2535 			count += ipfb->ipfb_count;
2536 			mutex_exit(&ipfb->ipfb_lock);
2537 		}
2538 		if (oipfb == NULL)
2539 			break;
2540 
2541 		if (count <= max_count)
2542 			return;	/* Somebody beat us to it, nothing to do */
2543 		mutex_enter(&oipfb->ipfb_lock);
2544 		ipf = oipfb->ipfb_ipf;
2545 		if (ipf != NULL) {
2546 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2547 		}
2548 		mutex_exit(&oipfb->ipfb_lock);
2549 	}
2550 }
2551 
2552 /*
2553  * free 'free_cnt' fragmented packets starting at ipf.
2554  */
2555 void
2556 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2557 {
2558 	size_t	count;
2559 	mblk_t	*mp;
2560 	mblk_t	*tmp;
2561 	ipf_t **ipfp = ipf->ipf_ptphn;
2562 
2563 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2564 	ASSERT(ipfp != NULL);
2565 	ASSERT(ipf != NULL);
2566 
2567 	while (ipf != NULL && free_cnt-- > 0) {
2568 		count = ipf->ipf_count;
2569 		mp = ipf->ipf_mp;
2570 		ipf = ipf->ipf_hash_next;
2571 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2572 			IP_REASS_SET_START(tmp, 0);
2573 			IP_REASS_SET_END(tmp, 0);
2574 		}
2575 		atomic_add_32(&ill->ill_frag_count, -count);
2576 		ASSERT(ipfb->ipfb_count >= count);
2577 		ipfb->ipfb_count -= count;
2578 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2579 		ipfb->ipfb_frag_pkts--;
2580 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2581 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2582 		freemsg(mp);
2583 	}
2584 
2585 	if (ipf)
2586 		ipf->ipf_ptphn = ipfp;
2587 	ipfp[0] = ipf;
2588 }
2589 
2590 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2591 	"obsolete and may be removed in a future release of Solaris.  Use " \
2592 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2593 
2594 /*
2595  * For obsolete per-interface forwarding configuration;
2596  * called in response to ND_GET.
2597  */
2598 /* ARGSUSED */
2599 static int
2600 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2601 {
2602 	ill_t *ill = (ill_t *)cp;
2603 
2604 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2605 
2606 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2607 	return (0);
2608 }
2609 
2610 /*
2611  * For obsolete per-interface forwarding configuration;
2612  * called in response to ND_SET.
2613  */
2614 /* ARGSUSED */
2615 static int
2616 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2617     cred_t *ioc_cr)
2618 {
2619 	long value;
2620 	int retval;
2621 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2622 
2623 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2624 
2625 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2626 	    value < 0 || value > 1) {
2627 		return (EINVAL);
2628 	}
2629 
2630 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2631 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2632 	rw_exit(&ipst->ips_ill_g_lock);
2633 	return (retval);
2634 }
2635 
2636 /*
2637  * Helper function for ill_forward_set().
2638  */
2639 static void
2640 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2641 {
2642 	ip_stack_t	*ipst = ill->ill_ipst;
2643 
2644 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2645 
2646 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2647 	    (enable ? "Enabling" : "Disabling"),
2648 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2649 	mutex_enter(&ill->ill_lock);
2650 	if (enable)
2651 		ill->ill_flags |= ILLF_ROUTER;
2652 	else
2653 		ill->ill_flags &= ~ILLF_ROUTER;
2654 	mutex_exit(&ill->ill_lock);
2655 	if (ill->ill_isv6)
2656 		ill_set_nce_router_flags(ill, enable);
2657 	/* Notify routing socket listeners of this change. */
2658 	if (ill->ill_ipif != NULL)
2659 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2660 }
2661 
2662 /*
2663  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2664  * socket messages for each interface whose flags we change.
2665  */
2666 int
2667 ill_forward_set(ill_t *ill, boolean_t enable)
2668 {
2669 	ipmp_illgrp_t *illg;
2670 	ip_stack_t *ipst = ill->ill_ipst;
2671 
2672 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2673 
2674 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2675 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2676 		return (0);
2677 
2678 	if (IS_LOOPBACK(ill))
2679 		return (EINVAL);
2680 
2681 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2682 		/*
2683 		 * Update all of the interfaces in the group.
2684 		 */
2685 		illg = ill->ill_grp;
2686 		ill = list_head(&illg->ig_if);
2687 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2688 			ill_forward_set_on_ill(ill, enable);
2689 
2690 		/*
2691 		 * Update the IPMP meta-interface.
2692 		 */
2693 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2694 		return (0);
2695 	}
2696 
2697 	ill_forward_set_on_ill(ill, enable);
2698 	return (0);
2699 }
2700 
2701 /*
2702  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2703  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2704  * set or clear.
2705  */
2706 static void
2707 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2708 {
2709 	ipif_t *ipif;
2710 	ncec_t *ncec;
2711 	nce_t *nce;
2712 
2713 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2714 		/*
2715 		 * NOTE: we match across the illgrp because nce's for
2716 		 * addresses on IPMP interfaces have an nce_ill that points to
2717 		 * the bound underlying ill.
2718 		 */
2719 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2720 		if (nce != NULL) {
2721 			ncec = nce->nce_common;
2722 			mutex_enter(&ncec->ncec_lock);
2723 			if (enable)
2724 				ncec->ncec_flags |= NCE_F_ISROUTER;
2725 			else
2726 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2727 			mutex_exit(&ncec->ncec_lock);
2728 			nce_refrele(nce);
2729 		}
2730 	}
2731 }
2732 
2733 /*
2734  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2735  * for this ill.  Make sure the v6/v4 question has been answered about this
2736  * ill.  The creation of this ndd variable is only for backwards compatibility.
2737  * The preferred way to control per-interface IP forwarding is through the
2738  * ILLF_ROUTER interface flag.
2739  */
2740 static int
2741 ill_set_ndd_name(ill_t *ill)
2742 {
2743 	char *suffix;
2744 	ip_stack_t	*ipst = ill->ill_ipst;
2745 
2746 	ASSERT(IAM_WRITER_ILL(ill));
2747 
2748 	if (ill->ill_isv6)
2749 		suffix = ipv6_forward_suffix;
2750 	else
2751 		suffix = ipv4_forward_suffix;
2752 
2753 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2754 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2755 	/*
2756 	 * Copies over the '\0'.
2757 	 * Note that strlen(suffix) is always bounded.
2758 	 */
2759 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2760 	    strlen(suffix) + 1);
2761 
2762 	/*
2763 	 * Use of the nd table requires holding the reader lock.
2764 	 * Modifying the nd table thru nd_load/nd_unload requires
2765 	 * the writer lock.
2766 	 */
2767 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2768 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2769 	    nd_ill_forward_set, (caddr_t)ill)) {
2770 		/*
2771 		 * If the nd_load failed, it only meant that it could not
2772 		 * allocate a new bunch of room for further NDD expansion.
2773 		 * Because of that, the ill_ndd_name will be set to 0, and
2774 		 * this interface is at the mercy of the global ip_forwarding
2775 		 * variable.
2776 		 */
2777 		rw_exit(&ipst->ips_ip_g_nd_lock);
2778 		ill->ill_ndd_name = NULL;
2779 		return (ENOMEM);
2780 	}
2781 	rw_exit(&ipst->ips_ip_g_nd_lock);
2782 	return (0);
2783 }
2784 
2785 /*
2786  * Intializes the context structure and returns the first ill in the list
2787  * cuurently start_list and end_list can have values:
2788  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2789  * IP_V4_G_HEAD		Traverse IPV4 list only.
2790  * IP_V6_G_HEAD		Traverse IPV6 list only.
2791  */
2792 
2793 /*
2794  * We don't check for CONDEMNED ills here. Caller must do that if
2795  * necessary under the ill lock.
2796  */
2797 ill_t *
2798 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2799     ip_stack_t *ipst)
2800 {
2801 	ill_if_t *ifp;
2802 	ill_t *ill;
2803 	avl_tree_t *avl_tree;
2804 
2805 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2806 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2807 
2808 	/*
2809 	 * setup the lists to search
2810 	 */
2811 	if (end_list != MAX_G_HEADS) {
2812 		ctx->ctx_current_list = start_list;
2813 		ctx->ctx_last_list = end_list;
2814 	} else {
2815 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2816 		ctx->ctx_current_list = 0;
2817 	}
2818 
2819 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2820 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2821 		if (ifp != (ill_if_t *)
2822 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2823 			avl_tree = &ifp->illif_avl_by_ppa;
2824 			ill = avl_first(avl_tree);
2825 			/*
2826 			 * ill is guaranteed to be non NULL or ifp should have
2827 			 * not existed.
2828 			 */
2829 			ASSERT(ill != NULL);
2830 			return (ill);
2831 		}
2832 		ctx->ctx_current_list++;
2833 	}
2834 
2835 	return (NULL);
2836 }
2837 
2838 /*
2839  * returns the next ill in the list. ill_first() must have been called
2840  * before calling ill_next() or bad things will happen.
2841  */
2842 
2843 /*
2844  * We don't check for CONDEMNED ills here. Caller must do that if
2845  * necessary under the ill lock.
2846  */
2847 ill_t *
2848 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2849 {
2850 	ill_if_t *ifp;
2851 	ill_t *ill;
2852 	ip_stack_t	*ipst = lastill->ill_ipst;
2853 
2854 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2855 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2856 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2857 	    AVL_AFTER)) != NULL) {
2858 		return (ill);
2859 	}
2860 
2861 	/* goto next ill_ifp in the list. */
2862 	ifp = lastill->ill_ifptr->illif_next;
2863 
2864 	/* make sure not at end of circular list */
2865 	while (ifp ==
2866 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2867 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2868 			return (NULL);
2869 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2870 	}
2871 
2872 	return (avl_first(&ifp->illif_avl_by_ppa));
2873 }
2874 
2875 /*
2876  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2877  * The final number (PPA) must not have any leading zeros.  Upon success, a
2878  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2879  */
2880 static char *
2881 ill_get_ppa_ptr(char *name)
2882 {
2883 	int namelen = strlen(name);
2884 	int end_ndx = namelen - 1;
2885 	int ppa_ndx, i;
2886 
2887 	/*
2888 	 * Check that the first character is [a-zA-Z], and that the last
2889 	 * character is [0-9].
2890 	 */
2891 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2892 		return (NULL);
2893 
2894 	/*
2895 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2896 	 */
2897 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2898 		if (!isdigit(name[ppa_ndx - 1]))
2899 			break;
2900 
2901 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2902 		return (NULL);
2903 
2904 	/*
2905 	 * Check that the intermediate characters are [a-z0-9.]
2906 	 */
2907 	for (i = 1; i < ppa_ndx; i++) {
2908 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2909 		    name[i] != '.' && name[i] != '_') {
2910 			return (NULL);
2911 		}
2912 	}
2913 
2914 	return (name + ppa_ndx);
2915 }
2916 
2917 /*
2918  * use avl tree to locate the ill.
2919  */
2920 static ill_t *
2921 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2922 {
2923 	char *ppa_ptr = NULL;
2924 	int len;
2925 	uint_t ppa;
2926 	ill_t *ill = NULL;
2927 	ill_if_t *ifp;
2928 	int list;
2929 
2930 	/*
2931 	 * get ppa ptr
2932 	 */
2933 	if (isv6)
2934 		list = IP_V6_G_HEAD;
2935 	else
2936 		list = IP_V4_G_HEAD;
2937 
2938 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2939 		return (NULL);
2940 	}
2941 
2942 	len = ppa_ptr - name + 1;
2943 
2944 	ppa = stoi(&ppa_ptr);
2945 
2946 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2947 
2948 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2949 		/*
2950 		 * match is done on len - 1 as the name is not null
2951 		 * terminated it contains ppa in addition to the interface
2952 		 * name.
2953 		 */
2954 		if ((ifp->illif_name_len == len) &&
2955 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2956 			break;
2957 		} else {
2958 			ifp = ifp->illif_next;
2959 		}
2960 	}
2961 
2962 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2963 		/*
2964 		 * Even the interface type does not exist.
2965 		 */
2966 		return (NULL);
2967 	}
2968 
2969 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2970 	if (ill != NULL) {
2971 		mutex_enter(&ill->ill_lock);
2972 		if (ILL_CAN_LOOKUP(ill)) {
2973 			ill_refhold_locked(ill);
2974 			mutex_exit(&ill->ill_lock);
2975 			return (ill);
2976 		}
2977 		mutex_exit(&ill->ill_lock);
2978 	}
2979 	return (NULL);
2980 }
2981 
2982 /*
2983  * comparison function for use with avl.
2984  */
2985 static int
2986 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2987 {
2988 	uint_t ppa;
2989 	uint_t ill_ppa;
2990 
2991 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2992 
2993 	ppa = *((uint_t *)ppa_ptr);
2994 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2995 	/*
2996 	 * We want the ill with the lowest ppa to be on the
2997 	 * top.
2998 	 */
2999 	if (ill_ppa < ppa)
3000 		return (1);
3001 	if (ill_ppa > ppa)
3002 		return (-1);
3003 	return (0);
3004 }
3005 
3006 /*
3007  * remove an interface type from the global list.
3008  */
3009 static void
3010 ill_delete_interface_type(ill_if_t *interface)
3011 {
3012 	ASSERT(interface != NULL);
3013 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3014 
3015 	avl_destroy(&interface->illif_avl_by_ppa);
3016 	if (interface->illif_ppa_arena != NULL)
3017 		vmem_destroy(interface->illif_ppa_arena);
3018 
3019 	remque(interface);
3020 
3021 	mi_free(interface);
3022 }
3023 
3024 /*
3025  * remove ill from the global list.
3026  */
3027 static void
3028 ill_glist_delete(ill_t *ill)
3029 {
3030 	ip_stack_t	*ipst;
3031 	phyint_t	*phyi;
3032 
3033 	if (ill == NULL)
3034 		return;
3035 	ipst = ill->ill_ipst;
3036 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3037 
3038 	/*
3039 	 * If the ill was never inserted into the AVL tree
3040 	 * we skip the if branch.
3041 	 */
3042 	if (ill->ill_ifptr != NULL) {
3043 		/*
3044 		 * remove from AVL tree and free ppa number
3045 		 */
3046 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3047 
3048 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3049 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3050 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3051 		}
3052 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3053 			ill_delete_interface_type(ill->ill_ifptr);
3054 		}
3055 
3056 		/*
3057 		 * Indicate ill is no longer in the list.
3058 		 */
3059 		ill->ill_ifptr = NULL;
3060 		ill->ill_name_length = 0;
3061 		ill->ill_name[0] = '\0';
3062 		ill->ill_ppa = UINT_MAX;
3063 	}
3064 
3065 	/* Generate one last event for this ill. */
3066 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3067 	    ill->ill_name_length);
3068 
3069 	ASSERT(ill->ill_phyint != NULL);
3070 	phyi = ill->ill_phyint;
3071 	ill->ill_phyint = NULL;
3072 
3073 	/*
3074 	 * ill_init allocates a phyint always to store the copy
3075 	 * of flags relevant to phyint. At that point in time, we could
3076 	 * not assign the name and hence phyint_illv4/v6 could not be
3077 	 * initialized. Later in ipif_set_values, we assign the name to
3078 	 * the ill, at which point in time we assign phyint_illv4/v6.
3079 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3080 	 */
3081 	if (ill->ill_flags & ILLF_IPV6)
3082 		phyi->phyint_illv6 = NULL;
3083 	else
3084 		phyi->phyint_illv4 = NULL;
3085 
3086 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3087 		rw_exit(&ipst->ips_ill_g_lock);
3088 		return;
3089 	}
3090 
3091 	/*
3092 	 * There are no ills left on this phyint; pull it out of the phyint
3093 	 * avl trees, and free it.
3094 	 */
3095 	if (phyi->phyint_ifindex > 0) {
3096 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3097 		    phyi);
3098 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3099 		    phyi);
3100 	}
3101 	rw_exit(&ipst->ips_ill_g_lock);
3102 
3103 	phyint_free(phyi);
3104 }
3105 
3106 /*
3107  * allocate a ppa, if the number of plumbed interfaces of this type are
3108  * less than ill_no_arena do a linear search to find a unused ppa.
3109  * When the number goes beyond ill_no_arena switch to using an arena.
3110  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3111  * is the return value for an error condition, so allocation starts at one
3112  * and is decremented by one.
3113  */
3114 static int
3115 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3116 {
3117 	ill_t *tmp_ill;
3118 	uint_t start, end;
3119 	int ppa;
3120 
3121 	if (ifp->illif_ppa_arena == NULL &&
3122 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3123 		/*
3124 		 * Create an arena.
3125 		 */
3126 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3127 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3128 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3129 			/* allocate what has already been assigned */
3130 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3131 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3132 		    tmp_ill, AVL_AFTER)) {
3133 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3134 			    1,		/* size */
3135 			    1,		/* align/quantum */
3136 			    0,		/* phase */
3137 			    0,		/* nocross */
3138 			    /* minaddr */
3139 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3140 			    /* maxaddr */
3141 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3142 			    VM_NOSLEEP|VM_FIRSTFIT);
3143 			if (ppa == 0) {
3144 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3145 				    " failed while switching"));
3146 				vmem_destroy(ifp->illif_ppa_arena);
3147 				ifp->illif_ppa_arena = NULL;
3148 				break;
3149 			}
3150 		}
3151 	}
3152 
3153 	if (ifp->illif_ppa_arena != NULL) {
3154 		if (ill->ill_ppa == UINT_MAX) {
3155 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3156 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3157 			if (ppa == 0)
3158 				return (EAGAIN);
3159 			ill->ill_ppa = --ppa;
3160 		} else {
3161 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3162 			    1, 		/* size */
3163 			    1, 		/* align/quantum */
3164 			    0, 		/* phase */
3165 			    0, 		/* nocross */
3166 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3167 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3168 			    VM_NOSLEEP|VM_FIRSTFIT);
3169 			/*
3170 			 * Most likely the allocation failed because
3171 			 * the requested ppa was in use.
3172 			 */
3173 			if (ppa == 0)
3174 				return (EEXIST);
3175 		}
3176 		return (0);
3177 	}
3178 
3179 	/*
3180 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3181 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3182 	 */
3183 	if (ill->ill_ppa == UINT_MAX) {
3184 		end = UINT_MAX - 1;
3185 		start = 0;
3186 	} else {
3187 		end = start = ill->ill_ppa;
3188 	}
3189 
3190 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3191 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3192 		if (start++ >= end) {
3193 			if (ill->ill_ppa == UINT_MAX)
3194 				return (EAGAIN);
3195 			else
3196 				return (EEXIST);
3197 		}
3198 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3199 	}
3200 	ill->ill_ppa = start;
3201 	return (0);
3202 }
3203 
3204 /*
3205  * Insert ill into the list of configured ill's. Once this function completes,
3206  * the ill is globally visible and is available through lookups. More precisely
3207  * this happens after the caller drops the ill_g_lock.
3208  */
3209 static int
3210 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3211 {
3212 	ill_if_t *ill_interface;
3213 	avl_index_t where = 0;
3214 	int error;
3215 	int name_length;
3216 	int index;
3217 	boolean_t check_length = B_FALSE;
3218 	ip_stack_t	*ipst = ill->ill_ipst;
3219 
3220 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3221 
3222 	name_length = mi_strlen(name) + 1;
3223 
3224 	if (isv6)
3225 		index = IP_V6_G_HEAD;
3226 	else
3227 		index = IP_V4_G_HEAD;
3228 
3229 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3230 	/*
3231 	 * Search for interface type based on name
3232 	 */
3233 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3234 		if ((ill_interface->illif_name_len == name_length) &&
3235 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3236 			break;
3237 		}
3238 		ill_interface = ill_interface->illif_next;
3239 	}
3240 
3241 	/*
3242 	 * Interface type not found, create one.
3243 	 */
3244 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3245 		ill_g_head_t ghead;
3246 
3247 		/*
3248 		 * allocate ill_if_t structure
3249 		 */
3250 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3251 		if (ill_interface == NULL) {
3252 			return (ENOMEM);
3253 		}
3254 
3255 		(void) strcpy(ill_interface->illif_name, name);
3256 		ill_interface->illif_name_len = name_length;
3257 
3258 		avl_create(&ill_interface->illif_avl_by_ppa,
3259 		    ill_compare_ppa, sizeof (ill_t),
3260 		    offsetof(struct ill_s, ill_avl_byppa));
3261 
3262 		/*
3263 		 * link the structure in the back to maintain order
3264 		 * of configuration for ifconfig output.
3265 		 */
3266 		ghead = ipst->ips_ill_g_heads[index];
3267 		insque(ill_interface, ghead.ill_g_list_tail);
3268 	}
3269 
3270 	if (ill->ill_ppa == UINT_MAX)
3271 		check_length = B_TRUE;
3272 
3273 	error = ill_alloc_ppa(ill_interface, ill);
3274 	if (error != 0) {
3275 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3276 			ill_delete_interface_type(ill->ill_ifptr);
3277 		return (error);
3278 	}
3279 
3280 	/*
3281 	 * When the ppa is choosen by the system, check that there is
3282 	 * enough space to insert ppa. if a specific ppa was passed in this
3283 	 * check is not required as the interface name passed in will have
3284 	 * the right ppa in it.
3285 	 */
3286 	if (check_length) {
3287 		/*
3288 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3289 		 */
3290 		char buf[sizeof (uint_t) * 3];
3291 
3292 		/*
3293 		 * convert ppa to string to calculate the amount of space
3294 		 * required for it in the name.
3295 		 */
3296 		numtos(ill->ill_ppa, buf);
3297 
3298 		/* Do we have enough space to insert ppa ? */
3299 
3300 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3301 			/* Free ppa and interface type struct */
3302 			if (ill_interface->illif_ppa_arena != NULL) {
3303 				vmem_free(ill_interface->illif_ppa_arena,
3304 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3305 			}
3306 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3307 				ill_delete_interface_type(ill->ill_ifptr);
3308 
3309 			return (EINVAL);
3310 		}
3311 	}
3312 
3313 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3314 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3315 
3316 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3317 	    &where);
3318 	ill->ill_ifptr = ill_interface;
3319 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3320 
3321 	ill_phyint_reinit(ill);
3322 	return (0);
3323 }
3324 
3325 /* Initialize the per phyint ipsq used for serialization */
3326 static boolean_t
3327 ipsq_init(ill_t *ill, boolean_t enter)
3328 {
3329 	ipsq_t  *ipsq;
3330 	ipxop_t	*ipx;
3331 
3332 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3333 		return (B_FALSE);
3334 
3335 	ill->ill_phyint->phyint_ipsq = ipsq;
3336 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3337 	ipx->ipx_ipsq = ipsq;
3338 	ipsq->ipsq_next = ipsq;
3339 	ipsq->ipsq_phyint = ill->ill_phyint;
3340 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3341 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3342 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3343 	if (enter) {
3344 		ipx->ipx_writer = curthread;
3345 		ipx->ipx_forced = B_FALSE;
3346 		ipx->ipx_reentry_cnt = 1;
3347 #ifdef DEBUG
3348 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3349 #endif
3350 	}
3351 	return (B_TRUE);
3352 }
3353 
3354 /*
3355  * ill_init is called by ip_open when a device control stream is opened.
3356  * It does a few initializations, and shoots a DL_INFO_REQ message down
3357  * to the driver.  The response is later picked up in ip_rput_dlpi and
3358  * used to set up default mechanisms for talking to the driver.  (Always
3359  * called as writer.)
3360  *
3361  * If this function returns error, ip_open will call ip_close which in
3362  * turn will call ill_delete to clean up any memory allocated here that
3363  * is not yet freed.
3364  */
3365 int
3366 ill_init(queue_t *q, ill_t *ill)
3367 {
3368 	int	count;
3369 	dl_info_req_t	*dlir;
3370 	mblk_t	*info_mp;
3371 	uchar_t *frag_ptr;
3372 
3373 	/*
3374 	 * The ill is initialized to zero by mi_alloc*(). In addition
3375 	 * some fields already contain valid values, initialized in
3376 	 * ip_open(), before we reach here.
3377 	 */
3378 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3379 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3380 	ill->ill_saved_ire_cnt = 0;
3381 
3382 	ill->ill_rq = q;
3383 	ill->ill_wq = WR(q);
3384 
3385 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3386 	    BPRI_HI);
3387 	if (info_mp == NULL)
3388 		return (ENOMEM);
3389 
3390 	/*
3391 	 * Allocate sufficient space to contain our fragment hash table and
3392 	 * the device name.
3393 	 */
3394 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3395 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
3396 	if (frag_ptr == NULL) {
3397 		freemsg(info_mp);
3398 		return (ENOMEM);
3399 	}
3400 	ill->ill_frag_ptr = frag_ptr;
3401 	ill->ill_frag_free_num_pkts = 0;
3402 	ill->ill_last_frag_clean_time = 0;
3403 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3404 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3405 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3406 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3407 		    NULL, MUTEX_DEFAULT, NULL);
3408 	}
3409 
3410 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3411 	if (ill->ill_phyint == NULL) {
3412 		freemsg(info_mp);
3413 		mi_free(frag_ptr);
3414 		return (ENOMEM);
3415 	}
3416 
3417 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3418 	/*
3419 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3420 	 * at this point because of the following reason. If we can't
3421 	 * enter the ipsq at some point and cv_wait, the writer that
3422 	 * wakes us up tries to locate us using the list of all phyints
3423 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3424 	 * If we don't set it now, we risk a missed wakeup.
3425 	 */
3426 	ill->ill_phyint->phyint_illv4 = ill;
3427 	ill->ill_ppa = UINT_MAX;
3428 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3429 
3430 	ill_set_inputfn(ill);
3431 
3432 	if (!ipsq_init(ill, B_TRUE)) {
3433 		freemsg(info_mp);
3434 		mi_free(frag_ptr);
3435 		mi_free(ill->ill_phyint);
3436 		return (ENOMEM);
3437 	}
3438 
3439 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3440 
3441 	/* Frag queue limit stuff */
3442 	ill->ill_frag_count = 0;
3443 	ill->ill_ipf_gen = 0;
3444 
3445 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3446 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3447 	ill->ill_global_timer = INFINITY;
3448 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3449 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3450 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3451 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3452 
3453 	/*
3454 	 * Initialize IPv6 configuration variables.  The IP module is always
3455 	 * opened as an IPv4 module.  Instead tracking down the cases where
3456 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3457 	 * here for convenience, this has no effect until the ill is set to do
3458 	 * IPv6.
3459 	 */
3460 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3461 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3462 	ill->ill_max_buf = ND_MAX_Q;
3463 	ill->ill_refcnt = 0;
3464 
3465 	/* Send down the Info Request to the driver. */
3466 	info_mp->b_datap->db_type = M_PCPROTO;
3467 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3468 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3469 	dlir->dl_primitive = DL_INFO_REQ;
3470 
3471 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3472 
3473 	qprocson(q);
3474 	ill_dlpi_send(ill, info_mp);
3475 
3476 	return (0);
3477 }
3478 
3479 /*
3480  * ill_dls_info
3481  * creates datalink socket info from the device.
3482  */
3483 int
3484 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3485 {
3486 	size_t	len;
3487 
3488 	sdl->sdl_family = AF_LINK;
3489 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3490 	sdl->sdl_type = ill->ill_type;
3491 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3492 	len = strlen(sdl->sdl_data);
3493 	ASSERT(len < 256);
3494 	sdl->sdl_nlen = (uchar_t)len;
3495 	sdl->sdl_alen = ill->ill_phys_addr_length;
3496 	sdl->sdl_slen = 0;
3497 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3498 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3499 
3500 	return (sizeof (struct sockaddr_dl));
3501 }
3502 
3503 /*
3504  * ill_xarp_info
3505  * creates xarp info from the device.
3506  */
3507 static int
3508 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3509 {
3510 	sdl->sdl_family = AF_LINK;
3511 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3512 	sdl->sdl_type = ill->ill_type;
3513 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3514 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3515 	sdl->sdl_alen = ill->ill_phys_addr_length;
3516 	sdl->sdl_slen = 0;
3517 	return (sdl->sdl_nlen);
3518 }
3519 
3520 static int
3521 loopback_kstat_update(kstat_t *ksp, int rw)
3522 {
3523 	kstat_named_t *kn;
3524 	netstackid_t	stackid;
3525 	netstack_t	*ns;
3526 	ip_stack_t	*ipst;
3527 
3528 	if (ksp == NULL || ksp->ks_data == NULL)
3529 		return (EIO);
3530 
3531 	if (rw == KSTAT_WRITE)
3532 		return (EACCES);
3533 
3534 	kn = KSTAT_NAMED_PTR(ksp);
3535 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3536 
3537 	ns = netstack_find_by_stackid(stackid);
3538 	if (ns == NULL)
3539 		return (-1);
3540 
3541 	ipst = ns->netstack_ip;
3542 	if (ipst == NULL) {
3543 		netstack_rele(ns);
3544 		return (-1);
3545 	}
3546 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3547 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3548 	netstack_rele(ns);
3549 	return (0);
3550 }
3551 
3552 /*
3553  * Has ifindex been plumbed already?
3554  */
3555 static boolean_t
3556 phyint_exists(uint_t index, ip_stack_t *ipst)
3557 {
3558 	ASSERT(index != 0);
3559 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3560 
3561 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3562 	    &index, NULL) != NULL);
3563 }
3564 
3565 /* Pick a unique ifindex */
3566 boolean_t
3567 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3568 {
3569 	uint_t starting_index;
3570 
3571 	if (!ipst->ips_ill_index_wrap) {
3572 		*indexp = ipst->ips_ill_index++;
3573 		if (ipst->ips_ill_index == 0) {
3574 			/* Reached the uint_t limit Next time wrap  */
3575 			ipst->ips_ill_index_wrap = B_TRUE;
3576 		}
3577 		return (B_TRUE);
3578 	}
3579 
3580 	/*
3581 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3582 	 * at this point and don't want to call any function that attempts
3583 	 * to get the lock again.
3584 	 */
3585 	starting_index = ipst->ips_ill_index++;
3586 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3587 		if (ipst->ips_ill_index != 0 &&
3588 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3589 			/* found unused index - use it */
3590 			*indexp = ipst->ips_ill_index;
3591 			return (B_TRUE);
3592 		}
3593 	}
3594 
3595 	/*
3596 	 * all interface indicies are inuse.
3597 	 */
3598 	return (B_FALSE);
3599 }
3600 
3601 /*
3602  * Assign a unique interface index for the phyint.
3603  */
3604 static boolean_t
3605 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3606 {
3607 	ASSERT(phyi->phyint_ifindex == 0);
3608 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3609 }
3610 
3611 /*
3612  * Initialize the flags on `phyi' as per the provided mactype.
3613  */
3614 static void
3615 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3616 {
3617 	uint64_t flags = 0;
3618 
3619 	/*
3620 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3621 	 * we always presume the underlying hardware is working and set
3622 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3623 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3624 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3625 	 */
3626 	if (mactype == SUNW_DL_IPMP)
3627 		flags |= PHYI_FAILED;
3628 	else
3629 		flags |= PHYI_RUNNING;
3630 
3631 	switch (mactype) {
3632 	case SUNW_DL_VNI:
3633 		flags |= PHYI_VIRTUAL;
3634 		break;
3635 	case SUNW_DL_IPMP:
3636 		flags |= PHYI_IPMP;
3637 		break;
3638 	case DL_LOOP:
3639 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3640 		break;
3641 	}
3642 
3643 	mutex_enter(&phyi->phyint_lock);
3644 	phyi->phyint_flags |= flags;
3645 	mutex_exit(&phyi->phyint_lock);
3646 }
3647 
3648 /*
3649  * Return a pointer to the ill which matches the supplied name.  Note that
3650  * the ill name length includes the null termination character.  (May be
3651  * called as writer.)
3652  * If do_alloc and the interface is "lo0" it will be automatically created.
3653  * Cannot bump up reference on condemned ills. So dup detect can't be done
3654  * using this func.
3655  */
3656 ill_t *
3657 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3658     boolean_t *did_alloc, ip_stack_t *ipst)
3659 {
3660 	ill_t	*ill;
3661 	ipif_t	*ipif;
3662 	ipsq_t	*ipsq;
3663 	kstat_named_t	*kn;
3664 	boolean_t isloopback;
3665 	in6_addr_t ov6addr;
3666 
3667 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3668 
3669 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3670 	ill = ill_find_by_name(name, isv6, ipst);
3671 	rw_exit(&ipst->ips_ill_g_lock);
3672 	if (ill != NULL)
3673 		return (ill);
3674 
3675 	/*
3676 	 * Couldn't find it.  Does this happen to be a lookup for the
3677 	 * loopback device and are we allowed to allocate it?
3678 	 */
3679 	if (!isloopback || !do_alloc)
3680 		return (NULL);
3681 
3682 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3683 	ill = ill_find_by_name(name, isv6, ipst);
3684 	if (ill != NULL) {
3685 		rw_exit(&ipst->ips_ill_g_lock);
3686 		return (ill);
3687 	}
3688 
3689 	/* Create the loopback device on demand */
3690 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3691 	    sizeof (ipif_loopback_name), BPRI_MED));
3692 	if (ill == NULL)
3693 		goto done;
3694 
3695 	*ill = ill_null;
3696 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3697 	ill->ill_ipst = ipst;
3698 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3699 	netstack_hold(ipst->ips_netstack);
3700 	/*
3701 	 * For exclusive stacks we set the zoneid to zero
3702 	 * to make IP operate as if in the global zone.
3703 	 */
3704 	ill->ill_zoneid = GLOBAL_ZONEID;
3705 
3706 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3707 	if (ill->ill_phyint == NULL)
3708 		goto done;
3709 
3710 	if (isv6)
3711 		ill->ill_phyint->phyint_illv6 = ill;
3712 	else
3713 		ill->ill_phyint->phyint_illv4 = ill;
3714 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3715 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3716 
3717 	if (isv6) {
3718 		ill->ill_isv6 = B_TRUE;
3719 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3720 	} else {
3721 		ill->ill_max_frag = ip_loopback_mtuplus;
3722 	}
3723 	if (!ill_allocate_mibs(ill))
3724 		goto done;
3725 	ill->ill_current_frag = ill->ill_max_frag;
3726 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3727 	/*
3728 	 * ipif_loopback_name can't be pointed at directly because its used
3729 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3730 	 * from the glist, ill_glist_delete() sets the first character of
3731 	 * ill_name to '\0'.
3732 	 */
3733 	ill->ill_name = (char *)ill + sizeof (*ill);
3734 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3735 	ill->ill_name_length = sizeof (ipif_loopback_name);
3736 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3737 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3738 
3739 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3740 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3741 	ill->ill_global_timer = INFINITY;
3742 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3743 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3744 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3745 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3746 
3747 	/* No resolver here. */
3748 	ill->ill_net_type = IRE_LOOPBACK;
3749 
3750 	/* Initialize the ipsq */
3751 	if (!ipsq_init(ill, B_FALSE))
3752 		goto done;
3753 
3754 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
3755 	if (ipif == NULL)
3756 		goto done;
3757 
3758 	ill->ill_flags = ILLF_MULTICAST;
3759 
3760 	ov6addr = ipif->ipif_v6lcl_addr;
3761 	/* Set up default loopback address and mask. */
3762 	if (!isv6) {
3763 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3764 
3765 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3766 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3767 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3768 		    ipif->ipif_v6subnet);
3769 		ill->ill_flags |= ILLF_IPV4;
3770 	} else {
3771 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3772 		ipif->ipif_v6net_mask = ipv6_all_ones;
3773 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3774 		    ipif->ipif_v6subnet);
3775 		ill->ill_flags |= ILLF_IPV6;
3776 	}
3777 
3778 	/*
3779 	 * Chain us in at the end of the ill list. hold the ill
3780 	 * before we make it globally visible. 1 for the lookup.
3781 	 */
3782 	ill->ill_refcnt = 0;
3783 	ill_refhold(ill);
3784 
3785 	ill->ill_frag_count = 0;
3786 	ill->ill_frag_free_num_pkts = 0;
3787 	ill->ill_last_frag_clean_time = 0;
3788 
3789 	ipsq = ill->ill_phyint->phyint_ipsq;
3790 
3791 	ill_set_inputfn(ill);
3792 
3793 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3794 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3795 
3796 	/* Let SCTP know so that it can add this to its list */
3797 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3798 
3799 	/*
3800 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3801 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3802 	 * requires to be after ill_glist_insert() since we need the
3803 	 * ill_index set. Pass on ipv6_loopback as the old address.
3804 	 */
3805 	sctp_update_ipif_addr(ipif, ov6addr);
3806 
3807 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3808 
3809 	/*
3810 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3811 	 * If so, free our original one.
3812 	 */
3813 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3814 		ipsq_delete(ipsq);
3815 
3816 	if (ipst->ips_loopback_ksp == NULL) {
3817 		/* Export loopback interface statistics */
3818 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3819 		    ipif_loopback_name, "net",
3820 		    KSTAT_TYPE_NAMED, 2, 0,
3821 		    ipst->ips_netstack->netstack_stackid);
3822 		if (ipst->ips_loopback_ksp != NULL) {
3823 			ipst->ips_loopback_ksp->ks_update =
3824 			    loopback_kstat_update;
3825 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3826 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3827 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3828 			ipst->ips_loopback_ksp->ks_private =
3829 			    (void *)(uintptr_t)ipst->ips_netstack->
3830 			    netstack_stackid;
3831 			kstat_install(ipst->ips_loopback_ksp);
3832 		}
3833 	}
3834 
3835 	*did_alloc = B_TRUE;
3836 	rw_exit(&ipst->ips_ill_g_lock);
3837 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3838 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3839 	return (ill);
3840 done:
3841 	if (ill != NULL) {
3842 		if (ill->ill_phyint != NULL) {
3843 			ipsq = ill->ill_phyint->phyint_ipsq;
3844 			if (ipsq != NULL) {
3845 				ipsq->ipsq_phyint = NULL;
3846 				ipsq_delete(ipsq);
3847 			}
3848 			mi_free(ill->ill_phyint);
3849 		}
3850 		ill_free_mib(ill);
3851 		if (ill->ill_ipst != NULL)
3852 			netstack_rele(ill->ill_ipst->ips_netstack);
3853 		mi_free(ill);
3854 	}
3855 	rw_exit(&ipst->ips_ill_g_lock);
3856 	return (NULL);
3857 }
3858 
3859 /*
3860  * For IPP calls - use the ip_stack_t for global stack.
3861  */
3862 ill_t *
3863 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3864 {
3865 	ip_stack_t	*ipst;
3866 	ill_t		*ill;
3867 
3868 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3869 	if (ipst == NULL) {
3870 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3871 		return (NULL);
3872 	}
3873 
3874 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3875 	netstack_rele(ipst->ips_netstack);
3876 	return (ill);
3877 }
3878 
3879 /*
3880  * Return a pointer to the ill which matches the index and IP version type.
3881  */
3882 ill_t *
3883 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3884 {
3885 	ill_t	*ill;
3886 	phyint_t *phyi;
3887 
3888 	/*
3889 	 * Indexes are stored in the phyint - a common structure
3890 	 * to both IPv4 and IPv6.
3891 	 */
3892 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3893 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3894 	    (void *) &index, NULL);
3895 	if (phyi != NULL) {
3896 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3897 		if (ill != NULL) {
3898 			mutex_enter(&ill->ill_lock);
3899 			if (!ILL_IS_CONDEMNED(ill)) {
3900 				ill_refhold_locked(ill);
3901 				mutex_exit(&ill->ill_lock);
3902 				rw_exit(&ipst->ips_ill_g_lock);
3903 				return (ill);
3904 			}
3905 			mutex_exit(&ill->ill_lock);
3906 		}
3907 	}
3908 	rw_exit(&ipst->ips_ill_g_lock);
3909 	return (NULL);
3910 }
3911 
3912 /*
3913  * Verify whether or not an interface index is valid.
3914  * It can be zero (meaning "reset") or an interface index assigned
3915  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3916  */
3917 boolean_t
3918 ip_ifindex_valid(uint_t ifindex, boolean_t isv6, ip_stack_t *ipst)
3919 {
3920 	ill_t		*ill;
3921 
3922 	if (ifindex == 0)
3923 		return (B_TRUE);
3924 
3925 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
3926 	if (ill == NULL)
3927 		return (B_FALSE);
3928 	if (IS_VNI(ill)) {
3929 		ill_refrele(ill);
3930 		return (B_FALSE);
3931 	}
3932 	ill_refrele(ill);
3933 	return (B_TRUE);
3934 }
3935 
3936 /*
3937  * Return the ifindex next in sequence after the passed in ifindex.
3938  * If there is no next ifindex for the given protocol, return 0.
3939  */
3940 uint_t
3941 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3942 {
3943 	phyint_t *phyi;
3944 	phyint_t *phyi_initial;
3945 	uint_t   ifindex;
3946 
3947 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3948 
3949 	if (index == 0) {
3950 		phyi = avl_first(
3951 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3952 	} else {
3953 		phyi = phyi_initial = avl_find(
3954 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3955 		    (void *) &index, NULL);
3956 	}
3957 
3958 	for (; phyi != NULL;
3959 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3960 	    phyi, AVL_AFTER)) {
3961 		/*
3962 		 * If we're not returning the first interface in the tree
3963 		 * and we still haven't moved past the phyint_t that
3964 		 * corresponds to index, avl_walk needs to be called again
3965 		 */
3966 		if (!((index != 0) && (phyi == phyi_initial))) {
3967 			if (isv6) {
3968 				if ((phyi->phyint_illv6) &&
3969 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3970 				    (phyi->phyint_illv6->ill_isv6 == 1))
3971 					break;
3972 			} else {
3973 				if ((phyi->phyint_illv4) &&
3974 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3975 				    (phyi->phyint_illv4->ill_isv6 == 0))
3976 					break;
3977 			}
3978 		}
3979 	}
3980 
3981 	rw_exit(&ipst->ips_ill_g_lock);
3982 
3983 	if (phyi != NULL)
3984 		ifindex = phyi->phyint_ifindex;
3985 	else
3986 		ifindex = 0;
3987 
3988 	return (ifindex);
3989 }
3990 
3991 /*
3992  * Return the ifindex for the named interface.
3993  * If there is no next ifindex for the interface, return 0.
3994  */
3995 uint_t
3996 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3997 {
3998 	phyint_t	*phyi;
3999 	avl_index_t	where = 0;
4000 	uint_t		ifindex;
4001 
4002 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4003 
4004 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4005 	    name, &where)) == NULL) {
4006 		rw_exit(&ipst->ips_ill_g_lock);
4007 		return (0);
4008 	}
4009 
4010 	ifindex = phyi->phyint_ifindex;
4011 
4012 	rw_exit(&ipst->ips_ill_g_lock);
4013 
4014 	return (ifindex);
4015 }
4016 
4017 /*
4018  * Return the ifindex to be used by upper layer protocols for instance
4019  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4020  */
4021 uint_t
4022 ill_get_upper_ifindex(const ill_t *ill)
4023 {
4024 	if (IS_UNDER_IPMP(ill))
4025 		return (ipmp_ill_get_ipmp_ifindex(ill));
4026 	else
4027 		return (ill->ill_phyint->phyint_ifindex);
4028 }
4029 
4030 
4031 /*
4032  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4033  * that gives a running thread a reference to the ill. This reference must be
4034  * released by the thread when it is done accessing the ill and related
4035  * objects. ill_refcnt can not be used to account for static references
4036  * such as other structures pointing to an ill. Callers must generally
4037  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4038  * or be sure that the ill is not being deleted or changing state before
4039  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4040  * ill won't change any of its critical state such as address, netmask etc.
4041  */
4042 void
4043 ill_refhold(ill_t *ill)
4044 {
4045 	mutex_enter(&ill->ill_lock);
4046 	ill->ill_refcnt++;
4047 	ILL_TRACE_REF(ill);
4048 	mutex_exit(&ill->ill_lock);
4049 }
4050 
4051 void
4052 ill_refhold_locked(ill_t *ill)
4053 {
4054 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4055 	ill->ill_refcnt++;
4056 	ILL_TRACE_REF(ill);
4057 }
4058 
4059 /* Returns true if we managed to get a refhold */
4060 boolean_t
4061 ill_check_and_refhold(ill_t *ill)
4062 {
4063 	mutex_enter(&ill->ill_lock);
4064 	if (!ILL_IS_CONDEMNED(ill)) {
4065 		ill_refhold_locked(ill);
4066 		mutex_exit(&ill->ill_lock);
4067 		return (B_TRUE);
4068 	}
4069 	mutex_exit(&ill->ill_lock);
4070 	return (B_FALSE);
4071 }
4072 
4073 /*
4074  * Must not be called while holding any locks. Otherwise if this is
4075  * the last reference to be released, there is a chance of recursive mutex
4076  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4077  * to restart an ioctl.
4078  */
4079 void
4080 ill_refrele(ill_t *ill)
4081 {
4082 	mutex_enter(&ill->ill_lock);
4083 	ASSERT(ill->ill_refcnt != 0);
4084 	ill->ill_refcnt--;
4085 	ILL_UNTRACE_REF(ill);
4086 	if (ill->ill_refcnt != 0) {
4087 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4088 		mutex_exit(&ill->ill_lock);
4089 		return;
4090 	}
4091 
4092 	/* Drops the ill_lock */
4093 	ipif_ill_refrele_tail(ill);
4094 }
4095 
4096 /*
4097  * Obtain a weak reference count on the ill. This reference ensures the
4098  * ill won't be freed, but the ill may change any of its critical state
4099  * such as netmask, address etc. Returns an error if the ill has started
4100  * closing.
4101  */
4102 boolean_t
4103 ill_waiter_inc(ill_t *ill)
4104 {
4105 	mutex_enter(&ill->ill_lock);
4106 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4107 		mutex_exit(&ill->ill_lock);
4108 		return (B_FALSE);
4109 	}
4110 	ill->ill_waiters++;
4111 	mutex_exit(&ill->ill_lock);
4112 	return (B_TRUE);
4113 }
4114 
4115 void
4116 ill_waiter_dcr(ill_t *ill)
4117 {
4118 	mutex_enter(&ill->ill_lock);
4119 	ill->ill_waiters--;
4120 	if (ill->ill_waiters == 0)
4121 		cv_broadcast(&ill->ill_cv);
4122 	mutex_exit(&ill->ill_lock);
4123 }
4124 
4125 /*
4126  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4127  * driver.  We construct best guess defaults for lower level information that
4128  * we need.  If an interface is brought up without injection of any overriding
4129  * information from outside, we have to be ready to go with these defaults.
4130  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4131  * we primarely want the dl_provider_style.
4132  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4133  * at which point we assume the other part of the information is valid.
4134  */
4135 void
4136 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4137 {
4138 	uchar_t		*brdcst_addr;
4139 	uint_t		brdcst_addr_length, phys_addr_length;
4140 	t_scalar_t	sap_length;
4141 	dl_info_ack_t	*dlia;
4142 	ip_m_t		*ipm;
4143 	dl_qos_cl_sel1_t *sel1;
4144 	int		min_mtu;
4145 
4146 	ASSERT(IAM_WRITER_ILL(ill));
4147 
4148 	/*
4149 	 * Till the ill is fully up  the ill is not globally visible.
4150 	 * So no need for a lock.
4151 	 */
4152 	dlia = (dl_info_ack_t *)mp->b_rptr;
4153 	ill->ill_mactype = dlia->dl_mac_type;
4154 
4155 	ipm = ip_m_lookup(dlia->dl_mac_type);
4156 	if (ipm == NULL) {
4157 		ipm = ip_m_lookup(DL_OTHER);
4158 		ASSERT(ipm != NULL);
4159 	}
4160 	ill->ill_media = ipm;
4161 
4162 	/*
4163 	 * When the new DLPI stuff is ready we'll pull lengths
4164 	 * from dlia.
4165 	 */
4166 	if (dlia->dl_version == DL_VERSION_2) {
4167 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4168 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4169 		    brdcst_addr_length);
4170 		if (brdcst_addr == NULL) {
4171 			brdcst_addr_length = 0;
4172 		}
4173 		sap_length = dlia->dl_sap_length;
4174 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4175 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4176 		    brdcst_addr_length, sap_length, phys_addr_length));
4177 	} else {
4178 		brdcst_addr_length = 6;
4179 		brdcst_addr = ip_six_byte_all_ones;
4180 		sap_length = -2;
4181 		phys_addr_length = brdcst_addr_length;
4182 	}
4183 
4184 	ill->ill_bcast_addr_length = brdcst_addr_length;
4185 	ill->ill_phys_addr_length = phys_addr_length;
4186 	ill->ill_sap_length = sap_length;
4187 
4188 	/*
4189 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4190 	 * but we must ensure a minimum IP MTU is used since other bits of
4191 	 * IP will fly apart otherwise.
4192 	 */
4193 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4194 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4195 	ill->ill_current_frag = ill->ill_max_frag;
4196 	ill->ill_mtu = ill->ill_max_frag;
4197 
4198 	ill->ill_type = ipm->ip_m_type;
4199 
4200 	if (!ill->ill_dlpi_style_set) {
4201 		if (dlia->dl_provider_style == DL_STYLE2)
4202 			ill->ill_needs_attach = 1;
4203 
4204 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4205 
4206 		/*
4207 		 * Allocate the first ipif on this ill.  We don't delay it
4208 		 * further as ioctl handling assumes at least one ipif exists.
4209 		 *
4210 		 * At this point we don't know whether the ill is v4 or v6.
4211 		 * We will know this whan the SIOCSLIFNAME happens and
4212 		 * the correct value for ill_isv6 will be assigned in
4213 		 * ipif_set_values(). We need to hold the ill lock and
4214 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4215 		 * the wakeup.
4216 		 */
4217 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4218 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
4219 		mutex_enter(&ill->ill_lock);
4220 		ASSERT(ill->ill_dlpi_style_set == 0);
4221 		ill->ill_dlpi_style_set = 1;
4222 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4223 		cv_broadcast(&ill->ill_cv);
4224 		mutex_exit(&ill->ill_lock);
4225 		freemsg(mp);
4226 		return;
4227 	}
4228 	ASSERT(ill->ill_ipif != NULL);
4229 	/*
4230 	 * We know whether it is IPv4 or IPv6 now, as this is the
4231 	 * second DL_INFO_ACK we are recieving in response to the
4232 	 * DL_INFO_REQ sent in ipif_set_values.
4233 	 */
4234 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4235 	/*
4236 	 * Clear all the flags that were set based on ill_bcast_addr_length
4237 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4238 	 * changed now and we need to re-evaluate.
4239 	 */
4240 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4241 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4242 
4243 	/*
4244 	 * Free ill_bcast_mp as things could have changed now.
4245 	 *
4246 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4247 	 * with no underlying interfaces (and thus an unknown broadcast
4248 	 * address length), but we enforce that an interface is broadcast-
4249 	 * capable as part of allowing it to join a group.
4250 	 */
4251 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4252 		if (ill->ill_bcast_mp != NULL)
4253 			freemsg(ill->ill_bcast_mp);
4254 		ill->ill_net_type = IRE_IF_NORESOLVER;
4255 
4256 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4257 		    ill->ill_phys_addr_length,
4258 		    ill->ill_sap,
4259 		    ill->ill_sap_length);
4260 
4261 		if (ill->ill_isv6)
4262 			/*
4263 			 * Note: xresolv interfaces will eventually need NOARP
4264 			 * set here as well, but that will require those
4265 			 * external resolvers to have some knowledge of
4266 			 * that flag and act appropriately. Not to be changed
4267 			 * at present.
4268 			 */
4269 			ill->ill_flags |= ILLF_NONUD;
4270 		else
4271 			ill->ill_flags |= ILLF_NOARP;
4272 
4273 		if (ill->ill_mactype == SUNW_DL_VNI) {
4274 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4275 		} else if (ill->ill_phys_addr_length == 0 ||
4276 		    ill->ill_mactype == DL_IPV4 ||
4277 		    ill->ill_mactype == DL_IPV6) {
4278 			/*
4279 			 * The underying link is point-to-point, so mark the
4280 			 * interface as such.  We can do IP multicast over
4281 			 * such a link since it transmits all network-layer
4282 			 * packets to the remote side the same way.
4283 			 */
4284 			ill->ill_flags |= ILLF_MULTICAST;
4285 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4286 		}
4287 	} else {
4288 		ill->ill_net_type = IRE_IF_RESOLVER;
4289 		if (ill->ill_bcast_mp != NULL)
4290 			freemsg(ill->ill_bcast_mp);
4291 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4292 		    ill->ill_bcast_addr_length, ill->ill_sap,
4293 		    ill->ill_sap_length);
4294 		/*
4295 		 * Later detect lack of DLPI driver multicast
4296 		 * capability by catching DL_ENABMULTI errors in
4297 		 * ip_rput_dlpi.
4298 		 */
4299 		ill->ill_flags |= ILLF_MULTICAST;
4300 		if (!ill->ill_isv6)
4301 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4302 	}
4303 
4304 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4305 	if (ill->ill_mactype == SUNW_DL_IPMP)
4306 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4307 
4308 	/* By default an interface does not support any CoS marking */
4309 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4310 
4311 	/*
4312 	 * If we get QoS information in DL_INFO_ACK, the device supports
4313 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4314 	 */
4315 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4316 	    dlia->dl_qos_length);
4317 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4318 		ill->ill_flags |= ILLF_COS_ENABLED;
4319 	}
4320 
4321 	/* Clear any previous error indication. */
4322 	ill->ill_error = 0;
4323 	freemsg(mp);
4324 }
4325 
4326 /*
4327  * Perform various checks to verify that an address would make sense as a
4328  * local, remote, or subnet interface address.
4329  */
4330 static boolean_t
4331 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4332 {
4333 	ipaddr_t	net_mask;
4334 
4335 	/*
4336 	 * Don't allow all zeroes, or all ones, but allow
4337 	 * all ones netmask.
4338 	 */
4339 	if ((net_mask = ip_net_mask(addr)) == 0)
4340 		return (B_FALSE);
4341 	/* A given netmask overrides the "guess" netmask */
4342 	if (subnet_mask != 0)
4343 		net_mask = subnet_mask;
4344 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4345 	    (addr == (addr | ~net_mask)))) {
4346 		return (B_FALSE);
4347 	}
4348 
4349 	/*
4350 	 * Even if the netmask is all ones, we do not allow address to be
4351 	 * 255.255.255.255
4352 	 */
4353 	if (addr == INADDR_BROADCAST)
4354 		return (B_FALSE);
4355 
4356 	if (CLASSD(addr))
4357 		return (B_FALSE);
4358 
4359 	return (B_TRUE);
4360 }
4361 
4362 #define	V6_IPIF_LINKLOCAL(p)	\
4363 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4364 
4365 /*
4366  * Compare two given ipifs and check if the second one is better than
4367  * the first one using the order of preference (not taking deprecated
4368  * into acount) specified in ipif_lookup_multicast().
4369  */
4370 static boolean_t
4371 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4372 {
4373 	/* Check the least preferred first. */
4374 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4375 		/* If both ipifs are the same, use the first one. */
4376 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4377 			return (B_FALSE);
4378 		else
4379 			return (B_TRUE);
4380 	}
4381 
4382 	/* For IPv6, check for link local address. */
4383 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4384 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4385 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4386 			/* The second one is equal or less preferred. */
4387 			return (B_FALSE);
4388 		} else {
4389 			return (B_TRUE);
4390 		}
4391 	}
4392 
4393 	/* Then check for point to point interface. */
4394 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4395 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4396 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4397 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4398 			return (B_FALSE);
4399 		} else {
4400 			return (B_TRUE);
4401 		}
4402 	}
4403 
4404 	/* old_ipif is a normal interface, so no need to use the new one. */
4405 	return (B_FALSE);
4406 }
4407 
4408 /*
4409  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4410  * The ipif must be up, and its ill must multicast-capable, not
4411  * condemned, not an underlying interface in an IPMP group, and
4412  * not a VNI interface.  Order of preference:
4413  *
4414  * 	1a. normal
4415  * 	1b. normal, but deprecated
4416  * 	2a. point to point
4417  * 	2b. point to point, but deprecated
4418  * 	3a. link local
4419  * 	3b. link local, but deprecated
4420  * 	4. loopback.
4421  */
4422 static ipif_t *
4423 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4424 {
4425 	ill_t			*ill;
4426 	ill_walk_context_t	ctx;
4427 	ipif_t			*ipif;
4428 	ipif_t			*saved_ipif = NULL;
4429 	ipif_t			*dep_ipif = NULL;
4430 
4431 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4432 	if (isv6)
4433 		ill = ILL_START_WALK_V6(&ctx, ipst);
4434 	else
4435 		ill = ILL_START_WALK_V4(&ctx, ipst);
4436 
4437 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4438 		mutex_enter(&ill->ill_lock);
4439 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4440 		    ILL_IS_CONDEMNED(ill) ||
4441 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4442 			mutex_exit(&ill->ill_lock);
4443 			continue;
4444 		}
4445 		for (ipif = ill->ill_ipif; ipif != NULL;
4446 		    ipif = ipif->ipif_next) {
4447 			if (zoneid != ipif->ipif_zoneid &&
4448 			    zoneid != ALL_ZONES &&
4449 			    ipif->ipif_zoneid != ALL_ZONES) {
4450 				continue;
4451 			}
4452 			if (!(ipif->ipif_flags & IPIF_UP) ||
4453 			    IPIF_IS_CONDEMNED(ipif)) {
4454 				continue;
4455 			}
4456 
4457 			/*
4458 			 * Found one candidate.  If it is deprecated,
4459 			 * remember it in dep_ipif.  If it is not deprecated,
4460 			 * remember it in saved_ipif.
4461 			 */
4462 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4463 				if (dep_ipif == NULL) {
4464 					dep_ipif = ipif;
4465 				} else if (ipif_comp_multi(dep_ipif, ipif,
4466 				    isv6)) {
4467 					/*
4468 					 * If the previous dep_ipif does not
4469 					 * belong to the same ill, we've done
4470 					 * a ipif_refhold() on it.  So we need
4471 					 * to release it.
4472 					 */
4473 					if (dep_ipif->ipif_ill != ill)
4474 						ipif_refrele(dep_ipif);
4475 					dep_ipif = ipif;
4476 				}
4477 				continue;
4478 			}
4479 			if (saved_ipif == NULL) {
4480 				saved_ipif = ipif;
4481 			} else {
4482 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4483 					if (saved_ipif->ipif_ill != ill)
4484 						ipif_refrele(saved_ipif);
4485 					saved_ipif = ipif;
4486 				}
4487 			}
4488 		}
4489 		/*
4490 		 * Before going to the next ill, do a ipif_refhold() on the
4491 		 * saved ones.
4492 		 */
4493 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4494 			ipif_refhold_locked(saved_ipif);
4495 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4496 			ipif_refhold_locked(dep_ipif);
4497 		mutex_exit(&ill->ill_lock);
4498 	}
4499 	rw_exit(&ipst->ips_ill_g_lock);
4500 
4501 	/*
4502 	 * If we have only the saved_ipif, return it.  But if we have both
4503 	 * saved_ipif and dep_ipif, check to see which one is better.
4504 	 */
4505 	if (saved_ipif != NULL) {
4506 		if (dep_ipif != NULL) {
4507 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4508 				ipif_refrele(saved_ipif);
4509 				return (dep_ipif);
4510 			} else {
4511 				ipif_refrele(dep_ipif);
4512 				return (saved_ipif);
4513 			}
4514 		}
4515 		return (saved_ipif);
4516 	} else {
4517 		return (dep_ipif);
4518 	}
4519 }
4520 
4521 ill_t *
4522 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4523 {
4524 	ipif_t *ipif;
4525 	ill_t *ill;
4526 
4527 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4528 	if (ipif == NULL)
4529 		return (NULL);
4530 
4531 	ill = ipif->ipif_ill;
4532 	ill_refhold(ill);
4533 	ipif_refrele(ipif);
4534 	return (ill);
4535 }
4536 
4537 /*
4538  * This function is called when an application does not specify an interface
4539  * to be used for multicast traffic (joining a group/sending data).  It
4540  * calls ire_lookup_multi() to look for an interface route for the
4541  * specified multicast group.  Doing this allows the administrator to add
4542  * prefix routes for multicast to indicate which interface to be used for
4543  * multicast traffic in the above scenario.  The route could be for all
4544  * multicast (224.0/4), for a single multicast group (a /32 route) or
4545  * anything in between.  If there is no such multicast route, we just find
4546  * any multicast capable interface and return it.  The returned ipif
4547  * is refhold'ed.
4548  *
4549  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4550  * unicast table. This is used by CGTP.
4551  */
4552 ill_t *
4553 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4554     boolean_t *multirtp, ipaddr_t *setsrcp)
4555 {
4556 	ill_t			*ill;
4557 
4558 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4559 	if (ill != NULL)
4560 		return (ill);
4561 
4562 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4563 }
4564 
4565 /*
4566  * Look for an ipif with the specified interface address and destination.
4567  * The destination address is used only for matching point-to-point interfaces.
4568  */
4569 ipif_t *
4570 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4571 {
4572 	ipif_t	*ipif;
4573 	ill_t	*ill;
4574 	ill_walk_context_t ctx;
4575 
4576 	/*
4577 	 * First match all the point-to-point interfaces
4578 	 * before looking at non-point-to-point interfaces.
4579 	 * This is done to avoid returning non-point-to-point
4580 	 * ipif instead of unnumbered point-to-point ipif.
4581 	 */
4582 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4583 	ill = ILL_START_WALK_V4(&ctx, ipst);
4584 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4585 		mutex_enter(&ill->ill_lock);
4586 		for (ipif = ill->ill_ipif; ipif != NULL;
4587 		    ipif = ipif->ipif_next) {
4588 			/* Allow the ipif to be down */
4589 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4590 			    (ipif->ipif_lcl_addr == if_addr) &&
4591 			    (ipif->ipif_pp_dst_addr == dst)) {
4592 				if (!IPIF_IS_CONDEMNED(ipif)) {
4593 					ipif_refhold_locked(ipif);
4594 					mutex_exit(&ill->ill_lock);
4595 					rw_exit(&ipst->ips_ill_g_lock);
4596 					return (ipif);
4597 				}
4598 			}
4599 		}
4600 		mutex_exit(&ill->ill_lock);
4601 	}
4602 	rw_exit(&ipst->ips_ill_g_lock);
4603 
4604 	/* lookup the ipif based on interface address */
4605 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4606 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4607 	return (ipif);
4608 }
4609 
4610 /*
4611  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4612  */
4613 static ipif_t *
4614 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4615     zoneid_t zoneid, ip_stack_t *ipst)
4616 {
4617 	ipif_t  *ipif;
4618 	ill_t   *ill;
4619 	boolean_t ptp = B_FALSE;
4620 	ill_walk_context_t	ctx;
4621 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4622 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4623 
4624 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4625 	/*
4626 	 * Repeat twice, first based on local addresses and
4627 	 * next time for pointopoint.
4628 	 */
4629 repeat:
4630 	ill = ILL_START_WALK_V4(&ctx, ipst);
4631 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4632 		if (match_ill != NULL && ill != match_ill &&
4633 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4634 			continue;
4635 		}
4636 		mutex_enter(&ill->ill_lock);
4637 		for (ipif = ill->ill_ipif; ipif != NULL;
4638 		    ipif = ipif->ipif_next) {
4639 			if (zoneid != ALL_ZONES &&
4640 			    zoneid != ipif->ipif_zoneid &&
4641 			    ipif->ipif_zoneid != ALL_ZONES)
4642 				continue;
4643 
4644 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4645 				continue;
4646 
4647 			/* Allow the ipif to be down */
4648 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4649 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4650 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4651 			    (ipif->ipif_pp_dst_addr == addr))) {
4652 				if (!IPIF_IS_CONDEMNED(ipif)) {
4653 					ipif_refhold_locked(ipif);
4654 					mutex_exit(&ill->ill_lock);
4655 					rw_exit(&ipst->ips_ill_g_lock);
4656 					return (ipif);
4657 				}
4658 			}
4659 		}
4660 		mutex_exit(&ill->ill_lock);
4661 	}
4662 
4663 	/* If we already did the ptp case, then we are done */
4664 	if (ptp) {
4665 		rw_exit(&ipst->ips_ill_g_lock);
4666 		return (NULL);
4667 	}
4668 	ptp = B_TRUE;
4669 	goto repeat;
4670 }
4671 
4672 /*
4673  * Lookup an ipif with the specified address.  For point-to-point links we
4674  * look for matches on either the destination address or the local address,
4675  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4676  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4677  * (or illgrp if `match_ill' is in an IPMP group).
4678  */
4679 ipif_t *
4680 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4681     ip_stack_t *ipst)
4682 {
4683 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4684 	    zoneid, ipst));
4685 }
4686 
4687 /*
4688  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4689  * except that we will only return an address if it is not marked as
4690  * IPIF_DUPLICATE
4691  */
4692 ipif_t *
4693 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4694     ip_stack_t *ipst)
4695 {
4696 	return (ipif_lookup_addr_common(addr, match_ill,
4697 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4698 	    zoneid, ipst));
4699 }
4700 
4701 /*
4702  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4703  * `match_ill' across the IPMP group.  This function is only needed in some
4704  * corner-cases; almost everything should use ipif_lookup_addr().
4705  */
4706 ipif_t *
4707 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4708 {
4709 	ASSERT(match_ill != NULL);
4710 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4711 	    ipst));
4712 }
4713 
4714 /*
4715  * Look for an ipif with the specified address. For point-point links
4716  * we look for matches on either the destination address and the local
4717  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4718  * is set.
4719  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4720  * ill (or illgrp if `match_ill' is in an IPMP group).
4721  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4722  */
4723 zoneid_t
4724 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4725 {
4726 	zoneid_t zoneid;
4727 	ipif_t  *ipif;
4728 	ill_t   *ill;
4729 	boolean_t ptp = B_FALSE;
4730 	ill_walk_context_t	ctx;
4731 
4732 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4733 	/*
4734 	 * Repeat twice, first based on local addresses and
4735 	 * next time for pointopoint.
4736 	 */
4737 repeat:
4738 	ill = ILL_START_WALK_V4(&ctx, ipst);
4739 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4740 		if (match_ill != NULL && ill != match_ill &&
4741 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4742 			continue;
4743 		}
4744 		mutex_enter(&ill->ill_lock);
4745 		for (ipif = ill->ill_ipif; ipif != NULL;
4746 		    ipif = ipif->ipif_next) {
4747 			/* Allow the ipif to be down */
4748 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4749 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4750 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4751 			    (ipif->ipif_pp_dst_addr == addr)) &&
4752 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4753 				zoneid = ipif->ipif_zoneid;
4754 				mutex_exit(&ill->ill_lock);
4755 				rw_exit(&ipst->ips_ill_g_lock);
4756 				/*
4757 				 * If ipif_zoneid was ALL_ZONES then we have
4758 				 * a trusted extensions shared IP address.
4759 				 * In that case GLOBAL_ZONEID works to send.
4760 				 */
4761 				if (zoneid == ALL_ZONES)
4762 					zoneid = GLOBAL_ZONEID;
4763 				return (zoneid);
4764 			}
4765 		}
4766 		mutex_exit(&ill->ill_lock);
4767 	}
4768 
4769 	/* If we already did the ptp case, then we are done */
4770 	if (ptp) {
4771 		rw_exit(&ipst->ips_ill_g_lock);
4772 		return (ALL_ZONES);
4773 	}
4774 	ptp = B_TRUE;
4775 	goto repeat;
4776 }
4777 
4778 /*
4779  * Look for an ipif that matches the specified remote address i.e. the
4780  * ipif that would receive the specified packet.
4781  * First look for directly connected interfaces and then do a recursive
4782  * IRE lookup and pick the first ipif corresponding to the source address in the
4783  * ire.
4784  * Returns: held ipif
4785  *
4786  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4787  */
4788 ipif_t *
4789 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4790 {
4791 	ipif_t	*ipif;
4792 
4793 	ASSERT(!ill->ill_isv6);
4794 
4795 	/*
4796 	 * Someone could be changing this ipif currently or change it
4797 	 * after we return this. Thus  a few packets could use the old
4798 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4799 	 * will atomically be updated or cleaned up with the new value
4800 	 * Thus we don't need a lock to check the flags or other attrs below.
4801 	 */
4802 	mutex_enter(&ill->ill_lock);
4803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4804 		if (IPIF_IS_CONDEMNED(ipif))
4805 			continue;
4806 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4807 		    ipif->ipif_zoneid != ALL_ZONES)
4808 			continue;
4809 		/* Allow the ipif to be down */
4810 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4811 			if ((ipif->ipif_pp_dst_addr == addr) ||
4812 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4813 			    ipif->ipif_lcl_addr == addr)) {
4814 				ipif_refhold_locked(ipif);
4815 				mutex_exit(&ill->ill_lock);
4816 				return (ipif);
4817 			}
4818 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4819 			ipif_refhold_locked(ipif);
4820 			mutex_exit(&ill->ill_lock);
4821 			return (ipif);
4822 		}
4823 	}
4824 	mutex_exit(&ill->ill_lock);
4825 	/*
4826 	 * For a remote destination it isn't possible to nail down a particular
4827 	 * ipif.
4828 	 */
4829 
4830 	/* Pick the first interface */
4831 	ipif = ipif_get_next_ipif(NULL, ill);
4832 	return (ipif);
4833 }
4834 
4835 /*
4836  * This func does not prevent refcnt from increasing. But if
4837  * the caller has taken steps to that effect, then this func
4838  * can be used to determine whether the ill has become quiescent
4839  */
4840 static boolean_t
4841 ill_is_quiescent(ill_t *ill)
4842 {
4843 	ipif_t	*ipif;
4844 
4845 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4846 
4847 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4848 		if (ipif->ipif_refcnt != 0)
4849 			return (B_FALSE);
4850 	}
4851 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4852 		return (B_FALSE);
4853 	}
4854 	return (B_TRUE);
4855 }
4856 
4857 boolean_t
4858 ill_is_freeable(ill_t *ill)
4859 {
4860 	ipif_t	*ipif;
4861 
4862 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4863 
4864 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4865 		if (ipif->ipif_refcnt != 0) {
4866 			return (B_FALSE);
4867 		}
4868 	}
4869 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4870 		return (B_FALSE);
4871 	}
4872 	return (B_TRUE);
4873 }
4874 
4875 /*
4876  * This func does not prevent refcnt from increasing. But if
4877  * the caller has taken steps to that effect, then this func
4878  * can be used to determine whether the ipif has become quiescent
4879  */
4880 static boolean_t
4881 ipif_is_quiescent(ipif_t *ipif)
4882 {
4883 	ill_t *ill;
4884 
4885 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4886 
4887 	if (ipif->ipif_refcnt != 0)
4888 		return (B_FALSE);
4889 
4890 	ill = ipif->ipif_ill;
4891 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4892 	    ill->ill_logical_down) {
4893 		return (B_TRUE);
4894 	}
4895 
4896 	/* This is the last ipif going down or being deleted on this ill */
4897 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4898 		return (B_FALSE);
4899 	}
4900 
4901 	return (B_TRUE);
4902 }
4903 
4904 /*
4905  * return true if the ipif can be destroyed: the ipif has to be quiescent
4906  * with zero references from ire/ilm to it.
4907  */
4908 static boolean_t
4909 ipif_is_freeable(ipif_t *ipif)
4910 {
4911 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4912 	ASSERT(ipif->ipif_id != 0);
4913 	return (ipif->ipif_refcnt == 0);
4914 }
4915 
4916 /*
4917  * The ipif/ill/ire has been refreled. Do the tail processing.
4918  * Determine if the ipif or ill in question has become quiescent and if so
4919  * wakeup close and/or restart any queued pending ioctl that is waiting
4920  * for the ipif_down (or ill_down)
4921  */
4922 void
4923 ipif_ill_refrele_tail(ill_t *ill)
4924 {
4925 	mblk_t	*mp;
4926 	conn_t	*connp;
4927 	ipsq_t	*ipsq;
4928 	ipxop_t	*ipx;
4929 	ipif_t	*ipif;
4930 	dl_notify_ind_t *dlindp;
4931 
4932 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4933 
4934 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4935 		/* ip_modclose() may be waiting */
4936 		cv_broadcast(&ill->ill_cv);
4937 	}
4938 
4939 	ipsq = ill->ill_phyint->phyint_ipsq;
4940 	mutex_enter(&ipsq->ipsq_lock);
4941 	ipx = ipsq->ipsq_xop;
4942 	mutex_enter(&ipx->ipx_lock);
4943 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4944 		goto unlock;
4945 
4946 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4947 
4948 	ipif = ipx->ipx_pending_ipif;
4949 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4950 		goto unlock;
4951 
4952 	switch (ipx->ipx_waitfor) {
4953 	case IPIF_DOWN:
4954 		if (!ipif_is_quiescent(ipif))
4955 			goto unlock;
4956 		break;
4957 	case IPIF_FREE:
4958 		if (!ipif_is_freeable(ipif))
4959 			goto unlock;
4960 		break;
4961 	case ILL_DOWN:
4962 		if (!ill_is_quiescent(ill))
4963 			goto unlock;
4964 		break;
4965 	case ILL_FREE:
4966 		/*
4967 		 * ILL_FREE is only for loopback; normal ill teardown waits
4968 		 * synchronously in ip_modclose() without using ipx_waitfor,
4969 		 * handled by the cv_broadcast() at the top of this function.
4970 		 */
4971 		if (!ill_is_freeable(ill))
4972 			goto unlock;
4973 		break;
4974 	default:
4975 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4976 		    (void *)ipsq, ipx->ipx_waitfor);
4977 	}
4978 
4979 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4980 	mutex_exit(&ipx->ipx_lock);
4981 	mp = ipsq_pending_mp_get(ipsq, &connp);
4982 	mutex_exit(&ipsq->ipsq_lock);
4983 	mutex_exit(&ill->ill_lock);
4984 
4985 	ASSERT(mp != NULL);
4986 	/*
4987 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4988 	 * we can only get here when the current operation decides it
4989 	 * it needs to quiesce via ipsq_pending_mp_add().
4990 	 */
4991 	switch (mp->b_datap->db_type) {
4992 	case M_PCPROTO:
4993 	case M_PROTO:
4994 		/*
4995 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4996 		 */
4997 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4998 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4999 
5000 		switch (dlindp->dl_notification) {
5001 		case DL_NOTE_PHYS_ADDR:
5002 			qwriter_ip(ill, ill->ill_rq, mp,
5003 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5004 			return;
5005 		case DL_NOTE_REPLUMB:
5006 			qwriter_ip(ill, ill->ill_rq, mp,
5007 			    ill_replumb_tail, CUR_OP, B_TRUE);
5008 			return;
5009 		default:
5010 			ASSERT(0);
5011 			ill_refrele(ill);
5012 		}
5013 		break;
5014 
5015 	case M_ERROR:
5016 	case M_HANGUP:
5017 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5018 		    B_TRUE);
5019 		return;
5020 
5021 	case M_IOCTL:
5022 	case M_IOCDATA:
5023 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5024 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5025 		return;
5026 
5027 	default:
5028 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5029 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5030 	}
5031 	return;
5032 unlock:
5033 	mutex_exit(&ipsq->ipsq_lock);
5034 	mutex_exit(&ipx->ipx_lock);
5035 	mutex_exit(&ill->ill_lock);
5036 }
5037 
5038 #ifdef DEBUG
5039 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5040 static void
5041 th_trace_rrecord(th_trace_t *th_trace)
5042 {
5043 	tr_buf_t *tr_buf;
5044 	uint_t lastref;
5045 
5046 	lastref = th_trace->th_trace_lastref;
5047 	lastref++;
5048 	if (lastref == TR_BUF_MAX)
5049 		lastref = 0;
5050 	th_trace->th_trace_lastref = lastref;
5051 	tr_buf = &th_trace->th_trbuf[lastref];
5052 	tr_buf->tr_time = ddi_get_lbolt();
5053 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5054 }
5055 
5056 static void
5057 th_trace_free(void *value)
5058 {
5059 	th_trace_t *th_trace = value;
5060 
5061 	ASSERT(th_trace->th_refcnt == 0);
5062 	kmem_free(th_trace, sizeof (*th_trace));
5063 }
5064 
5065 /*
5066  * Find or create the per-thread hash table used to track object references.
5067  * The ipst argument is NULL if we shouldn't allocate.
5068  *
5069  * Accesses per-thread data, so there's no need to lock here.
5070  */
5071 static mod_hash_t *
5072 th_trace_gethash(ip_stack_t *ipst)
5073 {
5074 	th_hash_t *thh;
5075 
5076 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5077 		mod_hash_t *mh;
5078 		char name[256];
5079 		size_t objsize, rshift;
5080 		int retv;
5081 
5082 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5083 			return (NULL);
5084 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5085 		    (void *)curthread);
5086 
5087 		/*
5088 		 * We use mod_hash_create_extended here rather than the more
5089 		 * obvious mod_hash_create_ptrhash because the latter has a
5090 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5091 		 * block.
5092 		 */
5093 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5094 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5095 		rshift = highbit(objsize);
5096 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5097 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5098 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5099 		if (mh == NULL) {
5100 			kmem_free(thh, sizeof (*thh));
5101 			return (NULL);
5102 		}
5103 		thh->thh_hash = mh;
5104 		thh->thh_ipst = ipst;
5105 		/*
5106 		 * We trace ills, ipifs, ires, and nces.  All of these are
5107 		 * per-IP-stack, so the lock on the thread list is as well.
5108 		 */
5109 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5110 		list_insert_tail(&ip_thread_list, thh);
5111 		rw_exit(&ip_thread_rwlock);
5112 		retv = tsd_set(ip_thread_data, thh);
5113 		ASSERT(retv == 0);
5114 	}
5115 	return (thh != NULL ? thh->thh_hash : NULL);
5116 }
5117 
5118 boolean_t
5119 th_trace_ref(const void *obj, ip_stack_t *ipst)
5120 {
5121 	th_trace_t *th_trace;
5122 	mod_hash_t *mh;
5123 	mod_hash_val_t val;
5124 
5125 	if ((mh = th_trace_gethash(ipst)) == NULL)
5126 		return (B_FALSE);
5127 
5128 	/*
5129 	 * Attempt to locate the trace buffer for this obj and thread.
5130 	 * If it does not exist, then allocate a new trace buffer and
5131 	 * insert into the hash.
5132 	 */
5133 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5134 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5135 		if (th_trace == NULL)
5136 			return (B_FALSE);
5137 
5138 		th_trace->th_id = curthread;
5139 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5140 		    (mod_hash_val_t)th_trace) != 0) {
5141 			kmem_free(th_trace, sizeof (th_trace_t));
5142 			return (B_FALSE);
5143 		}
5144 	} else {
5145 		th_trace = (th_trace_t *)val;
5146 	}
5147 
5148 	ASSERT(th_trace->th_refcnt >= 0 &&
5149 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5150 
5151 	th_trace->th_refcnt++;
5152 	th_trace_rrecord(th_trace);
5153 	return (B_TRUE);
5154 }
5155 
5156 /*
5157  * For the purpose of tracing a reference release, we assume that global
5158  * tracing is always on and that the same thread initiated the reference hold
5159  * is releasing.
5160  */
5161 void
5162 th_trace_unref(const void *obj)
5163 {
5164 	int retv;
5165 	mod_hash_t *mh;
5166 	th_trace_t *th_trace;
5167 	mod_hash_val_t val;
5168 
5169 	mh = th_trace_gethash(NULL);
5170 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5171 	ASSERT(retv == 0);
5172 	th_trace = (th_trace_t *)val;
5173 
5174 	ASSERT(th_trace->th_refcnt > 0);
5175 	th_trace->th_refcnt--;
5176 	th_trace_rrecord(th_trace);
5177 }
5178 
5179 /*
5180  * If tracing has been disabled, then we assume that the reference counts are
5181  * now useless, and we clear them out before destroying the entries.
5182  */
5183 void
5184 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5185 {
5186 	th_hash_t	*thh;
5187 	mod_hash_t	*mh;
5188 	mod_hash_val_t	val;
5189 	th_trace_t	*th_trace;
5190 	int		retv;
5191 
5192 	rw_enter(&ip_thread_rwlock, RW_READER);
5193 	for (thh = list_head(&ip_thread_list); thh != NULL;
5194 	    thh = list_next(&ip_thread_list, thh)) {
5195 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5196 		    &val) == 0) {
5197 			th_trace = (th_trace_t *)val;
5198 			if (trace_disable)
5199 				th_trace->th_refcnt = 0;
5200 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5201 			ASSERT(retv == 0);
5202 		}
5203 	}
5204 	rw_exit(&ip_thread_rwlock);
5205 }
5206 
5207 void
5208 ipif_trace_ref(ipif_t *ipif)
5209 {
5210 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5211 
5212 	if (ipif->ipif_trace_disable)
5213 		return;
5214 
5215 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5216 		ipif->ipif_trace_disable = B_TRUE;
5217 		ipif_trace_cleanup(ipif);
5218 	}
5219 }
5220 
5221 void
5222 ipif_untrace_ref(ipif_t *ipif)
5223 {
5224 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5225 
5226 	if (!ipif->ipif_trace_disable)
5227 		th_trace_unref(ipif);
5228 }
5229 
5230 void
5231 ill_trace_ref(ill_t *ill)
5232 {
5233 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5234 
5235 	if (ill->ill_trace_disable)
5236 		return;
5237 
5238 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5239 		ill->ill_trace_disable = B_TRUE;
5240 		ill_trace_cleanup(ill);
5241 	}
5242 }
5243 
5244 void
5245 ill_untrace_ref(ill_t *ill)
5246 {
5247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5248 
5249 	if (!ill->ill_trace_disable)
5250 		th_trace_unref(ill);
5251 }
5252 
5253 /*
5254  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5255  * failure, ipif_trace_disable is set.
5256  */
5257 static void
5258 ipif_trace_cleanup(const ipif_t *ipif)
5259 {
5260 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5261 }
5262 
5263 /*
5264  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5265  * failure, ill_trace_disable is set.
5266  */
5267 static void
5268 ill_trace_cleanup(const ill_t *ill)
5269 {
5270 	th_trace_cleanup(ill, ill->ill_trace_disable);
5271 }
5272 #endif /* DEBUG */
5273 
5274 void
5275 ipif_refhold_locked(ipif_t *ipif)
5276 {
5277 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5278 	ipif->ipif_refcnt++;
5279 	IPIF_TRACE_REF(ipif);
5280 }
5281 
5282 void
5283 ipif_refhold(ipif_t *ipif)
5284 {
5285 	ill_t	*ill;
5286 
5287 	ill = ipif->ipif_ill;
5288 	mutex_enter(&ill->ill_lock);
5289 	ipif->ipif_refcnt++;
5290 	IPIF_TRACE_REF(ipif);
5291 	mutex_exit(&ill->ill_lock);
5292 }
5293 
5294 /*
5295  * Must not be called while holding any locks. Otherwise if this is
5296  * the last reference to be released there is a chance of recursive mutex
5297  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5298  * to restart an ioctl.
5299  */
5300 void
5301 ipif_refrele(ipif_t *ipif)
5302 {
5303 	ill_t	*ill;
5304 
5305 	ill = ipif->ipif_ill;
5306 
5307 	mutex_enter(&ill->ill_lock);
5308 	ASSERT(ipif->ipif_refcnt != 0);
5309 	ipif->ipif_refcnt--;
5310 	IPIF_UNTRACE_REF(ipif);
5311 	if (ipif->ipif_refcnt != 0) {
5312 		mutex_exit(&ill->ill_lock);
5313 		return;
5314 	}
5315 
5316 	/* Drops the ill_lock */
5317 	ipif_ill_refrele_tail(ill);
5318 }
5319 
5320 ipif_t *
5321 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5322 {
5323 	ipif_t	*ipif;
5324 
5325 	mutex_enter(&ill->ill_lock);
5326 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5327 	    ipif != NULL; ipif = ipif->ipif_next) {
5328 		if (IPIF_IS_CONDEMNED(ipif))
5329 			continue;
5330 		ipif_refhold_locked(ipif);
5331 		mutex_exit(&ill->ill_lock);
5332 		return (ipif);
5333 	}
5334 	mutex_exit(&ill->ill_lock);
5335 	return (NULL);
5336 }
5337 
5338 /*
5339  * TODO: make this table extendible at run time
5340  * Return a pointer to the mac type info for 'mac_type'
5341  */
5342 static ip_m_t *
5343 ip_m_lookup(t_uscalar_t mac_type)
5344 {
5345 	ip_m_t	*ipm;
5346 
5347 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5348 		if (ipm->ip_m_mac_type == mac_type)
5349 			return (ipm);
5350 	return (NULL);
5351 }
5352 
5353 /*
5354  * Make a link layer address from the multicast IP address *addr.
5355  * To form the link layer address, invoke the ip_m_v*mapping function
5356  * associated with the link-layer type.
5357  */
5358 void
5359 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5360 {
5361 	ip_m_t *ipm;
5362 
5363 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5364 		return;
5365 
5366 	ASSERT(addr != NULL);
5367 
5368 	ipm = ip_m_lookup(ill->ill_mactype);
5369 	if (ipm == NULL ||
5370 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5371 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5372 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5373 		    ill->ill_name, ill->ill_mactype));
5374 		return;
5375 	}
5376 	if (ill->ill_isv6)
5377 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5378 	else
5379 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5380 }
5381 
5382 /*
5383  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5384  * ill is passed in to associate it with the correct interface.
5385  * If ire_arg is set, then we return the held IRE in that location.
5386  */
5387 int
5388 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5389     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5390     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5391 {
5392 	ire_t	*ire, *nire;
5393 	ire_t	*gw_ire = NULL;
5394 	ipif_t	*ipif = NULL;
5395 	uint_t	type;
5396 	int	match_flags = MATCH_IRE_TYPE;
5397 	tsol_gc_t *gc = NULL;
5398 	tsol_gcgrp_t *gcgrp = NULL;
5399 	boolean_t gcgrp_xtraref = B_FALSE;
5400 	boolean_t cgtp_broadcast;
5401 
5402 	ip1dbg(("ip_rt_add:"));
5403 
5404 	if (ire_arg != NULL)
5405 		*ire_arg = NULL;
5406 
5407 	/*
5408 	 * If this is the case of RTF_HOST being set, then we set the netmask
5409 	 * to all ones (regardless if one was supplied).
5410 	 */
5411 	if (flags & RTF_HOST)
5412 		mask = IP_HOST_MASK;
5413 
5414 	/*
5415 	 * Prevent routes with a zero gateway from being created (since
5416 	 * interfaces can currently be plumbed and brought up no assigned
5417 	 * address).
5418 	 */
5419 	if (gw_addr == 0)
5420 		return (ENETUNREACH);
5421 	/*
5422 	 * Get the ipif, if any, corresponding to the gw_addr
5423 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5424 	 * we match on the gatway and destination to handle unnumbered pt-pt
5425 	 * interfaces.
5426 	 */
5427 	if (ill != NULL)
5428 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5429 	else
5430 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5431 	if (ipif != NULL) {
5432 		if (IS_VNI(ipif->ipif_ill)) {
5433 			ipif_refrele(ipif);
5434 			return (EINVAL);
5435 		}
5436 	}
5437 
5438 	/*
5439 	 * GateD will attempt to create routes with a loopback interface
5440 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5441 	 * these routes to be added, but create them as interface routes
5442 	 * since the gateway is an interface address.
5443 	 */
5444 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5445 		flags &= ~RTF_GATEWAY;
5446 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5447 		    mask == IP_HOST_MASK) {
5448 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5449 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5450 			    NULL);
5451 			if (ire != NULL) {
5452 				ire_refrele(ire);
5453 				ipif_refrele(ipif);
5454 				return (EEXIST);
5455 			}
5456 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5457 			    "for 0x%x\n", (void *)ipif,
5458 			    ipif->ipif_ire_type,
5459 			    ntohl(ipif->ipif_lcl_addr)));
5460 			ire = ire_create(
5461 			    (uchar_t *)&dst_addr,	/* dest address */
5462 			    (uchar_t *)&mask,		/* mask */
5463 			    NULL,			/* no gateway */
5464 			    ipif->ipif_ire_type,	/* LOOPBACK */
5465 			    ipif->ipif_ill,
5466 			    zoneid,
5467 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5468 			    NULL,
5469 			    ipst);
5470 
5471 			if (ire == NULL) {
5472 				ipif_refrele(ipif);
5473 				return (ENOMEM);
5474 			}
5475 			/* src address assigned by the caller? */
5476 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5477 				ire->ire_setsrc_addr = src_addr;
5478 
5479 			nire = ire_add(ire);
5480 			if (nire == NULL) {
5481 				/*
5482 				 * In the result of failure, ire_add() will have
5483 				 * already deleted the ire in question, so there
5484 				 * is no need to do that here.
5485 				 */
5486 				ipif_refrele(ipif);
5487 				return (ENOMEM);
5488 			}
5489 			/*
5490 			 * Check if it was a duplicate entry. This handles
5491 			 * the case of two racing route adds for the same route
5492 			 */
5493 			if (nire != ire) {
5494 				ASSERT(nire->ire_identical_ref > 1);
5495 				ire_delete(nire);
5496 				ire_refrele(nire);
5497 				ipif_refrele(ipif);
5498 				return (EEXIST);
5499 			}
5500 			ire = nire;
5501 			goto save_ire;
5502 		}
5503 	}
5504 
5505 	/*
5506 	 * The routes for multicast with CGTP are quite special in that
5507 	 * the gateway is the local interface address, yet RTF_GATEWAY
5508 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5509 	 * this undocumented and unusual use of multicast routes.
5510 	 */
5511 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5512 		flags &= ~RTF_GATEWAY;
5513 
5514 	/*
5515 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5516 	 * and the gateway address provided is one of the system's interface
5517 	 * addresses.  By using the routing socket interface and supplying an
5518 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5519 	 * specifying an interface route to be created is available which uses
5520 	 * the interface index that specifies the outgoing interface rather than
5521 	 * the address of an outgoing interface (which may not be able to
5522 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5523 	 * flag, routes can be specified which not only specify the next-hop to
5524 	 * be used when routing to a certain prefix, but also which outgoing
5525 	 * interface should be used.
5526 	 *
5527 	 * Previously, interfaces would have unique addresses assigned to them
5528 	 * and so the address assigned to a particular interface could be used
5529 	 * to identify a particular interface.  One exception to this was the
5530 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5531 	 *
5532 	 * With the advent of IPv6 and its link-local addresses, this
5533 	 * restriction was relaxed and interfaces could share addresses between
5534 	 * themselves.  In fact, typically all of the link-local interfaces on
5535 	 * an IPv6 node or router will have the same link-local address.  In
5536 	 * order to differentiate between these interfaces, the use of an
5537 	 * interface index is necessary and this index can be carried inside a
5538 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5539 	 * of using the interface index, however, is that all of the ipif's that
5540 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5541 	 * cannot be used to differentiate between ipif's (or logical
5542 	 * interfaces) that belong to the same ill (physical interface).
5543 	 *
5544 	 * For example, in the following case involving IPv4 interfaces and
5545 	 * logical interfaces
5546 	 *
5547 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5548 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5549 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5550 	 *
5551 	 * the ipif's corresponding to each of these interface routes can be
5552 	 * uniquely identified by the "gateway" (actually interface address).
5553 	 *
5554 	 * In this case involving multiple IPv6 default routes to a particular
5555 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5556 	 * default route is of interest:
5557 	 *
5558 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5559 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5560 	 */
5561 
5562 	/* RTF_GATEWAY not set */
5563 	if (!(flags & RTF_GATEWAY)) {
5564 		if (sp != NULL) {
5565 			ip2dbg(("ip_rt_add: gateway security attributes "
5566 			    "cannot be set with interface route\n"));
5567 			if (ipif != NULL)
5568 				ipif_refrele(ipif);
5569 			return (EINVAL);
5570 		}
5571 
5572 		/*
5573 		 * Whether or not ill (RTA_IFP) is set, we require that
5574 		 * the gateway is one of our local addresses.
5575 		 */
5576 		if (ipif == NULL)
5577 			return (ENETUNREACH);
5578 
5579 		/*
5580 		 * We use MATCH_IRE_ILL here. If the caller specified an
5581 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5582 		 * we use the ill derived from the gateway address.
5583 		 * We can always match the gateway address since we record it
5584 		 * in ire_gateway_addr.
5585 		 * We don't allow RTA_IFP to specify a different ill than the
5586 		 * one matching the ipif to make sure we can delete the route.
5587 		 */
5588 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5589 		if (ill == NULL) {
5590 			ill = ipif->ipif_ill;
5591 		} else if (ill != ipif->ipif_ill) {
5592 			ipif_refrele(ipif);
5593 			return (EINVAL);
5594 		}
5595 
5596 		/*
5597 		 * We check for an existing entry at this point.
5598 		 *
5599 		 * Since a netmask isn't passed in via the ioctl interface
5600 		 * (SIOCADDRT), we don't check for a matching netmask in that
5601 		 * case.
5602 		 */
5603 		if (!ioctl_msg)
5604 			match_flags |= MATCH_IRE_MASK;
5605 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5606 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5607 		    NULL);
5608 		if (ire != NULL) {
5609 			ire_refrele(ire);
5610 			ipif_refrele(ipif);
5611 			return (EEXIST);
5612 		}
5613 
5614 		/*
5615 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5616 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5617 		 * gateway.
5618 		 */
5619 		ire = ire_create(
5620 		    (uchar_t *)&dst_addr,
5621 		    (uint8_t *)&mask,
5622 		    (uint8_t *)&gw_addr,
5623 		    ill->ill_net_type,
5624 		    ill,
5625 		    zoneid,
5626 		    flags,
5627 		    NULL,
5628 		    ipst);
5629 		if (ire == NULL) {
5630 			ipif_refrele(ipif);
5631 			return (ENOMEM);
5632 		}
5633 
5634 		/*
5635 		 * Some software (for example, GateD and Sun Cluster) attempts
5636 		 * to create (what amount to) IRE_PREFIX routes with the
5637 		 * loopback address as the gateway.  This is primarily done to
5638 		 * set up prefixes with the RTF_REJECT flag set (for example,
5639 		 * when generating aggregate routes.)
5640 		 *
5641 		 * If the IRE type (as defined by ill->ill_net_type) is
5642 		 * IRE_LOOPBACK, then we map the request into a
5643 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5644 		 * these interface routes, by definition, can only be that.
5645 		 *
5646 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5647 		 * routine, but rather using ire_create() directly.
5648 		 *
5649 		 */
5650 		if (ill->ill_net_type == IRE_LOOPBACK) {
5651 			ire->ire_type = IRE_IF_NORESOLVER;
5652 			ire->ire_flags |= RTF_BLACKHOLE;
5653 		}
5654 
5655 		/* src address assigned by the caller? */
5656 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5657 			ire->ire_setsrc_addr = src_addr;
5658 
5659 		nire = ire_add(ire);
5660 		if (nire == NULL) {
5661 			/*
5662 			 * In the result of failure, ire_add() will have
5663 			 * already deleted the ire in question, so there
5664 			 * is no need to do that here.
5665 			 */
5666 			ipif_refrele(ipif);
5667 			return (ENOMEM);
5668 		}
5669 		/*
5670 		 * Check if it was a duplicate entry. This handles
5671 		 * the case of two racing route adds for the same route
5672 		 */
5673 		if (nire != ire) {
5674 			ire_delete(nire);
5675 			ire_refrele(nire);
5676 			ipif_refrele(ipif);
5677 			return (EEXIST);
5678 		}
5679 		ire = nire;
5680 		goto save_ire;
5681 	}
5682 
5683 	/*
5684 	 * Get an interface IRE for the specified gateway.
5685 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5686 	 * gateway, it is currently unreachable and we fail the request
5687 	 * accordingly.
5688 	 * If RTA_IFP was specified we look on that particular ill.
5689 	 */
5690 	if (ill != NULL)
5691 		match_flags |= MATCH_IRE_ILL;
5692 
5693 	/* Check whether the gateway is reachable. */
5694 again:
5695 	type = IRE_INTERFACE;
5696 	if (flags & RTF_INDIRECT)
5697 		type |= IRE_OFFLINK;
5698 
5699 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5700 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5701 	if (gw_ire == NULL) {
5702 		/*
5703 		 * With IPMP, we allow host routes to influence in.mpathd's
5704 		 * target selection.  However, if the test addresses are on
5705 		 * their own network, the above lookup will fail since the
5706 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5707 		 * hidden test IREs to be found and try again.
5708 		 */
5709 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5710 			match_flags |= MATCH_IRE_TESTHIDDEN;
5711 			goto again;
5712 		}
5713 
5714 		if (ipif != NULL)
5715 			ipif_refrele(ipif);
5716 		return (ENETUNREACH);
5717 	}
5718 
5719 	/*
5720 	 * We create one of three types of IREs as a result of this request
5721 	 * based on the netmask.  A netmask of all ones (which is automatically
5722 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5723 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5724 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5725 	 * destination prefix.
5726 	 */
5727 	if (mask == IP_HOST_MASK)
5728 		type = IRE_HOST;
5729 	else if (mask == 0)
5730 		type = IRE_DEFAULT;
5731 	else
5732 		type = IRE_PREFIX;
5733 
5734 	/* check for a duplicate entry */
5735 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5736 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5737 	    0, ipst, NULL);
5738 	if (ire != NULL) {
5739 		if (ipif != NULL)
5740 			ipif_refrele(ipif);
5741 		ire_refrele(gw_ire);
5742 		ire_refrele(ire);
5743 		return (EEXIST);
5744 	}
5745 
5746 	/* Security attribute exists */
5747 	if (sp != NULL) {
5748 		tsol_gcgrp_addr_t ga;
5749 
5750 		/* find or create the gateway credentials group */
5751 		ga.ga_af = AF_INET;
5752 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5753 
5754 		/* we hold reference to it upon success */
5755 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5756 		if (gcgrp == NULL) {
5757 			if (ipif != NULL)
5758 				ipif_refrele(ipif);
5759 			ire_refrele(gw_ire);
5760 			return (ENOMEM);
5761 		}
5762 
5763 		/*
5764 		 * Create and add the security attribute to the group; a
5765 		 * reference to the group is made upon allocating a new
5766 		 * entry successfully.  If it finds an already-existing
5767 		 * entry for the security attribute in the group, it simply
5768 		 * returns it and no new reference is made to the group.
5769 		 */
5770 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5771 		if (gc == NULL) {
5772 			if (ipif != NULL)
5773 				ipif_refrele(ipif);
5774 			/* release reference held by gcgrp_lookup */
5775 			GCGRP_REFRELE(gcgrp);
5776 			ire_refrele(gw_ire);
5777 			return (ENOMEM);
5778 		}
5779 	}
5780 
5781 	/* Create the IRE. */
5782 	ire = ire_create(
5783 	    (uchar_t *)&dst_addr,		/* dest address */
5784 	    (uchar_t *)&mask,			/* mask */
5785 	    (uchar_t *)&gw_addr,		/* gateway address */
5786 	    (ushort_t)type,			/* IRE type */
5787 	    ill,
5788 	    zoneid,
5789 	    flags,
5790 	    gc,					/* security attribute */
5791 	    ipst);
5792 
5793 	/*
5794 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5795 	 * reference to the 'gcgrp'. We can now release the extra reference
5796 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5797 	 */
5798 	if (gcgrp_xtraref)
5799 		GCGRP_REFRELE(gcgrp);
5800 	if (ire == NULL) {
5801 		if (gc != NULL)
5802 			GC_REFRELE(gc);
5803 		if (ipif != NULL)
5804 			ipif_refrele(ipif);
5805 		ire_refrele(gw_ire);
5806 		return (ENOMEM);
5807 	}
5808 
5809 	/* Before we add, check if an extra CGTP broadcast is needed */
5810 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5811 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5812 
5813 	/* src address assigned by the caller? */
5814 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5815 		ire->ire_setsrc_addr = src_addr;
5816 
5817 	/*
5818 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5819 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5820 	 */
5821 
5822 	/* Add the new IRE. */
5823 	nire = ire_add(ire);
5824 	if (nire == NULL) {
5825 		/*
5826 		 * In the result of failure, ire_add() will have
5827 		 * already deleted the ire in question, so there
5828 		 * is no need to do that here.
5829 		 */
5830 		if (ipif != NULL)
5831 			ipif_refrele(ipif);
5832 		ire_refrele(gw_ire);
5833 		return (ENOMEM);
5834 	}
5835 	/*
5836 	 * Check if it was a duplicate entry. This handles
5837 	 * the case of two racing route adds for the same route
5838 	 */
5839 	if (nire != ire) {
5840 		ire_delete(nire);
5841 		ire_refrele(nire);
5842 		if (ipif != NULL)
5843 			ipif_refrele(ipif);
5844 		ire_refrele(gw_ire);
5845 		return (EEXIST);
5846 	}
5847 	ire = nire;
5848 
5849 	if (flags & RTF_MULTIRT) {
5850 		/*
5851 		 * Invoke the CGTP (multirouting) filtering module
5852 		 * to add the dst address in the filtering database.
5853 		 * Replicated inbound packets coming from that address
5854 		 * will be filtered to discard the duplicates.
5855 		 * It is not necessary to call the CGTP filter hook
5856 		 * when the dst address is a broadcast or multicast,
5857 		 * because an IP source address cannot be a broadcast
5858 		 * or a multicast.
5859 		 */
5860 		if (cgtp_broadcast) {
5861 			ip_cgtp_bcast_add(ire, ipst);
5862 			goto save_ire;
5863 		}
5864 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5865 		    !CLASSD(ire->ire_addr)) {
5866 			int res;
5867 			ipif_t *src_ipif;
5868 
5869 			/* Find the source address corresponding to gw_ire */
5870 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5871 			    NULL, zoneid, ipst);
5872 			if (src_ipif != NULL) {
5873 				res = ipst->ips_ip_cgtp_filter_ops->
5874 				    cfo_add_dest_v4(
5875 				    ipst->ips_netstack->netstack_stackid,
5876 				    ire->ire_addr,
5877 				    ire->ire_gateway_addr,
5878 				    ire->ire_setsrc_addr,
5879 				    src_ipif->ipif_lcl_addr);
5880 				ipif_refrele(src_ipif);
5881 			} else {
5882 				res = EADDRNOTAVAIL;
5883 			}
5884 			if (res != 0) {
5885 				if (ipif != NULL)
5886 					ipif_refrele(ipif);
5887 				ire_refrele(gw_ire);
5888 				ire_delete(ire);
5889 				ire_refrele(ire);	/* Held in ire_add */
5890 				return (res);
5891 			}
5892 		}
5893 	}
5894 
5895 save_ire:
5896 	if (gw_ire != NULL) {
5897 		ire_refrele(gw_ire);
5898 		gw_ire = NULL;
5899 	}
5900 	if (ill != NULL) {
5901 		/*
5902 		 * Save enough information so that we can recreate the IRE if
5903 		 * the interface goes down and then up.  The metrics associated
5904 		 * with the route will be saved as well when rts_setmetrics() is
5905 		 * called after the IRE has been created.  In the case where
5906 		 * memory cannot be allocated, none of this information will be
5907 		 * saved.
5908 		 */
5909 		ill_save_ire(ill, ire);
5910 	}
5911 	if (ioctl_msg)
5912 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5913 	if (ire_arg != NULL) {
5914 		/*
5915 		 * Store the ire that was successfully added into where ire_arg
5916 		 * points to so that callers don't have to look it up
5917 		 * themselves (but they are responsible for ire_refrele()ing
5918 		 * the ire when they are finished with it).
5919 		 */
5920 		*ire_arg = ire;
5921 	} else {
5922 		ire_refrele(ire);		/* Held in ire_add */
5923 	}
5924 	if (ipif != NULL)
5925 		ipif_refrele(ipif);
5926 	return (0);
5927 }
5928 
5929 /*
5930  * ip_rt_delete is called to delete an IPv4 route.
5931  * ill is passed in to associate it with the correct interface.
5932  */
5933 /* ARGSUSED4 */
5934 int
5935 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5936     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5937     ip_stack_t *ipst, zoneid_t zoneid)
5938 {
5939 	ire_t	*ire = NULL;
5940 	ipif_t	*ipif;
5941 	uint_t	type;
5942 	uint_t	match_flags = MATCH_IRE_TYPE;
5943 	int	err = 0;
5944 
5945 	ip1dbg(("ip_rt_delete:"));
5946 	/*
5947 	 * If this is the case of RTF_HOST being set, then we set the netmask
5948 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5949 	 */
5950 	if (flags & RTF_HOST) {
5951 		mask = IP_HOST_MASK;
5952 		match_flags |= MATCH_IRE_MASK;
5953 	} else if (rtm_addrs & RTA_NETMASK) {
5954 		match_flags |= MATCH_IRE_MASK;
5955 	}
5956 
5957 	/*
5958 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5959 	 * we check if the gateway address is one of our interfaces first,
5960 	 * and fall back on RTF_GATEWAY routes.
5961 	 *
5962 	 * This makes it possible to delete an original
5963 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5964 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5965 	 * and those can not be deleted here.
5966 	 *
5967 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5968 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5969 	 * otherwise we use the ill derived from the gateway address.
5970 	 * We can always match the gateway address since we record it
5971 	 * in ire_gateway_addr.
5972 	 *
5973 	 * For more detail on specifying routes by gateway address and by
5974 	 * interface index, see the comments in ip_rt_add().
5975 	 */
5976 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5977 	if (ipif != NULL) {
5978 		ill_t	*ill_match;
5979 
5980 		if (ill != NULL)
5981 			ill_match = ill;
5982 		else
5983 			ill_match = ipif->ipif_ill;
5984 
5985 		match_flags |= MATCH_IRE_ILL;
5986 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5987 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5988 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
5989 			    NULL);
5990 		}
5991 		if (ire == NULL) {
5992 			match_flags |= MATCH_IRE_GW;
5993 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5994 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5995 			    match_flags, 0, ipst, NULL);
5996 		}
5997 		/* Avoid deleting routes created by kernel from an ipif */
5998 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5999 			ire_refrele(ire);
6000 			ire = NULL;
6001 		}
6002 
6003 		/* Restore in case we didn't find a match */
6004 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6005 	}
6006 
6007 	if (ire == NULL) {
6008 		/*
6009 		 * At this point, the gateway address is not one of our own
6010 		 * addresses or a matching interface route was not found.  We
6011 		 * set the IRE type to lookup based on whether
6012 		 * this is a host route, a default route or just a prefix.
6013 		 *
6014 		 * If an ill was passed in, then the lookup is based on an
6015 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6016 		 */
6017 		match_flags |= MATCH_IRE_GW;
6018 		if (ill != NULL)
6019 			match_flags |= MATCH_IRE_ILL;
6020 		if (mask == IP_HOST_MASK)
6021 			type = IRE_HOST;
6022 		else if (mask == 0)
6023 			type = IRE_DEFAULT;
6024 		else
6025 			type = IRE_PREFIX;
6026 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6027 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6028 	}
6029 
6030 	if (ipif != NULL) {
6031 		ipif_refrele(ipif);
6032 		ipif = NULL;
6033 	}
6034 
6035 	if (ire == NULL)
6036 		return (ESRCH);
6037 
6038 	if (ire->ire_flags & RTF_MULTIRT) {
6039 		/*
6040 		 * Invoke the CGTP (multirouting) filtering module
6041 		 * to remove the dst address from the filtering database.
6042 		 * Packets coming from that address will no longer be
6043 		 * filtered to remove duplicates.
6044 		 */
6045 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6046 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6047 			    ipst->ips_netstack->netstack_stackid,
6048 			    ire->ire_addr, ire->ire_gateway_addr);
6049 		}
6050 		ip_cgtp_bcast_delete(ire, ipst);
6051 	}
6052 
6053 	ill = ire->ire_ill;
6054 	if (ill != NULL)
6055 		ill_remove_saved_ire(ill, ire);
6056 	if (ioctl_msg)
6057 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6058 	ire_delete(ire);
6059 	ire_refrele(ire);
6060 	return (err);
6061 }
6062 
6063 /*
6064  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6065  */
6066 /* ARGSUSED */
6067 int
6068 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6069     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6070 {
6071 	ipaddr_t dst_addr;
6072 	ipaddr_t gw_addr;
6073 	ipaddr_t mask;
6074 	int error = 0;
6075 	mblk_t *mp1;
6076 	struct rtentry *rt;
6077 	ipif_t *ipif = NULL;
6078 	ip_stack_t	*ipst;
6079 
6080 	ASSERT(q->q_next == NULL);
6081 	ipst = CONNQ_TO_IPST(q);
6082 
6083 	ip1dbg(("ip_siocaddrt:"));
6084 	/* Existence of mp1 verified in ip_wput_nondata */
6085 	mp1 = mp->b_cont->b_cont;
6086 	rt = (struct rtentry *)mp1->b_rptr;
6087 
6088 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6089 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6090 
6091 	/*
6092 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6093 	 * to a particular host address.  In this case, we set the netmask to
6094 	 * all ones for the particular destination address.  Otherwise,
6095 	 * determine the netmask to be used based on dst_addr and the interfaces
6096 	 * in use.
6097 	 */
6098 	if (rt->rt_flags & RTF_HOST) {
6099 		mask = IP_HOST_MASK;
6100 	} else {
6101 		/*
6102 		 * Note that ip_subnet_mask returns a zero mask in the case of
6103 		 * default (an all-zeroes address).
6104 		 */
6105 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6106 	}
6107 
6108 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6109 	    B_TRUE, NULL, ipst, ALL_ZONES);
6110 	if (ipif != NULL)
6111 		ipif_refrele(ipif);
6112 	return (error);
6113 }
6114 
6115 /*
6116  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6117  */
6118 /* ARGSUSED */
6119 int
6120 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6121     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6122 {
6123 	ipaddr_t dst_addr;
6124 	ipaddr_t gw_addr;
6125 	ipaddr_t mask;
6126 	int error;
6127 	mblk_t *mp1;
6128 	struct rtentry *rt;
6129 	ipif_t *ipif = NULL;
6130 	ip_stack_t	*ipst;
6131 
6132 	ASSERT(q->q_next == NULL);
6133 	ipst = CONNQ_TO_IPST(q);
6134 
6135 	ip1dbg(("ip_siocdelrt:"));
6136 	/* Existence of mp1 verified in ip_wput_nondata */
6137 	mp1 = mp->b_cont->b_cont;
6138 	rt = (struct rtentry *)mp1->b_rptr;
6139 
6140 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6141 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6142 
6143 	/*
6144 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6145 	 * to a particular host address.  In this case, we set the netmask to
6146 	 * all ones for the particular destination address.  Otherwise,
6147 	 * determine the netmask to be used based on dst_addr and the interfaces
6148 	 * in use.
6149 	 */
6150 	if (rt->rt_flags & RTF_HOST) {
6151 		mask = IP_HOST_MASK;
6152 	} else {
6153 		/*
6154 		 * Note that ip_subnet_mask returns a zero mask in the case of
6155 		 * default (an all-zeroes address).
6156 		 */
6157 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6158 	}
6159 
6160 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6161 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6162 	    ipst, ALL_ZONES);
6163 	if (ipif != NULL)
6164 		ipif_refrele(ipif);
6165 	return (error);
6166 }
6167 
6168 /*
6169  * Enqueue the mp onto the ipsq, chained by b_next.
6170  * b_prev stores the function to be executed later, and b_queue the queue
6171  * where this mp originated.
6172  */
6173 void
6174 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6175     ill_t *pending_ill)
6176 {
6177 	conn_t	*connp;
6178 	ipxop_t *ipx = ipsq->ipsq_xop;
6179 
6180 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6181 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6182 	ASSERT(func != NULL);
6183 
6184 	mp->b_queue = q;
6185 	mp->b_prev = (void *)func;
6186 	mp->b_next = NULL;
6187 
6188 	switch (type) {
6189 	case CUR_OP:
6190 		if (ipx->ipx_mptail != NULL) {
6191 			ASSERT(ipx->ipx_mphead != NULL);
6192 			ipx->ipx_mptail->b_next = mp;
6193 		} else {
6194 			ASSERT(ipx->ipx_mphead == NULL);
6195 			ipx->ipx_mphead = mp;
6196 		}
6197 		ipx->ipx_mptail = mp;
6198 		break;
6199 
6200 	case NEW_OP:
6201 		if (ipsq->ipsq_xopq_mptail != NULL) {
6202 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6203 			ipsq->ipsq_xopq_mptail->b_next = mp;
6204 		} else {
6205 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6206 			ipsq->ipsq_xopq_mphead = mp;
6207 		}
6208 		ipsq->ipsq_xopq_mptail = mp;
6209 		ipx->ipx_ipsq_queued = B_TRUE;
6210 		break;
6211 
6212 	case SWITCH_OP:
6213 		ASSERT(ipsq->ipsq_swxop != NULL);
6214 		/* only one switch operation is currently allowed */
6215 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6216 		ipsq->ipsq_switch_mp = mp;
6217 		ipx->ipx_ipsq_queued = B_TRUE;
6218 		break;
6219 	default:
6220 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6221 	}
6222 
6223 	if (CONN_Q(q) && pending_ill != NULL) {
6224 		connp = Q_TO_CONN(q);
6225 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6226 		connp->conn_oper_pending_ill = pending_ill;
6227 	}
6228 }
6229 
6230 /*
6231  * Dequeue the next message that requested exclusive access to this IPSQ's
6232  * xop.  Specifically:
6233  *
6234  *  1. If we're still processing the current operation on `ipsq', then
6235  *     dequeue the next message for the operation (from ipx_mphead), or
6236  *     return NULL if there are no queued messages for the operation.
6237  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6238  *
6239  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6240  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6241  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6242  *     leaving IPMP groups and require a careful dance -- see the comments
6243  *     in-line below for details.  If we're leaving a group xop or if we're
6244  *     joining a group xop and become writer on it, then we proceed to (3).
6245  *     Otherwise, we return NULL and exit the xop.
6246  *
6247  *  3. For each IPSQ in the xop, return any switch operation stored on
6248  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6249  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6250  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6251  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6252  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6253  *     each phyint in the group, including the IPMP meta-interface phyint.
6254  */
6255 static mblk_t *
6256 ipsq_dq(ipsq_t *ipsq)
6257 {
6258 	ill_t	*illv4, *illv6;
6259 	mblk_t	*mp;
6260 	ipsq_t	*xopipsq;
6261 	ipsq_t	*leftipsq = NULL;
6262 	ipxop_t *ipx;
6263 	phyint_t *phyi = ipsq->ipsq_phyint;
6264 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6265 	boolean_t emptied = B_FALSE;
6266 
6267 	/*
6268 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6269 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6270 	 */
6271 	rw_enter(&ipst->ips_ill_g_lock,
6272 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6273 	mutex_enter(&ipsq->ipsq_lock);
6274 	ipx = ipsq->ipsq_xop;
6275 	mutex_enter(&ipx->ipx_lock);
6276 
6277 	/*
6278 	 * Dequeue the next message associated with the current exclusive
6279 	 * operation, if any.
6280 	 */
6281 	if ((mp = ipx->ipx_mphead) != NULL) {
6282 		ipx->ipx_mphead = mp->b_next;
6283 		if (ipx->ipx_mphead == NULL)
6284 			ipx->ipx_mptail = NULL;
6285 		mp->b_next = (void *)ipsq;
6286 		goto out;
6287 	}
6288 
6289 	if (ipx->ipx_current_ipif != NULL)
6290 		goto empty;
6291 
6292 	if (ipsq->ipsq_swxop != NULL) {
6293 		/*
6294 		 * The exclusive operation that is now being completed has
6295 		 * requested a switch to a different xop.  This happens
6296 		 * when an interface joins or leaves an IPMP group.  Joins
6297 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6298 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6299 		 * (phyint_free()), or interface plumb for an ill type
6300 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6301 		 *
6302 		 * Xop switches are not allowed on the IPMP meta-interface.
6303 		 */
6304 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6305 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6306 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6307 
6308 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6309 			/*
6310 			 * We're switching back to our own xop, so we have two
6311 			 * xop's to drain/exit: our own, and the group xop
6312 			 * that we are leaving.
6313 			 *
6314 			 * First, pull ourselves out of the group ipsq list.
6315 			 * This is safe since we're writer on ill_g_lock.
6316 			 */
6317 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6318 
6319 			xopipsq = ipx->ipx_ipsq;
6320 			while (xopipsq->ipsq_next != ipsq)
6321 				xopipsq = xopipsq->ipsq_next;
6322 
6323 			xopipsq->ipsq_next = ipsq->ipsq_next;
6324 			ipsq->ipsq_next = ipsq;
6325 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6326 			ipsq->ipsq_swxop = NULL;
6327 
6328 			/*
6329 			 * Second, prepare to exit the group xop.  The actual
6330 			 * ipsq_exit() is done at the end of this function
6331 			 * since we cannot hold any locks across ipsq_exit().
6332 			 * Note that although we drop the group's ipx_lock, no
6333 			 * threads can proceed since we're still ipx_writer.
6334 			 */
6335 			leftipsq = xopipsq;
6336 			mutex_exit(&ipx->ipx_lock);
6337 
6338 			/*
6339 			 * Third, set ipx to point to our own xop (which was
6340 			 * inactive and therefore can be entered).
6341 			 */
6342 			ipx = ipsq->ipsq_xop;
6343 			mutex_enter(&ipx->ipx_lock);
6344 			ASSERT(ipx->ipx_writer == NULL);
6345 			ASSERT(ipx->ipx_current_ipif == NULL);
6346 		} else {
6347 			/*
6348 			 * We're switching from our own xop to a group xop.
6349 			 * The requestor of the switch must ensure that the
6350 			 * group xop cannot go away (e.g. by ensuring the
6351 			 * phyint associated with the xop cannot go away).
6352 			 *
6353 			 * If we can become writer on our new xop, then we'll
6354 			 * do the drain.  Otherwise, the current writer of our
6355 			 * new xop will do the drain when it exits.
6356 			 *
6357 			 * First, splice ourselves into the group IPSQ list.
6358 			 * This is safe since we're writer on ill_g_lock.
6359 			 */
6360 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6361 
6362 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6363 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6364 				xopipsq = xopipsq->ipsq_next;
6365 
6366 			xopipsq->ipsq_next = ipsq;
6367 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6368 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6369 			ipsq->ipsq_swxop = NULL;
6370 
6371 			/*
6372 			 * Second, exit our own xop, since it's now unused.
6373 			 * This is safe since we've got the only reference.
6374 			 */
6375 			ASSERT(ipx->ipx_writer == curthread);
6376 			ipx->ipx_writer = NULL;
6377 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6378 			ipx->ipx_ipsq_queued = B_FALSE;
6379 			mutex_exit(&ipx->ipx_lock);
6380 
6381 			/*
6382 			 * Third, set ipx to point to our new xop, and check
6383 			 * if we can become writer on it.  If we cannot, then
6384 			 * the current writer will drain the IPSQ group when
6385 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6386 			 * because we're still holding ipsq_lock.
6387 			 */
6388 			ipx = ipsq->ipsq_xop;
6389 			mutex_enter(&ipx->ipx_lock);
6390 			if (ipx->ipx_writer != NULL ||
6391 			    ipx->ipx_current_ipif != NULL) {
6392 				goto out;
6393 			}
6394 		}
6395 
6396 		/*
6397 		 * Fourth, become writer on our new ipx before we continue
6398 		 * with the drain.  Note that we never dropped ipsq_lock
6399 		 * above, so no other thread could've raced with us to
6400 		 * become writer first.  Also, we're holding ipx_lock, so
6401 		 * no other thread can examine the ipx right now.
6402 		 */
6403 		ASSERT(ipx->ipx_current_ipif == NULL);
6404 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6405 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6406 		ipx->ipx_writer = curthread;
6407 		ipx->ipx_forced = B_FALSE;
6408 #ifdef DEBUG
6409 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6410 #endif
6411 	}
6412 
6413 	xopipsq = ipsq;
6414 	do {
6415 		/*
6416 		 * So that other operations operate on a consistent and
6417 		 * complete phyint, a switch message on an IPSQ must be
6418 		 * handled prior to any other operations on that IPSQ.
6419 		 */
6420 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6421 			xopipsq->ipsq_switch_mp = NULL;
6422 			ASSERT(mp->b_next == NULL);
6423 			mp->b_next = (void *)xopipsq;
6424 			goto out;
6425 		}
6426 
6427 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6428 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6429 			if (xopipsq->ipsq_xopq_mphead == NULL)
6430 				xopipsq->ipsq_xopq_mptail = NULL;
6431 			mp->b_next = (void *)xopipsq;
6432 			goto out;
6433 		}
6434 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6435 empty:
6436 	/*
6437 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6438 	 * new messages can end up on any IPSQ in the xop.
6439 	 */
6440 	ipx->ipx_writer = NULL;
6441 	ipx->ipx_forced = B_FALSE;
6442 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6443 	ipx->ipx_ipsq_queued = B_FALSE;
6444 	emptied = B_TRUE;
6445 #ifdef	DEBUG
6446 	ipx->ipx_depth = 0;
6447 #endif
6448 out:
6449 	mutex_exit(&ipx->ipx_lock);
6450 	mutex_exit(&ipsq->ipsq_lock);
6451 
6452 	/*
6453 	 * If we completely emptied the xop, then wake up any threads waiting
6454 	 * to enter any of the IPSQ's associated with it.
6455 	 */
6456 	if (emptied) {
6457 		xopipsq = ipsq;
6458 		do {
6459 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6460 				continue;
6461 
6462 			illv4 = phyi->phyint_illv4;
6463 			illv6 = phyi->phyint_illv6;
6464 
6465 			GRAB_ILL_LOCKS(illv4, illv6);
6466 			if (illv4 != NULL)
6467 				cv_broadcast(&illv4->ill_cv);
6468 			if (illv6 != NULL)
6469 				cv_broadcast(&illv6->ill_cv);
6470 			RELEASE_ILL_LOCKS(illv4, illv6);
6471 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6472 	}
6473 	rw_exit(&ipst->ips_ill_g_lock);
6474 
6475 	/*
6476 	 * Now that all locks are dropped, exit the IPSQ we left.
6477 	 */
6478 	if (leftipsq != NULL)
6479 		ipsq_exit(leftipsq);
6480 
6481 	return (mp);
6482 }
6483 
6484 /*
6485  * Return completion status of previously initiated DLPI operations on
6486  * ills in the purview of an ipsq.
6487  */
6488 static boolean_t
6489 ipsq_dlpi_done(ipsq_t *ipsq)
6490 {
6491 	ipsq_t		*ipsq_start;
6492 	phyint_t	*phyi;
6493 	ill_t		*ill;
6494 
6495 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6496 	ipsq_start = ipsq;
6497 
6498 	do {
6499 		/*
6500 		 * The only current users of this function are ipsq_try_enter
6501 		 * and ipsq_enter which have made sure that ipsq_writer is
6502 		 * NULL before we reach here. ill_dlpi_pending is modified
6503 		 * only by an ipsq writer
6504 		 */
6505 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6506 		phyi = ipsq->ipsq_phyint;
6507 		/*
6508 		 * phyi could be NULL if a phyint that is part of an
6509 		 * IPMP group is being unplumbed. A more detailed
6510 		 * comment is in ipmp_grp_update_kstats()
6511 		 */
6512 		if (phyi != NULL) {
6513 			ill = phyi->phyint_illv4;
6514 			if (ill != NULL &&
6515 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6516 			    ill->ill_arl_dlpi_pending))
6517 				return (B_FALSE);
6518 
6519 			ill = phyi->phyint_illv6;
6520 			if (ill != NULL &&
6521 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6522 				return (B_FALSE);
6523 		}
6524 
6525 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6526 
6527 	return (B_TRUE);
6528 }
6529 
6530 /*
6531  * Enter the ipsq corresponding to ill, by waiting synchronously till
6532  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6533  * will have to drain completely before ipsq_enter returns success.
6534  * ipx_current_ipif will be set if some exclusive op is in progress,
6535  * and the ipsq_exit logic will start the next enqueued op after
6536  * completion of the current op. If 'force' is used, we don't wait
6537  * for the enqueued ops. This is needed when a conn_close wants to
6538  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6539  * of an ill can also use this option. But we dont' use it currently.
6540  */
6541 #define	ENTER_SQ_WAIT_TICKS 100
6542 boolean_t
6543 ipsq_enter(ill_t *ill, boolean_t force, int type)
6544 {
6545 	ipsq_t	*ipsq;
6546 	ipxop_t *ipx;
6547 	boolean_t waited_enough = B_FALSE;
6548 	ip_stack_t *ipst = ill->ill_ipst;
6549 
6550 	/*
6551 	 * Note that the relationship between ill and ipsq is fixed as long as
6552 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6553 	 * relationship between the IPSQ and xop cannot change.  However,
6554 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6555 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6556 	 * waking up all ills in the xop when it becomes available.
6557 	 */
6558 	for (;;) {
6559 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6560 		mutex_enter(&ill->ill_lock);
6561 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6562 			mutex_exit(&ill->ill_lock);
6563 			rw_exit(&ipst->ips_ill_g_lock);
6564 			return (B_FALSE);
6565 		}
6566 
6567 		ipsq = ill->ill_phyint->phyint_ipsq;
6568 		mutex_enter(&ipsq->ipsq_lock);
6569 		ipx = ipsq->ipsq_xop;
6570 		mutex_enter(&ipx->ipx_lock);
6571 
6572 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6573 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6574 		    waited_enough))
6575 			break;
6576 
6577 		rw_exit(&ipst->ips_ill_g_lock);
6578 
6579 		if (!force || ipx->ipx_writer != NULL) {
6580 			mutex_exit(&ipx->ipx_lock);
6581 			mutex_exit(&ipsq->ipsq_lock);
6582 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6583 		} else {
6584 			mutex_exit(&ipx->ipx_lock);
6585 			mutex_exit(&ipsq->ipsq_lock);
6586 			(void) cv_reltimedwait(&ill->ill_cv,
6587 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6588 			waited_enough = B_TRUE;
6589 		}
6590 		mutex_exit(&ill->ill_lock);
6591 	}
6592 
6593 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6594 	ASSERT(ipx->ipx_reentry_cnt == 0);
6595 	ipx->ipx_writer = curthread;
6596 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6597 	ipx->ipx_reentry_cnt++;
6598 #ifdef DEBUG
6599 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6600 #endif
6601 	mutex_exit(&ipx->ipx_lock);
6602 	mutex_exit(&ipsq->ipsq_lock);
6603 	mutex_exit(&ill->ill_lock);
6604 	rw_exit(&ipst->ips_ill_g_lock);
6605 
6606 	return (B_TRUE);
6607 }
6608 
6609 /*
6610  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6611  * across the call to the core interface ipsq_try_enter() and hence calls this
6612  * function directly. This is explained more fully in ipif_set_values().
6613  * In order to support the above constraint, ipsq_try_enter is implemented as
6614  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6615  */
6616 static ipsq_t *
6617 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6618     int type, boolean_t reentry_ok)
6619 {
6620 	ipsq_t	*ipsq;
6621 	ipxop_t	*ipx;
6622 	ip_stack_t *ipst = ill->ill_ipst;
6623 
6624 	/*
6625 	 * lock ordering:
6626 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6627 	 *
6628 	 * ipx of an ipsq can't change when ipsq_lock is held.
6629 	 */
6630 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6631 	GRAB_CONN_LOCK(q);
6632 	mutex_enter(&ill->ill_lock);
6633 	ipsq = ill->ill_phyint->phyint_ipsq;
6634 	mutex_enter(&ipsq->ipsq_lock);
6635 	ipx = ipsq->ipsq_xop;
6636 	mutex_enter(&ipx->ipx_lock);
6637 
6638 	/*
6639 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6640 	 *    (Note: If the caller does not specify reentry_ok then neither
6641 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6642 	 *    again. Otherwise it can lead to an infinite loop
6643 	 * 2. Enter the ipsq if there is no current writer and this attempted
6644 	 *    entry is part of the current operation
6645 	 * 3. Enter the ipsq if there is no current writer and this is a new
6646 	 *    operation and the operation queue is empty and there is no
6647 	 *    operation currently in progress and if all previously initiated
6648 	 *    DLPI operations have completed.
6649 	 */
6650 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6651 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6652 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6653 	    ipsq_dlpi_done(ipsq))))) {
6654 		/* Success. */
6655 		ipx->ipx_reentry_cnt++;
6656 		ipx->ipx_writer = curthread;
6657 		ipx->ipx_forced = B_FALSE;
6658 		mutex_exit(&ipx->ipx_lock);
6659 		mutex_exit(&ipsq->ipsq_lock);
6660 		mutex_exit(&ill->ill_lock);
6661 		RELEASE_CONN_LOCK(q);
6662 #ifdef DEBUG
6663 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6664 #endif
6665 		return (ipsq);
6666 	}
6667 
6668 	if (func != NULL)
6669 		ipsq_enq(ipsq, q, mp, func, type, ill);
6670 
6671 	mutex_exit(&ipx->ipx_lock);
6672 	mutex_exit(&ipsq->ipsq_lock);
6673 	mutex_exit(&ill->ill_lock);
6674 	RELEASE_CONN_LOCK(q);
6675 	return (NULL);
6676 }
6677 
6678 /*
6679  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6680  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6681  * There is one ipsq per phyint. The ipsq
6682  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6683  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6684  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6685  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6686  * up the interface) and are enqueued in ipx_mphead.
6687  *
6688  * If a thread does not want to reenter the ipsq when it is already writer,
6689  * it must make sure that the specified reentry point to be called later
6690  * when the ipsq is empty, nor any code path starting from the specified reentry
6691  * point must never ever try to enter the ipsq again. Otherwise it can lead
6692  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6693  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6694  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6695  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6696  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6697  * ioctl if the current ioctl has completed. If the current ioctl is still
6698  * in progress it simply returns. The current ioctl could be waiting for
6699  * a response from another module (the driver or could be waiting for
6700  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6701  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6702  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6703  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6704  * all associated DLPI operations have completed.
6705  */
6706 
6707 /*
6708  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6709  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6710  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6711  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6712  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6713  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6714  */
6715 ipsq_t *
6716 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6717     ipsq_func_t func, int type, boolean_t reentry_ok)
6718 {
6719 	ip_stack_t	*ipst;
6720 	ipsq_t		*ipsq;
6721 
6722 	/* Only 1 of ipif or ill can be specified */
6723 	ASSERT((ipif != NULL) ^ (ill != NULL));
6724 
6725 	if (ipif != NULL)
6726 		ill = ipif->ipif_ill;
6727 	ipst = ill->ill_ipst;
6728 
6729 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6730 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6731 	rw_exit(&ipst->ips_ill_g_lock);
6732 
6733 	return (ipsq);
6734 }
6735 
6736 /*
6737  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6738  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6739  * cannot be entered, the mp is queued for completion.
6740  */
6741 void
6742 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6743     boolean_t reentry_ok)
6744 {
6745 	ipsq_t	*ipsq;
6746 
6747 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6748 
6749 	/*
6750 	 * Drop the caller's refhold on the ill.  This is safe since we either
6751 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6752 	 * IPSQ, in which case we return without accessing ill anymore.  This
6753 	 * is needed because func needs to see the correct refcount.
6754 	 * e.g. removeif can work only then.
6755 	 */
6756 	ill_refrele(ill);
6757 	if (ipsq != NULL) {
6758 		(*func)(ipsq, q, mp, NULL);
6759 		ipsq_exit(ipsq);
6760 	}
6761 }
6762 
6763 /*
6764  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6765  * prior to exiting.  Caller must be writer on the specified IPSQ.
6766  */
6767 void
6768 ipsq_exit(ipsq_t *ipsq)
6769 {
6770 	mblk_t *mp;
6771 	ipsq_t *mp_ipsq;
6772 	queue_t	*q;
6773 	phyint_t *phyi;
6774 	ipsq_func_t func;
6775 
6776 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6777 
6778 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6779 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6780 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6781 		return;
6782 	}
6783 
6784 	for (;;) {
6785 		phyi = ipsq->ipsq_phyint;
6786 		mp = ipsq_dq(ipsq);
6787 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6788 
6789 		/*
6790 		 * If we've changed to a new IPSQ, and the phyint associated
6791 		 * with the old one has gone away, free the old IPSQ.  Note
6792 		 * that this cannot happen while the IPSQ is in a group.
6793 		 */
6794 		if (mp_ipsq != ipsq && phyi == NULL) {
6795 			ASSERT(ipsq->ipsq_next == ipsq);
6796 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6797 			ipsq_delete(ipsq);
6798 		}
6799 
6800 		if (mp == NULL)
6801 			break;
6802 
6803 		q = mp->b_queue;
6804 		func = (ipsq_func_t)mp->b_prev;
6805 		ipsq = mp_ipsq;
6806 		mp->b_next = mp->b_prev = NULL;
6807 		mp->b_queue = NULL;
6808 
6809 		/*
6810 		 * If 'q' is an conn queue, it is valid, since we did a
6811 		 * a refhold on the conn at the start of the ioctl.
6812 		 * If 'q' is an ill queue, it is valid, since close of an
6813 		 * ill will clean up its IPSQ.
6814 		 */
6815 		(*func)(ipsq, q, mp, NULL);
6816 	}
6817 }
6818 
6819 /*
6820  * Used to start any igmp or mld timers that could not be started
6821  * while holding ill_mcast_lock. The timers can't be started while holding
6822  * the lock, since mld/igmp_start_timers may need to call untimeout()
6823  * which can't be done while holding the lock which the timeout handler
6824  * acquires. Otherwise
6825  * there could be a deadlock since the timeout handlers
6826  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6827  * ill_mcast_lock.
6828  */
6829 void
6830 ill_mcast_timer_start(ip_stack_t *ipst)
6831 {
6832 	int		next;
6833 
6834 	mutex_enter(&ipst->ips_igmp_timer_lock);
6835 	next = ipst->ips_igmp_deferred_next;
6836 	ipst->ips_igmp_deferred_next = INFINITY;
6837 	mutex_exit(&ipst->ips_igmp_timer_lock);
6838 
6839 	if (next != INFINITY)
6840 		igmp_start_timers(next, ipst);
6841 
6842 	mutex_enter(&ipst->ips_mld_timer_lock);
6843 	next = ipst->ips_mld_deferred_next;
6844 	ipst->ips_mld_deferred_next = INFINITY;
6845 	mutex_exit(&ipst->ips_mld_timer_lock);
6846 
6847 	if (next != INFINITY)
6848 		mld_start_timers(next, ipst);
6849 }
6850 
6851 /*
6852  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6853  * and `ioccmd'.
6854  */
6855 void
6856 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6857 {
6858 	ill_t *ill = ipif->ipif_ill;
6859 	ipxop_t *ipx = ipsq->ipsq_xop;
6860 
6861 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6862 	ASSERT(ipx->ipx_current_ipif == NULL);
6863 	ASSERT(ipx->ipx_current_ioctl == 0);
6864 
6865 	ipx->ipx_current_done = B_FALSE;
6866 	ipx->ipx_current_ioctl = ioccmd;
6867 	mutex_enter(&ipx->ipx_lock);
6868 	ipx->ipx_current_ipif = ipif;
6869 	mutex_exit(&ipx->ipx_lock);
6870 
6871 	/*
6872 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6873 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6874 	 * references to the ipif (so that the references will eventually
6875 	 * drop to zero) and also prevents any "get" operations (e.g.,
6876 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6877 	 * operation has completed and the ipif is again in a stable state.
6878 	 *
6879 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6880 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6881 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6882 	 * ipifs will be affected.
6883 	 *
6884 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6885 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6886 	 * operate on.
6887 	 */
6888 	switch (ioccmd) {
6889 	case SIOCLIFREMOVEIF:
6890 		break;
6891 	case 0:
6892 		mutex_enter(&ill->ill_lock);
6893 		ipif = ipif->ipif_ill->ill_ipif;
6894 		for (; ipif != NULL; ipif = ipif->ipif_next)
6895 			ipif->ipif_state_flags |= IPIF_CHANGING;
6896 		mutex_exit(&ill->ill_lock);
6897 		break;
6898 	default:
6899 		mutex_enter(&ill->ill_lock);
6900 		ipif->ipif_state_flags |= IPIF_CHANGING;
6901 		mutex_exit(&ill->ill_lock);
6902 	}
6903 }
6904 
6905 /*
6906  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6907  * the next exclusive operation to begin once we ipsq_exit().  However, if
6908  * pending DLPI operations remain, then we will wait for the queue to drain
6909  * before allowing the next exclusive operation to begin.  This ensures that
6910  * DLPI operations from one exclusive operation are never improperly processed
6911  * as part of a subsequent exclusive operation.
6912  */
6913 void
6914 ipsq_current_finish(ipsq_t *ipsq)
6915 {
6916 	ipxop_t	*ipx = ipsq->ipsq_xop;
6917 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6918 	ipif_t	*ipif = ipx->ipx_current_ipif;
6919 
6920 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6921 
6922 	/*
6923 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6924 	 * (but in that case, IPIF_CHANGING will already be clear and no
6925 	 * pending DLPI messages can remain).
6926 	 */
6927 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6928 		ill_t *ill = ipif->ipif_ill;
6929 
6930 		mutex_enter(&ill->ill_lock);
6931 		dlpi_pending = ill->ill_dlpi_pending;
6932 		if (ipx->ipx_current_ioctl == 0) {
6933 			ipif = ill->ill_ipif;
6934 			for (; ipif != NULL; ipif = ipif->ipif_next)
6935 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6936 		} else {
6937 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6938 		}
6939 		mutex_exit(&ill->ill_lock);
6940 	}
6941 
6942 	ASSERT(!ipx->ipx_current_done);
6943 	ipx->ipx_current_done = B_TRUE;
6944 	ipx->ipx_current_ioctl = 0;
6945 	if (dlpi_pending == DL_PRIM_INVAL) {
6946 		mutex_enter(&ipx->ipx_lock);
6947 		ipx->ipx_current_ipif = NULL;
6948 		mutex_exit(&ipx->ipx_lock);
6949 	}
6950 }
6951 
6952 /*
6953  * The ill is closing. Flush all messages on the ipsq that originated
6954  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6955  * for this ill since ipsq_enter could not have entered until then.
6956  * New messages can't be queued since the CONDEMNED flag is set.
6957  */
6958 static void
6959 ipsq_flush(ill_t *ill)
6960 {
6961 	queue_t	*q;
6962 	mblk_t	*prev;
6963 	mblk_t	*mp;
6964 	mblk_t	*mp_next;
6965 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6966 
6967 	ASSERT(IAM_WRITER_ILL(ill));
6968 
6969 	/*
6970 	 * Flush any messages sent up by the driver.
6971 	 */
6972 	mutex_enter(&ipx->ipx_lock);
6973 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6974 		mp_next = mp->b_next;
6975 		q = mp->b_queue;
6976 		if (q == ill->ill_rq || q == ill->ill_wq) {
6977 			/* dequeue mp */
6978 			if (prev == NULL)
6979 				ipx->ipx_mphead = mp->b_next;
6980 			else
6981 				prev->b_next = mp->b_next;
6982 			if (ipx->ipx_mptail == mp) {
6983 				ASSERT(mp_next == NULL);
6984 				ipx->ipx_mptail = prev;
6985 			}
6986 			inet_freemsg(mp);
6987 		} else {
6988 			prev = mp;
6989 		}
6990 	}
6991 	mutex_exit(&ipx->ipx_lock);
6992 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6993 	ipsq_xopq_mp_cleanup(ill, NULL);
6994 }
6995 
6996 /*
6997  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6998  * and return the associated ipif.
6999  * Return value:
7000  *	Non zero: An error has occurred. ci may not be filled out.
7001  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7002  *	a held ipif in ci.ci_ipif.
7003  */
7004 int
7005 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7006     cmd_info_t *ci)
7007 {
7008 	char		*name;
7009 	struct ifreq    *ifr;
7010 	struct lifreq    *lifr;
7011 	ipif_t		*ipif = NULL;
7012 	ill_t		*ill;
7013 	conn_t		*connp;
7014 	boolean_t	isv6;
7015 	boolean_t	exists;
7016 	mblk_t		*mp1;
7017 	zoneid_t	zoneid;
7018 	ip_stack_t	*ipst;
7019 
7020 	if (q->q_next != NULL) {
7021 		ill = (ill_t *)q->q_ptr;
7022 		isv6 = ill->ill_isv6;
7023 		connp = NULL;
7024 		zoneid = ALL_ZONES;
7025 		ipst = ill->ill_ipst;
7026 	} else {
7027 		ill = NULL;
7028 		connp = Q_TO_CONN(q);
7029 		isv6 = (connp->conn_family == AF_INET6);
7030 		zoneid = connp->conn_zoneid;
7031 		if (zoneid == GLOBAL_ZONEID) {
7032 			/* global zone can access ipifs in all zones */
7033 			zoneid = ALL_ZONES;
7034 		}
7035 		ipst = connp->conn_netstack->netstack_ip;
7036 	}
7037 
7038 	/* Has been checked in ip_wput_nondata */
7039 	mp1 = mp->b_cont->b_cont;
7040 
7041 	if (ipip->ipi_cmd_type == IF_CMD) {
7042 		/* This a old style SIOC[GS]IF* command */
7043 		ifr = (struct ifreq *)mp1->b_rptr;
7044 		/*
7045 		 * Null terminate the string to protect against buffer
7046 		 * overrun. String was generated by user code and may not
7047 		 * be trusted.
7048 		 */
7049 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7050 		name = ifr->ifr_name;
7051 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7052 		ci->ci_sin6 = NULL;
7053 		ci->ci_lifr = (struct lifreq *)ifr;
7054 	} else {
7055 		/* This a new style SIOC[GS]LIF* command */
7056 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7057 		lifr = (struct lifreq *)mp1->b_rptr;
7058 		/*
7059 		 * Null terminate the string to protect against buffer
7060 		 * overrun. String was generated by user code and may not
7061 		 * be trusted.
7062 		 */
7063 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7064 		name = lifr->lifr_name;
7065 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7066 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7067 		ci->ci_lifr = lifr;
7068 	}
7069 
7070 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7071 		/*
7072 		 * The ioctl will be failed if the ioctl comes down
7073 		 * an conn stream
7074 		 */
7075 		if (ill == NULL) {
7076 			/*
7077 			 * Not an ill queue, return EINVAL same as the
7078 			 * old error code.
7079 			 */
7080 			return (ENXIO);
7081 		}
7082 		ipif = ill->ill_ipif;
7083 		ipif_refhold(ipif);
7084 	} else {
7085 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7086 		    &exists, isv6, zoneid, ipst);
7087 
7088 		/*
7089 		 * Ensure that get ioctls don't see any internal state changes
7090 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7091 		 * set.
7092 		 */
7093 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7094 		    !IAM_WRITER_IPIF(ipif)) {
7095 			ipsq_t	*ipsq;
7096 
7097 			if (connp != NULL)
7098 				mutex_enter(&connp->conn_lock);
7099 			mutex_enter(&ipif->ipif_ill->ill_lock);
7100 			if (IPIF_IS_CHANGING(ipif) &&
7101 			    !IPIF_IS_CONDEMNED(ipif)) {
7102 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7103 				mutex_enter(&ipsq->ipsq_lock);
7104 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7105 				mutex_exit(&ipif->ipif_ill->ill_lock);
7106 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7107 				    NEW_OP, ipif->ipif_ill);
7108 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7109 				mutex_exit(&ipsq->ipsq_lock);
7110 				if (connp != NULL)
7111 					mutex_exit(&connp->conn_lock);
7112 				ipif_refrele(ipif);
7113 				return (EINPROGRESS);
7114 			}
7115 			mutex_exit(&ipif->ipif_ill->ill_lock);
7116 			if (connp != NULL)
7117 				mutex_exit(&connp->conn_lock);
7118 		}
7119 	}
7120 
7121 	/*
7122 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7123 	 */
7124 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7125 		ipif_refrele(ipif);
7126 		return (ENXIO);
7127 	}
7128 
7129 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7130 	    name[0] == '\0') {
7131 		/*
7132 		 * Handle a or a SIOC?IF* with a null name
7133 		 * during plumb (on the ill queue before the I_PLINK).
7134 		 */
7135 		ipif = ill->ill_ipif;
7136 		ipif_refhold(ipif);
7137 	}
7138 
7139 	if (ipif == NULL)
7140 		return (ENXIO);
7141 
7142 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7143 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7144 
7145 	ci->ci_ipif = ipif;
7146 	return (0);
7147 }
7148 
7149 /*
7150  * Return the total number of ipifs.
7151  */
7152 static uint_t
7153 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7154 {
7155 	uint_t numifs = 0;
7156 	ill_t	*ill;
7157 	ill_walk_context_t	ctx;
7158 	ipif_t	*ipif;
7159 
7160 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7161 	ill = ILL_START_WALK_V4(&ctx, ipst);
7162 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7163 		if (IS_UNDER_IPMP(ill))
7164 			continue;
7165 		for (ipif = ill->ill_ipif; ipif != NULL;
7166 		    ipif = ipif->ipif_next) {
7167 			if (ipif->ipif_zoneid == zoneid ||
7168 			    ipif->ipif_zoneid == ALL_ZONES)
7169 				numifs++;
7170 		}
7171 	}
7172 	rw_exit(&ipst->ips_ill_g_lock);
7173 	return (numifs);
7174 }
7175 
7176 /*
7177  * Return the total number of ipifs.
7178  */
7179 static uint_t
7180 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7181 {
7182 	uint_t numifs = 0;
7183 	ill_t	*ill;
7184 	ipif_t	*ipif;
7185 	ill_walk_context_t	ctx;
7186 
7187 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7188 
7189 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7190 	if (family == AF_INET)
7191 		ill = ILL_START_WALK_V4(&ctx, ipst);
7192 	else if (family == AF_INET6)
7193 		ill = ILL_START_WALK_V6(&ctx, ipst);
7194 	else
7195 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7196 
7197 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7198 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7199 			continue;
7200 
7201 		for (ipif = ill->ill_ipif; ipif != NULL;
7202 		    ipif = ipif->ipif_next) {
7203 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7204 			    !(lifn_flags & LIFC_NOXMIT))
7205 				continue;
7206 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7207 			    !(lifn_flags & LIFC_TEMPORARY))
7208 				continue;
7209 			if (((ipif->ipif_flags &
7210 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7211 			    IPIF_DEPRECATED)) ||
7212 			    IS_LOOPBACK(ill) ||
7213 			    !(ipif->ipif_flags & IPIF_UP)) &&
7214 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7215 				continue;
7216 
7217 			if (zoneid != ipif->ipif_zoneid &&
7218 			    ipif->ipif_zoneid != ALL_ZONES &&
7219 			    (zoneid != GLOBAL_ZONEID ||
7220 			    !(lifn_flags & LIFC_ALLZONES)))
7221 				continue;
7222 
7223 			numifs++;
7224 		}
7225 	}
7226 	rw_exit(&ipst->ips_ill_g_lock);
7227 	return (numifs);
7228 }
7229 
7230 uint_t
7231 ip_get_lifsrcofnum(ill_t *ill)
7232 {
7233 	uint_t numifs = 0;
7234 	ill_t	*ill_head = ill;
7235 	ip_stack_t	*ipst = ill->ill_ipst;
7236 
7237 	/*
7238 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7239 	 * other thread may be trying to relink the ILLs in this usesrc group
7240 	 * and adjusting the ill_usesrc_grp_next pointers
7241 	 */
7242 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7243 	if ((ill->ill_usesrc_ifindex == 0) &&
7244 	    (ill->ill_usesrc_grp_next != NULL)) {
7245 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7246 		    ill = ill->ill_usesrc_grp_next)
7247 			numifs++;
7248 	}
7249 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7250 
7251 	return (numifs);
7252 }
7253 
7254 /* Null values are passed in for ipif, sin, and ifreq */
7255 /* ARGSUSED */
7256 int
7257 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7258     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7259 {
7260 	int *nump;
7261 	conn_t *connp = Q_TO_CONN(q);
7262 
7263 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7264 
7265 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7266 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7267 
7268 	*nump = ip_get_numifs(connp->conn_zoneid,
7269 	    connp->conn_netstack->netstack_ip);
7270 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7271 	return (0);
7272 }
7273 
7274 /* Null values are passed in for ipif, sin, and ifreq */
7275 /* ARGSUSED */
7276 int
7277 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7278     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7279 {
7280 	struct lifnum *lifn;
7281 	mblk_t	*mp1;
7282 	conn_t *connp = Q_TO_CONN(q);
7283 
7284 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7285 
7286 	/* Existence checked in ip_wput_nondata */
7287 	mp1 = mp->b_cont->b_cont;
7288 
7289 	lifn = (struct lifnum *)mp1->b_rptr;
7290 	switch (lifn->lifn_family) {
7291 	case AF_UNSPEC:
7292 	case AF_INET:
7293 	case AF_INET6:
7294 		break;
7295 	default:
7296 		return (EAFNOSUPPORT);
7297 	}
7298 
7299 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7300 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7301 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7302 	return (0);
7303 }
7304 
7305 /* ARGSUSED */
7306 int
7307 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7308     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7309 {
7310 	STRUCT_HANDLE(ifconf, ifc);
7311 	mblk_t *mp1;
7312 	struct iocblk *iocp;
7313 	struct ifreq *ifr;
7314 	ill_walk_context_t	ctx;
7315 	ill_t	*ill;
7316 	ipif_t	*ipif;
7317 	struct sockaddr_in *sin;
7318 	int32_t	ifclen;
7319 	zoneid_t zoneid;
7320 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7321 
7322 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7323 
7324 	ip1dbg(("ip_sioctl_get_ifconf"));
7325 	/* Existence verified in ip_wput_nondata */
7326 	mp1 = mp->b_cont->b_cont;
7327 	iocp = (struct iocblk *)mp->b_rptr;
7328 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7329 
7330 	/*
7331 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7332 	 * the user buffer address and length into which the list of struct
7333 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7334 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7335 	 * the SIOCGIFCONF operation was redefined to simply provide
7336 	 * a large output buffer into which we are supposed to jam the ifreq
7337 	 * array.  The same ioctl command code was used, despite the fact that
7338 	 * both the applications and the kernel code had to change, thus making
7339 	 * it impossible to support both interfaces.
7340 	 *
7341 	 * For reasons not good enough to try to explain, the following
7342 	 * algorithm is used for deciding what to do with one of these:
7343 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7344 	 * form with the output buffer coming down as the continuation message.
7345 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7346 	 * and we have to copy in the ifconf structure to find out how big the
7347 	 * output buffer is and where to copy out to.  Sure no problem...
7348 	 *
7349 	 */
7350 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7351 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7352 		int numifs = 0;
7353 		size_t ifc_bufsize;
7354 
7355 		/*
7356 		 * Must be (better be!) continuation of a TRANSPARENT
7357 		 * IOCTL.  We just copied in the ifconf structure.
7358 		 */
7359 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7360 		    (struct ifconf *)mp1->b_rptr);
7361 
7362 		/*
7363 		 * Allocate a buffer to hold requested information.
7364 		 *
7365 		 * If ifc_len is larger than what is needed, we only
7366 		 * allocate what we will use.
7367 		 *
7368 		 * If ifc_len is smaller than what is needed, return
7369 		 * EINVAL.
7370 		 *
7371 		 * XXX: the ill_t structure can hava 2 counters, for
7372 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7373 		 * number of interfaces for a device, so we don't need
7374 		 * to count them here...
7375 		 */
7376 		numifs = ip_get_numifs(zoneid, ipst);
7377 
7378 		ifclen = STRUCT_FGET(ifc, ifc_len);
7379 		ifc_bufsize = numifs * sizeof (struct ifreq);
7380 		if (ifc_bufsize > ifclen) {
7381 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7382 				/* old behaviour */
7383 				return (EINVAL);
7384 			} else {
7385 				ifc_bufsize = ifclen;
7386 			}
7387 		}
7388 
7389 		mp1 = mi_copyout_alloc(q, mp,
7390 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7391 		if (mp1 == NULL)
7392 			return (ENOMEM);
7393 
7394 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7395 	}
7396 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7397 	/*
7398 	 * the SIOCGIFCONF ioctl only knows about
7399 	 * IPv4 addresses, so don't try to tell
7400 	 * it about interfaces with IPv6-only
7401 	 * addresses. (Last parm 'isv6' is B_FALSE)
7402 	 */
7403 
7404 	ifr = (struct ifreq *)mp1->b_rptr;
7405 
7406 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7407 	ill = ILL_START_WALK_V4(&ctx, ipst);
7408 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7409 		if (IS_UNDER_IPMP(ill))
7410 			continue;
7411 		for (ipif = ill->ill_ipif; ipif != NULL;
7412 		    ipif = ipif->ipif_next) {
7413 			if (zoneid != ipif->ipif_zoneid &&
7414 			    ipif->ipif_zoneid != ALL_ZONES)
7415 				continue;
7416 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7417 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7418 					/* old behaviour */
7419 					rw_exit(&ipst->ips_ill_g_lock);
7420 					return (EINVAL);
7421 				} else {
7422 					goto if_copydone;
7423 				}
7424 			}
7425 			ipif_get_name(ipif, ifr->ifr_name,
7426 			    sizeof (ifr->ifr_name));
7427 			sin = (sin_t *)&ifr->ifr_addr;
7428 			*sin = sin_null;
7429 			sin->sin_family = AF_INET;
7430 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7431 			ifr++;
7432 		}
7433 	}
7434 if_copydone:
7435 	rw_exit(&ipst->ips_ill_g_lock);
7436 	mp1->b_wptr = (uchar_t *)ifr;
7437 
7438 	if (STRUCT_BUF(ifc) != NULL) {
7439 		STRUCT_FSET(ifc, ifc_len,
7440 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7441 	}
7442 	return (0);
7443 }
7444 
7445 /*
7446  * Get the interfaces using the address hosted on the interface passed in,
7447  * as a source adddress
7448  */
7449 /* ARGSUSED */
7450 int
7451 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7452     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7453 {
7454 	mblk_t *mp1;
7455 	ill_t	*ill, *ill_head;
7456 	ipif_t	*ipif, *orig_ipif;
7457 	int	numlifs = 0;
7458 	size_t	lifs_bufsize, lifsmaxlen;
7459 	struct	lifreq *lifr;
7460 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7461 	uint_t	ifindex;
7462 	zoneid_t zoneid;
7463 	boolean_t isv6 = B_FALSE;
7464 	struct	sockaddr_in	*sin;
7465 	struct	sockaddr_in6	*sin6;
7466 	STRUCT_HANDLE(lifsrcof, lifs);
7467 	ip_stack_t		*ipst;
7468 
7469 	ipst = CONNQ_TO_IPST(q);
7470 
7471 	ASSERT(q->q_next == NULL);
7472 
7473 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7474 
7475 	/* Existence verified in ip_wput_nondata */
7476 	mp1 = mp->b_cont->b_cont;
7477 
7478 	/*
7479 	 * Must be (better be!) continuation of a TRANSPARENT
7480 	 * IOCTL.  We just copied in the lifsrcof structure.
7481 	 */
7482 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7483 	    (struct lifsrcof *)mp1->b_rptr);
7484 
7485 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7486 		return (EINVAL);
7487 
7488 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7489 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7490 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7491 	if (ipif == NULL) {
7492 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7493 		    ifindex));
7494 		return (ENXIO);
7495 	}
7496 
7497 	/* Allocate a buffer to hold requested information */
7498 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7499 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7500 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7501 	/* The actual size needed is always returned in lifs_len */
7502 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7503 
7504 	/* If the amount we need is more than what is passed in, abort */
7505 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7506 		ipif_refrele(ipif);
7507 		return (0);
7508 	}
7509 
7510 	mp1 = mi_copyout_alloc(q, mp,
7511 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7512 	if (mp1 == NULL) {
7513 		ipif_refrele(ipif);
7514 		return (ENOMEM);
7515 	}
7516 
7517 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7518 	bzero(mp1->b_rptr, lifs_bufsize);
7519 
7520 	lifr = (struct lifreq *)mp1->b_rptr;
7521 
7522 	ill = ill_head = ipif->ipif_ill;
7523 	orig_ipif = ipif;
7524 
7525 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7526 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7527 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7528 
7529 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7530 	for (; (ill != NULL) && (ill != ill_head);
7531 	    ill = ill->ill_usesrc_grp_next) {
7532 
7533 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7534 			break;
7535 
7536 		ipif = ill->ill_ipif;
7537 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7538 		if (ipif->ipif_isv6) {
7539 			sin6 = (sin6_t *)&lifr->lifr_addr;
7540 			*sin6 = sin6_null;
7541 			sin6->sin6_family = AF_INET6;
7542 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7543 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7544 			    &ipif->ipif_v6net_mask);
7545 		} else {
7546 			sin = (sin_t *)&lifr->lifr_addr;
7547 			*sin = sin_null;
7548 			sin->sin_family = AF_INET;
7549 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7550 			lifr->lifr_addrlen = ip_mask_to_plen(
7551 			    ipif->ipif_net_mask);
7552 		}
7553 		lifr++;
7554 	}
7555 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7556 	rw_exit(&ipst->ips_ill_g_lock);
7557 	ipif_refrele(orig_ipif);
7558 	mp1->b_wptr = (uchar_t *)lifr;
7559 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7560 
7561 	return (0);
7562 }
7563 
7564 /* ARGSUSED */
7565 int
7566 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7567     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7568 {
7569 	mblk_t *mp1;
7570 	int	list;
7571 	ill_t	*ill;
7572 	ipif_t	*ipif;
7573 	int	flags;
7574 	int	numlifs = 0;
7575 	size_t	lifc_bufsize;
7576 	struct	lifreq *lifr;
7577 	sa_family_t	family;
7578 	struct	sockaddr_in	*sin;
7579 	struct	sockaddr_in6	*sin6;
7580 	ill_walk_context_t	ctx;
7581 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7582 	int32_t	lifclen;
7583 	zoneid_t zoneid;
7584 	STRUCT_HANDLE(lifconf, lifc);
7585 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7586 
7587 	ip1dbg(("ip_sioctl_get_lifconf"));
7588 
7589 	ASSERT(q->q_next == NULL);
7590 
7591 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7592 
7593 	/* Existence verified in ip_wput_nondata */
7594 	mp1 = mp->b_cont->b_cont;
7595 
7596 	/*
7597 	 * An extended version of SIOCGIFCONF that takes an
7598 	 * additional address family and flags field.
7599 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7600 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7601 	 * interfaces are omitted.
7602 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7603 	 * unless LIFC_TEMPORARY is specified.
7604 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7605 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7606 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7607 	 * has priority over LIFC_NOXMIT.
7608 	 */
7609 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7610 
7611 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7612 		return (EINVAL);
7613 
7614 	/*
7615 	 * Must be (better be!) continuation of a TRANSPARENT
7616 	 * IOCTL.  We just copied in the lifconf structure.
7617 	 */
7618 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7619 
7620 	family = STRUCT_FGET(lifc, lifc_family);
7621 	flags = STRUCT_FGET(lifc, lifc_flags);
7622 
7623 	switch (family) {
7624 	case AF_UNSPEC:
7625 		/*
7626 		 * walk all ILL's.
7627 		 */
7628 		list = MAX_G_HEADS;
7629 		break;
7630 	case AF_INET:
7631 		/*
7632 		 * walk only IPV4 ILL's.
7633 		 */
7634 		list = IP_V4_G_HEAD;
7635 		break;
7636 	case AF_INET6:
7637 		/*
7638 		 * walk only IPV6 ILL's.
7639 		 */
7640 		list = IP_V6_G_HEAD;
7641 		break;
7642 	default:
7643 		return (EAFNOSUPPORT);
7644 	}
7645 
7646 	/*
7647 	 * Allocate a buffer to hold requested information.
7648 	 *
7649 	 * If lifc_len is larger than what is needed, we only
7650 	 * allocate what we will use.
7651 	 *
7652 	 * If lifc_len is smaller than what is needed, return
7653 	 * EINVAL.
7654 	 */
7655 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7656 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7657 	lifclen = STRUCT_FGET(lifc, lifc_len);
7658 	if (lifc_bufsize > lifclen) {
7659 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7660 			return (EINVAL);
7661 		else
7662 			lifc_bufsize = lifclen;
7663 	}
7664 
7665 	mp1 = mi_copyout_alloc(q, mp,
7666 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7667 	if (mp1 == NULL)
7668 		return (ENOMEM);
7669 
7670 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7671 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7672 
7673 	lifr = (struct lifreq *)mp1->b_rptr;
7674 
7675 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7676 	ill = ill_first(list, list, &ctx, ipst);
7677 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7678 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7679 			continue;
7680 
7681 		for (ipif = ill->ill_ipif; ipif != NULL;
7682 		    ipif = ipif->ipif_next) {
7683 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7684 			    !(flags & LIFC_NOXMIT))
7685 				continue;
7686 
7687 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7688 			    !(flags & LIFC_TEMPORARY))
7689 				continue;
7690 
7691 			if (((ipif->ipif_flags &
7692 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7693 			    IPIF_DEPRECATED)) ||
7694 			    IS_LOOPBACK(ill) ||
7695 			    !(ipif->ipif_flags & IPIF_UP)) &&
7696 			    (flags & LIFC_EXTERNAL_SOURCE))
7697 				continue;
7698 
7699 			if (zoneid != ipif->ipif_zoneid &&
7700 			    ipif->ipif_zoneid != ALL_ZONES &&
7701 			    (zoneid != GLOBAL_ZONEID ||
7702 			    !(flags & LIFC_ALLZONES)))
7703 				continue;
7704 
7705 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7706 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7707 					rw_exit(&ipst->ips_ill_g_lock);
7708 					return (EINVAL);
7709 				} else {
7710 					goto lif_copydone;
7711 				}
7712 			}
7713 
7714 			ipif_get_name(ipif, lifr->lifr_name,
7715 			    sizeof (lifr->lifr_name));
7716 			lifr->lifr_type = ill->ill_type;
7717 			if (ipif->ipif_isv6) {
7718 				sin6 = (sin6_t *)&lifr->lifr_addr;
7719 				*sin6 = sin6_null;
7720 				sin6->sin6_family = AF_INET6;
7721 				sin6->sin6_addr =
7722 				    ipif->ipif_v6lcl_addr;
7723 				lifr->lifr_addrlen =
7724 				    ip_mask_to_plen_v6(
7725 				    &ipif->ipif_v6net_mask);
7726 			} else {
7727 				sin = (sin_t *)&lifr->lifr_addr;
7728 				*sin = sin_null;
7729 				sin->sin_family = AF_INET;
7730 				sin->sin_addr.s_addr =
7731 				    ipif->ipif_lcl_addr;
7732 				lifr->lifr_addrlen =
7733 				    ip_mask_to_plen(
7734 				    ipif->ipif_net_mask);
7735 			}
7736 			lifr++;
7737 		}
7738 	}
7739 lif_copydone:
7740 	rw_exit(&ipst->ips_ill_g_lock);
7741 
7742 	mp1->b_wptr = (uchar_t *)lifr;
7743 	if (STRUCT_BUF(lifc) != NULL) {
7744 		STRUCT_FSET(lifc, lifc_len,
7745 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7746 	}
7747 	return (0);
7748 }
7749 
7750 static void
7751 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7752 {
7753 	ip6_asp_t *table;
7754 	size_t table_size;
7755 	mblk_t *data_mp;
7756 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7757 	ip_stack_t	*ipst;
7758 
7759 	if (q->q_next == NULL)
7760 		ipst = CONNQ_TO_IPST(q);
7761 	else
7762 		ipst = ILLQ_TO_IPST(q);
7763 
7764 	/* These two ioctls are I_STR only */
7765 	if (iocp->ioc_count == TRANSPARENT) {
7766 		miocnak(q, mp, 0, EINVAL);
7767 		return;
7768 	}
7769 
7770 	data_mp = mp->b_cont;
7771 	if (data_mp == NULL) {
7772 		/* The user passed us a NULL argument */
7773 		table = NULL;
7774 		table_size = iocp->ioc_count;
7775 	} else {
7776 		/*
7777 		 * The user provided a table.  The stream head
7778 		 * may have copied in the user data in chunks,
7779 		 * so make sure everything is pulled up
7780 		 * properly.
7781 		 */
7782 		if (MBLKL(data_mp) < iocp->ioc_count) {
7783 			mblk_t *new_data_mp;
7784 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7785 			    NULL) {
7786 				miocnak(q, mp, 0, ENOMEM);
7787 				return;
7788 			}
7789 			freemsg(data_mp);
7790 			data_mp = new_data_mp;
7791 			mp->b_cont = data_mp;
7792 		}
7793 		table = (ip6_asp_t *)data_mp->b_rptr;
7794 		table_size = iocp->ioc_count;
7795 	}
7796 
7797 	switch (iocp->ioc_cmd) {
7798 	case SIOCGIP6ADDRPOLICY:
7799 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7800 		if (iocp->ioc_rval == -1)
7801 			iocp->ioc_error = EINVAL;
7802 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7803 		else if (table != NULL &&
7804 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7805 			ip6_asp_t *src = table;
7806 			ip6_asp32_t *dst = (void *)table;
7807 			int count = table_size / sizeof (ip6_asp_t);
7808 			int i;
7809 
7810 			/*
7811 			 * We need to do an in-place shrink of the array
7812 			 * to match the alignment attributes of the
7813 			 * 32-bit ABI looking at it.
7814 			 */
7815 			/* LINTED: logical expression always true: op "||" */
7816 			ASSERT(sizeof (*src) > sizeof (*dst));
7817 			for (i = 1; i < count; i++)
7818 				bcopy(src + i, dst + i, sizeof (*dst));
7819 		}
7820 #endif
7821 		break;
7822 
7823 	case SIOCSIP6ADDRPOLICY:
7824 		ASSERT(mp->b_prev == NULL);
7825 		mp->b_prev = (void *)q;
7826 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7827 		/*
7828 		 * We pass in the datamodel here so that the ip6_asp_replace()
7829 		 * routine can handle converting from 32-bit to native formats
7830 		 * where necessary.
7831 		 *
7832 		 * A better way to handle this might be to convert the inbound
7833 		 * data structure here, and hang it off a new 'mp'; thus the
7834 		 * ip6_asp_replace() logic would always be dealing with native
7835 		 * format data structures..
7836 		 *
7837 		 * (An even simpler way to handle these ioctls is to just
7838 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7839 		 * and just recompile everything that depends on it.)
7840 		 */
7841 #endif
7842 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7843 		    iocp->ioc_flag & IOC_MODELS);
7844 		return;
7845 	}
7846 
7847 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7848 	qreply(q, mp);
7849 }
7850 
7851 static void
7852 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7853 {
7854 	mblk_t 		*data_mp;
7855 	struct dstinforeq	*dir;
7856 	uint8_t		*end, *cur;
7857 	in6_addr_t	*daddr, *saddr;
7858 	ipaddr_t	v4daddr;
7859 	ire_t		*ire;
7860 	ipaddr_t	v4setsrc;
7861 	in6_addr_t	v6setsrc;
7862 	char		*slabel, *dlabel;
7863 	boolean_t	isipv4;
7864 	int		match_ire;
7865 	ill_t		*dst_ill;
7866 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7867 	conn_t		*connp = Q_TO_CONN(q);
7868 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7869 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7870 	uint64_t	ipif_flags;
7871 
7872 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7873 
7874 	/*
7875 	 * This ioctl is I_STR only, and must have a
7876 	 * data mblk following the M_IOCTL mblk.
7877 	 */
7878 	data_mp = mp->b_cont;
7879 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7880 		miocnak(q, mp, 0, EINVAL);
7881 		return;
7882 	}
7883 
7884 	if (MBLKL(data_mp) < iocp->ioc_count) {
7885 		mblk_t *new_data_mp;
7886 
7887 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7888 			miocnak(q, mp, 0, ENOMEM);
7889 			return;
7890 		}
7891 		freemsg(data_mp);
7892 		data_mp = new_data_mp;
7893 		mp->b_cont = data_mp;
7894 	}
7895 	match_ire = MATCH_IRE_DSTONLY;
7896 
7897 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7898 	    end - cur >= sizeof (struct dstinforeq);
7899 	    cur += sizeof (struct dstinforeq)) {
7900 		dir = (struct dstinforeq *)cur;
7901 		daddr = &dir->dir_daddr;
7902 		saddr = &dir->dir_saddr;
7903 
7904 		/*
7905 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7906 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7907 		 * and ip_select_source_v6() do not.
7908 		 */
7909 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7910 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7911 
7912 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7913 		if (isipv4) {
7914 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7915 			v4setsrc = INADDR_ANY;
7916 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7917 			    NULL, match_ire, B_TRUE, 0, ipst, &v4setsrc, NULL,
7918 			    NULL);
7919 		} else {
7920 			v6setsrc = ipv6_all_zeros;
7921 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7922 			    NULL, match_ire, B_TRUE, 0, ipst, &v6setsrc, NULL,
7923 			    NULL);
7924 		}
7925 		ASSERT(ire != NULL);
7926 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7927 			ire_refrele(ire);
7928 			dir->dir_dreachable = 0;
7929 
7930 			/* move on to next dst addr */
7931 			continue;
7932 		}
7933 		dir->dir_dreachable = 1;
7934 
7935 		dst_ill = ire_nexthop_ill(ire);
7936 		if (dst_ill == NULL) {
7937 			ire_refrele(ire);
7938 			continue;
7939 		}
7940 
7941 		/* With ipmp we most likely look at the ipmp ill here */
7942 		dir->dir_dmactype = dst_ill->ill_mactype;
7943 
7944 		if (isipv4) {
7945 			ipaddr_t v4saddr;
7946 
7947 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7948 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7949 			    &v4saddr, NULL, &ipif_flags) != 0) {
7950 				v4saddr = INADDR_ANY;
7951 				ipif_flags = 0;
7952 			}
7953 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7954 		} else {
7955 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7956 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7957 			    saddr, NULL, &ipif_flags) != 0) {
7958 				*saddr = ipv6_all_zeros;
7959 				ipif_flags = 0;
7960 			}
7961 		}
7962 
7963 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7964 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7965 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7966 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7967 		ire_refrele(ire);
7968 		ill_refrele(dst_ill);
7969 	}
7970 	miocack(q, mp, iocp->ioc_count, 0);
7971 }
7972 
7973 /*
7974  * Check if this is an address assigned to this machine.
7975  * Skips interfaces that are down by using ire checks.
7976  * Translates mapped addresses to v4 addresses and then
7977  * treats them as such, returning true if the v4 address
7978  * associated with this mapped address is configured.
7979  * Note: Applications will have to be careful what they do
7980  * with the response; use of mapped addresses limits
7981  * what can be done with the socket, especially with
7982  * respect to socket options and ioctls - neither IPv4
7983  * options nor IPv6 sticky options/ancillary data options
7984  * may be used.
7985  */
7986 /* ARGSUSED */
7987 int
7988 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7989     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7990 {
7991 	struct sioc_addrreq *sia;
7992 	sin_t *sin;
7993 	ire_t *ire;
7994 	mblk_t *mp1;
7995 	zoneid_t zoneid;
7996 	ip_stack_t	*ipst;
7997 
7998 	ip1dbg(("ip_sioctl_tmyaddr"));
7999 
8000 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8001 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8002 	ipst = CONNQ_TO_IPST(q);
8003 
8004 	/* Existence verified in ip_wput_nondata */
8005 	mp1 = mp->b_cont->b_cont;
8006 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8007 	sin = (sin_t *)&sia->sa_addr;
8008 	switch (sin->sin_family) {
8009 	case AF_INET6: {
8010 		sin6_t *sin6 = (sin6_t *)sin;
8011 
8012 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8013 			ipaddr_t v4_addr;
8014 
8015 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8016 			    v4_addr);
8017 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8018 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8019 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8020 		} else {
8021 			in6_addr_t v6addr;
8022 
8023 			v6addr = sin6->sin6_addr;
8024 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8025 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8026 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8027 		}
8028 		break;
8029 	}
8030 	case AF_INET: {
8031 		ipaddr_t v4addr;
8032 
8033 		v4addr = sin->sin_addr.s_addr;
8034 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8035 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8036 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8037 		break;
8038 	}
8039 	default:
8040 		return (EAFNOSUPPORT);
8041 	}
8042 	if (ire != NULL) {
8043 		sia->sa_res = 1;
8044 		ire_refrele(ire);
8045 	} else {
8046 		sia->sa_res = 0;
8047 	}
8048 	return (0);
8049 }
8050 
8051 /*
8052  * Check if this is an address assigned on-link i.e. neighbor,
8053  * and makes sure it's reachable from the current zone.
8054  * Returns true for my addresses as well.
8055  * Translates mapped addresses to v4 addresses and then
8056  * treats them as such, returning true if the v4 address
8057  * associated with this mapped address is configured.
8058  * Note: Applications will have to be careful what they do
8059  * with the response; use of mapped addresses limits
8060  * what can be done with the socket, especially with
8061  * respect to socket options and ioctls - neither IPv4
8062  * options nor IPv6 sticky options/ancillary data options
8063  * may be used.
8064  */
8065 /* ARGSUSED */
8066 int
8067 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8068     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8069 {
8070 	struct sioc_addrreq *sia;
8071 	sin_t *sin;
8072 	mblk_t	*mp1;
8073 	ire_t *ire = NULL;
8074 	zoneid_t zoneid;
8075 	ip_stack_t	*ipst;
8076 
8077 	ip1dbg(("ip_sioctl_tonlink"));
8078 
8079 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8080 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8081 	ipst = CONNQ_TO_IPST(q);
8082 
8083 	/* Existence verified in ip_wput_nondata */
8084 	mp1 = mp->b_cont->b_cont;
8085 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8086 	sin = (sin_t *)&sia->sa_addr;
8087 
8088 	/*
8089 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8090 	 * to make sure we only look at on-link unicast address.
8091 	 */
8092 	switch (sin->sin_family) {
8093 	case AF_INET6: {
8094 		sin6_t *sin6 = (sin6_t *)sin;
8095 
8096 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8097 			ipaddr_t v4_addr;
8098 
8099 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8100 			    v4_addr);
8101 			if (!CLASSD(v4_addr)) {
8102 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8103 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8104 				    0, ipst, NULL);
8105 			}
8106 		} else {
8107 			in6_addr_t v6addr;
8108 
8109 			v6addr = sin6->sin6_addr;
8110 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8111 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8112 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8113 				    ipst, NULL);
8114 			}
8115 		}
8116 		break;
8117 	}
8118 	case AF_INET: {
8119 		ipaddr_t v4addr;
8120 
8121 		v4addr = sin->sin_addr.s_addr;
8122 		if (!CLASSD(v4addr)) {
8123 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8124 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8125 		}
8126 		break;
8127 	}
8128 	default:
8129 		return (EAFNOSUPPORT);
8130 	}
8131 	sia->sa_res = 0;
8132 	if (ire != NULL) {
8133 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8134 
8135 		if ((ire->ire_type & IRE_ONLINK) &&
8136 		    !(ire->ire_type & IRE_BROADCAST))
8137 			sia->sa_res = 1;
8138 		ire_refrele(ire);
8139 	}
8140 	return (0);
8141 }
8142 
8143 /*
8144  * TBD: implement when kernel maintaines a list of site prefixes.
8145  */
8146 /* ARGSUSED */
8147 int
8148 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8149     ip_ioctl_cmd_t *ipip, void *ifreq)
8150 {
8151 	return (ENXIO);
8152 }
8153 
8154 /* ARP IOCTLs. */
8155 /* ARGSUSED */
8156 int
8157 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8158     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8159 {
8160 	int		err;
8161 	ipaddr_t	ipaddr;
8162 	struct iocblk	*iocp;
8163 	conn_t		*connp;
8164 	struct arpreq	*ar;
8165 	struct xarpreq	*xar;
8166 	int		arp_flags, flags, alength;
8167 	uchar_t		*lladdr;
8168 	ip_stack_t	*ipst;
8169 	ill_t		*ill = ipif->ipif_ill;
8170 	ill_t		*proxy_ill = NULL;
8171 	ipmp_arpent_t	*entp = NULL;
8172 	boolean_t	proxyarp = B_FALSE;
8173 	boolean_t	if_arp_ioctl = B_FALSE;
8174 	ncec_t		*ncec = NULL;
8175 	nce_t		*nce;
8176 
8177 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8178 	connp = Q_TO_CONN(q);
8179 	ipst = connp->conn_netstack->netstack_ip;
8180 	iocp = (struct iocblk *)mp->b_rptr;
8181 
8182 	if (ipip->ipi_cmd_type == XARP_CMD) {
8183 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8184 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8185 		ar = NULL;
8186 
8187 		arp_flags = xar->xarp_flags;
8188 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8189 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8190 		/*
8191 		 * Validate against user's link layer address length
8192 		 * input and name and addr length limits.
8193 		 */
8194 		alength = ill->ill_phys_addr_length;
8195 		if (ipip->ipi_cmd == SIOCSXARP) {
8196 			if (alength != xar->xarp_ha.sdl_alen ||
8197 			    (alength + xar->xarp_ha.sdl_nlen >
8198 			    sizeof (xar->xarp_ha.sdl_data)))
8199 				return (EINVAL);
8200 		}
8201 	} else {
8202 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8203 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8204 		xar = NULL;
8205 
8206 		arp_flags = ar->arp_flags;
8207 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8208 		/*
8209 		 * Theoretically, the sa_family could tell us what link
8210 		 * layer type this operation is trying to deal with. By
8211 		 * common usage AF_UNSPEC means ethernet. We'll assume
8212 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8213 		 * for now. Our new SIOC*XARP ioctls can be used more
8214 		 * generally.
8215 		 *
8216 		 * If the underlying media happens to have a non 6 byte
8217 		 * address, arp module will fail set/get, but the del
8218 		 * operation will succeed.
8219 		 */
8220 		alength = 6;
8221 		if ((ipip->ipi_cmd != SIOCDARP) &&
8222 		    (alength != ill->ill_phys_addr_length)) {
8223 			return (EINVAL);
8224 		}
8225 	}
8226 
8227 	/* Translate ATF* flags to NCE* flags */
8228 	flags = 0;
8229 	if (arp_flags & ATF_AUTHORITY)
8230 		flags |= NCE_F_AUTHORITY;
8231 	if (arp_flags & ATF_PERM)
8232 		flags |= NCE_F_NONUD; /* not subject to aging */
8233 	if (arp_flags & ATF_PUBL)
8234 		flags |= NCE_F_PUBLISH;
8235 
8236 	/*
8237 	 * IPMP ARP special handling:
8238 	 *
8239 	 * 1. Since ARP mappings must appear consistent across the group,
8240 	 *    prohibit changing ARP mappings on the underlying interfaces.
8241 	 *
8242 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8243 	 *    IP itself, prohibit changing them.
8244 	 *
8245 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8246 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8247 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8248 	 */
8249 	if (IS_UNDER_IPMP(ill)) {
8250 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8251 			return (EPERM);
8252 	}
8253 	if (IS_IPMP(ill)) {
8254 		ipmp_illgrp_t *illg = ill->ill_grp;
8255 
8256 		switch (ipip->ipi_cmd) {
8257 		case SIOCSARP:
8258 		case SIOCSXARP:
8259 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8260 			if (proxy_ill != NULL) {
8261 				proxyarp = B_TRUE;
8262 				if (!ipmp_ill_is_active(proxy_ill))
8263 					proxy_ill = ipmp_illgrp_next_ill(illg);
8264 				if (proxy_ill != NULL)
8265 					lladdr = proxy_ill->ill_phys_addr;
8266 			}
8267 			/* FALLTHRU */
8268 		}
8269 	}
8270 
8271 	ipaddr = sin->sin_addr.s_addr;
8272 	/*
8273 	 * don't match across illgrp per case (1) and (2).
8274 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8275 	 */
8276 	nce = nce_lookup_v4(ill, &ipaddr);
8277 	if (nce != NULL)
8278 		ncec = nce->nce_common;
8279 
8280 	switch (iocp->ioc_cmd) {
8281 	case SIOCDARP:
8282 	case SIOCDXARP: {
8283 		/*
8284 		 * Delete the NCE if any.
8285 		 */
8286 		if (ncec == NULL) {
8287 			iocp->ioc_error = ENXIO;
8288 			break;
8289 		}
8290 		/* Don't allow changes to arp mappings of local addresses. */
8291 		if (NCE_MYADDR(ncec)) {
8292 			nce_refrele(nce);
8293 			return (ENOTSUP);
8294 		}
8295 		iocp->ioc_error = 0;
8296 
8297 		/*
8298 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8299 		 * This will delete all the nce entries on the under_ills.
8300 		 */
8301 		ncec_delete(ncec);
8302 		/*
8303 		 * Once the NCE has been deleted, then the ire_dep* consistency
8304 		 * mechanism will find any IRE which depended on the now
8305 		 * condemned NCE (as part of sending packets).
8306 		 * That mechanism handles redirects by deleting redirects
8307 		 * that refer to UNREACHABLE nces.
8308 		 */
8309 		break;
8310 	}
8311 	case SIOCGARP:
8312 	case SIOCGXARP:
8313 		if (ncec != NULL) {
8314 			lladdr = ncec->ncec_lladdr;
8315 			flags = ncec->ncec_flags;
8316 			iocp->ioc_error = 0;
8317 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8318 		} else {
8319 			iocp->ioc_error = ENXIO;
8320 		}
8321 		break;
8322 	case SIOCSARP:
8323 	case SIOCSXARP:
8324 		/* Don't allow changes to arp mappings of local addresses. */
8325 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8326 			nce_refrele(nce);
8327 			return (ENOTSUP);
8328 		}
8329 
8330 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8331 		flags |= NCE_F_STATIC;
8332 		if (!if_arp_ioctl) {
8333 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8334 			    lladdr, alength, flags);
8335 		} else {
8336 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8337 			if (ipif != NULL) {
8338 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8339 				    lladdr, alength, flags);
8340 				ipif_refrele(ipif);
8341 			}
8342 		}
8343 		if (nce != NULL) {
8344 			nce_refrele(nce);
8345 			nce = NULL;
8346 		}
8347 		/*
8348 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8349 		 * by nce_add_common()
8350 		 */
8351 		err = nce_lookup_then_add_v4(ill, lladdr,
8352 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8353 		    &nce);
8354 		if (err == EEXIST) {
8355 			ncec = nce->nce_common;
8356 			mutex_enter(&ncec->ncec_lock);
8357 			ncec->ncec_state = ND_REACHABLE;
8358 			ncec->ncec_flags = flags;
8359 			nce_update(ncec, ND_UNCHANGED, lladdr);
8360 			mutex_exit(&ncec->ncec_lock);
8361 			err = 0;
8362 		}
8363 		if (nce != NULL) {
8364 			nce_refrele(nce);
8365 			nce = NULL;
8366 		}
8367 		if (IS_IPMP(ill) && err == 0) {
8368 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8369 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8370 			    flags);
8371 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8372 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8373 				break;
8374 			}
8375 		}
8376 		iocp->ioc_error = err;
8377 	}
8378 
8379 	if (nce != NULL) {
8380 		nce_refrele(nce);
8381 	}
8382 
8383 	/*
8384 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8385 	 */
8386 	if (entp != NULL)
8387 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8388 
8389 	return (iocp->ioc_error);
8390 }
8391 
8392 /*
8393  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8394  * the associated sin and refhold and return the associated ipif via `ci'.
8395  */
8396 int
8397 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8398     cmd_info_t *ci)
8399 {
8400 	mblk_t	*mp1;
8401 	sin_t	*sin;
8402 	conn_t	*connp;
8403 	ipif_t	*ipif;
8404 	ire_t	*ire = NULL;
8405 	ill_t	*ill = NULL;
8406 	boolean_t exists;
8407 	ip_stack_t *ipst;
8408 	struct arpreq *ar;
8409 	struct xarpreq *xar;
8410 	struct sockaddr_dl *sdl;
8411 
8412 	/* ioctl comes down on a conn */
8413 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8414 	connp = Q_TO_CONN(q);
8415 	if (connp->conn_family == AF_INET6)
8416 		return (ENXIO);
8417 
8418 	ipst = connp->conn_netstack->netstack_ip;
8419 
8420 	/* Verified in ip_wput_nondata */
8421 	mp1 = mp->b_cont->b_cont;
8422 
8423 	if (ipip->ipi_cmd_type == XARP_CMD) {
8424 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8425 		xar = (struct xarpreq *)mp1->b_rptr;
8426 		sin = (sin_t *)&xar->xarp_pa;
8427 		sdl = &xar->xarp_ha;
8428 
8429 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8430 			return (ENXIO);
8431 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8432 			return (EINVAL);
8433 	} else {
8434 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8435 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8436 		ar = (struct arpreq *)mp1->b_rptr;
8437 		sin = (sin_t *)&ar->arp_pa;
8438 	}
8439 
8440 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8441 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8442 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8443 		if (ipif == NULL)
8444 			return (ENXIO);
8445 		if (ipif->ipif_id != 0) {
8446 			ipif_refrele(ipif);
8447 			return (ENXIO);
8448 		}
8449 	} else {
8450 		/*
8451 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8452 		 * of 0: use the IP address to find the ipif.  If the IP
8453 		 * address is an IPMP test address, ire_ftable_lookup() will
8454 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8455 		 */
8456 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8457 		    ipst);
8458 		if (ipif == NULL) {
8459 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8460 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8461 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8462 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8463 				if (ire != NULL)
8464 					ire_refrele(ire);
8465 				return (ENXIO);
8466 			}
8467 			ASSERT(ire != NULL && ill != NULL);
8468 			ipif = ill->ill_ipif;
8469 			ipif_refhold(ipif);
8470 			ire_refrele(ire);
8471 		}
8472 	}
8473 
8474 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8475 		ipif_refrele(ipif);
8476 		return (ENXIO);
8477 	}
8478 
8479 	ci->ci_sin = sin;
8480 	ci->ci_ipif = ipif;
8481 	return (0);
8482 }
8483 
8484 /*
8485  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8486  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8487  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8488  * up and thus an ill can join that illgrp.
8489  *
8490  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8491  * open()/close() primarily because close() is not allowed to fail or block
8492  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8493  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8494  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8495  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8496  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8497  * state if I_UNLINK didn't occur.
8498  *
8499  * Note that for each plumb/unplumb operation, we may end up here more than
8500  * once because of the way ifconfig works.  However, it's OK to link the same
8501  * illgrp more than once, or unlink an illgrp that's already unlinked.
8502  */
8503 static int
8504 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8505 {
8506 	int err;
8507 	ip_stack_t *ipst = ill->ill_ipst;
8508 
8509 	ASSERT(IS_IPMP(ill));
8510 	ASSERT(IAM_WRITER_ILL(ill));
8511 
8512 	switch (ioccmd) {
8513 	case I_LINK:
8514 		return (ENOTSUP);
8515 
8516 	case I_PLINK:
8517 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8518 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8519 		rw_exit(&ipst->ips_ipmp_lock);
8520 		break;
8521 
8522 	case I_PUNLINK:
8523 		/*
8524 		 * Require all UP ipifs be brought down prior to unlinking the
8525 		 * illgrp so any associated IREs (and other state) is torched.
8526 		 */
8527 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8528 			return (EBUSY);
8529 
8530 		/*
8531 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8532 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8533 		 * join this group.  Specifically: ills trying to join grab
8534 		 * ipmp_lock and bump a "pending join" counter checked by
8535 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8536 		 * joins can occur (since we have ipmp_lock).  Once we drop
8537 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8538 		 * find the illgrp (since we unlinked it) and will return
8539 		 * EAFNOSUPPORT.  This will then take them back through the
8540 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8541 		 * back through I_PLINK above.
8542 		 */
8543 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8544 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8545 		rw_exit(&ipst->ips_ipmp_lock);
8546 		return (err);
8547 	default:
8548 		break;
8549 	}
8550 	return (0);
8551 }
8552 
8553 /*
8554  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8555  * atomically set/clear the muxids. Also complete the ioctl by acking or
8556  * naking it.  Note that the code is structured such that the link type,
8557  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8558  * its clones use the persistent link, while pppd(1M) and perhaps many
8559  * other daemons may use non-persistent link.  When combined with some
8560  * ill_t states, linking and unlinking lower streams may be used as
8561  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8562  */
8563 /* ARGSUSED */
8564 void
8565 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8566 {
8567 	mblk_t		*mp1;
8568 	struct linkblk	*li;
8569 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8570 	int		err = 0;
8571 
8572 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8573 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8574 
8575 	mp1 = mp->b_cont;	/* This is the linkblk info */
8576 	li = (struct linkblk *)mp1->b_rptr;
8577 
8578 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8579 	if (err == EINPROGRESS)
8580 		return;
8581 done:
8582 	if (err == 0)
8583 		miocack(q, mp, 0, 0);
8584 	else
8585 		miocnak(q, mp, 0, err);
8586 
8587 	/* Conn was refheld in ip_sioctl_copyin_setup */
8588 	if (CONN_Q(q))
8589 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8590 }
8591 
8592 /*
8593  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8594  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8595  * module stream).  If `doconsist' is set, then do the extended consistency
8596  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8597  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8598  * an error code on failure.
8599  */
8600 static int
8601 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8602     struct linkblk *li)
8603 {
8604 	int		err = 0;
8605 	ill_t  		*ill;
8606 	queue_t		*ipwq, *dwq;
8607 	const char	*name;
8608 	struct qinit	*qinfo;
8609 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8610 	boolean_t	entered_ipsq = B_FALSE;
8611 	boolean_t	is_ip = B_FALSE;
8612 	arl_t		*arl;
8613 
8614 	/*
8615 	 * Walk the lower stream to verify it's the IP module stream.
8616 	 * The IP module is identified by its name, wput function,
8617 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8618 	 * (li->l_qbot) will not vanish until this ioctl completes.
8619 	 */
8620 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8621 		qinfo = ipwq->q_qinfo;
8622 		name = qinfo->qi_minfo->mi_idname;
8623 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8624 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8625 			is_ip = B_TRUE;
8626 			break;
8627 		}
8628 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8629 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8630 			break;
8631 		}
8632 	}
8633 
8634 	/*
8635 	 * If this isn't an IP module stream, bail.
8636 	 */
8637 	if (ipwq == NULL)
8638 		return (0);
8639 
8640 	if (!is_ip) {
8641 		arl = (arl_t *)ipwq->q_ptr;
8642 		ill = arl_to_ill(arl);
8643 		if (ill == NULL)
8644 			return (0);
8645 	} else {
8646 		ill = ipwq->q_ptr;
8647 	}
8648 	ASSERT(ill != NULL);
8649 
8650 	if (ipsq == NULL) {
8651 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8652 		    NEW_OP, B_FALSE);
8653 		if (ipsq == NULL) {
8654 			if (!is_ip)
8655 				ill_refrele(ill);
8656 			return (EINPROGRESS);
8657 		}
8658 		entered_ipsq = B_TRUE;
8659 	}
8660 	ASSERT(IAM_WRITER_ILL(ill));
8661 	mutex_enter(&ill->ill_lock);
8662 	if (!is_ip) {
8663 		if (islink && ill->ill_muxid == 0) {
8664 			/*
8665 			 * Plumbing has to be done with IP plumbed first, arp
8666 			 * second, but here we have arp being plumbed first.
8667 			 */
8668 			mutex_exit(&ill->ill_lock);
8669 			ipsq_exit(ipsq);
8670 			ill_refrele(ill);
8671 			return (EINVAL);
8672 		}
8673 	}
8674 	mutex_exit(&ill->ill_lock);
8675 	if (!is_ip) {
8676 		arl->arl_muxid = islink ? li->l_index : 0;
8677 		ill_refrele(ill);
8678 		goto done;
8679 	}
8680 
8681 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8682 		goto done;
8683 
8684 	/*
8685 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8686 	 * the read queue of the module immediately below IP in the ill.
8687 	 * These are used during the capability negotiation below.
8688 	 */
8689 	ill->ill_lmod_rq = NULL;
8690 	ill->ill_lmod_cnt = 0;
8691 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8692 		ill->ill_lmod_rq = RD(dwq);
8693 		for (; dwq != NULL; dwq = dwq->q_next)
8694 			ill->ill_lmod_cnt++;
8695 	}
8696 
8697 	ill->ill_muxid = islink ? li->l_index : 0;
8698 
8699 	/*
8700 	 * Mark the ipsq busy until the capability operations initiated below
8701 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8702 	 * returns, but the capability operation may complete asynchronously
8703 	 * much later.
8704 	 */
8705 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8706 	/*
8707 	 * If there's at least one up ipif on this ill, then we're bound to
8708 	 * the underlying driver via DLPI.  In that case, renegotiate
8709 	 * capabilities to account for any possible change in modules
8710 	 * interposed between IP and the driver.
8711 	 */
8712 	if (ill->ill_ipif_up_count > 0) {
8713 		if (islink)
8714 			ill_capability_probe(ill);
8715 		else
8716 			ill_capability_reset(ill, B_FALSE);
8717 	}
8718 	ipsq_current_finish(ipsq);
8719 done:
8720 	if (entered_ipsq)
8721 		ipsq_exit(ipsq);
8722 
8723 	return (err);
8724 }
8725 
8726 /*
8727  * Search the ioctl command in the ioctl tables and return a pointer
8728  * to the ioctl command information. The ioctl command tables are
8729  * static and fully populated at compile time.
8730  */
8731 ip_ioctl_cmd_t *
8732 ip_sioctl_lookup(int ioc_cmd)
8733 {
8734 	int index;
8735 	ip_ioctl_cmd_t *ipip;
8736 	ip_ioctl_cmd_t *ipip_end;
8737 
8738 	if (ioc_cmd == IPI_DONTCARE)
8739 		return (NULL);
8740 
8741 	/*
8742 	 * Do a 2 step search. First search the indexed table
8743 	 * based on the least significant byte of the ioctl cmd.
8744 	 * If we don't find a match, then search the misc table
8745 	 * serially.
8746 	 */
8747 	index = ioc_cmd & 0xFF;
8748 	if (index < ip_ndx_ioctl_count) {
8749 		ipip = &ip_ndx_ioctl_table[index];
8750 		if (ipip->ipi_cmd == ioc_cmd) {
8751 			/* Found a match in the ndx table */
8752 			return (ipip);
8753 		}
8754 	}
8755 
8756 	/* Search the misc table */
8757 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8758 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8759 		if (ipip->ipi_cmd == ioc_cmd)
8760 			/* Found a match in the misc table */
8761 			return (ipip);
8762 	}
8763 
8764 	return (NULL);
8765 }
8766 
8767 /*
8768  * Wrapper function for resuming deferred ioctl processing
8769  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8770  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8771  */
8772 /* ARGSUSED */
8773 void
8774 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8775     void *dummy_arg)
8776 {
8777 	ip_sioctl_copyin_setup(q, mp);
8778 }
8779 
8780 /*
8781  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8782  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8783  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8784  * We establish here the size of the block to be copied in.  mi_copyin
8785  * arranges for this to happen, an processing continues in ip_wput_nondata with
8786  * an M_IOCDATA message.
8787  */
8788 void
8789 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8790 {
8791 	int	copyin_size;
8792 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8793 	ip_ioctl_cmd_t *ipip;
8794 	cred_t *cr;
8795 	ip_stack_t	*ipst;
8796 
8797 	if (CONN_Q(q))
8798 		ipst = CONNQ_TO_IPST(q);
8799 	else
8800 		ipst = ILLQ_TO_IPST(q);
8801 
8802 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8803 	if (ipip == NULL) {
8804 		/*
8805 		 * The ioctl is not one we understand or own.
8806 		 * Pass it along to be processed down stream,
8807 		 * if this is a module instance of IP, else nak
8808 		 * the ioctl.
8809 		 */
8810 		if (q->q_next == NULL) {
8811 			goto nak;
8812 		} else {
8813 			putnext(q, mp);
8814 			return;
8815 		}
8816 	}
8817 
8818 	/*
8819 	 * If this is deferred, then we will do all the checks when we
8820 	 * come back.
8821 	 */
8822 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8823 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8824 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8825 		return;
8826 	}
8827 
8828 	/*
8829 	 * Only allow a very small subset of IP ioctls on this stream if
8830 	 * IP is a module and not a driver. Allowing ioctls to be processed
8831 	 * in this case may cause assert failures or data corruption.
8832 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8833 	 * ioctls allowed on an IP module stream, after which this stream
8834 	 * normally becomes a multiplexor (at which time the stream head
8835 	 * will fail all ioctls).
8836 	 */
8837 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8838 		goto nak;
8839 	}
8840 
8841 	/* Make sure we have ioctl data to process. */
8842 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8843 		goto nak;
8844 
8845 	/*
8846 	 * Prefer dblk credential over ioctl credential; some synthesized
8847 	 * ioctls have kcred set because there's no way to crhold()
8848 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8849 	 * the framework; the caller of ioctl needs to hold the reference
8850 	 * for the duration of the call).
8851 	 */
8852 	cr = msg_getcred(mp, NULL);
8853 	if (cr == NULL)
8854 		cr = iocp->ioc_cr;
8855 
8856 	/* Make sure normal users don't send down privileged ioctls */
8857 	if ((ipip->ipi_flags & IPI_PRIV) &&
8858 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8859 		/* We checked the privilege earlier but log it here */
8860 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8861 		return;
8862 	}
8863 
8864 	/*
8865 	 * The ioctl command tables can only encode fixed length
8866 	 * ioctl data. If the length is variable, the table will
8867 	 * encode the length as zero. Such special cases are handled
8868 	 * below in the switch.
8869 	 */
8870 	if (ipip->ipi_copyin_size != 0) {
8871 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8872 		return;
8873 	}
8874 
8875 	switch (iocp->ioc_cmd) {
8876 	case O_SIOCGIFCONF:
8877 	case SIOCGIFCONF:
8878 		/*
8879 		 * This IOCTL is hilarious.  See comments in
8880 		 * ip_sioctl_get_ifconf for the story.
8881 		 */
8882 		if (iocp->ioc_count == TRANSPARENT)
8883 			copyin_size = SIZEOF_STRUCT(ifconf,
8884 			    iocp->ioc_flag);
8885 		else
8886 			copyin_size = iocp->ioc_count;
8887 		mi_copyin(q, mp, NULL, copyin_size);
8888 		return;
8889 
8890 	case O_SIOCGLIFCONF:
8891 	case SIOCGLIFCONF:
8892 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8893 		mi_copyin(q, mp, NULL, copyin_size);
8894 		return;
8895 
8896 	case SIOCGLIFSRCOF:
8897 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8898 		mi_copyin(q, mp, NULL, copyin_size);
8899 		return;
8900 	case SIOCGIP6ADDRPOLICY:
8901 		ip_sioctl_ip6addrpolicy(q, mp);
8902 		ip6_asp_table_refrele(ipst);
8903 		return;
8904 
8905 	case SIOCSIP6ADDRPOLICY:
8906 		ip_sioctl_ip6addrpolicy(q, mp);
8907 		return;
8908 
8909 	case SIOCGDSTINFO:
8910 		ip_sioctl_dstinfo(q, mp);
8911 		ip6_asp_table_refrele(ipst);
8912 		return;
8913 
8914 	case I_PLINK:
8915 	case I_PUNLINK:
8916 	case I_LINK:
8917 	case I_UNLINK:
8918 		/*
8919 		 * We treat non-persistent link similarly as the persistent
8920 		 * link case, in terms of plumbing/unplumbing, as well as
8921 		 * dynamic re-plumbing events indicator.  See comments
8922 		 * in ip_sioctl_plink() for more.
8923 		 *
8924 		 * Request can be enqueued in the 'ipsq' while waiting
8925 		 * to become exclusive. So bump up the conn ref.
8926 		 */
8927 		if (CONN_Q(q))
8928 			CONN_INC_REF(Q_TO_CONN(q));
8929 		ip_sioctl_plink(NULL, q, mp, NULL);
8930 		return;
8931 
8932 	case ND_GET:
8933 	case ND_SET:
8934 		/*
8935 		 * Use of the nd table requires holding the reader lock.
8936 		 * Modifying the nd table thru nd_load/nd_unload requires
8937 		 * the writer lock.
8938 		 */
8939 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8940 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8941 			rw_exit(&ipst->ips_ip_g_nd_lock);
8942 
8943 			if (iocp->ioc_error)
8944 				iocp->ioc_count = 0;
8945 			mp->b_datap->db_type = M_IOCACK;
8946 			qreply(q, mp);
8947 			return;
8948 		}
8949 		rw_exit(&ipst->ips_ip_g_nd_lock);
8950 		/*
8951 		 * We don't understand this subioctl of ND_GET / ND_SET.
8952 		 * Maybe intended for some driver / module below us
8953 		 */
8954 		if (q->q_next) {
8955 			putnext(q, mp);
8956 		} else {
8957 			iocp->ioc_error = ENOENT;
8958 			mp->b_datap->db_type = M_IOCNAK;
8959 			iocp->ioc_count = 0;
8960 			qreply(q, mp);
8961 		}
8962 		return;
8963 
8964 	case IP_IOCTL:
8965 		ip_wput_ioctl(q, mp);
8966 		return;
8967 
8968 	case SIOCILB:
8969 		/* The ioctl length varies depending on the ILB command. */
8970 		copyin_size = iocp->ioc_count;
8971 		if (copyin_size < sizeof (ilb_cmd_t))
8972 			goto nak;
8973 		mi_copyin(q, mp, NULL, copyin_size);
8974 		return;
8975 
8976 	default:
8977 		cmn_err(CE_PANIC, "should not happen ");
8978 	}
8979 nak:
8980 	if (mp->b_cont != NULL) {
8981 		freemsg(mp->b_cont);
8982 		mp->b_cont = NULL;
8983 	}
8984 	iocp->ioc_error = EINVAL;
8985 	mp->b_datap->db_type = M_IOCNAK;
8986 	iocp->ioc_count = 0;
8987 	qreply(q, mp);
8988 }
8989 
8990 static void
8991 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
8992 {
8993 	struct arpreq *ar;
8994 	struct xarpreq *xar;
8995 	mblk_t	*tmp;
8996 	struct iocblk *iocp;
8997 	int x_arp_ioctl = B_FALSE;
8998 	int *flagsp;
8999 	char *storage = NULL;
9000 
9001 	ASSERT(ill != NULL);
9002 
9003 	iocp = (struct iocblk *)mp->b_rptr;
9004 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9005 
9006 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9007 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9008 	    (iocp->ioc_cmd == SIOCSXARP)) {
9009 		x_arp_ioctl = B_TRUE;
9010 		xar = (struct xarpreq *)tmp->b_rptr;
9011 		flagsp = &xar->xarp_flags;
9012 		storage = xar->xarp_ha.sdl_data;
9013 	} else {
9014 		ar = (struct arpreq *)tmp->b_rptr;
9015 		flagsp = &ar->arp_flags;
9016 		storage = ar->arp_ha.sa_data;
9017 	}
9018 
9019 	/*
9020 	 * We're done if this is not an SIOCG{X}ARP
9021 	 */
9022 	if (x_arp_ioctl) {
9023 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9024 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9025 		    sizeof (xar->xarp_ha.sdl_data)) {
9026 			iocp->ioc_error = EINVAL;
9027 			return;
9028 		}
9029 	}
9030 	*flagsp = ATF_INUSE;
9031 	/*
9032 	 * If /sbin/arp told us we are the authority using the "permanent"
9033 	 * flag, or if this is one of my addresses print "permanent"
9034 	 * in the /sbin/arp output.
9035 	 */
9036 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9037 		*flagsp |= ATF_AUTHORITY;
9038 	if (flags & NCE_F_NONUD)
9039 		*flagsp |= ATF_PERM; /* not subject to aging */
9040 	if (flags & NCE_F_PUBLISH)
9041 		*flagsp |= ATF_PUBL;
9042 	if (hwaddr != NULL) {
9043 		*flagsp |= ATF_COM;
9044 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9045 	}
9046 }
9047 
9048 /*
9049  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9050  * interface) create the next available logical interface for this
9051  * physical interface.
9052  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9053  * ipif with the specified name.
9054  *
9055  * If the address family is not AF_UNSPEC then set the address as well.
9056  *
9057  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9058  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9059  *
9060  * Executed as a writer on the ill.
9061  * So no lock is needed to traverse the ipif chain, or examine the
9062  * phyint flags.
9063  */
9064 /* ARGSUSED */
9065 int
9066 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9067     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9068 {
9069 	mblk_t	*mp1;
9070 	struct lifreq *lifr;
9071 	boolean_t	isv6;
9072 	boolean_t	exists;
9073 	char 	*name;
9074 	char	*endp;
9075 	char	*cp;
9076 	int	namelen;
9077 	ipif_t	*ipif;
9078 	long	id;
9079 	ipsq_t	*ipsq;
9080 	ill_t	*ill;
9081 	sin_t	*sin;
9082 	int	err = 0;
9083 	boolean_t found_sep = B_FALSE;
9084 	conn_t	*connp;
9085 	zoneid_t zoneid;
9086 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9087 
9088 	ASSERT(q->q_next == NULL);
9089 	ip1dbg(("ip_sioctl_addif\n"));
9090 	/* Existence of mp1 has been checked in ip_wput_nondata */
9091 	mp1 = mp->b_cont->b_cont;
9092 	/*
9093 	 * Null terminate the string to protect against buffer
9094 	 * overrun. String was generated by user code and may not
9095 	 * be trusted.
9096 	 */
9097 	lifr = (struct lifreq *)mp1->b_rptr;
9098 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9099 	name = lifr->lifr_name;
9100 	ASSERT(CONN_Q(q));
9101 	connp = Q_TO_CONN(q);
9102 	isv6 = (connp->conn_family == AF_INET6);
9103 	zoneid = connp->conn_zoneid;
9104 	namelen = mi_strlen(name);
9105 	if (namelen == 0)
9106 		return (EINVAL);
9107 
9108 	exists = B_FALSE;
9109 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9110 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9111 		/*
9112 		 * Allow creating lo0 using SIOCLIFADDIF.
9113 		 * can't be any other writer thread. So can pass null below
9114 		 * for the last 4 args to ipif_lookup_name.
9115 		 */
9116 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9117 		    &exists, isv6, zoneid, ipst);
9118 		/* Prevent any further action */
9119 		if (ipif == NULL) {
9120 			return (ENOBUFS);
9121 		} else if (!exists) {
9122 			/* We created the ipif now and as writer */
9123 			ipif_refrele(ipif);
9124 			return (0);
9125 		} else {
9126 			ill = ipif->ipif_ill;
9127 			ill_refhold(ill);
9128 			ipif_refrele(ipif);
9129 		}
9130 	} else {
9131 		/* Look for a colon in the name. */
9132 		endp = &name[namelen];
9133 		for (cp = endp; --cp > name; ) {
9134 			if (*cp == IPIF_SEPARATOR_CHAR) {
9135 				found_sep = B_TRUE;
9136 				/*
9137 				 * Reject any non-decimal aliases for plumbing
9138 				 * of logical interfaces. Aliases with leading
9139 				 * zeroes are also rejected as they introduce
9140 				 * ambiguity in the naming of the interfaces.
9141 				 * Comparing with "0" takes care of all such
9142 				 * cases.
9143 				 */
9144 				if ((strncmp("0", cp+1, 1)) == 0)
9145 					return (EINVAL);
9146 
9147 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9148 				    id <= 0 || *endp != '\0') {
9149 					return (EINVAL);
9150 				}
9151 				*cp = '\0';
9152 				break;
9153 			}
9154 		}
9155 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9156 		if (found_sep)
9157 			*cp = IPIF_SEPARATOR_CHAR;
9158 		if (ill == NULL)
9159 			return (ENXIO);
9160 	}
9161 
9162 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9163 	    B_TRUE);
9164 
9165 	/*
9166 	 * Release the refhold due to the lookup, now that we are excl
9167 	 * or we are just returning
9168 	 */
9169 	ill_refrele(ill);
9170 
9171 	if (ipsq == NULL)
9172 		return (EINPROGRESS);
9173 
9174 	/* We are now exclusive on the IPSQ */
9175 	ASSERT(IAM_WRITER_ILL(ill));
9176 
9177 	if (found_sep) {
9178 		/* Now see if there is an IPIF with this unit number. */
9179 		for (ipif = ill->ill_ipif; ipif != NULL;
9180 		    ipif = ipif->ipif_next) {
9181 			if (ipif->ipif_id == id) {
9182 				err = EEXIST;
9183 				goto done;
9184 			}
9185 		}
9186 	}
9187 
9188 	/*
9189 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9190 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9191 	 * instead.
9192 	 */
9193 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9194 	    B_TRUE, B_TRUE)) == NULL) {
9195 		err = ENOBUFS;
9196 		goto done;
9197 	}
9198 
9199 	/* Return created name with ioctl */
9200 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9201 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9202 	ip1dbg(("created %s\n", lifr->lifr_name));
9203 
9204 	/* Set address */
9205 	sin = (sin_t *)&lifr->lifr_addr;
9206 	if (sin->sin_family != AF_UNSPEC) {
9207 		err = ip_sioctl_addr(ipif, sin, q, mp,
9208 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9209 	}
9210 
9211 done:
9212 	ipsq_exit(ipsq);
9213 	return (err);
9214 }
9215 
9216 /*
9217  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9218  * interface) delete it based on the IP address (on this physical interface).
9219  * Otherwise delete it based on the ipif_id.
9220  * Also, special handling to allow a removeif of lo0.
9221  */
9222 /* ARGSUSED */
9223 int
9224 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9225     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9226 {
9227 	conn_t		*connp;
9228 	ill_t		*ill = ipif->ipif_ill;
9229 	boolean_t	 success;
9230 	ip_stack_t	*ipst;
9231 
9232 	ipst = CONNQ_TO_IPST(q);
9233 
9234 	ASSERT(q->q_next == NULL);
9235 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9236 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9237 	ASSERT(IAM_WRITER_IPIF(ipif));
9238 
9239 	connp = Q_TO_CONN(q);
9240 	/*
9241 	 * Special case for unplumbing lo0 (the loopback physical interface).
9242 	 * If unplumbing lo0, the incoming address structure has been
9243 	 * initialized to all zeros. When unplumbing lo0, all its logical
9244 	 * interfaces must be removed too.
9245 	 *
9246 	 * Note that this interface may be called to remove a specific
9247 	 * loopback logical interface (eg, lo0:1). But in that case
9248 	 * ipif->ipif_id != 0 so that the code path for that case is the
9249 	 * same as any other interface (meaning it skips the code directly
9250 	 * below).
9251 	 */
9252 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9253 		if (sin->sin_family == AF_UNSPEC &&
9254 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9255 			/*
9256 			 * Mark it condemned. No new ref. will be made to ill.
9257 			 */
9258 			mutex_enter(&ill->ill_lock);
9259 			ill->ill_state_flags |= ILL_CONDEMNED;
9260 			for (ipif = ill->ill_ipif; ipif != NULL;
9261 			    ipif = ipif->ipif_next) {
9262 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9263 			}
9264 			mutex_exit(&ill->ill_lock);
9265 
9266 			ipif = ill->ill_ipif;
9267 			/* unplumb the loopback interface */
9268 			ill_delete(ill);
9269 			mutex_enter(&connp->conn_lock);
9270 			mutex_enter(&ill->ill_lock);
9271 
9272 			/* Are any references to this ill active */
9273 			if (ill_is_freeable(ill)) {
9274 				mutex_exit(&ill->ill_lock);
9275 				mutex_exit(&connp->conn_lock);
9276 				ill_delete_tail(ill);
9277 				mi_free(ill);
9278 				return (0);
9279 			}
9280 			success = ipsq_pending_mp_add(connp, ipif,
9281 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9282 			mutex_exit(&connp->conn_lock);
9283 			mutex_exit(&ill->ill_lock);
9284 			if (success)
9285 				return (EINPROGRESS);
9286 			else
9287 				return (EINTR);
9288 		}
9289 	}
9290 
9291 	if (ipif->ipif_id == 0) {
9292 		ipsq_t *ipsq;
9293 
9294 		/* Find based on address */
9295 		if (ipif->ipif_isv6) {
9296 			sin6_t *sin6;
9297 
9298 			if (sin->sin_family != AF_INET6)
9299 				return (EAFNOSUPPORT);
9300 
9301 			sin6 = (sin6_t *)sin;
9302 			/* We are a writer, so we should be able to lookup */
9303 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9304 			    ipst);
9305 		} else {
9306 			if (sin->sin_family != AF_INET)
9307 				return (EAFNOSUPPORT);
9308 
9309 			/* We are a writer, so we should be able to lookup */
9310 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9311 			    ipst);
9312 		}
9313 		if (ipif == NULL) {
9314 			return (EADDRNOTAVAIL);
9315 		}
9316 
9317 		/*
9318 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9319 		 * lifr_name of the physical interface but with an ip address
9320 		 * lifr_addr of a logical interface plumbed over it.
9321 		 * So update ipx_current_ipif now that ipif points to the
9322 		 * correct one.
9323 		 */
9324 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9325 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9326 
9327 		/* This is a writer */
9328 		ipif_refrele(ipif);
9329 	}
9330 
9331 	/*
9332 	 * Can not delete instance zero since it is tied to the ill.
9333 	 */
9334 	if (ipif->ipif_id == 0)
9335 		return (EBUSY);
9336 
9337 	mutex_enter(&ill->ill_lock);
9338 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9339 	mutex_exit(&ill->ill_lock);
9340 
9341 	ipif_free(ipif);
9342 
9343 	mutex_enter(&connp->conn_lock);
9344 	mutex_enter(&ill->ill_lock);
9345 
9346 	/* Are any references to this ipif active */
9347 	if (ipif_is_freeable(ipif)) {
9348 		mutex_exit(&ill->ill_lock);
9349 		mutex_exit(&connp->conn_lock);
9350 		ipif_non_duplicate(ipif);
9351 		(void) ipif_down_tail(ipif);
9352 		ipif_free_tail(ipif); /* frees ipif */
9353 		return (0);
9354 	}
9355 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9356 	    IPIF_FREE);
9357 	mutex_exit(&ill->ill_lock);
9358 	mutex_exit(&connp->conn_lock);
9359 	if (success)
9360 		return (EINPROGRESS);
9361 	else
9362 		return (EINTR);
9363 }
9364 
9365 /*
9366  * Restart the removeif ioctl. The refcnt has gone down to 0.
9367  * The ipif is already condemned. So can't find it thru lookups.
9368  */
9369 /* ARGSUSED */
9370 int
9371 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9372     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9373 {
9374 	ill_t *ill = ipif->ipif_ill;
9375 
9376 	ASSERT(IAM_WRITER_IPIF(ipif));
9377 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9378 
9379 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9380 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9381 
9382 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9383 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9384 		ill_delete_tail(ill);
9385 		mi_free(ill);
9386 		return (0);
9387 	}
9388 
9389 	ipif_non_duplicate(ipif);
9390 	(void) ipif_down_tail(ipif);
9391 	ipif_free_tail(ipif);
9392 
9393 	return (0);
9394 }
9395 
9396 /*
9397  * Set the local interface address.
9398  * Allow an address of all zero when the interface is down.
9399  */
9400 /* ARGSUSED */
9401 int
9402 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9403     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9404 {
9405 	int err = 0;
9406 	in6_addr_t v6addr;
9407 	boolean_t need_up = B_FALSE;
9408 
9409 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9410 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9411 
9412 	ASSERT(IAM_WRITER_IPIF(ipif));
9413 
9414 	if (ipif->ipif_isv6) {
9415 		sin6_t *sin6;
9416 		ill_t *ill;
9417 		phyint_t *phyi;
9418 
9419 		if (sin->sin_family != AF_INET6)
9420 			return (EAFNOSUPPORT);
9421 
9422 		sin6 = (sin6_t *)sin;
9423 		v6addr = sin6->sin6_addr;
9424 		ill = ipif->ipif_ill;
9425 		phyi = ill->ill_phyint;
9426 
9427 		/*
9428 		 * Enforce that true multicast interfaces have a link-local
9429 		 * address for logical unit 0.
9430 		 */
9431 		if (ipif->ipif_id == 0 &&
9432 		    (ill->ill_flags & ILLF_MULTICAST) &&
9433 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9434 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9435 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9436 			return (EADDRNOTAVAIL);
9437 		}
9438 
9439 		/*
9440 		 * up interfaces shouldn't have the unspecified address
9441 		 * unless they also have the IPIF_NOLOCAL flags set and
9442 		 * have a subnet assigned.
9443 		 */
9444 		if ((ipif->ipif_flags & IPIF_UP) &&
9445 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9446 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9447 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9448 			return (EADDRNOTAVAIL);
9449 		}
9450 
9451 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9452 			return (EADDRNOTAVAIL);
9453 	} else {
9454 		ipaddr_t addr;
9455 
9456 		if (sin->sin_family != AF_INET)
9457 			return (EAFNOSUPPORT);
9458 
9459 		addr = sin->sin_addr.s_addr;
9460 
9461 		/* Allow 0 as the local address. */
9462 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9463 			return (EADDRNOTAVAIL);
9464 
9465 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9466 	}
9467 
9468 	/*
9469 	 * Even if there is no change we redo things just to rerun
9470 	 * ipif_set_default.
9471 	 */
9472 	if (ipif->ipif_flags & IPIF_UP) {
9473 		/*
9474 		 * Setting a new local address, make sure
9475 		 * we have net and subnet bcast ire's for
9476 		 * the old address if we need them.
9477 		 */
9478 		/*
9479 		 * If the interface is already marked up,
9480 		 * we call ipif_down which will take care
9481 		 * of ditching any IREs that have been set
9482 		 * up based on the old interface address.
9483 		 */
9484 		err = ipif_logical_down(ipif, q, mp);
9485 		if (err == EINPROGRESS)
9486 			return (err);
9487 		(void) ipif_down_tail(ipif);
9488 		need_up = 1;
9489 	}
9490 
9491 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9492 	return (err);
9493 }
9494 
9495 int
9496 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9497     boolean_t need_up)
9498 {
9499 	in6_addr_t v6addr;
9500 	in6_addr_t ov6addr;
9501 	ipaddr_t addr;
9502 	sin6_t	*sin6;
9503 	int	sinlen;
9504 	int	err = 0;
9505 	ill_t	*ill = ipif->ipif_ill;
9506 	boolean_t need_dl_down;
9507 	boolean_t need_arp_down;
9508 	struct iocblk *iocp;
9509 
9510 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9511 
9512 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9513 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9514 	ASSERT(IAM_WRITER_IPIF(ipif));
9515 
9516 	/* Must cancel any pending timer before taking the ill_lock */
9517 	if (ipif->ipif_recovery_id != 0)
9518 		(void) untimeout(ipif->ipif_recovery_id);
9519 	ipif->ipif_recovery_id = 0;
9520 
9521 	if (ipif->ipif_isv6) {
9522 		sin6 = (sin6_t *)sin;
9523 		v6addr = sin6->sin6_addr;
9524 		sinlen = sizeof (struct sockaddr_in6);
9525 	} else {
9526 		addr = sin->sin_addr.s_addr;
9527 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9528 		sinlen = sizeof (struct sockaddr_in);
9529 	}
9530 	mutex_enter(&ill->ill_lock);
9531 	ov6addr = ipif->ipif_v6lcl_addr;
9532 	ipif->ipif_v6lcl_addr = v6addr;
9533 	sctp_update_ipif_addr(ipif, ov6addr);
9534 	ipif->ipif_addr_ready = 0;
9535 
9536 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9537 
9538 	/*
9539 	 * If the interface was previously marked as a duplicate, then since
9540 	 * we've now got a "new" address, it should no longer be considered a
9541 	 * duplicate -- even if the "new" address is the same as the old one.
9542 	 * Note that if all ipifs are down, we may have a pending ARP down
9543 	 * event to handle.  This is because we want to recover from duplicates
9544 	 * and thus delay tearing down ARP until the duplicates have been
9545 	 * removed or disabled.
9546 	 */
9547 	need_dl_down = need_arp_down = B_FALSE;
9548 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9549 		need_arp_down = !need_up;
9550 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9551 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9552 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9553 			need_dl_down = B_TRUE;
9554 		}
9555 	}
9556 
9557 	ipif_set_default(ipif);
9558 
9559 	/*
9560 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9561 	 * tag the ill so that future updates to the interface ID don't result
9562 	 * in this address getting automatically reconfigured from under the
9563 	 * administrator.
9564 	 */
9565 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9566 		ill->ill_manual_linklocal = 1;
9567 
9568 	/*
9569 	 * When publishing an interface address change event, we only notify
9570 	 * the event listeners of the new address.  It is assumed that if they
9571 	 * actively care about the addresses assigned that they will have
9572 	 * already discovered the previous address assigned (if there was one.)
9573 	 *
9574 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9575 	 */
9576 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9577 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9578 		    NE_ADDRESS_CHANGE, sin, sinlen);
9579 	}
9580 
9581 	mutex_exit(&ill->ill_lock);
9582 
9583 	if (need_up) {
9584 		/*
9585 		 * Now bring the interface back up.  If this
9586 		 * is the only IPIF for the ILL, ipif_up
9587 		 * will have to re-bind to the device, so
9588 		 * we may get back EINPROGRESS, in which
9589 		 * case, this IOCTL will get completed in
9590 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9591 		 */
9592 		err = ipif_up(ipif, q, mp);
9593 	} else {
9594 		/* Perhaps ilgs should use this ill */
9595 		update_conn_ill(NULL, ill->ill_ipst);
9596 	}
9597 
9598 	if (need_dl_down)
9599 		ill_dl_down(ill);
9600 
9601 	if (need_arp_down && !ill->ill_isv6)
9602 		(void) ipif_arp_down(ipif);
9603 
9604 	/*
9605 	 * The default multicast interface might have changed (for
9606 	 * instance if the IPv6 scope of the address changed)
9607 	 */
9608 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9609 
9610 	return (err);
9611 }
9612 
9613 /*
9614  * Restart entry point to restart the address set operation after the
9615  * refcounts have dropped to zero.
9616  */
9617 /* ARGSUSED */
9618 int
9619 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9620     ip_ioctl_cmd_t *ipip, void *ifreq)
9621 {
9622 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9623 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9624 	ASSERT(IAM_WRITER_IPIF(ipif));
9625 	(void) ipif_down_tail(ipif);
9626 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9627 }
9628 
9629 /* ARGSUSED */
9630 int
9631 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9632     ip_ioctl_cmd_t *ipip, void *if_req)
9633 {
9634 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9635 	struct lifreq *lifr = (struct lifreq *)if_req;
9636 
9637 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9639 	/*
9640 	 * The net mask and address can't change since we have a
9641 	 * reference to the ipif. So no lock is necessary.
9642 	 */
9643 	if (ipif->ipif_isv6) {
9644 		*sin6 = sin6_null;
9645 		sin6->sin6_family = AF_INET6;
9646 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9647 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9648 		lifr->lifr_addrlen =
9649 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9650 	} else {
9651 		*sin = sin_null;
9652 		sin->sin_family = AF_INET;
9653 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9654 		if (ipip->ipi_cmd_type == LIF_CMD) {
9655 			lifr->lifr_addrlen =
9656 			    ip_mask_to_plen(ipif->ipif_net_mask);
9657 		}
9658 	}
9659 	return (0);
9660 }
9661 
9662 /*
9663  * Set the destination address for a pt-pt interface.
9664  */
9665 /* ARGSUSED */
9666 int
9667 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9668     ip_ioctl_cmd_t *ipip, void *if_req)
9669 {
9670 	int err = 0;
9671 	in6_addr_t v6addr;
9672 	boolean_t need_up = B_FALSE;
9673 
9674 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9675 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9676 	ASSERT(IAM_WRITER_IPIF(ipif));
9677 
9678 	if (ipif->ipif_isv6) {
9679 		sin6_t *sin6;
9680 
9681 		if (sin->sin_family != AF_INET6)
9682 			return (EAFNOSUPPORT);
9683 
9684 		sin6 = (sin6_t *)sin;
9685 		v6addr = sin6->sin6_addr;
9686 
9687 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9688 			return (EADDRNOTAVAIL);
9689 	} else {
9690 		ipaddr_t addr;
9691 
9692 		if (sin->sin_family != AF_INET)
9693 			return (EAFNOSUPPORT);
9694 
9695 		addr = sin->sin_addr.s_addr;
9696 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9697 			return (EADDRNOTAVAIL);
9698 
9699 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9700 	}
9701 
9702 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9703 		return (0);	/* No change */
9704 
9705 	if (ipif->ipif_flags & IPIF_UP) {
9706 		/*
9707 		 * If the interface is already marked up,
9708 		 * we call ipif_down which will take care
9709 		 * of ditching any IREs that have been set
9710 		 * up based on the old pp dst address.
9711 		 */
9712 		err = ipif_logical_down(ipif, q, mp);
9713 		if (err == EINPROGRESS)
9714 			return (err);
9715 		(void) ipif_down_tail(ipif);
9716 		need_up = B_TRUE;
9717 	}
9718 	/*
9719 	 * could return EINPROGRESS. If so ioctl will complete in
9720 	 * ip_rput_dlpi_writer
9721 	 */
9722 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9723 	return (err);
9724 }
9725 
9726 static int
9727 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9728     boolean_t need_up)
9729 {
9730 	in6_addr_t v6addr;
9731 	ill_t	*ill = ipif->ipif_ill;
9732 	int	err = 0;
9733 	boolean_t need_dl_down;
9734 	boolean_t need_arp_down;
9735 
9736 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9737 	    ipif->ipif_id, (void *)ipif));
9738 
9739 	/* Must cancel any pending timer before taking the ill_lock */
9740 	if (ipif->ipif_recovery_id != 0)
9741 		(void) untimeout(ipif->ipif_recovery_id);
9742 	ipif->ipif_recovery_id = 0;
9743 
9744 	if (ipif->ipif_isv6) {
9745 		sin6_t *sin6;
9746 
9747 		sin6 = (sin6_t *)sin;
9748 		v6addr = sin6->sin6_addr;
9749 	} else {
9750 		ipaddr_t addr;
9751 
9752 		addr = sin->sin_addr.s_addr;
9753 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9754 	}
9755 	mutex_enter(&ill->ill_lock);
9756 	/* Set point to point destination address. */
9757 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9758 		/*
9759 		 * Allow this as a means of creating logical
9760 		 * pt-pt interfaces on top of e.g. an Ethernet.
9761 		 * XXX Undocumented HACK for testing.
9762 		 * pt-pt interfaces are created with NUD disabled.
9763 		 */
9764 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9765 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9766 		if (ipif->ipif_isv6)
9767 			ill->ill_flags |= ILLF_NONUD;
9768 	}
9769 
9770 	/*
9771 	 * If the interface was previously marked as a duplicate, then since
9772 	 * we've now got a "new" address, it should no longer be considered a
9773 	 * duplicate -- even if the "new" address is the same as the old one.
9774 	 * Note that if all ipifs are down, we may have a pending ARP down
9775 	 * event to handle.
9776 	 */
9777 	need_dl_down = need_arp_down = B_FALSE;
9778 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9779 		need_arp_down = !need_up;
9780 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9781 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9782 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9783 			need_dl_down = B_TRUE;
9784 		}
9785 	}
9786 
9787 	/* Set the new address. */
9788 	ipif->ipif_v6pp_dst_addr = v6addr;
9789 	/* Make sure subnet tracks pp_dst */
9790 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9791 	mutex_exit(&ill->ill_lock);
9792 
9793 	if (need_up) {
9794 		/*
9795 		 * Now bring the interface back up.  If this
9796 		 * is the only IPIF for the ILL, ipif_up
9797 		 * will have to re-bind to the device, so
9798 		 * we may get back EINPROGRESS, in which
9799 		 * case, this IOCTL will get completed in
9800 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9801 		 */
9802 		err = ipif_up(ipif, q, mp);
9803 	}
9804 
9805 	if (need_dl_down)
9806 		ill_dl_down(ill);
9807 	if (need_arp_down && !ipif->ipif_isv6)
9808 		(void) ipif_arp_down(ipif);
9809 
9810 	return (err);
9811 }
9812 
9813 /*
9814  * Restart entry point to restart the dstaddress set operation after the
9815  * refcounts have dropped to zero.
9816  */
9817 /* ARGSUSED */
9818 int
9819 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9820     ip_ioctl_cmd_t *ipip, void *ifreq)
9821 {
9822 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9823 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9824 	(void) ipif_down_tail(ipif);
9825 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9826 }
9827 
9828 /* ARGSUSED */
9829 int
9830 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9831     ip_ioctl_cmd_t *ipip, void *if_req)
9832 {
9833 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9834 
9835 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9836 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9837 	/*
9838 	 * Get point to point destination address. The addresses can't
9839 	 * change since we hold a reference to the ipif.
9840 	 */
9841 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9842 		return (EADDRNOTAVAIL);
9843 
9844 	if (ipif->ipif_isv6) {
9845 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9846 		*sin6 = sin6_null;
9847 		sin6->sin6_family = AF_INET6;
9848 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9849 	} else {
9850 		*sin = sin_null;
9851 		sin->sin_family = AF_INET;
9852 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9853 	}
9854 	return (0);
9855 }
9856 
9857 /*
9858  * Check which flags will change by the given flags being set
9859  * silently ignore flags which userland is not allowed to control.
9860  * (Because these flags may change between SIOCGLIFFLAGS and
9861  * SIOCSLIFFLAGS, and that's outside of userland's control,
9862  * we need to silently ignore them rather than fail.)
9863  */
9864 static void
9865 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9866     uint64_t *offp)
9867 {
9868 	ill_t		*ill = ipif->ipif_ill;
9869 	phyint_t 	*phyi = ill->ill_phyint;
9870 	uint64_t	cantchange_flags, intf_flags;
9871 	uint64_t	turn_on, turn_off;
9872 
9873 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9874 	cantchange_flags = IFF_CANTCHANGE;
9875 	if (IS_IPMP(ill))
9876 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9877 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9878 	turn_off = intf_flags & turn_on;
9879 	turn_on ^= turn_off;
9880 	*onp = turn_on;
9881 	*offp = turn_off;
9882 }
9883 
9884 /*
9885  * Set interface flags.  Many flags require special handling (e.g.,
9886  * bringing the interface down); see below for details.
9887  *
9888  * NOTE : We really don't enforce that ipif_id zero should be used
9889  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9890  *	  is because applications generally does SICGLIFFLAGS and
9891  *	  ORs in the new flags (that affects the logical) and does a
9892  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9893  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9894  *	  flags that will be turned on is correct with respect to
9895  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9896  */
9897 /* ARGSUSED */
9898 int
9899 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9900     ip_ioctl_cmd_t *ipip, void *if_req)
9901 {
9902 	uint64_t turn_on;
9903 	uint64_t turn_off;
9904 	int	err = 0;
9905 	phyint_t *phyi;
9906 	ill_t *ill;
9907 	conn_t *connp;
9908 	uint64_t intf_flags;
9909 	boolean_t phyint_flags_modified = B_FALSE;
9910 	uint64_t flags;
9911 	struct ifreq *ifr;
9912 	struct lifreq *lifr;
9913 	boolean_t set_linklocal = B_FALSE;
9914 
9915 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9917 
9918 	ASSERT(IAM_WRITER_IPIF(ipif));
9919 
9920 	ill = ipif->ipif_ill;
9921 	phyi = ill->ill_phyint;
9922 
9923 	if (ipip->ipi_cmd_type == IF_CMD) {
9924 		ifr = (struct ifreq *)if_req;
9925 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9926 	} else {
9927 		lifr = (struct lifreq *)if_req;
9928 		flags = lifr->lifr_flags;
9929 	}
9930 
9931 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9932 
9933 	/*
9934 	 * Have the flags been set correctly until now?
9935 	 */
9936 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9937 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9938 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9939 	/*
9940 	 * Compare the new flags to the old, and partition
9941 	 * into those coming on and those going off.
9942 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9943 	 */
9944 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9945 		flags |= intf_flags & ~0xFFFF;
9946 
9947 	/*
9948 	 * Explicitly fail attempts to change flags that are always invalid on
9949 	 * an IPMP meta-interface.
9950 	 */
9951 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9952 		return (EINVAL);
9953 
9954 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
9955 	if ((turn_on|turn_off) == 0)
9956 		return (0);	/* No change */
9957 
9958 	/*
9959 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
9960 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
9961 	 * allow it to be turned off.
9962 	 */
9963 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
9964 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
9965 		return (EINVAL);
9966 
9967 	if ((connp = Q_TO_CONN(q)) == NULL)
9968 		return (EINVAL);
9969 
9970 	/*
9971 	 * Only vrrp control socket is allowed to change IFF_UP and
9972 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
9973 	 */
9974 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
9975 		if (!connp->conn_isvrrp)
9976 			return (EINVAL);
9977 	}
9978 
9979 	/*
9980 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
9981 	 * VRRP control socket.
9982 	 */
9983 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
9984 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
9985 			return (EINVAL);
9986 	}
9987 
9988 	if (turn_on & IFF_NOFAILOVER) {
9989 		turn_on |= IFF_DEPRECATED;
9990 		flags |= IFF_DEPRECATED;
9991 	}
9992 
9993 	/*
9994 	 * On underlying interfaces, only allow applications to manage test
9995 	 * addresses -- otherwise, they may get confused when the address
9996 	 * moves as part of being brought up.  Likewise, prevent an
9997 	 * application-managed test address from being converted to a data
9998 	 * address.  To prevent migration of administratively up addresses in
9999 	 * the kernel, we don't allow them to be converted either.
10000 	 */
10001 	if (IS_UNDER_IPMP(ill)) {
10002 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10003 
10004 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10005 			return (EINVAL);
10006 
10007 		if ((turn_off & IFF_NOFAILOVER) &&
10008 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10009 			return (EINVAL);
10010 	}
10011 
10012 	/*
10013 	 * Only allow IFF_TEMPORARY flag to be set on
10014 	 * IPv6 interfaces.
10015 	 */
10016 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10017 		return (EINVAL);
10018 
10019 	/*
10020 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10021 	 */
10022 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10023 		return (EINVAL);
10024 
10025 	/*
10026 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10027 	 * interfaces.  It makes no sense in that context.
10028 	 */
10029 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10030 		return (EINVAL);
10031 
10032 	/*
10033 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10034 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10035 	 * If the link local address isn't set, and can be set, it will get
10036 	 * set later on in this function.
10037 	 */
10038 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10039 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10040 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10041 		if (ipif_cant_setlinklocal(ipif))
10042 			return (EINVAL);
10043 		set_linklocal = B_TRUE;
10044 	}
10045 
10046 	/*
10047 	 * If we modify physical interface flags, we'll potentially need to
10048 	 * send up two routing socket messages for the changes (one for the
10049 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10050 	 */
10051 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10052 		phyint_flags_modified = B_TRUE;
10053 
10054 	/*
10055 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10056 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10057 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10058 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10059 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10060 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10061 	 * will not be honored.
10062 	 */
10063 	if (turn_on & PHYI_STANDBY) {
10064 		/*
10065 		 * No need to grab ill_g_usesrc_lock here; see the
10066 		 * synchronization notes in ip.c.
10067 		 */
10068 		if (ill->ill_usesrc_grp_next != NULL ||
10069 		    intf_flags & PHYI_INACTIVE)
10070 			return (EINVAL);
10071 		if (!(flags & PHYI_FAILED)) {
10072 			flags |= PHYI_INACTIVE;
10073 			turn_on |= PHYI_INACTIVE;
10074 		}
10075 	}
10076 
10077 	if (turn_off & PHYI_STANDBY) {
10078 		flags &= ~PHYI_INACTIVE;
10079 		turn_off |= PHYI_INACTIVE;
10080 	}
10081 
10082 	/*
10083 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10084 	 * would end up on.
10085 	 */
10086 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10087 	    (PHYI_FAILED | PHYI_INACTIVE))
10088 		return (EINVAL);
10089 
10090 	/*
10091 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10092 	 * status of the interface.
10093 	 */
10094 	if ((turn_on | turn_off) & ILLF_ROUTER)
10095 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10096 
10097 	/*
10098 	 * If the interface is not UP and we are not going to
10099 	 * bring it UP, record the flags and return. When the
10100 	 * interface comes UP later, the right actions will be
10101 	 * taken.
10102 	 */
10103 	if (!(ipif->ipif_flags & IPIF_UP) &&
10104 	    !(turn_on & IPIF_UP)) {
10105 		/* Record new flags in their respective places. */
10106 		mutex_enter(&ill->ill_lock);
10107 		mutex_enter(&ill->ill_phyint->phyint_lock);
10108 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10109 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10110 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10111 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10112 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10113 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10114 		mutex_exit(&ill->ill_lock);
10115 		mutex_exit(&ill->ill_phyint->phyint_lock);
10116 
10117 		/*
10118 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10119 		 * same to the kernel: if any of them has been set by
10120 		 * userland, the interface cannot be used for data traffic.
10121 		 */
10122 		if ((turn_on|turn_off) &
10123 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10124 			ASSERT(!IS_IPMP(ill));
10125 			/*
10126 			 * It's possible the ill is part of an "anonymous"
10127 			 * IPMP group rather than a real group.  In that case,
10128 			 * there are no other interfaces in the group and thus
10129 			 * no need to call ipmp_phyint_refresh_active().
10130 			 */
10131 			if (IS_UNDER_IPMP(ill))
10132 				ipmp_phyint_refresh_active(phyi);
10133 		}
10134 
10135 		if (phyint_flags_modified) {
10136 			if (phyi->phyint_illv4 != NULL) {
10137 				ip_rts_ifmsg(phyi->phyint_illv4->
10138 				    ill_ipif, RTSQ_DEFAULT);
10139 			}
10140 			if (phyi->phyint_illv6 != NULL) {
10141 				ip_rts_ifmsg(phyi->phyint_illv6->
10142 				    ill_ipif, RTSQ_DEFAULT);
10143 			}
10144 		}
10145 		/* The default multicast interface might have changed */
10146 		ire_increment_multicast_generation(ill->ill_ipst,
10147 		    ill->ill_isv6);
10148 
10149 		return (0);
10150 	} else if (set_linklocal) {
10151 		mutex_enter(&ill->ill_lock);
10152 		if (set_linklocal)
10153 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10154 		mutex_exit(&ill->ill_lock);
10155 	}
10156 
10157 	/*
10158 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10159 	 * or point-to-point interfaces with an unspecified destination. We do
10160 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10161 	 * have a subnet assigned, which is how in.ndpd currently manages its
10162 	 * onlink prefix list when no addresses are configured with those
10163 	 * prefixes.
10164 	 */
10165 	if (ipif->ipif_isv6 &&
10166 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10167 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10168 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10169 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10170 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10171 		return (EINVAL);
10172 	}
10173 
10174 	/*
10175 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10176 	 * from being brought up.
10177 	 */
10178 	if (!ipif->ipif_isv6 &&
10179 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10180 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10181 		return (EINVAL);
10182 	}
10183 
10184 	/*
10185 	 * If we are going to change one or more of the flags that are
10186 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10187 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10188 	 * IPIF_NOFAILOVER, we will take special action.  This is
10189 	 * done by bring the ipif down, changing the flags and bringing
10190 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10191 	 * back up will trigger the address to be moved.
10192 	 *
10193 	 * If we are going to change IFF_NOACCEPT, we need to bring
10194 	 * all the ipifs down then bring them up again.	 The act of
10195 	 * bringing all the ipifs back up will trigger the local
10196 	 * ires being recreated with "no_accept" set/cleared.
10197 	 *
10198 	 * Note that ILLF_NOACCEPT is always set separately from the
10199 	 * other flags.
10200 	 */
10201 	if ((turn_on|turn_off) &
10202 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10203 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10204 	    IPIF_NOFAILOVER)) {
10205 		/*
10206 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10207 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10208 		 * entries shared between multiple ipifs on the same subnet.
10209 		 */
10210 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10211 		    !(turn_off & IPIF_UP)) {
10212 			if (ipif->ipif_flags & IPIF_UP)
10213 				ill->ill_logical_down = 1;
10214 			turn_on &= ~IPIF_UP;
10215 		}
10216 		err = ipif_down(ipif, q, mp);
10217 		ip1dbg(("ipif_down returns %d err ", err));
10218 		if (err == EINPROGRESS)
10219 			return (err);
10220 		(void) ipif_down_tail(ipif);
10221 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10222 		/*
10223 		 * If we can quiesce the ill, then continue.  If not, then
10224 		 * ip_sioctl_flags_tail() will be called from
10225 		 * ipif_ill_refrele_tail().
10226 		 */
10227 		ill_down_ipifs(ill, B_TRUE);
10228 
10229 		mutex_enter(&connp->conn_lock);
10230 		mutex_enter(&ill->ill_lock);
10231 		if (!ill_is_quiescent(ill)) {
10232 			boolean_t success;
10233 
10234 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10235 			    q, mp, ILL_DOWN);
10236 			mutex_exit(&ill->ill_lock);
10237 			mutex_exit(&connp->conn_lock);
10238 			return (success ? EINPROGRESS : EINTR);
10239 		}
10240 		mutex_exit(&ill->ill_lock);
10241 		mutex_exit(&connp->conn_lock);
10242 	}
10243 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10244 }
10245 
10246 static int
10247 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10248 {
10249 	ill_t	*ill;
10250 	phyint_t *phyi;
10251 	uint64_t turn_on, turn_off;
10252 	boolean_t phyint_flags_modified = B_FALSE;
10253 	int	err = 0;
10254 	boolean_t set_linklocal = B_FALSE;
10255 
10256 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10257 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10258 
10259 	ASSERT(IAM_WRITER_IPIF(ipif));
10260 
10261 	ill = ipif->ipif_ill;
10262 	phyi = ill->ill_phyint;
10263 
10264 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10265 
10266 	/*
10267 	 * IFF_UP is handled separately.
10268 	 */
10269 	turn_on &= ~IFF_UP;
10270 	turn_off &= ~IFF_UP;
10271 
10272 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10273 		phyint_flags_modified = B_TRUE;
10274 
10275 	/*
10276 	 * Now we change the flags. Track current value of
10277 	 * other flags in their respective places.
10278 	 */
10279 	mutex_enter(&ill->ill_lock);
10280 	mutex_enter(&phyi->phyint_lock);
10281 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10282 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10283 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10284 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10285 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10286 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10287 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10288 		set_linklocal = B_TRUE;
10289 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10290 	}
10291 
10292 	mutex_exit(&ill->ill_lock);
10293 	mutex_exit(&phyi->phyint_lock);
10294 
10295 	if (set_linklocal)
10296 		(void) ipif_setlinklocal(ipif);
10297 
10298 	/*
10299 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10300 	 * the kernel: if any of them has been set by userland, the interface
10301 	 * cannot be used for data traffic.
10302 	 */
10303 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10304 		ASSERT(!IS_IPMP(ill));
10305 		/*
10306 		 * It's possible the ill is part of an "anonymous" IPMP group
10307 		 * rather than a real group.  In that case, there are no other
10308 		 * interfaces in the group and thus no need for us to call
10309 		 * ipmp_phyint_refresh_active().
10310 		 */
10311 		if (IS_UNDER_IPMP(ill))
10312 			ipmp_phyint_refresh_active(phyi);
10313 	}
10314 
10315 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10316 		/*
10317 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10318 		 * ipifs that were brought down.
10319 		 *
10320 		 * The routing sockets messages are sent as the result
10321 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10322 		 * as well.
10323 		 */
10324 		err = ill_up_ipifs(ill, q, mp);
10325 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10326 		/*
10327 		 * XXX ipif_up really does not know whether a phyint flags
10328 		 * was modified or not. So, it sends up information on
10329 		 * only one routing sockets message. As we don't bring up
10330 		 * the interface and also set PHYI_ flags simultaneously
10331 		 * it should be okay.
10332 		 */
10333 		err = ipif_up(ipif, q, mp);
10334 	} else {
10335 		/*
10336 		 * Make sure routing socket sees all changes to the flags.
10337 		 * ipif_up_done* handles this when we use ipif_up.
10338 		 */
10339 		if (phyint_flags_modified) {
10340 			if (phyi->phyint_illv4 != NULL) {
10341 				ip_rts_ifmsg(phyi->phyint_illv4->
10342 				    ill_ipif, RTSQ_DEFAULT);
10343 			}
10344 			if (phyi->phyint_illv6 != NULL) {
10345 				ip_rts_ifmsg(phyi->phyint_illv6->
10346 				    ill_ipif, RTSQ_DEFAULT);
10347 			}
10348 		} else {
10349 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10350 		}
10351 		/*
10352 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10353 		 * this in need_up case.
10354 		 */
10355 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10356 	}
10357 
10358 	/* The default multicast interface might have changed */
10359 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10360 	return (err);
10361 }
10362 
10363 /*
10364  * Restart the flags operation now that the refcounts have dropped to zero.
10365  */
10366 /* ARGSUSED */
10367 int
10368 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10369     ip_ioctl_cmd_t *ipip, void *if_req)
10370 {
10371 	uint64_t flags;
10372 	struct ifreq *ifr = if_req;
10373 	struct lifreq *lifr = if_req;
10374 	uint64_t turn_on, turn_off;
10375 
10376 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10377 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10378 
10379 	if (ipip->ipi_cmd_type == IF_CMD) {
10380 		/* cast to uint16_t prevents unwanted sign extension */
10381 		flags = (uint16_t)ifr->ifr_flags;
10382 	} else {
10383 		flags = lifr->lifr_flags;
10384 	}
10385 
10386 	/*
10387 	 * If this function call is a result of the ILLF_NOACCEPT flag
10388 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10389 	 */
10390 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10391 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10392 		(void) ipif_down_tail(ipif);
10393 
10394 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10395 }
10396 
10397 /*
10398  * Can operate on either a module or a driver queue.
10399  */
10400 /* ARGSUSED */
10401 int
10402 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10403     ip_ioctl_cmd_t *ipip, void *if_req)
10404 {
10405 	/*
10406 	 * Has the flags been set correctly till now ?
10407 	 */
10408 	ill_t *ill = ipif->ipif_ill;
10409 	phyint_t *phyi = ill->ill_phyint;
10410 
10411 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10412 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10413 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10414 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10415 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10416 
10417 	/*
10418 	 * Need a lock since some flags can be set even when there are
10419 	 * references to the ipif.
10420 	 */
10421 	mutex_enter(&ill->ill_lock);
10422 	if (ipip->ipi_cmd_type == IF_CMD) {
10423 		struct ifreq *ifr = (struct ifreq *)if_req;
10424 
10425 		/* Get interface flags (low 16 only). */
10426 		ifr->ifr_flags = ((ipif->ipif_flags |
10427 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10428 	} else {
10429 		struct lifreq *lifr = (struct lifreq *)if_req;
10430 
10431 		/* Get interface flags. */
10432 		lifr->lifr_flags = ipif->ipif_flags |
10433 		    ill->ill_flags | phyi->phyint_flags;
10434 	}
10435 	mutex_exit(&ill->ill_lock);
10436 	return (0);
10437 }
10438 
10439 /*
10440  * We allow the MTU to be set on an ILL, but not have it be different
10441  * for different IPIFs since we don't actually send packets on IPIFs.
10442  */
10443 /* ARGSUSED */
10444 int
10445 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10446     ip_ioctl_cmd_t *ipip, void *if_req)
10447 {
10448 	int mtu;
10449 	int ip_min_mtu;
10450 	struct ifreq	*ifr;
10451 	struct lifreq *lifr;
10452 	ill_t	*ill;
10453 
10454 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10455 	    ipif->ipif_id, (void *)ipif));
10456 	if (ipip->ipi_cmd_type == IF_CMD) {
10457 		ifr = (struct ifreq *)if_req;
10458 		mtu = ifr->ifr_metric;
10459 	} else {
10460 		lifr = (struct lifreq *)if_req;
10461 		mtu = lifr->lifr_mtu;
10462 	}
10463 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10464 	if (ipif->ipif_id != 0)
10465 		return (EINVAL);
10466 
10467 	ill = ipif->ipif_ill;
10468 	if (ipif->ipif_isv6)
10469 		ip_min_mtu = IPV6_MIN_MTU;
10470 	else
10471 		ip_min_mtu = IP_MIN_MTU;
10472 
10473 	mutex_enter(&ill->ill_lock);
10474 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10475 		mutex_exit(&ill->ill_lock);
10476 		return (EINVAL);
10477 	}
10478 	/*
10479 	 * The dce and fragmentation code can handle changes to ill_mtu
10480 	 * concurrent with sending/fragmenting packets.
10481 	 */
10482 	ill->ill_mtu = mtu;
10483 	ill->ill_flags |= ILLF_FIXEDMTU;
10484 	mutex_exit(&ill->ill_lock);
10485 
10486 	/*
10487 	 * Make sure all dce_generation checks find out
10488 	 * that ill_mtu has changed.
10489 	 */
10490 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10491 
10492 	/* Update the MTU in SCTP's list */
10493 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10494 	return (0);
10495 }
10496 
10497 /* Get interface MTU. */
10498 /* ARGSUSED */
10499 int
10500 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10501 	ip_ioctl_cmd_t *ipip, void *if_req)
10502 {
10503 	struct ifreq	*ifr;
10504 	struct lifreq	*lifr;
10505 
10506 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10507 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10508 
10509 	/*
10510 	 * We allow a get on any logical interface even though the set
10511 	 * can only be done on logical unit 0.
10512 	 */
10513 	if (ipip->ipi_cmd_type == IF_CMD) {
10514 		ifr = (struct ifreq *)if_req;
10515 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10516 	} else {
10517 		lifr = (struct lifreq *)if_req;
10518 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10519 	}
10520 	return (0);
10521 }
10522 
10523 /* Set interface broadcast address. */
10524 /* ARGSUSED2 */
10525 int
10526 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10527 	ip_ioctl_cmd_t *ipip, void *if_req)
10528 {
10529 	ipaddr_t addr;
10530 	ire_t	*ire;
10531 	ill_t		*ill = ipif->ipif_ill;
10532 	ip_stack_t	*ipst = ill->ill_ipst;
10533 
10534 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10535 	    ipif->ipif_id));
10536 
10537 	ASSERT(IAM_WRITER_IPIF(ipif));
10538 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10539 		return (EADDRNOTAVAIL);
10540 
10541 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10542 
10543 	if (sin->sin_family != AF_INET)
10544 		return (EAFNOSUPPORT);
10545 
10546 	addr = sin->sin_addr.s_addr;
10547 	if (ipif->ipif_flags & IPIF_UP) {
10548 		/*
10549 		 * If we are already up, make sure the new
10550 		 * broadcast address makes sense.  If it does,
10551 		 * there should be an IRE for it already.
10552 		 */
10553 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10554 		    ill, ipif->ipif_zoneid, NULL,
10555 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10556 		if (ire == NULL) {
10557 			return (EINVAL);
10558 		} else {
10559 			ire_refrele(ire);
10560 		}
10561 	}
10562 	/*
10563 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10564 	 * needs to already exist we never need to change the set of
10565 	 * IRE_BROADCASTs when we are UP.
10566 	 */
10567 	if (addr != ipif->ipif_brd_addr)
10568 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10569 
10570 	return (0);
10571 }
10572 
10573 /* Get interface broadcast address. */
10574 /* ARGSUSED */
10575 int
10576 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10577     ip_ioctl_cmd_t *ipip, void *if_req)
10578 {
10579 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10580 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10581 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10582 		return (EADDRNOTAVAIL);
10583 
10584 	/* IPIF_BROADCAST not possible with IPv6 */
10585 	ASSERT(!ipif->ipif_isv6);
10586 	*sin = sin_null;
10587 	sin->sin_family = AF_INET;
10588 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10589 	return (0);
10590 }
10591 
10592 /*
10593  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10594  */
10595 /* ARGSUSED */
10596 int
10597 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10598     ip_ioctl_cmd_t *ipip, void *if_req)
10599 {
10600 	int err = 0;
10601 	in6_addr_t v6mask;
10602 
10603 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10604 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10605 
10606 	ASSERT(IAM_WRITER_IPIF(ipif));
10607 
10608 	if (ipif->ipif_isv6) {
10609 		sin6_t *sin6;
10610 
10611 		if (sin->sin_family != AF_INET6)
10612 			return (EAFNOSUPPORT);
10613 
10614 		sin6 = (sin6_t *)sin;
10615 		v6mask = sin6->sin6_addr;
10616 	} else {
10617 		ipaddr_t mask;
10618 
10619 		if (sin->sin_family != AF_INET)
10620 			return (EAFNOSUPPORT);
10621 
10622 		mask = sin->sin_addr.s_addr;
10623 		V4MASK_TO_V6(mask, v6mask);
10624 	}
10625 
10626 	/*
10627 	 * No big deal if the interface isn't already up, or the mask
10628 	 * isn't really changing, or this is pt-pt.
10629 	 */
10630 	if (!(ipif->ipif_flags & IPIF_UP) ||
10631 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10632 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10633 		ipif->ipif_v6net_mask = v6mask;
10634 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10635 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10636 			    ipif->ipif_v6net_mask,
10637 			    ipif->ipif_v6subnet);
10638 		}
10639 		return (0);
10640 	}
10641 	/*
10642 	 * Make sure we have valid net and subnet broadcast ire's
10643 	 * for the old netmask, if needed by other logical interfaces.
10644 	 */
10645 	err = ipif_logical_down(ipif, q, mp);
10646 	if (err == EINPROGRESS)
10647 		return (err);
10648 	(void) ipif_down_tail(ipif);
10649 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10650 	return (err);
10651 }
10652 
10653 static int
10654 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10655 {
10656 	in6_addr_t v6mask;
10657 	int err = 0;
10658 
10659 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10660 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10661 
10662 	if (ipif->ipif_isv6) {
10663 		sin6_t *sin6;
10664 
10665 		sin6 = (sin6_t *)sin;
10666 		v6mask = sin6->sin6_addr;
10667 	} else {
10668 		ipaddr_t mask;
10669 
10670 		mask = sin->sin_addr.s_addr;
10671 		V4MASK_TO_V6(mask, v6mask);
10672 	}
10673 
10674 	ipif->ipif_v6net_mask = v6mask;
10675 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10676 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10677 		    ipif->ipif_v6subnet);
10678 	}
10679 	err = ipif_up(ipif, q, mp);
10680 
10681 	if (err == 0 || err == EINPROGRESS) {
10682 		/*
10683 		 * The interface must be DL_BOUND if this packet has to
10684 		 * go out on the wire. Since we only go through a logical
10685 		 * down and are bound with the driver during an internal
10686 		 * down/up that is satisfied.
10687 		 */
10688 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10689 			/* Potentially broadcast an address mask reply. */
10690 			ipif_mask_reply(ipif);
10691 		}
10692 	}
10693 	return (err);
10694 }
10695 
10696 /* ARGSUSED */
10697 int
10698 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10699     ip_ioctl_cmd_t *ipip, void *if_req)
10700 {
10701 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10702 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10703 	(void) ipif_down_tail(ipif);
10704 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10705 }
10706 
10707 /* Get interface net mask. */
10708 /* ARGSUSED */
10709 int
10710 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10711     ip_ioctl_cmd_t *ipip, void *if_req)
10712 {
10713 	struct lifreq *lifr = (struct lifreq *)if_req;
10714 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10715 
10716 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10717 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10718 
10719 	/*
10720 	 * net mask can't change since we have a reference to the ipif.
10721 	 */
10722 	if (ipif->ipif_isv6) {
10723 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10724 		*sin6 = sin6_null;
10725 		sin6->sin6_family = AF_INET6;
10726 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10727 		lifr->lifr_addrlen =
10728 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10729 	} else {
10730 		*sin = sin_null;
10731 		sin->sin_family = AF_INET;
10732 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10733 		if (ipip->ipi_cmd_type == LIF_CMD) {
10734 			lifr->lifr_addrlen =
10735 			    ip_mask_to_plen(ipif->ipif_net_mask);
10736 		}
10737 	}
10738 	return (0);
10739 }
10740 
10741 /* ARGSUSED */
10742 int
10743 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10744     ip_ioctl_cmd_t *ipip, void *if_req)
10745 {
10746 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10747 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10748 
10749 	/*
10750 	 * Since no applications should ever be setting metrics on underlying
10751 	 * interfaces, we explicitly fail to smoke 'em out.
10752 	 */
10753 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10754 		return (EINVAL);
10755 
10756 	/*
10757 	 * Set interface metric.  We don't use this for
10758 	 * anything but we keep track of it in case it is
10759 	 * important to routing applications or such.
10760 	 */
10761 	if (ipip->ipi_cmd_type == IF_CMD) {
10762 		struct ifreq    *ifr;
10763 
10764 		ifr = (struct ifreq *)if_req;
10765 		ipif->ipif_metric = ifr->ifr_metric;
10766 	} else {
10767 		struct lifreq   *lifr;
10768 
10769 		lifr = (struct lifreq *)if_req;
10770 		ipif->ipif_metric = lifr->lifr_metric;
10771 	}
10772 	return (0);
10773 }
10774 
10775 /* ARGSUSED */
10776 int
10777 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10778     ip_ioctl_cmd_t *ipip, void *if_req)
10779 {
10780 	/* Get interface metric. */
10781 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10782 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10783 
10784 	if (ipip->ipi_cmd_type == IF_CMD) {
10785 		struct ifreq    *ifr;
10786 
10787 		ifr = (struct ifreq *)if_req;
10788 		ifr->ifr_metric = ipif->ipif_metric;
10789 	} else {
10790 		struct lifreq   *lifr;
10791 
10792 		lifr = (struct lifreq *)if_req;
10793 		lifr->lifr_metric = ipif->ipif_metric;
10794 	}
10795 
10796 	return (0);
10797 }
10798 
10799 /* ARGSUSED */
10800 int
10801 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10802     ip_ioctl_cmd_t *ipip, void *if_req)
10803 {
10804 	int	arp_muxid;
10805 
10806 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10807 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10808 	/*
10809 	 * Set the muxid returned from I_PLINK.
10810 	 */
10811 	if (ipip->ipi_cmd_type == IF_CMD) {
10812 		struct ifreq *ifr = (struct ifreq *)if_req;
10813 
10814 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10815 		arp_muxid = ifr->ifr_arp_muxid;
10816 	} else {
10817 		struct lifreq *lifr = (struct lifreq *)if_req;
10818 
10819 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10820 		arp_muxid = lifr->lifr_arp_muxid;
10821 	}
10822 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10823 	return (0);
10824 }
10825 
10826 /* ARGSUSED */
10827 int
10828 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10829     ip_ioctl_cmd_t *ipip, void *if_req)
10830 {
10831 	int	arp_muxid = 0;
10832 
10833 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10834 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10835 	/*
10836 	 * Get the muxid saved in ill for I_PUNLINK.
10837 	 */
10838 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10839 	if (ipip->ipi_cmd_type == IF_CMD) {
10840 		struct ifreq *ifr = (struct ifreq *)if_req;
10841 
10842 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10843 		ifr->ifr_arp_muxid = arp_muxid;
10844 	} else {
10845 		struct lifreq *lifr = (struct lifreq *)if_req;
10846 
10847 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10848 		lifr->lifr_arp_muxid = arp_muxid;
10849 	}
10850 	return (0);
10851 }
10852 
10853 /*
10854  * Set the subnet prefix. Does not modify the broadcast address.
10855  */
10856 /* ARGSUSED */
10857 int
10858 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10859     ip_ioctl_cmd_t *ipip, void *if_req)
10860 {
10861 	int err = 0;
10862 	in6_addr_t v6addr;
10863 	in6_addr_t v6mask;
10864 	boolean_t need_up = B_FALSE;
10865 	int addrlen;
10866 
10867 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10868 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10869 
10870 	ASSERT(IAM_WRITER_IPIF(ipif));
10871 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10872 
10873 	if (ipif->ipif_isv6) {
10874 		sin6_t *sin6;
10875 
10876 		if (sin->sin_family != AF_INET6)
10877 			return (EAFNOSUPPORT);
10878 
10879 		sin6 = (sin6_t *)sin;
10880 		v6addr = sin6->sin6_addr;
10881 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10882 			return (EADDRNOTAVAIL);
10883 	} else {
10884 		ipaddr_t addr;
10885 
10886 		if (sin->sin_family != AF_INET)
10887 			return (EAFNOSUPPORT);
10888 
10889 		addr = sin->sin_addr.s_addr;
10890 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10891 			return (EADDRNOTAVAIL);
10892 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10893 		/* Add 96 bits */
10894 		addrlen += IPV6_ABITS - IP_ABITS;
10895 	}
10896 
10897 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10898 		return (EINVAL);
10899 
10900 	/* Check if bits in the address is set past the mask */
10901 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10902 		return (EINVAL);
10903 
10904 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10905 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10906 		return (0);	/* No change */
10907 
10908 	if (ipif->ipif_flags & IPIF_UP) {
10909 		/*
10910 		 * If the interface is already marked up,
10911 		 * we call ipif_down which will take care
10912 		 * of ditching any IREs that have been set
10913 		 * up based on the old interface address.
10914 		 */
10915 		err = ipif_logical_down(ipif, q, mp);
10916 		if (err == EINPROGRESS)
10917 			return (err);
10918 		(void) ipif_down_tail(ipif);
10919 		need_up = B_TRUE;
10920 	}
10921 
10922 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10923 	return (err);
10924 }
10925 
10926 static int
10927 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10928     queue_t *q, mblk_t *mp, boolean_t need_up)
10929 {
10930 	ill_t	*ill = ipif->ipif_ill;
10931 	int	err = 0;
10932 
10933 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10934 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10935 
10936 	/* Set the new address. */
10937 	mutex_enter(&ill->ill_lock);
10938 	ipif->ipif_v6net_mask = v6mask;
10939 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10940 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10941 		    ipif->ipif_v6subnet);
10942 	}
10943 	mutex_exit(&ill->ill_lock);
10944 
10945 	if (need_up) {
10946 		/*
10947 		 * Now bring the interface back up.  If this
10948 		 * is the only IPIF for the ILL, ipif_up
10949 		 * will have to re-bind to the device, so
10950 		 * we may get back EINPROGRESS, in which
10951 		 * case, this IOCTL will get completed in
10952 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10953 		 */
10954 		err = ipif_up(ipif, q, mp);
10955 		if (err == EINPROGRESS)
10956 			return (err);
10957 	}
10958 	return (err);
10959 }
10960 
10961 /* ARGSUSED */
10962 int
10963 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10964     ip_ioctl_cmd_t *ipip, void *if_req)
10965 {
10966 	int	addrlen;
10967 	in6_addr_t v6addr;
10968 	in6_addr_t v6mask;
10969 	struct lifreq *lifr = (struct lifreq *)if_req;
10970 
10971 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
10972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10973 	(void) ipif_down_tail(ipif);
10974 
10975 	addrlen = lifr->lifr_addrlen;
10976 	if (ipif->ipif_isv6) {
10977 		sin6_t *sin6;
10978 
10979 		sin6 = (sin6_t *)sin;
10980 		v6addr = sin6->sin6_addr;
10981 	} else {
10982 		ipaddr_t addr;
10983 
10984 		addr = sin->sin_addr.s_addr;
10985 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10986 		addrlen += IPV6_ABITS - IP_ABITS;
10987 	}
10988 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
10989 
10990 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
10991 }
10992 
10993 /* ARGSUSED */
10994 int
10995 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10996     ip_ioctl_cmd_t *ipip, void *if_req)
10997 {
10998 	struct lifreq *lifr = (struct lifreq *)if_req;
10999 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11000 
11001 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11002 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11003 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11004 
11005 	if (ipif->ipif_isv6) {
11006 		*sin6 = sin6_null;
11007 		sin6->sin6_family = AF_INET6;
11008 		sin6->sin6_addr = ipif->ipif_v6subnet;
11009 		lifr->lifr_addrlen =
11010 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11011 	} else {
11012 		*sin = sin_null;
11013 		sin->sin_family = AF_INET;
11014 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11015 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11016 	}
11017 	return (0);
11018 }
11019 
11020 /*
11021  * Set the IPv6 address token.
11022  */
11023 /* ARGSUSED */
11024 int
11025 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11026     ip_ioctl_cmd_t *ipi, void *if_req)
11027 {
11028 	ill_t *ill = ipif->ipif_ill;
11029 	int err;
11030 	in6_addr_t v6addr;
11031 	in6_addr_t v6mask;
11032 	boolean_t need_up = B_FALSE;
11033 	int i;
11034 	sin6_t *sin6 = (sin6_t *)sin;
11035 	struct lifreq *lifr = (struct lifreq *)if_req;
11036 	int addrlen;
11037 
11038 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11039 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11040 	ASSERT(IAM_WRITER_IPIF(ipif));
11041 
11042 	addrlen = lifr->lifr_addrlen;
11043 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11044 	if (ipif->ipif_id != 0)
11045 		return (EINVAL);
11046 
11047 	if (!ipif->ipif_isv6)
11048 		return (EINVAL);
11049 
11050 	if (addrlen > IPV6_ABITS)
11051 		return (EINVAL);
11052 
11053 	v6addr = sin6->sin6_addr;
11054 
11055 	/*
11056 	 * The length of the token is the length from the end.  To get
11057 	 * the proper mask for this, compute the mask of the bits not
11058 	 * in the token; ie. the prefix, and then xor to get the mask.
11059 	 */
11060 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11061 		return (EINVAL);
11062 	for (i = 0; i < 4; i++) {
11063 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11064 	}
11065 
11066 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11067 	    ill->ill_token_length == addrlen)
11068 		return (0);	/* No change */
11069 
11070 	if (ipif->ipif_flags & IPIF_UP) {
11071 		err = ipif_logical_down(ipif, q, mp);
11072 		if (err == EINPROGRESS)
11073 			return (err);
11074 		(void) ipif_down_tail(ipif);
11075 		need_up = B_TRUE;
11076 	}
11077 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11078 	return (err);
11079 }
11080 
11081 static int
11082 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11083     mblk_t *mp, boolean_t need_up)
11084 {
11085 	in6_addr_t v6addr;
11086 	in6_addr_t v6mask;
11087 	ill_t	*ill = ipif->ipif_ill;
11088 	int	i;
11089 	int	err = 0;
11090 
11091 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11092 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11093 	v6addr = sin6->sin6_addr;
11094 	/*
11095 	 * The length of the token is the length from the end.  To get
11096 	 * the proper mask for this, compute the mask of the bits not
11097 	 * in the token; ie. the prefix, and then xor to get the mask.
11098 	 */
11099 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11100 	for (i = 0; i < 4; i++)
11101 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11102 
11103 	mutex_enter(&ill->ill_lock);
11104 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11105 	ill->ill_token_length = addrlen;
11106 	ill->ill_manual_token = 1;
11107 
11108 	/* Reconfigure the link-local address based on this new token */
11109 	ipif_setlinklocal(ill->ill_ipif);
11110 
11111 	mutex_exit(&ill->ill_lock);
11112 
11113 	if (need_up) {
11114 		/*
11115 		 * Now bring the interface back up.  If this
11116 		 * is the only IPIF for the ILL, ipif_up
11117 		 * will have to re-bind to the device, so
11118 		 * we may get back EINPROGRESS, in which
11119 		 * case, this IOCTL will get completed in
11120 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11121 		 */
11122 		err = ipif_up(ipif, q, mp);
11123 		if (err == EINPROGRESS)
11124 			return (err);
11125 	}
11126 	return (err);
11127 }
11128 
11129 /* ARGSUSED */
11130 int
11131 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11132     ip_ioctl_cmd_t *ipi, void *if_req)
11133 {
11134 	ill_t *ill;
11135 	sin6_t *sin6 = (sin6_t *)sin;
11136 	struct lifreq *lifr = (struct lifreq *)if_req;
11137 
11138 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11139 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11140 	if (ipif->ipif_id != 0)
11141 		return (EINVAL);
11142 
11143 	ill = ipif->ipif_ill;
11144 	if (!ill->ill_isv6)
11145 		return (ENXIO);
11146 
11147 	*sin6 = sin6_null;
11148 	sin6->sin6_family = AF_INET6;
11149 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11150 	sin6->sin6_addr = ill->ill_token;
11151 	lifr->lifr_addrlen = ill->ill_token_length;
11152 	return (0);
11153 }
11154 
11155 /*
11156  * Set (hardware) link specific information that might override
11157  * what was acquired through the DL_INFO_ACK.
11158  */
11159 /* ARGSUSED */
11160 int
11161 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11162     ip_ioctl_cmd_t *ipi, void *if_req)
11163 {
11164 	ill_t		*ill = ipif->ipif_ill;
11165 	int		ip_min_mtu;
11166 	struct lifreq	*lifr = (struct lifreq *)if_req;
11167 	lif_ifinfo_req_t *lir;
11168 
11169 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11170 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11171 	lir = &lifr->lifr_ifinfo;
11172 	ASSERT(IAM_WRITER_IPIF(ipif));
11173 
11174 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11175 	if (ipif->ipif_id != 0)
11176 		return (EINVAL);
11177 
11178 	/* Set interface MTU. */
11179 	if (ipif->ipif_isv6)
11180 		ip_min_mtu = IPV6_MIN_MTU;
11181 	else
11182 		ip_min_mtu = IP_MIN_MTU;
11183 
11184 	/*
11185 	 * Verify values before we set anything. Allow zero to
11186 	 * mean unspecified.
11187 	 *
11188 	 * XXX We should be able to set the user-defined lir_mtu to some value
11189 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11190 	 * ill_max_frag value tells us the max MTU that can be handled by the
11191 	 * datalink, whereas the ill_current_frag is dynamically computed for
11192 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11193 	 * since there is currently no way of distinguishing between
11194 	 * administratively fixed link mtu values (e.g., those set via
11195 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11196 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11197 	 * upper-bound.
11198 	 */
11199 	if (lir->lir_maxmtu != 0 &&
11200 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11201 	    lir->lir_maxmtu < ip_min_mtu))
11202 		return (EINVAL);
11203 	if (lir->lir_reachtime != 0 &&
11204 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11205 		return (EINVAL);
11206 	if (lir->lir_reachretrans != 0 &&
11207 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11208 		return (EINVAL);
11209 
11210 	mutex_enter(&ill->ill_lock);
11211 	/*
11212 	 * The dce and fragmentation code can handle changes to ill_mtu
11213 	 * concurrent with sending/fragmenting packets.
11214 	 */
11215 	if (lir->lir_maxmtu != 0)
11216 		ill->ill_user_mtu = lir->lir_maxmtu;
11217 
11218 	if (lir->lir_reachtime != 0)
11219 		ill->ill_reachable_time = lir->lir_reachtime;
11220 
11221 	if (lir->lir_reachretrans != 0)
11222 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11223 
11224 	ill->ill_max_hops = lir->lir_maxhops;
11225 	ill->ill_max_buf = ND_MAX_Q;
11226 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11227 		/*
11228 		 * ill_mtu is the actual interface MTU, obtained as the min
11229 		 * of user-configured mtu and the value announced by the
11230 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11231 		 * we have already made the choice of requiring
11232 		 * ill_user_mtu < ill_current_frag by the time we get here,
11233 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11234 		 * here.
11235 		 */
11236 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11237 	}
11238 	mutex_exit(&ill->ill_lock);
11239 
11240 	/*
11241 	 * Make sure all dce_generation checks find out
11242 	 * that ill_mtu has changed.
11243 	 */
11244 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11245 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11246 
11247 	/*
11248 	 * Refresh IPMP meta-interface MTU if necessary.
11249 	 */
11250 	if (IS_UNDER_IPMP(ill))
11251 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11252 
11253 	return (0);
11254 }
11255 
11256 /* ARGSUSED */
11257 int
11258 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11259     ip_ioctl_cmd_t *ipi, void *if_req)
11260 {
11261 	struct lif_ifinfo_req *lir;
11262 	ill_t *ill = ipif->ipif_ill;
11263 
11264 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11265 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11266 	if (ipif->ipif_id != 0)
11267 		return (EINVAL);
11268 
11269 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11270 	lir->lir_maxhops = ill->ill_max_hops;
11271 	lir->lir_reachtime = ill->ill_reachable_time;
11272 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11273 	lir->lir_maxmtu = ill->ill_mtu;
11274 
11275 	return (0);
11276 }
11277 
11278 /*
11279  * Return best guess as to the subnet mask for the specified address.
11280  * Based on the subnet masks for all the configured interfaces.
11281  *
11282  * We end up returning a zero mask in the case of default, multicast or
11283  * experimental.
11284  */
11285 static ipaddr_t
11286 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11287 {
11288 	ipaddr_t net_mask;
11289 	ill_t	*ill;
11290 	ipif_t	*ipif;
11291 	ill_walk_context_t ctx;
11292 	ipif_t	*fallback_ipif = NULL;
11293 
11294 	net_mask = ip_net_mask(addr);
11295 	if (net_mask == 0) {
11296 		*ipifp = NULL;
11297 		return (0);
11298 	}
11299 
11300 	/* Let's check to see if this is maybe a local subnet route. */
11301 	/* this function only applies to IPv4 interfaces */
11302 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11303 	ill = ILL_START_WALK_V4(&ctx, ipst);
11304 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11305 		mutex_enter(&ill->ill_lock);
11306 		for (ipif = ill->ill_ipif; ipif != NULL;
11307 		    ipif = ipif->ipif_next) {
11308 			if (IPIF_IS_CONDEMNED(ipif))
11309 				continue;
11310 			if (!(ipif->ipif_flags & IPIF_UP))
11311 				continue;
11312 			if ((ipif->ipif_subnet & net_mask) ==
11313 			    (addr & net_mask)) {
11314 				/*
11315 				 * Don't trust pt-pt interfaces if there are
11316 				 * other interfaces.
11317 				 */
11318 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11319 					if (fallback_ipif == NULL) {
11320 						ipif_refhold_locked(ipif);
11321 						fallback_ipif = ipif;
11322 					}
11323 					continue;
11324 				}
11325 
11326 				/*
11327 				 * Fine. Just assume the same net mask as the
11328 				 * directly attached subnet interface is using.
11329 				 */
11330 				ipif_refhold_locked(ipif);
11331 				mutex_exit(&ill->ill_lock);
11332 				rw_exit(&ipst->ips_ill_g_lock);
11333 				if (fallback_ipif != NULL)
11334 					ipif_refrele(fallback_ipif);
11335 				*ipifp = ipif;
11336 				return (ipif->ipif_net_mask);
11337 			}
11338 		}
11339 		mutex_exit(&ill->ill_lock);
11340 	}
11341 	rw_exit(&ipst->ips_ill_g_lock);
11342 
11343 	*ipifp = fallback_ipif;
11344 	return ((fallback_ipif != NULL) ?
11345 	    fallback_ipif->ipif_net_mask : net_mask);
11346 }
11347 
11348 /*
11349  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11350  */
11351 static void
11352 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11353 {
11354 	IOCP	iocp;
11355 	ipft_t	*ipft;
11356 	ipllc_t	*ipllc;
11357 	mblk_t	*mp1;
11358 	cred_t	*cr;
11359 	int	error = 0;
11360 	conn_t	*connp;
11361 
11362 	ip1dbg(("ip_wput_ioctl"));
11363 	iocp = (IOCP)mp->b_rptr;
11364 	mp1 = mp->b_cont;
11365 	if (mp1 == NULL) {
11366 		iocp->ioc_error = EINVAL;
11367 		mp->b_datap->db_type = M_IOCNAK;
11368 		iocp->ioc_count = 0;
11369 		qreply(q, mp);
11370 		return;
11371 	}
11372 
11373 	/*
11374 	 * These IOCTLs provide various control capabilities to
11375 	 * upstream agents such as ULPs and processes.	There
11376 	 * are currently two such IOCTLs implemented.  They
11377 	 * are used by TCP to provide update information for
11378 	 * existing IREs and to forcibly delete an IRE for a
11379 	 * host that is not responding, thereby forcing an
11380 	 * attempt at a new route.
11381 	 */
11382 	iocp->ioc_error = EINVAL;
11383 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11384 		goto done;
11385 
11386 	ipllc = (ipllc_t *)mp1->b_rptr;
11387 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11388 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11389 			break;
11390 	}
11391 	/*
11392 	 * prefer credential from mblk over ioctl;
11393 	 * see ip_sioctl_copyin_setup
11394 	 */
11395 	cr = msg_getcred(mp, NULL);
11396 	if (cr == NULL)
11397 		cr = iocp->ioc_cr;
11398 
11399 	/*
11400 	 * Refhold the conn in case the request gets queued up in some lookup
11401 	 */
11402 	ASSERT(CONN_Q(q));
11403 	connp = Q_TO_CONN(q);
11404 	CONN_INC_REF(connp);
11405 	if (ipft->ipft_pfi &&
11406 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11407 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11408 		error = (*ipft->ipft_pfi)(q,
11409 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11410 	}
11411 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11412 		/*
11413 		 * CONN_OPER_PENDING_DONE happens in the function called
11414 		 * through ipft_pfi above.
11415 		 */
11416 		return;
11417 	}
11418 
11419 	CONN_OPER_PENDING_DONE(connp);
11420 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11421 		freemsg(mp);
11422 		return;
11423 	}
11424 	iocp->ioc_error = error;
11425 
11426 done:
11427 	mp->b_datap->db_type = M_IOCACK;
11428 	if (iocp->ioc_error)
11429 		iocp->ioc_count = 0;
11430 	qreply(q, mp);
11431 }
11432 
11433 /*
11434  * Assign a unique id for the ipif. This is used by sctp_addr.c
11435  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11436  */
11437 static void
11438 ipif_assign_seqid(ipif_t *ipif)
11439 {
11440 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11441 
11442 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11443 }
11444 
11445 /*
11446  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11447  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11448  * that the clone is complete -- including the seqid -- and the expectation is
11449  * that the caller will either free or overwrite `sipif' before it's unlocked.
11450  */
11451 static void
11452 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11453 {
11454 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11455 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11456 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11457 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11458 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11459 
11460 	dipif->ipif_flags = sipif->ipif_flags;
11461 	dipif->ipif_metric = sipif->ipif_metric;
11462 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11463 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11464 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11465 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11466 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11467 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11468 
11469 	/*
11470 	 * As per the comment atop the function, we assume that these sipif
11471 	 * fields will be changed before sipif is unlocked.
11472 	 */
11473 	dipif->ipif_seqid = sipif->ipif_seqid;
11474 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11475 }
11476 
11477 /*
11478  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11479  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11480  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11481  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11482  * down (i.e., no DAD), of the same type, and unlocked.
11483  */
11484 static void
11485 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11486 {
11487 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11488 	ipxop_t *ipx = ipsq->ipsq_xop;
11489 
11490 	ASSERT(sipif != dipif);
11491 	ASSERT(sipif != virgipif);
11492 
11493 	/*
11494 	 * Grab all of the locks that protect the ipif in a defined order.
11495 	 */
11496 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11497 
11498 	ipif_clone(sipif, dipif);
11499 	if (virgipif != NULL) {
11500 		ipif_clone(virgipif, sipif);
11501 		mi_free(virgipif);
11502 	}
11503 
11504 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11505 
11506 	/*
11507 	 * Transfer ownership of the current xop, if necessary.
11508 	 */
11509 	if (ipx->ipx_current_ipif == sipif) {
11510 		ASSERT(ipx->ipx_pending_ipif == NULL);
11511 		mutex_enter(&ipx->ipx_lock);
11512 		ipx->ipx_current_ipif = dipif;
11513 		mutex_exit(&ipx->ipx_lock);
11514 	}
11515 
11516 	if (virgipif == NULL)
11517 		mi_free(sipif);
11518 }
11519 
11520 /*
11521  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11522  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11523  * be inserted into the first space available in the list. The value of
11524  * ipif_id will then be set to the appropriate value for its position.
11525  */
11526 static int
11527 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11528 {
11529 	ill_t *ill;
11530 	ipif_t *tipif;
11531 	ipif_t **tipifp;
11532 	int id;
11533 	ip_stack_t	*ipst;
11534 
11535 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11536 	    IAM_WRITER_IPIF(ipif));
11537 
11538 	ill = ipif->ipif_ill;
11539 	ASSERT(ill != NULL);
11540 	ipst = ill->ill_ipst;
11541 
11542 	/*
11543 	 * In the case of lo0:0 we already hold the ill_g_lock.
11544 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11545 	 * ipif_insert.
11546 	 */
11547 	if (acquire_g_lock)
11548 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11549 	mutex_enter(&ill->ill_lock);
11550 	id = ipif->ipif_id;
11551 	tipifp = &(ill->ill_ipif);
11552 	if (id == -1) {	/* need to find a real id */
11553 		id = 0;
11554 		while ((tipif = *tipifp) != NULL) {
11555 			ASSERT(tipif->ipif_id >= id);
11556 			if (tipif->ipif_id != id)
11557 				break; /* non-consecutive id */
11558 			id++;
11559 			tipifp = &(tipif->ipif_next);
11560 		}
11561 		/* limit number of logical interfaces */
11562 		if (id >= ipst->ips_ip_addrs_per_if) {
11563 			mutex_exit(&ill->ill_lock);
11564 			if (acquire_g_lock)
11565 				rw_exit(&ipst->ips_ill_g_lock);
11566 			return (-1);
11567 		}
11568 		ipif->ipif_id = id; /* assign new id */
11569 	} else if (id < ipst->ips_ip_addrs_per_if) {
11570 		/* we have a real id; insert ipif in the right place */
11571 		while ((tipif = *tipifp) != NULL) {
11572 			ASSERT(tipif->ipif_id != id);
11573 			if (tipif->ipif_id > id)
11574 				break; /* found correct location */
11575 			tipifp = &(tipif->ipif_next);
11576 		}
11577 	} else {
11578 		mutex_exit(&ill->ill_lock);
11579 		if (acquire_g_lock)
11580 			rw_exit(&ipst->ips_ill_g_lock);
11581 		return (-1);
11582 	}
11583 
11584 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11585 
11586 	ipif->ipif_next = tipif;
11587 	*tipifp = ipif;
11588 	mutex_exit(&ill->ill_lock);
11589 	if (acquire_g_lock)
11590 		rw_exit(&ipst->ips_ill_g_lock);
11591 
11592 	return (0);
11593 }
11594 
11595 static void
11596 ipif_remove(ipif_t *ipif)
11597 {
11598 	ipif_t	**ipifp;
11599 	ill_t	*ill = ipif->ipif_ill;
11600 
11601 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11602 
11603 	mutex_enter(&ill->ill_lock);
11604 	ipifp = &ill->ill_ipif;
11605 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11606 		if (*ipifp == ipif) {
11607 			*ipifp = ipif->ipif_next;
11608 			break;
11609 		}
11610 	}
11611 	mutex_exit(&ill->ill_lock);
11612 }
11613 
11614 /*
11615  * Allocate and initialize a new interface control structure.  (Always
11616  * called as writer.)
11617  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11618  * is not part of the global linked list of ills. ipif_seqid is unique
11619  * in the system and to preserve the uniqueness, it is assigned only
11620  * when ill becomes part of the global list. At that point ill will
11621  * have a name. If it doesn't get assigned here, it will get assigned
11622  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11623  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11624  * the interface flags or any other information from the DL_INFO_ACK for
11625  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11626  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11627  * second DL_INFO_ACK comes in from the driver.
11628  */
11629 static ipif_t *
11630 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11631     boolean_t insert)
11632 {
11633 	ipif_t	*ipif;
11634 	ip_stack_t *ipst = ill->ill_ipst;
11635 
11636 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11637 	    ill->ill_name, id, (void *)ill));
11638 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11639 
11640 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
11641 		return (NULL);
11642 	*ipif = ipif_zero;	/* start clean */
11643 
11644 	ipif->ipif_ill = ill;
11645 	ipif->ipif_id = id;	/* could be -1 */
11646 	/*
11647 	 * Inherit the zoneid from the ill; for the shared stack instance
11648 	 * this is always the global zone
11649 	 */
11650 	ipif->ipif_zoneid = ill->ill_zoneid;
11651 
11652 	ipif->ipif_refcnt = 0;
11653 
11654 	if (insert) {
11655 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
11656 			mi_free(ipif);
11657 			return (NULL);
11658 		}
11659 		/* -1 id should have been replaced by real id */
11660 		id = ipif->ipif_id;
11661 		ASSERT(id >= 0);
11662 	}
11663 
11664 	if (ill->ill_name[0] != '\0')
11665 		ipif_assign_seqid(ipif);
11666 
11667 	/*
11668 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11669 	 * (which must not exist yet because the zeroth ipif is created once
11670 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11671 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11672 	 */
11673 	if (id == 0 && IS_IPMP(ill)) {
11674 		if (ipmp_illgrp_create(ill) == NULL) {
11675 			if (insert) {
11676 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11677 				ipif_remove(ipif);
11678 				rw_exit(&ipst->ips_ill_g_lock);
11679 			}
11680 			mi_free(ipif);
11681 			return (NULL);
11682 		}
11683 	}
11684 
11685 	/*
11686 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11687 	 * not up and can't be looked up until the ioctl completes and the
11688 	 * IPIF_CHANGING flag is cleared.
11689 	 */
11690 	mutex_enter(&ill->ill_lock);
11691 
11692 	ipif->ipif_ire_type = ire_type;
11693 
11694 	if (ipif->ipif_isv6) {
11695 		ill->ill_flags |= ILLF_IPV6;
11696 	} else {
11697 		ipaddr_t inaddr_any = INADDR_ANY;
11698 
11699 		ill->ill_flags |= ILLF_IPV4;
11700 
11701 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11702 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11703 		    &ipif->ipif_v6lcl_addr);
11704 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11705 		    &ipif->ipif_v6subnet);
11706 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11707 		    &ipif->ipif_v6net_mask);
11708 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11709 		    &ipif->ipif_v6brd_addr);
11710 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11711 		    &ipif->ipif_v6pp_dst_addr);
11712 	}
11713 
11714 	/*
11715 	 * Don't set the interface flags etc. now, will do it in
11716 	 * ip_ll_subnet_defaults.
11717 	 */
11718 	if (!initialize)
11719 		goto out;
11720 
11721 	/*
11722 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11723 	 * with no underlying interfaces (and thus an unknown broadcast
11724 	 * address length), but all interfaces that can be placed into an IPMP
11725 	 * group are required to be broadcast-capable.
11726 	 */
11727 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11728 		/*
11729 		 * Later detect lack of DLPI driver multicast capability by
11730 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11731 		 */
11732 		ill->ill_flags |= ILLF_MULTICAST;
11733 		if (!ipif->ipif_isv6)
11734 			ipif->ipif_flags |= IPIF_BROADCAST;
11735 	} else {
11736 		if (ill->ill_net_type != IRE_LOOPBACK) {
11737 			if (ipif->ipif_isv6)
11738 				/*
11739 				 * Note: xresolv interfaces will eventually need
11740 				 * NOARP set here as well, but that will require
11741 				 * those external resolvers to have some
11742 				 * knowledge of that flag and act appropriately.
11743 				 * Not to be changed at present.
11744 				 */
11745 				ill->ill_flags |= ILLF_NONUD;
11746 			else
11747 				ill->ill_flags |= ILLF_NOARP;
11748 		}
11749 		if (ill->ill_phys_addr_length == 0) {
11750 			if (IS_VNI(ill)) {
11751 				ipif->ipif_flags |= IPIF_NOXMIT;
11752 			} else {
11753 				/* pt-pt supports multicast. */
11754 				ill->ill_flags |= ILLF_MULTICAST;
11755 				if (ill->ill_net_type != IRE_LOOPBACK)
11756 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11757 			}
11758 		}
11759 	}
11760 out:
11761 	mutex_exit(&ill->ill_lock);
11762 	return (ipif);
11763 }
11764 
11765 /*
11766  * Remove the neighbor cache entries associated with this logical
11767  * interface.
11768  */
11769 int
11770 ipif_arp_down(ipif_t *ipif)
11771 {
11772 	ill_t	*ill = ipif->ipif_ill;
11773 	int	err = 0;
11774 
11775 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11776 	ASSERT(IAM_WRITER_IPIF(ipif));
11777 
11778 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11779 	    ill_t *, ill, ipif_t *, ipif);
11780 	ipif_nce_down(ipif);
11781 
11782 	/*
11783 	 * If this is the last ipif that is going down and there are no
11784 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11785 	 * clean up ARP completely.
11786 	 */
11787 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11788 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11789 		/*
11790 		 * If this was the last ipif on an IPMP interface, purge any
11791 		 * static ARP entries associated with it.
11792 		 */
11793 		if (IS_IPMP(ill))
11794 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11795 
11796 		/* UNBIND, DETACH */
11797 		err = arp_ll_down(ill);
11798 	}
11799 
11800 	return (err);
11801 }
11802 
11803 /*
11804  * Get the resolver set up for a new IP address.  (Always called as writer.)
11805  * Called both for IPv4 and IPv6 interfaces, though it only does some
11806  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11807  *
11808  * The enumerated value res_act tunes the behavior:
11809  * 	* Res_act_initial: set up all the resolver structures for a new
11810  *	  IP address.
11811  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11812  *	  ARP message in defense of the address.
11813  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11814  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11815  *
11816  * Returns zero on success, or an errno upon failure.
11817  */
11818 int
11819 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11820 {
11821 	ill_t		*ill = ipif->ipif_ill;
11822 	int		err;
11823 	boolean_t	was_dup;
11824 
11825 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11826 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11827 	ASSERT(IAM_WRITER_IPIF(ipif));
11828 
11829 	was_dup = B_FALSE;
11830 	if (res_act == Res_act_initial) {
11831 		ipif->ipif_addr_ready = 0;
11832 		/*
11833 		 * We're bringing an interface up here.  There's no way that we
11834 		 * should need to shut down ARP now.
11835 		 */
11836 		mutex_enter(&ill->ill_lock);
11837 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11838 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11839 			ill->ill_ipif_dup_count--;
11840 			was_dup = B_TRUE;
11841 		}
11842 		mutex_exit(&ill->ill_lock);
11843 	}
11844 	if (ipif->ipif_recovery_id != 0)
11845 		(void) untimeout(ipif->ipif_recovery_id);
11846 	ipif->ipif_recovery_id = 0;
11847 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11848 		ipif->ipif_addr_ready = 1;
11849 		return (0);
11850 	}
11851 	/* NDP will set the ipif_addr_ready flag when it's ready */
11852 	if (ill->ill_isv6)
11853 		return (0);
11854 
11855 	err = ipif_arp_up(ipif, res_act, was_dup);
11856 	return (err);
11857 }
11858 
11859 /*
11860  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11861  * when a link has just gone back up.
11862  */
11863 static void
11864 ipif_nce_start_dad(ipif_t *ipif)
11865 {
11866 	ncec_t *ncec;
11867 	ill_t *ill = ipif->ipif_ill;
11868 	boolean_t isv6 = ill->ill_isv6;
11869 
11870 	if (isv6) {
11871 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11872 		    &ipif->ipif_v6lcl_addr);
11873 	} else {
11874 		ipaddr_t v4addr;
11875 
11876 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11877 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11878 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11879 			/*
11880 			 * If we can't contact ARP for some reason,
11881 			 * that's not really a problem.  Just send
11882 			 * out the routing socket notification that
11883 			 * DAD completion would have done, and continue.
11884 			 */
11885 			ipif_mask_reply(ipif);
11886 			ipif_up_notify(ipif);
11887 			ipif->ipif_addr_ready = 1;
11888 			return;
11889 		}
11890 
11891 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11892 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11893 	}
11894 
11895 	if (ncec == NULL) {
11896 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11897 		    (void *)ipif));
11898 		return;
11899 	}
11900 	if (!nce_restart_dad(ncec)) {
11901 		/*
11902 		 * If we can't restart DAD for some reason, that's not really a
11903 		 * problem.  Just send out the routing socket notification that
11904 		 * DAD completion would have done, and continue.
11905 		 */
11906 		ipif_up_notify(ipif);
11907 		ipif->ipif_addr_ready = 1;
11908 	}
11909 	ncec_refrele(ncec);
11910 }
11911 
11912 /*
11913  * Restart duplicate address detection on all interfaces on the given ill.
11914  *
11915  * This is called when an interface transitions from down to up
11916  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11917  *
11918  * Note that since the underlying physical link has transitioned, we must cause
11919  * at least one routing socket message to be sent here, either via DAD
11920  * completion or just by default on the first ipif.  (If we don't do this, then
11921  * in.mpathd will see long delays when doing link-based failure recovery.)
11922  */
11923 void
11924 ill_restart_dad(ill_t *ill, boolean_t went_up)
11925 {
11926 	ipif_t *ipif;
11927 
11928 	if (ill == NULL)
11929 		return;
11930 
11931 	/*
11932 	 * If layer two doesn't support duplicate address detection, then just
11933 	 * send the routing socket message now and be done with it.
11934 	 */
11935 	if (!ill->ill_isv6 && arp_no_defense) {
11936 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11937 		return;
11938 	}
11939 
11940 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11941 		if (went_up) {
11942 
11943 			if (ipif->ipif_flags & IPIF_UP) {
11944 				ipif_nce_start_dad(ipif);
11945 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
11946 				/*
11947 				 * kick off the bring-up process now.
11948 				 */
11949 				ipif_do_recovery(ipif);
11950 			} else {
11951 				/*
11952 				 * Unfortunately, the first ipif is "special"
11953 				 * and represents the underlying ill in the
11954 				 * routing socket messages.  Thus, when this
11955 				 * one ipif is down, we must still notify so
11956 				 * that the user knows the IFF_RUNNING status
11957 				 * change.  (If the first ipif is up, then
11958 				 * we'll handle eventual routing socket
11959 				 * notification via DAD completion.)
11960 				 */
11961 				if (ipif == ill->ill_ipif) {
11962 					ip_rts_ifmsg(ill->ill_ipif,
11963 					    RTSQ_DEFAULT);
11964 				}
11965 			}
11966 		} else {
11967 			/*
11968 			 * After link down, we'll need to send a new routing
11969 			 * message when the link comes back, so clear
11970 			 * ipif_addr_ready.
11971 			 */
11972 			ipif->ipif_addr_ready = 0;
11973 		}
11974 	}
11975 
11976 	/*
11977 	 * If we've torn down links, then notify the user right away.
11978 	 */
11979 	if (!went_up)
11980 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11981 }
11982 
11983 static void
11984 ipsq_delete(ipsq_t *ipsq)
11985 {
11986 	ipxop_t *ipx = ipsq->ipsq_xop;
11987 
11988 	ipsq->ipsq_ipst = NULL;
11989 	ASSERT(ipsq->ipsq_phyint == NULL);
11990 	ASSERT(ipsq->ipsq_xop != NULL);
11991 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
11992 	ASSERT(ipx->ipx_pending_mp == NULL);
11993 	kmem_free(ipsq, sizeof (ipsq_t));
11994 }
11995 
11996 static int
11997 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
11998 {
11999 	int err = 0;
12000 	ipif_t *ipif;
12001 
12002 	if (ill == NULL)
12003 		return (0);
12004 
12005 	ASSERT(IAM_WRITER_ILL(ill));
12006 	ill->ill_up_ipifs = B_TRUE;
12007 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12008 		if (ipif->ipif_was_up) {
12009 			if (!(ipif->ipif_flags & IPIF_UP))
12010 				err = ipif_up(ipif, q, mp);
12011 			ipif->ipif_was_up = B_FALSE;
12012 			if (err != 0) {
12013 				ASSERT(err == EINPROGRESS);
12014 				return (err);
12015 			}
12016 		}
12017 	}
12018 	ill->ill_up_ipifs = B_FALSE;
12019 	return (0);
12020 }
12021 
12022 /*
12023  * This function is called to bring up all the ipifs that were up before
12024  * bringing the ill down via ill_down_ipifs().
12025  */
12026 int
12027 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12028 {
12029 	int err;
12030 
12031 	ASSERT(IAM_WRITER_ILL(ill));
12032 
12033 	if (ill->ill_replumbing) {
12034 		ill->ill_replumbing = 0;
12035 		/*
12036 		 * Send down REPLUMB_DONE notification followed by the
12037 		 * BIND_REQ on the arp stream.
12038 		 */
12039 		if (!ill->ill_isv6)
12040 			arp_send_replumb_conf(ill);
12041 	}
12042 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12043 	if (err != 0)
12044 		return (err);
12045 
12046 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12047 }
12048 
12049 /*
12050  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12051  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12052  */
12053 static void
12054 ill_down_ipifs(ill_t *ill, boolean_t logical)
12055 {
12056 	ipif_t *ipif;
12057 
12058 	ASSERT(IAM_WRITER_ILL(ill));
12059 
12060 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12061 		/*
12062 		 * We go through the ipif_down logic even if the ipif
12063 		 * is already down, since routes can be added based
12064 		 * on down ipifs. Going through ipif_down once again
12065 		 * will delete any IREs created based on these routes.
12066 		 */
12067 		if (ipif->ipif_flags & IPIF_UP)
12068 			ipif->ipif_was_up = B_TRUE;
12069 
12070 		if (logical) {
12071 			(void) ipif_logical_down(ipif, NULL, NULL);
12072 			ipif_non_duplicate(ipif);
12073 			(void) ipif_down_tail(ipif);
12074 		} else {
12075 			(void) ipif_down(ipif, NULL, NULL);
12076 		}
12077 	}
12078 }
12079 
12080 /*
12081  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12082  * a look again at valid source addresses.
12083  * This should be called each time after the set of source addresses has been
12084  * changed.
12085  */
12086 void
12087 ip_update_source_selection(ip_stack_t *ipst)
12088 {
12089 	/* We skip past SRC_GENERATION_VERIFY */
12090 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12091 	    SRC_GENERATION_VERIFY)
12092 		atomic_add_32(&ipst->ips_src_generation, 1);
12093 }
12094 
12095 /*
12096  * Finish the group join started in ip_sioctl_groupname().
12097  */
12098 /* ARGSUSED */
12099 static void
12100 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12101 {
12102 	ill_t		*ill = q->q_ptr;
12103 	phyint_t	*phyi = ill->ill_phyint;
12104 	ipmp_grp_t	*grp = phyi->phyint_grp;
12105 	ip_stack_t	*ipst = ill->ill_ipst;
12106 
12107 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12108 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12109 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12110 
12111 	if (phyi->phyint_illv4 != NULL) {
12112 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12113 		VERIFY(grp->gr_pendv4-- > 0);
12114 		rw_exit(&ipst->ips_ipmp_lock);
12115 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12116 	}
12117 	if (phyi->phyint_illv6 != NULL) {
12118 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12119 		VERIFY(grp->gr_pendv6-- > 0);
12120 		rw_exit(&ipst->ips_ipmp_lock);
12121 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12122 	}
12123 	freemsg(mp);
12124 }
12125 
12126 /*
12127  * Process an SIOCSLIFGROUPNAME request.
12128  */
12129 /* ARGSUSED */
12130 int
12131 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12132     ip_ioctl_cmd_t *ipip, void *ifreq)
12133 {
12134 	struct lifreq	*lifr = ifreq;
12135 	ill_t		*ill = ipif->ipif_ill;
12136 	ip_stack_t	*ipst = ill->ill_ipst;
12137 	phyint_t	*phyi = ill->ill_phyint;
12138 	ipmp_grp_t	*grp = phyi->phyint_grp;
12139 	mblk_t		*ipsq_mp;
12140 	int		err = 0;
12141 
12142 	/*
12143 	 * Note that phyint_grp can only change here, where we're exclusive.
12144 	 */
12145 	ASSERT(IAM_WRITER_ILL(ill));
12146 
12147 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12148 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12149 		return (EINVAL);
12150 
12151 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12152 
12153 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12154 
12155 	/*
12156 	 * If the name hasn't changed, there's nothing to do.
12157 	 */
12158 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12159 		goto unlock;
12160 
12161 	/*
12162 	 * Handle requests to rename an IPMP meta-interface.
12163 	 *
12164 	 * Note that creation of the IPMP meta-interface is handled in
12165 	 * userland through the standard plumbing sequence.  As part of the
12166 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12167 	 * the name of the interface (see ipif_set_values_tail()).
12168 	 */
12169 	if (IS_IPMP(ill)) {
12170 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12171 		goto unlock;
12172 	}
12173 
12174 	/*
12175 	 * Handle requests to add or remove an IP interface from a group.
12176 	 */
12177 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12178 		/*
12179 		 * Moves are handled by first removing the interface from
12180 		 * its existing group, and then adding it to another group.
12181 		 * So, fail if it's already in a group.
12182 		 */
12183 		if (IS_UNDER_IPMP(ill)) {
12184 			err = EALREADY;
12185 			goto unlock;
12186 		}
12187 
12188 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12189 		if (grp == NULL) {
12190 			err = ENOENT;
12191 			goto unlock;
12192 		}
12193 
12194 		/*
12195 		 * Check if the phyint and its ills are suitable for
12196 		 * inclusion into the group.
12197 		 */
12198 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12199 			goto unlock;
12200 
12201 		/*
12202 		 * Checks pass; join the group, and enqueue the remaining
12203 		 * illgrp joins for when we've become part of the group xop
12204 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12205 		 * requires an mblk_t to scribble on, and since `mp' will be
12206 		 * freed as part of completing the ioctl, allocate another.
12207 		 */
12208 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12209 			err = ENOMEM;
12210 			goto unlock;
12211 		}
12212 
12213 		/*
12214 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12215 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12216 		 * before ip_join_illgrps() is called back.  See the comments
12217 		 * in ip_sioctl_plink_ipmp() for more.
12218 		 */
12219 		if (phyi->phyint_illv4 != NULL)
12220 			grp->gr_pendv4++;
12221 		if (phyi->phyint_illv6 != NULL)
12222 			grp->gr_pendv6++;
12223 
12224 		rw_exit(&ipst->ips_ipmp_lock);
12225 
12226 		ipmp_phyint_join_grp(phyi, grp);
12227 		ill_refhold(ill);
12228 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12229 		    SWITCH_OP, B_FALSE);
12230 		return (0);
12231 	} else {
12232 		/*
12233 		 * Request to remove the interface from a group.  If the
12234 		 * interface is not in a group, this trivially succeeds.
12235 		 */
12236 		rw_exit(&ipst->ips_ipmp_lock);
12237 		if (IS_UNDER_IPMP(ill))
12238 			ipmp_phyint_leave_grp(phyi);
12239 		return (0);
12240 	}
12241 unlock:
12242 	rw_exit(&ipst->ips_ipmp_lock);
12243 	return (err);
12244 }
12245 
12246 /*
12247  * Process an SIOCGLIFBINDING request.
12248  */
12249 /* ARGSUSED */
12250 int
12251 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12252     ip_ioctl_cmd_t *ipip, void *ifreq)
12253 {
12254 	ill_t		*ill;
12255 	struct lifreq	*lifr = ifreq;
12256 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12257 
12258 	if (!IS_IPMP(ipif->ipif_ill))
12259 		return (EINVAL);
12260 
12261 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12262 	if ((ill = ipif->ipif_bound_ill) == NULL)
12263 		lifr->lifr_binding[0] = '\0';
12264 	else
12265 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12266 	rw_exit(&ipst->ips_ipmp_lock);
12267 	return (0);
12268 }
12269 
12270 /*
12271  * Process an SIOCGLIFGROUPNAME request.
12272  */
12273 /* ARGSUSED */
12274 int
12275 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12276     ip_ioctl_cmd_t *ipip, void *ifreq)
12277 {
12278 	ipmp_grp_t	*grp;
12279 	struct lifreq	*lifr = ifreq;
12280 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12281 
12282 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12283 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12284 		lifr->lifr_groupname[0] = '\0';
12285 	else
12286 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12287 	rw_exit(&ipst->ips_ipmp_lock);
12288 	return (0);
12289 }
12290 
12291 /*
12292  * Process an SIOCGLIFGROUPINFO request.
12293  */
12294 /* ARGSUSED */
12295 int
12296 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12297     ip_ioctl_cmd_t *ipip, void *dummy)
12298 {
12299 	ipmp_grp_t	*grp;
12300 	lifgroupinfo_t	*lifgr;
12301 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12302 
12303 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12304 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12305 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12306 
12307 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12308 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12309 		rw_exit(&ipst->ips_ipmp_lock);
12310 		return (ENOENT);
12311 	}
12312 	ipmp_grp_info(grp, lifgr);
12313 	rw_exit(&ipst->ips_ipmp_lock);
12314 	return (0);
12315 }
12316 
12317 static void
12318 ill_dl_down(ill_t *ill)
12319 {
12320 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12321 
12322 	/*
12323 	 * The ill is down; unbind but stay attached since we're still
12324 	 * associated with a PPA. If we have negotiated DLPI capabilites
12325 	 * with the data link service provider (IDS_OK) then reset them.
12326 	 * The interval between unbinding and rebinding is potentially
12327 	 * unbounded hence we cannot assume things will be the same.
12328 	 * The DLPI capabilities will be probed again when the data link
12329 	 * is brought up.
12330 	 */
12331 	mblk_t	*mp = ill->ill_unbind_mp;
12332 
12333 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12334 
12335 	if (!ill->ill_replumbing) {
12336 		/* Free all ilms for this ill */
12337 		update_conn_ill(ill, ill->ill_ipst);
12338 	} else {
12339 		ill_leave_multicast(ill);
12340 	}
12341 
12342 	ill->ill_unbind_mp = NULL;
12343 	if (mp != NULL) {
12344 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12345 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12346 		    ill->ill_name));
12347 		mutex_enter(&ill->ill_lock);
12348 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12349 		mutex_exit(&ill->ill_lock);
12350 		/*
12351 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12352 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12353 		 * ill_capability_dld_disable disable rightaway. If this is not
12354 		 * an unplumb operation then the disable happens on receipt of
12355 		 * the capab ack via ip_rput_dlpi_writer ->
12356 		 * ill_capability_ack_thr. In both cases the order of
12357 		 * the operations seen by DLD is capability disable followed
12358 		 * by DL_UNBIND. Also the DLD capability disable needs a
12359 		 * cv_wait'able context.
12360 		 */
12361 		if (ill->ill_state_flags & ILL_CONDEMNED)
12362 			ill_capability_dld_disable(ill);
12363 		ill_capability_reset(ill, B_FALSE);
12364 		ill_dlpi_send(ill, mp);
12365 	}
12366 	mutex_enter(&ill->ill_lock);
12367 	ill->ill_dl_up = 0;
12368 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12369 	mutex_exit(&ill->ill_lock);
12370 }
12371 
12372 void
12373 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12374 {
12375 	union DL_primitives *dlp;
12376 	t_uscalar_t prim;
12377 	boolean_t waitack = B_FALSE;
12378 
12379 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12380 
12381 	dlp = (union DL_primitives *)mp->b_rptr;
12382 	prim = dlp->dl_primitive;
12383 
12384 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12385 	    dl_primstr(prim), prim, ill->ill_name));
12386 
12387 	switch (prim) {
12388 	case DL_PHYS_ADDR_REQ:
12389 	{
12390 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12391 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12392 		break;
12393 	}
12394 	case DL_BIND_REQ:
12395 		mutex_enter(&ill->ill_lock);
12396 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12397 		mutex_exit(&ill->ill_lock);
12398 		break;
12399 	}
12400 
12401 	/*
12402 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12403 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12404 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12405 	 */
12406 	mutex_enter(&ill->ill_lock);
12407 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12408 	    (prim == DL_UNBIND_REQ)) {
12409 		ill->ill_dlpi_pending = prim;
12410 		waitack = B_TRUE;
12411 	}
12412 
12413 	mutex_exit(&ill->ill_lock);
12414 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12415 	    char *, dl_primstr(prim), ill_t *, ill);
12416 	putnext(ill->ill_wq, mp);
12417 
12418 	/*
12419 	 * There is no ack for DL_NOTIFY_CONF messages
12420 	 */
12421 	if (waitack && prim == DL_NOTIFY_CONF)
12422 		ill_dlpi_done(ill, prim);
12423 }
12424 
12425 /*
12426  * Helper function for ill_dlpi_send().
12427  */
12428 /* ARGSUSED */
12429 static void
12430 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12431 {
12432 	ill_dlpi_send(q->q_ptr, mp);
12433 }
12434 
12435 /*
12436  * Send a DLPI control message to the driver but make sure there
12437  * is only one outstanding message. Uses ill_dlpi_pending to tell
12438  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12439  * when an ACK or a NAK is received to process the next queued message.
12440  */
12441 void
12442 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12443 {
12444 	mblk_t **mpp;
12445 
12446 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12447 
12448 	/*
12449 	 * To ensure that any DLPI requests for current exclusive operation
12450 	 * are always completely sent before any DLPI messages for other
12451 	 * operations, require writer access before enqueuing.
12452 	 */
12453 	if (!IAM_WRITER_ILL(ill)) {
12454 		ill_refhold(ill);
12455 		/* qwriter_ip() does the ill_refrele() */
12456 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12457 		    NEW_OP, B_TRUE);
12458 		return;
12459 	}
12460 
12461 	mutex_enter(&ill->ill_lock);
12462 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12463 		/* Must queue message. Tail insertion */
12464 		mpp = &ill->ill_dlpi_deferred;
12465 		while (*mpp != NULL)
12466 			mpp = &((*mpp)->b_next);
12467 
12468 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12469 		    "while %s pending\n", ill->ill_name,
12470 		    dl_primstr(ill->ill_dlpi_pending)));
12471 
12472 		*mpp = mp;
12473 		mutex_exit(&ill->ill_lock);
12474 		return;
12475 	}
12476 	mutex_exit(&ill->ill_lock);
12477 	ill_dlpi_dispatch(ill, mp);
12478 }
12479 
12480 void
12481 ill_capability_send(ill_t *ill, mblk_t *mp)
12482 {
12483 	ill->ill_capab_pending_cnt++;
12484 	ill_dlpi_send(ill, mp);
12485 }
12486 
12487 void
12488 ill_capability_done(ill_t *ill)
12489 {
12490 	ASSERT(ill->ill_capab_pending_cnt != 0);
12491 
12492 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12493 
12494 	ill->ill_capab_pending_cnt--;
12495 	if (ill->ill_capab_pending_cnt == 0 &&
12496 	    ill->ill_dlpi_capab_state == IDCS_OK)
12497 		ill_capability_reset_alloc(ill);
12498 }
12499 
12500 /*
12501  * Send all deferred DLPI messages without waiting for their ACKs.
12502  */
12503 void
12504 ill_dlpi_send_deferred(ill_t *ill)
12505 {
12506 	mblk_t *mp, *nextmp;
12507 
12508 	/*
12509 	 * Clear ill_dlpi_pending so that the message is not queued in
12510 	 * ill_dlpi_send().
12511 	 */
12512 	mutex_enter(&ill->ill_lock);
12513 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12514 	mp = ill->ill_dlpi_deferred;
12515 	ill->ill_dlpi_deferred = NULL;
12516 	mutex_exit(&ill->ill_lock);
12517 
12518 	for (; mp != NULL; mp = nextmp) {
12519 		nextmp = mp->b_next;
12520 		mp->b_next = NULL;
12521 		ill_dlpi_send(ill, mp);
12522 	}
12523 }
12524 
12525 /*
12526  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12527  */
12528 boolean_t
12529 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12530 {
12531 	t_uscalar_t pending;
12532 
12533 	mutex_enter(&ill->ill_lock);
12534 	if (ill->ill_dlpi_pending == prim) {
12535 		mutex_exit(&ill->ill_lock);
12536 		return (B_TRUE);
12537 	}
12538 
12539 	/*
12540 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12541 	 * without waiting, so don't print any warnings in that case.
12542 	 */
12543 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12544 		mutex_exit(&ill->ill_lock);
12545 		return (B_FALSE);
12546 	}
12547 	pending = ill->ill_dlpi_pending;
12548 	mutex_exit(&ill->ill_lock);
12549 
12550 	if (pending == DL_PRIM_INVAL) {
12551 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12552 		    "received unsolicited ack for %s on %s\n",
12553 		    dl_primstr(prim), ill->ill_name);
12554 	} else {
12555 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12556 		    "received unexpected ack for %s on %s (expecting %s)\n",
12557 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12558 	}
12559 	return (B_FALSE);
12560 }
12561 
12562 /*
12563  * Complete the current DLPI operation associated with `prim' on `ill' and
12564  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12565  * operations and the ill's current exclusive IPSQ operation has finished
12566  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12567  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12568  * the comments above ipsq_current_finish() for details.
12569  */
12570 void
12571 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12572 {
12573 	mblk_t *mp;
12574 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12575 	ipxop_t *ipx = ipsq->ipsq_xop;
12576 
12577 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12578 	mutex_enter(&ill->ill_lock);
12579 
12580 	ASSERT(prim != DL_PRIM_INVAL);
12581 	ASSERT(ill->ill_dlpi_pending == prim);
12582 
12583 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12584 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12585 
12586 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12587 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12588 		if (ipx->ipx_current_done) {
12589 			mutex_enter(&ipx->ipx_lock);
12590 			ipx->ipx_current_ipif = NULL;
12591 			mutex_exit(&ipx->ipx_lock);
12592 		}
12593 		cv_signal(&ill->ill_cv);
12594 		mutex_exit(&ill->ill_lock);
12595 		return;
12596 	}
12597 
12598 	ill->ill_dlpi_deferred = mp->b_next;
12599 	mp->b_next = NULL;
12600 	mutex_exit(&ill->ill_lock);
12601 
12602 	ill_dlpi_dispatch(ill, mp);
12603 }
12604 
12605 /*
12606  * Queue a (multicast) DLPI control message to be sent to the driver by
12607  * later calling ill_dlpi_send_queued.
12608  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12609  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12610  * for the same group to race.
12611  * We send DLPI control messages in order using ill_lock.
12612  * For IPMP we should be called on the cast_ill.
12613  */
12614 void
12615 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12616 {
12617 	mblk_t **mpp;
12618 
12619 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12620 
12621 	mutex_enter(&ill->ill_lock);
12622 	/* Must queue message. Tail insertion */
12623 	mpp = &ill->ill_dlpi_deferred;
12624 	while (*mpp != NULL)
12625 		mpp = &((*mpp)->b_next);
12626 
12627 	*mpp = mp;
12628 	mutex_exit(&ill->ill_lock);
12629 }
12630 
12631 /*
12632  * Send the messages that were queued. Make sure there is only
12633  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12634  * when an ACK or a NAK is received to process the next queued message.
12635  * For IPMP we are called on the upper ill, but when send what is queued
12636  * on the cast_ill.
12637  */
12638 void
12639 ill_dlpi_send_queued(ill_t *ill)
12640 {
12641 	mblk_t	*mp;
12642 	union DL_primitives *dlp;
12643 	t_uscalar_t prim;
12644 	ill_t *release_ill = NULL;
12645 
12646 	if (IS_IPMP(ill)) {
12647 		/* On the upper IPMP ill. */
12648 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12649 		if (release_ill == NULL) {
12650 			/* Avoid ever sending anything down to the ipmpstub */
12651 			return;
12652 		}
12653 		ill = release_ill;
12654 	}
12655 	mutex_enter(&ill->ill_lock);
12656 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12657 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12658 			/* Can't send. Somebody else will send it */
12659 			mutex_exit(&ill->ill_lock);
12660 			goto done;
12661 		}
12662 		ill->ill_dlpi_deferred = mp->b_next;
12663 		mp->b_next = NULL;
12664 		if (!ill->ill_dl_up) {
12665 			/*
12666 			 * Nobody there. All multicast addresses will be
12667 			 * re-joined when we get the DL_BIND_ACK bringing the
12668 			 * interface up.
12669 			 */
12670 			freemsg(mp);
12671 			continue;
12672 		}
12673 		dlp = (union DL_primitives *)mp->b_rptr;
12674 		prim = dlp->dl_primitive;
12675 
12676 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12677 		    (prim == DL_UNBIND_REQ)) {
12678 			ill->ill_dlpi_pending = prim;
12679 		}
12680 		mutex_exit(&ill->ill_lock);
12681 
12682 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12683 		    char *, dl_primstr(prim), ill_t *, ill);
12684 		putnext(ill->ill_wq, mp);
12685 		mutex_enter(&ill->ill_lock);
12686 	}
12687 	mutex_exit(&ill->ill_lock);
12688 done:
12689 	if (release_ill != NULL)
12690 		ill_refrele(release_ill);
12691 }
12692 
12693 /*
12694  * Queue an IP (IGMP/MLD) message to be sent by IP from
12695  * ill_mcast_send_queued
12696  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12697  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12698  * group to race.
12699  * We send them in order using ill_lock.
12700  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12701  */
12702 void
12703 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12704 {
12705 	mblk_t **mpp;
12706 	ill_t *release_ill = NULL;
12707 
12708 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12709 
12710 	if (IS_IPMP(ill)) {
12711 		/* On the upper IPMP ill. */
12712 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12713 		if (release_ill == NULL) {
12714 			/* Discard instead of queuing for the ipmp interface */
12715 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12716 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12717 			    mp, ill);
12718 			freemsg(mp);
12719 			return;
12720 		}
12721 		ill = release_ill;
12722 	}
12723 
12724 	mutex_enter(&ill->ill_lock);
12725 	/* Must queue message. Tail insertion */
12726 	mpp = &ill->ill_mcast_deferred;
12727 	while (*mpp != NULL)
12728 		mpp = &((*mpp)->b_next);
12729 
12730 	*mpp = mp;
12731 	mutex_exit(&ill->ill_lock);
12732 	if (release_ill != NULL)
12733 		ill_refrele(release_ill);
12734 }
12735 
12736 /*
12737  * Send the IP packets that were queued by ill_mcast_queue.
12738  * These are IGMP/MLD packets.
12739  *
12740  * For IPMP we are called on the upper ill, but when send what is queued
12741  * on the cast_ill.
12742  *
12743  * Request loopback of the report if we are acting as a multicast
12744  * router, so that the process-level routing demon can hear it.
12745  * This will run multiple times for the same group if there are members
12746  * on the same group for multiple ipif's on the same ill. The
12747  * igmp_input/mld_input code will suppress this due to the loopback thus we
12748  * always loopback membership report.
12749  *
12750  * We also need to make sure that this does not get load balanced
12751  * by IPMP. We do this by passing an ill to ip_output_simple.
12752  */
12753 void
12754 ill_mcast_send_queued(ill_t *ill)
12755 {
12756 	mblk_t	*mp;
12757 	ip_xmit_attr_t ixas;
12758 	ill_t *release_ill = NULL;
12759 
12760 	if (IS_IPMP(ill)) {
12761 		/* On the upper IPMP ill. */
12762 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12763 		if (release_ill == NULL) {
12764 			/*
12765 			 * We should have no messages on the ipmp interface
12766 			 * but no point in trying to send them.
12767 			 */
12768 			return;
12769 		}
12770 		ill = release_ill;
12771 	}
12772 	bzero(&ixas, sizeof (ixas));
12773 	ixas.ixa_zoneid = ALL_ZONES;
12774 	ixas.ixa_cred = kcred;
12775 	ixas.ixa_cpid = NOPID;
12776 	ixas.ixa_tsl = NULL;
12777 	/*
12778 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12779 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12780 	 * That is necessary to handle IGMP/MLD snooping switches.
12781 	 */
12782 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12783 	ixas.ixa_ipst = ill->ill_ipst;
12784 
12785 	mutex_enter(&ill->ill_lock);
12786 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12787 		ill->ill_mcast_deferred = mp->b_next;
12788 		mp->b_next = NULL;
12789 		if (!ill->ill_dl_up) {
12790 			/*
12791 			 * Nobody there. Just drop the ip packets.
12792 			 * IGMP/MLD will resend later, if this is a replumb.
12793 			 */
12794 			freemsg(mp);
12795 			continue;
12796 		}
12797 		mutex_enter(&ill->ill_phyint->phyint_lock);
12798 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12799 			/*
12800 			 * When the ill is getting deactivated, we only want to
12801 			 * send the DLPI messages, so drop IGMP/MLD packets.
12802 			 * DLPI messages are handled by ill_dlpi_send_queued()
12803 			 */
12804 			mutex_exit(&ill->ill_phyint->phyint_lock);
12805 			freemsg(mp);
12806 			continue;
12807 		}
12808 		mutex_exit(&ill->ill_phyint->phyint_lock);
12809 		mutex_exit(&ill->ill_lock);
12810 
12811 		/* Check whether we are sending IPv4 or IPv6. */
12812 		if (ill->ill_isv6) {
12813 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12814 
12815 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12816 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12817 		} else {
12818 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12819 
12820 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12821 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12822 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12823 		}
12824 
12825 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12826 		(void) ip_output_simple(mp, &ixas);
12827 		ixa_cleanup(&ixas);
12828 
12829 		mutex_enter(&ill->ill_lock);
12830 	}
12831 	mutex_exit(&ill->ill_lock);
12832 
12833 done:
12834 	if (release_ill != NULL)
12835 		ill_refrele(release_ill);
12836 }
12837 
12838 /*
12839  * Take down a specific interface, but don't lose any information about it.
12840  * (Always called as writer.)
12841  * This function goes through the down sequence even if the interface is
12842  * already down. There are 2 reasons.
12843  * a. Currently we permit interface routes that depend on down interfaces
12844  *    to be added. This behaviour itself is questionable. However it appears
12845  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12846  *    time. We go thru the cleanup in order to remove these routes.
12847  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12848  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12849  *    down, but we need to cleanup i.e. do ill_dl_down and
12850  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12851  *
12852  * IP-MT notes:
12853  *
12854  * Model of reference to interfaces.
12855  *
12856  * The following members in ipif_t track references to the ipif.
12857  *	int     ipif_refcnt;    Active reference count
12858  *
12859  * The following members in ill_t track references to the ill.
12860  *	int             ill_refcnt;     active refcnt
12861  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12862  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12863  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12864  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12865  *
12866  * Reference to an ipif or ill can be obtained in any of the following ways.
12867  *
12868  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12869  * Pointers to ipif / ill from other data structures viz ire and conn.
12870  * Implicit reference to the ipif / ill by holding a reference to the ire.
12871  *
12872  * The ipif/ill lookup functions return a reference held ipif / ill.
12873  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12874  * This is a purely dynamic reference count associated with threads holding
12875  * references to the ipif / ill. Pointers from other structures do not
12876  * count towards this reference count.
12877  *
12878  * ill_ire_cnt is the number of ire's associated with the
12879  * ill. This is incremented whenever a new ire is created referencing the
12880  * ill. This is done atomically inside ire_add_v[46] where the ire is
12881  * actually added to the ire hash table. The count is decremented in
12882  * ire_inactive where the ire is destroyed.
12883  *
12884  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12885  * This is incremented atomically in
12886  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12887  * table. Similarly it is decremented in ncec_inactive() where the ncec
12888  * is destroyed.
12889  *
12890  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12891  * incremented atomically in nce_add() where the nce is actually added to the
12892  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12893  * is destroyed.
12894  *
12895  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12896  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12897  *
12898  * Flow of ioctls involving interface down/up
12899  *
12900  * The following is the sequence of an attempt to set some critical flags on an
12901  * up interface.
12902  * ip_sioctl_flags
12903  * ipif_down
12904  * wait for ipif to be quiescent
12905  * ipif_down_tail
12906  * ip_sioctl_flags_tail
12907  *
12908  * All set ioctls that involve down/up sequence would have a skeleton similar
12909  * to the above. All the *tail functions are called after the refcounts have
12910  * dropped to the appropriate values.
12911  *
12912  * SIOC ioctls during the IPIF_CHANGING interval.
12913  *
12914  * Threads handling SIOC set ioctls serialize on the squeue, but this
12915  * is not done for SIOC get ioctls. Since a set ioctl can cause several
12916  * steps of internal changes to the state, some of which are visible in
12917  * ipif_flags (such as IFF_UP being cleared and later set), and we want
12918  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
12919  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
12920  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
12921  * the current exclusive operation completes. The IPIF_CHANGING check
12922  * and enqueue is atomic using the ill_lock and ipsq_lock. The
12923  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
12924  * change while the ill_lock is held. Before dropping the ill_lock we acquire
12925  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
12926  * until we release the ipsq_lock, even though the ill/ipif state flags
12927  * can change after we drop the ill_lock.
12928  */
12929 int
12930 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12931 {
12932 	ill_t		*ill = ipif->ipif_ill;
12933 	conn_t		*connp;
12934 	boolean_t	success;
12935 	boolean_t	ipif_was_up = B_FALSE;
12936 	ip_stack_t	*ipst = ill->ill_ipst;
12937 
12938 	ASSERT(IAM_WRITER_IPIF(ipif));
12939 
12940 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12941 
12942 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
12943 	    ill_t *, ill, ipif_t *, ipif);
12944 
12945 	if (ipif->ipif_flags & IPIF_UP) {
12946 		mutex_enter(&ill->ill_lock);
12947 		ipif->ipif_flags &= ~IPIF_UP;
12948 		ASSERT(ill->ill_ipif_up_count > 0);
12949 		--ill->ill_ipif_up_count;
12950 		mutex_exit(&ill->ill_lock);
12951 		ipif_was_up = B_TRUE;
12952 		/* Update status in SCTP's list */
12953 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
12954 		ill_nic_event_dispatch(ipif->ipif_ill,
12955 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
12956 	}
12957 
12958 	/*
12959 	 * Blow away memberships we established in ipif_multicast_up().
12960 	 */
12961 	ipif_multicast_down(ipif);
12962 
12963 	/*
12964 	 * Remove from the mapping for __sin6_src_id. We insert only
12965 	 * when the address is not INADDR_ANY. As IPv4 addresses are
12966 	 * stored as mapped addresses, we need to check for mapped
12967 	 * INADDR_ANY also.
12968 	 */
12969 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12970 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
12971 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
12972 		int err;
12973 
12974 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
12975 		    ipif->ipif_zoneid, ipst);
12976 		if (err != 0) {
12977 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
12978 		}
12979 	}
12980 
12981 	if (ipif_was_up) {
12982 		/* only delete if we'd added ire's before */
12983 		if (ipif->ipif_isv6)
12984 			ipif_delete_ires_v6(ipif);
12985 		else
12986 			ipif_delete_ires_v4(ipif);
12987 	}
12988 
12989 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
12990 		/*
12991 		 * Since the interface is now down, it may have just become
12992 		 * inactive.  Note that this needs to be done even for a
12993 		 * lll_logical_down(), or ARP entries will not get correctly
12994 		 * restored when the interface comes back up.
12995 		 */
12996 		if (IS_UNDER_IPMP(ill))
12997 			ipmp_ill_refresh_active(ill);
12998 	}
12999 
13000 	/*
13001 	 * neighbor-discovery or arp entries for this interface. The ipif
13002 	 * has to be quiesced, so we walk all the nce's and delete those
13003 	 * that point at the ipif->ipif_ill. At the same time, we also
13004 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13005 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13006 	 * that for ipif_down_tail()
13007 	 */
13008 	ipif_nce_down(ipif);
13009 
13010 	/*
13011 	 * If this is the last ipif on the ill, we also need to remove
13012 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13013 	 * never succeed.
13014 	 */
13015 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13016 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13017 
13018 	/*
13019 	 * Walk all CONNs that can have a reference on an ire for this
13020 	 * ipif (we actually walk all that now have stale references).
13021 	 */
13022 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13023 
13024 	/*
13025 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13026 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13027 	 * and ill_delete -> ipif_free -> ipif_down
13028 	 */
13029 	if (mp == NULL) {
13030 		ASSERT(q == NULL);
13031 		return (0);
13032 	}
13033 
13034 	if (CONN_Q(q)) {
13035 		connp = Q_TO_CONN(q);
13036 		mutex_enter(&connp->conn_lock);
13037 	} else {
13038 		connp = NULL;
13039 	}
13040 	mutex_enter(&ill->ill_lock);
13041 	/*
13042 	 * Are there any ire's pointing to this ipif that are still active ?
13043 	 * If this is the last ipif going down, are there any ire's pointing
13044 	 * to this ill that are still active ?
13045 	 */
13046 	if (ipif_is_quiescent(ipif)) {
13047 		mutex_exit(&ill->ill_lock);
13048 		if (connp != NULL)
13049 			mutex_exit(&connp->conn_lock);
13050 		return (0);
13051 	}
13052 
13053 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13054 	    ill->ill_name, (void *)ill));
13055 	/*
13056 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13057 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13058 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13059 	 */
13060 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13061 	if (!success) {
13062 		/* The conn is closing. So just return */
13063 		ASSERT(connp != NULL);
13064 		mutex_exit(&ill->ill_lock);
13065 		mutex_exit(&connp->conn_lock);
13066 		return (EINTR);
13067 	}
13068 
13069 	mutex_exit(&ill->ill_lock);
13070 	if (connp != NULL)
13071 		mutex_exit(&connp->conn_lock);
13072 	return (EINPROGRESS);
13073 }
13074 
13075 int
13076 ipif_down_tail(ipif_t *ipif)
13077 {
13078 	ill_t	*ill = ipif->ipif_ill;
13079 	int	err = 0;
13080 
13081 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13082 	    ill_t *, ill, ipif_t *, ipif);
13083 
13084 	/*
13085 	 * Skip any loopback interface (null wq).
13086 	 * If this is the last logical interface on the ill
13087 	 * have ill_dl_down tell the driver we are gone (unbind)
13088 	 * Note that lun 0 can ipif_down even though
13089 	 * there are other logical units that are up.
13090 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13091 	 */
13092 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13093 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13094 	    ill->ill_dl_up) {
13095 		ill_dl_down(ill);
13096 	}
13097 	if (!ipif->ipif_isv6)
13098 		err = ipif_arp_down(ipif);
13099 
13100 	ill->ill_logical_down = 0;
13101 
13102 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13103 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13104 	return (err);
13105 }
13106 
13107 /*
13108  * Bring interface logically down without bringing the physical interface
13109  * down e.g. when the netmask is changed. This avoids long lasting link
13110  * negotiations between an ethernet interface and a certain switches.
13111  */
13112 static int
13113 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13114 {
13115 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13116 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13117 
13118 	/*
13119 	 * The ill_logical_down flag is a transient flag. It is set here
13120 	 * and is cleared once the down has completed in ipif_down_tail.
13121 	 * This flag does not indicate whether the ill stream is in the
13122 	 * DL_BOUND state with the driver. Instead this flag is used by
13123 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13124 	 * the driver. The state of the ill stream i.e. whether it is
13125 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13126 	 */
13127 	ipif->ipif_ill->ill_logical_down = 1;
13128 	return (ipif_down(ipif, q, mp));
13129 }
13130 
13131 /*
13132  * Initiate deallocate of an IPIF. Always called as writer. Called by
13133  * ill_delete or ip_sioctl_removeif.
13134  */
13135 static void
13136 ipif_free(ipif_t *ipif)
13137 {
13138 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13139 
13140 	ASSERT(IAM_WRITER_IPIF(ipif));
13141 
13142 	if (ipif->ipif_recovery_id != 0)
13143 		(void) untimeout(ipif->ipif_recovery_id);
13144 	ipif->ipif_recovery_id = 0;
13145 
13146 	/*
13147 	 * Take down the interface. We can be called either from ill_delete
13148 	 * or from ip_sioctl_removeif.
13149 	 */
13150 	(void) ipif_down(ipif, NULL, NULL);
13151 
13152 	/*
13153 	 * Now that the interface is down, there's no chance it can still
13154 	 * become a duplicate.  Cancel any timer that may have been set while
13155 	 * tearing down.
13156 	 */
13157 	if (ipif->ipif_recovery_id != 0)
13158 		(void) untimeout(ipif->ipif_recovery_id);
13159 	ipif->ipif_recovery_id = 0;
13160 
13161 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13162 	/* Remove pointers to this ill in the multicast routing tables */
13163 	reset_mrt_vif_ipif(ipif);
13164 	/* If necessary, clear the cached source ipif rotor. */
13165 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13166 		ipif->ipif_ill->ill_src_ipif = NULL;
13167 	rw_exit(&ipst->ips_ill_g_lock);
13168 }
13169 
13170 static void
13171 ipif_free_tail(ipif_t *ipif)
13172 {
13173 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13174 
13175 	/*
13176 	 * Need to hold both ill_g_lock and ill_lock while
13177 	 * inserting or removing an ipif from the linked list
13178 	 * of ipifs hanging off the ill.
13179 	 */
13180 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13181 
13182 #ifdef DEBUG
13183 	ipif_trace_cleanup(ipif);
13184 #endif
13185 
13186 	/* Ask SCTP to take it out of it list */
13187 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13188 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13189 
13190 	/* Get it out of the ILL interface list. */
13191 	ipif_remove(ipif);
13192 	rw_exit(&ipst->ips_ill_g_lock);
13193 
13194 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13195 	ASSERT(ipif->ipif_recovery_id == 0);
13196 	ASSERT(ipif->ipif_ire_local == NULL);
13197 	ASSERT(ipif->ipif_ire_if == NULL);
13198 
13199 	/* Free the memory. */
13200 	mi_free(ipif);
13201 }
13202 
13203 /*
13204  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13205  * is zero.
13206  */
13207 void
13208 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13209 {
13210 	char	lbuf[LIFNAMSIZ];
13211 	char	*name;
13212 	size_t	name_len;
13213 
13214 	buf[0] = '\0';
13215 	name = ipif->ipif_ill->ill_name;
13216 	name_len = ipif->ipif_ill->ill_name_length;
13217 	if (ipif->ipif_id != 0) {
13218 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13219 		    ipif->ipif_id);
13220 		name = lbuf;
13221 		name_len = mi_strlen(name) + 1;
13222 	}
13223 	len -= 1;
13224 	buf[len] = '\0';
13225 	len = MIN(len, name_len);
13226 	bcopy(name, buf, len);
13227 }
13228 
13229 /*
13230  * Sets `buf' to an ill name.
13231  */
13232 void
13233 ill_get_name(const ill_t *ill, char *buf, int len)
13234 {
13235 	char	*name;
13236 	size_t	name_len;
13237 
13238 	name = ill->ill_name;
13239 	name_len = ill->ill_name_length;
13240 	len -= 1;
13241 	buf[len] = '\0';
13242 	len = MIN(len, name_len);
13243 	bcopy(name, buf, len);
13244 }
13245 
13246 /*
13247  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13248  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13249  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13250  * (May be called as writer.)
13251  */
13252 static ipif_t *
13253 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13254     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13255 {
13256 	char	*cp;
13257 	char	*endp;
13258 	long	id;
13259 	ill_t	*ill;
13260 	ipif_t	*ipif;
13261 	uint_t	ire_type;
13262 	boolean_t did_alloc = B_FALSE;
13263 
13264 	/*
13265 	 * If the caller wants to us to create the ipif, make sure we have a
13266 	 * valid zoneid
13267 	 */
13268 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13269 
13270 	if (namelen == 0) {
13271 		return (NULL);
13272 	}
13273 
13274 	*exists = B_FALSE;
13275 	/* Look for a colon in the name. */
13276 	endp = &name[namelen];
13277 	for (cp = endp; --cp > name; ) {
13278 		if (*cp == IPIF_SEPARATOR_CHAR)
13279 			break;
13280 	}
13281 
13282 	if (*cp == IPIF_SEPARATOR_CHAR) {
13283 		/*
13284 		 * Reject any non-decimal aliases for logical
13285 		 * interfaces. Aliases with leading zeroes
13286 		 * are also rejected as they introduce ambiguity
13287 		 * in the naming of the interfaces.
13288 		 * In order to confirm with existing semantics,
13289 		 * and to not break any programs/script relying
13290 		 * on that behaviour, if<0>:0 is considered to be
13291 		 * a valid interface.
13292 		 *
13293 		 * If alias has two or more digits and the first
13294 		 * is zero, fail.
13295 		 */
13296 		if (&cp[2] < endp && cp[1] == '0') {
13297 			return (NULL);
13298 		}
13299 	}
13300 
13301 	if (cp <= name) {
13302 		cp = endp;
13303 	} else {
13304 		*cp = '\0';
13305 	}
13306 
13307 	/*
13308 	 * Look up the ILL, based on the portion of the name
13309 	 * before the slash. ill_lookup_on_name returns a held ill.
13310 	 * Temporary to check whether ill exists already. If so
13311 	 * ill_lookup_on_name will clear it.
13312 	 */
13313 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13314 	    &did_alloc, ipst);
13315 	if (cp != endp)
13316 		*cp = IPIF_SEPARATOR_CHAR;
13317 	if (ill == NULL)
13318 		return (NULL);
13319 
13320 	/* Establish the unit number in the name. */
13321 	id = 0;
13322 	if (cp < endp && *endp == '\0') {
13323 		/* If there was a colon, the unit number follows. */
13324 		cp++;
13325 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13326 			ill_refrele(ill);
13327 			return (NULL);
13328 		}
13329 	}
13330 
13331 	mutex_enter(&ill->ill_lock);
13332 	/* Now see if there is an IPIF with this unit number. */
13333 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13334 		if (ipif->ipif_id == id) {
13335 			if (zoneid != ALL_ZONES &&
13336 			    zoneid != ipif->ipif_zoneid &&
13337 			    ipif->ipif_zoneid != ALL_ZONES) {
13338 				mutex_exit(&ill->ill_lock);
13339 				ill_refrele(ill);
13340 				return (NULL);
13341 			}
13342 			if (IPIF_CAN_LOOKUP(ipif)) {
13343 				ipif_refhold_locked(ipif);
13344 				mutex_exit(&ill->ill_lock);
13345 				if (!did_alloc)
13346 					*exists = B_TRUE;
13347 				/*
13348 				 * Drop locks before calling ill_refrele
13349 				 * since it can potentially call into
13350 				 * ipif_ill_refrele_tail which can end up
13351 				 * in trying to acquire any lock.
13352 				 */
13353 				ill_refrele(ill);
13354 				return (ipif);
13355 			}
13356 		}
13357 	}
13358 
13359 	if (!do_alloc) {
13360 		mutex_exit(&ill->ill_lock);
13361 		ill_refrele(ill);
13362 		return (NULL);
13363 	}
13364 
13365 	/*
13366 	 * If none found, atomically allocate and return a new one.
13367 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13368 	 * to support "receive only" use of lo0:1 etc. as is still done
13369 	 * below as an initial guess.
13370 	 * However, this is now likely to be overriden later in ipif_up_done()
13371 	 * when we know for sure what address has been configured on the
13372 	 * interface, since we might have more than one loopback interface
13373 	 * with a loopback address, e.g. in the case of zones, and all the
13374 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13375 	 */
13376 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13377 		ire_type = IRE_LOOPBACK;
13378 	else
13379 		ire_type = IRE_LOCAL;
13380 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
13381 	if (ipif != NULL)
13382 		ipif_refhold_locked(ipif);
13383 	mutex_exit(&ill->ill_lock);
13384 	ill_refrele(ill);
13385 	return (ipif);
13386 }
13387 
13388 /*
13389  * This routine is called whenever a new address comes up on an ipif.  If
13390  * we are configured to respond to address mask requests, then we are supposed
13391  * to broadcast an address mask reply at this time.  This routine is also
13392  * called if we are already up, but a netmask change is made.  This is legal
13393  * but might not make the system manager very popular.	(May be called
13394  * as writer.)
13395  */
13396 void
13397 ipif_mask_reply(ipif_t *ipif)
13398 {
13399 	icmph_t	*icmph;
13400 	ipha_t	*ipha;
13401 	mblk_t	*mp;
13402 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13403 	ip_xmit_attr_t ixas;
13404 
13405 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13406 
13407 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13408 		return;
13409 
13410 	/* ICMP mask reply is IPv4 only */
13411 	ASSERT(!ipif->ipif_isv6);
13412 	/* ICMP mask reply is not for a loopback interface */
13413 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13414 
13415 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13416 		return;
13417 
13418 	mp = allocb(REPLY_LEN, BPRI_HI);
13419 	if (mp == NULL)
13420 		return;
13421 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13422 
13423 	ipha = (ipha_t *)mp->b_rptr;
13424 	bzero(ipha, REPLY_LEN);
13425 	*ipha = icmp_ipha;
13426 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13427 	ipha->ipha_src = ipif->ipif_lcl_addr;
13428 	ipha->ipha_dst = ipif->ipif_brd_addr;
13429 	ipha->ipha_length = htons(REPLY_LEN);
13430 	ipha->ipha_ident = 0;
13431 
13432 	icmph = (icmph_t *)&ipha[1];
13433 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13434 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13435 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13436 
13437 	bzero(&ixas, sizeof (ixas));
13438 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13439 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13440 	ixas.ixa_zoneid = ALL_ZONES;
13441 	ixas.ixa_ifindex = 0;
13442 	ixas.ixa_ipst = ipst;
13443 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13444 	(void) ip_output_simple(mp, &ixas);
13445 	ixa_cleanup(&ixas);
13446 #undef	REPLY_LEN
13447 }
13448 
13449 /*
13450  * Join the ipif specific multicast groups.
13451  * Must be called after a mapping has been set up in the resolver.  (Always
13452  * called as writer.)
13453  */
13454 void
13455 ipif_multicast_up(ipif_t *ipif)
13456 {
13457 	int err;
13458 	ill_t *ill;
13459 	ilm_t *ilm;
13460 
13461 	ASSERT(IAM_WRITER_IPIF(ipif));
13462 
13463 	ill = ipif->ipif_ill;
13464 
13465 	ip1dbg(("ipif_multicast_up\n"));
13466 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13467 	    ipif->ipif_allhosts_ilm != NULL)
13468 		return;
13469 
13470 	if (ipif->ipif_isv6) {
13471 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13472 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13473 
13474 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13475 
13476 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13477 			return;
13478 
13479 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13480 
13481 		/*
13482 		 * Join the all hosts multicast address.  We skip this for
13483 		 * underlying IPMP interfaces since they should be invisible.
13484 		 */
13485 		if (!IS_UNDER_IPMP(ill)) {
13486 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13487 			    &err);
13488 			if (ilm == NULL) {
13489 				ASSERT(err != 0);
13490 				ip0dbg(("ipif_multicast_up: "
13491 				    "all_hosts_mcast failed %d\n", err));
13492 				return;
13493 			}
13494 			ipif->ipif_allhosts_ilm = ilm;
13495 		}
13496 
13497 		/*
13498 		 * Enable multicast for the solicited node multicast address.
13499 		 * If IPMP we need to put the membership on the upper ill.
13500 		 */
13501 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13502 			ill_t *mcast_ill = NULL;
13503 			boolean_t need_refrele;
13504 
13505 			if (IS_UNDER_IPMP(ill) &&
13506 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13507 				need_refrele = B_TRUE;
13508 			} else {
13509 				mcast_ill = ill;
13510 				need_refrele = B_FALSE;
13511 			}
13512 
13513 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13514 			    ipif->ipif_zoneid, &err);
13515 			if (need_refrele)
13516 				ill_refrele(mcast_ill);
13517 
13518 			if (ilm == NULL) {
13519 				ASSERT(err != 0);
13520 				ip0dbg(("ipif_multicast_up: solicited MC"
13521 				    " failed %d\n", err));
13522 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13523 					ipif->ipif_allhosts_ilm = NULL;
13524 					(void) ip_delmulti(ilm);
13525 				}
13526 				return;
13527 			}
13528 			ipif->ipif_solmulti_ilm = ilm;
13529 		}
13530 	} else {
13531 		in6_addr_t v6group;
13532 
13533 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13534 			return;
13535 
13536 		/* Join the all hosts multicast address */
13537 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13538 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13539 
13540 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13541 		if (ilm == NULL) {
13542 			ASSERT(err != 0);
13543 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13544 			return;
13545 		}
13546 		ipif->ipif_allhosts_ilm = ilm;
13547 	}
13548 }
13549 
13550 /*
13551  * Blow away any multicast groups that we joined in ipif_multicast_up().
13552  * (ilms from explicit memberships are handled in conn_update_ill.)
13553  */
13554 void
13555 ipif_multicast_down(ipif_t *ipif)
13556 {
13557 	ASSERT(IAM_WRITER_IPIF(ipif));
13558 
13559 	ip1dbg(("ipif_multicast_down\n"));
13560 
13561 	if (ipif->ipif_allhosts_ilm != NULL) {
13562 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13563 		ipif->ipif_allhosts_ilm = NULL;
13564 	}
13565 	if (ipif->ipif_solmulti_ilm != NULL) {
13566 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13567 		ipif->ipif_solmulti_ilm = NULL;
13568 	}
13569 }
13570 
13571 /*
13572  * Used when an interface comes up to recreate any extra routes on this
13573  * interface.
13574  */
13575 int
13576 ill_recover_saved_ire(ill_t *ill)
13577 {
13578 	mblk_t		*mp;
13579 	ip_stack_t	*ipst = ill->ill_ipst;
13580 
13581 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13582 
13583 	mutex_enter(&ill->ill_saved_ire_lock);
13584 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13585 		ire_t		*ire, *nire;
13586 		ifrt_t		*ifrt;
13587 
13588 		ifrt = (ifrt_t *)mp->b_rptr;
13589 		/*
13590 		 * Create a copy of the IRE with the saved address and netmask.
13591 		 */
13592 		if (ill->ill_isv6) {
13593 			ire = ire_create_v6(
13594 			    &ifrt->ifrt_v6addr,
13595 			    &ifrt->ifrt_v6mask,
13596 			    &ifrt->ifrt_v6gateway_addr,
13597 			    ifrt->ifrt_type,
13598 			    ill,
13599 			    ifrt->ifrt_zoneid,
13600 			    ifrt->ifrt_flags,
13601 			    NULL,
13602 			    ipst);
13603 		} else {
13604 			ire = ire_create(
13605 			    (uint8_t *)&ifrt->ifrt_addr,
13606 			    (uint8_t *)&ifrt->ifrt_mask,
13607 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13608 			    ifrt->ifrt_type,
13609 			    ill,
13610 			    ifrt->ifrt_zoneid,
13611 			    ifrt->ifrt_flags,
13612 			    NULL,
13613 			    ipst);
13614 		}
13615 		if (ire == NULL) {
13616 			mutex_exit(&ill->ill_saved_ire_lock);
13617 			return (ENOMEM);
13618 		}
13619 
13620 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13621 			if (ill->ill_isv6) {
13622 				ire->ire_setsrc_addr_v6 =
13623 				    ifrt->ifrt_v6setsrc_addr;
13624 			} else {
13625 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13626 			}
13627 		}
13628 
13629 		/*
13630 		 * Some software (for example, GateD and Sun Cluster) attempts
13631 		 * to create (what amount to) IRE_PREFIX routes with the
13632 		 * loopback address as the gateway.  This is primarily done to
13633 		 * set up prefixes with the RTF_REJECT flag set (for example,
13634 		 * when generating aggregate routes.)
13635 		 *
13636 		 * If the IRE type (as defined by ill->ill_net_type) is
13637 		 * IRE_LOOPBACK, then we map the request into a
13638 		 * IRE_IF_NORESOLVER.
13639 		 */
13640 		if (ill->ill_net_type == IRE_LOOPBACK)
13641 			ire->ire_type = IRE_IF_NORESOLVER;
13642 
13643 		/*
13644 		 * ire held by ire_add, will be refreled' towards the
13645 		 * the end of ipif_up_done
13646 		 */
13647 		nire = ire_add(ire);
13648 		/*
13649 		 * Check if it was a duplicate entry. This handles
13650 		 * the case of two racing route adds for the same route
13651 		 */
13652 		if (nire == NULL) {
13653 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13654 		} else if (nire != ire) {
13655 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13656 			    (void *)nire));
13657 			ire_delete(nire);
13658 		} else {
13659 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13660 			    (void *)nire));
13661 		}
13662 		if (nire != NULL)
13663 			ire_refrele(nire);
13664 	}
13665 	mutex_exit(&ill->ill_saved_ire_lock);
13666 	return (0);
13667 }
13668 
13669 /*
13670  * Used to set the netmask and broadcast address to default values when the
13671  * interface is brought up.  (Always called as writer.)
13672  */
13673 static void
13674 ipif_set_default(ipif_t *ipif)
13675 {
13676 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13677 
13678 	if (!ipif->ipif_isv6) {
13679 		/*
13680 		 * Interface holds an IPv4 address. Default
13681 		 * mask is the natural netmask.
13682 		 */
13683 		if (!ipif->ipif_net_mask) {
13684 			ipaddr_t	v4mask;
13685 
13686 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13687 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13688 		}
13689 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13690 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13691 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13692 		} else {
13693 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13694 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13695 		}
13696 		/*
13697 		 * NOTE: SunOS 4.X does this even if the broadcast address
13698 		 * has been already set thus we do the same here.
13699 		 */
13700 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13701 			ipaddr_t	v4addr;
13702 
13703 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13704 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13705 		}
13706 	} else {
13707 		/*
13708 		 * Interface holds an IPv6-only address.  Default
13709 		 * mask is all-ones.
13710 		 */
13711 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13712 			ipif->ipif_v6net_mask = ipv6_all_ones;
13713 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13714 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13715 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13716 		} else {
13717 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13718 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13719 		}
13720 	}
13721 }
13722 
13723 /*
13724  * Return 0 if this address can be used as local address without causing
13725  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13726  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13727  * Note that the same IPv6 link-local address is allowed as long as the ills
13728  * are not on the same link.
13729  */
13730 int
13731 ip_addr_availability_check(ipif_t *new_ipif)
13732 {
13733 	in6_addr_t our_v6addr;
13734 	ill_t *ill;
13735 	ipif_t *ipif;
13736 	ill_walk_context_t ctx;
13737 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13738 
13739 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13740 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13741 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13742 
13743 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13744 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13745 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13746 		return (0);
13747 
13748 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13749 
13750 	if (new_ipif->ipif_isv6)
13751 		ill = ILL_START_WALK_V6(&ctx, ipst);
13752 	else
13753 		ill = ILL_START_WALK_V4(&ctx, ipst);
13754 
13755 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13756 		for (ipif = ill->ill_ipif; ipif != NULL;
13757 		    ipif = ipif->ipif_next) {
13758 			if ((ipif == new_ipif) ||
13759 			    !(ipif->ipif_flags & IPIF_UP) ||
13760 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13761 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13762 			    &our_v6addr))
13763 				continue;
13764 
13765 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13766 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13767 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13768 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13769 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13770 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13771 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13772 				continue;
13773 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13774 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13775 				continue;
13776 			else if (new_ipif->ipif_ill == ill)
13777 				return (EADDRINUSE);
13778 			else
13779 				return (EADDRNOTAVAIL);
13780 		}
13781 	}
13782 
13783 	return (0);
13784 }
13785 
13786 /*
13787  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13788  * IREs for the ipif.
13789  * When the routine returns EINPROGRESS then mp has been consumed and
13790  * the ioctl will be acked from ip_rput_dlpi.
13791  */
13792 int
13793 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13794 {
13795 	ill_t		*ill = ipif->ipif_ill;
13796 	boolean_t 	isv6 = ipif->ipif_isv6;
13797 	int		err = 0;
13798 	boolean_t	success;
13799 	uint_t		ipif_orig_id;
13800 	ip_stack_t	*ipst = ill->ill_ipst;
13801 
13802 	ASSERT(IAM_WRITER_IPIF(ipif));
13803 
13804 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13805 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13806 	    ill_t *, ill, ipif_t *, ipif);
13807 
13808 	/* Shouldn't get here if it is already up. */
13809 	if (ipif->ipif_flags & IPIF_UP)
13810 		return (EALREADY);
13811 
13812 	/*
13813 	 * If this is a request to bring up a data address on an interface
13814 	 * under IPMP, then move the address to its IPMP meta-interface and
13815 	 * try to bring it up.  One complication is that the zeroth ipif for
13816 	 * an ill is special, in that every ill always has one, and that code
13817 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13818 	 */
13819 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13820 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13821 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13822 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13823 
13824 		/*
13825 		 * The ipif being brought up should be quiesced.  If it's not,
13826 		 * something has gone amiss and we need to bail out.  (If it's
13827 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13828 		 */
13829 		mutex_enter(&ill->ill_lock);
13830 		if (!ipif_is_quiescent(ipif)) {
13831 			mutex_exit(&ill->ill_lock);
13832 			return (EINVAL);
13833 		}
13834 		mutex_exit(&ill->ill_lock);
13835 
13836 		/*
13837 		 * If we're going to need to allocate ipifs, do it prior
13838 		 * to starting the move (and grabbing locks).
13839 		 */
13840 		if (ipif->ipif_id == 0) {
13841 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13842 			    B_FALSE);
13843 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13844 			    B_FALSE);
13845 			if (moveipif == NULL || stubipif == NULL) {
13846 				mi_free(moveipif);
13847 				mi_free(stubipif);
13848 				return (ENOMEM);
13849 			}
13850 		}
13851 
13852 		/*
13853 		 * Grab or transfer the ipif to move.  During the move, keep
13854 		 * ill_g_lock held to prevent any ill walker threads from
13855 		 * seeing things in an inconsistent state.
13856 		 */
13857 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13858 		if (ipif->ipif_id != 0) {
13859 			ipif_remove(ipif);
13860 		} else {
13861 			ipif_transfer(ipif, moveipif, stubipif);
13862 			ipif = moveipif;
13863 		}
13864 
13865 		/*
13866 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13867 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13868 		 * replace that one.  Otherwise, pick the next available slot.
13869 		 */
13870 		ipif->ipif_ill = ipmp_ill;
13871 		ipif_orig_id = ipif->ipif_id;
13872 
13873 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13874 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13875 			ipif = ipmp_ill->ill_ipif;
13876 		} else {
13877 			ipif->ipif_id = -1;
13878 			if (ipif_insert(ipif, B_FALSE) != 0) {
13879 				/*
13880 				 * No more available ipif_id's -- put it back
13881 				 * on the original ill and fail the operation.
13882 				 * Since we're writer on the ill, we can be
13883 				 * sure our old slot is still available.
13884 				 */
13885 				ipif->ipif_id = ipif_orig_id;
13886 				ipif->ipif_ill = ill;
13887 				if (ipif_orig_id == 0) {
13888 					ipif_transfer(ipif, ill->ill_ipif,
13889 					    NULL);
13890 				} else {
13891 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13892 				}
13893 				rw_exit(&ipst->ips_ill_g_lock);
13894 				return (ENOMEM);
13895 			}
13896 		}
13897 		rw_exit(&ipst->ips_ill_g_lock);
13898 
13899 		/*
13900 		 * Tell SCTP that the ipif has moved.  Note that even if we
13901 		 * had to allocate a new ipif, the original sequence id was
13902 		 * preserved and therefore SCTP won't know.
13903 		 */
13904 		sctp_move_ipif(ipif, ill, ipmp_ill);
13905 
13906 		/*
13907 		 * If the ipif being brought up was on slot zero, then we
13908 		 * first need to bring up the placeholder we stuck there.  In
13909 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13910 		 * call to ipif_up() itself, if we successfully bring up the
13911 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13912 		 */
13913 		if (ipif_orig_id == 0) {
13914 			ASSERT(ill->ill_move_ipif == NULL);
13915 			ill->ill_move_ipif = ipif;
13916 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
13917 				ASSERT(ill->ill_move_ipif == NULL);
13918 			if (err != EINPROGRESS)
13919 				ill->ill_move_ipif = NULL;
13920 			return (err);
13921 		}
13922 
13923 		/*
13924 		 * Bring it up on the IPMP ill.
13925 		 */
13926 		return (ipif_up(ipif, q, mp));
13927 	}
13928 
13929 	/* Skip arp/ndp for any loopback interface. */
13930 	if (ill->ill_wq != NULL) {
13931 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13932 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
13933 
13934 		if (!ill->ill_dl_up) {
13935 			/*
13936 			 * ill_dl_up is not yet set. i.e. we are yet to
13937 			 * DL_BIND with the driver and this is the first
13938 			 * logical interface on the ill to become "up".
13939 			 * Tell the driver to get going (via DL_BIND_REQ).
13940 			 * Note that changing "significant" IFF_ flags
13941 			 * address/netmask etc cause a down/up dance, but
13942 			 * does not cause an unbind (DL_UNBIND) with the driver
13943 			 */
13944 			return (ill_dl_up(ill, ipif, mp, q));
13945 		}
13946 
13947 		/*
13948 		 * ipif_resolver_up may end up needeing to bind/attach
13949 		 * the ARP stream, which in turn necessitates a
13950 		 * DLPI message exchange with the driver. ioctls are
13951 		 * serialized and so we cannot send more than one
13952 		 * interface up message at a time. If ipif_resolver_up
13953 		 * does need to wait for the DLPI handshake for the ARP stream,
13954 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
13955 		 */
13956 
13957 		ASSERT(connp != NULL || !CONN_Q(q));
13958 		if (connp != NULL)
13959 			mutex_enter(&connp->conn_lock);
13960 		mutex_enter(&ill->ill_lock);
13961 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13962 		mutex_exit(&ill->ill_lock);
13963 		if (connp != NULL)
13964 			mutex_exit(&connp->conn_lock);
13965 		if (!success)
13966 			return (EINTR);
13967 
13968 		/*
13969 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
13970 		 * complete when ipif_ndp_up returns.
13971 		 */
13972 		err = ipif_resolver_up(ipif, Res_act_initial);
13973 		if (err == EINPROGRESS) {
13974 			/* We will complete it in arp_bringup_done() */
13975 			return (err);
13976 		}
13977 
13978 		if (isv6 && err == 0)
13979 			err = ipif_ndp_up(ipif, B_TRUE);
13980 
13981 		ASSERT(err != EINPROGRESS);
13982 		mp = ipsq_pending_mp_get(ipsq, &connp);
13983 		ASSERT(mp != NULL);
13984 		if (err != 0)
13985 			return (err);
13986 	} else {
13987 		/*
13988 		 * Interfaces without underlying hardware don't do duplicate
13989 		 * address detection.
13990 		 */
13991 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
13992 		ipif->ipif_addr_ready = 1;
13993 		err = ill_add_ires(ill);
13994 		/* allocation failure? */
13995 		if (err != 0)
13996 			return (err);
13997 	}
13998 
13999 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14000 	if (err == 0 && ill->ill_move_ipif != NULL) {
14001 		ipif = ill->ill_move_ipif;
14002 		ill->ill_move_ipif = NULL;
14003 		return (ipif_up(ipif, q, mp));
14004 	}
14005 	return (err);
14006 }
14007 
14008 /*
14009  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14010  * The identical set of IREs need to be removed in ill_delete_ires().
14011  */
14012 int
14013 ill_add_ires(ill_t *ill)
14014 {
14015 	ire_t	*ire;
14016 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14017 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14018 
14019 	if (ill->ill_ire_multicast != NULL)
14020 		return (0);
14021 
14022 	/*
14023 	 * provide some dummy ire_addr for creating the ire.
14024 	 */
14025 	if (ill->ill_isv6) {
14026 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14027 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14028 	} else {
14029 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14030 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14031 	}
14032 	if (ire == NULL)
14033 		return (ENOMEM);
14034 
14035 	ill->ill_ire_multicast = ire;
14036 	return (0);
14037 }
14038 
14039 void
14040 ill_delete_ires(ill_t *ill)
14041 {
14042 	if (ill->ill_ire_multicast != NULL) {
14043 		/*
14044 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14045 		 * which was taken without any th_tracing enabled.
14046 		 * We also mark it as condemned (note that it was never added)
14047 		 * so that caching conn's can move off of it.
14048 		 */
14049 		ire_make_condemned(ill->ill_ire_multicast);
14050 		ire_refrele_notr(ill->ill_ire_multicast);
14051 		ill->ill_ire_multicast = NULL;
14052 	}
14053 }
14054 
14055 /*
14056  * Perform a bind for the physical device.
14057  * When the routine returns EINPROGRESS then mp has been consumed and
14058  * the ioctl will be acked from ip_rput_dlpi.
14059  * Allocate an unbind message and save it until ipif_down.
14060  */
14061 static int
14062 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14063 {
14064 	mblk_t	*bind_mp = NULL;
14065 	mblk_t	*unbind_mp = NULL;
14066 	conn_t	*connp;
14067 	boolean_t success;
14068 	int	err;
14069 
14070 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14071 
14072 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14073 	ASSERT(IAM_WRITER_ILL(ill));
14074 	ASSERT(mp != NULL);
14075 
14076 	/*
14077 	 * Make sure we have an IRE_MULTICAST in case we immediately
14078 	 * start receiving packets.
14079 	 */
14080 	err = ill_add_ires(ill);
14081 	if (err != 0)
14082 		goto bad;
14083 
14084 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14085 	    DL_BIND_REQ);
14086 	if (bind_mp == NULL)
14087 		goto bad;
14088 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14089 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14090 
14091 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14092 	if (unbind_mp == NULL)
14093 		goto bad;
14094 
14095 	/*
14096 	 * Record state needed to complete this operation when the
14097 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14098 	 */
14099 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14100 	ASSERT(connp != NULL || !CONN_Q(q));
14101 	GRAB_CONN_LOCK(q);
14102 	mutex_enter(&ipif->ipif_ill->ill_lock);
14103 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14104 	mutex_exit(&ipif->ipif_ill->ill_lock);
14105 	RELEASE_CONN_LOCK(q);
14106 	if (!success)
14107 		goto bad;
14108 
14109 	/*
14110 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14111 	 * the interface goes down.
14112 	 */
14113 	ASSERT(ill->ill_unbind_mp == NULL);
14114 	ill->ill_unbind_mp = unbind_mp;
14115 
14116 	ill_dlpi_send(ill, bind_mp);
14117 	/* Send down link-layer capabilities probe if not already done. */
14118 	ill_capability_probe(ill);
14119 
14120 	/*
14121 	 * Sysid used to rely on the fact that netboots set domainname
14122 	 * and the like. Now that miniroot boots aren't strictly netboots
14123 	 * and miniroot network configuration is driven from userland
14124 	 * these things still need to be set. This situation can be detected
14125 	 * by comparing the interface being configured here to the one
14126 	 * dhcifname was set to reference by the boot loader. Once sysid is
14127 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14128 	 */
14129 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14130 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14131 	    (strlen(srpc_domain) == 0)) {
14132 		if (dhcpinit() != 0)
14133 			cmn_err(CE_WARN, "no cached dhcp response");
14134 	}
14135 
14136 	/*
14137 	 * This operation will complete in ip_rput_dlpi with either
14138 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14139 	 */
14140 	return (EINPROGRESS);
14141 bad:
14142 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14143 
14144 	freemsg(bind_mp);
14145 	freemsg(unbind_mp);
14146 	return (ENOMEM);
14147 }
14148 
14149 /* Add room for tcp+ip headers */
14150 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14151 
14152 /*
14153  * DLPI and ARP is up.
14154  * Create all the IREs associated with an interface. Bring up multicast.
14155  * Set the interface flag and finish other initialization
14156  * that potentially had to be deferred to after DL_BIND_ACK.
14157  */
14158 int
14159 ipif_up_done(ipif_t *ipif)
14160 {
14161 	ill_t		*ill = ipif->ipif_ill;
14162 	int		err = 0;
14163 	boolean_t	loopback = B_FALSE;
14164 	boolean_t	update_src_selection = B_TRUE;
14165 	ipif_t		*tmp_ipif;
14166 
14167 	ip1dbg(("ipif_up_done(%s:%u)\n",
14168 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14169 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14170 	    ill_t *, ill, ipif_t *, ipif);
14171 
14172 	/* Check if this is a loopback interface */
14173 	if (ipif->ipif_ill->ill_wq == NULL)
14174 		loopback = B_TRUE;
14175 
14176 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14177 
14178 	/*
14179 	 * If all other interfaces for this ill are down or DEPRECATED,
14180 	 * or otherwise unsuitable for source address selection,
14181 	 * reset the src generation numbers to make sure source
14182 	 * address selection gets to take this new ipif into account.
14183 	 * No need to hold ill_lock while traversing the ipif list since
14184 	 * we are writer
14185 	 */
14186 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14187 	    tmp_ipif = tmp_ipif->ipif_next) {
14188 		if (((tmp_ipif->ipif_flags &
14189 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14190 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14191 		    (tmp_ipif == ipif))
14192 			continue;
14193 		/* first useable pre-existing interface */
14194 		update_src_selection = B_FALSE;
14195 		break;
14196 	}
14197 	if (update_src_selection)
14198 		ip_update_source_selection(ill->ill_ipst);
14199 
14200 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14201 		nce_t *loop_nce = NULL;
14202 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14203 
14204 		/*
14205 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14206 		 * ipif_lookup_on_name(), but in the case of zones we can have
14207 		 * several loopback addresses on lo0. So all the interfaces with
14208 		 * loopback addresses need to be marked IRE_LOOPBACK.
14209 		 */
14210 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14211 		    htonl(INADDR_LOOPBACK))
14212 			ipif->ipif_ire_type = IRE_LOOPBACK;
14213 		else
14214 			ipif->ipif_ire_type = IRE_LOCAL;
14215 		if (ill->ill_net_type != IRE_LOOPBACK)
14216 			flags |= NCE_F_PUBLISH;
14217 
14218 		/* add unicast nce for the local addr */
14219 		err = nce_lookup_then_add_v4(ill, NULL,
14220 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14221 		    ND_REACHABLE, &loop_nce);
14222 		/* A shared-IP zone sees EEXIST for lo0:N */
14223 		if (err == 0 || err == EEXIST) {
14224 			ipif->ipif_added_nce = 1;
14225 			loop_nce->nce_ipif_cnt++;
14226 			nce_refrele(loop_nce);
14227 			err = 0;
14228 		} else {
14229 			ASSERT(loop_nce == NULL);
14230 			return (err);
14231 		}
14232 	}
14233 
14234 	/* Create all the IREs associated with this interface */
14235 	err = ipif_add_ires_v4(ipif, loopback);
14236 	if (err != 0) {
14237 		/*
14238 		 * see comments about return value from
14239 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14240 		 */
14241 		if (err != EADDRINUSE) {
14242 			(void) ipif_arp_down(ipif);
14243 		} else {
14244 			/*
14245 			 * Make IPMP aware of the deleted ipif so that
14246 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14247 			 * can be completed. Note that we do not want to
14248 			 * destroy the nce that was created on the ipmp_ill
14249 			 * for the active copy of the duplicate address in
14250 			 * use.
14251 			 */
14252 			if (IS_IPMP(ill))
14253 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14254 			err = EADDRNOTAVAIL;
14255 		}
14256 		return (err);
14257 	}
14258 
14259 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14260 		/* Recover any additional IREs entries for this ill */
14261 		(void) ill_recover_saved_ire(ill);
14262 	}
14263 
14264 	if (ill->ill_need_recover_multicast) {
14265 		/*
14266 		 * Need to recover all multicast memberships in the driver.
14267 		 * This had to be deferred until we had attached.  The same
14268 		 * code exists in ipif_up_done_v6() to recover IPv6
14269 		 * memberships.
14270 		 *
14271 		 * Note that it would be preferable to unconditionally do the
14272 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14273 		 * that since ill_join_allmulti() depends on ill_dl_up being
14274 		 * set, and it is not set until we receive a DL_BIND_ACK after
14275 		 * having called ill_dl_up().
14276 		 */
14277 		ill_recover_multicast(ill);
14278 	}
14279 
14280 	if (ill->ill_ipif_up_count == 1) {
14281 		/*
14282 		 * Since the interface is now up, it may now be active.
14283 		 */
14284 		if (IS_UNDER_IPMP(ill))
14285 			ipmp_ill_refresh_active(ill);
14286 
14287 		/*
14288 		 * If this is an IPMP interface, we may now be able to
14289 		 * establish ARP entries.
14290 		 */
14291 		if (IS_IPMP(ill))
14292 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14293 	}
14294 
14295 	/* Join the allhosts multicast address */
14296 	ipif_multicast_up(ipif);
14297 
14298 	if (!loopback && !update_src_selection &&
14299 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14300 		ip_update_source_selection(ill->ill_ipst);
14301 
14302 	if (!loopback && ipif->ipif_addr_ready) {
14303 		/* Broadcast an address mask reply. */
14304 		ipif_mask_reply(ipif);
14305 	}
14306 	/* Perhaps ilgs should use this ill */
14307 	update_conn_ill(NULL, ill->ill_ipst);
14308 
14309 	/*
14310 	 * This had to be deferred until we had bound.  Tell routing sockets and
14311 	 * others that this interface is up if it looks like the address has
14312 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14313 	 * duplicate address detection to do its thing.
14314 	 */
14315 	if (ipif->ipif_addr_ready)
14316 		ipif_up_notify(ipif);
14317 	return (0);
14318 }
14319 
14320 /*
14321  * Add the IREs associated with the ipif.
14322  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14323  */
14324 static int
14325 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14326 {
14327 	ill_t		*ill = ipif->ipif_ill;
14328 	ip_stack_t	*ipst = ill->ill_ipst;
14329 	ire_t		*ire_array[20];
14330 	ire_t		**irep = ire_array;
14331 	ire_t		**irep1;
14332 	ipaddr_t	net_mask = 0;
14333 	ipaddr_t	subnet_mask, route_mask;
14334 	int		err;
14335 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14336 	ire_t		*ire_if = NULL;
14337 
14338 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14339 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14340 		/*
14341 		 * If we're on a labeled system then make sure that zone-
14342 		 * private addresses have proper remote host database entries.
14343 		 */
14344 		if (is_system_labeled() &&
14345 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14346 		    !tsol_check_interface_address(ipif))
14347 			return (EINVAL);
14348 
14349 		/* Register the source address for __sin6_src_id */
14350 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14351 		    ipif->ipif_zoneid, ipst);
14352 		if (err != 0) {
14353 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14354 			return (err);
14355 		}
14356 
14357 		/* If the interface address is set, create the local IRE. */
14358 		ire_local = ire_create(
14359 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14360 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14361 		    NULL,				/* no gateway */
14362 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14363 		    ipif->ipif_ill,
14364 		    ipif->ipif_zoneid,
14365 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14366 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14367 		    NULL,
14368 		    ipst);
14369 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14370 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14371 		    ipif->ipif_ire_type,
14372 		    ntohl(ipif->ipif_lcl_addr)));
14373 		if (ire_local == NULL) {
14374 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14375 			err = ENOMEM;
14376 			goto bad;
14377 		}
14378 	} else {
14379 		ip1dbg((
14380 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14381 		    ipif->ipif_ire_type,
14382 		    ntohl(ipif->ipif_lcl_addr),
14383 		    (uint_t)ipif->ipif_flags));
14384 	}
14385 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14386 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14387 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14388 	} else {
14389 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14390 	}
14391 
14392 	subnet_mask = ipif->ipif_net_mask;
14393 
14394 	/*
14395 	 * If mask was not specified, use natural netmask of
14396 	 * interface address. Also, store this mask back into the
14397 	 * ipif struct.
14398 	 */
14399 	if (subnet_mask == 0) {
14400 		subnet_mask = net_mask;
14401 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14402 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14403 		    ipif->ipif_v6subnet);
14404 	}
14405 
14406 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14407 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14408 	    ipif->ipif_subnet != INADDR_ANY) {
14409 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14410 
14411 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14412 			route_mask = IP_HOST_MASK;
14413 		} else {
14414 			route_mask = subnet_mask;
14415 		}
14416 
14417 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14418 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14419 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14420 		    ntohl(ipif->ipif_subnet)));
14421 		ire_if = ire_create(
14422 		    (uchar_t *)&ipif->ipif_subnet,
14423 		    (uchar_t *)&route_mask,
14424 		    (uchar_t *)&ipif->ipif_lcl_addr,
14425 		    ill->ill_net_type,
14426 		    ill,
14427 		    ipif->ipif_zoneid,
14428 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14429 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14430 		    NULL,
14431 		    ipst);
14432 		if (ire_if == NULL) {
14433 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14434 			err = ENOMEM;
14435 			goto bad;
14436 		}
14437 	}
14438 
14439 	/*
14440 	 * Create any necessary broadcast IREs.
14441 	 */
14442 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14443 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14444 		irep = ipif_create_bcast_ires(ipif, irep);
14445 
14446 	/* If an earlier ire_create failed, get out now */
14447 	for (irep1 = irep; irep1 > ire_array; ) {
14448 		irep1--;
14449 		if (*irep1 == NULL) {
14450 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14451 			err = ENOMEM;
14452 			goto bad;
14453 		}
14454 	}
14455 
14456 	/*
14457 	 * Need to atomically check for IP address availability under
14458 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14459 	 * ills or new ipifs can be added while we are checking availability.
14460 	 */
14461 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14462 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14463 	/* Mark it up, and increment counters. */
14464 	ipif->ipif_flags |= IPIF_UP;
14465 	ill->ill_ipif_up_count++;
14466 	err = ip_addr_availability_check(ipif);
14467 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14468 	rw_exit(&ipst->ips_ill_g_lock);
14469 
14470 	if (err != 0) {
14471 		/*
14472 		 * Our address may already be up on the same ill. In this case,
14473 		 * the ARP entry for our ipif replaced the one for the other
14474 		 * ipif. So we don't want to delete it (otherwise the other ipif
14475 		 * would be unable to send packets).
14476 		 * ip_addr_availability_check() identifies this case for us and
14477 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14478 		 * which is the expected error code.
14479 		 */
14480 		ill->ill_ipif_up_count--;
14481 		ipif->ipif_flags &= ~IPIF_UP;
14482 		goto bad;
14483 	}
14484 
14485 	/*
14486 	 * Add in all newly created IREs.  ire_create_bcast() has
14487 	 * already checked for duplicates of the IRE_BROADCAST type.
14488 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14489 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14490 	 * a /32 route.
14491 	 */
14492 	if (ire_if != NULL) {
14493 		ire_if = ire_add(ire_if);
14494 		if (ire_if == NULL) {
14495 			err = ENOMEM;
14496 			goto bad2;
14497 		}
14498 #ifdef DEBUG
14499 		ire_refhold_notr(ire_if);
14500 		ire_refrele(ire_if);
14501 #endif
14502 	}
14503 	if (ire_local != NULL) {
14504 		ire_local = ire_add(ire_local);
14505 		if (ire_local == NULL) {
14506 			err = ENOMEM;
14507 			goto bad2;
14508 		}
14509 #ifdef DEBUG
14510 		ire_refhold_notr(ire_local);
14511 		ire_refrele(ire_local);
14512 #endif
14513 	}
14514 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14515 	if (ire_local != NULL)
14516 		ipif->ipif_ire_local = ire_local;
14517 	if (ire_if != NULL)
14518 		ipif->ipif_ire_if = ire_if;
14519 	rw_exit(&ipst->ips_ill_g_lock);
14520 	ire_local = NULL;
14521 	ire_if = NULL;
14522 
14523 	/*
14524 	 * We first add all of them, and if that succeeds we refrele the
14525 	 * bunch. That enables us to delete all of them should any of the
14526 	 * ire_adds fail.
14527 	 */
14528 	for (irep1 = irep; irep1 > ire_array; ) {
14529 		irep1--;
14530 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14531 		*irep1 = ire_add(*irep1);
14532 		if (*irep1 == NULL) {
14533 			err = ENOMEM;
14534 			goto bad2;
14535 		}
14536 	}
14537 
14538 	for (irep1 = irep; irep1 > ire_array; ) {
14539 		irep1--;
14540 		/* refheld by ire_add. */
14541 		if (*irep1 != NULL) {
14542 			ire_refrele(*irep1);
14543 			*irep1 = NULL;
14544 		}
14545 	}
14546 
14547 	if (!loopback) {
14548 		/*
14549 		 * If the broadcast address has been set, make sure it makes
14550 		 * sense based on the interface address.
14551 		 * Only match on ill since we are sharing broadcast addresses.
14552 		 */
14553 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14554 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14555 			ire_t	*ire;
14556 
14557 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14558 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14559 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14560 
14561 			if (ire == NULL) {
14562 				/*
14563 				 * If there isn't a matching broadcast IRE,
14564 				 * revert to the default for this netmask.
14565 				 */
14566 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14567 				mutex_enter(&ipif->ipif_ill->ill_lock);
14568 				ipif_set_default(ipif);
14569 				mutex_exit(&ipif->ipif_ill->ill_lock);
14570 			} else {
14571 				ire_refrele(ire);
14572 			}
14573 		}
14574 
14575 	}
14576 	return (0);
14577 
14578 bad2:
14579 	ill->ill_ipif_up_count--;
14580 	ipif->ipif_flags &= ~IPIF_UP;
14581 
14582 bad:
14583 	ip1dbg(("ipif_add_ires: FAILED \n"));
14584 	if (ire_local != NULL)
14585 		ire_delete(ire_local);
14586 	if (ire_if != NULL)
14587 		ire_delete(ire_if);
14588 
14589 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14590 	ire_local = ipif->ipif_ire_local;
14591 	ipif->ipif_ire_local = NULL;
14592 	ire_if = ipif->ipif_ire_if;
14593 	ipif->ipif_ire_if = NULL;
14594 	rw_exit(&ipst->ips_ill_g_lock);
14595 	if (ire_local != NULL) {
14596 		ire_delete(ire_local);
14597 		ire_refrele_notr(ire_local);
14598 	}
14599 	if (ire_if != NULL) {
14600 		ire_delete(ire_if);
14601 		ire_refrele_notr(ire_if);
14602 	}
14603 
14604 	while (irep > ire_array) {
14605 		irep--;
14606 		if (*irep != NULL) {
14607 			ire_delete(*irep);
14608 		}
14609 	}
14610 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14611 
14612 	return (err);
14613 }
14614 
14615 /* Remove all the IREs created by ipif_add_ires_v4 */
14616 void
14617 ipif_delete_ires_v4(ipif_t *ipif)
14618 {
14619 	ill_t		*ill = ipif->ipif_ill;
14620 	ip_stack_t	*ipst = ill->ill_ipst;
14621 	ire_t		*ire;
14622 
14623 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14624 	ire = ipif->ipif_ire_local;
14625 	ipif->ipif_ire_local = NULL;
14626 	rw_exit(&ipst->ips_ill_g_lock);
14627 	if (ire != NULL) {
14628 		/*
14629 		 * Move count to ipif so we don't loose the count due to
14630 		 * a down/up dance.
14631 		 */
14632 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14633 
14634 		ire_delete(ire);
14635 		ire_refrele_notr(ire);
14636 	}
14637 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14638 	ire = ipif->ipif_ire_if;
14639 	ipif->ipif_ire_if = NULL;
14640 	rw_exit(&ipst->ips_ill_g_lock);
14641 	if (ire != NULL) {
14642 		ire_delete(ire);
14643 		ire_refrele_notr(ire);
14644 	}
14645 
14646 	/*
14647 	 * Delete the broadcast IREs.
14648 	 */
14649 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14650 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14651 		ipif_delete_bcast_ires(ipif);
14652 }
14653 
14654 /*
14655  * Checks for availbility of a usable source address (if there is one) when the
14656  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14657  * this selection is done regardless of the destination.
14658  */
14659 boolean_t
14660 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14661     ip_stack_t *ipst)
14662 {
14663 	ipif_t		*ipif = NULL;
14664 	ill_t		*uill;
14665 
14666 	ASSERT(ifindex != 0);
14667 
14668 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14669 	if (uill == NULL)
14670 		return (B_FALSE);
14671 
14672 	mutex_enter(&uill->ill_lock);
14673 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14674 		if (IPIF_IS_CONDEMNED(ipif))
14675 			continue;
14676 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14677 			continue;
14678 		if (!(ipif->ipif_flags & IPIF_UP))
14679 			continue;
14680 		if (ipif->ipif_zoneid != zoneid)
14681 			continue;
14682 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14683 		    ipif->ipif_lcl_addr == INADDR_ANY)
14684 			continue;
14685 		mutex_exit(&uill->ill_lock);
14686 		ill_refrele(uill);
14687 		return (B_TRUE);
14688 	}
14689 	mutex_exit(&uill->ill_lock);
14690 	ill_refrele(uill);
14691 	return (B_FALSE);
14692 }
14693 
14694 /*
14695  * Find an ipif with a good local address on the ill+zoneid.
14696  */
14697 ipif_t *
14698 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14699 {
14700 	ipif_t		*ipif;
14701 
14702 	mutex_enter(&ill->ill_lock);
14703 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14704 		if (IPIF_IS_CONDEMNED(ipif))
14705 			continue;
14706 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14707 			continue;
14708 		if (!(ipif->ipif_flags & IPIF_UP))
14709 			continue;
14710 		if (ipif->ipif_zoneid != zoneid &&
14711 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14712 			continue;
14713 		if (ill->ill_isv6 ?
14714 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14715 		    ipif->ipif_lcl_addr == INADDR_ANY)
14716 			continue;
14717 		ipif_refhold_locked(ipif);
14718 		mutex_exit(&ill->ill_lock);
14719 		return (ipif);
14720 	}
14721 	mutex_exit(&ill->ill_lock);
14722 	return (NULL);
14723 }
14724 
14725 /*
14726  * IP source address type, sorted from worst to best.  For a given type,
14727  * always prefer IP addresses on the same subnet.  All-zones addresses are
14728  * suboptimal because they pose problems with unlabeled destinations.
14729  */
14730 typedef enum {
14731 	IPIF_NONE,
14732 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14733 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14734 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14735 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14736 	IPIF_DIFFNET,			/* normal and different subnet */
14737 	IPIF_SAMENET,			/* normal and same subnet */
14738 	IPIF_LOCALADDR			/* local loopback */
14739 } ipif_type_t;
14740 
14741 /*
14742  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14743  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14744  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14745  * the first one, unless IPMP is used in which case we round-robin among them;
14746  * see below for more.
14747  *
14748  * Returns NULL if there is no suitable source address for the ill.
14749  * This only occurs when there is no valid source address for the ill.
14750  */
14751 ipif_t *
14752 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14753     boolean_t allow_usesrc, boolean_t *notreadyp)
14754 {
14755 	ill_t	*usill = NULL;
14756 	ill_t	*ipmp_ill = NULL;
14757 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14758 	ipif_type_t type, best_type;
14759 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14760 	ip_stack_t *ipst = ill->ill_ipst;
14761 	boolean_t samenet;
14762 
14763 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14764 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14765 		    B_FALSE, ipst);
14766 		if (usill != NULL)
14767 			ill = usill;	/* Select source from usesrc ILL */
14768 		else
14769 			return (NULL);
14770 	}
14771 
14772 	/*
14773 	 * Test addresses should never be used for source address selection,
14774 	 * so if we were passed one, switch to the IPMP meta-interface.
14775 	 */
14776 	if (IS_UNDER_IPMP(ill)) {
14777 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14778 			ill = ipmp_ill;	/* Select source from IPMP ill */
14779 		else
14780 			return (NULL);
14781 	}
14782 
14783 	/*
14784 	 * If we're dealing with an unlabeled destination on a labeled system,
14785 	 * make sure that we ignore source addresses that are incompatible with
14786 	 * the destination's default label.  That destination's default label
14787 	 * must dominate the minimum label on the source address.
14788 	 */
14789 	dst_rhtp = NULL;
14790 	if (is_system_labeled()) {
14791 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14792 		if (dst_rhtp == NULL)
14793 			return (NULL);
14794 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14795 			TPC_RELE(dst_rhtp);
14796 			dst_rhtp = NULL;
14797 		}
14798 	}
14799 
14800 	/*
14801 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14802 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14803 	 * After selecting the right ipif, under ill_lock make sure ipif is
14804 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14805 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14806 	 * but not under a lock.
14807 	 */
14808 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14809 retry:
14810 	/*
14811 	 * For source address selection, we treat the ipif list as circular
14812 	 * and continue until we get back to where we started.  This allows
14813 	 * IPMP to vary source address selection (which improves inbound load
14814 	 * spreading) by caching its last ending point and starting from
14815 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14816 	 * ills since that can't happen on the IPMP ill.
14817 	 */
14818 	start_ipif = ill->ill_ipif;
14819 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14820 		start_ipif = ill->ill_src_ipif;
14821 
14822 	ipif = start_ipif;
14823 	best_ipif = NULL;
14824 	best_type = IPIF_NONE;
14825 	do {
14826 		if ((next_ipif = ipif->ipif_next) == NULL)
14827 			next_ipif = ill->ill_ipif;
14828 
14829 		if (IPIF_IS_CONDEMNED(ipif))
14830 			continue;
14831 		/* Always skip NOLOCAL and ANYCAST interfaces */
14832 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14833 			continue;
14834 		/* Always skip NOACCEPT interfaces */
14835 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14836 			continue;
14837 		if (!(ipif->ipif_flags & IPIF_UP))
14838 			continue;
14839 
14840 		if (!ipif->ipif_addr_ready) {
14841 			if (notreadyp != NULL)
14842 				*notreadyp = B_TRUE;
14843 			continue;
14844 		}
14845 
14846 		if (zoneid != ALL_ZONES &&
14847 		    ipif->ipif_zoneid != zoneid &&
14848 		    ipif->ipif_zoneid != ALL_ZONES)
14849 			continue;
14850 
14851 		/*
14852 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14853 		 * are not valid as source addresses.
14854 		 */
14855 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14856 			continue;
14857 
14858 		/*
14859 		 * Check compatibility of local address for destination's
14860 		 * default label if we're on a labeled system.	Incompatible
14861 		 * addresses can't be used at all.
14862 		 */
14863 		if (dst_rhtp != NULL) {
14864 			boolean_t incompat;
14865 
14866 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14867 			    IPV4_VERSION, B_FALSE);
14868 			if (src_rhtp == NULL)
14869 				continue;
14870 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14871 			    src_rhtp->tpc_tp.tp_doi !=
14872 			    dst_rhtp->tpc_tp.tp_doi ||
14873 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14874 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14875 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14876 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14877 			TPC_RELE(src_rhtp);
14878 			if (incompat)
14879 				continue;
14880 		}
14881 
14882 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14883 
14884 		if (ipif->ipif_lcl_addr == dst) {
14885 			type = IPIF_LOCALADDR;
14886 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14887 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14888 			    IPIF_DIFFNET_DEPRECATED;
14889 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14890 			type = samenet ? IPIF_SAMENET_ALLZONES :
14891 			    IPIF_DIFFNET_ALLZONES;
14892 		} else {
14893 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14894 		}
14895 
14896 		if (type > best_type) {
14897 			best_type = type;
14898 			best_ipif = ipif;
14899 			if (best_type == IPIF_LOCALADDR)
14900 				break; /* can't get better */
14901 		}
14902 	} while ((ipif = next_ipif) != start_ipif);
14903 
14904 	if ((ipif = best_ipif) != NULL) {
14905 		mutex_enter(&ipif->ipif_ill->ill_lock);
14906 		if (IPIF_IS_CONDEMNED(ipif)) {
14907 			mutex_exit(&ipif->ipif_ill->ill_lock);
14908 			goto retry;
14909 		}
14910 		ipif_refhold_locked(ipif);
14911 
14912 		/*
14913 		 * For IPMP, update the source ipif rotor to the next ipif,
14914 		 * provided we can look it up.  (We must not use it if it's
14915 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
14916 		 * ipif_free() checked ill_src_ipif.)
14917 		 */
14918 		if (IS_IPMP(ill) && ipif != NULL) {
14919 			next_ipif = ipif->ipif_next;
14920 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
14921 				ill->ill_src_ipif = next_ipif;
14922 			else
14923 				ill->ill_src_ipif = NULL;
14924 		}
14925 		mutex_exit(&ipif->ipif_ill->ill_lock);
14926 	}
14927 
14928 	rw_exit(&ipst->ips_ill_g_lock);
14929 	if (usill != NULL)
14930 		ill_refrele(usill);
14931 	if (ipmp_ill != NULL)
14932 		ill_refrele(ipmp_ill);
14933 	if (dst_rhtp != NULL)
14934 		TPC_RELE(dst_rhtp);
14935 
14936 #ifdef DEBUG
14937 	if (ipif == NULL) {
14938 		char buf1[INET6_ADDRSTRLEN];
14939 
14940 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
14941 		    ill->ill_name,
14942 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
14943 	} else {
14944 		char buf1[INET6_ADDRSTRLEN];
14945 		char buf2[INET6_ADDRSTRLEN];
14946 
14947 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
14948 		    ipif->ipif_ill->ill_name,
14949 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
14950 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
14951 		    buf2, sizeof (buf2))));
14952 	}
14953 #endif /* DEBUG */
14954 	return (ipif);
14955 }
14956 
14957 /*
14958  * Pick a source address based on the destination ill and an optional setsrc
14959  * address.
14960  * The result is stored in srcp. If generation is set, then put the source
14961  * generation number there before we look for the source address (to avoid
14962  * missing changes in the set of source addresses.
14963  * If flagsp is set, then us it to pass back ipif_flags.
14964  *
14965  * If the caller wants to cache the returned source address and detect when
14966  * that might be stale, the caller should pass in a generation argument,
14967  * which the caller can later compare against ips_src_generation
14968  *
14969  * The precedence order for selecting an IPv4 source address is:
14970  *  - RTF_SETSRC on the offlink ire always wins.
14971  *  - If usrsrc is set, swap the ill to be the usesrc one.
14972  *  - If IPMP is used on the ill, select a random address from the most
14973  *    preferred ones below:
14974  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
14975  * 2. Not deprecated, not ALL_ZONES
14976  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
14977  * 4. Not deprecated, ALL_ZONES
14978  * 5. If onlink destination, same subnet and deprecated
14979  * 6. Deprecated.
14980  *
14981  * We have lower preference for ALL_ZONES IP addresses,
14982  * as they pose problems with unlabeled destinations.
14983  *
14984  * Note that when multiple IP addresses match e.g., #1 we pick
14985  * the first one if IPMP is not in use. With IPMP we randomize.
14986  */
14987 int
14988 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
14989     ipaddr_t multicast_ifaddr,
14990     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
14991     uint32_t *generation, uint64_t *flagsp)
14992 {
14993 	ipif_t *ipif;
14994 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
14995 
14996 	if (flagsp != NULL)
14997 		*flagsp = 0;
14998 
14999 	/*
15000 	 * Need to grab the generation number before we check to
15001 	 * avoid a race with a change to the set of local addresses.
15002 	 * No lock needed since the thread which updates the set of local
15003 	 * addresses use ipif/ill locks and exit those (hence a store memory
15004 	 * barrier) before doing the atomic increase of ips_src_generation.
15005 	 */
15006 	if (generation != NULL) {
15007 		*generation = ipst->ips_src_generation;
15008 	}
15009 
15010 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15011 		*srcp = multicast_ifaddr;
15012 		return (0);
15013 	}
15014 
15015 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15016 	if (setsrc != INADDR_ANY) {
15017 		*srcp = setsrc;
15018 		return (0);
15019 	}
15020 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15021 	if (ipif == NULL) {
15022 		if (notready)
15023 			return (ENETDOWN);
15024 		else
15025 			return (EADDRNOTAVAIL);
15026 	}
15027 	*srcp = ipif->ipif_lcl_addr;
15028 	if (flagsp != NULL)
15029 		*flagsp = ipif->ipif_flags;
15030 	ipif_refrele(ipif);
15031 	return (0);
15032 }
15033 
15034 /* ARGSUSED */
15035 int
15036 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15037 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15038 {
15039 	/*
15040 	 * ill_phyint_reinit merged the v4 and v6 into a single
15041 	 * ipsq.  We might not have been able to complete the
15042 	 * operation in ipif_set_values, if we could not become
15043 	 * exclusive.  If so restart it here.
15044 	 */
15045 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15046 }
15047 
15048 /*
15049  * Can operate on either a module or a driver queue.
15050  * Returns an error if not a module queue.
15051  */
15052 /* ARGSUSED */
15053 int
15054 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15055     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15056 {
15057 	queue_t		*q1 = q;
15058 	char 		*cp;
15059 	char		interf_name[LIFNAMSIZ];
15060 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15061 
15062 	if (q->q_next == NULL) {
15063 		ip1dbg((
15064 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15065 		return (EINVAL);
15066 	}
15067 
15068 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15069 		return (EALREADY);
15070 
15071 	do {
15072 		q1 = q1->q_next;
15073 	} while (q1->q_next);
15074 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15075 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15076 
15077 	/*
15078 	 * Here we are not going to delay the ioack until after
15079 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15080 	 * original ioctl message before sending the requests.
15081 	 */
15082 	return (ipif_set_values(q, mp, interf_name, &ppa));
15083 }
15084 
15085 /* ARGSUSED */
15086 int
15087 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15088     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15089 {
15090 	return (ENXIO);
15091 }
15092 
15093 /*
15094  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15095  * `irep'.  Returns a pointer to the next free `irep' entry
15096  * A mirror exists in ipif_delete_bcast_ires().
15097  *
15098  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15099  * done in ire_add.
15100  */
15101 static ire_t **
15102 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15103 {
15104 	ipaddr_t addr;
15105 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15106 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15107 	ill_t *ill = ipif->ipif_ill;
15108 	zoneid_t zoneid = ipif->ipif_zoneid;
15109 
15110 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15111 
15112 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15113 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15114 
15115 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15116 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15117 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15118 
15119 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15120 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15121 
15122 	/*
15123 	 * For backward compatibility, we create net broadcast IREs based on
15124 	 * the old "IP address class system", since some old machines only
15125 	 * respond to these class derived net broadcast.  However, we must not
15126 	 * create these net broadcast IREs if the subnetmask is shorter than
15127 	 * the IP address class based derived netmask.  Otherwise, we may
15128 	 * create a net broadcast address which is the same as an IP address
15129 	 * on the subnet -- and then TCP will refuse to talk to that address.
15130 	 */
15131 	if (netmask < subnetmask) {
15132 		addr = netmask & ipif->ipif_subnet;
15133 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15134 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15135 	}
15136 
15137 	/*
15138 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15139 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15140 	 * created.  Creating these broadcast IREs will only create confusion
15141 	 * as `addr' will be the same as the IP address.
15142 	 */
15143 	if (subnetmask != 0xFFFFFFFF) {
15144 		addr = ipif->ipif_subnet;
15145 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15146 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15147 	}
15148 
15149 	return (irep);
15150 }
15151 
15152 /*
15153  * Mirror of ipif_create_bcast_ires()
15154  */
15155 static void
15156 ipif_delete_bcast_ires(ipif_t *ipif)
15157 {
15158 	ipaddr_t	addr;
15159 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15160 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15161 	ill_t		*ill = ipif->ipif_ill;
15162 	zoneid_t	zoneid = ipif->ipif_zoneid;
15163 	ire_t		*ire;
15164 
15165 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15166 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15167 
15168 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15169 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15170 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15171 
15172 	ire = ire_lookup_bcast(ill, 0, zoneid);
15173 	ASSERT(ire != NULL);
15174 	ire_delete(ire); ire_refrele(ire);
15175 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15176 	ASSERT(ire != NULL);
15177 	ire_delete(ire); ire_refrele(ire);
15178 
15179 	/*
15180 	 * For backward compatibility, we create net broadcast IREs based on
15181 	 * the old "IP address class system", since some old machines only
15182 	 * respond to these class derived net broadcast.  However, we must not
15183 	 * create these net broadcast IREs if the subnetmask is shorter than
15184 	 * the IP address class based derived netmask.  Otherwise, we may
15185 	 * create a net broadcast address which is the same as an IP address
15186 	 * on the subnet -- and then TCP will refuse to talk to that address.
15187 	 */
15188 	if (netmask < subnetmask) {
15189 		addr = netmask & ipif->ipif_subnet;
15190 		ire = ire_lookup_bcast(ill, addr, zoneid);
15191 		ASSERT(ire != NULL);
15192 		ire_delete(ire); ire_refrele(ire);
15193 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15194 		ASSERT(ire != NULL);
15195 		ire_delete(ire); ire_refrele(ire);
15196 	}
15197 
15198 	/*
15199 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15200 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15201 	 * created.  Creating these broadcast IREs will only create confusion
15202 	 * as `addr' will be the same as the IP address.
15203 	 */
15204 	if (subnetmask != 0xFFFFFFFF) {
15205 		addr = ipif->ipif_subnet;
15206 		ire = ire_lookup_bcast(ill, addr, zoneid);
15207 		ASSERT(ire != NULL);
15208 		ire_delete(ire); ire_refrele(ire);
15209 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15210 		ASSERT(ire != NULL);
15211 		ire_delete(ire); ire_refrele(ire);
15212 	}
15213 }
15214 
15215 /*
15216  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15217  * from lifr_flags and the name from lifr_name.
15218  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15219  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15220  * Returns EINPROGRESS when mp has been consumed by queueing it on
15221  * ipx_pending_mp and the ioctl will complete in ip_rput.
15222  *
15223  * Can operate on either a module or a driver queue.
15224  * Returns an error if not a module queue.
15225  */
15226 /* ARGSUSED */
15227 int
15228 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15229     ip_ioctl_cmd_t *ipip, void *if_req)
15230 {
15231 	ill_t	*ill = q->q_ptr;
15232 	phyint_t *phyi;
15233 	ip_stack_t *ipst;
15234 	struct lifreq *lifr = if_req;
15235 	uint64_t new_flags;
15236 
15237 	ASSERT(ipif != NULL);
15238 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15239 
15240 	if (q->q_next == NULL) {
15241 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15242 		return (EINVAL);
15243 	}
15244 
15245 	/*
15246 	 * If we are not writer on 'q' then this interface exists already
15247 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15248 	 * so return EALREADY.
15249 	 */
15250 	if (ill != ipif->ipif_ill)
15251 		return (EALREADY);
15252 
15253 	if (ill->ill_name[0] != '\0')
15254 		return (EALREADY);
15255 
15256 	/*
15257 	 * If there's another ill already with the requested name, ensure
15258 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15259 	 * fuse together two unrelated ills, which will cause chaos.
15260 	 */
15261 	ipst = ill->ill_ipst;
15262 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15263 	    lifr->lifr_name, NULL);
15264 	if (phyi != NULL) {
15265 		ill_t *ill_mate = phyi->phyint_illv4;
15266 
15267 		if (ill_mate == NULL)
15268 			ill_mate = phyi->phyint_illv6;
15269 		ASSERT(ill_mate != NULL);
15270 
15271 		if (ill_mate->ill_media->ip_m_mac_type !=
15272 		    ill->ill_media->ip_m_mac_type) {
15273 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15274 			    "use the same ill name on differing media\n"));
15275 			return (EINVAL);
15276 		}
15277 	}
15278 
15279 	/*
15280 	 * We start off as IFF_IPV4 in ipif_allocate and become
15281 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15282 	 * The only flags that we read from user space are IFF_IPV4,
15283 	 * IFF_IPV6, and IFF_BROADCAST.
15284 	 *
15285 	 * This ill has not been inserted into the global list.
15286 	 * So we are still single threaded and don't need any lock
15287 	 *
15288 	 * Saniy check the flags.
15289 	 */
15290 
15291 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15292 	    ((lifr->lifr_flags & IFF_IPV6) ||
15293 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15294 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15295 		    "or IPv6 i.e., no broadcast \n"));
15296 		return (EINVAL);
15297 	}
15298 
15299 	new_flags =
15300 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15301 
15302 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15303 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15304 		    "IFF_IPV4 or IFF_IPV6\n"));
15305 		return (EINVAL);
15306 	}
15307 
15308 	/*
15309 	 * We always start off as IPv4, so only need to check for IPv6.
15310 	 */
15311 	if ((new_flags & IFF_IPV6) != 0) {
15312 		ill->ill_flags |= ILLF_IPV6;
15313 		ill->ill_flags &= ~ILLF_IPV4;
15314 	}
15315 
15316 	if ((new_flags & IFF_BROADCAST) != 0)
15317 		ipif->ipif_flags |= IPIF_BROADCAST;
15318 	else
15319 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15320 
15321 	/* We started off as V4. */
15322 	if (ill->ill_flags & ILLF_IPV6) {
15323 		ill->ill_phyint->phyint_illv6 = ill;
15324 		ill->ill_phyint->phyint_illv4 = NULL;
15325 	}
15326 
15327 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15328 }
15329 
15330 /* ARGSUSED */
15331 int
15332 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15333     ip_ioctl_cmd_t *ipip, void *if_req)
15334 {
15335 	/*
15336 	 * ill_phyint_reinit merged the v4 and v6 into a single
15337 	 * ipsq.  We might not have been able to complete the
15338 	 * slifname in ipif_set_values, if we could not become
15339 	 * exclusive.  If so restart it here
15340 	 */
15341 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15342 }
15343 
15344 /*
15345  * Return a pointer to the ipif which matches the index, IP version type and
15346  * zoneid.
15347  */
15348 ipif_t *
15349 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15350     ip_stack_t *ipst)
15351 {
15352 	ill_t	*ill;
15353 	ipif_t	*ipif = NULL;
15354 
15355 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15356 	if (ill != NULL) {
15357 		mutex_enter(&ill->ill_lock);
15358 		for (ipif = ill->ill_ipif; ipif != NULL;
15359 		    ipif = ipif->ipif_next) {
15360 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15361 			    zoneid == ipif->ipif_zoneid ||
15362 			    ipif->ipif_zoneid == ALL_ZONES)) {
15363 				ipif_refhold_locked(ipif);
15364 				break;
15365 			}
15366 		}
15367 		mutex_exit(&ill->ill_lock);
15368 		ill_refrele(ill);
15369 	}
15370 	return (ipif);
15371 }
15372 
15373 /*
15374  * Change an existing physical interface's index. If the new index
15375  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15376  * Finally, we update other systems which may have a dependence on the
15377  * index value.
15378  */
15379 /* ARGSUSED */
15380 int
15381 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15382     ip_ioctl_cmd_t *ipip, void *ifreq)
15383 {
15384 	ill_t		*ill;
15385 	phyint_t	*phyi;
15386 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15387 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15388 	uint_t	old_index, index;
15389 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15390 	avl_index_t	where;
15391 
15392 	if (ipip->ipi_cmd_type == IF_CMD)
15393 		index = ifr->ifr_index;
15394 	else
15395 		index = lifr->lifr_index;
15396 
15397 	/*
15398 	 * Only allow on physical interface. Also, index zero is illegal.
15399 	 */
15400 	ill = ipif->ipif_ill;
15401 	phyi = ill->ill_phyint;
15402 	if (ipif->ipif_id != 0 || index == 0) {
15403 		return (EINVAL);
15404 	}
15405 
15406 	/* If the index is not changing, no work to do */
15407 	if (phyi->phyint_ifindex == index)
15408 		return (0);
15409 
15410 	/*
15411 	 * Use phyint_exists() to determine if the new interface index
15412 	 * is already in use. If the index is unused then we need to
15413 	 * change the phyint's position in the phyint_list_avl_by_index
15414 	 * tree. If we do not do this, subsequent lookups (using the new
15415 	 * index value) will not find the phyint.
15416 	 */
15417 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15418 	if (phyint_exists(index, ipst)) {
15419 		rw_exit(&ipst->ips_ill_g_lock);
15420 		return (EEXIST);
15421 	}
15422 
15423 	/*
15424 	 * The new index is unused. Set it in the phyint. However we must not
15425 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15426 	 * changes. The event must be bound to old ifindex value.
15427 	 */
15428 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15429 	    &index, sizeof (index));
15430 
15431 	old_index = phyi->phyint_ifindex;
15432 	phyi->phyint_ifindex = index;
15433 
15434 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15435 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15436 	    &index, &where);
15437 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15438 	    phyi, where);
15439 	rw_exit(&ipst->ips_ill_g_lock);
15440 
15441 	/* Update SCTP's ILL list */
15442 	sctp_ill_reindex(ill, old_index);
15443 
15444 	/* Send the routing sockets message */
15445 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15446 	if (ILL_OTHER(ill))
15447 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15448 
15449 	/* Perhaps ilgs should use this ill */
15450 	update_conn_ill(NULL, ill->ill_ipst);
15451 	return (0);
15452 }
15453 
15454 /* ARGSUSED */
15455 int
15456 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15457     ip_ioctl_cmd_t *ipip, void *ifreq)
15458 {
15459 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15460 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15461 
15462 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15463 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15464 	/* Get the interface index */
15465 	if (ipip->ipi_cmd_type == IF_CMD) {
15466 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15467 	} else {
15468 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15469 	}
15470 	return (0);
15471 }
15472 
15473 /* ARGSUSED */
15474 int
15475 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15476     ip_ioctl_cmd_t *ipip, void *ifreq)
15477 {
15478 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15479 
15480 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15481 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15482 	/* Get the interface zone */
15483 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15484 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15485 	return (0);
15486 }
15487 
15488 /*
15489  * Set the zoneid of an interface.
15490  */
15491 /* ARGSUSED */
15492 int
15493 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15494     ip_ioctl_cmd_t *ipip, void *ifreq)
15495 {
15496 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15497 	int err = 0;
15498 	boolean_t need_up = B_FALSE;
15499 	zone_t *zptr;
15500 	zone_status_t status;
15501 	zoneid_t zoneid;
15502 
15503 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15504 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15505 		if (!is_system_labeled())
15506 			return (ENOTSUP);
15507 		zoneid = GLOBAL_ZONEID;
15508 	}
15509 
15510 	/* cannot assign instance zero to a non-global zone */
15511 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15512 		return (ENOTSUP);
15513 
15514 	/*
15515 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15516 	 * the event of a race with the zone shutdown processing, since IP
15517 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15518 	 * interface will be cleaned up even if the zone is shut down
15519 	 * immediately after the status check. If the interface can't be brought
15520 	 * down right away, and the zone is shut down before the restart
15521 	 * function is called, we resolve the possible races by rechecking the
15522 	 * zone status in the restart function.
15523 	 */
15524 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15525 		return (EINVAL);
15526 	status = zone_status_get(zptr);
15527 	zone_rele(zptr);
15528 
15529 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15530 		return (EINVAL);
15531 
15532 	if (ipif->ipif_flags & IPIF_UP) {
15533 		/*
15534 		 * If the interface is already marked up,
15535 		 * we call ipif_down which will take care
15536 		 * of ditching any IREs that have been set
15537 		 * up based on the old interface address.
15538 		 */
15539 		err = ipif_logical_down(ipif, q, mp);
15540 		if (err == EINPROGRESS)
15541 			return (err);
15542 		(void) ipif_down_tail(ipif);
15543 		need_up = B_TRUE;
15544 	}
15545 
15546 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15547 	return (err);
15548 }
15549 
15550 static int
15551 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15552     queue_t *q, mblk_t *mp, boolean_t need_up)
15553 {
15554 	int	err = 0;
15555 	ip_stack_t	*ipst;
15556 
15557 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15558 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15559 
15560 	if (CONN_Q(q))
15561 		ipst = CONNQ_TO_IPST(q);
15562 	else
15563 		ipst = ILLQ_TO_IPST(q);
15564 
15565 	/*
15566 	 * For exclusive stacks we don't allow a different zoneid than
15567 	 * global.
15568 	 */
15569 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15570 	    zoneid != GLOBAL_ZONEID)
15571 		return (EINVAL);
15572 
15573 	/* Set the new zone id. */
15574 	ipif->ipif_zoneid = zoneid;
15575 
15576 	/* Update sctp list */
15577 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15578 
15579 	/* The default multicast interface might have changed */
15580 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15581 
15582 	if (need_up) {
15583 		/*
15584 		 * Now bring the interface back up.  If this
15585 		 * is the only IPIF for the ILL, ipif_up
15586 		 * will have to re-bind to the device, so
15587 		 * we may get back EINPROGRESS, in which
15588 		 * case, this IOCTL will get completed in
15589 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15590 		 */
15591 		err = ipif_up(ipif, q, mp);
15592 	}
15593 	return (err);
15594 }
15595 
15596 /* ARGSUSED */
15597 int
15598 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15599     ip_ioctl_cmd_t *ipip, void *if_req)
15600 {
15601 	struct lifreq *lifr = (struct lifreq *)if_req;
15602 	zoneid_t zoneid;
15603 	zone_t *zptr;
15604 	zone_status_t status;
15605 
15606 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15607 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15608 		zoneid = GLOBAL_ZONEID;
15609 
15610 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15611 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15612 
15613 	/*
15614 	 * We recheck the zone status to resolve the following race condition:
15615 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15616 	 * 2) hme0:1 is up and can't be brought down right away;
15617 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15618 	 * 3) zone "myzone" is halted; the zone status switches to
15619 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15620 	 * the interfaces to remove - hme0:1 is not returned because it's not
15621 	 * yet in "myzone", so it won't be removed;
15622 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15623 	 * status check here, we would have hme0:1 in "myzone" after it's been
15624 	 * destroyed.
15625 	 * Note that if the status check fails, we need to bring the interface
15626 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15627 	 * ipif_up_done[_v6]().
15628 	 */
15629 	status = ZONE_IS_UNINITIALIZED;
15630 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15631 		status = zone_status_get(zptr);
15632 		zone_rele(zptr);
15633 	}
15634 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15635 		if (ipif->ipif_isv6) {
15636 			(void) ipif_up_done_v6(ipif);
15637 		} else {
15638 			(void) ipif_up_done(ipif);
15639 		}
15640 		return (EINVAL);
15641 	}
15642 
15643 	(void) ipif_down_tail(ipif);
15644 
15645 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15646 	    B_TRUE));
15647 }
15648 
15649 /*
15650  * Return the number of addresses on `ill' with one or more of the values
15651  * in `set' set and all of the values in `clear' clear.
15652  */
15653 static uint_t
15654 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15655 {
15656 	ipif_t	*ipif;
15657 	uint_t	cnt = 0;
15658 
15659 	ASSERT(IAM_WRITER_ILL(ill));
15660 
15661 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15662 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15663 			cnt++;
15664 
15665 	return (cnt);
15666 }
15667 
15668 /*
15669  * Return the number of migratable addresses on `ill' that are under
15670  * application control.
15671  */
15672 uint_t
15673 ill_appaddr_cnt(const ill_t *ill)
15674 {
15675 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15676 	    IPIF_NOFAILOVER));
15677 }
15678 
15679 /*
15680  * Return the number of point-to-point addresses on `ill'.
15681  */
15682 uint_t
15683 ill_ptpaddr_cnt(const ill_t *ill)
15684 {
15685 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15686 }
15687 
15688 /* ARGSUSED */
15689 int
15690 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15691 	ip_ioctl_cmd_t *ipip, void *ifreq)
15692 {
15693 	struct lifreq	*lifr = ifreq;
15694 
15695 	ASSERT(q->q_next == NULL);
15696 	ASSERT(CONN_Q(q));
15697 
15698 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15699 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15700 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15701 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15702 
15703 	return (0);
15704 }
15705 
15706 /* Find the previous ILL in this usesrc group */
15707 static ill_t *
15708 ill_prev_usesrc(ill_t *uill)
15709 {
15710 	ill_t *ill;
15711 
15712 	for (ill = uill->ill_usesrc_grp_next;
15713 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15714 	    ill = ill->ill_usesrc_grp_next)
15715 		/* do nothing */;
15716 	return (ill);
15717 }
15718 
15719 /*
15720  * Release all members of the usesrc group. This routine is called
15721  * from ill_delete when the interface being unplumbed is the
15722  * group head.
15723  *
15724  * This silently clears the usesrc that ifconfig setup.
15725  * An alternative would be to keep that ifindex, and drop packets on the floor
15726  * since no source address can be selected.
15727  * Even if we keep the current semantics, don't need a lock and a linked list.
15728  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15729  * the one that is being removed. Issue is how we return the usesrc users
15730  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15731  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15732  * ill walk, but the walker would need to insert in the ioctl response.
15733  */
15734 static void
15735 ill_disband_usesrc_group(ill_t *uill)
15736 {
15737 	ill_t *next_ill, *tmp_ill;
15738 	ip_stack_t	*ipst = uill->ill_ipst;
15739 
15740 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15741 	next_ill = uill->ill_usesrc_grp_next;
15742 
15743 	do {
15744 		ASSERT(next_ill != NULL);
15745 		tmp_ill = next_ill->ill_usesrc_grp_next;
15746 		ASSERT(tmp_ill != NULL);
15747 		next_ill->ill_usesrc_grp_next = NULL;
15748 		next_ill->ill_usesrc_ifindex = 0;
15749 		next_ill = tmp_ill;
15750 	} while (next_ill->ill_usesrc_ifindex != 0);
15751 	uill->ill_usesrc_grp_next = NULL;
15752 }
15753 
15754 /*
15755  * Remove the client usesrc ILL from the list and relink to a new list
15756  */
15757 int
15758 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15759 {
15760 	ill_t *ill, *tmp_ill;
15761 	ip_stack_t	*ipst = ucill->ill_ipst;
15762 
15763 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15764 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15765 
15766 	/*
15767 	 * Check if the usesrc client ILL passed in is not already
15768 	 * in use as a usesrc ILL i.e one whose source address is
15769 	 * in use OR a usesrc ILL is not already in use as a usesrc
15770 	 * client ILL
15771 	 */
15772 	if ((ucill->ill_usesrc_ifindex == 0) ||
15773 	    (uill->ill_usesrc_ifindex != 0)) {
15774 		return (-1);
15775 	}
15776 
15777 	ill = ill_prev_usesrc(ucill);
15778 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15779 
15780 	/* Remove from the current list */
15781 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15782 		/* Only two elements in the list */
15783 		ASSERT(ill->ill_usesrc_ifindex == 0);
15784 		ill->ill_usesrc_grp_next = NULL;
15785 	} else {
15786 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15787 	}
15788 
15789 	if (ifindex == 0) {
15790 		ucill->ill_usesrc_ifindex = 0;
15791 		ucill->ill_usesrc_grp_next = NULL;
15792 		return (0);
15793 	}
15794 
15795 	ucill->ill_usesrc_ifindex = ifindex;
15796 	tmp_ill = uill->ill_usesrc_grp_next;
15797 	uill->ill_usesrc_grp_next = ucill;
15798 	ucill->ill_usesrc_grp_next =
15799 	    (tmp_ill != NULL) ? tmp_ill : uill;
15800 	return (0);
15801 }
15802 
15803 /*
15804  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15805  * ip.c for locking details.
15806  */
15807 /* ARGSUSED */
15808 int
15809 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15810     ip_ioctl_cmd_t *ipip, void *ifreq)
15811 {
15812 	struct lifreq *lifr = (struct lifreq *)ifreq;
15813 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15814 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15815 	int err = 0, ret;
15816 	uint_t ifindex;
15817 	ipsq_t *ipsq = NULL;
15818 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15819 
15820 	ASSERT(IAM_WRITER_IPIF(ipif));
15821 	ASSERT(q->q_next == NULL);
15822 	ASSERT(CONN_Q(q));
15823 
15824 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15825 
15826 	ifindex = lifr->lifr_index;
15827 	if (ifindex == 0) {
15828 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15829 			/* non usesrc group interface, nothing to reset */
15830 			return (0);
15831 		}
15832 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15833 		/* valid reset request */
15834 		reset_flg = B_TRUE;
15835 	}
15836 
15837 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15838 	if (usesrc_ill == NULL) {
15839 		return (ENXIO);
15840 	}
15841 
15842 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15843 	    NEW_OP, B_TRUE);
15844 	if (ipsq == NULL) {
15845 		err = EINPROGRESS;
15846 		/* Operation enqueued on the ipsq of the usesrc ILL */
15847 		goto done;
15848 	}
15849 
15850 	/* USESRC isn't currently supported with IPMP */
15851 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15852 		err = ENOTSUP;
15853 		goto done;
15854 	}
15855 
15856 	/*
15857 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15858 	 * used by IPMP underlying interfaces, but someone might think it's
15859 	 * more general and try to use it independently with VNI.)
15860 	 */
15861 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15862 		err = ENOTSUP;
15863 		goto done;
15864 	}
15865 
15866 	/*
15867 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15868 	 * already a client then return EINVAL
15869 	 */
15870 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15871 		err = EINVAL;
15872 		goto done;
15873 	}
15874 
15875 	/*
15876 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15877 	 * be then this is a duplicate operation.
15878 	 */
15879 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15880 		err = 0;
15881 		goto done;
15882 	}
15883 
15884 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15885 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15886 	    usesrc_ill->ill_isv6));
15887 
15888 	/*
15889 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15890 	 * and the ill_usesrc_ifindex fields
15891 	 */
15892 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15893 
15894 	if (reset_flg) {
15895 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15896 		if (ret != 0) {
15897 			err = EINVAL;
15898 		}
15899 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15900 		goto done;
15901 	}
15902 
15903 	/*
15904 	 * Four possibilities to consider:
15905 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15906 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15907 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15908 	 * 4. Both are part of their respective usesrc groups
15909 	 */
15910 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15911 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15912 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
15913 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15914 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15915 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
15916 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
15917 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15918 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15919 		/* Insert at head of list */
15920 		usesrc_cli_ill->ill_usesrc_grp_next =
15921 		    usesrc_ill->ill_usesrc_grp_next;
15922 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15923 	} else {
15924 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
15925 		    ifindex);
15926 		if (ret != 0)
15927 			err = EINVAL;
15928 	}
15929 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
15930 
15931 done:
15932 	if (ipsq != NULL)
15933 		ipsq_exit(ipsq);
15934 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
15935 	ill_refrele(usesrc_ill);
15936 
15937 	/* Let conn_ixa caching know that source address selection changed */
15938 	ip_update_source_selection(ipst);
15939 
15940 	return (err);
15941 }
15942 
15943 /*
15944  * comparison function used by avl.
15945  */
15946 static int
15947 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
15948 {
15949 
15950 	uint_t index;
15951 
15952 	ASSERT(phyip != NULL && index_ptr != NULL);
15953 
15954 	index = *((uint_t *)index_ptr);
15955 	/*
15956 	 * let the phyint with the lowest index be on top.
15957 	 */
15958 	if (((phyint_t *)phyip)->phyint_ifindex < index)
15959 		return (1);
15960 	if (((phyint_t *)phyip)->phyint_ifindex > index)
15961 		return (-1);
15962 	return (0);
15963 }
15964 
15965 /*
15966  * comparison function used by avl.
15967  */
15968 static int
15969 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
15970 {
15971 	ill_t *ill;
15972 	int res = 0;
15973 
15974 	ASSERT(phyip != NULL && name_ptr != NULL);
15975 
15976 	if (((phyint_t *)phyip)->phyint_illv4)
15977 		ill = ((phyint_t *)phyip)->phyint_illv4;
15978 	else
15979 		ill = ((phyint_t *)phyip)->phyint_illv6;
15980 	ASSERT(ill != NULL);
15981 
15982 	res = strcmp(ill->ill_name, (char *)name_ptr);
15983 	if (res > 0)
15984 		return (1);
15985 	else if (res < 0)
15986 		return (-1);
15987 	return (0);
15988 }
15989 
15990 /*
15991  * This function is called on the unplumb path via ill_glist_delete() when
15992  * there are no ills left on the phyint and thus the phyint can be freed.
15993  */
15994 static void
15995 phyint_free(phyint_t *phyi)
15996 {
15997 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
15998 
15999 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16000 
16001 	/*
16002 	 * If this phyint was an IPMP meta-interface, blow away the group.
16003 	 * This is safe to do because all of the illgrps have already been
16004 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16005 	 * If we're cleaning up as a result of failed initialization,
16006 	 * phyint_grp may be NULL.
16007 	 */
16008 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16009 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16010 		ipmp_grp_destroy(phyi->phyint_grp);
16011 		phyi->phyint_grp = NULL;
16012 		rw_exit(&ipst->ips_ipmp_lock);
16013 	}
16014 
16015 	/*
16016 	 * If this interface was under IPMP, take it out of the group.
16017 	 */
16018 	if (phyi->phyint_grp != NULL)
16019 		ipmp_phyint_leave_grp(phyi);
16020 
16021 	/*
16022 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16023 	 * will be freed in ipsq_exit().
16024 	 */
16025 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16026 	phyi->phyint_name[0] = '\0';
16027 
16028 	mi_free(phyi);
16029 }
16030 
16031 /*
16032  * Attach the ill to the phyint structure which can be shared by both
16033  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16034  * function is called from ipif_set_values and ill_lookup_on_name (for
16035  * loopback) where we know the name of the ill. We lookup the ill and if
16036  * there is one present already with the name use that phyint. Otherwise
16037  * reuse the one allocated by ill_init.
16038  */
16039 static void
16040 ill_phyint_reinit(ill_t *ill)
16041 {
16042 	boolean_t isv6 = ill->ill_isv6;
16043 	phyint_t *phyi_old;
16044 	phyint_t *phyi;
16045 	avl_index_t where = 0;
16046 	ill_t	*ill_other = NULL;
16047 	ip_stack_t	*ipst = ill->ill_ipst;
16048 
16049 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16050 
16051 	phyi_old = ill->ill_phyint;
16052 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16053 	    phyi_old->phyint_illv6 == NULL));
16054 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16055 	    phyi_old->phyint_illv4 == NULL));
16056 	ASSERT(phyi_old->phyint_ifindex == 0);
16057 
16058 	/*
16059 	 * Now that our ill has a name, set it in the phyint.
16060 	 */
16061 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16062 
16063 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16064 	    ill->ill_name, &where);
16065 
16066 	/*
16067 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16068 	 *    the global list of ills. So no other thread could have located
16069 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16070 	 * 2. Now locate the other protocol instance of this ill.
16071 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16072 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16073 	 *    of neither ill can change.
16074 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16075 	 *    other ill.
16076 	 * 5. Release all locks.
16077 	 */
16078 
16079 	/*
16080 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16081 	 * we are initializing IPv4.
16082 	 */
16083 	if (phyi != NULL) {
16084 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16085 		ASSERT(ill_other->ill_phyint != NULL);
16086 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16087 		    (!isv6 && ill_other->ill_isv6));
16088 		GRAB_ILL_LOCKS(ill, ill_other);
16089 		/*
16090 		 * We are potentially throwing away phyint_flags which
16091 		 * could be different from the one that we obtain from
16092 		 * ill_other->ill_phyint. But it is okay as we are assuming
16093 		 * that the state maintained within IP is correct.
16094 		 */
16095 		mutex_enter(&phyi->phyint_lock);
16096 		if (isv6) {
16097 			ASSERT(phyi->phyint_illv6 == NULL);
16098 			phyi->phyint_illv6 = ill;
16099 		} else {
16100 			ASSERT(phyi->phyint_illv4 == NULL);
16101 			phyi->phyint_illv4 = ill;
16102 		}
16103 
16104 		/*
16105 		 * Delete the old phyint and make its ipsq eligible
16106 		 * to be freed in ipsq_exit().
16107 		 */
16108 		phyi_old->phyint_illv4 = NULL;
16109 		phyi_old->phyint_illv6 = NULL;
16110 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16111 		phyi_old->phyint_name[0] = '\0';
16112 		mi_free(phyi_old);
16113 	} else {
16114 		mutex_enter(&ill->ill_lock);
16115 		/*
16116 		 * We don't need to acquire any lock, since
16117 		 * the ill is not yet visible globally  and we
16118 		 * have not yet released the ill_g_lock.
16119 		 */
16120 		phyi = phyi_old;
16121 		mutex_enter(&phyi->phyint_lock);
16122 		/* XXX We need a recovery strategy here. */
16123 		if (!phyint_assign_ifindex(phyi, ipst))
16124 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16125 
16126 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16127 		    (void *)phyi, where);
16128 
16129 		(void) avl_find(&ipst->ips_phyint_g_list->
16130 		    phyint_list_avl_by_index,
16131 		    &phyi->phyint_ifindex, &where);
16132 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16133 		    (void *)phyi, where);
16134 	}
16135 
16136 	/*
16137 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16138 	 * pending mp is not affected because that is per ill basis.
16139 	 */
16140 	ill->ill_phyint = phyi;
16141 
16142 	/*
16143 	 * Now that the phyint's ifindex has been assigned, complete the
16144 	 * remaining
16145 	 */
16146 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16147 	if (ill->ill_isv6) {
16148 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16149 		    ill->ill_phyint->phyint_ifindex;
16150 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16151 	} else {
16152 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16153 	}
16154 
16155 	/*
16156 	 * Generate an event within the hooks framework to indicate that
16157 	 * a new interface has just been added to IP.  For this event to
16158 	 * be generated, the network interface must, at least, have an
16159 	 * ifindex assigned to it.  (We don't generate the event for
16160 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16161 	 *
16162 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16163 	 * that the ordering of delivered events to listeners matches the
16164 	 * order of them in the kernel.
16165 	 */
16166 	if (!IS_LOOPBACK(ill)) {
16167 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16168 		    ill->ill_name_length);
16169 	}
16170 	RELEASE_ILL_LOCKS(ill, ill_other);
16171 	mutex_exit(&phyi->phyint_lock);
16172 }
16173 
16174 /*
16175  * Notify any downstream modules of the name of this interface.
16176  * An M_IOCTL is used even though we don't expect a successful reply.
16177  * Any reply message from the driver (presumably an M_IOCNAK) will
16178  * eventually get discarded somewhere upstream.  The message format is
16179  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16180  * to IP.
16181  */
16182 static void
16183 ip_ifname_notify(ill_t *ill, queue_t *q)
16184 {
16185 	mblk_t *mp1, *mp2;
16186 	struct iocblk *iocp;
16187 	struct lifreq *lifr;
16188 
16189 	mp1 = mkiocb(SIOCSLIFNAME);
16190 	if (mp1 == NULL)
16191 		return;
16192 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16193 	if (mp2 == NULL) {
16194 		freeb(mp1);
16195 		return;
16196 	}
16197 
16198 	mp1->b_cont = mp2;
16199 	iocp = (struct iocblk *)mp1->b_rptr;
16200 	iocp->ioc_count = sizeof (struct lifreq);
16201 
16202 	lifr = (struct lifreq *)mp2->b_rptr;
16203 	mp2->b_wptr += sizeof (struct lifreq);
16204 	bzero(lifr, sizeof (struct lifreq));
16205 
16206 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16207 	lifr->lifr_ppa = ill->ill_ppa;
16208 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16209 
16210 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16211 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16212 	putnext(q, mp1);
16213 }
16214 
16215 static int
16216 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16217 {
16218 	int		err;
16219 	ip_stack_t	*ipst = ill->ill_ipst;
16220 	phyint_t	*phyi = ill->ill_phyint;
16221 
16222 	/* Set the obsolete NDD per-interface forwarding name. */
16223 	err = ill_set_ndd_name(ill);
16224 	if (err != 0) {
16225 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16226 		    err);
16227 	}
16228 
16229 	/*
16230 	 * Now that ill_name is set, the configuration for the IPMP
16231 	 * meta-interface can be performed.
16232 	 */
16233 	if (IS_IPMP(ill)) {
16234 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16235 		/*
16236 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16237 		 * meta-interface and we need to create the IPMP group.
16238 		 */
16239 		if (phyi->phyint_grp == NULL) {
16240 			/*
16241 			 * If someone has renamed another IPMP group to have
16242 			 * the same name as our interface, bail.
16243 			 */
16244 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16245 				rw_exit(&ipst->ips_ipmp_lock);
16246 				return (EEXIST);
16247 			}
16248 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16249 			if (phyi->phyint_grp == NULL) {
16250 				rw_exit(&ipst->ips_ipmp_lock);
16251 				return (ENOMEM);
16252 			}
16253 		}
16254 		rw_exit(&ipst->ips_ipmp_lock);
16255 	}
16256 
16257 	/* Tell downstream modules where they are. */
16258 	ip_ifname_notify(ill, q);
16259 
16260 	/*
16261 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16262 	 * Error cases are ENOMEM ...
16263 	 */
16264 	err = ill_dl_phys(ill, ipif, mp, q);
16265 
16266 	if (ill->ill_isv6) {
16267 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16268 		if (ipst->ips_mld_slowtimeout_id == 0) {
16269 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16270 			    (void *)ipst,
16271 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16272 		}
16273 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16274 	} else {
16275 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16276 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16277 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16278 			    (void *)ipst,
16279 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16280 		}
16281 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16282 	}
16283 
16284 	return (err);
16285 }
16286 
16287 /*
16288  * Common routine for ppa and ifname setting. Should be called exclusive.
16289  *
16290  * Returns EINPROGRESS when mp has been consumed by queueing it on
16291  * ipx_pending_mp and the ioctl will complete in ip_rput.
16292  *
16293  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16294  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16295  * For SLIFNAME, we pass these values back to the userland.
16296  */
16297 static int
16298 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16299 {
16300 	ill_t	*ill;
16301 	ipif_t	*ipif;
16302 	ipsq_t	*ipsq;
16303 	char	*ppa_ptr;
16304 	char	*old_ptr;
16305 	char	old_char;
16306 	int	error;
16307 	ip_stack_t	*ipst;
16308 
16309 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16310 	ASSERT(q->q_next != NULL);
16311 	ASSERT(interf_name != NULL);
16312 
16313 	ill = (ill_t *)q->q_ptr;
16314 	ipst = ill->ill_ipst;
16315 
16316 	ASSERT(ill->ill_ipst != NULL);
16317 	ASSERT(ill->ill_name[0] == '\0');
16318 	ASSERT(IAM_WRITER_ILL(ill));
16319 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16320 	ASSERT(ill->ill_ppa == UINT_MAX);
16321 
16322 	ill->ill_defend_start = ill->ill_defend_count = 0;
16323 	/* The ppa is sent down by ifconfig or is chosen */
16324 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16325 		return (EINVAL);
16326 	}
16327 
16328 	/*
16329 	 * make sure ppa passed in is same as ppa in the name.
16330 	 * This check is not made when ppa == UINT_MAX in that case ppa
16331 	 * in the name could be anything. System will choose a ppa and
16332 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16333 	 */
16334 	if (*new_ppa_ptr != UINT_MAX) {
16335 		/* stoi changes the pointer */
16336 		old_ptr = ppa_ptr;
16337 		/*
16338 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16339 		 * (they don't have an externally visible ppa).  We assign one
16340 		 * here so that we can manage the interface.  Note that in
16341 		 * the past this value was always 0 for DLPI 1 drivers.
16342 		 */
16343 		if (*new_ppa_ptr == 0)
16344 			*new_ppa_ptr = stoi(&old_ptr);
16345 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16346 			return (EINVAL);
16347 	}
16348 	/*
16349 	 * terminate string before ppa
16350 	 * save char at that location.
16351 	 */
16352 	old_char = ppa_ptr[0];
16353 	ppa_ptr[0] = '\0';
16354 
16355 	ill->ill_ppa = *new_ppa_ptr;
16356 	/*
16357 	 * Finish as much work now as possible before calling ill_glist_insert
16358 	 * which makes the ill globally visible and also merges it with the
16359 	 * other protocol instance of this phyint. The remaining work is
16360 	 * done after entering the ipsq which may happen sometime later.
16361 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16362 	 */
16363 	ipif = ill->ill_ipif;
16364 
16365 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16366 	ipif_assign_seqid(ipif);
16367 
16368 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16369 		ill->ill_flags |= ILLF_IPV4;
16370 
16371 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16372 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16373 
16374 	if (ill->ill_flags & ILLF_IPV6) {
16375 
16376 		ill->ill_isv6 = B_TRUE;
16377 		ill_set_inputfn(ill);
16378 		if (ill->ill_rq != NULL) {
16379 			ill->ill_rq->q_qinfo = &iprinitv6;
16380 		}
16381 
16382 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16383 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16384 		ipif->ipif_v6subnet = ipv6_all_zeros;
16385 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16386 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16387 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16388 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16389 		/*
16390 		 * point-to-point or Non-mulicast capable
16391 		 * interfaces won't do NUD unless explicitly
16392 		 * configured to do so.
16393 		 */
16394 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16395 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16396 			ill->ill_flags |= ILLF_NONUD;
16397 		}
16398 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16399 		if (ill->ill_flags & ILLF_NOARP) {
16400 			/*
16401 			 * Note: xresolv interfaces will eventually need
16402 			 * NOARP set here as well, but that will require
16403 			 * those external resolvers to have some
16404 			 * knowledge of that flag and act appropriately.
16405 			 * Not to be changed at present.
16406 			 */
16407 			ill->ill_flags &= ~ILLF_NOARP;
16408 		}
16409 		/*
16410 		 * Set the ILLF_ROUTER flag according to the global
16411 		 * IPv6 forwarding policy.
16412 		 */
16413 		if (ipst->ips_ipv6_forward != 0)
16414 			ill->ill_flags |= ILLF_ROUTER;
16415 	} else if (ill->ill_flags & ILLF_IPV4) {
16416 		ill->ill_isv6 = B_FALSE;
16417 		ill_set_inputfn(ill);
16418 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16419 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16420 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16421 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16422 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16423 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16424 		/*
16425 		 * Set the ILLF_ROUTER flag according to the global
16426 		 * IPv4 forwarding policy.
16427 		 */
16428 		if (ipst->ips_ip_g_forward != 0)
16429 			ill->ill_flags |= ILLF_ROUTER;
16430 	}
16431 
16432 	ASSERT(ill->ill_phyint != NULL);
16433 
16434 	/*
16435 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16436 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16437 	 */
16438 	if (!ill_allocate_mibs(ill))
16439 		return (ENOMEM);
16440 
16441 	/*
16442 	 * Pick a default sap until we get the DL_INFO_ACK back from
16443 	 * the driver.
16444 	 */
16445 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16446 	    ill->ill_media->ip_m_ipv4sap;
16447 
16448 	ill->ill_ifname_pending = 1;
16449 	ill->ill_ifname_pending_err = 0;
16450 
16451 	/*
16452 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16453 	 * that were joined while this ill was not bound to the DLPI link need
16454 	 * to be recovered by ill_recover_multicast().
16455 	 */
16456 	ill->ill_need_recover_multicast = 1;
16457 
16458 	ill_refhold(ill);
16459 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16460 	if ((error = ill_glist_insert(ill, interf_name,
16461 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16462 		ill->ill_ppa = UINT_MAX;
16463 		ill->ill_name[0] = '\0';
16464 		/*
16465 		 * undo null termination done above.
16466 		 */
16467 		ppa_ptr[0] = old_char;
16468 		rw_exit(&ipst->ips_ill_g_lock);
16469 		ill_refrele(ill);
16470 		return (error);
16471 	}
16472 
16473 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16474 
16475 	/*
16476 	 * When we return the buffer pointed to by interf_name should contain
16477 	 * the same name as in ill_name.
16478 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16479 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16480 	 * so copy full name and update the ppa ptr.
16481 	 * When ppa passed in != UINT_MAX all values are correct just undo
16482 	 * null termination, this saves a bcopy.
16483 	 */
16484 	if (*new_ppa_ptr == UINT_MAX) {
16485 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16486 		*new_ppa_ptr = ill->ill_ppa;
16487 	} else {
16488 		/*
16489 		 * undo null termination done above.
16490 		 */
16491 		ppa_ptr[0] = old_char;
16492 	}
16493 
16494 	/* Let SCTP know about this ILL */
16495 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16496 
16497 	/*
16498 	 * ill_glist_insert has made the ill visible globally, and
16499 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16500 	 * we need to hold the ips_ill_g_lock across the call to enter the
16501 	 * ipsq to enforce atomicity and prevent reordering. In the event
16502 	 * the ipsq has changed, and if the new ipsq is currently busy,
16503 	 * we need to make sure that this half-completed ioctl is ahead of
16504 	 * any subsequent ioctl. We achieve this by not dropping the
16505 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16506 	 * ensuring that new ioctls can't start.
16507 	 */
16508 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16509 	    B_TRUE);
16510 
16511 	rw_exit(&ipst->ips_ill_g_lock);
16512 	ill_refrele(ill);
16513 	if (ipsq == NULL)
16514 		return (EINPROGRESS);
16515 
16516 	/*
16517 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16518 	 */
16519 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16520 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16521 	else
16522 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16523 
16524 	error = ipif_set_values_tail(ill, ipif, mp, q);
16525 	ipsq_exit(ipsq);
16526 	if (error != 0 && error != EINPROGRESS) {
16527 		/*
16528 		 * restore previous values
16529 		 */
16530 		ill->ill_isv6 = B_FALSE;
16531 		ill_set_inputfn(ill);
16532 	}
16533 	return (error);
16534 }
16535 
16536 void
16537 ipif_init(ip_stack_t *ipst)
16538 {
16539 	int i;
16540 
16541 	for (i = 0; i < MAX_G_HEADS; i++) {
16542 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16543 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16544 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16545 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16546 	}
16547 
16548 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16549 	    ill_phyint_compare_index,
16550 	    sizeof (phyint_t),
16551 	    offsetof(struct phyint, phyint_avl_by_index));
16552 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16553 	    ill_phyint_compare_name,
16554 	    sizeof (phyint_t),
16555 	    offsetof(struct phyint, phyint_avl_by_name));
16556 }
16557 
16558 /*
16559  * Save enough information so that we can recreate the IRE if
16560  * the interface goes down and then up.
16561  */
16562 void
16563 ill_save_ire(ill_t *ill, ire_t *ire)
16564 {
16565 	mblk_t	*save_mp;
16566 
16567 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16568 	if (save_mp != NULL) {
16569 		ifrt_t	*ifrt;
16570 
16571 		save_mp->b_wptr += sizeof (ifrt_t);
16572 		ifrt = (ifrt_t *)save_mp->b_rptr;
16573 		bzero(ifrt, sizeof (ifrt_t));
16574 		ifrt->ifrt_type = ire->ire_type;
16575 		if (ire->ire_ipversion == IPV4_VERSION) {
16576 			ASSERT(!ill->ill_isv6);
16577 			ifrt->ifrt_addr = ire->ire_addr;
16578 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16579 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16580 			ifrt->ifrt_mask = ire->ire_mask;
16581 		} else {
16582 			ASSERT(ill->ill_isv6);
16583 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16584 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16585 			mutex_enter(&ire->ire_lock);
16586 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16587 			mutex_exit(&ire->ire_lock);
16588 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16589 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16590 		}
16591 		ifrt->ifrt_flags = ire->ire_flags;
16592 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16593 		mutex_enter(&ill->ill_saved_ire_lock);
16594 		save_mp->b_cont = ill->ill_saved_ire_mp;
16595 		ill->ill_saved_ire_mp = save_mp;
16596 		ill->ill_saved_ire_cnt++;
16597 		mutex_exit(&ill->ill_saved_ire_lock);
16598 	}
16599 }
16600 
16601 /*
16602  * Remove one entry from ill_saved_ire_mp.
16603  */
16604 void
16605 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16606 {
16607 	mblk_t	**mpp;
16608 	mblk_t	*mp;
16609 	ifrt_t	*ifrt;
16610 
16611 	/* Remove from ill_saved_ire_mp list if it is there */
16612 	mutex_enter(&ill->ill_saved_ire_lock);
16613 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16614 	    mpp = &(*mpp)->b_cont) {
16615 		in6_addr_t	gw_addr_v6;
16616 
16617 		/*
16618 		 * On a given ill, the tuple of address, gateway, mask,
16619 		 * ire_type, and zoneid is unique for each saved IRE.
16620 		 */
16621 		mp = *mpp;
16622 		ifrt = (ifrt_t *)mp->b_rptr;
16623 		/* ire_gateway_addr_v6 can change - need lock */
16624 		mutex_enter(&ire->ire_lock);
16625 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16626 		mutex_exit(&ire->ire_lock);
16627 
16628 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16629 		    ifrt->ifrt_type != ire->ire_type)
16630 			continue;
16631 
16632 		if (ill->ill_isv6 ?
16633 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16634 		    &ire->ire_addr_v6) &&
16635 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16636 		    &gw_addr_v6) &&
16637 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16638 		    &ire->ire_mask_v6)) :
16639 		    (ifrt->ifrt_addr == ire->ire_addr &&
16640 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16641 		    ifrt->ifrt_mask == ire->ire_mask)) {
16642 			*mpp = mp->b_cont;
16643 			ill->ill_saved_ire_cnt--;
16644 			freeb(mp);
16645 			break;
16646 		}
16647 	}
16648 	mutex_exit(&ill->ill_saved_ire_lock);
16649 }
16650 
16651 /*
16652  * IP multirouting broadcast routes handling
16653  * Append CGTP broadcast IREs to regular ones created
16654  * at ifconfig time.
16655  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16656  * the destination and the gateway are broadcast addresses.
16657  * The caller has verified that the destination is an IRE_BROADCAST and that
16658  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16659  * we create a MULTIRT IRE_BROADCAST.
16660  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16661  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16662  */
16663 static void
16664 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16665 {
16666 	ire_t *ire_prim;
16667 
16668 	ASSERT(ire != NULL);
16669 
16670 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16671 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16672 	    NULL);
16673 	if (ire_prim != NULL) {
16674 		/*
16675 		 * We are in the special case of broadcasts for
16676 		 * CGTP. We add an IRE_BROADCAST that holds
16677 		 * the RTF_MULTIRT flag, the destination
16678 		 * address and the low level
16679 		 * info of ire_prim. In other words, CGTP
16680 		 * broadcast is added to the redundant ipif.
16681 		 */
16682 		ill_t *ill_prim;
16683 		ire_t  *bcast_ire;
16684 
16685 		ill_prim = ire_prim->ire_ill;
16686 
16687 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16688 		    (void *)ire_prim, (void *)ill_prim));
16689 
16690 		bcast_ire = ire_create(
16691 		    (uchar_t *)&ire->ire_addr,
16692 		    (uchar_t *)&ip_g_all_ones,
16693 		    (uchar_t *)&ire->ire_gateway_addr,
16694 		    IRE_BROADCAST,
16695 		    ill_prim,
16696 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16697 		    ire->ire_flags | RTF_KERNEL,
16698 		    NULL,
16699 		    ipst);
16700 
16701 		/*
16702 		 * Here we assume that ire_add does head insertion so that
16703 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16704 		 */
16705 		if (bcast_ire != NULL) {
16706 			if (ire->ire_flags & RTF_SETSRC) {
16707 				bcast_ire->ire_setsrc_addr =
16708 				    ire->ire_setsrc_addr;
16709 			}
16710 			bcast_ire = ire_add(bcast_ire);
16711 			if (bcast_ire != NULL) {
16712 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16713 				    "added bcast_ire %p\n",
16714 				    (void *)bcast_ire));
16715 
16716 				ill_save_ire(ill_prim, bcast_ire);
16717 				ire_refrele(bcast_ire);
16718 			}
16719 		}
16720 		ire_refrele(ire_prim);
16721 	}
16722 }
16723 
16724 /*
16725  * IP multirouting broadcast routes handling
16726  * Remove the broadcast ire.
16727  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16728  * the destination and the gateway are broadcast addresses.
16729  * The caller has only verified that RTF_MULTIRT was set. We check
16730  * that the destination is broadcast and that the gateway is a broadcast
16731  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16732  */
16733 static void
16734 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16735 {
16736 	ASSERT(ire != NULL);
16737 
16738 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16739 		ire_t *ire_prim;
16740 
16741 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16742 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16743 		    ipst, NULL);
16744 		if (ire_prim != NULL) {
16745 			ill_t *ill_prim;
16746 			ire_t  *bcast_ire;
16747 
16748 			ill_prim = ire_prim->ire_ill;
16749 
16750 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16751 			    "ire_prim %p, ill_prim %p\n",
16752 			    (void *)ire_prim, (void *)ill_prim));
16753 
16754 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16755 			    ire->ire_gateway_addr, IRE_BROADCAST,
16756 			    ill_prim, ALL_ZONES, NULL,
16757 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16758 			    MATCH_IRE_MASK, 0, ipst, NULL);
16759 
16760 			if (bcast_ire != NULL) {
16761 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16762 				    "looked up bcast_ire %p\n",
16763 				    (void *)bcast_ire));
16764 				ill_remove_saved_ire(bcast_ire->ire_ill,
16765 				    bcast_ire);
16766 				ire_delete(bcast_ire);
16767 				ire_refrele(bcast_ire);
16768 			}
16769 			ire_refrele(ire_prim);
16770 		}
16771 	}
16772 }
16773 
16774 /*
16775  * Derive an interface id from the link layer address.
16776  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16777  */
16778 static void
16779 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16780 {
16781 	char		*addr;
16782 
16783 	/*
16784 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16785 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16786 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16787 	 * interface ID on IPv6 interfaces above links that actually have real
16788 	 * Ethernet addresses.
16789 	 */
16790 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16791 		/* Form EUI-64 like address */
16792 		addr = (char *)&v6addr->s6_addr32[2];
16793 		bcopy(ill->ill_phys_addr, addr, 3);
16794 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16795 		addr[3] = (char)0xff;
16796 		addr[4] = (char)0xfe;
16797 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16798 	}
16799 }
16800 
16801 /* ARGSUSED */
16802 static void
16803 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16804 {
16805 }
16806 
16807 typedef struct ipmp_ifcookie {
16808 	uint32_t	ic_hostid;
16809 	char		ic_ifname[LIFNAMSIZ];
16810 	char		ic_zonename[ZONENAME_MAX];
16811 } ipmp_ifcookie_t;
16812 
16813 /*
16814  * Construct a pseudo-random interface ID for the IPMP interface that's both
16815  * predictable and (almost) guaranteed to be unique.
16816  */
16817 static void
16818 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16819 {
16820 	zone_t		*zp;
16821 	uint8_t		*addr;
16822 	uchar_t		hash[16];
16823 	ulong_t 	hostid;
16824 	MD5_CTX		ctx;
16825 	ipmp_ifcookie_t	ic = { 0 };
16826 
16827 	ASSERT(IS_IPMP(ill));
16828 
16829 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16830 	ic.ic_hostid = htonl((uint32_t)hostid);
16831 
16832 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16833 
16834 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16835 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16836 		zone_rele(zp);
16837 	}
16838 
16839 	MD5Init(&ctx);
16840 	MD5Update(&ctx, &ic, sizeof (ic));
16841 	MD5Final(hash, &ctx);
16842 
16843 	/*
16844 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16845 	 */
16846 	addr = &v6addr->s6_addr8[8];
16847 	bcopy(hash + 8, addr, sizeof (uint64_t));
16848 	addr[0] &= ~0x2;				/* set local bit */
16849 }
16850 
16851 /*
16852  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16853  */
16854 static void
16855 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16856 {
16857 	phyint_t *phyi = ill->ill_phyint;
16858 
16859 	/*
16860 	 * Check PHYI_MULTI_BCAST and length of physical
16861 	 * address to determine if we use the mapping or the
16862 	 * broadcast address.
16863 	 */
16864 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16865 	    ill->ill_phys_addr_length != ETHERADDRL) {
16866 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16867 		return;
16868 	}
16869 	m_physaddr[0] = 0x33;
16870 	m_physaddr[1] = 0x33;
16871 	m_physaddr[2] = m_ip6addr[12];
16872 	m_physaddr[3] = m_ip6addr[13];
16873 	m_physaddr[4] = m_ip6addr[14];
16874 	m_physaddr[5] = m_ip6addr[15];
16875 }
16876 
16877 /*
16878  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16879  */
16880 static void
16881 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16882 {
16883 	phyint_t *phyi = ill->ill_phyint;
16884 
16885 	/*
16886 	 * Check PHYI_MULTI_BCAST and length of physical
16887 	 * address to determine if we use the mapping or the
16888 	 * broadcast address.
16889 	 */
16890 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16891 	    ill->ill_phys_addr_length != ETHERADDRL) {
16892 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16893 		return;
16894 	}
16895 	m_physaddr[0] = 0x01;
16896 	m_physaddr[1] = 0x00;
16897 	m_physaddr[2] = 0x5e;
16898 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16899 	m_physaddr[4] = m_ipaddr[2];
16900 	m_physaddr[5] = m_ipaddr[3];
16901 }
16902 
16903 /* ARGSUSED */
16904 static void
16905 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16906 {
16907 	/*
16908 	 * for the MULTI_BCAST case and other cases when we want to
16909 	 * use the link-layer broadcast address for multicast.
16910 	 */
16911 	uint8_t	*bphys_addr;
16912 	dl_unitdata_req_t *dlur;
16913 
16914 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16915 	if (ill->ill_sap_length < 0) {
16916 		bphys_addr = (uchar_t *)dlur +
16917 		    dlur->dl_dest_addr_offset;
16918 	} else  {
16919 		bphys_addr = (uchar_t *)dlur +
16920 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
16921 	}
16922 
16923 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
16924 }
16925 
16926 /*
16927  * Derive IPoIB interface id from the link layer address.
16928  */
16929 static void
16930 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16931 {
16932 	char		*addr;
16933 
16934 	ASSERT(ill->ill_phys_addr_length == 20);
16935 	addr = (char *)&v6addr->s6_addr32[2];
16936 	bcopy(ill->ill_phys_addr + 12, addr, 8);
16937 	/*
16938 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
16939 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
16940 	 * rules. In these cases, the IBA considers these GUIDs to be in
16941 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
16942 	 * required; vendors are required not to assign global EUI-64's
16943 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
16944 	 * of the interface identifier. Whether the GUID is in modified
16945 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
16946 	 * bit set to 1.
16947 	 */
16948 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
16949 }
16950 
16951 /*
16952  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
16953  * Note on mapping from multicast IP addresses to IPoIB multicast link
16954  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
16955  * The format of an IPoIB multicast address is:
16956  *
16957  *  4 byte QPN      Scope Sign.  Pkey
16958  * +--------------------------------------------+
16959  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
16960  * +--------------------------------------------+
16961  *
16962  * The Scope and Pkey components are properties of the IBA port and
16963  * network interface. They can be ascertained from the broadcast address.
16964  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
16965  */
16966 static void
16967 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16968 {
16969 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16970 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
16971 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16972 	uint8_t	*bphys_addr;
16973 	dl_unitdata_req_t *dlur;
16974 
16975 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
16976 
16977 	/*
16978 	 * RFC 4391: IPv4 MGID is 28-bit long.
16979 	 */
16980 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
16981 	m_physaddr[17] = m_ipaddr[1];
16982 	m_physaddr[18] = m_ipaddr[2];
16983 	m_physaddr[19] = m_ipaddr[3];
16984 
16985 
16986 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16987 	if (ill->ill_sap_length < 0) {
16988 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
16989 	} else  {
16990 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
16991 		    ill->ill_sap_length;
16992 	}
16993 	/*
16994 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
16995 	 */
16996 	m_physaddr[5] = bphys_addr[5];
16997 	m_physaddr[8] = bphys_addr[8];
16998 	m_physaddr[9] = bphys_addr[9];
16999 }
17000 
17001 static void
17002 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17003 {
17004 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17005 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17006 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17007 	uint8_t	*bphys_addr;
17008 	dl_unitdata_req_t *dlur;
17009 
17010 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17011 
17012 	/*
17013 	 * RFC 4391: IPv4 MGID is 80-bit long.
17014 	 */
17015 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17016 
17017 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17018 	if (ill->ill_sap_length < 0) {
17019 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17020 	} else  {
17021 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17022 		    ill->ill_sap_length;
17023 	}
17024 	/*
17025 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17026 	 */
17027 	m_physaddr[5] = bphys_addr[5];
17028 	m_physaddr[8] = bphys_addr[8];
17029 	m_physaddr[9] = bphys_addr[9];
17030 }
17031 
17032 /*
17033  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17034  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17035  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17036  * of RFC4213.
17037  */
17038 static void
17039 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17040 {
17041 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17042 	v6addr->s6_addr32[2] = 0;
17043 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17044 }
17045 
17046 /*
17047  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17048  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17049  * id.
17050  */
17051 static void
17052 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17053 {
17054 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17055 
17056 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17057 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17058 }
17059 
17060 static void
17061 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17062 {
17063 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17064 }
17065 
17066 static void
17067 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17068 {
17069 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17070 }
17071 
17072 static void
17073 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17074 {
17075 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17076 }
17077 
17078 static void
17079 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17080 {
17081 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17082 }
17083 
17084 /*
17085  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17086  * Returns an held ill, or NULL.
17087  */
17088 ill_t *
17089 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17090     ip_stack_t *ipst)
17091 {
17092 	ill_t	*ill;
17093 	ipif_t	*ipif;
17094 
17095 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17096 	if (ill == NULL)
17097 		return (NULL);
17098 
17099 	mutex_enter(&ill->ill_lock);
17100 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17101 		if (IPIF_IS_CONDEMNED(ipif))
17102 			continue;
17103 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17104 		    ipif->ipif_zoneid != ALL_ZONES)
17105 			continue;
17106 
17107 		mutex_exit(&ill->ill_lock);
17108 		return (ill);
17109 	}
17110 	mutex_exit(&ill->ill_lock);
17111 	ill_refrele(ill);
17112 	return (NULL);
17113 }
17114 
17115 /*
17116  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17117  * If a pointer to an ipif_t is returned then the caller will need to do
17118  * an ill_refrele().
17119  */
17120 ipif_t *
17121 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17122     ip_stack_t *ipst)
17123 {
17124 	ipif_t *ipif;
17125 	ill_t *ill;
17126 
17127 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17128 	if (ill == NULL)
17129 		return (NULL);
17130 
17131 	mutex_enter(&ill->ill_lock);
17132 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17133 		mutex_exit(&ill->ill_lock);
17134 		ill_refrele(ill);
17135 		return (NULL);
17136 	}
17137 
17138 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17139 		if (!IPIF_CAN_LOOKUP(ipif))
17140 			continue;
17141 		if (lifidx == ipif->ipif_id) {
17142 			ipif_refhold_locked(ipif);
17143 			break;
17144 		}
17145 	}
17146 
17147 	mutex_exit(&ill->ill_lock);
17148 	ill_refrele(ill);
17149 	return (ipif);
17150 }
17151 
17152 /*
17153  * Set ill_inputfn based on the current know state.
17154  * This needs to be called when any of the factors taken into
17155  * account changes.
17156  */
17157 void
17158 ill_set_inputfn(ill_t *ill)
17159 {
17160 	ip_stack_t	*ipst = ill->ill_ipst;
17161 
17162 	if (ill->ill_isv6) {
17163 		if (is_system_labeled())
17164 			ill->ill_inputfn = ill_input_full_v6;
17165 		else
17166 			ill->ill_inputfn = ill_input_short_v6;
17167 	} else {
17168 		if (is_system_labeled())
17169 			ill->ill_inputfn = ill_input_full_v4;
17170 		else if (ill->ill_dhcpinit != 0)
17171 			ill->ill_inputfn = ill_input_full_v4;
17172 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17173 		    != NULL)
17174 			ill->ill_inputfn = ill_input_full_v4;
17175 		else if (ipst->ips_ip_cgtp_filter &&
17176 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17177 			ill->ill_inputfn = ill_input_full_v4;
17178 		else
17179 			ill->ill_inputfn = ill_input_short_v4;
17180 	}
17181 }
17182 
17183 /*
17184  * Re-evaluate ill_inputfn for all the IPv4 ills.
17185  * Used when RSVP and CGTP comes and goes.
17186  */
17187 void
17188 ill_set_inputfn_all(ip_stack_t *ipst)
17189 {
17190 	ill_walk_context_t	ctx;
17191 	ill_t			*ill;
17192 
17193 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17194 	ill = ILL_START_WALK_V4(&ctx, ipst);
17195 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17196 		ill_set_inputfn(ill);
17197 
17198 	rw_exit(&ipst->ips_ill_g_lock);
17199 }
17200 
17201 /*
17202  * Set the physical address information for `ill' to the contents of the
17203  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17204  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17205  * EINPROGRESS will be returned.
17206  */
17207 int
17208 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17209 {
17210 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17211 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17212 
17213 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17214 
17215 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17216 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17217 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17218 		/* Changing DL_IPV6_TOKEN is not yet supported */
17219 		return (0);
17220 	}
17221 
17222 	/*
17223 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17224 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17225 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17226 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17227 	 */
17228 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17229 		freemsg(mp);
17230 		return (ENOMEM);
17231 	}
17232 
17233 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17234 	mutex_enter(&ill->ill_lock);
17235 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17236 	/* no more nce addition allowed */
17237 	mutex_exit(&ill->ill_lock);
17238 
17239 	/*
17240 	 * If we can quiesce the ill, then set the address.  If not, then
17241 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17242 	 */
17243 	ill_down_ipifs(ill, B_TRUE);
17244 	mutex_enter(&ill->ill_lock);
17245 	if (!ill_is_quiescent(ill)) {
17246 		/* call cannot fail since `conn_t *' argument is NULL */
17247 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17248 		    mp, ILL_DOWN);
17249 		mutex_exit(&ill->ill_lock);
17250 		return (EINPROGRESS);
17251 	}
17252 	mutex_exit(&ill->ill_lock);
17253 
17254 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17255 	return (0);
17256 }
17257 
17258 /*
17259  * Once the ill associated with `q' has quiesced, set its physical address
17260  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17261  * are passed (linked by b_cont), since we sometimes need to save two distinct
17262  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17263  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17264  * is quiesced, we know any stale nce's with the old address information have
17265  * already been removed, so we don't need to call nce_flush().
17266  */
17267 /* ARGSUSED */
17268 static void
17269 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17270 {
17271 	ill_t		*ill = q->q_ptr;
17272 	mblk_t		*addrmp2 = unlinkb(addrmp);
17273 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17274 	uint_t		addrlen, addroff;
17275 	int		status;
17276 
17277 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17278 
17279 	addroff	= dlindp->dl_addr_offset;
17280 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17281 
17282 	switch (dlindp->dl_data) {
17283 	case DL_IPV6_LINK_LAYER_ADDR:
17284 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17285 		freemsg(addrmp2);
17286 		break;
17287 
17288 	case DL_CURR_DEST_ADDR:
17289 		freemsg(ill->ill_dest_addr_mp);
17290 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17291 		ill->ill_dest_addr_mp = addrmp;
17292 		if (ill->ill_isv6) {
17293 			ill_setdesttoken(ill);
17294 			ipif_setdestlinklocal(ill->ill_ipif);
17295 		}
17296 		freemsg(addrmp2);
17297 		break;
17298 
17299 	case DL_CURR_PHYS_ADDR:
17300 		freemsg(ill->ill_phys_addr_mp);
17301 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17302 		ill->ill_phys_addr_mp = addrmp;
17303 		ill->ill_phys_addr_length = addrlen;
17304 		if (ill->ill_isv6)
17305 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17306 		else
17307 			freemsg(addrmp2);
17308 		if (ill->ill_isv6) {
17309 			ill_setdefaulttoken(ill);
17310 			ipif_setlinklocal(ill->ill_ipif);
17311 		}
17312 		break;
17313 	default:
17314 		ASSERT(0);
17315 	}
17316 
17317 	/*
17318 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17319 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17320 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17321 	 * brought up.
17322 	 */
17323 	status = ill_up_ipifs(ill, q, addrmp);
17324 	mutex_enter(&ill->ill_lock);
17325 	if (ill->ill_dl_up)
17326 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17327 	mutex_exit(&ill->ill_lock);
17328 	if (status != EINPROGRESS)
17329 		ipsq_current_finish(ipsq);
17330 }
17331 
17332 /*
17333  * Helper routine for setting the ill_nd_lla fields.
17334  */
17335 void
17336 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17337 {
17338 	freemsg(ill->ill_nd_lla_mp);
17339 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17340 	ill->ill_nd_lla_mp = ndmp;
17341 	ill->ill_nd_lla_len = addrlen;
17342 }
17343 
17344 /*
17345  * Replumb the ill.
17346  */
17347 int
17348 ill_replumb(ill_t *ill, mblk_t *mp)
17349 {
17350 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17351 
17352 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17353 
17354 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17355 
17356 	mutex_enter(&ill->ill_lock);
17357 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17358 	/* no more nce addition allowed */
17359 	mutex_exit(&ill->ill_lock);
17360 
17361 	/*
17362 	 * If we can quiesce the ill, then continue.  If not, then
17363 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17364 	 */
17365 	ill_down_ipifs(ill, B_FALSE);
17366 
17367 	mutex_enter(&ill->ill_lock);
17368 	if (!ill_is_quiescent(ill)) {
17369 		/* call cannot fail since `conn_t *' argument is NULL */
17370 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17371 		    mp, ILL_DOWN);
17372 		mutex_exit(&ill->ill_lock);
17373 		return (EINPROGRESS);
17374 	}
17375 	mutex_exit(&ill->ill_lock);
17376 
17377 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17378 	return (0);
17379 }
17380 
17381 /* ARGSUSED */
17382 static void
17383 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17384 {
17385 	ill_t *ill = q->q_ptr;
17386 	int err;
17387 	conn_t *connp = NULL;
17388 
17389 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17390 	freemsg(ill->ill_replumb_mp);
17391 	ill->ill_replumb_mp = copyb(mp);
17392 
17393 	if (ill->ill_replumb_mp == NULL) {
17394 		/* out of memory */
17395 		ipsq_current_finish(ipsq);
17396 		return;
17397 	}
17398 
17399 	mutex_enter(&ill->ill_lock);
17400 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17401 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17402 	mutex_exit(&ill->ill_lock);
17403 
17404 	if (!ill->ill_up_ipifs) {
17405 		/* already closing */
17406 		ipsq_current_finish(ipsq);
17407 		return;
17408 	}
17409 	ill->ill_replumbing = 1;
17410 	err = ill_down_ipifs_tail(ill);
17411 
17412 	/*
17413 	 * Successfully quiesced and brought down the interface, now we send
17414 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17415 	 * DL_NOTE_REPLUMB message.
17416 	 */
17417 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17418 	    DL_NOTIFY_CONF);
17419 	ASSERT(mp != NULL);
17420 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17421 	    DL_NOTE_REPLUMB_DONE;
17422 	ill_dlpi_send(ill, mp);
17423 
17424 	/*
17425 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17426 	 * streams have to be unbound. When all the DLPI exchanges are done,
17427 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17428 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17429 	 * arp_bringup_done().
17430 	 */
17431 	ASSERT(ill->ill_replumb_mp != NULL);
17432 	if (err == EINPROGRESS)
17433 		return;
17434 	else
17435 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17436 	ASSERT(connp == NULL);
17437 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17438 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17439 		return;
17440 	}
17441 	ipsq_current_finish(ipsq);
17442 }
17443 
17444 /*
17445  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17446  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17447  * as per the ioctl.  On failure, an errno is returned.
17448  */
17449 static int
17450 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17451 {
17452 	int rval;
17453 	struct strioctl iocb;
17454 
17455 	iocb.ic_cmd = cmd;
17456 	iocb.ic_timout = 15;
17457 	iocb.ic_len = bufsize;
17458 	iocb.ic_dp = buf;
17459 
17460 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17461 }
17462 
17463 /*
17464  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17465  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17466  */
17467 static int
17468 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17469     uint_t *bufsizep, cred_t *cr)
17470 {
17471 	int err;
17472 	struct lifnum lifn;
17473 
17474 	bzero(&lifn, sizeof (lifn));
17475 	lifn.lifn_family = af;
17476 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17477 
17478 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17479 		return (err);
17480 
17481 	/*
17482 	 * Pad the interface count to account for additional interfaces that
17483 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17484 	 */
17485 	lifn.lifn_count += 4;
17486 	bzero(lifcp, sizeof (*lifcp));
17487 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17488 	lifcp->lifc_family = af;
17489 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17490 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17491 
17492 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17493 	if (err != 0) {
17494 		kmem_free(lifcp->lifc_buf, *bufsizep);
17495 		return (err);
17496 	}
17497 
17498 	return (0);
17499 }
17500 
17501 /*
17502  * Helper for ip_interface_cleanup() that removes the loopback interface.
17503  */
17504 static void
17505 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17506 {
17507 	int err;
17508 	struct lifreq lifr;
17509 
17510 	bzero(&lifr, sizeof (lifr));
17511 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17512 
17513 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17514 	if (err != 0) {
17515 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17516 		    "error %d\n", isv6 ? "v6" : "v4", err));
17517 	}
17518 }
17519 
17520 /*
17521  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17522  * groups and that IPMP data addresses are down.  These conditions must be met
17523  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17524  */
17525 static void
17526 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17527 {
17528 	int af = isv6 ? AF_INET6 : AF_INET;
17529 	int i, nifs;
17530 	int err;
17531 	uint_t bufsize;
17532 	uint_t lifrsize = sizeof (struct lifreq);
17533 	struct lifconf lifc;
17534 	struct lifreq *lifrp;
17535 
17536 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17537 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17538 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17539 		return;
17540 	}
17541 
17542 	nifs = lifc.lifc_len / lifrsize;
17543 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17544 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17545 		if (err != 0) {
17546 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17547 			    "flags: error %d", lifrp->lifr_name, err);
17548 			continue;
17549 		}
17550 
17551 		if (lifrp->lifr_flags & IFF_IPMP) {
17552 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17553 				continue;
17554 
17555 			lifrp->lifr_flags &= ~IFF_UP;
17556 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17557 			if (err != 0) {
17558 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17559 				    "bring down (error %d); IPMP interface may "
17560 				    "not be shutdown", lifrp->lifr_name, err);
17561 			}
17562 
17563 			/*
17564 			 * Check if IFF_DUPLICATE is still set -- and if so,
17565 			 * reset the address to clear it.
17566 			 */
17567 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17568 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17569 				continue;
17570 
17571 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17572 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17573 			    lifrp, lifrsize, cr)) != 0) {
17574 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17575 				    "reset DAD (error %d); IPMP interface may "
17576 				    "not be shutdown", lifrp->lifr_name, err);
17577 			}
17578 			continue;
17579 		}
17580 
17581 		lifrp->lifr_groupname[0] = '\0';
17582 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17583 		if (err != 0) {
17584 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17585 			    "IPMP group (error %d); associated IPMP interface "
17586 			    "may not be shutdown", lifrp->lifr_name, err);
17587 			continue;
17588 		}
17589 	}
17590 
17591 	kmem_free(lifc.lifc_buf, bufsize);
17592 }
17593 
17594 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17595 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17596 
17597 /*
17598  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17599  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17600  * when the user-level processes in the zone are killed and the latter are
17601  * cleaned up by str_stack_shutdown().
17602  */
17603 void
17604 ip_interface_cleanup(ip_stack_t *ipst)
17605 {
17606 	ldi_handle_t	lh;
17607 	ldi_ident_t	li;
17608 	cred_t		*cr;
17609 	int		err;
17610 	int		i;
17611 	char		*devs[] = { UDP6DEV, UDPDEV };
17612 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17613 
17614 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17615 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17616 		    " error %d", err);
17617 		return;
17618 	}
17619 
17620 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17621 	ASSERT(cr != NULL);
17622 
17623 	/*
17624 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17625 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17626 	 * the loop.)
17627 	 */
17628 	for (i = 0; i < 2; i++) {
17629 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17630 		if (err != 0) {
17631 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17632 			    " error %d", devs[i], err);
17633 			continue;
17634 		}
17635 
17636 		ip_loopback_removeif(lh, i == 0, cr);
17637 		ip_ipmp_cleanup(lh, i == 0, cr);
17638 
17639 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17640 	}
17641 
17642 	ldi_ident_release(li);
17643 	crfree(cr);
17644 }
17645 
17646 /*
17647  * This needs to be in-sync with nic_event_t definition
17648  */
17649 static const char *
17650 ill_hook_event2str(nic_event_t event)
17651 {
17652 	switch (event) {
17653 	case NE_PLUMB:
17654 		return ("PLUMB");
17655 	case NE_UNPLUMB:
17656 		return ("UNPLUMB");
17657 	case NE_UP:
17658 		return ("UP");
17659 	case NE_DOWN:
17660 		return ("DOWN");
17661 	case NE_ADDRESS_CHANGE:
17662 		return ("ADDRESS_CHANGE");
17663 	case NE_LIF_UP:
17664 		return ("LIF_UP");
17665 	case NE_LIF_DOWN:
17666 		return ("LIF_DOWN");
17667 	case NE_IFINDEX_CHANGE:
17668 		return ("IFINDEX_CHANGE");
17669 	default:
17670 		return ("UNKNOWN");
17671 	}
17672 }
17673 
17674 void
17675 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17676     nic_event_data_t data, size_t datalen)
17677 {
17678 	ip_stack_t		*ipst = ill->ill_ipst;
17679 	hook_nic_event_int_t	*info;
17680 	const char		*str = NULL;
17681 
17682 	/* create a new nic event info */
17683 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17684 		goto fail;
17685 
17686 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17687 	info->hnei_event.hne_lif = lif;
17688 	info->hnei_event.hne_event = event;
17689 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17690 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17691 	info->hnei_event.hne_data = NULL;
17692 	info->hnei_event.hne_datalen = 0;
17693 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17694 
17695 	if (data != NULL && datalen != 0) {
17696 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17697 		if (info->hnei_event.hne_data == NULL)
17698 			goto fail;
17699 		bcopy(data, info->hnei_event.hne_data, datalen);
17700 		info->hnei_event.hne_datalen = datalen;
17701 	}
17702 
17703 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17704 	    DDI_NOSLEEP) == DDI_SUCCESS)
17705 		return;
17706 
17707 fail:
17708 	if (info != NULL) {
17709 		if (info->hnei_event.hne_data != NULL) {
17710 			kmem_free(info->hnei_event.hne_data,
17711 			    info->hnei_event.hne_datalen);
17712 		}
17713 		kmem_free(info, sizeof (hook_nic_event_t));
17714 	}
17715 	str = ill_hook_event2str(event);
17716 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17717 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17718 }
17719 
17720 static int
17721 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17722 {
17723 	int		err = 0;
17724 	const in_addr_t	*addr = NULL;
17725 	nce_t		*nce = NULL;
17726 	ill_t		*ill = ipif->ipif_ill;
17727 	ill_t		*bound_ill;
17728 	boolean_t	added_ipif = B_FALSE;
17729 	uint16_t	state;
17730 	uint16_t	flags;
17731 
17732 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17733 	    ill_t *, ill, ipif_t *, ipif);
17734 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17735 		addr = &ipif->ipif_lcl_addr;
17736 	}
17737 
17738 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17739 		if (res_act != Res_act_initial)
17740 			return (EINVAL);
17741 	}
17742 
17743 	if (addr != NULL) {
17744 		ipmp_illgrp_t	*illg = ill->ill_grp;
17745 
17746 		/* add unicast nce for the local addr */
17747 
17748 		if (IS_IPMP(ill)) {
17749 			/*
17750 			 * If we're here via ipif_up(), then the ipif
17751 			 * won't be bound yet -- add it to the group,
17752 			 * which will bind it if possible. (We would
17753 			 * add it in ipif_up(), but deleting on failure
17754 			 * there is gruesome.)  If we're here via
17755 			 * ipmp_ill_bind_ipif(), then the ipif has
17756 			 * already been added to the group and we
17757 			 * just need to use the binding.
17758 			 */
17759 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17760 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17761 				if (bound_ill == NULL) {
17762 					/*
17763 					 * We couldn't bind the ipif to an ill
17764 					 * yet, so we have nothing to publish.
17765 					 * Mark the address as ready and return.
17766 					 */
17767 					ipif->ipif_addr_ready = 1;
17768 					return (0);
17769 				}
17770 				added_ipif = B_TRUE;
17771 			}
17772 		} else {
17773 			bound_ill = ill;
17774 		}
17775 
17776 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17777 		    NCE_F_NONUD);
17778 		/*
17779 		 * If this is an initial bring-up (or the ipif was never
17780 		 * completely brought up), do DAD.  Otherwise, we're here
17781 		 * because IPMP has rebound an address to this ill: send
17782 		 * unsolicited advertisements (ARP announcements) to
17783 		 * inform others.
17784 		 */
17785 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17786 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17787 		} else {
17788 			state = ND_REACHABLE;
17789 			flags |= NCE_F_UNSOL_ADV;
17790 		}
17791 
17792 retry:
17793 		err = nce_lookup_then_add_v4(ill,
17794 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17795 		    addr, flags, state, &nce);
17796 
17797 		/*
17798 		 * note that we may encounter EEXIST if we are moving
17799 		 * the nce as a result of a rebind operation.
17800 		 */
17801 		switch (err) {
17802 		case 0:
17803 			ipif->ipif_added_nce = 1;
17804 			nce->nce_ipif_cnt++;
17805 			break;
17806 		case EEXIST:
17807 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17808 			    ill->ill_name));
17809 			if (!NCE_MYADDR(nce->nce_common)) {
17810 				/*
17811 				 * A leftover nce from before this address
17812 				 * existed
17813 				 */
17814 				ncec_delete(nce->nce_common);
17815 				nce_refrele(nce);
17816 				nce = NULL;
17817 				goto retry;
17818 			}
17819 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17820 				nce_refrele(nce);
17821 				nce = NULL;
17822 				ip1dbg(("ipif_arp_up: NCE already exists "
17823 				    "for %s:%u\n", ill->ill_name,
17824 				    ipif->ipif_id));
17825 				goto arp_up_done;
17826 			}
17827 			/*
17828 			 * Duplicate local addresses are permissible for
17829 			 * IPIF_POINTOPOINT interfaces which will get marked
17830 			 * IPIF_UNNUMBERED later in
17831 			 * ip_addr_availability_check().
17832 			 *
17833 			 * The nce_ipif_cnt field tracks the number of
17834 			 * ipifs that have nce_addr as their local address.
17835 			 */
17836 			ipif->ipif_addr_ready = 1;
17837 			ipif->ipif_added_nce = 1;
17838 			nce->nce_ipif_cnt++;
17839 			err = 0;
17840 			break;
17841 		default:
17842 			ASSERT(nce == NULL);
17843 			goto arp_up_done;
17844 		}
17845 		if (arp_no_defense) {
17846 			if ((ipif->ipif_flags & IPIF_UP) &&
17847 			    !ipif->ipif_addr_ready)
17848 				ipif_up_notify(ipif);
17849 			ipif->ipif_addr_ready = 1;
17850 		}
17851 	} else {
17852 		/* zero address. nothing to publish */
17853 		ipif->ipif_addr_ready = 1;
17854 	}
17855 	if (nce != NULL)
17856 		nce_refrele(nce);
17857 arp_up_done:
17858 	if (added_ipif && err != 0)
17859 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17860 	return (err);
17861 }
17862 
17863 int
17864 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17865 {
17866 	int 		err = 0;
17867 	ill_t 		*ill = ipif->ipif_ill;
17868 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17869 
17870 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17871 	    ill_t *, ill, ipif_t *, ipif);
17872 
17873 	/*
17874 	 * need to bring up ARP or setup mcast mapping only
17875 	 * when the first interface is coming UP.
17876 	 */
17877 	first_interface = (ill->ill_ipif_up_count == 0 &&
17878 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17879 
17880 	if (res_act == Res_act_initial && first_interface) {
17881 		/*
17882 		 * Send ATTACH + BIND
17883 		 */
17884 		err = arp_ll_up(ill);
17885 		if (err != EINPROGRESS && err != 0)
17886 			return (err);
17887 
17888 		/*
17889 		 * Add NCE for local address. Start DAD.
17890 		 * we'll wait to hear that DAD has finished
17891 		 * before using the interface.
17892 		 */
17893 		if (err == EINPROGRESS)
17894 			wait_for_dlpi = B_TRUE;
17895 	}
17896 
17897 	if (!wait_for_dlpi)
17898 		(void) ipif_arp_up_done_tail(ipif, res_act);
17899 
17900 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17901 }
17902 
17903 /*
17904  * Finish processing of "arp_up" after all the DLPI message
17905  * exchanges have completed between arp and the driver.
17906  */
17907 void
17908 arp_bringup_done(ill_t *ill, int err)
17909 {
17910 	mblk_t	*mp1;
17911 	ipif_t  *ipif;
17912 	conn_t *connp = NULL;
17913 	ipsq_t	*ipsq;
17914 	queue_t *q;
17915 
17916 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
17917 
17918 	ASSERT(IAM_WRITER_ILL(ill));
17919 
17920 	ipsq = ill->ill_phyint->phyint_ipsq;
17921 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17922 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17923 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17924 	if (mp1 == NULL) /* bringup was aborted by the user */
17925 		return;
17926 
17927 	/*
17928 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17929 	 * must have an associated conn_t.  Otherwise, we're bringing this
17930 	 * interface back up as part of handling an asynchronous event (e.g.,
17931 	 * physical address change).
17932 	 */
17933 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17934 		ASSERT(connp != NULL);
17935 		q = CONNP_TO_WQ(connp);
17936 	} else {
17937 		ASSERT(connp == NULL);
17938 		q = ill->ill_rq;
17939 	}
17940 	if (err == 0) {
17941 		if (ipif->ipif_isv6) {
17942 			if ((err = ipif_up_done_v6(ipif)) != 0)
17943 				ip0dbg(("arp_bringup_done: init failed\n"));
17944 		} else {
17945 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
17946 			if (err != 0 ||
17947 			    (err = ipif_up_done(ipif)) != 0) {
17948 				ip0dbg(("arp_bringup_done: "
17949 				    "init failed err %x\n", err));
17950 				(void) ipif_arp_down(ipif);
17951 			}
17952 
17953 		}
17954 	} else {
17955 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
17956 	}
17957 
17958 	if ((err == 0) && (ill->ill_up_ipifs)) {
17959 		err = ill_up_ipifs(ill, q, mp1);
17960 		if (err == EINPROGRESS)
17961 			return;
17962 	}
17963 
17964 	/*
17965 	 * If we have a moved ipif to bring up, and everything has succeeded
17966 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
17967 	 * down -- the admin can try to bring it up by hand if need be.
17968 	 */
17969 	if (ill->ill_move_ipif != NULL) {
17970 		ipif = ill->ill_move_ipif;
17971 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
17972 		    ipif->ipif_ill->ill_name));
17973 		ill->ill_move_ipif = NULL;
17974 		if (err == 0) {
17975 			err = ipif_up(ipif, q, mp1);
17976 			if (err == EINPROGRESS)
17977 				return;
17978 		}
17979 	}
17980 
17981 	/*
17982 	 * The operation must complete without EINPROGRESS since
17983 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
17984 	 * Otherwise, the operation will be stuck forever in the ipsq.
17985 	 */
17986 	ASSERT(err != EINPROGRESS);
17987 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17988 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
17989 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
17990 		    ill_t *, ill, ipif_t *, ipif);
17991 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
17992 	} else {
17993 		ipsq_current_finish(ipsq);
17994 	}
17995 }
17996 
17997 /*
17998  * Finish processing of arp replumb after all the DLPI message
17999  * exchanges have completed between arp and the driver.
18000  */
18001 void
18002 arp_replumb_done(ill_t *ill, int err)
18003 {
18004 	mblk_t	*mp1;
18005 	ipif_t  *ipif;
18006 	conn_t *connp = NULL;
18007 	ipsq_t	*ipsq;
18008 	queue_t *q;
18009 
18010 	ASSERT(IAM_WRITER_ILL(ill));
18011 
18012 	ipsq = ill->ill_phyint->phyint_ipsq;
18013 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18014 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18015 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18016 	if (mp1 == NULL) {
18017 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18018 		    ipsq->ipsq_xop->ipx_current_ioctl));
18019 		/* bringup was aborted by the user */
18020 		return;
18021 	}
18022 	/*
18023 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18024 	 * must have an associated conn_t.  Otherwise, we're bringing this
18025 	 * interface back up as part of handling an asynchronous event (e.g.,
18026 	 * physical address change).
18027 	 */
18028 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18029 		ASSERT(connp != NULL);
18030 		q = CONNP_TO_WQ(connp);
18031 	} else {
18032 		ASSERT(connp == NULL);
18033 		q = ill->ill_rq;
18034 	}
18035 	if ((err == 0) && (ill->ill_up_ipifs)) {
18036 		err = ill_up_ipifs(ill, q, mp1);
18037 		if (err == EINPROGRESS)
18038 			return;
18039 	}
18040 	/*
18041 	 * The operation must complete without EINPROGRESS since
18042 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18043 	 * Otherwise, the operation will be stuck forever in the ipsq.
18044 	 */
18045 	ASSERT(err != EINPROGRESS);
18046 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18047 		DTRACE_PROBE4(ipif__ioctl, char *,
18048 		    "arp_replumb_done finish",
18049 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18050 		    ill_t *, ill, ipif_t *, ipif);
18051 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18052 	} else {
18053 		ipsq_current_finish(ipsq);
18054 	}
18055 }
18056 
18057 void
18058 ipif_up_notify(ipif_t *ipif)
18059 {
18060 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18061 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18062 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18063 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18064 	    NE_LIF_UP, NULL, 0);
18065 }
18066 
18067 /*
18068  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18069  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18070  * TPI end points with STREAMS modules pushed above.  This is assured by not
18071  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18072  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18073  * while unwinding from the ispq and that could be a thread from the bottom.
18074  */
18075 /* ARGSUSED */
18076 int
18077 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18078     ip_ioctl_cmd_t *ipip, void *arg)
18079 {
18080 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18081 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18082 	int ret = 0;
18083 	int i;
18084 	size_t size;
18085 	ip_stack_t *ipst;
18086 	zoneid_t zoneid;
18087 	ilb_stack_t *ilbs;
18088 
18089 	ipst = CONNQ_TO_IPST(q);
18090 	ilbs = ipst->ips_netstack->netstack_ilb;
18091 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18092 
18093 	switch (command) {
18094 	case ILB_CREATE_RULE: {
18095 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18096 
18097 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18098 			ret = EINVAL;
18099 			break;
18100 		}
18101 
18102 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18103 		break;
18104 	}
18105 	case ILB_DESTROY_RULE:
18106 	case ILB_ENABLE_RULE:
18107 	case ILB_DISABLE_RULE: {
18108 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18109 
18110 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18111 			ret = EINVAL;
18112 			break;
18113 		}
18114 
18115 		if (cmd->flags & ILB_RULE_ALLRULES) {
18116 			if (command == ILB_DESTROY_RULE) {
18117 				ilb_rule_del_all(ilbs, zoneid);
18118 				break;
18119 			} else if (command == ILB_ENABLE_RULE) {
18120 				ilb_rule_enable_all(ilbs, zoneid);
18121 				break;
18122 			} else if (command == ILB_DISABLE_RULE) {
18123 				ilb_rule_disable_all(ilbs, zoneid);
18124 				break;
18125 			}
18126 		} else {
18127 			if (command == ILB_DESTROY_RULE) {
18128 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18129 			} else if (command == ILB_ENABLE_RULE) {
18130 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18131 				    NULL);
18132 			} else if (command == ILB_DISABLE_RULE) {
18133 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18134 				    NULL);
18135 			}
18136 		}
18137 		break;
18138 	}
18139 	case ILB_NUM_RULES: {
18140 		ilb_num_rules_cmd_t *cmd;
18141 
18142 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18143 			ret = EINVAL;
18144 			break;
18145 		}
18146 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18147 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18148 		break;
18149 	}
18150 	case ILB_RULE_NAMES: {
18151 		ilb_rule_names_cmd_t *cmd;
18152 
18153 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18154 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18155 		    cmd->num_names == 0) {
18156 			ret = EINVAL;
18157 			break;
18158 		}
18159 		size = cmd->num_names * ILB_RULE_NAMESZ;
18160 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18161 		    size != cmd_mp->b_wptr) {
18162 			ret = EINVAL;
18163 			break;
18164 		}
18165 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18166 		break;
18167 	}
18168 	case ILB_NUM_SERVERS: {
18169 		ilb_num_servers_cmd_t *cmd;
18170 
18171 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18172 			ret = EINVAL;
18173 			break;
18174 		}
18175 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18176 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18177 		    &(cmd->num));
18178 		break;
18179 	}
18180 	case ILB_LIST_RULE: {
18181 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18182 
18183 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18184 			ret = EINVAL;
18185 			break;
18186 		}
18187 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18188 		break;
18189 	}
18190 	case ILB_LIST_SERVERS: {
18191 		ilb_servers_info_cmd_t *cmd;
18192 
18193 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18194 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18195 		    cmd->num_servers == 0) {
18196 			ret = EINVAL;
18197 			break;
18198 		}
18199 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18200 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18201 		    size != cmd_mp->b_wptr) {
18202 			ret = EINVAL;
18203 			break;
18204 		}
18205 
18206 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18207 		    &cmd->num_servers);
18208 		break;
18209 	}
18210 	case ILB_ADD_SERVERS: {
18211 		ilb_servers_info_cmd_t *cmd;
18212 		ilb_rule_t *rule;
18213 
18214 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18215 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18216 			ret = EINVAL;
18217 			break;
18218 		}
18219 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18220 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18221 		    size != cmd_mp->b_wptr) {
18222 			ret = EINVAL;
18223 			break;
18224 		}
18225 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18226 		if (rule == NULL) {
18227 			ASSERT(ret != 0);
18228 			break;
18229 		}
18230 		for (i = 0; i < cmd->num_servers; i++) {
18231 			ilb_server_info_t *s;
18232 
18233 			s = &cmd->servers[i];
18234 			s->err = ilb_server_add(ilbs, rule, s);
18235 		}
18236 		ILB_RULE_REFRELE(rule);
18237 		break;
18238 	}
18239 	case ILB_DEL_SERVERS:
18240 	case ILB_ENABLE_SERVERS:
18241 	case ILB_DISABLE_SERVERS: {
18242 		ilb_servers_cmd_t *cmd;
18243 		ilb_rule_t *rule;
18244 		int (*f)();
18245 
18246 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18247 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18248 			ret = EINVAL;
18249 			break;
18250 		}
18251 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18252 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18253 		    size != cmd_mp->b_wptr) {
18254 			ret = EINVAL;
18255 			break;
18256 		}
18257 
18258 		if (command == ILB_DEL_SERVERS)
18259 			f = ilb_server_del;
18260 		else if (command == ILB_ENABLE_SERVERS)
18261 			f = ilb_server_enable;
18262 		else if (command == ILB_DISABLE_SERVERS)
18263 			f = ilb_server_disable;
18264 
18265 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18266 		if (rule == NULL) {
18267 			ASSERT(ret != 0);
18268 			break;
18269 		}
18270 
18271 		for (i = 0; i < cmd->num_servers; i++) {
18272 			ilb_server_arg_t *s;
18273 
18274 			s = &cmd->servers[i];
18275 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18276 		}
18277 		ILB_RULE_REFRELE(rule);
18278 		break;
18279 	}
18280 	case ILB_LIST_NAT_TABLE: {
18281 		ilb_list_nat_cmd_t *cmd;
18282 
18283 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18284 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18285 			ret = EINVAL;
18286 			break;
18287 		}
18288 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18289 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18290 		    size != cmd_mp->b_wptr) {
18291 			ret = EINVAL;
18292 			break;
18293 		}
18294 
18295 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18296 		    &cmd->flags);
18297 		break;
18298 	}
18299 	case ILB_LIST_STICKY_TABLE: {
18300 		ilb_list_sticky_cmd_t *cmd;
18301 
18302 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18303 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18304 			ret = EINVAL;
18305 			break;
18306 		}
18307 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18308 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18309 		    size != cmd_mp->b_wptr) {
18310 			ret = EINVAL;
18311 			break;
18312 		}
18313 
18314 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18315 		    &cmd->num_sticky, &cmd->flags);
18316 		break;
18317 	}
18318 	default:
18319 		ret = EINVAL;
18320 		break;
18321 	}
18322 done:
18323 	return (ret);
18324 }
18325 
18326 /* Remove all cache entries for this logical interface */
18327 void
18328 ipif_nce_down(ipif_t *ipif)
18329 {
18330 	ill_t *ill = ipif->ipif_ill;
18331 	nce_t *nce;
18332 
18333 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18334 	    ill_t *, ill, ipif_t *, ipif);
18335 	if (ipif->ipif_added_nce) {
18336 		if (ipif->ipif_isv6)
18337 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18338 		else
18339 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18340 		if (nce != NULL) {
18341 			if (--nce->nce_ipif_cnt == 0)
18342 				ncec_delete(nce->nce_common);
18343 			ipif->ipif_added_nce = 0;
18344 			nce_refrele(nce);
18345 		} else {
18346 			/*
18347 			 * nce may already be NULL because it was already
18348 			 * flushed, e.g., due to a call to nce_flush
18349 			 */
18350 			ipif->ipif_added_nce = 0;
18351 		}
18352 	}
18353 	/*
18354 	 * Make IPMP aware of the deleted data address.
18355 	 */
18356 	if (IS_IPMP(ill))
18357 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18358 
18359 	/*
18360 	 * Remove all other nces dependent on this ill when the last ipif
18361 	 * is going away.
18362 	 */
18363 	if (ill->ill_ipif_up_count == 0) {
18364 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18365 		    (uchar_t *)ill, ill->ill_ipst);
18366 		if (IS_UNDER_IPMP(ill))
18367 			nce_flush(ill, B_TRUE);
18368 	}
18369 }
18370