xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision 96c4a178a18cd52ee5001195f1552d9cef0c38f0)
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_free_mib(ill_t *ill);
171 static void	ill_glist_delete(ill_t *);
172 static void	ill_phyint_reinit(ill_t *ill);
173 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
174 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
175 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 
177 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
178 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
179 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
180 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
181 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
182 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
183 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
184 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
185 static ip_v4mapinfo_func_t ip_mbcast_mapping;
186 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
187 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
188 static void	phyint_free(phyint_t *);
189 
190 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
191 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
194 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
195     dl_capability_sub_t *);
196 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
197 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
198 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
199 		    dl_capability_sub_t *);
200 static void	ill_capability_dld_enable(ill_t *);
201 static void	ill_capability_ack_thr(void *);
202 static void	ill_capability_lso_enable(ill_t *);
203 static void	ill_capability_send(ill_t *, mblk_t *);
204 
205 static ill_t	*ill_prev_usesrc(ill_t *);
206 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
207 static void	ill_disband_usesrc_group(ill_t *);
208 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
209 
210 #ifdef DEBUG
211 static	void	ill_trace_cleanup(const ill_t *);
212 static	void	ipif_trace_cleanup(const ipif_t *);
213 #endif
214 
215 /*
216  * if we go over the memory footprint limit more than once in this msec
217  * interval, we'll start pruning aggressively.
218  */
219 int ip_min_frag_prune_time = 0;
220 
221 static ipft_t	ip_ioctl_ftbl[] = {
222 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
223 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
224 		IPFT_F_NO_REPLY },
225 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
226 	{ 0 }
227 };
228 
229 /* Simple ICMP IP Header Template */
230 static ipha_t icmp_ipha = {
231 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
232 };
233 
234 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
235 
236 static ip_m_t   ip_m_tbl[] = {
237 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
238 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
239 	    ip_nodef_v6intfid },
240 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
241 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
242 	    ip_nodef_v6intfid },
243 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
256 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
257 	    ip_ipv4_v6destintfid },
258 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
259 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
260 	    ip_ipv6_v6destintfid },
261 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
263 	    ip_nodef_v6intfid },
264 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
265 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
266 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
267 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
268 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
269 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
270 	    ip_nodef_v6intfid }
271 };
272 
273 static ill_t	ill_null;		/* Empty ILL for init. */
274 char	ipif_loopback_name[] = "lo0";
275 static char *ipv4_forward_suffix = ":ip_forwarding";
276 static char *ipv6_forward_suffix = ":ip6_forwarding";
277 static	sin6_t	sin6_null;	/* Zero address for quick clears */
278 static	sin_t	sin_null;	/* Zero address for quick clears */
279 
280 /* When set search for unused ipif_seqid */
281 static ipif_t	ipif_zero;
282 
283 /*
284  * ppa arena is created after these many
285  * interfaces have been plumbed.
286  */
287 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
288 
289 /*
290  * Allocate per-interface mibs.
291  * Returns true if ok. False otherwise.
292  *  ipsq  may not yet be allocated (loopback case ).
293  */
294 static boolean_t
295 ill_allocate_mibs(ill_t *ill)
296 {
297 	/* Already allocated? */
298 	if (ill->ill_ip_mib != NULL) {
299 		if (ill->ill_isv6)
300 			ASSERT(ill->ill_icmp6_mib != NULL);
301 		return (B_TRUE);
302 	}
303 
304 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
305 	    KM_NOSLEEP);
306 	if (ill->ill_ip_mib == NULL) {
307 		return (B_FALSE);
308 	}
309 
310 	/* Setup static information */
311 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
312 	    sizeof (mib2_ipIfStatsEntry_t));
313 	if (ill->ill_isv6) {
314 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
315 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
316 		    sizeof (mib2_ipv6AddrEntry_t));
317 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
318 		    sizeof (mib2_ipv6RouteEntry_t));
319 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
320 		    sizeof (mib2_ipv6NetToMediaEntry_t));
321 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
322 		    sizeof (ipv6_member_t));
323 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
324 		    sizeof (ipv6_grpsrc_t));
325 	} else {
326 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
327 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
328 		    sizeof (mib2_ipAddrEntry_t));
329 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
330 		    sizeof (mib2_ipRouteEntry_t));
331 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
332 		    sizeof (mib2_ipNetToMediaEntry_t));
333 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
334 		    sizeof (ip_member_t));
335 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
336 		    sizeof (ip_grpsrc_t));
337 
338 		/*
339 		 * For a v4 ill, we are done at this point, because per ill
340 		 * icmp mibs are only used for v6.
341 		 */
342 		return (B_TRUE);
343 	}
344 
345 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
346 	    KM_NOSLEEP);
347 	if (ill->ill_icmp6_mib == NULL) {
348 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
349 		ill->ill_ip_mib = NULL;
350 		return (B_FALSE);
351 	}
352 	/* static icmp info */
353 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
354 	    sizeof (mib2_ipv6IfIcmpEntry_t);
355 	/*
356 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
357 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
358 	 * -> ill_phyint_reinit
359 	 */
360 	return (B_TRUE);
361 }
362 
363 /*
364  * Completely vaporize a lower level tap and all associated interfaces.
365  * ill_delete is called only out of ip_close when the device control
366  * stream is being closed.
367  */
368 void
369 ill_delete(ill_t *ill)
370 {
371 	ipif_t	*ipif;
372 	ill_t	*prev_ill;
373 	ip_stack_t	*ipst = ill->ill_ipst;
374 
375 	/*
376 	 * ill_delete may be forcibly entering the ipsq. The previous
377 	 * ioctl may not have completed and may need to be aborted.
378 	 * ipsq_flush takes care of it. If we don't need to enter the
379 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
380 	 * ill_delete_tail is sufficient.
381 	 */
382 	ipsq_flush(ill);
383 
384 	/*
385 	 * Nuke all interfaces.  ipif_free will take down the interface,
386 	 * remove it from the list, and free the data structure.
387 	 * Walk down the ipif list and remove the logical interfaces
388 	 * first before removing the main ipif. We can't unplumb
389 	 * zeroth interface first in the case of IPv6 as update_conn_ill
390 	 * -> ip_ll_multireq de-references ill_ipif for checking
391 	 * POINTOPOINT.
392 	 *
393 	 * If ill_ipif was not properly initialized (i.e low on memory),
394 	 * then no interfaces to clean up. In this case just clean up the
395 	 * ill.
396 	 */
397 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
398 		ipif_free(ipif);
399 
400 	/*
401 	 * clean out all the nce_t entries that depend on this
402 	 * ill for the ill_phys_addr.
403 	 */
404 	nce_flush(ill, B_TRUE);
405 
406 	/* Clean up msgs on pending upcalls for mrouted */
407 	reset_mrt_ill(ill);
408 
409 	update_conn_ill(ill, ipst);
410 
411 	/*
412 	 * Remove multicast references added as a result of calls to
413 	 * ip_join_allmulti().
414 	 */
415 	ip_purge_allmulti(ill);
416 
417 	/*
418 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
419 	 */
420 	if (IS_UNDER_IPMP(ill))
421 		ipmp_ill_leave_illgrp(ill);
422 
423 	/*
424 	 * ill_down will arrange to blow off any IRE's dependent on this
425 	 * ILL, and shut down fragmentation reassembly.
426 	 */
427 	ill_down(ill);
428 
429 	/* Let SCTP know, so that it can remove this from its list. */
430 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
431 
432 	/*
433 	 * Walk all CONNs that can have a reference on an ire or nce for this
434 	 * ill (we actually walk all that now have stale references).
435 	 */
436 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
437 
438 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
439 	if (ill->ill_isv6)
440 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
441 
442 	/*
443 	 * If an address on this ILL is being used as a source address then
444 	 * clear out the pointers in other ILLs that point to this ILL.
445 	 */
446 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
447 	if (ill->ill_usesrc_grp_next != NULL) {
448 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
449 			ill_disband_usesrc_group(ill);
450 		} else {	/* consumer of the usesrc ILL */
451 			prev_ill = ill_prev_usesrc(ill);
452 			prev_ill->ill_usesrc_grp_next =
453 			    ill->ill_usesrc_grp_next;
454 		}
455 	}
456 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
457 }
458 
459 static void
460 ipif_non_duplicate(ipif_t *ipif)
461 {
462 	ill_t *ill = ipif->ipif_ill;
463 	mutex_enter(&ill->ill_lock);
464 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
465 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
466 		ASSERT(ill->ill_ipif_dup_count > 0);
467 		ill->ill_ipif_dup_count--;
468 	}
469 	mutex_exit(&ill->ill_lock);
470 }
471 
472 /*
473  * ill_delete_tail is called from ip_modclose after all references
474  * to the closing ill are gone. The wait is done in ip_modclose
475  */
476 void
477 ill_delete_tail(ill_t *ill)
478 {
479 	mblk_t	**mpp;
480 	ipif_t	*ipif;
481 	ip_stack_t	*ipst = ill->ill_ipst;
482 
483 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
484 		ipif_non_duplicate(ipif);
485 		(void) ipif_down_tail(ipif);
486 	}
487 
488 	ASSERT(ill->ill_ipif_dup_count == 0);
489 
490 	/*
491 	 * If polling capability is enabled (which signifies direct
492 	 * upcall into IP and driver has ill saved as a handle),
493 	 * we need to make sure that unbind has completed before we
494 	 * let the ill disappear and driver no longer has any reference
495 	 * to this ill.
496 	 */
497 	mutex_enter(&ill->ill_lock);
498 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
499 		cv_wait(&ill->ill_cv, &ill->ill_lock);
500 	mutex_exit(&ill->ill_lock);
501 	ASSERT(!(ill->ill_capabilities &
502 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
503 
504 	if (ill->ill_net_type != IRE_LOOPBACK)
505 		qprocsoff(ill->ill_rq);
506 
507 	/*
508 	 * We do an ipsq_flush once again now. New messages could have
509 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
510 	 * could also have landed up if an ioctl thread had looked up
511 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
512 	 * enqueued the ioctl when we did the ipsq_flush last time.
513 	 */
514 	ipsq_flush(ill);
515 
516 	/*
517 	 * Free capabilities.
518 	 */
519 	if (ill->ill_hcksum_capab != NULL) {
520 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
521 		ill->ill_hcksum_capab = NULL;
522 	}
523 
524 	if (ill->ill_zerocopy_capab != NULL) {
525 		kmem_free(ill->ill_zerocopy_capab,
526 		    sizeof (ill_zerocopy_capab_t));
527 		ill->ill_zerocopy_capab = NULL;
528 	}
529 
530 	if (ill->ill_lso_capab != NULL) {
531 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
532 		ill->ill_lso_capab = NULL;
533 	}
534 
535 	if (ill->ill_dld_capab != NULL) {
536 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
537 		ill->ill_dld_capab = NULL;
538 	}
539 
540 	while (ill->ill_ipif != NULL)
541 		ipif_free_tail(ill->ill_ipif);
542 
543 	/*
544 	 * We have removed all references to ilm from conn and the ones joined
545 	 * within the kernel.
546 	 *
547 	 * We don't walk conns, mrts and ires because
548 	 *
549 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
550 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
551 	 *    ill references.
552 	 */
553 
554 	/*
555 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
556 	 * is safe to do because the illgrp has already been unlinked from the
557 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
558 	 */
559 	if (IS_IPMP(ill)) {
560 		ipmp_illgrp_destroy(ill->ill_grp);
561 		ill->ill_grp = NULL;
562 	}
563 
564 	/*
565 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
566 	 * could free the phyint. No more reference to the phyint after this
567 	 * point.
568 	 */
569 	(void) ill_glist_delete(ill);
570 
571 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
572 	if (ill->ill_ndd_name != NULL)
573 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
574 	rw_exit(&ipst->ips_ip_g_nd_lock);
575 
576 	if (ill->ill_frag_ptr != NULL) {
577 		uint_t count;
578 
579 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
580 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
581 		}
582 		mi_free(ill->ill_frag_ptr);
583 		ill->ill_frag_ptr = NULL;
584 		ill->ill_frag_hash_tbl = NULL;
585 	}
586 
587 	freemsg(ill->ill_nd_lla_mp);
588 	/* Free all retained control messages. */
589 	mpp = &ill->ill_first_mp_to_free;
590 	do {
591 		while (mpp[0]) {
592 			mblk_t  *mp;
593 			mblk_t  *mp1;
594 
595 			mp = mpp[0];
596 			mpp[0] = mp->b_next;
597 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
598 				mp1->b_next = NULL;
599 				mp1->b_prev = NULL;
600 			}
601 			freemsg(mp);
602 		}
603 	} while (mpp++ != &ill->ill_last_mp_to_free);
604 
605 	ill_free_mib(ill);
606 
607 #ifdef DEBUG
608 	ill_trace_cleanup(ill);
609 #endif
610 
611 	/* The default multicast interface might have changed */
612 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
613 
614 	/* Drop refcnt here */
615 	netstack_rele(ill->ill_ipst->ips_netstack);
616 	ill->ill_ipst = NULL;
617 }
618 
619 static void
620 ill_free_mib(ill_t *ill)
621 {
622 	ip_stack_t *ipst = ill->ill_ipst;
623 
624 	/*
625 	 * MIB statistics must not be lost, so when an interface
626 	 * goes away the counter values will be added to the global
627 	 * MIBs.
628 	 */
629 	if (ill->ill_ip_mib != NULL) {
630 		if (ill->ill_isv6) {
631 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
632 			    ill->ill_ip_mib);
633 		} else {
634 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
635 			    ill->ill_ip_mib);
636 		}
637 
638 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
639 		ill->ill_ip_mib = NULL;
640 	}
641 	if (ill->ill_icmp6_mib != NULL) {
642 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
643 		    ill->ill_icmp6_mib);
644 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
645 		ill->ill_icmp6_mib = NULL;
646 	}
647 }
648 
649 /*
650  * Concatenate together a physical address and a sap.
651  *
652  * Sap_lengths are interpreted as follows:
653  *   sap_length == 0	==>	no sap
654  *   sap_length > 0	==>	sap is at the head of the dlpi address
655  *   sap_length < 0	==>	sap is at the tail of the dlpi address
656  */
657 static void
658 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
659     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
660 {
661 	uint16_t sap_addr = (uint16_t)sap_src;
662 
663 	if (sap_length == 0) {
664 		if (phys_src == NULL)
665 			bzero(dst, phys_length);
666 		else
667 			bcopy(phys_src, dst, phys_length);
668 	} else if (sap_length < 0) {
669 		if (phys_src == NULL)
670 			bzero(dst, phys_length);
671 		else
672 			bcopy(phys_src, dst, phys_length);
673 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
674 	} else {
675 		bcopy(&sap_addr, dst, sizeof (sap_addr));
676 		if (phys_src == NULL)
677 			bzero((char *)dst + sap_length, phys_length);
678 		else
679 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
680 	}
681 }
682 
683 /*
684  * Generate a dl_unitdata_req mblk for the device and address given.
685  * addr_length is the length of the physical portion of the address.
686  * If addr is NULL include an all zero address of the specified length.
687  * TRUE? In any case, addr_length is taken to be the entire length of the
688  * dlpi address, including the absolute value of sap_length.
689  */
690 mblk_t *
691 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
692 		t_scalar_t sap_length)
693 {
694 	dl_unitdata_req_t *dlur;
695 	mblk_t	*mp;
696 	t_scalar_t	abs_sap_length;		/* absolute value */
697 
698 	abs_sap_length = ABS(sap_length);
699 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
700 	    DL_UNITDATA_REQ);
701 	if (mp == NULL)
702 		return (NULL);
703 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
704 	/* HACK: accomodate incompatible DLPI drivers */
705 	if (addr_length == 8)
706 		addr_length = 6;
707 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
708 	dlur->dl_dest_addr_offset = sizeof (*dlur);
709 	dlur->dl_priority.dl_min = 0;
710 	dlur->dl_priority.dl_max = 0;
711 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
712 	    (uchar_t *)&dlur[1]);
713 	return (mp);
714 }
715 
716 /*
717  * Add the pending mp to the list. There can be only 1 pending mp
718  * in the list. Any exclusive ioctl that needs to wait for a response
719  * from another module or driver needs to use this function to set
720  * the ipx_pending_mp to the ioctl mblk and wait for the response from
721  * the other module/driver. This is also used while waiting for the
722  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
723  */
724 boolean_t
725 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
726     int waitfor)
727 {
728 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
729 
730 	ASSERT(IAM_WRITER_IPIF(ipif));
731 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
732 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
733 	ASSERT(ipx->ipx_pending_mp == NULL);
734 	/*
735 	 * The caller may be using a different ipif than the one passed into
736 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
737 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
738 	 * that `ipx_current_ipif == ipif'.
739 	 */
740 	ASSERT(ipx->ipx_current_ipif != NULL);
741 
742 	/*
743 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
744 	 * driver.
745 	 */
746 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
747 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
748 	    (DB_TYPE(add_mp) == M_PCPROTO));
749 
750 	if (connp != NULL) {
751 		ASSERT(MUTEX_HELD(&connp->conn_lock));
752 		/*
753 		 * Return error if the conn has started closing. The conn
754 		 * could have finished cleaning up the pending mp list,
755 		 * If so we should not add another mp to the list negating
756 		 * the cleanup.
757 		 */
758 		if (connp->conn_state_flags & CONN_CLOSING)
759 			return (B_FALSE);
760 	}
761 	mutex_enter(&ipx->ipx_lock);
762 	ipx->ipx_pending_ipif = ipif;
763 	/*
764 	 * Note down the queue in b_queue. This will be returned by
765 	 * ipsq_pending_mp_get. Caller will then use these values to restart
766 	 * the processing
767 	 */
768 	add_mp->b_next = NULL;
769 	add_mp->b_queue = q;
770 	ipx->ipx_pending_mp = add_mp;
771 	ipx->ipx_waitfor = waitfor;
772 	mutex_exit(&ipx->ipx_lock);
773 
774 	if (connp != NULL)
775 		connp->conn_oper_pending_ill = ipif->ipif_ill;
776 
777 	return (B_TRUE);
778 }
779 
780 /*
781  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
782  * queued in the list.
783  */
784 mblk_t *
785 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
786 {
787 	mblk_t	*curr = NULL;
788 	ipxop_t	*ipx = ipsq->ipsq_xop;
789 
790 	*connpp = NULL;
791 	mutex_enter(&ipx->ipx_lock);
792 	if (ipx->ipx_pending_mp == NULL) {
793 		mutex_exit(&ipx->ipx_lock);
794 		return (NULL);
795 	}
796 
797 	/* There can be only 1 such excl message */
798 	curr = ipx->ipx_pending_mp;
799 	ASSERT(curr->b_next == NULL);
800 	ipx->ipx_pending_ipif = NULL;
801 	ipx->ipx_pending_mp = NULL;
802 	ipx->ipx_waitfor = 0;
803 	mutex_exit(&ipx->ipx_lock);
804 
805 	if (CONN_Q(curr->b_queue)) {
806 		/*
807 		 * This mp did a refhold on the conn, at the start of the ioctl.
808 		 * So we can safely return a pointer to the conn to the caller.
809 		 */
810 		*connpp = Q_TO_CONN(curr->b_queue);
811 	} else {
812 		*connpp = NULL;
813 	}
814 	curr->b_next = NULL;
815 	curr->b_prev = NULL;
816 	return (curr);
817 }
818 
819 /*
820  * Cleanup the ioctl mp queued in ipx_pending_mp
821  * - Called in the ill_delete path
822  * - Called in the M_ERROR or M_HANGUP path on the ill.
823  * - Called in the conn close path.
824  */
825 boolean_t
826 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
827 {
828 	mblk_t	*mp;
829 	ipxop_t	*ipx;
830 	queue_t	*q;
831 	ipif_t	*ipif;
832 	int	cmd;
833 
834 	ASSERT(IAM_WRITER_ILL(ill));
835 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
836 
837 	/*
838 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
839 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
840 	 * even if it is meant for another ill, since we have to enqueue
841 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
842 	 * If connp is non-null we are called from the conn close path.
843 	 */
844 	mutex_enter(&ipx->ipx_lock);
845 	mp = ipx->ipx_pending_mp;
846 	if (mp == NULL || (connp != NULL &&
847 	    mp->b_queue != CONNP_TO_WQ(connp))) {
848 		mutex_exit(&ipx->ipx_lock);
849 		return (B_FALSE);
850 	}
851 	/* Now remove from the ipx_pending_mp */
852 	ipx->ipx_pending_mp = NULL;
853 	q = mp->b_queue;
854 	mp->b_next = NULL;
855 	mp->b_prev = NULL;
856 	mp->b_queue = NULL;
857 
858 	ipif = ipx->ipx_pending_ipif;
859 	ipx->ipx_pending_ipif = NULL;
860 	ipx->ipx_waitfor = 0;
861 	ipx->ipx_current_ipif = NULL;
862 	cmd = ipx->ipx_current_ioctl;
863 	ipx->ipx_current_ioctl = 0;
864 	ipx->ipx_current_done = B_TRUE;
865 	mutex_exit(&ipx->ipx_lock);
866 
867 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
868 		DTRACE_PROBE4(ipif__ioctl,
869 		    char *, "ipsq_pending_mp_cleanup",
870 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
871 		    ipif_t *, ipif);
872 		if (connp == NULL) {
873 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
874 		} else {
875 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
876 			mutex_enter(&ipif->ipif_ill->ill_lock);
877 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
878 			mutex_exit(&ipif->ipif_ill->ill_lock);
879 		}
880 	} else {
881 		/*
882 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
883 		 * be just inet_freemsg. we have to restart it
884 		 * otherwise the thread will be stuck.
885 		 */
886 		inet_freemsg(mp);
887 	}
888 	return (B_TRUE);
889 }
890 
891 /*
892  * Called in the conn close path and ill delete path
893  */
894 static void
895 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
896 {
897 	ipsq_t	*ipsq;
898 	mblk_t	*prev;
899 	mblk_t	*curr;
900 	mblk_t	*next;
901 	queue_t	*q;
902 	mblk_t	*tmp_list = NULL;
903 
904 	ASSERT(IAM_WRITER_ILL(ill));
905 	if (connp != NULL)
906 		q = CONNP_TO_WQ(connp);
907 	else
908 		q = ill->ill_wq;
909 
910 	ipsq = ill->ill_phyint->phyint_ipsq;
911 	/*
912 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
913 	 * In the case of ioctl from a conn, there can be only 1 mp
914 	 * queued on the ipsq. If an ill is being unplumbed, only messages
915 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
916 	 * ioctls meant for this ill form conn's are not flushed. They will
917 	 * be processed during ipsq_exit and will not find the ill and will
918 	 * return error.
919 	 */
920 	mutex_enter(&ipsq->ipsq_lock);
921 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
922 	    curr = next) {
923 		next = curr->b_next;
924 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
925 			/* Unlink the mblk from the pending mp list */
926 			if (prev != NULL) {
927 				prev->b_next = curr->b_next;
928 			} else {
929 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
930 				ipsq->ipsq_xopq_mphead = curr->b_next;
931 			}
932 			if (ipsq->ipsq_xopq_mptail == curr)
933 				ipsq->ipsq_xopq_mptail = prev;
934 			/*
935 			 * Create a temporary list and release the ipsq lock
936 			 * New elements are added to the head of the tmp_list
937 			 */
938 			curr->b_next = tmp_list;
939 			tmp_list = curr;
940 		} else {
941 			prev = curr;
942 		}
943 	}
944 	mutex_exit(&ipsq->ipsq_lock);
945 
946 	while (tmp_list != NULL) {
947 		curr = tmp_list;
948 		tmp_list = curr->b_next;
949 		curr->b_next = NULL;
950 		curr->b_prev = NULL;
951 		curr->b_queue = NULL;
952 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
953 			DTRACE_PROBE4(ipif__ioctl,
954 			    char *, "ipsq_xopq_mp_cleanup",
955 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
956 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
957 			    CONN_CLOSE : NO_COPYOUT, NULL);
958 		} else {
959 			/*
960 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
961 			 * this can't be just inet_freemsg. we have to
962 			 * restart it otherwise the thread will be stuck.
963 			 */
964 			inet_freemsg(curr);
965 		}
966 	}
967 }
968 
969 /*
970  * This conn has started closing. Cleanup any pending ioctl from this conn.
971  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
972  */
973 void
974 conn_ioctl_cleanup(conn_t *connp)
975 {
976 	ipsq_t	*ipsq;
977 	ill_t	*ill;
978 	boolean_t refheld;
979 
980 	/*
981 	 * Is any exclusive ioctl pending ? If so clean it up. If the
982 	 * ioctl has not yet started, the mp is pending in the list headed by
983 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
984 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
985 	 * is currently executing now the mp is not queued anywhere but
986 	 * conn_oper_pending_ill is null. The conn close will wait
987 	 * till the conn_ref drops to zero.
988 	 */
989 	mutex_enter(&connp->conn_lock);
990 	ill = connp->conn_oper_pending_ill;
991 	if (ill == NULL) {
992 		mutex_exit(&connp->conn_lock);
993 		return;
994 	}
995 
996 	/*
997 	 * We may not be able to refhold the ill if the ill/ipif
998 	 * is changing. But we need to make sure that the ill will
999 	 * not vanish. So we just bump up the ill_waiter count.
1000 	 */
1001 	refheld = ill_waiter_inc(ill);
1002 	mutex_exit(&connp->conn_lock);
1003 	if (refheld) {
1004 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1005 			ill_waiter_dcr(ill);
1006 			/*
1007 			 * Check whether this ioctl has started and is
1008 			 * pending. If it is not found there then check
1009 			 * whether this ioctl has not even started and is in
1010 			 * the ipsq_xopq list.
1011 			 */
1012 			if (!ipsq_pending_mp_cleanup(ill, connp))
1013 				ipsq_xopq_mp_cleanup(ill, connp);
1014 			ipsq = ill->ill_phyint->phyint_ipsq;
1015 			ipsq_exit(ipsq);
1016 			return;
1017 		}
1018 	}
1019 
1020 	/*
1021 	 * The ill is also closing and we could not bump up the
1022 	 * ill_waiter_count or we could not enter the ipsq. Leave
1023 	 * the cleanup to ill_delete
1024 	 */
1025 	mutex_enter(&connp->conn_lock);
1026 	while (connp->conn_oper_pending_ill != NULL)
1027 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1028 	mutex_exit(&connp->conn_lock);
1029 	if (refheld)
1030 		ill_waiter_dcr(ill);
1031 }
1032 
1033 /*
1034  * ipcl_walk function for cleaning up conn_*_ill fields.
1035  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1036  * conn_bound_if in place. We prefer dropping
1037  * packets instead of sending them out the wrong interface, or accepting
1038  * packets from the wrong ifindex.
1039  */
1040 static void
1041 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1042 {
1043 	ill_t	*ill = (ill_t *)arg;
1044 
1045 	mutex_enter(&connp->conn_lock);
1046 	if (connp->conn_dhcpinit_ill == ill) {
1047 		connp->conn_dhcpinit_ill = NULL;
1048 		ASSERT(ill->ill_dhcpinit != 0);
1049 		atomic_dec_32(&ill->ill_dhcpinit);
1050 		ill_set_inputfn(ill);
1051 	}
1052 	mutex_exit(&connp->conn_lock);
1053 }
1054 
1055 static int
1056 ill_down_ipifs_tail(ill_t *ill)
1057 {
1058 	ipif_t	*ipif;
1059 	int err;
1060 
1061 	ASSERT(IAM_WRITER_ILL(ill));
1062 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1063 		ipif_non_duplicate(ipif);
1064 		/*
1065 		 * ipif_down_tail will call arp_ll_down on the last ipif
1066 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1067 		 */
1068 		if ((err = ipif_down_tail(ipif)) != 0)
1069 			return (err);
1070 	}
1071 	return (0);
1072 }
1073 
1074 /* ARGSUSED */
1075 void
1076 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1077 {
1078 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1079 	(void) ill_down_ipifs_tail(q->q_ptr);
1080 	freemsg(mp);
1081 	ipsq_current_finish(ipsq);
1082 }
1083 
1084 /*
1085  * ill_down_start is called when we want to down this ill and bring it up again
1086  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1087  * all interfaces, but don't tear down any plumbing.
1088  */
1089 boolean_t
1090 ill_down_start(queue_t *q, mblk_t *mp)
1091 {
1092 	ill_t	*ill = q->q_ptr;
1093 	ipif_t	*ipif;
1094 
1095 	ASSERT(IAM_WRITER_ILL(ill));
1096 	mutex_enter(&ill->ill_lock);
1097 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1098 	/* no more nce addition allowed */
1099 	mutex_exit(&ill->ill_lock);
1100 
1101 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1102 		(void) ipif_down(ipif, NULL, NULL);
1103 
1104 	ill_down(ill);
1105 
1106 	/*
1107 	 * Walk all CONNs that can have a reference on an ire or nce for this
1108 	 * ill (we actually walk all that now have stale references).
1109 	 */
1110 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1111 
1112 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1113 	if (ill->ill_isv6)
1114 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1115 
1116 
1117 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1118 
1119 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1120 
1121 	/*
1122 	 * Atomically test and add the pending mp if references are active.
1123 	 */
1124 	mutex_enter(&ill->ill_lock);
1125 	if (!ill_is_quiescent(ill)) {
1126 		/* call cannot fail since `conn_t *' argument is NULL */
1127 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1128 		    mp, ILL_DOWN);
1129 		mutex_exit(&ill->ill_lock);
1130 		return (B_FALSE);
1131 	}
1132 	mutex_exit(&ill->ill_lock);
1133 	return (B_TRUE);
1134 }
1135 
1136 static void
1137 ill_down(ill_t *ill)
1138 {
1139 	mblk_t	*mp;
1140 	ip_stack_t	*ipst = ill->ill_ipst;
1141 
1142 	/*
1143 	 * Blow off any IREs dependent on this ILL.
1144 	 * The caller needs to handle conn_ixa_cleanup
1145 	 */
1146 	ill_delete_ires(ill);
1147 
1148 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1149 
1150 	/* Remove any conn_*_ill depending on this ill */
1151 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1152 
1153 	/*
1154 	 * Free state for additional IREs.
1155 	 */
1156 	mutex_enter(&ill->ill_saved_ire_lock);
1157 	mp = ill->ill_saved_ire_mp;
1158 	ill->ill_saved_ire_mp = NULL;
1159 	ill->ill_saved_ire_cnt = 0;
1160 	mutex_exit(&ill->ill_saved_ire_lock);
1161 	freemsg(mp);
1162 }
1163 
1164 /*
1165  * ire_walk routine used to delete every IRE that depends on
1166  * 'ill'.  (Always called as writer.)
1167  *
1168  * Note: since the routes added by the kernel are deleted separately,
1169  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1170  *
1171  * We also remove references on ire_nce_cache entries that refer to the ill.
1172  */
1173 void
1174 ill_downi(ire_t *ire, char *ill_arg)
1175 {
1176 	ill_t	*ill = (ill_t *)ill_arg;
1177 	nce_t	*nce;
1178 
1179 	mutex_enter(&ire->ire_lock);
1180 	nce = ire->ire_nce_cache;
1181 	if (nce != NULL && nce->nce_ill == ill)
1182 		ire->ire_nce_cache = NULL;
1183 	else
1184 		nce = NULL;
1185 	mutex_exit(&ire->ire_lock);
1186 	if (nce != NULL)
1187 		nce_refrele(nce);
1188 	if (ire->ire_ill == ill)
1189 		ire_delete(ire);
1190 }
1191 
1192 /* Remove IRE_IF_CLONE on this ill */
1193 void
1194 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1195 {
1196 	ill_t	*ill = (ill_t *)ill_arg;
1197 
1198 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1199 	if (ire->ire_ill == ill)
1200 		ire_delete(ire);
1201 }
1202 
1203 /* Consume an M_IOCACK of the fastpath probe. */
1204 void
1205 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1206 {
1207 	mblk_t	*mp1 = mp;
1208 
1209 	/*
1210 	 * If this was the first attempt turn on the fastpath probing.
1211 	 */
1212 	mutex_enter(&ill->ill_lock);
1213 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1214 		ill->ill_dlpi_fastpath_state = IDS_OK;
1215 	mutex_exit(&ill->ill_lock);
1216 
1217 	/* Free the M_IOCACK mblk, hold on to the data */
1218 	mp = mp->b_cont;
1219 	freeb(mp1);
1220 	if (mp == NULL)
1221 		return;
1222 	if (mp->b_cont != NULL)
1223 		nce_fastpath_update(ill, mp);
1224 	else
1225 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1226 	freemsg(mp);
1227 }
1228 
1229 /*
1230  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1231  * The data portion of the request is a dl_unitdata_req_t template for
1232  * what we would send downstream in the absence of a fastpath confirmation.
1233  */
1234 int
1235 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1236 {
1237 	struct iocblk	*ioc;
1238 	mblk_t	*mp;
1239 
1240 	if (dlur_mp == NULL)
1241 		return (EINVAL);
1242 
1243 	mutex_enter(&ill->ill_lock);
1244 	switch (ill->ill_dlpi_fastpath_state) {
1245 	case IDS_FAILED:
1246 		/*
1247 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1248 		 * support it.
1249 		 */
1250 		mutex_exit(&ill->ill_lock);
1251 		return (ENOTSUP);
1252 	case IDS_UNKNOWN:
1253 		/* This is the first probe */
1254 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1255 		break;
1256 	default:
1257 		break;
1258 	}
1259 	mutex_exit(&ill->ill_lock);
1260 
1261 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1262 		return (EAGAIN);
1263 
1264 	mp->b_cont = copyb(dlur_mp);
1265 	if (mp->b_cont == NULL) {
1266 		freeb(mp);
1267 		return (EAGAIN);
1268 	}
1269 
1270 	ioc = (struct iocblk *)mp->b_rptr;
1271 	ioc->ioc_count = msgdsize(mp->b_cont);
1272 
1273 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1274 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1275 	putnext(ill->ill_wq, mp);
1276 	return (0);
1277 }
1278 
1279 void
1280 ill_capability_probe(ill_t *ill)
1281 {
1282 	mblk_t	*mp;
1283 
1284 	ASSERT(IAM_WRITER_ILL(ill));
1285 
1286 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1287 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1288 		return;
1289 
1290 	/*
1291 	 * We are starting a new cycle of capability negotiation.
1292 	 * Free up the capab reset messages of any previous incarnation.
1293 	 * We will do a fresh allocation when we get the response to our probe
1294 	 */
1295 	if (ill->ill_capab_reset_mp != NULL) {
1296 		freemsg(ill->ill_capab_reset_mp);
1297 		ill->ill_capab_reset_mp = NULL;
1298 	}
1299 
1300 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1301 
1302 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1303 	if (mp == NULL)
1304 		return;
1305 
1306 	ill_capability_send(ill, mp);
1307 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1308 }
1309 
1310 void
1311 ill_capability_reset(ill_t *ill, boolean_t reneg)
1312 {
1313 	ASSERT(IAM_WRITER_ILL(ill));
1314 
1315 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1316 		return;
1317 
1318 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1319 
1320 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1321 	ill->ill_capab_reset_mp = NULL;
1322 	/*
1323 	 * We turn off all capabilities except those pertaining to
1324 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1325 	 * which will be turned off by the corresponding reset functions.
1326 	 */
1327 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1328 }
1329 
1330 static void
1331 ill_capability_reset_alloc(ill_t *ill)
1332 {
1333 	mblk_t *mp;
1334 	size_t	size = 0;
1335 	int	err;
1336 	dl_capability_req_t	*capb;
1337 
1338 	ASSERT(IAM_WRITER_ILL(ill));
1339 	ASSERT(ill->ill_capab_reset_mp == NULL);
1340 
1341 	if (ILL_HCKSUM_CAPABLE(ill)) {
1342 		size += sizeof (dl_capability_sub_t) +
1343 		    sizeof (dl_capab_hcksum_t);
1344 	}
1345 
1346 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1347 		size += sizeof (dl_capability_sub_t) +
1348 		    sizeof (dl_capab_zerocopy_t);
1349 	}
1350 
1351 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1352 		size += sizeof (dl_capability_sub_t) +
1353 		    sizeof (dl_capab_dld_t);
1354 	}
1355 
1356 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1357 	    STR_NOSIG, &err);
1358 
1359 	mp->b_datap->db_type = M_PROTO;
1360 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1361 
1362 	capb = (dl_capability_req_t *)mp->b_rptr;
1363 	capb->dl_primitive = DL_CAPABILITY_REQ;
1364 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1365 	capb->dl_sub_length = size;
1366 
1367 	mp->b_wptr += sizeof (dl_capability_req_t);
1368 
1369 	/*
1370 	 * Each handler fills in the corresponding dl_capability_sub_t
1371 	 * inside the mblk,
1372 	 */
1373 	ill_capability_hcksum_reset_fill(ill, mp);
1374 	ill_capability_zerocopy_reset_fill(ill, mp);
1375 	ill_capability_dld_reset_fill(ill, mp);
1376 
1377 	ill->ill_capab_reset_mp = mp;
1378 }
1379 
1380 static void
1381 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1382 {
1383 	dl_capab_id_t *id_ic;
1384 	uint_t sub_dl_cap = outers->dl_cap;
1385 	dl_capability_sub_t *inners;
1386 	uint8_t *capend;
1387 
1388 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1389 
1390 	/*
1391 	 * Note: range checks here are not absolutely sufficient to
1392 	 * make us robust against malformed messages sent by drivers;
1393 	 * this is in keeping with the rest of IP's dlpi handling.
1394 	 * (Remember, it's coming from something else in the kernel
1395 	 * address space)
1396 	 */
1397 
1398 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1399 	if (capend > mp->b_wptr) {
1400 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1401 		    "malformed sub-capability too long for mblk");
1402 		return;
1403 	}
1404 
1405 	id_ic = (dl_capab_id_t *)(outers + 1);
1406 
1407 	if (outers->dl_length < sizeof (*id_ic) ||
1408 	    (inners = &id_ic->id_subcap,
1409 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1410 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1411 		    "encapsulated capab type %d too long for mblk",
1412 		    inners->dl_cap);
1413 		return;
1414 	}
1415 
1416 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1417 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1418 		    "isn't as expected; pass-thru module(s) detected, "
1419 		    "discarding capability\n", inners->dl_cap));
1420 		return;
1421 	}
1422 
1423 	/* Process the encapsulated sub-capability */
1424 	ill_capability_dispatch(ill, mp, inners);
1425 }
1426 
1427 static void
1428 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1429 {
1430 	dl_capability_sub_t *dl_subcap;
1431 
1432 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1433 		return;
1434 
1435 	/*
1436 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1437 	 * initialized below since it is not used by DLD.
1438 	 */
1439 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1440 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1441 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1442 
1443 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1444 }
1445 
1446 static void
1447 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1448 {
1449 	switch (subp->dl_cap) {
1450 	case DL_CAPAB_HCKSUM:
1451 		ill_capability_hcksum_ack(ill, mp, subp);
1452 		break;
1453 	case DL_CAPAB_ZEROCOPY:
1454 		ill_capability_zerocopy_ack(ill, mp, subp);
1455 		break;
1456 	case DL_CAPAB_DLD:
1457 		ill_capability_dld_ack(ill, mp, subp);
1458 		break;
1459 	default:
1460 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1461 		    subp->dl_cap));
1462 	}
1463 }
1464 
1465 /*
1466  * Process a hardware checksum offload capability negotiation ack received
1467  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1468  * of a DL_CAPABILITY_ACK message.
1469  */
1470 static void
1471 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1472 {
1473 	dl_capability_req_t	*ocap;
1474 	dl_capab_hcksum_t	*ihck, *ohck;
1475 	ill_hcksum_capab_t	**ill_hcksum;
1476 	mblk_t			*nmp = NULL;
1477 	uint_t			sub_dl_cap = isub->dl_cap;
1478 	uint8_t			*capend;
1479 
1480 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1481 
1482 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1483 
1484 	/*
1485 	 * Note: range checks here are not absolutely sufficient to
1486 	 * make us robust against malformed messages sent by drivers;
1487 	 * this is in keeping with the rest of IP's dlpi handling.
1488 	 * (Remember, it's coming from something else in the kernel
1489 	 * address space)
1490 	 */
1491 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1492 	if (capend > mp->b_wptr) {
1493 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1494 		    "malformed sub-capability too long for mblk");
1495 		return;
1496 	}
1497 
1498 	/*
1499 	 * There are two types of acks we process here:
1500 	 * 1. acks in reply to a (first form) generic capability req
1501 	 *    (no ENABLE flag set)
1502 	 * 2. acks in reply to a ENABLE capability req.
1503 	 *    (ENABLE flag set)
1504 	 */
1505 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1506 
1507 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1508 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1509 		    "unsupported hardware checksum "
1510 		    "sub-capability (version %d, expected %d)",
1511 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1512 		return;
1513 	}
1514 
1515 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1516 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1517 		    "checksum capability isn't as expected; pass-thru "
1518 		    "module(s) detected, discarding capability\n"));
1519 		return;
1520 	}
1521 
1522 #define	CURR_HCKSUM_CAPAB				\
1523 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1524 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1525 
1526 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1527 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1528 		/* do ENABLE processing */
1529 		if (*ill_hcksum == NULL) {
1530 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1531 			    KM_NOSLEEP);
1532 
1533 			if (*ill_hcksum == NULL) {
1534 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1535 				    "could not enable hcksum version %d "
1536 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1537 				    ill->ill_name);
1538 				return;
1539 			}
1540 		}
1541 
1542 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1543 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1544 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1545 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1546 		    "has enabled hardware checksumming\n ",
1547 		    ill->ill_name));
1548 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1549 		/*
1550 		 * Enabling hardware checksum offload
1551 		 * Currently IP supports {TCP,UDP}/IPv4
1552 		 * partial and full cksum offload and
1553 		 * IPv4 header checksum offload.
1554 		 * Allocate new mblk which will
1555 		 * contain a new capability request
1556 		 * to enable hardware checksum offload.
1557 		 */
1558 		uint_t	size;
1559 		uchar_t	*rptr;
1560 
1561 		size = sizeof (dl_capability_req_t) +
1562 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1563 
1564 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1565 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1566 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1567 			    ill->ill_name);
1568 			return;
1569 		}
1570 
1571 		rptr = nmp->b_rptr;
1572 		/* initialize dl_capability_req_t */
1573 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1574 		ocap->dl_sub_offset =
1575 		    sizeof (dl_capability_req_t);
1576 		ocap->dl_sub_length =
1577 		    sizeof (dl_capability_sub_t) +
1578 		    isub->dl_length;
1579 		nmp->b_rptr += sizeof (dl_capability_req_t);
1580 
1581 		/* initialize dl_capability_sub_t */
1582 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1583 		nmp->b_rptr += sizeof (*isub);
1584 
1585 		/* initialize dl_capab_hcksum_t */
1586 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1587 		bcopy(ihck, ohck, sizeof (*ihck));
1588 
1589 		nmp->b_rptr = rptr;
1590 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1591 
1592 		/* Set ENABLE flag */
1593 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1594 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1595 
1596 		/*
1597 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1598 		 * hardware checksum acceleration.
1599 		 */
1600 		ill_capability_send(ill, nmp);
1601 	} else {
1602 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1603 		    "advertised %x hardware checksum capability flags\n",
1604 		    ill->ill_name, ihck->hcksum_txflags));
1605 	}
1606 }
1607 
1608 static void
1609 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1610 {
1611 	dl_capab_hcksum_t *hck_subcap;
1612 	dl_capability_sub_t *dl_subcap;
1613 
1614 	if (!ILL_HCKSUM_CAPABLE(ill))
1615 		return;
1616 
1617 	ASSERT(ill->ill_hcksum_capab != NULL);
1618 
1619 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1620 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1621 	dl_subcap->dl_length = sizeof (*hck_subcap);
1622 
1623 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1624 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1625 	hck_subcap->hcksum_txflags = 0;
1626 
1627 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1628 }
1629 
1630 static void
1631 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1632 {
1633 	mblk_t *nmp = NULL;
1634 	dl_capability_req_t *oc;
1635 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1636 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1637 	uint_t sub_dl_cap = isub->dl_cap;
1638 	uint8_t *capend;
1639 
1640 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1641 
1642 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1643 
1644 	/*
1645 	 * Note: range checks here are not absolutely sufficient to
1646 	 * make us robust against malformed messages sent by drivers;
1647 	 * this is in keeping with the rest of IP's dlpi handling.
1648 	 * (Remember, it's coming from something else in the kernel
1649 	 * address space)
1650 	 */
1651 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1652 	if (capend > mp->b_wptr) {
1653 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1654 		    "malformed sub-capability too long for mblk");
1655 		return;
1656 	}
1657 
1658 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1659 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1660 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1661 		    "unsupported ZEROCOPY sub-capability (version %d, "
1662 		    "expected %d)", zc_ic->zerocopy_version,
1663 		    ZEROCOPY_VERSION_1);
1664 		return;
1665 	}
1666 
1667 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1668 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1669 		    "capability isn't as expected; pass-thru module(s) "
1670 		    "detected, discarding capability\n"));
1671 		return;
1672 	}
1673 
1674 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1675 		if (*ill_zerocopy_capab == NULL) {
1676 			*ill_zerocopy_capab =
1677 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1678 			    KM_NOSLEEP);
1679 
1680 			if (*ill_zerocopy_capab == NULL) {
1681 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1682 				    "could not enable Zero-copy version %d "
1683 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1684 				    ill->ill_name);
1685 				return;
1686 			}
1687 		}
1688 
1689 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1690 		    "supports Zero-copy version %d\n", ill->ill_name,
1691 		    ZEROCOPY_VERSION_1));
1692 
1693 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1694 		    zc_ic->zerocopy_version;
1695 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1696 		    zc_ic->zerocopy_flags;
1697 
1698 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1699 	} else {
1700 		uint_t size;
1701 		uchar_t *rptr;
1702 
1703 		size = sizeof (dl_capability_req_t) +
1704 		    sizeof (dl_capability_sub_t) +
1705 		    sizeof (dl_capab_zerocopy_t);
1706 
1707 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1708 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1709 			    "could not enable zerocopy for %s (ENOMEM)\n",
1710 			    ill->ill_name);
1711 			return;
1712 		}
1713 
1714 		rptr = nmp->b_rptr;
1715 		/* initialize dl_capability_req_t */
1716 		oc = (dl_capability_req_t *)rptr;
1717 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1718 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1719 		    sizeof (dl_capab_zerocopy_t);
1720 		rptr += sizeof (dl_capability_req_t);
1721 
1722 		/* initialize dl_capability_sub_t */
1723 		bcopy(isub, rptr, sizeof (*isub));
1724 		rptr += sizeof (*isub);
1725 
1726 		/* initialize dl_capab_zerocopy_t */
1727 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1728 		*zc_oc = *zc_ic;
1729 
1730 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1731 		    "to enable zero-copy version %d\n", ill->ill_name,
1732 		    ZEROCOPY_VERSION_1));
1733 
1734 		/* set VMSAFE_MEM flag */
1735 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1736 
1737 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1738 		ill_capability_send(ill, nmp);
1739 	}
1740 }
1741 
1742 static void
1743 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1744 {
1745 	dl_capab_zerocopy_t *zerocopy_subcap;
1746 	dl_capability_sub_t *dl_subcap;
1747 
1748 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1749 		return;
1750 
1751 	ASSERT(ill->ill_zerocopy_capab != NULL);
1752 
1753 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1754 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1755 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1756 
1757 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1758 	zerocopy_subcap->zerocopy_version =
1759 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1760 	zerocopy_subcap->zerocopy_flags = 0;
1761 
1762 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1763 }
1764 
1765 /*
1766  * DLD capability
1767  * Refer to dld.h for more information regarding the purpose and usage
1768  * of this capability.
1769  */
1770 static void
1771 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1772 {
1773 	dl_capab_dld_t		*dld_ic, dld;
1774 	uint_t			sub_dl_cap = isub->dl_cap;
1775 	uint8_t			*capend;
1776 	ill_dld_capab_t		*idc;
1777 
1778 	ASSERT(IAM_WRITER_ILL(ill));
1779 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1780 
1781 	/*
1782 	 * Note: range checks here are not absolutely sufficient to
1783 	 * make us robust against malformed messages sent by drivers;
1784 	 * this is in keeping with the rest of IP's dlpi handling.
1785 	 * (Remember, it's coming from something else in the kernel
1786 	 * address space)
1787 	 */
1788 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1789 	if (capend > mp->b_wptr) {
1790 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1791 		    "malformed sub-capability too long for mblk");
1792 		return;
1793 	}
1794 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1795 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1796 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1797 		    "unsupported DLD sub-capability (version %d, "
1798 		    "expected %d)", dld_ic->dld_version,
1799 		    DLD_CURRENT_VERSION);
1800 		return;
1801 	}
1802 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1803 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1804 		    "capability isn't as expected; pass-thru module(s) "
1805 		    "detected, discarding capability\n"));
1806 		return;
1807 	}
1808 
1809 	/*
1810 	 * Copy locally to ensure alignment.
1811 	 */
1812 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1813 
1814 	if ((idc = ill->ill_dld_capab) == NULL) {
1815 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1816 		if (idc == NULL) {
1817 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1818 			    "could not enable DLD version %d "
1819 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1820 			    ill->ill_name);
1821 			return;
1822 		}
1823 		ill->ill_dld_capab = idc;
1824 	}
1825 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1826 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1827 	ip1dbg(("ill_capability_dld_ack: interface %s "
1828 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1829 
1830 	ill_capability_dld_enable(ill);
1831 }
1832 
1833 /*
1834  * Typically capability negotiation between IP and the driver happens via
1835  * DLPI message exchange. However GLD also offers a direct function call
1836  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1837  * But arbitrary function calls into IP or GLD are not permitted, since both
1838  * of them are protected by their own perimeter mechanism. The perimeter can
1839  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1840  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1841  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1842  * to enter the mac perimeter and then do the direct function calls into
1843  * GLD to enable squeue polling. The ring related callbacks from the mac into
1844  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1845  * protected by the mac perimeter.
1846  */
1847 static void
1848 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1849 {
1850 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1851 	int			err;
1852 
1853 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1854 	    DLD_ENABLE);
1855 	ASSERT(err == 0);
1856 }
1857 
1858 static void
1859 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1860 {
1861 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1862 	int			err;
1863 
1864 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1865 	    DLD_DISABLE);
1866 	ASSERT(err == 0);
1867 }
1868 
1869 boolean_t
1870 ill_mac_perim_held(ill_t *ill)
1871 {
1872 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1873 
1874 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1875 	    DLD_QUERY));
1876 }
1877 
1878 static void
1879 ill_capability_direct_enable(ill_t *ill)
1880 {
1881 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1882 	ill_dld_direct_t	*idd = &idc->idc_direct;
1883 	dld_capab_direct_t	direct;
1884 	int			rc;
1885 
1886 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1887 
1888 	bzero(&direct, sizeof (direct));
1889 	direct.di_rx_cf = (uintptr_t)ip_input;
1890 	direct.di_rx_ch = ill;
1891 
1892 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1893 	    DLD_ENABLE);
1894 	if (rc == 0) {
1895 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1896 		idd->idd_tx_dh = direct.di_tx_dh;
1897 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1898 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1899 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1900 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1901 		ASSERT(idd->idd_tx_cb_df != NULL);
1902 		ASSERT(idd->idd_tx_fctl_df != NULL);
1903 		ASSERT(idd->idd_tx_df != NULL);
1904 		/*
1905 		 * One time registration of flow enable callback function
1906 		 */
1907 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
1908 		    ill_flow_enable, ill);
1909 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
1910 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
1911 	} else {
1912 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
1913 		    "capability, rc = %d\n", rc);
1914 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
1915 	}
1916 }
1917 
1918 static void
1919 ill_capability_poll_enable(ill_t *ill)
1920 {
1921 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1922 	dld_capab_poll_t	poll;
1923 	int			rc;
1924 
1925 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1926 
1927 	bzero(&poll, sizeof (poll));
1928 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
1929 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
1930 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
1931 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
1932 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
1933 	poll.poll_ring_ch = ill;
1934 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
1935 	    DLD_ENABLE);
1936 	if (rc == 0) {
1937 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
1938 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
1939 	} else {
1940 		ip1dbg(("warning: could not enable POLL "
1941 		    "capability, rc = %d\n", rc));
1942 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
1943 	}
1944 }
1945 
1946 /*
1947  * Enable the LSO capability.
1948  */
1949 static void
1950 ill_capability_lso_enable(ill_t *ill)
1951 {
1952 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
1953 	dld_capab_lso_t	lso;
1954 	int rc;
1955 
1956 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1957 
1958 	if (ill->ill_lso_capab == NULL) {
1959 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
1960 		    KM_NOSLEEP);
1961 		if (ill->ill_lso_capab == NULL) {
1962 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
1963 			    "could not enable LSO for %s (ENOMEM)\n",
1964 			    ill->ill_name);
1965 			return;
1966 		}
1967 	}
1968 
1969 	bzero(&lso, sizeof (lso));
1970 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
1971 	    DLD_ENABLE)) == 0) {
1972 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
1973 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
1974 		ill->ill_capabilities |= ILL_CAPAB_LSO;
1975 		ip1dbg(("ill_capability_lso_enable: interface %s "
1976 		    "has enabled LSO\n ", ill->ill_name));
1977 	} else {
1978 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
1979 		ill->ill_lso_capab = NULL;
1980 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
1981 	}
1982 }
1983 
1984 static void
1985 ill_capability_dld_enable(ill_t *ill)
1986 {
1987 	mac_perim_handle_t mph;
1988 
1989 	ASSERT(IAM_WRITER_ILL(ill));
1990 
1991 	if (ill->ill_isv6)
1992 		return;
1993 
1994 	ill_mac_perim_enter(ill, &mph);
1995 	if (!ill->ill_isv6) {
1996 		ill_capability_direct_enable(ill);
1997 		ill_capability_poll_enable(ill);
1998 		ill_capability_lso_enable(ill);
1999 	}
2000 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2001 	ill_mac_perim_exit(ill, mph);
2002 }
2003 
2004 static void
2005 ill_capability_dld_disable(ill_t *ill)
2006 {
2007 	ill_dld_capab_t	*idc;
2008 	ill_dld_direct_t *idd;
2009 	mac_perim_handle_t	mph;
2010 
2011 	ASSERT(IAM_WRITER_ILL(ill));
2012 
2013 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2014 		return;
2015 
2016 	ill_mac_perim_enter(ill, &mph);
2017 
2018 	idc = ill->ill_dld_capab;
2019 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2020 		/*
2021 		 * For performance we avoid locks in the transmit data path
2022 		 * and don't maintain a count of the number of threads using
2023 		 * direct calls. Thus some threads could be using direct
2024 		 * transmit calls to GLD, even after the capability mechanism
2025 		 * turns it off. This is still safe since the handles used in
2026 		 * the direct calls continue to be valid until the unplumb is
2027 		 * completed. Remove the callback that was added (1-time) at
2028 		 * capab enable time.
2029 		 */
2030 		mutex_enter(&ill->ill_lock);
2031 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2032 		mutex_exit(&ill->ill_lock);
2033 		if (ill->ill_flownotify_mh != NULL) {
2034 			idd = &idc->idc_direct;
2035 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2036 			    ill->ill_flownotify_mh);
2037 			ill->ill_flownotify_mh = NULL;
2038 		}
2039 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2040 		    NULL, DLD_DISABLE);
2041 	}
2042 
2043 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2044 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2045 		ip_squeue_clean_all(ill);
2046 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2047 		    NULL, DLD_DISABLE);
2048 	}
2049 
2050 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2051 		ASSERT(ill->ill_lso_capab != NULL);
2052 		/*
2053 		 * Clear the capability flag for LSO but retain the
2054 		 * ill_lso_capab structure since it's possible that another
2055 		 * thread is still referring to it.  The structure only gets
2056 		 * deallocated when we destroy the ill.
2057 		 */
2058 
2059 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2060 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2061 		    NULL, DLD_DISABLE);
2062 	}
2063 
2064 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2065 	ill_mac_perim_exit(ill, mph);
2066 }
2067 
2068 /*
2069  * Capability Negotiation protocol
2070  *
2071  * We don't wait for DLPI capability operations to finish during interface
2072  * bringup or teardown. Doing so would introduce more asynchrony and the
2073  * interface up/down operations will need multiple return and restarts.
2074  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2075  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2076  * exclusive operation won't start until the DLPI operations of the previous
2077  * exclusive operation complete.
2078  *
2079  * The capability state machine is shown below.
2080  *
2081  * state		next state		event, action
2082  *
2083  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2084  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2085  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2086  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2087  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2088  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2089  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2090  *						    ill_capability_probe.
2091  */
2092 
2093 /*
2094  * Dedicated thread started from ip_stack_init that handles capability
2095  * disable. This thread ensures the taskq dispatch does not fail by waiting
2096  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2097  * that direct calls to DLD are done in a cv_waitable context.
2098  */
2099 void
2100 ill_taskq_dispatch(ip_stack_t *ipst)
2101 {
2102 	callb_cpr_t cprinfo;
2103 	char 	name[64];
2104 	mblk_t	*mp;
2105 
2106 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2107 	    ipst->ips_netstack->netstack_stackid);
2108 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2109 	    name);
2110 	mutex_enter(&ipst->ips_capab_taskq_lock);
2111 
2112 	for (;;) {
2113 		mp = ipst->ips_capab_taskq_head;
2114 		while (mp != NULL) {
2115 			ipst->ips_capab_taskq_head = mp->b_next;
2116 			if (ipst->ips_capab_taskq_head == NULL)
2117 				ipst->ips_capab_taskq_tail = NULL;
2118 			mutex_exit(&ipst->ips_capab_taskq_lock);
2119 			mp->b_next = NULL;
2120 
2121 			VERIFY(taskq_dispatch(system_taskq,
2122 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2123 			mutex_enter(&ipst->ips_capab_taskq_lock);
2124 			mp = ipst->ips_capab_taskq_head;
2125 		}
2126 
2127 		if (ipst->ips_capab_taskq_quit)
2128 			break;
2129 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2130 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2131 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2132 	}
2133 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2134 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2135 	CALLB_CPR_EXIT(&cprinfo);
2136 	thread_exit();
2137 }
2138 
2139 /*
2140  * Consume a new-style hardware capabilities negotiation ack.
2141  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
2142  */
2143 static void
2144 ill_capability_ack_thr(void *arg)
2145 {
2146 	mblk_t	*mp = arg;
2147 	dl_capability_ack_t *capp;
2148 	dl_capability_sub_t *subp, *endp;
2149 	ill_t	*ill;
2150 	boolean_t reneg;
2151 
2152 	ill = (ill_t *)mp->b_prev;
2153 	mp->b_prev = NULL;
2154 
2155 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2156 
2157 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2158 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2159 		/*
2160 		 * We have received the ack for our DL_CAPAB reset request.
2161 		 * There isnt' anything in the message that needs processing.
2162 		 * All message based capabilities have been disabled, now
2163 		 * do the function call based capability disable.
2164 		 */
2165 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2166 		ill_capability_dld_disable(ill);
2167 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2168 		if (reneg)
2169 			ill_capability_probe(ill);
2170 		goto done;
2171 	}
2172 
2173 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2174 		ill->ill_dlpi_capab_state = IDCS_OK;
2175 
2176 	capp = (dl_capability_ack_t *)mp->b_rptr;
2177 
2178 	if (capp->dl_sub_length == 0) {
2179 		/* no new-style capabilities */
2180 		goto done;
2181 	}
2182 
2183 	/* make sure the driver supplied correct dl_sub_length */
2184 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2185 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2186 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2187 		goto done;
2188 	}
2189 
2190 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2191 	/*
2192 	 * There are sub-capabilities. Process the ones we know about.
2193 	 * Loop until we don't have room for another sub-cap header..
2194 	 */
2195 	for (subp = SC(capp, capp->dl_sub_offset),
2196 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2197 	    subp <= endp;
2198 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2199 
2200 		switch (subp->dl_cap) {
2201 		case DL_CAPAB_ID_WRAPPER:
2202 			ill_capability_id_ack(ill, mp, subp);
2203 			break;
2204 		default:
2205 			ill_capability_dispatch(ill, mp, subp);
2206 			break;
2207 		}
2208 	}
2209 #undef SC
2210 done:
2211 	inet_freemsg(mp);
2212 	ill_capability_done(ill);
2213 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2214 }
2215 
2216 /*
2217  * This needs to be started in a taskq thread to provide a cv_waitable
2218  * context.
2219  */
2220 void
2221 ill_capability_ack(ill_t *ill, mblk_t *mp)
2222 {
2223 	ip_stack_t	*ipst = ill->ill_ipst;
2224 
2225 	mp->b_prev = (mblk_t *)ill;
2226 	ASSERT(mp->b_next == NULL);
2227 
2228 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2229 	    TQ_NOSLEEP) != 0)
2230 		return;
2231 
2232 	/*
2233 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2234 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2235 	 */
2236 	mutex_enter(&ipst->ips_capab_taskq_lock);
2237 	if (ipst->ips_capab_taskq_head == NULL) {
2238 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2239 		ipst->ips_capab_taskq_head = mp;
2240 	} else {
2241 		ipst->ips_capab_taskq_tail->b_next = mp;
2242 	}
2243 	ipst->ips_capab_taskq_tail = mp;
2244 
2245 	cv_signal(&ipst->ips_capab_taskq_cv);
2246 	mutex_exit(&ipst->ips_capab_taskq_lock);
2247 }
2248 
2249 /*
2250  * This routine is called to scan the fragmentation reassembly table for
2251  * the specified ILL for any packets that are starting to smell.
2252  * dead_interval is the maximum time in seconds that will be tolerated.  It
2253  * will either be the value specified in ip_g_frag_timeout, or zero if the
2254  * ILL is shutting down and it is time to blow everything off.
2255  *
2256  * It returns the number of seconds (as a time_t) that the next frag timer
2257  * should be scheduled for, 0 meaning that the timer doesn't need to be
2258  * re-started.  Note that the method of calculating next_timeout isn't
2259  * entirely accurate since time will flow between the time we grab
2260  * current_time and the time we schedule the next timeout.  This isn't a
2261  * big problem since this is the timer for sending an ICMP reassembly time
2262  * exceeded messages, and it doesn't have to be exactly accurate.
2263  *
2264  * This function is
2265  * sometimes called as writer, although this is not required.
2266  */
2267 time_t
2268 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2269 {
2270 	ipfb_t	*ipfb;
2271 	ipfb_t	*endp;
2272 	ipf_t	*ipf;
2273 	ipf_t	*ipfnext;
2274 	mblk_t	*mp;
2275 	time_t	current_time = gethrestime_sec();
2276 	time_t	next_timeout = 0;
2277 	uint32_t	hdr_length;
2278 	mblk_t	*send_icmp_head;
2279 	mblk_t	*send_icmp_head_v6;
2280 	ip_stack_t *ipst = ill->ill_ipst;
2281 	ip_recv_attr_t iras;
2282 
2283 	bzero(&iras, sizeof (iras));
2284 	iras.ira_flags = 0;
2285 	iras.ira_ill = iras.ira_rill = ill;
2286 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2287 	iras.ira_rifindex = iras.ira_ruifindex;
2288 
2289 	ipfb = ill->ill_frag_hash_tbl;
2290 	if (ipfb == NULL)
2291 		return (B_FALSE);
2292 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2293 	/* Walk the frag hash table. */
2294 	for (; ipfb < endp; ipfb++) {
2295 		send_icmp_head = NULL;
2296 		send_icmp_head_v6 = NULL;
2297 		mutex_enter(&ipfb->ipfb_lock);
2298 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2299 			time_t frag_time = current_time - ipf->ipf_timestamp;
2300 			time_t frag_timeout;
2301 
2302 			if (frag_time < dead_interval) {
2303 				/*
2304 				 * There are some outstanding fragments
2305 				 * that will timeout later.  Make note of
2306 				 * the time so that we can reschedule the
2307 				 * next timeout appropriately.
2308 				 */
2309 				frag_timeout = dead_interval - frag_time;
2310 				if (next_timeout == 0 ||
2311 				    frag_timeout < next_timeout) {
2312 					next_timeout = frag_timeout;
2313 				}
2314 				break;
2315 			}
2316 			/* Time's up.  Get it out of here. */
2317 			hdr_length = ipf->ipf_nf_hdr_len;
2318 			ipfnext = ipf->ipf_hash_next;
2319 			if (ipfnext)
2320 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2321 			*ipf->ipf_ptphn = ipfnext;
2322 			mp = ipf->ipf_mp->b_cont;
2323 			for (; mp; mp = mp->b_cont) {
2324 				/* Extra points for neatness. */
2325 				IP_REASS_SET_START(mp, 0);
2326 				IP_REASS_SET_END(mp, 0);
2327 			}
2328 			mp = ipf->ipf_mp->b_cont;
2329 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2330 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2331 			ipfb->ipfb_count -= ipf->ipf_count;
2332 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2333 			ipfb->ipfb_frag_pkts--;
2334 			/*
2335 			 * We do not send any icmp message from here because
2336 			 * we currently are holding the ipfb_lock for this
2337 			 * hash chain. If we try and send any icmp messages
2338 			 * from here we may end up via a put back into ip
2339 			 * trying to get the same lock, causing a recursive
2340 			 * mutex panic. Instead we build a list and send all
2341 			 * the icmp messages after we have dropped the lock.
2342 			 */
2343 			if (ill->ill_isv6) {
2344 				if (hdr_length != 0) {
2345 					mp->b_next = send_icmp_head_v6;
2346 					send_icmp_head_v6 = mp;
2347 				} else {
2348 					freemsg(mp);
2349 				}
2350 			} else {
2351 				if (hdr_length != 0) {
2352 					mp->b_next = send_icmp_head;
2353 					send_icmp_head = mp;
2354 				} else {
2355 					freemsg(mp);
2356 				}
2357 			}
2358 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2359 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2360 			freeb(ipf->ipf_mp);
2361 		}
2362 		mutex_exit(&ipfb->ipfb_lock);
2363 		/*
2364 		 * Now need to send any icmp messages that we delayed from
2365 		 * above.
2366 		 */
2367 		while (send_icmp_head_v6 != NULL) {
2368 			ip6_t *ip6h;
2369 
2370 			mp = send_icmp_head_v6;
2371 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2372 			mp->b_next = NULL;
2373 			ip6h = (ip6_t *)mp->b_rptr;
2374 			iras.ira_flags = 0;
2375 			/*
2376 			 * This will result in an incorrect ALL_ZONES zoneid
2377 			 * for multicast packets, but we
2378 			 * don't send ICMP errors for those in any case.
2379 			 */
2380 			iras.ira_zoneid =
2381 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2382 			    ill, ipst);
2383 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2384 			icmp_time_exceeded_v6(mp,
2385 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2386 			    &iras);
2387 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2388 		}
2389 		while (send_icmp_head != NULL) {
2390 			ipaddr_t dst;
2391 
2392 			mp = send_icmp_head;
2393 			send_icmp_head = send_icmp_head->b_next;
2394 			mp->b_next = NULL;
2395 
2396 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2397 
2398 			iras.ira_flags = IRAF_IS_IPV4;
2399 			/*
2400 			 * This will result in an incorrect ALL_ZONES zoneid
2401 			 * for broadcast and multicast packets, but we
2402 			 * don't send ICMP errors for those in any case.
2403 			 */
2404 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2405 			    ill, ipst);
2406 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2407 			icmp_time_exceeded(mp,
2408 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2409 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2410 		}
2411 	}
2412 	/*
2413 	 * A non-dying ILL will use the return value to decide whether to
2414 	 * restart the frag timer, and for how long.
2415 	 */
2416 	return (next_timeout);
2417 }
2418 
2419 /*
2420  * This routine is called when the approximate count of mblk memory used
2421  * for the specified ILL has exceeded max_count.
2422  */
2423 void
2424 ill_frag_prune(ill_t *ill, uint_t max_count)
2425 {
2426 	ipfb_t	*ipfb;
2427 	ipf_t	*ipf;
2428 	size_t	count;
2429 
2430 	/*
2431 	 * If we are here within ip_min_frag_prune_time msecs remove
2432 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2433 	 * ill_frag_free_num_pkts.
2434 	 */
2435 	mutex_enter(&ill->ill_lock);
2436 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
2437 	    (ip_min_frag_prune_time != 0 ?
2438 	    ip_min_frag_prune_time : msec_per_tick)) {
2439 
2440 		ill->ill_frag_free_num_pkts++;
2441 
2442 	} else {
2443 		ill->ill_frag_free_num_pkts = 0;
2444 	}
2445 	ill->ill_last_frag_clean_time = lbolt;
2446 	mutex_exit(&ill->ill_lock);
2447 
2448 	/*
2449 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2450 	 */
2451 	if (ill->ill_frag_free_num_pkts != 0) {
2452 		int ix;
2453 
2454 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2455 			ipfb = &ill->ill_frag_hash_tbl[ix];
2456 			mutex_enter(&ipfb->ipfb_lock);
2457 			if (ipfb->ipfb_ipf != NULL) {
2458 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2459 				    ill->ill_frag_free_num_pkts);
2460 			}
2461 			mutex_exit(&ipfb->ipfb_lock);
2462 		}
2463 	}
2464 	/*
2465 	 * While the reassembly list for this ILL is too big, prune a fragment
2466 	 * queue by age, oldest first.
2467 	 */
2468 	while (ill->ill_frag_count > max_count) {
2469 		int	ix;
2470 		ipfb_t	*oipfb = NULL;
2471 		uint_t	oldest = UINT_MAX;
2472 
2473 		count = 0;
2474 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2475 			ipfb = &ill->ill_frag_hash_tbl[ix];
2476 			mutex_enter(&ipfb->ipfb_lock);
2477 			ipf = ipfb->ipfb_ipf;
2478 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2479 				oldest = ipf->ipf_gen;
2480 				oipfb = ipfb;
2481 			}
2482 			count += ipfb->ipfb_count;
2483 			mutex_exit(&ipfb->ipfb_lock);
2484 		}
2485 		if (oipfb == NULL)
2486 			break;
2487 
2488 		if (count <= max_count)
2489 			return;	/* Somebody beat us to it, nothing to do */
2490 		mutex_enter(&oipfb->ipfb_lock);
2491 		ipf = oipfb->ipfb_ipf;
2492 		if (ipf != NULL) {
2493 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2494 		}
2495 		mutex_exit(&oipfb->ipfb_lock);
2496 	}
2497 }
2498 
2499 /*
2500  * free 'free_cnt' fragmented packets starting at ipf.
2501  */
2502 void
2503 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2504 {
2505 	size_t	count;
2506 	mblk_t	*mp;
2507 	mblk_t	*tmp;
2508 	ipf_t **ipfp = ipf->ipf_ptphn;
2509 
2510 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2511 	ASSERT(ipfp != NULL);
2512 	ASSERT(ipf != NULL);
2513 
2514 	while (ipf != NULL && free_cnt-- > 0) {
2515 		count = ipf->ipf_count;
2516 		mp = ipf->ipf_mp;
2517 		ipf = ipf->ipf_hash_next;
2518 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2519 			IP_REASS_SET_START(tmp, 0);
2520 			IP_REASS_SET_END(tmp, 0);
2521 		}
2522 		atomic_add_32(&ill->ill_frag_count, -count);
2523 		ASSERT(ipfb->ipfb_count >= count);
2524 		ipfb->ipfb_count -= count;
2525 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2526 		ipfb->ipfb_frag_pkts--;
2527 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2528 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2529 		freemsg(mp);
2530 	}
2531 
2532 	if (ipf)
2533 		ipf->ipf_ptphn = ipfp;
2534 	ipfp[0] = ipf;
2535 }
2536 
2537 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2538 	"obsolete and may be removed in a future release of Solaris.  Use " \
2539 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2540 
2541 /*
2542  * For obsolete per-interface forwarding configuration;
2543  * called in response to ND_GET.
2544  */
2545 /* ARGSUSED */
2546 static int
2547 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2548 {
2549 	ill_t *ill = (ill_t *)cp;
2550 
2551 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2552 
2553 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2554 	return (0);
2555 }
2556 
2557 /*
2558  * For obsolete per-interface forwarding configuration;
2559  * called in response to ND_SET.
2560  */
2561 /* ARGSUSED */
2562 static int
2563 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2564     cred_t *ioc_cr)
2565 {
2566 	long value;
2567 	int retval;
2568 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2569 
2570 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2571 
2572 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2573 	    value < 0 || value > 1) {
2574 		return (EINVAL);
2575 	}
2576 
2577 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2578 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2579 	rw_exit(&ipst->ips_ill_g_lock);
2580 	return (retval);
2581 }
2582 
2583 /*
2584  * Helper function for ill_forward_set().
2585  */
2586 static void
2587 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2588 {
2589 	ip_stack_t	*ipst = ill->ill_ipst;
2590 
2591 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2592 
2593 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2594 	    (enable ? "Enabling" : "Disabling"),
2595 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2596 	mutex_enter(&ill->ill_lock);
2597 	if (enable)
2598 		ill->ill_flags |= ILLF_ROUTER;
2599 	else
2600 		ill->ill_flags &= ~ILLF_ROUTER;
2601 	mutex_exit(&ill->ill_lock);
2602 	if (ill->ill_isv6)
2603 		ill_set_nce_router_flags(ill, enable);
2604 	/* Notify routing socket listeners of this change. */
2605 	if (ill->ill_ipif != NULL)
2606 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2607 }
2608 
2609 /*
2610  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2611  * socket messages for each interface whose flags we change.
2612  */
2613 int
2614 ill_forward_set(ill_t *ill, boolean_t enable)
2615 {
2616 	ipmp_illgrp_t *illg;
2617 	ip_stack_t *ipst = ill->ill_ipst;
2618 
2619 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2620 
2621 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2622 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2623 		return (0);
2624 
2625 	if (IS_LOOPBACK(ill))
2626 		return (EINVAL);
2627 
2628 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2629 		/*
2630 		 * Update all of the interfaces in the group.
2631 		 */
2632 		illg = ill->ill_grp;
2633 		ill = list_head(&illg->ig_if);
2634 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2635 			ill_forward_set_on_ill(ill, enable);
2636 
2637 		/*
2638 		 * Update the IPMP meta-interface.
2639 		 */
2640 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2641 		return (0);
2642 	}
2643 
2644 	ill_forward_set_on_ill(ill, enable);
2645 	return (0);
2646 }
2647 
2648 /*
2649  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2650  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2651  * set or clear.
2652  */
2653 static void
2654 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2655 {
2656 	ipif_t *ipif;
2657 	ncec_t *ncec;
2658 	nce_t *nce;
2659 
2660 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2661 		/*
2662 		 * NOTE: we match across the illgrp because nce's for
2663 		 * addresses on IPMP interfaces have an nce_ill that points to
2664 		 * the bound underlying ill.
2665 		 */
2666 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2667 		if (nce != NULL) {
2668 			ncec = nce->nce_common;
2669 			mutex_enter(&ncec->ncec_lock);
2670 			if (enable)
2671 				ncec->ncec_flags |= NCE_F_ISROUTER;
2672 			else
2673 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2674 			mutex_exit(&ncec->ncec_lock);
2675 			nce_refrele(nce);
2676 		}
2677 	}
2678 }
2679 
2680 /*
2681  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2682  * for this ill.  Make sure the v6/v4 question has been answered about this
2683  * ill.  The creation of this ndd variable is only for backwards compatibility.
2684  * The preferred way to control per-interface IP forwarding is through the
2685  * ILLF_ROUTER interface flag.
2686  */
2687 static int
2688 ill_set_ndd_name(ill_t *ill)
2689 {
2690 	char *suffix;
2691 	ip_stack_t	*ipst = ill->ill_ipst;
2692 
2693 	ASSERT(IAM_WRITER_ILL(ill));
2694 
2695 	if (ill->ill_isv6)
2696 		suffix = ipv6_forward_suffix;
2697 	else
2698 		suffix = ipv4_forward_suffix;
2699 
2700 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2701 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2702 	/*
2703 	 * Copies over the '\0'.
2704 	 * Note that strlen(suffix) is always bounded.
2705 	 */
2706 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2707 	    strlen(suffix) + 1);
2708 
2709 	/*
2710 	 * Use of the nd table requires holding the reader lock.
2711 	 * Modifying the nd table thru nd_load/nd_unload requires
2712 	 * the writer lock.
2713 	 */
2714 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2715 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2716 	    nd_ill_forward_set, (caddr_t)ill)) {
2717 		/*
2718 		 * If the nd_load failed, it only meant that it could not
2719 		 * allocate a new bunch of room for further NDD expansion.
2720 		 * Because of that, the ill_ndd_name will be set to 0, and
2721 		 * this interface is at the mercy of the global ip_forwarding
2722 		 * variable.
2723 		 */
2724 		rw_exit(&ipst->ips_ip_g_nd_lock);
2725 		ill->ill_ndd_name = NULL;
2726 		return (ENOMEM);
2727 	}
2728 	rw_exit(&ipst->ips_ip_g_nd_lock);
2729 	return (0);
2730 }
2731 
2732 /*
2733  * Intializes the context structure and returns the first ill in the list
2734  * cuurently start_list and end_list can have values:
2735  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2736  * IP_V4_G_HEAD		Traverse IPV4 list only.
2737  * IP_V6_G_HEAD		Traverse IPV6 list only.
2738  */
2739 
2740 /*
2741  * We don't check for CONDEMNED ills here. Caller must do that if
2742  * necessary under the ill lock.
2743  */
2744 ill_t *
2745 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2746     ip_stack_t *ipst)
2747 {
2748 	ill_if_t *ifp;
2749 	ill_t *ill;
2750 	avl_tree_t *avl_tree;
2751 
2752 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2753 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2754 
2755 	/*
2756 	 * setup the lists to search
2757 	 */
2758 	if (end_list != MAX_G_HEADS) {
2759 		ctx->ctx_current_list = start_list;
2760 		ctx->ctx_last_list = end_list;
2761 	} else {
2762 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2763 		ctx->ctx_current_list = 0;
2764 	}
2765 
2766 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2767 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2768 		if (ifp != (ill_if_t *)
2769 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2770 			avl_tree = &ifp->illif_avl_by_ppa;
2771 			ill = avl_first(avl_tree);
2772 			/*
2773 			 * ill is guaranteed to be non NULL or ifp should have
2774 			 * not existed.
2775 			 */
2776 			ASSERT(ill != NULL);
2777 			return (ill);
2778 		}
2779 		ctx->ctx_current_list++;
2780 	}
2781 
2782 	return (NULL);
2783 }
2784 
2785 /*
2786  * returns the next ill in the list. ill_first() must have been called
2787  * before calling ill_next() or bad things will happen.
2788  */
2789 
2790 /*
2791  * We don't check for CONDEMNED ills here. Caller must do that if
2792  * necessary under the ill lock.
2793  */
2794 ill_t *
2795 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2796 {
2797 	ill_if_t *ifp;
2798 	ill_t *ill;
2799 	ip_stack_t	*ipst = lastill->ill_ipst;
2800 
2801 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2802 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2803 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2804 	    AVL_AFTER)) != NULL) {
2805 		return (ill);
2806 	}
2807 
2808 	/* goto next ill_ifp in the list. */
2809 	ifp = lastill->ill_ifptr->illif_next;
2810 
2811 	/* make sure not at end of circular list */
2812 	while (ifp ==
2813 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2814 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2815 			return (NULL);
2816 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2817 	}
2818 
2819 	return (avl_first(&ifp->illif_avl_by_ppa));
2820 }
2821 
2822 /*
2823  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2824  * The final number (PPA) must not have any leading zeros.  Upon success, a
2825  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2826  */
2827 static char *
2828 ill_get_ppa_ptr(char *name)
2829 {
2830 	int namelen = strlen(name);
2831 	int end_ndx = namelen - 1;
2832 	int ppa_ndx, i;
2833 
2834 	/*
2835 	 * Check that the first character is [a-zA-Z], and that the last
2836 	 * character is [0-9].
2837 	 */
2838 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2839 		return (NULL);
2840 
2841 	/*
2842 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2843 	 */
2844 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2845 		if (!isdigit(name[ppa_ndx - 1]))
2846 			break;
2847 
2848 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2849 		return (NULL);
2850 
2851 	/*
2852 	 * Check that the intermediate characters are [a-z0-9.]
2853 	 */
2854 	for (i = 1; i < ppa_ndx; i++) {
2855 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2856 		    name[i] != '.' && name[i] != '_') {
2857 			return (NULL);
2858 		}
2859 	}
2860 
2861 	return (name + ppa_ndx);
2862 }
2863 
2864 /*
2865  * use avl tree to locate the ill.
2866  */
2867 static ill_t *
2868 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2869 {
2870 	char *ppa_ptr = NULL;
2871 	int len;
2872 	uint_t ppa;
2873 	ill_t *ill = NULL;
2874 	ill_if_t *ifp;
2875 	int list;
2876 
2877 	/*
2878 	 * get ppa ptr
2879 	 */
2880 	if (isv6)
2881 		list = IP_V6_G_HEAD;
2882 	else
2883 		list = IP_V4_G_HEAD;
2884 
2885 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2886 		return (NULL);
2887 	}
2888 
2889 	len = ppa_ptr - name + 1;
2890 
2891 	ppa = stoi(&ppa_ptr);
2892 
2893 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2894 
2895 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2896 		/*
2897 		 * match is done on len - 1 as the name is not null
2898 		 * terminated it contains ppa in addition to the interface
2899 		 * name.
2900 		 */
2901 		if ((ifp->illif_name_len == len) &&
2902 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2903 			break;
2904 		} else {
2905 			ifp = ifp->illif_next;
2906 		}
2907 	}
2908 
2909 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2910 		/*
2911 		 * Even the interface type does not exist.
2912 		 */
2913 		return (NULL);
2914 	}
2915 
2916 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2917 	if (ill != NULL) {
2918 		mutex_enter(&ill->ill_lock);
2919 		if (ILL_CAN_LOOKUP(ill)) {
2920 			ill_refhold_locked(ill);
2921 			mutex_exit(&ill->ill_lock);
2922 			return (ill);
2923 		}
2924 		mutex_exit(&ill->ill_lock);
2925 	}
2926 	return (NULL);
2927 }
2928 
2929 /*
2930  * comparison function for use with avl.
2931  */
2932 static int
2933 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2934 {
2935 	uint_t ppa;
2936 	uint_t ill_ppa;
2937 
2938 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2939 
2940 	ppa = *((uint_t *)ppa_ptr);
2941 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2942 	/*
2943 	 * We want the ill with the lowest ppa to be on the
2944 	 * top.
2945 	 */
2946 	if (ill_ppa < ppa)
2947 		return (1);
2948 	if (ill_ppa > ppa)
2949 		return (-1);
2950 	return (0);
2951 }
2952 
2953 /*
2954  * remove an interface type from the global list.
2955  */
2956 static void
2957 ill_delete_interface_type(ill_if_t *interface)
2958 {
2959 	ASSERT(interface != NULL);
2960 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2961 
2962 	avl_destroy(&interface->illif_avl_by_ppa);
2963 	if (interface->illif_ppa_arena != NULL)
2964 		vmem_destroy(interface->illif_ppa_arena);
2965 
2966 	remque(interface);
2967 
2968 	mi_free(interface);
2969 }
2970 
2971 /*
2972  * remove ill from the global list.
2973  */
2974 static void
2975 ill_glist_delete(ill_t *ill)
2976 {
2977 	ip_stack_t	*ipst;
2978 	phyint_t	*phyi;
2979 
2980 	if (ill == NULL)
2981 		return;
2982 	ipst = ill->ill_ipst;
2983 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2984 
2985 	/*
2986 	 * If the ill was never inserted into the AVL tree
2987 	 * we skip the if branch.
2988 	 */
2989 	if (ill->ill_ifptr != NULL) {
2990 		/*
2991 		 * remove from AVL tree and free ppa number
2992 		 */
2993 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
2994 
2995 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
2996 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
2997 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
2998 		}
2999 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3000 			ill_delete_interface_type(ill->ill_ifptr);
3001 		}
3002 
3003 		/*
3004 		 * Indicate ill is no longer in the list.
3005 		 */
3006 		ill->ill_ifptr = NULL;
3007 		ill->ill_name_length = 0;
3008 		ill->ill_name[0] = '\0';
3009 		ill->ill_ppa = UINT_MAX;
3010 	}
3011 
3012 	/* Generate one last event for this ill. */
3013 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3014 	    ill->ill_name_length);
3015 
3016 	ASSERT(ill->ill_phyint != NULL);
3017 	phyi = ill->ill_phyint;
3018 	ill->ill_phyint = NULL;
3019 
3020 	/*
3021 	 * ill_init allocates a phyint always to store the copy
3022 	 * of flags relevant to phyint. At that point in time, we could
3023 	 * not assign the name and hence phyint_illv4/v6 could not be
3024 	 * initialized. Later in ipif_set_values, we assign the name to
3025 	 * the ill, at which point in time we assign phyint_illv4/v6.
3026 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3027 	 */
3028 	if (ill->ill_flags & ILLF_IPV6)
3029 		phyi->phyint_illv6 = NULL;
3030 	else
3031 		phyi->phyint_illv4 = NULL;
3032 
3033 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3034 		rw_exit(&ipst->ips_ill_g_lock);
3035 		return;
3036 	}
3037 
3038 	/*
3039 	 * There are no ills left on this phyint; pull it out of the phyint
3040 	 * avl trees, and free it.
3041 	 */
3042 	if (phyi->phyint_ifindex > 0) {
3043 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3044 		    phyi);
3045 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3046 		    phyi);
3047 	}
3048 	rw_exit(&ipst->ips_ill_g_lock);
3049 
3050 	phyint_free(phyi);
3051 }
3052 
3053 /*
3054  * allocate a ppa, if the number of plumbed interfaces of this type are
3055  * less than ill_no_arena do a linear search to find a unused ppa.
3056  * When the number goes beyond ill_no_arena switch to using an arena.
3057  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3058  * is the return value for an error condition, so allocation starts at one
3059  * and is decremented by one.
3060  */
3061 static int
3062 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3063 {
3064 	ill_t *tmp_ill;
3065 	uint_t start, end;
3066 	int ppa;
3067 
3068 	if (ifp->illif_ppa_arena == NULL &&
3069 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3070 		/*
3071 		 * Create an arena.
3072 		 */
3073 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3074 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3075 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3076 			/* allocate what has already been assigned */
3077 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3078 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3079 		    tmp_ill, AVL_AFTER)) {
3080 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3081 			    1,		/* size */
3082 			    1,		/* align/quantum */
3083 			    0,		/* phase */
3084 			    0,		/* nocross */
3085 			    /* minaddr */
3086 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3087 			    /* maxaddr */
3088 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3089 			    VM_NOSLEEP|VM_FIRSTFIT);
3090 			if (ppa == 0) {
3091 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3092 				    " failed while switching"));
3093 				vmem_destroy(ifp->illif_ppa_arena);
3094 				ifp->illif_ppa_arena = NULL;
3095 				break;
3096 			}
3097 		}
3098 	}
3099 
3100 	if (ifp->illif_ppa_arena != NULL) {
3101 		if (ill->ill_ppa == UINT_MAX) {
3102 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3103 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3104 			if (ppa == 0)
3105 				return (EAGAIN);
3106 			ill->ill_ppa = --ppa;
3107 		} else {
3108 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3109 			    1, 		/* size */
3110 			    1, 		/* align/quantum */
3111 			    0, 		/* phase */
3112 			    0, 		/* nocross */
3113 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3114 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3115 			    VM_NOSLEEP|VM_FIRSTFIT);
3116 			/*
3117 			 * Most likely the allocation failed because
3118 			 * the requested ppa was in use.
3119 			 */
3120 			if (ppa == 0)
3121 				return (EEXIST);
3122 		}
3123 		return (0);
3124 	}
3125 
3126 	/*
3127 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3128 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3129 	 */
3130 	if (ill->ill_ppa == UINT_MAX) {
3131 		end = UINT_MAX - 1;
3132 		start = 0;
3133 	} else {
3134 		end = start = ill->ill_ppa;
3135 	}
3136 
3137 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3138 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3139 		if (start++ >= end) {
3140 			if (ill->ill_ppa == UINT_MAX)
3141 				return (EAGAIN);
3142 			else
3143 				return (EEXIST);
3144 		}
3145 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3146 	}
3147 	ill->ill_ppa = start;
3148 	return (0);
3149 }
3150 
3151 /*
3152  * Insert ill into the list of configured ill's. Once this function completes,
3153  * the ill is globally visible and is available through lookups. More precisely
3154  * this happens after the caller drops the ill_g_lock.
3155  */
3156 static int
3157 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3158 {
3159 	ill_if_t *ill_interface;
3160 	avl_index_t where = 0;
3161 	int error;
3162 	int name_length;
3163 	int index;
3164 	boolean_t check_length = B_FALSE;
3165 	ip_stack_t	*ipst = ill->ill_ipst;
3166 
3167 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3168 
3169 	name_length = mi_strlen(name) + 1;
3170 
3171 	if (isv6)
3172 		index = IP_V6_G_HEAD;
3173 	else
3174 		index = IP_V4_G_HEAD;
3175 
3176 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3177 	/*
3178 	 * Search for interface type based on name
3179 	 */
3180 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3181 		if ((ill_interface->illif_name_len == name_length) &&
3182 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3183 			break;
3184 		}
3185 		ill_interface = ill_interface->illif_next;
3186 	}
3187 
3188 	/*
3189 	 * Interface type not found, create one.
3190 	 */
3191 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3192 		ill_g_head_t ghead;
3193 
3194 		/*
3195 		 * allocate ill_if_t structure
3196 		 */
3197 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3198 		if (ill_interface == NULL) {
3199 			return (ENOMEM);
3200 		}
3201 
3202 		(void) strcpy(ill_interface->illif_name, name);
3203 		ill_interface->illif_name_len = name_length;
3204 
3205 		avl_create(&ill_interface->illif_avl_by_ppa,
3206 		    ill_compare_ppa, sizeof (ill_t),
3207 		    offsetof(struct ill_s, ill_avl_byppa));
3208 
3209 		/*
3210 		 * link the structure in the back to maintain order
3211 		 * of configuration for ifconfig output.
3212 		 */
3213 		ghead = ipst->ips_ill_g_heads[index];
3214 		insque(ill_interface, ghead.ill_g_list_tail);
3215 	}
3216 
3217 	if (ill->ill_ppa == UINT_MAX)
3218 		check_length = B_TRUE;
3219 
3220 	error = ill_alloc_ppa(ill_interface, ill);
3221 	if (error != 0) {
3222 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3223 			ill_delete_interface_type(ill->ill_ifptr);
3224 		return (error);
3225 	}
3226 
3227 	/*
3228 	 * When the ppa is choosen by the system, check that there is
3229 	 * enough space to insert ppa. if a specific ppa was passed in this
3230 	 * check is not required as the interface name passed in will have
3231 	 * the right ppa in it.
3232 	 */
3233 	if (check_length) {
3234 		/*
3235 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3236 		 */
3237 		char buf[sizeof (uint_t) * 3];
3238 
3239 		/*
3240 		 * convert ppa to string to calculate the amount of space
3241 		 * required for it in the name.
3242 		 */
3243 		numtos(ill->ill_ppa, buf);
3244 
3245 		/* Do we have enough space to insert ppa ? */
3246 
3247 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3248 			/* Free ppa and interface type struct */
3249 			if (ill_interface->illif_ppa_arena != NULL) {
3250 				vmem_free(ill_interface->illif_ppa_arena,
3251 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3252 			}
3253 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3254 				ill_delete_interface_type(ill->ill_ifptr);
3255 
3256 			return (EINVAL);
3257 		}
3258 	}
3259 
3260 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3261 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3262 
3263 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3264 	    &where);
3265 	ill->ill_ifptr = ill_interface;
3266 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3267 
3268 	ill_phyint_reinit(ill);
3269 	return (0);
3270 }
3271 
3272 /* Initialize the per phyint ipsq used for serialization */
3273 static boolean_t
3274 ipsq_init(ill_t *ill, boolean_t enter)
3275 {
3276 	ipsq_t  *ipsq;
3277 	ipxop_t	*ipx;
3278 
3279 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3280 		return (B_FALSE);
3281 
3282 	ill->ill_phyint->phyint_ipsq = ipsq;
3283 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3284 	ipx->ipx_ipsq = ipsq;
3285 	ipsq->ipsq_next = ipsq;
3286 	ipsq->ipsq_phyint = ill->ill_phyint;
3287 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3288 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3289 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3290 	if (enter) {
3291 		ipx->ipx_writer = curthread;
3292 		ipx->ipx_forced = B_FALSE;
3293 		ipx->ipx_reentry_cnt = 1;
3294 #ifdef DEBUG
3295 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3296 #endif
3297 	}
3298 	return (B_TRUE);
3299 }
3300 
3301 /*
3302  * ill_init is called by ip_open when a device control stream is opened.
3303  * It does a few initializations, and shoots a DL_INFO_REQ message down
3304  * to the driver.  The response is later picked up in ip_rput_dlpi and
3305  * used to set up default mechanisms for talking to the driver.  (Always
3306  * called as writer.)
3307  *
3308  * If this function returns error, ip_open will call ip_close which in
3309  * turn will call ill_delete to clean up any memory allocated here that
3310  * is not yet freed.
3311  */
3312 int
3313 ill_init(queue_t *q, ill_t *ill)
3314 {
3315 	int	count;
3316 	dl_info_req_t	*dlir;
3317 	mblk_t	*info_mp;
3318 	uchar_t *frag_ptr;
3319 
3320 	/*
3321 	 * The ill is initialized to zero by mi_alloc*(). In addition
3322 	 * some fields already contain valid values, initialized in
3323 	 * ip_open(), before we reach here.
3324 	 */
3325 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3326 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3327 	ill->ill_saved_ire_cnt = 0;
3328 
3329 	ill->ill_rq = q;
3330 	ill->ill_wq = WR(q);
3331 
3332 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3333 	    BPRI_HI);
3334 	if (info_mp == NULL)
3335 		return (ENOMEM);
3336 
3337 	/*
3338 	 * Allocate sufficient space to contain our fragment hash table and
3339 	 * the device name.
3340 	 */
3341 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3342 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
3343 	if (frag_ptr == NULL) {
3344 		freemsg(info_mp);
3345 		return (ENOMEM);
3346 	}
3347 	ill->ill_frag_ptr = frag_ptr;
3348 	ill->ill_frag_free_num_pkts = 0;
3349 	ill->ill_last_frag_clean_time = 0;
3350 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3351 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3352 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3353 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3354 		    NULL, MUTEX_DEFAULT, NULL);
3355 	}
3356 
3357 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3358 	if (ill->ill_phyint == NULL) {
3359 		freemsg(info_mp);
3360 		mi_free(frag_ptr);
3361 		return (ENOMEM);
3362 	}
3363 
3364 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3365 	/*
3366 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3367 	 * at this point because of the following reason. If we can't
3368 	 * enter the ipsq at some point and cv_wait, the writer that
3369 	 * wakes us up tries to locate us using the list of all phyints
3370 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3371 	 * If we don't set it now, we risk a missed wakeup.
3372 	 */
3373 	ill->ill_phyint->phyint_illv4 = ill;
3374 	ill->ill_ppa = UINT_MAX;
3375 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3376 
3377 	ill_set_inputfn(ill);
3378 
3379 	if (!ipsq_init(ill, B_TRUE)) {
3380 		freemsg(info_mp);
3381 		mi_free(frag_ptr);
3382 		mi_free(ill->ill_phyint);
3383 		return (ENOMEM);
3384 	}
3385 
3386 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3387 
3388 	/* Frag queue limit stuff */
3389 	ill->ill_frag_count = 0;
3390 	ill->ill_ipf_gen = 0;
3391 
3392 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3393 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3394 	ill->ill_global_timer = INFINITY;
3395 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3396 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3397 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3398 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3399 
3400 	/*
3401 	 * Initialize IPv6 configuration variables.  The IP module is always
3402 	 * opened as an IPv4 module.  Instead tracking down the cases where
3403 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3404 	 * here for convenience, this has no effect until the ill is set to do
3405 	 * IPv6.
3406 	 */
3407 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3408 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3409 	ill->ill_max_buf = ND_MAX_Q;
3410 	ill->ill_refcnt = 0;
3411 
3412 	/* Send down the Info Request to the driver. */
3413 	info_mp->b_datap->db_type = M_PCPROTO;
3414 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3415 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3416 	dlir->dl_primitive = DL_INFO_REQ;
3417 
3418 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3419 
3420 	qprocson(q);
3421 	ill_dlpi_send(ill, info_mp);
3422 
3423 	return (0);
3424 }
3425 
3426 /*
3427  * ill_dls_info
3428  * creates datalink socket info from the device.
3429  */
3430 int
3431 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3432 {
3433 	size_t	len;
3434 
3435 	sdl->sdl_family = AF_LINK;
3436 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3437 	sdl->sdl_type = ill->ill_type;
3438 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3439 	len = strlen(sdl->sdl_data);
3440 	ASSERT(len < 256);
3441 	sdl->sdl_nlen = (uchar_t)len;
3442 	sdl->sdl_alen = ill->ill_phys_addr_length;
3443 	sdl->sdl_slen = 0;
3444 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3445 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3446 
3447 	return (sizeof (struct sockaddr_dl));
3448 }
3449 
3450 /*
3451  * ill_xarp_info
3452  * creates xarp info from the device.
3453  */
3454 static int
3455 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3456 {
3457 	sdl->sdl_family = AF_LINK;
3458 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3459 	sdl->sdl_type = ill->ill_type;
3460 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3461 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3462 	sdl->sdl_alen = ill->ill_phys_addr_length;
3463 	sdl->sdl_slen = 0;
3464 	return (sdl->sdl_nlen);
3465 }
3466 
3467 static int
3468 loopback_kstat_update(kstat_t *ksp, int rw)
3469 {
3470 	kstat_named_t *kn;
3471 	netstackid_t	stackid;
3472 	netstack_t	*ns;
3473 	ip_stack_t	*ipst;
3474 
3475 	if (ksp == NULL || ksp->ks_data == NULL)
3476 		return (EIO);
3477 
3478 	if (rw == KSTAT_WRITE)
3479 		return (EACCES);
3480 
3481 	kn = KSTAT_NAMED_PTR(ksp);
3482 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3483 
3484 	ns = netstack_find_by_stackid(stackid);
3485 	if (ns == NULL)
3486 		return (-1);
3487 
3488 	ipst = ns->netstack_ip;
3489 	if (ipst == NULL) {
3490 		netstack_rele(ns);
3491 		return (-1);
3492 	}
3493 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3494 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3495 	netstack_rele(ns);
3496 	return (0);
3497 }
3498 
3499 /*
3500  * Has ifindex been plumbed already?
3501  */
3502 static boolean_t
3503 phyint_exists(uint_t index, ip_stack_t *ipst)
3504 {
3505 	ASSERT(index != 0);
3506 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3507 
3508 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3509 	    &index, NULL) != NULL);
3510 }
3511 
3512 /* Pick a unique ifindex */
3513 boolean_t
3514 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3515 {
3516 	uint_t starting_index;
3517 
3518 	if (!ipst->ips_ill_index_wrap) {
3519 		*indexp = ipst->ips_ill_index++;
3520 		if (ipst->ips_ill_index == 0) {
3521 			/* Reached the uint_t limit Next time wrap  */
3522 			ipst->ips_ill_index_wrap = B_TRUE;
3523 		}
3524 		return (B_TRUE);
3525 	}
3526 
3527 	/*
3528 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3529 	 * at this point and don't want to call any function that attempts
3530 	 * to get the lock again.
3531 	 */
3532 	starting_index = ipst->ips_ill_index++;
3533 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3534 		if (ipst->ips_ill_index != 0 &&
3535 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3536 			/* found unused index - use it */
3537 			*indexp = ipst->ips_ill_index;
3538 			return (B_TRUE);
3539 		}
3540 	}
3541 
3542 	/*
3543 	 * all interface indicies are inuse.
3544 	 */
3545 	return (B_FALSE);
3546 }
3547 
3548 /*
3549  * Assign a unique interface index for the phyint.
3550  */
3551 static boolean_t
3552 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3553 {
3554 	ASSERT(phyi->phyint_ifindex == 0);
3555 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3556 }
3557 
3558 /*
3559  * Initialize the flags on `phyi' as per the provided mactype.
3560  */
3561 static void
3562 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3563 {
3564 	uint64_t flags = 0;
3565 
3566 	/*
3567 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3568 	 * we always presume the underlying hardware is working and set
3569 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3570 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3571 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3572 	 */
3573 	if (mactype == SUNW_DL_IPMP)
3574 		flags |= PHYI_FAILED;
3575 	else
3576 		flags |= PHYI_RUNNING;
3577 
3578 	switch (mactype) {
3579 	case SUNW_DL_VNI:
3580 		flags |= PHYI_VIRTUAL;
3581 		break;
3582 	case SUNW_DL_IPMP:
3583 		flags |= PHYI_IPMP;
3584 		break;
3585 	case DL_LOOP:
3586 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3587 		break;
3588 	}
3589 
3590 	mutex_enter(&phyi->phyint_lock);
3591 	phyi->phyint_flags |= flags;
3592 	mutex_exit(&phyi->phyint_lock);
3593 }
3594 
3595 /*
3596  * Return a pointer to the ill which matches the supplied name.  Note that
3597  * the ill name length includes the null termination character.  (May be
3598  * called as writer.)
3599  * If do_alloc and the interface is "lo0" it will be automatically created.
3600  * Cannot bump up reference on condemned ills. So dup detect can't be done
3601  * using this func.
3602  */
3603 ill_t *
3604 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3605     boolean_t *did_alloc, ip_stack_t *ipst)
3606 {
3607 	ill_t	*ill;
3608 	ipif_t	*ipif;
3609 	ipsq_t	*ipsq;
3610 	kstat_named_t	*kn;
3611 	boolean_t isloopback;
3612 	in6_addr_t ov6addr;
3613 
3614 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3615 
3616 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3617 	ill = ill_find_by_name(name, isv6, ipst);
3618 	rw_exit(&ipst->ips_ill_g_lock);
3619 	if (ill != NULL)
3620 		return (ill);
3621 
3622 	/*
3623 	 * Couldn't find it.  Does this happen to be a lookup for the
3624 	 * loopback device and are we allowed to allocate it?
3625 	 */
3626 	if (!isloopback || !do_alloc)
3627 		return (NULL);
3628 
3629 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3630 	ill = ill_find_by_name(name, isv6, ipst);
3631 	if (ill != NULL) {
3632 		rw_exit(&ipst->ips_ill_g_lock);
3633 		return (ill);
3634 	}
3635 
3636 	/* Create the loopback device on demand */
3637 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3638 	    sizeof (ipif_loopback_name), BPRI_MED));
3639 	if (ill == NULL)
3640 		goto done;
3641 
3642 	*ill = ill_null;
3643 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3644 	ill->ill_ipst = ipst;
3645 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3646 	netstack_hold(ipst->ips_netstack);
3647 	/*
3648 	 * For exclusive stacks we set the zoneid to zero
3649 	 * to make IP operate as if in the global zone.
3650 	 */
3651 	ill->ill_zoneid = GLOBAL_ZONEID;
3652 
3653 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3654 	if (ill->ill_phyint == NULL)
3655 		goto done;
3656 
3657 	if (isv6)
3658 		ill->ill_phyint->phyint_illv6 = ill;
3659 	else
3660 		ill->ill_phyint->phyint_illv4 = ill;
3661 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3662 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3663 
3664 	if (isv6) {
3665 		ill->ill_isv6 = B_TRUE;
3666 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3667 	} else {
3668 		ill->ill_max_frag = ip_loopback_mtuplus;
3669 	}
3670 	if (!ill_allocate_mibs(ill))
3671 		goto done;
3672 	ill->ill_current_frag = ill->ill_max_frag;
3673 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3674 	/*
3675 	 * ipif_loopback_name can't be pointed at directly because its used
3676 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3677 	 * from the glist, ill_glist_delete() sets the first character of
3678 	 * ill_name to '\0'.
3679 	 */
3680 	ill->ill_name = (char *)ill + sizeof (*ill);
3681 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3682 	ill->ill_name_length = sizeof (ipif_loopback_name);
3683 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3684 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3685 
3686 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3687 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3688 	ill->ill_global_timer = INFINITY;
3689 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3690 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3691 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3692 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3693 
3694 	/* No resolver here. */
3695 	ill->ill_net_type = IRE_LOOPBACK;
3696 
3697 	/* Initialize the ipsq */
3698 	if (!ipsq_init(ill, B_FALSE))
3699 		goto done;
3700 
3701 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
3702 	if (ipif == NULL)
3703 		goto done;
3704 
3705 	ill->ill_flags = ILLF_MULTICAST;
3706 
3707 	ov6addr = ipif->ipif_v6lcl_addr;
3708 	/* Set up default loopback address and mask. */
3709 	if (!isv6) {
3710 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3711 
3712 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3713 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3714 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3715 		    ipif->ipif_v6subnet);
3716 		ill->ill_flags |= ILLF_IPV4;
3717 	} else {
3718 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3719 		ipif->ipif_v6net_mask = ipv6_all_ones;
3720 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3721 		    ipif->ipif_v6subnet);
3722 		ill->ill_flags |= ILLF_IPV6;
3723 	}
3724 
3725 	/*
3726 	 * Chain us in at the end of the ill list. hold the ill
3727 	 * before we make it globally visible. 1 for the lookup.
3728 	 */
3729 	ill->ill_refcnt = 0;
3730 	ill_refhold(ill);
3731 
3732 	ill->ill_frag_count = 0;
3733 	ill->ill_frag_free_num_pkts = 0;
3734 	ill->ill_last_frag_clean_time = 0;
3735 
3736 	ipsq = ill->ill_phyint->phyint_ipsq;
3737 
3738 	ill_set_inputfn(ill);
3739 
3740 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3741 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3742 
3743 	/* Let SCTP know so that it can add this to its list */
3744 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3745 
3746 	/*
3747 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3748 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3749 	 * requires to be after ill_glist_insert() since we need the
3750 	 * ill_index set. Pass on ipv6_loopback as the old address.
3751 	 */
3752 	sctp_update_ipif_addr(ipif, ov6addr);
3753 
3754 	/*
3755 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3756 	 * If so, free our original one.
3757 	 */
3758 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3759 		ipsq_delete(ipsq);
3760 
3761 	if (ipst->ips_loopback_ksp == NULL) {
3762 		/* Export loopback interface statistics */
3763 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3764 		    ipif_loopback_name, "net",
3765 		    KSTAT_TYPE_NAMED, 2, 0,
3766 		    ipst->ips_netstack->netstack_stackid);
3767 		if (ipst->ips_loopback_ksp != NULL) {
3768 			ipst->ips_loopback_ksp->ks_update =
3769 			    loopback_kstat_update;
3770 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3771 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3772 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3773 			ipst->ips_loopback_ksp->ks_private =
3774 			    (void *)(uintptr_t)ipst->ips_netstack->
3775 			    netstack_stackid;
3776 			kstat_install(ipst->ips_loopback_ksp);
3777 		}
3778 	}
3779 
3780 	*did_alloc = B_TRUE;
3781 	rw_exit(&ipst->ips_ill_g_lock);
3782 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3783 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3784 	return (ill);
3785 done:
3786 	if (ill != NULL) {
3787 		if (ill->ill_phyint != NULL) {
3788 			ipsq = ill->ill_phyint->phyint_ipsq;
3789 			if (ipsq != NULL) {
3790 				ipsq->ipsq_phyint = NULL;
3791 				ipsq_delete(ipsq);
3792 			}
3793 			mi_free(ill->ill_phyint);
3794 		}
3795 		ill_free_mib(ill);
3796 		if (ill->ill_ipst != NULL)
3797 			netstack_rele(ill->ill_ipst->ips_netstack);
3798 		mi_free(ill);
3799 	}
3800 	rw_exit(&ipst->ips_ill_g_lock);
3801 	return (NULL);
3802 }
3803 
3804 /*
3805  * For IPP calls - use the ip_stack_t for global stack.
3806  */
3807 ill_t *
3808 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3809 {
3810 	ip_stack_t	*ipst;
3811 	ill_t		*ill;
3812 
3813 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3814 	if (ipst == NULL) {
3815 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3816 		return (NULL);
3817 	}
3818 
3819 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3820 	netstack_rele(ipst->ips_netstack);
3821 	return (ill);
3822 }
3823 
3824 /*
3825  * Return a pointer to the ill which matches the index and IP version type.
3826  */
3827 ill_t *
3828 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3829 {
3830 	ill_t	*ill;
3831 	phyint_t *phyi;
3832 
3833 	/*
3834 	 * Indexes are stored in the phyint - a common structure
3835 	 * to both IPv4 and IPv6.
3836 	 */
3837 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3838 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3839 	    (void *) &index, NULL);
3840 	if (phyi != NULL) {
3841 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3842 		if (ill != NULL) {
3843 			mutex_enter(&ill->ill_lock);
3844 			if (!ILL_IS_CONDEMNED(ill)) {
3845 				ill_refhold_locked(ill);
3846 				mutex_exit(&ill->ill_lock);
3847 				rw_exit(&ipst->ips_ill_g_lock);
3848 				return (ill);
3849 			}
3850 			mutex_exit(&ill->ill_lock);
3851 		}
3852 	}
3853 	rw_exit(&ipst->ips_ill_g_lock);
3854 	return (NULL);
3855 }
3856 
3857 /*
3858  * Verify whether or not an interface index is valid.
3859  * It can be zero (meaning "reset") or an interface index assigned
3860  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3861  */
3862 boolean_t
3863 ip_ifindex_valid(uint_t ifindex, boolean_t isv6, ip_stack_t *ipst)
3864 {
3865 	ill_t		*ill;
3866 
3867 	if (ifindex == 0)
3868 		return (B_TRUE);
3869 
3870 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
3871 	if (ill == NULL)
3872 		return (B_FALSE);
3873 	if (IS_VNI(ill)) {
3874 		ill_refrele(ill);
3875 		return (B_FALSE);
3876 	}
3877 	ill_refrele(ill);
3878 	return (B_TRUE);
3879 }
3880 
3881 /*
3882  * Return the ifindex next in sequence after the passed in ifindex.
3883  * If there is no next ifindex for the given protocol, return 0.
3884  */
3885 uint_t
3886 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3887 {
3888 	phyint_t *phyi;
3889 	phyint_t *phyi_initial;
3890 	uint_t   ifindex;
3891 
3892 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3893 
3894 	if (index == 0) {
3895 		phyi = avl_first(
3896 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3897 	} else {
3898 		phyi = phyi_initial = avl_find(
3899 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3900 		    (void *) &index, NULL);
3901 	}
3902 
3903 	for (; phyi != NULL;
3904 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3905 	    phyi, AVL_AFTER)) {
3906 		/*
3907 		 * If we're not returning the first interface in the tree
3908 		 * and we still haven't moved past the phyint_t that
3909 		 * corresponds to index, avl_walk needs to be called again
3910 		 */
3911 		if (!((index != 0) && (phyi == phyi_initial))) {
3912 			if (isv6) {
3913 				if ((phyi->phyint_illv6) &&
3914 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3915 				    (phyi->phyint_illv6->ill_isv6 == 1))
3916 					break;
3917 			} else {
3918 				if ((phyi->phyint_illv4) &&
3919 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3920 				    (phyi->phyint_illv4->ill_isv6 == 0))
3921 					break;
3922 			}
3923 		}
3924 	}
3925 
3926 	rw_exit(&ipst->ips_ill_g_lock);
3927 
3928 	if (phyi != NULL)
3929 		ifindex = phyi->phyint_ifindex;
3930 	else
3931 		ifindex = 0;
3932 
3933 	return (ifindex);
3934 }
3935 
3936 /*
3937  * Return the ifindex for the named interface.
3938  * If there is no next ifindex for the interface, return 0.
3939  */
3940 uint_t
3941 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3942 {
3943 	phyint_t	*phyi;
3944 	avl_index_t	where = 0;
3945 	uint_t		ifindex;
3946 
3947 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3948 
3949 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3950 	    name, &where)) == NULL) {
3951 		rw_exit(&ipst->ips_ill_g_lock);
3952 		return (0);
3953 	}
3954 
3955 	ifindex = phyi->phyint_ifindex;
3956 
3957 	rw_exit(&ipst->ips_ill_g_lock);
3958 
3959 	return (ifindex);
3960 }
3961 
3962 /*
3963  * Return the ifindex to be used by upper layer protocols for instance
3964  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3965  */
3966 uint_t
3967 ill_get_upper_ifindex(const ill_t *ill)
3968 {
3969 	if (IS_UNDER_IPMP(ill))
3970 		return (ipmp_ill_get_ipmp_ifindex(ill));
3971 	else
3972 		return (ill->ill_phyint->phyint_ifindex);
3973 }
3974 
3975 
3976 /*
3977  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3978  * that gives a running thread a reference to the ill. This reference must be
3979  * released by the thread when it is done accessing the ill and related
3980  * objects. ill_refcnt can not be used to account for static references
3981  * such as other structures pointing to an ill. Callers must generally
3982  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3983  * or be sure that the ill is not being deleted or changing state before
3984  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3985  * ill won't change any of its critical state such as address, netmask etc.
3986  */
3987 void
3988 ill_refhold(ill_t *ill)
3989 {
3990 	mutex_enter(&ill->ill_lock);
3991 	ill->ill_refcnt++;
3992 	ILL_TRACE_REF(ill);
3993 	mutex_exit(&ill->ill_lock);
3994 }
3995 
3996 void
3997 ill_refhold_locked(ill_t *ill)
3998 {
3999 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4000 	ill->ill_refcnt++;
4001 	ILL_TRACE_REF(ill);
4002 }
4003 
4004 /* Returns true if we managed to get a refhold */
4005 boolean_t
4006 ill_check_and_refhold(ill_t *ill)
4007 {
4008 	mutex_enter(&ill->ill_lock);
4009 	if (!ILL_IS_CONDEMNED(ill)) {
4010 		ill_refhold_locked(ill);
4011 		mutex_exit(&ill->ill_lock);
4012 		return (B_TRUE);
4013 	}
4014 	mutex_exit(&ill->ill_lock);
4015 	return (B_FALSE);
4016 }
4017 
4018 /*
4019  * Must not be called while holding any locks. Otherwise if this is
4020  * the last reference to be released, there is a chance of recursive mutex
4021  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4022  * to restart an ioctl.
4023  */
4024 void
4025 ill_refrele(ill_t *ill)
4026 {
4027 	mutex_enter(&ill->ill_lock);
4028 	ASSERT(ill->ill_refcnt != 0);
4029 	ill->ill_refcnt--;
4030 	ILL_UNTRACE_REF(ill);
4031 	if (ill->ill_refcnt != 0) {
4032 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4033 		mutex_exit(&ill->ill_lock);
4034 		return;
4035 	}
4036 
4037 	/* Drops the ill_lock */
4038 	ipif_ill_refrele_tail(ill);
4039 }
4040 
4041 /*
4042  * Obtain a weak reference count on the ill. This reference ensures the
4043  * ill won't be freed, but the ill may change any of its critical state
4044  * such as netmask, address etc. Returns an error if the ill has started
4045  * closing.
4046  */
4047 boolean_t
4048 ill_waiter_inc(ill_t *ill)
4049 {
4050 	mutex_enter(&ill->ill_lock);
4051 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4052 		mutex_exit(&ill->ill_lock);
4053 		return (B_FALSE);
4054 	}
4055 	ill->ill_waiters++;
4056 	mutex_exit(&ill->ill_lock);
4057 	return (B_TRUE);
4058 }
4059 
4060 void
4061 ill_waiter_dcr(ill_t *ill)
4062 {
4063 	mutex_enter(&ill->ill_lock);
4064 	ill->ill_waiters--;
4065 	if (ill->ill_waiters == 0)
4066 		cv_broadcast(&ill->ill_cv);
4067 	mutex_exit(&ill->ill_lock);
4068 }
4069 
4070 /*
4071  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4072  * driver.  We construct best guess defaults for lower level information that
4073  * we need.  If an interface is brought up without injection of any overriding
4074  * information from outside, we have to be ready to go with these defaults.
4075  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4076  * we primarely want the dl_provider_style.
4077  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4078  * at which point we assume the other part of the information is valid.
4079  */
4080 void
4081 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4082 {
4083 	uchar_t		*brdcst_addr;
4084 	uint_t		brdcst_addr_length, phys_addr_length;
4085 	t_scalar_t	sap_length;
4086 	dl_info_ack_t	*dlia;
4087 	ip_m_t		*ipm;
4088 	dl_qos_cl_sel1_t *sel1;
4089 	int		min_mtu;
4090 
4091 	ASSERT(IAM_WRITER_ILL(ill));
4092 
4093 	/*
4094 	 * Till the ill is fully up  the ill is not globally visible.
4095 	 * So no need for a lock.
4096 	 */
4097 	dlia = (dl_info_ack_t *)mp->b_rptr;
4098 	ill->ill_mactype = dlia->dl_mac_type;
4099 
4100 	ipm = ip_m_lookup(dlia->dl_mac_type);
4101 	if (ipm == NULL) {
4102 		ipm = ip_m_lookup(DL_OTHER);
4103 		ASSERT(ipm != NULL);
4104 	}
4105 	ill->ill_media = ipm;
4106 
4107 	/*
4108 	 * When the new DLPI stuff is ready we'll pull lengths
4109 	 * from dlia.
4110 	 */
4111 	if (dlia->dl_version == DL_VERSION_2) {
4112 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4113 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4114 		    brdcst_addr_length);
4115 		if (brdcst_addr == NULL) {
4116 			brdcst_addr_length = 0;
4117 		}
4118 		sap_length = dlia->dl_sap_length;
4119 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4120 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4121 		    brdcst_addr_length, sap_length, phys_addr_length));
4122 	} else {
4123 		brdcst_addr_length = 6;
4124 		brdcst_addr = ip_six_byte_all_ones;
4125 		sap_length = -2;
4126 		phys_addr_length = brdcst_addr_length;
4127 	}
4128 
4129 	ill->ill_bcast_addr_length = brdcst_addr_length;
4130 	ill->ill_phys_addr_length = phys_addr_length;
4131 	ill->ill_sap_length = sap_length;
4132 
4133 	/*
4134 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4135 	 * but we must ensure a minimum IP MTU is used since other bits of
4136 	 * IP will fly apart otherwise.
4137 	 */
4138 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4139 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4140 	ill->ill_current_frag = ill->ill_max_frag;
4141 	ill->ill_mtu = ill->ill_max_frag;
4142 
4143 	ill->ill_type = ipm->ip_m_type;
4144 
4145 	if (!ill->ill_dlpi_style_set) {
4146 		if (dlia->dl_provider_style == DL_STYLE2)
4147 			ill->ill_needs_attach = 1;
4148 
4149 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4150 
4151 		/*
4152 		 * Allocate the first ipif on this ill.  We don't delay it
4153 		 * further as ioctl handling assumes at least one ipif exists.
4154 		 *
4155 		 * At this point we don't know whether the ill is v4 or v6.
4156 		 * We will know this whan the SIOCSLIFNAME happens and
4157 		 * the correct value for ill_isv6 will be assigned in
4158 		 * ipif_set_values(). We need to hold the ill lock and
4159 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4160 		 * the wakeup.
4161 		 */
4162 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4163 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
4164 		mutex_enter(&ill->ill_lock);
4165 		ASSERT(ill->ill_dlpi_style_set == 0);
4166 		ill->ill_dlpi_style_set = 1;
4167 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4168 		cv_broadcast(&ill->ill_cv);
4169 		mutex_exit(&ill->ill_lock);
4170 		freemsg(mp);
4171 		return;
4172 	}
4173 	ASSERT(ill->ill_ipif != NULL);
4174 	/*
4175 	 * We know whether it is IPv4 or IPv6 now, as this is the
4176 	 * second DL_INFO_ACK we are recieving in response to the
4177 	 * DL_INFO_REQ sent in ipif_set_values.
4178 	 */
4179 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4180 	/*
4181 	 * Clear all the flags that were set based on ill_bcast_addr_length
4182 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4183 	 * changed now and we need to re-evaluate.
4184 	 */
4185 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4186 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4187 
4188 	/*
4189 	 * Free ill_bcast_mp as things could have changed now.
4190 	 *
4191 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4192 	 * with no underlying interfaces (and thus an unknown broadcast
4193 	 * address length), but we enforce that an interface is broadcast-
4194 	 * capable as part of allowing it to join a group.
4195 	 */
4196 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4197 		if (ill->ill_bcast_mp != NULL)
4198 			freemsg(ill->ill_bcast_mp);
4199 		ill->ill_net_type = IRE_IF_NORESOLVER;
4200 
4201 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4202 		    ill->ill_phys_addr_length,
4203 		    ill->ill_sap,
4204 		    ill->ill_sap_length);
4205 
4206 		if (ill->ill_isv6)
4207 			/*
4208 			 * Note: xresolv interfaces will eventually need NOARP
4209 			 * set here as well, but that will require those
4210 			 * external resolvers to have some knowledge of
4211 			 * that flag and act appropriately. Not to be changed
4212 			 * at present.
4213 			 */
4214 			ill->ill_flags |= ILLF_NONUD;
4215 		else
4216 			ill->ill_flags |= ILLF_NOARP;
4217 
4218 		if (ill->ill_mactype == SUNW_DL_VNI) {
4219 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4220 		} else if (ill->ill_phys_addr_length == 0 ||
4221 		    ill->ill_mactype == DL_IPV4 ||
4222 		    ill->ill_mactype == DL_IPV6) {
4223 			/*
4224 			 * The underying link is point-to-point, so mark the
4225 			 * interface as such.  We can do IP multicast over
4226 			 * such a link since it transmits all network-layer
4227 			 * packets to the remote side the same way.
4228 			 */
4229 			ill->ill_flags |= ILLF_MULTICAST;
4230 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4231 		}
4232 	} else {
4233 		ill->ill_net_type = IRE_IF_RESOLVER;
4234 		if (ill->ill_bcast_mp != NULL)
4235 			freemsg(ill->ill_bcast_mp);
4236 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4237 		    ill->ill_bcast_addr_length, ill->ill_sap,
4238 		    ill->ill_sap_length);
4239 		/*
4240 		 * Later detect lack of DLPI driver multicast
4241 		 * capability by catching DL_ENABMULTI errors in
4242 		 * ip_rput_dlpi.
4243 		 */
4244 		ill->ill_flags |= ILLF_MULTICAST;
4245 		if (!ill->ill_isv6)
4246 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4247 	}
4248 
4249 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4250 	if (ill->ill_mactype == SUNW_DL_IPMP)
4251 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4252 
4253 	/* By default an interface does not support any CoS marking */
4254 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4255 
4256 	/*
4257 	 * If we get QoS information in DL_INFO_ACK, the device supports
4258 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4259 	 */
4260 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4261 	    dlia->dl_qos_length);
4262 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4263 		ill->ill_flags |= ILLF_COS_ENABLED;
4264 	}
4265 
4266 	/* Clear any previous error indication. */
4267 	ill->ill_error = 0;
4268 	freemsg(mp);
4269 }
4270 
4271 /*
4272  * Perform various checks to verify that an address would make sense as a
4273  * local, remote, or subnet interface address.
4274  */
4275 static boolean_t
4276 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4277 {
4278 	ipaddr_t	net_mask;
4279 
4280 	/*
4281 	 * Don't allow all zeroes, or all ones, but allow
4282 	 * all ones netmask.
4283 	 */
4284 	if ((net_mask = ip_net_mask(addr)) == 0)
4285 		return (B_FALSE);
4286 	/* A given netmask overrides the "guess" netmask */
4287 	if (subnet_mask != 0)
4288 		net_mask = subnet_mask;
4289 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4290 	    (addr == (addr | ~net_mask)))) {
4291 		return (B_FALSE);
4292 	}
4293 
4294 	/*
4295 	 * Even if the netmask is all ones, we do not allow address to be
4296 	 * 255.255.255.255
4297 	 */
4298 	if (addr == INADDR_BROADCAST)
4299 		return (B_FALSE);
4300 
4301 	if (CLASSD(addr))
4302 		return (B_FALSE);
4303 
4304 	return (B_TRUE);
4305 }
4306 
4307 #define	V6_IPIF_LINKLOCAL(p)	\
4308 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4309 
4310 /*
4311  * Compare two given ipifs and check if the second one is better than
4312  * the first one using the order of preference (not taking deprecated
4313  * into acount) specified in ipif_lookup_multicast().
4314  */
4315 static boolean_t
4316 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4317 {
4318 	/* Check the least preferred first. */
4319 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4320 		/* If both ipifs are the same, use the first one. */
4321 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4322 			return (B_FALSE);
4323 		else
4324 			return (B_TRUE);
4325 	}
4326 
4327 	/* For IPv6, check for link local address. */
4328 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4329 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4330 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4331 			/* The second one is equal or less preferred. */
4332 			return (B_FALSE);
4333 		} else {
4334 			return (B_TRUE);
4335 		}
4336 	}
4337 
4338 	/* Then check for point to point interface. */
4339 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4340 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4341 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4342 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4343 			return (B_FALSE);
4344 		} else {
4345 			return (B_TRUE);
4346 		}
4347 	}
4348 
4349 	/* old_ipif is a normal interface, so no need to use the new one. */
4350 	return (B_FALSE);
4351 }
4352 
4353 /*
4354  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4355  * The ipif must be up, and its ill must multicast-capable, not
4356  * condemned, not an underlying interface in an IPMP group, and
4357  * not a VNI interface.  Order of preference:
4358  *
4359  * 	1a. normal
4360  * 	1b. normal, but deprecated
4361  * 	2a. point to point
4362  * 	2b. point to point, but deprecated
4363  * 	3a. link local
4364  * 	3b. link local, but deprecated
4365  * 	4. loopback.
4366  */
4367 static ipif_t *
4368 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4369 {
4370 	ill_t			*ill;
4371 	ill_walk_context_t	ctx;
4372 	ipif_t			*ipif;
4373 	ipif_t			*saved_ipif = NULL;
4374 	ipif_t			*dep_ipif = NULL;
4375 
4376 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4377 	if (isv6)
4378 		ill = ILL_START_WALK_V6(&ctx, ipst);
4379 	else
4380 		ill = ILL_START_WALK_V4(&ctx, ipst);
4381 
4382 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4383 		mutex_enter(&ill->ill_lock);
4384 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4385 		    ILL_IS_CONDEMNED(ill) ||
4386 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4387 			mutex_exit(&ill->ill_lock);
4388 			continue;
4389 		}
4390 		for (ipif = ill->ill_ipif; ipif != NULL;
4391 		    ipif = ipif->ipif_next) {
4392 			if (zoneid != ipif->ipif_zoneid &&
4393 			    zoneid != ALL_ZONES &&
4394 			    ipif->ipif_zoneid != ALL_ZONES) {
4395 				continue;
4396 			}
4397 			if (!(ipif->ipif_flags & IPIF_UP) ||
4398 			    IPIF_IS_CONDEMNED(ipif)) {
4399 				continue;
4400 			}
4401 
4402 			/*
4403 			 * Found one candidate.  If it is deprecated,
4404 			 * remember it in dep_ipif.  If it is not deprecated,
4405 			 * remember it in saved_ipif.
4406 			 */
4407 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4408 				if (dep_ipif == NULL) {
4409 					dep_ipif = ipif;
4410 				} else if (ipif_comp_multi(dep_ipif, ipif,
4411 				    isv6)) {
4412 					/*
4413 					 * If the previous dep_ipif does not
4414 					 * belong to the same ill, we've done
4415 					 * a ipif_refhold() on it.  So we need
4416 					 * to release it.
4417 					 */
4418 					if (dep_ipif->ipif_ill != ill)
4419 						ipif_refrele(dep_ipif);
4420 					dep_ipif = ipif;
4421 				}
4422 				continue;
4423 			}
4424 			if (saved_ipif == NULL) {
4425 				saved_ipif = ipif;
4426 			} else {
4427 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4428 					if (saved_ipif->ipif_ill != ill)
4429 						ipif_refrele(saved_ipif);
4430 					saved_ipif = ipif;
4431 				}
4432 			}
4433 		}
4434 		/*
4435 		 * Before going to the next ill, do a ipif_refhold() on the
4436 		 * saved ones.
4437 		 */
4438 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4439 			ipif_refhold_locked(saved_ipif);
4440 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4441 			ipif_refhold_locked(dep_ipif);
4442 		mutex_exit(&ill->ill_lock);
4443 	}
4444 	rw_exit(&ipst->ips_ill_g_lock);
4445 
4446 	/*
4447 	 * If we have only the saved_ipif, return it.  But if we have both
4448 	 * saved_ipif and dep_ipif, check to see which one is better.
4449 	 */
4450 	if (saved_ipif != NULL) {
4451 		if (dep_ipif != NULL) {
4452 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4453 				ipif_refrele(saved_ipif);
4454 				return (dep_ipif);
4455 			} else {
4456 				ipif_refrele(dep_ipif);
4457 				return (saved_ipif);
4458 			}
4459 		}
4460 		return (saved_ipif);
4461 	} else {
4462 		return (dep_ipif);
4463 	}
4464 }
4465 
4466 ill_t *
4467 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4468 {
4469 	ipif_t *ipif;
4470 	ill_t *ill;
4471 
4472 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4473 	if (ipif == NULL)
4474 		return (NULL);
4475 
4476 	ill = ipif->ipif_ill;
4477 	ill_refhold(ill);
4478 	ipif_refrele(ipif);
4479 	return (ill);
4480 }
4481 
4482 /*
4483  * This function is called when an application does not specify an interface
4484  * to be used for multicast traffic (joining a group/sending data).  It
4485  * calls ire_lookup_multi() to look for an interface route for the
4486  * specified multicast group.  Doing this allows the administrator to add
4487  * prefix routes for multicast to indicate which interface to be used for
4488  * multicast traffic in the above scenario.  The route could be for all
4489  * multicast (224.0/4), for a single multicast group (a /32 route) or
4490  * anything in between.  If there is no such multicast route, we just find
4491  * any multicast capable interface and return it.  The returned ipif
4492  * is refhold'ed.
4493  *
4494  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4495  * unicast table. This is used by CGTP.
4496  */
4497 ill_t *
4498 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4499     boolean_t *multirtp, ipaddr_t *setsrcp)
4500 {
4501 	ill_t			*ill;
4502 
4503 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4504 	if (ill != NULL)
4505 		return (ill);
4506 
4507 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4508 }
4509 
4510 /*
4511  * Look for an ipif with the specified interface address and destination.
4512  * The destination address is used only for matching point-to-point interfaces.
4513  */
4514 ipif_t *
4515 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4516 {
4517 	ipif_t	*ipif;
4518 	ill_t	*ill;
4519 	ill_walk_context_t ctx;
4520 
4521 	/*
4522 	 * First match all the point-to-point interfaces
4523 	 * before looking at non-point-to-point interfaces.
4524 	 * This is done to avoid returning non-point-to-point
4525 	 * ipif instead of unnumbered point-to-point ipif.
4526 	 */
4527 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4528 	ill = ILL_START_WALK_V4(&ctx, ipst);
4529 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4530 		mutex_enter(&ill->ill_lock);
4531 		for (ipif = ill->ill_ipif; ipif != NULL;
4532 		    ipif = ipif->ipif_next) {
4533 			/* Allow the ipif to be down */
4534 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4535 			    (ipif->ipif_lcl_addr == if_addr) &&
4536 			    (ipif->ipif_pp_dst_addr == dst)) {
4537 				if (!IPIF_IS_CONDEMNED(ipif)) {
4538 					ipif_refhold_locked(ipif);
4539 					mutex_exit(&ill->ill_lock);
4540 					rw_exit(&ipst->ips_ill_g_lock);
4541 					return (ipif);
4542 				}
4543 			}
4544 		}
4545 		mutex_exit(&ill->ill_lock);
4546 	}
4547 	rw_exit(&ipst->ips_ill_g_lock);
4548 
4549 	/* lookup the ipif based on interface address */
4550 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4551 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4552 	return (ipif);
4553 }
4554 
4555 /*
4556  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4557  */
4558 static ipif_t *
4559 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4560     zoneid_t zoneid, ip_stack_t *ipst)
4561 {
4562 	ipif_t  *ipif;
4563 	ill_t   *ill;
4564 	boolean_t ptp = B_FALSE;
4565 	ill_walk_context_t	ctx;
4566 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4567 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4568 
4569 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4570 	/*
4571 	 * Repeat twice, first based on local addresses and
4572 	 * next time for pointopoint.
4573 	 */
4574 repeat:
4575 	ill = ILL_START_WALK_V4(&ctx, ipst);
4576 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4577 		if (match_ill != NULL && ill != match_ill &&
4578 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4579 			continue;
4580 		}
4581 		mutex_enter(&ill->ill_lock);
4582 		for (ipif = ill->ill_ipif; ipif != NULL;
4583 		    ipif = ipif->ipif_next) {
4584 			if (zoneid != ALL_ZONES &&
4585 			    zoneid != ipif->ipif_zoneid &&
4586 			    ipif->ipif_zoneid != ALL_ZONES)
4587 				continue;
4588 
4589 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4590 				continue;
4591 
4592 			/* Allow the ipif to be down */
4593 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4594 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4595 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4596 			    (ipif->ipif_pp_dst_addr == addr))) {
4597 				if (!IPIF_IS_CONDEMNED(ipif)) {
4598 					ipif_refhold_locked(ipif);
4599 					mutex_exit(&ill->ill_lock);
4600 					rw_exit(&ipst->ips_ill_g_lock);
4601 					return (ipif);
4602 				}
4603 			}
4604 		}
4605 		mutex_exit(&ill->ill_lock);
4606 	}
4607 
4608 	/* If we already did the ptp case, then we are done */
4609 	if (ptp) {
4610 		rw_exit(&ipst->ips_ill_g_lock);
4611 		return (NULL);
4612 	}
4613 	ptp = B_TRUE;
4614 	goto repeat;
4615 }
4616 
4617 /*
4618  * Lookup an ipif with the specified address.  For point-to-point links we
4619  * look for matches on either the destination address or the local address,
4620  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4621  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4622  * (or illgrp if `match_ill' is in an IPMP group).
4623  */
4624 ipif_t *
4625 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4626     ip_stack_t *ipst)
4627 {
4628 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4629 	    zoneid, ipst));
4630 }
4631 
4632 /*
4633  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4634  * except that we will only return an address if it is not marked as
4635  * IPIF_DUPLICATE
4636  */
4637 ipif_t *
4638 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4639     ip_stack_t *ipst)
4640 {
4641 	return (ipif_lookup_addr_common(addr, match_ill,
4642 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4643 	    zoneid, ipst));
4644 }
4645 
4646 /*
4647  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4648  * `match_ill' across the IPMP group.  This function is only needed in some
4649  * corner-cases; almost everything should use ipif_lookup_addr().
4650  */
4651 ipif_t *
4652 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4653 {
4654 	ASSERT(match_ill != NULL);
4655 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4656 	    ipst));
4657 }
4658 
4659 /*
4660  * Look for an ipif with the specified address. For point-point links
4661  * we look for matches on either the destination address and the local
4662  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4663  * is set.
4664  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4665  * ill (or illgrp if `match_ill' is in an IPMP group).
4666  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4667  */
4668 zoneid_t
4669 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4670 {
4671 	zoneid_t zoneid;
4672 	ipif_t  *ipif;
4673 	ill_t   *ill;
4674 	boolean_t ptp = B_FALSE;
4675 	ill_walk_context_t	ctx;
4676 
4677 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4678 	/*
4679 	 * Repeat twice, first based on local addresses and
4680 	 * next time for pointopoint.
4681 	 */
4682 repeat:
4683 	ill = ILL_START_WALK_V4(&ctx, ipst);
4684 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4685 		if (match_ill != NULL && ill != match_ill &&
4686 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4687 			continue;
4688 		}
4689 		mutex_enter(&ill->ill_lock);
4690 		for (ipif = ill->ill_ipif; ipif != NULL;
4691 		    ipif = ipif->ipif_next) {
4692 			/* Allow the ipif to be down */
4693 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4694 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4695 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4696 			    (ipif->ipif_pp_dst_addr == addr)) &&
4697 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4698 				zoneid = ipif->ipif_zoneid;
4699 				mutex_exit(&ill->ill_lock);
4700 				rw_exit(&ipst->ips_ill_g_lock);
4701 				/*
4702 				 * If ipif_zoneid was ALL_ZONES then we have
4703 				 * a trusted extensions shared IP address.
4704 				 * In that case GLOBAL_ZONEID works to send.
4705 				 */
4706 				if (zoneid == ALL_ZONES)
4707 					zoneid = GLOBAL_ZONEID;
4708 				return (zoneid);
4709 			}
4710 		}
4711 		mutex_exit(&ill->ill_lock);
4712 	}
4713 
4714 	/* If we already did the ptp case, then we are done */
4715 	if (ptp) {
4716 		rw_exit(&ipst->ips_ill_g_lock);
4717 		return (ALL_ZONES);
4718 	}
4719 	ptp = B_TRUE;
4720 	goto repeat;
4721 }
4722 
4723 /*
4724  * Look for an ipif that matches the specified remote address i.e. the
4725  * ipif that would receive the specified packet.
4726  * First look for directly connected interfaces and then do a recursive
4727  * IRE lookup and pick the first ipif corresponding to the source address in the
4728  * ire.
4729  * Returns: held ipif
4730  *
4731  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4732  */
4733 ipif_t *
4734 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4735 {
4736 	ipif_t	*ipif;
4737 
4738 	ASSERT(!ill->ill_isv6);
4739 
4740 	/*
4741 	 * Someone could be changing this ipif currently or change it
4742 	 * after we return this. Thus  a few packets could use the old
4743 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4744 	 * will atomically be updated or cleaned up with the new value
4745 	 * Thus we don't need a lock to check the flags or other attrs below.
4746 	 */
4747 	mutex_enter(&ill->ill_lock);
4748 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4749 		if (IPIF_IS_CONDEMNED(ipif))
4750 			continue;
4751 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4752 		    ipif->ipif_zoneid != ALL_ZONES)
4753 			continue;
4754 		/* Allow the ipif to be down */
4755 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4756 			if ((ipif->ipif_pp_dst_addr == addr) ||
4757 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4758 			    ipif->ipif_lcl_addr == addr)) {
4759 				ipif_refhold_locked(ipif);
4760 				mutex_exit(&ill->ill_lock);
4761 				return (ipif);
4762 			}
4763 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4764 			ipif_refhold_locked(ipif);
4765 			mutex_exit(&ill->ill_lock);
4766 			return (ipif);
4767 		}
4768 	}
4769 	mutex_exit(&ill->ill_lock);
4770 	/*
4771 	 * For a remote destination it isn't possible to nail down a particular
4772 	 * ipif.
4773 	 */
4774 
4775 	/* Pick the first interface */
4776 	ipif = ipif_get_next_ipif(NULL, ill);
4777 	return (ipif);
4778 }
4779 
4780 /*
4781  * This func does not prevent refcnt from increasing. But if
4782  * the caller has taken steps to that effect, then this func
4783  * can be used to determine whether the ill has become quiescent
4784  */
4785 static boolean_t
4786 ill_is_quiescent(ill_t *ill)
4787 {
4788 	ipif_t	*ipif;
4789 
4790 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4791 
4792 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4793 		if (ipif->ipif_refcnt != 0)
4794 			return (B_FALSE);
4795 	}
4796 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4797 		return (B_FALSE);
4798 	}
4799 	return (B_TRUE);
4800 }
4801 
4802 boolean_t
4803 ill_is_freeable(ill_t *ill)
4804 {
4805 	ipif_t	*ipif;
4806 
4807 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4808 
4809 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4810 		if (ipif->ipif_refcnt != 0) {
4811 			return (B_FALSE);
4812 		}
4813 	}
4814 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4815 		return (B_FALSE);
4816 	}
4817 	return (B_TRUE);
4818 }
4819 
4820 /*
4821  * This func does not prevent refcnt from increasing. But if
4822  * the caller has taken steps to that effect, then this func
4823  * can be used to determine whether the ipif has become quiescent
4824  */
4825 static boolean_t
4826 ipif_is_quiescent(ipif_t *ipif)
4827 {
4828 	ill_t *ill;
4829 
4830 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4831 
4832 	if (ipif->ipif_refcnt != 0)
4833 		return (B_FALSE);
4834 
4835 	ill = ipif->ipif_ill;
4836 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4837 	    ill->ill_logical_down) {
4838 		return (B_TRUE);
4839 	}
4840 
4841 	/* This is the last ipif going down or being deleted on this ill */
4842 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4843 		return (B_FALSE);
4844 	}
4845 
4846 	return (B_TRUE);
4847 }
4848 
4849 /*
4850  * return true if the ipif can be destroyed: the ipif has to be quiescent
4851  * with zero references from ire/ilm to it.
4852  */
4853 static boolean_t
4854 ipif_is_freeable(ipif_t *ipif)
4855 {
4856 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4857 	ASSERT(ipif->ipif_id != 0);
4858 	return (ipif->ipif_refcnt == 0);
4859 }
4860 
4861 /*
4862  * The ipif/ill/ire has been refreled. Do the tail processing.
4863  * Determine if the ipif or ill in question has become quiescent and if so
4864  * wakeup close and/or restart any queued pending ioctl that is waiting
4865  * for the ipif_down (or ill_down)
4866  */
4867 void
4868 ipif_ill_refrele_tail(ill_t *ill)
4869 {
4870 	mblk_t	*mp;
4871 	conn_t	*connp;
4872 	ipsq_t	*ipsq;
4873 	ipxop_t	*ipx;
4874 	ipif_t	*ipif;
4875 	dl_notify_ind_t *dlindp;
4876 
4877 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4878 
4879 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4880 		/* ip_modclose() may be waiting */
4881 		cv_broadcast(&ill->ill_cv);
4882 	}
4883 
4884 	ipsq = ill->ill_phyint->phyint_ipsq;
4885 	mutex_enter(&ipsq->ipsq_lock);
4886 	ipx = ipsq->ipsq_xop;
4887 	mutex_enter(&ipx->ipx_lock);
4888 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4889 		goto unlock;
4890 
4891 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4892 
4893 	ipif = ipx->ipx_pending_ipif;
4894 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4895 		goto unlock;
4896 
4897 	switch (ipx->ipx_waitfor) {
4898 	case IPIF_DOWN:
4899 		if (!ipif_is_quiescent(ipif))
4900 			goto unlock;
4901 		break;
4902 	case IPIF_FREE:
4903 		if (!ipif_is_freeable(ipif))
4904 			goto unlock;
4905 		break;
4906 	case ILL_DOWN:
4907 		if (!ill_is_quiescent(ill))
4908 			goto unlock;
4909 		break;
4910 	case ILL_FREE:
4911 		/*
4912 		 * ILL_FREE is only for loopback; normal ill teardown waits
4913 		 * synchronously in ip_modclose() without using ipx_waitfor,
4914 		 * handled by the cv_broadcast() at the top of this function.
4915 		 */
4916 		if (!ill_is_freeable(ill))
4917 			goto unlock;
4918 		break;
4919 	default:
4920 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4921 		    (void *)ipsq, ipx->ipx_waitfor);
4922 	}
4923 
4924 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4925 	mutex_exit(&ipx->ipx_lock);
4926 	mp = ipsq_pending_mp_get(ipsq, &connp);
4927 	mutex_exit(&ipsq->ipsq_lock);
4928 	mutex_exit(&ill->ill_lock);
4929 
4930 	ASSERT(mp != NULL);
4931 	/*
4932 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4933 	 * we can only get here when the current operation decides it
4934 	 * it needs to quiesce via ipsq_pending_mp_add().
4935 	 */
4936 	switch (mp->b_datap->db_type) {
4937 	case M_PCPROTO:
4938 	case M_PROTO:
4939 		/*
4940 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4941 		 */
4942 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4943 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4944 
4945 		switch (dlindp->dl_notification) {
4946 		case DL_NOTE_PHYS_ADDR:
4947 			qwriter_ip(ill, ill->ill_rq, mp,
4948 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4949 			return;
4950 		case DL_NOTE_REPLUMB:
4951 			qwriter_ip(ill, ill->ill_rq, mp,
4952 			    ill_replumb_tail, CUR_OP, B_TRUE);
4953 			return;
4954 		default:
4955 			ASSERT(0);
4956 			ill_refrele(ill);
4957 		}
4958 		break;
4959 
4960 	case M_ERROR:
4961 	case M_HANGUP:
4962 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4963 		    B_TRUE);
4964 		return;
4965 
4966 	case M_IOCTL:
4967 	case M_IOCDATA:
4968 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4969 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4970 		return;
4971 
4972 	default:
4973 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4974 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4975 	}
4976 	return;
4977 unlock:
4978 	mutex_exit(&ipsq->ipsq_lock);
4979 	mutex_exit(&ipx->ipx_lock);
4980 	mutex_exit(&ill->ill_lock);
4981 }
4982 
4983 #ifdef DEBUG
4984 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4985 static void
4986 th_trace_rrecord(th_trace_t *th_trace)
4987 {
4988 	tr_buf_t *tr_buf;
4989 	uint_t lastref;
4990 
4991 	lastref = th_trace->th_trace_lastref;
4992 	lastref++;
4993 	if (lastref == TR_BUF_MAX)
4994 		lastref = 0;
4995 	th_trace->th_trace_lastref = lastref;
4996 	tr_buf = &th_trace->th_trbuf[lastref];
4997 	tr_buf->tr_time = lbolt;
4998 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
4999 }
5000 
5001 static void
5002 th_trace_free(void *value)
5003 {
5004 	th_trace_t *th_trace = value;
5005 
5006 	ASSERT(th_trace->th_refcnt == 0);
5007 	kmem_free(th_trace, sizeof (*th_trace));
5008 }
5009 
5010 /*
5011  * Find or create the per-thread hash table used to track object references.
5012  * The ipst argument is NULL if we shouldn't allocate.
5013  *
5014  * Accesses per-thread data, so there's no need to lock here.
5015  */
5016 static mod_hash_t *
5017 th_trace_gethash(ip_stack_t *ipst)
5018 {
5019 	th_hash_t *thh;
5020 
5021 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5022 		mod_hash_t *mh;
5023 		char name[256];
5024 		size_t objsize, rshift;
5025 		int retv;
5026 
5027 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5028 			return (NULL);
5029 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5030 		    (void *)curthread);
5031 
5032 		/*
5033 		 * We use mod_hash_create_extended here rather than the more
5034 		 * obvious mod_hash_create_ptrhash because the latter has a
5035 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5036 		 * block.
5037 		 */
5038 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5039 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5040 		rshift = highbit(objsize);
5041 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5042 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5043 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5044 		if (mh == NULL) {
5045 			kmem_free(thh, sizeof (*thh));
5046 			return (NULL);
5047 		}
5048 		thh->thh_hash = mh;
5049 		thh->thh_ipst = ipst;
5050 		/*
5051 		 * We trace ills, ipifs, ires, and nces.  All of these are
5052 		 * per-IP-stack, so the lock on the thread list is as well.
5053 		 */
5054 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5055 		list_insert_tail(&ip_thread_list, thh);
5056 		rw_exit(&ip_thread_rwlock);
5057 		retv = tsd_set(ip_thread_data, thh);
5058 		ASSERT(retv == 0);
5059 	}
5060 	return (thh != NULL ? thh->thh_hash : NULL);
5061 }
5062 
5063 boolean_t
5064 th_trace_ref(const void *obj, ip_stack_t *ipst)
5065 {
5066 	th_trace_t *th_trace;
5067 	mod_hash_t *mh;
5068 	mod_hash_val_t val;
5069 
5070 	if ((mh = th_trace_gethash(ipst)) == NULL)
5071 		return (B_FALSE);
5072 
5073 	/*
5074 	 * Attempt to locate the trace buffer for this obj and thread.
5075 	 * If it does not exist, then allocate a new trace buffer and
5076 	 * insert into the hash.
5077 	 */
5078 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5079 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5080 		if (th_trace == NULL)
5081 			return (B_FALSE);
5082 
5083 		th_trace->th_id = curthread;
5084 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5085 		    (mod_hash_val_t)th_trace) != 0) {
5086 			kmem_free(th_trace, sizeof (th_trace_t));
5087 			return (B_FALSE);
5088 		}
5089 	} else {
5090 		th_trace = (th_trace_t *)val;
5091 	}
5092 
5093 	ASSERT(th_trace->th_refcnt >= 0 &&
5094 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5095 
5096 	th_trace->th_refcnt++;
5097 	th_trace_rrecord(th_trace);
5098 	return (B_TRUE);
5099 }
5100 
5101 /*
5102  * For the purpose of tracing a reference release, we assume that global
5103  * tracing is always on and that the same thread initiated the reference hold
5104  * is releasing.
5105  */
5106 void
5107 th_trace_unref(const void *obj)
5108 {
5109 	int retv;
5110 	mod_hash_t *mh;
5111 	th_trace_t *th_trace;
5112 	mod_hash_val_t val;
5113 
5114 	mh = th_trace_gethash(NULL);
5115 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5116 	ASSERT(retv == 0);
5117 	th_trace = (th_trace_t *)val;
5118 
5119 	ASSERT(th_trace->th_refcnt > 0);
5120 	th_trace->th_refcnt--;
5121 	th_trace_rrecord(th_trace);
5122 }
5123 
5124 /*
5125  * If tracing has been disabled, then we assume that the reference counts are
5126  * now useless, and we clear them out before destroying the entries.
5127  */
5128 void
5129 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5130 {
5131 	th_hash_t	*thh;
5132 	mod_hash_t	*mh;
5133 	mod_hash_val_t	val;
5134 	th_trace_t	*th_trace;
5135 	int		retv;
5136 
5137 	rw_enter(&ip_thread_rwlock, RW_READER);
5138 	for (thh = list_head(&ip_thread_list); thh != NULL;
5139 	    thh = list_next(&ip_thread_list, thh)) {
5140 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5141 		    &val) == 0) {
5142 			th_trace = (th_trace_t *)val;
5143 			if (trace_disable)
5144 				th_trace->th_refcnt = 0;
5145 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5146 			ASSERT(retv == 0);
5147 		}
5148 	}
5149 	rw_exit(&ip_thread_rwlock);
5150 }
5151 
5152 void
5153 ipif_trace_ref(ipif_t *ipif)
5154 {
5155 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5156 
5157 	if (ipif->ipif_trace_disable)
5158 		return;
5159 
5160 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5161 		ipif->ipif_trace_disable = B_TRUE;
5162 		ipif_trace_cleanup(ipif);
5163 	}
5164 }
5165 
5166 void
5167 ipif_untrace_ref(ipif_t *ipif)
5168 {
5169 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5170 
5171 	if (!ipif->ipif_trace_disable)
5172 		th_trace_unref(ipif);
5173 }
5174 
5175 void
5176 ill_trace_ref(ill_t *ill)
5177 {
5178 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5179 
5180 	if (ill->ill_trace_disable)
5181 		return;
5182 
5183 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5184 		ill->ill_trace_disable = B_TRUE;
5185 		ill_trace_cleanup(ill);
5186 	}
5187 }
5188 
5189 void
5190 ill_untrace_ref(ill_t *ill)
5191 {
5192 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5193 
5194 	if (!ill->ill_trace_disable)
5195 		th_trace_unref(ill);
5196 }
5197 
5198 /*
5199  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5200  * failure, ipif_trace_disable is set.
5201  */
5202 static void
5203 ipif_trace_cleanup(const ipif_t *ipif)
5204 {
5205 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5206 }
5207 
5208 /*
5209  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5210  * failure, ill_trace_disable is set.
5211  */
5212 static void
5213 ill_trace_cleanup(const ill_t *ill)
5214 {
5215 	th_trace_cleanup(ill, ill->ill_trace_disable);
5216 }
5217 #endif /* DEBUG */
5218 
5219 void
5220 ipif_refhold_locked(ipif_t *ipif)
5221 {
5222 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5223 	ipif->ipif_refcnt++;
5224 	IPIF_TRACE_REF(ipif);
5225 }
5226 
5227 void
5228 ipif_refhold(ipif_t *ipif)
5229 {
5230 	ill_t	*ill;
5231 
5232 	ill = ipif->ipif_ill;
5233 	mutex_enter(&ill->ill_lock);
5234 	ipif->ipif_refcnt++;
5235 	IPIF_TRACE_REF(ipif);
5236 	mutex_exit(&ill->ill_lock);
5237 }
5238 
5239 /*
5240  * Must not be called while holding any locks. Otherwise if this is
5241  * the last reference to be released there is a chance of recursive mutex
5242  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5243  * to restart an ioctl.
5244  */
5245 void
5246 ipif_refrele(ipif_t *ipif)
5247 {
5248 	ill_t	*ill;
5249 
5250 	ill = ipif->ipif_ill;
5251 
5252 	mutex_enter(&ill->ill_lock);
5253 	ASSERT(ipif->ipif_refcnt != 0);
5254 	ipif->ipif_refcnt--;
5255 	IPIF_UNTRACE_REF(ipif);
5256 	if (ipif->ipif_refcnt != 0) {
5257 		mutex_exit(&ill->ill_lock);
5258 		return;
5259 	}
5260 
5261 	/* Drops the ill_lock */
5262 	ipif_ill_refrele_tail(ill);
5263 }
5264 
5265 ipif_t *
5266 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5267 {
5268 	ipif_t	*ipif;
5269 
5270 	mutex_enter(&ill->ill_lock);
5271 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5272 	    ipif != NULL; ipif = ipif->ipif_next) {
5273 		if (IPIF_IS_CONDEMNED(ipif))
5274 			continue;
5275 		ipif_refhold_locked(ipif);
5276 		mutex_exit(&ill->ill_lock);
5277 		return (ipif);
5278 	}
5279 	mutex_exit(&ill->ill_lock);
5280 	return (NULL);
5281 }
5282 
5283 /*
5284  * TODO: make this table extendible at run time
5285  * Return a pointer to the mac type info for 'mac_type'
5286  */
5287 static ip_m_t *
5288 ip_m_lookup(t_uscalar_t mac_type)
5289 {
5290 	ip_m_t	*ipm;
5291 
5292 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5293 		if (ipm->ip_m_mac_type == mac_type)
5294 			return (ipm);
5295 	return (NULL);
5296 }
5297 
5298 /*
5299  * Make a link layer address from the multicast IP address *addr.
5300  * To form the link layer address, invoke the ip_m_v*mapping function
5301  * associated with the link-layer type.
5302  */
5303 void
5304 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5305 {
5306 	ip_m_t *ipm;
5307 
5308 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5309 		return;
5310 
5311 	ASSERT(addr != NULL);
5312 
5313 	ipm = ip_m_lookup(ill->ill_mactype);
5314 	if (ipm == NULL ||
5315 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5316 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5317 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5318 		    ill->ill_name, ill->ill_mactype));
5319 		return;
5320 	}
5321 	if (ill->ill_isv6)
5322 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5323 	else
5324 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5325 }
5326 
5327 /*
5328  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5329  * ill is passed in to associate it with the correct interface.
5330  * If ire_arg is set, then we return the held IRE in that location.
5331  */
5332 int
5333 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5334     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5335     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5336 {
5337 	ire_t	*ire, *nire;
5338 	ire_t	*gw_ire = NULL;
5339 	ipif_t	*ipif = NULL;
5340 	uint_t	type;
5341 	int	match_flags = MATCH_IRE_TYPE;
5342 	tsol_gc_t *gc = NULL;
5343 	tsol_gcgrp_t *gcgrp = NULL;
5344 	boolean_t gcgrp_xtraref = B_FALSE;
5345 	boolean_t cgtp_broadcast;
5346 
5347 	ip1dbg(("ip_rt_add:"));
5348 
5349 	if (ire_arg != NULL)
5350 		*ire_arg = NULL;
5351 
5352 	/*
5353 	 * If this is the case of RTF_HOST being set, then we set the netmask
5354 	 * to all ones (regardless if one was supplied).
5355 	 */
5356 	if (flags & RTF_HOST)
5357 		mask = IP_HOST_MASK;
5358 
5359 	/*
5360 	 * Prevent routes with a zero gateway from being created (since
5361 	 * interfaces can currently be plumbed and brought up no assigned
5362 	 * address).
5363 	 */
5364 	if (gw_addr == 0)
5365 		return (ENETUNREACH);
5366 	/*
5367 	 * Get the ipif, if any, corresponding to the gw_addr
5368 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5369 	 * we match on the gatway and destination to handle unnumbered pt-pt
5370 	 * interfaces.
5371 	 */
5372 	if (ill != NULL)
5373 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5374 	else
5375 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5376 	if (ipif != NULL) {
5377 		if (IS_VNI(ipif->ipif_ill)) {
5378 			ipif_refrele(ipif);
5379 			return (EINVAL);
5380 		}
5381 	}
5382 
5383 	/*
5384 	 * GateD will attempt to create routes with a loopback interface
5385 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5386 	 * these routes to be added, but create them as interface routes
5387 	 * since the gateway is an interface address.
5388 	 */
5389 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5390 		flags &= ~RTF_GATEWAY;
5391 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5392 		    mask == IP_HOST_MASK) {
5393 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5394 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5395 			    NULL);
5396 			if (ire != NULL) {
5397 				ire_refrele(ire);
5398 				ipif_refrele(ipif);
5399 				return (EEXIST);
5400 			}
5401 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5402 			    "for 0x%x\n", (void *)ipif,
5403 			    ipif->ipif_ire_type,
5404 			    ntohl(ipif->ipif_lcl_addr)));
5405 			ire = ire_create(
5406 			    (uchar_t *)&dst_addr,	/* dest address */
5407 			    (uchar_t *)&mask,		/* mask */
5408 			    NULL,			/* no gateway */
5409 			    ipif->ipif_ire_type,	/* LOOPBACK */
5410 			    ipif->ipif_ill,
5411 			    zoneid,
5412 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5413 			    NULL,
5414 			    ipst);
5415 
5416 			if (ire == NULL) {
5417 				ipif_refrele(ipif);
5418 				return (ENOMEM);
5419 			}
5420 			/* src address assigned by the caller? */
5421 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5422 				ire->ire_setsrc_addr = src_addr;
5423 
5424 			nire = ire_add(ire);
5425 			if (nire == NULL) {
5426 				/*
5427 				 * In the result of failure, ire_add() will have
5428 				 * already deleted the ire in question, so there
5429 				 * is no need to do that here.
5430 				 */
5431 				ipif_refrele(ipif);
5432 				return (ENOMEM);
5433 			}
5434 			/*
5435 			 * Check if it was a duplicate entry. This handles
5436 			 * the case of two racing route adds for the same route
5437 			 */
5438 			if (nire != ire) {
5439 				ASSERT(nire->ire_identical_ref > 1);
5440 				ire_delete(nire);
5441 				ire_refrele(nire);
5442 				ipif_refrele(ipif);
5443 				return (EEXIST);
5444 			}
5445 			ire = nire;
5446 			goto save_ire;
5447 		}
5448 	}
5449 
5450 	/*
5451 	 * The routes for multicast with CGTP are quite special in that
5452 	 * the gateway is the local interface address, yet RTF_GATEWAY
5453 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5454 	 * this undocumented and unusual use of multicast routes.
5455 	 */
5456 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5457 		flags &= ~RTF_GATEWAY;
5458 
5459 	/*
5460 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5461 	 * and the gateway address provided is one of the system's interface
5462 	 * addresses.  By using the routing socket interface and supplying an
5463 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5464 	 * specifying an interface route to be created is available which uses
5465 	 * the interface index that specifies the outgoing interface rather than
5466 	 * the address of an outgoing interface (which may not be able to
5467 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5468 	 * flag, routes can be specified which not only specify the next-hop to
5469 	 * be used when routing to a certain prefix, but also which outgoing
5470 	 * interface should be used.
5471 	 *
5472 	 * Previously, interfaces would have unique addresses assigned to them
5473 	 * and so the address assigned to a particular interface could be used
5474 	 * to identify a particular interface.  One exception to this was the
5475 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5476 	 *
5477 	 * With the advent of IPv6 and its link-local addresses, this
5478 	 * restriction was relaxed and interfaces could share addresses between
5479 	 * themselves.  In fact, typically all of the link-local interfaces on
5480 	 * an IPv6 node or router will have the same link-local address.  In
5481 	 * order to differentiate between these interfaces, the use of an
5482 	 * interface index is necessary and this index can be carried inside a
5483 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5484 	 * of using the interface index, however, is that all of the ipif's that
5485 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5486 	 * cannot be used to differentiate between ipif's (or logical
5487 	 * interfaces) that belong to the same ill (physical interface).
5488 	 *
5489 	 * For example, in the following case involving IPv4 interfaces and
5490 	 * logical interfaces
5491 	 *
5492 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5493 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5494 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5495 	 *
5496 	 * the ipif's corresponding to each of these interface routes can be
5497 	 * uniquely identified by the "gateway" (actually interface address).
5498 	 *
5499 	 * In this case involving multiple IPv6 default routes to a particular
5500 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5501 	 * default route is of interest:
5502 	 *
5503 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5504 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5505 	 */
5506 
5507 	/* RTF_GATEWAY not set */
5508 	if (!(flags & RTF_GATEWAY)) {
5509 		if (sp != NULL) {
5510 			ip2dbg(("ip_rt_add: gateway security attributes "
5511 			    "cannot be set with interface route\n"));
5512 			if (ipif != NULL)
5513 				ipif_refrele(ipif);
5514 			return (EINVAL);
5515 		}
5516 
5517 		/*
5518 		 * Whether or not ill (RTA_IFP) is set, we require that
5519 		 * the gateway is one of our local addresses.
5520 		 */
5521 		if (ipif == NULL)
5522 			return (ENETUNREACH);
5523 
5524 		/*
5525 		 * We use MATCH_IRE_ILL here. If the caller specified an
5526 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5527 		 * we use the ill derived from the gateway address.
5528 		 * We can always match the gateway address since we record it
5529 		 * in ire_gateway_addr.
5530 		 * We don't allow RTA_IFP to specify a different ill than the
5531 		 * one matching the ipif to make sure we can delete the route.
5532 		 */
5533 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5534 		if (ill == NULL) {
5535 			ill = ipif->ipif_ill;
5536 		} else if (ill != ipif->ipif_ill) {
5537 			ipif_refrele(ipif);
5538 			return (EINVAL);
5539 		}
5540 
5541 		/*
5542 		 * We check for an existing entry at this point.
5543 		 *
5544 		 * Since a netmask isn't passed in via the ioctl interface
5545 		 * (SIOCADDRT), we don't check for a matching netmask in that
5546 		 * case.
5547 		 */
5548 		if (!ioctl_msg)
5549 			match_flags |= MATCH_IRE_MASK;
5550 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5551 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5552 		    NULL);
5553 		if (ire != NULL) {
5554 			ire_refrele(ire);
5555 			ipif_refrele(ipif);
5556 			return (EEXIST);
5557 		}
5558 
5559 		/*
5560 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5561 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5562 		 * gateway.
5563 		 */
5564 		ire = ire_create(
5565 		    (uchar_t *)&dst_addr,
5566 		    (uint8_t *)&mask,
5567 		    (uint8_t *)&gw_addr,
5568 		    ill->ill_net_type,
5569 		    ill,
5570 		    zoneid,
5571 		    flags,
5572 		    NULL,
5573 		    ipst);
5574 		if (ire == NULL) {
5575 			ipif_refrele(ipif);
5576 			return (ENOMEM);
5577 		}
5578 
5579 		/*
5580 		 * Some software (for example, GateD and Sun Cluster) attempts
5581 		 * to create (what amount to) IRE_PREFIX routes with the
5582 		 * loopback address as the gateway.  This is primarily done to
5583 		 * set up prefixes with the RTF_REJECT flag set (for example,
5584 		 * when generating aggregate routes.)
5585 		 *
5586 		 * If the IRE type (as defined by ill->ill_net_type) is
5587 		 * IRE_LOOPBACK, then we map the request into a
5588 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5589 		 * these interface routes, by definition, can only be that.
5590 		 *
5591 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5592 		 * routine, but rather using ire_create() directly.
5593 		 *
5594 		 */
5595 		if (ill->ill_net_type == IRE_LOOPBACK) {
5596 			ire->ire_type = IRE_IF_NORESOLVER;
5597 			ire->ire_flags |= RTF_BLACKHOLE;
5598 		}
5599 
5600 		/* src address assigned by the caller? */
5601 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5602 			ire->ire_setsrc_addr = src_addr;
5603 
5604 		nire = ire_add(ire);
5605 		if (nire == NULL) {
5606 			/*
5607 			 * In the result of failure, ire_add() will have
5608 			 * already deleted the ire in question, so there
5609 			 * is no need to do that here.
5610 			 */
5611 			ipif_refrele(ipif);
5612 			return (ENOMEM);
5613 		}
5614 		/*
5615 		 * Check if it was a duplicate entry. This handles
5616 		 * the case of two racing route adds for the same route
5617 		 */
5618 		if (nire != ire) {
5619 			ire_delete(nire);
5620 			ire_refrele(nire);
5621 			ipif_refrele(ipif);
5622 			return (EEXIST);
5623 		}
5624 		ire = nire;
5625 		goto save_ire;
5626 	}
5627 
5628 	/*
5629 	 * Get an interface IRE for the specified gateway.
5630 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5631 	 * gateway, it is currently unreachable and we fail the request
5632 	 * accordingly.
5633 	 * If RTA_IFP was specified we look on that particular ill.
5634 	 */
5635 	if (ill != NULL)
5636 		match_flags |= MATCH_IRE_ILL;
5637 
5638 	/* Check whether the gateway is reachable. */
5639 again:
5640 	type = IRE_INTERFACE;
5641 	if (flags & RTF_INDIRECT)
5642 		type |= IRE_OFFLINK;
5643 
5644 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5645 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5646 	if (gw_ire == NULL) {
5647 		/*
5648 		 * With IPMP, we allow host routes to influence in.mpathd's
5649 		 * target selection.  However, if the test addresses are on
5650 		 * their own network, the above lookup will fail since the
5651 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5652 		 * hidden test IREs to be found and try again.
5653 		 */
5654 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5655 			match_flags |= MATCH_IRE_TESTHIDDEN;
5656 			goto again;
5657 		}
5658 
5659 		if (ipif != NULL)
5660 			ipif_refrele(ipif);
5661 		return (ENETUNREACH);
5662 	}
5663 
5664 	/*
5665 	 * We create one of three types of IREs as a result of this request
5666 	 * based on the netmask.  A netmask of all ones (which is automatically
5667 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5668 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5669 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5670 	 * destination prefix.
5671 	 */
5672 	if (mask == IP_HOST_MASK)
5673 		type = IRE_HOST;
5674 	else if (mask == 0)
5675 		type = IRE_DEFAULT;
5676 	else
5677 		type = IRE_PREFIX;
5678 
5679 	/* check for a duplicate entry */
5680 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5681 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5682 	    0, ipst, NULL);
5683 	if (ire != NULL) {
5684 		if (ipif != NULL)
5685 			ipif_refrele(ipif);
5686 		ire_refrele(gw_ire);
5687 		ire_refrele(ire);
5688 		return (EEXIST);
5689 	}
5690 
5691 	/* Security attribute exists */
5692 	if (sp != NULL) {
5693 		tsol_gcgrp_addr_t ga;
5694 
5695 		/* find or create the gateway credentials group */
5696 		ga.ga_af = AF_INET;
5697 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5698 
5699 		/* we hold reference to it upon success */
5700 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5701 		if (gcgrp == NULL) {
5702 			if (ipif != NULL)
5703 				ipif_refrele(ipif);
5704 			ire_refrele(gw_ire);
5705 			return (ENOMEM);
5706 		}
5707 
5708 		/*
5709 		 * Create and add the security attribute to the group; a
5710 		 * reference to the group is made upon allocating a new
5711 		 * entry successfully.  If it finds an already-existing
5712 		 * entry for the security attribute in the group, it simply
5713 		 * returns it and no new reference is made to the group.
5714 		 */
5715 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5716 		if (gc == NULL) {
5717 			if (ipif != NULL)
5718 				ipif_refrele(ipif);
5719 			/* release reference held by gcgrp_lookup */
5720 			GCGRP_REFRELE(gcgrp);
5721 			ire_refrele(gw_ire);
5722 			return (ENOMEM);
5723 		}
5724 	}
5725 
5726 	/* Create the IRE. */
5727 	ire = ire_create(
5728 	    (uchar_t *)&dst_addr,		/* dest address */
5729 	    (uchar_t *)&mask,			/* mask */
5730 	    (uchar_t *)&gw_addr,		/* gateway address */
5731 	    (ushort_t)type,			/* IRE type */
5732 	    ill,
5733 	    zoneid,
5734 	    flags,
5735 	    gc,					/* security attribute */
5736 	    ipst);
5737 
5738 	/*
5739 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5740 	 * reference to the 'gcgrp'. We can now release the extra reference
5741 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5742 	 */
5743 	if (gcgrp_xtraref)
5744 		GCGRP_REFRELE(gcgrp);
5745 	if (ire == NULL) {
5746 		if (gc != NULL)
5747 			GC_REFRELE(gc);
5748 		if (ipif != NULL)
5749 			ipif_refrele(ipif);
5750 		ire_refrele(gw_ire);
5751 		return (ENOMEM);
5752 	}
5753 
5754 	/* Before we add, check if an extra CGTP broadcast is needed */
5755 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5756 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5757 
5758 	/* src address assigned by the caller? */
5759 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5760 		ire->ire_setsrc_addr = src_addr;
5761 
5762 	/*
5763 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5764 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5765 	 */
5766 
5767 	/* Add the new IRE. */
5768 	nire = ire_add(ire);
5769 	if (nire == NULL) {
5770 		/*
5771 		 * In the result of failure, ire_add() will have
5772 		 * already deleted the ire in question, so there
5773 		 * is no need to do that here.
5774 		 */
5775 		if (ipif != NULL)
5776 			ipif_refrele(ipif);
5777 		ire_refrele(gw_ire);
5778 		return (ENOMEM);
5779 	}
5780 	/*
5781 	 * Check if it was a duplicate entry. This handles
5782 	 * the case of two racing route adds for the same route
5783 	 */
5784 	if (nire != ire) {
5785 		ire_delete(nire);
5786 		ire_refrele(nire);
5787 		if (ipif != NULL)
5788 			ipif_refrele(ipif);
5789 		ire_refrele(gw_ire);
5790 		return (EEXIST);
5791 	}
5792 	ire = nire;
5793 
5794 	if (flags & RTF_MULTIRT) {
5795 		/*
5796 		 * Invoke the CGTP (multirouting) filtering module
5797 		 * to add the dst address in the filtering database.
5798 		 * Replicated inbound packets coming from that address
5799 		 * will be filtered to discard the duplicates.
5800 		 * It is not necessary to call the CGTP filter hook
5801 		 * when the dst address is a broadcast or multicast,
5802 		 * because an IP source address cannot be a broadcast
5803 		 * or a multicast.
5804 		 */
5805 		if (cgtp_broadcast) {
5806 			ip_cgtp_bcast_add(ire, ipst);
5807 			goto save_ire;
5808 		}
5809 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5810 		    !CLASSD(ire->ire_addr)) {
5811 			int res;
5812 			ipif_t *src_ipif;
5813 
5814 			/* Find the source address corresponding to gw_ire */
5815 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5816 			    NULL, zoneid, ipst);
5817 			if (src_ipif != NULL) {
5818 				res = ipst->ips_ip_cgtp_filter_ops->
5819 				    cfo_add_dest_v4(
5820 				    ipst->ips_netstack->netstack_stackid,
5821 				    ire->ire_addr,
5822 				    ire->ire_gateway_addr,
5823 				    ire->ire_setsrc_addr,
5824 				    src_ipif->ipif_lcl_addr);
5825 				ipif_refrele(src_ipif);
5826 			} else {
5827 				res = EADDRNOTAVAIL;
5828 			}
5829 			if (res != 0) {
5830 				if (ipif != NULL)
5831 					ipif_refrele(ipif);
5832 				ire_refrele(gw_ire);
5833 				ire_delete(ire);
5834 				ire_refrele(ire);	/* Held in ire_add */
5835 				return (res);
5836 			}
5837 		}
5838 	}
5839 
5840 save_ire:
5841 	if (gw_ire != NULL) {
5842 		ire_refrele(gw_ire);
5843 		gw_ire = NULL;
5844 	}
5845 	if (ill != NULL) {
5846 		/*
5847 		 * Save enough information so that we can recreate the IRE if
5848 		 * the interface goes down and then up.  The metrics associated
5849 		 * with the route will be saved as well when rts_setmetrics() is
5850 		 * called after the IRE has been created.  In the case where
5851 		 * memory cannot be allocated, none of this information will be
5852 		 * saved.
5853 		 */
5854 		ill_save_ire(ill, ire);
5855 	}
5856 	if (ioctl_msg)
5857 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5858 	if (ire_arg != NULL) {
5859 		/*
5860 		 * Store the ire that was successfully added into where ire_arg
5861 		 * points to so that callers don't have to look it up
5862 		 * themselves (but they are responsible for ire_refrele()ing
5863 		 * the ire when they are finished with it).
5864 		 */
5865 		*ire_arg = ire;
5866 	} else {
5867 		ire_refrele(ire);		/* Held in ire_add */
5868 	}
5869 	if (ipif != NULL)
5870 		ipif_refrele(ipif);
5871 	return (0);
5872 }
5873 
5874 /*
5875  * ip_rt_delete is called to delete an IPv4 route.
5876  * ill is passed in to associate it with the correct interface.
5877  */
5878 /* ARGSUSED4 */
5879 int
5880 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5881     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5882     ip_stack_t *ipst, zoneid_t zoneid)
5883 {
5884 	ire_t	*ire = NULL;
5885 	ipif_t	*ipif;
5886 	uint_t	type;
5887 	uint_t	match_flags = MATCH_IRE_TYPE;
5888 	int	err = 0;
5889 
5890 	ip1dbg(("ip_rt_delete:"));
5891 	/*
5892 	 * If this is the case of RTF_HOST being set, then we set the netmask
5893 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5894 	 */
5895 	if (flags & RTF_HOST) {
5896 		mask = IP_HOST_MASK;
5897 		match_flags |= MATCH_IRE_MASK;
5898 	} else if (rtm_addrs & RTA_NETMASK) {
5899 		match_flags |= MATCH_IRE_MASK;
5900 	}
5901 
5902 	/*
5903 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5904 	 * we check if the gateway address is one of our interfaces first,
5905 	 * and fall back on RTF_GATEWAY routes.
5906 	 *
5907 	 * This makes it possible to delete an original
5908 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5909 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5910 	 * and those can not be deleted here.
5911 	 *
5912 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5913 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5914 	 * otherwise we use the ill derived from the gateway address.
5915 	 * We can always match the gateway address since we record it
5916 	 * in ire_gateway_addr.
5917 	 *
5918 	 * For more detail on specifying routes by gateway address and by
5919 	 * interface index, see the comments in ip_rt_add().
5920 	 */
5921 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5922 	if (ipif != NULL) {
5923 		ill_t	*ill_match;
5924 
5925 		if (ill != NULL)
5926 			ill_match = ill;
5927 		else
5928 			ill_match = ipif->ipif_ill;
5929 
5930 		match_flags |= MATCH_IRE_ILL;
5931 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5932 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5933 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
5934 			    NULL);
5935 		}
5936 		if (ire == NULL) {
5937 			match_flags |= MATCH_IRE_GW;
5938 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5939 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5940 			    match_flags, 0, ipst, NULL);
5941 		}
5942 		/* Avoid deleting routes created by kernel from an ipif */
5943 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5944 			ire_refrele(ire);
5945 			ire = NULL;
5946 		}
5947 
5948 		/* Restore in case we didn't find a match */
5949 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
5950 	}
5951 
5952 	if (ire == NULL) {
5953 		/*
5954 		 * At this point, the gateway address is not one of our own
5955 		 * addresses or a matching interface route was not found.  We
5956 		 * set the IRE type to lookup based on whether
5957 		 * this is a host route, a default route or just a prefix.
5958 		 *
5959 		 * If an ill was passed in, then the lookup is based on an
5960 		 * interface index so MATCH_IRE_ILL is added to match_flags.
5961 		 */
5962 		match_flags |= MATCH_IRE_GW;
5963 		if (ill != NULL)
5964 			match_flags |= MATCH_IRE_ILL;
5965 		if (mask == IP_HOST_MASK)
5966 			type = IRE_HOST;
5967 		else if (mask == 0)
5968 			type = IRE_DEFAULT;
5969 		else
5970 			type = IRE_PREFIX;
5971 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5972 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5973 	}
5974 
5975 	if (ipif != NULL) {
5976 		ipif_refrele(ipif);
5977 		ipif = NULL;
5978 	}
5979 
5980 	if (ire == NULL)
5981 		return (ESRCH);
5982 
5983 	if (ire->ire_flags & RTF_MULTIRT) {
5984 		/*
5985 		 * Invoke the CGTP (multirouting) filtering module
5986 		 * to remove the dst address from the filtering database.
5987 		 * Packets coming from that address will no longer be
5988 		 * filtered to remove duplicates.
5989 		 */
5990 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
5991 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
5992 			    ipst->ips_netstack->netstack_stackid,
5993 			    ire->ire_addr, ire->ire_gateway_addr);
5994 		}
5995 		ip_cgtp_bcast_delete(ire, ipst);
5996 	}
5997 
5998 	ill = ire->ire_ill;
5999 	if (ill != NULL)
6000 		ill_remove_saved_ire(ill, ire);
6001 	if (ioctl_msg)
6002 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6003 	ire_delete(ire);
6004 	ire_refrele(ire);
6005 	return (err);
6006 }
6007 
6008 /*
6009  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6010  */
6011 /* ARGSUSED */
6012 int
6013 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6014     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6015 {
6016 	ipaddr_t dst_addr;
6017 	ipaddr_t gw_addr;
6018 	ipaddr_t mask;
6019 	int error = 0;
6020 	mblk_t *mp1;
6021 	struct rtentry *rt;
6022 	ipif_t *ipif = NULL;
6023 	ip_stack_t	*ipst;
6024 
6025 	ASSERT(q->q_next == NULL);
6026 	ipst = CONNQ_TO_IPST(q);
6027 
6028 	ip1dbg(("ip_siocaddrt:"));
6029 	/* Existence of mp1 verified in ip_wput_nondata */
6030 	mp1 = mp->b_cont->b_cont;
6031 	rt = (struct rtentry *)mp1->b_rptr;
6032 
6033 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6034 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6035 
6036 	/*
6037 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6038 	 * to a particular host address.  In this case, we set the netmask to
6039 	 * all ones for the particular destination address.  Otherwise,
6040 	 * determine the netmask to be used based on dst_addr and the interfaces
6041 	 * in use.
6042 	 */
6043 	if (rt->rt_flags & RTF_HOST) {
6044 		mask = IP_HOST_MASK;
6045 	} else {
6046 		/*
6047 		 * Note that ip_subnet_mask returns a zero mask in the case of
6048 		 * default (an all-zeroes address).
6049 		 */
6050 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6051 	}
6052 
6053 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6054 	    B_TRUE, NULL, ipst, ALL_ZONES);
6055 	if (ipif != NULL)
6056 		ipif_refrele(ipif);
6057 	return (error);
6058 }
6059 
6060 /*
6061  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6062  */
6063 /* ARGSUSED */
6064 int
6065 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6066     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6067 {
6068 	ipaddr_t dst_addr;
6069 	ipaddr_t gw_addr;
6070 	ipaddr_t mask;
6071 	int error;
6072 	mblk_t *mp1;
6073 	struct rtentry *rt;
6074 	ipif_t *ipif = NULL;
6075 	ip_stack_t	*ipst;
6076 
6077 	ASSERT(q->q_next == NULL);
6078 	ipst = CONNQ_TO_IPST(q);
6079 
6080 	ip1dbg(("ip_siocdelrt:"));
6081 	/* Existence of mp1 verified in ip_wput_nondata */
6082 	mp1 = mp->b_cont->b_cont;
6083 	rt = (struct rtentry *)mp1->b_rptr;
6084 
6085 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6086 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6087 
6088 	/*
6089 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6090 	 * to a particular host address.  In this case, we set the netmask to
6091 	 * all ones for the particular destination address.  Otherwise,
6092 	 * determine the netmask to be used based on dst_addr and the interfaces
6093 	 * in use.
6094 	 */
6095 	if (rt->rt_flags & RTF_HOST) {
6096 		mask = IP_HOST_MASK;
6097 	} else {
6098 		/*
6099 		 * Note that ip_subnet_mask returns a zero mask in the case of
6100 		 * default (an all-zeroes address).
6101 		 */
6102 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6103 	}
6104 
6105 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6106 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6107 	    ipst, ALL_ZONES);
6108 	if (ipif != NULL)
6109 		ipif_refrele(ipif);
6110 	return (error);
6111 }
6112 
6113 /*
6114  * Enqueue the mp onto the ipsq, chained by b_next.
6115  * b_prev stores the function to be executed later, and b_queue the queue
6116  * where this mp originated.
6117  */
6118 void
6119 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6120     ill_t *pending_ill)
6121 {
6122 	conn_t	*connp;
6123 	ipxop_t *ipx = ipsq->ipsq_xop;
6124 
6125 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6126 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6127 	ASSERT(func != NULL);
6128 
6129 	mp->b_queue = q;
6130 	mp->b_prev = (void *)func;
6131 	mp->b_next = NULL;
6132 
6133 	switch (type) {
6134 	case CUR_OP:
6135 		if (ipx->ipx_mptail != NULL) {
6136 			ASSERT(ipx->ipx_mphead != NULL);
6137 			ipx->ipx_mptail->b_next = mp;
6138 		} else {
6139 			ASSERT(ipx->ipx_mphead == NULL);
6140 			ipx->ipx_mphead = mp;
6141 		}
6142 		ipx->ipx_mptail = mp;
6143 		break;
6144 
6145 	case NEW_OP:
6146 		if (ipsq->ipsq_xopq_mptail != NULL) {
6147 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6148 			ipsq->ipsq_xopq_mptail->b_next = mp;
6149 		} else {
6150 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6151 			ipsq->ipsq_xopq_mphead = mp;
6152 		}
6153 		ipsq->ipsq_xopq_mptail = mp;
6154 		ipx->ipx_ipsq_queued = B_TRUE;
6155 		break;
6156 
6157 	case SWITCH_OP:
6158 		ASSERT(ipsq->ipsq_swxop != NULL);
6159 		/* only one switch operation is currently allowed */
6160 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6161 		ipsq->ipsq_switch_mp = mp;
6162 		ipx->ipx_ipsq_queued = B_TRUE;
6163 		break;
6164 	default:
6165 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6166 	}
6167 
6168 	if (CONN_Q(q) && pending_ill != NULL) {
6169 		connp = Q_TO_CONN(q);
6170 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6171 		connp->conn_oper_pending_ill = pending_ill;
6172 	}
6173 }
6174 
6175 /*
6176  * Dequeue the next message that requested exclusive access to this IPSQ's
6177  * xop.  Specifically:
6178  *
6179  *  1. If we're still processing the current operation on `ipsq', then
6180  *     dequeue the next message for the operation (from ipx_mphead), or
6181  *     return NULL if there are no queued messages for the operation.
6182  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6183  *
6184  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6185  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6186  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6187  *     leaving IPMP groups and require a careful dance -- see the comments
6188  *     in-line below for details.  If we're leaving a group xop or if we're
6189  *     joining a group xop and become writer on it, then we proceed to (3).
6190  *     Otherwise, we return NULL and exit the xop.
6191  *
6192  *  3. For each IPSQ in the xop, return any switch operation stored on
6193  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6194  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6195  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6196  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6197  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6198  *     each phyint in the group, including the IPMP meta-interface phyint.
6199  */
6200 static mblk_t *
6201 ipsq_dq(ipsq_t *ipsq)
6202 {
6203 	ill_t	*illv4, *illv6;
6204 	mblk_t	*mp;
6205 	ipsq_t	*xopipsq;
6206 	ipsq_t	*leftipsq = NULL;
6207 	ipxop_t *ipx;
6208 	phyint_t *phyi = ipsq->ipsq_phyint;
6209 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6210 	boolean_t emptied = B_FALSE;
6211 
6212 	/*
6213 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6214 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6215 	 */
6216 	rw_enter(&ipst->ips_ill_g_lock,
6217 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6218 	mutex_enter(&ipsq->ipsq_lock);
6219 	ipx = ipsq->ipsq_xop;
6220 	mutex_enter(&ipx->ipx_lock);
6221 
6222 	/*
6223 	 * Dequeue the next message associated with the current exclusive
6224 	 * operation, if any.
6225 	 */
6226 	if ((mp = ipx->ipx_mphead) != NULL) {
6227 		ipx->ipx_mphead = mp->b_next;
6228 		if (ipx->ipx_mphead == NULL)
6229 			ipx->ipx_mptail = NULL;
6230 		mp->b_next = (void *)ipsq;
6231 		goto out;
6232 	}
6233 
6234 	if (ipx->ipx_current_ipif != NULL)
6235 		goto empty;
6236 
6237 	if (ipsq->ipsq_swxop != NULL) {
6238 		/*
6239 		 * The exclusive operation that is now being completed has
6240 		 * requested a switch to a different xop.  This happens
6241 		 * when an interface joins or leaves an IPMP group.  Joins
6242 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6243 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6244 		 * (phyint_free()), or interface plumb for an ill type
6245 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6246 		 *
6247 		 * Xop switches are not allowed on the IPMP meta-interface.
6248 		 */
6249 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6250 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6251 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6252 
6253 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6254 			/*
6255 			 * We're switching back to our own xop, so we have two
6256 			 * xop's to drain/exit: our own, and the group xop
6257 			 * that we are leaving.
6258 			 *
6259 			 * First, pull ourselves out of the group ipsq list.
6260 			 * This is safe since we're writer on ill_g_lock.
6261 			 */
6262 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6263 
6264 			xopipsq = ipx->ipx_ipsq;
6265 			while (xopipsq->ipsq_next != ipsq)
6266 				xopipsq = xopipsq->ipsq_next;
6267 
6268 			xopipsq->ipsq_next = ipsq->ipsq_next;
6269 			ipsq->ipsq_next = ipsq;
6270 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6271 			ipsq->ipsq_swxop = NULL;
6272 
6273 			/*
6274 			 * Second, prepare to exit the group xop.  The actual
6275 			 * ipsq_exit() is done at the end of this function
6276 			 * since we cannot hold any locks across ipsq_exit().
6277 			 * Note that although we drop the group's ipx_lock, no
6278 			 * threads can proceed since we're still ipx_writer.
6279 			 */
6280 			leftipsq = xopipsq;
6281 			mutex_exit(&ipx->ipx_lock);
6282 
6283 			/*
6284 			 * Third, set ipx to point to our own xop (which was
6285 			 * inactive and therefore can be entered).
6286 			 */
6287 			ipx = ipsq->ipsq_xop;
6288 			mutex_enter(&ipx->ipx_lock);
6289 			ASSERT(ipx->ipx_writer == NULL);
6290 			ASSERT(ipx->ipx_current_ipif == NULL);
6291 		} else {
6292 			/*
6293 			 * We're switching from our own xop to a group xop.
6294 			 * The requestor of the switch must ensure that the
6295 			 * group xop cannot go away (e.g. by ensuring the
6296 			 * phyint associated with the xop cannot go away).
6297 			 *
6298 			 * If we can become writer on our new xop, then we'll
6299 			 * do the drain.  Otherwise, the current writer of our
6300 			 * new xop will do the drain when it exits.
6301 			 *
6302 			 * First, splice ourselves into the group IPSQ list.
6303 			 * This is safe since we're writer on ill_g_lock.
6304 			 */
6305 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6306 
6307 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6308 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6309 				xopipsq = xopipsq->ipsq_next;
6310 
6311 			xopipsq->ipsq_next = ipsq;
6312 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6313 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6314 			ipsq->ipsq_swxop = NULL;
6315 
6316 			/*
6317 			 * Second, exit our own xop, since it's now unused.
6318 			 * This is safe since we've got the only reference.
6319 			 */
6320 			ASSERT(ipx->ipx_writer == curthread);
6321 			ipx->ipx_writer = NULL;
6322 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6323 			ipx->ipx_ipsq_queued = B_FALSE;
6324 			mutex_exit(&ipx->ipx_lock);
6325 
6326 			/*
6327 			 * Third, set ipx to point to our new xop, and check
6328 			 * if we can become writer on it.  If we cannot, then
6329 			 * the current writer will drain the IPSQ group when
6330 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6331 			 * because we're still holding ipsq_lock.
6332 			 */
6333 			ipx = ipsq->ipsq_xop;
6334 			mutex_enter(&ipx->ipx_lock);
6335 			if (ipx->ipx_writer != NULL ||
6336 			    ipx->ipx_current_ipif != NULL) {
6337 				goto out;
6338 			}
6339 		}
6340 
6341 		/*
6342 		 * Fourth, become writer on our new ipx before we continue
6343 		 * with the drain.  Note that we never dropped ipsq_lock
6344 		 * above, so no other thread could've raced with us to
6345 		 * become writer first.  Also, we're holding ipx_lock, so
6346 		 * no other thread can examine the ipx right now.
6347 		 */
6348 		ASSERT(ipx->ipx_current_ipif == NULL);
6349 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6350 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6351 		ipx->ipx_writer = curthread;
6352 		ipx->ipx_forced = B_FALSE;
6353 #ifdef DEBUG
6354 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6355 #endif
6356 	}
6357 
6358 	xopipsq = ipsq;
6359 	do {
6360 		/*
6361 		 * So that other operations operate on a consistent and
6362 		 * complete phyint, a switch message on an IPSQ must be
6363 		 * handled prior to any other operations on that IPSQ.
6364 		 */
6365 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6366 			xopipsq->ipsq_switch_mp = NULL;
6367 			ASSERT(mp->b_next == NULL);
6368 			mp->b_next = (void *)xopipsq;
6369 			goto out;
6370 		}
6371 
6372 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6373 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6374 			if (xopipsq->ipsq_xopq_mphead == NULL)
6375 				xopipsq->ipsq_xopq_mptail = NULL;
6376 			mp->b_next = (void *)xopipsq;
6377 			goto out;
6378 		}
6379 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6380 empty:
6381 	/*
6382 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6383 	 * new messages can end up on any IPSQ in the xop.
6384 	 */
6385 	ipx->ipx_writer = NULL;
6386 	ipx->ipx_forced = B_FALSE;
6387 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6388 	ipx->ipx_ipsq_queued = B_FALSE;
6389 	emptied = B_TRUE;
6390 #ifdef	DEBUG
6391 	ipx->ipx_depth = 0;
6392 #endif
6393 out:
6394 	mutex_exit(&ipx->ipx_lock);
6395 	mutex_exit(&ipsq->ipsq_lock);
6396 
6397 	/*
6398 	 * If we completely emptied the xop, then wake up any threads waiting
6399 	 * to enter any of the IPSQ's associated with it.
6400 	 */
6401 	if (emptied) {
6402 		xopipsq = ipsq;
6403 		do {
6404 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6405 				continue;
6406 
6407 			illv4 = phyi->phyint_illv4;
6408 			illv6 = phyi->phyint_illv6;
6409 
6410 			GRAB_ILL_LOCKS(illv4, illv6);
6411 			if (illv4 != NULL)
6412 				cv_broadcast(&illv4->ill_cv);
6413 			if (illv6 != NULL)
6414 				cv_broadcast(&illv6->ill_cv);
6415 			RELEASE_ILL_LOCKS(illv4, illv6);
6416 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6417 	}
6418 	rw_exit(&ipst->ips_ill_g_lock);
6419 
6420 	/*
6421 	 * Now that all locks are dropped, exit the IPSQ we left.
6422 	 */
6423 	if (leftipsq != NULL)
6424 		ipsq_exit(leftipsq);
6425 
6426 	return (mp);
6427 }
6428 
6429 /*
6430  * Return completion status of previously initiated DLPI operations on
6431  * ills in the purview of an ipsq.
6432  */
6433 static boolean_t
6434 ipsq_dlpi_done(ipsq_t *ipsq)
6435 {
6436 	ipsq_t		*ipsq_start;
6437 	phyint_t	*phyi;
6438 	ill_t		*ill;
6439 
6440 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6441 	ipsq_start = ipsq;
6442 
6443 	do {
6444 		/*
6445 		 * The only current users of this function are ipsq_try_enter
6446 		 * and ipsq_enter which have made sure that ipsq_writer is
6447 		 * NULL before we reach here. ill_dlpi_pending is modified
6448 		 * only by an ipsq writer
6449 		 */
6450 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6451 		phyi = ipsq->ipsq_phyint;
6452 		/*
6453 		 * phyi could be NULL if a phyint that is part of an
6454 		 * IPMP group is being unplumbed. A more detailed
6455 		 * comment is in ipmp_grp_update_kstats()
6456 		 */
6457 		if (phyi != NULL) {
6458 			ill = phyi->phyint_illv4;
6459 			if (ill != NULL &&
6460 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6461 			    ill->ill_arl_dlpi_pending))
6462 				return (B_FALSE);
6463 
6464 			ill = phyi->phyint_illv6;
6465 			if (ill != NULL &&
6466 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6467 				return (B_FALSE);
6468 		}
6469 
6470 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6471 
6472 	return (B_TRUE);
6473 }
6474 
6475 /*
6476  * Enter the ipsq corresponding to ill, by waiting synchronously till
6477  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6478  * will have to drain completely before ipsq_enter returns success.
6479  * ipx_current_ipif will be set if some exclusive op is in progress,
6480  * and the ipsq_exit logic will start the next enqueued op after
6481  * completion of the current op. If 'force' is used, we don't wait
6482  * for the enqueued ops. This is needed when a conn_close wants to
6483  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6484  * of an ill can also use this option. But we dont' use it currently.
6485  */
6486 #define	ENTER_SQ_WAIT_TICKS 100
6487 boolean_t
6488 ipsq_enter(ill_t *ill, boolean_t force, int type)
6489 {
6490 	ipsq_t	*ipsq;
6491 	ipxop_t *ipx;
6492 	boolean_t waited_enough = B_FALSE;
6493 	ip_stack_t *ipst = ill->ill_ipst;
6494 
6495 	/*
6496 	 * Note that the relationship between ill and ipsq is fixed as long as
6497 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6498 	 * relationship between the IPSQ and xop cannot change.  However,
6499 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6500 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6501 	 * waking up all ills in the xop when it becomes available.
6502 	 */
6503 	for (;;) {
6504 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6505 		mutex_enter(&ill->ill_lock);
6506 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6507 			mutex_exit(&ill->ill_lock);
6508 			rw_exit(&ipst->ips_ill_g_lock);
6509 			return (B_FALSE);
6510 		}
6511 
6512 		ipsq = ill->ill_phyint->phyint_ipsq;
6513 		mutex_enter(&ipsq->ipsq_lock);
6514 		ipx = ipsq->ipsq_xop;
6515 		mutex_enter(&ipx->ipx_lock);
6516 
6517 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6518 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6519 		    waited_enough))
6520 			break;
6521 
6522 		rw_exit(&ipst->ips_ill_g_lock);
6523 
6524 		if (!force || ipx->ipx_writer != NULL) {
6525 			mutex_exit(&ipx->ipx_lock);
6526 			mutex_exit(&ipsq->ipsq_lock);
6527 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6528 		} else {
6529 			mutex_exit(&ipx->ipx_lock);
6530 			mutex_exit(&ipsq->ipsq_lock);
6531 			(void) cv_timedwait(&ill->ill_cv,
6532 			    &ill->ill_lock, lbolt + ENTER_SQ_WAIT_TICKS);
6533 			waited_enough = B_TRUE;
6534 		}
6535 		mutex_exit(&ill->ill_lock);
6536 	}
6537 
6538 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6539 	ASSERT(ipx->ipx_reentry_cnt == 0);
6540 	ipx->ipx_writer = curthread;
6541 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6542 	ipx->ipx_reentry_cnt++;
6543 #ifdef DEBUG
6544 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6545 #endif
6546 	mutex_exit(&ipx->ipx_lock);
6547 	mutex_exit(&ipsq->ipsq_lock);
6548 	mutex_exit(&ill->ill_lock);
6549 	rw_exit(&ipst->ips_ill_g_lock);
6550 
6551 	return (B_TRUE);
6552 }
6553 
6554 /*
6555  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6556  * across the call to the core interface ipsq_try_enter() and hence calls this
6557  * function directly. This is explained more fully in ipif_set_values().
6558  * In order to support the above constraint, ipsq_try_enter is implemented as
6559  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6560  */
6561 static ipsq_t *
6562 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6563     int type, boolean_t reentry_ok)
6564 {
6565 	ipsq_t	*ipsq;
6566 	ipxop_t	*ipx;
6567 	ip_stack_t *ipst = ill->ill_ipst;
6568 
6569 	/*
6570 	 * lock ordering:
6571 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6572 	 *
6573 	 * ipx of an ipsq can't change when ipsq_lock is held.
6574 	 */
6575 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6576 	GRAB_CONN_LOCK(q);
6577 	mutex_enter(&ill->ill_lock);
6578 	ipsq = ill->ill_phyint->phyint_ipsq;
6579 	mutex_enter(&ipsq->ipsq_lock);
6580 	ipx = ipsq->ipsq_xop;
6581 	mutex_enter(&ipx->ipx_lock);
6582 
6583 	/*
6584 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6585 	 *    (Note: If the caller does not specify reentry_ok then neither
6586 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6587 	 *    again. Otherwise it can lead to an infinite loop
6588 	 * 2. Enter the ipsq if there is no current writer and this attempted
6589 	 *    entry is part of the current operation
6590 	 * 3. Enter the ipsq if there is no current writer and this is a new
6591 	 *    operation and the operation queue is empty and there is no
6592 	 *    operation currently in progress and if all previously initiated
6593 	 *    DLPI operations have completed.
6594 	 */
6595 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6596 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6597 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6598 	    ipsq_dlpi_done(ipsq))))) {
6599 		/* Success. */
6600 		ipx->ipx_reentry_cnt++;
6601 		ipx->ipx_writer = curthread;
6602 		ipx->ipx_forced = B_FALSE;
6603 		mutex_exit(&ipx->ipx_lock);
6604 		mutex_exit(&ipsq->ipsq_lock);
6605 		mutex_exit(&ill->ill_lock);
6606 		RELEASE_CONN_LOCK(q);
6607 #ifdef DEBUG
6608 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6609 #endif
6610 		return (ipsq);
6611 	}
6612 
6613 	if (func != NULL)
6614 		ipsq_enq(ipsq, q, mp, func, type, ill);
6615 
6616 	mutex_exit(&ipx->ipx_lock);
6617 	mutex_exit(&ipsq->ipsq_lock);
6618 	mutex_exit(&ill->ill_lock);
6619 	RELEASE_CONN_LOCK(q);
6620 	return (NULL);
6621 }
6622 
6623 /*
6624  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6625  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6626  * There is one ipsq per phyint. The ipsq
6627  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6628  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6629  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6630  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6631  * up the interface) and are enqueued in ipx_mphead.
6632  *
6633  * If a thread does not want to reenter the ipsq when it is already writer,
6634  * it must make sure that the specified reentry point to be called later
6635  * when the ipsq is empty, nor any code path starting from the specified reentry
6636  * point must never ever try to enter the ipsq again. Otherwise it can lead
6637  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6638  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6639  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6640  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6641  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6642  * ioctl if the current ioctl has completed. If the current ioctl is still
6643  * in progress it simply returns. The current ioctl could be waiting for
6644  * a response from another module (the driver or could be waiting for
6645  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6646  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6647  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6648  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6649  * all associated DLPI operations have completed.
6650  */
6651 
6652 /*
6653  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6654  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6655  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6656  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6657  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6658  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6659  */
6660 ipsq_t *
6661 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6662     ipsq_func_t func, int type, boolean_t reentry_ok)
6663 {
6664 	ip_stack_t	*ipst;
6665 	ipsq_t		*ipsq;
6666 
6667 	/* Only 1 of ipif or ill can be specified */
6668 	ASSERT((ipif != NULL) ^ (ill != NULL));
6669 
6670 	if (ipif != NULL)
6671 		ill = ipif->ipif_ill;
6672 	ipst = ill->ill_ipst;
6673 
6674 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6675 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6676 	rw_exit(&ipst->ips_ill_g_lock);
6677 
6678 	return (ipsq);
6679 }
6680 
6681 /*
6682  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6683  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6684  * cannot be entered, the mp is queued for completion.
6685  */
6686 void
6687 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6688     boolean_t reentry_ok)
6689 {
6690 	ipsq_t	*ipsq;
6691 
6692 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6693 
6694 	/*
6695 	 * Drop the caller's refhold on the ill.  This is safe since we either
6696 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6697 	 * IPSQ, in which case we return without accessing ill anymore.  This
6698 	 * is needed because func needs to see the correct refcount.
6699 	 * e.g. removeif can work only then.
6700 	 */
6701 	ill_refrele(ill);
6702 	if (ipsq != NULL) {
6703 		(*func)(ipsq, q, mp, NULL);
6704 		ipsq_exit(ipsq);
6705 	}
6706 }
6707 
6708 /*
6709  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6710  * prior to exiting.  Caller must be writer on the specified IPSQ.
6711  */
6712 void
6713 ipsq_exit(ipsq_t *ipsq)
6714 {
6715 	mblk_t *mp;
6716 	ipsq_t *mp_ipsq;
6717 	queue_t	*q;
6718 	phyint_t *phyi;
6719 	ipsq_func_t func;
6720 
6721 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6722 
6723 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6724 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6725 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6726 		return;
6727 	}
6728 
6729 	for (;;) {
6730 		phyi = ipsq->ipsq_phyint;
6731 		mp = ipsq_dq(ipsq);
6732 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6733 
6734 		/*
6735 		 * If we've changed to a new IPSQ, and the phyint associated
6736 		 * with the old one has gone away, free the old IPSQ.  Note
6737 		 * that this cannot happen while the IPSQ is in a group.
6738 		 */
6739 		if (mp_ipsq != ipsq && phyi == NULL) {
6740 			ASSERT(ipsq->ipsq_next == ipsq);
6741 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6742 			ipsq_delete(ipsq);
6743 		}
6744 
6745 		if (mp == NULL)
6746 			break;
6747 
6748 		q = mp->b_queue;
6749 		func = (ipsq_func_t)mp->b_prev;
6750 		ipsq = mp_ipsq;
6751 		mp->b_next = mp->b_prev = NULL;
6752 		mp->b_queue = NULL;
6753 
6754 		/*
6755 		 * If 'q' is an conn queue, it is valid, since we did a
6756 		 * a refhold on the conn at the start of the ioctl.
6757 		 * If 'q' is an ill queue, it is valid, since close of an
6758 		 * ill will clean up its IPSQ.
6759 		 */
6760 		(*func)(ipsq, q, mp, NULL);
6761 	}
6762 }
6763 
6764 /*
6765  * Used to start any igmp or mld timers that could not be started
6766  * while holding ill_mcast_lock. The timers can't be started while holding
6767  * the lock, since mld/igmp_start_timers may need to call untimeout()
6768  * which can't be done while holding the lock which the timeout handler
6769  * acquires. Otherwise
6770  * there could be a deadlock since the timeout handlers
6771  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6772  * ill_mcast_lock.
6773  */
6774 void
6775 ill_mcast_timer_start(ip_stack_t *ipst)
6776 {
6777 	int		next;
6778 
6779 	mutex_enter(&ipst->ips_igmp_timer_lock);
6780 	next = ipst->ips_igmp_deferred_next;
6781 	ipst->ips_igmp_deferred_next = INFINITY;
6782 	mutex_exit(&ipst->ips_igmp_timer_lock);
6783 
6784 	if (next != INFINITY)
6785 		igmp_start_timers(next, ipst);
6786 
6787 	mutex_enter(&ipst->ips_mld_timer_lock);
6788 	next = ipst->ips_mld_deferred_next;
6789 	ipst->ips_mld_deferred_next = INFINITY;
6790 	mutex_exit(&ipst->ips_mld_timer_lock);
6791 
6792 	if (next != INFINITY)
6793 		mld_start_timers(next, ipst);
6794 }
6795 
6796 /*
6797  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6798  * and `ioccmd'.
6799  */
6800 void
6801 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6802 {
6803 	ill_t *ill = ipif->ipif_ill;
6804 	ipxop_t *ipx = ipsq->ipsq_xop;
6805 
6806 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6807 	ASSERT(ipx->ipx_current_ipif == NULL);
6808 	ASSERT(ipx->ipx_current_ioctl == 0);
6809 
6810 	ipx->ipx_current_done = B_FALSE;
6811 	ipx->ipx_current_ioctl = ioccmd;
6812 	mutex_enter(&ipx->ipx_lock);
6813 	ipx->ipx_current_ipif = ipif;
6814 	mutex_exit(&ipx->ipx_lock);
6815 
6816 	/*
6817 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6818 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6819 	 * references to the ipif (so that the references will eventually
6820 	 * drop to zero) and also prevents any "get" operations (e.g.,
6821 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6822 	 * operation has completed and the ipif is again in a stable state.
6823 	 *
6824 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6825 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6826 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6827 	 * ipifs will be affected.
6828 	 *
6829 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6830 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6831 	 * operate on.
6832 	 */
6833 	switch (ioccmd) {
6834 	case SIOCLIFREMOVEIF:
6835 		break;
6836 	case 0:
6837 		mutex_enter(&ill->ill_lock);
6838 		ipif = ipif->ipif_ill->ill_ipif;
6839 		for (; ipif != NULL; ipif = ipif->ipif_next)
6840 			ipif->ipif_state_flags |= IPIF_CHANGING;
6841 		mutex_exit(&ill->ill_lock);
6842 		break;
6843 	default:
6844 		mutex_enter(&ill->ill_lock);
6845 		ipif->ipif_state_flags |= IPIF_CHANGING;
6846 		mutex_exit(&ill->ill_lock);
6847 	}
6848 }
6849 
6850 /*
6851  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6852  * the next exclusive operation to begin once we ipsq_exit().  However, if
6853  * pending DLPI operations remain, then we will wait for the queue to drain
6854  * before allowing the next exclusive operation to begin.  This ensures that
6855  * DLPI operations from one exclusive operation are never improperly processed
6856  * as part of a subsequent exclusive operation.
6857  */
6858 void
6859 ipsq_current_finish(ipsq_t *ipsq)
6860 {
6861 	ipxop_t	*ipx = ipsq->ipsq_xop;
6862 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6863 	ipif_t	*ipif = ipx->ipx_current_ipif;
6864 
6865 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6866 
6867 	/*
6868 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6869 	 * (but in that case, IPIF_CHANGING will already be clear and no
6870 	 * pending DLPI messages can remain).
6871 	 */
6872 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6873 		ill_t *ill = ipif->ipif_ill;
6874 
6875 		mutex_enter(&ill->ill_lock);
6876 		dlpi_pending = ill->ill_dlpi_pending;
6877 		if (ipx->ipx_current_ioctl == 0) {
6878 			ipif = ill->ill_ipif;
6879 			for (; ipif != NULL; ipif = ipif->ipif_next)
6880 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6881 		} else {
6882 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6883 		}
6884 		mutex_exit(&ill->ill_lock);
6885 	}
6886 
6887 	ASSERT(!ipx->ipx_current_done);
6888 	ipx->ipx_current_done = B_TRUE;
6889 	ipx->ipx_current_ioctl = 0;
6890 	if (dlpi_pending == DL_PRIM_INVAL) {
6891 		mutex_enter(&ipx->ipx_lock);
6892 		ipx->ipx_current_ipif = NULL;
6893 		mutex_exit(&ipx->ipx_lock);
6894 	}
6895 }
6896 
6897 /*
6898  * The ill is closing. Flush all messages on the ipsq that originated
6899  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6900  * for this ill since ipsq_enter could not have entered until then.
6901  * New messages can't be queued since the CONDEMNED flag is set.
6902  */
6903 static void
6904 ipsq_flush(ill_t *ill)
6905 {
6906 	queue_t	*q;
6907 	mblk_t	*prev;
6908 	mblk_t	*mp;
6909 	mblk_t	*mp_next;
6910 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6911 
6912 	ASSERT(IAM_WRITER_ILL(ill));
6913 
6914 	/*
6915 	 * Flush any messages sent up by the driver.
6916 	 */
6917 	mutex_enter(&ipx->ipx_lock);
6918 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6919 		mp_next = mp->b_next;
6920 		q = mp->b_queue;
6921 		if (q == ill->ill_rq || q == ill->ill_wq) {
6922 			/* dequeue mp */
6923 			if (prev == NULL)
6924 				ipx->ipx_mphead = mp->b_next;
6925 			else
6926 				prev->b_next = mp->b_next;
6927 			if (ipx->ipx_mptail == mp) {
6928 				ASSERT(mp_next == NULL);
6929 				ipx->ipx_mptail = prev;
6930 			}
6931 			inet_freemsg(mp);
6932 		} else {
6933 			prev = mp;
6934 		}
6935 	}
6936 	mutex_exit(&ipx->ipx_lock);
6937 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6938 	ipsq_xopq_mp_cleanup(ill, NULL);
6939 }
6940 
6941 /*
6942  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6943  * and return the associated ipif.
6944  * Return value:
6945  *	Non zero: An error has occurred. ci may not be filled out.
6946  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6947  *	a held ipif in ci.ci_ipif.
6948  */
6949 int
6950 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
6951     cmd_info_t *ci)
6952 {
6953 	char		*name;
6954 	struct ifreq    *ifr;
6955 	struct lifreq    *lifr;
6956 	ipif_t		*ipif = NULL;
6957 	ill_t		*ill;
6958 	conn_t		*connp;
6959 	boolean_t	isv6;
6960 	boolean_t	exists;
6961 	mblk_t		*mp1;
6962 	zoneid_t	zoneid;
6963 	ip_stack_t	*ipst;
6964 
6965 	if (q->q_next != NULL) {
6966 		ill = (ill_t *)q->q_ptr;
6967 		isv6 = ill->ill_isv6;
6968 		connp = NULL;
6969 		zoneid = ALL_ZONES;
6970 		ipst = ill->ill_ipst;
6971 	} else {
6972 		ill = NULL;
6973 		connp = Q_TO_CONN(q);
6974 		isv6 = (connp->conn_family == AF_INET6);
6975 		zoneid = connp->conn_zoneid;
6976 		if (zoneid == GLOBAL_ZONEID) {
6977 			/* global zone can access ipifs in all zones */
6978 			zoneid = ALL_ZONES;
6979 		}
6980 		ipst = connp->conn_netstack->netstack_ip;
6981 	}
6982 
6983 	/* Has been checked in ip_wput_nondata */
6984 	mp1 = mp->b_cont->b_cont;
6985 
6986 	if (ipip->ipi_cmd_type == IF_CMD) {
6987 		/* This a old style SIOC[GS]IF* command */
6988 		ifr = (struct ifreq *)mp1->b_rptr;
6989 		/*
6990 		 * Null terminate the string to protect against buffer
6991 		 * overrun. String was generated by user code and may not
6992 		 * be trusted.
6993 		 */
6994 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
6995 		name = ifr->ifr_name;
6996 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
6997 		ci->ci_sin6 = NULL;
6998 		ci->ci_lifr = (struct lifreq *)ifr;
6999 	} else {
7000 		/* This a new style SIOC[GS]LIF* command */
7001 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7002 		lifr = (struct lifreq *)mp1->b_rptr;
7003 		/*
7004 		 * Null terminate the string to protect against buffer
7005 		 * overrun. String was generated by user code and may not
7006 		 * be trusted.
7007 		 */
7008 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7009 		name = lifr->lifr_name;
7010 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7011 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7012 		ci->ci_lifr = lifr;
7013 	}
7014 
7015 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7016 		/*
7017 		 * The ioctl will be failed if the ioctl comes down
7018 		 * an conn stream
7019 		 */
7020 		if (ill == NULL) {
7021 			/*
7022 			 * Not an ill queue, return EINVAL same as the
7023 			 * old error code.
7024 			 */
7025 			return (ENXIO);
7026 		}
7027 		ipif = ill->ill_ipif;
7028 		ipif_refhold(ipif);
7029 	} else {
7030 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7031 		    &exists, isv6, zoneid, ipst);
7032 
7033 		/*
7034 		 * Ensure that get ioctls don't see any internal state changes
7035 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7036 		 * set.
7037 		 */
7038 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7039 		    !IAM_WRITER_IPIF(ipif)) {
7040 			ipsq_t	*ipsq;
7041 
7042 			if (connp != NULL)
7043 				mutex_enter(&connp->conn_lock);
7044 			mutex_enter(&ipif->ipif_ill->ill_lock);
7045 			if (IPIF_IS_CHANGING(ipif) &&
7046 			    !IPIF_IS_CONDEMNED(ipif)) {
7047 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7048 				mutex_enter(&ipsq->ipsq_lock);
7049 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7050 				mutex_exit(&ipif->ipif_ill->ill_lock);
7051 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7052 				    NEW_OP, ipif->ipif_ill);
7053 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7054 				mutex_exit(&ipsq->ipsq_lock);
7055 				if (connp != NULL)
7056 					mutex_exit(&connp->conn_lock);
7057 				ipif_refrele(ipif);
7058 				return (EINPROGRESS);
7059 			}
7060 			mutex_exit(&ipif->ipif_ill->ill_lock);
7061 			if (connp != NULL)
7062 				mutex_exit(&connp->conn_lock);
7063 		}
7064 	}
7065 
7066 	/*
7067 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7068 	 */
7069 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7070 		ipif_refrele(ipif);
7071 		return (ENXIO);
7072 	}
7073 
7074 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7075 	    name[0] == '\0') {
7076 		/*
7077 		 * Handle a or a SIOC?IF* with a null name
7078 		 * during plumb (on the ill queue before the I_PLINK).
7079 		 */
7080 		ipif = ill->ill_ipif;
7081 		ipif_refhold(ipif);
7082 	}
7083 
7084 	if (ipif == NULL)
7085 		return (ENXIO);
7086 
7087 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7088 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7089 
7090 	ci->ci_ipif = ipif;
7091 	return (0);
7092 }
7093 
7094 /*
7095  * Return the total number of ipifs.
7096  */
7097 static uint_t
7098 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7099 {
7100 	uint_t numifs = 0;
7101 	ill_t	*ill;
7102 	ill_walk_context_t	ctx;
7103 	ipif_t	*ipif;
7104 
7105 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7106 	ill = ILL_START_WALK_V4(&ctx, ipst);
7107 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7108 		if (IS_UNDER_IPMP(ill))
7109 			continue;
7110 		for (ipif = ill->ill_ipif; ipif != NULL;
7111 		    ipif = ipif->ipif_next) {
7112 			if (ipif->ipif_zoneid == zoneid ||
7113 			    ipif->ipif_zoneid == ALL_ZONES)
7114 				numifs++;
7115 		}
7116 	}
7117 	rw_exit(&ipst->ips_ill_g_lock);
7118 	return (numifs);
7119 }
7120 
7121 /*
7122  * Return the total number of ipifs.
7123  */
7124 static uint_t
7125 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7126 {
7127 	uint_t numifs = 0;
7128 	ill_t	*ill;
7129 	ipif_t	*ipif;
7130 	ill_walk_context_t	ctx;
7131 
7132 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7133 
7134 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7135 	if (family == AF_INET)
7136 		ill = ILL_START_WALK_V4(&ctx, ipst);
7137 	else if (family == AF_INET6)
7138 		ill = ILL_START_WALK_V6(&ctx, ipst);
7139 	else
7140 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7141 
7142 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7143 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7144 			continue;
7145 
7146 		for (ipif = ill->ill_ipif; ipif != NULL;
7147 		    ipif = ipif->ipif_next) {
7148 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7149 			    !(lifn_flags & LIFC_NOXMIT))
7150 				continue;
7151 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7152 			    !(lifn_flags & LIFC_TEMPORARY))
7153 				continue;
7154 			if (((ipif->ipif_flags &
7155 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7156 			    IPIF_DEPRECATED)) ||
7157 			    IS_LOOPBACK(ill) ||
7158 			    !(ipif->ipif_flags & IPIF_UP)) &&
7159 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7160 				continue;
7161 
7162 			if (zoneid != ipif->ipif_zoneid &&
7163 			    ipif->ipif_zoneid != ALL_ZONES &&
7164 			    (zoneid != GLOBAL_ZONEID ||
7165 			    !(lifn_flags & LIFC_ALLZONES)))
7166 				continue;
7167 
7168 			numifs++;
7169 		}
7170 	}
7171 	rw_exit(&ipst->ips_ill_g_lock);
7172 	return (numifs);
7173 }
7174 
7175 uint_t
7176 ip_get_lifsrcofnum(ill_t *ill)
7177 {
7178 	uint_t numifs = 0;
7179 	ill_t	*ill_head = ill;
7180 	ip_stack_t	*ipst = ill->ill_ipst;
7181 
7182 	/*
7183 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7184 	 * other thread may be trying to relink the ILLs in this usesrc group
7185 	 * and adjusting the ill_usesrc_grp_next pointers
7186 	 */
7187 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7188 	if ((ill->ill_usesrc_ifindex == 0) &&
7189 	    (ill->ill_usesrc_grp_next != NULL)) {
7190 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7191 		    ill = ill->ill_usesrc_grp_next)
7192 			numifs++;
7193 	}
7194 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7195 
7196 	return (numifs);
7197 }
7198 
7199 /* Null values are passed in for ipif, sin, and ifreq */
7200 /* ARGSUSED */
7201 int
7202 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7203     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7204 {
7205 	int *nump;
7206 	conn_t *connp = Q_TO_CONN(q);
7207 
7208 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7209 
7210 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7211 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7212 
7213 	*nump = ip_get_numifs(connp->conn_zoneid,
7214 	    connp->conn_netstack->netstack_ip);
7215 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7216 	return (0);
7217 }
7218 
7219 /* Null values are passed in for ipif, sin, and ifreq */
7220 /* ARGSUSED */
7221 int
7222 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7223     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7224 {
7225 	struct lifnum *lifn;
7226 	mblk_t	*mp1;
7227 	conn_t *connp = Q_TO_CONN(q);
7228 
7229 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7230 
7231 	/* Existence checked in ip_wput_nondata */
7232 	mp1 = mp->b_cont->b_cont;
7233 
7234 	lifn = (struct lifnum *)mp1->b_rptr;
7235 	switch (lifn->lifn_family) {
7236 	case AF_UNSPEC:
7237 	case AF_INET:
7238 	case AF_INET6:
7239 		break;
7240 	default:
7241 		return (EAFNOSUPPORT);
7242 	}
7243 
7244 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7245 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7246 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7247 	return (0);
7248 }
7249 
7250 /* ARGSUSED */
7251 int
7252 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7253     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7254 {
7255 	STRUCT_HANDLE(ifconf, ifc);
7256 	mblk_t *mp1;
7257 	struct iocblk *iocp;
7258 	struct ifreq *ifr;
7259 	ill_walk_context_t	ctx;
7260 	ill_t	*ill;
7261 	ipif_t	*ipif;
7262 	struct sockaddr_in *sin;
7263 	int32_t	ifclen;
7264 	zoneid_t zoneid;
7265 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7266 
7267 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7268 
7269 	ip1dbg(("ip_sioctl_get_ifconf"));
7270 	/* Existence verified in ip_wput_nondata */
7271 	mp1 = mp->b_cont->b_cont;
7272 	iocp = (struct iocblk *)mp->b_rptr;
7273 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7274 
7275 	/*
7276 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7277 	 * the user buffer address and length into which the list of struct
7278 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7279 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7280 	 * the SIOCGIFCONF operation was redefined to simply provide
7281 	 * a large output buffer into which we are supposed to jam the ifreq
7282 	 * array.  The same ioctl command code was used, despite the fact that
7283 	 * both the applications and the kernel code had to change, thus making
7284 	 * it impossible to support both interfaces.
7285 	 *
7286 	 * For reasons not good enough to try to explain, the following
7287 	 * algorithm is used for deciding what to do with one of these:
7288 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7289 	 * form with the output buffer coming down as the continuation message.
7290 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7291 	 * and we have to copy in the ifconf structure to find out how big the
7292 	 * output buffer is and where to copy out to.  Sure no problem...
7293 	 *
7294 	 */
7295 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7296 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7297 		int numifs = 0;
7298 		size_t ifc_bufsize;
7299 
7300 		/*
7301 		 * Must be (better be!) continuation of a TRANSPARENT
7302 		 * IOCTL.  We just copied in the ifconf structure.
7303 		 */
7304 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7305 		    (struct ifconf *)mp1->b_rptr);
7306 
7307 		/*
7308 		 * Allocate a buffer to hold requested information.
7309 		 *
7310 		 * If ifc_len is larger than what is needed, we only
7311 		 * allocate what we will use.
7312 		 *
7313 		 * If ifc_len is smaller than what is needed, return
7314 		 * EINVAL.
7315 		 *
7316 		 * XXX: the ill_t structure can hava 2 counters, for
7317 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7318 		 * number of interfaces for a device, so we don't need
7319 		 * to count them here...
7320 		 */
7321 		numifs = ip_get_numifs(zoneid, ipst);
7322 
7323 		ifclen = STRUCT_FGET(ifc, ifc_len);
7324 		ifc_bufsize = numifs * sizeof (struct ifreq);
7325 		if (ifc_bufsize > ifclen) {
7326 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7327 				/* old behaviour */
7328 				return (EINVAL);
7329 			} else {
7330 				ifc_bufsize = ifclen;
7331 			}
7332 		}
7333 
7334 		mp1 = mi_copyout_alloc(q, mp,
7335 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7336 		if (mp1 == NULL)
7337 			return (ENOMEM);
7338 
7339 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7340 	}
7341 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7342 	/*
7343 	 * the SIOCGIFCONF ioctl only knows about
7344 	 * IPv4 addresses, so don't try to tell
7345 	 * it about interfaces with IPv6-only
7346 	 * addresses. (Last parm 'isv6' is B_FALSE)
7347 	 */
7348 
7349 	ifr = (struct ifreq *)mp1->b_rptr;
7350 
7351 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7352 	ill = ILL_START_WALK_V4(&ctx, ipst);
7353 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7354 		if (IS_UNDER_IPMP(ill))
7355 			continue;
7356 		for (ipif = ill->ill_ipif; ipif != NULL;
7357 		    ipif = ipif->ipif_next) {
7358 			if (zoneid != ipif->ipif_zoneid &&
7359 			    ipif->ipif_zoneid != ALL_ZONES)
7360 				continue;
7361 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7362 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7363 					/* old behaviour */
7364 					rw_exit(&ipst->ips_ill_g_lock);
7365 					return (EINVAL);
7366 				} else {
7367 					goto if_copydone;
7368 				}
7369 			}
7370 			ipif_get_name(ipif, ifr->ifr_name,
7371 			    sizeof (ifr->ifr_name));
7372 			sin = (sin_t *)&ifr->ifr_addr;
7373 			*sin = sin_null;
7374 			sin->sin_family = AF_INET;
7375 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7376 			ifr++;
7377 		}
7378 	}
7379 if_copydone:
7380 	rw_exit(&ipst->ips_ill_g_lock);
7381 	mp1->b_wptr = (uchar_t *)ifr;
7382 
7383 	if (STRUCT_BUF(ifc) != NULL) {
7384 		STRUCT_FSET(ifc, ifc_len,
7385 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7386 	}
7387 	return (0);
7388 }
7389 
7390 /*
7391  * Get the interfaces using the address hosted on the interface passed in,
7392  * as a source adddress
7393  */
7394 /* ARGSUSED */
7395 int
7396 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7397     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7398 {
7399 	mblk_t *mp1;
7400 	ill_t	*ill, *ill_head;
7401 	ipif_t	*ipif, *orig_ipif;
7402 	int	numlifs = 0;
7403 	size_t	lifs_bufsize, lifsmaxlen;
7404 	struct	lifreq *lifr;
7405 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7406 	uint_t	ifindex;
7407 	zoneid_t zoneid;
7408 	boolean_t isv6 = B_FALSE;
7409 	struct	sockaddr_in	*sin;
7410 	struct	sockaddr_in6	*sin6;
7411 	STRUCT_HANDLE(lifsrcof, lifs);
7412 	ip_stack_t		*ipst;
7413 
7414 	ipst = CONNQ_TO_IPST(q);
7415 
7416 	ASSERT(q->q_next == NULL);
7417 
7418 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7419 
7420 	/* Existence verified in ip_wput_nondata */
7421 	mp1 = mp->b_cont->b_cont;
7422 
7423 	/*
7424 	 * Must be (better be!) continuation of a TRANSPARENT
7425 	 * IOCTL.  We just copied in the lifsrcof structure.
7426 	 */
7427 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7428 	    (struct lifsrcof *)mp1->b_rptr);
7429 
7430 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7431 		return (EINVAL);
7432 
7433 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7434 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7435 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7436 	if (ipif == NULL) {
7437 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7438 		    ifindex));
7439 		return (ENXIO);
7440 	}
7441 
7442 	/* Allocate a buffer to hold requested information */
7443 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7444 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7445 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7446 	/* The actual size needed is always returned in lifs_len */
7447 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7448 
7449 	/* If the amount we need is more than what is passed in, abort */
7450 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7451 		ipif_refrele(ipif);
7452 		return (0);
7453 	}
7454 
7455 	mp1 = mi_copyout_alloc(q, mp,
7456 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7457 	if (mp1 == NULL) {
7458 		ipif_refrele(ipif);
7459 		return (ENOMEM);
7460 	}
7461 
7462 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7463 	bzero(mp1->b_rptr, lifs_bufsize);
7464 
7465 	lifr = (struct lifreq *)mp1->b_rptr;
7466 
7467 	ill = ill_head = ipif->ipif_ill;
7468 	orig_ipif = ipif;
7469 
7470 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7471 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7472 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7473 
7474 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7475 	for (; (ill != NULL) && (ill != ill_head);
7476 	    ill = ill->ill_usesrc_grp_next) {
7477 
7478 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7479 			break;
7480 
7481 		ipif = ill->ill_ipif;
7482 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7483 		if (ipif->ipif_isv6) {
7484 			sin6 = (sin6_t *)&lifr->lifr_addr;
7485 			*sin6 = sin6_null;
7486 			sin6->sin6_family = AF_INET6;
7487 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7488 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7489 			    &ipif->ipif_v6net_mask);
7490 		} else {
7491 			sin = (sin_t *)&lifr->lifr_addr;
7492 			*sin = sin_null;
7493 			sin->sin_family = AF_INET;
7494 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7495 			lifr->lifr_addrlen = ip_mask_to_plen(
7496 			    ipif->ipif_net_mask);
7497 		}
7498 		lifr++;
7499 	}
7500 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7501 	rw_exit(&ipst->ips_ill_g_lock);
7502 	ipif_refrele(orig_ipif);
7503 	mp1->b_wptr = (uchar_t *)lifr;
7504 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7505 
7506 	return (0);
7507 }
7508 
7509 /* ARGSUSED */
7510 int
7511 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7512     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7513 {
7514 	mblk_t *mp1;
7515 	int	list;
7516 	ill_t	*ill;
7517 	ipif_t	*ipif;
7518 	int	flags;
7519 	int	numlifs = 0;
7520 	size_t	lifc_bufsize;
7521 	struct	lifreq *lifr;
7522 	sa_family_t	family;
7523 	struct	sockaddr_in	*sin;
7524 	struct	sockaddr_in6	*sin6;
7525 	ill_walk_context_t	ctx;
7526 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7527 	int32_t	lifclen;
7528 	zoneid_t zoneid;
7529 	STRUCT_HANDLE(lifconf, lifc);
7530 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7531 
7532 	ip1dbg(("ip_sioctl_get_lifconf"));
7533 
7534 	ASSERT(q->q_next == NULL);
7535 
7536 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7537 
7538 	/* Existence verified in ip_wput_nondata */
7539 	mp1 = mp->b_cont->b_cont;
7540 
7541 	/*
7542 	 * An extended version of SIOCGIFCONF that takes an
7543 	 * additional address family and flags field.
7544 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7545 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7546 	 * interfaces are omitted.
7547 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7548 	 * unless LIFC_TEMPORARY is specified.
7549 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7550 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7551 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7552 	 * has priority over LIFC_NOXMIT.
7553 	 */
7554 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7555 
7556 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7557 		return (EINVAL);
7558 
7559 	/*
7560 	 * Must be (better be!) continuation of a TRANSPARENT
7561 	 * IOCTL.  We just copied in the lifconf structure.
7562 	 */
7563 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7564 
7565 	family = STRUCT_FGET(lifc, lifc_family);
7566 	flags = STRUCT_FGET(lifc, lifc_flags);
7567 
7568 	switch (family) {
7569 	case AF_UNSPEC:
7570 		/*
7571 		 * walk all ILL's.
7572 		 */
7573 		list = MAX_G_HEADS;
7574 		break;
7575 	case AF_INET:
7576 		/*
7577 		 * walk only IPV4 ILL's.
7578 		 */
7579 		list = IP_V4_G_HEAD;
7580 		break;
7581 	case AF_INET6:
7582 		/*
7583 		 * walk only IPV6 ILL's.
7584 		 */
7585 		list = IP_V6_G_HEAD;
7586 		break;
7587 	default:
7588 		return (EAFNOSUPPORT);
7589 	}
7590 
7591 	/*
7592 	 * Allocate a buffer to hold requested information.
7593 	 *
7594 	 * If lifc_len is larger than what is needed, we only
7595 	 * allocate what we will use.
7596 	 *
7597 	 * If lifc_len is smaller than what is needed, return
7598 	 * EINVAL.
7599 	 */
7600 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7601 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7602 	lifclen = STRUCT_FGET(lifc, lifc_len);
7603 	if (lifc_bufsize > lifclen) {
7604 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7605 			return (EINVAL);
7606 		else
7607 			lifc_bufsize = lifclen;
7608 	}
7609 
7610 	mp1 = mi_copyout_alloc(q, mp,
7611 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7612 	if (mp1 == NULL)
7613 		return (ENOMEM);
7614 
7615 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7616 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7617 
7618 	lifr = (struct lifreq *)mp1->b_rptr;
7619 
7620 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7621 	ill = ill_first(list, list, &ctx, ipst);
7622 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7623 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7624 			continue;
7625 
7626 		for (ipif = ill->ill_ipif; ipif != NULL;
7627 		    ipif = ipif->ipif_next) {
7628 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7629 			    !(flags & LIFC_NOXMIT))
7630 				continue;
7631 
7632 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7633 			    !(flags & LIFC_TEMPORARY))
7634 				continue;
7635 
7636 			if (((ipif->ipif_flags &
7637 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7638 			    IPIF_DEPRECATED)) ||
7639 			    IS_LOOPBACK(ill) ||
7640 			    !(ipif->ipif_flags & IPIF_UP)) &&
7641 			    (flags & LIFC_EXTERNAL_SOURCE))
7642 				continue;
7643 
7644 			if (zoneid != ipif->ipif_zoneid &&
7645 			    ipif->ipif_zoneid != ALL_ZONES &&
7646 			    (zoneid != GLOBAL_ZONEID ||
7647 			    !(flags & LIFC_ALLZONES)))
7648 				continue;
7649 
7650 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7651 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7652 					rw_exit(&ipst->ips_ill_g_lock);
7653 					return (EINVAL);
7654 				} else {
7655 					goto lif_copydone;
7656 				}
7657 			}
7658 
7659 			ipif_get_name(ipif, lifr->lifr_name,
7660 			    sizeof (lifr->lifr_name));
7661 			lifr->lifr_type = ill->ill_type;
7662 			if (ipif->ipif_isv6) {
7663 				sin6 = (sin6_t *)&lifr->lifr_addr;
7664 				*sin6 = sin6_null;
7665 				sin6->sin6_family = AF_INET6;
7666 				sin6->sin6_addr =
7667 				    ipif->ipif_v6lcl_addr;
7668 				lifr->lifr_addrlen =
7669 				    ip_mask_to_plen_v6(
7670 				    &ipif->ipif_v6net_mask);
7671 			} else {
7672 				sin = (sin_t *)&lifr->lifr_addr;
7673 				*sin = sin_null;
7674 				sin->sin_family = AF_INET;
7675 				sin->sin_addr.s_addr =
7676 				    ipif->ipif_lcl_addr;
7677 				lifr->lifr_addrlen =
7678 				    ip_mask_to_plen(
7679 				    ipif->ipif_net_mask);
7680 			}
7681 			lifr++;
7682 		}
7683 	}
7684 lif_copydone:
7685 	rw_exit(&ipst->ips_ill_g_lock);
7686 
7687 	mp1->b_wptr = (uchar_t *)lifr;
7688 	if (STRUCT_BUF(lifc) != NULL) {
7689 		STRUCT_FSET(lifc, lifc_len,
7690 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7691 	}
7692 	return (0);
7693 }
7694 
7695 static void
7696 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7697 {
7698 	ip6_asp_t *table;
7699 	size_t table_size;
7700 	mblk_t *data_mp;
7701 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7702 	ip_stack_t	*ipst;
7703 
7704 	if (q->q_next == NULL)
7705 		ipst = CONNQ_TO_IPST(q);
7706 	else
7707 		ipst = ILLQ_TO_IPST(q);
7708 
7709 	/* These two ioctls are I_STR only */
7710 	if (iocp->ioc_count == TRANSPARENT) {
7711 		miocnak(q, mp, 0, EINVAL);
7712 		return;
7713 	}
7714 
7715 	data_mp = mp->b_cont;
7716 	if (data_mp == NULL) {
7717 		/* The user passed us a NULL argument */
7718 		table = NULL;
7719 		table_size = iocp->ioc_count;
7720 	} else {
7721 		/*
7722 		 * The user provided a table.  The stream head
7723 		 * may have copied in the user data in chunks,
7724 		 * so make sure everything is pulled up
7725 		 * properly.
7726 		 */
7727 		if (MBLKL(data_mp) < iocp->ioc_count) {
7728 			mblk_t *new_data_mp;
7729 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7730 			    NULL) {
7731 				miocnak(q, mp, 0, ENOMEM);
7732 				return;
7733 			}
7734 			freemsg(data_mp);
7735 			data_mp = new_data_mp;
7736 			mp->b_cont = data_mp;
7737 		}
7738 		table = (ip6_asp_t *)data_mp->b_rptr;
7739 		table_size = iocp->ioc_count;
7740 	}
7741 
7742 	switch (iocp->ioc_cmd) {
7743 	case SIOCGIP6ADDRPOLICY:
7744 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7745 		if (iocp->ioc_rval == -1)
7746 			iocp->ioc_error = EINVAL;
7747 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7748 		else if (table != NULL &&
7749 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7750 			ip6_asp_t *src = table;
7751 			ip6_asp32_t *dst = (void *)table;
7752 			int count = table_size / sizeof (ip6_asp_t);
7753 			int i;
7754 
7755 			/*
7756 			 * We need to do an in-place shrink of the array
7757 			 * to match the alignment attributes of the
7758 			 * 32-bit ABI looking at it.
7759 			 */
7760 			/* LINTED: logical expression always true: op "||" */
7761 			ASSERT(sizeof (*src) > sizeof (*dst));
7762 			for (i = 1; i < count; i++)
7763 				bcopy(src + i, dst + i, sizeof (*dst));
7764 		}
7765 #endif
7766 		break;
7767 
7768 	case SIOCSIP6ADDRPOLICY:
7769 		ASSERT(mp->b_prev == NULL);
7770 		mp->b_prev = (void *)q;
7771 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7772 		/*
7773 		 * We pass in the datamodel here so that the ip6_asp_replace()
7774 		 * routine can handle converting from 32-bit to native formats
7775 		 * where necessary.
7776 		 *
7777 		 * A better way to handle this might be to convert the inbound
7778 		 * data structure here, and hang it off a new 'mp'; thus the
7779 		 * ip6_asp_replace() logic would always be dealing with native
7780 		 * format data structures..
7781 		 *
7782 		 * (An even simpler way to handle these ioctls is to just
7783 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7784 		 * and just recompile everything that depends on it.)
7785 		 */
7786 #endif
7787 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7788 		    iocp->ioc_flag & IOC_MODELS);
7789 		return;
7790 	}
7791 
7792 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7793 	qreply(q, mp);
7794 }
7795 
7796 static void
7797 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7798 {
7799 	mblk_t 		*data_mp;
7800 	struct dstinforeq	*dir;
7801 	uint8_t		*end, *cur;
7802 	in6_addr_t	*daddr, *saddr;
7803 	ipaddr_t	v4daddr;
7804 	ire_t		*ire;
7805 	ipaddr_t	v4setsrc;
7806 	in6_addr_t	v6setsrc;
7807 	char		*slabel, *dlabel;
7808 	boolean_t	isipv4;
7809 	int		match_ire;
7810 	ill_t		*dst_ill;
7811 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7812 	conn_t		*connp = Q_TO_CONN(q);
7813 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7814 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7815 	uint64_t	ipif_flags;
7816 
7817 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7818 
7819 	/*
7820 	 * This ioctl is I_STR only, and must have a
7821 	 * data mblk following the M_IOCTL mblk.
7822 	 */
7823 	data_mp = mp->b_cont;
7824 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7825 		miocnak(q, mp, 0, EINVAL);
7826 		return;
7827 	}
7828 
7829 	if (MBLKL(data_mp) < iocp->ioc_count) {
7830 		mblk_t *new_data_mp;
7831 
7832 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7833 			miocnak(q, mp, 0, ENOMEM);
7834 			return;
7835 		}
7836 		freemsg(data_mp);
7837 		data_mp = new_data_mp;
7838 		mp->b_cont = data_mp;
7839 	}
7840 	match_ire = MATCH_IRE_DSTONLY;
7841 
7842 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7843 	    end - cur >= sizeof (struct dstinforeq);
7844 	    cur += sizeof (struct dstinforeq)) {
7845 		dir = (struct dstinforeq *)cur;
7846 		daddr = &dir->dir_daddr;
7847 		saddr = &dir->dir_saddr;
7848 
7849 		/*
7850 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7851 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7852 		 * and ip_select_source_v6() do not.
7853 		 */
7854 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7855 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7856 
7857 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7858 		if (isipv4) {
7859 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7860 			v4setsrc = INADDR_ANY;
7861 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7862 			    NULL, match_ire, B_TRUE, 0, ipst, &v4setsrc, NULL,
7863 			    NULL);
7864 		} else {
7865 			v6setsrc = ipv6_all_zeros;
7866 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7867 			    NULL, match_ire, B_TRUE, 0, ipst, &v6setsrc, NULL,
7868 			    NULL);
7869 		}
7870 		ASSERT(ire != NULL);
7871 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7872 			ire_refrele(ire);
7873 			dir->dir_dreachable = 0;
7874 
7875 			/* move on to next dst addr */
7876 			continue;
7877 		}
7878 		dir->dir_dreachable = 1;
7879 
7880 		dst_ill = ire_nexthop_ill(ire);
7881 		if (dst_ill == NULL) {
7882 			ire_refrele(ire);
7883 			continue;
7884 		}
7885 
7886 		/* With ipmp we most likely look at the ipmp ill here */
7887 		dir->dir_dmactype = dst_ill->ill_mactype;
7888 
7889 		if (isipv4) {
7890 			ipaddr_t v4saddr;
7891 
7892 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7893 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7894 			    &v4saddr, NULL, &ipif_flags) != 0) {
7895 				v4saddr = INADDR_ANY;
7896 				ipif_flags = 0;
7897 			}
7898 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7899 		} else {
7900 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7901 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7902 			    saddr, NULL, &ipif_flags) != 0) {
7903 				*saddr = ipv6_all_zeros;
7904 				ipif_flags = 0;
7905 			}
7906 		}
7907 
7908 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7909 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7910 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7911 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7912 		ire_refrele(ire);
7913 		ill_refrele(dst_ill);
7914 	}
7915 	miocack(q, mp, iocp->ioc_count, 0);
7916 }
7917 
7918 /*
7919  * Check if this is an address assigned to this machine.
7920  * Skips interfaces that are down by using ire checks.
7921  * Translates mapped addresses to v4 addresses and then
7922  * treats them as such, returning true if the v4 address
7923  * associated with this mapped address is configured.
7924  * Note: Applications will have to be careful what they do
7925  * with the response; use of mapped addresses limits
7926  * what can be done with the socket, especially with
7927  * respect to socket options and ioctls - neither IPv4
7928  * options nor IPv6 sticky options/ancillary data options
7929  * may be used.
7930  */
7931 /* ARGSUSED */
7932 int
7933 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7934     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7935 {
7936 	struct sioc_addrreq *sia;
7937 	sin_t *sin;
7938 	ire_t *ire;
7939 	mblk_t *mp1;
7940 	zoneid_t zoneid;
7941 	ip_stack_t	*ipst;
7942 
7943 	ip1dbg(("ip_sioctl_tmyaddr"));
7944 
7945 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7946 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7947 	ipst = CONNQ_TO_IPST(q);
7948 
7949 	/* Existence verified in ip_wput_nondata */
7950 	mp1 = mp->b_cont->b_cont;
7951 	sia = (struct sioc_addrreq *)mp1->b_rptr;
7952 	sin = (sin_t *)&sia->sa_addr;
7953 	switch (sin->sin_family) {
7954 	case AF_INET6: {
7955 		sin6_t *sin6 = (sin6_t *)sin;
7956 
7957 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7958 			ipaddr_t v4_addr;
7959 
7960 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
7961 			    v4_addr);
7962 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
7963 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7964 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7965 		} else {
7966 			in6_addr_t v6addr;
7967 
7968 			v6addr = sin6->sin6_addr;
7969 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
7970 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7971 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7972 		}
7973 		break;
7974 	}
7975 	case AF_INET: {
7976 		ipaddr_t v4addr;
7977 
7978 		v4addr = sin->sin_addr.s_addr;
7979 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
7980 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
7981 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7982 		break;
7983 	}
7984 	default:
7985 		return (EAFNOSUPPORT);
7986 	}
7987 	if (ire != NULL) {
7988 		sia->sa_res = 1;
7989 		ire_refrele(ire);
7990 	} else {
7991 		sia->sa_res = 0;
7992 	}
7993 	return (0);
7994 }
7995 
7996 /*
7997  * Check if this is an address assigned on-link i.e. neighbor,
7998  * and makes sure it's reachable from the current zone.
7999  * Returns true for my addresses as well.
8000  * Translates mapped addresses to v4 addresses and then
8001  * treats them as such, returning true if the v4 address
8002  * associated with this mapped address is configured.
8003  * Note: Applications will have to be careful what they do
8004  * with the response; use of mapped addresses limits
8005  * what can be done with the socket, especially with
8006  * respect to socket options and ioctls - neither IPv4
8007  * options nor IPv6 sticky options/ancillary data options
8008  * may be used.
8009  */
8010 /* ARGSUSED */
8011 int
8012 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8013     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8014 {
8015 	struct sioc_addrreq *sia;
8016 	sin_t *sin;
8017 	mblk_t	*mp1;
8018 	ire_t *ire = NULL;
8019 	zoneid_t zoneid;
8020 	ip_stack_t	*ipst;
8021 
8022 	ip1dbg(("ip_sioctl_tonlink"));
8023 
8024 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8025 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8026 	ipst = CONNQ_TO_IPST(q);
8027 
8028 	/* Existence verified in ip_wput_nondata */
8029 	mp1 = mp->b_cont->b_cont;
8030 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8031 	sin = (sin_t *)&sia->sa_addr;
8032 
8033 	/*
8034 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8035 	 * to make sure we only look at on-link unicast address.
8036 	 */
8037 	switch (sin->sin_family) {
8038 	case AF_INET6: {
8039 		sin6_t *sin6 = (sin6_t *)sin;
8040 
8041 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8042 			ipaddr_t v4_addr;
8043 
8044 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8045 			    v4_addr);
8046 			if (!CLASSD(v4_addr)) {
8047 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8048 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8049 				    0, ipst, NULL);
8050 			}
8051 		} else {
8052 			in6_addr_t v6addr;
8053 
8054 			v6addr = sin6->sin6_addr;
8055 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8056 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8057 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8058 				    ipst, NULL);
8059 			}
8060 		}
8061 		break;
8062 	}
8063 	case AF_INET: {
8064 		ipaddr_t v4addr;
8065 
8066 		v4addr = sin->sin_addr.s_addr;
8067 		if (!CLASSD(v4addr)) {
8068 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8069 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8070 		}
8071 		break;
8072 	}
8073 	default:
8074 		return (EAFNOSUPPORT);
8075 	}
8076 	sia->sa_res = 0;
8077 	if (ire != NULL) {
8078 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8079 
8080 		if ((ire->ire_type & IRE_ONLINK) &&
8081 		    !(ire->ire_type & IRE_BROADCAST))
8082 			sia->sa_res = 1;
8083 		ire_refrele(ire);
8084 	}
8085 	return (0);
8086 }
8087 
8088 /*
8089  * TBD: implement when kernel maintaines a list of site prefixes.
8090  */
8091 /* ARGSUSED */
8092 int
8093 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8094     ip_ioctl_cmd_t *ipip, void *ifreq)
8095 {
8096 	return (ENXIO);
8097 }
8098 
8099 /* ARP IOCTLs. */
8100 /* ARGSUSED */
8101 int
8102 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8103     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8104 {
8105 	int		err;
8106 	ipaddr_t	ipaddr;
8107 	struct iocblk	*iocp;
8108 	conn_t		*connp;
8109 	struct arpreq	*ar;
8110 	struct xarpreq	*xar;
8111 	int		arp_flags, flags, alength;
8112 	uchar_t		*lladdr;
8113 	ip_stack_t	*ipst;
8114 	ill_t		*ill = ipif->ipif_ill;
8115 	ill_t		*proxy_ill = NULL;
8116 	ipmp_arpent_t	*entp = NULL;
8117 	boolean_t	proxyarp = B_FALSE;
8118 	boolean_t	if_arp_ioctl = B_FALSE;
8119 	ncec_t		*ncec = NULL;
8120 	nce_t		*nce;
8121 
8122 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8123 	connp = Q_TO_CONN(q);
8124 	ipst = connp->conn_netstack->netstack_ip;
8125 	iocp = (struct iocblk *)mp->b_rptr;
8126 
8127 	if (ipip->ipi_cmd_type == XARP_CMD) {
8128 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8129 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8130 		ar = NULL;
8131 
8132 		arp_flags = xar->xarp_flags;
8133 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8134 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8135 		/*
8136 		 * Validate against user's link layer address length
8137 		 * input and name and addr length limits.
8138 		 */
8139 		alength = ill->ill_phys_addr_length;
8140 		if (ipip->ipi_cmd == SIOCSXARP) {
8141 			if (alength != xar->xarp_ha.sdl_alen ||
8142 			    (alength + xar->xarp_ha.sdl_nlen >
8143 			    sizeof (xar->xarp_ha.sdl_data)))
8144 				return (EINVAL);
8145 		}
8146 	} else {
8147 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8148 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8149 		xar = NULL;
8150 
8151 		arp_flags = ar->arp_flags;
8152 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8153 		/*
8154 		 * Theoretically, the sa_family could tell us what link
8155 		 * layer type this operation is trying to deal with. By
8156 		 * common usage AF_UNSPEC means ethernet. We'll assume
8157 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8158 		 * for now. Our new SIOC*XARP ioctls can be used more
8159 		 * generally.
8160 		 *
8161 		 * If the underlying media happens to have a non 6 byte
8162 		 * address, arp module will fail set/get, but the del
8163 		 * operation will succeed.
8164 		 */
8165 		alength = 6;
8166 		if ((ipip->ipi_cmd != SIOCDARP) &&
8167 		    (alength != ill->ill_phys_addr_length)) {
8168 			return (EINVAL);
8169 		}
8170 	}
8171 
8172 	/* Translate ATF* flags to NCE* flags */
8173 	flags = 0;
8174 	if (arp_flags & ATF_AUTHORITY)
8175 		flags |= NCE_F_AUTHORITY;
8176 	if (arp_flags & ATF_PERM)
8177 		flags |= NCE_F_NONUD; /* not subject to aging */
8178 	if (arp_flags & ATF_PUBL)
8179 		flags |= NCE_F_PUBLISH;
8180 
8181 	/*
8182 	 * IPMP ARP special handling:
8183 	 *
8184 	 * 1. Since ARP mappings must appear consistent across the group,
8185 	 *    prohibit changing ARP mappings on the underlying interfaces.
8186 	 *
8187 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8188 	 *    IP itself, prohibit changing them.
8189 	 *
8190 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8191 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8192 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8193 	 */
8194 	if (IS_UNDER_IPMP(ill)) {
8195 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8196 			return (EPERM);
8197 	}
8198 	if (IS_IPMP(ill)) {
8199 		ipmp_illgrp_t *illg = ill->ill_grp;
8200 
8201 		switch (ipip->ipi_cmd) {
8202 		case SIOCSARP:
8203 		case SIOCSXARP:
8204 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8205 			if (proxy_ill != NULL) {
8206 				proxyarp = B_TRUE;
8207 				if (!ipmp_ill_is_active(proxy_ill))
8208 					proxy_ill = ipmp_illgrp_next_ill(illg);
8209 				if (proxy_ill != NULL)
8210 					lladdr = proxy_ill->ill_phys_addr;
8211 			}
8212 			/* FALLTHRU */
8213 		}
8214 	}
8215 
8216 	ipaddr = sin->sin_addr.s_addr;
8217 	/*
8218 	 * don't match across illgrp per case (1) and (2).
8219 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8220 	 */
8221 	nce = nce_lookup_v4(ill, &ipaddr);
8222 	if (nce != NULL)
8223 		ncec = nce->nce_common;
8224 
8225 	switch (iocp->ioc_cmd) {
8226 	case SIOCDARP:
8227 	case SIOCDXARP: {
8228 		/*
8229 		 * Delete the NCE if any.
8230 		 */
8231 		if (ncec == NULL) {
8232 			iocp->ioc_error = ENXIO;
8233 			break;
8234 		}
8235 		/* Don't allow changes to arp mappings of local addresses. */
8236 		if (NCE_MYADDR(ncec)) {
8237 			nce_refrele(nce);
8238 			return (ENOTSUP);
8239 		}
8240 		iocp->ioc_error = 0;
8241 
8242 		/*
8243 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8244 		 * This will delete all the nce entries on the under_ills.
8245 		 */
8246 		ncec_delete(ncec);
8247 		/*
8248 		 * Once the NCE has been deleted, then the ire_dep* consistency
8249 		 * mechanism will find any IRE which depended on the now
8250 		 * condemned NCE (as part of sending packets).
8251 		 * That mechanism handles redirects by deleting redirects
8252 		 * that refer to UNREACHABLE nces.
8253 		 */
8254 		break;
8255 	}
8256 	case SIOCGARP:
8257 	case SIOCGXARP:
8258 		if (ncec != NULL) {
8259 			lladdr = ncec->ncec_lladdr;
8260 			flags = ncec->ncec_flags;
8261 			iocp->ioc_error = 0;
8262 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8263 		} else {
8264 			iocp->ioc_error = ENXIO;
8265 		}
8266 		break;
8267 	case SIOCSARP:
8268 	case SIOCSXARP:
8269 		/* Don't allow changes to arp mappings of local addresses. */
8270 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8271 			nce_refrele(nce);
8272 			return (ENOTSUP);
8273 		}
8274 
8275 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8276 		flags |= NCE_F_STATIC;
8277 		if (!if_arp_ioctl) {
8278 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8279 			    lladdr, alength, flags);
8280 		} else {
8281 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8282 			if (ipif != NULL) {
8283 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8284 				    lladdr, alength, flags);
8285 				ipif_refrele(ipif);
8286 			}
8287 		}
8288 		if (nce != NULL) {
8289 			nce_refrele(nce);
8290 			nce = NULL;
8291 		}
8292 		/*
8293 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8294 		 * by nce_add_common()
8295 		 */
8296 		err = nce_lookup_then_add_v4(ill, lladdr,
8297 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8298 		    &nce);
8299 		if (err == EEXIST) {
8300 			ncec = nce->nce_common;
8301 			mutex_enter(&ncec->ncec_lock);
8302 			ncec->ncec_state = ND_REACHABLE;
8303 			ncec->ncec_flags = flags;
8304 			nce_update(ncec, ND_UNCHANGED, lladdr);
8305 			mutex_exit(&ncec->ncec_lock);
8306 			err = 0;
8307 		}
8308 		if (nce != NULL) {
8309 			nce_refrele(nce);
8310 			nce = NULL;
8311 		}
8312 		if (IS_IPMP(ill) && err == 0) {
8313 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8314 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8315 			    flags);
8316 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8317 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8318 				break;
8319 			}
8320 		}
8321 		iocp->ioc_error = err;
8322 	}
8323 
8324 	if (nce != NULL) {
8325 		nce_refrele(nce);
8326 	}
8327 
8328 	/*
8329 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8330 	 */
8331 	if (entp != NULL)
8332 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8333 
8334 	return (iocp->ioc_error);
8335 }
8336 
8337 /*
8338  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8339  * the associated sin and refhold and return the associated ipif via `ci'.
8340  */
8341 int
8342 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8343     cmd_info_t *ci)
8344 {
8345 	mblk_t	*mp1;
8346 	sin_t	*sin;
8347 	conn_t	*connp;
8348 	ipif_t	*ipif;
8349 	ire_t	*ire = NULL;
8350 	ill_t	*ill = NULL;
8351 	boolean_t exists;
8352 	ip_stack_t *ipst;
8353 	struct arpreq *ar;
8354 	struct xarpreq *xar;
8355 	struct sockaddr_dl *sdl;
8356 
8357 	/* ioctl comes down on a conn */
8358 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8359 	connp = Q_TO_CONN(q);
8360 	if (connp->conn_family == AF_INET6)
8361 		return (ENXIO);
8362 
8363 	ipst = connp->conn_netstack->netstack_ip;
8364 
8365 	/* Verified in ip_wput_nondata */
8366 	mp1 = mp->b_cont->b_cont;
8367 
8368 	if (ipip->ipi_cmd_type == XARP_CMD) {
8369 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8370 		xar = (struct xarpreq *)mp1->b_rptr;
8371 		sin = (sin_t *)&xar->xarp_pa;
8372 		sdl = &xar->xarp_ha;
8373 
8374 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8375 			return (ENXIO);
8376 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8377 			return (EINVAL);
8378 	} else {
8379 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8380 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8381 		ar = (struct arpreq *)mp1->b_rptr;
8382 		sin = (sin_t *)&ar->arp_pa;
8383 	}
8384 
8385 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8386 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8387 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8388 		if (ipif == NULL)
8389 			return (ENXIO);
8390 		if (ipif->ipif_id != 0) {
8391 			ipif_refrele(ipif);
8392 			return (ENXIO);
8393 		}
8394 	} else {
8395 		/*
8396 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8397 		 * of 0: use the IP address to find the ipif.  If the IP
8398 		 * address is an IPMP test address, ire_ftable_lookup() will
8399 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8400 		 */
8401 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8402 		    ipst);
8403 		if (ipif == NULL) {
8404 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8405 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8406 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8407 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8408 				if (ire != NULL)
8409 					ire_refrele(ire);
8410 				return (ENXIO);
8411 			}
8412 			ASSERT(ire != NULL && ill != NULL);
8413 			ipif = ill->ill_ipif;
8414 			ipif_refhold(ipif);
8415 			ire_refrele(ire);
8416 		}
8417 	}
8418 
8419 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8420 		ipif_refrele(ipif);
8421 		return (ENXIO);
8422 	}
8423 
8424 	ci->ci_sin = sin;
8425 	ci->ci_ipif = ipif;
8426 	return (0);
8427 }
8428 
8429 /*
8430  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8431  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8432  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8433  * up and thus an ill can join that illgrp.
8434  *
8435  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8436  * open()/close() primarily because close() is not allowed to fail or block
8437  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8438  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8439  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8440  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8441  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8442  * state if I_UNLINK didn't occur.
8443  *
8444  * Note that for each plumb/unplumb operation, we may end up here more than
8445  * once because of the way ifconfig works.  However, it's OK to link the same
8446  * illgrp more than once, or unlink an illgrp that's already unlinked.
8447  */
8448 static int
8449 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8450 {
8451 	int err;
8452 	ip_stack_t *ipst = ill->ill_ipst;
8453 
8454 	ASSERT(IS_IPMP(ill));
8455 	ASSERT(IAM_WRITER_ILL(ill));
8456 
8457 	switch (ioccmd) {
8458 	case I_LINK:
8459 		return (ENOTSUP);
8460 
8461 	case I_PLINK:
8462 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8463 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8464 		rw_exit(&ipst->ips_ipmp_lock);
8465 		break;
8466 
8467 	case I_PUNLINK:
8468 		/*
8469 		 * Require all UP ipifs be brought down prior to unlinking the
8470 		 * illgrp so any associated IREs (and other state) is torched.
8471 		 */
8472 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8473 			return (EBUSY);
8474 
8475 		/*
8476 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8477 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8478 		 * join this group.  Specifically: ills trying to join grab
8479 		 * ipmp_lock and bump a "pending join" counter checked by
8480 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8481 		 * joins can occur (since we have ipmp_lock).  Once we drop
8482 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8483 		 * find the illgrp (since we unlinked it) and will return
8484 		 * EAFNOSUPPORT.  This will then take them back through the
8485 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8486 		 * back through I_PLINK above.
8487 		 */
8488 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8489 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8490 		rw_exit(&ipst->ips_ipmp_lock);
8491 		return (err);
8492 	default:
8493 		break;
8494 	}
8495 	return (0);
8496 }
8497 
8498 /*
8499  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8500  * atomically set/clear the muxids. Also complete the ioctl by acking or
8501  * naking it.  Note that the code is structured such that the link type,
8502  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8503  * its clones use the persistent link, while pppd(1M) and perhaps many
8504  * other daemons may use non-persistent link.  When combined with some
8505  * ill_t states, linking and unlinking lower streams may be used as
8506  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8507  */
8508 /* ARGSUSED */
8509 void
8510 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8511 {
8512 	mblk_t		*mp1;
8513 	struct linkblk	*li;
8514 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8515 	int		err = 0;
8516 
8517 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8518 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8519 
8520 	mp1 = mp->b_cont;	/* This is the linkblk info */
8521 	li = (struct linkblk *)mp1->b_rptr;
8522 
8523 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8524 	if (err == EINPROGRESS)
8525 		return;
8526 done:
8527 	if (err == 0)
8528 		miocack(q, mp, 0, 0);
8529 	else
8530 		miocnak(q, mp, 0, err);
8531 
8532 	/* Conn was refheld in ip_sioctl_copyin_setup */
8533 	if (CONN_Q(q))
8534 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8535 }
8536 
8537 /*
8538  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8539  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8540  * module stream).  If `doconsist' is set, then do the extended consistency
8541  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8542  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8543  * an error code on failure.
8544  */
8545 static int
8546 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8547     struct linkblk *li)
8548 {
8549 	int		err = 0;
8550 	ill_t  		*ill;
8551 	queue_t		*ipwq, *dwq;
8552 	const char	*name;
8553 	struct qinit	*qinfo;
8554 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8555 	boolean_t	entered_ipsq = B_FALSE;
8556 	boolean_t	is_ip = B_FALSE;
8557 	arl_t		*arl;
8558 
8559 	/*
8560 	 * Walk the lower stream to verify it's the IP module stream.
8561 	 * The IP module is identified by its name, wput function,
8562 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8563 	 * (li->l_qbot) will not vanish until this ioctl completes.
8564 	 */
8565 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8566 		qinfo = ipwq->q_qinfo;
8567 		name = qinfo->qi_minfo->mi_idname;
8568 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8569 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8570 			is_ip = B_TRUE;
8571 			break;
8572 		}
8573 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8574 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8575 			break;
8576 		}
8577 	}
8578 
8579 	/*
8580 	 * If this isn't an IP module stream, bail.
8581 	 */
8582 	if (ipwq == NULL)
8583 		return (0);
8584 
8585 	if (!is_ip) {
8586 		arl = (arl_t *)ipwq->q_ptr;
8587 		ill = arl_to_ill(arl);
8588 		if (ill == NULL)
8589 			return (0);
8590 	} else {
8591 		ill = ipwq->q_ptr;
8592 	}
8593 	ASSERT(ill != NULL);
8594 
8595 	if (ipsq == NULL) {
8596 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8597 		    NEW_OP, B_FALSE);
8598 		if (ipsq == NULL) {
8599 			if (!is_ip)
8600 				ill_refrele(ill);
8601 			return (EINPROGRESS);
8602 		}
8603 		entered_ipsq = B_TRUE;
8604 	}
8605 	ASSERT(IAM_WRITER_ILL(ill));
8606 	mutex_enter(&ill->ill_lock);
8607 	if (!is_ip) {
8608 		if (islink && ill->ill_muxid == 0) {
8609 			/*
8610 			 * Plumbing has to be done with IP plumbed first, arp
8611 			 * second, but here we have arp being plumbed first.
8612 			 */
8613 			mutex_exit(&ill->ill_lock);
8614 			ipsq_exit(ipsq);
8615 			ill_refrele(ill);
8616 			return (EINVAL);
8617 		}
8618 	}
8619 	mutex_exit(&ill->ill_lock);
8620 	if (!is_ip) {
8621 		arl->arl_muxid = islink ? li->l_index : 0;
8622 		ill_refrele(ill);
8623 		goto done;
8624 	}
8625 
8626 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8627 		goto done;
8628 
8629 	/*
8630 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8631 	 * the read queue of the module immediately below IP in the ill.
8632 	 * These are used during the capability negotiation below.
8633 	 */
8634 	ill->ill_lmod_rq = NULL;
8635 	ill->ill_lmod_cnt = 0;
8636 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8637 		ill->ill_lmod_rq = RD(dwq);
8638 		for (; dwq != NULL; dwq = dwq->q_next)
8639 			ill->ill_lmod_cnt++;
8640 	}
8641 
8642 	ill->ill_muxid = islink ? li->l_index : 0;
8643 
8644 	/*
8645 	 * Mark the ipsq busy until the capability operations initiated below
8646 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8647 	 * returns, but the capability operation may complete asynchronously
8648 	 * much later.
8649 	 */
8650 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8651 	/*
8652 	 * If there's at least one up ipif on this ill, then we're bound to
8653 	 * the underlying driver via DLPI.  In that case, renegotiate
8654 	 * capabilities to account for any possible change in modules
8655 	 * interposed between IP and the driver.
8656 	 */
8657 	if (ill->ill_ipif_up_count > 0) {
8658 		if (islink)
8659 			ill_capability_probe(ill);
8660 		else
8661 			ill_capability_reset(ill, B_FALSE);
8662 	}
8663 	ipsq_current_finish(ipsq);
8664 done:
8665 	if (entered_ipsq)
8666 		ipsq_exit(ipsq);
8667 
8668 	return (err);
8669 }
8670 
8671 /*
8672  * Search the ioctl command in the ioctl tables and return a pointer
8673  * to the ioctl command information. The ioctl command tables are
8674  * static and fully populated at compile time.
8675  */
8676 ip_ioctl_cmd_t *
8677 ip_sioctl_lookup(int ioc_cmd)
8678 {
8679 	int index;
8680 	ip_ioctl_cmd_t *ipip;
8681 	ip_ioctl_cmd_t *ipip_end;
8682 
8683 	if (ioc_cmd == IPI_DONTCARE)
8684 		return (NULL);
8685 
8686 	/*
8687 	 * Do a 2 step search. First search the indexed table
8688 	 * based on the least significant byte of the ioctl cmd.
8689 	 * If we don't find a match, then search the misc table
8690 	 * serially.
8691 	 */
8692 	index = ioc_cmd & 0xFF;
8693 	if (index < ip_ndx_ioctl_count) {
8694 		ipip = &ip_ndx_ioctl_table[index];
8695 		if (ipip->ipi_cmd == ioc_cmd) {
8696 			/* Found a match in the ndx table */
8697 			return (ipip);
8698 		}
8699 	}
8700 
8701 	/* Search the misc table */
8702 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8703 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8704 		if (ipip->ipi_cmd == ioc_cmd)
8705 			/* Found a match in the misc table */
8706 			return (ipip);
8707 	}
8708 
8709 	return (NULL);
8710 }
8711 
8712 /*
8713  * Wrapper function for resuming deferred ioctl processing
8714  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8715  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8716  */
8717 /* ARGSUSED */
8718 void
8719 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8720     void *dummy_arg)
8721 {
8722 	ip_sioctl_copyin_setup(q, mp);
8723 }
8724 
8725 /*
8726  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8727  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8728  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8729  * We establish here the size of the block to be copied in.  mi_copyin
8730  * arranges for this to happen, an processing continues in ip_wput_nondata with
8731  * an M_IOCDATA message.
8732  */
8733 void
8734 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8735 {
8736 	int	copyin_size;
8737 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8738 	ip_ioctl_cmd_t *ipip;
8739 	cred_t *cr;
8740 	ip_stack_t	*ipst;
8741 
8742 	if (CONN_Q(q))
8743 		ipst = CONNQ_TO_IPST(q);
8744 	else
8745 		ipst = ILLQ_TO_IPST(q);
8746 
8747 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8748 	if (ipip == NULL) {
8749 		/*
8750 		 * The ioctl is not one we understand or own.
8751 		 * Pass it along to be processed down stream,
8752 		 * if this is a module instance of IP, else nak
8753 		 * the ioctl.
8754 		 */
8755 		if (q->q_next == NULL) {
8756 			goto nak;
8757 		} else {
8758 			putnext(q, mp);
8759 			return;
8760 		}
8761 	}
8762 
8763 	/*
8764 	 * If this is deferred, then we will do all the checks when we
8765 	 * come back.
8766 	 */
8767 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8768 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8769 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8770 		return;
8771 	}
8772 
8773 	/*
8774 	 * Only allow a very small subset of IP ioctls on this stream if
8775 	 * IP is a module and not a driver. Allowing ioctls to be processed
8776 	 * in this case may cause assert failures or data corruption.
8777 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8778 	 * ioctls allowed on an IP module stream, after which this stream
8779 	 * normally becomes a multiplexor (at which time the stream head
8780 	 * will fail all ioctls).
8781 	 */
8782 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8783 		goto nak;
8784 	}
8785 
8786 	/* Make sure we have ioctl data to process. */
8787 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8788 		goto nak;
8789 
8790 	/*
8791 	 * Prefer dblk credential over ioctl credential; some synthesized
8792 	 * ioctls have kcred set because there's no way to crhold()
8793 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8794 	 * the framework; the caller of ioctl needs to hold the reference
8795 	 * for the duration of the call).
8796 	 */
8797 	cr = msg_getcred(mp, NULL);
8798 	if (cr == NULL)
8799 		cr = iocp->ioc_cr;
8800 
8801 	/* Make sure normal users don't send down privileged ioctls */
8802 	if ((ipip->ipi_flags & IPI_PRIV) &&
8803 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8804 		/* We checked the privilege earlier but log it here */
8805 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8806 		return;
8807 	}
8808 
8809 	/*
8810 	 * The ioctl command tables can only encode fixed length
8811 	 * ioctl data. If the length is variable, the table will
8812 	 * encode the length as zero. Such special cases are handled
8813 	 * below in the switch.
8814 	 */
8815 	if (ipip->ipi_copyin_size != 0) {
8816 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8817 		return;
8818 	}
8819 
8820 	switch (iocp->ioc_cmd) {
8821 	case O_SIOCGIFCONF:
8822 	case SIOCGIFCONF:
8823 		/*
8824 		 * This IOCTL is hilarious.  See comments in
8825 		 * ip_sioctl_get_ifconf for the story.
8826 		 */
8827 		if (iocp->ioc_count == TRANSPARENT)
8828 			copyin_size = SIZEOF_STRUCT(ifconf,
8829 			    iocp->ioc_flag);
8830 		else
8831 			copyin_size = iocp->ioc_count;
8832 		mi_copyin(q, mp, NULL, copyin_size);
8833 		return;
8834 
8835 	case O_SIOCGLIFCONF:
8836 	case SIOCGLIFCONF:
8837 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8838 		mi_copyin(q, mp, NULL, copyin_size);
8839 		return;
8840 
8841 	case SIOCGLIFSRCOF:
8842 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8843 		mi_copyin(q, mp, NULL, copyin_size);
8844 		return;
8845 	case SIOCGIP6ADDRPOLICY:
8846 		ip_sioctl_ip6addrpolicy(q, mp);
8847 		ip6_asp_table_refrele(ipst);
8848 		return;
8849 
8850 	case SIOCSIP6ADDRPOLICY:
8851 		ip_sioctl_ip6addrpolicy(q, mp);
8852 		return;
8853 
8854 	case SIOCGDSTINFO:
8855 		ip_sioctl_dstinfo(q, mp);
8856 		ip6_asp_table_refrele(ipst);
8857 		return;
8858 
8859 	case I_PLINK:
8860 	case I_PUNLINK:
8861 	case I_LINK:
8862 	case I_UNLINK:
8863 		/*
8864 		 * We treat non-persistent link similarly as the persistent
8865 		 * link case, in terms of plumbing/unplumbing, as well as
8866 		 * dynamic re-plumbing events indicator.  See comments
8867 		 * in ip_sioctl_plink() for more.
8868 		 *
8869 		 * Request can be enqueued in the 'ipsq' while waiting
8870 		 * to become exclusive. So bump up the conn ref.
8871 		 */
8872 		if (CONN_Q(q))
8873 			CONN_INC_REF(Q_TO_CONN(q));
8874 		ip_sioctl_plink(NULL, q, mp, NULL);
8875 		return;
8876 
8877 	case ND_GET:
8878 	case ND_SET:
8879 		/*
8880 		 * Use of the nd table requires holding the reader lock.
8881 		 * Modifying the nd table thru nd_load/nd_unload requires
8882 		 * the writer lock.
8883 		 */
8884 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8885 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8886 			rw_exit(&ipst->ips_ip_g_nd_lock);
8887 
8888 			if (iocp->ioc_error)
8889 				iocp->ioc_count = 0;
8890 			mp->b_datap->db_type = M_IOCACK;
8891 			qreply(q, mp);
8892 			return;
8893 		}
8894 		rw_exit(&ipst->ips_ip_g_nd_lock);
8895 		/*
8896 		 * We don't understand this subioctl of ND_GET / ND_SET.
8897 		 * Maybe intended for some driver / module below us
8898 		 */
8899 		if (q->q_next) {
8900 			putnext(q, mp);
8901 		} else {
8902 			iocp->ioc_error = ENOENT;
8903 			mp->b_datap->db_type = M_IOCNAK;
8904 			iocp->ioc_count = 0;
8905 			qreply(q, mp);
8906 		}
8907 		return;
8908 
8909 	case IP_IOCTL:
8910 		ip_wput_ioctl(q, mp);
8911 		return;
8912 
8913 	case SIOCILB:
8914 		/* The ioctl length varies depending on the ILB command. */
8915 		copyin_size = iocp->ioc_count;
8916 		if (copyin_size < sizeof (ilb_cmd_t))
8917 			goto nak;
8918 		mi_copyin(q, mp, NULL, copyin_size);
8919 		return;
8920 
8921 	default:
8922 		cmn_err(CE_PANIC, "should not happen ");
8923 	}
8924 nak:
8925 	if (mp->b_cont != NULL) {
8926 		freemsg(mp->b_cont);
8927 		mp->b_cont = NULL;
8928 	}
8929 	iocp->ioc_error = EINVAL;
8930 	mp->b_datap->db_type = M_IOCNAK;
8931 	iocp->ioc_count = 0;
8932 	qreply(q, mp);
8933 }
8934 
8935 static void
8936 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
8937 {
8938 	struct arpreq *ar;
8939 	struct xarpreq *xar;
8940 	mblk_t	*tmp;
8941 	struct iocblk *iocp;
8942 	int x_arp_ioctl = B_FALSE;
8943 	int *flagsp;
8944 	char *storage = NULL;
8945 
8946 	ASSERT(ill != NULL);
8947 
8948 	iocp = (struct iocblk *)mp->b_rptr;
8949 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
8950 
8951 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
8952 	if ((iocp->ioc_cmd == SIOCGXARP) ||
8953 	    (iocp->ioc_cmd == SIOCSXARP)) {
8954 		x_arp_ioctl = B_TRUE;
8955 		xar = (struct xarpreq *)tmp->b_rptr;
8956 		flagsp = &xar->xarp_flags;
8957 		storage = xar->xarp_ha.sdl_data;
8958 	} else {
8959 		ar = (struct arpreq *)tmp->b_rptr;
8960 		flagsp = &ar->arp_flags;
8961 		storage = ar->arp_ha.sa_data;
8962 	}
8963 
8964 	/*
8965 	 * We're done if this is not an SIOCG{X}ARP
8966 	 */
8967 	if (x_arp_ioctl) {
8968 		storage += ill_xarp_info(&xar->xarp_ha, ill);
8969 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
8970 		    sizeof (xar->xarp_ha.sdl_data)) {
8971 			iocp->ioc_error = EINVAL;
8972 			return;
8973 		}
8974 	}
8975 	*flagsp = ATF_INUSE;
8976 	/*
8977 	 * If /sbin/arp told us we are the authority using the "permanent"
8978 	 * flag, or if this is one of my addresses print "permanent"
8979 	 * in the /sbin/arp output.
8980 	 */
8981 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
8982 		*flagsp |= ATF_AUTHORITY;
8983 	if (flags & NCE_F_NONUD)
8984 		*flagsp |= ATF_PERM; /* not subject to aging */
8985 	if (flags & NCE_F_PUBLISH)
8986 		*flagsp |= ATF_PUBL;
8987 	if (hwaddr != NULL) {
8988 		*flagsp |= ATF_COM;
8989 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
8990 	}
8991 }
8992 
8993 /*
8994  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
8995  * interface) create the next available logical interface for this
8996  * physical interface.
8997  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
8998  * ipif with the specified name.
8999  *
9000  * If the address family is not AF_UNSPEC then set the address as well.
9001  *
9002  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9003  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9004  *
9005  * Executed as a writer on the ill.
9006  * So no lock is needed to traverse the ipif chain, or examine the
9007  * phyint flags.
9008  */
9009 /* ARGSUSED */
9010 int
9011 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9012     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9013 {
9014 	mblk_t	*mp1;
9015 	struct lifreq *lifr;
9016 	boolean_t	isv6;
9017 	boolean_t	exists;
9018 	char 	*name;
9019 	char	*endp;
9020 	char	*cp;
9021 	int	namelen;
9022 	ipif_t	*ipif;
9023 	long	id;
9024 	ipsq_t	*ipsq;
9025 	ill_t	*ill;
9026 	sin_t	*sin;
9027 	int	err = 0;
9028 	boolean_t found_sep = B_FALSE;
9029 	conn_t	*connp;
9030 	zoneid_t zoneid;
9031 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9032 
9033 	ASSERT(q->q_next == NULL);
9034 	ip1dbg(("ip_sioctl_addif\n"));
9035 	/* Existence of mp1 has been checked in ip_wput_nondata */
9036 	mp1 = mp->b_cont->b_cont;
9037 	/*
9038 	 * Null terminate the string to protect against buffer
9039 	 * overrun. String was generated by user code and may not
9040 	 * be trusted.
9041 	 */
9042 	lifr = (struct lifreq *)mp1->b_rptr;
9043 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9044 	name = lifr->lifr_name;
9045 	ASSERT(CONN_Q(q));
9046 	connp = Q_TO_CONN(q);
9047 	isv6 = (connp->conn_family == AF_INET6);
9048 	zoneid = connp->conn_zoneid;
9049 	namelen = mi_strlen(name);
9050 	if (namelen == 0)
9051 		return (EINVAL);
9052 
9053 	exists = B_FALSE;
9054 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9055 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9056 		/*
9057 		 * Allow creating lo0 using SIOCLIFADDIF.
9058 		 * can't be any other writer thread. So can pass null below
9059 		 * for the last 4 args to ipif_lookup_name.
9060 		 */
9061 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9062 		    &exists, isv6, zoneid, ipst);
9063 		/* Prevent any further action */
9064 		if (ipif == NULL) {
9065 			return (ENOBUFS);
9066 		} else if (!exists) {
9067 			/* We created the ipif now and as writer */
9068 			ipif_refrele(ipif);
9069 			return (0);
9070 		} else {
9071 			ill = ipif->ipif_ill;
9072 			ill_refhold(ill);
9073 			ipif_refrele(ipif);
9074 		}
9075 	} else {
9076 		/* Look for a colon in the name. */
9077 		endp = &name[namelen];
9078 		for (cp = endp; --cp > name; ) {
9079 			if (*cp == IPIF_SEPARATOR_CHAR) {
9080 				found_sep = B_TRUE;
9081 				/*
9082 				 * Reject any non-decimal aliases for plumbing
9083 				 * of logical interfaces. Aliases with leading
9084 				 * zeroes are also rejected as they introduce
9085 				 * ambiguity in the naming of the interfaces.
9086 				 * Comparing with "0" takes care of all such
9087 				 * cases.
9088 				 */
9089 				if ((strncmp("0", cp+1, 1)) == 0)
9090 					return (EINVAL);
9091 
9092 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9093 				    id <= 0 || *endp != '\0') {
9094 					return (EINVAL);
9095 				}
9096 				*cp = '\0';
9097 				break;
9098 			}
9099 		}
9100 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9101 		if (found_sep)
9102 			*cp = IPIF_SEPARATOR_CHAR;
9103 		if (ill == NULL)
9104 			return (ENXIO);
9105 	}
9106 
9107 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9108 	    B_TRUE);
9109 
9110 	/*
9111 	 * Release the refhold due to the lookup, now that we are excl
9112 	 * or we are just returning
9113 	 */
9114 	ill_refrele(ill);
9115 
9116 	if (ipsq == NULL)
9117 		return (EINPROGRESS);
9118 
9119 	/* We are now exclusive on the IPSQ */
9120 	ASSERT(IAM_WRITER_ILL(ill));
9121 
9122 	if (found_sep) {
9123 		/* Now see if there is an IPIF with this unit number. */
9124 		for (ipif = ill->ill_ipif; ipif != NULL;
9125 		    ipif = ipif->ipif_next) {
9126 			if (ipif->ipif_id == id) {
9127 				err = EEXIST;
9128 				goto done;
9129 			}
9130 		}
9131 	}
9132 
9133 	/*
9134 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9135 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9136 	 * instead.
9137 	 */
9138 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9139 	    B_TRUE, B_TRUE)) == NULL) {
9140 		err = ENOBUFS;
9141 		goto done;
9142 	}
9143 
9144 	/* Return created name with ioctl */
9145 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9146 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9147 	ip1dbg(("created %s\n", lifr->lifr_name));
9148 
9149 	/* Set address */
9150 	sin = (sin_t *)&lifr->lifr_addr;
9151 	if (sin->sin_family != AF_UNSPEC) {
9152 		err = ip_sioctl_addr(ipif, sin, q, mp,
9153 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9154 	}
9155 
9156 done:
9157 	ipsq_exit(ipsq);
9158 	return (err);
9159 }
9160 
9161 /*
9162  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9163  * interface) delete it based on the IP address (on this physical interface).
9164  * Otherwise delete it based on the ipif_id.
9165  * Also, special handling to allow a removeif of lo0.
9166  */
9167 /* ARGSUSED */
9168 int
9169 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9170     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9171 {
9172 	conn_t		*connp;
9173 	ill_t		*ill = ipif->ipif_ill;
9174 	boolean_t	 success;
9175 	ip_stack_t	*ipst;
9176 
9177 	ipst = CONNQ_TO_IPST(q);
9178 
9179 	ASSERT(q->q_next == NULL);
9180 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9181 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9182 	ASSERT(IAM_WRITER_IPIF(ipif));
9183 
9184 	connp = Q_TO_CONN(q);
9185 	/*
9186 	 * Special case for unplumbing lo0 (the loopback physical interface).
9187 	 * If unplumbing lo0, the incoming address structure has been
9188 	 * initialized to all zeros. When unplumbing lo0, all its logical
9189 	 * interfaces must be removed too.
9190 	 *
9191 	 * Note that this interface may be called to remove a specific
9192 	 * loopback logical interface (eg, lo0:1). But in that case
9193 	 * ipif->ipif_id != 0 so that the code path for that case is the
9194 	 * same as any other interface (meaning it skips the code directly
9195 	 * below).
9196 	 */
9197 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9198 		if (sin->sin_family == AF_UNSPEC &&
9199 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9200 			/*
9201 			 * Mark it condemned. No new ref. will be made to ill.
9202 			 */
9203 			mutex_enter(&ill->ill_lock);
9204 			ill->ill_state_flags |= ILL_CONDEMNED;
9205 			for (ipif = ill->ill_ipif; ipif != NULL;
9206 			    ipif = ipif->ipif_next) {
9207 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9208 			}
9209 			mutex_exit(&ill->ill_lock);
9210 
9211 			ipif = ill->ill_ipif;
9212 			/* unplumb the loopback interface */
9213 			ill_delete(ill);
9214 			mutex_enter(&connp->conn_lock);
9215 			mutex_enter(&ill->ill_lock);
9216 
9217 			/* Are any references to this ill active */
9218 			if (ill_is_freeable(ill)) {
9219 				mutex_exit(&ill->ill_lock);
9220 				mutex_exit(&connp->conn_lock);
9221 				ill_delete_tail(ill);
9222 				mi_free(ill);
9223 				return (0);
9224 			}
9225 			success = ipsq_pending_mp_add(connp, ipif,
9226 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9227 			mutex_exit(&connp->conn_lock);
9228 			mutex_exit(&ill->ill_lock);
9229 			if (success)
9230 				return (EINPROGRESS);
9231 			else
9232 				return (EINTR);
9233 		}
9234 	}
9235 
9236 	if (ipif->ipif_id == 0) {
9237 		ipsq_t *ipsq;
9238 
9239 		/* Find based on address */
9240 		if (ipif->ipif_isv6) {
9241 			sin6_t *sin6;
9242 
9243 			if (sin->sin_family != AF_INET6)
9244 				return (EAFNOSUPPORT);
9245 
9246 			sin6 = (sin6_t *)sin;
9247 			/* We are a writer, so we should be able to lookup */
9248 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9249 			    ipst);
9250 		} else {
9251 			if (sin->sin_family != AF_INET)
9252 				return (EAFNOSUPPORT);
9253 
9254 			/* We are a writer, so we should be able to lookup */
9255 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9256 			    ipst);
9257 		}
9258 		if (ipif == NULL) {
9259 			return (EADDRNOTAVAIL);
9260 		}
9261 
9262 		/*
9263 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9264 		 * lifr_name of the physical interface but with an ip address
9265 		 * lifr_addr of a logical interface plumbed over it.
9266 		 * So update ipx_current_ipif now that ipif points to the
9267 		 * correct one.
9268 		 */
9269 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9270 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9271 
9272 		/* This is a writer */
9273 		ipif_refrele(ipif);
9274 	}
9275 
9276 	/*
9277 	 * Can not delete instance zero since it is tied to the ill.
9278 	 */
9279 	if (ipif->ipif_id == 0)
9280 		return (EBUSY);
9281 
9282 	mutex_enter(&ill->ill_lock);
9283 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9284 	mutex_exit(&ill->ill_lock);
9285 
9286 	ipif_free(ipif);
9287 
9288 	mutex_enter(&connp->conn_lock);
9289 	mutex_enter(&ill->ill_lock);
9290 
9291 	/* Are any references to this ipif active */
9292 	if (ipif_is_freeable(ipif)) {
9293 		mutex_exit(&ill->ill_lock);
9294 		mutex_exit(&connp->conn_lock);
9295 		ipif_non_duplicate(ipif);
9296 		(void) ipif_down_tail(ipif);
9297 		ipif_free_tail(ipif); /* frees ipif */
9298 		return (0);
9299 	}
9300 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9301 	    IPIF_FREE);
9302 	mutex_exit(&ill->ill_lock);
9303 	mutex_exit(&connp->conn_lock);
9304 	if (success)
9305 		return (EINPROGRESS);
9306 	else
9307 		return (EINTR);
9308 }
9309 
9310 /*
9311  * Restart the removeif ioctl. The refcnt has gone down to 0.
9312  * The ipif is already condemned. So can't find it thru lookups.
9313  */
9314 /* ARGSUSED */
9315 int
9316 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9317     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9318 {
9319 	ill_t *ill = ipif->ipif_ill;
9320 
9321 	ASSERT(IAM_WRITER_IPIF(ipif));
9322 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9323 
9324 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9325 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9326 
9327 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9328 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9329 		ill_delete_tail(ill);
9330 		mi_free(ill);
9331 		return (0);
9332 	}
9333 
9334 	ipif_non_duplicate(ipif);
9335 	(void) ipif_down_tail(ipif);
9336 	ipif_free_tail(ipif);
9337 
9338 	return (0);
9339 }
9340 
9341 /*
9342  * Set the local interface address.
9343  * Allow an address of all zero when the interface is down.
9344  */
9345 /* ARGSUSED */
9346 int
9347 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9348     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9349 {
9350 	int err = 0;
9351 	in6_addr_t v6addr;
9352 	boolean_t need_up = B_FALSE;
9353 
9354 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9355 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9356 
9357 	ASSERT(IAM_WRITER_IPIF(ipif));
9358 
9359 	if (ipif->ipif_isv6) {
9360 		sin6_t *sin6;
9361 		ill_t *ill;
9362 		phyint_t *phyi;
9363 
9364 		if (sin->sin_family != AF_INET6)
9365 			return (EAFNOSUPPORT);
9366 
9367 		sin6 = (sin6_t *)sin;
9368 		v6addr = sin6->sin6_addr;
9369 		ill = ipif->ipif_ill;
9370 		phyi = ill->ill_phyint;
9371 
9372 		/*
9373 		 * Enforce that true multicast interfaces have a link-local
9374 		 * address for logical unit 0.
9375 		 */
9376 		if (ipif->ipif_id == 0 &&
9377 		    (ill->ill_flags & ILLF_MULTICAST) &&
9378 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9379 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9380 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9381 			return (EADDRNOTAVAIL);
9382 		}
9383 
9384 		/*
9385 		 * up interfaces shouldn't have the unspecified address
9386 		 * unless they also have the IPIF_NOLOCAL flags set and
9387 		 * have a subnet assigned.
9388 		 */
9389 		if ((ipif->ipif_flags & IPIF_UP) &&
9390 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9391 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9392 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9393 			return (EADDRNOTAVAIL);
9394 		}
9395 
9396 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9397 			return (EADDRNOTAVAIL);
9398 	} else {
9399 		ipaddr_t addr;
9400 
9401 		if (sin->sin_family != AF_INET)
9402 			return (EAFNOSUPPORT);
9403 
9404 		addr = sin->sin_addr.s_addr;
9405 
9406 		/* Allow 0 as the local address. */
9407 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9408 			return (EADDRNOTAVAIL);
9409 
9410 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9411 	}
9412 
9413 	/*
9414 	 * Even if there is no change we redo things just to rerun
9415 	 * ipif_set_default.
9416 	 */
9417 	if (ipif->ipif_flags & IPIF_UP) {
9418 		/*
9419 		 * Setting a new local address, make sure
9420 		 * we have net and subnet bcast ire's for
9421 		 * the old address if we need them.
9422 		 */
9423 		/*
9424 		 * If the interface is already marked up,
9425 		 * we call ipif_down which will take care
9426 		 * of ditching any IREs that have been set
9427 		 * up based on the old interface address.
9428 		 */
9429 		err = ipif_logical_down(ipif, q, mp);
9430 		if (err == EINPROGRESS)
9431 			return (err);
9432 		(void) ipif_down_tail(ipif);
9433 		need_up = 1;
9434 	}
9435 
9436 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9437 	return (err);
9438 }
9439 
9440 int
9441 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9442     boolean_t need_up)
9443 {
9444 	in6_addr_t v6addr;
9445 	in6_addr_t ov6addr;
9446 	ipaddr_t addr;
9447 	sin6_t	*sin6;
9448 	int	sinlen;
9449 	int	err = 0;
9450 	ill_t	*ill = ipif->ipif_ill;
9451 	boolean_t need_dl_down;
9452 	boolean_t need_arp_down;
9453 	struct iocblk *iocp;
9454 
9455 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9456 
9457 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9458 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9459 	ASSERT(IAM_WRITER_IPIF(ipif));
9460 
9461 	/* Must cancel any pending timer before taking the ill_lock */
9462 	if (ipif->ipif_recovery_id != 0)
9463 		(void) untimeout(ipif->ipif_recovery_id);
9464 	ipif->ipif_recovery_id = 0;
9465 
9466 	if (ipif->ipif_isv6) {
9467 		sin6 = (sin6_t *)sin;
9468 		v6addr = sin6->sin6_addr;
9469 		sinlen = sizeof (struct sockaddr_in6);
9470 	} else {
9471 		addr = sin->sin_addr.s_addr;
9472 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9473 		sinlen = sizeof (struct sockaddr_in);
9474 	}
9475 	mutex_enter(&ill->ill_lock);
9476 	ov6addr = ipif->ipif_v6lcl_addr;
9477 	ipif->ipif_v6lcl_addr = v6addr;
9478 	sctp_update_ipif_addr(ipif, ov6addr);
9479 	ipif->ipif_addr_ready = 0;
9480 
9481 	/*
9482 	 * If the interface was previously marked as a duplicate, then since
9483 	 * we've now got a "new" address, it should no longer be considered a
9484 	 * duplicate -- even if the "new" address is the same as the old one.
9485 	 * Note that if all ipifs are down, we may have a pending ARP down
9486 	 * event to handle.  This is because we want to recover from duplicates
9487 	 * and thus delay tearing down ARP until the duplicates have been
9488 	 * removed or disabled.
9489 	 */
9490 	need_dl_down = need_arp_down = B_FALSE;
9491 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9492 		need_arp_down = !need_up;
9493 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9494 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9495 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9496 			need_dl_down = B_TRUE;
9497 		}
9498 	}
9499 
9500 	ipif_set_default(ipif);
9501 
9502 	/*
9503 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9504 	 * tag the ill so that future updates to the interface ID don't result
9505 	 * in this address getting automatically reconfigured from under the
9506 	 * administrator.
9507 	 */
9508 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9509 		ill->ill_manual_linklocal = 1;
9510 
9511 	/*
9512 	 * When publishing an interface address change event, we only notify
9513 	 * the event listeners of the new address.  It is assumed that if they
9514 	 * actively care about the addresses assigned that they will have
9515 	 * already discovered the previous address assigned (if there was one.)
9516 	 *
9517 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9518 	 */
9519 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9520 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9521 		    NE_ADDRESS_CHANGE, sin, sinlen);
9522 	}
9523 
9524 	mutex_exit(&ill->ill_lock);
9525 
9526 	if (need_up) {
9527 		/*
9528 		 * Now bring the interface back up.  If this
9529 		 * is the only IPIF for the ILL, ipif_up
9530 		 * will have to re-bind to the device, so
9531 		 * we may get back EINPROGRESS, in which
9532 		 * case, this IOCTL will get completed in
9533 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9534 		 */
9535 		err = ipif_up(ipif, q, mp);
9536 	} else {
9537 		/* Perhaps ilgs should use this ill */
9538 		update_conn_ill(NULL, ill->ill_ipst);
9539 	}
9540 
9541 	if (need_dl_down)
9542 		ill_dl_down(ill);
9543 
9544 	if (need_arp_down && !ill->ill_isv6)
9545 		(void) ipif_arp_down(ipif);
9546 
9547 	/*
9548 	 * The default multicast interface might have changed (for
9549 	 * instance if the IPv6 scope of the address changed)
9550 	 */
9551 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9552 
9553 	return (err);
9554 }
9555 
9556 /*
9557  * Restart entry point to restart the address set operation after the
9558  * refcounts have dropped to zero.
9559  */
9560 /* ARGSUSED */
9561 int
9562 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9563     ip_ioctl_cmd_t *ipip, void *ifreq)
9564 {
9565 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9566 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9567 	ASSERT(IAM_WRITER_IPIF(ipif));
9568 	(void) ipif_down_tail(ipif);
9569 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9570 }
9571 
9572 /* ARGSUSED */
9573 int
9574 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9575     ip_ioctl_cmd_t *ipip, void *if_req)
9576 {
9577 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9578 	struct lifreq *lifr = (struct lifreq *)if_req;
9579 
9580 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9581 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9582 	/*
9583 	 * The net mask and address can't change since we have a
9584 	 * reference to the ipif. So no lock is necessary.
9585 	 */
9586 	if (ipif->ipif_isv6) {
9587 		*sin6 = sin6_null;
9588 		sin6->sin6_family = AF_INET6;
9589 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9590 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9591 		lifr->lifr_addrlen =
9592 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9593 	} else {
9594 		*sin = sin_null;
9595 		sin->sin_family = AF_INET;
9596 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9597 		if (ipip->ipi_cmd_type == LIF_CMD) {
9598 			lifr->lifr_addrlen =
9599 			    ip_mask_to_plen(ipif->ipif_net_mask);
9600 		}
9601 	}
9602 	return (0);
9603 }
9604 
9605 /*
9606  * Set the destination address for a pt-pt interface.
9607  */
9608 /* ARGSUSED */
9609 int
9610 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9611     ip_ioctl_cmd_t *ipip, void *if_req)
9612 {
9613 	int err = 0;
9614 	in6_addr_t v6addr;
9615 	boolean_t need_up = B_FALSE;
9616 
9617 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9618 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9619 	ASSERT(IAM_WRITER_IPIF(ipif));
9620 
9621 	if (ipif->ipif_isv6) {
9622 		sin6_t *sin6;
9623 
9624 		if (sin->sin_family != AF_INET6)
9625 			return (EAFNOSUPPORT);
9626 
9627 		sin6 = (sin6_t *)sin;
9628 		v6addr = sin6->sin6_addr;
9629 
9630 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9631 			return (EADDRNOTAVAIL);
9632 	} else {
9633 		ipaddr_t addr;
9634 
9635 		if (sin->sin_family != AF_INET)
9636 			return (EAFNOSUPPORT);
9637 
9638 		addr = sin->sin_addr.s_addr;
9639 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9640 			return (EADDRNOTAVAIL);
9641 
9642 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9643 	}
9644 
9645 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9646 		return (0);	/* No change */
9647 
9648 	if (ipif->ipif_flags & IPIF_UP) {
9649 		/*
9650 		 * If the interface is already marked up,
9651 		 * we call ipif_down which will take care
9652 		 * of ditching any IREs that have been set
9653 		 * up based on the old pp dst address.
9654 		 */
9655 		err = ipif_logical_down(ipif, q, mp);
9656 		if (err == EINPROGRESS)
9657 			return (err);
9658 		(void) ipif_down_tail(ipif);
9659 		need_up = B_TRUE;
9660 	}
9661 	/*
9662 	 * could return EINPROGRESS. If so ioctl will complete in
9663 	 * ip_rput_dlpi_writer
9664 	 */
9665 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9666 	return (err);
9667 }
9668 
9669 static int
9670 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9671     boolean_t need_up)
9672 {
9673 	in6_addr_t v6addr;
9674 	ill_t	*ill = ipif->ipif_ill;
9675 	int	err = 0;
9676 	boolean_t need_dl_down;
9677 	boolean_t need_arp_down;
9678 
9679 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9680 	    ipif->ipif_id, (void *)ipif));
9681 
9682 	/* Must cancel any pending timer before taking the ill_lock */
9683 	if (ipif->ipif_recovery_id != 0)
9684 		(void) untimeout(ipif->ipif_recovery_id);
9685 	ipif->ipif_recovery_id = 0;
9686 
9687 	if (ipif->ipif_isv6) {
9688 		sin6_t *sin6;
9689 
9690 		sin6 = (sin6_t *)sin;
9691 		v6addr = sin6->sin6_addr;
9692 	} else {
9693 		ipaddr_t addr;
9694 
9695 		addr = sin->sin_addr.s_addr;
9696 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9697 	}
9698 	mutex_enter(&ill->ill_lock);
9699 	/* Set point to point destination address. */
9700 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9701 		/*
9702 		 * Allow this as a means of creating logical
9703 		 * pt-pt interfaces on top of e.g. an Ethernet.
9704 		 * XXX Undocumented HACK for testing.
9705 		 * pt-pt interfaces are created with NUD disabled.
9706 		 */
9707 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9708 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9709 		if (ipif->ipif_isv6)
9710 			ill->ill_flags |= ILLF_NONUD;
9711 	}
9712 
9713 	/*
9714 	 * If the interface was previously marked as a duplicate, then since
9715 	 * we've now got a "new" address, it should no longer be considered a
9716 	 * duplicate -- even if the "new" address is the same as the old one.
9717 	 * Note that if all ipifs are down, we may have a pending ARP down
9718 	 * event to handle.
9719 	 */
9720 	need_dl_down = need_arp_down = B_FALSE;
9721 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9722 		need_arp_down = !need_up;
9723 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9724 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9725 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9726 			need_dl_down = B_TRUE;
9727 		}
9728 	}
9729 
9730 	/* Set the new address. */
9731 	ipif->ipif_v6pp_dst_addr = v6addr;
9732 	/* Make sure subnet tracks pp_dst */
9733 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9734 	mutex_exit(&ill->ill_lock);
9735 
9736 	if (need_up) {
9737 		/*
9738 		 * Now bring the interface back up.  If this
9739 		 * is the only IPIF for the ILL, ipif_up
9740 		 * will have to re-bind to the device, so
9741 		 * we may get back EINPROGRESS, in which
9742 		 * case, this IOCTL will get completed in
9743 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9744 		 */
9745 		err = ipif_up(ipif, q, mp);
9746 	}
9747 
9748 	if (need_dl_down)
9749 		ill_dl_down(ill);
9750 	if (need_arp_down && !ipif->ipif_isv6)
9751 		(void) ipif_arp_down(ipif);
9752 
9753 	return (err);
9754 }
9755 
9756 /*
9757  * Restart entry point to restart the dstaddress set operation after the
9758  * refcounts have dropped to zero.
9759  */
9760 /* ARGSUSED */
9761 int
9762 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9763     ip_ioctl_cmd_t *ipip, void *ifreq)
9764 {
9765 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9766 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9767 	(void) ipif_down_tail(ipif);
9768 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9769 }
9770 
9771 /* ARGSUSED */
9772 int
9773 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9774     ip_ioctl_cmd_t *ipip, void *if_req)
9775 {
9776 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9777 
9778 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9779 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9780 	/*
9781 	 * Get point to point destination address. The addresses can't
9782 	 * change since we hold a reference to the ipif.
9783 	 */
9784 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9785 		return (EADDRNOTAVAIL);
9786 
9787 	if (ipif->ipif_isv6) {
9788 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9789 		*sin6 = sin6_null;
9790 		sin6->sin6_family = AF_INET6;
9791 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9792 	} else {
9793 		*sin = sin_null;
9794 		sin->sin_family = AF_INET;
9795 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9796 	}
9797 	return (0);
9798 }
9799 
9800 /*
9801  * Set interface flags.  Many flags require special handling (e.g.,
9802  * bringing the interface down); see below for details.
9803  *
9804  * NOTE : We really don't enforce that ipif_id zero should be used
9805  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9806  *	  is because applications generally does SICGLIFFLAGS and
9807  *	  ORs in the new flags (that affects the logical) and does a
9808  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9809  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9810  *	  flags that will be turned on is correct with respect to
9811  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9812  */
9813 /* ARGSUSED */
9814 int
9815 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9816     ip_ioctl_cmd_t *ipip, void *if_req)
9817 {
9818 	uint64_t turn_on;
9819 	uint64_t turn_off;
9820 	int	err = 0;
9821 	phyint_t *phyi;
9822 	ill_t *ill;
9823 	uint64_t intf_flags, cantchange_flags;
9824 	boolean_t phyint_flags_modified = B_FALSE;
9825 	uint64_t flags;
9826 	struct ifreq *ifr;
9827 	struct lifreq *lifr;
9828 	boolean_t set_linklocal = B_FALSE;
9829 
9830 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9831 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9832 
9833 	ASSERT(IAM_WRITER_IPIF(ipif));
9834 
9835 	ill = ipif->ipif_ill;
9836 	phyi = ill->ill_phyint;
9837 
9838 	if (ipip->ipi_cmd_type == IF_CMD) {
9839 		ifr = (struct ifreq *)if_req;
9840 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9841 	} else {
9842 		lifr = (struct lifreq *)if_req;
9843 		flags = lifr->lifr_flags;
9844 	}
9845 
9846 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9847 
9848 	/*
9849 	 * Have the flags been set correctly until now?
9850 	 */
9851 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9852 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9853 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9854 	/*
9855 	 * Compare the new flags to the old, and partition
9856 	 * into those coming on and those going off.
9857 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9858 	 */
9859 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9860 		flags |= intf_flags & ~0xFFFF;
9861 
9862 	/*
9863 	 * Explicitly fail attempts to change flags that are always invalid on
9864 	 * an IPMP meta-interface.
9865 	 */
9866 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9867 		return (EINVAL);
9868 
9869 	/*
9870 	 * Check which flags will change; silently ignore flags which userland
9871 	 * is not allowed to control.  (Because these flags may change between
9872 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
9873 	 * control, we need to silently ignore them rather than fail.)
9874 	 */
9875 	cantchange_flags = IFF_CANTCHANGE;
9876 	if (IS_IPMP(ill))
9877 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9878 
9879 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9880 	if (turn_on == 0)
9881 		return (0);	/* No change */
9882 
9883 	turn_off = intf_flags & turn_on;
9884 	turn_on ^= turn_off;
9885 
9886 	/*
9887 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
9888 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
9889 	 * allow it to be turned off.
9890 	 */
9891 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
9892 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
9893 		return (EINVAL);
9894 
9895 	if (turn_on & IFF_NOFAILOVER) {
9896 		turn_on |= IFF_DEPRECATED;
9897 		flags |= IFF_DEPRECATED;
9898 	}
9899 
9900 	/*
9901 	 * On underlying interfaces, only allow applications to manage test
9902 	 * addresses -- otherwise, they may get confused when the address
9903 	 * moves as part of being brought up.  Likewise, prevent an
9904 	 * application-managed test address from being converted to a data
9905 	 * address.  To prevent migration of administratively up addresses in
9906 	 * the kernel, we don't allow them to be converted either.
9907 	 */
9908 	if (IS_UNDER_IPMP(ill)) {
9909 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
9910 
9911 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
9912 			return (EINVAL);
9913 
9914 		if ((turn_off & IFF_NOFAILOVER) &&
9915 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
9916 			return (EINVAL);
9917 	}
9918 
9919 	/*
9920 	 * Only allow IFF_TEMPORARY flag to be set on
9921 	 * IPv6 interfaces.
9922 	 */
9923 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
9924 		return (EINVAL);
9925 
9926 	/*
9927 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
9928 	 */
9929 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
9930 		return (EINVAL);
9931 
9932 	/*
9933 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
9934 	 * interfaces.  It makes no sense in that context.
9935 	 */
9936 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
9937 		return (EINVAL);
9938 
9939 	/*
9940 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
9941 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
9942 	 * If the link local address isn't set, and can be set, it will get
9943 	 * set later on in this function.
9944 	 */
9945 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
9946 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
9947 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
9948 		if (ipif_cant_setlinklocal(ipif))
9949 			return (EINVAL);
9950 		set_linklocal = B_TRUE;
9951 	}
9952 
9953 	/*
9954 	 * If we modify physical interface flags, we'll potentially need to
9955 	 * send up two routing socket messages for the changes (one for the
9956 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
9957 	 */
9958 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
9959 		phyint_flags_modified = B_TRUE;
9960 
9961 	/*
9962 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
9963 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
9964 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
9965 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
9966 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
9967 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
9968 	 * will not be honored.
9969 	 */
9970 	if (turn_on & PHYI_STANDBY) {
9971 		/*
9972 		 * No need to grab ill_g_usesrc_lock here; see the
9973 		 * synchronization notes in ip.c.
9974 		 */
9975 		if (ill->ill_usesrc_grp_next != NULL ||
9976 		    intf_flags & PHYI_INACTIVE)
9977 			return (EINVAL);
9978 		if (!(flags & PHYI_FAILED)) {
9979 			flags |= PHYI_INACTIVE;
9980 			turn_on |= PHYI_INACTIVE;
9981 		}
9982 	}
9983 
9984 	if (turn_off & PHYI_STANDBY) {
9985 		flags &= ~PHYI_INACTIVE;
9986 		turn_off |= PHYI_INACTIVE;
9987 	}
9988 
9989 	/*
9990 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
9991 	 * would end up on.
9992 	 */
9993 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
9994 	    (PHYI_FAILED | PHYI_INACTIVE))
9995 		return (EINVAL);
9996 
9997 	/*
9998 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
9999 	 * status of the interface.
10000 	 */
10001 	if ((turn_on | turn_off) & ILLF_ROUTER)
10002 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10003 
10004 	/*
10005 	 * If the interface is not UP and we are not going to
10006 	 * bring it UP, record the flags and return. When the
10007 	 * interface comes UP later, the right actions will be
10008 	 * taken.
10009 	 */
10010 	if (!(ipif->ipif_flags & IPIF_UP) &&
10011 	    !(turn_on & IPIF_UP)) {
10012 		/* Record new flags in their respective places. */
10013 		mutex_enter(&ill->ill_lock);
10014 		mutex_enter(&ill->ill_phyint->phyint_lock);
10015 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10016 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10017 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10018 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10019 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10020 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10021 		mutex_exit(&ill->ill_lock);
10022 		mutex_exit(&ill->ill_phyint->phyint_lock);
10023 
10024 		/*
10025 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10026 		 * same to the kernel: if any of them has been set by
10027 		 * userland, the interface cannot be used for data traffic.
10028 		 */
10029 		if ((turn_on|turn_off) &
10030 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10031 			ASSERT(!IS_IPMP(ill));
10032 			/*
10033 			 * It's possible the ill is part of an "anonymous"
10034 			 * IPMP group rather than a real group.  In that case,
10035 			 * there are no other interfaces in the group and thus
10036 			 * no need to call ipmp_phyint_refresh_active().
10037 			 */
10038 			if (IS_UNDER_IPMP(ill))
10039 				ipmp_phyint_refresh_active(phyi);
10040 		}
10041 
10042 		if (phyint_flags_modified) {
10043 			if (phyi->phyint_illv4 != NULL) {
10044 				ip_rts_ifmsg(phyi->phyint_illv4->
10045 				    ill_ipif, RTSQ_DEFAULT);
10046 			}
10047 			if (phyi->phyint_illv6 != NULL) {
10048 				ip_rts_ifmsg(phyi->phyint_illv6->
10049 				    ill_ipif, RTSQ_DEFAULT);
10050 			}
10051 		}
10052 		/* The default multicast interface might have changed */
10053 		ire_increment_multicast_generation(ill->ill_ipst,
10054 		    ill->ill_isv6);
10055 
10056 		return (0);
10057 	} else if (set_linklocal) {
10058 		mutex_enter(&ill->ill_lock);
10059 		if (set_linklocal)
10060 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10061 		mutex_exit(&ill->ill_lock);
10062 	}
10063 
10064 	/*
10065 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10066 	 * or point-to-point interfaces with an unspecified destination. We do
10067 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10068 	 * have a subnet assigned, which is how in.ndpd currently manages its
10069 	 * onlink prefix list when no addresses are configured with those
10070 	 * prefixes.
10071 	 */
10072 	if (ipif->ipif_isv6 &&
10073 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10074 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10075 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10076 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10077 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10078 		return (EINVAL);
10079 	}
10080 
10081 	/*
10082 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10083 	 * from being brought up.
10084 	 */
10085 	if (!ipif->ipif_isv6 &&
10086 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10087 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10088 		return (EINVAL);
10089 	}
10090 
10091 	/*
10092 	 * The only flag changes that we currently take specific action on are
10093 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10094 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10095 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
10096 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
10097 	 * of bringing it back up will trigger the address to be moved.
10098 	 */
10099 	if ((turn_on|turn_off) &
10100 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10101 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10102 	    IPIF_NOFAILOVER)) {
10103 		/*
10104 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10105 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10106 		 * entries shared between multiple ipifs on the same subnet.
10107 		 */
10108 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10109 		    !(turn_off & IPIF_UP)) {
10110 			if (ipif->ipif_flags & IPIF_UP)
10111 				ill->ill_logical_down = 1;
10112 			turn_on &= ~IPIF_UP;
10113 		}
10114 		err = ipif_down(ipif, q, mp);
10115 		ip1dbg(("ipif_down returns %d err ", err));
10116 		if (err == EINPROGRESS)
10117 			return (err);
10118 		(void) ipif_down_tail(ipif);
10119 	}
10120 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10121 }
10122 
10123 static int
10124 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10125 {
10126 	ill_t	*ill;
10127 	phyint_t *phyi;
10128 	uint64_t turn_on, turn_off;
10129 	uint64_t intf_flags, cantchange_flags;
10130 	boolean_t phyint_flags_modified = B_FALSE;
10131 	int	err = 0;
10132 	boolean_t set_linklocal = B_FALSE;
10133 
10134 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10135 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10136 
10137 	ASSERT(IAM_WRITER_IPIF(ipif));
10138 
10139 	ill = ipif->ipif_ill;
10140 	phyi = ill->ill_phyint;
10141 
10142 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10143 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
10144 	if (IS_IPMP(ill))
10145 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10146 
10147 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10148 	turn_off = intf_flags & turn_on;
10149 	turn_on ^= turn_off;
10150 
10151 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10152 		phyint_flags_modified = B_TRUE;
10153 
10154 	/*
10155 	 * Now we change the flags. Track current value of
10156 	 * other flags in their respective places.
10157 	 */
10158 	mutex_enter(&ill->ill_lock);
10159 	mutex_enter(&phyi->phyint_lock);
10160 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10161 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10162 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10163 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10164 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10165 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10166 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10167 		set_linklocal = B_TRUE;
10168 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10169 	}
10170 
10171 	mutex_exit(&ill->ill_lock);
10172 	mutex_exit(&phyi->phyint_lock);
10173 
10174 	if (set_linklocal)
10175 		(void) ipif_setlinklocal(ipif);
10176 
10177 	/*
10178 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10179 	 * the kernel: if any of them has been set by userland, the interface
10180 	 * cannot be used for data traffic.
10181 	 */
10182 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10183 		ASSERT(!IS_IPMP(ill));
10184 		/*
10185 		 * It's possible the ill is part of an "anonymous" IPMP group
10186 		 * rather than a real group.  In that case, there are no other
10187 		 * interfaces in the group and thus no need for us to call
10188 		 * ipmp_phyint_refresh_active().
10189 		 */
10190 		if (IS_UNDER_IPMP(ill))
10191 			ipmp_phyint_refresh_active(phyi);
10192 	}
10193 
10194 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10195 		/*
10196 		 * XXX ipif_up really does not know whether a phyint flags
10197 		 * was modified or not. So, it sends up information on
10198 		 * only one routing sockets message. As we don't bring up
10199 		 * the interface and also set PHYI_ flags simultaneously
10200 		 * it should be okay.
10201 		 */
10202 		err = ipif_up(ipif, q, mp);
10203 	} else {
10204 		/*
10205 		 * Make sure routing socket sees all changes to the flags.
10206 		 * ipif_up_done* handles this when we use ipif_up.
10207 		 */
10208 		if (phyint_flags_modified) {
10209 			if (phyi->phyint_illv4 != NULL) {
10210 				ip_rts_ifmsg(phyi->phyint_illv4->
10211 				    ill_ipif, RTSQ_DEFAULT);
10212 			}
10213 			if (phyi->phyint_illv6 != NULL) {
10214 				ip_rts_ifmsg(phyi->phyint_illv6->
10215 				    ill_ipif, RTSQ_DEFAULT);
10216 			}
10217 		} else {
10218 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10219 		}
10220 		/*
10221 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10222 		 * this in need_up case.
10223 		 */
10224 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10225 	}
10226 
10227 	/* The default multicast interface might have changed */
10228 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10229 	return (err);
10230 }
10231 
10232 /*
10233  * Restart the flags operation now that the refcounts have dropped to zero.
10234  */
10235 /* ARGSUSED */
10236 int
10237 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10238     ip_ioctl_cmd_t *ipip, void *if_req)
10239 {
10240 	uint64_t flags;
10241 	struct ifreq *ifr = if_req;
10242 	struct lifreq *lifr = if_req;
10243 
10244 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10245 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10246 
10247 	(void) ipif_down_tail(ipif);
10248 	if (ipip->ipi_cmd_type == IF_CMD) {
10249 		/* cast to uint16_t prevents unwanted sign extension */
10250 		flags = (uint16_t)ifr->ifr_flags;
10251 	} else {
10252 		flags = lifr->lifr_flags;
10253 	}
10254 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10255 }
10256 
10257 /*
10258  * Can operate on either a module or a driver queue.
10259  */
10260 /* ARGSUSED */
10261 int
10262 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10263     ip_ioctl_cmd_t *ipip, void *if_req)
10264 {
10265 	/*
10266 	 * Has the flags been set correctly till now ?
10267 	 */
10268 	ill_t *ill = ipif->ipif_ill;
10269 	phyint_t *phyi = ill->ill_phyint;
10270 
10271 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10272 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10273 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10274 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10275 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10276 
10277 	/*
10278 	 * Need a lock since some flags can be set even when there are
10279 	 * references to the ipif.
10280 	 */
10281 	mutex_enter(&ill->ill_lock);
10282 	if (ipip->ipi_cmd_type == IF_CMD) {
10283 		struct ifreq *ifr = (struct ifreq *)if_req;
10284 
10285 		/* Get interface flags (low 16 only). */
10286 		ifr->ifr_flags = ((ipif->ipif_flags |
10287 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10288 	} else {
10289 		struct lifreq *lifr = (struct lifreq *)if_req;
10290 
10291 		/* Get interface flags. */
10292 		lifr->lifr_flags = ipif->ipif_flags |
10293 		    ill->ill_flags | phyi->phyint_flags;
10294 	}
10295 	mutex_exit(&ill->ill_lock);
10296 	return (0);
10297 }
10298 
10299 /*
10300  * We allow the MTU to be set on an ILL, but not have it be different
10301  * for different IPIFs since we don't actually send packets on IPIFs.
10302  */
10303 /* ARGSUSED */
10304 int
10305 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10306     ip_ioctl_cmd_t *ipip, void *if_req)
10307 {
10308 	int mtu;
10309 	int ip_min_mtu;
10310 	struct ifreq	*ifr;
10311 	struct lifreq *lifr;
10312 	ill_t	*ill;
10313 
10314 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10315 	    ipif->ipif_id, (void *)ipif));
10316 	if (ipip->ipi_cmd_type == IF_CMD) {
10317 		ifr = (struct ifreq *)if_req;
10318 		mtu = ifr->ifr_metric;
10319 	} else {
10320 		lifr = (struct lifreq *)if_req;
10321 		mtu = lifr->lifr_mtu;
10322 	}
10323 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10324 	if (ipif->ipif_id != 0)
10325 		return (EINVAL);
10326 
10327 	ill = ipif->ipif_ill;
10328 	if (ipif->ipif_isv6)
10329 		ip_min_mtu = IPV6_MIN_MTU;
10330 	else
10331 		ip_min_mtu = IP_MIN_MTU;
10332 
10333 	mutex_enter(&ill->ill_lock);
10334 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10335 		mutex_exit(&ill->ill_lock);
10336 		return (EINVAL);
10337 	}
10338 	/*
10339 	 * The dce and fragmentation code can handle changes to ill_mtu
10340 	 * concurrent with sending/fragmenting packets.
10341 	 */
10342 	ill->ill_mtu = mtu;
10343 	ill->ill_flags |= ILLF_FIXEDMTU;
10344 	mutex_exit(&ill->ill_lock);
10345 
10346 	/*
10347 	 * Make sure all dce_generation checks find out
10348 	 * that ill_mtu has changed.
10349 	 */
10350 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10351 
10352 	/* Update the MTU in SCTP's list */
10353 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10354 	return (0);
10355 }
10356 
10357 /* Get interface MTU. */
10358 /* ARGSUSED */
10359 int
10360 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10361 	ip_ioctl_cmd_t *ipip, void *if_req)
10362 {
10363 	struct ifreq	*ifr;
10364 	struct lifreq	*lifr;
10365 
10366 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10367 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10368 
10369 	/*
10370 	 * We allow a get on any logical interface even though the set
10371 	 * can only be done on logical unit 0.
10372 	 */
10373 	if (ipip->ipi_cmd_type == IF_CMD) {
10374 		ifr = (struct ifreq *)if_req;
10375 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10376 	} else {
10377 		lifr = (struct lifreq *)if_req;
10378 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10379 	}
10380 	return (0);
10381 }
10382 
10383 /* Set interface broadcast address. */
10384 /* ARGSUSED2 */
10385 int
10386 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10387 	ip_ioctl_cmd_t *ipip, void *if_req)
10388 {
10389 	ipaddr_t addr;
10390 	ire_t	*ire;
10391 	ill_t		*ill = ipif->ipif_ill;
10392 	ip_stack_t	*ipst = ill->ill_ipst;
10393 
10394 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10395 	    ipif->ipif_id));
10396 
10397 	ASSERT(IAM_WRITER_IPIF(ipif));
10398 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10399 		return (EADDRNOTAVAIL);
10400 
10401 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10402 
10403 	if (sin->sin_family != AF_INET)
10404 		return (EAFNOSUPPORT);
10405 
10406 	addr = sin->sin_addr.s_addr;
10407 	if (ipif->ipif_flags & IPIF_UP) {
10408 		/*
10409 		 * If we are already up, make sure the new
10410 		 * broadcast address makes sense.  If it does,
10411 		 * there should be an IRE for it already.
10412 		 */
10413 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10414 		    ill, ipif->ipif_zoneid, NULL,
10415 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10416 		if (ire == NULL) {
10417 			return (EINVAL);
10418 		} else {
10419 			ire_refrele(ire);
10420 		}
10421 	}
10422 	/*
10423 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10424 	 * needs to already exist we never need to change the set of
10425 	 * IRE_BROADCASTs when we are UP.
10426 	 */
10427 	if (addr != ipif->ipif_brd_addr)
10428 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10429 
10430 	return (0);
10431 }
10432 
10433 /* Get interface broadcast address. */
10434 /* ARGSUSED */
10435 int
10436 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10437     ip_ioctl_cmd_t *ipip, void *if_req)
10438 {
10439 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10440 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10441 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10442 		return (EADDRNOTAVAIL);
10443 
10444 	/* IPIF_BROADCAST not possible with IPv6 */
10445 	ASSERT(!ipif->ipif_isv6);
10446 	*sin = sin_null;
10447 	sin->sin_family = AF_INET;
10448 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10449 	return (0);
10450 }
10451 
10452 /*
10453  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10454  */
10455 /* ARGSUSED */
10456 int
10457 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10458     ip_ioctl_cmd_t *ipip, void *if_req)
10459 {
10460 	int err = 0;
10461 	in6_addr_t v6mask;
10462 
10463 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10464 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10465 
10466 	ASSERT(IAM_WRITER_IPIF(ipif));
10467 
10468 	if (ipif->ipif_isv6) {
10469 		sin6_t *sin6;
10470 
10471 		if (sin->sin_family != AF_INET6)
10472 			return (EAFNOSUPPORT);
10473 
10474 		sin6 = (sin6_t *)sin;
10475 		v6mask = sin6->sin6_addr;
10476 	} else {
10477 		ipaddr_t mask;
10478 
10479 		if (sin->sin_family != AF_INET)
10480 			return (EAFNOSUPPORT);
10481 
10482 		mask = sin->sin_addr.s_addr;
10483 		V4MASK_TO_V6(mask, v6mask);
10484 	}
10485 
10486 	/*
10487 	 * No big deal if the interface isn't already up, or the mask
10488 	 * isn't really changing, or this is pt-pt.
10489 	 */
10490 	if (!(ipif->ipif_flags & IPIF_UP) ||
10491 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10492 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10493 		ipif->ipif_v6net_mask = v6mask;
10494 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10495 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10496 			    ipif->ipif_v6net_mask,
10497 			    ipif->ipif_v6subnet);
10498 		}
10499 		return (0);
10500 	}
10501 	/*
10502 	 * Make sure we have valid net and subnet broadcast ire's
10503 	 * for the old netmask, if needed by other logical interfaces.
10504 	 */
10505 	err = ipif_logical_down(ipif, q, mp);
10506 	if (err == EINPROGRESS)
10507 		return (err);
10508 	(void) ipif_down_tail(ipif);
10509 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10510 	return (err);
10511 }
10512 
10513 static int
10514 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10515 {
10516 	in6_addr_t v6mask;
10517 	int err = 0;
10518 
10519 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10520 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10521 
10522 	if (ipif->ipif_isv6) {
10523 		sin6_t *sin6;
10524 
10525 		sin6 = (sin6_t *)sin;
10526 		v6mask = sin6->sin6_addr;
10527 	} else {
10528 		ipaddr_t mask;
10529 
10530 		mask = sin->sin_addr.s_addr;
10531 		V4MASK_TO_V6(mask, v6mask);
10532 	}
10533 
10534 	ipif->ipif_v6net_mask = v6mask;
10535 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10536 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10537 		    ipif->ipif_v6subnet);
10538 	}
10539 	err = ipif_up(ipif, q, mp);
10540 
10541 	if (err == 0 || err == EINPROGRESS) {
10542 		/*
10543 		 * The interface must be DL_BOUND if this packet has to
10544 		 * go out on the wire. Since we only go through a logical
10545 		 * down and are bound with the driver during an internal
10546 		 * down/up that is satisfied.
10547 		 */
10548 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10549 			/* Potentially broadcast an address mask reply. */
10550 			ipif_mask_reply(ipif);
10551 		}
10552 	}
10553 	return (err);
10554 }
10555 
10556 /* ARGSUSED */
10557 int
10558 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10559     ip_ioctl_cmd_t *ipip, void *if_req)
10560 {
10561 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10562 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10563 	(void) ipif_down_tail(ipif);
10564 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10565 }
10566 
10567 /* Get interface net mask. */
10568 /* ARGSUSED */
10569 int
10570 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10571     ip_ioctl_cmd_t *ipip, void *if_req)
10572 {
10573 	struct lifreq *lifr = (struct lifreq *)if_req;
10574 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10575 
10576 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10577 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10578 
10579 	/*
10580 	 * net mask can't change since we have a reference to the ipif.
10581 	 */
10582 	if (ipif->ipif_isv6) {
10583 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10584 		*sin6 = sin6_null;
10585 		sin6->sin6_family = AF_INET6;
10586 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10587 		lifr->lifr_addrlen =
10588 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10589 	} else {
10590 		*sin = sin_null;
10591 		sin->sin_family = AF_INET;
10592 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10593 		if (ipip->ipi_cmd_type == LIF_CMD) {
10594 			lifr->lifr_addrlen =
10595 			    ip_mask_to_plen(ipif->ipif_net_mask);
10596 		}
10597 	}
10598 	return (0);
10599 }
10600 
10601 /* ARGSUSED */
10602 int
10603 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10604     ip_ioctl_cmd_t *ipip, void *if_req)
10605 {
10606 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10607 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10608 
10609 	/*
10610 	 * Since no applications should ever be setting metrics on underlying
10611 	 * interfaces, we explicitly fail to smoke 'em out.
10612 	 */
10613 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10614 		return (EINVAL);
10615 
10616 	/*
10617 	 * Set interface metric.  We don't use this for
10618 	 * anything but we keep track of it in case it is
10619 	 * important to routing applications or such.
10620 	 */
10621 	if (ipip->ipi_cmd_type == IF_CMD) {
10622 		struct ifreq    *ifr;
10623 
10624 		ifr = (struct ifreq *)if_req;
10625 		ipif->ipif_metric = ifr->ifr_metric;
10626 	} else {
10627 		struct lifreq   *lifr;
10628 
10629 		lifr = (struct lifreq *)if_req;
10630 		ipif->ipif_metric = lifr->lifr_metric;
10631 	}
10632 	return (0);
10633 }
10634 
10635 /* ARGSUSED */
10636 int
10637 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10638     ip_ioctl_cmd_t *ipip, void *if_req)
10639 {
10640 	/* Get interface metric. */
10641 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10642 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10643 
10644 	if (ipip->ipi_cmd_type == IF_CMD) {
10645 		struct ifreq    *ifr;
10646 
10647 		ifr = (struct ifreq *)if_req;
10648 		ifr->ifr_metric = ipif->ipif_metric;
10649 	} else {
10650 		struct lifreq   *lifr;
10651 
10652 		lifr = (struct lifreq *)if_req;
10653 		lifr->lifr_metric = ipif->ipif_metric;
10654 	}
10655 
10656 	return (0);
10657 }
10658 
10659 /* ARGSUSED */
10660 int
10661 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10662     ip_ioctl_cmd_t *ipip, void *if_req)
10663 {
10664 	int	arp_muxid;
10665 
10666 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10668 	/*
10669 	 * Set the muxid returned from I_PLINK.
10670 	 */
10671 	if (ipip->ipi_cmd_type == IF_CMD) {
10672 		struct ifreq *ifr = (struct ifreq *)if_req;
10673 
10674 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10675 		arp_muxid = ifr->ifr_arp_muxid;
10676 	} else {
10677 		struct lifreq *lifr = (struct lifreq *)if_req;
10678 
10679 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10680 		arp_muxid = lifr->lifr_arp_muxid;
10681 	}
10682 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10683 	return (0);
10684 }
10685 
10686 /* ARGSUSED */
10687 int
10688 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10689     ip_ioctl_cmd_t *ipip, void *if_req)
10690 {
10691 	int	arp_muxid = 0;
10692 
10693 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10695 	/*
10696 	 * Get the muxid saved in ill for I_PUNLINK.
10697 	 */
10698 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10699 	if (ipip->ipi_cmd_type == IF_CMD) {
10700 		struct ifreq *ifr = (struct ifreq *)if_req;
10701 
10702 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10703 		ifr->ifr_arp_muxid = arp_muxid;
10704 	} else {
10705 		struct lifreq *lifr = (struct lifreq *)if_req;
10706 
10707 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10708 		lifr->lifr_arp_muxid = arp_muxid;
10709 	}
10710 	return (0);
10711 }
10712 
10713 /*
10714  * Set the subnet prefix. Does not modify the broadcast address.
10715  */
10716 /* ARGSUSED */
10717 int
10718 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10719     ip_ioctl_cmd_t *ipip, void *if_req)
10720 {
10721 	int err = 0;
10722 	in6_addr_t v6addr;
10723 	in6_addr_t v6mask;
10724 	boolean_t need_up = B_FALSE;
10725 	int addrlen;
10726 
10727 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10728 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10729 
10730 	ASSERT(IAM_WRITER_IPIF(ipif));
10731 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10732 
10733 	if (ipif->ipif_isv6) {
10734 		sin6_t *sin6;
10735 
10736 		if (sin->sin_family != AF_INET6)
10737 			return (EAFNOSUPPORT);
10738 
10739 		sin6 = (sin6_t *)sin;
10740 		v6addr = sin6->sin6_addr;
10741 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10742 			return (EADDRNOTAVAIL);
10743 	} else {
10744 		ipaddr_t addr;
10745 
10746 		if (sin->sin_family != AF_INET)
10747 			return (EAFNOSUPPORT);
10748 
10749 		addr = sin->sin_addr.s_addr;
10750 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10751 			return (EADDRNOTAVAIL);
10752 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10753 		/* Add 96 bits */
10754 		addrlen += IPV6_ABITS - IP_ABITS;
10755 	}
10756 
10757 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10758 		return (EINVAL);
10759 
10760 	/* Check if bits in the address is set past the mask */
10761 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10762 		return (EINVAL);
10763 
10764 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10765 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10766 		return (0);	/* No change */
10767 
10768 	if (ipif->ipif_flags & IPIF_UP) {
10769 		/*
10770 		 * If the interface is already marked up,
10771 		 * we call ipif_down which will take care
10772 		 * of ditching any IREs that have been set
10773 		 * up based on the old interface address.
10774 		 */
10775 		err = ipif_logical_down(ipif, q, mp);
10776 		if (err == EINPROGRESS)
10777 			return (err);
10778 		(void) ipif_down_tail(ipif);
10779 		need_up = B_TRUE;
10780 	}
10781 
10782 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10783 	return (err);
10784 }
10785 
10786 static int
10787 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10788     queue_t *q, mblk_t *mp, boolean_t need_up)
10789 {
10790 	ill_t	*ill = ipif->ipif_ill;
10791 	int	err = 0;
10792 
10793 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10794 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10795 
10796 	/* Set the new address. */
10797 	mutex_enter(&ill->ill_lock);
10798 	ipif->ipif_v6net_mask = v6mask;
10799 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10800 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10801 		    ipif->ipif_v6subnet);
10802 	}
10803 	mutex_exit(&ill->ill_lock);
10804 
10805 	if (need_up) {
10806 		/*
10807 		 * Now bring the interface back up.  If this
10808 		 * is the only IPIF for the ILL, ipif_up
10809 		 * will have to re-bind to the device, so
10810 		 * we may get back EINPROGRESS, in which
10811 		 * case, this IOCTL will get completed in
10812 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10813 		 */
10814 		err = ipif_up(ipif, q, mp);
10815 		if (err == EINPROGRESS)
10816 			return (err);
10817 	}
10818 	return (err);
10819 }
10820 
10821 /* ARGSUSED */
10822 int
10823 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10824     ip_ioctl_cmd_t *ipip, void *if_req)
10825 {
10826 	int	addrlen;
10827 	in6_addr_t v6addr;
10828 	in6_addr_t v6mask;
10829 	struct lifreq *lifr = (struct lifreq *)if_req;
10830 
10831 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
10832 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10833 	(void) ipif_down_tail(ipif);
10834 
10835 	addrlen = lifr->lifr_addrlen;
10836 	if (ipif->ipif_isv6) {
10837 		sin6_t *sin6;
10838 
10839 		sin6 = (sin6_t *)sin;
10840 		v6addr = sin6->sin6_addr;
10841 	} else {
10842 		ipaddr_t addr;
10843 
10844 		addr = sin->sin_addr.s_addr;
10845 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10846 		addrlen += IPV6_ABITS - IP_ABITS;
10847 	}
10848 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
10849 
10850 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
10851 }
10852 
10853 /* ARGSUSED */
10854 int
10855 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10856     ip_ioctl_cmd_t *ipip, void *if_req)
10857 {
10858 	struct lifreq *lifr = (struct lifreq *)if_req;
10859 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
10860 
10861 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
10862 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10863 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10864 
10865 	if (ipif->ipif_isv6) {
10866 		*sin6 = sin6_null;
10867 		sin6->sin6_family = AF_INET6;
10868 		sin6->sin6_addr = ipif->ipif_v6subnet;
10869 		lifr->lifr_addrlen =
10870 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10871 	} else {
10872 		*sin = sin_null;
10873 		sin->sin_family = AF_INET;
10874 		sin->sin_addr.s_addr = ipif->ipif_subnet;
10875 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
10876 	}
10877 	return (0);
10878 }
10879 
10880 /*
10881  * Set the IPv6 address token.
10882  */
10883 /* ARGSUSED */
10884 int
10885 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10886     ip_ioctl_cmd_t *ipi, void *if_req)
10887 {
10888 	ill_t *ill = ipif->ipif_ill;
10889 	int err;
10890 	in6_addr_t v6addr;
10891 	in6_addr_t v6mask;
10892 	boolean_t need_up = B_FALSE;
10893 	int i;
10894 	sin6_t *sin6 = (sin6_t *)sin;
10895 	struct lifreq *lifr = (struct lifreq *)if_req;
10896 	int addrlen;
10897 
10898 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
10899 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10900 	ASSERT(IAM_WRITER_IPIF(ipif));
10901 
10902 	addrlen = lifr->lifr_addrlen;
10903 	/* Only allow for logical unit zero i.e. not on "le0:17" */
10904 	if (ipif->ipif_id != 0)
10905 		return (EINVAL);
10906 
10907 	if (!ipif->ipif_isv6)
10908 		return (EINVAL);
10909 
10910 	if (addrlen > IPV6_ABITS)
10911 		return (EINVAL);
10912 
10913 	v6addr = sin6->sin6_addr;
10914 
10915 	/*
10916 	 * The length of the token is the length from the end.  To get
10917 	 * the proper mask for this, compute the mask of the bits not
10918 	 * in the token; ie. the prefix, and then xor to get the mask.
10919 	 */
10920 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
10921 		return (EINVAL);
10922 	for (i = 0; i < 4; i++) {
10923 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
10924 	}
10925 
10926 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
10927 	    ill->ill_token_length == addrlen)
10928 		return (0);	/* No change */
10929 
10930 	if (ipif->ipif_flags & IPIF_UP) {
10931 		err = ipif_logical_down(ipif, q, mp);
10932 		if (err == EINPROGRESS)
10933 			return (err);
10934 		(void) ipif_down_tail(ipif);
10935 		need_up = B_TRUE;
10936 	}
10937 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
10938 	return (err);
10939 }
10940 
10941 static int
10942 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
10943     mblk_t *mp, boolean_t need_up)
10944 {
10945 	in6_addr_t v6addr;
10946 	in6_addr_t v6mask;
10947 	ill_t	*ill = ipif->ipif_ill;
10948 	int	i;
10949 	int	err = 0;
10950 
10951 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
10952 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10953 	v6addr = sin6->sin6_addr;
10954 	/*
10955 	 * The length of the token is the length from the end.  To get
10956 	 * the proper mask for this, compute the mask of the bits not
10957 	 * in the token; ie. the prefix, and then xor to get the mask.
10958 	 */
10959 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
10960 	for (i = 0; i < 4; i++)
10961 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
10962 
10963 	mutex_enter(&ill->ill_lock);
10964 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
10965 	ill->ill_token_length = addrlen;
10966 	ill->ill_manual_token = 1;
10967 
10968 	/* Reconfigure the link-local address based on this new token */
10969 	ipif_setlinklocal(ill->ill_ipif);
10970 
10971 	mutex_exit(&ill->ill_lock);
10972 
10973 	if (need_up) {
10974 		/*
10975 		 * Now bring the interface back up.  If this
10976 		 * is the only IPIF for the ILL, ipif_up
10977 		 * will have to re-bind to the device, so
10978 		 * we may get back EINPROGRESS, in which
10979 		 * case, this IOCTL will get completed in
10980 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10981 		 */
10982 		err = ipif_up(ipif, q, mp);
10983 		if (err == EINPROGRESS)
10984 			return (err);
10985 	}
10986 	return (err);
10987 }
10988 
10989 /* ARGSUSED */
10990 int
10991 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10992     ip_ioctl_cmd_t *ipi, void *if_req)
10993 {
10994 	ill_t *ill;
10995 	sin6_t *sin6 = (sin6_t *)sin;
10996 	struct lifreq *lifr = (struct lifreq *)if_req;
10997 
10998 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
10999 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11000 	if (ipif->ipif_id != 0)
11001 		return (EINVAL);
11002 
11003 	ill = ipif->ipif_ill;
11004 	if (!ill->ill_isv6)
11005 		return (ENXIO);
11006 
11007 	*sin6 = sin6_null;
11008 	sin6->sin6_family = AF_INET6;
11009 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11010 	sin6->sin6_addr = ill->ill_token;
11011 	lifr->lifr_addrlen = ill->ill_token_length;
11012 	return (0);
11013 }
11014 
11015 /*
11016  * Set (hardware) link specific information that might override
11017  * what was acquired through the DL_INFO_ACK.
11018  */
11019 /* ARGSUSED */
11020 int
11021 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11022     ip_ioctl_cmd_t *ipi, void *if_req)
11023 {
11024 	ill_t		*ill = ipif->ipif_ill;
11025 	int		ip_min_mtu;
11026 	struct lifreq	*lifr = (struct lifreq *)if_req;
11027 	lif_ifinfo_req_t *lir;
11028 
11029 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11030 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11031 	lir = &lifr->lifr_ifinfo;
11032 	ASSERT(IAM_WRITER_IPIF(ipif));
11033 
11034 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11035 	if (ipif->ipif_id != 0)
11036 		return (EINVAL);
11037 
11038 	/* Set interface MTU. */
11039 	if (ipif->ipif_isv6)
11040 		ip_min_mtu = IPV6_MIN_MTU;
11041 	else
11042 		ip_min_mtu = IP_MIN_MTU;
11043 
11044 	/*
11045 	 * Verify values before we set anything. Allow zero to
11046 	 * mean unspecified.
11047 	 *
11048 	 * XXX We should be able to set the user-defined lir_mtu to some value
11049 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11050 	 * ill_max_frag value tells us the max MTU that can be handled by the
11051 	 * datalink, whereas the ill_current_frag is dynamically computed for
11052 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11053 	 * since there is currently no way of distinguishing between
11054 	 * administratively fixed link mtu values (e.g., those set via
11055 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11056 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11057 	 * upper-bound.
11058 	 */
11059 	if (lir->lir_maxmtu != 0 &&
11060 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11061 	    lir->lir_maxmtu < ip_min_mtu))
11062 		return (EINVAL);
11063 	if (lir->lir_reachtime != 0 &&
11064 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11065 		return (EINVAL);
11066 	if (lir->lir_reachretrans != 0 &&
11067 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11068 		return (EINVAL);
11069 
11070 	mutex_enter(&ill->ill_lock);
11071 	/*
11072 	 * The dce and fragmentation code can handle changes to ill_mtu
11073 	 * concurrent with sending/fragmenting packets.
11074 	 */
11075 	if (lir->lir_maxmtu != 0)
11076 		ill->ill_user_mtu = lir->lir_maxmtu;
11077 
11078 	if (lir->lir_reachtime != 0)
11079 		ill->ill_reachable_time = lir->lir_reachtime;
11080 
11081 	if (lir->lir_reachretrans != 0)
11082 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11083 
11084 	ill->ill_max_hops = lir->lir_maxhops;
11085 	ill->ill_max_buf = ND_MAX_Q;
11086 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11087 		/*
11088 		 * ill_mtu is the actual interface MTU, obtained as the min
11089 		 * of user-configured mtu and the value announced by the
11090 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11091 		 * we have already made the choice of requiring
11092 		 * ill_user_mtu < ill_current_frag by the time we get here,
11093 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11094 		 * here.
11095 		 */
11096 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11097 	}
11098 	mutex_exit(&ill->ill_lock);
11099 
11100 	/*
11101 	 * Make sure all dce_generation checks find out
11102 	 * that ill_mtu has changed.
11103 	 */
11104 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11105 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11106 
11107 	/*
11108 	 * Refresh IPMP meta-interface MTU if necessary.
11109 	 */
11110 	if (IS_UNDER_IPMP(ill))
11111 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11112 
11113 	return (0);
11114 }
11115 
11116 /* ARGSUSED */
11117 int
11118 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11119     ip_ioctl_cmd_t *ipi, void *if_req)
11120 {
11121 	struct lif_ifinfo_req *lir;
11122 	ill_t *ill = ipif->ipif_ill;
11123 
11124 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11125 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11126 	if (ipif->ipif_id != 0)
11127 		return (EINVAL);
11128 
11129 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11130 	lir->lir_maxhops = ill->ill_max_hops;
11131 	lir->lir_reachtime = ill->ill_reachable_time;
11132 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11133 	lir->lir_maxmtu = ill->ill_mtu;
11134 
11135 	return (0);
11136 }
11137 
11138 /*
11139  * Return best guess as to the subnet mask for the specified address.
11140  * Based on the subnet masks for all the configured interfaces.
11141  *
11142  * We end up returning a zero mask in the case of default, multicast or
11143  * experimental.
11144  */
11145 static ipaddr_t
11146 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11147 {
11148 	ipaddr_t net_mask;
11149 	ill_t	*ill;
11150 	ipif_t	*ipif;
11151 	ill_walk_context_t ctx;
11152 	ipif_t	*fallback_ipif = NULL;
11153 
11154 	net_mask = ip_net_mask(addr);
11155 	if (net_mask == 0) {
11156 		*ipifp = NULL;
11157 		return (0);
11158 	}
11159 
11160 	/* Let's check to see if this is maybe a local subnet route. */
11161 	/* this function only applies to IPv4 interfaces */
11162 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11163 	ill = ILL_START_WALK_V4(&ctx, ipst);
11164 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11165 		mutex_enter(&ill->ill_lock);
11166 		for (ipif = ill->ill_ipif; ipif != NULL;
11167 		    ipif = ipif->ipif_next) {
11168 			if (IPIF_IS_CONDEMNED(ipif))
11169 				continue;
11170 			if (!(ipif->ipif_flags & IPIF_UP))
11171 				continue;
11172 			if ((ipif->ipif_subnet & net_mask) ==
11173 			    (addr & net_mask)) {
11174 				/*
11175 				 * Don't trust pt-pt interfaces if there are
11176 				 * other interfaces.
11177 				 */
11178 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11179 					if (fallback_ipif == NULL) {
11180 						ipif_refhold_locked(ipif);
11181 						fallback_ipif = ipif;
11182 					}
11183 					continue;
11184 				}
11185 
11186 				/*
11187 				 * Fine. Just assume the same net mask as the
11188 				 * directly attached subnet interface is using.
11189 				 */
11190 				ipif_refhold_locked(ipif);
11191 				mutex_exit(&ill->ill_lock);
11192 				rw_exit(&ipst->ips_ill_g_lock);
11193 				if (fallback_ipif != NULL)
11194 					ipif_refrele(fallback_ipif);
11195 				*ipifp = ipif;
11196 				return (ipif->ipif_net_mask);
11197 			}
11198 		}
11199 		mutex_exit(&ill->ill_lock);
11200 	}
11201 	rw_exit(&ipst->ips_ill_g_lock);
11202 
11203 	*ipifp = fallback_ipif;
11204 	return ((fallback_ipif != NULL) ?
11205 	    fallback_ipif->ipif_net_mask : net_mask);
11206 }
11207 
11208 /*
11209  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11210  */
11211 static void
11212 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11213 {
11214 	IOCP	iocp;
11215 	ipft_t	*ipft;
11216 	ipllc_t	*ipllc;
11217 	mblk_t	*mp1;
11218 	cred_t	*cr;
11219 	int	error = 0;
11220 	conn_t	*connp;
11221 
11222 	ip1dbg(("ip_wput_ioctl"));
11223 	iocp = (IOCP)mp->b_rptr;
11224 	mp1 = mp->b_cont;
11225 	if (mp1 == NULL) {
11226 		iocp->ioc_error = EINVAL;
11227 		mp->b_datap->db_type = M_IOCNAK;
11228 		iocp->ioc_count = 0;
11229 		qreply(q, mp);
11230 		return;
11231 	}
11232 
11233 	/*
11234 	 * These IOCTLs provide various control capabilities to
11235 	 * upstream agents such as ULPs and processes.	There
11236 	 * are currently two such IOCTLs implemented.  They
11237 	 * are used by TCP to provide update information for
11238 	 * existing IREs and to forcibly delete an IRE for a
11239 	 * host that is not responding, thereby forcing an
11240 	 * attempt at a new route.
11241 	 */
11242 	iocp->ioc_error = EINVAL;
11243 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11244 		goto done;
11245 
11246 	ipllc = (ipllc_t *)mp1->b_rptr;
11247 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11248 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11249 			break;
11250 	}
11251 	/*
11252 	 * prefer credential from mblk over ioctl;
11253 	 * see ip_sioctl_copyin_setup
11254 	 */
11255 	cr = msg_getcred(mp, NULL);
11256 	if (cr == NULL)
11257 		cr = iocp->ioc_cr;
11258 
11259 	/*
11260 	 * Refhold the conn in case the request gets queued up in some lookup
11261 	 */
11262 	ASSERT(CONN_Q(q));
11263 	connp = Q_TO_CONN(q);
11264 	CONN_INC_REF(connp);
11265 	if (ipft->ipft_pfi &&
11266 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11267 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11268 		error = (*ipft->ipft_pfi)(q,
11269 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11270 	}
11271 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11272 		/*
11273 		 * CONN_OPER_PENDING_DONE happens in the function called
11274 		 * through ipft_pfi above.
11275 		 */
11276 		return;
11277 	}
11278 
11279 	CONN_OPER_PENDING_DONE(connp);
11280 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11281 		freemsg(mp);
11282 		return;
11283 	}
11284 	iocp->ioc_error = error;
11285 
11286 done:
11287 	mp->b_datap->db_type = M_IOCACK;
11288 	if (iocp->ioc_error)
11289 		iocp->ioc_count = 0;
11290 	qreply(q, mp);
11291 }
11292 
11293 /*
11294  * Assign a unique id for the ipif. This is used by sctp_addr.c
11295  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11296  */
11297 static void
11298 ipif_assign_seqid(ipif_t *ipif)
11299 {
11300 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11301 
11302 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11303 }
11304 
11305 /*
11306  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11307  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11308  * that the clone is complete -- including the seqid -- and the expectation is
11309  * that the caller will either free or overwrite `sipif' before it's unlocked.
11310  */
11311 static void
11312 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11313 {
11314 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11315 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11316 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11317 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11318 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11319 
11320 	dipif->ipif_flags = sipif->ipif_flags;
11321 	dipif->ipif_metric = sipif->ipif_metric;
11322 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11323 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11324 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11325 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11326 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11327 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11328 
11329 	/*
11330 	 * As per the comment atop the function, we assume that these sipif
11331 	 * fields will be changed before sipif is unlocked.
11332 	 */
11333 	dipif->ipif_seqid = sipif->ipif_seqid;
11334 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11335 }
11336 
11337 /*
11338  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11339  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11340  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11341  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11342  * down (i.e., no DAD), of the same type, and unlocked.
11343  */
11344 static void
11345 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11346 {
11347 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11348 	ipxop_t *ipx = ipsq->ipsq_xop;
11349 
11350 	ASSERT(sipif != dipif);
11351 	ASSERT(sipif != virgipif);
11352 
11353 	/*
11354 	 * Grab all of the locks that protect the ipif in a defined order.
11355 	 */
11356 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11357 
11358 	ipif_clone(sipif, dipif);
11359 	if (virgipif != NULL) {
11360 		ipif_clone(virgipif, sipif);
11361 		mi_free(virgipif);
11362 	}
11363 
11364 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11365 
11366 	/*
11367 	 * Transfer ownership of the current xop, if necessary.
11368 	 */
11369 	if (ipx->ipx_current_ipif == sipif) {
11370 		ASSERT(ipx->ipx_pending_ipif == NULL);
11371 		mutex_enter(&ipx->ipx_lock);
11372 		ipx->ipx_current_ipif = dipif;
11373 		mutex_exit(&ipx->ipx_lock);
11374 	}
11375 
11376 	if (virgipif == NULL)
11377 		mi_free(sipif);
11378 }
11379 
11380 /*
11381  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11382  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11383  * be inserted into the first space available in the list. The value of
11384  * ipif_id will then be set to the appropriate value for its position.
11385  */
11386 static int
11387 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11388 {
11389 	ill_t *ill;
11390 	ipif_t *tipif;
11391 	ipif_t **tipifp;
11392 	int id;
11393 	ip_stack_t	*ipst;
11394 
11395 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11396 	    IAM_WRITER_IPIF(ipif));
11397 
11398 	ill = ipif->ipif_ill;
11399 	ASSERT(ill != NULL);
11400 	ipst = ill->ill_ipst;
11401 
11402 	/*
11403 	 * In the case of lo0:0 we already hold the ill_g_lock.
11404 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11405 	 * ipif_insert.
11406 	 */
11407 	if (acquire_g_lock)
11408 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11409 	mutex_enter(&ill->ill_lock);
11410 	id = ipif->ipif_id;
11411 	tipifp = &(ill->ill_ipif);
11412 	if (id == -1) {	/* need to find a real id */
11413 		id = 0;
11414 		while ((tipif = *tipifp) != NULL) {
11415 			ASSERT(tipif->ipif_id >= id);
11416 			if (tipif->ipif_id != id)
11417 				break; /* non-consecutive id */
11418 			id++;
11419 			tipifp = &(tipif->ipif_next);
11420 		}
11421 		/* limit number of logical interfaces */
11422 		if (id >= ipst->ips_ip_addrs_per_if) {
11423 			mutex_exit(&ill->ill_lock);
11424 			if (acquire_g_lock)
11425 				rw_exit(&ipst->ips_ill_g_lock);
11426 			return (-1);
11427 		}
11428 		ipif->ipif_id = id; /* assign new id */
11429 	} else if (id < ipst->ips_ip_addrs_per_if) {
11430 		/* we have a real id; insert ipif in the right place */
11431 		while ((tipif = *tipifp) != NULL) {
11432 			ASSERT(tipif->ipif_id != id);
11433 			if (tipif->ipif_id > id)
11434 				break; /* found correct location */
11435 			tipifp = &(tipif->ipif_next);
11436 		}
11437 	} else {
11438 		mutex_exit(&ill->ill_lock);
11439 		if (acquire_g_lock)
11440 			rw_exit(&ipst->ips_ill_g_lock);
11441 		return (-1);
11442 	}
11443 
11444 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11445 
11446 	ipif->ipif_next = tipif;
11447 	*tipifp = ipif;
11448 	mutex_exit(&ill->ill_lock);
11449 	if (acquire_g_lock)
11450 		rw_exit(&ipst->ips_ill_g_lock);
11451 
11452 	return (0);
11453 }
11454 
11455 static void
11456 ipif_remove(ipif_t *ipif)
11457 {
11458 	ipif_t	**ipifp;
11459 	ill_t	*ill = ipif->ipif_ill;
11460 
11461 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11462 
11463 	mutex_enter(&ill->ill_lock);
11464 	ipifp = &ill->ill_ipif;
11465 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11466 		if (*ipifp == ipif) {
11467 			*ipifp = ipif->ipif_next;
11468 			break;
11469 		}
11470 	}
11471 	mutex_exit(&ill->ill_lock);
11472 }
11473 
11474 /*
11475  * Allocate and initialize a new interface control structure.  (Always
11476  * called as writer.)
11477  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11478  * is not part of the global linked list of ills. ipif_seqid is unique
11479  * in the system and to preserve the uniqueness, it is assigned only
11480  * when ill becomes part of the global list. At that point ill will
11481  * have a name. If it doesn't get assigned here, it will get assigned
11482  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11483  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11484  * the interface flags or any other information from the DL_INFO_ACK for
11485  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11486  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11487  * second DL_INFO_ACK comes in from the driver.
11488  */
11489 static ipif_t *
11490 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11491     boolean_t insert)
11492 {
11493 	ipif_t	*ipif;
11494 	ip_stack_t *ipst = ill->ill_ipst;
11495 
11496 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11497 	    ill->ill_name, id, (void *)ill));
11498 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11499 
11500 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
11501 		return (NULL);
11502 	*ipif = ipif_zero;	/* start clean */
11503 
11504 	ipif->ipif_ill = ill;
11505 	ipif->ipif_id = id;	/* could be -1 */
11506 	/*
11507 	 * Inherit the zoneid from the ill; for the shared stack instance
11508 	 * this is always the global zone
11509 	 */
11510 	ipif->ipif_zoneid = ill->ill_zoneid;
11511 
11512 	ipif->ipif_refcnt = 0;
11513 
11514 	if (insert) {
11515 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
11516 			mi_free(ipif);
11517 			return (NULL);
11518 		}
11519 		/* -1 id should have been replaced by real id */
11520 		id = ipif->ipif_id;
11521 		ASSERT(id >= 0);
11522 	}
11523 
11524 	if (ill->ill_name[0] != '\0')
11525 		ipif_assign_seqid(ipif);
11526 
11527 	/*
11528 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11529 	 * (which must not exist yet because the zeroth ipif is created once
11530 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11531 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11532 	 */
11533 	if (id == 0 && IS_IPMP(ill)) {
11534 		if (ipmp_illgrp_create(ill) == NULL) {
11535 			if (insert) {
11536 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11537 				ipif_remove(ipif);
11538 				rw_exit(&ipst->ips_ill_g_lock);
11539 			}
11540 			mi_free(ipif);
11541 			return (NULL);
11542 		}
11543 	}
11544 
11545 	/*
11546 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11547 	 * not up and can't be looked up until the ioctl completes and the
11548 	 * IPIF_CHANGING flag is cleared.
11549 	 */
11550 	mutex_enter(&ill->ill_lock);
11551 
11552 	ipif->ipif_ire_type = ire_type;
11553 
11554 	if (ipif->ipif_isv6) {
11555 		ill->ill_flags |= ILLF_IPV6;
11556 	} else {
11557 		ipaddr_t inaddr_any = INADDR_ANY;
11558 
11559 		ill->ill_flags |= ILLF_IPV4;
11560 
11561 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11562 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11563 		    &ipif->ipif_v6lcl_addr);
11564 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11565 		    &ipif->ipif_v6subnet);
11566 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11567 		    &ipif->ipif_v6net_mask);
11568 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11569 		    &ipif->ipif_v6brd_addr);
11570 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11571 		    &ipif->ipif_v6pp_dst_addr);
11572 	}
11573 
11574 	/*
11575 	 * Don't set the interface flags etc. now, will do it in
11576 	 * ip_ll_subnet_defaults.
11577 	 */
11578 	if (!initialize)
11579 		goto out;
11580 
11581 	/*
11582 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11583 	 * with no underlying interfaces (and thus an unknown broadcast
11584 	 * address length), but all interfaces that can be placed into an IPMP
11585 	 * group are required to be broadcast-capable.
11586 	 */
11587 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11588 		/*
11589 		 * Later detect lack of DLPI driver multicast capability by
11590 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11591 		 */
11592 		ill->ill_flags |= ILLF_MULTICAST;
11593 		if (!ipif->ipif_isv6)
11594 			ipif->ipif_flags |= IPIF_BROADCAST;
11595 	} else {
11596 		if (ill->ill_net_type != IRE_LOOPBACK) {
11597 			if (ipif->ipif_isv6)
11598 				/*
11599 				 * Note: xresolv interfaces will eventually need
11600 				 * NOARP set here as well, but that will require
11601 				 * those external resolvers to have some
11602 				 * knowledge of that flag and act appropriately.
11603 				 * Not to be changed at present.
11604 				 */
11605 				ill->ill_flags |= ILLF_NONUD;
11606 			else
11607 				ill->ill_flags |= ILLF_NOARP;
11608 		}
11609 		if (ill->ill_phys_addr_length == 0) {
11610 			if (IS_VNI(ill)) {
11611 				ipif->ipif_flags |= IPIF_NOXMIT;
11612 			} else {
11613 				/* pt-pt supports multicast. */
11614 				ill->ill_flags |= ILLF_MULTICAST;
11615 				if (ill->ill_net_type != IRE_LOOPBACK)
11616 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11617 			}
11618 		}
11619 	}
11620 out:
11621 	mutex_exit(&ill->ill_lock);
11622 	return (ipif);
11623 }
11624 
11625 /*
11626  * Remove the neighbor cache entries associated with this logical
11627  * interface.
11628  */
11629 int
11630 ipif_arp_down(ipif_t *ipif)
11631 {
11632 	ill_t	*ill = ipif->ipif_ill;
11633 	int	err = 0;
11634 
11635 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11636 	ASSERT(IAM_WRITER_IPIF(ipif));
11637 
11638 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11639 	    ill_t *, ill, ipif_t *, ipif);
11640 	ipif_nce_down(ipif);
11641 
11642 	/*
11643 	 * If this is the last ipif that is going down and there are no
11644 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11645 	 * clean up ARP completely.
11646 	 */
11647 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11648 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11649 		/*
11650 		 * If this was the last ipif on an IPMP interface, purge any
11651 		 * static ARP entries associated with it.
11652 		 */
11653 		if (IS_IPMP(ill))
11654 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11655 
11656 		/* UNBIND, DETACH */
11657 		err = arp_ll_down(ill);
11658 	}
11659 
11660 	return (err);
11661 }
11662 
11663 /*
11664  * Get the resolver set up for a new IP address.  (Always called as writer.)
11665  * Called both for IPv4 and IPv6 interfaces, though it only does some
11666  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11667  *
11668  * The enumerated value res_act tunes the behavior:
11669  * 	* Res_act_initial: set up all the resolver structures for a new
11670  *	  IP address.
11671  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11672  *	  ARP message in defense of the address.
11673  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11674  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11675  *
11676  * Returns zero on success, or an errno upon failure.
11677  */
11678 int
11679 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11680 {
11681 	ill_t		*ill = ipif->ipif_ill;
11682 	int		err;
11683 	boolean_t	was_dup;
11684 
11685 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11686 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11687 	ASSERT(IAM_WRITER_IPIF(ipif));
11688 
11689 	was_dup = B_FALSE;
11690 	if (res_act == Res_act_initial) {
11691 		ipif->ipif_addr_ready = 0;
11692 		/*
11693 		 * We're bringing an interface up here.  There's no way that we
11694 		 * should need to shut down ARP now.
11695 		 */
11696 		mutex_enter(&ill->ill_lock);
11697 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11698 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11699 			ill->ill_ipif_dup_count--;
11700 			was_dup = B_TRUE;
11701 		}
11702 		mutex_exit(&ill->ill_lock);
11703 	}
11704 	if (ipif->ipif_recovery_id != 0)
11705 		(void) untimeout(ipif->ipif_recovery_id);
11706 	ipif->ipif_recovery_id = 0;
11707 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11708 		ipif->ipif_addr_ready = 1;
11709 		return (0);
11710 	}
11711 	/* NDP will set the ipif_addr_ready flag when it's ready */
11712 	if (ill->ill_isv6)
11713 		return (0);
11714 
11715 	err = ipif_arp_up(ipif, res_act, was_dup);
11716 	return (err);
11717 }
11718 
11719 /*
11720  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11721  * when a link has just gone back up.
11722  */
11723 static void
11724 ipif_nce_start_dad(ipif_t *ipif)
11725 {
11726 	ncec_t *ncec;
11727 	ill_t *ill = ipif->ipif_ill;
11728 	boolean_t isv6 = ill->ill_isv6;
11729 
11730 	if (isv6) {
11731 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11732 		    &ipif->ipif_v6lcl_addr);
11733 	} else {
11734 		ipaddr_t v4addr;
11735 
11736 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11737 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11738 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11739 			/*
11740 			 * If we can't contact ARP for some reason,
11741 			 * that's not really a problem.  Just send
11742 			 * out the routing socket notification that
11743 			 * DAD completion would have done, and continue.
11744 			 */
11745 			ipif_mask_reply(ipif);
11746 			ipif_up_notify(ipif);
11747 			ipif->ipif_addr_ready = 1;
11748 			return;
11749 		}
11750 
11751 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11752 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11753 	}
11754 
11755 	if (ncec == NULL) {
11756 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11757 		    (void *)ipif));
11758 		return;
11759 	}
11760 	if (!nce_restart_dad(ncec)) {
11761 		/*
11762 		 * If we can't restart DAD for some reason, that's not really a
11763 		 * problem.  Just send out the routing socket notification that
11764 		 * DAD completion would have done, and continue.
11765 		 */
11766 		ipif_up_notify(ipif);
11767 		ipif->ipif_addr_ready = 1;
11768 	}
11769 	ncec_refrele(ncec);
11770 }
11771 
11772 /*
11773  * Restart duplicate address detection on all interfaces on the given ill.
11774  *
11775  * This is called when an interface transitions from down to up
11776  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11777  *
11778  * Note that since the underlying physical link has transitioned, we must cause
11779  * at least one routing socket message to be sent here, either via DAD
11780  * completion or just by default on the first ipif.  (If we don't do this, then
11781  * in.mpathd will see long delays when doing link-based failure recovery.)
11782  */
11783 void
11784 ill_restart_dad(ill_t *ill, boolean_t went_up)
11785 {
11786 	ipif_t *ipif;
11787 
11788 	if (ill == NULL)
11789 		return;
11790 
11791 	/*
11792 	 * If layer two doesn't support duplicate address detection, then just
11793 	 * send the routing socket message now and be done with it.
11794 	 */
11795 	if (!ill->ill_isv6 && arp_no_defense) {
11796 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11797 		return;
11798 	}
11799 
11800 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11801 		if (went_up) {
11802 
11803 			if (ipif->ipif_flags & IPIF_UP) {
11804 				ipif_nce_start_dad(ipif);
11805 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
11806 				/*
11807 				 * kick off the bring-up process now.
11808 				 */
11809 				ipif_do_recovery(ipif);
11810 			} else {
11811 				/*
11812 				 * Unfortunately, the first ipif is "special"
11813 				 * and represents the underlying ill in the
11814 				 * routing socket messages.  Thus, when this
11815 				 * one ipif is down, we must still notify so
11816 				 * that the user knows the IFF_RUNNING status
11817 				 * change.  (If the first ipif is up, then
11818 				 * we'll handle eventual routing socket
11819 				 * notification via DAD completion.)
11820 				 */
11821 				if (ipif == ill->ill_ipif) {
11822 					ip_rts_ifmsg(ill->ill_ipif,
11823 					    RTSQ_DEFAULT);
11824 				}
11825 			}
11826 		} else {
11827 			/*
11828 			 * After link down, we'll need to send a new routing
11829 			 * message when the link comes back, so clear
11830 			 * ipif_addr_ready.
11831 			 */
11832 			ipif->ipif_addr_ready = 0;
11833 		}
11834 	}
11835 
11836 	/*
11837 	 * If we've torn down links, then notify the user right away.
11838 	 */
11839 	if (!went_up)
11840 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11841 }
11842 
11843 static void
11844 ipsq_delete(ipsq_t *ipsq)
11845 {
11846 	ipxop_t *ipx = ipsq->ipsq_xop;
11847 
11848 	ipsq->ipsq_ipst = NULL;
11849 	ASSERT(ipsq->ipsq_phyint == NULL);
11850 	ASSERT(ipsq->ipsq_xop != NULL);
11851 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
11852 	ASSERT(ipx->ipx_pending_mp == NULL);
11853 	kmem_free(ipsq, sizeof (ipsq_t));
11854 }
11855 
11856 static int
11857 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
11858 {
11859 	int err = 0;
11860 	ipif_t *ipif;
11861 
11862 	if (ill == NULL)
11863 		return (0);
11864 
11865 	ASSERT(IAM_WRITER_ILL(ill));
11866 	ill->ill_up_ipifs = B_TRUE;
11867 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11868 		if (ipif->ipif_was_up) {
11869 			if (!(ipif->ipif_flags & IPIF_UP))
11870 				err = ipif_up(ipif, q, mp);
11871 			ipif->ipif_was_up = B_FALSE;
11872 			if (err != 0) {
11873 				ASSERT(err == EINPROGRESS);
11874 				return (err);
11875 			}
11876 		}
11877 	}
11878 	ill->ill_up_ipifs = B_FALSE;
11879 	return (0);
11880 }
11881 
11882 /*
11883  * This function is called to bring up all the ipifs that were up before
11884  * bringing the ill down via ill_down_ipifs().
11885  */
11886 int
11887 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
11888 {
11889 	int err;
11890 
11891 	ASSERT(IAM_WRITER_ILL(ill));
11892 
11893 	if (ill->ill_replumbing) {
11894 		ill->ill_replumbing = 0;
11895 		/*
11896 		 * Send down REPLUMB_DONE notification followed by the
11897 		 * BIND_REQ on the arp stream.
11898 		 */
11899 		if (!ill->ill_isv6)
11900 			arp_send_replumb_conf(ill);
11901 	}
11902 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
11903 	if (err != 0)
11904 		return (err);
11905 
11906 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
11907 }
11908 
11909 /*
11910  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
11911  * down the ipifs without sending DL_UNBIND_REQ to the driver.
11912  */
11913 static void
11914 ill_down_ipifs(ill_t *ill, boolean_t logical)
11915 {
11916 	ipif_t *ipif;
11917 
11918 	ASSERT(IAM_WRITER_ILL(ill));
11919 
11920 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11921 		/*
11922 		 * We go through the ipif_down logic even if the ipif
11923 		 * is already down, since routes can be added based
11924 		 * on down ipifs. Going through ipif_down once again
11925 		 * will delete any IREs created based on these routes.
11926 		 */
11927 		if (ipif->ipif_flags & IPIF_UP)
11928 			ipif->ipif_was_up = B_TRUE;
11929 
11930 		if (logical) {
11931 			(void) ipif_logical_down(ipif, NULL, NULL);
11932 			ipif_non_duplicate(ipif);
11933 			(void) ipif_down_tail(ipif);
11934 		} else {
11935 			(void) ipif_down(ipif, NULL, NULL);
11936 		}
11937 	}
11938 }
11939 
11940 /*
11941  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
11942  * a look again at valid source addresses.
11943  * This should be called each time after the set of source addresses has been
11944  * changed.
11945  */
11946 void
11947 ip_update_source_selection(ip_stack_t *ipst)
11948 {
11949 	/* We skip past SRC_GENERATION_VERIFY */
11950 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
11951 	    SRC_GENERATION_VERIFY)
11952 		atomic_add_32(&ipst->ips_src_generation, 1);
11953 }
11954 
11955 /*
11956  * Finish the group join started in ip_sioctl_groupname().
11957  */
11958 /* ARGSUSED */
11959 static void
11960 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
11961 {
11962 	ill_t		*ill = q->q_ptr;
11963 	phyint_t	*phyi = ill->ill_phyint;
11964 	ipmp_grp_t	*grp = phyi->phyint_grp;
11965 	ip_stack_t	*ipst = ill->ill_ipst;
11966 
11967 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
11968 	ASSERT(!IS_IPMP(ill) && grp != NULL);
11969 	ASSERT(IAM_WRITER_IPSQ(ipsq));
11970 
11971 	if (phyi->phyint_illv4 != NULL) {
11972 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
11973 		VERIFY(grp->gr_pendv4-- > 0);
11974 		rw_exit(&ipst->ips_ipmp_lock);
11975 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
11976 	}
11977 	if (phyi->phyint_illv6 != NULL) {
11978 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
11979 		VERIFY(grp->gr_pendv6-- > 0);
11980 		rw_exit(&ipst->ips_ipmp_lock);
11981 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
11982 	}
11983 	freemsg(mp);
11984 }
11985 
11986 /*
11987  * Process an SIOCSLIFGROUPNAME request.
11988  */
11989 /* ARGSUSED */
11990 int
11991 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11992     ip_ioctl_cmd_t *ipip, void *ifreq)
11993 {
11994 	struct lifreq	*lifr = ifreq;
11995 	ill_t		*ill = ipif->ipif_ill;
11996 	ip_stack_t	*ipst = ill->ill_ipst;
11997 	phyint_t	*phyi = ill->ill_phyint;
11998 	ipmp_grp_t	*grp = phyi->phyint_grp;
11999 	mblk_t		*ipsq_mp;
12000 	int		err = 0;
12001 
12002 	/*
12003 	 * Note that phyint_grp can only change here, where we're exclusive.
12004 	 */
12005 	ASSERT(IAM_WRITER_ILL(ill));
12006 
12007 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12008 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12009 		return (EINVAL);
12010 
12011 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12012 
12013 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12014 
12015 	/*
12016 	 * If the name hasn't changed, there's nothing to do.
12017 	 */
12018 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12019 		goto unlock;
12020 
12021 	/*
12022 	 * Handle requests to rename an IPMP meta-interface.
12023 	 *
12024 	 * Note that creation of the IPMP meta-interface is handled in
12025 	 * userland through the standard plumbing sequence.  As part of the
12026 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12027 	 * the name of the interface (see ipif_set_values_tail()).
12028 	 */
12029 	if (IS_IPMP(ill)) {
12030 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12031 		goto unlock;
12032 	}
12033 
12034 	/*
12035 	 * Handle requests to add or remove an IP interface from a group.
12036 	 */
12037 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12038 		/*
12039 		 * Moves are handled by first removing the interface from
12040 		 * its existing group, and then adding it to another group.
12041 		 * So, fail if it's already in a group.
12042 		 */
12043 		if (IS_UNDER_IPMP(ill)) {
12044 			err = EALREADY;
12045 			goto unlock;
12046 		}
12047 
12048 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12049 		if (grp == NULL) {
12050 			err = ENOENT;
12051 			goto unlock;
12052 		}
12053 
12054 		/*
12055 		 * Check if the phyint and its ills are suitable for
12056 		 * inclusion into the group.
12057 		 */
12058 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12059 			goto unlock;
12060 
12061 		/*
12062 		 * Checks pass; join the group, and enqueue the remaining
12063 		 * illgrp joins for when we've become part of the group xop
12064 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12065 		 * requires an mblk_t to scribble on, and since `mp' will be
12066 		 * freed as part of completing the ioctl, allocate another.
12067 		 */
12068 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12069 			err = ENOMEM;
12070 			goto unlock;
12071 		}
12072 
12073 		/*
12074 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12075 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12076 		 * before ip_join_illgrps() is called back.  See the comments
12077 		 * in ip_sioctl_plink_ipmp() for more.
12078 		 */
12079 		if (phyi->phyint_illv4 != NULL)
12080 			grp->gr_pendv4++;
12081 		if (phyi->phyint_illv6 != NULL)
12082 			grp->gr_pendv6++;
12083 
12084 		rw_exit(&ipst->ips_ipmp_lock);
12085 
12086 		ipmp_phyint_join_grp(phyi, grp);
12087 		ill_refhold(ill);
12088 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12089 		    SWITCH_OP, B_FALSE);
12090 		return (0);
12091 	} else {
12092 		/*
12093 		 * Request to remove the interface from a group.  If the
12094 		 * interface is not in a group, this trivially succeeds.
12095 		 */
12096 		rw_exit(&ipst->ips_ipmp_lock);
12097 		if (IS_UNDER_IPMP(ill))
12098 			ipmp_phyint_leave_grp(phyi);
12099 		return (0);
12100 	}
12101 unlock:
12102 	rw_exit(&ipst->ips_ipmp_lock);
12103 	return (err);
12104 }
12105 
12106 /*
12107  * Process an SIOCGLIFBINDING request.
12108  */
12109 /* ARGSUSED */
12110 int
12111 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12112     ip_ioctl_cmd_t *ipip, void *ifreq)
12113 {
12114 	ill_t		*ill;
12115 	struct lifreq	*lifr = ifreq;
12116 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12117 
12118 	if (!IS_IPMP(ipif->ipif_ill))
12119 		return (EINVAL);
12120 
12121 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12122 	if ((ill = ipif->ipif_bound_ill) == NULL)
12123 		lifr->lifr_binding[0] = '\0';
12124 	else
12125 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12126 	rw_exit(&ipst->ips_ipmp_lock);
12127 	return (0);
12128 }
12129 
12130 /*
12131  * Process an SIOCGLIFGROUPNAME request.
12132  */
12133 /* ARGSUSED */
12134 int
12135 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12136     ip_ioctl_cmd_t *ipip, void *ifreq)
12137 {
12138 	ipmp_grp_t	*grp;
12139 	struct lifreq	*lifr = ifreq;
12140 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12141 
12142 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12143 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12144 		lifr->lifr_groupname[0] = '\0';
12145 	else
12146 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12147 	rw_exit(&ipst->ips_ipmp_lock);
12148 	return (0);
12149 }
12150 
12151 /*
12152  * Process an SIOCGLIFGROUPINFO request.
12153  */
12154 /* ARGSUSED */
12155 int
12156 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12157     ip_ioctl_cmd_t *ipip, void *dummy)
12158 {
12159 	ipmp_grp_t	*grp;
12160 	lifgroupinfo_t	*lifgr;
12161 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12162 
12163 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12164 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12165 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12166 
12167 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12168 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12169 		rw_exit(&ipst->ips_ipmp_lock);
12170 		return (ENOENT);
12171 	}
12172 	ipmp_grp_info(grp, lifgr);
12173 	rw_exit(&ipst->ips_ipmp_lock);
12174 	return (0);
12175 }
12176 
12177 static void
12178 ill_dl_down(ill_t *ill)
12179 {
12180 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12181 
12182 	/*
12183 	 * The ill is down; unbind but stay attached since we're still
12184 	 * associated with a PPA. If we have negotiated DLPI capabilites
12185 	 * with the data link service provider (IDS_OK) then reset them.
12186 	 * The interval between unbinding and rebinding is potentially
12187 	 * unbounded hence we cannot assume things will be the same.
12188 	 * The DLPI capabilities will be probed again when the data link
12189 	 * is brought up.
12190 	 */
12191 	mblk_t	*mp = ill->ill_unbind_mp;
12192 
12193 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12194 
12195 	if (!ill->ill_replumbing) {
12196 		/* Free all ilms for this ill */
12197 		update_conn_ill(ill, ill->ill_ipst);
12198 	} else {
12199 		ill_leave_multicast(ill);
12200 	}
12201 
12202 	ill->ill_unbind_mp = NULL;
12203 	if (mp != NULL) {
12204 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12205 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12206 		    ill->ill_name));
12207 		mutex_enter(&ill->ill_lock);
12208 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12209 		mutex_exit(&ill->ill_lock);
12210 		/*
12211 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12212 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12213 		 * ill_capability_dld_disable disable rightaway. If this is not
12214 		 * an unplumb operation then the disable happens on receipt of
12215 		 * the capab ack via ip_rput_dlpi_writer ->
12216 		 * ill_capability_ack_thr. In both cases the order of
12217 		 * the operations seen by DLD is capability disable followed
12218 		 * by DL_UNBIND. Also the DLD capability disable needs a
12219 		 * cv_wait'able context.
12220 		 */
12221 		if (ill->ill_state_flags & ILL_CONDEMNED)
12222 			ill_capability_dld_disable(ill);
12223 		ill_capability_reset(ill, B_FALSE);
12224 		ill_dlpi_send(ill, mp);
12225 	}
12226 	mutex_enter(&ill->ill_lock);
12227 	ill->ill_dl_up = 0;
12228 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12229 	mutex_exit(&ill->ill_lock);
12230 }
12231 
12232 void
12233 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12234 {
12235 	union DL_primitives *dlp;
12236 	t_uscalar_t prim;
12237 	boolean_t waitack = B_FALSE;
12238 
12239 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12240 
12241 	dlp = (union DL_primitives *)mp->b_rptr;
12242 	prim = dlp->dl_primitive;
12243 
12244 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12245 	    dl_primstr(prim), prim, ill->ill_name));
12246 
12247 	switch (prim) {
12248 	case DL_PHYS_ADDR_REQ:
12249 	{
12250 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12251 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12252 		break;
12253 	}
12254 	case DL_BIND_REQ:
12255 		mutex_enter(&ill->ill_lock);
12256 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12257 		mutex_exit(&ill->ill_lock);
12258 		break;
12259 	}
12260 
12261 	/*
12262 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12263 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12264 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12265 	 */
12266 	mutex_enter(&ill->ill_lock);
12267 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12268 	    (prim == DL_UNBIND_REQ)) {
12269 		ill->ill_dlpi_pending = prim;
12270 		waitack = B_TRUE;
12271 	}
12272 
12273 	mutex_exit(&ill->ill_lock);
12274 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12275 	    char *, dl_primstr(prim), ill_t *, ill);
12276 	putnext(ill->ill_wq, mp);
12277 
12278 	/*
12279 	 * There is no ack for DL_NOTIFY_CONF messages
12280 	 */
12281 	if (waitack && prim == DL_NOTIFY_CONF)
12282 		ill_dlpi_done(ill, prim);
12283 }
12284 
12285 /*
12286  * Helper function for ill_dlpi_send().
12287  */
12288 /* ARGSUSED */
12289 static void
12290 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12291 {
12292 	ill_dlpi_send(q->q_ptr, mp);
12293 }
12294 
12295 /*
12296  * Send a DLPI control message to the driver but make sure there
12297  * is only one outstanding message. Uses ill_dlpi_pending to tell
12298  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12299  * when an ACK or a NAK is received to process the next queued message.
12300  */
12301 void
12302 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12303 {
12304 	mblk_t **mpp;
12305 
12306 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12307 
12308 	/*
12309 	 * To ensure that any DLPI requests for current exclusive operation
12310 	 * are always completely sent before any DLPI messages for other
12311 	 * operations, require writer access before enqueuing.
12312 	 */
12313 	if (!IAM_WRITER_ILL(ill)) {
12314 		ill_refhold(ill);
12315 		/* qwriter_ip() does the ill_refrele() */
12316 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12317 		    NEW_OP, B_TRUE);
12318 		return;
12319 	}
12320 
12321 	mutex_enter(&ill->ill_lock);
12322 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12323 		/* Must queue message. Tail insertion */
12324 		mpp = &ill->ill_dlpi_deferred;
12325 		while (*mpp != NULL)
12326 			mpp = &((*mpp)->b_next);
12327 
12328 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12329 		    "while %s pending\n", ill->ill_name,
12330 		    dl_primstr(ill->ill_dlpi_pending)));
12331 
12332 		*mpp = mp;
12333 		mutex_exit(&ill->ill_lock);
12334 		return;
12335 	}
12336 	mutex_exit(&ill->ill_lock);
12337 	ill_dlpi_dispatch(ill, mp);
12338 }
12339 
12340 static void
12341 ill_capability_send(ill_t *ill, mblk_t *mp)
12342 {
12343 	ill->ill_capab_pending_cnt++;
12344 	ill_dlpi_send(ill, mp);
12345 }
12346 
12347 void
12348 ill_capability_done(ill_t *ill)
12349 {
12350 	ASSERT(ill->ill_capab_pending_cnt != 0);
12351 
12352 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12353 
12354 	ill->ill_capab_pending_cnt--;
12355 	if (ill->ill_capab_pending_cnt == 0 &&
12356 	    ill->ill_dlpi_capab_state == IDCS_OK)
12357 		ill_capability_reset_alloc(ill);
12358 }
12359 
12360 /*
12361  * Send all deferred DLPI messages without waiting for their ACKs.
12362  */
12363 void
12364 ill_dlpi_send_deferred(ill_t *ill)
12365 {
12366 	mblk_t *mp, *nextmp;
12367 
12368 	/*
12369 	 * Clear ill_dlpi_pending so that the message is not queued in
12370 	 * ill_dlpi_send().
12371 	 */
12372 	mutex_enter(&ill->ill_lock);
12373 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12374 	mp = ill->ill_dlpi_deferred;
12375 	ill->ill_dlpi_deferred = NULL;
12376 	mutex_exit(&ill->ill_lock);
12377 
12378 	for (; mp != NULL; mp = nextmp) {
12379 		nextmp = mp->b_next;
12380 		mp->b_next = NULL;
12381 		ill_dlpi_send(ill, mp);
12382 	}
12383 }
12384 
12385 /*
12386  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12387  */
12388 boolean_t
12389 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12390 {
12391 	t_uscalar_t pending;
12392 
12393 	mutex_enter(&ill->ill_lock);
12394 	if (ill->ill_dlpi_pending == prim) {
12395 		mutex_exit(&ill->ill_lock);
12396 		return (B_TRUE);
12397 	}
12398 
12399 	/*
12400 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12401 	 * without waiting, so don't print any warnings in that case.
12402 	 */
12403 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12404 		mutex_exit(&ill->ill_lock);
12405 		return (B_FALSE);
12406 	}
12407 	pending = ill->ill_dlpi_pending;
12408 	mutex_exit(&ill->ill_lock);
12409 
12410 	if (pending == DL_PRIM_INVAL) {
12411 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12412 		    "received unsolicited ack for %s on %s\n",
12413 		    dl_primstr(prim), ill->ill_name);
12414 	} else {
12415 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12416 		    "received unexpected ack for %s on %s (expecting %s)\n",
12417 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12418 	}
12419 	return (B_FALSE);
12420 }
12421 
12422 /*
12423  * Complete the current DLPI operation associated with `prim' on `ill' and
12424  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12425  * operations and the ill's current exclusive IPSQ operation has finished
12426  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12427  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12428  * the comments above ipsq_current_finish() for details.
12429  */
12430 void
12431 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12432 {
12433 	mblk_t *mp;
12434 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12435 	ipxop_t *ipx = ipsq->ipsq_xop;
12436 
12437 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12438 	mutex_enter(&ill->ill_lock);
12439 
12440 	ASSERT(prim != DL_PRIM_INVAL);
12441 	ASSERT(ill->ill_dlpi_pending == prim);
12442 
12443 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12444 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12445 
12446 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12447 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12448 		if (ipx->ipx_current_done) {
12449 			mutex_enter(&ipx->ipx_lock);
12450 			ipx->ipx_current_ipif = NULL;
12451 			mutex_exit(&ipx->ipx_lock);
12452 		}
12453 		cv_signal(&ill->ill_cv);
12454 		mutex_exit(&ill->ill_lock);
12455 		return;
12456 	}
12457 
12458 	ill->ill_dlpi_deferred = mp->b_next;
12459 	mp->b_next = NULL;
12460 	mutex_exit(&ill->ill_lock);
12461 
12462 	ill_dlpi_dispatch(ill, mp);
12463 }
12464 
12465 /*
12466  * Queue a (multicast) DLPI control message to be sent to the driver by
12467  * later calling ill_dlpi_send_queued.
12468  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12469  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12470  * for the same group to race.
12471  * We send DLPI control messages in order using ill_lock.
12472  * For IPMP we should be called on the cast_ill.
12473  */
12474 void
12475 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12476 {
12477 	mblk_t **mpp;
12478 
12479 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12480 
12481 	mutex_enter(&ill->ill_lock);
12482 	/* Must queue message. Tail insertion */
12483 	mpp = &ill->ill_dlpi_deferred;
12484 	while (*mpp != NULL)
12485 		mpp = &((*mpp)->b_next);
12486 
12487 	*mpp = mp;
12488 	mutex_exit(&ill->ill_lock);
12489 }
12490 
12491 /*
12492  * Send the messages that were queued. Make sure there is only
12493  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12494  * when an ACK or a NAK is received to process the next queued message.
12495  * For IPMP we are called on the upper ill, but when send what is queued
12496  * on the cast_ill.
12497  */
12498 void
12499 ill_dlpi_send_queued(ill_t *ill)
12500 {
12501 	mblk_t	*mp;
12502 	union DL_primitives *dlp;
12503 	t_uscalar_t prim;
12504 	ill_t *release_ill = NULL;
12505 
12506 	if (IS_IPMP(ill)) {
12507 		/* On the upper IPMP ill. */
12508 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12509 		if (release_ill == NULL) {
12510 			/* Avoid ever sending anything down to the ipmpstub */
12511 			return;
12512 		}
12513 		ill = release_ill;
12514 	}
12515 	mutex_enter(&ill->ill_lock);
12516 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12517 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12518 			/* Can't send. Somebody else will send it */
12519 			mutex_exit(&ill->ill_lock);
12520 			goto done;
12521 		}
12522 		ill->ill_dlpi_deferred = mp->b_next;
12523 		mp->b_next = NULL;
12524 		if (!ill->ill_dl_up) {
12525 			/*
12526 			 * Nobody there. All multicast addresses will be
12527 			 * re-joined when we get the DL_BIND_ACK bringing the
12528 			 * interface up.
12529 			 */
12530 			freemsg(mp);
12531 			continue;
12532 		}
12533 		dlp = (union DL_primitives *)mp->b_rptr;
12534 		prim = dlp->dl_primitive;
12535 
12536 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12537 		    (prim == DL_UNBIND_REQ)) {
12538 			ill->ill_dlpi_pending = prim;
12539 		}
12540 		mutex_exit(&ill->ill_lock);
12541 
12542 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12543 		    char *, dl_primstr(prim), ill_t *, ill);
12544 		putnext(ill->ill_wq, mp);
12545 		mutex_enter(&ill->ill_lock);
12546 	}
12547 	mutex_exit(&ill->ill_lock);
12548 done:
12549 	if (release_ill != NULL)
12550 		ill_refrele(release_ill);
12551 }
12552 
12553 /*
12554  * Queue an IP (IGMP/MLD) message to be sent by IP from
12555  * ill_mcast_send_queued
12556  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12557  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12558  * group to race.
12559  * We send them in order using ill_lock.
12560  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12561  */
12562 void
12563 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12564 {
12565 	mblk_t **mpp;
12566 	ill_t *release_ill = NULL;
12567 
12568 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12569 
12570 	if (IS_IPMP(ill)) {
12571 		/* On the upper IPMP ill. */
12572 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12573 		if (release_ill == NULL) {
12574 			/* Discard instead of queuing for the ipmp interface */
12575 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12576 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12577 			    mp, ill);
12578 			freemsg(mp);
12579 			return;
12580 		}
12581 		ill = release_ill;
12582 	}
12583 
12584 	mutex_enter(&ill->ill_lock);
12585 	/* Must queue message. Tail insertion */
12586 	mpp = &ill->ill_mcast_deferred;
12587 	while (*mpp != NULL)
12588 		mpp = &((*mpp)->b_next);
12589 
12590 	*mpp = mp;
12591 	mutex_exit(&ill->ill_lock);
12592 	if (release_ill != NULL)
12593 		ill_refrele(release_ill);
12594 }
12595 
12596 /*
12597  * Send the IP packets that were queued by ill_mcast_queue.
12598  * These are IGMP/MLD packets.
12599  *
12600  * For IPMP we are called on the upper ill, but when send what is queued
12601  * on the cast_ill.
12602  *
12603  * Request loopback of the report if we are acting as a multicast
12604  * router, so that the process-level routing demon can hear it.
12605  * This will run multiple times for the same group if there are members
12606  * on the same group for multiple ipif's on the same ill. The
12607  * igmp_input/mld_input code will suppress this due to the loopback thus we
12608  * always loopback membership report.
12609  *
12610  * We also need to make sure that this does not get load balanced
12611  * by IPMP. We do this by passing an ill to ip_output_simple.
12612  */
12613 void
12614 ill_mcast_send_queued(ill_t *ill)
12615 {
12616 	mblk_t	*mp;
12617 	ip_xmit_attr_t ixas;
12618 	ill_t *release_ill = NULL;
12619 
12620 	if (IS_IPMP(ill)) {
12621 		/* On the upper IPMP ill. */
12622 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12623 		if (release_ill == NULL) {
12624 			/*
12625 			 * We should have no messages on the ipmp interface
12626 			 * but no point in trying to send them.
12627 			 */
12628 			return;
12629 		}
12630 		ill = release_ill;
12631 	}
12632 	bzero(&ixas, sizeof (ixas));
12633 	ixas.ixa_zoneid = ALL_ZONES;
12634 	ixas.ixa_cred = kcred;
12635 	ixas.ixa_cpid = NOPID;
12636 	ixas.ixa_tsl = NULL;
12637 	/*
12638 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12639 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12640 	 * That is necessary to handle IGMP/MLD snooping switches.
12641 	 */
12642 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12643 	ixas.ixa_ipst = ill->ill_ipst;
12644 
12645 	mutex_enter(&ill->ill_lock);
12646 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12647 		ill->ill_mcast_deferred = mp->b_next;
12648 		mp->b_next = NULL;
12649 		if (!ill->ill_dl_up) {
12650 			/*
12651 			 * Nobody there. Just drop the ip packets.
12652 			 * IGMP/MLD will resend later, if this is a replumb.
12653 			 */
12654 			freemsg(mp);
12655 			continue;
12656 		}
12657 		mutex_enter(&ill->ill_phyint->phyint_lock);
12658 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12659 			/*
12660 			 * When the ill is getting deactivated, we only want to
12661 			 * send the DLPI messages, so drop IGMP/MLD packets.
12662 			 * DLPI messages are handled by ill_dlpi_send_queued()
12663 			 */
12664 			mutex_exit(&ill->ill_phyint->phyint_lock);
12665 			freemsg(mp);
12666 			continue;
12667 		}
12668 		mutex_exit(&ill->ill_phyint->phyint_lock);
12669 		mutex_exit(&ill->ill_lock);
12670 
12671 		/* Check whether we are sending IPv4 or IPv6. */
12672 		if (ill->ill_isv6) {
12673 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12674 
12675 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12676 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12677 		} else {
12678 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12679 
12680 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12681 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12682 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12683 		}
12684 
12685 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12686 		(void) ip_output_simple(mp, &ixas);
12687 		ixa_cleanup(&ixas);
12688 
12689 		mutex_enter(&ill->ill_lock);
12690 	}
12691 	mutex_exit(&ill->ill_lock);
12692 
12693 done:
12694 	if (release_ill != NULL)
12695 		ill_refrele(release_ill);
12696 }
12697 
12698 /*
12699  * Take down a specific interface, but don't lose any information about it.
12700  * (Always called as writer.)
12701  * This function goes through the down sequence even if the interface is
12702  * already down. There are 2 reasons.
12703  * a. Currently we permit interface routes that depend on down interfaces
12704  *    to be added. This behaviour itself is questionable. However it appears
12705  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12706  *    time. We go thru the cleanup in order to remove these routes.
12707  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12708  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12709  *    down, but we need to cleanup i.e. do ill_dl_down and
12710  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12711  *
12712  * IP-MT notes:
12713  *
12714  * Model of reference to interfaces.
12715  *
12716  * The following members in ipif_t track references to the ipif.
12717  *	int     ipif_refcnt;    Active reference count
12718  *
12719  * The following members in ill_t track references to the ill.
12720  *	int             ill_refcnt;     active refcnt
12721  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12722  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12723  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12724  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12725  *
12726  * Reference to an ipif or ill can be obtained in any of the following ways.
12727  *
12728  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12729  * Pointers to ipif / ill from other data structures viz ire and conn.
12730  * Implicit reference to the ipif / ill by holding a reference to the ire.
12731  *
12732  * The ipif/ill lookup functions return a reference held ipif / ill.
12733  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12734  * This is a purely dynamic reference count associated with threads holding
12735  * references to the ipif / ill. Pointers from other structures do not
12736  * count towards this reference count.
12737  *
12738  * ill_ire_cnt is the number of ire's associated with the
12739  * ill. This is incremented whenever a new ire is created referencing the
12740  * ill. This is done atomically inside ire_add_v[46] where the ire is
12741  * actually added to the ire hash table. The count is decremented in
12742  * ire_inactive where the ire is destroyed.
12743  *
12744  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12745  * This is incremented atomically in
12746  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12747  * table. Similarly it is decremented in ncec_inactive() where the ncec
12748  * is destroyed.
12749  *
12750  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12751  * incremented atomically in nce_add() where the nce is actually added to the
12752  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12753  * is destroyed.
12754  *
12755  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12756  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12757  *
12758  * Flow of ioctls involving interface down/up
12759  *
12760  * The following is the sequence of an attempt to set some critical flags on an
12761  * up interface.
12762  * ip_sioctl_flags
12763  * ipif_down
12764  * wait for ipif to be quiescent
12765  * ipif_down_tail
12766  * ip_sioctl_flags_tail
12767  *
12768  * All set ioctls that involve down/up sequence would have a skeleton similar
12769  * to the above. All the *tail functions are called after the refcounts have
12770  * dropped to the appropriate values.
12771  *
12772  * SIOC ioctls during the IPIF_CHANGING interval.
12773  *
12774  * Threads handling SIOC set ioctls serialize on the squeue, but this
12775  * is not done for SIOC get ioctls. Since a set ioctl can cause several
12776  * steps of internal changes to the state, some of which are visible in
12777  * ipif_flags (such as IFF_UP being cleared and later set), and we want
12778  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
12779  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
12780  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
12781  * the current exclusive operation completes. The IPIF_CHANGING check
12782  * and enqueue is atomic using the ill_lock and ipsq_lock. The
12783  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
12784  * change while the ill_lock is held. Before dropping the ill_lock we acquire
12785  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
12786  * until we release the ipsq_lock, even though the ill/ipif state flags
12787  * can change after we drop the ill_lock.
12788  */
12789 int
12790 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12791 {
12792 	ill_t		*ill = ipif->ipif_ill;
12793 	conn_t		*connp;
12794 	boolean_t	success;
12795 	boolean_t	ipif_was_up = B_FALSE;
12796 	ip_stack_t	*ipst = ill->ill_ipst;
12797 
12798 	ASSERT(IAM_WRITER_IPIF(ipif));
12799 
12800 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12801 
12802 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
12803 	    ill_t *, ill, ipif_t *, ipif);
12804 
12805 	if (ipif->ipif_flags & IPIF_UP) {
12806 		mutex_enter(&ill->ill_lock);
12807 		ipif->ipif_flags &= ~IPIF_UP;
12808 		ASSERT(ill->ill_ipif_up_count > 0);
12809 		--ill->ill_ipif_up_count;
12810 		mutex_exit(&ill->ill_lock);
12811 		ipif_was_up = B_TRUE;
12812 		/* Update status in SCTP's list */
12813 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
12814 		ill_nic_event_dispatch(ipif->ipif_ill,
12815 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
12816 	}
12817 
12818 	/*
12819 	 * Blow away memberships we established in ipif_multicast_up().
12820 	 */
12821 	ipif_multicast_down(ipif);
12822 
12823 	/*
12824 	 * Remove from the mapping for __sin6_src_id. We insert only
12825 	 * when the address is not INADDR_ANY. As IPv4 addresses are
12826 	 * stored as mapped addresses, we need to check for mapped
12827 	 * INADDR_ANY also.
12828 	 */
12829 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12830 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
12831 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
12832 		int err;
12833 
12834 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
12835 		    ipif->ipif_zoneid, ipst);
12836 		if (err != 0) {
12837 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
12838 		}
12839 	}
12840 
12841 	if (ipif_was_up) {
12842 		/* only delete if we'd added ire's before */
12843 		if (ipif->ipif_isv6)
12844 			ipif_delete_ires_v6(ipif);
12845 		else
12846 			ipif_delete_ires_v4(ipif);
12847 	}
12848 
12849 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
12850 		/*
12851 		 * Since the interface is now down, it may have just become
12852 		 * inactive.  Note that this needs to be done even for a
12853 		 * lll_logical_down(), or ARP entries will not get correctly
12854 		 * restored when the interface comes back up.
12855 		 */
12856 		if (IS_UNDER_IPMP(ill))
12857 			ipmp_ill_refresh_active(ill);
12858 	}
12859 
12860 	/*
12861 	 * neighbor-discovery or arp entries for this interface. The ipif
12862 	 * has to be quiesced, so we walk all the nce's and delete those
12863 	 * that point at the ipif->ipif_ill. At the same time, we also
12864 	 * update IPMP so that ipifs for data addresses are unbound. We dont
12865 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
12866 	 * that for ipif_down_tail()
12867 	 */
12868 	ipif_nce_down(ipif);
12869 
12870 	/*
12871 	 * If this is the last ipif on the ill, we also need to remove
12872 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
12873 	 * never succeed.
12874 	 */
12875 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
12876 		ire_walk_ill(0, 0, ill_downi, ill, ill);
12877 
12878 	/*
12879 	 * Walk all CONNs that can have a reference on an ire for this
12880 	 * ipif (we actually walk all that now have stale references).
12881 	 */
12882 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
12883 
12884 	/*
12885 	 * If mp is NULL the caller will wait for the appropriate refcnt.
12886 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
12887 	 * and ill_delete -> ipif_free -> ipif_down
12888 	 */
12889 	if (mp == NULL) {
12890 		ASSERT(q == NULL);
12891 		return (0);
12892 	}
12893 
12894 	if (CONN_Q(q)) {
12895 		connp = Q_TO_CONN(q);
12896 		mutex_enter(&connp->conn_lock);
12897 	} else {
12898 		connp = NULL;
12899 	}
12900 	mutex_enter(&ill->ill_lock);
12901 	/*
12902 	 * Are there any ire's pointing to this ipif that are still active ?
12903 	 * If this is the last ipif going down, are there any ire's pointing
12904 	 * to this ill that are still active ?
12905 	 */
12906 	if (ipif_is_quiescent(ipif)) {
12907 		mutex_exit(&ill->ill_lock);
12908 		if (connp != NULL)
12909 			mutex_exit(&connp->conn_lock);
12910 		return (0);
12911 	}
12912 
12913 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
12914 	    ill->ill_name, (void *)ill));
12915 	/*
12916 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
12917 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
12918 	 * which in turn is called by the last refrele on the ipif/ill/ire.
12919 	 */
12920 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
12921 	if (!success) {
12922 		/* The conn is closing. So just return */
12923 		ASSERT(connp != NULL);
12924 		mutex_exit(&ill->ill_lock);
12925 		mutex_exit(&connp->conn_lock);
12926 		return (EINTR);
12927 	}
12928 
12929 	mutex_exit(&ill->ill_lock);
12930 	if (connp != NULL)
12931 		mutex_exit(&connp->conn_lock);
12932 	return (EINPROGRESS);
12933 }
12934 
12935 int
12936 ipif_down_tail(ipif_t *ipif)
12937 {
12938 	ill_t	*ill = ipif->ipif_ill;
12939 	int	err = 0;
12940 
12941 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
12942 	    ill_t *, ill, ipif_t *, ipif);
12943 
12944 	/*
12945 	 * Skip any loopback interface (null wq).
12946 	 * If this is the last logical interface on the ill
12947 	 * have ill_dl_down tell the driver we are gone (unbind)
12948 	 * Note that lun 0 can ipif_down even though
12949 	 * there are other logical units that are up.
12950 	 * This occurs e.g. when we change a "significant" IFF_ flag.
12951 	 */
12952 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
12953 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12954 	    ill->ill_dl_up) {
12955 		ill_dl_down(ill);
12956 	}
12957 	if (!ipif->ipif_isv6)
12958 		err = ipif_arp_down(ipif);
12959 
12960 	ill->ill_logical_down = 0;
12961 
12962 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
12963 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
12964 	return (err);
12965 }
12966 
12967 /*
12968  * Bring interface logically down without bringing the physical interface
12969  * down e.g. when the netmask is changed. This avoids long lasting link
12970  * negotiations between an ethernet interface and a certain switches.
12971  */
12972 static int
12973 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12974 {
12975 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
12976 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
12977 
12978 	/*
12979 	 * The ill_logical_down flag is a transient flag. It is set here
12980 	 * and is cleared once the down has completed in ipif_down_tail.
12981 	 * This flag does not indicate whether the ill stream is in the
12982 	 * DL_BOUND state with the driver. Instead this flag is used by
12983 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
12984 	 * the driver. The state of the ill stream i.e. whether it is
12985 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
12986 	 */
12987 	ipif->ipif_ill->ill_logical_down = 1;
12988 	return (ipif_down(ipif, q, mp));
12989 }
12990 
12991 /*
12992  * Initiate deallocate of an IPIF. Always called as writer. Called by
12993  * ill_delete or ip_sioctl_removeif.
12994  */
12995 static void
12996 ipif_free(ipif_t *ipif)
12997 {
12998 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12999 
13000 	ASSERT(IAM_WRITER_IPIF(ipif));
13001 
13002 	if (ipif->ipif_recovery_id != 0)
13003 		(void) untimeout(ipif->ipif_recovery_id);
13004 	ipif->ipif_recovery_id = 0;
13005 
13006 	/*
13007 	 * Take down the interface. We can be called either from ill_delete
13008 	 * or from ip_sioctl_removeif.
13009 	 */
13010 	(void) ipif_down(ipif, NULL, NULL);
13011 
13012 	/*
13013 	 * Now that the interface is down, there's no chance it can still
13014 	 * become a duplicate.  Cancel any timer that may have been set while
13015 	 * tearing down.
13016 	 */
13017 	if (ipif->ipif_recovery_id != 0)
13018 		(void) untimeout(ipif->ipif_recovery_id);
13019 	ipif->ipif_recovery_id = 0;
13020 
13021 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13022 	/* Remove pointers to this ill in the multicast routing tables */
13023 	reset_mrt_vif_ipif(ipif);
13024 	/* If necessary, clear the cached source ipif rotor. */
13025 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13026 		ipif->ipif_ill->ill_src_ipif = NULL;
13027 	rw_exit(&ipst->ips_ill_g_lock);
13028 }
13029 
13030 static void
13031 ipif_free_tail(ipif_t *ipif)
13032 {
13033 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13034 
13035 	/*
13036 	 * Need to hold both ill_g_lock and ill_lock while
13037 	 * inserting or removing an ipif from the linked list
13038 	 * of ipifs hanging off the ill.
13039 	 */
13040 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13041 
13042 #ifdef DEBUG
13043 	ipif_trace_cleanup(ipif);
13044 #endif
13045 
13046 	/* Ask SCTP to take it out of it list */
13047 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13048 
13049 	/* Get it out of the ILL interface list. */
13050 	ipif_remove(ipif);
13051 	rw_exit(&ipst->ips_ill_g_lock);
13052 
13053 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13054 	ASSERT(ipif->ipif_recovery_id == 0);
13055 	ASSERT(ipif->ipif_ire_local == NULL);
13056 
13057 	/* Free the memory. */
13058 	mi_free(ipif);
13059 }
13060 
13061 /*
13062  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13063  * is zero.
13064  */
13065 void
13066 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13067 {
13068 	char	lbuf[LIFNAMSIZ];
13069 	char	*name;
13070 	size_t	name_len;
13071 
13072 	buf[0] = '\0';
13073 	name = ipif->ipif_ill->ill_name;
13074 	name_len = ipif->ipif_ill->ill_name_length;
13075 	if (ipif->ipif_id != 0) {
13076 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13077 		    ipif->ipif_id);
13078 		name = lbuf;
13079 		name_len = mi_strlen(name) + 1;
13080 	}
13081 	len -= 1;
13082 	buf[len] = '\0';
13083 	len = MIN(len, name_len);
13084 	bcopy(name, buf, len);
13085 }
13086 
13087 /*
13088  * Sets `buf' to an ill name.
13089  */
13090 void
13091 ill_get_name(const ill_t *ill, char *buf, int len)
13092 {
13093 	char	*name;
13094 	size_t	name_len;
13095 
13096 	name = ill->ill_name;
13097 	name_len = ill->ill_name_length;
13098 	len -= 1;
13099 	buf[len] = '\0';
13100 	len = MIN(len, name_len);
13101 	bcopy(name, buf, len);
13102 }
13103 
13104 /*
13105  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13106  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13107  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13108  * (May be called as writer.)
13109  */
13110 static ipif_t *
13111 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13112     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13113 {
13114 	char	*cp;
13115 	char	*endp;
13116 	long	id;
13117 	ill_t	*ill;
13118 	ipif_t	*ipif;
13119 	uint_t	ire_type;
13120 	boolean_t did_alloc = B_FALSE;
13121 
13122 	/*
13123 	 * If the caller wants to us to create the ipif, make sure we have a
13124 	 * valid zoneid
13125 	 */
13126 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13127 
13128 	if (namelen == 0) {
13129 		return (NULL);
13130 	}
13131 
13132 	*exists = B_FALSE;
13133 	/* Look for a colon in the name. */
13134 	endp = &name[namelen];
13135 	for (cp = endp; --cp > name; ) {
13136 		if (*cp == IPIF_SEPARATOR_CHAR)
13137 			break;
13138 	}
13139 
13140 	if (*cp == IPIF_SEPARATOR_CHAR) {
13141 		/*
13142 		 * Reject any non-decimal aliases for logical
13143 		 * interfaces. Aliases with leading zeroes
13144 		 * are also rejected as they introduce ambiguity
13145 		 * in the naming of the interfaces.
13146 		 * In order to confirm with existing semantics,
13147 		 * and to not break any programs/script relying
13148 		 * on that behaviour, if<0>:0 is considered to be
13149 		 * a valid interface.
13150 		 *
13151 		 * If alias has two or more digits and the first
13152 		 * is zero, fail.
13153 		 */
13154 		if (&cp[2] < endp && cp[1] == '0') {
13155 			return (NULL);
13156 		}
13157 	}
13158 
13159 	if (cp <= name) {
13160 		cp = endp;
13161 	} else {
13162 		*cp = '\0';
13163 	}
13164 
13165 	/*
13166 	 * Look up the ILL, based on the portion of the name
13167 	 * before the slash. ill_lookup_on_name returns a held ill.
13168 	 * Temporary to check whether ill exists already. If so
13169 	 * ill_lookup_on_name will clear it.
13170 	 */
13171 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13172 	    &did_alloc, ipst);
13173 	if (cp != endp)
13174 		*cp = IPIF_SEPARATOR_CHAR;
13175 	if (ill == NULL)
13176 		return (NULL);
13177 
13178 	/* Establish the unit number in the name. */
13179 	id = 0;
13180 	if (cp < endp && *endp == '\0') {
13181 		/* If there was a colon, the unit number follows. */
13182 		cp++;
13183 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13184 			ill_refrele(ill);
13185 			return (NULL);
13186 		}
13187 	}
13188 
13189 	mutex_enter(&ill->ill_lock);
13190 	/* Now see if there is an IPIF with this unit number. */
13191 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13192 		if (ipif->ipif_id == id) {
13193 			if (zoneid != ALL_ZONES &&
13194 			    zoneid != ipif->ipif_zoneid &&
13195 			    ipif->ipif_zoneid != ALL_ZONES) {
13196 				mutex_exit(&ill->ill_lock);
13197 				ill_refrele(ill);
13198 				return (NULL);
13199 			}
13200 			if (IPIF_CAN_LOOKUP(ipif)) {
13201 				ipif_refhold_locked(ipif);
13202 				mutex_exit(&ill->ill_lock);
13203 				if (!did_alloc)
13204 					*exists = B_TRUE;
13205 				/*
13206 				 * Drop locks before calling ill_refrele
13207 				 * since it can potentially call into
13208 				 * ipif_ill_refrele_tail which can end up
13209 				 * in trying to acquire any lock.
13210 				 */
13211 				ill_refrele(ill);
13212 				return (ipif);
13213 			}
13214 		}
13215 	}
13216 
13217 	if (!do_alloc) {
13218 		mutex_exit(&ill->ill_lock);
13219 		ill_refrele(ill);
13220 		return (NULL);
13221 	}
13222 
13223 	/*
13224 	 * If none found, atomically allocate and return a new one.
13225 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13226 	 * to support "receive only" use of lo0:1 etc. as is still done
13227 	 * below as an initial guess.
13228 	 * However, this is now likely to be overriden later in ipif_up_done()
13229 	 * when we know for sure what address has been configured on the
13230 	 * interface, since we might have more than one loopback interface
13231 	 * with a loopback address, e.g. in the case of zones, and all the
13232 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13233 	 */
13234 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13235 		ire_type = IRE_LOOPBACK;
13236 	else
13237 		ire_type = IRE_LOCAL;
13238 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
13239 	if (ipif != NULL)
13240 		ipif_refhold_locked(ipif);
13241 	mutex_exit(&ill->ill_lock);
13242 	ill_refrele(ill);
13243 	return (ipif);
13244 }
13245 
13246 /*
13247  * This routine is called whenever a new address comes up on an ipif.  If
13248  * we are configured to respond to address mask requests, then we are supposed
13249  * to broadcast an address mask reply at this time.  This routine is also
13250  * called if we are already up, but a netmask change is made.  This is legal
13251  * but might not make the system manager very popular.	(May be called
13252  * as writer.)
13253  */
13254 void
13255 ipif_mask_reply(ipif_t *ipif)
13256 {
13257 	icmph_t	*icmph;
13258 	ipha_t	*ipha;
13259 	mblk_t	*mp;
13260 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13261 	ip_xmit_attr_t ixas;
13262 
13263 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13264 
13265 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13266 		return;
13267 
13268 	/* ICMP mask reply is IPv4 only */
13269 	ASSERT(!ipif->ipif_isv6);
13270 	/* ICMP mask reply is not for a loopback interface */
13271 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13272 
13273 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13274 		return;
13275 
13276 	mp = allocb(REPLY_LEN, BPRI_HI);
13277 	if (mp == NULL)
13278 		return;
13279 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13280 
13281 	ipha = (ipha_t *)mp->b_rptr;
13282 	bzero(ipha, REPLY_LEN);
13283 	*ipha = icmp_ipha;
13284 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13285 	ipha->ipha_src = ipif->ipif_lcl_addr;
13286 	ipha->ipha_dst = ipif->ipif_brd_addr;
13287 	ipha->ipha_length = htons(REPLY_LEN);
13288 	ipha->ipha_ident = 0;
13289 
13290 	icmph = (icmph_t *)&ipha[1];
13291 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13292 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13293 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13294 
13295 	bzero(&ixas, sizeof (ixas));
13296 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13297 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13298 	ixas.ixa_zoneid = ALL_ZONES;
13299 	ixas.ixa_ifindex = 0;
13300 	ixas.ixa_ipst = ipst;
13301 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13302 	(void) ip_output_simple(mp, &ixas);
13303 	ixa_cleanup(&ixas);
13304 #undef	REPLY_LEN
13305 }
13306 
13307 /*
13308  * Join the ipif specific multicast groups.
13309  * Must be called after a mapping has been set up in the resolver.  (Always
13310  * called as writer.)
13311  */
13312 void
13313 ipif_multicast_up(ipif_t *ipif)
13314 {
13315 	int err;
13316 	ill_t *ill;
13317 	ilm_t *ilm;
13318 
13319 	ASSERT(IAM_WRITER_IPIF(ipif));
13320 
13321 	ill = ipif->ipif_ill;
13322 
13323 	ip1dbg(("ipif_multicast_up\n"));
13324 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13325 	    ipif->ipif_allhosts_ilm != NULL)
13326 		return;
13327 
13328 	if (ipif->ipif_isv6) {
13329 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13330 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13331 
13332 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13333 
13334 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13335 			return;
13336 
13337 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13338 
13339 		/*
13340 		 * Join the all hosts multicast address.  We skip this for
13341 		 * underlying IPMP interfaces since they should be invisible.
13342 		 */
13343 		if (!IS_UNDER_IPMP(ill)) {
13344 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13345 			    &err);
13346 			if (ilm == NULL) {
13347 				ASSERT(err != 0);
13348 				ip0dbg(("ipif_multicast_up: "
13349 				    "all_hosts_mcast failed %d\n", err));
13350 				return;
13351 			}
13352 			ipif->ipif_allhosts_ilm = ilm;
13353 		}
13354 
13355 		/*
13356 		 * Enable multicast for the solicited node multicast address.
13357 		 * If IPMP we need to put the membership on the upper ill.
13358 		 */
13359 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13360 			ill_t *mcast_ill = NULL;
13361 			boolean_t need_refrele;
13362 
13363 			if (IS_UNDER_IPMP(ill) &&
13364 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13365 				need_refrele = B_TRUE;
13366 			} else {
13367 				mcast_ill = ill;
13368 				need_refrele = B_FALSE;
13369 			}
13370 
13371 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13372 			    ipif->ipif_zoneid, &err);
13373 			if (need_refrele)
13374 				ill_refrele(mcast_ill);
13375 
13376 			if (ilm == NULL) {
13377 				ASSERT(err != 0);
13378 				ip0dbg(("ipif_multicast_up: solicited MC"
13379 				    " failed %d\n", err));
13380 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13381 					ipif->ipif_allhosts_ilm = NULL;
13382 					(void) ip_delmulti(ilm);
13383 				}
13384 				return;
13385 			}
13386 			ipif->ipif_solmulti_ilm = ilm;
13387 		}
13388 	} else {
13389 		in6_addr_t v6group;
13390 
13391 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13392 			return;
13393 
13394 		/* Join the all hosts multicast address */
13395 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13396 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13397 
13398 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13399 		if (ilm == NULL) {
13400 			ASSERT(err != 0);
13401 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13402 			return;
13403 		}
13404 		ipif->ipif_allhosts_ilm = ilm;
13405 	}
13406 }
13407 
13408 /*
13409  * Blow away any multicast groups that we joined in ipif_multicast_up().
13410  * (ilms from explicit memberships are handled in conn_update_ill.)
13411  */
13412 void
13413 ipif_multicast_down(ipif_t *ipif)
13414 {
13415 	ASSERT(IAM_WRITER_IPIF(ipif));
13416 
13417 	ip1dbg(("ipif_multicast_down\n"));
13418 
13419 	if (ipif->ipif_allhosts_ilm != NULL) {
13420 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13421 		ipif->ipif_allhosts_ilm = NULL;
13422 	}
13423 	if (ipif->ipif_solmulti_ilm != NULL) {
13424 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13425 		ipif->ipif_solmulti_ilm = NULL;
13426 	}
13427 }
13428 
13429 /*
13430  * Used when an interface comes up to recreate any extra routes on this
13431  * interface.
13432  */
13433 int
13434 ill_recover_saved_ire(ill_t *ill)
13435 {
13436 	mblk_t		*mp;
13437 	ip_stack_t	*ipst = ill->ill_ipst;
13438 
13439 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13440 
13441 	mutex_enter(&ill->ill_saved_ire_lock);
13442 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13443 		ire_t		*ire, *nire;
13444 		ifrt_t		*ifrt;
13445 
13446 		ifrt = (ifrt_t *)mp->b_rptr;
13447 		/*
13448 		 * Create a copy of the IRE with the saved address and netmask.
13449 		 */
13450 		if (ill->ill_isv6) {
13451 			ire = ire_create_v6(
13452 			    &ifrt->ifrt_v6addr,
13453 			    &ifrt->ifrt_v6mask,
13454 			    &ifrt->ifrt_v6gateway_addr,
13455 			    ifrt->ifrt_type,
13456 			    ill,
13457 			    ifrt->ifrt_zoneid,
13458 			    ifrt->ifrt_flags,
13459 			    NULL,
13460 			    ipst);
13461 		} else {
13462 			ire = ire_create(
13463 			    (uint8_t *)&ifrt->ifrt_addr,
13464 			    (uint8_t *)&ifrt->ifrt_mask,
13465 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13466 			    ifrt->ifrt_type,
13467 			    ill,
13468 			    ifrt->ifrt_zoneid,
13469 			    ifrt->ifrt_flags,
13470 			    NULL,
13471 			    ipst);
13472 		}
13473 		if (ire == NULL) {
13474 			mutex_exit(&ill->ill_saved_ire_lock);
13475 			return (ENOMEM);
13476 		}
13477 
13478 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13479 			if (ill->ill_isv6) {
13480 				ire->ire_setsrc_addr_v6 =
13481 				    ifrt->ifrt_v6setsrc_addr;
13482 			} else {
13483 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13484 			}
13485 		}
13486 
13487 		/*
13488 		 * Some software (for example, GateD and Sun Cluster) attempts
13489 		 * to create (what amount to) IRE_PREFIX routes with the
13490 		 * loopback address as the gateway.  This is primarily done to
13491 		 * set up prefixes with the RTF_REJECT flag set (for example,
13492 		 * when generating aggregate routes.)
13493 		 *
13494 		 * If the IRE type (as defined by ill->ill_net_type) is
13495 		 * IRE_LOOPBACK, then we map the request into a
13496 		 * IRE_IF_NORESOLVER.
13497 		 */
13498 		if (ill->ill_net_type == IRE_LOOPBACK)
13499 			ire->ire_type = IRE_IF_NORESOLVER;
13500 
13501 		/*
13502 		 * ire held by ire_add, will be refreled' towards the
13503 		 * the end of ipif_up_done
13504 		 */
13505 		nire = ire_add(ire);
13506 		/*
13507 		 * Check if it was a duplicate entry. This handles
13508 		 * the case of two racing route adds for the same route
13509 		 */
13510 		if (nire == NULL) {
13511 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13512 		} else if (nire != ire) {
13513 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13514 			    (void *)nire));
13515 			ire_delete(nire);
13516 		} else {
13517 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13518 			    (void *)nire));
13519 		}
13520 		if (nire != NULL)
13521 			ire_refrele(nire);
13522 	}
13523 	mutex_exit(&ill->ill_saved_ire_lock);
13524 	return (0);
13525 }
13526 
13527 /*
13528  * Used to set the netmask and broadcast address to default values when the
13529  * interface is brought up.  (Always called as writer.)
13530  */
13531 static void
13532 ipif_set_default(ipif_t *ipif)
13533 {
13534 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13535 
13536 	if (!ipif->ipif_isv6) {
13537 		/*
13538 		 * Interface holds an IPv4 address. Default
13539 		 * mask is the natural netmask.
13540 		 */
13541 		if (!ipif->ipif_net_mask) {
13542 			ipaddr_t	v4mask;
13543 
13544 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13545 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13546 		}
13547 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13548 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13549 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13550 		} else {
13551 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13552 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13553 		}
13554 		/*
13555 		 * NOTE: SunOS 4.X does this even if the broadcast address
13556 		 * has been already set thus we do the same here.
13557 		 */
13558 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13559 			ipaddr_t	v4addr;
13560 
13561 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13562 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13563 		}
13564 	} else {
13565 		/*
13566 		 * Interface holds an IPv6-only address.  Default
13567 		 * mask is all-ones.
13568 		 */
13569 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13570 			ipif->ipif_v6net_mask = ipv6_all_ones;
13571 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13572 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13573 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13574 		} else {
13575 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13576 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13577 		}
13578 	}
13579 }
13580 
13581 /*
13582  * Return 0 if this address can be used as local address without causing
13583  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13584  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13585  * Note that the same IPv6 link-local address is allowed as long as the ills
13586  * are not on the same link.
13587  */
13588 int
13589 ip_addr_availability_check(ipif_t *new_ipif)
13590 {
13591 	in6_addr_t our_v6addr;
13592 	ill_t *ill;
13593 	ipif_t *ipif;
13594 	ill_walk_context_t ctx;
13595 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13596 
13597 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13598 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13599 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13600 
13601 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13602 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13603 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13604 		return (0);
13605 
13606 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13607 
13608 	if (new_ipif->ipif_isv6)
13609 		ill = ILL_START_WALK_V6(&ctx, ipst);
13610 	else
13611 		ill = ILL_START_WALK_V4(&ctx, ipst);
13612 
13613 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13614 		for (ipif = ill->ill_ipif; ipif != NULL;
13615 		    ipif = ipif->ipif_next) {
13616 			if ((ipif == new_ipif) ||
13617 			    !(ipif->ipif_flags & IPIF_UP) ||
13618 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13619 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13620 			    &our_v6addr))
13621 				continue;
13622 
13623 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13624 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13625 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13626 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13627 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13628 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13629 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13630 				continue;
13631 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13632 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13633 				continue;
13634 			else if (new_ipif->ipif_ill == ill)
13635 				return (EADDRINUSE);
13636 			else
13637 				return (EADDRNOTAVAIL);
13638 		}
13639 	}
13640 
13641 	return (0);
13642 }
13643 
13644 /*
13645  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13646  * IREs for the ipif.
13647  * When the routine returns EINPROGRESS then mp has been consumed and
13648  * the ioctl will be acked from ip_rput_dlpi.
13649  */
13650 int
13651 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13652 {
13653 	ill_t		*ill = ipif->ipif_ill;
13654 	boolean_t 	isv6 = ipif->ipif_isv6;
13655 	int		err = 0;
13656 	boolean_t	success;
13657 	uint_t		ipif_orig_id;
13658 	ip_stack_t	*ipst = ill->ill_ipst;
13659 
13660 	ASSERT(IAM_WRITER_IPIF(ipif));
13661 
13662 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13663 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13664 	    ill_t *, ill, ipif_t *, ipif);
13665 
13666 	/* Shouldn't get here if it is already up. */
13667 	if (ipif->ipif_flags & IPIF_UP)
13668 		return (EALREADY);
13669 
13670 	/*
13671 	 * If this is a request to bring up a data address on an interface
13672 	 * under IPMP, then move the address to its IPMP meta-interface and
13673 	 * try to bring it up.  One complication is that the zeroth ipif for
13674 	 * an ill is special, in that every ill always has one, and that code
13675 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13676 	 */
13677 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13678 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13679 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13680 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13681 
13682 		/*
13683 		 * The ipif being brought up should be quiesced.  If it's not,
13684 		 * something has gone amiss and we need to bail out.  (If it's
13685 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13686 		 */
13687 		mutex_enter(&ill->ill_lock);
13688 		if (!ipif_is_quiescent(ipif)) {
13689 			mutex_exit(&ill->ill_lock);
13690 			return (EINVAL);
13691 		}
13692 		mutex_exit(&ill->ill_lock);
13693 
13694 		/*
13695 		 * If we're going to need to allocate ipifs, do it prior
13696 		 * to starting the move (and grabbing locks).
13697 		 */
13698 		if (ipif->ipif_id == 0) {
13699 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13700 			    B_FALSE);
13701 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13702 			    B_FALSE);
13703 			if (moveipif == NULL || stubipif == NULL) {
13704 				mi_free(moveipif);
13705 				mi_free(stubipif);
13706 				return (ENOMEM);
13707 			}
13708 		}
13709 
13710 		/*
13711 		 * Grab or transfer the ipif to move.  During the move, keep
13712 		 * ill_g_lock held to prevent any ill walker threads from
13713 		 * seeing things in an inconsistent state.
13714 		 */
13715 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13716 		if (ipif->ipif_id != 0) {
13717 			ipif_remove(ipif);
13718 		} else {
13719 			ipif_transfer(ipif, moveipif, stubipif);
13720 			ipif = moveipif;
13721 		}
13722 
13723 		/*
13724 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13725 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13726 		 * replace that one.  Otherwise, pick the next available slot.
13727 		 */
13728 		ipif->ipif_ill = ipmp_ill;
13729 		ipif_orig_id = ipif->ipif_id;
13730 
13731 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13732 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13733 			ipif = ipmp_ill->ill_ipif;
13734 		} else {
13735 			ipif->ipif_id = -1;
13736 			if (ipif_insert(ipif, B_FALSE) != 0) {
13737 				/*
13738 				 * No more available ipif_id's -- put it back
13739 				 * on the original ill and fail the operation.
13740 				 * Since we're writer on the ill, we can be
13741 				 * sure our old slot is still available.
13742 				 */
13743 				ipif->ipif_id = ipif_orig_id;
13744 				ipif->ipif_ill = ill;
13745 				if (ipif_orig_id == 0) {
13746 					ipif_transfer(ipif, ill->ill_ipif,
13747 					    NULL);
13748 				} else {
13749 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13750 				}
13751 				rw_exit(&ipst->ips_ill_g_lock);
13752 				return (ENOMEM);
13753 			}
13754 		}
13755 		rw_exit(&ipst->ips_ill_g_lock);
13756 
13757 		/*
13758 		 * Tell SCTP that the ipif has moved.  Note that even if we
13759 		 * had to allocate a new ipif, the original sequence id was
13760 		 * preserved and therefore SCTP won't know.
13761 		 */
13762 		sctp_move_ipif(ipif, ill, ipmp_ill);
13763 
13764 		/*
13765 		 * If the ipif being brought up was on slot zero, then we
13766 		 * first need to bring up the placeholder we stuck there.  In
13767 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13768 		 * call to ipif_up() itself, if we successfully bring up the
13769 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13770 		 */
13771 		if (ipif_orig_id == 0) {
13772 			ASSERT(ill->ill_move_ipif == NULL);
13773 			ill->ill_move_ipif = ipif;
13774 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
13775 				ASSERT(ill->ill_move_ipif == NULL);
13776 			if (err != EINPROGRESS)
13777 				ill->ill_move_ipif = NULL;
13778 			return (err);
13779 		}
13780 
13781 		/*
13782 		 * Bring it up on the IPMP ill.
13783 		 */
13784 		return (ipif_up(ipif, q, mp));
13785 	}
13786 
13787 	/* Skip arp/ndp for any loopback interface. */
13788 	if (ill->ill_wq != NULL) {
13789 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13790 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
13791 
13792 		if (!ill->ill_dl_up) {
13793 			/*
13794 			 * ill_dl_up is not yet set. i.e. we are yet to
13795 			 * DL_BIND with the driver and this is the first
13796 			 * logical interface on the ill to become "up".
13797 			 * Tell the driver to get going (via DL_BIND_REQ).
13798 			 * Note that changing "significant" IFF_ flags
13799 			 * address/netmask etc cause a down/up dance, but
13800 			 * does not cause an unbind (DL_UNBIND) with the driver
13801 			 */
13802 			return (ill_dl_up(ill, ipif, mp, q));
13803 		}
13804 
13805 		/*
13806 		 * ipif_resolver_up may end up needeing to bind/attach
13807 		 * the ARP stream, which in turn necessitates a
13808 		 * DLPI message exchange with the driver. ioctls are
13809 		 * serialized and so we cannot send more than one
13810 		 * interface up message at a time. If ipif_resolver_up
13811 		 * does need to wait for the DLPI handshake for the ARP stream,
13812 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
13813 		 */
13814 
13815 		ASSERT(connp != NULL || !CONN_Q(q));
13816 		if (connp != NULL)
13817 			mutex_enter(&connp->conn_lock);
13818 		mutex_enter(&ill->ill_lock);
13819 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13820 		mutex_exit(&ill->ill_lock);
13821 		if (connp != NULL)
13822 			mutex_exit(&connp->conn_lock);
13823 		if (!success)
13824 			return (EINTR);
13825 
13826 		/*
13827 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
13828 		 * complete when ipif_ndp_up returns.
13829 		 */
13830 		err = ipif_resolver_up(ipif, Res_act_initial);
13831 		if (err == EINPROGRESS) {
13832 			/* We will complete it in arp_bringup_done() */
13833 			return (err);
13834 		}
13835 
13836 		if (isv6 && err == 0)
13837 			err = ipif_ndp_up(ipif, B_TRUE);
13838 
13839 		ASSERT(err != EINPROGRESS);
13840 		mp = ipsq_pending_mp_get(ipsq, &connp);
13841 		ASSERT(mp != NULL);
13842 		if (err != 0)
13843 			return (err);
13844 	} else {
13845 		/*
13846 		 * Interfaces without underlying hardware don't do duplicate
13847 		 * address detection.
13848 		 */
13849 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
13850 		ipif->ipif_addr_ready = 1;
13851 		err = ill_add_ires(ill);
13852 		/* allocation failure? */
13853 		if (err != 0)
13854 			return (err);
13855 	}
13856 
13857 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
13858 	if (err == 0 && ill->ill_move_ipif != NULL) {
13859 		ipif = ill->ill_move_ipif;
13860 		ill->ill_move_ipif = NULL;
13861 		return (ipif_up(ipif, q, mp));
13862 	}
13863 	return (err);
13864 }
13865 
13866 /*
13867  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
13868  * The identical set of IREs need to be removed in ill_delete_ires().
13869  */
13870 int
13871 ill_add_ires(ill_t *ill)
13872 {
13873 	ire_t	*ire;
13874 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
13875 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
13876 
13877 	if (ill->ill_ire_multicast != NULL)
13878 		return (0);
13879 
13880 	/*
13881 	 * provide some dummy ire_addr for creating the ire.
13882 	 */
13883 	if (ill->ill_isv6) {
13884 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
13885 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
13886 	} else {
13887 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
13888 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
13889 	}
13890 	if (ire == NULL)
13891 		return (ENOMEM);
13892 
13893 	ill->ill_ire_multicast = ire;
13894 	return (0);
13895 }
13896 
13897 void
13898 ill_delete_ires(ill_t *ill)
13899 {
13900 	if (ill->ill_ire_multicast != NULL) {
13901 		/*
13902 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
13903 		 * which was taken without any th_tracing enabled.
13904 		 * We also mark it as condemned (note that it was never added)
13905 		 * so that caching conn's can move off of it.
13906 		 */
13907 		ire_make_condemned(ill->ill_ire_multicast);
13908 		ire_refrele_notr(ill->ill_ire_multicast);
13909 		ill->ill_ire_multicast = NULL;
13910 	}
13911 }
13912 
13913 /*
13914  * Perform a bind for the physical device.
13915  * When the routine returns EINPROGRESS then mp has been consumed and
13916  * the ioctl will be acked from ip_rput_dlpi.
13917  * Allocate an unbind message and save it until ipif_down.
13918  */
13919 static int
13920 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
13921 {
13922 	mblk_t	*bind_mp = NULL;
13923 	mblk_t	*unbind_mp = NULL;
13924 	conn_t	*connp;
13925 	boolean_t success;
13926 	int	err;
13927 
13928 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
13929 
13930 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
13931 	ASSERT(IAM_WRITER_ILL(ill));
13932 	ASSERT(mp != NULL);
13933 
13934 	/*
13935 	 * Make sure we have an IRE_MULTICAST in case we immediately
13936 	 * start receiving packets.
13937 	 */
13938 	err = ill_add_ires(ill);
13939 	if (err != 0)
13940 		goto bad;
13941 
13942 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
13943 	    DL_BIND_REQ);
13944 	if (bind_mp == NULL)
13945 		goto bad;
13946 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
13947 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
13948 
13949 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
13950 	if (unbind_mp == NULL)
13951 		goto bad;
13952 
13953 	/*
13954 	 * Record state needed to complete this operation when the
13955 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
13956 	 */
13957 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13958 	ASSERT(connp != NULL || !CONN_Q(q));
13959 	GRAB_CONN_LOCK(q);
13960 	mutex_enter(&ipif->ipif_ill->ill_lock);
13961 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13962 	mutex_exit(&ipif->ipif_ill->ill_lock);
13963 	RELEASE_CONN_LOCK(q);
13964 	if (!success)
13965 		goto bad;
13966 
13967 	/*
13968 	 * Save the unbind message for ill_dl_down(); it will be consumed when
13969 	 * the interface goes down.
13970 	 */
13971 	ASSERT(ill->ill_unbind_mp == NULL);
13972 	ill->ill_unbind_mp = unbind_mp;
13973 
13974 	ill_dlpi_send(ill, bind_mp);
13975 	/* Send down link-layer capabilities probe if not already done. */
13976 	ill_capability_probe(ill);
13977 
13978 	/*
13979 	 * Sysid used to rely on the fact that netboots set domainname
13980 	 * and the like. Now that miniroot boots aren't strictly netboots
13981 	 * and miniroot network configuration is driven from userland
13982 	 * these things still need to be set. This situation can be detected
13983 	 * by comparing the interface being configured here to the one
13984 	 * dhcifname was set to reference by the boot loader. Once sysid is
13985 	 * converted to use dhcp_ipc_getinfo() this call can go away.
13986 	 */
13987 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
13988 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
13989 	    (strlen(srpc_domain) == 0)) {
13990 		if (dhcpinit() != 0)
13991 			cmn_err(CE_WARN, "no cached dhcp response");
13992 	}
13993 
13994 	/*
13995 	 * This operation will complete in ip_rput_dlpi with either
13996 	 * a DL_BIND_ACK or DL_ERROR_ACK.
13997 	 */
13998 	return (EINPROGRESS);
13999 bad:
14000 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14001 
14002 	freemsg(bind_mp);
14003 	freemsg(unbind_mp);
14004 	return (ENOMEM);
14005 }
14006 
14007 /* Add room for tcp+ip headers */
14008 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14009 
14010 /*
14011  * DLPI and ARP is up.
14012  * Create all the IREs associated with an interface. Bring up multicast.
14013  * Set the interface flag and finish other initialization
14014  * that potentially had to be deferred to after DL_BIND_ACK.
14015  */
14016 int
14017 ipif_up_done(ipif_t *ipif)
14018 {
14019 	ill_t		*ill = ipif->ipif_ill;
14020 	int		err = 0;
14021 	boolean_t	loopback = B_FALSE;
14022 	boolean_t	update_src_selection = B_TRUE;
14023 	ipif_t		*tmp_ipif;
14024 
14025 	ip1dbg(("ipif_up_done(%s:%u)\n",
14026 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14027 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14028 	    ill_t *, ill, ipif_t *, ipif);
14029 
14030 	/* Check if this is a loopback interface */
14031 	if (ipif->ipif_ill->ill_wq == NULL)
14032 		loopback = B_TRUE;
14033 
14034 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14035 
14036 	/*
14037 	 * If all other interfaces for this ill are down or DEPRECATED,
14038 	 * or otherwise unsuitable for source address selection,
14039 	 * reset the src generation numbers to make sure source
14040 	 * address selection gets to take this new ipif into account.
14041 	 * No need to hold ill_lock while traversing the ipif list since
14042 	 * we are writer
14043 	 */
14044 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14045 	    tmp_ipif = tmp_ipif->ipif_next) {
14046 		if (((tmp_ipif->ipif_flags &
14047 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14048 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14049 		    (tmp_ipif == ipif))
14050 			continue;
14051 		/* first useable pre-existing interface */
14052 		update_src_selection = B_FALSE;
14053 		break;
14054 	}
14055 	if (update_src_selection)
14056 		ip_update_source_selection(ill->ill_ipst);
14057 
14058 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14059 		nce_t *loop_nce = NULL;
14060 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14061 
14062 		/*
14063 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14064 		 * ipif_lookup_on_name(), but in the case of zones we can have
14065 		 * several loopback addresses on lo0. So all the interfaces with
14066 		 * loopback addresses need to be marked IRE_LOOPBACK.
14067 		 */
14068 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14069 		    htonl(INADDR_LOOPBACK))
14070 			ipif->ipif_ire_type = IRE_LOOPBACK;
14071 		else
14072 			ipif->ipif_ire_type = IRE_LOCAL;
14073 		if (ill->ill_net_type != IRE_LOOPBACK)
14074 			flags |= NCE_F_PUBLISH;
14075 
14076 		/* add unicast nce for the local addr */
14077 		err = nce_lookup_then_add_v4(ill, NULL,
14078 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14079 		    ND_REACHABLE, &loop_nce);
14080 		/* A shared-IP zone sees EEXIST for lo0:N */
14081 		if (err == 0 || err == EEXIST) {
14082 			ipif->ipif_added_nce = 1;
14083 			loop_nce->nce_ipif_cnt++;
14084 			nce_refrele(loop_nce);
14085 			err = 0;
14086 		} else {
14087 			ASSERT(loop_nce == NULL);
14088 			return (err);
14089 		}
14090 	}
14091 
14092 	/* Create all the IREs associated with this interface */
14093 	err = ipif_add_ires_v4(ipif, loopback);
14094 	if (err != 0) {
14095 		/*
14096 		 * see comments about return value from
14097 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14098 		 */
14099 		if (err != EADDRINUSE) {
14100 			(void) ipif_arp_down(ipif);
14101 		} else {
14102 			/*
14103 			 * Make IPMP aware of the deleted ipif so that
14104 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14105 			 * can be completed. Note that we do not want to
14106 			 * destroy the nce that was created on the ipmp_ill
14107 			 * for the active copy of the duplicate address in
14108 			 * use.
14109 			 */
14110 			if (IS_IPMP(ill))
14111 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14112 			err = EADDRNOTAVAIL;
14113 		}
14114 		return (err);
14115 	}
14116 
14117 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14118 		/* Recover any additional IREs entries for this ill */
14119 		(void) ill_recover_saved_ire(ill);
14120 	}
14121 
14122 	if (ill->ill_need_recover_multicast) {
14123 		/*
14124 		 * Need to recover all multicast memberships in the driver.
14125 		 * This had to be deferred until we had attached.  The same
14126 		 * code exists in ipif_up_done_v6() to recover IPv6
14127 		 * memberships.
14128 		 *
14129 		 * Note that it would be preferable to unconditionally do the
14130 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14131 		 * that since ill_join_allmulti() depends on ill_dl_up being
14132 		 * set, and it is not set until we receive a DL_BIND_ACK after
14133 		 * having called ill_dl_up().
14134 		 */
14135 		ill_recover_multicast(ill);
14136 	}
14137 
14138 	if (ill->ill_ipif_up_count == 1) {
14139 		/*
14140 		 * Since the interface is now up, it may now be active.
14141 		 */
14142 		if (IS_UNDER_IPMP(ill))
14143 			ipmp_ill_refresh_active(ill);
14144 
14145 		/*
14146 		 * If this is an IPMP interface, we may now be able to
14147 		 * establish ARP entries.
14148 		 */
14149 		if (IS_IPMP(ill))
14150 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14151 	}
14152 
14153 	/* Join the allhosts multicast address */
14154 	ipif_multicast_up(ipif);
14155 
14156 	if (!loopback && !update_src_selection &&
14157 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14158 		ip_update_source_selection(ill->ill_ipst);
14159 
14160 	if (!loopback && ipif->ipif_addr_ready) {
14161 		/* Broadcast an address mask reply. */
14162 		ipif_mask_reply(ipif);
14163 	}
14164 	/* Perhaps ilgs should use this ill */
14165 	update_conn_ill(NULL, ill->ill_ipst);
14166 
14167 	/*
14168 	 * This had to be deferred until we had bound.  Tell routing sockets and
14169 	 * others that this interface is up if it looks like the address has
14170 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14171 	 * duplicate address detection to do its thing.
14172 	 */
14173 	if (ipif->ipif_addr_ready)
14174 		ipif_up_notify(ipif);
14175 	return (0);
14176 }
14177 
14178 /*
14179  * Add the IREs associated with the ipif.
14180  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14181  */
14182 static int
14183 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14184 {
14185 	ill_t		*ill = ipif->ipif_ill;
14186 	ip_stack_t	*ipst = ill->ill_ipst;
14187 	ire_t		*ire_array[20];
14188 	ire_t		**irep = ire_array;
14189 	ire_t		**irep1;
14190 	ipaddr_t	net_mask = 0;
14191 	ipaddr_t	subnet_mask, route_mask;
14192 	int		err;
14193 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14194 
14195 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14196 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14197 		/*
14198 		 * If we're on a labeled system then make sure that zone-
14199 		 * private addresses have proper remote host database entries.
14200 		 */
14201 		if (is_system_labeled() &&
14202 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14203 		    !tsol_check_interface_address(ipif))
14204 			return (EINVAL);
14205 
14206 		/* Register the source address for __sin6_src_id */
14207 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14208 		    ipif->ipif_zoneid, ipst);
14209 		if (err != 0) {
14210 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14211 			return (err);
14212 		}
14213 
14214 		/* If the interface address is set, create the local IRE. */
14215 		ire_local = ire_create(
14216 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14217 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14218 		    NULL,				/* no gateway */
14219 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14220 		    ipif->ipif_ill,
14221 		    ipif->ipif_zoneid,
14222 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14223 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14224 		    NULL,
14225 		    ipst);
14226 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14227 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14228 		    ipif->ipif_ire_type,
14229 		    ntohl(ipif->ipif_lcl_addr)));
14230 		if (ire_local == NULL) {
14231 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14232 			err = ENOMEM;
14233 			goto bad;
14234 		}
14235 	} else {
14236 		ip1dbg((
14237 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14238 		    ipif->ipif_ire_type,
14239 		    ntohl(ipif->ipif_lcl_addr),
14240 		    (uint_t)ipif->ipif_flags));
14241 	}
14242 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14243 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14244 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14245 	} else {
14246 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14247 	}
14248 
14249 	subnet_mask = ipif->ipif_net_mask;
14250 
14251 	/*
14252 	 * If mask was not specified, use natural netmask of
14253 	 * interface address. Also, store this mask back into the
14254 	 * ipif struct.
14255 	 */
14256 	if (subnet_mask == 0) {
14257 		subnet_mask = net_mask;
14258 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14259 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14260 		    ipif->ipif_v6subnet);
14261 	}
14262 
14263 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14264 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14265 	    ipif->ipif_subnet != INADDR_ANY) {
14266 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14267 
14268 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14269 			route_mask = IP_HOST_MASK;
14270 		} else {
14271 			route_mask = subnet_mask;
14272 		}
14273 
14274 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14275 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14276 		    (void *)ipif, (void *)ill,
14277 		    ill->ill_net_type,
14278 		    ntohl(ipif->ipif_subnet)));
14279 		*irep++ = ire_create(
14280 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
14281 		    (uchar_t *)&route_mask,		/* mask */
14282 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* gateway */
14283 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
14284 		    ill,
14285 		    ipif->ipif_zoneid,
14286 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14287 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14288 		    NULL,
14289 		    ipst);
14290 	}
14291 
14292 	/*
14293 	 * Create any necessary broadcast IREs.
14294 	 */
14295 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14296 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14297 		irep = ipif_create_bcast_ires(ipif, irep);
14298 
14299 	/* If an earlier ire_create failed, get out now */
14300 	for (irep1 = irep; irep1 > ire_array; ) {
14301 		irep1--;
14302 		if (*irep1 == NULL) {
14303 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14304 			err = ENOMEM;
14305 			goto bad;
14306 		}
14307 	}
14308 
14309 	/*
14310 	 * Need to atomically check for IP address availability under
14311 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14312 	 * ills or new ipifs can be added while we are checking availability.
14313 	 */
14314 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14315 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14316 	/* Mark it up, and increment counters. */
14317 	ipif->ipif_flags |= IPIF_UP;
14318 	ill->ill_ipif_up_count++;
14319 	err = ip_addr_availability_check(ipif);
14320 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14321 	rw_exit(&ipst->ips_ill_g_lock);
14322 
14323 	if (err != 0) {
14324 		/*
14325 		 * Our address may already be up on the same ill. In this case,
14326 		 * the ARP entry for our ipif replaced the one for the other
14327 		 * ipif. So we don't want to delete it (otherwise the other ipif
14328 		 * would be unable to send packets).
14329 		 * ip_addr_availability_check() identifies this case for us and
14330 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14331 		 * which is the expected error code.
14332 		 */
14333 		ill->ill_ipif_up_count--;
14334 		ipif->ipif_flags &= ~IPIF_UP;
14335 		goto bad;
14336 	}
14337 
14338 	/*
14339 	 * Add in all newly created IREs.  ire_create_bcast() has
14340 	 * already checked for duplicates of the IRE_BROADCAST type.
14341 	 */
14342 	if (ire_local != NULL) {
14343 		ire_local = ire_add(ire_local);
14344 #ifdef DEBUG
14345 		if (ire_local != NULL) {
14346 			ire_refhold_notr(ire_local);
14347 			ire_refrele(ire_local);
14348 		}
14349 #endif
14350 	}
14351 
14352 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14353 	if (ire_local != NULL)
14354 		ipif->ipif_ire_local = ire_local;
14355 	rw_exit(&ipst->ips_ill_g_lock);
14356 	ire_local = NULL;
14357 
14358 	for (irep1 = irep; irep1 > ire_array; ) {
14359 		irep1--;
14360 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14361 		/* refheld by ire_add. */
14362 		*irep1 = ire_add(*irep1);
14363 		if (*irep1 != NULL) {
14364 			ire_refrele(*irep1);
14365 			*irep1 = NULL;
14366 		}
14367 	}
14368 
14369 	if (!loopback) {
14370 		/*
14371 		 * If the broadcast address has been set, make sure it makes
14372 		 * sense based on the interface address.
14373 		 * Only match on ill since we are sharing broadcast addresses.
14374 		 */
14375 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14376 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14377 			ire_t	*ire;
14378 
14379 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14380 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14381 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14382 
14383 			if (ire == NULL) {
14384 				/*
14385 				 * If there isn't a matching broadcast IRE,
14386 				 * revert to the default for this netmask.
14387 				 */
14388 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14389 				mutex_enter(&ipif->ipif_ill->ill_lock);
14390 				ipif_set_default(ipif);
14391 				mutex_exit(&ipif->ipif_ill->ill_lock);
14392 			} else {
14393 				ire_refrele(ire);
14394 			}
14395 		}
14396 
14397 	}
14398 	return (0);
14399 
14400 bad:
14401 	ip1dbg(("ipif_add_ires: FAILED \n"));
14402 	if (ire_local != NULL)
14403 		ire_delete(ire_local);
14404 	while (irep > ire_array) {
14405 		irep--;
14406 		if (*irep != NULL) {
14407 			ire_delete(*irep);
14408 		}
14409 	}
14410 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14411 
14412 	return (err);
14413 }
14414 
14415 /* Remove all the IREs created by ipif_add_ires_v4 */
14416 void
14417 ipif_delete_ires_v4(ipif_t *ipif)
14418 {
14419 	ill_t		*ill = ipif->ipif_ill;
14420 	ip_stack_t	*ipst = ill->ill_ipst;
14421 	ipaddr_t	net_mask = 0;
14422 	ipaddr_t	subnet_mask, route_mask;
14423 	int		match_args;
14424 	ire_t		*ire;
14425 	boolean_t	loopback;
14426 
14427 	/* Check if this is a loopback interface */
14428 	loopback = (ipif->ipif_ill->ill_wq == NULL);
14429 
14430 	match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_MASK |
14431 	    MATCH_IRE_ZONEONLY;
14432 
14433 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14434 	if ((ire = ipif->ipif_ire_local) != NULL) {
14435 		ipif->ipif_ire_local = NULL;
14436 		rw_exit(&ipst->ips_ill_g_lock);
14437 		/*
14438 		 * Move count to ipif so we don't loose the count due to
14439 		 * a down/up dance.
14440 		 */
14441 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14442 
14443 		ire_delete(ire);
14444 		ire_refrele_notr(ire);
14445 	} else {
14446 		rw_exit(&ipst->ips_ill_g_lock);
14447 	}
14448 
14449 	match_args |= MATCH_IRE_GW;
14450 
14451 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14452 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14453 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14454 	} else {
14455 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14456 	}
14457 
14458 	subnet_mask = ipif->ipif_net_mask;
14459 
14460 	/*
14461 	 * If mask was not specified, use natural netmask of
14462 	 * interface address. Also, store this mask back into the
14463 	 * ipif struct.
14464 	 */
14465 	if (subnet_mask == 0)
14466 		subnet_mask = net_mask;
14467 
14468 	/* Delete the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14469 	if (IS_UNDER_IPMP(ill))
14470 		match_args |= MATCH_IRE_TESTHIDDEN;
14471 
14472 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14473 	    ipif->ipif_subnet != INADDR_ANY) {
14474 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14475 
14476 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14477 			route_mask = IP_HOST_MASK;
14478 		} else {
14479 			route_mask = subnet_mask;
14480 		}
14481 
14482 		ire = ire_ftable_lookup_v4(
14483 		    ipif->ipif_subnet,			/* dest address */
14484 		    route_mask,				/* mask */
14485 		    ipif->ipif_lcl_addr,		/* gateway */
14486 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
14487 		    ill,
14488 		    ipif->ipif_zoneid,
14489 		    NULL,
14490 		    match_args,
14491 		    0,
14492 		    ipst,
14493 		    NULL);
14494 		ASSERT(ire != NULL);
14495 		ire_delete(ire);
14496 		ire_refrele(ire);
14497 	}
14498 
14499 	/*
14500 	 * Create any necessary broadcast IREs.
14501 	 */
14502 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14503 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14504 		ipif_delete_bcast_ires(ipif);
14505 }
14506 
14507 /*
14508  * Checks for availbility of a usable source address (if there is one) when the
14509  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14510  * this selection is done regardless of the destination.
14511  */
14512 boolean_t
14513 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14514     ip_stack_t *ipst)
14515 {
14516 	ipif_t		*ipif = NULL;
14517 	ill_t		*uill;
14518 
14519 	ASSERT(ifindex != 0);
14520 
14521 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14522 	if (uill == NULL)
14523 		return (B_FALSE);
14524 
14525 	mutex_enter(&uill->ill_lock);
14526 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14527 		if (IPIF_IS_CONDEMNED(ipif))
14528 			continue;
14529 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14530 			continue;
14531 		if (!(ipif->ipif_flags & IPIF_UP))
14532 			continue;
14533 		if (ipif->ipif_zoneid != zoneid)
14534 			continue;
14535 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14536 		    ipif->ipif_lcl_addr == INADDR_ANY)
14537 			continue;
14538 		mutex_exit(&uill->ill_lock);
14539 		ill_refrele(uill);
14540 		return (B_TRUE);
14541 	}
14542 	mutex_exit(&uill->ill_lock);
14543 	ill_refrele(uill);
14544 	return (B_FALSE);
14545 }
14546 
14547 /*
14548  * Find an ipif with a good local address on the ill+zoneid.
14549  */
14550 ipif_t *
14551 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14552 {
14553 	ipif_t		*ipif;
14554 
14555 	mutex_enter(&ill->ill_lock);
14556 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14557 		if (IPIF_IS_CONDEMNED(ipif))
14558 			continue;
14559 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14560 			continue;
14561 		if (!(ipif->ipif_flags & IPIF_UP))
14562 			continue;
14563 		if (ipif->ipif_zoneid != zoneid &&
14564 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14565 			continue;
14566 		if (ill->ill_isv6 ?
14567 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14568 		    ipif->ipif_lcl_addr == INADDR_ANY)
14569 			continue;
14570 		ipif_refhold_locked(ipif);
14571 		mutex_exit(&ill->ill_lock);
14572 		return (ipif);
14573 	}
14574 	mutex_exit(&ill->ill_lock);
14575 	return (NULL);
14576 }
14577 
14578 /*
14579  * IP source address type, sorted from worst to best.  For a given type,
14580  * always prefer IP addresses on the same subnet.  All-zones addresses are
14581  * suboptimal because they pose problems with unlabeled destinations.
14582  */
14583 typedef enum {
14584 	IPIF_NONE,
14585 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14586 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14587 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14588 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14589 	IPIF_DIFFNET,			/* normal and different subnet */
14590 	IPIF_SAMENET,			/* normal and same subnet */
14591 	IPIF_LOCALADDR			/* local loopback */
14592 } ipif_type_t;
14593 
14594 /*
14595  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14596  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14597  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14598  * the first one, unless IPMP is used in which case we round-robin among them;
14599  * see below for more.
14600  *
14601  * Returns NULL if there is no suitable source address for the ill.
14602  * This only occurs when there is no valid source address for the ill.
14603  */
14604 ipif_t *
14605 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14606     boolean_t allow_usesrc, boolean_t *notreadyp)
14607 {
14608 	ill_t	*usill = NULL;
14609 	ill_t	*ipmp_ill = NULL;
14610 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14611 	ipif_type_t type, best_type;
14612 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14613 	ip_stack_t *ipst = ill->ill_ipst;
14614 	boolean_t samenet;
14615 
14616 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14617 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14618 		    B_FALSE, ipst);
14619 		if (usill != NULL)
14620 			ill = usill;	/* Select source from usesrc ILL */
14621 		else
14622 			return (NULL);
14623 	}
14624 
14625 	/*
14626 	 * Test addresses should never be used for source address selection,
14627 	 * so if we were passed one, switch to the IPMP meta-interface.
14628 	 */
14629 	if (IS_UNDER_IPMP(ill)) {
14630 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14631 			ill = ipmp_ill;	/* Select source from IPMP ill */
14632 		else
14633 			return (NULL);
14634 	}
14635 
14636 	/*
14637 	 * If we're dealing with an unlabeled destination on a labeled system,
14638 	 * make sure that we ignore source addresses that are incompatible with
14639 	 * the destination's default label.  That destination's default label
14640 	 * must dominate the minimum label on the source address.
14641 	 */
14642 	dst_rhtp = NULL;
14643 	if (is_system_labeled()) {
14644 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14645 		if (dst_rhtp == NULL)
14646 			return (NULL);
14647 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14648 			TPC_RELE(dst_rhtp);
14649 			dst_rhtp = NULL;
14650 		}
14651 	}
14652 
14653 	/*
14654 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14655 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14656 	 * After selecting the right ipif, under ill_lock make sure ipif is
14657 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14658 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14659 	 * but not under a lock.
14660 	 */
14661 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14662 retry:
14663 	/*
14664 	 * For source address selection, we treat the ipif list as circular
14665 	 * and continue until we get back to where we started.  This allows
14666 	 * IPMP to vary source address selection (which improves inbound load
14667 	 * spreading) by caching its last ending point and starting from
14668 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14669 	 * ills since that can't happen on the IPMP ill.
14670 	 */
14671 	start_ipif = ill->ill_ipif;
14672 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14673 		start_ipif = ill->ill_src_ipif;
14674 
14675 	ipif = start_ipif;
14676 	best_ipif = NULL;
14677 	best_type = IPIF_NONE;
14678 	do {
14679 		if ((next_ipif = ipif->ipif_next) == NULL)
14680 			next_ipif = ill->ill_ipif;
14681 
14682 		if (IPIF_IS_CONDEMNED(ipif))
14683 			continue;
14684 		/* Always skip NOLOCAL and ANYCAST interfaces */
14685 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14686 			continue;
14687 		if (!(ipif->ipif_flags & IPIF_UP))
14688 			continue;
14689 
14690 		if (!ipif->ipif_addr_ready) {
14691 			if (notreadyp != NULL)
14692 				*notreadyp = B_TRUE;
14693 			continue;
14694 		}
14695 
14696 		if (zoneid != ALL_ZONES &&
14697 		    ipif->ipif_zoneid != zoneid &&
14698 		    ipif->ipif_zoneid != ALL_ZONES)
14699 			continue;
14700 
14701 		/*
14702 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14703 		 * are not valid as source addresses.
14704 		 */
14705 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14706 			continue;
14707 
14708 		/*
14709 		 * Check compatibility of local address for destination's
14710 		 * default label if we're on a labeled system.	Incompatible
14711 		 * addresses can't be used at all.
14712 		 */
14713 		if (dst_rhtp != NULL) {
14714 			boolean_t incompat;
14715 
14716 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14717 			    IPV4_VERSION, B_FALSE);
14718 			if (src_rhtp == NULL)
14719 				continue;
14720 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14721 			    src_rhtp->tpc_tp.tp_doi !=
14722 			    dst_rhtp->tpc_tp.tp_doi ||
14723 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14724 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14725 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14726 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14727 			TPC_RELE(src_rhtp);
14728 			if (incompat)
14729 				continue;
14730 		}
14731 
14732 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14733 
14734 		if (ipif->ipif_lcl_addr == dst) {
14735 			type = IPIF_LOCALADDR;
14736 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14737 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14738 			    IPIF_DIFFNET_DEPRECATED;
14739 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14740 			type = samenet ? IPIF_SAMENET_ALLZONES :
14741 			    IPIF_DIFFNET_ALLZONES;
14742 		} else {
14743 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14744 		}
14745 
14746 		if (type > best_type) {
14747 			best_type = type;
14748 			best_ipif = ipif;
14749 			if (best_type == IPIF_LOCALADDR)
14750 				break; /* can't get better */
14751 		}
14752 	} while ((ipif = next_ipif) != start_ipif);
14753 
14754 	if ((ipif = best_ipif) != NULL) {
14755 		mutex_enter(&ipif->ipif_ill->ill_lock);
14756 		if (IPIF_IS_CONDEMNED(ipif)) {
14757 			mutex_exit(&ipif->ipif_ill->ill_lock);
14758 			goto retry;
14759 		}
14760 		ipif_refhold_locked(ipif);
14761 
14762 		/*
14763 		 * For IPMP, update the source ipif rotor to the next ipif,
14764 		 * provided we can look it up.  (We must not use it if it's
14765 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
14766 		 * ipif_free() checked ill_src_ipif.)
14767 		 */
14768 		if (IS_IPMP(ill) && ipif != NULL) {
14769 			next_ipif = ipif->ipif_next;
14770 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
14771 				ill->ill_src_ipif = next_ipif;
14772 			else
14773 				ill->ill_src_ipif = NULL;
14774 		}
14775 		mutex_exit(&ipif->ipif_ill->ill_lock);
14776 	}
14777 
14778 	rw_exit(&ipst->ips_ill_g_lock);
14779 	if (usill != NULL)
14780 		ill_refrele(usill);
14781 	if (ipmp_ill != NULL)
14782 		ill_refrele(ipmp_ill);
14783 	if (dst_rhtp != NULL)
14784 		TPC_RELE(dst_rhtp);
14785 
14786 #ifdef DEBUG
14787 	if (ipif == NULL) {
14788 		char buf1[INET6_ADDRSTRLEN];
14789 
14790 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
14791 		    ill->ill_name,
14792 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
14793 	} else {
14794 		char buf1[INET6_ADDRSTRLEN];
14795 		char buf2[INET6_ADDRSTRLEN];
14796 
14797 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
14798 		    ipif->ipif_ill->ill_name,
14799 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
14800 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
14801 		    buf2, sizeof (buf2))));
14802 	}
14803 #endif /* DEBUG */
14804 	return (ipif);
14805 }
14806 
14807 /*
14808  * Pick a source address based on the destination ill and an optional setsrc
14809  * address.
14810  * The result is stored in srcp. If generation is set, then put the source
14811  * generation number there before we look for the source address (to avoid
14812  * missing changes in the set of source addresses.
14813  * If flagsp is set, then us it to pass back ipif_flags.
14814  *
14815  * If the caller wants to cache the returned source address and detect when
14816  * that might be stale, the caller should pass in a generation argument,
14817  * which the caller can later compare against ips_src_generation
14818  *
14819  * The precedence order for selecting an IPv4 source address is:
14820  *  - RTF_SETSRC on the offlink ire always wins.
14821  *  - If usrsrc is set, swap the ill to be the usesrc one.
14822  *  - If IPMP is used on the ill, select a random address from the most
14823  *    preferred ones below:
14824  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
14825  * 2. Not deprecated, not ALL_ZONES
14826  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
14827  * 4. Not deprecated, ALL_ZONES
14828  * 5. If onlink destination, same subnet and deprecated
14829  * 6. Deprecated.
14830  *
14831  * We have lower preference for ALL_ZONES IP addresses,
14832  * as they pose problems with unlabeled destinations.
14833  *
14834  * Note that when multiple IP addresses match e.g., #1 we pick
14835  * the first one if IPMP is not in use. With IPMP we randomize.
14836  */
14837 int
14838 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
14839     ipaddr_t multicast_ifaddr,
14840     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
14841     uint32_t *generation, uint64_t *flagsp)
14842 {
14843 	ipif_t *ipif;
14844 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
14845 
14846 	if (flagsp != NULL)
14847 		*flagsp = 0;
14848 
14849 	/*
14850 	 * Need to grab the generation number before we check to
14851 	 * avoid a race with a change to the set of local addresses.
14852 	 * No lock needed since the thread which updates the set of local
14853 	 * addresses use ipif/ill locks and exit those (hence a store memory
14854 	 * barrier) before doing the atomic increase of ips_src_generation.
14855 	 */
14856 	if (generation != NULL) {
14857 		*generation = ipst->ips_src_generation;
14858 	}
14859 
14860 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
14861 		*srcp = multicast_ifaddr;
14862 		return (0);
14863 	}
14864 
14865 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
14866 	if (setsrc != INADDR_ANY) {
14867 		*srcp = setsrc;
14868 		return (0);
14869 	}
14870 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
14871 	if (ipif == NULL) {
14872 		if (notready)
14873 			return (ENETDOWN);
14874 		else
14875 			return (EADDRNOTAVAIL);
14876 	}
14877 	*srcp = ipif->ipif_lcl_addr;
14878 	if (flagsp != NULL)
14879 		*flagsp = ipif->ipif_flags;
14880 	ipif_refrele(ipif);
14881 	return (0);
14882 }
14883 
14884 /* ARGSUSED */
14885 int
14886 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14887 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14888 {
14889 	/*
14890 	 * ill_phyint_reinit merged the v4 and v6 into a single
14891 	 * ipsq.  We might not have been able to complete the
14892 	 * operation in ipif_set_values, if we could not become
14893 	 * exclusive.  If so restart it here.
14894 	 */
14895 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
14896 }
14897 
14898 /*
14899  * Can operate on either a module or a driver queue.
14900  * Returns an error if not a module queue.
14901  */
14902 /* ARGSUSED */
14903 int
14904 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14905     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14906 {
14907 	queue_t		*q1 = q;
14908 	char 		*cp;
14909 	char		interf_name[LIFNAMSIZ];
14910 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
14911 
14912 	if (q->q_next == NULL) {
14913 		ip1dbg((
14914 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
14915 		return (EINVAL);
14916 	}
14917 
14918 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
14919 		return (EALREADY);
14920 
14921 	do {
14922 		q1 = q1->q_next;
14923 	} while (q1->q_next);
14924 	cp = q1->q_qinfo->qi_minfo->mi_idname;
14925 	(void) sprintf(interf_name, "%s%d", cp, ppa);
14926 
14927 	/*
14928 	 * Here we are not going to delay the ioack until after
14929 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
14930 	 * original ioctl message before sending the requests.
14931 	 */
14932 	return (ipif_set_values(q, mp, interf_name, &ppa));
14933 }
14934 
14935 /* ARGSUSED */
14936 int
14937 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14938     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14939 {
14940 	return (ENXIO);
14941 }
14942 
14943 /*
14944  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
14945  * `irep'.  Returns a pointer to the next free `irep' entry
14946  * A mirror exists in ipif_delete_bcast_ires().
14947  *
14948  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
14949  * done in ire_add.
14950  */
14951 static ire_t **
14952 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
14953 {
14954 	ipaddr_t addr;
14955 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
14956 	ipaddr_t subnetmask = ipif->ipif_net_mask;
14957 	ill_t *ill = ipif->ipif_ill;
14958 	zoneid_t zoneid = ipif->ipif_zoneid;
14959 
14960 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
14961 
14962 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
14963 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
14964 
14965 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
14966 	    (ipif->ipif_flags & IPIF_NOLOCAL))
14967 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
14968 
14969 	irep = ire_create_bcast(ill, 0, zoneid, irep);
14970 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
14971 
14972 	/*
14973 	 * For backward compatibility, we create net broadcast IREs based on
14974 	 * the old "IP address class system", since some old machines only
14975 	 * respond to these class derived net broadcast.  However, we must not
14976 	 * create these net broadcast IREs if the subnetmask is shorter than
14977 	 * the IP address class based derived netmask.  Otherwise, we may
14978 	 * create a net broadcast address which is the same as an IP address
14979 	 * on the subnet -- and then TCP will refuse to talk to that address.
14980 	 */
14981 	if (netmask < subnetmask) {
14982 		addr = netmask & ipif->ipif_subnet;
14983 		irep = ire_create_bcast(ill, addr, zoneid, irep);
14984 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
14985 	}
14986 
14987 	/*
14988 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
14989 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
14990 	 * created.  Creating these broadcast IREs will only create confusion
14991 	 * as `addr' will be the same as the IP address.
14992 	 */
14993 	if (subnetmask != 0xFFFFFFFF) {
14994 		addr = ipif->ipif_subnet;
14995 		irep = ire_create_bcast(ill, addr, zoneid, irep);
14996 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
14997 	}
14998 
14999 	return (irep);
15000 }
15001 
15002 /*
15003  * Mirror of ipif_create_bcast_ires()
15004  */
15005 static void
15006 ipif_delete_bcast_ires(ipif_t *ipif)
15007 {
15008 	ipaddr_t	addr;
15009 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15010 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15011 	ill_t		*ill = ipif->ipif_ill;
15012 	zoneid_t	zoneid = ipif->ipif_zoneid;
15013 	ire_t		*ire;
15014 
15015 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15016 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15017 
15018 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15019 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15020 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15021 
15022 	ire = ire_lookup_bcast(ill, 0, zoneid);
15023 	ASSERT(ire != NULL);
15024 	ire_delete(ire); ire_refrele(ire);
15025 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15026 	ASSERT(ire != NULL);
15027 	ire_delete(ire); ire_refrele(ire);
15028 
15029 	/*
15030 	 * For backward compatibility, we create net broadcast IREs based on
15031 	 * the old "IP address class system", since some old machines only
15032 	 * respond to these class derived net broadcast.  However, we must not
15033 	 * create these net broadcast IREs if the subnetmask is shorter than
15034 	 * the IP address class based derived netmask.  Otherwise, we may
15035 	 * create a net broadcast address which is the same as an IP address
15036 	 * on the subnet -- and then TCP will refuse to talk to that address.
15037 	 */
15038 	if (netmask < subnetmask) {
15039 		addr = netmask & ipif->ipif_subnet;
15040 		ire = ire_lookup_bcast(ill, addr, zoneid);
15041 		ASSERT(ire != NULL);
15042 		ire_delete(ire); ire_refrele(ire);
15043 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15044 		ASSERT(ire != NULL);
15045 		ire_delete(ire); ire_refrele(ire);
15046 	}
15047 
15048 	/*
15049 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15050 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15051 	 * created.  Creating these broadcast IREs will only create confusion
15052 	 * as `addr' will be the same as the IP address.
15053 	 */
15054 	if (subnetmask != 0xFFFFFFFF) {
15055 		addr = ipif->ipif_subnet;
15056 		ire = ire_lookup_bcast(ill, addr, zoneid);
15057 		ASSERT(ire != NULL);
15058 		ire_delete(ire); ire_refrele(ire);
15059 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15060 		ASSERT(ire != NULL);
15061 		ire_delete(ire); ire_refrele(ire);
15062 	}
15063 }
15064 
15065 /*
15066  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15067  * from lifr_flags and the name from lifr_name.
15068  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15069  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15070  * Returns EINPROGRESS when mp has been consumed by queueing it on
15071  * ipx_pending_mp and the ioctl will complete in ip_rput.
15072  *
15073  * Can operate on either a module or a driver queue.
15074  * Returns an error if not a module queue.
15075  */
15076 /* ARGSUSED */
15077 int
15078 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15079     ip_ioctl_cmd_t *ipip, void *if_req)
15080 {
15081 	ill_t	*ill = q->q_ptr;
15082 	phyint_t *phyi;
15083 	ip_stack_t *ipst;
15084 	struct lifreq *lifr = if_req;
15085 	uint64_t new_flags;
15086 
15087 	ASSERT(ipif != NULL);
15088 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15089 
15090 	if (q->q_next == NULL) {
15091 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15092 		return (EINVAL);
15093 	}
15094 
15095 	/*
15096 	 * If we are not writer on 'q' then this interface exists already
15097 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15098 	 * so return EALREADY.
15099 	 */
15100 	if (ill != ipif->ipif_ill)
15101 		return (EALREADY);
15102 
15103 	if (ill->ill_name[0] != '\0')
15104 		return (EALREADY);
15105 
15106 	/*
15107 	 * If there's another ill already with the requested name, ensure
15108 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15109 	 * fuse together two unrelated ills, which will cause chaos.
15110 	 */
15111 	ipst = ill->ill_ipst;
15112 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15113 	    lifr->lifr_name, NULL);
15114 	if (phyi != NULL) {
15115 		ill_t *ill_mate = phyi->phyint_illv4;
15116 
15117 		if (ill_mate == NULL)
15118 			ill_mate = phyi->phyint_illv6;
15119 		ASSERT(ill_mate != NULL);
15120 
15121 		if (ill_mate->ill_media->ip_m_mac_type !=
15122 		    ill->ill_media->ip_m_mac_type) {
15123 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15124 			    "use the same ill name on differing media\n"));
15125 			return (EINVAL);
15126 		}
15127 	}
15128 
15129 	/*
15130 	 * We start off as IFF_IPV4 in ipif_allocate and become
15131 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15132 	 * The only flags that we read from user space are IFF_IPV4,
15133 	 * IFF_IPV6, and IFF_BROADCAST.
15134 	 *
15135 	 * This ill has not been inserted into the global list.
15136 	 * So we are still single threaded and don't need any lock
15137 	 *
15138 	 * Saniy check the flags.
15139 	 */
15140 
15141 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15142 	    ((lifr->lifr_flags & IFF_IPV6) ||
15143 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15144 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15145 		    "or IPv6 i.e., no broadcast \n"));
15146 		return (EINVAL);
15147 	}
15148 
15149 	new_flags =
15150 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15151 
15152 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15153 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15154 		    "IFF_IPV4 or IFF_IPV6\n"));
15155 		return (EINVAL);
15156 	}
15157 
15158 	/*
15159 	 * We always start off as IPv4, so only need to check for IPv6.
15160 	 */
15161 	if ((new_flags & IFF_IPV6) != 0) {
15162 		ill->ill_flags |= ILLF_IPV6;
15163 		ill->ill_flags &= ~ILLF_IPV4;
15164 	}
15165 
15166 	if ((new_flags & IFF_BROADCAST) != 0)
15167 		ipif->ipif_flags |= IPIF_BROADCAST;
15168 	else
15169 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15170 
15171 	/* We started off as V4. */
15172 	if (ill->ill_flags & ILLF_IPV6) {
15173 		ill->ill_phyint->phyint_illv6 = ill;
15174 		ill->ill_phyint->phyint_illv4 = NULL;
15175 	}
15176 
15177 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15178 }
15179 
15180 /* ARGSUSED */
15181 int
15182 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15183     ip_ioctl_cmd_t *ipip, void *if_req)
15184 {
15185 	/*
15186 	 * ill_phyint_reinit merged the v4 and v6 into a single
15187 	 * ipsq.  We might not have been able to complete the
15188 	 * slifname in ipif_set_values, if we could not become
15189 	 * exclusive.  If so restart it here
15190 	 */
15191 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15192 }
15193 
15194 /*
15195  * Return a pointer to the ipif which matches the index, IP version type and
15196  * zoneid.
15197  */
15198 ipif_t *
15199 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15200     ip_stack_t *ipst)
15201 {
15202 	ill_t	*ill;
15203 	ipif_t	*ipif = NULL;
15204 
15205 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15206 	if (ill != NULL) {
15207 		mutex_enter(&ill->ill_lock);
15208 		for (ipif = ill->ill_ipif; ipif != NULL;
15209 		    ipif = ipif->ipif_next) {
15210 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15211 			    zoneid == ipif->ipif_zoneid ||
15212 			    ipif->ipif_zoneid == ALL_ZONES)) {
15213 				ipif_refhold_locked(ipif);
15214 				break;
15215 			}
15216 		}
15217 		mutex_exit(&ill->ill_lock);
15218 		ill_refrele(ill);
15219 	}
15220 	return (ipif);
15221 }
15222 
15223 /*
15224  * Change an existing physical interface's index. If the new index
15225  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15226  * Finally, we update other systems which may have a dependence on the
15227  * index value.
15228  */
15229 /* ARGSUSED */
15230 int
15231 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15232     ip_ioctl_cmd_t *ipip, void *ifreq)
15233 {
15234 	ill_t		*ill;
15235 	phyint_t	*phyi;
15236 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15237 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15238 	uint_t	old_index, index;
15239 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15240 	avl_index_t	where;
15241 
15242 	if (ipip->ipi_cmd_type == IF_CMD)
15243 		index = ifr->ifr_index;
15244 	else
15245 		index = lifr->lifr_index;
15246 
15247 	/*
15248 	 * Only allow on physical interface. Also, index zero is illegal.
15249 	 */
15250 	ill = ipif->ipif_ill;
15251 	phyi = ill->ill_phyint;
15252 	if (ipif->ipif_id != 0 || index == 0) {
15253 		return (EINVAL);
15254 	}
15255 
15256 	/* If the index is not changing, no work to do */
15257 	if (phyi->phyint_ifindex == index)
15258 		return (0);
15259 
15260 	/*
15261 	 * Use phyint_exists() to determine if the new interface index
15262 	 * is already in use. If the index is unused then we need to
15263 	 * change the phyint's position in the phyint_list_avl_by_index
15264 	 * tree. If we do not do this, subsequent lookups (using the new
15265 	 * index value) will not find the phyint.
15266 	 */
15267 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15268 	if (phyint_exists(index, ipst)) {
15269 		rw_exit(&ipst->ips_ill_g_lock);
15270 		return (EEXIST);
15271 	}
15272 
15273 	/*
15274 	 * The new index is unused. Set it in the phyint. However we must not
15275 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15276 	 * changes. The event must be bound to old ifindex value.
15277 	 */
15278 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15279 	    &index, sizeof (index));
15280 
15281 	old_index = phyi->phyint_ifindex;
15282 	phyi->phyint_ifindex = index;
15283 
15284 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15285 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15286 	    &index, &where);
15287 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15288 	    phyi, where);
15289 	rw_exit(&ipst->ips_ill_g_lock);
15290 
15291 	/* Update SCTP's ILL list */
15292 	sctp_ill_reindex(ill, old_index);
15293 
15294 	/* Send the routing sockets message */
15295 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15296 	if (ILL_OTHER(ill))
15297 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15298 
15299 	/* Perhaps ilgs should use this ill */
15300 	update_conn_ill(NULL, ill->ill_ipst);
15301 	return (0);
15302 }
15303 
15304 /* ARGSUSED */
15305 int
15306 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15307     ip_ioctl_cmd_t *ipip, void *ifreq)
15308 {
15309 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15310 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15311 
15312 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15313 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15314 	/* Get the interface index */
15315 	if (ipip->ipi_cmd_type == IF_CMD) {
15316 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15317 	} else {
15318 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15319 	}
15320 	return (0);
15321 }
15322 
15323 /* ARGSUSED */
15324 int
15325 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15326     ip_ioctl_cmd_t *ipip, void *ifreq)
15327 {
15328 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15329 
15330 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15331 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15332 	/* Get the interface zone */
15333 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15334 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15335 	return (0);
15336 }
15337 
15338 /*
15339  * Set the zoneid of an interface.
15340  */
15341 /* ARGSUSED */
15342 int
15343 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15344     ip_ioctl_cmd_t *ipip, void *ifreq)
15345 {
15346 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15347 	int err = 0;
15348 	boolean_t need_up = B_FALSE;
15349 	zone_t *zptr;
15350 	zone_status_t status;
15351 	zoneid_t zoneid;
15352 
15353 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15354 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15355 		if (!is_system_labeled())
15356 			return (ENOTSUP);
15357 		zoneid = GLOBAL_ZONEID;
15358 	}
15359 
15360 	/* cannot assign instance zero to a non-global zone */
15361 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15362 		return (ENOTSUP);
15363 
15364 	/*
15365 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15366 	 * the event of a race with the zone shutdown processing, since IP
15367 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15368 	 * interface will be cleaned up even if the zone is shut down
15369 	 * immediately after the status check. If the interface can't be brought
15370 	 * down right away, and the zone is shut down before the restart
15371 	 * function is called, we resolve the possible races by rechecking the
15372 	 * zone status in the restart function.
15373 	 */
15374 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15375 		return (EINVAL);
15376 	status = zone_status_get(zptr);
15377 	zone_rele(zptr);
15378 
15379 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15380 		return (EINVAL);
15381 
15382 	if (ipif->ipif_flags & IPIF_UP) {
15383 		/*
15384 		 * If the interface is already marked up,
15385 		 * we call ipif_down which will take care
15386 		 * of ditching any IREs that have been set
15387 		 * up based on the old interface address.
15388 		 */
15389 		err = ipif_logical_down(ipif, q, mp);
15390 		if (err == EINPROGRESS)
15391 			return (err);
15392 		(void) ipif_down_tail(ipif);
15393 		need_up = B_TRUE;
15394 	}
15395 
15396 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15397 	return (err);
15398 }
15399 
15400 static int
15401 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15402     queue_t *q, mblk_t *mp, boolean_t need_up)
15403 {
15404 	int	err = 0;
15405 	ip_stack_t	*ipst;
15406 
15407 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15408 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15409 
15410 	if (CONN_Q(q))
15411 		ipst = CONNQ_TO_IPST(q);
15412 	else
15413 		ipst = ILLQ_TO_IPST(q);
15414 
15415 	/*
15416 	 * For exclusive stacks we don't allow a different zoneid than
15417 	 * global.
15418 	 */
15419 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15420 	    zoneid != GLOBAL_ZONEID)
15421 		return (EINVAL);
15422 
15423 	/* Set the new zone id. */
15424 	ipif->ipif_zoneid = zoneid;
15425 
15426 	/* Update sctp list */
15427 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15428 
15429 	/* The default multicast interface might have changed */
15430 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15431 
15432 	if (need_up) {
15433 		/*
15434 		 * Now bring the interface back up.  If this
15435 		 * is the only IPIF for the ILL, ipif_up
15436 		 * will have to re-bind to the device, so
15437 		 * we may get back EINPROGRESS, in which
15438 		 * case, this IOCTL will get completed in
15439 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15440 		 */
15441 		err = ipif_up(ipif, q, mp);
15442 	}
15443 	return (err);
15444 }
15445 
15446 /* ARGSUSED */
15447 int
15448 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15449     ip_ioctl_cmd_t *ipip, void *if_req)
15450 {
15451 	struct lifreq *lifr = (struct lifreq *)if_req;
15452 	zoneid_t zoneid;
15453 	zone_t *zptr;
15454 	zone_status_t status;
15455 
15456 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15457 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15458 		zoneid = GLOBAL_ZONEID;
15459 
15460 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15461 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15462 
15463 	/*
15464 	 * We recheck the zone status to resolve the following race condition:
15465 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15466 	 * 2) hme0:1 is up and can't be brought down right away;
15467 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15468 	 * 3) zone "myzone" is halted; the zone status switches to
15469 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15470 	 * the interfaces to remove - hme0:1 is not returned because it's not
15471 	 * yet in "myzone", so it won't be removed;
15472 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15473 	 * status check here, we would have hme0:1 in "myzone" after it's been
15474 	 * destroyed.
15475 	 * Note that if the status check fails, we need to bring the interface
15476 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15477 	 * ipif_up_done[_v6]().
15478 	 */
15479 	status = ZONE_IS_UNINITIALIZED;
15480 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15481 		status = zone_status_get(zptr);
15482 		zone_rele(zptr);
15483 	}
15484 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15485 		if (ipif->ipif_isv6) {
15486 			(void) ipif_up_done_v6(ipif);
15487 		} else {
15488 			(void) ipif_up_done(ipif);
15489 		}
15490 		return (EINVAL);
15491 	}
15492 
15493 	(void) ipif_down_tail(ipif);
15494 
15495 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15496 	    B_TRUE));
15497 }
15498 
15499 /*
15500  * Return the number of addresses on `ill' with one or more of the values
15501  * in `set' set and all of the values in `clear' clear.
15502  */
15503 static uint_t
15504 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15505 {
15506 	ipif_t	*ipif;
15507 	uint_t	cnt = 0;
15508 
15509 	ASSERT(IAM_WRITER_ILL(ill));
15510 
15511 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15512 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15513 			cnt++;
15514 
15515 	return (cnt);
15516 }
15517 
15518 /*
15519  * Return the number of migratable addresses on `ill' that are under
15520  * application control.
15521  */
15522 uint_t
15523 ill_appaddr_cnt(const ill_t *ill)
15524 {
15525 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15526 	    IPIF_NOFAILOVER));
15527 }
15528 
15529 /*
15530  * Return the number of point-to-point addresses on `ill'.
15531  */
15532 uint_t
15533 ill_ptpaddr_cnt(const ill_t *ill)
15534 {
15535 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15536 }
15537 
15538 /* ARGSUSED */
15539 int
15540 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15541 	ip_ioctl_cmd_t *ipip, void *ifreq)
15542 {
15543 	struct lifreq	*lifr = ifreq;
15544 
15545 	ASSERT(q->q_next == NULL);
15546 	ASSERT(CONN_Q(q));
15547 
15548 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15549 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15550 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15551 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15552 
15553 	return (0);
15554 }
15555 
15556 /* Find the previous ILL in this usesrc group */
15557 static ill_t *
15558 ill_prev_usesrc(ill_t *uill)
15559 {
15560 	ill_t *ill;
15561 
15562 	for (ill = uill->ill_usesrc_grp_next;
15563 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15564 	    ill = ill->ill_usesrc_grp_next)
15565 		/* do nothing */;
15566 	return (ill);
15567 }
15568 
15569 /*
15570  * Release all members of the usesrc group. This routine is called
15571  * from ill_delete when the interface being unplumbed is the
15572  * group head.
15573  *
15574  * This silently clears the usesrc that ifconfig setup.
15575  * An alternative would be to keep that ifindex, and drop packets on the floor
15576  * since no source address can be selected.
15577  * Even if we keep the current semantics, don't need a lock and a linked list.
15578  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15579  * the one that is being removed. Issue is how we return the usesrc users
15580  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15581  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15582  * ill walk, but the walker would need to insert in the ioctl response.
15583  */
15584 static void
15585 ill_disband_usesrc_group(ill_t *uill)
15586 {
15587 	ill_t *next_ill, *tmp_ill;
15588 	ip_stack_t	*ipst = uill->ill_ipst;
15589 
15590 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15591 	next_ill = uill->ill_usesrc_grp_next;
15592 
15593 	do {
15594 		ASSERT(next_ill != NULL);
15595 		tmp_ill = next_ill->ill_usesrc_grp_next;
15596 		ASSERT(tmp_ill != NULL);
15597 		next_ill->ill_usesrc_grp_next = NULL;
15598 		next_ill->ill_usesrc_ifindex = 0;
15599 		next_ill = tmp_ill;
15600 	} while (next_ill->ill_usesrc_ifindex != 0);
15601 	uill->ill_usesrc_grp_next = NULL;
15602 }
15603 
15604 /*
15605  * Remove the client usesrc ILL from the list and relink to a new list
15606  */
15607 int
15608 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15609 {
15610 	ill_t *ill, *tmp_ill;
15611 	ip_stack_t	*ipst = ucill->ill_ipst;
15612 
15613 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15614 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15615 
15616 	/*
15617 	 * Check if the usesrc client ILL passed in is not already
15618 	 * in use as a usesrc ILL i.e one whose source address is
15619 	 * in use OR a usesrc ILL is not already in use as a usesrc
15620 	 * client ILL
15621 	 */
15622 	if ((ucill->ill_usesrc_ifindex == 0) ||
15623 	    (uill->ill_usesrc_ifindex != 0)) {
15624 		return (-1);
15625 	}
15626 
15627 	ill = ill_prev_usesrc(ucill);
15628 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15629 
15630 	/* Remove from the current list */
15631 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15632 		/* Only two elements in the list */
15633 		ASSERT(ill->ill_usesrc_ifindex == 0);
15634 		ill->ill_usesrc_grp_next = NULL;
15635 	} else {
15636 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15637 	}
15638 
15639 	if (ifindex == 0) {
15640 		ucill->ill_usesrc_ifindex = 0;
15641 		ucill->ill_usesrc_grp_next = NULL;
15642 		return (0);
15643 	}
15644 
15645 	ucill->ill_usesrc_ifindex = ifindex;
15646 	tmp_ill = uill->ill_usesrc_grp_next;
15647 	uill->ill_usesrc_grp_next = ucill;
15648 	ucill->ill_usesrc_grp_next =
15649 	    (tmp_ill != NULL) ? tmp_ill : uill;
15650 	return (0);
15651 }
15652 
15653 /*
15654  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15655  * ip.c for locking details.
15656  */
15657 /* ARGSUSED */
15658 int
15659 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15660     ip_ioctl_cmd_t *ipip, void *ifreq)
15661 {
15662 	struct lifreq *lifr = (struct lifreq *)ifreq;
15663 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15664 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15665 	int err = 0, ret;
15666 	uint_t ifindex;
15667 	ipsq_t *ipsq = NULL;
15668 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15669 
15670 	ASSERT(IAM_WRITER_IPIF(ipif));
15671 	ASSERT(q->q_next == NULL);
15672 	ASSERT(CONN_Q(q));
15673 
15674 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15675 
15676 	ifindex = lifr->lifr_index;
15677 	if (ifindex == 0) {
15678 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15679 			/* non usesrc group interface, nothing to reset */
15680 			return (0);
15681 		}
15682 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15683 		/* valid reset request */
15684 		reset_flg = B_TRUE;
15685 	}
15686 
15687 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15688 	if (usesrc_ill == NULL) {
15689 		return (ENXIO);
15690 	}
15691 
15692 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15693 	    NEW_OP, B_TRUE);
15694 	if (ipsq == NULL) {
15695 		err = EINPROGRESS;
15696 		/* Operation enqueued on the ipsq of the usesrc ILL */
15697 		goto done;
15698 	}
15699 
15700 	/* USESRC isn't currently supported with IPMP */
15701 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15702 		err = ENOTSUP;
15703 		goto done;
15704 	}
15705 
15706 	/*
15707 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15708 	 * used by IPMP underlying interfaces, but someone might think it's
15709 	 * more general and try to use it independently with VNI.)
15710 	 */
15711 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15712 		err = ENOTSUP;
15713 		goto done;
15714 	}
15715 
15716 	/*
15717 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15718 	 * already a client then return EINVAL
15719 	 */
15720 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15721 		err = EINVAL;
15722 		goto done;
15723 	}
15724 
15725 	/*
15726 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15727 	 * be then this is a duplicate operation.
15728 	 */
15729 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15730 		err = 0;
15731 		goto done;
15732 	}
15733 
15734 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15735 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15736 	    usesrc_ill->ill_isv6));
15737 
15738 	/*
15739 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15740 	 * and the ill_usesrc_ifindex fields
15741 	 */
15742 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15743 
15744 	if (reset_flg) {
15745 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15746 		if (ret != 0) {
15747 			err = EINVAL;
15748 		}
15749 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15750 		goto done;
15751 	}
15752 
15753 	/*
15754 	 * Four possibilities to consider:
15755 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15756 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15757 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15758 	 * 4. Both are part of their respective usesrc groups
15759 	 */
15760 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15761 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15762 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
15763 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15764 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15765 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
15766 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
15767 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15768 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15769 		/* Insert at head of list */
15770 		usesrc_cli_ill->ill_usesrc_grp_next =
15771 		    usesrc_ill->ill_usesrc_grp_next;
15772 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15773 	} else {
15774 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
15775 		    ifindex);
15776 		if (ret != 0)
15777 			err = EINVAL;
15778 	}
15779 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
15780 
15781 done:
15782 	if (ipsq != NULL)
15783 		ipsq_exit(ipsq);
15784 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
15785 	ill_refrele(usesrc_ill);
15786 
15787 	/* Let conn_ixa caching know that source address selection changed */
15788 	ip_update_source_selection(ipst);
15789 
15790 	return (err);
15791 }
15792 
15793 /*
15794  * comparison function used by avl.
15795  */
15796 static int
15797 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
15798 {
15799 
15800 	uint_t index;
15801 
15802 	ASSERT(phyip != NULL && index_ptr != NULL);
15803 
15804 	index = *((uint_t *)index_ptr);
15805 	/*
15806 	 * let the phyint with the lowest index be on top.
15807 	 */
15808 	if (((phyint_t *)phyip)->phyint_ifindex < index)
15809 		return (1);
15810 	if (((phyint_t *)phyip)->phyint_ifindex > index)
15811 		return (-1);
15812 	return (0);
15813 }
15814 
15815 /*
15816  * comparison function used by avl.
15817  */
15818 static int
15819 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
15820 {
15821 	ill_t *ill;
15822 	int res = 0;
15823 
15824 	ASSERT(phyip != NULL && name_ptr != NULL);
15825 
15826 	if (((phyint_t *)phyip)->phyint_illv4)
15827 		ill = ((phyint_t *)phyip)->phyint_illv4;
15828 	else
15829 		ill = ((phyint_t *)phyip)->phyint_illv6;
15830 	ASSERT(ill != NULL);
15831 
15832 	res = strcmp(ill->ill_name, (char *)name_ptr);
15833 	if (res > 0)
15834 		return (1);
15835 	else if (res < 0)
15836 		return (-1);
15837 	return (0);
15838 }
15839 
15840 /*
15841  * This function is called on the unplumb path via ill_glist_delete() when
15842  * there are no ills left on the phyint and thus the phyint can be freed.
15843  */
15844 static void
15845 phyint_free(phyint_t *phyi)
15846 {
15847 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
15848 
15849 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
15850 
15851 	/*
15852 	 * If this phyint was an IPMP meta-interface, blow away the group.
15853 	 * This is safe to do because all of the illgrps have already been
15854 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
15855 	 * If we're cleaning up as a result of failed initialization,
15856 	 * phyint_grp may be NULL.
15857 	 */
15858 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
15859 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
15860 		ipmp_grp_destroy(phyi->phyint_grp);
15861 		phyi->phyint_grp = NULL;
15862 		rw_exit(&ipst->ips_ipmp_lock);
15863 	}
15864 
15865 	/*
15866 	 * If this interface was under IPMP, take it out of the group.
15867 	 */
15868 	if (phyi->phyint_grp != NULL)
15869 		ipmp_phyint_leave_grp(phyi);
15870 
15871 	/*
15872 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
15873 	 * will be freed in ipsq_exit().
15874 	 */
15875 	phyi->phyint_ipsq->ipsq_phyint = NULL;
15876 	phyi->phyint_name[0] = '\0';
15877 
15878 	mi_free(phyi);
15879 }
15880 
15881 /*
15882  * Attach the ill to the phyint structure which can be shared by both
15883  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
15884  * function is called from ipif_set_values and ill_lookup_on_name (for
15885  * loopback) where we know the name of the ill. We lookup the ill and if
15886  * there is one present already with the name use that phyint. Otherwise
15887  * reuse the one allocated by ill_init.
15888  */
15889 static void
15890 ill_phyint_reinit(ill_t *ill)
15891 {
15892 	boolean_t isv6 = ill->ill_isv6;
15893 	phyint_t *phyi_old;
15894 	phyint_t *phyi;
15895 	avl_index_t where = 0;
15896 	ill_t	*ill_other = NULL;
15897 	ip_stack_t	*ipst = ill->ill_ipst;
15898 
15899 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
15900 
15901 	phyi_old = ill->ill_phyint;
15902 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
15903 	    phyi_old->phyint_illv6 == NULL));
15904 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
15905 	    phyi_old->phyint_illv4 == NULL));
15906 	ASSERT(phyi_old->phyint_ifindex == 0);
15907 
15908 	/*
15909 	 * Now that our ill has a name, set it in the phyint.
15910 	 */
15911 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
15912 
15913 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15914 	    ill->ill_name, &where);
15915 
15916 	/*
15917 	 * 1. We grabbed the ill_g_lock before inserting this ill into
15918 	 *    the global list of ills. So no other thread could have located
15919 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
15920 	 * 2. Now locate the other protocol instance of this ill.
15921 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
15922 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
15923 	 *    of neither ill can change.
15924 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
15925 	 *    other ill.
15926 	 * 5. Release all locks.
15927 	 */
15928 
15929 	/*
15930 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
15931 	 * we are initializing IPv4.
15932 	 */
15933 	if (phyi != NULL) {
15934 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
15935 		ASSERT(ill_other->ill_phyint != NULL);
15936 		ASSERT((isv6 && !ill_other->ill_isv6) ||
15937 		    (!isv6 && ill_other->ill_isv6));
15938 		GRAB_ILL_LOCKS(ill, ill_other);
15939 		/*
15940 		 * We are potentially throwing away phyint_flags which
15941 		 * could be different from the one that we obtain from
15942 		 * ill_other->ill_phyint. But it is okay as we are assuming
15943 		 * that the state maintained within IP is correct.
15944 		 */
15945 		mutex_enter(&phyi->phyint_lock);
15946 		if (isv6) {
15947 			ASSERT(phyi->phyint_illv6 == NULL);
15948 			phyi->phyint_illv6 = ill;
15949 		} else {
15950 			ASSERT(phyi->phyint_illv4 == NULL);
15951 			phyi->phyint_illv4 = ill;
15952 		}
15953 
15954 		/*
15955 		 * Delete the old phyint and make its ipsq eligible
15956 		 * to be freed in ipsq_exit().
15957 		 */
15958 		phyi_old->phyint_illv4 = NULL;
15959 		phyi_old->phyint_illv6 = NULL;
15960 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
15961 		phyi_old->phyint_name[0] = '\0';
15962 		mi_free(phyi_old);
15963 	} else {
15964 		mutex_enter(&ill->ill_lock);
15965 		/*
15966 		 * We don't need to acquire any lock, since
15967 		 * the ill is not yet visible globally  and we
15968 		 * have not yet released the ill_g_lock.
15969 		 */
15970 		phyi = phyi_old;
15971 		mutex_enter(&phyi->phyint_lock);
15972 		/* XXX We need a recovery strategy here. */
15973 		if (!phyint_assign_ifindex(phyi, ipst))
15974 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
15975 
15976 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15977 		    (void *)phyi, where);
15978 
15979 		(void) avl_find(&ipst->ips_phyint_g_list->
15980 		    phyint_list_avl_by_index,
15981 		    &phyi->phyint_ifindex, &where);
15982 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15983 		    (void *)phyi, where);
15984 	}
15985 
15986 	/*
15987 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
15988 	 * pending mp is not affected because that is per ill basis.
15989 	 */
15990 	ill->ill_phyint = phyi;
15991 
15992 	/*
15993 	 * Now that the phyint's ifindex has been assigned, complete the
15994 	 * remaining
15995 	 */
15996 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
15997 	if (ill->ill_isv6) {
15998 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
15999 		    ill->ill_phyint->phyint_ifindex;
16000 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16001 	} else {
16002 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16003 	}
16004 
16005 	/*
16006 	 * Generate an event within the hooks framework to indicate that
16007 	 * a new interface has just been added to IP.  For this event to
16008 	 * be generated, the network interface must, at least, have an
16009 	 * ifindex assigned to it.  (We don't generate the event for
16010 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16011 	 *
16012 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16013 	 * that the ordering of delivered events to listeners matches the
16014 	 * order of them in the kernel.
16015 	 */
16016 	if (!IS_LOOPBACK(ill)) {
16017 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16018 		    ill->ill_name_length);
16019 	}
16020 	RELEASE_ILL_LOCKS(ill, ill_other);
16021 	mutex_exit(&phyi->phyint_lock);
16022 }
16023 
16024 /*
16025  * Notify any downstream modules of the name of this interface.
16026  * An M_IOCTL is used even though we don't expect a successful reply.
16027  * Any reply message from the driver (presumably an M_IOCNAK) will
16028  * eventually get discarded somewhere upstream.  The message format is
16029  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16030  * to IP.
16031  */
16032 static void
16033 ip_ifname_notify(ill_t *ill, queue_t *q)
16034 {
16035 	mblk_t *mp1, *mp2;
16036 	struct iocblk *iocp;
16037 	struct lifreq *lifr;
16038 
16039 	mp1 = mkiocb(SIOCSLIFNAME);
16040 	if (mp1 == NULL)
16041 		return;
16042 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16043 	if (mp2 == NULL) {
16044 		freeb(mp1);
16045 		return;
16046 	}
16047 
16048 	mp1->b_cont = mp2;
16049 	iocp = (struct iocblk *)mp1->b_rptr;
16050 	iocp->ioc_count = sizeof (struct lifreq);
16051 
16052 	lifr = (struct lifreq *)mp2->b_rptr;
16053 	mp2->b_wptr += sizeof (struct lifreq);
16054 	bzero(lifr, sizeof (struct lifreq));
16055 
16056 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16057 	lifr->lifr_ppa = ill->ill_ppa;
16058 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16059 
16060 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16061 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16062 	putnext(q, mp1);
16063 }
16064 
16065 static int
16066 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16067 {
16068 	int		err;
16069 	ip_stack_t	*ipst = ill->ill_ipst;
16070 	phyint_t	*phyi = ill->ill_phyint;
16071 
16072 	/* Set the obsolete NDD per-interface forwarding name. */
16073 	err = ill_set_ndd_name(ill);
16074 	if (err != 0) {
16075 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16076 		    err);
16077 	}
16078 
16079 	/*
16080 	 * Now that ill_name is set, the configuration for the IPMP
16081 	 * meta-interface can be performed.
16082 	 */
16083 	if (IS_IPMP(ill)) {
16084 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16085 		/*
16086 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16087 		 * meta-interface and we need to create the IPMP group.
16088 		 */
16089 		if (phyi->phyint_grp == NULL) {
16090 			/*
16091 			 * If someone has renamed another IPMP group to have
16092 			 * the same name as our interface, bail.
16093 			 */
16094 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16095 				rw_exit(&ipst->ips_ipmp_lock);
16096 				return (EEXIST);
16097 			}
16098 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16099 			if (phyi->phyint_grp == NULL) {
16100 				rw_exit(&ipst->ips_ipmp_lock);
16101 				return (ENOMEM);
16102 			}
16103 		}
16104 		rw_exit(&ipst->ips_ipmp_lock);
16105 	}
16106 
16107 	/* Tell downstream modules where they are. */
16108 	ip_ifname_notify(ill, q);
16109 
16110 	/*
16111 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16112 	 * Error cases are ENOMEM ...
16113 	 */
16114 	err = ill_dl_phys(ill, ipif, mp, q);
16115 
16116 	if (ill->ill_isv6) {
16117 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16118 		if (ipst->ips_mld_slowtimeout_id == 0) {
16119 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16120 			    (void *)ipst,
16121 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16122 		}
16123 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16124 	} else {
16125 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16126 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16127 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16128 			    (void *)ipst,
16129 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16130 		}
16131 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16132 	}
16133 
16134 	return (err);
16135 }
16136 
16137 /*
16138  * Common routine for ppa and ifname setting. Should be called exclusive.
16139  *
16140  * Returns EINPROGRESS when mp has been consumed by queueing it on
16141  * ipx_pending_mp and the ioctl will complete in ip_rput.
16142  *
16143  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16144  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16145  * For SLIFNAME, we pass these values back to the userland.
16146  */
16147 static int
16148 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16149 {
16150 	ill_t	*ill;
16151 	ipif_t	*ipif;
16152 	ipsq_t	*ipsq;
16153 	char	*ppa_ptr;
16154 	char	*old_ptr;
16155 	char	old_char;
16156 	int	error;
16157 	ip_stack_t	*ipst;
16158 
16159 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16160 	ASSERT(q->q_next != NULL);
16161 	ASSERT(interf_name != NULL);
16162 
16163 	ill = (ill_t *)q->q_ptr;
16164 	ipst = ill->ill_ipst;
16165 
16166 	ASSERT(ill->ill_ipst != NULL);
16167 	ASSERT(ill->ill_name[0] == '\0');
16168 	ASSERT(IAM_WRITER_ILL(ill));
16169 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16170 	ASSERT(ill->ill_ppa == UINT_MAX);
16171 
16172 	ill->ill_defend_start = ill->ill_defend_count = 0;
16173 	/* The ppa is sent down by ifconfig or is chosen */
16174 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16175 		return (EINVAL);
16176 	}
16177 
16178 	/*
16179 	 * make sure ppa passed in is same as ppa in the name.
16180 	 * This check is not made when ppa == UINT_MAX in that case ppa
16181 	 * in the name could be anything. System will choose a ppa and
16182 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16183 	 */
16184 	if (*new_ppa_ptr != UINT_MAX) {
16185 		/* stoi changes the pointer */
16186 		old_ptr = ppa_ptr;
16187 		/*
16188 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16189 		 * (they don't have an externally visible ppa).  We assign one
16190 		 * here so that we can manage the interface.  Note that in
16191 		 * the past this value was always 0 for DLPI 1 drivers.
16192 		 */
16193 		if (*new_ppa_ptr == 0)
16194 			*new_ppa_ptr = stoi(&old_ptr);
16195 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16196 			return (EINVAL);
16197 	}
16198 	/*
16199 	 * terminate string before ppa
16200 	 * save char at that location.
16201 	 */
16202 	old_char = ppa_ptr[0];
16203 	ppa_ptr[0] = '\0';
16204 
16205 	ill->ill_ppa = *new_ppa_ptr;
16206 	/*
16207 	 * Finish as much work now as possible before calling ill_glist_insert
16208 	 * which makes the ill globally visible and also merges it with the
16209 	 * other protocol instance of this phyint. The remaining work is
16210 	 * done after entering the ipsq which may happen sometime later.
16211 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16212 	 */
16213 	ipif = ill->ill_ipif;
16214 
16215 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16216 	ipif_assign_seqid(ipif);
16217 
16218 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16219 		ill->ill_flags |= ILLF_IPV4;
16220 
16221 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16222 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16223 
16224 	if (ill->ill_flags & ILLF_IPV6) {
16225 
16226 		ill->ill_isv6 = B_TRUE;
16227 		ill_set_inputfn(ill);
16228 		if (ill->ill_rq != NULL) {
16229 			ill->ill_rq->q_qinfo = &iprinitv6;
16230 		}
16231 
16232 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16233 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16234 		ipif->ipif_v6subnet = ipv6_all_zeros;
16235 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16236 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16237 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16238 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16239 		/*
16240 		 * point-to-point or Non-mulicast capable
16241 		 * interfaces won't do NUD unless explicitly
16242 		 * configured to do so.
16243 		 */
16244 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16245 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16246 			ill->ill_flags |= ILLF_NONUD;
16247 		}
16248 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16249 		if (ill->ill_flags & ILLF_NOARP) {
16250 			/*
16251 			 * Note: xresolv interfaces will eventually need
16252 			 * NOARP set here as well, but that will require
16253 			 * those external resolvers to have some
16254 			 * knowledge of that flag and act appropriately.
16255 			 * Not to be changed at present.
16256 			 */
16257 			ill->ill_flags &= ~ILLF_NOARP;
16258 		}
16259 		/*
16260 		 * Set the ILLF_ROUTER flag according to the global
16261 		 * IPv6 forwarding policy.
16262 		 */
16263 		if (ipst->ips_ipv6_forward != 0)
16264 			ill->ill_flags |= ILLF_ROUTER;
16265 	} else if (ill->ill_flags & ILLF_IPV4) {
16266 		ill->ill_isv6 = B_FALSE;
16267 		ill_set_inputfn(ill);
16268 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16269 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16270 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16271 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16272 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16273 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16274 		/*
16275 		 * Set the ILLF_ROUTER flag according to the global
16276 		 * IPv4 forwarding policy.
16277 		 */
16278 		if (ipst->ips_ip_g_forward != 0)
16279 			ill->ill_flags |= ILLF_ROUTER;
16280 	}
16281 
16282 	ASSERT(ill->ill_phyint != NULL);
16283 
16284 	/*
16285 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16286 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16287 	 */
16288 	if (!ill_allocate_mibs(ill))
16289 		return (ENOMEM);
16290 
16291 	/*
16292 	 * Pick a default sap until we get the DL_INFO_ACK back from
16293 	 * the driver.
16294 	 */
16295 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16296 	    ill->ill_media->ip_m_ipv4sap;
16297 
16298 	ill->ill_ifname_pending = 1;
16299 	ill->ill_ifname_pending_err = 0;
16300 
16301 	/*
16302 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16303 	 * that were joined while this ill was not bound to the DLPI link need
16304 	 * to be recovered by ill_recover_multicast().
16305 	 */
16306 	ill->ill_need_recover_multicast = 1;
16307 
16308 	ill_refhold(ill);
16309 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16310 	if ((error = ill_glist_insert(ill, interf_name,
16311 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16312 		ill->ill_ppa = UINT_MAX;
16313 		ill->ill_name[0] = '\0';
16314 		/*
16315 		 * undo null termination done above.
16316 		 */
16317 		ppa_ptr[0] = old_char;
16318 		rw_exit(&ipst->ips_ill_g_lock);
16319 		ill_refrele(ill);
16320 		return (error);
16321 	}
16322 
16323 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16324 
16325 	/*
16326 	 * When we return the buffer pointed to by interf_name should contain
16327 	 * the same name as in ill_name.
16328 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16329 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16330 	 * so copy full name and update the ppa ptr.
16331 	 * When ppa passed in != UINT_MAX all values are correct just undo
16332 	 * null termination, this saves a bcopy.
16333 	 */
16334 	if (*new_ppa_ptr == UINT_MAX) {
16335 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16336 		*new_ppa_ptr = ill->ill_ppa;
16337 	} else {
16338 		/*
16339 		 * undo null termination done above.
16340 		 */
16341 		ppa_ptr[0] = old_char;
16342 	}
16343 
16344 	/* Let SCTP know about this ILL */
16345 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16346 
16347 	/*
16348 	 * ill_glist_insert has made the ill visible globally, and
16349 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16350 	 * we need to hold the ips_ill_g_lock across the call to enter the
16351 	 * ipsq to enforce atomicity and prevent reordering. In the event
16352 	 * the ipsq has changed, and if the new ipsq is currently busy,
16353 	 * we need to make sure that this half-completed ioctl is ahead of
16354 	 * any subsequent ioctl. We achieve this by not dropping the
16355 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16356 	 * ensuring that new ioctls can't start.
16357 	 */
16358 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16359 	    B_TRUE);
16360 
16361 	rw_exit(&ipst->ips_ill_g_lock);
16362 	ill_refrele(ill);
16363 	if (ipsq == NULL)
16364 		return (EINPROGRESS);
16365 
16366 	/*
16367 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16368 	 */
16369 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16370 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16371 	else
16372 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16373 
16374 	error = ipif_set_values_tail(ill, ipif, mp, q);
16375 	ipsq_exit(ipsq);
16376 	if (error != 0 && error != EINPROGRESS) {
16377 		/*
16378 		 * restore previous values
16379 		 */
16380 		ill->ill_isv6 = B_FALSE;
16381 		ill_set_inputfn(ill);
16382 	}
16383 	return (error);
16384 }
16385 
16386 void
16387 ipif_init(ip_stack_t *ipst)
16388 {
16389 	int i;
16390 
16391 	for (i = 0; i < MAX_G_HEADS; i++) {
16392 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16393 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16394 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16395 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16396 	}
16397 
16398 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16399 	    ill_phyint_compare_index,
16400 	    sizeof (phyint_t),
16401 	    offsetof(struct phyint, phyint_avl_by_index));
16402 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16403 	    ill_phyint_compare_name,
16404 	    sizeof (phyint_t),
16405 	    offsetof(struct phyint, phyint_avl_by_name));
16406 }
16407 
16408 /*
16409  * Save enough information so that we can recreate the IRE if
16410  * the interface goes down and then up.
16411  */
16412 void
16413 ill_save_ire(ill_t *ill, ire_t *ire)
16414 {
16415 	mblk_t	*save_mp;
16416 
16417 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16418 	if (save_mp != NULL) {
16419 		ifrt_t	*ifrt;
16420 
16421 		save_mp->b_wptr += sizeof (ifrt_t);
16422 		ifrt = (ifrt_t *)save_mp->b_rptr;
16423 		bzero(ifrt, sizeof (ifrt_t));
16424 		ifrt->ifrt_type = ire->ire_type;
16425 		if (ire->ire_ipversion == IPV4_VERSION) {
16426 			ASSERT(!ill->ill_isv6);
16427 			ifrt->ifrt_addr = ire->ire_addr;
16428 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16429 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16430 			ifrt->ifrt_mask = ire->ire_mask;
16431 		} else {
16432 			ASSERT(ill->ill_isv6);
16433 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16434 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16435 			mutex_enter(&ire->ire_lock);
16436 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16437 			mutex_exit(&ire->ire_lock);
16438 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16439 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16440 		}
16441 		ifrt->ifrt_flags = ire->ire_flags;
16442 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16443 		mutex_enter(&ill->ill_saved_ire_lock);
16444 		save_mp->b_cont = ill->ill_saved_ire_mp;
16445 		ill->ill_saved_ire_mp = save_mp;
16446 		ill->ill_saved_ire_cnt++;
16447 		mutex_exit(&ill->ill_saved_ire_lock);
16448 	}
16449 }
16450 
16451 /*
16452  * Remove one entry from ill_saved_ire_mp.
16453  */
16454 void
16455 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16456 {
16457 	mblk_t	**mpp;
16458 	mblk_t	*mp;
16459 	ifrt_t	*ifrt;
16460 
16461 	/* Remove from ill_saved_ire_mp list if it is there */
16462 	mutex_enter(&ill->ill_saved_ire_lock);
16463 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16464 	    mpp = &(*mpp)->b_cont) {
16465 		in6_addr_t	gw_addr_v6;
16466 
16467 		/*
16468 		 * On a given ill, the tuple of address, gateway, mask,
16469 		 * ire_type, and zoneid is unique for each saved IRE.
16470 		 */
16471 		mp = *mpp;
16472 		ifrt = (ifrt_t *)mp->b_rptr;
16473 		/* ire_gateway_addr_v6 can change - need lock */
16474 		mutex_enter(&ire->ire_lock);
16475 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16476 		mutex_exit(&ire->ire_lock);
16477 
16478 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16479 		    ifrt->ifrt_type != ire->ire_type)
16480 			continue;
16481 
16482 		if (ill->ill_isv6 ?
16483 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16484 		    &ire->ire_addr_v6) &&
16485 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16486 		    &gw_addr_v6) &&
16487 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16488 		    &ire->ire_mask_v6)) :
16489 		    (ifrt->ifrt_addr == ire->ire_addr &&
16490 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16491 		    ifrt->ifrt_mask == ire->ire_mask)) {
16492 			*mpp = mp->b_cont;
16493 			ill->ill_saved_ire_cnt--;
16494 			freeb(mp);
16495 			break;
16496 		}
16497 	}
16498 	mutex_exit(&ill->ill_saved_ire_lock);
16499 }
16500 
16501 /*
16502  * IP multirouting broadcast routes handling
16503  * Append CGTP broadcast IREs to regular ones created
16504  * at ifconfig time.
16505  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16506  * the destination and the gateway are broadcast addresses.
16507  * The caller has verified that the destination is an IRE_BROADCAST and that
16508  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16509  * we create a MULTIRT IRE_BROADCAST.
16510  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16511  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16512  */
16513 static void
16514 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16515 {
16516 	ire_t *ire_prim;
16517 
16518 	ASSERT(ire != NULL);
16519 
16520 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16521 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16522 	    NULL);
16523 	if (ire_prim != NULL) {
16524 		/*
16525 		 * We are in the special case of broadcasts for
16526 		 * CGTP. We add an IRE_BROADCAST that holds
16527 		 * the RTF_MULTIRT flag, the destination
16528 		 * address and the low level
16529 		 * info of ire_prim. In other words, CGTP
16530 		 * broadcast is added to the redundant ipif.
16531 		 */
16532 		ill_t *ill_prim;
16533 		ire_t  *bcast_ire;
16534 
16535 		ill_prim = ire_prim->ire_ill;
16536 
16537 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16538 		    (void *)ire_prim, (void *)ill_prim));
16539 
16540 		bcast_ire = ire_create(
16541 		    (uchar_t *)&ire->ire_addr,
16542 		    (uchar_t *)&ip_g_all_ones,
16543 		    (uchar_t *)&ire->ire_gateway_addr,
16544 		    IRE_BROADCAST,
16545 		    ill_prim,
16546 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16547 		    ire->ire_flags | RTF_KERNEL,
16548 		    NULL,
16549 		    ipst);
16550 
16551 		/*
16552 		 * Here we assume that ire_add does head insertion so that
16553 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16554 		 */
16555 		if (bcast_ire != NULL) {
16556 			if (ire->ire_flags & RTF_SETSRC) {
16557 				bcast_ire->ire_setsrc_addr =
16558 				    ire->ire_setsrc_addr;
16559 			}
16560 			bcast_ire = ire_add(bcast_ire);
16561 			if (bcast_ire != NULL) {
16562 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16563 				    "added bcast_ire %p\n",
16564 				    (void *)bcast_ire));
16565 
16566 				ill_save_ire(ill_prim, bcast_ire);
16567 				ire_refrele(bcast_ire);
16568 			}
16569 		}
16570 		ire_refrele(ire_prim);
16571 	}
16572 }
16573 
16574 /*
16575  * IP multirouting broadcast routes handling
16576  * Remove the broadcast ire.
16577  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16578  * the destination and the gateway are broadcast addresses.
16579  * The caller has only verified that RTF_MULTIRT was set. We check
16580  * that the destination is broadcast and that the gateway is a broadcast
16581  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16582  */
16583 static void
16584 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16585 {
16586 	ASSERT(ire != NULL);
16587 
16588 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16589 		ire_t *ire_prim;
16590 
16591 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16592 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16593 		    ipst, NULL);
16594 		if (ire_prim != NULL) {
16595 			ill_t *ill_prim;
16596 			ire_t  *bcast_ire;
16597 
16598 			ill_prim = ire_prim->ire_ill;
16599 
16600 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16601 			    "ire_prim %p, ill_prim %p\n",
16602 			    (void *)ire_prim, (void *)ill_prim));
16603 
16604 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16605 			    ire->ire_gateway_addr, IRE_BROADCAST,
16606 			    ill_prim, ALL_ZONES, NULL,
16607 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16608 			    MATCH_IRE_MASK, 0, ipst, NULL);
16609 
16610 			if (bcast_ire != NULL) {
16611 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16612 				    "looked up bcast_ire %p\n",
16613 				    (void *)bcast_ire));
16614 				ill_remove_saved_ire(bcast_ire->ire_ill,
16615 				    bcast_ire);
16616 				ire_delete(bcast_ire);
16617 				ire_refrele(bcast_ire);
16618 			}
16619 			ire_refrele(ire_prim);
16620 		}
16621 	}
16622 }
16623 
16624 /*
16625  * Derive an interface id from the link layer address.
16626  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16627  */
16628 static void
16629 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16630 {
16631 	char		*addr;
16632 
16633 	/*
16634 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16635 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16636 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16637 	 * interface ID on IPv6 interfaces above links that actually have real
16638 	 * Ethernet addresses.
16639 	 */
16640 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16641 		/* Form EUI-64 like address */
16642 		addr = (char *)&v6addr->s6_addr32[2];
16643 		bcopy(ill->ill_phys_addr, addr, 3);
16644 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16645 		addr[3] = (char)0xff;
16646 		addr[4] = (char)0xfe;
16647 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16648 	}
16649 }
16650 
16651 /* ARGSUSED */
16652 static void
16653 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16654 {
16655 }
16656 
16657 typedef struct ipmp_ifcookie {
16658 	uint32_t	ic_hostid;
16659 	char		ic_ifname[LIFNAMSIZ];
16660 	char		ic_zonename[ZONENAME_MAX];
16661 } ipmp_ifcookie_t;
16662 
16663 /*
16664  * Construct a pseudo-random interface ID for the IPMP interface that's both
16665  * predictable and (almost) guaranteed to be unique.
16666  */
16667 static void
16668 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16669 {
16670 	zone_t		*zp;
16671 	uint8_t		*addr;
16672 	uchar_t		hash[16];
16673 	ulong_t 	hostid;
16674 	MD5_CTX		ctx;
16675 	ipmp_ifcookie_t	ic = { 0 };
16676 
16677 	ASSERT(IS_IPMP(ill));
16678 
16679 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16680 	ic.ic_hostid = htonl((uint32_t)hostid);
16681 
16682 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16683 
16684 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16685 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16686 		zone_rele(zp);
16687 	}
16688 
16689 	MD5Init(&ctx);
16690 	MD5Update(&ctx, &ic, sizeof (ic));
16691 	MD5Final(hash, &ctx);
16692 
16693 	/*
16694 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16695 	 */
16696 	addr = &v6addr->s6_addr8[8];
16697 	bcopy(hash + 8, addr, sizeof (uint64_t));
16698 	addr[0] &= ~0x2;				/* set local bit */
16699 }
16700 
16701 /*
16702  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16703  */
16704 static void
16705 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16706 {
16707 	phyint_t *phyi = ill->ill_phyint;
16708 
16709 	/*
16710 	 * Check PHYI_MULTI_BCAST and length of physical
16711 	 * address to determine if we use the mapping or the
16712 	 * broadcast address.
16713 	 */
16714 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16715 	    ill->ill_phys_addr_length != ETHERADDRL) {
16716 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16717 		return;
16718 	}
16719 	m_physaddr[0] = 0x33;
16720 	m_physaddr[1] = 0x33;
16721 	m_physaddr[2] = m_ip6addr[12];
16722 	m_physaddr[3] = m_ip6addr[13];
16723 	m_physaddr[4] = m_ip6addr[14];
16724 	m_physaddr[5] = m_ip6addr[15];
16725 }
16726 
16727 /*
16728  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16729  */
16730 static void
16731 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16732 {
16733 	phyint_t *phyi = ill->ill_phyint;
16734 
16735 	/*
16736 	 * Check PHYI_MULTI_BCAST and length of physical
16737 	 * address to determine if we use the mapping or the
16738 	 * broadcast address.
16739 	 */
16740 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16741 	    ill->ill_phys_addr_length != ETHERADDRL) {
16742 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16743 		return;
16744 	}
16745 	m_physaddr[0] = 0x01;
16746 	m_physaddr[1] = 0x00;
16747 	m_physaddr[2] = 0x5e;
16748 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16749 	m_physaddr[4] = m_ipaddr[2];
16750 	m_physaddr[5] = m_ipaddr[3];
16751 }
16752 
16753 /* ARGSUSED */
16754 static void
16755 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16756 {
16757 	/*
16758 	 * for the MULTI_BCAST case and other cases when we want to
16759 	 * use the link-layer broadcast address for multicast.
16760 	 */
16761 	uint8_t	*bphys_addr;
16762 	dl_unitdata_req_t *dlur;
16763 
16764 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16765 	if (ill->ill_sap_length < 0) {
16766 		bphys_addr = (uchar_t *)dlur +
16767 		    dlur->dl_dest_addr_offset;
16768 	} else  {
16769 		bphys_addr = (uchar_t *)dlur +
16770 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
16771 	}
16772 
16773 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
16774 }
16775 
16776 /*
16777  * Derive IPoIB interface id from the link layer address.
16778  */
16779 static void
16780 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16781 {
16782 	char		*addr;
16783 
16784 	ASSERT(ill->ill_phys_addr_length == 20);
16785 	addr = (char *)&v6addr->s6_addr32[2];
16786 	bcopy(ill->ill_phys_addr + 12, addr, 8);
16787 	/*
16788 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
16789 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
16790 	 * rules. In these cases, the IBA considers these GUIDs to be in
16791 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
16792 	 * required; vendors are required not to assign global EUI-64's
16793 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
16794 	 * of the interface identifier. Whether the GUID is in modified
16795 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
16796 	 * bit set to 1.
16797 	 */
16798 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
16799 }
16800 
16801 /*
16802  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
16803  * Note on mapping from multicast IP addresses to IPoIB multicast link
16804  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
16805  * The format of an IPoIB multicast address is:
16806  *
16807  *  4 byte QPN      Scope Sign.  Pkey
16808  * +--------------------------------------------+
16809  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
16810  * +--------------------------------------------+
16811  *
16812  * The Scope and Pkey components are properties of the IBA port and
16813  * network interface. They can be ascertained from the broadcast address.
16814  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
16815  */
16816 static void
16817 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16818 {
16819 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16820 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
16821 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16822 	uint8_t	*bphys_addr;
16823 	dl_unitdata_req_t *dlur;
16824 
16825 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
16826 
16827 	/*
16828 	 * RFC 4391: IPv4 MGID is 28-bit long.
16829 	 */
16830 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
16831 	m_physaddr[17] = m_ipaddr[1];
16832 	m_physaddr[18] = m_ipaddr[2];
16833 	m_physaddr[19] = m_ipaddr[3];
16834 
16835 
16836 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16837 	if (ill->ill_sap_length < 0) {
16838 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
16839 	} else  {
16840 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
16841 		    ill->ill_sap_length;
16842 	}
16843 	/*
16844 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
16845 	 */
16846 	m_physaddr[5] = bphys_addr[5];
16847 	m_physaddr[8] = bphys_addr[8];
16848 	m_physaddr[9] = bphys_addr[9];
16849 }
16850 
16851 static void
16852 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16853 {
16854 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16855 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
16856 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16857 	uint8_t	*bphys_addr;
16858 	dl_unitdata_req_t *dlur;
16859 
16860 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
16861 
16862 	/*
16863 	 * RFC 4391: IPv4 MGID is 80-bit long.
16864 	 */
16865 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
16866 
16867 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16868 	if (ill->ill_sap_length < 0) {
16869 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
16870 	} else  {
16871 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
16872 		    ill->ill_sap_length;
16873 	}
16874 	/*
16875 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
16876 	 */
16877 	m_physaddr[5] = bphys_addr[5];
16878 	m_physaddr[8] = bphys_addr[8];
16879 	m_physaddr[9] = bphys_addr[9];
16880 }
16881 
16882 /*
16883  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
16884  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
16885  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
16886  * of RFC4213.
16887  */
16888 static void
16889 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
16890 {
16891 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
16892 	v6addr->s6_addr32[2] = 0;
16893 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
16894 }
16895 
16896 /*
16897  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
16898  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
16899  * id.
16900  */
16901 static void
16902 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
16903 {
16904 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
16905 
16906 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
16907 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
16908 }
16909 
16910 static void
16911 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16912 {
16913 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
16914 }
16915 
16916 static void
16917 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
16918 {
16919 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
16920 }
16921 
16922 static void
16923 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16924 {
16925 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
16926 }
16927 
16928 static void
16929 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
16930 {
16931 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
16932 }
16933 
16934 /*
16935  * Lookup an ill and verify that the zoneid has an ipif on that ill.
16936  * Returns an held ill, or NULL.
16937  */
16938 ill_t *
16939 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
16940     ip_stack_t *ipst)
16941 {
16942 	ill_t	*ill;
16943 	ipif_t	*ipif;
16944 
16945 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
16946 	if (ill == NULL)
16947 		return (NULL);
16948 
16949 	mutex_enter(&ill->ill_lock);
16950 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16951 		if (IPIF_IS_CONDEMNED(ipif))
16952 			continue;
16953 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
16954 		    ipif->ipif_zoneid != ALL_ZONES)
16955 			continue;
16956 
16957 		mutex_exit(&ill->ill_lock);
16958 		return (ill);
16959 	}
16960 	mutex_exit(&ill->ill_lock);
16961 	ill_refrele(ill);
16962 	return (NULL);
16963 }
16964 
16965 /*
16966  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
16967  * If a pointer to an ipif_t is returned then the caller will need to do
16968  * an ill_refrele().
16969  */
16970 ipif_t *
16971 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
16972     ip_stack_t *ipst)
16973 {
16974 	ipif_t *ipif;
16975 	ill_t *ill;
16976 
16977 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16978 	if (ill == NULL)
16979 		return (NULL);
16980 
16981 	mutex_enter(&ill->ill_lock);
16982 	if (ill->ill_state_flags & ILL_CONDEMNED) {
16983 		mutex_exit(&ill->ill_lock);
16984 		ill_refrele(ill);
16985 		return (NULL);
16986 	}
16987 
16988 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16989 		if (!IPIF_CAN_LOOKUP(ipif))
16990 			continue;
16991 		if (lifidx == ipif->ipif_id) {
16992 			ipif_refhold_locked(ipif);
16993 			break;
16994 		}
16995 	}
16996 
16997 	mutex_exit(&ill->ill_lock);
16998 	ill_refrele(ill);
16999 	return (ipif);
17000 }
17001 
17002 /*
17003  * Set ill_inputfn based on the current know state.
17004  * This needs to be called when any of the factors taken into
17005  * account changes.
17006  */
17007 void
17008 ill_set_inputfn(ill_t *ill)
17009 {
17010 	ip_stack_t	*ipst = ill->ill_ipst;
17011 
17012 	if (ill->ill_isv6) {
17013 		if (is_system_labeled())
17014 			ill->ill_inputfn = ill_input_full_v6;
17015 		else
17016 			ill->ill_inputfn = ill_input_short_v6;
17017 	} else {
17018 		if (is_system_labeled())
17019 			ill->ill_inputfn = ill_input_full_v4;
17020 		else if (ill->ill_dhcpinit != 0)
17021 			ill->ill_inputfn = ill_input_full_v4;
17022 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17023 		    != NULL)
17024 			ill->ill_inputfn = ill_input_full_v4;
17025 		else if (ipst->ips_ip_cgtp_filter &&
17026 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17027 			ill->ill_inputfn = ill_input_full_v4;
17028 		else
17029 			ill->ill_inputfn = ill_input_short_v4;
17030 	}
17031 }
17032 
17033 /*
17034  * Re-evaluate ill_inputfn for all the IPv4 ills.
17035  * Used when RSVP and CGTP comes and goes.
17036  */
17037 void
17038 ill_set_inputfn_all(ip_stack_t *ipst)
17039 {
17040 	ill_walk_context_t	ctx;
17041 	ill_t			*ill;
17042 
17043 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17044 	ill = ILL_START_WALK_V4(&ctx, ipst);
17045 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17046 		ill_set_inputfn(ill);
17047 
17048 	rw_exit(&ipst->ips_ill_g_lock);
17049 }
17050 
17051 /*
17052  * Set the physical address information for `ill' to the contents of the
17053  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17054  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17055  * EINPROGRESS will be returned.
17056  */
17057 int
17058 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17059 {
17060 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17061 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17062 
17063 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17064 
17065 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17066 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17067 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17068 		/* Changing DL_IPV6_TOKEN is not yet supported */
17069 		return (0);
17070 	}
17071 
17072 	/*
17073 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17074 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17075 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17076 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17077 	 */
17078 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17079 		freemsg(mp);
17080 		return (ENOMEM);
17081 	}
17082 
17083 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17084 	mutex_enter(&ill->ill_lock);
17085 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17086 	/* no more nce addition allowed */
17087 	mutex_exit(&ill->ill_lock);
17088 
17089 	/*
17090 	 * If we can quiesce the ill, then set the address.  If not, then
17091 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17092 	 */
17093 	ill_down_ipifs(ill, B_TRUE);
17094 	mutex_enter(&ill->ill_lock);
17095 	if (!ill_is_quiescent(ill)) {
17096 		/* call cannot fail since `conn_t *' argument is NULL */
17097 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17098 		    mp, ILL_DOWN);
17099 		mutex_exit(&ill->ill_lock);
17100 		return (EINPROGRESS);
17101 	}
17102 	mutex_exit(&ill->ill_lock);
17103 
17104 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17105 	return (0);
17106 }
17107 
17108 /*
17109  * Once the ill associated with `q' has quiesced, set its physical address
17110  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17111  * are passed (linked by b_cont), since we sometimes need to save two distinct
17112  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17113  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17114  * is quiesced, we know any stale nce's with the old address information have
17115  * already been removed, so we don't need to call nce_flush().
17116  */
17117 /* ARGSUSED */
17118 static void
17119 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17120 {
17121 	ill_t		*ill = q->q_ptr;
17122 	mblk_t		*addrmp2 = unlinkb(addrmp);
17123 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17124 	uint_t		addrlen, addroff;
17125 	int		status;
17126 
17127 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17128 
17129 	addroff	= dlindp->dl_addr_offset;
17130 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17131 
17132 	switch (dlindp->dl_data) {
17133 	case DL_IPV6_LINK_LAYER_ADDR:
17134 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17135 		freemsg(addrmp2);
17136 		break;
17137 
17138 	case DL_CURR_DEST_ADDR:
17139 		freemsg(ill->ill_dest_addr_mp);
17140 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17141 		ill->ill_dest_addr_mp = addrmp;
17142 		if (ill->ill_isv6) {
17143 			ill_setdesttoken(ill);
17144 			ipif_setdestlinklocal(ill->ill_ipif);
17145 		}
17146 		freemsg(addrmp2);
17147 		break;
17148 
17149 	case DL_CURR_PHYS_ADDR:
17150 		freemsg(ill->ill_phys_addr_mp);
17151 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17152 		ill->ill_phys_addr_mp = addrmp;
17153 		ill->ill_phys_addr_length = addrlen;
17154 		if (ill->ill_isv6)
17155 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17156 		else
17157 			freemsg(addrmp2);
17158 		if (ill->ill_isv6) {
17159 			ill_setdefaulttoken(ill);
17160 			ipif_setlinklocal(ill->ill_ipif);
17161 		}
17162 		break;
17163 	default:
17164 		ASSERT(0);
17165 	}
17166 
17167 	/*
17168 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17169 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17170 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17171 	 * brought up.
17172 	 */
17173 	status = ill_up_ipifs(ill, q, addrmp);
17174 	mutex_enter(&ill->ill_lock);
17175 	if (ill->ill_dl_up)
17176 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17177 	mutex_exit(&ill->ill_lock);
17178 	if (status != EINPROGRESS)
17179 		ipsq_current_finish(ipsq);
17180 }
17181 
17182 /*
17183  * Helper routine for setting the ill_nd_lla fields.
17184  */
17185 void
17186 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17187 {
17188 	freemsg(ill->ill_nd_lla_mp);
17189 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17190 	ill->ill_nd_lla_mp = ndmp;
17191 	ill->ill_nd_lla_len = addrlen;
17192 }
17193 
17194 /*
17195  * Replumb the ill.
17196  */
17197 int
17198 ill_replumb(ill_t *ill, mblk_t *mp)
17199 {
17200 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17201 
17202 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17203 
17204 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17205 
17206 	mutex_enter(&ill->ill_lock);
17207 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17208 	/* no more nce addition allowed */
17209 	mutex_exit(&ill->ill_lock);
17210 
17211 	/*
17212 	 * If we can quiesce the ill, then continue.  If not, then
17213 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17214 	 */
17215 	ill_down_ipifs(ill, B_FALSE);
17216 
17217 	mutex_enter(&ill->ill_lock);
17218 	if (!ill_is_quiescent(ill)) {
17219 		/* call cannot fail since `conn_t *' argument is NULL */
17220 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17221 		    mp, ILL_DOWN);
17222 		mutex_exit(&ill->ill_lock);
17223 		return (EINPROGRESS);
17224 	}
17225 	mutex_exit(&ill->ill_lock);
17226 
17227 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17228 	return (0);
17229 }
17230 
17231 /* ARGSUSED */
17232 static void
17233 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17234 {
17235 	ill_t *ill = q->q_ptr;
17236 	int err;
17237 	conn_t *connp = NULL;
17238 
17239 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17240 	freemsg(ill->ill_replumb_mp);
17241 	ill->ill_replumb_mp = copyb(mp);
17242 
17243 	if (ill->ill_replumb_mp == NULL) {
17244 		/* out of memory */
17245 		ipsq_current_finish(ipsq);
17246 		return;
17247 	}
17248 
17249 	mutex_enter(&ill->ill_lock);
17250 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17251 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17252 	mutex_exit(&ill->ill_lock);
17253 
17254 	if (!ill->ill_up_ipifs) {
17255 		/* already closing */
17256 		ipsq_current_finish(ipsq);
17257 		return;
17258 	}
17259 	ill->ill_replumbing = 1;
17260 	err = ill_down_ipifs_tail(ill);
17261 
17262 	/*
17263 	 * Successfully quiesced and brought down the interface, now we send
17264 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17265 	 * DL_NOTE_REPLUMB message.
17266 	 */
17267 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17268 	    DL_NOTIFY_CONF);
17269 	ASSERT(mp != NULL);
17270 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17271 	    DL_NOTE_REPLUMB_DONE;
17272 	ill_dlpi_send(ill, mp);
17273 
17274 	/*
17275 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17276 	 * streams have to be unbound. When all the DLPI exchanges are done,
17277 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17278 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17279 	 * arp_bringup_done().
17280 	 */
17281 	ASSERT(ill->ill_replumb_mp != NULL);
17282 	if (err == EINPROGRESS)
17283 		return;
17284 	else
17285 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17286 	ASSERT(connp == NULL);
17287 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17288 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17289 		return;
17290 	}
17291 	ipsq_current_finish(ipsq);
17292 }
17293 
17294 /*
17295  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17296  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17297  * as per the ioctl.  On failure, an errno is returned.
17298  */
17299 static int
17300 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17301 {
17302 	int rval;
17303 	struct strioctl iocb;
17304 
17305 	iocb.ic_cmd = cmd;
17306 	iocb.ic_timout = 15;
17307 	iocb.ic_len = bufsize;
17308 	iocb.ic_dp = buf;
17309 
17310 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17311 }
17312 
17313 /*
17314  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17315  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17316  */
17317 static int
17318 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17319     uint_t *bufsizep, cred_t *cr)
17320 {
17321 	int err;
17322 	struct lifnum lifn;
17323 
17324 	bzero(&lifn, sizeof (lifn));
17325 	lifn.lifn_family = af;
17326 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17327 
17328 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17329 		return (err);
17330 
17331 	/*
17332 	 * Pad the interface count to account for additional interfaces that
17333 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17334 	 */
17335 	lifn.lifn_count += 4;
17336 	bzero(lifcp, sizeof (*lifcp));
17337 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17338 	lifcp->lifc_family = af;
17339 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17340 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17341 
17342 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17343 	if (err != 0) {
17344 		kmem_free(lifcp->lifc_buf, *bufsizep);
17345 		return (err);
17346 	}
17347 
17348 	return (0);
17349 }
17350 
17351 /*
17352  * Helper for ip_interface_cleanup() that removes the loopback interface.
17353  */
17354 static void
17355 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17356 {
17357 	int err;
17358 	struct lifreq lifr;
17359 
17360 	bzero(&lifr, sizeof (lifr));
17361 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17362 
17363 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17364 	if (err != 0) {
17365 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17366 		    "error %d\n", isv6 ? "v6" : "v4", err));
17367 	}
17368 }
17369 
17370 /*
17371  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17372  * groups and that IPMP data addresses are down.  These conditions must be met
17373  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17374  */
17375 static void
17376 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17377 {
17378 	int af = isv6 ? AF_INET6 : AF_INET;
17379 	int i, nifs;
17380 	int err;
17381 	uint_t bufsize;
17382 	uint_t lifrsize = sizeof (struct lifreq);
17383 	struct lifconf lifc;
17384 	struct lifreq *lifrp;
17385 
17386 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17387 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17388 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17389 		return;
17390 	}
17391 
17392 	nifs = lifc.lifc_len / lifrsize;
17393 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17394 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17395 		if (err != 0) {
17396 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17397 			    "flags: error %d", lifrp->lifr_name, err);
17398 			continue;
17399 		}
17400 
17401 		if (lifrp->lifr_flags & IFF_IPMP) {
17402 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17403 				continue;
17404 
17405 			lifrp->lifr_flags &= ~IFF_UP;
17406 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17407 			if (err != 0) {
17408 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17409 				    "bring down (error %d); IPMP interface may "
17410 				    "not be shutdown", lifrp->lifr_name, err);
17411 			}
17412 
17413 			/*
17414 			 * Check if IFF_DUPLICATE is still set -- and if so,
17415 			 * reset the address to clear it.
17416 			 */
17417 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17418 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17419 				continue;
17420 
17421 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17422 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17423 			    lifrp, lifrsize, cr)) != 0) {
17424 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17425 				    "reset DAD (error %d); IPMP interface may "
17426 				    "not be shutdown", lifrp->lifr_name, err);
17427 			}
17428 			continue;
17429 		}
17430 
17431 		lifrp->lifr_groupname[0] = '\0';
17432 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17433 		if (err != 0) {
17434 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17435 			    "IPMP group (error %d); associated IPMP interface "
17436 			    "may not be shutdown", lifrp->lifr_name, err);
17437 			continue;
17438 		}
17439 	}
17440 
17441 	kmem_free(lifc.lifc_buf, bufsize);
17442 }
17443 
17444 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17445 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17446 
17447 /*
17448  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17449  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17450  * when the user-level processes in the zone are killed and the latter are
17451  * cleaned up by str_stack_shutdown().
17452  */
17453 void
17454 ip_interface_cleanup(ip_stack_t *ipst)
17455 {
17456 	ldi_handle_t	lh;
17457 	ldi_ident_t	li;
17458 	cred_t		*cr;
17459 	int		err;
17460 	int		i;
17461 	char		*devs[] = { UDP6DEV, UDPDEV };
17462 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17463 
17464 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17465 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17466 		    " error %d", err);
17467 		return;
17468 	}
17469 
17470 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17471 	ASSERT(cr != NULL);
17472 
17473 	/*
17474 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17475 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17476 	 * the loop.)
17477 	 */
17478 	for (i = 0; i < 2; i++) {
17479 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17480 		if (err != 0) {
17481 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17482 			    " error %d", devs[i], err);
17483 			continue;
17484 		}
17485 
17486 		ip_loopback_removeif(lh, i == 0, cr);
17487 		ip_ipmp_cleanup(lh, i == 0, cr);
17488 
17489 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17490 	}
17491 
17492 	ldi_ident_release(li);
17493 	crfree(cr);
17494 }
17495 
17496 /*
17497  * This needs to be in-sync with nic_event_t definition
17498  */
17499 static const char *
17500 ill_hook_event2str(nic_event_t event)
17501 {
17502 	switch (event) {
17503 	case NE_PLUMB:
17504 		return ("PLUMB");
17505 	case NE_UNPLUMB:
17506 		return ("UNPLUMB");
17507 	case NE_UP:
17508 		return ("UP");
17509 	case NE_DOWN:
17510 		return ("DOWN");
17511 	case NE_ADDRESS_CHANGE:
17512 		return ("ADDRESS_CHANGE");
17513 	case NE_LIF_UP:
17514 		return ("LIF_UP");
17515 	case NE_LIF_DOWN:
17516 		return ("LIF_DOWN");
17517 	case NE_IFINDEX_CHANGE:
17518 		return ("IFINDEX_CHANGE");
17519 	default:
17520 		return ("UNKNOWN");
17521 	}
17522 }
17523 
17524 void
17525 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17526     nic_event_data_t data, size_t datalen)
17527 {
17528 	ip_stack_t		*ipst = ill->ill_ipst;
17529 	hook_nic_event_int_t	*info;
17530 	const char		*str = NULL;
17531 
17532 	/* create a new nic event info */
17533 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17534 		goto fail;
17535 
17536 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17537 	info->hnei_event.hne_lif = lif;
17538 	info->hnei_event.hne_event = event;
17539 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17540 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17541 	info->hnei_event.hne_data = NULL;
17542 	info->hnei_event.hne_datalen = 0;
17543 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17544 
17545 	if (data != NULL && datalen != 0) {
17546 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17547 		if (info->hnei_event.hne_data == NULL)
17548 			goto fail;
17549 		bcopy(data, info->hnei_event.hne_data, datalen);
17550 		info->hnei_event.hne_datalen = datalen;
17551 	}
17552 
17553 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17554 	    DDI_NOSLEEP) == DDI_SUCCESS)
17555 		return;
17556 
17557 fail:
17558 	if (info != NULL) {
17559 		if (info->hnei_event.hne_data != NULL) {
17560 			kmem_free(info->hnei_event.hne_data,
17561 			    info->hnei_event.hne_datalen);
17562 		}
17563 		kmem_free(info, sizeof (hook_nic_event_t));
17564 	}
17565 	str = ill_hook_event2str(event);
17566 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17567 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17568 }
17569 
17570 static int
17571 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17572 {
17573 	int		err = 0;
17574 	const in_addr_t	*addr = NULL;
17575 	nce_t		*nce = NULL;
17576 	ill_t		*ill = ipif->ipif_ill;
17577 	ill_t		*bound_ill;
17578 	boolean_t	added_ipif = B_FALSE;
17579 	uint16_t	state;
17580 	uint16_t	flags;
17581 
17582 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17583 	    ill_t *, ill, ipif_t *, ipif);
17584 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17585 		addr = &ipif->ipif_lcl_addr;
17586 	}
17587 
17588 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17589 		if (res_act != Res_act_initial)
17590 			return (EINVAL);
17591 	}
17592 
17593 	if (addr != NULL) {
17594 		ipmp_illgrp_t	*illg = ill->ill_grp;
17595 
17596 		/* add unicast nce for the local addr */
17597 
17598 		if (IS_IPMP(ill)) {
17599 			/*
17600 			 * If we're here via ipif_up(), then the ipif
17601 			 * won't be bound yet -- add it to the group,
17602 			 * which will bind it if possible. (We would
17603 			 * add it in ipif_up(), but deleting on failure
17604 			 * there is gruesome.)  If we're here via
17605 			 * ipmp_ill_bind_ipif(), then the ipif has
17606 			 * already been added to the group and we
17607 			 * just need to use the binding.
17608 			 */
17609 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17610 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17611 				if (bound_ill == NULL) {
17612 					/*
17613 					 * We couldn't bind the ipif to an ill
17614 					 * yet, so we have nothing to publish.
17615 					 * Mark the address as ready and return.
17616 					 */
17617 					ipif->ipif_addr_ready = 1;
17618 					return (0);
17619 				}
17620 				added_ipif = B_TRUE;
17621 			}
17622 		} else {
17623 			bound_ill = ill;
17624 		}
17625 
17626 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17627 		    NCE_F_NONUD);
17628 		/*
17629 		 * If this is an initial bring-up (or the ipif was never
17630 		 * completely brought up), do DAD.  Otherwise, we're here
17631 		 * because IPMP has rebound an address to this ill: send
17632 		 * unsolicited advertisements (ARP announcements) to
17633 		 * inform others.
17634 		 */
17635 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17636 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17637 		} else {
17638 			state = ND_REACHABLE;
17639 			flags |= NCE_F_UNSOL_ADV;
17640 		}
17641 
17642 retry:
17643 		err = nce_lookup_then_add_v4(ill,
17644 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17645 		    addr, flags, state, &nce);
17646 
17647 		/*
17648 		 * note that we may encounter EEXIST if we are moving
17649 		 * the nce as a result of a rebind operation.
17650 		 */
17651 		switch (err) {
17652 		case 0:
17653 			ipif->ipif_added_nce = 1;
17654 			nce->nce_ipif_cnt++;
17655 			break;
17656 		case EEXIST:
17657 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17658 			    ill->ill_name));
17659 			if (!NCE_MYADDR(nce->nce_common)) {
17660 				/*
17661 				 * A leftover nce from before this address
17662 				 * existed
17663 				 */
17664 				ncec_delete(nce->nce_common);
17665 				nce_refrele(nce);
17666 				nce = NULL;
17667 				goto retry;
17668 			}
17669 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17670 				nce_refrele(nce);
17671 				nce = NULL;
17672 				ip1dbg(("ipif_arp_up: NCE already exists "
17673 				    "for %s:%u\n", ill->ill_name,
17674 				    ipif->ipif_id));
17675 				goto arp_up_done;
17676 			}
17677 			/*
17678 			 * Duplicate local addresses are permissible for
17679 			 * IPIF_POINTOPOINT interfaces which will get marked
17680 			 * IPIF_UNNUMBERED later in
17681 			 * ip_addr_availability_check().
17682 			 *
17683 			 * The nce_ipif_cnt field tracks the number of
17684 			 * ipifs that have nce_addr as their local address.
17685 			 */
17686 			ipif->ipif_addr_ready = 1;
17687 			ipif->ipif_added_nce = 1;
17688 			nce->nce_ipif_cnt++;
17689 			err = 0;
17690 			break;
17691 		default:
17692 			ASSERT(nce == NULL);
17693 			goto arp_up_done;
17694 		}
17695 		if (arp_no_defense) {
17696 			if ((ipif->ipif_flags & IPIF_UP) &&
17697 			    !ipif->ipif_addr_ready)
17698 				ipif_up_notify(ipif);
17699 			ipif->ipif_addr_ready = 1;
17700 		}
17701 	} else {
17702 		/* zero address. nothing to publish */
17703 		ipif->ipif_addr_ready = 1;
17704 	}
17705 	if (nce != NULL)
17706 		nce_refrele(nce);
17707 arp_up_done:
17708 	if (added_ipif && err != 0)
17709 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17710 	return (err);
17711 }
17712 
17713 int
17714 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17715 {
17716 	int 		err = 0;
17717 	ill_t 		*ill = ipif->ipif_ill;
17718 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17719 
17720 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17721 	    ill_t *, ill, ipif_t *, ipif);
17722 
17723 	/*
17724 	 * need to bring up ARP or setup mcast mapping only
17725 	 * when the first interface is coming UP.
17726 	 */
17727 	first_interface = (ill->ill_ipif_up_count == 0 &&
17728 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17729 
17730 	if (res_act == Res_act_initial && first_interface) {
17731 		/*
17732 		 * Send ATTACH + BIND
17733 		 */
17734 		err = arp_ll_up(ill);
17735 		if (err != EINPROGRESS && err != 0)
17736 			return (err);
17737 
17738 		/*
17739 		 * Add NCE for local address. Start DAD.
17740 		 * we'll wait to hear that DAD has finished
17741 		 * before using the interface.
17742 		 */
17743 		if (err == EINPROGRESS)
17744 			wait_for_dlpi = B_TRUE;
17745 	}
17746 
17747 	if (!wait_for_dlpi)
17748 		(void) ipif_arp_up_done_tail(ipif, res_act);
17749 
17750 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17751 }
17752 
17753 /*
17754  * Finish processing of "arp_up" after all the DLPI message
17755  * exchanges have completed between arp and the driver.
17756  */
17757 void
17758 arp_bringup_done(ill_t *ill, int err)
17759 {
17760 	mblk_t	*mp1;
17761 	ipif_t  *ipif;
17762 	conn_t *connp = NULL;
17763 	ipsq_t	*ipsq;
17764 	queue_t *q;
17765 
17766 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
17767 
17768 	ASSERT(IAM_WRITER_ILL(ill));
17769 
17770 	ipsq = ill->ill_phyint->phyint_ipsq;
17771 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17772 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17773 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17774 	if (mp1 == NULL) /* bringup was aborted by the user */
17775 		return;
17776 
17777 	/*
17778 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17779 	 * must have an associated conn_t.  Otherwise, we're bringing this
17780 	 * interface back up as part of handling an asynchronous event (e.g.,
17781 	 * physical address change).
17782 	 */
17783 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17784 		ASSERT(connp != NULL);
17785 		q = CONNP_TO_WQ(connp);
17786 	} else {
17787 		ASSERT(connp == NULL);
17788 		q = ill->ill_rq;
17789 	}
17790 	if (err == 0) {
17791 		if (ipif->ipif_isv6) {
17792 			if ((err = ipif_up_done_v6(ipif)) != 0)
17793 				ip0dbg(("arp_bringup_done: init failed\n"));
17794 		} else {
17795 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
17796 			if (err != 0 || (err = ipif_up_done(ipif)) != 0)
17797 				ip0dbg(("arp_bringup_done: init failed\n"));
17798 		}
17799 	} else {
17800 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
17801 	}
17802 
17803 	if ((err == 0) && (ill->ill_up_ipifs)) {
17804 		err = ill_up_ipifs(ill, q, mp1);
17805 		if (err == EINPROGRESS)
17806 			return;
17807 	}
17808 
17809 	/*
17810 	 * If we have a moved ipif to bring up, and everything has succeeded
17811 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
17812 	 * down -- the admin can try to bring it up by hand if need be.
17813 	 */
17814 	if (ill->ill_move_ipif != NULL) {
17815 		ipif = ill->ill_move_ipif;
17816 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
17817 		    ipif->ipif_ill->ill_name));
17818 		ill->ill_move_ipif = NULL;
17819 		if (err == 0) {
17820 			err = ipif_up(ipif, q, mp1);
17821 			if (err == EINPROGRESS)
17822 				return;
17823 		}
17824 	}
17825 
17826 	/*
17827 	 * The operation must complete without EINPROGRESS since
17828 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
17829 	 * Otherwise, the operation will be stuck forever in the ipsq.
17830 	 */
17831 	ASSERT(err != EINPROGRESS);
17832 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17833 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
17834 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
17835 		    ill_t *, ill, ipif_t *, ipif);
17836 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
17837 	} else {
17838 		ipsq_current_finish(ipsq);
17839 	}
17840 }
17841 
17842 /*
17843  * Finish processing of arp replumb after all the DLPI message
17844  * exchanges have completed between arp and the driver.
17845  */
17846 void
17847 arp_replumb_done(ill_t *ill, int err)
17848 {
17849 	mblk_t	*mp1;
17850 	ipif_t  *ipif;
17851 	conn_t *connp = NULL;
17852 	ipsq_t	*ipsq;
17853 	queue_t *q;
17854 
17855 	ASSERT(IAM_WRITER_ILL(ill));
17856 
17857 	ipsq = ill->ill_phyint->phyint_ipsq;
17858 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17859 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17860 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17861 	if (mp1 == NULL) {
17862 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
17863 		    ipsq->ipsq_xop->ipx_current_ioctl));
17864 		/* bringup was aborted by the user */
17865 		return;
17866 	}
17867 	/*
17868 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17869 	 * must have an associated conn_t.  Otherwise, we're bringing this
17870 	 * interface back up as part of handling an asynchronous event (e.g.,
17871 	 * physical address change).
17872 	 */
17873 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17874 		ASSERT(connp != NULL);
17875 		q = CONNP_TO_WQ(connp);
17876 	} else {
17877 		ASSERT(connp == NULL);
17878 		q = ill->ill_rq;
17879 	}
17880 	if ((err == 0) && (ill->ill_up_ipifs)) {
17881 		err = ill_up_ipifs(ill, q, mp1);
17882 		if (err == EINPROGRESS)
17883 			return;
17884 	}
17885 	/*
17886 	 * The operation must complete without EINPROGRESS since
17887 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
17888 	 * Otherwise, the operation will be stuck forever in the ipsq.
17889 	 */
17890 	ASSERT(err != EINPROGRESS);
17891 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17892 		DTRACE_PROBE4(ipif__ioctl, char *,
17893 		    "arp_replumb_done finish",
17894 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
17895 		    ill_t *, ill, ipif_t *, ipif);
17896 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
17897 	} else {
17898 		ipsq_current_finish(ipsq);
17899 	}
17900 }
17901 
17902 void
17903 ipif_up_notify(ipif_t *ipif)
17904 {
17905 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17906 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
17907 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
17908 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
17909 	    NE_LIF_UP, NULL, 0);
17910 }
17911 
17912 /*
17913  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
17914  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
17915  * TPI end points with STREAMS modules pushed above.  This is assured by not
17916  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
17917  * never ends up on an ipsq, otherwise we may end up processing the ioctl
17918  * while unwinding from the ispq and that could be a thread from the bottom.
17919  */
17920 /* ARGSUSED */
17921 int
17922 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17923     ip_ioctl_cmd_t *ipip, void *arg)
17924 {
17925 	mblk_t *cmd_mp = mp->b_cont->b_cont;
17926 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
17927 	int ret = 0;
17928 	int i;
17929 	size_t size;
17930 	ip_stack_t *ipst;
17931 	zoneid_t zoneid;
17932 	ilb_stack_t *ilbs;
17933 
17934 	ipst = CONNQ_TO_IPST(q);
17935 	ilbs = ipst->ips_netstack->netstack_ilb;
17936 	zoneid = Q_TO_CONN(q)->conn_zoneid;
17937 
17938 	switch (command) {
17939 	case ILB_CREATE_RULE: {
17940 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
17941 
17942 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
17943 			ret = EINVAL;
17944 			break;
17945 		}
17946 
17947 		ret = ilb_rule_add(ilbs, zoneid, cmd);
17948 		break;
17949 	}
17950 	case ILB_DESTROY_RULE:
17951 	case ILB_ENABLE_RULE:
17952 	case ILB_DISABLE_RULE: {
17953 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
17954 
17955 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
17956 			ret = EINVAL;
17957 			break;
17958 		}
17959 
17960 		if (cmd->flags & ILB_RULE_ALLRULES) {
17961 			if (command == ILB_DESTROY_RULE) {
17962 				ilb_rule_del_all(ilbs, zoneid);
17963 				break;
17964 			} else if (command == ILB_ENABLE_RULE) {
17965 				ilb_rule_enable_all(ilbs, zoneid);
17966 				break;
17967 			} else if (command == ILB_DISABLE_RULE) {
17968 				ilb_rule_disable_all(ilbs, zoneid);
17969 				break;
17970 			}
17971 		} else {
17972 			if (command == ILB_DESTROY_RULE) {
17973 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
17974 			} else if (command == ILB_ENABLE_RULE) {
17975 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
17976 				    NULL);
17977 			} else if (command == ILB_DISABLE_RULE) {
17978 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
17979 				    NULL);
17980 			}
17981 		}
17982 		break;
17983 	}
17984 	case ILB_NUM_RULES: {
17985 		ilb_num_rules_cmd_t *cmd;
17986 
17987 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
17988 			ret = EINVAL;
17989 			break;
17990 		}
17991 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
17992 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
17993 		break;
17994 	}
17995 	case ILB_RULE_NAMES: {
17996 		ilb_rule_names_cmd_t *cmd;
17997 
17998 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
17999 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18000 		    cmd->num_names == 0) {
18001 			ret = EINVAL;
18002 			break;
18003 		}
18004 		size = cmd->num_names * ILB_RULE_NAMESZ;
18005 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18006 		    size != cmd_mp->b_wptr) {
18007 			ret = EINVAL;
18008 			break;
18009 		}
18010 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18011 		break;
18012 	}
18013 	case ILB_NUM_SERVERS: {
18014 		ilb_num_servers_cmd_t *cmd;
18015 
18016 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18017 			ret = EINVAL;
18018 			break;
18019 		}
18020 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18021 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18022 		    &(cmd->num));
18023 		break;
18024 	}
18025 	case ILB_LIST_RULE: {
18026 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18027 
18028 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18029 			ret = EINVAL;
18030 			break;
18031 		}
18032 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18033 		break;
18034 	}
18035 	case ILB_LIST_SERVERS: {
18036 		ilb_servers_info_cmd_t *cmd;
18037 
18038 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18039 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18040 		    cmd->num_servers == 0) {
18041 			ret = EINVAL;
18042 			break;
18043 		}
18044 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18045 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18046 		    size != cmd_mp->b_wptr) {
18047 			ret = EINVAL;
18048 			break;
18049 		}
18050 
18051 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18052 		    &cmd->num_servers);
18053 		break;
18054 	}
18055 	case ILB_ADD_SERVERS: {
18056 		ilb_servers_info_cmd_t *cmd;
18057 		ilb_rule_t *rule;
18058 
18059 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18060 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18061 			ret = EINVAL;
18062 			break;
18063 		}
18064 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18065 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18066 		    size != cmd_mp->b_wptr) {
18067 			ret = EINVAL;
18068 			break;
18069 		}
18070 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18071 		if (rule == NULL) {
18072 			ASSERT(ret != 0);
18073 			break;
18074 		}
18075 		for (i = 0; i < cmd->num_servers; i++) {
18076 			ilb_server_info_t *s;
18077 
18078 			s = &cmd->servers[i];
18079 			s->err = ilb_server_add(ilbs, rule, s);
18080 		}
18081 		ILB_RULE_REFRELE(rule);
18082 		break;
18083 	}
18084 	case ILB_DEL_SERVERS:
18085 	case ILB_ENABLE_SERVERS:
18086 	case ILB_DISABLE_SERVERS: {
18087 		ilb_servers_cmd_t *cmd;
18088 		ilb_rule_t *rule;
18089 		int (*f)();
18090 
18091 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18092 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18093 			ret = EINVAL;
18094 			break;
18095 		}
18096 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18097 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18098 		    size != cmd_mp->b_wptr) {
18099 			ret = EINVAL;
18100 			break;
18101 		}
18102 
18103 		if (command == ILB_DEL_SERVERS)
18104 			f = ilb_server_del;
18105 		else if (command == ILB_ENABLE_SERVERS)
18106 			f = ilb_server_enable;
18107 		else if (command == ILB_DISABLE_SERVERS)
18108 			f = ilb_server_disable;
18109 
18110 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18111 		if (rule == NULL) {
18112 			ASSERT(ret != 0);
18113 			break;
18114 		}
18115 
18116 		for (i = 0; i < cmd->num_servers; i++) {
18117 			ilb_server_arg_t *s;
18118 
18119 			s = &cmd->servers[i];
18120 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18121 		}
18122 		ILB_RULE_REFRELE(rule);
18123 		break;
18124 	}
18125 	case ILB_LIST_NAT_TABLE: {
18126 		ilb_list_nat_cmd_t *cmd;
18127 
18128 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18129 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18130 			ret = EINVAL;
18131 			break;
18132 		}
18133 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18134 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18135 		    size != cmd_mp->b_wptr) {
18136 			ret = EINVAL;
18137 			break;
18138 		}
18139 
18140 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18141 		    &cmd->flags);
18142 		break;
18143 	}
18144 	case ILB_LIST_STICKY_TABLE: {
18145 		ilb_list_sticky_cmd_t *cmd;
18146 
18147 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18148 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18149 			ret = EINVAL;
18150 			break;
18151 		}
18152 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18153 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18154 		    size != cmd_mp->b_wptr) {
18155 			ret = EINVAL;
18156 			break;
18157 		}
18158 
18159 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18160 		    &cmd->num_sticky, &cmd->flags);
18161 		break;
18162 	}
18163 	default:
18164 		ret = EINVAL;
18165 		break;
18166 	}
18167 done:
18168 	return (ret);
18169 }
18170 
18171 /* Remove all cache entries for this logical interface */
18172 void
18173 ipif_nce_down(ipif_t *ipif)
18174 {
18175 	ill_t *ill = ipif->ipif_ill;
18176 	nce_t *nce;
18177 
18178 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18179 	    ill_t *, ill, ipif_t *, ipif);
18180 	if (ipif->ipif_added_nce) {
18181 		if (ipif->ipif_isv6)
18182 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18183 		else
18184 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18185 		if (nce != NULL) {
18186 			if (--nce->nce_ipif_cnt == 0)
18187 				ncec_delete(nce->nce_common);
18188 			ipif->ipif_added_nce = 0;
18189 			nce_refrele(nce);
18190 		} else {
18191 			/*
18192 			 * nce may already be NULL because it was already
18193 			 * flushed, e.g., due to a call to nce_flush
18194 			 */
18195 			ipif->ipif_added_nce = 0;
18196 		}
18197 	}
18198 	/*
18199 	 * Make IPMP aware of the deleted data address.
18200 	 */
18201 	if (IS_IPMP(ill))
18202 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18203 
18204 	/*
18205 	 * Remove all other nces dependent on this ill when the last ipif
18206 	 * is going away.
18207 	 */
18208 	if (ill->ill_ipif_up_count == 0) {
18209 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18210 		    (uchar_t *)ill, ill->ill_ipst);
18211 		if (IS_UNDER_IPMP(ill))
18212 			nce_flush(ill, B_TRUE);
18213 	}
18214 }
18215