xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision edf0039d316aafc2c1128aca35079119ecf024f1)
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 	clock_t now;
2430 
2431 	/*
2432 	 * If we are here within ip_min_frag_prune_time msecs remove
2433 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2434 	 * ill_frag_free_num_pkts.
2435 	 */
2436 	mutex_enter(&ill->ill_lock);
2437 	now = ddi_get_lbolt();
2438 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2439 	    (ip_min_frag_prune_time != 0 ?
2440 	    ip_min_frag_prune_time : msec_per_tick)) {
2441 
2442 		ill->ill_frag_free_num_pkts++;
2443 
2444 	} else {
2445 		ill->ill_frag_free_num_pkts = 0;
2446 	}
2447 	ill->ill_last_frag_clean_time = now;
2448 	mutex_exit(&ill->ill_lock);
2449 
2450 	/*
2451 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2452 	 */
2453 	if (ill->ill_frag_free_num_pkts != 0) {
2454 		int ix;
2455 
2456 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2457 			ipfb = &ill->ill_frag_hash_tbl[ix];
2458 			mutex_enter(&ipfb->ipfb_lock);
2459 			if (ipfb->ipfb_ipf != NULL) {
2460 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2461 				    ill->ill_frag_free_num_pkts);
2462 			}
2463 			mutex_exit(&ipfb->ipfb_lock);
2464 		}
2465 	}
2466 	/*
2467 	 * While the reassembly list for this ILL is too big, prune a fragment
2468 	 * queue by age, oldest first.
2469 	 */
2470 	while (ill->ill_frag_count > max_count) {
2471 		int	ix;
2472 		ipfb_t	*oipfb = NULL;
2473 		uint_t	oldest = UINT_MAX;
2474 
2475 		count = 0;
2476 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2477 			ipfb = &ill->ill_frag_hash_tbl[ix];
2478 			mutex_enter(&ipfb->ipfb_lock);
2479 			ipf = ipfb->ipfb_ipf;
2480 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2481 				oldest = ipf->ipf_gen;
2482 				oipfb = ipfb;
2483 			}
2484 			count += ipfb->ipfb_count;
2485 			mutex_exit(&ipfb->ipfb_lock);
2486 		}
2487 		if (oipfb == NULL)
2488 			break;
2489 
2490 		if (count <= max_count)
2491 			return;	/* Somebody beat us to it, nothing to do */
2492 		mutex_enter(&oipfb->ipfb_lock);
2493 		ipf = oipfb->ipfb_ipf;
2494 		if (ipf != NULL) {
2495 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2496 		}
2497 		mutex_exit(&oipfb->ipfb_lock);
2498 	}
2499 }
2500 
2501 /*
2502  * free 'free_cnt' fragmented packets starting at ipf.
2503  */
2504 void
2505 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2506 {
2507 	size_t	count;
2508 	mblk_t	*mp;
2509 	mblk_t	*tmp;
2510 	ipf_t **ipfp = ipf->ipf_ptphn;
2511 
2512 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2513 	ASSERT(ipfp != NULL);
2514 	ASSERT(ipf != NULL);
2515 
2516 	while (ipf != NULL && free_cnt-- > 0) {
2517 		count = ipf->ipf_count;
2518 		mp = ipf->ipf_mp;
2519 		ipf = ipf->ipf_hash_next;
2520 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2521 			IP_REASS_SET_START(tmp, 0);
2522 			IP_REASS_SET_END(tmp, 0);
2523 		}
2524 		atomic_add_32(&ill->ill_frag_count, -count);
2525 		ASSERT(ipfb->ipfb_count >= count);
2526 		ipfb->ipfb_count -= count;
2527 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2528 		ipfb->ipfb_frag_pkts--;
2529 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2530 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2531 		freemsg(mp);
2532 	}
2533 
2534 	if (ipf)
2535 		ipf->ipf_ptphn = ipfp;
2536 	ipfp[0] = ipf;
2537 }
2538 
2539 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2540 	"obsolete and may be removed in a future release of Solaris.  Use " \
2541 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2542 
2543 /*
2544  * For obsolete per-interface forwarding configuration;
2545  * called in response to ND_GET.
2546  */
2547 /* ARGSUSED */
2548 static int
2549 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2550 {
2551 	ill_t *ill = (ill_t *)cp;
2552 
2553 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2554 
2555 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2556 	return (0);
2557 }
2558 
2559 /*
2560  * For obsolete per-interface forwarding configuration;
2561  * called in response to ND_SET.
2562  */
2563 /* ARGSUSED */
2564 static int
2565 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2566     cred_t *ioc_cr)
2567 {
2568 	long value;
2569 	int retval;
2570 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2571 
2572 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2573 
2574 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2575 	    value < 0 || value > 1) {
2576 		return (EINVAL);
2577 	}
2578 
2579 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2580 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2581 	rw_exit(&ipst->ips_ill_g_lock);
2582 	return (retval);
2583 }
2584 
2585 /*
2586  * Helper function for ill_forward_set().
2587  */
2588 static void
2589 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2590 {
2591 	ip_stack_t	*ipst = ill->ill_ipst;
2592 
2593 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2594 
2595 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2596 	    (enable ? "Enabling" : "Disabling"),
2597 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2598 	mutex_enter(&ill->ill_lock);
2599 	if (enable)
2600 		ill->ill_flags |= ILLF_ROUTER;
2601 	else
2602 		ill->ill_flags &= ~ILLF_ROUTER;
2603 	mutex_exit(&ill->ill_lock);
2604 	if (ill->ill_isv6)
2605 		ill_set_nce_router_flags(ill, enable);
2606 	/* Notify routing socket listeners of this change. */
2607 	if (ill->ill_ipif != NULL)
2608 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2609 }
2610 
2611 /*
2612  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2613  * socket messages for each interface whose flags we change.
2614  */
2615 int
2616 ill_forward_set(ill_t *ill, boolean_t enable)
2617 {
2618 	ipmp_illgrp_t *illg;
2619 	ip_stack_t *ipst = ill->ill_ipst;
2620 
2621 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2622 
2623 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2624 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2625 		return (0);
2626 
2627 	if (IS_LOOPBACK(ill))
2628 		return (EINVAL);
2629 
2630 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2631 		/*
2632 		 * Update all of the interfaces in the group.
2633 		 */
2634 		illg = ill->ill_grp;
2635 		ill = list_head(&illg->ig_if);
2636 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2637 			ill_forward_set_on_ill(ill, enable);
2638 
2639 		/*
2640 		 * Update the IPMP meta-interface.
2641 		 */
2642 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2643 		return (0);
2644 	}
2645 
2646 	ill_forward_set_on_ill(ill, enable);
2647 	return (0);
2648 }
2649 
2650 /*
2651  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2652  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2653  * set or clear.
2654  */
2655 static void
2656 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2657 {
2658 	ipif_t *ipif;
2659 	ncec_t *ncec;
2660 	nce_t *nce;
2661 
2662 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2663 		/*
2664 		 * NOTE: we match across the illgrp because nce's for
2665 		 * addresses on IPMP interfaces have an nce_ill that points to
2666 		 * the bound underlying ill.
2667 		 */
2668 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2669 		if (nce != NULL) {
2670 			ncec = nce->nce_common;
2671 			mutex_enter(&ncec->ncec_lock);
2672 			if (enable)
2673 				ncec->ncec_flags |= NCE_F_ISROUTER;
2674 			else
2675 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2676 			mutex_exit(&ncec->ncec_lock);
2677 			nce_refrele(nce);
2678 		}
2679 	}
2680 }
2681 
2682 /*
2683  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2684  * for this ill.  Make sure the v6/v4 question has been answered about this
2685  * ill.  The creation of this ndd variable is only for backwards compatibility.
2686  * The preferred way to control per-interface IP forwarding is through the
2687  * ILLF_ROUTER interface flag.
2688  */
2689 static int
2690 ill_set_ndd_name(ill_t *ill)
2691 {
2692 	char *suffix;
2693 	ip_stack_t	*ipst = ill->ill_ipst;
2694 
2695 	ASSERT(IAM_WRITER_ILL(ill));
2696 
2697 	if (ill->ill_isv6)
2698 		suffix = ipv6_forward_suffix;
2699 	else
2700 		suffix = ipv4_forward_suffix;
2701 
2702 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2703 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2704 	/*
2705 	 * Copies over the '\0'.
2706 	 * Note that strlen(suffix) is always bounded.
2707 	 */
2708 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2709 	    strlen(suffix) + 1);
2710 
2711 	/*
2712 	 * Use of the nd table requires holding the reader lock.
2713 	 * Modifying the nd table thru nd_load/nd_unload requires
2714 	 * the writer lock.
2715 	 */
2716 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2717 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2718 	    nd_ill_forward_set, (caddr_t)ill)) {
2719 		/*
2720 		 * If the nd_load failed, it only meant that it could not
2721 		 * allocate a new bunch of room for further NDD expansion.
2722 		 * Because of that, the ill_ndd_name will be set to 0, and
2723 		 * this interface is at the mercy of the global ip_forwarding
2724 		 * variable.
2725 		 */
2726 		rw_exit(&ipst->ips_ip_g_nd_lock);
2727 		ill->ill_ndd_name = NULL;
2728 		return (ENOMEM);
2729 	}
2730 	rw_exit(&ipst->ips_ip_g_nd_lock);
2731 	return (0);
2732 }
2733 
2734 /*
2735  * Intializes the context structure and returns the first ill in the list
2736  * cuurently start_list and end_list can have values:
2737  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2738  * IP_V4_G_HEAD		Traverse IPV4 list only.
2739  * IP_V6_G_HEAD		Traverse IPV6 list only.
2740  */
2741 
2742 /*
2743  * We don't check for CONDEMNED ills here. Caller must do that if
2744  * necessary under the ill lock.
2745  */
2746 ill_t *
2747 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2748     ip_stack_t *ipst)
2749 {
2750 	ill_if_t *ifp;
2751 	ill_t *ill;
2752 	avl_tree_t *avl_tree;
2753 
2754 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2755 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2756 
2757 	/*
2758 	 * setup the lists to search
2759 	 */
2760 	if (end_list != MAX_G_HEADS) {
2761 		ctx->ctx_current_list = start_list;
2762 		ctx->ctx_last_list = end_list;
2763 	} else {
2764 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2765 		ctx->ctx_current_list = 0;
2766 	}
2767 
2768 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2769 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2770 		if (ifp != (ill_if_t *)
2771 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2772 			avl_tree = &ifp->illif_avl_by_ppa;
2773 			ill = avl_first(avl_tree);
2774 			/*
2775 			 * ill is guaranteed to be non NULL or ifp should have
2776 			 * not existed.
2777 			 */
2778 			ASSERT(ill != NULL);
2779 			return (ill);
2780 		}
2781 		ctx->ctx_current_list++;
2782 	}
2783 
2784 	return (NULL);
2785 }
2786 
2787 /*
2788  * returns the next ill in the list. ill_first() must have been called
2789  * before calling ill_next() or bad things will happen.
2790  */
2791 
2792 /*
2793  * We don't check for CONDEMNED ills here. Caller must do that if
2794  * necessary under the ill lock.
2795  */
2796 ill_t *
2797 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2798 {
2799 	ill_if_t *ifp;
2800 	ill_t *ill;
2801 	ip_stack_t	*ipst = lastill->ill_ipst;
2802 
2803 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2804 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2805 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2806 	    AVL_AFTER)) != NULL) {
2807 		return (ill);
2808 	}
2809 
2810 	/* goto next ill_ifp in the list. */
2811 	ifp = lastill->ill_ifptr->illif_next;
2812 
2813 	/* make sure not at end of circular list */
2814 	while (ifp ==
2815 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2816 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2817 			return (NULL);
2818 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2819 	}
2820 
2821 	return (avl_first(&ifp->illif_avl_by_ppa));
2822 }
2823 
2824 /*
2825  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2826  * The final number (PPA) must not have any leading zeros.  Upon success, a
2827  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2828  */
2829 static char *
2830 ill_get_ppa_ptr(char *name)
2831 {
2832 	int namelen = strlen(name);
2833 	int end_ndx = namelen - 1;
2834 	int ppa_ndx, i;
2835 
2836 	/*
2837 	 * Check that the first character is [a-zA-Z], and that the last
2838 	 * character is [0-9].
2839 	 */
2840 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2841 		return (NULL);
2842 
2843 	/*
2844 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2845 	 */
2846 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2847 		if (!isdigit(name[ppa_ndx - 1]))
2848 			break;
2849 
2850 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2851 		return (NULL);
2852 
2853 	/*
2854 	 * Check that the intermediate characters are [a-z0-9.]
2855 	 */
2856 	for (i = 1; i < ppa_ndx; i++) {
2857 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2858 		    name[i] != '.' && name[i] != '_') {
2859 			return (NULL);
2860 		}
2861 	}
2862 
2863 	return (name + ppa_ndx);
2864 }
2865 
2866 /*
2867  * use avl tree to locate the ill.
2868  */
2869 static ill_t *
2870 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2871 {
2872 	char *ppa_ptr = NULL;
2873 	int len;
2874 	uint_t ppa;
2875 	ill_t *ill = NULL;
2876 	ill_if_t *ifp;
2877 	int list;
2878 
2879 	/*
2880 	 * get ppa ptr
2881 	 */
2882 	if (isv6)
2883 		list = IP_V6_G_HEAD;
2884 	else
2885 		list = IP_V4_G_HEAD;
2886 
2887 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2888 		return (NULL);
2889 	}
2890 
2891 	len = ppa_ptr - name + 1;
2892 
2893 	ppa = stoi(&ppa_ptr);
2894 
2895 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2896 
2897 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2898 		/*
2899 		 * match is done on len - 1 as the name is not null
2900 		 * terminated it contains ppa in addition to the interface
2901 		 * name.
2902 		 */
2903 		if ((ifp->illif_name_len == len) &&
2904 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2905 			break;
2906 		} else {
2907 			ifp = ifp->illif_next;
2908 		}
2909 	}
2910 
2911 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2912 		/*
2913 		 * Even the interface type does not exist.
2914 		 */
2915 		return (NULL);
2916 	}
2917 
2918 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2919 	if (ill != NULL) {
2920 		mutex_enter(&ill->ill_lock);
2921 		if (ILL_CAN_LOOKUP(ill)) {
2922 			ill_refhold_locked(ill);
2923 			mutex_exit(&ill->ill_lock);
2924 			return (ill);
2925 		}
2926 		mutex_exit(&ill->ill_lock);
2927 	}
2928 	return (NULL);
2929 }
2930 
2931 /*
2932  * comparison function for use with avl.
2933  */
2934 static int
2935 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2936 {
2937 	uint_t ppa;
2938 	uint_t ill_ppa;
2939 
2940 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2941 
2942 	ppa = *((uint_t *)ppa_ptr);
2943 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2944 	/*
2945 	 * We want the ill with the lowest ppa to be on the
2946 	 * top.
2947 	 */
2948 	if (ill_ppa < ppa)
2949 		return (1);
2950 	if (ill_ppa > ppa)
2951 		return (-1);
2952 	return (0);
2953 }
2954 
2955 /*
2956  * remove an interface type from the global list.
2957  */
2958 static void
2959 ill_delete_interface_type(ill_if_t *interface)
2960 {
2961 	ASSERT(interface != NULL);
2962 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2963 
2964 	avl_destroy(&interface->illif_avl_by_ppa);
2965 	if (interface->illif_ppa_arena != NULL)
2966 		vmem_destroy(interface->illif_ppa_arena);
2967 
2968 	remque(interface);
2969 
2970 	mi_free(interface);
2971 }
2972 
2973 /*
2974  * remove ill from the global list.
2975  */
2976 static void
2977 ill_glist_delete(ill_t *ill)
2978 {
2979 	ip_stack_t	*ipst;
2980 	phyint_t	*phyi;
2981 
2982 	if (ill == NULL)
2983 		return;
2984 	ipst = ill->ill_ipst;
2985 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2986 
2987 	/*
2988 	 * If the ill was never inserted into the AVL tree
2989 	 * we skip the if branch.
2990 	 */
2991 	if (ill->ill_ifptr != NULL) {
2992 		/*
2993 		 * remove from AVL tree and free ppa number
2994 		 */
2995 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
2996 
2997 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
2998 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
2999 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3000 		}
3001 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3002 			ill_delete_interface_type(ill->ill_ifptr);
3003 		}
3004 
3005 		/*
3006 		 * Indicate ill is no longer in the list.
3007 		 */
3008 		ill->ill_ifptr = NULL;
3009 		ill->ill_name_length = 0;
3010 		ill->ill_name[0] = '\0';
3011 		ill->ill_ppa = UINT_MAX;
3012 	}
3013 
3014 	/* Generate one last event for this ill. */
3015 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3016 	    ill->ill_name_length);
3017 
3018 	ASSERT(ill->ill_phyint != NULL);
3019 	phyi = ill->ill_phyint;
3020 	ill->ill_phyint = NULL;
3021 
3022 	/*
3023 	 * ill_init allocates a phyint always to store the copy
3024 	 * of flags relevant to phyint. At that point in time, we could
3025 	 * not assign the name and hence phyint_illv4/v6 could not be
3026 	 * initialized. Later in ipif_set_values, we assign the name to
3027 	 * the ill, at which point in time we assign phyint_illv4/v6.
3028 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3029 	 */
3030 	if (ill->ill_flags & ILLF_IPV6)
3031 		phyi->phyint_illv6 = NULL;
3032 	else
3033 		phyi->phyint_illv4 = NULL;
3034 
3035 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3036 		rw_exit(&ipst->ips_ill_g_lock);
3037 		return;
3038 	}
3039 
3040 	/*
3041 	 * There are no ills left on this phyint; pull it out of the phyint
3042 	 * avl trees, and free it.
3043 	 */
3044 	if (phyi->phyint_ifindex > 0) {
3045 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3046 		    phyi);
3047 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3048 		    phyi);
3049 	}
3050 	rw_exit(&ipst->ips_ill_g_lock);
3051 
3052 	phyint_free(phyi);
3053 }
3054 
3055 /*
3056  * allocate a ppa, if the number of plumbed interfaces of this type are
3057  * less than ill_no_arena do a linear search to find a unused ppa.
3058  * When the number goes beyond ill_no_arena switch to using an arena.
3059  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3060  * is the return value for an error condition, so allocation starts at one
3061  * and is decremented by one.
3062  */
3063 static int
3064 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3065 {
3066 	ill_t *tmp_ill;
3067 	uint_t start, end;
3068 	int ppa;
3069 
3070 	if (ifp->illif_ppa_arena == NULL &&
3071 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3072 		/*
3073 		 * Create an arena.
3074 		 */
3075 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3076 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3077 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3078 			/* allocate what has already been assigned */
3079 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3080 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3081 		    tmp_ill, AVL_AFTER)) {
3082 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3083 			    1,		/* size */
3084 			    1,		/* align/quantum */
3085 			    0,		/* phase */
3086 			    0,		/* nocross */
3087 			    /* minaddr */
3088 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3089 			    /* maxaddr */
3090 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3091 			    VM_NOSLEEP|VM_FIRSTFIT);
3092 			if (ppa == 0) {
3093 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3094 				    " failed while switching"));
3095 				vmem_destroy(ifp->illif_ppa_arena);
3096 				ifp->illif_ppa_arena = NULL;
3097 				break;
3098 			}
3099 		}
3100 	}
3101 
3102 	if (ifp->illif_ppa_arena != NULL) {
3103 		if (ill->ill_ppa == UINT_MAX) {
3104 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3105 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3106 			if (ppa == 0)
3107 				return (EAGAIN);
3108 			ill->ill_ppa = --ppa;
3109 		} else {
3110 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3111 			    1, 		/* size */
3112 			    1, 		/* align/quantum */
3113 			    0, 		/* phase */
3114 			    0, 		/* nocross */
3115 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3116 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3117 			    VM_NOSLEEP|VM_FIRSTFIT);
3118 			/*
3119 			 * Most likely the allocation failed because
3120 			 * the requested ppa was in use.
3121 			 */
3122 			if (ppa == 0)
3123 				return (EEXIST);
3124 		}
3125 		return (0);
3126 	}
3127 
3128 	/*
3129 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3130 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3131 	 */
3132 	if (ill->ill_ppa == UINT_MAX) {
3133 		end = UINT_MAX - 1;
3134 		start = 0;
3135 	} else {
3136 		end = start = ill->ill_ppa;
3137 	}
3138 
3139 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3140 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3141 		if (start++ >= end) {
3142 			if (ill->ill_ppa == UINT_MAX)
3143 				return (EAGAIN);
3144 			else
3145 				return (EEXIST);
3146 		}
3147 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3148 	}
3149 	ill->ill_ppa = start;
3150 	return (0);
3151 }
3152 
3153 /*
3154  * Insert ill into the list of configured ill's. Once this function completes,
3155  * the ill is globally visible and is available through lookups. More precisely
3156  * this happens after the caller drops the ill_g_lock.
3157  */
3158 static int
3159 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3160 {
3161 	ill_if_t *ill_interface;
3162 	avl_index_t where = 0;
3163 	int error;
3164 	int name_length;
3165 	int index;
3166 	boolean_t check_length = B_FALSE;
3167 	ip_stack_t	*ipst = ill->ill_ipst;
3168 
3169 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3170 
3171 	name_length = mi_strlen(name) + 1;
3172 
3173 	if (isv6)
3174 		index = IP_V6_G_HEAD;
3175 	else
3176 		index = IP_V4_G_HEAD;
3177 
3178 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3179 	/*
3180 	 * Search for interface type based on name
3181 	 */
3182 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3183 		if ((ill_interface->illif_name_len == name_length) &&
3184 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3185 			break;
3186 		}
3187 		ill_interface = ill_interface->illif_next;
3188 	}
3189 
3190 	/*
3191 	 * Interface type not found, create one.
3192 	 */
3193 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3194 		ill_g_head_t ghead;
3195 
3196 		/*
3197 		 * allocate ill_if_t structure
3198 		 */
3199 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3200 		if (ill_interface == NULL) {
3201 			return (ENOMEM);
3202 		}
3203 
3204 		(void) strcpy(ill_interface->illif_name, name);
3205 		ill_interface->illif_name_len = name_length;
3206 
3207 		avl_create(&ill_interface->illif_avl_by_ppa,
3208 		    ill_compare_ppa, sizeof (ill_t),
3209 		    offsetof(struct ill_s, ill_avl_byppa));
3210 
3211 		/*
3212 		 * link the structure in the back to maintain order
3213 		 * of configuration for ifconfig output.
3214 		 */
3215 		ghead = ipst->ips_ill_g_heads[index];
3216 		insque(ill_interface, ghead.ill_g_list_tail);
3217 	}
3218 
3219 	if (ill->ill_ppa == UINT_MAX)
3220 		check_length = B_TRUE;
3221 
3222 	error = ill_alloc_ppa(ill_interface, ill);
3223 	if (error != 0) {
3224 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3225 			ill_delete_interface_type(ill->ill_ifptr);
3226 		return (error);
3227 	}
3228 
3229 	/*
3230 	 * When the ppa is choosen by the system, check that there is
3231 	 * enough space to insert ppa. if a specific ppa was passed in this
3232 	 * check is not required as the interface name passed in will have
3233 	 * the right ppa in it.
3234 	 */
3235 	if (check_length) {
3236 		/*
3237 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3238 		 */
3239 		char buf[sizeof (uint_t) * 3];
3240 
3241 		/*
3242 		 * convert ppa to string to calculate the amount of space
3243 		 * required for it in the name.
3244 		 */
3245 		numtos(ill->ill_ppa, buf);
3246 
3247 		/* Do we have enough space to insert ppa ? */
3248 
3249 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3250 			/* Free ppa and interface type struct */
3251 			if (ill_interface->illif_ppa_arena != NULL) {
3252 				vmem_free(ill_interface->illif_ppa_arena,
3253 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3254 			}
3255 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3256 				ill_delete_interface_type(ill->ill_ifptr);
3257 
3258 			return (EINVAL);
3259 		}
3260 	}
3261 
3262 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3263 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3264 
3265 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3266 	    &where);
3267 	ill->ill_ifptr = ill_interface;
3268 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3269 
3270 	ill_phyint_reinit(ill);
3271 	return (0);
3272 }
3273 
3274 /* Initialize the per phyint ipsq used for serialization */
3275 static boolean_t
3276 ipsq_init(ill_t *ill, boolean_t enter)
3277 {
3278 	ipsq_t  *ipsq;
3279 	ipxop_t	*ipx;
3280 
3281 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3282 		return (B_FALSE);
3283 
3284 	ill->ill_phyint->phyint_ipsq = ipsq;
3285 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3286 	ipx->ipx_ipsq = ipsq;
3287 	ipsq->ipsq_next = ipsq;
3288 	ipsq->ipsq_phyint = ill->ill_phyint;
3289 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3290 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3291 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3292 	if (enter) {
3293 		ipx->ipx_writer = curthread;
3294 		ipx->ipx_forced = B_FALSE;
3295 		ipx->ipx_reentry_cnt = 1;
3296 #ifdef DEBUG
3297 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3298 #endif
3299 	}
3300 	return (B_TRUE);
3301 }
3302 
3303 /*
3304  * ill_init is called by ip_open when a device control stream is opened.
3305  * It does a few initializations, and shoots a DL_INFO_REQ message down
3306  * to the driver.  The response is later picked up in ip_rput_dlpi and
3307  * used to set up default mechanisms for talking to the driver.  (Always
3308  * called as writer.)
3309  *
3310  * If this function returns error, ip_open will call ip_close which in
3311  * turn will call ill_delete to clean up any memory allocated here that
3312  * is not yet freed.
3313  */
3314 int
3315 ill_init(queue_t *q, ill_t *ill)
3316 {
3317 	int	count;
3318 	dl_info_req_t	*dlir;
3319 	mblk_t	*info_mp;
3320 	uchar_t *frag_ptr;
3321 
3322 	/*
3323 	 * The ill is initialized to zero by mi_alloc*(). In addition
3324 	 * some fields already contain valid values, initialized in
3325 	 * ip_open(), before we reach here.
3326 	 */
3327 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3328 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3329 	ill->ill_saved_ire_cnt = 0;
3330 
3331 	ill->ill_rq = q;
3332 	ill->ill_wq = WR(q);
3333 
3334 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3335 	    BPRI_HI);
3336 	if (info_mp == NULL)
3337 		return (ENOMEM);
3338 
3339 	/*
3340 	 * Allocate sufficient space to contain our fragment hash table and
3341 	 * the device name.
3342 	 */
3343 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3344 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
3345 	if (frag_ptr == NULL) {
3346 		freemsg(info_mp);
3347 		return (ENOMEM);
3348 	}
3349 	ill->ill_frag_ptr = frag_ptr;
3350 	ill->ill_frag_free_num_pkts = 0;
3351 	ill->ill_last_frag_clean_time = 0;
3352 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3353 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3354 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3355 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3356 		    NULL, MUTEX_DEFAULT, NULL);
3357 	}
3358 
3359 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3360 	if (ill->ill_phyint == NULL) {
3361 		freemsg(info_mp);
3362 		mi_free(frag_ptr);
3363 		return (ENOMEM);
3364 	}
3365 
3366 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3367 	/*
3368 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3369 	 * at this point because of the following reason. If we can't
3370 	 * enter the ipsq at some point and cv_wait, the writer that
3371 	 * wakes us up tries to locate us using the list of all phyints
3372 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3373 	 * If we don't set it now, we risk a missed wakeup.
3374 	 */
3375 	ill->ill_phyint->phyint_illv4 = ill;
3376 	ill->ill_ppa = UINT_MAX;
3377 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3378 
3379 	ill_set_inputfn(ill);
3380 
3381 	if (!ipsq_init(ill, B_TRUE)) {
3382 		freemsg(info_mp);
3383 		mi_free(frag_ptr);
3384 		mi_free(ill->ill_phyint);
3385 		return (ENOMEM);
3386 	}
3387 
3388 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3389 
3390 	/* Frag queue limit stuff */
3391 	ill->ill_frag_count = 0;
3392 	ill->ill_ipf_gen = 0;
3393 
3394 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3395 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3396 	ill->ill_global_timer = INFINITY;
3397 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3398 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3399 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3400 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3401 
3402 	/*
3403 	 * Initialize IPv6 configuration variables.  The IP module is always
3404 	 * opened as an IPv4 module.  Instead tracking down the cases where
3405 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3406 	 * here for convenience, this has no effect until the ill is set to do
3407 	 * IPv6.
3408 	 */
3409 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3410 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3411 	ill->ill_max_buf = ND_MAX_Q;
3412 	ill->ill_refcnt = 0;
3413 
3414 	/* Send down the Info Request to the driver. */
3415 	info_mp->b_datap->db_type = M_PCPROTO;
3416 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3417 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3418 	dlir->dl_primitive = DL_INFO_REQ;
3419 
3420 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3421 
3422 	qprocson(q);
3423 	ill_dlpi_send(ill, info_mp);
3424 
3425 	return (0);
3426 }
3427 
3428 /*
3429  * ill_dls_info
3430  * creates datalink socket info from the device.
3431  */
3432 int
3433 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3434 {
3435 	size_t	len;
3436 
3437 	sdl->sdl_family = AF_LINK;
3438 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3439 	sdl->sdl_type = ill->ill_type;
3440 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3441 	len = strlen(sdl->sdl_data);
3442 	ASSERT(len < 256);
3443 	sdl->sdl_nlen = (uchar_t)len;
3444 	sdl->sdl_alen = ill->ill_phys_addr_length;
3445 	sdl->sdl_slen = 0;
3446 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3447 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3448 
3449 	return (sizeof (struct sockaddr_dl));
3450 }
3451 
3452 /*
3453  * ill_xarp_info
3454  * creates xarp info from the device.
3455  */
3456 static int
3457 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3458 {
3459 	sdl->sdl_family = AF_LINK;
3460 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3461 	sdl->sdl_type = ill->ill_type;
3462 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3463 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3464 	sdl->sdl_alen = ill->ill_phys_addr_length;
3465 	sdl->sdl_slen = 0;
3466 	return (sdl->sdl_nlen);
3467 }
3468 
3469 static int
3470 loopback_kstat_update(kstat_t *ksp, int rw)
3471 {
3472 	kstat_named_t *kn;
3473 	netstackid_t	stackid;
3474 	netstack_t	*ns;
3475 	ip_stack_t	*ipst;
3476 
3477 	if (ksp == NULL || ksp->ks_data == NULL)
3478 		return (EIO);
3479 
3480 	if (rw == KSTAT_WRITE)
3481 		return (EACCES);
3482 
3483 	kn = KSTAT_NAMED_PTR(ksp);
3484 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3485 
3486 	ns = netstack_find_by_stackid(stackid);
3487 	if (ns == NULL)
3488 		return (-1);
3489 
3490 	ipst = ns->netstack_ip;
3491 	if (ipst == NULL) {
3492 		netstack_rele(ns);
3493 		return (-1);
3494 	}
3495 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3496 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3497 	netstack_rele(ns);
3498 	return (0);
3499 }
3500 
3501 /*
3502  * Has ifindex been plumbed already?
3503  */
3504 static boolean_t
3505 phyint_exists(uint_t index, ip_stack_t *ipst)
3506 {
3507 	ASSERT(index != 0);
3508 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3509 
3510 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3511 	    &index, NULL) != NULL);
3512 }
3513 
3514 /* Pick a unique ifindex */
3515 boolean_t
3516 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3517 {
3518 	uint_t starting_index;
3519 
3520 	if (!ipst->ips_ill_index_wrap) {
3521 		*indexp = ipst->ips_ill_index++;
3522 		if (ipst->ips_ill_index == 0) {
3523 			/* Reached the uint_t limit Next time wrap  */
3524 			ipst->ips_ill_index_wrap = B_TRUE;
3525 		}
3526 		return (B_TRUE);
3527 	}
3528 
3529 	/*
3530 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3531 	 * at this point and don't want to call any function that attempts
3532 	 * to get the lock again.
3533 	 */
3534 	starting_index = ipst->ips_ill_index++;
3535 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3536 		if (ipst->ips_ill_index != 0 &&
3537 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3538 			/* found unused index - use it */
3539 			*indexp = ipst->ips_ill_index;
3540 			return (B_TRUE);
3541 		}
3542 	}
3543 
3544 	/*
3545 	 * all interface indicies are inuse.
3546 	 */
3547 	return (B_FALSE);
3548 }
3549 
3550 /*
3551  * Assign a unique interface index for the phyint.
3552  */
3553 static boolean_t
3554 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3555 {
3556 	ASSERT(phyi->phyint_ifindex == 0);
3557 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3558 }
3559 
3560 /*
3561  * Initialize the flags on `phyi' as per the provided mactype.
3562  */
3563 static void
3564 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3565 {
3566 	uint64_t flags = 0;
3567 
3568 	/*
3569 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3570 	 * we always presume the underlying hardware is working and set
3571 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3572 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3573 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3574 	 */
3575 	if (mactype == SUNW_DL_IPMP)
3576 		flags |= PHYI_FAILED;
3577 	else
3578 		flags |= PHYI_RUNNING;
3579 
3580 	switch (mactype) {
3581 	case SUNW_DL_VNI:
3582 		flags |= PHYI_VIRTUAL;
3583 		break;
3584 	case SUNW_DL_IPMP:
3585 		flags |= PHYI_IPMP;
3586 		break;
3587 	case DL_LOOP:
3588 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3589 		break;
3590 	}
3591 
3592 	mutex_enter(&phyi->phyint_lock);
3593 	phyi->phyint_flags |= flags;
3594 	mutex_exit(&phyi->phyint_lock);
3595 }
3596 
3597 /*
3598  * Return a pointer to the ill which matches the supplied name.  Note that
3599  * the ill name length includes the null termination character.  (May be
3600  * called as writer.)
3601  * If do_alloc and the interface is "lo0" it will be automatically created.
3602  * Cannot bump up reference on condemned ills. So dup detect can't be done
3603  * using this func.
3604  */
3605 ill_t *
3606 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3607     boolean_t *did_alloc, ip_stack_t *ipst)
3608 {
3609 	ill_t	*ill;
3610 	ipif_t	*ipif;
3611 	ipsq_t	*ipsq;
3612 	kstat_named_t	*kn;
3613 	boolean_t isloopback;
3614 	in6_addr_t ov6addr;
3615 
3616 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3617 
3618 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3619 	ill = ill_find_by_name(name, isv6, ipst);
3620 	rw_exit(&ipst->ips_ill_g_lock);
3621 	if (ill != NULL)
3622 		return (ill);
3623 
3624 	/*
3625 	 * Couldn't find it.  Does this happen to be a lookup for the
3626 	 * loopback device and are we allowed to allocate it?
3627 	 */
3628 	if (!isloopback || !do_alloc)
3629 		return (NULL);
3630 
3631 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3632 	ill = ill_find_by_name(name, isv6, ipst);
3633 	if (ill != NULL) {
3634 		rw_exit(&ipst->ips_ill_g_lock);
3635 		return (ill);
3636 	}
3637 
3638 	/* Create the loopback device on demand */
3639 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3640 	    sizeof (ipif_loopback_name), BPRI_MED));
3641 	if (ill == NULL)
3642 		goto done;
3643 
3644 	*ill = ill_null;
3645 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3646 	ill->ill_ipst = ipst;
3647 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3648 	netstack_hold(ipst->ips_netstack);
3649 	/*
3650 	 * For exclusive stacks we set the zoneid to zero
3651 	 * to make IP operate as if in the global zone.
3652 	 */
3653 	ill->ill_zoneid = GLOBAL_ZONEID;
3654 
3655 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3656 	if (ill->ill_phyint == NULL)
3657 		goto done;
3658 
3659 	if (isv6)
3660 		ill->ill_phyint->phyint_illv6 = ill;
3661 	else
3662 		ill->ill_phyint->phyint_illv4 = ill;
3663 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3664 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3665 
3666 	if (isv6) {
3667 		ill->ill_isv6 = B_TRUE;
3668 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3669 	} else {
3670 		ill->ill_max_frag = ip_loopback_mtuplus;
3671 	}
3672 	if (!ill_allocate_mibs(ill))
3673 		goto done;
3674 	ill->ill_current_frag = ill->ill_max_frag;
3675 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3676 	/*
3677 	 * ipif_loopback_name can't be pointed at directly because its used
3678 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3679 	 * from the glist, ill_glist_delete() sets the first character of
3680 	 * ill_name to '\0'.
3681 	 */
3682 	ill->ill_name = (char *)ill + sizeof (*ill);
3683 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3684 	ill->ill_name_length = sizeof (ipif_loopback_name);
3685 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3686 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3687 
3688 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3689 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3690 	ill->ill_global_timer = INFINITY;
3691 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3692 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3693 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3694 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3695 
3696 	/* No resolver here. */
3697 	ill->ill_net_type = IRE_LOOPBACK;
3698 
3699 	/* Initialize the ipsq */
3700 	if (!ipsq_init(ill, B_FALSE))
3701 		goto done;
3702 
3703 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE);
3704 	if (ipif == NULL)
3705 		goto done;
3706 
3707 	ill->ill_flags = ILLF_MULTICAST;
3708 
3709 	ov6addr = ipif->ipif_v6lcl_addr;
3710 	/* Set up default loopback address and mask. */
3711 	if (!isv6) {
3712 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3713 
3714 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3715 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3716 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3717 		    ipif->ipif_v6subnet);
3718 		ill->ill_flags |= ILLF_IPV4;
3719 	} else {
3720 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3721 		ipif->ipif_v6net_mask = ipv6_all_ones;
3722 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3723 		    ipif->ipif_v6subnet);
3724 		ill->ill_flags |= ILLF_IPV6;
3725 	}
3726 
3727 	/*
3728 	 * Chain us in at the end of the ill list. hold the ill
3729 	 * before we make it globally visible. 1 for the lookup.
3730 	 */
3731 	ill->ill_refcnt = 0;
3732 	ill_refhold(ill);
3733 
3734 	ill->ill_frag_count = 0;
3735 	ill->ill_frag_free_num_pkts = 0;
3736 	ill->ill_last_frag_clean_time = 0;
3737 
3738 	ipsq = ill->ill_phyint->phyint_ipsq;
3739 
3740 	ill_set_inputfn(ill);
3741 
3742 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3743 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3744 
3745 	/* Let SCTP know so that it can add this to its list */
3746 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3747 
3748 	/*
3749 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3750 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3751 	 * requires to be after ill_glist_insert() since we need the
3752 	 * ill_index set. Pass on ipv6_loopback as the old address.
3753 	 */
3754 	sctp_update_ipif_addr(ipif, ov6addr);
3755 
3756 	/*
3757 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3758 	 * If so, free our original one.
3759 	 */
3760 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3761 		ipsq_delete(ipsq);
3762 
3763 	if (ipst->ips_loopback_ksp == NULL) {
3764 		/* Export loopback interface statistics */
3765 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3766 		    ipif_loopback_name, "net",
3767 		    KSTAT_TYPE_NAMED, 2, 0,
3768 		    ipst->ips_netstack->netstack_stackid);
3769 		if (ipst->ips_loopback_ksp != NULL) {
3770 			ipst->ips_loopback_ksp->ks_update =
3771 			    loopback_kstat_update;
3772 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3773 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3774 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3775 			ipst->ips_loopback_ksp->ks_private =
3776 			    (void *)(uintptr_t)ipst->ips_netstack->
3777 			    netstack_stackid;
3778 			kstat_install(ipst->ips_loopback_ksp);
3779 		}
3780 	}
3781 
3782 	*did_alloc = B_TRUE;
3783 	rw_exit(&ipst->ips_ill_g_lock);
3784 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3785 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3786 	return (ill);
3787 done:
3788 	if (ill != NULL) {
3789 		if (ill->ill_phyint != NULL) {
3790 			ipsq = ill->ill_phyint->phyint_ipsq;
3791 			if (ipsq != NULL) {
3792 				ipsq->ipsq_phyint = NULL;
3793 				ipsq_delete(ipsq);
3794 			}
3795 			mi_free(ill->ill_phyint);
3796 		}
3797 		ill_free_mib(ill);
3798 		if (ill->ill_ipst != NULL)
3799 			netstack_rele(ill->ill_ipst->ips_netstack);
3800 		mi_free(ill);
3801 	}
3802 	rw_exit(&ipst->ips_ill_g_lock);
3803 	return (NULL);
3804 }
3805 
3806 /*
3807  * For IPP calls - use the ip_stack_t for global stack.
3808  */
3809 ill_t *
3810 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3811 {
3812 	ip_stack_t	*ipst;
3813 	ill_t		*ill;
3814 
3815 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3816 	if (ipst == NULL) {
3817 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3818 		return (NULL);
3819 	}
3820 
3821 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3822 	netstack_rele(ipst->ips_netstack);
3823 	return (ill);
3824 }
3825 
3826 /*
3827  * Return a pointer to the ill which matches the index and IP version type.
3828  */
3829 ill_t *
3830 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3831 {
3832 	ill_t	*ill;
3833 	phyint_t *phyi;
3834 
3835 	/*
3836 	 * Indexes are stored in the phyint - a common structure
3837 	 * to both IPv4 and IPv6.
3838 	 */
3839 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3840 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3841 	    (void *) &index, NULL);
3842 	if (phyi != NULL) {
3843 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3844 		if (ill != NULL) {
3845 			mutex_enter(&ill->ill_lock);
3846 			if (!ILL_IS_CONDEMNED(ill)) {
3847 				ill_refhold_locked(ill);
3848 				mutex_exit(&ill->ill_lock);
3849 				rw_exit(&ipst->ips_ill_g_lock);
3850 				return (ill);
3851 			}
3852 			mutex_exit(&ill->ill_lock);
3853 		}
3854 	}
3855 	rw_exit(&ipst->ips_ill_g_lock);
3856 	return (NULL);
3857 }
3858 
3859 /*
3860  * Verify whether or not an interface index is valid.
3861  * It can be zero (meaning "reset") or an interface index assigned
3862  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3863  */
3864 boolean_t
3865 ip_ifindex_valid(uint_t ifindex, boolean_t isv6, ip_stack_t *ipst)
3866 {
3867 	ill_t		*ill;
3868 
3869 	if (ifindex == 0)
3870 		return (B_TRUE);
3871 
3872 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
3873 	if (ill == NULL)
3874 		return (B_FALSE);
3875 	if (IS_VNI(ill)) {
3876 		ill_refrele(ill);
3877 		return (B_FALSE);
3878 	}
3879 	ill_refrele(ill);
3880 	return (B_TRUE);
3881 }
3882 
3883 /*
3884  * Return the ifindex next in sequence after the passed in ifindex.
3885  * If there is no next ifindex for the given protocol, return 0.
3886  */
3887 uint_t
3888 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3889 {
3890 	phyint_t *phyi;
3891 	phyint_t *phyi_initial;
3892 	uint_t   ifindex;
3893 
3894 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3895 
3896 	if (index == 0) {
3897 		phyi = avl_first(
3898 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3899 	} else {
3900 		phyi = phyi_initial = avl_find(
3901 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3902 		    (void *) &index, NULL);
3903 	}
3904 
3905 	for (; phyi != NULL;
3906 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3907 	    phyi, AVL_AFTER)) {
3908 		/*
3909 		 * If we're not returning the first interface in the tree
3910 		 * and we still haven't moved past the phyint_t that
3911 		 * corresponds to index, avl_walk needs to be called again
3912 		 */
3913 		if (!((index != 0) && (phyi == phyi_initial))) {
3914 			if (isv6) {
3915 				if ((phyi->phyint_illv6) &&
3916 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3917 				    (phyi->phyint_illv6->ill_isv6 == 1))
3918 					break;
3919 			} else {
3920 				if ((phyi->phyint_illv4) &&
3921 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3922 				    (phyi->phyint_illv4->ill_isv6 == 0))
3923 					break;
3924 			}
3925 		}
3926 	}
3927 
3928 	rw_exit(&ipst->ips_ill_g_lock);
3929 
3930 	if (phyi != NULL)
3931 		ifindex = phyi->phyint_ifindex;
3932 	else
3933 		ifindex = 0;
3934 
3935 	return (ifindex);
3936 }
3937 
3938 /*
3939  * Return the ifindex for the named interface.
3940  * If there is no next ifindex for the interface, return 0.
3941  */
3942 uint_t
3943 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3944 {
3945 	phyint_t	*phyi;
3946 	avl_index_t	where = 0;
3947 	uint_t		ifindex;
3948 
3949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3950 
3951 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3952 	    name, &where)) == NULL) {
3953 		rw_exit(&ipst->ips_ill_g_lock);
3954 		return (0);
3955 	}
3956 
3957 	ifindex = phyi->phyint_ifindex;
3958 
3959 	rw_exit(&ipst->ips_ill_g_lock);
3960 
3961 	return (ifindex);
3962 }
3963 
3964 /*
3965  * Return the ifindex to be used by upper layer protocols for instance
3966  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3967  */
3968 uint_t
3969 ill_get_upper_ifindex(const ill_t *ill)
3970 {
3971 	if (IS_UNDER_IPMP(ill))
3972 		return (ipmp_ill_get_ipmp_ifindex(ill));
3973 	else
3974 		return (ill->ill_phyint->phyint_ifindex);
3975 }
3976 
3977 
3978 /*
3979  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3980  * that gives a running thread a reference to the ill. This reference must be
3981  * released by the thread when it is done accessing the ill and related
3982  * objects. ill_refcnt can not be used to account for static references
3983  * such as other structures pointing to an ill. Callers must generally
3984  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3985  * or be sure that the ill is not being deleted or changing state before
3986  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3987  * ill won't change any of its critical state such as address, netmask etc.
3988  */
3989 void
3990 ill_refhold(ill_t *ill)
3991 {
3992 	mutex_enter(&ill->ill_lock);
3993 	ill->ill_refcnt++;
3994 	ILL_TRACE_REF(ill);
3995 	mutex_exit(&ill->ill_lock);
3996 }
3997 
3998 void
3999 ill_refhold_locked(ill_t *ill)
4000 {
4001 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4002 	ill->ill_refcnt++;
4003 	ILL_TRACE_REF(ill);
4004 }
4005 
4006 /* Returns true if we managed to get a refhold */
4007 boolean_t
4008 ill_check_and_refhold(ill_t *ill)
4009 {
4010 	mutex_enter(&ill->ill_lock);
4011 	if (!ILL_IS_CONDEMNED(ill)) {
4012 		ill_refhold_locked(ill);
4013 		mutex_exit(&ill->ill_lock);
4014 		return (B_TRUE);
4015 	}
4016 	mutex_exit(&ill->ill_lock);
4017 	return (B_FALSE);
4018 }
4019 
4020 /*
4021  * Must not be called while holding any locks. Otherwise if this is
4022  * the last reference to be released, there is a chance of recursive mutex
4023  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4024  * to restart an ioctl.
4025  */
4026 void
4027 ill_refrele(ill_t *ill)
4028 {
4029 	mutex_enter(&ill->ill_lock);
4030 	ASSERT(ill->ill_refcnt != 0);
4031 	ill->ill_refcnt--;
4032 	ILL_UNTRACE_REF(ill);
4033 	if (ill->ill_refcnt != 0) {
4034 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4035 		mutex_exit(&ill->ill_lock);
4036 		return;
4037 	}
4038 
4039 	/* Drops the ill_lock */
4040 	ipif_ill_refrele_tail(ill);
4041 }
4042 
4043 /*
4044  * Obtain a weak reference count on the ill. This reference ensures the
4045  * ill won't be freed, but the ill may change any of its critical state
4046  * such as netmask, address etc. Returns an error if the ill has started
4047  * closing.
4048  */
4049 boolean_t
4050 ill_waiter_inc(ill_t *ill)
4051 {
4052 	mutex_enter(&ill->ill_lock);
4053 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4054 		mutex_exit(&ill->ill_lock);
4055 		return (B_FALSE);
4056 	}
4057 	ill->ill_waiters++;
4058 	mutex_exit(&ill->ill_lock);
4059 	return (B_TRUE);
4060 }
4061 
4062 void
4063 ill_waiter_dcr(ill_t *ill)
4064 {
4065 	mutex_enter(&ill->ill_lock);
4066 	ill->ill_waiters--;
4067 	if (ill->ill_waiters == 0)
4068 		cv_broadcast(&ill->ill_cv);
4069 	mutex_exit(&ill->ill_lock);
4070 }
4071 
4072 /*
4073  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4074  * driver.  We construct best guess defaults for lower level information that
4075  * we need.  If an interface is brought up without injection of any overriding
4076  * information from outside, we have to be ready to go with these defaults.
4077  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4078  * we primarely want the dl_provider_style.
4079  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4080  * at which point we assume the other part of the information is valid.
4081  */
4082 void
4083 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4084 {
4085 	uchar_t		*brdcst_addr;
4086 	uint_t		brdcst_addr_length, phys_addr_length;
4087 	t_scalar_t	sap_length;
4088 	dl_info_ack_t	*dlia;
4089 	ip_m_t		*ipm;
4090 	dl_qos_cl_sel1_t *sel1;
4091 	int		min_mtu;
4092 
4093 	ASSERT(IAM_WRITER_ILL(ill));
4094 
4095 	/*
4096 	 * Till the ill is fully up  the ill is not globally visible.
4097 	 * So no need for a lock.
4098 	 */
4099 	dlia = (dl_info_ack_t *)mp->b_rptr;
4100 	ill->ill_mactype = dlia->dl_mac_type;
4101 
4102 	ipm = ip_m_lookup(dlia->dl_mac_type);
4103 	if (ipm == NULL) {
4104 		ipm = ip_m_lookup(DL_OTHER);
4105 		ASSERT(ipm != NULL);
4106 	}
4107 	ill->ill_media = ipm;
4108 
4109 	/*
4110 	 * When the new DLPI stuff is ready we'll pull lengths
4111 	 * from dlia.
4112 	 */
4113 	if (dlia->dl_version == DL_VERSION_2) {
4114 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4115 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4116 		    brdcst_addr_length);
4117 		if (brdcst_addr == NULL) {
4118 			brdcst_addr_length = 0;
4119 		}
4120 		sap_length = dlia->dl_sap_length;
4121 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4122 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4123 		    brdcst_addr_length, sap_length, phys_addr_length));
4124 	} else {
4125 		brdcst_addr_length = 6;
4126 		brdcst_addr = ip_six_byte_all_ones;
4127 		sap_length = -2;
4128 		phys_addr_length = brdcst_addr_length;
4129 	}
4130 
4131 	ill->ill_bcast_addr_length = brdcst_addr_length;
4132 	ill->ill_phys_addr_length = phys_addr_length;
4133 	ill->ill_sap_length = sap_length;
4134 
4135 	/*
4136 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4137 	 * but we must ensure a minimum IP MTU is used since other bits of
4138 	 * IP will fly apart otherwise.
4139 	 */
4140 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4141 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4142 	ill->ill_current_frag = ill->ill_max_frag;
4143 	ill->ill_mtu = ill->ill_max_frag;
4144 
4145 	ill->ill_type = ipm->ip_m_type;
4146 
4147 	if (!ill->ill_dlpi_style_set) {
4148 		if (dlia->dl_provider_style == DL_STYLE2)
4149 			ill->ill_needs_attach = 1;
4150 
4151 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4152 
4153 		/*
4154 		 * Allocate the first ipif on this ill.  We don't delay it
4155 		 * further as ioctl handling assumes at least one ipif exists.
4156 		 *
4157 		 * At this point we don't know whether the ill is v4 or v6.
4158 		 * We will know this whan the SIOCSLIFNAME happens and
4159 		 * the correct value for ill_isv6 will be assigned in
4160 		 * ipif_set_values(). We need to hold the ill lock and
4161 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4162 		 * the wakeup.
4163 		 */
4164 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4165 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE);
4166 		mutex_enter(&ill->ill_lock);
4167 		ASSERT(ill->ill_dlpi_style_set == 0);
4168 		ill->ill_dlpi_style_set = 1;
4169 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4170 		cv_broadcast(&ill->ill_cv);
4171 		mutex_exit(&ill->ill_lock);
4172 		freemsg(mp);
4173 		return;
4174 	}
4175 	ASSERT(ill->ill_ipif != NULL);
4176 	/*
4177 	 * We know whether it is IPv4 or IPv6 now, as this is the
4178 	 * second DL_INFO_ACK we are recieving in response to the
4179 	 * DL_INFO_REQ sent in ipif_set_values.
4180 	 */
4181 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4182 	/*
4183 	 * Clear all the flags that were set based on ill_bcast_addr_length
4184 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4185 	 * changed now and we need to re-evaluate.
4186 	 */
4187 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4188 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4189 
4190 	/*
4191 	 * Free ill_bcast_mp as things could have changed now.
4192 	 *
4193 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4194 	 * with no underlying interfaces (and thus an unknown broadcast
4195 	 * address length), but we enforce that an interface is broadcast-
4196 	 * capable as part of allowing it to join a group.
4197 	 */
4198 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4199 		if (ill->ill_bcast_mp != NULL)
4200 			freemsg(ill->ill_bcast_mp);
4201 		ill->ill_net_type = IRE_IF_NORESOLVER;
4202 
4203 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4204 		    ill->ill_phys_addr_length,
4205 		    ill->ill_sap,
4206 		    ill->ill_sap_length);
4207 
4208 		if (ill->ill_isv6)
4209 			/*
4210 			 * Note: xresolv interfaces will eventually need NOARP
4211 			 * set here as well, but that will require those
4212 			 * external resolvers to have some knowledge of
4213 			 * that flag and act appropriately. Not to be changed
4214 			 * at present.
4215 			 */
4216 			ill->ill_flags |= ILLF_NONUD;
4217 		else
4218 			ill->ill_flags |= ILLF_NOARP;
4219 
4220 		if (ill->ill_mactype == SUNW_DL_VNI) {
4221 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4222 		} else if (ill->ill_phys_addr_length == 0 ||
4223 		    ill->ill_mactype == DL_IPV4 ||
4224 		    ill->ill_mactype == DL_IPV6) {
4225 			/*
4226 			 * The underying link is point-to-point, so mark the
4227 			 * interface as such.  We can do IP multicast over
4228 			 * such a link since it transmits all network-layer
4229 			 * packets to the remote side the same way.
4230 			 */
4231 			ill->ill_flags |= ILLF_MULTICAST;
4232 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4233 		}
4234 	} else {
4235 		ill->ill_net_type = IRE_IF_RESOLVER;
4236 		if (ill->ill_bcast_mp != NULL)
4237 			freemsg(ill->ill_bcast_mp);
4238 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4239 		    ill->ill_bcast_addr_length, ill->ill_sap,
4240 		    ill->ill_sap_length);
4241 		/*
4242 		 * Later detect lack of DLPI driver multicast
4243 		 * capability by catching DL_ENABMULTI errors in
4244 		 * ip_rput_dlpi.
4245 		 */
4246 		ill->ill_flags |= ILLF_MULTICAST;
4247 		if (!ill->ill_isv6)
4248 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4249 	}
4250 
4251 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4252 	if (ill->ill_mactype == SUNW_DL_IPMP)
4253 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4254 
4255 	/* By default an interface does not support any CoS marking */
4256 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4257 
4258 	/*
4259 	 * If we get QoS information in DL_INFO_ACK, the device supports
4260 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4261 	 */
4262 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4263 	    dlia->dl_qos_length);
4264 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4265 		ill->ill_flags |= ILLF_COS_ENABLED;
4266 	}
4267 
4268 	/* Clear any previous error indication. */
4269 	ill->ill_error = 0;
4270 	freemsg(mp);
4271 }
4272 
4273 /*
4274  * Perform various checks to verify that an address would make sense as a
4275  * local, remote, or subnet interface address.
4276  */
4277 static boolean_t
4278 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4279 {
4280 	ipaddr_t	net_mask;
4281 
4282 	/*
4283 	 * Don't allow all zeroes, or all ones, but allow
4284 	 * all ones netmask.
4285 	 */
4286 	if ((net_mask = ip_net_mask(addr)) == 0)
4287 		return (B_FALSE);
4288 	/* A given netmask overrides the "guess" netmask */
4289 	if (subnet_mask != 0)
4290 		net_mask = subnet_mask;
4291 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4292 	    (addr == (addr | ~net_mask)))) {
4293 		return (B_FALSE);
4294 	}
4295 
4296 	/*
4297 	 * Even if the netmask is all ones, we do not allow address to be
4298 	 * 255.255.255.255
4299 	 */
4300 	if (addr == INADDR_BROADCAST)
4301 		return (B_FALSE);
4302 
4303 	if (CLASSD(addr))
4304 		return (B_FALSE);
4305 
4306 	return (B_TRUE);
4307 }
4308 
4309 #define	V6_IPIF_LINKLOCAL(p)	\
4310 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4311 
4312 /*
4313  * Compare two given ipifs and check if the second one is better than
4314  * the first one using the order of preference (not taking deprecated
4315  * into acount) specified in ipif_lookup_multicast().
4316  */
4317 static boolean_t
4318 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4319 {
4320 	/* Check the least preferred first. */
4321 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4322 		/* If both ipifs are the same, use the first one. */
4323 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4324 			return (B_FALSE);
4325 		else
4326 			return (B_TRUE);
4327 	}
4328 
4329 	/* For IPv6, check for link local address. */
4330 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4331 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4332 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4333 			/* The second one is equal or less preferred. */
4334 			return (B_FALSE);
4335 		} else {
4336 			return (B_TRUE);
4337 		}
4338 	}
4339 
4340 	/* Then check for point to point interface. */
4341 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4342 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4343 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4344 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4345 			return (B_FALSE);
4346 		} else {
4347 			return (B_TRUE);
4348 		}
4349 	}
4350 
4351 	/* old_ipif is a normal interface, so no need to use the new one. */
4352 	return (B_FALSE);
4353 }
4354 
4355 /*
4356  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4357  * The ipif must be up, and its ill must multicast-capable, not
4358  * condemned, not an underlying interface in an IPMP group, and
4359  * not a VNI interface.  Order of preference:
4360  *
4361  * 	1a. normal
4362  * 	1b. normal, but deprecated
4363  * 	2a. point to point
4364  * 	2b. point to point, but deprecated
4365  * 	3a. link local
4366  * 	3b. link local, but deprecated
4367  * 	4. loopback.
4368  */
4369 static ipif_t *
4370 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4371 {
4372 	ill_t			*ill;
4373 	ill_walk_context_t	ctx;
4374 	ipif_t			*ipif;
4375 	ipif_t			*saved_ipif = NULL;
4376 	ipif_t			*dep_ipif = NULL;
4377 
4378 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4379 	if (isv6)
4380 		ill = ILL_START_WALK_V6(&ctx, ipst);
4381 	else
4382 		ill = ILL_START_WALK_V4(&ctx, ipst);
4383 
4384 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4385 		mutex_enter(&ill->ill_lock);
4386 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4387 		    ILL_IS_CONDEMNED(ill) ||
4388 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4389 			mutex_exit(&ill->ill_lock);
4390 			continue;
4391 		}
4392 		for (ipif = ill->ill_ipif; ipif != NULL;
4393 		    ipif = ipif->ipif_next) {
4394 			if (zoneid != ipif->ipif_zoneid &&
4395 			    zoneid != ALL_ZONES &&
4396 			    ipif->ipif_zoneid != ALL_ZONES) {
4397 				continue;
4398 			}
4399 			if (!(ipif->ipif_flags & IPIF_UP) ||
4400 			    IPIF_IS_CONDEMNED(ipif)) {
4401 				continue;
4402 			}
4403 
4404 			/*
4405 			 * Found one candidate.  If it is deprecated,
4406 			 * remember it in dep_ipif.  If it is not deprecated,
4407 			 * remember it in saved_ipif.
4408 			 */
4409 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4410 				if (dep_ipif == NULL) {
4411 					dep_ipif = ipif;
4412 				} else if (ipif_comp_multi(dep_ipif, ipif,
4413 				    isv6)) {
4414 					/*
4415 					 * If the previous dep_ipif does not
4416 					 * belong to the same ill, we've done
4417 					 * a ipif_refhold() on it.  So we need
4418 					 * to release it.
4419 					 */
4420 					if (dep_ipif->ipif_ill != ill)
4421 						ipif_refrele(dep_ipif);
4422 					dep_ipif = ipif;
4423 				}
4424 				continue;
4425 			}
4426 			if (saved_ipif == NULL) {
4427 				saved_ipif = ipif;
4428 			} else {
4429 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4430 					if (saved_ipif->ipif_ill != ill)
4431 						ipif_refrele(saved_ipif);
4432 					saved_ipif = ipif;
4433 				}
4434 			}
4435 		}
4436 		/*
4437 		 * Before going to the next ill, do a ipif_refhold() on the
4438 		 * saved ones.
4439 		 */
4440 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4441 			ipif_refhold_locked(saved_ipif);
4442 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4443 			ipif_refhold_locked(dep_ipif);
4444 		mutex_exit(&ill->ill_lock);
4445 	}
4446 	rw_exit(&ipst->ips_ill_g_lock);
4447 
4448 	/*
4449 	 * If we have only the saved_ipif, return it.  But if we have both
4450 	 * saved_ipif and dep_ipif, check to see which one is better.
4451 	 */
4452 	if (saved_ipif != NULL) {
4453 		if (dep_ipif != NULL) {
4454 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4455 				ipif_refrele(saved_ipif);
4456 				return (dep_ipif);
4457 			} else {
4458 				ipif_refrele(dep_ipif);
4459 				return (saved_ipif);
4460 			}
4461 		}
4462 		return (saved_ipif);
4463 	} else {
4464 		return (dep_ipif);
4465 	}
4466 }
4467 
4468 ill_t *
4469 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4470 {
4471 	ipif_t *ipif;
4472 	ill_t *ill;
4473 
4474 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4475 	if (ipif == NULL)
4476 		return (NULL);
4477 
4478 	ill = ipif->ipif_ill;
4479 	ill_refhold(ill);
4480 	ipif_refrele(ipif);
4481 	return (ill);
4482 }
4483 
4484 /*
4485  * This function is called when an application does not specify an interface
4486  * to be used for multicast traffic (joining a group/sending data).  It
4487  * calls ire_lookup_multi() to look for an interface route for the
4488  * specified multicast group.  Doing this allows the administrator to add
4489  * prefix routes for multicast to indicate which interface to be used for
4490  * multicast traffic in the above scenario.  The route could be for all
4491  * multicast (224.0/4), for a single multicast group (a /32 route) or
4492  * anything in between.  If there is no such multicast route, we just find
4493  * any multicast capable interface and return it.  The returned ipif
4494  * is refhold'ed.
4495  *
4496  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4497  * unicast table. This is used by CGTP.
4498  */
4499 ill_t *
4500 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4501     boolean_t *multirtp, ipaddr_t *setsrcp)
4502 {
4503 	ill_t			*ill;
4504 
4505 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4506 	if (ill != NULL)
4507 		return (ill);
4508 
4509 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4510 }
4511 
4512 /*
4513  * Look for an ipif with the specified interface address and destination.
4514  * The destination address is used only for matching point-to-point interfaces.
4515  */
4516 ipif_t *
4517 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4518 {
4519 	ipif_t	*ipif;
4520 	ill_t	*ill;
4521 	ill_walk_context_t ctx;
4522 
4523 	/*
4524 	 * First match all the point-to-point interfaces
4525 	 * before looking at non-point-to-point interfaces.
4526 	 * This is done to avoid returning non-point-to-point
4527 	 * ipif instead of unnumbered point-to-point ipif.
4528 	 */
4529 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4530 	ill = ILL_START_WALK_V4(&ctx, ipst);
4531 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4532 		mutex_enter(&ill->ill_lock);
4533 		for (ipif = ill->ill_ipif; ipif != NULL;
4534 		    ipif = ipif->ipif_next) {
4535 			/* Allow the ipif to be down */
4536 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4537 			    (ipif->ipif_lcl_addr == if_addr) &&
4538 			    (ipif->ipif_pp_dst_addr == dst)) {
4539 				if (!IPIF_IS_CONDEMNED(ipif)) {
4540 					ipif_refhold_locked(ipif);
4541 					mutex_exit(&ill->ill_lock);
4542 					rw_exit(&ipst->ips_ill_g_lock);
4543 					return (ipif);
4544 				}
4545 			}
4546 		}
4547 		mutex_exit(&ill->ill_lock);
4548 	}
4549 	rw_exit(&ipst->ips_ill_g_lock);
4550 
4551 	/* lookup the ipif based on interface address */
4552 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4553 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4554 	return (ipif);
4555 }
4556 
4557 /*
4558  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4559  */
4560 static ipif_t *
4561 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4562     zoneid_t zoneid, ip_stack_t *ipst)
4563 {
4564 	ipif_t  *ipif;
4565 	ill_t   *ill;
4566 	boolean_t ptp = B_FALSE;
4567 	ill_walk_context_t	ctx;
4568 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4569 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4570 
4571 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4572 	/*
4573 	 * Repeat twice, first based on local addresses and
4574 	 * next time for pointopoint.
4575 	 */
4576 repeat:
4577 	ill = ILL_START_WALK_V4(&ctx, ipst);
4578 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4579 		if (match_ill != NULL && ill != match_ill &&
4580 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4581 			continue;
4582 		}
4583 		mutex_enter(&ill->ill_lock);
4584 		for (ipif = ill->ill_ipif; ipif != NULL;
4585 		    ipif = ipif->ipif_next) {
4586 			if (zoneid != ALL_ZONES &&
4587 			    zoneid != ipif->ipif_zoneid &&
4588 			    ipif->ipif_zoneid != ALL_ZONES)
4589 				continue;
4590 
4591 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4592 				continue;
4593 
4594 			/* Allow the ipif to be down */
4595 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4596 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4597 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4598 			    (ipif->ipif_pp_dst_addr == addr))) {
4599 				if (!IPIF_IS_CONDEMNED(ipif)) {
4600 					ipif_refhold_locked(ipif);
4601 					mutex_exit(&ill->ill_lock);
4602 					rw_exit(&ipst->ips_ill_g_lock);
4603 					return (ipif);
4604 				}
4605 			}
4606 		}
4607 		mutex_exit(&ill->ill_lock);
4608 	}
4609 
4610 	/* If we already did the ptp case, then we are done */
4611 	if (ptp) {
4612 		rw_exit(&ipst->ips_ill_g_lock);
4613 		return (NULL);
4614 	}
4615 	ptp = B_TRUE;
4616 	goto repeat;
4617 }
4618 
4619 /*
4620  * Lookup an ipif with the specified address.  For point-to-point links we
4621  * look for matches on either the destination address or the local address,
4622  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4623  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4624  * (or illgrp if `match_ill' is in an IPMP group).
4625  */
4626 ipif_t *
4627 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4628     ip_stack_t *ipst)
4629 {
4630 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4631 	    zoneid, ipst));
4632 }
4633 
4634 /*
4635  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4636  * except that we will only return an address if it is not marked as
4637  * IPIF_DUPLICATE
4638  */
4639 ipif_t *
4640 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4641     ip_stack_t *ipst)
4642 {
4643 	return (ipif_lookup_addr_common(addr, match_ill,
4644 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4645 	    zoneid, ipst));
4646 }
4647 
4648 /*
4649  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4650  * `match_ill' across the IPMP group.  This function is only needed in some
4651  * corner-cases; almost everything should use ipif_lookup_addr().
4652  */
4653 ipif_t *
4654 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4655 {
4656 	ASSERT(match_ill != NULL);
4657 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4658 	    ipst));
4659 }
4660 
4661 /*
4662  * Look for an ipif with the specified address. For point-point links
4663  * we look for matches on either the destination address and the local
4664  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4665  * is set.
4666  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4667  * ill (or illgrp if `match_ill' is in an IPMP group).
4668  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4669  */
4670 zoneid_t
4671 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4672 {
4673 	zoneid_t zoneid;
4674 	ipif_t  *ipif;
4675 	ill_t   *ill;
4676 	boolean_t ptp = B_FALSE;
4677 	ill_walk_context_t	ctx;
4678 
4679 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4680 	/*
4681 	 * Repeat twice, first based on local addresses and
4682 	 * next time for pointopoint.
4683 	 */
4684 repeat:
4685 	ill = ILL_START_WALK_V4(&ctx, ipst);
4686 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4687 		if (match_ill != NULL && ill != match_ill &&
4688 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4689 			continue;
4690 		}
4691 		mutex_enter(&ill->ill_lock);
4692 		for (ipif = ill->ill_ipif; ipif != NULL;
4693 		    ipif = ipif->ipif_next) {
4694 			/* Allow the ipif to be down */
4695 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4696 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4697 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4698 			    (ipif->ipif_pp_dst_addr == addr)) &&
4699 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4700 				zoneid = ipif->ipif_zoneid;
4701 				mutex_exit(&ill->ill_lock);
4702 				rw_exit(&ipst->ips_ill_g_lock);
4703 				/*
4704 				 * If ipif_zoneid was ALL_ZONES then we have
4705 				 * a trusted extensions shared IP address.
4706 				 * In that case GLOBAL_ZONEID works to send.
4707 				 */
4708 				if (zoneid == ALL_ZONES)
4709 					zoneid = GLOBAL_ZONEID;
4710 				return (zoneid);
4711 			}
4712 		}
4713 		mutex_exit(&ill->ill_lock);
4714 	}
4715 
4716 	/* If we already did the ptp case, then we are done */
4717 	if (ptp) {
4718 		rw_exit(&ipst->ips_ill_g_lock);
4719 		return (ALL_ZONES);
4720 	}
4721 	ptp = B_TRUE;
4722 	goto repeat;
4723 }
4724 
4725 /*
4726  * Look for an ipif that matches the specified remote address i.e. the
4727  * ipif that would receive the specified packet.
4728  * First look for directly connected interfaces and then do a recursive
4729  * IRE lookup and pick the first ipif corresponding to the source address in the
4730  * ire.
4731  * Returns: held ipif
4732  *
4733  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4734  */
4735 ipif_t *
4736 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4737 {
4738 	ipif_t	*ipif;
4739 
4740 	ASSERT(!ill->ill_isv6);
4741 
4742 	/*
4743 	 * Someone could be changing this ipif currently or change it
4744 	 * after we return this. Thus  a few packets could use the old
4745 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4746 	 * will atomically be updated or cleaned up with the new value
4747 	 * Thus we don't need a lock to check the flags or other attrs below.
4748 	 */
4749 	mutex_enter(&ill->ill_lock);
4750 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4751 		if (IPIF_IS_CONDEMNED(ipif))
4752 			continue;
4753 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4754 		    ipif->ipif_zoneid != ALL_ZONES)
4755 			continue;
4756 		/* Allow the ipif to be down */
4757 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4758 			if ((ipif->ipif_pp_dst_addr == addr) ||
4759 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4760 			    ipif->ipif_lcl_addr == addr)) {
4761 				ipif_refhold_locked(ipif);
4762 				mutex_exit(&ill->ill_lock);
4763 				return (ipif);
4764 			}
4765 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4766 			ipif_refhold_locked(ipif);
4767 			mutex_exit(&ill->ill_lock);
4768 			return (ipif);
4769 		}
4770 	}
4771 	mutex_exit(&ill->ill_lock);
4772 	/*
4773 	 * For a remote destination it isn't possible to nail down a particular
4774 	 * ipif.
4775 	 */
4776 
4777 	/* Pick the first interface */
4778 	ipif = ipif_get_next_ipif(NULL, ill);
4779 	return (ipif);
4780 }
4781 
4782 /*
4783  * This func does not prevent refcnt from increasing. But if
4784  * the caller has taken steps to that effect, then this func
4785  * can be used to determine whether the ill has become quiescent
4786  */
4787 static boolean_t
4788 ill_is_quiescent(ill_t *ill)
4789 {
4790 	ipif_t	*ipif;
4791 
4792 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4793 
4794 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4795 		if (ipif->ipif_refcnt != 0)
4796 			return (B_FALSE);
4797 	}
4798 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4799 		return (B_FALSE);
4800 	}
4801 	return (B_TRUE);
4802 }
4803 
4804 boolean_t
4805 ill_is_freeable(ill_t *ill)
4806 {
4807 	ipif_t	*ipif;
4808 
4809 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4810 
4811 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4812 		if (ipif->ipif_refcnt != 0) {
4813 			return (B_FALSE);
4814 		}
4815 	}
4816 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4817 		return (B_FALSE);
4818 	}
4819 	return (B_TRUE);
4820 }
4821 
4822 /*
4823  * This func does not prevent refcnt from increasing. But if
4824  * the caller has taken steps to that effect, then this func
4825  * can be used to determine whether the ipif has become quiescent
4826  */
4827 static boolean_t
4828 ipif_is_quiescent(ipif_t *ipif)
4829 {
4830 	ill_t *ill;
4831 
4832 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4833 
4834 	if (ipif->ipif_refcnt != 0)
4835 		return (B_FALSE);
4836 
4837 	ill = ipif->ipif_ill;
4838 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4839 	    ill->ill_logical_down) {
4840 		return (B_TRUE);
4841 	}
4842 
4843 	/* This is the last ipif going down or being deleted on this ill */
4844 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4845 		return (B_FALSE);
4846 	}
4847 
4848 	return (B_TRUE);
4849 }
4850 
4851 /*
4852  * return true if the ipif can be destroyed: the ipif has to be quiescent
4853  * with zero references from ire/ilm to it.
4854  */
4855 static boolean_t
4856 ipif_is_freeable(ipif_t *ipif)
4857 {
4858 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4859 	ASSERT(ipif->ipif_id != 0);
4860 	return (ipif->ipif_refcnt == 0);
4861 }
4862 
4863 /*
4864  * The ipif/ill/ire has been refreled. Do the tail processing.
4865  * Determine if the ipif or ill in question has become quiescent and if so
4866  * wakeup close and/or restart any queued pending ioctl that is waiting
4867  * for the ipif_down (or ill_down)
4868  */
4869 void
4870 ipif_ill_refrele_tail(ill_t *ill)
4871 {
4872 	mblk_t	*mp;
4873 	conn_t	*connp;
4874 	ipsq_t	*ipsq;
4875 	ipxop_t	*ipx;
4876 	ipif_t	*ipif;
4877 	dl_notify_ind_t *dlindp;
4878 
4879 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4880 
4881 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4882 		/* ip_modclose() may be waiting */
4883 		cv_broadcast(&ill->ill_cv);
4884 	}
4885 
4886 	ipsq = ill->ill_phyint->phyint_ipsq;
4887 	mutex_enter(&ipsq->ipsq_lock);
4888 	ipx = ipsq->ipsq_xop;
4889 	mutex_enter(&ipx->ipx_lock);
4890 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4891 		goto unlock;
4892 
4893 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4894 
4895 	ipif = ipx->ipx_pending_ipif;
4896 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4897 		goto unlock;
4898 
4899 	switch (ipx->ipx_waitfor) {
4900 	case IPIF_DOWN:
4901 		if (!ipif_is_quiescent(ipif))
4902 			goto unlock;
4903 		break;
4904 	case IPIF_FREE:
4905 		if (!ipif_is_freeable(ipif))
4906 			goto unlock;
4907 		break;
4908 	case ILL_DOWN:
4909 		if (!ill_is_quiescent(ill))
4910 			goto unlock;
4911 		break;
4912 	case ILL_FREE:
4913 		/*
4914 		 * ILL_FREE is only for loopback; normal ill teardown waits
4915 		 * synchronously in ip_modclose() without using ipx_waitfor,
4916 		 * handled by the cv_broadcast() at the top of this function.
4917 		 */
4918 		if (!ill_is_freeable(ill))
4919 			goto unlock;
4920 		break;
4921 	default:
4922 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4923 		    (void *)ipsq, ipx->ipx_waitfor);
4924 	}
4925 
4926 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4927 	mutex_exit(&ipx->ipx_lock);
4928 	mp = ipsq_pending_mp_get(ipsq, &connp);
4929 	mutex_exit(&ipsq->ipsq_lock);
4930 	mutex_exit(&ill->ill_lock);
4931 
4932 	ASSERT(mp != NULL);
4933 	/*
4934 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4935 	 * we can only get here when the current operation decides it
4936 	 * it needs to quiesce via ipsq_pending_mp_add().
4937 	 */
4938 	switch (mp->b_datap->db_type) {
4939 	case M_PCPROTO:
4940 	case M_PROTO:
4941 		/*
4942 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4943 		 */
4944 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4945 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4946 
4947 		switch (dlindp->dl_notification) {
4948 		case DL_NOTE_PHYS_ADDR:
4949 			qwriter_ip(ill, ill->ill_rq, mp,
4950 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4951 			return;
4952 		case DL_NOTE_REPLUMB:
4953 			qwriter_ip(ill, ill->ill_rq, mp,
4954 			    ill_replumb_tail, CUR_OP, B_TRUE);
4955 			return;
4956 		default:
4957 			ASSERT(0);
4958 			ill_refrele(ill);
4959 		}
4960 		break;
4961 
4962 	case M_ERROR:
4963 	case M_HANGUP:
4964 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4965 		    B_TRUE);
4966 		return;
4967 
4968 	case M_IOCTL:
4969 	case M_IOCDATA:
4970 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4971 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4972 		return;
4973 
4974 	default:
4975 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4976 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4977 	}
4978 	return;
4979 unlock:
4980 	mutex_exit(&ipsq->ipsq_lock);
4981 	mutex_exit(&ipx->ipx_lock);
4982 	mutex_exit(&ill->ill_lock);
4983 }
4984 
4985 #ifdef DEBUG
4986 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4987 static void
4988 th_trace_rrecord(th_trace_t *th_trace)
4989 {
4990 	tr_buf_t *tr_buf;
4991 	uint_t lastref;
4992 
4993 	lastref = th_trace->th_trace_lastref;
4994 	lastref++;
4995 	if (lastref == TR_BUF_MAX)
4996 		lastref = 0;
4997 	th_trace->th_trace_lastref = lastref;
4998 	tr_buf = &th_trace->th_trbuf[lastref];
4999 	tr_buf->tr_time = ddi_get_lbolt();
5000 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5001 }
5002 
5003 static void
5004 th_trace_free(void *value)
5005 {
5006 	th_trace_t *th_trace = value;
5007 
5008 	ASSERT(th_trace->th_refcnt == 0);
5009 	kmem_free(th_trace, sizeof (*th_trace));
5010 }
5011 
5012 /*
5013  * Find or create the per-thread hash table used to track object references.
5014  * The ipst argument is NULL if we shouldn't allocate.
5015  *
5016  * Accesses per-thread data, so there's no need to lock here.
5017  */
5018 static mod_hash_t *
5019 th_trace_gethash(ip_stack_t *ipst)
5020 {
5021 	th_hash_t *thh;
5022 
5023 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5024 		mod_hash_t *mh;
5025 		char name[256];
5026 		size_t objsize, rshift;
5027 		int retv;
5028 
5029 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5030 			return (NULL);
5031 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5032 		    (void *)curthread);
5033 
5034 		/*
5035 		 * We use mod_hash_create_extended here rather than the more
5036 		 * obvious mod_hash_create_ptrhash because the latter has a
5037 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5038 		 * block.
5039 		 */
5040 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5041 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5042 		rshift = highbit(objsize);
5043 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5044 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5045 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5046 		if (mh == NULL) {
5047 			kmem_free(thh, sizeof (*thh));
5048 			return (NULL);
5049 		}
5050 		thh->thh_hash = mh;
5051 		thh->thh_ipst = ipst;
5052 		/*
5053 		 * We trace ills, ipifs, ires, and nces.  All of these are
5054 		 * per-IP-stack, so the lock on the thread list is as well.
5055 		 */
5056 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5057 		list_insert_tail(&ip_thread_list, thh);
5058 		rw_exit(&ip_thread_rwlock);
5059 		retv = tsd_set(ip_thread_data, thh);
5060 		ASSERT(retv == 0);
5061 	}
5062 	return (thh != NULL ? thh->thh_hash : NULL);
5063 }
5064 
5065 boolean_t
5066 th_trace_ref(const void *obj, ip_stack_t *ipst)
5067 {
5068 	th_trace_t *th_trace;
5069 	mod_hash_t *mh;
5070 	mod_hash_val_t val;
5071 
5072 	if ((mh = th_trace_gethash(ipst)) == NULL)
5073 		return (B_FALSE);
5074 
5075 	/*
5076 	 * Attempt to locate the trace buffer for this obj and thread.
5077 	 * If it does not exist, then allocate a new trace buffer and
5078 	 * insert into the hash.
5079 	 */
5080 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5081 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5082 		if (th_trace == NULL)
5083 			return (B_FALSE);
5084 
5085 		th_trace->th_id = curthread;
5086 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5087 		    (mod_hash_val_t)th_trace) != 0) {
5088 			kmem_free(th_trace, sizeof (th_trace_t));
5089 			return (B_FALSE);
5090 		}
5091 	} else {
5092 		th_trace = (th_trace_t *)val;
5093 	}
5094 
5095 	ASSERT(th_trace->th_refcnt >= 0 &&
5096 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5097 
5098 	th_trace->th_refcnt++;
5099 	th_trace_rrecord(th_trace);
5100 	return (B_TRUE);
5101 }
5102 
5103 /*
5104  * For the purpose of tracing a reference release, we assume that global
5105  * tracing is always on and that the same thread initiated the reference hold
5106  * is releasing.
5107  */
5108 void
5109 th_trace_unref(const void *obj)
5110 {
5111 	int retv;
5112 	mod_hash_t *mh;
5113 	th_trace_t *th_trace;
5114 	mod_hash_val_t val;
5115 
5116 	mh = th_trace_gethash(NULL);
5117 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5118 	ASSERT(retv == 0);
5119 	th_trace = (th_trace_t *)val;
5120 
5121 	ASSERT(th_trace->th_refcnt > 0);
5122 	th_trace->th_refcnt--;
5123 	th_trace_rrecord(th_trace);
5124 }
5125 
5126 /*
5127  * If tracing has been disabled, then we assume that the reference counts are
5128  * now useless, and we clear them out before destroying the entries.
5129  */
5130 void
5131 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5132 {
5133 	th_hash_t	*thh;
5134 	mod_hash_t	*mh;
5135 	mod_hash_val_t	val;
5136 	th_trace_t	*th_trace;
5137 	int		retv;
5138 
5139 	rw_enter(&ip_thread_rwlock, RW_READER);
5140 	for (thh = list_head(&ip_thread_list); thh != NULL;
5141 	    thh = list_next(&ip_thread_list, thh)) {
5142 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5143 		    &val) == 0) {
5144 			th_trace = (th_trace_t *)val;
5145 			if (trace_disable)
5146 				th_trace->th_refcnt = 0;
5147 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5148 			ASSERT(retv == 0);
5149 		}
5150 	}
5151 	rw_exit(&ip_thread_rwlock);
5152 }
5153 
5154 void
5155 ipif_trace_ref(ipif_t *ipif)
5156 {
5157 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5158 
5159 	if (ipif->ipif_trace_disable)
5160 		return;
5161 
5162 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5163 		ipif->ipif_trace_disable = B_TRUE;
5164 		ipif_trace_cleanup(ipif);
5165 	}
5166 }
5167 
5168 void
5169 ipif_untrace_ref(ipif_t *ipif)
5170 {
5171 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5172 
5173 	if (!ipif->ipif_trace_disable)
5174 		th_trace_unref(ipif);
5175 }
5176 
5177 void
5178 ill_trace_ref(ill_t *ill)
5179 {
5180 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5181 
5182 	if (ill->ill_trace_disable)
5183 		return;
5184 
5185 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5186 		ill->ill_trace_disable = B_TRUE;
5187 		ill_trace_cleanup(ill);
5188 	}
5189 }
5190 
5191 void
5192 ill_untrace_ref(ill_t *ill)
5193 {
5194 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5195 
5196 	if (!ill->ill_trace_disable)
5197 		th_trace_unref(ill);
5198 }
5199 
5200 /*
5201  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5202  * failure, ipif_trace_disable is set.
5203  */
5204 static void
5205 ipif_trace_cleanup(const ipif_t *ipif)
5206 {
5207 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5208 }
5209 
5210 /*
5211  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5212  * failure, ill_trace_disable is set.
5213  */
5214 static void
5215 ill_trace_cleanup(const ill_t *ill)
5216 {
5217 	th_trace_cleanup(ill, ill->ill_trace_disable);
5218 }
5219 #endif /* DEBUG */
5220 
5221 void
5222 ipif_refhold_locked(ipif_t *ipif)
5223 {
5224 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5225 	ipif->ipif_refcnt++;
5226 	IPIF_TRACE_REF(ipif);
5227 }
5228 
5229 void
5230 ipif_refhold(ipif_t *ipif)
5231 {
5232 	ill_t	*ill;
5233 
5234 	ill = ipif->ipif_ill;
5235 	mutex_enter(&ill->ill_lock);
5236 	ipif->ipif_refcnt++;
5237 	IPIF_TRACE_REF(ipif);
5238 	mutex_exit(&ill->ill_lock);
5239 }
5240 
5241 /*
5242  * Must not be called while holding any locks. Otherwise if this is
5243  * the last reference to be released there is a chance of recursive mutex
5244  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5245  * to restart an ioctl.
5246  */
5247 void
5248 ipif_refrele(ipif_t *ipif)
5249 {
5250 	ill_t	*ill;
5251 
5252 	ill = ipif->ipif_ill;
5253 
5254 	mutex_enter(&ill->ill_lock);
5255 	ASSERT(ipif->ipif_refcnt != 0);
5256 	ipif->ipif_refcnt--;
5257 	IPIF_UNTRACE_REF(ipif);
5258 	if (ipif->ipif_refcnt != 0) {
5259 		mutex_exit(&ill->ill_lock);
5260 		return;
5261 	}
5262 
5263 	/* Drops the ill_lock */
5264 	ipif_ill_refrele_tail(ill);
5265 }
5266 
5267 ipif_t *
5268 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5269 {
5270 	ipif_t	*ipif;
5271 
5272 	mutex_enter(&ill->ill_lock);
5273 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5274 	    ipif != NULL; ipif = ipif->ipif_next) {
5275 		if (IPIF_IS_CONDEMNED(ipif))
5276 			continue;
5277 		ipif_refhold_locked(ipif);
5278 		mutex_exit(&ill->ill_lock);
5279 		return (ipif);
5280 	}
5281 	mutex_exit(&ill->ill_lock);
5282 	return (NULL);
5283 }
5284 
5285 /*
5286  * TODO: make this table extendible at run time
5287  * Return a pointer to the mac type info for 'mac_type'
5288  */
5289 static ip_m_t *
5290 ip_m_lookup(t_uscalar_t mac_type)
5291 {
5292 	ip_m_t	*ipm;
5293 
5294 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5295 		if (ipm->ip_m_mac_type == mac_type)
5296 			return (ipm);
5297 	return (NULL);
5298 }
5299 
5300 /*
5301  * Make a link layer address from the multicast IP address *addr.
5302  * To form the link layer address, invoke the ip_m_v*mapping function
5303  * associated with the link-layer type.
5304  */
5305 void
5306 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5307 {
5308 	ip_m_t *ipm;
5309 
5310 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5311 		return;
5312 
5313 	ASSERT(addr != NULL);
5314 
5315 	ipm = ip_m_lookup(ill->ill_mactype);
5316 	if (ipm == NULL ||
5317 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5318 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5319 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5320 		    ill->ill_name, ill->ill_mactype));
5321 		return;
5322 	}
5323 	if (ill->ill_isv6)
5324 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5325 	else
5326 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5327 }
5328 
5329 /*
5330  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5331  * ill is passed in to associate it with the correct interface.
5332  * If ire_arg is set, then we return the held IRE in that location.
5333  */
5334 int
5335 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5336     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5337     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5338 {
5339 	ire_t	*ire, *nire;
5340 	ire_t	*gw_ire = NULL;
5341 	ipif_t	*ipif = NULL;
5342 	uint_t	type;
5343 	int	match_flags = MATCH_IRE_TYPE;
5344 	tsol_gc_t *gc = NULL;
5345 	tsol_gcgrp_t *gcgrp = NULL;
5346 	boolean_t gcgrp_xtraref = B_FALSE;
5347 	boolean_t cgtp_broadcast;
5348 
5349 	ip1dbg(("ip_rt_add:"));
5350 
5351 	if (ire_arg != NULL)
5352 		*ire_arg = NULL;
5353 
5354 	/*
5355 	 * If this is the case of RTF_HOST being set, then we set the netmask
5356 	 * to all ones (regardless if one was supplied).
5357 	 */
5358 	if (flags & RTF_HOST)
5359 		mask = IP_HOST_MASK;
5360 
5361 	/*
5362 	 * Prevent routes with a zero gateway from being created (since
5363 	 * interfaces can currently be plumbed and brought up no assigned
5364 	 * address).
5365 	 */
5366 	if (gw_addr == 0)
5367 		return (ENETUNREACH);
5368 	/*
5369 	 * Get the ipif, if any, corresponding to the gw_addr
5370 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5371 	 * we match on the gatway and destination to handle unnumbered pt-pt
5372 	 * interfaces.
5373 	 */
5374 	if (ill != NULL)
5375 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5376 	else
5377 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5378 	if (ipif != NULL) {
5379 		if (IS_VNI(ipif->ipif_ill)) {
5380 			ipif_refrele(ipif);
5381 			return (EINVAL);
5382 		}
5383 	}
5384 
5385 	/*
5386 	 * GateD will attempt to create routes with a loopback interface
5387 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5388 	 * these routes to be added, but create them as interface routes
5389 	 * since the gateway is an interface address.
5390 	 */
5391 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5392 		flags &= ~RTF_GATEWAY;
5393 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5394 		    mask == IP_HOST_MASK) {
5395 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5396 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5397 			    NULL);
5398 			if (ire != NULL) {
5399 				ire_refrele(ire);
5400 				ipif_refrele(ipif);
5401 				return (EEXIST);
5402 			}
5403 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5404 			    "for 0x%x\n", (void *)ipif,
5405 			    ipif->ipif_ire_type,
5406 			    ntohl(ipif->ipif_lcl_addr)));
5407 			ire = ire_create(
5408 			    (uchar_t *)&dst_addr,	/* dest address */
5409 			    (uchar_t *)&mask,		/* mask */
5410 			    NULL,			/* no gateway */
5411 			    ipif->ipif_ire_type,	/* LOOPBACK */
5412 			    ipif->ipif_ill,
5413 			    zoneid,
5414 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5415 			    NULL,
5416 			    ipst);
5417 
5418 			if (ire == NULL) {
5419 				ipif_refrele(ipif);
5420 				return (ENOMEM);
5421 			}
5422 			/* src address assigned by the caller? */
5423 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5424 				ire->ire_setsrc_addr = src_addr;
5425 
5426 			nire = ire_add(ire);
5427 			if (nire == NULL) {
5428 				/*
5429 				 * In the result of failure, ire_add() will have
5430 				 * already deleted the ire in question, so there
5431 				 * is no need to do that here.
5432 				 */
5433 				ipif_refrele(ipif);
5434 				return (ENOMEM);
5435 			}
5436 			/*
5437 			 * Check if it was a duplicate entry. This handles
5438 			 * the case of two racing route adds for the same route
5439 			 */
5440 			if (nire != ire) {
5441 				ASSERT(nire->ire_identical_ref > 1);
5442 				ire_delete(nire);
5443 				ire_refrele(nire);
5444 				ipif_refrele(ipif);
5445 				return (EEXIST);
5446 			}
5447 			ire = nire;
5448 			goto save_ire;
5449 		}
5450 	}
5451 
5452 	/*
5453 	 * The routes for multicast with CGTP are quite special in that
5454 	 * the gateway is the local interface address, yet RTF_GATEWAY
5455 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5456 	 * this undocumented and unusual use of multicast routes.
5457 	 */
5458 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5459 		flags &= ~RTF_GATEWAY;
5460 
5461 	/*
5462 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5463 	 * and the gateway address provided is one of the system's interface
5464 	 * addresses.  By using the routing socket interface and supplying an
5465 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5466 	 * specifying an interface route to be created is available which uses
5467 	 * the interface index that specifies the outgoing interface rather than
5468 	 * the address of an outgoing interface (which may not be able to
5469 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5470 	 * flag, routes can be specified which not only specify the next-hop to
5471 	 * be used when routing to a certain prefix, but also which outgoing
5472 	 * interface should be used.
5473 	 *
5474 	 * Previously, interfaces would have unique addresses assigned to them
5475 	 * and so the address assigned to a particular interface could be used
5476 	 * to identify a particular interface.  One exception to this was the
5477 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5478 	 *
5479 	 * With the advent of IPv6 and its link-local addresses, this
5480 	 * restriction was relaxed and interfaces could share addresses between
5481 	 * themselves.  In fact, typically all of the link-local interfaces on
5482 	 * an IPv6 node or router will have the same link-local address.  In
5483 	 * order to differentiate between these interfaces, the use of an
5484 	 * interface index is necessary and this index can be carried inside a
5485 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5486 	 * of using the interface index, however, is that all of the ipif's that
5487 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5488 	 * cannot be used to differentiate between ipif's (or logical
5489 	 * interfaces) that belong to the same ill (physical interface).
5490 	 *
5491 	 * For example, in the following case involving IPv4 interfaces and
5492 	 * logical interfaces
5493 	 *
5494 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5495 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5496 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5497 	 *
5498 	 * the ipif's corresponding to each of these interface routes can be
5499 	 * uniquely identified by the "gateway" (actually interface address).
5500 	 *
5501 	 * In this case involving multiple IPv6 default routes to a particular
5502 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5503 	 * default route is of interest:
5504 	 *
5505 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5506 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5507 	 */
5508 
5509 	/* RTF_GATEWAY not set */
5510 	if (!(flags & RTF_GATEWAY)) {
5511 		if (sp != NULL) {
5512 			ip2dbg(("ip_rt_add: gateway security attributes "
5513 			    "cannot be set with interface route\n"));
5514 			if (ipif != NULL)
5515 				ipif_refrele(ipif);
5516 			return (EINVAL);
5517 		}
5518 
5519 		/*
5520 		 * Whether or not ill (RTA_IFP) is set, we require that
5521 		 * the gateway is one of our local addresses.
5522 		 */
5523 		if (ipif == NULL)
5524 			return (ENETUNREACH);
5525 
5526 		/*
5527 		 * We use MATCH_IRE_ILL here. If the caller specified an
5528 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5529 		 * we use the ill derived from the gateway address.
5530 		 * We can always match the gateway address since we record it
5531 		 * in ire_gateway_addr.
5532 		 * We don't allow RTA_IFP to specify a different ill than the
5533 		 * one matching the ipif to make sure we can delete the route.
5534 		 */
5535 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5536 		if (ill == NULL) {
5537 			ill = ipif->ipif_ill;
5538 		} else if (ill != ipif->ipif_ill) {
5539 			ipif_refrele(ipif);
5540 			return (EINVAL);
5541 		}
5542 
5543 		/*
5544 		 * We check for an existing entry at this point.
5545 		 *
5546 		 * Since a netmask isn't passed in via the ioctl interface
5547 		 * (SIOCADDRT), we don't check for a matching netmask in that
5548 		 * case.
5549 		 */
5550 		if (!ioctl_msg)
5551 			match_flags |= MATCH_IRE_MASK;
5552 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5553 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5554 		    NULL);
5555 		if (ire != NULL) {
5556 			ire_refrele(ire);
5557 			ipif_refrele(ipif);
5558 			return (EEXIST);
5559 		}
5560 
5561 		/*
5562 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5563 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5564 		 * gateway.
5565 		 */
5566 		ire = ire_create(
5567 		    (uchar_t *)&dst_addr,
5568 		    (uint8_t *)&mask,
5569 		    (uint8_t *)&gw_addr,
5570 		    ill->ill_net_type,
5571 		    ill,
5572 		    zoneid,
5573 		    flags,
5574 		    NULL,
5575 		    ipst);
5576 		if (ire == NULL) {
5577 			ipif_refrele(ipif);
5578 			return (ENOMEM);
5579 		}
5580 
5581 		/*
5582 		 * Some software (for example, GateD and Sun Cluster) attempts
5583 		 * to create (what amount to) IRE_PREFIX routes with the
5584 		 * loopback address as the gateway.  This is primarily done to
5585 		 * set up prefixes with the RTF_REJECT flag set (for example,
5586 		 * when generating aggregate routes.)
5587 		 *
5588 		 * If the IRE type (as defined by ill->ill_net_type) is
5589 		 * IRE_LOOPBACK, then we map the request into a
5590 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5591 		 * these interface routes, by definition, can only be that.
5592 		 *
5593 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5594 		 * routine, but rather using ire_create() directly.
5595 		 *
5596 		 */
5597 		if (ill->ill_net_type == IRE_LOOPBACK) {
5598 			ire->ire_type = IRE_IF_NORESOLVER;
5599 			ire->ire_flags |= RTF_BLACKHOLE;
5600 		}
5601 
5602 		/* src address assigned by the caller? */
5603 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5604 			ire->ire_setsrc_addr = src_addr;
5605 
5606 		nire = ire_add(ire);
5607 		if (nire == NULL) {
5608 			/*
5609 			 * In the result of failure, ire_add() will have
5610 			 * already deleted the ire in question, so there
5611 			 * is no need to do that here.
5612 			 */
5613 			ipif_refrele(ipif);
5614 			return (ENOMEM);
5615 		}
5616 		/*
5617 		 * Check if it was a duplicate entry. This handles
5618 		 * the case of two racing route adds for the same route
5619 		 */
5620 		if (nire != ire) {
5621 			ire_delete(nire);
5622 			ire_refrele(nire);
5623 			ipif_refrele(ipif);
5624 			return (EEXIST);
5625 		}
5626 		ire = nire;
5627 		goto save_ire;
5628 	}
5629 
5630 	/*
5631 	 * Get an interface IRE for the specified gateway.
5632 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5633 	 * gateway, it is currently unreachable and we fail the request
5634 	 * accordingly.
5635 	 * If RTA_IFP was specified we look on that particular ill.
5636 	 */
5637 	if (ill != NULL)
5638 		match_flags |= MATCH_IRE_ILL;
5639 
5640 	/* Check whether the gateway is reachable. */
5641 again:
5642 	type = IRE_INTERFACE;
5643 	if (flags & RTF_INDIRECT)
5644 		type |= IRE_OFFLINK;
5645 
5646 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5647 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5648 	if (gw_ire == NULL) {
5649 		/*
5650 		 * With IPMP, we allow host routes to influence in.mpathd's
5651 		 * target selection.  However, if the test addresses are on
5652 		 * their own network, the above lookup will fail since the
5653 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5654 		 * hidden test IREs to be found and try again.
5655 		 */
5656 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5657 			match_flags |= MATCH_IRE_TESTHIDDEN;
5658 			goto again;
5659 		}
5660 
5661 		if (ipif != NULL)
5662 			ipif_refrele(ipif);
5663 		return (ENETUNREACH);
5664 	}
5665 
5666 	/*
5667 	 * We create one of three types of IREs as a result of this request
5668 	 * based on the netmask.  A netmask of all ones (which is automatically
5669 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5670 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5671 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5672 	 * destination prefix.
5673 	 */
5674 	if (mask == IP_HOST_MASK)
5675 		type = IRE_HOST;
5676 	else if (mask == 0)
5677 		type = IRE_DEFAULT;
5678 	else
5679 		type = IRE_PREFIX;
5680 
5681 	/* check for a duplicate entry */
5682 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5683 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5684 	    0, ipst, NULL);
5685 	if (ire != NULL) {
5686 		if (ipif != NULL)
5687 			ipif_refrele(ipif);
5688 		ire_refrele(gw_ire);
5689 		ire_refrele(ire);
5690 		return (EEXIST);
5691 	}
5692 
5693 	/* Security attribute exists */
5694 	if (sp != NULL) {
5695 		tsol_gcgrp_addr_t ga;
5696 
5697 		/* find or create the gateway credentials group */
5698 		ga.ga_af = AF_INET;
5699 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5700 
5701 		/* we hold reference to it upon success */
5702 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5703 		if (gcgrp == NULL) {
5704 			if (ipif != NULL)
5705 				ipif_refrele(ipif);
5706 			ire_refrele(gw_ire);
5707 			return (ENOMEM);
5708 		}
5709 
5710 		/*
5711 		 * Create and add the security attribute to the group; a
5712 		 * reference to the group is made upon allocating a new
5713 		 * entry successfully.  If it finds an already-existing
5714 		 * entry for the security attribute in the group, it simply
5715 		 * returns it and no new reference is made to the group.
5716 		 */
5717 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5718 		if (gc == NULL) {
5719 			if (ipif != NULL)
5720 				ipif_refrele(ipif);
5721 			/* release reference held by gcgrp_lookup */
5722 			GCGRP_REFRELE(gcgrp);
5723 			ire_refrele(gw_ire);
5724 			return (ENOMEM);
5725 		}
5726 	}
5727 
5728 	/* Create the IRE. */
5729 	ire = ire_create(
5730 	    (uchar_t *)&dst_addr,		/* dest address */
5731 	    (uchar_t *)&mask,			/* mask */
5732 	    (uchar_t *)&gw_addr,		/* gateway address */
5733 	    (ushort_t)type,			/* IRE type */
5734 	    ill,
5735 	    zoneid,
5736 	    flags,
5737 	    gc,					/* security attribute */
5738 	    ipst);
5739 
5740 	/*
5741 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5742 	 * reference to the 'gcgrp'. We can now release the extra reference
5743 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5744 	 */
5745 	if (gcgrp_xtraref)
5746 		GCGRP_REFRELE(gcgrp);
5747 	if (ire == NULL) {
5748 		if (gc != NULL)
5749 			GC_REFRELE(gc);
5750 		if (ipif != NULL)
5751 			ipif_refrele(ipif);
5752 		ire_refrele(gw_ire);
5753 		return (ENOMEM);
5754 	}
5755 
5756 	/* Before we add, check if an extra CGTP broadcast is needed */
5757 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5758 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5759 
5760 	/* src address assigned by the caller? */
5761 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5762 		ire->ire_setsrc_addr = src_addr;
5763 
5764 	/*
5765 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5766 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5767 	 */
5768 
5769 	/* Add the new IRE. */
5770 	nire = ire_add(ire);
5771 	if (nire == NULL) {
5772 		/*
5773 		 * In the result of failure, ire_add() will have
5774 		 * already deleted the ire in question, so there
5775 		 * is no need to do that here.
5776 		 */
5777 		if (ipif != NULL)
5778 			ipif_refrele(ipif);
5779 		ire_refrele(gw_ire);
5780 		return (ENOMEM);
5781 	}
5782 	/*
5783 	 * Check if it was a duplicate entry. This handles
5784 	 * the case of two racing route adds for the same route
5785 	 */
5786 	if (nire != ire) {
5787 		ire_delete(nire);
5788 		ire_refrele(nire);
5789 		if (ipif != NULL)
5790 			ipif_refrele(ipif);
5791 		ire_refrele(gw_ire);
5792 		return (EEXIST);
5793 	}
5794 	ire = nire;
5795 
5796 	if (flags & RTF_MULTIRT) {
5797 		/*
5798 		 * Invoke the CGTP (multirouting) filtering module
5799 		 * to add the dst address in the filtering database.
5800 		 * Replicated inbound packets coming from that address
5801 		 * will be filtered to discard the duplicates.
5802 		 * It is not necessary to call the CGTP filter hook
5803 		 * when the dst address is a broadcast or multicast,
5804 		 * because an IP source address cannot be a broadcast
5805 		 * or a multicast.
5806 		 */
5807 		if (cgtp_broadcast) {
5808 			ip_cgtp_bcast_add(ire, ipst);
5809 			goto save_ire;
5810 		}
5811 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5812 		    !CLASSD(ire->ire_addr)) {
5813 			int res;
5814 			ipif_t *src_ipif;
5815 
5816 			/* Find the source address corresponding to gw_ire */
5817 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5818 			    NULL, zoneid, ipst);
5819 			if (src_ipif != NULL) {
5820 				res = ipst->ips_ip_cgtp_filter_ops->
5821 				    cfo_add_dest_v4(
5822 				    ipst->ips_netstack->netstack_stackid,
5823 				    ire->ire_addr,
5824 				    ire->ire_gateway_addr,
5825 				    ire->ire_setsrc_addr,
5826 				    src_ipif->ipif_lcl_addr);
5827 				ipif_refrele(src_ipif);
5828 			} else {
5829 				res = EADDRNOTAVAIL;
5830 			}
5831 			if (res != 0) {
5832 				if (ipif != NULL)
5833 					ipif_refrele(ipif);
5834 				ire_refrele(gw_ire);
5835 				ire_delete(ire);
5836 				ire_refrele(ire);	/* Held in ire_add */
5837 				return (res);
5838 			}
5839 		}
5840 	}
5841 
5842 save_ire:
5843 	if (gw_ire != NULL) {
5844 		ire_refrele(gw_ire);
5845 		gw_ire = NULL;
5846 	}
5847 	if (ill != NULL) {
5848 		/*
5849 		 * Save enough information so that we can recreate the IRE if
5850 		 * the interface goes down and then up.  The metrics associated
5851 		 * with the route will be saved as well when rts_setmetrics() is
5852 		 * called after the IRE has been created.  In the case where
5853 		 * memory cannot be allocated, none of this information will be
5854 		 * saved.
5855 		 */
5856 		ill_save_ire(ill, ire);
5857 	}
5858 	if (ioctl_msg)
5859 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5860 	if (ire_arg != NULL) {
5861 		/*
5862 		 * Store the ire that was successfully added into where ire_arg
5863 		 * points to so that callers don't have to look it up
5864 		 * themselves (but they are responsible for ire_refrele()ing
5865 		 * the ire when they are finished with it).
5866 		 */
5867 		*ire_arg = ire;
5868 	} else {
5869 		ire_refrele(ire);		/* Held in ire_add */
5870 	}
5871 	if (ipif != NULL)
5872 		ipif_refrele(ipif);
5873 	return (0);
5874 }
5875 
5876 /*
5877  * ip_rt_delete is called to delete an IPv4 route.
5878  * ill is passed in to associate it with the correct interface.
5879  */
5880 /* ARGSUSED4 */
5881 int
5882 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5883     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5884     ip_stack_t *ipst, zoneid_t zoneid)
5885 {
5886 	ire_t	*ire = NULL;
5887 	ipif_t	*ipif;
5888 	uint_t	type;
5889 	uint_t	match_flags = MATCH_IRE_TYPE;
5890 	int	err = 0;
5891 
5892 	ip1dbg(("ip_rt_delete:"));
5893 	/*
5894 	 * If this is the case of RTF_HOST being set, then we set the netmask
5895 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5896 	 */
5897 	if (flags & RTF_HOST) {
5898 		mask = IP_HOST_MASK;
5899 		match_flags |= MATCH_IRE_MASK;
5900 	} else if (rtm_addrs & RTA_NETMASK) {
5901 		match_flags |= MATCH_IRE_MASK;
5902 	}
5903 
5904 	/*
5905 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5906 	 * we check if the gateway address is one of our interfaces first,
5907 	 * and fall back on RTF_GATEWAY routes.
5908 	 *
5909 	 * This makes it possible to delete an original
5910 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5911 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5912 	 * and those can not be deleted here.
5913 	 *
5914 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5915 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5916 	 * otherwise we use the ill derived from the gateway address.
5917 	 * We can always match the gateway address since we record it
5918 	 * in ire_gateway_addr.
5919 	 *
5920 	 * For more detail on specifying routes by gateway address and by
5921 	 * interface index, see the comments in ip_rt_add().
5922 	 */
5923 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5924 	if (ipif != NULL) {
5925 		ill_t	*ill_match;
5926 
5927 		if (ill != NULL)
5928 			ill_match = ill;
5929 		else
5930 			ill_match = ipif->ipif_ill;
5931 
5932 		match_flags |= MATCH_IRE_ILL;
5933 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5934 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5935 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
5936 			    NULL);
5937 		}
5938 		if (ire == NULL) {
5939 			match_flags |= MATCH_IRE_GW;
5940 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5941 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5942 			    match_flags, 0, ipst, NULL);
5943 		}
5944 		/* Avoid deleting routes created by kernel from an ipif */
5945 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5946 			ire_refrele(ire);
5947 			ire = NULL;
5948 		}
5949 
5950 		/* Restore in case we didn't find a match */
5951 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
5952 	}
5953 
5954 	if (ire == NULL) {
5955 		/*
5956 		 * At this point, the gateway address is not one of our own
5957 		 * addresses or a matching interface route was not found.  We
5958 		 * set the IRE type to lookup based on whether
5959 		 * this is a host route, a default route or just a prefix.
5960 		 *
5961 		 * If an ill was passed in, then the lookup is based on an
5962 		 * interface index so MATCH_IRE_ILL is added to match_flags.
5963 		 */
5964 		match_flags |= MATCH_IRE_GW;
5965 		if (ill != NULL)
5966 			match_flags |= MATCH_IRE_ILL;
5967 		if (mask == IP_HOST_MASK)
5968 			type = IRE_HOST;
5969 		else if (mask == 0)
5970 			type = IRE_DEFAULT;
5971 		else
5972 			type = IRE_PREFIX;
5973 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5974 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5975 	}
5976 
5977 	if (ipif != NULL) {
5978 		ipif_refrele(ipif);
5979 		ipif = NULL;
5980 	}
5981 
5982 	if (ire == NULL)
5983 		return (ESRCH);
5984 
5985 	if (ire->ire_flags & RTF_MULTIRT) {
5986 		/*
5987 		 * Invoke the CGTP (multirouting) filtering module
5988 		 * to remove the dst address from the filtering database.
5989 		 * Packets coming from that address will no longer be
5990 		 * filtered to remove duplicates.
5991 		 */
5992 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
5993 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
5994 			    ipst->ips_netstack->netstack_stackid,
5995 			    ire->ire_addr, ire->ire_gateway_addr);
5996 		}
5997 		ip_cgtp_bcast_delete(ire, ipst);
5998 	}
5999 
6000 	ill = ire->ire_ill;
6001 	if (ill != NULL)
6002 		ill_remove_saved_ire(ill, ire);
6003 	if (ioctl_msg)
6004 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6005 	ire_delete(ire);
6006 	ire_refrele(ire);
6007 	return (err);
6008 }
6009 
6010 /*
6011  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6012  */
6013 /* ARGSUSED */
6014 int
6015 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6016     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6017 {
6018 	ipaddr_t dst_addr;
6019 	ipaddr_t gw_addr;
6020 	ipaddr_t mask;
6021 	int error = 0;
6022 	mblk_t *mp1;
6023 	struct rtentry *rt;
6024 	ipif_t *ipif = NULL;
6025 	ip_stack_t	*ipst;
6026 
6027 	ASSERT(q->q_next == NULL);
6028 	ipst = CONNQ_TO_IPST(q);
6029 
6030 	ip1dbg(("ip_siocaddrt:"));
6031 	/* Existence of mp1 verified in ip_wput_nondata */
6032 	mp1 = mp->b_cont->b_cont;
6033 	rt = (struct rtentry *)mp1->b_rptr;
6034 
6035 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6036 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6037 
6038 	/*
6039 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6040 	 * to a particular host address.  In this case, we set the netmask to
6041 	 * all ones for the particular destination address.  Otherwise,
6042 	 * determine the netmask to be used based on dst_addr and the interfaces
6043 	 * in use.
6044 	 */
6045 	if (rt->rt_flags & RTF_HOST) {
6046 		mask = IP_HOST_MASK;
6047 	} else {
6048 		/*
6049 		 * Note that ip_subnet_mask returns a zero mask in the case of
6050 		 * default (an all-zeroes address).
6051 		 */
6052 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6053 	}
6054 
6055 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6056 	    B_TRUE, NULL, ipst, ALL_ZONES);
6057 	if (ipif != NULL)
6058 		ipif_refrele(ipif);
6059 	return (error);
6060 }
6061 
6062 /*
6063  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6064  */
6065 /* ARGSUSED */
6066 int
6067 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6068     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6069 {
6070 	ipaddr_t dst_addr;
6071 	ipaddr_t gw_addr;
6072 	ipaddr_t mask;
6073 	int error;
6074 	mblk_t *mp1;
6075 	struct rtentry *rt;
6076 	ipif_t *ipif = NULL;
6077 	ip_stack_t	*ipst;
6078 
6079 	ASSERT(q->q_next == NULL);
6080 	ipst = CONNQ_TO_IPST(q);
6081 
6082 	ip1dbg(("ip_siocdelrt:"));
6083 	/* Existence of mp1 verified in ip_wput_nondata */
6084 	mp1 = mp->b_cont->b_cont;
6085 	rt = (struct rtentry *)mp1->b_rptr;
6086 
6087 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6088 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6089 
6090 	/*
6091 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6092 	 * to a particular host address.  In this case, we set the netmask to
6093 	 * all ones for the particular destination address.  Otherwise,
6094 	 * determine the netmask to be used based on dst_addr and the interfaces
6095 	 * in use.
6096 	 */
6097 	if (rt->rt_flags & RTF_HOST) {
6098 		mask = IP_HOST_MASK;
6099 	} else {
6100 		/*
6101 		 * Note that ip_subnet_mask returns a zero mask in the case of
6102 		 * default (an all-zeroes address).
6103 		 */
6104 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6105 	}
6106 
6107 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6108 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6109 	    ipst, ALL_ZONES);
6110 	if (ipif != NULL)
6111 		ipif_refrele(ipif);
6112 	return (error);
6113 }
6114 
6115 /*
6116  * Enqueue the mp onto the ipsq, chained by b_next.
6117  * b_prev stores the function to be executed later, and b_queue the queue
6118  * where this mp originated.
6119  */
6120 void
6121 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6122     ill_t *pending_ill)
6123 {
6124 	conn_t	*connp;
6125 	ipxop_t *ipx = ipsq->ipsq_xop;
6126 
6127 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6128 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6129 	ASSERT(func != NULL);
6130 
6131 	mp->b_queue = q;
6132 	mp->b_prev = (void *)func;
6133 	mp->b_next = NULL;
6134 
6135 	switch (type) {
6136 	case CUR_OP:
6137 		if (ipx->ipx_mptail != NULL) {
6138 			ASSERT(ipx->ipx_mphead != NULL);
6139 			ipx->ipx_mptail->b_next = mp;
6140 		} else {
6141 			ASSERT(ipx->ipx_mphead == NULL);
6142 			ipx->ipx_mphead = mp;
6143 		}
6144 		ipx->ipx_mptail = mp;
6145 		break;
6146 
6147 	case NEW_OP:
6148 		if (ipsq->ipsq_xopq_mptail != NULL) {
6149 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6150 			ipsq->ipsq_xopq_mptail->b_next = mp;
6151 		} else {
6152 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6153 			ipsq->ipsq_xopq_mphead = mp;
6154 		}
6155 		ipsq->ipsq_xopq_mptail = mp;
6156 		ipx->ipx_ipsq_queued = B_TRUE;
6157 		break;
6158 
6159 	case SWITCH_OP:
6160 		ASSERT(ipsq->ipsq_swxop != NULL);
6161 		/* only one switch operation is currently allowed */
6162 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6163 		ipsq->ipsq_switch_mp = mp;
6164 		ipx->ipx_ipsq_queued = B_TRUE;
6165 		break;
6166 	default:
6167 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6168 	}
6169 
6170 	if (CONN_Q(q) && pending_ill != NULL) {
6171 		connp = Q_TO_CONN(q);
6172 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6173 		connp->conn_oper_pending_ill = pending_ill;
6174 	}
6175 }
6176 
6177 /*
6178  * Dequeue the next message that requested exclusive access to this IPSQ's
6179  * xop.  Specifically:
6180  *
6181  *  1. If we're still processing the current operation on `ipsq', then
6182  *     dequeue the next message for the operation (from ipx_mphead), or
6183  *     return NULL if there are no queued messages for the operation.
6184  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6185  *
6186  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6187  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6188  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6189  *     leaving IPMP groups and require a careful dance -- see the comments
6190  *     in-line below for details.  If we're leaving a group xop or if we're
6191  *     joining a group xop and become writer on it, then we proceed to (3).
6192  *     Otherwise, we return NULL and exit the xop.
6193  *
6194  *  3. For each IPSQ in the xop, return any switch operation stored on
6195  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6196  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6197  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6198  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6199  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6200  *     each phyint in the group, including the IPMP meta-interface phyint.
6201  */
6202 static mblk_t *
6203 ipsq_dq(ipsq_t *ipsq)
6204 {
6205 	ill_t	*illv4, *illv6;
6206 	mblk_t	*mp;
6207 	ipsq_t	*xopipsq;
6208 	ipsq_t	*leftipsq = NULL;
6209 	ipxop_t *ipx;
6210 	phyint_t *phyi = ipsq->ipsq_phyint;
6211 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6212 	boolean_t emptied = B_FALSE;
6213 
6214 	/*
6215 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6216 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6217 	 */
6218 	rw_enter(&ipst->ips_ill_g_lock,
6219 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6220 	mutex_enter(&ipsq->ipsq_lock);
6221 	ipx = ipsq->ipsq_xop;
6222 	mutex_enter(&ipx->ipx_lock);
6223 
6224 	/*
6225 	 * Dequeue the next message associated with the current exclusive
6226 	 * operation, if any.
6227 	 */
6228 	if ((mp = ipx->ipx_mphead) != NULL) {
6229 		ipx->ipx_mphead = mp->b_next;
6230 		if (ipx->ipx_mphead == NULL)
6231 			ipx->ipx_mptail = NULL;
6232 		mp->b_next = (void *)ipsq;
6233 		goto out;
6234 	}
6235 
6236 	if (ipx->ipx_current_ipif != NULL)
6237 		goto empty;
6238 
6239 	if (ipsq->ipsq_swxop != NULL) {
6240 		/*
6241 		 * The exclusive operation that is now being completed has
6242 		 * requested a switch to a different xop.  This happens
6243 		 * when an interface joins or leaves an IPMP group.  Joins
6244 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6245 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6246 		 * (phyint_free()), or interface plumb for an ill type
6247 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6248 		 *
6249 		 * Xop switches are not allowed on the IPMP meta-interface.
6250 		 */
6251 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6252 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6253 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6254 
6255 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6256 			/*
6257 			 * We're switching back to our own xop, so we have two
6258 			 * xop's to drain/exit: our own, and the group xop
6259 			 * that we are leaving.
6260 			 *
6261 			 * First, pull ourselves out of the group ipsq list.
6262 			 * This is safe since we're writer on ill_g_lock.
6263 			 */
6264 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6265 
6266 			xopipsq = ipx->ipx_ipsq;
6267 			while (xopipsq->ipsq_next != ipsq)
6268 				xopipsq = xopipsq->ipsq_next;
6269 
6270 			xopipsq->ipsq_next = ipsq->ipsq_next;
6271 			ipsq->ipsq_next = ipsq;
6272 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6273 			ipsq->ipsq_swxop = NULL;
6274 
6275 			/*
6276 			 * Second, prepare to exit the group xop.  The actual
6277 			 * ipsq_exit() is done at the end of this function
6278 			 * since we cannot hold any locks across ipsq_exit().
6279 			 * Note that although we drop the group's ipx_lock, no
6280 			 * threads can proceed since we're still ipx_writer.
6281 			 */
6282 			leftipsq = xopipsq;
6283 			mutex_exit(&ipx->ipx_lock);
6284 
6285 			/*
6286 			 * Third, set ipx to point to our own xop (which was
6287 			 * inactive and therefore can be entered).
6288 			 */
6289 			ipx = ipsq->ipsq_xop;
6290 			mutex_enter(&ipx->ipx_lock);
6291 			ASSERT(ipx->ipx_writer == NULL);
6292 			ASSERT(ipx->ipx_current_ipif == NULL);
6293 		} else {
6294 			/*
6295 			 * We're switching from our own xop to a group xop.
6296 			 * The requestor of the switch must ensure that the
6297 			 * group xop cannot go away (e.g. by ensuring the
6298 			 * phyint associated with the xop cannot go away).
6299 			 *
6300 			 * If we can become writer on our new xop, then we'll
6301 			 * do the drain.  Otherwise, the current writer of our
6302 			 * new xop will do the drain when it exits.
6303 			 *
6304 			 * First, splice ourselves into the group IPSQ list.
6305 			 * This is safe since we're writer on ill_g_lock.
6306 			 */
6307 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6308 
6309 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6310 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6311 				xopipsq = xopipsq->ipsq_next;
6312 
6313 			xopipsq->ipsq_next = ipsq;
6314 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6315 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6316 			ipsq->ipsq_swxop = NULL;
6317 
6318 			/*
6319 			 * Second, exit our own xop, since it's now unused.
6320 			 * This is safe since we've got the only reference.
6321 			 */
6322 			ASSERT(ipx->ipx_writer == curthread);
6323 			ipx->ipx_writer = NULL;
6324 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6325 			ipx->ipx_ipsq_queued = B_FALSE;
6326 			mutex_exit(&ipx->ipx_lock);
6327 
6328 			/*
6329 			 * Third, set ipx to point to our new xop, and check
6330 			 * if we can become writer on it.  If we cannot, then
6331 			 * the current writer will drain the IPSQ group when
6332 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6333 			 * because we're still holding ipsq_lock.
6334 			 */
6335 			ipx = ipsq->ipsq_xop;
6336 			mutex_enter(&ipx->ipx_lock);
6337 			if (ipx->ipx_writer != NULL ||
6338 			    ipx->ipx_current_ipif != NULL) {
6339 				goto out;
6340 			}
6341 		}
6342 
6343 		/*
6344 		 * Fourth, become writer on our new ipx before we continue
6345 		 * with the drain.  Note that we never dropped ipsq_lock
6346 		 * above, so no other thread could've raced with us to
6347 		 * become writer first.  Also, we're holding ipx_lock, so
6348 		 * no other thread can examine the ipx right now.
6349 		 */
6350 		ASSERT(ipx->ipx_current_ipif == NULL);
6351 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6352 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6353 		ipx->ipx_writer = curthread;
6354 		ipx->ipx_forced = B_FALSE;
6355 #ifdef DEBUG
6356 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6357 #endif
6358 	}
6359 
6360 	xopipsq = ipsq;
6361 	do {
6362 		/*
6363 		 * So that other operations operate on a consistent and
6364 		 * complete phyint, a switch message on an IPSQ must be
6365 		 * handled prior to any other operations on that IPSQ.
6366 		 */
6367 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6368 			xopipsq->ipsq_switch_mp = NULL;
6369 			ASSERT(mp->b_next == NULL);
6370 			mp->b_next = (void *)xopipsq;
6371 			goto out;
6372 		}
6373 
6374 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6375 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6376 			if (xopipsq->ipsq_xopq_mphead == NULL)
6377 				xopipsq->ipsq_xopq_mptail = NULL;
6378 			mp->b_next = (void *)xopipsq;
6379 			goto out;
6380 		}
6381 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6382 empty:
6383 	/*
6384 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6385 	 * new messages can end up on any IPSQ in the xop.
6386 	 */
6387 	ipx->ipx_writer = NULL;
6388 	ipx->ipx_forced = B_FALSE;
6389 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6390 	ipx->ipx_ipsq_queued = B_FALSE;
6391 	emptied = B_TRUE;
6392 #ifdef	DEBUG
6393 	ipx->ipx_depth = 0;
6394 #endif
6395 out:
6396 	mutex_exit(&ipx->ipx_lock);
6397 	mutex_exit(&ipsq->ipsq_lock);
6398 
6399 	/*
6400 	 * If we completely emptied the xop, then wake up any threads waiting
6401 	 * to enter any of the IPSQ's associated with it.
6402 	 */
6403 	if (emptied) {
6404 		xopipsq = ipsq;
6405 		do {
6406 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6407 				continue;
6408 
6409 			illv4 = phyi->phyint_illv4;
6410 			illv6 = phyi->phyint_illv6;
6411 
6412 			GRAB_ILL_LOCKS(illv4, illv6);
6413 			if (illv4 != NULL)
6414 				cv_broadcast(&illv4->ill_cv);
6415 			if (illv6 != NULL)
6416 				cv_broadcast(&illv6->ill_cv);
6417 			RELEASE_ILL_LOCKS(illv4, illv6);
6418 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6419 	}
6420 	rw_exit(&ipst->ips_ill_g_lock);
6421 
6422 	/*
6423 	 * Now that all locks are dropped, exit the IPSQ we left.
6424 	 */
6425 	if (leftipsq != NULL)
6426 		ipsq_exit(leftipsq);
6427 
6428 	return (mp);
6429 }
6430 
6431 /*
6432  * Return completion status of previously initiated DLPI operations on
6433  * ills in the purview of an ipsq.
6434  */
6435 static boolean_t
6436 ipsq_dlpi_done(ipsq_t *ipsq)
6437 {
6438 	ipsq_t		*ipsq_start;
6439 	phyint_t	*phyi;
6440 	ill_t		*ill;
6441 
6442 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6443 	ipsq_start = ipsq;
6444 
6445 	do {
6446 		/*
6447 		 * The only current users of this function are ipsq_try_enter
6448 		 * and ipsq_enter which have made sure that ipsq_writer is
6449 		 * NULL before we reach here. ill_dlpi_pending is modified
6450 		 * only by an ipsq writer
6451 		 */
6452 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6453 		phyi = ipsq->ipsq_phyint;
6454 		/*
6455 		 * phyi could be NULL if a phyint that is part of an
6456 		 * IPMP group is being unplumbed. A more detailed
6457 		 * comment is in ipmp_grp_update_kstats()
6458 		 */
6459 		if (phyi != NULL) {
6460 			ill = phyi->phyint_illv4;
6461 			if (ill != NULL &&
6462 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6463 			    ill->ill_arl_dlpi_pending))
6464 				return (B_FALSE);
6465 
6466 			ill = phyi->phyint_illv6;
6467 			if (ill != NULL &&
6468 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6469 				return (B_FALSE);
6470 		}
6471 
6472 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6473 
6474 	return (B_TRUE);
6475 }
6476 
6477 /*
6478  * Enter the ipsq corresponding to ill, by waiting synchronously till
6479  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6480  * will have to drain completely before ipsq_enter returns success.
6481  * ipx_current_ipif will be set if some exclusive op is in progress,
6482  * and the ipsq_exit logic will start the next enqueued op after
6483  * completion of the current op. If 'force' is used, we don't wait
6484  * for the enqueued ops. This is needed when a conn_close wants to
6485  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6486  * of an ill can also use this option. But we dont' use it currently.
6487  */
6488 #define	ENTER_SQ_WAIT_TICKS 100
6489 boolean_t
6490 ipsq_enter(ill_t *ill, boolean_t force, int type)
6491 {
6492 	ipsq_t	*ipsq;
6493 	ipxop_t *ipx;
6494 	boolean_t waited_enough = B_FALSE;
6495 	ip_stack_t *ipst = ill->ill_ipst;
6496 
6497 	/*
6498 	 * Note that the relationship between ill and ipsq is fixed as long as
6499 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6500 	 * relationship between the IPSQ and xop cannot change.  However,
6501 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6502 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6503 	 * waking up all ills in the xop when it becomes available.
6504 	 */
6505 	for (;;) {
6506 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6507 		mutex_enter(&ill->ill_lock);
6508 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6509 			mutex_exit(&ill->ill_lock);
6510 			rw_exit(&ipst->ips_ill_g_lock);
6511 			return (B_FALSE);
6512 		}
6513 
6514 		ipsq = ill->ill_phyint->phyint_ipsq;
6515 		mutex_enter(&ipsq->ipsq_lock);
6516 		ipx = ipsq->ipsq_xop;
6517 		mutex_enter(&ipx->ipx_lock);
6518 
6519 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6520 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6521 		    waited_enough))
6522 			break;
6523 
6524 		rw_exit(&ipst->ips_ill_g_lock);
6525 
6526 		if (!force || ipx->ipx_writer != NULL) {
6527 			mutex_exit(&ipx->ipx_lock);
6528 			mutex_exit(&ipsq->ipsq_lock);
6529 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6530 		} else {
6531 			mutex_exit(&ipx->ipx_lock);
6532 			mutex_exit(&ipsq->ipsq_lock);
6533 			(void) cv_reltimedwait(&ill->ill_cv,
6534 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6535 			waited_enough = B_TRUE;
6536 		}
6537 		mutex_exit(&ill->ill_lock);
6538 	}
6539 
6540 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6541 	ASSERT(ipx->ipx_reentry_cnt == 0);
6542 	ipx->ipx_writer = curthread;
6543 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6544 	ipx->ipx_reentry_cnt++;
6545 #ifdef DEBUG
6546 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6547 #endif
6548 	mutex_exit(&ipx->ipx_lock);
6549 	mutex_exit(&ipsq->ipsq_lock);
6550 	mutex_exit(&ill->ill_lock);
6551 	rw_exit(&ipst->ips_ill_g_lock);
6552 
6553 	return (B_TRUE);
6554 }
6555 
6556 /*
6557  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6558  * across the call to the core interface ipsq_try_enter() and hence calls this
6559  * function directly. This is explained more fully in ipif_set_values().
6560  * In order to support the above constraint, ipsq_try_enter is implemented as
6561  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6562  */
6563 static ipsq_t *
6564 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6565     int type, boolean_t reentry_ok)
6566 {
6567 	ipsq_t	*ipsq;
6568 	ipxop_t	*ipx;
6569 	ip_stack_t *ipst = ill->ill_ipst;
6570 
6571 	/*
6572 	 * lock ordering:
6573 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6574 	 *
6575 	 * ipx of an ipsq can't change when ipsq_lock is held.
6576 	 */
6577 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6578 	GRAB_CONN_LOCK(q);
6579 	mutex_enter(&ill->ill_lock);
6580 	ipsq = ill->ill_phyint->phyint_ipsq;
6581 	mutex_enter(&ipsq->ipsq_lock);
6582 	ipx = ipsq->ipsq_xop;
6583 	mutex_enter(&ipx->ipx_lock);
6584 
6585 	/*
6586 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6587 	 *    (Note: If the caller does not specify reentry_ok then neither
6588 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6589 	 *    again. Otherwise it can lead to an infinite loop
6590 	 * 2. Enter the ipsq if there is no current writer and this attempted
6591 	 *    entry is part of the current operation
6592 	 * 3. Enter the ipsq if there is no current writer and this is a new
6593 	 *    operation and the operation queue is empty and there is no
6594 	 *    operation currently in progress and if all previously initiated
6595 	 *    DLPI operations have completed.
6596 	 */
6597 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6598 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6599 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6600 	    ipsq_dlpi_done(ipsq))))) {
6601 		/* Success. */
6602 		ipx->ipx_reentry_cnt++;
6603 		ipx->ipx_writer = curthread;
6604 		ipx->ipx_forced = B_FALSE;
6605 		mutex_exit(&ipx->ipx_lock);
6606 		mutex_exit(&ipsq->ipsq_lock);
6607 		mutex_exit(&ill->ill_lock);
6608 		RELEASE_CONN_LOCK(q);
6609 #ifdef DEBUG
6610 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6611 #endif
6612 		return (ipsq);
6613 	}
6614 
6615 	if (func != NULL)
6616 		ipsq_enq(ipsq, q, mp, func, type, ill);
6617 
6618 	mutex_exit(&ipx->ipx_lock);
6619 	mutex_exit(&ipsq->ipsq_lock);
6620 	mutex_exit(&ill->ill_lock);
6621 	RELEASE_CONN_LOCK(q);
6622 	return (NULL);
6623 }
6624 
6625 /*
6626  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6627  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6628  * There is one ipsq per phyint. The ipsq
6629  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6630  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6631  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6632  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6633  * up the interface) and are enqueued in ipx_mphead.
6634  *
6635  * If a thread does not want to reenter the ipsq when it is already writer,
6636  * it must make sure that the specified reentry point to be called later
6637  * when the ipsq is empty, nor any code path starting from the specified reentry
6638  * point must never ever try to enter the ipsq again. Otherwise it can lead
6639  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6640  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6641  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6642  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6643  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6644  * ioctl if the current ioctl has completed. If the current ioctl is still
6645  * in progress it simply returns. The current ioctl could be waiting for
6646  * a response from another module (the driver or could be waiting for
6647  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6648  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6649  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6650  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6651  * all associated DLPI operations have completed.
6652  */
6653 
6654 /*
6655  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6656  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6657  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6658  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6659  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6660  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6661  */
6662 ipsq_t *
6663 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6664     ipsq_func_t func, int type, boolean_t reentry_ok)
6665 {
6666 	ip_stack_t	*ipst;
6667 	ipsq_t		*ipsq;
6668 
6669 	/* Only 1 of ipif or ill can be specified */
6670 	ASSERT((ipif != NULL) ^ (ill != NULL));
6671 
6672 	if (ipif != NULL)
6673 		ill = ipif->ipif_ill;
6674 	ipst = ill->ill_ipst;
6675 
6676 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6677 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6678 	rw_exit(&ipst->ips_ill_g_lock);
6679 
6680 	return (ipsq);
6681 }
6682 
6683 /*
6684  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6685  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6686  * cannot be entered, the mp is queued for completion.
6687  */
6688 void
6689 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6690     boolean_t reentry_ok)
6691 {
6692 	ipsq_t	*ipsq;
6693 
6694 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6695 
6696 	/*
6697 	 * Drop the caller's refhold on the ill.  This is safe since we either
6698 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6699 	 * IPSQ, in which case we return without accessing ill anymore.  This
6700 	 * is needed because func needs to see the correct refcount.
6701 	 * e.g. removeif can work only then.
6702 	 */
6703 	ill_refrele(ill);
6704 	if (ipsq != NULL) {
6705 		(*func)(ipsq, q, mp, NULL);
6706 		ipsq_exit(ipsq);
6707 	}
6708 }
6709 
6710 /*
6711  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6712  * prior to exiting.  Caller must be writer on the specified IPSQ.
6713  */
6714 void
6715 ipsq_exit(ipsq_t *ipsq)
6716 {
6717 	mblk_t *mp;
6718 	ipsq_t *mp_ipsq;
6719 	queue_t	*q;
6720 	phyint_t *phyi;
6721 	ipsq_func_t func;
6722 
6723 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6724 
6725 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6726 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6727 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6728 		return;
6729 	}
6730 
6731 	for (;;) {
6732 		phyi = ipsq->ipsq_phyint;
6733 		mp = ipsq_dq(ipsq);
6734 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6735 
6736 		/*
6737 		 * If we've changed to a new IPSQ, and the phyint associated
6738 		 * with the old one has gone away, free the old IPSQ.  Note
6739 		 * that this cannot happen while the IPSQ is in a group.
6740 		 */
6741 		if (mp_ipsq != ipsq && phyi == NULL) {
6742 			ASSERT(ipsq->ipsq_next == ipsq);
6743 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6744 			ipsq_delete(ipsq);
6745 		}
6746 
6747 		if (mp == NULL)
6748 			break;
6749 
6750 		q = mp->b_queue;
6751 		func = (ipsq_func_t)mp->b_prev;
6752 		ipsq = mp_ipsq;
6753 		mp->b_next = mp->b_prev = NULL;
6754 		mp->b_queue = NULL;
6755 
6756 		/*
6757 		 * If 'q' is an conn queue, it is valid, since we did a
6758 		 * a refhold on the conn at the start of the ioctl.
6759 		 * If 'q' is an ill queue, it is valid, since close of an
6760 		 * ill will clean up its IPSQ.
6761 		 */
6762 		(*func)(ipsq, q, mp, NULL);
6763 	}
6764 }
6765 
6766 /*
6767  * Used to start any igmp or mld timers that could not be started
6768  * while holding ill_mcast_lock. The timers can't be started while holding
6769  * the lock, since mld/igmp_start_timers may need to call untimeout()
6770  * which can't be done while holding the lock which the timeout handler
6771  * acquires. Otherwise
6772  * there could be a deadlock since the timeout handlers
6773  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6774  * ill_mcast_lock.
6775  */
6776 void
6777 ill_mcast_timer_start(ip_stack_t *ipst)
6778 {
6779 	int		next;
6780 
6781 	mutex_enter(&ipst->ips_igmp_timer_lock);
6782 	next = ipst->ips_igmp_deferred_next;
6783 	ipst->ips_igmp_deferred_next = INFINITY;
6784 	mutex_exit(&ipst->ips_igmp_timer_lock);
6785 
6786 	if (next != INFINITY)
6787 		igmp_start_timers(next, ipst);
6788 
6789 	mutex_enter(&ipst->ips_mld_timer_lock);
6790 	next = ipst->ips_mld_deferred_next;
6791 	ipst->ips_mld_deferred_next = INFINITY;
6792 	mutex_exit(&ipst->ips_mld_timer_lock);
6793 
6794 	if (next != INFINITY)
6795 		mld_start_timers(next, ipst);
6796 }
6797 
6798 /*
6799  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6800  * and `ioccmd'.
6801  */
6802 void
6803 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6804 {
6805 	ill_t *ill = ipif->ipif_ill;
6806 	ipxop_t *ipx = ipsq->ipsq_xop;
6807 
6808 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6809 	ASSERT(ipx->ipx_current_ipif == NULL);
6810 	ASSERT(ipx->ipx_current_ioctl == 0);
6811 
6812 	ipx->ipx_current_done = B_FALSE;
6813 	ipx->ipx_current_ioctl = ioccmd;
6814 	mutex_enter(&ipx->ipx_lock);
6815 	ipx->ipx_current_ipif = ipif;
6816 	mutex_exit(&ipx->ipx_lock);
6817 
6818 	/*
6819 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6820 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6821 	 * references to the ipif (so that the references will eventually
6822 	 * drop to zero) and also prevents any "get" operations (e.g.,
6823 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6824 	 * operation has completed and the ipif is again in a stable state.
6825 	 *
6826 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6827 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6828 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6829 	 * ipifs will be affected.
6830 	 *
6831 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6832 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6833 	 * operate on.
6834 	 */
6835 	switch (ioccmd) {
6836 	case SIOCLIFREMOVEIF:
6837 		break;
6838 	case 0:
6839 		mutex_enter(&ill->ill_lock);
6840 		ipif = ipif->ipif_ill->ill_ipif;
6841 		for (; ipif != NULL; ipif = ipif->ipif_next)
6842 			ipif->ipif_state_flags |= IPIF_CHANGING;
6843 		mutex_exit(&ill->ill_lock);
6844 		break;
6845 	default:
6846 		mutex_enter(&ill->ill_lock);
6847 		ipif->ipif_state_flags |= IPIF_CHANGING;
6848 		mutex_exit(&ill->ill_lock);
6849 	}
6850 }
6851 
6852 /*
6853  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6854  * the next exclusive operation to begin once we ipsq_exit().  However, if
6855  * pending DLPI operations remain, then we will wait for the queue to drain
6856  * before allowing the next exclusive operation to begin.  This ensures that
6857  * DLPI operations from one exclusive operation are never improperly processed
6858  * as part of a subsequent exclusive operation.
6859  */
6860 void
6861 ipsq_current_finish(ipsq_t *ipsq)
6862 {
6863 	ipxop_t	*ipx = ipsq->ipsq_xop;
6864 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6865 	ipif_t	*ipif = ipx->ipx_current_ipif;
6866 
6867 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6868 
6869 	/*
6870 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6871 	 * (but in that case, IPIF_CHANGING will already be clear and no
6872 	 * pending DLPI messages can remain).
6873 	 */
6874 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6875 		ill_t *ill = ipif->ipif_ill;
6876 
6877 		mutex_enter(&ill->ill_lock);
6878 		dlpi_pending = ill->ill_dlpi_pending;
6879 		if (ipx->ipx_current_ioctl == 0) {
6880 			ipif = ill->ill_ipif;
6881 			for (; ipif != NULL; ipif = ipif->ipif_next)
6882 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6883 		} else {
6884 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6885 		}
6886 		mutex_exit(&ill->ill_lock);
6887 	}
6888 
6889 	ASSERT(!ipx->ipx_current_done);
6890 	ipx->ipx_current_done = B_TRUE;
6891 	ipx->ipx_current_ioctl = 0;
6892 	if (dlpi_pending == DL_PRIM_INVAL) {
6893 		mutex_enter(&ipx->ipx_lock);
6894 		ipx->ipx_current_ipif = NULL;
6895 		mutex_exit(&ipx->ipx_lock);
6896 	}
6897 }
6898 
6899 /*
6900  * The ill is closing. Flush all messages on the ipsq that originated
6901  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6902  * for this ill since ipsq_enter could not have entered until then.
6903  * New messages can't be queued since the CONDEMNED flag is set.
6904  */
6905 static void
6906 ipsq_flush(ill_t *ill)
6907 {
6908 	queue_t	*q;
6909 	mblk_t	*prev;
6910 	mblk_t	*mp;
6911 	mblk_t	*mp_next;
6912 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6913 
6914 	ASSERT(IAM_WRITER_ILL(ill));
6915 
6916 	/*
6917 	 * Flush any messages sent up by the driver.
6918 	 */
6919 	mutex_enter(&ipx->ipx_lock);
6920 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6921 		mp_next = mp->b_next;
6922 		q = mp->b_queue;
6923 		if (q == ill->ill_rq || q == ill->ill_wq) {
6924 			/* dequeue mp */
6925 			if (prev == NULL)
6926 				ipx->ipx_mphead = mp->b_next;
6927 			else
6928 				prev->b_next = mp->b_next;
6929 			if (ipx->ipx_mptail == mp) {
6930 				ASSERT(mp_next == NULL);
6931 				ipx->ipx_mptail = prev;
6932 			}
6933 			inet_freemsg(mp);
6934 		} else {
6935 			prev = mp;
6936 		}
6937 	}
6938 	mutex_exit(&ipx->ipx_lock);
6939 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6940 	ipsq_xopq_mp_cleanup(ill, NULL);
6941 }
6942 
6943 /*
6944  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6945  * and return the associated ipif.
6946  * Return value:
6947  *	Non zero: An error has occurred. ci may not be filled out.
6948  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6949  *	a held ipif in ci.ci_ipif.
6950  */
6951 int
6952 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
6953     cmd_info_t *ci)
6954 {
6955 	char		*name;
6956 	struct ifreq    *ifr;
6957 	struct lifreq    *lifr;
6958 	ipif_t		*ipif = NULL;
6959 	ill_t		*ill;
6960 	conn_t		*connp;
6961 	boolean_t	isv6;
6962 	boolean_t	exists;
6963 	mblk_t		*mp1;
6964 	zoneid_t	zoneid;
6965 	ip_stack_t	*ipst;
6966 
6967 	if (q->q_next != NULL) {
6968 		ill = (ill_t *)q->q_ptr;
6969 		isv6 = ill->ill_isv6;
6970 		connp = NULL;
6971 		zoneid = ALL_ZONES;
6972 		ipst = ill->ill_ipst;
6973 	} else {
6974 		ill = NULL;
6975 		connp = Q_TO_CONN(q);
6976 		isv6 = (connp->conn_family == AF_INET6);
6977 		zoneid = connp->conn_zoneid;
6978 		if (zoneid == GLOBAL_ZONEID) {
6979 			/* global zone can access ipifs in all zones */
6980 			zoneid = ALL_ZONES;
6981 		}
6982 		ipst = connp->conn_netstack->netstack_ip;
6983 	}
6984 
6985 	/* Has been checked in ip_wput_nondata */
6986 	mp1 = mp->b_cont->b_cont;
6987 
6988 	if (ipip->ipi_cmd_type == IF_CMD) {
6989 		/* This a old style SIOC[GS]IF* command */
6990 		ifr = (struct ifreq *)mp1->b_rptr;
6991 		/*
6992 		 * Null terminate the string to protect against buffer
6993 		 * overrun. String was generated by user code and may not
6994 		 * be trusted.
6995 		 */
6996 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
6997 		name = ifr->ifr_name;
6998 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
6999 		ci->ci_sin6 = NULL;
7000 		ci->ci_lifr = (struct lifreq *)ifr;
7001 	} else {
7002 		/* This a new style SIOC[GS]LIF* command */
7003 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7004 		lifr = (struct lifreq *)mp1->b_rptr;
7005 		/*
7006 		 * Null terminate the string to protect against buffer
7007 		 * overrun. String was generated by user code and may not
7008 		 * be trusted.
7009 		 */
7010 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7011 		name = lifr->lifr_name;
7012 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7013 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7014 		ci->ci_lifr = lifr;
7015 	}
7016 
7017 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7018 		/*
7019 		 * The ioctl will be failed if the ioctl comes down
7020 		 * an conn stream
7021 		 */
7022 		if (ill == NULL) {
7023 			/*
7024 			 * Not an ill queue, return EINVAL same as the
7025 			 * old error code.
7026 			 */
7027 			return (ENXIO);
7028 		}
7029 		ipif = ill->ill_ipif;
7030 		ipif_refhold(ipif);
7031 	} else {
7032 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7033 		    &exists, isv6, zoneid, ipst);
7034 
7035 		/*
7036 		 * Ensure that get ioctls don't see any internal state changes
7037 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7038 		 * set.
7039 		 */
7040 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7041 		    !IAM_WRITER_IPIF(ipif)) {
7042 			ipsq_t	*ipsq;
7043 
7044 			if (connp != NULL)
7045 				mutex_enter(&connp->conn_lock);
7046 			mutex_enter(&ipif->ipif_ill->ill_lock);
7047 			if (IPIF_IS_CHANGING(ipif) &&
7048 			    !IPIF_IS_CONDEMNED(ipif)) {
7049 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7050 				mutex_enter(&ipsq->ipsq_lock);
7051 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7052 				mutex_exit(&ipif->ipif_ill->ill_lock);
7053 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7054 				    NEW_OP, ipif->ipif_ill);
7055 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7056 				mutex_exit(&ipsq->ipsq_lock);
7057 				if (connp != NULL)
7058 					mutex_exit(&connp->conn_lock);
7059 				ipif_refrele(ipif);
7060 				return (EINPROGRESS);
7061 			}
7062 			mutex_exit(&ipif->ipif_ill->ill_lock);
7063 			if (connp != NULL)
7064 				mutex_exit(&connp->conn_lock);
7065 		}
7066 	}
7067 
7068 	/*
7069 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7070 	 */
7071 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7072 		ipif_refrele(ipif);
7073 		return (ENXIO);
7074 	}
7075 
7076 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7077 	    name[0] == '\0') {
7078 		/*
7079 		 * Handle a or a SIOC?IF* with a null name
7080 		 * during plumb (on the ill queue before the I_PLINK).
7081 		 */
7082 		ipif = ill->ill_ipif;
7083 		ipif_refhold(ipif);
7084 	}
7085 
7086 	if (ipif == NULL)
7087 		return (ENXIO);
7088 
7089 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7090 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7091 
7092 	ci->ci_ipif = ipif;
7093 	return (0);
7094 }
7095 
7096 /*
7097  * Return the total number of ipifs.
7098  */
7099 static uint_t
7100 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7101 {
7102 	uint_t numifs = 0;
7103 	ill_t	*ill;
7104 	ill_walk_context_t	ctx;
7105 	ipif_t	*ipif;
7106 
7107 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7108 	ill = ILL_START_WALK_V4(&ctx, ipst);
7109 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7110 		if (IS_UNDER_IPMP(ill))
7111 			continue;
7112 		for (ipif = ill->ill_ipif; ipif != NULL;
7113 		    ipif = ipif->ipif_next) {
7114 			if (ipif->ipif_zoneid == zoneid ||
7115 			    ipif->ipif_zoneid == ALL_ZONES)
7116 				numifs++;
7117 		}
7118 	}
7119 	rw_exit(&ipst->ips_ill_g_lock);
7120 	return (numifs);
7121 }
7122 
7123 /*
7124  * Return the total number of ipifs.
7125  */
7126 static uint_t
7127 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7128 {
7129 	uint_t numifs = 0;
7130 	ill_t	*ill;
7131 	ipif_t	*ipif;
7132 	ill_walk_context_t	ctx;
7133 
7134 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7135 
7136 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7137 	if (family == AF_INET)
7138 		ill = ILL_START_WALK_V4(&ctx, ipst);
7139 	else if (family == AF_INET6)
7140 		ill = ILL_START_WALK_V6(&ctx, ipst);
7141 	else
7142 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7143 
7144 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7145 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7146 			continue;
7147 
7148 		for (ipif = ill->ill_ipif; ipif != NULL;
7149 		    ipif = ipif->ipif_next) {
7150 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7151 			    !(lifn_flags & LIFC_NOXMIT))
7152 				continue;
7153 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7154 			    !(lifn_flags & LIFC_TEMPORARY))
7155 				continue;
7156 			if (((ipif->ipif_flags &
7157 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7158 			    IPIF_DEPRECATED)) ||
7159 			    IS_LOOPBACK(ill) ||
7160 			    !(ipif->ipif_flags & IPIF_UP)) &&
7161 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7162 				continue;
7163 
7164 			if (zoneid != ipif->ipif_zoneid &&
7165 			    ipif->ipif_zoneid != ALL_ZONES &&
7166 			    (zoneid != GLOBAL_ZONEID ||
7167 			    !(lifn_flags & LIFC_ALLZONES)))
7168 				continue;
7169 
7170 			numifs++;
7171 		}
7172 	}
7173 	rw_exit(&ipst->ips_ill_g_lock);
7174 	return (numifs);
7175 }
7176 
7177 uint_t
7178 ip_get_lifsrcofnum(ill_t *ill)
7179 {
7180 	uint_t numifs = 0;
7181 	ill_t	*ill_head = ill;
7182 	ip_stack_t	*ipst = ill->ill_ipst;
7183 
7184 	/*
7185 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7186 	 * other thread may be trying to relink the ILLs in this usesrc group
7187 	 * and adjusting the ill_usesrc_grp_next pointers
7188 	 */
7189 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7190 	if ((ill->ill_usesrc_ifindex == 0) &&
7191 	    (ill->ill_usesrc_grp_next != NULL)) {
7192 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7193 		    ill = ill->ill_usesrc_grp_next)
7194 			numifs++;
7195 	}
7196 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7197 
7198 	return (numifs);
7199 }
7200 
7201 /* Null values are passed in for ipif, sin, and ifreq */
7202 /* ARGSUSED */
7203 int
7204 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7205     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7206 {
7207 	int *nump;
7208 	conn_t *connp = Q_TO_CONN(q);
7209 
7210 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7211 
7212 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7213 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7214 
7215 	*nump = ip_get_numifs(connp->conn_zoneid,
7216 	    connp->conn_netstack->netstack_ip);
7217 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7218 	return (0);
7219 }
7220 
7221 /* Null values are passed in for ipif, sin, and ifreq */
7222 /* ARGSUSED */
7223 int
7224 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7225     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7226 {
7227 	struct lifnum *lifn;
7228 	mblk_t	*mp1;
7229 	conn_t *connp = Q_TO_CONN(q);
7230 
7231 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7232 
7233 	/* Existence checked in ip_wput_nondata */
7234 	mp1 = mp->b_cont->b_cont;
7235 
7236 	lifn = (struct lifnum *)mp1->b_rptr;
7237 	switch (lifn->lifn_family) {
7238 	case AF_UNSPEC:
7239 	case AF_INET:
7240 	case AF_INET6:
7241 		break;
7242 	default:
7243 		return (EAFNOSUPPORT);
7244 	}
7245 
7246 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7247 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7248 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7249 	return (0);
7250 }
7251 
7252 /* ARGSUSED */
7253 int
7254 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7255     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7256 {
7257 	STRUCT_HANDLE(ifconf, ifc);
7258 	mblk_t *mp1;
7259 	struct iocblk *iocp;
7260 	struct ifreq *ifr;
7261 	ill_walk_context_t	ctx;
7262 	ill_t	*ill;
7263 	ipif_t	*ipif;
7264 	struct sockaddr_in *sin;
7265 	int32_t	ifclen;
7266 	zoneid_t zoneid;
7267 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7268 
7269 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7270 
7271 	ip1dbg(("ip_sioctl_get_ifconf"));
7272 	/* Existence verified in ip_wput_nondata */
7273 	mp1 = mp->b_cont->b_cont;
7274 	iocp = (struct iocblk *)mp->b_rptr;
7275 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7276 
7277 	/*
7278 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7279 	 * the user buffer address and length into which the list of struct
7280 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7281 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7282 	 * the SIOCGIFCONF operation was redefined to simply provide
7283 	 * a large output buffer into which we are supposed to jam the ifreq
7284 	 * array.  The same ioctl command code was used, despite the fact that
7285 	 * both the applications and the kernel code had to change, thus making
7286 	 * it impossible to support both interfaces.
7287 	 *
7288 	 * For reasons not good enough to try to explain, the following
7289 	 * algorithm is used for deciding what to do with one of these:
7290 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7291 	 * form with the output buffer coming down as the continuation message.
7292 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7293 	 * and we have to copy in the ifconf structure to find out how big the
7294 	 * output buffer is and where to copy out to.  Sure no problem...
7295 	 *
7296 	 */
7297 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7298 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7299 		int numifs = 0;
7300 		size_t ifc_bufsize;
7301 
7302 		/*
7303 		 * Must be (better be!) continuation of a TRANSPARENT
7304 		 * IOCTL.  We just copied in the ifconf structure.
7305 		 */
7306 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7307 		    (struct ifconf *)mp1->b_rptr);
7308 
7309 		/*
7310 		 * Allocate a buffer to hold requested information.
7311 		 *
7312 		 * If ifc_len is larger than what is needed, we only
7313 		 * allocate what we will use.
7314 		 *
7315 		 * If ifc_len is smaller than what is needed, return
7316 		 * EINVAL.
7317 		 *
7318 		 * XXX: the ill_t structure can hava 2 counters, for
7319 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7320 		 * number of interfaces for a device, so we don't need
7321 		 * to count them here...
7322 		 */
7323 		numifs = ip_get_numifs(zoneid, ipst);
7324 
7325 		ifclen = STRUCT_FGET(ifc, ifc_len);
7326 		ifc_bufsize = numifs * sizeof (struct ifreq);
7327 		if (ifc_bufsize > ifclen) {
7328 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7329 				/* old behaviour */
7330 				return (EINVAL);
7331 			} else {
7332 				ifc_bufsize = ifclen;
7333 			}
7334 		}
7335 
7336 		mp1 = mi_copyout_alloc(q, mp,
7337 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7338 		if (mp1 == NULL)
7339 			return (ENOMEM);
7340 
7341 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7342 	}
7343 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7344 	/*
7345 	 * the SIOCGIFCONF ioctl only knows about
7346 	 * IPv4 addresses, so don't try to tell
7347 	 * it about interfaces with IPv6-only
7348 	 * addresses. (Last parm 'isv6' is B_FALSE)
7349 	 */
7350 
7351 	ifr = (struct ifreq *)mp1->b_rptr;
7352 
7353 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7354 	ill = ILL_START_WALK_V4(&ctx, ipst);
7355 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7356 		if (IS_UNDER_IPMP(ill))
7357 			continue;
7358 		for (ipif = ill->ill_ipif; ipif != NULL;
7359 		    ipif = ipif->ipif_next) {
7360 			if (zoneid != ipif->ipif_zoneid &&
7361 			    ipif->ipif_zoneid != ALL_ZONES)
7362 				continue;
7363 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7364 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7365 					/* old behaviour */
7366 					rw_exit(&ipst->ips_ill_g_lock);
7367 					return (EINVAL);
7368 				} else {
7369 					goto if_copydone;
7370 				}
7371 			}
7372 			ipif_get_name(ipif, ifr->ifr_name,
7373 			    sizeof (ifr->ifr_name));
7374 			sin = (sin_t *)&ifr->ifr_addr;
7375 			*sin = sin_null;
7376 			sin->sin_family = AF_INET;
7377 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7378 			ifr++;
7379 		}
7380 	}
7381 if_copydone:
7382 	rw_exit(&ipst->ips_ill_g_lock);
7383 	mp1->b_wptr = (uchar_t *)ifr;
7384 
7385 	if (STRUCT_BUF(ifc) != NULL) {
7386 		STRUCT_FSET(ifc, ifc_len,
7387 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7388 	}
7389 	return (0);
7390 }
7391 
7392 /*
7393  * Get the interfaces using the address hosted on the interface passed in,
7394  * as a source adddress
7395  */
7396 /* ARGSUSED */
7397 int
7398 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7399     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7400 {
7401 	mblk_t *mp1;
7402 	ill_t	*ill, *ill_head;
7403 	ipif_t	*ipif, *orig_ipif;
7404 	int	numlifs = 0;
7405 	size_t	lifs_bufsize, lifsmaxlen;
7406 	struct	lifreq *lifr;
7407 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7408 	uint_t	ifindex;
7409 	zoneid_t zoneid;
7410 	boolean_t isv6 = B_FALSE;
7411 	struct	sockaddr_in	*sin;
7412 	struct	sockaddr_in6	*sin6;
7413 	STRUCT_HANDLE(lifsrcof, lifs);
7414 	ip_stack_t		*ipst;
7415 
7416 	ipst = CONNQ_TO_IPST(q);
7417 
7418 	ASSERT(q->q_next == NULL);
7419 
7420 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7421 
7422 	/* Existence verified in ip_wput_nondata */
7423 	mp1 = mp->b_cont->b_cont;
7424 
7425 	/*
7426 	 * Must be (better be!) continuation of a TRANSPARENT
7427 	 * IOCTL.  We just copied in the lifsrcof structure.
7428 	 */
7429 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7430 	    (struct lifsrcof *)mp1->b_rptr);
7431 
7432 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7433 		return (EINVAL);
7434 
7435 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7436 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7437 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7438 	if (ipif == NULL) {
7439 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7440 		    ifindex));
7441 		return (ENXIO);
7442 	}
7443 
7444 	/* Allocate a buffer to hold requested information */
7445 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7446 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7447 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7448 	/* The actual size needed is always returned in lifs_len */
7449 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7450 
7451 	/* If the amount we need is more than what is passed in, abort */
7452 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7453 		ipif_refrele(ipif);
7454 		return (0);
7455 	}
7456 
7457 	mp1 = mi_copyout_alloc(q, mp,
7458 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7459 	if (mp1 == NULL) {
7460 		ipif_refrele(ipif);
7461 		return (ENOMEM);
7462 	}
7463 
7464 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7465 	bzero(mp1->b_rptr, lifs_bufsize);
7466 
7467 	lifr = (struct lifreq *)mp1->b_rptr;
7468 
7469 	ill = ill_head = ipif->ipif_ill;
7470 	orig_ipif = ipif;
7471 
7472 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7473 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7474 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7475 
7476 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7477 	for (; (ill != NULL) && (ill != ill_head);
7478 	    ill = ill->ill_usesrc_grp_next) {
7479 
7480 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7481 			break;
7482 
7483 		ipif = ill->ill_ipif;
7484 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7485 		if (ipif->ipif_isv6) {
7486 			sin6 = (sin6_t *)&lifr->lifr_addr;
7487 			*sin6 = sin6_null;
7488 			sin6->sin6_family = AF_INET6;
7489 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7490 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7491 			    &ipif->ipif_v6net_mask);
7492 		} else {
7493 			sin = (sin_t *)&lifr->lifr_addr;
7494 			*sin = sin_null;
7495 			sin->sin_family = AF_INET;
7496 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7497 			lifr->lifr_addrlen = ip_mask_to_plen(
7498 			    ipif->ipif_net_mask);
7499 		}
7500 		lifr++;
7501 	}
7502 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7503 	rw_exit(&ipst->ips_ill_g_lock);
7504 	ipif_refrele(orig_ipif);
7505 	mp1->b_wptr = (uchar_t *)lifr;
7506 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7507 
7508 	return (0);
7509 }
7510 
7511 /* ARGSUSED */
7512 int
7513 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7514     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7515 {
7516 	mblk_t *mp1;
7517 	int	list;
7518 	ill_t	*ill;
7519 	ipif_t	*ipif;
7520 	int	flags;
7521 	int	numlifs = 0;
7522 	size_t	lifc_bufsize;
7523 	struct	lifreq *lifr;
7524 	sa_family_t	family;
7525 	struct	sockaddr_in	*sin;
7526 	struct	sockaddr_in6	*sin6;
7527 	ill_walk_context_t	ctx;
7528 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7529 	int32_t	lifclen;
7530 	zoneid_t zoneid;
7531 	STRUCT_HANDLE(lifconf, lifc);
7532 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7533 
7534 	ip1dbg(("ip_sioctl_get_lifconf"));
7535 
7536 	ASSERT(q->q_next == NULL);
7537 
7538 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7539 
7540 	/* Existence verified in ip_wput_nondata */
7541 	mp1 = mp->b_cont->b_cont;
7542 
7543 	/*
7544 	 * An extended version of SIOCGIFCONF that takes an
7545 	 * additional address family and flags field.
7546 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7547 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7548 	 * interfaces are omitted.
7549 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7550 	 * unless LIFC_TEMPORARY is specified.
7551 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7552 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7553 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7554 	 * has priority over LIFC_NOXMIT.
7555 	 */
7556 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7557 
7558 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7559 		return (EINVAL);
7560 
7561 	/*
7562 	 * Must be (better be!) continuation of a TRANSPARENT
7563 	 * IOCTL.  We just copied in the lifconf structure.
7564 	 */
7565 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7566 
7567 	family = STRUCT_FGET(lifc, lifc_family);
7568 	flags = STRUCT_FGET(lifc, lifc_flags);
7569 
7570 	switch (family) {
7571 	case AF_UNSPEC:
7572 		/*
7573 		 * walk all ILL's.
7574 		 */
7575 		list = MAX_G_HEADS;
7576 		break;
7577 	case AF_INET:
7578 		/*
7579 		 * walk only IPV4 ILL's.
7580 		 */
7581 		list = IP_V4_G_HEAD;
7582 		break;
7583 	case AF_INET6:
7584 		/*
7585 		 * walk only IPV6 ILL's.
7586 		 */
7587 		list = IP_V6_G_HEAD;
7588 		break;
7589 	default:
7590 		return (EAFNOSUPPORT);
7591 	}
7592 
7593 	/*
7594 	 * Allocate a buffer to hold requested information.
7595 	 *
7596 	 * If lifc_len is larger than what is needed, we only
7597 	 * allocate what we will use.
7598 	 *
7599 	 * If lifc_len is smaller than what is needed, return
7600 	 * EINVAL.
7601 	 */
7602 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7603 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7604 	lifclen = STRUCT_FGET(lifc, lifc_len);
7605 	if (lifc_bufsize > lifclen) {
7606 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7607 			return (EINVAL);
7608 		else
7609 			lifc_bufsize = lifclen;
7610 	}
7611 
7612 	mp1 = mi_copyout_alloc(q, mp,
7613 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7614 	if (mp1 == NULL)
7615 		return (ENOMEM);
7616 
7617 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7618 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7619 
7620 	lifr = (struct lifreq *)mp1->b_rptr;
7621 
7622 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7623 	ill = ill_first(list, list, &ctx, ipst);
7624 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7625 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7626 			continue;
7627 
7628 		for (ipif = ill->ill_ipif; ipif != NULL;
7629 		    ipif = ipif->ipif_next) {
7630 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7631 			    !(flags & LIFC_NOXMIT))
7632 				continue;
7633 
7634 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7635 			    !(flags & LIFC_TEMPORARY))
7636 				continue;
7637 
7638 			if (((ipif->ipif_flags &
7639 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7640 			    IPIF_DEPRECATED)) ||
7641 			    IS_LOOPBACK(ill) ||
7642 			    !(ipif->ipif_flags & IPIF_UP)) &&
7643 			    (flags & LIFC_EXTERNAL_SOURCE))
7644 				continue;
7645 
7646 			if (zoneid != ipif->ipif_zoneid &&
7647 			    ipif->ipif_zoneid != ALL_ZONES &&
7648 			    (zoneid != GLOBAL_ZONEID ||
7649 			    !(flags & LIFC_ALLZONES)))
7650 				continue;
7651 
7652 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7653 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7654 					rw_exit(&ipst->ips_ill_g_lock);
7655 					return (EINVAL);
7656 				} else {
7657 					goto lif_copydone;
7658 				}
7659 			}
7660 
7661 			ipif_get_name(ipif, lifr->lifr_name,
7662 			    sizeof (lifr->lifr_name));
7663 			lifr->lifr_type = ill->ill_type;
7664 			if (ipif->ipif_isv6) {
7665 				sin6 = (sin6_t *)&lifr->lifr_addr;
7666 				*sin6 = sin6_null;
7667 				sin6->sin6_family = AF_INET6;
7668 				sin6->sin6_addr =
7669 				    ipif->ipif_v6lcl_addr;
7670 				lifr->lifr_addrlen =
7671 				    ip_mask_to_plen_v6(
7672 				    &ipif->ipif_v6net_mask);
7673 			} else {
7674 				sin = (sin_t *)&lifr->lifr_addr;
7675 				*sin = sin_null;
7676 				sin->sin_family = AF_INET;
7677 				sin->sin_addr.s_addr =
7678 				    ipif->ipif_lcl_addr;
7679 				lifr->lifr_addrlen =
7680 				    ip_mask_to_plen(
7681 				    ipif->ipif_net_mask);
7682 			}
7683 			lifr++;
7684 		}
7685 	}
7686 lif_copydone:
7687 	rw_exit(&ipst->ips_ill_g_lock);
7688 
7689 	mp1->b_wptr = (uchar_t *)lifr;
7690 	if (STRUCT_BUF(lifc) != NULL) {
7691 		STRUCT_FSET(lifc, lifc_len,
7692 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7693 	}
7694 	return (0);
7695 }
7696 
7697 static void
7698 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7699 {
7700 	ip6_asp_t *table;
7701 	size_t table_size;
7702 	mblk_t *data_mp;
7703 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7704 	ip_stack_t	*ipst;
7705 
7706 	if (q->q_next == NULL)
7707 		ipst = CONNQ_TO_IPST(q);
7708 	else
7709 		ipst = ILLQ_TO_IPST(q);
7710 
7711 	/* These two ioctls are I_STR only */
7712 	if (iocp->ioc_count == TRANSPARENT) {
7713 		miocnak(q, mp, 0, EINVAL);
7714 		return;
7715 	}
7716 
7717 	data_mp = mp->b_cont;
7718 	if (data_mp == NULL) {
7719 		/* The user passed us a NULL argument */
7720 		table = NULL;
7721 		table_size = iocp->ioc_count;
7722 	} else {
7723 		/*
7724 		 * The user provided a table.  The stream head
7725 		 * may have copied in the user data in chunks,
7726 		 * so make sure everything is pulled up
7727 		 * properly.
7728 		 */
7729 		if (MBLKL(data_mp) < iocp->ioc_count) {
7730 			mblk_t *new_data_mp;
7731 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7732 			    NULL) {
7733 				miocnak(q, mp, 0, ENOMEM);
7734 				return;
7735 			}
7736 			freemsg(data_mp);
7737 			data_mp = new_data_mp;
7738 			mp->b_cont = data_mp;
7739 		}
7740 		table = (ip6_asp_t *)data_mp->b_rptr;
7741 		table_size = iocp->ioc_count;
7742 	}
7743 
7744 	switch (iocp->ioc_cmd) {
7745 	case SIOCGIP6ADDRPOLICY:
7746 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7747 		if (iocp->ioc_rval == -1)
7748 			iocp->ioc_error = EINVAL;
7749 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7750 		else if (table != NULL &&
7751 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7752 			ip6_asp_t *src = table;
7753 			ip6_asp32_t *dst = (void *)table;
7754 			int count = table_size / sizeof (ip6_asp_t);
7755 			int i;
7756 
7757 			/*
7758 			 * We need to do an in-place shrink of the array
7759 			 * to match the alignment attributes of the
7760 			 * 32-bit ABI looking at it.
7761 			 */
7762 			/* LINTED: logical expression always true: op "||" */
7763 			ASSERT(sizeof (*src) > sizeof (*dst));
7764 			for (i = 1; i < count; i++)
7765 				bcopy(src + i, dst + i, sizeof (*dst));
7766 		}
7767 #endif
7768 		break;
7769 
7770 	case SIOCSIP6ADDRPOLICY:
7771 		ASSERT(mp->b_prev == NULL);
7772 		mp->b_prev = (void *)q;
7773 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7774 		/*
7775 		 * We pass in the datamodel here so that the ip6_asp_replace()
7776 		 * routine can handle converting from 32-bit to native formats
7777 		 * where necessary.
7778 		 *
7779 		 * A better way to handle this might be to convert the inbound
7780 		 * data structure here, and hang it off a new 'mp'; thus the
7781 		 * ip6_asp_replace() logic would always be dealing with native
7782 		 * format data structures..
7783 		 *
7784 		 * (An even simpler way to handle these ioctls is to just
7785 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7786 		 * and just recompile everything that depends on it.)
7787 		 */
7788 #endif
7789 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7790 		    iocp->ioc_flag & IOC_MODELS);
7791 		return;
7792 	}
7793 
7794 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7795 	qreply(q, mp);
7796 }
7797 
7798 static void
7799 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7800 {
7801 	mblk_t 		*data_mp;
7802 	struct dstinforeq	*dir;
7803 	uint8_t		*end, *cur;
7804 	in6_addr_t	*daddr, *saddr;
7805 	ipaddr_t	v4daddr;
7806 	ire_t		*ire;
7807 	ipaddr_t	v4setsrc;
7808 	in6_addr_t	v6setsrc;
7809 	char		*slabel, *dlabel;
7810 	boolean_t	isipv4;
7811 	int		match_ire;
7812 	ill_t		*dst_ill;
7813 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7814 	conn_t		*connp = Q_TO_CONN(q);
7815 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7816 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7817 	uint64_t	ipif_flags;
7818 
7819 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7820 
7821 	/*
7822 	 * This ioctl is I_STR only, and must have a
7823 	 * data mblk following the M_IOCTL mblk.
7824 	 */
7825 	data_mp = mp->b_cont;
7826 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7827 		miocnak(q, mp, 0, EINVAL);
7828 		return;
7829 	}
7830 
7831 	if (MBLKL(data_mp) < iocp->ioc_count) {
7832 		mblk_t *new_data_mp;
7833 
7834 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7835 			miocnak(q, mp, 0, ENOMEM);
7836 			return;
7837 		}
7838 		freemsg(data_mp);
7839 		data_mp = new_data_mp;
7840 		mp->b_cont = data_mp;
7841 	}
7842 	match_ire = MATCH_IRE_DSTONLY;
7843 
7844 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7845 	    end - cur >= sizeof (struct dstinforeq);
7846 	    cur += sizeof (struct dstinforeq)) {
7847 		dir = (struct dstinforeq *)cur;
7848 		daddr = &dir->dir_daddr;
7849 		saddr = &dir->dir_saddr;
7850 
7851 		/*
7852 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7853 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7854 		 * and ip_select_source_v6() do not.
7855 		 */
7856 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7857 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7858 
7859 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7860 		if (isipv4) {
7861 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7862 			v4setsrc = INADDR_ANY;
7863 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7864 			    NULL, match_ire, B_TRUE, 0, ipst, &v4setsrc, NULL,
7865 			    NULL);
7866 		} else {
7867 			v6setsrc = ipv6_all_zeros;
7868 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7869 			    NULL, match_ire, B_TRUE, 0, ipst, &v6setsrc, NULL,
7870 			    NULL);
7871 		}
7872 		ASSERT(ire != NULL);
7873 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7874 			ire_refrele(ire);
7875 			dir->dir_dreachable = 0;
7876 
7877 			/* move on to next dst addr */
7878 			continue;
7879 		}
7880 		dir->dir_dreachable = 1;
7881 
7882 		dst_ill = ire_nexthop_ill(ire);
7883 		if (dst_ill == NULL) {
7884 			ire_refrele(ire);
7885 			continue;
7886 		}
7887 
7888 		/* With ipmp we most likely look at the ipmp ill here */
7889 		dir->dir_dmactype = dst_ill->ill_mactype;
7890 
7891 		if (isipv4) {
7892 			ipaddr_t v4saddr;
7893 
7894 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7895 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7896 			    &v4saddr, NULL, &ipif_flags) != 0) {
7897 				v4saddr = INADDR_ANY;
7898 				ipif_flags = 0;
7899 			}
7900 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7901 		} else {
7902 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7903 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7904 			    saddr, NULL, &ipif_flags) != 0) {
7905 				*saddr = ipv6_all_zeros;
7906 				ipif_flags = 0;
7907 			}
7908 		}
7909 
7910 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7911 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7912 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7913 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7914 		ire_refrele(ire);
7915 		ill_refrele(dst_ill);
7916 	}
7917 	miocack(q, mp, iocp->ioc_count, 0);
7918 }
7919 
7920 /*
7921  * Check if this is an address assigned to this machine.
7922  * Skips interfaces that are down by using ire checks.
7923  * Translates mapped addresses to v4 addresses and then
7924  * treats them as such, returning true if the v4 address
7925  * associated with this mapped address is configured.
7926  * Note: Applications will have to be careful what they do
7927  * with the response; use of mapped addresses limits
7928  * what can be done with the socket, especially with
7929  * respect to socket options and ioctls - neither IPv4
7930  * options nor IPv6 sticky options/ancillary data options
7931  * may be used.
7932  */
7933 /* ARGSUSED */
7934 int
7935 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7936     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7937 {
7938 	struct sioc_addrreq *sia;
7939 	sin_t *sin;
7940 	ire_t *ire;
7941 	mblk_t *mp1;
7942 	zoneid_t zoneid;
7943 	ip_stack_t	*ipst;
7944 
7945 	ip1dbg(("ip_sioctl_tmyaddr"));
7946 
7947 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7948 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7949 	ipst = CONNQ_TO_IPST(q);
7950 
7951 	/* Existence verified in ip_wput_nondata */
7952 	mp1 = mp->b_cont->b_cont;
7953 	sia = (struct sioc_addrreq *)mp1->b_rptr;
7954 	sin = (sin_t *)&sia->sa_addr;
7955 	switch (sin->sin_family) {
7956 	case AF_INET6: {
7957 		sin6_t *sin6 = (sin6_t *)sin;
7958 
7959 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7960 			ipaddr_t v4_addr;
7961 
7962 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
7963 			    v4_addr);
7964 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
7965 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7966 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7967 		} else {
7968 			in6_addr_t v6addr;
7969 
7970 			v6addr = sin6->sin6_addr;
7971 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
7972 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7973 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7974 		}
7975 		break;
7976 	}
7977 	case AF_INET: {
7978 		ipaddr_t v4addr;
7979 
7980 		v4addr = sin->sin_addr.s_addr;
7981 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
7982 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
7983 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7984 		break;
7985 	}
7986 	default:
7987 		return (EAFNOSUPPORT);
7988 	}
7989 	if (ire != NULL) {
7990 		sia->sa_res = 1;
7991 		ire_refrele(ire);
7992 	} else {
7993 		sia->sa_res = 0;
7994 	}
7995 	return (0);
7996 }
7997 
7998 /*
7999  * Check if this is an address assigned on-link i.e. neighbor,
8000  * and makes sure it's reachable from the current zone.
8001  * Returns true for my addresses as well.
8002  * Translates mapped addresses to v4 addresses and then
8003  * treats them as such, returning true if the v4 address
8004  * associated with this mapped address is configured.
8005  * Note: Applications will have to be careful what they do
8006  * with the response; use of mapped addresses limits
8007  * what can be done with the socket, especially with
8008  * respect to socket options and ioctls - neither IPv4
8009  * options nor IPv6 sticky options/ancillary data options
8010  * may be used.
8011  */
8012 /* ARGSUSED */
8013 int
8014 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8015     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8016 {
8017 	struct sioc_addrreq *sia;
8018 	sin_t *sin;
8019 	mblk_t	*mp1;
8020 	ire_t *ire = NULL;
8021 	zoneid_t zoneid;
8022 	ip_stack_t	*ipst;
8023 
8024 	ip1dbg(("ip_sioctl_tonlink"));
8025 
8026 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8027 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8028 	ipst = CONNQ_TO_IPST(q);
8029 
8030 	/* Existence verified in ip_wput_nondata */
8031 	mp1 = mp->b_cont->b_cont;
8032 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8033 	sin = (sin_t *)&sia->sa_addr;
8034 
8035 	/*
8036 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8037 	 * to make sure we only look at on-link unicast address.
8038 	 */
8039 	switch (sin->sin_family) {
8040 	case AF_INET6: {
8041 		sin6_t *sin6 = (sin6_t *)sin;
8042 
8043 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8044 			ipaddr_t v4_addr;
8045 
8046 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8047 			    v4_addr);
8048 			if (!CLASSD(v4_addr)) {
8049 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8050 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8051 				    0, ipst, NULL);
8052 			}
8053 		} else {
8054 			in6_addr_t v6addr;
8055 
8056 			v6addr = sin6->sin6_addr;
8057 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8058 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8059 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8060 				    ipst, NULL);
8061 			}
8062 		}
8063 		break;
8064 	}
8065 	case AF_INET: {
8066 		ipaddr_t v4addr;
8067 
8068 		v4addr = sin->sin_addr.s_addr;
8069 		if (!CLASSD(v4addr)) {
8070 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8071 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8072 		}
8073 		break;
8074 	}
8075 	default:
8076 		return (EAFNOSUPPORT);
8077 	}
8078 	sia->sa_res = 0;
8079 	if (ire != NULL) {
8080 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8081 
8082 		if ((ire->ire_type & IRE_ONLINK) &&
8083 		    !(ire->ire_type & IRE_BROADCAST))
8084 			sia->sa_res = 1;
8085 		ire_refrele(ire);
8086 	}
8087 	return (0);
8088 }
8089 
8090 /*
8091  * TBD: implement when kernel maintaines a list of site prefixes.
8092  */
8093 /* ARGSUSED */
8094 int
8095 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8096     ip_ioctl_cmd_t *ipip, void *ifreq)
8097 {
8098 	return (ENXIO);
8099 }
8100 
8101 /* ARP IOCTLs. */
8102 /* ARGSUSED */
8103 int
8104 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8105     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8106 {
8107 	int		err;
8108 	ipaddr_t	ipaddr;
8109 	struct iocblk	*iocp;
8110 	conn_t		*connp;
8111 	struct arpreq	*ar;
8112 	struct xarpreq	*xar;
8113 	int		arp_flags, flags, alength;
8114 	uchar_t		*lladdr;
8115 	ip_stack_t	*ipst;
8116 	ill_t		*ill = ipif->ipif_ill;
8117 	ill_t		*proxy_ill = NULL;
8118 	ipmp_arpent_t	*entp = NULL;
8119 	boolean_t	proxyarp = B_FALSE;
8120 	boolean_t	if_arp_ioctl = B_FALSE;
8121 	ncec_t		*ncec = NULL;
8122 	nce_t		*nce;
8123 
8124 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8125 	connp = Q_TO_CONN(q);
8126 	ipst = connp->conn_netstack->netstack_ip;
8127 	iocp = (struct iocblk *)mp->b_rptr;
8128 
8129 	if (ipip->ipi_cmd_type == XARP_CMD) {
8130 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8131 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8132 		ar = NULL;
8133 
8134 		arp_flags = xar->xarp_flags;
8135 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8136 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8137 		/*
8138 		 * Validate against user's link layer address length
8139 		 * input and name and addr length limits.
8140 		 */
8141 		alength = ill->ill_phys_addr_length;
8142 		if (ipip->ipi_cmd == SIOCSXARP) {
8143 			if (alength != xar->xarp_ha.sdl_alen ||
8144 			    (alength + xar->xarp_ha.sdl_nlen >
8145 			    sizeof (xar->xarp_ha.sdl_data)))
8146 				return (EINVAL);
8147 		}
8148 	} else {
8149 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8150 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8151 		xar = NULL;
8152 
8153 		arp_flags = ar->arp_flags;
8154 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8155 		/*
8156 		 * Theoretically, the sa_family could tell us what link
8157 		 * layer type this operation is trying to deal with. By
8158 		 * common usage AF_UNSPEC means ethernet. We'll assume
8159 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8160 		 * for now. Our new SIOC*XARP ioctls can be used more
8161 		 * generally.
8162 		 *
8163 		 * If the underlying media happens to have a non 6 byte
8164 		 * address, arp module will fail set/get, but the del
8165 		 * operation will succeed.
8166 		 */
8167 		alength = 6;
8168 		if ((ipip->ipi_cmd != SIOCDARP) &&
8169 		    (alength != ill->ill_phys_addr_length)) {
8170 			return (EINVAL);
8171 		}
8172 	}
8173 
8174 	/* Translate ATF* flags to NCE* flags */
8175 	flags = 0;
8176 	if (arp_flags & ATF_AUTHORITY)
8177 		flags |= NCE_F_AUTHORITY;
8178 	if (arp_flags & ATF_PERM)
8179 		flags |= NCE_F_NONUD; /* not subject to aging */
8180 	if (arp_flags & ATF_PUBL)
8181 		flags |= NCE_F_PUBLISH;
8182 
8183 	/*
8184 	 * IPMP ARP special handling:
8185 	 *
8186 	 * 1. Since ARP mappings must appear consistent across the group,
8187 	 *    prohibit changing ARP mappings on the underlying interfaces.
8188 	 *
8189 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8190 	 *    IP itself, prohibit changing them.
8191 	 *
8192 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8193 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8194 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8195 	 */
8196 	if (IS_UNDER_IPMP(ill)) {
8197 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8198 			return (EPERM);
8199 	}
8200 	if (IS_IPMP(ill)) {
8201 		ipmp_illgrp_t *illg = ill->ill_grp;
8202 
8203 		switch (ipip->ipi_cmd) {
8204 		case SIOCSARP:
8205 		case SIOCSXARP:
8206 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8207 			if (proxy_ill != NULL) {
8208 				proxyarp = B_TRUE;
8209 				if (!ipmp_ill_is_active(proxy_ill))
8210 					proxy_ill = ipmp_illgrp_next_ill(illg);
8211 				if (proxy_ill != NULL)
8212 					lladdr = proxy_ill->ill_phys_addr;
8213 			}
8214 			/* FALLTHRU */
8215 		}
8216 	}
8217 
8218 	ipaddr = sin->sin_addr.s_addr;
8219 	/*
8220 	 * don't match across illgrp per case (1) and (2).
8221 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8222 	 */
8223 	nce = nce_lookup_v4(ill, &ipaddr);
8224 	if (nce != NULL)
8225 		ncec = nce->nce_common;
8226 
8227 	switch (iocp->ioc_cmd) {
8228 	case SIOCDARP:
8229 	case SIOCDXARP: {
8230 		/*
8231 		 * Delete the NCE if any.
8232 		 */
8233 		if (ncec == NULL) {
8234 			iocp->ioc_error = ENXIO;
8235 			break;
8236 		}
8237 		/* Don't allow changes to arp mappings of local addresses. */
8238 		if (NCE_MYADDR(ncec)) {
8239 			nce_refrele(nce);
8240 			return (ENOTSUP);
8241 		}
8242 		iocp->ioc_error = 0;
8243 
8244 		/*
8245 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8246 		 * This will delete all the nce entries on the under_ills.
8247 		 */
8248 		ncec_delete(ncec);
8249 		/*
8250 		 * Once the NCE has been deleted, then the ire_dep* consistency
8251 		 * mechanism will find any IRE which depended on the now
8252 		 * condemned NCE (as part of sending packets).
8253 		 * That mechanism handles redirects by deleting redirects
8254 		 * that refer to UNREACHABLE nces.
8255 		 */
8256 		break;
8257 	}
8258 	case SIOCGARP:
8259 	case SIOCGXARP:
8260 		if (ncec != NULL) {
8261 			lladdr = ncec->ncec_lladdr;
8262 			flags = ncec->ncec_flags;
8263 			iocp->ioc_error = 0;
8264 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8265 		} else {
8266 			iocp->ioc_error = ENXIO;
8267 		}
8268 		break;
8269 	case SIOCSARP:
8270 	case SIOCSXARP:
8271 		/* Don't allow changes to arp mappings of local addresses. */
8272 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8273 			nce_refrele(nce);
8274 			return (ENOTSUP);
8275 		}
8276 
8277 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8278 		flags |= NCE_F_STATIC;
8279 		if (!if_arp_ioctl) {
8280 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8281 			    lladdr, alength, flags);
8282 		} else {
8283 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8284 			if (ipif != NULL) {
8285 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8286 				    lladdr, alength, flags);
8287 				ipif_refrele(ipif);
8288 			}
8289 		}
8290 		if (nce != NULL) {
8291 			nce_refrele(nce);
8292 			nce = NULL;
8293 		}
8294 		/*
8295 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8296 		 * by nce_add_common()
8297 		 */
8298 		err = nce_lookup_then_add_v4(ill, lladdr,
8299 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8300 		    &nce);
8301 		if (err == EEXIST) {
8302 			ncec = nce->nce_common;
8303 			mutex_enter(&ncec->ncec_lock);
8304 			ncec->ncec_state = ND_REACHABLE;
8305 			ncec->ncec_flags = flags;
8306 			nce_update(ncec, ND_UNCHANGED, lladdr);
8307 			mutex_exit(&ncec->ncec_lock);
8308 			err = 0;
8309 		}
8310 		if (nce != NULL) {
8311 			nce_refrele(nce);
8312 			nce = NULL;
8313 		}
8314 		if (IS_IPMP(ill) && err == 0) {
8315 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8316 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8317 			    flags);
8318 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8319 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8320 				break;
8321 			}
8322 		}
8323 		iocp->ioc_error = err;
8324 	}
8325 
8326 	if (nce != NULL) {
8327 		nce_refrele(nce);
8328 	}
8329 
8330 	/*
8331 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8332 	 */
8333 	if (entp != NULL)
8334 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8335 
8336 	return (iocp->ioc_error);
8337 }
8338 
8339 /*
8340  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8341  * the associated sin and refhold and return the associated ipif via `ci'.
8342  */
8343 int
8344 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8345     cmd_info_t *ci)
8346 {
8347 	mblk_t	*mp1;
8348 	sin_t	*sin;
8349 	conn_t	*connp;
8350 	ipif_t	*ipif;
8351 	ire_t	*ire = NULL;
8352 	ill_t	*ill = NULL;
8353 	boolean_t exists;
8354 	ip_stack_t *ipst;
8355 	struct arpreq *ar;
8356 	struct xarpreq *xar;
8357 	struct sockaddr_dl *sdl;
8358 
8359 	/* ioctl comes down on a conn */
8360 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8361 	connp = Q_TO_CONN(q);
8362 	if (connp->conn_family == AF_INET6)
8363 		return (ENXIO);
8364 
8365 	ipst = connp->conn_netstack->netstack_ip;
8366 
8367 	/* Verified in ip_wput_nondata */
8368 	mp1 = mp->b_cont->b_cont;
8369 
8370 	if (ipip->ipi_cmd_type == XARP_CMD) {
8371 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8372 		xar = (struct xarpreq *)mp1->b_rptr;
8373 		sin = (sin_t *)&xar->xarp_pa;
8374 		sdl = &xar->xarp_ha;
8375 
8376 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8377 			return (ENXIO);
8378 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8379 			return (EINVAL);
8380 	} else {
8381 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8382 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8383 		ar = (struct arpreq *)mp1->b_rptr;
8384 		sin = (sin_t *)&ar->arp_pa;
8385 	}
8386 
8387 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8388 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8389 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8390 		if (ipif == NULL)
8391 			return (ENXIO);
8392 		if (ipif->ipif_id != 0) {
8393 			ipif_refrele(ipif);
8394 			return (ENXIO);
8395 		}
8396 	} else {
8397 		/*
8398 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8399 		 * of 0: use the IP address to find the ipif.  If the IP
8400 		 * address is an IPMP test address, ire_ftable_lookup() will
8401 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8402 		 */
8403 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8404 		    ipst);
8405 		if (ipif == NULL) {
8406 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8407 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8408 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8409 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8410 				if (ire != NULL)
8411 					ire_refrele(ire);
8412 				return (ENXIO);
8413 			}
8414 			ASSERT(ire != NULL && ill != NULL);
8415 			ipif = ill->ill_ipif;
8416 			ipif_refhold(ipif);
8417 			ire_refrele(ire);
8418 		}
8419 	}
8420 
8421 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8422 		ipif_refrele(ipif);
8423 		return (ENXIO);
8424 	}
8425 
8426 	ci->ci_sin = sin;
8427 	ci->ci_ipif = ipif;
8428 	return (0);
8429 }
8430 
8431 /*
8432  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8433  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8434  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8435  * up and thus an ill can join that illgrp.
8436  *
8437  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8438  * open()/close() primarily because close() is not allowed to fail or block
8439  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8440  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8441  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8442  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8443  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8444  * state if I_UNLINK didn't occur.
8445  *
8446  * Note that for each plumb/unplumb operation, we may end up here more than
8447  * once because of the way ifconfig works.  However, it's OK to link the same
8448  * illgrp more than once, or unlink an illgrp that's already unlinked.
8449  */
8450 static int
8451 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8452 {
8453 	int err;
8454 	ip_stack_t *ipst = ill->ill_ipst;
8455 
8456 	ASSERT(IS_IPMP(ill));
8457 	ASSERT(IAM_WRITER_ILL(ill));
8458 
8459 	switch (ioccmd) {
8460 	case I_LINK:
8461 		return (ENOTSUP);
8462 
8463 	case I_PLINK:
8464 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8465 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8466 		rw_exit(&ipst->ips_ipmp_lock);
8467 		break;
8468 
8469 	case I_PUNLINK:
8470 		/*
8471 		 * Require all UP ipifs be brought down prior to unlinking the
8472 		 * illgrp so any associated IREs (and other state) is torched.
8473 		 */
8474 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8475 			return (EBUSY);
8476 
8477 		/*
8478 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8479 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8480 		 * join this group.  Specifically: ills trying to join grab
8481 		 * ipmp_lock and bump a "pending join" counter checked by
8482 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8483 		 * joins can occur (since we have ipmp_lock).  Once we drop
8484 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8485 		 * find the illgrp (since we unlinked it) and will return
8486 		 * EAFNOSUPPORT.  This will then take them back through the
8487 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8488 		 * back through I_PLINK above.
8489 		 */
8490 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8491 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8492 		rw_exit(&ipst->ips_ipmp_lock);
8493 		return (err);
8494 	default:
8495 		break;
8496 	}
8497 	return (0);
8498 }
8499 
8500 /*
8501  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8502  * atomically set/clear the muxids. Also complete the ioctl by acking or
8503  * naking it.  Note that the code is structured such that the link type,
8504  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8505  * its clones use the persistent link, while pppd(1M) and perhaps many
8506  * other daemons may use non-persistent link.  When combined with some
8507  * ill_t states, linking and unlinking lower streams may be used as
8508  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8509  */
8510 /* ARGSUSED */
8511 void
8512 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8513 {
8514 	mblk_t		*mp1;
8515 	struct linkblk	*li;
8516 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8517 	int		err = 0;
8518 
8519 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8520 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8521 
8522 	mp1 = mp->b_cont;	/* This is the linkblk info */
8523 	li = (struct linkblk *)mp1->b_rptr;
8524 
8525 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8526 	if (err == EINPROGRESS)
8527 		return;
8528 done:
8529 	if (err == 0)
8530 		miocack(q, mp, 0, 0);
8531 	else
8532 		miocnak(q, mp, 0, err);
8533 
8534 	/* Conn was refheld in ip_sioctl_copyin_setup */
8535 	if (CONN_Q(q))
8536 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8537 }
8538 
8539 /*
8540  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8541  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8542  * module stream).  If `doconsist' is set, then do the extended consistency
8543  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8544  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8545  * an error code on failure.
8546  */
8547 static int
8548 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8549     struct linkblk *li)
8550 {
8551 	int		err = 0;
8552 	ill_t  		*ill;
8553 	queue_t		*ipwq, *dwq;
8554 	const char	*name;
8555 	struct qinit	*qinfo;
8556 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8557 	boolean_t	entered_ipsq = B_FALSE;
8558 	boolean_t	is_ip = B_FALSE;
8559 	arl_t		*arl;
8560 
8561 	/*
8562 	 * Walk the lower stream to verify it's the IP module stream.
8563 	 * The IP module is identified by its name, wput function,
8564 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8565 	 * (li->l_qbot) will not vanish until this ioctl completes.
8566 	 */
8567 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8568 		qinfo = ipwq->q_qinfo;
8569 		name = qinfo->qi_minfo->mi_idname;
8570 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8571 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8572 			is_ip = B_TRUE;
8573 			break;
8574 		}
8575 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8576 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8577 			break;
8578 		}
8579 	}
8580 
8581 	/*
8582 	 * If this isn't an IP module stream, bail.
8583 	 */
8584 	if (ipwq == NULL)
8585 		return (0);
8586 
8587 	if (!is_ip) {
8588 		arl = (arl_t *)ipwq->q_ptr;
8589 		ill = arl_to_ill(arl);
8590 		if (ill == NULL)
8591 			return (0);
8592 	} else {
8593 		ill = ipwq->q_ptr;
8594 	}
8595 	ASSERT(ill != NULL);
8596 
8597 	if (ipsq == NULL) {
8598 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8599 		    NEW_OP, B_FALSE);
8600 		if (ipsq == NULL) {
8601 			if (!is_ip)
8602 				ill_refrele(ill);
8603 			return (EINPROGRESS);
8604 		}
8605 		entered_ipsq = B_TRUE;
8606 	}
8607 	ASSERT(IAM_WRITER_ILL(ill));
8608 	mutex_enter(&ill->ill_lock);
8609 	if (!is_ip) {
8610 		if (islink && ill->ill_muxid == 0) {
8611 			/*
8612 			 * Plumbing has to be done with IP plumbed first, arp
8613 			 * second, but here we have arp being plumbed first.
8614 			 */
8615 			mutex_exit(&ill->ill_lock);
8616 			ipsq_exit(ipsq);
8617 			ill_refrele(ill);
8618 			return (EINVAL);
8619 		}
8620 	}
8621 	mutex_exit(&ill->ill_lock);
8622 	if (!is_ip) {
8623 		arl->arl_muxid = islink ? li->l_index : 0;
8624 		ill_refrele(ill);
8625 		goto done;
8626 	}
8627 
8628 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8629 		goto done;
8630 
8631 	/*
8632 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8633 	 * the read queue of the module immediately below IP in the ill.
8634 	 * These are used during the capability negotiation below.
8635 	 */
8636 	ill->ill_lmod_rq = NULL;
8637 	ill->ill_lmod_cnt = 0;
8638 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8639 		ill->ill_lmod_rq = RD(dwq);
8640 		for (; dwq != NULL; dwq = dwq->q_next)
8641 			ill->ill_lmod_cnt++;
8642 	}
8643 
8644 	ill->ill_muxid = islink ? li->l_index : 0;
8645 
8646 	/*
8647 	 * Mark the ipsq busy until the capability operations initiated below
8648 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8649 	 * returns, but the capability operation may complete asynchronously
8650 	 * much later.
8651 	 */
8652 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8653 	/*
8654 	 * If there's at least one up ipif on this ill, then we're bound to
8655 	 * the underlying driver via DLPI.  In that case, renegotiate
8656 	 * capabilities to account for any possible change in modules
8657 	 * interposed between IP and the driver.
8658 	 */
8659 	if (ill->ill_ipif_up_count > 0) {
8660 		if (islink)
8661 			ill_capability_probe(ill);
8662 		else
8663 			ill_capability_reset(ill, B_FALSE);
8664 	}
8665 	ipsq_current_finish(ipsq);
8666 done:
8667 	if (entered_ipsq)
8668 		ipsq_exit(ipsq);
8669 
8670 	return (err);
8671 }
8672 
8673 /*
8674  * Search the ioctl command in the ioctl tables and return a pointer
8675  * to the ioctl command information. The ioctl command tables are
8676  * static and fully populated at compile time.
8677  */
8678 ip_ioctl_cmd_t *
8679 ip_sioctl_lookup(int ioc_cmd)
8680 {
8681 	int index;
8682 	ip_ioctl_cmd_t *ipip;
8683 	ip_ioctl_cmd_t *ipip_end;
8684 
8685 	if (ioc_cmd == IPI_DONTCARE)
8686 		return (NULL);
8687 
8688 	/*
8689 	 * Do a 2 step search. First search the indexed table
8690 	 * based on the least significant byte of the ioctl cmd.
8691 	 * If we don't find a match, then search the misc table
8692 	 * serially.
8693 	 */
8694 	index = ioc_cmd & 0xFF;
8695 	if (index < ip_ndx_ioctl_count) {
8696 		ipip = &ip_ndx_ioctl_table[index];
8697 		if (ipip->ipi_cmd == ioc_cmd) {
8698 			/* Found a match in the ndx table */
8699 			return (ipip);
8700 		}
8701 	}
8702 
8703 	/* Search the misc table */
8704 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8705 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8706 		if (ipip->ipi_cmd == ioc_cmd)
8707 			/* Found a match in the misc table */
8708 			return (ipip);
8709 	}
8710 
8711 	return (NULL);
8712 }
8713 
8714 /*
8715  * Wrapper function for resuming deferred ioctl processing
8716  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8717  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8718  */
8719 /* ARGSUSED */
8720 void
8721 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8722     void *dummy_arg)
8723 {
8724 	ip_sioctl_copyin_setup(q, mp);
8725 }
8726 
8727 /*
8728  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8729  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8730  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8731  * We establish here the size of the block to be copied in.  mi_copyin
8732  * arranges for this to happen, an processing continues in ip_wput_nondata with
8733  * an M_IOCDATA message.
8734  */
8735 void
8736 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8737 {
8738 	int	copyin_size;
8739 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8740 	ip_ioctl_cmd_t *ipip;
8741 	cred_t *cr;
8742 	ip_stack_t	*ipst;
8743 
8744 	if (CONN_Q(q))
8745 		ipst = CONNQ_TO_IPST(q);
8746 	else
8747 		ipst = ILLQ_TO_IPST(q);
8748 
8749 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8750 	if (ipip == NULL) {
8751 		/*
8752 		 * The ioctl is not one we understand or own.
8753 		 * Pass it along to be processed down stream,
8754 		 * if this is a module instance of IP, else nak
8755 		 * the ioctl.
8756 		 */
8757 		if (q->q_next == NULL) {
8758 			goto nak;
8759 		} else {
8760 			putnext(q, mp);
8761 			return;
8762 		}
8763 	}
8764 
8765 	/*
8766 	 * If this is deferred, then we will do all the checks when we
8767 	 * come back.
8768 	 */
8769 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8770 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8771 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8772 		return;
8773 	}
8774 
8775 	/*
8776 	 * Only allow a very small subset of IP ioctls on this stream if
8777 	 * IP is a module and not a driver. Allowing ioctls to be processed
8778 	 * in this case may cause assert failures or data corruption.
8779 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8780 	 * ioctls allowed on an IP module stream, after which this stream
8781 	 * normally becomes a multiplexor (at which time the stream head
8782 	 * will fail all ioctls).
8783 	 */
8784 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8785 		goto nak;
8786 	}
8787 
8788 	/* Make sure we have ioctl data to process. */
8789 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8790 		goto nak;
8791 
8792 	/*
8793 	 * Prefer dblk credential over ioctl credential; some synthesized
8794 	 * ioctls have kcred set because there's no way to crhold()
8795 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8796 	 * the framework; the caller of ioctl needs to hold the reference
8797 	 * for the duration of the call).
8798 	 */
8799 	cr = msg_getcred(mp, NULL);
8800 	if (cr == NULL)
8801 		cr = iocp->ioc_cr;
8802 
8803 	/* Make sure normal users don't send down privileged ioctls */
8804 	if ((ipip->ipi_flags & IPI_PRIV) &&
8805 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8806 		/* We checked the privilege earlier but log it here */
8807 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8808 		return;
8809 	}
8810 
8811 	/*
8812 	 * The ioctl command tables can only encode fixed length
8813 	 * ioctl data. If the length is variable, the table will
8814 	 * encode the length as zero. Such special cases are handled
8815 	 * below in the switch.
8816 	 */
8817 	if (ipip->ipi_copyin_size != 0) {
8818 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8819 		return;
8820 	}
8821 
8822 	switch (iocp->ioc_cmd) {
8823 	case O_SIOCGIFCONF:
8824 	case SIOCGIFCONF:
8825 		/*
8826 		 * This IOCTL is hilarious.  See comments in
8827 		 * ip_sioctl_get_ifconf for the story.
8828 		 */
8829 		if (iocp->ioc_count == TRANSPARENT)
8830 			copyin_size = SIZEOF_STRUCT(ifconf,
8831 			    iocp->ioc_flag);
8832 		else
8833 			copyin_size = iocp->ioc_count;
8834 		mi_copyin(q, mp, NULL, copyin_size);
8835 		return;
8836 
8837 	case O_SIOCGLIFCONF:
8838 	case SIOCGLIFCONF:
8839 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8840 		mi_copyin(q, mp, NULL, copyin_size);
8841 		return;
8842 
8843 	case SIOCGLIFSRCOF:
8844 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8845 		mi_copyin(q, mp, NULL, copyin_size);
8846 		return;
8847 	case SIOCGIP6ADDRPOLICY:
8848 		ip_sioctl_ip6addrpolicy(q, mp);
8849 		ip6_asp_table_refrele(ipst);
8850 		return;
8851 
8852 	case SIOCSIP6ADDRPOLICY:
8853 		ip_sioctl_ip6addrpolicy(q, mp);
8854 		return;
8855 
8856 	case SIOCGDSTINFO:
8857 		ip_sioctl_dstinfo(q, mp);
8858 		ip6_asp_table_refrele(ipst);
8859 		return;
8860 
8861 	case I_PLINK:
8862 	case I_PUNLINK:
8863 	case I_LINK:
8864 	case I_UNLINK:
8865 		/*
8866 		 * We treat non-persistent link similarly as the persistent
8867 		 * link case, in terms of plumbing/unplumbing, as well as
8868 		 * dynamic re-plumbing events indicator.  See comments
8869 		 * in ip_sioctl_plink() for more.
8870 		 *
8871 		 * Request can be enqueued in the 'ipsq' while waiting
8872 		 * to become exclusive. So bump up the conn ref.
8873 		 */
8874 		if (CONN_Q(q))
8875 			CONN_INC_REF(Q_TO_CONN(q));
8876 		ip_sioctl_plink(NULL, q, mp, NULL);
8877 		return;
8878 
8879 	case ND_GET:
8880 	case ND_SET:
8881 		/*
8882 		 * Use of the nd table requires holding the reader lock.
8883 		 * Modifying the nd table thru nd_load/nd_unload requires
8884 		 * the writer lock.
8885 		 */
8886 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8887 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8888 			rw_exit(&ipst->ips_ip_g_nd_lock);
8889 
8890 			if (iocp->ioc_error)
8891 				iocp->ioc_count = 0;
8892 			mp->b_datap->db_type = M_IOCACK;
8893 			qreply(q, mp);
8894 			return;
8895 		}
8896 		rw_exit(&ipst->ips_ip_g_nd_lock);
8897 		/*
8898 		 * We don't understand this subioctl of ND_GET / ND_SET.
8899 		 * Maybe intended for some driver / module below us
8900 		 */
8901 		if (q->q_next) {
8902 			putnext(q, mp);
8903 		} else {
8904 			iocp->ioc_error = ENOENT;
8905 			mp->b_datap->db_type = M_IOCNAK;
8906 			iocp->ioc_count = 0;
8907 			qreply(q, mp);
8908 		}
8909 		return;
8910 
8911 	case IP_IOCTL:
8912 		ip_wput_ioctl(q, mp);
8913 		return;
8914 
8915 	case SIOCILB:
8916 		/* The ioctl length varies depending on the ILB command. */
8917 		copyin_size = iocp->ioc_count;
8918 		if (copyin_size < sizeof (ilb_cmd_t))
8919 			goto nak;
8920 		mi_copyin(q, mp, NULL, copyin_size);
8921 		return;
8922 
8923 	default:
8924 		cmn_err(CE_PANIC, "should not happen ");
8925 	}
8926 nak:
8927 	if (mp->b_cont != NULL) {
8928 		freemsg(mp->b_cont);
8929 		mp->b_cont = NULL;
8930 	}
8931 	iocp->ioc_error = EINVAL;
8932 	mp->b_datap->db_type = M_IOCNAK;
8933 	iocp->ioc_count = 0;
8934 	qreply(q, mp);
8935 }
8936 
8937 static void
8938 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
8939 {
8940 	struct arpreq *ar;
8941 	struct xarpreq *xar;
8942 	mblk_t	*tmp;
8943 	struct iocblk *iocp;
8944 	int x_arp_ioctl = B_FALSE;
8945 	int *flagsp;
8946 	char *storage = NULL;
8947 
8948 	ASSERT(ill != NULL);
8949 
8950 	iocp = (struct iocblk *)mp->b_rptr;
8951 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
8952 
8953 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
8954 	if ((iocp->ioc_cmd == SIOCGXARP) ||
8955 	    (iocp->ioc_cmd == SIOCSXARP)) {
8956 		x_arp_ioctl = B_TRUE;
8957 		xar = (struct xarpreq *)tmp->b_rptr;
8958 		flagsp = &xar->xarp_flags;
8959 		storage = xar->xarp_ha.sdl_data;
8960 	} else {
8961 		ar = (struct arpreq *)tmp->b_rptr;
8962 		flagsp = &ar->arp_flags;
8963 		storage = ar->arp_ha.sa_data;
8964 	}
8965 
8966 	/*
8967 	 * We're done if this is not an SIOCG{X}ARP
8968 	 */
8969 	if (x_arp_ioctl) {
8970 		storage += ill_xarp_info(&xar->xarp_ha, ill);
8971 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
8972 		    sizeof (xar->xarp_ha.sdl_data)) {
8973 			iocp->ioc_error = EINVAL;
8974 			return;
8975 		}
8976 	}
8977 	*flagsp = ATF_INUSE;
8978 	/*
8979 	 * If /sbin/arp told us we are the authority using the "permanent"
8980 	 * flag, or if this is one of my addresses print "permanent"
8981 	 * in the /sbin/arp output.
8982 	 */
8983 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
8984 		*flagsp |= ATF_AUTHORITY;
8985 	if (flags & NCE_F_NONUD)
8986 		*flagsp |= ATF_PERM; /* not subject to aging */
8987 	if (flags & NCE_F_PUBLISH)
8988 		*flagsp |= ATF_PUBL;
8989 	if (hwaddr != NULL) {
8990 		*flagsp |= ATF_COM;
8991 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
8992 	}
8993 }
8994 
8995 /*
8996  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
8997  * interface) create the next available logical interface for this
8998  * physical interface.
8999  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9000  * ipif with the specified name.
9001  *
9002  * If the address family is not AF_UNSPEC then set the address as well.
9003  *
9004  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9005  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9006  *
9007  * Executed as a writer on the ill.
9008  * So no lock is needed to traverse the ipif chain, or examine the
9009  * phyint flags.
9010  */
9011 /* ARGSUSED */
9012 int
9013 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9014     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9015 {
9016 	mblk_t	*mp1;
9017 	struct lifreq *lifr;
9018 	boolean_t	isv6;
9019 	boolean_t	exists;
9020 	char 	*name;
9021 	char	*endp;
9022 	char	*cp;
9023 	int	namelen;
9024 	ipif_t	*ipif;
9025 	long	id;
9026 	ipsq_t	*ipsq;
9027 	ill_t	*ill;
9028 	sin_t	*sin;
9029 	int	err = 0;
9030 	boolean_t found_sep = B_FALSE;
9031 	conn_t	*connp;
9032 	zoneid_t zoneid;
9033 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9034 
9035 	ASSERT(q->q_next == NULL);
9036 	ip1dbg(("ip_sioctl_addif\n"));
9037 	/* Existence of mp1 has been checked in ip_wput_nondata */
9038 	mp1 = mp->b_cont->b_cont;
9039 	/*
9040 	 * Null terminate the string to protect against buffer
9041 	 * overrun. String was generated by user code and may not
9042 	 * be trusted.
9043 	 */
9044 	lifr = (struct lifreq *)mp1->b_rptr;
9045 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9046 	name = lifr->lifr_name;
9047 	ASSERT(CONN_Q(q));
9048 	connp = Q_TO_CONN(q);
9049 	isv6 = (connp->conn_family == AF_INET6);
9050 	zoneid = connp->conn_zoneid;
9051 	namelen = mi_strlen(name);
9052 	if (namelen == 0)
9053 		return (EINVAL);
9054 
9055 	exists = B_FALSE;
9056 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9057 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9058 		/*
9059 		 * Allow creating lo0 using SIOCLIFADDIF.
9060 		 * can't be any other writer thread. So can pass null below
9061 		 * for the last 4 args to ipif_lookup_name.
9062 		 */
9063 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9064 		    &exists, isv6, zoneid, ipst);
9065 		/* Prevent any further action */
9066 		if (ipif == NULL) {
9067 			return (ENOBUFS);
9068 		} else if (!exists) {
9069 			/* We created the ipif now and as writer */
9070 			ipif_refrele(ipif);
9071 			return (0);
9072 		} else {
9073 			ill = ipif->ipif_ill;
9074 			ill_refhold(ill);
9075 			ipif_refrele(ipif);
9076 		}
9077 	} else {
9078 		/* Look for a colon in the name. */
9079 		endp = &name[namelen];
9080 		for (cp = endp; --cp > name; ) {
9081 			if (*cp == IPIF_SEPARATOR_CHAR) {
9082 				found_sep = B_TRUE;
9083 				/*
9084 				 * Reject any non-decimal aliases for plumbing
9085 				 * of logical interfaces. Aliases with leading
9086 				 * zeroes are also rejected as they introduce
9087 				 * ambiguity in the naming of the interfaces.
9088 				 * Comparing with "0" takes care of all such
9089 				 * cases.
9090 				 */
9091 				if ((strncmp("0", cp+1, 1)) == 0)
9092 					return (EINVAL);
9093 
9094 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9095 				    id <= 0 || *endp != '\0') {
9096 					return (EINVAL);
9097 				}
9098 				*cp = '\0';
9099 				break;
9100 			}
9101 		}
9102 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9103 		if (found_sep)
9104 			*cp = IPIF_SEPARATOR_CHAR;
9105 		if (ill == NULL)
9106 			return (ENXIO);
9107 	}
9108 
9109 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9110 	    B_TRUE);
9111 
9112 	/*
9113 	 * Release the refhold due to the lookup, now that we are excl
9114 	 * or we are just returning
9115 	 */
9116 	ill_refrele(ill);
9117 
9118 	if (ipsq == NULL)
9119 		return (EINPROGRESS);
9120 
9121 	/* We are now exclusive on the IPSQ */
9122 	ASSERT(IAM_WRITER_ILL(ill));
9123 
9124 	if (found_sep) {
9125 		/* Now see if there is an IPIF with this unit number. */
9126 		for (ipif = ill->ill_ipif; ipif != NULL;
9127 		    ipif = ipif->ipif_next) {
9128 			if (ipif->ipif_id == id) {
9129 				err = EEXIST;
9130 				goto done;
9131 			}
9132 		}
9133 	}
9134 
9135 	/*
9136 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9137 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9138 	 * instead.
9139 	 */
9140 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9141 	    B_TRUE, B_TRUE)) == NULL) {
9142 		err = ENOBUFS;
9143 		goto done;
9144 	}
9145 
9146 	/* Return created name with ioctl */
9147 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9148 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9149 	ip1dbg(("created %s\n", lifr->lifr_name));
9150 
9151 	/* Set address */
9152 	sin = (sin_t *)&lifr->lifr_addr;
9153 	if (sin->sin_family != AF_UNSPEC) {
9154 		err = ip_sioctl_addr(ipif, sin, q, mp,
9155 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9156 	}
9157 
9158 done:
9159 	ipsq_exit(ipsq);
9160 	return (err);
9161 }
9162 
9163 /*
9164  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9165  * interface) delete it based on the IP address (on this physical interface).
9166  * Otherwise delete it based on the ipif_id.
9167  * Also, special handling to allow a removeif of lo0.
9168  */
9169 /* ARGSUSED */
9170 int
9171 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9172     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9173 {
9174 	conn_t		*connp;
9175 	ill_t		*ill = ipif->ipif_ill;
9176 	boolean_t	 success;
9177 	ip_stack_t	*ipst;
9178 
9179 	ipst = CONNQ_TO_IPST(q);
9180 
9181 	ASSERT(q->q_next == NULL);
9182 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9183 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9184 	ASSERT(IAM_WRITER_IPIF(ipif));
9185 
9186 	connp = Q_TO_CONN(q);
9187 	/*
9188 	 * Special case for unplumbing lo0 (the loopback physical interface).
9189 	 * If unplumbing lo0, the incoming address structure has been
9190 	 * initialized to all zeros. When unplumbing lo0, all its logical
9191 	 * interfaces must be removed too.
9192 	 *
9193 	 * Note that this interface may be called to remove a specific
9194 	 * loopback logical interface (eg, lo0:1). But in that case
9195 	 * ipif->ipif_id != 0 so that the code path for that case is the
9196 	 * same as any other interface (meaning it skips the code directly
9197 	 * below).
9198 	 */
9199 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9200 		if (sin->sin_family == AF_UNSPEC &&
9201 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9202 			/*
9203 			 * Mark it condemned. No new ref. will be made to ill.
9204 			 */
9205 			mutex_enter(&ill->ill_lock);
9206 			ill->ill_state_flags |= ILL_CONDEMNED;
9207 			for (ipif = ill->ill_ipif; ipif != NULL;
9208 			    ipif = ipif->ipif_next) {
9209 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9210 			}
9211 			mutex_exit(&ill->ill_lock);
9212 
9213 			ipif = ill->ill_ipif;
9214 			/* unplumb the loopback interface */
9215 			ill_delete(ill);
9216 			mutex_enter(&connp->conn_lock);
9217 			mutex_enter(&ill->ill_lock);
9218 
9219 			/* Are any references to this ill active */
9220 			if (ill_is_freeable(ill)) {
9221 				mutex_exit(&ill->ill_lock);
9222 				mutex_exit(&connp->conn_lock);
9223 				ill_delete_tail(ill);
9224 				mi_free(ill);
9225 				return (0);
9226 			}
9227 			success = ipsq_pending_mp_add(connp, ipif,
9228 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9229 			mutex_exit(&connp->conn_lock);
9230 			mutex_exit(&ill->ill_lock);
9231 			if (success)
9232 				return (EINPROGRESS);
9233 			else
9234 				return (EINTR);
9235 		}
9236 	}
9237 
9238 	if (ipif->ipif_id == 0) {
9239 		ipsq_t *ipsq;
9240 
9241 		/* Find based on address */
9242 		if (ipif->ipif_isv6) {
9243 			sin6_t *sin6;
9244 
9245 			if (sin->sin_family != AF_INET6)
9246 				return (EAFNOSUPPORT);
9247 
9248 			sin6 = (sin6_t *)sin;
9249 			/* We are a writer, so we should be able to lookup */
9250 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9251 			    ipst);
9252 		} else {
9253 			if (sin->sin_family != AF_INET)
9254 				return (EAFNOSUPPORT);
9255 
9256 			/* We are a writer, so we should be able to lookup */
9257 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9258 			    ipst);
9259 		}
9260 		if (ipif == NULL) {
9261 			return (EADDRNOTAVAIL);
9262 		}
9263 
9264 		/*
9265 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9266 		 * lifr_name of the physical interface but with an ip address
9267 		 * lifr_addr of a logical interface plumbed over it.
9268 		 * So update ipx_current_ipif now that ipif points to the
9269 		 * correct one.
9270 		 */
9271 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9272 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9273 
9274 		/* This is a writer */
9275 		ipif_refrele(ipif);
9276 	}
9277 
9278 	/*
9279 	 * Can not delete instance zero since it is tied to the ill.
9280 	 */
9281 	if (ipif->ipif_id == 0)
9282 		return (EBUSY);
9283 
9284 	mutex_enter(&ill->ill_lock);
9285 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9286 	mutex_exit(&ill->ill_lock);
9287 
9288 	ipif_free(ipif);
9289 
9290 	mutex_enter(&connp->conn_lock);
9291 	mutex_enter(&ill->ill_lock);
9292 
9293 	/* Are any references to this ipif active */
9294 	if (ipif_is_freeable(ipif)) {
9295 		mutex_exit(&ill->ill_lock);
9296 		mutex_exit(&connp->conn_lock);
9297 		ipif_non_duplicate(ipif);
9298 		(void) ipif_down_tail(ipif);
9299 		ipif_free_tail(ipif); /* frees ipif */
9300 		return (0);
9301 	}
9302 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9303 	    IPIF_FREE);
9304 	mutex_exit(&ill->ill_lock);
9305 	mutex_exit(&connp->conn_lock);
9306 	if (success)
9307 		return (EINPROGRESS);
9308 	else
9309 		return (EINTR);
9310 }
9311 
9312 /*
9313  * Restart the removeif ioctl. The refcnt has gone down to 0.
9314  * The ipif is already condemned. So can't find it thru lookups.
9315  */
9316 /* ARGSUSED */
9317 int
9318 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9319     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9320 {
9321 	ill_t *ill = ipif->ipif_ill;
9322 
9323 	ASSERT(IAM_WRITER_IPIF(ipif));
9324 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9325 
9326 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9327 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9328 
9329 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9330 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9331 		ill_delete_tail(ill);
9332 		mi_free(ill);
9333 		return (0);
9334 	}
9335 
9336 	ipif_non_duplicate(ipif);
9337 	(void) ipif_down_tail(ipif);
9338 	ipif_free_tail(ipif);
9339 
9340 	return (0);
9341 }
9342 
9343 /*
9344  * Set the local interface address.
9345  * Allow an address of all zero when the interface is down.
9346  */
9347 /* ARGSUSED */
9348 int
9349 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9350     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9351 {
9352 	int err = 0;
9353 	in6_addr_t v6addr;
9354 	boolean_t need_up = B_FALSE;
9355 
9356 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9357 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9358 
9359 	ASSERT(IAM_WRITER_IPIF(ipif));
9360 
9361 	if (ipif->ipif_isv6) {
9362 		sin6_t *sin6;
9363 		ill_t *ill;
9364 		phyint_t *phyi;
9365 
9366 		if (sin->sin_family != AF_INET6)
9367 			return (EAFNOSUPPORT);
9368 
9369 		sin6 = (sin6_t *)sin;
9370 		v6addr = sin6->sin6_addr;
9371 		ill = ipif->ipif_ill;
9372 		phyi = ill->ill_phyint;
9373 
9374 		/*
9375 		 * Enforce that true multicast interfaces have a link-local
9376 		 * address for logical unit 0.
9377 		 */
9378 		if (ipif->ipif_id == 0 &&
9379 		    (ill->ill_flags & ILLF_MULTICAST) &&
9380 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9381 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9382 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9383 			return (EADDRNOTAVAIL);
9384 		}
9385 
9386 		/*
9387 		 * up interfaces shouldn't have the unspecified address
9388 		 * unless they also have the IPIF_NOLOCAL flags set and
9389 		 * have a subnet assigned.
9390 		 */
9391 		if ((ipif->ipif_flags & IPIF_UP) &&
9392 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9393 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9394 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9395 			return (EADDRNOTAVAIL);
9396 		}
9397 
9398 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9399 			return (EADDRNOTAVAIL);
9400 	} else {
9401 		ipaddr_t addr;
9402 
9403 		if (sin->sin_family != AF_INET)
9404 			return (EAFNOSUPPORT);
9405 
9406 		addr = sin->sin_addr.s_addr;
9407 
9408 		/* Allow 0 as the local address. */
9409 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9410 			return (EADDRNOTAVAIL);
9411 
9412 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9413 	}
9414 
9415 	/*
9416 	 * Even if there is no change we redo things just to rerun
9417 	 * ipif_set_default.
9418 	 */
9419 	if (ipif->ipif_flags & IPIF_UP) {
9420 		/*
9421 		 * Setting a new local address, make sure
9422 		 * we have net and subnet bcast ire's for
9423 		 * the old address if we need them.
9424 		 */
9425 		/*
9426 		 * If the interface is already marked up,
9427 		 * we call ipif_down which will take care
9428 		 * of ditching any IREs that have been set
9429 		 * up based on the old interface address.
9430 		 */
9431 		err = ipif_logical_down(ipif, q, mp);
9432 		if (err == EINPROGRESS)
9433 			return (err);
9434 		(void) ipif_down_tail(ipif);
9435 		need_up = 1;
9436 	}
9437 
9438 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9439 	return (err);
9440 }
9441 
9442 int
9443 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9444     boolean_t need_up)
9445 {
9446 	in6_addr_t v6addr;
9447 	in6_addr_t ov6addr;
9448 	ipaddr_t addr;
9449 	sin6_t	*sin6;
9450 	int	sinlen;
9451 	int	err = 0;
9452 	ill_t	*ill = ipif->ipif_ill;
9453 	boolean_t need_dl_down;
9454 	boolean_t need_arp_down;
9455 	struct iocblk *iocp;
9456 
9457 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9458 
9459 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9460 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9461 	ASSERT(IAM_WRITER_IPIF(ipif));
9462 
9463 	/* Must cancel any pending timer before taking the ill_lock */
9464 	if (ipif->ipif_recovery_id != 0)
9465 		(void) untimeout(ipif->ipif_recovery_id);
9466 	ipif->ipif_recovery_id = 0;
9467 
9468 	if (ipif->ipif_isv6) {
9469 		sin6 = (sin6_t *)sin;
9470 		v6addr = sin6->sin6_addr;
9471 		sinlen = sizeof (struct sockaddr_in6);
9472 	} else {
9473 		addr = sin->sin_addr.s_addr;
9474 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9475 		sinlen = sizeof (struct sockaddr_in);
9476 	}
9477 	mutex_enter(&ill->ill_lock);
9478 	ov6addr = ipif->ipif_v6lcl_addr;
9479 	ipif->ipif_v6lcl_addr = v6addr;
9480 	sctp_update_ipif_addr(ipif, ov6addr);
9481 	ipif->ipif_addr_ready = 0;
9482 
9483 	/*
9484 	 * If the interface was previously marked as a duplicate, then since
9485 	 * we've now got a "new" address, it should no longer be considered a
9486 	 * duplicate -- even if the "new" address is the same as the old one.
9487 	 * Note that if all ipifs are down, we may have a pending ARP down
9488 	 * event to handle.  This is because we want to recover from duplicates
9489 	 * and thus delay tearing down ARP until the duplicates have been
9490 	 * removed or disabled.
9491 	 */
9492 	need_dl_down = need_arp_down = B_FALSE;
9493 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9494 		need_arp_down = !need_up;
9495 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9496 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9497 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9498 			need_dl_down = B_TRUE;
9499 		}
9500 	}
9501 
9502 	ipif_set_default(ipif);
9503 
9504 	/*
9505 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9506 	 * tag the ill so that future updates to the interface ID don't result
9507 	 * in this address getting automatically reconfigured from under the
9508 	 * administrator.
9509 	 */
9510 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9511 		ill->ill_manual_linklocal = 1;
9512 
9513 	/*
9514 	 * When publishing an interface address change event, we only notify
9515 	 * the event listeners of the new address.  It is assumed that if they
9516 	 * actively care about the addresses assigned that they will have
9517 	 * already discovered the previous address assigned (if there was one.)
9518 	 *
9519 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9520 	 */
9521 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9522 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9523 		    NE_ADDRESS_CHANGE, sin, sinlen);
9524 	}
9525 
9526 	mutex_exit(&ill->ill_lock);
9527 
9528 	if (need_up) {
9529 		/*
9530 		 * Now bring the interface back up.  If this
9531 		 * is the only IPIF for the ILL, ipif_up
9532 		 * will have to re-bind to the device, so
9533 		 * we may get back EINPROGRESS, in which
9534 		 * case, this IOCTL will get completed in
9535 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9536 		 */
9537 		err = ipif_up(ipif, q, mp);
9538 	} else {
9539 		/* Perhaps ilgs should use this ill */
9540 		update_conn_ill(NULL, ill->ill_ipst);
9541 	}
9542 
9543 	if (need_dl_down)
9544 		ill_dl_down(ill);
9545 
9546 	if (need_arp_down && !ill->ill_isv6)
9547 		(void) ipif_arp_down(ipif);
9548 
9549 	/*
9550 	 * The default multicast interface might have changed (for
9551 	 * instance if the IPv6 scope of the address changed)
9552 	 */
9553 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9554 
9555 	return (err);
9556 }
9557 
9558 /*
9559  * Restart entry point to restart the address set operation after the
9560  * refcounts have dropped to zero.
9561  */
9562 /* ARGSUSED */
9563 int
9564 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9565     ip_ioctl_cmd_t *ipip, void *ifreq)
9566 {
9567 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9568 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9569 	ASSERT(IAM_WRITER_IPIF(ipif));
9570 	(void) ipif_down_tail(ipif);
9571 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9572 }
9573 
9574 /* ARGSUSED */
9575 int
9576 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9577     ip_ioctl_cmd_t *ipip, void *if_req)
9578 {
9579 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9580 	struct lifreq *lifr = (struct lifreq *)if_req;
9581 
9582 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9583 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9584 	/*
9585 	 * The net mask and address can't change since we have a
9586 	 * reference to the ipif. So no lock is necessary.
9587 	 */
9588 	if (ipif->ipif_isv6) {
9589 		*sin6 = sin6_null;
9590 		sin6->sin6_family = AF_INET6;
9591 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9592 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9593 		lifr->lifr_addrlen =
9594 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9595 	} else {
9596 		*sin = sin_null;
9597 		sin->sin_family = AF_INET;
9598 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9599 		if (ipip->ipi_cmd_type == LIF_CMD) {
9600 			lifr->lifr_addrlen =
9601 			    ip_mask_to_plen(ipif->ipif_net_mask);
9602 		}
9603 	}
9604 	return (0);
9605 }
9606 
9607 /*
9608  * Set the destination address for a pt-pt interface.
9609  */
9610 /* ARGSUSED */
9611 int
9612 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9613     ip_ioctl_cmd_t *ipip, void *if_req)
9614 {
9615 	int err = 0;
9616 	in6_addr_t v6addr;
9617 	boolean_t need_up = B_FALSE;
9618 
9619 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9620 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9621 	ASSERT(IAM_WRITER_IPIF(ipif));
9622 
9623 	if (ipif->ipif_isv6) {
9624 		sin6_t *sin6;
9625 
9626 		if (sin->sin_family != AF_INET6)
9627 			return (EAFNOSUPPORT);
9628 
9629 		sin6 = (sin6_t *)sin;
9630 		v6addr = sin6->sin6_addr;
9631 
9632 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9633 			return (EADDRNOTAVAIL);
9634 	} else {
9635 		ipaddr_t addr;
9636 
9637 		if (sin->sin_family != AF_INET)
9638 			return (EAFNOSUPPORT);
9639 
9640 		addr = sin->sin_addr.s_addr;
9641 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9642 			return (EADDRNOTAVAIL);
9643 
9644 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9645 	}
9646 
9647 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9648 		return (0);	/* No change */
9649 
9650 	if (ipif->ipif_flags & IPIF_UP) {
9651 		/*
9652 		 * If the interface is already marked up,
9653 		 * we call ipif_down which will take care
9654 		 * of ditching any IREs that have been set
9655 		 * up based on the old pp dst address.
9656 		 */
9657 		err = ipif_logical_down(ipif, q, mp);
9658 		if (err == EINPROGRESS)
9659 			return (err);
9660 		(void) ipif_down_tail(ipif);
9661 		need_up = B_TRUE;
9662 	}
9663 	/*
9664 	 * could return EINPROGRESS. If so ioctl will complete in
9665 	 * ip_rput_dlpi_writer
9666 	 */
9667 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9668 	return (err);
9669 }
9670 
9671 static int
9672 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9673     boolean_t need_up)
9674 {
9675 	in6_addr_t v6addr;
9676 	ill_t	*ill = ipif->ipif_ill;
9677 	int	err = 0;
9678 	boolean_t need_dl_down;
9679 	boolean_t need_arp_down;
9680 
9681 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9682 	    ipif->ipif_id, (void *)ipif));
9683 
9684 	/* Must cancel any pending timer before taking the ill_lock */
9685 	if (ipif->ipif_recovery_id != 0)
9686 		(void) untimeout(ipif->ipif_recovery_id);
9687 	ipif->ipif_recovery_id = 0;
9688 
9689 	if (ipif->ipif_isv6) {
9690 		sin6_t *sin6;
9691 
9692 		sin6 = (sin6_t *)sin;
9693 		v6addr = sin6->sin6_addr;
9694 	} else {
9695 		ipaddr_t addr;
9696 
9697 		addr = sin->sin_addr.s_addr;
9698 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9699 	}
9700 	mutex_enter(&ill->ill_lock);
9701 	/* Set point to point destination address. */
9702 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9703 		/*
9704 		 * Allow this as a means of creating logical
9705 		 * pt-pt interfaces on top of e.g. an Ethernet.
9706 		 * XXX Undocumented HACK for testing.
9707 		 * pt-pt interfaces are created with NUD disabled.
9708 		 */
9709 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9710 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9711 		if (ipif->ipif_isv6)
9712 			ill->ill_flags |= ILLF_NONUD;
9713 	}
9714 
9715 	/*
9716 	 * If the interface was previously marked as a duplicate, then since
9717 	 * we've now got a "new" address, it should no longer be considered a
9718 	 * duplicate -- even if the "new" address is the same as the old one.
9719 	 * Note that if all ipifs are down, we may have a pending ARP down
9720 	 * event to handle.
9721 	 */
9722 	need_dl_down = need_arp_down = B_FALSE;
9723 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9724 		need_arp_down = !need_up;
9725 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9726 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9727 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9728 			need_dl_down = B_TRUE;
9729 		}
9730 	}
9731 
9732 	/* Set the new address. */
9733 	ipif->ipif_v6pp_dst_addr = v6addr;
9734 	/* Make sure subnet tracks pp_dst */
9735 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9736 	mutex_exit(&ill->ill_lock);
9737 
9738 	if (need_up) {
9739 		/*
9740 		 * Now bring the interface back up.  If this
9741 		 * is the only IPIF for the ILL, ipif_up
9742 		 * will have to re-bind to the device, so
9743 		 * we may get back EINPROGRESS, in which
9744 		 * case, this IOCTL will get completed in
9745 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9746 		 */
9747 		err = ipif_up(ipif, q, mp);
9748 	}
9749 
9750 	if (need_dl_down)
9751 		ill_dl_down(ill);
9752 	if (need_arp_down && !ipif->ipif_isv6)
9753 		(void) ipif_arp_down(ipif);
9754 
9755 	return (err);
9756 }
9757 
9758 /*
9759  * Restart entry point to restart the dstaddress set operation after the
9760  * refcounts have dropped to zero.
9761  */
9762 /* ARGSUSED */
9763 int
9764 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9765     ip_ioctl_cmd_t *ipip, void *ifreq)
9766 {
9767 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9768 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9769 	(void) ipif_down_tail(ipif);
9770 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9771 }
9772 
9773 /* ARGSUSED */
9774 int
9775 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9776     ip_ioctl_cmd_t *ipip, void *if_req)
9777 {
9778 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9779 
9780 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9782 	/*
9783 	 * Get point to point destination address. The addresses can't
9784 	 * change since we hold a reference to the ipif.
9785 	 */
9786 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9787 		return (EADDRNOTAVAIL);
9788 
9789 	if (ipif->ipif_isv6) {
9790 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9791 		*sin6 = sin6_null;
9792 		sin6->sin6_family = AF_INET6;
9793 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9794 	} else {
9795 		*sin = sin_null;
9796 		sin->sin_family = AF_INET;
9797 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9798 	}
9799 	return (0);
9800 }
9801 
9802 /*
9803  * Set interface flags.  Many flags require special handling (e.g.,
9804  * bringing the interface down); see below for details.
9805  *
9806  * NOTE : We really don't enforce that ipif_id zero should be used
9807  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9808  *	  is because applications generally does SICGLIFFLAGS and
9809  *	  ORs in the new flags (that affects the logical) and does a
9810  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9811  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9812  *	  flags that will be turned on is correct with respect to
9813  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9814  */
9815 /* ARGSUSED */
9816 int
9817 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9818     ip_ioctl_cmd_t *ipip, void *if_req)
9819 {
9820 	uint64_t turn_on;
9821 	uint64_t turn_off;
9822 	int	err = 0;
9823 	phyint_t *phyi;
9824 	ill_t *ill;
9825 	uint64_t intf_flags, cantchange_flags;
9826 	boolean_t phyint_flags_modified = B_FALSE;
9827 	uint64_t flags;
9828 	struct ifreq *ifr;
9829 	struct lifreq *lifr;
9830 	boolean_t set_linklocal = B_FALSE;
9831 
9832 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9834 
9835 	ASSERT(IAM_WRITER_IPIF(ipif));
9836 
9837 	ill = ipif->ipif_ill;
9838 	phyi = ill->ill_phyint;
9839 
9840 	if (ipip->ipi_cmd_type == IF_CMD) {
9841 		ifr = (struct ifreq *)if_req;
9842 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9843 	} else {
9844 		lifr = (struct lifreq *)if_req;
9845 		flags = lifr->lifr_flags;
9846 	}
9847 
9848 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9849 
9850 	/*
9851 	 * Have the flags been set correctly until now?
9852 	 */
9853 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9854 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9855 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9856 	/*
9857 	 * Compare the new flags to the old, and partition
9858 	 * into those coming on and those going off.
9859 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9860 	 */
9861 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9862 		flags |= intf_flags & ~0xFFFF;
9863 
9864 	/*
9865 	 * Explicitly fail attempts to change flags that are always invalid on
9866 	 * an IPMP meta-interface.
9867 	 */
9868 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9869 		return (EINVAL);
9870 
9871 	/*
9872 	 * Check which flags will change; silently ignore flags which userland
9873 	 * is not allowed to control.  (Because these flags may change between
9874 	 * SIOCGLIFFLAGS and SIOCSLIFFLAGS, and that's outside of userland's
9875 	 * control, we need to silently ignore them rather than fail.)
9876 	 */
9877 	cantchange_flags = IFF_CANTCHANGE;
9878 	if (IS_IPMP(ill))
9879 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9880 
9881 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9882 	if (turn_on == 0)
9883 		return (0);	/* No change */
9884 
9885 	turn_off = intf_flags & turn_on;
9886 	turn_on ^= turn_off;
9887 
9888 	/*
9889 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
9890 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
9891 	 * allow it to be turned off.
9892 	 */
9893 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
9894 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
9895 		return (EINVAL);
9896 
9897 	if (turn_on & IFF_NOFAILOVER) {
9898 		turn_on |= IFF_DEPRECATED;
9899 		flags |= IFF_DEPRECATED;
9900 	}
9901 
9902 	/*
9903 	 * On underlying interfaces, only allow applications to manage test
9904 	 * addresses -- otherwise, they may get confused when the address
9905 	 * moves as part of being brought up.  Likewise, prevent an
9906 	 * application-managed test address from being converted to a data
9907 	 * address.  To prevent migration of administratively up addresses in
9908 	 * the kernel, we don't allow them to be converted either.
9909 	 */
9910 	if (IS_UNDER_IPMP(ill)) {
9911 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
9912 
9913 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
9914 			return (EINVAL);
9915 
9916 		if ((turn_off & IFF_NOFAILOVER) &&
9917 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
9918 			return (EINVAL);
9919 	}
9920 
9921 	/*
9922 	 * Only allow IFF_TEMPORARY flag to be set on
9923 	 * IPv6 interfaces.
9924 	 */
9925 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
9926 		return (EINVAL);
9927 
9928 	/*
9929 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
9930 	 */
9931 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
9932 		return (EINVAL);
9933 
9934 	/*
9935 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
9936 	 * interfaces.  It makes no sense in that context.
9937 	 */
9938 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
9939 		return (EINVAL);
9940 
9941 	/*
9942 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
9943 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
9944 	 * If the link local address isn't set, and can be set, it will get
9945 	 * set later on in this function.
9946 	 */
9947 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
9948 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
9949 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
9950 		if (ipif_cant_setlinklocal(ipif))
9951 			return (EINVAL);
9952 		set_linklocal = B_TRUE;
9953 	}
9954 
9955 	/*
9956 	 * If we modify physical interface flags, we'll potentially need to
9957 	 * send up two routing socket messages for the changes (one for the
9958 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
9959 	 */
9960 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
9961 		phyint_flags_modified = B_TRUE;
9962 
9963 	/*
9964 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
9965 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
9966 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
9967 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
9968 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
9969 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
9970 	 * will not be honored.
9971 	 */
9972 	if (turn_on & PHYI_STANDBY) {
9973 		/*
9974 		 * No need to grab ill_g_usesrc_lock here; see the
9975 		 * synchronization notes in ip.c.
9976 		 */
9977 		if (ill->ill_usesrc_grp_next != NULL ||
9978 		    intf_flags & PHYI_INACTIVE)
9979 			return (EINVAL);
9980 		if (!(flags & PHYI_FAILED)) {
9981 			flags |= PHYI_INACTIVE;
9982 			turn_on |= PHYI_INACTIVE;
9983 		}
9984 	}
9985 
9986 	if (turn_off & PHYI_STANDBY) {
9987 		flags &= ~PHYI_INACTIVE;
9988 		turn_off |= PHYI_INACTIVE;
9989 	}
9990 
9991 	/*
9992 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
9993 	 * would end up on.
9994 	 */
9995 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
9996 	    (PHYI_FAILED | PHYI_INACTIVE))
9997 		return (EINVAL);
9998 
9999 	/*
10000 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10001 	 * status of the interface.
10002 	 */
10003 	if ((turn_on | turn_off) & ILLF_ROUTER)
10004 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10005 
10006 	/*
10007 	 * If the interface is not UP and we are not going to
10008 	 * bring it UP, record the flags and return. When the
10009 	 * interface comes UP later, the right actions will be
10010 	 * taken.
10011 	 */
10012 	if (!(ipif->ipif_flags & IPIF_UP) &&
10013 	    !(turn_on & IPIF_UP)) {
10014 		/* Record new flags in their respective places. */
10015 		mutex_enter(&ill->ill_lock);
10016 		mutex_enter(&ill->ill_phyint->phyint_lock);
10017 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10018 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10019 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10020 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10021 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10022 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10023 		mutex_exit(&ill->ill_lock);
10024 		mutex_exit(&ill->ill_phyint->phyint_lock);
10025 
10026 		/*
10027 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10028 		 * same to the kernel: if any of them has been set by
10029 		 * userland, the interface cannot be used for data traffic.
10030 		 */
10031 		if ((turn_on|turn_off) &
10032 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10033 			ASSERT(!IS_IPMP(ill));
10034 			/*
10035 			 * It's possible the ill is part of an "anonymous"
10036 			 * IPMP group rather than a real group.  In that case,
10037 			 * there are no other interfaces in the group and thus
10038 			 * no need to call ipmp_phyint_refresh_active().
10039 			 */
10040 			if (IS_UNDER_IPMP(ill))
10041 				ipmp_phyint_refresh_active(phyi);
10042 		}
10043 
10044 		if (phyint_flags_modified) {
10045 			if (phyi->phyint_illv4 != NULL) {
10046 				ip_rts_ifmsg(phyi->phyint_illv4->
10047 				    ill_ipif, RTSQ_DEFAULT);
10048 			}
10049 			if (phyi->phyint_illv6 != NULL) {
10050 				ip_rts_ifmsg(phyi->phyint_illv6->
10051 				    ill_ipif, RTSQ_DEFAULT);
10052 			}
10053 		}
10054 		/* The default multicast interface might have changed */
10055 		ire_increment_multicast_generation(ill->ill_ipst,
10056 		    ill->ill_isv6);
10057 
10058 		return (0);
10059 	} else if (set_linklocal) {
10060 		mutex_enter(&ill->ill_lock);
10061 		if (set_linklocal)
10062 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10063 		mutex_exit(&ill->ill_lock);
10064 	}
10065 
10066 	/*
10067 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10068 	 * or point-to-point interfaces with an unspecified destination. We do
10069 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10070 	 * have a subnet assigned, which is how in.ndpd currently manages its
10071 	 * onlink prefix list when no addresses are configured with those
10072 	 * prefixes.
10073 	 */
10074 	if (ipif->ipif_isv6 &&
10075 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10076 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10077 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10078 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10079 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10080 		return (EINVAL);
10081 	}
10082 
10083 	/*
10084 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10085 	 * from being brought up.
10086 	 */
10087 	if (!ipif->ipif_isv6 &&
10088 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10089 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10090 		return (EINVAL);
10091 	}
10092 
10093 	/*
10094 	 * The only flag changes that we currently take specific action on are
10095 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10096 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10097 	 * IPIF_NOFAILOVER.  This is done by bring the ipif down, changing the
10098 	 * flags and bringing it back up again.  For IPIF_NOFAILOVER, the act
10099 	 * of bringing it back up will trigger the address to be moved.
10100 	 */
10101 	if ((turn_on|turn_off) &
10102 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10103 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10104 	    IPIF_NOFAILOVER)) {
10105 		/*
10106 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10107 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10108 		 * entries shared between multiple ipifs on the same subnet.
10109 		 */
10110 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10111 		    !(turn_off & IPIF_UP)) {
10112 			if (ipif->ipif_flags & IPIF_UP)
10113 				ill->ill_logical_down = 1;
10114 			turn_on &= ~IPIF_UP;
10115 		}
10116 		err = ipif_down(ipif, q, mp);
10117 		ip1dbg(("ipif_down returns %d err ", err));
10118 		if (err == EINPROGRESS)
10119 			return (err);
10120 		(void) ipif_down_tail(ipif);
10121 	}
10122 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10123 }
10124 
10125 static int
10126 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10127 {
10128 	ill_t	*ill;
10129 	phyint_t *phyi;
10130 	uint64_t turn_on, turn_off;
10131 	uint64_t intf_flags, cantchange_flags;
10132 	boolean_t phyint_flags_modified = B_FALSE;
10133 	int	err = 0;
10134 	boolean_t set_linklocal = B_FALSE;
10135 
10136 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10137 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10138 
10139 	ASSERT(IAM_WRITER_IPIF(ipif));
10140 
10141 	ill = ipif->ipif_ill;
10142 	phyi = ill->ill_phyint;
10143 
10144 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10145 	cantchange_flags = IFF_CANTCHANGE | IFF_UP;
10146 	if (IS_IPMP(ill))
10147 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10148 
10149 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10150 	turn_off = intf_flags & turn_on;
10151 	turn_on ^= turn_off;
10152 
10153 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10154 		phyint_flags_modified = B_TRUE;
10155 
10156 	/*
10157 	 * Now we change the flags. Track current value of
10158 	 * other flags in their respective places.
10159 	 */
10160 	mutex_enter(&ill->ill_lock);
10161 	mutex_enter(&phyi->phyint_lock);
10162 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10163 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10164 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10165 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10166 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10167 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10168 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10169 		set_linklocal = B_TRUE;
10170 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10171 	}
10172 
10173 	mutex_exit(&ill->ill_lock);
10174 	mutex_exit(&phyi->phyint_lock);
10175 
10176 	if (set_linklocal)
10177 		(void) ipif_setlinklocal(ipif);
10178 
10179 	/*
10180 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10181 	 * the kernel: if any of them has been set by userland, the interface
10182 	 * cannot be used for data traffic.
10183 	 */
10184 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10185 		ASSERT(!IS_IPMP(ill));
10186 		/*
10187 		 * It's possible the ill is part of an "anonymous" IPMP group
10188 		 * rather than a real group.  In that case, there are no other
10189 		 * interfaces in the group and thus no need for us to call
10190 		 * ipmp_phyint_refresh_active().
10191 		 */
10192 		if (IS_UNDER_IPMP(ill))
10193 			ipmp_phyint_refresh_active(phyi);
10194 	}
10195 
10196 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10197 		/*
10198 		 * XXX ipif_up really does not know whether a phyint flags
10199 		 * was modified or not. So, it sends up information on
10200 		 * only one routing sockets message. As we don't bring up
10201 		 * the interface and also set PHYI_ flags simultaneously
10202 		 * it should be okay.
10203 		 */
10204 		err = ipif_up(ipif, q, mp);
10205 	} else {
10206 		/*
10207 		 * Make sure routing socket sees all changes to the flags.
10208 		 * ipif_up_done* handles this when we use ipif_up.
10209 		 */
10210 		if (phyint_flags_modified) {
10211 			if (phyi->phyint_illv4 != NULL) {
10212 				ip_rts_ifmsg(phyi->phyint_illv4->
10213 				    ill_ipif, RTSQ_DEFAULT);
10214 			}
10215 			if (phyi->phyint_illv6 != NULL) {
10216 				ip_rts_ifmsg(phyi->phyint_illv6->
10217 				    ill_ipif, RTSQ_DEFAULT);
10218 			}
10219 		} else {
10220 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10221 		}
10222 		/*
10223 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10224 		 * this in need_up case.
10225 		 */
10226 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10227 	}
10228 
10229 	/* The default multicast interface might have changed */
10230 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10231 	return (err);
10232 }
10233 
10234 /*
10235  * Restart the flags operation now that the refcounts have dropped to zero.
10236  */
10237 /* ARGSUSED */
10238 int
10239 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10240     ip_ioctl_cmd_t *ipip, void *if_req)
10241 {
10242 	uint64_t flags;
10243 	struct ifreq *ifr = if_req;
10244 	struct lifreq *lifr = if_req;
10245 
10246 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10247 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10248 
10249 	(void) ipif_down_tail(ipif);
10250 	if (ipip->ipi_cmd_type == IF_CMD) {
10251 		/* cast to uint16_t prevents unwanted sign extension */
10252 		flags = (uint16_t)ifr->ifr_flags;
10253 	} else {
10254 		flags = lifr->lifr_flags;
10255 	}
10256 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10257 }
10258 
10259 /*
10260  * Can operate on either a module or a driver queue.
10261  */
10262 /* ARGSUSED */
10263 int
10264 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10265     ip_ioctl_cmd_t *ipip, void *if_req)
10266 {
10267 	/*
10268 	 * Has the flags been set correctly till now ?
10269 	 */
10270 	ill_t *ill = ipif->ipif_ill;
10271 	phyint_t *phyi = ill->ill_phyint;
10272 
10273 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10274 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10275 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10276 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10277 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10278 
10279 	/*
10280 	 * Need a lock since some flags can be set even when there are
10281 	 * references to the ipif.
10282 	 */
10283 	mutex_enter(&ill->ill_lock);
10284 	if (ipip->ipi_cmd_type == IF_CMD) {
10285 		struct ifreq *ifr = (struct ifreq *)if_req;
10286 
10287 		/* Get interface flags (low 16 only). */
10288 		ifr->ifr_flags = ((ipif->ipif_flags |
10289 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10290 	} else {
10291 		struct lifreq *lifr = (struct lifreq *)if_req;
10292 
10293 		/* Get interface flags. */
10294 		lifr->lifr_flags = ipif->ipif_flags |
10295 		    ill->ill_flags | phyi->phyint_flags;
10296 	}
10297 	mutex_exit(&ill->ill_lock);
10298 	return (0);
10299 }
10300 
10301 /*
10302  * We allow the MTU to be set on an ILL, but not have it be different
10303  * for different IPIFs since we don't actually send packets on IPIFs.
10304  */
10305 /* ARGSUSED */
10306 int
10307 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10308     ip_ioctl_cmd_t *ipip, void *if_req)
10309 {
10310 	int mtu;
10311 	int ip_min_mtu;
10312 	struct ifreq	*ifr;
10313 	struct lifreq *lifr;
10314 	ill_t	*ill;
10315 
10316 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10317 	    ipif->ipif_id, (void *)ipif));
10318 	if (ipip->ipi_cmd_type == IF_CMD) {
10319 		ifr = (struct ifreq *)if_req;
10320 		mtu = ifr->ifr_metric;
10321 	} else {
10322 		lifr = (struct lifreq *)if_req;
10323 		mtu = lifr->lifr_mtu;
10324 	}
10325 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10326 	if (ipif->ipif_id != 0)
10327 		return (EINVAL);
10328 
10329 	ill = ipif->ipif_ill;
10330 	if (ipif->ipif_isv6)
10331 		ip_min_mtu = IPV6_MIN_MTU;
10332 	else
10333 		ip_min_mtu = IP_MIN_MTU;
10334 
10335 	mutex_enter(&ill->ill_lock);
10336 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10337 		mutex_exit(&ill->ill_lock);
10338 		return (EINVAL);
10339 	}
10340 	/*
10341 	 * The dce and fragmentation code can handle changes to ill_mtu
10342 	 * concurrent with sending/fragmenting packets.
10343 	 */
10344 	ill->ill_mtu = mtu;
10345 	ill->ill_flags |= ILLF_FIXEDMTU;
10346 	mutex_exit(&ill->ill_lock);
10347 
10348 	/*
10349 	 * Make sure all dce_generation checks find out
10350 	 * that ill_mtu has changed.
10351 	 */
10352 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10353 
10354 	/* Update the MTU in SCTP's list */
10355 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10356 	return (0);
10357 }
10358 
10359 /* Get interface MTU. */
10360 /* ARGSUSED */
10361 int
10362 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10363 	ip_ioctl_cmd_t *ipip, void *if_req)
10364 {
10365 	struct ifreq	*ifr;
10366 	struct lifreq	*lifr;
10367 
10368 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10369 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10370 
10371 	/*
10372 	 * We allow a get on any logical interface even though the set
10373 	 * can only be done on logical unit 0.
10374 	 */
10375 	if (ipip->ipi_cmd_type == IF_CMD) {
10376 		ifr = (struct ifreq *)if_req;
10377 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10378 	} else {
10379 		lifr = (struct lifreq *)if_req;
10380 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10381 	}
10382 	return (0);
10383 }
10384 
10385 /* Set interface broadcast address. */
10386 /* ARGSUSED2 */
10387 int
10388 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10389 	ip_ioctl_cmd_t *ipip, void *if_req)
10390 {
10391 	ipaddr_t addr;
10392 	ire_t	*ire;
10393 	ill_t		*ill = ipif->ipif_ill;
10394 	ip_stack_t	*ipst = ill->ill_ipst;
10395 
10396 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10397 	    ipif->ipif_id));
10398 
10399 	ASSERT(IAM_WRITER_IPIF(ipif));
10400 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10401 		return (EADDRNOTAVAIL);
10402 
10403 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10404 
10405 	if (sin->sin_family != AF_INET)
10406 		return (EAFNOSUPPORT);
10407 
10408 	addr = sin->sin_addr.s_addr;
10409 	if (ipif->ipif_flags & IPIF_UP) {
10410 		/*
10411 		 * If we are already up, make sure the new
10412 		 * broadcast address makes sense.  If it does,
10413 		 * there should be an IRE for it already.
10414 		 */
10415 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10416 		    ill, ipif->ipif_zoneid, NULL,
10417 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10418 		if (ire == NULL) {
10419 			return (EINVAL);
10420 		} else {
10421 			ire_refrele(ire);
10422 		}
10423 	}
10424 	/*
10425 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10426 	 * needs to already exist we never need to change the set of
10427 	 * IRE_BROADCASTs when we are UP.
10428 	 */
10429 	if (addr != ipif->ipif_brd_addr)
10430 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10431 
10432 	return (0);
10433 }
10434 
10435 /* Get interface broadcast address. */
10436 /* ARGSUSED */
10437 int
10438 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10439     ip_ioctl_cmd_t *ipip, void *if_req)
10440 {
10441 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10442 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10443 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10444 		return (EADDRNOTAVAIL);
10445 
10446 	/* IPIF_BROADCAST not possible with IPv6 */
10447 	ASSERT(!ipif->ipif_isv6);
10448 	*sin = sin_null;
10449 	sin->sin_family = AF_INET;
10450 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10451 	return (0);
10452 }
10453 
10454 /*
10455  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10456  */
10457 /* ARGSUSED */
10458 int
10459 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10460     ip_ioctl_cmd_t *ipip, void *if_req)
10461 {
10462 	int err = 0;
10463 	in6_addr_t v6mask;
10464 
10465 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10466 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10467 
10468 	ASSERT(IAM_WRITER_IPIF(ipif));
10469 
10470 	if (ipif->ipif_isv6) {
10471 		sin6_t *sin6;
10472 
10473 		if (sin->sin_family != AF_INET6)
10474 			return (EAFNOSUPPORT);
10475 
10476 		sin6 = (sin6_t *)sin;
10477 		v6mask = sin6->sin6_addr;
10478 	} else {
10479 		ipaddr_t mask;
10480 
10481 		if (sin->sin_family != AF_INET)
10482 			return (EAFNOSUPPORT);
10483 
10484 		mask = sin->sin_addr.s_addr;
10485 		V4MASK_TO_V6(mask, v6mask);
10486 	}
10487 
10488 	/*
10489 	 * No big deal if the interface isn't already up, or the mask
10490 	 * isn't really changing, or this is pt-pt.
10491 	 */
10492 	if (!(ipif->ipif_flags & IPIF_UP) ||
10493 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10494 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10495 		ipif->ipif_v6net_mask = v6mask;
10496 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10497 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10498 			    ipif->ipif_v6net_mask,
10499 			    ipif->ipif_v6subnet);
10500 		}
10501 		return (0);
10502 	}
10503 	/*
10504 	 * Make sure we have valid net and subnet broadcast ire's
10505 	 * for the old netmask, if needed by other logical interfaces.
10506 	 */
10507 	err = ipif_logical_down(ipif, q, mp);
10508 	if (err == EINPROGRESS)
10509 		return (err);
10510 	(void) ipif_down_tail(ipif);
10511 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10512 	return (err);
10513 }
10514 
10515 static int
10516 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10517 {
10518 	in6_addr_t v6mask;
10519 	int err = 0;
10520 
10521 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10522 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10523 
10524 	if (ipif->ipif_isv6) {
10525 		sin6_t *sin6;
10526 
10527 		sin6 = (sin6_t *)sin;
10528 		v6mask = sin6->sin6_addr;
10529 	} else {
10530 		ipaddr_t mask;
10531 
10532 		mask = sin->sin_addr.s_addr;
10533 		V4MASK_TO_V6(mask, v6mask);
10534 	}
10535 
10536 	ipif->ipif_v6net_mask = v6mask;
10537 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10538 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10539 		    ipif->ipif_v6subnet);
10540 	}
10541 	err = ipif_up(ipif, q, mp);
10542 
10543 	if (err == 0 || err == EINPROGRESS) {
10544 		/*
10545 		 * The interface must be DL_BOUND if this packet has to
10546 		 * go out on the wire. Since we only go through a logical
10547 		 * down and are bound with the driver during an internal
10548 		 * down/up that is satisfied.
10549 		 */
10550 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10551 			/* Potentially broadcast an address mask reply. */
10552 			ipif_mask_reply(ipif);
10553 		}
10554 	}
10555 	return (err);
10556 }
10557 
10558 /* ARGSUSED */
10559 int
10560 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10561     ip_ioctl_cmd_t *ipip, void *if_req)
10562 {
10563 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10564 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10565 	(void) ipif_down_tail(ipif);
10566 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10567 }
10568 
10569 /* Get interface net mask. */
10570 /* ARGSUSED */
10571 int
10572 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10573     ip_ioctl_cmd_t *ipip, void *if_req)
10574 {
10575 	struct lifreq *lifr = (struct lifreq *)if_req;
10576 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10577 
10578 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10579 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10580 
10581 	/*
10582 	 * net mask can't change since we have a reference to the ipif.
10583 	 */
10584 	if (ipif->ipif_isv6) {
10585 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10586 		*sin6 = sin6_null;
10587 		sin6->sin6_family = AF_INET6;
10588 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10589 		lifr->lifr_addrlen =
10590 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10591 	} else {
10592 		*sin = sin_null;
10593 		sin->sin_family = AF_INET;
10594 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10595 		if (ipip->ipi_cmd_type == LIF_CMD) {
10596 			lifr->lifr_addrlen =
10597 			    ip_mask_to_plen(ipif->ipif_net_mask);
10598 		}
10599 	}
10600 	return (0);
10601 }
10602 
10603 /* ARGSUSED */
10604 int
10605 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10606     ip_ioctl_cmd_t *ipip, void *if_req)
10607 {
10608 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10609 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10610 
10611 	/*
10612 	 * Since no applications should ever be setting metrics on underlying
10613 	 * interfaces, we explicitly fail to smoke 'em out.
10614 	 */
10615 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10616 		return (EINVAL);
10617 
10618 	/*
10619 	 * Set interface metric.  We don't use this for
10620 	 * anything but we keep track of it in case it is
10621 	 * important to routing applications or such.
10622 	 */
10623 	if (ipip->ipi_cmd_type == IF_CMD) {
10624 		struct ifreq    *ifr;
10625 
10626 		ifr = (struct ifreq *)if_req;
10627 		ipif->ipif_metric = ifr->ifr_metric;
10628 	} else {
10629 		struct lifreq   *lifr;
10630 
10631 		lifr = (struct lifreq *)if_req;
10632 		ipif->ipif_metric = lifr->lifr_metric;
10633 	}
10634 	return (0);
10635 }
10636 
10637 /* ARGSUSED */
10638 int
10639 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10640     ip_ioctl_cmd_t *ipip, void *if_req)
10641 {
10642 	/* Get interface metric. */
10643 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10644 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10645 
10646 	if (ipip->ipi_cmd_type == IF_CMD) {
10647 		struct ifreq    *ifr;
10648 
10649 		ifr = (struct ifreq *)if_req;
10650 		ifr->ifr_metric = ipif->ipif_metric;
10651 	} else {
10652 		struct lifreq   *lifr;
10653 
10654 		lifr = (struct lifreq *)if_req;
10655 		lifr->lifr_metric = ipif->ipif_metric;
10656 	}
10657 
10658 	return (0);
10659 }
10660 
10661 /* ARGSUSED */
10662 int
10663 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10664     ip_ioctl_cmd_t *ipip, void *if_req)
10665 {
10666 	int	arp_muxid;
10667 
10668 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10669 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10670 	/*
10671 	 * Set the muxid returned from I_PLINK.
10672 	 */
10673 	if (ipip->ipi_cmd_type == IF_CMD) {
10674 		struct ifreq *ifr = (struct ifreq *)if_req;
10675 
10676 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10677 		arp_muxid = ifr->ifr_arp_muxid;
10678 	} else {
10679 		struct lifreq *lifr = (struct lifreq *)if_req;
10680 
10681 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10682 		arp_muxid = lifr->lifr_arp_muxid;
10683 	}
10684 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10685 	return (0);
10686 }
10687 
10688 /* ARGSUSED */
10689 int
10690 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10691     ip_ioctl_cmd_t *ipip, void *if_req)
10692 {
10693 	int	arp_muxid = 0;
10694 
10695 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10696 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10697 	/*
10698 	 * Get the muxid saved in ill for I_PUNLINK.
10699 	 */
10700 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10701 	if (ipip->ipi_cmd_type == IF_CMD) {
10702 		struct ifreq *ifr = (struct ifreq *)if_req;
10703 
10704 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10705 		ifr->ifr_arp_muxid = arp_muxid;
10706 	} else {
10707 		struct lifreq *lifr = (struct lifreq *)if_req;
10708 
10709 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10710 		lifr->lifr_arp_muxid = arp_muxid;
10711 	}
10712 	return (0);
10713 }
10714 
10715 /*
10716  * Set the subnet prefix. Does not modify the broadcast address.
10717  */
10718 /* ARGSUSED */
10719 int
10720 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10721     ip_ioctl_cmd_t *ipip, void *if_req)
10722 {
10723 	int err = 0;
10724 	in6_addr_t v6addr;
10725 	in6_addr_t v6mask;
10726 	boolean_t need_up = B_FALSE;
10727 	int addrlen;
10728 
10729 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10730 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10731 
10732 	ASSERT(IAM_WRITER_IPIF(ipif));
10733 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10734 
10735 	if (ipif->ipif_isv6) {
10736 		sin6_t *sin6;
10737 
10738 		if (sin->sin_family != AF_INET6)
10739 			return (EAFNOSUPPORT);
10740 
10741 		sin6 = (sin6_t *)sin;
10742 		v6addr = sin6->sin6_addr;
10743 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10744 			return (EADDRNOTAVAIL);
10745 	} else {
10746 		ipaddr_t addr;
10747 
10748 		if (sin->sin_family != AF_INET)
10749 			return (EAFNOSUPPORT);
10750 
10751 		addr = sin->sin_addr.s_addr;
10752 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10753 			return (EADDRNOTAVAIL);
10754 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10755 		/* Add 96 bits */
10756 		addrlen += IPV6_ABITS - IP_ABITS;
10757 	}
10758 
10759 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10760 		return (EINVAL);
10761 
10762 	/* Check if bits in the address is set past the mask */
10763 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10764 		return (EINVAL);
10765 
10766 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10767 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10768 		return (0);	/* No change */
10769 
10770 	if (ipif->ipif_flags & IPIF_UP) {
10771 		/*
10772 		 * If the interface is already marked up,
10773 		 * we call ipif_down which will take care
10774 		 * of ditching any IREs that have been set
10775 		 * up based on the old interface address.
10776 		 */
10777 		err = ipif_logical_down(ipif, q, mp);
10778 		if (err == EINPROGRESS)
10779 			return (err);
10780 		(void) ipif_down_tail(ipif);
10781 		need_up = B_TRUE;
10782 	}
10783 
10784 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10785 	return (err);
10786 }
10787 
10788 static int
10789 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10790     queue_t *q, mblk_t *mp, boolean_t need_up)
10791 {
10792 	ill_t	*ill = ipif->ipif_ill;
10793 	int	err = 0;
10794 
10795 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10796 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10797 
10798 	/* Set the new address. */
10799 	mutex_enter(&ill->ill_lock);
10800 	ipif->ipif_v6net_mask = v6mask;
10801 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10802 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10803 		    ipif->ipif_v6subnet);
10804 	}
10805 	mutex_exit(&ill->ill_lock);
10806 
10807 	if (need_up) {
10808 		/*
10809 		 * Now bring the interface back up.  If this
10810 		 * is the only IPIF for the ILL, ipif_up
10811 		 * will have to re-bind to the device, so
10812 		 * we may get back EINPROGRESS, in which
10813 		 * case, this IOCTL will get completed in
10814 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10815 		 */
10816 		err = ipif_up(ipif, q, mp);
10817 		if (err == EINPROGRESS)
10818 			return (err);
10819 	}
10820 	return (err);
10821 }
10822 
10823 /* ARGSUSED */
10824 int
10825 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10826     ip_ioctl_cmd_t *ipip, void *if_req)
10827 {
10828 	int	addrlen;
10829 	in6_addr_t v6addr;
10830 	in6_addr_t v6mask;
10831 	struct lifreq *lifr = (struct lifreq *)if_req;
10832 
10833 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
10834 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10835 	(void) ipif_down_tail(ipif);
10836 
10837 	addrlen = lifr->lifr_addrlen;
10838 	if (ipif->ipif_isv6) {
10839 		sin6_t *sin6;
10840 
10841 		sin6 = (sin6_t *)sin;
10842 		v6addr = sin6->sin6_addr;
10843 	} else {
10844 		ipaddr_t addr;
10845 
10846 		addr = sin->sin_addr.s_addr;
10847 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10848 		addrlen += IPV6_ABITS - IP_ABITS;
10849 	}
10850 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
10851 
10852 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
10853 }
10854 
10855 /* ARGSUSED */
10856 int
10857 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10858     ip_ioctl_cmd_t *ipip, void *if_req)
10859 {
10860 	struct lifreq *lifr = (struct lifreq *)if_req;
10861 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
10862 
10863 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
10864 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10865 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10866 
10867 	if (ipif->ipif_isv6) {
10868 		*sin6 = sin6_null;
10869 		sin6->sin6_family = AF_INET6;
10870 		sin6->sin6_addr = ipif->ipif_v6subnet;
10871 		lifr->lifr_addrlen =
10872 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10873 	} else {
10874 		*sin = sin_null;
10875 		sin->sin_family = AF_INET;
10876 		sin->sin_addr.s_addr = ipif->ipif_subnet;
10877 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
10878 	}
10879 	return (0);
10880 }
10881 
10882 /*
10883  * Set the IPv6 address token.
10884  */
10885 /* ARGSUSED */
10886 int
10887 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10888     ip_ioctl_cmd_t *ipi, void *if_req)
10889 {
10890 	ill_t *ill = ipif->ipif_ill;
10891 	int err;
10892 	in6_addr_t v6addr;
10893 	in6_addr_t v6mask;
10894 	boolean_t need_up = B_FALSE;
10895 	int i;
10896 	sin6_t *sin6 = (sin6_t *)sin;
10897 	struct lifreq *lifr = (struct lifreq *)if_req;
10898 	int addrlen;
10899 
10900 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
10901 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10902 	ASSERT(IAM_WRITER_IPIF(ipif));
10903 
10904 	addrlen = lifr->lifr_addrlen;
10905 	/* Only allow for logical unit zero i.e. not on "le0:17" */
10906 	if (ipif->ipif_id != 0)
10907 		return (EINVAL);
10908 
10909 	if (!ipif->ipif_isv6)
10910 		return (EINVAL);
10911 
10912 	if (addrlen > IPV6_ABITS)
10913 		return (EINVAL);
10914 
10915 	v6addr = sin6->sin6_addr;
10916 
10917 	/*
10918 	 * The length of the token is the length from the end.  To get
10919 	 * the proper mask for this, compute the mask of the bits not
10920 	 * in the token; ie. the prefix, and then xor to get the mask.
10921 	 */
10922 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
10923 		return (EINVAL);
10924 	for (i = 0; i < 4; i++) {
10925 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
10926 	}
10927 
10928 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
10929 	    ill->ill_token_length == addrlen)
10930 		return (0);	/* No change */
10931 
10932 	if (ipif->ipif_flags & IPIF_UP) {
10933 		err = ipif_logical_down(ipif, q, mp);
10934 		if (err == EINPROGRESS)
10935 			return (err);
10936 		(void) ipif_down_tail(ipif);
10937 		need_up = B_TRUE;
10938 	}
10939 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
10940 	return (err);
10941 }
10942 
10943 static int
10944 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
10945     mblk_t *mp, boolean_t need_up)
10946 {
10947 	in6_addr_t v6addr;
10948 	in6_addr_t v6mask;
10949 	ill_t	*ill = ipif->ipif_ill;
10950 	int	i;
10951 	int	err = 0;
10952 
10953 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
10954 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10955 	v6addr = sin6->sin6_addr;
10956 	/*
10957 	 * The length of the token is the length from the end.  To get
10958 	 * the proper mask for this, compute the mask of the bits not
10959 	 * in the token; ie. the prefix, and then xor to get the mask.
10960 	 */
10961 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
10962 	for (i = 0; i < 4; i++)
10963 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
10964 
10965 	mutex_enter(&ill->ill_lock);
10966 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
10967 	ill->ill_token_length = addrlen;
10968 	ill->ill_manual_token = 1;
10969 
10970 	/* Reconfigure the link-local address based on this new token */
10971 	ipif_setlinklocal(ill->ill_ipif);
10972 
10973 	mutex_exit(&ill->ill_lock);
10974 
10975 	if (need_up) {
10976 		/*
10977 		 * Now bring the interface back up.  If this
10978 		 * is the only IPIF for the ILL, ipif_up
10979 		 * will have to re-bind to the device, so
10980 		 * we may get back EINPROGRESS, in which
10981 		 * case, this IOCTL will get completed in
10982 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10983 		 */
10984 		err = ipif_up(ipif, q, mp);
10985 		if (err == EINPROGRESS)
10986 			return (err);
10987 	}
10988 	return (err);
10989 }
10990 
10991 /* ARGSUSED */
10992 int
10993 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10994     ip_ioctl_cmd_t *ipi, void *if_req)
10995 {
10996 	ill_t *ill;
10997 	sin6_t *sin6 = (sin6_t *)sin;
10998 	struct lifreq *lifr = (struct lifreq *)if_req;
10999 
11000 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11001 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11002 	if (ipif->ipif_id != 0)
11003 		return (EINVAL);
11004 
11005 	ill = ipif->ipif_ill;
11006 	if (!ill->ill_isv6)
11007 		return (ENXIO);
11008 
11009 	*sin6 = sin6_null;
11010 	sin6->sin6_family = AF_INET6;
11011 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11012 	sin6->sin6_addr = ill->ill_token;
11013 	lifr->lifr_addrlen = ill->ill_token_length;
11014 	return (0);
11015 }
11016 
11017 /*
11018  * Set (hardware) link specific information that might override
11019  * what was acquired through the DL_INFO_ACK.
11020  */
11021 /* ARGSUSED */
11022 int
11023 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11024     ip_ioctl_cmd_t *ipi, void *if_req)
11025 {
11026 	ill_t		*ill = ipif->ipif_ill;
11027 	int		ip_min_mtu;
11028 	struct lifreq	*lifr = (struct lifreq *)if_req;
11029 	lif_ifinfo_req_t *lir;
11030 
11031 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11032 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11033 	lir = &lifr->lifr_ifinfo;
11034 	ASSERT(IAM_WRITER_IPIF(ipif));
11035 
11036 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11037 	if (ipif->ipif_id != 0)
11038 		return (EINVAL);
11039 
11040 	/* Set interface MTU. */
11041 	if (ipif->ipif_isv6)
11042 		ip_min_mtu = IPV6_MIN_MTU;
11043 	else
11044 		ip_min_mtu = IP_MIN_MTU;
11045 
11046 	/*
11047 	 * Verify values before we set anything. Allow zero to
11048 	 * mean unspecified.
11049 	 *
11050 	 * XXX We should be able to set the user-defined lir_mtu to some value
11051 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11052 	 * ill_max_frag value tells us the max MTU that can be handled by the
11053 	 * datalink, whereas the ill_current_frag is dynamically computed for
11054 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11055 	 * since there is currently no way of distinguishing between
11056 	 * administratively fixed link mtu values (e.g., those set via
11057 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11058 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11059 	 * upper-bound.
11060 	 */
11061 	if (lir->lir_maxmtu != 0 &&
11062 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11063 	    lir->lir_maxmtu < ip_min_mtu))
11064 		return (EINVAL);
11065 	if (lir->lir_reachtime != 0 &&
11066 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11067 		return (EINVAL);
11068 	if (lir->lir_reachretrans != 0 &&
11069 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11070 		return (EINVAL);
11071 
11072 	mutex_enter(&ill->ill_lock);
11073 	/*
11074 	 * The dce and fragmentation code can handle changes to ill_mtu
11075 	 * concurrent with sending/fragmenting packets.
11076 	 */
11077 	if (lir->lir_maxmtu != 0)
11078 		ill->ill_user_mtu = lir->lir_maxmtu;
11079 
11080 	if (lir->lir_reachtime != 0)
11081 		ill->ill_reachable_time = lir->lir_reachtime;
11082 
11083 	if (lir->lir_reachretrans != 0)
11084 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11085 
11086 	ill->ill_max_hops = lir->lir_maxhops;
11087 	ill->ill_max_buf = ND_MAX_Q;
11088 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11089 		/*
11090 		 * ill_mtu is the actual interface MTU, obtained as the min
11091 		 * of user-configured mtu and the value announced by the
11092 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11093 		 * we have already made the choice of requiring
11094 		 * ill_user_mtu < ill_current_frag by the time we get here,
11095 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11096 		 * here.
11097 		 */
11098 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11099 	}
11100 	mutex_exit(&ill->ill_lock);
11101 
11102 	/*
11103 	 * Make sure all dce_generation checks find out
11104 	 * that ill_mtu has changed.
11105 	 */
11106 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11107 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11108 
11109 	/*
11110 	 * Refresh IPMP meta-interface MTU if necessary.
11111 	 */
11112 	if (IS_UNDER_IPMP(ill))
11113 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11114 
11115 	return (0);
11116 }
11117 
11118 /* ARGSUSED */
11119 int
11120 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11121     ip_ioctl_cmd_t *ipi, void *if_req)
11122 {
11123 	struct lif_ifinfo_req *lir;
11124 	ill_t *ill = ipif->ipif_ill;
11125 
11126 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11127 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11128 	if (ipif->ipif_id != 0)
11129 		return (EINVAL);
11130 
11131 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11132 	lir->lir_maxhops = ill->ill_max_hops;
11133 	lir->lir_reachtime = ill->ill_reachable_time;
11134 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11135 	lir->lir_maxmtu = ill->ill_mtu;
11136 
11137 	return (0);
11138 }
11139 
11140 /*
11141  * Return best guess as to the subnet mask for the specified address.
11142  * Based on the subnet masks for all the configured interfaces.
11143  *
11144  * We end up returning a zero mask in the case of default, multicast or
11145  * experimental.
11146  */
11147 static ipaddr_t
11148 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11149 {
11150 	ipaddr_t net_mask;
11151 	ill_t	*ill;
11152 	ipif_t	*ipif;
11153 	ill_walk_context_t ctx;
11154 	ipif_t	*fallback_ipif = NULL;
11155 
11156 	net_mask = ip_net_mask(addr);
11157 	if (net_mask == 0) {
11158 		*ipifp = NULL;
11159 		return (0);
11160 	}
11161 
11162 	/* Let's check to see if this is maybe a local subnet route. */
11163 	/* this function only applies to IPv4 interfaces */
11164 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11165 	ill = ILL_START_WALK_V4(&ctx, ipst);
11166 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11167 		mutex_enter(&ill->ill_lock);
11168 		for (ipif = ill->ill_ipif; ipif != NULL;
11169 		    ipif = ipif->ipif_next) {
11170 			if (IPIF_IS_CONDEMNED(ipif))
11171 				continue;
11172 			if (!(ipif->ipif_flags & IPIF_UP))
11173 				continue;
11174 			if ((ipif->ipif_subnet & net_mask) ==
11175 			    (addr & net_mask)) {
11176 				/*
11177 				 * Don't trust pt-pt interfaces if there are
11178 				 * other interfaces.
11179 				 */
11180 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11181 					if (fallback_ipif == NULL) {
11182 						ipif_refhold_locked(ipif);
11183 						fallback_ipif = ipif;
11184 					}
11185 					continue;
11186 				}
11187 
11188 				/*
11189 				 * Fine. Just assume the same net mask as the
11190 				 * directly attached subnet interface is using.
11191 				 */
11192 				ipif_refhold_locked(ipif);
11193 				mutex_exit(&ill->ill_lock);
11194 				rw_exit(&ipst->ips_ill_g_lock);
11195 				if (fallback_ipif != NULL)
11196 					ipif_refrele(fallback_ipif);
11197 				*ipifp = ipif;
11198 				return (ipif->ipif_net_mask);
11199 			}
11200 		}
11201 		mutex_exit(&ill->ill_lock);
11202 	}
11203 	rw_exit(&ipst->ips_ill_g_lock);
11204 
11205 	*ipifp = fallback_ipif;
11206 	return ((fallback_ipif != NULL) ?
11207 	    fallback_ipif->ipif_net_mask : net_mask);
11208 }
11209 
11210 /*
11211  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11212  */
11213 static void
11214 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11215 {
11216 	IOCP	iocp;
11217 	ipft_t	*ipft;
11218 	ipllc_t	*ipllc;
11219 	mblk_t	*mp1;
11220 	cred_t	*cr;
11221 	int	error = 0;
11222 	conn_t	*connp;
11223 
11224 	ip1dbg(("ip_wput_ioctl"));
11225 	iocp = (IOCP)mp->b_rptr;
11226 	mp1 = mp->b_cont;
11227 	if (mp1 == NULL) {
11228 		iocp->ioc_error = EINVAL;
11229 		mp->b_datap->db_type = M_IOCNAK;
11230 		iocp->ioc_count = 0;
11231 		qreply(q, mp);
11232 		return;
11233 	}
11234 
11235 	/*
11236 	 * These IOCTLs provide various control capabilities to
11237 	 * upstream agents such as ULPs and processes.	There
11238 	 * are currently two such IOCTLs implemented.  They
11239 	 * are used by TCP to provide update information for
11240 	 * existing IREs and to forcibly delete an IRE for a
11241 	 * host that is not responding, thereby forcing an
11242 	 * attempt at a new route.
11243 	 */
11244 	iocp->ioc_error = EINVAL;
11245 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11246 		goto done;
11247 
11248 	ipllc = (ipllc_t *)mp1->b_rptr;
11249 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11250 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11251 			break;
11252 	}
11253 	/*
11254 	 * prefer credential from mblk over ioctl;
11255 	 * see ip_sioctl_copyin_setup
11256 	 */
11257 	cr = msg_getcred(mp, NULL);
11258 	if (cr == NULL)
11259 		cr = iocp->ioc_cr;
11260 
11261 	/*
11262 	 * Refhold the conn in case the request gets queued up in some lookup
11263 	 */
11264 	ASSERT(CONN_Q(q));
11265 	connp = Q_TO_CONN(q);
11266 	CONN_INC_REF(connp);
11267 	if (ipft->ipft_pfi &&
11268 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11269 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11270 		error = (*ipft->ipft_pfi)(q,
11271 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11272 	}
11273 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11274 		/*
11275 		 * CONN_OPER_PENDING_DONE happens in the function called
11276 		 * through ipft_pfi above.
11277 		 */
11278 		return;
11279 	}
11280 
11281 	CONN_OPER_PENDING_DONE(connp);
11282 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11283 		freemsg(mp);
11284 		return;
11285 	}
11286 	iocp->ioc_error = error;
11287 
11288 done:
11289 	mp->b_datap->db_type = M_IOCACK;
11290 	if (iocp->ioc_error)
11291 		iocp->ioc_count = 0;
11292 	qreply(q, mp);
11293 }
11294 
11295 /*
11296  * Assign a unique id for the ipif. This is used by sctp_addr.c
11297  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11298  */
11299 static void
11300 ipif_assign_seqid(ipif_t *ipif)
11301 {
11302 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11303 
11304 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11305 }
11306 
11307 /*
11308  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11309  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11310  * that the clone is complete -- including the seqid -- and the expectation is
11311  * that the caller will either free or overwrite `sipif' before it's unlocked.
11312  */
11313 static void
11314 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11315 {
11316 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11317 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11318 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11319 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11320 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11321 
11322 	dipif->ipif_flags = sipif->ipif_flags;
11323 	dipif->ipif_metric = sipif->ipif_metric;
11324 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11325 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11326 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11327 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11328 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11329 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11330 
11331 	/*
11332 	 * As per the comment atop the function, we assume that these sipif
11333 	 * fields will be changed before sipif is unlocked.
11334 	 */
11335 	dipif->ipif_seqid = sipif->ipif_seqid;
11336 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11337 }
11338 
11339 /*
11340  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11341  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11342  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11343  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11344  * down (i.e., no DAD), of the same type, and unlocked.
11345  */
11346 static void
11347 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11348 {
11349 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11350 	ipxop_t *ipx = ipsq->ipsq_xop;
11351 
11352 	ASSERT(sipif != dipif);
11353 	ASSERT(sipif != virgipif);
11354 
11355 	/*
11356 	 * Grab all of the locks that protect the ipif in a defined order.
11357 	 */
11358 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11359 
11360 	ipif_clone(sipif, dipif);
11361 	if (virgipif != NULL) {
11362 		ipif_clone(virgipif, sipif);
11363 		mi_free(virgipif);
11364 	}
11365 
11366 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11367 
11368 	/*
11369 	 * Transfer ownership of the current xop, if necessary.
11370 	 */
11371 	if (ipx->ipx_current_ipif == sipif) {
11372 		ASSERT(ipx->ipx_pending_ipif == NULL);
11373 		mutex_enter(&ipx->ipx_lock);
11374 		ipx->ipx_current_ipif = dipif;
11375 		mutex_exit(&ipx->ipx_lock);
11376 	}
11377 
11378 	if (virgipif == NULL)
11379 		mi_free(sipif);
11380 }
11381 
11382 /*
11383  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11384  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11385  * be inserted into the first space available in the list. The value of
11386  * ipif_id will then be set to the appropriate value for its position.
11387  */
11388 static int
11389 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11390 {
11391 	ill_t *ill;
11392 	ipif_t *tipif;
11393 	ipif_t **tipifp;
11394 	int id;
11395 	ip_stack_t	*ipst;
11396 
11397 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11398 	    IAM_WRITER_IPIF(ipif));
11399 
11400 	ill = ipif->ipif_ill;
11401 	ASSERT(ill != NULL);
11402 	ipst = ill->ill_ipst;
11403 
11404 	/*
11405 	 * In the case of lo0:0 we already hold the ill_g_lock.
11406 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11407 	 * ipif_insert.
11408 	 */
11409 	if (acquire_g_lock)
11410 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11411 	mutex_enter(&ill->ill_lock);
11412 	id = ipif->ipif_id;
11413 	tipifp = &(ill->ill_ipif);
11414 	if (id == -1) {	/* need to find a real id */
11415 		id = 0;
11416 		while ((tipif = *tipifp) != NULL) {
11417 			ASSERT(tipif->ipif_id >= id);
11418 			if (tipif->ipif_id != id)
11419 				break; /* non-consecutive id */
11420 			id++;
11421 			tipifp = &(tipif->ipif_next);
11422 		}
11423 		/* limit number of logical interfaces */
11424 		if (id >= ipst->ips_ip_addrs_per_if) {
11425 			mutex_exit(&ill->ill_lock);
11426 			if (acquire_g_lock)
11427 				rw_exit(&ipst->ips_ill_g_lock);
11428 			return (-1);
11429 		}
11430 		ipif->ipif_id = id; /* assign new id */
11431 	} else if (id < ipst->ips_ip_addrs_per_if) {
11432 		/* we have a real id; insert ipif in the right place */
11433 		while ((tipif = *tipifp) != NULL) {
11434 			ASSERT(tipif->ipif_id != id);
11435 			if (tipif->ipif_id > id)
11436 				break; /* found correct location */
11437 			tipifp = &(tipif->ipif_next);
11438 		}
11439 	} else {
11440 		mutex_exit(&ill->ill_lock);
11441 		if (acquire_g_lock)
11442 			rw_exit(&ipst->ips_ill_g_lock);
11443 		return (-1);
11444 	}
11445 
11446 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11447 
11448 	ipif->ipif_next = tipif;
11449 	*tipifp = ipif;
11450 	mutex_exit(&ill->ill_lock);
11451 	if (acquire_g_lock)
11452 		rw_exit(&ipst->ips_ill_g_lock);
11453 
11454 	return (0);
11455 }
11456 
11457 static void
11458 ipif_remove(ipif_t *ipif)
11459 {
11460 	ipif_t	**ipifp;
11461 	ill_t	*ill = ipif->ipif_ill;
11462 
11463 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11464 
11465 	mutex_enter(&ill->ill_lock);
11466 	ipifp = &ill->ill_ipif;
11467 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11468 		if (*ipifp == ipif) {
11469 			*ipifp = ipif->ipif_next;
11470 			break;
11471 		}
11472 	}
11473 	mutex_exit(&ill->ill_lock);
11474 }
11475 
11476 /*
11477  * Allocate and initialize a new interface control structure.  (Always
11478  * called as writer.)
11479  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11480  * is not part of the global linked list of ills. ipif_seqid is unique
11481  * in the system and to preserve the uniqueness, it is assigned only
11482  * when ill becomes part of the global list. At that point ill will
11483  * have a name. If it doesn't get assigned here, it will get assigned
11484  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11485  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11486  * the interface flags or any other information from the DL_INFO_ACK for
11487  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11488  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11489  * second DL_INFO_ACK comes in from the driver.
11490  */
11491 static ipif_t *
11492 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11493     boolean_t insert)
11494 {
11495 	ipif_t	*ipif;
11496 	ip_stack_t *ipst = ill->ill_ipst;
11497 
11498 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11499 	    ill->ill_name, id, (void *)ill));
11500 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11501 
11502 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
11503 		return (NULL);
11504 	*ipif = ipif_zero;	/* start clean */
11505 
11506 	ipif->ipif_ill = ill;
11507 	ipif->ipif_id = id;	/* could be -1 */
11508 	/*
11509 	 * Inherit the zoneid from the ill; for the shared stack instance
11510 	 * this is always the global zone
11511 	 */
11512 	ipif->ipif_zoneid = ill->ill_zoneid;
11513 
11514 	ipif->ipif_refcnt = 0;
11515 
11516 	if (insert) {
11517 		if (ipif_insert(ipif, ire_type != IRE_LOOPBACK) != 0) {
11518 			mi_free(ipif);
11519 			return (NULL);
11520 		}
11521 		/* -1 id should have been replaced by real id */
11522 		id = ipif->ipif_id;
11523 		ASSERT(id >= 0);
11524 	}
11525 
11526 	if (ill->ill_name[0] != '\0')
11527 		ipif_assign_seqid(ipif);
11528 
11529 	/*
11530 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11531 	 * (which must not exist yet because the zeroth ipif is created once
11532 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11533 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11534 	 */
11535 	if (id == 0 && IS_IPMP(ill)) {
11536 		if (ipmp_illgrp_create(ill) == NULL) {
11537 			if (insert) {
11538 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11539 				ipif_remove(ipif);
11540 				rw_exit(&ipst->ips_ill_g_lock);
11541 			}
11542 			mi_free(ipif);
11543 			return (NULL);
11544 		}
11545 	}
11546 
11547 	/*
11548 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11549 	 * not up and can't be looked up until the ioctl completes and the
11550 	 * IPIF_CHANGING flag is cleared.
11551 	 */
11552 	mutex_enter(&ill->ill_lock);
11553 
11554 	ipif->ipif_ire_type = ire_type;
11555 
11556 	if (ipif->ipif_isv6) {
11557 		ill->ill_flags |= ILLF_IPV6;
11558 	} else {
11559 		ipaddr_t inaddr_any = INADDR_ANY;
11560 
11561 		ill->ill_flags |= ILLF_IPV4;
11562 
11563 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11564 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11565 		    &ipif->ipif_v6lcl_addr);
11566 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11567 		    &ipif->ipif_v6subnet);
11568 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11569 		    &ipif->ipif_v6net_mask);
11570 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11571 		    &ipif->ipif_v6brd_addr);
11572 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11573 		    &ipif->ipif_v6pp_dst_addr);
11574 	}
11575 
11576 	/*
11577 	 * Don't set the interface flags etc. now, will do it in
11578 	 * ip_ll_subnet_defaults.
11579 	 */
11580 	if (!initialize)
11581 		goto out;
11582 
11583 	/*
11584 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11585 	 * with no underlying interfaces (and thus an unknown broadcast
11586 	 * address length), but all interfaces that can be placed into an IPMP
11587 	 * group are required to be broadcast-capable.
11588 	 */
11589 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11590 		/*
11591 		 * Later detect lack of DLPI driver multicast capability by
11592 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11593 		 */
11594 		ill->ill_flags |= ILLF_MULTICAST;
11595 		if (!ipif->ipif_isv6)
11596 			ipif->ipif_flags |= IPIF_BROADCAST;
11597 	} else {
11598 		if (ill->ill_net_type != IRE_LOOPBACK) {
11599 			if (ipif->ipif_isv6)
11600 				/*
11601 				 * Note: xresolv interfaces will eventually need
11602 				 * NOARP set here as well, but that will require
11603 				 * those external resolvers to have some
11604 				 * knowledge of that flag and act appropriately.
11605 				 * Not to be changed at present.
11606 				 */
11607 				ill->ill_flags |= ILLF_NONUD;
11608 			else
11609 				ill->ill_flags |= ILLF_NOARP;
11610 		}
11611 		if (ill->ill_phys_addr_length == 0) {
11612 			if (IS_VNI(ill)) {
11613 				ipif->ipif_flags |= IPIF_NOXMIT;
11614 			} else {
11615 				/* pt-pt supports multicast. */
11616 				ill->ill_flags |= ILLF_MULTICAST;
11617 				if (ill->ill_net_type != IRE_LOOPBACK)
11618 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11619 			}
11620 		}
11621 	}
11622 out:
11623 	mutex_exit(&ill->ill_lock);
11624 	return (ipif);
11625 }
11626 
11627 /*
11628  * Remove the neighbor cache entries associated with this logical
11629  * interface.
11630  */
11631 int
11632 ipif_arp_down(ipif_t *ipif)
11633 {
11634 	ill_t	*ill = ipif->ipif_ill;
11635 	int	err = 0;
11636 
11637 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11638 	ASSERT(IAM_WRITER_IPIF(ipif));
11639 
11640 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11641 	    ill_t *, ill, ipif_t *, ipif);
11642 	ipif_nce_down(ipif);
11643 
11644 	/*
11645 	 * If this is the last ipif that is going down and there are no
11646 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11647 	 * clean up ARP completely.
11648 	 */
11649 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11650 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11651 		/*
11652 		 * If this was the last ipif on an IPMP interface, purge any
11653 		 * static ARP entries associated with it.
11654 		 */
11655 		if (IS_IPMP(ill))
11656 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11657 
11658 		/* UNBIND, DETACH */
11659 		err = arp_ll_down(ill);
11660 	}
11661 
11662 	return (err);
11663 }
11664 
11665 /*
11666  * Get the resolver set up for a new IP address.  (Always called as writer.)
11667  * Called both for IPv4 and IPv6 interfaces, though it only does some
11668  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11669  *
11670  * The enumerated value res_act tunes the behavior:
11671  * 	* Res_act_initial: set up all the resolver structures for a new
11672  *	  IP address.
11673  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11674  *	  ARP message in defense of the address.
11675  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11676  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11677  *
11678  * Returns zero on success, or an errno upon failure.
11679  */
11680 int
11681 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11682 {
11683 	ill_t		*ill = ipif->ipif_ill;
11684 	int		err;
11685 	boolean_t	was_dup;
11686 
11687 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11688 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11689 	ASSERT(IAM_WRITER_IPIF(ipif));
11690 
11691 	was_dup = B_FALSE;
11692 	if (res_act == Res_act_initial) {
11693 		ipif->ipif_addr_ready = 0;
11694 		/*
11695 		 * We're bringing an interface up here.  There's no way that we
11696 		 * should need to shut down ARP now.
11697 		 */
11698 		mutex_enter(&ill->ill_lock);
11699 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11700 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11701 			ill->ill_ipif_dup_count--;
11702 			was_dup = B_TRUE;
11703 		}
11704 		mutex_exit(&ill->ill_lock);
11705 	}
11706 	if (ipif->ipif_recovery_id != 0)
11707 		(void) untimeout(ipif->ipif_recovery_id);
11708 	ipif->ipif_recovery_id = 0;
11709 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11710 		ipif->ipif_addr_ready = 1;
11711 		return (0);
11712 	}
11713 	/* NDP will set the ipif_addr_ready flag when it's ready */
11714 	if (ill->ill_isv6)
11715 		return (0);
11716 
11717 	err = ipif_arp_up(ipif, res_act, was_dup);
11718 	return (err);
11719 }
11720 
11721 /*
11722  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11723  * when a link has just gone back up.
11724  */
11725 static void
11726 ipif_nce_start_dad(ipif_t *ipif)
11727 {
11728 	ncec_t *ncec;
11729 	ill_t *ill = ipif->ipif_ill;
11730 	boolean_t isv6 = ill->ill_isv6;
11731 
11732 	if (isv6) {
11733 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11734 		    &ipif->ipif_v6lcl_addr);
11735 	} else {
11736 		ipaddr_t v4addr;
11737 
11738 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11739 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11740 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11741 			/*
11742 			 * If we can't contact ARP for some reason,
11743 			 * that's not really a problem.  Just send
11744 			 * out the routing socket notification that
11745 			 * DAD completion would have done, and continue.
11746 			 */
11747 			ipif_mask_reply(ipif);
11748 			ipif_up_notify(ipif);
11749 			ipif->ipif_addr_ready = 1;
11750 			return;
11751 		}
11752 
11753 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11754 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11755 	}
11756 
11757 	if (ncec == NULL) {
11758 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11759 		    (void *)ipif));
11760 		return;
11761 	}
11762 	if (!nce_restart_dad(ncec)) {
11763 		/*
11764 		 * If we can't restart DAD for some reason, that's not really a
11765 		 * problem.  Just send out the routing socket notification that
11766 		 * DAD completion would have done, and continue.
11767 		 */
11768 		ipif_up_notify(ipif);
11769 		ipif->ipif_addr_ready = 1;
11770 	}
11771 	ncec_refrele(ncec);
11772 }
11773 
11774 /*
11775  * Restart duplicate address detection on all interfaces on the given ill.
11776  *
11777  * This is called when an interface transitions from down to up
11778  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11779  *
11780  * Note that since the underlying physical link has transitioned, we must cause
11781  * at least one routing socket message to be sent here, either via DAD
11782  * completion or just by default on the first ipif.  (If we don't do this, then
11783  * in.mpathd will see long delays when doing link-based failure recovery.)
11784  */
11785 void
11786 ill_restart_dad(ill_t *ill, boolean_t went_up)
11787 {
11788 	ipif_t *ipif;
11789 
11790 	if (ill == NULL)
11791 		return;
11792 
11793 	/*
11794 	 * If layer two doesn't support duplicate address detection, then just
11795 	 * send the routing socket message now and be done with it.
11796 	 */
11797 	if (!ill->ill_isv6 && arp_no_defense) {
11798 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11799 		return;
11800 	}
11801 
11802 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11803 		if (went_up) {
11804 
11805 			if (ipif->ipif_flags & IPIF_UP) {
11806 				ipif_nce_start_dad(ipif);
11807 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
11808 				/*
11809 				 * kick off the bring-up process now.
11810 				 */
11811 				ipif_do_recovery(ipif);
11812 			} else {
11813 				/*
11814 				 * Unfortunately, the first ipif is "special"
11815 				 * and represents the underlying ill in the
11816 				 * routing socket messages.  Thus, when this
11817 				 * one ipif is down, we must still notify so
11818 				 * that the user knows the IFF_RUNNING status
11819 				 * change.  (If the first ipif is up, then
11820 				 * we'll handle eventual routing socket
11821 				 * notification via DAD completion.)
11822 				 */
11823 				if (ipif == ill->ill_ipif) {
11824 					ip_rts_ifmsg(ill->ill_ipif,
11825 					    RTSQ_DEFAULT);
11826 				}
11827 			}
11828 		} else {
11829 			/*
11830 			 * After link down, we'll need to send a new routing
11831 			 * message when the link comes back, so clear
11832 			 * ipif_addr_ready.
11833 			 */
11834 			ipif->ipif_addr_ready = 0;
11835 		}
11836 	}
11837 
11838 	/*
11839 	 * If we've torn down links, then notify the user right away.
11840 	 */
11841 	if (!went_up)
11842 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11843 }
11844 
11845 static void
11846 ipsq_delete(ipsq_t *ipsq)
11847 {
11848 	ipxop_t *ipx = ipsq->ipsq_xop;
11849 
11850 	ipsq->ipsq_ipst = NULL;
11851 	ASSERT(ipsq->ipsq_phyint == NULL);
11852 	ASSERT(ipsq->ipsq_xop != NULL);
11853 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
11854 	ASSERT(ipx->ipx_pending_mp == NULL);
11855 	kmem_free(ipsq, sizeof (ipsq_t));
11856 }
11857 
11858 static int
11859 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
11860 {
11861 	int err = 0;
11862 	ipif_t *ipif;
11863 
11864 	if (ill == NULL)
11865 		return (0);
11866 
11867 	ASSERT(IAM_WRITER_ILL(ill));
11868 	ill->ill_up_ipifs = B_TRUE;
11869 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11870 		if (ipif->ipif_was_up) {
11871 			if (!(ipif->ipif_flags & IPIF_UP))
11872 				err = ipif_up(ipif, q, mp);
11873 			ipif->ipif_was_up = B_FALSE;
11874 			if (err != 0) {
11875 				ASSERT(err == EINPROGRESS);
11876 				return (err);
11877 			}
11878 		}
11879 	}
11880 	ill->ill_up_ipifs = B_FALSE;
11881 	return (0);
11882 }
11883 
11884 /*
11885  * This function is called to bring up all the ipifs that were up before
11886  * bringing the ill down via ill_down_ipifs().
11887  */
11888 int
11889 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
11890 {
11891 	int err;
11892 
11893 	ASSERT(IAM_WRITER_ILL(ill));
11894 
11895 	if (ill->ill_replumbing) {
11896 		ill->ill_replumbing = 0;
11897 		/*
11898 		 * Send down REPLUMB_DONE notification followed by the
11899 		 * BIND_REQ on the arp stream.
11900 		 */
11901 		if (!ill->ill_isv6)
11902 			arp_send_replumb_conf(ill);
11903 	}
11904 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
11905 	if (err != 0)
11906 		return (err);
11907 
11908 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
11909 }
11910 
11911 /*
11912  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
11913  * down the ipifs without sending DL_UNBIND_REQ to the driver.
11914  */
11915 static void
11916 ill_down_ipifs(ill_t *ill, boolean_t logical)
11917 {
11918 	ipif_t *ipif;
11919 
11920 	ASSERT(IAM_WRITER_ILL(ill));
11921 
11922 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11923 		/*
11924 		 * We go through the ipif_down logic even if the ipif
11925 		 * is already down, since routes can be added based
11926 		 * on down ipifs. Going through ipif_down once again
11927 		 * will delete any IREs created based on these routes.
11928 		 */
11929 		if (ipif->ipif_flags & IPIF_UP)
11930 			ipif->ipif_was_up = B_TRUE;
11931 
11932 		if (logical) {
11933 			(void) ipif_logical_down(ipif, NULL, NULL);
11934 			ipif_non_duplicate(ipif);
11935 			(void) ipif_down_tail(ipif);
11936 		} else {
11937 			(void) ipif_down(ipif, NULL, NULL);
11938 		}
11939 	}
11940 }
11941 
11942 /*
11943  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
11944  * a look again at valid source addresses.
11945  * This should be called each time after the set of source addresses has been
11946  * changed.
11947  */
11948 void
11949 ip_update_source_selection(ip_stack_t *ipst)
11950 {
11951 	/* We skip past SRC_GENERATION_VERIFY */
11952 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
11953 	    SRC_GENERATION_VERIFY)
11954 		atomic_add_32(&ipst->ips_src_generation, 1);
11955 }
11956 
11957 /*
11958  * Finish the group join started in ip_sioctl_groupname().
11959  */
11960 /* ARGSUSED */
11961 static void
11962 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
11963 {
11964 	ill_t		*ill = q->q_ptr;
11965 	phyint_t	*phyi = ill->ill_phyint;
11966 	ipmp_grp_t	*grp = phyi->phyint_grp;
11967 	ip_stack_t	*ipst = ill->ill_ipst;
11968 
11969 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
11970 	ASSERT(!IS_IPMP(ill) && grp != NULL);
11971 	ASSERT(IAM_WRITER_IPSQ(ipsq));
11972 
11973 	if (phyi->phyint_illv4 != NULL) {
11974 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
11975 		VERIFY(grp->gr_pendv4-- > 0);
11976 		rw_exit(&ipst->ips_ipmp_lock);
11977 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
11978 	}
11979 	if (phyi->phyint_illv6 != NULL) {
11980 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
11981 		VERIFY(grp->gr_pendv6-- > 0);
11982 		rw_exit(&ipst->ips_ipmp_lock);
11983 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
11984 	}
11985 	freemsg(mp);
11986 }
11987 
11988 /*
11989  * Process an SIOCSLIFGROUPNAME request.
11990  */
11991 /* ARGSUSED */
11992 int
11993 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11994     ip_ioctl_cmd_t *ipip, void *ifreq)
11995 {
11996 	struct lifreq	*lifr = ifreq;
11997 	ill_t		*ill = ipif->ipif_ill;
11998 	ip_stack_t	*ipst = ill->ill_ipst;
11999 	phyint_t	*phyi = ill->ill_phyint;
12000 	ipmp_grp_t	*grp = phyi->phyint_grp;
12001 	mblk_t		*ipsq_mp;
12002 	int		err = 0;
12003 
12004 	/*
12005 	 * Note that phyint_grp can only change here, where we're exclusive.
12006 	 */
12007 	ASSERT(IAM_WRITER_ILL(ill));
12008 
12009 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12010 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12011 		return (EINVAL);
12012 
12013 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12014 
12015 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12016 
12017 	/*
12018 	 * If the name hasn't changed, there's nothing to do.
12019 	 */
12020 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12021 		goto unlock;
12022 
12023 	/*
12024 	 * Handle requests to rename an IPMP meta-interface.
12025 	 *
12026 	 * Note that creation of the IPMP meta-interface is handled in
12027 	 * userland through the standard plumbing sequence.  As part of the
12028 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12029 	 * the name of the interface (see ipif_set_values_tail()).
12030 	 */
12031 	if (IS_IPMP(ill)) {
12032 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12033 		goto unlock;
12034 	}
12035 
12036 	/*
12037 	 * Handle requests to add or remove an IP interface from a group.
12038 	 */
12039 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12040 		/*
12041 		 * Moves are handled by first removing the interface from
12042 		 * its existing group, and then adding it to another group.
12043 		 * So, fail if it's already in a group.
12044 		 */
12045 		if (IS_UNDER_IPMP(ill)) {
12046 			err = EALREADY;
12047 			goto unlock;
12048 		}
12049 
12050 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12051 		if (grp == NULL) {
12052 			err = ENOENT;
12053 			goto unlock;
12054 		}
12055 
12056 		/*
12057 		 * Check if the phyint and its ills are suitable for
12058 		 * inclusion into the group.
12059 		 */
12060 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12061 			goto unlock;
12062 
12063 		/*
12064 		 * Checks pass; join the group, and enqueue the remaining
12065 		 * illgrp joins for when we've become part of the group xop
12066 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12067 		 * requires an mblk_t to scribble on, and since `mp' will be
12068 		 * freed as part of completing the ioctl, allocate another.
12069 		 */
12070 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12071 			err = ENOMEM;
12072 			goto unlock;
12073 		}
12074 
12075 		/*
12076 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12077 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12078 		 * before ip_join_illgrps() is called back.  See the comments
12079 		 * in ip_sioctl_plink_ipmp() for more.
12080 		 */
12081 		if (phyi->phyint_illv4 != NULL)
12082 			grp->gr_pendv4++;
12083 		if (phyi->phyint_illv6 != NULL)
12084 			grp->gr_pendv6++;
12085 
12086 		rw_exit(&ipst->ips_ipmp_lock);
12087 
12088 		ipmp_phyint_join_grp(phyi, grp);
12089 		ill_refhold(ill);
12090 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12091 		    SWITCH_OP, B_FALSE);
12092 		return (0);
12093 	} else {
12094 		/*
12095 		 * Request to remove the interface from a group.  If the
12096 		 * interface is not in a group, this trivially succeeds.
12097 		 */
12098 		rw_exit(&ipst->ips_ipmp_lock);
12099 		if (IS_UNDER_IPMP(ill))
12100 			ipmp_phyint_leave_grp(phyi);
12101 		return (0);
12102 	}
12103 unlock:
12104 	rw_exit(&ipst->ips_ipmp_lock);
12105 	return (err);
12106 }
12107 
12108 /*
12109  * Process an SIOCGLIFBINDING request.
12110  */
12111 /* ARGSUSED */
12112 int
12113 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12114     ip_ioctl_cmd_t *ipip, void *ifreq)
12115 {
12116 	ill_t		*ill;
12117 	struct lifreq	*lifr = ifreq;
12118 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12119 
12120 	if (!IS_IPMP(ipif->ipif_ill))
12121 		return (EINVAL);
12122 
12123 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12124 	if ((ill = ipif->ipif_bound_ill) == NULL)
12125 		lifr->lifr_binding[0] = '\0';
12126 	else
12127 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12128 	rw_exit(&ipst->ips_ipmp_lock);
12129 	return (0);
12130 }
12131 
12132 /*
12133  * Process an SIOCGLIFGROUPNAME request.
12134  */
12135 /* ARGSUSED */
12136 int
12137 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12138     ip_ioctl_cmd_t *ipip, void *ifreq)
12139 {
12140 	ipmp_grp_t	*grp;
12141 	struct lifreq	*lifr = ifreq;
12142 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12143 
12144 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12145 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12146 		lifr->lifr_groupname[0] = '\0';
12147 	else
12148 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12149 	rw_exit(&ipst->ips_ipmp_lock);
12150 	return (0);
12151 }
12152 
12153 /*
12154  * Process an SIOCGLIFGROUPINFO request.
12155  */
12156 /* ARGSUSED */
12157 int
12158 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12159     ip_ioctl_cmd_t *ipip, void *dummy)
12160 {
12161 	ipmp_grp_t	*grp;
12162 	lifgroupinfo_t	*lifgr;
12163 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12164 
12165 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12166 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12167 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12168 
12169 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12170 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12171 		rw_exit(&ipst->ips_ipmp_lock);
12172 		return (ENOENT);
12173 	}
12174 	ipmp_grp_info(grp, lifgr);
12175 	rw_exit(&ipst->ips_ipmp_lock);
12176 	return (0);
12177 }
12178 
12179 static void
12180 ill_dl_down(ill_t *ill)
12181 {
12182 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12183 
12184 	/*
12185 	 * The ill is down; unbind but stay attached since we're still
12186 	 * associated with a PPA. If we have negotiated DLPI capabilites
12187 	 * with the data link service provider (IDS_OK) then reset them.
12188 	 * The interval between unbinding and rebinding is potentially
12189 	 * unbounded hence we cannot assume things will be the same.
12190 	 * The DLPI capabilities will be probed again when the data link
12191 	 * is brought up.
12192 	 */
12193 	mblk_t	*mp = ill->ill_unbind_mp;
12194 
12195 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12196 
12197 	if (!ill->ill_replumbing) {
12198 		/* Free all ilms for this ill */
12199 		update_conn_ill(ill, ill->ill_ipst);
12200 	} else {
12201 		ill_leave_multicast(ill);
12202 	}
12203 
12204 	ill->ill_unbind_mp = NULL;
12205 	if (mp != NULL) {
12206 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12207 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12208 		    ill->ill_name));
12209 		mutex_enter(&ill->ill_lock);
12210 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12211 		mutex_exit(&ill->ill_lock);
12212 		/*
12213 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12214 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12215 		 * ill_capability_dld_disable disable rightaway. If this is not
12216 		 * an unplumb operation then the disable happens on receipt of
12217 		 * the capab ack via ip_rput_dlpi_writer ->
12218 		 * ill_capability_ack_thr. In both cases the order of
12219 		 * the operations seen by DLD is capability disable followed
12220 		 * by DL_UNBIND. Also the DLD capability disable needs a
12221 		 * cv_wait'able context.
12222 		 */
12223 		if (ill->ill_state_flags & ILL_CONDEMNED)
12224 			ill_capability_dld_disable(ill);
12225 		ill_capability_reset(ill, B_FALSE);
12226 		ill_dlpi_send(ill, mp);
12227 	}
12228 	mutex_enter(&ill->ill_lock);
12229 	ill->ill_dl_up = 0;
12230 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12231 	mutex_exit(&ill->ill_lock);
12232 }
12233 
12234 void
12235 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12236 {
12237 	union DL_primitives *dlp;
12238 	t_uscalar_t prim;
12239 	boolean_t waitack = B_FALSE;
12240 
12241 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12242 
12243 	dlp = (union DL_primitives *)mp->b_rptr;
12244 	prim = dlp->dl_primitive;
12245 
12246 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12247 	    dl_primstr(prim), prim, ill->ill_name));
12248 
12249 	switch (prim) {
12250 	case DL_PHYS_ADDR_REQ:
12251 	{
12252 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12253 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12254 		break;
12255 	}
12256 	case DL_BIND_REQ:
12257 		mutex_enter(&ill->ill_lock);
12258 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12259 		mutex_exit(&ill->ill_lock);
12260 		break;
12261 	}
12262 
12263 	/*
12264 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12265 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12266 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12267 	 */
12268 	mutex_enter(&ill->ill_lock);
12269 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12270 	    (prim == DL_UNBIND_REQ)) {
12271 		ill->ill_dlpi_pending = prim;
12272 		waitack = B_TRUE;
12273 	}
12274 
12275 	mutex_exit(&ill->ill_lock);
12276 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12277 	    char *, dl_primstr(prim), ill_t *, ill);
12278 	putnext(ill->ill_wq, mp);
12279 
12280 	/*
12281 	 * There is no ack for DL_NOTIFY_CONF messages
12282 	 */
12283 	if (waitack && prim == DL_NOTIFY_CONF)
12284 		ill_dlpi_done(ill, prim);
12285 }
12286 
12287 /*
12288  * Helper function for ill_dlpi_send().
12289  */
12290 /* ARGSUSED */
12291 static void
12292 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12293 {
12294 	ill_dlpi_send(q->q_ptr, mp);
12295 }
12296 
12297 /*
12298  * Send a DLPI control message to the driver but make sure there
12299  * is only one outstanding message. Uses ill_dlpi_pending to tell
12300  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12301  * when an ACK or a NAK is received to process the next queued message.
12302  */
12303 void
12304 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12305 {
12306 	mblk_t **mpp;
12307 
12308 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12309 
12310 	/*
12311 	 * To ensure that any DLPI requests for current exclusive operation
12312 	 * are always completely sent before any DLPI messages for other
12313 	 * operations, require writer access before enqueuing.
12314 	 */
12315 	if (!IAM_WRITER_ILL(ill)) {
12316 		ill_refhold(ill);
12317 		/* qwriter_ip() does the ill_refrele() */
12318 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12319 		    NEW_OP, B_TRUE);
12320 		return;
12321 	}
12322 
12323 	mutex_enter(&ill->ill_lock);
12324 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12325 		/* Must queue message. Tail insertion */
12326 		mpp = &ill->ill_dlpi_deferred;
12327 		while (*mpp != NULL)
12328 			mpp = &((*mpp)->b_next);
12329 
12330 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12331 		    "while %s pending\n", ill->ill_name,
12332 		    dl_primstr(ill->ill_dlpi_pending)));
12333 
12334 		*mpp = mp;
12335 		mutex_exit(&ill->ill_lock);
12336 		return;
12337 	}
12338 	mutex_exit(&ill->ill_lock);
12339 	ill_dlpi_dispatch(ill, mp);
12340 }
12341 
12342 static void
12343 ill_capability_send(ill_t *ill, mblk_t *mp)
12344 {
12345 	ill->ill_capab_pending_cnt++;
12346 	ill_dlpi_send(ill, mp);
12347 }
12348 
12349 void
12350 ill_capability_done(ill_t *ill)
12351 {
12352 	ASSERT(ill->ill_capab_pending_cnt != 0);
12353 
12354 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12355 
12356 	ill->ill_capab_pending_cnt--;
12357 	if (ill->ill_capab_pending_cnt == 0 &&
12358 	    ill->ill_dlpi_capab_state == IDCS_OK)
12359 		ill_capability_reset_alloc(ill);
12360 }
12361 
12362 /*
12363  * Send all deferred DLPI messages without waiting for their ACKs.
12364  */
12365 void
12366 ill_dlpi_send_deferred(ill_t *ill)
12367 {
12368 	mblk_t *mp, *nextmp;
12369 
12370 	/*
12371 	 * Clear ill_dlpi_pending so that the message is not queued in
12372 	 * ill_dlpi_send().
12373 	 */
12374 	mutex_enter(&ill->ill_lock);
12375 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12376 	mp = ill->ill_dlpi_deferred;
12377 	ill->ill_dlpi_deferred = NULL;
12378 	mutex_exit(&ill->ill_lock);
12379 
12380 	for (; mp != NULL; mp = nextmp) {
12381 		nextmp = mp->b_next;
12382 		mp->b_next = NULL;
12383 		ill_dlpi_send(ill, mp);
12384 	}
12385 }
12386 
12387 /*
12388  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12389  */
12390 boolean_t
12391 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12392 {
12393 	t_uscalar_t pending;
12394 
12395 	mutex_enter(&ill->ill_lock);
12396 	if (ill->ill_dlpi_pending == prim) {
12397 		mutex_exit(&ill->ill_lock);
12398 		return (B_TRUE);
12399 	}
12400 
12401 	/*
12402 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12403 	 * without waiting, so don't print any warnings in that case.
12404 	 */
12405 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12406 		mutex_exit(&ill->ill_lock);
12407 		return (B_FALSE);
12408 	}
12409 	pending = ill->ill_dlpi_pending;
12410 	mutex_exit(&ill->ill_lock);
12411 
12412 	if (pending == DL_PRIM_INVAL) {
12413 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12414 		    "received unsolicited ack for %s on %s\n",
12415 		    dl_primstr(prim), ill->ill_name);
12416 	} else {
12417 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12418 		    "received unexpected ack for %s on %s (expecting %s)\n",
12419 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12420 	}
12421 	return (B_FALSE);
12422 }
12423 
12424 /*
12425  * Complete the current DLPI operation associated with `prim' on `ill' and
12426  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12427  * operations and the ill's current exclusive IPSQ operation has finished
12428  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12429  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12430  * the comments above ipsq_current_finish() for details.
12431  */
12432 void
12433 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12434 {
12435 	mblk_t *mp;
12436 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12437 	ipxop_t *ipx = ipsq->ipsq_xop;
12438 
12439 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12440 	mutex_enter(&ill->ill_lock);
12441 
12442 	ASSERT(prim != DL_PRIM_INVAL);
12443 	ASSERT(ill->ill_dlpi_pending == prim);
12444 
12445 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12446 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12447 
12448 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12449 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12450 		if (ipx->ipx_current_done) {
12451 			mutex_enter(&ipx->ipx_lock);
12452 			ipx->ipx_current_ipif = NULL;
12453 			mutex_exit(&ipx->ipx_lock);
12454 		}
12455 		cv_signal(&ill->ill_cv);
12456 		mutex_exit(&ill->ill_lock);
12457 		return;
12458 	}
12459 
12460 	ill->ill_dlpi_deferred = mp->b_next;
12461 	mp->b_next = NULL;
12462 	mutex_exit(&ill->ill_lock);
12463 
12464 	ill_dlpi_dispatch(ill, mp);
12465 }
12466 
12467 /*
12468  * Queue a (multicast) DLPI control message to be sent to the driver by
12469  * later calling ill_dlpi_send_queued.
12470  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12471  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12472  * for the same group to race.
12473  * We send DLPI control messages in order using ill_lock.
12474  * For IPMP we should be called on the cast_ill.
12475  */
12476 void
12477 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12478 {
12479 	mblk_t **mpp;
12480 
12481 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12482 
12483 	mutex_enter(&ill->ill_lock);
12484 	/* Must queue message. Tail insertion */
12485 	mpp = &ill->ill_dlpi_deferred;
12486 	while (*mpp != NULL)
12487 		mpp = &((*mpp)->b_next);
12488 
12489 	*mpp = mp;
12490 	mutex_exit(&ill->ill_lock);
12491 }
12492 
12493 /*
12494  * Send the messages that were queued. Make sure there is only
12495  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12496  * when an ACK or a NAK is received to process the next queued message.
12497  * For IPMP we are called on the upper ill, but when send what is queued
12498  * on the cast_ill.
12499  */
12500 void
12501 ill_dlpi_send_queued(ill_t *ill)
12502 {
12503 	mblk_t	*mp;
12504 	union DL_primitives *dlp;
12505 	t_uscalar_t prim;
12506 	ill_t *release_ill = NULL;
12507 
12508 	if (IS_IPMP(ill)) {
12509 		/* On the upper IPMP ill. */
12510 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12511 		if (release_ill == NULL) {
12512 			/* Avoid ever sending anything down to the ipmpstub */
12513 			return;
12514 		}
12515 		ill = release_ill;
12516 	}
12517 	mutex_enter(&ill->ill_lock);
12518 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12519 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12520 			/* Can't send. Somebody else will send it */
12521 			mutex_exit(&ill->ill_lock);
12522 			goto done;
12523 		}
12524 		ill->ill_dlpi_deferred = mp->b_next;
12525 		mp->b_next = NULL;
12526 		if (!ill->ill_dl_up) {
12527 			/*
12528 			 * Nobody there. All multicast addresses will be
12529 			 * re-joined when we get the DL_BIND_ACK bringing the
12530 			 * interface up.
12531 			 */
12532 			freemsg(mp);
12533 			continue;
12534 		}
12535 		dlp = (union DL_primitives *)mp->b_rptr;
12536 		prim = dlp->dl_primitive;
12537 
12538 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12539 		    (prim == DL_UNBIND_REQ)) {
12540 			ill->ill_dlpi_pending = prim;
12541 		}
12542 		mutex_exit(&ill->ill_lock);
12543 
12544 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12545 		    char *, dl_primstr(prim), ill_t *, ill);
12546 		putnext(ill->ill_wq, mp);
12547 		mutex_enter(&ill->ill_lock);
12548 	}
12549 	mutex_exit(&ill->ill_lock);
12550 done:
12551 	if (release_ill != NULL)
12552 		ill_refrele(release_ill);
12553 }
12554 
12555 /*
12556  * Queue an IP (IGMP/MLD) message to be sent by IP from
12557  * ill_mcast_send_queued
12558  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12559  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12560  * group to race.
12561  * We send them in order using ill_lock.
12562  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12563  */
12564 void
12565 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12566 {
12567 	mblk_t **mpp;
12568 	ill_t *release_ill = NULL;
12569 
12570 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12571 
12572 	if (IS_IPMP(ill)) {
12573 		/* On the upper IPMP ill. */
12574 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12575 		if (release_ill == NULL) {
12576 			/* Discard instead of queuing for the ipmp interface */
12577 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12578 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12579 			    mp, ill);
12580 			freemsg(mp);
12581 			return;
12582 		}
12583 		ill = release_ill;
12584 	}
12585 
12586 	mutex_enter(&ill->ill_lock);
12587 	/* Must queue message. Tail insertion */
12588 	mpp = &ill->ill_mcast_deferred;
12589 	while (*mpp != NULL)
12590 		mpp = &((*mpp)->b_next);
12591 
12592 	*mpp = mp;
12593 	mutex_exit(&ill->ill_lock);
12594 	if (release_ill != NULL)
12595 		ill_refrele(release_ill);
12596 }
12597 
12598 /*
12599  * Send the IP packets that were queued by ill_mcast_queue.
12600  * These are IGMP/MLD packets.
12601  *
12602  * For IPMP we are called on the upper ill, but when send what is queued
12603  * on the cast_ill.
12604  *
12605  * Request loopback of the report if we are acting as a multicast
12606  * router, so that the process-level routing demon can hear it.
12607  * This will run multiple times for the same group if there are members
12608  * on the same group for multiple ipif's on the same ill. The
12609  * igmp_input/mld_input code will suppress this due to the loopback thus we
12610  * always loopback membership report.
12611  *
12612  * We also need to make sure that this does not get load balanced
12613  * by IPMP. We do this by passing an ill to ip_output_simple.
12614  */
12615 void
12616 ill_mcast_send_queued(ill_t *ill)
12617 {
12618 	mblk_t	*mp;
12619 	ip_xmit_attr_t ixas;
12620 	ill_t *release_ill = NULL;
12621 
12622 	if (IS_IPMP(ill)) {
12623 		/* On the upper IPMP ill. */
12624 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12625 		if (release_ill == NULL) {
12626 			/*
12627 			 * We should have no messages on the ipmp interface
12628 			 * but no point in trying to send them.
12629 			 */
12630 			return;
12631 		}
12632 		ill = release_ill;
12633 	}
12634 	bzero(&ixas, sizeof (ixas));
12635 	ixas.ixa_zoneid = ALL_ZONES;
12636 	ixas.ixa_cred = kcred;
12637 	ixas.ixa_cpid = NOPID;
12638 	ixas.ixa_tsl = NULL;
12639 	/*
12640 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12641 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12642 	 * That is necessary to handle IGMP/MLD snooping switches.
12643 	 */
12644 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12645 	ixas.ixa_ipst = ill->ill_ipst;
12646 
12647 	mutex_enter(&ill->ill_lock);
12648 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12649 		ill->ill_mcast_deferred = mp->b_next;
12650 		mp->b_next = NULL;
12651 		if (!ill->ill_dl_up) {
12652 			/*
12653 			 * Nobody there. Just drop the ip packets.
12654 			 * IGMP/MLD will resend later, if this is a replumb.
12655 			 */
12656 			freemsg(mp);
12657 			continue;
12658 		}
12659 		mutex_enter(&ill->ill_phyint->phyint_lock);
12660 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12661 			/*
12662 			 * When the ill is getting deactivated, we only want to
12663 			 * send the DLPI messages, so drop IGMP/MLD packets.
12664 			 * DLPI messages are handled by ill_dlpi_send_queued()
12665 			 */
12666 			mutex_exit(&ill->ill_phyint->phyint_lock);
12667 			freemsg(mp);
12668 			continue;
12669 		}
12670 		mutex_exit(&ill->ill_phyint->phyint_lock);
12671 		mutex_exit(&ill->ill_lock);
12672 
12673 		/* Check whether we are sending IPv4 or IPv6. */
12674 		if (ill->ill_isv6) {
12675 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12676 
12677 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12678 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12679 		} else {
12680 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12681 
12682 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12683 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12684 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12685 		}
12686 
12687 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12688 		(void) ip_output_simple(mp, &ixas);
12689 		ixa_cleanup(&ixas);
12690 
12691 		mutex_enter(&ill->ill_lock);
12692 	}
12693 	mutex_exit(&ill->ill_lock);
12694 
12695 done:
12696 	if (release_ill != NULL)
12697 		ill_refrele(release_ill);
12698 }
12699 
12700 /*
12701  * Take down a specific interface, but don't lose any information about it.
12702  * (Always called as writer.)
12703  * This function goes through the down sequence even if the interface is
12704  * already down. There are 2 reasons.
12705  * a. Currently we permit interface routes that depend on down interfaces
12706  *    to be added. This behaviour itself is questionable. However it appears
12707  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12708  *    time. We go thru the cleanup in order to remove these routes.
12709  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12710  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12711  *    down, but we need to cleanup i.e. do ill_dl_down and
12712  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12713  *
12714  * IP-MT notes:
12715  *
12716  * Model of reference to interfaces.
12717  *
12718  * The following members in ipif_t track references to the ipif.
12719  *	int     ipif_refcnt;    Active reference count
12720  *
12721  * The following members in ill_t track references to the ill.
12722  *	int             ill_refcnt;     active refcnt
12723  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12724  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12725  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12726  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12727  *
12728  * Reference to an ipif or ill can be obtained in any of the following ways.
12729  *
12730  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12731  * Pointers to ipif / ill from other data structures viz ire and conn.
12732  * Implicit reference to the ipif / ill by holding a reference to the ire.
12733  *
12734  * The ipif/ill lookup functions return a reference held ipif / ill.
12735  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12736  * This is a purely dynamic reference count associated with threads holding
12737  * references to the ipif / ill. Pointers from other structures do not
12738  * count towards this reference count.
12739  *
12740  * ill_ire_cnt is the number of ire's associated with the
12741  * ill. This is incremented whenever a new ire is created referencing the
12742  * ill. This is done atomically inside ire_add_v[46] where the ire is
12743  * actually added to the ire hash table. The count is decremented in
12744  * ire_inactive where the ire is destroyed.
12745  *
12746  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12747  * This is incremented atomically in
12748  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12749  * table. Similarly it is decremented in ncec_inactive() where the ncec
12750  * is destroyed.
12751  *
12752  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12753  * incremented atomically in nce_add() where the nce is actually added to the
12754  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12755  * is destroyed.
12756  *
12757  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12758  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12759  *
12760  * Flow of ioctls involving interface down/up
12761  *
12762  * The following is the sequence of an attempt to set some critical flags on an
12763  * up interface.
12764  * ip_sioctl_flags
12765  * ipif_down
12766  * wait for ipif to be quiescent
12767  * ipif_down_tail
12768  * ip_sioctl_flags_tail
12769  *
12770  * All set ioctls that involve down/up sequence would have a skeleton similar
12771  * to the above. All the *tail functions are called after the refcounts have
12772  * dropped to the appropriate values.
12773  *
12774  * SIOC ioctls during the IPIF_CHANGING interval.
12775  *
12776  * Threads handling SIOC set ioctls serialize on the squeue, but this
12777  * is not done for SIOC get ioctls. Since a set ioctl can cause several
12778  * steps of internal changes to the state, some of which are visible in
12779  * ipif_flags (such as IFF_UP being cleared and later set), and we want
12780  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
12781  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
12782  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
12783  * the current exclusive operation completes. The IPIF_CHANGING check
12784  * and enqueue is atomic using the ill_lock and ipsq_lock. The
12785  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
12786  * change while the ill_lock is held. Before dropping the ill_lock we acquire
12787  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
12788  * until we release the ipsq_lock, even though the ill/ipif state flags
12789  * can change after we drop the ill_lock.
12790  */
12791 int
12792 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12793 {
12794 	ill_t		*ill = ipif->ipif_ill;
12795 	conn_t		*connp;
12796 	boolean_t	success;
12797 	boolean_t	ipif_was_up = B_FALSE;
12798 	ip_stack_t	*ipst = ill->ill_ipst;
12799 
12800 	ASSERT(IAM_WRITER_IPIF(ipif));
12801 
12802 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12803 
12804 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
12805 	    ill_t *, ill, ipif_t *, ipif);
12806 
12807 	if (ipif->ipif_flags & IPIF_UP) {
12808 		mutex_enter(&ill->ill_lock);
12809 		ipif->ipif_flags &= ~IPIF_UP;
12810 		ASSERT(ill->ill_ipif_up_count > 0);
12811 		--ill->ill_ipif_up_count;
12812 		mutex_exit(&ill->ill_lock);
12813 		ipif_was_up = B_TRUE;
12814 		/* Update status in SCTP's list */
12815 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
12816 		ill_nic_event_dispatch(ipif->ipif_ill,
12817 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
12818 	}
12819 
12820 	/*
12821 	 * Blow away memberships we established in ipif_multicast_up().
12822 	 */
12823 	ipif_multicast_down(ipif);
12824 
12825 	/*
12826 	 * Remove from the mapping for __sin6_src_id. We insert only
12827 	 * when the address is not INADDR_ANY. As IPv4 addresses are
12828 	 * stored as mapped addresses, we need to check for mapped
12829 	 * INADDR_ANY also.
12830 	 */
12831 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12832 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
12833 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
12834 		int err;
12835 
12836 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
12837 		    ipif->ipif_zoneid, ipst);
12838 		if (err != 0) {
12839 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
12840 		}
12841 	}
12842 
12843 	if (ipif_was_up) {
12844 		/* only delete if we'd added ire's before */
12845 		if (ipif->ipif_isv6)
12846 			ipif_delete_ires_v6(ipif);
12847 		else
12848 			ipif_delete_ires_v4(ipif);
12849 	}
12850 
12851 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
12852 		/*
12853 		 * Since the interface is now down, it may have just become
12854 		 * inactive.  Note that this needs to be done even for a
12855 		 * lll_logical_down(), or ARP entries will not get correctly
12856 		 * restored when the interface comes back up.
12857 		 */
12858 		if (IS_UNDER_IPMP(ill))
12859 			ipmp_ill_refresh_active(ill);
12860 	}
12861 
12862 	/*
12863 	 * neighbor-discovery or arp entries for this interface. The ipif
12864 	 * has to be quiesced, so we walk all the nce's and delete those
12865 	 * that point at the ipif->ipif_ill. At the same time, we also
12866 	 * update IPMP so that ipifs for data addresses are unbound. We dont
12867 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
12868 	 * that for ipif_down_tail()
12869 	 */
12870 	ipif_nce_down(ipif);
12871 
12872 	/*
12873 	 * If this is the last ipif on the ill, we also need to remove
12874 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
12875 	 * never succeed.
12876 	 */
12877 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
12878 		ire_walk_ill(0, 0, ill_downi, ill, ill);
12879 
12880 	/*
12881 	 * Walk all CONNs that can have a reference on an ire for this
12882 	 * ipif (we actually walk all that now have stale references).
12883 	 */
12884 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
12885 
12886 	/*
12887 	 * If mp is NULL the caller will wait for the appropriate refcnt.
12888 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
12889 	 * and ill_delete -> ipif_free -> ipif_down
12890 	 */
12891 	if (mp == NULL) {
12892 		ASSERT(q == NULL);
12893 		return (0);
12894 	}
12895 
12896 	if (CONN_Q(q)) {
12897 		connp = Q_TO_CONN(q);
12898 		mutex_enter(&connp->conn_lock);
12899 	} else {
12900 		connp = NULL;
12901 	}
12902 	mutex_enter(&ill->ill_lock);
12903 	/*
12904 	 * Are there any ire's pointing to this ipif that are still active ?
12905 	 * If this is the last ipif going down, are there any ire's pointing
12906 	 * to this ill that are still active ?
12907 	 */
12908 	if (ipif_is_quiescent(ipif)) {
12909 		mutex_exit(&ill->ill_lock);
12910 		if (connp != NULL)
12911 			mutex_exit(&connp->conn_lock);
12912 		return (0);
12913 	}
12914 
12915 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
12916 	    ill->ill_name, (void *)ill));
12917 	/*
12918 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
12919 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
12920 	 * which in turn is called by the last refrele on the ipif/ill/ire.
12921 	 */
12922 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
12923 	if (!success) {
12924 		/* The conn is closing. So just return */
12925 		ASSERT(connp != NULL);
12926 		mutex_exit(&ill->ill_lock);
12927 		mutex_exit(&connp->conn_lock);
12928 		return (EINTR);
12929 	}
12930 
12931 	mutex_exit(&ill->ill_lock);
12932 	if (connp != NULL)
12933 		mutex_exit(&connp->conn_lock);
12934 	return (EINPROGRESS);
12935 }
12936 
12937 int
12938 ipif_down_tail(ipif_t *ipif)
12939 {
12940 	ill_t	*ill = ipif->ipif_ill;
12941 	int	err = 0;
12942 
12943 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
12944 	    ill_t *, ill, ipif_t *, ipif);
12945 
12946 	/*
12947 	 * Skip any loopback interface (null wq).
12948 	 * If this is the last logical interface on the ill
12949 	 * have ill_dl_down tell the driver we are gone (unbind)
12950 	 * Note that lun 0 can ipif_down even though
12951 	 * there are other logical units that are up.
12952 	 * This occurs e.g. when we change a "significant" IFF_ flag.
12953 	 */
12954 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
12955 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12956 	    ill->ill_dl_up) {
12957 		ill_dl_down(ill);
12958 	}
12959 	if (!ipif->ipif_isv6)
12960 		err = ipif_arp_down(ipif);
12961 
12962 	ill->ill_logical_down = 0;
12963 
12964 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
12965 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
12966 	return (err);
12967 }
12968 
12969 /*
12970  * Bring interface logically down without bringing the physical interface
12971  * down e.g. when the netmask is changed. This avoids long lasting link
12972  * negotiations between an ethernet interface and a certain switches.
12973  */
12974 static int
12975 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12976 {
12977 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
12978 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
12979 
12980 	/*
12981 	 * The ill_logical_down flag is a transient flag. It is set here
12982 	 * and is cleared once the down has completed in ipif_down_tail.
12983 	 * This flag does not indicate whether the ill stream is in the
12984 	 * DL_BOUND state with the driver. Instead this flag is used by
12985 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
12986 	 * the driver. The state of the ill stream i.e. whether it is
12987 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
12988 	 */
12989 	ipif->ipif_ill->ill_logical_down = 1;
12990 	return (ipif_down(ipif, q, mp));
12991 }
12992 
12993 /*
12994  * Initiate deallocate of an IPIF. Always called as writer. Called by
12995  * ill_delete or ip_sioctl_removeif.
12996  */
12997 static void
12998 ipif_free(ipif_t *ipif)
12999 {
13000 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13001 
13002 	ASSERT(IAM_WRITER_IPIF(ipif));
13003 
13004 	if (ipif->ipif_recovery_id != 0)
13005 		(void) untimeout(ipif->ipif_recovery_id);
13006 	ipif->ipif_recovery_id = 0;
13007 
13008 	/*
13009 	 * Take down the interface. We can be called either from ill_delete
13010 	 * or from ip_sioctl_removeif.
13011 	 */
13012 	(void) ipif_down(ipif, NULL, NULL);
13013 
13014 	/*
13015 	 * Now that the interface is down, there's no chance it can still
13016 	 * become a duplicate.  Cancel any timer that may have been set while
13017 	 * tearing down.
13018 	 */
13019 	if (ipif->ipif_recovery_id != 0)
13020 		(void) untimeout(ipif->ipif_recovery_id);
13021 	ipif->ipif_recovery_id = 0;
13022 
13023 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13024 	/* Remove pointers to this ill in the multicast routing tables */
13025 	reset_mrt_vif_ipif(ipif);
13026 	/* If necessary, clear the cached source ipif rotor. */
13027 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13028 		ipif->ipif_ill->ill_src_ipif = NULL;
13029 	rw_exit(&ipst->ips_ill_g_lock);
13030 }
13031 
13032 static void
13033 ipif_free_tail(ipif_t *ipif)
13034 {
13035 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13036 
13037 	/*
13038 	 * Need to hold both ill_g_lock and ill_lock while
13039 	 * inserting or removing an ipif from the linked list
13040 	 * of ipifs hanging off the ill.
13041 	 */
13042 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13043 
13044 #ifdef DEBUG
13045 	ipif_trace_cleanup(ipif);
13046 #endif
13047 
13048 	/* Ask SCTP to take it out of it list */
13049 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13050 
13051 	/* Get it out of the ILL interface list. */
13052 	ipif_remove(ipif);
13053 	rw_exit(&ipst->ips_ill_g_lock);
13054 
13055 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13056 	ASSERT(ipif->ipif_recovery_id == 0);
13057 	ASSERT(ipif->ipif_ire_local == NULL);
13058 
13059 	/* Free the memory. */
13060 	mi_free(ipif);
13061 }
13062 
13063 /*
13064  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13065  * is zero.
13066  */
13067 void
13068 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13069 {
13070 	char	lbuf[LIFNAMSIZ];
13071 	char	*name;
13072 	size_t	name_len;
13073 
13074 	buf[0] = '\0';
13075 	name = ipif->ipif_ill->ill_name;
13076 	name_len = ipif->ipif_ill->ill_name_length;
13077 	if (ipif->ipif_id != 0) {
13078 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13079 		    ipif->ipif_id);
13080 		name = lbuf;
13081 		name_len = mi_strlen(name) + 1;
13082 	}
13083 	len -= 1;
13084 	buf[len] = '\0';
13085 	len = MIN(len, name_len);
13086 	bcopy(name, buf, len);
13087 }
13088 
13089 /*
13090  * Sets `buf' to an ill name.
13091  */
13092 void
13093 ill_get_name(const ill_t *ill, char *buf, int len)
13094 {
13095 	char	*name;
13096 	size_t	name_len;
13097 
13098 	name = ill->ill_name;
13099 	name_len = ill->ill_name_length;
13100 	len -= 1;
13101 	buf[len] = '\0';
13102 	len = MIN(len, name_len);
13103 	bcopy(name, buf, len);
13104 }
13105 
13106 /*
13107  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13108  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13109  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13110  * (May be called as writer.)
13111  */
13112 static ipif_t *
13113 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13114     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13115 {
13116 	char	*cp;
13117 	char	*endp;
13118 	long	id;
13119 	ill_t	*ill;
13120 	ipif_t	*ipif;
13121 	uint_t	ire_type;
13122 	boolean_t did_alloc = B_FALSE;
13123 
13124 	/*
13125 	 * If the caller wants to us to create the ipif, make sure we have a
13126 	 * valid zoneid
13127 	 */
13128 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13129 
13130 	if (namelen == 0) {
13131 		return (NULL);
13132 	}
13133 
13134 	*exists = B_FALSE;
13135 	/* Look for a colon in the name. */
13136 	endp = &name[namelen];
13137 	for (cp = endp; --cp > name; ) {
13138 		if (*cp == IPIF_SEPARATOR_CHAR)
13139 			break;
13140 	}
13141 
13142 	if (*cp == IPIF_SEPARATOR_CHAR) {
13143 		/*
13144 		 * Reject any non-decimal aliases for logical
13145 		 * interfaces. Aliases with leading zeroes
13146 		 * are also rejected as they introduce ambiguity
13147 		 * in the naming of the interfaces.
13148 		 * In order to confirm with existing semantics,
13149 		 * and to not break any programs/script relying
13150 		 * on that behaviour, if<0>:0 is considered to be
13151 		 * a valid interface.
13152 		 *
13153 		 * If alias has two or more digits and the first
13154 		 * is zero, fail.
13155 		 */
13156 		if (&cp[2] < endp && cp[1] == '0') {
13157 			return (NULL);
13158 		}
13159 	}
13160 
13161 	if (cp <= name) {
13162 		cp = endp;
13163 	} else {
13164 		*cp = '\0';
13165 	}
13166 
13167 	/*
13168 	 * Look up the ILL, based on the portion of the name
13169 	 * before the slash. ill_lookup_on_name returns a held ill.
13170 	 * Temporary to check whether ill exists already. If so
13171 	 * ill_lookup_on_name will clear it.
13172 	 */
13173 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13174 	    &did_alloc, ipst);
13175 	if (cp != endp)
13176 		*cp = IPIF_SEPARATOR_CHAR;
13177 	if (ill == NULL)
13178 		return (NULL);
13179 
13180 	/* Establish the unit number in the name. */
13181 	id = 0;
13182 	if (cp < endp && *endp == '\0') {
13183 		/* If there was a colon, the unit number follows. */
13184 		cp++;
13185 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13186 			ill_refrele(ill);
13187 			return (NULL);
13188 		}
13189 	}
13190 
13191 	mutex_enter(&ill->ill_lock);
13192 	/* Now see if there is an IPIF with this unit number. */
13193 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13194 		if (ipif->ipif_id == id) {
13195 			if (zoneid != ALL_ZONES &&
13196 			    zoneid != ipif->ipif_zoneid &&
13197 			    ipif->ipif_zoneid != ALL_ZONES) {
13198 				mutex_exit(&ill->ill_lock);
13199 				ill_refrele(ill);
13200 				return (NULL);
13201 			}
13202 			if (IPIF_CAN_LOOKUP(ipif)) {
13203 				ipif_refhold_locked(ipif);
13204 				mutex_exit(&ill->ill_lock);
13205 				if (!did_alloc)
13206 					*exists = B_TRUE;
13207 				/*
13208 				 * Drop locks before calling ill_refrele
13209 				 * since it can potentially call into
13210 				 * ipif_ill_refrele_tail which can end up
13211 				 * in trying to acquire any lock.
13212 				 */
13213 				ill_refrele(ill);
13214 				return (ipif);
13215 			}
13216 		}
13217 	}
13218 
13219 	if (!do_alloc) {
13220 		mutex_exit(&ill->ill_lock);
13221 		ill_refrele(ill);
13222 		return (NULL);
13223 	}
13224 
13225 	/*
13226 	 * If none found, atomically allocate and return a new one.
13227 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13228 	 * to support "receive only" use of lo0:1 etc. as is still done
13229 	 * below as an initial guess.
13230 	 * However, this is now likely to be overriden later in ipif_up_done()
13231 	 * when we know for sure what address has been configured on the
13232 	 * interface, since we might have more than one loopback interface
13233 	 * with a loopback address, e.g. in the case of zones, and all the
13234 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13235 	 */
13236 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13237 		ire_type = IRE_LOOPBACK;
13238 	else
13239 		ire_type = IRE_LOCAL;
13240 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE);
13241 	if (ipif != NULL)
13242 		ipif_refhold_locked(ipif);
13243 	mutex_exit(&ill->ill_lock);
13244 	ill_refrele(ill);
13245 	return (ipif);
13246 }
13247 
13248 /*
13249  * This routine is called whenever a new address comes up on an ipif.  If
13250  * we are configured to respond to address mask requests, then we are supposed
13251  * to broadcast an address mask reply at this time.  This routine is also
13252  * called if we are already up, but a netmask change is made.  This is legal
13253  * but might not make the system manager very popular.	(May be called
13254  * as writer.)
13255  */
13256 void
13257 ipif_mask_reply(ipif_t *ipif)
13258 {
13259 	icmph_t	*icmph;
13260 	ipha_t	*ipha;
13261 	mblk_t	*mp;
13262 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13263 	ip_xmit_attr_t ixas;
13264 
13265 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13266 
13267 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13268 		return;
13269 
13270 	/* ICMP mask reply is IPv4 only */
13271 	ASSERT(!ipif->ipif_isv6);
13272 	/* ICMP mask reply is not for a loopback interface */
13273 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13274 
13275 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13276 		return;
13277 
13278 	mp = allocb(REPLY_LEN, BPRI_HI);
13279 	if (mp == NULL)
13280 		return;
13281 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13282 
13283 	ipha = (ipha_t *)mp->b_rptr;
13284 	bzero(ipha, REPLY_LEN);
13285 	*ipha = icmp_ipha;
13286 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13287 	ipha->ipha_src = ipif->ipif_lcl_addr;
13288 	ipha->ipha_dst = ipif->ipif_brd_addr;
13289 	ipha->ipha_length = htons(REPLY_LEN);
13290 	ipha->ipha_ident = 0;
13291 
13292 	icmph = (icmph_t *)&ipha[1];
13293 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13294 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13295 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13296 
13297 	bzero(&ixas, sizeof (ixas));
13298 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13299 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13300 	ixas.ixa_zoneid = ALL_ZONES;
13301 	ixas.ixa_ifindex = 0;
13302 	ixas.ixa_ipst = ipst;
13303 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13304 	(void) ip_output_simple(mp, &ixas);
13305 	ixa_cleanup(&ixas);
13306 #undef	REPLY_LEN
13307 }
13308 
13309 /*
13310  * Join the ipif specific multicast groups.
13311  * Must be called after a mapping has been set up in the resolver.  (Always
13312  * called as writer.)
13313  */
13314 void
13315 ipif_multicast_up(ipif_t *ipif)
13316 {
13317 	int err;
13318 	ill_t *ill;
13319 	ilm_t *ilm;
13320 
13321 	ASSERT(IAM_WRITER_IPIF(ipif));
13322 
13323 	ill = ipif->ipif_ill;
13324 
13325 	ip1dbg(("ipif_multicast_up\n"));
13326 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13327 	    ipif->ipif_allhosts_ilm != NULL)
13328 		return;
13329 
13330 	if (ipif->ipif_isv6) {
13331 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13332 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13333 
13334 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13335 
13336 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13337 			return;
13338 
13339 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13340 
13341 		/*
13342 		 * Join the all hosts multicast address.  We skip this for
13343 		 * underlying IPMP interfaces since they should be invisible.
13344 		 */
13345 		if (!IS_UNDER_IPMP(ill)) {
13346 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13347 			    &err);
13348 			if (ilm == NULL) {
13349 				ASSERT(err != 0);
13350 				ip0dbg(("ipif_multicast_up: "
13351 				    "all_hosts_mcast failed %d\n", err));
13352 				return;
13353 			}
13354 			ipif->ipif_allhosts_ilm = ilm;
13355 		}
13356 
13357 		/*
13358 		 * Enable multicast for the solicited node multicast address.
13359 		 * If IPMP we need to put the membership on the upper ill.
13360 		 */
13361 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13362 			ill_t *mcast_ill = NULL;
13363 			boolean_t need_refrele;
13364 
13365 			if (IS_UNDER_IPMP(ill) &&
13366 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13367 				need_refrele = B_TRUE;
13368 			} else {
13369 				mcast_ill = ill;
13370 				need_refrele = B_FALSE;
13371 			}
13372 
13373 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13374 			    ipif->ipif_zoneid, &err);
13375 			if (need_refrele)
13376 				ill_refrele(mcast_ill);
13377 
13378 			if (ilm == NULL) {
13379 				ASSERT(err != 0);
13380 				ip0dbg(("ipif_multicast_up: solicited MC"
13381 				    " failed %d\n", err));
13382 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13383 					ipif->ipif_allhosts_ilm = NULL;
13384 					(void) ip_delmulti(ilm);
13385 				}
13386 				return;
13387 			}
13388 			ipif->ipif_solmulti_ilm = ilm;
13389 		}
13390 	} else {
13391 		in6_addr_t v6group;
13392 
13393 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13394 			return;
13395 
13396 		/* Join the all hosts multicast address */
13397 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13398 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13399 
13400 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13401 		if (ilm == NULL) {
13402 			ASSERT(err != 0);
13403 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13404 			return;
13405 		}
13406 		ipif->ipif_allhosts_ilm = ilm;
13407 	}
13408 }
13409 
13410 /*
13411  * Blow away any multicast groups that we joined in ipif_multicast_up().
13412  * (ilms from explicit memberships are handled in conn_update_ill.)
13413  */
13414 void
13415 ipif_multicast_down(ipif_t *ipif)
13416 {
13417 	ASSERT(IAM_WRITER_IPIF(ipif));
13418 
13419 	ip1dbg(("ipif_multicast_down\n"));
13420 
13421 	if (ipif->ipif_allhosts_ilm != NULL) {
13422 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13423 		ipif->ipif_allhosts_ilm = NULL;
13424 	}
13425 	if (ipif->ipif_solmulti_ilm != NULL) {
13426 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13427 		ipif->ipif_solmulti_ilm = NULL;
13428 	}
13429 }
13430 
13431 /*
13432  * Used when an interface comes up to recreate any extra routes on this
13433  * interface.
13434  */
13435 int
13436 ill_recover_saved_ire(ill_t *ill)
13437 {
13438 	mblk_t		*mp;
13439 	ip_stack_t	*ipst = ill->ill_ipst;
13440 
13441 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13442 
13443 	mutex_enter(&ill->ill_saved_ire_lock);
13444 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13445 		ire_t		*ire, *nire;
13446 		ifrt_t		*ifrt;
13447 
13448 		ifrt = (ifrt_t *)mp->b_rptr;
13449 		/*
13450 		 * Create a copy of the IRE with the saved address and netmask.
13451 		 */
13452 		if (ill->ill_isv6) {
13453 			ire = ire_create_v6(
13454 			    &ifrt->ifrt_v6addr,
13455 			    &ifrt->ifrt_v6mask,
13456 			    &ifrt->ifrt_v6gateway_addr,
13457 			    ifrt->ifrt_type,
13458 			    ill,
13459 			    ifrt->ifrt_zoneid,
13460 			    ifrt->ifrt_flags,
13461 			    NULL,
13462 			    ipst);
13463 		} else {
13464 			ire = ire_create(
13465 			    (uint8_t *)&ifrt->ifrt_addr,
13466 			    (uint8_t *)&ifrt->ifrt_mask,
13467 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13468 			    ifrt->ifrt_type,
13469 			    ill,
13470 			    ifrt->ifrt_zoneid,
13471 			    ifrt->ifrt_flags,
13472 			    NULL,
13473 			    ipst);
13474 		}
13475 		if (ire == NULL) {
13476 			mutex_exit(&ill->ill_saved_ire_lock);
13477 			return (ENOMEM);
13478 		}
13479 
13480 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13481 			if (ill->ill_isv6) {
13482 				ire->ire_setsrc_addr_v6 =
13483 				    ifrt->ifrt_v6setsrc_addr;
13484 			} else {
13485 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13486 			}
13487 		}
13488 
13489 		/*
13490 		 * Some software (for example, GateD and Sun Cluster) attempts
13491 		 * to create (what amount to) IRE_PREFIX routes with the
13492 		 * loopback address as the gateway.  This is primarily done to
13493 		 * set up prefixes with the RTF_REJECT flag set (for example,
13494 		 * when generating aggregate routes.)
13495 		 *
13496 		 * If the IRE type (as defined by ill->ill_net_type) is
13497 		 * IRE_LOOPBACK, then we map the request into a
13498 		 * IRE_IF_NORESOLVER.
13499 		 */
13500 		if (ill->ill_net_type == IRE_LOOPBACK)
13501 			ire->ire_type = IRE_IF_NORESOLVER;
13502 
13503 		/*
13504 		 * ire held by ire_add, will be refreled' towards the
13505 		 * the end of ipif_up_done
13506 		 */
13507 		nire = ire_add(ire);
13508 		/*
13509 		 * Check if it was a duplicate entry. This handles
13510 		 * the case of two racing route adds for the same route
13511 		 */
13512 		if (nire == NULL) {
13513 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13514 		} else if (nire != ire) {
13515 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13516 			    (void *)nire));
13517 			ire_delete(nire);
13518 		} else {
13519 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13520 			    (void *)nire));
13521 		}
13522 		if (nire != NULL)
13523 			ire_refrele(nire);
13524 	}
13525 	mutex_exit(&ill->ill_saved_ire_lock);
13526 	return (0);
13527 }
13528 
13529 /*
13530  * Used to set the netmask and broadcast address to default values when the
13531  * interface is brought up.  (Always called as writer.)
13532  */
13533 static void
13534 ipif_set_default(ipif_t *ipif)
13535 {
13536 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13537 
13538 	if (!ipif->ipif_isv6) {
13539 		/*
13540 		 * Interface holds an IPv4 address. Default
13541 		 * mask is the natural netmask.
13542 		 */
13543 		if (!ipif->ipif_net_mask) {
13544 			ipaddr_t	v4mask;
13545 
13546 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13547 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13548 		}
13549 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13550 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13551 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13552 		} else {
13553 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13554 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13555 		}
13556 		/*
13557 		 * NOTE: SunOS 4.X does this even if the broadcast address
13558 		 * has been already set thus we do the same here.
13559 		 */
13560 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13561 			ipaddr_t	v4addr;
13562 
13563 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13564 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13565 		}
13566 	} else {
13567 		/*
13568 		 * Interface holds an IPv6-only address.  Default
13569 		 * mask is all-ones.
13570 		 */
13571 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13572 			ipif->ipif_v6net_mask = ipv6_all_ones;
13573 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13574 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13575 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13576 		} else {
13577 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13578 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13579 		}
13580 	}
13581 }
13582 
13583 /*
13584  * Return 0 if this address can be used as local address without causing
13585  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13586  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13587  * Note that the same IPv6 link-local address is allowed as long as the ills
13588  * are not on the same link.
13589  */
13590 int
13591 ip_addr_availability_check(ipif_t *new_ipif)
13592 {
13593 	in6_addr_t our_v6addr;
13594 	ill_t *ill;
13595 	ipif_t *ipif;
13596 	ill_walk_context_t ctx;
13597 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13598 
13599 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13600 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13601 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13602 
13603 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13604 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13605 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13606 		return (0);
13607 
13608 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13609 
13610 	if (new_ipif->ipif_isv6)
13611 		ill = ILL_START_WALK_V6(&ctx, ipst);
13612 	else
13613 		ill = ILL_START_WALK_V4(&ctx, ipst);
13614 
13615 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13616 		for (ipif = ill->ill_ipif; ipif != NULL;
13617 		    ipif = ipif->ipif_next) {
13618 			if ((ipif == new_ipif) ||
13619 			    !(ipif->ipif_flags & IPIF_UP) ||
13620 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13621 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13622 			    &our_v6addr))
13623 				continue;
13624 
13625 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13626 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13627 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13628 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13629 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13630 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13631 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13632 				continue;
13633 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13634 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13635 				continue;
13636 			else if (new_ipif->ipif_ill == ill)
13637 				return (EADDRINUSE);
13638 			else
13639 				return (EADDRNOTAVAIL);
13640 		}
13641 	}
13642 
13643 	return (0);
13644 }
13645 
13646 /*
13647  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13648  * IREs for the ipif.
13649  * When the routine returns EINPROGRESS then mp has been consumed and
13650  * the ioctl will be acked from ip_rput_dlpi.
13651  */
13652 int
13653 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13654 {
13655 	ill_t		*ill = ipif->ipif_ill;
13656 	boolean_t 	isv6 = ipif->ipif_isv6;
13657 	int		err = 0;
13658 	boolean_t	success;
13659 	uint_t		ipif_orig_id;
13660 	ip_stack_t	*ipst = ill->ill_ipst;
13661 
13662 	ASSERT(IAM_WRITER_IPIF(ipif));
13663 
13664 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13665 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13666 	    ill_t *, ill, ipif_t *, ipif);
13667 
13668 	/* Shouldn't get here if it is already up. */
13669 	if (ipif->ipif_flags & IPIF_UP)
13670 		return (EALREADY);
13671 
13672 	/*
13673 	 * If this is a request to bring up a data address on an interface
13674 	 * under IPMP, then move the address to its IPMP meta-interface and
13675 	 * try to bring it up.  One complication is that the zeroth ipif for
13676 	 * an ill is special, in that every ill always has one, and that code
13677 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13678 	 */
13679 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13680 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13681 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13682 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13683 
13684 		/*
13685 		 * The ipif being brought up should be quiesced.  If it's not,
13686 		 * something has gone amiss and we need to bail out.  (If it's
13687 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13688 		 */
13689 		mutex_enter(&ill->ill_lock);
13690 		if (!ipif_is_quiescent(ipif)) {
13691 			mutex_exit(&ill->ill_lock);
13692 			return (EINVAL);
13693 		}
13694 		mutex_exit(&ill->ill_lock);
13695 
13696 		/*
13697 		 * If we're going to need to allocate ipifs, do it prior
13698 		 * to starting the move (and grabbing locks).
13699 		 */
13700 		if (ipif->ipif_id == 0) {
13701 			moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13702 			    B_FALSE);
13703 			stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13704 			    B_FALSE);
13705 			if (moveipif == NULL || stubipif == NULL) {
13706 				mi_free(moveipif);
13707 				mi_free(stubipif);
13708 				return (ENOMEM);
13709 			}
13710 		}
13711 
13712 		/*
13713 		 * Grab or transfer the ipif to move.  During the move, keep
13714 		 * ill_g_lock held to prevent any ill walker threads from
13715 		 * seeing things in an inconsistent state.
13716 		 */
13717 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13718 		if (ipif->ipif_id != 0) {
13719 			ipif_remove(ipif);
13720 		} else {
13721 			ipif_transfer(ipif, moveipif, stubipif);
13722 			ipif = moveipif;
13723 		}
13724 
13725 		/*
13726 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13727 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13728 		 * replace that one.  Otherwise, pick the next available slot.
13729 		 */
13730 		ipif->ipif_ill = ipmp_ill;
13731 		ipif_orig_id = ipif->ipif_id;
13732 
13733 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13734 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13735 			ipif = ipmp_ill->ill_ipif;
13736 		} else {
13737 			ipif->ipif_id = -1;
13738 			if (ipif_insert(ipif, B_FALSE) != 0) {
13739 				/*
13740 				 * No more available ipif_id's -- put it back
13741 				 * on the original ill and fail the operation.
13742 				 * Since we're writer on the ill, we can be
13743 				 * sure our old slot is still available.
13744 				 */
13745 				ipif->ipif_id = ipif_orig_id;
13746 				ipif->ipif_ill = ill;
13747 				if (ipif_orig_id == 0) {
13748 					ipif_transfer(ipif, ill->ill_ipif,
13749 					    NULL);
13750 				} else {
13751 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13752 				}
13753 				rw_exit(&ipst->ips_ill_g_lock);
13754 				return (ENOMEM);
13755 			}
13756 		}
13757 		rw_exit(&ipst->ips_ill_g_lock);
13758 
13759 		/*
13760 		 * Tell SCTP that the ipif has moved.  Note that even if we
13761 		 * had to allocate a new ipif, the original sequence id was
13762 		 * preserved and therefore SCTP won't know.
13763 		 */
13764 		sctp_move_ipif(ipif, ill, ipmp_ill);
13765 
13766 		/*
13767 		 * If the ipif being brought up was on slot zero, then we
13768 		 * first need to bring up the placeholder we stuck there.  In
13769 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13770 		 * call to ipif_up() itself, if we successfully bring up the
13771 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13772 		 */
13773 		if (ipif_orig_id == 0) {
13774 			ASSERT(ill->ill_move_ipif == NULL);
13775 			ill->ill_move_ipif = ipif;
13776 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
13777 				ASSERT(ill->ill_move_ipif == NULL);
13778 			if (err != EINPROGRESS)
13779 				ill->ill_move_ipif = NULL;
13780 			return (err);
13781 		}
13782 
13783 		/*
13784 		 * Bring it up on the IPMP ill.
13785 		 */
13786 		return (ipif_up(ipif, q, mp));
13787 	}
13788 
13789 	/* Skip arp/ndp for any loopback interface. */
13790 	if (ill->ill_wq != NULL) {
13791 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13792 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
13793 
13794 		if (!ill->ill_dl_up) {
13795 			/*
13796 			 * ill_dl_up is not yet set. i.e. we are yet to
13797 			 * DL_BIND with the driver and this is the first
13798 			 * logical interface on the ill to become "up".
13799 			 * Tell the driver to get going (via DL_BIND_REQ).
13800 			 * Note that changing "significant" IFF_ flags
13801 			 * address/netmask etc cause a down/up dance, but
13802 			 * does not cause an unbind (DL_UNBIND) with the driver
13803 			 */
13804 			return (ill_dl_up(ill, ipif, mp, q));
13805 		}
13806 
13807 		/*
13808 		 * ipif_resolver_up may end up needeing to bind/attach
13809 		 * the ARP stream, which in turn necessitates a
13810 		 * DLPI message exchange with the driver. ioctls are
13811 		 * serialized and so we cannot send more than one
13812 		 * interface up message at a time. If ipif_resolver_up
13813 		 * does need to wait for the DLPI handshake for the ARP stream,
13814 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
13815 		 */
13816 
13817 		ASSERT(connp != NULL || !CONN_Q(q));
13818 		if (connp != NULL)
13819 			mutex_enter(&connp->conn_lock);
13820 		mutex_enter(&ill->ill_lock);
13821 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13822 		mutex_exit(&ill->ill_lock);
13823 		if (connp != NULL)
13824 			mutex_exit(&connp->conn_lock);
13825 		if (!success)
13826 			return (EINTR);
13827 
13828 		/*
13829 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
13830 		 * complete when ipif_ndp_up returns.
13831 		 */
13832 		err = ipif_resolver_up(ipif, Res_act_initial);
13833 		if (err == EINPROGRESS) {
13834 			/* We will complete it in arp_bringup_done() */
13835 			return (err);
13836 		}
13837 
13838 		if (isv6 && err == 0)
13839 			err = ipif_ndp_up(ipif, B_TRUE);
13840 
13841 		ASSERT(err != EINPROGRESS);
13842 		mp = ipsq_pending_mp_get(ipsq, &connp);
13843 		ASSERT(mp != NULL);
13844 		if (err != 0)
13845 			return (err);
13846 	} else {
13847 		/*
13848 		 * Interfaces without underlying hardware don't do duplicate
13849 		 * address detection.
13850 		 */
13851 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
13852 		ipif->ipif_addr_ready = 1;
13853 		err = ill_add_ires(ill);
13854 		/* allocation failure? */
13855 		if (err != 0)
13856 			return (err);
13857 	}
13858 
13859 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
13860 	if (err == 0 && ill->ill_move_ipif != NULL) {
13861 		ipif = ill->ill_move_ipif;
13862 		ill->ill_move_ipif = NULL;
13863 		return (ipif_up(ipif, q, mp));
13864 	}
13865 	return (err);
13866 }
13867 
13868 /*
13869  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
13870  * The identical set of IREs need to be removed in ill_delete_ires().
13871  */
13872 int
13873 ill_add_ires(ill_t *ill)
13874 {
13875 	ire_t	*ire;
13876 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
13877 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
13878 
13879 	if (ill->ill_ire_multicast != NULL)
13880 		return (0);
13881 
13882 	/*
13883 	 * provide some dummy ire_addr for creating the ire.
13884 	 */
13885 	if (ill->ill_isv6) {
13886 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
13887 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
13888 	} else {
13889 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
13890 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
13891 	}
13892 	if (ire == NULL)
13893 		return (ENOMEM);
13894 
13895 	ill->ill_ire_multicast = ire;
13896 	return (0);
13897 }
13898 
13899 void
13900 ill_delete_ires(ill_t *ill)
13901 {
13902 	if (ill->ill_ire_multicast != NULL) {
13903 		/*
13904 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
13905 		 * which was taken without any th_tracing enabled.
13906 		 * We also mark it as condemned (note that it was never added)
13907 		 * so that caching conn's can move off of it.
13908 		 */
13909 		ire_make_condemned(ill->ill_ire_multicast);
13910 		ire_refrele_notr(ill->ill_ire_multicast);
13911 		ill->ill_ire_multicast = NULL;
13912 	}
13913 }
13914 
13915 /*
13916  * Perform a bind for the physical device.
13917  * When the routine returns EINPROGRESS then mp has been consumed and
13918  * the ioctl will be acked from ip_rput_dlpi.
13919  * Allocate an unbind message and save it until ipif_down.
13920  */
13921 static int
13922 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
13923 {
13924 	mblk_t	*bind_mp = NULL;
13925 	mblk_t	*unbind_mp = NULL;
13926 	conn_t	*connp;
13927 	boolean_t success;
13928 	int	err;
13929 
13930 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
13931 
13932 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
13933 	ASSERT(IAM_WRITER_ILL(ill));
13934 	ASSERT(mp != NULL);
13935 
13936 	/*
13937 	 * Make sure we have an IRE_MULTICAST in case we immediately
13938 	 * start receiving packets.
13939 	 */
13940 	err = ill_add_ires(ill);
13941 	if (err != 0)
13942 		goto bad;
13943 
13944 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
13945 	    DL_BIND_REQ);
13946 	if (bind_mp == NULL)
13947 		goto bad;
13948 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
13949 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
13950 
13951 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
13952 	if (unbind_mp == NULL)
13953 		goto bad;
13954 
13955 	/*
13956 	 * Record state needed to complete this operation when the
13957 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
13958 	 */
13959 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13960 	ASSERT(connp != NULL || !CONN_Q(q));
13961 	GRAB_CONN_LOCK(q);
13962 	mutex_enter(&ipif->ipif_ill->ill_lock);
13963 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13964 	mutex_exit(&ipif->ipif_ill->ill_lock);
13965 	RELEASE_CONN_LOCK(q);
13966 	if (!success)
13967 		goto bad;
13968 
13969 	/*
13970 	 * Save the unbind message for ill_dl_down(); it will be consumed when
13971 	 * the interface goes down.
13972 	 */
13973 	ASSERT(ill->ill_unbind_mp == NULL);
13974 	ill->ill_unbind_mp = unbind_mp;
13975 
13976 	ill_dlpi_send(ill, bind_mp);
13977 	/* Send down link-layer capabilities probe if not already done. */
13978 	ill_capability_probe(ill);
13979 
13980 	/*
13981 	 * Sysid used to rely on the fact that netboots set domainname
13982 	 * and the like. Now that miniroot boots aren't strictly netboots
13983 	 * and miniroot network configuration is driven from userland
13984 	 * these things still need to be set. This situation can be detected
13985 	 * by comparing the interface being configured here to the one
13986 	 * dhcifname was set to reference by the boot loader. Once sysid is
13987 	 * converted to use dhcp_ipc_getinfo() this call can go away.
13988 	 */
13989 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
13990 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
13991 	    (strlen(srpc_domain) == 0)) {
13992 		if (dhcpinit() != 0)
13993 			cmn_err(CE_WARN, "no cached dhcp response");
13994 	}
13995 
13996 	/*
13997 	 * This operation will complete in ip_rput_dlpi with either
13998 	 * a DL_BIND_ACK or DL_ERROR_ACK.
13999 	 */
14000 	return (EINPROGRESS);
14001 bad:
14002 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14003 
14004 	freemsg(bind_mp);
14005 	freemsg(unbind_mp);
14006 	return (ENOMEM);
14007 }
14008 
14009 /* Add room for tcp+ip headers */
14010 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14011 
14012 /*
14013  * DLPI and ARP is up.
14014  * Create all the IREs associated with an interface. Bring up multicast.
14015  * Set the interface flag and finish other initialization
14016  * that potentially had to be deferred to after DL_BIND_ACK.
14017  */
14018 int
14019 ipif_up_done(ipif_t *ipif)
14020 {
14021 	ill_t		*ill = ipif->ipif_ill;
14022 	int		err = 0;
14023 	boolean_t	loopback = B_FALSE;
14024 	boolean_t	update_src_selection = B_TRUE;
14025 	ipif_t		*tmp_ipif;
14026 
14027 	ip1dbg(("ipif_up_done(%s:%u)\n",
14028 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14029 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14030 	    ill_t *, ill, ipif_t *, ipif);
14031 
14032 	/* Check if this is a loopback interface */
14033 	if (ipif->ipif_ill->ill_wq == NULL)
14034 		loopback = B_TRUE;
14035 
14036 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14037 
14038 	/*
14039 	 * If all other interfaces for this ill are down or DEPRECATED,
14040 	 * or otherwise unsuitable for source address selection,
14041 	 * reset the src generation numbers to make sure source
14042 	 * address selection gets to take this new ipif into account.
14043 	 * No need to hold ill_lock while traversing the ipif list since
14044 	 * we are writer
14045 	 */
14046 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14047 	    tmp_ipif = tmp_ipif->ipif_next) {
14048 		if (((tmp_ipif->ipif_flags &
14049 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14050 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14051 		    (tmp_ipif == ipif))
14052 			continue;
14053 		/* first useable pre-existing interface */
14054 		update_src_selection = B_FALSE;
14055 		break;
14056 	}
14057 	if (update_src_selection)
14058 		ip_update_source_selection(ill->ill_ipst);
14059 
14060 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14061 		nce_t *loop_nce = NULL;
14062 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14063 
14064 		/*
14065 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14066 		 * ipif_lookup_on_name(), but in the case of zones we can have
14067 		 * several loopback addresses on lo0. So all the interfaces with
14068 		 * loopback addresses need to be marked IRE_LOOPBACK.
14069 		 */
14070 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14071 		    htonl(INADDR_LOOPBACK))
14072 			ipif->ipif_ire_type = IRE_LOOPBACK;
14073 		else
14074 			ipif->ipif_ire_type = IRE_LOCAL;
14075 		if (ill->ill_net_type != IRE_LOOPBACK)
14076 			flags |= NCE_F_PUBLISH;
14077 
14078 		/* add unicast nce for the local addr */
14079 		err = nce_lookup_then_add_v4(ill, NULL,
14080 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14081 		    ND_REACHABLE, &loop_nce);
14082 		/* A shared-IP zone sees EEXIST for lo0:N */
14083 		if (err == 0 || err == EEXIST) {
14084 			ipif->ipif_added_nce = 1;
14085 			loop_nce->nce_ipif_cnt++;
14086 			nce_refrele(loop_nce);
14087 			err = 0;
14088 		} else {
14089 			ASSERT(loop_nce == NULL);
14090 			return (err);
14091 		}
14092 	}
14093 
14094 	/* Create all the IREs associated with this interface */
14095 	err = ipif_add_ires_v4(ipif, loopback);
14096 	if (err != 0) {
14097 		/*
14098 		 * see comments about return value from
14099 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14100 		 */
14101 		if (err != EADDRINUSE) {
14102 			(void) ipif_arp_down(ipif);
14103 		} else {
14104 			/*
14105 			 * Make IPMP aware of the deleted ipif so that
14106 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14107 			 * can be completed. Note that we do not want to
14108 			 * destroy the nce that was created on the ipmp_ill
14109 			 * for the active copy of the duplicate address in
14110 			 * use.
14111 			 */
14112 			if (IS_IPMP(ill))
14113 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14114 			err = EADDRNOTAVAIL;
14115 		}
14116 		return (err);
14117 	}
14118 
14119 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14120 		/* Recover any additional IREs entries for this ill */
14121 		(void) ill_recover_saved_ire(ill);
14122 	}
14123 
14124 	if (ill->ill_need_recover_multicast) {
14125 		/*
14126 		 * Need to recover all multicast memberships in the driver.
14127 		 * This had to be deferred until we had attached.  The same
14128 		 * code exists in ipif_up_done_v6() to recover IPv6
14129 		 * memberships.
14130 		 *
14131 		 * Note that it would be preferable to unconditionally do the
14132 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14133 		 * that since ill_join_allmulti() depends on ill_dl_up being
14134 		 * set, and it is not set until we receive a DL_BIND_ACK after
14135 		 * having called ill_dl_up().
14136 		 */
14137 		ill_recover_multicast(ill);
14138 	}
14139 
14140 	if (ill->ill_ipif_up_count == 1) {
14141 		/*
14142 		 * Since the interface is now up, it may now be active.
14143 		 */
14144 		if (IS_UNDER_IPMP(ill))
14145 			ipmp_ill_refresh_active(ill);
14146 
14147 		/*
14148 		 * If this is an IPMP interface, we may now be able to
14149 		 * establish ARP entries.
14150 		 */
14151 		if (IS_IPMP(ill))
14152 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14153 	}
14154 
14155 	/* Join the allhosts multicast address */
14156 	ipif_multicast_up(ipif);
14157 
14158 	if (!loopback && !update_src_selection &&
14159 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14160 		ip_update_source_selection(ill->ill_ipst);
14161 
14162 	if (!loopback && ipif->ipif_addr_ready) {
14163 		/* Broadcast an address mask reply. */
14164 		ipif_mask_reply(ipif);
14165 	}
14166 	/* Perhaps ilgs should use this ill */
14167 	update_conn_ill(NULL, ill->ill_ipst);
14168 
14169 	/*
14170 	 * This had to be deferred until we had bound.  Tell routing sockets and
14171 	 * others that this interface is up if it looks like the address has
14172 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14173 	 * duplicate address detection to do its thing.
14174 	 */
14175 	if (ipif->ipif_addr_ready)
14176 		ipif_up_notify(ipif);
14177 	return (0);
14178 }
14179 
14180 /*
14181  * Add the IREs associated with the ipif.
14182  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14183  */
14184 static int
14185 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14186 {
14187 	ill_t		*ill = ipif->ipif_ill;
14188 	ip_stack_t	*ipst = ill->ill_ipst;
14189 	ire_t		*ire_array[20];
14190 	ire_t		**irep = ire_array;
14191 	ire_t		**irep1;
14192 	ipaddr_t	net_mask = 0;
14193 	ipaddr_t	subnet_mask, route_mask;
14194 	int		err;
14195 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14196 
14197 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14198 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14199 		/*
14200 		 * If we're on a labeled system then make sure that zone-
14201 		 * private addresses have proper remote host database entries.
14202 		 */
14203 		if (is_system_labeled() &&
14204 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14205 		    !tsol_check_interface_address(ipif))
14206 			return (EINVAL);
14207 
14208 		/* Register the source address for __sin6_src_id */
14209 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14210 		    ipif->ipif_zoneid, ipst);
14211 		if (err != 0) {
14212 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14213 			return (err);
14214 		}
14215 
14216 		/* If the interface address is set, create the local IRE. */
14217 		ire_local = ire_create(
14218 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14219 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14220 		    NULL,				/* no gateway */
14221 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14222 		    ipif->ipif_ill,
14223 		    ipif->ipif_zoneid,
14224 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14225 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14226 		    NULL,
14227 		    ipst);
14228 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14229 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14230 		    ipif->ipif_ire_type,
14231 		    ntohl(ipif->ipif_lcl_addr)));
14232 		if (ire_local == NULL) {
14233 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14234 			err = ENOMEM;
14235 			goto bad;
14236 		}
14237 	} else {
14238 		ip1dbg((
14239 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14240 		    ipif->ipif_ire_type,
14241 		    ntohl(ipif->ipif_lcl_addr),
14242 		    (uint_t)ipif->ipif_flags));
14243 	}
14244 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14245 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14246 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14247 	} else {
14248 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14249 	}
14250 
14251 	subnet_mask = ipif->ipif_net_mask;
14252 
14253 	/*
14254 	 * If mask was not specified, use natural netmask of
14255 	 * interface address. Also, store this mask back into the
14256 	 * ipif struct.
14257 	 */
14258 	if (subnet_mask == 0) {
14259 		subnet_mask = net_mask;
14260 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14261 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14262 		    ipif->ipif_v6subnet);
14263 	}
14264 
14265 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14266 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14267 	    ipif->ipif_subnet != INADDR_ANY) {
14268 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14269 
14270 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14271 			route_mask = IP_HOST_MASK;
14272 		} else {
14273 			route_mask = subnet_mask;
14274 		}
14275 
14276 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14277 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14278 		    (void *)ipif, (void *)ill,
14279 		    ill->ill_net_type,
14280 		    ntohl(ipif->ipif_subnet)));
14281 		*irep++ = ire_create(
14282 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
14283 		    (uchar_t *)&route_mask,		/* mask */
14284 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* gateway */
14285 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
14286 		    ill,
14287 		    ipif->ipif_zoneid,
14288 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14289 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14290 		    NULL,
14291 		    ipst);
14292 	}
14293 
14294 	/*
14295 	 * Create any necessary broadcast IREs.
14296 	 */
14297 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14298 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14299 		irep = ipif_create_bcast_ires(ipif, irep);
14300 
14301 	/* If an earlier ire_create failed, get out now */
14302 	for (irep1 = irep; irep1 > ire_array; ) {
14303 		irep1--;
14304 		if (*irep1 == NULL) {
14305 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14306 			err = ENOMEM;
14307 			goto bad;
14308 		}
14309 	}
14310 
14311 	/*
14312 	 * Need to atomically check for IP address availability under
14313 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14314 	 * ills or new ipifs can be added while we are checking availability.
14315 	 */
14316 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14317 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14318 	/* Mark it up, and increment counters. */
14319 	ipif->ipif_flags |= IPIF_UP;
14320 	ill->ill_ipif_up_count++;
14321 	err = ip_addr_availability_check(ipif);
14322 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14323 	rw_exit(&ipst->ips_ill_g_lock);
14324 
14325 	if (err != 0) {
14326 		/*
14327 		 * Our address may already be up on the same ill. In this case,
14328 		 * the ARP entry for our ipif replaced the one for the other
14329 		 * ipif. So we don't want to delete it (otherwise the other ipif
14330 		 * would be unable to send packets).
14331 		 * ip_addr_availability_check() identifies this case for us and
14332 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14333 		 * which is the expected error code.
14334 		 */
14335 		ill->ill_ipif_up_count--;
14336 		ipif->ipif_flags &= ~IPIF_UP;
14337 		goto bad;
14338 	}
14339 
14340 	/*
14341 	 * Add in all newly created IREs.  ire_create_bcast() has
14342 	 * already checked for duplicates of the IRE_BROADCAST type.
14343 	 */
14344 	if (ire_local != NULL) {
14345 		ire_local = ire_add(ire_local);
14346 #ifdef DEBUG
14347 		if (ire_local != NULL) {
14348 			ire_refhold_notr(ire_local);
14349 			ire_refrele(ire_local);
14350 		}
14351 #endif
14352 	}
14353 
14354 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14355 	if (ire_local != NULL)
14356 		ipif->ipif_ire_local = ire_local;
14357 	rw_exit(&ipst->ips_ill_g_lock);
14358 	ire_local = NULL;
14359 
14360 	for (irep1 = irep; irep1 > ire_array; ) {
14361 		irep1--;
14362 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14363 		/* refheld by ire_add. */
14364 		*irep1 = ire_add(*irep1);
14365 		if (*irep1 != NULL) {
14366 			ire_refrele(*irep1);
14367 			*irep1 = NULL;
14368 		}
14369 	}
14370 
14371 	if (!loopback) {
14372 		/*
14373 		 * If the broadcast address has been set, make sure it makes
14374 		 * sense based on the interface address.
14375 		 * Only match on ill since we are sharing broadcast addresses.
14376 		 */
14377 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14378 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14379 			ire_t	*ire;
14380 
14381 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14382 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14383 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14384 
14385 			if (ire == NULL) {
14386 				/*
14387 				 * If there isn't a matching broadcast IRE,
14388 				 * revert to the default for this netmask.
14389 				 */
14390 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14391 				mutex_enter(&ipif->ipif_ill->ill_lock);
14392 				ipif_set_default(ipif);
14393 				mutex_exit(&ipif->ipif_ill->ill_lock);
14394 			} else {
14395 				ire_refrele(ire);
14396 			}
14397 		}
14398 
14399 	}
14400 	return (0);
14401 
14402 bad:
14403 	ip1dbg(("ipif_add_ires: FAILED \n"));
14404 	if (ire_local != NULL)
14405 		ire_delete(ire_local);
14406 	while (irep > ire_array) {
14407 		irep--;
14408 		if (*irep != NULL) {
14409 			ire_delete(*irep);
14410 		}
14411 	}
14412 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14413 
14414 	return (err);
14415 }
14416 
14417 /* Remove all the IREs created by ipif_add_ires_v4 */
14418 void
14419 ipif_delete_ires_v4(ipif_t *ipif)
14420 {
14421 	ill_t		*ill = ipif->ipif_ill;
14422 	ip_stack_t	*ipst = ill->ill_ipst;
14423 	ipaddr_t	net_mask = 0;
14424 	ipaddr_t	subnet_mask, route_mask;
14425 	int		match_args;
14426 	ire_t		*ire;
14427 	boolean_t	loopback;
14428 
14429 	/* Check if this is a loopback interface */
14430 	loopback = (ipif->ipif_ill->ill_wq == NULL);
14431 
14432 	match_args = MATCH_IRE_TYPE | MATCH_IRE_ILL | MATCH_IRE_MASK |
14433 	    MATCH_IRE_ZONEONLY;
14434 
14435 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14436 	if ((ire = ipif->ipif_ire_local) != NULL) {
14437 		ipif->ipif_ire_local = NULL;
14438 		rw_exit(&ipst->ips_ill_g_lock);
14439 		/*
14440 		 * Move count to ipif so we don't loose the count due to
14441 		 * a down/up dance.
14442 		 */
14443 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14444 
14445 		ire_delete(ire);
14446 		ire_refrele_notr(ire);
14447 	} else {
14448 		rw_exit(&ipst->ips_ill_g_lock);
14449 	}
14450 
14451 	match_args |= MATCH_IRE_GW;
14452 
14453 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14454 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14455 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14456 	} else {
14457 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14458 	}
14459 
14460 	subnet_mask = ipif->ipif_net_mask;
14461 
14462 	/*
14463 	 * If mask was not specified, use natural netmask of
14464 	 * interface address. Also, store this mask back into the
14465 	 * ipif struct.
14466 	 */
14467 	if (subnet_mask == 0)
14468 		subnet_mask = net_mask;
14469 
14470 	/* Delete the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14471 	if (IS_UNDER_IPMP(ill))
14472 		match_args |= MATCH_IRE_TESTHIDDEN;
14473 
14474 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14475 	    ipif->ipif_subnet != INADDR_ANY) {
14476 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14477 
14478 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14479 			route_mask = IP_HOST_MASK;
14480 		} else {
14481 			route_mask = subnet_mask;
14482 		}
14483 
14484 		ire = ire_ftable_lookup_v4(
14485 		    ipif->ipif_subnet,			/* dest address */
14486 		    route_mask,				/* mask */
14487 		    ipif->ipif_lcl_addr,		/* gateway */
14488 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
14489 		    ill,
14490 		    ipif->ipif_zoneid,
14491 		    NULL,
14492 		    match_args,
14493 		    0,
14494 		    ipst,
14495 		    NULL);
14496 		ASSERT(ire != NULL);
14497 		ire_delete(ire);
14498 		ire_refrele(ire);
14499 	}
14500 
14501 	/*
14502 	 * Create any necessary broadcast IREs.
14503 	 */
14504 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14505 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14506 		ipif_delete_bcast_ires(ipif);
14507 }
14508 
14509 /*
14510  * Checks for availbility of a usable source address (if there is one) when the
14511  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14512  * this selection is done regardless of the destination.
14513  */
14514 boolean_t
14515 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14516     ip_stack_t *ipst)
14517 {
14518 	ipif_t		*ipif = NULL;
14519 	ill_t		*uill;
14520 
14521 	ASSERT(ifindex != 0);
14522 
14523 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14524 	if (uill == NULL)
14525 		return (B_FALSE);
14526 
14527 	mutex_enter(&uill->ill_lock);
14528 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14529 		if (IPIF_IS_CONDEMNED(ipif))
14530 			continue;
14531 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14532 			continue;
14533 		if (!(ipif->ipif_flags & IPIF_UP))
14534 			continue;
14535 		if (ipif->ipif_zoneid != zoneid)
14536 			continue;
14537 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14538 		    ipif->ipif_lcl_addr == INADDR_ANY)
14539 			continue;
14540 		mutex_exit(&uill->ill_lock);
14541 		ill_refrele(uill);
14542 		return (B_TRUE);
14543 	}
14544 	mutex_exit(&uill->ill_lock);
14545 	ill_refrele(uill);
14546 	return (B_FALSE);
14547 }
14548 
14549 /*
14550  * Find an ipif with a good local address on the ill+zoneid.
14551  */
14552 ipif_t *
14553 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14554 {
14555 	ipif_t		*ipif;
14556 
14557 	mutex_enter(&ill->ill_lock);
14558 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14559 		if (IPIF_IS_CONDEMNED(ipif))
14560 			continue;
14561 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14562 			continue;
14563 		if (!(ipif->ipif_flags & IPIF_UP))
14564 			continue;
14565 		if (ipif->ipif_zoneid != zoneid &&
14566 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14567 			continue;
14568 		if (ill->ill_isv6 ?
14569 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14570 		    ipif->ipif_lcl_addr == INADDR_ANY)
14571 			continue;
14572 		ipif_refhold_locked(ipif);
14573 		mutex_exit(&ill->ill_lock);
14574 		return (ipif);
14575 	}
14576 	mutex_exit(&ill->ill_lock);
14577 	return (NULL);
14578 }
14579 
14580 /*
14581  * IP source address type, sorted from worst to best.  For a given type,
14582  * always prefer IP addresses on the same subnet.  All-zones addresses are
14583  * suboptimal because they pose problems with unlabeled destinations.
14584  */
14585 typedef enum {
14586 	IPIF_NONE,
14587 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14588 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14589 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14590 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14591 	IPIF_DIFFNET,			/* normal and different subnet */
14592 	IPIF_SAMENET,			/* normal and same subnet */
14593 	IPIF_LOCALADDR			/* local loopback */
14594 } ipif_type_t;
14595 
14596 /*
14597  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14598  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14599  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14600  * the first one, unless IPMP is used in which case we round-robin among them;
14601  * see below for more.
14602  *
14603  * Returns NULL if there is no suitable source address for the ill.
14604  * This only occurs when there is no valid source address for the ill.
14605  */
14606 ipif_t *
14607 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14608     boolean_t allow_usesrc, boolean_t *notreadyp)
14609 {
14610 	ill_t	*usill = NULL;
14611 	ill_t	*ipmp_ill = NULL;
14612 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14613 	ipif_type_t type, best_type;
14614 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14615 	ip_stack_t *ipst = ill->ill_ipst;
14616 	boolean_t samenet;
14617 
14618 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14619 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14620 		    B_FALSE, ipst);
14621 		if (usill != NULL)
14622 			ill = usill;	/* Select source from usesrc ILL */
14623 		else
14624 			return (NULL);
14625 	}
14626 
14627 	/*
14628 	 * Test addresses should never be used for source address selection,
14629 	 * so if we were passed one, switch to the IPMP meta-interface.
14630 	 */
14631 	if (IS_UNDER_IPMP(ill)) {
14632 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14633 			ill = ipmp_ill;	/* Select source from IPMP ill */
14634 		else
14635 			return (NULL);
14636 	}
14637 
14638 	/*
14639 	 * If we're dealing with an unlabeled destination on a labeled system,
14640 	 * make sure that we ignore source addresses that are incompatible with
14641 	 * the destination's default label.  That destination's default label
14642 	 * must dominate the minimum label on the source address.
14643 	 */
14644 	dst_rhtp = NULL;
14645 	if (is_system_labeled()) {
14646 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14647 		if (dst_rhtp == NULL)
14648 			return (NULL);
14649 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14650 			TPC_RELE(dst_rhtp);
14651 			dst_rhtp = NULL;
14652 		}
14653 	}
14654 
14655 	/*
14656 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14657 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14658 	 * After selecting the right ipif, under ill_lock make sure ipif is
14659 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14660 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14661 	 * but not under a lock.
14662 	 */
14663 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14664 retry:
14665 	/*
14666 	 * For source address selection, we treat the ipif list as circular
14667 	 * and continue until we get back to where we started.  This allows
14668 	 * IPMP to vary source address selection (which improves inbound load
14669 	 * spreading) by caching its last ending point and starting from
14670 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14671 	 * ills since that can't happen on the IPMP ill.
14672 	 */
14673 	start_ipif = ill->ill_ipif;
14674 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14675 		start_ipif = ill->ill_src_ipif;
14676 
14677 	ipif = start_ipif;
14678 	best_ipif = NULL;
14679 	best_type = IPIF_NONE;
14680 	do {
14681 		if ((next_ipif = ipif->ipif_next) == NULL)
14682 			next_ipif = ill->ill_ipif;
14683 
14684 		if (IPIF_IS_CONDEMNED(ipif))
14685 			continue;
14686 		/* Always skip NOLOCAL and ANYCAST interfaces */
14687 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14688 			continue;
14689 		if (!(ipif->ipif_flags & IPIF_UP))
14690 			continue;
14691 
14692 		if (!ipif->ipif_addr_ready) {
14693 			if (notreadyp != NULL)
14694 				*notreadyp = B_TRUE;
14695 			continue;
14696 		}
14697 
14698 		if (zoneid != ALL_ZONES &&
14699 		    ipif->ipif_zoneid != zoneid &&
14700 		    ipif->ipif_zoneid != ALL_ZONES)
14701 			continue;
14702 
14703 		/*
14704 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14705 		 * are not valid as source addresses.
14706 		 */
14707 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14708 			continue;
14709 
14710 		/*
14711 		 * Check compatibility of local address for destination's
14712 		 * default label if we're on a labeled system.	Incompatible
14713 		 * addresses can't be used at all.
14714 		 */
14715 		if (dst_rhtp != NULL) {
14716 			boolean_t incompat;
14717 
14718 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14719 			    IPV4_VERSION, B_FALSE);
14720 			if (src_rhtp == NULL)
14721 				continue;
14722 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14723 			    src_rhtp->tpc_tp.tp_doi !=
14724 			    dst_rhtp->tpc_tp.tp_doi ||
14725 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14726 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14727 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14728 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14729 			TPC_RELE(src_rhtp);
14730 			if (incompat)
14731 				continue;
14732 		}
14733 
14734 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14735 
14736 		if (ipif->ipif_lcl_addr == dst) {
14737 			type = IPIF_LOCALADDR;
14738 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14739 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14740 			    IPIF_DIFFNET_DEPRECATED;
14741 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14742 			type = samenet ? IPIF_SAMENET_ALLZONES :
14743 			    IPIF_DIFFNET_ALLZONES;
14744 		} else {
14745 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14746 		}
14747 
14748 		if (type > best_type) {
14749 			best_type = type;
14750 			best_ipif = ipif;
14751 			if (best_type == IPIF_LOCALADDR)
14752 				break; /* can't get better */
14753 		}
14754 	} while ((ipif = next_ipif) != start_ipif);
14755 
14756 	if ((ipif = best_ipif) != NULL) {
14757 		mutex_enter(&ipif->ipif_ill->ill_lock);
14758 		if (IPIF_IS_CONDEMNED(ipif)) {
14759 			mutex_exit(&ipif->ipif_ill->ill_lock);
14760 			goto retry;
14761 		}
14762 		ipif_refhold_locked(ipif);
14763 
14764 		/*
14765 		 * For IPMP, update the source ipif rotor to the next ipif,
14766 		 * provided we can look it up.  (We must not use it if it's
14767 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
14768 		 * ipif_free() checked ill_src_ipif.)
14769 		 */
14770 		if (IS_IPMP(ill) && ipif != NULL) {
14771 			next_ipif = ipif->ipif_next;
14772 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
14773 				ill->ill_src_ipif = next_ipif;
14774 			else
14775 				ill->ill_src_ipif = NULL;
14776 		}
14777 		mutex_exit(&ipif->ipif_ill->ill_lock);
14778 	}
14779 
14780 	rw_exit(&ipst->ips_ill_g_lock);
14781 	if (usill != NULL)
14782 		ill_refrele(usill);
14783 	if (ipmp_ill != NULL)
14784 		ill_refrele(ipmp_ill);
14785 	if (dst_rhtp != NULL)
14786 		TPC_RELE(dst_rhtp);
14787 
14788 #ifdef DEBUG
14789 	if (ipif == NULL) {
14790 		char buf1[INET6_ADDRSTRLEN];
14791 
14792 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
14793 		    ill->ill_name,
14794 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
14795 	} else {
14796 		char buf1[INET6_ADDRSTRLEN];
14797 		char buf2[INET6_ADDRSTRLEN];
14798 
14799 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
14800 		    ipif->ipif_ill->ill_name,
14801 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
14802 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
14803 		    buf2, sizeof (buf2))));
14804 	}
14805 #endif /* DEBUG */
14806 	return (ipif);
14807 }
14808 
14809 /*
14810  * Pick a source address based on the destination ill and an optional setsrc
14811  * address.
14812  * The result is stored in srcp. If generation is set, then put the source
14813  * generation number there before we look for the source address (to avoid
14814  * missing changes in the set of source addresses.
14815  * If flagsp is set, then us it to pass back ipif_flags.
14816  *
14817  * If the caller wants to cache the returned source address and detect when
14818  * that might be stale, the caller should pass in a generation argument,
14819  * which the caller can later compare against ips_src_generation
14820  *
14821  * The precedence order for selecting an IPv4 source address is:
14822  *  - RTF_SETSRC on the offlink ire always wins.
14823  *  - If usrsrc is set, swap the ill to be the usesrc one.
14824  *  - If IPMP is used on the ill, select a random address from the most
14825  *    preferred ones below:
14826  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
14827  * 2. Not deprecated, not ALL_ZONES
14828  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
14829  * 4. Not deprecated, ALL_ZONES
14830  * 5. If onlink destination, same subnet and deprecated
14831  * 6. Deprecated.
14832  *
14833  * We have lower preference for ALL_ZONES IP addresses,
14834  * as they pose problems with unlabeled destinations.
14835  *
14836  * Note that when multiple IP addresses match e.g., #1 we pick
14837  * the first one if IPMP is not in use. With IPMP we randomize.
14838  */
14839 int
14840 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
14841     ipaddr_t multicast_ifaddr,
14842     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
14843     uint32_t *generation, uint64_t *flagsp)
14844 {
14845 	ipif_t *ipif;
14846 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
14847 
14848 	if (flagsp != NULL)
14849 		*flagsp = 0;
14850 
14851 	/*
14852 	 * Need to grab the generation number before we check to
14853 	 * avoid a race with a change to the set of local addresses.
14854 	 * No lock needed since the thread which updates the set of local
14855 	 * addresses use ipif/ill locks and exit those (hence a store memory
14856 	 * barrier) before doing the atomic increase of ips_src_generation.
14857 	 */
14858 	if (generation != NULL) {
14859 		*generation = ipst->ips_src_generation;
14860 	}
14861 
14862 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
14863 		*srcp = multicast_ifaddr;
14864 		return (0);
14865 	}
14866 
14867 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
14868 	if (setsrc != INADDR_ANY) {
14869 		*srcp = setsrc;
14870 		return (0);
14871 	}
14872 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
14873 	if (ipif == NULL) {
14874 		if (notready)
14875 			return (ENETDOWN);
14876 		else
14877 			return (EADDRNOTAVAIL);
14878 	}
14879 	*srcp = ipif->ipif_lcl_addr;
14880 	if (flagsp != NULL)
14881 		*flagsp = ipif->ipif_flags;
14882 	ipif_refrele(ipif);
14883 	return (0);
14884 }
14885 
14886 /* ARGSUSED */
14887 int
14888 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14889 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14890 {
14891 	/*
14892 	 * ill_phyint_reinit merged the v4 and v6 into a single
14893 	 * ipsq.  We might not have been able to complete the
14894 	 * operation in ipif_set_values, if we could not become
14895 	 * exclusive.  If so restart it here.
14896 	 */
14897 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
14898 }
14899 
14900 /*
14901  * Can operate on either a module or a driver queue.
14902  * Returns an error if not a module queue.
14903  */
14904 /* ARGSUSED */
14905 int
14906 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14907     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14908 {
14909 	queue_t		*q1 = q;
14910 	char 		*cp;
14911 	char		interf_name[LIFNAMSIZ];
14912 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
14913 
14914 	if (q->q_next == NULL) {
14915 		ip1dbg((
14916 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
14917 		return (EINVAL);
14918 	}
14919 
14920 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
14921 		return (EALREADY);
14922 
14923 	do {
14924 		q1 = q1->q_next;
14925 	} while (q1->q_next);
14926 	cp = q1->q_qinfo->qi_minfo->mi_idname;
14927 	(void) sprintf(interf_name, "%s%d", cp, ppa);
14928 
14929 	/*
14930 	 * Here we are not going to delay the ioack until after
14931 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
14932 	 * original ioctl message before sending the requests.
14933 	 */
14934 	return (ipif_set_values(q, mp, interf_name, &ppa));
14935 }
14936 
14937 /* ARGSUSED */
14938 int
14939 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
14940     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
14941 {
14942 	return (ENXIO);
14943 }
14944 
14945 /*
14946  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
14947  * `irep'.  Returns a pointer to the next free `irep' entry
14948  * A mirror exists in ipif_delete_bcast_ires().
14949  *
14950  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
14951  * done in ire_add.
14952  */
14953 static ire_t **
14954 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
14955 {
14956 	ipaddr_t addr;
14957 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
14958 	ipaddr_t subnetmask = ipif->ipif_net_mask;
14959 	ill_t *ill = ipif->ipif_ill;
14960 	zoneid_t zoneid = ipif->ipif_zoneid;
14961 
14962 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
14963 
14964 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
14965 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
14966 
14967 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
14968 	    (ipif->ipif_flags & IPIF_NOLOCAL))
14969 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
14970 
14971 	irep = ire_create_bcast(ill, 0, zoneid, irep);
14972 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
14973 
14974 	/*
14975 	 * For backward compatibility, we create net broadcast IREs based on
14976 	 * the old "IP address class system", since some old machines only
14977 	 * respond to these class derived net broadcast.  However, we must not
14978 	 * create these net broadcast IREs if the subnetmask is shorter than
14979 	 * the IP address class based derived netmask.  Otherwise, we may
14980 	 * create a net broadcast address which is the same as an IP address
14981 	 * on the subnet -- and then TCP will refuse to talk to that address.
14982 	 */
14983 	if (netmask < subnetmask) {
14984 		addr = netmask & ipif->ipif_subnet;
14985 		irep = ire_create_bcast(ill, addr, zoneid, irep);
14986 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
14987 	}
14988 
14989 	/*
14990 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
14991 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
14992 	 * created.  Creating these broadcast IREs will only create confusion
14993 	 * as `addr' will be the same as the IP address.
14994 	 */
14995 	if (subnetmask != 0xFFFFFFFF) {
14996 		addr = ipif->ipif_subnet;
14997 		irep = ire_create_bcast(ill, addr, zoneid, irep);
14998 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
14999 	}
15000 
15001 	return (irep);
15002 }
15003 
15004 /*
15005  * Mirror of ipif_create_bcast_ires()
15006  */
15007 static void
15008 ipif_delete_bcast_ires(ipif_t *ipif)
15009 {
15010 	ipaddr_t	addr;
15011 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15012 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15013 	ill_t		*ill = ipif->ipif_ill;
15014 	zoneid_t	zoneid = ipif->ipif_zoneid;
15015 	ire_t		*ire;
15016 
15017 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15018 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15019 
15020 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15021 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15022 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15023 
15024 	ire = ire_lookup_bcast(ill, 0, zoneid);
15025 	ASSERT(ire != NULL);
15026 	ire_delete(ire); ire_refrele(ire);
15027 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15028 	ASSERT(ire != NULL);
15029 	ire_delete(ire); ire_refrele(ire);
15030 
15031 	/*
15032 	 * For backward compatibility, we create net broadcast IREs based on
15033 	 * the old "IP address class system", since some old machines only
15034 	 * respond to these class derived net broadcast.  However, we must not
15035 	 * create these net broadcast IREs if the subnetmask is shorter than
15036 	 * the IP address class based derived netmask.  Otherwise, we may
15037 	 * create a net broadcast address which is the same as an IP address
15038 	 * on the subnet -- and then TCP will refuse to talk to that address.
15039 	 */
15040 	if (netmask < subnetmask) {
15041 		addr = netmask & ipif->ipif_subnet;
15042 		ire = ire_lookup_bcast(ill, addr, zoneid);
15043 		ASSERT(ire != NULL);
15044 		ire_delete(ire); ire_refrele(ire);
15045 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15046 		ASSERT(ire != NULL);
15047 		ire_delete(ire); ire_refrele(ire);
15048 	}
15049 
15050 	/*
15051 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15052 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15053 	 * created.  Creating these broadcast IREs will only create confusion
15054 	 * as `addr' will be the same as the IP address.
15055 	 */
15056 	if (subnetmask != 0xFFFFFFFF) {
15057 		addr = ipif->ipif_subnet;
15058 		ire = ire_lookup_bcast(ill, addr, zoneid);
15059 		ASSERT(ire != NULL);
15060 		ire_delete(ire); ire_refrele(ire);
15061 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15062 		ASSERT(ire != NULL);
15063 		ire_delete(ire); ire_refrele(ire);
15064 	}
15065 }
15066 
15067 /*
15068  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15069  * from lifr_flags and the name from lifr_name.
15070  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15071  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15072  * Returns EINPROGRESS when mp has been consumed by queueing it on
15073  * ipx_pending_mp and the ioctl will complete in ip_rput.
15074  *
15075  * Can operate on either a module or a driver queue.
15076  * Returns an error if not a module queue.
15077  */
15078 /* ARGSUSED */
15079 int
15080 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15081     ip_ioctl_cmd_t *ipip, void *if_req)
15082 {
15083 	ill_t	*ill = q->q_ptr;
15084 	phyint_t *phyi;
15085 	ip_stack_t *ipst;
15086 	struct lifreq *lifr = if_req;
15087 	uint64_t new_flags;
15088 
15089 	ASSERT(ipif != NULL);
15090 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15091 
15092 	if (q->q_next == NULL) {
15093 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15094 		return (EINVAL);
15095 	}
15096 
15097 	/*
15098 	 * If we are not writer on 'q' then this interface exists already
15099 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15100 	 * so return EALREADY.
15101 	 */
15102 	if (ill != ipif->ipif_ill)
15103 		return (EALREADY);
15104 
15105 	if (ill->ill_name[0] != '\0')
15106 		return (EALREADY);
15107 
15108 	/*
15109 	 * If there's another ill already with the requested name, ensure
15110 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15111 	 * fuse together two unrelated ills, which will cause chaos.
15112 	 */
15113 	ipst = ill->ill_ipst;
15114 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15115 	    lifr->lifr_name, NULL);
15116 	if (phyi != NULL) {
15117 		ill_t *ill_mate = phyi->phyint_illv4;
15118 
15119 		if (ill_mate == NULL)
15120 			ill_mate = phyi->phyint_illv6;
15121 		ASSERT(ill_mate != NULL);
15122 
15123 		if (ill_mate->ill_media->ip_m_mac_type !=
15124 		    ill->ill_media->ip_m_mac_type) {
15125 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15126 			    "use the same ill name on differing media\n"));
15127 			return (EINVAL);
15128 		}
15129 	}
15130 
15131 	/*
15132 	 * We start off as IFF_IPV4 in ipif_allocate and become
15133 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15134 	 * The only flags that we read from user space are IFF_IPV4,
15135 	 * IFF_IPV6, and IFF_BROADCAST.
15136 	 *
15137 	 * This ill has not been inserted into the global list.
15138 	 * So we are still single threaded and don't need any lock
15139 	 *
15140 	 * Saniy check the flags.
15141 	 */
15142 
15143 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15144 	    ((lifr->lifr_flags & IFF_IPV6) ||
15145 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15146 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15147 		    "or IPv6 i.e., no broadcast \n"));
15148 		return (EINVAL);
15149 	}
15150 
15151 	new_flags =
15152 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15153 
15154 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15155 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15156 		    "IFF_IPV4 or IFF_IPV6\n"));
15157 		return (EINVAL);
15158 	}
15159 
15160 	/*
15161 	 * We always start off as IPv4, so only need to check for IPv6.
15162 	 */
15163 	if ((new_flags & IFF_IPV6) != 0) {
15164 		ill->ill_flags |= ILLF_IPV6;
15165 		ill->ill_flags &= ~ILLF_IPV4;
15166 	}
15167 
15168 	if ((new_flags & IFF_BROADCAST) != 0)
15169 		ipif->ipif_flags |= IPIF_BROADCAST;
15170 	else
15171 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15172 
15173 	/* We started off as V4. */
15174 	if (ill->ill_flags & ILLF_IPV6) {
15175 		ill->ill_phyint->phyint_illv6 = ill;
15176 		ill->ill_phyint->phyint_illv4 = NULL;
15177 	}
15178 
15179 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15180 }
15181 
15182 /* ARGSUSED */
15183 int
15184 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15185     ip_ioctl_cmd_t *ipip, void *if_req)
15186 {
15187 	/*
15188 	 * ill_phyint_reinit merged the v4 and v6 into a single
15189 	 * ipsq.  We might not have been able to complete the
15190 	 * slifname in ipif_set_values, if we could not become
15191 	 * exclusive.  If so restart it here
15192 	 */
15193 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15194 }
15195 
15196 /*
15197  * Return a pointer to the ipif which matches the index, IP version type and
15198  * zoneid.
15199  */
15200 ipif_t *
15201 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15202     ip_stack_t *ipst)
15203 {
15204 	ill_t	*ill;
15205 	ipif_t	*ipif = NULL;
15206 
15207 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15208 	if (ill != NULL) {
15209 		mutex_enter(&ill->ill_lock);
15210 		for (ipif = ill->ill_ipif; ipif != NULL;
15211 		    ipif = ipif->ipif_next) {
15212 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15213 			    zoneid == ipif->ipif_zoneid ||
15214 			    ipif->ipif_zoneid == ALL_ZONES)) {
15215 				ipif_refhold_locked(ipif);
15216 				break;
15217 			}
15218 		}
15219 		mutex_exit(&ill->ill_lock);
15220 		ill_refrele(ill);
15221 	}
15222 	return (ipif);
15223 }
15224 
15225 /*
15226  * Change an existing physical interface's index. If the new index
15227  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15228  * Finally, we update other systems which may have a dependence on the
15229  * index value.
15230  */
15231 /* ARGSUSED */
15232 int
15233 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15234     ip_ioctl_cmd_t *ipip, void *ifreq)
15235 {
15236 	ill_t		*ill;
15237 	phyint_t	*phyi;
15238 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15239 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15240 	uint_t	old_index, index;
15241 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15242 	avl_index_t	where;
15243 
15244 	if (ipip->ipi_cmd_type == IF_CMD)
15245 		index = ifr->ifr_index;
15246 	else
15247 		index = lifr->lifr_index;
15248 
15249 	/*
15250 	 * Only allow on physical interface. Also, index zero is illegal.
15251 	 */
15252 	ill = ipif->ipif_ill;
15253 	phyi = ill->ill_phyint;
15254 	if (ipif->ipif_id != 0 || index == 0) {
15255 		return (EINVAL);
15256 	}
15257 
15258 	/* If the index is not changing, no work to do */
15259 	if (phyi->phyint_ifindex == index)
15260 		return (0);
15261 
15262 	/*
15263 	 * Use phyint_exists() to determine if the new interface index
15264 	 * is already in use. If the index is unused then we need to
15265 	 * change the phyint's position in the phyint_list_avl_by_index
15266 	 * tree. If we do not do this, subsequent lookups (using the new
15267 	 * index value) will not find the phyint.
15268 	 */
15269 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15270 	if (phyint_exists(index, ipst)) {
15271 		rw_exit(&ipst->ips_ill_g_lock);
15272 		return (EEXIST);
15273 	}
15274 
15275 	/*
15276 	 * The new index is unused. Set it in the phyint. However we must not
15277 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15278 	 * changes. The event must be bound to old ifindex value.
15279 	 */
15280 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15281 	    &index, sizeof (index));
15282 
15283 	old_index = phyi->phyint_ifindex;
15284 	phyi->phyint_ifindex = index;
15285 
15286 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15287 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15288 	    &index, &where);
15289 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15290 	    phyi, where);
15291 	rw_exit(&ipst->ips_ill_g_lock);
15292 
15293 	/* Update SCTP's ILL list */
15294 	sctp_ill_reindex(ill, old_index);
15295 
15296 	/* Send the routing sockets message */
15297 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15298 	if (ILL_OTHER(ill))
15299 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15300 
15301 	/* Perhaps ilgs should use this ill */
15302 	update_conn_ill(NULL, ill->ill_ipst);
15303 	return (0);
15304 }
15305 
15306 /* ARGSUSED */
15307 int
15308 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15309     ip_ioctl_cmd_t *ipip, void *ifreq)
15310 {
15311 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15312 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15313 
15314 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15315 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15316 	/* Get the interface index */
15317 	if (ipip->ipi_cmd_type == IF_CMD) {
15318 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15319 	} else {
15320 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15321 	}
15322 	return (0);
15323 }
15324 
15325 /* ARGSUSED */
15326 int
15327 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15328     ip_ioctl_cmd_t *ipip, void *ifreq)
15329 {
15330 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15331 
15332 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15333 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15334 	/* Get the interface zone */
15335 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15336 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15337 	return (0);
15338 }
15339 
15340 /*
15341  * Set the zoneid of an interface.
15342  */
15343 /* ARGSUSED */
15344 int
15345 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15346     ip_ioctl_cmd_t *ipip, void *ifreq)
15347 {
15348 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15349 	int err = 0;
15350 	boolean_t need_up = B_FALSE;
15351 	zone_t *zptr;
15352 	zone_status_t status;
15353 	zoneid_t zoneid;
15354 
15355 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15356 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15357 		if (!is_system_labeled())
15358 			return (ENOTSUP);
15359 		zoneid = GLOBAL_ZONEID;
15360 	}
15361 
15362 	/* cannot assign instance zero to a non-global zone */
15363 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15364 		return (ENOTSUP);
15365 
15366 	/*
15367 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15368 	 * the event of a race with the zone shutdown processing, since IP
15369 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15370 	 * interface will be cleaned up even if the zone is shut down
15371 	 * immediately after the status check. If the interface can't be brought
15372 	 * down right away, and the zone is shut down before the restart
15373 	 * function is called, we resolve the possible races by rechecking the
15374 	 * zone status in the restart function.
15375 	 */
15376 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15377 		return (EINVAL);
15378 	status = zone_status_get(zptr);
15379 	zone_rele(zptr);
15380 
15381 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15382 		return (EINVAL);
15383 
15384 	if (ipif->ipif_flags & IPIF_UP) {
15385 		/*
15386 		 * If the interface is already marked up,
15387 		 * we call ipif_down which will take care
15388 		 * of ditching any IREs that have been set
15389 		 * up based on the old interface address.
15390 		 */
15391 		err = ipif_logical_down(ipif, q, mp);
15392 		if (err == EINPROGRESS)
15393 			return (err);
15394 		(void) ipif_down_tail(ipif);
15395 		need_up = B_TRUE;
15396 	}
15397 
15398 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15399 	return (err);
15400 }
15401 
15402 static int
15403 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15404     queue_t *q, mblk_t *mp, boolean_t need_up)
15405 {
15406 	int	err = 0;
15407 	ip_stack_t	*ipst;
15408 
15409 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15410 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15411 
15412 	if (CONN_Q(q))
15413 		ipst = CONNQ_TO_IPST(q);
15414 	else
15415 		ipst = ILLQ_TO_IPST(q);
15416 
15417 	/*
15418 	 * For exclusive stacks we don't allow a different zoneid than
15419 	 * global.
15420 	 */
15421 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15422 	    zoneid != GLOBAL_ZONEID)
15423 		return (EINVAL);
15424 
15425 	/* Set the new zone id. */
15426 	ipif->ipif_zoneid = zoneid;
15427 
15428 	/* Update sctp list */
15429 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15430 
15431 	/* The default multicast interface might have changed */
15432 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15433 
15434 	if (need_up) {
15435 		/*
15436 		 * Now bring the interface back up.  If this
15437 		 * is the only IPIF for the ILL, ipif_up
15438 		 * will have to re-bind to the device, so
15439 		 * we may get back EINPROGRESS, in which
15440 		 * case, this IOCTL will get completed in
15441 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15442 		 */
15443 		err = ipif_up(ipif, q, mp);
15444 	}
15445 	return (err);
15446 }
15447 
15448 /* ARGSUSED */
15449 int
15450 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15451     ip_ioctl_cmd_t *ipip, void *if_req)
15452 {
15453 	struct lifreq *lifr = (struct lifreq *)if_req;
15454 	zoneid_t zoneid;
15455 	zone_t *zptr;
15456 	zone_status_t status;
15457 
15458 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15459 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15460 		zoneid = GLOBAL_ZONEID;
15461 
15462 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15463 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15464 
15465 	/*
15466 	 * We recheck the zone status to resolve the following race condition:
15467 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15468 	 * 2) hme0:1 is up and can't be brought down right away;
15469 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15470 	 * 3) zone "myzone" is halted; the zone status switches to
15471 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15472 	 * the interfaces to remove - hme0:1 is not returned because it's not
15473 	 * yet in "myzone", so it won't be removed;
15474 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15475 	 * status check here, we would have hme0:1 in "myzone" after it's been
15476 	 * destroyed.
15477 	 * Note that if the status check fails, we need to bring the interface
15478 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15479 	 * ipif_up_done[_v6]().
15480 	 */
15481 	status = ZONE_IS_UNINITIALIZED;
15482 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15483 		status = zone_status_get(zptr);
15484 		zone_rele(zptr);
15485 	}
15486 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15487 		if (ipif->ipif_isv6) {
15488 			(void) ipif_up_done_v6(ipif);
15489 		} else {
15490 			(void) ipif_up_done(ipif);
15491 		}
15492 		return (EINVAL);
15493 	}
15494 
15495 	(void) ipif_down_tail(ipif);
15496 
15497 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15498 	    B_TRUE));
15499 }
15500 
15501 /*
15502  * Return the number of addresses on `ill' with one or more of the values
15503  * in `set' set and all of the values in `clear' clear.
15504  */
15505 static uint_t
15506 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15507 {
15508 	ipif_t	*ipif;
15509 	uint_t	cnt = 0;
15510 
15511 	ASSERT(IAM_WRITER_ILL(ill));
15512 
15513 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15514 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15515 			cnt++;
15516 
15517 	return (cnt);
15518 }
15519 
15520 /*
15521  * Return the number of migratable addresses on `ill' that are under
15522  * application control.
15523  */
15524 uint_t
15525 ill_appaddr_cnt(const ill_t *ill)
15526 {
15527 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15528 	    IPIF_NOFAILOVER));
15529 }
15530 
15531 /*
15532  * Return the number of point-to-point addresses on `ill'.
15533  */
15534 uint_t
15535 ill_ptpaddr_cnt(const ill_t *ill)
15536 {
15537 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15538 }
15539 
15540 /* ARGSUSED */
15541 int
15542 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15543 	ip_ioctl_cmd_t *ipip, void *ifreq)
15544 {
15545 	struct lifreq	*lifr = ifreq;
15546 
15547 	ASSERT(q->q_next == NULL);
15548 	ASSERT(CONN_Q(q));
15549 
15550 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15551 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15552 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15553 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15554 
15555 	return (0);
15556 }
15557 
15558 /* Find the previous ILL in this usesrc group */
15559 static ill_t *
15560 ill_prev_usesrc(ill_t *uill)
15561 {
15562 	ill_t *ill;
15563 
15564 	for (ill = uill->ill_usesrc_grp_next;
15565 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15566 	    ill = ill->ill_usesrc_grp_next)
15567 		/* do nothing */;
15568 	return (ill);
15569 }
15570 
15571 /*
15572  * Release all members of the usesrc group. This routine is called
15573  * from ill_delete when the interface being unplumbed is the
15574  * group head.
15575  *
15576  * This silently clears the usesrc that ifconfig setup.
15577  * An alternative would be to keep that ifindex, and drop packets on the floor
15578  * since no source address can be selected.
15579  * Even if we keep the current semantics, don't need a lock and a linked list.
15580  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15581  * the one that is being removed. Issue is how we return the usesrc users
15582  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15583  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15584  * ill walk, but the walker would need to insert in the ioctl response.
15585  */
15586 static void
15587 ill_disband_usesrc_group(ill_t *uill)
15588 {
15589 	ill_t *next_ill, *tmp_ill;
15590 	ip_stack_t	*ipst = uill->ill_ipst;
15591 
15592 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15593 	next_ill = uill->ill_usesrc_grp_next;
15594 
15595 	do {
15596 		ASSERT(next_ill != NULL);
15597 		tmp_ill = next_ill->ill_usesrc_grp_next;
15598 		ASSERT(tmp_ill != NULL);
15599 		next_ill->ill_usesrc_grp_next = NULL;
15600 		next_ill->ill_usesrc_ifindex = 0;
15601 		next_ill = tmp_ill;
15602 	} while (next_ill->ill_usesrc_ifindex != 0);
15603 	uill->ill_usesrc_grp_next = NULL;
15604 }
15605 
15606 /*
15607  * Remove the client usesrc ILL from the list and relink to a new list
15608  */
15609 int
15610 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15611 {
15612 	ill_t *ill, *tmp_ill;
15613 	ip_stack_t	*ipst = ucill->ill_ipst;
15614 
15615 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15616 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15617 
15618 	/*
15619 	 * Check if the usesrc client ILL passed in is not already
15620 	 * in use as a usesrc ILL i.e one whose source address is
15621 	 * in use OR a usesrc ILL is not already in use as a usesrc
15622 	 * client ILL
15623 	 */
15624 	if ((ucill->ill_usesrc_ifindex == 0) ||
15625 	    (uill->ill_usesrc_ifindex != 0)) {
15626 		return (-1);
15627 	}
15628 
15629 	ill = ill_prev_usesrc(ucill);
15630 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15631 
15632 	/* Remove from the current list */
15633 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15634 		/* Only two elements in the list */
15635 		ASSERT(ill->ill_usesrc_ifindex == 0);
15636 		ill->ill_usesrc_grp_next = NULL;
15637 	} else {
15638 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15639 	}
15640 
15641 	if (ifindex == 0) {
15642 		ucill->ill_usesrc_ifindex = 0;
15643 		ucill->ill_usesrc_grp_next = NULL;
15644 		return (0);
15645 	}
15646 
15647 	ucill->ill_usesrc_ifindex = ifindex;
15648 	tmp_ill = uill->ill_usesrc_grp_next;
15649 	uill->ill_usesrc_grp_next = ucill;
15650 	ucill->ill_usesrc_grp_next =
15651 	    (tmp_ill != NULL) ? tmp_ill : uill;
15652 	return (0);
15653 }
15654 
15655 /*
15656  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15657  * ip.c for locking details.
15658  */
15659 /* ARGSUSED */
15660 int
15661 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15662     ip_ioctl_cmd_t *ipip, void *ifreq)
15663 {
15664 	struct lifreq *lifr = (struct lifreq *)ifreq;
15665 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15666 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15667 	int err = 0, ret;
15668 	uint_t ifindex;
15669 	ipsq_t *ipsq = NULL;
15670 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15671 
15672 	ASSERT(IAM_WRITER_IPIF(ipif));
15673 	ASSERT(q->q_next == NULL);
15674 	ASSERT(CONN_Q(q));
15675 
15676 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15677 
15678 	ifindex = lifr->lifr_index;
15679 	if (ifindex == 0) {
15680 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15681 			/* non usesrc group interface, nothing to reset */
15682 			return (0);
15683 		}
15684 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15685 		/* valid reset request */
15686 		reset_flg = B_TRUE;
15687 	}
15688 
15689 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15690 	if (usesrc_ill == NULL) {
15691 		return (ENXIO);
15692 	}
15693 
15694 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15695 	    NEW_OP, B_TRUE);
15696 	if (ipsq == NULL) {
15697 		err = EINPROGRESS;
15698 		/* Operation enqueued on the ipsq of the usesrc ILL */
15699 		goto done;
15700 	}
15701 
15702 	/* USESRC isn't currently supported with IPMP */
15703 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15704 		err = ENOTSUP;
15705 		goto done;
15706 	}
15707 
15708 	/*
15709 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15710 	 * used by IPMP underlying interfaces, but someone might think it's
15711 	 * more general and try to use it independently with VNI.)
15712 	 */
15713 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15714 		err = ENOTSUP;
15715 		goto done;
15716 	}
15717 
15718 	/*
15719 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15720 	 * already a client then return EINVAL
15721 	 */
15722 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15723 		err = EINVAL;
15724 		goto done;
15725 	}
15726 
15727 	/*
15728 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15729 	 * be then this is a duplicate operation.
15730 	 */
15731 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15732 		err = 0;
15733 		goto done;
15734 	}
15735 
15736 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15737 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15738 	    usesrc_ill->ill_isv6));
15739 
15740 	/*
15741 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15742 	 * and the ill_usesrc_ifindex fields
15743 	 */
15744 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15745 
15746 	if (reset_flg) {
15747 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15748 		if (ret != 0) {
15749 			err = EINVAL;
15750 		}
15751 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15752 		goto done;
15753 	}
15754 
15755 	/*
15756 	 * Four possibilities to consider:
15757 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15758 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15759 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15760 	 * 4. Both are part of their respective usesrc groups
15761 	 */
15762 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15763 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15764 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
15765 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15766 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15767 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
15768 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
15769 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15770 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15771 		/* Insert at head of list */
15772 		usesrc_cli_ill->ill_usesrc_grp_next =
15773 		    usesrc_ill->ill_usesrc_grp_next;
15774 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15775 	} else {
15776 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
15777 		    ifindex);
15778 		if (ret != 0)
15779 			err = EINVAL;
15780 	}
15781 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
15782 
15783 done:
15784 	if (ipsq != NULL)
15785 		ipsq_exit(ipsq);
15786 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
15787 	ill_refrele(usesrc_ill);
15788 
15789 	/* Let conn_ixa caching know that source address selection changed */
15790 	ip_update_source_selection(ipst);
15791 
15792 	return (err);
15793 }
15794 
15795 /*
15796  * comparison function used by avl.
15797  */
15798 static int
15799 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
15800 {
15801 
15802 	uint_t index;
15803 
15804 	ASSERT(phyip != NULL && index_ptr != NULL);
15805 
15806 	index = *((uint_t *)index_ptr);
15807 	/*
15808 	 * let the phyint with the lowest index be on top.
15809 	 */
15810 	if (((phyint_t *)phyip)->phyint_ifindex < index)
15811 		return (1);
15812 	if (((phyint_t *)phyip)->phyint_ifindex > index)
15813 		return (-1);
15814 	return (0);
15815 }
15816 
15817 /*
15818  * comparison function used by avl.
15819  */
15820 static int
15821 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
15822 {
15823 	ill_t *ill;
15824 	int res = 0;
15825 
15826 	ASSERT(phyip != NULL && name_ptr != NULL);
15827 
15828 	if (((phyint_t *)phyip)->phyint_illv4)
15829 		ill = ((phyint_t *)phyip)->phyint_illv4;
15830 	else
15831 		ill = ((phyint_t *)phyip)->phyint_illv6;
15832 	ASSERT(ill != NULL);
15833 
15834 	res = strcmp(ill->ill_name, (char *)name_ptr);
15835 	if (res > 0)
15836 		return (1);
15837 	else if (res < 0)
15838 		return (-1);
15839 	return (0);
15840 }
15841 
15842 /*
15843  * This function is called on the unplumb path via ill_glist_delete() when
15844  * there are no ills left on the phyint and thus the phyint can be freed.
15845  */
15846 static void
15847 phyint_free(phyint_t *phyi)
15848 {
15849 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
15850 
15851 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
15852 
15853 	/*
15854 	 * If this phyint was an IPMP meta-interface, blow away the group.
15855 	 * This is safe to do because all of the illgrps have already been
15856 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
15857 	 * If we're cleaning up as a result of failed initialization,
15858 	 * phyint_grp may be NULL.
15859 	 */
15860 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
15861 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
15862 		ipmp_grp_destroy(phyi->phyint_grp);
15863 		phyi->phyint_grp = NULL;
15864 		rw_exit(&ipst->ips_ipmp_lock);
15865 	}
15866 
15867 	/*
15868 	 * If this interface was under IPMP, take it out of the group.
15869 	 */
15870 	if (phyi->phyint_grp != NULL)
15871 		ipmp_phyint_leave_grp(phyi);
15872 
15873 	/*
15874 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
15875 	 * will be freed in ipsq_exit().
15876 	 */
15877 	phyi->phyint_ipsq->ipsq_phyint = NULL;
15878 	phyi->phyint_name[0] = '\0';
15879 
15880 	mi_free(phyi);
15881 }
15882 
15883 /*
15884  * Attach the ill to the phyint structure which can be shared by both
15885  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
15886  * function is called from ipif_set_values and ill_lookup_on_name (for
15887  * loopback) where we know the name of the ill. We lookup the ill and if
15888  * there is one present already with the name use that phyint. Otherwise
15889  * reuse the one allocated by ill_init.
15890  */
15891 static void
15892 ill_phyint_reinit(ill_t *ill)
15893 {
15894 	boolean_t isv6 = ill->ill_isv6;
15895 	phyint_t *phyi_old;
15896 	phyint_t *phyi;
15897 	avl_index_t where = 0;
15898 	ill_t	*ill_other = NULL;
15899 	ip_stack_t	*ipst = ill->ill_ipst;
15900 
15901 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
15902 
15903 	phyi_old = ill->ill_phyint;
15904 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
15905 	    phyi_old->phyint_illv6 == NULL));
15906 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
15907 	    phyi_old->phyint_illv4 == NULL));
15908 	ASSERT(phyi_old->phyint_ifindex == 0);
15909 
15910 	/*
15911 	 * Now that our ill has a name, set it in the phyint.
15912 	 */
15913 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
15914 
15915 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15916 	    ill->ill_name, &where);
15917 
15918 	/*
15919 	 * 1. We grabbed the ill_g_lock before inserting this ill into
15920 	 *    the global list of ills. So no other thread could have located
15921 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
15922 	 * 2. Now locate the other protocol instance of this ill.
15923 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
15924 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
15925 	 *    of neither ill can change.
15926 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
15927 	 *    other ill.
15928 	 * 5. Release all locks.
15929 	 */
15930 
15931 	/*
15932 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
15933 	 * we are initializing IPv4.
15934 	 */
15935 	if (phyi != NULL) {
15936 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
15937 		ASSERT(ill_other->ill_phyint != NULL);
15938 		ASSERT((isv6 && !ill_other->ill_isv6) ||
15939 		    (!isv6 && ill_other->ill_isv6));
15940 		GRAB_ILL_LOCKS(ill, ill_other);
15941 		/*
15942 		 * We are potentially throwing away phyint_flags which
15943 		 * could be different from the one that we obtain from
15944 		 * ill_other->ill_phyint. But it is okay as we are assuming
15945 		 * that the state maintained within IP is correct.
15946 		 */
15947 		mutex_enter(&phyi->phyint_lock);
15948 		if (isv6) {
15949 			ASSERT(phyi->phyint_illv6 == NULL);
15950 			phyi->phyint_illv6 = ill;
15951 		} else {
15952 			ASSERT(phyi->phyint_illv4 == NULL);
15953 			phyi->phyint_illv4 = ill;
15954 		}
15955 
15956 		/*
15957 		 * Delete the old phyint and make its ipsq eligible
15958 		 * to be freed in ipsq_exit().
15959 		 */
15960 		phyi_old->phyint_illv4 = NULL;
15961 		phyi_old->phyint_illv6 = NULL;
15962 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
15963 		phyi_old->phyint_name[0] = '\0';
15964 		mi_free(phyi_old);
15965 	} else {
15966 		mutex_enter(&ill->ill_lock);
15967 		/*
15968 		 * We don't need to acquire any lock, since
15969 		 * the ill is not yet visible globally  and we
15970 		 * have not yet released the ill_g_lock.
15971 		 */
15972 		phyi = phyi_old;
15973 		mutex_enter(&phyi->phyint_lock);
15974 		/* XXX We need a recovery strategy here. */
15975 		if (!phyint_assign_ifindex(phyi, ipst))
15976 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
15977 
15978 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15979 		    (void *)phyi, where);
15980 
15981 		(void) avl_find(&ipst->ips_phyint_g_list->
15982 		    phyint_list_avl_by_index,
15983 		    &phyi->phyint_ifindex, &where);
15984 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15985 		    (void *)phyi, where);
15986 	}
15987 
15988 	/*
15989 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
15990 	 * pending mp is not affected because that is per ill basis.
15991 	 */
15992 	ill->ill_phyint = phyi;
15993 
15994 	/*
15995 	 * Now that the phyint's ifindex has been assigned, complete the
15996 	 * remaining
15997 	 */
15998 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
15999 	if (ill->ill_isv6) {
16000 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16001 		    ill->ill_phyint->phyint_ifindex;
16002 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16003 	} else {
16004 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16005 	}
16006 
16007 	/*
16008 	 * Generate an event within the hooks framework to indicate that
16009 	 * a new interface has just been added to IP.  For this event to
16010 	 * be generated, the network interface must, at least, have an
16011 	 * ifindex assigned to it.  (We don't generate the event for
16012 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16013 	 *
16014 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16015 	 * that the ordering of delivered events to listeners matches the
16016 	 * order of them in the kernel.
16017 	 */
16018 	if (!IS_LOOPBACK(ill)) {
16019 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16020 		    ill->ill_name_length);
16021 	}
16022 	RELEASE_ILL_LOCKS(ill, ill_other);
16023 	mutex_exit(&phyi->phyint_lock);
16024 }
16025 
16026 /*
16027  * Notify any downstream modules of the name of this interface.
16028  * An M_IOCTL is used even though we don't expect a successful reply.
16029  * Any reply message from the driver (presumably an M_IOCNAK) will
16030  * eventually get discarded somewhere upstream.  The message format is
16031  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16032  * to IP.
16033  */
16034 static void
16035 ip_ifname_notify(ill_t *ill, queue_t *q)
16036 {
16037 	mblk_t *mp1, *mp2;
16038 	struct iocblk *iocp;
16039 	struct lifreq *lifr;
16040 
16041 	mp1 = mkiocb(SIOCSLIFNAME);
16042 	if (mp1 == NULL)
16043 		return;
16044 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16045 	if (mp2 == NULL) {
16046 		freeb(mp1);
16047 		return;
16048 	}
16049 
16050 	mp1->b_cont = mp2;
16051 	iocp = (struct iocblk *)mp1->b_rptr;
16052 	iocp->ioc_count = sizeof (struct lifreq);
16053 
16054 	lifr = (struct lifreq *)mp2->b_rptr;
16055 	mp2->b_wptr += sizeof (struct lifreq);
16056 	bzero(lifr, sizeof (struct lifreq));
16057 
16058 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16059 	lifr->lifr_ppa = ill->ill_ppa;
16060 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16061 
16062 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16063 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16064 	putnext(q, mp1);
16065 }
16066 
16067 static int
16068 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16069 {
16070 	int		err;
16071 	ip_stack_t	*ipst = ill->ill_ipst;
16072 	phyint_t	*phyi = ill->ill_phyint;
16073 
16074 	/* Set the obsolete NDD per-interface forwarding name. */
16075 	err = ill_set_ndd_name(ill);
16076 	if (err != 0) {
16077 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16078 		    err);
16079 	}
16080 
16081 	/*
16082 	 * Now that ill_name is set, the configuration for the IPMP
16083 	 * meta-interface can be performed.
16084 	 */
16085 	if (IS_IPMP(ill)) {
16086 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16087 		/*
16088 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16089 		 * meta-interface and we need to create the IPMP group.
16090 		 */
16091 		if (phyi->phyint_grp == NULL) {
16092 			/*
16093 			 * If someone has renamed another IPMP group to have
16094 			 * the same name as our interface, bail.
16095 			 */
16096 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16097 				rw_exit(&ipst->ips_ipmp_lock);
16098 				return (EEXIST);
16099 			}
16100 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16101 			if (phyi->phyint_grp == NULL) {
16102 				rw_exit(&ipst->ips_ipmp_lock);
16103 				return (ENOMEM);
16104 			}
16105 		}
16106 		rw_exit(&ipst->ips_ipmp_lock);
16107 	}
16108 
16109 	/* Tell downstream modules where they are. */
16110 	ip_ifname_notify(ill, q);
16111 
16112 	/*
16113 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16114 	 * Error cases are ENOMEM ...
16115 	 */
16116 	err = ill_dl_phys(ill, ipif, mp, q);
16117 
16118 	if (ill->ill_isv6) {
16119 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16120 		if (ipst->ips_mld_slowtimeout_id == 0) {
16121 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16122 			    (void *)ipst,
16123 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16124 		}
16125 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16126 	} else {
16127 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16128 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16129 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16130 			    (void *)ipst,
16131 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16132 		}
16133 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16134 	}
16135 
16136 	return (err);
16137 }
16138 
16139 /*
16140  * Common routine for ppa and ifname setting. Should be called exclusive.
16141  *
16142  * Returns EINPROGRESS when mp has been consumed by queueing it on
16143  * ipx_pending_mp and the ioctl will complete in ip_rput.
16144  *
16145  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16146  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16147  * For SLIFNAME, we pass these values back to the userland.
16148  */
16149 static int
16150 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16151 {
16152 	ill_t	*ill;
16153 	ipif_t	*ipif;
16154 	ipsq_t	*ipsq;
16155 	char	*ppa_ptr;
16156 	char	*old_ptr;
16157 	char	old_char;
16158 	int	error;
16159 	ip_stack_t	*ipst;
16160 
16161 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16162 	ASSERT(q->q_next != NULL);
16163 	ASSERT(interf_name != NULL);
16164 
16165 	ill = (ill_t *)q->q_ptr;
16166 	ipst = ill->ill_ipst;
16167 
16168 	ASSERT(ill->ill_ipst != NULL);
16169 	ASSERT(ill->ill_name[0] == '\0');
16170 	ASSERT(IAM_WRITER_ILL(ill));
16171 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16172 	ASSERT(ill->ill_ppa == UINT_MAX);
16173 
16174 	ill->ill_defend_start = ill->ill_defend_count = 0;
16175 	/* The ppa is sent down by ifconfig or is chosen */
16176 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16177 		return (EINVAL);
16178 	}
16179 
16180 	/*
16181 	 * make sure ppa passed in is same as ppa in the name.
16182 	 * This check is not made when ppa == UINT_MAX in that case ppa
16183 	 * in the name could be anything. System will choose a ppa and
16184 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16185 	 */
16186 	if (*new_ppa_ptr != UINT_MAX) {
16187 		/* stoi changes the pointer */
16188 		old_ptr = ppa_ptr;
16189 		/*
16190 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16191 		 * (they don't have an externally visible ppa).  We assign one
16192 		 * here so that we can manage the interface.  Note that in
16193 		 * the past this value was always 0 for DLPI 1 drivers.
16194 		 */
16195 		if (*new_ppa_ptr == 0)
16196 			*new_ppa_ptr = stoi(&old_ptr);
16197 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16198 			return (EINVAL);
16199 	}
16200 	/*
16201 	 * terminate string before ppa
16202 	 * save char at that location.
16203 	 */
16204 	old_char = ppa_ptr[0];
16205 	ppa_ptr[0] = '\0';
16206 
16207 	ill->ill_ppa = *new_ppa_ptr;
16208 	/*
16209 	 * Finish as much work now as possible before calling ill_glist_insert
16210 	 * which makes the ill globally visible and also merges it with the
16211 	 * other protocol instance of this phyint. The remaining work is
16212 	 * done after entering the ipsq which may happen sometime later.
16213 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16214 	 */
16215 	ipif = ill->ill_ipif;
16216 
16217 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16218 	ipif_assign_seqid(ipif);
16219 
16220 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16221 		ill->ill_flags |= ILLF_IPV4;
16222 
16223 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16224 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16225 
16226 	if (ill->ill_flags & ILLF_IPV6) {
16227 
16228 		ill->ill_isv6 = B_TRUE;
16229 		ill_set_inputfn(ill);
16230 		if (ill->ill_rq != NULL) {
16231 			ill->ill_rq->q_qinfo = &iprinitv6;
16232 		}
16233 
16234 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16235 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16236 		ipif->ipif_v6subnet = ipv6_all_zeros;
16237 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16238 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16239 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16240 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16241 		/*
16242 		 * point-to-point or Non-mulicast capable
16243 		 * interfaces won't do NUD unless explicitly
16244 		 * configured to do so.
16245 		 */
16246 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16247 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16248 			ill->ill_flags |= ILLF_NONUD;
16249 		}
16250 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16251 		if (ill->ill_flags & ILLF_NOARP) {
16252 			/*
16253 			 * Note: xresolv interfaces will eventually need
16254 			 * NOARP set here as well, but that will require
16255 			 * those external resolvers to have some
16256 			 * knowledge of that flag and act appropriately.
16257 			 * Not to be changed at present.
16258 			 */
16259 			ill->ill_flags &= ~ILLF_NOARP;
16260 		}
16261 		/*
16262 		 * Set the ILLF_ROUTER flag according to the global
16263 		 * IPv6 forwarding policy.
16264 		 */
16265 		if (ipst->ips_ipv6_forward != 0)
16266 			ill->ill_flags |= ILLF_ROUTER;
16267 	} else if (ill->ill_flags & ILLF_IPV4) {
16268 		ill->ill_isv6 = B_FALSE;
16269 		ill_set_inputfn(ill);
16270 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16271 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16272 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16273 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16274 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16275 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16276 		/*
16277 		 * Set the ILLF_ROUTER flag according to the global
16278 		 * IPv4 forwarding policy.
16279 		 */
16280 		if (ipst->ips_ip_g_forward != 0)
16281 			ill->ill_flags |= ILLF_ROUTER;
16282 	}
16283 
16284 	ASSERT(ill->ill_phyint != NULL);
16285 
16286 	/*
16287 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16288 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16289 	 */
16290 	if (!ill_allocate_mibs(ill))
16291 		return (ENOMEM);
16292 
16293 	/*
16294 	 * Pick a default sap until we get the DL_INFO_ACK back from
16295 	 * the driver.
16296 	 */
16297 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16298 	    ill->ill_media->ip_m_ipv4sap;
16299 
16300 	ill->ill_ifname_pending = 1;
16301 	ill->ill_ifname_pending_err = 0;
16302 
16303 	/*
16304 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16305 	 * that were joined while this ill was not bound to the DLPI link need
16306 	 * to be recovered by ill_recover_multicast().
16307 	 */
16308 	ill->ill_need_recover_multicast = 1;
16309 
16310 	ill_refhold(ill);
16311 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16312 	if ((error = ill_glist_insert(ill, interf_name,
16313 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16314 		ill->ill_ppa = UINT_MAX;
16315 		ill->ill_name[0] = '\0';
16316 		/*
16317 		 * undo null termination done above.
16318 		 */
16319 		ppa_ptr[0] = old_char;
16320 		rw_exit(&ipst->ips_ill_g_lock);
16321 		ill_refrele(ill);
16322 		return (error);
16323 	}
16324 
16325 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16326 
16327 	/*
16328 	 * When we return the buffer pointed to by interf_name should contain
16329 	 * the same name as in ill_name.
16330 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16331 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16332 	 * so copy full name and update the ppa ptr.
16333 	 * When ppa passed in != UINT_MAX all values are correct just undo
16334 	 * null termination, this saves a bcopy.
16335 	 */
16336 	if (*new_ppa_ptr == UINT_MAX) {
16337 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16338 		*new_ppa_ptr = ill->ill_ppa;
16339 	} else {
16340 		/*
16341 		 * undo null termination done above.
16342 		 */
16343 		ppa_ptr[0] = old_char;
16344 	}
16345 
16346 	/* Let SCTP know about this ILL */
16347 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16348 
16349 	/*
16350 	 * ill_glist_insert has made the ill visible globally, and
16351 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16352 	 * we need to hold the ips_ill_g_lock across the call to enter the
16353 	 * ipsq to enforce atomicity and prevent reordering. In the event
16354 	 * the ipsq has changed, and if the new ipsq is currently busy,
16355 	 * we need to make sure that this half-completed ioctl is ahead of
16356 	 * any subsequent ioctl. We achieve this by not dropping the
16357 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16358 	 * ensuring that new ioctls can't start.
16359 	 */
16360 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16361 	    B_TRUE);
16362 
16363 	rw_exit(&ipst->ips_ill_g_lock);
16364 	ill_refrele(ill);
16365 	if (ipsq == NULL)
16366 		return (EINPROGRESS);
16367 
16368 	/*
16369 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16370 	 */
16371 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16372 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16373 	else
16374 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16375 
16376 	error = ipif_set_values_tail(ill, ipif, mp, q);
16377 	ipsq_exit(ipsq);
16378 	if (error != 0 && error != EINPROGRESS) {
16379 		/*
16380 		 * restore previous values
16381 		 */
16382 		ill->ill_isv6 = B_FALSE;
16383 		ill_set_inputfn(ill);
16384 	}
16385 	return (error);
16386 }
16387 
16388 void
16389 ipif_init(ip_stack_t *ipst)
16390 {
16391 	int i;
16392 
16393 	for (i = 0; i < MAX_G_HEADS; i++) {
16394 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16395 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16396 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16397 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16398 	}
16399 
16400 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16401 	    ill_phyint_compare_index,
16402 	    sizeof (phyint_t),
16403 	    offsetof(struct phyint, phyint_avl_by_index));
16404 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16405 	    ill_phyint_compare_name,
16406 	    sizeof (phyint_t),
16407 	    offsetof(struct phyint, phyint_avl_by_name));
16408 }
16409 
16410 /*
16411  * Save enough information so that we can recreate the IRE if
16412  * the interface goes down and then up.
16413  */
16414 void
16415 ill_save_ire(ill_t *ill, ire_t *ire)
16416 {
16417 	mblk_t	*save_mp;
16418 
16419 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16420 	if (save_mp != NULL) {
16421 		ifrt_t	*ifrt;
16422 
16423 		save_mp->b_wptr += sizeof (ifrt_t);
16424 		ifrt = (ifrt_t *)save_mp->b_rptr;
16425 		bzero(ifrt, sizeof (ifrt_t));
16426 		ifrt->ifrt_type = ire->ire_type;
16427 		if (ire->ire_ipversion == IPV4_VERSION) {
16428 			ASSERT(!ill->ill_isv6);
16429 			ifrt->ifrt_addr = ire->ire_addr;
16430 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16431 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16432 			ifrt->ifrt_mask = ire->ire_mask;
16433 		} else {
16434 			ASSERT(ill->ill_isv6);
16435 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16436 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16437 			mutex_enter(&ire->ire_lock);
16438 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16439 			mutex_exit(&ire->ire_lock);
16440 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16441 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16442 		}
16443 		ifrt->ifrt_flags = ire->ire_flags;
16444 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16445 		mutex_enter(&ill->ill_saved_ire_lock);
16446 		save_mp->b_cont = ill->ill_saved_ire_mp;
16447 		ill->ill_saved_ire_mp = save_mp;
16448 		ill->ill_saved_ire_cnt++;
16449 		mutex_exit(&ill->ill_saved_ire_lock);
16450 	}
16451 }
16452 
16453 /*
16454  * Remove one entry from ill_saved_ire_mp.
16455  */
16456 void
16457 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16458 {
16459 	mblk_t	**mpp;
16460 	mblk_t	*mp;
16461 	ifrt_t	*ifrt;
16462 
16463 	/* Remove from ill_saved_ire_mp list if it is there */
16464 	mutex_enter(&ill->ill_saved_ire_lock);
16465 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16466 	    mpp = &(*mpp)->b_cont) {
16467 		in6_addr_t	gw_addr_v6;
16468 
16469 		/*
16470 		 * On a given ill, the tuple of address, gateway, mask,
16471 		 * ire_type, and zoneid is unique for each saved IRE.
16472 		 */
16473 		mp = *mpp;
16474 		ifrt = (ifrt_t *)mp->b_rptr;
16475 		/* ire_gateway_addr_v6 can change - need lock */
16476 		mutex_enter(&ire->ire_lock);
16477 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16478 		mutex_exit(&ire->ire_lock);
16479 
16480 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16481 		    ifrt->ifrt_type != ire->ire_type)
16482 			continue;
16483 
16484 		if (ill->ill_isv6 ?
16485 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16486 		    &ire->ire_addr_v6) &&
16487 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16488 		    &gw_addr_v6) &&
16489 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16490 		    &ire->ire_mask_v6)) :
16491 		    (ifrt->ifrt_addr == ire->ire_addr &&
16492 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16493 		    ifrt->ifrt_mask == ire->ire_mask)) {
16494 			*mpp = mp->b_cont;
16495 			ill->ill_saved_ire_cnt--;
16496 			freeb(mp);
16497 			break;
16498 		}
16499 	}
16500 	mutex_exit(&ill->ill_saved_ire_lock);
16501 }
16502 
16503 /*
16504  * IP multirouting broadcast routes handling
16505  * Append CGTP broadcast IREs to regular ones created
16506  * at ifconfig time.
16507  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16508  * the destination and the gateway are broadcast addresses.
16509  * The caller has verified that the destination is an IRE_BROADCAST and that
16510  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16511  * we create a MULTIRT IRE_BROADCAST.
16512  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16513  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16514  */
16515 static void
16516 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16517 {
16518 	ire_t *ire_prim;
16519 
16520 	ASSERT(ire != NULL);
16521 
16522 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16523 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16524 	    NULL);
16525 	if (ire_prim != NULL) {
16526 		/*
16527 		 * We are in the special case of broadcasts for
16528 		 * CGTP. We add an IRE_BROADCAST that holds
16529 		 * the RTF_MULTIRT flag, the destination
16530 		 * address and the low level
16531 		 * info of ire_prim. In other words, CGTP
16532 		 * broadcast is added to the redundant ipif.
16533 		 */
16534 		ill_t *ill_prim;
16535 		ire_t  *bcast_ire;
16536 
16537 		ill_prim = ire_prim->ire_ill;
16538 
16539 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16540 		    (void *)ire_prim, (void *)ill_prim));
16541 
16542 		bcast_ire = ire_create(
16543 		    (uchar_t *)&ire->ire_addr,
16544 		    (uchar_t *)&ip_g_all_ones,
16545 		    (uchar_t *)&ire->ire_gateway_addr,
16546 		    IRE_BROADCAST,
16547 		    ill_prim,
16548 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16549 		    ire->ire_flags | RTF_KERNEL,
16550 		    NULL,
16551 		    ipst);
16552 
16553 		/*
16554 		 * Here we assume that ire_add does head insertion so that
16555 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16556 		 */
16557 		if (bcast_ire != NULL) {
16558 			if (ire->ire_flags & RTF_SETSRC) {
16559 				bcast_ire->ire_setsrc_addr =
16560 				    ire->ire_setsrc_addr;
16561 			}
16562 			bcast_ire = ire_add(bcast_ire);
16563 			if (bcast_ire != NULL) {
16564 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16565 				    "added bcast_ire %p\n",
16566 				    (void *)bcast_ire));
16567 
16568 				ill_save_ire(ill_prim, bcast_ire);
16569 				ire_refrele(bcast_ire);
16570 			}
16571 		}
16572 		ire_refrele(ire_prim);
16573 	}
16574 }
16575 
16576 /*
16577  * IP multirouting broadcast routes handling
16578  * Remove the broadcast ire.
16579  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16580  * the destination and the gateway are broadcast addresses.
16581  * The caller has only verified that RTF_MULTIRT was set. We check
16582  * that the destination is broadcast and that the gateway is a broadcast
16583  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16584  */
16585 static void
16586 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16587 {
16588 	ASSERT(ire != NULL);
16589 
16590 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16591 		ire_t *ire_prim;
16592 
16593 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16594 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16595 		    ipst, NULL);
16596 		if (ire_prim != NULL) {
16597 			ill_t *ill_prim;
16598 			ire_t  *bcast_ire;
16599 
16600 			ill_prim = ire_prim->ire_ill;
16601 
16602 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16603 			    "ire_prim %p, ill_prim %p\n",
16604 			    (void *)ire_prim, (void *)ill_prim));
16605 
16606 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16607 			    ire->ire_gateway_addr, IRE_BROADCAST,
16608 			    ill_prim, ALL_ZONES, NULL,
16609 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16610 			    MATCH_IRE_MASK, 0, ipst, NULL);
16611 
16612 			if (bcast_ire != NULL) {
16613 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16614 				    "looked up bcast_ire %p\n",
16615 				    (void *)bcast_ire));
16616 				ill_remove_saved_ire(bcast_ire->ire_ill,
16617 				    bcast_ire);
16618 				ire_delete(bcast_ire);
16619 				ire_refrele(bcast_ire);
16620 			}
16621 			ire_refrele(ire_prim);
16622 		}
16623 	}
16624 }
16625 
16626 /*
16627  * Derive an interface id from the link layer address.
16628  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16629  */
16630 static void
16631 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16632 {
16633 	char		*addr;
16634 
16635 	/*
16636 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16637 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16638 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16639 	 * interface ID on IPv6 interfaces above links that actually have real
16640 	 * Ethernet addresses.
16641 	 */
16642 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16643 		/* Form EUI-64 like address */
16644 		addr = (char *)&v6addr->s6_addr32[2];
16645 		bcopy(ill->ill_phys_addr, addr, 3);
16646 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16647 		addr[3] = (char)0xff;
16648 		addr[4] = (char)0xfe;
16649 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16650 	}
16651 }
16652 
16653 /* ARGSUSED */
16654 static void
16655 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16656 {
16657 }
16658 
16659 typedef struct ipmp_ifcookie {
16660 	uint32_t	ic_hostid;
16661 	char		ic_ifname[LIFNAMSIZ];
16662 	char		ic_zonename[ZONENAME_MAX];
16663 } ipmp_ifcookie_t;
16664 
16665 /*
16666  * Construct a pseudo-random interface ID for the IPMP interface that's both
16667  * predictable and (almost) guaranteed to be unique.
16668  */
16669 static void
16670 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16671 {
16672 	zone_t		*zp;
16673 	uint8_t		*addr;
16674 	uchar_t		hash[16];
16675 	ulong_t 	hostid;
16676 	MD5_CTX		ctx;
16677 	ipmp_ifcookie_t	ic = { 0 };
16678 
16679 	ASSERT(IS_IPMP(ill));
16680 
16681 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16682 	ic.ic_hostid = htonl((uint32_t)hostid);
16683 
16684 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16685 
16686 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16687 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16688 		zone_rele(zp);
16689 	}
16690 
16691 	MD5Init(&ctx);
16692 	MD5Update(&ctx, &ic, sizeof (ic));
16693 	MD5Final(hash, &ctx);
16694 
16695 	/*
16696 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16697 	 */
16698 	addr = &v6addr->s6_addr8[8];
16699 	bcopy(hash + 8, addr, sizeof (uint64_t));
16700 	addr[0] &= ~0x2;				/* set local bit */
16701 }
16702 
16703 /*
16704  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16705  */
16706 static void
16707 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16708 {
16709 	phyint_t *phyi = ill->ill_phyint;
16710 
16711 	/*
16712 	 * Check PHYI_MULTI_BCAST and length of physical
16713 	 * address to determine if we use the mapping or the
16714 	 * broadcast address.
16715 	 */
16716 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16717 	    ill->ill_phys_addr_length != ETHERADDRL) {
16718 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16719 		return;
16720 	}
16721 	m_physaddr[0] = 0x33;
16722 	m_physaddr[1] = 0x33;
16723 	m_physaddr[2] = m_ip6addr[12];
16724 	m_physaddr[3] = m_ip6addr[13];
16725 	m_physaddr[4] = m_ip6addr[14];
16726 	m_physaddr[5] = m_ip6addr[15];
16727 }
16728 
16729 /*
16730  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16731  */
16732 static void
16733 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16734 {
16735 	phyint_t *phyi = ill->ill_phyint;
16736 
16737 	/*
16738 	 * Check PHYI_MULTI_BCAST and length of physical
16739 	 * address to determine if we use the mapping or the
16740 	 * broadcast address.
16741 	 */
16742 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16743 	    ill->ill_phys_addr_length != ETHERADDRL) {
16744 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16745 		return;
16746 	}
16747 	m_physaddr[0] = 0x01;
16748 	m_physaddr[1] = 0x00;
16749 	m_physaddr[2] = 0x5e;
16750 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16751 	m_physaddr[4] = m_ipaddr[2];
16752 	m_physaddr[5] = m_ipaddr[3];
16753 }
16754 
16755 /* ARGSUSED */
16756 static void
16757 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16758 {
16759 	/*
16760 	 * for the MULTI_BCAST case and other cases when we want to
16761 	 * use the link-layer broadcast address for multicast.
16762 	 */
16763 	uint8_t	*bphys_addr;
16764 	dl_unitdata_req_t *dlur;
16765 
16766 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16767 	if (ill->ill_sap_length < 0) {
16768 		bphys_addr = (uchar_t *)dlur +
16769 		    dlur->dl_dest_addr_offset;
16770 	} else  {
16771 		bphys_addr = (uchar_t *)dlur +
16772 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
16773 	}
16774 
16775 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
16776 }
16777 
16778 /*
16779  * Derive IPoIB interface id from the link layer address.
16780  */
16781 static void
16782 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16783 {
16784 	char		*addr;
16785 
16786 	ASSERT(ill->ill_phys_addr_length == 20);
16787 	addr = (char *)&v6addr->s6_addr32[2];
16788 	bcopy(ill->ill_phys_addr + 12, addr, 8);
16789 	/*
16790 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
16791 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
16792 	 * rules. In these cases, the IBA considers these GUIDs to be in
16793 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
16794 	 * required; vendors are required not to assign global EUI-64's
16795 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
16796 	 * of the interface identifier. Whether the GUID is in modified
16797 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
16798 	 * bit set to 1.
16799 	 */
16800 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
16801 }
16802 
16803 /*
16804  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
16805  * Note on mapping from multicast IP addresses to IPoIB multicast link
16806  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
16807  * The format of an IPoIB multicast address is:
16808  *
16809  *  4 byte QPN      Scope Sign.  Pkey
16810  * +--------------------------------------------+
16811  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
16812  * +--------------------------------------------+
16813  *
16814  * The Scope and Pkey components are properties of the IBA port and
16815  * network interface. They can be ascertained from the broadcast address.
16816  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
16817  */
16818 static void
16819 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16820 {
16821 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16822 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
16823 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16824 	uint8_t	*bphys_addr;
16825 	dl_unitdata_req_t *dlur;
16826 
16827 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
16828 
16829 	/*
16830 	 * RFC 4391: IPv4 MGID is 28-bit long.
16831 	 */
16832 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
16833 	m_physaddr[17] = m_ipaddr[1];
16834 	m_physaddr[18] = m_ipaddr[2];
16835 	m_physaddr[19] = m_ipaddr[3];
16836 
16837 
16838 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16839 	if (ill->ill_sap_length < 0) {
16840 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
16841 	} else  {
16842 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
16843 		    ill->ill_sap_length;
16844 	}
16845 	/*
16846 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
16847 	 */
16848 	m_physaddr[5] = bphys_addr[5];
16849 	m_physaddr[8] = bphys_addr[8];
16850 	m_physaddr[9] = bphys_addr[9];
16851 }
16852 
16853 static void
16854 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16855 {
16856 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16857 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
16858 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16859 	uint8_t	*bphys_addr;
16860 	dl_unitdata_req_t *dlur;
16861 
16862 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
16863 
16864 	/*
16865 	 * RFC 4391: IPv4 MGID is 80-bit long.
16866 	 */
16867 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
16868 
16869 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16870 	if (ill->ill_sap_length < 0) {
16871 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
16872 	} else  {
16873 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
16874 		    ill->ill_sap_length;
16875 	}
16876 	/*
16877 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
16878 	 */
16879 	m_physaddr[5] = bphys_addr[5];
16880 	m_physaddr[8] = bphys_addr[8];
16881 	m_physaddr[9] = bphys_addr[9];
16882 }
16883 
16884 /*
16885  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
16886  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
16887  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
16888  * of RFC4213.
16889  */
16890 static void
16891 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
16892 {
16893 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
16894 	v6addr->s6_addr32[2] = 0;
16895 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
16896 }
16897 
16898 /*
16899  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
16900  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
16901  * id.
16902  */
16903 static void
16904 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
16905 {
16906 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
16907 
16908 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
16909 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
16910 }
16911 
16912 static void
16913 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16914 {
16915 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
16916 }
16917 
16918 static void
16919 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
16920 {
16921 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
16922 }
16923 
16924 static void
16925 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16926 {
16927 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
16928 }
16929 
16930 static void
16931 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
16932 {
16933 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
16934 }
16935 
16936 /*
16937  * Lookup an ill and verify that the zoneid has an ipif on that ill.
16938  * Returns an held ill, or NULL.
16939  */
16940 ill_t *
16941 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
16942     ip_stack_t *ipst)
16943 {
16944 	ill_t	*ill;
16945 	ipif_t	*ipif;
16946 
16947 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
16948 	if (ill == NULL)
16949 		return (NULL);
16950 
16951 	mutex_enter(&ill->ill_lock);
16952 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16953 		if (IPIF_IS_CONDEMNED(ipif))
16954 			continue;
16955 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
16956 		    ipif->ipif_zoneid != ALL_ZONES)
16957 			continue;
16958 
16959 		mutex_exit(&ill->ill_lock);
16960 		return (ill);
16961 	}
16962 	mutex_exit(&ill->ill_lock);
16963 	ill_refrele(ill);
16964 	return (NULL);
16965 }
16966 
16967 /*
16968  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
16969  * If a pointer to an ipif_t is returned then the caller will need to do
16970  * an ill_refrele().
16971  */
16972 ipif_t *
16973 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
16974     ip_stack_t *ipst)
16975 {
16976 	ipif_t *ipif;
16977 	ill_t *ill;
16978 
16979 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16980 	if (ill == NULL)
16981 		return (NULL);
16982 
16983 	mutex_enter(&ill->ill_lock);
16984 	if (ill->ill_state_flags & ILL_CONDEMNED) {
16985 		mutex_exit(&ill->ill_lock);
16986 		ill_refrele(ill);
16987 		return (NULL);
16988 	}
16989 
16990 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
16991 		if (!IPIF_CAN_LOOKUP(ipif))
16992 			continue;
16993 		if (lifidx == ipif->ipif_id) {
16994 			ipif_refhold_locked(ipif);
16995 			break;
16996 		}
16997 	}
16998 
16999 	mutex_exit(&ill->ill_lock);
17000 	ill_refrele(ill);
17001 	return (ipif);
17002 }
17003 
17004 /*
17005  * Set ill_inputfn based on the current know state.
17006  * This needs to be called when any of the factors taken into
17007  * account changes.
17008  */
17009 void
17010 ill_set_inputfn(ill_t *ill)
17011 {
17012 	ip_stack_t	*ipst = ill->ill_ipst;
17013 
17014 	if (ill->ill_isv6) {
17015 		if (is_system_labeled())
17016 			ill->ill_inputfn = ill_input_full_v6;
17017 		else
17018 			ill->ill_inputfn = ill_input_short_v6;
17019 	} else {
17020 		if (is_system_labeled())
17021 			ill->ill_inputfn = ill_input_full_v4;
17022 		else if (ill->ill_dhcpinit != 0)
17023 			ill->ill_inputfn = ill_input_full_v4;
17024 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17025 		    != NULL)
17026 			ill->ill_inputfn = ill_input_full_v4;
17027 		else if (ipst->ips_ip_cgtp_filter &&
17028 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17029 			ill->ill_inputfn = ill_input_full_v4;
17030 		else
17031 			ill->ill_inputfn = ill_input_short_v4;
17032 	}
17033 }
17034 
17035 /*
17036  * Re-evaluate ill_inputfn for all the IPv4 ills.
17037  * Used when RSVP and CGTP comes and goes.
17038  */
17039 void
17040 ill_set_inputfn_all(ip_stack_t *ipst)
17041 {
17042 	ill_walk_context_t	ctx;
17043 	ill_t			*ill;
17044 
17045 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17046 	ill = ILL_START_WALK_V4(&ctx, ipst);
17047 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17048 		ill_set_inputfn(ill);
17049 
17050 	rw_exit(&ipst->ips_ill_g_lock);
17051 }
17052 
17053 /*
17054  * Set the physical address information for `ill' to the contents of the
17055  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17056  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17057  * EINPROGRESS will be returned.
17058  */
17059 int
17060 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17061 {
17062 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17063 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17064 
17065 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17066 
17067 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17068 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17069 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17070 		/* Changing DL_IPV6_TOKEN is not yet supported */
17071 		return (0);
17072 	}
17073 
17074 	/*
17075 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17076 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17077 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17078 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17079 	 */
17080 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17081 		freemsg(mp);
17082 		return (ENOMEM);
17083 	}
17084 
17085 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17086 	mutex_enter(&ill->ill_lock);
17087 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17088 	/* no more nce addition allowed */
17089 	mutex_exit(&ill->ill_lock);
17090 
17091 	/*
17092 	 * If we can quiesce the ill, then set the address.  If not, then
17093 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17094 	 */
17095 	ill_down_ipifs(ill, B_TRUE);
17096 	mutex_enter(&ill->ill_lock);
17097 	if (!ill_is_quiescent(ill)) {
17098 		/* call cannot fail since `conn_t *' argument is NULL */
17099 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17100 		    mp, ILL_DOWN);
17101 		mutex_exit(&ill->ill_lock);
17102 		return (EINPROGRESS);
17103 	}
17104 	mutex_exit(&ill->ill_lock);
17105 
17106 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17107 	return (0);
17108 }
17109 
17110 /*
17111  * Once the ill associated with `q' has quiesced, set its physical address
17112  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17113  * are passed (linked by b_cont), since we sometimes need to save two distinct
17114  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17115  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17116  * is quiesced, we know any stale nce's with the old address information have
17117  * already been removed, so we don't need to call nce_flush().
17118  */
17119 /* ARGSUSED */
17120 static void
17121 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17122 {
17123 	ill_t		*ill = q->q_ptr;
17124 	mblk_t		*addrmp2 = unlinkb(addrmp);
17125 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17126 	uint_t		addrlen, addroff;
17127 	int		status;
17128 
17129 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17130 
17131 	addroff	= dlindp->dl_addr_offset;
17132 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17133 
17134 	switch (dlindp->dl_data) {
17135 	case DL_IPV6_LINK_LAYER_ADDR:
17136 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17137 		freemsg(addrmp2);
17138 		break;
17139 
17140 	case DL_CURR_DEST_ADDR:
17141 		freemsg(ill->ill_dest_addr_mp);
17142 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17143 		ill->ill_dest_addr_mp = addrmp;
17144 		if (ill->ill_isv6) {
17145 			ill_setdesttoken(ill);
17146 			ipif_setdestlinklocal(ill->ill_ipif);
17147 		}
17148 		freemsg(addrmp2);
17149 		break;
17150 
17151 	case DL_CURR_PHYS_ADDR:
17152 		freemsg(ill->ill_phys_addr_mp);
17153 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17154 		ill->ill_phys_addr_mp = addrmp;
17155 		ill->ill_phys_addr_length = addrlen;
17156 		if (ill->ill_isv6)
17157 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17158 		else
17159 			freemsg(addrmp2);
17160 		if (ill->ill_isv6) {
17161 			ill_setdefaulttoken(ill);
17162 			ipif_setlinklocal(ill->ill_ipif);
17163 		}
17164 		break;
17165 	default:
17166 		ASSERT(0);
17167 	}
17168 
17169 	/*
17170 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17171 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17172 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17173 	 * brought up.
17174 	 */
17175 	status = ill_up_ipifs(ill, q, addrmp);
17176 	mutex_enter(&ill->ill_lock);
17177 	if (ill->ill_dl_up)
17178 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17179 	mutex_exit(&ill->ill_lock);
17180 	if (status != EINPROGRESS)
17181 		ipsq_current_finish(ipsq);
17182 }
17183 
17184 /*
17185  * Helper routine for setting the ill_nd_lla fields.
17186  */
17187 void
17188 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17189 {
17190 	freemsg(ill->ill_nd_lla_mp);
17191 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17192 	ill->ill_nd_lla_mp = ndmp;
17193 	ill->ill_nd_lla_len = addrlen;
17194 }
17195 
17196 /*
17197  * Replumb the ill.
17198  */
17199 int
17200 ill_replumb(ill_t *ill, mblk_t *mp)
17201 {
17202 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17203 
17204 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17205 
17206 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17207 
17208 	mutex_enter(&ill->ill_lock);
17209 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17210 	/* no more nce addition allowed */
17211 	mutex_exit(&ill->ill_lock);
17212 
17213 	/*
17214 	 * If we can quiesce the ill, then continue.  If not, then
17215 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17216 	 */
17217 	ill_down_ipifs(ill, B_FALSE);
17218 
17219 	mutex_enter(&ill->ill_lock);
17220 	if (!ill_is_quiescent(ill)) {
17221 		/* call cannot fail since `conn_t *' argument is NULL */
17222 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17223 		    mp, ILL_DOWN);
17224 		mutex_exit(&ill->ill_lock);
17225 		return (EINPROGRESS);
17226 	}
17227 	mutex_exit(&ill->ill_lock);
17228 
17229 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17230 	return (0);
17231 }
17232 
17233 /* ARGSUSED */
17234 static void
17235 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17236 {
17237 	ill_t *ill = q->q_ptr;
17238 	int err;
17239 	conn_t *connp = NULL;
17240 
17241 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17242 	freemsg(ill->ill_replumb_mp);
17243 	ill->ill_replumb_mp = copyb(mp);
17244 
17245 	if (ill->ill_replumb_mp == NULL) {
17246 		/* out of memory */
17247 		ipsq_current_finish(ipsq);
17248 		return;
17249 	}
17250 
17251 	mutex_enter(&ill->ill_lock);
17252 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17253 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17254 	mutex_exit(&ill->ill_lock);
17255 
17256 	if (!ill->ill_up_ipifs) {
17257 		/* already closing */
17258 		ipsq_current_finish(ipsq);
17259 		return;
17260 	}
17261 	ill->ill_replumbing = 1;
17262 	err = ill_down_ipifs_tail(ill);
17263 
17264 	/*
17265 	 * Successfully quiesced and brought down the interface, now we send
17266 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17267 	 * DL_NOTE_REPLUMB message.
17268 	 */
17269 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17270 	    DL_NOTIFY_CONF);
17271 	ASSERT(mp != NULL);
17272 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17273 	    DL_NOTE_REPLUMB_DONE;
17274 	ill_dlpi_send(ill, mp);
17275 
17276 	/*
17277 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17278 	 * streams have to be unbound. When all the DLPI exchanges are done,
17279 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17280 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17281 	 * arp_bringup_done().
17282 	 */
17283 	ASSERT(ill->ill_replumb_mp != NULL);
17284 	if (err == EINPROGRESS)
17285 		return;
17286 	else
17287 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17288 	ASSERT(connp == NULL);
17289 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17290 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17291 		return;
17292 	}
17293 	ipsq_current_finish(ipsq);
17294 }
17295 
17296 /*
17297  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17298  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17299  * as per the ioctl.  On failure, an errno is returned.
17300  */
17301 static int
17302 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17303 {
17304 	int rval;
17305 	struct strioctl iocb;
17306 
17307 	iocb.ic_cmd = cmd;
17308 	iocb.ic_timout = 15;
17309 	iocb.ic_len = bufsize;
17310 	iocb.ic_dp = buf;
17311 
17312 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17313 }
17314 
17315 /*
17316  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17317  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17318  */
17319 static int
17320 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17321     uint_t *bufsizep, cred_t *cr)
17322 {
17323 	int err;
17324 	struct lifnum lifn;
17325 
17326 	bzero(&lifn, sizeof (lifn));
17327 	lifn.lifn_family = af;
17328 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17329 
17330 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17331 		return (err);
17332 
17333 	/*
17334 	 * Pad the interface count to account for additional interfaces that
17335 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17336 	 */
17337 	lifn.lifn_count += 4;
17338 	bzero(lifcp, sizeof (*lifcp));
17339 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17340 	lifcp->lifc_family = af;
17341 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17342 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17343 
17344 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17345 	if (err != 0) {
17346 		kmem_free(lifcp->lifc_buf, *bufsizep);
17347 		return (err);
17348 	}
17349 
17350 	return (0);
17351 }
17352 
17353 /*
17354  * Helper for ip_interface_cleanup() that removes the loopback interface.
17355  */
17356 static void
17357 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17358 {
17359 	int err;
17360 	struct lifreq lifr;
17361 
17362 	bzero(&lifr, sizeof (lifr));
17363 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17364 
17365 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17366 	if (err != 0) {
17367 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17368 		    "error %d\n", isv6 ? "v6" : "v4", err));
17369 	}
17370 }
17371 
17372 /*
17373  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17374  * groups and that IPMP data addresses are down.  These conditions must be met
17375  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17376  */
17377 static void
17378 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17379 {
17380 	int af = isv6 ? AF_INET6 : AF_INET;
17381 	int i, nifs;
17382 	int err;
17383 	uint_t bufsize;
17384 	uint_t lifrsize = sizeof (struct lifreq);
17385 	struct lifconf lifc;
17386 	struct lifreq *lifrp;
17387 
17388 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17389 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17390 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17391 		return;
17392 	}
17393 
17394 	nifs = lifc.lifc_len / lifrsize;
17395 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17396 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17397 		if (err != 0) {
17398 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17399 			    "flags: error %d", lifrp->lifr_name, err);
17400 			continue;
17401 		}
17402 
17403 		if (lifrp->lifr_flags & IFF_IPMP) {
17404 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17405 				continue;
17406 
17407 			lifrp->lifr_flags &= ~IFF_UP;
17408 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17409 			if (err != 0) {
17410 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17411 				    "bring down (error %d); IPMP interface may "
17412 				    "not be shutdown", lifrp->lifr_name, err);
17413 			}
17414 
17415 			/*
17416 			 * Check if IFF_DUPLICATE is still set -- and if so,
17417 			 * reset the address to clear it.
17418 			 */
17419 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17420 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17421 				continue;
17422 
17423 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17424 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17425 			    lifrp, lifrsize, cr)) != 0) {
17426 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17427 				    "reset DAD (error %d); IPMP interface may "
17428 				    "not be shutdown", lifrp->lifr_name, err);
17429 			}
17430 			continue;
17431 		}
17432 
17433 		lifrp->lifr_groupname[0] = '\0';
17434 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17435 		if (err != 0) {
17436 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17437 			    "IPMP group (error %d); associated IPMP interface "
17438 			    "may not be shutdown", lifrp->lifr_name, err);
17439 			continue;
17440 		}
17441 	}
17442 
17443 	kmem_free(lifc.lifc_buf, bufsize);
17444 }
17445 
17446 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17447 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17448 
17449 /*
17450  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17451  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17452  * when the user-level processes in the zone are killed and the latter are
17453  * cleaned up by str_stack_shutdown().
17454  */
17455 void
17456 ip_interface_cleanup(ip_stack_t *ipst)
17457 {
17458 	ldi_handle_t	lh;
17459 	ldi_ident_t	li;
17460 	cred_t		*cr;
17461 	int		err;
17462 	int		i;
17463 	char		*devs[] = { UDP6DEV, UDPDEV };
17464 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17465 
17466 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17467 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17468 		    " error %d", err);
17469 		return;
17470 	}
17471 
17472 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17473 	ASSERT(cr != NULL);
17474 
17475 	/*
17476 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17477 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17478 	 * the loop.)
17479 	 */
17480 	for (i = 0; i < 2; i++) {
17481 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17482 		if (err != 0) {
17483 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17484 			    " error %d", devs[i], err);
17485 			continue;
17486 		}
17487 
17488 		ip_loopback_removeif(lh, i == 0, cr);
17489 		ip_ipmp_cleanup(lh, i == 0, cr);
17490 
17491 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17492 	}
17493 
17494 	ldi_ident_release(li);
17495 	crfree(cr);
17496 }
17497 
17498 /*
17499  * This needs to be in-sync with nic_event_t definition
17500  */
17501 static const char *
17502 ill_hook_event2str(nic_event_t event)
17503 {
17504 	switch (event) {
17505 	case NE_PLUMB:
17506 		return ("PLUMB");
17507 	case NE_UNPLUMB:
17508 		return ("UNPLUMB");
17509 	case NE_UP:
17510 		return ("UP");
17511 	case NE_DOWN:
17512 		return ("DOWN");
17513 	case NE_ADDRESS_CHANGE:
17514 		return ("ADDRESS_CHANGE");
17515 	case NE_LIF_UP:
17516 		return ("LIF_UP");
17517 	case NE_LIF_DOWN:
17518 		return ("LIF_DOWN");
17519 	case NE_IFINDEX_CHANGE:
17520 		return ("IFINDEX_CHANGE");
17521 	default:
17522 		return ("UNKNOWN");
17523 	}
17524 }
17525 
17526 void
17527 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17528     nic_event_data_t data, size_t datalen)
17529 {
17530 	ip_stack_t		*ipst = ill->ill_ipst;
17531 	hook_nic_event_int_t	*info;
17532 	const char		*str = NULL;
17533 
17534 	/* create a new nic event info */
17535 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17536 		goto fail;
17537 
17538 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17539 	info->hnei_event.hne_lif = lif;
17540 	info->hnei_event.hne_event = event;
17541 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17542 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17543 	info->hnei_event.hne_data = NULL;
17544 	info->hnei_event.hne_datalen = 0;
17545 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17546 
17547 	if (data != NULL && datalen != 0) {
17548 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17549 		if (info->hnei_event.hne_data == NULL)
17550 			goto fail;
17551 		bcopy(data, info->hnei_event.hne_data, datalen);
17552 		info->hnei_event.hne_datalen = datalen;
17553 	}
17554 
17555 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17556 	    DDI_NOSLEEP) == DDI_SUCCESS)
17557 		return;
17558 
17559 fail:
17560 	if (info != NULL) {
17561 		if (info->hnei_event.hne_data != NULL) {
17562 			kmem_free(info->hnei_event.hne_data,
17563 			    info->hnei_event.hne_datalen);
17564 		}
17565 		kmem_free(info, sizeof (hook_nic_event_t));
17566 	}
17567 	str = ill_hook_event2str(event);
17568 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17569 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17570 }
17571 
17572 static int
17573 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17574 {
17575 	int		err = 0;
17576 	const in_addr_t	*addr = NULL;
17577 	nce_t		*nce = NULL;
17578 	ill_t		*ill = ipif->ipif_ill;
17579 	ill_t		*bound_ill;
17580 	boolean_t	added_ipif = B_FALSE;
17581 	uint16_t	state;
17582 	uint16_t	flags;
17583 
17584 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17585 	    ill_t *, ill, ipif_t *, ipif);
17586 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17587 		addr = &ipif->ipif_lcl_addr;
17588 	}
17589 
17590 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17591 		if (res_act != Res_act_initial)
17592 			return (EINVAL);
17593 	}
17594 
17595 	if (addr != NULL) {
17596 		ipmp_illgrp_t	*illg = ill->ill_grp;
17597 
17598 		/* add unicast nce for the local addr */
17599 
17600 		if (IS_IPMP(ill)) {
17601 			/*
17602 			 * If we're here via ipif_up(), then the ipif
17603 			 * won't be bound yet -- add it to the group,
17604 			 * which will bind it if possible. (We would
17605 			 * add it in ipif_up(), but deleting on failure
17606 			 * there is gruesome.)  If we're here via
17607 			 * ipmp_ill_bind_ipif(), then the ipif has
17608 			 * already been added to the group and we
17609 			 * just need to use the binding.
17610 			 */
17611 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17612 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17613 				if (bound_ill == NULL) {
17614 					/*
17615 					 * We couldn't bind the ipif to an ill
17616 					 * yet, so we have nothing to publish.
17617 					 * Mark the address as ready and return.
17618 					 */
17619 					ipif->ipif_addr_ready = 1;
17620 					return (0);
17621 				}
17622 				added_ipif = B_TRUE;
17623 			}
17624 		} else {
17625 			bound_ill = ill;
17626 		}
17627 
17628 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17629 		    NCE_F_NONUD);
17630 		/*
17631 		 * If this is an initial bring-up (or the ipif was never
17632 		 * completely brought up), do DAD.  Otherwise, we're here
17633 		 * because IPMP has rebound an address to this ill: send
17634 		 * unsolicited advertisements (ARP announcements) to
17635 		 * inform others.
17636 		 */
17637 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17638 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17639 		} else {
17640 			state = ND_REACHABLE;
17641 			flags |= NCE_F_UNSOL_ADV;
17642 		}
17643 
17644 retry:
17645 		err = nce_lookup_then_add_v4(ill,
17646 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17647 		    addr, flags, state, &nce);
17648 
17649 		/*
17650 		 * note that we may encounter EEXIST if we are moving
17651 		 * the nce as a result of a rebind operation.
17652 		 */
17653 		switch (err) {
17654 		case 0:
17655 			ipif->ipif_added_nce = 1;
17656 			nce->nce_ipif_cnt++;
17657 			break;
17658 		case EEXIST:
17659 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17660 			    ill->ill_name));
17661 			if (!NCE_MYADDR(nce->nce_common)) {
17662 				/*
17663 				 * A leftover nce from before this address
17664 				 * existed
17665 				 */
17666 				ncec_delete(nce->nce_common);
17667 				nce_refrele(nce);
17668 				nce = NULL;
17669 				goto retry;
17670 			}
17671 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17672 				nce_refrele(nce);
17673 				nce = NULL;
17674 				ip1dbg(("ipif_arp_up: NCE already exists "
17675 				    "for %s:%u\n", ill->ill_name,
17676 				    ipif->ipif_id));
17677 				goto arp_up_done;
17678 			}
17679 			/*
17680 			 * Duplicate local addresses are permissible for
17681 			 * IPIF_POINTOPOINT interfaces which will get marked
17682 			 * IPIF_UNNUMBERED later in
17683 			 * ip_addr_availability_check().
17684 			 *
17685 			 * The nce_ipif_cnt field tracks the number of
17686 			 * ipifs that have nce_addr as their local address.
17687 			 */
17688 			ipif->ipif_addr_ready = 1;
17689 			ipif->ipif_added_nce = 1;
17690 			nce->nce_ipif_cnt++;
17691 			err = 0;
17692 			break;
17693 		default:
17694 			ASSERT(nce == NULL);
17695 			goto arp_up_done;
17696 		}
17697 		if (arp_no_defense) {
17698 			if ((ipif->ipif_flags & IPIF_UP) &&
17699 			    !ipif->ipif_addr_ready)
17700 				ipif_up_notify(ipif);
17701 			ipif->ipif_addr_ready = 1;
17702 		}
17703 	} else {
17704 		/* zero address. nothing to publish */
17705 		ipif->ipif_addr_ready = 1;
17706 	}
17707 	if (nce != NULL)
17708 		nce_refrele(nce);
17709 arp_up_done:
17710 	if (added_ipif && err != 0)
17711 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17712 	return (err);
17713 }
17714 
17715 int
17716 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17717 {
17718 	int 		err = 0;
17719 	ill_t 		*ill = ipif->ipif_ill;
17720 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17721 
17722 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17723 	    ill_t *, ill, ipif_t *, ipif);
17724 
17725 	/*
17726 	 * need to bring up ARP or setup mcast mapping only
17727 	 * when the first interface is coming UP.
17728 	 */
17729 	first_interface = (ill->ill_ipif_up_count == 0 &&
17730 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17731 
17732 	if (res_act == Res_act_initial && first_interface) {
17733 		/*
17734 		 * Send ATTACH + BIND
17735 		 */
17736 		err = arp_ll_up(ill);
17737 		if (err != EINPROGRESS && err != 0)
17738 			return (err);
17739 
17740 		/*
17741 		 * Add NCE for local address. Start DAD.
17742 		 * we'll wait to hear that DAD has finished
17743 		 * before using the interface.
17744 		 */
17745 		if (err == EINPROGRESS)
17746 			wait_for_dlpi = B_TRUE;
17747 	}
17748 
17749 	if (!wait_for_dlpi)
17750 		(void) ipif_arp_up_done_tail(ipif, res_act);
17751 
17752 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17753 }
17754 
17755 /*
17756  * Finish processing of "arp_up" after all the DLPI message
17757  * exchanges have completed between arp and the driver.
17758  */
17759 void
17760 arp_bringup_done(ill_t *ill, int err)
17761 {
17762 	mblk_t	*mp1;
17763 	ipif_t  *ipif;
17764 	conn_t *connp = NULL;
17765 	ipsq_t	*ipsq;
17766 	queue_t *q;
17767 
17768 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
17769 
17770 	ASSERT(IAM_WRITER_ILL(ill));
17771 
17772 	ipsq = ill->ill_phyint->phyint_ipsq;
17773 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17774 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17775 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17776 	if (mp1 == NULL) /* bringup was aborted by the user */
17777 		return;
17778 
17779 	/*
17780 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17781 	 * must have an associated conn_t.  Otherwise, we're bringing this
17782 	 * interface back up as part of handling an asynchronous event (e.g.,
17783 	 * physical address change).
17784 	 */
17785 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17786 		ASSERT(connp != NULL);
17787 		q = CONNP_TO_WQ(connp);
17788 	} else {
17789 		ASSERT(connp == NULL);
17790 		q = ill->ill_rq;
17791 	}
17792 	if (err == 0) {
17793 		if (ipif->ipif_isv6) {
17794 			if ((err = ipif_up_done_v6(ipif)) != 0)
17795 				ip0dbg(("arp_bringup_done: init failed\n"));
17796 		} else {
17797 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
17798 			if (err != 0 || (err = ipif_up_done(ipif)) != 0)
17799 				ip0dbg(("arp_bringup_done: init failed\n"));
17800 		}
17801 	} else {
17802 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
17803 	}
17804 
17805 	if ((err == 0) && (ill->ill_up_ipifs)) {
17806 		err = ill_up_ipifs(ill, q, mp1);
17807 		if (err == EINPROGRESS)
17808 			return;
17809 	}
17810 
17811 	/*
17812 	 * If we have a moved ipif to bring up, and everything has succeeded
17813 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
17814 	 * down -- the admin can try to bring it up by hand if need be.
17815 	 */
17816 	if (ill->ill_move_ipif != NULL) {
17817 		ipif = ill->ill_move_ipif;
17818 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
17819 		    ipif->ipif_ill->ill_name));
17820 		ill->ill_move_ipif = NULL;
17821 		if (err == 0) {
17822 			err = ipif_up(ipif, q, mp1);
17823 			if (err == EINPROGRESS)
17824 				return;
17825 		}
17826 	}
17827 
17828 	/*
17829 	 * The operation must complete without EINPROGRESS since
17830 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
17831 	 * Otherwise, the operation will be stuck forever in the ipsq.
17832 	 */
17833 	ASSERT(err != EINPROGRESS);
17834 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17835 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
17836 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
17837 		    ill_t *, ill, ipif_t *, ipif);
17838 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
17839 	} else {
17840 		ipsq_current_finish(ipsq);
17841 	}
17842 }
17843 
17844 /*
17845  * Finish processing of arp replumb after all the DLPI message
17846  * exchanges have completed between arp and the driver.
17847  */
17848 void
17849 arp_replumb_done(ill_t *ill, int err)
17850 {
17851 	mblk_t	*mp1;
17852 	ipif_t  *ipif;
17853 	conn_t *connp = NULL;
17854 	ipsq_t	*ipsq;
17855 	queue_t *q;
17856 
17857 	ASSERT(IAM_WRITER_ILL(ill));
17858 
17859 	ipsq = ill->ill_phyint->phyint_ipsq;
17860 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17861 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17862 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17863 	if (mp1 == NULL) {
17864 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
17865 		    ipsq->ipsq_xop->ipx_current_ioctl));
17866 		/* bringup was aborted by the user */
17867 		return;
17868 	}
17869 	/*
17870 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17871 	 * must have an associated conn_t.  Otherwise, we're bringing this
17872 	 * interface back up as part of handling an asynchronous event (e.g.,
17873 	 * physical address change).
17874 	 */
17875 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17876 		ASSERT(connp != NULL);
17877 		q = CONNP_TO_WQ(connp);
17878 	} else {
17879 		ASSERT(connp == NULL);
17880 		q = ill->ill_rq;
17881 	}
17882 	if ((err == 0) && (ill->ill_up_ipifs)) {
17883 		err = ill_up_ipifs(ill, q, mp1);
17884 		if (err == EINPROGRESS)
17885 			return;
17886 	}
17887 	/*
17888 	 * The operation must complete without EINPROGRESS since
17889 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
17890 	 * Otherwise, the operation will be stuck forever in the ipsq.
17891 	 */
17892 	ASSERT(err != EINPROGRESS);
17893 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17894 		DTRACE_PROBE4(ipif__ioctl, char *,
17895 		    "arp_replumb_done finish",
17896 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
17897 		    ill_t *, ill, ipif_t *, ipif);
17898 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
17899 	} else {
17900 		ipsq_current_finish(ipsq);
17901 	}
17902 }
17903 
17904 void
17905 ipif_up_notify(ipif_t *ipif)
17906 {
17907 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
17908 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
17909 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
17910 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
17911 	    NE_LIF_UP, NULL, 0);
17912 }
17913 
17914 /*
17915  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
17916  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
17917  * TPI end points with STREAMS modules pushed above.  This is assured by not
17918  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
17919  * never ends up on an ipsq, otherwise we may end up processing the ioctl
17920  * while unwinding from the ispq and that could be a thread from the bottom.
17921  */
17922 /* ARGSUSED */
17923 int
17924 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17925     ip_ioctl_cmd_t *ipip, void *arg)
17926 {
17927 	mblk_t *cmd_mp = mp->b_cont->b_cont;
17928 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
17929 	int ret = 0;
17930 	int i;
17931 	size_t size;
17932 	ip_stack_t *ipst;
17933 	zoneid_t zoneid;
17934 	ilb_stack_t *ilbs;
17935 
17936 	ipst = CONNQ_TO_IPST(q);
17937 	ilbs = ipst->ips_netstack->netstack_ilb;
17938 	zoneid = Q_TO_CONN(q)->conn_zoneid;
17939 
17940 	switch (command) {
17941 	case ILB_CREATE_RULE: {
17942 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
17943 
17944 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
17945 			ret = EINVAL;
17946 			break;
17947 		}
17948 
17949 		ret = ilb_rule_add(ilbs, zoneid, cmd);
17950 		break;
17951 	}
17952 	case ILB_DESTROY_RULE:
17953 	case ILB_ENABLE_RULE:
17954 	case ILB_DISABLE_RULE: {
17955 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
17956 
17957 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
17958 			ret = EINVAL;
17959 			break;
17960 		}
17961 
17962 		if (cmd->flags & ILB_RULE_ALLRULES) {
17963 			if (command == ILB_DESTROY_RULE) {
17964 				ilb_rule_del_all(ilbs, zoneid);
17965 				break;
17966 			} else if (command == ILB_ENABLE_RULE) {
17967 				ilb_rule_enable_all(ilbs, zoneid);
17968 				break;
17969 			} else if (command == ILB_DISABLE_RULE) {
17970 				ilb_rule_disable_all(ilbs, zoneid);
17971 				break;
17972 			}
17973 		} else {
17974 			if (command == ILB_DESTROY_RULE) {
17975 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
17976 			} else if (command == ILB_ENABLE_RULE) {
17977 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
17978 				    NULL);
17979 			} else if (command == ILB_DISABLE_RULE) {
17980 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
17981 				    NULL);
17982 			}
17983 		}
17984 		break;
17985 	}
17986 	case ILB_NUM_RULES: {
17987 		ilb_num_rules_cmd_t *cmd;
17988 
17989 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
17990 			ret = EINVAL;
17991 			break;
17992 		}
17993 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
17994 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
17995 		break;
17996 	}
17997 	case ILB_RULE_NAMES: {
17998 		ilb_rule_names_cmd_t *cmd;
17999 
18000 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18001 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18002 		    cmd->num_names == 0) {
18003 			ret = EINVAL;
18004 			break;
18005 		}
18006 		size = cmd->num_names * ILB_RULE_NAMESZ;
18007 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18008 		    size != cmd_mp->b_wptr) {
18009 			ret = EINVAL;
18010 			break;
18011 		}
18012 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18013 		break;
18014 	}
18015 	case ILB_NUM_SERVERS: {
18016 		ilb_num_servers_cmd_t *cmd;
18017 
18018 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18019 			ret = EINVAL;
18020 			break;
18021 		}
18022 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18023 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18024 		    &(cmd->num));
18025 		break;
18026 	}
18027 	case ILB_LIST_RULE: {
18028 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18029 
18030 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18031 			ret = EINVAL;
18032 			break;
18033 		}
18034 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18035 		break;
18036 	}
18037 	case ILB_LIST_SERVERS: {
18038 		ilb_servers_info_cmd_t *cmd;
18039 
18040 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18041 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18042 		    cmd->num_servers == 0) {
18043 			ret = EINVAL;
18044 			break;
18045 		}
18046 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18047 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18048 		    size != cmd_mp->b_wptr) {
18049 			ret = EINVAL;
18050 			break;
18051 		}
18052 
18053 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18054 		    &cmd->num_servers);
18055 		break;
18056 	}
18057 	case ILB_ADD_SERVERS: {
18058 		ilb_servers_info_cmd_t *cmd;
18059 		ilb_rule_t *rule;
18060 
18061 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18062 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18063 			ret = EINVAL;
18064 			break;
18065 		}
18066 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18067 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18068 		    size != cmd_mp->b_wptr) {
18069 			ret = EINVAL;
18070 			break;
18071 		}
18072 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18073 		if (rule == NULL) {
18074 			ASSERT(ret != 0);
18075 			break;
18076 		}
18077 		for (i = 0; i < cmd->num_servers; i++) {
18078 			ilb_server_info_t *s;
18079 
18080 			s = &cmd->servers[i];
18081 			s->err = ilb_server_add(ilbs, rule, s);
18082 		}
18083 		ILB_RULE_REFRELE(rule);
18084 		break;
18085 	}
18086 	case ILB_DEL_SERVERS:
18087 	case ILB_ENABLE_SERVERS:
18088 	case ILB_DISABLE_SERVERS: {
18089 		ilb_servers_cmd_t *cmd;
18090 		ilb_rule_t *rule;
18091 		int (*f)();
18092 
18093 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18094 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18095 			ret = EINVAL;
18096 			break;
18097 		}
18098 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18099 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18100 		    size != cmd_mp->b_wptr) {
18101 			ret = EINVAL;
18102 			break;
18103 		}
18104 
18105 		if (command == ILB_DEL_SERVERS)
18106 			f = ilb_server_del;
18107 		else if (command == ILB_ENABLE_SERVERS)
18108 			f = ilb_server_enable;
18109 		else if (command == ILB_DISABLE_SERVERS)
18110 			f = ilb_server_disable;
18111 
18112 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18113 		if (rule == NULL) {
18114 			ASSERT(ret != 0);
18115 			break;
18116 		}
18117 
18118 		for (i = 0; i < cmd->num_servers; i++) {
18119 			ilb_server_arg_t *s;
18120 
18121 			s = &cmd->servers[i];
18122 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18123 		}
18124 		ILB_RULE_REFRELE(rule);
18125 		break;
18126 	}
18127 	case ILB_LIST_NAT_TABLE: {
18128 		ilb_list_nat_cmd_t *cmd;
18129 
18130 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18131 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18132 			ret = EINVAL;
18133 			break;
18134 		}
18135 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18136 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18137 		    size != cmd_mp->b_wptr) {
18138 			ret = EINVAL;
18139 			break;
18140 		}
18141 
18142 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18143 		    &cmd->flags);
18144 		break;
18145 	}
18146 	case ILB_LIST_STICKY_TABLE: {
18147 		ilb_list_sticky_cmd_t *cmd;
18148 
18149 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18150 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18151 			ret = EINVAL;
18152 			break;
18153 		}
18154 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18155 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18156 		    size != cmd_mp->b_wptr) {
18157 			ret = EINVAL;
18158 			break;
18159 		}
18160 
18161 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18162 		    &cmd->num_sticky, &cmd->flags);
18163 		break;
18164 	}
18165 	default:
18166 		ret = EINVAL;
18167 		break;
18168 	}
18169 done:
18170 	return (ret);
18171 }
18172 
18173 /* Remove all cache entries for this logical interface */
18174 void
18175 ipif_nce_down(ipif_t *ipif)
18176 {
18177 	ill_t *ill = ipif->ipif_ill;
18178 	nce_t *nce;
18179 
18180 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18181 	    ill_t *, ill, ipif_t *, ipif);
18182 	if (ipif->ipif_added_nce) {
18183 		if (ipif->ipif_isv6)
18184 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18185 		else
18186 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18187 		if (nce != NULL) {
18188 			if (--nce->nce_ipif_cnt == 0)
18189 				ncec_delete(nce->nce_common);
18190 			ipif->ipif_added_nce = 0;
18191 			nce_refrele(nce);
18192 		} else {
18193 			/*
18194 			 * nce may already be NULL because it was already
18195 			 * flushed, e.g., due to a call to nce_flush
18196 			 */
18197 			ipif->ipif_added_nce = 0;
18198 		}
18199 	}
18200 	/*
18201 	 * Make IPMP aware of the deleted data address.
18202 	 */
18203 	if (IS_IPMP(ill))
18204 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18205 
18206 	/*
18207 	 * Remove all other nces dependent on this ill when the last ipif
18208 	 * is going away.
18209 	 */
18210 	if (ill->ill_ipif_up_count == 0) {
18211 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18212 		    (uchar_t *)ill, ill->ill_ipst);
18213 		if (IS_UNDER_IPMP(ill))
18214 			nce_flush(ill, B_TRUE);
18215 	}
18216 }
18217