xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 2dea4eed7ad1c66ae4770263aa2911815a8b86eb)
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 2010 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, int *errorp);
148 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
149 static void	ipif_delete_bcast_ires(ipif_t *ipif);
150 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
151 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
152 		    boolean_t isv6);
153 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
154 static void	ipif_free(ipif_t *ipif);
155 static void	ipif_free_tail(ipif_t *ipif);
156 static void	ipif_set_default(ipif_t *ipif);
157 static int	ipif_set_values(queue_t *q, mblk_t *mp,
158     char *interf_name, uint_t *ppa);
159 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
160     queue_t *q);
161 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
162     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
163     ip_stack_t *);
164 
165 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
166 static void	ill_delete_interface_type(ill_if_t *);
167 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
168 static void	ill_dl_down(ill_t *ill);
169 static void	ill_down(ill_t *ill);
170 static void	ill_down_ipifs(ill_t *, boolean_t);
171 static void	ill_free_mib(ill_t *ill);
172 static void	ill_glist_delete(ill_t *);
173 static void	ill_phyint_reinit(ill_t *ill);
174 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
175 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
177 
178 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
179 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
180 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
181 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
182 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
183 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
184 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
185 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
186 static ip_v4mapinfo_func_t ip_mbcast_mapping;
187 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
188 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
189 static void	phyint_free(phyint_t *);
190 
191 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
194 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
196 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
197     dl_capability_sub_t *);
198 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
199 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
200 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
201 		    dl_capability_sub_t *);
202 static void	ill_capability_dld_enable(ill_t *);
203 static void	ill_capability_ack_thr(void *);
204 static void	ill_capability_lso_enable(ill_t *);
205 
206 static ill_t	*ill_prev_usesrc(ill_t *);
207 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
208 static void	ill_disband_usesrc_group(ill_t *);
209 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
210 
211 #ifdef DEBUG
212 static	void	ill_trace_cleanup(const ill_t *);
213 static	void	ipif_trace_cleanup(const ipif_t *);
214 #endif
215 
216 static	void	ill_dlpi_clear_deferred(ill_t *ill);
217 
218 /*
219  * if we go over the memory footprint limit more than once in this msec
220  * interval, we'll start pruning aggressively.
221  */
222 int ip_min_frag_prune_time = 0;
223 
224 static ipft_t	ip_ioctl_ftbl[] = {
225 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
226 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
227 		IPFT_F_NO_REPLY },
228 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
229 	{ 0 }
230 };
231 
232 /* Simple ICMP IP Header Template */
233 static ipha_t icmp_ipha = {
234 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
235 };
236 
237 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
238 
239 static ip_m_t   ip_m_tbl[] = {
240 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
241 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
242 	    ip_nodef_v6intfid },
243 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
259 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
260 	    ip_ipv4_v6destintfid },
261 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
263 	    ip_ipv6_v6destintfid },
264 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_nodef_v6intfid },
267 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
269 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
271 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
272 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
273 	    ip_nodef_v6intfid }
274 };
275 
276 static ill_t	ill_null;		/* Empty ILL for init. */
277 char	ipif_loopback_name[] = "lo0";
278 static char *ipv4_forward_suffix = ":ip_forwarding";
279 static char *ipv6_forward_suffix = ":ip6_forwarding";
280 static	sin6_t	sin6_null;	/* Zero address for quick clears */
281 static	sin_t	sin_null;	/* Zero address for quick clears */
282 
283 /* When set search for unused ipif_seqid */
284 static ipif_t	ipif_zero;
285 
286 /*
287  * ppa arena is created after these many
288  * interfaces have been plumbed.
289  */
290 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
291 
292 /*
293  * Allocate per-interface mibs.
294  * Returns true if ok. False otherwise.
295  *  ipsq  may not yet be allocated (loopback case ).
296  */
297 static boolean_t
298 ill_allocate_mibs(ill_t *ill)
299 {
300 	/* Already allocated? */
301 	if (ill->ill_ip_mib != NULL) {
302 		if (ill->ill_isv6)
303 			ASSERT(ill->ill_icmp6_mib != NULL);
304 		return (B_TRUE);
305 	}
306 
307 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
308 	    KM_NOSLEEP);
309 	if (ill->ill_ip_mib == NULL) {
310 		return (B_FALSE);
311 	}
312 
313 	/* Setup static information */
314 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
315 	    sizeof (mib2_ipIfStatsEntry_t));
316 	if (ill->ill_isv6) {
317 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
318 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
319 		    sizeof (mib2_ipv6AddrEntry_t));
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
321 		    sizeof (mib2_ipv6RouteEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
323 		    sizeof (mib2_ipv6NetToMediaEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
325 		    sizeof (ipv6_member_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
327 		    sizeof (ipv6_grpsrc_t));
328 	} else {
329 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
331 		    sizeof (mib2_ipAddrEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
333 		    sizeof (mib2_ipRouteEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
335 		    sizeof (mib2_ipNetToMediaEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
337 		    sizeof (ip_member_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
339 		    sizeof (ip_grpsrc_t));
340 
341 		/*
342 		 * For a v4 ill, we are done at this point, because per ill
343 		 * icmp mibs are only used for v6.
344 		 */
345 		return (B_TRUE);
346 	}
347 
348 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
349 	    KM_NOSLEEP);
350 	if (ill->ill_icmp6_mib == NULL) {
351 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
352 		ill->ill_ip_mib = NULL;
353 		return (B_FALSE);
354 	}
355 	/* static icmp info */
356 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
357 	    sizeof (mib2_ipv6IfIcmpEntry_t);
358 	/*
359 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
360 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
361 	 * -> ill_phyint_reinit
362 	 */
363 	return (B_TRUE);
364 }
365 
366 /*
367  * Completely vaporize a lower level tap and all associated interfaces.
368  * ill_delete is called only out of ip_close when the device control
369  * stream is being closed.
370  */
371 void
372 ill_delete(ill_t *ill)
373 {
374 	ipif_t	*ipif;
375 	ill_t	*prev_ill;
376 	ip_stack_t	*ipst = ill->ill_ipst;
377 
378 	/*
379 	 * ill_delete may be forcibly entering the ipsq. The previous
380 	 * ioctl may not have completed and may need to be aborted.
381 	 * ipsq_flush takes care of it. If we don't need to enter the
382 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
383 	 * ill_delete_tail is sufficient.
384 	 */
385 	ipsq_flush(ill);
386 
387 	/*
388 	 * Nuke all interfaces.  ipif_free will take down the interface,
389 	 * remove it from the list, and free the data structure.
390 	 * Walk down the ipif list and remove the logical interfaces
391 	 * first before removing the main ipif. We can't unplumb
392 	 * zeroth interface first in the case of IPv6 as update_conn_ill
393 	 * -> ip_ll_multireq de-references ill_ipif for checking
394 	 * POINTOPOINT.
395 	 *
396 	 * If ill_ipif was not properly initialized (i.e low on memory),
397 	 * then no interfaces to clean up. In this case just clean up the
398 	 * ill.
399 	 */
400 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
401 		ipif_free(ipif);
402 
403 	/*
404 	 * clean out all the nce_t entries that depend on this
405 	 * ill for the ill_phys_addr.
406 	 */
407 	nce_flush(ill, B_TRUE);
408 
409 	/* Clean up msgs on pending upcalls for mrouted */
410 	reset_mrt_ill(ill);
411 
412 	update_conn_ill(ill, ipst);
413 
414 	/*
415 	 * Remove multicast references added as a result of calls to
416 	 * ip_join_allmulti().
417 	 */
418 	ip_purge_allmulti(ill);
419 
420 	/*
421 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
422 	 */
423 	if (IS_UNDER_IPMP(ill))
424 		ipmp_ill_leave_illgrp(ill);
425 
426 	/*
427 	 * ill_down will arrange to blow off any IRE's dependent on this
428 	 * ILL, and shut down fragmentation reassembly.
429 	 */
430 	ill_down(ill);
431 
432 	/* Let SCTP know, so that it can remove this from its list. */
433 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
434 
435 	/*
436 	 * Walk all CONNs that can have a reference on an ire or nce for this
437 	 * ill (we actually walk all that now have stale references).
438 	 */
439 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
440 
441 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
442 	if (ill->ill_isv6)
443 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
444 
445 	/*
446 	 * If an address on this ILL is being used as a source address then
447 	 * clear out the pointers in other ILLs that point to this ILL.
448 	 */
449 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
450 	if (ill->ill_usesrc_grp_next != NULL) {
451 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
452 			ill_disband_usesrc_group(ill);
453 		} else {	/* consumer of the usesrc ILL */
454 			prev_ill = ill_prev_usesrc(ill);
455 			prev_ill->ill_usesrc_grp_next =
456 			    ill->ill_usesrc_grp_next;
457 		}
458 	}
459 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
460 }
461 
462 static void
463 ipif_non_duplicate(ipif_t *ipif)
464 {
465 	ill_t *ill = ipif->ipif_ill;
466 	mutex_enter(&ill->ill_lock);
467 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
468 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
469 		ASSERT(ill->ill_ipif_dup_count > 0);
470 		ill->ill_ipif_dup_count--;
471 	}
472 	mutex_exit(&ill->ill_lock);
473 }
474 
475 /*
476  * ill_delete_tail is called from ip_modclose after all references
477  * to the closing ill are gone. The wait is done in ip_modclose
478  */
479 void
480 ill_delete_tail(ill_t *ill)
481 {
482 	mblk_t	**mpp;
483 	ipif_t	*ipif;
484 	ip_stack_t	*ipst = ill->ill_ipst;
485 
486 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
487 		ipif_non_duplicate(ipif);
488 		(void) ipif_down_tail(ipif);
489 	}
490 
491 	ASSERT(ill->ill_ipif_dup_count == 0);
492 
493 	/*
494 	 * If polling capability is enabled (which signifies direct
495 	 * upcall into IP and driver has ill saved as a handle),
496 	 * we need to make sure that unbind has completed before we
497 	 * let the ill disappear and driver no longer has any reference
498 	 * to this ill.
499 	 */
500 	mutex_enter(&ill->ill_lock);
501 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
502 		cv_wait(&ill->ill_cv, &ill->ill_lock);
503 	mutex_exit(&ill->ill_lock);
504 	ASSERT(!(ill->ill_capabilities &
505 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
506 
507 	if (ill->ill_net_type != IRE_LOOPBACK)
508 		qprocsoff(ill->ill_rq);
509 
510 	/*
511 	 * We do an ipsq_flush once again now. New messages could have
512 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
513 	 * could also have landed up if an ioctl thread had looked up
514 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
515 	 * enqueued the ioctl when we did the ipsq_flush last time.
516 	 */
517 	ipsq_flush(ill);
518 
519 	/*
520 	 * Free capabilities.
521 	 */
522 	if (ill->ill_hcksum_capab != NULL) {
523 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
524 		ill->ill_hcksum_capab = NULL;
525 	}
526 
527 	if (ill->ill_zerocopy_capab != NULL) {
528 		kmem_free(ill->ill_zerocopy_capab,
529 		    sizeof (ill_zerocopy_capab_t));
530 		ill->ill_zerocopy_capab = NULL;
531 	}
532 
533 	if (ill->ill_lso_capab != NULL) {
534 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
535 		ill->ill_lso_capab = NULL;
536 	}
537 
538 	if (ill->ill_dld_capab != NULL) {
539 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
540 		ill->ill_dld_capab = NULL;
541 	}
542 
543 	while (ill->ill_ipif != NULL)
544 		ipif_free_tail(ill->ill_ipif);
545 
546 	/*
547 	 * We have removed all references to ilm from conn and the ones joined
548 	 * within the kernel.
549 	 *
550 	 * We don't walk conns, mrts and ires because
551 	 *
552 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
553 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
554 	 *    ill references.
555 	 */
556 
557 	/*
558 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
559 	 * is safe to do because the illgrp has already been unlinked from the
560 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
561 	 */
562 	if (IS_IPMP(ill)) {
563 		ipmp_illgrp_destroy(ill->ill_grp);
564 		ill->ill_grp = NULL;
565 	}
566 
567 	/*
568 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
569 	 * could free the phyint. No more reference to the phyint after this
570 	 * point.
571 	 */
572 	(void) ill_glist_delete(ill);
573 
574 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
575 	if (ill->ill_ndd_name != NULL)
576 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
577 	rw_exit(&ipst->ips_ip_g_nd_lock);
578 
579 	if (ill->ill_frag_ptr != NULL) {
580 		uint_t count;
581 
582 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
583 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
584 		}
585 		mi_free(ill->ill_frag_ptr);
586 		ill->ill_frag_ptr = NULL;
587 		ill->ill_frag_hash_tbl = NULL;
588 	}
589 
590 	freemsg(ill->ill_nd_lla_mp);
591 	/* Free all retained control messages. */
592 	mpp = &ill->ill_first_mp_to_free;
593 	do {
594 		while (mpp[0]) {
595 			mblk_t  *mp;
596 			mblk_t  *mp1;
597 
598 			mp = mpp[0];
599 			mpp[0] = mp->b_next;
600 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
601 				mp1->b_next = NULL;
602 				mp1->b_prev = NULL;
603 			}
604 			freemsg(mp);
605 		}
606 	} while (mpp++ != &ill->ill_last_mp_to_free);
607 
608 	ill_free_mib(ill);
609 
610 #ifdef DEBUG
611 	ill_trace_cleanup(ill);
612 #endif
613 
614 	/* The default multicast interface might have changed */
615 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
616 
617 	/* Drop refcnt here */
618 	netstack_rele(ill->ill_ipst->ips_netstack);
619 	ill->ill_ipst = NULL;
620 }
621 
622 static void
623 ill_free_mib(ill_t *ill)
624 {
625 	ip_stack_t *ipst = ill->ill_ipst;
626 
627 	/*
628 	 * MIB statistics must not be lost, so when an interface
629 	 * goes away the counter values will be added to the global
630 	 * MIBs.
631 	 */
632 	if (ill->ill_ip_mib != NULL) {
633 		if (ill->ill_isv6) {
634 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
635 			    ill->ill_ip_mib);
636 		} else {
637 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
638 			    ill->ill_ip_mib);
639 		}
640 
641 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
642 		ill->ill_ip_mib = NULL;
643 	}
644 	if (ill->ill_icmp6_mib != NULL) {
645 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
646 		    ill->ill_icmp6_mib);
647 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
648 		ill->ill_icmp6_mib = NULL;
649 	}
650 }
651 
652 /*
653  * Concatenate together a physical address and a sap.
654  *
655  * Sap_lengths are interpreted as follows:
656  *   sap_length == 0	==>	no sap
657  *   sap_length > 0	==>	sap is at the head of the dlpi address
658  *   sap_length < 0	==>	sap is at the tail of the dlpi address
659  */
660 static void
661 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
662     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
663 {
664 	uint16_t sap_addr = (uint16_t)sap_src;
665 
666 	if (sap_length == 0) {
667 		if (phys_src == NULL)
668 			bzero(dst, phys_length);
669 		else
670 			bcopy(phys_src, dst, phys_length);
671 	} else if (sap_length < 0) {
672 		if (phys_src == NULL)
673 			bzero(dst, phys_length);
674 		else
675 			bcopy(phys_src, dst, phys_length);
676 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
677 	} else {
678 		bcopy(&sap_addr, dst, sizeof (sap_addr));
679 		if (phys_src == NULL)
680 			bzero((char *)dst + sap_length, phys_length);
681 		else
682 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
683 	}
684 }
685 
686 /*
687  * Generate a dl_unitdata_req mblk for the device and address given.
688  * addr_length is the length of the physical portion of the address.
689  * If addr is NULL include an all zero address of the specified length.
690  * TRUE? In any case, addr_length is taken to be the entire length of the
691  * dlpi address, including the absolute value of sap_length.
692  */
693 mblk_t *
694 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
695 		t_scalar_t sap_length)
696 {
697 	dl_unitdata_req_t *dlur;
698 	mblk_t	*mp;
699 	t_scalar_t	abs_sap_length;		/* absolute value */
700 
701 	abs_sap_length = ABS(sap_length);
702 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
703 	    DL_UNITDATA_REQ);
704 	if (mp == NULL)
705 		return (NULL);
706 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
707 	/* HACK: accomodate incompatible DLPI drivers */
708 	if (addr_length == 8)
709 		addr_length = 6;
710 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
711 	dlur->dl_dest_addr_offset = sizeof (*dlur);
712 	dlur->dl_priority.dl_min = 0;
713 	dlur->dl_priority.dl_max = 0;
714 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
715 	    (uchar_t *)&dlur[1]);
716 	return (mp);
717 }
718 
719 /*
720  * Add the pending mp to the list. There can be only 1 pending mp
721  * in the list. Any exclusive ioctl that needs to wait for a response
722  * from another module or driver needs to use this function to set
723  * the ipx_pending_mp to the ioctl mblk and wait for the response from
724  * the other module/driver. This is also used while waiting for the
725  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
726  */
727 boolean_t
728 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
729     int waitfor)
730 {
731 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
732 
733 	ASSERT(IAM_WRITER_IPIF(ipif));
734 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
735 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
736 	ASSERT(ipx->ipx_pending_mp == NULL);
737 	/*
738 	 * The caller may be using a different ipif than the one passed into
739 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
740 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
741 	 * that `ipx_current_ipif == ipif'.
742 	 */
743 	ASSERT(ipx->ipx_current_ipif != NULL);
744 
745 	/*
746 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
747 	 * driver.
748 	 */
749 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
750 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
751 	    (DB_TYPE(add_mp) == M_PCPROTO));
752 
753 	if (connp != NULL) {
754 		ASSERT(MUTEX_HELD(&connp->conn_lock));
755 		/*
756 		 * Return error if the conn has started closing. The conn
757 		 * could have finished cleaning up the pending mp list,
758 		 * If so we should not add another mp to the list negating
759 		 * the cleanup.
760 		 */
761 		if (connp->conn_state_flags & CONN_CLOSING)
762 			return (B_FALSE);
763 	}
764 	mutex_enter(&ipx->ipx_lock);
765 	ipx->ipx_pending_ipif = ipif;
766 	/*
767 	 * Note down the queue in b_queue. This will be returned by
768 	 * ipsq_pending_mp_get. Caller will then use these values to restart
769 	 * the processing
770 	 */
771 	add_mp->b_next = NULL;
772 	add_mp->b_queue = q;
773 	ipx->ipx_pending_mp = add_mp;
774 	ipx->ipx_waitfor = waitfor;
775 	mutex_exit(&ipx->ipx_lock);
776 
777 	if (connp != NULL)
778 		connp->conn_oper_pending_ill = ipif->ipif_ill;
779 
780 	return (B_TRUE);
781 }
782 
783 /*
784  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
785  * queued in the list.
786  */
787 mblk_t *
788 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
789 {
790 	mblk_t	*curr = NULL;
791 	ipxop_t	*ipx = ipsq->ipsq_xop;
792 
793 	*connpp = NULL;
794 	mutex_enter(&ipx->ipx_lock);
795 	if (ipx->ipx_pending_mp == NULL) {
796 		mutex_exit(&ipx->ipx_lock);
797 		return (NULL);
798 	}
799 
800 	/* There can be only 1 such excl message */
801 	curr = ipx->ipx_pending_mp;
802 	ASSERT(curr->b_next == NULL);
803 	ipx->ipx_pending_ipif = NULL;
804 	ipx->ipx_pending_mp = NULL;
805 	ipx->ipx_waitfor = 0;
806 	mutex_exit(&ipx->ipx_lock);
807 
808 	if (CONN_Q(curr->b_queue)) {
809 		/*
810 		 * This mp did a refhold on the conn, at the start of the ioctl.
811 		 * So we can safely return a pointer to the conn to the caller.
812 		 */
813 		*connpp = Q_TO_CONN(curr->b_queue);
814 	} else {
815 		*connpp = NULL;
816 	}
817 	curr->b_next = NULL;
818 	curr->b_prev = NULL;
819 	return (curr);
820 }
821 
822 /*
823  * Cleanup the ioctl mp queued in ipx_pending_mp
824  * - Called in the ill_delete path
825  * - Called in the M_ERROR or M_HANGUP path on the ill.
826  * - Called in the conn close path.
827  *
828  * Returns success on finding the pending mblk associated with the ioctl or
829  * exclusive operation in progress, failure otherwise.
830  */
831 boolean_t
832 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
833 {
834 	mblk_t	*mp;
835 	ipxop_t	*ipx;
836 	queue_t	*q;
837 	ipif_t	*ipif;
838 	int	cmd;
839 
840 	ASSERT(IAM_WRITER_ILL(ill));
841 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
842 
843 	mutex_enter(&ipx->ipx_lock);
844 	mp = ipx->ipx_pending_mp;
845 	if (connp != NULL) {
846 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
847 			/*
848 			 * Nothing to clean since the conn that is closing
849 			 * does not have a matching pending mblk in
850 			 * ipx_pending_mp.
851 			 */
852 			mutex_exit(&ipx->ipx_lock);
853 			return (B_FALSE);
854 		}
855 	} else {
856 		/*
857 		 * A non-zero ill_error signifies we are called in the
858 		 * M_ERROR or M_HANGUP path and we need to unconditionally
859 		 * abort any current ioctl and do the corresponding cleanup.
860 		 * A zero ill_error means we are in the ill_delete path and
861 		 * we do the cleanup only if there is a pending mp.
862 		 */
863 		if (mp == NULL && ill->ill_error == 0) {
864 			mutex_exit(&ipx->ipx_lock);
865 			return (B_FALSE);
866 		}
867 	}
868 
869 	/* Now remove from the ipx_pending_mp */
870 	ipx->ipx_pending_mp = NULL;
871 	ipif = ipx->ipx_pending_ipif;
872 	ipx->ipx_pending_ipif = NULL;
873 	ipx->ipx_waitfor = 0;
874 	ipx->ipx_current_ipif = NULL;
875 	cmd = ipx->ipx_current_ioctl;
876 	ipx->ipx_current_ioctl = 0;
877 	ipx->ipx_current_done = B_TRUE;
878 	mutex_exit(&ipx->ipx_lock);
879 
880 	if (mp == NULL)
881 		return (B_FALSE);
882 
883 	q = mp->b_queue;
884 	mp->b_next = NULL;
885 	mp->b_prev = NULL;
886 	mp->b_queue = NULL;
887 
888 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
889 		DTRACE_PROBE4(ipif__ioctl,
890 		    char *, "ipsq_pending_mp_cleanup",
891 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
892 		    ipif_t *, ipif);
893 		if (connp == NULL) {
894 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
895 		} else {
896 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
897 			mutex_enter(&ipif->ipif_ill->ill_lock);
898 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
899 			mutex_exit(&ipif->ipif_ill->ill_lock);
900 		}
901 	} else {
902 		inet_freemsg(mp);
903 	}
904 	return (B_TRUE);
905 }
906 
907 /*
908  * Called in the conn close path and ill delete path
909  */
910 static void
911 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
912 {
913 	ipsq_t	*ipsq;
914 	mblk_t	*prev;
915 	mblk_t	*curr;
916 	mblk_t	*next;
917 	queue_t	*wq, *rq = NULL;
918 	mblk_t	*tmp_list = NULL;
919 
920 	ASSERT(IAM_WRITER_ILL(ill));
921 	if (connp != NULL)
922 		wq = CONNP_TO_WQ(connp);
923 	else
924 		wq = ill->ill_wq;
925 
926 	/*
927 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
928 	 * against this here.
929 	 */
930 	if (wq != NULL)
931 		rq = RD(wq);
932 
933 	ipsq = ill->ill_phyint->phyint_ipsq;
934 	/*
935 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
936 	 * In the case of ioctl from a conn, there can be only 1 mp
937 	 * queued on the ipsq. If an ill is being unplumbed, only messages
938 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
939 	 * ioctls meant for this ill form conn's are not flushed. They will
940 	 * be processed during ipsq_exit and will not find the ill and will
941 	 * return error.
942 	 */
943 	mutex_enter(&ipsq->ipsq_lock);
944 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
945 	    curr = next) {
946 		next = curr->b_next;
947 		if (curr->b_queue == wq || curr->b_queue == rq) {
948 			/* Unlink the mblk from the pending mp list */
949 			if (prev != NULL) {
950 				prev->b_next = curr->b_next;
951 			} else {
952 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
953 				ipsq->ipsq_xopq_mphead = curr->b_next;
954 			}
955 			if (ipsq->ipsq_xopq_mptail == curr)
956 				ipsq->ipsq_xopq_mptail = prev;
957 			/*
958 			 * Create a temporary list and release the ipsq lock
959 			 * New elements are added to the head of the tmp_list
960 			 */
961 			curr->b_next = tmp_list;
962 			tmp_list = curr;
963 		} else {
964 			prev = curr;
965 		}
966 	}
967 	mutex_exit(&ipsq->ipsq_lock);
968 
969 	while (tmp_list != NULL) {
970 		curr = tmp_list;
971 		tmp_list = curr->b_next;
972 		curr->b_next = NULL;
973 		curr->b_prev = NULL;
974 		curr->b_queue = NULL;
975 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
976 			DTRACE_PROBE4(ipif__ioctl,
977 			    char *, "ipsq_xopq_mp_cleanup",
978 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
979 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
980 			    CONN_CLOSE : NO_COPYOUT, NULL);
981 		} else {
982 			/*
983 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
984 			 * this can't be just inet_freemsg. we have to
985 			 * restart it otherwise the thread will be stuck.
986 			 */
987 			inet_freemsg(curr);
988 		}
989 	}
990 }
991 
992 /*
993  * This conn has started closing. Cleanup any pending ioctl from this conn.
994  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
995  */
996 void
997 conn_ioctl_cleanup(conn_t *connp)
998 {
999 	ipsq_t	*ipsq;
1000 	ill_t	*ill;
1001 	boolean_t refheld;
1002 
1003 	/*
1004 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1005 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1006 	 * started the mp could be present in ipx_pending_mp. Note that if
1007 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1008 	 * not yet queued anywhere. In this case, the conn close code will wait
1009 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1010 	 * tcp_close will wait first until all ioctls have completed for this
1011 	 * conn.
1012 	 */
1013 	mutex_enter(&connp->conn_lock);
1014 	ill = connp->conn_oper_pending_ill;
1015 	if (ill == NULL) {
1016 		mutex_exit(&connp->conn_lock);
1017 		return;
1018 	}
1019 
1020 	/*
1021 	 * We may not be able to refhold the ill if the ill/ipif
1022 	 * is changing. But we need to make sure that the ill will
1023 	 * not vanish. So we just bump up the ill_waiter count.
1024 	 */
1025 	refheld = ill_waiter_inc(ill);
1026 	mutex_exit(&connp->conn_lock);
1027 	if (refheld) {
1028 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1029 			ill_waiter_dcr(ill);
1030 			/*
1031 			 * Check whether this ioctl has started and is
1032 			 * pending. If it is not found there then check
1033 			 * whether this ioctl has not even started and is in
1034 			 * the ipsq_xopq list.
1035 			 */
1036 			if (!ipsq_pending_mp_cleanup(ill, connp))
1037 				ipsq_xopq_mp_cleanup(ill, connp);
1038 			ipsq = ill->ill_phyint->phyint_ipsq;
1039 			ipsq_exit(ipsq);
1040 			return;
1041 		}
1042 	}
1043 
1044 	/*
1045 	 * The ill is also closing and we could not bump up the
1046 	 * ill_waiter_count or we could not enter the ipsq. Leave
1047 	 * the cleanup to ill_delete
1048 	 */
1049 	mutex_enter(&connp->conn_lock);
1050 	while (connp->conn_oper_pending_ill != NULL)
1051 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1052 	mutex_exit(&connp->conn_lock);
1053 	if (refheld)
1054 		ill_waiter_dcr(ill);
1055 }
1056 
1057 /*
1058  * ipcl_walk function for cleaning up conn_*_ill fields.
1059  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1060  * conn_bound_if in place. We prefer dropping
1061  * packets instead of sending them out the wrong interface, or accepting
1062  * packets from the wrong ifindex.
1063  */
1064 static void
1065 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1066 {
1067 	ill_t	*ill = (ill_t *)arg;
1068 
1069 	mutex_enter(&connp->conn_lock);
1070 	if (connp->conn_dhcpinit_ill == ill) {
1071 		connp->conn_dhcpinit_ill = NULL;
1072 		ASSERT(ill->ill_dhcpinit != 0);
1073 		atomic_dec_32(&ill->ill_dhcpinit);
1074 		ill_set_inputfn(ill);
1075 	}
1076 	mutex_exit(&connp->conn_lock);
1077 }
1078 
1079 static int
1080 ill_down_ipifs_tail(ill_t *ill)
1081 {
1082 	ipif_t	*ipif;
1083 	int err;
1084 
1085 	ASSERT(IAM_WRITER_ILL(ill));
1086 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1087 		ipif_non_duplicate(ipif);
1088 		/*
1089 		 * ipif_down_tail will call arp_ll_down on the last ipif
1090 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1091 		 */
1092 		if ((err = ipif_down_tail(ipif)) != 0)
1093 			return (err);
1094 	}
1095 	return (0);
1096 }
1097 
1098 /* ARGSUSED */
1099 void
1100 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1101 {
1102 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1103 	(void) ill_down_ipifs_tail(q->q_ptr);
1104 	freemsg(mp);
1105 	ipsq_current_finish(ipsq);
1106 }
1107 
1108 /*
1109  * ill_down_start is called when we want to down this ill and bring it up again
1110  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1111  * all interfaces, but don't tear down any plumbing.
1112  */
1113 boolean_t
1114 ill_down_start(queue_t *q, mblk_t *mp)
1115 {
1116 	ill_t	*ill = q->q_ptr;
1117 	ipif_t	*ipif;
1118 
1119 	ASSERT(IAM_WRITER_ILL(ill));
1120 	mutex_enter(&ill->ill_lock);
1121 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1122 	/* no more nce addition allowed */
1123 	mutex_exit(&ill->ill_lock);
1124 
1125 	/*
1126 	 * It is possible that some ioctl is already in progress while we
1127 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1128 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1129 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1130 	 * the in progress ioctl from ever completing.
1131 	 *
1132 	 * The thread that started the ioctl (if any) must have returned,
1133 	 * since we are now executing as writer. After the 2 calls below,
1134 	 * the state of the ipsq and the ill would reflect no trace of any
1135 	 * pending operation. Subsequently if there is any response to the
1136 	 * original ioctl from the driver, it would be discarded as an
1137 	 * unsolicited message from the driver.
1138 	 */
1139 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1140 	ill_dlpi_clear_deferred(ill);
1141 
1142 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1143 		(void) ipif_down(ipif, NULL, NULL);
1144 
1145 	ill_down(ill);
1146 
1147 	/*
1148 	 * Walk all CONNs that can have a reference on an ire or nce for this
1149 	 * ill (we actually walk all that now have stale references).
1150 	 */
1151 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1152 
1153 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1154 	if (ill->ill_isv6)
1155 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1156 
1157 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1158 
1159 	/*
1160 	 * Atomically test and add the pending mp if references are active.
1161 	 */
1162 	mutex_enter(&ill->ill_lock);
1163 	if (!ill_is_quiescent(ill)) {
1164 		/* call cannot fail since `conn_t *' argument is NULL */
1165 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1166 		    mp, ILL_DOWN);
1167 		mutex_exit(&ill->ill_lock);
1168 		return (B_FALSE);
1169 	}
1170 	mutex_exit(&ill->ill_lock);
1171 	return (B_TRUE);
1172 }
1173 
1174 static void
1175 ill_down(ill_t *ill)
1176 {
1177 	mblk_t	*mp;
1178 	ip_stack_t	*ipst = ill->ill_ipst;
1179 
1180 	/*
1181 	 * Blow off any IREs dependent on this ILL.
1182 	 * The caller needs to handle conn_ixa_cleanup
1183 	 */
1184 	ill_delete_ires(ill);
1185 
1186 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1187 
1188 	/* Remove any conn_*_ill depending on this ill */
1189 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1190 
1191 	/*
1192 	 * Free state for additional IREs.
1193 	 */
1194 	mutex_enter(&ill->ill_saved_ire_lock);
1195 	mp = ill->ill_saved_ire_mp;
1196 	ill->ill_saved_ire_mp = NULL;
1197 	ill->ill_saved_ire_cnt = 0;
1198 	mutex_exit(&ill->ill_saved_ire_lock);
1199 	freemsg(mp);
1200 }
1201 
1202 /*
1203  * ire_walk routine used to delete every IRE that depends on
1204  * 'ill'.  (Always called as writer.)
1205  *
1206  * Note: since the routes added by the kernel are deleted separately,
1207  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1208  *
1209  * We also remove references on ire_nce_cache entries that refer to the ill.
1210  */
1211 void
1212 ill_downi(ire_t *ire, char *ill_arg)
1213 {
1214 	ill_t	*ill = (ill_t *)ill_arg;
1215 	nce_t	*nce;
1216 
1217 	mutex_enter(&ire->ire_lock);
1218 	nce = ire->ire_nce_cache;
1219 	if (nce != NULL && nce->nce_ill == ill)
1220 		ire->ire_nce_cache = NULL;
1221 	else
1222 		nce = NULL;
1223 	mutex_exit(&ire->ire_lock);
1224 	if (nce != NULL)
1225 		nce_refrele(nce);
1226 	if (ire->ire_ill == ill)
1227 		ire_delete(ire);
1228 }
1229 
1230 /* Remove IRE_IF_CLONE on this ill */
1231 void
1232 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1233 {
1234 	ill_t	*ill = (ill_t *)ill_arg;
1235 
1236 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1237 	if (ire->ire_ill == ill)
1238 		ire_delete(ire);
1239 }
1240 
1241 /* Consume an M_IOCACK of the fastpath probe. */
1242 void
1243 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1244 {
1245 	mblk_t	*mp1 = mp;
1246 
1247 	/*
1248 	 * If this was the first attempt turn on the fastpath probing.
1249 	 */
1250 	mutex_enter(&ill->ill_lock);
1251 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1252 		ill->ill_dlpi_fastpath_state = IDS_OK;
1253 	mutex_exit(&ill->ill_lock);
1254 
1255 	/* Free the M_IOCACK mblk, hold on to the data */
1256 	mp = mp->b_cont;
1257 	freeb(mp1);
1258 	if (mp == NULL)
1259 		return;
1260 	if (mp->b_cont != NULL)
1261 		nce_fastpath_update(ill, mp);
1262 	else
1263 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1264 	freemsg(mp);
1265 }
1266 
1267 /*
1268  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1269  * The data portion of the request is a dl_unitdata_req_t template for
1270  * what we would send downstream in the absence of a fastpath confirmation.
1271  */
1272 int
1273 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1274 {
1275 	struct iocblk	*ioc;
1276 	mblk_t	*mp;
1277 
1278 	if (dlur_mp == NULL)
1279 		return (EINVAL);
1280 
1281 	mutex_enter(&ill->ill_lock);
1282 	switch (ill->ill_dlpi_fastpath_state) {
1283 	case IDS_FAILED:
1284 		/*
1285 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1286 		 * support it.
1287 		 */
1288 		mutex_exit(&ill->ill_lock);
1289 		return (ENOTSUP);
1290 	case IDS_UNKNOWN:
1291 		/* This is the first probe */
1292 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1293 		break;
1294 	default:
1295 		break;
1296 	}
1297 	mutex_exit(&ill->ill_lock);
1298 
1299 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1300 		return (EAGAIN);
1301 
1302 	mp->b_cont = copyb(dlur_mp);
1303 	if (mp->b_cont == NULL) {
1304 		freeb(mp);
1305 		return (EAGAIN);
1306 	}
1307 
1308 	ioc = (struct iocblk *)mp->b_rptr;
1309 	ioc->ioc_count = msgdsize(mp->b_cont);
1310 
1311 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1312 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1313 	putnext(ill->ill_wq, mp);
1314 	return (0);
1315 }
1316 
1317 void
1318 ill_capability_probe(ill_t *ill)
1319 {
1320 	mblk_t	*mp;
1321 
1322 	ASSERT(IAM_WRITER_ILL(ill));
1323 
1324 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1325 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1326 		return;
1327 
1328 	/*
1329 	 * We are starting a new cycle of capability negotiation.
1330 	 * Free up the capab reset messages of any previous incarnation.
1331 	 * We will do a fresh allocation when we get the response to our probe
1332 	 */
1333 	if (ill->ill_capab_reset_mp != NULL) {
1334 		freemsg(ill->ill_capab_reset_mp);
1335 		ill->ill_capab_reset_mp = NULL;
1336 	}
1337 
1338 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1339 
1340 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1341 	if (mp == NULL)
1342 		return;
1343 
1344 	ill_capability_send(ill, mp);
1345 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1346 }
1347 
1348 void
1349 ill_capability_reset(ill_t *ill, boolean_t reneg)
1350 {
1351 	ASSERT(IAM_WRITER_ILL(ill));
1352 
1353 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1354 		return;
1355 
1356 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1357 
1358 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1359 	ill->ill_capab_reset_mp = NULL;
1360 	/*
1361 	 * We turn off all capabilities except those pertaining to
1362 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1363 	 * which will be turned off by the corresponding reset functions.
1364 	 */
1365 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1366 }
1367 
1368 static void
1369 ill_capability_reset_alloc(ill_t *ill)
1370 {
1371 	mblk_t *mp;
1372 	size_t	size = 0;
1373 	int	err;
1374 	dl_capability_req_t	*capb;
1375 
1376 	ASSERT(IAM_WRITER_ILL(ill));
1377 	ASSERT(ill->ill_capab_reset_mp == NULL);
1378 
1379 	if (ILL_HCKSUM_CAPABLE(ill)) {
1380 		size += sizeof (dl_capability_sub_t) +
1381 		    sizeof (dl_capab_hcksum_t);
1382 	}
1383 
1384 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1385 		size += sizeof (dl_capability_sub_t) +
1386 		    sizeof (dl_capab_zerocopy_t);
1387 	}
1388 
1389 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1390 		size += sizeof (dl_capability_sub_t) +
1391 		    sizeof (dl_capab_dld_t);
1392 	}
1393 
1394 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1395 	    STR_NOSIG, &err);
1396 
1397 	mp->b_datap->db_type = M_PROTO;
1398 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1399 
1400 	capb = (dl_capability_req_t *)mp->b_rptr;
1401 	capb->dl_primitive = DL_CAPABILITY_REQ;
1402 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1403 	capb->dl_sub_length = size;
1404 
1405 	mp->b_wptr += sizeof (dl_capability_req_t);
1406 
1407 	/*
1408 	 * Each handler fills in the corresponding dl_capability_sub_t
1409 	 * inside the mblk,
1410 	 */
1411 	ill_capability_hcksum_reset_fill(ill, mp);
1412 	ill_capability_zerocopy_reset_fill(ill, mp);
1413 	ill_capability_dld_reset_fill(ill, mp);
1414 
1415 	ill->ill_capab_reset_mp = mp;
1416 }
1417 
1418 static void
1419 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1420 {
1421 	dl_capab_id_t *id_ic;
1422 	uint_t sub_dl_cap = outers->dl_cap;
1423 	dl_capability_sub_t *inners;
1424 	uint8_t *capend;
1425 
1426 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1427 
1428 	/*
1429 	 * Note: range checks here are not absolutely sufficient to
1430 	 * make us robust against malformed messages sent by drivers;
1431 	 * this is in keeping with the rest of IP's dlpi handling.
1432 	 * (Remember, it's coming from something else in the kernel
1433 	 * address space)
1434 	 */
1435 
1436 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1437 	if (capend > mp->b_wptr) {
1438 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1439 		    "malformed sub-capability too long for mblk");
1440 		return;
1441 	}
1442 
1443 	id_ic = (dl_capab_id_t *)(outers + 1);
1444 
1445 	if (outers->dl_length < sizeof (*id_ic) ||
1446 	    (inners = &id_ic->id_subcap,
1447 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1448 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1449 		    "encapsulated capab type %d too long for mblk",
1450 		    inners->dl_cap);
1451 		return;
1452 	}
1453 
1454 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1455 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1456 		    "isn't as expected; pass-thru module(s) detected, "
1457 		    "discarding capability\n", inners->dl_cap));
1458 		return;
1459 	}
1460 
1461 	/* Process the encapsulated sub-capability */
1462 	ill_capability_dispatch(ill, mp, inners);
1463 }
1464 
1465 static void
1466 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1467 {
1468 	dl_capability_sub_t *dl_subcap;
1469 
1470 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1471 		return;
1472 
1473 	/*
1474 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1475 	 * initialized below since it is not used by DLD.
1476 	 */
1477 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1478 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1479 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1480 
1481 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1482 }
1483 
1484 static void
1485 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1486 {
1487 	/*
1488 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1489 	 * is only to get the VRRP capability.
1490 	 *
1491 	 * Note that we cannot check ill_ipif_up_count here since
1492 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1493 	 * That is done asynchronously, and can race with this function.
1494 	 */
1495 	if (!ill->ill_dl_up) {
1496 		if (subp->dl_cap == DL_CAPAB_VRRP)
1497 			ill_capability_vrrp_ack(ill, mp, subp);
1498 		return;
1499 	}
1500 
1501 	switch (subp->dl_cap) {
1502 	case DL_CAPAB_HCKSUM:
1503 		ill_capability_hcksum_ack(ill, mp, subp);
1504 		break;
1505 	case DL_CAPAB_ZEROCOPY:
1506 		ill_capability_zerocopy_ack(ill, mp, subp);
1507 		break;
1508 	case DL_CAPAB_DLD:
1509 		ill_capability_dld_ack(ill, mp, subp);
1510 		break;
1511 	case DL_CAPAB_VRRP:
1512 		break;
1513 	default:
1514 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1515 		    subp->dl_cap));
1516 	}
1517 }
1518 
1519 /*
1520  * Process the vrrp capability received from a DLS Provider. isub must point
1521  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1522  */
1523 static void
1524 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1525 {
1526 	dl_capab_vrrp_t	*vrrp;
1527 	uint_t		sub_dl_cap = isub->dl_cap;
1528 	uint8_t		*capend;
1529 
1530 	ASSERT(IAM_WRITER_ILL(ill));
1531 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1532 
1533 	/*
1534 	 * Note: range checks here are not absolutely sufficient to
1535 	 * make us robust against malformed messages sent by drivers;
1536 	 * this is in keeping with the rest of IP's dlpi handling.
1537 	 * (Remember, it's coming from something else in the kernel
1538 	 * address space)
1539 	 */
1540 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1541 	if (capend > mp->b_wptr) {
1542 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1543 		    "malformed sub-capability too long for mblk");
1544 		return;
1545 	}
1546 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1547 
1548 	/*
1549 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1550 	 */
1551 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1552 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1553 		ill->ill_flags |= ILLF_VRRP;
1554 	}
1555 }
1556 
1557 /*
1558  * Process a hardware checksum offload capability negotiation ack received
1559  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1560  * of a DL_CAPABILITY_ACK message.
1561  */
1562 static void
1563 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1564 {
1565 	dl_capability_req_t	*ocap;
1566 	dl_capab_hcksum_t	*ihck, *ohck;
1567 	ill_hcksum_capab_t	**ill_hcksum;
1568 	mblk_t			*nmp = NULL;
1569 	uint_t			sub_dl_cap = isub->dl_cap;
1570 	uint8_t			*capend;
1571 
1572 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1573 
1574 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1575 
1576 	/*
1577 	 * Note: range checks here are not absolutely sufficient to
1578 	 * make us robust against malformed messages sent by drivers;
1579 	 * this is in keeping with the rest of IP's dlpi handling.
1580 	 * (Remember, it's coming from something else in the kernel
1581 	 * address space)
1582 	 */
1583 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1584 	if (capend > mp->b_wptr) {
1585 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1586 		    "malformed sub-capability too long for mblk");
1587 		return;
1588 	}
1589 
1590 	/*
1591 	 * There are two types of acks we process here:
1592 	 * 1. acks in reply to a (first form) generic capability req
1593 	 *    (no ENABLE flag set)
1594 	 * 2. acks in reply to a ENABLE capability req.
1595 	 *    (ENABLE flag set)
1596 	 */
1597 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1598 
1599 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1600 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1601 		    "unsupported hardware checksum "
1602 		    "sub-capability (version %d, expected %d)",
1603 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1604 		return;
1605 	}
1606 
1607 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1608 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1609 		    "checksum capability isn't as expected; pass-thru "
1610 		    "module(s) detected, discarding capability\n"));
1611 		return;
1612 	}
1613 
1614 #define	CURR_HCKSUM_CAPAB				\
1615 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1616 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1617 
1618 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1619 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1620 		/* do ENABLE processing */
1621 		if (*ill_hcksum == NULL) {
1622 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1623 			    KM_NOSLEEP);
1624 
1625 			if (*ill_hcksum == NULL) {
1626 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1627 				    "could not enable hcksum version %d "
1628 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1629 				    ill->ill_name);
1630 				return;
1631 			}
1632 		}
1633 
1634 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1635 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1636 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1637 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1638 		    "has enabled hardware checksumming\n ",
1639 		    ill->ill_name));
1640 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1641 		/*
1642 		 * Enabling hardware checksum offload
1643 		 * Currently IP supports {TCP,UDP}/IPv4
1644 		 * partial and full cksum offload and
1645 		 * IPv4 header checksum offload.
1646 		 * Allocate new mblk which will
1647 		 * contain a new capability request
1648 		 * to enable hardware checksum offload.
1649 		 */
1650 		uint_t	size;
1651 		uchar_t	*rptr;
1652 
1653 		size = sizeof (dl_capability_req_t) +
1654 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1655 
1656 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1657 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1658 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1659 			    ill->ill_name);
1660 			return;
1661 		}
1662 
1663 		rptr = nmp->b_rptr;
1664 		/* initialize dl_capability_req_t */
1665 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1666 		ocap->dl_sub_offset =
1667 		    sizeof (dl_capability_req_t);
1668 		ocap->dl_sub_length =
1669 		    sizeof (dl_capability_sub_t) +
1670 		    isub->dl_length;
1671 		nmp->b_rptr += sizeof (dl_capability_req_t);
1672 
1673 		/* initialize dl_capability_sub_t */
1674 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1675 		nmp->b_rptr += sizeof (*isub);
1676 
1677 		/* initialize dl_capab_hcksum_t */
1678 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1679 		bcopy(ihck, ohck, sizeof (*ihck));
1680 
1681 		nmp->b_rptr = rptr;
1682 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1683 
1684 		/* Set ENABLE flag */
1685 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1686 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1687 
1688 		/*
1689 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1690 		 * hardware checksum acceleration.
1691 		 */
1692 		ill_capability_send(ill, nmp);
1693 	} else {
1694 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1695 		    "advertised %x hardware checksum capability flags\n",
1696 		    ill->ill_name, ihck->hcksum_txflags));
1697 	}
1698 }
1699 
1700 static void
1701 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1702 {
1703 	dl_capab_hcksum_t *hck_subcap;
1704 	dl_capability_sub_t *dl_subcap;
1705 
1706 	if (!ILL_HCKSUM_CAPABLE(ill))
1707 		return;
1708 
1709 	ASSERT(ill->ill_hcksum_capab != NULL);
1710 
1711 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1712 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1713 	dl_subcap->dl_length = sizeof (*hck_subcap);
1714 
1715 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1716 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1717 	hck_subcap->hcksum_txflags = 0;
1718 
1719 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1720 }
1721 
1722 static void
1723 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1724 {
1725 	mblk_t *nmp = NULL;
1726 	dl_capability_req_t *oc;
1727 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1728 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1729 	uint_t sub_dl_cap = isub->dl_cap;
1730 	uint8_t *capend;
1731 
1732 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1733 
1734 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1735 
1736 	/*
1737 	 * Note: range checks here are not absolutely sufficient to
1738 	 * make us robust against malformed messages sent by drivers;
1739 	 * this is in keeping with the rest of IP's dlpi handling.
1740 	 * (Remember, it's coming from something else in the kernel
1741 	 * address space)
1742 	 */
1743 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1744 	if (capend > mp->b_wptr) {
1745 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1746 		    "malformed sub-capability too long for mblk");
1747 		return;
1748 	}
1749 
1750 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1751 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1752 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1753 		    "unsupported ZEROCOPY sub-capability (version %d, "
1754 		    "expected %d)", zc_ic->zerocopy_version,
1755 		    ZEROCOPY_VERSION_1);
1756 		return;
1757 	}
1758 
1759 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1760 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1761 		    "capability isn't as expected; pass-thru module(s) "
1762 		    "detected, discarding capability\n"));
1763 		return;
1764 	}
1765 
1766 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1767 		if (*ill_zerocopy_capab == NULL) {
1768 			*ill_zerocopy_capab =
1769 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1770 			    KM_NOSLEEP);
1771 
1772 			if (*ill_zerocopy_capab == NULL) {
1773 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1774 				    "could not enable Zero-copy version %d "
1775 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1776 				    ill->ill_name);
1777 				return;
1778 			}
1779 		}
1780 
1781 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1782 		    "supports Zero-copy version %d\n", ill->ill_name,
1783 		    ZEROCOPY_VERSION_1));
1784 
1785 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1786 		    zc_ic->zerocopy_version;
1787 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1788 		    zc_ic->zerocopy_flags;
1789 
1790 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1791 	} else {
1792 		uint_t size;
1793 		uchar_t *rptr;
1794 
1795 		size = sizeof (dl_capability_req_t) +
1796 		    sizeof (dl_capability_sub_t) +
1797 		    sizeof (dl_capab_zerocopy_t);
1798 
1799 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1800 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1801 			    "could not enable zerocopy for %s (ENOMEM)\n",
1802 			    ill->ill_name);
1803 			return;
1804 		}
1805 
1806 		rptr = nmp->b_rptr;
1807 		/* initialize dl_capability_req_t */
1808 		oc = (dl_capability_req_t *)rptr;
1809 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1810 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1811 		    sizeof (dl_capab_zerocopy_t);
1812 		rptr += sizeof (dl_capability_req_t);
1813 
1814 		/* initialize dl_capability_sub_t */
1815 		bcopy(isub, rptr, sizeof (*isub));
1816 		rptr += sizeof (*isub);
1817 
1818 		/* initialize dl_capab_zerocopy_t */
1819 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1820 		*zc_oc = *zc_ic;
1821 
1822 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1823 		    "to enable zero-copy version %d\n", ill->ill_name,
1824 		    ZEROCOPY_VERSION_1));
1825 
1826 		/* set VMSAFE_MEM flag */
1827 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1828 
1829 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1830 		ill_capability_send(ill, nmp);
1831 	}
1832 }
1833 
1834 static void
1835 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1836 {
1837 	dl_capab_zerocopy_t *zerocopy_subcap;
1838 	dl_capability_sub_t *dl_subcap;
1839 
1840 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1841 		return;
1842 
1843 	ASSERT(ill->ill_zerocopy_capab != NULL);
1844 
1845 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1846 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1847 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1848 
1849 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1850 	zerocopy_subcap->zerocopy_version =
1851 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1852 	zerocopy_subcap->zerocopy_flags = 0;
1853 
1854 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1855 }
1856 
1857 /*
1858  * DLD capability
1859  * Refer to dld.h for more information regarding the purpose and usage
1860  * of this capability.
1861  */
1862 static void
1863 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1864 {
1865 	dl_capab_dld_t		*dld_ic, dld;
1866 	uint_t			sub_dl_cap = isub->dl_cap;
1867 	uint8_t			*capend;
1868 	ill_dld_capab_t		*idc;
1869 
1870 	ASSERT(IAM_WRITER_ILL(ill));
1871 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1872 
1873 	/*
1874 	 * Note: range checks here are not absolutely sufficient to
1875 	 * make us robust against malformed messages sent by drivers;
1876 	 * this is in keeping with the rest of IP's dlpi handling.
1877 	 * (Remember, it's coming from something else in the kernel
1878 	 * address space)
1879 	 */
1880 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1881 	if (capend > mp->b_wptr) {
1882 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1883 		    "malformed sub-capability too long for mblk");
1884 		return;
1885 	}
1886 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1887 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1888 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1889 		    "unsupported DLD sub-capability (version %d, "
1890 		    "expected %d)", dld_ic->dld_version,
1891 		    DLD_CURRENT_VERSION);
1892 		return;
1893 	}
1894 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1895 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1896 		    "capability isn't as expected; pass-thru module(s) "
1897 		    "detected, discarding capability\n"));
1898 		return;
1899 	}
1900 
1901 	/*
1902 	 * Copy locally to ensure alignment.
1903 	 */
1904 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1905 
1906 	if ((idc = ill->ill_dld_capab) == NULL) {
1907 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1908 		if (idc == NULL) {
1909 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1910 			    "could not enable DLD version %d "
1911 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1912 			    ill->ill_name);
1913 			return;
1914 		}
1915 		ill->ill_dld_capab = idc;
1916 	}
1917 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1918 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1919 	ip1dbg(("ill_capability_dld_ack: interface %s "
1920 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1921 
1922 	ill_capability_dld_enable(ill);
1923 }
1924 
1925 /*
1926  * Typically capability negotiation between IP and the driver happens via
1927  * DLPI message exchange. However GLD also offers a direct function call
1928  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1929  * But arbitrary function calls into IP or GLD are not permitted, since both
1930  * of them are protected by their own perimeter mechanism. The perimeter can
1931  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1932  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1933  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1934  * to enter the mac perimeter and then do the direct function calls into
1935  * GLD to enable squeue polling. The ring related callbacks from the mac into
1936  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1937  * protected by the mac perimeter.
1938  */
1939 static void
1940 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1941 {
1942 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1943 	int			err;
1944 
1945 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1946 	    DLD_ENABLE);
1947 	ASSERT(err == 0);
1948 }
1949 
1950 static void
1951 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1952 {
1953 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1954 	int			err;
1955 
1956 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1957 	    DLD_DISABLE);
1958 	ASSERT(err == 0);
1959 }
1960 
1961 boolean_t
1962 ill_mac_perim_held(ill_t *ill)
1963 {
1964 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1965 
1966 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1967 	    DLD_QUERY));
1968 }
1969 
1970 static void
1971 ill_capability_direct_enable(ill_t *ill)
1972 {
1973 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1974 	ill_dld_direct_t	*idd = &idc->idc_direct;
1975 	dld_capab_direct_t	direct;
1976 	int			rc;
1977 
1978 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1979 
1980 	bzero(&direct, sizeof (direct));
1981 	direct.di_rx_cf = (uintptr_t)ip_input;
1982 	direct.di_rx_ch = ill;
1983 
1984 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1985 	    DLD_ENABLE);
1986 	if (rc == 0) {
1987 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1988 		idd->idd_tx_dh = direct.di_tx_dh;
1989 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1990 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1991 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1992 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1993 		ASSERT(idd->idd_tx_cb_df != NULL);
1994 		ASSERT(idd->idd_tx_fctl_df != NULL);
1995 		ASSERT(idd->idd_tx_df != NULL);
1996 		/*
1997 		 * One time registration of flow enable callback function
1998 		 */
1999 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2000 		    ill_flow_enable, ill);
2001 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2002 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2003 	} else {
2004 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2005 		    "capability, rc = %d\n", rc);
2006 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2007 	}
2008 }
2009 
2010 static void
2011 ill_capability_poll_enable(ill_t *ill)
2012 {
2013 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2014 	dld_capab_poll_t	poll;
2015 	int			rc;
2016 
2017 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2018 
2019 	bzero(&poll, sizeof (poll));
2020 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2021 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2022 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2023 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2024 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2025 	poll.poll_ring_ch = ill;
2026 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2027 	    DLD_ENABLE);
2028 	if (rc == 0) {
2029 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2030 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2031 	} else {
2032 		ip1dbg(("warning: could not enable POLL "
2033 		    "capability, rc = %d\n", rc));
2034 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2035 	}
2036 }
2037 
2038 /*
2039  * Enable the LSO capability.
2040  */
2041 static void
2042 ill_capability_lso_enable(ill_t *ill)
2043 {
2044 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2045 	dld_capab_lso_t	lso;
2046 	int rc;
2047 
2048 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2049 
2050 	if (ill->ill_lso_capab == NULL) {
2051 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2052 		    KM_NOSLEEP);
2053 		if (ill->ill_lso_capab == NULL) {
2054 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2055 			    "could not enable LSO for %s (ENOMEM)\n",
2056 			    ill->ill_name);
2057 			return;
2058 		}
2059 	}
2060 
2061 	bzero(&lso, sizeof (lso));
2062 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2063 	    DLD_ENABLE)) == 0) {
2064 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2065 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2066 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2067 		ip1dbg(("ill_capability_lso_enable: interface %s "
2068 		    "has enabled LSO\n ", ill->ill_name));
2069 	} else {
2070 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2071 		ill->ill_lso_capab = NULL;
2072 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2073 	}
2074 }
2075 
2076 static void
2077 ill_capability_dld_enable(ill_t *ill)
2078 {
2079 	mac_perim_handle_t mph;
2080 
2081 	ASSERT(IAM_WRITER_ILL(ill));
2082 
2083 	if (ill->ill_isv6)
2084 		return;
2085 
2086 	ill_mac_perim_enter(ill, &mph);
2087 	if (!ill->ill_isv6) {
2088 		ill_capability_direct_enable(ill);
2089 		ill_capability_poll_enable(ill);
2090 		ill_capability_lso_enable(ill);
2091 	}
2092 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2093 	ill_mac_perim_exit(ill, mph);
2094 }
2095 
2096 static void
2097 ill_capability_dld_disable(ill_t *ill)
2098 {
2099 	ill_dld_capab_t	*idc;
2100 	ill_dld_direct_t *idd;
2101 	mac_perim_handle_t	mph;
2102 
2103 	ASSERT(IAM_WRITER_ILL(ill));
2104 
2105 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2106 		return;
2107 
2108 	ill_mac_perim_enter(ill, &mph);
2109 
2110 	idc = ill->ill_dld_capab;
2111 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2112 		/*
2113 		 * For performance we avoid locks in the transmit data path
2114 		 * and don't maintain a count of the number of threads using
2115 		 * direct calls. Thus some threads could be using direct
2116 		 * transmit calls to GLD, even after the capability mechanism
2117 		 * turns it off. This is still safe since the handles used in
2118 		 * the direct calls continue to be valid until the unplumb is
2119 		 * completed. Remove the callback that was added (1-time) at
2120 		 * capab enable time.
2121 		 */
2122 		mutex_enter(&ill->ill_lock);
2123 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2124 		mutex_exit(&ill->ill_lock);
2125 		if (ill->ill_flownotify_mh != NULL) {
2126 			idd = &idc->idc_direct;
2127 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2128 			    ill->ill_flownotify_mh);
2129 			ill->ill_flownotify_mh = NULL;
2130 		}
2131 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2132 		    NULL, DLD_DISABLE);
2133 	}
2134 
2135 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2136 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2137 		ip_squeue_clean_all(ill);
2138 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2139 		    NULL, DLD_DISABLE);
2140 	}
2141 
2142 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2143 		ASSERT(ill->ill_lso_capab != NULL);
2144 		/*
2145 		 * Clear the capability flag for LSO but retain the
2146 		 * ill_lso_capab structure since it's possible that another
2147 		 * thread is still referring to it.  The structure only gets
2148 		 * deallocated when we destroy the ill.
2149 		 */
2150 
2151 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2152 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2153 		    NULL, DLD_DISABLE);
2154 	}
2155 
2156 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2157 	ill_mac_perim_exit(ill, mph);
2158 }
2159 
2160 /*
2161  * Capability Negotiation protocol
2162  *
2163  * We don't wait for DLPI capability operations to finish during interface
2164  * bringup or teardown. Doing so would introduce more asynchrony and the
2165  * interface up/down operations will need multiple return and restarts.
2166  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2167  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2168  * exclusive operation won't start until the DLPI operations of the previous
2169  * exclusive operation complete.
2170  *
2171  * The capability state machine is shown below.
2172  *
2173  * state		next state		event, action
2174  *
2175  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2176  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2177  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2178  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2179  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2180  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2181  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2182  *						    ill_capability_probe.
2183  */
2184 
2185 /*
2186  * Dedicated thread started from ip_stack_init that handles capability
2187  * disable. This thread ensures the taskq dispatch does not fail by waiting
2188  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2189  * that direct calls to DLD are done in a cv_waitable context.
2190  */
2191 void
2192 ill_taskq_dispatch(ip_stack_t *ipst)
2193 {
2194 	callb_cpr_t cprinfo;
2195 	char 	name[64];
2196 	mblk_t	*mp;
2197 
2198 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2199 	    ipst->ips_netstack->netstack_stackid);
2200 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2201 	    name);
2202 	mutex_enter(&ipst->ips_capab_taskq_lock);
2203 
2204 	for (;;) {
2205 		mp = ipst->ips_capab_taskq_head;
2206 		while (mp != NULL) {
2207 			ipst->ips_capab_taskq_head = mp->b_next;
2208 			if (ipst->ips_capab_taskq_head == NULL)
2209 				ipst->ips_capab_taskq_tail = NULL;
2210 			mutex_exit(&ipst->ips_capab_taskq_lock);
2211 			mp->b_next = NULL;
2212 
2213 			VERIFY(taskq_dispatch(system_taskq,
2214 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2215 			mutex_enter(&ipst->ips_capab_taskq_lock);
2216 			mp = ipst->ips_capab_taskq_head;
2217 		}
2218 
2219 		if (ipst->ips_capab_taskq_quit)
2220 			break;
2221 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2222 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2223 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2224 	}
2225 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2226 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2227 	CALLB_CPR_EXIT(&cprinfo);
2228 	thread_exit();
2229 }
2230 
2231 /*
2232  * Consume a new-style hardware capabilities negotiation ack.
2233  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2234  */
2235 static void
2236 ill_capability_ack_thr(void *arg)
2237 {
2238 	mblk_t	*mp = arg;
2239 	dl_capability_ack_t *capp;
2240 	dl_capability_sub_t *subp, *endp;
2241 	ill_t	*ill;
2242 	boolean_t reneg;
2243 
2244 	ill = (ill_t *)mp->b_prev;
2245 	mp->b_prev = NULL;
2246 
2247 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2248 
2249 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2250 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2251 		/*
2252 		 * We have received the ack for our DL_CAPAB reset request.
2253 		 * There isnt' anything in the message that needs processing.
2254 		 * All message based capabilities have been disabled, now
2255 		 * do the function call based capability disable.
2256 		 */
2257 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2258 		ill_capability_dld_disable(ill);
2259 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2260 		if (reneg)
2261 			ill_capability_probe(ill);
2262 		goto done;
2263 	}
2264 
2265 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2266 		ill->ill_dlpi_capab_state = IDCS_OK;
2267 
2268 	capp = (dl_capability_ack_t *)mp->b_rptr;
2269 
2270 	if (capp->dl_sub_length == 0) {
2271 		/* no new-style capabilities */
2272 		goto done;
2273 	}
2274 
2275 	/* make sure the driver supplied correct dl_sub_length */
2276 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2277 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2278 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2279 		goto done;
2280 	}
2281 
2282 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2283 	/*
2284 	 * There are sub-capabilities. Process the ones we know about.
2285 	 * Loop until we don't have room for another sub-cap header..
2286 	 */
2287 	for (subp = SC(capp, capp->dl_sub_offset),
2288 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2289 	    subp <= endp;
2290 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2291 
2292 		switch (subp->dl_cap) {
2293 		case DL_CAPAB_ID_WRAPPER:
2294 			ill_capability_id_ack(ill, mp, subp);
2295 			break;
2296 		default:
2297 			ill_capability_dispatch(ill, mp, subp);
2298 			break;
2299 		}
2300 	}
2301 #undef SC
2302 done:
2303 	inet_freemsg(mp);
2304 	ill_capability_done(ill);
2305 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2306 }
2307 
2308 /*
2309  * This needs to be started in a taskq thread to provide a cv_waitable
2310  * context.
2311  */
2312 void
2313 ill_capability_ack(ill_t *ill, mblk_t *mp)
2314 {
2315 	ip_stack_t	*ipst = ill->ill_ipst;
2316 
2317 	mp->b_prev = (mblk_t *)ill;
2318 	ASSERT(mp->b_next == NULL);
2319 
2320 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2321 	    TQ_NOSLEEP) != 0)
2322 		return;
2323 
2324 	/*
2325 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2326 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2327 	 */
2328 	mutex_enter(&ipst->ips_capab_taskq_lock);
2329 	if (ipst->ips_capab_taskq_head == NULL) {
2330 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2331 		ipst->ips_capab_taskq_head = mp;
2332 	} else {
2333 		ipst->ips_capab_taskq_tail->b_next = mp;
2334 	}
2335 	ipst->ips_capab_taskq_tail = mp;
2336 
2337 	cv_signal(&ipst->ips_capab_taskq_cv);
2338 	mutex_exit(&ipst->ips_capab_taskq_lock);
2339 }
2340 
2341 /*
2342  * This routine is called to scan the fragmentation reassembly table for
2343  * the specified ILL for any packets that are starting to smell.
2344  * dead_interval is the maximum time in seconds that will be tolerated.  It
2345  * will either be the value specified in ip_g_frag_timeout, or zero if the
2346  * ILL is shutting down and it is time to blow everything off.
2347  *
2348  * It returns the number of seconds (as a time_t) that the next frag timer
2349  * should be scheduled for, 0 meaning that the timer doesn't need to be
2350  * re-started.  Note that the method of calculating next_timeout isn't
2351  * entirely accurate since time will flow between the time we grab
2352  * current_time and the time we schedule the next timeout.  This isn't a
2353  * big problem since this is the timer for sending an ICMP reassembly time
2354  * exceeded messages, and it doesn't have to be exactly accurate.
2355  *
2356  * This function is
2357  * sometimes called as writer, although this is not required.
2358  */
2359 time_t
2360 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2361 {
2362 	ipfb_t	*ipfb;
2363 	ipfb_t	*endp;
2364 	ipf_t	*ipf;
2365 	ipf_t	*ipfnext;
2366 	mblk_t	*mp;
2367 	time_t	current_time = gethrestime_sec();
2368 	time_t	next_timeout = 0;
2369 	uint32_t	hdr_length;
2370 	mblk_t	*send_icmp_head;
2371 	mblk_t	*send_icmp_head_v6;
2372 	ip_stack_t *ipst = ill->ill_ipst;
2373 	ip_recv_attr_t iras;
2374 
2375 	bzero(&iras, sizeof (iras));
2376 	iras.ira_flags = 0;
2377 	iras.ira_ill = iras.ira_rill = ill;
2378 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2379 	iras.ira_rifindex = iras.ira_ruifindex;
2380 
2381 	ipfb = ill->ill_frag_hash_tbl;
2382 	if (ipfb == NULL)
2383 		return (B_FALSE);
2384 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2385 	/* Walk the frag hash table. */
2386 	for (; ipfb < endp; ipfb++) {
2387 		send_icmp_head = NULL;
2388 		send_icmp_head_v6 = NULL;
2389 		mutex_enter(&ipfb->ipfb_lock);
2390 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2391 			time_t frag_time = current_time - ipf->ipf_timestamp;
2392 			time_t frag_timeout;
2393 
2394 			if (frag_time < dead_interval) {
2395 				/*
2396 				 * There are some outstanding fragments
2397 				 * that will timeout later.  Make note of
2398 				 * the time so that we can reschedule the
2399 				 * next timeout appropriately.
2400 				 */
2401 				frag_timeout = dead_interval - frag_time;
2402 				if (next_timeout == 0 ||
2403 				    frag_timeout < next_timeout) {
2404 					next_timeout = frag_timeout;
2405 				}
2406 				break;
2407 			}
2408 			/* Time's up.  Get it out of here. */
2409 			hdr_length = ipf->ipf_nf_hdr_len;
2410 			ipfnext = ipf->ipf_hash_next;
2411 			if (ipfnext)
2412 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2413 			*ipf->ipf_ptphn = ipfnext;
2414 			mp = ipf->ipf_mp->b_cont;
2415 			for (; mp; mp = mp->b_cont) {
2416 				/* Extra points for neatness. */
2417 				IP_REASS_SET_START(mp, 0);
2418 				IP_REASS_SET_END(mp, 0);
2419 			}
2420 			mp = ipf->ipf_mp->b_cont;
2421 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2422 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2423 			ipfb->ipfb_count -= ipf->ipf_count;
2424 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2425 			ipfb->ipfb_frag_pkts--;
2426 			/*
2427 			 * We do not send any icmp message from here because
2428 			 * we currently are holding the ipfb_lock for this
2429 			 * hash chain. If we try and send any icmp messages
2430 			 * from here we may end up via a put back into ip
2431 			 * trying to get the same lock, causing a recursive
2432 			 * mutex panic. Instead we build a list and send all
2433 			 * the icmp messages after we have dropped the lock.
2434 			 */
2435 			if (ill->ill_isv6) {
2436 				if (hdr_length != 0) {
2437 					mp->b_next = send_icmp_head_v6;
2438 					send_icmp_head_v6 = mp;
2439 				} else {
2440 					freemsg(mp);
2441 				}
2442 			} else {
2443 				if (hdr_length != 0) {
2444 					mp->b_next = send_icmp_head;
2445 					send_icmp_head = mp;
2446 				} else {
2447 					freemsg(mp);
2448 				}
2449 			}
2450 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2451 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2452 			freeb(ipf->ipf_mp);
2453 		}
2454 		mutex_exit(&ipfb->ipfb_lock);
2455 		/*
2456 		 * Now need to send any icmp messages that we delayed from
2457 		 * above.
2458 		 */
2459 		while (send_icmp_head_v6 != NULL) {
2460 			ip6_t *ip6h;
2461 
2462 			mp = send_icmp_head_v6;
2463 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2464 			mp->b_next = NULL;
2465 			ip6h = (ip6_t *)mp->b_rptr;
2466 			iras.ira_flags = 0;
2467 			/*
2468 			 * This will result in an incorrect ALL_ZONES zoneid
2469 			 * for multicast packets, but we
2470 			 * don't send ICMP errors for those in any case.
2471 			 */
2472 			iras.ira_zoneid =
2473 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2474 			    ill, ipst);
2475 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2476 			icmp_time_exceeded_v6(mp,
2477 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2478 			    &iras);
2479 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2480 		}
2481 		while (send_icmp_head != NULL) {
2482 			ipaddr_t dst;
2483 
2484 			mp = send_icmp_head;
2485 			send_icmp_head = send_icmp_head->b_next;
2486 			mp->b_next = NULL;
2487 
2488 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2489 
2490 			iras.ira_flags = IRAF_IS_IPV4;
2491 			/*
2492 			 * This will result in an incorrect ALL_ZONES zoneid
2493 			 * for broadcast and multicast packets, but we
2494 			 * don't send ICMP errors for those in any case.
2495 			 */
2496 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2497 			    ill, ipst);
2498 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2499 			icmp_time_exceeded(mp,
2500 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2501 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2502 		}
2503 	}
2504 	/*
2505 	 * A non-dying ILL will use the return value to decide whether to
2506 	 * restart the frag timer, and for how long.
2507 	 */
2508 	return (next_timeout);
2509 }
2510 
2511 /*
2512  * This routine is called when the approximate count of mblk memory used
2513  * for the specified ILL has exceeded max_count.
2514  */
2515 void
2516 ill_frag_prune(ill_t *ill, uint_t max_count)
2517 {
2518 	ipfb_t	*ipfb;
2519 	ipf_t	*ipf;
2520 	size_t	count;
2521 	clock_t now;
2522 
2523 	/*
2524 	 * If we are here within ip_min_frag_prune_time msecs remove
2525 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2526 	 * ill_frag_free_num_pkts.
2527 	 */
2528 	mutex_enter(&ill->ill_lock);
2529 	now = ddi_get_lbolt();
2530 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2531 	    (ip_min_frag_prune_time != 0 ?
2532 	    ip_min_frag_prune_time : msec_per_tick)) {
2533 
2534 		ill->ill_frag_free_num_pkts++;
2535 
2536 	} else {
2537 		ill->ill_frag_free_num_pkts = 0;
2538 	}
2539 	ill->ill_last_frag_clean_time = now;
2540 	mutex_exit(&ill->ill_lock);
2541 
2542 	/*
2543 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2544 	 */
2545 	if (ill->ill_frag_free_num_pkts != 0) {
2546 		int ix;
2547 
2548 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2549 			ipfb = &ill->ill_frag_hash_tbl[ix];
2550 			mutex_enter(&ipfb->ipfb_lock);
2551 			if (ipfb->ipfb_ipf != NULL) {
2552 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2553 				    ill->ill_frag_free_num_pkts);
2554 			}
2555 			mutex_exit(&ipfb->ipfb_lock);
2556 		}
2557 	}
2558 	/*
2559 	 * While the reassembly list for this ILL is too big, prune a fragment
2560 	 * queue by age, oldest first.
2561 	 */
2562 	while (ill->ill_frag_count > max_count) {
2563 		int	ix;
2564 		ipfb_t	*oipfb = NULL;
2565 		uint_t	oldest = UINT_MAX;
2566 
2567 		count = 0;
2568 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2569 			ipfb = &ill->ill_frag_hash_tbl[ix];
2570 			mutex_enter(&ipfb->ipfb_lock);
2571 			ipf = ipfb->ipfb_ipf;
2572 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2573 				oldest = ipf->ipf_gen;
2574 				oipfb = ipfb;
2575 			}
2576 			count += ipfb->ipfb_count;
2577 			mutex_exit(&ipfb->ipfb_lock);
2578 		}
2579 		if (oipfb == NULL)
2580 			break;
2581 
2582 		if (count <= max_count)
2583 			return;	/* Somebody beat us to it, nothing to do */
2584 		mutex_enter(&oipfb->ipfb_lock);
2585 		ipf = oipfb->ipfb_ipf;
2586 		if (ipf != NULL) {
2587 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2588 		}
2589 		mutex_exit(&oipfb->ipfb_lock);
2590 	}
2591 }
2592 
2593 /*
2594  * free 'free_cnt' fragmented packets starting at ipf.
2595  */
2596 void
2597 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2598 {
2599 	size_t	count;
2600 	mblk_t	*mp;
2601 	mblk_t	*tmp;
2602 	ipf_t **ipfp = ipf->ipf_ptphn;
2603 
2604 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2605 	ASSERT(ipfp != NULL);
2606 	ASSERT(ipf != NULL);
2607 
2608 	while (ipf != NULL && free_cnt-- > 0) {
2609 		count = ipf->ipf_count;
2610 		mp = ipf->ipf_mp;
2611 		ipf = ipf->ipf_hash_next;
2612 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2613 			IP_REASS_SET_START(tmp, 0);
2614 			IP_REASS_SET_END(tmp, 0);
2615 		}
2616 		atomic_add_32(&ill->ill_frag_count, -count);
2617 		ASSERT(ipfb->ipfb_count >= count);
2618 		ipfb->ipfb_count -= count;
2619 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2620 		ipfb->ipfb_frag_pkts--;
2621 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2622 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2623 		freemsg(mp);
2624 	}
2625 
2626 	if (ipf)
2627 		ipf->ipf_ptphn = ipfp;
2628 	ipfp[0] = ipf;
2629 }
2630 
2631 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2632 	"obsolete and may be removed in a future release of Solaris.  Use " \
2633 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2634 
2635 /*
2636  * For obsolete per-interface forwarding configuration;
2637  * called in response to ND_GET.
2638  */
2639 /* ARGSUSED */
2640 static int
2641 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2642 {
2643 	ill_t *ill = (ill_t *)cp;
2644 
2645 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2646 
2647 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2648 	return (0);
2649 }
2650 
2651 /*
2652  * For obsolete per-interface forwarding configuration;
2653  * called in response to ND_SET.
2654  */
2655 /* ARGSUSED */
2656 static int
2657 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2658     cred_t *ioc_cr)
2659 {
2660 	long value;
2661 	int retval;
2662 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2663 
2664 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2665 
2666 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2667 	    value < 0 || value > 1) {
2668 		return (EINVAL);
2669 	}
2670 
2671 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2672 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2673 	rw_exit(&ipst->ips_ill_g_lock);
2674 	return (retval);
2675 }
2676 
2677 /*
2678  * Helper function for ill_forward_set().
2679  */
2680 static void
2681 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2682 {
2683 	ip_stack_t	*ipst = ill->ill_ipst;
2684 
2685 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2686 
2687 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2688 	    (enable ? "Enabling" : "Disabling"),
2689 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2690 	mutex_enter(&ill->ill_lock);
2691 	if (enable)
2692 		ill->ill_flags |= ILLF_ROUTER;
2693 	else
2694 		ill->ill_flags &= ~ILLF_ROUTER;
2695 	mutex_exit(&ill->ill_lock);
2696 	if (ill->ill_isv6)
2697 		ill_set_nce_router_flags(ill, enable);
2698 	/* Notify routing socket listeners of this change. */
2699 	if (ill->ill_ipif != NULL)
2700 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2701 }
2702 
2703 /*
2704  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2705  * socket messages for each interface whose flags we change.
2706  */
2707 int
2708 ill_forward_set(ill_t *ill, boolean_t enable)
2709 {
2710 	ipmp_illgrp_t *illg;
2711 	ip_stack_t *ipst = ill->ill_ipst;
2712 
2713 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2714 
2715 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2716 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2717 		return (0);
2718 
2719 	if (IS_LOOPBACK(ill))
2720 		return (EINVAL);
2721 
2722 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2723 		/*
2724 		 * Update all of the interfaces in the group.
2725 		 */
2726 		illg = ill->ill_grp;
2727 		ill = list_head(&illg->ig_if);
2728 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2729 			ill_forward_set_on_ill(ill, enable);
2730 
2731 		/*
2732 		 * Update the IPMP meta-interface.
2733 		 */
2734 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2735 		return (0);
2736 	}
2737 
2738 	ill_forward_set_on_ill(ill, enable);
2739 	return (0);
2740 }
2741 
2742 /*
2743  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2744  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2745  * set or clear.
2746  */
2747 static void
2748 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2749 {
2750 	ipif_t *ipif;
2751 	ncec_t *ncec;
2752 	nce_t *nce;
2753 
2754 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2755 		/*
2756 		 * NOTE: we match across the illgrp because nce's for
2757 		 * addresses on IPMP interfaces have an nce_ill that points to
2758 		 * the bound underlying ill.
2759 		 */
2760 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2761 		if (nce != NULL) {
2762 			ncec = nce->nce_common;
2763 			mutex_enter(&ncec->ncec_lock);
2764 			if (enable)
2765 				ncec->ncec_flags |= NCE_F_ISROUTER;
2766 			else
2767 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2768 			mutex_exit(&ncec->ncec_lock);
2769 			nce_refrele(nce);
2770 		}
2771 	}
2772 }
2773 
2774 /*
2775  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2776  * for this ill.  Make sure the v6/v4 question has been answered about this
2777  * ill.  The creation of this ndd variable is only for backwards compatibility.
2778  * The preferred way to control per-interface IP forwarding is through the
2779  * ILLF_ROUTER interface flag.
2780  */
2781 static int
2782 ill_set_ndd_name(ill_t *ill)
2783 {
2784 	char *suffix;
2785 	ip_stack_t	*ipst = ill->ill_ipst;
2786 
2787 	ASSERT(IAM_WRITER_ILL(ill));
2788 
2789 	if (ill->ill_isv6)
2790 		suffix = ipv6_forward_suffix;
2791 	else
2792 		suffix = ipv4_forward_suffix;
2793 
2794 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2795 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2796 	/*
2797 	 * Copies over the '\0'.
2798 	 * Note that strlen(suffix) is always bounded.
2799 	 */
2800 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2801 	    strlen(suffix) + 1);
2802 
2803 	/*
2804 	 * Use of the nd table requires holding the reader lock.
2805 	 * Modifying the nd table thru nd_load/nd_unload requires
2806 	 * the writer lock.
2807 	 */
2808 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2809 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2810 	    nd_ill_forward_set, (caddr_t)ill)) {
2811 		/*
2812 		 * If the nd_load failed, it only meant that it could not
2813 		 * allocate a new bunch of room for further NDD expansion.
2814 		 * Because of that, the ill_ndd_name will be set to 0, and
2815 		 * this interface is at the mercy of the global ip_forwarding
2816 		 * variable.
2817 		 */
2818 		rw_exit(&ipst->ips_ip_g_nd_lock);
2819 		ill->ill_ndd_name = NULL;
2820 		return (ENOMEM);
2821 	}
2822 	rw_exit(&ipst->ips_ip_g_nd_lock);
2823 	return (0);
2824 }
2825 
2826 /*
2827  * Intializes the context structure and returns the first ill in the list
2828  * cuurently start_list and end_list can have values:
2829  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2830  * IP_V4_G_HEAD		Traverse IPV4 list only.
2831  * IP_V6_G_HEAD		Traverse IPV6 list only.
2832  */
2833 
2834 /*
2835  * We don't check for CONDEMNED ills here. Caller must do that if
2836  * necessary under the ill lock.
2837  */
2838 ill_t *
2839 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2840     ip_stack_t *ipst)
2841 {
2842 	ill_if_t *ifp;
2843 	ill_t *ill;
2844 	avl_tree_t *avl_tree;
2845 
2846 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2847 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2848 
2849 	/*
2850 	 * setup the lists to search
2851 	 */
2852 	if (end_list != MAX_G_HEADS) {
2853 		ctx->ctx_current_list = start_list;
2854 		ctx->ctx_last_list = end_list;
2855 	} else {
2856 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2857 		ctx->ctx_current_list = 0;
2858 	}
2859 
2860 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2861 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2862 		if (ifp != (ill_if_t *)
2863 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2864 			avl_tree = &ifp->illif_avl_by_ppa;
2865 			ill = avl_first(avl_tree);
2866 			/*
2867 			 * ill is guaranteed to be non NULL or ifp should have
2868 			 * not existed.
2869 			 */
2870 			ASSERT(ill != NULL);
2871 			return (ill);
2872 		}
2873 		ctx->ctx_current_list++;
2874 	}
2875 
2876 	return (NULL);
2877 }
2878 
2879 /*
2880  * returns the next ill in the list. ill_first() must have been called
2881  * before calling ill_next() or bad things will happen.
2882  */
2883 
2884 /*
2885  * We don't check for CONDEMNED ills here. Caller must do that if
2886  * necessary under the ill lock.
2887  */
2888 ill_t *
2889 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2890 {
2891 	ill_if_t *ifp;
2892 	ill_t *ill;
2893 	ip_stack_t	*ipst = lastill->ill_ipst;
2894 
2895 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2896 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2897 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2898 	    AVL_AFTER)) != NULL) {
2899 		return (ill);
2900 	}
2901 
2902 	/* goto next ill_ifp in the list. */
2903 	ifp = lastill->ill_ifptr->illif_next;
2904 
2905 	/* make sure not at end of circular list */
2906 	while (ifp ==
2907 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2908 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2909 			return (NULL);
2910 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2911 	}
2912 
2913 	return (avl_first(&ifp->illif_avl_by_ppa));
2914 }
2915 
2916 /*
2917  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2918  * The final number (PPA) must not have any leading zeros.  Upon success, a
2919  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2920  */
2921 static char *
2922 ill_get_ppa_ptr(char *name)
2923 {
2924 	int namelen = strlen(name);
2925 	int end_ndx = namelen - 1;
2926 	int ppa_ndx, i;
2927 
2928 	/*
2929 	 * Check that the first character is [a-zA-Z], and that the last
2930 	 * character is [0-9].
2931 	 */
2932 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2933 		return (NULL);
2934 
2935 	/*
2936 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2937 	 */
2938 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2939 		if (!isdigit(name[ppa_ndx - 1]))
2940 			break;
2941 
2942 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2943 		return (NULL);
2944 
2945 	/*
2946 	 * Check that the intermediate characters are [a-z0-9.]
2947 	 */
2948 	for (i = 1; i < ppa_ndx; i++) {
2949 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2950 		    name[i] != '.' && name[i] != '_') {
2951 			return (NULL);
2952 		}
2953 	}
2954 
2955 	return (name + ppa_ndx);
2956 }
2957 
2958 /*
2959  * use avl tree to locate the ill.
2960  */
2961 static ill_t *
2962 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2963 {
2964 	char *ppa_ptr = NULL;
2965 	int len;
2966 	uint_t ppa;
2967 	ill_t *ill = NULL;
2968 	ill_if_t *ifp;
2969 	int list;
2970 
2971 	/*
2972 	 * get ppa ptr
2973 	 */
2974 	if (isv6)
2975 		list = IP_V6_G_HEAD;
2976 	else
2977 		list = IP_V4_G_HEAD;
2978 
2979 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2980 		return (NULL);
2981 	}
2982 
2983 	len = ppa_ptr - name + 1;
2984 
2985 	ppa = stoi(&ppa_ptr);
2986 
2987 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2988 
2989 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2990 		/*
2991 		 * match is done on len - 1 as the name is not null
2992 		 * terminated it contains ppa in addition to the interface
2993 		 * name.
2994 		 */
2995 		if ((ifp->illif_name_len == len) &&
2996 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2997 			break;
2998 		} else {
2999 			ifp = ifp->illif_next;
3000 		}
3001 	}
3002 
3003 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3004 		/*
3005 		 * Even the interface type does not exist.
3006 		 */
3007 		return (NULL);
3008 	}
3009 
3010 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
3011 	if (ill != NULL) {
3012 		mutex_enter(&ill->ill_lock);
3013 		if (ILL_CAN_LOOKUP(ill)) {
3014 			ill_refhold_locked(ill);
3015 			mutex_exit(&ill->ill_lock);
3016 			return (ill);
3017 		}
3018 		mutex_exit(&ill->ill_lock);
3019 	}
3020 	return (NULL);
3021 }
3022 
3023 /*
3024  * comparison function for use with avl.
3025  */
3026 static int
3027 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3028 {
3029 	uint_t ppa;
3030 	uint_t ill_ppa;
3031 
3032 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3033 
3034 	ppa = *((uint_t *)ppa_ptr);
3035 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3036 	/*
3037 	 * We want the ill with the lowest ppa to be on the
3038 	 * top.
3039 	 */
3040 	if (ill_ppa < ppa)
3041 		return (1);
3042 	if (ill_ppa > ppa)
3043 		return (-1);
3044 	return (0);
3045 }
3046 
3047 /*
3048  * remove an interface type from the global list.
3049  */
3050 static void
3051 ill_delete_interface_type(ill_if_t *interface)
3052 {
3053 	ASSERT(interface != NULL);
3054 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3055 
3056 	avl_destroy(&interface->illif_avl_by_ppa);
3057 	if (interface->illif_ppa_arena != NULL)
3058 		vmem_destroy(interface->illif_ppa_arena);
3059 
3060 	remque(interface);
3061 
3062 	mi_free(interface);
3063 }
3064 
3065 /*
3066  * remove ill from the global list.
3067  */
3068 static void
3069 ill_glist_delete(ill_t *ill)
3070 {
3071 	ip_stack_t	*ipst;
3072 	phyint_t	*phyi;
3073 
3074 	if (ill == NULL)
3075 		return;
3076 	ipst = ill->ill_ipst;
3077 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3078 
3079 	/*
3080 	 * If the ill was never inserted into the AVL tree
3081 	 * we skip the if branch.
3082 	 */
3083 	if (ill->ill_ifptr != NULL) {
3084 		/*
3085 		 * remove from AVL tree and free ppa number
3086 		 */
3087 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3088 
3089 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3090 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3091 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3092 		}
3093 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3094 			ill_delete_interface_type(ill->ill_ifptr);
3095 		}
3096 
3097 		/*
3098 		 * Indicate ill is no longer in the list.
3099 		 */
3100 		ill->ill_ifptr = NULL;
3101 		ill->ill_name_length = 0;
3102 		ill->ill_name[0] = '\0';
3103 		ill->ill_ppa = UINT_MAX;
3104 	}
3105 
3106 	/* Generate one last event for this ill. */
3107 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3108 	    ill->ill_name_length);
3109 
3110 	ASSERT(ill->ill_phyint != NULL);
3111 	phyi = ill->ill_phyint;
3112 	ill->ill_phyint = NULL;
3113 
3114 	/*
3115 	 * ill_init allocates a phyint always to store the copy
3116 	 * of flags relevant to phyint. At that point in time, we could
3117 	 * not assign the name and hence phyint_illv4/v6 could not be
3118 	 * initialized. Later in ipif_set_values, we assign the name to
3119 	 * the ill, at which point in time we assign phyint_illv4/v6.
3120 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3121 	 */
3122 	if (ill->ill_flags & ILLF_IPV6)
3123 		phyi->phyint_illv6 = NULL;
3124 	else
3125 		phyi->phyint_illv4 = NULL;
3126 
3127 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3128 		rw_exit(&ipst->ips_ill_g_lock);
3129 		return;
3130 	}
3131 
3132 	/*
3133 	 * There are no ills left on this phyint; pull it out of the phyint
3134 	 * avl trees, and free it.
3135 	 */
3136 	if (phyi->phyint_ifindex > 0) {
3137 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3138 		    phyi);
3139 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3140 		    phyi);
3141 	}
3142 	rw_exit(&ipst->ips_ill_g_lock);
3143 
3144 	phyint_free(phyi);
3145 }
3146 
3147 /*
3148  * allocate a ppa, if the number of plumbed interfaces of this type are
3149  * less than ill_no_arena do a linear search to find a unused ppa.
3150  * When the number goes beyond ill_no_arena switch to using an arena.
3151  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3152  * is the return value for an error condition, so allocation starts at one
3153  * and is decremented by one.
3154  */
3155 static int
3156 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3157 {
3158 	ill_t *tmp_ill;
3159 	uint_t start, end;
3160 	int ppa;
3161 
3162 	if (ifp->illif_ppa_arena == NULL &&
3163 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3164 		/*
3165 		 * Create an arena.
3166 		 */
3167 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3168 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3169 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3170 			/* allocate what has already been assigned */
3171 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3172 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3173 		    tmp_ill, AVL_AFTER)) {
3174 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3175 			    1,		/* size */
3176 			    1,		/* align/quantum */
3177 			    0,		/* phase */
3178 			    0,		/* nocross */
3179 			    /* minaddr */
3180 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3181 			    /* maxaddr */
3182 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3183 			    VM_NOSLEEP|VM_FIRSTFIT);
3184 			if (ppa == 0) {
3185 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3186 				    " failed while switching"));
3187 				vmem_destroy(ifp->illif_ppa_arena);
3188 				ifp->illif_ppa_arena = NULL;
3189 				break;
3190 			}
3191 		}
3192 	}
3193 
3194 	if (ifp->illif_ppa_arena != NULL) {
3195 		if (ill->ill_ppa == UINT_MAX) {
3196 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3197 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3198 			if (ppa == 0)
3199 				return (EAGAIN);
3200 			ill->ill_ppa = --ppa;
3201 		} else {
3202 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3203 			    1, 		/* size */
3204 			    1, 		/* align/quantum */
3205 			    0, 		/* phase */
3206 			    0, 		/* nocross */
3207 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3208 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3209 			    VM_NOSLEEP|VM_FIRSTFIT);
3210 			/*
3211 			 * Most likely the allocation failed because
3212 			 * the requested ppa was in use.
3213 			 */
3214 			if (ppa == 0)
3215 				return (EEXIST);
3216 		}
3217 		return (0);
3218 	}
3219 
3220 	/*
3221 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3222 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3223 	 */
3224 	if (ill->ill_ppa == UINT_MAX) {
3225 		end = UINT_MAX - 1;
3226 		start = 0;
3227 	} else {
3228 		end = start = ill->ill_ppa;
3229 	}
3230 
3231 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3232 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3233 		if (start++ >= end) {
3234 			if (ill->ill_ppa == UINT_MAX)
3235 				return (EAGAIN);
3236 			else
3237 				return (EEXIST);
3238 		}
3239 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3240 	}
3241 	ill->ill_ppa = start;
3242 	return (0);
3243 }
3244 
3245 /*
3246  * Insert ill into the list of configured ill's. Once this function completes,
3247  * the ill is globally visible and is available through lookups. More precisely
3248  * this happens after the caller drops the ill_g_lock.
3249  */
3250 static int
3251 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3252 {
3253 	ill_if_t *ill_interface;
3254 	avl_index_t where = 0;
3255 	int error;
3256 	int name_length;
3257 	int index;
3258 	boolean_t check_length = B_FALSE;
3259 	ip_stack_t	*ipst = ill->ill_ipst;
3260 
3261 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3262 
3263 	name_length = mi_strlen(name) + 1;
3264 
3265 	if (isv6)
3266 		index = IP_V6_G_HEAD;
3267 	else
3268 		index = IP_V4_G_HEAD;
3269 
3270 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3271 	/*
3272 	 * Search for interface type based on name
3273 	 */
3274 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3275 		if ((ill_interface->illif_name_len == name_length) &&
3276 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3277 			break;
3278 		}
3279 		ill_interface = ill_interface->illif_next;
3280 	}
3281 
3282 	/*
3283 	 * Interface type not found, create one.
3284 	 */
3285 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3286 		ill_g_head_t ghead;
3287 
3288 		/*
3289 		 * allocate ill_if_t structure
3290 		 */
3291 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3292 		if (ill_interface == NULL) {
3293 			return (ENOMEM);
3294 		}
3295 
3296 		(void) strcpy(ill_interface->illif_name, name);
3297 		ill_interface->illif_name_len = name_length;
3298 
3299 		avl_create(&ill_interface->illif_avl_by_ppa,
3300 		    ill_compare_ppa, sizeof (ill_t),
3301 		    offsetof(struct ill_s, ill_avl_byppa));
3302 
3303 		/*
3304 		 * link the structure in the back to maintain order
3305 		 * of configuration for ifconfig output.
3306 		 */
3307 		ghead = ipst->ips_ill_g_heads[index];
3308 		insque(ill_interface, ghead.ill_g_list_tail);
3309 	}
3310 
3311 	if (ill->ill_ppa == UINT_MAX)
3312 		check_length = B_TRUE;
3313 
3314 	error = ill_alloc_ppa(ill_interface, ill);
3315 	if (error != 0) {
3316 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3317 			ill_delete_interface_type(ill->ill_ifptr);
3318 		return (error);
3319 	}
3320 
3321 	/*
3322 	 * When the ppa is choosen by the system, check that there is
3323 	 * enough space to insert ppa. if a specific ppa was passed in this
3324 	 * check is not required as the interface name passed in will have
3325 	 * the right ppa in it.
3326 	 */
3327 	if (check_length) {
3328 		/*
3329 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3330 		 */
3331 		char buf[sizeof (uint_t) * 3];
3332 
3333 		/*
3334 		 * convert ppa to string to calculate the amount of space
3335 		 * required for it in the name.
3336 		 */
3337 		numtos(ill->ill_ppa, buf);
3338 
3339 		/* Do we have enough space to insert ppa ? */
3340 
3341 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3342 			/* Free ppa and interface type struct */
3343 			if (ill_interface->illif_ppa_arena != NULL) {
3344 				vmem_free(ill_interface->illif_ppa_arena,
3345 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3346 			}
3347 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3348 				ill_delete_interface_type(ill->ill_ifptr);
3349 
3350 			return (EINVAL);
3351 		}
3352 	}
3353 
3354 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3355 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3356 
3357 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3358 	    &where);
3359 	ill->ill_ifptr = ill_interface;
3360 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3361 
3362 	ill_phyint_reinit(ill);
3363 	return (0);
3364 }
3365 
3366 /* Initialize the per phyint ipsq used for serialization */
3367 static boolean_t
3368 ipsq_init(ill_t *ill, boolean_t enter)
3369 {
3370 	ipsq_t  *ipsq;
3371 	ipxop_t	*ipx;
3372 
3373 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3374 		return (B_FALSE);
3375 
3376 	ill->ill_phyint->phyint_ipsq = ipsq;
3377 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3378 	ipx->ipx_ipsq = ipsq;
3379 	ipsq->ipsq_next = ipsq;
3380 	ipsq->ipsq_phyint = ill->ill_phyint;
3381 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3382 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3383 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3384 	if (enter) {
3385 		ipx->ipx_writer = curthread;
3386 		ipx->ipx_forced = B_FALSE;
3387 		ipx->ipx_reentry_cnt = 1;
3388 #ifdef DEBUG
3389 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3390 #endif
3391 	}
3392 	return (B_TRUE);
3393 }
3394 
3395 /*
3396  * ill_init is called by ip_open when a device control stream is opened.
3397  * It does a few initializations, and shoots a DL_INFO_REQ message down
3398  * to the driver.  The response is later picked up in ip_rput_dlpi and
3399  * used to set up default mechanisms for talking to the driver.  (Always
3400  * called as writer.)
3401  *
3402  * If this function returns error, ip_open will call ip_close which in
3403  * turn will call ill_delete to clean up any memory allocated here that
3404  * is not yet freed.
3405  */
3406 int
3407 ill_init(queue_t *q, ill_t *ill)
3408 {
3409 	int	count;
3410 	dl_info_req_t	*dlir;
3411 	mblk_t	*info_mp;
3412 	uchar_t *frag_ptr;
3413 
3414 	/*
3415 	 * The ill is initialized to zero by mi_alloc*(). In addition
3416 	 * some fields already contain valid values, initialized in
3417 	 * ip_open(), before we reach here.
3418 	 */
3419 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3420 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3421 	ill->ill_saved_ire_cnt = 0;
3422 
3423 	ill->ill_rq = q;
3424 	ill->ill_wq = WR(q);
3425 
3426 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3427 	    BPRI_HI);
3428 	if (info_mp == NULL)
3429 		return (ENOMEM);
3430 
3431 	/*
3432 	 * Allocate sufficient space to contain our fragment hash table and
3433 	 * the device name.
3434 	 */
3435 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3436 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3437 	if (frag_ptr == NULL) {
3438 		freemsg(info_mp);
3439 		return (ENOMEM);
3440 	}
3441 	ill->ill_frag_ptr = frag_ptr;
3442 	ill->ill_frag_free_num_pkts = 0;
3443 	ill->ill_last_frag_clean_time = 0;
3444 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3445 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3446 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3447 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3448 		    NULL, MUTEX_DEFAULT, NULL);
3449 	}
3450 
3451 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3452 	if (ill->ill_phyint == NULL) {
3453 		freemsg(info_mp);
3454 		mi_free(frag_ptr);
3455 		return (ENOMEM);
3456 	}
3457 
3458 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3459 	/*
3460 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3461 	 * at this point because of the following reason. If we can't
3462 	 * enter the ipsq at some point and cv_wait, the writer that
3463 	 * wakes us up tries to locate us using the list of all phyints
3464 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3465 	 * If we don't set it now, we risk a missed wakeup.
3466 	 */
3467 	ill->ill_phyint->phyint_illv4 = ill;
3468 	ill->ill_ppa = UINT_MAX;
3469 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3470 
3471 	ill_set_inputfn(ill);
3472 
3473 	if (!ipsq_init(ill, B_TRUE)) {
3474 		freemsg(info_mp);
3475 		mi_free(frag_ptr);
3476 		mi_free(ill->ill_phyint);
3477 		return (ENOMEM);
3478 	}
3479 
3480 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3481 
3482 	/* Frag queue limit stuff */
3483 	ill->ill_frag_count = 0;
3484 	ill->ill_ipf_gen = 0;
3485 
3486 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3487 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3488 	ill->ill_global_timer = INFINITY;
3489 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3490 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3491 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3492 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3493 
3494 	/*
3495 	 * Initialize IPv6 configuration variables.  The IP module is always
3496 	 * opened as an IPv4 module.  Instead tracking down the cases where
3497 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3498 	 * here for convenience, this has no effect until the ill is set to do
3499 	 * IPv6.
3500 	 */
3501 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3502 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3503 	ill->ill_max_buf = ND_MAX_Q;
3504 	ill->ill_refcnt = 0;
3505 
3506 	/* Send down the Info Request to the driver. */
3507 	info_mp->b_datap->db_type = M_PCPROTO;
3508 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3509 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3510 	dlir->dl_primitive = DL_INFO_REQ;
3511 
3512 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3513 
3514 	qprocson(q);
3515 	ill_dlpi_send(ill, info_mp);
3516 
3517 	return (0);
3518 }
3519 
3520 /*
3521  * ill_dls_info
3522  * creates datalink socket info from the device.
3523  */
3524 int
3525 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3526 {
3527 	size_t	len;
3528 
3529 	sdl->sdl_family = AF_LINK;
3530 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3531 	sdl->sdl_type = ill->ill_type;
3532 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3533 	len = strlen(sdl->sdl_data);
3534 	ASSERT(len < 256);
3535 	sdl->sdl_nlen = (uchar_t)len;
3536 	sdl->sdl_alen = ill->ill_phys_addr_length;
3537 	sdl->sdl_slen = 0;
3538 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3539 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3540 
3541 	return (sizeof (struct sockaddr_dl));
3542 }
3543 
3544 /*
3545  * ill_xarp_info
3546  * creates xarp info from the device.
3547  */
3548 static int
3549 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3550 {
3551 	sdl->sdl_family = AF_LINK;
3552 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3553 	sdl->sdl_type = ill->ill_type;
3554 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3555 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3556 	sdl->sdl_alen = ill->ill_phys_addr_length;
3557 	sdl->sdl_slen = 0;
3558 	return (sdl->sdl_nlen);
3559 }
3560 
3561 static int
3562 loopback_kstat_update(kstat_t *ksp, int rw)
3563 {
3564 	kstat_named_t *kn;
3565 	netstackid_t	stackid;
3566 	netstack_t	*ns;
3567 	ip_stack_t	*ipst;
3568 
3569 	if (ksp == NULL || ksp->ks_data == NULL)
3570 		return (EIO);
3571 
3572 	if (rw == KSTAT_WRITE)
3573 		return (EACCES);
3574 
3575 	kn = KSTAT_NAMED_PTR(ksp);
3576 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3577 
3578 	ns = netstack_find_by_stackid(stackid);
3579 	if (ns == NULL)
3580 		return (-1);
3581 
3582 	ipst = ns->netstack_ip;
3583 	if (ipst == NULL) {
3584 		netstack_rele(ns);
3585 		return (-1);
3586 	}
3587 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3588 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3589 	netstack_rele(ns);
3590 	return (0);
3591 }
3592 
3593 /*
3594  * Has ifindex been plumbed already?
3595  */
3596 static boolean_t
3597 phyint_exists(uint_t index, ip_stack_t *ipst)
3598 {
3599 	ASSERT(index != 0);
3600 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3601 
3602 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3603 	    &index, NULL) != NULL);
3604 }
3605 
3606 /* Pick a unique ifindex */
3607 boolean_t
3608 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3609 {
3610 	uint_t starting_index;
3611 
3612 	if (!ipst->ips_ill_index_wrap) {
3613 		*indexp = ipst->ips_ill_index++;
3614 		if (ipst->ips_ill_index == 0) {
3615 			/* Reached the uint_t limit Next time wrap  */
3616 			ipst->ips_ill_index_wrap = B_TRUE;
3617 		}
3618 		return (B_TRUE);
3619 	}
3620 
3621 	/*
3622 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3623 	 * at this point and don't want to call any function that attempts
3624 	 * to get the lock again.
3625 	 */
3626 	starting_index = ipst->ips_ill_index++;
3627 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3628 		if (ipst->ips_ill_index != 0 &&
3629 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3630 			/* found unused index - use it */
3631 			*indexp = ipst->ips_ill_index;
3632 			return (B_TRUE);
3633 		}
3634 	}
3635 
3636 	/*
3637 	 * all interface indicies are inuse.
3638 	 */
3639 	return (B_FALSE);
3640 }
3641 
3642 /*
3643  * Assign a unique interface index for the phyint.
3644  */
3645 static boolean_t
3646 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3647 {
3648 	ASSERT(phyi->phyint_ifindex == 0);
3649 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3650 }
3651 
3652 /*
3653  * Initialize the flags on `phyi' as per the provided mactype.
3654  */
3655 static void
3656 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3657 {
3658 	uint64_t flags = 0;
3659 
3660 	/*
3661 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3662 	 * we always presume the underlying hardware is working and set
3663 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3664 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3665 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3666 	 */
3667 	if (mactype == SUNW_DL_IPMP)
3668 		flags |= PHYI_FAILED;
3669 	else
3670 		flags |= PHYI_RUNNING;
3671 
3672 	switch (mactype) {
3673 	case SUNW_DL_VNI:
3674 		flags |= PHYI_VIRTUAL;
3675 		break;
3676 	case SUNW_DL_IPMP:
3677 		flags |= PHYI_IPMP;
3678 		break;
3679 	case DL_LOOP:
3680 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3681 		break;
3682 	}
3683 
3684 	mutex_enter(&phyi->phyint_lock);
3685 	phyi->phyint_flags |= flags;
3686 	mutex_exit(&phyi->phyint_lock);
3687 }
3688 
3689 /*
3690  * Return a pointer to the ill which matches the supplied name.  Note that
3691  * the ill name length includes the null termination character.  (May be
3692  * called as writer.)
3693  * If do_alloc and the interface is "lo0" it will be automatically created.
3694  * Cannot bump up reference on condemned ills. So dup detect can't be done
3695  * using this func.
3696  */
3697 ill_t *
3698 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3699     boolean_t *did_alloc, ip_stack_t *ipst)
3700 {
3701 	ill_t	*ill;
3702 	ipif_t	*ipif;
3703 	ipsq_t	*ipsq;
3704 	kstat_named_t	*kn;
3705 	boolean_t isloopback;
3706 	in6_addr_t ov6addr;
3707 
3708 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3709 
3710 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3711 	ill = ill_find_by_name(name, isv6, ipst);
3712 	rw_exit(&ipst->ips_ill_g_lock);
3713 	if (ill != NULL)
3714 		return (ill);
3715 
3716 	/*
3717 	 * Couldn't find it.  Does this happen to be a lookup for the
3718 	 * loopback device and are we allowed to allocate it?
3719 	 */
3720 	if (!isloopback || !do_alloc)
3721 		return (NULL);
3722 
3723 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3724 	ill = ill_find_by_name(name, isv6, ipst);
3725 	if (ill != NULL) {
3726 		rw_exit(&ipst->ips_ill_g_lock);
3727 		return (ill);
3728 	}
3729 
3730 	/* Create the loopback device on demand */
3731 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3732 	    sizeof (ipif_loopback_name), BPRI_MED));
3733 	if (ill == NULL)
3734 		goto done;
3735 
3736 	*ill = ill_null;
3737 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3738 	ill->ill_ipst = ipst;
3739 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3740 	netstack_hold(ipst->ips_netstack);
3741 	/*
3742 	 * For exclusive stacks we set the zoneid to zero
3743 	 * to make IP operate as if in the global zone.
3744 	 */
3745 	ill->ill_zoneid = GLOBAL_ZONEID;
3746 
3747 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3748 	if (ill->ill_phyint == NULL)
3749 		goto done;
3750 
3751 	if (isv6)
3752 		ill->ill_phyint->phyint_illv6 = ill;
3753 	else
3754 		ill->ill_phyint->phyint_illv4 = ill;
3755 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3756 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3757 
3758 	if (isv6) {
3759 		ill->ill_isv6 = B_TRUE;
3760 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3761 	} else {
3762 		ill->ill_max_frag = ip_loopback_mtuplus;
3763 	}
3764 	if (!ill_allocate_mibs(ill))
3765 		goto done;
3766 	ill->ill_current_frag = ill->ill_max_frag;
3767 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3768 	/*
3769 	 * ipif_loopback_name can't be pointed at directly because its used
3770 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3771 	 * from the glist, ill_glist_delete() sets the first character of
3772 	 * ill_name to '\0'.
3773 	 */
3774 	ill->ill_name = (char *)ill + sizeof (*ill);
3775 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3776 	ill->ill_name_length = sizeof (ipif_loopback_name);
3777 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3778 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3779 
3780 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3781 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3782 	ill->ill_global_timer = INFINITY;
3783 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3784 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3785 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3786 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3787 
3788 	/* No resolver here. */
3789 	ill->ill_net_type = IRE_LOOPBACK;
3790 
3791 	/* Initialize the ipsq */
3792 	if (!ipsq_init(ill, B_FALSE))
3793 		goto done;
3794 
3795 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3796 	if (ipif == NULL)
3797 		goto done;
3798 
3799 	ill->ill_flags = ILLF_MULTICAST;
3800 
3801 	ov6addr = ipif->ipif_v6lcl_addr;
3802 	/* Set up default loopback address and mask. */
3803 	if (!isv6) {
3804 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3805 
3806 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3807 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3808 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3809 		    ipif->ipif_v6subnet);
3810 		ill->ill_flags |= ILLF_IPV4;
3811 	} else {
3812 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3813 		ipif->ipif_v6net_mask = ipv6_all_ones;
3814 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3815 		    ipif->ipif_v6subnet);
3816 		ill->ill_flags |= ILLF_IPV6;
3817 	}
3818 
3819 	/*
3820 	 * Chain us in at the end of the ill list. hold the ill
3821 	 * before we make it globally visible. 1 for the lookup.
3822 	 */
3823 	ill->ill_refcnt = 0;
3824 	ill_refhold(ill);
3825 
3826 	ill->ill_frag_count = 0;
3827 	ill->ill_frag_free_num_pkts = 0;
3828 	ill->ill_last_frag_clean_time = 0;
3829 
3830 	ipsq = ill->ill_phyint->phyint_ipsq;
3831 
3832 	ill_set_inputfn(ill);
3833 
3834 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3835 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3836 
3837 	/* Let SCTP know so that it can add this to its list */
3838 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3839 
3840 	/*
3841 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3842 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3843 	 * requires to be after ill_glist_insert() since we need the
3844 	 * ill_index set. Pass on ipv6_loopback as the old address.
3845 	 */
3846 	sctp_update_ipif_addr(ipif, ov6addr);
3847 
3848 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3849 
3850 	/*
3851 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3852 	 * If so, free our original one.
3853 	 */
3854 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3855 		ipsq_delete(ipsq);
3856 
3857 	if (ipst->ips_loopback_ksp == NULL) {
3858 		/* Export loopback interface statistics */
3859 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3860 		    ipif_loopback_name, "net",
3861 		    KSTAT_TYPE_NAMED, 2, 0,
3862 		    ipst->ips_netstack->netstack_stackid);
3863 		if (ipst->ips_loopback_ksp != NULL) {
3864 			ipst->ips_loopback_ksp->ks_update =
3865 			    loopback_kstat_update;
3866 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3867 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3868 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3869 			ipst->ips_loopback_ksp->ks_private =
3870 			    (void *)(uintptr_t)ipst->ips_netstack->
3871 			    netstack_stackid;
3872 			kstat_install(ipst->ips_loopback_ksp);
3873 		}
3874 	}
3875 
3876 	*did_alloc = B_TRUE;
3877 	rw_exit(&ipst->ips_ill_g_lock);
3878 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3879 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3880 	return (ill);
3881 done:
3882 	if (ill != NULL) {
3883 		if (ill->ill_phyint != NULL) {
3884 			ipsq = ill->ill_phyint->phyint_ipsq;
3885 			if (ipsq != NULL) {
3886 				ipsq->ipsq_phyint = NULL;
3887 				ipsq_delete(ipsq);
3888 			}
3889 			mi_free(ill->ill_phyint);
3890 		}
3891 		ill_free_mib(ill);
3892 		if (ill->ill_ipst != NULL)
3893 			netstack_rele(ill->ill_ipst->ips_netstack);
3894 		mi_free(ill);
3895 	}
3896 	rw_exit(&ipst->ips_ill_g_lock);
3897 	return (NULL);
3898 }
3899 
3900 /*
3901  * For IPP calls - use the ip_stack_t for global stack.
3902  */
3903 ill_t *
3904 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3905 {
3906 	ip_stack_t	*ipst;
3907 	ill_t		*ill;
3908 
3909 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3910 	if (ipst == NULL) {
3911 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3912 		return (NULL);
3913 	}
3914 
3915 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3916 	netstack_rele(ipst->ips_netstack);
3917 	return (ill);
3918 }
3919 
3920 /*
3921  * Return a pointer to the ill which matches the index and IP version type.
3922  */
3923 ill_t *
3924 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3925 {
3926 	ill_t	*ill;
3927 	phyint_t *phyi;
3928 
3929 	/*
3930 	 * Indexes are stored in the phyint - a common structure
3931 	 * to both IPv4 and IPv6.
3932 	 */
3933 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3934 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3935 	    (void *) &index, NULL);
3936 	if (phyi != NULL) {
3937 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3938 		if (ill != NULL) {
3939 			mutex_enter(&ill->ill_lock);
3940 			if (!ILL_IS_CONDEMNED(ill)) {
3941 				ill_refhold_locked(ill);
3942 				mutex_exit(&ill->ill_lock);
3943 				rw_exit(&ipst->ips_ill_g_lock);
3944 				return (ill);
3945 			}
3946 			mutex_exit(&ill->ill_lock);
3947 		}
3948 	}
3949 	rw_exit(&ipst->ips_ill_g_lock);
3950 	return (NULL);
3951 }
3952 
3953 /*
3954  * Verify whether or not an interface index is valid for the specified zoneid
3955  * to transmit packets.
3956  * It can be zero (meaning "reset") or an interface index assigned
3957  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3958  */
3959 boolean_t
3960 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3961     ip_stack_t *ipst)
3962 {
3963 	ill_t		*ill;
3964 
3965 	if (ifindex == 0)
3966 		return (B_TRUE);
3967 
3968 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3969 	if (ill == NULL)
3970 		return (B_FALSE);
3971 	if (IS_VNI(ill)) {
3972 		ill_refrele(ill);
3973 		return (B_FALSE);
3974 	}
3975 	ill_refrele(ill);
3976 	return (B_TRUE);
3977 }
3978 
3979 /*
3980  * Return the ifindex next in sequence after the passed in ifindex.
3981  * If there is no next ifindex for the given protocol, return 0.
3982  */
3983 uint_t
3984 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3985 {
3986 	phyint_t *phyi;
3987 	phyint_t *phyi_initial;
3988 	uint_t   ifindex;
3989 
3990 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3991 
3992 	if (index == 0) {
3993 		phyi = avl_first(
3994 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3995 	} else {
3996 		phyi = phyi_initial = avl_find(
3997 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3998 		    (void *) &index, NULL);
3999 	}
4000 
4001 	for (; phyi != NULL;
4002 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4003 	    phyi, AVL_AFTER)) {
4004 		/*
4005 		 * If we're not returning the first interface in the tree
4006 		 * and we still haven't moved past the phyint_t that
4007 		 * corresponds to index, avl_walk needs to be called again
4008 		 */
4009 		if (!((index != 0) && (phyi == phyi_initial))) {
4010 			if (isv6) {
4011 				if ((phyi->phyint_illv6) &&
4012 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
4013 				    (phyi->phyint_illv6->ill_isv6 == 1))
4014 					break;
4015 			} else {
4016 				if ((phyi->phyint_illv4) &&
4017 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
4018 				    (phyi->phyint_illv4->ill_isv6 == 0))
4019 					break;
4020 			}
4021 		}
4022 	}
4023 
4024 	rw_exit(&ipst->ips_ill_g_lock);
4025 
4026 	if (phyi != NULL)
4027 		ifindex = phyi->phyint_ifindex;
4028 	else
4029 		ifindex = 0;
4030 
4031 	return (ifindex);
4032 }
4033 
4034 /*
4035  * Return the ifindex for the named interface.
4036  * If there is no next ifindex for the interface, return 0.
4037  */
4038 uint_t
4039 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4040 {
4041 	phyint_t	*phyi;
4042 	avl_index_t	where = 0;
4043 	uint_t		ifindex;
4044 
4045 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4046 
4047 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4048 	    name, &where)) == NULL) {
4049 		rw_exit(&ipst->ips_ill_g_lock);
4050 		return (0);
4051 	}
4052 
4053 	ifindex = phyi->phyint_ifindex;
4054 
4055 	rw_exit(&ipst->ips_ill_g_lock);
4056 
4057 	return (ifindex);
4058 }
4059 
4060 /*
4061  * Return the ifindex to be used by upper layer protocols for instance
4062  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4063  */
4064 uint_t
4065 ill_get_upper_ifindex(const ill_t *ill)
4066 {
4067 	if (IS_UNDER_IPMP(ill))
4068 		return (ipmp_ill_get_ipmp_ifindex(ill));
4069 	else
4070 		return (ill->ill_phyint->phyint_ifindex);
4071 }
4072 
4073 
4074 /*
4075  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4076  * that gives a running thread a reference to the ill. This reference must be
4077  * released by the thread when it is done accessing the ill and related
4078  * objects. ill_refcnt can not be used to account for static references
4079  * such as other structures pointing to an ill. Callers must generally
4080  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4081  * or be sure that the ill is not being deleted or changing state before
4082  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4083  * ill won't change any of its critical state such as address, netmask etc.
4084  */
4085 void
4086 ill_refhold(ill_t *ill)
4087 {
4088 	mutex_enter(&ill->ill_lock);
4089 	ill->ill_refcnt++;
4090 	ILL_TRACE_REF(ill);
4091 	mutex_exit(&ill->ill_lock);
4092 }
4093 
4094 void
4095 ill_refhold_locked(ill_t *ill)
4096 {
4097 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4098 	ill->ill_refcnt++;
4099 	ILL_TRACE_REF(ill);
4100 }
4101 
4102 /* Returns true if we managed to get a refhold */
4103 boolean_t
4104 ill_check_and_refhold(ill_t *ill)
4105 {
4106 	mutex_enter(&ill->ill_lock);
4107 	if (!ILL_IS_CONDEMNED(ill)) {
4108 		ill_refhold_locked(ill);
4109 		mutex_exit(&ill->ill_lock);
4110 		return (B_TRUE);
4111 	}
4112 	mutex_exit(&ill->ill_lock);
4113 	return (B_FALSE);
4114 }
4115 
4116 /*
4117  * Must not be called while holding any locks. Otherwise if this is
4118  * the last reference to be released, there is a chance of recursive mutex
4119  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4120  * to restart an ioctl.
4121  */
4122 void
4123 ill_refrele(ill_t *ill)
4124 {
4125 	mutex_enter(&ill->ill_lock);
4126 	ASSERT(ill->ill_refcnt != 0);
4127 	ill->ill_refcnt--;
4128 	ILL_UNTRACE_REF(ill);
4129 	if (ill->ill_refcnt != 0) {
4130 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4131 		mutex_exit(&ill->ill_lock);
4132 		return;
4133 	}
4134 
4135 	/* Drops the ill_lock */
4136 	ipif_ill_refrele_tail(ill);
4137 }
4138 
4139 /*
4140  * Obtain a weak reference count on the ill. This reference ensures the
4141  * ill won't be freed, but the ill may change any of its critical state
4142  * such as netmask, address etc. Returns an error if the ill has started
4143  * closing.
4144  */
4145 boolean_t
4146 ill_waiter_inc(ill_t *ill)
4147 {
4148 	mutex_enter(&ill->ill_lock);
4149 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4150 		mutex_exit(&ill->ill_lock);
4151 		return (B_FALSE);
4152 	}
4153 	ill->ill_waiters++;
4154 	mutex_exit(&ill->ill_lock);
4155 	return (B_TRUE);
4156 }
4157 
4158 void
4159 ill_waiter_dcr(ill_t *ill)
4160 {
4161 	mutex_enter(&ill->ill_lock);
4162 	ill->ill_waiters--;
4163 	if (ill->ill_waiters == 0)
4164 		cv_broadcast(&ill->ill_cv);
4165 	mutex_exit(&ill->ill_lock);
4166 }
4167 
4168 /*
4169  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4170  * driver.  We construct best guess defaults for lower level information that
4171  * we need.  If an interface is brought up without injection of any overriding
4172  * information from outside, we have to be ready to go with these defaults.
4173  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4174  * we primarely want the dl_provider_style.
4175  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4176  * at which point we assume the other part of the information is valid.
4177  */
4178 void
4179 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4180 {
4181 	uchar_t		*brdcst_addr;
4182 	uint_t		brdcst_addr_length, phys_addr_length;
4183 	t_scalar_t	sap_length;
4184 	dl_info_ack_t	*dlia;
4185 	ip_m_t		*ipm;
4186 	dl_qos_cl_sel1_t *sel1;
4187 	int		min_mtu;
4188 
4189 	ASSERT(IAM_WRITER_ILL(ill));
4190 
4191 	/*
4192 	 * Till the ill is fully up  the ill is not globally visible.
4193 	 * So no need for a lock.
4194 	 */
4195 	dlia = (dl_info_ack_t *)mp->b_rptr;
4196 	ill->ill_mactype = dlia->dl_mac_type;
4197 
4198 	ipm = ip_m_lookup(dlia->dl_mac_type);
4199 	if (ipm == NULL) {
4200 		ipm = ip_m_lookup(DL_OTHER);
4201 		ASSERT(ipm != NULL);
4202 	}
4203 	ill->ill_media = ipm;
4204 
4205 	/*
4206 	 * When the new DLPI stuff is ready we'll pull lengths
4207 	 * from dlia.
4208 	 */
4209 	if (dlia->dl_version == DL_VERSION_2) {
4210 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4211 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4212 		    brdcst_addr_length);
4213 		if (brdcst_addr == NULL) {
4214 			brdcst_addr_length = 0;
4215 		}
4216 		sap_length = dlia->dl_sap_length;
4217 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4218 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4219 		    brdcst_addr_length, sap_length, phys_addr_length));
4220 	} else {
4221 		brdcst_addr_length = 6;
4222 		brdcst_addr = ip_six_byte_all_ones;
4223 		sap_length = -2;
4224 		phys_addr_length = brdcst_addr_length;
4225 	}
4226 
4227 	ill->ill_bcast_addr_length = brdcst_addr_length;
4228 	ill->ill_phys_addr_length = phys_addr_length;
4229 	ill->ill_sap_length = sap_length;
4230 
4231 	/*
4232 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4233 	 * but we must ensure a minimum IP MTU is used since other bits of
4234 	 * IP will fly apart otherwise.
4235 	 */
4236 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4237 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4238 	ill->ill_current_frag = ill->ill_max_frag;
4239 	ill->ill_mtu = ill->ill_max_frag;
4240 
4241 	ill->ill_type = ipm->ip_m_type;
4242 
4243 	if (!ill->ill_dlpi_style_set) {
4244 		if (dlia->dl_provider_style == DL_STYLE2)
4245 			ill->ill_needs_attach = 1;
4246 
4247 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4248 
4249 		/*
4250 		 * Allocate the first ipif on this ill.  We don't delay it
4251 		 * further as ioctl handling assumes at least one ipif exists.
4252 		 *
4253 		 * At this point we don't know whether the ill is v4 or v6.
4254 		 * We will know this whan the SIOCSLIFNAME happens and
4255 		 * the correct value for ill_isv6 will be assigned in
4256 		 * ipif_set_values(). We need to hold the ill lock and
4257 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4258 		 * the wakeup.
4259 		 */
4260 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4261 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4262 		mutex_enter(&ill->ill_lock);
4263 		ASSERT(ill->ill_dlpi_style_set == 0);
4264 		ill->ill_dlpi_style_set = 1;
4265 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4266 		cv_broadcast(&ill->ill_cv);
4267 		mutex_exit(&ill->ill_lock);
4268 		freemsg(mp);
4269 		return;
4270 	}
4271 	ASSERT(ill->ill_ipif != NULL);
4272 	/*
4273 	 * We know whether it is IPv4 or IPv6 now, as this is the
4274 	 * second DL_INFO_ACK we are recieving in response to the
4275 	 * DL_INFO_REQ sent in ipif_set_values.
4276 	 */
4277 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4278 	/*
4279 	 * Clear all the flags that were set based on ill_bcast_addr_length
4280 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4281 	 * changed now and we need to re-evaluate.
4282 	 */
4283 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4284 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4285 
4286 	/*
4287 	 * Free ill_bcast_mp as things could have changed now.
4288 	 *
4289 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4290 	 * with no underlying interfaces (and thus an unknown broadcast
4291 	 * address length), but we enforce that an interface is broadcast-
4292 	 * capable as part of allowing it to join a group.
4293 	 */
4294 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4295 		if (ill->ill_bcast_mp != NULL)
4296 			freemsg(ill->ill_bcast_mp);
4297 		ill->ill_net_type = IRE_IF_NORESOLVER;
4298 
4299 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4300 		    ill->ill_phys_addr_length,
4301 		    ill->ill_sap,
4302 		    ill->ill_sap_length);
4303 
4304 		if (ill->ill_isv6)
4305 			/*
4306 			 * Note: xresolv interfaces will eventually need NOARP
4307 			 * set here as well, but that will require those
4308 			 * external resolvers to have some knowledge of
4309 			 * that flag and act appropriately. Not to be changed
4310 			 * at present.
4311 			 */
4312 			ill->ill_flags |= ILLF_NONUD;
4313 		else
4314 			ill->ill_flags |= ILLF_NOARP;
4315 
4316 		if (ill->ill_mactype == SUNW_DL_VNI) {
4317 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4318 		} else if (ill->ill_phys_addr_length == 0 ||
4319 		    ill->ill_mactype == DL_IPV4 ||
4320 		    ill->ill_mactype == DL_IPV6) {
4321 			/*
4322 			 * The underying link is point-to-point, so mark the
4323 			 * interface as such.  We can do IP multicast over
4324 			 * such a link since it transmits all network-layer
4325 			 * packets to the remote side the same way.
4326 			 */
4327 			ill->ill_flags |= ILLF_MULTICAST;
4328 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4329 		}
4330 	} else {
4331 		ill->ill_net_type = IRE_IF_RESOLVER;
4332 		if (ill->ill_bcast_mp != NULL)
4333 			freemsg(ill->ill_bcast_mp);
4334 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4335 		    ill->ill_bcast_addr_length, ill->ill_sap,
4336 		    ill->ill_sap_length);
4337 		/*
4338 		 * Later detect lack of DLPI driver multicast
4339 		 * capability by catching DL_ENABMULTI errors in
4340 		 * ip_rput_dlpi.
4341 		 */
4342 		ill->ill_flags |= ILLF_MULTICAST;
4343 		if (!ill->ill_isv6)
4344 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4345 	}
4346 
4347 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4348 	if (ill->ill_mactype == SUNW_DL_IPMP)
4349 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4350 
4351 	/* By default an interface does not support any CoS marking */
4352 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4353 
4354 	/*
4355 	 * If we get QoS information in DL_INFO_ACK, the device supports
4356 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4357 	 */
4358 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4359 	    dlia->dl_qos_length);
4360 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4361 		ill->ill_flags |= ILLF_COS_ENABLED;
4362 	}
4363 
4364 	/* Clear any previous error indication. */
4365 	ill->ill_error = 0;
4366 	freemsg(mp);
4367 }
4368 
4369 /*
4370  * Perform various checks to verify that an address would make sense as a
4371  * local, remote, or subnet interface address.
4372  */
4373 static boolean_t
4374 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4375 {
4376 	ipaddr_t	net_mask;
4377 
4378 	/*
4379 	 * Don't allow all zeroes, or all ones, but allow
4380 	 * all ones netmask.
4381 	 */
4382 	if ((net_mask = ip_net_mask(addr)) == 0)
4383 		return (B_FALSE);
4384 	/* A given netmask overrides the "guess" netmask */
4385 	if (subnet_mask != 0)
4386 		net_mask = subnet_mask;
4387 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4388 	    (addr == (addr | ~net_mask)))) {
4389 		return (B_FALSE);
4390 	}
4391 
4392 	/*
4393 	 * Even if the netmask is all ones, we do not allow address to be
4394 	 * 255.255.255.255
4395 	 */
4396 	if (addr == INADDR_BROADCAST)
4397 		return (B_FALSE);
4398 
4399 	if (CLASSD(addr))
4400 		return (B_FALSE);
4401 
4402 	return (B_TRUE);
4403 }
4404 
4405 #define	V6_IPIF_LINKLOCAL(p)	\
4406 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4407 
4408 /*
4409  * Compare two given ipifs and check if the second one is better than
4410  * the first one using the order of preference (not taking deprecated
4411  * into acount) specified in ipif_lookup_multicast().
4412  */
4413 static boolean_t
4414 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4415 {
4416 	/* Check the least preferred first. */
4417 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4418 		/* If both ipifs are the same, use the first one. */
4419 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4420 			return (B_FALSE);
4421 		else
4422 			return (B_TRUE);
4423 	}
4424 
4425 	/* For IPv6, check for link local address. */
4426 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4427 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4428 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4429 			/* The second one is equal or less preferred. */
4430 			return (B_FALSE);
4431 		} else {
4432 			return (B_TRUE);
4433 		}
4434 	}
4435 
4436 	/* Then check for point to point interface. */
4437 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4438 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4439 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4440 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4441 			return (B_FALSE);
4442 		} else {
4443 			return (B_TRUE);
4444 		}
4445 	}
4446 
4447 	/* old_ipif is a normal interface, so no need to use the new one. */
4448 	return (B_FALSE);
4449 }
4450 
4451 /*
4452  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4453  * The ipif must be up, and its ill must multicast-capable, not
4454  * condemned, not an underlying interface in an IPMP group, and
4455  * not a VNI interface.  Order of preference:
4456  *
4457  * 	1a. normal
4458  * 	1b. normal, but deprecated
4459  * 	2a. point to point
4460  * 	2b. point to point, but deprecated
4461  * 	3a. link local
4462  * 	3b. link local, but deprecated
4463  * 	4. loopback.
4464  */
4465 static ipif_t *
4466 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4467 {
4468 	ill_t			*ill;
4469 	ill_walk_context_t	ctx;
4470 	ipif_t			*ipif;
4471 	ipif_t			*saved_ipif = NULL;
4472 	ipif_t			*dep_ipif = NULL;
4473 
4474 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4475 	if (isv6)
4476 		ill = ILL_START_WALK_V6(&ctx, ipst);
4477 	else
4478 		ill = ILL_START_WALK_V4(&ctx, ipst);
4479 
4480 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4481 		mutex_enter(&ill->ill_lock);
4482 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4483 		    ILL_IS_CONDEMNED(ill) ||
4484 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4485 			mutex_exit(&ill->ill_lock);
4486 			continue;
4487 		}
4488 		for (ipif = ill->ill_ipif; ipif != NULL;
4489 		    ipif = ipif->ipif_next) {
4490 			if (zoneid != ipif->ipif_zoneid &&
4491 			    zoneid != ALL_ZONES &&
4492 			    ipif->ipif_zoneid != ALL_ZONES) {
4493 				continue;
4494 			}
4495 			if (!(ipif->ipif_flags & IPIF_UP) ||
4496 			    IPIF_IS_CONDEMNED(ipif)) {
4497 				continue;
4498 			}
4499 
4500 			/*
4501 			 * Found one candidate.  If it is deprecated,
4502 			 * remember it in dep_ipif.  If it is not deprecated,
4503 			 * remember it in saved_ipif.
4504 			 */
4505 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4506 				if (dep_ipif == NULL) {
4507 					dep_ipif = ipif;
4508 				} else if (ipif_comp_multi(dep_ipif, ipif,
4509 				    isv6)) {
4510 					/*
4511 					 * If the previous dep_ipif does not
4512 					 * belong to the same ill, we've done
4513 					 * a ipif_refhold() on it.  So we need
4514 					 * to release it.
4515 					 */
4516 					if (dep_ipif->ipif_ill != ill)
4517 						ipif_refrele(dep_ipif);
4518 					dep_ipif = ipif;
4519 				}
4520 				continue;
4521 			}
4522 			if (saved_ipif == NULL) {
4523 				saved_ipif = ipif;
4524 			} else {
4525 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4526 					if (saved_ipif->ipif_ill != ill)
4527 						ipif_refrele(saved_ipif);
4528 					saved_ipif = ipif;
4529 				}
4530 			}
4531 		}
4532 		/*
4533 		 * Before going to the next ill, do a ipif_refhold() on the
4534 		 * saved ones.
4535 		 */
4536 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4537 			ipif_refhold_locked(saved_ipif);
4538 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4539 			ipif_refhold_locked(dep_ipif);
4540 		mutex_exit(&ill->ill_lock);
4541 	}
4542 	rw_exit(&ipst->ips_ill_g_lock);
4543 
4544 	/*
4545 	 * If we have only the saved_ipif, return it.  But if we have both
4546 	 * saved_ipif and dep_ipif, check to see which one is better.
4547 	 */
4548 	if (saved_ipif != NULL) {
4549 		if (dep_ipif != NULL) {
4550 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4551 				ipif_refrele(saved_ipif);
4552 				return (dep_ipif);
4553 			} else {
4554 				ipif_refrele(dep_ipif);
4555 				return (saved_ipif);
4556 			}
4557 		}
4558 		return (saved_ipif);
4559 	} else {
4560 		return (dep_ipif);
4561 	}
4562 }
4563 
4564 ill_t *
4565 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4566 {
4567 	ipif_t *ipif;
4568 	ill_t *ill;
4569 
4570 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4571 	if (ipif == NULL)
4572 		return (NULL);
4573 
4574 	ill = ipif->ipif_ill;
4575 	ill_refhold(ill);
4576 	ipif_refrele(ipif);
4577 	return (ill);
4578 }
4579 
4580 /*
4581  * This function is called when an application does not specify an interface
4582  * to be used for multicast traffic (joining a group/sending data).  It
4583  * calls ire_lookup_multi() to look for an interface route for the
4584  * specified multicast group.  Doing this allows the administrator to add
4585  * prefix routes for multicast to indicate which interface to be used for
4586  * multicast traffic in the above scenario.  The route could be for all
4587  * multicast (224.0/4), for a single multicast group (a /32 route) or
4588  * anything in between.  If there is no such multicast route, we just find
4589  * any multicast capable interface and return it.  The returned ipif
4590  * is refhold'ed.
4591  *
4592  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4593  * unicast table. This is used by CGTP.
4594  */
4595 ill_t *
4596 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4597     boolean_t *multirtp, ipaddr_t *setsrcp)
4598 {
4599 	ill_t			*ill;
4600 
4601 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4602 	if (ill != NULL)
4603 		return (ill);
4604 
4605 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4606 }
4607 
4608 /*
4609  * Look for an ipif with the specified interface address and destination.
4610  * The destination address is used only for matching point-to-point interfaces.
4611  */
4612 ipif_t *
4613 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4614 {
4615 	ipif_t	*ipif;
4616 	ill_t	*ill;
4617 	ill_walk_context_t ctx;
4618 
4619 	/*
4620 	 * First match all the point-to-point interfaces
4621 	 * before looking at non-point-to-point interfaces.
4622 	 * This is done to avoid returning non-point-to-point
4623 	 * ipif instead of unnumbered point-to-point ipif.
4624 	 */
4625 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4626 	ill = ILL_START_WALK_V4(&ctx, ipst);
4627 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4628 		mutex_enter(&ill->ill_lock);
4629 		for (ipif = ill->ill_ipif; ipif != NULL;
4630 		    ipif = ipif->ipif_next) {
4631 			/* Allow the ipif to be down */
4632 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4633 			    (ipif->ipif_lcl_addr == if_addr) &&
4634 			    (ipif->ipif_pp_dst_addr == dst)) {
4635 				if (!IPIF_IS_CONDEMNED(ipif)) {
4636 					ipif_refhold_locked(ipif);
4637 					mutex_exit(&ill->ill_lock);
4638 					rw_exit(&ipst->ips_ill_g_lock);
4639 					return (ipif);
4640 				}
4641 			}
4642 		}
4643 		mutex_exit(&ill->ill_lock);
4644 	}
4645 	rw_exit(&ipst->ips_ill_g_lock);
4646 
4647 	/* lookup the ipif based on interface address */
4648 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4649 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4650 	return (ipif);
4651 }
4652 
4653 /*
4654  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4655  */
4656 static ipif_t *
4657 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4658     zoneid_t zoneid, ip_stack_t *ipst)
4659 {
4660 	ipif_t  *ipif;
4661 	ill_t   *ill;
4662 	boolean_t ptp = B_FALSE;
4663 	ill_walk_context_t	ctx;
4664 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4665 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4666 
4667 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4668 	/*
4669 	 * Repeat twice, first based on local addresses and
4670 	 * next time for pointopoint.
4671 	 */
4672 repeat:
4673 	ill = ILL_START_WALK_V4(&ctx, ipst);
4674 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4675 		if (match_ill != NULL && ill != match_ill &&
4676 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4677 			continue;
4678 		}
4679 		mutex_enter(&ill->ill_lock);
4680 		for (ipif = ill->ill_ipif; ipif != NULL;
4681 		    ipif = ipif->ipif_next) {
4682 			if (zoneid != ALL_ZONES &&
4683 			    zoneid != ipif->ipif_zoneid &&
4684 			    ipif->ipif_zoneid != ALL_ZONES)
4685 				continue;
4686 
4687 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4688 				continue;
4689 
4690 			/* Allow the ipif to be down */
4691 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4692 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4693 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4694 			    (ipif->ipif_pp_dst_addr == addr))) {
4695 				if (!IPIF_IS_CONDEMNED(ipif)) {
4696 					ipif_refhold_locked(ipif);
4697 					mutex_exit(&ill->ill_lock);
4698 					rw_exit(&ipst->ips_ill_g_lock);
4699 					return (ipif);
4700 				}
4701 			}
4702 		}
4703 		mutex_exit(&ill->ill_lock);
4704 	}
4705 
4706 	/* If we already did the ptp case, then we are done */
4707 	if (ptp) {
4708 		rw_exit(&ipst->ips_ill_g_lock);
4709 		return (NULL);
4710 	}
4711 	ptp = B_TRUE;
4712 	goto repeat;
4713 }
4714 
4715 /*
4716  * Lookup an ipif with the specified address.  For point-to-point links we
4717  * look for matches on either the destination address or the local address,
4718  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4719  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4720  * (or illgrp if `match_ill' is in an IPMP group).
4721  */
4722 ipif_t *
4723 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4724     ip_stack_t *ipst)
4725 {
4726 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4727 	    zoneid, ipst));
4728 }
4729 
4730 /*
4731  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4732  * except that we will only return an address if it is not marked as
4733  * IPIF_DUPLICATE
4734  */
4735 ipif_t *
4736 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4737     ip_stack_t *ipst)
4738 {
4739 	return (ipif_lookup_addr_common(addr, match_ill,
4740 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4741 	    zoneid, ipst));
4742 }
4743 
4744 /*
4745  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4746  * `match_ill' across the IPMP group.  This function is only needed in some
4747  * corner-cases; almost everything should use ipif_lookup_addr().
4748  */
4749 ipif_t *
4750 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4751 {
4752 	ASSERT(match_ill != NULL);
4753 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4754 	    ipst));
4755 }
4756 
4757 /*
4758  * Look for an ipif with the specified address. For point-point links
4759  * we look for matches on either the destination address and the local
4760  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4761  * is set.
4762  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4763  * ill (or illgrp if `match_ill' is in an IPMP group).
4764  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4765  */
4766 zoneid_t
4767 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4768 {
4769 	zoneid_t zoneid;
4770 	ipif_t  *ipif;
4771 	ill_t   *ill;
4772 	boolean_t ptp = B_FALSE;
4773 	ill_walk_context_t	ctx;
4774 
4775 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4776 	/*
4777 	 * Repeat twice, first based on local addresses and
4778 	 * next time for pointopoint.
4779 	 */
4780 repeat:
4781 	ill = ILL_START_WALK_V4(&ctx, ipst);
4782 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4783 		if (match_ill != NULL && ill != match_ill &&
4784 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4785 			continue;
4786 		}
4787 		mutex_enter(&ill->ill_lock);
4788 		for (ipif = ill->ill_ipif; ipif != NULL;
4789 		    ipif = ipif->ipif_next) {
4790 			/* Allow the ipif to be down */
4791 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4792 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4793 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4794 			    (ipif->ipif_pp_dst_addr == addr)) &&
4795 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4796 				zoneid = ipif->ipif_zoneid;
4797 				mutex_exit(&ill->ill_lock);
4798 				rw_exit(&ipst->ips_ill_g_lock);
4799 				/*
4800 				 * If ipif_zoneid was ALL_ZONES then we have
4801 				 * a trusted extensions shared IP address.
4802 				 * In that case GLOBAL_ZONEID works to send.
4803 				 */
4804 				if (zoneid == ALL_ZONES)
4805 					zoneid = GLOBAL_ZONEID;
4806 				return (zoneid);
4807 			}
4808 		}
4809 		mutex_exit(&ill->ill_lock);
4810 	}
4811 
4812 	/* If we already did the ptp case, then we are done */
4813 	if (ptp) {
4814 		rw_exit(&ipst->ips_ill_g_lock);
4815 		return (ALL_ZONES);
4816 	}
4817 	ptp = B_TRUE;
4818 	goto repeat;
4819 }
4820 
4821 /*
4822  * Look for an ipif that matches the specified remote address i.e. the
4823  * ipif that would receive the specified packet.
4824  * First look for directly connected interfaces and then do a recursive
4825  * IRE lookup and pick the first ipif corresponding to the source address in the
4826  * ire.
4827  * Returns: held ipif
4828  *
4829  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4830  */
4831 ipif_t *
4832 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4833 {
4834 	ipif_t	*ipif;
4835 
4836 	ASSERT(!ill->ill_isv6);
4837 
4838 	/*
4839 	 * Someone could be changing this ipif currently or change it
4840 	 * after we return this. Thus  a few packets could use the old
4841 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4842 	 * will atomically be updated or cleaned up with the new value
4843 	 * Thus we don't need a lock to check the flags or other attrs below.
4844 	 */
4845 	mutex_enter(&ill->ill_lock);
4846 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4847 		if (IPIF_IS_CONDEMNED(ipif))
4848 			continue;
4849 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4850 		    ipif->ipif_zoneid != ALL_ZONES)
4851 			continue;
4852 		/* Allow the ipif to be down */
4853 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4854 			if ((ipif->ipif_pp_dst_addr == addr) ||
4855 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4856 			    ipif->ipif_lcl_addr == addr)) {
4857 				ipif_refhold_locked(ipif);
4858 				mutex_exit(&ill->ill_lock);
4859 				return (ipif);
4860 			}
4861 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4862 			ipif_refhold_locked(ipif);
4863 			mutex_exit(&ill->ill_lock);
4864 			return (ipif);
4865 		}
4866 	}
4867 	mutex_exit(&ill->ill_lock);
4868 	/*
4869 	 * For a remote destination it isn't possible to nail down a particular
4870 	 * ipif.
4871 	 */
4872 
4873 	/* Pick the first interface */
4874 	ipif = ipif_get_next_ipif(NULL, ill);
4875 	return (ipif);
4876 }
4877 
4878 /*
4879  * This func does not prevent refcnt from increasing. But if
4880  * the caller has taken steps to that effect, then this func
4881  * can be used to determine whether the ill has become quiescent
4882  */
4883 static boolean_t
4884 ill_is_quiescent(ill_t *ill)
4885 {
4886 	ipif_t	*ipif;
4887 
4888 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4889 
4890 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4891 		if (ipif->ipif_refcnt != 0)
4892 			return (B_FALSE);
4893 	}
4894 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4895 		return (B_FALSE);
4896 	}
4897 	return (B_TRUE);
4898 }
4899 
4900 boolean_t
4901 ill_is_freeable(ill_t *ill)
4902 {
4903 	ipif_t	*ipif;
4904 
4905 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4906 
4907 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4908 		if (ipif->ipif_refcnt != 0) {
4909 			return (B_FALSE);
4910 		}
4911 	}
4912 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4913 		return (B_FALSE);
4914 	}
4915 	return (B_TRUE);
4916 }
4917 
4918 /*
4919  * This func does not prevent refcnt from increasing. But if
4920  * the caller has taken steps to that effect, then this func
4921  * can be used to determine whether the ipif has become quiescent
4922  */
4923 static boolean_t
4924 ipif_is_quiescent(ipif_t *ipif)
4925 {
4926 	ill_t *ill;
4927 
4928 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4929 
4930 	if (ipif->ipif_refcnt != 0)
4931 		return (B_FALSE);
4932 
4933 	ill = ipif->ipif_ill;
4934 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4935 	    ill->ill_logical_down) {
4936 		return (B_TRUE);
4937 	}
4938 
4939 	/* This is the last ipif going down or being deleted on this ill */
4940 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4941 		return (B_FALSE);
4942 	}
4943 
4944 	return (B_TRUE);
4945 }
4946 
4947 /*
4948  * return true if the ipif can be destroyed: the ipif has to be quiescent
4949  * with zero references from ire/ilm to it.
4950  */
4951 static boolean_t
4952 ipif_is_freeable(ipif_t *ipif)
4953 {
4954 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4955 	ASSERT(ipif->ipif_id != 0);
4956 	return (ipif->ipif_refcnt == 0);
4957 }
4958 
4959 /*
4960  * The ipif/ill/ire has been refreled. Do the tail processing.
4961  * Determine if the ipif or ill in question has become quiescent and if so
4962  * wakeup close and/or restart any queued pending ioctl that is waiting
4963  * for the ipif_down (or ill_down)
4964  */
4965 void
4966 ipif_ill_refrele_tail(ill_t *ill)
4967 {
4968 	mblk_t	*mp;
4969 	conn_t	*connp;
4970 	ipsq_t	*ipsq;
4971 	ipxop_t	*ipx;
4972 	ipif_t	*ipif;
4973 	dl_notify_ind_t *dlindp;
4974 
4975 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4976 
4977 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4978 		/* ip_modclose() may be waiting */
4979 		cv_broadcast(&ill->ill_cv);
4980 	}
4981 
4982 	ipsq = ill->ill_phyint->phyint_ipsq;
4983 	mutex_enter(&ipsq->ipsq_lock);
4984 	ipx = ipsq->ipsq_xop;
4985 	mutex_enter(&ipx->ipx_lock);
4986 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4987 		goto unlock;
4988 
4989 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4990 
4991 	ipif = ipx->ipx_pending_ipif;
4992 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4993 		goto unlock;
4994 
4995 	switch (ipx->ipx_waitfor) {
4996 	case IPIF_DOWN:
4997 		if (!ipif_is_quiescent(ipif))
4998 			goto unlock;
4999 		break;
5000 	case IPIF_FREE:
5001 		if (!ipif_is_freeable(ipif))
5002 			goto unlock;
5003 		break;
5004 	case ILL_DOWN:
5005 		if (!ill_is_quiescent(ill))
5006 			goto unlock;
5007 		break;
5008 	case ILL_FREE:
5009 		/*
5010 		 * ILL_FREE is only for loopback; normal ill teardown waits
5011 		 * synchronously in ip_modclose() without using ipx_waitfor,
5012 		 * handled by the cv_broadcast() at the top of this function.
5013 		 */
5014 		if (!ill_is_freeable(ill))
5015 			goto unlock;
5016 		break;
5017 	default:
5018 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
5019 		    (void *)ipsq, ipx->ipx_waitfor);
5020 	}
5021 
5022 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
5023 	mutex_exit(&ipx->ipx_lock);
5024 	mp = ipsq_pending_mp_get(ipsq, &connp);
5025 	mutex_exit(&ipsq->ipsq_lock);
5026 	mutex_exit(&ill->ill_lock);
5027 
5028 	ASSERT(mp != NULL);
5029 	/*
5030 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5031 	 * we can only get here when the current operation decides it
5032 	 * it needs to quiesce via ipsq_pending_mp_add().
5033 	 */
5034 	switch (mp->b_datap->db_type) {
5035 	case M_PCPROTO:
5036 	case M_PROTO:
5037 		/*
5038 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5039 		 */
5040 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5041 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5042 
5043 		switch (dlindp->dl_notification) {
5044 		case DL_NOTE_PHYS_ADDR:
5045 			qwriter_ip(ill, ill->ill_rq, mp,
5046 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5047 			return;
5048 		case DL_NOTE_REPLUMB:
5049 			qwriter_ip(ill, ill->ill_rq, mp,
5050 			    ill_replumb_tail, CUR_OP, B_TRUE);
5051 			return;
5052 		default:
5053 			ASSERT(0);
5054 			ill_refrele(ill);
5055 		}
5056 		break;
5057 
5058 	case M_ERROR:
5059 	case M_HANGUP:
5060 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5061 		    B_TRUE);
5062 		return;
5063 
5064 	case M_IOCTL:
5065 	case M_IOCDATA:
5066 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5067 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5068 		return;
5069 
5070 	default:
5071 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5072 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5073 	}
5074 	return;
5075 unlock:
5076 	mutex_exit(&ipsq->ipsq_lock);
5077 	mutex_exit(&ipx->ipx_lock);
5078 	mutex_exit(&ill->ill_lock);
5079 }
5080 
5081 #ifdef DEBUG
5082 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5083 static void
5084 th_trace_rrecord(th_trace_t *th_trace)
5085 {
5086 	tr_buf_t *tr_buf;
5087 	uint_t lastref;
5088 
5089 	lastref = th_trace->th_trace_lastref;
5090 	lastref++;
5091 	if (lastref == TR_BUF_MAX)
5092 		lastref = 0;
5093 	th_trace->th_trace_lastref = lastref;
5094 	tr_buf = &th_trace->th_trbuf[lastref];
5095 	tr_buf->tr_time = ddi_get_lbolt();
5096 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5097 }
5098 
5099 static void
5100 th_trace_free(void *value)
5101 {
5102 	th_trace_t *th_trace = value;
5103 
5104 	ASSERT(th_trace->th_refcnt == 0);
5105 	kmem_free(th_trace, sizeof (*th_trace));
5106 }
5107 
5108 /*
5109  * Find or create the per-thread hash table used to track object references.
5110  * The ipst argument is NULL if we shouldn't allocate.
5111  *
5112  * Accesses per-thread data, so there's no need to lock here.
5113  */
5114 static mod_hash_t *
5115 th_trace_gethash(ip_stack_t *ipst)
5116 {
5117 	th_hash_t *thh;
5118 
5119 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5120 		mod_hash_t *mh;
5121 		char name[256];
5122 		size_t objsize, rshift;
5123 		int retv;
5124 
5125 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5126 			return (NULL);
5127 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5128 		    (void *)curthread);
5129 
5130 		/*
5131 		 * We use mod_hash_create_extended here rather than the more
5132 		 * obvious mod_hash_create_ptrhash because the latter has a
5133 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5134 		 * block.
5135 		 */
5136 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5137 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5138 		rshift = highbit(objsize);
5139 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5140 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5141 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5142 		if (mh == NULL) {
5143 			kmem_free(thh, sizeof (*thh));
5144 			return (NULL);
5145 		}
5146 		thh->thh_hash = mh;
5147 		thh->thh_ipst = ipst;
5148 		/*
5149 		 * We trace ills, ipifs, ires, and nces.  All of these are
5150 		 * per-IP-stack, so the lock on the thread list is as well.
5151 		 */
5152 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5153 		list_insert_tail(&ip_thread_list, thh);
5154 		rw_exit(&ip_thread_rwlock);
5155 		retv = tsd_set(ip_thread_data, thh);
5156 		ASSERT(retv == 0);
5157 	}
5158 	return (thh != NULL ? thh->thh_hash : NULL);
5159 }
5160 
5161 boolean_t
5162 th_trace_ref(const void *obj, ip_stack_t *ipst)
5163 {
5164 	th_trace_t *th_trace;
5165 	mod_hash_t *mh;
5166 	mod_hash_val_t val;
5167 
5168 	if ((mh = th_trace_gethash(ipst)) == NULL)
5169 		return (B_FALSE);
5170 
5171 	/*
5172 	 * Attempt to locate the trace buffer for this obj and thread.
5173 	 * If it does not exist, then allocate a new trace buffer and
5174 	 * insert into the hash.
5175 	 */
5176 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5177 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5178 		if (th_trace == NULL)
5179 			return (B_FALSE);
5180 
5181 		th_trace->th_id = curthread;
5182 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5183 		    (mod_hash_val_t)th_trace) != 0) {
5184 			kmem_free(th_trace, sizeof (th_trace_t));
5185 			return (B_FALSE);
5186 		}
5187 	} else {
5188 		th_trace = (th_trace_t *)val;
5189 	}
5190 
5191 	ASSERT(th_trace->th_refcnt >= 0 &&
5192 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5193 
5194 	th_trace->th_refcnt++;
5195 	th_trace_rrecord(th_trace);
5196 	return (B_TRUE);
5197 }
5198 
5199 /*
5200  * For the purpose of tracing a reference release, we assume that global
5201  * tracing is always on and that the same thread initiated the reference hold
5202  * is releasing.
5203  */
5204 void
5205 th_trace_unref(const void *obj)
5206 {
5207 	int retv;
5208 	mod_hash_t *mh;
5209 	th_trace_t *th_trace;
5210 	mod_hash_val_t val;
5211 
5212 	mh = th_trace_gethash(NULL);
5213 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5214 	ASSERT(retv == 0);
5215 	th_trace = (th_trace_t *)val;
5216 
5217 	ASSERT(th_trace->th_refcnt > 0);
5218 	th_trace->th_refcnt--;
5219 	th_trace_rrecord(th_trace);
5220 }
5221 
5222 /*
5223  * If tracing has been disabled, then we assume that the reference counts are
5224  * now useless, and we clear them out before destroying the entries.
5225  */
5226 void
5227 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5228 {
5229 	th_hash_t	*thh;
5230 	mod_hash_t	*mh;
5231 	mod_hash_val_t	val;
5232 	th_trace_t	*th_trace;
5233 	int		retv;
5234 
5235 	rw_enter(&ip_thread_rwlock, RW_READER);
5236 	for (thh = list_head(&ip_thread_list); thh != NULL;
5237 	    thh = list_next(&ip_thread_list, thh)) {
5238 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5239 		    &val) == 0) {
5240 			th_trace = (th_trace_t *)val;
5241 			if (trace_disable)
5242 				th_trace->th_refcnt = 0;
5243 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5244 			ASSERT(retv == 0);
5245 		}
5246 	}
5247 	rw_exit(&ip_thread_rwlock);
5248 }
5249 
5250 void
5251 ipif_trace_ref(ipif_t *ipif)
5252 {
5253 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5254 
5255 	if (ipif->ipif_trace_disable)
5256 		return;
5257 
5258 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5259 		ipif->ipif_trace_disable = B_TRUE;
5260 		ipif_trace_cleanup(ipif);
5261 	}
5262 }
5263 
5264 void
5265 ipif_untrace_ref(ipif_t *ipif)
5266 {
5267 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5268 
5269 	if (!ipif->ipif_trace_disable)
5270 		th_trace_unref(ipif);
5271 }
5272 
5273 void
5274 ill_trace_ref(ill_t *ill)
5275 {
5276 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5277 
5278 	if (ill->ill_trace_disable)
5279 		return;
5280 
5281 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5282 		ill->ill_trace_disable = B_TRUE;
5283 		ill_trace_cleanup(ill);
5284 	}
5285 }
5286 
5287 void
5288 ill_untrace_ref(ill_t *ill)
5289 {
5290 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5291 
5292 	if (!ill->ill_trace_disable)
5293 		th_trace_unref(ill);
5294 }
5295 
5296 /*
5297  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5298  * failure, ipif_trace_disable is set.
5299  */
5300 static void
5301 ipif_trace_cleanup(const ipif_t *ipif)
5302 {
5303 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5304 }
5305 
5306 /*
5307  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5308  * failure, ill_trace_disable is set.
5309  */
5310 static void
5311 ill_trace_cleanup(const ill_t *ill)
5312 {
5313 	th_trace_cleanup(ill, ill->ill_trace_disable);
5314 }
5315 #endif /* DEBUG */
5316 
5317 void
5318 ipif_refhold_locked(ipif_t *ipif)
5319 {
5320 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5321 	ipif->ipif_refcnt++;
5322 	IPIF_TRACE_REF(ipif);
5323 }
5324 
5325 void
5326 ipif_refhold(ipif_t *ipif)
5327 {
5328 	ill_t	*ill;
5329 
5330 	ill = ipif->ipif_ill;
5331 	mutex_enter(&ill->ill_lock);
5332 	ipif->ipif_refcnt++;
5333 	IPIF_TRACE_REF(ipif);
5334 	mutex_exit(&ill->ill_lock);
5335 }
5336 
5337 /*
5338  * Must not be called while holding any locks. Otherwise if this is
5339  * the last reference to be released there is a chance of recursive mutex
5340  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5341  * to restart an ioctl.
5342  */
5343 void
5344 ipif_refrele(ipif_t *ipif)
5345 {
5346 	ill_t	*ill;
5347 
5348 	ill = ipif->ipif_ill;
5349 
5350 	mutex_enter(&ill->ill_lock);
5351 	ASSERT(ipif->ipif_refcnt != 0);
5352 	ipif->ipif_refcnt--;
5353 	IPIF_UNTRACE_REF(ipif);
5354 	if (ipif->ipif_refcnt != 0) {
5355 		mutex_exit(&ill->ill_lock);
5356 		return;
5357 	}
5358 
5359 	/* Drops the ill_lock */
5360 	ipif_ill_refrele_tail(ill);
5361 }
5362 
5363 ipif_t *
5364 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5365 {
5366 	ipif_t	*ipif;
5367 
5368 	mutex_enter(&ill->ill_lock);
5369 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5370 	    ipif != NULL; ipif = ipif->ipif_next) {
5371 		if (IPIF_IS_CONDEMNED(ipif))
5372 			continue;
5373 		ipif_refhold_locked(ipif);
5374 		mutex_exit(&ill->ill_lock);
5375 		return (ipif);
5376 	}
5377 	mutex_exit(&ill->ill_lock);
5378 	return (NULL);
5379 }
5380 
5381 /*
5382  * TODO: make this table extendible at run time
5383  * Return a pointer to the mac type info for 'mac_type'
5384  */
5385 static ip_m_t *
5386 ip_m_lookup(t_uscalar_t mac_type)
5387 {
5388 	ip_m_t	*ipm;
5389 
5390 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5391 		if (ipm->ip_m_mac_type == mac_type)
5392 			return (ipm);
5393 	return (NULL);
5394 }
5395 
5396 /*
5397  * Make a link layer address from the multicast IP address *addr.
5398  * To form the link layer address, invoke the ip_m_v*mapping function
5399  * associated with the link-layer type.
5400  */
5401 void
5402 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5403 {
5404 	ip_m_t *ipm;
5405 
5406 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5407 		return;
5408 
5409 	ASSERT(addr != NULL);
5410 
5411 	ipm = ip_m_lookup(ill->ill_mactype);
5412 	if (ipm == NULL ||
5413 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5414 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5415 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5416 		    ill->ill_name, ill->ill_mactype));
5417 		return;
5418 	}
5419 	if (ill->ill_isv6)
5420 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5421 	else
5422 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5423 }
5424 
5425 /*
5426  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5427  * ill is passed in to associate it with the correct interface.
5428  * If ire_arg is set, then we return the held IRE in that location.
5429  */
5430 int
5431 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5432     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5433     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5434 {
5435 	ire_t	*ire, *nire;
5436 	ire_t	*gw_ire = NULL;
5437 	ipif_t	*ipif = NULL;
5438 	uint_t	type;
5439 	int	match_flags = MATCH_IRE_TYPE;
5440 	tsol_gc_t *gc = NULL;
5441 	tsol_gcgrp_t *gcgrp = NULL;
5442 	boolean_t gcgrp_xtraref = B_FALSE;
5443 	boolean_t cgtp_broadcast;
5444 
5445 	ip1dbg(("ip_rt_add:"));
5446 
5447 	if (ire_arg != NULL)
5448 		*ire_arg = NULL;
5449 
5450 	/*
5451 	 * If this is the case of RTF_HOST being set, then we set the netmask
5452 	 * to all ones (regardless if one was supplied).
5453 	 */
5454 	if (flags & RTF_HOST)
5455 		mask = IP_HOST_MASK;
5456 
5457 	/*
5458 	 * Prevent routes with a zero gateway from being created (since
5459 	 * interfaces can currently be plumbed and brought up no assigned
5460 	 * address).
5461 	 */
5462 	if (gw_addr == 0)
5463 		return (ENETUNREACH);
5464 	/*
5465 	 * Get the ipif, if any, corresponding to the gw_addr
5466 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5467 	 * we match on the gatway and destination to handle unnumbered pt-pt
5468 	 * interfaces.
5469 	 */
5470 	if (ill != NULL)
5471 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5472 	else
5473 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5474 	if (ipif != NULL) {
5475 		if (IS_VNI(ipif->ipif_ill)) {
5476 			ipif_refrele(ipif);
5477 			return (EINVAL);
5478 		}
5479 	}
5480 
5481 	/*
5482 	 * GateD will attempt to create routes with a loopback interface
5483 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5484 	 * these routes to be added, but create them as interface routes
5485 	 * since the gateway is an interface address.
5486 	 */
5487 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5488 		flags &= ~RTF_GATEWAY;
5489 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5490 		    mask == IP_HOST_MASK) {
5491 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5492 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5493 			    NULL);
5494 			if (ire != NULL) {
5495 				ire_refrele(ire);
5496 				ipif_refrele(ipif);
5497 				return (EEXIST);
5498 			}
5499 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5500 			    "for 0x%x\n", (void *)ipif,
5501 			    ipif->ipif_ire_type,
5502 			    ntohl(ipif->ipif_lcl_addr)));
5503 			ire = ire_create(
5504 			    (uchar_t *)&dst_addr,	/* dest address */
5505 			    (uchar_t *)&mask,		/* mask */
5506 			    NULL,			/* no gateway */
5507 			    ipif->ipif_ire_type,	/* LOOPBACK */
5508 			    ipif->ipif_ill,
5509 			    zoneid,
5510 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5511 			    NULL,
5512 			    ipst);
5513 
5514 			if (ire == NULL) {
5515 				ipif_refrele(ipif);
5516 				return (ENOMEM);
5517 			}
5518 			/* src address assigned by the caller? */
5519 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5520 				ire->ire_setsrc_addr = src_addr;
5521 
5522 			nire = ire_add(ire);
5523 			if (nire == NULL) {
5524 				/*
5525 				 * In the result of failure, ire_add() will have
5526 				 * already deleted the ire in question, so there
5527 				 * is no need to do that here.
5528 				 */
5529 				ipif_refrele(ipif);
5530 				return (ENOMEM);
5531 			}
5532 			/*
5533 			 * Check if it was a duplicate entry. This handles
5534 			 * the case of two racing route adds for the same route
5535 			 */
5536 			if (nire != ire) {
5537 				ASSERT(nire->ire_identical_ref > 1);
5538 				ire_delete(nire);
5539 				ire_refrele(nire);
5540 				ipif_refrele(ipif);
5541 				return (EEXIST);
5542 			}
5543 			ire = nire;
5544 			goto save_ire;
5545 		}
5546 	}
5547 
5548 	/*
5549 	 * The routes for multicast with CGTP are quite special in that
5550 	 * the gateway is the local interface address, yet RTF_GATEWAY
5551 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5552 	 * this undocumented and unusual use of multicast routes.
5553 	 */
5554 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5555 		flags &= ~RTF_GATEWAY;
5556 
5557 	/*
5558 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5559 	 * and the gateway address provided is one of the system's interface
5560 	 * addresses.  By using the routing socket interface and supplying an
5561 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5562 	 * specifying an interface route to be created is available which uses
5563 	 * the interface index that specifies the outgoing interface rather than
5564 	 * the address of an outgoing interface (which may not be able to
5565 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5566 	 * flag, routes can be specified which not only specify the next-hop to
5567 	 * be used when routing to a certain prefix, but also which outgoing
5568 	 * interface should be used.
5569 	 *
5570 	 * Previously, interfaces would have unique addresses assigned to them
5571 	 * and so the address assigned to a particular interface could be used
5572 	 * to identify a particular interface.  One exception to this was the
5573 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5574 	 *
5575 	 * With the advent of IPv6 and its link-local addresses, this
5576 	 * restriction was relaxed and interfaces could share addresses between
5577 	 * themselves.  In fact, typically all of the link-local interfaces on
5578 	 * an IPv6 node or router will have the same link-local address.  In
5579 	 * order to differentiate between these interfaces, the use of an
5580 	 * interface index is necessary and this index can be carried inside a
5581 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5582 	 * of using the interface index, however, is that all of the ipif's that
5583 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5584 	 * cannot be used to differentiate between ipif's (or logical
5585 	 * interfaces) that belong to the same ill (physical interface).
5586 	 *
5587 	 * For example, in the following case involving IPv4 interfaces and
5588 	 * logical interfaces
5589 	 *
5590 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5591 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5592 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5593 	 *
5594 	 * the ipif's corresponding to each of these interface routes can be
5595 	 * uniquely identified by the "gateway" (actually interface address).
5596 	 *
5597 	 * In this case involving multiple IPv6 default routes to a particular
5598 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5599 	 * default route is of interest:
5600 	 *
5601 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5602 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5603 	 */
5604 
5605 	/* RTF_GATEWAY not set */
5606 	if (!(flags & RTF_GATEWAY)) {
5607 		if (sp != NULL) {
5608 			ip2dbg(("ip_rt_add: gateway security attributes "
5609 			    "cannot be set with interface route\n"));
5610 			if (ipif != NULL)
5611 				ipif_refrele(ipif);
5612 			return (EINVAL);
5613 		}
5614 
5615 		/*
5616 		 * Whether or not ill (RTA_IFP) is set, we require that
5617 		 * the gateway is one of our local addresses.
5618 		 */
5619 		if (ipif == NULL)
5620 			return (ENETUNREACH);
5621 
5622 		/*
5623 		 * We use MATCH_IRE_ILL here. If the caller specified an
5624 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5625 		 * we use the ill derived from the gateway address.
5626 		 * We can always match the gateway address since we record it
5627 		 * in ire_gateway_addr.
5628 		 * We don't allow RTA_IFP to specify a different ill than the
5629 		 * one matching the ipif to make sure we can delete the route.
5630 		 */
5631 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5632 		if (ill == NULL) {
5633 			ill = ipif->ipif_ill;
5634 		} else if (ill != ipif->ipif_ill) {
5635 			ipif_refrele(ipif);
5636 			return (EINVAL);
5637 		}
5638 
5639 		/*
5640 		 * We check for an existing entry at this point.
5641 		 *
5642 		 * Since a netmask isn't passed in via the ioctl interface
5643 		 * (SIOCADDRT), we don't check for a matching netmask in that
5644 		 * case.
5645 		 */
5646 		if (!ioctl_msg)
5647 			match_flags |= MATCH_IRE_MASK;
5648 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5649 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5650 		    NULL);
5651 		if (ire != NULL) {
5652 			ire_refrele(ire);
5653 			ipif_refrele(ipif);
5654 			return (EEXIST);
5655 		}
5656 
5657 		/*
5658 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5659 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5660 		 * gateway.
5661 		 */
5662 		ire = ire_create(
5663 		    (uchar_t *)&dst_addr,
5664 		    (uint8_t *)&mask,
5665 		    (uint8_t *)&gw_addr,
5666 		    ill->ill_net_type,
5667 		    ill,
5668 		    zoneid,
5669 		    flags,
5670 		    NULL,
5671 		    ipst);
5672 		if (ire == NULL) {
5673 			ipif_refrele(ipif);
5674 			return (ENOMEM);
5675 		}
5676 
5677 		/*
5678 		 * Some software (for example, GateD and Sun Cluster) attempts
5679 		 * to create (what amount to) IRE_PREFIX routes with the
5680 		 * loopback address as the gateway.  This is primarily done to
5681 		 * set up prefixes with the RTF_REJECT flag set (for example,
5682 		 * when generating aggregate routes.)
5683 		 *
5684 		 * If the IRE type (as defined by ill->ill_net_type) is
5685 		 * IRE_LOOPBACK, then we map the request into a
5686 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5687 		 * these interface routes, by definition, can only be that.
5688 		 *
5689 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5690 		 * routine, but rather using ire_create() directly.
5691 		 *
5692 		 */
5693 		if (ill->ill_net_type == IRE_LOOPBACK) {
5694 			ire->ire_type = IRE_IF_NORESOLVER;
5695 			ire->ire_flags |= RTF_BLACKHOLE;
5696 		}
5697 
5698 		/* src address assigned by the caller? */
5699 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5700 			ire->ire_setsrc_addr = src_addr;
5701 
5702 		nire = ire_add(ire);
5703 		if (nire == NULL) {
5704 			/*
5705 			 * In the result of failure, ire_add() will have
5706 			 * already deleted the ire in question, so there
5707 			 * is no need to do that here.
5708 			 */
5709 			ipif_refrele(ipif);
5710 			return (ENOMEM);
5711 		}
5712 		/*
5713 		 * Check if it was a duplicate entry. This handles
5714 		 * the case of two racing route adds for the same route
5715 		 */
5716 		if (nire != ire) {
5717 			ire_delete(nire);
5718 			ire_refrele(nire);
5719 			ipif_refrele(ipif);
5720 			return (EEXIST);
5721 		}
5722 		ire = nire;
5723 		goto save_ire;
5724 	}
5725 
5726 	/*
5727 	 * Get an interface IRE for the specified gateway.
5728 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5729 	 * gateway, it is currently unreachable and we fail the request
5730 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5731 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5732 	 * If RTA_IFP was specified we look on that particular ill.
5733 	 */
5734 	if (ill != NULL)
5735 		match_flags |= MATCH_IRE_ILL;
5736 
5737 	/* Check whether the gateway is reachable. */
5738 again:
5739 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5740 	if (flags & RTF_INDIRECT)
5741 		type |= IRE_OFFLINK;
5742 
5743 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5744 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5745 	if (gw_ire == NULL) {
5746 		/*
5747 		 * With IPMP, we allow host routes to influence in.mpathd's
5748 		 * target selection.  However, if the test addresses are on
5749 		 * their own network, the above lookup will fail since the
5750 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5751 		 * hidden test IREs to be found and try again.
5752 		 */
5753 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5754 			match_flags |= MATCH_IRE_TESTHIDDEN;
5755 			goto again;
5756 		}
5757 		if (ipif != NULL)
5758 			ipif_refrele(ipif);
5759 		return (ENETUNREACH);
5760 	}
5761 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5762 		ire_refrele(gw_ire);
5763 		if (ipif != NULL)
5764 			ipif_refrele(ipif);
5765 		return (ENETUNREACH);
5766 	}
5767 
5768 	/*
5769 	 * We create one of three types of IREs as a result of this request
5770 	 * based on the netmask.  A netmask of all ones (which is automatically
5771 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5772 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5773 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5774 	 * destination prefix.
5775 	 */
5776 	if (mask == IP_HOST_MASK)
5777 		type = IRE_HOST;
5778 	else if (mask == 0)
5779 		type = IRE_DEFAULT;
5780 	else
5781 		type = IRE_PREFIX;
5782 
5783 	/* check for a duplicate entry */
5784 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5785 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5786 	    0, ipst, NULL);
5787 	if (ire != NULL) {
5788 		if (ipif != NULL)
5789 			ipif_refrele(ipif);
5790 		ire_refrele(gw_ire);
5791 		ire_refrele(ire);
5792 		return (EEXIST);
5793 	}
5794 
5795 	/* Security attribute exists */
5796 	if (sp != NULL) {
5797 		tsol_gcgrp_addr_t ga;
5798 
5799 		/* find or create the gateway credentials group */
5800 		ga.ga_af = AF_INET;
5801 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5802 
5803 		/* we hold reference to it upon success */
5804 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5805 		if (gcgrp == NULL) {
5806 			if (ipif != NULL)
5807 				ipif_refrele(ipif);
5808 			ire_refrele(gw_ire);
5809 			return (ENOMEM);
5810 		}
5811 
5812 		/*
5813 		 * Create and add the security attribute to the group; a
5814 		 * reference to the group is made upon allocating a new
5815 		 * entry successfully.  If it finds an already-existing
5816 		 * entry for the security attribute in the group, it simply
5817 		 * returns it and no new reference is made to the group.
5818 		 */
5819 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5820 		if (gc == NULL) {
5821 			if (ipif != NULL)
5822 				ipif_refrele(ipif);
5823 			/* release reference held by gcgrp_lookup */
5824 			GCGRP_REFRELE(gcgrp);
5825 			ire_refrele(gw_ire);
5826 			return (ENOMEM);
5827 		}
5828 	}
5829 
5830 	/* Create the IRE. */
5831 	ire = ire_create(
5832 	    (uchar_t *)&dst_addr,		/* dest address */
5833 	    (uchar_t *)&mask,			/* mask */
5834 	    (uchar_t *)&gw_addr,		/* gateway address */
5835 	    (ushort_t)type,			/* IRE type */
5836 	    ill,
5837 	    zoneid,
5838 	    flags,
5839 	    gc,					/* security attribute */
5840 	    ipst);
5841 
5842 	/*
5843 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5844 	 * reference to the 'gcgrp'. We can now release the extra reference
5845 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5846 	 */
5847 	if (gcgrp_xtraref)
5848 		GCGRP_REFRELE(gcgrp);
5849 	if (ire == NULL) {
5850 		if (gc != NULL)
5851 			GC_REFRELE(gc);
5852 		if (ipif != NULL)
5853 			ipif_refrele(ipif);
5854 		ire_refrele(gw_ire);
5855 		return (ENOMEM);
5856 	}
5857 
5858 	/* Before we add, check if an extra CGTP broadcast is needed */
5859 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5860 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5861 
5862 	/* src address assigned by the caller? */
5863 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5864 		ire->ire_setsrc_addr = src_addr;
5865 
5866 	/*
5867 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5868 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5869 	 */
5870 
5871 	/* Add the new IRE. */
5872 	nire = ire_add(ire);
5873 	if (nire == NULL) {
5874 		/*
5875 		 * In the result of failure, ire_add() will have
5876 		 * already deleted the ire in question, so there
5877 		 * is no need to do that here.
5878 		 */
5879 		if (ipif != NULL)
5880 			ipif_refrele(ipif);
5881 		ire_refrele(gw_ire);
5882 		return (ENOMEM);
5883 	}
5884 	/*
5885 	 * Check if it was a duplicate entry. This handles
5886 	 * the case of two racing route adds for the same route
5887 	 */
5888 	if (nire != ire) {
5889 		ire_delete(nire);
5890 		ire_refrele(nire);
5891 		if (ipif != NULL)
5892 			ipif_refrele(ipif);
5893 		ire_refrele(gw_ire);
5894 		return (EEXIST);
5895 	}
5896 	ire = nire;
5897 
5898 	if (flags & RTF_MULTIRT) {
5899 		/*
5900 		 * Invoke the CGTP (multirouting) filtering module
5901 		 * to add the dst address in the filtering database.
5902 		 * Replicated inbound packets coming from that address
5903 		 * will be filtered to discard the duplicates.
5904 		 * It is not necessary to call the CGTP filter hook
5905 		 * when the dst address is a broadcast or multicast,
5906 		 * because an IP source address cannot be a broadcast
5907 		 * or a multicast.
5908 		 */
5909 		if (cgtp_broadcast) {
5910 			ip_cgtp_bcast_add(ire, ipst);
5911 			goto save_ire;
5912 		}
5913 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5914 		    !CLASSD(ire->ire_addr)) {
5915 			int res;
5916 			ipif_t *src_ipif;
5917 
5918 			/* Find the source address corresponding to gw_ire */
5919 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5920 			    NULL, zoneid, ipst);
5921 			if (src_ipif != NULL) {
5922 				res = ipst->ips_ip_cgtp_filter_ops->
5923 				    cfo_add_dest_v4(
5924 				    ipst->ips_netstack->netstack_stackid,
5925 				    ire->ire_addr,
5926 				    ire->ire_gateway_addr,
5927 				    ire->ire_setsrc_addr,
5928 				    src_ipif->ipif_lcl_addr);
5929 				ipif_refrele(src_ipif);
5930 			} else {
5931 				res = EADDRNOTAVAIL;
5932 			}
5933 			if (res != 0) {
5934 				if (ipif != NULL)
5935 					ipif_refrele(ipif);
5936 				ire_refrele(gw_ire);
5937 				ire_delete(ire);
5938 				ire_refrele(ire);	/* Held in ire_add */
5939 				return (res);
5940 			}
5941 		}
5942 	}
5943 
5944 save_ire:
5945 	if (gw_ire != NULL) {
5946 		ire_refrele(gw_ire);
5947 		gw_ire = NULL;
5948 	}
5949 	if (ill != NULL) {
5950 		/*
5951 		 * Save enough information so that we can recreate the IRE if
5952 		 * the interface goes down and then up.  The metrics associated
5953 		 * with the route will be saved as well when rts_setmetrics() is
5954 		 * called after the IRE has been created.  In the case where
5955 		 * memory cannot be allocated, none of this information will be
5956 		 * saved.
5957 		 */
5958 		ill_save_ire(ill, ire);
5959 	}
5960 	if (ioctl_msg)
5961 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5962 	if (ire_arg != NULL) {
5963 		/*
5964 		 * Store the ire that was successfully added into where ire_arg
5965 		 * points to so that callers don't have to look it up
5966 		 * themselves (but they are responsible for ire_refrele()ing
5967 		 * the ire when they are finished with it).
5968 		 */
5969 		*ire_arg = ire;
5970 	} else {
5971 		ire_refrele(ire);		/* Held in ire_add */
5972 	}
5973 	if (ipif != NULL)
5974 		ipif_refrele(ipif);
5975 	return (0);
5976 }
5977 
5978 /*
5979  * ip_rt_delete is called to delete an IPv4 route.
5980  * ill is passed in to associate it with the correct interface.
5981  */
5982 /* ARGSUSED4 */
5983 int
5984 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5985     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5986     ip_stack_t *ipst, zoneid_t zoneid)
5987 {
5988 	ire_t	*ire = NULL;
5989 	ipif_t	*ipif;
5990 	uint_t	type;
5991 	uint_t	match_flags = MATCH_IRE_TYPE;
5992 	int	err = 0;
5993 
5994 	ip1dbg(("ip_rt_delete:"));
5995 	/*
5996 	 * If this is the case of RTF_HOST being set, then we set the netmask
5997 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5998 	 */
5999 	if (flags & RTF_HOST) {
6000 		mask = IP_HOST_MASK;
6001 		match_flags |= MATCH_IRE_MASK;
6002 	} else if (rtm_addrs & RTA_NETMASK) {
6003 		match_flags |= MATCH_IRE_MASK;
6004 	}
6005 
6006 	/*
6007 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6008 	 * we check if the gateway address is one of our interfaces first,
6009 	 * and fall back on RTF_GATEWAY routes.
6010 	 *
6011 	 * This makes it possible to delete an original
6012 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6013 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6014 	 * and those can not be deleted here.
6015 	 *
6016 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6017 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6018 	 * otherwise we use the ill derived from the gateway address.
6019 	 * We can always match the gateway address since we record it
6020 	 * in ire_gateway_addr.
6021 	 *
6022 	 * For more detail on specifying routes by gateway address and by
6023 	 * interface index, see the comments in ip_rt_add().
6024 	 */
6025 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6026 	if (ipif != NULL) {
6027 		ill_t	*ill_match;
6028 
6029 		if (ill != NULL)
6030 			ill_match = ill;
6031 		else
6032 			ill_match = ipif->ipif_ill;
6033 
6034 		match_flags |= MATCH_IRE_ILL;
6035 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6036 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
6037 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
6038 			    NULL);
6039 		}
6040 		if (ire == NULL) {
6041 			match_flags |= MATCH_IRE_GW;
6042 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6043 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6044 			    match_flags, 0, ipst, NULL);
6045 		}
6046 		/* Avoid deleting routes created by kernel from an ipif */
6047 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6048 			ire_refrele(ire);
6049 			ire = NULL;
6050 		}
6051 
6052 		/* Restore in case we didn't find a match */
6053 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6054 	}
6055 
6056 	if (ire == NULL) {
6057 		/*
6058 		 * At this point, the gateway address is not one of our own
6059 		 * addresses or a matching interface route was not found.  We
6060 		 * set the IRE type to lookup based on whether
6061 		 * this is a host route, a default route or just a prefix.
6062 		 *
6063 		 * If an ill was passed in, then the lookup is based on an
6064 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6065 		 */
6066 		match_flags |= MATCH_IRE_GW;
6067 		if (ill != NULL)
6068 			match_flags |= MATCH_IRE_ILL;
6069 		if (mask == IP_HOST_MASK)
6070 			type = IRE_HOST;
6071 		else if (mask == 0)
6072 			type = IRE_DEFAULT;
6073 		else
6074 			type = IRE_PREFIX;
6075 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6076 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6077 	}
6078 
6079 	if (ipif != NULL) {
6080 		ipif_refrele(ipif);
6081 		ipif = NULL;
6082 	}
6083 
6084 	if (ire == NULL)
6085 		return (ESRCH);
6086 
6087 	if (ire->ire_flags & RTF_MULTIRT) {
6088 		/*
6089 		 * Invoke the CGTP (multirouting) filtering module
6090 		 * to remove the dst address from the filtering database.
6091 		 * Packets coming from that address will no longer be
6092 		 * filtered to remove duplicates.
6093 		 */
6094 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6095 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6096 			    ipst->ips_netstack->netstack_stackid,
6097 			    ire->ire_addr, ire->ire_gateway_addr);
6098 		}
6099 		ip_cgtp_bcast_delete(ire, ipst);
6100 	}
6101 
6102 	ill = ire->ire_ill;
6103 	if (ill != NULL)
6104 		ill_remove_saved_ire(ill, ire);
6105 	if (ioctl_msg)
6106 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6107 	ire_delete(ire);
6108 	ire_refrele(ire);
6109 	return (err);
6110 }
6111 
6112 /*
6113  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6114  */
6115 /* ARGSUSED */
6116 int
6117 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6118     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6119 {
6120 	ipaddr_t dst_addr;
6121 	ipaddr_t gw_addr;
6122 	ipaddr_t mask;
6123 	int error = 0;
6124 	mblk_t *mp1;
6125 	struct rtentry *rt;
6126 	ipif_t *ipif = NULL;
6127 	ip_stack_t	*ipst;
6128 
6129 	ASSERT(q->q_next == NULL);
6130 	ipst = CONNQ_TO_IPST(q);
6131 
6132 	ip1dbg(("ip_siocaddrt:"));
6133 	/* Existence of mp1 verified in ip_wput_nondata */
6134 	mp1 = mp->b_cont->b_cont;
6135 	rt = (struct rtentry *)mp1->b_rptr;
6136 
6137 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6138 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6139 
6140 	/*
6141 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6142 	 * to a particular host address.  In this case, we set the netmask to
6143 	 * all ones for the particular destination address.  Otherwise,
6144 	 * determine the netmask to be used based on dst_addr and the interfaces
6145 	 * in use.
6146 	 */
6147 	if (rt->rt_flags & RTF_HOST) {
6148 		mask = IP_HOST_MASK;
6149 	} else {
6150 		/*
6151 		 * Note that ip_subnet_mask returns a zero mask in the case of
6152 		 * default (an all-zeroes address).
6153 		 */
6154 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6155 	}
6156 
6157 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6158 	    B_TRUE, NULL, ipst, ALL_ZONES);
6159 	if (ipif != NULL)
6160 		ipif_refrele(ipif);
6161 	return (error);
6162 }
6163 
6164 /*
6165  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6166  */
6167 /* ARGSUSED */
6168 int
6169 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6170     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6171 {
6172 	ipaddr_t dst_addr;
6173 	ipaddr_t gw_addr;
6174 	ipaddr_t mask;
6175 	int error;
6176 	mblk_t *mp1;
6177 	struct rtentry *rt;
6178 	ipif_t *ipif = NULL;
6179 	ip_stack_t	*ipst;
6180 
6181 	ASSERT(q->q_next == NULL);
6182 	ipst = CONNQ_TO_IPST(q);
6183 
6184 	ip1dbg(("ip_siocdelrt:"));
6185 	/* Existence of mp1 verified in ip_wput_nondata */
6186 	mp1 = mp->b_cont->b_cont;
6187 	rt = (struct rtentry *)mp1->b_rptr;
6188 
6189 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6190 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6191 
6192 	/*
6193 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6194 	 * to a particular host address.  In this case, we set the netmask to
6195 	 * all ones for the particular destination address.  Otherwise,
6196 	 * determine the netmask to be used based on dst_addr and the interfaces
6197 	 * in use.
6198 	 */
6199 	if (rt->rt_flags & RTF_HOST) {
6200 		mask = IP_HOST_MASK;
6201 	} else {
6202 		/*
6203 		 * Note that ip_subnet_mask returns a zero mask in the case of
6204 		 * default (an all-zeroes address).
6205 		 */
6206 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6207 	}
6208 
6209 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6210 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6211 	    ipst, ALL_ZONES);
6212 	if (ipif != NULL)
6213 		ipif_refrele(ipif);
6214 	return (error);
6215 }
6216 
6217 /*
6218  * Enqueue the mp onto the ipsq, chained by b_next.
6219  * b_prev stores the function to be executed later, and b_queue the queue
6220  * where this mp originated.
6221  */
6222 void
6223 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6224     ill_t *pending_ill)
6225 {
6226 	conn_t	*connp;
6227 	ipxop_t *ipx = ipsq->ipsq_xop;
6228 
6229 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6230 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6231 	ASSERT(func != NULL);
6232 
6233 	mp->b_queue = q;
6234 	mp->b_prev = (void *)func;
6235 	mp->b_next = NULL;
6236 
6237 	switch (type) {
6238 	case CUR_OP:
6239 		if (ipx->ipx_mptail != NULL) {
6240 			ASSERT(ipx->ipx_mphead != NULL);
6241 			ipx->ipx_mptail->b_next = mp;
6242 		} else {
6243 			ASSERT(ipx->ipx_mphead == NULL);
6244 			ipx->ipx_mphead = mp;
6245 		}
6246 		ipx->ipx_mptail = mp;
6247 		break;
6248 
6249 	case NEW_OP:
6250 		if (ipsq->ipsq_xopq_mptail != NULL) {
6251 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6252 			ipsq->ipsq_xopq_mptail->b_next = mp;
6253 		} else {
6254 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6255 			ipsq->ipsq_xopq_mphead = mp;
6256 		}
6257 		ipsq->ipsq_xopq_mptail = mp;
6258 		ipx->ipx_ipsq_queued = B_TRUE;
6259 		break;
6260 
6261 	case SWITCH_OP:
6262 		ASSERT(ipsq->ipsq_swxop != NULL);
6263 		/* only one switch operation is currently allowed */
6264 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6265 		ipsq->ipsq_switch_mp = mp;
6266 		ipx->ipx_ipsq_queued = B_TRUE;
6267 		break;
6268 	default:
6269 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6270 	}
6271 
6272 	if (CONN_Q(q) && pending_ill != NULL) {
6273 		connp = Q_TO_CONN(q);
6274 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6275 		connp->conn_oper_pending_ill = pending_ill;
6276 	}
6277 }
6278 
6279 /*
6280  * Dequeue the next message that requested exclusive access to this IPSQ's
6281  * xop.  Specifically:
6282  *
6283  *  1. If we're still processing the current operation on `ipsq', then
6284  *     dequeue the next message for the operation (from ipx_mphead), or
6285  *     return NULL if there are no queued messages for the operation.
6286  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6287  *
6288  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6289  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6290  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6291  *     leaving IPMP groups and require a careful dance -- see the comments
6292  *     in-line below for details.  If we're leaving a group xop or if we're
6293  *     joining a group xop and become writer on it, then we proceed to (3).
6294  *     Otherwise, we return NULL and exit the xop.
6295  *
6296  *  3. For each IPSQ in the xop, return any switch operation stored on
6297  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6298  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6299  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6300  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6301  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6302  *     each phyint in the group, including the IPMP meta-interface phyint.
6303  */
6304 static mblk_t *
6305 ipsq_dq(ipsq_t *ipsq)
6306 {
6307 	ill_t	*illv4, *illv6;
6308 	mblk_t	*mp;
6309 	ipsq_t	*xopipsq;
6310 	ipsq_t	*leftipsq = NULL;
6311 	ipxop_t *ipx;
6312 	phyint_t *phyi = ipsq->ipsq_phyint;
6313 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6314 	boolean_t emptied = B_FALSE;
6315 
6316 	/*
6317 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6318 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6319 	 */
6320 	rw_enter(&ipst->ips_ill_g_lock,
6321 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6322 	mutex_enter(&ipsq->ipsq_lock);
6323 	ipx = ipsq->ipsq_xop;
6324 	mutex_enter(&ipx->ipx_lock);
6325 
6326 	/*
6327 	 * Dequeue the next message associated with the current exclusive
6328 	 * operation, if any.
6329 	 */
6330 	if ((mp = ipx->ipx_mphead) != NULL) {
6331 		ipx->ipx_mphead = mp->b_next;
6332 		if (ipx->ipx_mphead == NULL)
6333 			ipx->ipx_mptail = NULL;
6334 		mp->b_next = (void *)ipsq;
6335 		goto out;
6336 	}
6337 
6338 	if (ipx->ipx_current_ipif != NULL)
6339 		goto empty;
6340 
6341 	if (ipsq->ipsq_swxop != NULL) {
6342 		/*
6343 		 * The exclusive operation that is now being completed has
6344 		 * requested a switch to a different xop.  This happens
6345 		 * when an interface joins or leaves an IPMP group.  Joins
6346 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6347 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6348 		 * (phyint_free()), or interface plumb for an ill type
6349 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6350 		 *
6351 		 * Xop switches are not allowed on the IPMP meta-interface.
6352 		 */
6353 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6354 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6355 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6356 
6357 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6358 			/*
6359 			 * We're switching back to our own xop, so we have two
6360 			 * xop's to drain/exit: our own, and the group xop
6361 			 * that we are leaving.
6362 			 *
6363 			 * First, pull ourselves out of the group ipsq list.
6364 			 * This is safe since we're writer on ill_g_lock.
6365 			 */
6366 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6367 
6368 			xopipsq = ipx->ipx_ipsq;
6369 			while (xopipsq->ipsq_next != ipsq)
6370 				xopipsq = xopipsq->ipsq_next;
6371 
6372 			xopipsq->ipsq_next = ipsq->ipsq_next;
6373 			ipsq->ipsq_next = ipsq;
6374 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6375 			ipsq->ipsq_swxop = NULL;
6376 
6377 			/*
6378 			 * Second, prepare to exit the group xop.  The actual
6379 			 * ipsq_exit() is done at the end of this function
6380 			 * since we cannot hold any locks across ipsq_exit().
6381 			 * Note that although we drop the group's ipx_lock, no
6382 			 * threads can proceed since we're still ipx_writer.
6383 			 */
6384 			leftipsq = xopipsq;
6385 			mutex_exit(&ipx->ipx_lock);
6386 
6387 			/*
6388 			 * Third, set ipx to point to our own xop (which was
6389 			 * inactive and therefore can be entered).
6390 			 */
6391 			ipx = ipsq->ipsq_xop;
6392 			mutex_enter(&ipx->ipx_lock);
6393 			ASSERT(ipx->ipx_writer == NULL);
6394 			ASSERT(ipx->ipx_current_ipif == NULL);
6395 		} else {
6396 			/*
6397 			 * We're switching from our own xop to a group xop.
6398 			 * The requestor of the switch must ensure that the
6399 			 * group xop cannot go away (e.g. by ensuring the
6400 			 * phyint associated with the xop cannot go away).
6401 			 *
6402 			 * If we can become writer on our new xop, then we'll
6403 			 * do the drain.  Otherwise, the current writer of our
6404 			 * new xop will do the drain when it exits.
6405 			 *
6406 			 * First, splice ourselves into the group IPSQ list.
6407 			 * This is safe since we're writer on ill_g_lock.
6408 			 */
6409 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6410 
6411 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6412 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6413 				xopipsq = xopipsq->ipsq_next;
6414 
6415 			xopipsq->ipsq_next = ipsq;
6416 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6417 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6418 			ipsq->ipsq_swxop = NULL;
6419 
6420 			/*
6421 			 * Second, exit our own xop, since it's now unused.
6422 			 * This is safe since we've got the only reference.
6423 			 */
6424 			ASSERT(ipx->ipx_writer == curthread);
6425 			ipx->ipx_writer = NULL;
6426 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6427 			ipx->ipx_ipsq_queued = B_FALSE;
6428 			mutex_exit(&ipx->ipx_lock);
6429 
6430 			/*
6431 			 * Third, set ipx to point to our new xop, and check
6432 			 * if we can become writer on it.  If we cannot, then
6433 			 * the current writer will drain the IPSQ group when
6434 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6435 			 * because we're still holding ipsq_lock.
6436 			 */
6437 			ipx = ipsq->ipsq_xop;
6438 			mutex_enter(&ipx->ipx_lock);
6439 			if (ipx->ipx_writer != NULL ||
6440 			    ipx->ipx_current_ipif != NULL) {
6441 				goto out;
6442 			}
6443 		}
6444 
6445 		/*
6446 		 * Fourth, become writer on our new ipx before we continue
6447 		 * with the drain.  Note that we never dropped ipsq_lock
6448 		 * above, so no other thread could've raced with us to
6449 		 * become writer first.  Also, we're holding ipx_lock, so
6450 		 * no other thread can examine the ipx right now.
6451 		 */
6452 		ASSERT(ipx->ipx_current_ipif == NULL);
6453 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6454 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6455 		ipx->ipx_writer = curthread;
6456 		ipx->ipx_forced = B_FALSE;
6457 #ifdef DEBUG
6458 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6459 #endif
6460 	}
6461 
6462 	xopipsq = ipsq;
6463 	do {
6464 		/*
6465 		 * So that other operations operate on a consistent and
6466 		 * complete phyint, a switch message on an IPSQ must be
6467 		 * handled prior to any other operations on that IPSQ.
6468 		 */
6469 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6470 			xopipsq->ipsq_switch_mp = NULL;
6471 			ASSERT(mp->b_next == NULL);
6472 			mp->b_next = (void *)xopipsq;
6473 			goto out;
6474 		}
6475 
6476 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6477 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6478 			if (xopipsq->ipsq_xopq_mphead == NULL)
6479 				xopipsq->ipsq_xopq_mptail = NULL;
6480 			mp->b_next = (void *)xopipsq;
6481 			goto out;
6482 		}
6483 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6484 empty:
6485 	/*
6486 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6487 	 * new messages can end up on any IPSQ in the xop.
6488 	 */
6489 	ipx->ipx_writer = NULL;
6490 	ipx->ipx_forced = B_FALSE;
6491 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6492 	ipx->ipx_ipsq_queued = B_FALSE;
6493 	emptied = B_TRUE;
6494 #ifdef	DEBUG
6495 	ipx->ipx_depth = 0;
6496 #endif
6497 out:
6498 	mutex_exit(&ipx->ipx_lock);
6499 	mutex_exit(&ipsq->ipsq_lock);
6500 
6501 	/*
6502 	 * If we completely emptied the xop, then wake up any threads waiting
6503 	 * to enter any of the IPSQ's associated with it.
6504 	 */
6505 	if (emptied) {
6506 		xopipsq = ipsq;
6507 		do {
6508 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6509 				continue;
6510 
6511 			illv4 = phyi->phyint_illv4;
6512 			illv6 = phyi->phyint_illv6;
6513 
6514 			GRAB_ILL_LOCKS(illv4, illv6);
6515 			if (illv4 != NULL)
6516 				cv_broadcast(&illv4->ill_cv);
6517 			if (illv6 != NULL)
6518 				cv_broadcast(&illv6->ill_cv);
6519 			RELEASE_ILL_LOCKS(illv4, illv6);
6520 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6521 	}
6522 	rw_exit(&ipst->ips_ill_g_lock);
6523 
6524 	/*
6525 	 * Now that all locks are dropped, exit the IPSQ we left.
6526 	 */
6527 	if (leftipsq != NULL)
6528 		ipsq_exit(leftipsq);
6529 
6530 	return (mp);
6531 }
6532 
6533 /*
6534  * Return completion status of previously initiated DLPI operations on
6535  * ills in the purview of an ipsq.
6536  */
6537 static boolean_t
6538 ipsq_dlpi_done(ipsq_t *ipsq)
6539 {
6540 	ipsq_t		*ipsq_start;
6541 	phyint_t	*phyi;
6542 	ill_t		*ill;
6543 
6544 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6545 	ipsq_start = ipsq;
6546 
6547 	do {
6548 		/*
6549 		 * The only current users of this function are ipsq_try_enter
6550 		 * and ipsq_enter which have made sure that ipsq_writer is
6551 		 * NULL before we reach here. ill_dlpi_pending is modified
6552 		 * only by an ipsq writer
6553 		 */
6554 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6555 		phyi = ipsq->ipsq_phyint;
6556 		/*
6557 		 * phyi could be NULL if a phyint that is part of an
6558 		 * IPMP group is being unplumbed. A more detailed
6559 		 * comment is in ipmp_grp_update_kstats()
6560 		 */
6561 		if (phyi != NULL) {
6562 			ill = phyi->phyint_illv4;
6563 			if (ill != NULL &&
6564 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6565 			    ill->ill_arl_dlpi_pending))
6566 				return (B_FALSE);
6567 
6568 			ill = phyi->phyint_illv6;
6569 			if (ill != NULL &&
6570 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6571 				return (B_FALSE);
6572 		}
6573 
6574 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6575 
6576 	return (B_TRUE);
6577 }
6578 
6579 /*
6580  * Enter the ipsq corresponding to ill, by waiting synchronously till
6581  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6582  * will have to drain completely before ipsq_enter returns success.
6583  * ipx_current_ipif will be set if some exclusive op is in progress,
6584  * and the ipsq_exit logic will start the next enqueued op after
6585  * completion of the current op. If 'force' is used, we don't wait
6586  * for the enqueued ops. This is needed when a conn_close wants to
6587  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6588  * of an ill can also use this option. But we dont' use it currently.
6589  */
6590 #define	ENTER_SQ_WAIT_TICKS 100
6591 boolean_t
6592 ipsq_enter(ill_t *ill, boolean_t force, int type)
6593 {
6594 	ipsq_t	*ipsq;
6595 	ipxop_t *ipx;
6596 	boolean_t waited_enough = B_FALSE;
6597 	ip_stack_t *ipst = ill->ill_ipst;
6598 
6599 	/*
6600 	 * Note that the relationship between ill and ipsq is fixed as long as
6601 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6602 	 * relationship between the IPSQ and xop cannot change.  However,
6603 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6604 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6605 	 * waking up all ills in the xop when it becomes available.
6606 	 */
6607 	for (;;) {
6608 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6609 		mutex_enter(&ill->ill_lock);
6610 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6611 			mutex_exit(&ill->ill_lock);
6612 			rw_exit(&ipst->ips_ill_g_lock);
6613 			return (B_FALSE);
6614 		}
6615 
6616 		ipsq = ill->ill_phyint->phyint_ipsq;
6617 		mutex_enter(&ipsq->ipsq_lock);
6618 		ipx = ipsq->ipsq_xop;
6619 		mutex_enter(&ipx->ipx_lock);
6620 
6621 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6622 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6623 		    waited_enough))
6624 			break;
6625 
6626 		rw_exit(&ipst->ips_ill_g_lock);
6627 
6628 		if (!force || ipx->ipx_writer != NULL) {
6629 			mutex_exit(&ipx->ipx_lock);
6630 			mutex_exit(&ipsq->ipsq_lock);
6631 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6632 		} else {
6633 			mutex_exit(&ipx->ipx_lock);
6634 			mutex_exit(&ipsq->ipsq_lock);
6635 			(void) cv_reltimedwait(&ill->ill_cv,
6636 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6637 			waited_enough = B_TRUE;
6638 		}
6639 		mutex_exit(&ill->ill_lock);
6640 	}
6641 
6642 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6643 	ASSERT(ipx->ipx_reentry_cnt == 0);
6644 	ipx->ipx_writer = curthread;
6645 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6646 	ipx->ipx_reentry_cnt++;
6647 #ifdef DEBUG
6648 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6649 #endif
6650 	mutex_exit(&ipx->ipx_lock);
6651 	mutex_exit(&ipsq->ipsq_lock);
6652 	mutex_exit(&ill->ill_lock);
6653 	rw_exit(&ipst->ips_ill_g_lock);
6654 
6655 	return (B_TRUE);
6656 }
6657 
6658 /*
6659  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6660  * across the call to the core interface ipsq_try_enter() and hence calls this
6661  * function directly. This is explained more fully in ipif_set_values().
6662  * In order to support the above constraint, ipsq_try_enter is implemented as
6663  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6664  */
6665 static ipsq_t *
6666 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6667     int type, boolean_t reentry_ok)
6668 {
6669 	ipsq_t	*ipsq;
6670 	ipxop_t	*ipx;
6671 	ip_stack_t *ipst = ill->ill_ipst;
6672 
6673 	/*
6674 	 * lock ordering:
6675 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6676 	 *
6677 	 * ipx of an ipsq can't change when ipsq_lock is held.
6678 	 */
6679 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6680 	GRAB_CONN_LOCK(q);
6681 	mutex_enter(&ill->ill_lock);
6682 	ipsq = ill->ill_phyint->phyint_ipsq;
6683 	mutex_enter(&ipsq->ipsq_lock);
6684 	ipx = ipsq->ipsq_xop;
6685 	mutex_enter(&ipx->ipx_lock);
6686 
6687 	/*
6688 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6689 	 *    (Note: If the caller does not specify reentry_ok then neither
6690 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6691 	 *    again. Otherwise it can lead to an infinite loop
6692 	 * 2. Enter the ipsq if there is no current writer and this attempted
6693 	 *    entry is part of the current operation
6694 	 * 3. Enter the ipsq if there is no current writer and this is a new
6695 	 *    operation and the operation queue is empty and there is no
6696 	 *    operation currently in progress and if all previously initiated
6697 	 *    DLPI operations have completed.
6698 	 */
6699 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6700 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6701 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6702 	    ipsq_dlpi_done(ipsq))))) {
6703 		/* Success. */
6704 		ipx->ipx_reentry_cnt++;
6705 		ipx->ipx_writer = curthread;
6706 		ipx->ipx_forced = B_FALSE;
6707 		mutex_exit(&ipx->ipx_lock);
6708 		mutex_exit(&ipsq->ipsq_lock);
6709 		mutex_exit(&ill->ill_lock);
6710 		RELEASE_CONN_LOCK(q);
6711 #ifdef DEBUG
6712 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6713 #endif
6714 		return (ipsq);
6715 	}
6716 
6717 	if (func != NULL)
6718 		ipsq_enq(ipsq, q, mp, func, type, ill);
6719 
6720 	mutex_exit(&ipx->ipx_lock);
6721 	mutex_exit(&ipsq->ipsq_lock);
6722 	mutex_exit(&ill->ill_lock);
6723 	RELEASE_CONN_LOCK(q);
6724 	return (NULL);
6725 }
6726 
6727 /*
6728  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6729  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6730  * There is one ipsq per phyint. The ipsq
6731  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6732  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6733  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6734  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6735  * up the interface) and are enqueued in ipx_mphead.
6736  *
6737  * If a thread does not want to reenter the ipsq when it is already writer,
6738  * it must make sure that the specified reentry point to be called later
6739  * when the ipsq is empty, nor any code path starting from the specified reentry
6740  * point must never ever try to enter the ipsq again. Otherwise it can lead
6741  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6742  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6743  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6744  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6745  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6746  * ioctl if the current ioctl has completed. If the current ioctl is still
6747  * in progress it simply returns. The current ioctl could be waiting for
6748  * a response from another module (the driver or could be waiting for
6749  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6750  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6751  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6752  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6753  * all associated DLPI operations have completed.
6754  */
6755 
6756 /*
6757  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6758  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6759  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6760  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6761  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6762  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6763  */
6764 ipsq_t *
6765 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6766     ipsq_func_t func, int type, boolean_t reentry_ok)
6767 {
6768 	ip_stack_t	*ipst;
6769 	ipsq_t		*ipsq;
6770 
6771 	/* Only 1 of ipif or ill can be specified */
6772 	ASSERT((ipif != NULL) ^ (ill != NULL));
6773 
6774 	if (ipif != NULL)
6775 		ill = ipif->ipif_ill;
6776 	ipst = ill->ill_ipst;
6777 
6778 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6779 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6780 	rw_exit(&ipst->ips_ill_g_lock);
6781 
6782 	return (ipsq);
6783 }
6784 
6785 /*
6786  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6787  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6788  * cannot be entered, the mp is queued for completion.
6789  */
6790 void
6791 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6792     boolean_t reentry_ok)
6793 {
6794 	ipsq_t	*ipsq;
6795 
6796 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6797 
6798 	/*
6799 	 * Drop the caller's refhold on the ill.  This is safe since we either
6800 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6801 	 * IPSQ, in which case we return without accessing ill anymore.  This
6802 	 * is needed because func needs to see the correct refcount.
6803 	 * e.g. removeif can work only then.
6804 	 */
6805 	ill_refrele(ill);
6806 	if (ipsq != NULL) {
6807 		(*func)(ipsq, q, mp, NULL);
6808 		ipsq_exit(ipsq);
6809 	}
6810 }
6811 
6812 /*
6813  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6814  * prior to exiting.  Caller must be writer on the specified IPSQ.
6815  */
6816 void
6817 ipsq_exit(ipsq_t *ipsq)
6818 {
6819 	mblk_t *mp;
6820 	ipsq_t *mp_ipsq;
6821 	queue_t	*q;
6822 	phyint_t *phyi;
6823 	ipsq_func_t func;
6824 
6825 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6826 
6827 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6828 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6829 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6830 		return;
6831 	}
6832 
6833 	for (;;) {
6834 		phyi = ipsq->ipsq_phyint;
6835 		mp = ipsq_dq(ipsq);
6836 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6837 
6838 		/*
6839 		 * If we've changed to a new IPSQ, and the phyint associated
6840 		 * with the old one has gone away, free the old IPSQ.  Note
6841 		 * that this cannot happen while the IPSQ is in a group.
6842 		 */
6843 		if (mp_ipsq != ipsq && phyi == NULL) {
6844 			ASSERT(ipsq->ipsq_next == ipsq);
6845 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6846 			ipsq_delete(ipsq);
6847 		}
6848 
6849 		if (mp == NULL)
6850 			break;
6851 
6852 		q = mp->b_queue;
6853 		func = (ipsq_func_t)mp->b_prev;
6854 		ipsq = mp_ipsq;
6855 		mp->b_next = mp->b_prev = NULL;
6856 		mp->b_queue = NULL;
6857 
6858 		/*
6859 		 * If 'q' is an conn queue, it is valid, since we did a
6860 		 * a refhold on the conn at the start of the ioctl.
6861 		 * If 'q' is an ill queue, it is valid, since close of an
6862 		 * ill will clean up its IPSQ.
6863 		 */
6864 		(*func)(ipsq, q, mp, NULL);
6865 	}
6866 }
6867 
6868 /*
6869  * Used to start any igmp or mld timers that could not be started
6870  * while holding ill_mcast_lock. The timers can't be started while holding
6871  * the lock, since mld/igmp_start_timers may need to call untimeout()
6872  * which can't be done while holding the lock which the timeout handler
6873  * acquires. Otherwise
6874  * there could be a deadlock since the timeout handlers
6875  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6876  * ill_mcast_lock.
6877  */
6878 void
6879 ill_mcast_timer_start(ip_stack_t *ipst)
6880 {
6881 	int		next;
6882 
6883 	mutex_enter(&ipst->ips_igmp_timer_lock);
6884 	next = ipst->ips_igmp_deferred_next;
6885 	ipst->ips_igmp_deferred_next = INFINITY;
6886 	mutex_exit(&ipst->ips_igmp_timer_lock);
6887 
6888 	if (next != INFINITY)
6889 		igmp_start_timers(next, ipst);
6890 
6891 	mutex_enter(&ipst->ips_mld_timer_lock);
6892 	next = ipst->ips_mld_deferred_next;
6893 	ipst->ips_mld_deferred_next = INFINITY;
6894 	mutex_exit(&ipst->ips_mld_timer_lock);
6895 
6896 	if (next != INFINITY)
6897 		mld_start_timers(next, ipst);
6898 }
6899 
6900 /*
6901  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6902  * and `ioccmd'.
6903  */
6904 void
6905 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6906 {
6907 	ill_t *ill = ipif->ipif_ill;
6908 	ipxop_t *ipx = ipsq->ipsq_xop;
6909 
6910 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6911 	ASSERT(ipx->ipx_current_ipif == NULL);
6912 	ASSERT(ipx->ipx_current_ioctl == 0);
6913 
6914 	ipx->ipx_current_done = B_FALSE;
6915 	ipx->ipx_current_ioctl = ioccmd;
6916 	mutex_enter(&ipx->ipx_lock);
6917 	ipx->ipx_current_ipif = ipif;
6918 	mutex_exit(&ipx->ipx_lock);
6919 
6920 	/*
6921 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6922 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6923 	 * references to the ipif (so that the references will eventually
6924 	 * drop to zero) and also prevents any "get" operations (e.g.,
6925 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6926 	 * operation has completed and the ipif is again in a stable state.
6927 	 *
6928 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6929 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6930 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6931 	 * ipifs will be affected.
6932 	 *
6933 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6934 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6935 	 * operate on.
6936 	 */
6937 	switch (ioccmd) {
6938 	case SIOCLIFREMOVEIF:
6939 		break;
6940 	case 0:
6941 		mutex_enter(&ill->ill_lock);
6942 		ipif = ipif->ipif_ill->ill_ipif;
6943 		for (; ipif != NULL; ipif = ipif->ipif_next)
6944 			ipif->ipif_state_flags |= IPIF_CHANGING;
6945 		mutex_exit(&ill->ill_lock);
6946 		break;
6947 	default:
6948 		mutex_enter(&ill->ill_lock);
6949 		ipif->ipif_state_flags |= IPIF_CHANGING;
6950 		mutex_exit(&ill->ill_lock);
6951 	}
6952 }
6953 
6954 /*
6955  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6956  * the next exclusive operation to begin once we ipsq_exit().  However, if
6957  * pending DLPI operations remain, then we will wait for the queue to drain
6958  * before allowing the next exclusive operation to begin.  This ensures that
6959  * DLPI operations from one exclusive operation are never improperly processed
6960  * as part of a subsequent exclusive operation.
6961  */
6962 void
6963 ipsq_current_finish(ipsq_t *ipsq)
6964 {
6965 	ipxop_t	*ipx = ipsq->ipsq_xop;
6966 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6967 	ipif_t	*ipif = ipx->ipx_current_ipif;
6968 
6969 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6970 
6971 	/*
6972 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6973 	 * (but in that case, IPIF_CHANGING will already be clear and no
6974 	 * pending DLPI messages can remain).
6975 	 */
6976 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6977 		ill_t *ill = ipif->ipif_ill;
6978 
6979 		mutex_enter(&ill->ill_lock);
6980 		dlpi_pending = ill->ill_dlpi_pending;
6981 		if (ipx->ipx_current_ioctl == 0) {
6982 			ipif = ill->ill_ipif;
6983 			for (; ipif != NULL; ipif = ipif->ipif_next)
6984 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6985 		} else {
6986 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6987 		}
6988 		mutex_exit(&ill->ill_lock);
6989 	}
6990 
6991 	ASSERT(!ipx->ipx_current_done);
6992 	ipx->ipx_current_done = B_TRUE;
6993 	ipx->ipx_current_ioctl = 0;
6994 	if (dlpi_pending == DL_PRIM_INVAL) {
6995 		mutex_enter(&ipx->ipx_lock);
6996 		ipx->ipx_current_ipif = NULL;
6997 		mutex_exit(&ipx->ipx_lock);
6998 	}
6999 }
7000 
7001 /*
7002  * The ill is closing. Flush all messages on the ipsq that originated
7003  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7004  * for this ill since ipsq_enter could not have entered until then.
7005  * New messages can't be queued since the CONDEMNED flag is set.
7006  */
7007 static void
7008 ipsq_flush(ill_t *ill)
7009 {
7010 	queue_t	*q;
7011 	mblk_t	*prev;
7012 	mblk_t	*mp;
7013 	mblk_t	*mp_next;
7014 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7015 
7016 	ASSERT(IAM_WRITER_ILL(ill));
7017 
7018 	/*
7019 	 * Flush any messages sent up by the driver.
7020 	 */
7021 	mutex_enter(&ipx->ipx_lock);
7022 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7023 		mp_next = mp->b_next;
7024 		q = mp->b_queue;
7025 		if (q == ill->ill_rq || q == ill->ill_wq) {
7026 			/* dequeue mp */
7027 			if (prev == NULL)
7028 				ipx->ipx_mphead = mp->b_next;
7029 			else
7030 				prev->b_next = mp->b_next;
7031 			if (ipx->ipx_mptail == mp) {
7032 				ASSERT(mp_next == NULL);
7033 				ipx->ipx_mptail = prev;
7034 			}
7035 			inet_freemsg(mp);
7036 		} else {
7037 			prev = mp;
7038 		}
7039 	}
7040 	mutex_exit(&ipx->ipx_lock);
7041 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7042 	ipsq_xopq_mp_cleanup(ill, NULL);
7043 }
7044 
7045 /*
7046  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7047  * and return the associated ipif.
7048  * Return value:
7049  *	Non zero: An error has occurred. ci may not be filled out.
7050  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7051  *	a held ipif in ci.ci_ipif.
7052  */
7053 int
7054 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7055     cmd_info_t *ci)
7056 {
7057 	char		*name;
7058 	struct ifreq    *ifr;
7059 	struct lifreq    *lifr;
7060 	ipif_t		*ipif = NULL;
7061 	ill_t		*ill;
7062 	conn_t		*connp;
7063 	boolean_t	isv6;
7064 	boolean_t	exists;
7065 	mblk_t		*mp1;
7066 	zoneid_t	zoneid;
7067 	ip_stack_t	*ipst;
7068 
7069 	if (q->q_next != NULL) {
7070 		ill = (ill_t *)q->q_ptr;
7071 		isv6 = ill->ill_isv6;
7072 		connp = NULL;
7073 		zoneid = ALL_ZONES;
7074 		ipst = ill->ill_ipst;
7075 	} else {
7076 		ill = NULL;
7077 		connp = Q_TO_CONN(q);
7078 		isv6 = (connp->conn_family == AF_INET6);
7079 		zoneid = connp->conn_zoneid;
7080 		if (zoneid == GLOBAL_ZONEID) {
7081 			/* global zone can access ipifs in all zones */
7082 			zoneid = ALL_ZONES;
7083 		}
7084 		ipst = connp->conn_netstack->netstack_ip;
7085 	}
7086 
7087 	/* Has been checked in ip_wput_nondata */
7088 	mp1 = mp->b_cont->b_cont;
7089 
7090 	if (ipip->ipi_cmd_type == IF_CMD) {
7091 		/* This a old style SIOC[GS]IF* command */
7092 		ifr = (struct ifreq *)mp1->b_rptr;
7093 		/*
7094 		 * Null terminate the string to protect against buffer
7095 		 * overrun. String was generated by user code and may not
7096 		 * be trusted.
7097 		 */
7098 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7099 		name = ifr->ifr_name;
7100 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7101 		ci->ci_sin6 = NULL;
7102 		ci->ci_lifr = (struct lifreq *)ifr;
7103 	} else {
7104 		/* This a new style SIOC[GS]LIF* command */
7105 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7106 		lifr = (struct lifreq *)mp1->b_rptr;
7107 		/*
7108 		 * Null terminate the string to protect against buffer
7109 		 * overrun. String was generated by user code and may not
7110 		 * be trusted.
7111 		 */
7112 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7113 		name = lifr->lifr_name;
7114 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7115 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7116 		ci->ci_lifr = lifr;
7117 	}
7118 
7119 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7120 		/*
7121 		 * The ioctl will be failed if the ioctl comes down
7122 		 * an conn stream
7123 		 */
7124 		if (ill == NULL) {
7125 			/*
7126 			 * Not an ill queue, return EINVAL same as the
7127 			 * old error code.
7128 			 */
7129 			return (ENXIO);
7130 		}
7131 		ipif = ill->ill_ipif;
7132 		ipif_refhold(ipif);
7133 	} else {
7134 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7135 		    &exists, isv6, zoneid, ipst);
7136 
7137 		/*
7138 		 * Ensure that get ioctls don't see any internal state changes
7139 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7140 		 * set.
7141 		 */
7142 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7143 		    !IAM_WRITER_IPIF(ipif)) {
7144 			ipsq_t	*ipsq;
7145 
7146 			if (connp != NULL)
7147 				mutex_enter(&connp->conn_lock);
7148 			mutex_enter(&ipif->ipif_ill->ill_lock);
7149 			if (IPIF_IS_CHANGING(ipif) &&
7150 			    !IPIF_IS_CONDEMNED(ipif)) {
7151 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7152 				mutex_enter(&ipsq->ipsq_lock);
7153 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7154 				mutex_exit(&ipif->ipif_ill->ill_lock);
7155 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7156 				    NEW_OP, ipif->ipif_ill);
7157 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7158 				mutex_exit(&ipsq->ipsq_lock);
7159 				if (connp != NULL)
7160 					mutex_exit(&connp->conn_lock);
7161 				ipif_refrele(ipif);
7162 				return (EINPROGRESS);
7163 			}
7164 			mutex_exit(&ipif->ipif_ill->ill_lock);
7165 			if (connp != NULL)
7166 				mutex_exit(&connp->conn_lock);
7167 		}
7168 	}
7169 
7170 	/*
7171 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7172 	 */
7173 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7174 		ipif_refrele(ipif);
7175 		return (ENXIO);
7176 	}
7177 
7178 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7179 	    name[0] == '\0') {
7180 		/*
7181 		 * Handle a or a SIOC?IF* with a null name
7182 		 * during plumb (on the ill queue before the I_PLINK).
7183 		 */
7184 		ipif = ill->ill_ipif;
7185 		ipif_refhold(ipif);
7186 	}
7187 
7188 	if (ipif == NULL)
7189 		return (ENXIO);
7190 
7191 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7192 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7193 
7194 	ci->ci_ipif = ipif;
7195 	return (0);
7196 }
7197 
7198 /*
7199  * Return the total number of ipifs.
7200  */
7201 static uint_t
7202 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7203 {
7204 	uint_t numifs = 0;
7205 	ill_t	*ill;
7206 	ill_walk_context_t	ctx;
7207 	ipif_t	*ipif;
7208 
7209 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7210 	ill = ILL_START_WALK_V4(&ctx, ipst);
7211 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7212 		if (IS_UNDER_IPMP(ill))
7213 			continue;
7214 		for (ipif = ill->ill_ipif; ipif != NULL;
7215 		    ipif = ipif->ipif_next) {
7216 			if (ipif->ipif_zoneid == zoneid ||
7217 			    ipif->ipif_zoneid == ALL_ZONES)
7218 				numifs++;
7219 		}
7220 	}
7221 	rw_exit(&ipst->ips_ill_g_lock);
7222 	return (numifs);
7223 }
7224 
7225 /*
7226  * Return the total number of ipifs.
7227  */
7228 static uint_t
7229 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7230 {
7231 	uint_t numifs = 0;
7232 	ill_t	*ill;
7233 	ipif_t	*ipif;
7234 	ill_walk_context_t	ctx;
7235 
7236 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7237 
7238 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7239 	if (family == AF_INET)
7240 		ill = ILL_START_WALK_V4(&ctx, ipst);
7241 	else if (family == AF_INET6)
7242 		ill = ILL_START_WALK_V6(&ctx, ipst);
7243 	else
7244 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7245 
7246 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7247 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7248 			continue;
7249 
7250 		for (ipif = ill->ill_ipif; ipif != NULL;
7251 		    ipif = ipif->ipif_next) {
7252 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7253 			    !(lifn_flags & LIFC_NOXMIT))
7254 				continue;
7255 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7256 			    !(lifn_flags & LIFC_TEMPORARY))
7257 				continue;
7258 			if (((ipif->ipif_flags &
7259 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7260 			    IPIF_DEPRECATED)) ||
7261 			    IS_LOOPBACK(ill) ||
7262 			    !(ipif->ipif_flags & IPIF_UP)) &&
7263 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7264 				continue;
7265 
7266 			if (zoneid != ipif->ipif_zoneid &&
7267 			    ipif->ipif_zoneid != ALL_ZONES &&
7268 			    (zoneid != GLOBAL_ZONEID ||
7269 			    !(lifn_flags & LIFC_ALLZONES)))
7270 				continue;
7271 
7272 			numifs++;
7273 		}
7274 	}
7275 	rw_exit(&ipst->ips_ill_g_lock);
7276 	return (numifs);
7277 }
7278 
7279 uint_t
7280 ip_get_lifsrcofnum(ill_t *ill)
7281 {
7282 	uint_t numifs = 0;
7283 	ill_t	*ill_head = ill;
7284 	ip_stack_t	*ipst = ill->ill_ipst;
7285 
7286 	/*
7287 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7288 	 * other thread may be trying to relink the ILLs in this usesrc group
7289 	 * and adjusting the ill_usesrc_grp_next pointers
7290 	 */
7291 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7292 	if ((ill->ill_usesrc_ifindex == 0) &&
7293 	    (ill->ill_usesrc_grp_next != NULL)) {
7294 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7295 		    ill = ill->ill_usesrc_grp_next)
7296 			numifs++;
7297 	}
7298 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7299 
7300 	return (numifs);
7301 }
7302 
7303 /* Null values are passed in for ipif, sin, and ifreq */
7304 /* ARGSUSED */
7305 int
7306 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7307     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7308 {
7309 	int *nump;
7310 	conn_t *connp = Q_TO_CONN(q);
7311 
7312 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7313 
7314 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7315 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7316 
7317 	*nump = ip_get_numifs(connp->conn_zoneid,
7318 	    connp->conn_netstack->netstack_ip);
7319 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7320 	return (0);
7321 }
7322 
7323 /* Null values are passed in for ipif, sin, and ifreq */
7324 /* ARGSUSED */
7325 int
7326 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7327     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7328 {
7329 	struct lifnum *lifn;
7330 	mblk_t	*mp1;
7331 	conn_t *connp = Q_TO_CONN(q);
7332 
7333 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7334 
7335 	/* Existence checked in ip_wput_nondata */
7336 	mp1 = mp->b_cont->b_cont;
7337 
7338 	lifn = (struct lifnum *)mp1->b_rptr;
7339 	switch (lifn->lifn_family) {
7340 	case AF_UNSPEC:
7341 	case AF_INET:
7342 	case AF_INET6:
7343 		break;
7344 	default:
7345 		return (EAFNOSUPPORT);
7346 	}
7347 
7348 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7349 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7350 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7351 	return (0);
7352 }
7353 
7354 /* ARGSUSED */
7355 int
7356 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7357     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7358 {
7359 	STRUCT_HANDLE(ifconf, ifc);
7360 	mblk_t *mp1;
7361 	struct iocblk *iocp;
7362 	struct ifreq *ifr;
7363 	ill_walk_context_t	ctx;
7364 	ill_t	*ill;
7365 	ipif_t	*ipif;
7366 	struct sockaddr_in *sin;
7367 	int32_t	ifclen;
7368 	zoneid_t zoneid;
7369 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7370 
7371 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7372 
7373 	ip1dbg(("ip_sioctl_get_ifconf"));
7374 	/* Existence verified in ip_wput_nondata */
7375 	mp1 = mp->b_cont->b_cont;
7376 	iocp = (struct iocblk *)mp->b_rptr;
7377 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7378 
7379 	/*
7380 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7381 	 * the user buffer address and length into which the list of struct
7382 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7383 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7384 	 * the SIOCGIFCONF operation was redefined to simply provide
7385 	 * a large output buffer into which we are supposed to jam the ifreq
7386 	 * array.  The same ioctl command code was used, despite the fact that
7387 	 * both the applications and the kernel code had to change, thus making
7388 	 * it impossible to support both interfaces.
7389 	 *
7390 	 * For reasons not good enough to try to explain, the following
7391 	 * algorithm is used for deciding what to do with one of these:
7392 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7393 	 * form with the output buffer coming down as the continuation message.
7394 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7395 	 * and we have to copy in the ifconf structure to find out how big the
7396 	 * output buffer is and where to copy out to.  Sure no problem...
7397 	 *
7398 	 */
7399 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7400 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7401 		int numifs = 0;
7402 		size_t ifc_bufsize;
7403 
7404 		/*
7405 		 * Must be (better be!) continuation of a TRANSPARENT
7406 		 * IOCTL.  We just copied in the ifconf structure.
7407 		 */
7408 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7409 		    (struct ifconf *)mp1->b_rptr);
7410 
7411 		/*
7412 		 * Allocate a buffer to hold requested information.
7413 		 *
7414 		 * If ifc_len is larger than what is needed, we only
7415 		 * allocate what we will use.
7416 		 *
7417 		 * If ifc_len is smaller than what is needed, return
7418 		 * EINVAL.
7419 		 *
7420 		 * XXX: the ill_t structure can hava 2 counters, for
7421 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7422 		 * number of interfaces for a device, so we don't need
7423 		 * to count them here...
7424 		 */
7425 		numifs = ip_get_numifs(zoneid, ipst);
7426 
7427 		ifclen = STRUCT_FGET(ifc, ifc_len);
7428 		ifc_bufsize = numifs * sizeof (struct ifreq);
7429 		if (ifc_bufsize > ifclen) {
7430 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7431 				/* old behaviour */
7432 				return (EINVAL);
7433 			} else {
7434 				ifc_bufsize = ifclen;
7435 			}
7436 		}
7437 
7438 		mp1 = mi_copyout_alloc(q, mp,
7439 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7440 		if (mp1 == NULL)
7441 			return (ENOMEM);
7442 
7443 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7444 	}
7445 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7446 	/*
7447 	 * the SIOCGIFCONF ioctl only knows about
7448 	 * IPv4 addresses, so don't try to tell
7449 	 * it about interfaces with IPv6-only
7450 	 * addresses. (Last parm 'isv6' is B_FALSE)
7451 	 */
7452 
7453 	ifr = (struct ifreq *)mp1->b_rptr;
7454 
7455 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7456 	ill = ILL_START_WALK_V4(&ctx, ipst);
7457 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7458 		if (IS_UNDER_IPMP(ill))
7459 			continue;
7460 		for (ipif = ill->ill_ipif; ipif != NULL;
7461 		    ipif = ipif->ipif_next) {
7462 			if (zoneid != ipif->ipif_zoneid &&
7463 			    ipif->ipif_zoneid != ALL_ZONES)
7464 				continue;
7465 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7466 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7467 					/* old behaviour */
7468 					rw_exit(&ipst->ips_ill_g_lock);
7469 					return (EINVAL);
7470 				} else {
7471 					goto if_copydone;
7472 				}
7473 			}
7474 			ipif_get_name(ipif, ifr->ifr_name,
7475 			    sizeof (ifr->ifr_name));
7476 			sin = (sin_t *)&ifr->ifr_addr;
7477 			*sin = sin_null;
7478 			sin->sin_family = AF_INET;
7479 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7480 			ifr++;
7481 		}
7482 	}
7483 if_copydone:
7484 	rw_exit(&ipst->ips_ill_g_lock);
7485 	mp1->b_wptr = (uchar_t *)ifr;
7486 
7487 	if (STRUCT_BUF(ifc) != NULL) {
7488 		STRUCT_FSET(ifc, ifc_len,
7489 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7490 	}
7491 	return (0);
7492 }
7493 
7494 /*
7495  * Get the interfaces using the address hosted on the interface passed in,
7496  * as a source adddress
7497  */
7498 /* ARGSUSED */
7499 int
7500 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7501     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7502 {
7503 	mblk_t *mp1;
7504 	ill_t	*ill, *ill_head;
7505 	ipif_t	*ipif, *orig_ipif;
7506 	int	numlifs = 0;
7507 	size_t	lifs_bufsize, lifsmaxlen;
7508 	struct	lifreq *lifr;
7509 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7510 	uint_t	ifindex;
7511 	zoneid_t zoneid;
7512 	boolean_t isv6 = B_FALSE;
7513 	struct	sockaddr_in	*sin;
7514 	struct	sockaddr_in6	*sin6;
7515 	STRUCT_HANDLE(lifsrcof, lifs);
7516 	ip_stack_t		*ipst;
7517 
7518 	ipst = CONNQ_TO_IPST(q);
7519 
7520 	ASSERT(q->q_next == NULL);
7521 
7522 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7523 
7524 	/* Existence verified in ip_wput_nondata */
7525 	mp1 = mp->b_cont->b_cont;
7526 
7527 	/*
7528 	 * Must be (better be!) continuation of a TRANSPARENT
7529 	 * IOCTL.  We just copied in the lifsrcof structure.
7530 	 */
7531 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7532 	    (struct lifsrcof *)mp1->b_rptr);
7533 
7534 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7535 		return (EINVAL);
7536 
7537 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7538 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7539 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7540 	if (ipif == NULL) {
7541 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7542 		    ifindex));
7543 		return (ENXIO);
7544 	}
7545 
7546 	/* Allocate a buffer to hold requested information */
7547 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7548 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7549 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7550 	/* The actual size needed is always returned in lifs_len */
7551 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7552 
7553 	/* If the amount we need is more than what is passed in, abort */
7554 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7555 		ipif_refrele(ipif);
7556 		return (0);
7557 	}
7558 
7559 	mp1 = mi_copyout_alloc(q, mp,
7560 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7561 	if (mp1 == NULL) {
7562 		ipif_refrele(ipif);
7563 		return (ENOMEM);
7564 	}
7565 
7566 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7567 	bzero(mp1->b_rptr, lifs_bufsize);
7568 
7569 	lifr = (struct lifreq *)mp1->b_rptr;
7570 
7571 	ill = ill_head = ipif->ipif_ill;
7572 	orig_ipif = ipif;
7573 
7574 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7575 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7576 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7577 
7578 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7579 	for (; (ill != NULL) && (ill != ill_head);
7580 	    ill = ill->ill_usesrc_grp_next) {
7581 
7582 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7583 			break;
7584 
7585 		ipif = ill->ill_ipif;
7586 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7587 		if (ipif->ipif_isv6) {
7588 			sin6 = (sin6_t *)&lifr->lifr_addr;
7589 			*sin6 = sin6_null;
7590 			sin6->sin6_family = AF_INET6;
7591 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7592 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7593 			    &ipif->ipif_v6net_mask);
7594 		} else {
7595 			sin = (sin_t *)&lifr->lifr_addr;
7596 			*sin = sin_null;
7597 			sin->sin_family = AF_INET;
7598 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7599 			lifr->lifr_addrlen = ip_mask_to_plen(
7600 			    ipif->ipif_net_mask);
7601 		}
7602 		lifr++;
7603 	}
7604 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7605 	rw_exit(&ipst->ips_ill_g_lock);
7606 	ipif_refrele(orig_ipif);
7607 	mp1->b_wptr = (uchar_t *)lifr;
7608 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7609 
7610 	return (0);
7611 }
7612 
7613 /* ARGSUSED */
7614 int
7615 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7616     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7617 {
7618 	mblk_t *mp1;
7619 	int	list;
7620 	ill_t	*ill;
7621 	ipif_t	*ipif;
7622 	int	flags;
7623 	int	numlifs = 0;
7624 	size_t	lifc_bufsize;
7625 	struct	lifreq *lifr;
7626 	sa_family_t	family;
7627 	struct	sockaddr_in	*sin;
7628 	struct	sockaddr_in6	*sin6;
7629 	ill_walk_context_t	ctx;
7630 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7631 	int32_t	lifclen;
7632 	zoneid_t zoneid;
7633 	STRUCT_HANDLE(lifconf, lifc);
7634 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7635 
7636 	ip1dbg(("ip_sioctl_get_lifconf"));
7637 
7638 	ASSERT(q->q_next == NULL);
7639 
7640 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7641 
7642 	/* Existence verified in ip_wput_nondata */
7643 	mp1 = mp->b_cont->b_cont;
7644 
7645 	/*
7646 	 * An extended version of SIOCGIFCONF that takes an
7647 	 * additional address family and flags field.
7648 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7649 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7650 	 * interfaces are omitted.
7651 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7652 	 * unless LIFC_TEMPORARY is specified.
7653 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7654 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7655 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7656 	 * has priority over LIFC_NOXMIT.
7657 	 */
7658 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7659 
7660 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7661 		return (EINVAL);
7662 
7663 	/*
7664 	 * Must be (better be!) continuation of a TRANSPARENT
7665 	 * IOCTL.  We just copied in the lifconf structure.
7666 	 */
7667 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7668 
7669 	family = STRUCT_FGET(lifc, lifc_family);
7670 	flags = STRUCT_FGET(lifc, lifc_flags);
7671 
7672 	switch (family) {
7673 	case AF_UNSPEC:
7674 		/*
7675 		 * walk all ILL's.
7676 		 */
7677 		list = MAX_G_HEADS;
7678 		break;
7679 	case AF_INET:
7680 		/*
7681 		 * walk only IPV4 ILL's.
7682 		 */
7683 		list = IP_V4_G_HEAD;
7684 		break;
7685 	case AF_INET6:
7686 		/*
7687 		 * walk only IPV6 ILL's.
7688 		 */
7689 		list = IP_V6_G_HEAD;
7690 		break;
7691 	default:
7692 		return (EAFNOSUPPORT);
7693 	}
7694 
7695 	/*
7696 	 * Allocate a buffer to hold requested information.
7697 	 *
7698 	 * If lifc_len is larger than what is needed, we only
7699 	 * allocate what we will use.
7700 	 *
7701 	 * If lifc_len is smaller than what is needed, return
7702 	 * EINVAL.
7703 	 */
7704 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7705 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7706 	lifclen = STRUCT_FGET(lifc, lifc_len);
7707 	if (lifc_bufsize > lifclen) {
7708 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7709 			return (EINVAL);
7710 		else
7711 			lifc_bufsize = lifclen;
7712 	}
7713 
7714 	mp1 = mi_copyout_alloc(q, mp,
7715 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7716 	if (mp1 == NULL)
7717 		return (ENOMEM);
7718 
7719 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7720 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7721 
7722 	lifr = (struct lifreq *)mp1->b_rptr;
7723 
7724 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7725 	ill = ill_first(list, list, &ctx, ipst);
7726 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7727 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7728 			continue;
7729 
7730 		for (ipif = ill->ill_ipif; ipif != NULL;
7731 		    ipif = ipif->ipif_next) {
7732 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7733 			    !(flags & LIFC_NOXMIT))
7734 				continue;
7735 
7736 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7737 			    !(flags & LIFC_TEMPORARY))
7738 				continue;
7739 
7740 			if (((ipif->ipif_flags &
7741 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7742 			    IPIF_DEPRECATED)) ||
7743 			    IS_LOOPBACK(ill) ||
7744 			    !(ipif->ipif_flags & IPIF_UP)) &&
7745 			    (flags & LIFC_EXTERNAL_SOURCE))
7746 				continue;
7747 
7748 			if (zoneid != ipif->ipif_zoneid &&
7749 			    ipif->ipif_zoneid != ALL_ZONES &&
7750 			    (zoneid != GLOBAL_ZONEID ||
7751 			    !(flags & LIFC_ALLZONES)))
7752 				continue;
7753 
7754 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7755 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7756 					rw_exit(&ipst->ips_ill_g_lock);
7757 					return (EINVAL);
7758 				} else {
7759 					goto lif_copydone;
7760 				}
7761 			}
7762 
7763 			ipif_get_name(ipif, lifr->lifr_name,
7764 			    sizeof (lifr->lifr_name));
7765 			lifr->lifr_type = ill->ill_type;
7766 			if (ipif->ipif_isv6) {
7767 				sin6 = (sin6_t *)&lifr->lifr_addr;
7768 				*sin6 = sin6_null;
7769 				sin6->sin6_family = AF_INET6;
7770 				sin6->sin6_addr =
7771 				    ipif->ipif_v6lcl_addr;
7772 				lifr->lifr_addrlen =
7773 				    ip_mask_to_plen_v6(
7774 				    &ipif->ipif_v6net_mask);
7775 			} else {
7776 				sin = (sin_t *)&lifr->lifr_addr;
7777 				*sin = sin_null;
7778 				sin->sin_family = AF_INET;
7779 				sin->sin_addr.s_addr =
7780 				    ipif->ipif_lcl_addr;
7781 				lifr->lifr_addrlen =
7782 				    ip_mask_to_plen(
7783 				    ipif->ipif_net_mask);
7784 			}
7785 			lifr++;
7786 		}
7787 	}
7788 lif_copydone:
7789 	rw_exit(&ipst->ips_ill_g_lock);
7790 
7791 	mp1->b_wptr = (uchar_t *)lifr;
7792 	if (STRUCT_BUF(lifc) != NULL) {
7793 		STRUCT_FSET(lifc, lifc_len,
7794 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7795 	}
7796 	return (0);
7797 }
7798 
7799 static void
7800 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7801 {
7802 	ip6_asp_t *table;
7803 	size_t table_size;
7804 	mblk_t *data_mp;
7805 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7806 	ip_stack_t	*ipst;
7807 
7808 	if (q->q_next == NULL)
7809 		ipst = CONNQ_TO_IPST(q);
7810 	else
7811 		ipst = ILLQ_TO_IPST(q);
7812 
7813 	/* These two ioctls are I_STR only */
7814 	if (iocp->ioc_count == TRANSPARENT) {
7815 		miocnak(q, mp, 0, EINVAL);
7816 		return;
7817 	}
7818 
7819 	data_mp = mp->b_cont;
7820 	if (data_mp == NULL) {
7821 		/* The user passed us a NULL argument */
7822 		table = NULL;
7823 		table_size = iocp->ioc_count;
7824 	} else {
7825 		/*
7826 		 * The user provided a table.  The stream head
7827 		 * may have copied in the user data in chunks,
7828 		 * so make sure everything is pulled up
7829 		 * properly.
7830 		 */
7831 		if (MBLKL(data_mp) < iocp->ioc_count) {
7832 			mblk_t *new_data_mp;
7833 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7834 			    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 		table = (ip6_asp_t *)data_mp->b_rptr;
7843 		table_size = iocp->ioc_count;
7844 	}
7845 
7846 	switch (iocp->ioc_cmd) {
7847 	case SIOCGIP6ADDRPOLICY:
7848 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7849 		if (iocp->ioc_rval == -1)
7850 			iocp->ioc_error = EINVAL;
7851 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7852 		else if (table != NULL &&
7853 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7854 			ip6_asp_t *src = table;
7855 			ip6_asp32_t *dst = (void *)table;
7856 			int count = table_size / sizeof (ip6_asp_t);
7857 			int i;
7858 
7859 			/*
7860 			 * We need to do an in-place shrink of the array
7861 			 * to match the alignment attributes of the
7862 			 * 32-bit ABI looking at it.
7863 			 */
7864 			/* LINTED: logical expression always true: op "||" */
7865 			ASSERT(sizeof (*src) > sizeof (*dst));
7866 			for (i = 1; i < count; i++)
7867 				bcopy(src + i, dst + i, sizeof (*dst));
7868 		}
7869 #endif
7870 		break;
7871 
7872 	case SIOCSIP6ADDRPOLICY:
7873 		ASSERT(mp->b_prev == NULL);
7874 		mp->b_prev = (void *)q;
7875 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7876 		/*
7877 		 * We pass in the datamodel here so that the ip6_asp_replace()
7878 		 * routine can handle converting from 32-bit to native formats
7879 		 * where necessary.
7880 		 *
7881 		 * A better way to handle this might be to convert the inbound
7882 		 * data structure here, and hang it off a new 'mp'; thus the
7883 		 * ip6_asp_replace() logic would always be dealing with native
7884 		 * format data structures..
7885 		 *
7886 		 * (An even simpler way to handle these ioctls is to just
7887 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7888 		 * and just recompile everything that depends on it.)
7889 		 */
7890 #endif
7891 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7892 		    iocp->ioc_flag & IOC_MODELS);
7893 		return;
7894 	}
7895 
7896 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7897 	qreply(q, mp);
7898 }
7899 
7900 static void
7901 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7902 {
7903 	mblk_t 		*data_mp;
7904 	struct dstinforeq	*dir;
7905 	uint8_t		*end, *cur;
7906 	in6_addr_t	*daddr, *saddr;
7907 	ipaddr_t	v4daddr;
7908 	ire_t		*ire;
7909 	ipaddr_t	v4setsrc;
7910 	in6_addr_t	v6setsrc;
7911 	char		*slabel, *dlabel;
7912 	boolean_t	isipv4;
7913 	int		match_ire;
7914 	ill_t		*dst_ill;
7915 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7916 	conn_t		*connp = Q_TO_CONN(q);
7917 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7918 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7919 	uint64_t	ipif_flags;
7920 
7921 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7922 
7923 	/*
7924 	 * This ioctl is I_STR only, and must have a
7925 	 * data mblk following the M_IOCTL mblk.
7926 	 */
7927 	data_mp = mp->b_cont;
7928 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7929 		miocnak(q, mp, 0, EINVAL);
7930 		return;
7931 	}
7932 
7933 	if (MBLKL(data_mp) < iocp->ioc_count) {
7934 		mblk_t *new_data_mp;
7935 
7936 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7937 			miocnak(q, mp, 0, ENOMEM);
7938 			return;
7939 		}
7940 		freemsg(data_mp);
7941 		data_mp = new_data_mp;
7942 		mp->b_cont = data_mp;
7943 	}
7944 	match_ire = MATCH_IRE_DSTONLY;
7945 
7946 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7947 	    end - cur >= sizeof (struct dstinforeq);
7948 	    cur += sizeof (struct dstinforeq)) {
7949 		dir = (struct dstinforeq *)cur;
7950 		daddr = &dir->dir_daddr;
7951 		saddr = &dir->dir_saddr;
7952 
7953 		/*
7954 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7955 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7956 		 * and ip_select_source_v6() do not.
7957 		 */
7958 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7959 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7960 
7961 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7962 		if (isipv4) {
7963 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7964 			v4setsrc = INADDR_ANY;
7965 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7966 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7967 			    NULL, NULL);
7968 		} else {
7969 			v6setsrc = ipv6_all_zeros;
7970 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7971 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7972 			    NULL, NULL);
7973 		}
7974 		ASSERT(ire != NULL);
7975 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7976 			ire_refrele(ire);
7977 			dir->dir_dreachable = 0;
7978 
7979 			/* move on to next dst addr */
7980 			continue;
7981 		}
7982 		dir->dir_dreachable = 1;
7983 
7984 		dst_ill = ire_nexthop_ill(ire);
7985 		if (dst_ill == NULL) {
7986 			ire_refrele(ire);
7987 			continue;
7988 		}
7989 
7990 		/* With ipmp we most likely look at the ipmp ill here */
7991 		dir->dir_dmactype = dst_ill->ill_mactype;
7992 
7993 		if (isipv4) {
7994 			ipaddr_t v4saddr;
7995 
7996 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7997 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7998 			    &v4saddr, NULL, &ipif_flags) != 0) {
7999 				v4saddr = INADDR_ANY;
8000 				ipif_flags = 0;
8001 			}
8002 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
8003 		} else {
8004 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
8005 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8006 			    saddr, NULL, &ipif_flags) != 0) {
8007 				*saddr = ipv6_all_zeros;
8008 				ipif_flags = 0;
8009 			}
8010 		}
8011 
8012 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8013 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
8014 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8015 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8016 		ire_refrele(ire);
8017 		ill_refrele(dst_ill);
8018 	}
8019 	miocack(q, mp, iocp->ioc_count, 0);
8020 }
8021 
8022 /*
8023  * Check if this is an address assigned to this machine.
8024  * Skips interfaces that are down by using ire checks.
8025  * Translates mapped addresses to v4 addresses and then
8026  * treats them as such, returning true if the v4 address
8027  * associated with this mapped address is configured.
8028  * Note: Applications will have to be careful what they do
8029  * with the response; use of mapped addresses limits
8030  * what can be done with the socket, especially with
8031  * respect to socket options and ioctls - neither IPv4
8032  * options nor IPv6 sticky options/ancillary data options
8033  * may be used.
8034  */
8035 /* ARGSUSED */
8036 int
8037 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8038     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8039 {
8040 	struct sioc_addrreq *sia;
8041 	sin_t *sin;
8042 	ire_t *ire;
8043 	mblk_t *mp1;
8044 	zoneid_t zoneid;
8045 	ip_stack_t	*ipst;
8046 
8047 	ip1dbg(("ip_sioctl_tmyaddr"));
8048 
8049 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8050 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8051 	ipst = CONNQ_TO_IPST(q);
8052 
8053 	/* Existence verified in ip_wput_nondata */
8054 	mp1 = mp->b_cont->b_cont;
8055 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8056 	sin = (sin_t *)&sia->sa_addr;
8057 	switch (sin->sin_family) {
8058 	case AF_INET6: {
8059 		sin6_t *sin6 = (sin6_t *)sin;
8060 
8061 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8062 			ipaddr_t v4_addr;
8063 
8064 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8065 			    v4_addr);
8066 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8067 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8068 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8069 		} else {
8070 			in6_addr_t v6addr;
8071 
8072 			v6addr = sin6->sin6_addr;
8073 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8074 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8075 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8076 		}
8077 		break;
8078 	}
8079 	case AF_INET: {
8080 		ipaddr_t v4addr;
8081 
8082 		v4addr = sin->sin_addr.s_addr;
8083 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8084 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8085 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8086 		break;
8087 	}
8088 	default:
8089 		return (EAFNOSUPPORT);
8090 	}
8091 	if (ire != NULL) {
8092 		sia->sa_res = 1;
8093 		ire_refrele(ire);
8094 	} else {
8095 		sia->sa_res = 0;
8096 	}
8097 	return (0);
8098 }
8099 
8100 /*
8101  * Check if this is an address assigned on-link i.e. neighbor,
8102  * and makes sure it's reachable from the current zone.
8103  * Returns true for my addresses as well.
8104  * Translates mapped addresses to v4 addresses and then
8105  * treats them as such, returning true if the v4 address
8106  * associated with this mapped address is configured.
8107  * Note: Applications will have to be careful what they do
8108  * with the response; use of mapped addresses limits
8109  * what can be done with the socket, especially with
8110  * respect to socket options and ioctls - neither IPv4
8111  * options nor IPv6 sticky options/ancillary data options
8112  * may be used.
8113  */
8114 /* ARGSUSED */
8115 int
8116 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8117     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8118 {
8119 	struct sioc_addrreq *sia;
8120 	sin_t *sin;
8121 	mblk_t	*mp1;
8122 	ire_t *ire = NULL;
8123 	zoneid_t zoneid;
8124 	ip_stack_t	*ipst;
8125 
8126 	ip1dbg(("ip_sioctl_tonlink"));
8127 
8128 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8129 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8130 	ipst = CONNQ_TO_IPST(q);
8131 
8132 	/* Existence verified in ip_wput_nondata */
8133 	mp1 = mp->b_cont->b_cont;
8134 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8135 	sin = (sin_t *)&sia->sa_addr;
8136 
8137 	/*
8138 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8139 	 * to make sure we only look at on-link unicast address.
8140 	 */
8141 	switch (sin->sin_family) {
8142 	case AF_INET6: {
8143 		sin6_t *sin6 = (sin6_t *)sin;
8144 
8145 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8146 			ipaddr_t v4_addr;
8147 
8148 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8149 			    v4_addr);
8150 			if (!CLASSD(v4_addr)) {
8151 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8152 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8153 				    0, ipst, NULL);
8154 			}
8155 		} else {
8156 			in6_addr_t v6addr;
8157 
8158 			v6addr = sin6->sin6_addr;
8159 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8160 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8161 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8162 				    ipst, NULL);
8163 			}
8164 		}
8165 		break;
8166 	}
8167 	case AF_INET: {
8168 		ipaddr_t v4addr;
8169 
8170 		v4addr = sin->sin_addr.s_addr;
8171 		if (!CLASSD(v4addr)) {
8172 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8173 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8174 		}
8175 		break;
8176 	}
8177 	default:
8178 		return (EAFNOSUPPORT);
8179 	}
8180 	sia->sa_res = 0;
8181 	if (ire != NULL) {
8182 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8183 
8184 		if ((ire->ire_type & IRE_ONLINK) &&
8185 		    !(ire->ire_type & IRE_BROADCAST))
8186 			sia->sa_res = 1;
8187 		ire_refrele(ire);
8188 	}
8189 	return (0);
8190 }
8191 
8192 /*
8193  * TBD: implement when kernel maintaines a list of site prefixes.
8194  */
8195 /* ARGSUSED */
8196 int
8197 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8198     ip_ioctl_cmd_t *ipip, void *ifreq)
8199 {
8200 	return (ENXIO);
8201 }
8202 
8203 /* ARP IOCTLs. */
8204 /* ARGSUSED */
8205 int
8206 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8207     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8208 {
8209 	int		err;
8210 	ipaddr_t	ipaddr;
8211 	struct iocblk	*iocp;
8212 	conn_t		*connp;
8213 	struct arpreq	*ar;
8214 	struct xarpreq	*xar;
8215 	int		arp_flags, flags, alength;
8216 	uchar_t		*lladdr;
8217 	ip_stack_t	*ipst;
8218 	ill_t		*ill = ipif->ipif_ill;
8219 	ill_t		*proxy_ill = NULL;
8220 	ipmp_arpent_t	*entp = NULL;
8221 	boolean_t	proxyarp = B_FALSE;
8222 	boolean_t	if_arp_ioctl = B_FALSE;
8223 	ncec_t		*ncec = NULL;
8224 	nce_t		*nce;
8225 
8226 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8227 	connp = Q_TO_CONN(q);
8228 	ipst = connp->conn_netstack->netstack_ip;
8229 	iocp = (struct iocblk *)mp->b_rptr;
8230 
8231 	if (ipip->ipi_cmd_type == XARP_CMD) {
8232 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8233 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8234 		ar = NULL;
8235 
8236 		arp_flags = xar->xarp_flags;
8237 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8238 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8239 		/*
8240 		 * Validate against user's link layer address length
8241 		 * input and name and addr length limits.
8242 		 */
8243 		alength = ill->ill_phys_addr_length;
8244 		if (ipip->ipi_cmd == SIOCSXARP) {
8245 			if (alength != xar->xarp_ha.sdl_alen ||
8246 			    (alength + xar->xarp_ha.sdl_nlen >
8247 			    sizeof (xar->xarp_ha.sdl_data)))
8248 				return (EINVAL);
8249 		}
8250 	} else {
8251 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8252 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8253 		xar = NULL;
8254 
8255 		arp_flags = ar->arp_flags;
8256 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8257 		/*
8258 		 * Theoretically, the sa_family could tell us what link
8259 		 * layer type this operation is trying to deal with. By
8260 		 * common usage AF_UNSPEC means ethernet. We'll assume
8261 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8262 		 * for now. Our new SIOC*XARP ioctls can be used more
8263 		 * generally.
8264 		 *
8265 		 * If the underlying media happens to have a non 6 byte
8266 		 * address, arp module will fail set/get, but the del
8267 		 * operation will succeed.
8268 		 */
8269 		alength = 6;
8270 		if ((ipip->ipi_cmd != SIOCDARP) &&
8271 		    (alength != ill->ill_phys_addr_length)) {
8272 			return (EINVAL);
8273 		}
8274 	}
8275 
8276 	/* Translate ATF* flags to NCE* flags */
8277 	flags = 0;
8278 	if (arp_flags & ATF_AUTHORITY)
8279 		flags |= NCE_F_AUTHORITY;
8280 	if (arp_flags & ATF_PERM)
8281 		flags |= NCE_F_NONUD; /* not subject to aging */
8282 	if (arp_flags & ATF_PUBL)
8283 		flags |= NCE_F_PUBLISH;
8284 
8285 	/*
8286 	 * IPMP ARP special handling:
8287 	 *
8288 	 * 1. Since ARP mappings must appear consistent across the group,
8289 	 *    prohibit changing ARP mappings on the underlying interfaces.
8290 	 *
8291 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8292 	 *    IP itself, prohibit changing them.
8293 	 *
8294 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8295 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8296 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8297 	 */
8298 	if (IS_UNDER_IPMP(ill)) {
8299 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8300 			return (EPERM);
8301 	}
8302 	if (IS_IPMP(ill)) {
8303 		ipmp_illgrp_t *illg = ill->ill_grp;
8304 
8305 		switch (ipip->ipi_cmd) {
8306 		case SIOCSARP:
8307 		case SIOCSXARP:
8308 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8309 			if (proxy_ill != NULL) {
8310 				proxyarp = B_TRUE;
8311 				if (!ipmp_ill_is_active(proxy_ill))
8312 					proxy_ill = ipmp_illgrp_next_ill(illg);
8313 				if (proxy_ill != NULL)
8314 					lladdr = proxy_ill->ill_phys_addr;
8315 			}
8316 			/* FALLTHRU */
8317 		}
8318 	}
8319 
8320 	ipaddr = sin->sin_addr.s_addr;
8321 	/*
8322 	 * don't match across illgrp per case (1) and (2).
8323 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8324 	 */
8325 	nce = nce_lookup_v4(ill, &ipaddr);
8326 	if (nce != NULL)
8327 		ncec = nce->nce_common;
8328 
8329 	switch (iocp->ioc_cmd) {
8330 	case SIOCDARP:
8331 	case SIOCDXARP: {
8332 		/*
8333 		 * Delete the NCE if any.
8334 		 */
8335 		if (ncec == NULL) {
8336 			iocp->ioc_error = ENXIO;
8337 			break;
8338 		}
8339 		/* Don't allow changes to arp mappings of local addresses. */
8340 		if (NCE_MYADDR(ncec)) {
8341 			nce_refrele(nce);
8342 			return (ENOTSUP);
8343 		}
8344 		iocp->ioc_error = 0;
8345 
8346 		/*
8347 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8348 		 * This will delete all the nce entries on the under_ills.
8349 		 */
8350 		ncec_delete(ncec);
8351 		/*
8352 		 * Once the NCE has been deleted, then the ire_dep* consistency
8353 		 * mechanism will find any IRE which depended on the now
8354 		 * condemned NCE (as part of sending packets).
8355 		 * That mechanism handles redirects by deleting redirects
8356 		 * that refer to UNREACHABLE nces.
8357 		 */
8358 		break;
8359 	}
8360 	case SIOCGARP:
8361 	case SIOCGXARP:
8362 		if (ncec != NULL) {
8363 			lladdr = ncec->ncec_lladdr;
8364 			flags = ncec->ncec_flags;
8365 			iocp->ioc_error = 0;
8366 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8367 		} else {
8368 			iocp->ioc_error = ENXIO;
8369 		}
8370 		break;
8371 	case SIOCSARP:
8372 	case SIOCSXARP:
8373 		/* Don't allow changes to arp mappings of local addresses. */
8374 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8375 			nce_refrele(nce);
8376 			return (ENOTSUP);
8377 		}
8378 
8379 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8380 		flags |= NCE_F_STATIC;
8381 		if (!if_arp_ioctl) {
8382 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8383 			    lladdr, alength, flags);
8384 		} else {
8385 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8386 			if (ipif != NULL) {
8387 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8388 				    lladdr, alength, flags);
8389 				ipif_refrele(ipif);
8390 			}
8391 		}
8392 		if (nce != NULL) {
8393 			nce_refrele(nce);
8394 			nce = NULL;
8395 		}
8396 		/*
8397 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8398 		 * by nce_add_common()
8399 		 */
8400 		err = nce_lookup_then_add_v4(ill, lladdr,
8401 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8402 		    &nce);
8403 		if (err == EEXIST) {
8404 			ncec = nce->nce_common;
8405 			mutex_enter(&ncec->ncec_lock);
8406 			ncec->ncec_state = ND_REACHABLE;
8407 			ncec->ncec_flags = flags;
8408 			nce_update(ncec, ND_UNCHANGED, lladdr);
8409 			mutex_exit(&ncec->ncec_lock);
8410 			err = 0;
8411 		}
8412 		if (nce != NULL) {
8413 			nce_refrele(nce);
8414 			nce = NULL;
8415 		}
8416 		if (IS_IPMP(ill) && err == 0) {
8417 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8418 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8419 			    flags);
8420 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8421 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8422 				break;
8423 			}
8424 		}
8425 		iocp->ioc_error = err;
8426 	}
8427 
8428 	if (nce != NULL) {
8429 		nce_refrele(nce);
8430 	}
8431 
8432 	/*
8433 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8434 	 */
8435 	if (entp != NULL)
8436 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8437 
8438 	return (iocp->ioc_error);
8439 }
8440 
8441 /*
8442  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8443  * the associated sin and refhold and return the associated ipif via `ci'.
8444  */
8445 int
8446 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8447     cmd_info_t *ci)
8448 {
8449 	mblk_t	*mp1;
8450 	sin_t	*sin;
8451 	conn_t	*connp;
8452 	ipif_t	*ipif;
8453 	ire_t	*ire = NULL;
8454 	ill_t	*ill = NULL;
8455 	boolean_t exists;
8456 	ip_stack_t *ipst;
8457 	struct arpreq *ar;
8458 	struct xarpreq *xar;
8459 	struct sockaddr_dl *sdl;
8460 
8461 	/* ioctl comes down on a conn */
8462 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8463 	connp = Q_TO_CONN(q);
8464 	if (connp->conn_family == AF_INET6)
8465 		return (ENXIO);
8466 
8467 	ipst = connp->conn_netstack->netstack_ip;
8468 
8469 	/* Verified in ip_wput_nondata */
8470 	mp1 = mp->b_cont->b_cont;
8471 
8472 	if (ipip->ipi_cmd_type == XARP_CMD) {
8473 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8474 		xar = (struct xarpreq *)mp1->b_rptr;
8475 		sin = (sin_t *)&xar->xarp_pa;
8476 		sdl = &xar->xarp_ha;
8477 
8478 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8479 			return (ENXIO);
8480 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8481 			return (EINVAL);
8482 	} else {
8483 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8484 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8485 		ar = (struct arpreq *)mp1->b_rptr;
8486 		sin = (sin_t *)&ar->arp_pa;
8487 	}
8488 
8489 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8490 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8491 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8492 		if (ipif == NULL)
8493 			return (ENXIO);
8494 		if (ipif->ipif_id != 0) {
8495 			ipif_refrele(ipif);
8496 			return (ENXIO);
8497 		}
8498 	} else {
8499 		/*
8500 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8501 		 * of 0: use the IP address to find the ipif.  If the IP
8502 		 * address is an IPMP test address, ire_ftable_lookup() will
8503 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8504 		 */
8505 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8506 		    ipst);
8507 		if (ipif == NULL) {
8508 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8509 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8510 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8511 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8512 				if (ire != NULL)
8513 					ire_refrele(ire);
8514 				return (ENXIO);
8515 			}
8516 			ASSERT(ire != NULL && ill != NULL);
8517 			ipif = ill->ill_ipif;
8518 			ipif_refhold(ipif);
8519 			ire_refrele(ire);
8520 		}
8521 	}
8522 
8523 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8524 		ipif_refrele(ipif);
8525 		return (ENXIO);
8526 	}
8527 
8528 	ci->ci_sin = sin;
8529 	ci->ci_ipif = ipif;
8530 	return (0);
8531 }
8532 
8533 /*
8534  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8535  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8536  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8537  * up and thus an ill can join that illgrp.
8538  *
8539  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8540  * open()/close() primarily because close() is not allowed to fail or block
8541  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8542  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8543  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8544  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8545  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8546  * state if I_UNLINK didn't occur.
8547  *
8548  * Note that for each plumb/unplumb operation, we may end up here more than
8549  * once because of the way ifconfig works.  However, it's OK to link the same
8550  * illgrp more than once, or unlink an illgrp that's already unlinked.
8551  */
8552 static int
8553 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8554 {
8555 	int err;
8556 	ip_stack_t *ipst = ill->ill_ipst;
8557 
8558 	ASSERT(IS_IPMP(ill));
8559 	ASSERT(IAM_WRITER_ILL(ill));
8560 
8561 	switch (ioccmd) {
8562 	case I_LINK:
8563 		return (ENOTSUP);
8564 
8565 	case I_PLINK:
8566 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8567 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8568 		rw_exit(&ipst->ips_ipmp_lock);
8569 		break;
8570 
8571 	case I_PUNLINK:
8572 		/*
8573 		 * Require all UP ipifs be brought down prior to unlinking the
8574 		 * illgrp so any associated IREs (and other state) is torched.
8575 		 */
8576 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8577 			return (EBUSY);
8578 
8579 		/*
8580 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8581 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8582 		 * join this group.  Specifically: ills trying to join grab
8583 		 * ipmp_lock and bump a "pending join" counter checked by
8584 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8585 		 * joins can occur (since we have ipmp_lock).  Once we drop
8586 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8587 		 * find the illgrp (since we unlinked it) and will return
8588 		 * EAFNOSUPPORT.  This will then take them back through the
8589 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8590 		 * back through I_PLINK above.
8591 		 */
8592 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8593 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8594 		rw_exit(&ipst->ips_ipmp_lock);
8595 		return (err);
8596 	default:
8597 		break;
8598 	}
8599 	return (0);
8600 }
8601 
8602 /*
8603  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8604  * atomically set/clear the muxids. Also complete the ioctl by acking or
8605  * naking it.  Note that the code is structured such that the link type,
8606  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8607  * its clones use the persistent link, while pppd(1M) and perhaps many
8608  * other daemons may use non-persistent link.  When combined with some
8609  * ill_t states, linking and unlinking lower streams may be used as
8610  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8611  */
8612 /* ARGSUSED */
8613 void
8614 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8615 {
8616 	mblk_t		*mp1;
8617 	struct linkblk	*li;
8618 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8619 	int		err = 0;
8620 
8621 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8622 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8623 
8624 	mp1 = mp->b_cont;	/* This is the linkblk info */
8625 	li = (struct linkblk *)mp1->b_rptr;
8626 
8627 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8628 	if (err == EINPROGRESS)
8629 		return;
8630 done:
8631 	if (err == 0)
8632 		miocack(q, mp, 0, 0);
8633 	else
8634 		miocnak(q, mp, 0, err);
8635 
8636 	/* Conn was refheld in ip_sioctl_copyin_setup */
8637 	if (CONN_Q(q))
8638 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8639 }
8640 
8641 /*
8642  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8643  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8644  * module stream).  If `doconsist' is set, then do the extended consistency
8645  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8646  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8647  * an error code on failure.
8648  */
8649 static int
8650 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8651     struct linkblk *li)
8652 {
8653 	int		err = 0;
8654 	ill_t  		*ill;
8655 	queue_t		*ipwq, *dwq;
8656 	const char	*name;
8657 	struct qinit	*qinfo;
8658 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8659 	boolean_t	entered_ipsq = B_FALSE;
8660 	boolean_t	is_ip = B_FALSE;
8661 	arl_t		*arl;
8662 
8663 	/*
8664 	 * Walk the lower stream to verify it's the IP module stream.
8665 	 * The IP module is identified by its name, wput function,
8666 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8667 	 * (li->l_qbot) will not vanish until this ioctl completes.
8668 	 */
8669 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8670 		qinfo = ipwq->q_qinfo;
8671 		name = qinfo->qi_minfo->mi_idname;
8672 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8673 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8674 			is_ip = B_TRUE;
8675 			break;
8676 		}
8677 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8678 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8679 			break;
8680 		}
8681 	}
8682 
8683 	/*
8684 	 * If this isn't an IP module stream, bail.
8685 	 */
8686 	if (ipwq == NULL)
8687 		return (0);
8688 
8689 	if (!is_ip) {
8690 		arl = (arl_t *)ipwq->q_ptr;
8691 		ill = arl_to_ill(arl);
8692 		if (ill == NULL)
8693 			return (0);
8694 	} else {
8695 		ill = ipwq->q_ptr;
8696 	}
8697 	ASSERT(ill != NULL);
8698 
8699 	if (ipsq == NULL) {
8700 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8701 		    NEW_OP, B_FALSE);
8702 		if (ipsq == NULL) {
8703 			if (!is_ip)
8704 				ill_refrele(ill);
8705 			return (EINPROGRESS);
8706 		}
8707 		entered_ipsq = B_TRUE;
8708 	}
8709 	ASSERT(IAM_WRITER_ILL(ill));
8710 	mutex_enter(&ill->ill_lock);
8711 	if (!is_ip) {
8712 		if (islink && ill->ill_muxid == 0) {
8713 			/*
8714 			 * Plumbing has to be done with IP plumbed first, arp
8715 			 * second, but here we have arp being plumbed first.
8716 			 */
8717 			mutex_exit(&ill->ill_lock);
8718 			ipsq_exit(ipsq);
8719 			ill_refrele(ill);
8720 			return (EINVAL);
8721 		}
8722 	}
8723 	mutex_exit(&ill->ill_lock);
8724 	if (!is_ip) {
8725 		arl->arl_muxid = islink ? li->l_index : 0;
8726 		ill_refrele(ill);
8727 		goto done;
8728 	}
8729 
8730 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8731 		goto done;
8732 
8733 	/*
8734 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8735 	 * the read queue of the module immediately below IP in the ill.
8736 	 * These are used during the capability negotiation below.
8737 	 */
8738 	ill->ill_lmod_rq = NULL;
8739 	ill->ill_lmod_cnt = 0;
8740 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8741 		ill->ill_lmod_rq = RD(dwq);
8742 		for (; dwq != NULL; dwq = dwq->q_next)
8743 			ill->ill_lmod_cnt++;
8744 	}
8745 
8746 	ill->ill_muxid = islink ? li->l_index : 0;
8747 
8748 	/*
8749 	 * Mark the ipsq busy until the capability operations initiated below
8750 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8751 	 * returns, but the capability operation may complete asynchronously
8752 	 * much later.
8753 	 */
8754 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8755 	/*
8756 	 * If there's at least one up ipif on this ill, then we're bound to
8757 	 * the underlying driver via DLPI.  In that case, renegotiate
8758 	 * capabilities to account for any possible change in modules
8759 	 * interposed between IP and the driver.
8760 	 */
8761 	if (ill->ill_ipif_up_count > 0) {
8762 		if (islink)
8763 			ill_capability_probe(ill);
8764 		else
8765 			ill_capability_reset(ill, B_FALSE);
8766 	}
8767 	ipsq_current_finish(ipsq);
8768 done:
8769 	if (entered_ipsq)
8770 		ipsq_exit(ipsq);
8771 
8772 	return (err);
8773 }
8774 
8775 /*
8776  * Search the ioctl command in the ioctl tables and return a pointer
8777  * to the ioctl command information. The ioctl command tables are
8778  * static and fully populated at compile time.
8779  */
8780 ip_ioctl_cmd_t *
8781 ip_sioctl_lookup(int ioc_cmd)
8782 {
8783 	int index;
8784 	ip_ioctl_cmd_t *ipip;
8785 	ip_ioctl_cmd_t *ipip_end;
8786 
8787 	if (ioc_cmd == IPI_DONTCARE)
8788 		return (NULL);
8789 
8790 	/*
8791 	 * Do a 2 step search. First search the indexed table
8792 	 * based on the least significant byte of the ioctl cmd.
8793 	 * If we don't find a match, then search the misc table
8794 	 * serially.
8795 	 */
8796 	index = ioc_cmd & 0xFF;
8797 	if (index < ip_ndx_ioctl_count) {
8798 		ipip = &ip_ndx_ioctl_table[index];
8799 		if (ipip->ipi_cmd == ioc_cmd) {
8800 			/* Found a match in the ndx table */
8801 			return (ipip);
8802 		}
8803 	}
8804 
8805 	/* Search the misc table */
8806 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8807 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8808 		if (ipip->ipi_cmd == ioc_cmd)
8809 			/* Found a match in the misc table */
8810 			return (ipip);
8811 	}
8812 
8813 	return (NULL);
8814 }
8815 
8816 /*
8817  * Wrapper function for resuming deferred ioctl processing
8818  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8819  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8820  */
8821 /* ARGSUSED */
8822 void
8823 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8824     void *dummy_arg)
8825 {
8826 	ip_sioctl_copyin_setup(q, mp);
8827 }
8828 
8829 /*
8830  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8831  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8832  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8833  * We establish here the size of the block to be copied in.  mi_copyin
8834  * arranges for this to happen, an processing continues in ip_wput_nondata with
8835  * an M_IOCDATA message.
8836  */
8837 void
8838 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8839 {
8840 	int	copyin_size;
8841 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8842 	ip_ioctl_cmd_t *ipip;
8843 	cred_t *cr;
8844 	ip_stack_t	*ipst;
8845 
8846 	if (CONN_Q(q))
8847 		ipst = CONNQ_TO_IPST(q);
8848 	else
8849 		ipst = ILLQ_TO_IPST(q);
8850 
8851 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8852 	if (ipip == NULL) {
8853 		/*
8854 		 * The ioctl is not one we understand or own.
8855 		 * Pass it along to be processed down stream,
8856 		 * if this is a module instance of IP, else nak
8857 		 * the ioctl.
8858 		 */
8859 		if (q->q_next == NULL) {
8860 			goto nak;
8861 		} else {
8862 			putnext(q, mp);
8863 			return;
8864 		}
8865 	}
8866 
8867 	/*
8868 	 * If this is deferred, then we will do all the checks when we
8869 	 * come back.
8870 	 */
8871 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8872 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8873 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8874 		return;
8875 	}
8876 
8877 	/*
8878 	 * Only allow a very small subset of IP ioctls on this stream if
8879 	 * IP is a module and not a driver. Allowing ioctls to be processed
8880 	 * in this case may cause assert failures or data corruption.
8881 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8882 	 * ioctls allowed on an IP module stream, after which this stream
8883 	 * normally becomes a multiplexor (at which time the stream head
8884 	 * will fail all ioctls).
8885 	 */
8886 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8887 		goto nak;
8888 	}
8889 
8890 	/* Make sure we have ioctl data to process. */
8891 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8892 		goto nak;
8893 
8894 	/*
8895 	 * Prefer dblk credential over ioctl credential; some synthesized
8896 	 * ioctls have kcred set because there's no way to crhold()
8897 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8898 	 * the framework; the caller of ioctl needs to hold the reference
8899 	 * for the duration of the call).
8900 	 */
8901 	cr = msg_getcred(mp, NULL);
8902 	if (cr == NULL)
8903 		cr = iocp->ioc_cr;
8904 
8905 	/* Make sure normal users don't send down privileged ioctls */
8906 	if ((ipip->ipi_flags & IPI_PRIV) &&
8907 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8908 		/* We checked the privilege earlier but log it here */
8909 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8910 		return;
8911 	}
8912 
8913 	/*
8914 	 * The ioctl command tables can only encode fixed length
8915 	 * ioctl data. If the length is variable, the table will
8916 	 * encode the length as zero. Such special cases are handled
8917 	 * below in the switch.
8918 	 */
8919 	if (ipip->ipi_copyin_size != 0) {
8920 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8921 		return;
8922 	}
8923 
8924 	switch (iocp->ioc_cmd) {
8925 	case O_SIOCGIFCONF:
8926 	case SIOCGIFCONF:
8927 		/*
8928 		 * This IOCTL is hilarious.  See comments in
8929 		 * ip_sioctl_get_ifconf for the story.
8930 		 */
8931 		if (iocp->ioc_count == TRANSPARENT)
8932 			copyin_size = SIZEOF_STRUCT(ifconf,
8933 			    iocp->ioc_flag);
8934 		else
8935 			copyin_size = iocp->ioc_count;
8936 		mi_copyin(q, mp, NULL, copyin_size);
8937 		return;
8938 
8939 	case O_SIOCGLIFCONF:
8940 	case SIOCGLIFCONF:
8941 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8942 		mi_copyin(q, mp, NULL, copyin_size);
8943 		return;
8944 
8945 	case SIOCGLIFSRCOF:
8946 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8947 		mi_copyin(q, mp, NULL, copyin_size);
8948 		return;
8949 	case SIOCGIP6ADDRPOLICY:
8950 		ip_sioctl_ip6addrpolicy(q, mp);
8951 		ip6_asp_table_refrele(ipst);
8952 		return;
8953 
8954 	case SIOCSIP6ADDRPOLICY:
8955 		ip_sioctl_ip6addrpolicy(q, mp);
8956 		return;
8957 
8958 	case SIOCGDSTINFO:
8959 		ip_sioctl_dstinfo(q, mp);
8960 		ip6_asp_table_refrele(ipst);
8961 		return;
8962 
8963 	case I_PLINK:
8964 	case I_PUNLINK:
8965 	case I_LINK:
8966 	case I_UNLINK:
8967 		/*
8968 		 * We treat non-persistent link similarly as the persistent
8969 		 * link case, in terms of plumbing/unplumbing, as well as
8970 		 * dynamic re-plumbing events indicator.  See comments
8971 		 * in ip_sioctl_plink() for more.
8972 		 *
8973 		 * Request can be enqueued in the 'ipsq' while waiting
8974 		 * to become exclusive. So bump up the conn ref.
8975 		 */
8976 		if (CONN_Q(q))
8977 			CONN_INC_REF(Q_TO_CONN(q));
8978 		ip_sioctl_plink(NULL, q, mp, NULL);
8979 		return;
8980 
8981 	case ND_GET:
8982 	case ND_SET:
8983 		/*
8984 		 * Use of the nd table requires holding the reader lock.
8985 		 * Modifying the nd table thru nd_load/nd_unload requires
8986 		 * the writer lock.
8987 		 */
8988 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8989 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8990 			rw_exit(&ipst->ips_ip_g_nd_lock);
8991 
8992 			if (iocp->ioc_error)
8993 				iocp->ioc_count = 0;
8994 			mp->b_datap->db_type = M_IOCACK;
8995 			qreply(q, mp);
8996 			return;
8997 		}
8998 		rw_exit(&ipst->ips_ip_g_nd_lock);
8999 		/*
9000 		 * We don't understand this subioctl of ND_GET / ND_SET.
9001 		 * Maybe intended for some driver / module below us
9002 		 */
9003 		if (q->q_next) {
9004 			putnext(q, mp);
9005 		} else {
9006 			iocp->ioc_error = ENOENT;
9007 			mp->b_datap->db_type = M_IOCNAK;
9008 			iocp->ioc_count = 0;
9009 			qreply(q, mp);
9010 		}
9011 		return;
9012 
9013 	case IP_IOCTL:
9014 		ip_wput_ioctl(q, mp);
9015 		return;
9016 
9017 	case SIOCILB:
9018 		/* The ioctl length varies depending on the ILB command. */
9019 		copyin_size = iocp->ioc_count;
9020 		if (copyin_size < sizeof (ilb_cmd_t))
9021 			goto nak;
9022 		mi_copyin(q, mp, NULL, copyin_size);
9023 		return;
9024 
9025 	default:
9026 		cmn_err(CE_PANIC, "should not happen ");
9027 	}
9028 nak:
9029 	if (mp->b_cont != NULL) {
9030 		freemsg(mp->b_cont);
9031 		mp->b_cont = NULL;
9032 	}
9033 	iocp->ioc_error = EINVAL;
9034 	mp->b_datap->db_type = M_IOCNAK;
9035 	iocp->ioc_count = 0;
9036 	qreply(q, mp);
9037 }
9038 
9039 static void
9040 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9041 {
9042 	struct arpreq *ar;
9043 	struct xarpreq *xar;
9044 	mblk_t	*tmp;
9045 	struct iocblk *iocp;
9046 	int x_arp_ioctl = B_FALSE;
9047 	int *flagsp;
9048 	char *storage = NULL;
9049 
9050 	ASSERT(ill != NULL);
9051 
9052 	iocp = (struct iocblk *)mp->b_rptr;
9053 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9054 
9055 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9056 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9057 	    (iocp->ioc_cmd == SIOCSXARP)) {
9058 		x_arp_ioctl = B_TRUE;
9059 		xar = (struct xarpreq *)tmp->b_rptr;
9060 		flagsp = &xar->xarp_flags;
9061 		storage = xar->xarp_ha.sdl_data;
9062 	} else {
9063 		ar = (struct arpreq *)tmp->b_rptr;
9064 		flagsp = &ar->arp_flags;
9065 		storage = ar->arp_ha.sa_data;
9066 	}
9067 
9068 	/*
9069 	 * We're done if this is not an SIOCG{X}ARP
9070 	 */
9071 	if (x_arp_ioctl) {
9072 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9073 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9074 		    sizeof (xar->xarp_ha.sdl_data)) {
9075 			iocp->ioc_error = EINVAL;
9076 			return;
9077 		}
9078 	}
9079 	*flagsp = ATF_INUSE;
9080 	/*
9081 	 * If /sbin/arp told us we are the authority using the "permanent"
9082 	 * flag, or if this is one of my addresses print "permanent"
9083 	 * in the /sbin/arp output.
9084 	 */
9085 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9086 		*flagsp |= ATF_AUTHORITY;
9087 	if (flags & NCE_F_NONUD)
9088 		*flagsp |= ATF_PERM; /* not subject to aging */
9089 	if (flags & NCE_F_PUBLISH)
9090 		*flagsp |= ATF_PUBL;
9091 	if (hwaddr != NULL) {
9092 		*flagsp |= ATF_COM;
9093 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9094 	}
9095 }
9096 
9097 /*
9098  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9099  * interface) create the next available logical interface for this
9100  * physical interface.
9101  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9102  * ipif with the specified name.
9103  *
9104  * If the address family is not AF_UNSPEC then set the address as well.
9105  *
9106  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9107  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9108  *
9109  * Executed as a writer on the ill.
9110  * So no lock is needed to traverse the ipif chain, or examine the
9111  * phyint flags.
9112  */
9113 /* ARGSUSED */
9114 int
9115 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9116     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9117 {
9118 	mblk_t	*mp1;
9119 	struct lifreq *lifr;
9120 	boolean_t	isv6;
9121 	boolean_t	exists;
9122 	char 	*name;
9123 	char	*endp;
9124 	char	*cp;
9125 	int	namelen;
9126 	ipif_t	*ipif;
9127 	long	id;
9128 	ipsq_t	*ipsq;
9129 	ill_t	*ill;
9130 	sin_t	*sin;
9131 	int	err = 0;
9132 	boolean_t found_sep = B_FALSE;
9133 	conn_t	*connp;
9134 	zoneid_t zoneid;
9135 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9136 
9137 	ASSERT(q->q_next == NULL);
9138 	ip1dbg(("ip_sioctl_addif\n"));
9139 	/* Existence of mp1 has been checked in ip_wput_nondata */
9140 	mp1 = mp->b_cont->b_cont;
9141 	/*
9142 	 * Null terminate the string to protect against buffer
9143 	 * overrun. String was generated by user code and may not
9144 	 * be trusted.
9145 	 */
9146 	lifr = (struct lifreq *)mp1->b_rptr;
9147 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9148 	name = lifr->lifr_name;
9149 	ASSERT(CONN_Q(q));
9150 	connp = Q_TO_CONN(q);
9151 	isv6 = (connp->conn_family == AF_INET6);
9152 	zoneid = connp->conn_zoneid;
9153 	namelen = mi_strlen(name);
9154 	if (namelen == 0)
9155 		return (EINVAL);
9156 
9157 	exists = B_FALSE;
9158 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9159 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9160 		/*
9161 		 * Allow creating lo0 using SIOCLIFADDIF.
9162 		 * can't be any other writer thread. So can pass null below
9163 		 * for the last 4 args to ipif_lookup_name.
9164 		 */
9165 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9166 		    &exists, isv6, zoneid, ipst);
9167 		/* Prevent any further action */
9168 		if (ipif == NULL) {
9169 			return (ENOBUFS);
9170 		} else if (!exists) {
9171 			/* We created the ipif now and as writer */
9172 			ipif_refrele(ipif);
9173 			return (0);
9174 		} else {
9175 			ill = ipif->ipif_ill;
9176 			ill_refhold(ill);
9177 			ipif_refrele(ipif);
9178 		}
9179 	} else {
9180 		/* Look for a colon in the name. */
9181 		endp = &name[namelen];
9182 		for (cp = endp; --cp > name; ) {
9183 			if (*cp == IPIF_SEPARATOR_CHAR) {
9184 				found_sep = B_TRUE;
9185 				/*
9186 				 * Reject any non-decimal aliases for plumbing
9187 				 * of logical interfaces. Aliases with leading
9188 				 * zeroes are also rejected as they introduce
9189 				 * ambiguity in the naming of the interfaces.
9190 				 * Comparing with "0" takes care of all such
9191 				 * cases.
9192 				 */
9193 				if ((strncmp("0", cp+1, 1)) == 0)
9194 					return (EINVAL);
9195 
9196 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9197 				    id <= 0 || *endp != '\0') {
9198 					return (EINVAL);
9199 				}
9200 				*cp = '\0';
9201 				break;
9202 			}
9203 		}
9204 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9205 		if (found_sep)
9206 			*cp = IPIF_SEPARATOR_CHAR;
9207 		if (ill == NULL)
9208 			return (ENXIO);
9209 	}
9210 
9211 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9212 	    B_TRUE);
9213 
9214 	/*
9215 	 * Release the refhold due to the lookup, now that we are excl
9216 	 * or we are just returning
9217 	 */
9218 	ill_refrele(ill);
9219 
9220 	if (ipsq == NULL)
9221 		return (EINPROGRESS);
9222 
9223 	/* We are now exclusive on the IPSQ */
9224 	ASSERT(IAM_WRITER_ILL(ill));
9225 
9226 	if (found_sep) {
9227 		/* Now see if there is an IPIF with this unit number. */
9228 		for (ipif = ill->ill_ipif; ipif != NULL;
9229 		    ipif = ipif->ipif_next) {
9230 			if (ipif->ipif_id == id) {
9231 				err = EEXIST;
9232 				goto done;
9233 			}
9234 		}
9235 	}
9236 
9237 	/*
9238 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9239 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9240 	 * instead.
9241 	 */
9242 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9243 	    B_TRUE, B_TRUE, &err)) == NULL) {
9244 		goto done;
9245 	}
9246 
9247 	/* Return created name with ioctl */
9248 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9249 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9250 	ip1dbg(("created %s\n", lifr->lifr_name));
9251 
9252 	/* Set address */
9253 	sin = (sin_t *)&lifr->lifr_addr;
9254 	if (sin->sin_family != AF_UNSPEC) {
9255 		err = ip_sioctl_addr(ipif, sin, q, mp,
9256 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9257 	}
9258 
9259 done:
9260 	ipsq_exit(ipsq);
9261 	return (err);
9262 }
9263 
9264 /*
9265  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9266  * interface) delete it based on the IP address (on this physical interface).
9267  * Otherwise delete it based on the ipif_id.
9268  * Also, special handling to allow a removeif of lo0.
9269  */
9270 /* ARGSUSED */
9271 int
9272 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9273     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9274 {
9275 	conn_t		*connp;
9276 	ill_t		*ill = ipif->ipif_ill;
9277 	boolean_t	 success;
9278 	ip_stack_t	*ipst;
9279 
9280 	ipst = CONNQ_TO_IPST(q);
9281 
9282 	ASSERT(q->q_next == NULL);
9283 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9284 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9285 	ASSERT(IAM_WRITER_IPIF(ipif));
9286 
9287 	connp = Q_TO_CONN(q);
9288 	/*
9289 	 * Special case for unplumbing lo0 (the loopback physical interface).
9290 	 * If unplumbing lo0, the incoming address structure has been
9291 	 * initialized to all zeros. When unplumbing lo0, all its logical
9292 	 * interfaces must be removed too.
9293 	 *
9294 	 * Note that this interface may be called to remove a specific
9295 	 * loopback logical interface (eg, lo0:1). But in that case
9296 	 * ipif->ipif_id != 0 so that the code path for that case is the
9297 	 * same as any other interface (meaning it skips the code directly
9298 	 * below).
9299 	 */
9300 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9301 		if (sin->sin_family == AF_UNSPEC &&
9302 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9303 			/*
9304 			 * Mark it condemned. No new ref. will be made to ill.
9305 			 */
9306 			mutex_enter(&ill->ill_lock);
9307 			ill->ill_state_flags |= ILL_CONDEMNED;
9308 			for (ipif = ill->ill_ipif; ipif != NULL;
9309 			    ipif = ipif->ipif_next) {
9310 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9311 			}
9312 			mutex_exit(&ill->ill_lock);
9313 
9314 			ipif = ill->ill_ipif;
9315 			/* unplumb the loopback interface */
9316 			ill_delete(ill);
9317 			mutex_enter(&connp->conn_lock);
9318 			mutex_enter(&ill->ill_lock);
9319 
9320 			/* Are any references to this ill active */
9321 			if (ill_is_freeable(ill)) {
9322 				mutex_exit(&ill->ill_lock);
9323 				mutex_exit(&connp->conn_lock);
9324 				ill_delete_tail(ill);
9325 				mi_free(ill);
9326 				return (0);
9327 			}
9328 			success = ipsq_pending_mp_add(connp, ipif,
9329 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9330 			mutex_exit(&connp->conn_lock);
9331 			mutex_exit(&ill->ill_lock);
9332 			if (success)
9333 				return (EINPROGRESS);
9334 			else
9335 				return (EINTR);
9336 		}
9337 	}
9338 
9339 	if (ipif->ipif_id == 0) {
9340 		ipsq_t *ipsq;
9341 
9342 		/* Find based on address */
9343 		if (ipif->ipif_isv6) {
9344 			sin6_t *sin6;
9345 
9346 			if (sin->sin_family != AF_INET6)
9347 				return (EAFNOSUPPORT);
9348 
9349 			sin6 = (sin6_t *)sin;
9350 			/* We are a writer, so we should be able to lookup */
9351 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9352 			    ipst);
9353 		} else {
9354 			if (sin->sin_family != AF_INET)
9355 				return (EAFNOSUPPORT);
9356 
9357 			/* We are a writer, so we should be able to lookup */
9358 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9359 			    ipst);
9360 		}
9361 		if (ipif == NULL) {
9362 			return (EADDRNOTAVAIL);
9363 		}
9364 
9365 		/*
9366 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9367 		 * lifr_name of the physical interface but with an ip address
9368 		 * lifr_addr of a logical interface plumbed over it.
9369 		 * So update ipx_current_ipif now that ipif points to the
9370 		 * correct one.
9371 		 */
9372 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9373 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9374 
9375 		/* This is a writer */
9376 		ipif_refrele(ipif);
9377 	}
9378 
9379 	/*
9380 	 * Can not delete instance zero since it is tied to the ill.
9381 	 */
9382 	if (ipif->ipif_id == 0)
9383 		return (EBUSY);
9384 
9385 	mutex_enter(&ill->ill_lock);
9386 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9387 	mutex_exit(&ill->ill_lock);
9388 
9389 	ipif_free(ipif);
9390 
9391 	mutex_enter(&connp->conn_lock);
9392 	mutex_enter(&ill->ill_lock);
9393 
9394 	/* Are any references to this ipif active */
9395 	if (ipif_is_freeable(ipif)) {
9396 		mutex_exit(&ill->ill_lock);
9397 		mutex_exit(&connp->conn_lock);
9398 		ipif_non_duplicate(ipif);
9399 		(void) ipif_down_tail(ipif);
9400 		ipif_free_tail(ipif); /* frees ipif */
9401 		return (0);
9402 	}
9403 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9404 	    IPIF_FREE);
9405 	mutex_exit(&ill->ill_lock);
9406 	mutex_exit(&connp->conn_lock);
9407 	if (success)
9408 		return (EINPROGRESS);
9409 	else
9410 		return (EINTR);
9411 }
9412 
9413 /*
9414  * Restart the removeif ioctl. The refcnt has gone down to 0.
9415  * The ipif is already condemned. So can't find it thru lookups.
9416  */
9417 /* ARGSUSED */
9418 int
9419 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9420     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9421 {
9422 	ill_t *ill = ipif->ipif_ill;
9423 
9424 	ASSERT(IAM_WRITER_IPIF(ipif));
9425 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9426 
9427 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9428 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9429 
9430 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9431 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9432 		ill_delete_tail(ill);
9433 		mi_free(ill);
9434 		return (0);
9435 	}
9436 
9437 	ipif_non_duplicate(ipif);
9438 	(void) ipif_down_tail(ipif);
9439 	ipif_free_tail(ipif);
9440 
9441 	return (0);
9442 }
9443 
9444 /*
9445  * Set the local interface address.
9446  * Allow an address of all zero when the interface is down.
9447  */
9448 /* ARGSUSED */
9449 int
9450 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9451     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9452 {
9453 	int err = 0;
9454 	in6_addr_t v6addr;
9455 	boolean_t need_up = B_FALSE;
9456 
9457 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9458 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9459 
9460 	ASSERT(IAM_WRITER_IPIF(ipif));
9461 
9462 	if (ipif->ipif_isv6) {
9463 		sin6_t *sin6;
9464 		ill_t *ill;
9465 		phyint_t *phyi;
9466 
9467 		if (sin->sin_family != AF_INET6)
9468 			return (EAFNOSUPPORT);
9469 
9470 		sin6 = (sin6_t *)sin;
9471 		v6addr = sin6->sin6_addr;
9472 		ill = ipif->ipif_ill;
9473 		phyi = ill->ill_phyint;
9474 
9475 		/*
9476 		 * Enforce that true multicast interfaces have a link-local
9477 		 * address for logical unit 0.
9478 		 */
9479 		if (ipif->ipif_id == 0 &&
9480 		    (ill->ill_flags & ILLF_MULTICAST) &&
9481 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9482 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9483 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9484 			return (EADDRNOTAVAIL);
9485 		}
9486 
9487 		/*
9488 		 * up interfaces shouldn't have the unspecified address
9489 		 * unless they also have the IPIF_NOLOCAL flags set and
9490 		 * have a subnet assigned.
9491 		 */
9492 		if ((ipif->ipif_flags & IPIF_UP) &&
9493 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9494 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9495 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9496 			return (EADDRNOTAVAIL);
9497 		}
9498 
9499 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9500 			return (EADDRNOTAVAIL);
9501 	} else {
9502 		ipaddr_t addr;
9503 
9504 		if (sin->sin_family != AF_INET)
9505 			return (EAFNOSUPPORT);
9506 
9507 		addr = sin->sin_addr.s_addr;
9508 
9509 		/* Allow 0 as the local address. */
9510 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9511 			return (EADDRNOTAVAIL);
9512 
9513 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9514 	}
9515 
9516 	/*
9517 	 * Even if there is no change we redo things just to rerun
9518 	 * ipif_set_default.
9519 	 */
9520 	if (ipif->ipif_flags & IPIF_UP) {
9521 		/*
9522 		 * Setting a new local address, make sure
9523 		 * we have net and subnet bcast ire's for
9524 		 * the old address if we need them.
9525 		 */
9526 		/*
9527 		 * If the interface is already marked up,
9528 		 * we call ipif_down which will take care
9529 		 * of ditching any IREs that have been set
9530 		 * up based on the old interface address.
9531 		 */
9532 		err = ipif_logical_down(ipif, q, mp);
9533 		if (err == EINPROGRESS)
9534 			return (err);
9535 		(void) ipif_down_tail(ipif);
9536 		need_up = 1;
9537 	}
9538 
9539 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9540 	return (err);
9541 }
9542 
9543 int
9544 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9545     boolean_t need_up)
9546 {
9547 	in6_addr_t v6addr;
9548 	in6_addr_t ov6addr;
9549 	ipaddr_t addr;
9550 	sin6_t	*sin6;
9551 	int	sinlen;
9552 	int	err = 0;
9553 	ill_t	*ill = ipif->ipif_ill;
9554 	boolean_t need_dl_down;
9555 	boolean_t need_arp_down;
9556 	struct iocblk *iocp;
9557 
9558 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9559 
9560 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9561 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9562 	ASSERT(IAM_WRITER_IPIF(ipif));
9563 
9564 	/* Must cancel any pending timer before taking the ill_lock */
9565 	if (ipif->ipif_recovery_id != 0)
9566 		(void) untimeout(ipif->ipif_recovery_id);
9567 	ipif->ipif_recovery_id = 0;
9568 
9569 	if (ipif->ipif_isv6) {
9570 		sin6 = (sin6_t *)sin;
9571 		v6addr = sin6->sin6_addr;
9572 		sinlen = sizeof (struct sockaddr_in6);
9573 	} else {
9574 		addr = sin->sin_addr.s_addr;
9575 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9576 		sinlen = sizeof (struct sockaddr_in);
9577 	}
9578 	mutex_enter(&ill->ill_lock);
9579 	ov6addr = ipif->ipif_v6lcl_addr;
9580 	ipif->ipif_v6lcl_addr = v6addr;
9581 	sctp_update_ipif_addr(ipif, ov6addr);
9582 	ipif->ipif_addr_ready = 0;
9583 
9584 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9585 
9586 	/*
9587 	 * If the interface was previously marked as a duplicate, then since
9588 	 * we've now got a "new" address, it should no longer be considered a
9589 	 * duplicate -- even if the "new" address is the same as the old one.
9590 	 * Note that if all ipifs are down, we may have a pending ARP down
9591 	 * event to handle.  This is because we want to recover from duplicates
9592 	 * and thus delay tearing down ARP until the duplicates have been
9593 	 * removed or disabled.
9594 	 */
9595 	need_dl_down = need_arp_down = B_FALSE;
9596 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9597 		need_arp_down = !need_up;
9598 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9599 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9600 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9601 			need_dl_down = B_TRUE;
9602 		}
9603 	}
9604 
9605 	ipif_set_default(ipif);
9606 
9607 	/*
9608 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9609 	 * tag the ill so that future updates to the interface ID don't result
9610 	 * in this address getting automatically reconfigured from under the
9611 	 * administrator.
9612 	 */
9613 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9614 		ill->ill_manual_linklocal = 1;
9615 
9616 	/*
9617 	 * When publishing an interface address change event, we only notify
9618 	 * the event listeners of the new address.  It is assumed that if they
9619 	 * actively care about the addresses assigned that they will have
9620 	 * already discovered the previous address assigned (if there was one.)
9621 	 *
9622 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9623 	 */
9624 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9625 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9626 		    NE_ADDRESS_CHANGE, sin, sinlen);
9627 	}
9628 
9629 	mutex_exit(&ill->ill_lock);
9630 
9631 	if (need_up) {
9632 		/*
9633 		 * Now bring the interface back up.  If this
9634 		 * is the only IPIF for the ILL, ipif_up
9635 		 * will have to re-bind to the device, so
9636 		 * we may get back EINPROGRESS, in which
9637 		 * case, this IOCTL will get completed in
9638 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9639 		 */
9640 		err = ipif_up(ipif, q, mp);
9641 	} else {
9642 		/* Perhaps ilgs should use this ill */
9643 		update_conn_ill(NULL, ill->ill_ipst);
9644 	}
9645 
9646 	if (need_dl_down)
9647 		ill_dl_down(ill);
9648 
9649 	if (need_arp_down && !ill->ill_isv6)
9650 		(void) ipif_arp_down(ipif);
9651 
9652 	/*
9653 	 * The default multicast interface might have changed (for
9654 	 * instance if the IPv6 scope of the address changed)
9655 	 */
9656 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9657 
9658 	return (err);
9659 }
9660 
9661 /*
9662  * Restart entry point to restart the address set operation after the
9663  * refcounts have dropped to zero.
9664  */
9665 /* ARGSUSED */
9666 int
9667 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9668     ip_ioctl_cmd_t *ipip, void *ifreq)
9669 {
9670 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9671 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9672 	ASSERT(IAM_WRITER_IPIF(ipif));
9673 	(void) ipif_down_tail(ipif);
9674 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9675 }
9676 
9677 /* ARGSUSED */
9678 int
9679 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9680     ip_ioctl_cmd_t *ipip, void *if_req)
9681 {
9682 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9683 	struct lifreq *lifr = (struct lifreq *)if_req;
9684 
9685 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9686 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9687 	/*
9688 	 * The net mask and address can't change since we have a
9689 	 * reference to the ipif. So no lock is necessary.
9690 	 */
9691 	if (ipif->ipif_isv6) {
9692 		*sin6 = sin6_null;
9693 		sin6->sin6_family = AF_INET6;
9694 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9695 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9696 		lifr->lifr_addrlen =
9697 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9698 	} else {
9699 		*sin = sin_null;
9700 		sin->sin_family = AF_INET;
9701 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9702 		if (ipip->ipi_cmd_type == LIF_CMD) {
9703 			lifr->lifr_addrlen =
9704 			    ip_mask_to_plen(ipif->ipif_net_mask);
9705 		}
9706 	}
9707 	return (0);
9708 }
9709 
9710 /*
9711  * Set the destination address for a pt-pt interface.
9712  */
9713 /* ARGSUSED */
9714 int
9715 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9716     ip_ioctl_cmd_t *ipip, void *if_req)
9717 {
9718 	int err = 0;
9719 	in6_addr_t v6addr;
9720 	boolean_t need_up = B_FALSE;
9721 
9722 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9723 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9724 	ASSERT(IAM_WRITER_IPIF(ipif));
9725 
9726 	if (ipif->ipif_isv6) {
9727 		sin6_t *sin6;
9728 
9729 		if (sin->sin_family != AF_INET6)
9730 			return (EAFNOSUPPORT);
9731 
9732 		sin6 = (sin6_t *)sin;
9733 		v6addr = sin6->sin6_addr;
9734 
9735 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9736 			return (EADDRNOTAVAIL);
9737 	} else {
9738 		ipaddr_t addr;
9739 
9740 		if (sin->sin_family != AF_INET)
9741 			return (EAFNOSUPPORT);
9742 
9743 		addr = sin->sin_addr.s_addr;
9744 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9745 			return (EADDRNOTAVAIL);
9746 
9747 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9748 	}
9749 
9750 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9751 		return (0);	/* No change */
9752 
9753 	if (ipif->ipif_flags & IPIF_UP) {
9754 		/*
9755 		 * If the interface is already marked up,
9756 		 * we call ipif_down which will take care
9757 		 * of ditching any IREs that have been set
9758 		 * up based on the old pp dst address.
9759 		 */
9760 		err = ipif_logical_down(ipif, q, mp);
9761 		if (err == EINPROGRESS)
9762 			return (err);
9763 		(void) ipif_down_tail(ipif);
9764 		need_up = B_TRUE;
9765 	}
9766 	/*
9767 	 * could return EINPROGRESS. If so ioctl will complete in
9768 	 * ip_rput_dlpi_writer
9769 	 */
9770 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9771 	return (err);
9772 }
9773 
9774 static int
9775 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9776     boolean_t need_up)
9777 {
9778 	in6_addr_t v6addr;
9779 	ill_t	*ill = ipif->ipif_ill;
9780 	int	err = 0;
9781 	boolean_t need_dl_down;
9782 	boolean_t need_arp_down;
9783 
9784 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9785 	    ipif->ipif_id, (void *)ipif));
9786 
9787 	/* Must cancel any pending timer before taking the ill_lock */
9788 	if (ipif->ipif_recovery_id != 0)
9789 		(void) untimeout(ipif->ipif_recovery_id);
9790 	ipif->ipif_recovery_id = 0;
9791 
9792 	if (ipif->ipif_isv6) {
9793 		sin6_t *sin6;
9794 
9795 		sin6 = (sin6_t *)sin;
9796 		v6addr = sin6->sin6_addr;
9797 	} else {
9798 		ipaddr_t addr;
9799 
9800 		addr = sin->sin_addr.s_addr;
9801 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9802 	}
9803 	mutex_enter(&ill->ill_lock);
9804 	/* Set point to point destination address. */
9805 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9806 		/*
9807 		 * Allow this as a means of creating logical
9808 		 * pt-pt interfaces on top of e.g. an Ethernet.
9809 		 * XXX Undocumented HACK for testing.
9810 		 * pt-pt interfaces are created with NUD disabled.
9811 		 */
9812 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9813 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9814 		if (ipif->ipif_isv6)
9815 			ill->ill_flags |= ILLF_NONUD;
9816 	}
9817 
9818 	/*
9819 	 * If the interface was previously marked as a duplicate, then since
9820 	 * we've now got a "new" address, it should no longer be considered a
9821 	 * duplicate -- even if the "new" address is the same as the old one.
9822 	 * Note that if all ipifs are down, we may have a pending ARP down
9823 	 * event to handle.
9824 	 */
9825 	need_dl_down = need_arp_down = B_FALSE;
9826 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9827 		need_arp_down = !need_up;
9828 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9829 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9830 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9831 			need_dl_down = B_TRUE;
9832 		}
9833 	}
9834 
9835 	/*
9836 	 * If we've just manually set the IPv6 destination link-local address
9837 	 * (0th ipif), tag the ill so that future updates to the destination
9838 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9839 	 * result in this address getting automatically reconfigured from
9840 	 * under the administrator.
9841 	 */
9842 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9843 		ill->ill_manual_dst_linklocal = 1;
9844 
9845 	/* Set the new address. */
9846 	ipif->ipif_v6pp_dst_addr = v6addr;
9847 	/* Make sure subnet tracks pp_dst */
9848 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9849 	mutex_exit(&ill->ill_lock);
9850 
9851 	if (need_up) {
9852 		/*
9853 		 * Now bring the interface back up.  If this
9854 		 * is the only IPIF for the ILL, ipif_up
9855 		 * will have to re-bind to the device, so
9856 		 * we may get back EINPROGRESS, in which
9857 		 * case, this IOCTL will get completed in
9858 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9859 		 */
9860 		err = ipif_up(ipif, q, mp);
9861 	}
9862 
9863 	if (need_dl_down)
9864 		ill_dl_down(ill);
9865 	if (need_arp_down && !ipif->ipif_isv6)
9866 		(void) ipif_arp_down(ipif);
9867 
9868 	return (err);
9869 }
9870 
9871 /*
9872  * Restart entry point to restart the dstaddress set operation after the
9873  * refcounts have dropped to zero.
9874  */
9875 /* ARGSUSED */
9876 int
9877 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9878     ip_ioctl_cmd_t *ipip, void *ifreq)
9879 {
9880 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9881 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9882 	(void) ipif_down_tail(ipif);
9883 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9884 }
9885 
9886 /* ARGSUSED */
9887 int
9888 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9889     ip_ioctl_cmd_t *ipip, void *if_req)
9890 {
9891 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9892 
9893 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9894 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9895 	/*
9896 	 * Get point to point destination address. The addresses can't
9897 	 * change since we hold a reference to the ipif.
9898 	 */
9899 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9900 		return (EADDRNOTAVAIL);
9901 
9902 	if (ipif->ipif_isv6) {
9903 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9904 		*sin6 = sin6_null;
9905 		sin6->sin6_family = AF_INET6;
9906 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9907 	} else {
9908 		*sin = sin_null;
9909 		sin->sin_family = AF_INET;
9910 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9911 	}
9912 	return (0);
9913 }
9914 
9915 /*
9916  * Check which flags will change by the given flags being set
9917  * silently ignore flags which userland is not allowed to control.
9918  * (Because these flags may change between SIOCGLIFFLAGS and
9919  * SIOCSLIFFLAGS, and that's outside of userland's control,
9920  * we need to silently ignore them rather than fail.)
9921  */
9922 static void
9923 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9924     uint64_t *offp)
9925 {
9926 	ill_t		*ill = ipif->ipif_ill;
9927 	phyint_t 	*phyi = ill->ill_phyint;
9928 	uint64_t	cantchange_flags, intf_flags;
9929 	uint64_t	turn_on, turn_off;
9930 
9931 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9932 	cantchange_flags = IFF_CANTCHANGE;
9933 	if (IS_IPMP(ill))
9934 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9935 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9936 	turn_off = intf_flags & turn_on;
9937 	turn_on ^= turn_off;
9938 	*onp = turn_on;
9939 	*offp = turn_off;
9940 }
9941 
9942 /*
9943  * Set interface flags.  Many flags require special handling (e.g.,
9944  * bringing the interface down); see below for details.
9945  *
9946  * NOTE : We really don't enforce that ipif_id zero should be used
9947  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9948  *	  is because applications generally does SICGLIFFLAGS and
9949  *	  ORs in the new flags (that affects the logical) and does a
9950  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9951  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9952  *	  flags that will be turned on is correct with respect to
9953  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9954  */
9955 /* ARGSUSED */
9956 int
9957 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9958     ip_ioctl_cmd_t *ipip, void *if_req)
9959 {
9960 	uint64_t turn_on;
9961 	uint64_t turn_off;
9962 	int	err = 0;
9963 	phyint_t *phyi;
9964 	ill_t *ill;
9965 	conn_t *connp;
9966 	uint64_t intf_flags;
9967 	boolean_t phyint_flags_modified = B_FALSE;
9968 	uint64_t flags;
9969 	struct ifreq *ifr;
9970 	struct lifreq *lifr;
9971 	boolean_t set_linklocal = B_FALSE;
9972 
9973 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9974 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9975 
9976 	ASSERT(IAM_WRITER_IPIF(ipif));
9977 
9978 	ill = ipif->ipif_ill;
9979 	phyi = ill->ill_phyint;
9980 
9981 	if (ipip->ipi_cmd_type == IF_CMD) {
9982 		ifr = (struct ifreq *)if_req;
9983 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9984 	} else {
9985 		lifr = (struct lifreq *)if_req;
9986 		flags = lifr->lifr_flags;
9987 	}
9988 
9989 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9990 
9991 	/*
9992 	 * Have the flags been set correctly until now?
9993 	 */
9994 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9995 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9996 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9997 	/*
9998 	 * Compare the new flags to the old, and partition
9999 	 * into those coming on and those going off.
10000 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10001 	 */
10002 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10003 		flags |= intf_flags & ~0xFFFF;
10004 
10005 	/*
10006 	 * Explicitly fail attempts to change flags that are always invalid on
10007 	 * an IPMP meta-interface.
10008 	 */
10009 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10010 		return (EINVAL);
10011 
10012 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10013 	if ((turn_on|turn_off) == 0)
10014 		return (0);	/* No change */
10015 
10016 	/*
10017 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10018 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10019 	 * allow it to be turned off.
10020 	 */
10021 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10022 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10023 		return (EINVAL);
10024 
10025 	if ((connp = Q_TO_CONN(q)) == NULL)
10026 		return (EINVAL);
10027 
10028 	/*
10029 	 * Only vrrp control socket is allowed to change IFF_UP and
10030 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10031 	 */
10032 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10033 		if (!connp->conn_isvrrp)
10034 			return (EINVAL);
10035 	}
10036 
10037 	/*
10038 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10039 	 * VRRP control socket.
10040 	 */
10041 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10042 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10043 			return (EINVAL);
10044 	}
10045 
10046 	if (turn_on & IFF_NOFAILOVER) {
10047 		turn_on |= IFF_DEPRECATED;
10048 		flags |= IFF_DEPRECATED;
10049 	}
10050 
10051 	/*
10052 	 * On underlying interfaces, only allow applications to manage test
10053 	 * addresses -- otherwise, they may get confused when the address
10054 	 * moves as part of being brought up.  Likewise, prevent an
10055 	 * application-managed test address from being converted to a data
10056 	 * address.  To prevent migration of administratively up addresses in
10057 	 * the kernel, we don't allow them to be converted either.
10058 	 */
10059 	if (IS_UNDER_IPMP(ill)) {
10060 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10061 
10062 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10063 			return (EINVAL);
10064 
10065 		if ((turn_off & IFF_NOFAILOVER) &&
10066 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10067 			return (EINVAL);
10068 	}
10069 
10070 	/*
10071 	 * Only allow IFF_TEMPORARY flag to be set on
10072 	 * IPv6 interfaces.
10073 	 */
10074 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10075 		return (EINVAL);
10076 
10077 	/*
10078 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10079 	 */
10080 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10081 		return (EINVAL);
10082 
10083 	/*
10084 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10085 	 * interfaces.  It makes no sense in that context.
10086 	 */
10087 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10088 		return (EINVAL);
10089 
10090 	/*
10091 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10092 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10093 	 * If the link local address isn't set, and can be set, it will get
10094 	 * set later on in this function.
10095 	 */
10096 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10097 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10098 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10099 		if (ipif_cant_setlinklocal(ipif))
10100 			return (EINVAL);
10101 		set_linklocal = B_TRUE;
10102 	}
10103 
10104 	/*
10105 	 * If we modify physical interface flags, we'll potentially need to
10106 	 * send up two routing socket messages for the changes (one for the
10107 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10108 	 */
10109 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10110 		phyint_flags_modified = B_TRUE;
10111 
10112 	/*
10113 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10114 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10115 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10116 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10117 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10118 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10119 	 * will not be honored.
10120 	 */
10121 	if (turn_on & PHYI_STANDBY) {
10122 		/*
10123 		 * No need to grab ill_g_usesrc_lock here; see the
10124 		 * synchronization notes in ip.c.
10125 		 */
10126 		if (ill->ill_usesrc_grp_next != NULL ||
10127 		    intf_flags & PHYI_INACTIVE)
10128 			return (EINVAL);
10129 		if (!(flags & PHYI_FAILED)) {
10130 			flags |= PHYI_INACTIVE;
10131 			turn_on |= PHYI_INACTIVE;
10132 		}
10133 	}
10134 
10135 	if (turn_off & PHYI_STANDBY) {
10136 		flags &= ~PHYI_INACTIVE;
10137 		turn_off |= PHYI_INACTIVE;
10138 	}
10139 
10140 	/*
10141 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10142 	 * would end up on.
10143 	 */
10144 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10145 	    (PHYI_FAILED | PHYI_INACTIVE))
10146 		return (EINVAL);
10147 
10148 	/*
10149 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10150 	 * status of the interface.
10151 	 */
10152 	if ((turn_on | turn_off) & ILLF_ROUTER)
10153 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10154 
10155 	/*
10156 	 * If the interface is not UP and we are not going to
10157 	 * bring it UP, record the flags and return. When the
10158 	 * interface comes UP later, the right actions will be
10159 	 * taken.
10160 	 */
10161 	if (!(ipif->ipif_flags & IPIF_UP) &&
10162 	    !(turn_on & IPIF_UP)) {
10163 		/* Record new flags in their respective places. */
10164 		mutex_enter(&ill->ill_lock);
10165 		mutex_enter(&ill->ill_phyint->phyint_lock);
10166 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10167 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10168 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10169 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10170 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10171 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10172 		mutex_exit(&ill->ill_lock);
10173 		mutex_exit(&ill->ill_phyint->phyint_lock);
10174 
10175 		/*
10176 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10177 		 * same to the kernel: if any of them has been set by
10178 		 * userland, the interface cannot be used for data traffic.
10179 		 */
10180 		if ((turn_on|turn_off) &
10181 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10182 			ASSERT(!IS_IPMP(ill));
10183 			/*
10184 			 * It's possible the ill is part of an "anonymous"
10185 			 * IPMP group rather than a real group.  In that case,
10186 			 * there are no other interfaces in the group and thus
10187 			 * no need to call ipmp_phyint_refresh_active().
10188 			 */
10189 			if (IS_UNDER_IPMP(ill))
10190 				ipmp_phyint_refresh_active(phyi);
10191 		}
10192 
10193 		if (phyint_flags_modified) {
10194 			if (phyi->phyint_illv4 != NULL) {
10195 				ip_rts_ifmsg(phyi->phyint_illv4->
10196 				    ill_ipif, RTSQ_DEFAULT);
10197 			}
10198 			if (phyi->phyint_illv6 != NULL) {
10199 				ip_rts_ifmsg(phyi->phyint_illv6->
10200 				    ill_ipif, RTSQ_DEFAULT);
10201 			}
10202 		}
10203 		/* The default multicast interface might have changed */
10204 		ire_increment_multicast_generation(ill->ill_ipst,
10205 		    ill->ill_isv6);
10206 
10207 		return (0);
10208 	} else if (set_linklocal) {
10209 		mutex_enter(&ill->ill_lock);
10210 		if (set_linklocal)
10211 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10212 		mutex_exit(&ill->ill_lock);
10213 	}
10214 
10215 	/*
10216 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10217 	 * or point-to-point interfaces with an unspecified destination. We do
10218 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10219 	 * have a subnet assigned, which is how in.ndpd currently manages its
10220 	 * onlink prefix list when no addresses are configured with those
10221 	 * prefixes.
10222 	 */
10223 	if (ipif->ipif_isv6 &&
10224 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10225 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10226 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10227 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10228 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10229 		return (EINVAL);
10230 	}
10231 
10232 	/*
10233 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10234 	 * from being brought up.
10235 	 */
10236 	if (!ipif->ipif_isv6 &&
10237 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10238 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10239 		return (EINVAL);
10240 	}
10241 
10242 	/*
10243 	 * If we are going to change one or more of the flags that are
10244 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10245 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10246 	 * IPIF_NOFAILOVER, we will take special action.  This is
10247 	 * done by bring the ipif down, changing the flags and bringing
10248 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10249 	 * back up will trigger the address to be moved.
10250 	 *
10251 	 * If we are going to change IFF_NOACCEPT, we need to bring
10252 	 * all the ipifs down then bring them up again.	 The act of
10253 	 * bringing all the ipifs back up will trigger the local
10254 	 * ires being recreated with "no_accept" set/cleared.
10255 	 *
10256 	 * Note that ILLF_NOACCEPT is always set separately from the
10257 	 * other flags.
10258 	 */
10259 	if ((turn_on|turn_off) &
10260 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10261 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10262 	    IPIF_NOFAILOVER)) {
10263 		/*
10264 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10265 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10266 		 * entries shared between multiple ipifs on the same subnet.
10267 		 */
10268 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10269 		    !(turn_off & IPIF_UP)) {
10270 			if (ipif->ipif_flags & IPIF_UP)
10271 				ill->ill_logical_down = 1;
10272 			turn_on &= ~IPIF_UP;
10273 		}
10274 		err = ipif_down(ipif, q, mp);
10275 		ip1dbg(("ipif_down returns %d err ", err));
10276 		if (err == EINPROGRESS)
10277 			return (err);
10278 		(void) ipif_down_tail(ipif);
10279 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10280 		/*
10281 		 * If we can quiesce the ill, then continue.  If not, then
10282 		 * ip_sioctl_flags_tail() will be called from
10283 		 * ipif_ill_refrele_tail().
10284 		 */
10285 		ill_down_ipifs(ill, B_TRUE);
10286 
10287 		mutex_enter(&connp->conn_lock);
10288 		mutex_enter(&ill->ill_lock);
10289 		if (!ill_is_quiescent(ill)) {
10290 			boolean_t success;
10291 
10292 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10293 			    q, mp, ILL_DOWN);
10294 			mutex_exit(&ill->ill_lock);
10295 			mutex_exit(&connp->conn_lock);
10296 			return (success ? EINPROGRESS : EINTR);
10297 		}
10298 		mutex_exit(&ill->ill_lock);
10299 		mutex_exit(&connp->conn_lock);
10300 	}
10301 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10302 }
10303 
10304 static int
10305 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10306 {
10307 	ill_t	*ill;
10308 	phyint_t *phyi;
10309 	uint64_t turn_on, turn_off;
10310 	boolean_t phyint_flags_modified = B_FALSE;
10311 	int	err = 0;
10312 	boolean_t set_linklocal = B_FALSE;
10313 
10314 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10315 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10316 
10317 	ASSERT(IAM_WRITER_IPIF(ipif));
10318 
10319 	ill = ipif->ipif_ill;
10320 	phyi = ill->ill_phyint;
10321 
10322 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10323 
10324 	/*
10325 	 * IFF_UP is handled separately.
10326 	 */
10327 	turn_on &= ~IFF_UP;
10328 	turn_off &= ~IFF_UP;
10329 
10330 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10331 		phyint_flags_modified = B_TRUE;
10332 
10333 	/*
10334 	 * Now we change the flags. Track current value of
10335 	 * other flags in their respective places.
10336 	 */
10337 	mutex_enter(&ill->ill_lock);
10338 	mutex_enter(&phyi->phyint_lock);
10339 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10340 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10341 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10342 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10343 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10344 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10345 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10346 		set_linklocal = B_TRUE;
10347 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10348 	}
10349 
10350 	mutex_exit(&ill->ill_lock);
10351 	mutex_exit(&phyi->phyint_lock);
10352 
10353 	if (set_linklocal)
10354 		(void) ipif_setlinklocal(ipif);
10355 
10356 	/*
10357 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10358 	 * the kernel: if any of them has been set by userland, the interface
10359 	 * cannot be used for data traffic.
10360 	 */
10361 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10362 		ASSERT(!IS_IPMP(ill));
10363 		/*
10364 		 * It's possible the ill is part of an "anonymous" IPMP group
10365 		 * rather than a real group.  In that case, there are no other
10366 		 * interfaces in the group and thus no need for us to call
10367 		 * ipmp_phyint_refresh_active().
10368 		 */
10369 		if (IS_UNDER_IPMP(ill))
10370 			ipmp_phyint_refresh_active(phyi);
10371 	}
10372 
10373 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10374 		/*
10375 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10376 		 * ipifs that were brought down.
10377 		 *
10378 		 * The routing sockets messages are sent as the result
10379 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10380 		 * as well.
10381 		 */
10382 		err = ill_up_ipifs(ill, q, mp);
10383 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10384 		/*
10385 		 * XXX ipif_up really does not know whether a phyint flags
10386 		 * was modified or not. So, it sends up information on
10387 		 * only one routing sockets message. As we don't bring up
10388 		 * the interface and also set PHYI_ flags simultaneously
10389 		 * it should be okay.
10390 		 */
10391 		err = ipif_up(ipif, q, mp);
10392 	} else {
10393 		/*
10394 		 * Make sure routing socket sees all changes to the flags.
10395 		 * ipif_up_done* handles this when we use ipif_up.
10396 		 */
10397 		if (phyint_flags_modified) {
10398 			if (phyi->phyint_illv4 != NULL) {
10399 				ip_rts_ifmsg(phyi->phyint_illv4->
10400 				    ill_ipif, RTSQ_DEFAULT);
10401 			}
10402 			if (phyi->phyint_illv6 != NULL) {
10403 				ip_rts_ifmsg(phyi->phyint_illv6->
10404 				    ill_ipif, RTSQ_DEFAULT);
10405 			}
10406 		} else {
10407 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10408 		}
10409 		/*
10410 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10411 		 * this in need_up case.
10412 		 */
10413 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10414 	}
10415 
10416 	/* The default multicast interface might have changed */
10417 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10418 	return (err);
10419 }
10420 
10421 /*
10422  * Restart the flags operation now that the refcounts have dropped to zero.
10423  */
10424 /* ARGSUSED */
10425 int
10426 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10427     ip_ioctl_cmd_t *ipip, void *if_req)
10428 {
10429 	uint64_t flags;
10430 	struct ifreq *ifr = if_req;
10431 	struct lifreq *lifr = if_req;
10432 	uint64_t turn_on, turn_off;
10433 
10434 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10435 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10436 
10437 	if (ipip->ipi_cmd_type == IF_CMD) {
10438 		/* cast to uint16_t prevents unwanted sign extension */
10439 		flags = (uint16_t)ifr->ifr_flags;
10440 	} else {
10441 		flags = lifr->lifr_flags;
10442 	}
10443 
10444 	/*
10445 	 * If this function call is a result of the ILLF_NOACCEPT flag
10446 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10447 	 */
10448 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10449 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10450 		(void) ipif_down_tail(ipif);
10451 
10452 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10453 }
10454 
10455 /*
10456  * Can operate on either a module or a driver queue.
10457  */
10458 /* ARGSUSED */
10459 int
10460 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10461     ip_ioctl_cmd_t *ipip, void *if_req)
10462 {
10463 	/*
10464 	 * Has the flags been set correctly till now ?
10465 	 */
10466 	ill_t *ill = ipif->ipif_ill;
10467 	phyint_t *phyi = ill->ill_phyint;
10468 
10469 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10470 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10471 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10472 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10473 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10474 
10475 	/*
10476 	 * Need a lock since some flags can be set even when there are
10477 	 * references to the ipif.
10478 	 */
10479 	mutex_enter(&ill->ill_lock);
10480 	if (ipip->ipi_cmd_type == IF_CMD) {
10481 		struct ifreq *ifr = (struct ifreq *)if_req;
10482 
10483 		/* Get interface flags (low 16 only). */
10484 		ifr->ifr_flags = ((ipif->ipif_flags |
10485 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10486 	} else {
10487 		struct lifreq *lifr = (struct lifreq *)if_req;
10488 
10489 		/* Get interface flags. */
10490 		lifr->lifr_flags = ipif->ipif_flags |
10491 		    ill->ill_flags | phyi->phyint_flags;
10492 	}
10493 	mutex_exit(&ill->ill_lock);
10494 	return (0);
10495 }
10496 
10497 /*
10498  * We allow the MTU to be set on an ILL, but not have it be different
10499  * for different IPIFs since we don't actually send packets on IPIFs.
10500  */
10501 /* ARGSUSED */
10502 int
10503 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10504     ip_ioctl_cmd_t *ipip, void *if_req)
10505 {
10506 	int mtu;
10507 	int ip_min_mtu;
10508 	struct ifreq	*ifr;
10509 	struct lifreq *lifr;
10510 	ill_t	*ill;
10511 
10512 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10513 	    ipif->ipif_id, (void *)ipif));
10514 	if (ipip->ipi_cmd_type == IF_CMD) {
10515 		ifr = (struct ifreq *)if_req;
10516 		mtu = ifr->ifr_metric;
10517 	} else {
10518 		lifr = (struct lifreq *)if_req;
10519 		mtu = lifr->lifr_mtu;
10520 	}
10521 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10522 	if (ipif->ipif_id != 0)
10523 		return (EINVAL);
10524 
10525 	ill = ipif->ipif_ill;
10526 	if (ipif->ipif_isv6)
10527 		ip_min_mtu = IPV6_MIN_MTU;
10528 	else
10529 		ip_min_mtu = IP_MIN_MTU;
10530 
10531 	mutex_enter(&ill->ill_lock);
10532 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10533 		mutex_exit(&ill->ill_lock);
10534 		return (EINVAL);
10535 	}
10536 	/*
10537 	 * The dce and fragmentation code can handle changes to ill_mtu
10538 	 * concurrent with sending/fragmenting packets.
10539 	 */
10540 	ill->ill_mtu = mtu;
10541 	ill->ill_flags |= ILLF_FIXEDMTU;
10542 	mutex_exit(&ill->ill_lock);
10543 
10544 	/*
10545 	 * Make sure all dce_generation checks find out
10546 	 * that ill_mtu has changed.
10547 	 */
10548 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10549 
10550 	/* Update the MTU in SCTP's list */
10551 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10552 	return (0);
10553 }
10554 
10555 /* Get interface MTU. */
10556 /* ARGSUSED */
10557 int
10558 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10559 	ip_ioctl_cmd_t *ipip, void *if_req)
10560 {
10561 	struct ifreq	*ifr;
10562 	struct lifreq	*lifr;
10563 
10564 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10565 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10566 
10567 	/*
10568 	 * We allow a get on any logical interface even though the set
10569 	 * can only be done on logical unit 0.
10570 	 */
10571 	if (ipip->ipi_cmd_type == IF_CMD) {
10572 		ifr = (struct ifreq *)if_req;
10573 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10574 	} else {
10575 		lifr = (struct lifreq *)if_req;
10576 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10577 	}
10578 	return (0);
10579 }
10580 
10581 /* Set interface broadcast address. */
10582 /* ARGSUSED2 */
10583 int
10584 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10585 	ip_ioctl_cmd_t *ipip, void *if_req)
10586 {
10587 	ipaddr_t addr;
10588 	ire_t	*ire;
10589 	ill_t		*ill = ipif->ipif_ill;
10590 	ip_stack_t	*ipst = ill->ill_ipst;
10591 
10592 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10593 	    ipif->ipif_id));
10594 
10595 	ASSERT(IAM_WRITER_IPIF(ipif));
10596 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10597 		return (EADDRNOTAVAIL);
10598 
10599 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10600 
10601 	if (sin->sin_family != AF_INET)
10602 		return (EAFNOSUPPORT);
10603 
10604 	addr = sin->sin_addr.s_addr;
10605 	if (ipif->ipif_flags & IPIF_UP) {
10606 		/*
10607 		 * If we are already up, make sure the new
10608 		 * broadcast address makes sense.  If it does,
10609 		 * there should be an IRE for it already.
10610 		 */
10611 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10612 		    ill, ipif->ipif_zoneid, NULL,
10613 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10614 		if (ire == NULL) {
10615 			return (EINVAL);
10616 		} else {
10617 			ire_refrele(ire);
10618 		}
10619 	}
10620 	/*
10621 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10622 	 * needs to already exist we never need to change the set of
10623 	 * IRE_BROADCASTs when we are UP.
10624 	 */
10625 	if (addr != ipif->ipif_brd_addr)
10626 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10627 
10628 	return (0);
10629 }
10630 
10631 /* Get interface broadcast address. */
10632 /* ARGSUSED */
10633 int
10634 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10635     ip_ioctl_cmd_t *ipip, void *if_req)
10636 {
10637 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10638 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10639 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10640 		return (EADDRNOTAVAIL);
10641 
10642 	/* IPIF_BROADCAST not possible with IPv6 */
10643 	ASSERT(!ipif->ipif_isv6);
10644 	*sin = sin_null;
10645 	sin->sin_family = AF_INET;
10646 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10647 	return (0);
10648 }
10649 
10650 /*
10651  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10652  */
10653 /* ARGSUSED */
10654 int
10655 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10656     ip_ioctl_cmd_t *ipip, void *if_req)
10657 {
10658 	int err = 0;
10659 	in6_addr_t v6mask;
10660 
10661 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10662 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10663 
10664 	ASSERT(IAM_WRITER_IPIF(ipif));
10665 
10666 	if (ipif->ipif_isv6) {
10667 		sin6_t *sin6;
10668 
10669 		if (sin->sin_family != AF_INET6)
10670 			return (EAFNOSUPPORT);
10671 
10672 		sin6 = (sin6_t *)sin;
10673 		v6mask = sin6->sin6_addr;
10674 	} else {
10675 		ipaddr_t mask;
10676 
10677 		if (sin->sin_family != AF_INET)
10678 			return (EAFNOSUPPORT);
10679 
10680 		mask = sin->sin_addr.s_addr;
10681 		V4MASK_TO_V6(mask, v6mask);
10682 	}
10683 
10684 	/*
10685 	 * No big deal if the interface isn't already up, or the mask
10686 	 * isn't really changing, or this is pt-pt.
10687 	 */
10688 	if (!(ipif->ipif_flags & IPIF_UP) ||
10689 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10690 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10691 		ipif->ipif_v6net_mask = v6mask;
10692 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10693 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10694 			    ipif->ipif_v6net_mask,
10695 			    ipif->ipif_v6subnet);
10696 		}
10697 		return (0);
10698 	}
10699 	/*
10700 	 * Make sure we have valid net and subnet broadcast ire's
10701 	 * for the old netmask, if needed by other logical interfaces.
10702 	 */
10703 	err = ipif_logical_down(ipif, q, mp);
10704 	if (err == EINPROGRESS)
10705 		return (err);
10706 	(void) ipif_down_tail(ipif);
10707 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10708 	return (err);
10709 }
10710 
10711 static int
10712 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10713 {
10714 	in6_addr_t v6mask;
10715 	int err = 0;
10716 
10717 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10718 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10719 
10720 	if (ipif->ipif_isv6) {
10721 		sin6_t *sin6;
10722 
10723 		sin6 = (sin6_t *)sin;
10724 		v6mask = sin6->sin6_addr;
10725 	} else {
10726 		ipaddr_t mask;
10727 
10728 		mask = sin->sin_addr.s_addr;
10729 		V4MASK_TO_V6(mask, v6mask);
10730 	}
10731 
10732 	ipif->ipif_v6net_mask = v6mask;
10733 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10734 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10735 		    ipif->ipif_v6subnet);
10736 	}
10737 	err = ipif_up(ipif, q, mp);
10738 
10739 	if (err == 0 || err == EINPROGRESS) {
10740 		/*
10741 		 * The interface must be DL_BOUND if this packet has to
10742 		 * go out on the wire. Since we only go through a logical
10743 		 * down and are bound with the driver during an internal
10744 		 * down/up that is satisfied.
10745 		 */
10746 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10747 			/* Potentially broadcast an address mask reply. */
10748 			ipif_mask_reply(ipif);
10749 		}
10750 	}
10751 	return (err);
10752 }
10753 
10754 /* ARGSUSED */
10755 int
10756 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10757     ip_ioctl_cmd_t *ipip, void *if_req)
10758 {
10759 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10760 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10761 	(void) ipif_down_tail(ipif);
10762 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10763 }
10764 
10765 /* Get interface net mask. */
10766 /* ARGSUSED */
10767 int
10768 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10769     ip_ioctl_cmd_t *ipip, void *if_req)
10770 {
10771 	struct lifreq *lifr = (struct lifreq *)if_req;
10772 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10773 
10774 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10775 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10776 
10777 	/*
10778 	 * net mask can't change since we have a reference to the ipif.
10779 	 */
10780 	if (ipif->ipif_isv6) {
10781 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10782 		*sin6 = sin6_null;
10783 		sin6->sin6_family = AF_INET6;
10784 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10785 		lifr->lifr_addrlen =
10786 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10787 	} else {
10788 		*sin = sin_null;
10789 		sin->sin_family = AF_INET;
10790 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10791 		if (ipip->ipi_cmd_type == LIF_CMD) {
10792 			lifr->lifr_addrlen =
10793 			    ip_mask_to_plen(ipif->ipif_net_mask);
10794 		}
10795 	}
10796 	return (0);
10797 }
10798 
10799 /* ARGSUSED */
10800 int
10801 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10802     ip_ioctl_cmd_t *ipip, void *if_req)
10803 {
10804 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10805 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10806 
10807 	/*
10808 	 * Since no applications should ever be setting metrics on underlying
10809 	 * interfaces, we explicitly fail to smoke 'em out.
10810 	 */
10811 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10812 		return (EINVAL);
10813 
10814 	/*
10815 	 * Set interface metric.  We don't use this for
10816 	 * anything but we keep track of it in case it is
10817 	 * important to routing applications or such.
10818 	 */
10819 	if (ipip->ipi_cmd_type == IF_CMD) {
10820 		struct ifreq    *ifr;
10821 
10822 		ifr = (struct ifreq *)if_req;
10823 		ipif->ipif_metric = ifr->ifr_metric;
10824 	} else {
10825 		struct lifreq   *lifr;
10826 
10827 		lifr = (struct lifreq *)if_req;
10828 		ipif->ipif_metric = lifr->lifr_metric;
10829 	}
10830 	return (0);
10831 }
10832 
10833 /* ARGSUSED */
10834 int
10835 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10836     ip_ioctl_cmd_t *ipip, void *if_req)
10837 {
10838 	/* Get interface metric. */
10839 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10840 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10841 
10842 	if (ipip->ipi_cmd_type == IF_CMD) {
10843 		struct ifreq    *ifr;
10844 
10845 		ifr = (struct ifreq *)if_req;
10846 		ifr->ifr_metric = ipif->ipif_metric;
10847 	} else {
10848 		struct lifreq   *lifr;
10849 
10850 		lifr = (struct lifreq *)if_req;
10851 		lifr->lifr_metric = ipif->ipif_metric;
10852 	}
10853 
10854 	return (0);
10855 }
10856 
10857 /* ARGSUSED */
10858 int
10859 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10860     ip_ioctl_cmd_t *ipip, void *if_req)
10861 {
10862 	int	arp_muxid;
10863 
10864 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10865 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10866 	/*
10867 	 * Set the muxid returned from I_PLINK.
10868 	 */
10869 	if (ipip->ipi_cmd_type == IF_CMD) {
10870 		struct ifreq *ifr = (struct ifreq *)if_req;
10871 
10872 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10873 		arp_muxid = ifr->ifr_arp_muxid;
10874 	} else {
10875 		struct lifreq *lifr = (struct lifreq *)if_req;
10876 
10877 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10878 		arp_muxid = lifr->lifr_arp_muxid;
10879 	}
10880 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10881 	return (0);
10882 }
10883 
10884 /* ARGSUSED */
10885 int
10886 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10887     ip_ioctl_cmd_t *ipip, void *if_req)
10888 {
10889 	int	arp_muxid = 0;
10890 
10891 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10892 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10893 	/*
10894 	 * Get the muxid saved in ill for I_PUNLINK.
10895 	 */
10896 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10897 	if (ipip->ipi_cmd_type == IF_CMD) {
10898 		struct ifreq *ifr = (struct ifreq *)if_req;
10899 
10900 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10901 		ifr->ifr_arp_muxid = arp_muxid;
10902 	} else {
10903 		struct lifreq *lifr = (struct lifreq *)if_req;
10904 
10905 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10906 		lifr->lifr_arp_muxid = arp_muxid;
10907 	}
10908 	return (0);
10909 }
10910 
10911 /*
10912  * Set the subnet prefix. Does not modify the broadcast address.
10913  */
10914 /* ARGSUSED */
10915 int
10916 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10917     ip_ioctl_cmd_t *ipip, void *if_req)
10918 {
10919 	int err = 0;
10920 	in6_addr_t v6addr;
10921 	in6_addr_t v6mask;
10922 	boolean_t need_up = B_FALSE;
10923 	int addrlen;
10924 
10925 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10926 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10927 
10928 	ASSERT(IAM_WRITER_IPIF(ipif));
10929 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10930 
10931 	if (ipif->ipif_isv6) {
10932 		sin6_t *sin6;
10933 
10934 		if (sin->sin_family != AF_INET6)
10935 			return (EAFNOSUPPORT);
10936 
10937 		sin6 = (sin6_t *)sin;
10938 		v6addr = sin6->sin6_addr;
10939 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10940 			return (EADDRNOTAVAIL);
10941 	} else {
10942 		ipaddr_t addr;
10943 
10944 		if (sin->sin_family != AF_INET)
10945 			return (EAFNOSUPPORT);
10946 
10947 		addr = sin->sin_addr.s_addr;
10948 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10949 			return (EADDRNOTAVAIL);
10950 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10951 		/* Add 96 bits */
10952 		addrlen += IPV6_ABITS - IP_ABITS;
10953 	}
10954 
10955 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10956 		return (EINVAL);
10957 
10958 	/* Check if bits in the address is set past the mask */
10959 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10960 		return (EINVAL);
10961 
10962 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10963 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10964 		return (0);	/* No change */
10965 
10966 	if (ipif->ipif_flags & IPIF_UP) {
10967 		/*
10968 		 * If the interface is already marked up,
10969 		 * we call ipif_down which will take care
10970 		 * of ditching any IREs that have been set
10971 		 * up based on the old interface address.
10972 		 */
10973 		err = ipif_logical_down(ipif, q, mp);
10974 		if (err == EINPROGRESS)
10975 			return (err);
10976 		(void) ipif_down_tail(ipif);
10977 		need_up = B_TRUE;
10978 	}
10979 
10980 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10981 	return (err);
10982 }
10983 
10984 static int
10985 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10986     queue_t *q, mblk_t *mp, boolean_t need_up)
10987 {
10988 	ill_t	*ill = ipif->ipif_ill;
10989 	int	err = 0;
10990 
10991 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10992 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10993 
10994 	/* Set the new address. */
10995 	mutex_enter(&ill->ill_lock);
10996 	ipif->ipif_v6net_mask = v6mask;
10997 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10998 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10999 		    ipif->ipif_v6subnet);
11000 	}
11001 	mutex_exit(&ill->ill_lock);
11002 
11003 	if (need_up) {
11004 		/*
11005 		 * Now bring the interface back up.  If this
11006 		 * is the only IPIF for the ILL, ipif_up
11007 		 * will have to re-bind to the device, so
11008 		 * we may get back EINPROGRESS, in which
11009 		 * case, this IOCTL will get completed in
11010 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11011 		 */
11012 		err = ipif_up(ipif, q, mp);
11013 		if (err == EINPROGRESS)
11014 			return (err);
11015 	}
11016 	return (err);
11017 }
11018 
11019 /* ARGSUSED */
11020 int
11021 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11022     ip_ioctl_cmd_t *ipip, void *if_req)
11023 {
11024 	int	addrlen;
11025 	in6_addr_t v6addr;
11026 	in6_addr_t v6mask;
11027 	struct lifreq *lifr = (struct lifreq *)if_req;
11028 
11029 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11030 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11031 	(void) ipif_down_tail(ipif);
11032 
11033 	addrlen = lifr->lifr_addrlen;
11034 	if (ipif->ipif_isv6) {
11035 		sin6_t *sin6;
11036 
11037 		sin6 = (sin6_t *)sin;
11038 		v6addr = sin6->sin6_addr;
11039 	} else {
11040 		ipaddr_t addr;
11041 
11042 		addr = sin->sin_addr.s_addr;
11043 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11044 		addrlen += IPV6_ABITS - IP_ABITS;
11045 	}
11046 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11047 
11048 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11049 }
11050 
11051 /* ARGSUSED */
11052 int
11053 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11054     ip_ioctl_cmd_t *ipip, void *if_req)
11055 {
11056 	struct lifreq *lifr = (struct lifreq *)if_req;
11057 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11058 
11059 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11060 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11061 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11062 
11063 	if (ipif->ipif_isv6) {
11064 		*sin6 = sin6_null;
11065 		sin6->sin6_family = AF_INET6;
11066 		sin6->sin6_addr = ipif->ipif_v6subnet;
11067 		lifr->lifr_addrlen =
11068 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11069 	} else {
11070 		*sin = sin_null;
11071 		sin->sin_family = AF_INET;
11072 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11073 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11074 	}
11075 	return (0);
11076 }
11077 
11078 /*
11079  * Set the IPv6 address token.
11080  */
11081 /* ARGSUSED */
11082 int
11083 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11084     ip_ioctl_cmd_t *ipi, void *if_req)
11085 {
11086 	ill_t *ill = ipif->ipif_ill;
11087 	int err;
11088 	in6_addr_t v6addr;
11089 	in6_addr_t v6mask;
11090 	boolean_t need_up = B_FALSE;
11091 	int i;
11092 	sin6_t *sin6 = (sin6_t *)sin;
11093 	struct lifreq *lifr = (struct lifreq *)if_req;
11094 	int addrlen;
11095 
11096 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11097 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11098 	ASSERT(IAM_WRITER_IPIF(ipif));
11099 
11100 	addrlen = lifr->lifr_addrlen;
11101 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11102 	if (ipif->ipif_id != 0)
11103 		return (EINVAL);
11104 
11105 	if (!ipif->ipif_isv6)
11106 		return (EINVAL);
11107 
11108 	if (addrlen > IPV6_ABITS)
11109 		return (EINVAL);
11110 
11111 	v6addr = sin6->sin6_addr;
11112 
11113 	/*
11114 	 * The length of the token is the length from the end.  To get
11115 	 * the proper mask for this, compute the mask of the bits not
11116 	 * in the token; ie. the prefix, and then xor to get the mask.
11117 	 */
11118 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11119 		return (EINVAL);
11120 	for (i = 0; i < 4; i++) {
11121 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11122 	}
11123 
11124 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11125 	    ill->ill_token_length == addrlen)
11126 		return (0);	/* No change */
11127 
11128 	if (ipif->ipif_flags & IPIF_UP) {
11129 		err = ipif_logical_down(ipif, q, mp);
11130 		if (err == EINPROGRESS)
11131 			return (err);
11132 		(void) ipif_down_tail(ipif);
11133 		need_up = B_TRUE;
11134 	}
11135 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11136 	return (err);
11137 }
11138 
11139 static int
11140 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11141     mblk_t *mp, boolean_t need_up)
11142 {
11143 	in6_addr_t v6addr;
11144 	in6_addr_t v6mask;
11145 	ill_t	*ill = ipif->ipif_ill;
11146 	int	i;
11147 	int	err = 0;
11148 
11149 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11150 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11151 	v6addr = sin6->sin6_addr;
11152 	/*
11153 	 * The length of the token is the length from the end.  To get
11154 	 * the proper mask for this, compute the mask of the bits not
11155 	 * in the token; ie. the prefix, and then xor to get the mask.
11156 	 */
11157 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11158 	for (i = 0; i < 4; i++)
11159 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11160 
11161 	mutex_enter(&ill->ill_lock);
11162 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11163 	ill->ill_token_length = addrlen;
11164 	ill->ill_manual_token = 1;
11165 
11166 	/* Reconfigure the link-local address based on this new token */
11167 	ipif_setlinklocal(ill->ill_ipif);
11168 
11169 	mutex_exit(&ill->ill_lock);
11170 
11171 	if (need_up) {
11172 		/*
11173 		 * Now bring the interface back up.  If this
11174 		 * is the only IPIF for the ILL, ipif_up
11175 		 * will have to re-bind to the device, so
11176 		 * we may get back EINPROGRESS, in which
11177 		 * case, this IOCTL will get completed in
11178 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11179 		 */
11180 		err = ipif_up(ipif, q, mp);
11181 		if (err == EINPROGRESS)
11182 			return (err);
11183 	}
11184 	return (err);
11185 }
11186 
11187 /* ARGSUSED */
11188 int
11189 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11190     ip_ioctl_cmd_t *ipi, void *if_req)
11191 {
11192 	ill_t *ill;
11193 	sin6_t *sin6 = (sin6_t *)sin;
11194 	struct lifreq *lifr = (struct lifreq *)if_req;
11195 
11196 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11197 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11198 	if (ipif->ipif_id != 0)
11199 		return (EINVAL);
11200 
11201 	ill = ipif->ipif_ill;
11202 	if (!ill->ill_isv6)
11203 		return (ENXIO);
11204 
11205 	*sin6 = sin6_null;
11206 	sin6->sin6_family = AF_INET6;
11207 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11208 	sin6->sin6_addr = ill->ill_token;
11209 	lifr->lifr_addrlen = ill->ill_token_length;
11210 	return (0);
11211 }
11212 
11213 /*
11214  * Set (hardware) link specific information that might override
11215  * what was acquired through the DL_INFO_ACK.
11216  */
11217 /* ARGSUSED */
11218 int
11219 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11220     ip_ioctl_cmd_t *ipi, void *if_req)
11221 {
11222 	ill_t		*ill = ipif->ipif_ill;
11223 	int		ip_min_mtu;
11224 	struct lifreq	*lifr = (struct lifreq *)if_req;
11225 	lif_ifinfo_req_t *lir;
11226 
11227 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11228 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11229 	lir = &lifr->lifr_ifinfo;
11230 	ASSERT(IAM_WRITER_IPIF(ipif));
11231 
11232 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11233 	if (ipif->ipif_id != 0)
11234 		return (EINVAL);
11235 
11236 	/* Set interface MTU. */
11237 	if (ipif->ipif_isv6)
11238 		ip_min_mtu = IPV6_MIN_MTU;
11239 	else
11240 		ip_min_mtu = IP_MIN_MTU;
11241 
11242 	/*
11243 	 * Verify values before we set anything. Allow zero to
11244 	 * mean unspecified.
11245 	 *
11246 	 * XXX We should be able to set the user-defined lir_mtu to some value
11247 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11248 	 * ill_max_frag value tells us the max MTU that can be handled by the
11249 	 * datalink, whereas the ill_current_frag is dynamically computed for
11250 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11251 	 * since there is currently no way of distinguishing between
11252 	 * administratively fixed link mtu values (e.g., those set via
11253 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11254 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11255 	 * upper-bound.
11256 	 */
11257 	if (lir->lir_maxmtu != 0 &&
11258 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11259 	    lir->lir_maxmtu < ip_min_mtu))
11260 		return (EINVAL);
11261 	if (lir->lir_reachtime != 0 &&
11262 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11263 		return (EINVAL);
11264 	if (lir->lir_reachretrans != 0 &&
11265 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11266 		return (EINVAL);
11267 
11268 	mutex_enter(&ill->ill_lock);
11269 	/*
11270 	 * The dce and fragmentation code can handle changes to ill_mtu
11271 	 * concurrent with sending/fragmenting packets.
11272 	 */
11273 	if (lir->lir_maxmtu != 0)
11274 		ill->ill_user_mtu = lir->lir_maxmtu;
11275 
11276 	if (lir->lir_reachtime != 0)
11277 		ill->ill_reachable_time = lir->lir_reachtime;
11278 
11279 	if (lir->lir_reachretrans != 0)
11280 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11281 
11282 	ill->ill_max_hops = lir->lir_maxhops;
11283 	ill->ill_max_buf = ND_MAX_Q;
11284 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11285 		/*
11286 		 * ill_mtu is the actual interface MTU, obtained as the min
11287 		 * of user-configured mtu and the value announced by the
11288 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11289 		 * we have already made the choice of requiring
11290 		 * ill_user_mtu < ill_current_frag by the time we get here,
11291 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11292 		 * here.
11293 		 */
11294 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11295 	}
11296 	mutex_exit(&ill->ill_lock);
11297 
11298 	/*
11299 	 * Make sure all dce_generation checks find out
11300 	 * that ill_mtu has changed.
11301 	 */
11302 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11303 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11304 
11305 	/*
11306 	 * Refresh IPMP meta-interface MTU if necessary.
11307 	 */
11308 	if (IS_UNDER_IPMP(ill))
11309 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11310 
11311 	return (0);
11312 }
11313 
11314 /* ARGSUSED */
11315 int
11316 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11317     ip_ioctl_cmd_t *ipi, void *if_req)
11318 {
11319 	struct lif_ifinfo_req *lir;
11320 	ill_t *ill = ipif->ipif_ill;
11321 
11322 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11323 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11324 	if (ipif->ipif_id != 0)
11325 		return (EINVAL);
11326 
11327 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11328 	lir->lir_maxhops = ill->ill_max_hops;
11329 	lir->lir_reachtime = ill->ill_reachable_time;
11330 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11331 	lir->lir_maxmtu = ill->ill_mtu;
11332 
11333 	return (0);
11334 }
11335 
11336 /*
11337  * Return best guess as to the subnet mask for the specified address.
11338  * Based on the subnet masks for all the configured interfaces.
11339  *
11340  * We end up returning a zero mask in the case of default, multicast or
11341  * experimental.
11342  */
11343 static ipaddr_t
11344 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11345 {
11346 	ipaddr_t net_mask;
11347 	ill_t	*ill;
11348 	ipif_t	*ipif;
11349 	ill_walk_context_t ctx;
11350 	ipif_t	*fallback_ipif = NULL;
11351 
11352 	net_mask = ip_net_mask(addr);
11353 	if (net_mask == 0) {
11354 		*ipifp = NULL;
11355 		return (0);
11356 	}
11357 
11358 	/* Let's check to see if this is maybe a local subnet route. */
11359 	/* this function only applies to IPv4 interfaces */
11360 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11361 	ill = ILL_START_WALK_V4(&ctx, ipst);
11362 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11363 		mutex_enter(&ill->ill_lock);
11364 		for (ipif = ill->ill_ipif; ipif != NULL;
11365 		    ipif = ipif->ipif_next) {
11366 			if (IPIF_IS_CONDEMNED(ipif))
11367 				continue;
11368 			if (!(ipif->ipif_flags & IPIF_UP))
11369 				continue;
11370 			if ((ipif->ipif_subnet & net_mask) ==
11371 			    (addr & net_mask)) {
11372 				/*
11373 				 * Don't trust pt-pt interfaces if there are
11374 				 * other interfaces.
11375 				 */
11376 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11377 					if (fallback_ipif == NULL) {
11378 						ipif_refhold_locked(ipif);
11379 						fallback_ipif = ipif;
11380 					}
11381 					continue;
11382 				}
11383 
11384 				/*
11385 				 * Fine. Just assume the same net mask as the
11386 				 * directly attached subnet interface is using.
11387 				 */
11388 				ipif_refhold_locked(ipif);
11389 				mutex_exit(&ill->ill_lock);
11390 				rw_exit(&ipst->ips_ill_g_lock);
11391 				if (fallback_ipif != NULL)
11392 					ipif_refrele(fallback_ipif);
11393 				*ipifp = ipif;
11394 				return (ipif->ipif_net_mask);
11395 			}
11396 		}
11397 		mutex_exit(&ill->ill_lock);
11398 	}
11399 	rw_exit(&ipst->ips_ill_g_lock);
11400 
11401 	*ipifp = fallback_ipif;
11402 	return ((fallback_ipif != NULL) ?
11403 	    fallback_ipif->ipif_net_mask : net_mask);
11404 }
11405 
11406 /*
11407  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11408  */
11409 static void
11410 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11411 {
11412 	IOCP	iocp;
11413 	ipft_t	*ipft;
11414 	ipllc_t	*ipllc;
11415 	mblk_t	*mp1;
11416 	cred_t	*cr;
11417 	int	error = 0;
11418 	conn_t	*connp;
11419 
11420 	ip1dbg(("ip_wput_ioctl"));
11421 	iocp = (IOCP)mp->b_rptr;
11422 	mp1 = mp->b_cont;
11423 	if (mp1 == NULL) {
11424 		iocp->ioc_error = EINVAL;
11425 		mp->b_datap->db_type = M_IOCNAK;
11426 		iocp->ioc_count = 0;
11427 		qreply(q, mp);
11428 		return;
11429 	}
11430 
11431 	/*
11432 	 * These IOCTLs provide various control capabilities to
11433 	 * upstream agents such as ULPs and processes.	There
11434 	 * are currently two such IOCTLs implemented.  They
11435 	 * are used by TCP to provide update information for
11436 	 * existing IREs and to forcibly delete an IRE for a
11437 	 * host that is not responding, thereby forcing an
11438 	 * attempt at a new route.
11439 	 */
11440 	iocp->ioc_error = EINVAL;
11441 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11442 		goto done;
11443 
11444 	ipllc = (ipllc_t *)mp1->b_rptr;
11445 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11446 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11447 			break;
11448 	}
11449 	/*
11450 	 * prefer credential from mblk over ioctl;
11451 	 * see ip_sioctl_copyin_setup
11452 	 */
11453 	cr = msg_getcred(mp, NULL);
11454 	if (cr == NULL)
11455 		cr = iocp->ioc_cr;
11456 
11457 	/*
11458 	 * Refhold the conn in case the request gets queued up in some lookup
11459 	 */
11460 	ASSERT(CONN_Q(q));
11461 	connp = Q_TO_CONN(q);
11462 	CONN_INC_REF(connp);
11463 	if (ipft->ipft_pfi &&
11464 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11465 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11466 		error = (*ipft->ipft_pfi)(q,
11467 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11468 	}
11469 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11470 		/*
11471 		 * CONN_OPER_PENDING_DONE happens in the function called
11472 		 * through ipft_pfi above.
11473 		 */
11474 		return;
11475 	}
11476 
11477 	CONN_OPER_PENDING_DONE(connp);
11478 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11479 		freemsg(mp);
11480 		return;
11481 	}
11482 	iocp->ioc_error = error;
11483 
11484 done:
11485 	mp->b_datap->db_type = M_IOCACK;
11486 	if (iocp->ioc_error)
11487 		iocp->ioc_count = 0;
11488 	qreply(q, mp);
11489 }
11490 
11491 /*
11492  * Assign a unique id for the ipif. This is used by sctp_addr.c
11493  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11494  */
11495 static void
11496 ipif_assign_seqid(ipif_t *ipif)
11497 {
11498 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11499 
11500 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11501 }
11502 
11503 /*
11504  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11505  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11506  * that the clone is complete -- including the seqid -- and the expectation is
11507  * that the caller will either free or overwrite `sipif' before it's unlocked.
11508  */
11509 static void
11510 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11511 {
11512 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11513 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11514 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11515 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11516 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11517 
11518 	dipif->ipif_flags = sipif->ipif_flags;
11519 	dipif->ipif_metric = sipif->ipif_metric;
11520 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11521 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11522 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11523 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11524 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11525 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11526 
11527 	/*
11528 	 * As per the comment atop the function, we assume that these sipif
11529 	 * fields will be changed before sipif is unlocked.
11530 	 */
11531 	dipif->ipif_seqid = sipif->ipif_seqid;
11532 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11533 }
11534 
11535 /*
11536  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11537  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11538  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11539  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11540  * down (i.e., no DAD), of the same type, and unlocked.
11541  */
11542 static void
11543 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11544 {
11545 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11546 	ipxop_t *ipx = ipsq->ipsq_xop;
11547 
11548 	ASSERT(sipif != dipif);
11549 	ASSERT(sipif != virgipif);
11550 
11551 	/*
11552 	 * Grab all of the locks that protect the ipif in a defined order.
11553 	 */
11554 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11555 
11556 	ipif_clone(sipif, dipif);
11557 	if (virgipif != NULL) {
11558 		ipif_clone(virgipif, sipif);
11559 		mi_free(virgipif);
11560 	}
11561 
11562 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11563 
11564 	/*
11565 	 * Transfer ownership of the current xop, if necessary.
11566 	 */
11567 	if (ipx->ipx_current_ipif == sipif) {
11568 		ASSERT(ipx->ipx_pending_ipif == NULL);
11569 		mutex_enter(&ipx->ipx_lock);
11570 		ipx->ipx_current_ipif = dipif;
11571 		mutex_exit(&ipx->ipx_lock);
11572 	}
11573 
11574 	if (virgipif == NULL)
11575 		mi_free(sipif);
11576 }
11577 
11578 /*
11579  * checks if:
11580  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11581  *	- logical interface is within the allowed range
11582  */
11583 static int
11584 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11585 {
11586 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11587 		return (ENAMETOOLONG);
11588 
11589 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11590 		return (ERANGE);
11591 	return (0);
11592 }
11593 
11594 /*
11595  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11596  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11597  * be inserted into the first space available in the list. The value of
11598  * ipif_id will then be set to the appropriate value for its position.
11599  */
11600 static int
11601 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11602 {
11603 	ill_t *ill;
11604 	ipif_t *tipif;
11605 	ipif_t **tipifp;
11606 	int id, err;
11607 	ip_stack_t	*ipst;
11608 
11609 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11610 	    IAM_WRITER_IPIF(ipif));
11611 
11612 	ill = ipif->ipif_ill;
11613 	ASSERT(ill != NULL);
11614 	ipst = ill->ill_ipst;
11615 
11616 	/*
11617 	 * In the case of lo0:0 we already hold the ill_g_lock.
11618 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11619 	 * ipif_insert.
11620 	 */
11621 	if (acquire_g_lock)
11622 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11623 	mutex_enter(&ill->ill_lock);
11624 	id = ipif->ipif_id;
11625 	tipifp = &(ill->ill_ipif);
11626 	if (id == -1) {	/* need to find a real id */
11627 		id = 0;
11628 		while ((tipif = *tipifp) != NULL) {
11629 			ASSERT(tipif->ipif_id >= id);
11630 			if (tipif->ipif_id != id)
11631 				break; /* non-consecutive id */
11632 			id++;
11633 			tipifp = &(tipif->ipif_next);
11634 		}
11635 		if ((err = is_lifname_valid(ill, id)) != 0) {
11636 			mutex_exit(&ill->ill_lock);
11637 			if (acquire_g_lock)
11638 				rw_exit(&ipst->ips_ill_g_lock);
11639 			return (err);
11640 		}
11641 		ipif->ipif_id = id; /* assign new id */
11642 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11643 		/* we have a real id; insert ipif in the right place */
11644 		while ((tipif = *tipifp) != NULL) {
11645 			ASSERT(tipif->ipif_id != id);
11646 			if (tipif->ipif_id > id)
11647 				break; /* found correct location */
11648 			tipifp = &(tipif->ipif_next);
11649 		}
11650 	} else {
11651 		mutex_exit(&ill->ill_lock);
11652 		if (acquire_g_lock)
11653 			rw_exit(&ipst->ips_ill_g_lock);
11654 		return (err);
11655 	}
11656 
11657 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11658 
11659 	ipif->ipif_next = tipif;
11660 	*tipifp = ipif;
11661 	mutex_exit(&ill->ill_lock);
11662 	if (acquire_g_lock)
11663 		rw_exit(&ipst->ips_ill_g_lock);
11664 
11665 	return (0);
11666 }
11667 
11668 static void
11669 ipif_remove(ipif_t *ipif)
11670 {
11671 	ipif_t	**ipifp;
11672 	ill_t	*ill = ipif->ipif_ill;
11673 
11674 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11675 
11676 	mutex_enter(&ill->ill_lock);
11677 	ipifp = &ill->ill_ipif;
11678 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11679 		if (*ipifp == ipif) {
11680 			*ipifp = ipif->ipif_next;
11681 			break;
11682 		}
11683 	}
11684 	mutex_exit(&ill->ill_lock);
11685 }
11686 
11687 /*
11688  * Allocate and initialize a new interface control structure.  (Always
11689  * called as writer.)
11690  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11691  * is not part of the global linked list of ills. ipif_seqid is unique
11692  * in the system and to preserve the uniqueness, it is assigned only
11693  * when ill becomes part of the global list. At that point ill will
11694  * have a name. If it doesn't get assigned here, it will get assigned
11695  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11696  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11697  * the interface flags or any other information from the DL_INFO_ACK for
11698  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11699  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11700  * second DL_INFO_ACK comes in from the driver.
11701  */
11702 static ipif_t *
11703 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11704     boolean_t insert, int *errorp)
11705 {
11706 	int err;
11707 	ipif_t	*ipif;
11708 	ip_stack_t *ipst = ill->ill_ipst;
11709 
11710 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11711 	    ill->ill_name, id, (void *)ill));
11712 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11713 
11714 	if (errorp != NULL)
11715 		*errorp = 0;
11716 
11717 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11718 		if (errorp != NULL)
11719 			*errorp = ENOMEM;
11720 		return (NULL);
11721 	}
11722 	*ipif = ipif_zero;	/* start clean */
11723 
11724 	ipif->ipif_ill = ill;
11725 	ipif->ipif_id = id;	/* could be -1 */
11726 	/*
11727 	 * Inherit the zoneid from the ill; for the shared stack instance
11728 	 * this is always the global zone
11729 	 */
11730 	ipif->ipif_zoneid = ill->ill_zoneid;
11731 
11732 	ipif->ipif_refcnt = 0;
11733 
11734 	if (insert) {
11735 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11736 			mi_free(ipif);
11737 			if (errorp != NULL)
11738 				*errorp = err;
11739 			return (NULL);
11740 		}
11741 		/* -1 id should have been replaced by real id */
11742 		id = ipif->ipif_id;
11743 		ASSERT(id >= 0);
11744 	}
11745 
11746 	if (ill->ill_name[0] != '\0')
11747 		ipif_assign_seqid(ipif);
11748 
11749 	/*
11750 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11751 	 * (which must not exist yet because the zeroth ipif is created once
11752 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11753 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11754 	 */
11755 	if (id == 0 && IS_IPMP(ill)) {
11756 		if (ipmp_illgrp_create(ill) == NULL) {
11757 			if (insert) {
11758 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11759 				ipif_remove(ipif);
11760 				rw_exit(&ipst->ips_ill_g_lock);
11761 			}
11762 			mi_free(ipif);
11763 			if (errorp != NULL)
11764 				*errorp = ENOMEM;
11765 			return (NULL);
11766 		}
11767 	}
11768 
11769 	/*
11770 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11771 	 * not up and can't be looked up until the ioctl completes and the
11772 	 * IPIF_CHANGING flag is cleared.
11773 	 */
11774 	mutex_enter(&ill->ill_lock);
11775 
11776 	ipif->ipif_ire_type = ire_type;
11777 
11778 	if (ipif->ipif_isv6) {
11779 		ill->ill_flags |= ILLF_IPV6;
11780 	} else {
11781 		ipaddr_t inaddr_any = INADDR_ANY;
11782 
11783 		ill->ill_flags |= ILLF_IPV4;
11784 
11785 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11786 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11787 		    &ipif->ipif_v6lcl_addr);
11788 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11789 		    &ipif->ipif_v6subnet);
11790 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11791 		    &ipif->ipif_v6net_mask);
11792 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11793 		    &ipif->ipif_v6brd_addr);
11794 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11795 		    &ipif->ipif_v6pp_dst_addr);
11796 	}
11797 
11798 	/*
11799 	 * Don't set the interface flags etc. now, will do it in
11800 	 * ip_ll_subnet_defaults.
11801 	 */
11802 	if (!initialize)
11803 		goto out;
11804 
11805 	/*
11806 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11807 	 * with no underlying interfaces (and thus an unknown broadcast
11808 	 * address length), but all interfaces that can be placed into an IPMP
11809 	 * group are required to be broadcast-capable.
11810 	 */
11811 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11812 		/*
11813 		 * Later detect lack of DLPI driver multicast capability by
11814 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11815 		 */
11816 		ill->ill_flags |= ILLF_MULTICAST;
11817 		if (!ipif->ipif_isv6)
11818 			ipif->ipif_flags |= IPIF_BROADCAST;
11819 	} else {
11820 		if (ill->ill_net_type != IRE_LOOPBACK) {
11821 			if (ipif->ipif_isv6)
11822 				/*
11823 				 * Note: xresolv interfaces will eventually need
11824 				 * NOARP set here as well, but that will require
11825 				 * those external resolvers to have some
11826 				 * knowledge of that flag and act appropriately.
11827 				 * Not to be changed at present.
11828 				 */
11829 				ill->ill_flags |= ILLF_NONUD;
11830 			else
11831 				ill->ill_flags |= ILLF_NOARP;
11832 		}
11833 		if (ill->ill_phys_addr_length == 0) {
11834 			if (IS_VNI(ill)) {
11835 				ipif->ipif_flags |= IPIF_NOXMIT;
11836 			} else {
11837 				/* pt-pt supports multicast. */
11838 				ill->ill_flags |= ILLF_MULTICAST;
11839 				if (ill->ill_net_type != IRE_LOOPBACK)
11840 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11841 			}
11842 		}
11843 	}
11844 out:
11845 	mutex_exit(&ill->ill_lock);
11846 	return (ipif);
11847 }
11848 
11849 /*
11850  * Remove the neighbor cache entries associated with this logical
11851  * interface.
11852  */
11853 int
11854 ipif_arp_down(ipif_t *ipif)
11855 {
11856 	ill_t	*ill = ipif->ipif_ill;
11857 	int	err = 0;
11858 
11859 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11860 	ASSERT(IAM_WRITER_IPIF(ipif));
11861 
11862 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11863 	    ill_t *, ill, ipif_t *, ipif);
11864 	ipif_nce_down(ipif);
11865 
11866 	/*
11867 	 * If this is the last ipif that is going down and there are no
11868 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11869 	 * clean up ARP completely.
11870 	 */
11871 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11872 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11873 		/*
11874 		 * If this was the last ipif on an IPMP interface, purge any
11875 		 * static ARP entries associated with it.
11876 		 */
11877 		if (IS_IPMP(ill))
11878 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11879 
11880 		/* UNBIND, DETACH */
11881 		err = arp_ll_down(ill);
11882 	}
11883 
11884 	return (err);
11885 }
11886 
11887 /*
11888  * Get the resolver set up for a new IP address.  (Always called as writer.)
11889  * Called both for IPv4 and IPv6 interfaces, though it only does some
11890  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11891  *
11892  * The enumerated value res_act tunes the behavior:
11893  * 	* Res_act_initial: set up all the resolver structures for a new
11894  *	  IP address.
11895  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11896  *	  ARP message in defense of the address.
11897  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11898  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11899  *
11900  * Returns zero on success, or an errno upon failure.
11901  */
11902 int
11903 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11904 {
11905 	ill_t		*ill = ipif->ipif_ill;
11906 	int		err;
11907 	boolean_t	was_dup;
11908 
11909 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11910 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11911 	ASSERT(IAM_WRITER_IPIF(ipif));
11912 
11913 	was_dup = B_FALSE;
11914 	if (res_act == Res_act_initial) {
11915 		ipif->ipif_addr_ready = 0;
11916 		/*
11917 		 * We're bringing an interface up here.  There's no way that we
11918 		 * should need to shut down ARP now.
11919 		 */
11920 		mutex_enter(&ill->ill_lock);
11921 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11922 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11923 			ill->ill_ipif_dup_count--;
11924 			was_dup = B_TRUE;
11925 		}
11926 		mutex_exit(&ill->ill_lock);
11927 	}
11928 	if (ipif->ipif_recovery_id != 0)
11929 		(void) untimeout(ipif->ipif_recovery_id);
11930 	ipif->ipif_recovery_id = 0;
11931 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11932 		ipif->ipif_addr_ready = 1;
11933 		return (0);
11934 	}
11935 	/* NDP will set the ipif_addr_ready flag when it's ready */
11936 	if (ill->ill_isv6)
11937 		return (0);
11938 
11939 	err = ipif_arp_up(ipif, res_act, was_dup);
11940 	return (err);
11941 }
11942 
11943 /*
11944  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11945  * when a link has just gone back up.
11946  */
11947 static void
11948 ipif_nce_start_dad(ipif_t *ipif)
11949 {
11950 	ncec_t *ncec;
11951 	ill_t *ill = ipif->ipif_ill;
11952 	boolean_t isv6 = ill->ill_isv6;
11953 
11954 	if (isv6) {
11955 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11956 		    &ipif->ipif_v6lcl_addr);
11957 	} else {
11958 		ipaddr_t v4addr;
11959 
11960 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11961 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11962 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11963 			/*
11964 			 * If we can't contact ARP for some reason,
11965 			 * that's not really a problem.  Just send
11966 			 * out the routing socket notification that
11967 			 * DAD completion would have done, and continue.
11968 			 */
11969 			ipif_mask_reply(ipif);
11970 			ipif_up_notify(ipif);
11971 			ipif->ipif_addr_ready = 1;
11972 			return;
11973 		}
11974 
11975 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11976 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11977 	}
11978 
11979 	if (ncec == NULL) {
11980 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11981 		    (void *)ipif));
11982 		return;
11983 	}
11984 	if (!nce_restart_dad(ncec)) {
11985 		/*
11986 		 * If we can't restart DAD for some reason, that's not really a
11987 		 * problem.  Just send out the routing socket notification that
11988 		 * DAD completion would have done, and continue.
11989 		 */
11990 		ipif_up_notify(ipif);
11991 		ipif->ipif_addr_ready = 1;
11992 	}
11993 	ncec_refrele(ncec);
11994 }
11995 
11996 /*
11997  * Restart duplicate address detection on all interfaces on the given ill.
11998  *
11999  * This is called when an interface transitions from down to up
12000  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12001  *
12002  * Note that since the underlying physical link has transitioned, we must cause
12003  * at least one routing socket message to be sent here, either via DAD
12004  * completion or just by default on the first ipif.  (If we don't do this, then
12005  * in.mpathd will see long delays when doing link-based failure recovery.)
12006  */
12007 void
12008 ill_restart_dad(ill_t *ill, boolean_t went_up)
12009 {
12010 	ipif_t *ipif;
12011 
12012 	if (ill == NULL)
12013 		return;
12014 
12015 	/*
12016 	 * If layer two doesn't support duplicate address detection, then just
12017 	 * send the routing socket message now and be done with it.
12018 	 */
12019 	if (!ill->ill_isv6 && arp_no_defense) {
12020 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12021 		return;
12022 	}
12023 
12024 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12025 		if (went_up) {
12026 
12027 			if (ipif->ipif_flags & IPIF_UP) {
12028 				ipif_nce_start_dad(ipif);
12029 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12030 				/*
12031 				 * kick off the bring-up process now.
12032 				 */
12033 				ipif_do_recovery(ipif);
12034 			} else {
12035 				/*
12036 				 * Unfortunately, the first ipif is "special"
12037 				 * and represents the underlying ill in the
12038 				 * routing socket messages.  Thus, when this
12039 				 * one ipif is down, we must still notify so
12040 				 * that the user knows the IFF_RUNNING status
12041 				 * change.  (If the first ipif is up, then
12042 				 * we'll handle eventual routing socket
12043 				 * notification via DAD completion.)
12044 				 */
12045 				if (ipif == ill->ill_ipif) {
12046 					ip_rts_ifmsg(ill->ill_ipif,
12047 					    RTSQ_DEFAULT);
12048 				}
12049 			}
12050 		} else {
12051 			/*
12052 			 * After link down, we'll need to send a new routing
12053 			 * message when the link comes back, so clear
12054 			 * ipif_addr_ready.
12055 			 */
12056 			ipif->ipif_addr_ready = 0;
12057 		}
12058 	}
12059 
12060 	/*
12061 	 * If we've torn down links, then notify the user right away.
12062 	 */
12063 	if (!went_up)
12064 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12065 }
12066 
12067 static void
12068 ipsq_delete(ipsq_t *ipsq)
12069 {
12070 	ipxop_t *ipx = ipsq->ipsq_xop;
12071 
12072 	ipsq->ipsq_ipst = NULL;
12073 	ASSERT(ipsq->ipsq_phyint == NULL);
12074 	ASSERT(ipsq->ipsq_xop != NULL);
12075 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12076 	ASSERT(ipx->ipx_pending_mp == NULL);
12077 	kmem_free(ipsq, sizeof (ipsq_t));
12078 }
12079 
12080 static int
12081 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12082 {
12083 	int err = 0;
12084 	ipif_t *ipif;
12085 
12086 	if (ill == NULL)
12087 		return (0);
12088 
12089 	ASSERT(IAM_WRITER_ILL(ill));
12090 	ill->ill_up_ipifs = B_TRUE;
12091 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12092 		if (ipif->ipif_was_up) {
12093 			if (!(ipif->ipif_flags & IPIF_UP))
12094 				err = ipif_up(ipif, q, mp);
12095 			ipif->ipif_was_up = B_FALSE;
12096 			if (err != 0) {
12097 				ASSERT(err == EINPROGRESS);
12098 				return (err);
12099 			}
12100 		}
12101 	}
12102 	ill->ill_up_ipifs = B_FALSE;
12103 	return (0);
12104 }
12105 
12106 /*
12107  * This function is called to bring up all the ipifs that were up before
12108  * bringing the ill down via ill_down_ipifs().
12109  */
12110 int
12111 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12112 {
12113 	int err;
12114 
12115 	ASSERT(IAM_WRITER_ILL(ill));
12116 
12117 	if (ill->ill_replumbing) {
12118 		ill->ill_replumbing = 0;
12119 		/*
12120 		 * Send down REPLUMB_DONE notification followed by the
12121 		 * BIND_REQ on the arp stream.
12122 		 */
12123 		if (!ill->ill_isv6)
12124 			arp_send_replumb_conf(ill);
12125 	}
12126 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12127 	if (err != 0)
12128 		return (err);
12129 
12130 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12131 }
12132 
12133 /*
12134  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12135  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12136  */
12137 static void
12138 ill_down_ipifs(ill_t *ill, boolean_t logical)
12139 {
12140 	ipif_t *ipif;
12141 
12142 	ASSERT(IAM_WRITER_ILL(ill));
12143 
12144 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12145 		/*
12146 		 * We go through the ipif_down logic even if the ipif
12147 		 * is already down, since routes can be added based
12148 		 * on down ipifs. Going through ipif_down once again
12149 		 * will delete any IREs created based on these routes.
12150 		 */
12151 		if (ipif->ipif_flags & IPIF_UP)
12152 			ipif->ipif_was_up = B_TRUE;
12153 
12154 		if (logical) {
12155 			(void) ipif_logical_down(ipif, NULL, NULL);
12156 			ipif_non_duplicate(ipif);
12157 			(void) ipif_down_tail(ipif);
12158 		} else {
12159 			(void) ipif_down(ipif, NULL, NULL);
12160 		}
12161 	}
12162 }
12163 
12164 /*
12165  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12166  * a look again at valid source addresses.
12167  * This should be called each time after the set of source addresses has been
12168  * changed.
12169  */
12170 void
12171 ip_update_source_selection(ip_stack_t *ipst)
12172 {
12173 	/* We skip past SRC_GENERATION_VERIFY */
12174 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12175 	    SRC_GENERATION_VERIFY)
12176 		atomic_add_32(&ipst->ips_src_generation, 1);
12177 }
12178 
12179 /*
12180  * Finish the group join started in ip_sioctl_groupname().
12181  */
12182 /* ARGSUSED */
12183 static void
12184 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12185 {
12186 	ill_t		*ill = q->q_ptr;
12187 	phyint_t	*phyi = ill->ill_phyint;
12188 	ipmp_grp_t	*grp = phyi->phyint_grp;
12189 	ip_stack_t	*ipst = ill->ill_ipst;
12190 
12191 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12192 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12193 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12194 
12195 	if (phyi->phyint_illv4 != NULL) {
12196 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12197 		VERIFY(grp->gr_pendv4-- > 0);
12198 		rw_exit(&ipst->ips_ipmp_lock);
12199 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12200 	}
12201 	if (phyi->phyint_illv6 != NULL) {
12202 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12203 		VERIFY(grp->gr_pendv6-- > 0);
12204 		rw_exit(&ipst->ips_ipmp_lock);
12205 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12206 	}
12207 	freemsg(mp);
12208 }
12209 
12210 /*
12211  * Process an SIOCSLIFGROUPNAME request.
12212  */
12213 /* ARGSUSED */
12214 int
12215 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12216     ip_ioctl_cmd_t *ipip, void *ifreq)
12217 {
12218 	struct lifreq	*lifr = ifreq;
12219 	ill_t		*ill = ipif->ipif_ill;
12220 	ip_stack_t	*ipst = ill->ill_ipst;
12221 	phyint_t	*phyi = ill->ill_phyint;
12222 	ipmp_grp_t	*grp = phyi->phyint_grp;
12223 	mblk_t		*ipsq_mp;
12224 	int		err = 0;
12225 
12226 	/*
12227 	 * Note that phyint_grp can only change here, where we're exclusive.
12228 	 */
12229 	ASSERT(IAM_WRITER_ILL(ill));
12230 
12231 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12232 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12233 		return (EINVAL);
12234 
12235 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12236 
12237 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12238 
12239 	/*
12240 	 * If the name hasn't changed, there's nothing to do.
12241 	 */
12242 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12243 		goto unlock;
12244 
12245 	/*
12246 	 * Handle requests to rename an IPMP meta-interface.
12247 	 *
12248 	 * Note that creation of the IPMP meta-interface is handled in
12249 	 * userland through the standard plumbing sequence.  As part of the
12250 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12251 	 * the name of the interface (see ipif_set_values_tail()).
12252 	 */
12253 	if (IS_IPMP(ill)) {
12254 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12255 		goto unlock;
12256 	}
12257 
12258 	/*
12259 	 * Handle requests to add or remove an IP interface from a group.
12260 	 */
12261 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12262 		/*
12263 		 * Moves are handled by first removing the interface from
12264 		 * its existing group, and then adding it to another group.
12265 		 * So, fail if it's already in a group.
12266 		 */
12267 		if (IS_UNDER_IPMP(ill)) {
12268 			err = EALREADY;
12269 			goto unlock;
12270 		}
12271 
12272 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12273 		if (grp == NULL) {
12274 			err = ENOENT;
12275 			goto unlock;
12276 		}
12277 
12278 		/*
12279 		 * Check if the phyint and its ills are suitable for
12280 		 * inclusion into the group.
12281 		 */
12282 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12283 			goto unlock;
12284 
12285 		/*
12286 		 * Checks pass; join the group, and enqueue the remaining
12287 		 * illgrp joins for when we've become part of the group xop
12288 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12289 		 * requires an mblk_t to scribble on, and since `mp' will be
12290 		 * freed as part of completing the ioctl, allocate another.
12291 		 */
12292 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12293 			err = ENOMEM;
12294 			goto unlock;
12295 		}
12296 
12297 		/*
12298 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12299 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12300 		 * before ip_join_illgrps() is called back.  See the comments
12301 		 * in ip_sioctl_plink_ipmp() for more.
12302 		 */
12303 		if (phyi->phyint_illv4 != NULL)
12304 			grp->gr_pendv4++;
12305 		if (phyi->phyint_illv6 != NULL)
12306 			grp->gr_pendv6++;
12307 
12308 		rw_exit(&ipst->ips_ipmp_lock);
12309 
12310 		ipmp_phyint_join_grp(phyi, grp);
12311 		ill_refhold(ill);
12312 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12313 		    SWITCH_OP, B_FALSE);
12314 		return (0);
12315 	} else {
12316 		/*
12317 		 * Request to remove the interface from a group.  If the
12318 		 * interface is not in a group, this trivially succeeds.
12319 		 */
12320 		rw_exit(&ipst->ips_ipmp_lock);
12321 		if (IS_UNDER_IPMP(ill))
12322 			ipmp_phyint_leave_grp(phyi);
12323 		return (0);
12324 	}
12325 unlock:
12326 	rw_exit(&ipst->ips_ipmp_lock);
12327 	return (err);
12328 }
12329 
12330 /*
12331  * Process an SIOCGLIFBINDING request.
12332  */
12333 /* ARGSUSED */
12334 int
12335 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12336     ip_ioctl_cmd_t *ipip, void *ifreq)
12337 {
12338 	ill_t		*ill;
12339 	struct lifreq	*lifr = ifreq;
12340 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12341 
12342 	if (!IS_IPMP(ipif->ipif_ill))
12343 		return (EINVAL);
12344 
12345 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12346 	if ((ill = ipif->ipif_bound_ill) == NULL)
12347 		lifr->lifr_binding[0] = '\0';
12348 	else
12349 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12350 	rw_exit(&ipst->ips_ipmp_lock);
12351 	return (0);
12352 }
12353 
12354 /*
12355  * Process an SIOCGLIFGROUPNAME request.
12356  */
12357 /* ARGSUSED */
12358 int
12359 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12360     ip_ioctl_cmd_t *ipip, void *ifreq)
12361 {
12362 	ipmp_grp_t	*grp;
12363 	struct lifreq	*lifr = ifreq;
12364 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12365 
12366 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12367 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12368 		lifr->lifr_groupname[0] = '\0';
12369 	else
12370 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12371 	rw_exit(&ipst->ips_ipmp_lock);
12372 	return (0);
12373 }
12374 
12375 /*
12376  * Process an SIOCGLIFGROUPINFO request.
12377  */
12378 /* ARGSUSED */
12379 int
12380 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12381     ip_ioctl_cmd_t *ipip, void *dummy)
12382 {
12383 	ipmp_grp_t	*grp;
12384 	lifgroupinfo_t	*lifgr;
12385 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12386 
12387 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12388 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12389 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12390 
12391 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12392 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12393 		rw_exit(&ipst->ips_ipmp_lock);
12394 		return (ENOENT);
12395 	}
12396 	ipmp_grp_info(grp, lifgr);
12397 	rw_exit(&ipst->ips_ipmp_lock);
12398 	return (0);
12399 }
12400 
12401 static void
12402 ill_dl_down(ill_t *ill)
12403 {
12404 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12405 
12406 	/*
12407 	 * The ill is down; unbind but stay attached since we're still
12408 	 * associated with a PPA. If we have negotiated DLPI capabilites
12409 	 * with the data link service provider (IDS_OK) then reset them.
12410 	 * The interval between unbinding and rebinding is potentially
12411 	 * unbounded hence we cannot assume things will be the same.
12412 	 * The DLPI capabilities will be probed again when the data link
12413 	 * is brought up.
12414 	 */
12415 	mblk_t	*mp = ill->ill_unbind_mp;
12416 
12417 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12418 
12419 	if (!ill->ill_replumbing) {
12420 		/* Free all ilms for this ill */
12421 		update_conn_ill(ill, ill->ill_ipst);
12422 	} else {
12423 		ill_leave_multicast(ill);
12424 	}
12425 
12426 	ill->ill_unbind_mp = NULL;
12427 	if (mp != NULL) {
12428 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12429 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12430 		    ill->ill_name));
12431 		mutex_enter(&ill->ill_lock);
12432 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12433 		mutex_exit(&ill->ill_lock);
12434 		/*
12435 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12436 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12437 		 * ill_capability_dld_disable disable rightaway. If this is not
12438 		 * an unplumb operation then the disable happens on receipt of
12439 		 * the capab ack via ip_rput_dlpi_writer ->
12440 		 * ill_capability_ack_thr. In both cases the order of
12441 		 * the operations seen by DLD is capability disable followed
12442 		 * by DL_UNBIND. Also the DLD capability disable needs a
12443 		 * cv_wait'able context.
12444 		 */
12445 		if (ill->ill_state_flags & ILL_CONDEMNED)
12446 			ill_capability_dld_disable(ill);
12447 		ill_capability_reset(ill, B_FALSE);
12448 		ill_dlpi_send(ill, mp);
12449 	}
12450 	mutex_enter(&ill->ill_lock);
12451 	ill->ill_dl_up = 0;
12452 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12453 	mutex_exit(&ill->ill_lock);
12454 }
12455 
12456 void
12457 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12458 {
12459 	union DL_primitives *dlp;
12460 	t_uscalar_t prim;
12461 	boolean_t waitack = B_FALSE;
12462 
12463 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12464 
12465 	dlp = (union DL_primitives *)mp->b_rptr;
12466 	prim = dlp->dl_primitive;
12467 
12468 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12469 	    dl_primstr(prim), prim, ill->ill_name));
12470 
12471 	switch (prim) {
12472 	case DL_PHYS_ADDR_REQ:
12473 	{
12474 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12475 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12476 		break;
12477 	}
12478 	case DL_BIND_REQ:
12479 		mutex_enter(&ill->ill_lock);
12480 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12481 		mutex_exit(&ill->ill_lock);
12482 		break;
12483 	}
12484 
12485 	/*
12486 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12487 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12488 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12489 	 */
12490 	mutex_enter(&ill->ill_lock);
12491 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12492 	    (prim == DL_UNBIND_REQ)) {
12493 		ill->ill_dlpi_pending = prim;
12494 		waitack = B_TRUE;
12495 	}
12496 
12497 	mutex_exit(&ill->ill_lock);
12498 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12499 	    char *, dl_primstr(prim), ill_t *, ill);
12500 	putnext(ill->ill_wq, mp);
12501 
12502 	/*
12503 	 * There is no ack for DL_NOTIFY_CONF messages
12504 	 */
12505 	if (waitack && prim == DL_NOTIFY_CONF)
12506 		ill_dlpi_done(ill, prim);
12507 }
12508 
12509 /*
12510  * Helper function for ill_dlpi_send().
12511  */
12512 /* ARGSUSED */
12513 static void
12514 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12515 {
12516 	ill_dlpi_send(q->q_ptr, mp);
12517 }
12518 
12519 /*
12520  * Send a DLPI control message to the driver but make sure there
12521  * is only one outstanding message. Uses ill_dlpi_pending to tell
12522  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12523  * when an ACK or a NAK is received to process the next queued message.
12524  */
12525 void
12526 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12527 {
12528 	mblk_t **mpp;
12529 
12530 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12531 
12532 	/*
12533 	 * To ensure that any DLPI requests for current exclusive operation
12534 	 * are always completely sent before any DLPI messages for other
12535 	 * operations, require writer access before enqueuing.
12536 	 */
12537 	if (!IAM_WRITER_ILL(ill)) {
12538 		ill_refhold(ill);
12539 		/* qwriter_ip() does the ill_refrele() */
12540 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12541 		    NEW_OP, B_TRUE);
12542 		return;
12543 	}
12544 
12545 	mutex_enter(&ill->ill_lock);
12546 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12547 		/* Must queue message. Tail insertion */
12548 		mpp = &ill->ill_dlpi_deferred;
12549 		while (*mpp != NULL)
12550 			mpp = &((*mpp)->b_next);
12551 
12552 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12553 		    "while %s pending\n", ill->ill_name,
12554 		    dl_primstr(ill->ill_dlpi_pending)));
12555 
12556 		*mpp = mp;
12557 		mutex_exit(&ill->ill_lock);
12558 		return;
12559 	}
12560 	mutex_exit(&ill->ill_lock);
12561 	ill_dlpi_dispatch(ill, mp);
12562 }
12563 
12564 void
12565 ill_capability_send(ill_t *ill, mblk_t *mp)
12566 {
12567 	ill->ill_capab_pending_cnt++;
12568 	ill_dlpi_send(ill, mp);
12569 }
12570 
12571 void
12572 ill_capability_done(ill_t *ill)
12573 {
12574 	ASSERT(ill->ill_capab_pending_cnt != 0);
12575 
12576 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12577 
12578 	ill->ill_capab_pending_cnt--;
12579 	if (ill->ill_capab_pending_cnt == 0 &&
12580 	    ill->ill_dlpi_capab_state == IDCS_OK)
12581 		ill_capability_reset_alloc(ill);
12582 }
12583 
12584 /*
12585  * Send all deferred DLPI messages without waiting for their ACKs.
12586  */
12587 void
12588 ill_dlpi_send_deferred(ill_t *ill)
12589 {
12590 	mblk_t *mp, *nextmp;
12591 
12592 	/*
12593 	 * Clear ill_dlpi_pending so that the message is not queued in
12594 	 * ill_dlpi_send().
12595 	 */
12596 	mutex_enter(&ill->ill_lock);
12597 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12598 	mp = ill->ill_dlpi_deferred;
12599 	ill->ill_dlpi_deferred = NULL;
12600 	mutex_exit(&ill->ill_lock);
12601 
12602 	for (; mp != NULL; mp = nextmp) {
12603 		nextmp = mp->b_next;
12604 		mp->b_next = NULL;
12605 		ill_dlpi_send(ill, mp);
12606 	}
12607 }
12608 
12609 /*
12610  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12611  * or M_HANGUP
12612  */
12613 static void
12614 ill_dlpi_clear_deferred(ill_t *ill)
12615 {
12616 	mblk_t	*mp, *nextmp;
12617 
12618 	mutex_enter(&ill->ill_lock);
12619 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12620 	mp = ill->ill_dlpi_deferred;
12621 	ill->ill_dlpi_deferred = NULL;
12622 	mutex_exit(&ill->ill_lock);
12623 
12624 	for (; mp != NULL; mp = nextmp) {
12625 		nextmp = mp->b_next;
12626 		inet_freemsg(mp);
12627 	}
12628 }
12629 
12630 /*
12631  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12632  */
12633 boolean_t
12634 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12635 {
12636 	t_uscalar_t pending;
12637 
12638 	mutex_enter(&ill->ill_lock);
12639 	if (ill->ill_dlpi_pending == prim) {
12640 		mutex_exit(&ill->ill_lock);
12641 		return (B_TRUE);
12642 	}
12643 
12644 	/*
12645 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12646 	 * without waiting, so don't print any warnings in that case.
12647 	 */
12648 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12649 		mutex_exit(&ill->ill_lock);
12650 		return (B_FALSE);
12651 	}
12652 	pending = ill->ill_dlpi_pending;
12653 	mutex_exit(&ill->ill_lock);
12654 
12655 	if (pending == DL_PRIM_INVAL) {
12656 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12657 		    "received unsolicited ack for %s on %s\n",
12658 		    dl_primstr(prim), ill->ill_name);
12659 	} else {
12660 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12661 		    "received unexpected ack for %s on %s (expecting %s)\n",
12662 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12663 	}
12664 	return (B_FALSE);
12665 }
12666 
12667 /*
12668  * Complete the current DLPI operation associated with `prim' on `ill' and
12669  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12670  * operations and the ill's current exclusive IPSQ operation has finished
12671  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12672  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12673  * the comments above ipsq_current_finish() for details.
12674  */
12675 void
12676 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12677 {
12678 	mblk_t *mp;
12679 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12680 	ipxop_t *ipx = ipsq->ipsq_xop;
12681 
12682 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12683 	mutex_enter(&ill->ill_lock);
12684 
12685 	ASSERT(prim != DL_PRIM_INVAL);
12686 	ASSERT(ill->ill_dlpi_pending == prim);
12687 
12688 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12689 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12690 
12691 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12692 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12693 		if (ipx->ipx_current_done) {
12694 			mutex_enter(&ipx->ipx_lock);
12695 			ipx->ipx_current_ipif = NULL;
12696 			mutex_exit(&ipx->ipx_lock);
12697 		}
12698 		cv_signal(&ill->ill_cv);
12699 		mutex_exit(&ill->ill_lock);
12700 		return;
12701 	}
12702 
12703 	ill->ill_dlpi_deferred = mp->b_next;
12704 	mp->b_next = NULL;
12705 	mutex_exit(&ill->ill_lock);
12706 
12707 	ill_dlpi_dispatch(ill, mp);
12708 }
12709 
12710 /*
12711  * Queue a (multicast) DLPI control message to be sent to the driver by
12712  * later calling ill_dlpi_send_queued.
12713  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12714  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12715  * for the same group to race.
12716  * We send DLPI control messages in order using ill_lock.
12717  * For IPMP we should be called on the cast_ill.
12718  */
12719 void
12720 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12721 {
12722 	mblk_t **mpp;
12723 
12724 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12725 
12726 	mutex_enter(&ill->ill_lock);
12727 	/* Must queue message. Tail insertion */
12728 	mpp = &ill->ill_dlpi_deferred;
12729 	while (*mpp != NULL)
12730 		mpp = &((*mpp)->b_next);
12731 
12732 	*mpp = mp;
12733 	mutex_exit(&ill->ill_lock);
12734 }
12735 
12736 /*
12737  * Send the messages that were queued. Make sure there is only
12738  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12739  * when an ACK or a NAK is received to process the next queued message.
12740  * For IPMP we are called on the upper ill, but when send what is queued
12741  * on the cast_ill.
12742  */
12743 void
12744 ill_dlpi_send_queued(ill_t *ill)
12745 {
12746 	mblk_t	*mp;
12747 	union DL_primitives *dlp;
12748 	t_uscalar_t prim;
12749 	ill_t *release_ill = NULL;
12750 
12751 	if (IS_IPMP(ill)) {
12752 		/* On the upper IPMP ill. */
12753 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12754 		if (release_ill == NULL) {
12755 			/* Avoid ever sending anything down to the ipmpstub */
12756 			return;
12757 		}
12758 		ill = release_ill;
12759 	}
12760 	mutex_enter(&ill->ill_lock);
12761 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12762 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12763 			/* Can't send. Somebody else will send it */
12764 			mutex_exit(&ill->ill_lock);
12765 			goto done;
12766 		}
12767 		ill->ill_dlpi_deferred = mp->b_next;
12768 		mp->b_next = NULL;
12769 		if (!ill->ill_dl_up) {
12770 			/*
12771 			 * Nobody there. All multicast addresses will be
12772 			 * re-joined when we get the DL_BIND_ACK bringing the
12773 			 * interface up.
12774 			 */
12775 			freemsg(mp);
12776 			continue;
12777 		}
12778 		dlp = (union DL_primitives *)mp->b_rptr;
12779 		prim = dlp->dl_primitive;
12780 
12781 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12782 		    (prim == DL_UNBIND_REQ)) {
12783 			ill->ill_dlpi_pending = prim;
12784 		}
12785 		mutex_exit(&ill->ill_lock);
12786 
12787 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12788 		    char *, dl_primstr(prim), ill_t *, ill);
12789 		putnext(ill->ill_wq, mp);
12790 		mutex_enter(&ill->ill_lock);
12791 	}
12792 	mutex_exit(&ill->ill_lock);
12793 done:
12794 	if (release_ill != NULL)
12795 		ill_refrele(release_ill);
12796 }
12797 
12798 /*
12799  * Queue an IP (IGMP/MLD) message to be sent by IP from
12800  * ill_mcast_send_queued
12801  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12802  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12803  * group to race.
12804  * We send them in order using ill_lock.
12805  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12806  */
12807 void
12808 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12809 {
12810 	mblk_t **mpp;
12811 	ill_t *release_ill = NULL;
12812 
12813 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12814 
12815 	if (IS_IPMP(ill)) {
12816 		/* On the upper IPMP ill. */
12817 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12818 		if (release_ill == NULL) {
12819 			/* Discard instead of queuing for the ipmp interface */
12820 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12821 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12822 			    mp, ill);
12823 			freemsg(mp);
12824 			return;
12825 		}
12826 		ill = release_ill;
12827 	}
12828 
12829 	mutex_enter(&ill->ill_lock);
12830 	/* Must queue message. Tail insertion */
12831 	mpp = &ill->ill_mcast_deferred;
12832 	while (*mpp != NULL)
12833 		mpp = &((*mpp)->b_next);
12834 
12835 	*mpp = mp;
12836 	mutex_exit(&ill->ill_lock);
12837 	if (release_ill != NULL)
12838 		ill_refrele(release_ill);
12839 }
12840 
12841 /*
12842  * Send the IP packets that were queued by ill_mcast_queue.
12843  * These are IGMP/MLD packets.
12844  *
12845  * For IPMP we are called on the upper ill, but when send what is queued
12846  * on the cast_ill.
12847  *
12848  * Request loopback of the report if we are acting as a multicast
12849  * router, so that the process-level routing demon can hear it.
12850  * This will run multiple times for the same group if there are members
12851  * on the same group for multiple ipif's on the same ill. The
12852  * igmp_input/mld_input code will suppress this due to the loopback thus we
12853  * always loopback membership report.
12854  *
12855  * We also need to make sure that this does not get load balanced
12856  * by IPMP. We do this by passing an ill to ip_output_simple.
12857  */
12858 void
12859 ill_mcast_send_queued(ill_t *ill)
12860 {
12861 	mblk_t	*mp;
12862 	ip_xmit_attr_t ixas;
12863 	ill_t *release_ill = NULL;
12864 
12865 	if (IS_IPMP(ill)) {
12866 		/* On the upper IPMP ill. */
12867 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12868 		if (release_ill == NULL) {
12869 			/*
12870 			 * We should have no messages on the ipmp interface
12871 			 * but no point in trying to send them.
12872 			 */
12873 			return;
12874 		}
12875 		ill = release_ill;
12876 	}
12877 	bzero(&ixas, sizeof (ixas));
12878 	ixas.ixa_zoneid = ALL_ZONES;
12879 	ixas.ixa_cred = kcred;
12880 	ixas.ixa_cpid = NOPID;
12881 	ixas.ixa_tsl = NULL;
12882 	/*
12883 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12884 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12885 	 * That is necessary to handle IGMP/MLD snooping switches.
12886 	 */
12887 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12888 	ixas.ixa_ipst = ill->ill_ipst;
12889 
12890 	mutex_enter(&ill->ill_lock);
12891 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12892 		ill->ill_mcast_deferred = mp->b_next;
12893 		mp->b_next = NULL;
12894 		if (!ill->ill_dl_up) {
12895 			/*
12896 			 * Nobody there. Just drop the ip packets.
12897 			 * IGMP/MLD will resend later, if this is a replumb.
12898 			 */
12899 			freemsg(mp);
12900 			continue;
12901 		}
12902 		mutex_enter(&ill->ill_phyint->phyint_lock);
12903 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12904 			/*
12905 			 * When the ill is getting deactivated, we only want to
12906 			 * send the DLPI messages, so drop IGMP/MLD packets.
12907 			 * DLPI messages are handled by ill_dlpi_send_queued()
12908 			 */
12909 			mutex_exit(&ill->ill_phyint->phyint_lock);
12910 			freemsg(mp);
12911 			continue;
12912 		}
12913 		mutex_exit(&ill->ill_phyint->phyint_lock);
12914 		mutex_exit(&ill->ill_lock);
12915 
12916 		/* Check whether we are sending IPv4 or IPv6. */
12917 		if (ill->ill_isv6) {
12918 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12919 
12920 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12921 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12922 		} else {
12923 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12924 
12925 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12926 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12927 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12928 		}
12929 
12930 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12931 		(void) ip_output_simple(mp, &ixas);
12932 		ixa_cleanup(&ixas);
12933 
12934 		mutex_enter(&ill->ill_lock);
12935 	}
12936 	mutex_exit(&ill->ill_lock);
12937 
12938 done:
12939 	if (release_ill != NULL)
12940 		ill_refrele(release_ill);
12941 }
12942 
12943 /*
12944  * Take down a specific interface, but don't lose any information about it.
12945  * (Always called as writer.)
12946  * This function goes through the down sequence even if the interface is
12947  * already down. There are 2 reasons.
12948  * a. Currently we permit interface routes that depend on down interfaces
12949  *    to be added. This behaviour itself is questionable. However it appears
12950  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12951  *    time. We go thru the cleanup in order to remove these routes.
12952  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12953  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12954  *    down, but we need to cleanup i.e. do ill_dl_down and
12955  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12956  *
12957  * IP-MT notes:
12958  *
12959  * Model of reference to interfaces.
12960  *
12961  * The following members in ipif_t track references to the ipif.
12962  *	int     ipif_refcnt;    Active reference count
12963  *
12964  * The following members in ill_t track references to the ill.
12965  *	int             ill_refcnt;     active refcnt
12966  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12967  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12968  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12969  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12970  *
12971  * Reference to an ipif or ill can be obtained in any of the following ways.
12972  *
12973  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12974  * Pointers to ipif / ill from other data structures viz ire and conn.
12975  * Implicit reference to the ipif / ill by holding a reference to the ire.
12976  *
12977  * The ipif/ill lookup functions return a reference held ipif / ill.
12978  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12979  * This is a purely dynamic reference count associated with threads holding
12980  * references to the ipif / ill. Pointers from other structures do not
12981  * count towards this reference count.
12982  *
12983  * ill_ire_cnt is the number of ire's associated with the
12984  * ill. This is incremented whenever a new ire is created referencing the
12985  * ill. This is done atomically inside ire_add_v[46] where the ire is
12986  * actually added to the ire hash table. The count is decremented in
12987  * ire_inactive where the ire is destroyed.
12988  *
12989  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12990  * This is incremented atomically in
12991  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12992  * table. Similarly it is decremented in ncec_inactive() where the ncec
12993  * is destroyed.
12994  *
12995  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12996  * incremented atomically in nce_add() where the nce is actually added to the
12997  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12998  * is destroyed.
12999  *
13000  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13001  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13002  *
13003  * Flow of ioctls involving interface down/up
13004  *
13005  * The following is the sequence of an attempt to set some critical flags on an
13006  * up interface.
13007  * ip_sioctl_flags
13008  * ipif_down
13009  * wait for ipif to be quiescent
13010  * ipif_down_tail
13011  * ip_sioctl_flags_tail
13012  *
13013  * All set ioctls that involve down/up sequence would have a skeleton similar
13014  * to the above. All the *tail functions are called after the refcounts have
13015  * dropped to the appropriate values.
13016  *
13017  * SIOC ioctls during the IPIF_CHANGING interval.
13018  *
13019  * Threads handling SIOC set ioctls serialize on the squeue, but this
13020  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13021  * steps of internal changes to the state, some of which are visible in
13022  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13023  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13024  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13025  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13026  * the current exclusive operation completes. The IPIF_CHANGING check
13027  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13028  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13029  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13030  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13031  * until we release the ipsq_lock, even though the ill/ipif state flags
13032  * can change after we drop the ill_lock.
13033  */
13034 int
13035 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13036 {
13037 	ill_t		*ill = ipif->ipif_ill;
13038 	conn_t		*connp;
13039 	boolean_t	success;
13040 	boolean_t	ipif_was_up = B_FALSE;
13041 	ip_stack_t	*ipst = ill->ill_ipst;
13042 
13043 	ASSERT(IAM_WRITER_IPIF(ipif));
13044 
13045 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13046 
13047 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13048 	    ill_t *, ill, ipif_t *, ipif);
13049 
13050 	if (ipif->ipif_flags & IPIF_UP) {
13051 		mutex_enter(&ill->ill_lock);
13052 		ipif->ipif_flags &= ~IPIF_UP;
13053 		ASSERT(ill->ill_ipif_up_count > 0);
13054 		--ill->ill_ipif_up_count;
13055 		mutex_exit(&ill->ill_lock);
13056 		ipif_was_up = B_TRUE;
13057 		/* Update status in SCTP's list */
13058 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13059 		ill_nic_event_dispatch(ipif->ipif_ill,
13060 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13061 	}
13062 
13063 	/*
13064 	 * Blow away memberships we established in ipif_multicast_up().
13065 	 */
13066 	ipif_multicast_down(ipif);
13067 
13068 	/*
13069 	 * Remove from the mapping for __sin6_src_id. We insert only
13070 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13071 	 * stored as mapped addresses, we need to check for mapped
13072 	 * INADDR_ANY also.
13073 	 */
13074 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13075 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13076 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13077 		int err;
13078 
13079 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13080 		    ipif->ipif_zoneid, ipst);
13081 		if (err != 0) {
13082 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13083 		}
13084 	}
13085 
13086 	if (ipif_was_up) {
13087 		/* only delete if we'd added ire's before */
13088 		if (ipif->ipif_isv6)
13089 			ipif_delete_ires_v6(ipif);
13090 		else
13091 			ipif_delete_ires_v4(ipif);
13092 	}
13093 
13094 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13095 		/*
13096 		 * Since the interface is now down, it may have just become
13097 		 * inactive.  Note that this needs to be done even for a
13098 		 * lll_logical_down(), or ARP entries will not get correctly
13099 		 * restored when the interface comes back up.
13100 		 */
13101 		if (IS_UNDER_IPMP(ill))
13102 			ipmp_ill_refresh_active(ill);
13103 	}
13104 
13105 	/*
13106 	 * neighbor-discovery or arp entries for this interface. The ipif
13107 	 * has to be quiesced, so we walk all the nce's and delete those
13108 	 * that point at the ipif->ipif_ill. At the same time, we also
13109 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13110 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13111 	 * that for ipif_down_tail()
13112 	 */
13113 	ipif_nce_down(ipif);
13114 
13115 	/*
13116 	 * If this is the last ipif on the ill, we also need to remove
13117 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13118 	 * never succeed.
13119 	 */
13120 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13121 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13122 
13123 	/*
13124 	 * Walk all CONNs that can have a reference on an ire for this
13125 	 * ipif (we actually walk all that now have stale references).
13126 	 */
13127 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13128 
13129 	/*
13130 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13131 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13132 	 * and ill_delete -> ipif_free -> ipif_down
13133 	 */
13134 	if (mp == NULL) {
13135 		ASSERT(q == NULL);
13136 		return (0);
13137 	}
13138 
13139 	if (CONN_Q(q)) {
13140 		connp = Q_TO_CONN(q);
13141 		mutex_enter(&connp->conn_lock);
13142 	} else {
13143 		connp = NULL;
13144 	}
13145 	mutex_enter(&ill->ill_lock);
13146 	/*
13147 	 * Are there any ire's pointing to this ipif that are still active ?
13148 	 * If this is the last ipif going down, are there any ire's pointing
13149 	 * to this ill that are still active ?
13150 	 */
13151 	if (ipif_is_quiescent(ipif)) {
13152 		mutex_exit(&ill->ill_lock);
13153 		if (connp != NULL)
13154 			mutex_exit(&connp->conn_lock);
13155 		return (0);
13156 	}
13157 
13158 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13159 	    ill->ill_name, (void *)ill));
13160 	/*
13161 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13162 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13163 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13164 	 */
13165 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13166 	if (!success) {
13167 		/* The conn is closing. So just return */
13168 		ASSERT(connp != NULL);
13169 		mutex_exit(&ill->ill_lock);
13170 		mutex_exit(&connp->conn_lock);
13171 		return (EINTR);
13172 	}
13173 
13174 	mutex_exit(&ill->ill_lock);
13175 	if (connp != NULL)
13176 		mutex_exit(&connp->conn_lock);
13177 	return (EINPROGRESS);
13178 }
13179 
13180 int
13181 ipif_down_tail(ipif_t *ipif)
13182 {
13183 	ill_t	*ill = ipif->ipif_ill;
13184 	int	err = 0;
13185 
13186 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13187 	    ill_t *, ill, ipif_t *, ipif);
13188 
13189 	/*
13190 	 * Skip any loopback interface (null wq).
13191 	 * If this is the last logical interface on the ill
13192 	 * have ill_dl_down tell the driver we are gone (unbind)
13193 	 * Note that lun 0 can ipif_down even though
13194 	 * there are other logical units that are up.
13195 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13196 	 */
13197 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13198 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13199 	    ill->ill_dl_up) {
13200 		ill_dl_down(ill);
13201 	}
13202 	if (!ipif->ipif_isv6)
13203 		err = ipif_arp_down(ipif);
13204 
13205 	ill->ill_logical_down = 0;
13206 
13207 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13208 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13209 	return (err);
13210 }
13211 
13212 /*
13213  * Bring interface logically down without bringing the physical interface
13214  * down e.g. when the netmask is changed. This avoids long lasting link
13215  * negotiations between an ethernet interface and a certain switches.
13216  */
13217 static int
13218 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13219 {
13220 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13221 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13222 
13223 	/*
13224 	 * The ill_logical_down flag is a transient flag. It is set here
13225 	 * and is cleared once the down has completed in ipif_down_tail.
13226 	 * This flag does not indicate whether the ill stream is in the
13227 	 * DL_BOUND state with the driver. Instead this flag is used by
13228 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13229 	 * the driver. The state of the ill stream i.e. whether it is
13230 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13231 	 */
13232 	ipif->ipif_ill->ill_logical_down = 1;
13233 	return (ipif_down(ipif, q, mp));
13234 }
13235 
13236 /*
13237  * Initiate deallocate of an IPIF. Always called as writer. Called by
13238  * ill_delete or ip_sioctl_removeif.
13239  */
13240 static void
13241 ipif_free(ipif_t *ipif)
13242 {
13243 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13244 
13245 	ASSERT(IAM_WRITER_IPIF(ipif));
13246 
13247 	if (ipif->ipif_recovery_id != 0)
13248 		(void) untimeout(ipif->ipif_recovery_id);
13249 	ipif->ipif_recovery_id = 0;
13250 
13251 	/*
13252 	 * Take down the interface. We can be called either from ill_delete
13253 	 * or from ip_sioctl_removeif.
13254 	 */
13255 	(void) ipif_down(ipif, NULL, NULL);
13256 
13257 	/*
13258 	 * Now that the interface is down, there's no chance it can still
13259 	 * become a duplicate.  Cancel any timer that may have been set while
13260 	 * tearing down.
13261 	 */
13262 	if (ipif->ipif_recovery_id != 0)
13263 		(void) untimeout(ipif->ipif_recovery_id);
13264 	ipif->ipif_recovery_id = 0;
13265 
13266 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13267 	/* Remove pointers to this ill in the multicast routing tables */
13268 	reset_mrt_vif_ipif(ipif);
13269 	/* If necessary, clear the cached source ipif rotor. */
13270 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13271 		ipif->ipif_ill->ill_src_ipif = NULL;
13272 	rw_exit(&ipst->ips_ill_g_lock);
13273 }
13274 
13275 static void
13276 ipif_free_tail(ipif_t *ipif)
13277 {
13278 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13279 
13280 	/*
13281 	 * Need to hold both ill_g_lock and ill_lock while
13282 	 * inserting or removing an ipif from the linked list
13283 	 * of ipifs hanging off the ill.
13284 	 */
13285 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13286 
13287 #ifdef DEBUG
13288 	ipif_trace_cleanup(ipif);
13289 #endif
13290 
13291 	/* Ask SCTP to take it out of it list */
13292 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13293 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13294 
13295 	/* Get it out of the ILL interface list. */
13296 	ipif_remove(ipif);
13297 	rw_exit(&ipst->ips_ill_g_lock);
13298 
13299 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13300 	ASSERT(ipif->ipif_recovery_id == 0);
13301 	ASSERT(ipif->ipif_ire_local == NULL);
13302 	ASSERT(ipif->ipif_ire_if == NULL);
13303 
13304 	/* Free the memory. */
13305 	mi_free(ipif);
13306 }
13307 
13308 /*
13309  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13310  * is zero.
13311  */
13312 void
13313 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13314 {
13315 	char	lbuf[LIFNAMSIZ];
13316 	char	*name;
13317 	size_t	name_len;
13318 
13319 	buf[0] = '\0';
13320 	name = ipif->ipif_ill->ill_name;
13321 	name_len = ipif->ipif_ill->ill_name_length;
13322 	if (ipif->ipif_id != 0) {
13323 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13324 		    ipif->ipif_id);
13325 		name = lbuf;
13326 		name_len = mi_strlen(name) + 1;
13327 	}
13328 	len -= 1;
13329 	buf[len] = '\0';
13330 	len = MIN(len, name_len);
13331 	bcopy(name, buf, len);
13332 }
13333 
13334 /*
13335  * Sets `buf' to an ill name.
13336  */
13337 void
13338 ill_get_name(const ill_t *ill, char *buf, int len)
13339 {
13340 	char	*name;
13341 	size_t	name_len;
13342 
13343 	name = ill->ill_name;
13344 	name_len = ill->ill_name_length;
13345 	len -= 1;
13346 	buf[len] = '\0';
13347 	len = MIN(len, name_len);
13348 	bcopy(name, buf, len);
13349 }
13350 
13351 /*
13352  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13353  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13354  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13355  * (May be called as writer.)
13356  */
13357 static ipif_t *
13358 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13359     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13360 {
13361 	char	*cp;
13362 	char	*endp;
13363 	long	id;
13364 	ill_t	*ill;
13365 	ipif_t	*ipif;
13366 	uint_t	ire_type;
13367 	boolean_t did_alloc = B_FALSE;
13368 
13369 	/*
13370 	 * If the caller wants to us to create the ipif, make sure we have a
13371 	 * valid zoneid
13372 	 */
13373 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13374 
13375 	if (namelen == 0) {
13376 		return (NULL);
13377 	}
13378 
13379 	*exists = B_FALSE;
13380 	/* Look for a colon in the name. */
13381 	endp = &name[namelen];
13382 	for (cp = endp; --cp > name; ) {
13383 		if (*cp == IPIF_SEPARATOR_CHAR)
13384 			break;
13385 	}
13386 
13387 	if (*cp == IPIF_SEPARATOR_CHAR) {
13388 		/*
13389 		 * Reject any non-decimal aliases for logical
13390 		 * interfaces. Aliases with leading zeroes
13391 		 * are also rejected as they introduce ambiguity
13392 		 * in the naming of the interfaces.
13393 		 * In order to confirm with existing semantics,
13394 		 * and to not break any programs/script relying
13395 		 * on that behaviour, if<0>:0 is considered to be
13396 		 * a valid interface.
13397 		 *
13398 		 * If alias has two or more digits and the first
13399 		 * is zero, fail.
13400 		 */
13401 		if (&cp[2] < endp && cp[1] == '0') {
13402 			return (NULL);
13403 		}
13404 	}
13405 
13406 	if (cp <= name) {
13407 		cp = endp;
13408 	} else {
13409 		*cp = '\0';
13410 	}
13411 
13412 	/*
13413 	 * Look up the ILL, based on the portion of the name
13414 	 * before the slash. ill_lookup_on_name returns a held ill.
13415 	 * Temporary to check whether ill exists already. If so
13416 	 * ill_lookup_on_name will clear it.
13417 	 */
13418 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13419 	    &did_alloc, ipst);
13420 	if (cp != endp)
13421 		*cp = IPIF_SEPARATOR_CHAR;
13422 	if (ill == NULL)
13423 		return (NULL);
13424 
13425 	/* Establish the unit number in the name. */
13426 	id = 0;
13427 	if (cp < endp && *endp == '\0') {
13428 		/* If there was a colon, the unit number follows. */
13429 		cp++;
13430 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13431 			ill_refrele(ill);
13432 			return (NULL);
13433 		}
13434 	}
13435 
13436 	mutex_enter(&ill->ill_lock);
13437 	/* Now see if there is an IPIF with this unit number. */
13438 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13439 		if (ipif->ipif_id == id) {
13440 			if (zoneid != ALL_ZONES &&
13441 			    zoneid != ipif->ipif_zoneid &&
13442 			    ipif->ipif_zoneid != ALL_ZONES) {
13443 				mutex_exit(&ill->ill_lock);
13444 				ill_refrele(ill);
13445 				return (NULL);
13446 			}
13447 			if (IPIF_CAN_LOOKUP(ipif)) {
13448 				ipif_refhold_locked(ipif);
13449 				mutex_exit(&ill->ill_lock);
13450 				if (!did_alloc)
13451 					*exists = B_TRUE;
13452 				/*
13453 				 * Drop locks before calling ill_refrele
13454 				 * since it can potentially call into
13455 				 * ipif_ill_refrele_tail which can end up
13456 				 * in trying to acquire any lock.
13457 				 */
13458 				ill_refrele(ill);
13459 				return (ipif);
13460 			}
13461 		}
13462 	}
13463 
13464 	if (!do_alloc) {
13465 		mutex_exit(&ill->ill_lock);
13466 		ill_refrele(ill);
13467 		return (NULL);
13468 	}
13469 
13470 	/*
13471 	 * If none found, atomically allocate and return a new one.
13472 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13473 	 * to support "receive only" use of lo0:1 etc. as is still done
13474 	 * below as an initial guess.
13475 	 * However, this is now likely to be overriden later in ipif_up_done()
13476 	 * when we know for sure what address has been configured on the
13477 	 * interface, since we might have more than one loopback interface
13478 	 * with a loopback address, e.g. in the case of zones, and all the
13479 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13480 	 */
13481 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13482 		ire_type = IRE_LOOPBACK;
13483 	else
13484 		ire_type = IRE_LOCAL;
13485 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13486 	if (ipif != NULL)
13487 		ipif_refhold_locked(ipif);
13488 	mutex_exit(&ill->ill_lock);
13489 	ill_refrele(ill);
13490 	return (ipif);
13491 }
13492 
13493 /*
13494  * This routine is called whenever a new address comes up on an ipif.  If
13495  * we are configured to respond to address mask requests, then we are supposed
13496  * to broadcast an address mask reply at this time.  This routine is also
13497  * called if we are already up, but a netmask change is made.  This is legal
13498  * but might not make the system manager very popular.	(May be called
13499  * as writer.)
13500  */
13501 void
13502 ipif_mask_reply(ipif_t *ipif)
13503 {
13504 	icmph_t	*icmph;
13505 	ipha_t	*ipha;
13506 	mblk_t	*mp;
13507 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13508 	ip_xmit_attr_t ixas;
13509 
13510 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13511 
13512 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13513 		return;
13514 
13515 	/* ICMP mask reply is IPv4 only */
13516 	ASSERT(!ipif->ipif_isv6);
13517 	/* ICMP mask reply is not for a loopback interface */
13518 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13519 
13520 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13521 		return;
13522 
13523 	mp = allocb(REPLY_LEN, BPRI_HI);
13524 	if (mp == NULL)
13525 		return;
13526 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13527 
13528 	ipha = (ipha_t *)mp->b_rptr;
13529 	bzero(ipha, REPLY_LEN);
13530 	*ipha = icmp_ipha;
13531 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13532 	ipha->ipha_src = ipif->ipif_lcl_addr;
13533 	ipha->ipha_dst = ipif->ipif_brd_addr;
13534 	ipha->ipha_length = htons(REPLY_LEN);
13535 	ipha->ipha_ident = 0;
13536 
13537 	icmph = (icmph_t *)&ipha[1];
13538 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13539 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13540 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13541 
13542 	bzero(&ixas, sizeof (ixas));
13543 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13544 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13545 	ixas.ixa_zoneid = ALL_ZONES;
13546 	ixas.ixa_ifindex = 0;
13547 	ixas.ixa_ipst = ipst;
13548 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13549 	(void) ip_output_simple(mp, &ixas);
13550 	ixa_cleanup(&ixas);
13551 #undef	REPLY_LEN
13552 }
13553 
13554 /*
13555  * Join the ipif specific multicast groups.
13556  * Must be called after a mapping has been set up in the resolver.  (Always
13557  * called as writer.)
13558  */
13559 void
13560 ipif_multicast_up(ipif_t *ipif)
13561 {
13562 	int err;
13563 	ill_t *ill;
13564 	ilm_t *ilm;
13565 
13566 	ASSERT(IAM_WRITER_IPIF(ipif));
13567 
13568 	ill = ipif->ipif_ill;
13569 
13570 	ip1dbg(("ipif_multicast_up\n"));
13571 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13572 	    ipif->ipif_allhosts_ilm != NULL)
13573 		return;
13574 
13575 	if (ipif->ipif_isv6) {
13576 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13577 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13578 
13579 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13580 
13581 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13582 			return;
13583 
13584 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13585 
13586 		/*
13587 		 * Join the all hosts multicast address.  We skip this for
13588 		 * underlying IPMP interfaces since they should be invisible.
13589 		 */
13590 		if (!IS_UNDER_IPMP(ill)) {
13591 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13592 			    &err);
13593 			if (ilm == NULL) {
13594 				ASSERT(err != 0);
13595 				ip0dbg(("ipif_multicast_up: "
13596 				    "all_hosts_mcast failed %d\n", err));
13597 				return;
13598 			}
13599 			ipif->ipif_allhosts_ilm = ilm;
13600 		}
13601 
13602 		/*
13603 		 * Enable multicast for the solicited node multicast address.
13604 		 * If IPMP we need to put the membership on the upper ill.
13605 		 */
13606 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13607 			ill_t *mcast_ill = NULL;
13608 			boolean_t need_refrele;
13609 
13610 			if (IS_UNDER_IPMP(ill) &&
13611 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13612 				need_refrele = B_TRUE;
13613 			} else {
13614 				mcast_ill = ill;
13615 				need_refrele = B_FALSE;
13616 			}
13617 
13618 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13619 			    ipif->ipif_zoneid, &err);
13620 			if (need_refrele)
13621 				ill_refrele(mcast_ill);
13622 
13623 			if (ilm == NULL) {
13624 				ASSERT(err != 0);
13625 				ip0dbg(("ipif_multicast_up: solicited MC"
13626 				    " failed %d\n", err));
13627 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13628 					ipif->ipif_allhosts_ilm = NULL;
13629 					(void) ip_delmulti(ilm);
13630 				}
13631 				return;
13632 			}
13633 			ipif->ipif_solmulti_ilm = ilm;
13634 		}
13635 	} else {
13636 		in6_addr_t v6group;
13637 
13638 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13639 			return;
13640 
13641 		/* Join the all hosts multicast address */
13642 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13643 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13644 
13645 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13646 		if (ilm == NULL) {
13647 			ASSERT(err != 0);
13648 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13649 			return;
13650 		}
13651 		ipif->ipif_allhosts_ilm = ilm;
13652 	}
13653 }
13654 
13655 /*
13656  * Blow away any multicast groups that we joined in ipif_multicast_up().
13657  * (ilms from explicit memberships are handled in conn_update_ill.)
13658  */
13659 void
13660 ipif_multicast_down(ipif_t *ipif)
13661 {
13662 	ASSERT(IAM_WRITER_IPIF(ipif));
13663 
13664 	ip1dbg(("ipif_multicast_down\n"));
13665 
13666 	if (ipif->ipif_allhosts_ilm != NULL) {
13667 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13668 		ipif->ipif_allhosts_ilm = NULL;
13669 	}
13670 	if (ipif->ipif_solmulti_ilm != NULL) {
13671 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13672 		ipif->ipif_solmulti_ilm = NULL;
13673 	}
13674 }
13675 
13676 /*
13677  * Used when an interface comes up to recreate any extra routes on this
13678  * interface.
13679  */
13680 int
13681 ill_recover_saved_ire(ill_t *ill)
13682 {
13683 	mblk_t		*mp;
13684 	ip_stack_t	*ipst = ill->ill_ipst;
13685 
13686 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13687 
13688 	mutex_enter(&ill->ill_saved_ire_lock);
13689 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13690 		ire_t		*ire, *nire;
13691 		ifrt_t		*ifrt;
13692 
13693 		ifrt = (ifrt_t *)mp->b_rptr;
13694 		/*
13695 		 * Create a copy of the IRE with the saved address and netmask.
13696 		 */
13697 		if (ill->ill_isv6) {
13698 			ire = ire_create_v6(
13699 			    &ifrt->ifrt_v6addr,
13700 			    &ifrt->ifrt_v6mask,
13701 			    &ifrt->ifrt_v6gateway_addr,
13702 			    ifrt->ifrt_type,
13703 			    ill,
13704 			    ifrt->ifrt_zoneid,
13705 			    ifrt->ifrt_flags,
13706 			    NULL,
13707 			    ipst);
13708 		} else {
13709 			ire = ire_create(
13710 			    (uint8_t *)&ifrt->ifrt_addr,
13711 			    (uint8_t *)&ifrt->ifrt_mask,
13712 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13713 			    ifrt->ifrt_type,
13714 			    ill,
13715 			    ifrt->ifrt_zoneid,
13716 			    ifrt->ifrt_flags,
13717 			    NULL,
13718 			    ipst);
13719 		}
13720 		if (ire == NULL) {
13721 			mutex_exit(&ill->ill_saved_ire_lock);
13722 			return (ENOMEM);
13723 		}
13724 
13725 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13726 			if (ill->ill_isv6) {
13727 				ire->ire_setsrc_addr_v6 =
13728 				    ifrt->ifrt_v6setsrc_addr;
13729 			} else {
13730 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13731 			}
13732 		}
13733 
13734 		/*
13735 		 * Some software (for example, GateD and Sun Cluster) attempts
13736 		 * to create (what amount to) IRE_PREFIX routes with the
13737 		 * loopback address as the gateway.  This is primarily done to
13738 		 * set up prefixes with the RTF_REJECT flag set (for example,
13739 		 * when generating aggregate routes.)
13740 		 *
13741 		 * If the IRE type (as defined by ill->ill_net_type) is
13742 		 * IRE_LOOPBACK, then we map the request into a
13743 		 * IRE_IF_NORESOLVER.
13744 		 */
13745 		if (ill->ill_net_type == IRE_LOOPBACK)
13746 			ire->ire_type = IRE_IF_NORESOLVER;
13747 
13748 		/*
13749 		 * ire held by ire_add, will be refreled' towards the
13750 		 * the end of ipif_up_done
13751 		 */
13752 		nire = ire_add(ire);
13753 		/*
13754 		 * Check if it was a duplicate entry. This handles
13755 		 * the case of two racing route adds for the same route
13756 		 */
13757 		if (nire == NULL) {
13758 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13759 		} else if (nire != ire) {
13760 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13761 			    (void *)nire));
13762 			ire_delete(nire);
13763 		} else {
13764 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13765 			    (void *)nire));
13766 		}
13767 		if (nire != NULL)
13768 			ire_refrele(nire);
13769 	}
13770 	mutex_exit(&ill->ill_saved_ire_lock);
13771 	return (0);
13772 }
13773 
13774 /*
13775  * Used to set the netmask and broadcast address to default values when the
13776  * interface is brought up.  (Always called as writer.)
13777  */
13778 static void
13779 ipif_set_default(ipif_t *ipif)
13780 {
13781 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13782 
13783 	if (!ipif->ipif_isv6) {
13784 		/*
13785 		 * Interface holds an IPv4 address. Default
13786 		 * mask is the natural netmask.
13787 		 */
13788 		if (!ipif->ipif_net_mask) {
13789 			ipaddr_t	v4mask;
13790 
13791 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13792 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13793 		}
13794 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13795 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13796 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13797 		} else {
13798 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13799 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13800 		}
13801 		/*
13802 		 * NOTE: SunOS 4.X does this even if the broadcast address
13803 		 * has been already set thus we do the same here.
13804 		 */
13805 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13806 			ipaddr_t	v4addr;
13807 
13808 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13809 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13810 		}
13811 	} else {
13812 		/*
13813 		 * Interface holds an IPv6-only address.  Default
13814 		 * mask is all-ones.
13815 		 */
13816 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13817 			ipif->ipif_v6net_mask = ipv6_all_ones;
13818 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13819 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13820 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13821 		} else {
13822 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13823 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13824 		}
13825 	}
13826 }
13827 
13828 /*
13829  * Return 0 if this address can be used as local address without causing
13830  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13831  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13832  * Note that the same IPv6 link-local address is allowed as long as the ills
13833  * are not on the same link.
13834  */
13835 int
13836 ip_addr_availability_check(ipif_t *new_ipif)
13837 {
13838 	in6_addr_t our_v6addr;
13839 	ill_t *ill;
13840 	ipif_t *ipif;
13841 	ill_walk_context_t ctx;
13842 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13843 
13844 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13845 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13846 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13847 
13848 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13849 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13850 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13851 		return (0);
13852 
13853 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13854 
13855 	if (new_ipif->ipif_isv6)
13856 		ill = ILL_START_WALK_V6(&ctx, ipst);
13857 	else
13858 		ill = ILL_START_WALK_V4(&ctx, ipst);
13859 
13860 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13861 		for (ipif = ill->ill_ipif; ipif != NULL;
13862 		    ipif = ipif->ipif_next) {
13863 			if ((ipif == new_ipif) ||
13864 			    !(ipif->ipif_flags & IPIF_UP) ||
13865 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13866 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13867 			    &our_v6addr))
13868 				continue;
13869 
13870 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13871 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13872 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13873 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13874 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13875 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13876 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13877 				continue;
13878 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13879 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13880 				continue;
13881 			else if (new_ipif->ipif_ill == ill)
13882 				return (EADDRINUSE);
13883 			else
13884 				return (EADDRNOTAVAIL);
13885 		}
13886 	}
13887 
13888 	return (0);
13889 }
13890 
13891 /*
13892  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13893  * IREs for the ipif.
13894  * When the routine returns EINPROGRESS then mp has been consumed and
13895  * the ioctl will be acked from ip_rput_dlpi.
13896  */
13897 int
13898 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13899 {
13900 	ill_t		*ill = ipif->ipif_ill;
13901 	boolean_t 	isv6 = ipif->ipif_isv6;
13902 	int		err = 0;
13903 	boolean_t	success;
13904 	uint_t		ipif_orig_id;
13905 	ip_stack_t	*ipst = ill->ill_ipst;
13906 
13907 	ASSERT(IAM_WRITER_IPIF(ipif));
13908 
13909 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13910 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13911 	    ill_t *, ill, ipif_t *, ipif);
13912 
13913 	/* Shouldn't get here if it is already up. */
13914 	if (ipif->ipif_flags & IPIF_UP)
13915 		return (EALREADY);
13916 
13917 	/*
13918 	 * If this is a request to bring up a data address on an interface
13919 	 * under IPMP, then move the address to its IPMP meta-interface and
13920 	 * try to bring it up.  One complication is that the zeroth ipif for
13921 	 * an ill is special, in that every ill always has one, and that code
13922 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13923 	 */
13924 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13925 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13926 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13927 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13928 
13929 		/*
13930 		 * The ipif being brought up should be quiesced.  If it's not,
13931 		 * something has gone amiss and we need to bail out.  (If it's
13932 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13933 		 */
13934 		mutex_enter(&ill->ill_lock);
13935 		if (!ipif_is_quiescent(ipif)) {
13936 			mutex_exit(&ill->ill_lock);
13937 			return (EINVAL);
13938 		}
13939 		mutex_exit(&ill->ill_lock);
13940 
13941 		/*
13942 		 * If we're going to need to allocate ipifs, do it prior
13943 		 * to starting the move (and grabbing locks).
13944 		 */
13945 		if (ipif->ipif_id == 0) {
13946 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13947 			    B_FALSE, &err)) == NULL) {
13948 				return (err);
13949 			}
13950 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13951 			    B_FALSE, &err)) == NULL) {
13952 				mi_free(moveipif);
13953 				return (err);
13954 			}
13955 		}
13956 
13957 		/*
13958 		 * Grab or transfer the ipif to move.  During the move, keep
13959 		 * ill_g_lock held to prevent any ill walker threads from
13960 		 * seeing things in an inconsistent state.
13961 		 */
13962 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13963 		if (ipif->ipif_id != 0) {
13964 			ipif_remove(ipif);
13965 		} else {
13966 			ipif_transfer(ipif, moveipif, stubipif);
13967 			ipif = moveipif;
13968 		}
13969 
13970 		/*
13971 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13972 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13973 		 * replace that one.  Otherwise, pick the next available slot.
13974 		 */
13975 		ipif->ipif_ill = ipmp_ill;
13976 		ipif_orig_id = ipif->ipif_id;
13977 
13978 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13979 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13980 			ipif = ipmp_ill->ill_ipif;
13981 		} else {
13982 			ipif->ipif_id = -1;
13983 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
13984 				/*
13985 				 * No more available ipif_id's -- put it back
13986 				 * on the original ill and fail the operation.
13987 				 * Since we're writer on the ill, we can be
13988 				 * sure our old slot is still available.
13989 				 */
13990 				ipif->ipif_id = ipif_orig_id;
13991 				ipif->ipif_ill = ill;
13992 				if (ipif_orig_id == 0) {
13993 					ipif_transfer(ipif, ill->ill_ipif,
13994 					    NULL);
13995 				} else {
13996 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13997 				}
13998 				rw_exit(&ipst->ips_ill_g_lock);
13999 				return (err);
14000 			}
14001 		}
14002 		rw_exit(&ipst->ips_ill_g_lock);
14003 
14004 		/*
14005 		 * Tell SCTP that the ipif has moved.  Note that even if we
14006 		 * had to allocate a new ipif, the original sequence id was
14007 		 * preserved and therefore SCTP won't know.
14008 		 */
14009 		sctp_move_ipif(ipif, ill, ipmp_ill);
14010 
14011 		/*
14012 		 * If the ipif being brought up was on slot zero, then we
14013 		 * first need to bring up the placeholder we stuck there.  In
14014 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14015 		 * call to ipif_up() itself, if we successfully bring up the
14016 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14017 		 */
14018 		if (ipif_orig_id == 0) {
14019 			ASSERT(ill->ill_move_ipif == NULL);
14020 			ill->ill_move_ipif = ipif;
14021 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14022 				ASSERT(ill->ill_move_ipif == NULL);
14023 			if (err != EINPROGRESS)
14024 				ill->ill_move_ipif = NULL;
14025 			return (err);
14026 		}
14027 
14028 		/*
14029 		 * Bring it up on the IPMP ill.
14030 		 */
14031 		return (ipif_up(ipif, q, mp));
14032 	}
14033 
14034 	/* Skip arp/ndp for any loopback interface. */
14035 	if (ill->ill_wq != NULL) {
14036 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14037 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14038 
14039 		if (!ill->ill_dl_up) {
14040 			/*
14041 			 * ill_dl_up is not yet set. i.e. we are yet to
14042 			 * DL_BIND with the driver and this is the first
14043 			 * logical interface on the ill to become "up".
14044 			 * Tell the driver to get going (via DL_BIND_REQ).
14045 			 * Note that changing "significant" IFF_ flags
14046 			 * address/netmask etc cause a down/up dance, but
14047 			 * does not cause an unbind (DL_UNBIND) with the driver
14048 			 */
14049 			return (ill_dl_up(ill, ipif, mp, q));
14050 		}
14051 
14052 		/*
14053 		 * ipif_resolver_up may end up needeing to bind/attach
14054 		 * the ARP stream, which in turn necessitates a
14055 		 * DLPI message exchange with the driver. ioctls are
14056 		 * serialized and so we cannot send more than one
14057 		 * interface up message at a time. If ipif_resolver_up
14058 		 * does need to wait for the DLPI handshake for the ARP stream,
14059 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14060 		 */
14061 
14062 		ASSERT(connp != NULL || !CONN_Q(q));
14063 		if (connp != NULL)
14064 			mutex_enter(&connp->conn_lock);
14065 		mutex_enter(&ill->ill_lock);
14066 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14067 		mutex_exit(&ill->ill_lock);
14068 		if (connp != NULL)
14069 			mutex_exit(&connp->conn_lock);
14070 		if (!success)
14071 			return (EINTR);
14072 
14073 		/*
14074 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14075 		 * complete when ipif_ndp_up returns.
14076 		 */
14077 		err = ipif_resolver_up(ipif, Res_act_initial);
14078 		if (err == EINPROGRESS) {
14079 			/* We will complete it in arp_bringup_done() */
14080 			return (err);
14081 		}
14082 
14083 		if (isv6 && err == 0)
14084 			err = ipif_ndp_up(ipif, B_TRUE);
14085 
14086 		ASSERT(err != EINPROGRESS);
14087 		mp = ipsq_pending_mp_get(ipsq, &connp);
14088 		ASSERT(mp != NULL);
14089 		if (err != 0)
14090 			return (err);
14091 	} else {
14092 		/*
14093 		 * Interfaces without underlying hardware don't do duplicate
14094 		 * address detection.
14095 		 */
14096 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14097 		ipif->ipif_addr_ready = 1;
14098 		err = ill_add_ires(ill);
14099 		/* allocation failure? */
14100 		if (err != 0)
14101 			return (err);
14102 	}
14103 
14104 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14105 	if (err == 0 && ill->ill_move_ipif != NULL) {
14106 		ipif = ill->ill_move_ipif;
14107 		ill->ill_move_ipif = NULL;
14108 		return (ipif_up(ipif, q, mp));
14109 	}
14110 	return (err);
14111 }
14112 
14113 /*
14114  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14115  * The identical set of IREs need to be removed in ill_delete_ires().
14116  */
14117 int
14118 ill_add_ires(ill_t *ill)
14119 {
14120 	ire_t	*ire;
14121 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14122 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14123 
14124 	if (ill->ill_ire_multicast != NULL)
14125 		return (0);
14126 
14127 	/*
14128 	 * provide some dummy ire_addr for creating the ire.
14129 	 */
14130 	if (ill->ill_isv6) {
14131 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14132 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14133 	} else {
14134 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14135 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14136 	}
14137 	if (ire == NULL)
14138 		return (ENOMEM);
14139 
14140 	ill->ill_ire_multicast = ire;
14141 	return (0);
14142 }
14143 
14144 void
14145 ill_delete_ires(ill_t *ill)
14146 {
14147 	if (ill->ill_ire_multicast != NULL) {
14148 		/*
14149 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14150 		 * which was taken without any th_tracing enabled.
14151 		 * We also mark it as condemned (note that it was never added)
14152 		 * so that caching conn's can move off of it.
14153 		 */
14154 		ire_make_condemned(ill->ill_ire_multicast);
14155 		ire_refrele_notr(ill->ill_ire_multicast);
14156 		ill->ill_ire_multicast = NULL;
14157 	}
14158 }
14159 
14160 /*
14161  * Perform a bind for the physical device.
14162  * When the routine returns EINPROGRESS then mp has been consumed and
14163  * the ioctl will be acked from ip_rput_dlpi.
14164  * Allocate an unbind message and save it until ipif_down.
14165  */
14166 static int
14167 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14168 {
14169 	mblk_t	*bind_mp = NULL;
14170 	mblk_t	*unbind_mp = NULL;
14171 	conn_t	*connp;
14172 	boolean_t success;
14173 	int	err;
14174 
14175 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14176 
14177 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14178 	ASSERT(IAM_WRITER_ILL(ill));
14179 	ASSERT(mp != NULL);
14180 
14181 	/*
14182 	 * Make sure we have an IRE_MULTICAST in case we immediately
14183 	 * start receiving packets.
14184 	 */
14185 	err = ill_add_ires(ill);
14186 	if (err != 0)
14187 		goto bad;
14188 
14189 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14190 	    DL_BIND_REQ);
14191 	if (bind_mp == NULL)
14192 		goto bad;
14193 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14194 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14195 
14196 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14197 	if (unbind_mp == NULL)
14198 		goto bad;
14199 
14200 	/*
14201 	 * Record state needed to complete this operation when the
14202 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14203 	 */
14204 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14205 	ASSERT(connp != NULL || !CONN_Q(q));
14206 	GRAB_CONN_LOCK(q);
14207 	mutex_enter(&ipif->ipif_ill->ill_lock);
14208 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14209 	mutex_exit(&ipif->ipif_ill->ill_lock);
14210 	RELEASE_CONN_LOCK(q);
14211 	if (!success)
14212 		goto bad;
14213 
14214 	/*
14215 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14216 	 * the interface goes down.
14217 	 */
14218 	ASSERT(ill->ill_unbind_mp == NULL);
14219 	ill->ill_unbind_mp = unbind_mp;
14220 
14221 	ill_dlpi_send(ill, bind_mp);
14222 	/* Send down link-layer capabilities probe if not already done. */
14223 	ill_capability_probe(ill);
14224 
14225 	/*
14226 	 * Sysid used to rely on the fact that netboots set domainname
14227 	 * and the like. Now that miniroot boots aren't strictly netboots
14228 	 * and miniroot network configuration is driven from userland
14229 	 * these things still need to be set. This situation can be detected
14230 	 * by comparing the interface being configured here to the one
14231 	 * dhcifname was set to reference by the boot loader. Once sysid is
14232 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14233 	 */
14234 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14235 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14236 	    (strlen(srpc_domain) == 0)) {
14237 		if (dhcpinit() != 0)
14238 			cmn_err(CE_WARN, "no cached dhcp response");
14239 	}
14240 
14241 	/*
14242 	 * This operation will complete in ip_rput_dlpi with either
14243 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14244 	 */
14245 	return (EINPROGRESS);
14246 bad:
14247 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14248 
14249 	freemsg(bind_mp);
14250 	freemsg(unbind_mp);
14251 	return (ENOMEM);
14252 }
14253 
14254 /* Add room for tcp+ip headers */
14255 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14256 
14257 /*
14258  * DLPI and ARP is up.
14259  * Create all the IREs associated with an interface. Bring up multicast.
14260  * Set the interface flag and finish other initialization
14261  * that potentially had to be deferred to after DL_BIND_ACK.
14262  */
14263 int
14264 ipif_up_done(ipif_t *ipif)
14265 {
14266 	ill_t		*ill = ipif->ipif_ill;
14267 	int		err = 0;
14268 	boolean_t	loopback = B_FALSE;
14269 	boolean_t	update_src_selection = B_TRUE;
14270 	ipif_t		*tmp_ipif;
14271 
14272 	ip1dbg(("ipif_up_done(%s:%u)\n",
14273 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14274 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14275 	    ill_t *, ill, ipif_t *, ipif);
14276 
14277 	/* Check if this is a loopback interface */
14278 	if (ipif->ipif_ill->ill_wq == NULL)
14279 		loopback = B_TRUE;
14280 
14281 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14282 
14283 	/*
14284 	 * If all other interfaces for this ill are down or DEPRECATED,
14285 	 * or otherwise unsuitable for source address selection,
14286 	 * reset the src generation numbers to make sure source
14287 	 * address selection gets to take this new ipif into account.
14288 	 * No need to hold ill_lock while traversing the ipif list since
14289 	 * we are writer
14290 	 */
14291 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14292 	    tmp_ipif = tmp_ipif->ipif_next) {
14293 		if (((tmp_ipif->ipif_flags &
14294 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14295 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14296 		    (tmp_ipif == ipif))
14297 			continue;
14298 		/* first useable pre-existing interface */
14299 		update_src_selection = B_FALSE;
14300 		break;
14301 	}
14302 	if (update_src_selection)
14303 		ip_update_source_selection(ill->ill_ipst);
14304 
14305 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14306 		nce_t *loop_nce = NULL;
14307 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14308 
14309 		/*
14310 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14311 		 * ipif_lookup_on_name(), but in the case of zones we can have
14312 		 * several loopback addresses on lo0. So all the interfaces with
14313 		 * loopback addresses need to be marked IRE_LOOPBACK.
14314 		 */
14315 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14316 		    htonl(INADDR_LOOPBACK))
14317 			ipif->ipif_ire_type = IRE_LOOPBACK;
14318 		else
14319 			ipif->ipif_ire_type = IRE_LOCAL;
14320 		if (ill->ill_net_type != IRE_LOOPBACK)
14321 			flags |= NCE_F_PUBLISH;
14322 
14323 		/* add unicast nce for the local addr */
14324 		err = nce_lookup_then_add_v4(ill, NULL,
14325 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14326 		    ND_REACHABLE, &loop_nce);
14327 		/* A shared-IP zone sees EEXIST for lo0:N */
14328 		if (err == 0 || err == EEXIST) {
14329 			ipif->ipif_added_nce = 1;
14330 			loop_nce->nce_ipif_cnt++;
14331 			nce_refrele(loop_nce);
14332 			err = 0;
14333 		} else {
14334 			ASSERT(loop_nce == NULL);
14335 			return (err);
14336 		}
14337 	}
14338 
14339 	/* Create all the IREs associated with this interface */
14340 	err = ipif_add_ires_v4(ipif, loopback);
14341 	if (err != 0) {
14342 		/*
14343 		 * see comments about return value from
14344 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14345 		 */
14346 		if (err != EADDRINUSE) {
14347 			(void) ipif_arp_down(ipif);
14348 		} else {
14349 			/*
14350 			 * Make IPMP aware of the deleted ipif so that
14351 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14352 			 * can be completed. Note that we do not want to
14353 			 * destroy the nce that was created on the ipmp_ill
14354 			 * for the active copy of the duplicate address in
14355 			 * use.
14356 			 */
14357 			if (IS_IPMP(ill))
14358 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14359 			err = EADDRNOTAVAIL;
14360 		}
14361 		return (err);
14362 	}
14363 
14364 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14365 		/* Recover any additional IREs entries for this ill */
14366 		(void) ill_recover_saved_ire(ill);
14367 	}
14368 
14369 	if (ill->ill_need_recover_multicast) {
14370 		/*
14371 		 * Need to recover all multicast memberships in the driver.
14372 		 * This had to be deferred until we had attached.  The same
14373 		 * code exists in ipif_up_done_v6() to recover IPv6
14374 		 * memberships.
14375 		 *
14376 		 * Note that it would be preferable to unconditionally do the
14377 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14378 		 * that since ill_join_allmulti() depends on ill_dl_up being
14379 		 * set, and it is not set until we receive a DL_BIND_ACK after
14380 		 * having called ill_dl_up().
14381 		 */
14382 		ill_recover_multicast(ill);
14383 	}
14384 
14385 	if (ill->ill_ipif_up_count == 1) {
14386 		/*
14387 		 * Since the interface is now up, it may now be active.
14388 		 */
14389 		if (IS_UNDER_IPMP(ill))
14390 			ipmp_ill_refresh_active(ill);
14391 
14392 		/*
14393 		 * If this is an IPMP interface, we may now be able to
14394 		 * establish ARP entries.
14395 		 */
14396 		if (IS_IPMP(ill))
14397 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14398 	}
14399 
14400 	/* Join the allhosts multicast address */
14401 	ipif_multicast_up(ipif);
14402 
14403 	if (!loopback && !update_src_selection &&
14404 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14405 		ip_update_source_selection(ill->ill_ipst);
14406 
14407 	if (!loopback && ipif->ipif_addr_ready) {
14408 		/* Broadcast an address mask reply. */
14409 		ipif_mask_reply(ipif);
14410 	}
14411 	/* Perhaps ilgs should use this ill */
14412 	update_conn_ill(NULL, ill->ill_ipst);
14413 
14414 	/*
14415 	 * This had to be deferred until we had bound.  Tell routing sockets and
14416 	 * others that this interface is up if it looks like the address has
14417 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14418 	 * duplicate address detection to do its thing.
14419 	 */
14420 	if (ipif->ipif_addr_ready)
14421 		ipif_up_notify(ipif);
14422 	return (0);
14423 }
14424 
14425 /*
14426  * Add the IREs associated with the ipif.
14427  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14428  */
14429 static int
14430 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14431 {
14432 	ill_t		*ill = ipif->ipif_ill;
14433 	ip_stack_t	*ipst = ill->ill_ipst;
14434 	ire_t		*ire_array[20];
14435 	ire_t		**irep = ire_array;
14436 	ire_t		**irep1;
14437 	ipaddr_t	net_mask = 0;
14438 	ipaddr_t	subnet_mask, route_mask;
14439 	int		err;
14440 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14441 	ire_t		*ire_if = NULL;
14442 	uchar_t		*gw;
14443 
14444 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14445 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14446 		/*
14447 		 * If we're on a labeled system then make sure that zone-
14448 		 * private addresses have proper remote host database entries.
14449 		 */
14450 		if (is_system_labeled() &&
14451 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14452 		    !tsol_check_interface_address(ipif))
14453 			return (EINVAL);
14454 
14455 		/* Register the source address for __sin6_src_id */
14456 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14457 		    ipif->ipif_zoneid, ipst);
14458 		if (err != 0) {
14459 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14460 			return (err);
14461 		}
14462 
14463 		if (loopback)
14464 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14465 		else
14466 			gw = NULL;
14467 
14468 		/* If the interface address is set, create the local IRE. */
14469 		ire_local = ire_create(
14470 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14471 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14472 		    gw,					/* gateway */
14473 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14474 		    ipif->ipif_ill,
14475 		    ipif->ipif_zoneid,
14476 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14477 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14478 		    NULL,
14479 		    ipst);
14480 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14481 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14482 		    ipif->ipif_ire_type,
14483 		    ntohl(ipif->ipif_lcl_addr)));
14484 		if (ire_local == NULL) {
14485 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14486 			err = ENOMEM;
14487 			goto bad;
14488 		}
14489 	} else {
14490 		ip1dbg((
14491 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14492 		    ipif->ipif_ire_type,
14493 		    ntohl(ipif->ipif_lcl_addr),
14494 		    (uint_t)ipif->ipif_flags));
14495 	}
14496 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14497 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14498 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14499 	} else {
14500 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14501 	}
14502 
14503 	subnet_mask = ipif->ipif_net_mask;
14504 
14505 	/*
14506 	 * If mask was not specified, use natural netmask of
14507 	 * interface address. Also, store this mask back into the
14508 	 * ipif struct.
14509 	 */
14510 	if (subnet_mask == 0) {
14511 		subnet_mask = net_mask;
14512 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14513 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14514 		    ipif->ipif_v6subnet);
14515 	}
14516 
14517 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14518 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14519 	    ipif->ipif_subnet != INADDR_ANY) {
14520 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14521 
14522 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14523 			route_mask = IP_HOST_MASK;
14524 		} else {
14525 			route_mask = subnet_mask;
14526 		}
14527 
14528 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14529 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14530 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14531 		    ntohl(ipif->ipif_subnet)));
14532 		ire_if = ire_create(
14533 		    (uchar_t *)&ipif->ipif_subnet,
14534 		    (uchar_t *)&route_mask,
14535 		    (uchar_t *)&ipif->ipif_lcl_addr,
14536 		    ill->ill_net_type,
14537 		    ill,
14538 		    ipif->ipif_zoneid,
14539 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14540 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14541 		    NULL,
14542 		    ipst);
14543 		if (ire_if == NULL) {
14544 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14545 			err = ENOMEM;
14546 			goto bad;
14547 		}
14548 	}
14549 
14550 	/*
14551 	 * Create any necessary broadcast IREs.
14552 	 */
14553 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14554 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14555 		irep = ipif_create_bcast_ires(ipif, irep);
14556 
14557 	/* If an earlier ire_create failed, get out now */
14558 	for (irep1 = irep; irep1 > ire_array; ) {
14559 		irep1--;
14560 		if (*irep1 == NULL) {
14561 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14562 			err = ENOMEM;
14563 			goto bad;
14564 		}
14565 	}
14566 
14567 	/*
14568 	 * Need to atomically check for IP address availability under
14569 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14570 	 * ills or new ipifs can be added while we are checking availability.
14571 	 */
14572 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14573 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14574 	/* Mark it up, and increment counters. */
14575 	ipif->ipif_flags |= IPIF_UP;
14576 	ill->ill_ipif_up_count++;
14577 	err = ip_addr_availability_check(ipif);
14578 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14579 	rw_exit(&ipst->ips_ill_g_lock);
14580 
14581 	if (err != 0) {
14582 		/*
14583 		 * Our address may already be up on the same ill. In this case,
14584 		 * the ARP entry for our ipif replaced the one for the other
14585 		 * ipif. So we don't want to delete it (otherwise the other ipif
14586 		 * would be unable to send packets).
14587 		 * ip_addr_availability_check() identifies this case for us and
14588 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14589 		 * which is the expected error code.
14590 		 */
14591 		ill->ill_ipif_up_count--;
14592 		ipif->ipif_flags &= ~IPIF_UP;
14593 		goto bad;
14594 	}
14595 
14596 	/*
14597 	 * Add in all newly created IREs.  ire_create_bcast() has
14598 	 * already checked for duplicates of the IRE_BROADCAST type.
14599 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14600 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14601 	 * a /32 route.
14602 	 */
14603 	if (ire_if != NULL) {
14604 		ire_if = ire_add(ire_if);
14605 		if (ire_if == NULL) {
14606 			err = ENOMEM;
14607 			goto bad2;
14608 		}
14609 #ifdef DEBUG
14610 		ire_refhold_notr(ire_if);
14611 		ire_refrele(ire_if);
14612 #endif
14613 	}
14614 	if (ire_local != NULL) {
14615 		ire_local = ire_add(ire_local);
14616 		if (ire_local == NULL) {
14617 			err = ENOMEM;
14618 			goto bad2;
14619 		}
14620 #ifdef DEBUG
14621 		ire_refhold_notr(ire_local);
14622 		ire_refrele(ire_local);
14623 #endif
14624 	}
14625 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14626 	if (ire_local != NULL)
14627 		ipif->ipif_ire_local = ire_local;
14628 	if (ire_if != NULL)
14629 		ipif->ipif_ire_if = ire_if;
14630 	rw_exit(&ipst->ips_ill_g_lock);
14631 	ire_local = NULL;
14632 	ire_if = NULL;
14633 
14634 	/*
14635 	 * We first add all of them, and if that succeeds we refrele the
14636 	 * bunch. That enables us to delete all of them should any of the
14637 	 * ire_adds fail.
14638 	 */
14639 	for (irep1 = irep; irep1 > ire_array; ) {
14640 		irep1--;
14641 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14642 		*irep1 = ire_add(*irep1);
14643 		if (*irep1 == NULL) {
14644 			err = ENOMEM;
14645 			goto bad2;
14646 		}
14647 	}
14648 
14649 	for (irep1 = irep; irep1 > ire_array; ) {
14650 		irep1--;
14651 		/* refheld by ire_add. */
14652 		if (*irep1 != NULL) {
14653 			ire_refrele(*irep1);
14654 			*irep1 = NULL;
14655 		}
14656 	}
14657 
14658 	if (!loopback) {
14659 		/*
14660 		 * If the broadcast address has been set, make sure it makes
14661 		 * sense based on the interface address.
14662 		 * Only match on ill since we are sharing broadcast addresses.
14663 		 */
14664 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14665 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14666 			ire_t	*ire;
14667 
14668 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14669 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14670 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14671 
14672 			if (ire == NULL) {
14673 				/*
14674 				 * If there isn't a matching broadcast IRE,
14675 				 * revert to the default for this netmask.
14676 				 */
14677 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14678 				mutex_enter(&ipif->ipif_ill->ill_lock);
14679 				ipif_set_default(ipif);
14680 				mutex_exit(&ipif->ipif_ill->ill_lock);
14681 			} else {
14682 				ire_refrele(ire);
14683 			}
14684 		}
14685 
14686 	}
14687 	return (0);
14688 
14689 bad2:
14690 	ill->ill_ipif_up_count--;
14691 	ipif->ipif_flags &= ~IPIF_UP;
14692 
14693 bad:
14694 	ip1dbg(("ipif_add_ires: FAILED \n"));
14695 	if (ire_local != NULL)
14696 		ire_delete(ire_local);
14697 	if (ire_if != NULL)
14698 		ire_delete(ire_if);
14699 
14700 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14701 	ire_local = ipif->ipif_ire_local;
14702 	ipif->ipif_ire_local = NULL;
14703 	ire_if = ipif->ipif_ire_if;
14704 	ipif->ipif_ire_if = NULL;
14705 	rw_exit(&ipst->ips_ill_g_lock);
14706 	if (ire_local != NULL) {
14707 		ire_delete(ire_local);
14708 		ire_refrele_notr(ire_local);
14709 	}
14710 	if (ire_if != NULL) {
14711 		ire_delete(ire_if);
14712 		ire_refrele_notr(ire_if);
14713 	}
14714 
14715 	while (irep > ire_array) {
14716 		irep--;
14717 		if (*irep != NULL) {
14718 			ire_delete(*irep);
14719 		}
14720 	}
14721 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14722 
14723 	return (err);
14724 }
14725 
14726 /* Remove all the IREs created by ipif_add_ires_v4 */
14727 void
14728 ipif_delete_ires_v4(ipif_t *ipif)
14729 {
14730 	ill_t		*ill = ipif->ipif_ill;
14731 	ip_stack_t	*ipst = ill->ill_ipst;
14732 	ire_t		*ire;
14733 
14734 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14735 	ire = ipif->ipif_ire_local;
14736 	ipif->ipif_ire_local = NULL;
14737 	rw_exit(&ipst->ips_ill_g_lock);
14738 	if (ire != NULL) {
14739 		/*
14740 		 * Move count to ipif so we don't loose the count due to
14741 		 * a down/up dance.
14742 		 */
14743 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14744 
14745 		ire_delete(ire);
14746 		ire_refrele_notr(ire);
14747 	}
14748 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14749 	ire = ipif->ipif_ire_if;
14750 	ipif->ipif_ire_if = NULL;
14751 	rw_exit(&ipst->ips_ill_g_lock);
14752 	if (ire != NULL) {
14753 		ire_delete(ire);
14754 		ire_refrele_notr(ire);
14755 	}
14756 
14757 	/*
14758 	 * Delete the broadcast IREs.
14759 	 */
14760 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14761 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14762 		ipif_delete_bcast_ires(ipif);
14763 }
14764 
14765 /*
14766  * Checks for availbility of a usable source address (if there is one) when the
14767  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14768  * this selection is done regardless of the destination.
14769  */
14770 boolean_t
14771 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14772     ip_stack_t *ipst)
14773 {
14774 	ipif_t		*ipif = NULL;
14775 	ill_t		*uill;
14776 
14777 	ASSERT(ifindex != 0);
14778 
14779 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14780 	if (uill == NULL)
14781 		return (B_FALSE);
14782 
14783 	mutex_enter(&uill->ill_lock);
14784 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14785 		if (IPIF_IS_CONDEMNED(ipif))
14786 			continue;
14787 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14788 			continue;
14789 		if (!(ipif->ipif_flags & IPIF_UP))
14790 			continue;
14791 		if (ipif->ipif_zoneid != zoneid)
14792 			continue;
14793 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14794 		    ipif->ipif_lcl_addr == INADDR_ANY)
14795 			continue;
14796 		mutex_exit(&uill->ill_lock);
14797 		ill_refrele(uill);
14798 		return (B_TRUE);
14799 	}
14800 	mutex_exit(&uill->ill_lock);
14801 	ill_refrele(uill);
14802 	return (B_FALSE);
14803 }
14804 
14805 /*
14806  * Find an ipif with a good local address on the ill+zoneid.
14807  */
14808 ipif_t *
14809 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14810 {
14811 	ipif_t		*ipif;
14812 
14813 	mutex_enter(&ill->ill_lock);
14814 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14815 		if (IPIF_IS_CONDEMNED(ipif))
14816 			continue;
14817 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14818 			continue;
14819 		if (!(ipif->ipif_flags & IPIF_UP))
14820 			continue;
14821 		if (ipif->ipif_zoneid != zoneid &&
14822 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14823 			continue;
14824 		if (ill->ill_isv6 ?
14825 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14826 		    ipif->ipif_lcl_addr == INADDR_ANY)
14827 			continue;
14828 		ipif_refhold_locked(ipif);
14829 		mutex_exit(&ill->ill_lock);
14830 		return (ipif);
14831 	}
14832 	mutex_exit(&ill->ill_lock);
14833 	return (NULL);
14834 }
14835 
14836 /*
14837  * IP source address type, sorted from worst to best.  For a given type,
14838  * always prefer IP addresses on the same subnet.  All-zones addresses are
14839  * suboptimal because they pose problems with unlabeled destinations.
14840  */
14841 typedef enum {
14842 	IPIF_NONE,
14843 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14844 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14845 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14846 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14847 	IPIF_DIFFNET,			/* normal and different subnet */
14848 	IPIF_SAMENET,			/* normal and same subnet */
14849 	IPIF_LOCALADDR			/* local loopback */
14850 } ipif_type_t;
14851 
14852 /*
14853  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14854  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14855  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14856  * the first one, unless IPMP is used in which case we round-robin among them;
14857  * see below for more.
14858  *
14859  * Returns NULL if there is no suitable source address for the ill.
14860  * This only occurs when there is no valid source address for the ill.
14861  */
14862 ipif_t *
14863 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14864     boolean_t allow_usesrc, boolean_t *notreadyp)
14865 {
14866 	ill_t	*usill = NULL;
14867 	ill_t	*ipmp_ill = NULL;
14868 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14869 	ipif_type_t type, best_type;
14870 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14871 	ip_stack_t *ipst = ill->ill_ipst;
14872 	boolean_t samenet;
14873 
14874 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14875 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14876 		    B_FALSE, ipst);
14877 		if (usill != NULL)
14878 			ill = usill;	/* Select source from usesrc ILL */
14879 		else
14880 			return (NULL);
14881 	}
14882 
14883 	/*
14884 	 * Test addresses should never be used for source address selection,
14885 	 * so if we were passed one, switch to the IPMP meta-interface.
14886 	 */
14887 	if (IS_UNDER_IPMP(ill)) {
14888 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14889 			ill = ipmp_ill;	/* Select source from IPMP ill */
14890 		else
14891 			return (NULL);
14892 	}
14893 
14894 	/*
14895 	 * If we're dealing with an unlabeled destination on a labeled system,
14896 	 * make sure that we ignore source addresses that are incompatible with
14897 	 * the destination's default label.  That destination's default label
14898 	 * must dominate the minimum label on the source address.
14899 	 */
14900 	dst_rhtp = NULL;
14901 	if (is_system_labeled()) {
14902 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14903 		if (dst_rhtp == NULL)
14904 			return (NULL);
14905 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14906 			TPC_RELE(dst_rhtp);
14907 			dst_rhtp = NULL;
14908 		}
14909 	}
14910 
14911 	/*
14912 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14913 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14914 	 * After selecting the right ipif, under ill_lock make sure ipif is
14915 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14916 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14917 	 * but not under a lock.
14918 	 */
14919 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14920 retry:
14921 	/*
14922 	 * For source address selection, we treat the ipif list as circular
14923 	 * and continue until we get back to where we started.  This allows
14924 	 * IPMP to vary source address selection (which improves inbound load
14925 	 * spreading) by caching its last ending point and starting from
14926 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14927 	 * ills since that can't happen on the IPMP ill.
14928 	 */
14929 	start_ipif = ill->ill_ipif;
14930 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14931 		start_ipif = ill->ill_src_ipif;
14932 
14933 	ipif = start_ipif;
14934 	best_ipif = NULL;
14935 	best_type = IPIF_NONE;
14936 	do {
14937 		if ((next_ipif = ipif->ipif_next) == NULL)
14938 			next_ipif = ill->ill_ipif;
14939 
14940 		if (IPIF_IS_CONDEMNED(ipif))
14941 			continue;
14942 		/* Always skip NOLOCAL and ANYCAST interfaces */
14943 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14944 			continue;
14945 		/* Always skip NOACCEPT interfaces */
14946 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14947 			continue;
14948 		if (!(ipif->ipif_flags & IPIF_UP))
14949 			continue;
14950 
14951 		if (!ipif->ipif_addr_ready) {
14952 			if (notreadyp != NULL)
14953 				*notreadyp = B_TRUE;
14954 			continue;
14955 		}
14956 
14957 		if (zoneid != ALL_ZONES &&
14958 		    ipif->ipif_zoneid != zoneid &&
14959 		    ipif->ipif_zoneid != ALL_ZONES)
14960 			continue;
14961 
14962 		/*
14963 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14964 		 * are not valid as source addresses.
14965 		 */
14966 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14967 			continue;
14968 
14969 		/*
14970 		 * Check compatibility of local address for destination's
14971 		 * default label if we're on a labeled system.	Incompatible
14972 		 * addresses can't be used at all.
14973 		 */
14974 		if (dst_rhtp != NULL) {
14975 			boolean_t incompat;
14976 
14977 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14978 			    IPV4_VERSION, B_FALSE);
14979 			if (src_rhtp == NULL)
14980 				continue;
14981 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14982 			    src_rhtp->tpc_tp.tp_doi !=
14983 			    dst_rhtp->tpc_tp.tp_doi ||
14984 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14985 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14986 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14987 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14988 			TPC_RELE(src_rhtp);
14989 			if (incompat)
14990 				continue;
14991 		}
14992 
14993 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14994 
14995 		if (ipif->ipif_lcl_addr == dst) {
14996 			type = IPIF_LOCALADDR;
14997 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14998 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14999 			    IPIF_DIFFNET_DEPRECATED;
15000 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15001 			type = samenet ? IPIF_SAMENET_ALLZONES :
15002 			    IPIF_DIFFNET_ALLZONES;
15003 		} else {
15004 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15005 		}
15006 
15007 		if (type > best_type) {
15008 			best_type = type;
15009 			best_ipif = ipif;
15010 			if (best_type == IPIF_LOCALADDR)
15011 				break; /* can't get better */
15012 		}
15013 	} while ((ipif = next_ipif) != start_ipif);
15014 
15015 	if ((ipif = best_ipif) != NULL) {
15016 		mutex_enter(&ipif->ipif_ill->ill_lock);
15017 		if (IPIF_IS_CONDEMNED(ipif)) {
15018 			mutex_exit(&ipif->ipif_ill->ill_lock);
15019 			goto retry;
15020 		}
15021 		ipif_refhold_locked(ipif);
15022 
15023 		/*
15024 		 * For IPMP, update the source ipif rotor to the next ipif,
15025 		 * provided we can look it up.  (We must not use it if it's
15026 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15027 		 * ipif_free() checked ill_src_ipif.)
15028 		 */
15029 		if (IS_IPMP(ill) && ipif != NULL) {
15030 			next_ipif = ipif->ipif_next;
15031 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15032 				ill->ill_src_ipif = next_ipif;
15033 			else
15034 				ill->ill_src_ipif = NULL;
15035 		}
15036 		mutex_exit(&ipif->ipif_ill->ill_lock);
15037 	}
15038 
15039 	rw_exit(&ipst->ips_ill_g_lock);
15040 	if (usill != NULL)
15041 		ill_refrele(usill);
15042 	if (ipmp_ill != NULL)
15043 		ill_refrele(ipmp_ill);
15044 	if (dst_rhtp != NULL)
15045 		TPC_RELE(dst_rhtp);
15046 
15047 #ifdef DEBUG
15048 	if (ipif == NULL) {
15049 		char buf1[INET6_ADDRSTRLEN];
15050 
15051 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15052 		    ill->ill_name,
15053 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15054 	} else {
15055 		char buf1[INET6_ADDRSTRLEN];
15056 		char buf2[INET6_ADDRSTRLEN];
15057 
15058 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15059 		    ipif->ipif_ill->ill_name,
15060 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15061 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15062 		    buf2, sizeof (buf2))));
15063 	}
15064 #endif /* DEBUG */
15065 	return (ipif);
15066 }
15067 
15068 /*
15069  * Pick a source address based on the destination ill and an optional setsrc
15070  * address.
15071  * The result is stored in srcp. If generation is set, then put the source
15072  * generation number there before we look for the source address (to avoid
15073  * missing changes in the set of source addresses.
15074  * If flagsp is set, then us it to pass back ipif_flags.
15075  *
15076  * If the caller wants to cache the returned source address and detect when
15077  * that might be stale, the caller should pass in a generation argument,
15078  * which the caller can later compare against ips_src_generation
15079  *
15080  * The precedence order for selecting an IPv4 source address is:
15081  *  - RTF_SETSRC on the offlink ire always wins.
15082  *  - If usrsrc is set, swap the ill to be the usesrc one.
15083  *  - If IPMP is used on the ill, select a random address from the most
15084  *    preferred ones below:
15085  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15086  * 2. Not deprecated, not ALL_ZONES
15087  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15088  * 4. Not deprecated, ALL_ZONES
15089  * 5. If onlink destination, same subnet and deprecated
15090  * 6. Deprecated.
15091  *
15092  * We have lower preference for ALL_ZONES IP addresses,
15093  * as they pose problems with unlabeled destinations.
15094  *
15095  * Note that when multiple IP addresses match e.g., #1 we pick
15096  * the first one if IPMP is not in use. With IPMP we randomize.
15097  */
15098 int
15099 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15100     ipaddr_t multicast_ifaddr,
15101     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15102     uint32_t *generation, uint64_t *flagsp)
15103 {
15104 	ipif_t *ipif;
15105 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15106 
15107 	if (flagsp != NULL)
15108 		*flagsp = 0;
15109 
15110 	/*
15111 	 * Need to grab the generation number before we check to
15112 	 * avoid a race with a change to the set of local addresses.
15113 	 * No lock needed since the thread which updates the set of local
15114 	 * addresses use ipif/ill locks and exit those (hence a store memory
15115 	 * barrier) before doing the atomic increase of ips_src_generation.
15116 	 */
15117 	if (generation != NULL) {
15118 		*generation = ipst->ips_src_generation;
15119 	}
15120 
15121 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15122 		*srcp = multicast_ifaddr;
15123 		return (0);
15124 	}
15125 
15126 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15127 	if (setsrc != INADDR_ANY) {
15128 		*srcp = setsrc;
15129 		return (0);
15130 	}
15131 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15132 	if (ipif == NULL) {
15133 		if (notready)
15134 			return (ENETDOWN);
15135 		else
15136 			return (EADDRNOTAVAIL);
15137 	}
15138 	*srcp = ipif->ipif_lcl_addr;
15139 	if (flagsp != NULL)
15140 		*flagsp = ipif->ipif_flags;
15141 	ipif_refrele(ipif);
15142 	return (0);
15143 }
15144 
15145 /* ARGSUSED */
15146 int
15147 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15148 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15149 {
15150 	/*
15151 	 * ill_phyint_reinit merged the v4 and v6 into a single
15152 	 * ipsq.  We might not have been able to complete the
15153 	 * operation in ipif_set_values, if we could not become
15154 	 * exclusive.  If so restart it here.
15155 	 */
15156 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15157 }
15158 
15159 /*
15160  * Can operate on either a module or a driver queue.
15161  * Returns an error if not a module queue.
15162  */
15163 /* ARGSUSED */
15164 int
15165 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15166     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15167 {
15168 	queue_t		*q1 = q;
15169 	char 		*cp;
15170 	char		interf_name[LIFNAMSIZ];
15171 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15172 
15173 	if (q->q_next == NULL) {
15174 		ip1dbg((
15175 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15176 		return (EINVAL);
15177 	}
15178 
15179 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15180 		return (EALREADY);
15181 
15182 	do {
15183 		q1 = q1->q_next;
15184 	} while (q1->q_next);
15185 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15186 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15187 
15188 	/*
15189 	 * Here we are not going to delay the ioack until after
15190 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15191 	 * original ioctl message before sending the requests.
15192 	 */
15193 	return (ipif_set_values(q, mp, interf_name, &ppa));
15194 }
15195 
15196 /* ARGSUSED */
15197 int
15198 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15199     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15200 {
15201 	return (ENXIO);
15202 }
15203 
15204 /*
15205  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15206  * `irep'.  Returns a pointer to the next free `irep' entry
15207  * A mirror exists in ipif_delete_bcast_ires().
15208  *
15209  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15210  * done in ire_add.
15211  */
15212 static ire_t **
15213 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15214 {
15215 	ipaddr_t addr;
15216 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15217 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15218 	ill_t *ill = ipif->ipif_ill;
15219 	zoneid_t zoneid = ipif->ipif_zoneid;
15220 
15221 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15222 
15223 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15224 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15225 
15226 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15227 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15228 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15229 
15230 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15231 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15232 
15233 	/*
15234 	 * For backward compatibility, we create net broadcast IREs based on
15235 	 * the old "IP address class system", since some old machines only
15236 	 * respond to these class derived net broadcast.  However, we must not
15237 	 * create these net broadcast IREs if the subnetmask is shorter than
15238 	 * the IP address class based derived netmask.  Otherwise, we may
15239 	 * create a net broadcast address which is the same as an IP address
15240 	 * on the subnet -- and then TCP will refuse to talk to that address.
15241 	 */
15242 	if (netmask < subnetmask) {
15243 		addr = netmask & ipif->ipif_subnet;
15244 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15245 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15246 	}
15247 
15248 	/*
15249 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15250 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15251 	 * created.  Creating these broadcast IREs will only create confusion
15252 	 * as `addr' will be the same as the IP address.
15253 	 */
15254 	if (subnetmask != 0xFFFFFFFF) {
15255 		addr = ipif->ipif_subnet;
15256 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15257 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15258 	}
15259 
15260 	return (irep);
15261 }
15262 
15263 /*
15264  * Mirror of ipif_create_bcast_ires()
15265  */
15266 static void
15267 ipif_delete_bcast_ires(ipif_t *ipif)
15268 {
15269 	ipaddr_t	addr;
15270 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15271 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15272 	ill_t		*ill = ipif->ipif_ill;
15273 	zoneid_t	zoneid = ipif->ipif_zoneid;
15274 	ire_t		*ire;
15275 
15276 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15277 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15278 
15279 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15280 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15281 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15282 
15283 	ire = ire_lookup_bcast(ill, 0, zoneid);
15284 	ASSERT(ire != NULL);
15285 	ire_delete(ire); ire_refrele(ire);
15286 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15287 	ASSERT(ire != NULL);
15288 	ire_delete(ire); ire_refrele(ire);
15289 
15290 	/*
15291 	 * For backward compatibility, we create net broadcast IREs based on
15292 	 * the old "IP address class system", since some old machines only
15293 	 * respond to these class derived net broadcast.  However, we must not
15294 	 * create these net broadcast IREs if the subnetmask is shorter than
15295 	 * the IP address class based derived netmask.  Otherwise, we may
15296 	 * create a net broadcast address which is the same as an IP address
15297 	 * on the subnet -- and then TCP will refuse to talk to that address.
15298 	 */
15299 	if (netmask < subnetmask) {
15300 		addr = netmask & ipif->ipif_subnet;
15301 		ire = ire_lookup_bcast(ill, addr, zoneid);
15302 		ASSERT(ire != NULL);
15303 		ire_delete(ire); ire_refrele(ire);
15304 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15305 		ASSERT(ire != NULL);
15306 		ire_delete(ire); ire_refrele(ire);
15307 	}
15308 
15309 	/*
15310 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15311 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15312 	 * created.  Creating these broadcast IREs will only create confusion
15313 	 * as `addr' will be the same as the IP address.
15314 	 */
15315 	if (subnetmask != 0xFFFFFFFF) {
15316 		addr = ipif->ipif_subnet;
15317 		ire = ire_lookup_bcast(ill, addr, zoneid);
15318 		ASSERT(ire != NULL);
15319 		ire_delete(ire); ire_refrele(ire);
15320 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15321 		ASSERT(ire != NULL);
15322 		ire_delete(ire); ire_refrele(ire);
15323 	}
15324 }
15325 
15326 /*
15327  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15328  * from lifr_flags and the name from lifr_name.
15329  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15330  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15331  * Returns EINPROGRESS when mp has been consumed by queueing it on
15332  * ipx_pending_mp and the ioctl will complete in ip_rput.
15333  *
15334  * Can operate on either a module or a driver queue.
15335  * Returns an error if not a module queue.
15336  */
15337 /* ARGSUSED */
15338 int
15339 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15340     ip_ioctl_cmd_t *ipip, void *if_req)
15341 {
15342 	ill_t	*ill = q->q_ptr;
15343 	phyint_t *phyi;
15344 	ip_stack_t *ipst;
15345 	struct lifreq *lifr = if_req;
15346 	uint64_t new_flags;
15347 
15348 	ASSERT(ipif != NULL);
15349 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15350 
15351 	if (q->q_next == NULL) {
15352 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15353 		return (EINVAL);
15354 	}
15355 
15356 	/*
15357 	 * If we are not writer on 'q' then this interface exists already
15358 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15359 	 * so return EALREADY.
15360 	 */
15361 	if (ill != ipif->ipif_ill)
15362 		return (EALREADY);
15363 
15364 	if (ill->ill_name[0] != '\0')
15365 		return (EALREADY);
15366 
15367 	/*
15368 	 * If there's another ill already with the requested name, ensure
15369 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15370 	 * fuse together two unrelated ills, which will cause chaos.
15371 	 */
15372 	ipst = ill->ill_ipst;
15373 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15374 	    lifr->lifr_name, NULL);
15375 	if (phyi != NULL) {
15376 		ill_t *ill_mate = phyi->phyint_illv4;
15377 
15378 		if (ill_mate == NULL)
15379 			ill_mate = phyi->phyint_illv6;
15380 		ASSERT(ill_mate != NULL);
15381 
15382 		if (ill_mate->ill_media->ip_m_mac_type !=
15383 		    ill->ill_media->ip_m_mac_type) {
15384 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15385 			    "use the same ill name on differing media\n"));
15386 			return (EINVAL);
15387 		}
15388 	}
15389 
15390 	/*
15391 	 * We start off as IFF_IPV4 in ipif_allocate and become
15392 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15393 	 * The only flags that we read from user space are IFF_IPV4,
15394 	 * IFF_IPV6, and IFF_BROADCAST.
15395 	 *
15396 	 * This ill has not been inserted into the global list.
15397 	 * So we are still single threaded and don't need any lock
15398 	 *
15399 	 * Saniy check the flags.
15400 	 */
15401 
15402 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15403 	    ((lifr->lifr_flags & IFF_IPV6) ||
15404 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15405 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15406 		    "or IPv6 i.e., no broadcast \n"));
15407 		return (EINVAL);
15408 	}
15409 
15410 	new_flags =
15411 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15412 
15413 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15414 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15415 		    "IFF_IPV4 or IFF_IPV6\n"));
15416 		return (EINVAL);
15417 	}
15418 
15419 	/*
15420 	 * We always start off as IPv4, so only need to check for IPv6.
15421 	 */
15422 	if ((new_flags & IFF_IPV6) != 0) {
15423 		ill->ill_flags |= ILLF_IPV6;
15424 		ill->ill_flags &= ~ILLF_IPV4;
15425 	}
15426 
15427 	if ((new_flags & IFF_BROADCAST) != 0)
15428 		ipif->ipif_flags |= IPIF_BROADCAST;
15429 	else
15430 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15431 
15432 	/* We started off as V4. */
15433 	if (ill->ill_flags & ILLF_IPV6) {
15434 		ill->ill_phyint->phyint_illv6 = ill;
15435 		ill->ill_phyint->phyint_illv4 = NULL;
15436 	}
15437 
15438 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15439 }
15440 
15441 /* ARGSUSED */
15442 int
15443 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15444     ip_ioctl_cmd_t *ipip, void *if_req)
15445 {
15446 	/*
15447 	 * ill_phyint_reinit merged the v4 and v6 into a single
15448 	 * ipsq.  We might not have been able to complete the
15449 	 * slifname in ipif_set_values, if we could not become
15450 	 * exclusive.  If so restart it here
15451 	 */
15452 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15453 }
15454 
15455 /*
15456  * Return a pointer to the ipif which matches the index, IP version type and
15457  * zoneid.
15458  */
15459 ipif_t *
15460 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15461     ip_stack_t *ipst)
15462 {
15463 	ill_t	*ill;
15464 	ipif_t	*ipif = NULL;
15465 
15466 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15467 	if (ill != NULL) {
15468 		mutex_enter(&ill->ill_lock);
15469 		for (ipif = ill->ill_ipif; ipif != NULL;
15470 		    ipif = ipif->ipif_next) {
15471 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15472 			    zoneid == ipif->ipif_zoneid ||
15473 			    ipif->ipif_zoneid == ALL_ZONES)) {
15474 				ipif_refhold_locked(ipif);
15475 				break;
15476 			}
15477 		}
15478 		mutex_exit(&ill->ill_lock);
15479 		ill_refrele(ill);
15480 	}
15481 	return (ipif);
15482 }
15483 
15484 /*
15485  * Change an existing physical interface's index. If the new index
15486  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15487  * Finally, we update other systems which may have a dependence on the
15488  * index value.
15489  */
15490 /* ARGSUSED */
15491 int
15492 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15493     ip_ioctl_cmd_t *ipip, void *ifreq)
15494 {
15495 	ill_t		*ill;
15496 	phyint_t	*phyi;
15497 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15498 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15499 	uint_t	old_index, index;
15500 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15501 	avl_index_t	where;
15502 
15503 	if (ipip->ipi_cmd_type == IF_CMD)
15504 		index = ifr->ifr_index;
15505 	else
15506 		index = lifr->lifr_index;
15507 
15508 	/*
15509 	 * Only allow on physical interface. Also, index zero is illegal.
15510 	 */
15511 	ill = ipif->ipif_ill;
15512 	phyi = ill->ill_phyint;
15513 	if (ipif->ipif_id != 0 || index == 0) {
15514 		return (EINVAL);
15515 	}
15516 
15517 	/* If the index is not changing, no work to do */
15518 	if (phyi->phyint_ifindex == index)
15519 		return (0);
15520 
15521 	/*
15522 	 * Use phyint_exists() to determine if the new interface index
15523 	 * is already in use. If the index is unused then we need to
15524 	 * change the phyint's position in the phyint_list_avl_by_index
15525 	 * tree. If we do not do this, subsequent lookups (using the new
15526 	 * index value) will not find the phyint.
15527 	 */
15528 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15529 	if (phyint_exists(index, ipst)) {
15530 		rw_exit(&ipst->ips_ill_g_lock);
15531 		return (EEXIST);
15532 	}
15533 
15534 	/*
15535 	 * The new index is unused. Set it in the phyint. However we must not
15536 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15537 	 * changes. The event must be bound to old ifindex value.
15538 	 */
15539 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15540 	    &index, sizeof (index));
15541 
15542 	old_index = phyi->phyint_ifindex;
15543 	phyi->phyint_ifindex = index;
15544 
15545 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15546 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15547 	    &index, &where);
15548 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15549 	    phyi, where);
15550 	rw_exit(&ipst->ips_ill_g_lock);
15551 
15552 	/* Update SCTP's ILL list */
15553 	sctp_ill_reindex(ill, old_index);
15554 
15555 	/* Send the routing sockets message */
15556 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15557 	if (ILL_OTHER(ill))
15558 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15559 
15560 	/* Perhaps ilgs should use this ill */
15561 	update_conn_ill(NULL, ill->ill_ipst);
15562 	return (0);
15563 }
15564 
15565 /* ARGSUSED */
15566 int
15567 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15568     ip_ioctl_cmd_t *ipip, void *ifreq)
15569 {
15570 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15571 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15572 
15573 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15574 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15575 	/* Get the interface index */
15576 	if (ipip->ipi_cmd_type == IF_CMD) {
15577 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15578 	} else {
15579 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15580 	}
15581 	return (0);
15582 }
15583 
15584 /* ARGSUSED */
15585 int
15586 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15587     ip_ioctl_cmd_t *ipip, void *ifreq)
15588 {
15589 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15590 
15591 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15592 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15593 	/* Get the interface zone */
15594 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15595 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15596 	return (0);
15597 }
15598 
15599 /*
15600  * Set the zoneid of an interface.
15601  */
15602 /* ARGSUSED */
15603 int
15604 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15605     ip_ioctl_cmd_t *ipip, void *ifreq)
15606 {
15607 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15608 	int err = 0;
15609 	boolean_t need_up = B_FALSE;
15610 	zone_t *zptr;
15611 	zone_status_t status;
15612 	zoneid_t zoneid;
15613 
15614 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15615 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15616 		if (!is_system_labeled())
15617 			return (ENOTSUP);
15618 		zoneid = GLOBAL_ZONEID;
15619 	}
15620 
15621 	/* cannot assign instance zero to a non-global zone */
15622 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15623 		return (ENOTSUP);
15624 
15625 	/*
15626 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15627 	 * the event of a race with the zone shutdown processing, since IP
15628 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15629 	 * interface will be cleaned up even if the zone is shut down
15630 	 * immediately after the status check. If the interface can't be brought
15631 	 * down right away, and the zone is shut down before the restart
15632 	 * function is called, we resolve the possible races by rechecking the
15633 	 * zone status in the restart function.
15634 	 */
15635 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15636 		return (EINVAL);
15637 	status = zone_status_get(zptr);
15638 	zone_rele(zptr);
15639 
15640 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15641 		return (EINVAL);
15642 
15643 	if (ipif->ipif_flags & IPIF_UP) {
15644 		/*
15645 		 * If the interface is already marked up,
15646 		 * we call ipif_down which will take care
15647 		 * of ditching any IREs that have been set
15648 		 * up based on the old interface address.
15649 		 */
15650 		err = ipif_logical_down(ipif, q, mp);
15651 		if (err == EINPROGRESS)
15652 			return (err);
15653 		(void) ipif_down_tail(ipif);
15654 		need_up = B_TRUE;
15655 	}
15656 
15657 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15658 	return (err);
15659 }
15660 
15661 static int
15662 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15663     queue_t *q, mblk_t *mp, boolean_t need_up)
15664 {
15665 	int	err = 0;
15666 	ip_stack_t	*ipst;
15667 
15668 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15669 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15670 
15671 	if (CONN_Q(q))
15672 		ipst = CONNQ_TO_IPST(q);
15673 	else
15674 		ipst = ILLQ_TO_IPST(q);
15675 
15676 	/*
15677 	 * For exclusive stacks we don't allow a different zoneid than
15678 	 * global.
15679 	 */
15680 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15681 	    zoneid != GLOBAL_ZONEID)
15682 		return (EINVAL);
15683 
15684 	/* Set the new zone id. */
15685 	ipif->ipif_zoneid = zoneid;
15686 
15687 	/* Update sctp list */
15688 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15689 
15690 	/* The default multicast interface might have changed */
15691 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15692 
15693 	if (need_up) {
15694 		/*
15695 		 * Now bring the interface back up.  If this
15696 		 * is the only IPIF for the ILL, ipif_up
15697 		 * will have to re-bind to the device, so
15698 		 * we may get back EINPROGRESS, in which
15699 		 * case, this IOCTL will get completed in
15700 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15701 		 */
15702 		err = ipif_up(ipif, q, mp);
15703 	}
15704 	return (err);
15705 }
15706 
15707 /* ARGSUSED */
15708 int
15709 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15710     ip_ioctl_cmd_t *ipip, void *if_req)
15711 {
15712 	struct lifreq *lifr = (struct lifreq *)if_req;
15713 	zoneid_t zoneid;
15714 	zone_t *zptr;
15715 	zone_status_t status;
15716 
15717 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15718 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15719 		zoneid = GLOBAL_ZONEID;
15720 
15721 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15723 
15724 	/*
15725 	 * We recheck the zone status to resolve the following race condition:
15726 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15727 	 * 2) hme0:1 is up and can't be brought down right away;
15728 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15729 	 * 3) zone "myzone" is halted; the zone status switches to
15730 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15731 	 * the interfaces to remove - hme0:1 is not returned because it's not
15732 	 * yet in "myzone", so it won't be removed;
15733 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15734 	 * status check here, we would have hme0:1 in "myzone" after it's been
15735 	 * destroyed.
15736 	 * Note that if the status check fails, we need to bring the interface
15737 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15738 	 * ipif_up_done[_v6]().
15739 	 */
15740 	status = ZONE_IS_UNINITIALIZED;
15741 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15742 		status = zone_status_get(zptr);
15743 		zone_rele(zptr);
15744 	}
15745 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15746 		if (ipif->ipif_isv6) {
15747 			(void) ipif_up_done_v6(ipif);
15748 		} else {
15749 			(void) ipif_up_done(ipif);
15750 		}
15751 		return (EINVAL);
15752 	}
15753 
15754 	(void) ipif_down_tail(ipif);
15755 
15756 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15757 	    B_TRUE));
15758 }
15759 
15760 /*
15761  * Return the number of addresses on `ill' with one or more of the values
15762  * in `set' set and all of the values in `clear' clear.
15763  */
15764 static uint_t
15765 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15766 {
15767 	ipif_t	*ipif;
15768 	uint_t	cnt = 0;
15769 
15770 	ASSERT(IAM_WRITER_ILL(ill));
15771 
15772 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15773 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15774 			cnt++;
15775 
15776 	return (cnt);
15777 }
15778 
15779 /*
15780  * Return the number of migratable addresses on `ill' that are under
15781  * application control.
15782  */
15783 uint_t
15784 ill_appaddr_cnt(const ill_t *ill)
15785 {
15786 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15787 	    IPIF_NOFAILOVER));
15788 }
15789 
15790 /*
15791  * Return the number of point-to-point addresses on `ill'.
15792  */
15793 uint_t
15794 ill_ptpaddr_cnt(const ill_t *ill)
15795 {
15796 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15797 }
15798 
15799 /* ARGSUSED */
15800 int
15801 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15802 	ip_ioctl_cmd_t *ipip, void *ifreq)
15803 {
15804 	struct lifreq	*lifr = ifreq;
15805 
15806 	ASSERT(q->q_next == NULL);
15807 	ASSERT(CONN_Q(q));
15808 
15809 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15810 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15811 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15812 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15813 
15814 	return (0);
15815 }
15816 
15817 /* Find the previous ILL in this usesrc group */
15818 static ill_t *
15819 ill_prev_usesrc(ill_t *uill)
15820 {
15821 	ill_t *ill;
15822 
15823 	for (ill = uill->ill_usesrc_grp_next;
15824 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15825 	    ill = ill->ill_usesrc_grp_next)
15826 		/* do nothing */;
15827 	return (ill);
15828 }
15829 
15830 /*
15831  * Release all members of the usesrc group. This routine is called
15832  * from ill_delete when the interface being unplumbed is the
15833  * group head.
15834  *
15835  * This silently clears the usesrc that ifconfig setup.
15836  * An alternative would be to keep that ifindex, and drop packets on the floor
15837  * since no source address can be selected.
15838  * Even if we keep the current semantics, don't need a lock and a linked list.
15839  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15840  * the one that is being removed. Issue is how we return the usesrc users
15841  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15842  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15843  * ill walk, but the walker would need to insert in the ioctl response.
15844  */
15845 static void
15846 ill_disband_usesrc_group(ill_t *uill)
15847 {
15848 	ill_t *next_ill, *tmp_ill;
15849 	ip_stack_t	*ipst = uill->ill_ipst;
15850 
15851 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15852 	next_ill = uill->ill_usesrc_grp_next;
15853 
15854 	do {
15855 		ASSERT(next_ill != NULL);
15856 		tmp_ill = next_ill->ill_usesrc_grp_next;
15857 		ASSERT(tmp_ill != NULL);
15858 		next_ill->ill_usesrc_grp_next = NULL;
15859 		next_ill->ill_usesrc_ifindex = 0;
15860 		next_ill = tmp_ill;
15861 	} while (next_ill->ill_usesrc_ifindex != 0);
15862 	uill->ill_usesrc_grp_next = NULL;
15863 }
15864 
15865 /*
15866  * Remove the client usesrc ILL from the list and relink to a new list
15867  */
15868 int
15869 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15870 {
15871 	ill_t *ill, *tmp_ill;
15872 	ip_stack_t	*ipst = ucill->ill_ipst;
15873 
15874 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15875 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15876 
15877 	/*
15878 	 * Check if the usesrc client ILL passed in is not already
15879 	 * in use as a usesrc ILL i.e one whose source address is
15880 	 * in use OR a usesrc ILL is not already in use as a usesrc
15881 	 * client ILL
15882 	 */
15883 	if ((ucill->ill_usesrc_ifindex == 0) ||
15884 	    (uill->ill_usesrc_ifindex != 0)) {
15885 		return (-1);
15886 	}
15887 
15888 	ill = ill_prev_usesrc(ucill);
15889 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15890 
15891 	/* Remove from the current list */
15892 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15893 		/* Only two elements in the list */
15894 		ASSERT(ill->ill_usesrc_ifindex == 0);
15895 		ill->ill_usesrc_grp_next = NULL;
15896 	} else {
15897 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15898 	}
15899 
15900 	if (ifindex == 0) {
15901 		ucill->ill_usesrc_ifindex = 0;
15902 		ucill->ill_usesrc_grp_next = NULL;
15903 		return (0);
15904 	}
15905 
15906 	ucill->ill_usesrc_ifindex = ifindex;
15907 	tmp_ill = uill->ill_usesrc_grp_next;
15908 	uill->ill_usesrc_grp_next = ucill;
15909 	ucill->ill_usesrc_grp_next =
15910 	    (tmp_ill != NULL) ? tmp_ill : uill;
15911 	return (0);
15912 }
15913 
15914 /*
15915  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15916  * ip.c for locking details.
15917  */
15918 /* ARGSUSED */
15919 int
15920 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15921     ip_ioctl_cmd_t *ipip, void *ifreq)
15922 {
15923 	struct lifreq *lifr = (struct lifreq *)ifreq;
15924 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15925 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15926 	int err = 0, ret;
15927 	uint_t ifindex;
15928 	ipsq_t *ipsq = NULL;
15929 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15930 
15931 	ASSERT(IAM_WRITER_IPIF(ipif));
15932 	ASSERT(q->q_next == NULL);
15933 	ASSERT(CONN_Q(q));
15934 
15935 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15936 
15937 	ifindex = lifr->lifr_index;
15938 	if (ifindex == 0) {
15939 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15940 			/* non usesrc group interface, nothing to reset */
15941 			return (0);
15942 		}
15943 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15944 		/* valid reset request */
15945 		reset_flg = B_TRUE;
15946 	}
15947 
15948 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15949 	if (usesrc_ill == NULL) {
15950 		return (ENXIO);
15951 	}
15952 
15953 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15954 	    NEW_OP, B_TRUE);
15955 	if (ipsq == NULL) {
15956 		err = EINPROGRESS;
15957 		/* Operation enqueued on the ipsq of the usesrc ILL */
15958 		goto done;
15959 	}
15960 
15961 	/* USESRC isn't currently supported with IPMP */
15962 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15963 		err = ENOTSUP;
15964 		goto done;
15965 	}
15966 
15967 	/*
15968 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15969 	 * used by IPMP underlying interfaces, but someone might think it's
15970 	 * more general and try to use it independently with VNI.)
15971 	 */
15972 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15973 		err = ENOTSUP;
15974 		goto done;
15975 	}
15976 
15977 	/*
15978 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15979 	 * already a client then return EINVAL
15980 	 */
15981 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15982 		err = EINVAL;
15983 		goto done;
15984 	}
15985 
15986 	/*
15987 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15988 	 * be then this is a duplicate operation.
15989 	 */
15990 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15991 		err = 0;
15992 		goto done;
15993 	}
15994 
15995 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15996 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15997 	    usesrc_ill->ill_isv6));
15998 
15999 	/*
16000 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16001 	 * and the ill_usesrc_ifindex fields
16002 	 */
16003 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16004 
16005 	if (reset_flg) {
16006 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16007 		if (ret != 0) {
16008 			err = EINVAL;
16009 		}
16010 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16011 		goto done;
16012 	}
16013 
16014 	/*
16015 	 * Four possibilities to consider:
16016 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16017 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16018 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16019 	 * 4. Both are part of their respective usesrc groups
16020 	 */
16021 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16022 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16023 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16024 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16025 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16026 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16027 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16028 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16029 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16030 		/* Insert at head of list */
16031 		usesrc_cli_ill->ill_usesrc_grp_next =
16032 		    usesrc_ill->ill_usesrc_grp_next;
16033 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16034 	} else {
16035 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16036 		    ifindex);
16037 		if (ret != 0)
16038 			err = EINVAL;
16039 	}
16040 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16041 
16042 done:
16043 	if (ipsq != NULL)
16044 		ipsq_exit(ipsq);
16045 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16046 	ill_refrele(usesrc_ill);
16047 
16048 	/* Let conn_ixa caching know that source address selection changed */
16049 	ip_update_source_selection(ipst);
16050 
16051 	return (err);
16052 }
16053 
16054 /*
16055  * comparison function used by avl.
16056  */
16057 static int
16058 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16059 {
16060 
16061 	uint_t index;
16062 
16063 	ASSERT(phyip != NULL && index_ptr != NULL);
16064 
16065 	index = *((uint_t *)index_ptr);
16066 	/*
16067 	 * let the phyint with the lowest index be on top.
16068 	 */
16069 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16070 		return (1);
16071 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16072 		return (-1);
16073 	return (0);
16074 }
16075 
16076 /*
16077  * comparison function used by avl.
16078  */
16079 static int
16080 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16081 {
16082 	ill_t *ill;
16083 	int res = 0;
16084 
16085 	ASSERT(phyip != NULL && name_ptr != NULL);
16086 
16087 	if (((phyint_t *)phyip)->phyint_illv4)
16088 		ill = ((phyint_t *)phyip)->phyint_illv4;
16089 	else
16090 		ill = ((phyint_t *)phyip)->phyint_illv6;
16091 	ASSERT(ill != NULL);
16092 
16093 	res = strcmp(ill->ill_name, (char *)name_ptr);
16094 	if (res > 0)
16095 		return (1);
16096 	else if (res < 0)
16097 		return (-1);
16098 	return (0);
16099 }
16100 
16101 /*
16102  * This function is called on the unplumb path via ill_glist_delete() when
16103  * there are no ills left on the phyint and thus the phyint can be freed.
16104  */
16105 static void
16106 phyint_free(phyint_t *phyi)
16107 {
16108 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16109 
16110 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16111 
16112 	/*
16113 	 * If this phyint was an IPMP meta-interface, blow away the group.
16114 	 * This is safe to do because all of the illgrps have already been
16115 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16116 	 * If we're cleaning up as a result of failed initialization,
16117 	 * phyint_grp may be NULL.
16118 	 */
16119 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16120 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16121 		ipmp_grp_destroy(phyi->phyint_grp);
16122 		phyi->phyint_grp = NULL;
16123 		rw_exit(&ipst->ips_ipmp_lock);
16124 	}
16125 
16126 	/*
16127 	 * If this interface was under IPMP, take it out of the group.
16128 	 */
16129 	if (phyi->phyint_grp != NULL)
16130 		ipmp_phyint_leave_grp(phyi);
16131 
16132 	/*
16133 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16134 	 * will be freed in ipsq_exit().
16135 	 */
16136 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16137 	phyi->phyint_name[0] = '\0';
16138 
16139 	mi_free(phyi);
16140 }
16141 
16142 /*
16143  * Attach the ill to the phyint structure which can be shared by both
16144  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16145  * function is called from ipif_set_values and ill_lookup_on_name (for
16146  * loopback) where we know the name of the ill. We lookup the ill and if
16147  * there is one present already with the name use that phyint. Otherwise
16148  * reuse the one allocated by ill_init.
16149  */
16150 static void
16151 ill_phyint_reinit(ill_t *ill)
16152 {
16153 	boolean_t isv6 = ill->ill_isv6;
16154 	phyint_t *phyi_old;
16155 	phyint_t *phyi;
16156 	avl_index_t where = 0;
16157 	ill_t	*ill_other = NULL;
16158 	ip_stack_t	*ipst = ill->ill_ipst;
16159 
16160 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16161 
16162 	phyi_old = ill->ill_phyint;
16163 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16164 	    phyi_old->phyint_illv6 == NULL));
16165 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16166 	    phyi_old->phyint_illv4 == NULL));
16167 	ASSERT(phyi_old->phyint_ifindex == 0);
16168 
16169 	/*
16170 	 * Now that our ill has a name, set it in the phyint.
16171 	 */
16172 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16173 
16174 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16175 	    ill->ill_name, &where);
16176 
16177 	/*
16178 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16179 	 *    the global list of ills. So no other thread could have located
16180 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16181 	 * 2. Now locate the other protocol instance of this ill.
16182 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16183 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16184 	 *    of neither ill can change.
16185 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16186 	 *    other ill.
16187 	 * 5. Release all locks.
16188 	 */
16189 
16190 	/*
16191 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16192 	 * we are initializing IPv4.
16193 	 */
16194 	if (phyi != NULL) {
16195 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16196 		ASSERT(ill_other->ill_phyint != NULL);
16197 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16198 		    (!isv6 && ill_other->ill_isv6));
16199 		GRAB_ILL_LOCKS(ill, ill_other);
16200 		/*
16201 		 * We are potentially throwing away phyint_flags which
16202 		 * could be different from the one that we obtain from
16203 		 * ill_other->ill_phyint. But it is okay as we are assuming
16204 		 * that the state maintained within IP is correct.
16205 		 */
16206 		mutex_enter(&phyi->phyint_lock);
16207 		if (isv6) {
16208 			ASSERT(phyi->phyint_illv6 == NULL);
16209 			phyi->phyint_illv6 = ill;
16210 		} else {
16211 			ASSERT(phyi->phyint_illv4 == NULL);
16212 			phyi->phyint_illv4 = ill;
16213 		}
16214 
16215 		/*
16216 		 * Delete the old phyint and make its ipsq eligible
16217 		 * to be freed in ipsq_exit().
16218 		 */
16219 		phyi_old->phyint_illv4 = NULL;
16220 		phyi_old->phyint_illv6 = NULL;
16221 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16222 		phyi_old->phyint_name[0] = '\0';
16223 		mi_free(phyi_old);
16224 	} else {
16225 		mutex_enter(&ill->ill_lock);
16226 		/*
16227 		 * We don't need to acquire any lock, since
16228 		 * the ill is not yet visible globally  and we
16229 		 * have not yet released the ill_g_lock.
16230 		 */
16231 		phyi = phyi_old;
16232 		mutex_enter(&phyi->phyint_lock);
16233 		/* XXX We need a recovery strategy here. */
16234 		if (!phyint_assign_ifindex(phyi, ipst))
16235 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16236 
16237 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16238 		    (void *)phyi, where);
16239 
16240 		(void) avl_find(&ipst->ips_phyint_g_list->
16241 		    phyint_list_avl_by_index,
16242 		    &phyi->phyint_ifindex, &where);
16243 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16244 		    (void *)phyi, where);
16245 	}
16246 
16247 	/*
16248 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16249 	 * pending mp is not affected because that is per ill basis.
16250 	 */
16251 	ill->ill_phyint = phyi;
16252 
16253 	/*
16254 	 * Now that the phyint's ifindex has been assigned, complete the
16255 	 * remaining
16256 	 */
16257 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16258 	if (ill->ill_isv6) {
16259 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16260 		    ill->ill_phyint->phyint_ifindex;
16261 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16262 	} else {
16263 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16264 	}
16265 
16266 	/*
16267 	 * Generate an event within the hooks framework to indicate that
16268 	 * a new interface has just been added to IP.  For this event to
16269 	 * be generated, the network interface must, at least, have an
16270 	 * ifindex assigned to it.  (We don't generate the event for
16271 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16272 	 *
16273 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16274 	 * that the ordering of delivered events to listeners matches the
16275 	 * order of them in the kernel.
16276 	 */
16277 	if (!IS_LOOPBACK(ill)) {
16278 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16279 		    ill->ill_name_length);
16280 	}
16281 	RELEASE_ILL_LOCKS(ill, ill_other);
16282 	mutex_exit(&phyi->phyint_lock);
16283 }
16284 
16285 /*
16286  * Notify any downstream modules of the name of this interface.
16287  * An M_IOCTL is used even though we don't expect a successful reply.
16288  * Any reply message from the driver (presumably an M_IOCNAK) will
16289  * eventually get discarded somewhere upstream.  The message format is
16290  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16291  * to IP.
16292  */
16293 static void
16294 ip_ifname_notify(ill_t *ill, queue_t *q)
16295 {
16296 	mblk_t *mp1, *mp2;
16297 	struct iocblk *iocp;
16298 	struct lifreq *lifr;
16299 
16300 	mp1 = mkiocb(SIOCSLIFNAME);
16301 	if (mp1 == NULL)
16302 		return;
16303 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16304 	if (mp2 == NULL) {
16305 		freeb(mp1);
16306 		return;
16307 	}
16308 
16309 	mp1->b_cont = mp2;
16310 	iocp = (struct iocblk *)mp1->b_rptr;
16311 	iocp->ioc_count = sizeof (struct lifreq);
16312 
16313 	lifr = (struct lifreq *)mp2->b_rptr;
16314 	mp2->b_wptr += sizeof (struct lifreq);
16315 	bzero(lifr, sizeof (struct lifreq));
16316 
16317 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16318 	lifr->lifr_ppa = ill->ill_ppa;
16319 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16320 
16321 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16322 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16323 	putnext(q, mp1);
16324 }
16325 
16326 static int
16327 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16328 {
16329 	int		err;
16330 	ip_stack_t	*ipst = ill->ill_ipst;
16331 	phyint_t	*phyi = ill->ill_phyint;
16332 
16333 	/* Set the obsolete NDD per-interface forwarding name. */
16334 	err = ill_set_ndd_name(ill);
16335 	if (err != 0) {
16336 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16337 		    err);
16338 	}
16339 
16340 	/*
16341 	 * Now that ill_name is set, the configuration for the IPMP
16342 	 * meta-interface can be performed.
16343 	 */
16344 	if (IS_IPMP(ill)) {
16345 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16346 		/*
16347 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16348 		 * meta-interface and we need to create the IPMP group.
16349 		 */
16350 		if (phyi->phyint_grp == NULL) {
16351 			/*
16352 			 * If someone has renamed another IPMP group to have
16353 			 * the same name as our interface, bail.
16354 			 */
16355 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16356 				rw_exit(&ipst->ips_ipmp_lock);
16357 				return (EEXIST);
16358 			}
16359 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16360 			if (phyi->phyint_grp == NULL) {
16361 				rw_exit(&ipst->ips_ipmp_lock);
16362 				return (ENOMEM);
16363 			}
16364 		}
16365 		rw_exit(&ipst->ips_ipmp_lock);
16366 	}
16367 
16368 	/* Tell downstream modules where they are. */
16369 	ip_ifname_notify(ill, q);
16370 
16371 	/*
16372 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16373 	 * Error cases are ENOMEM ...
16374 	 */
16375 	err = ill_dl_phys(ill, ipif, mp, q);
16376 
16377 	if (ill->ill_isv6) {
16378 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16379 		if (ipst->ips_mld_slowtimeout_id == 0) {
16380 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16381 			    (void *)ipst,
16382 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16383 		}
16384 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16385 	} else {
16386 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16387 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16388 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16389 			    (void *)ipst,
16390 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16391 		}
16392 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16393 	}
16394 
16395 	return (err);
16396 }
16397 
16398 /*
16399  * Common routine for ppa and ifname setting. Should be called exclusive.
16400  *
16401  * Returns EINPROGRESS when mp has been consumed by queueing it on
16402  * ipx_pending_mp and the ioctl will complete in ip_rput.
16403  *
16404  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16405  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16406  * For SLIFNAME, we pass these values back to the userland.
16407  */
16408 static int
16409 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16410 {
16411 	ill_t	*ill;
16412 	ipif_t	*ipif;
16413 	ipsq_t	*ipsq;
16414 	char	*ppa_ptr;
16415 	char	*old_ptr;
16416 	char	old_char;
16417 	int	error;
16418 	ip_stack_t	*ipst;
16419 
16420 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16421 	ASSERT(q->q_next != NULL);
16422 	ASSERT(interf_name != NULL);
16423 
16424 	ill = (ill_t *)q->q_ptr;
16425 	ipst = ill->ill_ipst;
16426 
16427 	ASSERT(ill->ill_ipst != NULL);
16428 	ASSERT(ill->ill_name[0] == '\0');
16429 	ASSERT(IAM_WRITER_ILL(ill));
16430 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16431 	ASSERT(ill->ill_ppa == UINT_MAX);
16432 
16433 	ill->ill_defend_start = ill->ill_defend_count = 0;
16434 	/* The ppa is sent down by ifconfig or is chosen */
16435 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16436 		return (EINVAL);
16437 	}
16438 
16439 	/*
16440 	 * make sure ppa passed in is same as ppa in the name.
16441 	 * This check is not made when ppa == UINT_MAX in that case ppa
16442 	 * in the name could be anything. System will choose a ppa and
16443 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16444 	 */
16445 	if (*new_ppa_ptr != UINT_MAX) {
16446 		/* stoi changes the pointer */
16447 		old_ptr = ppa_ptr;
16448 		/*
16449 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16450 		 * (they don't have an externally visible ppa).  We assign one
16451 		 * here so that we can manage the interface.  Note that in
16452 		 * the past this value was always 0 for DLPI 1 drivers.
16453 		 */
16454 		if (*new_ppa_ptr == 0)
16455 			*new_ppa_ptr = stoi(&old_ptr);
16456 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16457 			return (EINVAL);
16458 	}
16459 	/*
16460 	 * terminate string before ppa
16461 	 * save char at that location.
16462 	 */
16463 	old_char = ppa_ptr[0];
16464 	ppa_ptr[0] = '\0';
16465 
16466 	ill->ill_ppa = *new_ppa_ptr;
16467 	/*
16468 	 * Finish as much work now as possible before calling ill_glist_insert
16469 	 * which makes the ill globally visible and also merges it with the
16470 	 * other protocol instance of this phyint. The remaining work is
16471 	 * done after entering the ipsq which may happen sometime later.
16472 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16473 	 */
16474 	ipif = ill->ill_ipif;
16475 
16476 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16477 	ipif_assign_seqid(ipif);
16478 
16479 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16480 		ill->ill_flags |= ILLF_IPV4;
16481 
16482 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16483 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16484 
16485 	if (ill->ill_flags & ILLF_IPV6) {
16486 
16487 		ill->ill_isv6 = B_TRUE;
16488 		ill_set_inputfn(ill);
16489 		if (ill->ill_rq != NULL) {
16490 			ill->ill_rq->q_qinfo = &iprinitv6;
16491 		}
16492 
16493 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16494 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16495 		ipif->ipif_v6subnet = ipv6_all_zeros;
16496 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16497 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16498 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16499 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16500 		/*
16501 		 * point-to-point or Non-mulicast capable
16502 		 * interfaces won't do NUD unless explicitly
16503 		 * configured to do so.
16504 		 */
16505 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16506 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16507 			ill->ill_flags |= ILLF_NONUD;
16508 		}
16509 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16510 		if (ill->ill_flags & ILLF_NOARP) {
16511 			/*
16512 			 * Note: xresolv interfaces will eventually need
16513 			 * NOARP set here as well, but that will require
16514 			 * those external resolvers to have some
16515 			 * knowledge of that flag and act appropriately.
16516 			 * Not to be changed at present.
16517 			 */
16518 			ill->ill_flags &= ~ILLF_NOARP;
16519 		}
16520 		/*
16521 		 * Set the ILLF_ROUTER flag according to the global
16522 		 * IPv6 forwarding policy.
16523 		 */
16524 		if (ipst->ips_ipv6_forward != 0)
16525 			ill->ill_flags |= ILLF_ROUTER;
16526 	} else if (ill->ill_flags & ILLF_IPV4) {
16527 		ill->ill_isv6 = B_FALSE;
16528 		ill_set_inputfn(ill);
16529 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16530 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16531 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16532 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16533 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16534 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16535 		/*
16536 		 * Set the ILLF_ROUTER flag according to the global
16537 		 * IPv4 forwarding policy.
16538 		 */
16539 		if (ipst->ips_ip_g_forward != 0)
16540 			ill->ill_flags |= ILLF_ROUTER;
16541 	}
16542 
16543 	ASSERT(ill->ill_phyint != NULL);
16544 
16545 	/*
16546 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16547 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16548 	 */
16549 	if (!ill_allocate_mibs(ill))
16550 		return (ENOMEM);
16551 
16552 	/*
16553 	 * Pick a default sap until we get the DL_INFO_ACK back from
16554 	 * the driver.
16555 	 */
16556 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16557 	    ill->ill_media->ip_m_ipv4sap;
16558 
16559 	ill->ill_ifname_pending = 1;
16560 	ill->ill_ifname_pending_err = 0;
16561 
16562 	/*
16563 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16564 	 * that were joined while this ill was not bound to the DLPI link need
16565 	 * to be recovered by ill_recover_multicast().
16566 	 */
16567 	ill->ill_need_recover_multicast = 1;
16568 
16569 	ill_refhold(ill);
16570 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16571 	if ((error = ill_glist_insert(ill, interf_name,
16572 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16573 		ill->ill_ppa = UINT_MAX;
16574 		ill->ill_name[0] = '\0';
16575 		/*
16576 		 * undo null termination done above.
16577 		 */
16578 		ppa_ptr[0] = old_char;
16579 		rw_exit(&ipst->ips_ill_g_lock);
16580 		ill_refrele(ill);
16581 		return (error);
16582 	}
16583 
16584 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16585 
16586 	/*
16587 	 * When we return the buffer pointed to by interf_name should contain
16588 	 * the same name as in ill_name.
16589 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16590 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16591 	 * so copy full name and update the ppa ptr.
16592 	 * When ppa passed in != UINT_MAX all values are correct just undo
16593 	 * null termination, this saves a bcopy.
16594 	 */
16595 	if (*new_ppa_ptr == UINT_MAX) {
16596 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16597 		*new_ppa_ptr = ill->ill_ppa;
16598 	} else {
16599 		/*
16600 		 * undo null termination done above.
16601 		 */
16602 		ppa_ptr[0] = old_char;
16603 	}
16604 
16605 	/* Let SCTP know about this ILL */
16606 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16607 
16608 	/*
16609 	 * ill_glist_insert has made the ill visible globally, and
16610 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16611 	 * we need to hold the ips_ill_g_lock across the call to enter the
16612 	 * ipsq to enforce atomicity and prevent reordering. In the event
16613 	 * the ipsq has changed, and if the new ipsq is currently busy,
16614 	 * we need to make sure that this half-completed ioctl is ahead of
16615 	 * any subsequent ioctl. We achieve this by not dropping the
16616 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16617 	 * ensuring that new ioctls can't start.
16618 	 */
16619 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16620 	    B_TRUE);
16621 
16622 	rw_exit(&ipst->ips_ill_g_lock);
16623 	ill_refrele(ill);
16624 	if (ipsq == NULL)
16625 		return (EINPROGRESS);
16626 
16627 	/*
16628 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16629 	 */
16630 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16631 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16632 	else
16633 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16634 
16635 	error = ipif_set_values_tail(ill, ipif, mp, q);
16636 	ipsq_exit(ipsq);
16637 	if (error != 0 && error != EINPROGRESS) {
16638 		/*
16639 		 * restore previous values
16640 		 */
16641 		ill->ill_isv6 = B_FALSE;
16642 		ill_set_inputfn(ill);
16643 	}
16644 	return (error);
16645 }
16646 
16647 void
16648 ipif_init(ip_stack_t *ipst)
16649 {
16650 	int i;
16651 
16652 	for (i = 0; i < MAX_G_HEADS; i++) {
16653 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16654 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16655 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16656 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16657 	}
16658 
16659 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16660 	    ill_phyint_compare_index,
16661 	    sizeof (phyint_t),
16662 	    offsetof(struct phyint, phyint_avl_by_index));
16663 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16664 	    ill_phyint_compare_name,
16665 	    sizeof (phyint_t),
16666 	    offsetof(struct phyint, phyint_avl_by_name));
16667 }
16668 
16669 /*
16670  * Save enough information so that we can recreate the IRE if
16671  * the interface goes down and then up.
16672  */
16673 void
16674 ill_save_ire(ill_t *ill, ire_t *ire)
16675 {
16676 	mblk_t	*save_mp;
16677 
16678 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16679 	if (save_mp != NULL) {
16680 		ifrt_t	*ifrt;
16681 
16682 		save_mp->b_wptr += sizeof (ifrt_t);
16683 		ifrt = (ifrt_t *)save_mp->b_rptr;
16684 		bzero(ifrt, sizeof (ifrt_t));
16685 		ifrt->ifrt_type = ire->ire_type;
16686 		if (ire->ire_ipversion == IPV4_VERSION) {
16687 			ASSERT(!ill->ill_isv6);
16688 			ifrt->ifrt_addr = ire->ire_addr;
16689 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16690 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16691 			ifrt->ifrt_mask = ire->ire_mask;
16692 		} else {
16693 			ASSERT(ill->ill_isv6);
16694 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16695 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16696 			mutex_enter(&ire->ire_lock);
16697 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16698 			mutex_exit(&ire->ire_lock);
16699 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16700 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16701 		}
16702 		ifrt->ifrt_flags = ire->ire_flags;
16703 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16704 		mutex_enter(&ill->ill_saved_ire_lock);
16705 		save_mp->b_cont = ill->ill_saved_ire_mp;
16706 		ill->ill_saved_ire_mp = save_mp;
16707 		ill->ill_saved_ire_cnt++;
16708 		mutex_exit(&ill->ill_saved_ire_lock);
16709 	}
16710 }
16711 
16712 /*
16713  * Remove one entry from ill_saved_ire_mp.
16714  */
16715 void
16716 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16717 {
16718 	mblk_t	**mpp;
16719 	mblk_t	*mp;
16720 	ifrt_t	*ifrt;
16721 
16722 	/* Remove from ill_saved_ire_mp list if it is there */
16723 	mutex_enter(&ill->ill_saved_ire_lock);
16724 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16725 	    mpp = &(*mpp)->b_cont) {
16726 		in6_addr_t	gw_addr_v6;
16727 
16728 		/*
16729 		 * On a given ill, the tuple of address, gateway, mask,
16730 		 * ire_type, and zoneid is unique for each saved IRE.
16731 		 */
16732 		mp = *mpp;
16733 		ifrt = (ifrt_t *)mp->b_rptr;
16734 		/* ire_gateway_addr_v6 can change - need lock */
16735 		mutex_enter(&ire->ire_lock);
16736 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16737 		mutex_exit(&ire->ire_lock);
16738 
16739 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16740 		    ifrt->ifrt_type != ire->ire_type)
16741 			continue;
16742 
16743 		if (ill->ill_isv6 ?
16744 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16745 		    &ire->ire_addr_v6) &&
16746 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16747 		    &gw_addr_v6) &&
16748 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16749 		    &ire->ire_mask_v6)) :
16750 		    (ifrt->ifrt_addr == ire->ire_addr &&
16751 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16752 		    ifrt->ifrt_mask == ire->ire_mask)) {
16753 			*mpp = mp->b_cont;
16754 			ill->ill_saved_ire_cnt--;
16755 			freeb(mp);
16756 			break;
16757 		}
16758 	}
16759 	mutex_exit(&ill->ill_saved_ire_lock);
16760 }
16761 
16762 /*
16763  * IP multirouting broadcast routes handling
16764  * Append CGTP broadcast IREs to regular ones created
16765  * at ifconfig time.
16766  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16767  * the destination and the gateway are broadcast addresses.
16768  * The caller has verified that the destination is an IRE_BROADCAST and that
16769  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16770  * we create a MULTIRT IRE_BROADCAST.
16771  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16772  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16773  */
16774 static void
16775 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16776 {
16777 	ire_t *ire_prim;
16778 
16779 	ASSERT(ire != NULL);
16780 
16781 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16782 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16783 	    NULL);
16784 	if (ire_prim != NULL) {
16785 		/*
16786 		 * We are in the special case of broadcasts for
16787 		 * CGTP. We add an IRE_BROADCAST that holds
16788 		 * the RTF_MULTIRT flag, the destination
16789 		 * address and the low level
16790 		 * info of ire_prim. In other words, CGTP
16791 		 * broadcast is added to the redundant ipif.
16792 		 */
16793 		ill_t *ill_prim;
16794 		ire_t  *bcast_ire;
16795 
16796 		ill_prim = ire_prim->ire_ill;
16797 
16798 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16799 		    (void *)ire_prim, (void *)ill_prim));
16800 
16801 		bcast_ire = ire_create(
16802 		    (uchar_t *)&ire->ire_addr,
16803 		    (uchar_t *)&ip_g_all_ones,
16804 		    (uchar_t *)&ire->ire_gateway_addr,
16805 		    IRE_BROADCAST,
16806 		    ill_prim,
16807 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16808 		    ire->ire_flags | RTF_KERNEL,
16809 		    NULL,
16810 		    ipst);
16811 
16812 		/*
16813 		 * Here we assume that ire_add does head insertion so that
16814 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16815 		 */
16816 		if (bcast_ire != NULL) {
16817 			if (ire->ire_flags & RTF_SETSRC) {
16818 				bcast_ire->ire_setsrc_addr =
16819 				    ire->ire_setsrc_addr;
16820 			}
16821 			bcast_ire = ire_add(bcast_ire);
16822 			if (bcast_ire != NULL) {
16823 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16824 				    "added bcast_ire %p\n",
16825 				    (void *)bcast_ire));
16826 
16827 				ill_save_ire(ill_prim, bcast_ire);
16828 				ire_refrele(bcast_ire);
16829 			}
16830 		}
16831 		ire_refrele(ire_prim);
16832 	}
16833 }
16834 
16835 /*
16836  * IP multirouting broadcast routes handling
16837  * Remove the broadcast ire.
16838  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16839  * the destination and the gateway are broadcast addresses.
16840  * The caller has only verified that RTF_MULTIRT was set. We check
16841  * that the destination is broadcast and that the gateway is a broadcast
16842  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16843  */
16844 static void
16845 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16846 {
16847 	ASSERT(ire != NULL);
16848 
16849 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16850 		ire_t *ire_prim;
16851 
16852 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16853 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16854 		    ipst, NULL);
16855 		if (ire_prim != NULL) {
16856 			ill_t *ill_prim;
16857 			ire_t  *bcast_ire;
16858 
16859 			ill_prim = ire_prim->ire_ill;
16860 
16861 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16862 			    "ire_prim %p, ill_prim %p\n",
16863 			    (void *)ire_prim, (void *)ill_prim));
16864 
16865 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16866 			    ire->ire_gateway_addr, IRE_BROADCAST,
16867 			    ill_prim, ALL_ZONES, NULL,
16868 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16869 			    MATCH_IRE_MASK, 0, ipst, NULL);
16870 
16871 			if (bcast_ire != NULL) {
16872 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16873 				    "looked up bcast_ire %p\n",
16874 				    (void *)bcast_ire));
16875 				ill_remove_saved_ire(bcast_ire->ire_ill,
16876 				    bcast_ire);
16877 				ire_delete(bcast_ire);
16878 				ire_refrele(bcast_ire);
16879 			}
16880 			ire_refrele(ire_prim);
16881 		}
16882 	}
16883 }
16884 
16885 /*
16886  * Derive an interface id from the link layer address.
16887  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16888  */
16889 static void
16890 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16891 {
16892 	char		*addr;
16893 
16894 	/*
16895 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16896 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16897 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16898 	 * interface ID on IPv6 interfaces above links that actually have real
16899 	 * Ethernet addresses.
16900 	 */
16901 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16902 		/* Form EUI-64 like address */
16903 		addr = (char *)&v6addr->s6_addr32[2];
16904 		bcopy(ill->ill_phys_addr, addr, 3);
16905 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16906 		addr[3] = (char)0xff;
16907 		addr[4] = (char)0xfe;
16908 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16909 	}
16910 }
16911 
16912 /* ARGSUSED */
16913 static void
16914 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16915 {
16916 }
16917 
16918 typedef struct ipmp_ifcookie {
16919 	uint32_t	ic_hostid;
16920 	char		ic_ifname[LIFNAMSIZ];
16921 	char		ic_zonename[ZONENAME_MAX];
16922 } ipmp_ifcookie_t;
16923 
16924 /*
16925  * Construct a pseudo-random interface ID for the IPMP interface that's both
16926  * predictable and (almost) guaranteed to be unique.
16927  */
16928 static void
16929 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16930 {
16931 	zone_t		*zp;
16932 	uint8_t		*addr;
16933 	uchar_t		hash[16];
16934 	ulong_t 	hostid;
16935 	MD5_CTX		ctx;
16936 	ipmp_ifcookie_t	ic = { 0 };
16937 
16938 	ASSERT(IS_IPMP(ill));
16939 
16940 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16941 	ic.ic_hostid = htonl((uint32_t)hostid);
16942 
16943 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16944 
16945 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16946 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16947 		zone_rele(zp);
16948 	}
16949 
16950 	MD5Init(&ctx);
16951 	MD5Update(&ctx, &ic, sizeof (ic));
16952 	MD5Final(hash, &ctx);
16953 
16954 	/*
16955 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16956 	 */
16957 	addr = &v6addr->s6_addr8[8];
16958 	bcopy(hash + 8, addr, sizeof (uint64_t));
16959 	addr[0] &= ~0x2;				/* set local bit */
16960 }
16961 
16962 /*
16963  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16964  */
16965 static void
16966 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16967 {
16968 	phyint_t *phyi = ill->ill_phyint;
16969 
16970 	/*
16971 	 * Check PHYI_MULTI_BCAST and length of physical
16972 	 * address to determine if we use the mapping or the
16973 	 * broadcast address.
16974 	 */
16975 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16976 	    ill->ill_phys_addr_length != ETHERADDRL) {
16977 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16978 		return;
16979 	}
16980 	m_physaddr[0] = 0x33;
16981 	m_physaddr[1] = 0x33;
16982 	m_physaddr[2] = m_ip6addr[12];
16983 	m_physaddr[3] = m_ip6addr[13];
16984 	m_physaddr[4] = m_ip6addr[14];
16985 	m_physaddr[5] = m_ip6addr[15];
16986 }
16987 
16988 /*
16989  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16990  */
16991 static void
16992 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16993 {
16994 	phyint_t *phyi = ill->ill_phyint;
16995 
16996 	/*
16997 	 * Check PHYI_MULTI_BCAST and length of physical
16998 	 * address to determine if we use the mapping or the
16999 	 * broadcast address.
17000 	 */
17001 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17002 	    ill->ill_phys_addr_length != ETHERADDRL) {
17003 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17004 		return;
17005 	}
17006 	m_physaddr[0] = 0x01;
17007 	m_physaddr[1] = 0x00;
17008 	m_physaddr[2] = 0x5e;
17009 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17010 	m_physaddr[4] = m_ipaddr[2];
17011 	m_physaddr[5] = m_ipaddr[3];
17012 }
17013 
17014 /* ARGSUSED */
17015 static void
17016 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17017 {
17018 	/*
17019 	 * for the MULTI_BCAST case and other cases when we want to
17020 	 * use the link-layer broadcast address for multicast.
17021 	 */
17022 	uint8_t	*bphys_addr;
17023 	dl_unitdata_req_t *dlur;
17024 
17025 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17026 	if (ill->ill_sap_length < 0) {
17027 		bphys_addr = (uchar_t *)dlur +
17028 		    dlur->dl_dest_addr_offset;
17029 	} else  {
17030 		bphys_addr = (uchar_t *)dlur +
17031 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17032 	}
17033 
17034 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17035 }
17036 
17037 /*
17038  * Derive IPoIB interface id from the link layer address.
17039  */
17040 static void
17041 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17042 {
17043 	char		*addr;
17044 
17045 	ASSERT(ill->ill_phys_addr_length == 20);
17046 	addr = (char *)&v6addr->s6_addr32[2];
17047 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17048 	/*
17049 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17050 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17051 	 * rules. In these cases, the IBA considers these GUIDs to be in
17052 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17053 	 * required; vendors are required not to assign global EUI-64's
17054 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17055 	 * of the interface identifier. Whether the GUID is in modified
17056 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17057 	 * bit set to 1.
17058 	 */
17059 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17060 }
17061 
17062 /*
17063  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17064  * Note on mapping from multicast IP addresses to IPoIB multicast link
17065  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17066  * The format of an IPoIB multicast address is:
17067  *
17068  *  4 byte QPN      Scope Sign.  Pkey
17069  * +--------------------------------------------+
17070  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17071  * +--------------------------------------------+
17072  *
17073  * The Scope and Pkey components are properties of the IBA port and
17074  * network interface. They can be ascertained from the broadcast address.
17075  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17076  */
17077 static void
17078 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17079 {
17080 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17081 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17082 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17083 	uint8_t	*bphys_addr;
17084 	dl_unitdata_req_t *dlur;
17085 
17086 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17087 
17088 	/*
17089 	 * RFC 4391: IPv4 MGID is 28-bit long.
17090 	 */
17091 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17092 	m_physaddr[17] = m_ipaddr[1];
17093 	m_physaddr[18] = m_ipaddr[2];
17094 	m_physaddr[19] = m_ipaddr[3];
17095 
17096 
17097 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17098 	if (ill->ill_sap_length < 0) {
17099 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17100 	} else  {
17101 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17102 		    ill->ill_sap_length;
17103 	}
17104 	/*
17105 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17106 	 */
17107 	m_physaddr[5] = bphys_addr[5];
17108 	m_physaddr[8] = bphys_addr[8];
17109 	m_physaddr[9] = bphys_addr[9];
17110 }
17111 
17112 static void
17113 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17114 {
17115 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17116 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17117 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17118 	uint8_t	*bphys_addr;
17119 	dl_unitdata_req_t *dlur;
17120 
17121 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17122 
17123 	/*
17124 	 * RFC 4391: IPv4 MGID is 80-bit long.
17125 	 */
17126 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17127 
17128 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17129 	if (ill->ill_sap_length < 0) {
17130 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17131 	} else  {
17132 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17133 		    ill->ill_sap_length;
17134 	}
17135 	/*
17136 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17137 	 */
17138 	m_physaddr[5] = bphys_addr[5];
17139 	m_physaddr[8] = bphys_addr[8];
17140 	m_physaddr[9] = bphys_addr[9];
17141 }
17142 
17143 /*
17144  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17145  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17146  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17147  * of RFC4213.
17148  */
17149 static void
17150 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17151 {
17152 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17153 	v6addr->s6_addr32[2] = 0;
17154 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17155 }
17156 
17157 /*
17158  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17159  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17160  * id.
17161  */
17162 static void
17163 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17164 {
17165 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17166 
17167 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17168 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17169 }
17170 
17171 static void
17172 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17173 {
17174 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17175 }
17176 
17177 static void
17178 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17179 {
17180 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17181 }
17182 
17183 static void
17184 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17185 {
17186 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17187 }
17188 
17189 static void
17190 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17191 {
17192 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17193 }
17194 
17195 /*
17196  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17197  * Returns an held ill, or NULL.
17198  */
17199 ill_t *
17200 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17201     ip_stack_t *ipst)
17202 {
17203 	ill_t	*ill;
17204 	ipif_t	*ipif;
17205 
17206 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17207 	if (ill == NULL)
17208 		return (NULL);
17209 
17210 	mutex_enter(&ill->ill_lock);
17211 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17212 		if (IPIF_IS_CONDEMNED(ipif))
17213 			continue;
17214 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17215 		    ipif->ipif_zoneid != ALL_ZONES)
17216 			continue;
17217 
17218 		mutex_exit(&ill->ill_lock);
17219 		return (ill);
17220 	}
17221 	mutex_exit(&ill->ill_lock);
17222 	ill_refrele(ill);
17223 	return (NULL);
17224 }
17225 
17226 /*
17227  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17228  * If a pointer to an ipif_t is returned then the caller will need to do
17229  * an ill_refrele().
17230  */
17231 ipif_t *
17232 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17233     ip_stack_t *ipst)
17234 {
17235 	ipif_t *ipif;
17236 	ill_t *ill;
17237 
17238 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17239 	if (ill == NULL)
17240 		return (NULL);
17241 
17242 	mutex_enter(&ill->ill_lock);
17243 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17244 		mutex_exit(&ill->ill_lock);
17245 		ill_refrele(ill);
17246 		return (NULL);
17247 	}
17248 
17249 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17250 		if (!IPIF_CAN_LOOKUP(ipif))
17251 			continue;
17252 		if (lifidx == ipif->ipif_id) {
17253 			ipif_refhold_locked(ipif);
17254 			break;
17255 		}
17256 	}
17257 
17258 	mutex_exit(&ill->ill_lock);
17259 	ill_refrele(ill);
17260 	return (ipif);
17261 }
17262 
17263 /*
17264  * Set ill_inputfn based on the current know state.
17265  * This needs to be called when any of the factors taken into
17266  * account changes.
17267  */
17268 void
17269 ill_set_inputfn(ill_t *ill)
17270 {
17271 	ip_stack_t	*ipst = ill->ill_ipst;
17272 
17273 	if (ill->ill_isv6) {
17274 		if (is_system_labeled())
17275 			ill->ill_inputfn = ill_input_full_v6;
17276 		else
17277 			ill->ill_inputfn = ill_input_short_v6;
17278 	} else {
17279 		if (is_system_labeled())
17280 			ill->ill_inputfn = ill_input_full_v4;
17281 		else if (ill->ill_dhcpinit != 0)
17282 			ill->ill_inputfn = ill_input_full_v4;
17283 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17284 		    != NULL)
17285 			ill->ill_inputfn = ill_input_full_v4;
17286 		else if (ipst->ips_ip_cgtp_filter &&
17287 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17288 			ill->ill_inputfn = ill_input_full_v4;
17289 		else
17290 			ill->ill_inputfn = ill_input_short_v4;
17291 	}
17292 }
17293 
17294 /*
17295  * Re-evaluate ill_inputfn for all the IPv4 ills.
17296  * Used when RSVP and CGTP comes and goes.
17297  */
17298 void
17299 ill_set_inputfn_all(ip_stack_t *ipst)
17300 {
17301 	ill_walk_context_t	ctx;
17302 	ill_t			*ill;
17303 
17304 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17305 	ill = ILL_START_WALK_V4(&ctx, ipst);
17306 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17307 		ill_set_inputfn(ill);
17308 
17309 	rw_exit(&ipst->ips_ill_g_lock);
17310 }
17311 
17312 /*
17313  * Set the physical address information for `ill' to the contents of the
17314  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17315  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17316  * EINPROGRESS will be returned.
17317  */
17318 int
17319 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17320 {
17321 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17322 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17323 
17324 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17325 
17326 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17327 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17328 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17329 		/* Changing DL_IPV6_TOKEN is not yet supported */
17330 		return (0);
17331 	}
17332 
17333 	/*
17334 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17335 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17336 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17337 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17338 	 */
17339 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17340 		freemsg(mp);
17341 		return (ENOMEM);
17342 	}
17343 
17344 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17345 	mutex_enter(&ill->ill_lock);
17346 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17347 	/* no more nce addition allowed */
17348 	mutex_exit(&ill->ill_lock);
17349 
17350 	/*
17351 	 * If we can quiesce the ill, then set the address.  If not, then
17352 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17353 	 */
17354 	ill_down_ipifs(ill, B_TRUE);
17355 	mutex_enter(&ill->ill_lock);
17356 	if (!ill_is_quiescent(ill)) {
17357 		/* call cannot fail since `conn_t *' argument is NULL */
17358 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17359 		    mp, ILL_DOWN);
17360 		mutex_exit(&ill->ill_lock);
17361 		return (EINPROGRESS);
17362 	}
17363 	mutex_exit(&ill->ill_lock);
17364 
17365 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17366 	return (0);
17367 }
17368 
17369 /*
17370  * Once the ill associated with `q' has quiesced, set its physical address
17371  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17372  * are passed (linked by b_cont), since we sometimes need to save two distinct
17373  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17374  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17375  * is quiesced, we know any stale nce's with the old address information have
17376  * already been removed, so we don't need to call nce_flush().
17377  */
17378 /* ARGSUSED */
17379 static void
17380 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17381 {
17382 	ill_t		*ill = q->q_ptr;
17383 	mblk_t		*addrmp2 = unlinkb(addrmp);
17384 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17385 	uint_t		addrlen, addroff;
17386 	int		status;
17387 
17388 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17389 
17390 	addroff	= dlindp->dl_addr_offset;
17391 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17392 
17393 	switch (dlindp->dl_data) {
17394 	case DL_IPV6_LINK_LAYER_ADDR:
17395 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17396 		freemsg(addrmp2);
17397 		break;
17398 
17399 	case DL_CURR_DEST_ADDR:
17400 		freemsg(ill->ill_dest_addr_mp);
17401 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17402 		ill->ill_dest_addr_mp = addrmp;
17403 		if (ill->ill_isv6) {
17404 			ill_setdesttoken(ill);
17405 			ipif_setdestlinklocal(ill->ill_ipif);
17406 		}
17407 		freemsg(addrmp2);
17408 		break;
17409 
17410 	case DL_CURR_PHYS_ADDR:
17411 		freemsg(ill->ill_phys_addr_mp);
17412 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17413 		ill->ill_phys_addr_mp = addrmp;
17414 		ill->ill_phys_addr_length = addrlen;
17415 		if (ill->ill_isv6)
17416 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17417 		else
17418 			freemsg(addrmp2);
17419 		if (ill->ill_isv6) {
17420 			ill_setdefaulttoken(ill);
17421 			ipif_setlinklocal(ill->ill_ipif);
17422 		}
17423 		break;
17424 	default:
17425 		ASSERT(0);
17426 	}
17427 
17428 	/*
17429 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17430 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17431 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17432 	 * brought up.
17433 	 */
17434 	status = ill_up_ipifs(ill, q, addrmp);
17435 	mutex_enter(&ill->ill_lock);
17436 	if (ill->ill_dl_up)
17437 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17438 	mutex_exit(&ill->ill_lock);
17439 	if (status != EINPROGRESS)
17440 		ipsq_current_finish(ipsq);
17441 }
17442 
17443 /*
17444  * Helper routine for setting the ill_nd_lla fields.
17445  */
17446 void
17447 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17448 {
17449 	freemsg(ill->ill_nd_lla_mp);
17450 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17451 	ill->ill_nd_lla_mp = ndmp;
17452 	ill->ill_nd_lla_len = addrlen;
17453 }
17454 
17455 /*
17456  * Replumb the ill.
17457  */
17458 int
17459 ill_replumb(ill_t *ill, mblk_t *mp)
17460 {
17461 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17462 
17463 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17464 
17465 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17466 
17467 	mutex_enter(&ill->ill_lock);
17468 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17469 	/* no more nce addition allowed */
17470 	mutex_exit(&ill->ill_lock);
17471 
17472 	/*
17473 	 * If we can quiesce the ill, then continue.  If not, then
17474 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17475 	 */
17476 	ill_down_ipifs(ill, B_FALSE);
17477 
17478 	mutex_enter(&ill->ill_lock);
17479 	if (!ill_is_quiescent(ill)) {
17480 		/* call cannot fail since `conn_t *' argument is NULL */
17481 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17482 		    mp, ILL_DOWN);
17483 		mutex_exit(&ill->ill_lock);
17484 		return (EINPROGRESS);
17485 	}
17486 	mutex_exit(&ill->ill_lock);
17487 
17488 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17489 	return (0);
17490 }
17491 
17492 /* ARGSUSED */
17493 static void
17494 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17495 {
17496 	ill_t *ill = q->q_ptr;
17497 	int err;
17498 	conn_t *connp = NULL;
17499 
17500 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17501 	freemsg(ill->ill_replumb_mp);
17502 	ill->ill_replumb_mp = copyb(mp);
17503 
17504 	if (ill->ill_replumb_mp == NULL) {
17505 		/* out of memory */
17506 		ipsq_current_finish(ipsq);
17507 		return;
17508 	}
17509 
17510 	mutex_enter(&ill->ill_lock);
17511 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17512 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17513 	mutex_exit(&ill->ill_lock);
17514 
17515 	if (!ill->ill_up_ipifs) {
17516 		/* already closing */
17517 		ipsq_current_finish(ipsq);
17518 		return;
17519 	}
17520 	ill->ill_replumbing = 1;
17521 	err = ill_down_ipifs_tail(ill);
17522 
17523 	/*
17524 	 * Successfully quiesced and brought down the interface, now we send
17525 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17526 	 * DL_NOTE_REPLUMB message.
17527 	 */
17528 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17529 	    DL_NOTIFY_CONF);
17530 	ASSERT(mp != NULL);
17531 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17532 	    DL_NOTE_REPLUMB_DONE;
17533 	ill_dlpi_send(ill, mp);
17534 
17535 	/*
17536 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17537 	 * streams have to be unbound. When all the DLPI exchanges are done,
17538 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17539 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17540 	 * arp_bringup_done().
17541 	 */
17542 	ASSERT(ill->ill_replumb_mp != NULL);
17543 	if (err == EINPROGRESS)
17544 		return;
17545 	else
17546 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17547 	ASSERT(connp == NULL);
17548 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17549 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17550 		return;
17551 	}
17552 	ipsq_current_finish(ipsq);
17553 }
17554 
17555 /*
17556  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17557  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17558  * as per the ioctl.  On failure, an errno is returned.
17559  */
17560 static int
17561 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17562 {
17563 	int rval;
17564 	struct strioctl iocb;
17565 
17566 	iocb.ic_cmd = cmd;
17567 	iocb.ic_timout = 15;
17568 	iocb.ic_len = bufsize;
17569 	iocb.ic_dp = buf;
17570 
17571 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17572 }
17573 
17574 /*
17575  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17576  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17577  */
17578 static int
17579 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17580     uint_t *bufsizep, cred_t *cr)
17581 {
17582 	int err;
17583 	struct lifnum lifn;
17584 
17585 	bzero(&lifn, sizeof (lifn));
17586 	lifn.lifn_family = af;
17587 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17588 
17589 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17590 		return (err);
17591 
17592 	/*
17593 	 * Pad the interface count to account for additional interfaces that
17594 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17595 	 */
17596 	lifn.lifn_count += 4;
17597 	bzero(lifcp, sizeof (*lifcp));
17598 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17599 	lifcp->lifc_family = af;
17600 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17601 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17602 
17603 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17604 	if (err != 0) {
17605 		kmem_free(lifcp->lifc_buf, *bufsizep);
17606 		return (err);
17607 	}
17608 
17609 	return (0);
17610 }
17611 
17612 /*
17613  * Helper for ip_interface_cleanup() that removes the loopback interface.
17614  */
17615 static void
17616 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17617 {
17618 	int err;
17619 	struct lifreq lifr;
17620 
17621 	bzero(&lifr, sizeof (lifr));
17622 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17623 
17624 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17625 	if (err != 0) {
17626 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17627 		    "error %d\n", isv6 ? "v6" : "v4", err));
17628 	}
17629 }
17630 
17631 /*
17632  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17633  * groups and that IPMP data addresses are down.  These conditions must be met
17634  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17635  */
17636 static void
17637 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17638 {
17639 	int af = isv6 ? AF_INET6 : AF_INET;
17640 	int i, nifs;
17641 	int err;
17642 	uint_t bufsize;
17643 	uint_t lifrsize = sizeof (struct lifreq);
17644 	struct lifconf lifc;
17645 	struct lifreq *lifrp;
17646 
17647 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17648 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17649 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17650 		return;
17651 	}
17652 
17653 	nifs = lifc.lifc_len / lifrsize;
17654 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17655 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17656 		if (err != 0) {
17657 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17658 			    "flags: error %d", lifrp->lifr_name, err);
17659 			continue;
17660 		}
17661 
17662 		if (lifrp->lifr_flags & IFF_IPMP) {
17663 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17664 				continue;
17665 
17666 			lifrp->lifr_flags &= ~IFF_UP;
17667 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17668 			if (err != 0) {
17669 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17670 				    "bring down (error %d); IPMP interface may "
17671 				    "not be shutdown", lifrp->lifr_name, err);
17672 			}
17673 
17674 			/*
17675 			 * Check if IFF_DUPLICATE is still set -- and if so,
17676 			 * reset the address to clear it.
17677 			 */
17678 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17679 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17680 				continue;
17681 
17682 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17683 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17684 			    lifrp, lifrsize, cr)) != 0) {
17685 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17686 				    "reset DAD (error %d); IPMP interface may "
17687 				    "not be shutdown", lifrp->lifr_name, err);
17688 			}
17689 			continue;
17690 		}
17691 
17692 		lifrp->lifr_groupname[0] = '\0';
17693 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17694 		if (err != 0) {
17695 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17696 			    "IPMP group (error %d); associated IPMP interface "
17697 			    "may not be shutdown", lifrp->lifr_name, err);
17698 			continue;
17699 		}
17700 	}
17701 
17702 	kmem_free(lifc.lifc_buf, bufsize);
17703 }
17704 
17705 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17706 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17707 
17708 /*
17709  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17710  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17711  * when the user-level processes in the zone are killed and the latter are
17712  * cleaned up by str_stack_shutdown().
17713  */
17714 void
17715 ip_interface_cleanup(ip_stack_t *ipst)
17716 {
17717 	ldi_handle_t	lh;
17718 	ldi_ident_t	li;
17719 	cred_t		*cr;
17720 	int		err;
17721 	int		i;
17722 	char		*devs[] = { UDP6DEV, UDPDEV };
17723 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17724 
17725 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17726 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17727 		    " error %d", err);
17728 		return;
17729 	}
17730 
17731 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17732 	ASSERT(cr != NULL);
17733 
17734 	/*
17735 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17736 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17737 	 * the loop.)
17738 	 */
17739 	for (i = 0; i < 2; i++) {
17740 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17741 		if (err != 0) {
17742 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17743 			    " error %d", devs[i], err);
17744 			continue;
17745 		}
17746 
17747 		ip_loopback_removeif(lh, i == 0, cr);
17748 		ip_ipmp_cleanup(lh, i == 0, cr);
17749 
17750 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17751 	}
17752 
17753 	ldi_ident_release(li);
17754 	crfree(cr);
17755 }
17756 
17757 /*
17758  * This needs to be in-sync with nic_event_t definition
17759  */
17760 static const char *
17761 ill_hook_event2str(nic_event_t event)
17762 {
17763 	switch (event) {
17764 	case NE_PLUMB:
17765 		return ("PLUMB");
17766 	case NE_UNPLUMB:
17767 		return ("UNPLUMB");
17768 	case NE_UP:
17769 		return ("UP");
17770 	case NE_DOWN:
17771 		return ("DOWN");
17772 	case NE_ADDRESS_CHANGE:
17773 		return ("ADDRESS_CHANGE");
17774 	case NE_LIF_UP:
17775 		return ("LIF_UP");
17776 	case NE_LIF_DOWN:
17777 		return ("LIF_DOWN");
17778 	case NE_IFINDEX_CHANGE:
17779 		return ("IFINDEX_CHANGE");
17780 	default:
17781 		return ("UNKNOWN");
17782 	}
17783 }
17784 
17785 void
17786 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17787     nic_event_data_t data, size_t datalen)
17788 {
17789 	ip_stack_t		*ipst = ill->ill_ipst;
17790 	hook_nic_event_int_t	*info;
17791 	const char		*str = NULL;
17792 
17793 	/* create a new nic event info */
17794 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17795 		goto fail;
17796 
17797 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17798 	info->hnei_event.hne_lif = lif;
17799 	info->hnei_event.hne_event = event;
17800 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17801 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17802 	info->hnei_event.hne_data = NULL;
17803 	info->hnei_event.hne_datalen = 0;
17804 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17805 
17806 	if (data != NULL && datalen != 0) {
17807 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17808 		if (info->hnei_event.hne_data == NULL)
17809 			goto fail;
17810 		bcopy(data, info->hnei_event.hne_data, datalen);
17811 		info->hnei_event.hne_datalen = datalen;
17812 	}
17813 
17814 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17815 	    DDI_NOSLEEP) == DDI_SUCCESS)
17816 		return;
17817 
17818 fail:
17819 	if (info != NULL) {
17820 		if (info->hnei_event.hne_data != NULL) {
17821 			kmem_free(info->hnei_event.hne_data,
17822 			    info->hnei_event.hne_datalen);
17823 		}
17824 		kmem_free(info, sizeof (hook_nic_event_t));
17825 	}
17826 	str = ill_hook_event2str(event);
17827 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17828 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17829 }
17830 
17831 static int
17832 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17833 {
17834 	int		err = 0;
17835 	const in_addr_t	*addr = NULL;
17836 	nce_t		*nce = NULL;
17837 	ill_t		*ill = ipif->ipif_ill;
17838 	ill_t		*bound_ill;
17839 	boolean_t	added_ipif = B_FALSE;
17840 	uint16_t	state;
17841 	uint16_t	flags;
17842 
17843 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17844 	    ill_t *, ill, ipif_t *, ipif);
17845 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17846 		addr = &ipif->ipif_lcl_addr;
17847 	}
17848 
17849 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17850 		if (res_act != Res_act_initial)
17851 			return (EINVAL);
17852 	}
17853 
17854 	if (addr != NULL) {
17855 		ipmp_illgrp_t	*illg = ill->ill_grp;
17856 
17857 		/* add unicast nce for the local addr */
17858 
17859 		if (IS_IPMP(ill)) {
17860 			/*
17861 			 * If we're here via ipif_up(), then the ipif
17862 			 * won't be bound yet -- add it to the group,
17863 			 * which will bind it if possible. (We would
17864 			 * add it in ipif_up(), but deleting on failure
17865 			 * there is gruesome.)  If we're here via
17866 			 * ipmp_ill_bind_ipif(), then the ipif has
17867 			 * already been added to the group and we
17868 			 * just need to use the binding.
17869 			 */
17870 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17871 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17872 				if (bound_ill == NULL) {
17873 					/*
17874 					 * We couldn't bind the ipif to an ill
17875 					 * yet, so we have nothing to publish.
17876 					 * Mark the address as ready and return.
17877 					 */
17878 					ipif->ipif_addr_ready = 1;
17879 					return (0);
17880 				}
17881 				added_ipif = B_TRUE;
17882 			}
17883 		} else {
17884 			bound_ill = ill;
17885 		}
17886 
17887 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17888 		    NCE_F_NONUD);
17889 		/*
17890 		 * If this is an initial bring-up (or the ipif was never
17891 		 * completely brought up), do DAD.  Otherwise, we're here
17892 		 * because IPMP has rebound an address to this ill: send
17893 		 * unsolicited advertisements (ARP announcements) to
17894 		 * inform others.
17895 		 */
17896 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17897 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17898 		} else {
17899 			state = ND_REACHABLE;
17900 			flags |= NCE_F_UNSOL_ADV;
17901 		}
17902 
17903 retry:
17904 		err = nce_lookup_then_add_v4(ill,
17905 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17906 		    addr, flags, state, &nce);
17907 
17908 		/*
17909 		 * note that we may encounter EEXIST if we are moving
17910 		 * the nce as a result of a rebind operation.
17911 		 */
17912 		switch (err) {
17913 		case 0:
17914 			ipif->ipif_added_nce = 1;
17915 			nce->nce_ipif_cnt++;
17916 			break;
17917 		case EEXIST:
17918 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17919 			    ill->ill_name));
17920 			if (!NCE_MYADDR(nce->nce_common)) {
17921 				/*
17922 				 * A leftover nce from before this address
17923 				 * existed
17924 				 */
17925 				ncec_delete(nce->nce_common);
17926 				nce_refrele(nce);
17927 				nce = NULL;
17928 				goto retry;
17929 			}
17930 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17931 				nce_refrele(nce);
17932 				nce = NULL;
17933 				ip1dbg(("ipif_arp_up: NCE already exists "
17934 				    "for %s:%u\n", ill->ill_name,
17935 				    ipif->ipif_id));
17936 				goto arp_up_done;
17937 			}
17938 			/*
17939 			 * Duplicate local addresses are permissible for
17940 			 * IPIF_POINTOPOINT interfaces which will get marked
17941 			 * IPIF_UNNUMBERED later in
17942 			 * ip_addr_availability_check().
17943 			 *
17944 			 * The nce_ipif_cnt field tracks the number of
17945 			 * ipifs that have nce_addr as their local address.
17946 			 */
17947 			ipif->ipif_addr_ready = 1;
17948 			ipif->ipif_added_nce = 1;
17949 			nce->nce_ipif_cnt++;
17950 			err = 0;
17951 			break;
17952 		default:
17953 			ASSERT(nce == NULL);
17954 			goto arp_up_done;
17955 		}
17956 		if (arp_no_defense) {
17957 			if ((ipif->ipif_flags & IPIF_UP) &&
17958 			    !ipif->ipif_addr_ready)
17959 				ipif_up_notify(ipif);
17960 			ipif->ipif_addr_ready = 1;
17961 		}
17962 	} else {
17963 		/* zero address. nothing to publish */
17964 		ipif->ipif_addr_ready = 1;
17965 	}
17966 	if (nce != NULL)
17967 		nce_refrele(nce);
17968 arp_up_done:
17969 	if (added_ipif && err != 0)
17970 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17971 	return (err);
17972 }
17973 
17974 int
17975 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17976 {
17977 	int 		err = 0;
17978 	ill_t 		*ill = ipif->ipif_ill;
17979 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17980 
17981 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17982 	    ill_t *, ill, ipif_t *, ipif);
17983 
17984 	/*
17985 	 * need to bring up ARP or setup mcast mapping only
17986 	 * when the first interface is coming UP.
17987 	 */
17988 	first_interface = (ill->ill_ipif_up_count == 0 &&
17989 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17990 
17991 	if (res_act == Res_act_initial && first_interface) {
17992 		/*
17993 		 * Send ATTACH + BIND
17994 		 */
17995 		err = arp_ll_up(ill);
17996 		if (err != EINPROGRESS && err != 0)
17997 			return (err);
17998 
17999 		/*
18000 		 * Add NCE for local address. Start DAD.
18001 		 * we'll wait to hear that DAD has finished
18002 		 * before using the interface.
18003 		 */
18004 		if (err == EINPROGRESS)
18005 			wait_for_dlpi = B_TRUE;
18006 	}
18007 
18008 	if (!wait_for_dlpi)
18009 		(void) ipif_arp_up_done_tail(ipif, res_act);
18010 
18011 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18012 }
18013 
18014 /*
18015  * Finish processing of "arp_up" after all the DLPI message
18016  * exchanges have completed between arp and the driver.
18017  */
18018 void
18019 arp_bringup_done(ill_t *ill, int err)
18020 {
18021 	mblk_t	*mp1;
18022 	ipif_t  *ipif;
18023 	conn_t *connp = NULL;
18024 	ipsq_t	*ipsq;
18025 	queue_t *q;
18026 
18027 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18028 
18029 	ASSERT(IAM_WRITER_ILL(ill));
18030 
18031 	ipsq = ill->ill_phyint->phyint_ipsq;
18032 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18033 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18034 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18035 	if (mp1 == NULL) /* bringup was aborted by the user */
18036 		return;
18037 
18038 	/*
18039 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18040 	 * must have an associated conn_t.  Otherwise, we're bringing this
18041 	 * interface back up as part of handling an asynchronous event (e.g.,
18042 	 * physical address change).
18043 	 */
18044 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18045 		ASSERT(connp != NULL);
18046 		q = CONNP_TO_WQ(connp);
18047 	} else {
18048 		ASSERT(connp == NULL);
18049 		q = ill->ill_rq;
18050 	}
18051 	if (err == 0) {
18052 		if (ipif->ipif_isv6) {
18053 			if ((err = ipif_up_done_v6(ipif)) != 0)
18054 				ip0dbg(("arp_bringup_done: init failed\n"));
18055 		} else {
18056 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18057 			if (err != 0 ||
18058 			    (err = ipif_up_done(ipif)) != 0) {
18059 				ip0dbg(("arp_bringup_done: "
18060 				    "init failed err %x\n", err));
18061 				(void) ipif_arp_down(ipif);
18062 			}
18063 
18064 		}
18065 	} else {
18066 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18067 	}
18068 
18069 	if ((err == 0) && (ill->ill_up_ipifs)) {
18070 		err = ill_up_ipifs(ill, q, mp1);
18071 		if (err == EINPROGRESS)
18072 			return;
18073 	}
18074 
18075 	/*
18076 	 * If we have a moved ipif to bring up, and everything has succeeded
18077 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18078 	 * down -- the admin can try to bring it up by hand if need be.
18079 	 */
18080 	if (ill->ill_move_ipif != NULL) {
18081 		ipif = ill->ill_move_ipif;
18082 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18083 		    ipif->ipif_ill->ill_name));
18084 		ill->ill_move_ipif = NULL;
18085 		if (err == 0) {
18086 			err = ipif_up(ipif, q, mp1);
18087 			if (err == EINPROGRESS)
18088 				return;
18089 		}
18090 	}
18091 
18092 	/*
18093 	 * The operation must complete without EINPROGRESS since
18094 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18095 	 * Otherwise, the operation will be stuck forever in the ipsq.
18096 	 */
18097 	ASSERT(err != EINPROGRESS);
18098 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18099 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18100 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18101 		    ill_t *, ill, ipif_t *, ipif);
18102 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18103 	} else {
18104 		ipsq_current_finish(ipsq);
18105 	}
18106 }
18107 
18108 /*
18109  * Finish processing of arp replumb after all the DLPI message
18110  * exchanges have completed between arp and the driver.
18111  */
18112 void
18113 arp_replumb_done(ill_t *ill, int err)
18114 {
18115 	mblk_t	*mp1;
18116 	ipif_t  *ipif;
18117 	conn_t *connp = NULL;
18118 	ipsq_t	*ipsq;
18119 	queue_t *q;
18120 
18121 	ASSERT(IAM_WRITER_ILL(ill));
18122 
18123 	ipsq = ill->ill_phyint->phyint_ipsq;
18124 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18125 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18126 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18127 	if (mp1 == NULL) {
18128 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18129 		    ipsq->ipsq_xop->ipx_current_ioctl));
18130 		/* bringup was aborted by the user */
18131 		return;
18132 	}
18133 	/*
18134 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18135 	 * must have an associated conn_t.  Otherwise, we're bringing this
18136 	 * interface back up as part of handling an asynchronous event (e.g.,
18137 	 * physical address change).
18138 	 */
18139 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18140 		ASSERT(connp != NULL);
18141 		q = CONNP_TO_WQ(connp);
18142 	} else {
18143 		ASSERT(connp == NULL);
18144 		q = ill->ill_rq;
18145 	}
18146 	if ((err == 0) && (ill->ill_up_ipifs)) {
18147 		err = ill_up_ipifs(ill, q, mp1);
18148 		if (err == EINPROGRESS)
18149 			return;
18150 	}
18151 	/*
18152 	 * The operation must complete without EINPROGRESS since
18153 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18154 	 * Otherwise, the operation will be stuck forever in the ipsq.
18155 	 */
18156 	ASSERT(err != EINPROGRESS);
18157 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18158 		DTRACE_PROBE4(ipif__ioctl, char *,
18159 		    "arp_replumb_done finish",
18160 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18161 		    ill_t *, ill, ipif_t *, ipif);
18162 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18163 	} else {
18164 		ipsq_current_finish(ipsq);
18165 	}
18166 }
18167 
18168 void
18169 ipif_up_notify(ipif_t *ipif)
18170 {
18171 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18172 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18173 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18174 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18175 	    NE_LIF_UP, NULL, 0);
18176 }
18177 
18178 /*
18179  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18180  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18181  * TPI end points with STREAMS modules pushed above.  This is assured by not
18182  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18183  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18184  * while unwinding from the ispq and that could be a thread from the bottom.
18185  */
18186 /* ARGSUSED */
18187 int
18188 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18189     ip_ioctl_cmd_t *ipip, void *arg)
18190 {
18191 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18192 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18193 	int ret = 0;
18194 	int i;
18195 	size_t size;
18196 	ip_stack_t *ipst;
18197 	zoneid_t zoneid;
18198 	ilb_stack_t *ilbs;
18199 
18200 	ipst = CONNQ_TO_IPST(q);
18201 	ilbs = ipst->ips_netstack->netstack_ilb;
18202 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18203 
18204 	switch (command) {
18205 	case ILB_CREATE_RULE: {
18206 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18207 
18208 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18209 			ret = EINVAL;
18210 			break;
18211 		}
18212 
18213 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18214 		break;
18215 	}
18216 	case ILB_DESTROY_RULE:
18217 	case ILB_ENABLE_RULE:
18218 	case ILB_DISABLE_RULE: {
18219 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18220 
18221 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18222 			ret = EINVAL;
18223 			break;
18224 		}
18225 
18226 		if (cmd->flags & ILB_RULE_ALLRULES) {
18227 			if (command == ILB_DESTROY_RULE) {
18228 				ilb_rule_del_all(ilbs, zoneid);
18229 				break;
18230 			} else if (command == ILB_ENABLE_RULE) {
18231 				ilb_rule_enable_all(ilbs, zoneid);
18232 				break;
18233 			} else if (command == ILB_DISABLE_RULE) {
18234 				ilb_rule_disable_all(ilbs, zoneid);
18235 				break;
18236 			}
18237 		} else {
18238 			if (command == ILB_DESTROY_RULE) {
18239 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18240 			} else if (command == ILB_ENABLE_RULE) {
18241 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18242 				    NULL);
18243 			} else if (command == ILB_DISABLE_RULE) {
18244 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18245 				    NULL);
18246 			}
18247 		}
18248 		break;
18249 	}
18250 	case ILB_NUM_RULES: {
18251 		ilb_num_rules_cmd_t *cmd;
18252 
18253 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18254 			ret = EINVAL;
18255 			break;
18256 		}
18257 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18258 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18259 		break;
18260 	}
18261 	case ILB_RULE_NAMES: {
18262 		ilb_rule_names_cmd_t *cmd;
18263 
18264 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18265 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18266 		    cmd->num_names == 0) {
18267 			ret = EINVAL;
18268 			break;
18269 		}
18270 		size = cmd->num_names * ILB_RULE_NAMESZ;
18271 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18272 		    size != cmd_mp->b_wptr) {
18273 			ret = EINVAL;
18274 			break;
18275 		}
18276 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18277 		break;
18278 	}
18279 	case ILB_NUM_SERVERS: {
18280 		ilb_num_servers_cmd_t *cmd;
18281 
18282 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18283 			ret = EINVAL;
18284 			break;
18285 		}
18286 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18287 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18288 		    &(cmd->num));
18289 		break;
18290 	}
18291 	case ILB_LIST_RULE: {
18292 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18293 
18294 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18295 			ret = EINVAL;
18296 			break;
18297 		}
18298 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18299 		break;
18300 	}
18301 	case ILB_LIST_SERVERS: {
18302 		ilb_servers_info_cmd_t *cmd;
18303 
18304 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18305 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18306 		    cmd->num_servers == 0) {
18307 			ret = EINVAL;
18308 			break;
18309 		}
18310 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18311 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18312 		    size != cmd_mp->b_wptr) {
18313 			ret = EINVAL;
18314 			break;
18315 		}
18316 
18317 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18318 		    &cmd->num_servers);
18319 		break;
18320 	}
18321 	case ILB_ADD_SERVERS: {
18322 		ilb_servers_info_cmd_t *cmd;
18323 		ilb_rule_t *rule;
18324 
18325 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18326 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18327 			ret = EINVAL;
18328 			break;
18329 		}
18330 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18331 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18332 		    size != cmd_mp->b_wptr) {
18333 			ret = EINVAL;
18334 			break;
18335 		}
18336 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18337 		if (rule == NULL) {
18338 			ASSERT(ret != 0);
18339 			break;
18340 		}
18341 		for (i = 0; i < cmd->num_servers; i++) {
18342 			ilb_server_info_t *s;
18343 
18344 			s = &cmd->servers[i];
18345 			s->err = ilb_server_add(ilbs, rule, s);
18346 		}
18347 		ILB_RULE_REFRELE(rule);
18348 		break;
18349 	}
18350 	case ILB_DEL_SERVERS:
18351 	case ILB_ENABLE_SERVERS:
18352 	case ILB_DISABLE_SERVERS: {
18353 		ilb_servers_cmd_t *cmd;
18354 		ilb_rule_t *rule;
18355 		int (*f)();
18356 
18357 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18358 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18359 			ret = EINVAL;
18360 			break;
18361 		}
18362 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18363 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18364 		    size != cmd_mp->b_wptr) {
18365 			ret = EINVAL;
18366 			break;
18367 		}
18368 
18369 		if (command == ILB_DEL_SERVERS)
18370 			f = ilb_server_del;
18371 		else if (command == ILB_ENABLE_SERVERS)
18372 			f = ilb_server_enable;
18373 		else if (command == ILB_DISABLE_SERVERS)
18374 			f = ilb_server_disable;
18375 
18376 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18377 		if (rule == NULL) {
18378 			ASSERT(ret != 0);
18379 			break;
18380 		}
18381 
18382 		for (i = 0; i < cmd->num_servers; i++) {
18383 			ilb_server_arg_t *s;
18384 
18385 			s = &cmd->servers[i];
18386 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18387 		}
18388 		ILB_RULE_REFRELE(rule);
18389 		break;
18390 	}
18391 	case ILB_LIST_NAT_TABLE: {
18392 		ilb_list_nat_cmd_t *cmd;
18393 
18394 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18395 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18396 			ret = EINVAL;
18397 			break;
18398 		}
18399 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18400 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18401 		    size != cmd_mp->b_wptr) {
18402 			ret = EINVAL;
18403 			break;
18404 		}
18405 
18406 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18407 		    &cmd->flags);
18408 		break;
18409 	}
18410 	case ILB_LIST_STICKY_TABLE: {
18411 		ilb_list_sticky_cmd_t *cmd;
18412 
18413 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18414 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18415 			ret = EINVAL;
18416 			break;
18417 		}
18418 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18419 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18420 		    size != cmd_mp->b_wptr) {
18421 			ret = EINVAL;
18422 			break;
18423 		}
18424 
18425 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18426 		    &cmd->num_sticky, &cmd->flags);
18427 		break;
18428 	}
18429 	default:
18430 		ret = EINVAL;
18431 		break;
18432 	}
18433 done:
18434 	return (ret);
18435 }
18436 
18437 /* Remove all cache entries for this logical interface */
18438 void
18439 ipif_nce_down(ipif_t *ipif)
18440 {
18441 	ill_t *ill = ipif->ipif_ill;
18442 	nce_t *nce;
18443 
18444 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18445 	    ill_t *, ill, ipif_t *, ipif);
18446 	if (ipif->ipif_added_nce) {
18447 		if (ipif->ipif_isv6)
18448 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18449 		else
18450 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18451 		if (nce != NULL) {
18452 			if (--nce->nce_ipif_cnt == 0)
18453 				ncec_delete(nce->nce_common);
18454 			ipif->ipif_added_nce = 0;
18455 			nce_refrele(nce);
18456 		} else {
18457 			/*
18458 			 * nce may already be NULL because it was already
18459 			 * flushed, e.g., due to a call to nce_flush
18460 			 */
18461 			ipif->ipif_added_nce = 0;
18462 		}
18463 	}
18464 	/*
18465 	 * Make IPMP aware of the deleted data address.
18466 	 */
18467 	if (IS_IPMP(ill))
18468 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18469 
18470 	/*
18471 	 * Remove all other nces dependent on this ill when the last ipif
18472 	 * is going away.
18473 	 */
18474 	if (ill->ill_ipif_up_count == 0) {
18475 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18476 		    (uchar_t *)ill, ill->ill_ipst);
18477 		if (IS_UNDER_IPMP(ill))
18478 			nce_flush(ill, B_TRUE);
18479 	}
18480 }
18481