xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision d2365b013d4199b49b3a1438d57aea23423e02ad)
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 flush all
938 	 * the messages.
939 	 */
940 	mutex_enter(&ipsq->ipsq_lock);
941 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
942 	    curr = next) {
943 		next = curr->b_next;
944 		if (connp == NULL ||
945 		    (curr->b_queue == wq || curr->b_queue == rq)) {
946 			/* Unlink the mblk from the pending mp list */
947 			if (prev != NULL) {
948 				prev->b_next = curr->b_next;
949 			} else {
950 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
951 				ipsq->ipsq_xopq_mphead = curr->b_next;
952 			}
953 			if (ipsq->ipsq_xopq_mptail == curr)
954 				ipsq->ipsq_xopq_mptail = prev;
955 			/*
956 			 * Create a temporary list and release the ipsq lock
957 			 * New elements are added to the head of the tmp_list
958 			 */
959 			curr->b_next = tmp_list;
960 			tmp_list = curr;
961 		} else {
962 			prev = curr;
963 		}
964 	}
965 	mutex_exit(&ipsq->ipsq_lock);
966 
967 	while (tmp_list != NULL) {
968 		curr = tmp_list;
969 		tmp_list = curr->b_next;
970 		curr->b_next = NULL;
971 		curr->b_prev = NULL;
972 		curr->b_queue = NULL;
973 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
974 			DTRACE_PROBE4(ipif__ioctl,
975 			    char *, "ipsq_xopq_mp_cleanup",
976 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
977 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
978 			    CONN_CLOSE : NO_COPYOUT, NULL);
979 		} else {
980 			/*
981 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
982 			 * this can't be just inet_freemsg. we have to
983 			 * restart it otherwise the thread will be stuck.
984 			 */
985 			inet_freemsg(curr);
986 		}
987 	}
988 }
989 
990 /*
991  * This conn has started closing. Cleanup any pending ioctl from this conn.
992  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
993  */
994 void
995 conn_ioctl_cleanup(conn_t *connp)
996 {
997 	ipsq_t	*ipsq;
998 	ill_t	*ill;
999 	boolean_t refheld;
1000 
1001 	/*
1002 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1003 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1004 	 * started the mp could be present in ipx_pending_mp. Note that if
1005 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1006 	 * not yet queued anywhere. In this case, the conn close code will wait
1007 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1008 	 * tcp_close will wait first until all ioctls have completed for this
1009 	 * conn.
1010 	 */
1011 	mutex_enter(&connp->conn_lock);
1012 	ill = connp->conn_oper_pending_ill;
1013 	if (ill == NULL) {
1014 		mutex_exit(&connp->conn_lock);
1015 		return;
1016 	}
1017 
1018 	/*
1019 	 * We may not be able to refhold the ill if the ill/ipif
1020 	 * is changing. But we need to make sure that the ill will
1021 	 * not vanish. So we just bump up the ill_waiter count.
1022 	 */
1023 	refheld = ill_waiter_inc(ill);
1024 	mutex_exit(&connp->conn_lock);
1025 	if (refheld) {
1026 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1027 			ill_waiter_dcr(ill);
1028 			/*
1029 			 * Check whether this ioctl has started and is
1030 			 * pending. If it is not found there then check
1031 			 * whether this ioctl has not even started and is in
1032 			 * the ipsq_xopq list.
1033 			 */
1034 			if (!ipsq_pending_mp_cleanup(ill, connp))
1035 				ipsq_xopq_mp_cleanup(ill, connp);
1036 			ipsq = ill->ill_phyint->phyint_ipsq;
1037 			ipsq_exit(ipsq);
1038 			return;
1039 		}
1040 	}
1041 
1042 	/*
1043 	 * The ill is also closing and we could not bump up the
1044 	 * ill_waiter_count or we could not enter the ipsq. Leave
1045 	 * the cleanup to ill_delete
1046 	 */
1047 	mutex_enter(&connp->conn_lock);
1048 	while (connp->conn_oper_pending_ill != NULL)
1049 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1050 	mutex_exit(&connp->conn_lock);
1051 	if (refheld)
1052 		ill_waiter_dcr(ill);
1053 }
1054 
1055 /*
1056  * ipcl_walk function for cleaning up conn_*_ill fields.
1057  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1058  * conn_bound_if in place. We prefer dropping
1059  * packets instead of sending them out the wrong interface, or accepting
1060  * packets from the wrong ifindex.
1061  */
1062 static void
1063 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1064 {
1065 	ill_t	*ill = (ill_t *)arg;
1066 
1067 	mutex_enter(&connp->conn_lock);
1068 	if (connp->conn_dhcpinit_ill == ill) {
1069 		connp->conn_dhcpinit_ill = NULL;
1070 		ASSERT(ill->ill_dhcpinit != 0);
1071 		atomic_dec_32(&ill->ill_dhcpinit);
1072 		ill_set_inputfn(ill);
1073 	}
1074 	mutex_exit(&connp->conn_lock);
1075 }
1076 
1077 static int
1078 ill_down_ipifs_tail(ill_t *ill)
1079 {
1080 	ipif_t	*ipif;
1081 	int err;
1082 
1083 	ASSERT(IAM_WRITER_ILL(ill));
1084 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1085 		ipif_non_duplicate(ipif);
1086 		/*
1087 		 * ipif_down_tail will call arp_ll_down on the last ipif
1088 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1089 		 */
1090 		if ((err = ipif_down_tail(ipif)) != 0)
1091 			return (err);
1092 	}
1093 	return (0);
1094 }
1095 
1096 /* ARGSUSED */
1097 void
1098 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1099 {
1100 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1101 	(void) ill_down_ipifs_tail(q->q_ptr);
1102 	freemsg(mp);
1103 	ipsq_current_finish(ipsq);
1104 }
1105 
1106 /*
1107  * ill_down_start is called when we want to down this ill and bring it up again
1108  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1109  * all interfaces, but don't tear down any plumbing.
1110  */
1111 boolean_t
1112 ill_down_start(queue_t *q, mblk_t *mp)
1113 {
1114 	ill_t	*ill = q->q_ptr;
1115 	ipif_t	*ipif;
1116 
1117 	ASSERT(IAM_WRITER_ILL(ill));
1118 	mutex_enter(&ill->ill_lock);
1119 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1120 	/* no more nce addition allowed */
1121 	mutex_exit(&ill->ill_lock);
1122 
1123 	/*
1124 	 * It is possible that some ioctl is already in progress while we
1125 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1126 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1127 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1128 	 * the in progress ioctl from ever completing.
1129 	 *
1130 	 * The thread that started the ioctl (if any) must have returned,
1131 	 * since we are now executing as writer. After the 2 calls below,
1132 	 * the state of the ipsq and the ill would reflect no trace of any
1133 	 * pending operation. Subsequently if there is any response to the
1134 	 * original ioctl from the driver, it would be discarded as an
1135 	 * unsolicited message from the driver.
1136 	 */
1137 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1138 	ill_dlpi_clear_deferred(ill);
1139 
1140 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1141 		(void) ipif_down(ipif, NULL, NULL);
1142 
1143 	ill_down(ill);
1144 
1145 	/*
1146 	 * Walk all CONNs that can have a reference on an ire or nce for this
1147 	 * ill (we actually walk all that now have stale references).
1148 	 */
1149 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1150 
1151 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1152 	if (ill->ill_isv6)
1153 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1154 
1155 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1156 
1157 	/*
1158 	 * Atomically test and add the pending mp if references are active.
1159 	 */
1160 	mutex_enter(&ill->ill_lock);
1161 	if (!ill_is_quiescent(ill)) {
1162 		/* call cannot fail since `conn_t *' argument is NULL */
1163 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1164 		    mp, ILL_DOWN);
1165 		mutex_exit(&ill->ill_lock);
1166 		return (B_FALSE);
1167 	}
1168 	mutex_exit(&ill->ill_lock);
1169 	return (B_TRUE);
1170 }
1171 
1172 static void
1173 ill_down(ill_t *ill)
1174 {
1175 	mblk_t	*mp;
1176 	ip_stack_t	*ipst = ill->ill_ipst;
1177 
1178 	/*
1179 	 * Blow off any IREs dependent on this ILL.
1180 	 * The caller needs to handle conn_ixa_cleanup
1181 	 */
1182 	ill_delete_ires(ill);
1183 
1184 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1185 
1186 	/* Remove any conn_*_ill depending on this ill */
1187 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1188 
1189 	/*
1190 	 * Free state for additional IREs.
1191 	 */
1192 	mutex_enter(&ill->ill_saved_ire_lock);
1193 	mp = ill->ill_saved_ire_mp;
1194 	ill->ill_saved_ire_mp = NULL;
1195 	ill->ill_saved_ire_cnt = 0;
1196 	mutex_exit(&ill->ill_saved_ire_lock);
1197 	freemsg(mp);
1198 }
1199 
1200 /*
1201  * ire_walk routine used to delete every IRE that depends on
1202  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1203  *
1204  * Note: since the routes added by the kernel are deleted separately,
1205  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1206  *
1207  * We also remove references on ire_nce_cache entries that refer to the ill.
1208  */
1209 void
1210 ill_downi(ire_t *ire, char *ill_arg)
1211 {
1212 	ill_t	*ill = (ill_t *)ill_arg;
1213 	nce_t	*nce;
1214 
1215 	mutex_enter(&ire->ire_lock);
1216 	nce = ire->ire_nce_cache;
1217 	if (nce != NULL && nce->nce_ill == ill)
1218 		ire->ire_nce_cache = NULL;
1219 	else
1220 		nce = NULL;
1221 	mutex_exit(&ire->ire_lock);
1222 	if (nce != NULL)
1223 		nce_refrele(nce);
1224 	if (ire->ire_ill == ill) {
1225 		/*
1226 		 * The existing interface binding for ire must be
1227 		 * deleted before trying to bind the route to another
1228 		 * interface. However, since we are using the contents of the
1229 		 * ire after ire_delete, the caller has to ensure that
1230 		 * CONDEMNED (deleted) ire's are not removed from the list
1231 		 * when ire_delete() returns. Currently ill_downi() is
1232 		 * only called as part of ire_walk*() routines, so that
1233 		 * the irb_refhold() done by ire_walk*() will ensure that
1234 		 * ire_delete() does not lead to ire_inactive().
1235 		 */
1236 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1237 		ire_delete(ire);
1238 		if (ire->ire_unbound)
1239 			ire_rebind(ire);
1240 	}
1241 }
1242 
1243 /* Remove IRE_IF_CLONE on this ill */
1244 void
1245 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1246 {
1247 	ill_t	*ill = (ill_t *)ill_arg;
1248 
1249 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1250 	if (ire->ire_ill == ill)
1251 		ire_delete(ire);
1252 }
1253 
1254 /* Consume an M_IOCACK of the fastpath probe. */
1255 void
1256 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1257 {
1258 	mblk_t	*mp1 = mp;
1259 
1260 	/*
1261 	 * If this was the first attempt turn on the fastpath probing.
1262 	 */
1263 	mutex_enter(&ill->ill_lock);
1264 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1265 		ill->ill_dlpi_fastpath_state = IDS_OK;
1266 	mutex_exit(&ill->ill_lock);
1267 
1268 	/* Free the M_IOCACK mblk, hold on to the data */
1269 	mp = mp->b_cont;
1270 	freeb(mp1);
1271 	if (mp == NULL)
1272 		return;
1273 	if (mp->b_cont != NULL)
1274 		nce_fastpath_update(ill, mp);
1275 	else
1276 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1277 	freemsg(mp);
1278 }
1279 
1280 /*
1281  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1282  * The data portion of the request is a dl_unitdata_req_t template for
1283  * what we would send downstream in the absence of a fastpath confirmation.
1284  */
1285 int
1286 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1287 {
1288 	struct iocblk	*ioc;
1289 	mblk_t	*mp;
1290 
1291 	if (dlur_mp == NULL)
1292 		return (EINVAL);
1293 
1294 	mutex_enter(&ill->ill_lock);
1295 	switch (ill->ill_dlpi_fastpath_state) {
1296 	case IDS_FAILED:
1297 		/*
1298 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1299 		 * support it.
1300 		 */
1301 		mutex_exit(&ill->ill_lock);
1302 		return (ENOTSUP);
1303 	case IDS_UNKNOWN:
1304 		/* This is the first probe */
1305 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1306 		break;
1307 	default:
1308 		break;
1309 	}
1310 	mutex_exit(&ill->ill_lock);
1311 
1312 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1313 		return (EAGAIN);
1314 
1315 	mp->b_cont = copyb(dlur_mp);
1316 	if (mp->b_cont == NULL) {
1317 		freeb(mp);
1318 		return (EAGAIN);
1319 	}
1320 
1321 	ioc = (struct iocblk *)mp->b_rptr;
1322 	ioc->ioc_count = msgdsize(mp->b_cont);
1323 
1324 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1325 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1326 	putnext(ill->ill_wq, mp);
1327 	return (0);
1328 }
1329 
1330 void
1331 ill_capability_probe(ill_t *ill)
1332 {
1333 	mblk_t	*mp;
1334 
1335 	ASSERT(IAM_WRITER_ILL(ill));
1336 
1337 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1338 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1339 		return;
1340 
1341 	/*
1342 	 * We are starting a new cycle of capability negotiation.
1343 	 * Free up the capab reset messages of any previous incarnation.
1344 	 * We will do a fresh allocation when we get the response to our probe
1345 	 */
1346 	if (ill->ill_capab_reset_mp != NULL) {
1347 		freemsg(ill->ill_capab_reset_mp);
1348 		ill->ill_capab_reset_mp = NULL;
1349 	}
1350 
1351 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1352 
1353 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1354 	if (mp == NULL)
1355 		return;
1356 
1357 	ill_capability_send(ill, mp);
1358 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1359 }
1360 
1361 void
1362 ill_capability_reset(ill_t *ill, boolean_t reneg)
1363 {
1364 	ASSERT(IAM_WRITER_ILL(ill));
1365 
1366 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1367 		return;
1368 
1369 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1370 
1371 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1372 	ill->ill_capab_reset_mp = NULL;
1373 	/*
1374 	 * We turn off all capabilities except those pertaining to
1375 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1376 	 * which will be turned off by the corresponding reset functions.
1377 	 */
1378 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1379 }
1380 
1381 static void
1382 ill_capability_reset_alloc(ill_t *ill)
1383 {
1384 	mblk_t *mp;
1385 	size_t	size = 0;
1386 	int	err;
1387 	dl_capability_req_t	*capb;
1388 
1389 	ASSERT(IAM_WRITER_ILL(ill));
1390 	ASSERT(ill->ill_capab_reset_mp == NULL);
1391 
1392 	if (ILL_HCKSUM_CAPABLE(ill)) {
1393 		size += sizeof (dl_capability_sub_t) +
1394 		    sizeof (dl_capab_hcksum_t);
1395 	}
1396 
1397 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1398 		size += sizeof (dl_capability_sub_t) +
1399 		    sizeof (dl_capab_zerocopy_t);
1400 	}
1401 
1402 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1403 		size += sizeof (dl_capability_sub_t) +
1404 		    sizeof (dl_capab_dld_t);
1405 	}
1406 
1407 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1408 	    STR_NOSIG, &err);
1409 
1410 	mp->b_datap->db_type = M_PROTO;
1411 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1412 
1413 	capb = (dl_capability_req_t *)mp->b_rptr;
1414 	capb->dl_primitive = DL_CAPABILITY_REQ;
1415 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1416 	capb->dl_sub_length = size;
1417 
1418 	mp->b_wptr += sizeof (dl_capability_req_t);
1419 
1420 	/*
1421 	 * Each handler fills in the corresponding dl_capability_sub_t
1422 	 * inside the mblk,
1423 	 */
1424 	ill_capability_hcksum_reset_fill(ill, mp);
1425 	ill_capability_zerocopy_reset_fill(ill, mp);
1426 	ill_capability_dld_reset_fill(ill, mp);
1427 
1428 	ill->ill_capab_reset_mp = mp;
1429 }
1430 
1431 static void
1432 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1433 {
1434 	dl_capab_id_t *id_ic;
1435 	uint_t sub_dl_cap = outers->dl_cap;
1436 	dl_capability_sub_t *inners;
1437 	uint8_t *capend;
1438 
1439 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1440 
1441 	/*
1442 	 * Note: range checks here are not absolutely sufficient to
1443 	 * make us robust against malformed messages sent by drivers;
1444 	 * this is in keeping with the rest of IP's dlpi handling.
1445 	 * (Remember, it's coming from something else in the kernel
1446 	 * address space)
1447 	 */
1448 
1449 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1450 	if (capend > mp->b_wptr) {
1451 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1452 		    "malformed sub-capability too long for mblk");
1453 		return;
1454 	}
1455 
1456 	id_ic = (dl_capab_id_t *)(outers + 1);
1457 
1458 	if (outers->dl_length < sizeof (*id_ic) ||
1459 	    (inners = &id_ic->id_subcap,
1460 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1461 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1462 		    "encapsulated capab type %d too long for mblk",
1463 		    inners->dl_cap);
1464 		return;
1465 	}
1466 
1467 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1468 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1469 		    "isn't as expected; pass-thru module(s) detected, "
1470 		    "discarding capability\n", inners->dl_cap));
1471 		return;
1472 	}
1473 
1474 	/* Process the encapsulated sub-capability */
1475 	ill_capability_dispatch(ill, mp, inners);
1476 }
1477 
1478 static void
1479 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1480 {
1481 	dl_capability_sub_t *dl_subcap;
1482 
1483 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1484 		return;
1485 
1486 	/*
1487 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1488 	 * initialized below since it is not used by DLD.
1489 	 */
1490 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1491 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1492 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1493 
1494 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1495 }
1496 
1497 static void
1498 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1499 {
1500 	/*
1501 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1502 	 * is only to get the VRRP capability.
1503 	 *
1504 	 * Note that we cannot check ill_ipif_up_count here since
1505 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1506 	 * That is done asynchronously, and can race with this function.
1507 	 */
1508 	if (!ill->ill_dl_up) {
1509 		if (subp->dl_cap == DL_CAPAB_VRRP)
1510 			ill_capability_vrrp_ack(ill, mp, subp);
1511 		return;
1512 	}
1513 
1514 	switch (subp->dl_cap) {
1515 	case DL_CAPAB_HCKSUM:
1516 		ill_capability_hcksum_ack(ill, mp, subp);
1517 		break;
1518 	case DL_CAPAB_ZEROCOPY:
1519 		ill_capability_zerocopy_ack(ill, mp, subp);
1520 		break;
1521 	case DL_CAPAB_DLD:
1522 		ill_capability_dld_ack(ill, mp, subp);
1523 		break;
1524 	case DL_CAPAB_VRRP:
1525 		break;
1526 	default:
1527 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1528 		    subp->dl_cap));
1529 	}
1530 }
1531 
1532 /*
1533  * Process the vrrp capability received from a DLS Provider. isub must point
1534  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1535  */
1536 static void
1537 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1538 {
1539 	dl_capab_vrrp_t	*vrrp;
1540 	uint_t		sub_dl_cap = isub->dl_cap;
1541 	uint8_t		*capend;
1542 
1543 	ASSERT(IAM_WRITER_ILL(ill));
1544 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1545 
1546 	/*
1547 	 * Note: range checks here are not absolutely sufficient to
1548 	 * make us robust against malformed messages sent by drivers;
1549 	 * this is in keeping with the rest of IP's dlpi handling.
1550 	 * (Remember, it's coming from something else in the kernel
1551 	 * address space)
1552 	 */
1553 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1554 	if (capend > mp->b_wptr) {
1555 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1556 		    "malformed sub-capability too long for mblk");
1557 		return;
1558 	}
1559 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1560 
1561 	/*
1562 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1563 	 */
1564 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1565 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1566 		ill->ill_flags |= ILLF_VRRP;
1567 	}
1568 }
1569 
1570 /*
1571  * Process a hardware checksum offload capability negotiation ack received
1572  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1573  * of a DL_CAPABILITY_ACK message.
1574  */
1575 static void
1576 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1577 {
1578 	dl_capability_req_t	*ocap;
1579 	dl_capab_hcksum_t	*ihck, *ohck;
1580 	ill_hcksum_capab_t	**ill_hcksum;
1581 	mblk_t			*nmp = NULL;
1582 	uint_t			sub_dl_cap = isub->dl_cap;
1583 	uint8_t			*capend;
1584 
1585 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1586 
1587 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1588 
1589 	/*
1590 	 * Note: range checks here are not absolutely sufficient to
1591 	 * make us robust against malformed messages sent by drivers;
1592 	 * this is in keeping with the rest of IP's dlpi handling.
1593 	 * (Remember, it's coming from something else in the kernel
1594 	 * address space)
1595 	 */
1596 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1597 	if (capend > mp->b_wptr) {
1598 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1599 		    "malformed sub-capability too long for mblk");
1600 		return;
1601 	}
1602 
1603 	/*
1604 	 * There are two types of acks we process here:
1605 	 * 1. acks in reply to a (first form) generic capability req
1606 	 *    (no ENABLE flag set)
1607 	 * 2. acks in reply to a ENABLE capability req.
1608 	 *    (ENABLE flag set)
1609 	 */
1610 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1611 
1612 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1613 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1614 		    "unsupported hardware checksum "
1615 		    "sub-capability (version %d, expected %d)",
1616 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1617 		return;
1618 	}
1619 
1620 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1621 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1622 		    "checksum capability isn't as expected; pass-thru "
1623 		    "module(s) detected, discarding capability\n"));
1624 		return;
1625 	}
1626 
1627 #define	CURR_HCKSUM_CAPAB				\
1628 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1629 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1630 
1631 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1632 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1633 		/* do ENABLE processing */
1634 		if (*ill_hcksum == NULL) {
1635 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1636 			    KM_NOSLEEP);
1637 
1638 			if (*ill_hcksum == NULL) {
1639 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1640 				    "could not enable hcksum version %d "
1641 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1642 				    ill->ill_name);
1643 				return;
1644 			}
1645 		}
1646 
1647 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1648 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1649 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1650 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1651 		    "has enabled hardware checksumming\n ",
1652 		    ill->ill_name));
1653 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1654 		/*
1655 		 * Enabling hardware checksum offload
1656 		 * Currently IP supports {TCP,UDP}/IPv4
1657 		 * partial and full cksum offload and
1658 		 * IPv4 header checksum offload.
1659 		 * Allocate new mblk which will
1660 		 * contain a new capability request
1661 		 * to enable hardware checksum offload.
1662 		 */
1663 		uint_t	size;
1664 		uchar_t	*rptr;
1665 
1666 		size = sizeof (dl_capability_req_t) +
1667 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1668 
1669 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1670 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1671 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1672 			    ill->ill_name);
1673 			return;
1674 		}
1675 
1676 		rptr = nmp->b_rptr;
1677 		/* initialize dl_capability_req_t */
1678 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1679 		ocap->dl_sub_offset =
1680 		    sizeof (dl_capability_req_t);
1681 		ocap->dl_sub_length =
1682 		    sizeof (dl_capability_sub_t) +
1683 		    isub->dl_length;
1684 		nmp->b_rptr += sizeof (dl_capability_req_t);
1685 
1686 		/* initialize dl_capability_sub_t */
1687 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1688 		nmp->b_rptr += sizeof (*isub);
1689 
1690 		/* initialize dl_capab_hcksum_t */
1691 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1692 		bcopy(ihck, ohck, sizeof (*ihck));
1693 
1694 		nmp->b_rptr = rptr;
1695 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1696 
1697 		/* Set ENABLE flag */
1698 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1699 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1700 
1701 		/*
1702 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1703 		 * hardware checksum acceleration.
1704 		 */
1705 		ill_capability_send(ill, nmp);
1706 	} else {
1707 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1708 		    "advertised %x hardware checksum capability flags\n",
1709 		    ill->ill_name, ihck->hcksum_txflags));
1710 	}
1711 }
1712 
1713 static void
1714 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1715 {
1716 	dl_capab_hcksum_t *hck_subcap;
1717 	dl_capability_sub_t *dl_subcap;
1718 
1719 	if (!ILL_HCKSUM_CAPABLE(ill))
1720 		return;
1721 
1722 	ASSERT(ill->ill_hcksum_capab != NULL);
1723 
1724 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1725 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1726 	dl_subcap->dl_length = sizeof (*hck_subcap);
1727 
1728 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1729 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1730 	hck_subcap->hcksum_txflags = 0;
1731 
1732 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1733 }
1734 
1735 static void
1736 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1737 {
1738 	mblk_t *nmp = NULL;
1739 	dl_capability_req_t *oc;
1740 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1741 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1742 	uint_t sub_dl_cap = isub->dl_cap;
1743 	uint8_t *capend;
1744 
1745 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1746 
1747 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1748 
1749 	/*
1750 	 * Note: range checks here are not absolutely sufficient to
1751 	 * make us robust against malformed messages sent by drivers;
1752 	 * this is in keeping with the rest of IP's dlpi handling.
1753 	 * (Remember, it's coming from something else in the kernel
1754 	 * address space)
1755 	 */
1756 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1757 	if (capend > mp->b_wptr) {
1758 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1759 		    "malformed sub-capability too long for mblk");
1760 		return;
1761 	}
1762 
1763 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1764 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1765 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1766 		    "unsupported ZEROCOPY sub-capability (version %d, "
1767 		    "expected %d)", zc_ic->zerocopy_version,
1768 		    ZEROCOPY_VERSION_1);
1769 		return;
1770 	}
1771 
1772 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1773 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1774 		    "capability isn't as expected; pass-thru module(s) "
1775 		    "detected, discarding capability\n"));
1776 		return;
1777 	}
1778 
1779 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1780 		if (*ill_zerocopy_capab == NULL) {
1781 			*ill_zerocopy_capab =
1782 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1783 			    KM_NOSLEEP);
1784 
1785 			if (*ill_zerocopy_capab == NULL) {
1786 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1787 				    "could not enable Zero-copy version %d "
1788 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1789 				    ill->ill_name);
1790 				return;
1791 			}
1792 		}
1793 
1794 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1795 		    "supports Zero-copy version %d\n", ill->ill_name,
1796 		    ZEROCOPY_VERSION_1));
1797 
1798 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1799 		    zc_ic->zerocopy_version;
1800 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1801 		    zc_ic->zerocopy_flags;
1802 
1803 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1804 	} else {
1805 		uint_t size;
1806 		uchar_t *rptr;
1807 
1808 		size = sizeof (dl_capability_req_t) +
1809 		    sizeof (dl_capability_sub_t) +
1810 		    sizeof (dl_capab_zerocopy_t);
1811 
1812 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1813 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1814 			    "could not enable zerocopy for %s (ENOMEM)\n",
1815 			    ill->ill_name);
1816 			return;
1817 		}
1818 
1819 		rptr = nmp->b_rptr;
1820 		/* initialize dl_capability_req_t */
1821 		oc = (dl_capability_req_t *)rptr;
1822 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1823 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1824 		    sizeof (dl_capab_zerocopy_t);
1825 		rptr += sizeof (dl_capability_req_t);
1826 
1827 		/* initialize dl_capability_sub_t */
1828 		bcopy(isub, rptr, sizeof (*isub));
1829 		rptr += sizeof (*isub);
1830 
1831 		/* initialize dl_capab_zerocopy_t */
1832 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1833 		*zc_oc = *zc_ic;
1834 
1835 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1836 		    "to enable zero-copy version %d\n", ill->ill_name,
1837 		    ZEROCOPY_VERSION_1));
1838 
1839 		/* set VMSAFE_MEM flag */
1840 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1841 
1842 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1843 		ill_capability_send(ill, nmp);
1844 	}
1845 }
1846 
1847 static void
1848 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1849 {
1850 	dl_capab_zerocopy_t *zerocopy_subcap;
1851 	dl_capability_sub_t *dl_subcap;
1852 
1853 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1854 		return;
1855 
1856 	ASSERT(ill->ill_zerocopy_capab != NULL);
1857 
1858 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1859 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1860 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1861 
1862 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1863 	zerocopy_subcap->zerocopy_version =
1864 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1865 	zerocopy_subcap->zerocopy_flags = 0;
1866 
1867 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1868 }
1869 
1870 /*
1871  * DLD capability
1872  * Refer to dld.h for more information regarding the purpose and usage
1873  * of this capability.
1874  */
1875 static void
1876 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1877 {
1878 	dl_capab_dld_t		*dld_ic, dld;
1879 	uint_t			sub_dl_cap = isub->dl_cap;
1880 	uint8_t			*capend;
1881 	ill_dld_capab_t		*idc;
1882 
1883 	ASSERT(IAM_WRITER_ILL(ill));
1884 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1885 
1886 	/*
1887 	 * Note: range checks here are not absolutely sufficient to
1888 	 * make us robust against malformed messages sent by drivers;
1889 	 * this is in keeping with the rest of IP's dlpi handling.
1890 	 * (Remember, it's coming from something else in the kernel
1891 	 * address space)
1892 	 */
1893 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1894 	if (capend > mp->b_wptr) {
1895 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1896 		    "malformed sub-capability too long for mblk");
1897 		return;
1898 	}
1899 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1900 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1901 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1902 		    "unsupported DLD sub-capability (version %d, "
1903 		    "expected %d)", dld_ic->dld_version,
1904 		    DLD_CURRENT_VERSION);
1905 		return;
1906 	}
1907 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1908 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1909 		    "capability isn't as expected; pass-thru module(s) "
1910 		    "detected, discarding capability\n"));
1911 		return;
1912 	}
1913 
1914 	/*
1915 	 * Copy locally to ensure alignment.
1916 	 */
1917 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1918 
1919 	if ((idc = ill->ill_dld_capab) == NULL) {
1920 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1921 		if (idc == NULL) {
1922 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1923 			    "could not enable DLD version %d "
1924 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1925 			    ill->ill_name);
1926 			return;
1927 		}
1928 		ill->ill_dld_capab = idc;
1929 	}
1930 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1931 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1932 	ip1dbg(("ill_capability_dld_ack: interface %s "
1933 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1934 
1935 	ill_capability_dld_enable(ill);
1936 }
1937 
1938 /*
1939  * Typically capability negotiation between IP and the driver happens via
1940  * DLPI message exchange. However GLD also offers a direct function call
1941  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1942  * But arbitrary function calls into IP or GLD are not permitted, since both
1943  * of them are protected by their own perimeter mechanism. The perimeter can
1944  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1945  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1946  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1947  * to enter the mac perimeter and then do the direct function calls into
1948  * GLD to enable squeue polling. The ring related callbacks from the mac into
1949  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1950  * protected by the mac perimeter.
1951  */
1952 static void
1953 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1954 {
1955 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1956 	int			err;
1957 
1958 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1959 	    DLD_ENABLE);
1960 	ASSERT(err == 0);
1961 }
1962 
1963 static void
1964 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1965 {
1966 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1967 	int			err;
1968 
1969 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1970 	    DLD_DISABLE);
1971 	ASSERT(err == 0);
1972 }
1973 
1974 boolean_t
1975 ill_mac_perim_held(ill_t *ill)
1976 {
1977 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1978 
1979 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1980 	    DLD_QUERY));
1981 }
1982 
1983 static void
1984 ill_capability_direct_enable(ill_t *ill)
1985 {
1986 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1987 	ill_dld_direct_t	*idd = &idc->idc_direct;
1988 	dld_capab_direct_t	direct;
1989 	int			rc;
1990 
1991 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1992 
1993 	bzero(&direct, sizeof (direct));
1994 	direct.di_rx_cf = (uintptr_t)ip_input;
1995 	direct.di_rx_ch = ill;
1996 
1997 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1998 	    DLD_ENABLE);
1999 	if (rc == 0) {
2000 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2001 		idd->idd_tx_dh = direct.di_tx_dh;
2002 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2003 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2004 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2005 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2006 		ASSERT(idd->idd_tx_cb_df != NULL);
2007 		ASSERT(idd->idd_tx_fctl_df != NULL);
2008 		ASSERT(idd->idd_tx_df != NULL);
2009 		/*
2010 		 * One time registration of flow enable callback function
2011 		 */
2012 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2013 		    ill_flow_enable, ill);
2014 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2015 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2016 	} else {
2017 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2018 		    "capability, rc = %d\n", rc);
2019 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2020 	}
2021 }
2022 
2023 static void
2024 ill_capability_poll_enable(ill_t *ill)
2025 {
2026 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2027 	dld_capab_poll_t	poll;
2028 	int			rc;
2029 
2030 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2031 
2032 	bzero(&poll, sizeof (poll));
2033 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2034 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2035 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2036 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2037 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2038 	poll.poll_ring_ch = ill;
2039 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2040 	    DLD_ENABLE);
2041 	if (rc == 0) {
2042 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2043 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2044 	} else {
2045 		ip1dbg(("warning: could not enable POLL "
2046 		    "capability, rc = %d\n", rc));
2047 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2048 	}
2049 }
2050 
2051 /*
2052  * Enable the LSO capability.
2053  */
2054 static void
2055 ill_capability_lso_enable(ill_t *ill)
2056 {
2057 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2058 	dld_capab_lso_t	lso;
2059 	int rc;
2060 
2061 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2062 
2063 	if (ill->ill_lso_capab == NULL) {
2064 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2065 		    KM_NOSLEEP);
2066 		if (ill->ill_lso_capab == NULL) {
2067 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2068 			    "could not enable LSO for %s (ENOMEM)\n",
2069 			    ill->ill_name);
2070 			return;
2071 		}
2072 	}
2073 
2074 	bzero(&lso, sizeof (lso));
2075 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2076 	    DLD_ENABLE)) == 0) {
2077 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2078 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2079 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2080 		ip1dbg(("ill_capability_lso_enable: interface %s "
2081 		    "has enabled LSO\n ", ill->ill_name));
2082 	} else {
2083 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2084 		ill->ill_lso_capab = NULL;
2085 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2086 	}
2087 }
2088 
2089 static void
2090 ill_capability_dld_enable(ill_t *ill)
2091 {
2092 	mac_perim_handle_t mph;
2093 
2094 	ASSERT(IAM_WRITER_ILL(ill));
2095 
2096 	if (ill->ill_isv6)
2097 		return;
2098 
2099 	ill_mac_perim_enter(ill, &mph);
2100 	if (!ill->ill_isv6) {
2101 		ill_capability_direct_enable(ill);
2102 		ill_capability_poll_enable(ill);
2103 		ill_capability_lso_enable(ill);
2104 	}
2105 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2106 	ill_mac_perim_exit(ill, mph);
2107 }
2108 
2109 static void
2110 ill_capability_dld_disable(ill_t *ill)
2111 {
2112 	ill_dld_capab_t	*idc;
2113 	ill_dld_direct_t *idd;
2114 	mac_perim_handle_t	mph;
2115 
2116 	ASSERT(IAM_WRITER_ILL(ill));
2117 
2118 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2119 		return;
2120 
2121 	ill_mac_perim_enter(ill, &mph);
2122 
2123 	idc = ill->ill_dld_capab;
2124 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2125 		/*
2126 		 * For performance we avoid locks in the transmit data path
2127 		 * and don't maintain a count of the number of threads using
2128 		 * direct calls. Thus some threads could be using direct
2129 		 * transmit calls to GLD, even after the capability mechanism
2130 		 * turns it off. This is still safe since the handles used in
2131 		 * the direct calls continue to be valid until the unplumb is
2132 		 * completed. Remove the callback that was added (1-time) at
2133 		 * capab enable time.
2134 		 */
2135 		mutex_enter(&ill->ill_lock);
2136 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2137 		mutex_exit(&ill->ill_lock);
2138 		if (ill->ill_flownotify_mh != NULL) {
2139 			idd = &idc->idc_direct;
2140 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2141 			    ill->ill_flownotify_mh);
2142 			ill->ill_flownotify_mh = NULL;
2143 		}
2144 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2145 		    NULL, DLD_DISABLE);
2146 	}
2147 
2148 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2149 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2150 		ip_squeue_clean_all(ill);
2151 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2152 		    NULL, DLD_DISABLE);
2153 	}
2154 
2155 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2156 		ASSERT(ill->ill_lso_capab != NULL);
2157 		/*
2158 		 * Clear the capability flag for LSO but retain the
2159 		 * ill_lso_capab structure since it's possible that another
2160 		 * thread is still referring to it.  The structure only gets
2161 		 * deallocated when we destroy the ill.
2162 		 */
2163 
2164 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2165 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2166 		    NULL, DLD_DISABLE);
2167 	}
2168 
2169 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2170 	ill_mac_perim_exit(ill, mph);
2171 }
2172 
2173 /*
2174  * Capability Negotiation protocol
2175  *
2176  * We don't wait for DLPI capability operations to finish during interface
2177  * bringup or teardown. Doing so would introduce more asynchrony and the
2178  * interface up/down operations will need multiple return and restarts.
2179  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2180  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2181  * exclusive operation won't start until the DLPI operations of the previous
2182  * exclusive operation complete.
2183  *
2184  * The capability state machine is shown below.
2185  *
2186  * state		next state		event, action
2187  *
2188  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2189  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2190  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2191  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2192  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2193  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2194  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2195  *						    ill_capability_probe.
2196  */
2197 
2198 /*
2199  * Dedicated thread started from ip_stack_init that handles capability
2200  * disable. This thread ensures the taskq dispatch does not fail by waiting
2201  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2202  * that direct calls to DLD are done in a cv_waitable context.
2203  */
2204 void
2205 ill_taskq_dispatch(ip_stack_t *ipst)
2206 {
2207 	callb_cpr_t cprinfo;
2208 	char 	name[64];
2209 	mblk_t	*mp;
2210 
2211 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2212 	    ipst->ips_netstack->netstack_stackid);
2213 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2214 	    name);
2215 	mutex_enter(&ipst->ips_capab_taskq_lock);
2216 
2217 	for (;;) {
2218 		mp = ipst->ips_capab_taskq_head;
2219 		while (mp != NULL) {
2220 			ipst->ips_capab_taskq_head = mp->b_next;
2221 			if (ipst->ips_capab_taskq_head == NULL)
2222 				ipst->ips_capab_taskq_tail = NULL;
2223 			mutex_exit(&ipst->ips_capab_taskq_lock);
2224 			mp->b_next = NULL;
2225 
2226 			VERIFY(taskq_dispatch(system_taskq,
2227 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2228 			mutex_enter(&ipst->ips_capab_taskq_lock);
2229 			mp = ipst->ips_capab_taskq_head;
2230 		}
2231 
2232 		if (ipst->ips_capab_taskq_quit)
2233 			break;
2234 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2235 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2236 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2237 	}
2238 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2239 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2240 	CALLB_CPR_EXIT(&cprinfo);
2241 	thread_exit();
2242 }
2243 
2244 /*
2245  * Consume a new-style hardware capabilities negotiation ack.
2246  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2247  */
2248 static void
2249 ill_capability_ack_thr(void *arg)
2250 {
2251 	mblk_t	*mp = arg;
2252 	dl_capability_ack_t *capp;
2253 	dl_capability_sub_t *subp, *endp;
2254 	ill_t	*ill;
2255 	boolean_t reneg;
2256 
2257 	ill = (ill_t *)mp->b_prev;
2258 	mp->b_prev = NULL;
2259 
2260 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2261 
2262 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2263 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2264 		/*
2265 		 * We have received the ack for our DL_CAPAB reset request.
2266 		 * There isnt' anything in the message that needs processing.
2267 		 * All message based capabilities have been disabled, now
2268 		 * do the function call based capability disable.
2269 		 */
2270 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2271 		ill_capability_dld_disable(ill);
2272 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2273 		if (reneg)
2274 			ill_capability_probe(ill);
2275 		goto done;
2276 	}
2277 
2278 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2279 		ill->ill_dlpi_capab_state = IDCS_OK;
2280 
2281 	capp = (dl_capability_ack_t *)mp->b_rptr;
2282 
2283 	if (capp->dl_sub_length == 0) {
2284 		/* no new-style capabilities */
2285 		goto done;
2286 	}
2287 
2288 	/* make sure the driver supplied correct dl_sub_length */
2289 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2290 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2291 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2292 		goto done;
2293 	}
2294 
2295 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2296 	/*
2297 	 * There are sub-capabilities. Process the ones we know about.
2298 	 * Loop until we don't have room for another sub-cap header..
2299 	 */
2300 	for (subp = SC(capp, capp->dl_sub_offset),
2301 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2302 	    subp <= endp;
2303 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2304 
2305 		switch (subp->dl_cap) {
2306 		case DL_CAPAB_ID_WRAPPER:
2307 			ill_capability_id_ack(ill, mp, subp);
2308 			break;
2309 		default:
2310 			ill_capability_dispatch(ill, mp, subp);
2311 			break;
2312 		}
2313 	}
2314 #undef SC
2315 done:
2316 	inet_freemsg(mp);
2317 	ill_capability_done(ill);
2318 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2319 }
2320 
2321 /*
2322  * This needs to be started in a taskq thread to provide a cv_waitable
2323  * context.
2324  */
2325 void
2326 ill_capability_ack(ill_t *ill, mblk_t *mp)
2327 {
2328 	ip_stack_t	*ipst = ill->ill_ipst;
2329 
2330 	mp->b_prev = (mblk_t *)ill;
2331 	ASSERT(mp->b_next == NULL);
2332 
2333 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2334 	    TQ_NOSLEEP) != 0)
2335 		return;
2336 
2337 	/*
2338 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2339 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2340 	 */
2341 	mutex_enter(&ipst->ips_capab_taskq_lock);
2342 	if (ipst->ips_capab_taskq_head == NULL) {
2343 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2344 		ipst->ips_capab_taskq_head = mp;
2345 	} else {
2346 		ipst->ips_capab_taskq_tail->b_next = mp;
2347 	}
2348 	ipst->ips_capab_taskq_tail = mp;
2349 
2350 	cv_signal(&ipst->ips_capab_taskq_cv);
2351 	mutex_exit(&ipst->ips_capab_taskq_lock);
2352 }
2353 
2354 /*
2355  * This routine is called to scan the fragmentation reassembly table for
2356  * the specified ILL for any packets that are starting to smell.
2357  * dead_interval is the maximum time in seconds that will be tolerated.  It
2358  * will either be the value specified in ip_g_frag_timeout, or zero if the
2359  * ILL is shutting down and it is time to blow everything off.
2360  *
2361  * It returns the number of seconds (as a time_t) that the next frag timer
2362  * should be scheduled for, 0 meaning that the timer doesn't need to be
2363  * re-started.  Note that the method of calculating next_timeout isn't
2364  * entirely accurate since time will flow between the time we grab
2365  * current_time and the time we schedule the next timeout.  This isn't a
2366  * big problem since this is the timer for sending an ICMP reassembly time
2367  * exceeded messages, and it doesn't have to be exactly accurate.
2368  *
2369  * This function is
2370  * sometimes called as writer, although this is not required.
2371  */
2372 time_t
2373 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2374 {
2375 	ipfb_t	*ipfb;
2376 	ipfb_t	*endp;
2377 	ipf_t	*ipf;
2378 	ipf_t	*ipfnext;
2379 	mblk_t	*mp;
2380 	time_t	current_time = gethrestime_sec();
2381 	time_t	next_timeout = 0;
2382 	uint32_t	hdr_length;
2383 	mblk_t	*send_icmp_head;
2384 	mblk_t	*send_icmp_head_v6;
2385 	ip_stack_t *ipst = ill->ill_ipst;
2386 	ip_recv_attr_t iras;
2387 
2388 	bzero(&iras, sizeof (iras));
2389 	iras.ira_flags = 0;
2390 	iras.ira_ill = iras.ira_rill = ill;
2391 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2392 	iras.ira_rifindex = iras.ira_ruifindex;
2393 
2394 	ipfb = ill->ill_frag_hash_tbl;
2395 	if (ipfb == NULL)
2396 		return (B_FALSE);
2397 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2398 	/* Walk the frag hash table. */
2399 	for (; ipfb < endp; ipfb++) {
2400 		send_icmp_head = NULL;
2401 		send_icmp_head_v6 = NULL;
2402 		mutex_enter(&ipfb->ipfb_lock);
2403 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2404 			time_t frag_time = current_time - ipf->ipf_timestamp;
2405 			time_t frag_timeout;
2406 
2407 			if (frag_time < dead_interval) {
2408 				/*
2409 				 * There are some outstanding fragments
2410 				 * that will timeout later.  Make note of
2411 				 * the time so that we can reschedule the
2412 				 * next timeout appropriately.
2413 				 */
2414 				frag_timeout = dead_interval - frag_time;
2415 				if (next_timeout == 0 ||
2416 				    frag_timeout < next_timeout) {
2417 					next_timeout = frag_timeout;
2418 				}
2419 				break;
2420 			}
2421 			/* Time's up.  Get it out of here. */
2422 			hdr_length = ipf->ipf_nf_hdr_len;
2423 			ipfnext = ipf->ipf_hash_next;
2424 			if (ipfnext)
2425 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2426 			*ipf->ipf_ptphn = ipfnext;
2427 			mp = ipf->ipf_mp->b_cont;
2428 			for (; mp; mp = mp->b_cont) {
2429 				/* Extra points for neatness. */
2430 				IP_REASS_SET_START(mp, 0);
2431 				IP_REASS_SET_END(mp, 0);
2432 			}
2433 			mp = ipf->ipf_mp->b_cont;
2434 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2435 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2436 			ipfb->ipfb_count -= ipf->ipf_count;
2437 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2438 			ipfb->ipfb_frag_pkts--;
2439 			/*
2440 			 * We do not send any icmp message from here because
2441 			 * we currently are holding the ipfb_lock for this
2442 			 * hash chain. If we try and send any icmp messages
2443 			 * from here we may end up via a put back into ip
2444 			 * trying to get the same lock, causing a recursive
2445 			 * mutex panic. Instead we build a list and send all
2446 			 * the icmp messages after we have dropped the lock.
2447 			 */
2448 			if (ill->ill_isv6) {
2449 				if (hdr_length != 0) {
2450 					mp->b_next = send_icmp_head_v6;
2451 					send_icmp_head_v6 = mp;
2452 				} else {
2453 					freemsg(mp);
2454 				}
2455 			} else {
2456 				if (hdr_length != 0) {
2457 					mp->b_next = send_icmp_head;
2458 					send_icmp_head = mp;
2459 				} else {
2460 					freemsg(mp);
2461 				}
2462 			}
2463 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2464 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2465 			freeb(ipf->ipf_mp);
2466 		}
2467 		mutex_exit(&ipfb->ipfb_lock);
2468 		/*
2469 		 * Now need to send any icmp messages that we delayed from
2470 		 * above.
2471 		 */
2472 		while (send_icmp_head_v6 != NULL) {
2473 			ip6_t *ip6h;
2474 
2475 			mp = send_icmp_head_v6;
2476 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2477 			mp->b_next = NULL;
2478 			ip6h = (ip6_t *)mp->b_rptr;
2479 			iras.ira_flags = 0;
2480 			/*
2481 			 * This will result in an incorrect ALL_ZONES zoneid
2482 			 * for multicast packets, but we
2483 			 * don't send ICMP errors for those in any case.
2484 			 */
2485 			iras.ira_zoneid =
2486 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2487 			    ill, ipst);
2488 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2489 			icmp_time_exceeded_v6(mp,
2490 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2491 			    &iras);
2492 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2493 		}
2494 		while (send_icmp_head != NULL) {
2495 			ipaddr_t dst;
2496 
2497 			mp = send_icmp_head;
2498 			send_icmp_head = send_icmp_head->b_next;
2499 			mp->b_next = NULL;
2500 
2501 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2502 
2503 			iras.ira_flags = IRAF_IS_IPV4;
2504 			/*
2505 			 * This will result in an incorrect ALL_ZONES zoneid
2506 			 * for broadcast and multicast packets, but we
2507 			 * don't send ICMP errors for those in any case.
2508 			 */
2509 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2510 			    ill, ipst);
2511 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2512 			icmp_time_exceeded(mp,
2513 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2514 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2515 		}
2516 	}
2517 	/*
2518 	 * A non-dying ILL will use the return value to decide whether to
2519 	 * restart the frag timer, and for how long.
2520 	 */
2521 	return (next_timeout);
2522 }
2523 
2524 /*
2525  * This routine is called when the approximate count of mblk memory used
2526  * for the specified ILL has exceeded max_count.
2527  */
2528 void
2529 ill_frag_prune(ill_t *ill, uint_t max_count)
2530 {
2531 	ipfb_t	*ipfb;
2532 	ipf_t	*ipf;
2533 	size_t	count;
2534 	clock_t now;
2535 
2536 	/*
2537 	 * If we are here within ip_min_frag_prune_time msecs remove
2538 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2539 	 * ill_frag_free_num_pkts.
2540 	 */
2541 	mutex_enter(&ill->ill_lock);
2542 	now = ddi_get_lbolt();
2543 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2544 	    (ip_min_frag_prune_time != 0 ?
2545 	    ip_min_frag_prune_time : msec_per_tick)) {
2546 
2547 		ill->ill_frag_free_num_pkts++;
2548 
2549 	} else {
2550 		ill->ill_frag_free_num_pkts = 0;
2551 	}
2552 	ill->ill_last_frag_clean_time = now;
2553 	mutex_exit(&ill->ill_lock);
2554 
2555 	/*
2556 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2557 	 */
2558 	if (ill->ill_frag_free_num_pkts != 0) {
2559 		int ix;
2560 
2561 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2562 			ipfb = &ill->ill_frag_hash_tbl[ix];
2563 			mutex_enter(&ipfb->ipfb_lock);
2564 			if (ipfb->ipfb_ipf != NULL) {
2565 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2566 				    ill->ill_frag_free_num_pkts);
2567 			}
2568 			mutex_exit(&ipfb->ipfb_lock);
2569 		}
2570 	}
2571 	/*
2572 	 * While the reassembly list for this ILL is too big, prune a fragment
2573 	 * queue by age, oldest first.
2574 	 */
2575 	while (ill->ill_frag_count > max_count) {
2576 		int	ix;
2577 		ipfb_t	*oipfb = NULL;
2578 		uint_t	oldest = UINT_MAX;
2579 
2580 		count = 0;
2581 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2582 			ipfb = &ill->ill_frag_hash_tbl[ix];
2583 			mutex_enter(&ipfb->ipfb_lock);
2584 			ipf = ipfb->ipfb_ipf;
2585 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2586 				oldest = ipf->ipf_gen;
2587 				oipfb = ipfb;
2588 			}
2589 			count += ipfb->ipfb_count;
2590 			mutex_exit(&ipfb->ipfb_lock);
2591 		}
2592 		if (oipfb == NULL)
2593 			break;
2594 
2595 		if (count <= max_count)
2596 			return;	/* Somebody beat us to it, nothing to do */
2597 		mutex_enter(&oipfb->ipfb_lock);
2598 		ipf = oipfb->ipfb_ipf;
2599 		if (ipf != NULL) {
2600 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2601 		}
2602 		mutex_exit(&oipfb->ipfb_lock);
2603 	}
2604 }
2605 
2606 /*
2607  * free 'free_cnt' fragmented packets starting at ipf.
2608  */
2609 void
2610 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2611 {
2612 	size_t	count;
2613 	mblk_t	*mp;
2614 	mblk_t	*tmp;
2615 	ipf_t **ipfp = ipf->ipf_ptphn;
2616 
2617 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2618 	ASSERT(ipfp != NULL);
2619 	ASSERT(ipf != NULL);
2620 
2621 	while (ipf != NULL && free_cnt-- > 0) {
2622 		count = ipf->ipf_count;
2623 		mp = ipf->ipf_mp;
2624 		ipf = ipf->ipf_hash_next;
2625 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2626 			IP_REASS_SET_START(tmp, 0);
2627 			IP_REASS_SET_END(tmp, 0);
2628 		}
2629 		atomic_add_32(&ill->ill_frag_count, -count);
2630 		ASSERT(ipfb->ipfb_count >= count);
2631 		ipfb->ipfb_count -= count;
2632 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2633 		ipfb->ipfb_frag_pkts--;
2634 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2635 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2636 		freemsg(mp);
2637 	}
2638 
2639 	if (ipf)
2640 		ipf->ipf_ptphn = ipfp;
2641 	ipfp[0] = ipf;
2642 }
2643 
2644 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2645 	"obsolete and may be removed in a future release of Solaris.  Use " \
2646 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2647 
2648 /*
2649  * For obsolete per-interface forwarding configuration;
2650  * called in response to ND_GET.
2651  */
2652 /* ARGSUSED */
2653 static int
2654 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2655 {
2656 	ill_t *ill = (ill_t *)cp;
2657 
2658 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2659 
2660 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2661 	return (0);
2662 }
2663 
2664 /*
2665  * For obsolete per-interface forwarding configuration;
2666  * called in response to ND_SET.
2667  */
2668 /* ARGSUSED */
2669 static int
2670 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2671     cred_t *ioc_cr)
2672 {
2673 	long value;
2674 	int retval;
2675 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2676 
2677 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2678 
2679 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2680 	    value < 0 || value > 1) {
2681 		return (EINVAL);
2682 	}
2683 
2684 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2685 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2686 	rw_exit(&ipst->ips_ill_g_lock);
2687 	return (retval);
2688 }
2689 
2690 /*
2691  * Helper function for ill_forward_set().
2692  */
2693 static void
2694 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2695 {
2696 	ip_stack_t	*ipst = ill->ill_ipst;
2697 
2698 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2699 
2700 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2701 	    (enable ? "Enabling" : "Disabling"),
2702 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2703 	mutex_enter(&ill->ill_lock);
2704 	if (enable)
2705 		ill->ill_flags |= ILLF_ROUTER;
2706 	else
2707 		ill->ill_flags &= ~ILLF_ROUTER;
2708 	mutex_exit(&ill->ill_lock);
2709 	if (ill->ill_isv6)
2710 		ill_set_nce_router_flags(ill, enable);
2711 	/* Notify routing socket listeners of this change. */
2712 	if (ill->ill_ipif != NULL)
2713 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2714 }
2715 
2716 /*
2717  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2718  * socket messages for each interface whose flags we change.
2719  */
2720 int
2721 ill_forward_set(ill_t *ill, boolean_t enable)
2722 {
2723 	ipmp_illgrp_t *illg;
2724 	ip_stack_t *ipst = ill->ill_ipst;
2725 
2726 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2727 
2728 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2729 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2730 		return (0);
2731 
2732 	if (IS_LOOPBACK(ill))
2733 		return (EINVAL);
2734 
2735 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2736 		/*
2737 		 * Update all of the interfaces in the group.
2738 		 */
2739 		illg = ill->ill_grp;
2740 		ill = list_head(&illg->ig_if);
2741 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2742 			ill_forward_set_on_ill(ill, enable);
2743 
2744 		/*
2745 		 * Update the IPMP meta-interface.
2746 		 */
2747 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2748 		return (0);
2749 	}
2750 
2751 	ill_forward_set_on_ill(ill, enable);
2752 	return (0);
2753 }
2754 
2755 /*
2756  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2757  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2758  * set or clear.
2759  */
2760 static void
2761 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2762 {
2763 	ipif_t *ipif;
2764 	ncec_t *ncec;
2765 	nce_t *nce;
2766 
2767 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2768 		/*
2769 		 * NOTE: we match across the illgrp because nce's for
2770 		 * addresses on IPMP interfaces have an nce_ill that points to
2771 		 * the bound underlying ill.
2772 		 */
2773 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2774 		if (nce != NULL) {
2775 			ncec = nce->nce_common;
2776 			mutex_enter(&ncec->ncec_lock);
2777 			if (enable)
2778 				ncec->ncec_flags |= NCE_F_ISROUTER;
2779 			else
2780 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2781 			mutex_exit(&ncec->ncec_lock);
2782 			nce_refrele(nce);
2783 		}
2784 	}
2785 }
2786 
2787 /*
2788  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2789  * for this ill.  Make sure the v6/v4 question has been answered about this
2790  * ill.  The creation of this ndd variable is only for backwards compatibility.
2791  * The preferred way to control per-interface IP forwarding is through the
2792  * ILLF_ROUTER interface flag.
2793  */
2794 static int
2795 ill_set_ndd_name(ill_t *ill)
2796 {
2797 	char *suffix;
2798 	ip_stack_t	*ipst = ill->ill_ipst;
2799 
2800 	ASSERT(IAM_WRITER_ILL(ill));
2801 
2802 	if (ill->ill_isv6)
2803 		suffix = ipv6_forward_suffix;
2804 	else
2805 		suffix = ipv4_forward_suffix;
2806 
2807 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2808 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2809 	/*
2810 	 * Copies over the '\0'.
2811 	 * Note that strlen(suffix) is always bounded.
2812 	 */
2813 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2814 	    strlen(suffix) + 1);
2815 
2816 	/*
2817 	 * Use of the nd table requires holding the reader lock.
2818 	 * Modifying the nd table thru nd_load/nd_unload requires
2819 	 * the writer lock.
2820 	 */
2821 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2822 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2823 	    nd_ill_forward_set, (caddr_t)ill)) {
2824 		/*
2825 		 * If the nd_load failed, it only meant that it could not
2826 		 * allocate a new bunch of room for further NDD expansion.
2827 		 * Because of that, the ill_ndd_name will be set to 0, and
2828 		 * this interface is at the mercy of the global ip_forwarding
2829 		 * variable.
2830 		 */
2831 		rw_exit(&ipst->ips_ip_g_nd_lock);
2832 		ill->ill_ndd_name = NULL;
2833 		return (ENOMEM);
2834 	}
2835 	rw_exit(&ipst->ips_ip_g_nd_lock);
2836 	return (0);
2837 }
2838 
2839 /*
2840  * Intializes the context structure and returns the first ill in the list
2841  * cuurently start_list and end_list can have values:
2842  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2843  * IP_V4_G_HEAD		Traverse IPV4 list only.
2844  * IP_V6_G_HEAD		Traverse IPV6 list only.
2845  */
2846 
2847 /*
2848  * We don't check for CONDEMNED ills here. Caller must do that if
2849  * necessary under the ill lock.
2850  */
2851 ill_t *
2852 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2853     ip_stack_t *ipst)
2854 {
2855 	ill_if_t *ifp;
2856 	ill_t *ill;
2857 	avl_tree_t *avl_tree;
2858 
2859 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2860 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2861 
2862 	/*
2863 	 * setup the lists to search
2864 	 */
2865 	if (end_list != MAX_G_HEADS) {
2866 		ctx->ctx_current_list = start_list;
2867 		ctx->ctx_last_list = end_list;
2868 	} else {
2869 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2870 		ctx->ctx_current_list = 0;
2871 	}
2872 
2873 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2874 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2875 		if (ifp != (ill_if_t *)
2876 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2877 			avl_tree = &ifp->illif_avl_by_ppa;
2878 			ill = avl_first(avl_tree);
2879 			/*
2880 			 * ill is guaranteed to be non NULL or ifp should have
2881 			 * not existed.
2882 			 */
2883 			ASSERT(ill != NULL);
2884 			return (ill);
2885 		}
2886 		ctx->ctx_current_list++;
2887 	}
2888 
2889 	return (NULL);
2890 }
2891 
2892 /*
2893  * returns the next ill in the list. ill_first() must have been called
2894  * before calling ill_next() or bad things will happen.
2895  */
2896 
2897 /*
2898  * We don't check for CONDEMNED ills here. Caller must do that if
2899  * necessary under the ill lock.
2900  */
2901 ill_t *
2902 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2903 {
2904 	ill_if_t *ifp;
2905 	ill_t *ill;
2906 	ip_stack_t	*ipst = lastill->ill_ipst;
2907 
2908 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2909 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2910 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2911 	    AVL_AFTER)) != NULL) {
2912 		return (ill);
2913 	}
2914 
2915 	/* goto next ill_ifp in the list. */
2916 	ifp = lastill->ill_ifptr->illif_next;
2917 
2918 	/* make sure not at end of circular list */
2919 	while (ifp ==
2920 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2921 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2922 			return (NULL);
2923 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2924 	}
2925 
2926 	return (avl_first(&ifp->illif_avl_by_ppa));
2927 }
2928 
2929 /*
2930  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2931  * The final number (PPA) must not have any leading zeros.  Upon success, a
2932  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2933  */
2934 static char *
2935 ill_get_ppa_ptr(char *name)
2936 {
2937 	int namelen = strlen(name);
2938 	int end_ndx = namelen - 1;
2939 	int ppa_ndx, i;
2940 
2941 	/*
2942 	 * Check that the first character is [a-zA-Z], and that the last
2943 	 * character is [0-9].
2944 	 */
2945 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2946 		return (NULL);
2947 
2948 	/*
2949 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2950 	 */
2951 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2952 		if (!isdigit(name[ppa_ndx - 1]))
2953 			break;
2954 
2955 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2956 		return (NULL);
2957 
2958 	/*
2959 	 * Check that the intermediate characters are [a-z0-9.]
2960 	 */
2961 	for (i = 1; i < ppa_ndx; i++) {
2962 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2963 		    name[i] != '.' && name[i] != '_') {
2964 			return (NULL);
2965 		}
2966 	}
2967 
2968 	return (name + ppa_ndx);
2969 }
2970 
2971 /*
2972  * use avl tree to locate the ill.
2973  */
2974 static ill_t *
2975 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2976 {
2977 	char *ppa_ptr = NULL;
2978 	int len;
2979 	uint_t ppa;
2980 	ill_t *ill = NULL;
2981 	ill_if_t *ifp;
2982 	int list;
2983 
2984 	/*
2985 	 * get ppa ptr
2986 	 */
2987 	if (isv6)
2988 		list = IP_V6_G_HEAD;
2989 	else
2990 		list = IP_V4_G_HEAD;
2991 
2992 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2993 		return (NULL);
2994 	}
2995 
2996 	len = ppa_ptr - name + 1;
2997 
2998 	ppa = stoi(&ppa_ptr);
2999 
3000 	ifp = IP_VX_ILL_G_LIST(list, ipst);
3001 
3002 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3003 		/*
3004 		 * match is done on len - 1 as the name is not null
3005 		 * terminated it contains ppa in addition to the interface
3006 		 * name.
3007 		 */
3008 		if ((ifp->illif_name_len == len) &&
3009 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3010 			break;
3011 		} else {
3012 			ifp = ifp->illif_next;
3013 		}
3014 	}
3015 
3016 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3017 		/*
3018 		 * Even the interface type does not exist.
3019 		 */
3020 		return (NULL);
3021 	}
3022 
3023 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
3024 	if (ill != NULL) {
3025 		mutex_enter(&ill->ill_lock);
3026 		if (ILL_CAN_LOOKUP(ill)) {
3027 			ill_refhold_locked(ill);
3028 			mutex_exit(&ill->ill_lock);
3029 			return (ill);
3030 		}
3031 		mutex_exit(&ill->ill_lock);
3032 	}
3033 	return (NULL);
3034 }
3035 
3036 /*
3037  * comparison function for use with avl.
3038  */
3039 static int
3040 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3041 {
3042 	uint_t ppa;
3043 	uint_t ill_ppa;
3044 
3045 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3046 
3047 	ppa = *((uint_t *)ppa_ptr);
3048 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3049 	/*
3050 	 * We want the ill with the lowest ppa to be on the
3051 	 * top.
3052 	 */
3053 	if (ill_ppa < ppa)
3054 		return (1);
3055 	if (ill_ppa > ppa)
3056 		return (-1);
3057 	return (0);
3058 }
3059 
3060 /*
3061  * remove an interface type from the global list.
3062  */
3063 static void
3064 ill_delete_interface_type(ill_if_t *interface)
3065 {
3066 	ASSERT(interface != NULL);
3067 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3068 
3069 	avl_destroy(&interface->illif_avl_by_ppa);
3070 	if (interface->illif_ppa_arena != NULL)
3071 		vmem_destroy(interface->illif_ppa_arena);
3072 
3073 	remque(interface);
3074 
3075 	mi_free(interface);
3076 }
3077 
3078 /*
3079  * remove ill from the global list.
3080  */
3081 static void
3082 ill_glist_delete(ill_t *ill)
3083 {
3084 	ip_stack_t	*ipst;
3085 	phyint_t	*phyi;
3086 
3087 	if (ill == NULL)
3088 		return;
3089 	ipst = ill->ill_ipst;
3090 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3091 
3092 	/*
3093 	 * If the ill was never inserted into the AVL tree
3094 	 * we skip the if branch.
3095 	 */
3096 	if (ill->ill_ifptr != NULL) {
3097 		/*
3098 		 * remove from AVL tree and free ppa number
3099 		 */
3100 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3101 
3102 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3103 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3104 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3105 		}
3106 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3107 			ill_delete_interface_type(ill->ill_ifptr);
3108 		}
3109 
3110 		/*
3111 		 * Indicate ill is no longer in the list.
3112 		 */
3113 		ill->ill_ifptr = NULL;
3114 		ill->ill_name_length = 0;
3115 		ill->ill_name[0] = '\0';
3116 		ill->ill_ppa = UINT_MAX;
3117 	}
3118 
3119 	/* Generate one last event for this ill. */
3120 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3121 	    ill->ill_name_length);
3122 
3123 	ASSERT(ill->ill_phyint != NULL);
3124 	phyi = ill->ill_phyint;
3125 	ill->ill_phyint = NULL;
3126 
3127 	/*
3128 	 * ill_init allocates a phyint always to store the copy
3129 	 * of flags relevant to phyint. At that point in time, we could
3130 	 * not assign the name and hence phyint_illv4/v6 could not be
3131 	 * initialized. Later in ipif_set_values, we assign the name to
3132 	 * the ill, at which point in time we assign phyint_illv4/v6.
3133 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3134 	 */
3135 	if (ill->ill_flags & ILLF_IPV6)
3136 		phyi->phyint_illv6 = NULL;
3137 	else
3138 		phyi->phyint_illv4 = NULL;
3139 
3140 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3141 		rw_exit(&ipst->ips_ill_g_lock);
3142 		return;
3143 	}
3144 
3145 	/*
3146 	 * There are no ills left on this phyint; pull it out of the phyint
3147 	 * avl trees, and free it.
3148 	 */
3149 	if (phyi->phyint_ifindex > 0) {
3150 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3151 		    phyi);
3152 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3153 		    phyi);
3154 	}
3155 	rw_exit(&ipst->ips_ill_g_lock);
3156 
3157 	phyint_free(phyi);
3158 }
3159 
3160 /*
3161  * allocate a ppa, if the number of plumbed interfaces of this type are
3162  * less than ill_no_arena do a linear search to find a unused ppa.
3163  * When the number goes beyond ill_no_arena switch to using an arena.
3164  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3165  * is the return value for an error condition, so allocation starts at one
3166  * and is decremented by one.
3167  */
3168 static int
3169 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3170 {
3171 	ill_t *tmp_ill;
3172 	uint_t start, end;
3173 	int ppa;
3174 
3175 	if (ifp->illif_ppa_arena == NULL &&
3176 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3177 		/*
3178 		 * Create an arena.
3179 		 */
3180 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3181 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3182 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3183 			/* allocate what has already been assigned */
3184 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3185 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3186 		    tmp_ill, AVL_AFTER)) {
3187 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3188 			    1,		/* size */
3189 			    1,		/* align/quantum */
3190 			    0,		/* phase */
3191 			    0,		/* nocross */
3192 			    /* minaddr */
3193 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3194 			    /* maxaddr */
3195 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3196 			    VM_NOSLEEP|VM_FIRSTFIT);
3197 			if (ppa == 0) {
3198 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3199 				    " failed while switching"));
3200 				vmem_destroy(ifp->illif_ppa_arena);
3201 				ifp->illif_ppa_arena = NULL;
3202 				break;
3203 			}
3204 		}
3205 	}
3206 
3207 	if (ifp->illif_ppa_arena != NULL) {
3208 		if (ill->ill_ppa == UINT_MAX) {
3209 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3210 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3211 			if (ppa == 0)
3212 				return (EAGAIN);
3213 			ill->ill_ppa = --ppa;
3214 		} else {
3215 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3216 			    1, 		/* size */
3217 			    1, 		/* align/quantum */
3218 			    0, 		/* phase */
3219 			    0, 		/* nocross */
3220 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3221 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3222 			    VM_NOSLEEP|VM_FIRSTFIT);
3223 			/*
3224 			 * Most likely the allocation failed because
3225 			 * the requested ppa was in use.
3226 			 */
3227 			if (ppa == 0)
3228 				return (EEXIST);
3229 		}
3230 		return (0);
3231 	}
3232 
3233 	/*
3234 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3235 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3236 	 */
3237 	if (ill->ill_ppa == UINT_MAX) {
3238 		end = UINT_MAX - 1;
3239 		start = 0;
3240 	} else {
3241 		end = start = ill->ill_ppa;
3242 	}
3243 
3244 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3245 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3246 		if (start++ >= end) {
3247 			if (ill->ill_ppa == UINT_MAX)
3248 				return (EAGAIN);
3249 			else
3250 				return (EEXIST);
3251 		}
3252 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3253 	}
3254 	ill->ill_ppa = start;
3255 	return (0);
3256 }
3257 
3258 /*
3259  * Insert ill into the list of configured ill's. Once this function completes,
3260  * the ill is globally visible and is available through lookups. More precisely
3261  * this happens after the caller drops the ill_g_lock.
3262  */
3263 static int
3264 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3265 {
3266 	ill_if_t *ill_interface;
3267 	avl_index_t where = 0;
3268 	int error;
3269 	int name_length;
3270 	int index;
3271 	boolean_t check_length = B_FALSE;
3272 	ip_stack_t	*ipst = ill->ill_ipst;
3273 
3274 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3275 
3276 	name_length = mi_strlen(name) + 1;
3277 
3278 	if (isv6)
3279 		index = IP_V6_G_HEAD;
3280 	else
3281 		index = IP_V4_G_HEAD;
3282 
3283 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3284 	/*
3285 	 * Search for interface type based on name
3286 	 */
3287 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3288 		if ((ill_interface->illif_name_len == name_length) &&
3289 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3290 			break;
3291 		}
3292 		ill_interface = ill_interface->illif_next;
3293 	}
3294 
3295 	/*
3296 	 * Interface type not found, create one.
3297 	 */
3298 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3299 		ill_g_head_t ghead;
3300 
3301 		/*
3302 		 * allocate ill_if_t structure
3303 		 */
3304 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3305 		if (ill_interface == NULL) {
3306 			return (ENOMEM);
3307 		}
3308 
3309 		(void) strcpy(ill_interface->illif_name, name);
3310 		ill_interface->illif_name_len = name_length;
3311 
3312 		avl_create(&ill_interface->illif_avl_by_ppa,
3313 		    ill_compare_ppa, sizeof (ill_t),
3314 		    offsetof(struct ill_s, ill_avl_byppa));
3315 
3316 		/*
3317 		 * link the structure in the back to maintain order
3318 		 * of configuration for ifconfig output.
3319 		 */
3320 		ghead = ipst->ips_ill_g_heads[index];
3321 		insque(ill_interface, ghead.ill_g_list_tail);
3322 	}
3323 
3324 	if (ill->ill_ppa == UINT_MAX)
3325 		check_length = B_TRUE;
3326 
3327 	error = ill_alloc_ppa(ill_interface, ill);
3328 	if (error != 0) {
3329 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3330 			ill_delete_interface_type(ill->ill_ifptr);
3331 		return (error);
3332 	}
3333 
3334 	/*
3335 	 * When the ppa is choosen by the system, check that there is
3336 	 * enough space to insert ppa. if a specific ppa was passed in this
3337 	 * check is not required as the interface name passed in will have
3338 	 * the right ppa in it.
3339 	 */
3340 	if (check_length) {
3341 		/*
3342 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3343 		 */
3344 		char buf[sizeof (uint_t) * 3];
3345 
3346 		/*
3347 		 * convert ppa to string to calculate the amount of space
3348 		 * required for it in the name.
3349 		 */
3350 		numtos(ill->ill_ppa, buf);
3351 
3352 		/* Do we have enough space to insert ppa ? */
3353 
3354 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3355 			/* Free ppa and interface type struct */
3356 			if (ill_interface->illif_ppa_arena != NULL) {
3357 				vmem_free(ill_interface->illif_ppa_arena,
3358 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3359 			}
3360 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3361 				ill_delete_interface_type(ill->ill_ifptr);
3362 
3363 			return (EINVAL);
3364 		}
3365 	}
3366 
3367 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3368 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3369 
3370 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3371 	    &where);
3372 	ill->ill_ifptr = ill_interface;
3373 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3374 
3375 	ill_phyint_reinit(ill);
3376 	return (0);
3377 }
3378 
3379 /* Initialize the per phyint ipsq used for serialization */
3380 static boolean_t
3381 ipsq_init(ill_t *ill, boolean_t enter)
3382 {
3383 	ipsq_t  *ipsq;
3384 	ipxop_t	*ipx;
3385 
3386 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3387 		return (B_FALSE);
3388 
3389 	ill->ill_phyint->phyint_ipsq = ipsq;
3390 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3391 	ipx->ipx_ipsq = ipsq;
3392 	ipsq->ipsq_next = ipsq;
3393 	ipsq->ipsq_phyint = ill->ill_phyint;
3394 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3395 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3396 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3397 	if (enter) {
3398 		ipx->ipx_writer = curthread;
3399 		ipx->ipx_forced = B_FALSE;
3400 		ipx->ipx_reentry_cnt = 1;
3401 #ifdef DEBUG
3402 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3403 #endif
3404 	}
3405 	return (B_TRUE);
3406 }
3407 
3408 /*
3409  * ill_init is called by ip_open when a device control stream is opened.
3410  * It does a few initializations, and shoots a DL_INFO_REQ message down
3411  * to the driver.  The response is later picked up in ip_rput_dlpi and
3412  * used to set up default mechanisms for talking to the driver.  (Always
3413  * called as writer.)
3414  *
3415  * If this function returns error, ip_open will call ip_close which in
3416  * turn will call ill_delete to clean up any memory allocated here that
3417  * is not yet freed.
3418  */
3419 int
3420 ill_init(queue_t *q, ill_t *ill)
3421 {
3422 	int	count;
3423 	dl_info_req_t	*dlir;
3424 	mblk_t	*info_mp;
3425 	uchar_t *frag_ptr;
3426 
3427 	/*
3428 	 * The ill is initialized to zero by mi_alloc*(). In addition
3429 	 * some fields already contain valid values, initialized in
3430 	 * ip_open(), before we reach here.
3431 	 */
3432 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3433 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3434 	ill->ill_saved_ire_cnt = 0;
3435 
3436 	ill->ill_rq = q;
3437 	ill->ill_wq = WR(q);
3438 
3439 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3440 	    BPRI_HI);
3441 	if (info_mp == NULL)
3442 		return (ENOMEM);
3443 
3444 	/*
3445 	 * Allocate sufficient space to contain our fragment hash table and
3446 	 * the device name.
3447 	 */
3448 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3449 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3450 	if (frag_ptr == NULL) {
3451 		freemsg(info_mp);
3452 		return (ENOMEM);
3453 	}
3454 	ill->ill_frag_ptr = frag_ptr;
3455 	ill->ill_frag_free_num_pkts = 0;
3456 	ill->ill_last_frag_clean_time = 0;
3457 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3458 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3459 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3460 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3461 		    NULL, MUTEX_DEFAULT, NULL);
3462 	}
3463 
3464 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3465 	if (ill->ill_phyint == NULL) {
3466 		freemsg(info_mp);
3467 		mi_free(frag_ptr);
3468 		return (ENOMEM);
3469 	}
3470 
3471 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3472 	/*
3473 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3474 	 * at this point because of the following reason. If we can't
3475 	 * enter the ipsq at some point and cv_wait, the writer that
3476 	 * wakes us up tries to locate us using the list of all phyints
3477 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3478 	 * If we don't set it now, we risk a missed wakeup.
3479 	 */
3480 	ill->ill_phyint->phyint_illv4 = ill;
3481 	ill->ill_ppa = UINT_MAX;
3482 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3483 
3484 	ill_set_inputfn(ill);
3485 
3486 	if (!ipsq_init(ill, B_TRUE)) {
3487 		freemsg(info_mp);
3488 		mi_free(frag_ptr);
3489 		mi_free(ill->ill_phyint);
3490 		return (ENOMEM);
3491 	}
3492 
3493 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3494 
3495 	/* Frag queue limit stuff */
3496 	ill->ill_frag_count = 0;
3497 	ill->ill_ipf_gen = 0;
3498 
3499 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3500 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3501 	ill->ill_global_timer = INFINITY;
3502 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3503 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3504 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3505 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3506 
3507 	/*
3508 	 * Initialize IPv6 configuration variables.  The IP module is always
3509 	 * opened as an IPv4 module.  Instead tracking down the cases where
3510 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3511 	 * here for convenience, this has no effect until the ill is set to do
3512 	 * IPv6.
3513 	 */
3514 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3515 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3516 	ill->ill_max_buf = ND_MAX_Q;
3517 	ill->ill_refcnt = 0;
3518 
3519 	/* Send down the Info Request to the driver. */
3520 	info_mp->b_datap->db_type = M_PCPROTO;
3521 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3522 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3523 	dlir->dl_primitive = DL_INFO_REQ;
3524 
3525 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3526 
3527 	qprocson(q);
3528 	ill_dlpi_send(ill, info_mp);
3529 
3530 	return (0);
3531 }
3532 
3533 /*
3534  * ill_dls_info
3535  * creates datalink socket info from the device.
3536  */
3537 int
3538 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3539 {
3540 	size_t	len;
3541 
3542 	sdl->sdl_family = AF_LINK;
3543 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3544 	sdl->sdl_type = ill->ill_type;
3545 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3546 	len = strlen(sdl->sdl_data);
3547 	ASSERT(len < 256);
3548 	sdl->sdl_nlen = (uchar_t)len;
3549 	sdl->sdl_alen = ill->ill_phys_addr_length;
3550 	sdl->sdl_slen = 0;
3551 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3552 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3553 
3554 	return (sizeof (struct sockaddr_dl));
3555 }
3556 
3557 /*
3558  * ill_xarp_info
3559  * creates xarp info from the device.
3560  */
3561 static int
3562 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3563 {
3564 	sdl->sdl_family = AF_LINK;
3565 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3566 	sdl->sdl_type = ill->ill_type;
3567 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3568 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3569 	sdl->sdl_alen = ill->ill_phys_addr_length;
3570 	sdl->sdl_slen = 0;
3571 	return (sdl->sdl_nlen);
3572 }
3573 
3574 static int
3575 loopback_kstat_update(kstat_t *ksp, int rw)
3576 {
3577 	kstat_named_t *kn;
3578 	netstackid_t	stackid;
3579 	netstack_t	*ns;
3580 	ip_stack_t	*ipst;
3581 
3582 	if (ksp == NULL || ksp->ks_data == NULL)
3583 		return (EIO);
3584 
3585 	if (rw == KSTAT_WRITE)
3586 		return (EACCES);
3587 
3588 	kn = KSTAT_NAMED_PTR(ksp);
3589 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3590 
3591 	ns = netstack_find_by_stackid(stackid);
3592 	if (ns == NULL)
3593 		return (-1);
3594 
3595 	ipst = ns->netstack_ip;
3596 	if (ipst == NULL) {
3597 		netstack_rele(ns);
3598 		return (-1);
3599 	}
3600 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3601 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3602 	netstack_rele(ns);
3603 	return (0);
3604 }
3605 
3606 /*
3607  * Has ifindex been plumbed already?
3608  */
3609 static boolean_t
3610 phyint_exists(uint_t index, ip_stack_t *ipst)
3611 {
3612 	ASSERT(index != 0);
3613 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3614 
3615 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3616 	    &index, NULL) != NULL);
3617 }
3618 
3619 /* Pick a unique ifindex */
3620 boolean_t
3621 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3622 {
3623 	uint_t starting_index;
3624 
3625 	if (!ipst->ips_ill_index_wrap) {
3626 		*indexp = ipst->ips_ill_index++;
3627 		if (ipst->ips_ill_index == 0) {
3628 			/* Reached the uint_t limit Next time wrap  */
3629 			ipst->ips_ill_index_wrap = B_TRUE;
3630 		}
3631 		return (B_TRUE);
3632 	}
3633 
3634 	/*
3635 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3636 	 * at this point and don't want to call any function that attempts
3637 	 * to get the lock again.
3638 	 */
3639 	starting_index = ipst->ips_ill_index++;
3640 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3641 		if (ipst->ips_ill_index != 0 &&
3642 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3643 			/* found unused index - use it */
3644 			*indexp = ipst->ips_ill_index;
3645 			return (B_TRUE);
3646 		}
3647 	}
3648 
3649 	/*
3650 	 * all interface indicies are inuse.
3651 	 */
3652 	return (B_FALSE);
3653 }
3654 
3655 /*
3656  * Assign a unique interface index for the phyint.
3657  */
3658 static boolean_t
3659 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3660 {
3661 	ASSERT(phyi->phyint_ifindex == 0);
3662 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3663 }
3664 
3665 /*
3666  * Initialize the flags on `phyi' as per the provided mactype.
3667  */
3668 static void
3669 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3670 {
3671 	uint64_t flags = 0;
3672 
3673 	/*
3674 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3675 	 * we always presume the underlying hardware is working and set
3676 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3677 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3678 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3679 	 */
3680 	if (mactype == SUNW_DL_IPMP)
3681 		flags |= PHYI_FAILED;
3682 	else
3683 		flags |= PHYI_RUNNING;
3684 
3685 	switch (mactype) {
3686 	case SUNW_DL_VNI:
3687 		flags |= PHYI_VIRTUAL;
3688 		break;
3689 	case SUNW_DL_IPMP:
3690 		flags |= PHYI_IPMP;
3691 		break;
3692 	case DL_LOOP:
3693 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3694 		break;
3695 	}
3696 
3697 	mutex_enter(&phyi->phyint_lock);
3698 	phyi->phyint_flags |= flags;
3699 	mutex_exit(&phyi->phyint_lock);
3700 }
3701 
3702 /*
3703  * Return a pointer to the ill which matches the supplied name.  Note that
3704  * the ill name length includes the null termination character.  (May be
3705  * called as writer.)
3706  * If do_alloc and the interface is "lo0" it will be automatically created.
3707  * Cannot bump up reference on condemned ills. So dup detect can't be done
3708  * using this func.
3709  */
3710 ill_t *
3711 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3712     boolean_t *did_alloc, ip_stack_t *ipst)
3713 {
3714 	ill_t	*ill;
3715 	ipif_t	*ipif;
3716 	ipsq_t	*ipsq;
3717 	kstat_named_t	*kn;
3718 	boolean_t isloopback;
3719 	in6_addr_t ov6addr;
3720 
3721 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3722 
3723 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3724 	ill = ill_find_by_name(name, isv6, ipst);
3725 	rw_exit(&ipst->ips_ill_g_lock);
3726 	if (ill != NULL)
3727 		return (ill);
3728 
3729 	/*
3730 	 * Couldn't find it.  Does this happen to be a lookup for the
3731 	 * loopback device and are we allowed to allocate it?
3732 	 */
3733 	if (!isloopback || !do_alloc)
3734 		return (NULL);
3735 
3736 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3737 	ill = ill_find_by_name(name, isv6, ipst);
3738 	if (ill != NULL) {
3739 		rw_exit(&ipst->ips_ill_g_lock);
3740 		return (ill);
3741 	}
3742 
3743 	/* Create the loopback device on demand */
3744 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3745 	    sizeof (ipif_loopback_name), BPRI_MED));
3746 	if (ill == NULL)
3747 		goto done;
3748 
3749 	*ill = ill_null;
3750 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3751 	ill->ill_ipst = ipst;
3752 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3753 	netstack_hold(ipst->ips_netstack);
3754 	/*
3755 	 * For exclusive stacks we set the zoneid to zero
3756 	 * to make IP operate as if in the global zone.
3757 	 */
3758 	ill->ill_zoneid = GLOBAL_ZONEID;
3759 
3760 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3761 	if (ill->ill_phyint == NULL)
3762 		goto done;
3763 
3764 	if (isv6)
3765 		ill->ill_phyint->phyint_illv6 = ill;
3766 	else
3767 		ill->ill_phyint->phyint_illv4 = ill;
3768 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3769 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3770 
3771 	if (isv6) {
3772 		ill->ill_isv6 = B_TRUE;
3773 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3774 	} else {
3775 		ill->ill_max_frag = ip_loopback_mtuplus;
3776 	}
3777 	if (!ill_allocate_mibs(ill))
3778 		goto done;
3779 	ill->ill_current_frag = ill->ill_max_frag;
3780 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3781 	/*
3782 	 * ipif_loopback_name can't be pointed at directly because its used
3783 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3784 	 * from the glist, ill_glist_delete() sets the first character of
3785 	 * ill_name to '\0'.
3786 	 */
3787 	ill->ill_name = (char *)ill + sizeof (*ill);
3788 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3789 	ill->ill_name_length = sizeof (ipif_loopback_name);
3790 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3791 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3792 
3793 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3794 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3795 	ill->ill_global_timer = INFINITY;
3796 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3797 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3798 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3799 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3800 
3801 	/* No resolver here. */
3802 	ill->ill_net_type = IRE_LOOPBACK;
3803 
3804 	/* Initialize the ipsq */
3805 	if (!ipsq_init(ill, B_FALSE))
3806 		goto done;
3807 
3808 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3809 	if (ipif == NULL)
3810 		goto done;
3811 
3812 	ill->ill_flags = ILLF_MULTICAST;
3813 
3814 	ov6addr = ipif->ipif_v6lcl_addr;
3815 	/* Set up default loopback address and mask. */
3816 	if (!isv6) {
3817 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3818 
3819 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3820 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3821 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3822 		    ipif->ipif_v6subnet);
3823 		ill->ill_flags |= ILLF_IPV4;
3824 	} else {
3825 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3826 		ipif->ipif_v6net_mask = ipv6_all_ones;
3827 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3828 		    ipif->ipif_v6subnet);
3829 		ill->ill_flags |= ILLF_IPV6;
3830 	}
3831 
3832 	/*
3833 	 * Chain us in at the end of the ill list. hold the ill
3834 	 * before we make it globally visible. 1 for the lookup.
3835 	 */
3836 	ill->ill_refcnt = 0;
3837 	ill_refhold(ill);
3838 
3839 	ill->ill_frag_count = 0;
3840 	ill->ill_frag_free_num_pkts = 0;
3841 	ill->ill_last_frag_clean_time = 0;
3842 
3843 	ipsq = ill->ill_phyint->phyint_ipsq;
3844 
3845 	ill_set_inputfn(ill);
3846 
3847 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3848 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3849 
3850 	/* Let SCTP know so that it can add this to its list */
3851 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3852 
3853 	/*
3854 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3855 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3856 	 * requires to be after ill_glist_insert() since we need the
3857 	 * ill_index set. Pass on ipv6_loopback as the old address.
3858 	 */
3859 	sctp_update_ipif_addr(ipif, ov6addr);
3860 
3861 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3862 
3863 	/*
3864 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3865 	 * If so, free our original one.
3866 	 */
3867 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3868 		ipsq_delete(ipsq);
3869 
3870 	if (ipst->ips_loopback_ksp == NULL) {
3871 		/* Export loopback interface statistics */
3872 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3873 		    ipif_loopback_name, "net",
3874 		    KSTAT_TYPE_NAMED, 2, 0,
3875 		    ipst->ips_netstack->netstack_stackid);
3876 		if (ipst->ips_loopback_ksp != NULL) {
3877 			ipst->ips_loopback_ksp->ks_update =
3878 			    loopback_kstat_update;
3879 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3880 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3881 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3882 			ipst->ips_loopback_ksp->ks_private =
3883 			    (void *)(uintptr_t)ipst->ips_netstack->
3884 			    netstack_stackid;
3885 			kstat_install(ipst->ips_loopback_ksp);
3886 		}
3887 	}
3888 
3889 	*did_alloc = B_TRUE;
3890 	rw_exit(&ipst->ips_ill_g_lock);
3891 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3892 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3893 	return (ill);
3894 done:
3895 	if (ill != NULL) {
3896 		if (ill->ill_phyint != NULL) {
3897 			ipsq = ill->ill_phyint->phyint_ipsq;
3898 			if (ipsq != NULL) {
3899 				ipsq->ipsq_phyint = NULL;
3900 				ipsq_delete(ipsq);
3901 			}
3902 			mi_free(ill->ill_phyint);
3903 		}
3904 		ill_free_mib(ill);
3905 		if (ill->ill_ipst != NULL)
3906 			netstack_rele(ill->ill_ipst->ips_netstack);
3907 		mi_free(ill);
3908 	}
3909 	rw_exit(&ipst->ips_ill_g_lock);
3910 	return (NULL);
3911 }
3912 
3913 /*
3914  * For IPP calls - use the ip_stack_t for global stack.
3915  */
3916 ill_t *
3917 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3918 {
3919 	ip_stack_t	*ipst;
3920 	ill_t		*ill;
3921 
3922 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3923 	if (ipst == NULL) {
3924 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3925 		return (NULL);
3926 	}
3927 
3928 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3929 	netstack_rele(ipst->ips_netstack);
3930 	return (ill);
3931 }
3932 
3933 /*
3934  * Return a pointer to the ill which matches the index and IP version type.
3935  */
3936 ill_t *
3937 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3938 {
3939 	ill_t	*ill;
3940 	phyint_t *phyi;
3941 
3942 	/*
3943 	 * Indexes are stored in the phyint - a common structure
3944 	 * to both IPv4 and IPv6.
3945 	 */
3946 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3947 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3948 	    (void *) &index, NULL);
3949 	if (phyi != NULL) {
3950 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3951 		if (ill != NULL) {
3952 			mutex_enter(&ill->ill_lock);
3953 			if (!ILL_IS_CONDEMNED(ill)) {
3954 				ill_refhold_locked(ill);
3955 				mutex_exit(&ill->ill_lock);
3956 				rw_exit(&ipst->ips_ill_g_lock);
3957 				return (ill);
3958 			}
3959 			mutex_exit(&ill->ill_lock);
3960 		}
3961 	}
3962 	rw_exit(&ipst->ips_ill_g_lock);
3963 	return (NULL);
3964 }
3965 
3966 /*
3967  * Verify whether or not an interface index is valid for the specified zoneid
3968  * to transmit packets.
3969  * It can be zero (meaning "reset") or an interface index assigned
3970  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3971  */
3972 boolean_t
3973 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3974     ip_stack_t *ipst)
3975 {
3976 	ill_t		*ill;
3977 
3978 	if (ifindex == 0)
3979 		return (B_TRUE);
3980 
3981 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3982 	if (ill == NULL)
3983 		return (B_FALSE);
3984 	if (IS_VNI(ill)) {
3985 		ill_refrele(ill);
3986 		return (B_FALSE);
3987 	}
3988 	ill_refrele(ill);
3989 	return (B_TRUE);
3990 }
3991 
3992 /*
3993  * Return the ifindex next in sequence after the passed in ifindex.
3994  * If there is no next ifindex for the given protocol, return 0.
3995  */
3996 uint_t
3997 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3998 {
3999 	phyint_t *phyi;
4000 	phyint_t *phyi_initial;
4001 	uint_t   ifindex;
4002 
4003 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4004 
4005 	if (index == 0) {
4006 		phyi = avl_first(
4007 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4008 	} else {
4009 		phyi = phyi_initial = avl_find(
4010 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4011 		    (void *) &index, NULL);
4012 	}
4013 
4014 	for (; phyi != NULL;
4015 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4016 	    phyi, AVL_AFTER)) {
4017 		/*
4018 		 * If we're not returning the first interface in the tree
4019 		 * and we still haven't moved past the phyint_t that
4020 		 * corresponds to index, avl_walk needs to be called again
4021 		 */
4022 		if (!((index != 0) && (phyi == phyi_initial))) {
4023 			if (isv6) {
4024 				if ((phyi->phyint_illv6) &&
4025 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
4026 				    (phyi->phyint_illv6->ill_isv6 == 1))
4027 					break;
4028 			} else {
4029 				if ((phyi->phyint_illv4) &&
4030 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
4031 				    (phyi->phyint_illv4->ill_isv6 == 0))
4032 					break;
4033 			}
4034 		}
4035 	}
4036 
4037 	rw_exit(&ipst->ips_ill_g_lock);
4038 
4039 	if (phyi != NULL)
4040 		ifindex = phyi->phyint_ifindex;
4041 	else
4042 		ifindex = 0;
4043 
4044 	return (ifindex);
4045 }
4046 
4047 /*
4048  * Return the ifindex for the named interface.
4049  * If there is no next ifindex for the interface, return 0.
4050  */
4051 uint_t
4052 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4053 {
4054 	phyint_t	*phyi;
4055 	avl_index_t	where = 0;
4056 	uint_t		ifindex;
4057 
4058 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4059 
4060 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4061 	    name, &where)) == NULL) {
4062 		rw_exit(&ipst->ips_ill_g_lock);
4063 		return (0);
4064 	}
4065 
4066 	ifindex = phyi->phyint_ifindex;
4067 
4068 	rw_exit(&ipst->ips_ill_g_lock);
4069 
4070 	return (ifindex);
4071 }
4072 
4073 /*
4074  * Return the ifindex to be used by upper layer protocols for instance
4075  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4076  */
4077 uint_t
4078 ill_get_upper_ifindex(const ill_t *ill)
4079 {
4080 	if (IS_UNDER_IPMP(ill))
4081 		return (ipmp_ill_get_ipmp_ifindex(ill));
4082 	else
4083 		return (ill->ill_phyint->phyint_ifindex);
4084 }
4085 
4086 
4087 /*
4088  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4089  * that gives a running thread a reference to the ill. This reference must be
4090  * released by the thread when it is done accessing the ill and related
4091  * objects. ill_refcnt can not be used to account for static references
4092  * such as other structures pointing to an ill. Callers must generally
4093  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4094  * or be sure that the ill is not being deleted or changing state before
4095  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4096  * ill won't change any of its critical state such as address, netmask etc.
4097  */
4098 void
4099 ill_refhold(ill_t *ill)
4100 {
4101 	mutex_enter(&ill->ill_lock);
4102 	ill->ill_refcnt++;
4103 	ILL_TRACE_REF(ill);
4104 	mutex_exit(&ill->ill_lock);
4105 }
4106 
4107 void
4108 ill_refhold_locked(ill_t *ill)
4109 {
4110 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4111 	ill->ill_refcnt++;
4112 	ILL_TRACE_REF(ill);
4113 }
4114 
4115 /* Returns true if we managed to get a refhold */
4116 boolean_t
4117 ill_check_and_refhold(ill_t *ill)
4118 {
4119 	mutex_enter(&ill->ill_lock);
4120 	if (!ILL_IS_CONDEMNED(ill)) {
4121 		ill_refhold_locked(ill);
4122 		mutex_exit(&ill->ill_lock);
4123 		return (B_TRUE);
4124 	}
4125 	mutex_exit(&ill->ill_lock);
4126 	return (B_FALSE);
4127 }
4128 
4129 /*
4130  * Must not be called while holding any locks. Otherwise if this is
4131  * the last reference to be released, there is a chance of recursive mutex
4132  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4133  * to restart an ioctl.
4134  */
4135 void
4136 ill_refrele(ill_t *ill)
4137 {
4138 	mutex_enter(&ill->ill_lock);
4139 	ASSERT(ill->ill_refcnt != 0);
4140 	ill->ill_refcnt--;
4141 	ILL_UNTRACE_REF(ill);
4142 	if (ill->ill_refcnt != 0) {
4143 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4144 		mutex_exit(&ill->ill_lock);
4145 		return;
4146 	}
4147 
4148 	/* Drops the ill_lock */
4149 	ipif_ill_refrele_tail(ill);
4150 }
4151 
4152 /*
4153  * Obtain a weak reference count on the ill. This reference ensures the
4154  * ill won't be freed, but the ill may change any of its critical state
4155  * such as netmask, address etc. Returns an error if the ill has started
4156  * closing.
4157  */
4158 boolean_t
4159 ill_waiter_inc(ill_t *ill)
4160 {
4161 	mutex_enter(&ill->ill_lock);
4162 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4163 		mutex_exit(&ill->ill_lock);
4164 		return (B_FALSE);
4165 	}
4166 	ill->ill_waiters++;
4167 	mutex_exit(&ill->ill_lock);
4168 	return (B_TRUE);
4169 }
4170 
4171 void
4172 ill_waiter_dcr(ill_t *ill)
4173 {
4174 	mutex_enter(&ill->ill_lock);
4175 	ill->ill_waiters--;
4176 	if (ill->ill_waiters == 0)
4177 		cv_broadcast(&ill->ill_cv);
4178 	mutex_exit(&ill->ill_lock);
4179 }
4180 
4181 /*
4182  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4183  * driver.  We construct best guess defaults for lower level information that
4184  * we need.  If an interface is brought up without injection of any overriding
4185  * information from outside, we have to be ready to go with these defaults.
4186  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4187  * we primarely want the dl_provider_style.
4188  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4189  * at which point we assume the other part of the information is valid.
4190  */
4191 void
4192 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4193 {
4194 	uchar_t		*brdcst_addr;
4195 	uint_t		brdcst_addr_length, phys_addr_length;
4196 	t_scalar_t	sap_length;
4197 	dl_info_ack_t	*dlia;
4198 	ip_m_t		*ipm;
4199 	dl_qos_cl_sel1_t *sel1;
4200 	int		min_mtu;
4201 
4202 	ASSERT(IAM_WRITER_ILL(ill));
4203 
4204 	/*
4205 	 * Till the ill is fully up  the ill is not globally visible.
4206 	 * So no need for a lock.
4207 	 */
4208 	dlia = (dl_info_ack_t *)mp->b_rptr;
4209 	ill->ill_mactype = dlia->dl_mac_type;
4210 
4211 	ipm = ip_m_lookup(dlia->dl_mac_type);
4212 	if (ipm == NULL) {
4213 		ipm = ip_m_lookup(DL_OTHER);
4214 		ASSERT(ipm != NULL);
4215 	}
4216 	ill->ill_media = ipm;
4217 
4218 	/*
4219 	 * When the new DLPI stuff is ready we'll pull lengths
4220 	 * from dlia.
4221 	 */
4222 	if (dlia->dl_version == DL_VERSION_2) {
4223 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4224 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4225 		    brdcst_addr_length);
4226 		if (brdcst_addr == NULL) {
4227 			brdcst_addr_length = 0;
4228 		}
4229 		sap_length = dlia->dl_sap_length;
4230 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4231 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4232 		    brdcst_addr_length, sap_length, phys_addr_length));
4233 	} else {
4234 		brdcst_addr_length = 6;
4235 		brdcst_addr = ip_six_byte_all_ones;
4236 		sap_length = -2;
4237 		phys_addr_length = brdcst_addr_length;
4238 	}
4239 
4240 	ill->ill_bcast_addr_length = brdcst_addr_length;
4241 	ill->ill_phys_addr_length = phys_addr_length;
4242 	ill->ill_sap_length = sap_length;
4243 
4244 	/*
4245 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4246 	 * but we must ensure a minimum IP MTU is used since other bits of
4247 	 * IP will fly apart otherwise.
4248 	 */
4249 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4250 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4251 	ill->ill_current_frag = ill->ill_max_frag;
4252 	ill->ill_mtu = ill->ill_max_frag;
4253 
4254 	ill->ill_type = ipm->ip_m_type;
4255 
4256 	if (!ill->ill_dlpi_style_set) {
4257 		if (dlia->dl_provider_style == DL_STYLE2)
4258 			ill->ill_needs_attach = 1;
4259 
4260 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4261 
4262 		/*
4263 		 * Allocate the first ipif on this ill.  We don't delay it
4264 		 * further as ioctl handling assumes at least one ipif exists.
4265 		 *
4266 		 * At this point we don't know whether the ill is v4 or v6.
4267 		 * We will know this whan the SIOCSLIFNAME happens and
4268 		 * the correct value for ill_isv6 will be assigned in
4269 		 * ipif_set_values(). We need to hold the ill lock and
4270 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4271 		 * the wakeup.
4272 		 */
4273 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4274 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4275 		mutex_enter(&ill->ill_lock);
4276 		ASSERT(ill->ill_dlpi_style_set == 0);
4277 		ill->ill_dlpi_style_set = 1;
4278 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4279 		cv_broadcast(&ill->ill_cv);
4280 		mutex_exit(&ill->ill_lock);
4281 		freemsg(mp);
4282 		return;
4283 	}
4284 	ASSERT(ill->ill_ipif != NULL);
4285 	/*
4286 	 * We know whether it is IPv4 or IPv6 now, as this is the
4287 	 * second DL_INFO_ACK we are recieving in response to the
4288 	 * DL_INFO_REQ sent in ipif_set_values.
4289 	 */
4290 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4291 	/*
4292 	 * Clear all the flags that were set based on ill_bcast_addr_length
4293 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4294 	 * changed now and we need to re-evaluate.
4295 	 */
4296 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4297 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4298 
4299 	/*
4300 	 * Free ill_bcast_mp as things could have changed now.
4301 	 *
4302 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4303 	 * with no underlying interfaces (and thus an unknown broadcast
4304 	 * address length), but we enforce that an interface is broadcast-
4305 	 * capable as part of allowing it to join a group.
4306 	 */
4307 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4308 		if (ill->ill_bcast_mp != NULL)
4309 			freemsg(ill->ill_bcast_mp);
4310 		ill->ill_net_type = IRE_IF_NORESOLVER;
4311 
4312 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4313 		    ill->ill_phys_addr_length,
4314 		    ill->ill_sap,
4315 		    ill->ill_sap_length);
4316 
4317 		if (ill->ill_isv6)
4318 			/*
4319 			 * Note: xresolv interfaces will eventually need NOARP
4320 			 * set here as well, but that will require those
4321 			 * external resolvers to have some knowledge of
4322 			 * that flag and act appropriately. Not to be changed
4323 			 * at present.
4324 			 */
4325 			ill->ill_flags |= ILLF_NONUD;
4326 		else
4327 			ill->ill_flags |= ILLF_NOARP;
4328 
4329 		if (ill->ill_mactype == SUNW_DL_VNI) {
4330 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4331 		} else if (ill->ill_phys_addr_length == 0 ||
4332 		    ill->ill_mactype == DL_IPV4 ||
4333 		    ill->ill_mactype == DL_IPV6) {
4334 			/*
4335 			 * The underying link is point-to-point, so mark the
4336 			 * interface as such.  We can do IP multicast over
4337 			 * such a link since it transmits all network-layer
4338 			 * packets to the remote side the same way.
4339 			 */
4340 			ill->ill_flags |= ILLF_MULTICAST;
4341 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4342 		}
4343 	} else {
4344 		ill->ill_net_type = IRE_IF_RESOLVER;
4345 		if (ill->ill_bcast_mp != NULL)
4346 			freemsg(ill->ill_bcast_mp);
4347 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4348 		    ill->ill_bcast_addr_length, ill->ill_sap,
4349 		    ill->ill_sap_length);
4350 		/*
4351 		 * Later detect lack of DLPI driver multicast
4352 		 * capability by catching DL_ENABMULTI errors in
4353 		 * ip_rput_dlpi.
4354 		 */
4355 		ill->ill_flags |= ILLF_MULTICAST;
4356 		if (!ill->ill_isv6)
4357 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4358 	}
4359 
4360 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4361 	if (ill->ill_mactype == SUNW_DL_IPMP)
4362 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4363 
4364 	/* By default an interface does not support any CoS marking */
4365 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4366 
4367 	/*
4368 	 * If we get QoS information in DL_INFO_ACK, the device supports
4369 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4370 	 */
4371 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4372 	    dlia->dl_qos_length);
4373 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4374 		ill->ill_flags |= ILLF_COS_ENABLED;
4375 	}
4376 
4377 	/* Clear any previous error indication. */
4378 	ill->ill_error = 0;
4379 	freemsg(mp);
4380 }
4381 
4382 /*
4383  * Perform various checks to verify that an address would make sense as a
4384  * local, remote, or subnet interface address.
4385  */
4386 static boolean_t
4387 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4388 {
4389 	ipaddr_t	net_mask;
4390 
4391 	/*
4392 	 * Don't allow all zeroes, or all ones, but allow
4393 	 * all ones netmask.
4394 	 */
4395 	if ((net_mask = ip_net_mask(addr)) == 0)
4396 		return (B_FALSE);
4397 	/* A given netmask overrides the "guess" netmask */
4398 	if (subnet_mask != 0)
4399 		net_mask = subnet_mask;
4400 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4401 	    (addr == (addr | ~net_mask)))) {
4402 		return (B_FALSE);
4403 	}
4404 
4405 	/*
4406 	 * Even if the netmask is all ones, we do not allow address to be
4407 	 * 255.255.255.255
4408 	 */
4409 	if (addr == INADDR_BROADCAST)
4410 		return (B_FALSE);
4411 
4412 	if (CLASSD(addr))
4413 		return (B_FALSE);
4414 
4415 	return (B_TRUE);
4416 }
4417 
4418 #define	V6_IPIF_LINKLOCAL(p)	\
4419 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4420 
4421 /*
4422  * Compare two given ipifs and check if the second one is better than
4423  * the first one using the order of preference (not taking deprecated
4424  * into acount) specified in ipif_lookup_multicast().
4425  */
4426 static boolean_t
4427 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4428 {
4429 	/* Check the least preferred first. */
4430 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4431 		/* If both ipifs are the same, use the first one. */
4432 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4433 			return (B_FALSE);
4434 		else
4435 			return (B_TRUE);
4436 	}
4437 
4438 	/* For IPv6, check for link local address. */
4439 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4440 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4441 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4442 			/* The second one is equal or less preferred. */
4443 			return (B_FALSE);
4444 		} else {
4445 			return (B_TRUE);
4446 		}
4447 	}
4448 
4449 	/* Then check for point to point interface. */
4450 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4451 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4452 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4453 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4454 			return (B_FALSE);
4455 		} else {
4456 			return (B_TRUE);
4457 		}
4458 	}
4459 
4460 	/* old_ipif is a normal interface, so no need to use the new one. */
4461 	return (B_FALSE);
4462 }
4463 
4464 /*
4465  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4466  * The ipif must be up, and its ill must multicast-capable, not
4467  * condemned, not an underlying interface in an IPMP group, and
4468  * not a VNI interface.  Order of preference:
4469  *
4470  * 	1a. normal
4471  * 	1b. normal, but deprecated
4472  * 	2a. point to point
4473  * 	2b. point to point, but deprecated
4474  * 	3a. link local
4475  * 	3b. link local, but deprecated
4476  * 	4. loopback.
4477  */
4478 static ipif_t *
4479 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4480 {
4481 	ill_t			*ill;
4482 	ill_walk_context_t	ctx;
4483 	ipif_t			*ipif;
4484 	ipif_t			*saved_ipif = NULL;
4485 	ipif_t			*dep_ipif = NULL;
4486 
4487 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4488 	if (isv6)
4489 		ill = ILL_START_WALK_V6(&ctx, ipst);
4490 	else
4491 		ill = ILL_START_WALK_V4(&ctx, ipst);
4492 
4493 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4494 		mutex_enter(&ill->ill_lock);
4495 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4496 		    ILL_IS_CONDEMNED(ill) ||
4497 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4498 			mutex_exit(&ill->ill_lock);
4499 			continue;
4500 		}
4501 		for (ipif = ill->ill_ipif; ipif != NULL;
4502 		    ipif = ipif->ipif_next) {
4503 			if (zoneid != ipif->ipif_zoneid &&
4504 			    zoneid != ALL_ZONES &&
4505 			    ipif->ipif_zoneid != ALL_ZONES) {
4506 				continue;
4507 			}
4508 			if (!(ipif->ipif_flags & IPIF_UP) ||
4509 			    IPIF_IS_CONDEMNED(ipif)) {
4510 				continue;
4511 			}
4512 
4513 			/*
4514 			 * Found one candidate.  If it is deprecated,
4515 			 * remember it in dep_ipif.  If it is not deprecated,
4516 			 * remember it in saved_ipif.
4517 			 */
4518 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4519 				if (dep_ipif == NULL) {
4520 					dep_ipif = ipif;
4521 				} else if (ipif_comp_multi(dep_ipif, ipif,
4522 				    isv6)) {
4523 					/*
4524 					 * If the previous dep_ipif does not
4525 					 * belong to the same ill, we've done
4526 					 * a ipif_refhold() on it.  So we need
4527 					 * to release it.
4528 					 */
4529 					if (dep_ipif->ipif_ill != ill)
4530 						ipif_refrele(dep_ipif);
4531 					dep_ipif = ipif;
4532 				}
4533 				continue;
4534 			}
4535 			if (saved_ipif == NULL) {
4536 				saved_ipif = ipif;
4537 			} else {
4538 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4539 					if (saved_ipif->ipif_ill != ill)
4540 						ipif_refrele(saved_ipif);
4541 					saved_ipif = ipif;
4542 				}
4543 			}
4544 		}
4545 		/*
4546 		 * Before going to the next ill, do a ipif_refhold() on the
4547 		 * saved ones.
4548 		 */
4549 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4550 			ipif_refhold_locked(saved_ipif);
4551 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4552 			ipif_refhold_locked(dep_ipif);
4553 		mutex_exit(&ill->ill_lock);
4554 	}
4555 	rw_exit(&ipst->ips_ill_g_lock);
4556 
4557 	/*
4558 	 * If we have only the saved_ipif, return it.  But if we have both
4559 	 * saved_ipif and dep_ipif, check to see which one is better.
4560 	 */
4561 	if (saved_ipif != NULL) {
4562 		if (dep_ipif != NULL) {
4563 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4564 				ipif_refrele(saved_ipif);
4565 				return (dep_ipif);
4566 			} else {
4567 				ipif_refrele(dep_ipif);
4568 				return (saved_ipif);
4569 			}
4570 		}
4571 		return (saved_ipif);
4572 	} else {
4573 		return (dep_ipif);
4574 	}
4575 }
4576 
4577 ill_t *
4578 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4579 {
4580 	ipif_t *ipif;
4581 	ill_t *ill;
4582 
4583 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4584 	if (ipif == NULL)
4585 		return (NULL);
4586 
4587 	ill = ipif->ipif_ill;
4588 	ill_refhold(ill);
4589 	ipif_refrele(ipif);
4590 	return (ill);
4591 }
4592 
4593 /*
4594  * This function is called when an application does not specify an interface
4595  * to be used for multicast traffic (joining a group/sending data).  It
4596  * calls ire_lookup_multi() to look for an interface route for the
4597  * specified multicast group.  Doing this allows the administrator to add
4598  * prefix routes for multicast to indicate which interface to be used for
4599  * multicast traffic in the above scenario.  The route could be for all
4600  * multicast (224.0/4), for a single multicast group (a /32 route) or
4601  * anything in between.  If there is no such multicast route, we just find
4602  * any multicast capable interface and return it.  The returned ipif
4603  * is refhold'ed.
4604  *
4605  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4606  * unicast table. This is used by CGTP.
4607  */
4608 ill_t *
4609 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4610     boolean_t *multirtp, ipaddr_t *setsrcp)
4611 {
4612 	ill_t			*ill;
4613 
4614 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4615 	if (ill != NULL)
4616 		return (ill);
4617 
4618 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4619 }
4620 
4621 /*
4622  * Look for an ipif with the specified interface address and destination.
4623  * The destination address is used only for matching point-to-point interfaces.
4624  */
4625 ipif_t *
4626 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4627 {
4628 	ipif_t	*ipif;
4629 	ill_t	*ill;
4630 	ill_walk_context_t ctx;
4631 
4632 	/*
4633 	 * First match all the point-to-point interfaces
4634 	 * before looking at non-point-to-point interfaces.
4635 	 * This is done to avoid returning non-point-to-point
4636 	 * ipif instead of unnumbered point-to-point ipif.
4637 	 */
4638 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4639 	ill = ILL_START_WALK_V4(&ctx, ipst);
4640 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4641 		mutex_enter(&ill->ill_lock);
4642 		for (ipif = ill->ill_ipif; ipif != NULL;
4643 		    ipif = ipif->ipif_next) {
4644 			/* Allow the ipif to be down */
4645 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4646 			    (ipif->ipif_lcl_addr == if_addr) &&
4647 			    (ipif->ipif_pp_dst_addr == dst)) {
4648 				if (!IPIF_IS_CONDEMNED(ipif)) {
4649 					ipif_refhold_locked(ipif);
4650 					mutex_exit(&ill->ill_lock);
4651 					rw_exit(&ipst->ips_ill_g_lock);
4652 					return (ipif);
4653 				}
4654 			}
4655 		}
4656 		mutex_exit(&ill->ill_lock);
4657 	}
4658 	rw_exit(&ipst->ips_ill_g_lock);
4659 
4660 	/* lookup the ipif based on interface address */
4661 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4662 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4663 	return (ipif);
4664 }
4665 
4666 /*
4667  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4668  */
4669 static ipif_t *
4670 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4671     zoneid_t zoneid, ip_stack_t *ipst)
4672 {
4673 	ipif_t  *ipif;
4674 	ill_t   *ill;
4675 	boolean_t ptp = B_FALSE;
4676 	ill_walk_context_t	ctx;
4677 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4678 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4679 
4680 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4681 	/*
4682 	 * Repeat twice, first based on local addresses and
4683 	 * next time for pointopoint.
4684 	 */
4685 repeat:
4686 	ill = ILL_START_WALK_V4(&ctx, ipst);
4687 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4688 		if (match_ill != NULL && ill != match_ill &&
4689 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4690 			continue;
4691 		}
4692 		mutex_enter(&ill->ill_lock);
4693 		for (ipif = ill->ill_ipif; ipif != NULL;
4694 		    ipif = ipif->ipif_next) {
4695 			if (zoneid != ALL_ZONES &&
4696 			    zoneid != ipif->ipif_zoneid &&
4697 			    ipif->ipif_zoneid != ALL_ZONES)
4698 				continue;
4699 
4700 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4701 				continue;
4702 
4703 			/* Allow the ipif to be down */
4704 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4705 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4706 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4707 			    (ipif->ipif_pp_dst_addr == addr))) {
4708 				if (!IPIF_IS_CONDEMNED(ipif)) {
4709 					ipif_refhold_locked(ipif);
4710 					mutex_exit(&ill->ill_lock);
4711 					rw_exit(&ipst->ips_ill_g_lock);
4712 					return (ipif);
4713 				}
4714 			}
4715 		}
4716 		mutex_exit(&ill->ill_lock);
4717 	}
4718 
4719 	/* If we already did the ptp case, then we are done */
4720 	if (ptp) {
4721 		rw_exit(&ipst->ips_ill_g_lock);
4722 		return (NULL);
4723 	}
4724 	ptp = B_TRUE;
4725 	goto repeat;
4726 }
4727 
4728 /*
4729  * Lookup an ipif with the specified address.  For point-to-point links we
4730  * look for matches on either the destination address or the local address,
4731  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4732  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4733  * (or illgrp if `match_ill' is in an IPMP group).
4734  */
4735 ipif_t *
4736 ipif_lookup_addr(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, IPIF_MATCH_ILLGRP,
4740 	    zoneid, ipst));
4741 }
4742 
4743 /*
4744  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4745  * except that we will only return an address if it is not marked as
4746  * IPIF_DUPLICATE
4747  */
4748 ipif_t *
4749 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4750     ip_stack_t *ipst)
4751 {
4752 	return (ipif_lookup_addr_common(addr, match_ill,
4753 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4754 	    zoneid, ipst));
4755 }
4756 
4757 /*
4758  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4759  * `match_ill' across the IPMP group.  This function is only needed in some
4760  * corner-cases; almost everything should use ipif_lookup_addr().
4761  */
4762 ipif_t *
4763 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4764 {
4765 	ASSERT(match_ill != NULL);
4766 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4767 	    ipst));
4768 }
4769 
4770 /*
4771  * Look for an ipif with the specified address. For point-point links
4772  * we look for matches on either the destination address and the local
4773  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4774  * is set.
4775  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4776  * ill (or illgrp if `match_ill' is in an IPMP group).
4777  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4778  */
4779 zoneid_t
4780 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4781 {
4782 	zoneid_t zoneid;
4783 	ipif_t  *ipif;
4784 	ill_t   *ill;
4785 	boolean_t ptp = B_FALSE;
4786 	ill_walk_context_t	ctx;
4787 
4788 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4789 	/*
4790 	 * Repeat twice, first based on local addresses and
4791 	 * next time for pointopoint.
4792 	 */
4793 repeat:
4794 	ill = ILL_START_WALK_V4(&ctx, ipst);
4795 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4796 		if (match_ill != NULL && ill != match_ill &&
4797 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4798 			continue;
4799 		}
4800 		mutex_enter(&ill->ill_lock);
4801 		for (ipif = ill->ill_ipif; ipif != NULL;
4802 		    ipif = ipif->ipif_next) {
4803 			/* Allow the ipif to be down */
4804 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4805 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4806 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4807 			    (ipif->ipif_pp_dst_addr == addr)) &&
4808 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4809 				zoneid = ipif->ipif_zoneid;
4810 				mutex_exit(&ill->ill_lock);
4811 				rw_exit(&ipst->ips_ill_g_lock);
4812 				/*
4813 				 * If ipif_zoneid was ALL_ZONES then we have
4814 				 * a trusted extensions shared IP address.
4815 				 * In that case GLOBAL_ZONEID works to send.
4816 				 */
4817 				if (zoneid == ALL_ZONES)
4818 					zoneid = GLOBAL_ZONEID;
4819 				return (zoneid);
4820 			}
4821 		}
4822 		mutex_exit(&ill->ill_lock);
4823 	}
4824 
4825 	/* If we already did the ptp case, then we are done */
4826 	if (ptp) {
4827 		rw_exit(&ipst->ips_ill_g_lock);
4828 		return (ALL_ZONES);
4829 	}
4830 	ptp = B_TRUE;
4831 	goto repeat;
4832 }
4833 
4834 /*
4835  * Look for an ipif that matches the specified remote address i.e. the
4836  * ipif that would receive the specified packet.
4837  * First look for directly connected interfaces and then do a recursive
4838  * IRE lookup and pick the first ipif corresponding to the source address in the
4839  * ire.
4840  * Returns: held ipif
4841  *
4842  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4843  */
4844 ipif_t *
4845 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4846 {
4847 	ipif_t	*ipif;
4848 
4849 	ASSERT(!ill->ill_isv6);
4850 
4851 	/*
4852 	 * Someone could be changing this ipif currently or change it
4853 	 * after we return this. Thus  a few packets could use the old
4854 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4855 	 * will atomically be updated or cleaned up with the new value
4856 	 * Thus we don't need a lock to check the flags or other attrs below.
4857 	 */
4858 	mutex_enter(&ill->ill_lock);
4859 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4860 		if (IPIF_IS_CONDEMNED(ipif))
4861 			continue;
4862 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4863 		    ipif->ipif_zoneid != ALL_ZONES)
4864 			continue;
4865 		/* Allow the ipif to be down */
4866 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4867 			if ((ipif->ipif_pp_dst_addr == addr) ||
4868 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4869 			    ipif->ipif_lcl_addr == addr)) {
4870 				ipif_refhold_locked(ipif);
4871 				mutex_exit(&ill->ill_lock);
4872 				return (ipif);
4873 			}
4874 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4875 			ipif_refhold_locked(ipif);
4876 			mutex_exit(&ill->ill_lock);
4877 			return (ipif);
4878 		}
4879 	}
4880 	mutex_exit(&ill->ill_lock);
4881 	/*
4882 	 * For a remote destination it isn't possible to nail down a particular
4883 	 * ipif.
4884 	 */
4885 
4886 	/* Pick the first interface */
4887 	ipif = ipif_get_next_ipif(NULL, ill);
4888 	return (ipif);
4889 }
4890 
4891 /*
4892  * This func does not prevent refcnt from increasing. But if
4893  * the caller has taken steps to that effect, then this func
4894  * can be used to determine whether the ill has become quiescent
4895  */
4896 static boolean_t
4897 ill_is_quiescent(ill_t *ill)
4898 {
4899 	ipif_t	*ipif;
4900 
4901 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4902 
4903 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4904 		if (ipif->ipif_refcnt != 0)
4905 			return (B_FALSE);
4906 	}
4907 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4908 		return (B_FALSE);
4909 	}
4910 	return (B_TRUE);
4911 }
4912 
4913 boolean_t
4914 ill_is_freeable(ill_t *ill)
4915 {
4916 	ipif_t	*ipif;
4917 
4918 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4919 
4920 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4921 		if (ipif->ipif_refcnt != 0) {
4922 			return (B_FALSE);
4923 		}
4924 	}
4925 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4926 		return (B_FALSE);
4927 	}
4928 	return (B_TRUE);
4929 }
4930 
4931 /*
4932  * This func does not prevent refcnt from increasing. But if
4933  * the caller has taken steps to that effect, then this func
4934  * can be used to determine whether the ipif has become quiescent
4935  */
4936 static boolean_t
4937 ipif_is_quiescent(ipif_t *ipif)
4938 {
4939 	ill_t *ill;
4940 
4941 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4942 
4943 	if (ipif->ipif_refcnt != 0)
4944 		return (B_FALSE);
4945 
4946 	ill = ipif->ipif_ill;
4947 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4948 	    ill->ill_logical_down) {
4949 		return (B_TRUE);
4950 	}
4951 
4952 	/* This is the last ipif going down or being deleted on this ill */
4953 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4954 		return (B_FALSE);
4955 	}
4956 
4957 	return (B_TRUE);
4958 }
4959 
4960 /*
4961  * return true if the ipif can be destroyed: the ipif has to be quiescent
4962  * with zero references from ire/ilm to it.
4963  */
4964 static boolean_t
4965 ipif_is_freeable(ipif_t *ipif)
4966 {
4967 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4968 	ASSERT(ipif->ipif_id != 0);
4969 	return (ipif->ipif_refcnt == 0);
4970 }
4971 
4972 /*
4973  * The ipif/ill/ire has been refreled. Do the tail processing.
4974  * Determine if the ipif or ill in question has become quiescent and if so
4975  * wakeup close and/or restart any queued pending ioctl that is waiting
4976  * for the ipif_down (or ill_down)
4977  */
4978 void
4979 ipif_ill_refrele_tail(ill_t *ill)
4980 {
4981 	mblk_t	*mp;
4982 	conn_t	*connp;
4983 	ipsq_t	*ipsq;
4984 	ipxop_t	*ipx;
4985 	ipif_t	*ipif;
4986 	dl_notify_ind_t *dlindp;
4987 
4988 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4989 
4990 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4991 		/* ip_modclose() may be waiting */
4992 		cv_broadcast(&ill->ill_cv);
4993 	}
4994 
4995 	ipsq = ill->ill_phyint->phyint_ipsq;
4996 	mutex_enter(&ipsq->ipsq_lock);
4997 	ipx = ipsq->ipsq_xop;
4998 	mutex_enter(&ipx->ipx_lock);
4999 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
5000 		goto unlock;
5001 
5002 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
5003 
5004 	ipif = ipx->ipx_pending_ipif;
5005 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
5006 		goto unlock;
5007 
5008 	switch (ipx->ipx_waitfor) {
5009 	case IPIF_DOWN:
5010 		if (!ipif_is_quiescent(ipif))
5011 			goto unlock;
5012 		break;
5013 	case IPIF_FREE:
5014 		if (!ipif_is_freeable(ipif))
5015 			goto unlock;
5016 		break;
5017 	case ILL_DOWN:
5018 		if (!ill_is_quiescent(ill))
5019 			goto unlock;
5020 		break;
5021 	case ILL_FREE:
5022 		/*
5023 		 * ILL_FREE is only for loopback; normal ill teardown waits
5024 		 * synchronously in ip_modclose() without using ipx_waitfor,
5025 		 * handled by the cv_broadcast() at the top of this function.
5026 		 */
5027 		if (!ill_is_freeable(ill))
5028 			goto unlock;
5029 		break;
5030 	default:
5031 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
5032 		    (void *)ipsq, ipx->ipx_waitfor);
5033 	}
5034 
5035 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
5036 	mutex_exit(&ipx->ipx_lock);
5037 	mp = ipsq_pending_mp_get(ipsq, &connp);
5038 	mutex_exit(&ipsq->ipsq_lock);
5039 	mutex_exit(&ill->ill_lock);
5040 
5041 	ASSERT(mp != NULL);
5042 	/*
5043 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5044 	 * we can only get here when the current operation decides it
5045 	 * it needs to quiesce via ipsq_pending_mp_add().
5046 	 */
5047 	switch (mp->b_datap->db_type) {
5048 	case M_PCPROTO:
5049 	case M_PROTO:
5050 		/*
5051 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5052 		 */
5053 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5054 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5055 
5056 		switch (dlindp->dl_notification) {
5057 		case DL_NOTE_PHYS_ADDR:
5058 			qwriter_ip(ill, ill->ill_rq, mp,
5059 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5060 			return;
5061 		case DL_NOTE_REPLUMB:
5062 			qwriter_ip(ill, ill->ill_rq, mp,
5063 			    ill_replumb_tail, CUR_OP, B_TRUE);
5064 			return;
5065 		default:
5066 			ASSERT(0);
5067 			ill_refrele(ill);
5068 		}
5069 		break;
5070 
5071 	case M_ERROR:
5072 	case M_HANGUP:
5073 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5074 		    B_TRUE);
5075 		return;
5076 
5077 	case M_IOCTL:
5078 	case M_IOCDATA:
5079 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5080 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5081 		return;
5082 
5083 	default:
5084 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5085 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5086 	}
5087 	return;
5088 unlock:
5089 	mutex_exit(&ipsq->ipsq_lock);
5090 	mutex_exit(&ipx->ipx_lock);
5091 	mutex_exit(&ill->ill_lock);
5092 }
5093 
5094 #ifdef DEBUG
5095 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5096 static void
5097 th_trace_rrecord(th_trace_t *th_trace)
5098 {
5099 	tr_buf_t *tr_buf;
5100 	uint_t lastref;
5101 
5102 	lastref = th_trace->th_trace_lastref;
5103 	lastref++;
5104 	if (lastref == TR_BUF_MAX)
5105 		lastref = 0;
5106 	th_trace->th_trace_lastref = lastref;
5107 	tr_buf = &th_trace->th_trbuf[lastref];
5108 	tr_buf->tr_time = ddi_get_lbolt();
5109 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5110 }
5111 
5112 static void
5113 th_trace_free(void *value)
5114 {
5115 	th_trace_t *th_trace = value;
5116 
5117 	ASSERT(th_trace->th_refcnt == 0);
5118 	kmem_free(th_trace, sizeof (*th_trace));
5119 }
5120 
5121 /*
5122  * Find or create the per-thread hash table used to track object references.
5123  * The ipst argument is NULL if we shouldn't allocate.
5124  *
5125  * Accesses per-thread data, so there's no need to lock here.
5126  */
5127 static mod_hash_t *
5128 th_trace_gethash(ip_stack_t *ipst)
5129 {
5130 	th_hash_t *thh;
5131 
5132 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5133 		mod_hash_t *mh;
5134 		char name[256];
5135 		size_t objsize, rshift;
5136 		int retv;
5137 
5138 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5139 			return (NULL);
5140 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5141 		    (void *)curthread);
5142 
5143 		/*
5144 		 * We use mod_hash_create_extended here rather than the more
5145 		 * obvious mod_hash_create_ptrhash because the latter has a
5146 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5147 		 * block.
5148 		 */
5149 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5150 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5151 		rshift = highbit(objsize);
5152 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5153 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5154 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5155 		if (mh == NULL) {
5156 			kmem_free(thh, sizeof (*thh));
5157 			return (NULL);
5158 		}
5159 		thh->thh_hash = mh;
5160 		thh->thh_ipst = ipst;
5161 		/*
5162 		 * We trace ills, ipifs, ires, and nces.  All of these are
5163 		 * per-IP-stack, so the lock on the thread list is as well.
5164 		 */
5165 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5166 		list_insert_tail(&ip_thread_list, thh);
5167 		rw_exit(&ip_thread_rwlock);
5168 		retv = tsd_set(ip_thread_data, thh);
5169 		ASSERT(retv == 0);
5170 	}
5171 	return (thh != NULL ? thh->thh_hash : NULL);
5172 }
5173 
5174 boolean_t
5175 th_trace_ref(const void *obj, ip_stack_t *ipst)
5176 {
5177 	th_trace_t *th_trace;
5178 	mod_hash_t *mh;
5179 	mod_hash_val_t val;
5180 
5181 	if ((mh = th_trace_gethash(ipst)) == NULL)
5182 		return (B_FALSE);
5183 
5184 	/*
5185 	 * Attempt to locate the trace buffer for this obj and thread.
5186 	 * If it does not exist, then allocate a new trace buffer and
5187 	 * insert into the hash.
5188 	 */
5189 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5190 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5191 		if (th_trace == NULL)
5192 			return (B_FALSE);
5193 
5194 		th_trace->th_id = curthread;
5195 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5196 		    (mod_hash_val_t)th_trace) != 0) {
5197 			kmem_free(th_trace, sizeof (th_trace_t));
5198 			return (B_FALSE);
5199 		}
5200 	} else {
5201 		th_trace = (th_trace_t *)val;
5202 	}
5203 
5204 	ASSERT(th_trace->th_refcnt >= 0 &&
5205 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5206 
5207 	th_trace->th_refcnt++;
5208 	th_trace_rrecord(th_trace);
5209 	return (B_TRUE);
5210 }
5211 
5212 /*
5213  * For the purpose of tracing a reference release, we assume that global
5214  * tracing is always on and that the same thread initiated the reference hold
5215  * is releasing.
5216  */
5217 void
5218 th_trace_unref(const void *obj)
5219 {
5220 	int retv;
5221 	mod_hash_t *mh;
5222 	th_trace_t *th_trace;
5223 	mod_hash_val_t val;
5224 
5225 	mh = th_trace_gethash(NULL);
5226 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5227 	ASSERT(retv == 0);
5228 	th_trace = (th_trace_t *)val;
5229 
5230 	ASSERT(th_trace->th_refcnt > 0);
5231 	th_trace->th_refcnt--;
5232 	th_trace_rrecord(th_trace);
5233 }
5234 
5235 /*
5236  * If tracing has been disabled, then we assume that the reference counts are
5237  * now useless, and we clear them out before destroying the entries.
5238  */
5239 void
5240 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5241 {
5242 	th_hash_t	*thh;
5243 	mod_hash_t	*mh;
5244 	mod_hash_val_t	val;
5245 	th_trace_t	*th_trace;
5246 	int		retv;
5247 
5248 	rw_enter(&ip_thread_rwlock, RW_READER);
5249 	for (thh = list_head(&ip_thread_list); thh != NULL;
5250 	    thh = list_next(&ip_thread_list, thh)) {
5251 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5252 		    &val) == 0) {
5253 			th_trace = (th_trace_t *)val;
5254 			if (trace_disable)
5255 				th_trace->th_refcnt = 0;
5256 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5257 			ASSERT(retv == 0);
5258 		}
5259 	}
5260 	rw_exit(&ip_thread_rwlock);
5261 }
5262 
5263 void
5264 ipif_trace_ref(ipif_t *ipif)
5265 {
5266 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5267 
5268 	if (ipif->ipif_trace_disable)
5269 		return;
5270 
5271 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5272 		ipif->ipif_trace_disable = B_TRUE;
5273 		ipif_trace_cleanup(ipif);
5274 	}
5275 }
5276 
5277 void
5278 ipif_untrace_ref(ipif_t *ipif)
5279 {
5280 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5281 
5282 	if (!ipif->ipif_trace_disable)
5283 		th_trace_unref(ipif);
5284 }
5285 
5286 void
5287 ill_trace_ref(ill_t *ill)
5288 {
5289 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5290 
5291 	if (ill->ill_trace_disable)
5292 		return;
5293 
5294 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5295 		ill->ill_trace_disable = B_TRUE;
5296 		ill_trace_cleanup(ill);
5297 	}
5298 }
5299 
5300 void
5301 ill_untrace_ref(ill_t *ill)
5302 {
5303 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5304 
5305 	if (!ill->ill_trace_disable)
5306 		th_trace_unref(ill);
5307 }
5308 
5309 /*
5310  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5311  * failure, ipif_trace_disable is set.
5312  */
5313 static void
5314 ipif_trace_cleanup(const ipif_t *ipif)
5315 {
5316 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5317 }
5318 
5319 /*
5320  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5321  * failure, ill_trace_disable is set.
5322  */
5323 static void
5324 ill_trace_cleanup(const ill_t *ill)
5325 {
5326 	th_trace_cleanup(ill, ill->ill_trace_disable);
5327 }
5328 #endif /* DEBUG */
5329 
5330 void
5331 ipif_refhold_locked(ipif_t *ipif)
5332 {
5333 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5334 	ipif->ipif_refcnt++;
5335 	IPIF_TRACE_REF(ipif);
5336 }
5337 
5338 void
5339 ipif_refhold(ipif_t *ipif)
5340 {
5341 	ill_t	*ill;
5342 
5343 	ill = ipif->ipif_ill;
5344 	mutex_enter(&ill->ill_lock);
5345 	ipif->ipif_refcnt++;
5346 	IPIF_TRACE_REF(ipif);
5347 	mutex_exit(&ill->ill_lock);
5348 }
5349 
5350 /*
5351  * Must not be called while holding any locks. Otherwise if this is
5352  * the last reference to be released there is a chance of recursive mutex
5353  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5354  * to restart an ioctl.
5355  */
5356 void
5357 ipif_refrele(ipif_t *ipif)
5358 {
5359 	ill_t	*ill;
5360 
5361 	ill = ipif->ipif_ill;
5362 
5363 	mutex_enter(&ill->ill_lock);
5364 	ASSERT(ipif->ipif_refcnt != 0);
5365 	ipif->ipif_refcnt--;
5366 	IPIF_UNTRACE_REF(ipif);
5367 	if (ipif->ipif_refcnt != 0) {
5368 		mutex_exit(&ill->ill_lock);
5369 		return;
5370 	}
5371 
5372 	/* Drops the ill_lock */
5373 	ipif_ill_refrele_tail(ill);
5374 }
5375 
5376 ipif_t *
5377 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5378 {
5379 	ipif_t	*ipif;
5380 
5381 	mutex_enter(&ill->ill_lock);
5382 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5383 	    ipif != NULL; ipif = ipif->ipif_next) {
5384 		if (IPIF_IS_CONDEMNED(ipif))
5385 			continue;
5386 		ipif_refhold_locked(ipif);
5387 		mutex_exit(&ill->ill_lock);
5388 		return (ipif);
5389 	}
5390 	mutex_exit(&ill->ill_lock);
5391 	return (NULL);
5392 }
5393 
5394 /*
5395  * TODO: make this table extendible at run time
5396  * Return a pointer to the mac type info for 'mac_type'
5397  */
5398 static ip_m_t *
5399 ip_m_lookup(t_uscalar_t mac_type)
5400 {
5401 	ip_m_t	*ipm;
5402 
5403 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5404 		if (ipm->ip_m_mac_type == mac_type)
5405 			return (ipm);
5406 	return (NULL);
5407 }
5408 
5409 /*
5410  * Make a link layer address from the multicast IP address *addr.
5411  * To form the link layer address, invoke the ip_m_v*mapping function
5412  * associated with the link-layer type.
5413  */
5414 void
5415 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5416 {
5417 	ip_m_t *ipm;
5418 
5419 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5420 		return;
5421 
5422 	ASSERT(addr != NULL);
5423 
5424 	ipm = ip_m_lookup(ill->ill_mactype);
5425 	if (ipm == NULL ||
5426 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5427 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5428 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5429 		    ill->ill_name, ill->ill_mactype));
5430 		return;
5431 	}
5432 	if (ill->ill_isv6)
5433 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5434 	else
5435 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5436 }
5437 
5438 /*
5439  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5440  * ill is passed in to associate it with the correct interface.
5441  * If ire_arg is set, then we return the held IRE in that location.
5442  */
5443 int
5444 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5445     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5446     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5447 {
5448 	ire_t	*ire, *nire;
5449 	ire_t	*gw_ire = NULL;
5450 	ipif_t	*ipif = NULL;
5451 	uint_t	type;
5452 	int	match_flags = MATCH_IRE_TYPE;
5453 	tsol_gc_t *gc = NULL;
5454 	tsol_gcgrp_t *gcgrp = NULL;
5455 	boolean_t gcgrp_xtraref = B_FALSE;
5456 	boolean_t cgtp_broadcast;
5457 	boolean_t unbound = B_FALSE;
5458 
5459 	ip1dbg(("ip_rt_add:"));
5460 
5461 	if (ire_arg != NULL)
5462 		*ire_arg = NULL;
5463 
5464 	/*
5465 	 * If this is the case of RTF_HOST being set, then we set the netmask
5466 	 * to all ones (regardless if one was supplied).
5467 	 */
5468 	if (flags & RTF_HOST)
5469 		mask = IP_HOST_MASK;
5470 
5471 	/*
5472 	 * Prevent routes with a zero gateway from being created (since
5473 	 * interfaces can currently be plumbed and brought up no assigned
5474 	 * address).
5475 	 */
5476 	if (gw_addr == 0)
5477 		return (ENETUNREACH);
5478 	/*
5479 	 * Get the ipif, if any, corresponding to the gw_addr
5480 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5481 	 * we match on the gatway and destination to handle unnumbered pt-pt
5482 	 * interfaces.
5483 	 */
5484 	if (ill != NULL)
5485 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5486 	else
5487 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5488 	if (ipif != NULL) {
5489 		if (IS_VNI(ipif->ipif_ill)) {
5490 			ipif_refrele(ipif);
5491 			return (EINVAL);
5492 		}
5493 	}
5494 
5495 	/*
5496 	 * GateD will attempt to create routes with a loopback interface
5497 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5498 	 * these routes to be added, but create them as interface routes
5499 	 * since the gateway is an interface address.
5500 	 */
5501 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5502 		flags &= ~RTF_GATEWAY;
5503 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5504 		    mask == IP_HOST_MASK) {
5505 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5506 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5507 			    NULL);
5508 			if (ire != NULL) {
5509 				ire_refrele(ire);
5510 				ipif_refrele(ipif);
5511 				return (EEXIST);
5512 			}
5513 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5514 			    "for 0x%x\n", (void *)ipif,
5515 			    ipif->ipif_ire_type,
5516 			    ntohl(ipif->ipif_lcl_addr)));
5517 			ire = ire_create(
5518 			    (uchar_t *)&dst_addr,	/* dest address */
5519 			    (uchar_t *)&mask,		/* mask */
5520 			    NULL,			/* no gateway */
5521 			    ipif->ipif_ire_type,	/* LOOPBACK */
5522 			    ipif->ipif_ill,
5523 			    zoneid,
5524 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5525 			    NULL,
5526 			    ipst);
5527 
5528 			if (ire == NULL) {
5529 				ipif_refrele(ipif);
5530 				return (ENOMEM);
5531 			}
5532 			/* src address assigned by the caller? */
5533 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5534 				ire->ire_setsrc_addr = src_addr;
5535 
5536 			nire = ire_add(ire);
5537 			if (nire == NULL) {
5538 				/*
5539 				 * In the result of failure, ire_add() will have
5540 				 * already deleted the ire in question, so there
5541 				 * is no need to do that here.
5542 				 */
5543 				ipif_refrele(ipif);
5544 				return (ENOMEM);
5545 			}
5546 			/*
5547 			 * Check if it was a duplicate entry. This handles
5548 			 * the case of two racing route adds for the same route
5549 			 */
5550 			if (nire != ire) {
5551 				ASSERT(nire->ire_identical_ref > 1);
5552 				ire_delete(nire);
5553 				ire_refrele(nire);
5554 				ipif_refrele(ipif);
5555 				return (EEXIST);
5556 			}
5557 			ire = nire;
5558 			goto save_ire;
5559 		}
5560 	}
5561 
5562 	/*
5563 	 * The routes for multicast with CGTP are quite special in that
5564 	 * the gateway is the local interface address, yet RTF_GATEWAY
5565 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5566 	 * this undocumented and unusual use of multicast routes.
5567 	 */
5568 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5569 		flags &= ~RTF_GATEWAY;
5570 
5571 	/*
5572 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5573 	 * and the gateway address provided is one of the system's interface
5574 	 * addresses.  By using the routing socket interface and supplying an
5575 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5576 	 * specifying an interface route to be created is available which uses
5577 	 * the interface index that specifies the outgoing interface rather than
5578 	 * the address of an outgoing interface (which may not be able to
5579 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5580 	 * flag, routes can be specified which not only specify the next-hop to
5581 	 * be used when routing to a certain prefix, but also which outgoing
5582 	 * interface should be used.
5583 	 *
5584 	 * Previously, interfaces would have unique addresses assigned to them
5585 	 * and so the address assigned to a particular interface could be used
5586 	 * to identify a particular interface.  One exception to this was the
5587 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5588 	 *
5589 	 * With the advent of IPv6 and its link-local addresses, this
5590 	 * restriction was relaxed and interfaces could share addresses between
5591 	 * themselves.  In fact, typically all of the link-local interfaces on
5592 	 * an IPv6 node or router will have the same link-local address.  In
5593 	 * order to differentiate between these interfaces, the use of an
5594 	 * interface index is necessary and this index can be carried inside a
5595 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5596 	 * of using the interface index, however, is that all of the ipif's that
5597 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5598 	 * cannot be used to differentiate between ipif's (or logical
5599 	 * interfaces) that belong to the same ill (physical interface).
5600 	 *
5601 	 * For example, in the following case involving IPv4 interfaces and
5602 	 * logical interfaces
5603 	 *
5604 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5605 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5606 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5607 	 *
5608 	 * the ipif's corresponding to each of these interface routes can be
5609 	 * uniquely identified by the "gateway" (actually interface address).
5610 	 *
5611 	 * In this case involving multiple IPv6 default routes to a particular
5612 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5613 	 * default route is of interest:
5614 	 *
5615 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5616 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5617 	 */
5618 
5619 	/* RTF_GATEWAY not set */
5620 	if (!(flags & RTF_GATEWAY)) {
5621 		if (sp != NULL) {
5622 			ip2dbg(("ip_rt_add: gateway security attributes "
5623 			    "cannot be set with interface route\n"));
5624 			if (ipif != NULL)
5625 				ipif_refrele(ipif);
5626 			return (EINVAL);
5627 		}
5628 
5629 		/*
5630 		 * Whether or not ill (RTA_IFP) is set, we require that
5631 		 * the gateway is one of our local addresses.
5632 		 */
5633 		if (ipif == NULL)
5634 			return (ENETUNREACH);
5635 
5636 		/*
5637 		 * We use MATCH_IRE_ILL here. If the caller specified an
5638 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5639 		 * we use the ill derived from the gateway address.
5640 		 * We can always match the gateway address since we record it
5641 		 * in ire_gateway_addr.
5642 		 * We don't allow RTA_IFP to specify a different ill than the
5643 		 * one matching the ipif to make sure we can delete the route.
5644 		 */
5645 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5646 		if (ill == NULL) {
5647 			ill = ipif->ipif_ill;
5648 		} else if (ill != ipif->ipif_ill) {
5649 			ipif_refrele(ipif);
5650 			return (EINVAL);
5651 		}
5652 
5653 		/*
5654 		 * We check for an existing entry at this point.
5655 		 *
5656 		 * Since a netmask isn't passed in via the ioctl interface
5657 		 * (SIOCADDRT), we don't check for a matching netmask in that
5658 		 * case.
5659 		 */
5660 		if (!ioctl_msg)
5661 			match_flags |= MATCH_IRE_MASK;
5662 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5663 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5664 		    NULL);
5665 		if (ire != NULL) {
5666 			ire_refrele(ire);
5667 			ipif_refrele(ipif);
5668 			return (EEXIST);
5669 		}
5670 
5671 		/*
5672 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5673 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5674 		 * gateway.
5675 		 */
5676 		ire = ire_create(
5677 		    (uchar_t *)&dst_addr,
5678 		    (uint8_t *)&mask,
5679 		    (uint8_t *)&gw_addr,
5680 		    ill->ill_net_type,
5681 		    ill,
5682 		    zoneid,
5683 		    flags,
5684 		    NULL,
5685 		    ipst);
5686 		if (ire == NULL) {
5687 			ipif_refrele(ipif);
5688 			return (ENOMEM);
5689 		}
5690 
5691 		/*
5692 		 * Some software (for example, GateD and Sun Cluster) attempts
5693 		 * to create (what amount to) IRE_PREFIX routes with the
5694 		 * loopback address as the gateway.  This is primarily done to
5695 		 * set up prefixes with the RTF_REJECT flag set (for example,
5696 		 * when generating aggregate routes.)
5697 		 *
5698 		 * If the IRE type (as defined by ill->ill_net_type) is
5699 		 * IRE_LOOPBACK, then we map the request into a
5700 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5701 		 * these interface routes, by definition, can only be that.
5702 		 *
5703 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5704 		 * routine, but rather using ire_create() directly.
5705 		 *
5706 		 */
5707 		if (ill->ill_net_type == IRE_LOOPBACK) {
5708 			ire->ire_type = IRE_IF_NORESOLVER;
5709 			ire->ire_flags |= RTF_BLACKHOLE;
5710 		}
5711 
5712 		/* src address assigned by the caller? */
5713 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5714 			ire->ire_setsrc_addr = src_addr;
5715 
5716 		nire = ire_add(ire);
5717 		if (nire == NULL) {
5718 			/*
5719 			 * In the result of failure, ire_add() will have
5720 			 * already deleted the ire in question, so there
5721 			 * is no need to do that here.
5722 			 */
5723 			ipif_refrele(ipif);
5724 			return (ENOMEM);
5725 		}
5726 		/*
5727 		 * Check if it was a duplicate entry. This handles
5728 		 * the case of two racing route adds for the same route
5729 		 */
5730 		if (nire != ire) {
5731 			ire_delete(nire);
5732 			ire_refrele(nire);
5733 			ipif_refrele(ipif);
5734 			return (EEXIST);
5735 		}
5736 		ire = nire;
5737 		goto save_ire;
5738 	}
5739 
5740 	/*
5741 	 * Get an interface IRE for the specified gateway.
5742 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5743 	 * gateway, it is currently unreachable and we fail the request
5744 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5745 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5746 	 * If RTA_IFP was specified we look on that particular ill.
5747 	 */
5748 	if (ill != NULL)
5749 		match_flags |= MATCH_IRE_ILL;
5750 
5751 	/* Check whether the gateway is reachable. */
5752 again:
5753 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5754 	if (flags & RTF_INDIRECT)
5755 		type |= IRE_OFFLINK;
5756 
5757 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5758 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5759 	if (gw_ire == NULL) {
5760 		/*
5761 		 * With IPMP, we allow host routes to influence in.mpathd's
5762 		 * target selection.  However, if the test addresses are on
5763 		 * their own network, the above lookup will fail since the
5764 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5765 		 * hidden test IREs to be found and try again.
5766 		 */
5767 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5768 			match_flags |= MATCH_IRE_TESTHIDDEN;
5769 			goto again;
5770 		}
5771 		if (ipif != NULL)
5772 			ipif_refrele(ipif);
5773 		return (ENETUNREACH);
5774 	}
5775 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5776 		ire_refrele(gw_ire);
5777 		if (ipif != NULL)
5778 			ipif_refrele(ipif);
5779 		return (ENETUNREACH);
5780 	}
5781 
5782 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5783 		unbound = B_TRUE;
5784 		if (ipst->ips_ip_strict_src_multihoming > 0)
5785 			ill = gw_ire->ire_ill;
5786 	}
5787 
5788 	/*
5789 	 * We create one of three types of IREs as a result of this request
5790 	 * based on the netmask.  A netmask of all ones (which is automatically
5791 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5792 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5793 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5794 	 * destination prefix.
5795 	 */
5796 	if (mask == IP_HOST_MASK)
5797 		type = IRE_HOST;
5798 	else if (mask == 0)
5799 		type = IRE_DEFAULT;
5800 	else
5801 		type = IRE_PREFIX;
5802 
5803 	/* check for a duplicate entry */
5804 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5805 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5806 	    0, ipst, NULL);
5807 	if (ire != NULL) {
5808 		if (ipif != NULL)
5809 			ipif_refrele(ipif);
5810 		ire_refrele(gw_ire);
5811 		ire_refrele(ire);
5812 		return (EEXIST);
5813 	}
5814 
5815 	/* Security attribute exists */
5816 	if (sp != NULL) {
5817 		tsol_gcgrp_addr_t ga;
5818 
5819 		/* find or create the gateway credentials group */
5820 		ga.ga_af = AF_INET;
5821 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5822 
5823 		/* we hold reference to it upon success */
5824 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5825 		if (gcgrp == NULL) {
5826 			if (ipif != NULL)
5827 				ipif_refrele(ipif);
5828 			ire_refrele(gw_ire);
5829 			return (ENOMEM);
5830 		}
5831 
5832 		/*
5833 		 * Create and add the security attribute to the group; a
5834 		 * reference to the group is made upon allocating a new
5835 		 * entry successfully.  If it finds an already-existing
5836 		 * entry for the security attribute in the group, it simply
5837 		 * returns it and no new reference is made to the group.
5838 		 */
5839 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5840 		if (gc == NULL) {
5841 			if (ipif != NULL)
5842 				ipif_refrele(ipif);
5843 			/* release reference held by gcgrp_lookup */
5844 			GCGRP_REFRELE(gcgrp);
5845 			ire_refrele(gw_ire);
5846 			return (ENOMEM);
5847 		}
5848 	}
5849 
5850 	/* Create the IRE. */
5851 	ire = ire_create(
5852 	    (uchar_t *)&dst_addr,		/* dest address */
5853 	    (uchar_t *)&mask,			/* mask */
5854 	    (uchar_t *)&gw_addr,		/* gateway address */
5855 	    (ushort_t)type,			/* IRE type */
5856 	    ill,
5857 	    zoneid,
5858 	    flags,
5859 	    gc,					/* security attribute */
5860 	    ipst);
5861 
5862 	/*
5863 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5864 	 * reference to the 'gcgrp'. We can now release the extra reference
5865 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5866 	 */
5867 	if (gcgrp_xtraref)
5868 		GCGRP_REFRELE(gcgrp);
5869 	if (ire == NULL) {
5870 		if (gc != NULL)
5871 			GC_REFRELE(gc);
5872 		if (ipif != NULL)
5873 			ipif_refrele(ipif);
5874 		ire_refrele(gw_ire);
5875 		return (ENOMEM);
5876 	}
5877 
5878 	/* Before we add, check if an extra CGTP broadcast is needed */
5879 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5880 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5881 
5882 	/* src address assigned by the caller? */
5883 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5884 		ire->ire_setsrc_addr = src_addr;
5885 
5886 	ire->ire_unbound = unbound;
5887 
5888 	/*
5889 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5890 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5891 	 */
5892 
5893 	/* Add the new IRE. */
5894 	nire = ire_add(ire);
5895 	if (nire == NULL) {
5896 		/*
5897 		 * In the result of failure, ire_add() will have
5898 		 * already deleted the ire in question, so there
5899 		 * is no need to do that here.
5900 		 */
5901 		if (ipif != NULL)
5902 			ipif_refrele(ipif);
5903 		ire_refrele(gw_ire);
5904 		return (ENOMEM);
5905 	}
5906 	/*
5907 	 * Check if it was a duplicate entry. This handles
5908 	 * the case of two racing route adds for the same route
5909 	 */
5910 	if (nire != ire) {
5911 		ire_delete(nire);
5912 		ire_refrele(nire);
5913 		if (ipif != NULL)
5914 			ipif_refrele(ipif);
5915 		ire_refrele(gw_ire);
5916 		return (EEXIST);
5917 	}
5918 	ire = nire;
5919 
5920 	if (flags & RTF_MULTIRT) {
5921 		/*
5922 		 * Invoke the CGTP (multirouting) filtering module
5923 		 * to add the dst address in the filtering database.
5924 		 * Replicated inbound packets coming from that address
5925 		 * will be filtered to discard the duplicates.
5926 		 * It is not necessary to call the CGTP filter hook
5927 		 * when the dst address is a broadcast or multicast,
5928 		 * because an IP source address cannot be a broadcast
5929 		 * or a multicast.
5930 		 */
5931 		if (cgtp_broadcast) {
5932 			ip_cgtp_bcast_add(ire, ipst);
5933 			goto save_ire;
5934 		}
5935 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5936 		    !CLASSD(ire->ire_addr)) {
5937 			int res;
5938 			ipif_t *src_ipif;
5939 
5940 			/* Find the source address corresponding to gw_ire */
5941 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5942 			    NULL, zoneid, ipst);
5943 			if (src_ipif != NULL) {
5944 				res = ipst->ips_ip_cgtp_filter_ops->
5945 				    cfo_add_dest_v4(
5946 				    ipst->ips_netstack->netstack_stackid,
5947 				    ire->ire_addr,
5948 				    ire->ire_gateway_addr,
5949 				    ire->ire_setsrc_addr,
5950 				    src_ipif->ipif_lcl_addr);
5951 				ipif_refrele(src_ipif);
5952 			} else {
5953 				res = EADDRNOTAVAIL;
5954 			}
5955 			if (res != 0) {
5956 				if (ipif != NULL)
5957 					ipif_refrele(ipif);
5958 				ire_refrele(gw_ire);
5959 				ire_delete(ire);
5960 				ire_refrele(ire);	/* Held in ire_add */
5961 				return (res);
5962 			}
5963 		}
5964 	}
5965 
5966 save_ire:
5967 	if (gw_ire != NULL) {
5968 		ire_refrele(gw_ire);
5969 		gw_ire = NULL;
5970 	}
5971 	if (ill != NULL) {
5972 		/*
5973 		 * Save enough information so that we can recreate the IRE if
5974 		 * the interface goes down and then up.  The metrics associated
5975 		 * with the route will be saved as well when rts_setmetrics() is
5976 		 * called after the IRE has been created.  In the case where
5977 		 * memory cannot be allocated, none of this information will be
5978 		 * saved.
5979 		 */
5980 		ill_save_ire(ill, ire);
5981 	}
5982 	if (ioctl_msg)
5983 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5984 	if (ire_arg != NULL) {
5985 		/*
5986 		 * Store the ire that was successfully added into where ire_arg
5987 		 * points to so that callers don't have to look it up
5988 		 * themselves (but they are responsible for ire_refrele()ing
5989 		 * the ire when they are finished with it).
5990 		 */
5991 		*ire_arg = ire;
5992 	} else {
5993 		ire_refrele(ire);		/* Held in ire_add */
5994 	}
5995 	if (ipif != NULL)
5996 		ipif_refrele(ipif);
5997 	return (0);
5998 }
5999 
6000 /*
6001  * ip_rt_delete is called to delete an IPv4 route.
6002  * ill is passed in to associate it with the correct interface.
6003  */
6004 /* ARGSUSED4 */
6005 int
6006 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6007     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
6008     ip_stack_t *ipst, zoneid_t zoneid)
6009 {
6010 	ire_t	*ire = NULL;
6011 	ipif_t	*ipif;
6012 	uint_t	type;
6013 	uint_t	match_flags = MATCH_IRE_TYPE;
6014 	int	err = 0;
6015 
6016 	ip1dbg(("ip_rt_delete:"));
6017 	/*
6018 	 * If this is the case of RTF_HOST being set, then we set the netmask
6019 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6020 	 */
6021 	if (flags & RTF_HOST) {
6022 		mask = IP_HOST_MASK;
6023 		match_flags |= MATCH_IRE_MASK;
6024 	} else if (rtm_addrs & RTA_NETMASK) {
6025 		match_flags |= MATCH_IRE_MASK;
6026 	}
6027 
6028 	/*
6029 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6030 	 * we check if the gateway address is one of our interfaces first,
6031 	 * and fall back on RTF_GATEWAY routes.
6032 	 *
6033 	 * This makes it possible to delete an original
6034 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6035 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6036 	 * and those can not be deleted here.
6037 	 *
6038 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6039 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6040 	 * otherwise we use the ill derived from the gateway address.
6041 	 * We can always match the gateway address since we record it
6042 	 * in ire_gateway_addr.
6043 	 *
6044 	 * For more detail on specifying routes by gateway address and by
6045 	 * interface index, see the comments in ip_rt_add().
6046 	 */
6047 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6048 	if (ipif != NULL) {
6049 		ill_t	*ill_match;
6050 
6051 		if (ill != NULL)
6052 			ill_match = ill;
6053 		else
6054 			ill_match = ipif->ipif_ill;
6055 
6056 		match_flags |= MATCH_IRE_ILL;
6057 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6058 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
6059 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
6060 			    NULL);
6061 		}
6062 		if (ire == NULL) {
6063 			match_flags |= MATCH_IRE_GW;
6064 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6065 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6066 			    match_flags, 0, ipst, NULL);
6067 		}
6068 		/* Avoid deleting routes created by kernel from an ipif */
6069 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6070 			ire_refrele(ire);
6071 			ire = NULL;
6072 		}
6073 
6074 		/* Restore in case we didn't find a match */
6075 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6076 	}
6077 
6078 	if (ire == NULL) {
6079 		/*
6080 		 * At this point, the gateway address is not one of our own
6081 		 * addresses or a matching interface route was not found.  We
6082 		 * set the IRE type to lookup based on whether
6083 		 * this is a host route, a default route or just a prefix.
6084 		 *
6085 		 * If an ill was passed in, then the lookup is based on an
6086 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6087 		 */
6088 		match_flags |= MATCH_IRE_GW;
6089 		if (ill != NULL)
6090 			match_flags |= MATCH_IRE_ILL;
6091 		if (mask == IP_HOST_MASK)
6092 			type = IRE_HOST;
6093 		else if (mask == 0)
6094 			type = IRE_DEFAULT;
6095 		else
6096 			type = IRE_PREFIX;
6097 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6098 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6099 	}
6100 
6101 	if (ipif != NULL) {
6102 		ipif_refrele(ipif);
6103 		ipif = NULL;
6104 	}
6105 
6106 	if (ire == NULL)
6107 		return (ESRCH);
6108 
6109 	if (ire->ire_flags & RTF_MULTIRT) {
6110 		/*
6111 		 * Invoke the CGTP (multirouting) filtering module
6112 		 * to remove the dst address from the filtering database.
6113 		 * Packets coming from that address will no longer be
6114 		 * filtered to remove duplicates.
6115 		 */
6116 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6117 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6118 			    ipst->ips_netstack->netstack_stackid,
6119 			    ire->ire_addr, ire->ire_gateway_addr);
6120 		}
6121 		ip_cgtp_bcast_delete(ire, ipst);
6122 	}
6123 
6124 	ill = ire->ire_ill;
6125 	if (ill != NULL)
6126 		ill_remove_saved_ire(ill, ire);
6127 	if (ioctl_msg)
6128 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6129 	ire_delete(ire);
6130 	ire_refrele(ire);
6131 	return (err);
6132 }
6133 
6134 /*
6135  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6136  */
6137 /* ARGSUSED */
6138 int
6139 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6140     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6141 {
6142 	ipaddr_t dst_addr;
6143 	ipaddr_t gw_addr;
6144 	ipaddr_t mask;
6145 	int error = 0;
6146 	mblk_t *mp1;
6147 	struct rtentry *rt;
6148 	ipif_t *ipif = NULL;
6149 	ip_stack_t	*ipst;
6150 
6151 	ASSERT(q->q_next == NULL);
6152 	ipst = CONNQ_TO_IPST(q);
6153 
6154 	ip1dbg(("ip_siocaddrt:"));
6155 	/* Existence of mp1 verified in ip_wput_nondata */
6156 	mp1 = mp->b_cont->b_cont;
6157 	rt = (struct rtentry *)mp1->b_rptr;
6158 
6159 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6160 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6161 
6162 	/*
6163 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6164 	 * to a particular host address.  In this case, we set the netmask to
6165 	 * all ones for the particular destination address.  Otherwise,
6166 	 * determine the netmask to be used based on dst_addr and the interfaces
6167 	 * in use.
6168 	 */
6169 	if (rt->rt_flags & RTF_HOST) {
6170 		mask = IP_HOST_MASK;
6171 	} else {
6172 		/*
6173 		 * Note that ip_subnet_mask returns a zero mask in the case of
6174 		 * default (an all-zeroes address).
6175 		 */
6176 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6177 	}
6178 
6179 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6180 	    B_TRUE, NULL, ipst, ALL_ZONES);
6181 	if (ipif != NULL)
6182 		ipif_refrele(ipif);
6183 	return (error);
6184 }
6185 
6186 /*
6187  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6188  */
6189 /* ARGSUSED */
6190 int
6191 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6192     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6193 {
6194 	ipaddr_t dst_addr;
6195 	ipaddr_t gw_addr;
6196 	ipaddr_t mask;
6197 	int error;
6198 	mblk_t *mp1;
6199 	struct rtentry *rt;
6200 	ipif_t *ipif = NULL;
6201 	ip_stack_t	*ipst;
6202 
6203 	ASSERT(q->q_next == NULL);
6204 	ipst = CONNQ_TO_IPST(q);
6205 
6206 	ip1dbg(("ip_siocdelrt:"));
6207 	/* Existence of mp1 verified in ip_wput_nondata */
6208 	mp1 = mp->b_cont->b_cont;
6209 	rt = (struct rtentry *)mp1->b_rptr;
6210 
6211 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6212 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6213 
6214 	/*
6215 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6216 	 * to a particular host address.  In this case, we set the netmask to
6217 	 * all ones for the particular destination address.  Otherwise,
6218 	 * determine the netmask to be used based on dst_addr and the interfaces
6219 	 * in use.
6220 	 */
6221 	if (rt->rt_flags & RTF_HOST) {
6222 		mask = IP_HOST_MASK;
6223 	} else {
6224 		/*
6225 		 * Note that ip_subnet_mask returns a zero mask in the case of
6226 		 * default (an all-zeroes address).
6227 		 */
6228 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6229 	}
6230 
6231 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6232 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6233 	    ipst, ALL_ZONES);
6234 	if (ipif != NULL)
6235 		ipif_refrele(ipif);
6236 	return (error);
6237 }
6238 
6239 /*
6240  * Enqueue the mp onto the ipsq, chained by b_next.
6241  * b_prev stores the function to be executed later, and b_queue the queue
6242  * where this mp originated.
6243  */
6244 void
6245 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6246     ill_t *pending_ill)
6247 {
6248 	conn_t	*connp;
6249 	ipxop_t *ipx = ipsq->ipsq_xop;
6250 
6251 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6252 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6253 	ASSERT(func != NULL);
6254 
6255 	mp->b_queue = q;
6256 	mp->b_prev = (void *)func;
6257 	mp->b_next = NULL;
6258 
6259 	switch (type) {
6260 	case CUR_OP:
6261 		if (ipx->ipx_mptail != NULL) {
6262 			ASSERT(ipx->ipx_mphead != NULL);
6263 			ipx->ipx_mptail->b_next = mp;
6264 		} else {
6265 			ASSERT(ipx->ipx_mphead == NULL);
6266 			ipx->ipx_mphead = mp;
6267 		}
6268 		ipx->ipx_mptail = mp;
6269 		break;
6270 
6271 	case NEW_OP:
6272 		if (ipsq->ipsq_xopq_mptail != NULL) {
6273 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6274 			ipsq->ipsq_xopq_mptail->b_next = mp;
6275 		} else {
6276 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6277 			ipsq->ipsq_xopq_mphead = mp;
6278 		}
6279 		ipsq->ipsq_xopq_mptail = mp;
6280 		ipx->ipx_ipsq_queued = B_TRUE;
6281 		break;
6282 
6283 	case SWITCH_OP:
6284 		ASSERT(ipsq->ipsq_swxop != NULL);
6285 		/* only one switch operation is currently allowed */
6286 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6287 		ipsq->ipsq_switch_mp = mp;
6288 		ipx->ipx_ipsq_queued = B_TRUE;
6289 		break;
6290 	default:
6291 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6292 	}
6293 
6294 	if (CONN_Q(q) && pending_ill != NULL) {
6295 		connp = Q_TO_CONN(q);
6296 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6297 		connp->conn_oper_pending_ill = pending_ill;
6298 	}
6299 }
6300 
6301 /*
6302  * Dequeue the next message that requested exclusive access to this IPSQ's
6303  * xop.  Specifically:
6304  *
6305  *  1. If we're still processing the current operation on `ipsq', then
6306  *     dequeue the next message for the operation (from ipx_mphead), or
6307  *     return NULL if there are no queued messages for the operation.
6308  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6309  *
6310  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6311  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6312  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6313  *     leaving IPMP groups and require a careful dance -- see the comments
6314  *     in-line below for details.  If we're leaving a group xop or if we're
6315  *     joining a group xop and become writer on it, then we proceed to (3).
6316  *     Otherwise, we return NULL and exit the xop.
6317  *
6318  *  3. For each IPSQ in the xop, return any switch operation stored on
6319  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6320  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6321  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6322  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6323  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6324  *     each phyint in the group, including the IPMP meta-interface phyint.
6325  */
6326 static mblk_t *
6327 ipsq_dq(ipsq_t *ipsq)
6328 {
6329 	ill_t	*illv4, *illv6;
6330 	mblk_t	*mp;
6331 	ipsq_t	*xopipsq;
6332 	ipsq_t	*leftipsq = NULL;
6333 	ipxop_t *ipx;
6334 	phyint_t *phyi = ipsq->ipsq_phyint;
6335 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6336 	boolean_t emptied = B_FALSE;
6337 
6338 	/*
6339 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6340 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6341 	 */
6342 	rw_enter(&ipst->ips_ill_g_lock,
6343 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6344 	mutex_enter(&ipsq->ipsq_lock);
6345 	ipx = ipsq->ipsq_xop;
6346 	mutex_enter(&ipx->ipx_lock);
6347 
6348 	/*
6349 	 * Dequeue the next message associated with the current exclusive
6350 	 * operation, if any.
6351 	 */
6352 	if ((mp = ipx->ipx_mphead) != NULL) {
6353 		ipx->ipx_mphead = mp->b_next;
6354 		if (ipx->ipx_mphead == NULL)
6355 			ipx->ipx_mptail = NULL;
6356 		mp->b_next = (void *)ipsq;
6357 		goto out;
6358 	}
6359 
6360 	if (ipx->ipx_current_ipif != NULL)
6361 		goto empty;
6362 
6363 	if (ipsq->ipsq_swxop != NULL) {
6364 		/*
6365 		 * The exclusive operation that is now being completed has
6366 		 * requested a switch to a different xop.  This happens
6367 		 * when an interface joins or leaves an IPMP group.  Joins
6368 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6369 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6370 		 * (phyint_free()), or interface plumb for an ill type
6371 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6372 		 *
6373 		 * Xop switches are not allowed on the IPMP meta-interface.
6374 		 */
6375 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6376 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6377 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6378 
6379 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6380 			/*
6381 			 * We're switching back to our own xop, so we have two
6382 			 * xop's to drain/exit: our own, and the group xop
6383 			 * that we are leaving.
6384 			 *
6385 			 * First, pull ourselves out of the group ipsq list.
6386 			 * This is safe since we're writer on ill_g_lock.
6387 			 */
6388 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6389 
6390 			xopipsq = ipx->ipx_ipsq;
6391 			while (xopipsq->ipsq_next != ipsq)
6392 				xopipsq = xopipsq->ipsq_next;
6393 
6394 			xopipsq->ipsq_next = ipsq->ipsq_next;
6395 			ipsq->ipsq_next = ipsq;
6396 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6397 			ipsq->ipsq_swxop = NULL;
6398 
6399 			/*
6400 			 * Second, prepare to exit the group xop.  The actual
6401 			 * ipsq_exit() is done at the end of this function
6402 			 * since we cannot hold any locks across ipsq_exit().
6403 			 * Note that although we drop the group's ipx_lock, no
6404 			 * threads can proceed since we're still ipx_writer.
6405 			 */
6406 			leftipsq = xopipsq;
6407 			mutex_exit(&ipx->ipx_lock);
6408 
6409 			/*
6410 			 * Third, set ipx to point to our own xop (which was
6411 			 * inactive and therefore can be entered).
6412 			 */
6413 			ipx = ipsq->ipsq_xop;
6414 			mutex_enter(&ipx->ipx_lock);
6415 			ASSERT(ipx->ipx_writer == NULL);
6416 			ASSERT(ipx->ipx_current_ipif == NULL);
6417 		} else {
6418 			/*
6419 			 * We're switching from our own xop to a group xop.
6420 			 * The requestor of the switch must ensure that the
6421 			 * group xop cannot go away (e.g. by ensuring the
6422 			 * phyint associated with the xop cannot go away).
6423 			 *
6424 			 * If we can become writer on our new xop, then we'll
6425 			 * do the drain.  Otherwise, the current writer of our
6426 			 * new xop will do the drain when it exits.
6427 			 *
6428 			 * First, splice ourselves into the group IPSQ list.
6429 			 * This is safe since we're writer on ill_g_lock.
6430 			 */
6431 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6432 
6433 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6434 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6435 				xopipsq = xopipsq->ipsq_next;
6436 
6437 			xopipsq->ipsq_next = ipsq;
6438 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6439 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6440 			ipsq->ipsq_swxop = NULL;
6441 
6442 			/*
6443 			 * Second, exit our own xop, since it's now unused.
6444 			 * This is safe since we've got the only reference.
6445 			 */
6446 			ASSERT(ipx->ipx_writer == curthread);
6447 			ipx->ipx_writer = NULL;
6448 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6449 			ipx->ipx_ipsq_queued = B_FALSE;
6450 			mutex_exit(&ipx->ipx_lock);
6451 
6452 			/*
6453 			 * Third, set ipx to point to our new xop, and check
6454 			 * if we can become writer on it.  If we cannot, then
6455 			 * the current writer will drain the IPSQ group when
6456 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6457 			 * because we're still holding ipsq_lock.
6458 			 */
6459 			ipx = ipsq->ipsq_xop;
6460 			mutex_enter(&ipx->ipx_lock);
6461 			if (ipx->ipx_writer != NULL ||
6462 			    ipx->ipx_current_ipif != NULL) {
6463 				goto out;
6464 			}
6465 		}
6466 
6467 		/*
6468 		 * Fourth, become writer on our new ipx before we continue
6469 		 * with the drain.  Note that we never dropped ipsq_lock
6470 		 * above, so no other thread could've raced with us to
6471 		 * become writer first.  Also, we're holding ipx_lock, so
6472 		 * no other thread can examine the ipx right now.
6473 		 */
6474 		ASSERT(ipx->ipx_current_ipif == NULL);
6475 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6476 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6477 		ipx->ipx_writer = curthread;
6478 		ipx->ipx_forced = B_FALSE;
6479 #ifdef DEBUG
6480 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6481 #endif
6482 	}
6483 
6484 	xopipsq = ipsq;
6485 	do {
6486 		/*
6487 		 * So that other operations operate on a consistent and
6488 		 * complete phyint, a switch message on an IPSQ must be
6489 		 * handled prior to any other operations on that IPSQ.
6490 		 */
6491 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6492 			xopipsq->ipsq_switch_mp = NULL;
6493 			ASSERT(mp->b_next == NULL);
6494 			mp->b_next = (void *)xopipsq;
6495 			goto out;
6496 		}
6497 
6498 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6499 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6500 			if (xopipsq->ipsq_xopq_mphead == NULL)
6501 				xopipsq->ipsq_xopq_mptail = NULL;
6502 			mp->b_next = (void *)xopipsq;
6503 			goto out;
6504 		}
6505 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6506 empty:
6507 	/*
6508 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6509 	 * new messages can end up on any IPSQ in the xop.
6510 	 */
6511 	ipx->ipx_writer = NULL;
6512 	ipx->ipx_forced = B_FALSE;
6513 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6514 	ipx->ipx_ipsq_queued = B_FALSE;
6515 	emptied = B_TRUE;
6516 #ifdef	DEBUG
6517 	ipx->ipx_depth = 0;
6518 #endif
6519 out:
6520 	mutex_exit(&ipx->ipx_lock);
6521 	mutex_exit(&ipsq->ipsq_lock);
6522 
6523 	/*
6524 	 * If we completely emptied the xop, then wake up any threads waiting
6525 	 * to enter any of the IPSQ's associated with it.
6526 	 */
6527 	if (emptied) {
6528 		xopipsq = ipsq;
6529 		do {
6530 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6531 				continue;
6532 
6533 			illv4 = phyi->phyint_illv4;
6534 			illv6 = phyi->phyint_illv6;
6535 
6536 			GRAB_ILL_LOCKS(illv4, illv6);
6537 			if (illv4 != NULL)
6538 				cv_broadcast(&illv4->ill_cv);
6539 			if (illv6 != NULL)
6540 				cv_broadcast(&illv6->ill_cv);
6541 			RELEASE_ILL_LOCKS(illv4, illv6);
6542 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6543 	}
6544 	rw_exit(&ipst->ips_ill_g_lock);
6545 
6546 	/*
6547 	 * Now that all locks are dropped, exit the IPSQ we left.
6548 	 */
6549 	if (leftipsq != NULL)
6550 		ipsq_exit(leftipsq);
6551 
6552 	return (mp);
6553 }
6554 
6555 /*
6556  * Return completion status of previously initiated DLPI operations on
6557  * ills in the purview of an ipsq.
6558  */
6559 static boolean_t
6560 ipsq_dlpi_done(ipsq_t *ipsq)
6561 {
6562 	ipsq_t		*ipsq_start;
6563 	phyint_t	*phyi;
6564 	ill_t		*ill;
6565 
6566 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6567 	ipsq_start = ipsq;
6568 
6569 	do {
6570 		/*
6571 		 * The only current users of this function are ipsq_try_enter
6572 		 * and ipsq_enter which have made sure that ipsq_writer is
6573 		 * NULL before we reach here. ill_dlpi_pending is modified
6574 		 * only by an ipsq writer
6575 		 */
6576 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6577 		phyi = ipsq->ipsq_phyint;
6578 		/*
6579 		 * phyi could be NULL if a phyint that is part of an
6580 		 * IPMP group is being unplumbed. A more detailed
6581 		 * comment is in ipmp_grp_update_kstats()
6582 		 */
6583 		if (phyi != NULL) {
6584 			ill = phyi->phyint_illv4;
6585 			if (ill != NULL &&
6586 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6587 			    ill->ill_arl_dlpi_pending))
6588 				return (B_FALSE);
6589 
6590 			ill = phyi->phyint_illv6;
6591 			if (ill != NULL &&
6592 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6593 				return (B_FALSE);
6594 		}
6595 
6596 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6597 
6598 	return (B_TRUE);
6599 }
6600 
6601 /*
6602  * Enter the ipsq corresponding to ill, by waiting synchronously till
6603  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6604  * will have to drain completely before ipsq_enter returns success.
6605  * ipx_current_ipif will be set if some exclusive op is in progress,
6606  * and the ipsq_exit logic will start the next enqueued op after
6607  * completion of the current op. If 'force' is used, we don't wait
6608  * for the enqueued ops. This is needed when a conn_close wants to
6609  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6610  * of an ill can also use this option. But we dont' use it currently.
6611  */
6612 #define	ENTER_SQ_WAIT_TICKS 100
6613 boolean_t
6614 ipsq_enter(ill_t *ill, boolean_t force, int type)
6615 {
6616 	ipsq_t	*ipsq;
6617 	ipxop_t *ipx;
6618 	boolean_t waited_enough = B_FALSE;
6619 	ip_stack_t *ipst = ill->ill_ipst;
6620 
6621 	/*
6622 	 * Note that the relationship between ill and ipsq is fixed as long as
6623 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6624 	 * relationship between the IPSQ and xop cannot change.  However,
6625 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6626 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6627 	 * waking up all ills in the xop when it becomes available.
6628 	 */
6629 	for (;;) {
6630 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6631 		mutex_enter(&ill->ill_lock);
6632 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6633 			mutex_exit(&ill->ill_lock);
6634 			rw_exit(&ipst->ips_ill_g_lock);
6635 			return (B_FALSE);
6636 		}
6637 
6638 		ipsq = ill->ill_phyint->phyint_ipsq;
6639 		mutex_enter(&ipsq->ipsq_lock);
6640 		ipx = ipsq->ipsq_xop;
6641 		mutex_enter(&ipx->ipx_lock);
6642 
6643 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6644 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6645 		    waited_enough))
6646 			break;
6647 
6648 		rw_exit(&ipst->ips_ill_g_lock);
6649 
6650 		if (!force || ipx->ipx_writer != NULL) {
6651 			mutex_exit(&ipx->ipx_lock);
6652 			mutex_exit(&ipsq->ipsq_lock);
6653 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6654 		} else {
6655 			mutex_exit(&ipx->ipx_lock);
6656 			mutex_exit(&ipsq->ipsq_lock);
6657 			(void) cv_reltimedwait(&ill->ill_cv,
6658 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6659 			waited_enough = B_TRUE;
6660 		}
6661 		mutex_exit(&ill->ill_lock);
6662 	}
6663 
6664 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6665 	ASSERT(ipx->ipx_reentry_cnt == 0);
6666 	ipx->ipx_writer = curthread;
6667 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6668 	ipx->ipx_reentry_cnt++;
6669 #ifdef DEBUG
6670 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6671 #endif
6672 	mutex_exit(&ipx->ipx_lock);
6673 	mutex_exit(&ipsq->ipsq_lock);
6674 	mutex_exit(&ill->ill_lock);
6675 	rw_exit(&ipst->ips_ill_g_lock);
6676 
6677 	return (B_TRUE);
6678 }
6679 
6680 /*
6681  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6682  * across the call to the core interface ipsq_try_enter() and hence calls this
6683  * function directly. This is explained more fully in ipif_set_values().
6684  * In order to support the above constraint, ipsq_try_enter is implemented as
6685  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6686  */
6687 static ipsq_t *
6688 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6689     int type, boolean_t reentry_ok)
6690 {
6691 	ipsq_t	*ipsq;
6692 	ipxop_t	*ipx;
6693 	ip_stack_t *ipst = ill->ill_ipst;
6694 
6695 	/*
6696 	 * lock ordering:
6697 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6698 	 *
6699 	 * ipx of an ipsq can't change when ipsq_lock is held.
6700 	 */
6701 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6702 	GRAB_CONN_LOCK(q);
6703 	mutex_enter(&ill->ill_lock);
6704 	ipsq = ill->ill_phyint->phyint_ipsq;
6705 	mutex_enter(&ipsq->ipsq_lock);
6706 	ipx = ipsq->ipsq_xop;
6707 	mutex_enter(&ipx->ipx_lock);
6708 
6709 	/*
6710 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6711 	 *    (Note: If the caller does not specify reentry_ok then neither
6712 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6713 	 *    again. Otherwise it can lead to an infinite loop
6714 	 * 2. Enter the ipsq if there is no current writer and this attempted
6715 	 *    entry is part of the current operation
6716 	 * 3. Enter the ipsq if there is no current writer and this is a new
6717 	 *    operation and the operation queue is empty and there is no
6718 	 *    operation currently in progress and if all previously initiated
6719 	 *    DLPI operations have completed.
6720 	 */
6721 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6722 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6723 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6724 	    ipsq_dlpi_done(ipsq))))) {
6725 		/* Success. */
6726 		ipx->ipx_reentry_cnt++;
6727 		ipx->ipx_writer = curthread;
6728 		ipx->ipx_forced = B_FALSE;
6729 		mutex_exit(&ipx->ipx_lock);
6730 		mutex_exit(&ipsq->ipsq_lock);
6731 		mutex_exit(&ill->ill_lock);
6732 		RELEASE_CONN_LOCK(q);
6733 #ifdef DEBUG
6734 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6735 #endif
6736 		return (ipsq);
6737 	}
6738 
6739 	if (func != NULL)
6740 		ipsq_enq(ipsq, q, mp, func, type, ill);
6741 
6742 	mutex_exit(&ipx->ipx_lock);
6743 	mutex_exit(&ipsq->ipsq_lock);
6744 	mutex_exit(&ill->ill_lock);
6745 	RELEASE_CONN_LOCK(q);
6746 	return (NULL);
6747 }
6748 
6749 /*
6750  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6751  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6752  * There is one ipsq per phyint. The ipsq
6753  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6754  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6755  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6756  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6757  * up the interface) and are enqueued in ipx_mphead.
6758  *
6759  * If a thread does not want to reenter the ipsq when it is already writer,
6760  * it must make sure that the specified reentry point to be called later
6761  * when the ipsq is empty, nor any code path starting from the specified reentry
6762  * point must never ever try to enter the ipsq again. Otherwise it can lead
6763  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6764  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6765  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6766  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6767  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6768  * ioctl if the current ioctl has completed. If the current ioctl is still
6769  * in progress it simply returns. The current ioctl could be waiting for
6770  * a response from another module (the driver or could be waiting for
6771  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6772  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6773  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6774  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6775  * all associated DLPI operations have completed.
6776  */
6777 
6778 /*
6779  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6780  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6781  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6782  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6783  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6784  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6785  */
6786 ipsq_t *
6787 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6788     ipsq_func_t func, int type, boolean_t reentry_ok)
6789 {
6790 	ip_stack_t	*ipst;
6791 	ipsq_t		*ipsq;
6792 
6793 	/* Only 1 of ipif or ill can be specified */
6794 	ASSERT((ipif != NULL) ^ (ill != NULL));
6795 
6796 	if (ipif != NULL)
6797 		ill = ipif->ipif_ill;
6798 	ipst = ill->ill_ipst;
6799 
6800 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6801 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6802 	rw_exit(&ipst->ips_ill_g_lock);
6803 
6804 	return (ipsq);
6805 }
6806 
6807 /*
6808  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6809  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6810  * cannot be entered, the mp is queued for completion.
6811  */
6812 void
6813 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6814     boolean_t reentry_ok)
6815 {
6816 	ipsq_t	*ipsq;
6817 
6818 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6819 
6820 	/*
6821 	 * Drop the caller's refhold on the ill.  This is safe since we either
6822 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6823 	 * IPSQ, in which case we return without accessing ill anymore.  This
6824 	 * is needed because func needs to see the correct refcount.
6825 	 * e.g. removeif can work only then.
6826 	 */
6827 	ill_refrele(ill);
6828 	if (ipsq != NULL) {
6829 		(*func)(ipsq, q, mp, NULL);
6830 		ipsq_exit(ipsq);
6831 	}
6832 }
6833 
6834 /*
6835  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6836  * prior to exiting.  Caller must be writer on the specified IPSQ.
6837  */
6838 void
6839 ipsq_exit(ipsq_t *ipsq)
6840 {
6841 	mblk_t *mp;
6842 	ipsq_t *mp_ipsq;
6843 	queue_t	*q;
6844 	phyint_t *phyi;
6845 	ipsq_func_t func;
6846 
6847 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6848 
6849 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6850 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6851 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6852 		return;
6853 	}
6854 
6855 	for (;;) {
6856 		phyi = ipsq->ipsq_phyint;
6857 		mp = ipsq_dq(ipsq);
6858 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6859 
6860 		/*
6861 		 * If we've changed to a new IPSQ, and the phyint associated
6862 		 * with the old one has gone away, free the old IPSQ.  Note
6863 		 * that this cannot happen while the IPSQ is in a group.
6864 		 */
6865 		if (mp_ipsq != ipsq && phyi == NULL) {
6866 			ASSERT(ipsq->ipsq_next == ipsq);
6867 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6868 			ipsq_delete(ipsq);
6869 		}
6870 
6871 		if (mp == NULL)
6872 			break;
6873 
6874 		q = mp->b_queue;
6875 		func = (ipsq_func_t)mp->b_prev;
6876 		ipsq = mp_ipsq;
6877 		mp->b_next = mp->b_prev = NULL;
6878 		mp->b_queue = NULL;
6879 
6880 		/*
6881 		 * If 'q' is an conn queue, it is valid, since we did a
6882 		 * a refhold on the conn at the start of the ioctl.
6883 		 * If 'q' is an ill queue, it is valid, since close of an
6884 		 * ill will clean up its IPSQ.
6885 		 */
6886 		(*func)(ipsq, q, mp, NULL);
6887 	}
6888 }
6889 
6890 /*
6891  * Used to start any igmp or mld timers that could not be started
6892  * while holding ill_mcast_lock. The timers can't be started while holding
6893  * the lock, since mld/igmp_start_timers may need to call untimeout()
6894  * which can't be done while holding the lock which the timeout handler
6895  * acquires. Otherwise
6896  * there could be a deadlock since the timeout handlers
6897  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6898  * ill_mcast_lock.
6899  */
6900 void
6901 ill_mcast_timer_start(ip_stack_t *ipst)
6902 {
6903 	int		next;
6904 
6905 	mutex_enter(&ipst->ips_igmp_timer_lock);
6906 	next = ipst->ips_igmp_deferred_next;
6907 	ipst->ips_igmp_deferred_next = INFINITY;
6908 	mutex_exit(&ipst->ips_igmp_timer_lock);
6909 
6910 	if (next != INFINITY)
6911 		igmp_start_timers(next, ipst);
6912 
6913 	mutex_enter(&ipst->ips_mld_timer_lock);
6914 	next = ipst->ips_mld_deferred_next;
6915 	ipst->ips_mld_deferred_next = INFINITY;
6916 	mutex_exit(&ipst->ips_mld_timer_lock);
6917 
6918 	if (next != INFINITY)
6919 		mld_start_timers(next, ipst);
6920 }
6921 
6922 /*
6923  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6924  * and `ioccmd'.
6925  */
6926 void
6927 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6928 {
6929 	ill_t *ill = ipif->ipif_ill;
6930 	ipxop_t *ipx = ipsq->ipsq_xop;
6931 
6932 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6933 	ASSERT(ipx->ipx_current_ipif == NULL);
6934 	ASSERT(ipx->ipx_current_ioctl == 0);
6935 
6936 	ipx->ipx_current_done = B_FALSE;
6937 	ipx->ipx_current_ioctl = ioccmd;
6938 	mutex_enter(&ipx->ipx_lock);
6939 	ipx->ipx_current_ipif = ipif;
6940 	mutex_exit(&ipx->ipx_lock);
6941 
6942 	/*
6943 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6944 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6945 	 * references to the ipif (so that the references will eventually
6946 	 * drop to zero) and also prevents any "get" operations (e.g.,
6947 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6948 	 * operation has completed and the ipif is again in a stable state.
6949 	 *
6950 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6951 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6952 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6953 	 * ipifs will be affected.
6954 	 *
6955 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6956 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6957 	 * operate on.
6958 	 */
6959 	switch (ioccmd) {
6960 	case SIOCLIFREMOVEIF:
6961 		break;
6962 	case 0:
6963 		mutex_enter(&ill->ill_lock);
6964 		ipif = ipif->ipif_ill->ill_ipif;
6965 		for (; ipif != NULL; ipif = ipif->ipif_next)
6966 			ipif->ipif_state_flags |= IPIF_CHANGING;
6967 		mutex_exit(&ill->ill_lock);
6968 		break;
6969 	default:
6970 		mutex_enter(&ill->ill_lock);
6971 		ipif->ipif_state_flags |= IPIF_CHANGING;
6972 		mutex_exit(&ill->ill_lock);
6973 	}
6974 }
6975 
6976 /*
6977  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6978  * the next exclusive operation to begin once we ipsq_exit().  However, if
6979  * pending DLPI operations remain, then we will wait for the queue to drain
6980  * before allowing the next exclusive operation to begin.  This ensures that
6981  * DLPI operations from one exclusive operation are never improperly processed
6982  * as part of a subsequent exclusive operation.
6983  */
6984 void
6985 ipsq_current_finish(ipsq_t *ipsq)
6986 {
6987 	ipxop_t	*ipx = ipsq->ipsq_xop;
6988 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6989 	ipif_t	*ipif = ipx->ipx_current_ipif;
6990 
6991 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6992 
6993 	/*
6994 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6995 	 * (but in that case, IPIF_CHANGING will already be clear and no
6996 	 * pending DLPI messages can remain).
6997 	 */
6998 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6999 		ill_t *ill = ipif->ipif_ill;
7000 
7001 		mutex_enter(&ill->ill_lock);
7002 		dlpi_pending = ill->ill_dlpi_pending;
7003 		if (ipx->ipx_current_ioctl == 0) {
7004 			ipif = ill->ill_ipif;
7005 			for (; ipif != NULL; ipif = ipif->ipif_next)
7006 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7007 		} else {
7008 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7009 		}
7010 		mutex_exit(&ill->ill_lock);
7011 	}
7012 
7013 	ASSERT(!ipx->ipx_current_done);
7014 	ipx->ipx_current_done = B_TRUE;
7015 	ipx->ipx_current_ioctl = 0;
7016 	if (dlpi_pending == DL_PRIM_INVAL) {
7017 		mutex_enter(&ipx->ipx_lock);
7018 		ipx->ipx_current_ipif = NULL;
7019 		mutex_exit(&ipx->ipx_lock);
7020 	}
7021 }
7022 
7023 /*
7024  * The ill is closing. Flush all messages on the ipsq that originated
7025  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7026  * for this ill since ipsq_enter could not have entered until then.
7027  * New messages can't be queued since the CONDEMNED flag is set.
7028  */
7029 static void
7030 ipsq_flush(ill_t *ill)
7031 {
7032 	queue_t	*q;
7033 	mblk_t	*prev;
7034 	mblk_t	*mp;
7035 	mblk_t	*mp_next;
7036 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7037 
7038 	ASSERT(IAM_WRITER_ILL(ill));
7039 
7040 	/*
7041 	 * Flush any messages sent up by the driver.
7042 	 */
7043 	mutex_enter(&ipx->ipx_lock);
7044 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7045 		mp_next = mp->b_next;
7046 		q = mp->b_queue;
7047 		if (q == ill->ill_rq || q == ill->ill_wq) {
7048 			/* dequeue mp */
7049 			if (prev == NULL)
7050 				ipx->ipx_mphead = mp->b_next;
7051 			else
7052 				prev->b_next = mp->b_next;
7053 			if (ipx->ipx_mptail == mp) {
7054 				ASSERT(mp_next == NULL);
7055 				ipx->ipx_mptail = prev;
7056 			}
7057 			inet_freemsg(mp);
7058 		} else {
7059 			prev = mp;
7060 		}
7061 	}
7062 	mutex_exit(&ipx->ipx_lock);
7063 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7064 	ipsq_xopq_mp_cleanup(ill, NULL);
7065 }
7066 
7067 /*
7068  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7069  * and return the associated ipif.
7070  * Return value:
7071  *	Non zero: An error has occurred. ci may not be filled out.
7072  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7073  *	a held ipif in ci.ci_ipif.
7074  */
7075 int
7076 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7077     cmd_info_t *ci)
7078 {
7079 	char		*name;
7080 	struct ifreq    *ifr;
7081 	struct lifreq    *lifr;
7082 	ipif_t		*ipif = NULL;
7083 	ill_t		*ill;
7084 	conn_t		*connp;
7085 	boolean_t	isv6;
7086 	boolean_t	exists;
7087 	mblk_t		*mp1;
7088 	zoneid_t	zoneid;
7089 	ip_stack_t	*ipst;
7090 
7091 	if (q->q_next != NULL) {
7092 		ill = (ill_t *)q->q_ptr;
7093 		isv6 = ill->ill_isv6;
7094 		connp = NULL;
7095 		zoneid = ALL_ZONES;
7096 		ipst = ill->ill_ipst;
7097 	} else {
7098 		ill = NULL;
7099 		connp = Q_TO_CONN(q);
7100 		isv6 = (connp->conn_family == AF_INET6);
7101 		zoneid = connp->conn_zoneid;
7102 		if (zoneid == GLOBAL_ZONEID) {
7103 			/* global zone can access ipifs in all zones */
7104 			zoneid = ALL_ZONES;
7105 		}
7106 		ipst = connp->conn_netstack->netstack_ip;
7107 	}
7108 
7109 	/* Has been checked in ip_wput_nondata */
7110 	mp1 = mp->b_cont->b_cont;
7111 
7112 	if (ipip->ipi_cmd_type == IF_CMD) {
7113 		/* This a old style SIOC[GS]IF* command */
7114 		ifr = (struct ifreq *)mp1->b_rptr;
7115 		/*
7116 		 * Null terminate the string to protect against buffer
7117 		 * overrun. String was generated by user code and may not
7118 		 * be trusted.
7119 		 */
7120 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7121 		name = ifr->ifr_name;
7122 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7123 		ci->ci_sin6 = NULL;
7124 		ci->ci_lifr = (struct lifreq *)ifr;
7125 	} else {
7126 		/* This a new style SIOC[GS]LIF* command */
7127 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7128 		lifr = (struct lifreq *)mp1->b_rptr;
7129 		/*
7130 		 * Null terminate the string to protect against buffer
7131 		 * overrun. String was generated by user code and may not
7132 		 * be trusted.
7133 		 */
7134 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7135 		name = lifr->lifr_name;
7136 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7137 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7138 		ci->ci_lifr = lifr;
7139 	}
7140 
7141 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7142 		/*
7143 		 * The ioctl will be failed if the ioctl comes down
7144 		 * an conn stream
7145 		 */
7146 		if (ill == NULL) {
7147 			/*
7148 			 * Not an ill queue, return EINVAL same as the
7149 			 * old error code.
7150 			 */
7151 			return (ENXIO);
7152 		}
7153 		ipif = ill->ill_ipif;
7154 		ipif_refhold(ipif);
7155 	} else {
7156 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7157 		    &exists, isv6, zoneid, ipst);
7158 
7159 		/*
7160 		 * Ensure that get ioctls don't see any internal state changes
7161 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7162 		 * set.
7163 		 */
7164 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7165 		    !IAM_WRITER_IPIF(ipif)) {
7166 			ipsq_t	*ipsq;
7167 
7168 			if (connp != NULL)
7169 				mutex_enter(&connp->conn_lock);
7170 			mutex_enter(&ipif->ipif_ill->ill_lock);
7171 			if (IPIF_IS_CHANGING(ipif) &&
7172 			    !IPIF_IS_CONDEMNED(ipif)) {
7173 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7174 				mutex_enter(&ipsq->ipsq_lock);
7175 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7176 				mutex_exit(&ipif->ipif_ill->ill_lock);
7177 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7178 				    NEW_OP, ipif->ipif_ill);
7179 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7180 				mutex_exit(&ipsq->ipsq_lock);
7181 				if (connp != NULL)
7182 					mutex_exit(&connp->conn_lock);
7183 				ipif_refrele(ipif);
7184 				return (EINPROGRESS);
7185 			}
7186 			mutex_exit(&ipif->ipif_ill->ill_lock);
7187 			if (connp != NULL)
7188 				mutex_exit(&connp->conn_lock);
7189 		}
7190 	}
7191 
7192 	/*
7193 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7194 	 */
7195 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7196 		ipif_refrele(ipif);
7197 		return (ENXIO);
7198 	}
7199 
7200 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7201 	    name[0] == '\0') {
7202 		/*
7203 		 * Handle a or a SIOC?IF* with a null name
7204 		 * during plumb (on the ill queue before the I_PLINK).
7205 		 */
7206 		ipif = ill->ill_ipif;
7207 		ipif_refhold(ipif);
7208 	}
7209 
7210 	if (ipif == NULL)
7211 		return (ENXIO);
7212 
7213 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7214 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7215 
7216 	ci->ci_ipif = ipif;
7217 	return (0);
7218 }
7219 
7220 /*
7221  * Return the total number of ipifs.
7222  */
7223 static uint_t
7224 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7225 {
7226 	uint_t numifs = 0;
7227 	ill_t	*ill;
7228 	ill_walk_context_t	ctx;
7229 	ipif_t	*ipif;
7230 
7231 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7232 	ill = ILL_START_WALK_V4(&ctx, ipst);
7233 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7234 		if (IS_UNDER_IPMP(ill))
7235 			continue;
7236 		for (ipif = ill->ill_ipif; ipif != NULL;
7237 		    ipif = ipif->ipif_next) {
7238 			if (ipif->ipif_zoneid == zoneid ||
7239 			    ipif->ipif_zoneid == ALL_ZONES)
7240 				numifs++;
7241 		}
7242 	}
7243 	rw_exit(&ipst->ips_ill_g_lock);
7244 	return (numifs);
7245 }
7246 
7247 /*
7248  * Return the total number of ipifs.
7249  */
7250 static uint_t
7251 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7252 {
7253 	uint_t numifs = 0;
7254 	ill_t	*ill;
7255 	ipif_t	*ipif;
7256 	ill_walk_context_t	ctx;
7257 
7258 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7259 
7260 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7261 	if (family == AF_INET)
7262 		ill = ILL_START_WALK_V4(&ctx, ipst);
7263 	else if (family == AF_INET6)
7264 		ill = ILL_START_WALK_V6(&ctx, ipst);
7265 	else
7266 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7267 
7268 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7269 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7270 			continue;
7271 
7272 		for (ipif = ill->ill_ipif; ipif != NULL;
7273 		    ipif = ipif->ipif_next) {
7274 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7275 			    !(lifn_flags & LIFC_NOXMIT))
7276 				continue;
7277 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7278 			    !(lifn_flags & LIFC_TEMPORARY))
7279 				continue;
7280 			if (((ipif->ipif_flags &
7281 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7282 			    IPIF_DEPRECATED)) ||
7283 			    IS_LOOPBACK(ill) ||
7284 			    !(ipif->ipif_flags & IPIF_UP)) &&
7285 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7286 				continue;
7287 
7288 			if (zoneid != ipif->ipif_zoneid &&
7289 			    ipif->ipif_zoneid != ALL_ZONES &&
7290 			    (zoneid != GLOBAL_ZONEID ||
7291 			    !(lifn_flags & LIFC_ALLZONES)))
7292 				continue;
7293 
7294 			numifs++;
7295 		}
7296 	}
7297 	rw_exit(&ipst->ips_ill_g_lock);
7298 	return (numifs);
7299 }
7300 
7301 uint_t
7302 ip_get_lifsrcofnum(ill_t *ill)
7303 {
7304 	uint_t numifs = 0;
7305 	ill_t	*ill_head = ill;
7306 	ip_stack_t	*ipst = ill->ill_ipst;
7307 
7308 	/*
7309 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7310 	 * other thread may be trying to relink the ILLs in this usesrc group
7311 	 * and adjusting the ill_usesrc_grp_next pointers
7312 	 */
7313 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7314 	if ((ill->ill_usesrc_ifindex == 0) &&
7315 	    (ill->ill_usesrc_grp_next != NULL)) {
7316 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7317 		    ill = ill->ill_usesrc_grp_next)
7318 			numifs++;
7319 	}
7320 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7321 
7322 	return (numifs);
7323 }
7324 
7325 /* Null values are passed in for ipif, sin, and ifreq */
7326 /* ARGSUSED */
7327 int
7328 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7329     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7330 {
7331 	int *nump;
7332 	conn_t *connp = Q_TO_CONN(q);
7333 
7334 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7335 
7336 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7337 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7338 
7339 	*nump = ip_get_numifs(connp->conn_zoneid,
7340 	    connp->conn_netstack->netstack_ip);
7341 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7342 	return (0);
7343 }
7344 
7345 /* Null values are passed in for ipif, sin, and ifreq */
7346 /* ARGSUSED */
7347 int
7348 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7349     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7350 {
7351 	struct lifnum *lifn;
7352 	mblk_t	*mp1;
7353 	conn_t *connp = Q_TO_CONN(q);
7354 
7355 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7356 
7357 	/* Existence checked in ip_wput_nondata */
7358 	mp1 = mp->b_cont->b_cont;
7359 
7360 	lifn = (struct lifnum *)mp1->b_rptr;
7361 	switch (lifn->lifn_family) {
7362 	case AF_UNSPEC:
7363 	case AF_INET:
7364 	case AF_INET6:
7365 		break;
7366 	default:
7367 		return (EAFNOSUPPORT);
7368 	}
7369 
7370 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7371 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7372 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7373 	return (0);
7374 }
7375 
7376 /* ARGSUSED */
7377 int
7378 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7379     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7380 {
7381 	STRUCT_HANDLE(ifconf, ifc);
7382 	mblk_t *mp1;
7383 	struct iocblk *iocp;
7384 	struct ifreq *ifr;
7385 	ill_walk_context_t	ctx;
7386 	ill_t	*ill;
7387 	ipif_t	*ipif;
7388 	struct sockaddr_in *sin;
7389 	int32_t	ifclen;
7390 	zoneid_t zoneid;
7391 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7392 
7393 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7394 
7395 	ip1dbg(("ip_sioctl_get_ifconf"));
7396 	/* Existence verified in ip_wput_nondata */
7397 	mp1 = mp->b_cont->b_cont;
7398 	iocp = (struct iocblk *)mp->b_rptr;
7399 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7400 
7401 	/*
7402 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7403 	 * the user buffer address and length into which the list of struct
7404 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7405 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7406 	 * the SIOCGIFCONF operation was redefined to simply provide
7407 	 * a large output buffer into which we are supposed to jam the ifreq
7408 	 * array.  The same ioctl command code was used, despite the fact that
7409 	 * both the applications and the kernel code had to change, thus making
7410 	 * it impossible to support both interfaces.
7411 	 *
7412 	 * For reasons not good enough to try to explain, the following
7413 	 * algorithm is used for deciding what to do with one of these:
7414 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7415 	 * form with the output buffer coming down as the continuation message.
7416 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7417 	 * and we have to copy in the ifconf structure to find out how big the
7418 	 * output buffer is and where to copy out to.  Sure no problem...
7419 	 *
7420 	 */
7421 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7422 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7423 		int numifs = 0;
7424 		size_t ifc_bufsize;
7425 
7426 		/*
7427 		 * Must be (better be!) continuation of a TRANSPARENT
7428 		 * IOCTL.  We just copied in the ifconf structure.
7429 		 */
7430 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7431 		    (struct ifconf *)mp1->b_rptr);
7432 
7433 		/*
7434 		 * Allocate a buffer to hold requested information.
7435 		 *
7436 		 * If ifc_len is larger than what is needed, we only
7437 		 * allocate what we will use.
7438 		 *
7439 		 * If ifc_len is smaller than what is needed, return
7440 		 * EINVAL.
7441 		 *
7442 		 * XXX: the ill_t structure can hava 2 counters, for
7443 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7444 		 * number of interfaces for a device, so we don't need
7445 		 * to count them here...
7446 		 */
7447 		numifs = ip_get_numifs(zoneid, ipst);
7448 
7449 		ifclen = STRUCT_FGET(ifc, ifc_len);
7450 		ifc_bufsize = numifs * sizeof (struct ifreq);
7451 		if (ifc_bufsize > ifclen) {
7452 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7453 				/* old behaviour */
7454 				return (EINVAL);
7455 			} else {
7456 				ifc_bufsize = ifclen;
7457 			}
7458 		}
7459 
7460 		mp1 = mi_copyout_alloc(q, mp,
7461 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7462 		if (mp1 == NULL)
7463 			return (ENOMEM);
7464 
7465 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7466 	}
7467 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7468 	/*
7469 	 * the SIOCGIFCONF ioctl only knows about
7470 	 * IPv4 addresses, so don't try to tell
7471 	 * it about interfaces with IPv6-only
7472 	 * addresses. (Last parm 'isv6' is B_FALSE)
7473 	 */
7474 
7475 	ifr = (struct ifreq *)mp1->b_rptr;
7476 
7477 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7478 	ill = ILL_START_WALK_V4(&ctx, ipst);
7479 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7480 		if (IS_UNDER_IPMP(ill))
7481 			continue;
7482 		for (ipif = ill->ill_ipif; ipif != NULL;
7483 		    ipif = ipif->ipif_next) {
7484 			if (zoneid != ipif->ipif_zoneid &&
7485 			    ipif->ipif_zoneid != ALL_ZONES)
7486 				continue;
7487 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7488 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7489 					/* old behaviour */
7490 					rw_exit(&ipst->ips_ill_g_lock);
7491 					return (EINVAL);
7492 				} else {
7493 					goto if_copydone;
7494 				}
7495 			}
7496 			ipif_get_name(ipif, ifr->ifr_name,
7497 			    sizeof (ifr->ifr_name));
7498 			sin = (sin_t *)&ifr->ifr_addr;
7499 			*sin = sin_null;
7500 			sin->sin_family = AF_INET;
7501 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7502 			ifr++;
7503 		}
7504 	}
7505 if_copydone:
7506 	rw_exit(&ipst->ips_ill_g_lock);
7507 	mp1->b_wptr = (uchar_t *)ifr;
7508 
7509 	if (STRUCT_BUF(ifc) != NULL) {
7510 		STRUCT_FSET(ifc, ifc_len,
7511 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7512 	}
7513 	return (0);
7514 }
7515 
7516 /*
7517  * Get the interfaces using the address hosted on the interface passed in,
7518  * as a source adddress
7519  */
7520 /* ARGSUSED */
7521 int
7522 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7523     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7524 {
7525 	mblk_t *mp1;
7526 	ill_t	*ill, *ill_head;
7527 	ipif_t	*ipif, *orig_ipif;
7528 	int	numlifs = 0;
7529 	size_t	lifs_bufsize, lifsmaxlen;
7530 	struct	lifreq *lifr;
7531 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7532 	uint_t	ifindex;
7533 	zoneid_t zoneid;
7534 	boolean_t isv6 = B_FALSE;
7535 	struct	sockaddr_in	*sin;
7536 	struct	sockaddr_in6	*sin6;
7537 	STRUCT_HANDLE(lifsrcof, lifs);
7538 	ip_stack_t		*ipst;
7539 
7540 	ipst = CONNQ_TO_IPST(q);
7541 
7542 	ASSERT(q->q_next == NULL);
7543 
7544 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7545 
7546 	/* Existence verified in ip_wput_nondata */
7547 	mp1 = mp->b_cont->b_cont;
7548 
7549 	/*
7550 	 * Must be (better be!) continuation of a TRANSPARENT
7551 	 * IOCTL.  We just copied in the lifsrcof structure.
7552 	 */
7553 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7554 	    (struct lifsrcof *)mp1->b_rptr);
7555 
7556 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7557 		return (EINVAL);
7558 
7559 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7560 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7561 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7562 	if (ipif == NULL) {
7563 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7564 		    ifindex));
7565 		return (ENXIO);
7566 	}
7567 
7568 	/* Allocate a buffer to hold requested information */
7569 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7570 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7571 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7572 	/* The actual size needed is always returned in lifs_len */
7573 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7574 
7575 	/* If the amount we need is more than what is passed in, abort */
7576 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7577 		ipif_refrele(ipif);
7578 		return (0);
7579 	}
7580 
7581 	mp1 = mi_copyout_alloc(q, mp,
7582 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7583 	if (mp1 == NULL) {
7584 		ipif_refrele(ipif);
7585 		return (ENOMEM);
7586 	}
7587 
7588 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7589 	bzero(mp1->b_rptr, lifs_bufsize);
7590 
7591 	lifr = (struct lifreq *)mp1->b_rptr;
7592 
7593 	ill = ill_head = ipif->ipif_ill;
7594 	orig_ipif = ipif;
7595 
7596 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7597 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7598 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7599 
7600 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7601 	for (; (ill != NULL) && (ill != ill_head);
7602 	    ill = ill->ill_usesrc_grp_next) {
7603 
7604 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7605 			break;
7606 
7607 		ipif = ill->ill_ipif;
7608 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7609 		if (ipif->ipif_isv6) {
7610 			sin6 = (sin6_t *)&lifr->lifr_addr;
7611 			*sin6 = sin6_null;
7612 			sin6->sin6_family = AF_INET6;
7613 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7614 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7615 			    &ipif->ipif_v6net_mask);
7616 		} else {
7617 			sin = (sin_t *)&lifr->lifr_addr;
7618 			*sin = sin_null;
7619 			sin->sin_family = AF_INET;
7620 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7621 			lifr->lifr_addrlen = ip_mask_to_plen(
7622 			    ipif->ipif_net_mask);
7623 		}
7624 		lifr++;
7625 	}
7626 	rw_exit(&ipst->ips_ill_g_lock);
7627 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7628 	ipif_refrele(orig_ipif);
7629 	mp1->b_wptr = (uchar_t *)lifr;
7630 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7631 
7632 	return (0);
7633 }
7634 
7635 /* ARGSUSED */
7636 int
7637 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7638     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7639 {
7640 	mblk_t *mp1;
7641 	int	list;
7642 	ill_t	*ill;
7643 	ipif_t	*ipif;
7644 	int	flags;
7645 	int	numlifs = 0;
7646 	size_t	lifc_bufsize;
7647 	struct	lifreq *lifr;
7648 	sa_family_t	family;
7649 	struct	sockaddr_in	*sin;
7650 	struct	sockaddr_in6	*sin6;
7651 	ill_walk_context_t	ctx;
7652 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7653 	int32_t	lifclen;
7654 	zoneid_t zoneid;
7655 	STRUCT_HANDLE(lifconf, lifc);
7656 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7657 
7658 	ip1dbg(("ip_sioctl_get_lifconf"));
7659 
7660 	ASSERT(q->q_next == NULL);
7661 
7662 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7663 
7664 	/* Existence verified in ip_wput_nondata */
7665 	mp1 = mp->b_cont->b_cont;
7666 
7667 	/*
7668 	 * An extended version of SIOCGIFCONF that takes an
7669 	 * additional address family and flags field.
7670 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7671 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7672 	 * interfaces are omitted.
7673 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7674 	 * unless LIFC_TEMPORARY is specified.
7675 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7676 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7677 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7678 	 * has priority over LIFC_NOXMIT.
7679 	 */
7680 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7681 
7682 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7683 		return (EINVAL);
7684 
7685 	/*
7686 	 * Must be (better be!) continuation of a TRANSPARENT
7687 	 * IOCTL.  We just copied in the lifconf structure.
7688 	 */
7689 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7690 
7691 	family = STRUCT_FGET(lifc, lifc_family);
7692 	flags = STRUCT_FGET(lifc, lifc_flags);
7693 
7694 	switch (family) {
7695 	case AF_UNSPEC:
7696 		/*
7697 		 * walk all ILL's.
7698 		 */
7699 		list = MAX_G_HEADS;
7700 		break;
7701 	case AF_INET:
7702 		/*
7703 		 * walk only IPV4 ILL's.
7704 		 */
7705 		list = IP_V4_G_HEAD;
7706 		break;
7707 	case AF_INET6:
7708 		/*
7709 		 * walk only IPV6 ILL's.
7710 		 */
7711 		list = IP_V6_G_HEAD;
7712 		break;
7713 	default:
7714 		return (EAFNOSUPPORT);
7715 	}
7716 
7717 	/*
7718 	 * Allocate a buffer to hold requested information.
7719 	 *
7720 	 * If lifc_len is larger than what is needed, we only
7721 	 * allocate what we will use.
7722 	 *
7723 	 * If lifc_len is smaller than what is needed, return
7724 	 * EINVAL.
7725 	 */
7726 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7727 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7728 	lifclen = STRUCT_FGET(lifc, lifc_len);
7729 	if (lifc_bufsize > lifclen) {
7730 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7731 			return (EINVAL);
7732 		else
7733 			lifc_bufsize = lifclen;
7734 	}
7735 
7736 	mp1 = mi_copyout_alloc(q, mp,
7737 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7738 	if (mp1 == NULL)
7739 		return (ENOMEM);
7740 
7741 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7742 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7743 
7744 	lifr = (struct lifreq *)mp1->b_rptr;
7745 
7746 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7747 	ill = ill_first(list, list, &ctx, ipst);
7748 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7749 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7750 			continue;
7751 
7752 		for (ipif = ill->ill_ipif; ipif != NULL;
7753 		    ipif = ipif->ipif_next) {
7754 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7755 			    !(flags & LIFC_NOXMIT))
7756 				continue;
7757 
7758 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7759 			    !(flags & LIFC_TEMPORARY))
7760 				continue;
7761 
7762 			if (((ipif->ipif_flags &
7763 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7764 			    IPIF_DEPRECATED)) ||
7765 			    IS_LOOPBACK(ill) ||
7766 			    !(ipif->ipif_flags & IPIF_UP)) &&
7767 			    (flags & LIFC_EXTERNAL_SOURCE))
7768 				continue;
7769 
7770 			if (zoneid != ipif->ipif_zoneid &&
7771 			    ipif->ipif_zoneid != ALL_ZONES &&
7772 			    (zoneid != GLOBAL_ZONEID ||
7773 			    !(flags & LIFC_ALLZONES)))
7774 				continue;
7775 
7776 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7777 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7778 					rw_exit(&ipst->ips_ill_g_lock);
7779 					return (EINVAL);
7780 				} else {
7781 					goto lif_copydone;
7782 				}
7783 			}
7784 
7785 			ipif_get_name(ipif, lifr->lifr_name,
7786 			    sizeof (lifr->lifr_name));
7787 			lifr->lifr_type = ill->ill_type;
7788 			if (ipif->ipif_isv6) {
7789 				sin6 = (sin6_t *)&lifr->lifr_addr;
7790 				*sin6 = sin6_null;
7791 				sin6->sin6_family = AF_INET6;
7792 				sin6->sin6_addr =
7793 				    ipif->ipif_v6lcl_addr;
7794 				lifr->lifr_addrlen =
7795 				    ip_mask_to_plen_v6(
7796 				    &ipif->ipif_v6net_mask);
7797 			} else {
7798 				sin = (sin_t *)&lifr->lifr_addr;
7799 				*sin = sin_null;
7800 				sin->sin_family = AF_INET;
7801 				sin->sin_addr.s_addr =
7802 				    ipif->ipif_lcl_addr;
7803 				lifr->lifr_addrlen =
7804 				    ip_mask_to_plen(
7805 				    ipif->ipif_net_mask);
7806 			}
7807 			lifr++;
7808 		}
7809 	}
7810 lif_copydone:
7811 	rw_exit(&ipst->ips_ill_g_lock);
7812 
7813 	mp1->b_wptr = (uchar_t *)lifr;
7814 	if (STRUCT_BUF(lifc) != NULL) {
7815 		STRUCT_FSET(lifc, lifc_len,
7816 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7817 	}
7818 	return (0);
7819 }
7820 
7821 static void
7822 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7823 {
7824 	ip6_asp_t *table;
7825 	size_t table_size;
7826 	mblk_t *data_mp;
7827 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7828 	ip_stack_t	*ipst;
7829 
7830 	if (q->q_next == NULL)
7831 		ipst = CONNQ_TO_IPST(q);
7832 	else
7833 		ipst = ILLQ_TO_IPST(q);
7834 
7835 	/* These two ioctls are I_STR only */
7836 	if (iocp->ioc_count == TRANSPARENT) {
7837 		miocnak(q, mp, 0, EINVAL);
7838 		return;
7839 	}
7840 
7841 	data_mp = mp->b_cont;
7842 	if (data_mp == NULL) {
7843 		/* The user passed us a NULL argument */
7844 		table = NULL;
7845 		table_size = iocp->ioc_count;
7846 	} else {
7847 		/*
7848 		 * The user provided a table.  The stream head
7849 		 * may have copied in the user data in chunks,
7850 		 * so make sure everything is pulled up
7851 		 * properly.
7852 		 */
7853 		if (MBLKL(data_mp) < iocp->ioc_count) {
7854 			mblk_t *new_data_mp;
7855 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7856 			    NULL) {
7857 				miocnak(q, mp, 0, ENOMEM);
7858 				return;
7859 			}
7860 			freemsg(data_mp);
7861 			data_mp = new_data_mp;
7862 			mp->b_cont = data_mp;
7863 		}
7864 		table = (ip6_asp_t *)data_mp->b_rptr;
7865 		table_size = iocp->ioc_count;
7866 	}
7867 
7868 	switch (iocp->ioc_cmd) {
7869 	case SIOCGIP6ADDRPOLICY:
7870 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7871 		if (iocp->ioc_rval == -1)
7872 			iocp->ioc_error = EINVAL;
7873 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7874 		else if (table != NULL &&
7875 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7876 			ip6_asp_t *src = table;
7877 			ip6_asp32_t *dst = (void *)table;
7878 			int count = table_size / sizeof (ip6_asp_t);
7879 			int i;
7880 
7881 			/*
7882 			 * We need to do an in-place shrink of the array
7883 			 * to match the alignment attributes of the
7884 			 * 32-bit ABI looking at it.
7885 			 */
7886 			/* LINTED: logical expression always true: op "||" */
7887 			ASSERT(sizeof (*src) > sizeof (*dst));
7888 			for (i = 1; i < count; i++)
7889 				bcopy(src + i, dst + i, sizeof (*dst));
7890 		}
7891 #endif
7892 		break;
7893 
7894 	case SIOCSIP6ADDRPOLICY:
7895 		ASSERT(mp->b_prev == NULL);
7896 		mp->b_prev = (void *)q;
7897 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7898 		/*
7899 		 * We pass in the datamodel here so that the ip6_asp_replace()
7900 		 * routine can handle converting from 32-bit to native formats
7901 		 * where necessary.
7902 		 *
7903 		 * A better way to handle this might be to convert the inbound
7904 		 * data structure here, and hang it off a new 'mp'; thus the
7905 		 * ip6_asp_replace() logic would always be dealing with native
7906 		 * format data structures..
7907 		 *
7908 		 * (An even simpler way to handle these ioctls is to just
7909 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7910 		 * and just recompile everything that depends on it.)
7911 		 */
7912 #endif
7913 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7914 		    iocp->ioc_flag & IOC_MODELS);
7915 		return;
7916 	}
7917 
7918 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7919 	qreply(q, mp);
7920 }
7921 
7922 static void
7923 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7924 {
7925 	mblk_t 		*data_mp;
7926 	struct dstinforeq	*dir;
7927 	uint8_t		*end, *cur;
7928 	in6_addr_t	*daddr, *saddr;
7929 	ipaddr_t	v4daddr;
7930 	ire_t		*ire;
7931 	ipaddr_t	v4setsrc;
7932 	in6_addr_t	v6setsrc;
7933 	char		*slabel, *dlabel;
7934 	boolean_t	isipv4;
7935 	int		match_ire;
7936 	ill_t		*dst_ill;
7937 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7938 	conn_t		*connp = Q_TO_CONN(q);
7939 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7940 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7941 	uint64_t	ipif_flags;
7942 
7943 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7944 
7945 	/*
7946 	 * This ioctl is I_STR only, and must have a
7947 	 * data mblk following the M_IOCTL mblk.
7948 	 */
7949 	data_mp = mp->b_cont;
7950 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7951 		miocnak(q, mp, 0, EINVAL);
7952 		return;
7953 	}
7954 
7955 	if (MBLKL(data_mp) < iocp->ioc_count) {
7956 		mblk_t *new_data_mp;
7957 
7958 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7959 			miocnak(q, mp, 0, ENOMEM);
7960 			return;
7961 		}
7962 		freemsg(data_mp);
7963 		data_mp = new_data_mp;
7964 		mp->b_cont = data_mp;
7965 	}
7966 	match_ire = MATCH_IRE_DSTONLY;
7967 
7968 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7969 	    end - cur >= sizeof (struct dstinforeq);
7970 	    cur += sizeof (struct dstinforeq)) {
7971 		dir = (struct dstinforeq *)cur;
7972 		daddr = &dir->dir_daddr;
7973 		saddr = &dir->dir_saddr;
7974 
7975 		/*
7976 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7977 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7978 		 * and ip_select_source_v6() do not.
7979 		 */
7980 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7981 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7982 
7983 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7984 		if (isipv4) {
7985 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7986 			v4setsrc = INADDR_ANY;
7987 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7988 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7989 			    NULL, NULL);
7990 		} else {
7991 			v6setsrc = ipv6_all_zeros;
7992 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7993 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7994 			    NULL, NULL);
7995 		}
7996 		ASSERT(ire != NULL);
7997 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7998 			ire_refrele(ire);
7999 			dir->dir_dreachable = 0;
8000 
8001 			/* move on to next dst addr */
8002 			continue;
8003 		}
8004 		dir->dir_dreachable = 1;
8005 
8006 		dst_ill = ire_nexthop_ill(ire);
8007 		if (dst_ill == NULL) {
8008 			ire_refrele(ire);
8009 			continue;
8010 		}
8011 
8012 		/* With ipmp we most likely look at the ipmp ill here */
8013 		dir->dir_dmactype = dst_ill->ill_mactype;
8014 
8015 		if (isipv4) {
8016 			ipaddr_t v4saddr;
8017 
8018 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
8019 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
8020 			    &v4saddr, NULL, &ipif_flags) != 0) {
8021 				v4saddr = INADDR_ANY;
8022 				ipif_flags = 0;
8023 			}
8024 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
8025 		} else {
8026 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
8027 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8028 			    saddr, NULL, &ipif_flags) != 0) {
8029 				*saddr = ipv6_all_zeros;
8030 				ipif_flags = 0;
8031 			}
8032 		}
8033 
8034 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8035 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
8036 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8037 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8038 		ire_refrele(ire);
8039 		ill_refrele(dst_ill);
8040 	}
8041 	miocack(q, mp, iocp->ioc_count, 0);
8042 }
8043 
8044 /*
8045  * Check if this is an address assigned to this machine.
8046  * Skips interfaces that are down by using ire checks.
8047  * Translates mapped addresses to v4 addresses and then
8048  * treats them as such, returning true if the v4 address
8049  * associated with this mapped address is configured.
8050  * Note: Applications will have to be careful what they do
8051  * with the response; use of mapped addresses limits
8052  * what can be done with the socket, especially with
8053  * respect to socket options and ioctls - neither IPv4
8054  * options nor IPv6 sticky options/ancillary data options
8055  * may be used.
8056  */
8057 /* ARGSUSED */
8058 int
8059 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8060     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8061 {
8062 	struct sioc_addrreq *sia;
8063 	sin_t *sin;
8064 	ire_t *ire;
8065 	mblk_t *mp1;
8066 	zoneid_t zoneid;
8067 	ip_stack_t	*ipst;
8068 
8069 	ip1dbg(("ip_sioctl_tmyaddr"));
8070 
8071 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8072 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8073 	ipst = CONNQ_TO_IPST(q);
8074 
8075 	/* Existence verified in ip_wput_nondata */
8076 	mp1 = mp->b_cont->b_cont;
8077 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8078 	sin = (sin_t *)&sia->sa_addr;
8079 	switch (sin->sin_family) {
8080 	case AF_INET6: {
8081 		sin6_t *sin6 = (sin6_t *)sin;
8082 
8083 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8084 			ipaddr_t v4_addr;
8085 
8086 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8087 			    v4_addr);
8088 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8089 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8090 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8091 		} else {
8092 			in6_addr_t v6addr;
8093 
8094 			v6addr = sin6->sin6_addr;
8095 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8096 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8097 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8098 		}
8099 		break;
8100 	}
8101 	case AF_INET: {
8102 		ipaddr_t v4addr;
8103 
8104 		v4addr = sin->sin_addr.s_addr;
8105 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8106 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8107 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8108 		break;
8109 	}
8110 	default:
8111 		return (EAFNOSUPPORT);
8112 	}
8113 	if (ire != NULL) {
8114 		sia->sa_res = 1;
8115 		ire_refrele(ire);
8116 	} else {
8117 		sia->sa_res = 0;
8118 	}
8119 	return (0);
8120 }
8121 
8122 /*
8123  * Check if this is an address assigned on-link i.e. neighbor,
8124  * and makes sure it's reachable from the current zone.
8125  * Returns true for my addresses as well.
8126  * Translates mapped addresses to v4 addresses and then
8127  * treats them as such, returning true if the v4 address
8128  * associated with this mapped address is configured.
8129  * Note: Applications will have to be careful what they do
8130  * with the response; use of mapped addresses limits
8131  * what can be done with the socket, especially with
8132  * respect to socket options and ioctls - neither IPv4
8133  * options nor IPv6 sticky options/ancillary data options
8134  * may be used.
8135  */
8136 /* ARGSUSED */
8137 int
8138 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8139     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8140 {
8141 	struct sioc_addrreq *sia;
8142 	sin_t *sin;
8143 	mblk_t	*mp1;
8144 	ire_t *ire = NULL;
8145 	zoneid_t zoneid;
8146 	ip_stack_t	*ipst;
8147 
8148 	ip1dbg(("ip_sioctl_tonlink"));
8149 
8150 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8151 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8152 	ipst = CONNQ_TO_IPST(q);
8153 
8154 	/* Existence verified in ip_wput_nondata */
8155 	mp1 = mp->b_cont->b_cont;
8156 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8157 	sin = (sin_t *)&sia->sa_addr;
8158 
8159 	/*
8160 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8161 	 * to make sure we only look at on-link unicast address.
8162 	 */
8163 	switch (sin->sin_family) {
8164 	case AF_INET6: {
8165 		sin6_t *sin6 = (sin6_t *)sin;
8166 
8167 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8168 			ipaddr_t v4_addr;
8169 
8170 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8171 			    v4_addr);
8172 			if (!CLASSD(v4_addr)) {
8173 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8174 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8175 				    0, ipst, NULL);
8176 			}
8177 		} else {
8178 			in6_addr_t v6addr;
8179 
8180 			v6addr = sin6->sin6_addr;
8181 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8182 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8183 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8184 				    ipst, NULL);
8185 			}
8186 		}
8187 		break;
8188 	}
8189 	case AF_INET: {
8190 		ipaddr_t v4addr;
8191 
8192 		v4addr = sin->sin_addr.s_addr;
8193 		if (!CLASSD(v4addr)) {
8194 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8195 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8196 		}
8197 		break;
8198 	}
8199 	default:
8200 		return (EAFNOSUPPORT);
8201 	}
8202 	sia->sa_res = 0;
8203 	if (ire != NULL) {
8204 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8205 
8206 		if ((ire->ire_type & IRE_ONLINK) &&
8207 		    !(ire->ire_type & IRE_BROADCAST))
8208 			sia->sa_res = 1;
8209 		ire_refrele(ire);
8210 	}
8211 	return (0);
8212 }
8213 
8214 /*
8215  * TBD: implement when kernel maintaines a list of site prefixes.
8216  */
8217 /* ARGSUSED */
8218 int
8219 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8220     ip_ioctl_cmd_t *ipip, void *ifreq)
8221 {
8222 	return (ENXIO);
8223 }
8224 
8225 /* ARP IOCTLs. */
8226 /* ARGSUSED */
8227 int
8228 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8229     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8230 {
8231 	int		err;
8232 	ipaddr_t	ipaddr;
8233 	struct iocblk	*iocp;
8234 	conn_t		*connp;
8235 	struct arpreq	*ar;
8236 	struct xarpreq	*xar;
8237 	int		arp_flags, flags, alength;
8238 	uchar_t		*lladdr;
8239 	ip_stack_t	*ipst;
8240 	ill_t		*ill = ipif->ipif_ill;
8241 	ill_t		*proxy_ill = NULL;
8242 	ipmp_arpent_t	*entp = NULL;
8243 	boolean_t	proxyarp = B_FALSE;
8244 	boolean_t	if_arp_ioctl = B_FALSE;
8245 	ncec_t		*ncec = NULL;
8246 	nce_t		*nce;
8247 
8248 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8249 	connp = Q_TO_CONN(q);
8250 	ipst = connp->conn_netstack->netstack_ip;
8251 	iocp = (struct iocblk *)mp->b_rptr;
8252 
8253 	if (ipip->ipi_cmd_type == XARP_CMD) {
8254 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8255 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8256 		ar = NULL;
8257 
8258 		arp_flags = xar->xarp_flags;
8259 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8260 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8261 		/*
8262 		 * Validate against user's link layer address length
8263 		 * input and name and addr length limits.
8264 		 */
8265 		alength = ill->ill_phys_addr_length;
8266 		if (ipip->ipi_cmd == SIOCSXARP) {
8267 			if (alength != xar->xarp_ha.sdl_alen ||
8268 			    (alength + xar->xarp_ha.sdl_nlen >
8269 			    sizeof (xar->xarp_ha.sdl_data)))
8270 				return (EINVAL);
8271 		}
8272 	} else {
8273 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8274 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8275 		xar = NULL;
8276 
8277 		arp_flags = ar->arp_flags;
8278 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8279 		/*
8280 		 * Theoretically, the sa_family could tell us what link
8281 		 * layer type this operation is trying to deal with. By
8282 		 * common usage AF_UNSPEC means ethernet. We'll assume
8283 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8284 		 * for now. Our new SIOC*XARP ioctls can be used more
8285 		 * generally.
8286 		 *
8287 		 * If the underlying media happens to have a non 6 byte
8288 		 * address, arp module will fail set/get, but the del
8289 		 * operation will succeed.
8290 		 */
8291 		alength = 6;
8292 		if ((ipip->ipi_cmd != SIOCDARP) &&
8293 		    (alength != ill->ill_phys_addr_length)) {
8294 			return (EINVAL);
8295 		}
8296 	}
8297 
8298 	/* Translate ATF* flags to NCE* flags */
8299 	flags = 0;
8300 	if (arp_flags & ATF_AUTHORITY)
8301 		flags |= NCE_F_AUTHORITY;
8302 	if (arp_flags & ATF_PERM)
8303 		flags |= NCE_F_NONUD; /* not subject to aging */
8304 	if (arp_flags & ATF_PUBL)
8305 		flags |= NCE_F_PUBLISH;
8306 
8307 	/*
8308 	 * IPMP ARP special handling:
8309 	 *
8310 	 * 1. Since ARP mappings must appear consistent across the group,
8311 	 *    prohibit changing ARP mappings on the underlying interfaces.
8312 	 *
8313 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8314 	 *    IP itself, prohibit changing them.
8315 	 *
8316 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8317 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8318 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8319 	 */
8320 	if (IS_UNDER_IPMP(ill)) {
8321 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8322 			return (EPERM);
8323 	}
8324 	if (IS_IPMP(ill)) {
8325 		ipmp_illgrp_t *illg = ill->ill_grp;
8326 
8327 		switch (ipip->ipi_cmd) {
8328 		case SIOCSARP:
8329 		case SIOCSXARP:
8330 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8331 			if (proxy_ill != NULL) {
8332 				proxyarp = B_TRUE;
8333 				if (!ipmp_ill_is_active(proxy_ill))
8334 					proxy_ill = ipmp_illgrp_next_ill(illg);
8335 				if (proxy_ill != NULL)
8336 					lladdr = proxy_ill->ill_phys_addr;
8337 			}
8338 			/* FALLTHRU */
8339 		}
8340 	}
8341 
8342 	ipaddr = sin->sin_addr.s_addr;
8343 	/*
8344 	 * don't match across illgrp per case (1) and (2).
8345 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8346 	 */
8347 	nce = nce_lookup_v4(ill, &ipaddr);
8348 	if (nce != NULL)
8349 		ncec = nce->nce_common;
8350 
8351 	switch (iocp->ioc_cmd) {
8352 	case SIOCDARP:
8353 	case SIOCDXARP: {
8354 		/*
8355 		 * Delete the NCE if any.
8356 		 */
8357 		if (ncec == NULL) {
8358 			iocp->ioc_error = ENXIO;
8359 			break;
8360 		}
8361 		/* Don't allow changes to arp mappings of local addresses. */
8362 		if (NCE_MYADDR(ncec)) {
8363 			nce_refrele(nce);
8364 			return (ENOTSUP);
8365 		}
8366 		iocp->ioc_error = 0;
8367 
8368 		/*
8369 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8370 		 * This will delete all the nce entries on the under_ills.
8371 		 */
8372 		ncec_delete(ncec);
8373 		/*
8374 		 * Once the NCE has been deleted, then the ire_dep* consistency
8375 		 * mechanism will find any IRE which depended on the now
8376 		 * condemned NCE (as part of sending packets).
8377 		 * That mechanism handles redirects by deleting redirects
8378 		 * that refer to UNREACHABLE nces.
8379 		 */
8380 		break;
8381 	}
8382 	case SIOCGARP:
8383 	case SIOCGXARP:
8384 		if (ncec != NULL) {
8385 			lladdr = ncec->ncec_lladdr;
8386 			flags = ncec->ncec_flags;
8387 			iocp->ioc_error = 0;
8388 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8389 		} else {
8390 			iocp->ioc_error = ENXIO;
8391 		}
8392 		break;
8393 	case SIOCSARP:
8394 	case SIOCSXARP:
8395 		/* Don't allow changes to arp mappings of local addresses. */
8396 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8397 			nce_refrele(nce);
8398 			return (ENOTSUP);
8399 		}
8400 
8401 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8402 		flags |= NCE_F_STATIC;
8403 		if (!if_arp_ioctl) {
8404 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8405 			    lladdr, alength, flags);
8406 		} else {
8407 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8408 			if (ipif != NULL) {
8409 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8410 				    lladdr, alength, flags);
8411 				ipif_refrele(ipif);
8412 			}
8413 		}
8414 		if (nce != NULL) {
8415 			nce_refrele(nce);
8416 			nce = NULL;
8417 		}
8418 		/*
8419 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8420 		 * by nce_add_common()
8421 		 */
8422 		err = nce_lookup_then_add_v4(ill, lladdr,
8423 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8424 		    &nce);
8425 		if (err == EEXIST) {
8426 			ncec = nce->nce_common;
8427 			mutex_enter(&ncec->ncec_lock);
8428 			ncec->ncec_state = ND_REACHABLE;
8429 			ncec->ncec_flags = flags;
8430 			nce_update(ncec, ND_UNCHANGED, lladdr);
8431 			mutex_exit(&ncec->ncec_lock);
8432 			err = 0;
8433 		}
8434 		if (nce != NULL) {
8435 			nce_refrele(nce);
8436 			nce = NULL;
8437 		}
8438 		if (IS_IPMP(ill) && err == 0) {
8439 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8440 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8441 			    flags);
8442 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8443 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8444 				break;
8445 			}
8446 		}
8447 		iocp->ioc_error = err;
8448 	}
8449 
8450 	if (nce != NULL) {
8451 		nce_refrele(nce);
8452 	}
8453 
8454 	/*
8455 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8456 	 */
8457 	if (entp != NULL)
8458 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8459 
8460 	return (iocp->ioc_error);
8461 }
8462 
8463 /*
8464  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8465  * the associated sin and refhold and return the associated ipif via `ci'.
8466  */
8467 int
8468 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8469     cmd_info_t *ci)
8470 {
8471 	mblk_t	*mp1;
8472 	sin_t	*sin;
8473 	conn_t	*connp;
8474 	ipif_t	*ipif;
8475 	ire_t	*ire = NULL;
8476 	ill_t	*ill = NULL;
8477 	boolean_t exists;
8478 	ip_stack_t *ipst;
8479 	struct arpreq *ar;
8480 	struct xarpreq *xar;
8481 	struct sockaddr_dl *sdl;
8482 
8483 	/* ioctl comes down on a conn */
8484 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8485 	connp = Q_TO_CONN(q);
8486 	if (connp->conn_family == AF_INET6)
8487 		return (ENXIO);
8488 
8489 	ipst = connp->conn_netstack->netstack_ip;
8490 
8491 	/* Verified in ip_wput_nondata */
8492 	mp1 = mp->b_cont->b_cont;
8493 
8494 	if (ipip->ipi_cmd_type == XARP_CMD) {
8495 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8496 		xar = (struct xarpreq *)mp1->b_rptr;
8497 		sin = (sin_t *)&xar->xarp_pa;
8498 		sdl = &xar->xarp_ha;
8499 
8500 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8501 			return (ENXIO);
8502 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8503 			return (EINVAL);
8504 	} else {
8505 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8506 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8507 		ar = (struct arpreq *)mp1->b_rptr;
8508 		sin = (sin_t *)&ar->arp_pa;
8509 	}
8510 
8511 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8512 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8513 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8514 		if (ipif == NULL)
8515 			return (ENXIO);
8516 		if (ipif->ipif_id != 0) {
8517 			ipif_refrele(ipif);
8518 			return (ENXIO);
8519 		}
8520 	} else {
8521 		/*
8522 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8523 		 * of 0: use the IP address to find the ipif.  If the IP
8524 		 * address is an IPMP test address, ire_ftable_lookup() will
8525 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8526 		 */
8527 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8528 		    ipst);
8529 		if (ipif == NULL) {
8530 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8531 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8532 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8533 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8534 				if (ire != NULL)
8535 					ire_refrele(ire);
8536 				return (ENXIO);
8537 			}
8538 			ASSERT(ire != NULL && ill != NULL);
8539 			ipif = ill->ill_ipif;
8540 			ipif_refhold(ipif);
8541 			ire_refrele(ire);
8542 		}
8543 	}
8544 
8545 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8546 		ipif_refrele(ipif);
8547 		return (ENXIO);
8548 	}
8549 
8550 	ci->ci_sin = sin;
8551 	ci->ci_ipif = ipif;
8552 	return (0);
8553 }
8554 
8555 /*
8556  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8557  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8558  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8559  * up and thus an ill can join that illgrp.
8560  *
8561  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8562  * open()/close() primarily because close() is not allowed to fail or block
8563  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8564  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8565  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8566  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8567  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8568  * state if I_UNLINK didn't occur.
8569  *
8570  * Note that for each plumb/unplumb operation, we may end up here more than
8571  * once because of the way ifconfig works.  However, it's OK to link the same
8572  * illgrp more than once, or unlink an illgrp that's already unlinked.
8573  */
8574 static int
8575 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8576 {
8577 	int err;
8578 	ip_stack_t *ipst = ill->ill_ipst;
8579 
8580 	ASSERT(IS_IPMP(ill));
8581 	ASSERT(IAM_WRITER_ILL(ill));
8582 
8583 	switch (ioccmd) {
8584 	case I_LINK:
8585 		return (ENOTSUP);
8586 
8587 	case I_PLINK:
8588 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8589 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8590 		rw_exit(&ipst->ips_ipmp_lock);
8591 		break;
8592 
8593 	case I_PUNLINK:
8594 		/*
8595 		 * Require all UP ipifs be brought down prior to unlinking the
8596 		 * illgrp so any associated IREs (and other state) is torched.
8597 		 */
8598 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8599 			return (EBUSY);
8600 
8601 		/*
8602 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8603 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8604 		 * join this group.  Specifically: ills trying to join grab
8605 		 * ipmp_lock and bump a "pending join" counter checked by
8606 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8607 		 * joins can occur (since we have ipmp_lock).  Once we drop
8608 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8609 		 * find the illgrp (since we unlinked it) and will return
8610 		 * EAFNOSUPPORT.  This will then take them back through the
8611 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8612 		 * back through I_PLINK above.
8613 		 */
8614 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8615 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8616 		rw_exit(&ipst->ips_ipmp_lock);
8617 		return (err);
8618 	default:
8619 		break;
8620 	}
8621 	return (0);
8622 }
8623 
8624 /*
8625  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8626  * atomically set/clear the muxids. Also complete the ioctl by acking or
8627  * naking it.  Note that the code is structured such that the link type,
8628  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8629  * its clones use the persistent link, while pppd(1M) and perhaps many
8630  * other daemons may use non-persistent link.  When combined with some
8631  * ill_t states, linking and unlinking lower streams may be used as
8632  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8633  */
8634 /* ARGSUSED */
8635 void
8636 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8637 {
8638 	mblk_t		*mp1;
8639 	struct linkblk	*li;
8640 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8641 	int		err = 0;
8642 
8643 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8644 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8645 
8646 	mp1 = mp->b_cont;	/* This is the linkblk info */
8647 	li = (struct linkblk *)mp1->b_rptr;
8648 
8649 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8650 	if (err == EINPROGRESS)
8651 		return;
8652 done:
8653 	if (err == 0)
8654 		miocack(q, mp, 0, 0);
8655 	else
8656 		miocnak(q, mp, 0, err);
8657 
8658 	/* Conn was refheld in ip_sioctl_copyin_setup */
8659 	if (CONN_Q(q))
8660 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8661 }
8662 
8663 /*
8664  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8665  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8666  * module stream).  If `doconsist' is set, then do the extended consistency
8667  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8668  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8669  * an error code on failure.
8670  */
8671 static int
8672 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8673     struct linkblk *li)
8674 {
8675 	int		err = 0;
8676 	ill_t  		*ill;
8677 	queue_t		*ipwq, *dwq;
8678 	const char	*name;
8679 	struct qinit	*qinfo;
8680 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8681 	boolean_t	entered_ipsq = B_FALSE;
8682 	boolean_t	is_ip = B_FALSE;
8683 	arl_t		*arl;
8684 
8685 	/*
8686 	 * Walk the lower stream to verify it's the IP module stream.
8687 	 * The IP module is identified by its name, wput function,
8688 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8689 	 * (li->l_qbot) will not vanish until this ioctl completes.
8690 	 */
8691 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8692 		qinfo = ipwq->q_qinfo;
8693 		name = qinfo->qi_minfo->mi_idname;
8694 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8695 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8696 			is_ip = B_TRUE;
8697 			break;
8698 		}
8699 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8700 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8701 			break;
8702 		}
8703 	}
8704 
8705 	/*
8706 	 * If this isn't an IP module stream, bail.
8707 	 */
8708 	if (ipwq == NULL)
8709 		return (0);
8710 
8711 	if (!is_ip) {
8712 		arl = (arl_t *)ipwq->q_ptr;
8713 		ill = arl_to_ill(arl);
8714 		if (ill == NULL)
8715 			return (0);
8716 	} else {
8717 		ill = ipwq->q_ptr;
8718 	}
8719 	ASSERT(ill != NULL);
8720 
8721 	if (ipsq == NULL) {
8722 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8723 		    NEW_OP, B_FALSE);
8724 		if (ipsq == NULL) {
8725 			if (!is_ip)
8726 				ill_refrele(ill);
8727 			return (EINPROGRESS);
8728 		}
8729 		entered_ipsq = B_TRUE;
8730 	}
8731 	ASSERT(IAM_WRITER_ILL(ill));
8732 	mutex_enter(&ill->ill_lock);
8733 	if (!is_ip) {
8734 		if (islink && ill->ill_muxid == 0) {
8735 			/*
8736 			 * Plumbing has to be done with IP plumbed first, arp
8737 			 * second, but here we have arp being plumbed first.
8738 			 */
8739 			mutex_exit(&ill->ill_lock);
8740 			ipsq_exit(ipsq);
8741 			ill_refrele(ill);
8742 			return (EINVAL);
8743 		}
8744 	}
8745 	mutex_exit(&ill->ill_lock);
8746 	if (!is_ip) {
8747 		arl->arl_muxid = islink ? li->l_index : 0;
8748 		ill_refrele(ill);
8749 		goto done;
8750 	}
8751 
8752 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8753 		goto done;
8754 
8755 	/*
8756 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8757 	 * the read queue of the module immediately below IP in the ill.
8758 	 * These are used during the capability negotiation below.
8759 	 */
8760 	ill->ill_lmod_rq = NULL;
8761 	ill->ill_lmod_cnt = 0;
8762 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8763 		ill->ill_lmod_rq = RD(dwq);
8764 		for (; dwq != NULL; dwq = dwq->q_next)
8765 			ill->ill_lmod_cnt++;
8766 	}
8767 
8768 	ill->ill_muxid = islink ? li->l_index : 0;
8769 
8770 	/*
8771 	 * Mark the ipsq busy until the capability operations initiated below
8772 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8773 	 * returns, but the capability operation may complete asynchronously
8774 	 * much later.
8775 	 */
8776 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8777 	/*
8778 	 * If there's at least one up ipif on this ill, then we're bound to
8779 	 * the underlying driver via DLPI.  In that case, renegotiate
8780 	 * capabilities to account for any possible change in modules
8781 	 * interposed between IP and the driver.
8782 	 */
8783 	if (ill->ill_ipif_up_count > 0) {
8784 		if (islink)
8785 			ill_capability_probe(ill);
8786 		else
8787 			ill_capability_reset(ill, B_FALSE);
8788 	}
8789 	ipsq_current_finish(ipsq);
8790 done:
8791 	if (entered_ipsq)
8792 		ipsq_exit(ipsq);
8793 
8794 	return (err);
8795 }
8796 
8797 /*
8798  * Search the ioctl command in the ioctl tables and return a pointer
8799  * to the ioctl command information. The ioctl command tables are
8800  * static and fully populated at compile time.
8801  */
8802 ip_ioctl_cmd_t *
8803 ip_sioctl_lookup(int ioc_cmd)
8804 {
8805 	int index;
8806 	ip_ioctl_cmd_t *ipip;
8807 	ip_ioctl_cmd_t *ipip_end;
8808 
8809 	if (ioc_cmd == IPI_DONTCARE)
8810 		return (NULL);
8811 
8812 	/*
8813 	 * Do a 2 step search. First search the indexed table
8814 	 * based on the least significant byte of the ioctl cmd.
8815 	 * If we don't find a match, then search the misc table
8816 	 * serially.
8817 	 */
8818 	index = ioc_cmd & 0xFF;
8819 	if (index < ip_ndx_ioctl_count) {
8820 		ipip = &ip_ndx_ioctl_table[index];
8821 		if (ipip->ipi_cmd == ioc_cmd) {
8822 			/* Found a match in the ndx table */
8823 			return (ipip);
8824 		}
8825 	}
8826 
8827 	/* Search the misc table */
8828 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8829 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8830 		if (ipip->ipi_cmd == ioc_cmd)
8831 			/* Found a match in the misc table */
8832 			return (ipip);
8833 	}
8834 
8835 	return (NULL);
8836 }
8837 
8838 /*
8839  * Wrapper function for resuming deferred ioctl processing
8840  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8841  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8842  */
8843 /* ARGSUSED */
8844 void
8845 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8846     void *dummy_arg)
8847 {
8848 	ip_sioctl_copyin_setup(q, mp);
8849 }
8850 
8851 /*
8852  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8853  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8854  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8855  * We establish here the size of the block to be copied in.  mi_copyin
8856  * arranges for this to happen, an processing continues in ip_wput_nondata with
8857  * an M_IOCDATA message.
8858  */
8859 void
8860 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8861 {
8862 	int	copyin_size;
8863 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8864 	ip_ioctl_cmd_t *ipip;
8865 	cred_t *cr;
8866 	ip_stack_t	*ipst;
8867 
8868 	if (CONN_Q(q))
8869 		ipst = CONNQ_TO_IPST(q);
8870 	else
8871 		ipst = ILLQ_TO_IPST(q);
8872 
8873 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8874 	if (ipip == NULL) {
8875 		/*
8876 		 * The ioctl is not one we understand or own.
8877 		 * Pass it along to be processed down stream,
8878 		 * if this is a module instance of IP, else nak
8879 		 * the ioctl.
8880 		 */
8881 		if (q->q_next == NULL) {
8882 			goto nak;
8883 		} else {
8884 			putnext(q, mp);
8885 			return;
8886 		}
8887 	}
8888 
8889 	/*
8890 	 * If this is deferred, then we will do all the checks when we
8891 	 * come back.
8892 	 */
8893 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8894 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8895 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8896 		return;
8897 	}
8898 
8899 	/*
8900 	 * Only allow a very small subset of IP ioctls on this stream if
8901 	 * IP is a module and not a driver. Allowing ioctls to be processed
8902 	 * in this case may cause assert failures or data corruption.
8903 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8904 	 * ioctls allowed on an IP module stream, after which this stream
8905 	 * normally becomes a multiplexor (at which time the stream head
8906 	 * will fail all ioctls).
8907 	 */
8908 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8909 		goto nak;
8910 	}
8911 
8912 	/* Make sure we have ioctl data to process. */
8913 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8914 		goto nak;
8915 
8916 	/*
8917 	 * Prefer dblk credential over ioctl credential; some synthesized
8918 	 * ioctls have kcred set because there's no way to crhold()
8919 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8920 	 * the framework; the caller of ioctl needs to hold the reference
8921 	 * for the duration of the call).
8922 	 */
8923 	cr = msg_getcred(mp, NULL);
8924 	if (cr == NULL)
8925 		cr = iocp->ioc_cr;
8926 
8927 	/* Make sure normal users don't send down privileged ioctls */
8928 	if ((ipip->ipi_flags & IPI_PRIV) &&
8929 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8930 		/* We checked the privilege earlier but log it here */
8931 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8932 		return;
8933 	}
8934 
8935 	/*
8936 	 * The ioctl command tables can only encode fixed length
8937 	 * ioctl data. If the length is variable, the table will
8938 	 * encode the length as zero. Such special cases are handled
8939 	 * below in the switch.
8940 	 */
8941 	if (ipip->ipi_copyin_size != 0) {
8942 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8943 		return;
8944 	}
8945 
8946 	switch (iocp->ioc_cmd) {
8947 	case O_SIOCGIFCONF:
8948 	case SIOCGIFCONF:
8949 		/*
8950 		 * This IOCTL is hilarious.  See comments in
8951 		 * ip_sioctl_get_ifconf for the story.
8952 		 */
8953 		if (iocp->ioc_count == TRANSPARENT)
8954 			copyin_size = SIZEOF_STRUCT(ifconf,
8955 			    iocp->ioc_flag);
8956 		else
8957 			copyin_size = iocp->ioc_count;
8958 		mi_copyin(q, mp, NULL, copyin_size);
8959 		return;
8960 
8961 	case O_SIOCGLIFCONF:
8962 	case SIOCGLIFCONF:
8963 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8964 		mi_copyin(q, mp, NULL, copyin_size);
8965 		return;
8966 
8967 	case SIOCGLIFSRCOF:
8968 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8969 		mi_copyin(q, mp, NULL, copyin_size);
8970 		return;
8971 	case SIOCGIP6ADDRPOLICY:
8972 		ip_sioctl_ip6addrpolicy(q, mp);
8973 		ip6_asp_table_refrele(ipst);
8974 		return;
8975 
8976 	case SIOCSIP6ADDRPOLICY:
8977 		ip_sioctl_ip6addrpolicy(q, mp);
8978 		return;
8979 
8980 	case SIOCGDSTINFO:
8981 		ip_sioctl_dstinfo(q, mp);
8982 		ip6_asp_table_refrele(ipst);
8983 		return;
8984 
8985 	case I_PLINK:
8986 	case I_PUNLINK:
8987 	case I_LINK:
8988 	case I_UNLINK:
8989 		/*
8990 		 * We treat non-persistent link similarly as the persistent
8991 		 * link case, in terms of plumbing/unplumbing, as well as
8992 		 * dynamic re-plumbing events indicator.  See comments
8993 		 * in ip_sioctl_plink() for more.
8994 		 *
8995 		 * Request can be enqueued in the 'ipsq' while waiting
8996 		 * to become exclusive. So bump up the conn ref.
8997 		 */
8998 		if (CONN_Q(q))
8999 			CONN_INC_REF(Q_TO_CONN(q));
9000 		ip_sioctl_plink(NULL, q, mp, NULL);
9001 		return;
9002 
9003 	case ND_GET:
9004 	case ND_SET:
9005 		/*
9006 		 * Use of the nd table requires holding the reader lock.
9007 		 * Modifying the nd table thru nd_load/nd_unload requires
9008 		 * the writer lock.
9009 		 */
9010 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
9011 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
9012 			rw_exit(&ipst->ips_ip_g_nd_lock);
9013 
9014 			if (iocp->ioc_error)
9015 				iocp->ioc_count = 0;
9016 			mp->b_datap->db_type = M_IOCACK;
9017 			qreply(q, mp);
9018 			return;
9019 		}
9020 		rw_exit(&ipst->ips_ip_g_nd_lock);
9021 		/*
9022 		 * We don't understand this subioctl of ND_GET / ND_SET.
9023 		 * Maybe intended for some driver / module below us
9024 		 */
9025 		if (q->q_next) {
9026 			putnext(q, mp);
9027 		} else {
9028 			iocp->ioc_error = ENOENT;
9029 			mp->b_datap->db_type = M_IOCNAK;
9030 			iocp->ioc_count = 0;
9031 			qreply(q, mp);
9032 		}
9033 		return;
9034 
9035 	case IP_IOCTL:
9036 		ip_wput_ioctl(q, mp);
9037 		return;
9038 
9039 	case SIOCILB:
9040 		/* The ioctl length varies depending on the ILB command. */
9041 		copyin_size = iocp->ioc_count;
9042 		if (copyin_size < sizeof (ilb_cmd_t))
9043 			goto nak;
9044 		mi_copyin(q, mp, NULL, copyin_size);
9045 		return;
9046 
9047 	default:
9048 		cmn_err(CE_PANIC, "should not happen ");
9049 	}
9050 nak:
9051 	if (mp->b_cont != NULL) {
9052 		freemsg(mp->b_cont);
9053 		mp->b_cont = NULL;
9054 	}
9055 	iocp->ioc_error = EINVAL;
9056 	mp->b_datap->db_type = M_IOCNAK;
9057 	iocp->ioc_count = 0;
9058 	qreply(q, mp);
9059 }
9060 
9061 static void
9062 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9063 {
9064 	struct arpreq *ar;
9065 	struct xarpreq *xar;
9066 	mblk_t	*tmp;
9067 	struct iocblk *iocp;
9068 	int x_arp_ioctl = B_FALSE;
9069 	int *flagsp;
9070 	char *storage = NULL;
9071 
9072 	ASSERT(ill != NULL);
9073 
9074 	iocp = (struct iocblk *)mp->b_rptr;
9075 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9076 
9077 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9078 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9079 	    (iocp->ioc_cmd == SIOCSXARP)) {
9080 		x_arp_ioctl = B_TRUE;
9081 		xar = (struct xarpreq *)tmp->b_rptr;
9082 		flagsp = &xar->xarp_flags;
9083 		storage = xar->xarp_ha.sdl_data;
9084 	} else {
9085 		ar = (struct arpreq *)tmp->b_rptr;
9086 		flagsp = &ar->arp_flags;
9087 		storage = ar->arp_ha.sa_data;
9088 	}
9089 
9090 	/*
9091 	 * We're done if this is not an SIOCG{X}ARP
9092 	 */
9093 	if (x_arp_ioctl) {
9094 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9095 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9096 		    sizeof (xar->xarp_ha.sdl_data)) {
9097 			iocp->ioc_error = EINVAL;
9098 			return;
9099 		}
9100 	}
9101 	*flagsp = ATF_INUSE;
9102 	/*
9103 	 * If /sbin/arp told us we are the authority using the "permanent"
9104 	 * flag, or if this is one of my addresses print "permanent"
9105 	 * in the /sbin/arp output.
9106 	 */
9107 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9108 		*flagsp |= ATF_AUTHORITY;
9109 	if (flags & NCE_F_NONUD)
9110 		*flagsp |= ATF_PERM; /* not subject to aging */
9111 	if (flags & NCE_F_PUBLISH)
9112 		*flagsp |= ATF_PUBL;
9113 	if (hwaddr != NULL) {
9114 		*flagsp |= ATF_COM;
9115 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9116 	}
9117 }
9118 
9119 /*
9120  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9121  * interface) create the next available logical interface for this
9122  * physical interface.
9123  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9124  * ipif with the specified name.
9125  *
9126  * If the address family is not AF_UNSPEC then set the address as well.
9127  *
9128  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9129  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9130  *
9131  * Executed as a writer on the ill.
9132  * So no lock is needed to traverse the ipif chain, or examine the
9133  * phyint flags.
9134  */
9135 /* ARGSUSED */
9136 int
9137 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9138     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9139 {
9140 	mblk_t	*mp1;
9141 	struct lifreq *lifr;
9142 	boolean_t	isv6;
9143 	boolean_t	exists;
9144 	char 	*name;
9145 	char	*endp;
9146 	char	*cp;
9147 	int	namelen;
9148 	ipif_t	*ipif;
9149 	long	id;
9150 	ipsq_t	*ipsq;
9151 	ill_t	*ill;
9152 	sin_t	*sin;
9153 	int	err = 0;
9154 	boolean_t found_sep = B_FALSE;
9155 	conn_t	*connp;
9156 	zoneid_t zoneid;
9157 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9158 
9159 	ASSERT(q->q_next == NULL);
9160 	ip1dbg(("ip_sioctl_addif\n"));
9161 	/* Existence of mp1 has been checked in ip_wput_nondata */
9162 	mp1 = mp->b_cont->b_cont;
9163 	/*
9164 	 * Null terminate the string to protect against buffer
9165 	 * overrun. String was generated by user code and may not
9166 	 * be trusted.
9167 	 */
9168 	lifr = (struct lifreq *)mp1->b_rptr;
9169 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9170 	name = lifr->lifr_name;
9171 	ASSERT(CONN_Q(q));
9172 	connp = Q_TO_CONN(q);
9173 	isv6 = (connp->conn_family == AF_INET6);
9174 	zoneid = connp->conn_zoneid;
9175 	namelen = mi_strlen(name);
9176 	if (namelen == 0)
9177 		return (EINVAL);
9178 
9179 	exists = B_FALSE;
9180 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9181 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9182 		/*
9183 		 * Allow creating lo0 using SIOCLIFADDIF.
9184 		 * can't be any other writer thread. So can pass null below
9185 		 * for the last 4 args to ipif_lookup_name.
9186 		 */
9187 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9188 		    &exists, isv6, zoneid, ipst);
9189 		/* Prevent any further action */
9190 		if (ipif == NULL) {
9191 			return (ENOBUFS);
9192 		} else if (!exists) {
9193 			/* We created the ipif now and as writer */
9194 			ipif_refrele(ipif);
9195 			return (0);
9196 		} else {
9197 			ill = ipif->ipif_ill;
9198 			ill_refhold(ill);
9199 			ipif_refrele(ipif);
9200 		}
9201 	} else {
9202 		/* Look for a colon in the name. */
9203 		endp = &name[namelen];
9204 		for (cp = endp; --cp > name; ) {
9205 			if (*cp == IPIF_SEPARATOR_CHAR) {
9206 				found_sep = B_TRUE;
9207 				/*
9208 				 * Reject any non-decimal aliases for plumbing
9209 				 * of logical interfaces. Aliases with leading
9210 				 * zeroes are also rejected as they introduce
9211 				 * ambiguity in the naming of the interfaces.
9212 				 * Comparing with "0" takes care of all such
9213 				 * cases.
9214 				 */
9215 				if ((strncmp("0", cp+1, 1)) == 0)
9216 					return (EINVAL);
9217 
9218 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9219 				    id <= 0 || *endp != '\0') {
9220 					return (EINVAL);
9221 				}
9222 				*cp = '\0';
9223 				break;
9224 			}
9225 		}
9226 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9227 		if (found_sep)
9228 			*cp = IPIF_SEPARATOR_CHAR;
9229 		if (ill == NULL)
9230 			return (ENXIO);
9231 	}
9232 
9233 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9234 	    B_TRUE);
9235 
9236 	/*
9237 	 * Release the refhold due to the lookup, now that we are excl
9238 	 * or we are just returning
9239 	 */
9240 	ill_refrele(ill);
9241 
9242 	if (ipsq == NULL)
9243 		return (EINPROGRESS);
9244 
9245 	/* We are now exclusive on the IPSQ */
9246 	ASSERT(IAM_WRITER_ILL(ill));
9247 
9248 	if (found_sep) {
9249 		/* Now see if there is an IPIF with this unit number. */
9250 		for (ipif = ill->ill_ipif; ipif != NULL;
9251 		    ipif = ipif->ipif_next) {
9252 			if (ipif->ipif_id == id) {
9253 				err = EEXIST;
9254 				goto done;
9255 			}
9256 		}
9257 	}
9258 
9259 	/*
9260 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9261 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9262 	 * instead.
9263 	 */
9264 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9265 	    B_TRUE, B_TRUE, &err)) == NULL) {
9266 		goto done;
9267 	}
9268 
9269 	/* Return created name with ioctl */
9270 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9271 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9272 	ip1dbg(("created %s\n", lifr->lifr_name));
9273 
9274 	/* Set address */
9275 	sin = (sin_t *)&lifr->lifr_addr;
9276 	if (sin->sin_family != AF_UNSPEC) {
9277 		err = ip_sioctl_addr(ipif, sin, q, mp,
9278 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9279 	}
9280 
9281 done:
9282 	ipsq_exit(ipsq);
9283 	return (err);
9284 }
9285 
9286 /*
9287  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9288  * interface) delete it based on the IP address (on this physical interface).
9289  * Otherwise delete it based on the ipif_id.
9290  * Also, special handling to allow a removeif of lo0.
9291  */
9292 /* ARGSUSED */
9293 int
9294 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9295     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9296 {
9297 	conn_t		*connp;
9298 	ill_t		*ill = ipif->ipif_ill;
9299 	boolean_t	 success;
9300 	ip_stack_t	*ipst;
9301 
9302 	ipst = CONNQ_TO_IPST(q);
9303 
9304 	ASSERT(q->q_next == NULL);
9305 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9306 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9307 	ASSERT(IAM_WRITER_IPIF(ipif));
9308 
9309 	connp = Q_TO_CONN(q);
9310 	/*
9311 	 * Special case for unplumbing lo0 (the loopback physical interface).
9312 	 * If unplumbing lo0, the incoming address structure has been
9313 	 * initialized to all zeros. When unplumbing lo0, all its logical
9314 	 * interfaces must be removed too.
9315 	 *
9316 	 * Note that this interface may be called to remove a specific
9317 	 * loopback logical interface (eg, lo0:1). But in that case
9318 	 * ipif->ipif_id != 0 so that the code path for that case is the
9319 	 * same as any other interface (meaning it skips the code directly
9320 	 * below).
9321 	 */
9322 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9323 		if (sin->sin_family == AF_UNSPEC &&
9324 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9325 			/*
9326 			 * Mark it condemned. No new ref. will be made to ill.
9327 			 */
9328 			mutex_enter(&ill->ill_lock);
9329 			ill->ill_state_flags |= ILL_CONDEMNED;
9330 			for (ipif = ill->ill_ipif; ipif != NULL;
9331 			    ipif = ipif->ipif_next) {
9332 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9333 			}
9334 			mutex_exit(&ill->ill_lock);
9335 
9336 			ipif = ill->ill_ipif;
9337 			/* unplumb the loopback interface */
9338 			ill_delete(ill);
9339 			mutex_enter(&connp->conn_lock);
9340 			mutex_enter(&ill->ill_lock);
9341 
9342 			/* Are any references to this ill active */
9343 			if (ill_is_freeable(ill)) {
9344 				mutex_exit(&ill->ill_lock);
9345 				mutex_exit(&connp->conn_lock);
9346 				ill_delete_tail(ill);
9347 				mi_free(ill);
9348 				return (0);
9349 			}
9350 			success = ipsq_pending_mp_add(connp, ipif,
9351 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9352 			mutex_exit(&connp->conn_lock);
9353 			mutex_exit(&ill->ill_lock);
9354 			if (success)
9355 				return (EINPROGRESS);
9356 			else
9357 				return (EINTR);
9358 		}
9359 	}
9360 
9361 	if (ipif->ipif_id == 0) {
9362 		ipsq_t *ipsq;
9363 
9364 		/* Find based on address */
9365 		if (ipif->ipif_isv6) {
9366 			sin6_t *sin6;
9367 
9368 			if (sin->sin_family != AF_INET6)
9369 				return (EAFNOSUPPORT);
9370 
9371 			sin6 = (sin6_t *)sin;
9372 			/* We are a writer, so we should be able to lookup */
9373 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9374 			    ipst);
9375 		} else {
9376 			if (sin->sin_family != AF_INET)
9377 				return (EAFNOSUPPORT);
9378 
9379 			/* We are a writer, so we should be able to lookup */
9380 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9381 			    ipst);
9382 		}
9383 		if (ipif == NULL) {
9384 			return (EADDRNOTAVAIL);
9385 		}
9386 
9387 		/*
9388 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9389 		 * lifr_name of the physical interface but with an ip address
9390 		 * lifr_addr of a logical interface plumbed over it.
9391 		 * So update ipx_current_ipif now that ipif points to the
9392 		 * correct one.
9393 		 */
9394 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9395 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9396 
9397 		/* This is a writer */
9398 		ipif_refrele(ipif);
9399 	}
9400 
9401 	/*
9402 	 * Can not delete instance zero since it is tied to the ill.
9403 	 */
9404 	if (ipif->ipif_id == 0)
9405 		return (EBUSY);
9406 
9407 	mutex_enter(&ill->ill_lock);
9408 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9409 	mutex_exit(&ill->ill_lock);
9410 
9411 	ipif_free(ipif);
9412 
9413 	mutex_enter(&connp->conn_lock);
9414 	mutex_enter(&ill->ill_lock);
9415 
9416 	/* Are any references to this ipif active */
9417 	if (ipif_is_freeable(ipif)) {
9418 		mutex_exit(&ill->ill_lock);
9419 		mutex_exit(&connp->conn_lock);
9420 		ipif_non_duplicate(ipif);
9421 		(void) ipif_down_tail(ipif);
9422 		ipif_free_tail(ipif); /* frees ipif */
9423 		return (0);
9424 	}
9425 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9426 	    IPIF_FREE);
9427 	mutex_exit(&ill->ill_lock);
9428 	mutex_exit(&connp->conn_lock);
9429 	if (success)
9430 		return (EINPROGRESS);
9431 	else
9432 		return (EINTR);
9433 }
9434 
9435 /*
9436  * Restart the removeif ioctl. The refcnt has gone down to 0.
9437  * The ipif is already condemned. So can't find it thru lookups.
9438  */
9439 /* ARGSUSED */
9440 int
9441 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9442     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9443 {
9444 	ill_t *ill = ipif->ipif_ill;
9445 
9446 	ASSERT(IAM_WRITER_IPIF(ipif));
9447 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9448 
9449 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9450 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9451 
9452 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9453 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9454 		ill_delete_tail(ill);
9455 		mi_free(ill);
9456 		return (0);
9457 	}
9458 
9459 	ipif_non_duplicate(ipif);
9460 	(void) ipif_down_tail(ipif);
9461 	ipif_free_tail(ipif);
9462 
9463 	return (0);
9464 }
9465 
9466 /*
9467  * Set the local interface address.
9468  * Allow an address of all zero when the interface is down.
9469  */
9470 /* ARGSUSED */
9471 int
9472 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9473     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9474 {
9475 	int err = 0;
9476 	in6_addr_t v6addr;
9477 	boolean_t need_up = B_FALSE;
9478 
9479 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9480 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9481 
9482 	ASSERT(IAM_WRITER_IPIF(ipif));
9483 
9484 	if (ipif->ipif_isv6) {
9485 		sin6_t *sin6;
9486 		ill_t *ill;
9487 		phyint_t *phyi;
9488 
9489 		if (sin->sin_family != AF_INET6)
9490 			return (EAFNOSUPPORT);
9491 
9492 		sin6 = (sin6_t *)sin;
9493 		v6addr = sin6->sin6_addr;
9494 		ill = ipif->ipif_ill;
9495 		phyi = ill->ill_phyint;
9496 
9497 		/*
9498 		 * Enforce that true multicast interfaces have a link-local
9499 		 * address for logical unit 0.
9500 		 */
9501 		if (ipif->ipif_id == 0 &&
9502 		    (ill->ill_flags & ILLF_MULTICAST) &&
9503 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9504 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9505 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9506 			return (EADDRNOTAVAIL);
9507 		}
9508 
9509 		/*
9510 		 * up interfaces shouldn't have the unspecified address
9511 		 * unless they also have the IPIF_NOLOCAL flags set and
9512 		 * have a subnet assigned.
9513 		 */
9514 		if ((ipif->ipif_flags & IPIF_UP) &&
9515 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9516 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9517 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9518 			return (EADDRNOTAVAIL);
9519 		}
9520 
9521 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9522 			return (EADDRNOTAVAIL);
9523 	} else {
9524 		ipaddr_t addr;
9525 
9526 		if (sin->sin_family != AF_INET)
9527 			return (EAFNOSUPPORT);
9528 
9529 		addr = sin->sin_addr.s_addr;
9530 
9531 		/* Allow 0 as the local address. */
9532 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9533 			return (EADDRNOTAVAIL);
9534 
9535 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9536 	}
9537 
9538 	/*
9539 	 * Even if there is no change we redo things just to rerun
9540 	 * ipif_set_default.
9541 	 */
9542 	if (ipif->ipif_flags & IPIF_UP) {
9543 		/*
9544 		 * Setting a new local address, make sure
9545 		 * we have net and subnet bcast ire's for
9546 		 * the old address if we need them.
9547 		 */
9548 		/*
9549 		 * If the interface is already marked up,
9550 		 * we call ipif_down which will take care
9551 		 * of ditching any IREs that have been set
9552 		 * up based on the old interface address.
9553 		 */
9554 		err = ipif_logical_down(ipif, q, mp);
9555 		if (err == EINPROGRESS)
9556 			return (err);
9557 		(void) ipif_down_tail(ipif);
9558 		need_up = 1;
9559 	}
9560 
9561 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9562 	return (err);
9563 }
9564 
9565 int
9566 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9567     boolean_t need_up)
9568 {
9569 	in6_addr_t v6addr;
9570 	in6_addr_t ov6addr;
9571 	ipaddr_t addr;
9572 	sin6_t	*sin6;
9573 	int	sinlen;
9574 	int	err = 0;
9575 	ill_t	*ill = ipif->ipif_ill;
9576 	boolean_t need_dl_down;
9577 	boolean_t need_arp_down;
9578 	struct iocblk *iocp;
9579 
9580 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9581 
9582 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9583 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9584 	ASSERT(IAM_WRITER_IPIF(ipif));
9585 
9586 	/* Must cancel any pending timer before taking the ill_lock */
9587 	if (ipif->ipif_recovery_id != 0)
9588 		(void) untimeout(ipif->ipif_recovery_id);
9589 	ipif->ipif_recovery_id = 0;
9590 
9591 	if (ipif->ipif_isv6) {
9592 		sin6 = (sin6_t *)sin;
9593 		v6addr = sin6->sin6_addr;
9594 		sinlen = sizeof (struct sockaddr_in6);
9595 	} else {
9596 		addr = sin->sin_addr.s_addr;
9597 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9598 		sinlen = sizeof (struct sockaddr_in);
9599 	}
9600 	mutex_enter(&ill->ill_lock);
9601 	ov6addr = ipif->ipif_v6lcl_addr;
9602 	ipif->ipif_v6lcl_addr = v6addr;
9603 	sctp_update_ipif_addr(ipif, ov6addr);
9604 	ipif->ipif_addr_ready = 0;
9605 
9606 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9607 
9608 	/*
9609 	 * If the interface was previously marked as a duplicate, then since
9610 	 * we've now got a "new" address, it should no longer be considered a
9611 	 * duplicate -- even if the "new" address is the same as the old one.
9612 	 * Note that if all ipifs are down, we may have a pending ARP down
9613 	 * event to handle.  This is because we want to recover from duplicates
9614 	 * and thus delay tearing down ARP until the duplicates have been
9615 	 * removed or disabled.
9616 	 */
9617 	need_dl_down = need_arp_down = B_FALSE;
9618 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9619 		need_arp_down = !need_up;
9620 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9621 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9622 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9623 			need_dl_down = B_TRUE;
9624 		}
9625 	}
9626 
9627 	ipif_set_default(ipif);
9628 
9629 	/*
9630 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9631 	 * tag the ill so that future updates to the interface ID don't result
9632 	 * in this address getting automatically reconfigured from under the
9633 	 * administrator.
9634 	 */
9635 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9636 		ill->ill_manual_linklocal = 1;
9637 
9638 	/*
9639 	 * When publishing an interface address change event, we only notify
9640 	 * the event listeners of the new address.  It is assumed that if they
9641 	 * actively care about the addresses assigned that they will have
9642 	 * already discovered the previous address assigned (if there was one.)
9643 	 *
9644 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9645 	 */
9646 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9647 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9648 		    NE_ADDRESS_CHANGE, sin, sinlen);
9649 	}
9650 
9651 	mutex_exit(&ill->ill_lock);
9652 
9653 	if (need_up) {
9654 		/*
9655 		 * Now bring the interface back up.  If this
9656 		 * is the only IPIF for the ILL, ipif_up
9657 		 * will have to re-bind to the device, so
9658 		 * we may get back EINPROGRESS, in which
9659 		 * case, this IOCTL will get completed in
9660 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9661 		 */
9662 		err = ipif_up(ipif, q, mp);
9663 	} else {
9664 		/* Perhaps ilgs should use this ill */
9665 		update_conn_ill(NULL, ill->ill_ipst);
9666 	}
9667 
9668 	if (need_dl_down)
9669 		ill_dl_down(ill);
9670 
9671 	if (need_arp_down && !ill->ill_isv6)
9672 		(void) ipif_arp_down(ipif);
9673 
9674 	/*
9675 	 * The default multicast interface might have changed (for
9676 	 * instance if the IPv6 scope of the address changed)
9677 	 */
9678 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9679 
9680 	return (err);
9681 }
9682 
9683 /*
9684  * Restart entry point to restart the address set operation after the
9685  * refcounts have dropped to zero.
9686  */
9687 /* ARGSUSED */
9688 int
9689 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9690     ip_ioctl_cmd_t *ipip, void *ifreq)
9691 {
9692 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9693 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9694 	ASSERT(IAM_WRITER_IPIF(ipif));
9695 	(void) ipif_down_tail(ipif);
9696 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9697 }
9698 
9699 /* ARGSUSED */
9700 int
9701 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9702     ip_ioctl_cmd_t *ipip, void *if_req)
9703 {
9704 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9705 	struct lifreq *lifr = (struct lifreq *)if_req;
9706 
9707 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9708 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9709 	/*
9710 	 * The net mask and address can't change since we have a
9711 	 * reference to the ipif. So no lock is necessary.
9712 	 */
9713 	if (ipif->ipif_isv6) {
9714 		*sin6 = sin6_null;
9715 		sin6->sin6_family = AF_INET6;
9716 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9717 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9718 		lifr->lifr_addrlen =
9719 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9720 	} else {
9721 		*sin = sin_null;
9722 		sin->sin_family = AF_INET;
9723 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9724 		if (ipip->ipi_cmd_type == LIF_CMD) {
9725 			lifr->lifr_addrlen =
9726 			    ip_mask_to_plen(ipif->ipif_net_mask);
9727 		}
9728 	}
9729 	return (0);
9730 }
9731 
9732 /*
9733  * Set the destination address for a pt-pt interface.
9734  */
9735 /* ARGSUSED */
9736 int
9737 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9738     ip_ioctl_cmd_t *ipip, void *if_req)
9739 {
9740 	int err = 0;
9741 	in6_addr_t v6addr;
9742 	boolean_t need_up = B_FALSE;
9743 
9744 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9745 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9746 	ASSERT(IAM_WRITER_IPIF(ipif));
9747 
9748 	if (ipif->ipif_isv6) {
9749 		sin6_t *sin6;
9750 
9751 		if (sin->sin_family != AF_INET6)
9752 			return (EAFNOSUPPORT);
9753 
9754 		sin6 = (sin6_t *)sin;
9755 		v6addr = sin6->sin6_addr;
9756 
9757 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9758 			return (EADDRNOTAVAIL);
9759 	} else {
9760 		ipaddr_t addr;
9761 
9762 		if (sin->sin_family != AF_INET)
9763 			return (EAFNOSUPPORT);
9764 
9765 		addr = sin->sin_addr.s_addr;
9766 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9767 			return (EADDRNOTAVAIL);
9768 
9769 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9770 	}
9771 
9772 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9773 		return (0);	/* No change */
9774 
9775 	if (ipif->ipif_flags & IPIF_UP) {
9776 		/*
9777 		 * If the interface is already marked up,
9778 		 * we call ipif_down which will take care
9779 		 * of ditching any IREs that have been set
9780 		 * up based on the old pp dst address.
9781 		 */
9782 		err = ipif_logical_down(ipif, q, mp);
9783 		if (err == EINPROGRESS)
9784 			return (err);
9785 		(void) ipif_down_tail(ipif);
9786 		need_up = B_TRUE;
9787 	}
9788 	/*
9789 	 * could return EINPROGRESS. If so ioctl will complete in
9790 	 * ip_rput_dlpi_writer
9791 	 */
9792 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9793 	return (err);
9794 }
9795 
9796 static int
9797 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9798     boolean_t need_up)
9799 {
9800 	in6_addr_t v6addr;
9801 	ill_t	*ill = ipif->ipif_ill;
9802 	int	err = 0;
9803 	boolean_t need_dl_down;
9804 	boolean_t need_arp_down;
9805 
9806 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9807 	    ipif->ipif_id, (void *)ipif));
9808 
9809 	/* Must cancel any pending timer before taking the ill_lock */
9810 	if (ipif->ipif_recovery_id != 0)
9811 		(void) untimeout(ipif->ipif_recovery_id);
9812 	ipif->ipif_recovery_id = 0;
9813 
9814 	if (ipif->ipif_isv6) {
9815 		sin6_t *sin6;
9816 
9817 		sin6 = (sin6_t *)sin;
9818 		v6addr = sin6->sin6_addr;
9819 	} else {
9820 		ipaddr_t addr;
9821 
9822 		addr = sin->sin_addr.s_addr;
9823 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9824 	}
9825 	mutex_enter(&ill->ill_lock);
9826 	/* Set point to point destination address. */
9827 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9828 		/*
9829 		 * Allow this as a means of creating logical
9830 		 * pt-pt interfaces on top of e.g. an Ethernet.
9831 		 * XXX Undocumented HACK for testing.
9832 		 * pt-pt interfaces are created with NUD disabled.
9833 		 */
9834 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9835 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9836 		if (ipif->ipif_isv6)
9837 			ill->ill_flags |= ILLF_NONUD;
9838 	}
9839 
9840 	/*
9841 	 * If the interface was previously marked as a duplicate, then since
9842 	 * we've now got a "new" address, it should no longer be considered a
9843 	 * duplicate -- even if the "new" address is the same as the old one.
9844 	 * Note that if all ipifs are down, we may have a pending ARP down
9845 	 * event to handle.
9846 	 */
9847 	need_dl_down = need_arp_down = B_FALSE;
9848 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9849 		need_arp_down = !need_up;
9850 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9851 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9852 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9853 			need_dl_down = B_TRUE;
9854 		}
9855 	}
9856 
9857 	/*
9858 	 * If we've just manually set the IPv6 destination link-local address
9859 	 * (0th ipif), tag the ill so that future updates to the destination
9860 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9861 	 * result in this address getting automatically reconfigured from
9862 	 * under the administrator.
9863 	 */
9864 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9865 		ill->ill_manual_dst_linklocal = 1;
9866 
9867 	/* Set the new address. */
9868 	ipif->ipif_v6pp_dst_addr = v6addr;
9869 	/* Make sure subnet tracks pp_dst */
9870 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9871 	mutex_exit(&ill->ill_lock);
9872 
9873 	if (need_up) {
9874 		/*
9875 		 * Now bring the interface back up.  If this
9876 		 * is the only IPIF for the ILL, ipif_up
9877 		 * will have to re-bind to the device, so
9878 		 * we may get back EINPROGRESS, in which
9879 		 * case, this IOCTL will get completed in
9880 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9881 		 */
9882 		err = ipif_up(ipif, q, mp);
9883 	}
9884 
9885 	if (need_dl_down)
9886 		ill_dl_down(ill);
9887 	if (need_arp_down && !ipif->ipif_isv6)
9888 		(void) ipif_arp_down(ipif);
9889 
9890 	return (err);
9891 }
9892 
9893 /*
9894  * Restart entry point to restart the dstaddress set operation after the
9895  * refcounts have dropped to zero.
9896  */
9897 /* ARGSUSED */
9898 int
9899 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9900     ip_ioctl_cmd_t *ipip, void *ifreq)
9901 {
9902 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9903 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9904 	(void) ipif_down_tail(ipif);
9905 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9906 }
9907 
9908 /* ARGSUSED */
9909 int
9910 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9911     ip_ioctl_cmd_t *ipip, void *if_req)
9912 {
9913 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9914 
9915 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9917 	/*
9918 	 * Get point to point destination address. The addresses can't
9919 	 * change since we hold a reference to the ipif.
9920 	 */
9921 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9922 		return (EADDRNOTAVAIL);
9923 
9924 	if (ipif->ipif_isv6) {
9925 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9926 		*sin6 = sin6_null;
9927 		sin6->sin6_family = AF_INET6;
9928 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9929 	} else {
9930 		*sin = sin_null;
9931 		sin->sin_family = AF_INET;
9932 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9933 	}
9934 	return (0);
9935 }
9936 
9937 /*
9938  * Check which flags will change by the given flags being set
9939  * silently ignore flags which userland is not allowed to control.
9940  * (Because these flags may change between SIOCGLIFFLAGS and
9941  * SIOCSLIFFLAGS, and that's outside of userland's control,
9942  * we need to silently ignore them rather than fail.)
9943  */
9944 static void
9945 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9946     uint64_t *offp)
9947 {
9948 	ill_t		*ill = ipif->ipif_ill;
9949 	phyint_t 	*phyi = ill->ill_phyint;
9950 	uint64_t	cantchange_flags, intf_flags;
9951 	uint64_t	turn_on, turn_off;
9952 
9953 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9954 	cantchange_flags = IFF_CANTCHANGE;
9955 	if (IS_IPMP(ill))
9956 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9957 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9958 	turn_off = intf_flags & turn_on;
9959 	turn_on ^= turn_off;
9960 	*onp = turn_on;
9961 	*offp = turn_off;
9962 }
9963 
9964 /*
9965  * Set interface flags.  Many flags require special handling (e.g.,
9966  * bringing the interface down); see below for details.
9967  *
9968  * NOTE : We really don't enforce that ipif_id zero should be used
9969  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9970  *	  is because applications generally does SICGLIFFLAGS and
9971  *	  ORs in the new flags (that affects the logical) and does a
9972  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9973  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9974  *	  flags that will be turned on is correct with respect to
9975  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9976  */
9977 /* ARGSUSED */
9978 int
9979 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9980     ip_ioctl_cmd_t *ipip, void *if_req)
9981 {
9982 	uint64_t turn_on;
9983 	uint64_t turn_off;
9984 	int	err = 0;
9985 	phyint_t *phyi;
9986 	ill_t *ill;
9987 	conn_t *connp;
9988 	uint64_t intf_flags;
9989 	boolean_t phyint_flags_modified = B_FALSE;
9990 	uint64_t flags;
9991 	struct ifreq *ifr;
9992 	struct lifreq *lifr;
9993 	boolean_t set_linklocal = B_FALSE;
9994 
9995 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9996 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9997 
9998 	ASSERT(IAM_WRITER_IPIF(ipif));
9999 
10000 	ill = ipif->ipif_ill;
10001 	phyi = ill->ill_phyint;
10002 
10003 	if (ipip->ipi_cmd_type == IF_CMD) {
10004 		ifr = (struct ifreq *)if_req;
10005 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10006 	} else {
10007 		lifr = (struct lifreq *)if_req;
10008 		flags = lifr->lifr_flags;
10009 	}
10010 
10011 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10012 
10013 	/*
10014 	 * Have the flags been set correctly until now?
10015 	 */
10016 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10017 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10018 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10019 	/*
10020 	 * Compare the new flags to the old, and partition
10021 	 * into those coming on and those going off.
10022 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10023 	 */
10024 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10025 		flags |= intf_flags & ~0xFFFF;
10026 
10027 	/*
10028 	 * Explicitly fail attempts to change flags that are always invalid on
10029 	 * an IPMP meta-interface.
10030 	 */
10031 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10032 		return (EINVAL);
10033 
10034 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10035 	if ((turn_on|turn_off) == 0)
10036 		return (0);	/* No change */
10037 
10038 	/*
10039 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10040 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10041 	 * allow it to be turned off.
10042 	 */
10043 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10044 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10045 		return (EINVAL);
10046 
10047 	if ((connp = Q_TO_CONN(q)) == NULL)
10048 		return (EINVAL);
10049 
10050 	/*
10051 	 * Only vrrp control socket is allowed to change IFF_UP and
10052 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10053 	 */
10054 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10055 		if (!connp->conn_isvrrp)
10056 			return (EINVAL);
10057 	}
10058 
10059 	/*
10060 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10061 	 * VRRP control socket.
10062 	 */
10063 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10064 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10065 			return (EINVAL);
10066 	}
10067 
10068 	if (turn_on & IFF_NOFAILOVER) {
10069 		turn_on |= IFF_DEPRECATED;
10070 		flags |= IFF_DEPRECATED;
10071 	}
10072 
10073 	/*
10074 	 * On underlying interfaces, only allow applications to manage test
10075 	 * addresses -- otherwise, they may get confused when the address
10076 	 * moves as part of being brought up.  Likewise, prevent an
10077 	 * application-managed test address from being converted to a data
10078 	 * address.  To prevent migration of administratively up addresses in
10079 	 * the kernel, we don't allow them to be converted either.
10080 	 */
10081 	if (IS_UNDER_IPMP(ill)) {
10082 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10083 
10084 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10085 			return (EINVAL);
10086 
10087 		if ((turn_off & IFF_NOFAILOVER) &&
10088 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10089 			return (EINVAL);
10090 	}
10091 
10092 	/*
10093 	 * Only allow IFF_TEMPORARY flag to be set on
10094 	 * IPv6 interfaces.
10095 	 */
10096 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10097 		return (EINVAL);
10098 
10099 	/*
10100 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10101 	 */
10102 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10103 		return (EINVAL);
10104 
10105 	/*
10106 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10107 	 * interfaces.  It makes no sense in that context.
10108 	 */
10109 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10110 		return (EINVAL);
10111 
10112 	/*
10113 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10114 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10115 	 * If the link local address isn't set, and can be set, it will get
10116 	 * set later on in this function.
10117 	 */
10118 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10119 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10120 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10121 		if (ipif_cant_setlinklocal(ipif))
10122 			return (EINVAL);
10123 		set_linklocal = B_TRUE;
10124 	}
10125 
10126 	/*
10127 	 * If we modify physical interface flags, we'll potentially need to
10128 	 * send up two routing socket messages for the changes (one for the
10129 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10130 	 */
10131 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10132 		phyint_flags_modified = B_TRUE;
10133 
10134 	/*
10135 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10136 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10137 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10138 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10139 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10140 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10141 	 * will not be honored.
10142 	 */
10143 	if (turn_on & PHYI_STANDBY) {
10144 		/*
10145 		 * No need to grab ill_g_usesrc_lock here; see the
10146 		 * synchronization notes in ip.c.
10147 		 */
10148 		if (ill->ill_usesrc_grp_next != NULL ||
10149 		    intf_flags & PHYI_INACTIVE)
10150 			return (EINVAL);
10151 		if (!(flags & PHYI_FAILED)) {
10152 			flags |= PHYI_INACTIVE;
10153 			turn_on |= PHYI_INACTIVE;
10154 		}
10155 	}
10156 
10157 	if (turn_off & PHYI_STANDBY) {
10158 		flags &= ~PHYI_INACTIVE;
10159 		turn_off |= PHYI_INACTIVE;
10160 	}
10161 
10162 	/*
10163 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10164 	 * would end up on.
10165 	 */
10166 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10167 	    (PHYI_FAILED | PHYI_INACTIVE))
10168 		return (EINVAL);
10169 
10170 	/*
10171 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10172 	 * status of the interface.
10173 	 */
10174 	if ((turn_on | turn_off) & ILLF_ROUTER)
10175 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10176 
10177 	/*
10178 	 * If the interface is not UP and we are not going to
10179 	 * bring it UP, record the flags and return. When the
10180 	 * interface comes UP later, the right actions will be
10181 	 * taken.
10182 	 */
10183 	if (!(ipif->ipif_flags & IPIF_UP) &&
10184 	    !(turn_on & IPIF_UP)) {
10185 		/* Record new flags in their respective places. */
10186 		mutex_enter(&ill->ill_lock);
10187 		mutex_enter(&ill->ill_phyint->phyint_lock);
10188 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10189 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10190 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10191 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10192 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10193 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10194 		mutex_exit(&ill->ill_lock);
10195 		mutex_exit(&ill->ill_phyint->phyint_lock);
10196 
10197 		/*
10198 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10199 		 * same to the kernel: if any of them has been set by
10200 		 * userland, the interface cannot be used for data traffic.
10201 		 */
10202 		if ((turn_on|turn_off) &
10203 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10204 			ASSERT(!IS_IPMP(ill));
10205 			/*
10206 			 * It's possible the ill is part of an "anonymous"
10207 			 * IPMP group rather than a real group.  In that case,
10208 			 * there are no other interfaces in the group and thus
10209 			 * no need to call ipmp_phyint_refresh_active().
10210 			 */
10211 			if (IS_UNDER_IPMP(ill))
10212 				ipmp_phyint_refresh_active(phyi);
10213 		}
10214 
10215 		if (phyint_flags_modified) {
10216 			if (phyi->phyint_illv4 != NULL) {
10217 				ip_rts_ifmsg(phyi->phyint_illv4->
10218 				    ill_ipif, RTSQ_DEFAULT);
10219 			}
10220 			if (phyi->phyint_illv6 != NULL) {
10221 				ip_rts_ifmsg(phyi->phyint_illv6->
10222 				    ill_ipif, RTSQ_DEFAULT);
10223 			}
10224 		}
10225 		/* The default multicast interface might have changed */
10226 		ire_increment_multicast_generation(ill->ill_ipst,
10227 		    ill->ill_isv6);
10228 
10229 		return (0);
10230 	} else if (set_linklocal) {
10231 		mutex_enter(&ill->ill_lock);
10232 		if (set_linklocal)
10233 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10234 		mutex_exit(&ill->ill_lock);
10235 	}
10236 
10237 	/*
10238 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10239 	 * or point-to-point interfaces with an unspecified destination. We do
10240 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10241 	 * have a subnet assigned, which is how in.ndpd currently manages its
10242 	 * onlink prefix list when no addresses are configured with those
10243 	 * prefixes.
10244 	 */
10245 	if (ipif->ipif_isv6 &&
10246 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10247 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10248 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10249 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10250 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10251 		return (EINVAL);
10252 	}
10253 
10254 	/*
10255 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10256 	 * from being brought up.
10257 	 */
10258 	if (!ipif->ipif_isv6 &&
10259 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10260 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10261 		return (EINVAL);
10262 	}
10263 
10264 	/*
10265 	 * If we are going to change one or more of the flags that are
10266 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10267 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10268 	 * IPIF_NOFAILOVER, we will take special action.  This is
10269 	 * done by bring the ipif down, changing the flags and bringing
10270 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10271 	 * back up will trigger the address to be moved.
10272 	 *
10273 	 * If we are going to change IFF_NOACCEPT, we need to bring
10274 	 * all the ipifs down then bring them up again.	 The act of
10275 	 * bringing all the ipifs back up will trigger the local
10276 	 * ires being recreated with "no_accept" set/cleared.
10277 	 *
10278 	 * Note that ILLF_NOACCEPT is always set separately from the
10279 	 * other flags.
10280 	 */
10281 	if ((turn_on|turn_off) &
10282 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10283 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10284 	    IPIF_NOFAILOVER)) {
10285 		/*
10286 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10287 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10288 		 * entries shared between multiple ipifs on the same subnet.
10289 		 */
10290 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10291 		    !(turn_off & IPIF_UP)) {
10292 			if (ipif->ipif_flags & IPIF_UP)
10293 				ill->ill_logical_down = 1;
10294 			turn_on &= ~IPIF_UP;
10295 		}
10296 		err = ipif_down(ipif, q, mp);
10297 		ip1dbg(("ipif_down returns %d err ", err));
10298 		if (err == EINPROGRESS)
10299 			return (err);
10300 		(void) ipif_down_tail(ipif);
10301 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10302 		/*
10303 		 * If we can quiesce the ill, then continue.  If not, then
10304 		 * ip_sioctl_flags_tail() will be called from
10305 		 * ipif_ill_refrele_tail().
10306 		 */
10307 		ill_down_ipifs(ill, B_TRUE);
10308 
10309 		mutex_enter(&connp->conn_lock);
10310 		mutex_enter(&ill->ill_lock);
10311 		if (!ill_is_quiescent(ill)) {
10312 			boolean_t success;
10313 
10314 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10315 			    q, mp, ILL_DOWN);
10316 			mutex_exit(&ill->ill_lock);
10317 			mutex_exit(&connp->conn_lock);
10318 			return (success ? EINPROGRESS : EINTR);
10319 		}
10320 		mutex_exit(&ill->ill_lock);
10321 		mutex_exit(&connp->conn_lock);
10322 	}
10323 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10324 }
10325 
10326 static int
10327 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10328 {
10329 	ill_t	*ill;
10330 	phyint_t *phyi;
10331 	uint64_t turn_on, turn_off;
10332 	boolean_t phyint_flags_modified = B_FALSE;
10333 	int	err = 0;
10334 	boolean_t set_linklocal = B_FALSE;
10335 
10336 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10337 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10338 
10339 	ASSERT(IAM_WRITER_IPIF(ipif));
10340 
10341 	ill = ipif->ipif_ill;
10342 	phyi = ill->ill_phyint;
10343 
10344 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10345 
10346 	/*
10347 	 * IFF_UP is handled separately.
10348 	 */
10349 	turn_on &= ~IFF_UP;
10350 	turn_off &= ~IFF_UP;
10351 
10352 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10353 		phyint_flags_modified = B_TRUE;
10354 
10355 	/*
10356 	 * Now we change the flags. Track current value of
10357 	 * other flags in their respective places.
10358 	 */
10359 	mutex_enter(&ill->ill_lock);
10360 	mutex_enter(&phyi->phyint_lock);
10361 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10362 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10363 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10364 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10365 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10366 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10367 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10368 		set_linklocal = B_TRUE;
10369 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10370 	}
10371 
10372 	mutex_exit(&ill->ill_lock);
10373 	mutex_exit(&phyi->phyint_lock);
10374 
10375 	if (set_linklocal)
10376 		(void) ipif_setlinklocal(ipif);
10377 
10378 	/*
10379 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10380 	 * the kernel: if any of them has been set by userland, the interface
10381 	 * cannot be used for data traffic.
10382 	 */
10383 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10384 		ASSERT(!IS_IPMP(ill));
10385 		/*
10386 		 * It's possible the ill is part of an "anonymous" IPMP group
10387 		 * rather than a real group.  In that case, there are no other
10388 		 * interfaces in the group and thus no need for us to call
10389 		 * ipmp_phyint_refresh_active().
10390 		 */
10391 		if (IS_UNDER_IPMP(ill))
10392 			ipmp_phyint_refresh_active(phyi);
10393 	}
10394 
10395 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10396 		/*
10397 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10398 		 * ipifs that were brought down.
10399 		 *
10400 		 * The routing sockets messages are sent as the result
10401 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10402 		 * as well.
10403 		 */
10404 		err = ill_up_ipifs(ill, q, mp);
10405 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10406 		/*
10407 		 * XXX ipif_up really does not know whether a phyint flags
10408 		 * was modified or not. So, it sends up information on
10409 		 * only one routing sockets message. As we don't bring up
10410 		 * the interface and also set PHYI_ flags simultaneously
10411 		 * it should be okay.
10412 		 */
10413 		err = ipif_up(ipif, q, mp);
10414 	} else {
10415 		/*
10416 		 * Make sure routing socket sees all changes to the flags.
10417 		 * ipif_up_done* handles this when we use ipif_up.
10418 		 */
10419 		if (phyint_flags_modified) {
10420 			if (phyi->phyint_illv4 != NULL) {
10421 				ip_rts_ifmsg(phyi->phyint_illv4->
10422 				    ill_ipif, RTSQ_DEFAULT);
10423 			}
10424 			if (phyi->phyint_illv6 != NULL) {
10425 				ip_rts_ifmsg(phyi->phyint_illv6->
10426 				    ill_ipif, RTSQ_DEFAULT);
10427 			}
10428 		} else {
10429 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10430 		}
10431 		/*
10432 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10433 		 * this in need_up case.
10434 		 */
10435 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10436 	}
10437 
10438 	/* The default multicast interface might have changed */
10439 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10440 	return (err);
10441 }
10442 
10443 /*
10444  * Restart the flags operation now that the refcounts have dropped to zero.
10445  */
10446 /* ARGSUSED */
10447 int
10448 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10449     ip_ioctl_cmd_t *ipip, void *if_req)
10450 {
10451 	uint64_t flags;
10452 	struct ifreq *ifr = if_req;
10453 	struct lifreq *lifr = if_req;
10454 	uint64_t turn_on, turn_off;
10455 
10456 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10458 
10459 	if (ipip->ipi_cmd_type == IF_CMD) {
10460 		/* cast to uint16_t prevents unwanted sign extension */
10461 		flags = (uint16_t)ifr->ifr_flags;
10462 	} else {
10463 		flags = lifr->lifr_flags;
10464 	}
10465 
10466 	/*
10467 	 * If this function call is a result of the ILLF_NOACCEPT flag
10468 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10469 	 */
10470 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10471 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10472 		(void) ipif_down_tail(ipif);
10473 
10474 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10475 }
10476 
10477 /*
10478  * Can operate on either a module or a driver queue.
10479  */
10480 /* ARGSUSED */
10481 int
10482 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10483     ip_ioctl_cmd_t *ipip, void *if_req)
10484 {
10485 	/*
10486 	 * Has the flags been set correctly till now ?
10487 	 */
10488 	ill_t *ill = ipif->ipif_ill;
10489 	phyint_t *phyi = ill->ill_phyint;
10490 
10491 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10492 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10493 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10494 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10495 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10496 
10497 	/*
10498 	 * Need a lock since some flags can be set even when there are
10499 	 * references to the ipif.
10500 	 */
10501 	mutex_enter(&ill->ill_lock);
10502 	if (ipip->ipi_cmd_type == IF_CMD) {
10503 		struct ifreq *ifr = (struct ifreq *)if_req;
10504 
10505 		/* Get interface flags (low 16 only). */
10506 		ifr->ifr_flags = ((ipif->ipif_flags |
10507 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10508 	} else {
10509 		struct lifreq *lifr = (struct lifreq *)if_req;
10510 
10511 		/* Get interface flags. */
10512 		lifr->lifr_flags = ipif->ipif_flags |
10513 		    ill->ill_flags | phyi->phyint_flags;
10514 	}
10515 	mutex_exit(&ill->ill_lock);
10516 	return (0);
10517 }
10518 
10519 /*
10520  * We allow the MTU to be set on an ILL, but not have it be different
10521  * for different IPIFs since we don't actually send packets on IPIFs.
10522  */
10523 /* ARGSUSED */
10524 int
10525 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10526     ip_ioctl_cmd_t *ipip, void *if_req)
10527 {
10528 	int mtu;
10529 	int ip_min_mtu;
10530 	struct ifreq	*ifr;
10531 	struct lifreq *lifr;
10532 	ill_t	*ill;
10533 
10534 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10535 	    ipif->ipif_id, (void *)ipif));
10536 	if (ipip->ipi_cmd_type == IF_CMD) {
10537 		ifr = (struct ifreq *)if_req;
10538 		mtu = ifr->ifr_metric;
10539 	} else {
10540 		lifr = (struct lifreq *)if_req;
10541 		mtu = lifr->lifr_mtu;
10542 	}
10543 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10544 	if (ipif->ipif_id != 0)
10545 		return (EINVAL);
10546 
10547 	ill = ipif->ipif_ill;
10548 	if (ipif->ipif_isv6)
10549 		ip_min_mtu = IPV6_MIN_MTU;
10550 	else
10551 		ip_min_mtu = IP_MIN_MTU;
10552 
10553 	mutex_enter(&ill->ill_lock);
10554 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10555 		mutex_exit(&ill->ill_lock);
10556 		return (EINVAL);
10557 	}
10558 	/*
10559 	 * The dce and fragmentation code can handle changes to ill_mtu
10560 	 * concurrent with sending/fragmenting packets.
10561 	 */
10562 	ill->ill_mtu = mtu;
10563 	ill->ill_flags |= ILLF_FIXEDMTU;
10564 	mutex_exit(&ill->ill_lock);
10565 
10566 	/*
10567 	 * Make sure all dce_generation checks find out
10568 	 * that ill_mtu has changed.
10569 	 */
10570 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10571 
10572 	/* Update the MTU in SCTP's list */
10573 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10574 	return (0);
10575 }
10576 
10577 /* Get interface MTU. */
10578 /* ARGSUSED */
10579 int
10580 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10581 	ip_ioctl_cmd_t *ipip, void *if_req)
10582 {
10583 	struct ifreq	*ifr;
10584 	struct lifreq	*lifr;
10585 
10586 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10587 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10588 
10589 	/*
10590 	 * We allow a get on any logical interface even though the set
10591 	 * can only be done on logical unit 0.
10592 	 */
10593 	if (ipip->ipi_cmd_type == IF_CMD) {
10594 		ifr = (struct ifreq *)if_req;
10595 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10596 	} else {
10597 		lifr = (struct lifreq *)if_req;
10598 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10599 	}
10600 	return (0);
10601 }
10602 
10603 /* Set interface broadcast address. */
10604 /* ARGSUSED2 */
10605 int
10606 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10607 	ip_ioctl_cmd_t *ipip, void *if_req)
10608 {
10609 	ipaddr_t addr;
10610 	ire_t	*ire;
10611 	ill_t		*ill = ipif->ipif_ill;
10612 	ip_stack_t	*ipst = ill->ill_ipst;
10613 
10614 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10615 	    ipif->ipif_id));
10616 
10617 	ASSERT(IAM_WRITER_IPIF(ipif));
10618 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10619 		return (EADDRNOTAVAIL);
10620 
10621 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10622 
10623 	if (sin->sin_family != AF_INET)
10624 		return (EAFNOSUPPORT);
10625 
10626 	addr = sin->sin_addr.s_addr;
10627 	if (ipif->ipif_flags & IPIF_UP) {
10628 		/*
10629 		 * If we are already up, make sure the new
10630 		 * broadcast address makes sense.  If it does,
10631 		 * there should be an IRE for it already.
10632 		 */
10633 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10634 		    ill, ipif->ipif_zoneid, NULL,
10635 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10636 		if (ire == NULL) {
10637 			return (EINVAL);
10638 		} else {
10639 			ire_refrele(ire);
10640 		}
10641 	}
10642 	/*
10643 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10644 	 * needs to already exist we never need to change the set of
10645 	 * IRE_BROADCASTs when we are UP.
10646 	 */
10647 	if (addr != ipif->ipif_brd_addr)
10648 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10649 
10650 	return (0);
10651 }
10652 
10653 /* Get interface broadcast address. */
10654 /* ARGSUSED */
10655 int
10656 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10657     ip_ioctl_cmd_t *ipip, void *if_req)
10658 {
10659 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10660 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10661 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10662 		return (EADDRNOTAVAIL);
10663 
10664 	/* IPIF_BROADCAST not possible with IPv6 */
10665 	ASSERT(!ipif->ipif_isv6);
10666 	*sin = sin_null;
10667 	sin->sin_family = AF_INET;
10668 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10669 	return (0);
10670 }
10671 
10672 /*
10673  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10674  */
10675 /* ARGSUSED */
10676 int
10677 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10678     ip_ioctl_cmd_t *ipip, void *if_req)
10679 {
10680 	int err = 0;
10681 	in6_addr_t v6mask;
10682 
10683 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10684 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10685 
10686 	ASSERT(IAM_WRITER_IPIF(ipif));
10687 
10688 	if (ipif->ipif_isv6) {
10689 		sin6_t *sin6;
10690 
10691 		if (sin->sin_family != AF_INET6)
10692 			return (EAFNOSUPPORT);
10693 
10694 		sin6 = (sin6_t *)sin;
10695 		v6mask = sin6->sin6_addr;
10696 	} else {
10697 		ipaddr_t mask;
10698 
10699 		if (sin->sin_family != AF_INET)
10700 			return (EAFNOSUPPORT);
10701 
10702 		mask = sin->sin_addr.s_addr;
10703 		V4MASK_TO_V6(mask, v6mask);
10704 	}
10705 
10706 	/*
10707 	 * No big deal if the interface isn't already up, or the mask
10708 	 * isn't really changing, or this is pt-pt.
10709 	 */
10710 	if (!(ipif->ipif_flags & IPIF_UP) ||
10711 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10712 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10713 		ipif->ipif_v6net_mask = v6mask;
10714 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10715 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10716 			    ipif->ipif_v6net_mask,
10717 			    ipif->ipif_v6subnet);
10718 		}
10719 		return (0);
10720 	}
10721 	/*
10722 	 * Make sure we have valid net and subnet broadcast ire's
10723 	 * for the old netmask, if needed by other logical interfaces.
10724 	 */
10725 	err = ipif_logical_down(ipif, q, mp);
10726 	if (err == EINPROGRESS)
10727 		return (err);
10728 	(void) ipif_down_tail(ipif);
10729 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10730 	return (err);
10731 }
10732 
10733 static int
10734 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10735 {
10736 	in6_addr_t v6mask;
10737 	int err = 0;
10738 
10739 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10740 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10741 
10742 	if (ipif->ipif_isv6) {
10743 		sin6_t *sin6;
10744 
10745 		sin6 = (sin6_t *)sin;
10746 		v6mask = sin6->sin6_addr;
10747 	} else {
10748 		ipaddr_t mask;
10749 
10750 		mask = sin->sin_addr.s_addr;
10751 		V4MASK_TO_V6(mask, v6mask);
10752 	}
10753 
10754 	ipif->ipif_v6net_mask = v6mask;
10755 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10756 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10757 		    ipif->ipif_v6subnet);
10758 	}
10759 	err = ipif_up(ipif, q, mp);
10760 
10761 	if (err == 0 || err == EINPROGRESS) {
10762 		/*
10763 		 * The interface must be DL_BOUND if this packet has to
10764 		 * go out on the wire. Since we only go through a logical
10765 		 * down and are bound with the driver during an internal
10766 		 * down/up that is satisfied.
10767 		 */
10768 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10769 			/* Potentially broadcast an address mask reply. */
10770 			ipif_mask_reply(ipif);
10771 		}
10772 	}
10773 	return (err);
10774 }
10775 
10776 /* ARGSUSED */
10777 int
10778 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10779     ip_ioctl_cmd_t *ipip, void *if_req)
10780 {
10781 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10782 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10783 	(void) ipif_down_tail(ipif);
10784 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10785 }
10786 
10787 /* Get interface net mask. */
10788 /* ARGSUSED */
10789 int
10790 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10791     ip_ioctl_cmd_t *ipip, void *if_req)
10792 {
10793 	struct lifreq *lifr = (struct lifreq *)if_req;
10794 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10795 
10796 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10797 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10798 
10799 	/*
10800 	 * net mask can't change since we have a reference to the ipif.
10801 	 */
10802 	if (ipif->ipif_isv6) {
10803 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10804 		*sin6 = sin6_null;
10805 		sin6->sin6_family = AF_INET6;
10806 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10807 		lifr->lifr_addrlen =
10808 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10809 	} else {
10810 		*sin = sin_null;
10811 		sin->sin_family = AF_INET;
10812 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10813 		if (ipip->ipi_cmd_type == LIF_CMD) {
10814 			lifr->lifr_addrlen =
10815 			    ip_mask_to_plen(ipif->ipif_net_mask);
10816 		}
10817 	}
10818 	return (0);
10819 }
10820 
10821 /* ARGSUSED */
10822 int
10823 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10824     ip_ioctl_cmd_t *ipip, void *if_req)
10825 {
10826 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10827 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10828 
10829 	/*
10830 	 * Since no applications should ever be setting metrics on underlying
10831 	 * interfaces, we explicitly fail to smoke 'em out.
10832 	 */
10833 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10834 		return (EINVAL);
10835 
10836 	/*
10837 	 * Set interface metric.  We don't use this for
10838 	 * anything but we keep track of it in case it is
10839 	 * important to routing applications or such.
10840 	 */
10841 	if (ipip->ipi_cmd_type == IF_CMD) {
10842 		struct ifreq    *ifr;
10843 
10844 		ifr = (struct ifreq *)if_req;
10845 		ipif->ipif_metric = ifr->ifr_metric;
10846 	} else {
10847 		struct lifreq   *lifr;
10848 
10849 		lifr = (struct lifreq *)if_req;
10850 		ipif->ipif_metric = lifr->lifr_metric;
10851 	}
10852 	return (0);
10853 }
10854 
10855 /* ARGSUSED */
10856 int
10857 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10858     ip_ioctl_cmd_t *ipip, void *if_req)
10859 {
10860 	/* Get interface metric. */
10861 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10862 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10863 
10864 	if (ipip->ipi_cmd_type == IF_CMD) {
10865 		struct ifreq    *ifr;
10866 
10867 		ifr = (struct ifreq *)if_req;
10868 		ifr->ifr_metric = ipif->ipif_metric;
10869 	} else {
10870 		struct lifreq   *lifr;
10871 
10872 		lifr = (struct lifreq *)if_req;
10873 		lifr->lifr_metric = ipif->ipif_metric;
10874 	}
10875 
10876 	return (0);
10877 }
10878 
10879 /* ARGSUSED */
10880 int
10881 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10882     ip_ioctl_cmd_t *ipip, void *if_req)
10883 {
10884 	int	arp_muxid;
10885 
10886 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10887 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10888 	/*
10889 	 * Set the muxid returned from I_PLINK.
10890 	 */
10891 	if (ipip->ipi_cmd_type == IF_CMD) {
10892 		struct ifreq *ifr = (struct ifreq *)if_req;
10893 
10894 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10895 		arp_muxid = ifr->ifr_arp_muxid;
10896 	} else {
10897 		struct lifreq *lifr = (struct lifreq *)if_req;
10898 
10899 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10900 		arp_muxid = lifr->lifr_arp_muxid;
10901 	}
10902 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10903 	return (0);
10904 }
10905 
10906 /* ARGSUSED */
10907 int
10908 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10909     ip_ioctl_cmd_t *ipip, void *if_req)
10910 {
10911 	int	arp_muxid = 0;
10912 
10913 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10914 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10915 	/*
10916 	 * Get the muxid saved in ill for I_PUNLINK.
10917 	 */
10918 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10919 	if (ipip->ipi_cmd_type == IF_CMD) {
10920 		struct ifreq *ifr = (struct ifreq *)if_req;
10921 
10922 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10923 		ifr->ifr_arp_muxid = arp_muxid;
10924 	} else {
10925 		struct lifreq *lifr = (struct lifreq *)if_req;
10926 
10927 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10928 		lifr->lifr_arp_muxid = arp_muxid;
10929 	}
10930 	return (0);
10931 }
10932 
10933 /*
10934  * Set the subnet prefix. Does not modify the broadcast address.
10935  */
10936 /* ARGSUSED */
10937 int
10938 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10939     ip_ioctl_cmd_t *ipip, void *if_req)
10940 {
10941 	int err = 0;
10942 	in6_addr_t v6addr;
10943 	in6_addr_t v6mask;
10944 	boolean_t need_up = B_FALSE;
10945 	int addrlen;
10946 
10947 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10948 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10949 
10950 	ASSERT(IAM_WRITER_IPIF(ipif));
10951 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10952 
10953 	if (ipif->ipif_isv6) {
10954 		sin6_t *sin6;
10955 
10956 		if (sin->sin_family != AF_INET6)
10957 			return (EAFNOSUPPORT);
10958 
10959 		sin6 = (sin6_t *)sin;
10960 		v6addr = sin6->sin6_addr;
10961 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10962 			return (EADDRNOTAVAIL);
10963 	} else {
10964 		ipaddr_t addr;
10965 
10966 		if (sin->sin_family != AF_INET)
10967 			return (EAFNOSUPPORT);
10968 
10969 		addr = sin->sin_addr.s_addr;
10970 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10971 			return (EADDRNOTAVAIL);
10972 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10973 		/* Add 96 bits */
10974 		addrlen += IPV6_ABITS - IP_ABITS;
10975 	}
10976 
10977 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10978 		return (EINVAL);
10979 
10980 	/* Check if bits in the address is set past the mask */
10981 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10982 		return (EINVAL);
10983 
10984 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10985 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10986 		return (0);	/* No change */
10987 
10988 	if (ipif->ipif_flags & IPIF_UP) {
10989 		/*
10990 		 * If the interface is already marked up,
10991 		 * we call ipif_down which will take care
10992 		 * of ditching any IREs that have been set
10993 		 * up based on the old interface address.
10994 		 */
10995 		err = ipif_logical_down(ipif, q, mp);
10996 		if (err == EINPROGRESS)
10997 			return (err);
10998 		(void) ipif_down_tail(ipif);
10999 		need_up = B_TRUE;
11000 	}
11001 
11002 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11003 	return (err);
11004 }
11005 
11006 static int
11007 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11008     queue_t *q, mblk_t *mp, boolean_t need_up)
11009 {
11010 	ill_t	*ill = ipif->ipif_ill;
11011 	int	err = 0;
11012 
11013 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11014 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11015 
11016 	/* Set the new address. */
11017 	mutex_enter(&ill->ill_lock);
11018 	ipif->ipif_v6net_mask = v6mask;
11019 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11020 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11021 		    ipif->ipif_v6subnet);
11022 	}
11023 	mutex_exit(&ill->ill_lock);
11024 
11025 	if (need_up) {
11026 		/*
11027 		 * Now bring the interface back up.  If this
11028 		 * is the only IPIF for the ILL, ipif_up
11029 		 * will have to re-bind to the device, so
11030 		 * we may get back EINPROGRESS, in which
11031 		 * case, this IOCTL will get completed in
11032 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11033 		 */
11034 		err = ipif_up(ipif, q, mp);
11035 		if (err == EINPROGRESS)
11036 			return (err);
11037 	}
11038 	return (err);
11039 }
11040 
11041 /* ARGSUSED */
11042 int
11043 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11044     ip_ioctl_cmd_t *ipip, void *if_req)
11045 {
11046 	int	addrlen;
11047 	in6_addr_t v6addr;
11048 	in6_addr_t v6mask;
11049 	struct lifreq *lifr = (struct lifreq *)if_req;
11050 
11051 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11052 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11053 	(void) ipif_down_tail(ipif);
11054 
11055 	addrlen = lifr->lifr_addrlen;
11056 	if (ipif->ipif_isv6) {
11057 		sin6_t *sin6;
11058 
11059 		sin6 = (sin6_t *)sin;
11060 		v6addr = sin6->sin6_addr;
11061 	} else {
11062 		ipaddr_t addr;
11063 
11064 		addr = sin->sin_addr.s_addr;
11065 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11066 		addrlen += IPV6_ABITS - IP_ABITS;
11067 	}
11068 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11069 
11070 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11071 }
11072 
11073 /* ARGSUSED */
11074 int
11075 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11076     ip_ioctl_cmd_t *ipip, void *if_req)
11077 {
11078 	struct lifreq *lifr = (struct lifreq *)if_req;
11079 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11080 
11081 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11082 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11083 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11084 
11085 	if (ipif->ipif_isv6) {
11086 		*sin6 = sin6_null;
11087 		sin6->sin6_family = AF_INET6;
11088 		sin6->sin6_addr = ipif->ipif_v6subnet;
11089 		lifr->lifr_addrlen =
11090 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11091 	} else {
11092 		*sin = sin_null;
11093 		sin->sin_family = AF_INET;
11094 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11095 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11096 	}
11097 	return (0);
11098 }
11099 
11100 /*
11101  * Set the IPv6 address token.
11102  */
11103 /* ARGSUSED */
11104 int
11105 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11106     ip_ioctl_cmd_t *ipi, void *if_req)
11107 {
11108 	ill_t *ill = ipif->ipif_ill;
11109 	int err;
11110 	in6_addr_t v6addr;
11111 	in6_addr_t v6mask;
11112 	boolean_t need_up = B_FALSE;
11113 	int i;
11114 	sin6_t *sin6 = (sin6_t *)sin;
11115 	struct lifreq *lifr = (struct lifreq *)if_req;
11116 	int addrlen;
11117 
11118 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11119 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11120 	ASSERT(IAM_WRITER_IPIF(ipif));
11121 
11122 	addrlen = lifr->lifr_addrlen;
11123 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11124 	if (ipif->ipif_id != 0)
11125 		return (EINVAL);
11126 
11127 	if (!ipif->ipif_isv6)
11128 		return (EINVAL);
11129 
11130 	if (addrlen > IPV6_ABITS)
11131 		return (EINVAL);
11132 
11133 	v6addr = sin6->sin6_addr;
11134 
11135 	/*
11136 	 * The length of the token is the length from the end.  To get
11137 	 * the proper mask for this, compute the mask of the bits not
11138 	 * in the token; ie. the prefix, and then xor to get the mask.
11139 	 */
11140 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11141 		return (EINVAL);
11142 	for (i = 0; i < 4; i++) {
11143 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11144 	}
11145 
11146 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11147 	    ill->ill_token_length == addrlen)
11148 		return (0);	/* No change */
11149 
11150 	if (ipif->ipif_flags & IPIF_UP) {
11151 		err = ipif_logical_down(ipif, q, mp);
11152 		if (err == EINPROGRESS)
11153 			return (err);
11154 		(void) ipif_down_tail(ipif);
11155 		need_up = B_TRUE;
11156 	}
11157 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11158 	return (err);
11159 }
11160 
11161 static int
11162 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11163     mblk_t *mp, boolean_t need_up)
11164 {
11165 	in6_addr_t v6addr;
11166 	in6_addr_t v6mask;
11167 	ill_t	*ill = ipif->ipif_ill;
11168 	int	i;
11169 	int	err = 0;
11170 
11171 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11172 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11173 	v6addr = sin6->sin6_addr;
11174 	/*
11175 	 * The length of the token is the length from the end.  To get
11176 	 * the proper mask for this, compute the mask of the bits not
11177 	 * in the token; ie. the prefix, and then xor to get the mask.
11178 	 */
11179 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11180 	for (i = 0; i < 4; i++)
11181 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11182 
11183 	mutex_enter(&ill->ill_lock);
11184 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11185 	ill->ill_token_length = addrlen;
11186 	ill->ill_manual_token = 1;
11187 
11188 	/* Reconfigure the link-local address based on this new token */
11189 	ipif_setlinklocal(ill->ill_ipif);
11190 
11191 	mutex_exit(&ill->ill_lock);
11192 
11193 	if (need_up) {
11194 		/*
11195 		 * Now bring the interface back up.  If this
11196 		 * is the only IPIF for the ILL, ipif_up
11197 		 * will have to re-bind to the device, so
11198 		 * we may get back EINPROGRESS, in which
11199 		 * case, this IOCTL will get completed in
11200 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11201 		 */
11202 		err = ipif_up(ipif, q, mp);
11203 		if (err == EINPROGRESS)
11204 			return (err);
11205 	}
11206 	return (err);
11207 }
11208 
11209 /* ARGSUSED */
11210 int
11211 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11212     ip_ioctl_cmd_t *ipi, void *if_req)
11213 {
11214 	ill_t *ill;
11215 	sin6_t *sin6 = (sin6_t *)sin;
11216 	struct lifreq *lifr = (struct lifreq *)if_req;
11217 
11218 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11219 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11220 	if (ipif->ipif_id != 0)
11221 		return (EINVAL);
11222 
11223 	ill = ipif->ipif_ill;
11224 	if (!ill->ill_isv6)
11225 		return (ENXIO);
11226 
11227 	*sin6 = sin6_null;
11228 	sin6->sin6_family = AF_INET6;
11229 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11230 	sin6->sin6_addr = ill->ill_token;
11231 	lifr->lifr_addrlen = ill->ill_token_length;
11232 	return (0);
11233 }
11234 
11235 /*
11236  * Set (hardware) link specific information that might override
11237  * what was acquired through the DL_INFO_ACK.
11238  */
11239 /* ARGSUSED */
11240 int
11241 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11242     ip_ioctl_cmd_t *ipi, void *if_req)
11243 {
11244 	ill_t		*ill = ipif->ipif_ill;
11245 	int		ip_min_mtu;
11246 	struct lifreq	*lifr = (struct lifreq *)if_req;
11247 	lif_ifinfo_req_t *lir;
11248 
11249 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11250 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11251 	lir = &lifr->lifr_ifinfo;
11252 	ASSERT(IAM_WRITER_IPIF(ipif));
11253 
11254 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11255 	if (ipif->ipif_id != 0)
11256 		return (EINVAL);
11257 
11258 	/* Set interface MTU. */
11259 	if (ipif->ipif_isv6)
11260 		ip_min_mtu = IPV6_MIN_MTU;
11261 	else
11262 		ip_min_mtu = IP_MIN_MTU;
11263 
11264 	/*
11265 	 * Verify values before we set anything. Allow zero to
11266 	 * mean unspecified.
11267 	 *
11268 	 * XXX We should be able to set the user-defined lir_mtu to some value
11269 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11270 	 * ill_max_frag value tells us the max MTU that can be handled by the
11271 	 * datalink, whereas the ill_current_frag is dynamically computed for
11272 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11273 	 * since there is currently no way of distinguishing between
11274 	 * administratively fixed link mtu values (e.g., those set via
11275 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11276 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11277 	 * upper-bound.
11278 	 */
11279 	if (lir->lir_maxmtu != 0 &&
11280 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11281 	    lir->lir_maxmtu < ip_min_mtu))
11282 		return (EINVAL);
11283 	if (lir->lir_reachtime != 0 &&
11284 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11285 		return (EINVAL);
11286 	if (lir->lir_reachretrans != 0 &&
11287 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11288 		return (EINVAL);
11289 
11290 	mutex_enter(&ill->ill_lock);
11291 	/*
11292 	 * The dce and fragmentation code can handle changes to ill_mtu
11293 	 * concurrent with sending/fragmenting packets.
11294 	 */
11295 	if (lir->lir_maxmtu != 0)
11296 		ill->ill_user_mtu = lir->lir_maxmtu;
11297 
11298 	if (lir->lir_reachtime != 0)
11299 		ill->ill_reachable_time = lir->lir_reachtime;
11300 
11301 	if (lir->lir_reachretrans != 0)
11302 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11303 
11304 	ill->ill_max_hops = lir->lir_maxhops;
11305 	ill->ill_max_buf = ND_MAX_Q;
11306 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11307 		/*
11308 		 * ill_mtu is the actual interface MTU, obtained as the min
11309 		 * of user-configured mtu and the value announced by the
11310 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11311 		 * we have already made the choice of requiring
11312 		 * ill_user_mtu < ill_current_frag by the time we get here,
11313 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11314 		 * here.
11315 		 */
11316 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11317 	}
11318 	mutex_exit(&ill->ill_lock);
11319 
11320 	/*
11321 	 * Make sure all dce_generation checks find out
11322 	 * that ill_mtu has changed.
11323 	 */
11324 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11325 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11326 
11327 	/*
11328 	 * Refresh IPMP meta-interface MTU if necessary.
11329 	 */
11330 	if (IS_UNDER_IPMP(ill))
11331 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11332 
11333 	return (0);
11334 }
11335 
11336 /* ARGSUSED */
11337 int
11338 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11339     ip_ioctl_cmd_t *ipi, void *if_req)
11340 {
11341 	struct lif_ifinfo_req *lir;
11342 	ill_t *ill = ipif->ipif_ill;
11343 
11344 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11345 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11346 	if (ipif->ipif_id != 0)
11347 		return (EINVAL);
11348 
11349 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11350 	lir->lir_maxhops = ill->ill_max_hops;
11351 	lir->lir_reachtime = ill->ill_reachable_time;
11352 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11353 	lir->lir_maxmtu = ill->ill_mtu;
11354 
11355 	return (0);
11356 }
11357 
11358 /*
11359  * Return best guess as to the subnet mask for the specified address.
11360  * Based on the subnet masks for all the configured interfaces.
11361  *
11362  * We end up returning a zero mask in the case of default, multicast or
11363  * experimental.
11364  */
11365 static ipaddr_t
11366 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11367 {
11368 	ipaddr_t net_mask;
11369 	ill_t	*ill;
11370 	ipif_t	*ipif;
11371 	ill_walk_context_t ctx;
11372 	ipif_t	*fallback_ipif = NULL;
11373 
11374 	net_mask = ip_net_mask(addr);
11375 	if (net_mask == 0) {
11376 		*ipifp = NULL;
11377 		return (0);
11378 	}
11379 
11380 	/* Let's check to see if this is maybe a local subnet route. */
11381 	/* this function only applies to IPv4 interfaces */
11382 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11383 	ill = ILL_START_WALK_V4(&ctx, ipst);
11384 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11385 		mutex_enter(&ill->ill_lock);
11386 		for (ipif = ill->ill_ipif; ipif != NULL;
11387 		    ipif = ipif->ipif_next) {
11388 			if (IPIF_IS_CONDEMNED(ipif))
11389 				continue;
11390 			if (!(ipif->ipif_flags & IPIF_UP))
11391 				continue;
11392 			if ((ipif->ipif_subnet & net_mask) ==
11393 			    (addr & net_mask)) {
11394 				/*
11395 				 * Don't trust pt-pt interfaces if there are
11396 				 * other interfaces.
11397 				 */
11398 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11399 					if (fallback_ipif == NULL) {
11400 						ipif_refhold_locked(ipif);
11401 						fallback_ipif = ipif;
11402 					}
11403 					continue;
11404 				}
11405 
11406 				/*
11407 				 * Fine. Just assume the same net mask as the
11408 				 * directly attached subnet interface is using.
11409 				 */
11410 				ipif_refhold_locked(ipif);
11411 				mutex_exit(&ill->ill_lock);
11412 				rw_exit(&ipst->ips_ill_g_lock);
11413 				if (fallback_ipif != NULL)
11414 					ipif_refrele(fallback_ipif);
11415 				*ipifp = ipif;
11416 				return (ipif->ipif_net_mask);
11417 			}
11418 		}
11419 		mutex_exit(&ill->ill_lock);
11420 	}
11421 	rw_exit(&ipst->ips_ill_g_lock);
11422 
11423 	*ipifp = fallback_ipif;
11424 	return ((fallback_ipif != NULL) ?
11425 	    fallback_ipif->ipif_net_mask : net_mask);
11426 }
11427 
11428 /*
11429  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11430  */
11431 static void
11432 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11433 {
11434 	IOCP	iocp;
11435 	ipft_t	*ipft;
11436 	ipllc_t	*ipllc;
11437 	mblk_t	*mp1;
11438 	cred_t	*cr;
11439 	int	error = 0;
11440 	conn_t	*connp;
11441 
11442 	ip1dbg(("ip_wput_ioctl"));
11443 	iocp = (IOCP)mp->b_rptr;
11444 	mp1 = mp->b_cont;
11445 	if (mp1 == NULL) {
11446 		iocp->ioc_error = EINVAL;
11447 		mp->b_datap->db_type = M_IOCNAK;
11448 		iocp->ioc_count = 0;
11449 		qreply(q, mp);
11450 		return;
11451 	}
11452 
11453 	/*
11454 	 * These IOCTLs provide various control capabilities to
11455 	 * upstream agents such as ULPs and processes.	There
11456 	 * are currently two such IOCTLs implemented.  They
11457 	 * are used by TCP to provide update information for
11458 	 * existing IREs and to forcibly delete an IRE for a
11459 	 * host that is not responding, thereby forcing an
11460 	 * attempt at a new route.
11461 	 */
11462 	iocp->ioc_error = EINVAL;
11463 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11464 		goto done;
11465 
11466 	ipllc = (ipllc_t *)mp1->b_rptr;
11467 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11468 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11469 			break;
11470 	}
11471 	/*
11472 	 * prefer credential from mblk over ioctl;
11473 	 * see ip_sioctl_copyin_setup
11474 	 */
11475 	cr = msg_getcred(mp, NULL);
11476 	if (cr == NULL)
11477 		cr = iocp->ioc_cr;
11478 
11479 	/*
11480 	 * Refhold the conn in case the request gets queued up in some lookup
11481 	 */
11482 	ASSERT(CONN_Q(q));
11483 	connp = Q_TO_CONN(q);
11484 	CONN_INC_REF(connp);
11485 	if (ipft->ipft_pfi &&
11486 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11487 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11488 		error = (*ipft->ipft_pfi)(q,
11489 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11490 	}
11491 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11492 		/*
11493 		 * CONN_OPER_PENDING_DONE happens in the function called
11494 		 * through ipft_pfi above.
11495 		 */
11496 		return;
11497 	}
11498 
11499 	CONN_OPER_PENDING_DONE(connp);
11500 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11501 		freemsg(mp);
11502 		return;
11503 	}
11504 	iocp->ioc_error = error;
11505 
11506 done:
11507 	mp->b_datap->db_type = M_IOCACK;
11508 	if (iocp->ioc_error)
11509 		iocp->ioc_count = 0;
11510 	qreply(q, mp);
11511 }
11512 
11513 /*
11514  * Assign a unique id for the ipif. This is used by sctp_addr.c
11515  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11516  */
11517 static void
11518 ipif_assign_seqid(ipif_t *ipif)
11519 {
11520 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11521 
11522 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11523 }
11524 
11525 /*
11526  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11527  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11528  * that the clone is complete -- including the seqid -- and the expectation is
11529  * that the caller will either free or overwrite `sipif' before it's unlocked.
11530  */
11531 static void
11532 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11533 {
11534 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11535 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11536 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11537 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11538 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11539 
11540 	dipif->ipif_flags = sipif->ipif_flags;
11541 	dipif->ipif_metric = sipif->ipif_metric;
11542 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11543 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11544 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11545 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11546 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11547 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11548 
11549 	/*
11550 	 * As per the comment atop the function, we assume that these sipif
11551 	 * fields will be changed before sipif is unlocked.
11552 	 */
11553 	dipif->ipif_seqid = sipif->ipif_seqid;
11554 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11555 }
11556 
11557 /*
11558  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11559  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11560  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11561  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11562  * down (i.e., no DAD), of the same type, and unlocked.
11563  */
11564 static void
11565 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11566 {
11567 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11568 	ipxop_t *ipx = ipsq->ipsq_xop;
11569 
11570 	ASSERT(sipif != dipif);
11571 	ASSERT(sipif != virgipif);
11572 
11573 	/*
11574 	 * Grab all of the locks that protect the ipif in a defined order.
11575 	 */
11576 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11577 
11578 	ipif_clone(sipif, dipif);
11579 	if (virgipif != NULL) {
11580 		ipif_clone(virgipif, sipif);
11581 		mi_free(virgipif);
11582 	}
11583 
11584 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11585 
11586 	/*
11587 	 * Transfer ownership of the current xop, if necessary.
11588 	 */
11589 	if (ipx->ipx_current_ipif == sipif) {
11590 		ASSERT(ipx->ipx_pending_ipif == NULL);
11591 		mutex_enter(&ipx->ipx_lock);
11592 		ipx->ipx_current_ipif = dipif;
11593 		mutex_exit(&ipx->ipx_lock);
11594 	}
11595 
11596 	if (virgipif == NULL)
11597 		mi_free(sipif);
11598 }
11599 
11600 /*
11601  * checks if:
11602  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11603  *	- logical interface is within the allowed range
11604  */
11605 static int
11606 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11607 {
11608 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11609 		return (ENAMETOOLONG);
11610 
11611 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11612 		return (ERANGE);
11613 	return (0);
11614 }
11615 
11616 /*
11617  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11618  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11619  * be inserted into the first space available in the list. The value of
11620  * ipif_id will then be set to the appropriate value for its position.
11621  */
11622 static int
11623 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11624 {
11625 	ill_t *ill;
11626 	ipif_t *tipif;
11627 	ipif_t **tipifp;
11628 	int id, err;
11629 	ip_stack_t	*ipst;
11630 
11631 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11632 	    IAM_WRITER_IPIF(ipif));
11633 
11634 	ill = ipif->ipif_ill;
11635 	ASSERT(ill != NULL);
11636 	ipst = ill->ill_ipst;
11637 
11638 	/*
11639 	 * In the case of lo0:0 we already hold the ill_g_lock.
11640 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11641 	 * ipif_insert.
11642 	 */
11643 	if (acquire_g_lock)
11644 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11645 	mutex_enter(&ill->ill_lock);
11646 	id = ipif->ipif_id;
11647 	tipifp = &(ill->ill_ipif);
11648 	if (id == -1) {	/* need to find a real id */
11649 		id = 0;
11650 		while ((tipif = *tipifp) != NULL) {
11651 			ASSERT(tipif->ipif_id >= id);
11652 			if (tipif->ipif_id != id)
11653 				break; /* non-consecutive id */
11654 			id++;
11655 			tipifp = &(tipif->ipif_next);
11656 		}
11657 		if ((err = is_lifname_valid(ill, id)) != 0) {
11658 			mutex_exit(&ill->ill_lock);
11659 			if (acquire_g_lock)
11660 				rw_exit(&ipst->ips_ill_g_lock);
11661 			return (err);
11662 		}
11663 		ipif->ipif_id = id; /* assign new id */
11664 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11665 		/* we have a real id; insert ipif in the right place */
11666 		while ((tipif = *tipifp) != NULL) {
11667 			ASSERT(tipif->ipif_id != id);
11668 			if (tipif->ipif_id > id)
11669 				break; /* found correct location */
11670 			tipifp = &(tipif->ipif_next);
11671 		}
11672 	} else {
11673 		mutex_exit(&ill->ill_lock);
11674 		if (acquire_g_lock)
11675 			rw_exit(&ipst->ips_ill_g_lock);
11676 		return (err);
11677 	}
11678 
11679 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11680 
11681 	ipif->ipif_next = tipif;
11682 	*tipifp = ipif;
11683 	mutex_exit(&ill->ill_lock);
11684 	if (acquire_g_lock)
11685 		rw_exit(&ipst->ips_ill_g_lock);
11686 
11687 	return (0);
11688 }
11689 
11690 static void
11691 ipif_remove(ipif_t *ipif)
11692 {
11693 	ipif_t	**ipifp;
11694 	ill_t	*ill = ipif->ipif_ill;
11695 
11696 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11697 
11698 	mutex_enter(&ill->ill_lock);
11699 	ipifp = &ill->ill_ipif;
11700 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11701 		if (*ipifp == ipif) {
11702 			*ipifp = ipif->ipif_next;
11703 			break;
11704 		}
11705 	}
11706 	mutex_exit(&ill->ill_lock);
11707 }
11708 
11709 /*
11710  * Allocate and initialize a new interface control structure.  (Always
11711  * called as writer.)
11712  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11713  * is not part of the global linked list of ills. ipif_seqid is unique
11714  * in the system and to preserve the uniqueness, it is assigned only
11715  * when ill becomes part of the global list. At that point ill will
11716  * have a name. If it doesn't get assigned here, it will get assigned
11717  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11718  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11719  * the interface flags or any other information from the DL_INFO_ACK for
11720  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11721  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11722  * second DL_INFO_ACK comes in from the driver.
11723  */
11724 static ipif_t *
11725 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11726     boolean_t insert, int *errorp)
11727 {
11728 	int err;
11729 	ipif_t	*ipif;
11730 	ip_stack_t *ipst = ill->ill_ipst;
11731 
11732 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11733 	    ill->ill_name, id, (void *)ill));
11734 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11735 
11736 	if (errorp != NULL)
11737 		*errorp = 0;
11738 
11739 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11740 		if (errorp != NULL)
11741 			*errorp = ENOMEM;
11742 		return (NULL);
11743 	}
11744 	*ipif = ipif_zero;	/* start clean */
11745 
11746 	ipif->ipif_ill = ill;
11747 	ipif->ipif_id = id;	/* could be -1 */
11748 	/*
11749 	 * Inherit the zoneid from the ill; for the shared stack instance
11750 	 * this is always the global zone
11751 	 */
11752 	ipif->ipif_zoneid = ill->ill_zoneid;
11753 
11754 	ipif->ipif_refcnt = 0;
11755 
11756 	if (insert) {
11757 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11758 			mi_free(ipif);
11759 			if (errorp != NULL)
11760 				*errorp = err;
11761 			return (NULL);
11762 		}
11763 		/* -1 id should have been replaced by real id */
11764 		id = ipif->ipif_id;
11765 		ASSERT(id >= 0);
11766 	}
11767 
11768 	if (ill->ill_name[0] != '\0')
11769 		ipif_assign_seqid(ipif);
11770 
11771 	/*
11772 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11773 	 * (which must not exist yet because the zeroth ipif is created once
11774 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11775 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11776 	 */
11777 	if (id == 0 && IS_IPMP(ill)) {
11778 		if (ipmp_illgrp_create(ill) == NULL) {
11779 			if (insert) {
11780 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11781 				ipif_remove(ipif);
11782 				rw_exit(&ipst->ips_ill_g_lock);
11783 			}
11784 			mi_free(ipif);
11785 			if (errorp != NULL)
11786 				*errorp = ENOMEM;
11787 			return (NULL);
11788 		}
11789 	}
11790 
11791 	/*
11792 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11793 	 * not up and can't be looked up until the ioctl completes and the
11794 	 * IPIF_CHANGING flag is cleared.
11795 	 */
11796 	mutex_enter(&ill->ill_lock);
11797 
11798 	ipif->ipif_ire_type = ire_type;
11799 
11800 	if (ipif->ipif_isv6) {
11801 		ill->ill_flags |= ILLF_IPV6;
11802 	} else {
11803 		ipaddr_t inaddr_any = INADDR_ANY;
11804 
11805 		ill->ill_flags |= ILLF_IPV4;
11806 
11807 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11808 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11809 		    &ipif->ipif_v6lcl_addr);
11810 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11811 		    &ipif->ipif_v6subnet);
11812 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11813 		    &ipif->ipif_v6net_mask);
11814 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11815 		    &ipif->ipif_v6brd_addr);
11816 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11817 		    &ipif->ipif_v6pp_dst_addr);
11818 	}
11819 
11820 	/*
11821 	 * Don't set the interface flags etc. now, will do it in
11822 	 * ip_ll_subnet_defaults.
11823 	 */
11824 	if (!initialize)
11825 		goto out;
11826 
11827 	/*
11828 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11829 	 * with no underlying interfaces (and thus an unknown broadcast
11830 	 * address length), but all interfaces that can be placed into an IPMP
11831 	 * group are required to be broadcast-capable.
11832 	 */
11833 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11834 		/*
11835 		 * Later detect lack of DLPI driver multicast capability by
11836 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11837 		 */
11838 		ill->ill_flags |= ILLF_MULTICAST;
11839 		if (!ipif->ipif_isv6)
11840 			ipif->ipif_flags |= IPIF_BROADCAST;
11841 	} else {
11842 		if (ill->ill_net_type != IRE_LOOPBACK) {
11843 			if (ipif->ipif_isv6)
11844 				/*
11845 				 * Note: xresolv interfaces will eventually need
11846 				 * NOARP set here as well, but that will require
11847 				 * those external resolvers to have some
11848 				 * knowledge of that flag and act appropriately.
11849 				 * Not to be changed at present.
11850 				 */
11851 				ill->ill_flags |= ILLF_NONUD;
11852 			else
11853 				ill->ill_flags |= ILLF_NOARP;
11854 		}
11855 		if (ill->ill_phys_addr_length == 0) {
11856 			if (IS_VNI(ill)) {
11857 				ipif->ipif_flags |= IPIF_NOXMIT;
11858 			} else {
11859 				/* pt-pt supports multicast. */
11860 				ill->ill_flags |= ILLF_MULTICAST;
11861 				if (ill->ill_net_type != IRE_LOOPBACK)
11862 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11863 			}
11864 		}
11865 	}
11866 out:
11867 	mutex_exit(&ill->ill_lock);
11868 	return (ipif);
11869 }
11870 
11871 /*
11872  * Remove the neighbor cache entries associated with this logical
11873  * interface.
11874  */
11875 int
11876 ipif_arp_down(ipif_t *ipif)
11877 {
11878 	ill_t	*ill = ipif->ipif_ill;
11879 	int	err = 0;
11880 
11881 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11882 	ASSERT(IAM_WRITER_IPIF(ipif));
11883 
11884 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11885 	    ill_t *, ill, ipif_t *, ipif);
11886 	ipif_nce_down(ipif);
11887 
11888 	/*
11889 	 * If this is the last ipif that is going down and there are no
11890 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11891 	 * clean up ARP completely.
11892 	 */
11893 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11894 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11895 		/*
11896 		 * If this was the last ipif on an IPMP interface, purge any
11897 		 * static ARP entries associated with it.
11898 		 */
11899 		if (IS_IPMP(ill))
11900 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11901 
11902 		/* UNBIND, DETACH */
11903 		err = arp_ll_down(ill);
11904 	}
11905 
11906 	return (err);
11907 }
11908 
11909 /*
11910  * Get the resolver set up for a new IP address.  (Always called as writer.)
11911  * Called both for IPv4 and IPv6 interfaces, though it only does some
11912  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11913  *
11914  * The enumerated value res_act tunes the behavior:
11915  * 	* Res_act_initial: set up all the resolver structures for a new
11916  *	  IP address.
11917  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11918  *	  ARP message in defense of the address.
11919  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11920  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11921  *
11922  * Returns zero on success, or an errno upon failure.
11923  */
11924 int
11925 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11926 {
11927 	ill_t		*ill = ipif->ipif_ill;
11928 	int		err;
11929 	boolean_t	was_dup;
11930 
11931 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11932 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11933 	ASSERT(IAM_WRITER_IPIF(ipif));
11934 
11935 	was_dup = B_FALSE;
11936 	if (res_act == Res_act_initial) {
11937 		ipif->ipif_addr_ready = 0;
11938 		/*
11939 		 * We're bringing an interface up here.  There's no way that we
11940 		 * should need to shut down ARP now.
11941 		 */
11942 		mutex_enter(&ill->ill_lock);
11943 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11944 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11945 			ill->ill_ipif_dup_count--;
11946 			was_dup = B_TRUE;
11947 		}
11948 		mutex_exit(&ill->ill_lock);
11949 	}
11950 	if (ipif->ipif_recovery_id != 0)
11951 		(void) untimeout(ipif->ipif_recovery_id);
11952 	ipif->ipif_recovery_id = 0;
11953 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11954 		ipif->ipif_addr_ready = 1;
11955 		return (0);
11956 	}
11957 	/* NDP will set the ipif_addr_ready flag when it's ready */
11958 	if (ill->ill_isv6)
11959 		return (0);
11960 
11961 	err = ipif_arp_up(ipif, res_act, was_dup);
11962 	return (err);
11963 }
11964 
11965 /*
11966  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11967  * when a link has just gone back up.
11968  */
11969 static void
11970 ipif_nce_start_dad(ipif_t *ipif)
11971 {
11972 	ncec_t *ncec;
11973 	ill_t *ill = ipif->ipif_ill;
11974 	boolean_t isv6 = ill->ill_isv6;
11975 
11976 	if (isv6) {
11977 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11978 		    &ipif->ipif_v6lcl_addr);
11979 	} else {
11980 		ipaddr_t v4addr;
11981 
11982 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11983 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11984 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11985 			/*
11986 			 * If we can't contact ARP for some reason,
11987 			 * that's not really a problem.  Just send
11988 			 * out the routing socket notification that
11989 			 * DAD completion would have done, and continue.
11990 			 */
11991 			ipif_mask_reply(ipif);
11992 			ipif_up_notify(ipif);
11993 			ipif->ipif_addr_ready = 1;
11994 			return;
11995 		}
11996 
11997 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11998 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11999 	}
12000 
12001 	if (ncec == NULL) {
12002 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12003 		    (void *)ipif));
12004 		return;
12005 	}
12006 	if (!nce_restart_dad(ncec)) {
12007 		/*
12008 		 * If we can't restart DAD for some reason, that's not really a
12009 		 * problem.  Just send out the routing socket notification that
12010 		 * DAD completion would have done, and continue.
12011 		 */
12012 		ipif_up_notify(ipif);
12013 		ipif->ipif_addr_ready = 1;
12014 	}
12015 	ncec_refrele(ncec);
12016 }
12017 
12018 /*
12019  * Restart duplicate address detection on all interfaces on the given ill.
12020  *
12021  * This is called when an interface transitions from down to up
12022  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12023  *
12024  * Note that since the underlying physical link has transitioned, we must cause
12025  * at least one routing socket message to be sent here, either via DAD
12026  * completion or just by default on the first ipif.  (If we don't do this, then
12027  * in.mpathd will see long delays when doing link-based failure recovery.)
12028  */
12029 void
12030 ill_restart_dad(ill_t *ill, boolean_t went_up)
12031 {
12032 	ipif_t *ipif;
12033 
12034 	if (ill == NULL)
12035 		return;
12036 
12037 	/*
12038 	 * If layer two doesn't support duplicate address detection, then just
12039 	 * send the routing socket message now and be done with it.
12040 	 */
12041 	if (!ill->ill_isv6 && arp_no_defense) {
12042 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12043 		return;
12044 	}
12045 
12046 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12047 		if (went_up) {
12048 
12049 			if (ipif->ipif_flags & IPIF_UP) {
12050 				ipif_nce_start_dad(ipif);
12051 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12052 				/*
12053 				 * kick off the bring-up process now.
12054 				 */
12055 				ipif_do_recovery(ipif);
12056 			} else {
12057 				/*
12058 				 * Unfortunately, the first ipif is "special"
12059 				 * and represents the underlying ill in the
12060 				 * routing socket messages.  Thus, when this
12061 				 * one ipif is down, we must still notify so
12062 				 * that the user knows the IFF_RUNNING status
12063 				 * change.  (If the first ipif is up, then
12064 				 * we'll handle eventual routing socket
12065 				 * notification via DAD completion.)
12066 				 */
12067 				if (ipif == ill->ill_ipif) {
12068 					ip_rts_ifmsg(ill->ill_ipif,
12069 					    RTSQ_DEFAULT);
12070 				}
12071 			}
12072 		} else {
12073 			/*
12074 			 * After link down, we'll need to send a new routing
12075 			 * message when the link comes back, so clear
12076 			 * ipif_addr_ready.
12077 			 */
12078 			ipif->ipif_addr_ready = 0;
12079 		}
12080 	}
12081 
12082 	/*
12083 	 * If we've torn down links, then notify the user right away.
12084 	 */
12085 	if (!went_up)
12086 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12087 }
12088 
12089 static void
12090 ipsq_delete(ipsq_t *ipsq)
12091 {
12092 	ipxop_t *ipx = ipsq->ipsq_xop;
12093 
12094 	ipsq->ipsq_ipst = NULL;
12095 	ASSERT(ipsq->ipsq_phyint == NULL);
12096 	ASSERT(ipsq->ipsq_xop != NULL);
12097 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12098 	ASSERT(ipx->ipx_pending_mp == NULL);
12099 	kmem_free(ipsq, sizeof (ipsq_t));
12100 }
12101 
12102 static int
12103 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12104 {
12105 	int err = 0;
12106 	ipif_t *ipif;
12107 
12108 	if (ill == NULL)
12109 		return (0);
12110 
12111 	ASSERT(IAM_WRITER_ILL(ill));
12112 	ill->ill_up_ipifs = B_TRUE;
12113 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12114 		if (ipif->ipif_was_up) {
12115 			if (!(ipif->ipif_flags & IPIF_UP))
12116 				err = ipif_up(ipif, q, mp);
12117 			ipif->ipif_was_up = B_FALSE;
12118 			if (err != 0) {
12119 				ASSERT(err == EINPROGRESS);
12120 				return (err);
12121 			}
12122 		}
12123 	}
12124 	ill->ill_up_ipifs = B_FALSE;
12125 	return (0);
12126 }
12127 
12128 /*
12129  * This function is called to bring up all the ipifs that were up before
12130  * bringing the ill down via ill_down_ipifs().
12131  */
12132 int
12133 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12134 {
12135 	int err;
12136 
12137 	ASSERT(IAM_WRITER_ILL(ill));
12138 
12139 	if (ill->ill_replumbing) {
12140 		ill->ill_replumbing = 0;
12141 		/*
12142 		 * Send down REPLUMB_DONE notification followed by the
12143 		 * BIND_REQ on the arp stream.
12144 		 */
12145 		if (!ill->ill_isv6)
12146 			arp_send_replumb_conf(ill);
12147 	}
12148 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12149 	if (err != 0)
12150 		return (err);
12151 
12152 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12153 }
12154 
12155 /*
12156  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12157  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12158  */
12159 static void
12160 ill_down_ipifs(ill_t *ill, boolean_t logical)
12161 {
12162 	ipif_t *ipif;
12163 
12164 	ASSERT(IAM_WRITER_ILL(ill));
12165 
12166 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12167 		/*
12168 		 * We go through the ipif_down logic even if the ipif
12169 		 * is already down, since routes can be added based
12170 		 * on down ipifs. Going through ipif_down once again
12171 		 * will delete any IREs created based on these routes.
12172 		 */
12173 		if (ipif->ipif_flags & IPIF_UP)
12174 			ipif->ipif_was_up = B_TRUE;
12175 
12176 		if (logical) {
12177 			(void) ipif_logical_down(ipif, NULL, NULL);
12178 			ipif_non_duplicate(ipif);
12179 			(void) ipif_down_tail(ipif);
12180 		} else {
12181 			(void) ipif_down(ipif, NULL, NULL);
12182 		}
12183 	}
12184 }
12185 
12186 /*
12187  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12188  * a look again at valid source addresses.
12189  * This should be called each time after the set of source addresses has been
12190  * changed.
12191  */
12192 void
12193 ip_update_source_selection(ip_stack_t *ipst)
12194 {
12195 	/* We skip past SRC_GENERATION_VERIFY */
12196 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12197 	    SRC_GENERATION_VERIFY)
12198 		atomic_add_32(&ipst->ips_src_generation, 1);
12199 }
12200 
12201 /*
12202  * Finish the group join started in ip_sioctl_groupname().
12203  */
12204 /* ARGSUSED */
12205 static void
12206 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12207 {
12208 	ill_t		*ill = q->q_ptr;
12209 	phyint_t	*phyi = ill->ill_phyint;
12210 	ipmp_grp_t	*grp = phyi->phyint_grp;
12211 	ip_stack_t	*ipst = ill->ill_ipst;
12212 
12213 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12214 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12215 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12216 
12217 	if (phyi->phyint_illv4 != NULL) {
12218 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12219 		VERIFY(grp->gr_pendv4-- > 0);
12220 		rw_exit(&ipst->ips_ipmp_lock);
12221 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12222 	}
12223 	if (phyi->phyint_illv6 != NULL) {
12224 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12225 		VERIFY(grp->gr_pendv6-- > 0);
12226 		rw_exit(&ipst->ips_ipmp_lock);
12227 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12228 	}
12229 	freemsg(mp);
12230 }
12231 
12232 /*
12233  * Process an SIOCSLIFGROUPNAME request.
12234  */
12235 /* ARGSUSED */
12236 int
12237 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12238     ip_ioctl_cmd_t *ipip, void *ifreq)
12239 {
12240 	struct lifreq	*lifr = ifreq;
12241 	ill_t		*ill = ipif->ipif_ill;
12242 	ip_stack_t	*ipst = ill->ill_ipst;
12243 	phyint_t	*phyi = ill->ill_phyint;
12244 	ipmp_grp_t	*grp = phyi->phyint_grp;
12245 	mblk_t		*ipsq_mp;
12246 	int		err = 0;
12247 
12248 	/*
12249 	 * Note that phyint_grp can only change here, where we're exclusive.
12250 	 */
12251 	ASSERT(IAM_WRITER_ILL(ill));
12252 
12253 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12254 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12255 		return (EINVAL);
12256 
12257 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12258 
12259 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12260 
12261 	/*
12262 	 * If the name hasn't changed, there's nothing to do.
12263 	 */
12264 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12265 		goto unlock;
12266 
12267 	/*
12268 	 * Handle requests to rename an IPMP meta-interface.
12269 	 *
12270 	 * Note that creation of the IPMP meta-interface is handled in
12271 	 * userland through the standard plumbing sequence.  As part of the
12272 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12273 	 * the name of the interface (see ipif_set_values_tail()).
12274 	 */
12275 	if (IS_IPMP(ill)) {
12276 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12277 		goto unlock;
12278 	}
12279 
12280 	/*
12281 	 * Handle requests to add or remove an IP interface from a group.
12282 	 */
12283 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12284 		/*
12285 		 * Moves are handled by first removing the interface from
12286 		 * its existing group, and then adding it to another group.
12287 		 * So, fail if it's already in a group.
12288 		 */
12289 		if (IS_UNDER_IPMP(ill)) {
12290 			err = EALREADY;
12291 			goto unlock;
12292 		}
12293 
12294 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12295 		if (grp == NULL) {
12296 			err = ENOENT;
12297 			goto unlock;
12298 		}
12299 
12300 		/*
12301 		 * Check if the phyint and its ills are suitable for
12302 		 * inclusion into the group.
12303 		 */
12304 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12305 			goto unlock;
12306 
12307 		/*
12308 		 * Checks pass; join the group, and enqueue the remaining
12309 		 * illgrp joins for when we've become part of the group xop
12310 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12311 		 * requires an mblk_t to scribble on, and since `mp' will be
12312 		 * freed as part of completing the ioctl, allocate another.
12313 		 */
12314 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12315 			err = ENOMEM;
12316 			goto unlock;
12317 		}
12318 
12319 		/*
12320 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12321 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12322 		 * before ip_join_illgrps() is called back.  See the comments
12323 		 * in ip_sioctl_plink_ipmp() for more.
12324 		 */
12325 		if (phyi->phyint_illv4 != NULL)
12326 			grp->gr_pendv4++;
12327 		if (phyi->phyint_illv6 != NULL)
12328 			grp->gr_pendv6++;
12329 
12330 		rw_exit(&ipst->ips_ipmp_lock);
12331 
12332 		ipmp_phyint_join_grp(phyi, grp);
12333 		ill_refhold(ill);
12334 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12335 		    SWITCH_OP, B_FALSE);
12336 		return (0);
12337 	} else {
12338 		/*
12339 		 * Request to remove the interface from a group.  If the
12340 		 * interface is not in a group, this trivially succeeds.
12341 		 */
12342 		rw_exit(&ipst->ips_ipmp_lock);
12343 		if (IS_UNDER_IPMP(ill))
12344 			ipmp_phyint_leave_grp(phyi);
12345 		return (0);
12346 	}
12347 unlock:
12348 	rw_exit(&ipst->ips_ipmp_lock);
12349 	return (err);
12350 }
12351 
12352 /*
12353  * Process an SIOCGLIFBINDING request.
12354  */
12355 /* ARGSUSED */
12356 int
12357 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12358     ip_ioctl_cmd_t *ipip, void *ifreq)
12359 {
12360 	ill_t		*ill;
12361 	struct lifreq	*lifr = ifreq;
12362 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12363 
12364 	if (!IS_IPMP(ipif->ipif_ill))
12365 		return (EINVAL);
12366 
12367 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12368 	if ((ill = ipif->ipif_bound_ill) == NULL)
12369 		lifr->lifr_binding[0] = '\0';
12370 	else
12371 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12372 	rw_exit(&ipst->ips_ipmp_lock);
12373 	return (0);
12374 }
12375 
12376 /*
12377  * Process an SIOCGLIFGROUPNAME request.
12378  */
12379 /* ARGSUSED */
12380 int
12381 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12382     ip_ioctl_cmd_t *ipip, void *ifreq)
12383 {
12384 	ipmp_grp_t	*grp;
12385 	struct lifreq	*lifr = ifreq;
12386 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12387 
12388 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12389 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12390 		lifr->lifr_groupname[0] = '\0';
12391 	else
12392 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12393 	rw_exit(&ipst->ips_ipmp_lock);
12394 	return (0);
12395 }
12396 
12397 /*
12398  * Process an SIOCGLIFGROUPINFO request.
12399  */
12400 /* ARGSUSED */
12401 int
12402 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12403     ip_ioctl_cmd_t *ipip, void *dummy)
12404 {
12405 	ipmp_grp_t	*grp;
12406 	lifgroupinfo_t	*lifgr;
12407 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12408 
12409 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12410 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12411 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12412 
12413 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12414 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12415 		rw_exit(&ipst->ips_ipmp_lock);
12416 		return (ENOENT);
12417 	}
12418 	ipmp_grp_info(grp, lifgr);
12419 	rw_exit(&ipst->ips_ipmp_lock);
12420 	return (0);
12421 }
12422 
12423 static void
12424 ill_dl_down(ill_t *ill)
12425 {
12426 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12427 
12428 	/*
12429 	 * The ill is down; unbind but stay attached since we're still
12430 	 * associated with a PPA. If we have negotiated DLPI capabilites
12431 	 * with the data link service provider (IDS_OK) then reset them.
12432 	 * The interval between unbinding and rebinding is potentially
12433 	 * unbounded hence we cannot assume things will be the same.
12434 	 * The DLPI capabilities will be probed again when the data link
12435 	 * is brought up.
12436 	 */
12437 	mblk_t	*mp = ill->ill_unbind_mp;
12438 
12439 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12440 
12441 	if (!ill->ill_replumbing) {
12442 		/* Free all ilms for this ill */
12443 		update_conn_ill(ill, ill->ill_ipst);
12444 	} else {
12445 		ill_leave_multicast(ill);
12446 	}
12447 
12448 	ill->ill_unbind_mp = NULL;
12449 	if (mp != NULL) {
12450 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12451 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12452 		    ill->ill_name));
12453 		mutex_enter(&ill->ill_lock);
12454 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12455 		mutex_exit(&ill->ill_lock);
12456 		/*
12457 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12458 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12459 		 * ill_capability_dld_disable disable rightaway. If this is not
12460 		 * an unplumb operation then the disable happens on receipt of
12461 		 * the capab ack via ip_rput_dlpi_writer ->
12462 		 * ill_capability_ack_thr. In both cases the order of
12463 		 * the operations seen by DLD is capability disable followed
12464 		 * by DL_UNBIND. Also the DLD capability disable needs a
12465 		 * cv_wait'able context.
12466 		 */
12467 		if (ill->ill_state_flags & ILL_CONDEMNED)
12468 			ill_capability_dld_disable(ill);
12469 		ill_capability_reset(ill, B_FALSE);
12470 		ill_dlpi_send(ill, mp);
12471 	}
12472 	mutex_enter(&ill->ill_lock);
12473 	ill->ill_dl_up = 0;
12474 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12475 	mutex_exit(&ill->ill_lock);
12476 }
12477 
12478 void
12479 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12480 {
12481 	union DL_primitives *dlp;
12482 	t_uscalar_t prim;
12483 	boolean_t waitack = B_FALSE;
12484 
12485 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12486 
12487 	dlp = (union DL_primitives *)mp->b_rptr;
12488 	prim = dlp->dl_primitive;
12489 
12490 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12491 	    dl_primstr(prim), prim, ill->ill_name));
12492 
12493 	switch (prim) {
12494 	case DL_PHYS_ADDR_REQ:
12495 	{
12496 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12497 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12498 		break;
12499 	}
12500 	case DL_BIND_REQ:
12501 		mutex_enter(&ill->ill_lock);
12502 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12503 		mutex_exit(&ill->ill_lock);
12504 		break;
12505 	}
12506 
12507 	/*
12508 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12509 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12510 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12511 	 */
12512 	mutex_enter(&ill->ill_lock);
12513 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12514 	    (prim == DL_UNBIND_REQ)) {
12515 		ill->ill_dlpi_pending = prim;
12516 		waitack = B_TRUE;
12517 	}
12518 
12519 	mutex_exit(&ill->ill_lock);
12520 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12521 	    char *, dl_primstr(prim), ill_t *, ill);
12522 	putnext(ill->ill_wq, mp);
12523 
12524 	/*
12525 	 * There is no ack for DL_NOTIFY_CONF messages
12526 	 */
12527 	if (waitack && prim == DL_NOTIFY_CONF)
12528 		ill_dlpi_done(ill, prim);
12529 }
12530 
12531 /*
12532  * Helper function for ill_dlpi_send().
12533  */
12534 /* ARGSUSED */
12535 static void
12536 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12537 {
12538 	ill_dlpi_send(q->q_ptr, mp);
12539 }
12540 
12541 /*
12542  * Send a DLPI control message to the driver but make sure there
12543  * is only one outstanding message. Uses ill_dlpi_pending to tell
12544  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12545  * when an ACK or a NAK is received to process the next queued message.
12546  */
12547 void
12548 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12549 {
12550 	mblk_t **mpp;
12551 
12552 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12553 
12554 	/*
12555 	 * To ensure that any DLPI requests for current exclusive operation
12556 	 * are always completely sent before any DLPI messages for other
12557 	 * operations, require writer access before enqueuing.
12558 	 */
12559 	if (!IAM_WRITER_ILL(ill)) {
12560 		ill_refhold(ill);
12561 		/* qwriter_ip() does the ill_refrele() */
12562 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12563 		    NEW_OP, B_TRUE);
12564 		return;
12565 	}
12566 
12567 	mutex_enter(&ill->ill_lock);
12568 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12569 		/* Must queue message. Tail insertion */
12570 		mpp = &ill->ill_dlpi_deferred;
12571 		while (*mpp != NULL)
12572 			mpp = &((*mpp)->b_next);
12573 
12574 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12575 		    "while %s pending\n", ill->ill_name,
12576 		    dl_primstr(ill->ill_dlpi_pending)));
12577 
12578 		*mpp = mp;
12579 		mutex_exit(&ill->ill_lock);
12580 		return;
12581 	}
12582 	mutex_exit(&ill->ill_lock);
12583 	ill_dlpi_dispatch(ill, mp);
12584 }
12585 
12586 void
12587 ill_capability_send(ill_t *ill, mblk_t *mp)
12588 {
12589 	ill->ill_capab_pending_cnt++;
12590 	ill_dlpi_send(ill, mp);
12591 }
12592 
12593 void
12594 ill_capability_done(ill_t *ill)
12595 {
12596 	ASSERT(ill->ill_capab_pending_cnt != 0);
12597 
12598 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12599 
12600 	ill->ill_capab_pending_cnt--;
12601 	if (ill->ill_capab_pending_cnt == 0 &&
12602 	    ill->ill_dlpi_capab_state == IDCS_OK)
12603 		ill_capability_reset_alloc(ill);
12604 }
12605 
12606 /*
12607  * Send all deferred DLPI messages without waiting for their ACKs.
12608  */
12609 void
12610 ill_dlpi_send_deferred(ill_t *ill)
12611 {
12612 	mblk_t *mp, *nextmp;
12613 
12614 	/*
12615 	 * Clear ill_dlpi_pending so that the message is not queued in
12616 	 * ill_dlpi_send().
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 		mp->b_next = NULL;
12627 		ill_dlpi_send(ill, mp);
12628 	}
12629 }
12630 
12631 /*
12632  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12633  * or M_HANGUP
12634  */
12635 static void
12636 ill_dlpi_clear_deferred(ill_t *ill)
12637 {
12638 	mblk_t	*mp, *nextmp;
12639 
12640 	mutex_enter(&ill->ill_lock);
12641 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12642 	mp = ill->ill_dlpi_deferred;
12643 	ill->ill_dlpi_deferred = NULL;
12644 	mutex_exit(&ill->ill_lock);
12645 
12646 	for (; mp != NULL; mp = nextmp) {
12647 		nextmp = mp->b_next;
12648 		inet_freemsg(mp);
12649 	}
12650 }
12651 
12652 /*
12653  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12654  */
12655 boolean_t
12656 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12657 {
12658 	t_uscalar_t pending;
12659 
12660 	mutex_enter(&ill->ill_lock);
12661 	if (ill->ill_dlpi_pending == prim) {
12662 		mutex_exit(&ill->ill_lock);
12663 		return (B_TRUE);
12664 	}
12665 
12666 	/*
12667 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12668 	 * without waiting, so don't print any warnings in that case.
12669 	 */
12670 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12671 		mutex_exit(&ill->ill_lock);
12672 		return (B_FALSE);
12673 	}
12674 	pending = ill->ill_dlpi_pending;
12675 	mutex_exit(&ill->ill_lock);
12676 
12677 	if (pending == DL_PRIM_INVAL) {
12678 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12679 		    "received unsolicited ack for %s on %s\n",
12680 		    dl_primstr(prim), ill->ill_name);
12681 	} else {
12682 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12683 		    "received unexpected ack for %s on %s (expecting %s)\n",
12684 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12685 	}
12686 	return (B_FALSE);
12687 }
12688 
12689 /*
12690  * Complete the current DLPI operation associated with `prim' on `ill' and
12691  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12692  * operations and the ill's current exclusive IPSQ operation has finished
12693  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12694  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12695  * the comments above ipsq_current_finish() for details.
12696  */
12697 void
12698 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12699 {
12700 	mblk_t *mp;
12701 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12702 	ipxop_t *ipx = ipsq->ipsq_xop;
12703 
12704 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12705 	mutex_enter(&ill->ill_lock);
12706 
12707 	ASSERT(prim != DL_PRIM_INVAL);
12708 	ASSERT(ill->ill_dlpi_pending == prim);
12709 
12710 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12711 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12712 
12713 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12714 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12715 		if (ipx->ipx_current_done) {
12716 			mutex_enter(&ipx->ipx_lock);
12717 			ipx->ipx_current_ipif = NULL;
12718 			mutex_exit(&ipx->ipx_lock);
12719 		}
12720 		cv_signal(&ill->ill_cv);
12721 		mutex_exit(&ill->ill_lock);
12722 		return;
12723 	}
12724 
12725 	ill->ill_dlpi_deferred = mp->b_next;
12726 	mp->b_next = NULL;
12727 	mutex_exit(&ill->ill_lock);
12728 
12729 	ill_dlpi_dispatch(ill, mp);
12730 }
12731 
12732 /*
12733  * Queue a (multicast) DLPI control message to be sent to the driver by
12734  * later calling ill_dlpi_send_queued.
12735  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12736  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12737  * for the same group to race.
12738  * We send DLPI control messages in order using ill_lock.
12739  * For IPMP we should be called on the cast_ill.
12740  */
12741 void
12742 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12743 {
12744 	mblk_t **mpp;
12745 
12746 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12747 
12748 	mutex_enter(&ill->ill_lock);
12749 	/* Must queue message. Tail insertion */
12750 	mpp = &ill->ill_dlpi_deferred;
12751 	while (*mpp != NULL)
12752 		mpp = &((*mpp)->b_next);
12753 
12754 	*mpp = mp;
12755 	mutex_exit(&ill->ill_lock);
12756 }
12757 
12758 /*
12759  * Send the messages that were queued. Make sure there is only
12760  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12761  * when an ACK or a NAK is received to process the next queued message.
12762  * For IPMP we are called on the upper ill, but when send what is queued
12763  * on the cast_ill.
12764  */
12765 void
12766 ill_dlpi_send_queued(ill_t *ill)
12767 {
12768 	mblk_t	*mp;
12769 	union DL_primitives *dlp;
12770 	t_uscalar_t prim;
12771 	ill_t *release_ill = NULL;
12772 
12773 	if (IS_IPMP(ill)) {
12774 		/* On the upper IPMP ill. */
12775 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12776 		if (release_ill == NULL) {
12777 			/* Avoid ever sending anything down to the ipmpstub */
12778 			return;
12779 		}
12780 		ill = release_ill;
12781 	}
12782 	mutex_enter(&ill->ill_lock);
12783 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12784 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12785 			/* Can't send. Somebody else will send it */
12786 			mutex_exit(&ill->ill_lock);
12787 			goto done;
12788 		}
12789 		ill->ill_dlpi_deferred = mp->b_next;
12790 		mp->b_next = NULL;
12791 		if (!ill->ill_dl_up) {
12792 			/*
12793 			 * Nobody there. All multicast addresses will be
12794 			 * re-joined when we get the DL_BIND_ACK bringing the
12795 			 * interface up.
12796 			 */
12797 			freemsg(mp);
12798 			continue;
12799 		}
12800 		dlp = (union DL_primitives *)mp->b_rptr;
12801 		prim = dlp->dl_primitive;
12802 
12803 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12804 		    (prim == DL_UNBIND_REQ)) {
12805 			ill->ill_dlpi_pending = prim;
12806 		}
12807 		mutex_exit(&ill->ill_lock);
12808 
12809 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12810 		    char *, dl_primstr(prim), ill_t *, ill);
12811 		putnext(ill->ill_wq, mp);
12812 		mutex_enter(&ill->ill_lock);
12813 	}
12814 	mutex_exit(&ill->ill_lock);
12815 done:
12816 	if (release_ill != NULL)
12817 		ill_refrele(release_ill);
12818 }
12819 
12820 /*
12821  * Queue an IP (IGMP/MLD) message to be sent by IP from
12822  * ill_mcast_send_queued
12823  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12824  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12825  * group to race.
12826  * We send them in order using ill_lock.
12827  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12828  */
12829 void
12830 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12831 {
12832 	mblk_t **mpp;
12833 	ill_t *release_ill = NULL;
12834 
12835 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12836 
12837 	if (IS_IPMP(ill)) {
12838 		/* On the upper IPMP ill. */
12839 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12840 		if (release_ill == NULL) {
12841 			/* Discard instead of queuing for the ipmp interface */
12842 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12843 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12844 			    mp, ill);
12845 			freemsg(mp);
12846 			return;
12847 		}
12848 		ill = release_ill;
12849 	}
12850 
12851 	mutex_enter(&ill->ill_lock);
12852 	/* Must queue message. Tail insertion */
12853 	mpp = &ill->ill_mcast_deferred;
12854 	while (*mpp != NULL)
12855 		mpp = &((*mpp)->b_next);
12856 
12857 	*mpp = mp;
12858 	mutex_exit(&ill->ill_lock);
12859 	if (release_ill != NULL)
12860 		ill_refrele(release_ill);
12861 }
12862 
12863 /*
12864  * Send the IP packets that were queued by ill_mcast_queue.
12865  * These are IGMP/MLD packets.
12866  *
12867  * For IPMP we are called on the upper ill, but when send what is queued
12868  * on the cast_ill.
12869  *
12870  * Request loopback of the report if we are acting as a multicast
12871  * router, so that the process-level routing demon can hear it.
12872  * This will run multiple times for the same group if there are members
12873  * on the same group for multiple ipif's on the same ill. The
12874  * igmp_input/mld_input code will suppress this due to the loopback thus we
12875  * always loopback membership report.
12876  *
12877  * We also need to make sure that this does not get load balanced
12878  * by IPMP. We do this by passing an ill to ip_output_simple.
12879  */
12880 void
12881 ill_mcast_send_queued(ill_t *ill)
12882 {
12883 	mblk_t	*mp;
12884 	ip_xmit_attr_t ixas;
12885 	ill_t *release_ill = NULL;
12886 
12887 	if (IS_IPMP(ill)) {
12888 		/* On the upper IPMP ill. */
12889 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12890 		if (release_ill == NULL) {
12891 			/*
12892 			 * We should have no messages on the ipmp interface
12893 			 * but no point in trying to send them.
12894 			 */
12895 			return;
12896 		}
12897 		ill = release_ill;
12898 	}
12899 	bzero(&ixas, sizeof (ixas));
12900 	ixas.ixa_zoneid = ALL_ZONES;
12901 	ixas.ixa_cred = kcred;
12902 	ixas.ixa_cpid = NOPID;
12903 	ixas.ixa_tsl = NULL;
12904 	/*
12905 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12906 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12907 	 * That is necessary to handle IGMP/MLD snooping switches.
12908 	 */
12909 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12910 	ixas.ixa_ipst = ill->ill_ipst;
12911 
12912 	mutex_enter(&ill->ill_lock);
12913 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12914 		ill->ill_mcast_deferred = mp->b_next;
12915 		mp->b_next = NULL;
12916 		if (!ill->ill_dl_up) {
12917 			/*
12918 			 * Nobody there. Just drop the ip packets.
12919 			 * IGMP/MLD will resend later, if this is a replumb.
12920 			 */
12921 			freemsg(mp);
12922 			continue;
12923 		}
12924 		mutex_enter(&ill->ill_phyint->phyint_lock);
12925 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12926 			/*
12927 			 * When the ill is getting deactivated, we only want to
12928 			 * send the DLPI messages, so drop IGMP/MLD packets.
12929 			 * DLPI messages are handled by ill_dlpi_send_queued()
12930 			 */
12931 			mutex_exit(&ill->ill_phyint->phyint_lock);
12932 			freemsg(mp);
12933 			continue;
12934 		}
12935 		mutex_exit(&ill->ill_phyint->phyint_lock);
12936 		mutex_exit(&ill->ill_lock);
12937 
12938 		/* Check whether we are sending IPv4 or IPv6. */
12939 		if (ill->ill_isv6) {
12940 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12941 
12942 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12943 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12944 		} else {
12945 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12946 
12947 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12948 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12949 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12950 		}
12951 
12952 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12953 		(void) ip_output_simple(mp, &ixas);
12954 		ixa_cleanup(&ixas);
12955 
12956 		mutex_enter(&ill->ill_lock);
12957 	}
12958 	mutex_exit(&ill->ill_lock);
12959 
12960 done:
12961 	if (release_ill != NULL)
12962 		ill_refrele(release_ill);
12963 }
12964 
12965 /*
12966  * Take down a specific interface, but don't lose any information about it.
12967  * (Always called as writer.)
12968  * This function goes through the down sequence even if the interface is
12969  * already down. There are 2 reasons.
12970  * a. Currently we permit interface routes that depend on down interfaces
12971  *    to be added. This behaviour itself is questionable. However it appears
12972  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12973  *    time. We go thru the cleanup in order to remove these routes.
12974  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12975  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12976  *    down, but we need to cleanup i.e. do ill_dl_down and
12977  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12978  *
12979  * IP-MT notes:
12980  *
12981  * Model of reference to interfaces.
12982  *
12983  * The following members in ipif_t track references to the ipif.
12984  *	int     ipif_refcnt;    Active reference count
12985  *
12986  * The following members in ill_t track references to the ill.
12987  *	int             ill_refcnt;     active refcnt
12988  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12989  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12990  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12991  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12992  *
12993  * Reference to an ipif or ill can be obtained in any of the following ways.
12994  *
12995  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12996  * Pointers to ipif / ill from other data structures viz ire and conn.
12997  * Implicit reference to the ipif / ill by holding a reference to the ire.
12998  *
12999  * The ipif/ill lookup functions return a reference held ipif / ill.
13000  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13001  * This is a purely dynamic reference count associated with threads holding
13002  * references to the ipif / ill. Pointers from other structures do not
13003  * count towards this reference count.
13004  *
13005  * ill_ire_cnt is the number of ire's associated with the
13006  * ill. This is incremented whenever a new ire is created referencing the
13007  * ill. This is done atomically inside ire_add_v[46] where the ire is
13008  * actually added to the ire hash table. The count is decremented in
13009  * ire_inactive where the ire is destroyed.
13010  *
13011  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13012  * This is incremented atomically in
13013  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13014  * table. Similarly it is decremented in ncec_inactive() where the ncec
13015  * is destroyed.
13016  *
13017  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13018  * incremented atomically in nce_add() where the nce is actually added to the
13019  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13020  * is destroyed.
13021  *
13022  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13023  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13024  *
13025  * Flow of ioctls involving interface down/up
13026  *
13027  * The following is the sequence of an attempt to set some critical flags on an
13028  * up interface.
13029  * ip_sioctl_flags
13030  * ipif_down
13031  * wait for ipif to be quiescent
13032  * ipif_down_tail
13033  * ip_sioctl_flags_tail
13034  *
13035  * All set ioctls that involve down/up sequence would have a skeleton similar
13036  * to the above. All the *tail functions are called after the refcounts have
13037  * dropped to the appropriate values.
13038  *
13039  * SIOC ioctls during the IPIF_CHANGING interval.
13040  *
13041  * Threads handling SIOC set ioctls serialize on the squeue, but this
13042  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13043  * steps of internal changes to the state, some of which are visible in
13044  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13045  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13046  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13047  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13048  * the current exclusive operation completes. The IPIF_CHANGING check
13049  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13050  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13051  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13052  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13053  * until we release the ipsq_lock, even though the ill/ipif state flags
13054  * can change after we drop the ill_lock.
13055  */
13056 int
13057 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13058 {
13059 	ill_t		*ill = ipif->ipif_ill;
13060 	conn_t		*connp;
13061 	boolean_t	success;
13062 	boolean_t	ipif_was_up = B_FALSE;
13063 	ip_stack_t	*ipst = ill->ill_ipst;
13064 
13065 	ASSERT(IAM_WRITER_IPIF(ipif));
13066 
13067 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13068 
13069 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13070 	    ill_t *, ill, ipif_t *, ipif);
13071 
13072 	if (ipif->ipif_flags & IPIF_UP) {
13073 		mutex_enter(&ill->ill_lock);
13074 		ipif->ipif_flags &= ~IPIF_UP;
13075 		ASSERT(ill->ill_ipif_up_count > 0);
13076 		--ill->ill_ipif_up_count;
13077 		mutex_exit(&ill->ill_lock);
13078 		ipif_was_up = B_TRUE;
13079 		/* Update status in SCTP's list */
13080 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13081 		ill_nic_event_dispatch(ipif->ipif_ill,
13082 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13083 	}
13084 
13085 	/*
13086 	 * Blow away memberships we established in ipif_multicast_up().
13087 	 */
13088 	ipif_multicast_down(ipif);
13089 
13090 	/*
13091 	 * Remove from the mapping for __sin6_src_id. We insert only
13092 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13093 	 * stored as mapped addresses, we need to check for mapped
13094 	 * INADDR_ANY also.
13095 	 */
13096 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13097 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13098 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13099 		int err;
13100 
13101 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13102 		    ipif->ipif_zoneid, ipst);
13103 		if (err != 0) {
13104 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13105 		}
13106 	}
13107 
13108 	if (ipif_was_up) {
13109 		/* only delete if we'd added ire's before */
13110 		if (ipif->ipif_isv6)
13111 			ipif_delete_ires_v6(ipif);
13112 		else
13113 			ipif_delete_ires_v4(ipif);
13114 	}
13115 
13116 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13117 		/*
13118 		 * Since the interface is now down, it may have just become
13119 		 * inactive.  Note that this needs to be done even for a
13120 		 * lll_logical_down(), or ARP entries will not get correctly
13121 		 * restored when the interface comes back up.
13122 		 */
13123 		if (IS_UNDER_IPMP(ill))
13124 			ipmp_ill_refresh_active(ill);
13125 	}
13126 
13127 	/*
13128 	 * neighbor-discovery or arp entries for this interface. The ipif
13129 	 * has to be quiesced, so we walk all the nce's and delete those
13130 	 * that point at the ipif->ipif_ill. At the same time, we also
13131 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13132 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13133 	 * that for ipif_down_tail()
13134 	 */
13135 	ipif_nce_down(ipif);
13136 
13137 	/*
13138 	 * If this is the last ipif on the ill, we also need to remove
13139 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13140 	 * never succeed.
13141 	 */
13142 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13143 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13144 
13145 	/*
13146 	 * Walk all CONNs that can have a reference on an ire for this
13147 	 * ipif (we actually walk all that now have stale references).
13148 	 */
13149 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13150 
13151 	/*
13152 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13153 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13154 	 * and ill_delete -> ipif_free -> ipif_down
13155 	 */
13156 	if (mp == NULL) {
13157 		ASSERT(q == NULL);
13158 		return (0);
13159 	}
13160 
13161 	if (CONN_Q(q)) {
13162 		connp = Q_TO_CONN(q);
13163 		mutex_enter(&connp->conn_lock);
13164 	} else {
13165 		connp = NULL;
13166 	}
13167 	mutex_enter(&ill->ill_lock);
13168 	/*
13169 	 * Are there any ire's pointing to this ipif that are still active ?
13170 	 * If this is the last ipif going down, are there any ire's pointing
13171 	 * to this ill that are still active ?
13172 	 */
13173 	if (ipif_is_quiescent(ipif)) {
13174 		mutex_exit(&ill->ill_lock);
13175 		if (connp != NULL)
13176 			mutex_exit(&connp->conn_lock);
13177 		return (0);
13178 	}
13179 
13180 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13181 	    ill->ill_name, (void *)ill));
13182 	/*
13183 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13184 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13185 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13186 	 */
13187 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13188 	if (!success) {
13189 		/* The conn is closing. So just return */
13190 		ASSERT(connp != NULL);
13191 		mutex_exit(&ill->ill_lock);
13192 		mutex_exit(&connp->conn_lock);
13193 		return (EINTR);
13194 	}
13195 
13196 	mutex_exit(&ill->ill_lock);
13197 	if (connp != NULL)
13198 		mutex_exit(&connp->conn_lock);
13199 	return (EINPROGRESS);
13200 }
13201 
13202 int
13203 ipif_down_tail(ipif_t *ipif)
13204 {
13205 	ill_t	*ill = ipif->ipif_ill;
13206 	int	err = 0;
13207 
13208 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13209 	    ill_t *, ill, ipif_t *, ipif);
13210 
13211 	/*
13212 	 * Skip any loopback interface (null wq).
13213 	 * If this is the last logical interface on the ill
13214 	 * have ill_dl_down tell the driver we are gone (unbind)
13215 	 * Note that lun 0 can ipif_down even though
13216 	 * there are other logical units that are up.
13217 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13218 	 */
13219 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13220 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13221 	    ill->ill_dl_up) {
13222 		ill_dl_down(ill);
13223 	}
13224 	if (!ipif->ipif_isv6)
13225 		err = ipif_arp_down(ipif);
13226 
13227 	ill->ill_logical_down = 0;
13228 
13229 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13230 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13231 	return (err);
13232 }
13233 
13234 /*
13235  * Bring interface logically down without bringing the physical interface
13236  * down e.g. when the netmask is changed. This avoids long lasting link
13237  * negotiations between an ethernet interface and a certain switches.
13238  */
13239 static int
13240 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13241 {
13242 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13243 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13244 
13245 	/*
13246 	 * The ill_logical_down flag is a transient flag. It is set here
13247 	 * and is cleared once the down has completed in ipif_down_tail.
13248 	 * This flag does not indicate whether the ill stream is in the
13249 	 * DL_BOUND state with the driver. Instead this flag is used by
13250 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13251 	 * the driver. The state of the ill stream i.e. whether it is
13252 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13253 	 */
13254 	ipif->ipif_ill->ill_logical_down = 1;
13255 	return (ipif_down(ipif, q, mp));
13256 }
13257 
13258 /*
13259  * Initiate deallocate of an IPIF. Always called as writer. Called by
13260  * ill_delete or ip_sioctl_removeif.
13261  */
13262 static void
13263 ipif_free(ipif_t *ipif)
13264 {
13265 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13266 
13267 	ASSERT(IAM_WRITER_IPIF(ipif));
13268 
13269 	if (ipif->ipif_recovery_id != 0)
13270 		(void) untimeout(ipif->ipif_recovery_id);
13271 	ipif->ipif_recovery_id = 0;
13272 
13273 	/*
13274 	 * Take down the interface. We can be called either from ill_delete
13275 	 * or from ip_sioctl_removeif.
13276 	 */
13277 	(void) ipif_down(ipif, NULL, NULL);
13278 
13279 	/*
13280 	 * Now that the interface is down, there's no chance it can still
13281 	 * become a duplicate.  Cancel any timer that may have been set while
13282 	 * tearing down.
13283 	 */
13284 	if (ipif->ipif_recovery_id != 0)
13285 		(void) untimeout(ipif->ipif_recovery_id);
13286 	ipif->ipif_recovery_id = 0;
13287 
13288 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13289 	/* Remove pointers to this ill in the multicast routing tables */
13290 	reset_mrt_vif_ipif(ipif);
13291 	/* If necessary, clear the cached source ipif rotor. */
13292 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13293 		ipif->ipif_ill->ill_src_ipif = NULL;
13294 	rw_exit(&ipst->ips_ill_g_lock);
13295 }
13296 
13297 static void
13298 ipif_free_tail(ipif_t *ipif)
13299 {
13300 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13301 
13302 	/*
13303 	 * Need to hold both ill_g_lock and ill_lock while
13304 	 * inserting or removing an ipif from the linked list
13305 	 * of ipifs hanging off the ill.
13306 	 */
13307 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13308 
13309 #ifdef DEBUG
13310 	ipif_trace_cleanup(ipif);
13311 #endif
13312 
13313 	/* Ask SCTP to take it out of it list */
13314 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13315 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13316 
13317 	/* Get it out of the ILL interface list. */
13318 	ipif_remove(ipif);
13319 	rw_exit(&ipst->ips_ill_g_lock);
13320 
13321 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13322 	ASSERT(ipif->ipif_recovery_id == 0);
13323 	ASSERT(ipif->ipif_ire_local == NULL);
13324 	ASSERT(ipif->ipif_ire_if == NULL);
13325 
13326 	/* Free the memory. */
13327 	mi_free(ipif);
13328 }
13329 
13330 /*
13331  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13332  * is zero.
13333  */
13334 void
13335 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13336 {
13337 	char	lbuf[LIFNAMSIZ];
13338 	char	*name;
13339 	size_t	name_len;
13340 
13341 	buf[0] = '\0';
13342 	name = ipif->ipif_ill->ill_name;
13343 	name_len = ipif->ipif_ill->ill_name_length;
13344 	if (ipif->ipif_id != 0) {
13345 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13346 		    ipif->ipif_id);
13347 		name = lbuf;
13348 		name_len = mi_strlen(name) + 1;
13349 	}
13350 	len -= 1;
13351 	buf[len] = '\0';
13352 	len = MIN(len, name_len);
13353 	bcopy(name, buf, len);
13354 }
13355 
13356 /*
13357  * Sets `buf' to an ill name.
13358  */
13359 void
13360 ill_get_name(const ill_t *ill, char *buf, int len)
13361 {
13362 	char	*name;
13363 	size_t	name_len;
13364 
13365 	name = ill->ill_name;
13366 	name_len = ill->ill_name_length;
13367 	len -= 1;
13368 	buf[len] = '\0';
13369 	len = MIN(len, name_len);
13370 	bcopy(name, buf, len);
13371 }
13372 
13373 /*
13374  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13375  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13376  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13377  * (May be called as writer.)
13378  */
13379 static ipif_t *
13380 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13381     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13382 {
13383 	char	*cp;
13384 	char	*endp;
13385 	long	id;
13386 	ill_t	*ill;
13387 	ipif_t	*ipif;
13388 	uint_t	ire_type;
13389 	boolean_t did_alloc = B_FALSE;
13390 
13391 	/*
13392 	 * If the caller wants to us to create the ipif, make sure we have a
13393 	 * valid zoneid
13394 	 */
13395 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13396 
13397 	if (namelen == 0) {
13398 		return (NULL);
13399 	}
13400 
13401 	*exists = B_FALSE;
13402 	/* Look for a colon in the name. */
13403 	endp = &name[namelen];
13404 	for (cp = endp; --cp > name; ) {
13405 		if (*cp == IPIF_SEPARATOR_CHAR)
13406 			break;
13407 	}
13408 
13409 	if (*cp == IPIF_SEPARATOR_CHAR) {
13410 		/*
13411 		 * Reject any non-decimal aliases for logical
13412 		 * interfaces. Aliases with leading zeroes
13413 		 * are also rejected as they introduce ambiguity
13414 		 * in the naming of the interfaces.
13415 		 * In order to confirm with existing semantics,
13416 		 * and to not break any programs/script relying
13417 		 * on that behaviour, if<0>:0 is considered to be
13418 		 * a valid interface.
13419 		 *
13420 		 * If alias has two or more digits and the first
13421 		 * is zero, fail.
13422 		 */
13423 		if (&cp[2] < endp && cp[1] == '0') {
13424 			return (NULL);
13425 		}
13426 	}
13427 
13428 	if (cp <= name) {
13429 		cp = endp;
13430 	} else {
13431 		*cp = '\0';
13432 	}
13433 
13434 	/*
13435 	 * Look up the ILL, based on the portion of the name
13436 	 * before the slash. ill_lookup_on_name returns a held ill.
13437 	 * Temporary to check whether ill exists already. If so
13438 	 * ill_lookup_on_name will clear it.
13439 	 */
13440 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13441 	    &did_alloc, ipst);
13442 	if (cp != endp)
13443 		*cp = IPIF_SEPARATOR_CHAR;
13444 	if (ill == NULL)
13445 		return (NULL);
13446 
13447 	/* Establish the unit number in the name. */
13448 	id = 0;
13449 	if (cp < endp && *endp == '\0') {
13450 		/* If there was a colon, the unit number follows. */
13451 		cp++;
13452 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13453 			ill_refrele(ill);
13454 			return (NULL);
13455 		}
13456 	}
13457 
13458 	mutex_enter(&ill->ill_lock);
13459 	/* Now see if there is an IPIF with this unit number. */
13460 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13461 		if (ipif->ipif_id == id) {
13462 			if (zoneid != ALL_ZONES &&
13463 			    zoneid != ipif->ipif_zoneid &&
13464 			    ipif->ipif_zoneid != ALL_ZONES) {
13465 				mutex_exit(&ill->ill_lock);
13466 				ill_refrele(ill);
13467 				return (NULL);
13468 			}
13469 			if (IPIF_CAN_LOOKUP(ipif)) {
13470 				ipif_refhold_locked(ipif);
13471 				mutex_exit(&ill->ill_lock);
13472 				if (!did_alloc)
13473 					*exists = B_TRUE;
13474 				/*
13475 				 * Drop locks before calling ill_refrele
13476 				 * since it can potentially call into
13477 				 * ipif_ill_refrele_tail which can end up
13478 				 * in trying to acquire any lock.
13479 				 */
13480 				ill_refrele(ill);
13481 				return (ipif);
13482 			}
13483 		}
13484 	}
13485 
13486 	if (!do_alloc) {
13487 		mutex_exit(&ill->ill_lock);
13488 		ill_refrele(ill);
13489 		return (NULL);
13490 	}
13491 
13492 	/*
13493 	 * If none found, atomically allocate and return a new one.
13494 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13495 	 * to support "receive only" use of lo0:1 etc. as is still done
13496 	 * below as an initial guess.
13497 	 * However, this is now likely to be overriden later in ipif_up_done()
13498 	 * when we know for sure what address has been configured on the
13499 	 * interface, since we might have more than one loopback interface
13500 	 * with a loopback address, e.g. in the case of zones, and all the
13501 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13502 	 */
13503 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13504 		ire_type = IRE_LOOPBACK;
13505 	else
13506 		ire_type = IRE_LOCAL;
13507 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13508 	if (ipif != NULL)
13509 		ipif_refhold_locked(ipif);
13510 	mutex_exit(&ill->ill_lock);
13511 	ill_refrele(ill);
13512 	return (ipif);
13513 }
13514 
13515 /*
13516  * This routine is called whenever a new address comes up on an ipif.  If
13517  * we are configured to respond to address mask requests, then we are supposed
13518  * to broadcast an address mask reply at this time.  This routine is also
13519  * called if we are already up, but a netmask change is made.  This is legal
13520  * but might not make the system manager very popular.	(May be called
13521  * as writer.)
13522  */
13523 void
13524 ipif_mask_reply(ipif_t *ipif)
13525 {
13526 	icmph_t	*icmph;
13527 	ipha_t	*ipha;
13528 	mblk_t	*mp;
13529 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13530 	ip_xmit_attr_t ixas;
13531 
13532 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13533 
13534 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13535 		return;
13536 
13537 	/* ICMP mask reply is IPv4 only */
13538 	ASSERT(!ipif->ipif_isv6);
13539 	/* ICMP mask reply is not for a loopback interface */
13540 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13541 
13542 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13543 		return;
13544 
13545 	mp = allocb(REPLY_LEN, BPRI_HI);
13546 	if (mp == NULL)
13547 		return;
13548 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13549 
13550 	ipha = (ipha_t *)mp->b_rptr;
13551 	bzero(ipha, REPLY_LEN);
13552 	*ipha = icmp_ipha;
13553 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13554 	ipha->ipha_src = ipif->ipif_lcl_addr;
13555 	ipha->ipha_dst = ipif->ipif_brd_addr;
13556 	ipha->ipha_length = htons(REPLY_LEN);
13557 	ipha->ipha_ident = 0;
13558 
13559 	icmph = (icmph_t *)&ipha[1];
13560 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13561 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13562 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13563 
13564 	bzero(&ixas, sizeof (ixas));
13565 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13566 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13567 	ixas.ixa_zoneid = ALL_ZONES;
13568 	ixas.ixa_ifindex = 0;
13569 	ixas.ixa_ipst = ipst;
13570 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13571 	(void) ip_output_simple(mp, &ixas);
13572 	ixa_cleanup(&ixas);
13573 #undef	REPLY_LEN
13574 }
13575 
13576 /*
13577  * Join the ipif specific multicast groups.
13578  * Must be called after a mapping has been set up in the resolver.  (Always
13579  * called as writer.)
13580  */
13581 void
13582 ipif_multicast_up(ipif_t *ipif)
13583 {
13584 	int err;
13585 	ill_t *ill;
13586 	ilm_t *ilm;
13587 
13588 	ASSERT(IAM_WRITER_IPIF(ipif));
13589 
13590 	ill = ipif->ipif_ill;
13591 
13592 	ip1dbg(("ipif_multicast_up\n"));
13593 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13594 	    ipif->ipif_allhosts_ilm != NULL)
13595 		return;
13596 
13597 	if (ipif->ipif_isv6) {
13598 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13599 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13600 
13601 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13602 
13603 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13604 			return;
13605 
13606 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13607 
13608 		/*
13609 		 * Join the all hosts multicast address.  We skip this for
13610 		 * underlying IPMP interfaces since they should be invisible.
13611 		 */
13612 		if (!IS_UNDER_IPMP(ill)) {
13613 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13614 			    &err);
13615 			if (ilm == NULL) {
13616 				ASSERT(err != 0);
13617 				ip0dbg(("ipif_multicast_up: "
13618 				    "all_hosts_mcast failed %d\n", err));
13619 				return;
13620 			}
13621 			ipif->ipif_allhosts_ilm = ilm;
13622 		}
13623 
13624 		/*
13625 		 * Enable multicast for the solicited node multicast address.
13626 		 * If IPMP we need to put the membership on the upper ill.
13627 		 */
13628 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13629 			ill_t *mcast_ill = NULL;
13630 			boolean_t need_refrele;
13631 
13632 			if (IS_UNDER_IPMP(ill) &&
13633 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13634 				need_refrele = B_TRUE;
13635 			} else {
13636 				mcast_ill = ill;
13637 				need_refrele = B_FALSE;
13638 			}
13639 
13640 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13641 			    ipif->ipif_zoneid, &err);
13642 			if (need_refrele)
13643 				ill_refrele(mcast_ill);
13644 
13645 			if (ilm == NULL) {
13646 				ASSERT(err != 0);
13647 				ip0dbg(("ipif_multicast_up: solicited MC"
13648 				    " failed %d\n", err));
13649 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13650 					ipif->ipif_allhosts_ilm = NULL;
13651 					(void) ip_delmulti(ilm);
13652 				}
13653 				return;
13654 			}
13655 			ipif->ipif_solmulti_ilm = ilm;
13656 		}
13657 	} else {
13658 		in6_addr_t v6group;
13659 
13660 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13661 			return;
13662 
13663 		/* Join the all hosts multicast address */
13664 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13665 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13666 
13667 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13668 		if (ilm == NULL) {
13669 			ASSERT(err != 0);
13670 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13671 			return;
13672 		}
13673 		ipif->ipif_allhosts_ilm = ilm;
13674 	}
13675 }
13676 
13677 /*
13678  * Blow away any multicast groups that we joined in ipif_multicast_up().
13679  * (ilms from explicit memberships are handled in conn_update_ill.)
13680  */
13681 void
13682 ipif_multicast_down(ipif_t *ipif)
13683 {
13684 	ASSERT(IAM_WRITER_IPIF(ipif));
13685 
13686 	ip1dbg(("ipif_multicast_down\n"));
13687 
13688 	if (ipif->ipif_allhosts_ilm != NULL) {
13689 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13690 		ipif->ipif_allhosts_ilm = NULL;
13691 	}
13692 	if (ipif->ipif_solmulti_ilm != NULL) {
13693 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13694 		ipif->ipif_solmulti_ilm = NULL;
13695 	}
13696 }
13697 
13698 /*
13699  * Used when an interface comes up to recreate any extra routes on this
13700  * interface.
13701  */
13702 int
13703 ill_recover_saved_ire(ill_t *ill)
13704 {
13705 	mblk_t		*mp;
13706 	ip_stack_t	*ipst = ill->ill_ipst;
13707 
13708 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13709 
13710 	mutex_enter(&ill->ill_saved_ire_lock);
13711 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13712 		ire_t		*ire, *nire;
13713 		ifrt_t		*ifrt;
13714 
13715 		ifrt = (ifrt_t *)mp->b_rptr;
13716 		/*
13717 		 * Create a copy of the IRE with the saved address and netmask.
13718 		 */
13719 		if (ill->ill_isv6) {
13720 			ire = ire_create_v6(
13721 			    &ifrt->ifrt_v6addr,
13722 			    &ifrt->ifrt_v6mask,
13723 			    &ifrt->ifrt_v6gateway_addr,
13724 			    ifrt->ifrt_type,
13725 			    ill,
13726 			    ifrt->ifrt_zoneid,
13727 			    ifrt->ifrt_flags,
13728 			    NULL,
13729 			    ipst);
13730 		} else {
13731 			ire = ire_create(
13732 			    (uint8_t *)&ifrt->ifrt_addr,
13733 			    (uint8_t *)&ifrt->ifrt_mask,
13734 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13735 			    ifrt->ifrt_type,
13736 			    ill,
13737 			    ifrt->ifrt_zoneid,
13738 			    ifrt->ifrt_flags,
13739 			    NULL,
13740 			    ipst);
13741 		}
13742 		if (ire == NULL) {
13743 			mutex_exit(&ill->ill_saved_ire_lock);
13744 			return (ENOMEM);
13745 		}
13746 
13747 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13748 			if (ill->ill_isv6) {
13749 				ire->ire_setsrc_addr_v6 =
13750 				    ifrt->ifrt_v6setsrc_addr;
13751 			} else {
13752 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13753 			}
13754 		}
13755 
13756 		/*
13757 		 * Some software (for example, GateD and Sun Cluster) attempts
13758 		 * to create (what amount to) IRE_PREFIX routes with the
13759 		 * loopback address as the gateway.  This is primarily done to
13760 		 * set up prefixes with the RTF_REJECT flag set (for example,
13761 		 * when generating aggregate routes.)
13762 		 *
13763 		 * If the IRE type (as defined by ill->ill_net_type) is
13764 		 * IRE_LOOPBACK, then we map the request into a
13765 		 * IRE_IF_NORESOLVER.
13766 		 */
13767 		if (ill->ill_net_type == IRE_LOOPBACK)
13768 			ire->ire_type = IRE_IF_NORESOLVER;
13769 
13770 		/*
13771 		 * ire held by ire_add, will be refreled' towards the
13772 		 * the end of ipif_up_done
13773 		 */
13774 		nire = ire_add(ire);
13775 		/*
13776 		 * Check if it was a duplicate entry. This handles
13777 		 * the case of two racing route adds for the same route
13778 		 */
13779 		if (nire == NULL) {
13780 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13781 		} else if (nire != ire) {
13782 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13783 			    (void *)nire));
13784 			ire_delete(nire);
13785 		} else {
13786 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13787 			    (void *)nire));
13788 		}
13789 		if (nire != NULL)
13790 			ire_refrele(nire);
13791 	}
13792 	mutex_exit(&ill->ill_saved_ire_lock);
13793 	return (0);
13794 }
13795 
13796 /*
13797  * Used to set the netmask and broadcast address to default values when the
13798  * interface is brought up.  (Always called as writer.)
13799  */
13800 static void
13801 ipif_set_default(ipif_t *ipif)
13802 {
13803 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13804 
13805 	if (!ipif->ipif_isv6) {
13806 		/*
13807 		 * Interface holds an IPv4 address. Default
13808 		 * mask is the natural netmask.
13809 		 */
13810 		if (!ipif->ipif_net_mask) {
13811 			ipaddr_t	v4mask;
13812 
13813 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13814 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13815 		}
13816 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13817 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13818 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13819 		} else {
13820 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13821 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13822 		}
13823 		/*
13824 		 * NOTE: SunOS 4.X does this even if the broadcast address
13825 		 * has been already set thus we do the same here.
13826 		 */
13827 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13828 			ipaddr_t	v4addr;
13829 
13830 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13831 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13832 		}
13833 	} else {
13834 		/*
13835 		 * Interface holds an IPv6-only address.  Default
13836 		 * mask is all-ones.
13837 		 */
13838 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13839 			ipif->ipif_v6net_mask = ipv6_all_ones;
13840 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13841 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13842 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13843 		} else {
13844 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13845 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13846 		}
13847 	}
13848 }
13849 
13850 /*
13851  * Return 0 if this address can be used as local address without causing
13852  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13853  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13854  * Note that the same IPv6 link-local address is allowed as long as the ills
13855  * are not on the same link.
13856  */
13857 int
13858 ip_addr_availability_check(ipif_t *new_ipif)
13859 {
13860 	in6_addr_t our_v6addr;
13861 	ill_t *ill;
13862 	ipif_t *ipif;
13863 	ill_walk_context_t ctx;
13864 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13865 
13866 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13867 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13868 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13869 
13870 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13871 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13872 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13873 		return (0);
13874 
13875 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13876 
13877 	if (new_ipif->ipif_isv6)
13878 		ill = ILL_START_WALK_V6(&ctx, ipst);
13879 	else
13880 		ill = ILL_START_WALK_V4(&ctx, ipst);
13881 
13882 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13883 		for (ipif = ill->ill_ipif; ipif != NULL;
13884 		    ipif = ipif->ipif_next) {
13885 			if ((ipif == new_ipif) ||
13886 			    !(ipif->ipif_flags & IPIF_UP) ||
13887 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13888 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13889 			    &our_v6addr))
13890 				continue;
13891 
13892 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13893 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13894 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13895 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13896 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13897 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13898 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13899 				continue;
13900 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13901 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13902 				continue;
13903 			else if (new_ipif->ipif_ill == ill)
13904 				return (EADDRINUSE);
13905 			else
13906 				return (EADDRNOTAVAIL);
13907 		}
13908 	}
13909 
13910 	return (0);
13911 }
13912 
13913 /*
13914  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13915  * IREs for the ipif.
13916  * When the routine returns EINPROGRESS then mp has been consumed and
13917  * the ioctl will be acked from ip_rput_dlpi.
13918  */
13919 int
13920 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13921 {
13922 	ill_t		*ill = ipif->ipif_ill;
13923 	boolean_t 	isv6 = ipif->ipif_isv6;
13924 	int		err = 0;
13925 	boolean_t	success;
13926 	uint_t		ipif_orig_id;
13927 	ip_stack_t	*ipst = ill->ill_ipst;
13928 
13929 	ASSERT(IAM_WRITER_IPIF(ipif));
13930 
13931 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13932 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13933 	    ill_t *, ill, ipif_t *, ipif);
13934 
13935 	/* Shouldn't get here if it is already up. */
13936 	if (ipif->ipif_flags & IPIF_UP)
13937 		return (EALREADY);
13938 
13939 	/*
13940 	 * If this is a request to bring up a data address on an interface
13941 	 * under IPMP, then move the address to its IPMP meta-interface and
13942 	 * try to bring it up.  One complication is that the zeroth ipif for
13943 	 * an ill is special, in that every ill always has one, and that code
13944 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13945 	 */
13946 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13947 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13948 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13949 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13950 
13951 		/*
13952 		 * The ipif being brought up should be quiesced.  If it's not,
13953 		 * something has gone amiss and we need to bail out.  (If it's
13954 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13955 		 */
13956 		mutex_enter(&ill->ill_lock);
13957 		if (!ipif_is_quiescent(ipif)) {
13958 			mutex_exit(&ill->ill_lock);
13959 			return (EINVAL);
13960 		}
13961 		mutex_exit(&ill->ill_lock);
13962 
13963 		/*
13964 		 * If we're going to need to allocate ipifs, do it prior
13965 		 * to starting the move (and grabbing locks).
13966 		 */
13967 		if (ipif->ipif_id == 0) {
13968 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13969 			    B_FALSE, &err)) == NULL) {
13970 				return (err);
13971 			}
13972 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13973 			    B_FALSE, &err)) == NULL) {
13974 				mi_free(moveipif);
13975 				return (err);
13976 			}
13977 		}
13978 
13979 		/*
13980 		 * Grab or transfer the ipif to move.  During the move, keep
13981 		 * ill_g_lock held to prevent any ill walker threads from
13982 		 * seeing things in an inconsistent state.
13983 		 */
13984 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13985 		if (ipif->ipif_id != 0) {
13986 			ipif_remove(ipif);
13987 		} else {
13988 			ipif_transfer(ipif, moveipif, stubipif);
13989 			ipif = moveipif;
13990 		}
13991 
13992 		/*
13993 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13994 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13995 		 * replace that one.  Otherwise, pick the next available slot.
13996 		 */
13997 		ipif->ipif_ill = ipmp_ill;
13998 		ipif_orig_id = ipif->ipif_id;
13999 
14000 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14001 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14002 			ipif = ipmp_ill->ill_ipif;
14003 		} else {
14004 			ipif->ipif_id = -1;
14005 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14006 				/*
14007 				 * No more available ipif_id's -- put it back
14008 				 * on the original ill and fail the operation.
14009 				 * Since we're writer on the ill, we can be
14010 				 * sure our old slot is still available.
14011 				 */
14012 				ipif->ipif_id = ipif_orig_id;
14013 				ipif->ipif_ill = ill;
14014 				if (ipif_orig_id == 0) {
14015 					ipif_transfer(ipif, ill->ill_ipif,
14016 					    NULL);
14017 				} else {
14018 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14019 				}
14020 				rw_exit(&ipst->ips_ill_g_lock);
14021 				return (err);
14022 			}
14023 		}
14024 		rw_exit(&ipst->ips_ill_g_lock);
14025 
14026 		/*
14027 		 * Tell SCTP that the ipif has moved.  Note that even if we
14028 		 * had to allocate a new ipif, the original sequence id was
14029 		 * preserved and therefore SCTP won't know.
14030 		 */
14031 		sctp_move_ipif(ipif, ill, ipmp_ill);
14032 
14033 		/*
14034 		 * If the ipif being brought up was on slot zero, then we
14035 		 * first need to bring up the placeholder we stuck there.  In
14036 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14037 		 * call to ipif_up() itself, if we successfully bring up the
14038 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14039 		 */
14040 		if (ipif_orig_id == 0) {
14041 			ASSERT(ill->ill_move_ipif == NULL);
14042 			ill->ill_move_ipif = ipif;
14043 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14044 				ASSERT(ill->ill_move_ipif == NULL);
14045 			if (err != EINPROGRESS)
14046 				ill->ill_move_ipif = NULL;
14047 			return (err);
14048 		}
14049 
14050 		/*
14051 		 * Bring it up on the IPMP ill.
14052 		 */
14053 		return (ipif_up(ipif, q, mp));
14054 	}
14055 
14056 	/* Skip arp/ndp for any loopback interface. */
14057 	if (ill->ill_wq != NULL) {
14058 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14059 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14060 
14061 		if (!ill->ill_dl_up) {
14062 			/*
14063 			 * ill_dl_up is not yet set. i.e. we are yet to
14064 			 * DL_BIND with the driver and this is the first
14065 			 * logical interface on the ill to become "up".
14066 			 * Tell the driver to get going (via DL_BIND_REQ).
14067 			 * Note that changing "significant" IFF_ flags
14068 			 * address/netmask etc cause a down/up dance, but
14069 			 * does not cause an unbind (DL_UNBIND) with the driver
14070 			 */
14071 			return (ill_dl_up(ill, ipif, mp, q));
14072 		}
14073 
14074 		/*
14075 		 * ipif_resolver_up may end up needeing to bind/attach
14076 		 * the ARP stream, which in turn necessitates a
14077 		 * DLPI message exchange with the driver. ioctls are
14078 		 * serialized and so we cannot send more than one
14079 		 * interface up message at a time. If ipif_resolver_up
14080 		 * does need to wait for the DLPI handshake for the ARP stream,
14081 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14082 		 */
14083 
14084 		ASSERT(connp != NULL || !CONN_Q(q));
14085 		if (connp != NULL)
14086 			mutex_enter(&connp->conn_lock);
14087 		mutex_enter(&ill->ill_lock);
14088 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14089 		mutex_exit(&ill->ill_lock);
14090 		if (connp != NULL)
14091 			mutex_exit(&connp->conn_lock);
14092 		if (!success)
14093 			return (EINTR);
14094 
14095 		/*
14096 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14097 		 * complete when ipif_ndp_up returns.
14098 		 */
14099 		err = ipif_resolver_up(ipif, Res_act_initial);
14100 		if (err == EINPROGRESS) {
14101 			/* We will complete it in arp_bringup_done() */
14102 			return (err);
14103 		}
14104 
14105 		if (isv6 && err == 0)
14106 			err = ipif_ndp_up(ipif, B_TRUE);
14107 
14108 		ASSERT(err != EINPROGRESS);
14109 		mp = ipsq_pending_mp_get(ipsq, &connp);
14110 		ASSERT(mp != NULL);
14111 		if (err != 0)
14112 			return (err);
14113 	} else {
14114 		/*
14115 		 * Interfaces without underlying hardware don't do duplicate
14116 		 * address detection.
14117 		 */
14118 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14119 		ipif->ipif_addr_ready = 1;
14120 		err = ill_add_ires(ill);
14121 		/* allocation failure? */
14122 		if (err != 0)
14123 			return (err);
14124 	}
14125 
14126 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14127 	if (err == 0 && ill->ill_move_ipif != NULL) {
14128 		ipif = ill->ill_move_ipif;
14129 		ill->ill_move_ipif = NULL;
14130 		return (ipif_up(ipif, q, mp));
14131 	}
14132 	return (err);
14133 }
14134 
14135 /*
14136  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14137  * The identical set of IREs need to be removed in ill_delete_ires().
14138  */
14139 int
14140 ill_add_ires(ill_t *ill)
14141 {
14142 	ire_t	*ire;
14143 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14144 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14145 
14146 	if (ill->ill_ire_multicast != NULL)
14147 		return (0);
14148 
14149 	/*
14150 	 * provide some dummy ire_addr for creating the ire.
14151 	 */
14152 	if (ill->ill_isv6) {
14153 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14154 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14155 	} else {
14156 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14157 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14158 	}
14159 	if (ire == NULL)
14160 		return (ENOMEM);
14161 
14162 	ill->ill_ire_multicast = ire;
14163 	return (0);
14164 }
14165 
14166 void
14167 ill_delete_ires(ill_t *ill)
14168 {
14169 	if (ill->ill_ire_multicast != NULL) {
14170 		/*
14171 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14172 		 * which was taken without any th_tracing enabled.
14173 		 * We also mark it as condemned (note that it was never added)
14174 		 * so that caching conn's can move off of it.
14175 		 */
14176 		ire_make_condemned(ill->ill_ire_multicast);
14177 		ire_refrele_notr(ill->ill_ire_multicast);
14178 		ill->ill_ire_multicast = NULL;
14179 	}
14180 }
14181 
14182 /*
14183  * Perform a bind for the physical device.
14184  * When the routine returns EINPROGRESS then mp has been consumed and
14185  * the ioctl will be acked from ip_rput_dlpi.
14186  * Allocate an unbind message and save it until ipif_down.
14187  */
14188 static int
14189 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14190 {
14191 	mblk_t	*bind_mp = NULL;
14192 	mblk_t	*unbind_mp = NULL;
14193 	conn_t	*connp;
14194 	boolean_t success;
14195 	int	err;
14196 
14197 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14198 
14199 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14200 	ASSERT(IAM_WRITER_ILL(ill));
14201 	ASSERT(mp != NULL);
14202 
14203 	/*
14204 	 * Make sure we have an IRE_MULTICAST in case we immediately
14205 	 * start receiving packets.
14206 	 */
14207 	err = ill_add_ires(ill);
14208 	if (err != 0)
14209 		goto bad;
14210 
14211 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14212 	    DL_BIND_REQ);
14213 	if (bind_mp == NULL)
14214 		goto bad;
14215 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14216 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14217 
14218 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14219 	if (unbind_mp == NULL)
14220 		goto bad;
14221 
14222 	/*
14223 	 * Record state needed to complete this operation when the
14224 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14225 	 */
14226 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14227 	ASSERT(connp != NULL || !CONN_Q(q));
14228 	GRAB_CONN_LOCK(q);
14229 	mutex_enter(&ipif->ipif_ill->ill_lock);
14230 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14231 	mutex_exit(&ipif->ipif_ill->ill_lock);
14232 	RELEASE_CONN_LOCK(q);
14233 	if (!success)
14234 		goto bad;
14235 
14236 	/*
14237 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14238 	 * the interface goes down.
14239 	 */
14240 	ASSERT(ill->ill_unbind_mp == NULL);
14241 	ill->ill_unbind_mp = unbind_mp;
14242 
14243 	ill_dlpi_send(ill, bind_mp);
14244 	/* Send down link-layer capabilities probe if not already done. */
14245 	ill_capability_probe(ill);
14246 
14247 	/*
14248 	 * Sysid used to rely on the fact that netboots set domainname
14249 	 * and the like. Now that miniroot boots aren't strictly netboots
14250 	 * and miniroot network configuration is driven from userland
14251 	 * these things still need to be set. This situation can be detected
14252 	 * by comparing the interface being configured here to the one
14253 	 * dhcifname was set to reference by the boot loader. Once sysid is
14254 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14255 	 */
14256 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14257 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14258 	    (strlen(srpc_domain) == 0)) {
14259 		if (dhcpinit() != 0)
14260 			cmn_err(CE_WARN, "no cached dhcp response");
14261 	}
14262 
14263 	/*
14264 	 * This operation will complete in ip_rput_dlpi with either
14265 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14266 	 */
14267 	return (EINPROGRESS);
14268 bad:
14269 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14270 
14271 	freemsg(bind_mp);
14272 	freemsg(unbind_mp);
14273 	return (ENOMEM);
14274 }
14275 
14276 /* Add room for tcp+ip headers */
14277 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14278 
14279 /*
14280  * DLPI and ARP is up.
14281  * Create all the IREs associated with an interface. Bring up multicast.
14282  * Set the interface flag and finish other initialization
14283  * that potentially had to be deferred to after DL_BIND_ACK.
14284  */
14285 int
14286 ipif_up_done(ipif_t *ipif)
14287 {
14288 	ill_t		*ill = ipif->ipif_ill;
14289 	int		err = 0;
14290 	boolean_t	loopback = B_FALSE;
14291 	boolean_t	update_src_selection = B_TRUE;
14292 	ipif_t		*tmp_ipif;
14293 
14294 	ip1dbg(("ipif_up_done(%s:%u)\n",
14295 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14296 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14297 	    ill_t *, ill, ipif_t *, ipif);
14298 
14299 	/* Check if this is a loopback interface */
14300 	if (ipif->ipif_ill->ill_wq == NULL)
14301 		loopback = B_TRUE;
14302 
14303 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14304 
14305 	/*
14306 	 * If all other interfaces for this ill are down or DEPRECATED,
14307 	 * or otherwise unsuitable for source address selection,
14308 	 * reset the src generation numbers to make sure source
14309 	 * address selection gets to take this new ipif into account.
14310 	 * No need to hold ill_lock while traversing the ipif list since
14311 	 * we are writer
14312 	 */
14313 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14314 	    tmp_ipif = tmp_ipif->ipif_next) {
14315 		if (((tmp_ipif->ipif_flags &
14316 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14317 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14318 		    (tmp_ipif == ipif))
14319 			continue;
14320 		/* first useable pre-existing interface */
14321 		update_src_selection = B_FALSE;
14322 		break;
14323 	}
14324 	if (update_src_selection)
14325 		ip_update_source_selection(ill->ill_ipst);
14326 
14327 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14328 		nce_t *loop_nce = NULL;
14329 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14330 
14331 		/*
14332 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14333 		 * ipif_lookup_on_name(), but in the case of zones we can have
14334 		 * several loopback addresses on lo0. So all the interfaces with
14335 		 * loopback addresses need to be marked IRE_LOOPBACK.
14336 		 */
14337 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14338 		    htonl(INADDR_LOOPBACK))
14339 			ipif->ipif_ire_type = IRE_LOOPBACK;
14340 		else
14341 			ipif->ipif_ire_type = IRE_LOCAL;
14342 		if (ill->ill_net_type != IRE_LOOPBACK)
14343 			flags |= NCE_F_PUBLISH;
14344 
14345 		/* add unicast nce for the local addr */
14346 		err = nce_lookup_then_add_v4(ill, NULL,
14347 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14348 		    ND_REACHABLE, &loop_nce);
14349 		/* A shared-IP zone sees EEXIST for lo0:N */
14350 		if (err == 0 || err == EEXIST) {
14351 			ipif->ipif_added_nce = 1;
14352 			loop_nce->nce_ipif_cnt++;
14353 			nce_refrele(loop_nce);
14354 			err = 0;
14355 		} else {
14356 			ASSERT(loop_nce == NULL);
14357 			return (err);
14358 		}
14359 	}
14360 
14361 	/* Create all the IREs associated with this interface */
14362 	err = ipif_add_ires_v4(ipif, loopback);
14363 	if (err != 0) {
14364 		/*
14365 		 * see comments about return value from
14366 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14367 		 */
14368 		if (err != EADDRINUSE) {
14369 			(void) ipif_arp_down(ipif);
14370 		} else {
14371 			/*
14372 			 * Make IPMP aware of the deleted ipif so that
14373 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14374 			 * can be completed. Note that we do not want to
14375 			 * destroy the nce that was created on the ipmp_ill
14376 			 * for the active copy of the duplicate address in
14377 			 * use.
14378 			 */
14379 			if (IS_IPMP(ill))
14380 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14381 			err = EADDRNOTAVAIL;
14382 		}
14383 		return (err);
14384 	}
14385 
14386 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14387 		/* Recover any additional IREs entries for this ill */
14388 		(void) ill_recover_saved_ire(ill);
14389 	}
14390 
14391 	if (ill->ill_need_recover_multicast) {
14392 		/*
14393 		 * Need to recover all multicast memberships in the driver.
14394 		 * This had to be deferred until we had attached.  The same
14395 		 * code exists in ipif_up_done_v6() to recover IPv6
14396 		 * memberships.
14397 		 *
14398 		 * Note that it would be preferable to unconditionally do the
14399 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14400 		 * that since ill_join_allmulti() depends on ill_dl_up being
14401 		 * set, and it is not set until we receive a DL_BIND_ACK after
14402 		 * having called ill_dl_up().
14403 		 */
14404 		ill_recover_multicast(ill);
14405 	}
14406 
14407 	if (ill->ill_ipif_up_count == 1) {
14408 		/*
14409 		 * Since the interface is now up, it may now be active.
14410 		 */
14411 		if (IS_UNDER_IPMP(ill))
14412 			ipmp_ill_refresh_active(ill);
14413 
14414 		/*
14415 		 * If this is an IPMP interface, we may now be able to
14416 		 * establish ARP entries.
14417 		 */
14418 		if (IS_IPMP(ill))
14419 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14420 	}
14421 
14422 	/* Join the allhosts multicast address */
14423 	ipif_multicast_up(ipif);
14424 
14425 	if (!loopback && !update_src_selection &&
14426 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14427 		ip_update_source_selection(ill->ill_ipst);
14428 
14429 	if (!loopback && ipif->ipif_addr_ready) {
14430 		/* Broadcast an address mask reply. */
14431 		ipif_mask_reply(ipif);
14432 	}
14433 	/* Perhaps ilgs should use this ill */
14434 	update_conn_ill(NULL, ill->ill_ipst);
14435 
14436 	/*
14437 	 * This had to be deferred until we had bound.  Tell routing sockets and
14438 	 * others that this interface is up if it looks like the address has
14439 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14440 	 * duplicate address detection to do its thing.
14441 	 */
14442 	if (ipif->ipif_addr_ready)
14443 		ipif_up_notify(ipif);
14444 	return (0);
14445 }
14446 
14447 /*
14448  * Add the IREs associated with the ipif.
14449  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14450  */
14451 static int
14452 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14453 {
14454 	ill_t		*ill = ipif->ipif_ill;
14455 	ip_stack_t	*ipst = ill->ill_ipst;
14456 	ire_t		*ire_array[20];
14457 	ire_t		**irep = ire_array;
14458 	ire_t		**irep1;
14459 	ipaddr_t	net_mask = 0;
14460 	ipaddr_t	subnet_mask, route_mask;
14461 	int		err;
14462 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14463 	ire_t		*ire_if = NULL;
14464 	uchar_t		*gw;
14465 
14466 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14467 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14468 		/*
14469 		 * If we're on a labeled system then make sure that zone-
14470 		 * private addresses have proper remote host database entries.
14471 		 */
14472 		if (is_system_labeled() &&
14473 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14474 		    !tsol_check_interface_address(ipif))
14475 			return (EINVAL);
14476 
14477 		/* Register the source address for __sin6_src_id */
14478 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14479 		    ipif->ipif_zoneid, ipst);
14480 		if (err != 0) {
14481 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14482 			return (err);
14483 		}
14484 
14485 		if (loopback)
14486 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14487 		else
14488 			gw = NULL;
14489 
14490 		/* If the interface address is set, create the local IRE. */
14491 		ire_local = ire_create(
14492 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14493 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14494 		    gw,					/* gateway */
14495 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14496 		    ipif->ipif_ill,
14497 		    ipif->ipif_zoneid,
14498 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14499 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14500 		    NULL,
14501 		    ipst);
14502 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14503 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14504 		    ipif->ipif_ire_type,
14505 		    ntohl(ipif->ipif_lcl_addr)));
14506 		if (ire_local == NULL) {
14507 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14508 			err = ENOMEM;
14509 			goto bad;
14510 		}
14511 	} else {
14512 		ip1dbg((
14513 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14514 		    ipif->ipif_ire_type,
14515 		    ntohl(ipif->ipif_lcl_addr),
14516 		    (uint_t)ipif->ipif_flags));
14517 	}
14518 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14519 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14520 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14521 	} else {
14522 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14523 	}
14524 
14525 	subnet_mask = ipif->ipif_net_mask;
14526 
14527 	/*
14528 	 * If mask was not specified, use natural netmask of
14529 	 * interface address. Also, store this mask back into the
14530 	 * ipif struct.
14531 	 */
14532 	if (subnet_mask == 0) {
14533 		subnet_mask = net_mask;
14534 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14535 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14536 		    ipif->ipif_v6subnet);
14537 	}
14538 
14539 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14540 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14541 	    ipif->ipif_subnet != INADDR_ANY) {
14542 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14543 
14544 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14545 			route_mask = IP_HOST_MASK;
14546 		} else {
14547 			route_mask = subnet_mask;
14548 		}
14549 
14550 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14551 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14552 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14553 		    ntohl(ipif->ipif_subnet)));
14554 		ire_if = ire_create(
14555 		    (uchar_t *)&ipif->ipif_subnet,
14556 		    (uchar_t *)&route_mask,
14557 		    (uchar_t *)&ipif->ipif_lcl_addr,
14558 		    ill->ill_net_type,
14559 		    ill,
14560 		    ipif->ipif_zoneid,
14561 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14562 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14563 		    NULL,
14564 		    ipst);
14565 		if (ire_if == NULL) {
14566 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14567 			err = ENOMEM;
14568 			goto bad;
14569 		}
14570 	}
14571 
14572 	/*
14573 	 * Create any necessary broadcast IREs.
14574 	 */
14575 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14576 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14577 		irep = ipif_create_bcast_ires(ipif, irep);
14578 
14579 	/* If an earlier ire_create failed, get out now */
14580 	for (irep1 = irep; irep1 > ire_array; ) {
14581 		irep1--;
14582 		if (*irep1 == NULL) {
14583 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14584 			err = ENOMEM;
14585 			goto bad;
14586 		}
14587 	}
14588 
14589 	/*
14590 	 * Need to atomically check for IP address availability under
14591 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14592 	 * ills or new ipifs can be added while we are checking availability.
14593 	 */
14594 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14595 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14596 	/* Mark it up, and increment counters. */
14597 	ipif->ipif_flags |= IPIF_UP;
14598 	ill->ill_ipif_up_count++;
14599 	err = ip_addr_availability_check(ipif);
14600 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14601 	rw_exit(&ipst->ips_ill_g_lock);
14602 
14603 	if (err != 0) {
14604 		/*
14605 		 * Our address may already be up on the same ill. In this case,
14606 		 * the ARP entry for our ipif replaced the one for the other
14607 		 * ipif. So we don't want to delete it (otherwise the other ipif
14608 		 * would be unable to send packets).
14609 		 * ip_addr_availability_check() identifies this case for us and
14610 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14611 		 * which is the expected error code.
14612 		 */
14613 		ill->ill_ipif_up_count--;
14614 		ipif->ipif_flags &= ~IPIF_UP;
14615 		goto bad;
14616 	}
14617 
14618 	/*
14619 	 * Add in all newly created IREs.  ire_create_bcast() has
14620 	 * already checked for duplicates of the IRE_BROADCAST type.
14621 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14622 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14623 	 * a /32 route.
14624 	 */
14625 	if (ire_if != NULL) {
14626 		ire_if = ire_add(ire_if);
14627 		if (ire_if == NULL) {
14628 			err = ENOMEM;
14629 			goto bad2;
14630 		}
14631 #ifdef DEBUG
14632 		ire_refhold_notr(ire_if);
14633 		ire_refrele(ire_if);
14634 #endif
14635 	}
14636 	if (ire_local != NULL) {
14637 		ire_local = ire_add(ire_local);
14638 		if (ire_local == NULL) {
14639 			err = ENOMEM;
14640 			goto bad2;
14641 		}
14642 #ifdef DEBUG
14643 		ire_refhold_notr(ire_local);
14644 		ire_refrele(ire_local);
14645 #endif
14646 	}
14647 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14648 	if (ire_local != NULL)
14649 		ipif->ipif_ire_local = ire_local;
14650 	if (ire_if != NULL)
14651 		ipif->ipif_ire_if = ire_if;
14652 	rw_exit(&ipst->ips_ill_g_lock);
14653 	ire_local = NULL;
14654 	ire_if = NULL;
14655 
14656 	/*
14657 	 * We first add all of them, and if that succeeds we refrele the
14658 	 * bunch. That enables us to delete all of them should any of the
14659 	 * ire_adds fail.
14660 	 */
14661 	for (irep1 = irep; irep1 > ire_array; ) {
14662 		irep1--;
14663 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14664 		*irep1 = ire_add(*irep1);
14665 		if (*irep1 == NULL) {
14666 			err = ENOMEM;
14667 			goto bad2;
14668 		}
14669 	}
14670 
14671 	for (irep1 = irep; irep1 > ire_array; ) {
14672 		irep1--;
14673 		/* refheld by ire_add. */
14674 		if (*irep1 != NULL) {
14675 			ire_refrele(*irep1);
14676 			*irep1 = NULL;
14677 		}
14678 	}
14679 
14680 	if (!loopback) {
14681 		/*
14682 		 * If the broadcast address has been set, make sure it makes
14683 		 * sense based on the interface address.
14684 		 * Only match on ill since we are sharing broadcast addresses.
14685 		 */
14686 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14687 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14688 			ire_t	*ire;
14689 
14690 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14691 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14692 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14693 
14694 			if (ire == NULL) {
14695 				/*
14696 				 * If there isn't a matching broadcast IRE,
14697 				 * revert to the default for this netmask.
14698 				 */
14699 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14700 				mutex_enter(&ipif->ipif_ill->ill_lock);
14701 				ipif_set_default(ipif);
14702 				mutex_exit(&ipif->ipif_ill->ill_lock);
14703 			} else {
14704 				ire_refrele(ire);
14705 			}
14706 		}
14707 
14708 	}
14709 	return (0);
14710 
14711 bad2:
14712 	ill->ill_ipif_up_count--;
14713 	ipif->ipif_flags &= ~IPIF_UP;
14714 
14715 bad:
14716 	ip1dbg(("ipif_add_ires: FAILED \n"));
14717 	if (ire_local != NULL)
14718 		ire_delete(ire_local);
14719 	if (ire_if != NULL)
14720 		ire_delete(ire_if);
14721 
14722 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14723 	ire_local = ipif->ipif_ire_local;
14724 	ipif->ipif_ire_local = NULL;
14725 	ire_if = ipif->ipif_ire_if;
14726 	ipif->ipif_ire_if = NULL;
14727 	rw_exit(&ipst->ips_ill_g_lock);
14728 	if (ire_local != NULL) {
14729 		ire_delete(ire_local);
14730 		ire_refrele_notr(ire_local);
14731 	}
14732 	if (ire_if != NULL) {
14733 		ire_delete(ire_if);
14734 		ire_refrele_notr(ire_if);
14735 	}
14736 
14737 	while (irep > ire_array) {
14738 		irep--;
14739 		if (*irep != NULL) {
14740 			ire_delete(*irep);
14741 		}
14742 	}
14743 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14744 
14745 	return (err);
14746 }
14747 
14748 /* Remove all the IREs created by ipif_add_ires_v4 */
14749 void
14750 ipif_delete_ires_v4(ipif_t *ipif)
14751 {
14752 	ill_t		*ill = ipif->ipif_ill;
14753 	ip_stack_t	*ipst = ill->ill_ipst;
14754 	ire_t		*ire;
14755 
14756 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14757 	ire = ipif->ipif_ire_local;
14758 	ipif->ipif_ire_local = NULL;
14759 	rw_exit(&ipst->ips_ill_g_lock);
14760 	if (ire != NULL) {
14761 		/*
14762 		 * Move count to ipif so we don't loose the count due to
14763 		 * a down/up dance.
14764 		 */
14765 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14766 
14767 		ire_delete(ire);
14768 		ire_refrele_notr(ire);
14769 	}
14770 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14771 	ire = ipif->ipif_ire_if;
14772 	ipif->ipif_ire_if = NULL;
14773 	rw_exit(&ipst->ips_ill_g_lock);
14774 	if (ire != NULL) {
14775 		ire_delete(ire);
14776 		ire_refrele_notr(ire);
14777 	}
14778 
14779 	/*
14780 	 * Delete the broadcast IREs.
14781 	 */
14782 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14783 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14784 		ipif_delete_bcast_ires(ipif);
14785 }
14786 
14787 /*
14788  * Checks for availbility of a usable source address (if there is one) when the
14789  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14790  * this selection is done regardless of the destination.
14791  */
14792 boolean_t
14793 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14794     ip_stack_t *ipst)
14795 {
14796 	ipif_t		*ipif = NULL;
14797 	ill_t		*uill;
14798 
14799 	ASSERT(ifindex != 0);
14800 
14801 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14802 	if (uill == NULL)
14803 		return (B_FALSE);
14804 
14805 	mutex_enter(&uill->ill_lock);
14806 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14807 		if (IPIF_IS_CONDEMNED(ipif))
14808 			continue;
14809 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14810 			continue;
14811 		if (!(ipif->ipif_flags & IPIF_UP))
14812 			continue;
14813 		if (ipif->ipif_zoneid != zoneid)
14814 			continue;
14815 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14816 		    ipif->ipif_lcl_addr == INADDR_ANY)
14817 			continue;
14818 		mutex_exit(&uill->ill_lock);
14819 		ill_refrele(uill);
14820 		return (B_TRUE);
14821 	}
14822 	mutex_exit(&uill->ill_lock);
14823 	ill_refrele(uill);
14824 	return (B_FALSE);
14825 }
14826 
14827 /*
14828  * Find an ipif with a good local address on the ill+zoneid.
14829  */
14830 ipif_t *
14831 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14832 {
14833 	ipif_t		*ipif;
14834 
14835 	mutex_enter(&ill->ill_lock);
14836 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14837 		if (IPIF_IS_CONDEMNED(ipif))
14838 			continue;
14839 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14840 			continue;
14841 		if (!(ipif->ipif_flags & IPIF_UP))
14842 			continue;
14843 		if (ipif->ipif_zoneid != zoneid &&
14844 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14845 			continue;
14846 		if (ill->ill_isv6 ?
14847 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14848 		    ipif->ipif_lcl_addr == INADDR_ANY)
14849 			continue;
14850 		ipif_refhold_locked(ipif);
14851 		mutex_exit(&ill->ill_lock);
14852 		return (ipif);
14853 	}
14854 	mutex_exit(&ill->ill_lock);
14855 	return (NULL);
14856 }
14857 
14858 /*
14859  * IP source address type, sorted from worst to best.  For a given type,
14860  * always prefer IP addresses on the same subnet.  All-zones addresses are
14861  * suboptimal because they pose problems with unlabeled destinations.
14862  */
14863 typedef enum {
14864 	IPIF_NONE,
14865 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14866 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14867 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14868 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14869 	IPIF_DIFFNET,			/* normal and different subnet */
14870 	IPIF_SAMENET,			/* normal and same subnet */
14871 	IPIF_LOCALADDR			/* local loopback */
14872 } ipif_type_t;
14873 
14874 /*
14875  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14876  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14877  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14878  * the first one, unless IPMP is used in which case we round-robin among them;
14879  * see below for more.
14880  *
14881  * Returns NULL if there is no suitable source address for the ill.
14882  * This only occurs when there is no valid source address for the ill.
14883  */
14884 ipif_t *
14885 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14886     boolean_t allow_usesrc, boolean_t *notreadyp)
14887 {
14888 	ill_t	*usill = NULL;
14889 	ill_t	*ipmp_ill = NULL;
14890 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14891 	ipif_type_t type, best_type;
14892 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14893 	ip_stack_t *ipst = ill->ill_ipst;
14894 	boolean_t samenet;
14895 
14896 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14897 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14898 		    B_FALSE, ipst);
14899 		if (usill != NULL)
14900 			ill = usill;	/* Select source from usesrc ILL */
14901 		else
14902 			return (NULL);
14903 	}
14904 
14905 	/*
14906 	 * Test addresses should never be used for source address selection,
14907 	 * so if we were passed one, switch to the IPMP meta-interface.
14908 	 */
14909 	if (IS_UNDER_IPMP(ill)) {
14910 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14911 			ill = ipmp_ill;	/* Select source from IPMP ill */
14912 		else
14913 			return (NULL);
14914 	}
14915 
14916 	/*
14917 	 * If we're dealing with an unlabeled destination on a labeled system,
14918 	 * make sure that we ignore source addresses that are incompatible with
14919 	 * the destination's default label.  That destination's default label
14920 	 * must dominate the minimum label on the source address.
14921 	 */
14922 	dst_rhtp = NULL;
14923 	if (is_system_labeled()) {
14924 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14925 		if (dst_rhtp == NULL)
14926 			return (NULL);
14927 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14928 			TPC_RELE(dst_rhtp);
14929 			dst_rhtp = NULL;
14930 		}
14931 	}
14932 
14933 	/*
14934 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14935 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14936 	 * After selecting the right ipif, under ill_lock make sure ipif is
14937 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14938 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14939 	 * but not under a lock.
14940 	 */
14941 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14942 retry:
14943 	/*
14944 	 * For source address selection, we treat the ipif list as circular
14945 	 * and continue until we get back to where we started.  This allows
14946 	 * IPMP to vary source address selection (which improves inbound load
14947 	 * spreading) by caching its last ending point and starting from
14948 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14949 	 * ills since that can't happen on the IPMP ill.
14950 	 */
14951 	start_ipif = ill->ill_ipif;
14952 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14953 		start_ipif = ill->ill_src_ipif;
14954 
14955 	ipif = start_ipif;
14956 	best_ipif = NULL;
14957 	best_type = IPIF_NONE;
14958 	do {
14959 		if ((next_ipif = ipif->ipif_next) == NULL)
14960 			next_ipif = ill->ill_ipif;
14961 
14962 		if (IPIF_IS_CONDEMNED(ipif))
14963 			continue;
14964 		/* Always skip NOLOCAL and ANYCAST interfaces */
14965 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14966 			continue;
14967 		/* Always skip NOACCEPT interfaces */
14968 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14969 			continue;
14970 		if (!(ipif->ipif_flags & IPIF_UP))
14971 			continue;
14972 
14973 		if (!ipif->ipif_addr_ready) {
14974 			if (notreadyp != NULL)
14975 				*notreadyp = B_TRUE;
14976 			continue;
14977 		}
14978 
14979 		if (zoneid != ALL_ZONES &&
14980 		    ipif->ipif_zoneid != zoneid &&
14981 		    ipif->ipif_zoneid != ALL_ZONES)
14982 			continue;
14983 
14984 		/*
14985 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14986 		 * are not valid as source addresses.
14987 		 */
14988 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14989 			continue;
14990 
14991 		/*
14992 		 * Check compatibility of local address for destination's
14993 		 * default label if we're on a labeled system.	Incompatible
14994 		 * addresses can't be used at all.
14995 		 */
14996 		if (dst_rhtp != NULL) {
14997 			boolean_t incompat;
14998 
14999 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15000 			    IPV4_VERSION, B_FALSE);
15001 			if (src_rhtp == NULL)
15002 				continue;
15003 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15004 			    src_rhtp->tpc_tp.tp_doi !=
15005 			    dst_rhtp->tpc_tp.tp_doi ||
15006 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15007 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15008 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15009 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15010 			TPC_RELE(src_rhtp);
15011 			if (incompat)
15012 				continue;
15013 		}
15014 
15015 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15016 
15017 		if (ipif->ipif_lcl_addr == dst) {
15018 			type = IPIF_LOCALADDR;
15019 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15020 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15021 			    IPIF_DIFFNET_DEPRECATED;
15022 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15023 			type = samenet ? IPIF_SAMENET_ALLZONES :
15024 			    IPIF_DIFFNET_ALLZONES;
15025 		} else {
15026 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15027 		}
15028 
15029 		if (type > best_type) {
15030 			best_type = type;
15031 			best_ipif = ipif;
15032 			if (best_type == IPIF_LOCALADDR)
15033 				break; /* can't get better */
15034 		}
15035 	} while ((ipif = next_ipif) != start_ipif);
15036 
15037 	if ((ipif = best_ipif) != NULL) {
15038 		mutex_enter(&ipif->ipif_ill->ill_lock);
15039 		if (IPIF_IS_CONDEMNED(ipif)) {
15040 			mutex_exit(&ipif->ipif_ill->ill_lock);
15041 			goto retry;
15042 		}
15043 		ipif_refhold_locked(ipif);
15044 
15045 		/*
15046 		 * For IPMP, update the source ipif rotor to the next ipif,
15047 		 * provided we can look it up.  (We must not use it if it's
15048 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15049 		 * ipif_free() checked ill_src_ipif.)
15050 		 */
15051 		if (IS_IPMP(ill) && ipif != NULL) {
15052 			next_ipif = ipif->ipif_next;
15053 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15054 				ill->ill_src_ipif = next_ipif;
15055 			else
15056 				ill->ill_src_ipif = NULL;
15057 		}
15058 		mutex_exit(&ipif->ipif_ill->ill_lock);
15059 	}
15060 
15061 	rw_exit(&ipst->ips_ill_g_lock);
15062 	if (usill != NULL)
15063 		ill_refrele(usill);
15064 	if (ipmp_ill != NULL)
15065 		ill_refrele(ipmp_ill);
15066 	if (dst_rhtp != NULL)
15067 		TPC_RELE(dst_rhtp);
15068 
15069 #ifdef DEBUG
15070 	if (ipif == NULL) {
15071 		char buf1[INET6_ADDRSTRLEN];
15072 
15073 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15074 		    ill->ill_name,
15075 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15076 	} else {
15077 		char buf1[INET6_ADDRSTRLEN];
15078 		char buf2[INET6_ADDRSTRLEN];
15079 
15080 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15081 		    ipif->ipif_ill->ill_name,
15082 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15083 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15084 		    buf2, sizeof (buf2))));
15085 	}
15086 #endif /* DEBUG */
15087 	return (ipif);
15088 }
15089 
15090 /*
15091  * Pick a source address based on the destination ill and an optional setsrc
15092  * address.
15093  * The result is stored in srcp. If generation is set, then put the source
15094  * generation number there before we look for the source address (to avoid
15095  * missing changes in the set of source addresses.
15096  * If flagsp is set, then us it to pass back ipif_flags.
15097  *
15098  * If the caller wants to cache the returned source address and detect when
15099  * that might be stale, the caller should pass in a generation argument,
15100  * which the caller can later compare against ips_src_generation
15101  *
15102  * The precedence order for selecting an IPv4 source address is:
15103  *  - RTF_SETSRC on the offlink ire always wins.
15104  *  - If usrsrc is set, swap the ill to be the usesrc one.
15105  *  - If IPMP is used on the ill, select a random address from the most
15106  *    preferred ones below:
15107  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15108  * 2. Not deprecated, not ALL_ZONES
15109  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15110  * 4. Not deprecated, ALL_ZONES
15111  * 5. If onlink destination, same subnet and deprecated
15112  * 6. Deprecated.
15113  *
15114  * We have lower preference for ALL_ZONES IP addresses,
15115  * as they pose problems with unlabeled destinations.
15116  *
15117  * Note that when multiple IP addresses match e.g., #1 we pick
15118  * the first one if IPMP is not in use. With IPMP we randomize.
15119  */
15120 int
15121 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15122     ipaddr_t multicast_ifaddr,
15123     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15124     uint32_t *generation, uint64_t *flagsp)
15125 {
15126 	ipif_t *ipif;
15127 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15128 
15129 	if (flagsp != NULL)
15130 		*flagsp = 0;
15131 
15132 	/*
15133 	 * Need to grab the generation number before we check to
15134 	 * avoid a race with a change to the set of local addresses.
15135 	 * No lock needed since the thread which updates the set of local
15136 	 * addresses use ipif/ill locks and exit those (hence a store memory
15137 	 * barrier) before doing the atomic increase of ips_src_generation.
15138 	 */
15139 	if (generation != NULL) {
15140 		*generation = ipst->ips_src_generation;
15141 	}
15142 
15143 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15144 		*srcp = multicast_ifaddr;
15145 		return (0);
15146 	}
15147 
15148 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15149 	if (setsrc != INADDR_ANY) {
15150 		*srcp = setsrc;
15151 		return (0);
15152 	}
15153 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15154 	if (ipif == NULL) {
15155 		if (notready)
15156 			return (ENETDOWN);
15157 		else
15158 			return (EADDRNOTAVAIL);
15159 	}
15160 	*srcp = ipif->ipif_lcl_addr;
15161 	if (flagsp != NULL)
15162 		*flagsp = ipif->ipif_flags;
15163 	ipif_refrele(ipif);
15164 	return (0);
15165 }
15166 
15167 /* ARGSUSED */
15168 int
15169 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15170 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15171 {
15172 	/*
15173 	 * ill_phyint_reinit merged the v4 and v6 into a single
15174 	 * ipsq.  We might not have been able to complete the
15175 	 * operation in ipif_set_values, if we could not become
15176 	 * exclusive.  If so restart it here.
15177 	 */
15178 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15179 }
15180 
15181 /*
15182  * Can operate on either a module or a driver queue.
15183  * Returns an error if not a module queue.
15184  */
15185 /* ARGSUSED */
15186 int
15187 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15188     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15189 {
15190 	queue_t		*q1 = q;
15191 	char 		*cp;
15192 	char		interf_name[LIFNAMSIZ];
15193 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15194 
15195 	if (q->q_next == NULL) {
15196 		ip1dbg((
15197 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15198 		return (EINVAL);
15199 	}
15200 
15201 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15202 		return (EALREADY);
15203 
15204 	do {
15205 		q1 = q1->q_next;
15206 	} while (q1->q_next);
15207 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15208 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15209 
15210 	/*
15211 	 * Here we are not going to delay the ioack until after
15212 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15213 	 * original ioctl message before sending the requests.
15214 	 */
15215 	return (ipif_set_values(q, mp, interf_name, &ppa));
15216 }
15217 
15218 /* ARGSUSED */
15219 int
15220 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15221     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15222 {
15223 	return (ENXIO);
15224 }
15225 
15226 /*
15227  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15228  * `irep'.  Returns a pointer to the next free `irep' entry
15229  * A mirror exists in ipif_delete_bcast_ires().
15230  *
15231  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15232  * done in ire_add.
15233  */
15234 static ire_t **
15235 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15236 {
15237 	ipaddr_t addr;
15238 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15239 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15240 	ill_t *ill = ipif->ipif_ill;
15241 	zoneid_t zoneid = ipif->ipif_zoneid;
15242 
15243 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15244 
15245 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15246 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15247 
15248 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15249 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15250 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15251 
15252 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15253 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15254 
15255 	/*
15256 	 * For backward compatibility, we create net broadcast IREs based on
15257 	 * the old "IP address class system", since some old machines only
15258 	 * respond to these class derived net broadcast.  However, we must not
15259 	 * create these net broadcast IREs if the subnetmask is shorter than
15260 	 * the IP address class based derived netmask.  Otherwise, we may
15261 	 * create a net broadcast address which is the same as an IP address
15262 	 * on the subnet -- and then TCP will refuse to talk to that address.
15263 	 */
15264 	if (netmask < subnetmask) {
15265 		addr = netmask & ipif->ipif_subnet;
15266 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15267 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15268 	}
15269 
15270 	/*
15271 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15272 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15273 	 * created.  Creating these broadcast IREs will only create confusion
15274 	 * as `addr' will be the same as the IP address.
15275 	 */
15276 	if (subnetmask != 0xFFFFFFFF) {
15277 		addr = ipif->ipif_subnet;
15278 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15279 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15280 	}
15281 
15282 	return (irep);
15283 }
15284 
15285 /*
15286  * Mirror of ipif_create_bcast_ires()
15287  */
15288 static void
15289 ipif_delete_bcast_ires(ipif_t *ipif)
15290 {
15291 	ipaddr_t	addr;
15292 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15293 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15294 	ill_t		*ill = ipif->ipif_ill;
15295 	zoneid_t	zoneid = ipif->ipif_zoneid;
15296 	ire_t		*ire;
15297 
15298 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15299 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15300 
15301 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15302 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15303 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15304 
15305 	ire = ire_lookup_bcast(ill, 0, zoneid);
15306 	ASSERT(ire != NULL);
15307 	ire_delete(ire); ire_refrele(ire);
15308 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15309 	ASSERT(ire != NULL);
15310 	ire_delete(ire); ire_refrele(ire);
15311 
15312 	/*
15313 	 * For backward compatibility, we create net broadcast IREs based on
15314 	 * the old "IP address class system", since some old machines only
15315 	 * respond to these class derived net broadcast.  However, we must not
15316 	 * create these net broadcast IREs if the subnetmask is shorter than
15317 	 * the IP address class based derived netmask.  Otherwise, we may
15318 	 * create a net broadcast address which is the same as an IP address
15319 	 * on the subnet -- and then TCP will refuse to talk to that address.
15320 	 */
15321 	if (netmask < subnetmask) {
15322 		addr = netmask & ipif->ipif_subnet;
15323 		ire = ire_lookup_bcast(ill, addr, zoneid);
15324 		ASSERT(ire != NULL);
15325 		ire_delete(ire); ire_refrele(ire);
15326 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15327 		ASSERT(ire != NULL);
15328 		ire_delete(ire); ire_refrele(ire);
15329 	}
15330 
15331 	/*
15332 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15333 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15334 	 * created.  Creating these broadcast IREs will only create confusion
15335 	 * as `addr' will be the same as the IP address.
15336 	 */
15337 	if (subnetmask != 0xFFFFFFFF) {
15338 		addr = ipif->ipif_subnet;
15339 		ire = ire_lookup_bcast(ill, addr, zoneid);
15340 		ASSERT(ire != NULL);
15341 		ire_delete(ire); ire_refrele(ire);
15342 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15343 		ASSERT(ire != NULL);
15344 		ire_delete(ire); ire_refrele(ire);
15345 	}
15346 }
15347 
15348 /*
15349  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15350  * from lifr_flags and the name from lifr_name.
15351  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15352  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15353  * Returns EINPROGRESS when mp has been consumed by queueing it on
15354  * ipx_pending_mp and the ioctl will complete in ip_rput.
15355  *
15356  * Can operate on either a module or a driver queue.
15357  * Returns an error if not a module queue.
15358  */
15359 /* ARGSUSED */
15360 int
15361 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15362     ip_ioctl_cmd_t *ipip, void *if_req)
15363 {
15364 	ill_t	*ill = q->q_ptr;
15365 	phyint_t *phyi;
15366 	ip_stack_t *ipst;
15367 	struct lifreq *lifr = if_req;
15368 	uint64_t new_flags;
15369 
15370 	ASSERT(ipif != NULL);
15371 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15372 
15373 	if (q->q_next == NULL) {
15374 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15375 		return (EINVAL);
15376 	}
15377 
15378 	/*
15379 	 * If we are not writer on 'q' then this interface exists already
15380 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15381 	 * so return EALREADY.
15382 	 */
15383 	if (ill != ipif->ipif_ill)
15384 		return (EALREADY);
15385 
15386 	if (ill->ill_name[0] != '\0')
15387 		return (EALREADY);
15388 
15389 	/*
15390 	 * If there's another ill already with the requested name, ensure
15391 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15392 	 * fuse together two unrelated ills, which will cause chaos.
15393 	 */
15394 	ipst = ill->ill_ipst;
15395 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15396 	    lifr->lifr_name, NULL);
15397 	if (phyi != NULL) {
15398 		ill_t *ill_mate = phyi->phyint_illv4;
15399 
15400 		if (ill_mate == NULL)
15401 			ill_mate = phyi->phyint_illv6;
15402 		ASSERT(ill_mate != NULL);
15403 
15404 		if (ill_mate->ill_media->ip_m_mac_type !=
15405 		    ill->ill_media->ip_m_mac_type) {
15406 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15407 			    "use the same ill name on differing media\n"));
15408 			return (EINVAL);
15409 		}
15410 	}
15411 
15412 	/*
15413 	 * We start off as IFF_IPV4 in ipif_allocate and become
15414 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15415 	 * The only flags that we read from user space are IFF_IPV4,
15416 	 * IFF_IPV6, and IFF_BROADCAST.
15417 	 *
15418 	 * This ill has not been inserted into the global list.
15419 	 * So we are still single threaded and don't need any lock
15420 	 *
15421 	 * Saniy check the flags.
15422 	 */
15423 
15424 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15425 	    ((lifr->lifr_flags & IFF_IPV6) ||
15426 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15427 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15428 		    "or IPv6 i.e., no broadcast \n"));
15429 		return (EINVAL);
15430 	}
15431 
15432 	new_flags =
15433 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15434 
15435 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15436 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15437 		    "IFF_IPV4 or IFF_IPV6\n"));
15438 		return (EINVAL);
15439 	}
15440 
15441 	/*
15442 	 * We always start off as IPv4, so only need to check for IPv6.
15443 	 */
15444 	if ((new_flags & IFF_IPV6) != 0) {
15445 		ill->ill_flags |= ILLF_IPV6;
15446 		ill->ill_flags &= ~ILLF_IPV4;
15447 	}
15448 
15449 	if ((new_flags & IFF_BROADCAST) != 0)
15450 		ipif->ipif_flags |= IPIF_BROADCAST;
15451 	else
15452 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15453 
15454 	/* We started off as V4. */
15455 	if (ill->ill_flags & ILLF_IPV6) {
15456 		ill->ill_phyint->phyint_illv6 = ill;
15457 		ill->ill_phyint->phyint_illv4 = NULL;
15458 	}
15459 
15460 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15461 }
15462 
15463 /* ARGSUSED */
15464 int
15465 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15466     ip_ioctl_cmd_t *ipip, void *if_req)
15467 {
15468 	/*
15469 	 * ill_phyint_reinit merged the v4 and v6 into a single
15470 	 * ipsq.  We might not have been able to complete the
15471 	 * slifname in ipif_set_values, if we could not become
15472 	 * exclusive.  If so restart it here
15473 	 */
15474 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15475 }
15476 
15477 /*
15478  * Return a pointer to the ipif which matches the index, IP version type and
15479  * zoneid.
15480  */
15481 ipif_t *
15482 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15483     ip_stack_t *ipst)
15484 {
15485 	ill_t	*ill;
15486 	ipif_t	*ipif = NULL;
15487 
15488 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15489 	if (ill != NULL) {
15490 		mutex_enter(&ill->ill_lock);
15491 		for (ipif = ill->ill_ipif; ipif != NULL;
15492 		    ipif = ipif->ipif_next) {
15493 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15494 			    zoneid == ipif->ipif_zoneid ||
15495 			    ipif->ipif_zoneid == ALL_ZONES)) {
15496 				ipif_refhold_locked(ipif);
15497 				break;
15498 			}
15499 		}
15500 		mutex_exit(&ill->ill_lock);
15501 		ill_refrele(ill);
15502 	}
15503 	return (ipif);
15504 }
15505 
15506 /*
15507  * Change an existing physical interface's index. If the new index
15508  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15509  * Finally, we update other systems which may have a dependence on the
15510  * index value.
15511  */
15512 /* ARGSUSED */
15513 int
15514 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15515     ip_ioctl_cmd_t *ipip, void *ifreq)
15516 {
15517 	ill_t		*ill;
15518 	phyint_t	*phyi;
15519 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15520 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15521 	uint_t	old_index, index;
15522 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15523 	avl_index_t	where;
15524 
15525 	if (ipip->ipi_cmd_type == IF_CMD)
15526 		index = ifr->ifr_index;
15527 	else
15528 		index = lifr->lifr_index;
15529 
15530 	/*
15531 	 * Only allow on physical interface. Also, index zero is illegal.
15532 	 */
15533 	ill = ipif->ipif_ill;
15534 	phyi = ill->ill_phyint;
15535 	if (ipif->ipif_id != 0 || index == 0) {
15536 		return (EINVAL);
15537 	}
15538 
15539 	/* If the index is not changing, no work to do */
15540 	if (phyi->phyint_ifindex == index)
15541 		return (0);
15542 
15543 	/*
15544 	 * Use phyint_exists() to determine if the new interface index
15545 	 * is already in use. If the index is unused then we need to
15546 	 * change the phyint's position in the phyint_list_avl_by_index
15547 	 * tree. If we do not do this, subsequent lookups (using the new
15548 	 * index value) will not find the phyint.
15549 	 */
15550 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15551 	if (phyint_exists(index, ipst)) {
15552 		rw_exit(&ipst->ips_ill_g_lock);
15553 		return (EEXIST);
15554 	}
15555 
15556 	/*
15557 	 * The new index is unused. Set it in the phyint. However we must not
15558 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15559 	 * changes. The event must be bound to old ifindex value.
15560 	 */
15561 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15562 	    &index, sizeof (index));
15563 
15564 	old_index = phyi->phyint_ifindex;
15565 	phyi->phyint_ifindex = index;
15566 
15567 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15568 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15569 	    &index, &where);
15570 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15571 	    phyi, where);
15572 	rw_exit(&ipst->ips_ill_g_lock);
15573 
15574 	/* Update SCTP's ILL list */
15575 	sctp_ill_reindex(ill, old_index);
15576 
15577 	/* Send the routing sockets message */
15578 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15579 	if (ILL_OTHER(ill))
15580 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15581 
15582 	/* Perhaps ilgs should use this ill */
15583 	update_conn_ill(NULL, ill->ill_ipst);
15584 	return (0);
15585 }
15586 
15587 /* ARGSUSED */
15588 int
15589 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15590     ip_ioctl_cmd_t *ipip, void *ifreq)
15591 {
15592 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15593 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15594 
15595 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15596 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15597 	/* Get the interface index */
15598 	if (ipip->ipi_cmd_type == IF_CMD) {
15599 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15600 	} else {
15601 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15602 	}
15603 	return (0);
15604 }
15605 
15606 /* ARGSUSED */
15607 int
15608 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15609     ip_ioctl_cmd_t *ipip, void *ifreq)
15610 {
15611 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15612 
15613 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15614 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15615 	/* Get the interface zone */
15616 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15617 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15618 	return (0);
15619 }
15620 
15621 /*
15622  * Set the zoneid of an interface.
15623  */
15624 /* ARGSUSED */
15625 int
15626 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15627     ip_ioctl_cmd_t *ipip, void *ifreq)
15628 {
15629 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15630 	int err = 0;
15631 	boolean_t need_up = B_FALSE;
15632 	zone_t *zptr;
15633 	zone_status_t status;
15634 	zoneid_t zoneid;
15635 
15636 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15637 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15638 		if (!is_system_labeled())
15639 			return (ENOTSUP);
15640 		zoneid = GLOBAL_ZONEID;
15641 	}
15642 
15643 	/* cannot assign instance zero to a non-global zone */
15644 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15645 		return (ENOTSUP);
15646 
15647 	/*
15648 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15649 	 * the event of a race with the zone shutdown processing, since IP
15650 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15651 	 * interface will be cleaned up even if the zone is shut down
15652 	 * immediately after the status check. If the interface can't be brought
15653 	 * down right away, and the zone is shut down before the restart
15654 	 * function is called, we resolve the possible races by rechecking the
15655 	 * zone status in the restart function.
15656 	 */
15657 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15658 		return (EINVAL);
15659 	status = zone_status_get(zptr);
15660 	zone_rele(zptr);
15661 
15662 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15663 		return (EINVAL);
15664 
15665 	if (ipif->ipif_flags & IPIF_UP) {
15666 		/*
15667 		 * If the interface is already marked up,
15668 		 * we call ipif_down which will take care
15669 		 * of ditching any IREs that have been set
15670 		 * up based on the old interface address.
15671 		 */
15672 		err = ipif_logical_down(ipif, q, mp);
15673 		if (err == EINPROGRESS)
15674 			return (err);
15675 		(void) ipif_down_tail(ipif);
15676 		need_up = B_TRUE;
15677 	}
15678 
15679 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15680 	return (err);
15681 }
15682 
15683 static int
15684 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15685     queue_t *q, mblk_t *mp, boolean_t need_up)
15686 {
15687 	int	err = 0;
15688 	ip_stack_t	*ipst;
15689 
15690 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15691 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15692 
15693 	if (CONN_Q(q))
15694 		ipst = CONNQ_TO_IPST(q);
15695 	else
15696 		ipst = ILLQ_TO_IPST(q);
15697 
15698 	/*
15699 	 * For exclusive stacks we don't allow a different zoneid than
15700 	 * global.
15701 	 */
15702 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15703 	    zoneid != GLOBAL_ZONEID)
15704 		return (EINVAL);
15705 
15706 	/* Set the new zone id. */
15707 	ipif->ipif_zoneid = zoneid;
15708 
15709 	/* Update sctp list */
15710 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15711 
15712 	/* The default multicast interface might have changed */
15713 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15714 
15715 	if (need_up) {
15716 		/*
15717 		 * Now bring the interface back up.  If this
15718 		 * is the only IPIF for the ILL, ipif_up
15719 		 * will have to re-bind to the device, so
15720 		 * we may get back EINPROGRESS, in which
15721 		 * case, this IOCTL will get completed in
15722 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15723 		 */
15724 		err = ipif_up(ipif, q, mp);
15725 	}
15726 	return (err);
15727 }
15728 
15729 /* ARGSUSED */
15730 int
15731 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15732     ip_ioctl_cmd_t *ipip, void *if_req)
15733 {
15734 	struct lifreq *lifr = (struct lifreq *)if_req;
15735 	zoneid_t zoneid;
15736 	zone_t *zptr;
15737 	zone_status_t status;
15738 
15739 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15740 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15741 		zoneid = GLOBAL_ZONEID;
15742 
15743 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15744 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15745 
15746 	/*
15747 	 * We recheck the zone status to resolve the following race condition:
15748 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15749 	 * 2) hme0:1 is up and can't be brought down right away;
15750 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15751 	 * 3) zone "myzone" is halted; the zone status switches to
15752 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15753 	 * the interfaces to remove - hme0:1 is not returned because it's not
15754 	 * yet in "myzone", so it won't be removed;
15755 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15756 	 * status check here, we would have hme0:1 in "myzone" after it's been
15757 	 * destroyed.
15758 	 * Note that if the status check fails, we need to bring the interface
15759 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15760 	 * ipif_up_done[_v6]().
15761 	 */
15762 	status = ZONE_IS_UNINITIALIZED;
15763 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15764 		status = zone_status_get(zptr);
15765 		zone_rele(zptr);
15766 	}
15767 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15768 		if (ipif->ipif_isv6) {
15769 			(void) ipif_up_done_v6(ipif);
15770 		} else {
15771 			(void) ipif_up_done(ipif);
15772 		}
15773 		return (EINVAL);
15774 	}
15775 
15776 	(void) ipif_down_tail(ipif);
15777 
15778 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15779 	    B_TRUE));
15780 }
15781 
15782 /*
15783  * Return the number of addresses on `ill' with one or more of the values
15784  * in `set' set and all of the values in `clear' clear.
15785  */
15786 static uint_t
15787 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15788 {
15789 	ipif_t	*ipif;
15790 	uint_t	cnt = 0;
15791 
15792 	ASSERT(IAM_WRITER_ILL(ill));
15793 
15794 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15795 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15796 			cnt++;
15797 
15798 	return (cnt);
15799 }
15800 
15801 /*
15802  * Return the number of migratable addresses on `ill' that are under
15803  * application control.
15804  */
15805 uint_t
15806 ill_appaddr_cnt(const ill_t *ill)
15807 {
15808 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15809 	    IPIF_NOFAILOVER));
15810 }
15811 
15812 /*
15813  * Return the number of point-to-point addresses on `ill'.
15814  */
15815 uint_t
15816 ill_ptpaddr_cnt(const ill_t *ill)
15817 {
15818 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15819 }
15820 
15821 /* ARGSUSED */
15822 int
15823 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15824 	ip_ioctl_cmd_t *ipip, void *ifreq)
15825 {
15826 	struct lifreq	*lifr = ifreq;
15827 
15828 	ASSERT(q->q_next == NULL);
15829 	ASSERT(CONN_Q(q));
15830 
15831 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15832 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15833 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15834 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15835 
15836 	return (0);
15837 }
15838 
15839 /* Find the previous ILL in this usesrc group */
15840 static ill_t *
15841 ill_prev_usesrc(ill_t *uill)
15842 {
15843 	ill_t *ill;
15844 
15845 	for (ill = uill->ill_usesrc_grp_next;
15846 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15847 	    ill = ill->ill_usesrc_grp_next)
15848 		/* do nothing */;
15849 	return (ill);
15850 }
15851 
15852 /*
15853  * Release all members of the usesrc group. This routine is called
15854  * from ill_delete when the interface being unplumbed is the
15855  * group head.
15856  *
15857  * This silently clears the usesrc that ifconfig setup.
15858  * An alternative would be to keep that ifindex, and drop packets on the floor
15859  * since no source address can be selected.
15860  * Even if we keep the current semantics, don't need a lock and a linked list.
15861  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15862  * the one that is being removed. Issue is how we return the usesrc users
15863  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15864  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15865  * ill walk, but the walker would need to insert in the ioctl response.
15866  */
15867 static void
15868 ill_disband_usesrc_group(ill_t *uill)
15869 {
15870 	ill_t *next_ill, *tmp_ill;
15871 	ip_stack_t	*ipst = uill->ill_ipst;
15872 
15873 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15874 	next_ill = uill->ill_usesrc_grp_next;
15875 
15876 	do {
15877 		ASSERT(next_ill != NULL);
15878 		tmp_ill = next_ill->ill_usesrc_grp_next;
15879 		ASSERT(tmp_ill != NULL);
15880 		next_ill->ill_usesrc_grp_next = NULL;
15881 		next_ill->ill_usesrc_ifindex = 0;
15882 		next_ill = tmp_ill;
15883 	} while (next_ill->ill_usesrc_ifindex != 0);
15884 	uill->ill_usesrc_grp_next = NULL;
15885 }
15886 
15887 /*
15888  * Remove the client usesrc ILL from the list and relink to a new list
15889  */
15890 int
15891 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15892 {
15893 	ill_t *ill, *tmp_ill;
15894 	ip_stack_t	*ipst = ucill->ill_ipst;
15895 
15896 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15897 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15898 
15899 	/*
15900 	 * Check if the usesrc client ILL passed in is not already
15901 	 * in use as a usesrc ILL i.e one whose source address is
15902 	 * in use OR a usesrc ILL is not already in use as a usesrc
15903 	 * client ILL
15904 	 */
15905 	if ((ucill->ill_usesrc_ifindex == 0) ||
15906 	    (uill->ill_usesrc_ifindex != 0)) {
15907 		return (-1);
15908 	}
15909 
15910 	ill = ill_prev_usesrc(ucill);
15911 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15912 
15913 	/* Remove from the current list */
15914 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15915 		/* Only two elements in the list */
15916 		ASSERT(ill->ill_usesrc_ifindex == 0);
15917 		ill->ill_usesrc_grp_next = NULL;
15918 	} else {
15919 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15920 	}
15921 
15922 	if (ifindex == 0) {
15923 		ucill->ill_usesrc_ifindex = 0;
15924 		ucill->ill_usesrc_grp_next = NULL;
15925 		return (0);
15926 	}
15927 
15928 	ucill->ill_usesrc_ifindex = ifindex;
15929 	tmp_ill = uill->ill_usesrc_grp_next;
15930 	uill->ill_usesrc_grp_next = ucill;
15931 	ucill->ill_usesrc_grp_next =
15932 	    (tmp_ill != NULL) ? tmp_ill : uill;
15933 	return (0);
15934 }
15935 
15936 /*
15937  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15938  * ip.c for locking details.
15939  */
15940 /* ARGSUSED */
15941 int
15942 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15943     ip_ioctl_cmd_t *ipip, void *ifreq)
15944 {
15945 	struct lifreq *lifr = (struct lifreq *)ifreq;
15946 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15947 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15948 	int err = 0, ret;
15949 	uint_t ifindex;
15950 	ipsq_t *ipsq = NULL;
15951 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15952 
15953 	ASSERT(IAM_WRITER_IPIF(ipif));
15954 	ASSERT(q->q_next == NULL);
15955 	ASSERT(CONN_Q(q));
15956 
15957 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15958 
15959 	ifindex = lifr->lifr_index;
15960 	if (ifindex == 0) {
15961 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15962 			/* non usesrc group interface, nothing to reset */
15963 			return (0);
15964 		}
15965 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15966 		/* valid reset request */
15967 		reset_flg = B_TRUE;
15968 	}
15969 
15970 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15971 	if (usesrc_ill == NULL) {
15972 		return (ENXIO);
15973 	}
15974 
15975 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15976 	    NEW_OP, B_TRUE);
15977 	if (ipsq == NULL) {
15978 		err = EINPROGRESS;
15979 		/* Operation enqueued on the ipsq of the usesrc ILL */
15980 		goto done;
15981 	}
15982 
15983 	/* USESRC isn't currently supported with IPMP */
15984 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15985 		err = ENOTSUP;
15986 		goto done;
15987 	}
15988 
15989 	/*
15990 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15991 	 * used by IPMP underlying interfaces, but someone might think it's
15992 	 * more general and try to use it independently with VNI.)
15993 	 */
15994 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15995 		err = ENOTSUP;
15996 		goto done;
15997 	}
15998 
15999 	/*
16000 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16001 	 * already a client then return EINVAL
16002 	 */
16003 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16004 		err = EINVAL;
16005 		goto done;
16006 	}
16007 
16008 	/*
16009 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16010 	 * be then this is a duplicate operation.
16011 	 */
16012 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16013 		err = 0;
16014 		goto done;
16015 	}
16016 
16017 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16018 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16019 	    usesrc_ill->ill_isv6));
16020 
16021 	/*
16022 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16023 	 * and the ill_usesrc_ifindex fields
16024 	 */
16025 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16026 
16027 	if (reset_flg) {
16028 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16029 		if (ret != 0) {
16030 			err = EINVAL;
16031 		}
16032 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16033 		goto done;
16034 	}
16035 
16036 	/*
16037 	 * Four possibilities to consider:
16038 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16039 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16040 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16041 	 * 4. Both are part of their respective usesrc groups
16042 	 */
16043 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16044 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16045 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16046 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16047 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16048 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16049 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16050 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16051 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16052 		/* Insert at head of list */
16053 		usesrc_cli_ill->ill_usesrc_grp_next =
16054 		    usesrc_ill->ill_usesrc_grp_next;
16055 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16056 	} else {
16057 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16058 		    ifindex);
16059 		if (ret != 0)
16060 			err = EINVAL;
16061 	}
16062 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16063 
16064 done:
16065 	if (ipsq != NULL)
16066 		ipsq_exit(ipsq);
16067 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16068 	ill_refrele(usesrc_ill);
16069 
16070 	/* Let conn_ixa caching know that source address selection changed */
16071 	ip_update_source_selection(ipst);
16072 
16073 	return (err);
16074 }
16075 
16076 /*
16077  * comparison function used by avl.
16078  */
16079 static int
16080 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16081 {
16082 
16083 	uint_t index;
16084 
16085 	ASSERT(phyip != NULL && index_ptr != NULL);
16086 
16087 	index = *((uint_t *)index_ptr);
16088 	/*
16089 	 * let the phyint with the lowest index be on top.
16090 	 */
16091 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16092 		return (1);
16093 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16094 		return (-1);
16095 	return (0);
16096 }
16097 
16098 /*
16099  * comparison function used by avl.
16100  */
16101 static int
16102 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16103 {
16104 	ill_t *ill;
16105 	int res = 0;
16106 
16107 	ASSERT(phyip != NULL && name_ptr != NULL);
16108 
16109 	if (((phyint_t *)phyip)->phyint_illv4)
16110 		ill = ((phyint_t *)phyip)->phyint_illv4;
16111 	else
16112 		ill = ((phyint_t *)phyip)->phyint_illv6;
16113 	ASSERT(ill != NULL);
16114 
16115 	res = strcmp(ill->ill_name, (char *)name_ptr);
16116 	if (res > 0)
16117 		return (1);
16118 	else if (res < 0)
16119 		return (-1);
16120 	return (0);
16121 }
16122 
16123 /*
16124  * This function is called on the unplumb path via ill_glist_delete() when
16125  * there are no ills left on the phyint and thus the phyint can be freed.
16126  */
16127 static void
16128 phyint_free(phyint_t *phyi)
16129 {
16130 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16131 
16132 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16133 
16134 	/*
16135 	 * If this phyint was an IPMP meta-interface, blow away the group.
16136 	 * This is safe to do because all of the illgrps have already been
16137 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16138 	 * If we're cleaning up as a result of failed initialization,
16139 	 * phyint_grp may be NULL.
16140 	 */
16141 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16142 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16143 		ipmp_grp_destroy(phyi->phyint_grp);
16144 		phyi->phyint_grp = NULL;
16145 		rw_exit(&ipst->ips_ipmp_lock);
16146 	}
16147 
16148 	/*
16149 	 * If this interface was under IPMP, take it out of the group.
16150 	 */
16151 	if (phyi->phyint_grp != NULL)
16152 		ipmp_phyint_leave_grp(phyi);
16153 
16154 	/*
16155 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16156 	 * will be freed in ipsq_exit().
16157 	 */
16158 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16159 	phyi->phyint_name[0] = '\0';
16160 
16161 	mi_free(phyi);
16162 }
16163 
16164 /*
16165  * Attach the ill to the phyint structure which can be shared by both
16166  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16167  * function is called from ipif_set_values and ill_lookup_on_name (for
16168  * loopback) where we know the name of the ill. We lookup the ill and if
16169  * there is one present already with the name use that phyint. Otherwise
16170  * reuse the one allocated by ill_init.
16171  */
16172 static void
16173 ill_phyint_reinit(ill_t *ill)
16174 {
16175 	boolean_t isv6 = ill->ill_isv6;
16176 	phyint_t *phyi_old;
16177 	phyint_t *phyi;
16178 	avl_index_t where = 0;
16179 	ill_t	*ill_other = NULL;
16180 	ip_stack_t	*ipst = ill->ill_ipst;
16181 
16182 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16183 
16184 	phyi_old = ill->ill_phyint;
16185 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16186 	    phyi_old->phyint_illv6 == NULL));
16187 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16188 	    phyi_old->phyint_illv4 == NULL));
16189 	ASSERT(phyi_old->phyint_ifindex == 0);
16190 
16191 	/*
16192 	 * Now that our ill has a name, set it in the phyint.
16193 	 */
16194 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16195 
16196 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16197 	    ill->ill_name, &where);
16198 
16199 	/*
16200 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16201 	 *    the global list of ills. So no other thread could have located
16202 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16203 	 * 2. Now locate the other protocol instance of this ill.
16204 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16205 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16206 	 *    of neither ill can change.
16207 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16208 	 *    other ill.
16209 	 * 5. Release all locks.
16210 	 */
16211 
16212 	/*
16213 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16214 	 * we are initializing IPv4.
16215 	 */
16216 	if (phyi != NULL) {
16217 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16218 		ASSERT(ill_other->ill_phyint != NULL);
16219 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16220 		    (!isv6 && ill_other->ill_isv6));
16221 		GRAB_ILL_LOCKS(ill, ill_other);
16222 		/*
16223 		 * We are potentially throwing away phyint_flags which
16224 		 * could be different from the one that we obtain from
16225 		 * ill_other->ill_phyint. But it is okay as we are assuming
16226 		 * that the state maintained within IP is correct.
16227 		 */
16228 		mutex_enter(&phyi->phyint_lock);
16229 		if (isv6) {
16230 			ASSERT(phyi->phyint_illv6 == NULL);
16231 			phyi->phyint_illv6 = ill;
16232 		} else {
16233 			ASSERT(phyi->phyint_illv4 == NULL);
16234 			phyi->phyint_illv4 = ill;
16235 		}
16236 
16237 		/*
16238 		 * Delete the old phyint and make its ipsq eligible
16239 		 * to be freed in ipsq_exit().
16240 		 */
16241 		phyi_old->phyint_illv4 = NULL;
16242 		phyi_old->phyint_illv6 = NULL;
16243 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16244 		phyi_old->phyint_name[0] = '\0';
16245 		mi_free(phyi_old);
16246 	} else {
16247 		mutex_enter(&ill->ill_lock);
16248 		/*
16249 		 * We don't need to acquire any lock, since
16250 		 * the ill is not yet visible globally  and we
16251 		 * have not yet released the ill_g_lock.
16252 		 */
16253 		phyi = phyi_old;
16254 		mutex_enter(&phyi->phyint_lock);
16255 		/* XXX We need a recovery strategy here. */
16256 		if (!phyint_assign_ifindex(phyi, ipst))
16257 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16258 
16259 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16260 		    (void *)phyi, where);
16261 
16262 		(void) avl_find(&ipst->ips_phyint_g_list->
16263 		    phyint_list_avl_by_index,
16264 		    &phyi->phyint_ifindex, &where);
16265 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16266 		    (void *)phyi, where);
16267 	}
16268 
16269 	/*
16270 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16271 	 * pending mp is not affected because that is per ill basis.
16272 	 */
16273 	ill->ill_phyint = phyi;
16274 
16275 	/*
16276 	 * Now that the phyint's ifindex has been assigned, complete the
16277 	 * remaining
16278 	 */
16279 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16280 	if (ill->ill_isv6) {
16281 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16282 		    ill->ill_phyint->phyint_ifindex;
16283 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16284 	} else {
16285 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16286 	}
16287 
16288 	/*
16289 	 * Generate an event within the hooks framework to indicate that
16290 	 * a new interface has just been added to IP.  For this event to
16291 	 * be generated, the network interface must, at least, have an
16292 	 * ifindex assigned to it.  (We don't generate the event for
16293 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16294 	 *
16295 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16296 	 * that the ordering of delivered events to listeners matches the
16297 	 * order of them in the kernel.
16298 	 */
16299 	if (!IS_LOOPBACK(ill)) {
16300 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16301 		    ill->ill_name_length);
16302 	}
16303 	RELEASE_ILL_LOCKS(ill, ill_other);
16304 	mutex_exit(&phyi->phyint_lock);
16305 }
16306 
16307 /*
16308  * Notify any downstream modules of the name of this interface.
16309  * An M_IOCTL is used even though we don't expect a successful reply.
16310  * Any reply message from the driver (presumably an M_IOCNAK) will
16311  * eventually get discarded somewhere upstream.  The message format is
16312  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16313  * to IP.
16314  */
16315 static void
16316 ip_ifname_notify(ill_t *ill, queue_t *q)
16317 {
16318 	mblk_t *mp1, *mp2;
16319 	struct iocblk *iocp;
16320 	struct lifreq *lifr;
16321 
16322 	mp1 = mkiocb(SIOCSLIFNAME);
16323 	if (mp1 == NULL)
16324 		return;
16325 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16326 	if (mp2 == NULL) {
16327 		freeb(mp1);
16328 		return;
16329 	}
16330 
16331 	mp1->b_cont = mp2;
16332 	iocp = (struct iocblk *)mp1->b_rptr;
16333 	iocp->ioc_count = sizeof (struct lifreq);
16334 
16335 	lifr = (struct lifreq *)mp2->b_rptr;
16336 	mp2->b_wptr += sizeof (struct lifreq);
16337 	bzero(lifr, sizeof (struct lifreq));
16338 
16339 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16340 	lifr->lifr_ppa = ill->ill_ppa;
16341 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16342 
16343 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16344 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16345 	putnext(q, mp1);
16346 }
16347 
16348 static int
16349 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16350 {
16351 	int		err;
16352 	ip_stack_t	*ipst = ill->ill_ipst;
16353 	phyint_t	*phyi = ill->ill_phyint;
16354 
16355 	/* Set the obsolete NDD per-interface forwarding name. */
16356 	err = ill_set_ndd_name(ill);
16357 	if (err != 0) {
16358 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16359 		    err);
16360 	}
16361 
16362 	/*
16363 	 * Now that ill_name is set, the configuration for the IPMP
16364 	 * meta-interface can be performed.
16365 	 */
16366 	if (IS_IPMP(ill)) {
16367 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16368 		/*
16369 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16370 		 * meta-interface and we need to create the IPMP group.
16371 		 */
16372 		if (phyi->phyint_grp == NULL) {
16373 			/*
16374 			 * If someone has renamed another IPMP group to have
16375 			 * the same name as our interface, bail.
16376 			 */
16377 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16378 				rw_exit(&ipst->ips_ipmp_lock);
16379 				return (EEXIST);
16380 			}
16381 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16382 			if (phyi->phyint_grp == NULL) {
16383 				rw_exit(&ipst->ips_ipmp_lock);
16384 				return (ENOMEM);
16385 			}
16386 		}
16387 		rw_exit(&ipst->ips_ipmp_lock);
16388 	}
16389 
16390 	/* Tell downstream modules where they are. */
16391 	ip_ifname_notify(ill, q);
16392 
16393 	/*
16394 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16395 	 * Error cases are ENOMEM ...
16396 	 */
16397 	err = ill_dl_phys(ill, ipif, mp, q);
16398 
16399 	if (ill->ill_isv6) {
16400 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16401 		if (ipst->ips_mld_slowtimeout_id == 0) {
16402 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16403 			    (void *)ipst,
16404 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16405 		}
16406 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16407 	} else {
16408 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16409 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16410 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16411 			    (void *)ipst,
16412 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16413 		}
16414 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16415 	}
16416 
16417 	return (err);
16418 }
16419 
16420 /*
16421  * Common routine for ppa and ifname setting. Should be called exclusive.
16422  *
16423  * Returns EINPROGRESS when mp has been consumed by queueing it on
16424  * ipx_pending_mp and the ioctl will complete in ip_rput.
16425  *
16426  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16427  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16428  * For SLIFNAME, we pass these values back to the userland.
16429  */
16430 static int
16431 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16432 {
16433 	ill_t	*ill;
16434 	ipif_t	*ipif;
16435 	ipsq_t	*ipsq;
16436 	char	*ppa_ptr;
16437 	char	*old_ptr;
16438 	char	old_char;
16439 	int	error;
16440 	ip_stack_t	*ipst;
16441 
16442 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16443 	ASSERT(q->q_next != NULL);
16444 	ASSERT(interf_name != NULL);
16445 
16446 	ill = (ill_t *)q->q_ptr;
16447 	ipst = ill->ill_ipst;
16448 
16449 	ASSERT(ill->ill_ipst != NULL);
16450 	ASSERT(ill->ill_name[0] == '\0');
16451 	ASSERT(IAM_WRITER_ILL(ill));
16452 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16453 	ASSERT(ill->ill_ppa == UINT_MAX);
16454 
16455 	ill->ill_defend_start = ill->ill_defend_count = 0;
16456 	/* The ppa is sent down by ifconfig or is chosen */
16457 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16458 		return (EINVAL);
16459 	}
16460 
16461 	/*
16462 	 * make sure ppa passed in is same as ppa in the name.
16463 	 * This check is not made when ppa == UINT_MAX in that case ppa
16464 	 * in the name could be anything. System will choose a ppa and
16465 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16466 	 */
16467 	if (*new_ppa_ptr != UINT_MAX) {
16468 		/* stoi changes the pointer */
16469 		old_ptr = ppa_ptr;
16470 		/*
16471 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16472 		 * (they don't have an externally visible ppa).  We assign one
16473 		 * here so that we can manage the interface.  Note that in
16474 		 * the past this value was always 0 for DLPI 1 drivers.
16475 		 */
16476 		if (*new_ppa_ptr == 0)
16477 			*new_ppa_ptr = stoi(&old_ptr);
16478 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16479 			return (EINVAL);
16480 	}
16481 	/*
16482 	 * terminate string before ppa
16483 	 * save char at that location.
16484 	 */
16485 	old_char = ppa_ptr[0];
16486 	ppa_ptr[0] = '\0';
16487 
16488 	ill->ill_ppa = *new_ppa_ptr;
16489 	/*
16490 	 * Finish as much work now as possible before calling ill_glist_insert
16491 	 * which makes the ill globally visible and also merges it with the
16492 	 * other protocol instance of this phyint. The remaining work is
16493 	 * done after entering the ipsq which may happen sometime later.
16494 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16495 	 */
16496 	ipif = ill->ill_ipif;
16497 
16498 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16499 	ipif_assign_seqid(ipif);
16500 
16501 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16502 		ill->ill_flags |= ILLF_IPV4;
16503 
16504 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16505 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16506 
16507 	if (ill->ill_flags & ILLF_IPV6) {
16508 
16509 		ill->ill_isv6 = B_TRUE;
16510 		ill_set_inputfn(ill);
16511 		if (ill->ill_rq != NULL) {
16512 			ill->ill_rq->q_qinfo = &iprinitv6;
16513 		}
16514 
16515 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16516 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16517 		ipif->ipif_v6subnet = ipv6_all_zeros;
16518 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16519 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16520 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16521 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16522 		/*
16523 		 * point-to-point or Non-mulicast capable
16524 		 * interfaces won't do NUD unless explicitly
16525 		 * configured to do so.
16526 		 */
16527 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16528 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16529 			ill->ill_flags |= ILLF_NONUD;
16530 		}
16531 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16532 		if (ill->ill_flags & ILLF_NOARP) {
16533 			/*
16534 			 * Note: xresolv interfaces will eventually need
16535 			 * NOARP set here as well, but that will require
16536 			 * those external resolvers to have some
16537 			 * knowledge of that flag and act appropriately.
16538 			 * Not to be changed at present.
16539 			 */
16540 			ill->ill_flags &= ~ILLF_NOARP;
16541 		}
16542 		/*
16543 		 * Set the ILLF_ROUTER flag according to the global
16544 		 * IPv6 forwarding policy.
16545 		 */
16546 		if (ipst->ips_ipv6_forward != 0)
16547 			ill->ill_flags |= ILLF_ROUTER;
16548 	} else if (ill->ill_flags & ILLF_IPV4) {
16549 		ill->ill_isv6 = B_FALSE;
16550 		ill_set_inputfn(ill);
16551 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16552 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16553 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16554 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16555 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16556 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16557 		/*
16558 		 * Set the ILLF_ROUTER flag according to the global
16559 		 * IPv4 forwarding policy.
16560 		 */
16561 		if (ipst->ips_ip_g_forward != 0)
16562 			ill->ill_flags |= ILLF_ROUTER;
16563 	}
16564 
16565 	ASSERT(ill->ill_phyint != NULL);
16566 
16567 	/*
16568 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16569 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16570 	 */
16571 	if (!ill_allocate_mibs(ill))
16572 		return (ENOMEM);
16573 
16574 	/*
16575 	 * Pick a default sap until we get the DL_INFO_ACK back from
16576 	 * the driver.
16577 	 */
16578 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16579 	    ill->ill_media->ip_m_ipv4sap;
16580 
16581 	ill->ill_ifname_pending = 1;
16582 	ill->ill_ifname_pending_err = 0;
16583 
16584 	/*
16585 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16586 	 * that were joined while this ill was not bound to the DLPI link need
16587 	 * to be recovered by ill_recover_multicast().
16588 	 */
16589 	ill->ill_need_recover_multicast = 1;
16590 
16591 	ill_refhold(ill);
16592 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16593 	if ((error = ill_glist_insert(ill, interf_name,
16594 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16595 		ill->ill_ppa = UINT_MAX;
16596 		ill->ill_name[0] = '\0';
16597 		/*
16598 		 * undo null termination done above.
16599 		 */
16600 		ppa_ptr[0] = old_char;
16601 		rw_exit(&ipst->ips_ill_g_lock);
16602 		ill_refrele(ill);
16603 		return (error);
16604 	}
16605 
16606 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16607 
16608 	/*
16609 	 * When we return the buffer pointed to by interf_name should contain
16610 	 * the same name as in ill_name.
16611 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16612 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16613 	 * so copy full name and update the ppa ptr.
16614 	 * When ppa passed in != UINT_MAX all values are correct just undo
16615 	 * null termination, this saves a bcopy.
16616 	 */
16617 	if (*new_ppa_ptr == UINT_MAX) {
16618 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16619 		*new_ppa_ptr = ill->ill_ppa;
16620 	} else {
16621 		/*
16622 		 * undo null termination done above.
16623 		 */
16624 		ppa_ptr[0] = old_char;
16625 	}
16626 
16627 	/* Let SCTP know about this ILL */
16628 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16629 
16630 	/*
16631 	 * ill_glist_insert has made the ill visible globally, and
16632 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16633 	 * we need to hold the ips_ill_g_lock across the call to enter the
16634 	 * ipsq to enforce atomicity and prevent reordering. In the event
16635 	 * the ipsq has changed, and if the new ipsq is currently busy,
16636 	 * we need to make sure that this half-completed ioctl is ahead of
16637 	 * any subsequent ioctl. We achieve this by not dropping the
16638 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16639 	 * ensuring that new ioctls can't start.
16640 	 */
16641 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16642 	    B_TRUE);
16643 
16644 	rw_exit(&ipst->ips_ill_g_lock);
16645 	ill_refrele(ill);
16646 	if (ipsq == NULL)
16647 		return (EINPROGRESS);
16648 
16649 	/*
16650 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16651 	 */
16652 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16653 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16654 	else
16655 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16656 
16657 	error = ipif_set_values_tail(ill, ipif, mp, q);
16658 	ipsq_exit(ipsq);
16659 	if (error != 0 && error != EINPROGRESS) {
16660 		/*
16661 		 * restore previous values
16662 		 */
16663 		ill->ill_isv6 = B_FALSE;
16664 		ill_set_inputfn(ill);
16665 	}
16666 	return (error);
16667 }
16668 
16669 void
16670 ipif_init(ip_stack_t *ipst)
16671 {
16672 	int i;
16673 
16674 	for (i = 0; i < MAX_G_HEADS; i++) {
16675 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16676 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16677 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16678 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16679 	}
16680 
16681 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16682 	    ill_phyint_compare_index,
16683 	    sizeof (phyint_t),
16684 	    offsetof(struct phyint, phyint_avl_by_index));
16685 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16686 	    ill_phyint_compare_name,
16687 	    sizeof (phyint_t),
16688 	    offsetof(struct phyint, phyint_avl_by_name));
16689 }
16690 
16691 /*
16692  * Save enough information so that we can recreate the IRE if
16693  * the interface goes down and then up.
16694  */
16695 void
16696 ill_save_ire(ill_t *ill, ire_t *ire)
16697 {
16698 	mblk_t	*save_mp;
16699 
16700 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16701 	if (save_mp != NULL) {
16702 		ifrt_t	*ifrt;
16703 
16704 		save_mp->b_wptr += sizeof (ifrt_t);
16705 		ifrt = (ifrt_t *)save_mp->b_rptr;
16706 		bzero(ifrt, sizeof (ifrt_t));
16707 		ifrt->ifrt_type = ire->ire_type;
16708 		if (ire->ire_ipversion == IPV4_VERSION) {
16709 			ASSERT(!ill->ill_isv6);
16710 			ifrt->ifrt_addr = ire->ire_addr;
16711 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16712 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16713 			ifrt->ifrt_mask = ire->ire_mask;
16714 		} else {
16715 			ASSERT(ill->ill_isv6);
16716 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16717 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16718 			mutex_enter(&ire->ire_lock);
16719 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16720 			mutex_exit(&ire->ire_lock);
16721 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16722 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16723 		}
16724 		ifrt->ifrt_flags = ire->ire_flags;
16725 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16726 		mutex_enter(&ill->ill_saved_ire_lock);
16727 		save_mp->b_cont = ill->ill_saved_ire_mp;
16728 		ill->ill_saved_ire_mp = save_mp;
16729 		ill->ill_saved_ire_cnt++;
16730 		mutex_exit(&ill->ill_saved_ire_lock);
16731 	}
16732 }
16733 
16734 /*
16735  * Remove one entry from ill_saved_ire_mp.
16736  */
16737 void
16738 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16739 {
16740 	mblk_t	**mpp;
16741 	mblk_t	*mp;
16742 	ifrt_t	*ifrt;
16743 
16744 	/* Remove from ill_saved_ire_mp list if it is there */
16745 	mutex_enter(&ill->ill_saved_ire_lock);
16746 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16747 	    mpp = &(*mpp)->b_cont) {
16748 		in6_addr_t	gw_addr_v6;
16749 
16750 		/*
16751 		 * On a given ill, the tuple of address, gateway, mask,
16752 		 * ire_type, and zoneid is unique for each saved IRE.
16753 		 */
16754 		mp = *mpp;
16755 		ifrt = (ifrt_t *)mp->b_rptr;
16756 		/* ire_gateway_addr_v6 can change - need lock */
16757 		mutex_enter(&ire->ire_lock);
16758 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16759 		mutex_exit(&ire->ire_lock);
16760 
16761 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16762 		    ifrt->ifrt_type != ire->ire_type)
16763 			continue;
16764 
16765 		if (ill->ill_isv6 ?
16766 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16767 		    &ire->ire_addr_v6) &&
16768 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16769 		    &gw_addr_v6) &&
16770 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16771 		    &ire->ire_mask_v6)) :
16772 		    (ifrt->ifrt_addr == ire->ire_addr &&
16773 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16774 		    ifrt->ifrt_mask == ire->ire_mask)) {
16775 			*mpp = mp->b_cont;
16776 			ill->ill_saved_ire_cnt--;
16777 			freeb(mp);
16778 			break;
16779 		}
16780 	}
16781 	mutex_exit(&ill->ill_saved_ire_lock);
16782 }
16783 
16784 /*
16785  * IP multirouting broadcast routes handling
16786  * Append CGTP broadcast IREs to regular ones created
16787  * at ifconfig time.
16788  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16789  * the destination and the gateway are broadcast addresses.
16790  * The caller has verified that the destination is an IRE_BROADCAST and that
16791  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16792  * we create a MULTIRT IRE_BROADCAST.
16793  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16794  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16795  */
16796 static void
16797 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16798 {
16799 	ire_t *ire_prim;
16800 
16801 	ASSERT(ire != NULL);
16802 
16803 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16804 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16805 	    NULL);
16806 	if (ire_prim != NULL) {
16807 		/*
16808 		 * We are in the special case of broadcasts for
16809 		 * CGTP. We add an IRE_BROADCAST that holds
16810 		 * the RTF_MULTIRT flag, the destination
16811 		 * address and the low level
16812 		 * info of ire_prim. In other words, CGTP
16813 		 * broadcast is added to the redundant ipif.
16814 		 */
16815 		ill_t *ill_prim;
16816 		ire_t  *bcast_ire;
16817 
16818 		ill_prim = ire_prim->ire_ill;
16819 
16820 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16821 		    (void *)ire_prim, (void *)ill_prim));
16822 
16823 		bcast_ire = ire_create(
16824 		    (uchar_t *)&ire->ire_addr,
16825 		    (uchar_t *)&ip_g_all_ones,
16826 		    (uchar_t *)&ire->ire_gateway_addr,
16827 		    IRE_BROADCAST,
16828 		    ill_prim,
16829 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16830 		    ire->ire_flags | RTF_KERNEL,
16831 		    NULL,
16832 		    ipst);
16833 
16834 		/*
16835 		 * Here we assume that ire_add does head insertion so that
16836 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16837 		 */
16838 		if (bcast_ire != NULL) {
16839 			if (ire->ire_flags & RTF_SETSRC) {
16840 				bcast_ire->ire_setsrc_addr =
16841 				    ire->ire_setsrc_addr;
16842 			}
16843 			bcast_ire = ire_add(bcast_ire);
16844 			if (bcast_ire != NULL) {
16845 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16846 				    "added bcast_ire %p\n",
16847 				    (void *)bcast_ire));
16848 
16849 				ill_save_ire(ill_prim, bcast_ire);
16850 				ire_refrele(bcast_ire);
16851 			}
16852 		}
16853 		ire_refrele(ire_prim);
16854 	}
16855 }
16856 
16857 /*
16858  * IP multirouting broadcast routes handling
16859  * Remove the broadcast ire.
16860  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16861  * the destination and the gateway are broadcast addresses.
16862  * The caller has only verified that RTF_MULTIRT was set. We check
16863  * that the destination is broadcast and that the gateway is a broadcast
16864  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16865  */
16866 static void
16867 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16868 {
16869 	ASSERT(ire != NULL);
16870 
16871 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16872 		ire_t *ire_prim;
16873 
16874 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16875 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16876 		    ipst, NULL);
16877 		if (ire_prim != NULL) {
16878 			ill_t *ill_prim;
16879 			ire_t  *bcast_ire;
16880 
16881 			ill_prim = ire_prim->ire_ill;
16882 
16883 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16884 			    "ire_prim %p, ill_prim %p\n",
16885 			    (void *)ire_prim, (void *)ill_prim));
16886 
16887 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16888 			    ire->ire_gateway_addr, IRE_BROADCAST,
16889 			    ill_prim, ALL_ZONES, NULL,
16890 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16891 			    MATCH_IRE_MASK, 0, ipst, NULL);
16892 
16893 			if (bcast_ire != NULL) {
16894 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16895 				    "looked up bcast_ire %p\n",
16896 				    (void *)bcast_ire));
16897 				ill_remove_saved_ire(bcast_ire->ire_ill,
16898 				    bcast_ire);
16899 				ire_delete(bcast_ire);
16900 				ire_refrele(bcast_ire);
16901 			}
16902 			ire_refrele(ire_prim);
16903 		}
16904 	}
16905 }
16906 
16907 /*
16908  * Derive an interface id from the link layer address.
16909  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16910  */
16911 static void
16912 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16913 {
16914 	char		*addr;
16915 
16916 	/*
16917 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16918 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16919 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16920 	 * interface ID on IPv6 interfaces above links that actually have real
16921 	 * Ethernet addresses.
16922 	 */
16923 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16924 		/* Form EUI-64 like address */
16925 		addr = (char *)&v6addr->s6_addr32[2];
16926 		bcopy(ill->ill_phys_addr, addr, 3);
16927 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16928 		addr[3] = (char)0xff;
16929 		addr[4] = (char)0xfe;
16930 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16931 	}
16932 }
16933 
16934 /* ARGSUSED */
16935 static void
16936 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16937 {
16938 }
16939 
16940 typedef struct ipmp_ifcookie {
16941 	uint32_t	ic_hostid;
16942 	char		ic_ifname[LIFNAMSIZ];
16943 	char		ic_zonename[ZONENAME_MAX];
16944 } ipmp_ifcookie_t;
16945 
16946 /*
16947  * Construct a pseudo-random interface ID for the IPMP interface that's both
16948  * predictable and (almost) guaranteed to be unique.
16949  */
16950 static void
16951 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16952 {
16953 	zone_t		*zp;
16954 	uint8_t		*addr;
16955 	uchar_t		hash[16];
16956 	ulong_t 	hostid;
16957 	MD5_CTX		ctx;
16958 	ipmp_ifcookie_t	ic = { 0 };
16959 
16960 	ASSERT(IS_IPMP(ill));
16961 
16962 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16963 	ic.ic_hostid = htonl((uint32_t)hostid);
16964 
16965 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16966 
16967 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16968 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16969 		zone_rele(zp);
16970 	}
16971 
16972 	MD5Init(&ctx);
16973 	MD5Update(&ctx, &ic, sizeof (ic));
16974 	MD5Final(hash, &ctx);
16975 
16976 	/*
16977 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16978 	 */
16979 	addr = &v6addr->s6_addr8[8];
16980 	bcopy(hash + 8, addr, sizeof (uint64_t));
16981 	addr[0] &= ~0x2;				/* set local bit */
16982 }
16983 
16984 /*
16985  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16986  */
16987 static void
16988 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16989 {
16990 	phyint_t *phyi = ill->ill_phyint;
16991 
16992 	/*
16993 	 * Check PHYI_MULTI_BCAST and length of physical
16994 	 * address to determine if we use the mapping or the
16995 	 * broadcast address.
16996 	 */
16997 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16998 	    ill->ill_phys_addr_length != ETHERADDRL) {
16999 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17000 		return;
17001 	}
17002 	m_physaddr[0] = 0x33;
17003 	m_physaddr[1] = 0x33;
17004 	m_physaddr[2] = m_ip6addr[12];
17005 	m_physaddr[3] = m_ip6addr[13];
17006 	m_physaddr[4] = m_ip6addr[14];
17007 	m_physaddr[5] = m_ip6addr[15];
17008 }
17009 
17010 /*
17011  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17012  */
17013 static void
17014 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17015 {
17016 	phyint_t *phyi = ill->ill_phyint;
17017 
17018 	/*
17019 	 * Check PHYI_MULTI_BCAST and length of physical
17020 	 * address to determine if we use the mapping or the
17021 	 * broadcast address.
17022 	 */
17023 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17024 	    ill->ill_phys_addr_length != ETHERADDRL) {
17025 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17026 		return;
17027 	}
17028 	m_physaddr[0] = 0x01;
17029 	m_physaddr[1] = 0x00;
17030 	m_physaddr[2] = 0x5e;
17031 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17032 	m_physaddr[4] = m_ipaddr[2];
17033 	m_physaddr[5] = m_ipaddr[3];
17034 }
17035 
17036 /* ARGSUSED */
17037 static void
17038 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17039 {
17040 	/*
17041 	 * for the MULTI_BCAST case and other cases when we want to
17042 	 * use the link-layer broadcast address for multicast.
17043 	 */
17044 	uint8_t	*bphys_addr;
17045 	dl_unitdata_req_t *dlur;
17046 
17047 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17048 	if (ill->ill_sap_length < 0) {
17049 		bphys_addr = (uchar_t *)dlur +
17050 		    dlur->dl_dest_addr_offset;
17051 	} else  {
17052 		bphys_addr = (uchar_t *)dlur +
17053 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17054 	}
17055 
17056 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17057 }
17058 
17059 /*
17060  * Derive IPoIB interface id from the link layer address.
17061  */
17062 static void
17063 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17064 {
17065 	char		*addr;
17066 
17067 	ASSERT(ill->ill_phys_addr_length == 20);
17068 	addr = (char *)&v6addr->s6_addr32[2];
17069 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17070 	/*
17071 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17072 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17073 	 * rules. In these cases, the IBA considers these GUIDs to be in
17074 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17075 	 * required; vendors are required not to assign global EUI-64's
17076 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17077 	 * of the interface identifier. Whether the GUID is in modified
17078 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17079 	 * bit set to 1.
17080 	 */
17081 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17082 }
17083 
17084 /*
17085  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17086  * Note on mapping from multicast IP addresses to IPoIB multicast link
17087  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17088  * The format of an IPoIB multicast address is:
17089  *
17090  *  4 byte QPN      Scope Sign.  Pkey
17091  * +--------------------------------------------+
17092  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17093  * +--------------------------------------------+
17094  *
17095  * The Scope and Pkey components are properties of the IBA port and
17096  * network interface. They can be ascertained from the broadcast address.
17097  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17098  */
17099 static void
17100 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17101 {
17102 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17103 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17104 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17105 	uint8_t	*bphys_addr;
17106 	dl_unitdata_req_t *dlur;
17107 
17108 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17109 
17110 	/*
17111 	 * RFC 4391: IPv4 MGID is 28-bit long.
17112 	 */
17113 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17114 	m_physaddr[17] = m_ipaddr[1];
17115 	m_physaddr[18] = m_ipaddr[2];
17116 	m_physaddr[19] = m_ipaddr[3];
17117 
17118 
17119 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17120 	if (ill->ill_sap_length < 0) {
17121 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17122 	} else  {
17123 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17124 		    ill->ill_sap_length;
17125 	}
17126 	/*
17127 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17128 	 */
17129 	m_physaddr[5] = bphys_addr[5];
17130 	m_physaddr[8] = bphys_addr[8];
17131 	m_physaddr[9] = bphys_addr[9];
17132 }
17133 
17134 static void
17135 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17136 {
17137 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17138 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17139 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17140 	uint8_t	*bphys_addr;
17141 	dl_unitdata_req_t *dlur;
17142 
17143 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17144 
17145 	/*
17146 	 * RFC 4391: IPv4 MGID is 80-bit long.
17147 	 */
17148 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17149 
17150 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17151 	if (ill->ill_sap_length < 0) {
17152 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17153 	} else  {
17154 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17155 		    ill->ill_sap_length;
17156 	}
17157 	/*
17158 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17159 	 */
17160 	m_physaddr[5] = bphys_addr[5];
17161 	m_physaddr[8] = bphys_addr[8];
17162 	m_physaddr[9] = bphys_addr[9];
17163 }
17164 
17165 /*
17166  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17167  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17168  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17169  * of RFC4213.
17170  */
17171 static void
17172 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17173 {
17174 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17175 	v6addr->s6_addr32[2] = 0;
17176 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17177 }
17178 
17179 /*
17180  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17181  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17182  * id.
17183  */
17184 static void
17185 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17186 {
17187 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17188 
17189 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17190 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17191 }
17192 
17193 static void
17194 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17195 {
17196 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17197 }
17198 
17199 static void
17200 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17201 {
17202 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17203 }
17204 
17205 static void
17206 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17207 {
17208 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17209 }
17210 
17211 static void
17212 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17213 {
17214 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17215 }
17216 
17217 /*
17218  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17219  * Returns an held ill, or NULL.
17220  */
17221 ill_t *
17222 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17223     ip_stack_t *ipst)
17224 {
17225 	ill_t	*ill;
17226 	ipif_t	*ipif;
17227 
17228 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17229 	if (ill == NULL)
17230 		return (NULL);
17231 
17232 	mutex_enter(&ill->ill_lock);
17233 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17234 		if (IPIF_IS_CONDEMNED(ipif))
17235 			continue;
17236 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17237 		    ipif->ipif_zoneid != ALL_ZONES)
17238 			continue;
17239 
17240 		mutex_exit(&ill->ill_lock);
17241 		return (ill);
17242 	}
17243 	mutex_exit(&ill->ill_lock);
17244 	ill_refrele(ill);
17245 	return (NULL);
17246 }
17247 
17248 /*
17249  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17250  * If a pointer to an ipif_t is returned then the caller will need to do
17251  * an ill_refrele().
17252  */
17253 ipif_t *
17254 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17255     ip_stack_t *ipst)
17256 {
17257 	ipif_t *ipif;
17258 	ill_t *ill;
17259 
17260 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17261 	if (ill == NULL)
17262 		return (NULL);
17263 
17264 	mutex_enter(&ill->ill_lock);
17265 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17266 		mutex_exit(&ill->ill_lock);
17267 		ill_refrele(ill);
17268 		return (NULL);
17269 	}
17270 
17271 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17272 		if (!IPIF_CAN_LOOKUP(ipif))
17273 			continue;
17274 		if (lifidx == ipif->ipif_id) {
17275 			ipif_refhold_locked(ipif);
17276 			break;
17277 		}
17278 	}
17279 
17280 	mutex_exit(&ill->ill_lock);
17281 	ill_refrele(ill);
17282 	return (ipif);
17283 }
17284 
17285 /*
17286  * Set ill_inputfn based on the current know state.
17287  * This needs to be called when any of the factors taken into
17288  * account changes.
17289  */
17290 void
17291 ill_set_inputfn(ill_t *ill)
17292 {
17293 	ip_stack_t	*ipst = ill->ill_ipst;
17294 
17295 	if (ill->ill_isv6) {
17296 		if (is_system_labeled())
17297 			ill->ill_inputfn = ill_input_full_v6;
17298 		else
17299 			ill->ill_inputfn = ill_input_short_v6;
17300 	} else {
17301 		if (is_system_labeled())
17302 			ill->ill_inputfn = ill_input_full_v4;
17303 		else if (ill->ill_dhcpinit != 0)
17304 			ill->ill_inputfn = ill_input_full_v4;
17305 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17306 		    != NULL)
17307 			ill->ill_inputfn = ill_input_full_v4;
17308 		else if (ipst->ips_ip_cgtp_filter &&
17309 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17310 			ill->ill_inputfn = ill_input_full_v4;
17311 		else
17312 			ill->ill_inputfn = ill_input_short_v4;
17313 	}
17314 }
17315 
17316 /*
17317  * Re-evaluate ill_inputfn for all the IPv4 ills.
17318  * Used when RSVP and CGTP comes and goes.
17319  */
17320 void
17321 ill_set_inputfn_all(ip_stack_t *ipst)
17322 {
17323 	ill_walk_context_t	ctx;
17324 	ill_t			*ill;
17325 
17326 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17327 	ill = ILL_START_WALK_V4(&ctx, ipst);
17328 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17329 		ill_set_inputfn(ill);
17330 
17331 	rw_exit(&ipst->ips_ill_g_lock);
17332 }
17333 
17334 /*
17335  * Set the physical address information for `ill' to the contents of the
17336  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17337  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17338  * EINPROGRESS will be returned.
17339  */
17340 int
17341 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17342 {
17343 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17344 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17345 
17346 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17347 
17348 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17349 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17350 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17351 		/* Changing DL_IPV6_TOKEN is not yet supported */
17352 		return (0);
17353 	}
17354 
17355 	/*
17356 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17357 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17358 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17359 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17360 	 */
17361 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17362 		freemsg(mp);
17363 		return (ENOMEM);
17364 	}
17365 
17366 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17367 	mutex_enter(&ill->ill_lock);
17368 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17369 	/* no more nce addition allowed */
17370 	mutex_exit(&ill->ill_lock);
17371 
17372 	/*
17373 	 * If we can quiesce the ill, then set the address.  If not, then
17374 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17375 	 */
17376 	ill_down_ipifs(ill, B_TRUE);
17377 	mutex_enter(&ill->ill_lock);
17378 	if (!ill_is_quiescent(ill)) {
17379 		/* call cannot fail since `conn_t *' argument is NULL */
17380 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17381 		    mp, ILL_DOWN);
17382 		mutex_exit(&ill->ill_lock);
17383 		return (EINPROGRESS);
17384 	}
17385 	mutex_exit(&ill->ill_lock);
17386 
17387 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17388 	return (0);
17389 }
17390 
17391 /*
17392  * Once the ill associated with `q' has quiesced, set its physical address
17393  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17394  * are passed (linked by b_cont), since we sometimes need to save two distinct
17395  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17396  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17397  * is quiesced, we know any stale nce's with the old address information have
17398  * already been removed, so we don't need to call nce_flush().
17399  */
17400 /* ARGSUSED */
17401 static void
17402 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17403 {
17404 	ill_t		*ill = q->q_ptr;
17405 	mblk_t		*addrmp2 = unlinkb(addrmp);
17406 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17407 	uint_t		addrlen, addroff;
17408 	int		status;
17409 
17410 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17411 
17412 	addroff	= dlindp->dl_addr_offset;
17413 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17414 
17415 	switch (dlindp->dl_data) {
17416 	case DL_IPV6_LINK_LAYER_ADDR:
17417 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17418 		freemsg(addrmp2);
17419 		break;
17420 
17421 	case DL_CURR_DEST_ADDR:
17422 		freemsg(ill->ill_dest_addr_mp);
17423 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17424 		ill->ill_dest_addr_mp = addrmp;
17425 		if (ill->ill_isv6) {
17426 			ill_setdesttoken(ill);
17427 			ipif_setdestlinklocal(ill->ill_ipif);
17428 		}
17429 		freemsg(addrmp2);
17430 		break;
17431 
17432 	case DL_CURR_PHYS_ADDR:
17433 		freemsg(ill->ill_phys_addr_mp);
17434 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17435 		ill->ill_phys_addr_mp = addrmp;
17436 		ill->ill_phys_addr_length = addrlen;
17437 		if (ill->ill_isv6)
17438 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17439 		else
17440 			freemsg(addrmp2);
17441 		if (ill->ill_isv6) {
17442 			ill_setdefaulttoken(ill);
17443 			ipif_setlinklocal(ill->ill_ipif);
17444 		}
17445 		break;
17446 	default:
17447 		ASSERT(0);
17448 	}
17449 
17450 	/*
17451 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17452 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17453 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17454 	 * brought up.
17455 	 */
17456 	status = ill_up_ipifs(ill, q, addrmp);
17457 	mutex_enter(&ill->ill_lock);
17458 	if (ill->ill_dl_up)
17459 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17460 	mutex_exit(&ill->ill_lock);
17461 	if (status != EINPROGRESS)
17462 		ipsq_current_finish(ipsq);
17463 }
17464 
17465 /*
17466  * Helper routine for setting the ill_nd_lla fields.
17467  */
17468 void
17469 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17470 {
17471 	freemsg(ill->ill_nd_lla_mp);
17472 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17473 	ill->ill_nd_lla_mp = ndmp;
17474 	ill->ill_nd_lla_len = addrlen;
17475 }
17476 
17477 /*
17478  * Replumb the ill.
17479  */
17480 int
17481 ill_replumb(ill_t *ill, mblk_t *mp)
17482 {
17483 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17484 
17485 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17486 
17487 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17488 
17489 	mutex_enter(&ill->ill_lock);
17490 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17491 	/* no more nce addition allowed */
17492 	mutex_exit(&ill->ill_lock);
17493 
17494 	/*
17495 	 * If we can quiesce the ill, then continue.  If not, then
17496 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17497 	 */
17498 	ill_down_ipifs(ill, B_FALSE);
17499 
17500 	mutex_enter(&ill->ill_lock);
17501 	if (!ill_is_quiescent(ill)) {
17502 		/* call cannot fail since `conn_t *' argument is NULL */
17503 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17504 		    mp, ILL_DOWN);
17505 		mutex_exit(&ill->ill_lock);
17506 		return (EINPROGRESS);
17507 	}
17508 	mutex_exit(&ill->ill_lock);
17509 
17510 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17511 	return (0);
17512 }
17513 
17514 /* ARGSUSED */
17515 static void
17516 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17517 {
17518 	ill_t *ill = q->q_ptr;
17519 	int err;
17520 	conn_t *connp = NULL;
17521 
17522 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17523 	freemsg(ill->ill_replumb_mp);
17524 	ill->ill_replumb_mp = copyb(mp);
17525 
17526 	if (ill->ill_replumb_mp == NULL) {
17527 		/* out of memory */
17528 		ipsq_current_finish(ipsq);
17529 		return;
17530 	}
17531 
17532 	mutex_enter(&ill->ill_lock);
17533 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17534 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17535 	mutex_exit(&ill->ill_lock);
17536 
17537 	if (!ill->ill_up_ipifs) {
17538 		/* already closing */
17539 		ipsq_current_finish(ipsq);
17540 		return;
17541 	}
17542 	ill->ill_replumbing = 1;
17543 	err = ill_down_ipifs_tail(ill);
17544 
17545 	/*
17546 	 * Successfully quiesced and brought down the interface, now we send
17547 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17548 	 * DL_NOTE_REPLUMB message.
17549 	 */
17550 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17551 	    DL_NOTIFY_CONF);
17552 	ASSERT(mp != NULL);
17553 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17554 	    DL_NOTE_REPLUMB_DONE;
17555 	ill_dlpi_send(ill, mp);
17556 
17557 	/*
17558 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17559 	 * streams have to be unbound. When all the DLPI exchanges are done,
17560 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17561 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17562 	 * arp_bringup_done().
17563 	 */
17564 	ASSERT(ill->ill_replumb_mp != NULL);
17565 	if (err == EINPROGRESS)
17566 		return;
17567 	else
17568 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17569 	ASSERT(connp == NULL);
17570 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17571 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17572 		return;
17573 	}
17574 	ipsq_current_finish(ipsq);
17575 }
17576 
17577 /*
17578  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17579  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17580  * as per the ioctl.  On failure, an errno is returned.
17581  */
17582 static int
17583 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17584 {
17585 	int rval;
17586 	struct strioctl iocb;
17587 
17588 	iocb.ic_cmd = cmd;
17589 	iocb.ic_timout = 15;
17590 	iocb.ic_len = bufsize;
17591 	iocb.ic_dp = buf;
17592 
17593 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17594 }
17595 
17596 /*
17597  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17598  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17599  */
17600 static int
17601 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17602     uint_t *bufsizep, cred_t *cr)
17603 {
17604 	int err;
17605 	struct lifnum lifn;
17606 
17607 	bzero(&lifn, sizeof (lifn));
17608 	lifn.lifn_family = af;
17609 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17610 
17611 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17612 		return (err);
17613 
17614 	/*
17615 	 * Pad the interface count to account for additional interfaces that
17616 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17617 	 */
17618 	lifn.lifn_count += 4;
17619 	bzero(lifcp, sizeof (*lifcp));
17620 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17621 	lifcp->lifc_family = af;
17622 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17623 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17624 
17625 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17626 	if (err != 0) {
17627 		kmem_free(lifcp->lifc_buf, *bufsizep);
17628 		return (err);
17629 	}
17630 
17631 	return (0);
17632 }
17633 
17634 /*
17635  * Helper for ip_interface_cleanup() that removes the loopback interface.
17636  */
17637 static void
17638 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17639 {
17640 	int err;
17641 	struct lifreq lifr;
17642 
17643 	bzero(&lifr, sizeof (lifr));
17644 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17645 
17646 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17647 	if (err != 0) {
17648 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17649 		    "error %d\n", isv6 ? "v6" : "v4", err));
17650 	}
17651 }
17652 
17653 /*
17654  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17655  * groups and that IPMP data addresses are down.  These conditions must be met
17656  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17657  */
17658 static void
17659 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17660 {
17661 	int af = isv6 ? AF_INET6 : AF_INET;
17662 	int i, nifs;
17663 	int err;
17664 	uint_t bufsize;
17665 	uint_t lifrsize = sizeof (struct lifreq);
17666 	struct lifconf lifc;
17667 	struct lifreq *lifrp;
17668 
17669 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17670 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17671 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17672 		return;
17673 	}
17674 
17675 	nifs = lifc.lifc_len / lifrsize;
17676 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17677 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17678 		if (err != 0) {
17679 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17680 			    "flags: error %d", lifrp->lifr_name, err);
17681 			continue;
17682 		}
17683 
17684 		if (lifrp->lifr_flags & IFF_IPMP) {
17685 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17686 				continue;
17687 
17688 			lifrp->lifr_flags &= ~IFF_UP;
17689 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17690 			if (err != 0) {
17691 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17692 				    "bring down (error %d); IPMP interface may "
17693 				    "not be shutdown", lifrp->lifr_name, err);
17694 			}
17695 
17696 			/*
17697 			 * Check if IFF_DUPLICATE is still set -- and if so,
17698 			 * reset the address to clear it.
17699 			 */
17700 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17701 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17702 				continue;
17703 
17704 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17705 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17706 			    lifrp, lifrsize, cr)) != 0) {
17707 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17708 				    "reset DAD (error %d); IPMP interface may "
17709 				    "not be shutdown", lifrp->lifr_name, err);
17710 			}
17711 			continue;
17712 		}
17713 
17714 		lifrp->lifr_groupname[0] = '\0';
17715 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17716 		if (err != 0) {
17717 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17718 			    "IPMP group (error %d); associated IPMP interface "
17719 			    "may not be shutdown", lifrp->lifr_name, err);
17720 			continue;
17721 		}
17722 	}
17723 
17724 	kmem_free(lifc.lifc_buf, bufsize);
17725 }
17726 
17727 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17728 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17729 
17730 /*
17731  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17732  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17733  * when the user-level processes in the zone are killed and the latter are
17734  * cleaned up by str_stack_shutdown().
17735  */
17736 void
17737 ip_interface_cleanup(ip_stack_t *ipst)
17738 {
17739 	ldi_handle_t	lh;
17740 	ldi_ident_t	li;
17741 	cred_t		*cr;
17742 	int		err;
17743 	int		i;
17744 	char		*devs[] = { UDP6DEV, UDPDEV };
17745 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17746 
17747 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17748 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17749 		    " error %d", err);
17750 		return;
17751 	}
17752 
17753 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17754 	ASSERT(cr != NULL);
17755 
17756 	/*
17757 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17758 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17759 	 * the loop.)
17760 	 */
17761 	for (i = 0; i < 2; i++) {
17762 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17763 		if (err != 0) {
17764 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17765 			    " error %d", devs[i], err);
17766 			continue;
17767 		}
17768 
17769 		ip_loopback_removeif(lh, i == 0, cr);
17770 		ip_ipmp_cleanup(lh, i == 0, cr);
17771 
17772 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17773 	}
17774 
17775 	ldi_ident_release(li);
17776 	crfree(cr);
17777 }
17778 
17779 /*
17780  * This needs to be in-sync with nic_event_t definition
17781  */
17782 static const char *
17783 ill_hook_event2str(nic_event_t event)
17784 {
17785 	switch (event) {
17786 	case NE_PLUMB:
17787 		return ("PLUMB");
17788 	case NE_UNPLUMB:
17789 		return ("UNPLUMB");
17790 	case NE_UP:
17791 		return ("UP");
17792 	case NE_DOWN:
17793 		return ("DOWN");
17794 	case NE_ADDRESS_CHANGE:
17795 		return ("ADDRESS_CHANGE");
17796 	case NE_LIF_UP:
17797 		return ("LIF_UP");
17798 	case NE_LIF_DOWN:
17799 		return ("LIF_DOWN");
17800 	case NE_IFINDEX_CHANGE:
17801 		return ("IFINDEX_CHANGE");
17802 	default:
17803 		return ("UNKNOWN");
17804 	}
17805 }
17806 
17807 void
17808 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17809     nic_event_data_t data, size_t datalen)
17810 {
17811 	ip_stack_t		*ipst = ill->ill_ipst;
17812 	hook_nic_event_int_t	*info;
17813 	const char		*str = NULL;
17814 
17815 	/* create a new nic event info */
17816 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17817 		goto fail;
17818 
17819 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17820 	info->hnei_event.hne_lif = lif;
17821 	info->hnei_event.hne_event = event;
17822 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17823 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17824 	info->hnei_event.hne_data = NULL;
17825 	info->hnei_event.hne_datalen = 0;
17826 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17827 
17828 	if (data != NULL && datalen != 0) {
17829 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17830 		if (info->hnei_event.hne_data == NULL)
17831 			goto fail;
17832 		bcopy(data, info->hnei_event.hne_data, datalen);
17833 		info->hnei_event.hne_datalen = datalen;
17834 	}
17835 
17836 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17837 	    DDI_NOSLEEP) == DDI_SUCCESS)
17838 		return;
17839 
17840 fail:
17841 	if (info != NULL) {
17842 		if (info->hnei_event.hne_data != NULL) {
17843 			kmem_free(info->hnei_event.hne_data,
17844 			    info->hnei_event.hne_datalen);
17845 		}
17846 		kmem_free(info, sizeof (hook_nic_event_t));
17847 	}
17848 	str = ill_hook_event2str(event);
17849 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17850 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17851 }
17852 
17853 static int
17854 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17855 {
17856 	int		err = 0;
17857 	const in_addr_t	*addr = NULL;
17858 	nce_t		*nce = NULL;
17859 	ill_t		*ill = ipif->ipif_ill;
17860 	ill_t		*bound_ill;
17861 	boolean_t	added_ipif = B_FALSE;
17862 	uint16_t	state;
17863 	uint16_t	flags;
17864 
17865 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17866 	    ill_t *, ill, ipif_t *, ipif);
17867 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17868 		addr = &ipif->ipif_lcl_addr;
17869 	}
17870 
17871 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17872 		if (res_act != Res_act_initial)
17873 			return (EINVAL);
17874 	}
17875 
17876 	if (addr != NULL) {
17877 		ipmp_illgrp_t	*illg = ill->ill_grp;
17878 
17879 		/* add unicast nce for the local addr */
17880 
17881 		if (IS_IPMP(ill)) {
17882 			/*
17883 			 * If we're here via ipif_up(), then the ipif
17884 			 * won't be bound yet -- add it to the group,
17885 			 * which will bind it if possible. (We would
17886 			 * add it in ipif_up(), but deleting on failure
17887 			 * there is gruesome.)  If we're here via
17888 			 * ipmp_ill_bind_ipif(), then the ipif has
17889 			 * already been added to the group and we
17890 			 * just need to use the binding.
17891 			 */
17892 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17893 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17894 				if (bound_ill == NULL) {
17895 					/*
17896 					 * We couldn't bind the ipif to an ill
17897 					 * yet, so we have nothing to publish.
17898 					 * Mark the address as ready and return.
17899 					 */
17900 					ipif->ipif_addr_ready = 1;
17901 					return (0);
17902 				}
17903 				added_ipif = B_TRUE;
17904 			}
17905 		} else {
17906 			bound_ill = ill;
17907 		}
17908 
17909 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17910 		    NCE_F_NONUD);
17911 		/*
17912 		 * If this is an initial bring-up (or the ipif was never
17913 		 * completely brought up), do DAD.  Otherwise, we're here
17914 		 * because IPMP has rebound an address to this ill: send
17915 		 * unsolicited advertisements (ARP announcements) to
17916 		 * inform others.
17917 		 */
17918 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17919 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17920 		} else {
17921 			state = ND_REACHABLE;
17922 			flags |= NCE_F_UNSOL_ADV;
17923 		}
17924 
17925 retry:
17926 		err = nce_lookup_then_add_v4(ill,
17927 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17928 		    addr, flags, state, &nce);
17929 
17930 		/*
17931 		 * note that we may encounter EEXIST if we are moving
17932 		 * the nce as a result of a rebind operation.
17933 		 */
17934 		switch (err) {
17935 		case 0:
17936 			ipif->ipif_added_nce = 1;
17937 			nce->nce_ipif_cnt++;
17938 			break;
17939 		case EEXIST:
17940 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17941 			    ill->ill_name));
17942 			if (!NCE_MYADDR(nce->nce_common)) {
17943 				/*
17944 				 * A leftover nce from before this address
17945 				 * existed
17946 				 */
17947 				ncec_delete(nce->nce_common);
17948 				nce_refrele(nce);
17949 				nce = NULL;
17950 				goto retry;
17951 			}
17952 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17953 				nce_refrele(nce);
17954 				nce = NULL;
17955 				ip1dbg(("ipif_arp_up: NCE already exists "
17956 				    "for %s:%u\n", ill->ill_name,
17957 				    ipif->ipif_id));
17958 				goto arp_up_done;
17959 			}
17960 			/*
17961 			 * Duplicate local addresses are permissible for
17962 			 * IPIF_POINTOPOINT interfaces which will get marked
17963 			 * IPIF_UNNUMBERED later in
17964 			 * ip_addr_availability_check().
17965 			 *
17966 			 * The nce_ipif_cnt field tracks the number of
17967 			 * ipifs that have nce_addr as their local address.
17968 			 */
17969 			ipif->ipif_addr_ready = 1;
17970 			ipif->ipif_added_nce = 1;
17971 			nce->nce_ipif_cnt++;
17972 			err = 0;
17973 			break;
17974 		default:
17975 			ASSERT(nce == NULL);
17976 			goto arp_up_done;
17977 		}
17978 		if (arp_no_defense) {
17979 			if ((ipif->ipif_flags & IPIF_UP) &&
17980 			    !ipif->ipif_addr_ready)
17981 				ipif_up_notify(ipif);
17982 			ipif->ipif_addr_ready = 1;
17983 		}
17984 	} else {
17985 		/* zero address. nothing to publish */
17986 		ipif->ipif_addr_ready = 1;
17987 	}
17988 	if (nce != NULL)
17989 		nce_refrele(nce);
17990 arp_up_done:
17991 	if (added_ipif && err != 0)
17992 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17993 	return (err);
17994 }
17995 
17996 int
17997 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17998 {
17999 	int 		err = 0;
18000 	ill_t 		*ill = ipif->ipif_ill;
18001 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18002 
18003 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18004 	    ill_t *, ill, ipif_t *, ipif);
18005 
18006 	/*
18007 	 * need to bring up ARP or setup mcast mapping only
18008 	 * when the first interface is coming UP.
18009 	 */
18010 	first_interface = (ill->ill_ipif_up_count == 0 &&
18011 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18012 
18013 	if (res_act == Res_act_initial && first_interface) {
18014 		/*
18015 		 * Send ATTACH + BIND
18016 		 */
18017 		err = arp_ll_up(ill);
18018 		if (err != EINPROGRESS && err != 0)
18019 			return (err);
18020 
18021 		/*
18022 		 * Add NCE for local address. Start DAD.
18023 		 * we'll wait to hear that DAD has finished
18024 		 * before using the interface.
18025 		 */
18026 		if (err == EINPROGRESS)
18027 			wait_for_dlpi = B_TRUE;
18028 	}
18029 
18030 	if (!wait_for_dlpi)
18031 		(void) ipif_arp_up_done_tail(ipif, res_act);
18032 
18033 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18034 }
18035 
18036 /*
18037  * Finish processing of "arp_up" after all the DLPI message
18038  * exchanges have completed between arp and the driver.
18039  */
18040 void
18041 arp_bringup_done(ill_t *ill, int err)
18042 {
18043 	mblk_t	*mp1;
18044 	ipif_t  *ipif;
18045 	conn_t *connp = NULL;
18046 	ipsq_t	*ipsq;
18047 	queue_t *q;
18048 
18049 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18050 
18051 	ASSERT(IAM_WRITER_ILL(ill));
18052 
18053 	ipsq = ill->ill_phyint->phyint_ipsq;
18054 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18055 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18056 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18057 	if (mp1 == NULL) /* bringup was aborted by the user */
18058 		return;
18059 
18060 	/*
18061 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18062 	 * must have an associated conn_t.  Otherwise, we're bringing this
18063 	 * interface back up as part of handling an asynchronous event (e.g.,
18064 	 * physical address change).
18065 	 */
18066 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18067 		ASSERT(connp != NULL);
18068 		q = CONNP_TO_WQ(connp);
18069 	} else {
18070 		ASSERT(connp == NULL);
18071 		q = ill->ill_rq;
18072 	}
18073 	if (err == 0) {
18074 		if (ipif->ipif_isv6) {
18075 			if ((err = ipif_up_done_v6(ipif)) != 0)
18076 				ip0dbg(("arp_bringup_done: init failed\n"));
18077 		} else {
18078 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18079 			if (err != 0 ||
18080 			    (err = ipif_up_done(ipif)) != 0) {
18081 				ip0dbg(("arp_bringup_done: "
18082 				    "init failed err %x\n", err));
18083 				(void) ipif_arp_down(ipif);
18084 			}
18085 
18086 		}
18087 	} else {
18088 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18089 	}
18090 
18091 	if ((err == 0) && (ill->ill_up_ipifs)) {
18092 		err = ill_up_ipifs(ill, q, mp1);
18093 		if (err == EINPROGRESS)
18094 			return;
18095 	}
18096 
18097 	/*
18098 	 * If we have a moved ipif to bring up, and everything has succeeded
18099 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18100 	 * down -- the admin can try to bring it up by hand if need be.
18101 	 */
18102 	if (ill->ill_move_ipif != NULL) {
18103 		ipif = ill->ill_move_ipif;
18104 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18105 		    ipif->ipif_ill->ill_name));
18106 		ill->ill_move_ipif = NULL;
18107 		if (err == 0) {
18108 			err = ipif_up(ipif, q, mp1);
18109 			if (err == EINPROGRESS)
18110 				return;
18111 		}
18112 	}
18113 
18114 	/*
18115 	 * The operation must complete without EINPROGRESS since
18116 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18117 	 * Otherwise, the operation will be stuck forever in the ipsq.
18118 	 */
18119 	ASSERT(err != EINPROGRESS);
18120 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18121 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18122 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18123 		    ill_t *, ill, ipif_t *, ipif);
18124 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18125 	} else {
18126 		ipsq_current_finish(ipsq);
18127 	}
18128 }
18129 
18130 /*
18131  * Finish processing of arp replumb after all the DLPI message
18132  * exchanges have completed between arp and the driver.
18133  */
18134 void
18135 arp_replumb_done(ill_t *ill, int err)
18136 {
18137 	mblk_t	*mp1;
18138 	ipif_t  *ipif;
18139 	conn_t *connp = NULL;
18140 	ipsq_t	*ipsq;
18141 	queue_t *q;
18142 
18143 	ASSERT(IAM_WRITER_ILL(ill));
18144 
18145 	ipsq = ill->ill_phyint->phyint_ipsq;
18146 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18147 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18148 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18149 	if (mp1 == NULL) {
18150 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18151 		    ipsq->ipsq_xop->ipx_current_ioctl));
18152 		/* bringup was aborted by the user */
18153 		return;
18154 	}
18155 	/*
18156 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18157 	 * must have an associated conn_t.  Otherwise, we're bringing this
18158 	 * interface back up as part of handling an asynchronous event (e.g.,
18159 	 * physical address change).
18160 	 */
18161 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18162 		ASSERT(connp != NULL);
18163 		q = CONNP_TO_WQ(connp);
18164 	} else {
18165 		ASSERT(connp == NULL);
18166 		q = ill->ill_rq;
18167 	}
18168 	if ((err == 0) && (ill->ill_up_ipifs)) {
18169 		err = ill_up_ipifs(ill, q, mp1);
18170 		if (err == EINPROGRESS)
18171 			return;
18172 	}
18173 	/*
18174 	 * The operation must complete without EINPROGRESS since
18175 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18176 	 * Otherwise, the operation will be stuck forever in the ipsq.
18177 	 */
18178 	ASSERT(err != EINPROGRESS);
18179 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18180 		DTRACE_PROBE4(ipif__ioctl, char *,
18181 		    "arp_replumb_done finish",
18182 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18183 		    ill_t *, ill, ipif_t *, ipif);
18184 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18185 	} else {
18186 		ipsq_current_finish(ipsq);
18187 	}
18188 }
18189 
18190 void
18191 ipif_up_notify(ipif_t *ipif)
18192 {
18193 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18194 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18195 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18196 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18197 	    NE_LIF_UP, NULL, 0);
18198 }
18199 
18200 /*
18201  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18202  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18203  * TPI end points with STREAMS modules pushed above.  This is assured by not
18204  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18205  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18206  * while unwinding from the ispq and that could be a thread from the bottom.
18207  */
18208 /* ARGSUSED */
18209 int
18210 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18211     ip_ioctl_cmd_t *ipip, void *arg)
18212 {
18213 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18214 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18215 	int ret = 0;
18216 	int i;
18217 	size_t size;
18218 	ip_stack_t *ipst;
18219 	zoneid_t zoneid;
18220 	ilb_stack_t *ilbs;
18221 
18222 	ipst = CONNQ_TO_IPST(q);
18223 	ilbs = ipst->ips_netstack->netstack_ilb;
18224 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18225 
18226 	switch (command) {
18227 	case ILB_CREATE_RULE: {
18228 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18229 
18230 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18231 			ret = EINVAL;
18232 			break;
18233 		}
18234 
18235 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18236 		break;
18237 	}
18238 	case ILB_DESTROY_RULE:
18239 	case ILB_ENABLE_RULE:
18240 	case ILB_DISABLE_RULE: {
18241 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18242 
18243 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18244 			ret = EINVAL;
18245 			break;
18246 		}
18247 
18248 		if (cmd->flags & ILB_RULE_ALLRULES) {
18249 			if (command == ILB_DESTROY_RULE) {
18250 				ilb_rule_del_all(ilbs, zoneid);
18251 				break;
18252 			} else if (command == ILB_ENABLE_RULE) {
18253 				ilb_rule_enable_all(ilbs, zoneid);
18254 				break;
18255 			} else if (command == ILB_DISABLE_RULE) {
18256 				ilb_rule_disable_all(ilbs, zoneid);
18257 				break;
18258 			}
18259 		} else {
18260 			if (command == ILB_DESTROY_RULE) {
18261 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18262 			} else if (command == ILB_ENABLE_RULE) {
18263 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18264 				    NULL);
18265 			} else if (command == ILB_DISABLE_RULE) {
18266 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18267 				    NULL);
18268 			}
18269 		}
18270 		break;
18271 	}
18272 	case ILB_NUM_RULES: {
18273 		ilb_num_rules_cmd_t *cmd;
18274 
18275 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18276 			ret = EINVAL;
18277 			break;
18278 		}
18279 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18280 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18281 		break;
18282 	}
18283 	case ILB_RULE_NAMES: {
18284 		ilb_rule_names_cmd_t *cmd;
18285 
18286 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18287 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18288 		    cmd->num_names == 0) {
18289 			ret = EINVAL;
18290 			break;
18291 		}
18292 		size = cmd->num_names * ILB_RULE_NAMESZ;
18293 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18294 		    size != cmd_mp->b_wptr) {
18295 			ret = EINVAL;
18296 			break;
18297 		}
18298 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18299 		break;
18300 	}
18301 	case ILB_NUM_SERVERS: {
18302 		ilb_num_servers_cmd_t *cmd;
18303 
18304 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18305 			ret = EINVAL;
18306 			break;
18307 		}
18308 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18309 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18310 		    &(cmd->num));
18311 		break;
18312 	}
18313 	case ILB_LIST_RULE: {
18314 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18315 
18316 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18317 			ret = EINVAL;
18318 			break;
18319 		}
18320 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18321 		break;
18322 	}
18323 	case ILB_LIST_SERVERS: {
18324 		ilb_servers_info_cmd_t *cmd;
18325 
18326 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18327 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18328 		    cmd->num_servers == 0) {
18329 			ret = EINVAL;
18330 			break;
18331 		}
18332 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18333 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18334 		    size != cmd_mp->b_wptr) {
18335 			ret = EINVAL;
18336 			break;
18337 		}
18338 
18339 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18340 		    &cmd->num_servers);
18341 		break;
18342 	}
18343 	case ILB_ADD_SERVERS: {
18344 		ilb_servers_info_cmd_t *cmd;
18345 		ilb_rule_t *rule;
18346 
18347 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18348 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18349 			ret = EINVAL;
18350 			break;
18351 		}
18352 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18353 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18354 		    size != cmd_mp->b_wptr) {
18355 			ret = EINVAL;
18356 			break;
18357 		}
18358 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18359 		if (rule == NULL) {
18360 			ASSERT(ret != 0);
18361 			break;
18362 		}
18363 		for (i = 0; i < cmd->num_servers; i++) {
18364 			ilb_server_info_t *s;
18365 
18366 			s = &cmd->servers[i];
18367 			s->err = ilb_server_add(ilbs, rule, s);
18368 		}
18369 		ILB_RULE_REFRELE(rule);
18370 		break;
18371 	}
18372 	case ILB_DEL_SERVERS:
18373 	case ILB_ENABLE_SERVERS:
18374 	case ILB_DISABLE_SERVERS: {
18375 		ilb_servers_cmd_t *cmd;
18376 		ilb_rule_t *rule;
18377 		int (*f)();
18378 
18379 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18380 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18381 			ret = EINVAL;
18382 			break;
18383 		}
18384 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18385 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18386 		    size != cmd_mp->b_wptr) {
18387 			ret = EINVAL;
18388 			break;
18389 		}
18390 
18391 		if (command == ILB_DEL_SERVERS)
18392 			f = ilb_server_del;
18393 		else if (command == ILB_ENABLE_SERVERS)
18394 			f = ilb_server_enable;
18395 		else if (command == ILB_DISABLE_SERVERS)
18396 			f = ilb_server_disable;
18397 
18398 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18399 		if (rule == NULL) {
18400 			ASSERT(ret != 0);
18401 			break;
18402 		}
18403 
18404 		for (i = 0; i < cmd->num_servers; i++) {
18405 			ilb_server_arg_t *s;
18406 
18407 			s = &cmd->servers[i];
18408 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18409 		}
18410 		ILB_RULE_REFRELE(rule);
18411 		break;
18412 	}
18413 	case ILB_LIST_NAT_TABLE: {
18414 		ilb_list_nat_cmd_t *cmd;
18415 
18416 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18417 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18418 			ret = EINVAL;
18419 			break;
18420 		}
18421 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18422 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18423 		    size != cmd_mp->b_wptr) {
18424 			ret = EINVAL;
18425 			break;
18426 		}
18427 
18428 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18429 		    &cmd->flags);
18430 		break;
18431 	}
18432 	case ILB_LIST_STICKY_TABLE: {
18433 		ilb_list_sticky_cmd_t *cmd;
18434 
18435 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18436 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18437 			ret = EINVAL;
18438 			break;
18439 		}
18440 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18441 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18442 		    size != cmd_mp->b_wptr) {
18443 			ret = EINVAL;
18444 			break;
18445 		}
18446 
18447 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18448 		    &cmd->num_sticky, &cmd->flags);
18449 		break;
18450 	}
18451 	default:
18452 		ret = EINVAL;
18453 		break;
18454 	}
18455 done:
18456 	return (ret);
18457 }
18458 
18459 /* Remove all cache entries for this logical interface */
18460 void
18461 ipif_nce_down(ipif_t *ipif)
18462 {
18463 	ill_t *ill = ipif->ipif_ill;
18464 	nce_t *nce;
18465 
18466 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18467 	    ill_t *, ill, ipif_t *, ipif);
18468 	if (ipif->ipif_added_nce) {
18469 		if (ipif->ipif_isv6)
18470 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18471 		else
18472 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18473 		if (nce != NULL) {
18474 			if (--nce->nce_ipif_cnt == 0)
18475 				ncec_delete(nce->nce_common);
18476 			ipif->ipif_added_nce = 0;
18477 			nce_refrele(nce);
18478 		} else {
18479 			/*
18480 			 * nce may already be NULL because it was already
18481 			 * flushed, e.g., due to a call to nce_flush
18482 			 */
18483 			ipif->ipif_added_nce = 0;
18484 		}
18485 	}
18486 	/*
18487 	 * Make IPMP aware of the deleted data address.
18488 	 */
18489 	if (IS_IPMP(ill))
18490 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18491 
18492 	/*
18493 	 * Remove all other nces dependent on this ill when the last ipif
18494 	 * is going away.
18495 	 */
18496 	if (ill->ill_ipif_up_count == 0) {
18497 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18498 		    (uchar_t *)ill, ill->ill_ipst);
18499 		if (IS_UNDER_IPMP(ill))
18500 			nce_flush(ill, B_TRUE);
18501 	}
18502 }
18503 
18504 /*
18505  * find the first interface that uses usill for its source address.
18506  */
18507 ill_t *
18508 ill_lookup_usesrc(ill_t *usill)
18509 {
18510 	ip_stack_t *ipst = usill->ill_ipst;
18511 	ill_t *ill;
18512 
18513 	ASSERT(usill != NULL);
18514 
18515 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18516 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18517 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18518 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18519 	    ill = ill->ill_usesrc_grp_next) {
18520 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18521 		    !ILL_IS_CONDEMNED(ill)) {
18522 			ill_refhold(ill);
18523 			break;
18524 		}
18525 	}
18526 	rw_exit(&ipst->ips_ill_g_lock);
18527 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18528 	return (ill);
18529 }
18530