xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision c39526b769298791ff5b0b6c5e761f49aabaeb4e)
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 (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 1990 Mentat Inc.
24  */
25 
26 /*
27  * This file contains the interface control functions for IP.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/dlpi.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/sysmacros.h>
36 #include <sys/strsubr.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/ctype.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.h>
68 #include <sys/md5.h>
69 
70 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
71 #include <inet/mi.h>
72 #include <inet/nd.h>
73 #include <inet/tunables.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 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
105 #include <inet/udp_impl.h> /* needed for udp_stack_t */
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
121 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
122 		    char *value, caddr_t cp, cred_t *ioc_cr);
123 
124 static boolean_t ill_is_quiescent(ill_t *);
125 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
126 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
127 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
128     mblk_t *mp, boolean_t need_up);
129 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
130     mblk_t *mp, boolean_t need_up);
131 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
132     queue_t *q, mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
134     mblk_t *mp);
135 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp);
137 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
138     queue_t *q, mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
140     int ioccmd, struct linkblk *li);
141 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
142 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
143 static void	ipsq_flush(ill_t *ill);
144 
145 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static void	ipsq_delete(ipsq_t *);
148 
149 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
150     boolean_t initialize, boolean_t insert, int *errorp);
151 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
152 static void	ipif_delete_bcast_ires(ipif_t *ipif);
153 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
154 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
155 		    boolean_t isv6);
156 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
157 static void	ipif_free(ipif_t *ipif);
158 static void	ipif_free_tail(ipif_t *ipif);
159 static void	ipif_set_default(ipif_t *ipif);
160 static int	ipif_set_values(queue_t *q, mblk_t *mp,
161     char *interf_name, uint_t *ppa);
162 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
163     queue_t *q);
164 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
165     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
166     ip_stack_t *);
167 
168 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
169 static void	ill_delete_interface_type(ill_if_t *);
170 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
171 static void	ill_dl_down(ill_t *ill);
172 static void	ill_down(ill_t *ill);
173 static void	ill_down_ipifs(ill_t *, boolean_t);
174 static void	ill_free_mib(ill_t *ill);
175 static void	ill_glist_delete(ill_t *);
176 static void	ill_phyint_reinit(ill_t *ill);
177 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
178 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
179 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
180 
181 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
182 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
183 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
184 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
185 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
186 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
187 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
188 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
189 static ip_v4mapinfo_func_t ip_mbcast_mapping;
190 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
191 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
192 static void	phyint_free(phyint_t *);
193 
194 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
196 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
197 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
198 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
199 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
200     dl_capability_sub_t *);
201 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
202 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
203 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
204 		    dl_capability_sub_t *);
205 static void	ill_capability_dld_enable(ill_t *);
206 static void	ill_capability_ack_thr(void *);
207 static void	ill_capability_lso_enable(ill_t *);
208 
209 static ill_t	*ill_prev_usesrc(ill_t *);
210 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
211 static void	ill_disband_usesrc_group(ill_t *);
212 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
213 
214 #ifdef DEBUG
215 static	void	ill_trace_cleanup(const ill_t *);
216 static	void	ipif_trace_cleanup(const ipif_t *);
217 #endif
218 
219 static	void	ill_dlpi_clear_deferred(ill_t *ill);
220 
221 /*
222  * if we go over the memory footprint limit more than once in this msec
223  * interval, we'll start pruning aggressively.
224  */
225 int ip_min_frag_prune_time = 0;
226 
227 static ipft_t	ip_ioctl_ftbl[] = {
228 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
229 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
230 		IPFT_F_NO_REPLY },
231 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
232 	{ 0 }
233 };
234 
235 /* Simple ICMP IP Header Template */
236 static ipha_t icmp_ipha = {
237 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
238 };
239 
240 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
241 
242 static ip_m_t   ip_m_tbl[] = {
243 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
260 	    ip_nodef_v6intfid },
261 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
263 	    ip_ipv4_v6destintfid },
264 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
266 	    ip_ipv6_v6destintfid },
267 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
268 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
269 	    ip_nodef_v6intfid },
270 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
271 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
272 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
273 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
274 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
275 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
276 	    ip_nodef_v6intfid }
277 };
278 
279 static ill_t	ill_null;		/* Empty ILL for init. */
280 char	ipif_loopback_name[] = "lo0";
281 
282 /* These are used by all IP network modules. */
283 sin6_t	sin6_null;	/* Zero address for quick clears */
284 sin_t	sin_null;	/* Zero address for quick clears */
285 
286 /* When set search for unused ipif_seqid */
287 static ipif_t	ipif_zero;
288 
289 /*
290  * ppa arena is created after these many
291  * interfaces have been plumbed.
292  */
293 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
294 
295 /*
296  * Allocate per-interface mibs.
297  * Returns true if ok. False otherwise.
298  *  ipsq  may not yet be allocated (loopback case ).
299  */
300 static boolean_t
301 ill_allocate_mibs(ill_t *ill)
302 {
303 	/* Already allocated? */
304 	if (ill->ill_ip_mib != NULL) {
305 		if (ill->ill_isv6)
306 			ASSERT(ill->ill_icmp6_mib != NULL);
307 		return (B_TRUE);
308 	}
309 
310 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
311 	    KM_NOSLEEP);
312 	if (ill->ill_ip_mib == NULL) {
313 		return (B_FALSE);
314 	}
315 
316 	/* Setup static information */
317 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
318 	    sizeof (mib2_ipIfStatsEntry_t));
319 	if (ill->ill_isv6) {
320 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
321 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
322 		    sizeof (mib2_ipv6AddrEntry_t));
323 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
324 		    sizeof (mib2_ipv6RouteEntry_t));
325 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
326 		    sizeof (mib2_ipv6NetToMediaEntry_t));
327 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
328 		    sizeof (ipv6_member_t));
329 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
330 		    sizeof (ipv6_grpsrc_t));
331 	} else {
332 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
333 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
334 		    sizeof (mib2_ipAddrEntry_t));
335 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
336 		    sizeof (mib2_ipRouteEntry_t));
337 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
338 		    sizeof (mib2_ipNetToMediaEntry_t));
339 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
340 		    sizeof (ip_member_t));
341 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
342 		    sizeof (ip_grpsrc_t));
343 
344 		/*
345 		 * For a v4 ill, we are done at this point, because per ill
346 		 * icmp mibs are only used for v6.
347 		 */
348 		return (B_TRUE);
349 	}
350 
351 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
352 	    KM_NOSLEEP);
353 	if (ill->ill_icmp6_mib == NULL) {
354 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
355 		ill->ill_ip_mib = NULL;
356 		return (B_FALSE);
357 	}
358 	/* static icmp info */
359 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
360 	    sizeof (mib2_ipv6IfIcmpEntry_t);
361 	/*
362 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
363 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
364 	 * -> ill_phyint_reinit
365 	 */
366 	return (B_TRUE);
367 }
368 
369 /*
370  * Completely vaporize a lower level tap and all associated interfaces.
371  * ill_delete is called only out of ip_close when the device control
372  * stream is being closed.
373  */
374 void
375 ill_delete(ill_t *ill)
376 {
377 	ipif_t	*ipif;
378 	ill_t	*prev_ill;
379 	ip_stack_t	*ipst = ill->ill_ipst;
380 
381 	/*
382 	 * ill_delete may be forcibly entering the ipsq. The previous
383 	 * ioctl may not have completed and may need to be aborted.
384 	 * ipsq_flush takes care of it. If we don't need to enter the
385 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
386 	 * ill_delete_tail is sufficient.
387 	 */
388 	ipsq_flush(ill);
389 
390 	/*
391 	 * Nuke all interfaces.  ipif_free will take down the interface,
392 	 * remove it from the list, and free the data structure.
393 	 * Walk down the ipif list and remove the logical interfaces
394 	 * first before removing the main ipif. We can't unplumb
395 	 * zeroth interface first in the case of IPv6 as update_conn_ill
396 	 * -> ip_ll_multireq de-references ill_ipif for checking
397 	 * POINTOPOINT.
398 	 *
399 	 * If ill_ipif was not properly initialized (i.e low on memory),
400 	 * then no interfaces to clean up. In this case just clean up the
401 	 * ill.
402 	 */
403 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
404 		ipif_free(ipif);
405 
406 	/*
407 	 * clean out all the nce_t entries that depend on this
408 	 * ill for the ill_phys_addr.
409 	 */
410 	nce_flush(ill, B_TRUE);
411 
412 	/* Clean up msgs on pending upcalls for mrouted */
413 	reset_mrt_ill(ill);
414 
415 	update_conn_ill(ill, ipst);
416 
417 	/*
418 	 * Remove multicast references added as a result of calls to
419 	 * ip_join_allmulti().
420 	 */
421 	ip_purge_allmulti(ill);
422 
423 	/*
424 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
425 	 */
426 	if (IS_UNDER_IPMP(ill))
427 		ipmp_ill_leave_illgrp(ill);
428 
429 	/*
430 	 * ill_down will arrange to blow off any IRE's dependent on this
431 	 * ILL, and shut down fragmentation reassembly.
432 	 */
433 	ill_down(ill);
434 
435 	/* Let SCTP know, so that it can remove this from its list. */
436 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
437 
438 	/*
439 	 * Walk all CONNs that can have a reference on an ire or nce for this
440 	 * ill (we actually walk all that now have stale references).
441 	 */
442 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
443 
444 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
445 	if (ill->ill_isv6)
446 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
447 
448 	/*
449 	 * If an address on this ILL is being used as a source address then
450 	 * clear out the pointers in other ILLs that point to this ILL.
451 	 */
452 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
453 	if (ill->ill_usesrc_grp_next != NULL) {
454 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
455 			ill_disband_usesrc_group(ill);
456 		} else {	/* consumer of the usesrc ILL */
457 			prev_ill = ill_prev_usesrc(ill);
458 			prev_ill->ill_usesrc_grp_next =
459 			    ill->ill_usesrc_grp_next;
460 		}
461 	}
462 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
463 }
464 
465 static void
466 ipif_non_duplicate(ipif_t *ipif)
467 {
468 	ill_t *ill = ipif->ipif_ill;
469 	mutex_enter(&ill->ill_lock);
470 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
471 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
472 		ASSERT(ill->ill_ipif_dup_count > 0);
473 		ill->ill_ipif_dup_count--;
474 	}
475 	mutex_exit(&ill->ill_lock);
476 }
477 
478 /*
479  * ill_delete_tail is called from ip_modclose after all references
480  * to the closing ill are gone. The wait is done in ip_modclose
481  */
482 void
483 ill_delete_tail(ill_t *ill)
484 {
485 	mblk_t	**mpp;
486 	ipif_t	*ipif;
487 	ip_stack_t *ipst = ill->ill_ipst;
488 
489 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
490 		ipif_non_duplicate(ipif);
491 		(void) ipif_down_tail(ipif);
492 	}
493 
494 	ASSERT(ill->ill_ipif_dup_count == 0);
495 
496 	/*
497 	 * If polling capability is enabled (which signifies direct
498 	 * upcall into IP and driver has ill saved as a handle),
499 	 * we need to make sure that unbind has completed before we
500 	 * let the ill disappear and driver no longer has any reference
501 	 * to this ill.
502 	 */
503 	mutex_enter(&ill->ill_lock);
504 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
505 		cv_wait(&ill->ill_cv, &ill->ill_lock);
506 	mutex_exit(&ill->ill_lock);
507 	ASSERT(!(ill->ill_capabilities &
508 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
509 
510 	if (ill->ill_net_type != IRE_LOOPBACK)
511 		qprocsoff(ill->ill_rq);
512 
513 	/*
514 	 * We do an ipsq_flush once again now. New messages could have
515 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
516 	 * could also have landed up if an ioctl thread had looked up
517 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
518 	 * enqueued the ioctl when we did the ipsq_flush last time.
519 	 */
520 	ipsq_flush(ill);
521 
522 	/*
523 	 * Free capabilities.
524 	 */
525 	if (ill->ill_hcksum_capab != NULL) {
526 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
527 		ill->ill_hcksum_capab = NULL;
528 	}
529 
530 	if (ill->ill_zerocopy_capab != NULL) {
531 		kmem_free(ill->ill_zerocopy_capab,
532 		    sizeof (ill_zerocopy_capab_t));
533 		ill->ill_zerocopy_capab = NULL;
534 	}
535 
536 	if (ill->ill_lso_capab != NULL) {
537 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
538 		ill->ill_lso_capab = NULL;
539 	}
540 
541 	if (ill->ill_dld_capab != NULL) {
542 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
543 		ill->ill_dld_capab = NULL;
544 	}
545 
546 	while (ill->ill_ipif != NULL)
547 		ipif_free_tail(ill->ill_ipif);
548 
549 	/*
550 	 * We have removed all references to ilm from conn and the ones joined
551 	 * within the kernel.
552 	 *
553 	 * We don't walk conns, mrts and ires because
554 	 *
555 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
556 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
557 	 *    ill references.
558 	 */
559 
560 	/*
561 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
562 	 * is safe to do because the illgrp has already been unlinked from the
563 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
564 	 */
565 	if (IS_IPMP(ill)) {
566 		ipmp_illgrp_destroy(ill->ill_grp);
567 		ill->ill_grp = NULL;
568 	}
569 
570 	/*
571 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
572 	 * could free the phyint. No more reference to the phyint after this
573 	 * point.
574 	 */
575 	(void) ill_glist_delete(ill);
576 
577 	if (ill->ill_frag_ptr != NULL) {
578 		uint_t count;
579 
580 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
581 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
582 		}
583 		mi_free(ill->ill_frag_ptr);
584 		ill->ill_frag_ptr = NULL;
585 		ill->ill_frag_hash_tbl = NULL;
586 	}
587 
588 	freemsg(ill->ill_nd_lla_mp);
589 	/* Free all retained control messages. */
590 	mpp = &ill->ill_first_mp_to_free;
591 	do {
592 		while (mpp[0]) {
593 			mblk_t  *mp;
594 			mblk_t  *mp1;
595 
596 			mp = mpp[0];
597 			mpp[0] = mp->b_next;
598 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
599 				mp1->b_next = NULL;
600 				mp1->b_prev = NULL;
601 			}
602 			freemsg(mp);
603 		}
604 	} while (mpp++ != &ill->ill_last_mp_to_free);
605 
606 	ill_free_mib(ill);
607 
608 #ifdef DEBUG
609 	ill_trace_cleanup(ill);
610 #endif
611 
612 	/* The default multicast interface might have changed */
613 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
614 
615 	/* Drop refcnt here */
616 	netstack_rele(ill->ill_ipst->ips_netstack);
617 	ill->ill_ipst = NULL;
618 }
619 
620 static void
621 ill_free_mib(ill_t *ill)
622 {
623 	ip_stack_t *ipst = ill->ill_ipst;
624 
625 	/*
626 	 * MIB statistics must not be lost, so when an interface
627 	 * goes away the counter values will be added to the global
628 	 * MIBs.
629 	 */
630 	if (ill->ill_ip_mib != NULL) {
631 		if (ill->ill_isv6) {
632 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
633 			    ill->ill_ip_mib);
634 		} else {
635 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
636 			    ill->ill_ip_mib);
637 		}
638 
639 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
640 		ill->ill_ip_mib = NULL;
641 	}
642 	if (ill->ill_icmp6_mib != NULL) {
643 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
644 		    ill->ill_icmp6_mib);
645 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
646 		ill->ill_icmp6_mib = NULL;
647 	}
648 }
649 
650 /*
651  * Concatenate together a physical address and a sap.
652  *
653  * Sap_lengths are interpreted as follows:
654  *   sap_length == 0	==>	no sap
655  *   sap_length > 0	==>	sap is at the head of the dlpi address
656  *   sap_length < 0	==>	sap is at the tail of the dlpi address
657  */
658 static void
659 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
660     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
661 {
662 	uint16_t sap_addr = (uint16_t)sap_src;
663 
664 	if (sap_length == 0) {
665 		if (phys_src == NULL)
666 			bzero(dst, phys_length);
667 		else
668 			bcopy(phys_src, dst, phys_length);
669 	} else if (sap_length < 0) {
670 		if (phys_src == NULL)
671 			bzero(dst, phys_length);
672 		else
673 			bcopy(phys_src, dst, phys_length);
674 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
675 	} else {
676 		bcopy(&sap_addr, dst, sizeof (sap_addr));
677 		if (phys_src == NULL)
678 			bzero((char *)dst + sap_length, phys_length);
679 		else
680 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
681 	}
682 }
683 
684 /*
685  * Generate a dl_unitdata_req mblk for the device and address given.
686  * addr_length is the length of the physical portion of the address.
687  * If addr is NULL include an all zero address of the specified length.
688  * TRUE? In any case, addr_length is taken to be the entire length of the
689  * dlpi address, including the absolute value of sap_length.
690  */
691 mblk_t *
692 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
693 		t_scalar_t sap_length)
694 {
695 	dl_unitdata_req_t *dlur;
696 	mblk_t	*mp;
697 	t_scalar_t	abs_sap_length;		/* absolute value */
698 
699 	abs_sap_length = ABS(sap_length);
700 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
701 	    DL_UNITDATA_REQ);
702 	if (mp == NULL)
703 		return (NULL);
704 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
705 	/* HACK: accomodate incompatible DLPI drivers */
706 	if (addr_length == 8)
707 		addr_length = 6;
708 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
709 	dlur->dl_dest_addr_offset = sizeof (*dlur);
710 	dlur->dl_priority.dl_min = 0;
711 	dlur->dl_priority.dl_max = 0;
712 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
713 	    (uchar_t *)&dlur[1]);
714 	return (mp);
715 }
716 
717 /*
718  * Add the pending mp to the list. There can be only 1 pending mp
719  * in the list. Any exclusive ioctl that needs to wait for a response
720  * from another module or driver needs to use this function to set
721  * the ipx_pending_mp to the ioctl mblk and wait for the response from
722  * the other module/driver. This is also used while waiting for the
723  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
724  */
725 boolean_t
726 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
727     int waitfor)
728 {
729 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
730 
731 	ASSERT(IAM_WRITER_IPIF(ipif));
732 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
733 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
734 	ASSERT(ipx->ipx_pending_mp == NULL);
735 	/*
736 	 * The caller may be using a different ipif than the one passed into
737 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
738 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
739 	 * that `ipx_current_ipif == ipif'.
740 	 */
741 	ASSERT(ipx->ipx_current_ipif != NULL);
742 
743 	/*
744 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
745 	 * driver.
746 	 */
747 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
748 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
749 	    (DB_TYPE(add_mp) == M_PCPROTO));
750 
751 	if (connp != NULL) {
752 		ASSERT(MUTEX_HELD(&connp->conn_lock));
753 		/*
754 		 * Return error if the conn has started closing. The conn
755 		 * could have finished cleaning up the pending mp list,
756 		 * If so we should not add another mp to the list negating
757 		 * the cleanup.
758 		 */
759 		if (connp->conn_state_flags & CONN_CLOSING)
760 			return (B_FALSE);
761 	}
762 	mutex_enter(&ipx->ipx_lock);
763 	ipx->ipx_pending_ipif = ipif;
764 	/*
765 	 * Note down the queue in b_queue. This will be returned by
766 	 * ipsq_pending_mp_get. Caller will then use these values to restart
767 	 * the processing
768 	 */
769 	add_mp->b_next = NULL;
770 	add_mp->b_queue = q;
771 	ipx->ipx_pending_mp = add_mp;
772 	ipx->ipx_waitfor = waitfor;
773 	mutex_exit(&ipx->ipx_lock);
774 
775 	if (connp != NULL)
776 		connp->conn_oper_pending_ill = ipif->ipif_ill;
777 
778 	return (B_TRUE);
779 }
780 
781 /*
782  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
783  * queued in the list.
784  */
785 mblk_t *
786 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
787 {
788 	mblk_t	*curr = NULL;
789 	ipxop_t	*ipx = ipsq->ipsq_xop;
790 
791 	*connpp = NULL;
792 	mutex_enter(&ipx->ipx_lock);
793 	if (ipx->ipx_pending_mp == NULL) {
794 		mutex_exit(&ipx->ipx_lock);
795 		return (NULL);
796 	}
797 
798 	/* There can be only 1 such excl message */
799 	curr = ipx->ipx_pending_mp;
800 	ASSERT(curr->b_next == NULL);
801 	ipx->ipx_pending_ipif = NULL;
802 	ipx->ipx_pending_mp = NULL;
803 	ipx->ipx_waitfor = 0;
804 	mutex_exit(&ipx->ipx_lock);
805 
806 	if (CONN_Q(curr->b_queue)) {
807 		/*
808 		 * This mp did a refhold on the conn, at the start of the ioctl.
809 		 * So we can safely return a pointer to the conn to the caller.
810 		 */
811 		*connpp = Q_TO_CONN(curr->b_queue);
812 	} else {
813 		*connpp = NULL;
814 	}
815 	curr->b_next = NULL;
816 	curr->b_prev = NULL;
817 	return (curr);
818 }
819 
820 /*
821  * Cleanup the ioctl mp queued in ipx_pending_mp
822  * - Called in the ill_delete path
823  * - Called in the M_ERROR or M_HANGUP path on the ill.
824  * - Called in the conn close path.
825  *
826  * Returns success on finding the pending mblk associated with the ioctl or
827  * exclusive operation in progress, failure otherwise.
828  */
829 boolean_t
830 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
831 {
832 	mblk_t	*mp;
833 	ipxop_t	*ipx;
834 	queue_t	*q;
835 	ipif_t	*ipif;
836 	int	cmd;
837 
838 	ASSERT(IAM_WRITER_ILL(ill));
839 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
840 
841 	mutex_enter(&ipx->ipx_lock);
842 	mp = ipx->ipx_pending_mp;
843 	if (connp != NULL) {
844 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
845 			/*
846 			 * Nothing to clean since the conn that is closing
847 			 * does not have a matching pending mblk in
848 			 * ipx_pending_mp.
849 			 */
850 			mutex_exit(&ipx->ipx_lock);
851 			return (B_FALSE);
852 		}
853 	} else {
854 		/*
855 		 * A non-zero ill_error signifies we are called in the
856 		 * M_ERROR or M_HANGUP path and we need to unconditionally
857 		 * abort any current ioctl and do the corresponding cleanup.
858 		 * A zero ill_error means we are in the ill_delete path and
859 		 * we do the cleanup only if there is a pending mp.
860 		 */
861 		if (mp == NULL && ill->ill_error == 0) {
862 			mutex_exit(&ipx->ipx_lock);
863 			return (B_FALSE);
864 		}
865 	}
866 
867 	/* Now remove from the ipx_pending_mp */
868 	ipx->ipx_pending_mp = NULL;
869 	ipif = ipx->ipx_pending_ipif;
870 	ipx->ipx_pending_ipif = NULL;
871 	ipx->ipx_waitfor = 0;
872 	ipx->ipx_current_ipif = NULL;
873 	cmd = ipx->ipx_current_ioctl;
874 	ipx->ipx_current_ioctl = 0;
875 	ipx->ipx_current_done = B_TRUE;
876 	mutex_exit(&ipx->ipx_lock);
877 
878 	if (mp == NULL)
879 		return (B_FALSE);
880 
881 	q = mp->b_queue;
882 	mp->b_next = NULL;
883 	mp->b_prev = NULL;
884 	mp->b_queue = NULL;
885 
886 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
887 		DTRACE_PROBE4(ipif__ioctl,
888 		    char *, "ipsq_pending_mp_cleanup",
889 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
890 		    ipif_t *, ipif);
891 		if (connp == NULL) {
892 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
893 		} else {
894 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
895 			mutex_enter(&ipif->ipif_ill->ill_lock);
896 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
897 			mutex_exit(&ipif->ipif_ill->ill_lock);
898 		}
899 	} else {
900 		inet_freemsg(mp);
901 	}
902 	return (B_TRUE);
903 }
904 
905 /*
906  * Called in the conn close path and ill delete path
907  */
908 static void
909 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
910 {
911 	ipsq_t	*ipsq;
912 	mblk_t	*prev;
913 	mblk_t	*curr;
914 	mblk_t	*next;
915 	queue_t	*wq, *rq = NULL;
916 	mblk_t	*tmp_list = NULL;
917 
918 	ASSERT(IAM_WRITER_ILL(ill));
919 	if (connp != NULL)
920 		wq = CONNP_TO_WQ(connp);
921 	else
922 		wq = ill->ill_wq;
923 
924 	/*
925 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
926 	 * against this here.
927 	 */
928 	if (wq != NULL)
929 		rq = RD(wq);
930 
931 	ipsq = ill->ill_phyint->phyint_ipsq;
932 	/*
933 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
934 	 * In the case of ioctl from a conn, there can be only 1 mp
935 	 * queued on the ipsq. If an ill is being unplumbed flush all
936 	 * the messages.
937 	 */
938 	mutex_enter(&ipsq->ipsq_lock);
939 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
940 	    curr = next) {
941 		next = curr->b_next;
942 		if (connp == NULL ||
943 		    (curr->b_queue == wq || curr->b_queue == rq)) {
944 			/* Unlink the mblk from the pending mp list */
945 			if (prev != NULL) {
946 				prev->b_next = curr->b_next;
947 			} else {
948 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
949 				ipsq->ipsq_xopq_mphead = curr->b_next;
950 			}
951 			if (ipsq->ipsq_xopq_mptail == curr)
952 				ipsq->ipsq_xopq_mptail = prev;
953 			/*
954 			 * Create a temporary list and release the ipsq lock
955 			 * New elements are added to the head of the tmp_list
956 			 */
957 			curr->b_next = tmp_list;
958 			tmp_list = curr;
959 		} else {
960 			prev = curr;
961 		}
962 	}
963 	mutex_exit(&ipsq->ipsq_lock);
964 
965 	while (tmp_list != NULL) {
966 		curr = tmp_list;
967 		tmp_list = curr->b_next;
968 		curr->b_next = NULL;
969 		curr->b_prev = NULL;
970 		wq = curr->b_queue;
971 		curr->b_queue = NULL;
972 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
973 			DTRACE_PROBE4(ipif__ioctl,
974 			    char *, "ipsq_xopq_mp_cleanup",
975 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
976 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
977 			    CONN_CLOSE : NO_COPYOUT, NULL);
978 		} else {
979 			/*
980 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
981 			 * this can't be just inet_freemsg. we have to
982 			 * restart it otherwise the thread will be stuck.
983 			 */
984 			inet_freemsg(curr);
985 		}
986 	}
987 }
988 
989 /*
990  * This conn has started closing. Cleanup any pending ioctl from this conn.
991  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
992  */
993 void
994 conn_ioctl_cleanup(conn_t *connp)
995 {
996 	ipsq_t	*ipsq;
997 	ill_t	*ill;
998 	boolean_t refheld;
999 
1000 	/*
1001 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1002 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1003 	 * started the mp could be present in ipx_pending_mp. Note that if
1004 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1005 	 * not yet queued anywhere. In this case, the conn close code will wait
1006 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1007 	 * tcp_close will wait first until all ioctls have completed for this
1008 	 * conn.
1009 	 */
1010 	mutex_enter(&connp->conn_lock);
1011 	ill = connp->conn_oper_pending_ill;
1012 	if (ill == NULL) {
1013 		mutex_exit(&connp->conn_lock);
1014 		return;
1015 	}
1016 
1017 	/*
1018 	 * We may not be able to refhold the ill if the ill/ipif
1019 	 * is changing. But we need to make sure that the ill will
1020 	 * not vanish. So we just bump up the ill_waiter count.
1021 	 */
1022 	refheld = ill_waiter_inc(ill);
1023 	mutex_exit(&connp->conn_lock);
1024 	if (refheld) {
1025 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1026 			ill_waiter_dcr(ill);
1027 			/*
1028 			 * Check whether this ioctl has started and is
1029 			 * pending. If it is not found there then check
1030 			 * whether this ioctl has not even started and is in
1031 			 * the ipsq_xopq list.
1032 			 */
1033 			if (!ipsq_pending_mp_cleanup(ill, connp))
1034 				ipsq_xopq_mp_cleanup(ill, connp);
1035 			ipsq = ill->ill_phyint->phyint_ipsq;
1036 			ipsq_exit(ipsq);
1037 			return;
1038 		}
1039 	}
1040 
1041 	/*
1042 	 * The ill is also closing and we could not bump up the
1043 	 * ill_waiter_count or we could not enter the ipsq. Leave
1044 	 * the cleanup to ill_delete
1045 	 */
1046 	mutex_enter(&connp->conn_lock);
1047 	while (connp->conn_oper_pending_ill != NULL)
1048 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1049 	mutex_exit(&connp->conn_lock);
1050 	if (refheld)
1051 		ill_waiter_dcr(ill);
1052 }
1053 
1054 /*
1055  * ipcl_walk function for cleaning up conn_*_ill fields.
1056  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1057  * conn_bound_if in place. We prefer dropping
1058  * packets instead of sending them out the wrong interface, or accepting
1059  * packets from the wrong ifindex.
1060  */
1061 static void
1062 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1063 {
1064 	ill_t	*ill = (ill_t *)arg;
1065 
1066 	mutex_enter(&connp->conn_lock);
1067 	if (connp->conn_dhcpinit_ill == ill) {
1068 		connp->conn_dhcpinit_ill = NULL;
1069 		ASSERT(ill->ill_dhcpinit != 0);
1070 		atomic_dec_32(&ill->ill_dhcpinit);
1071 		ill_set_inputfn(ill);
1072 	}
1073 	mutex_exit(&connp->conn_lock);
1074 }
1075 
1076 static int
1077 ill_down_ipifs_tail(ill_t *ill)
1078 {
1079 	ipif_t	*ipif;
1080 	int err;
1081 
1082 	ASSERT(IAM_WRITER_ILL(ill));
1083 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1084 		ipif_non_duplicate(ipif);
1085 		/*
1086 		 * ipif_down_tail will call arp_ll_down on the last ipif
1087 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1088 		 */
1089 		if ((err = ipif_down_tail(ipif)) != 0)
1090 			return (err);
1091 	}
1092 	return (0);
1093 }
1094 
1095 /* ARGSUSED */
1096 void
1097 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1098 {
1099 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1100 	(void) ill_down_ipifs_tail(q->q_ptr);
1101 	freemsg(mp);
1102 	ipsq_current_finish(ipsq);
1103 }
1104 
1105 /*
1106  * ill_down_start is called when we want to down this ill and bring it up again
1107  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1108  * all interfaces, but don't tear down any plumbing.
1109  */
1110 boolean_t
1111 ill_down_start(queue_t *q, mblk_t *mp)
1112 {
1113 	ill_t	*ill = q->q_ptr;
1114 	ipif_t	*ipif;
1115 
1116 	ASSERT(IAM_WRITER_ILL(ill));
1117 	mutex_enter(&ill->ill_lock);
1118 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1119 	/* no more nce addition allowed */
1120 	mutex_exit(&ill->ill_lock);
1121 
1122 	/*
1123 	 * It is possible that some ioctl is already in progress while we
1124 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1125 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1126 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1127 	 * the in progress ioctl from ever completing.
1128 	 *
1129 	 * The thread that started the ioctl (if any) must have returned,
1130 	 * since we are now executing as writer. After the 2 calls below,
1131 	 * the state of the ipsq and the ill would reflect no trace of any
1132 	 * pending operation. Subsequently if there is any response to the
1133 	 * original ioctl from the driver, it would be discarded as an
1134 	 * unsolicited message from the driver.
1135 	 */
1136 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1137 	ill_dlpi_clear_deferred(ill);
1138 
1139 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1140 		(void) ipif_down(ipif, NULL, NULL);
1141 
1142 	ill_down(ill);
1143 
1144 	/*
1145 	 * Walk all CONNs that can have a reference on an ire or nce for this
1146 	 * ill (we actually walk all that now have stale references).
1147 	 */
1148 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1149 
1150 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1151 	if (ill->ill_isv6)
1152 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1153 
1154 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1155 
1156 	/*
1157 	 * Atomically test and add the pending mp if references are active.
1158 	 */
1159 	mutex_enter(&ill->ill_lock);
1160 	if (!ill_is_quiescent(ill)) {
1161 		/* call cannot fail since `conn_t *' argument is NULL */
1162 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1163 		    mp, ILL_DOWN);
1164 		mutex_exit(&ill->ill_lock);
1165 		return (B_FALSE);
1166 	}
1167 	mutex_exit(&ill->ill_lock);
1168 	return (B_TRUE);
1169 }
1170 
1171 static void
1172 ill_down(ill_t *ill)
1173 {
1174 	mblk_t	*mp;
1175 	ip_stack_t	*ipst = ill->ill_ipst;
1176 
1177 	/*
1178 	 * Blow off any IREs dependent on this ILL.
1179 	 * The caller needs to handle conn_ixa_cleanup
1180 	 */
1181 	ill_delete_ires(ill);
1182 
1183 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1184 
1185 	/* Remove any conn_*_ill depending on this ill */
1186 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1187 
1188 	/*
1189 	 * Free state for additional IREs.
1190 	 */
1191 	mutex_enter(&ill->ill_saved_ire_lock);
1192 	mp = ill->ill_saved_ire_mp;
1193 	ill->ill_saved_ire_mp = NULL;
1194 	ill->ill_saved_ire_cnt = 0;
1195 	mutex_exit(&ill->ill_saved_ire_lock);
1196 	freemsg(mp);
1197 }
1198 
1199 /*
1200  * ire_walk routine used to delete every IRE that depends on
1201  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1202  *
1203  * Note: since the routes added by the kernel are deleted separately,
1204  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1205  *
1206  * We also remove references on ire_nce_cache entries that refer to the ill.
1207  */
1208 void
1209 ill_downi(ire_t *ire, char *ill_arg)
1210 {
1211 	ill_t	*ill = (ill_t *)ill_arg;
1212 	nce_t	*nce;
1213 
1214 	mutex_enter(&ire->ire_lock);
1215 	nce = ire->ire_nce_cache;
1216 	if (nce != NULL && nce->nce_ill == ill)
1217 		ire->ire_nce_cache = NULL;
1218 	else
1219 		nce = NULL;
1220 	mutex_exit(&ire->ire_lock);
1221 	if (nce != NULL)
1222 		nce_refrele(nce);
1223 	if (ire->ire_ill == ill) {
1224 		/*
1225 		 * The existing interface binding for ire must be
1226 		 * deleted before trying to bind the route to another
1227 		 * interface. However, since we are using the contents of the
1228 		 * ire after ire_delete, the caller has to ensure that
1229 		 * CONDEMNED (deleted) ire's are not removed from the list
1230 		 * when ire_delete() returns. Currently ill_downi() is
1231 		 * only called as part of ire_walk*() routines, so that
1232 		 * the irb_refhold() done by ire_walk*() will ensure that
1233 		 * ire_delete() does not lead to ire_inactive().
1234 		 */
1235 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1236 		ire_delete(ire);
1237 		if (ire->ire_unbound)
1238 			ire_rebind(ire);
1239 	}
1240 }
1241 
1242 /* Remove IRE_IF_CLONE on this ill */
1243 void
1244 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1245 {
1246 	ill_t	*ill = (ill_t *)ill_arg;
1247 
1248 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1249 	if (ire->ire_ill == ill)
1250 		ire_delete(ire);
1251 }
1252 
1253 /* Consume an M_IOCACK of the fastpath probe. */
1254 void
1255 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1256 {
1257 	mblk_t	*mp1 = mp;
1258 
1259 	/*
1260 	 * If this was the first attempt turn on the fastpath probing.
1261 	 */
1262 	mutex_enter(&ill->ill_lock);
1263 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1264 		ill->ill_dlpi_fastpath_state = IDS_OK;
1265 	mutex_exit(&ill->ill_lock);
1266 
1267 	/* Free the M_IOCACK mblk, hold on to the data */
1268 	mp = mp->b_cont;
1269 	freeb(mp1);
1270 	if (mp == NULL)
1271 		return;
1272 	if (mp->b_cont != NULL)
1273 		nce_fastpath_update(ill, mp);
1274 	else
1275 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1276 	freemsg(mp);
1277 }
1278 
1279 /*
1280  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1281  * The data portion of the request is a dl_unitdata_req_t template for
1282  * what we would send downstream in the absence of a fastpath confirmation.
1283  */
1284 int
1285 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1286 {
1287 	struct iocblk	*ioc;
1288 	mblk_t	*mp;
1289 
1290 	if (dlur_mp == NULL)
1291 		return (EINVAL);
1292 
1293 	mutex_enter(&ill->ill_lock);
1294 	switch (ill->ill_dlpi_fastpath_state) {
1295 	case IDS_FAILED:
1296 		/*
1297 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1298 		 * support it.
1299 		 */
1300 		mutex_exit(&ill->ill_lock);
1301 		return (ENOTSUP);
1302 	case IDS_UNKNOWN:
1303 		/* This is the first probe */
1304 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1305 		break;
1306 	default:
1307 		break;
1308 	}
1309 	mutex_exit(&ill->ill_lock);
1310 
1311 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1312 		return (EAGAIN);
1313 
1314 	mp->b_cont = copyb(dlur_mp);
1315 	if (mp->b_cont == NULL) {
1316 		freeb(mp);
1317 		return (EAGAIN);
1318 	}
1319 
1320 	ioc = (struct iocblk *)mp->b_rptr;
1321 	ioc->ioc_count = msgdsize(mp->b_cont);
1322 
1323 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1324 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1325 	putnext(ill->ill_wq, mp);
1326 	return (0);
1327 }
1328 
1329 void
1330 ill_capability_probe(ill_t *ill)
1331 {
1332 	mblk_t	*mp;
1333 
1334 	ASSERT(IAM_WRITER_ILL(ill));
1335 
1336 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1337 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1338 		return;
1339 
1340 	/*
1341 	 * We are starting a new cycle of capability negotiation.
1342 	 * Free up the capab reset messages of any previous incarnation.
1343 	 * We will do a fresh allocation when we get the response to our probe
1344 	 */
1345 	if (ill->ill_capab_reset_mp != NULL) {
1346 		freemsg(ill->ill_capab_reset_mp);
1347 		ill->ill_capab_reset_mp = NULL;
1348 	}
1349 
1350 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1351 
1352 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1353 	if (mp == NULL)
1354 		return;
1355 
1356 	ill_capability_send(ill, mp);
1357 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1358 }
1359 
1360 void
1361 ill_capability_reset(ill_t *ill, boolean_t reneg)
1362 {
1363 	ASSERT(IAM_WRITER_ILL(ill));
1364 
1365 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1366 		return;
1367 
1368 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1369 
1370 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1371 	ill->ill_capab_reset_mp = NULL;
1372 	/*
1373 	 * We turn off all capabilities except those pertaining to
1374 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1375 	 * which will be turned off by the corresponding reset functions.
1376 	 */
1377 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1378 }
1379 
1380 static void
1381 ill_capability_reset_alloc(ill_t *ill)
1382 {
1383 	mblk_t *mp;
1384 	size_t	size = 0;
1385 	int	err;
1386 	dl_capability_req_t	*capb;
1387 
1388 	ASSERT(IAM_WRITER_ILL(ill));
1389 	ASSERT(ill->ill_capab_reset_mp == NULL);
1390 
1391 	if (ILL_HCKSUM_CAPABLE(ill)) {
1392 		size += sizeof (dl_capability_sub_t) +
1393 		    sizeof (dl_capab_hcksum_t);
1394 	}
1395 
1396 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1397 		size += sizeof (dl_capability_sub_t) +
1398 		    sizeof (dl_capab_zerocopy_t);
1399 	}
1400 
1401 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1402 		size += sizeof (dl_capability_sub_t) +
1403 		    sizeof (dl_capab_dld_t);
1404 	}
1405 
1406 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1407 	    STR_NOSIG, &err);
1408 
1409 	mp->b_datap->db_type = M_PROTO;
1410 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1411 
1412 	capb = (dl_capability_req_t *)mp->b_rptr;
1413 	capb->dl_primitive = DL_CAPABILITY_REQ;
1414 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1415 	capb->dl_sub_length = size;
1416 
1417 	mp->b_wptr += sizeof (dl_capability_req_t);
1418 
1419 	/*
1420 	 * Each handler fills in the corresponding dl_capability_sub_t
1421 	 * inside the mblk,
1422 	 */
1423 	ill_capability_hcksum_reset_fill(ill, mp);
1424 	ill_capability_zerocopy_reset_fill(ill, mp);
1425 	ill_capability_dld_reset_fill(ill, mp);
1426 
1427 	ill->ill_capab_reset_mp = mp;
1428 }
1429 
1430 static void
1431 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1432 {
1433 	dl_capab_id_t *id_ic;
1434 	uint_t sub_dl_cap = outers->dl_cap;
1435 	dl_capability_sub_t *inners;
1436 	uint8_t *capend;
1437 
1438 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1439 
1440 	/*
1441 	 * Note: range checks here are not absolutely sufficient to
1442 	 * make us robust against malformed messages sent by drivers;
1443 	 * this is in keeping with the rest of IP's dlpi handling.
1444 	 * (Remember, it's coming from something else in the kernel
1445 	 * address space)
1446 	 */
1447 
1448 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1449 	if (capend > mp->b_wptr) {
1450 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1451 		    "malformed sub-capability too long for mblk");
1452 		return;
1453 	}
1454 
1455 	id_ic = (dl_capab_id_t *)(outers + 1);
1456 
1457 	if (outers->dl_length < sizeof (*id_ic) ||
1458 	    (inners = &id_ic->id_subcap,
1459 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1460 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1461 		    "encapsulated capab type %d too long for mblk",
1462 		    inners->dl_cap);
1463 		return;
1464 	}
1465 
1466 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1467 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1468 		    "isn't as expected; pass-thru module(s) detected, "
1469 		    "discarding capability\n", inners->dl_cap));
1470 		return;
1471 	}
1472 
1473 	/* Process the encapsulated sub-capability */
1474 	ill_capability_dispatch(ill, mp, inners);
1475 }
1476 
1477 static void
1478 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1479 {
1480 	dl_capability_sub_t *dl_subcap;
1481 
1482 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1483 		return;
1484 
1485 	/*
1486 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1487 	 * initialized below since it is not used by DLD.
1488 	 */
1489 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1490 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1491 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1492 
1493 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1494 }
1495 
1496 static void
1497 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1498 {
1499 	/*
1500 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1501 	 * is only to get the VRRP capability.
1502 	 *
1503 	 * Note that we cannot check ill_ipif_up_count here since
1504 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1505 	 * That is done asynchronously, and can race with this function.
1506 	 */
1507 	if (!ill->ill_dl_up) {
1508 		if (subp->dl_cap == DL_CAPAB_VRRP)
1509 			ill_capability_vrrp_ack(ill, mp, subp);
1510 		return;
1511 	}
1512 
1513 	switch (subp->dl_cap) {
1514 	case DL_CAPAB_HCKSUM:
1515 		ill_capability_hcksum_ack(ill, mp, subp);
1516 		break;
1517 	case DL_CAPAB_ZEROCOPY:
1518 		ill_capability_zerocopy_ack(ill, mp, subp);
1519 		break;
1520 	case DL_CAPAB_DLD:
1521 		ill_capability_dld_ack(ill, mp, subp);
1522 		break;
1523 	case DL_CAPAB_VRRP:
1524 		break;
1525 	default:
1526 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1527 		    subp->dl_cap));
1528 	}
1529 }
1530 
1531 /*
1532  * Process the vrrp capability received from a DLS Provider. isub must point
1533  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1534  */
1535 static void
1536 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1537 {
1538 	dl_capab_vrrp_t	*vrrp;
1539 	uint_t		sub_dl_cap = isub->dl_cap;
1540 	uint8_t		*capend;
1541 
1542 	ASSERT(IAM_WRITER_ILL(ill));
1543 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1544 
1545 	/*
1546 	 * Note: range checks here are not absolutely sufficient to
1547 	 * make us robust against malformed messages sent by drivers;
1548 	 * this is in keeping with the rest of IP's dlpi handling.
1549 	 * (Remember, it's coming from something else in the kernel
1550 	 * address space)
1551 	 */
1552 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1553 	if (capend > mp->b_wptr) {
1554 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1555 		    "malformed sub-capability too long for mblk");
1556 		return;
1557 	}
1558 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1559 
1560 	/*
1561 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1562 	 */
1563 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1564 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1565 		ill->ill_flags |= ILLF_VRRP;
1566 	}
1567 }
1568 
1569 /*
1570  * Process a hardware checksum offload capability negotiation ack received
1571  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1572  * of a DL_CAPABILITY_ACK message.
1573  */
1574 static void
1575 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1576 {
1577 	dl_capability_req_t	*ocap;
1578 	dl_capab_hcksum_t	*ihck, *ohck;
1579 	ill_hcksum_capab_t	**ill_hcksum;
1580 	mblk_t			*nmp = NULL;
1581 	uint_t			sub_dl_cap = isub->dl_cap;
1582 	uint8_t			*capend;
1583 
1584 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1585 
1586 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1587 
1588 	/*
1589 	 * Note: range checks here are not absolutely sufficient to
1590 	 * make us robust against malformed messages sent by drivers;
1591 	 * this is in keeping with the rest of IP's dlpi handling.
1592 	 * (Remember, it's coming from something else in the kernel
1593 	 * address space)
1594 	 */
1595 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1596 	if (capend > mp->b_wptr) {
1597 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1598 		    "malformed sub-capability too long for mblk");
1599 		return;
1600 	}
1601 
1602 	/*
1603 	 * There are two types of acks we process here:
1604 	 * 1. acks in reply to a (first form) generic capability req
1605 	 *    (no ENABLE flag set)
1606 	 * 2. acks in reply to a ENABLE capability req.
1607 	 *    (ENABLE flag set)
1608 	 */
1609 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1610 
1611 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1612 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1613 		    "unsupported hardware checksum "
1614 		    "sub-capability (version %d, expected %d)",
1615 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1616 		return;
1617 	}
1618 
1619 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1620 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1621 		    "checksum capability isn't as expected; pass-thru "
1622 		    "module(s) detected, discarding capability\n"));
1623 		return;
1624 	}
1625 
1626 #define	CURR_HCKSUM_CAPAB				\
1627 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1628 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1629 
1630 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1631 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1632 		/* do ENABLE processing */
1633 		if (*ill_hcksum == NULL) {
1634 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1635 			    KM_NOSLEEP);
1636 
1637 			if (*ill_hcksum == NULL) {
1638 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1639 				    "could not enable hcksum version %d "
1640 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1641 				    ill->ill_name);
1642 				return;
1643 			}
1644 		}
1645 
1646 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1647 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1648 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1649 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1650 		    "has enabled hardware checksumming\n ",
1651 		    ill->ill_name));
1652 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1653 		/*
1654 		 * Enabling hardware checksum offload
1655 		 * Currently IP supports {TCP,UDP}/IPv4
1656 		 * partial and full cksum offload and
1657 		 * IPv4 header checksum offload.
1658 		 * Allocate new mblk which will
1659 		 * contain a new capability request
1660 		 * to enable hardware checksum offload.
1661 		 */
1662 		uint_t	size;
1663 		uchar_t	*rptr;
1664 
1665 		size = sizeof (dl_capability_req_t) +
1666 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1667 
1668 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1669 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1670 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1671 			    ill->ill_name);
1672 			return;
1673 		}
1674 
1675 		rptr = nmp->b_rptr;
1676 		/* initialize dl_capability_req_t */
1677 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1678 		ocap->dl_sub_offset =
1679 		    sizeof (dl_capability_req_t);
1680 		ocap->dl_sub_length =
1681 		    sizeof (dl_capability_sub_t) +
1682 		    isub->dl_length;
1683 		nmp->b_rptr += sizeof (dl_capability_req_t);
1684 
1685 		/* initialize dl_capability_sub_t */
1686 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1687 		nmp->b_rptr += sizeof (*isub);
1688 
1689 		/* initialize dl_capab_hcksum_t */
1690 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1691 		bcopy(ihck, ohck, sizeof (*ihck));
1692 
1693 		nmp->b_rptr = rptr;
1694 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1695 
1696 		/* Set ENABLE flag */
1697 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1698 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1699 
1700 		/*
1701 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1702 		 * hardware checksum acceleration.
1703 		 */
1704 		ill_capability_send(ill, nmp);
1705 	} else {
1706 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1707 		    "advertised %x hardware checksum capability flags\n",
1708 		    ill->ill_name, ihck->hcksum_txflags));
1709 	}
1710 }
1711 
1712 static void
1713 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1714 {
1715 	dl_capab_hcksum_t *hck_subcap;
1716 	dl_capability_sub_t *dl_subcap;
1717 
1718 	if (!ILL_HCKSUM_CAPABLE(ill))
1719 		return;
1720 
1721 	ASSERT(ill->ill_hcksum_capab != NULL);
1722 
1723 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1724 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1725 	dl_subcap->dl_length = sizeof (*hck_subcap);
1726 
1727 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1728 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1729 	hck_subcap->hcksum_txflags = 0;
1730 
1731 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1732 }
1733 
1734 static void
1735 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1736 {
1737 	mblk_t *nmp = NULL;
1738 	dl_capability_req_t *oc;
1739 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1740 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1741 	uint_t sub_dl_cap = isub->dl_cap;
1742 	uint8_t *capend;
1743 
1744 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1745 
1746 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1747 
1748 	/*
1749 	 * Note: range checks here are not absolutely sufficient to
1750 	 * make us robust against malformed messages sent by drivers;
1751 	 * this is in keeping with the rest of IP's dlpi handling.
1752 	 * (Remember, it's coming from something else in the kernel
1753 	 * address space)
1754 	 */
1755 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1756 	if (capend > mp->b_wptr) {
1757 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1758 		    "malformed sub-capability too long for mblk");
1759 		return;
1760 	}
1761 
1762 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1763 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1764 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1765 		    "unsupported ZEROCOPY sub-capability (version %d, "
1766 		    "expected %d)", zc_ic->zerocopy_version,
1767 		    ZEROCOPY_VERSION_1);
1768 		return;
1769 	}
1770 
1771 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1772 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1773 		    "capability isn't as expected; pass-thru module(s) "
1774 		    "detected, discarding capability\n"));
1775 		return;
1776 	}
1777 
1778 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1779 		if (*ill_zerocopy_capab == NULL) {
1780 			*ill_zerocopy_capab =
1781 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1782 			    KM_NOSLEEP);
1783 
1784 			if (*ill_zerocopy_capab == NULL) {
1785 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1786 				    "could not enable Zero-copy version %d "
1787 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1788 				    ill->ill_name);
1789 				return;
1790 			}
1791 		}
1792 
1793 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1794 		    "supports Zero-copy version %d\n", ill->ill_name,
1795 		    ZEROCOPY_VERSION_1));
1796 
1797 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1798 		    zc_ic->zerocopy_version;
1799 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1800 		    zc_ic->zerocopy_flags;
1801 
1802 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1803 	} else {
1804 		uint_t size;
1805 		uchar_t *rptr;
1806 
1807 		size = sizeof (dl_capability_req_t) +
1808 		    sizeof (dl_capability_sub_t) +
1809 		    sizeof (dl_capab_zerocopy_t);
1810 
1811 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1812 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1813 			    "could not enable zerocopy for %s (ENOMEM)\n",
1814 			    ill->ill_name);
1815 			return;
1816 		}
1817 
1818 		rptr = nmp->b_rptr;
1819 		/* initialize dl_capability_req_t */
1820 		oc = (dl_capability_req_t *)rptr;
1821 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1822 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1823 		    sizeof (dl_capab_zerocopy_t);
1824 		rptr += sizeof (dl_capability_req_t);
1825 
1826 		/* initialize dl_capability_sub_t */
1827 		bcopy(isub, rptr, sizeof (*isub));
1828 		rptr += sizeof (*isub);
1829 
1830 		/* initialize dl_capab_zerocopy_t */
1831 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1832 		*zc_oc = *zc_ic;
1833 
1834 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1835 		    "to enable zero-copy version %d\n", ill->ill_name,
1836 		    ZEROCOPY_VERSION_1));
1837 
1838 		/* set VMSAFE_MEM flag */
1839 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1840 
1841 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1842 		ill_capability_send(ill, nmp);
1843 	}
1844 }
1845 
1846 static void
1847 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1848 {
1849 	dl_capab_zerocopy_t *zerocopy_subcap;
1850 	dl_capability_sub_t *dl_subcap;
1851 
1852 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1853 		return;
1854 
1855 	ASSERT(ill->ill_zerocopy_capab != NULL);
1856 
1857 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1858 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1859 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1860 
1861 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1862 	zerocopy_subcap->zerocopy_version =
1863 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1864 	zerocopy_subcap->zerocopy_flags = 0;
1865 
1866 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1867 }
1868 
1869 /*
1870  * DLD capability
1871  * Refer to dld.h for more information regarding the purpose and usage
1872  * of this capability.
1873  */
1874 static void
1875 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1876 {
1877 	dl_capab_dld_t		*dld_ic, dld;
1878 	uint_t			sub_dl_cap = isub->dl_cap;
1879 	uint8_t			*capend;
1880 	ill_dld_capab_t		*idc;
1881 
1882 	ASSERT(IAM_WRITER_ILL(ill));
1883 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1884 
1885 	/*
1886 	 * Note: range checks here are not absolutely sufficient to
1887 	 * make us robust against malformed messages sent by drivers;
1888 	 * this is in keeping with the rest of IP's dlpi handling.
1889 	 * (Remember, it's coming from something else in the kernel
1890 	 * address space)
1891 	 */
1892 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1893 	if (capend > mp->b_wptr) {
1894 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1895 		    "malformed sub-capability too long for mblk");
1896 		return;
1897 	}
1898 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1899 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1900 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1901 		    "unsupported DLD sub-capability (version %d, "
1902 		    "expected %d)", dld_ic->dld_version,
1903 		    DLD_CURRENT_VERSION);
1904 		return;
1905 	}
1906 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1907 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1908 		    "capability isn't as expected; pass-thru module(s) "
1909 		    "detected, discarding capability\n"));
1910 		return;
1911 	}
1912 
1913 	/*
1914 	 * Copy locally to ensure alignment.
1915 	 */
1916 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1917 
1918 	if ((idc = ill->ill_dld_capab) == NULL) {
1919 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1920 		if (idc == NULL) {
1921 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1922 			    "could not enable DLD version %d "
1923 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1924 			    ill->ill_name);
1925 			return;
1926 		}
1927 		ill->ill_dld_capab = idc;
1928 	}
1929 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1930 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1931 	ip1dbg(("ill_capability_dld_ack: interface %s "
1932 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1933 
1934 	ill_capability_dld_enable(ill);
1935 }
1936 
1937 /*
1938  * Typically capability negotiation between IP and the driver happens via
1939  * DLPI message exchange. However GLD also offers a direct function call
1940  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1941  * But arbitrary function calls into IP or GLD are not permitted, since both
1942  * of them are protected by their own perimeter mechanism. The perimeter can
1943  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1944  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1945  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1946  * to enter the mac perimeter and then do the direct function calls into
1947  * GLD to enable squeue polling. The ring related callbacks from the mac into
1948  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1949  * protected by the mac perimeter.
1950  */
1951 static void
1952 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1953 {
1954 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1955 	int			err;
1956 
1957 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1958 	    DLD_ENABLE);
1959 	ASSERT(err == 0);
1960 }
1961 
1962 static void
1963 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1964 {
1965 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1966 	int			err;
1967 
1968 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1969 	    DLD_DISABLE);
1970 	ASSERT(err == 0);
1971 }
1972 
1973 boolean_t
1974 ill_mac_perim_held(ill_t *ill)
1975 {
1976 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1977 
1978 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1979 	    DLD_QUERY));
1980 }
1981 
1982 static void
1983 ill_capability_direct_enable(ill_t *ill)
1984 {
1985 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1986 	ill_dld_direct_t	*idd = &idc->idc_direct;
1987 	dld_capab_direct_t	direct;
1988 	int			rc;
1989 
1990 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1991 
1992 	bzero(&direct, sizeof (direct));
1993 	direct.di_rx_cf = (uintptr_t)ip_input;
1994 	direct.di_rx_ch = ill;
1995 
1996 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1997 	    DLD_ENABLE);
1998 	if (rc == 0) {
1999 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2000 		idd->idd_tx_dh = direct.di_tx_dh;
2001 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2002 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2003 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2004 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2005 		ASSERT(idd->idd_tx_cb_df != NULL);
2006 		ASSERT(idd->idd_tx_fctl_df != NULL);
2007 		ASSERT(idd->idd_tx_df != NULL);
2008 		/*
2009 		 * One time registration of flow enable callback function
2010 		 */
2011 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2012 		    ill_flow_enable, ill);
2013 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2014 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2015 	} else {
2016 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2017 		    "capability, rc = %d\n", rc);
2018 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2019 	}
2020 }
2021 
2022 static void
2023 ill_capability_poll_enable(ill_t *ill)
2024 {
2025 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2026 	dld_capab_poll_t	poll;
2027 	int			rc;
2028 
2029 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2030 
2031 	bzero(&poll, sizeof (poll));
2032 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2033 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2034 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2035 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2036 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2037 	poll.poll_ring_ch = ill;
2038 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2039 	    DLD_ENABLE);
2040 	if (rc == 0) {
2041 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2042 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2043 	} else {
2044 		ip1dbg(("warning: could not enable POLL "
2045 		    "capability, rc = %d\n", rc));
2046 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2047 	}
2048 }
2049 
2050 /*
2051  * Enable the LSO capability.
2052  */
2053 static void
2054 ill_capability_lso_enable(ill_t *ill)
2055 {
2056 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2057 	dld_capab_lso_t	lso;
2058 	int rc;
2059 
2060 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2061 
2062 	if (ill->ill_lso_capab == NULL) {
2063 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2064 		    KM_NOSLEEP);
2065 		if (ill->ill_lso_capab == NULL) {
2066 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2067 			    "could not enable LSO for %s (ENOMEM)\n",
2068 			    ill->ill_name);
2069 			return;
2070 		}
2071 	}
2072 
2073 	bzero(&lso, sizeof (lso));
2074 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2075 	    DLD_ENABLE)) == 0) {
2076 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2077 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2078 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2079 		ip1dbg(("ill_capability_lso_enable: interface %s "
2080 		    "has enabled LSO\n ", ill->ill_name));
2081 	} else {
2082 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2083 		ill->ill_lso_capab = NULL;
2084 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2085 	}
2086 }
2087 
2088 static void
2089 ill_capability_dld_enable(ill_t *ill)
2090 {
2091 	mac_perim_handle_t mph;
2092 
2093 	ASSERT(IAM_WRITER_ILL(ill));
2094 
2095 	if (ill->ill_isv6)
2096 		return;
2097 
2098 	ill_mac_perim_enter(ill, &mph);
2099 	if (!ill->ill_isv6) {
2100 		ill_capability_direct_enable(ill);
2101 		ill_capability_poll_enable(ill);
2102 		ill_capability_lso_enable(ill);
2103 	}
2104 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2105 	ill_mac_perim_exit(ill, mph);
2106 }
2107 
2108 static void
2109 ill_capability_dld_disable(ill_t *ill)
2110 {
2111 	ill_dld_capab_t	*idc;
2112 	ill_dld_direct_t *idd;
2113 	mac_perim_handle_t	mph;
2114 
2115 	ASSERT(IAM_WRITER_ILL(ill));
2116 
2117 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2118 		return;
2119 
2120 	ill_mac_perim_enter(ill, &mph);
2121 
2122 	idc = ill->ill_dld_capab;
2123 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2124 		/*
2125 		 * For performance we avoid locks in the transmit data path
2126 		 * and don't maintain a count of the number of threads using
2127 		 * direct calls. Thus some threads could be using direct
2128 		 * transmit calls to GLD, even after the capability mechanism
2129 		 * turns it off. This is still safe since the handles used in
2130 		 * the direct calls continue to be valid until the unplumb is
2131 		 * completed. Remove the callback that was added (1-time) at
2132 		 * capab enable time.
2133 		 */
2134 		mutex_enter(&ill->ill_lock);
2135 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2136 		mutex_exit(&ill->ill_lock);
2137 		if (ill->ill_flownotify_mh != NULL) {
2138 			idd = &idc->idc_direct;
2139 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2140 			    ill->ill_flownotify_mh);
2141 			ill->ill_flownotify_mh = NULL;
2142 		}
2143 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2144 		    NULL, DLD_DISABLE);
2145 	}
2146 
2147 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2148 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2149 		ip_squeue_clean_all(ill);
2150 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2151 		    NULL, DLD_DISABLE);
2152 	}
2153 
2154 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2155 		ASSERT(ill->ill_lso_capab != NULL);
2156 		/*
2157 		 * Clear the capability flag for LSO but retain the
2158 		 * ill_lso_capab structure since it's possible that another
2159 		 * thread is still referring to it.  The structure only gets
2160 		 * deallocated when we destroy the ill.
2161 		 */
2162 
2163 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2164 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2165 		    NULL, DLD_DISABLE);
2166 	}
2167 
2168 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2169 	ill_mac_perim_exit(ill, mph);
2170 }
2171 
2172 /*
2173  * Capability Negotiation protocol
2174  *
2175  * We don't wait for DLPI capability operations to finish during interface
2176  * bringup or teardown. Doing so would introduce more asynchrony and the
2177  * interface up/down operations will need multiple return and restarts.
2178  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2179  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2180  * exclusive operation won't start until the DLPI operations of the previous
2181  * exclusive operation complete.
2182  *
2183  * The capability state machine is shown below.
2184  *
2185  * state		next state		event, action
2186  *
2187  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2188  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2189  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2190  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2191  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2192  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2193  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2194  *						    ill_capability_probe.
2195  */
2196 
2197 /*
2198  * Dedicated thread started from ip_stack_init that handles capability
2199  * disable. This thread ensures the taskq dispatch does not fail by waiting
2200  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2201  * that direct calls to DLD are done in a cv_waitable context.
2202  */
2203 void
2204 ill_taskq_dispatch(ip_stack_t *ipst)
2205 {
2206 	callb_cpr_t cprinfo;
2207 	char 	name[64];
2208 	mblk_t	*mp;
2209 
2210 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2211 	    ipst->ips_netstack->netstack_stackid);
2212 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2213 	    name);
2214 	mutex_enter(&ipst->ips_capab_taskq_lock);
2215 
2216 	for (;;) {
2217 		mp = ipst->ips_capab_taskq_head;
2218 		while (mp != NULL) {
2219 			ipst->ips_capab_taskq_head = mp->b_next;
2220 			if (ipst->ips_capab_taskq_head == NULL)
2221 				ipst->ips_capab_taskq_tail = NULL;
2222 			mutex_exit(&ipst->ips_capab_taskq_lock);
2223 			mp->b_next = NULL;
2224 
2225 			VERIFY(taskq_dispatch(system_taskq,
2226 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2227 			mutex_enter(&ipst->ips_capab_taskq_lock);
2228 			mp = ipst->ips_capab_taskq_head;
2229 		}
2230 
2231 		if (ipst->ips_capab_taskq_quit)
2232 			break;
2233 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2234 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2235 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2236 	}
2237 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2238 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2239 	CALLB_CPR_EXIT(&cprinfo);
2240 	thread_exit();
2241 }
2242 
2243 /*
2244  * Consume a new-style hardware capabilities negotiation ack.
2245  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2246  */
2247 static void
2248 ill_capability_ack_thr(void *arg)
2249 {
2250 	mblk_t	*mp = arg;
2251 	dl_capability_ack_t *capp;
2252 	dl_capability_sub_t *subp, *endp;
2253 	ill_t	*ill;
2254 	boolean_t reneg;
2255 
2256 	ill = (ill_t *)mp->b_prev;
2257 	mp->b_prev = NULL;
2258 
2259 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2260 
2261 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2262 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2263 		/*
2264 		 * We have received the ack for our DL_CAPAB reset request.
2265 		 * There isnt' anything in the message that needs processing.
2266 		 * All message based capabilities have been disabled, now
2267 		 * do the function call based capability disable.
2268 		 */
2269 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2270 		ill_capability_dld_disable(ill);
2271 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2272 		if (reneg)
2273 			ill_capability_probe(ill);
2274 		goto done;
2275 	}
2276 
2277 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2278 		ill->ill_dlpi_capab_state = IDCS_OK;
2279 
2280 	capp = (dl_capability_ack_t *)mp->b_rptr;
2281 
2282 	if (capp->dl_sub_length == 0) {
2283 		/* no new-style capabilities */
2284 		goto done;
2285 	}
2286 
2287 	/* make sure the driver supplied correct dl_sub_length */
2288 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2289 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2290 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2291 		goto done;
2292 	}
2293 
2294 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2295 	/*
2296 	 * There are sub-capabilities. Process the ones we know about.
2297 	 * Loop until we don't have room for another sub-cap header..
2298 	 */
2299 	for (subp = SC(capp, capp->dl_sub_offset),
2300 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2301 	    subp <= endp;
2302 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2303 
2304 		switch (subp->dl_cap) {
2305 		case DL_CAPAB_ID_WRAPPER:
2306 			ill_capability_id_ack(ill, mp, subp);
2307 			break;
2308 		default:
2309 			ill_capability_dispatch(ill, mp, subp);
2310 			break;
2311 		}
2312 	}
2313 #undef SC
2314 done:
2315 	inet_freemsg(mp);
2316 	ill_capability_done(ill);
2317 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2318 }
2319 
2320 /*
2321  * This needs to be started in a taskq thread to provide a cv_waitable
2322  * context.
2323  */
2324 void
2325 ill_capability_ack(ill_t *ill, mblk_t *mp)
2326 {
2327 	ip_stack_t	*ipst = ill->ill_ipst;
2328 
2329 	mp->b_prev = (mblk_t *)ill;
2330 	ASSERT(mp->b_next == NULL);
2331 
2332 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2333 	    TQ_NOSLEEP) != 0)
2334 		return;
2335 
2336 	/*
2337 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2338 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2339 	 */
2340 	mutex_enter(&ipst->ips_capab_taskq_lock);
2341 	if (ipst->ips_capab_taskq_head == NULL) {
2342 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2343 		ipst->ips_capab_taskq_head = mp;
2344 	} else {
2345 		ipst->ips_capab_taskq_tail->b_next = mp;
2346 	}
2347 	ipst->ips_capab_taskq_tail = mp;
2348 
2349 	cv_signal(&ipst->ips_capab_taskq_cv);
2350 	mutex_exit(&ipst->ips_capab_taskq_lock);
2351 }
2352 
2353 /*
2354  * This routine is called to scan the fragmentation reassembly table for
2355  * the specified ILL for any packets that are starting to smell.
2356  * dead_interval is the maximum time in seconds that will be tolerated.  It
2357  * will either be the value specified in ip_g_frag_timeout, or zero if the
2358  * ILL is shutting down and it is time to blow everything off.
2359  *
2360  * It returns the number of seconds (as a time_t) that the next frag timer
2361  * should be scheduled for, 0 meaning that the timer doesn't need to be
2362  * re-started.  Note that the method of calculating next_timeout isn't
2363  * entirely accurate since time will flow between the time we grab
2364  * current_time and the time we schedule the next timeout.  This isn't a
2365  * big problem since this is the timer for sending an ICMP reassembly time
2366  * exceeded messages, and it doesn't have to be exactly accurate.
2367  *
2368  * This function is
2369  * sometimes called as writer, although this is not required.
2370  */
2371 time_t
2372 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2373 {
2374 	ipfb_t	*ipfb;
2375 	ipfb_t	*endp;
2376 	ipf_t	*ipf;
2377 	ipf_t	*ipfnext;
2378 	mblk_t	*mp;
2379 	time_t	current_time = gethrestime_sec();
2380 	time_t	next_timeout = 0;
2381 	uint32_t	hdr_length;
2382 	mblk_t	*send_icmp_head;
2383 	mblk_t	*send_icmp_head_v6;
2384 	ip_stack_t *ipst = ill->ill_ipst;
2385 	ip_recv_attr_t iras;
2386 
2387 	bzero(&iras, sizeof (iras));
2388 	iras.ira_flags = 0;
2389 	iras.ira_ill = iras.ira_rill = ill;
2390 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2391 	iras.ira_rifindex = iras.ira_ruifindex;
2392 
2393 	ipfb = ill->ill_frag_hash_tbl;
2394 	if (ipfb == NULL)
2395 		return (B_FALSE);
2396 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2397 	/* Walk the frag hash table. */
2398 	for (; ipfb < endp; ipfb++) {
2399 		send_icmp_head = NULL;
2400 		send_icmp_head_v6 = NULL;
2401 		mutex_enter(&ipfb->ipfb_lock);
2402 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2403 			time_t frag_time = current_time - ipf->ipf_timestamp;
2404 			time_t frag_timeout;
2405 
2406 			if (frag_time < dead_interval) {
2407 				/*
2408 				 * There are some outstanding fragments
2409 				 * that will timeout later.  Make note of
2410 				 * the time so that we can reschedule the
2411 				 * next timeout appropriately.
2412 				 */
2413 				frag_timeout = dead_interval - frag_time;
2414 				if (next_timeout == 0 ||
2415 				    frag_timeout < next_timeout) {
2416 					next_timeout = frag_timeout;
2417 				}
2418 				break;
2419 			}
2420 			/* Time's up.  Get it out of here. */
2421 			hdr_length = ipf->ipf_nf_hdr_len;
2422 			ipfnext = ipf->ipf_hash_next;
2423 			if (ipfnext)
2424 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2425 			*ipf->ipf_ptphn = ipfnext;
2426 			mp = ipf->ipf_mp->b_cont;
2427 			for (; mp; mp = mp->b_cont) {
2428 				/* Extra points for neatness. */
2429 				IP_REASS_SET_START(mp, 0);
2430 				IP_REASS_SET_END(mp, 0);
2431 			}
2432 			mp = ipf->ipf_mp->b_cont;
2433 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2434 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2435 			ipfb->ipfb_count -= ipf->ipf_count;
2436 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2437 			ipfb->ipfb_frag_pkts--;
2438 			/*
2439 			 * We do not send any icmp message from here because
2440 			 * we currently are holding the ipfb_lock for this
2441 			 * hash chain. If we try and send any icmp messages
2442 			 * from here we may end up via a put back into ip
2443 			 * trying to get the same lock, causing a recursive
2444 			 * mutex panic. Instead we build a list and send all
2445 			 * the icmp messages after we have dropped the lock.
2446 			 */
2447 			if (ill->ill_isv6) {
2448 				if (hdr_length != 0) {
2449 					mp->b_next = send_icmp_head_v6;
2450 					send_icmp_head_v6 = mp;
2451 				} else {
2452 					freemsg(mp);
2453 				}
2454 			} else {
2455 				if (hdr_length != 0) {
2456 					mp->b_next = send_icmp_head;
2457 					send_icmp_head = mp;
2458 				} else {
2459 					freemsg(mp);
2460 				}
2461 			}
2462 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2463 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2464 			freeb(ipf->ipf_mp);
2465 		}
2466 		mutex_exit(&ipfb->ipfb_lock);
2467 		/*
2468 		 * Now need to send any icmp messages that we delayed from
2469 		 * above.
2470 		 */
2471 		while (send_icmp_head_v6 != NULL) {
2472 			ip6_t *ip6h;
2473 
2474 			mp = send_icmp_head_v6;
2475 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2476 			mp->b_next = NULL;
2477 			ip6h = (ip6_t *)mp->b_rptr;
2478 			iras.ira_flags = 0;
2479 			/*
2480 			 * This will result in an incorrect ALL_ZONES zoneid
2481 			 * for multicast packets, but we
2482 			 * don't send ICMP errors for those in any case.
2483 			 */
2484 			iras.ira_zoneid =
2485 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2486 			    ill, ipst);
2487 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2488 			icmp_time_exceeded_v6(mp,
2489 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2490 			    &iras);
2491 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2492 		}
2493 		while (send_icmp_head != NULL) {
2494 			ipaddr_t dst;
2495 
2496 			mp = send_icmp_head;
2497 			send_icmp_head = send_icmp_head->b_next;
2498 			mp->b_next = NULL;
2499 
2500 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2501 
2502 			iras.ira_flags = IRAF_IS_IPV4;
2503 			/*
2504 			 * This will result in an incorrect ALL_ZONES zoneid
2505 			 * for broadcast and multicast packets, but we
2506 			 * don't send ICMP errors for those in any case.
2507 			 */
2508 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2509 			    ill, ipst);
2510 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2511 			icmp_time_exceeded(mp,
2512 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2513 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2514 		}
2515 	}
2516 	/*
2517 	 * A non-dying ILL will use the return value to decide whether to
2518 	 * restart the frag timer, and for how long.
2519 	 */
2520 	return (next_timeout);
2521 }
2522 
2523 /*
2524  * This routine is called when the approximate count of mblk memory used
2525  * for the specified ILL has exceeded max_count.
2526  */
2527 void
2528 ill_frag_prune(ill_t *ill, uint_t max_count)
2529 {
2530 	ipfb_t	*ipfb;
2531 	ipf_t	*ipf;
2532 	size_t	count;
2533 	clock_t now;
2534 
2535 	/*
2536 	 * If we are here within ip_min_frag_prune_time msecs remove
2537 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2538 	 * ill_frag_free_num_pkts.
2539 	 */
2540 	mutex_enter(&ill->ill_lock);
2541 	now = ddi_get_lbolt();
2542 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2543 	    (ip_min_frag_prune_time != 0 ?
2544 	    ip_min_frag_prune_time : msec_per_tick)) {
2545 
2546 		ill->ill_frag_free_num_pkts++;
2547 
2548 	} else {
2549 		ill->ill_frag_free_num_pkts = 0;
2550 	}
2551 	ill->ill_last_frag_clean_time = now;
2552 	mutex_exit(&ill->ill_lock);
2553 
2554 	/*
2555 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2556 	 */
2557 	if (ill->ill_frag_free_num_pkts != 0) {
2558 		int ix;
2559 
2560 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2561 			ipfb = &ill->ill_frag_hash_tbl[ix];
2562 			mutex_enter(&ipfb->ipfb_lock);
2563 			if (ipfb->ipfb_ipf != NULL) {
2564 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2565 				    ill->ill_frag_free_num_pkts);
2566 			}
2567 			mutex_exit(&ipfb->ipfb_lock);
2568 		}
2569 	}
2570 	/*
2571 	 * While the reassembly list for this ILL is too big, prune a fragment
2572 	 * queue by age, oldest first.
2573 	 */
2574 	while (ill->ill_frag_count > max_count) {
2575 		int	ix;
2576 		ipfb_t	*oipfb = NULL;
2577 		uint_t	oldest = UINT_MAX;
2578 
2579 		count = 0;
2580 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2581 			ipfb = &ill->ill_frag_hash_tbl[ix];
2582 			mutex_enter(&ipfb->ipfb_lock);
2583 			ipf = ipfb->ipfb_ipf;
2584 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2585 				oldest = ipf->ipf_gen;
2586 				oipfb = ipfb;
2587 			}
2588 			count += ipfb->ipfb_count;
2589 			mutex_exit(&ipfb->ipfb_lock);
2590 		}
2591 		if (oipfb == NULL)
2592 			break;
2593 
2594 		if (count <= max_count)
2595 			return;	/* Somebody beat us to it, nothing to do */
2596 		mutex_enter(&oipfb->ipfb_lock);
2597 		ipf = oipfb->ipfb_ipf;
2598 		if (ipf != NULL) {
2599 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2600 		}
2601 		mutex_exit(&oipfb->ipfb_lock);
2602 	}
2603 }
2604 
2605 /*
2606  * free 'free_cnt' fragmented packets starting at ipf.
2607  */
2608 void
2609 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2610 {
2611 	size_t	count;
2612 	mblk_t	*mp;
2613 	mblk_t	*tmp;
2614 	ipf_t **ipfp = ipf->ipf_ptphn;
2615 
2616 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2617 	ASSERT(ipfp != NULL);
2618 	ASSERT(ipf != NULL);
2619 
2620 	while (ipf != NULL && free_cnt-- > 0) {
2621 		count = ipf->ipf_count;
2622 		mp = ipf->ipf_mp;
2623 		ipf = ipf->ipf_hash_next;
2624 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2625 			IP_REASS_SET_START(tmp, 0);
2626 			IP_REASS_SET_END(tmp, 0);
2627 		}
2628 		atomic_add_32(&ill->ill_frag_count, -count);
2629 		ASSERT(ipfb->ipfb_count >= count);
2630 		ipfb->ipfb_count -= count;
2631 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2632 		ipfb->ipfb_frag_pkts--;
2633 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2634 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2635 		freemsg(mp);
2636 	}
2637 
2638 	if (ipf)
2639 		ipf->ipf_ptphn = ipfp;
2640 	ipfp[0] = ipf;
2641 }
2642 
2643 /*
2644  * Helper function for ill_forward_set().
2645  */
2646 static void
2647 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2648 {
2649 	ip_stack_t	*ipst = ill->ill_ipst;
2650 
2651 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2652 
2653 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2654 	    (enable ? "Enabling" : "Disabling"),
2655 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2656 	mutex_enter(&ill->ill_lock);
2657 	if (enable)
2658 		ill->ill_flags |= ILLF_ROUTER;
2659 	else
2660 		ill->ill_flags &= ~ILLF_ROUTER;
2661 	mutex_exit(&ill->ill_lock);
2662 	if (ill->ill_isv6)
2663 		ill_set_nce_router_flags(ill, enable);
2664 	/* Notify routing socket listeners of this change. */
2665 	if (ill->ill_ipif != NULL)
2666 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2667 }
2668 
2669 /*
2670  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2671  * socket messages for each interface whose flags we change.
2672  */
2673 int
2674 ill_forward_set(ill_t *ill, boolean_t enable)
2675 {
2676 	ipmp_illgrp_t *illg;
2677 	ip_stack_t *ipst = ill->ill_ipst;
2678 
2679 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2680 
2681 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2682 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2683 		return (0);
2684 
2685 	if (IS_LOOPBACK(ill))
2686 		return (EINVAL);
2687 
2688 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2689 		/*
2690 		 * Update all of the interfaces in the group.
2691 		 */
2692 		illg = ill->ill_grp;
2693 		ill = list_head(&illg->ig_if);
2694 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2695 			ill_forward_set_on_ill(ill, enable);
2696 
2697 		/*
2698 		 * Update the IPMP meta-interface.
2699 		 */
2700 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2701 		return (0);
2702 	}
2703 
2704 	ill_forward_set_on_ill(ill, enable);
2705 	return (0);
2706 }
2707 
2708 /*
2709  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2710  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2711  * set or clear.
2712  */
2713 static void
2714 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2715 {
2716 	ipif_t *ipif;
2717 	ncec_t *ncec;
2718 	nce_t *nce;
2719 
2720 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2721 		/*
2722 		 * NOTE: we match across the illgrp because nce's for
2723 		 * addresses on IPMP interfaces have an nce_ill that points to
2724 		 * the bound underlying ill.
2725 		 */
2726 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2727 		if (nce != NULL) {
2728 			ncec = nce->nce_common;
2729 			mutex_enter(&ncec->ncec_lock);
2730 			if (enable)
2731 				ncec->ncec_flags |= NCE_F_ISROUTER;
2732 			else
2733 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2734 			mutex_exit(&ncec->ncec_lock);
2735 			nce_refrele(nce);
2736 		}
2737 	}
2738 }
2739 
2740 /*
2741  * Intializes the context structure and returns the first ill in the list
2742  * cuurently start_list and end_list can have values:
2743  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2744  * IP_V4_G_HEAD		Traverse IPV4 list only.
2745  * IP_V6_G_HEAD		Traverse IPV6 list only.
2746  */
2747 
2748 /*
2749  * We don't check for CONDEMNED ills here. Caller must do that if
2750  * necessary under the ill lock.
2751  */
2752 ill_t *
2753 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2754     ip_stack_t *ipst)
2755 {
2756 	ill_if_t *ifp;
2757 	ill_t *ill;
2758 	avl_tree_t *avl_tree;
2759 
2760 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2761 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2762 
2763 	/*
2764 	 * setup the lists to search
2765 	 */
2766 	if (end_list != MAX_G_HEADS) {
2767 		ctx->ctx_current_list = start_list;
2768 		ctx->ctx_last_list = end_list;
2769 	} else {
2770 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2771 		ctx->ctx_current_list = 0;
2772 	}
2773 
2774 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2775 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2776 		if (ifp != (ill_if_t *)
2777 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2778 			avl_tree = &ifp->illif_avl_by_ppa;
2779 			ill = avl_first(avl_tree);
2780 			/*
2781 			 * ill is guaranteed to be non NULL or ifp should have
2782 			 * not existed.
2783 			 */
2784 			ASSERT(ill != NULL);
2785 			return (ill);
2786 		}
2787 		ctx->ctx_current_list++;
2788 	}
2789 
2790 	return (NULL);
2791 }
2792 
2793 /*
2794  * returns the next ill in the list. ill_first() must have been called
2795  * before calling ill_next() or bad things will happen.
2796  */
2797 
2798 /*
2799  * We don't check for CONDEMNED ills here. Caller must do that if
2800  * necessary under the ill lock.
2801  */
2802 ill_t *
2803 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2804 {
2805 	ill_if_t *ifp;
2806 	ill_t *ill;
2807 	ip_stack_t	*ipst = lastill->ill_ipst;
2808 
2809 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2810 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2811 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2812 	    AVL_AFTER)) != NULL) {
2813 		return (ill);
2814 	}
2815 
2816 	/* goto next ill_ifp in the list. */
2817 	ifp = lastill->ill_ifptr->illif_next;
2818 
2819 	/* make sure not at end of circular list */
2820 	while (ifp ==
2821 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2822 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2823 			return (NULL);
2824 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2825 	}
2826 
2827 	return (avl_first(&ifp->illif_avl_by_ppa));
2828 }
2829 
2830 /*
2831  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2832  * The final number (PPA) must not have any leading zeros.  Upon success, a
2833  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2834  */
2835 static char *
2836 ill_get_ppa_ptr(char *name)
2837 {
2838 	int namelen = strlen(name);
2839 	int end_ndx = namelen - 1;
2840 	int ppa_ndx, i;
2841 
2842 	/*
2843 	 * Check that the first character is [a-zA-Z], and that the last
2844 	 * character is [0-9].
2845 	 */
2846 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2847 		return (NULL);
2848 
2849 	/*
2850 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2851 	 */
2852 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2853 		if (!isdigit(name[ppa_ndx - 1]))
2854 			break;
2855 
2856 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2857 		return (NULL);
2858 
2859 	/*
2860 	 * Check that the intermediate characters are [a-z0-9.]
2861 	 */
2862 	for (i = 1; i < ppa_ndx; i++) {
2863 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2864 		    name[i] != '.' && name[i] != '_') {
2865 			return (NULL);
2866 		}
2867 	}
2868 
2869 	return (name + ppa_ndx);
2870 }
2871 
2872 /*
2873  * use avl tree to locate the ill.
2874  */
2875 static ill_t *
2876 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2877 {
2878 	char *ppa_ptr = NULL;
2879 	int len;
2880 	uint_t ppa;
2881 	ill_t *ill = NULL;
2882 	ill_if_t *ifp;
2883 	int list;
2884 
2885 	/*
2886 	 * get ppa ptr
2887 	 */
2888 	if (isv6)
2889 		list = IP_V6_G_HEAD;
2890 	else
2891 		list = IP_V4_G_HEAD;
2892 
2893 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2894 		return (NULL);
2895 	}
2896 
2897 	len = ppa_ptr - name + 1;
2898 
2899 	ppa = stoi(&ppa_ptr);
2900 
2901 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2902 
2903 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2904 		/*
2905 		 * match is done on len - 1 as the name is not null
2906 		 * terminated it contains ppa in addition to the interface
2907 		 * name.
2908 		 */
2909 		if ((ifp->illif_name_len == len) &&
2910 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2911 			break;
2912 		} else {
2913 			ifp = ifp->illif_next;
2914 		}
2915 	}
2916 
2917 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2918 		/*
2919 		 * Even the interface type does not exist.
2920 		 */
2921 		return (NULL);
2922 	}
2923 
2924 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2925 	if (ill != NULL) {
2926 		mutex_enter(&ill->ill_lock);
2927 		if (ILL_CAN_LOOKUP(ill)) {
2928 			ill_refhold_locked(ill);
2929 			mutex_exit(&ill->ill_lock);
2930 			return (ill);
2931 		}
2932 		mutex_exit(&ill->ill_lock);
2933 	}
2934 	return (NULL);
2935 }
2936 
2937 /*
2938  * comparison function for use with avl.
2939  */
2940 static int
2941 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2942 {
2943 	uint_t ppa;
2944 	uint_t ill_ppa;
2945 
2946 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2947 
2948 	ppa = *((uint_t *)ppa_ptr);
2949 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2950 	/*
2951 	 * We want the ill with the lowest ppa to be on the
2952 	 * top.
2953 	 */
2954 	if (ill_ppa < ppa)
2955 		return (1);
2956 	if (ill_ppa > ppa)
2957 		return (-1);
2958 	return (0);
2959 }
2960 
2961 /*
2962  * remove an interface type from the global list.
2963  */
2964 static void
2965 ill_delete_interface_type(ill_if_t *interface)
2966 {
2967 	ASSERT(interface != NULL);
2968 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2969 
2970 	avl_destroy(&interface->illif_avl_by_ppa);
2971 	if (interface->illif_ppa_arena != NULL)
2972 		vmem_destroy(interface->illif_ppa_arena);
2973 
2974 	remque(interface);
2975 
2976 	mi_free(interface);
2977 }
2978 
2979 /*
2980  * remove ill from the global list.
2981  */
2982 static void
2983 ill_glist_delete(ill_t *ill)
2984 {
2985 	ip_stack_t	*ipst;
2986 	phyint_t	*phyi;
2987 
2988 	if (ill == NULL)
2989 		return;
2990 	ipst = ill->ill_ipst;
2991 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2992 
2993 	/*
2994 	 * If the ill was never inserted into the AVL tree
2995 	 * we skip the if branch.
2996 	 */
2997 	if (ill->ill_ifptr != NULL) {
2998 		/*
2999 		 * remove from AVL tree and free ppa number
3000 		 */
3001 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3002 
3003 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3004 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3005 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3006 		}
3007 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3008 			ill_delete_interface_type(ill->ill_ifptr);
3009 		}
3010 
3011 		/*
3012 		 * Indicate ill is no longer in the list.
3013 		 */
3014 		ill->ill_ifptr = NULL;
3015 		ill->ill_name_length = 0;
3016 		ill->ill_name[0] = '\0';
3017 		ill->ill_ppa = UINT_MAX;
3018 	}
3019 
3020 	/* Generate one last event for this ill. */
3021 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3022 	    ill->ill_name_length);
3023 
3024 	ASSERT(ill->ill_phyint != NULL);
3025 	phyi = ill->ill_phyint;
3026 	ill->ill_phyint = NULL;
3027 
3028 	/*
3029 	 * ill_init allocates a phyint always to store the copy
3030 	 * of flags relevant to phyint. At that point in time, we could
3031 	 * not assign the name and hence phyint_illv4/v6 could not be
3032 	 * initialized. Later in ipif_set_values, we assign the name to
3033 	 * the ill, at which point in time we assign phyint_illv4/v6.
3034 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3035 	 */
3036 	if (ill->ill_flags & ILLF_IPV6)
3037 		phyi->phyint_illv6 = NULL;
3038 	else
3039 		phyi->phyint_illv4 = NULL;
3040 
3041 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3042 		rw_exit(&ipst->ips_ill_g_lock);
3043 		return;
3044 	}
3045 
3046 	/*
3047 	 * There are no ills left on this phyint; pull it out of the phyint
3048 	 * avl trees, and free it.
3049 	 */
3050 	if (phyi->phyint_ifindex > 0) {
3051 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3052 		    phyi);
3053 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3054 		    phyi);
3055 	}
3056 	rw_exit(&ipst->ips_ill_g_lock);
3057 
3058 	phyint_free(phyi);
3059 }
3060 
3061 /*
3062  * allocate a ppa, if the number of plumbed interfaces of this type are
3063  * less than ill_no_arena do a linear search to find a unused ppa.
3064  * When the number goes beyond ill_no_arena switch to using an arena.
3065  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3066  * is the return value for an error condition, so allocation starts at one
3067  * and is decremented by one.
3068  */
3069 static int
3070 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3071 {
3072 	ill_t *tmp_ill;
3073 	uint_t start, end;
3074 	int ppa;
3075 
3076 	if (ifp->illif_ppa_arena == NULL &&
3077 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3078 		/*
3079 		 * Create an arena.
3080 		 */
3081 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3082 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3083 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3084 			/* allocate what has already been assigned */
3085 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3086 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3087 		    tmp_ill, AVL_AFTER)) {
3088 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3089 			    1,		/* size */
3090 			    1,		/* align/quantum */
3091 			    0,		/* phase */
3092 			    0,		/* nocross */
3093 			    /* minaddr */
3094 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3095 			    /* maxaddr */
3096 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3097 			    VM_NOSLEEP|VM_FIRSTFIT);
3098 			if (ppa == 0) {
3099 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3100 				    " failed while switching"));
3101 				vmem_destroy(ifp->illif_ppa_arena);
3102 				ifp->illif_ppa_arena = NULL;
3103 				break;
3104 			}
3105 		}
3106 	}
3107 
3108 	if (ifp->illif_ppa_arena != NULL) {
3109 		if (ill->ill_ppa == UINT_MAX) {
3110 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3111 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3112 			if (ppa == 0)
3113 				return (EAGAIN);
3114 			ill->ill_ppa = --ppa;
3115 		} else {
3116 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3117 			    1, 		/* size */
3118 			    1, 		/* align/quantum */
3119 			    0, 		/* phase */
3120 			    0, 		/* nocross */
3121 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3122 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3123 			    VM_NOSLEEP|VM_FIRSTFIT);
3124 			/*
3125 			 * Most likely the allocation failed because
3126 			 * the requested ppa was in use.
3127 			 */
3128 			if (ppa == 0)
3129 				return (EEXIST);
3130 		}
3131 		return (0);
3132 	}
3133 
3134 	/*
3135 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3136 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3137 	 */
3138 	if (ill->ill_ppa == UINT_MAX) {
3139 		end = UINT_MAX - 1;
3140 		start = 0;
3141 	} else {
3142 		end = start = ill->ill_ppa;
3143 	}
3144 
3145 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3146 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3147 		if (start++ >= end) {
3148 			if (ill->ill_ppa == UINT_MAX)
3149 				return (EAGAIN);
3150 			else
3151 				return (EEXIST);
3152 		}
3153 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3154 	}
3155 	ill->ill_ppa = start;
3156 	return (0);
3157 }
3158 
3159 /*
3160  * Insert ill into the list of configured ill's. Once this function completes,
3161  * the ill is globally visible and is available through lookups. More precisely
3162  * this happens after the caller drops the ill_g_lock.
3163  */
3164 static int
3165 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3166 {
3167 	ill_if_t *ill_interface;
3168 	avl_index_t where = 0;
3169 	int error;
3170 	int name_length;
3171 	int index;
3172 	boolean_t check_length = B_FALSE;
3173 	ip_stack_t	*ipst = ill->ill_ipst;
3174 
3175 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3176 
3177 	name_length = mi_strlen(name) + 1;
3178 
3179 	if (isv6)
3180 		index = IP_V6_G_HEAD;
3181 	else
3182 		index = IP_V4_G_HEAD;
3183 
3184 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3185 	/*
3186 	 * Search for interface type based on name
3187 	 */
3188 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3189 		if ((ill_interface->illif_name_len == name_length) &&
3190 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3191 			break;
3192 		}
3193 		ill_interface = ill_interface->illif_next;
3194 	}
3195 
3196 	/*
3197 	 * Interface type not found, create one.
3198 	 */
3199 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3200 		ill_g_head_t ghead;
3201 
3202 		/*
3203 		 * allocate ill_if_t structure
3204 		 */
3205 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3206 		if (ill_interface == NULL) {
3207 			return (ENOMEM);
3208 		}
3209 
3210 		(void) strcpy(ill_interface->illif_name, name);
3211 		ill_interface->illif_name_len = name_length;
3212 
3213 		avl_create(&ill_interface->illif_avl_by_ppa,
3214 		    ill_compare_ppa, sizeof (ill_t),
3215 		    offsetof(struct ill_s, ill_avl_byppa));
3216 
3217 		/*
3218 		 * link the structure in the back to maintain order
3219 		 * of configuration for ifconfig output.
3220 		 */
3221 		ghead = ipst->ips_ill_g_heads[index];
3222 		insque(ill_interface, ghead.ill_g_list_tail);
3223 	}
3224 
3225 	if (ill->ill_ppa == UINT_MAX)
3226 		check_length = B_TRUE;
3227 
3228 	error = ill_alloc_ppa(ill_interface, ill);
3229 	if (error != 0) {
3230 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3231 			ill_delete_interface_type(ill->ill_ifptr);
3232 		return (error);
3233 	}
3234 
3235 	/*
3236 	 * When the ppa is choosen by the system, check that there is
3237 	 * enough space to insert ppa. if a specific ppa was passed in this
3238 	 * check is not required as the interface name passed in will have
3239 	 * the right ppa in it.
3240 	 */
3241 	if (check_length) {
3242 		/*
3243 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3244 		 */
3245 		char buf[sizeof (uint_t) * 3];
3246 
3247 		/*
3248 		 * convert ppa to string to calculate the amount of space
3249 		 * required for it in the name.
3250 		 */
3251 		numtos(ill->ill_ppa, buf);
3252 
3253 		/* Do we have enough space to insert ppa ? */
3254 
3255 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3256 			/* Free ppa and interface type struct */
3257 			if (ill_interface->illif_ppa_arena != NULL) {
3258 				vmem_free(ill_interface->illif_ppa_arena,
3259 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3260 			}
3261 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3262 				ill_delete_interface_type(ill->ill_ifptr);
3263 
3264 			return (EINVAL);
3265 		}
3266 	}
3267 
3268 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3269 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3270 
3271 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3272 	    &where);
3273 	ill->ill_ifptr = ill_interface;
3274 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3275 
3276 	ill_phyint_reinit(ill);
3277 	return (0);
3278 }
3279 
3280 /* Initialize the per phyint ipsq used for serialization */
3281 static boolean_t
3282 ipsq_init(ill_t *ill, boolean_t enter)
3283 {
3284 	ipsq_t  *ipsq;
3285 	ipxop_t	*ipx;
3286 
3287 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3288 		return (B_FALSE);
3289 
3290 	ill->ill_phyint->phyint_ipsq = ipsq;
3291 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3292 	ipx->ipx_ipsq = ipsq;
3293 	ipsq->ipsq_next = ipsq;
3294 	ipsq->ipsq_phyint = ill->ill_phyint;
3295 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3296 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3297 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3298 	if (enter) {
3299 		ipx->ipx_writer = curthread;
3300 		ipx->ipx_forced = B_FALSE;
3301 		ipx->ipx_reentry_cnt = 1;
3302 #ifdef DEBUG
3303 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3304 #endif
3305 	}
3306 	return (B_TRUE);
3307 }
3308 
3309 /*
3310  * ill_init is called by ip_open when a device control stream is opened.
3311  * It does a few initializations, and shoots a DL_INFO_REQ message down
3312  * to the driver.  The response is later picked up in ip_rput_dlpi and
3313  * used to set up default mechanisms for talking to the driver.  (Always
3314  * called as writer.)
3315  *
3316  * If this function returns error, ip_open will call ip_close which in
3317  * turn will call ill_delete to clean up any memory allocated here that
3318  * is not yet freed.
3319  */
3320 int
3321 ill_init(queue_t *q, ill_t *ill)
3322 {
3323 	int	count;
3324 	dl_info_req_t	*dlir;
3325 	mblk_t	*info_mp;
3326 	uchar_t *frag_ptr;
3327 
3328 	/*
3329 	 * The ill is initialized to zero by mi_alloc*(). In addition
3330 	 * some fields already contain valid values, initialized in
3331 	 * ip_open(), before we reach here.
3332 	 */
3333 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3334 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3335 	ill->ill_saved_ire_cnt = 0;
3336 
3337 	ill->ill_rq = q;
3338 	ill->ill_wq = WR(q);
3339 
3340 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3341 	    BPRI_HI);
3342 	if (info_mp == NULL)
3343 		return (ENOMEM);
3344 
3345 	/*
3346 	 * Allocate sufficient space to contain our fragment hash table and
3347 	 * the device name.
3348 	 */
3349 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3350 	if (frag_ptr == NULL) {
3351 		freemsg(info_mp);
3352 		return (ENOMEM);
3353 	}
3354 	ill->ill_frag_ptr = frag_ptr;
3355 	ill->ill_frag_free_num_pkts = 0;
3356 	ill->ill_last_frag_clean_time = 0;
3357 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3358 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3359 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3360 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3361 		    NULL, MUTEX_DEFAULT, NULL);
3362 	}
3363 
3364 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3365 	if (ill->ill_phyint == NULL) {
3366 		freemsg(info_mp);
3367 		mi_free(frag_ptr);
3368 		return (ENOMEM);
3369 	}
3370 
3371 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3372 	/*
3373 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3374 	 * at this point because of the following reason. If we can't
3375 	 * enter the ipsq at some point and cv_wait, the writer that
3376 	 * wakes us up tries to locate us using the list of all phyints
3377 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3378 	 * If we don't set it now, we risk a missed wakeup.
3379 	 */
3380 	ill->ill_phyint->phyint_illv4 = ill;
3381 	ill->ill_ppa = UINT_MAX;
3382 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3383 
3384 	ill_set_inputfn(ill);
3385 
3386 	if (!ipsq_init(ill, B_TRUE)) {
3387 		freemsg(info_mp);
3388 		mi_free(frag_ptr);
3389 		mi_free(ill->ill_phyint);
3390 		return (ENOMEM);
3391 	}
3392 
3393 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3394 
3395 	/* Frag queue limit stuff */
3396 	ill->ill_frag_count = 0;
3397 	ill->ill_ipf_gen = 0;
3398 
3399 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3400 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3401 	ill->ill_global_timer = INFINITY;
3402 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3403 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3404 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3405 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3406 
3407 	/*
3408 	 * Initialize IPv6 configuration variables.  The IP module is always
3409 	 * opened as an IPv4 module.  Instead tracking down the cases where
3410 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3411 	 * here for convenience, this has no effect until the ill is set to do
3412 	 * IPv6.
3413 	 */
3414 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3415 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3416 	ill->ill_max_buf = ND_MAX_Q;
3417 	ill->ill_refcnt = 0;
3418 
3419 	/* Send down the Info Request to the driver. */
3420 	info_mp->b_datap->db_type = M_PCPROTO;
3421 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3422 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3423 	dlir->dl_primitive = DL_INFO_REQ;
3424 
3425 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3426 
3427 	qprocson(q);
3428 	ill_dlpi_send(ill, info_mp);
3429 
3430 	return (0);
3431 }
3432 
3433 /*
3434  * ill_dls_info
3435  * creates datalink socket info from the device.
3436  */
3437 int
3438 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3439 {
3440 	size_t	len;
3441 
3442 	sdl->sdl_family = AF_LINK;
3443 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3444 	sdl->sdl_type = ill->ill_type;
3445 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3446 	len = strlen(sdl->sdl_data);
3447 	ASSERT(len < 256);
3448 	sdl->sdl_nlen = (uchar_t)len;
3449 	sdl->sdl_alen = ill->ill_phys_addr_length;
3450 	sdl->sdl_slen = 0;
3451 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3452 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3453 
3454 	return (sizeof (struct sockaddr_dl));
3455 }
3456 
3457 /*
3458  * ill_xarp_info
3459  * creates xarp info from the device.
3460  */
3461 static int
3462 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3463 {
3464 	sdl->sdl_family = AF_LINK;
3465 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3466 	sdl->sdl_type = ill->ill_type;
3467 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3468 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3469 	sdl->sdl_alen = ill->ill_phys_addr_length;
3470 	sdl->sdl_slen = 0;
3471 	return (sdl->sdl_nlen);
3472 }
3473 
3474 static int
3475 loopback_kstat_update(kstat_t *ksp, int rw)
3476 {
3477 	kstat_named_t *kn;
3478 	netstackid_t	stackid;
3479 	netstack_t	*ns;
3480 	ip_stack_t	*ipst;
3481 
3482 	if (ksp == NULL || ksp->ks_data == NULL)
3483 		return (EIO);
3484 
3485 	if (rw == KSTAT_WRITE)
3486 		return (EACCES);
3487 
3488 	kn = KSTAT_NAMED_PTR(ksp);
3489 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3490 
3491 	ns = netstack_find_by_stackid(stackid);
3492 	if (ns == NULL)
3493 		return (-1);
3494 
3495 	ipst = ns->netstack_ip;
3496 	if (ipst == NULL) {
3497 		netstack_rele(ns);
3498 		return (-1);
3499 	}
3500 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3501 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3502 	netstack_rele(ns);
3503 	return (0);
3504 }
3505 
3506 /*
3507  * Has ifindex been plumbed already?
3508  */
3509 static boolean_t
3510 phyint_exists(uint_t index, ip_stack_t *ipst)
3511 {
3512 	ASSERT(index != 0);
3513 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3514 
3515 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3516 	    &index, NULL) != NULL);
3517 }
3518 
3519 /* Pick a unique ifindex */
3520 boolean_t
3521 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3522 {
3523 	uint_t starting_index;
3524 
3525 	if (!ipst->ips_ill_index_wrap) {
3526 		*indexp = ipst->ips_ill_index++;
3527 		if (ipst->ips_ill_index == 0) {
3528 			/* Reached the uint_t limit Next time wrap  */
3529 			ipst->ips_ill_index_wrap = B_TRUE;
3530 		}
3531 		return (B_TRUE);
3532 	}
3533 
3534 	/*
3535 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3536 	 * at this point and don't want to call any function that attempts
3537 	 * to get the lock again.
3538 	 */
3539 	starting_index = ipst->ips_ill_index++;
3540 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3541 		if (ipst->ips_ill_index != 0 &&
3542 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3543 			/* found unused index - use it */
3544 			*indexp = ipst->ips_ill_index;
3545 			return (B_TRUE);
3546 		}
3547 	}
3548 
3549 	/*
3550 	 * all interface indicies are inuse.
3551 	 */
3552 	return (B_FALSE);
3553 }
3554 
3555 /*
3556  * Assign a unique interface index for the phyint.
3557  */
3558 static boolean_t
3559 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3560 {
3561 	ASSERT(phyi->phyint_ifindex == 0);
3562 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3563 }
3564 
3565 /*
3566  * Initialize the flags on `phyi' as per the provided mactype.
3567  */
3568 static void
3569 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3570 {
3571 	uint64_t flags = 0;
3572 
3573 	/*
3574 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3575 	 * we always presume the underlying hardware is working and set
3576 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3577 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3578 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3579 	 */
3580 	if (mactype == SUNW_DL_IPMP)
3581 		flags |= PHYI_FAILED;
3582 	else
3583 		flags |= PHYI_RUNNING;
3584 
3585 	switch (mactype) {
3586 	case SUNW_DL_VNI:
3587 		flags |= PHYI_VIRTUAL;
3588 		break;
3589 	case SUNW_DL_IPMP:
3590 		flags |= PHYI_IPMP;
3591 		break;
3592 	case DL_LOOP:
3593 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3594 		break;
3595 	}
3596 
3597 	mutex_enter(&phyi->phyint_lock);
3598 	phyi->phyint_flags |= flags;
3599 	mutex_exit(&phyi->phyint_lock);
3600 }
3601 
3602 /*
3603  * Return a pointer to the ill which matches the supplied name.  Note that
3604  * the ill name length includes the null termination character.  (May be
3605  * called as writer.)
3606  * If do_alloc and the interface is "lo0" it will be automatically created.
3607  * Cannot bump up reference on condemned ills. So dup detect can't be done
3608  * using this func.
3609  */
3610 ill_t *
3611 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3612     boolean_t *did_alloc, ip_stack_t *ipst)
3613 {
3614 	ill_t	*ill;
3615 	ipif_t	*ipif;
3616 	ipsq_t	*ipsq;
3617 	kstat_named_t	*kn;
3618 	boolean_t isloopback;
3619 	in6_addr_t ov6addr;
3620 
3621 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3622 
3623 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3624 	ill = ill_find_by_name(name, isv6, ipst);
3625 	rw_exit(&ipst->ips_ill_g_lock);
3626 	if (ill != NULL)
3627 		return (ill);
3628 
3629 	/*
3630 	 * Couldn't find it.  Does this happen to be a lookup for the
3631 	 * loopback device and are we allowed to allocate it?
3632 	 */
3633 	if (!isloopback || !do_alloc)
3634 		return (NULL);
3635 
3636 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3637 	ill = ill_find_by_name(name, isv6, ipst);
3638 	if (ill != NULL) {
3639 		rw_exit(&ipst->ips_ill_g_lock);
3640 		return (ill);
3641 	}
3642 
3643 	/* Create the loopback device on demand */
3644 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3645 	    sizeof (ipif_loopback_name), BPRI_MED));
3646 	if (ill == NULL)
3647 		goto done;
3648 
3649 	*ill = ill_null;
3650 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3651 	ill->ill_ipst = ipst;
3652 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3653 	netstack_hold(ipst->ips_netstack);
3654 	/*
3655 	 * For exclusive stacks we set the zoneid to zero
3656 	 * to make IP operate as if in the global zone.
3657 	 */
3658 	ill->ill_zoneid = GLOBAL_ZONEID;
3659 
3660 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3661 	if (ill->ill_phyint == NULL)
3662 		goto done;
3663 
3664 	if (isv6)
3665 		ill->ill_phyint->phyint_illv6 = ill;
3666 	else
3667 		ill->ill_phyint->phyint_illv4 = ill;
3668 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3669 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3670 
3671 	if (isv6) {
3672 		ill->ill_isv6 = B_TRUE;
3673 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3674 	} else {
3675 		ill->ill_max_frag = ip_loopback_mtuplus;
3676 	}
3677 	if (!ill_allocate_mibs(ill))
3678 		goto done;
3679 	ill->ill_current_frag = ill->ill_max_frag;
3680 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3681 	/*
3682 	 * ipif_loopback_name can't be pointed at directly because its used
3683 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3684 	 * from the glist, ill_glist_delete() sets the first character of
3685 	 * ill_name to '\0'.
3686 	 */
3687 	ill->ill_name = (char *)ill + sizeof (*ill);
3688 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3689 	ill->ill_name_length = sizeof (ipif_loopback_name);
3690 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3691 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3692 
3693 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3694 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3695 	ill->ill_global_timer = INFINITY;
3696 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3697 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3698 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3699 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3700 
3701 	/* No resolver here. */
3702 	ill->ill_net_type = IRE_LOOPBACK;
3703 
3704 	/* Initialize the ipsq */
3705 	if (!ipsq_init(ill, B_FALSE))
3706 		goto done;
3707 
3708 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3709 	if (ipif == NULL)
3710 		goto done;
3711 
3712 	ill->ill_flags = ILLF_MULTICAST;
3713 
3714 	ov6addr = ipif->ipif_v6lcl_addr;
3715 	/* Set up default loopback address and mask. */
3716 	if (!isv6) {
3717 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3718 
3719 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3720 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3721 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3722 		    ipif->ipif_v6subnet);
3723 		ill->ill_flags |= ILLF_IPV4;
3724 	} else {
3725 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3726 		ipif->ipif_v6net_mask = ipv6_all_ones;
3727 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3728 		    ipif->ipif_v6subnet);
3729 		ill->ill_flags |= ILLF_IPV6;
3730 	}
3731 
3732 	/*
3733 	 * Chain us in at the end of the ill list. hold the ill
3734 	 * before we make it globally visible. 1 for the lookup.
3735 	 */
3736 	ill->ill_refcnt = 0;
3737 	ill_refhold(ill);
3738 
3739 	ill->ill_frag_count = 0;
3740 	ill->ill_frag_free_num_pkts = 0;
3741 	ill->ill_last_frag_clean_time = 0;
3742 
3743 	ipsq = ill->ill_phyint->phyint_ipsq;
3744 
3745 	ill_set_inputfn(ill);
3746 
3747 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3748 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3749 
3750 	/* Let SCTP know so that it can add this to its list */
3751 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3752 
3753 	/*
3754 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3755 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3756 	 * requires to be after ill_glist_insert() since we need the
3757 	 * ill_index set. Pass on ipv6_loopback as the old address.
3758 	 */
3759 	sctp_update_ipif_addr(ipif, ov6addr);
3760 
3761 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3762 
3763 	/*
3764 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3765 	 * If so, free our original one.
3766 	 */
3767 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3768 		ipsq_delete(ipsq);
3769 
3770 	if (ipst->ips_loopback_ksp == NULL) {
3771 		/* Export loopback interface statistics */
3772 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3773 		    ipif_loopback_name, "net",
3774 		    KSTAT_TYPE_NAMED, 2, 0,
3775 		    ipst->ips_netstack->netstack_stackid);
3776 		if (ipst->ips_loopback_ksp != NULL) {
3777 			ipst->ips_loopback_ksp->ks_update =
3778 			    loopback_kstat_update;
3779 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3780 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3781 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3782 			ipst->ips_loopback_ksp->ks_private =
3783 			    (void *)(uintptr_t)ipst->ips_netstack->
3784 			    netstack_stackid;
3785 			kstat_install(ipst->ips_loopback_ksp);
3786 		}
3787 	}
3788 
3789 	*did_alloc = B_TRUE;
3790 	rw_exit(&ipst->ips_ill_g_lock);
3791 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3792 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3793 	return (ill);
3794 done:
3795 	if (ill != NULL) {
3796 		if (ill->ill_phyint != NULL) {
3797 			ipsq = ill->ill_phyint->phyint_ipsq;
3798 			if (ipsq != NULL) {
3799 				ipsq->ipsq_phyint = NULL;
3800 				ipsq_delete(ipsq);
3801 			}
3802 			mi_free(ill->ill_phyint);
3803 		}
3804 		ill_free_mib(ill);
3805 		if (ill->ill_ipst != NULL)
3806 			netstack_rele(ill->ill_ipst->ips_netstack);
3807 		mi_free(ill);
3808 	}
3809 	rw_exit(&ipst->ips_ill_g_lock);
3810 	return (NULL);
3811 }
3812 
3813 /*
3814  * For IPP calls - use the ip_stack_t for global stack.
3815  */
3816 ill_t *
3817 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3818 {
3819 	ip_stack_t	*ipst;
3820 	ill_t		*ill;
3821 
3822 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3823 	if (ipst == NULL) {
3824 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3825 		return (NULL);
3826 	}
3827 
3828 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3829 	netstack_rele(ipst->ips_netstack);
3830 	return (ill);
3831 }
3832 
3833 /*
3834  * Return a pointer to the ill which matches the index and IP version type.
3835  */
3836 ill_t *
3837 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3838 {
3839 	ill_t	*ill;
3840 	phyint_t *phyi;
3841 
3842 	/*
3843 	 * Indexes are stored in the phyint - a common structure
3844 	 * to both IPv4 and IPv6.
3845 	 */
3846 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3847 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3848 	    (void *) &index, NULL);
3849 	if (phyi != NULL) {
3850 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3851 		if (ill != NULL) {
3852 			mutex_enter(&ill->ill_lock);
3853 			if (!ILL_IS_CONDEMNED(ill)) {
3854 				ill_refhold_locked(ill);
3855 				mutex_exit(&ill->ill_lock);
3856 				rw_exit(&ipst->ips_ill_g_lock);
3857 				return (ill);
3858 			}
3859 			mutex_exit(&ill->ill_lock);
3860 		}
3861 	}
3862 	rw_exit(&ipst->ips_ill_g_lock);
3863 	return (NULL);
3864 }
3865 
3866 /*
3867  * Verify whether or not an interface index is valid for the specified zoneid
3868  * to transmit packets.
3869  * It can be zero (meaning "reset") or an interface index assigned
3870  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3871  */
3872 boolean_t
3873 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3874     ip_stack_t *ipst)
3875 {
3876 	ill_t		*ill;
3877 
3878 	if (ifindex == 0)
3879 		return (B_TRUE);
3880 
3881 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3882 	if (ill == NULL)
3883 		return (B_FALSE);
3884 	if (IS_VNI(ill)) {
3885 		ill_refrele(ill);
3886 		return (B_FALSE);
3887 	}
3888 	ill_refrele(ill);
3889 	return (B_TRUE);
3890 }
3891 
3892 /*
3893  * Return the ifindex next in sequence after the passed in ifindex.
3894  * If there is no next ifindex for the given protocol, return 0.
3895  */
3896 uint_t
3897 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3898 {
3899 	phyint_t *phyi;
3900 	phyint_t *phyi_initial;
3901 	uint_t   ifindex;
3902 
3903 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3904 
3905 	if (index == 0) {
3906 		phyi = avl_first(
3907 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3908 	} else {
3909 		phyi = phyi_initial = avl_find(
3910 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3911 		    (void *) &index, NULL);
3912 	}
3913 
3914 	for (; phyi != NULL;
3915 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3916 	    phyi, AVL_AFTER)) {
3917 		/*
3918 		 * If we're not returning the first interface in the tree
3919 		 * and we still haven't moved past the phyint_t that
3920 		 * corresponds to index, avl_walk needs to be called again
3921 		 */
3922 		if (!((index != 0) && (phyi == phyi_initial))) {
3923 			if (isv6) {
3924 				if ((phyi->phyint_illv6) &&
3925 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3926 				    (phyi->phyint_illv6->ill_isv6 == 1))
3927 					break;
3928 			} else {
3929 				if ((phyi->phyint_illv4) &&
3930 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3931 				    (phyi->phyint_illv4->ill_isv6 == 0))
3932 					break;
3933 			}
3934 		}
3935 	}
3936 
3937 	rw_exit(&ipst->ips_ill_g_lock);
3938 
3939 	if (phyi != NULL)
3940 		ifindex = phyi->phyint_ifindex;
3941 	else
3942 		ifindex = 0;
3943 
3944 	return (ifindex);
3945 }
3946 
3947 /*
3948  * Return the ifindex for the named interface.
3949  * If there is no next ifindex for the interface, return 0.
3950  */
3951 uint_t
3952 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3953 {
3954 	phyint_t	*phyi;
3955 	avl_index_t	where = 0;
3956 	uint_t		ifindex;
3957 
3958 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3959 
3960 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3961 	    name, &where)) == NULL) {
3962 		rw_exit(&ipst->ips_ill_g_lock);
3963 		return (0);
3964 	}
3965 
3966 	ifindex = phyi->phyint_ifindex;
3967 
3968 	rw_exit(&ipst->ips_ill_g_lock);
3969 
3970 	return (ifindex);
3971 }
3972 
3973 /*
3974  * Return the ifindex to be used by upper layer protocols for instance
3975  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3976  */
3977 uint_t
3978 ill_get_upper_ifindex(const ill_t *ill)
3979 {
3980 	if (IS_UNDER_IPMP(ill))
3981 		return (ipmp_ill_get_ipmp_ifindex(ill));
3982 	else
3983 		return (ill->ill_phyint->phyint_ifindex);
3984 }
3985 
3986 
3987 /*
3988  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3989  * that gives a running thread a reference to the ill. This reference must be
3990  * released by the thread when it is done accessing the ill and related
3991  * objects. ill_refcnt can not be used to account for static references
3992  * such as other structures pointing to an ill. Callers must generally
3993  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3994  * or be sure that the ill is not being deleted or changing state before
3995  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3996  * ill won't change any of its critical state such as address, netmask etc.
3997  */
3998 void
3999 ill_refhold(ill_t *ill)
4000 {
4001 	mutex_enter(&ill->ill_lock);
4002 	ill->ill_refcnt++;
4003 	ILL_TRACE_REF(ill);
4004 	mutex_exit(&ill->ill_lock);
4005 }
4006 
4007 void
4008 ill_refhold_locked(ill_t *ill)
4009 {
4010 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4011 	ill->ill_refcnt++;
4012 	ILL_TRACE_REF(ill);
4013 }
4014 
4015 /* Returns true if we managed to get a refhold */
4016 boolean_t
4017 ill_check_and_refhold(ill_t *ill)
4018 {
4019 	mutex_enter(&ill->ill_lock);
4020 	if (!ILL_IS_CONDEMNED(ill)) {
4021 		ill_refhold_locked(ill);
4022 		mutex_exit(&ill->ill_lock);
4023 		return (B_TRUE);
4024 	}
4025 	mutex_exit(&ill->ill_lock);
4026 	return (B_FALSE);
4027 }
4028 
4029 /*
4030  * Must not be called while holding any locks. Otherwise if this is
4031  * the last reference to be released, there is a chance of recursive mutex
4032  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4033  * to restart an ioctl.
4034  */
4035 void
4036 ill_refrele(ill_t *ill)
4037 {
4038 	mutex_enter(&ill->ill_lock);
4039 	ASSERT(ill->ill_refcnt != 0);
4040 	ill->ill_refcnt--;
4041 	ILL_UNTRACE_REF(ill);
4042 	if (ill->ill_refcnt != 0) {
4043 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4044 		mutex_exit(&ill->ill_lock);
4045 		return;
4046 	}
4047 
4048 	/* Drops the ill_lock */
4049 	ipif_ill_refrele_tail(ill);
4050 }
4051 
4052 /*
4053  * Obtain a weak reference count on the ill. This reference ensures the
4054  * ill won't be freed, but the ill may change any of its critical state
4055  * such as netmask, address etc. Returns an error if the ill has started
4056  * closing.
4057  */
4058 boolean_t
4059 ill_waiter_inc(ill_t *ill)
4060 {
4061 	mutex_enter(&ill->ill_lock);
4062 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4063 		mutex_exit(&ill->ill_lock);
4064 		return (B_FALSE);
4065 	}
4066 	ill->ill_waiters++;
4067 	mutex_exit(&ill->ill_lock);
4068 	return (B_TRUE);
4069 }
4070 
4071 void
4072 ill_waiter_dcr(ill_t *ill)
4073 {
4074 	mutex_enter(&ill->ill_lock);
4075 	ill->ill_waiters--;
4076 	if (ill->ill_waiters == 0)
4077 		cv_broadcast(&ill->ill_cv);
4078 	mutex_exit(&ill->ill_lock);
4079 }
4080 
4081 /*
4082  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4083  * driver.  We construct best guess defaults for lower level information that
4084  * we need.  If an interface is brought up without injection of any overriding
4085  * information from outside, we have to be ready to go with these defaults.
4086  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4087  * we primarely want the dl_provider_style.
4088  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4089  * at which point we assume the other part of the information is valid.
4090  */
4091 void
4092 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4093 {
4094 	uchar_t		*brdcst_addr;
4095 	uint_t		brdcst_addr_length, phys_addr_length;
4096 	t_scalar_t	sap_length;
4097 	dl_info_ack_t	*dlia;
4098 	ip_m_t		*ipm;
4099 	dl_qos_cl_sel1_t *sel1;
4100 	int		min_mtu;
4101 
4102 	ASSERT(IAM_WRITER_ILL(ill));
4103 
4104 	/*
4105 	 * Till the ill is fully up  the ill is not globally visible.
4106 	 * So no need for a lock.
4107 	 */
4108 	dlia = (dl_info_ack_t *)mp->b_rptr;
4109 	ill->ill_mactype = dlia->dl_mac_type;
4110 
4111 	ipm = ip_m_lookup(dlia->dl_mac_type);
4112 	if (ipm == NULL) {
4113 		ipm = ip_m_lookup(DL_OTHER);
4114 		ASSERT(ipm != NULL);
4115 	}
4116 	ill->ill_media = ipm;
4117 
4118 	/*
4119 	 * When the new DLPI stuff is ready we'll pull lengths
4120 	 * from dlia.
4121 	 */
4122 	if (dlia->dl_version == DL_VERSION_2) {
4123 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4124 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4125 		    brdcst_addr_length);
4126 		if (brdcst_addr == NULL) {
4127 			brdcst_addr_length = 0;
4128 		}
4129 		sap_length = dlia->dl_sap_length;
4130 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4131 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4132 		    brdcst_addr_length, sap_length, phys_addr_length));
4133 	} else {
4134 		brdcst_addr_length = 6;
4135 		brdcst_addr = ip_six_byte_all_ones;
4136 		sap_length = -2;
4137 		phys_addr_length = brdcst_addr_length;
4138 	}
4139 
4140 	ill->ill_bcast_addr_length = brdcst_addr_length;
4141 	ill->ill_phys_addr_length = phys_addr_length;
4142 	ill->ill_sap_length = sap_length;
4143 
4144 	/*
4145 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4146 	 * but we must ensure a minimum IP MTU is used since other bits of
4147 	 * IP will fly apart otherwise.
4148 	 */
4149 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4150 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4151 	ill->ill_current_frag = ill->ill_max_frag;
4152 	ill->ill_mtu = ill->ill_max_frag;
4153 
4154 	ill->ill_type = ipm->ip_m_type;
4155 
4156 	if (!ill->ill_dlpi_style_set) {
4157 		if (dlia->dl_provider_style == DL_STYLE2)
4158 			ill->ill_needs_attach = 1;
4159 
4160 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4161 
4162 		/*
4163 		 * Allocate the first ipif on this ill.  We don't delay it
4164 		 * further as ioctl handling assumes at least one ipif exists.
4165 		 *
4166 		 * At this point we don't know whether the ill is v4 or v6.
4167 		 * We will know this whan the SIOCSLIFNAME happens and
4168 		 * the correct value for ill_isv6 will be assigned in
4169 		 * ipif_set_values(). We need to hold the ill lock and
4170 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4171 		 * the wakeup.
4172 		 */
4173 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4174 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4175 		mutex_enter(&ill->ill_lock);
4176 		ASSERT(ill->ill_dlpi_style_set == 0);
4177 		ill->ill_dlpi_style_set = 1;
4178 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4179 		cv_broadcast(&ill->ill_cv);
4180 		mutex_exit(&ill->ill_lock);
4181 		freemsg(mp);
4182 		return;
4183 	}
4184 	ASSERT(ill->ill_ipif != NULL);
4185 	/*
4186 	 * We know whether it is IPv4 or IPv6 now, as this is the
4187 	 * second DL_INFO_ACK we are recieving in response to the
4188 	 * DL_INFO_REQ sent in ipif_set_values.
4189 	 */
4190 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4191 	/*
4192 	 * Clear all the flags that were set based on ill_bcast_addr_length
4193 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4194 	 * changed now and we need to re-evaluate.
4195 	 */
4196 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4197 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4198 
4199 	/*
4200 	 * Free ill_bcast_mp as things could have changed now.
4201 	 *
4202 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4203 	 * with no underlying interfaces (and thus an unknown broadcast
4204 	 * address length), but we enforce that an interface is broadcast-
4205 	 * capable as part of allowing it to join a group.
4206 	 */
4207 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4208 		if (ill->ill_bcast_mp != NULL)
4209 			freemsg(ill->ill_bcast_mp);
4210 		ill->ill_net_type = IRE_IF_NORESOLVER;
4211 
4212 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4213 		    ill->ill_phys_addr_length,
4214 		    ill->ill_sap,
4215 		    ill->ill_sap_length);
4216 
4217 		if (ill->ill_isv6)
4218 			/*
4219 			 * Note: xresolv interfaces will eventually need NOARP
4220 			 * set here as well, but that will require those
4221 			 * external resolvers to have some knowledge of
4222 			 * that flag and act appropriately. Not to be changed
4223 			 * at present.
4224 			 */
4225 			ill->ill_flags |= ILLF_NONUD;
4226 		else
4227 			ill->ill_flags |= ILLF_NOARP;
4228 
4229 		if (ill->ill_mactype == SUNW_DL_VNI) {
4230 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4231 		} else if (ill->ill_phys_addr_length == 0 ||
4232 		    ill->ill_mactype == DL_IPV4 ||
4233 		    ill->ill_mactype == DL_IPV6) {
4234 			/*
4235 			 * The underying link is point-to-point, so mark the
4236 			 * interface as such.  We can do IP multicast over
4237 			 * such a link since it transmits all network-layer
4238 			 * packets to the remote side the same way.
4239 			 */
4240 			ill->ill_flags |= ILLF_MULTICAST;
4241 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4242 		}
4243 	} else {
4244 		ill->ill_net_type = IRE_IF_RESOLVER;
4245 		if (ill->ill_bcast_mp != NULL)
4246 			freemsg(ill->ill_bcast_mp);
4247 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4248 		    ill->ill_bcast_addr_length, ill->ill_sap,
4249 		    ill->ill_sap_length);
4250 		/*
4251 		 * Later detect lack of DLPI driver multicast
4252 		 * capability by catching DL_ENABMULTI errors in
4253 		 * ip_rput_dlpi.
4254 		 */
4255 		ill->ill_flags |= ILLF_MULTICAST;
4256 		if (!ill->ill_isv6)
4257 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4258 	}
4259 
4260 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4261 	if (ill->ill_mactype == SUNW_DL_IPMP)
4262 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4263 
4264 	/* By default an interface does not support any CoS marking */
4265 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4266 
4267 	/*
4268 	 * If we get QoS information in DL_INFO_ACK, the device supports
4269 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4270 	 */
4271 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4272 	    dlia->dl_qos_length);
4273 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4274 		ill->ill_flags |= ILLF_COS_ENABLED;
4275 	}
4276 
4277 	/* Clear any previous error indication. */
4278 	ill->ill_error = 0;
4279 	freemsg(mp);
4280 }
4281 
4282 /*
4283  * Perform various checks to verify that an address would make sense as a
4284  * local, remote, or subnet interface address.
4285  */
4286 static boolean_t
4287 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4288 {
4289 	ipaddr_t	net_mask;
4290 
4291 	/*
4292 	 * Don't allow all zeroes, or all ones, but allow
4293 	 * all ones netmask.
4294 	 */
4295 	if ((net_mask = ip_net_mask(addr)) == 0)
4296 		return (B_FALSE);
4297 	/* A given netmask overrides the "guess" netmask */
4298 	if (subnet_mask != 0)
4299 		net_mask = subnet_mask;
4300 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4301 	    (addr == (addr | ~net_mask)))) {
4302 		return (B_FALSE);
4303 	}
4304 
4305 	/*
4306 	 * Even if the netmask is all ones, we do not allow address to be
4307 	 * 255.255.255.255
4308 	 */
4309 	if (addr == INADDR_BROADCAST)
4310 		return (B_FALSE);
4311 
4312 	if (CLASSD(addr))
4313 		return (B_FALSE);
4314 
4315 	return (B_TRUE);
4316 }
4317 
4318 #define	V6_IPIF_LINKLOCAL(p)	\
4319 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4320 
4321 /*
4322  * Compare two given ipifs and check if the second one is better than
4323  * the first one using the order of preference (not taking deprecated
4324  * into acount) specified in ipif_lookup_multicast().
4325  */
4326 static boolean_t
4327 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4328 {
4329 	/* Check the least preferred first. */
4330 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4331 		/* If both ipifs are the same, use the first one. */
4332 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4333 			return (B_FALSE);
4334 		else
4335 			return (B_TRUE);
4336 	}
4337 
4338 	/* For IPv6, check for link local address. */
4339 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4340 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4341 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4342 			/* The second one is equal or less preferred. */
4343 			return (B_FALSE);
4344 		} else {
4345 			return (B_TRUE);
4346 		}
4347 	}
4348 
4349 	/* Then check for point to point interface. */
4350 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4351 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4352 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4353 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4354 			return (B_FALSE);
4355 		} else {
4356 			return (B_TRUE);
4357 		}
4358 	}
4359 
4360 	/* old_ipif is a normal interface, so no need to use the new one. */
4361 	return (B_FALSE);
4362 }
4363 
4364 /*
4365  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4366  * The ipif must be up, and its ill must multicast-capable, not
4367  * condemned, not an underlying interface in an IPMP group, and
4368  * not a VNI interface.  Order of preference:
4369  *
4370  * 	1a. normal
4371  * 	1b. normal, but deprecated
4372  * 	2a. point to point
4373  * 	2b. point to point, but deprecated
4374  * 	3a. link local
4375  * 	3b. link local, but deprecated
4376  * 	4. loopback.
4377  */
4378 static ipif_t *
4379 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4380 {
4381 	ill_t			*ill;
4382 	ill_walk_context_t	ctx;
4383 	ipif_t			*ipif;
4384 	ipif_t			*saved_ipif = NULL;
4385 	ipif_t			*dep_ipif = NULL;
4386 
4387 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4388 	if (isv6)
4389 		ill = ILL_START_WALK_V6(&ctx, ipst);
4390 	else
4391 		ill = ILL_START_WALK_V4(&ctx, ipst);
4392 
4393 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4394 		mutex_enter(&ill->ill_lock);
4395 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4396 		    ILL_IS_CONDEMNED(ill) ||
4397 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4398 			mutex_exit(&ill->ill_lock);
4399 			continue;
4400 		}
4401 		for (ipif = ill->ill_ipif; ipif != NULL;
4402 		    ipif = ipif->ipif_next) {
4403 			if (zoneid != ipif->ipif_zoneid &&
4404 			    zoneid != ALL_ZONES &&
4405 			    ipif->ipif_zoneid != ALL_ZONES) {
4406 				continue;
4407 			}
4408 			if (!(ipif->ipif_flags & IPIF_UP) ||
4409 			    IPIF_IS_CONDEMNED(ipif)) {
4410 				continue;
4411 			}
4412 
4413 			/*
4414 			 * Found one candidate.  If it is deprecated,
4415 			 * remember it in dep_ipif.  If it is not deprecated,
4416 			 * remember it in saved_ipif.
4417 			 */
4418 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4419 				if (dep_ipif == NULL) {
4420 					dep_ipif = ipif;
4421 				} else if (ipif_comp_multi(dep_ipif, ipif,
4422 				    isv6)) {
4423 					/*
4424 					 * If the previous dep_ipif does not
4425 					 * belong to the same ill, we've done
4426 					 * a ipif_refhold() on it.  So we need
4427 					 * to release it.
4428 					 */
4429 					if (dep_ipif->ipif_ill != ill)
4430 						ipif_refrele(dep_ipif);
4431 					dep_ipif = ipif;
4432 				}
4433 				continue;
4434 			}
4435 			if (saved_ipif == NULL) {
4436 				saved_ipif = ipif;
4437 			} else {
4438 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4439 					if (saved_ipif->ipif_ill != ill)
4440 						ipif_refrele(saved_ipif);
4441 					saved_ipif = ipif;
4442 				}
4443 			}
4444 		}
4445 		/*
4446 		 * Before going to the next ill, do a ipif_refhold() on the
4447 		 * saved ones.
4448 		 */
4449 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4450 			ipif_refhold_locked(saved_ipif);
4451 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4452 			ipif_refhold_locked(dep_ipif);
4453 		mutex_exit(&ill->ill_lock);
4454 	}
4455 	rw_exit(&ipst->ips_ill_g_lock);
4456 
4457 	/*
4458 	 * If we have only the saved_ipif, return it.  But if we have both
4459 	 * saved_ipif and dep_ipif, check to see which one is better.
4460 	 */
4461 	if (saved_ipif != NULL) {
4462 		if (dep_ipif != NULL) {
4463 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4464 				ipif_refrele(saved_ipif);
4465 				return (dep_ipif);
4466 			} else {
4467 				ipif_refrele(dep_ipif);
4468 				return (saved_ipif);
4469 			}
4470 		}
4471 		return (saved_ipif);
4472 	} else {
4473 		return (dep_ipif);
4474 	}
4475 }
4476 
4477 ill_t *
4478 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4479 {
4480 	ipif_t *ipif;
4481 	ill_t *ill;
4482 
4483 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4484 	if (ipif == NULL)
4485 		return (NULL);
4486 
4487 	ill = ipif->ipif_ill;
4488 	ill_refhold(ill);
4489 	ipif_refrele(ipif);
4490 	return (ill);
4491 }
4492 
4493 /*
4494  * This function is called when an application does not specify an interface
4495  * to be used for multicast traffic (joining a group/sending data).  It
4496  * calls ire_lookup_multi() to look for an interface route for the
4497  * specified multicast group.  Doing this allows the administrator to add
4498  * prefix routes for multicast to indicate which interface to be used for
4499  * multicast traffic in the above scenario.  The route could be for all
4500  * multicast (224.0/4), for a single multicast group (a /32 route) or
4501  * anything in between.  If there is no such multicast route, we just find
4502  * any multicast capable interface and return it.  The returned ipif
4503  * is refhold'ed.
4504  *
4505  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4506  * unicast table. This is used by CGTP.
4507  */
4508 ill_t *
4509 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4510     boolean_t *multirtp, ipaddr_t *setsrcp)
4511 {
4512 	ill_t			*ill;
4513 
4514 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4515 	if (ill != NULL)
4516 		return (ill);
4517 
4518 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4519 }
4520 
4521 /*
4522  * Look for an ipif with the specified interface address and destination.
4523  * The destination address is used only for matching point-to-point interfaces.
4524  */
4525 ipif_t *
4526 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4527 {
4528 	ipif_t	*ipif;
4529 	ill_t	*ill;
4530 	ill_walk_context_t ctx;
4531 
4532 	/*
4533 	 * First match all the point-to-point interfaces
4534 	 * before looking at non-point-to-point interfaces.
4535 	 * This is done to avoid returning non-point-to-point
4536 	 * ipif instead of unnumbered point-to-point ipif.
4537 	 */
4538 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4539 	ill = ILL_START_WALK_V4(&ctx, ipst);
4540 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4541 		mutex_enter(&ill->ill_lock);
4542 		for (ipif = ill->ill_ipif; ipif != NULL;
4543 		    ipif = ipif->ipif_next) {
4544 			/* Allow the ipif to be down */
4545 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4546 			    (ipif->ipif_lcl_addr == if_addr) &&
4547 			    (ipif->ipif_pp_dst_addr == dst)) {
4548 				if (!IPIF_IS_CONDEMNED(ipif)) {
4549 					ipif_refhold_locked(ipif);
4550 					mutex_exit(&ill->ill_lock);
4551 					rw_exit(&ipst->ips_ill_g_lock);
4552 					return (ipif);
4553 				}
4554 			}
4555 		}
4556 		mutex_exit(&ill->ill_lock);
4557 	}
4558 	rw_exit(&ipst->ips_ill_g_lock);
4559 
4560 	/* lookup the ipif based on interface address */
4561 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4562 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4563 	return (ipif);
4564 }
4565 
4566 /*
4567  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4568  */
4569 static ipif_t *
4570 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4571     zoneid_t zoneid, ip_stack_t *ipst)
4572 {
4573 	ipif_t  *ipif;
4574 	ill_t   *ill;
4575 	boolean_t ptp = B_FALSE;
4576 	ill_walk_context_t	ctx;
4577 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4578 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4579 
4580 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4581 	/*
4582 	 * Repeat twice, first based on local addresses and
4583 	 * next time for pointopoint.
4584 	 */
4585 repeat:
4586 	ill = ILL_START_WALK_V4(&ctx, ipst);
4587 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4588 		if (match_ill != NULL && ill != match_ill &&
4589 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4590 			continue;
4591 		}
4592 		mutex_enter(&ill->ill_lock);
4593 		for (ipif = ill->ill_ipif; ipif != NULL;
4594 		    ipif = ipif->ipif_next) {
4595 			if (zoneid != ALL_ZONES &&
4596 			    zoneid != ipif->ipif_zoneid &&
4597 			    ipif->ipif_zoneid != ALL_ZONES)
4598 				continue;
4599 
4600 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4601 				continue;
4602 
4603 			/* Allow the ipif to be down */
4604 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4605 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4606 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4607 			    (ipif->ipif_pp_dst_addr == addr))) {
4608 				if (!IPIF_IS_CONDEMNED(ipif)) {
4609 					ipif_refhold_locked(ipif);
4610 					mutex_exit(&ill->ill_lock);
4611 					rw_exit(&ipst->ips_ill_g_lock);
4612 					return (ipif);
4613 				}
4614 			}
4615 		}
4616 		mutex_exit(&ill->ill_lock);
4617 	}
4618 
4619 	/* If we already did the ptp case, then we are done */
4620 	if (ptp) {
4621 		rw_exit(&ipst->ips_ill_g_lock);
4622 		return (NULL);
4623 	}
4624 	ptp = B_TRUE;
4625 	goto repeat;
4626 }
4627 
4628 /*
4629  * Lookup an ipif with the specified address.  For point-to-point links we
4630  * look for matches on either the destination address or the local address,
4631  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4632  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4633  * (or illgrp if `match_ill' is in an IPMP group).
4634  */
4635 ipif_t *
4636 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4637     ip_stack_t *ipst)
4638 {
4639 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4640 	    zoneid, ipst));
4641 }
4642 
4643 /*
4644  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4645  * except that we will only return an address if it is not marked as
4646  * IPIF_DUPLICATE
4647  */
4648 ipif_t *
4649 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4650     ip_stack_t *ipst)
4651 {
4652 	return (ipif_lookup_addr_common(addr, match_ill,
4653 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4654 	    zoneid, ipst));
4655 }
4656 
4657 /*
4658  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4659  * `match_ill' across the IPMP group.  This function is only needed in some
4660  * corner-cases; almost everything should use ipif_lookup_addr().
4661  */
4662 ipif_t *
4663 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4664 {
4665 	ASSERT(match_ill != NULL);
4666 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4667 	    ipst));
4668 }
4669 
4670 /*
4671  * Look for an ipif with the specified address. For point-point links
4672  * we look for matches on either the destination address and the local
4673  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4674  * is set.
4675  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4676  * ill (or illgrp if `match_ill' is in an IPMP group).
4677  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4678  */
4679 zoneid_t
4680 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4681 {
4682 	zoneid_t zoneid;
4683 	ipif_t  *ipif;
4684 	ill_t   *ill;
4685 	boolean_t ptp = B_FALSE;
4686 	ill_walk_context_t	ctx;
4687 
4688 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4689 	/*
4690 	 * Repeat twice, first based on local addresses and
4691 	 * next time for pointopoint.
4692 	 */
4693 repeat:
4694 	ill = ILL_START_WALK_V4(&ctx, ipst);
4695 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4696 		if (match_ill != NULL && ill != match_ill &&
4697 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4698 			continue;
4699 		}
4700 		mutex_enter(&ill->ill_lock);
4701 		for (ipif = ill->ill_ipif; ipif != NULL;
4702 		    ipif = ipif->ipif_next) {
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 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4709 				zoneid = ipif->ipif_zoneid;
4710 				mutex_exit(&ill->ill_lock);
4711 				rw_exit(&ipst->ips_ill_g_lock);
4712 				/*
4713 				 * If ipif_zoneid was ALL_ZONES then we have
4714 				 * a trusted extensions shared IP address.
4715 				 * In that case GLOBAL_ZONEID works to send.
4716 				 */
4717 				if (zoneid == ALL_ZONES)
4718 					zoneid = GLOBAL_ZONEID;
4719 				return (zoneid);
4720 			}
4721 		}
4722 		mutex_exit(&ill->ill_lock);
4723 	}
4724 
4725 	/* If we already did the ptp case, then we are done */
4726 	if (ptp) {
4727 		rw_exit(&ipst->ips_ill_g_lock);
4728 		return (ALL_ZONES);
4729 	}
4730 	ptp = B_TRUE;
4731 	goto repeat;
4732 }
4733 
4734 /*
4735  * Look for an ipif that matches the specified remote address i.e. the
4736  * ipif that would receive the specified packet.
4737  * First look for directly connected interfaces and then do a recursive
4738  * IRE lookup and pick the first ipif corresponding to the source address in the
4739  * ire.
4740  * Returns: held ipif
4741  *
4742  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4743  */
4744 ipif_t *
4745 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4746 {
4747 	ipif_t	*ipif;
4748 
4749 	ASSERT(!ill->ill_isv6);
4750 
4751 	/*
4752 	 * Someone could be changing this ipif currently or change it
4753 	 * after we return this. Thus  a few packets could use the old
4754 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4755 	 * will atomically be updated or cleaned up with the new value
4756 	 * Thus we don't need a lock to check the flags or other attrs below.
4757 	 */
4758 	mutex_enter(&ill->ill_lock);
4759 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4760 		if (IPIF_IS_CONDEMNED(ipif))
4761 			continue;
4762 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4763 		    ipif->ipif_zoneid != ALL_ZONES)
4764 			continue;
4765 		/* Allow the ipif to be down */
4766 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4767 			if ((ipif->ipif_pp_dst_addr == addr) ||
4768 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4769 			    ipif->ipif_lcl_addr == addr)) {
4770 				ipif_refhold_locked(ipif);
4771 				mutex_exit(&ill->ill_lock);
4772 				return (ipif);
4773 			}
4774 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4775 			ipif_refhold_locked(ipif);
4776 			mutex_exit(&ill->ill_lock);
4777 			return (ipif);
4778 		}
4779 	}
4780 	mutex_exit(&ill->ill_lock);
4781 	/*
4782 	 * For a remote destination it isn't possible to nail down a particular
4783 	 * ipif.
4784 	 */
4785 
4786 	/* Pick the first interface */
4787 	ipif = ipif_get_next_ipif(NULL, ill);
4788 	return (ipif);
4789 }
4790 
4791 /*
4792  * This func does not prevent refcnt from increasing. But if
4793  * the caller has taken steps to that effect, then this func
4794  * can be used to determine whether the ill has become quiescent
4795  */
4796 static boolean_t
4797 ill_is_quiescent(ill_t *ill)
4798 {
4799 	ipif_t	*ipif;
4800 
4801 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4802 
4803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4804 		if (ipif->ipif_refcnt != 0)
4805 			return (B_FALSE);
4806 	}
4807 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4808 		return (B_FALSE);
4809 	}
4810 	return (B_TRUE);
4811 }
4812 
4813 boolean_t
4814 ill_is_freeable(ill_t *ill)
4815 {
4816 	ipif_t	*ipif;
4817 
4818 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4819 
4820 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4821 		if (ipif->ipif_refcnt != 0) {
4822 			return (B_FALSE);
4823 		}
4824 	}
4825 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4826 		return (B_FALSE);
4827 	}
4828 	return (B_TRUE);
4829 }
4830 
4831 /*
4832  * This func does not prevent refcnt from increasing. But if
4833  * the caller has taken steps to that effect, then this func
4834  * can be used to determine whether the ipif has become quiescent
4835  */
4836 static boolean_t
4837 ipif_is_quiescent(ipif_t *ipif)
4838 {
4839 	ill_t *ill;
4840 
4841 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4842 
4843 	if (ipif->ipif_refcnt != 0)
4844 		return (B_FALSE);
4845 
4846 	ill = ipif->ipif_ill;
4847 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4848 	    ill->ill_logical_down) {
4849 		return (B_TRUE);
4850 	}
4851 
4852 	/* This is the last ipif going down or being deleted on this ill */
4853 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4854 		return (B_FALSE);
4855 	}
4856 
4857 	return (B_TRUE);
4858 }
4859 
4860 /*
4861  * return true if the ipif can be destroyed: the ipif has to be quiescent
4862  * with zero references from ire/ilm to it.
4863  */
4864 static boolean_t
4865 ipif_is_freeable(ipif_t *ipif)
4866 {
4867 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4868 	ASSERT(ipif->ipif_id != 0);
4869 	return (ipif->ipif_refcnt == 0);
4870 }
4871 
4872 /*
4873  * The ipif/ill/ire has been refreled. Do the tail processing.
4874  * Determine if the ipif or ill in question has become quiescent and if so
4875  * wakeup close and/or restart any queued pending ioctl that is waiting
4876  * for the ipif_down (or ill_down)
4877  */
4878 void
4879 ipif_ill_refrele_tail(ill_t *ill)
4880 {
4881 	mblk_t	*mp;
4882 	conn_t	*connp;
4883 	ipsq_t	*ipsq;
4884 	ipxop_t	*ipx;
4885 	ipif_t	*ipif;
4886 	dl_notify_ind_t *dlindp;
4887 
4888 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4889 
4890 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4891 		/* ip_modclose() may be waiting */
4892 		cv_broadcast(&ill->ill_cv);
4893 	}
4894 
4895 	ipsq = ill->ill_phyint->phyint_ipsq;
4896 	mutex_enter(&ipsq->ipsq_lock);
4897 	ipx = ipsq->ipsq_xop;
4898 	mutex_enter(&ipx->ipx_lock);
4899 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4900 		goto unlock;
4901 
4902 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4903 
4904 	ipif = ipx->ipx_pending_ipif;
4905 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4906 		goto unlock;
4907 
4908 	switch (ipx->ipx_waitfor) {
4909 	case IPIF_DOWN:
4910 		if (!ipif_is_quiescent(ipif))
4911 			goto unlock;
4912 		break;
4913 	case IPIF_FREE:
4914 		if (!ipif_is_freeable(ipif))
4915 			goto unlock;
4916 		break;
4917 	case ILL_DOWN:
4918 		if (!ill_is_quiescent(ill))
4919 			goto unlock;
4920 		break;
4921 	case ILL_FREE:
4922 		/*
4923 		 * ILL_FREE is only for loopback; normal ill teardown waits
4924 		 * synchronously in ip_modclose() without using ipx_waitfor,
4925 		 * handled by the cv_broadcast() at the top of this function.
4926 		 */
4927 		if (!ill_is_freeable(ill))
4928 			goto unlock;
4929 		break;
4930 	default:
4931 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4932 		    (void *)ipsq, ipx->ipx_waitfor);
4933 	}
4934 
4935 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4936 	mutex_exit(&ipx->ipx_lock);
4937 	mp = ipsq_pending_mp_get(ipsq, &connp);
4938 	mutex_exit(&ipsq->ipsq_lock);
4939 	mutex_exit(&ill->ill_lock);
4940 
4941 	ASSERT(mp != NULL);
4942 	/*
4943 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4944 	 * we can only get here when the current operation decides it
4945 	 * it needs to quiesce via ipsq_pending_mp_add().
4946 	 */
4947 	switch (mp->b_datap->db_type) {
4948 	case M_PCPROTO:
4949 	case M_PROTO:
4950 		/*
4951 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4952 		 */
4953 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4954 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4955 
4956 		switch (dlindp->dl_notification) {
4957 		case DL_NOTE_PHYS_ADDR:
4958 			qwriter_ip(ill, ill->ill_rq, mp,
4959 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4960 			return;
4961 		case DL_NOTE_REPLUMB:
4962 			qwriter_ip(ill, ill->ill_rq, mp,
4963 			    ill_replumb_tail, CUR_OP, B_TRUE);
4964 			return;
4965 		default:
4966 			ASSERT(0);
4967 			ill_refrele(ill);
4968 		}
4969 		break;
4970 
4971 	case M_ERROR:
4972 	case M_HANGUP:
4973 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4974 		    B_TRUE);
4975 		return;
4976 
4977 	case M_IOCTL:
4978 	case M_IOCDATA:
4979 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4980 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4981 		return;
4982 
4983 	default:
4984 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4985 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4986 	}
4987 	return;
4988 unlock:
4989 	mutex_exit(&ipsq->ipsq_lock);
4990 	mutex_exit(&ipx->ipx_lock);
4991 	mutex_exit(&ill->ill_lock);
4992 }
4993 
4994 #ifdef DEBUG
4995 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4996 static void
4997 th_trace_rrecord(th_trace_t *th_trace)
4998 {
4999 	tr_buf_t *tr_buf;
5000 	uint_t lastref;
5001 
5002 	lastref = th_trace->th_trace_lastref;
5003 	lastref++;
5004 	if (lastref == TR_BUF_MAX)
5005 		lastref = 0;
5006 	th_trace->th_trace_lastref = lastref;
5007 	tr_buf = &th_trace->th_trbuf[lastref];
5008 	tr_buf->tr_time = ddi_get_lbolt();
5009 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5010 }
5011 
5012 static void
5013 th_trace_free(void *value)
5014 {
5015 	th_trace_t *th_trace = value;
5016 
5017 	ASSERT(th_trace->th_refcnt == 0);
5018 	kmem_free(th_trace, sizeof (*th_trace));
5019 }
5020 
5021 /*
5022  * Find or create the per-thread hash table used to track object references.
5023  * The ipst argument is NULL if we shouldn't allocate.
5024  *
5025  * Accesses per-thread data, so there's no need to lock here.
5026  */
5027 static mod_hash_t *
5028 th_trace_gethash(ip_stack_t *ipst)
5029 {
5030 	th_hash_t *thh;
5031 
5032 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5033 		mod_hash_t *mh;
5034 		char name[256];
5035 		size_t objsize, rshift;
5036 		int retv;
5037 
5038 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5039 			return (NULL);
5040 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5041 		    (void *)curthread);
5042 
5043 		/*
5044 		 * We use mod_hash_create_extended here rather than the more
5045 		 * obvious mod_hash_create_ptrhash because the latter has a
5046 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5047 		 * block.
5048 		 */
5049 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5050 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5051 		rshift = highbit(objsize);
5052 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5053 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5054 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5055 		if (mh == NULL) {
5056 			kmem_free(thh, sizeof (*thh));
5057 			return (NULL);
5058 		}
5059 		thh->thh_hash = mh;
5060 		thh->thh_ipst = ipst;
5061 		/*
5062 		 * We trace ills, ipifs, ires, and nces.  All of these are
5063 		 * per-IP-stack, so the lock on the thread list is as well.
5064 		 */
5065 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5066 		list_insert_tail(&ip_thread_list, thh);
5067 		rw_exit(&ip_thread_rwlock);
5068 		retv = tsd_set(ip_thread_data, thh);
5069 		ASSERT(retv == 0);
5070 	}
5071 	return (thh != NULL ? thh->thh_hash : NULL);
5072 }
5073 
5074 boolean_t
5075 th_trace_ref(const void *obj, ip_stack_t *ipst)
5076 {
5077 	th_trace_t *th_trace;
5078 	mod_hash_t *mh;
5079 	mod_hash_val_t val;
5080 
5081 	if ((mh = th_trace_gethash(ipst)) == NULL)
5082 		return (B_FALSE);
5083 
5084 	/*
5085 	 * Attempt to locate the trace buffer for this obj and thread.
5086 	 * If it does not exist, then allocate a new trace buffer and
5087 	 * insert into the hash.
5088 	 */
5089 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5090 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5091 		if (th_trace == NULL)
5092 			return (B_FALSE);
5093 
5094 		th_trace->th_id = curthread;
5095 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5096 		    (mod_hash_val_t)th_trace) != 0) {
5097 			kmem_free(th_trace, sizeof (th_trace_t));
5098 			return (B_FALSE);
5099 		}
5100 	} else {
5101 		th_trace = (th_trace_t *)val;
5102 	}
5103 
5104 	ASSERT(th_trace->th_refcnt >= 0 &&
5105 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5106 
5107 	th_trace->th_refcnt++;
5108 	th_trace_rrecord(th_trace);
5109 	return (B_TRUE);
5110 }
5111 
5112 /*
5113  * For the purpose of tracing a reference release, we assume that global
5114  * tracing is always on and that the same thread initiated the reference hold
5115  * is releasing.
5116  */
5117 void
5118 th_trace_unref(const void *obj)
5119 {
5120 	int retv;
5121 	mod_hash_t *mh;
5122 	th_trace_t *th_trace;
5123 	mod_hash_val_t val;
5124 
5125 	mh = th_trace_gethash(NULL);
5126 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5127 	ASSERT(retv == 0);
5128 	th_trace = (th_trace_t *)val;
5129 
5130 	ASSERT(th_trace->th_refcnt > 0);
5131 	th_trace->th_refcnt--;
5132 	th_trace_rrecord(th_trace);
5133 }
5134 
5135 /*
5136  * If tracing has been disabled, then we assume that the reference counts are
5137  * now useless, and we clear them out before destroying the entries.
5138  */
5139 void
5140 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5141 {
5142 	th_hash_t	*thh;
5143 	mod_hash_t	*mh;
5144 	mod_hash_val_t	val;
5145 	th_trace_t	*th_trace;
5146 	int		retv;
5147 
5148 	rw_enter(&ip_thread_rwlock, RW_READER);
5149 	for (thh = list_head(&ip_thread_list); thh != NULL;
5150 	    thh = list_next(&ip_thread_list, thh)) {
5151 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5152 		    &val) == 0) {
5153 			th_trace = (th_trace_t *)val;
5154 			if (trace_disable)
5155 				th_trace->th_refcnt = 0;
5156 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5157 			ASSERT(retv == 0);
5158 		}
5159 	}
5160 	rw_exit(&ip_thread_rwlock);
5161 }
5162 
5163 void
5164 ipif_trace_ref(ipif_t *ipif)
5165 {
5166 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5167 
5168 	if (ipif->ipif_trace_disable)
5169 		return;
5170 
5171 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5172 		ipif->ipif_trace_disable = B_TRUE;
5173 		ipif_trace_cleanup(ipif);
5174 	}
5175 }
5176 
5177 void
5178 ipif_untrace_ref(ipif_t *ipif)
5179 {
5180 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5181 
5182 	if (!ipif->ipif_trace_disable)
5183 		th_trace_unref(ipif);
5184 }
5185 
5186 void
5187 ill_trace_ref(ill_t *ill)
5188 {
5189 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5190 
5191 	if (ill->ill_trace_disable)
5192 		return;
5193 
5194 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5195 		ill->ill_trace_disable = B_TRUE;
5196 		ill_trace_cleanup(ill);
5197 	}
5198 }
5199 
5200 void
5201 ill_untrace_ref(ill_t *ill)
5202 {
5203 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5204 
5205 	if (!ill->ill_trace_disable)
5206 		th_trace_unref(ill);
5207 }
5208 
5209 /*
5210  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5211  * failure, ipif_trace_disable is set.
5212  */
5213 static void
5214 ipif_trace_cleanup(const ipif_t *ipif)
5215 {
5216 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5217 }
5218 
5219 /*
5220  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5221  * failure, ill_trace_disable is set.
5222  */
5223 static void
5224 ill_trace_cleanup(const ill_t *ill)
5225 {
5226 	th_trace_cleanup(ill, ill->ill_trace_disable);
5227 }
5228 #endif /* DEBUG */
5229 
5230 void
5231 ipif_refhold_locked(ipif_t *ipif)
5232 {
5233 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5234 	ipif->ipif_refcnt++;
5235 	IPIF_TRACE_REF(ipif);
5236 }
5237 
5238 void
5239 ipif_refhold(ipif_t *ipif)
5240 {
5241 	ill_t	*ill;
5242 
5243 	ill = ipif->ipif_ill;
5244 	mutex_enter(&ill->ill_lock);
5245 	ipif->ipif_refcnt++;
5246 	IPIF_TRACE_REF(ipif);
5247 	mutex_exit(&ill->ill_lock);
5248 }
5249 
5250 /*
5251  * Must not be called while holding any locks. Otherwise if this is
5252  * the last reference to be released there is a chance of recursive mutex
5253  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5254  * to restart an ioctl.
5255  */
5256 void
5257 ipif_refrele(ipif_t *ipif)
5258 {
5259 	ill_t	*ill;
5260 
5261 	ill = ipif->ipif_ill;
5262 
5263 	mutex_enter(&ill->ill_lock);
5264 	ASSERT(ipif->ipif_refcnt != 0);
5265 	ipif->ipif_refcnt--;
5266 	IPIF_UNTRACE_REF(ipif);
5267 	if (ipif->ipif_refcnt != 0) {
5268 		mutex_exit(&ill->ill_lock);
5269 		return;
5270 	}
5271 
5272 	/* Drops the ill_lock */
5273 	ipif_ill_refrele_tail(ill);
5274 }
5275 
5276 ipif_t *
5277 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5278 {
5279 	ipif_t	*ipif;
5280 
5281 	mutex_enter(&ill->ill_lock);
5282 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5283 	    ipif != NULL; ipif = ipif->ipif_next) {
5284 		if (IPIF_IS_CONDEMNED(ipif))
5285 			continue;
5286 		ipif_refhold_locked(ipif);
5287 		mutex_exit(&ill->ill_lock);
5288 		return (ipif);
5289 	}
5290 	mutex_exit(&ill->ill_lock);
5291 	return (NULL);
5292 }
5293 
5294 /*
5295  * TODO: make this table extendible at run time
5296  * Return a pointer to the mac type info for 'mac_type'
5297  */
5298 static ip_m_t *
5299 ip_m_lookup(t_uscalar_t mac_type)
5300 {
5301 	ip_m_t	*ipm;
5302 
5303 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5304 		if (ipm->ip_m_mac_type == mac_type)
5305 			return (ipm);
5306 	return (NULL);
5307 }
5308 
5309 /*
5310  * Make a link layer address from the multicast IP address *addr.
5311  * To form the link layer address, invoke the ip_m_v*mapping function
5312  * associated with the link-layer type.
5313  */
5314 void
5315 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5316 {
5317 	ip_m_t *ipm;
5318 
5319 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5320 		return;
5321 
5322 	ASSERT(addr != NULL);
5323 
5324 	ipm = ip_m_lookup(ill->ill_mactype);
5325 	if (ipm == NULL ||
5326 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5327 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5328 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5329 		    ill->ill_name, ill->ill_mactype));
5330 		return;
5331 	}
5332 	if (ill->ill_isv6)
5333 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5334 	else
5335 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5336 }
5337 
5338 /*
5339  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5340  * Otherwise returns B_TRUE.
5341  *
5342  * The netmask can be verified to be contiguous with 32 shifts and or
5343  * operations. Take the contiguous mask (in host byte order) and compute
5344  * 	mask | mask << 1 | mask << 2 | ... | mask << 31
5345  * the result will be the same as the 'mask' for contiguous mask.
5346  */
5347 static boolean_t
5348 ip_contiguous_mask(uint32_t mask)
5349 {
5350 	uint32_t	m = mask;
5351 	int		i;
5352 
5353 	for (i = 1; i < 32; i++)
5354 		m |= (mask << i);
5355 
5356 	return (m == mask);
5357 }
5358 
5359 /*
5360  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5361  * ill is passed in to associate it with the correct interface.
5362  * If ire_arg is set, then we return the held IRE in that location.
5363  */
5364 int
5365 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5366     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5367     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5368 {
5369 	ire_t	*ire, *nire;
5370 	ire_t	*gw_ire = NULL;
5371 	ipif_t	*ipif = NULL;
5372 	uint_t	type;
5373 	int	match_flags = MATCH_IRE_TYPE;
5374 	tsol_gc_t *gc = NULL;
5375 	tsol_gcgrp_t *gcgrp = NULL;
5376 	boolean_t gcgrp_xtraref = B_FALSE;
5377 	boolean_t cgtp_broadcast;
5378 	boolean_t unbound = B_FALSE;
5379 
5380 	ip1dbg(("ip_rt_add:"));
5381 
5382 	if (ire_arg != NULL)
5383 		*ire_arg = NULL;
5384 
5385 	/* disallow non-contiguous netmasks */
5386 	if (!ip_contiguous_mask(ntohl(mask)))
5387 		return (ENOTSUP);
5388 
5389 	/*
5390 	 * If this is the case of RTF_HOST being set, then we set the netmask
5391 	 * to all ones (regardless if one was supplied).
5392 	 */
5393 	if (flags & RTF_HOST)
5394 		mask = IP_HOST_MASK;
5395 
5396 	/*
5397 	 * Prevent routes with a zero gateway from being created (since
5398 	 * interfaces can currently be plumbed and brought up no assigned
5399 	 * address).
5400 	 */
5401 	if (gw_addr == 0)
5402 		return (ENETUNREACH);
5403 	/*
5404 	 * Get the ipif, if any, corresponding to the gw_addr
5405 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5406 	 * we match on the gatway and destination to handle unnumbered pt-pt
5407 	 * interfaces.
5408 	 */
5409 	if (ill != NULL)
5410 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5411 	else
5412 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5413 	if (ipif != NULL) {
5414 		if (IS_VNI(ipif->ipif_ill)) {
5415 			ipif_refrele(ipif);
5416 			return (EINVAL);
5417 		}
5418 	}
5419 
5420 	/*
5421 	 * GateD will attempt to create routes with a loopback interface
5422 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5423 	 * these routes to be added, but create them as interface routes
5424 	 * since the gateway is an interface address.
5425 	 */
5426 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5427 		flags &= ~RTF_GATEWAY;
5428 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5429 		    mask == IP_HOST_MASK) {
5430 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5431 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5432 			    NULL);
5433 			if (ire != NULL) {
5434 				ire_refrele(ire);
5435 				ipif_refrele(ipif);
5436 				return (EEXIST);
5437 			}
5438 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5439 			    "for 0x%x\n", (void *)ipif,
5440 			    ipif->ipif_ire_type,
5441 			    ntohl(ipif->ipif_lcl_addr)));
5442 			ire = ire_create(
5443 			    (uchar_t *)&dst_addr,	/* dest address */
5444 			    (uchar_t *)&mask,		/* mask */
5445 			    NULL,			/* no gateway */
5446 			    ipif->ipif_ire_type,	/* LOOPBACK */
5447 			    ipif->ipif_ill,
5448 			    zoneid,
5449 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5450 			    NULL,
5451 			    ipst);
5452 
5453 			if (ire == NULL) {
5454 				ipif_refrele(ipif);
5455 				return (ENOMEM);
5456 			}
5457 			/* src address assigned by the caller? */
5458 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5459 				ire->ire_setsrc_addr = src_addr;
5460 
5461 			nire = ire_add(ire);
5462 			if (nire == NULL) {
5463 				/*
5464 				 * In the result of failure, ire_add() will have
5465 				 * already deleted the ire in question, so there
5466 				 * is no need to do that here.
5467 				 */
5468 				ipif_refrele(ipif);
5469 				return (ENOMEM);
5470 			}
5471 			/*
5472 			 * Check if it was a duplicate entry. This handles
5473 			 * the case of two racing route adds for the same route
5474 			 */
5475 			if (nire != ire) {
5476 				ASSERT(nire->ire_identical_ref > 1);
5477 				ire_delete(nire);
5478 				ire_refrele(nire);
5479 				ipif_refrele(ipif);
5480 				return (EEXIST);
5481 			}
5482 			ire = nire;
5483 			goto save_ire;
5484 		}
5485 	}
5486 
5487 	/*
5488 	 * The routes for multicast with CGTP are quite special in that
5489 	 * the gateway is the local interface address, yet RTF_GATEWAY
5490 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5491 	 * this undocumented and unusual use of multicast routes.
5492 	 */
5493 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5494 		flags &= ~RTF_GATEWAY;
5495 
5496 	/*
5497 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5498 	 * and the gateway address provided is one of the system's interface
5499 	 * addresses.  By using the routing socket interface and supplying an
5500 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5501 	 * specifying an interface route to be created is available which uses
5502 	 * the interface index that specifies the outgoing interface rather than
5503 	 * the address of an outgoing interface (which may not be able to
5504 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5505 	 * flag, routes can be specified which not only specify the next-hop to
5506 	 * be used when routing to a certain prefix, but also which outgoing
5507 	 * interface should be used.
5508 	 *
5509 	 * Previously, interfaces would have unique addresses assigned to them
5510 	 * and so the address assigned to a particular interface could be used
5511 	 * to identify a particular interface.  One exception to this was the
5512 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5513 	 *
5514 	 * With the advent of IPv6 and its link-local addresses, this
5515 	 * restriction was relaxed and interfaces could share addresses between
5516 	 * themselves.  In fact, typically all of the link-local interfaces on
5517 	 * an IPv6 node or router will have the same link-local address.  In
5518 	 * order to differentiate between these interfaces, the use of an
5519 	 * interface index is necessary and this index can be carried inside a
5520 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5521 	 * of using the interface index, however, is that all of the ipif's that
5522 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5523 	 * cannot be used to differentiate between ipif's (or logical
5524 	 * interfaces) that belong to the same ill (physical interface).
5525 	 *
5526 	 * For example, in the following case involving IPv4 interfaces and
5527 	 * logical interfaces
5528 	 *
5529 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5530 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5531 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5532 	 *
5533 	 * the ipif's corresponding to each of these interface routes can be
5534 	 * uniquely identified by the "gateway" (actually interface address).
5535 	 *
5536 	 * In this case involving multiple IPv6 default routes to a particular
5537 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5538 	 * default route is of interest:
5539 	 *
5540 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5541 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5542 	 */
5543 
5544 	/* RTF_GATEWAY not set */
5545 	if (!(flags & RTF_GATEWAY)) {
5546 		if (sp != NULL) {
5547 			ip2dbg(("ip_rt_add: gateway security attributes "
5548 			    "cannot be set with interface route\n"));
5549 			if (ipif != NULL)
5550 				ipif_refrele(ipif);
5551 			return (EINVAL);
5552 		}
5553 
5554 		/*
5555 		 * Whether or not ill (RTA_IFP) is set, we require that
5556 		 * the gateway is one of our local addresses.
5557 		 */
5558 		if (ipif == NULL)
5559 			return (ENETUNREACH);
5560 
5561 		/*
5562 		 * We use MATCH_IRE_ILL here. If the caller specified an
5563 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5564 		 * we use the ill derived from the gateway address.
5565 		 * We can always match the gateway address since we record it
5566 		 * in ire_gateway_addr.
5567 		 * We don't allow RTA_IFP to specify a different ill than the
5568 		 * one matching the ipif to make sure we can delete the route.
5569 		 */
5570 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5571 		if (ill == NULL) {
5572 			ill = ipif->ipif_ill;
5573 		} else if (ill != ipif->ipif_ill) {
5574 			ipif_refrele(ipif);
5575 			return (EINVAL);
5576 		}
5577 
5578 		/*
5579 		 * We check for an existing entry at this point.
5580 		 *
5581 		 * Since a netmask isn't passed in via the ioctl interface
5582 		 * (SIOCADDRT), we don't check for a matching netmask in that
5583 		 * case.
5584 		 */
5585 		if (!ioctl_msg)
5586 			match_flags |= MATCH_IRE_MASK;
5587 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5588 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5589 		    NULL);
5590 		if (ire != NULL) {
5591 			ire_refrele(ire);
5592 			ipif_refrele(ipif);
5593 			return (EEXIST);
5594 		}
5595 
5596 		/*
5597 		 * Some software (for example, GateD and Sun Cluster) attempts
5598 		 * to create (what amount to) IRE_PREFIX routes with the
5599 		 * loopback address as the gateway.  This is primarily done to
5600 		 * set up prefixes with the RTF_REJECT flag set (for example,
5601 		 * when generating aggregate routes.)
5602 		 *
5603 		 * If the IRE type (as defined by ill->ill_net_type) would be
5604 		 * IRE_LOOPBACK, then we map the request into a
5605 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5606 		 * these interface routes, by definition, can only be that.
5607 		 *
5608 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5609 		 * routine, but rather using ire_create() directly.
5610 		 *
5611 		 */
5612 		type = ill->ill_net_type;
5613 		if (type == IRE_LOOPBACK) {
5614 			type = IRE_IF_NORESOLVER;
5615 			flags |= RTF_BLACKHOLE;
5616 		}
5617 
5618 		/*
5619 		 * Create a copy of the IRE_IF_NORESOLVER or
5620 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5621 		 * gateway.
5622 		 */
5623 		ire = ire_create(
5624 		    (uchar_t *)&dst_addr,
5625 		    (uint8_t *)&mask,
5626 		    (uint8_t *)&gw_addr,
5627 		    type,
5628 		    ill,
5629 		    zoneid,
5630 		    flags,
5631 		    NULL,
5632 		    ipst);
5633 		if (ire == NULL) {
5634 			ipif_refrele(ipif);
5635 			return (ENOMEM);
5636 		}
5637 
5638 		/* src address assigned by the caller? */
5639 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5640 			ire->ire_setsrc_addr = src_addr;
5641 
5642 		nire = ire_add(ire);
5643 		if (nire == NULL) {
5644 			/*
5645 			 * In the result of failure, ire_add() will have
5646 			 * already deleted the ire in question, so there
5647 			 * is no need to do that here.
5648 			 */
5649 			ipif_refrele(ipif);
5650 			return (ENOMEM);
5651 		}
5652 		/*
5653 		 * Check if it was a duplicate entry. This handles
5654 		 * the case of two racing route adds for the same route
5655 		 */
5656 		if (nire != ire) {
5657 			ire_delete(nire);
5658 			ire_refrele(nire);
5659 			ipif_refrele(ipif);
5660 			return (EEXIST);
5661 		}
5662 		ire = nire;
5663 		goto save_ire;
5664 	}
5665 
5666 	/*
5667 	 * Get an interface IRE for the specified gateway.
5668 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5669 	 * gateway, it is currently unreachable and we fail the request
5670 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5671 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5672 	 * If RTA_IFP was specified we look on that particular ill.
5673 	 */
5674 	if (ill != NULL)
5675 		match_flags |= MATCH_IRE_ILL;
5676 
5677 	/* Check whether the gateway is reachable. */
5678 again:
5679 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5680 	if (flags & RTF_INDIRECT)
5681 		type |= IRE_OFFLINK;
5682 
5683 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5684 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5685 	if (gw_ire == NULL) {
5686 		/*
5687 		 * With IPMP, we allow host routes to influence in.mpathd's
5688 		 * target selection.  However, if the test addresses are on
5689 		 * their own network, the above lookup will fail since the
5690 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5691 		 * hidden test IREs to be found and try again.
5692 		 */
5693 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5694 			match_flags |= MATCH_IRE_TESTHIDDEN;
5695 			goto again;
5696 		}
5697 		if (ipif != NULL)
5698 			ipif_refrele(ipif);
5699 		return (ENETUNREACH);
5700 	}
5701 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5702 		ire_refrele(gw_ire);
5703 		if (ipif != NULL)
5704 			ipif_refrele(ipif);
5705 		return (ENETUNREACH);
5706 	}
5707 
5708 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5709 		unbound = B_TRUE;
5710 		if (ipst->ips_ip_strict_src_multihoming > 0)
5711 			ill = gw_ire->ire_ill;
5712 	}
5713 
5714 	/*
5715 	 * We create one of three types of IREs as a result of this request
5716 	 * based on the netmask.  A netmask of all ones (which is automatically
5717 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5718 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5719 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5720 	 * destination prefix.
5721 	 */
5722 	if (mask == IP_HOST_MASK)
5723 		type = IRE_HOST;
5724 	else if (mask == 0)
5725 		type = IRE_DEFAULT;
5726 	else
5727 		type = IRE_PREFIX;
5728 
5729 	/* check for a duplicate entry */
5730 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5731 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5732 	    0, ipst, NULL);
5733 	if (ire != NULL) {
5734 		if (ipif != NULL)
5735 			ipif_refrele(ipif);
5736 		ire_refrele(gw_ire);
5737 		ire_refrele(ire);
5738 		return (EEXIST);
5739 	}
5740 
5741 	/* Security attribute exists */
5742 	if (sp != NULL) {
5743 		tsol_gcgrp_addr_t ga;
5744 
5745 		/* find or create the gateway credentials group */
5746 		ga.ga_af = AF_INET;
5747 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5748 
5749 		/* we hold reference to it upon success */
5750 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5751 		if (gcgrp == NULL) {
5752 			if (ipif != NULL)
5753 				ipif_refrele(ipif);
5754 			ire_refrele(gw_ire);
5755 			return (ENOMEM);
5756 		}
5757 
5758 		/*
5759 		 * Create and add the security attribute to the group; a
5760 		 * reference to the group is made upon allocating a new
5761 		 * entry successfully.  If it finds an already-existing
5762 		 * entry for the security attribute in the group, it simply
5763 		 * returns it and no new reference is made to the group.
5764 		 */
5765 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5766 		if (gc == NULL) {
5767 			if (ipif != NULL)
5768 				ipif_refrele(ipif);
5769 			/* release reference held by gcgrp_lookup */
5770 			GCGRP_REFRELE(gcgrp);
5771 			ire_refrele(gw_ire);
5772 			return (ENOMEM);
5773 		}
5774 	}
5775 
5776 	/* Create the IRE. */
5777 	ire = ire_create(
5778 	    (uchar_t *)&dst_addr,		/* dest address */
5779 	    (uchar_t *)&mask,			/* mask */
5780 	    (uchar_t *)&gw_addr,		/* gateway address */
5781 	    (ushort_t)type,			/* IRE type */
5782 	    ill,
5783 	    zoneid,
5784 	    flags,
5785 	    gc,					/* security attribute */
5786 	    ipst);
5787 
5788 	/*
5789 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5790 	 * reference to the 'gcgrp'. We can now release the extra reference
5791 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5792 	 */
5793 	if (gcgrp_xtraref)
5794 		GCGRP_REFRELE(gcgrp);
5795 	if (ire == NULL) {
5796 		if (gc != NULL)
5797 			GC_REFRELE(gc);
5798 		if (ipif != NULL)
5799 			ipif_refrele(ipif);
5800 		ire_refrele(gw_ire);
5801 		return (ENOMEM);
5802 	}
5803 
5804 	/* Before we add, check if an extra CGTP broadcast is needed */
5805 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5806 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5807 
5808 	/* src address assigned by the caller? */
5809 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5810 		ire->ire_setsrc_addr = src_addr;
5811 
5812 	ire->ire_unbound = unbound;
5813 
5814 	/*
5815 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5816 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5817 	 */
5818 
5819 	/* Add the new IRE. */
5820 	nire = ire_add(ire);
5821 	if (nire == NULL) {
5822 		/*
5823 		 * In the result of failure, ire_add() will have
5824 		 * already deleted the ire in question, so there
5825 		 * is no need to do that here.
5826 		 */
5827 		if (ipif != NULL)
5828 			ipif_refrele(ipif);
5829 		ire_refrele(gw_ire);
5830 		return (ENOMEM);
5831 	}
5832 	/*
5833 	 * Check if it was a duplicate entry. This handles
5834 	 * the case of two racing route adds for the same route
5835 	 */
5836 	if (nire != ire) {
5837 		ire_delete(nire);
5838 		ire_refrele(nire);
5839 		if (ipif != NULL)
5840 			ipif_refrele(ipif);
5841 		ire_refrele(gw_ire);
5842 		return (EEXIST);
5843 	}
5844 	ire = nire;
5845 
5846 	if (flags & RTF_MULTIRT) {
5847 		/*
5848 		 * Invoke the CGTP (multirouting) filtering module
5849 		 * to add the dst address in the filtering database.
5850 		 * Replicated inbound packets coming from that address
5851 		 * will be filtered to discard the duplicates.
5852 		 * It is not necessary to call the CGTP filter hook
5853 		 * when the dst address is a broadcast or multicast,
5854 		 * because an IP source address cannot be a broadcast
5855 		 * or a multicast.
5856 		 */
5857 		if (cgtp_broadcast) {
5858 			ip_cgtp_bcast_add(ire, ipst);
5859 			goto save_ire;
5860 		}
5861 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5862 		    !CLASSD(ire->ire_addr)) {
5863 			int res;
5864 			ipif_t *src_ipif;
5865 
5866 			/* Find the source address corresponding to gw_ire */
5867 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5868 			    NULL, zoneid, ipst);
5869 			if (src_ipif != NULL) {
5870 				res = ipst->ips_ip_cgtp_filter_ops->
5871 				    cfo_add_dest_v4(
5872 				    ipst->ips_netstack->netstack_stackid,
5873 				    ire->ire_addr,
5874 				    ire->ire_gateway_addr,
5875 				    ire->ire_setsrc_addr,
5876 				    src_ipif->ipif_lcl_addr);
5877 				ipif_refrele(src_ipif);
5878 			} else {
5879 				res = EADDRNOTAVAIL;
5880 			}
5881 			if (res != 0) {
5882 				if (ipif != NULL)
5883 					ipif_refrele(ipif);
5884 				ire_refrele(gw_ire);
5885 				ire_delete(ire);
5886 				ire_refrele(ire);	/* Held in ire_add */
5887 				return (res);
5888 			}
5889 		}
5890 	}
5891 
5892 save_ire:
5893 	if (gw_ire != NULL) {
5894 		ire_refrele(gw_ire);
5895 		gw_ire = NULL;
5896 	}
5897 	if (ill != NULL) {
5898 		/*
5899 		 * Save enough information so that we can recreate the IRE if
5900 		 * the interface goes down and then up.  The metrics associated
5901 		 * with the route will be saved as well when rts_setmetrics() is
5902 		 * called after the IRE has been created.  In the case where
5903 		 * memory cannot be allocated, none of this information will be
5904 		 * saved.
5905 		 */
5906 		ill_save_ire(ill, ire);
5907 	}
5908 	if (ioctl_msg)
5909 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5910 	if (ire_arg != NULL) {
5911 		/*
5912 		 * Store the ire that was successfully added into where ire_arg
5913 		 * points to so that callers don't have to look it up
5914 		 * themselves (but they are responsible for ire_refrele()ing
5915 		 * the ire when they are finished with it).
5916 		 */
5917 		*ire_arg = ire;
5918 	} else {
5919 		ire_refrele(ire);		/* Held in ire_add */
5920 	}
5921 	if (ipif != NULL)
5922 		ipif_refrele(ipif);
5923 	return (0);
5924 }
5925 
5926 /*
5927  * ip_rt_delete is called to delete an IPv4 route.
5928  * ill is passed in to associate it with the correct interface.
5929  */
5930 /* ARGSUSED4 */
5931 int
5932 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5933     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5934     ip_stack_t *ipst, zoneid_t zoneid)
5935 {
5936 	ire_t	*ire = NULL;
5937 	ipif_t	*ipif;
5938 	uint_t	type;
5939 	uint_t	match_flags = MATCH_IRE_TYPE;
5940 	int	err = 0;
5941 
5942 	ip1dbg(("ip_rt_delete:"));
5943 	/*
5944 	 * If this is the case of RTF_HOST being set, then we set the netmask
5945 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5946 	 */
5947 	if (flags & RTF_HOST) {
5948 		mask = IP_HOST_MASK;
5949 		match_flags |= MATCH_IRE_MASK;
5950 	} else if (rtm_addrs & RTA_NETMASK) {
5951 		match_flags |= MATCH_IRE_MASK;
5952 	}
5953 
5954 	/*
5955 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5956 	 * we check if the gateway address is one of our interfaces first,
5957 	 * and fall back on RTF_GATEWAY routes.
5958 	 *
5959 	 * This makes it possible to delete an original
5960 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5961 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5962 	 * and those can not be deleted here.
5963 	 *
5964 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5965 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5966 	 * otherwise we use the ill derived from the gateway address.
5967 	 * We can always match the gateway address since we record it
5968 	 * in ire_gateway_addr.
5969 	 *
5970 	 * For more detail on specifying routes by gateway address and by
5971 	 * interface index, see the comments in ip_rt_add().
5972 	 */
5973 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5974 	if (ipif != NULL) {
5975 		ill_t	*ill_match;
5976 
5977 		if (ill != NULL)
5978 			ill_match = ill;
5979 		else
5980 			ill_match = ipif->ipif_ill;
5981 
5982 		match_flags |= MATCH_IRE_ILL;
5983 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5984 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
5985 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
5986 			    match_flags, 0, ipst, NULL);
5987 		}
5988 		if (ire == NULL) {
5989 			match_flags |= MATCH_IRE_GW;
5990 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5991 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5992 			    match_flags, 0, ipst, NULL);
5993 		}
5994 		/* Avoid deleting routes created by kernel from an ipif */
5995 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5996 			ire_refrele(ire);
5997 			ire = NULL;
5998 		}
5999 
6000 		/* Restore in case we didn't find a match */
6001 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6002 	}
6003 
6004 	if (ire == NULL) {
6005 		/*
6006 		 * At this point, the gateway address is not one of our own
6007 		 * addresses or a matching interface route was not found.  We
6008 		 * set the IRE type to lookup based on whether
6009 		 * this is a host route, a default route or just a prefix.
6010 		 *
6011 		 * If an ill was passed in, then the lookup is based on an
6012 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6013 		 */
6014 		match_flags |= MATCH_IRE_GW;
6015 		if (ill != NULL)
6016 			match_flags |= MATCH_IRE_ILL;
6017 		if (mask == IP_HOST_MASK)
6018 			type = IRE_HOST;
6019 		else if (mask == 0)
6020 			type = IRE_DEFAULT;
6021 		else
6022 			type = IRE_PREFIX;
6023 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6024 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6025 	}
6026 
6027 	if (ipif != NULL) {
6028 		ipif_refrele(ipif);
6029 		ipif = NULL;
6030 	}
6031 
6032 	if (ire == NULL)
6033 		return (ESRCH);
6034 
6035 	if (ire->ire_flags & RTF_MULTIRT) {
6036 		/*
6037 		 * Invoke the CGTP (multirouting) filtering module
6038 		 * to remove the dst address from the filtering database.
6039 		 * Packets coming from that address will no longer be
6040 		 * filtered to remove duplicates.
6041 		 */
6042 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6043 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6044 			    ipst->ips_netstack->netstack_stackid,
6045 			    ire->ire_addr, ire->ire_gateway_addr);
6046 		}
6047 		ip_cgtp_bcast_delete(ire, ipst);
6048 	}
6049 
6050 	ill = ire->ire_ill;
6051 	if (ill != NULL)
6052 		ill_remove_saved_ire(ill, ire);
6053 	if (ioctl_msg)
6054 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6055 	ire_delete(ire);
6056 	ire_refrele(ire);
6057 	return (err);
6058 }
6059 
6060 /*
6061  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6062  */
6063 /* ARGSUSED */
6064 int
6065 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6066     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6067 {
6068 	ipaddr_t dst_addr;
6069 	ipaddr_t gw_addr;
6070 	ipaddr_t mask;
6071 	int error = 0;
6072 	mblk_t *mp1;
6073 	struct rtentry *rt;
6074 	ipif_t *ipif = NULL;
6075 	ip_stack_t	*ipst;
6076 
6077 	ASSERT(q->q_next == NULL);
6078 	ipst = CONNQ_TO_IPST(q);
6079 
6080 	ip1dbg(("ip_siocaddrt:"));
6081 	/* Existence of mp1 verified in ip_wput_nondata */
6082 	mp1 = mp->b_cont->b_cont;
6083 	rt = (struct rtentry *)mp1->b_rptr;
6084 
6085 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6086 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6087 
6088 	/*
6089 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6090 	 * to a particular host address.  In this case, we set the netmask to
6091 	 * all ones for the particular destination address.  Otherwise,
6092 	 * determine the netmask to be used based on dst_addr and the interfaces
6093 	 * in use.
6094 	 */
6095 	if (rt->rt_flags & RTF_HOST) {
6096 		mask = IP_HOST_MASK;
6097 	} else {
6098 		/*
6099 		 * Note that ip_subnet_mask returns a zero mask in the case of
6100 		 * default (an all-zeroes address).
6101 		 */
6102 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6103 	}
6104 
6105 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6106 	    B_TRUE, NULL, ipst, ALL_ZONES);
6107 	if (ipif != NULL)
6108 		ipif_refrele(ipif);
6109 	return (error);
6110 }
6111 
6112 /*
6113  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6114  */
6115 /* ARGSUSED */
6116 int
6117 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6118     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6119 {
6120 	ipaddr_t dst_addr;
6121 	ipaddr_t gw_addr;
6122 	ipaddr_t mask;
6123 	int error;
6124 	mblk_t *mp1;
6125 	struct rtentry *rt;
6126 	ipif_t *ipif = NULL;
6127 	ip_stack_t	*ipst;
6128 
6129 	ASSERT(q->q_next == NULL);
6130 	ipst = CONNQ_TO_IPST(q);
6131 
6132 	ip1dbg(("ip_siocdelrt:"));
6133 	/* Existence of mp1 verified in ip_wput_nondata */
6134 	mp1 = mp->b_cont->b_cont;
6135 	rt = (struct rtentry *)mp1->b_rptr;
6136 
6137 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6138 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6139 
6140 	/*
6141 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6142 	 * to a particular host address.  In this case, we set the netmask to
6143 	 * all ones for the particular destination address.  Otherwise,
6144 	 * determine the netmask to be used based on dst_addr and the interfaces
6145 	 * in use.
6146 	 */
6147 	if (rt->rt_flags & RTF_HOST) {
6148 		mask = IP_HOST_MASK;
6149 	} else {
6150 		/*
6151 		 * Note that ip_subnet_mask returns a zero mask in the case of
6152 		 * default (an all-zeroes address).
6153 		 */
6154 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6155 	}
6156 
6157 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6158 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6159 	    ipst, ALL_ZONES);
6160 	if (ipif != NULL)
6161 		ipif_refrele(ipif);
6162 	return (error);
6163 }
6164 
6165 /*
6166  * Enqueue the mp onto the ipsq, chained by b_next.
6167  * b_prev stores the function to be executed later, and b_queue the queue
6168  * where this mp originated.
6169  */
6170 void
6171 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6172     ill_t *pending_ill)
6173 {
6174 	conn_t	*connp;
6175 	ipxop_t *ipx = ipsq->ipsq_xop;
6176 
6177 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6178 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6179 	ASSERT(func != NULL);
6180 
6181 	mp->b_queue = q;
6182 	mp->b_prev = (void *)func;
6183 	mp->b_next = NULL;
6184 
6185 	switch (type) {
6186 	case CUR_OP:
6187 		if (ipx->ipx_mptail != NULL) {
6188 			ASSERT(ipx->ipx_mphead != NULL);
6189 			ipx->ipx_mptail->b_next = mp;
6190 		} else {
6191 			ASSERT(ipx->ipx_mphead == NULL);
6192 			ipx->ipx_mphead = mp;
6193 		}
6194 		ipx->ipx_mptail = mp;
6195 		break;
6196 
6197 	case NEW_OP:
6198 		if (ipsq->ipsq_xopq_mptail != NULL) {
6199 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6200 			ipsq->ipsq_xopq_mptail->b_next = mp;
6201 		} else {
6202 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6203 			ipsq->ipsq_xopq_mphead = mp;
6204 		}
6205 		ipsq->ipsq_xopq_mptail = mp;
6206 		ipx->ipx_ipsq_queued = B_TRUE;
6207 		break;
6208 
6209 	case SWITCH_OP:
6210 		ASSERT(ipsq->ipsq_swxop != NULL);
6211 		/* only one switch operation is currently allowed */
6212 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6213 		ipsq->ipsq_switch_mp = mp;
6214 		ipx->ipx_ipsq_queued = B_TRUE;
6215 		break;
6216 	default:
6217 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6218 	}
6219 
6220 	if (CONN_Q(q) && pending_ill != NULL) {
6221 		connp = Q_TO_CONN(q);
6222 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6223 		connp->conn_oper_pending_ill = pending_ill;
6224 	}
6225 }
6226 
6227 /*
6228  * Dequeue the next message that requested exclusive access to this IPSQ's
6229  * xop.  Specifically:
6230  *
6231  *  1. If we're still processing the current operation on `ipsq', then
6232  *     dequeue the next message for the operation (from ipx_mphead), or
6233  *     return NULL if there are no queued messages for the operation.
6234  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6235  *
6236  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6237  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6238  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6239  *     leaving IPMP groups and require a careful dance -- see the comments
6240  *     in-line below for details.  If we're leaving a group xop or if we're
6241  *     joining a group xop and become writer on it, then we proceed to (3).
6242  *     Otherwise, we return NULL and exit the xop.
6243  *
6244  *  3. For each IPSQ in the xop, return any switch operation stored on
6245  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6246  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6247  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6248  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6249  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6250  *     each phyint in the group, including the IPMP meta-interface phyint.
6251  */
6252 static mblk_t *
6253 ipsq_dq(ipsq_t *ipsq)
6254 {
6255 	ill_t	*illv4, *illv6;
6256 	mblk_t	*mp;
6257 	ipsq_t	*xopipsq;
6258 	ipsq_t	*leftipsq = NULL;
6259 	ipxop_t *ipx;
6260 	phyint_t *phyi = ipsq->ipsq_phyint;
6261 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6262 	boolean_t emptied = B_FALSE;
6263 
6264 	/*
6265 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6266 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6267 	 */
6268 	rw_enter(&ipst->ips_ill_g_lock,
6269 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6270 	mutex_enter(&ipsq->ipsq_lock);
6271 	ipx = ipsq->ipsq_xop;
6272 	mutex_enter(&ipx->ipx_lock);
6273 
6274 	/*
6275 	 * Dequeue the next message associated with the current exclusive
6276 	 * operation, if any.
6277 	 */
6278 	if ((mp = ipx->ipx_mphead) != NULL) {
6279 		ipx->ipx_mphead = mp->b_next;
6280 		if (ipx->ipx_mphead == NULL)
6281 			ipx->ipx_mptail = NULL;
6282 		mp->b_next = (void *)ipsq;
6283 		goto out;
6284 	}
6285 
6286 	if (ipx->ipx_current_ipif != NULL)
6287 		goto empty;
6288 
6289 	if (ipsq->ipsq_swxop != NULL) {
6290 		/*
6291 		 * The exclusive operation that is now being completed has
6292 		 * requested a switch to a different xop.  This happens
6293 		 * when an interface joins or leaves an IPMP group.  Joins
6294 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6295 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6296 		 * (phyint_free()), or interface plumb for an ill type
6297 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6298 		 *
6299 		 * Xop switches are not allowed on the IPMP meta-interface.
6300 		 */
6301 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6302 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6303 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6304 
6305 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6306 			/*
6307 			 * We're switching back to our own xop, so we have two
6308 			 * xop's to drain/exit: our own, and the group xop
6309 			 * that we are leaving.
6310 			 *
6311 			 * First, pull ourselves out of the group ipsq list.
6312 			 * This is safe since we're writer on ill_g_lock.
6313 			 */
6314 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6315 
6316 			xopipsq = ipx->ipx_ipsq;
6317 			while (xopipsq->ipsq_next != ipsq)
6318 				xopipsq = xopipsq->ipsq_next;
6319 
6320 			xopipsq->ipsq_next = ipsq->ipsq_next;
6321 			ipsq->ipsq_next = ipsq;
6322 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6323 			ipsq->ipsq_swxop = NULL;
6324 
6325 			/*
6326 			 * Second, prepare to exit the group xop.  The actual
6327 			 * ipsq_exit() is done at the end of this function
6328 			 * since we cannot hold any locks across ipsq_exit().
6329 			 * Note that although we drop the group's ipx_lock, no
6330 			 * threads can proceed since we're still ipx_writer.
6331 			 */
6332 			leftipsq = xopipsq;
6333 			mutex_exit(&ipx->ipx_lock);
6334 
6335 			/*
6336 			 * Third, set ipx to point to our own xop (which was
6337 			 * inactive and therefore can be entered).
6338 			 */
6339 			ipx = ipsq->ipsq_xop;
6340 			mutex_enter(&ipx->ipx_lock);
6341 			ASSERT(ipx->ipx_writer == NULL);
6342 			ASSERT(ipx->ipx_current_ipif == NULL);
6343 		} else {
6344 			/*
6345 			 * We're switching from our own xop to a group xop.
6346 			 * The requestor of the switch must ensure that the
6347 			 * group xop cannot go away (e.g. by ensuring the
6348 			 * phyint associated with the xop cannot go away).
6349 			 *
6350 			 * If we can become writer on our new xop, then we'll
6351 			 * do the drain.  Otherwise, the current writer of our
6352 			 * new xop will do the drain when it exits.
6353 			 *
6354 			 * First, splice ourselves into the group IPSQ list.
6355 			 * This is safe since we're writer on ill_g_lock.
6356 			 */
6357 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6358 
6359 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6360 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6361 				xopipsq = xopipsq->ipsq_next;
6362 
6363 			xopipsq->ipsq_next = ipsq;
6364 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6365 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6366 			ipsq->ipsq_swxop = NULL;
6367 
6368 			/*
6369 			 * Second, exit our own xop, since it's now unused.
6370 			 * This is safe since we've got the only reference.
6371 			 */
6372 			ASSERT(ipx->ipx_writer == curthread);
6373 			ipx->ipx_writer = NULL;
6374 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6375 			ipx->ipx_ipsq_queued = B_FALSE;
6376 			mutex_exit(&ipx->ipx_lock);
6377 
6378 			/*
6379 			 * Third, set ipx to point to our new xop, and check
6380 			 * if we can become writer on it.  If we cannot, then
6381 			 * the current writer will drain the IPSQ group when
6382 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6383 			 * because we're still holding ipsq_lock.
6384 			 */
6385 			ipx = ipsq->ipsq_xop;
6386 			mutex_enter(&ipx->ipx_lock);
6387 			if (ipx->ipx_writer != NULL ||
6388 			    ipx->ipx_current_ipif != NULL) {
6389 				goto out;
6390 			}
6391 		}
6392 
6393 		/*
6394 		 * Fourth, become writer on our new ipx before we continue
6395 		 * with the drain.  Note that we never dropped ipsq_lock
6396 		 * above, so no other thread could've raced with us to
6397 		 * become writer first.  Also, we're holding ipx_lock, so
6398 		 * no other thread can examine the ipx right now.
6399 		 */
6400 		ASSERT(ipx->ipx_current_ipif == NULL);
6401 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6402 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6403 		ipx->ipx_writer = curthread;
6404 		ipx->ipx_forced = B_FALSE;
6405 #ifdef DEBUG
6406 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6407 #endif
6408 	}
6409 
6410 	xopipsq = ipsq;
6411 	do {
6412 		/*
6413 		 * So that other operations operate on a consistent and
6414 		 * complete phyint, a switch message on an IPSQ must be
6415 		 * handled prior to any other operations on that IPSQ.
6416 		 */
6417 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6418 			xopipsq->ipsq_switch_mp = NULL;
6419 			ASSERT(mp->b_next == NULL);
6420 			mp->b_next = (void *)xopipsq;
6421 			goto out;
6422 		}
6423 
6424 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6425 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6426 			if (xopipsq->ipsq_xopq_mphead == NULL)
6427 				xopipsq->ipsq_xopq_mptail = NULL;
6428 			mp->b_next = (void *)xopipsq;
6429 			goto out;
6430 		}
6431 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6432 empty:
6433 	/*
6434 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6435 	 * new messages can end up on any IPSQ in the xop.
6436 	 */
6437 	ipx->ipx_writer = NULL;
6438 	ipx->ipx_forced = B_FALSE;
6439 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6440 	ipx->ipx_ipsq_queued = B_FALSE;
6441 	emptied = B_TRUE;
6442 #ifdef	DEBUG
6443 	ipx->ipx_depth = 0;
6444 #endif
6445 out:
6446 	mutex_exit(&ipx->ipx_lock);
6447 	mutex_exit(&ipsq->ipsq_lock);
6448 
6449 	/*
6450 	 * If we completely emptied the xop, then wake up any threads waiting
6451 	 * to enter any of the IPSQ's associated with it.
6452 	 */
6453 	if (emptied) {
6454 		xopipsq = ipsq;
6455 		do {
6456 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6457 				continue;
6458 
6459 			illv4 = phyi->phyint_illv4;
6460 			illv6 = phyi->phyint_illv6;
6461 
6462 			GRAB_ILL_LOCKS(illv4, illv6);
6463 			if (illv4 != NULL)
6464 				cv_broadcast(&illv4->ill_cv);
6465 			if (illv6 != NULL)
6466 				cv_broadcast(&illv6->ill_cv);
6467 			RELEASE_ILL_LOCKS(illv4, illv6);
6468 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6469 	}
6470 	rw_exit(&ipst->ips_ill_g_lock);
6471 
6472 	/*
6473 	 * Now that all locks are dropped, exit the IPSQ we left.
6474 	 */
6475 	if (leftipsq != NULL)
6476 		ipsq_exit(leftipsq);
6477 
6478 	return (mp);
6479 }
6480 
6481 /*
6482  * Return completion status of previously initiated DLPI operations on
6483  * ills in the purview of an ipsq.
6484  */
6485 static boolean_t
6486 ipsq_dlpi_done(ipsq_t *ipsq)
6487 {
6488 	ipsq_t		*ipsq_start;
6489 	phyint_t	*phyi;
6490 	ill_t		*ill;
6491 
6492 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6493 	ipsq_start = ipsq;
6494 
6495 	do {
6496 		/*
6497 		 * The only current users of this function are ipsq_try_enter
6498 		 * and ipsq_enter which have made sure that ipsq_writer is
6499 		 * NULL before we reach here. ill_dlpi_pending is modified
6500 		 * only by an ipsq writer
6501 		 */
6502 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6503 		phyi = ipsq->ipsq_phyint;
6504 		/*
6505 		 * phyi could be NULL if a phyint that is part of an
6506 		 * IPMP group is being unplumbed. A more detailed
6507 		 * comment is in ipmp_grp_update_kstats()
6508 		 */
6509 		if (phyi != NULL) {
6510 			ill = phyi->phyint_illv4;
6511 			if (ill != NULL &&
6512 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6513 			    ill->ill_arl_dlpi_pending))
6514 				return (B_FALSE);
6515 
6516 			ill = phyi->phyint_illv6;
6517 			if (ill != NULL &&
6518 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6519 				return (B_FALSE);
6520 		}
6521 
6522 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6523 
6524 	return (B_TRUE);
6525 }
6526 
6527 /*
6528  * Enter the ipsq corresponding to ill, by waiting synchronously till
6529  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6530  * will have to drain completely before ipsq_enter returns success.
6531  * ipx_current_ipif will be set if some exclusive op is in progress,
6532  * and the ipsq_exit logic will start the next enqueued op after
6533  * completion of the current op. If 'force' is used, we don't wait
6534  * for the enqueued ops. This is needed when a conn_close wants to
6535  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6536  * of an ill can also use this option. But we dont' use it currently.
6537  */
6538 #define	ENTER_SQ_WAIT_TICKS 100
6539 boolean_t
6540 ipsq_enter(ill_t *ill, boolean_t force, int type)
6541 {
6542 	ipsq_t	*ipsq;
6543 	ipxop_t *ipx;
6544 	boolean_t waited_enough = B_FALSE;
6545 	ip_stack_t *ipst = ill->ill_ipst;
6546 
6547 	/*
6548 	 * Note that the relationship between ill and ipsq is fixed as long as
6549 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6550 	 * relationship between the IPSQ and xop cannot change.  However,
6551 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6552 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6553 	 * waking up all ills in the xop when it becomes available.
6554 	 */
6555 	for (;;) {
6556 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6557 		mutex_enter(&ill->ill_lock);
6558 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6559 			mutex_exit(&ill->ill_lock);
6560 			rw_exit(&ipst->ips_ill_g_lock);
6561 			return (B_FALSE);
6562 		}
6563 
6564 		ipsq = ill->ill_phyint->phyint_ipsq;
6565 		mutex_enter(&ipsq->ipsq_lock);
6566 		ipx = ipsq->ipsq_xop;
6567 		mutex_enter(&ipx->ipx_lock);
6568 
6569 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6570 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6571 		    waited_enough))
6572 			break;
6573 
6574 		rw_exit(&ipst->ips_ill_g_lock);
6575 
6576 		if (!force || ipx->ipx_writer != NULL) {
6577 			mutex_exit(&ipx->ipx_lock);
6578 			mutex_exit(&ipsq->ipsq_lock);
6579 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6580 		} else {
6581 			mutex_exit(&ipx->ipx_lock);
6582 			mutex_exit(&ipsq->ipsq_lock);
6583 			(void) cv_reltimedwait(&ill->ill_cv,
6584 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6585 			waited_enough = B_TRUE;
6586 		}
6587 		mutex_exit(&ill->ill_lock);
6588 	}
6589 
6590 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6591 	ASSERT(ipx->ipx_reentry_cnt == 0);
6592 	ipx->ipx_writer = curthread;
6593 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6594 	ipx->ipx_reentry_cnt++;
6595 #ifdef DEBUG
6596 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6597 #endif
6598 	mutex_exit(&ipx->ipx_lock);
6599 	mutex_exit(&ipsq->ipsq_lock);
6600 	mutex_exit(&ill->ill_lock);
6601 	rw_exit(&ipst->ips_ill_g_lock);
6602 
6603 	return (B_TRUE);
6604 }
6605 
6606 /*
6607  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6608  * across the call to the core interface ipsq_try_enter() and hence calls this
6609  * function directly. This is explained more fully in ipif_set_values().
6610  * In order to support the above constraint, ipsq_try_enter is implemented as
6611  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6612  */
6613 static ipsq_t *
6614 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6615     int type, boolean_t reentry_ok)
6616 {
6617 	ipsq_t	*ipsq;
6618 	ipxop_t	*ipx;
6619 	ip_stack_t *ipst = ill->ill_ipst;
6620 
6621 	/*
6622 	 * lock ordering:
6623 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6624 	 *
6625 	 * ipx of an ipsq can't change when ipsq_lock is held.
6626 	 */
6627 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6628 	GRAB_CONN_LOCK(q);
6629 	mutex_enter(&ill->ill_lock);
6630 	ipsq = ill->ill_phyint->phyint_ipsq;
6631 	mutex_enter(&ipsq->ipsq_lock);
6632 	ipx = ipsq->ipsq_xop;
6633 	mutex_enter(&ipx->ipx_lock);
6634 
6635 	/*
6636 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6637 	 *    (Note: If the caller does not specify reentry_ok then neither
6638 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6639 	 *    again. Otherwise it can lead to an infinite loop
6640 	 * 2. Enter the ipsq if there is no current writer and this attempted
6641 	 *    entry is part of the current operation
6642 	 * 3. Enter the ipsq if there is no current writer and this is a new
6643 	 *    operation and the operation queue is empty and there is no
6644 	 *    operation currently in progress and if all previously initiated
6645 	 *    DLPI operations have completed.
6646 	 */
6647 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6648 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6649 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6650 	    ipsq_dlpi_done(ipsq))))) {
6651 		/* Success. */
6652 		ipx->ipx_reentry_cnt++;
6653 		ipx->ipx_writer = curthread;
6654 		ipx->ipx_forced = B_FALSE;
6655 		mutex_exit(&ipx->ipx_lock);
6656 		mutex_exit(&ipsq->ipsq_lock);
6657 		mutex_exit(&ill->ill_lock);
6658 		RELEASE_CONN_LOCK(q);
6659 #ifdef DEBUG
6660 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6661 #endif
6662 		return (ipsq);
6663 	}
6664 
6665 	if (func != NULL)
6666 		ipsq_enq(ipsq, q, mp, func, type, ill);
6667 
6668 	mutex_exit(&ipx->ipx_lock);
6669 	mutex_exit(&ipsq->ipsq_lock);
6670 	mutex_exit(&ill->ill_lock);
6671 	RELEASE_CONN_LOCK(q);
6672 	return (NULL);
6673 }
6674 
6675 /*
6676  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6677  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6678  * There is one ipsq per phyint. The ipsq
6679  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6680  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6681  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6682  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6683  * up the interface) and are enqueued in ipx_mphead.
6684  *
6685  * If a thread does not want to reenter the ipsq when it is already writer,
6686  * it must make sure that the specified reentry point to be called later
6687  * when the ipsq is empty, nor any code path starting from the specified reentry
6688  * point must never ever try to enter the ipsq again. Otherwise it can lead
6689  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6690  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6691  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6692  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6693  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6694  * ioctl if the current ioctl has completed. If the current ioctl is still
6695  * in progress it simply returns. The current ioctl could be waiting for
6696  * a response from another module (the driver or could be waiting for
6697  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6698  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6699  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6700  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6701  * all associated DLPI operations have completed.
6702  */
6703 
6704 /*
6705  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6706  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6707  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6708  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6709  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6710  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6711  */
6712 ipsq_t *
6713 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6714     ipsq_func_t func, int type, boolean_t reentry_ok)
6715 {
6716 	ip_stack_t	*ipst;
6717 	ipsq_t		*ipsq;
6718 
6719 	/* Only 1 of ipif or ill can be specified */
6720 	ASSERT((ipif != NULL) ^ (ill != NULL));
6721 
6722 	if (ipif != NULL)
6723 		ill = ipif->ipif_ill;
6724 	ipst = ill->ill_ipst;
6725 
6726 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6727 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6728 	rw_exit(&ipst->ips_ill_g_lock);
6729 
6730 	return (ipsq);
6731 }
6732 
6733 /*
6734  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6735  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6736  * cannot be entered, the mp is queued for completion.
6737  */
6738 void
6739 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6740     boolean_t reentry_ok)
6741 {
6742 	ipsq_t	*ipsq;
6743 
6744 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6745 
6746 	/*
6747 	 * Drop the caller's refhold on the ill.  This is safe since we either
6748 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6749 	 * IPSQ, in which case we return without accessing ill anymore.  This
6750 	 * is needed because func needs to see the correct refcount.
6751 	 * e.g. removeif can work only then.
6752 	 */
6753 	ill_refrele(ill);
6754 	if (ipsq != NULL) {
6755 		(*func)(ipsq, q, mp, NULL);
6756 		ipsq_exit(ipsq);
6757 	}
6758 }
6759 
6760 /*
6761  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6762  * prior to exiting.  Caller must be writer on the specified IPSQ.
6763  */
6764 void
6765 ipsq_exit(ipsq_t *ipsq)
6766 {
6767 	mblk_t *mp;
6768 	ipsq_t *mp_ipsq;
6769 	queue_t	*q;
6770 	phyint_t *phyi;
6771 	ipsq_func_t func;
6772 
6773 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6774 
6775 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6776 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6777 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6778 		return;
6779 	}
6780 
6781 	for (;;) {
6782 		phyi = ipsq->ipsq_phyint;
6783 		mp = ipsq_dq(ipsq);
6784 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6785 
6786 		/*
6787 		 * If we've changed to a new IPSQ, and the phyint associated
6788 		 * with the old one has gone away, free the old IPSQ.  Note
6789 		 * that this cannot happen while the IPSQ is in a group.
6790 		 */
6791 		if (mp_ipsq != ipsq && phyi == NULL) {
6792 			ASSERT(ipsq->ipsq_next == ipsq);
6793 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6794 			ipsq_delete(ipsq);
6795 		}
6796 
6797 		if (mp == NULL)
6798 			break;
6799 
6800 		q = mp->b_queue;
6801 		func = (ipsq_func_t)mp->b_prev;
6802 		ipsq = mp_ipsq;
6803 		mp->b_next = mp->b_prev = NULL;
6804 		mp->b_queue = NULL;
6805 
6806 		/*
6807 		 * If 'q' is an conn queue, it is valid, since we did a
6808 		 * a refhold on the conn at the start of the ioctl.
6809 		 * If 'q' is an ill queue, it is valid, since close of an
6810 		 * ill will clean up its IPSQ.
6811 		 */
6812 		(*func)(ipsq, q, mp, NULL);
6813 	}
6814 }
6815 
6816 /*
6817  * Used to start any igmp or mld timers that could not be started
6818  * while holding ill_mcast_lock. The timers can't be started while holding
6819  * the lock, since mld/igmp_start_timers may need to call untimeout()
6820  * which can't be done while holding the lock which the timeout handler
6821  * acquires. Otherwise
6822  * there could be a deadlock since the timeout handlers
6823  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6824  * ill_mcast_lock.
6825  */
6826 void
6827 ill_mcast_timer_start(ip_stack_t *ipst)
6828 {
6829 	int		next;
6830 
6831 	mutex_enter(&ipst->ips_igmp_timer_lock);
6832 	next = ipst->ips_igmp_deferred_next;
6833 	ipst->ips_igmp_deferred_next = INFINITY;
6834 	mutex_exit(&ipst->ips_igmp_timer_lock);
6835 
6836 	if (next != INFINITY)
6837 		igmp_start_timers(next, ipst);
6838 
6839 	mutex_enter(&ipst->ips_mld_timer_lock);
6840 	next = ipst->ips_mld_deferred_next;
6841 	ipst->ips_mld_deferred_next = INFINITY;
6842 	mutex_exit(&ipst->ips_mld_timer_lock);
6843 
6844 	if (next != INFINITY)
6845 		mld_start_timers(next, ipst);
6846 }
6847 
6848 /*
6849  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6850  * and `ioccmd'.
6851  */
6852 void
6853 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6854 {
6855 	ill_t *ill = ipif->ipif_ill;
6856 	ipxop_t *ipx = ipsq->ipsq_xop;
6857 
6858 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6859 	ASSERT(ipx->ipx_current_ipif == NULL);
6860 	ASSERT(ipx->ipx_current_ioctl == 0);
6861 
6862 	ipx->ipx_current_done = B_FALSE;
6863 	ipx->ipx_current_ioctl = ioccmd;
6864 	mutex_enter(&ipx->ipx_lock);
6865 	ipx->ipx_current_ipif = ipif;
6866 	mutex_exit(&ipx->ipx_lock);
6867 
6868 	/*
6869 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6870 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6871 	 * references to the ipif (so that the references will eventually
6872 	 * drop to zero) and also prevents any "get" operations (e.g.,
6873 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6874 	 * operation has completed and the ipif is again in a stable state.
6875 	 *
6876 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6877 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6878 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6879 	 * ipifs will be affected.
6880 	 *
6881 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6882 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6883 	 * operate on.
6884 	 */
6885 	switch (ioccmd) {
6886 	case SIOCLIFREMOVEIF:
6887 		break;
6888 	case 0:
6889 		mutex_enter(&ill->ill_lock);
6890 		ipif = ipif->ipif_ill->ill_ipif;
6891 		for (; ipif != NULL; ipif = ipif->ipif_next)
6892 			ipif->ipif_state_flags |= IPIF_CHANGING;
6893 		mutex_exit(&ill->ill_lock);
6894 		break;
6895 	default:
6896 		mutex_enter(&ill->ill_lock);
6897 		ipif->ipif_state_flags |= IPIF_CHANGING;
6898 		mutex_exit(&ill->ill_lock);
6899 	}
6900 }
6901 
6902 /*
6903  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6904  * the next exclusive operation to begin once we ipsq_exit().  However, if
6905  * pending DLPI operations remain, then we will wait for the queue to drain
6906  * before allowing the next exclusive operation to begin.  This ensures that
6907  * DLPI operations from one exclusive operation are never improperly processed
6908  * as part of a subsequent exclusive operation.
6909  */
6910 void
6911 ipsq_current_finish(ipsq_t *ipsq)
6912 {
6913 	ipxop_t	*ipx = ipsq->ipsq_xop;
6914 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6915 	ipif_t	*ipif = ipx->ipx_current_ipif;
6916 
6917 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6918 
6919 	/*
6920 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6921 	 * (but in that case, IPIF_CHANGING will already be clear and no
6922 	 * pending DLPI messages can remain).
6923 	 */
6924 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6925 		ill_t *ill = ipif->ipif_ill;
6926 
6927 		mutex_enter(&ill->ill_lock);
6928 		dlpi_pending = ill->ill_dlpi_pending;
6929 		if (ipx->ipx_current_ioctl == 0) {
6930 			ipif = ill->ill_ipif;
6931 			for (; ipif != NULL; ipif = ipif->ipif_next)
6932 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6933 		} else {
6934 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6935 		}
6936 		mutex_exit(&ill->ill_lock);
6937 	}
6938 
6939 	ASSERT(!ipx->ipx_current_done);
6940 	ipx->ipx_current_done = B_TRUE;
6941 	ipx->ipx_current_ioctl = 0;
6942 	if (dlpi_pending == DL_PRIM_INVAL) {
6943 		mutex_enter(&ipx->ipx_lock);
6944 		ipx->ipx_current_ipif = NULL;
6945 		mutex_exit(&ipx->ipx_lock);
6946 	}
6947 }
6948 
6949 /*
6950  * The ill is closing. Flush all messages on the ipsq that originated
6951  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6952  * for this ill since ipsq_enter could not have entered until then.
6953  * New messages can't be queued since the CONDEMNED flag is set.
6954  */
6955 static void
6956 ipsq_flush(ill_t *ill)
6957 {
6958 	queue_t	*q;
6959 	mblk_t	*prev;
6960 	mblk_t	*mp;
6961 	mblk_t	*mp_next;
6962 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6963 
6964 	ASSERT(IAM_WRITER_ILL(ill));
6965 
6966 	/*
6967 	 * Flush any messages sent up by the driver.
6968 	 */
6969 	mutex_enter(&ipx->ipx_lock);
6970 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6971 		mp_next = mp->b_next;
6972 		q = mp->b_queue;
6973 		if (q == ill->ill_rq || q == ill->ill_wq) {
6974 			/* dequeue mp */
6975 			if (prev == NULL)
6976 				ipx->ipx_mphead = mp->b_next;
6977 			else
6978 				prev->b_next = mp->b_next;
6979 			if (ipx->ipx_mptail == mp) {
6980 				ASSERT(mp_next == NULL);
6981 				ipx->ipx_mptail = prev;
6982 			}
6983 			inet_freemsg(mp);
6984 		} else {
6985 			prev = mp;
6986 		}
6987 	}
6988 	mutex_exit(&ipx->ipx_lock);
6989 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6990 	ipsq_xopq_mp_cleanup(ill, NULL);
6991 }
6992 
6993 /*
6994  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6995  * and return the associated ipif.
6996  * Return value:
6997  *	Non zero: An error has occurred. ci may not be filled out.
6998  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6999  *	a held ipif in ci.ci_ipif.
7000  */
7001 int
7002 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7003     cmd_info_t *ci)
7004 {
7005 	char		*name;
7006 	struct ifreq    *ifr;
7007 	struct lifreq    *lifr;
7008 	ipif_t		*ipif = NULL;
7009 	ill_t		*ill;
7010 	conn_t		*connp;
7011 	boolean_t	isv6;
7012 	boolean_t	exists;
7013 	mblk_t		*mp1;
7014 	zoneid_t	zoneid;
7015 	ip_stack_t	*ipst;
7016 
7017 	if (q->q_next != NULL) {
7018 		ill = (ill_t *)q->q_ptr;
7019 		isv6 = ill->ill_isv6;
7020 		connp = NULL;
7021 		zoneid = ALL_ZONES;
7022 		ipst = ill->ill_ipst;
7023 	} else {
7024 		ill = NULL;
7025 		connp = Q_TO_CONN(q);
7026 		isv6 = (connp->conn_family == AF_INET6);
7027 		zoneid = connp->conn_zoneid;
7028 		if (zoneid == GLOBAL_ZONEID) {
7029 			/* global zone can access ipifs in all zones */
7030 			zoneid = ALL_ZONES;
7031 		}
7032 		ipst = connp->conn_netstack->netstack_ip;
7033 	}
7034 
7035 	/* Has been checked in ip_wput_nondata */
7036 	mp1 = mp->b_cont->b_cont;
7037 
7038 	if (ipip->ipi_cmd_type == IF_CMD) {
7039 		/* This a old style SIOC[GS]IF* command */
7040 		ifr = (struct ifreq *)mp1->b_rptr;
7041 		/*
7042 		 * Null terminate the string to protect against buffer
7043 		 * overrun. String was generated by user code and may not
7044 		 * be trusted.
7045 		 */
7046 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7047 		name = ifr->ifr_name;
7048 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7049 		ci->ci_sin6 = NULL;
7050 		ci->ci_lifr = (struct lifreq *)ifr;
7051 	} else {
7052 		/* This a new style SIOC[GS]LIF* command */
7053 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7054 		lifr = (struct lifreq *)mp1->b_rptr;
7055 		/*
7056 		 * Null terminate the string to protect against buffer
7057 		 * overrun. String was generated by user code and may not
7058 		 * be trusted.
7059 		 */
7060 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7061 		name = lifr->lifr_name;
7062 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7063 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7064 		ci->ci_lifr = lifr;
7065 	}
7066 
7067 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7068 		/*
7069 		 * The ioctl will be failed if the ioctl comes down
7070 		 * an conn stream
7071 		 */
7072 		if (ill == NULL) {
7073 			/*
7074 			 * Not an ill queue, return EINVAL same as the
7075 			 * old error code.
7076 			 */
7077 			return (ENXIO);
7078 		}
7079 		ipif = ill->ill_ipif;
7080 		ipif_refhold(ipif);
7081 	} else {
7082 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7083 		    &exists, isv6, zoneid, ipst);
7084 
7085 		/*
7086 		 * Ensure that get ioctls don't see any internal state changes
7087 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7088 		 * set.
7089 		 */
7090 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7091 		    !IAM_WRITER_IPIF(ipif)) {
7092 			ipsq_t	*ipsq;
7093 
7094 			if (connp != NULL)
7095 				mutex_enter(&connp->conn_lock);
7096 			mutex_enter(&ipif->ipif_ill->ill_lock);
7097 			if (IPIF_IS_CHANGING(ipif) &&
7098 			    !IPIF_IS_CONDEMNED(ipif)) {
7099 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7100 				mutex_enter(&ipsq->ipsq_lock);
7101 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7102 				mutex_exit(&ipif->ipif_ill->ill_lock);
7103 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7104 				    NEW_OP, ipif->ipif_ill);
7105 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7106 				mutex_exit(&ipsq->ipsq_lock);
7107 				if (connp != NULL)
7108 					mutex_exit(&connp->conn_lock);
7109 				ipif_refrele(ipif);
7110 				return (EINPROGRESS);
7111 			}
7112 			mutex_exit(&ipif->ipif_ill->ill_lock);
7113 			if (connp != NULL)
7114 				mutex_exit(&connp->conn_lock);
7115 		}
7116 	}
7117 
7118 	/*
7119 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7120 	 */
7121 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7122 		ipif_refrele(ipif);
7123 		return (ENXIO);
7124 	}
7125 
7126 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7127 	    name[0] == '\0') {
7128 		/*
7129 		 * Handle a or a SIOC?IF* with a null name
7130 		 * during plumb (on the ill queue before the I_PLINK).
7131 		 */
7132 		ipif = ill->ill_ipif;
7133 		ipif_refhold(ipif);
7134 	}
7135 
7136 	if (ipif == NULL)
7137 		return (ENXIO);
7138 
7139 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7140 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7141 
7142 	ci->ci_ipif = ipif;
7143 	return (0);
7144 }
7145 
7146 /*
7147  * Return the total number of ipifs.
7148  */
7149 static uint_t
7150 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7151 {
7152 	uint_t numifs = 0;
7153 	ill_t	*ill;
7154 	ill_walk_context_t	ctx;
7155 	ipif_t	*ipif;
7156 
7157 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7158 	ill = ILL_START_WALK_V4(&ctx, ipst);
7159 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7160 		if (IS_UNDER_IPMP(ill))
7161 			continue;
7162 		for (ipif = ill->ill_ipif; ipif != NULL;
7163 		    ipif = ipif->ipif_next) {
7164 			if (ipif->ipif_zoneid == zoneid ||
7165 			    ipif->ipif_zoneid == ALL_ZONES)
7166 				numifs++;
7167 		}
7168 	}
7169 	rw_exit(&ipst->ips_ill_g_lock);
7170 	return (numifs);
7171 }
7172 
7173 /*
7174  * Return the total number of ipifs.
7175  */
7176 static uint_t
7177 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7178 {
7179 	uint_t numifs = 0;
7180 	ill_t	*ill;
7181 	ipif_t	*ipif;
7182 	ill_walk_context_t	ctx;
7183 
7184 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7185 
7186 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7187 	if (family == AF_INET)
7188 		ill = ILL_START_WALK_V4(&ctx, ipst);
7189 	else if (family == AF_INET6)
7190 		ill = ILL_START_WALK_V6(&ctx, ipst);
7191 	else
7192 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7193 
7194 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7195 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7196 			continue;
7197 
7198 		for (ipif = ill->ill_ipif; ipif != NULL;
7199 		    ipif = ipif->ipif_next) {
7200 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7201 			    !(lifn_flags & LIFC_NOXMIT))
7202 				continue;
7203 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7204 			    !(lifn_flags & LIFC_TEMPORARY))
7205 				continue;
7206 			if (((ipif->ipif_flags &
7207 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7208 			    IPIF_DEPRECATED)) ||
7209 			    IS_LOOPBACK(ill) ||
7210 			    !(ipif->ipif_flags & IPIF_UP)) &&
7211 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7212 				continue;
7213 
7214 			if (zoneid != ipif->ipif_zoneid &&
7215 			    ipif->ipif_zoneid != ALL_ZONES &&
7216 			    (zoneid != GLOBAL_ZONEID ||
7217 			    !(lifn_flags & LIFC_ALLZONES)))
7218 				continue;
7219 
7220 			numifs++;
7221 		}
7222 	}
7223 	rw_exit(&ipst->ips_ill_g_lock);
7224 	return (numifs);
7225 }
7226 
7227 uint_t
7228 ip_get_lifsrcofnum(ill_t *ill)
7229 {
7230 	uint_t numifs = 0;
7231 	ill_t	*ill_head = ill;
7232 	ip_stack_t	*ipst = ill->ill_ipst;
7233 
7234 	/*
7235 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7236 	 * other thread may be trying to relink the ILLs in this usesrc group
7237 	 * and adjusting the ill_usesrc_grp_next pointers
7238 	 */
7239 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7240 	if ((ill->ill_usesrc_ifindex == 0) &&
7241 	    (ill->ill_usesrc_grp_next != NULL)) {
7242 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7243 		    ill = ill->ill_usesrc_grp_next)
7244 			numifs++;
7245 	}
7246 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7247 
7248 	return (numifs);
7249 }
7250 
7251 /* Null values are passed in for ipif, sin, and ifreq */
7252 /* ARGSUSED */
7253 int
7254 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7255     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7256 {
7257 	int *nump;
7258 	conn_t *connp = Q_TO_CONN(q);
7259 
7260 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7261 
7262 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7263 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7264 
7265 	*nump = ip_get_numifs(connp->conn_zoneid,
7266 	    connp->conn_netstack->netstack_ip);
7267 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7268 	return (0);
7269 }
7270 
7271 /* Null values are passed in for ipif, sin, and ifreq */
7272 /* ARGSUSED */
7273 int
7274 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7275     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7276 {
7277 	struct lifnum *lifn;
7278 	mblk_t	*mp1;
7279 	conn_t *connp = Q_TO_CONN(q);
7280 
7281 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7282 
7283 	/* Existence checked in ip_wput_nondata */
7284 	mp1 = mp->b_cont->b_cont;
7285 
7286 	lifn = (struct lifnum *)mp1->b_rptr;
7287 	switch (lifn->lifn_family) {
7288 	case AF_UNSPEC:
7289 	case AF_INET:
7290 	case AF_INET6:
7291 		break;
7292 	default:
7293 		return (EAFNOSUPPORT);
7294 	}
7295 
7296 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7297 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7298 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7299 	return (0);
7300 }
7301 
7302 /* ARGSUSED */
7303 int
7304 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7305     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7306 {
7307 	STRUCT_HANDLE(ifconf, ifc);
7308 	mblk_t *mp1;
7309 	struct iocblk *iocp;
7310 	struct ifreq *ifr;
7311 	ill_walk_context_t	ctx;
7312 	ill_t	*ill;
7313 	ipif_t	*ipif;
7314 	struct sockaddr_in *sin;
7315 	int32_t	ifclen;
7316 	zoneid_t zoneid;
7317 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7318 
7319 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7320 
7321 	ip1dbg(("ip_sioctl_get_ifconf"));
7322 	/* Existence verified in ip_wput_nondata */
7323 	mp1 = mp->b_cont->b_cont;
7324 	iocp = (struct iocblk *)mp->b_rptr;
7325 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7326 
7327 	/*
7328 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7329 	 * the user buffer address and length into which the list of struct
7330 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7331 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7332 	 * the SIOCGIFCONF operation was redefined to simply provide
7333 	 * a large output buffer into which we are supposed to jam the ifreq
7334 	 * array.  The same ioctl command code was used, despite the fact that
7335 	 * both the applications and the kernel code had to change, thus making
7336 	 * it impossible to support both interfaces.
7337 	 *
7338 	 * For reasons not good enough to try to explain, the following
7339 	 * algorithm is used for deciding what to do with one of these:
7340 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7341 	 * form with the output buffer coming down as the continuation message.
7342 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7343 	 * and we have to copy in the ifconf structure to find out how big the
7344 	 * output buffer is and where to copy out to.  Sure no problem...
7345 	 *
7346 	 */
7347 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7348 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7349 		int numifs = 0;
7350 		size_t ifc_bufsize;
7351 
7352 		/*
7353 		 * Must be (better be!) continuation of a TRANSPARENT
7354 		 * IOCTL.  We just copied in the ifconf structure.
7355 		 */
7356 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7357 		    (struct ifconf *)mp1->b_rptr);
7358 
7359 		/*
7360 		 * Allocate a buffer to hold requested information.
7361 		 *
7362 		 * If ifc_len is larger than what is needed, we only
7363 		 * allocate what we will use.
7364 		 *
7365 		 * If ifc_len is smaller than what is needed, return
7366 		 * EINVAL.
7367 		 *
7368 		 * XXX: the ill_t structure can hava 2 counters, for
7369 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7370 		 * number of interfaces for a device, so we don't need
7371 		 * to count them here...
7372 		 */
7373 		numifs = ip_get_numifs(zoneid, ipst);
7374 
7375 		ifclen = STRUCT_FGET(ifc, ifc_len);
7376 		ifc_bufsize = numifs * sizeof (struct ifreq);
7377 		if (ifc_bufsize > ifclen) {
7378 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7379 				/* old behaviour */
7380 				return (EINVAL);
7381 			} else {
7382 				ifc_bufsize = ifclen;
7383 			}
7384 		}
7385 
7386 		mp1 = mi_copyout_alloc(q, mp,
7387 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7388 		if (mp1 == NULL)
7389 			return (ENOMEM);
7390 
7391 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7392 	}
7393 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7394 	/*
7395 	 * the SIOCGIFCONF ioctl only knows about
7396 	 * IPv4 addresses, so don't try to tell
7397 	 * it about interfaces with IPv6-only
7398 	 * addresses. (Last parm 'isv6' is B_FALSE)
7399 	 */
7400 
7401 	ifr = (struct ifreq *)mp1->b_rptr;
7402 
7403 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7404 	ill = ILL_START_WALK_V4(&ctx, ipst);
7405 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7406 		if (IS_UNDER_IPMP(ill))
7407 			continue;
7408 		for (ipif = ill->ill_ipif; ipif != NULL;
7409 		    ipif = ipif->ipif_next) {
7410 			if (zoneid != ipif->ipif_zoneid &&
7411 			    ipif->ipif_zoneid != ALL_ZONES)
7412 				continue;
7413 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7414 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7415 					/* old behaviour */
7416 					rw_exit(&ipst->ips_ill_g_lock);
7417 					return (EINVAL);
7418 				} else {
7419 					goto if_copydone;
7420 				}
7421 			}
7422 			ipif_get_name(ipif, ifr->ifr_name,
7423 			    sizeof (ifr->ifr_name));
7424 			sin = (sin_t *)&ifr->ifr_addr;
7425 			*sin = sin_null;
7426 			sin->sin_family = AF_INET;
7427 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7428 			ifr++;
7429 		}
7430 	}
7431 if_copydone:
7432 	rw_exit(&ipst->ips_ill_g_lock);
7433 	mp1->b_wptr = (uchar_t *)ifr;
7434 
7435 	if (STRUCT_BUF(ifc) != NULL) {
7436 		STRUCT_FSET(ifc, ifc_len,
7437 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7438 	}
7439 	return (0);
7440 }
7441 
7442 /*
7443  * Get the interfaces using the address hosted on the interface passed in,
7444  * as a source adddress
7445  */
7446 /* ARGSUSED */
7447 int
7448 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7449     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7450 {
7451 	mblk_t *mp1;
7452 	ill_t	*ill, *ill_head;
7453 	ipif_t	*ipif, *orig_ipif;
7454 	int	numlifs = 0;
7455 	size_t	lifs_bufsize, lifsmaxlen;
7456 	struct	lifreq *lifr;
7457 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7458 	uint_t	ifindex;
7459 	zoneid_t zoneid;
7460 	boolean_t isv6 = B_FALSE;
7461 	struct	sockaddr_in	*sin;
7462 	struct	sockaddr_in6	*sin6;
7463 	STRUCT_HANDLE(lifsrcof, lifs);
7464 	ip_stack_t		*ipst;
7465 
7466 	ipst = CONNQ_TO_IPST(q);
7467 
7468 	ASSERT(q->q_next == NULL);
7469 
7470 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7471 
7472 	/* Existence verified in ip_wput_nondata */
7473 	mp1 = mp->b_cont->b_cont;
7474 
7475 	/*
7476 	 * Must be (better be!) continuation of a TRANSPARENT
7477 	 * IOCTL.  We just copied in the lifsrcof structure.
7478 	 */
7479 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7480 	    (struct lifsrcof *)mp1->b_rptr);
7481 
7482 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7483 		return (EINVAL);
7484 
7485 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7486 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7487 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7488 	if (ipif == NULL) {
7489 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7490 		    ifindex));
7491 		return (ENXIO);
7492 	}
7493 
7494 	/* Allocate a buffer to hold requested information */
7495 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7496 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7497 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7498 	/* The actual size needed is always returned in lifs_len */
7499 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7500 
7501 	/* If the amount we need is more than what is passed in, abort */
7502 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7503 		ipif_refrele(ipif);
7504 		return (0);
7505 	}
7506 
7507 	mp1 = mi_copyout_alloc(q, mp,
7508 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7509 	if (mp1 == NULL) {
7510 		ipif_refrele(ipif);
7511 		return (ENOMEM);
7512 	}
7513 
7514 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7515 	bzero(mp1->b_rptr, lifs_bufsize);
7516 
7517 	lifr = (struct lifreq *)mp1->b_rptr;
7518 
7519 	ill = ill_head = ipif->ipif_ill;
7520 	orig_ipif = ipif;
7521 
7522 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7523 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7524 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7525 
7526 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7527 	for (; (ill != NULL) && (ill != ill_head);
7528 	    ill = ill->ill_usesrc_grp_next) {
7529 
7530 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7531 			break;
7532 
7533 		ipif = ill->ill_ipif;
7534 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7535 		if (ipif->ipif_isv6) {
7536 			sin6 = (sin6_t *)&lifr->lifr_addr;
7537 			*sin6 = sin6_null;
7538 			sin6->sin6_family = AF_INET6;
7539 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7540 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7541 			    &ipif->ipif_v6net_mask);
7542 		} else {
7543 			sin = (sin_t *)&lifr->lifr_addr;
7544 			*sin = sin_null;
7545 			sin->sin_family = AF_INET;
7546 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7547 			lifr->lifr_addrlen = ip_mask_to_plen(
7548 			    ipif->ipif_net_mask);
7549 		}
7550 		lifr++;
7551 	}
7552 	rw_exit(&ipst->ips_ill_g_lock);
7553 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7554 	ipif_refrele(orig_ipif);
7555 	mp1->b_wptr = (uchar_t *)lifr;
7556 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7557 
7558 	return (0);
7559 }
7560 
7561 /* ARGSUSED */
7562 int
7563 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7564     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7565 {
7566 	mblk_t *mp1;
7567 	int	list;
7568 	ill_t	*ill;
7569 	ipif_t	*ipif;
7570 	int	flags;
7571 	int	numlifs = 0;
7572 	size_t	lifc_bufsize;
7573 	struct	lifreq *lifr;
7574 	sa_family_t	family;
7575 	struct	sockaddr_in	*sin;
7576 	struct	sockaddr_in6	*sin6;
7577 	ill_walk_context_t	ctx;
7578 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7579 	int32_t	lifclen;
7580 	zoneid_t zoneid;
7581 	STRUCT_HANDLE(lifconf, lifc);
7582 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7583 
7584 	ip1dbg(("ip_sioctl_get_lifconf"));
7585 
7586 	ASSERT(q->q_next == NULL);
7587 
7588 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7589 
7590 	/* Existence verified in ip_wput_nondata */
7591 	mp1 = mp->b_cont->b_cont;
7592 
7593 	/*
7594 	 * An extended version of SIOCGIFCONF that takes an
7595 	 * additional address family and flags field.
7596 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7597 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7598 	 * interfaces are omitted.
7599 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7600 	 * unless LIFC_TEMPORARY is specified.
7601 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7602 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7603 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7604 	 * has priority over LIFC_NOXMIT.
7605 	 */
7606 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7607 
7608 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7609 		return (EINVAL);
7610 
7611 	/*
7612 	 * Must be (better be!) continuation of a TRANSPARENT
7613 	 * IOCTL.  We just copied in the lifconf structure.
7614 	 */
7615 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7616 
7617 	family = STRUCT_FGET(lifc, lifc_family);
7618 	flags = STRUCT_FGET(lifc, lifc_flags);
7619 
7620 	switch (family) {
7621 	case AF_UNSPEC:
7622 		/*
7623 		 * walk all ILL's.
7624 		 */
7625 		list = MAX_G_HEADS;
7626 		break;
7627 	case AF_INET:
7628 		/*
7629 		 * walk only IPV4 ILL's.
7630 		 */
7631 		list = IP_V4_G_HEAD;
7632 		break;
7633 	case AF_INET6:
7634 		/*
7635 		 * walk only IPV6 ILL's.
7636 		 */
7637 		list = IP_V6_G_HEAD;
7638 		break;
7639 	default:
7640 		return (EAFNOSUPPORT);
7641 	}
7642 
7643 	/*
7644 	 * Allocate a buffer to hold requested information.
7645 	 *
7646 	 * If lifc_len is larger than what is needed, we only
7647 	 * allocate what we will use.
7648 	 *
7649 	 * If lifc_len is smaller than what is needed, return
7650 	 * EINVAL.
7651 	 */
7652 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7653 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7654 	lifclen = STRUCT_FGET(lifc, lifc_len);
7655 	if (lifc_bufsize > lifclen) {
7656 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7657 			return (EINVAL);
7658 		else
7659 			lifc_bufsize = lifclen;
7660 	}
7661 
7662 	mp1 = mi_copyout_alloc(q, mp,
7663 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7664 	if (mp1 == NULL)
7665 		return (ENOMEM);
7666 
7667 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7668 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7669 
7670 	lifr = (struct lifreq *)mp1->b_rptr;
7671 
7672 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7673 	ill = ill_first(list, list, &ctx, ipst);
7674 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7675 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7676 			continue;
7677 
7678 		for (ipif = ill->ill_ipif; ipif != NULL;
7679 		    ipif = ipif->ipif_next) {
7680 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7681 			    !(flags & LIFC_NOXMIT))
7682 				continue;
7683 
7684 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7685 			    !(flags & LIFC_TEMPORARY))
7686 				continue;
7687 
7688 			if (((ipif->ipif_flags &
7689 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7690 			    IPIF_DEPRECATED)) ||
7691 			    IS_LOOPBACK(ill) ||
7692 			    !(ipif->ipif_flags & IPIF_UP)) &&
7693 			    (flags & LIFC_EXTERNAL_SOURCE))
7694 				continue;
7695 
7696 			if (zoneid != ipif->ipif_zoneid &&
7697 			    ipif->ipif_zoneid != ALL_ZONES &&
7698 			    (zoneid != GLOBAL_ZONEID ||
7699 			    !(flags & LIFC_ALLZONES)))
7700 				continue;
7701 
7702 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7703 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7704 					rw_exit(&ipst->ips_ill_g_lock);
7705 					return (EINVAL);
7706 				} else {
7707 					goto lif_copydone;
7708 				}
7709 			}
7710 
7711 			ipif_get_name(ipif, lifr->lifr_name,
7712 			    sizeof (lifr->lifr_name));
7713 			lifr->lifr_type = ill->ill_type;
7714 			if (ipif->ipif_isv6) {
7715 				sin6 = (sin6_t *)&lifr->lifr_addr;
7716 				*sin6 = sin6_null;
7717 				sin6->sin6_family = AF_INET6;
7718 				sin6->sin6_addr =
7719 				    ipif->ipif_v6lcl_addr;
7720 				lifr->lifr_addrlen =
7721 				    ip_mask_to_plen_v6(
7722 				    &ipif->ipif_v6net_mask);
7723 			} else {
7724 				sin = (sin_t *)&lifr->lifr_addr;
7725 				*sin = sin_null;
7726 				sin->sin_family = AF_INET;
7727 				sin->sin_addr.s_addr =
7728 				    ipif->ipif_lcl_addr;
7729 				lifr->lifr_addrlen =
7730 				    ip_mask_to_plen(
7731 				    ipif->ipif_net_mask);
7732 			}
7733 			lifr++;
7734 		}
7735 	}
7736 lif_copydone:
7737 	rw_exit(&ipst->ips_ill_g_lock);
7738 
7739 	mp1->b_wptr = (uchar_t *)lifr;
7740 	if (STRUCT_BUF(lifc) != NULL) {
7741 		STRUCT_FSET(lifc, lifc_len,
7742 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7743 	}
7744 	return (0);
7745 }
7746 
7747 static void
7748 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7749 {
7750 	ip6_asp_t *table;
7751 	size_t table_size;
7752 	mblk_t *data_mp;
7753 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7754 	ip_stack_t	*ipst;
7755 
7756 	if (q->q_next == NULL)
7757 		ipst = CONNQ_TO_IPST(q);
7758 	else
7759 		ipst = ILLQ_TO_IPST(q);
7760 
7761 	/* These two ioctls are I_STR only */
7762 	if (iocp->ioc_count == TRANSPARENT) {
7763 		miocnak(q, mp, 0, EINVAL);
7764 		return;
7765 	}
7766 
7767 	data_mp = mp->b_cont;
7768 	if (data_mp == NULL) {
7769 		/* The user passed us a NULL argument */
7770 		table = NULL;
7771 		table_size = iocp->ioc_count;
7772 	} else {
7773 		/*
7774 		 * The user provided a table.  The stream head
7775 		 * may have copied in the user data in chunks,
7776 		 * so make sure everything is pulled up
7777 		 * properly.
7778 		 */
7779 		if (MBLKL(data_mp) < iocp->ioc_count) {
7780 			mblk_t *new_data_mp;
7781 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7782 			    NULL) {
7783 				miocnak(q, mp, 0, ENOMEM);
7784 				return;
7785 			}
7786 			freemsg(data_mp);
7787 			data_mp = new_data_mp;
7788 			mp->b_cont = data_mp;
7789 		}
7790 		table = (ip6_asp_t *)data_mp->b_rptr;
7791 		table_size = iocp->ioc_count;
7792 	}
7793 
7794 	switch (iocp->ioc_cmd) {
7795 	case SIOCGIP6ADDRPOLICY:
7796 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7797 		if (iocp->ioc_rval == -1)
7798 			iocp->ioc_error = EINVAL;
7799 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7800 		else if (table != NULL &&
7801 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7802 			ip6_asp_t *src = table;
7803 			ip6_asp32_t *dst = (void *)table;
7804 			int count = table_size / sizeof (ip6_asp_t);
7805 			int i;
7806 
7807 			/*
7808 			 * We need to do an in-place shrink of the array
7809 			 * to match the alignment attributes of the
7810 			 * 32-bit ABI looking at it.
7811 			 */
7812 			/* LINTED: logical expression always true: op "||" */
7813 			ASSERT(sizeof (*src) > sizeof (*dst));
7814 			for (i = 1; i < count; i++)
7815 				bcopy(src + i, dst + i, sizeof (*dst));
7816 		}
7817 #endif
7818 		break;
7819 
7820 	case SIOCSIP6ADDRPOLICY:
7821 		ASSERT(mp->b_prev == NULL);
7822 		mp->b_prev = (void *)q;
7823 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7824 		/*
7825 		 * We pass in the datamodel here so that the ip6_asp_replace()
7826 		 * routine can handle converting from 32-bit to native formats
7827 		 * where necessary.
7828 		 *
7829 		 * A better way to handle this might be to convert the inbound
7830 		 * data structure here, and hang it off a new 'mp'; thus the
7831 		 * ip6_asp_replace() logic would always be dealing with native
7832 		 * format data structures..
7833 		 *
7834 		 * (An even simpler way to handle these ioctls is to just
7835 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7836 		 * and just recompile everything that depends on it.)
7837 		 */
7838 #endif
7839 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7840 		    iocp->ioc_flag & IOC_MODELS);
7841 		return;
7842 	}
7843 
7844 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7845 	qreply(q, mp);
7846 }
7847 
7848 static void
7849 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7850 {
7851 	mblk_t 		*data_mp;
7852 	struct dstinforeq	*dir;
7853 	uint8_t		*end, *cur;
7854 	in6_addr_t	*daddr, *saddr;
7855 	ipaddr_t	v4daddr;
7856 	ire_t		*ire;
7857 	ipaddr_t	v4setsrc;
7858 	in6_addr_t	v6setsrc;
7859 	char		*slabel, *dlabel;
7860 	boolean_t	isipv4;
7861 	int		match_ire;
7862 	ill_t		*dst_ill;
7863 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7864 	conn_t		*connp = Q_TO_CONN(q);
7865 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7866 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7867 	uint64_t	ipif_flags;
7868 
7869 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7870 
7871 	/*
7872 	 * This ioctl is I_STR only, and must have a
7873 	 * data mblk following the M_IOCTL mblk.
7874 	 */
7875 	data_mp = mp->b_cont;
7876 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7877 		miocnak(q, mp, 0, EINVAL);
7878 		return;
7879 	}
7880 
7881 	if (MBLKL(data_mp) < iocp->ioc_count) {
7882 		mblk_t *new_data_mp;
7883 
7884 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7885 			miocnak(q, mp, 0, ENOMEM);
7886 			return;
7887 		}
7888 		freemsg(data_mp);
7889 		data_mp = new_data_mp;
7890 		mp->b_cont = data_mp;
7891 	}
7892 	match_ire = MATCH_IRE_DSTONLY;
7893 
7894 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7895 	    end - cur >= sizeof (struct dstinforeq);
7896 	    cur += sizeof (struct dstinforeq)) {
7897 		dir = (struct dstinforeq *)cur;
7898 		daddr = &dir->dir_daddr;
7899 		saddr = &dir->dir_saddr;
7900 
7901 		/*
7902 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7903 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7904 		 * and ip_select_source_v6() do not.
7905 		 */
7906 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7907 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7908 
7909 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7910 		if (isipv4) {
7911 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7912 			v4setsrc = INADDR_ANY;
7913 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7914 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7915 			    NULL, NULL);
7916 		} else {
7917 			v6setsrc = ipv6_all_zeros;
7918 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7919 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7920 			    NULL, NULL);
7921 		}
7922 		ASSERT(ire != NULL);
7923 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7924 			ire_refrele(ire);
7925 			dir->dir_dreachable = 0;
7926 
7927 			/* move on to next dst addr */
7928 			continue;
7929 		}
7930 		dir->dir_dreachable = 1;
7931 
7932 		dst_ill = ire_nexthop_ill(ire);
7933 		if (dst_ill == NULL) {
7934 			ire_refrele(ire);
7935 			continue;
7936 		}
7937 
7938 		/* With ipmp we most likely look at the ipmp ill here */
7939 		dir->dir_dmactype = dst_ill->ill_mactype;
7940 
7941 		if (isipv4) {
7942 			ipaddr_t v4saddr;
7943 
7944 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7945 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7946 			    &v4saddr, NULL, &ipif_flags) != 0) {
7947 				v4saddr = INADDR_ANY;
7948 				ipif_flags = 0;
7949 			}
7950 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7951 		} else {
7952 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7953 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7954 			    saddr, NULL, &ipif_flags) != 0) {
7955 				*saddr = ipv6_all_zeros;
7956 				ipif_flags = 0;
7957 			}
7958 		}
7959 
7960 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7961 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7962 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7963 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7964 		ire_refrele(ire);
7965 		ill_refrele(dst_ill);
7966 	}
7967 	miocack(q, mp, iocp->ioc_count, 0);
7968 }
7969 
7970 /*
7971  * Check if this is an address assigned to this machine.
7972  * Skips interfaces that are down by using ire checks.
7973  * Translates mapped addresses to v4 addresses and then
7974  * treats them as such, returning true if the v4 address
7975  * associated with this mapped address is configured.
7976  * Note: Applications will have to be careful what they do
7977  * with the response; use of mapped addresses limits
7978  * what can be done with the socket, especially with
7979  * respect to socket options and ioctls - neither IPv4
7980  * options nor IPv6 sticky options/ancillary data options
7981  * may be used.
7982  */
7983 /* ARGSUSED */
7984 int
7985 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7986     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7987 {
7988 	struct sioc_addrreq *sia;
7989 	sin_t *sin;
7990 	ire_t *ire;
7991 	mblk_t *mp1;
7992 	zoneid_t zoneid;
7993 	ip_stack_t	*ipst;
7994 
7995 	ip1dbg(("ip_sioctl_tmyaddr"));
7996 
7997 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7998 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7999 	ipst = CONNQ_TO_IPST(q);
8000 
8001 	/* Existence verified in ip_wput_nondata */
8002 	mp1 = mp->b_cont->b_cont;
8003 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8004 	sin = (sin_t *)&sia->sa_addr;
8005 	switch (sin->sin_family) {
8006 	case AF_INET6: {
8007 		sin6_t *sin6 = (sin6_t *)sin;
8008 
8009 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8010 			ipaddr_t v4_addr;
8011 
8012 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8013 			    v4_addr);
8014 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8015 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8016 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8017 		} else {
8018 			in6_addr_t v6addr;
8019 
8020 			v6addr = sin6->sin6_addr;
8021 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8022 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8023 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8024 		}
8025 		break;
8026 	}
8027 	case AF_INET: {
8028 		ipaddr_t v4addr;
8029 
8030 		v4addr = sin->sin_addr.s_addr;
8031 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8032 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8033 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8034 		break;
8035 	}
8036 	default:
8037 		return (EAFNOSUPPORT);
8038 	}
8039 	if (ire != NULL) {
8040 		sia->sa_res = 1;
8041 		ire_refrele(ire);
8042 	} else {
8043 		sia->sa_res = 0;
8044 	}
8045 	return (0);
8046 }
8047 
8048 /*
8049  * Check if this is an address assigned on-link i.e. neighbor,
8050  * and makes sure it's reachable from the current zone.
8051  * Returns true for my addresses as well.
8052  * Translates mapped addresses to v4 addresses and then
8053  * treats them as such, returning true if the v4 address
8054  * associated with this mapped address is configured.
8055  * Note: Applications will have to be careful what they do
8056  * with the response; use of mapped addresses limits
8057  * what can be done with the socket, especially with
8058  * respect to socket options and ioctls - neither IPv4
8059  * options nor IPv6 sticky options/ancillary data options
8060  * may be used.
8061  */
8062 /* ARGSUSED */
8063 int
8064 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8065     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8066 {
8067 	struct sioc_addrreq *sia;
8068 	sin_t *sin;
8069 	mblk_t	*mp1;
8070 	ire_t *ire = NULL;
8071 	zoneid_t zoneid;
8072 	ip_stack_t	*ipst;
8073 
8074 	ip1dbg(("ip_sioctl_tonlink"));
8075 
8076 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8077 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8078 	ipst = CONNQ_TO_IPST(q);
8079 
8080 	/* Existence verified in ip_wput_nondata */
8081 	mp1 = mp->b_cont->b_cont;
8082 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8083 	sin = (sin_t *)&sia->sa_addr;
8084 
8085 	/*
8086 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8087 	 * to make sure we only look at on-link unicast address.
8088 	 */
8089 	switch (sin->sin_family) {
8090 	case AF_INET6: {
8091 		sin6_t *sin6 = (sin6_t *)sin;
8092 
8093 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8094 			ipaddr_t v4_addr;
8095 
8096 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8097 			    v4_addr);
8098 			if (!CLASSD(v4_addr)) {
8099 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8100 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8101 				    0, ipst, NULL);
8102 			}
8103 		} else {
8104 			in6_addr_t v6addr;
8105 
8106 			v6addr = sin6->sin6_addr;
8107 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8108 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8109 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8110 				    ipst, NULL);
8111 			}
8112 		}
8113 		break;
8114 	}
8115 	case AF_INET: {
8116 		ipaddr_t v4addr;
8117 
8118 		v4addr = sin->sin_addr.s_addr;
8119 		if (!CLASSD(v4addr)) {
8120 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8121 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8122 		}
8123 		break;
8124 	}
8125 	default:
8126 		return (EAFNOSUPPORT);
8127 	}
8128 	sia->sa_res = 0;
8129 	if (ire != NULL) {
8130 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8131 
8132 		if ((ire->ire_type & IRE_ONLINK) &&
8133 		    !(ire->ire_type & IRE_BROADCAST))
8134 			sia->sa_res = 1;
8135 		ire_refrele(ire);
8136 	}
8137 	return (0);
8138 }
8139 
8140 /*
8141  * TBD: implement when kernel maintaines a list of site prefixes.
8142  */
8143 /* ARGSUSED */
8144 int
8145 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8146     ip_ioctl_cmd_t *ipip, void *ifreq)
8147 {
8148 	return (ENXIO);
8149 }
8150 
8151 /* ARP IOCTLs. */
8152 /* ARGSUSED */
8153 int
8154 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8155     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8156 {
8157 	int		err;
8158 	ipaddr_t	ipaddr;
8159 	struct iocblk	*iocp;
8160 	conn_t		*connp;
8161 	struct arpreq	*ar;
8162 	struct xarpreq	*xar;
8163 	int		arp_flags, flags, alength;
8164 	uchar_t		*lladdr;
8165 	ip_stack_t	*ipst;
8166 	ill_t		*ill = ipif->ipif_ill;
8167 	ill_t		*proxy_ill = NULL;
8168 	ipmp_arpent_t	*entp = NULL;
8169 	boolean_t	proxyarp = B_FALSE;
8170 	boolean_t	if_arp_ioctl = B_FALSE;
8171 	ncec_t		*ncec = NULL;
8172 	nce_t		*nce;
8173 
8174 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8175 	connp = Q_TO_CONN(q);
8176 	ipst = connp->conn_netstack->netstack_ip;
8177 	iocp = (struct iocblk *)mp->b_rptr;
8178 
8179 	if (ipip->ipi_cmd_type == XARP_CMD) {
8180 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8181 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8182 		ar = NULL;
8183 
8184 		arp_flags = xar->xarp_flags;
8185 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8186 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8187 		/*
8188 		 * Validate against user's link layer address length
8189 		 * input and name and addr length limits.
8190 		 */
8191 		alength = ill->ill_phys_addr_length;
8192 		if (ipip->ipi_cmd == SIOCSXARP) {
8193 			if (alength != xar->xarp_ha.sdl_alen ||
8194 			    (alength + xar->xarp_ha.sdl_nlen >
8195 			    sizeof (xar->xarp_ha.sdl_data)))
8196 				return (EINVAL);
8197 		}
8198 	} else {
8199 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8200 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8201 		xar = NULL;
8202 
8203 		arp_flags = ar->arp_flags;
8204 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8205 		/*
8206 		 * Theoretically, the sa_family could tell us what link
8207 		 * layer type this operation is trying to deal with. By
8208 		 * common usage AF_UNSPEC means ethernet. We'll assume
8209 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8210 		 * for now. Our new SIOC*XARP ioctls can be used more
8211 		 * generally.
8212 		 *
8213 		 * If the underlying media happens to have a non 6 byte
8214 		 * address, arp module will fail set/get, but the del
8215 		 * operation will succeed.
8216 		 */
8217 		alength = 6;
8218 		if ((ipip->ipi_cmd != SIOCDARP) &&
8219 		    (alength != ill->ill_phys_addr_length)) {
8220 			return (EINVAL);
8221 		}
8222 	}
8223 
8224 	/* Translate ATF* flags to NCE* flags */
8225 	flags = 0;
8226 	if (arp_flags & ATF_AUTHORITY)
8227 		flags |= NCE_F_AUTHORITY;
8228 	if (arp_flags & ATF_PERM)
8229 		flags |= NCE_F_NONUD; /* not subject to aging */
8230 	if (arp_flags & ATF_PUBL)
8231 		flags |= NCE_F_PUBLISH;
8232 
8233 	/*
8234 	 * IPMP ARP special handling:
8235 	 *
8236 	 * 1. Since ARP mappings must appear consistent across the group,
8237 	 *    prohibit changing ARP mappings on the underlying interfaces.
8238 	 *
8239 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8240 	 *    IP itself, prohibit changing them.
8241 	 *
8242 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8243 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8244 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8245 	 */
8246 	if (IS_UNDER_IPMP(ill)) {
8247 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8248 			return (EPERM);
8249 	}
8250 	if (IS_IPMP(ill)) {
8251 		ipmp_illgrp_t *illg = ill->ill_grp;
8252 
8253 		switch (ipip->ipi_cmd) {
8254 		case SIOCSARP:
8255 		case SIOCSXARP:
8256 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8257 			if (proxy_ill != NULL) {
8258 				proxyarp = B_TRUE;
8259 				if (!ipmp_ill_is_active(proxy_ill))
8260 					proxy_ill = ipmp_illgrp_next_ill(illg);
8261 				if (proxy_ill != NULL)
8262 					lladdr = proxy_ill->ill_phys_addr;
8263 			}
8264 			/* FALLTHRU */
8265 		}
8266 	}
8267 
8268 	ipaddr = sin->sin_addr.s_addr;
8269 	/*
8270 	 * don't match across illgrp per case (1) and (2).
8271 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8272 	 */
8273 	nce = nce_lookup_v4(ill, &ipaddr);
8274 	if (nce != NULL)
8275 		ncec = nce->nce_common;
8276 
8277 	switch (iocp->ioc_cmd) {
8278 	case SIOCDARP:
8279 	case SIOCDXARP: {
8280 		/*
8281 		 * Delete the NCE if any.
8282 		 */
8283 		if (ncec == NULL) {
8284 			iocp->ioc_error = ENXIO;
8285 			break;
8286 		}
8287 		/* Don't allow changes to arp mappings of local addresses. */
8288 		if (NCE_MYADDR(ncec)) {
8289 			nce_refrele(nce);
8290 			return (ENOTSUP);
8291 		}
8292 		iocp->ioc_error = 0;
8293 
8294 		/*
8295 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8296 		 * This will delete all the nce entries on the under_ills.
8297 		 */
8298 		ncec_delete(ncec);
8299 		/*
8300 		 * Once the NCE has been deleted, then the ire_dep* consistency
8301 		 * mechanism will find any IRE which depended on the now
8302 		 * condemned NCE (as part of sending packets).
8303 		 * That mechanism handles redirects by deleting redirects
8304 		 * that refer to UNREACHABLE nces.
8305 		 */
8306 		break;
8307 	}
8308 	case SIOCGARP:
8309 	case SIOCGXARP:
8310 		if (ncec != NULL) {
8311 			lladdr = ncec->ncec_lladdr;
8312 			flags = ncec->ncec_flags;
8313 			iocp->ioc_error = 0;
8314 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8315 		} else {
8316 			iocp->ioc_error = ENXIO;
8317 		}
8318 		break;
8319 	case SIOCSARP:
8320 	case SIOCSXARP:
8321 		/* Don't allow changes to arp mappings of local addresses. */
8322 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8323 			nce_refrele(nce);
8324 			return (ENOTSUP);
8325 		}
8326 
8327 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8328 		flags |= NCE_F_STATIC;
8329 		if (!if_arp_ioctl) {
8330 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8331 			    lladdr, alength, flags);
8332 		} else {
8333 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8334 			if (ipif != NULL) {
8335 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8336 				    lladdr, alength, flags);
8337 				ipif_refrele(ipif);
8338 			}
8339 		}
8340 		if (nce != NULL) {
8341 			nce_refrele(nce);
8342 			nce = NULL;
8343 		}
8344 		/*
8345 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8346 		 * by nce_add_common()
8347 		 */
8348 		err = nce_lookup_then_add_v4(ill, lladdr,
8349 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8350 		    &nce);
8351 		if (err == EEXIST) {
8352 			ncec = nce->nce_common;
8353 			mutex_enter(&ncec->ncec_lock);
8354 			ncec->ncec_state = ND_REACHABLE;
8355 			ncec->ncec_flags = flags;
8356 			nce_update(ncec, ND_UNCHANGED, lladdr);
8357 			mutex_exit(&ncec->ncec_lock);
8358 			err = 0;
8359 		}
8360 		if (nce != NULL) {
8361 			nce_refrele(nce);
8362 			nce = NULL;
8363 		}
8364 		if (IS_IPMP(ill) && err == 0) {
8365 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8366 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8367 			    flags);
8368 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8369 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8370 				break;
8371 			}
8372 		}
8373 		iocp->ioc_error = err;
8374 	}
8375 
8376 	if (nce != NULL) {
8377 		nce_refrele(nce);
8378 	}
8379 
8380 	/*
8381 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8382 	 */
8383 	if (entp != NULL)
8384 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8385 
8386 	return (iocp->ioc_error);
8387 }
8388 
8389 /*
8390  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8391  * the associated sin and refhold and return the associated ipif via `ci'.
8392  */
8393 int
8394 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8395     cmd_info_t *ci)
8396 {
8397 	mblk_t	*mp1;
8398 	sin_t	*sin;
8399 	conn_t	*connp;
8400 	ipif_t	*ipif;
8401 	ire_t	*ire = NULL;
8402 	ill_t	*ill = NULL;
8403 	boolean_t exists;
8404 	ip_stack_t *ipst;
8405 	struct arpreq *ar;
8406 	struct xarpreq *xar;
8407 	struct sockaddr_dl *sdl;
8408 
8409 	/* ioctl comes down on a conn */
8410 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8411 	connp = Q_TO_CONN(q);
8412 	if (connp->conn_family == AF_INET6)
8413 		return (ENXIO);
8414 
8415 	ipst = connp->conn_netstack->netstack_ip;
8416 
8417 	/* Verified in ip_wput_nondata */
8418 	mp1 = mp->b_cont->b_cont;
8419 
8420 	if (ipip->ipi_cmd_type == XARP_CMD) {
8421 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8422 		xar = (struct xarpreq *)mp1->b_rptr;
8423 		sin = (sin_t *)&xar->xarp_pa;
8424 		sdl = &xar->xarp_ha;
8425 
8426 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8427 			return (ENXIO);
8428 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8429 			return (EINVAL);
8430 	} else {
8431 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8432 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8433 		ar = (struct arpreq *)mp1->b_rptr;
8434 		sin = (sin_t *)&ar->arp_pa;
8435 	}
8436 
8437 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8438 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8439 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8440 		if (ipif == NULL)
8441 			return (ENXIO);
8442 		if (ipif->ipif_id != 0) {
8443 			ipif_refrele(ipif);
8444 			return (ENXIO);
8445 		}
8446 	} else {
8447 		/*
8448 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8449 		 * of 0: use the IP address to find the ipif.  If the IP
8450 		 * address is an IPMP test address, ire_ftable_lookup() will
8451 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8452 		 */
8453 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8454 		    ipst);
8455 		if (ipif == NULL) {
8456 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8457 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8458 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8459 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8460 				if (ire != NULL)
8461 					ire_refrele(ire);
8462 				return (ENXIO);
8463 			}
8464 			ASSERT(ire != NULL && ill != NULL);
8465 			ipif = ill->ill_ipif;
8466 			ipif_refhold(ipif);
8467 			ire_refrele(ire);
8468 		}
8469 	}
8470 
8471 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8472 		ipif_refrele(ipif);
8473 		return (ENXIO);
8474 	}
8475 
8476 	ci->ci_sin = sin;
8477 	ci->ci_ipif = ipif;
8478 	return (0);
8479 }
8480 
8481 /*
8482  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8483  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8484  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8485  * up and thus an ill can join that illgrp.
8486  *
8487  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8488  * open()/close() primarily because close() is not allowed to fail or block
8489  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8490  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8491  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8492  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8493  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8494  * state if I_UNLINK didn't occur.
8495  *
8496  * Note that for each plumb/unplumb operation, we may end up here more than
8497  * once because of the way ifconfig works.  However, it's OK to link the same
8498  * illgrp more than once, or unlink an illgrp that's already unlinked.
8499  */
8500 static int
8501 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8502 {
8503 	int err;
8504 	ip_stack_t *ipst = ill->ill_ipst;
8505 
8506 	ASSERT(IS_IPMP(ill));
8507 	ASSERT(IAM_WRITER_ILL(ill));
8508 
8509 	switch (ioccmd) {
8510 	case I_LINK:
8511 		return (ENOTSUP);
8512 
8513 	case I_PLINK:
8514 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8515 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8516 		rw_exit(&ipst->ips_ipmp_lock);
8517 		break;
8518 
8519 	case I_PUNLINK:
8520 		/*
8521 		 * Require all UP ipifs be brought down prior to unlinking the
8522 		 * illgrp so any associated IREs (and other state) is torched.
8523 		 */
8524 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8525 			return (EBUSY);
8526 
8527 		/*
8528 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8529 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8530 		 * join this group.  Specifically: ills trying to join grab
8531 		 * ipmp_lock and bump a "pending join" counter checked by
8532 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8533 		 * joins can occur (since we have ipmp_lock).  Once we drop
8534 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8535 		 * find the illgrp (since we unlinked it) and will return
8536 		 * EAFNOSUPPORT.  This will then take them back through the
8537 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8538 		 * back through I_PLINK above.
8539 		 */
8540 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8541 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8542 		rw_exit(&ipst->ips_ipmp_lock);
8543 		return (err);
8544 	default:
8545 		break;
8546 	}
8547 	return (0);
8548 }
8549 
8550 /*
8551  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8552  * atomically set/clear the muxids. Also complete the ioctl by acking or
8553  * naking it.  Note that the code is structured such that the link type,
8554  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8555  * its clones use the persistent link, while pppd(1M) and perhaps many
8556  * other daemons may use non-persistent link.  When combined with some
8557  * ill_t states, linking and unlinking lower streams may be used as
8558  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8559  */
8560 /* ARGSUSED */
8561 void
8562 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8563 {
8564 	mblk_t		*mp1;
8565 	struct linkblk	*li;
8566 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8567 	int		err = 0;
8568 
8569 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8570 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8571 
8572 	mp1 = mp->b_cont;	/* This is the linkblk info */
8573 	li = (struct linkblk *)mp1->b_rptr;
8574 
8575 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8576 	if (err == EINPROGRESS)
8577 		return;
8578 done:
8579 	if (err == 0)
8580 		miocack(q, mp, 0, 0);
8581 	else
8582 		miocnak(q, mp, 0, err);
8583 
8584 	/* Conn was refheld in ip_sioctl_copyin_setup */
8585 	if (CONN_Q(q))
8586 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8587 }
8588 
8589 /*
8590  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8591  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8592  * module stream).  If `doconsist' is set, then do the extended consistency
8593  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8594  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8595  * an error code on failure.
8596  */
8597 static int
8598 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8599     struct linkblk *li)
8600 {
8601 	int		err = 0;
8602 	ill_t  		*ill;
8603 	queue_t		*ipwq, *dwq;
8604 	const char	*name;
8605 	struct qinit	*qinfo;
8606 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8607 	boolean_t	entered_ipsq = B_FALSE;
8608 	boolean_t	is_ip = B_FALSE;
8609 	arl_t		*arl;
8610 
8611 	/*
8612 	 * Walk the lower stream to verify it's the IP module stream.
8613 	 * The IP module is identified by its name, wput function,
8614 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8615 	 * (li->l_qbot) will not vanish until this ioctl completes.
8616 	 */
8617 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8618 		qinfo = ipwq->q_qinfo;
8619 		name = qinfo->qi_minfo->mi_idname;
8620 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8621 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8622 			is_ip = B_TRUE;
8623 			break;
8624 		}
8625 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8626 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8627 			break;
8628 		}
8629 	}
8630 
8631 	/*
8632 	 * If this isn't an IP module stream, bail.
8633 	 */
8634 	if (ipwq == NULL)
8635 		return (0);
8636 
8637 	if (!is_ip) {
8638 		arl = (arl_t *)ipwq->q_ptr;
8639 		ill = arl_to_ill(arl);
8640 		if (ill == NULL)
8641 			return (0);
8642 	} else {
8643 		ill = ipwq->q_ptr;
8644 	}
8645 	ASSERT(ill != NULL);
8646 
8647 	if (ipsq == NULL) {
8648 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8649 		    NEW_OP, B_FALSE);
8650 		if (ipsq == NULL) {
8651 			if (!is_ip)
8652 				ill_refrele(ill);
8653 			return (EINPROGRESS);
8654 		}
8655 		entered_ipsq = B_TRUE;
8656 	}
8657 	ASSERT(IAM_WRITER_ILL(ill));
8658 	mutex_enter(&ill->ill_lock);
8659 	if (!is_ip) {
8660 		if (islink && ill->ill_muxid == 0) {
8661 			/*
8662 			 * Plumbing has to be done with IP plumbed first, arp
8663 			 * second, but here we have arp being plumbed first.
8664 			 */
8665 			mutex_exit(&ill->ill_lock);
8666 			ipsq_exit(ipsq);
8667 			ill_refrele(ill);
8668 			return (EINVAL);
8669 		}
8670 	}
8671 	mutex_exit(&ill->ill_lock);
8672 	if (!is_ip) {
8673 		arl->arl_muxid = islink ? li->l_index : 0;
8674 		ill_refrele(ill);
8675 		goto done;
8676 	}
8677 
8678 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8679 		goto done;
8680 
8681 	/*
8682 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8683 	 * the read queue of the module immediately below IP in the ill.
8684 	 * These are used during the capability negotiation below.
8685 	 */
8686 	ill->ill_lmod_rq = NULL;
8687 	ill->ill_lmod_cnt = 0;
8688 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8689 		ill->ill_lmod_rq = RD(dwq);
8690 		for (; dwq != NULL; dwq = dwq->q_next)
8691 			ill->ill_lmod_cnt++;
8692 	}
8693 
8694 	ill->ill_muxid = islink ? li->l_index : 0;
8695 
8696 	/*
8697 	 * Mark the ipsq busy until the capability operations initiated below
8698 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8699 	 * returns, but the capability operation may complete asynchronously
8700 	 * much later.
8701 	 */
8702 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8703 	/*
8704 	 * If there's at least one up ipif on this ill, then we're bound to
8705 	 * the underlying driver via DLPI.  In that case, renegotiate
8706 	 * capabilities to account for any possible change in modules
8707 	 * interposed between IP and the driver.
8708 	 */
8709 	if (ill->ill_ipif_up_count > 0) {
8710 		if (islink)
8711 			ill_capability_probe(ill);
8712 		else
8713 			ill_capability_reset(ill, B_FALSE);
8714 	}
8715 	ipsq_current_finish(ipsq);
8716 done:
8717 	if (entered_ipsq)
8718 		ipsq_exit(ipsq);
8719 
8720 	return (err);
8721 }
8722 
8723 /*
8724  * Search the ioctl command in the ioctl tables and return a pointer
8725  * to the ioctl command information. The ioctl command tables are
8726  * static and fully populated at compile time.
8727  */
8728 ip_ioctl_cmd_t *
8729 ip_sioctl_lookup(int ioc_cmd)
8730 {
8731 	int index;
8732 	ip_ioctl_cmd_t *ipip;
8733 	ip_ioctl_cmd_t *ipip_end;
8734 
8735 	if (ioc_cmd == IPI_DONTCARE)
8736 		return (NULL);
8737 
8738 	/*
8739 	 * Do a 2 step search. First search the indexed table
8740 	 * based on the least significant byte of the ioctl cmd.
8741 	 * If we don't find a match, then search the misc table
8742 	 * serially.
8743 	 */
8744 	index = ioc_cmd & 0xFF;
8745 	if (index < ip_ndx_ioctl_count) {
8746 		ipip = &ip_ndx_ioctl_table[index];
8747 		if (ipip->ipi_cmd == ioc_cmd) {
8748 			/* Found a match in the ndx table */
8749 			return (ipip);
8750 		}
8751 	}
8752 
8753 	/* Search the misc table */
8754 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8755 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8756 		if (ipip->ipi_cmd == ioc_cmd)
8757 			/* Found a match in the misc table */
8758 			return (ipip);
8759 	}
8760 
8761 	return (NULL);
8762 }
8763 
8764 /*
8765  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8766  */
8767 static boolean_t
8768 getset_ioctl_checks(mblk_t *mp)
8769 {
8770 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8771 	mblk_t		*mp1 = mp->b_cont;
8772 	mod_ioc_prop_t	*pioc;
8773 	uint_t		flags;
8774 	uint_t		pioc_size;
8775 
8776 	/* do sanity checks on various arguments */
8777 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8778 	    iocp->ioc_count == TRANSPARENT) {
8779 		return (B_FALSE);
8780 	}
8781 	if (msgdsize(mp1) < iocp->ioc_count) {
8782 		if (!pullupmsg(mp1, iocp->ioc_count))
8783 			return (B_FALSE);
8784 	}
8785 
8786 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8787 
8788 	/* sanity checks on mpr_valsize */
8789 	pioc_size = sizeof (mod_ioc_prop_t);
8790 	if (pioc->mpr_valsize != 0)
8791 		pioc_size += pioc->mpr_valsize - 1;
8792 
8793 	if (iocp->ioc_count != pioc_size)
8794 		return (B_FALSE);
8795 
8796 	flags = pioc->mpr_flags;
8797 	if (iocp->ioc_cmd == SIOCSETPROP) {
8798 		/*
8799 		 * One can either reset the value to it's default value or
8800 		 * change the current value or append/remove the value from
8801 		 * a multi-valued properties.
8802 		 */
8803 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8804 		    flags != MOD_PROP_ACTIVE &&
8805 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8806 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8807 			return (B_FALSE);
8808 	} else {
8809 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8810 
8811 		/*
8812 		 * One can retrieve only one kind of property information
8813 		 * at a time.
8814 		 */
8815 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8816 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8817 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8818 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8819 			return (B_FALSE);
8820 	}
8821 
8822 	return (B_TRUE);
8823 }
8824 
8825 /*
8826  * process the SIOC{SET|GET}PROP ioctl's
8827  */
8828 /* ARGSUSED */
8829 static void
8830 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8831 {
8832 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8833 	mblk_t		*mp1 = mp->b_cont;
8834 	mod_ioc_prop_t	*pioc;
8835 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8836 	ip_stack_t	*ipst;
8837 	icmp_stack_t	*is;
8838 	tcp_stack_t	*tcps;
8839 	sctp_stack_t	*sctps;
8840 	udp_stack_t	*us;
8841 	netstack_t	*stack;
8842 	void		*cbarg;
8843 	cred_t		*cr;
8844 	boolean_t 	set;
8845 	int		err;
8846 
8847 	ASSERT(q->q_next == NULL);
8848 	ASSERT(CONN_Q(q));
8849 
8850 	if (!getset_ioctl_checks(mp)) {
8851 		miocnak(q, mp, 0, EINVAL);
8852 		return;
8853 	}
8854 	ipst = CONNQ_TO_IPST(q);
8855 	stack = ipst->ips_netstack;
8856 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8857 
8858 	switch (pioc->mpr_proto) {
8859 	case MOD_PROTO_IP:
8860 	case MOD_PROTO_IPV4:
8861 	case MOD_PROTO_IPV6:
8862 		ptbl = ipst->ips_propinfo_tbl;
8863 		cbarg = ipst;
8864 		break;
8865 	case MOD_PROTO_RAWIP:
8866 		is = stack->netstack_icmp;
8867 		ptbl = is->is_propinfo_tbl;
8868 		cbarg = is;
8869 		break;
8870 	case MOD_PROTO_TCP:
8871 		tcps = stack->netstack_tcp;
8872 		ptbl = tcps->tcps_propinfo_tbl;
8873 		cbarg = tcps;
8874 		break;
8875 	case MOD_PROTO_UDP:
8876 		us = stack->netstack_udp;
8877 		ptbl = us->us_propinfo_tbl;
8878 		cbarg = us;
8879 		break;
8880 	case MOD_PROTO_SCTP:
8881 		sctps = stack->netstack_sctp;
8882 		ptbl = sctps->sctps_propinfo_tbl;
8883 		cbarg = sctps;
8884 		break;
8885 	default:
8886 		miocnak(q, mp, 0, EINVAL);
8887 		return;
8888 	}
8889 
8890 	/* search for given property in respective protocol propinfo table */
8891 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8892 		if (strcmp(pinfo->mpi_name, pioc->mpr_name) == 0 &&
8893 		    pinfo->mpi_proto == pioc->mpr_proto)
8894 			break;
8895 	}
8896 	if (pinfo->mpi_name == NULL) {
8897 		miocnak(q, mp, 0, ENOENT);
8898 		return;
8899 	}
8900 
8901 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8902 	if (set && pinfo->mpi_setf != NULL) {
8903 		cr = msg_getcred(mp, NULL);
8904 		if (cr == NULL)
8905 			cr = iocp->ioc_cr;
8906 		err = pinfo->mpi_setf(cbarg, cr, pinfo, pioc->mpr_ifname,
8907 		    pioc->mpr_val, pioc->mpr_flags);
8908 	} else if (!set && pinfo->mpi_getf != NULL) {
8909 		err = pinfo->mpi_getf(cbarg, pinfo, pioc->mpr_ifname,
8910 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8911 	} else {
8912 		err = EPERM;
8913 	}
8914 
8915 	if (err != 0) {
8916 		miocnak(q, mp, 0, err);
8917 	} else {
8918 		if (set)
8919 			miocack(q, mp, 0, 0);
8920 		else    /* For get, we need to return back the data */
8921 			miocack(q, mp, iocp->ioc_count, 0);
8922 	}
8923 }
8924 
8925 /*
8926  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8927  * as several routing daemons have unfortunately used this 'unpublished'
8928  * but well-known ioctls.
8929  */
8930 /* ARGSUSED */
8931 static void
8932 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8933 {
8934 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8935 	mblk_t		*mp1 = mp->b_cont;
8936 	char		*pname, *pval, *buf;
8937 	uint_t		bufsize, proto;
8938 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8939 	ip_stack_t	*ipst;
8940 	int		err = 0;
8941 
8942 	ASSERT(CONN_Q(q));
8943 	ipst = CONNQ_TO_IPST(q);
8944 
8945 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8946 		miocnak(q, mp, 0, EINVAL);
8947 		return;
8948 	}
8949 
8950 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8951 	pval = buf = pname = (char *)mp1->b_rptr;
8952 	bufsize = MBLKL(mp1);
8953 
8954 	if (strcmp(pname, "ip_forwarding") == 0) {
8955 		pname = "forwarding";
8956 		proto = MOD_PROTO_IPV4;
8957 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8958 		pname = "forwarding";
8959 		proto = MOD_PROTO_IPV6;
8960 	} else {
8961 		miocnak(q, mp, 0, EINVAL);
8962 		return;
8963 	}
8964 
8965 	ptbl = ipst->ips_propinfo_tbl;
8966 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8967 		if (strcmp(pinfo->mpi_name, pname) == 0 &&
8968 		    pinfo->mpi_proto == proto)
8969 			break;
8970 	}
8971 
8972 	ASSERT(pinfo->mpi_name != NULL);
8973 
8974 	switch (iocp->ioc_cmd) {
8975 	case ND_GET:
8976 		if ((err = pinfo->mpi_getf(ipst, pinfo, NULL, buf, bufsize,
8977 		    0)) == 0) {
8978 			miocack(q, mp, iocp->ioc_count, 0);
8979 			return;
8980 		}
8981 		break;
8982 	case ND_SET:
8983 		/*
8984 		 * buffer will have property name and value in the following
8985 		 * format,
8986 		 * <property name>'\0'<property value>'\0', extract them;
8987 		 */
8988 		while (*pval++)
8989 			noop;
8990 
8991 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8992 			err = EINVAL;
8993 		} else if ((err = pinfo->mpi_setf(ipst, NULL, pinfo, NULL,
8994 		    pval, 0)) == 0) {
8995 			miocack(q, mp, 0, 0);
8996 			return;
8997 		}
8998 		break;
8999 	default:
9000 		err = EINVAL;
9001 		break;
9002 	}
9003 	miocnak(q, mp, 0, err);
9004 }
9005 
9006 /*
9007  * Wrapper function for resuming deferred ioctl processing
9008  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9009  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9010  */
9011 /* ARGSUSED */
9012 void
9013 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9014     void *dummy_arg)
9015 {
9016 	ip_sioctl_copyin_setup(q, mp);
9017 }
9018 
9019 /*
9020  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9021  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9022  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9023  * We establish here the size of the block to be copied in.  mi_copyin
9024  * arranges for this to happen, an processing continues in ip_wput_nondata with
9025  * an M_IOCDATA message.
9026  */
9027 void
9028 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9029 {
9030 	int	copyin_size;
9031 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9032 	ip_ioctl_cmd_t *ipip;
9033 	cred_t *cr;
9034 	ip_stack_t	*ipst;
9035 
9036 	if (CONN_Q(q))
9037 		ipst = CONNQ_TO_IPST(q);
9038 	else
9039 		ipst = ILLQ_TO_IPST(q);
9040 
9041 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9042 	if (ipip == NULL) {
9043 		/*
9044 		 * The ioctl is not one we understand or own.
9045 		 * Pass it along to be processed down stream,
9046 		 * if this is a module instance of IP, else nak
9047 		 * the ioctl.
9048 		 */
9049 		if (q->q_next == NULL) {
9050 			goto nak;
9051 		} else {
9052 			putnext(q, mp);
9053 			return;
9054 		}
9055 	}
9056 
9057 	/*
9058 	 * If this is deferred, then we will do all the checks when we
9059 	 * come back.
9060 	 */
9061 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9062 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9063 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9064 		return;
9065 	}
9066 
9067 	/*
9068 	 * Only allow a very small subset of IP ioctls on this stream if
9069 	 * IP is a module and not a driver. Allowing ioctls to be processed
9070 	 * in this case may cause assert failures or data corruption.
9071 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9072 	 * ioctls allowed on an IP module stream, after which this stream
9073 	 * normally becomes a multiplexor (at which time the stream head
9074 	 * will fail all ioctls).
9075 	 */
9076 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9077 		goto nak;
9078 	}
9079 
9080 	/* Make sure we have ioctl data to process. */
9081 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9082 		goto nak;
9083 
9084 	/*
9085 	 * Prefer dblk credential over ioctl credential; some synthesized
9086 	 * ioctls have kcred set because there's no way to crhold()
9087 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9088 	 * the framework; the caller of ioctl needs to hold the reference
9089 	 * for the duration of the call).
9090 	 */
9091 	cr = msg_getcred(mp, NULL);
9092 	if (cr == NULL)
9093 		cr = iocp->ioc_cr;
9094 
9095 	/* Make sure normal users don't send down privileged ioctls */
9096 	if ((ipip->ipi_flags & IPI_PRIV) &&
9097 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9098 		/* We checked the privilege earlier but log it here */
9099 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9100 		return;
9101 	}
9102 
9103 	/*
9104 	 * The ioctl command tables can only encode fixed length
9105 	 * ioctl data. If the length is variable, the table will
9106 	 * encode the length as zero. Such special cases are handled
9107 	 * below in the switch.
9108 	 */
9109 	if (ipip->ipi_copyin_size != 0) {
9110 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9111 		return;
9112 	}
9113 
9114 	switch (iocp->ioc_cmd) {
9115 	case O_SIOCGIFCONF:
9116 	case SIOCGIFCONF:
9117 		/*
9118 		 * This IOCTL is hilarious.  See comments in
9119 		 * ip_sioctl_get_ifconf for the story.
9120 		 */
9121 		if (iocp->ioc_count == TRANSPARENT)
9122 			copyin_size = SIZEOF_STRUCT(ifconf,
9123 			    iocp->ioc_flag);
9124 		else
9125 			copyin_size = iocp->ioc_count;
9126 		mi_copyin(q, mp, NULL, copyin_size);
9127 		return;
9128 
9129 	case O_SIOCGLIFCONF:
9130 	case SIOCGLIFCONF:
9131 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9132 		mi_copyin(q, mp, NULL, copyin_size);
9133 		return;
9134 
9135 	case SIOCGLIFSRCOF:
9136 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9137 		mi_copyin(q, mp, NULL, copyin_size);
9138 		return;
9139 
9140 	case SIOCGIP6ADDRPOLICY:
9141 		ip_sioctl_ip6addrpolicy(q, mp);
9142 		ip6_asp_table_refrele(ipst);
9143 		return;
9144 
9145 	case SIOCSIP6ADDRPOLICY:
9146 		ip_sioctl_ip6addrpolicy(q, mp);
9147 		return;
9148 
9149 	case SIOCGDSTINFO:
9150 		ip_sioctl_dstinfo(q, mp);
9151 		ip6_asp_table_refrele(ipst);
9152 		return;
9153 
9154 	case ND_SET:
9155 	case ND_GET:
9156 		ip_process_legacy_nddprop(q, mp);
9157 		return;
9158 
9159 	case SIOCSETPROP:
9160 	case SIOCGETPROP:
9161 		ip_sioctl_getsetprop(q, mp);
9162 		return;
9163 
9164 	case I_PLINK:
9165 	case I_PUNLINK:
9166 	case I_LINK:
9167 	case I_UNLINK:
9168 		/*
9169 		 * We treat non-persistent link similarly as the persistent
9170 		 * link case, in terms of plumbing/unplumbing, as well as
9171 		 * dynamic re-plumbing events indicator.  See comments
9172 		 * in ip_sioctl_plink() for more.
9173 		 *
9174 		 * Request can be enqueued in the 'ipsq' while waiting
9175 		 * to become exclusive. So bump up the conn ref.
9176 		 */
9177 		if (CONN_Q(q))
9178 			CONN_INC_REF(Q_TO_CONN(q));
9179 		ip_sioctl_plink(NULL, q, mp, NULL);
9180 		return;
9181 
9182 	case IP_IOCTL:
9183 		ip_wput_ioctl(q, mp);
9184 		return;
9185 
9186 	case SIOCILB:
9187 		/* The ioctl length varies depending on the ILB command. */
9188 		copyin_size = iocp->ioc_count;
9189 		if (copyin_size < sizeof (ilb_cmd_t))
9190 			goto nak;
9191 		mi_copyin(q, mp, NULL, copyin_size);
9192 		return;
9193 
9194 	default:
9195 		cmn_err(CE_PANIC, "should not happen ");
9196 	}
9197 nak:
9198 	if (mp->b_cont != NULL) {
9199 		freemsg(mp->b_cont);
9200 		mp->b_cont = NULL;
9201 	}
9202 	iocp->ioc_error = EINVAL;
9203 	mp->b_datap->db_type = M_IOCNAK;
9204 	iocp->ioc_count = 0;
9205 	qreply(q, mp);
9206 }
9207 
9208 static void
9209 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9210 {
9211 	struct arpreq *ar;
9212 	struct xarpreq *xar;
9213 	mblk_t	*tmp;
9214 	struct iocblk *iocp;
9215 	int x_arp_ioctl = B_FALSE;
9216 	int *flagsp;
9217 	char *storage = NULL;
9218 
9219 	ASSERT(ill != NULL);
9220 
9221 	iocp = (struct iocblk *)mp->b_rptr;
9222 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9223 
9224 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9225 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9226 	    (iocp->ioc_cmd == SIOCSXARP)) {
9227 		x_arp_ioctl = B_TRUE;
9228 		xar = (struct xarpreq *)tmp->b_rptr;
9229 		flagsp = &xar->xarp_flags;
9230 		storage = xar->xarp_ha.sdl_data;
9231 	} else {
9232 		ar = (struct arpreq *)tmp->b_rptr;
9233 		flagsp = &ar->arp_flags;
9234 		storage = ar->arp_ha.sa_data;
9235 	}
9236 
9237 	/*
9238 	 * We're done if this is not an SIOCG{X}ARP
9239 	 */
9240 	if (x_arp_ioctl) {
9241 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9242 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9243 		    sizeof (xar->xarp_ha.sdl_data)) {
9244 			iocp->ioc_error = EINVAL;
9245 			return;
9246 		}
9247 	}
9248 	*flagsp = ATF_INUSE;
9249 	/*
9250 	 * If /sbin/arp told us we are the authority using the "permanent"
9251 	 * flag, or if this is one of my addresses print "permanent"
9252 	 * in the /sbin/arp output.
9253 	 */
9254 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9255 		*flagsp |= ATF_AUTHORITY;
9256 	if (flags & NCE_F_NONUD)
9257 		*flagsp |= ATF_PERM; /* not subject to aging */
9258 	if (flags & NCE_F_PUBLISH)
9259 		*flagsp |= ATF_PUBL;
9260 	if (hwaddr != NULL) {
9261 		*flagsp |= ATF_COM;
9262 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9263 	}
9264 }
9265 
9266 /*
9267  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9268  * interface) create the next available logical interface for this
9269  * physical interface.
9270  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9271  * ipif with the specified name.
9272  *
9273  * If the address family is not AF_UNSPEC then set the address as well.
9274  *
9275  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9276  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9277  *
9278  * Executed as a writer on the ill.
9279  * So no lock is needed to traverse the ipif chain, or examine the
9280  * phyint flags.
9281  */
9282 /* ARGSUSED */
9283 int
9284 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9285     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9286 {
9287 	mblk_t	*mp1;
9288 	struct lifreq *lifr;
9289 	boolean_t	isv6;
9290 	boolean_t	exists;
9291 	char 	*name;
9292 	char	*endp;
9293 	char	*cp;
9294 	int	namelen;
9295 	ipif_t	*ipif;
9296 	long	id;
9297 	ipsq_t	*ipsq;
9298 	ill_t	*ill;
9299 	sin_t	*sin;
9300 	int	err = 0;
9301 	boolean_t found_sep = B_FALSE;
9302 	conn_t	*connp;
9303 	zoneid_t zoneid;
9304 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9305 
9306 	ASSERT(q->q_next == NULL);
9307 	ip1dbg(("ip_sioctl_addif\n"));
9308 	/* Existence of mp1 has been checked in ip_wput_nondata */
9309 	mp1 = mp->b_cont->b_cont;
9310 	/*
9311 	 * Null terminate the string to protect against buffer
9312 	 * overrun. String was generated by user code and may not
9313 	 * be trusted.
9314 	 */
9315 	lifr = (struct lifreq *)mp1->b_rptr;
9316 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9317 	name = lifr->lifr_name;
9318 	ASSERT(CONN_Q(q));
9319 	connp = Q_TO_CONN(q);
9320 	isv6 = (connp->conn_family == AF_INET6);
9321 	zoneid = connp->conn_zoneid;
9322 	namelen = mi_strlen(name);
9323 	if (namelen == 0)
9324 		return (EINVAL);
9325 
9326 	exists = B_FALSE;
9327 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9328 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9329 		/*
9330 		 * Allow creating lo0 using SIOCLIFADDIF.
9331 		 * can't be any other writer thread. So can pass null below
9332 		 * for the last 4 args to ipif_lookup_name.
9333 		 */
9334 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9335 		    &exists, isv6, zoneid, ipst);
9336 		/* Prevent any further action */
9337 		if (ipif == NULL) {
9338 			return (ENOBUFS);
9339 		} else if (!exists) {
9340 			/* We created the ipif now and as writer */
9341 			ipif_refrele(ipif);
9342 			return (0);
9343 		} else {
9344 			ill = ipif->ipif_ill;
9345 			ill_refhold(ill);
9346 			ipif_refrele(ipif);
9347 		}
9348 	} else {
9349 		/* Look for a colon in the name. */
9350 		endp = &name[namelen];
9351 		for (cp = endp; --cp > name; ) {
9352 			if (*cp == IPIF_SEPARATOR_CHAR) {
9353 				found_sep = B_TRUE;
9354 				/*
9355 				 * Reject any non-decimal aliases for plumbing
9356 				 * of logical interfaces. Aliases with leading
9357 				 * zeroes are also rejected as they introduce
9358 				 * ambiguity in the naming of the interfaces.
9359 				 * Comparing with "0" takes care of all such
9360 				 * cases.
9361 				 */
9362 				if ((strncmp("0", cp+1, 1)) == 0)
9363 					return (EINVAL);
9364 
9365 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9366 				    id <= 0 || *endp != '\0') {
9367 					return (EINVAL);
9368 				}
9369 				*cp = '\0';
9370 				break;
9371 			}
9372 		}
9373 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9374 		if (found_sep)
9375 			*cp = IPIF_SEPARATOR_CHAR;
9376 		if (ill == NULL)
9377 			return (ENXIO);
9378 	}
9379 
9380 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9381 	    B_TRUE);
9382 
9383 	/*
9384 	 * Release the refhold due to the lookup, now that we are excl
9385 	 * or we are just returning
9386 	 */
9387 	ill_refrele(ill);
9388 
9389 	if (ipsq == NULL)
9390 		return (EINPROGRESS);
9391 
9392 	/* We are now exclusive on the IPSQ */
9393 	ASSERT(IAM_WRITER_ILL(ill));
9394 
9395 	if (found_sep) {
9396 		/* Now see if there is an IPIF with this unit number. */
9397 		for (ipif = ill->ill_ipif; ipif != NULL;
9398 		    ipif = ipif->ipif_next) {
9399 			if (ipif->ipif_id == id) {
9400 				err = EEXIST;
9401 				goto done;
9402 			}
9403 		}
9404 	}
9405 
9406 	/*
9407 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9408 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9409 	 * instead.
9410 	 */
9411 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9412 	    B_TRUE, B_TRUE, &err)) == NULL) {
9413 		goto done;
9414 	}
9415 
9416 	/* Return created name with ioctl */
9417 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9418 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9419 	ip1dbg(("created %s\n", lifr->lifr_name));
9420 
9421 	/* Set address */
9422 	sin = (sin_t *)&lifr->lifr_addr;
9423 	if (sin->sin_family != AF_UNSPEC) {
9424 		err = ip_sioctl_addr(ipif, sin, q, mp,
9425 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9426 	}
9427 
9428 done:
9429 	ipsq_exit(ipsq);
9430 	return (err);
9431 }
9432 
9433 /*
9434  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9435  * interface) delete it based on the IP address (on this physical interface).
9436  * Otherwise delete it based on the ipif_id.
9437  * Also, special handling to allow a removeif of lo0.
9438  */
9439 /* ARGSUSED */
9440 int
9441 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9442     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9443 {
9444 	conn_t		*connp;
9445 	ill_t		*ill = ipif->ipif_ill;
9446 	boolean_t	 success;
9447 	ip_stack_t	*ipst;
9448 
9449 	ipst = CONNQ_TO_IPST(q);
9450 
9451 	ASSERT(q->q_next == NULL);
9452 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9453 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9454 	ASSERT(IAM_WRITER_IPIF(ipif));
9455 
9456 	connp = Q_TO_CONN(q);
9457 	/*
9458 	 * Special case for unplumbing lo0 (the loopback physical interface).
9459 	 * If unplumbing lo0, the incoming address structure has been
9460 	 * initialized to all zeros. When unplumbing lo0, all its logical
9461 	 * interfaces must be removed too.
9462 	 *
9463 	 * Note that this interface may be called to remove a specific
9464 	 * loopback logical interface (eg, lo0:1). But in that case
9465 	 * ipif->ipif_id != 0 so that the code path for that case is the
9466 	 * same as any other interface (meaning it skips the code directly
9467 	 * below).
9468 	 */
9469 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9470 		if (sin->sin_family == AF_UNSPEC &&
9471 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9472 			/*
9473 			 * Mark it condemned. No new ref. will be made to ill.
9474 			 */
9475 			mutex_enter(&ill->ill_lock);
9476 			ill->ill_state_flags |= ILL_CONDEMNED;
9477 			for (ipif = ill->ill_ipif; ipif != NULL;
9478 			    ipif = ipif->ipif_next) {
9479 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9480 			}
9481 			mutex_exit(&ill->ill_lock);
9482 
9483 			ipif = ill->ill_ipif;
9484 			/* unplumb the loopback interface */
9485 			ill_delete(ill);
9486 			mutex_enter(&connp->conn_lock);
9487 			mutex_enter(&ill->ill_lock);
9488 
9489 			/* Are any references to this ill active */
9490 			if (ill_is_freeable(ill)) {
9491 				mutex_exit(&ill->ill_lock);
9492 				mutex_exit(&connp->conn_lock);
9493 				ill_delete_tail(ill);
9494 				mi_free(ill);
9495 				return (0);
9496 			}
9497 			success = ipsq_pending_mp_add(connp, ipif,
9498 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9499 			mutex_exit(&connp->conn_lock);
9500 			mutex_exit(&ill->ill_lock);
9501 			if (success)
9502 				return (EINPROGRESS);
9503 			else
9504 				return (EINTR);
9505 		}
9506 	}
9507 
9508 	if (ipif->ipif_id == 0) {
9509 		ipsq_t *ipsq;
9510 
9511 		/* Find based on address */
9512 		if (ipif->ipif_isv6) {
9513 			sin6_t *sin6;
9514 
9515 			if (sin->sin_family != AF_INET6)
9516 				return (EAFNOSUPPORT);
9517 
9518 			sin6 = (sin6_t *)sin;
9519 			/* We are a writer, so we should be able to lookup */
9520 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9521 			    ipst);
9522 		} else {
9523 			if (sin->sin_family != AF_INET)
9524 				return (EAFNOSUPPORT);
9525 
9526 			/* We are a writer, so we should be able to lookup */
9527 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9528 			    ipst);
9529 		}
9530 		if (ipif == NULL) {
9531 			return (EADDRNOTAVAIL);
9532 		}
9533 
9534 		/*
9535 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9536 		 * lifr_name of the physical interface but with an ip address
9537 		 * lifr_addr of a logical interface plumbed over it.
9538 		 * So update ipx_current_ipif now that ipif points to the
9539 		 * correct one.
9540 		 */
9541 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9542 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9543 
9544 		/* This is a writer */
9545 		ipif_refrele(ipif);
9546 	}
9547 
9548 	/*
9549 	 * Can not delete instance zero since it is tied to the ill.
9550 	 */
9551 	if (ipif->ipif_id == 0)
9552 		return (EBUSY);
9553 
9554 	mutex_enter(&ill->ill_lock);
9555 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9556 	mutex_exit(&ill->ill_lock);
9557 
9558 	ipif_free(ipif);
9559 
9560 	mutex_enter(&connp->conn_lock);
9561 	mutex_enter(&ill->ill_lock);
9562 
9563 	/* Are any references to this ipif active */
9564 	if (ipif_is_freeable(ipif)) {
9565 		mutex_exit(&ill->ill_lock);
9566 		mutex_exit(&connp->conn_lock);
9567 		ipif_non_duplicate(ipif);
9568 		(void) ipif_down_tail(ipif);
9569 		ipif_free_tail(ipif); /* frees ipif */
9570 		return (0);
9571 	}
9572 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9573 	    IPIF_FREE);
9574 	mutex_exit(&ill->ill_lock);
9575 	mutex_exit(&connp->conn_lock);
9576 	if (success)
9577 		return (EINPROGRESS);
9578 	else
9579 		return (EINTR);
9580 }
9581 
9582 /*
9583  * Restart the removeif ioctl. The refcnt has gone down to 0.
9584  * The ipif is already condemned. So can't find it thru lookups.
9585  */
9586 /* ARGSUSED */
9587 int
9588 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9589     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9590 {
9591 	ill_t *ill = ipif->ipif_ill;
9592 
9593 	ASSERT(IAM_WRITER_IPIF(ipif));
9594 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9595 
9596 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9597 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9598 
9599 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9600 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9601 		ill_delete_tail(ill);
9602 		mi_free(ill);
9603 		return (0);
9604 	}
9605 
9606 	ipif_non_duplicate(ipif);
9607 	(void) ipif_down_tail(ipif);
9608 	ipif_free_tail(ipif);
9609 
9610 	return (0);
9611 }
9612 
9613 /*
9614  * Set the local interface address using the given prefix and ill_token.
9615  */
9616 /* ARGSUSED */
9617 int
9618 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9619     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9620 {
9621 	int err;
9622 	in6_addr_t v6addr;
9623 	sin6_t *sin6;
9624 	ill_t *ill;
9625 	int i;
9626 
9627 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9628 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9629 
9630 	ASSERT(IAM_WRITER_IPIF(ipif));
9631 
9632 	if (!ipif->ipif_isv6)
9633 		return (EINVAL);
9634 
9635 	if (sin->sin_family != AF_INET6)
9636 		return (EAFNOSUPPORT);
9637 
9638 	sin6 = (sin6_t *)sin;
9639 	v6addr = sin6->sin6_addr;
9640 	ill = ipif->ipif_ill;
9641 
9642 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9643 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9644 		return (EADDRNOTAVAIL);
9645 
9646 	for (i = 0; i < 4; i++)
9647 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9648 
9649 	err = ip_sioctl_addr(ipif, sin, q, mp,
9650 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9651 	return (err);
9652 }
9653 
9654 /*
9655  * Restart entry point to restart the address set operation after the
9656  * refcounts have dropped to zero.
9657  */
9658 /* ARGSUSED */
9659 int
9660 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9661     ip_ioctl_cmd_t *ipip, void *ifreq)
9662 {
9663 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9664 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9665 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9666 }
9667 
9668 /*
9669  * Set the local interface address.
9670  * Allow an address of all zero when the interface is down.
9671  */
9672 /* ARGSUSED */
9673 int
9674 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9675     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9676 {
9677 	int err = 0;
9678 	in6_addr_t v6addr;
9679 	boolean_t need_up = B_FALSE;
9680 
9681 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9682 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9683 
9684 	ASSERT(IAM_WRITER_IPIF(ipif));
9685 
9686 	if (ipif->ipif_isv6) {
9687 		sin6_t *sin6;
9688 		ill_t *ill;
9689 		phyint_t *phyi;
9690 
9691 		if (sin->sin_family != AF_INET6)
9692 			return (EAFNOSUPPORT);
9693 
9694 		sin6 = (sin6_t *)sin;
9695 		v6addr = sin6->sin6_addr;
9696 		ill = ipif->ipif_ill;
9697 		phyi = ill->ill_phyint;
9698 
9699 		/*
9700 		 * Enforce that true multicast interfaces have a link-local
9701 		 * address for logical unit 0.
9702 		 *
9703 		 * However for those ipif's for which link-local address was
9704 		 * not created by default, also allow setting :: as the address.
9705 		 * This scenario would arise, when we delete an address on ipif
9706 		 * with logical unit 0, we would want to set :: as the address.
9707 		 */
9708 		if (ipif->ipif_id == 0 &&
9709 		    (ill->ill_flags & ILLF_MULTICAST) &&
9710 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9711 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9712 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9713 
9714 			/*
9715 			 * if default link-local was not created by kernel for
9716 			 * this ill, allow setting :: as the address on ipif:0.
9717 			 */
9718 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9719 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9720 					return (EADDRNOTAVAIL);
9721 			} else {
9722 				return (EADDRNOTAVAIL);
9723 			}
9724 		}
9725 
9726 		/*
9727 		 * up interfaces shouldn't have the unspecified address
9728 		 * unless they also have the IPIF_NOLOCAL flags set and
9729 		 * have a subnet assigned.
9730 		 */
9731 		if ((ipif->ipif_flags & IPIF_UP) &&
9732 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9733 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9734 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9735 			return (EADDRNOTAVAIL);
9736 		}
9737 
9738 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9739 			return (EADDRNOTAVAIL);
9740 	} else {
9741 		ipaddr_t addr;
9742 
9743 		if (sin->sin_family != AF_INET)
9744 			return (EAFNOSUPPORT);
9745 
9746 		addr = sin->sin_addr.s_addr;
9747 
9748 		/* Allow INADDR_ANY as the local address. */
9749 		if (addr != INADDR_ANY &&
9750 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9751 			return (EADDRNOTAVAIL);
9752 
9753 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9754 	}
9755 
9756 	/*
9757 	 * Even if there is no change we redo things just to rerun
9758 	 * ipif_set_default.
9759 	 */
9760 	if (ipif->ipif_flags & IPIF_UP) {
9761 		/*
9762 		 * Setting a new local address, make sure
9763 		 * we have net and subnet bcast ire's for
9764 		 * the old address if we need them.
9765 		 */
9766 		/*
9767 		 * If the interface is already marked up,
9768 		 * we call ipif_down which will take care
9769 		 * of ditching any IREs that have been set
9770 		 * up based on the old interface address.
9771 		 */
9772 		err = ipif_logical_down(ipif, q, mp);
9773 		if (err == EINPROGRESS)
9774 			return (err);
9775 		(void) ipif_down_tail(ipif);
9776 		need_up = 1;
9777 	}
9778 
9779 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9780 	return (err);
9781 }
9782 
9783 int
9784 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9785     boolean_t need_up)
9786 {
9787 	in6_addr_t v6addr;
9788 	in6_addr_t ov6addr;
9789 	ipaddr_t addr;
9790 	sin6_t	*sin6;
9791 	int	sinlen;
9792 	int	err = 0;
9793 	ill_t	*ill = ipif->ipif_ill;
9794 	boolean_t need_dl_down;
9795 	boolean_t need_arp_down;
9796 	struct iocblk *iocp;
9797 
9798 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9799 
9800 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9801 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9802 	ASSERT(IAM_WRITER_IPIF(ipif));
9803 
9804 	/* Must cancel any pending timer before taking the ill_lock */
9805 	if (ipif->ipif_recovery_id != 0)
9806 		(void) untimeout(ipif->ipif_recovery_id);
9807 	ipif->ipif_recovery_id = 0;
9808 
9809 	if (ipif->ipif_isv6) {
9810 		sin6 = (sin6_t *)sin;
9811 		v6addr = sin6->sin6_addr;
9812 		sinlen = sizeof (struct sockaddr_in6);
9813 	} else {
9814 		addr = sin->sin_addr.s_addr;
9815 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9816 		sinlen = sizeof (struct sockaddr_in);
9817 	}
9818 	mutex_enter(&ill->ill_lock);
9819 	ov6addr = ipif->ipif_v6lcl_addr;
9820 	ipif->ipif_v6lcl_addr = v6addr;
9821 	sctp_update_ipif_addr(ipif, ov6addr);
9822 	ipif->ipif_addr_ready = 0;
9823 
9824 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9825 
9826 	/*
9827 	 * If the interface was previously marked as a duplicate, then since
9828 	 * we've now got a "new" address, it should no longer be considered a
9829 	 * duplicate -- even if the "new" address is the same as the old one.
9830 	 * Note that if all ipifs are down, we may have a pending ARP down
9831 	 * event to handle.  This is because we want to recover from duplicates
9832 	 * and thus delay tearing down ARP until the duplicates have been
9833 	 * removed or disabled.
9834 	 */
9835 	need_dl_down = need_arp_down = B_FALSE;
9836 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9837 		need_arp_down = !need_up;
9838 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9839 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9840 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9841 			need_dl_down = B_TRUE;
9842 		}
9843 	}
9844 
9845 	ipif_set_default(ipif);
9846 
9847 	/*
9848 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9849 	 * tag the ill so that future updates to the interface ID don't result
9850 	 * in this address getting automatically reconfigured from under the
9851 	 * administrator.
9852 	 */
9853 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9854 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9855 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9856 			ill->ill_manual_linklocal = 1;
9857 	}
9858 
9859 	/*
9860 	 * When publishing an interface address change event, we only notify
9861 	 * the event listeners of the new address.  It is assumed that if they
9862 	 * actively care about the addresses assigned that they will have
9863 	 * already discovered the previous address assigned (if there was one.)
9864 	 *
9865 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9866 	 */
9867 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9868 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9869 		    NE_ADDRESS_CHANGE, sin, sinlen);
9870 	}
9871 
9872 	mutex_exit(&ill->ill_lock);
9873 
9874 	if (need_up) {
9875 		/*
9876 		 * Now bring the interface back up.  If this
9877 		 * is the only IPIF for the ILL, ipif_up
9878 		 * will have to re-bind to the device, so
9879 		 * we may get back EINPROGRESS, in which
9880 		 * case, this IOCTL will get completed in
9881 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9882 		 */
9883 		err = ipif_up(ipif, q, mp);
9884 	} else {
9885 		/* Perhaps ilgs should use this ill */
9886 		update_conn_ill(NULL, ill->ill_ipst);
9887 	}
9888 
9889 	if (need_dl_down)
9890 		ill_dl_down(ill);
9891 
9892 	if (need_arp_down && !ill->ill_isv6)
9893 		(void) ipif_arp_down(ipif);
9894 
9895 	/*
9896 	 * The default multicast interface might have changed (for
9897 	 * instance if the IPv6 scope of the address changed)
9898 	 */
9899 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9900 
9901 	return (err);
9902 }
9903 
9904 /*
9905  * Restart entry point to restart the address set operation after the
9906  * refcounts have dropped to zero.
9907  */
9908 /* ARGSUSED */
9909 int
9910 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9911     ip_ioctl_cmd_t *ipip, void *ifreq)
9912 {
9913 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9914 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9915 	ASSERT(IAM_WRITER_IPIF(ipif));
9916 	(void) ipif_down_tail(ipif);
9917 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9918 }
9919 
9920 /* ARGSUSED */
9921 int
9922 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9923     ip_ioctl_cmd_t *ipip, void *if_req)
9924 {
9925 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9926 	struct lifreq *lifr = (struct lifreq *)if_req;
9927 
9928 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9929 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9930 	/*
9931 	 * The net mask and address can't change since we have a
9932 	 * reference to the ipif. So no lock is necessary.
9933 	 */
9934 	if (ipif->ipif_isv6) {
9935 		*sin6 = sin6_null;
9936 		sin6->sin6_family = AF_INET6;
9937 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9938 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9939 		lifr->lifr_addrlen =
9940 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9941 	} else {
9942 		*sin = sin_null;
9943 		sin->sin_family = AF_INET;
9944 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9945 		if (ipip->ipi_cmd_type == LIF_CMD) {
9946 			lifr->lifr_addrlen =
9947 			    ip_mask_to_plen(ipif->ipif_net_mask);
9948 		}
9949 	}
9950 	return (0);
9951 }
9952 
9953 /*
9954  * Set the destination address for a pt-pt interface.
9955  */
9956 /* ARGSUSED */
9957 int
9958 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9959     ip_ioctl_cmd_t *ipip, void *if_req)
9960 {
9961 	int err = 0;
9962 	in6_addr_t v6addr;
9963 	boolean_t need_up = B_FALSE;
9964 
9965 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9966 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9967 	ASSERT(IAM_WRITER_IPIF(ipif));
9968 
9969 	if (ipif->ipif_isv6) {
9970 		sin6_t *sin6;
9971 
9972 		if (sin->sin_family != AF_INET6)
9973 			return (EAFNOSUPPORT);
9974 
9975 		sin6 = (sin6_t *)sin;
9976 		v6addr = sin6->sin6_addr;
9977 
9978 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9979 			return (EADDRNOTAVAIL);
9980 	} else {
9981 		ipaddr_t addr;
9982 
9983 		if (sin->sin_family != AF_INET)
9984 			return (EAFNOSUPPORT);
9985 
9986 		addr = sin->sin_addr.s_addr;
9987 		if (addr != INADDR_ANY &&
9988 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
9989 			return (EADDRNOTAVAIL);
9990 		}
9991 
9992 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9993 	}
9994 
9995 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9996 		return (0);	/* No change */
9997 
9998 	if (ipif->ipif_flags & IPIF_UP) {
9999 		/*
10000 		 * If the interface is already marked up,
10001 		 * we call ipif_down which will take care
10002 		 * of ditching any IREs that have been set
10003 		 * up based on the old pp dst address.
10004 		 */
10005 		err = ipif_logical_down(ipif, q, mp);
10006 		if (err == EINPROGRESS)
10007 			return (err);
10008 		(void) ipif_down_tail(ipif);
10009 		need_up = B_TRUE;
10010 	}
10011 	/*
10012 	 * could return EINPROGRESS. If so ioctl will complete in
10013 	 * ip_rput_dlpi_writer
10014 	 */
10015 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10016 	return (err);
10017 }
10018 
10019 static int
10020 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10021     boolean_t need_up)
10022 {
10023 	in6_addr_t v6addr;
10024 	ill_t	*ill = ipif->ipif_ill;
10025 	int	err = 0;
10026 	boolean_t need_dl_down;
10027 	boolean_t need_arp_down;
10028 
10029 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10030 	    ipif->ipif_id, (void *)ipif));
10031 
10032 	/* Must cancel any pending timer before taking the ill_lock */
10033 	if (ipif->ipif_recovery_id != 0)
10034 		(void) untimeout(ipif->ipif_recovery_id);
10035 	ipif->ipif_recovery_id = 0;
10036 
10037 	if (ipif->ipif_isv6) {
10038 		sin6_t *sin6;
10039 
10040 		sin6 = (sin6_t *)sin;
10041 		v6addr = sin6->sin6_addr;
10042 	} else {
10043 		ipaddr_t addr;
10044 
10045 		addr = sin->sin_addr.s_addr;
10046 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10047 	}
10048 	mutex_enter(&ill->ill_lock);
10049 	/* Set point to point destination address. */
10050 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10051 		/*
10052 		 * Allow this as a means of creating logical
10053 		 * pt-pt interfaces on top of e.g. an Ethernet.
10054 		 * XXX Undocumented HACK for testing.
10055 		 * pt-pt interfaces are created with NUD disabled.
10056 		 */
10057 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10058 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10059 		if (ipif->ipif_isv6)
10060 			ill->ill_flags |= ILLF_NONUD;
10061 	}
10062 
10063 	/*
10064 	 * If the interface was previously marked as a duplicate, then since
10065 	 * we've now got a "new" address, it should no longer be considered a
10066 	 * duplicate -- even if the "new" address is the same as the old one.
10067 	 * Note that if all ipifs are down, we may have a pending ARP down
10068 	 * event to handle.
10069 	 */
10070 	need_dl_down = need_arp_down = B_FALSE;
10071 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10072 		need_arp_down = !need_up;
10073 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10074 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10075 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10076 			need_dl_down = B_TRUE;
10077 		}
10078 	}
10079 
10080 	/*
10081 	 * If we've just manually set the IPv6 destination link-local address
10082 	 * (0th ipif), tag the ill so that future updates to the destination
10083 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10084 	 * result in this address getting automatically reconfigured from
10085 	 * under the administrator.
10086 	 */
10087 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10088 		ill->ill_manual_dst_linklocal = 1;
10089 
10090 	/* Set the new address. */
10091 	ipif->ipif_v6pp_dst_addr = v6addr;
10092 	/* Make sure subnet tracks pp_dst */
10093 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10094 	mutex_exit(&ill->ill_lock);
10095 
10096 	if (need_up) {
10097 		/*
10098 		 * Now bring the interface back up.  If this
10099 		 * is the only IPIF for the ILL, ipif_up
10100 		 * will have to re-bind to the device, so
10101 		 * we may get back EINPROGRESS, in which
10102 		 * case, this IOCTL will get completed in
10103 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10104 		 */
10105 		err = ipif_up(ipif, q, mp);
10106 	}
10107 
10108 	if (need_dl_down)
10109 		ill_dl_down(ill);
10110 	if (need_arp_down && !ipif->ipif_isv6)
10111 		(void) ipif_arp_down(ipif);
10112 
10113 	return (err);
10114 }
10115 
10116 /*
10117  * Restart entry point to restart the dstaddress set operation after the
10118  * refcounts have dropped to zero.
10119  */
10120 /* ARGSUSED */
10121 int
10122 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10123     ip_ioctl_cmd_t *ipip, void *ifreq)
10124 {
10125 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10126 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10127 	(void) ipif_down_tail(ipif);
10128 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10129 }
10130 
10131 /* ARGSUSED */
10132 int
10133 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10134     ip_ioctl_cmd_t *ipip, void *if_req)
10135 {
10136 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10137 
10138 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10139 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10140 	/*
10141 	 * Get point to point destination address. The addresses can't
10142 	 * change since we hold a reference to the ipif.
10143 	 */
10144 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10145 		return (EADDRNOTAVAIL);
10146 
10147 	if (ipif->ipif_isv6) {
10148 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10149 		*sin6 = sin6_null;
10150 		sin6->sin6_family = AF_INET6;
10151 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10152 	} else {
10153 		*sin = sin_null;
10154 		sin->sin_family = AF_INET;
10155 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10156 	}
10157 	return (0);
10158 }
10159 
10160 /*
10161  * Check which flags will change by the given flags being set
10162  * silently ignore flags which userland is not allowed to control.
10163  * (Because these flags may change between SIOCGLIFFLAGS and
10164  * SIOCSLIFFLAGS, and that's outside of userland's control,
10165  * we need to silently ignore them rather than fail.)
10166  */
10167 static void
10168 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10169     uint64_t *offp)
10170 {
10171 	ill_t		*ill = ipif->ipif_ill;
10172 	phyint_t 	*phyi = ill->ill_phyint;
10173 	uint64_t	cantchange_flags, intf_flags;
10174 	uint64_t	turn_on, turn_off;
10175 
10176 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10177 	cantchange_flags = IFF_CANTCHANGE;
10178 	if (IS_IPMP(ill))
10179 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10180 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10181 	turn_off = intf_flags & turn_on;
10182 	turn_on ^= turn_off;
10183 	*onp = turn_on;
10184 	*offp = turn_off;
10185 }
10186 
10187 /*
10188  * Set interface flags.  Many flags require special handling (e.g.,
10189  * bringing the interface down); see below for details.
10190  *
10191  * NOTE : We really don't enforce that ipif_id zero should be used
10192  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10193  *	  is because applications generally does SICGLIFFLAGS and
10194  *	  ORs in the new flags (that affects the logical) and does a
10195  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10196  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10197  *	  flags that will be turned on is correct with respect to
10198  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10199  */
10200 /* ARGSUSED */
10201 int
10202 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10203     ip_ioctl_cmd_t *ipip, void *if_req)
10204 {
10205 	uint64_t turn_on;
10206 	uint64_t turn_off;
10207 	int	err = 0;
10208 	phyint_t *phyi;
10209 	ill_t *ill;
10210 	conn_t *connp;
10211 	uint64_t intf_flags;
10212 	boolean_t phyint_flags_modified = B_FALSE;
10213 	uint64_t flags;
10214 	struct ifreq *ifr;
10215 	struct lifreq *lifr;
10216 	boolean_t set_linklocal = B_FALSE;
10217 
10218 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10219 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10220 
10221 	ASSERT(IAM_WRITER_IPIF(ipif));
10222 
10223 	ill = ipif->ipif_ill;
10224 	phyi = ill->ill_phyint;
10225 
10226 	if (ipip->ipi_cmd_type == IF_CMD) {
10227 		ifr = (struct ifreq *)if_req;
10228 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10229 	} else {
10230 		lifr = (struct lifreq *)if_req;
10231 		flags = lifr->lifr_flags;
10232 	}
10233 
10234 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10235 
10236 	/*
10237 	 * Have the flags been set correctly until now?
10238 	 */
10239 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10240 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10241 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10242 	/*
10243 	 * Compare the new flags to the old, and partition
10244 	 * into those coming on and those going off.
10245 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10246 	 */
10247 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10248 		flags |= intf_flags & ~0xFFFF;
10249 
10250 	/*
10251 	 * Explicitly fail attempts to change flags that are always invalid on
10252 	 * an IPMP meta-interface.
10253 	 */
10254 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10255 		return (EINVAL);
10256 
10257 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10258 	if ((turn_on|turn_off) == 0)
10259 		return (0);	/* No change */
10260 
10261 	/*
10262 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10263 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10264 	 * allow it to be turned off.
10265 	 */
10266 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10267 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10268 		return (EINVAL);
10269 
10270 	if ((connp = Q_TO_CONN(q)) == NULL)
10271 		return (EINVAL);
10272 
10273 	/*
10274 	 * Only vrrp control socket is allowed to change IFF_UP and
10275 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10276 	 */
10277 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10278 		if (!connp->conn_isvrrp)
10279 			return (EINVAL);
10280 	}
10281 
10282 	/*
10283 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10284 	 * VRRP control socket.
10285 	 */
10286 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10287 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10288 			return (EINVAL);
10289 	}
10290 
10291 	if (turn_on & IFF_NOFAILOVER) {
10292 		turn_on |= IFF_DEPRECATED;
10293 		flags |= IFF_DEPRECATED;
10294 	}
10295 
10296 	/*
10297 	 * On underlying interfaces, only allow applications to manage test
10298 	 * addresses -- otherwise, they may get confused when the address
10299 	 * moves as part of being brought up.  Likewise, prevent an
10300 	 * application-managed test address from being converted to a data
10301 	 * address.  To prevent migration of administratively up addresses in
10302 	 * the kernel, we don't allow them to be converted either.
10303 	 */
10304 	if (IS_UNDER_IPMP(ill)) {
10305 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10306 
10307 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10308 			return (EINVAL);
10309 
10310 		if ((turn_off & IFF_NOFAILOVER) &&
10311 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10312 			return (EINVAL);
10313 	}
10314 
10315 	/*
10316 	 * Only allow IFF_TEMPORARY flag to be set on
10317 	 * IPv6 interfaces.
10318 	 */
10319 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10320 		return (EINVAL);
10321 
10322 	/*
10323 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10324 	 */
10325 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10326 		return (EINVAL);
10327 
10328 	/*
10329 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10330 	 * interfaces.  It makes no sense in that context.
10331 	 */
10332 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10333 		return (EINVAL);
10334 
10335 	/*
10336 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10337 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10338 	 * If the link local address isn't set, and can be set, it will get
10339 	 * set later on in this function.
10340 	 */
10341 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10342 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10343 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10344 		if (ipif_cant_setlinklocal(ipif))
10345 			return (EINVAL);
10346 		set_linklocal = B_TRUE;
10347 	}
10348 
10349 	/*
10350 	 * If we modify physical interface flags, we'll potentially need to
10351 	 * send up two routing socket messages for the changes (one for the
10352 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10353 	 */
10354 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10355 		phyint_flags_modified = B_TRUE;
10356 
10357 	/*
10358 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10359 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10360 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10361 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10362 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10363 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10364 	 * will not be honored.
10365 	 */
10366 	if (turn_on & PHYI_STANDBY) {
10367 		/*
10368 		 * No need to grab ill_g_usesrc_lock here; see the
10369 		 * synchronization notes in ip.c.
10370 		 */
10371 		if (ill->ill_usesrc_grp_next != NULL ||
10372 		    intf_flags & PHYI_INACTIVE)
10373 			return (EINVAL);
10374 		if (!(flags & PHYI_FAILED)) {
10375 			flags |= PHYI_INACTIVE;
10376 			turn_on |= PHYI_INACTIVE;
10377 		}
10378 	}
10379 
10380 	if (turn_off & PHYI_STANDBY) {
10381 		flags &= ~PHYI_INACTIVE;
10382 		turn_off |= PHYI_INACTIVE;
10383 	}
10384 
10385 	/*
10386 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10387 	 * would end up on.
10388 	 */
10389 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10390 	    (PHYI_FAILED | PHYI_INACTIVE))
10391 		return (EINVAL);
10392 
10393 	/*
10394 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10395 	 * status of the interface.
10396 	 */
10397 	if ((turn_on | turn_off) & ILLF_ROUTER)
10398 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10399 
10400 	/*
10401 	 * If the interface is not UP and we are not going to
10402 	 * bring it UP, record the flags and return. When the
10403 	 * interface comes UP later, the right actions will be
10404 	 * taken.
10405 	 */
10406 	if (!(ipif->ipif_flags & IPIF_UP) &&
10407 	    !(turn_on & IPIF_UP)) {
10408 		/* Record new flags in their respective places. */
10409 		mutex_enter(&ill->ill_lock);
10410 		mutex_enter(&ill->ill_phyint->phyint_lock);
10411 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10412 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10413 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10414 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10415 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10416 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10417 		mutex_exit(&ill->ill_lock);
10418 		mutex_exit(&ill->ill_phyint->phyint_lock);
10419 
10420 		/*
10421 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10422 		 * same to the kernel: if any of them has been set by
10423 		 * userland, the interface cannot be used for data traffic.
10424 		 */
10425 		if ((turn_on|turn_off) &
10426 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10427 			ASSERT(!IS_IPMP(ill));
10428 			/*
10429 			 * It's possible the ill is part of an "anonymous"
10430 			 * IPMP group rather than a real group.  In that case,
10431 			 * there are no other interfaces in the group and thus
10432 			 * no need to call ipmp_phyint_refresh_active().
10433 			 */
10434 			if (IS_UNDER_IPMP(ill))
10435 				ipmp_phyint_refresh_active(phyi);
10436 		}
10437 
10438 		if (phyint_flags_modified) {
10439 			if (phyi->phyint_illv4 != NULL) {
10440 				ip_rts_ifmsg(phyi->phyint_illv4->
10441 				    ill_ipif, RTSQ_DEFAULT);
10442 			}
10443 			if (phyi->phyint_illv6 != NULL) {
10444 				ip_rts_ifmsg(phyi->phyint_illv6->
10445 				    ill_ipif, RTSQ_DEFAULT);
10446 			}
10447 		}
10448 		/* The default multicast interface might have changed */
10449 		ire_increment_multicast_generation(ill->ill_ipst,
10450 		    ill->ill_isv6);
10451 
10452 		return (0);
10453 	} else if (set_linklocal) {
10454 		mutex_enter(&ill->ill_lock);
10455 		if (set_linklocal)
10456 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10457 		mutex_exit(&ill->ill_lock);
10458 	}
10459 
10460 	/*
10461 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10462 	 * or point-to-point interfaces with an unspecified destination. We do
10463 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10464 	 * have a subnet assigned, which is how in.ndpd currently manages its
10465 	 * onlink prefix list when no addresses are configured with those
10466 	 * prefixes.
10467 	 */
10468 	if (ipif->ipif_isv6 &&
10469 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10470 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10471 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10472 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10473 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10474 		return (EINVAL);
10475 	}
10476 
10477 	/*
10478 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10479 	 * from being brought up.
10480 	 */
10481 	if (!ipif->ipif_isv6 &&
10482 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10483 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10484 		return (EINVAL);
10485 	}
10486 
10487 	/*
10488 	 * If we are going to change one or more of the flags that are
10489 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10490 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10491 	 * IPIF_NOFAILOVER, we will take special action.  This is
10492 	 * done by bring the ipif down, changing the flags and bringing
10493 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10494 	 * back up will trigger the address to be moved.
10495 	 *
10496 	 * If we are going to change IFF_NOACCEPT, we need to bring
10497 	 * all the ipifs down then bring them up again.	 The act of
10498 	 * bringing all the ipifs back up will trigger the local
10499 	 * ires being recreated with "no_accept" set/cleared.
10500 	 *
10501 	 * Note that ILLF_NOACCEPT is always set separately from the
10502 	 * other flags.
10503 	 */
10504 	if ((turn_on|turn_off) &
10505 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10506 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10507 	    IPIF_NOFAILOVER)) {
10508 		/*
10509 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10510 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10511 		 * entries shared between multiple ipifs on the same subnet.
10512 		 */
10513 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10514 		    !(turn_off & IPIF_UP)) {
10515 			if (ipif->ipif_flags & IPIF_UP)
10516 				ill->ill_logical_down = 1;
10517 			turn_on &= ~IPIF_UP;
10518 		}
10519 		err = ipif_down(ipif, q, mp);
10520 		ip1dbg(("ipif_down returns %d err ", err));
10521 		if (err == EINPROGRESS)
10522 			return (err);
10523 		(void) ipif_down_tail(ipif);
10524 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10525 		/*
10526 		 * If we can quiesce the ill, then continue.  If not, then
10527 		 * ip_sioctl_flags_tail() will be called from
10528 		 * ipif_ill_refrele_tail().
10529 		 */
10530 		ill_down_ipifs(ill, B_TRUE);
10531 
10532 		mutex_enter(&connp->conn_lock);
10533 		mutex_enter(&ill->ill_lock);
10534 		if (!ill_is_quiescent(ill)) {
10535 			boolean_t success;
10536 
10537 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10538 			    q, mp, ILL_DOWN);
10539 			mutex_exit(&ill->ill_lock);
10540 			mutex_exit(&connp->conn_lock);
10541 			return (success ? EINPROGRESS : EINTR);
10542 		}
10543 		mutex_exit(&ill->ill_lock);
10544 		mutex_exit(&connp->conn_lock);
10545 	}
10546 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10547 }
10548 
10549 static int
10550 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10551 {
10552 	ill_t	*ill;
10553 	phyint_t *phyi;
10554 	uint64_t turn_on, turn_off;
10555 	boolean_t phyint_flags_modified = B_FALSE;
10556 	int	err = 0;
10557 	boolean_t set_linklocal = B_FALSE;
10558 
10559 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10560 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10561 
10562 	ASSERT(IAM_WRITER_IPIF(ipif));
10563 
10564 	ill = ipif->ipif_ill;
10565 	phyi = ill->ill_phyint;
10566 
10567 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10568 
10569 	/*
10570 	 * IFF_UP is handled separately.
10571 	 */
10572 	turn_on &= ~IFF_UP;
10573 	turn_off &= ~IFF_UP;
10574 
10575 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10576 		phyint_flags_modified = B_TRUE;
10577 
10578 	/*
10579 	 * Now we change the flags. Track current value of
10580 	 * other flags in their respective places.
10581 	 */
10582 	mutex_enter(&ill->ill_lock);
10583 	mutex_enter(&phyi->phyint_lock);
10584 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10585 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10586 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10587 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10588 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10589 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10590 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10591 		set_linklocal = B_TRUE;
10592 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10593 	}
10594 
10595 	mutex_exit(&ill->ill_lock);
10596 	mutex_exit(&phyi->phyint_lock);
10597 
10598 	if (set_linklocal)
10599 		(void) ipif_setlinklocal(ipif);
10600 
10601 	/*
10602 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10603 	 * the kernel: if any of them has been set by userland, the interface
10604 	 * cannot be used for data traffic.
10605 	 */
10606 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10607 		ASSERT(!IS_IPMP(ill));
10608 		/*
10609 		 * It's possible the ill is part of an "anonymous" IPMP group
10610 		 * rather than a real group.  In that case, there are no other
10611 		 * interfaces in the group and thus no need for us to call
10612 		 * ipmp_phyint_refresh_active().
10613 		 */
10614 		if (IS_UNDER_IPMP(ill))
10615 			ipmp_phyint_refresh_active(phyi);
10616 	}
10617 
10618 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10619 		/*
10620 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10621 		 * ipifs that were brought down.
10622 		 *
10623 		 * The routing sockets messages are sent as the result
10624 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10625 		 * as well.
10626 		 */
10627 		err = ill_up_ipifs(ill, q, mp);
10628 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10629 		/*
10630 		 * XXX ipif_up really does not know whether a phyint flags
10631 		 * was modified or not. So, it sends up information on
10632 		 * only one routing sockets message. As we don't bring up
10633 		 * the interface and also set PHYI_ flags simultaneously
10634 		 * it should be okay.
10635 		 */
10636 		err = ipif_up(ipif, q, mp);
10637 	} else {
10638 		/*
10639 		 * Make sure routing socket sees all changes to the flags.
10640 		 * ipif_up_done* handles this when we use ipif_up.
10641 		 */
10642 		if (phyint_flags_modified) {
10643 			if (phyi->phyint_illv4 != NULL) {
10644 				ip_rts_ifmsg(phyi->phyint_illv4->
10645 				    ill_ipif, RTSQ_DEFAULT);
10646 			}
10647 			if (phyi->phyint_illv6 != NULL) {
10648 				ip_rts_ifmsg(phyi->phyint_illv6->
10649 				    ill_ipif, RTSQ_DEFAULT);
10650 			}
10651 		} else {
10652 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10653 		}
10654 		/*
10655 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10656 		 * this in need_up case.
10657 		 */
10658 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10659 	}
10660 
10661 	/* The default multicast interface might have changed */
10662 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10663 	return (err);
10664 }
10665 
10666 /*
10667  * Restart the flags operation now that the refcounts have dropped to zero.
10668  */
10669 /* ARGSUSED */
10670 int
10671 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10672     ip_ioctl_cmd_t *ipip, void *if_req)
10673 {
10674 	uint64_t flags;
10675 	struct ifreq *ifr = if_req;
10676 	struct lifreq *lifr = if_req;
10677 	uint64_t turn_on, turn_off;
10678 
10679 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10680 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10681 
10682 	if (ipip->ipi_cmd_type == IF_CMD) {
10683 		/* cast to uint16_t prevents unwanted sign extension */
10684 		flags = (uint16_t)ifr->ifr_flags;
10685 	} else {
10686 		flags = lifr->lifr_flags;
10687 	}
10688 
10689 	/*
10690 	 * If this function call is a result of the ILLF_NOACCEPT flag
10691 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10692 	 */
10693 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10694 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10695 		(void) ipif_down_tail(ipif);
10696 
10697 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10698 }
10699 
10700 /*
10701  * Can operate on either a module or a driver queue.
10702  */
10703 /* ARGSUSED */
10704 int
10705 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10706     ip_ioctl_cmd_t *ipip, void *if_req)
10707 {
10708 	/*
10709 	 * Has the flags been set correctly till now ?
10710 	 */
10711 	ill_t *ill = ipif->ipif_ill;
10712 	phyint_t *phyi = ill->ill_phyint;
10713 
10714 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10715 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10716 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10717 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10718 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10719 
10720 	/*
10721 	 * Need a lock since some flags can be set even when there are
10722 	 * references to the ipif.
10723 	 */
10724 	mutex_enter(&ill->ill_lock);
10725 	if (ipip->ipi_cmd_type == IF_CMD) {
10726 		struct ifreq *ifr = (struct ifreq *)if_req;
10727 
10728 		/* Get interface flags (low 16 only). */
10729 		ifr->ifr_flags = ((ipif->ipif_flags |
10730 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10731 	} else {
10732 		struct lifreq *lifr = (struct lifreq *)if_req;
10733 
10734 		/* Get interface flags. */
10735 		lifr->lifr_flags = ipif->ipif_flags |
10736 		    ill->ill_flags | phyi->phyint_flags;
10737 	}
10738 	mutex_exit(&ill->ill_lock);
10739 	return (0);
10740 }
10741 
10742 /*
10743  * We allow the MTU to be set on an ILL, but not have it be different
10744  * for different IPIFs since we don't actually send packets on IPIFs.
10745  */
10746 /* ARGSUSED */
10747 int
10748 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10749     ip_ioctl_cmd_t *ipip, void *if_req)
10750 {
10751 	int mtu;
10752 	int ip_min_mtu;
10753 	struct ifreq	*ifr;
10754 	struct lifreq *lifr;
10755 	ill_t	*ill;
10756 
10757 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10758 	    ipif->ipif_id, (void *)ipif));
10759 	if (ipip->ipi_cmd_type == IF_CMD) {
10760 		ifr = (struct ifreq *)if_req;
10761 		mtu = ifr->ifr_metric;
10762 	} else {
10763 		lifr = (struct lifreq *)if_req;
10764 		mtu = lifr->lifr_mtu;
10765 	}
10766 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10767 	if (ipif->ipif_id != 0)
10768 		return (EINVAL);
10769 
10770 	ill = ipif->ipif_ill;
10771 	if (ipif->ipif_isv6)
10772 		ip_min_mtu = IPV6_MIN_MTU;
10773 	else
10774 		ip_min_mtu = IP_MIN_MTU;
10775 
10776 	mutex_enter(&ill->ill_lock);
10777 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10778 		mutex_exit(&ill->ill_lock);
10779 		return (EINVAL);
10780 	}
10781 	/*
10782 	 * The dce and fragmentation code can handle changes to ill_mtu
10783 	 * concurrent with sending/fragmenting packets.
10784 	 */
10785 	ill->ill_mtu = mtu;
10786 	ill->ill_flags |= ILLF_FIXEDMTU;
10787 	mutex_exit(&ill->ill_lock);
10788 
10789 	/*
10790 	 * Make sure all dce_generation checks find out
10791 	 * that ill_mtu has changed.
10792 	 */
10793 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10794 
10795 	/*
10796 	 * Refresh IPMP meta-interface MTU if necessary.
10797 	 */
10798 	if (IS_UNDER_IPMP(ill))
10799 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10800 
10801 	/* Update the MTU in SCTP's list */
10802 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10803 	return (0);
10804 }
10805 
10806 /* Get interface MTU. */
10807 /* ARGSUSED */
10808 int
10809 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10810 	ip_ioctl_cmd_t *ipip, void *if_req)
10811 {
10812 	struct ifreq	*ifr;
10813 	struct lifreq	*lifr;
10814 
10815 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10817 
10818 	/*
10819 	 * We allow a get on any logical interface even though the set
10820 	 * can only be done on logical unit 0.
10821 	 */
10822 	if (ipip->ipi_cmd_type == IF_CMD) {
10823 		ifr = (struct ifreq *)if_req;
10824 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10825 	} else {
10826 		lifr = (struct lifreq *)if_req;
10827 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10828 	}
10829 	return (0);
10830 }
10831 
10832 /* Set interface broadcast address. */
10833 /* ARGSUSED2 */
10834 int
10835 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10836 	ip_ioctl_cmd_t *ipip, void *if_req)
10837 {
10838 	ipaddr_t addr;
10839 	ire_t	*ire;
10840 	ill_t		*ill = ipif->ipif_ill;
10841 	ip_stack_t	*ipst = ill->ill_ipst;
10842 
10843 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10844 	    ipif->ipif_id));
10845 
10846 	ASSERT(IAM_WRITER_IPIF(ipif));
10847 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10848 		return (EADDRNOTAVAIL);
10849 
10850 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10851 
10852 	if (sin->sin_family != AF_INET)
10853 		return (EAFNOSUPPORT);
10854 
10855 	addr = sin->sin_addr.s_addr;
10856 
10857 	if (ipif->ipif_flags & IPIF_UP) {
10858 		/*
10859 		 * If we are already up, make sure the new
10860 		 * broadcast address makes sense.  If it does,
10861 		 * there should be an IRE for it already.
10862 		 */
10863 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10864 		    ill, ipif->ipif_zoneid, NULL,
10865 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10866 		if (ire == NULL) {
10867 			return (EINVAL);
10868 		} else {
10869 			ire_refrele(ire);
10870 		}
10871 	}
10872 	/*
10873 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10874 	 * needs to already exist we never need to change the set of
10875 	 * IRE_BROADCASTs when we are UP.
10876 	 */
10877 	if (addr != ipif->ipif_brd_addr)
10878 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10879 
10880 	return (0);
10881 }
10882 
10883 /* Get interface broadcast address. */
10884 /* ARGSUSED */
10885 int
10886 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10887     ip_ioctl_cmd_t *ipip, void *if_req)
10888 {
10889 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10890 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10891 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10892 		return (EADDRNOTAVAIL);
10893 
10894 	/* IPIF_BROADCAST not possible with IPv6 */
10895 	ASSERT(!ipif->ipif_isv6);
10896 	*sin = sin_null;
10897 	sin->sin_family = AF_INET;
10898 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10899 	return (0);
10900 }
10901 
10902 /*
10903  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10904  */
10905 /* ARGSUSED */
10906 int
10907 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10908     ip_ioctl_cmd_t *ipip, void *if_req)
10909 {
10910 	int err = 0;
10911 	in6_addr_t v6mask;
10912 
10913 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10914 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10915 
10916 	ASSERT(IAM_WRITER_IPIF(ipif));
10917 
10918 	if (ipif->ipif_isv6) {
10919 		sin6_t *sin6;
10920 
10921 		if (sin->sin_family != AF_INET6)
10922 			return (EAFNOSUPPORT);
10923 
10924 		sin6 = (sin6_t *)sin;
10925 		v6mask = sin6->sin6_addr;
10926 	} else {
10927 		ipaddr_t mask;
10928 
10929 		if (sin->sin_family != AF_INET)
10930 			return (EAFNOSUPPORT);
10931 
10932 		mask = sin->sin_addr.s_addr;
10933 		if (!ip_contiguous_mask(ntohl(mask)))
10934 			return (ENOTSUP);
10935 		V4MASK_TO_V6(mask, v6mask);
10936 	}
10937 
10938 	/*
10939 	 * No big deal if the interface isn't already up, or the mask
10940 	 * isn't really changing, or this is pt-pt.
10941 	 */
10942 	if (!(ipif->ipif_flags & IPIF_UP) ||
10943 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10944 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10945 		ipif->ipif_v6net_mask = v6mask;
10946 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10947 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10948 			    ipif->ipif_v6net_mask,
10949 			    ipif->ipif_v6subnet);
10950 		}
10951 		return (0);
10952 	}
10953 	/*
10954 	 * Make sure we have valid net and subnet broadcast ire's
10955 	 * for the old netmask, if needed by other logical interfaces.
10956 	 */
10957 	err = ipif_logical_down(ipif, q, mp);
10958 	if (err == EINPROGRESS)
10959 		return (err);
10960 	(void) ipif_down_tail(ipif);
10961 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10962 	return (err);
10963 }
10964 
10965 static int
10966 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10967 {
10968 	in6_addr_t v6mask;
10969 	int err = 0;
10970 
10971 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10973 
10974 	if (ipif->ipif_isv6) {
10975 		sin6_t *sin6;
10976 
10977 		sin6 = (sin6_t *)sin;
10978 		v6mask = sin6->sin6_addr;
10979 	} else {
10980 		ipaddr_t mask;
10981 
10982 		mask = sin->sin_addr.s_addr;
10983 		V4MASK_TO_V6(mask, v6mask);
10984 	}
10985 
10986 	ipif->ipif_v6net_mask = v6mask;
10987 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10988 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10989 		    ipif->ipif_v6subnet);
10990 	}
10991 	err = ipif_up(ipif, q, mp);
10992 
10993 	if (err == 0 || err == EINPROGRESS) {
10994 		/*
10995 		 * The interface must be DL_BOUND if this packet has to
10996 		 * go out on the wire. Since we only go through a logical
10997 		 * down and are bound with the driver during an internal
10998 		 * down/up that is satisfied.
10999 		 */
11000 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11001 			/* Potentially broadcast an address mask reply. */
11002 			ipif_mask_reply(ipif);
11003 		}
11004 	}
11005 	return (err);
11006 }
11007 
11008 /* ARGSUSED */
11009 int
11010 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11011     ip_ioctl_cmd_t *ipip, void *if_req)
11012 {
11013 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11014 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11015 	(void) ipif_down_tail(ipif);
11016 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11017 }
11018 
11019 /* Get interface net mask. */
11020 /* ARGSUSED */
11021 int
11022 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11023     ip_ioctl_cmd_t *ipip, void *if_req)
11024 {
11025 	struct lifreq *lifr = (struct lifreq *)if_req;
11026 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11027 
11028 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11029 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11030 
11031 	/*
11032 	 * net mask can't change since we have a reference to the ipif.
11033 	 */
11034 	if (ipif->ipif_isv6) {
11035 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11036 		*sin6 = sin6_null;
11037 		sin6->sin6_family = AF_INET6;
11038 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11039 		lifr->lifr_addrlen =
11040 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11041 	} else {
11042 		*sin = sin_null;
11043 		sin->sin_family = AF_INET;
11044 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11045 		if (ipip->ipi_cmd_type == LIF_CMD) {
11046 			lifr->lifr_addrlen =
11047 			    ip_mask_to_plen(ipif->ipif_net_mask);
11048 		}
11049 	}
11050 	return (0);
11051 }
11052 
11053 /* ARGSUSED */
11054 int
11055 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11056     ip_ioctl_cmd_t *ipip, void *if_req)
11057 {
11058 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11059 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11060 
11061 	/*
11062 	 * Since no applications should ever be setting metrics on underlying
11063 	 * interfaces, we explicitly fail to smoke 'em out.
11064 	 */
11065 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11066 		return (EINVAL);
11067 
11068 	/*
11069 	 * Set interface metric.  We don't use this for
11070 	 * anything but we keep track of it in case it is
11071 	 * important to routing applications or such.
11072 	 */
11073 	if (ipip->ipi_cmd_type == IF_CMD) {
11074 		struct ifreq    *ifr;
11075 
11076 		ifr = (struct ifreq *)if_req;
11077 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11078 	} else {
11079 		struct lifreq   *lifr;
11080 
11081 		lifr = (struct lifreq *)if_req;
11082 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11083 	}
11084 	return (0);
11085 }
11086 
11087 /* ARGSUSED */
11088 int
11089 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11090     ip_ioctl_cmd_t *ipip, void *if_req)
11091 {
11092 	/* Get interface metric. */
11093 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11094 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11095 
11096 	if (ipip->ipi_cmd_type == IF_CMD) {
11097 		struct ifreq    *ifr;
11098 
11099 		ifr = (struct ifreq *)if_req;
11100 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11101 	} else {
11102 		struct lifreq   *lifr;
11103 
11104 		lifr = (struct lifreq *)if_req;
11105 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11106 	}
11107 
11108 	return (0);
11109 }
11110 
11111 /* ARGSUSED */
11112 int
11113 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11114     ip_ioctl_cmd_t *ipip, void *if_req)
11115 {
11116 	int	arp_muxid;
11117 
11118 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11119 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11120 	/*
11121 	 * Set the muxid returned from I_PLINK.
11122 	 */
11123 	if (ipip->ipi_cmd_type == IF_CMD) {
11124 		struct ifreq *ifr = (struct ifreq *)if_req;
11125 
11126 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11127 		arp_muxid = ifr->ifr_arp_muxid;
11128 	} else {
11129 		struct lifreq *lifr = (struct lifreq *)if_req;
11130 
11131 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11132 		arp_muxid = lifr->lifr_arp_muxid;
11133 	}
11134 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11135 	return (0);
11136 }
11137 
11138 /* ARGSUSED */
11139 int
11140 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11141     ip_ioctl_cmd_t *ipip, void *if_req)
11142 {
11143 	int	arp_muxid = 0;
11144 
11145 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11146 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11147 	/*
11148 	 * Get the muxid saved in ill for I_PUNLINK.
11149 	 */
11150 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11151 	if (ipip->ipi_cmd_type == IF_CMD) {
11152 		struct ifreq *ifr = (struct ifreq *)if_req;
11153 
11154 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11155 		ifr->ifr_arp_muxid = arp_muxid;
11156 	} else {
11157 		struct lifreq *lifr = (struct lifreq *)if_req;
11158 
11159 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11160 		lifr->lifr_arp_muxid = arp_muxid;
11161 	}
11162 	return (0);
11163 }
11164 
11165 /*
11166  * Set the subnet prefix. Does not modify the broadcast address.
11167  */
11168 /* ARGSUSED */
11169 int
11170 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11171     ip_ioctl_cmd_t *ipip, void *if_req)
11172 {
11173 	int err = 0;
11174 	in6_addr_t v6addr;
11175 	in6_addr_t v6mask;
11176 	boolean_t need_up = B_FALSE;
11177 	int addrlen;
11178 
11179 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11180 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11181 
11182 	ASSERT(IAM_WRITER_IPIF(ipif));
11183 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11184 
11185 	if (ipif->ipif_isv6) {
11186 		sin6_t *sin6;
11187 
11188 		if (sin->sin_family != AF_INET6)
11189 			return (EAFNOSUPPORT);
11190 
11191 		sin6 = (sin6_t *)sin;
11192 		v6addr = sin6->sin6_addr;
11193 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11194 			return (EADDRNOTAVAIL);
11195 	} else {
11196 		ipaddr_t addr;
11197 
11198 		if (sin->sin_family != AF_INET)
11199 			return (EAFNOSUPPORT);
11200 
11201 		addr = sin->sin_addr.s_addr;
11202 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11203 			return (EADDRNOTAVAIL);
11204 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11205 		/* Add 96 bits */
11206 		addrlen += IPV6_ABITS - IP_ABITS;
11207 	}
11208 
11209 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11210 		return (EINVAL);
11211 
11212 	/* Check if bits in the address is set past the mask */
11213 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11214 		return (EINVAL);
11215 
11216 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11217 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11218 		return (0);	/* No change */
11219 
11220 	if (ipif->ipif_flags & IPIF_UP) {
11221 		/*
11222 		 * If the interface is already marked up,
11223 		 * we call ipif_down which will take care
11224 		 * of ditching any IREs that have been set
11225 		 * up based on the old interface address.
11226 		 */
11227 		err = ipif_logical_down(ipif, q, mp);
11228 		if (err == EINPROGRESS)
11229 			return (err);
11230 		(void) ipif_down_tail(ipif);
11231 		need_up = B_TRUE;
11232 	}
11233 
11234 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11235 	return (err);
11236 }
11237 
11238 static int
11239 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11240     queue_t *q, mblk_t *mp, boolean_t need_up)
11241 {
11242 	ill_t	*ill = ipif->ipif_ill;
11243 	int	err = 0;
11244 
11245 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11246 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11247 
11248 	/* Set the new address. */
11249 	mutex_enter(&ill->ill_lock);
11250 	ipif->ipif_v6net_mask = v6mask;
11251 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11252 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11253 		    ipif->ipif_v6subnet);
11254 	}
11255 	mutex_exit(&ill->ill_lock);
11256 
11257 	if (need_up) {
11258 		/*
11259 		 * Now bring the interface back up.  If this
11260 		 * is the only IPIF for the ILL, ipif_up
11261 		 * will have to re-bind to the device, so
11262 		 * we may get back EINPROGRESS, in which
11263 		 * case, this IOCTL will get completed in
11264 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11265 		 */
11266 		err = ipif_up(ipif, q, mp);
11267 		if (err == EINPROGRESS)
11268 			return (err);
11269 	}
11270 	return (err);
11271 }
11272 
11273 /* ARGSUSED */
11274 int
11275 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11276     ip_ioctl_cmd_t *ipip, void *if_req)
11277 {
11278 	int	addrlen;
11279 	in6_addr_t v6addr;
11280 	in6_addr_t v6mask;
11281 	struct lifreq *lifr = (struct lifreq *)if_req;
11282 
11283 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11284 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11285 	(void) ipif_down_tail(ipif);
11286 
11287 	addrlen = lifr->lifr_addrlen;
11288 	if (ipif->ipif_isv6) {
11289 		sin6_t *sin6;
11290 
11291 		sin6 = (sin6_t *)sin;
11292 		v6addr = sin6->sin6_addr;
11293 	} else {
11294 		ipaddr_t addr;
11295 
11296 		addr = sin->sin_addr.s_addr;
11297 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11298 		addrlen += IPV6_ABITS - IP_ABITS;
11299 	}
11300 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11301 
11302 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11303 }
11304 
11305 /* ARGSUSED */
11306 int
11307 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11308     ip_ioctl_cmd_t *ipip, void *if_req)
11309 {
11310 	struct lifreq *lifr = (struct lifreq *)if_req;
11311 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11312 
11313 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11314 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11315 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11316 
11317 	if (ipif->ipif_isv6) {
11318 		*sin6 = sin6_null;
11319 		sin6->sin6_family = AF_INET6;
11320 		sin6->sin6_addr = ipif->ipif_v6subnet;
11321 		lifr->lifr_addrlen =
11322 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11323 	} else {
11324 		*sin = sin_null;
11325 		sin->sin_family = AF_INET;
11326 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11327 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11328 	}
11329 	return (0);
11330 }
11331 
11332 /*
11333  * Set the IPv6 address token.
11334  */
11335 /* ARGSUSED */
11336 int
11337 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11338     ip_ioctl_cmd_t *ipi, void *if_req)
11339 {
11340 	ill_t *ill = ipif->ipif_ill;
11341 	int err;
11342 	in6_addr_t v6addr;
11343 	in6_addr_t v6mask;
11344 	boolean_t need_up = B_FALSE;
11345 	int i;
11346 	sin6_t *sin6 = (sin6_t *)sin;
11347 	struct lifreq *lifr = (struct lifreq *)if_req;
11348 	int addrlen;
11349 
11350 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11351 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11352 	ASSERT(IAM_WRITER_IPIF(ipif));
11353 
11354 	addrlen = lifr->lifr_addrlen;
11355 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11356 	if (ipif->ipif_id != 0)
11357 		return (EINVAL);
11358 
11359 	if (!ipif->ipif_isv6)
11360 		return (EINVAL);
11361 
11362 	if (addrlen > IPV6_ABITS)
11363 		return (EINVAL);
11364 
11365 	v6addr = sin6->sin6_addr;
11366 
11367 	/*
11368 	 * The length of the token is the length from the end.  To get
11369 	 * the proper mask for this, compute the mask of the bits not
11370 	 * in the token; ie. the prefix, and then xor to get the mask.
11371 	 */
11372 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11373 		return (EINVAL);
11374 	for (i = 0; i < 4; i++) {
11375 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11376 	}
11377 
11378 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11379 	    ill->ill_token_length == addrlen)
11380 		return (0);	/* No change */
11381 
11382 	if (ipif->ipif_flags & IPIF_UP) {
11383 		err = ipif_logical_down(ipif, q, mp);
11384 		if (err == EINPROGRESS)
11385 			return (err);
11386 		(void) ipif_down_tail(ipif);
11387 		need_up = B_TRUE;
11388 	}
11389 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11390 	return (err);
11391 }
11392 
11393 static int
11394 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11395     mblk_t *mp, boolean_t need_up)
11396 {
11397 	in6_addr_t v6addr;
11398 	in6_addr_t v6mask;
11399 	ill_t	*ill = ipif->ipif_ill;
11400 	int	i;
11401 	int	err = 0;
11402 
11403 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11404 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11405 	v6addr = sin6->sin6_addr;
11406 	/*
11407 	 * The length of the token is the length from the end.  To get
11408 	 * the proper mask for this, compute the mask of the bits not
11409 	 * in the token; ie. the prefix, and then xor to get the mask.
11410 	 */
11411 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11412 	for (i = 0; i < 4; i++)
11413 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11414 
11415 	mutex_enter(&ill->ill_lock);
11416 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11417 	ill->ill_token_length = addrlen;
11418 	ill->ill_manual_token = 1;
11419 
11420 	/* Reconfigure the link-local address based on this new token */
11421 	ipif_setlinklocal(ill->ill_ipif);
11422 
11423 	mutex_exit(&ill->ill_lock);
11424 
11425 	if (need_up) {
11426 		/*
11427 		 * Now bring the interface back up.  If this
11428 		 * is the only IPIF for the ILL, ipif_up
11429 		 * will have to re-bind to the device, so
11430 		 * we may get back EINPROGRESS, in which
11431 		 * case, this IOCTL will get completed in
11432 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11433 		 */
11434 		err = ipif_up(ipif, q, mp);
11435 		if (err == EINPROGRESS)
11436 			return (err);
11437 	}
11438 	return (err);
11439 }
11440 
11441 /* ARGSUSED */
11442 int
11443 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11444     ip_ioctl_cmd_t *ipi, void *if_req)
11445 {
11446 	ill_t *ill;
11447 	sin6_t *sin6 = (sin6_t *)sin;
11448 	struct lifreq *lifr = (struct lifreq *)if_req;
11449 
11450 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11451 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11452 	if (ipif->ipif_id != 0)
11453 		return (EINVAL);
11454 
11455 	ill = ipif->ipif_ill;
11456 	if (!ill->ill_isv6)
11457 		return (ENXIO);
11458 
11459 	*sin6 = sin6_null;
11460 	sin6->sin6_family = AF_INET6;
11461 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11462 	sin6->sin6_addr = ill->ill_token;
11463 	lifr->lifr_addrlen = ill->ill_token_length;
11464 	return (0);
11465 }
11466 
11467 /*
11468  * Set (hardware) link specific information that might override
11469  * what was acquired through the DL_INFO_ACK.
11470  */
11471 /* ARGSUSED */
11472 int
11473 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11474     ip_ioctl_cmd_t *ipi, void *if_req)
11475 {
11476 	ill_t		*ill = ipif->ipif_ill;
11477 	int		ip_min_mtu;
11478 	struct lifreq	*lifr = (struct lifreq *)if_req;
11479 	lif_ifinfo_req_t *lir;
11480 
11481 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11482 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11483 	lir = &lifr->lifr_ifinfo;
11484 	ASSERT(IAM_WRITER_IPIF(ipif));
11485 
11486 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11487 	if (ipif->ipif_id != 0)
11488 		return (EINVAL);
11489 
11490 	/* Set interface MTU. */
11491 	if (ipif->ipif_isv6)
11492 		ip_min_mtu = IPV6_MIN_MTU;
11493 	else
11494 		ip_min_mtu = IP_MIN_MTU;
11495 
11496 	/*
11497 	 * Verify values before we set anything. Allow zero to
11498 	 * mean unspecified.
11499 	 *
11500 	 * XXX We should be able to set the user-defined lir_mtu to some value
11501 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11502 	 * ill_max_frag value tells us the max MTU that can be handled by the
11503 	 * datalink, whereas the ill_current_frag is dynamically computed for
11504 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11505 	 * since there is currently no way of distinguishing between
11506 	 * administratively fixed link mtu values (e.g., those set via
11507 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11508 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11509 	 * upper-bound.
11510 	 */
11511 	if (lir->lir_maxmtu != 0 &&
11512 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11513 	    lir->lir_maxmtu < ip_min_mtu))
11514 		return (EINVAL);
11515 	if (lir->lir_reachtime != 0 &&
11516 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11517 		return (EINVAL);
11518 	if (lir->lir_reachretrans != 0 &&
11519 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11520 		return (EINVAL);
11521 
11522 	mutex_enter(&ill->ill_lock);
11523 	/*
11524 	 * The dce and fragmentation code can handle changes to ill_mtu
11525 	 * concurrent with sending/fragmenting packets.
11526 	 */
11527 	if (lir->lir_maxmtu != 0)
11528 		ill->ill_user_mtu = lir->lir_maxmtu;
11529 
11530 	if (lir->lir_reachtime != 0)
11531 		ill->ill_reachable_time = lir->lir_reachtime;
11532 
11533 	if (lir->lir_reachretrans != 0)
11534 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11535 
11536 	ill->ill_max_hops = lir->lir_maxhops;
11537 	ill->ill_max_buf = ND_MAX_Q;
11538 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11539 		/*
11540 		 * ill_mtu is the actual interface MTU, obtained as the min
11541 		 * of user-configured mtu and the value announced by the
11542 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11543 		 * we have already made the choice of requiring
11544 		 * ill_user_mtu < ill_current_frag by the time we get here,
11545 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11546 		 * here.
11547 		 */
11548 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11549 	}
11550 	mutex_exit(&ill->ill_lock);
11551 
11552 	/*
11553 	 * Make sure all dce_generation checks find out
11554 	 * that ill_mtu has changed.
11555 	 */
11556 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11557 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11558 
11559 	/*
11560 	 * Refresh IPMP meta-interface MTU if necessary.
11561 	 */
11562 	if (IS_UNDER_IPMP(ill))
11563 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11564 
11565 	return (0);
11566 }
11567 
11568 /* ARGSUSED */
11569 int
11570 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11571     ip_ioctl_cmd_t *ipi, void *if_req)
11572 {
11573 	struct lif_ifinfo_req *lir;
11574 	ill_t *ill = ipif->ipif_ill;
11575 
11576 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11577 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11578 	if (ipif->ipif_id != 0)
11579 		return (EINVAL);
11580 
11581 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11582 	lir->lir_maxhops = ill->ill_max_hops;
11583 	lir->lir_reachtime = ill->ill_reachable_time;
11584 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11585 	lir->lir_maxmtu = ill->ill_mtu;
11586 
11587 	return (0);
11588 }
11589 
11590 /*
11591  * Return best guess as to the subnet mask for the specified address.
11592  * Based on the subnet masks for all the configured interfaces.
11593  *
11594  * We end up returning a zero mask in the case of default, multicast or
11595  * experimental.
11596  */
11597 static ipaddr_t
11598 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11599 {
11600 	ipaddr_t net_mask;
11601 	ill_t	*ill;
11602 	ipif_t	*ipif;
11603 	ill_walk_context_t ctx;
11604 	ipif_t	*fallback_ipif = NULL;
11605 
11606 	net_mask = ip_net_mask(addr);
11607 	if (net_mask == 0) {
11608 		*ipifp = NULL;
11609 		return (0);
11610 	}
11611 
11612 	/* Let's check to see if this is maybe a local subnet route. */
11613 	/* this function only applies to IPv4 interfaces */
11614 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11615 	ill = ILL_START_WALK_V4(&ctx, ipst);
11616 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11617 		mutex_enter(&ill->ill_lock);
11618 		for (ipif = ill->ill_ipif; ipif != NULL;
11619 		    ipif = ipif->ipif_next) {
11620 			if (IPIF_IS_CONDEMNED(ipif))
11621 				continue;
11622 			if (!(ipif->ipif_flags & IPIF_UP))
11623 				continue;
11624 			if ((ipif->ipif_subnet & net_mask) ==
11625 			    (addr & net_mask)) {
11626 				/*
11627 				 * Don't trust pt-pt interfaces if there are
11628 				 * other interfaces.
11629 				 */
11630 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11631 					if (fallback_ipif == NULL) {
11632 						ipif_refhold_locked(ipif);
11633 						fallback_ipif = ipif;
11634 					}
11635 					continue;
11636 				}
11637 
11638 				/*
11639 				 * Fine. Just assume the same net mask as the
11640 				 * directly attached subnet interface is using.
11641 				 */
11642 				ipif_refhold_locked(ipif);
11643 				mutex_exit(&ill->ill_lock);
11644 				rw_exit(&ipst->ips_ill_g_lock);
11645 				if (fallback_ipif != NULL)
11646 					ipif_refrele(fallback_ipif);
11647 				*ipifp = ipif;
11648 				return (ipif->ipif_net_mask);
11649 			}
11650 		}
11651 		mutex_exit(&ill->ill_lock);
11652 	}
11653 	rw_exit(&ipst->ips_ill_g_lock);
11654 
11655 	*ipifp = fallback_ipif;
11656 	return ((fallback_ipif != NULL) ?
11657 	    fallback_ipif->ipif_net_mask : net_mask);
11658 }
11659 
11660 /*
11661  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11662  */
11663 static void
11664 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11665 {
11666 	IOCP	iocp;
11667 	ipft_t	*ipft;
11668 	ipllc_t	*ipllc;
11669 	mblk_t	*mp1;
11670 	cred_t	*cr;
11671 	int	error = 0;
11672 	conn_t	*connp;
11673 
11674 	ip1dbg(("ip_wput_ioctl"));
11675 	iocp = (IOCP)mp->b_rptr;
11676 	mp1 = mp->b_cont;
11677 	if (mp1 == NULL) {
11678 		iocp->ioc_error = EINVAL;
11679 		mp->b_datap->db_type = M_IOCNAK;
11680 		iocp->ioc_count = 0;
11681 		qreply(q, mp);
11682 		return;
11683 	}
11684 
11685 	/*
11686 	 * These IOCTLs provide various control capabilities to
11687 	 * upstream agents such as ULPs and processes.	There
11688 	 * are currently two such IOCTLs implemented.  They
11689 	 * are used by TCP to provide update information for
11690 	 * existing IREs and to forcibly delete an IRE for a
11691 	 * host that is not responding, thereby forcing an
11692 	 * attempt at a new route.
11693 	 */
11694 	iocp->ioc_error = EINVAL;
11695 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11696 		goto done;
11697 
11698 	ipllc = (ipllc_t *)mp1->b_rptr;
11699 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11700 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11701 			break;
11702 	}
11703 	/*
11704 	 * prefer credential from mblk over ioctl;
11705 	 * see ip_sioctl_copyin_setup
11706 	 */
11707 	cr = msg_getcred(mp, NULL);
11708 	if (cr == NULL)
11709 		cr = iocp->ioc_cr;
11710 
11711 	/*
11712 	 * Refhold the conn in case the request gets queued up in some lookup
11713 	 */
11714 	ASSERT(CONN_Q(q));
11715 	connp = Q_TO_CONN(q);
11716 	CONN_INC_REF(connp);
11717 	if (ipft->ipft_pfi &&
11718 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11719 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11720 		error = (*ipft->ipft_pfi)(q,
11721 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11722 	}
11723 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11724 		/*
11725 		 * CONN_OPER_PENDING_DONE happens in the function called
11726 		 * through ipft_pfi above.
11727 		 */
11728 		return;
11729 	}
11730 
11731 	CONN_OPER_PENDING_DONE(connp);
11732 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11733 		freemsg(mp);
11734 		return;
11735 	}
11736 	iocp->ioc_error = error;
11737 
11738 done:
11739 	mp->b_datap->db_type = M_IOCACK;
11740 	if (iocp->ioc_error)
11741 		iocp->ioc_count = 0;
11742 	qreply(q, mp);
11743 }
11744 
11745 /*
11746  * Assign a unique id for the ipif. This is used by sctp_addr.c
11747  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11748  */
11749 static void
11750 ipif_assign_seqid(ipif_t *ipif)
11751 {
11752 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11753 
11754 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11755 }
11756 
11757 /*
11758  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11759  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11760  * that the clone is complete -- including the seqid -- and the expectation is
11761  * that the caller will either free or overwrite `sipif' before it's unlocked.
11762  */
11763 static void
11764 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11765 {
11766 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11767 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11768 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11769 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11770 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11771 
11772 	dipif->ipif_flags = sipif->ipif_flags;
11773 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11774 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11775 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11776 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11777 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11778 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11779 
11780 	/*
11781 	 * As per the comment atop the function, we assume that these sipif
11782 	 * fields will be changed before sipif is unlocked.
11783 	 */
11784 	dipif->ipif_seqid = sipif->ipif_seqid;
11785 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11786 }
11787 
11788 /*
11789  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11790  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11791  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11792  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11793  * down (i.e., no DAD), of the same type, and unlocked.
11794  */
11795 static void
11796 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11797 {
11798 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11799 	ipxop_t *ipx = ipsq->ipsq_xop;
11800 
11801 	ASSERT(sipif != dipif);
11802 	ASSERT(sipif != virgipif);
11803 
11804 	/*
11805 	 * Grab all of the locks that protect the ipif in a defined order.
11806 	 */
11807 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11808 
11809 	ipif_clone(sipif, dipif);
11810 	if (virgipif != NULL) {
11811 		ipif_clone(virgipif, sipif);
11812 		mi_free(virgipif);
11813 	}
11814 
11815 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11816 
11817 	/*
11818 	 * Transfer ownership of the current xop, if necessary.
11819 	 */
11820 	if (ipx->ipx_current_ipif == sipif) {
11821 		ASSERT(ipx->ipx_pending_ipif == NULL);
11822 		mutex_enter(&ipx->ipx_lock);
11823 		ipx->ipx_current_ipif = dipif;
11824 		mutex_exit(&ipx->ipx_lock);
11825 	}
11826 
11827 	if (virgipif == NULL)
11828 		mi_free(sipif);
11829 }
11830 
11831 /*
11832  * checks if:
11833  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11834  *	- logical interface is within the allowed range
11835  */
11836 static int
11837 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11838 {
11839 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11840 		return (ENAMETOOLONG);
11841 
11842 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11843 		return (ERANGE);
11844 	return (0);
11845 }
11846 
11847 /*
11848  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11849  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11850  * be inserted into the first space available in the list. The value of
11851  * ipif_id will then be set to the appropriate value for its position.
11852  */
11853 static int
11854 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11855 {
11856 	ill_t *ill;
11857 	ipif_t *tipif;
11858 	ipif_t **tipifp;
11859 	int id, err;
11860 	ip_stack_t	*ipst;
11861 
11862 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11863 	    IAM_WRITER_IPIF(ipif));
11864 
11865 	ill = ipif->ipif_ill;
11866 	ASSERT(ill != NULL);
11867 	ipst = ill->ill_ipst;
11868 
11869 	/*
11870 	 * In the case of lo0:0 we already hold the ill_g_lock.
11871 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11872 	 * ipif_insert.
11873 	 */
11874 	if (acquire_g_lock)
11875 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11876 	mutex_enter(&ill->ill_lock);
11877 	id = ipif->ipif_id;
11878 	tipifp = &(ill->ill_ipif);
11879 	if (id == -1) {	/* need to find a real id */
11880 		id = 0;
11881 		while ((tipif = *tipifp) != NULL) {
11882 			ASSERT(tipif->ipif_id >= id);
11883 			if (tipif->ipif_id != id)
11884 				break; /* non-consecutive id */
11885 			id++;
11886 			tipifp = &(tipif->ipif_next);
11887 		}
11888 		if ((err = is_lifname_valid(ill, id)) != 0) {
11889 			mutex_exit(&ill->ill_lock);
11890 			if (acquire_g_lock)
11891 				rw_exit(&ipst->ips_ill_g_lock);
11892 			return (err);
11893 		}
11894 		ipif->ipif_id = id; /* assign new id */
11895 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11896 		/* we have a real id; insert ipif in the right place */
11897 		while ((tipif = *tipifp) != NULL) {
11898 			ASSERT(tipif->ipif_id != id);
11899 			if (tipif->ipif_id > id)
11900 				break; /* found correct location */
11901 			tipifp = &(tipif->ipif_next);
11902 		}
11903 	} else {
11904 		mutex_exit(&ill->ill_lock);
11905 		if (acquire_g_lock)
11906 			rw_exit(&ipst->ips_ill_g_lock);
11907 		return (err);
11908 	}
11909 
11910 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11911 
11912 	ipif->ipif_next = tipif;
11913 	*tipifp = ipif;
11914 	mutex_exit(&ill->ill_lock);
11915 	if (acquire_g_lock)
11916 		rw_exit(&ipst->ips_ill_g_lock);
11917 
11918 	return (0);
11919 }
11920 
11921 static void
11922 ipif_remove(ipif_t *ipif)
11923 {
11924 	ipif_t	**ipifp;
11925 	ill_t	*ill = ipif->ipif_ill;
11926 
11927 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11928 
11929 	mutex_enter(&ill->ill_lock);
11930 	ipifp = &ill->ill_ipif;
11931 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11932 		if (*ipifp == ipif) {
11933 			*ipifp = ipif->ipif_next;
11934 			break;
11935 		}
11936 	}
11937 	mutex_exit(&ill->ill_lock);
11938 }
11939 
11940 /*
11941  * Allocate and initialize a new interface control structure.  (Always
11942  * called as writer.)
11943  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11944  * is not part of the global linked list of ills. ipif_seqid is unique
11945  * in the system and to preserve the uniqueness, it is assigned only
11946  * when ill becomes part of the global list. At that point ill will
11947  * have a name. If it doesn't get assigned here, it will get assigned
11948  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11949  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11950  * the interface flags or any other information from the DL_INFO_ACK for
11951  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11952  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11953  * second DL_INFO_ACK comes in from the driver.
11954  */
11955 static ipif_t *
11956 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11957     boolean_t insert, int *errorp)
11958 {
11959 	int err;
11960 	ipif_t	*ipif;
11961 	ip_stack_t *ipst = ill->ill_ipst;
11962 
11963 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11964 	    ill->ill_name, id, (void *)ill));
11965 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11966 
11967 	if (errorp != NULL)
11968 		*errorp = 0;
11969 
11970 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11971 		if (errorp != NULL)
11972 			*errorp = ENOMEM;
11973 		return (NULL);
11974 	}
11975 	*ipif = ipif_zero;	/* start clean */
11976 
11977 	ipif->ipif_ill = ill;
11978 	ipif->ipif_id = id;	/* could be -1 */
11979 	/*
11980 	 * Inherit the zoneid from the ill; for the shared stack instance
11981 	 * this is always the global zone
11982 	 */
11983 	ipif->ipif_zoneid = ill->ill_zoneid;
11984 
11985 	ipif->ipif_refcnt = 0;
11986 
11987 	if (insert) {
11988 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11989 			mi_free(ipif);
11990 			if (errorp != NULL)
11991 				*errorp = err;
11992 			return (NULL);
11993 		}
11994 		/* -1 id should have been replaced by real id */
11995 		id = ipif->ipif_id;
11996 		ASSERT(id >= 0);
11997 	}
11998 
11999 	if (ill->ill_name[0] != '\0')
12000 		ipif_assign_seqid(ipif);
12001 
12002 	/*
12003 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
12004 	 * (which must not exist yet because the zeroth ipif is created once
12005 	 * per ill).  However, do not not link it to the ipmp_grp_t until
12006 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12007 	 */
12008 	if (id == 0 && IS_IPMP(ill)) {
12009 		if (ipmp_illgrp_create(ill) == NULL) {
12010 			if (insert) {
12011 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12012 				ipif_remove(ipif);
12013 				rw_exit(&ipst->ips_ill_g_lock);
12014 			}
12015 			mi_free(ipif);
12016 			if (errorp != NULL)
12017 				*errorp = ENOMEM;
12018 			return (NULL);
12019 		}
12020 	}
12021 
12022 	/*
12023 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12024 	 * not up and can't be looked up until the ioctl completes and the
12025 	 * IPIF_CHANGING flag is cleared.
12026 	 */
12027 	mutex_enter(&ill->ill_lock);
12028 
12029 	ipif->ipif_ire_type = ire_type;
12030 
12031 	if (ipif->ipif_isv6) {
12032 		ill->ill_flags |= ILLF_IPV6;
12033 	} else {
12034 		ipaddr_t inaddr_any = INADDR_ANY;
12035 
12036 		ill->ill_flags |= ILLF_IPV4;
12037 
12038 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12039 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12040 		    &ipif->ipif_v6lcl_addr);
12041 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12042 		    &ipif->ipif_v6subnet);
12043 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12044 		    &ipif->ipif_v6net_mask);
12045 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12046 		    &ipif->ipif_v6brd_addr);
12047 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12048 		    &ipif->ipif_v6pp_dst_addr);
12049 	}
12050 
12051 	/*
12052 	 * Don't set the interface flags etc. now, will do it in
12053 	 * ip_ll_subnet_defaults.
12054 	 */
12055 	if (!initialize)
12056 		goto out;
12057 
12058 	/*
12059 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12060 	 * with no underlying interfaces (and thus an unknown broadcast
12061 	 * address length), but all interfaces that can be placed into an IPMP
12062 	 * group are required to be broadcast-capable.
12063 	 */
12064 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12065 		/*
12066 		 * Later detect lack of DLPI driver multicast capability by
12067 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12068 		 */
12069 		ill->ill_flags |= ILLF_MULTICAST;
12070 		if (!ipif->ipif_isv6)
12071 			ipif->ipif_flags |= IPIF_BROADCAST;
12072 	} else {
12073 		if (ill->ill_net_type != IRE_LOOPBACK) {
12074 			if (ipif->ipif_isv6)
12075 				/*
12076 				 * Note: xresolv interfaces will eventually need
12077 				 * NOARP set here as well, but that will require
12078 				 * those external resolvers to have some
12079 				 * knowledge of that flag and act appropriately.
12080 				 * Not to be changed at present.
12081 				 */
12082 				ill->ill_flags |= ILLF_NONUD;
12083 			else
12084 				ill->ill_flags |= ILLF_NOARP;
12085 		}
12086 		if (ill->ill_phys_addr_length == 0) {
12087 			if (IS_VNI(ill)) {
12088 				ipif->ipif_flags |= IPIF_NOXMIT;
12089 			} else {
12090 				/* pt-pt supports multicast. */
12091 				ill->ill_flags |= ILLF_MULTICAST;
12092 				if (ill->ill_net_type != IRE_LOOPBACK)
12093 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12094 			}
12095 		}
12096 	}
12097 out:
12098 	mutex_exit(&ill->ill_lock);
12099 	return (ipif);
12100 }
12101 
12102 /*
12103  * Remove the neighbor cache entries associated with this logical
12104  * interface.
12105  */
12106 int
12107 ipif_arp_down(ipif_t *ipif)
12108 {
12109 	ill_t	*ill = ipif->ipif_ill;
12110 	int	err = 0;
12111 
12112 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12113 	ASSERT(IAM_WRITER_IPIF(ipif));
12114 
12115 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12116 	    ill_t *, ill, ipif_t *, ipif);
12117 	ipif_nce_down(ipif);
12118 
12119 	/*
12120 	 * If this is the last ipif that is going down and there are no
12121 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12122 	 * clean up ARP completely.
12123 	 */
12124 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12125 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12126 		/*
12127 		 * If this was the last ipif on an IPMP interface, purge any
12128 		 * static ARP entries associated with it.
12129 		 */
12130 		if (IS_IPMP(ill))
12131 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12132 
12133 		/* UNBIND, DETACH */
12134 		err = arp_ll_down(ill);
12135 	}
12136 
12137 	return (err);
12138 }
12139 
12140 /*
12141  * Get the resolver set up for a new IP address.  (Always called as writer.)
12142  * Called both for IPv4 and IPv6 interfaces, though it only does some
12143  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12144  *
12145  * The enumerated value res_act tunes the behavior:
12146  * 	* Res_act_initial: set up all the resolver structures for a new
12147  *	  IP address.
12148  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12149  *	  ARP message in defense of the address.
12150  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12151  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12152  *
12153  * Returns zero on success, or an errno upon failure.
12154  */
12155 int
12156 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12157 {
12158 	ill_t		*ill = ipif->ipif_ill;
12159 	int		err;
12160 	boolean_t	was_dup;
12161 
12162 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12163 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12164 	ASSERT(IAM_WRITER_IPIF(ipif));
12165 
12166 	was_dup = B_FALSE;
12167 	if (res_act == Res_act_initial) {
12168 		ipif->ipif_addr_ready = 0;
12169 		/*
12170 		 * We're bringing an interface up here.  There's no way that we
12171 		 * should need to shut down ARP now.
12172 		 */
12173 		mutex_enter(&ill->ill_lock);
12174 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12175 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12176 			ill->ill_ipif_dup_count--;
12177 			was_dup = B_TRUE;
12178 		}
12179 		mutex_exit(&ill->ill_lock);
12180 	}
12181 	if (ipif->ipif_recovery_id != 0)
12182 		(void) untimeout(ipif->ipif_recovery_id);
12183 	ipif->ipif_recovery_id = 0;
12184 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12185 		ipif->ipif_addr_ready = 1;
12186 		return (0);
12187 	}
12188 	/* NDP will set the ipif_addr_ready flag when it's ready */
12189 	if (ill->ill_isv6)
12190 		return (0);
12191 
12192 	err = ipif_arp_up(ipif, res_act, was_dup);
12193 	return (err);
12194 }
12195 
12196 /*
12197  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12198  * when a link has just gone back up.
12199  */
12200 static void
12201 ipif_nce_start_dad(ipif_t *ipif)
12202 {
12203 	ncec_t *ncec;
12204 	ill_t *ill = ipif->ipif_ill;
12205 	boolean_t isv6 = ill->ill_isv6;
12206 
12207 	if (isv6) {
12208 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12209 		    &ipif->ipif_v6lcl_addr);
12210 	} else {
12211 		ipaddr_t v4addr;
12212 
12213 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12214 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12215 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12216 			/*
12217 			 * If we can't contact ARP for some reason,
12218 			 * that's not really a problem.  Just send
12219 			 * out the routing socket notification that
12220 			 * DAD completion would have done, and continue.
12221 			 */
12222 			ipif_mask_reply(ipif);
12223 			ipif_up_notify(ipif);
12224 			ipif->ipif_addr_ready = 1;
12225 			return;
12226 		}
12227 
12228 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12229 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12230 	}
12231 
12232 	if (ncec == NULL) {
12233 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12234 		    (void *)ipif));
12235 		return;
12236 	}
12237 	if (!nce_restart_dad(ncec)) {
12238 		/*
12239 		 * If we can't restart DAD for some reason, that's not really a
12240 		 * problem.  Just send out the routing socket notification that
12241 		 * DAD completion would have done, and continue.
12242 		 */
12243 		ipif_up_notify(ipif);
12244 		ipif->ipif_addr_ready = 1;
12245 	}
12246 	ncec_refrele(ncec);
12247 }
12248 
12249 /*
12250  * Restart duplicate address detection on all interfaces on the given ill.
12251  *
12252  * This is called when an interface transitions from down to up
12253  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12254  *
12255  * Note that since the underlying physical link has transitioned, we must cause
12256  * at least one routing socket message to be sent here, either via DAD
12257  * completion or just by default on the first ipif.  (If we don't do this, then
12258  * in.mpathd will see long delays when doing link-based failure recovery.)
12259  */
12260 void
12261 ill_restart_dad(ill_t *ill, boolean_t went_up)
12262 {
12263 	ipif_t *ipif;
12264 
12265 	if (ill == NULL)
12266 		return;
12267 
12268 	/*
12269 	 * If layer two doesn't support duplicate address detection, then just
12270 	 * send the routing socket message now and be done with it.
12271 	 */
12272 	if (!ill->ill_isv6 && arp_no_defense) {
12273 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12274 		return;
12275 	}
12276 
12277 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12278 		if (went_up) {
12279 
12280 			if (ipif->ipif_flags & IPIF_UP) {
12281 				ipif_nce_start_dad(ipif);
12282 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12283 				/*
12284 				 * kick off the bring-up process now.
12285 				 */
12286 				ipif_do_recovery(ipif);
12287 			} else {
12288 				/*
12289 				 * Unfortunately, the first ipif is "special"
12290 				 * and represents the underlying ill in the
12291 				 * routing socket messages.  Thus, when this
12292 				 * one ipif is down, we must still notify so
12293 				 * that the user knows the IFF_RUNNING status
12294 				 * change.  (If the first ipif is up, then
12295 				 * we'll handle eventual routing socket
12296 				 * notification via DAD completion.)
12297 				 */
12298 				if (ipif == ill->ill_ipif) {
12299 					ip_rts_ifmsg(ill->ill_ipif,
12300 					    RTSQ_DEFAULT);
12301 				}
12302 			}
12303 		} else {
12304 			/*
12305 			 * After link down, we'll need to send a new routing
12306 			 * message when the link comes back, so clear
12307 			 * ipif_addr_ready.
12308 			 */
12309 			ipif->ipif_addr_ready = 0;
12310 		}
12311 	}
12312 
12313 	/*
12314 	 * If we've torn down links, then notify the user right away.
12315 	 */
12316 	if (!went_up)
12317 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12318 }
12319 
12320 static void
12321 ipsq_delete(ipsq_t *ipsq)
12322 {
12323 	ipxop_t *ipx = ipsq->ipsq_xop;
12324 
12325 	ipsq->ipsq_ipst = NULL;
12326 	ASSERT(ipsq->ipsq_phyint == NULL);
12327 	ASSERT(ipsq->ipsq_xop != NULL);
12328 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12329 	ASSERT(ipx->ipx_pending_mp == NULL);
12330 	kmem_free(ipsq, sizeof (ipsq_t));
12331 }
12332 
12333 static int
12334 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12335 {
12336 	int err = 0;
12337 	ipif_t *ipif;
12338 
12339 	if (ill == NULL)
12340 		return (0);
12341 
12342 	ASSERT(IAM_WRITER_ILL(ill));
12343 	ill->ill_up_ipifs = B_TRUE;
12344 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12345 		if (ipif->ipif_was_up) {
12346 			if (!(ipif->ipif_flags & IPIF_UP))
12347 				err = ipif_up(ipif, q, mp);
12348 			ipif->ipif_was_up = B_FALSE;
12349 			if (err != 0) {
12350 				ASSERT(err == EINPROGRESS);
12351 				return (err);
12352 			}
12353 		}
12354 	}
12355 	ill->ill_up_ipifs = B_FALSE;
12356 	return (0);
12357 }
12358 
12359 /*
12360  * This function is called to bring up all the ipifs that were up before
12361  * bringing the ill down via ill_down_ipifs().
12362  */
12363 int
12364 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12365 {
12366 	int err;
12367 
12368 	ASSERT(IAM_WRITER_ILL(ill));
12369 
12370 	if (ill->ill_replumbing) {
12371 		ill->ill_replumbing = 0;
12372 		/*
12373 		 * Send down REPLUMB_DONE notification followed by the
12374 		 * BIND_REQ on the arp stream.
12375 		 */
12376 		if (!ill->ill_isv6)
12377 			arp_send_replumb_conf(ill);
12378 	}
12379 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12380 	if (err != 0)
12381 		return (err);
12382 
12383 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12384 }
12385 
12386 /*
12387  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12388  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12389  */
12390 static void
12391 ill_down_ipifs(ill_t *ill, boolean_t logical)
12392 {
12393 	ipif_t *ipif;
12394 
12395 	ASSERT(IAM_WRITER_ILL(ill));
12396 
12397 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12398 		/*
12399 		 * We go through the ipif_down logic even if the ipif
12400 		 * is already down, since routes can be added based
12401 		 * on down ipifs. Going through ipif_down once again
12402 		 * will delete any IREs created based on these routes.
12403 		 */
12404 		if (ipif->ipif_flags & IPIF_UP)
12405 			ipif->ipif_was_up = B_TRUE;
12406 
12407 		if (logical) {
12408 			(void) ipif_logical_down(ipif, NULL, NULL);
12409 			ipif_non_duplicate(ipif);
12410 			(void) ipif_down_tail(ipif);
12411 		} else {
12412 			(void) ipif_down(ipif, NULL, NULL);
12413 		}
12414 	}
12415 }
12416 
12417 /*
12418  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12419  * a look again at valid source addresses.
12420  * This should be called each time after the set of source addresses has been
12421  * changed.
12422  */
12423 void
12424 ip_update_source_selection(ip_stack_t *ipst)
12425 {
12426 	/* We skip past SRC_GENERATION_VERIFY */
12427 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12428 	    SRC_GENERATION_VERIFY)
12429 		atomic_add_32(&ipst->ips_src_generation, 1);
12430 }
12431 
12432 /*
12433  * Finish the group join started in ip_sioctl_groupname().
12434  */
12435 /* ARGSUSED */
12436 static void
12437 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12438 {
12439 	ill_t		*ill = q->q_ptr;
12440 	phyint_t	*phyi = ill->ill_phyint;
12441 	ipmp_grp_t	*grp = phyi->phyint_grp;
12442 	ip_stack_t	*ipst = ill->ill_ipst;
12443 
12444 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12445 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12446 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12447 
12448 	if (phyi->phyint_illv4 != NULL) {
12449 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12450 		VERIFY(grp->gr_pendv4-- > 0);
12451 		rw_exit(&ipst->ips_ipmp_lock);
12452 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12453 	}
12454 	if (phyi->phyint_illv6 != NULL) {
12455 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12456 		VERIFY(grp->gr_pendv6-- > 0);
12457 		rw_exit(&ipst->ips_ipmp_lock);
12458 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12459 	}
12460 	freemsg(mp);
12461 }
12462 
12463 /*
12464  * Process an SIOCSLIFGROUPNAME request.
12465  */
12466 /* ARGSUSED */
12467 int
12468 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12469     ip_ioctl_cmd_t *ipip, void *ifreq)
12470 {
12471 	struct lifreq	*lifr = ifreq;
12472 	ill_t		*ill = ipif->ipif_ill;
12473 	ip_stack_t	*ipst = ill->ill_ipst;
12474 	phyint_t	*phyi = ill->ill_phyint;
12475 	ipmp_grp_t	*grp = phyi->phyint_grp;
12476 	mblk_t		*ipsq_mp;
12477 	int		err = 0;
12478 
12479 	/*
12480 	 * Note that phyint_grp can only change here, where we're exclusive.
12481 	 */
12482 	ASSERT(IAM_WRITER_ILL(ill));
12483 
12484 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12485 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12486 		return (EINVAL);
12487 
12488 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12489 
12490 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12491 
12492 	/*
12493 	 * If the name hasn't changed, there's nothing to do.
12494 	 */
12495 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12496 		goto unlock;
12497 
12498 	/*
12499 	 * Handle requests to rename an IPMP meta-interface.
12500 	 *
12501 	 * Note that creation of the IPMP meta-interface is handled in
12502 	 * userland through the standard plumbing sequence.  As part of the
12503 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12504 	 * the name of the interface (see ipif_set_values_tail()).
12505 	 */
12506 	if (IS_IPMP(ill)) {
12507 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12508 		goto unlock;
12509 	}
12510 
12511 	/*
12512 	 * Handle requests to add or remove an IP interface from a group.
12513 	 */
12514 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12515 		/*
12516 		 * Moves are handled by first removing the interface from
12517 		 * its existing group, and then adding it to another group.
12518 		 * So, fail if it's already in a group.
12519 		 */
12520 		if (IS_UNDER_IPMP(ill)) {
12521 			err = EALREADY;
12522 			goto unlock;
12523 		}
12524 
12525 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12526 		if (grp == NULL) {
12527 			err = ENOENT;
12528 			goto unlock;
12529 		}
12530 
12531 		/*
12532 		 * Check if the phyint and its ills are suitable for
12533 		 * inclusion into the group.
12534 		 */
12535 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12536 			goto unlock;
12537 
12538 		/*
12539 		 * Checks pass; join the group, and enqueue the remaining
12540 		 * illgrp joins for when we've become part of the group xop
12541 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12542 		 * requires an mblk_t to scribble on, and since `mp' will be
12543 		 * freed as part of completing the ioctl, allocate another.
12544 		 */
12545 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12546 			err = ENOMEM;
12547 			goto unlock;
12548 		}
12549 
12550 		/*
12551 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12552 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12553 		 * before ip_join_illgrps() is called back.  See the comments
12554 		 * in ip_sioctl_plink_ipmp() for more.
12555 		 */
12556 		if (phyi->phyint_illv4 != NULL)
12557 			grp->gr_pendv4++;
12558 		if (phyi->phyint_illv6 != NULL)
12559 			grp->gr_pendv6++;
12560 
12561 		rw_exit(&ipst->ips_ipmp_lock);
12562 
12563 		ipmp_phyint_join_grp(phyi, grp);
12564 		ill_refhold(ill);
12565 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12566 		    SWITCH_OP, B_FALSE);
12567 		return (0);
12568 	} else {
12569 		/*
12570 		 * Request to remove the interface from a group.  If the
12571 		 * interface is not in a group, this trivially succeeds.
12572 		 */
12573 		rw_exit(&ipst->ips_ipmp_lock);
12574 		if (IS_UNDER_IPMP(ill))
12575 			ipmp_phyint_leave_grp(phyi);
12576 		return (0);
12577 	}
12578 unlock:
12579 	rw_exit(&ipst->ips_ipmp_lock);
12580 	return (err);
12581 }
12582 
12583 /*
12584  * Process an SIOCGLIFBINDING request.
12585  */
12586 /* ARGSUSED */
12587 int
12588 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12589     ip_ioctl_cmd_t *ipip, void *ifreq)
12590 {
12591 	ill_t		*ill;
12592 	struct lifreq	*lifr = ifreq;
12593 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12594 
12595 	if (!IS_IPMP(ipif->ipif_ill))
12596 		return (EINVAL);
12597 
12598 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12599 	if ((ill = ipif->ipif_bound_ill) == NULL)
12600 		lifr->lifr_binding[0] = '\0';
12601 	else
12602 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12603 	rw_exit(&ipst->ips_ipmp_lock);
12604 	return (0);
12605 }
12606 
12607 /*
12608  * Process an SIOCGLIFGROUPNAME request.
12609  */
12610 /* ARGSUSED */
12611 int
12612 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12613     ip_ioctl_cmd_t *ipip, void *ifreq)
12614 {
12615 	ipmp_grp_t	*grp;
12616 	struct lifreq	*lifr = ifreq;
12617 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12618 
12619 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12620 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12621 		lifr->lifr_groupname[0] = '\0';
12622 	else
12623 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12624 	rw_exit(&ipst->ips_ipmp_lock);
12625 	return (0);
12626 }
12627 
12628 /*
12629  * Process an SIOCGLIFGROUPINFO request.
12630  */
12631 /* ARGSUSED */
12632 int
12633 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12634     ip_ioctl_cmd_t *ipip, void *dummy)
12635 {
12636 	ipmp_grp_t	*grp;
12637 	lifgroupinfo_t	*lifgr;
12638 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12639 
12640 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12641 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12642 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12643 
12644 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12645 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12646 		rw_exit(&ipst->ips_ipmp_lock);
12647 		return (ENOENT);
12648 	}
12649 	ipmp_grp_info(grp, lifgr);
12650 	rw_exit(&ipst->ips_ipmp_lock);
12651 	return (0);
12652 }
12653 
12654 static void
12655 ill_dl_down(ill_t *ill)
12656 {
12657 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12658 
12659 	/*
12660 	 * The ill is down; unbind but stay attached since we're still
12661 	 * associated with a PPA. If we have negotiated DLPI capabilites
12662 	 * with the data link service provider (IDS_OK) then reset them.
12663 	 * The interval between unbinding and rebinding is potentially
12664 	 * unbounded hence we cannot assume things will be the same.
12665 	 * The DLPI capabilities will be probed again when the data link
12666 	 * is brought up.
12667 	 */
12668 	mblk_t	*mp = ill->ill_unbind_mp;
12669 
12670 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12671 
12672 	if (!ill->ill_replumbing) {
12673 		/* Free all ilms for this ill */
12674 		update_conn_ill(ill, ill->ill_ipst);
12675 	} else {
12676 		ill_leave_multicast(ill);
12677 	}
12678 
12679 	ill->ill_unbind_mp = NULL;
12680 	if (mp != NULL) {
12681 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12682 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12683 		    ill->ill_name));
12684 		mutex_enter(&ill->ill_lock);
12685 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12686 		mutex_exit(&ill->ill_lock);
12687 		/*
12688 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12689 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12690 		 * ill_capability_dld_disable disable rightaway. If this is not
12691 		 * an unplumb operation then the disable happens on receipt of
12692 		 * the capab ack via ip_rput_dlpi_writer ->
12693 		 * ill_capability_ack_thr. In both cases the order of
12694 		 * the operations seen by DLD is capability disable followed
12695 		 * by DL_UNBIND. Also the DLD capability disable needs a
12696 		 * cv_wait'able context.
12697 		 */
12698 		if (ill->ill_state_flags & ILL_CONDEMNED)
12699 			ill_capability_dld_disable(ill);
12700 		ill_capability_reset(ill, B_FALSE);
12701 		ill_dlpi_send(ill, mp);
12702 	}
12703 	mutex_enter(&ill->ill_lock);
12704 	ill->ill_dl_up = 0;
12705 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12706 	mutex_exit(&ill->ill_lock);
12707 }
12708 
12709 void
12710 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12711 {
12712 	union DL_primitives *dlp;
12713 	t_uscalar_t prim;
12714 	boolean_t waitack = B_FALSE;
12715 
12716 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12717 
12718 	dlp = (union DL_primitives *)mp->b_rptr;
12719 	prim = dlp->dl_primitive;
12720 
12721 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12722 	    dl_primstr(prim), prim, ill->ill_name));
12723 
12724 	switch (prim) {
12725 	case DL_PHYS_ADDR_REQ:
12726 	{
12727 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12728 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12729 		break;
12730 	}
12731 	case DL_BIND_REQ:
12732 		mutex_enter(&ill->ill_lock);
12733 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12734 		mutex_exit(&ill->ill_lock);
12735 		break;
12736 	}
12737 
12738 	/*
12739 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12740 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12741 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12742 	 */
12743 	mutex_enter(&ill->ill_lock);
12744 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12745 	    (prim == DL_UNBIND_REQ)) {
12746 		ill->ill_dlpi_pending = prim;
12747 		waitack = B_TRUE;
12748 	}
12749 
12750 	mutex_exit(&ill->ill_lock);
12751 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12752 	    char *, dl_primstr(prim), ill_t *, ill);
12753 	putnext(ill->ill_wq, mp);
12754 
12755 	/*
12756 	 * There is no ack for DL_NOTIFY_CONF messages
12757 	 */
12758 	if (waitack && prim == DL_NOTIFY_CONF)
12759 		ill_dlpi_done(ill, prim);
12760 }
12761 
12762 /*
12763  * Helper function for ill_dlpi_send().
12764  */
12765 /* ARGSUSED */
12766 static void
12767 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12768 {
12769 	ill_dlpi_send(q->q_ptr, mp);
12770 }
12771 
12772 /*
12773  * Send a DLPI control message to the driver but make sure there
12774  * is only one outstanding message. Uses ill_dlpi_pending to tell
12775  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12776  * when an ACK or a NAK is received to process the next queued message.
12777  */
12778 void
12779 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12780 {
12781 	mblk_t **mpp;
12782 
12783 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12784 
12785 	/*
12786 	 * To ensure that any DLPI requests for current exclusive operation
12787 	 * are always completely sent before any DLPI messages for other
12788 	 * operations, require writer access before enqueuing.
12789 	 */
12790 	if (!IAM_WRITER_ILL(ill)) {
12791 		ill_refhold(ill);
12792 		/* qwriter_ip() does the ill_refrele() */
12793 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12794 		    NEW_OP, B_TRUE);
12795 		return;
12796 	}
12797 
12798 	mutex_enter(&ill->ill_lock);
12799 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12800 		/* Must queue message. Tail insertion */
12801 		mpp = &ill->ill_dlpi_deferred;
12802 		while (*mpp != NULL)
12803 			mpp = &((*mpp)->b_next);
12804 
12805 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12806 		    "while %s pending\n", ill->ill_name,
12807 		    dl_primstr(ill->ill_dlpi_pending)));
12808 
12809 		*mpp = mp;
12810 		mutex_exit(&ill->ill_lock);
12811 		return;
12812 	}
12813 	mutex_exit(&ill->ill_lock);
12814 	ill_dlpi_dispatch(ill, mp);
12815 }
12816 
12817 void
12818 ill_capability_send(ill_t *ill, mblk_t *mp)
12819 {
12820 	ill->ill_capab_pending_cnt++;
12821 	ill_dlpi_send(ill, mp);
12822 }
12823 
12824 void
12825 ill_capability_done(ill_t *ill)
12826 {
12827 	ASSERT(ill->ill_capab_pending_cnt != 0);
12828 
12829 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12830 
12831 	ill->ill_capab_pending_cnt--;
12832 	if (ill->ill_capab_pending_cnt == 0 &&
12833 	    ill->ill_dlpi_capab_state == IDCS_OK)
12834 		ill_capability_reset_alloc(ill);
12835 }
12836 
12837 /*
12838  * Send all deferred DLPI messages without waiting for their ACKs.
12839  */
12840 void
12841 ill_dlpi_send_deferred(ill_t *ill)
12842 {
12843 	mblk_t *mp, *nextmp;
12844 
12845 	/*
12846 	 * Clear ill_dlpi_pending so that the message is not queued in
12847 	 * ill_dlpi_send().
12848 	 */
12849 	mutex_enter(&ill->ill_lock);
12850 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12851 	mp = ill->ill_dlpi_deferred;
12852 	ill->ill_dlpi_deferred = NULL;
12853 	mutex_exit(&ill->ill_lock);
12854 
12855 	for (; mp != NULL; mp = nextmp) {
12856 		nextmp = mp->b_next;
12857 		mp->b_next = NULL;
12858 		ill_dlpi_send(ill, mp);
12859 	}
12860 }
12861 
12862 /*
12863  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12864  * or M_HANGUP
12865  */
12866 static void
12867 ill_dlpi_clear_deferred(ill_t *ill)
12868 {
12869 	mblk_t	*mp, *nextmp;
12870 
12871 	mutex_enter(&ill->ill_lock);
12872 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12873 	mp = ill->ill_dlpi_deferred;
12874 	ill->ill_dlpi_deferred = NULL;
12875 	mutex_exit(&ill->ill_lock);
12876 
12877 	for (; mp != NULL; mp = nextmp) {
12878 		nextmp = mp->b_next;
12879 		inet_freemsg(mp);
12880 	}
12881 }
12882 
12883 /*
12884  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12885  */
12886 boolean_t
12887 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12888 {
12889 	t_uscalar_t pending;
12890 
12891 	mutex_enter(&ill->ill_lock);
12892 	if (ill->ill_dlpi_pending == prim) {
12893 		mutex_exit(&ill->ill_lock);
12894 		return (B_TRUE);
12895 	}
12896 
12897 	/*
12898 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12899 	 * without waiting, so don't print any warnings in that case.
12900 	 */
12901 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12902 		mutex_exit(&ill->ill_lock);
12903 		return (B_FALSE);
12904 	}
12905 	pending = ill->ill_dlpi_pending;
12906 	mutex_exit(&ill->ill_lock);
12907 
12908 	if (pending == DL_PRIM_INVAL) {
12909 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12910 		    "received unsolicited ack for %s on %s\n",
12911 		    dl_primstr(prim), ill->ill_name);
12912 	} else {
12913 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12914 		    "received unexpected ack for %s on %s (expecting %s)\n",
12915 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12916 	}
12917 	return (B_FALSE);
12918 }
12919 
12920 /*
12921  * Complete the current DLPI operation associated with `prim' on `ill' and
12922  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12923  * operations and the ill's current exclusive IPSQ operation has finished
12924  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12925  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12926  * the comments above ipsq_current_finish() for details.
12927  */
12928 void
12929 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12930 {
12931 	mblk_t *mp;
12932 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12933 	ipxop_t *ipx = ipsq->ipsq_xop;
12934 
12935 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12936 	mutex_enter(&ill->ill_lock);
12937 
12938 	ASSERT(prim != DL_PRIM_INVAL);
12939 	ASSERT(ill->ill_dlpi_pending == prim);
12940 
12941 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12942 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12943 
12944 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12945 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12946 		if (ipx->ipx_current_done) {
12947 			mutex_enter(&ipx->ipx_lock);
12948 			ipx->ipx_current_ipif = NULL;
12949 			mutex_exit(&ipx->ipx_lock);
12950 		}
12951 		cv_signal(&ill->ill_cv);
12952 		mutex_exit(&ill->ill_lock);
12953 		return;
12954 	}
12955 
12956 	ill->ill_dlpi_deferred = mp->b_next;
12957 	mp->b_next = NULL;
12958 	mutex_exit(&ill->ill_lock);
12959 
12960 	ill_dlpi_dispatch(ill, mp);
12961 }
12962 
12963 /*
12964  * Queue a (multicast) DLPI control message to be sent to the driver by
12965  * later calling ill_dlpi_send_queued.
12966  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12967  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12968  * for the same group to race.
12969  * We send DLPI control messages in order using ill_lock.
12970  * For IPMP we should be called on the cast_ill.
12971  */
12972 void
12973 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12974 {
12975 	mblk_t **mpp;
12976 
12977 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12978 
12979 	mutex_enter(&ill->ill_lock);
12980 	/* Must queue message. Tail insertion */
12981 	mpp = &ill->ill_dlpi_deferred;
12982 	while (*mpp != NULL)
12983 		mpp = &((*mpp)->b_next);
12984 
12985 	*mpp = mp;
12986 	mutex_exit(&ill->ill_lock);
12987 }
12988 
12989 /*
12990  * Send the messages that were queued. Make sure there is only
12991  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12992  * when an ACK or a NAK is received to process the next queued message.
12993  * For IPMP we are called on the upper ill, but when send what is queued
12994  * on the cast_ill.
12995  */
12996 void
12997 ill_dlpi_send_queued(ill_t *ill)
12998 {
12999 	mblk_t	*mp;
13000 	union DL_primitives *dlp;
13001 	t_uscalar_t prim;
13002 	ill_t *release_ill = NULL;
13003 
13004 	if (IS_IPMP(ill)) {
13005 		/* On the upper IPMP ill. */
13006 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13007 		if (release_ill == NULL) {
13008 			/* Avoid ever sending anything down to the ipmpstub */
13009 			return;
13010 		}
13011 		ill = release_ill;
13012 	}
13013 	mutex_enter(&ill->ill_lock);
13014 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
13015 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13016 			/* Can't send. Somebody else will send it */
13017 			mutex_exit(&ill->ill_lock);
13018 			goto done;
13019 		}
13020 		ill->ill_dlpi_deferred = mp->b_next;
13021 		mp->b_next = NULL;
13022 		if (!ill->ill_dl_up) {
13023 			/*
13024 			 * Nobody there. All multicast addresses will be
13025 			 * re-joined when we get the DL_BIND_ACK bringing the
13026 			 * interface up.
13027 			 */
13028 			freemsg(mp);
13029 			continue;
13030 		}
13031 		dlp = (union DL_primitives *)mp->b_rptr;
13032 		prim = dlp->dl_primitive;
13033 
13034 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13035 		    (prim == DL_UNBIND_REQ)) {
13036 			ill->ill_dlpi_pending = prim;
13037 		}
13038 		mutex_exit(&ill->ill_lock);
13039 
13040 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13041 		    char *, dl_primstr(prim), ill_t *, ill);
13042 		putnext(ill->ill_wq, mp);
13043 		mutex_enter(&ill->ill_lock);
13044 	}
13045 	mutex_exit(&ill->ill_lock);
13046 done:
13047 	if (release_ill != NULL)
13048 		ill_refrele(release_ill);
13049 }
13050 
13051 /*
13052  * Queue an IP (IGMP/MLD) message to be sent by IP from
13053  * ill_mcast_send_queued
13054  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13055  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13056  * group to race.
13057  * We send them in order using ill_lock.
13058  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13059  */
13060 void
13061 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13062 {
13063 	mblk_t **mpp;
13064 	ill_t *release_ill = NULL;
13065 
13066 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13067 
13068 	if (IS_IPMP(ill)) {
13069 		/* On the upper IPMP ill. */
13070 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13071 		if (release_ill == NULL) {
13072 			/* Discard instead of queuing for the ipmp interface */
13073 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13074 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13075 			    mp, ill);
13076 			freemsg(mp);
13077 			return;
13078 		}
13079 		ill = release_ill;
13080 	}
13081 
13082 	mutex_enter(&ill->ill_lock);
13083 	/* Must queue message. Tail insertion */
13084 	mpp = &ill->ill_mcast_deferred;
13085 	while (*mpp != NULL)
13086 		mpp = &((*mpp)->b_next);
13087 
13088 	*mpp = mp;
13089 	mutex_exit(&ill->ill_lock);
13090 	if (release_ill != NULL)
13091 		ill_refrele(release_ill);
13092 }
13093 
13094 /*
13095  * Send the IP packets that were queued by ill_mcast_queue.
13096  * These are IGMP/MLD packets.
13097  *
13098  * For IPMP we are called on the upper ill, but when send what is queued
13099  * on the cast_ill.
13100  *
13101  * Request loopback of the report if we are acting as a multicast
13102  * router, so that the process-level routing demon can hear it.
13103  * This will run multiple times for the same group if there are members
13104  * on the same group for multiple ipif's on the same ill. The
13105  * igmp_input/mld_input code will suppress this due to the loopback thus we
13106  * always loopback membership report.
13107  *
13108  * We also need to make sure that this does not get load balanced
13109  * by IPMP. We do this by passing an ill to ip_output_simple.
13110  */
13111 void
13112 ill_mcast_send_queued(ill_t *ill)
13113 {
13114 	mblk_t	*mp;
13115 	ip_xmit_attr_t ixas;
13116 	ill_t *release_ill = NULL;
13117 
13118 	if (IS_IPMP(ill)) {
13119 		/* On the upper IPMP ill. */
13120 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13121 		if (release_ill == NULL) {
13122 			/*
13123 			 * We should have no messages on the ipmp interface
13124 			 * but no point in trying to send them.
13125 			 */
13126 			return;
13127 		}
13128 		ill = release_ill;
13129 	}
13130 	bzero(&ixas, sizeof (ixas));
13131 	ixas.ixa_zoneid = ALL_ZONES;
13132 	ixas.ixa_cred = kcred;
13133 	ixas.ixa_cpid = NOPID;
13134 	ixas.ixa_tsl = NULL;
13135 	/*
13136 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13137 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13138 	 * That is necessary to handle IGMP/MLD snooping switches.
13139 	 */
13140 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13141 	ixas.ixa_ipst = ill->ill_ipst;
13142 
13143 	mutex_enter(&ill->ill_lock);
13144 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13145 		ill->ill_mcast_deferred = mp->b_next;
13146 		mp->b_next = NULL;
13147 		if (!ill->ill_dl_up) {
13148 			/*
13149 			 * Nobody there. Just drop the ip packets.
13150 			 * IGMP/MLD will resend later, if this is a replumb.
13151 			 */
13152 			freemsg(mp);
13153 			continue;
13154 		}
13155 		mutex_enter(&ill->ill_phyint->phyint_lock);
13156 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13157 			/*
13158 			 * When the ill is getting deactivated, we only want to
13159 			 * send the DLPI messages, so drop IGMP/MLD packets.
13160 			 * DLPI messages are handled by ill_dlpi_send_queued()
13161 			 */
13162 			mutex_exit(&ill->ill_phyint->phyint_lock);
13163 			freemsg(mp);
13164 			continue;
13165 		}
13166 		mutex_exit(&ill->ill_phyint->phyint_lock);
13167 		mutex_exit(&ill->ill_lock);
13168 
13169 		/* Check whether we are sending IPv4 or IPv6. */
13170 		if (ill->ill_isv6) {
13171 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13172 
13173 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13174 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13175 		} else {
13176 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13177 
13178 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13179 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13180 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13181 		}
13182 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13183 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13184 		(void) ip_output_simple(mp, &ixas);
13185 		ixa_cleanup(&ixas);
13186 
13187 		mutex_enter(&ill->ill_lock);
13188 	}
13189 	mutex_exit(&ill->ill_lock);
13190 
13191 done:
13192 	if (release_ill != NULL)
13193 		ill_refrele(release_ill);
13194 }
13195 
13196 /*
13197  * Take down a specific interface, but don't lose any information about it.
13198  * (Always called as writer.)
13199  * This function goes through the down sequence even if the interface is
13200  * already down. There are 2 reasons.
13201  * a. Currently we permit interface routes that depend on down interfaces
13202  *    to be added. This behaviour itself is questionable. However it appears
13203  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13204  *    time. We go thru the cleanup in order to remove these routes.
13205  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13206  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13207  *    down, but we need to cleanup i.e. do ill_dl_down and
13208  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13209  *
13210  * IP-MT notes:
13211  *
13212  * Model of reference to interfaces.
13213  *
13214  * The following members in ipif_t track references to the ipif.
13215  *	int     ipif_refcnt;    Active reference count
13216  *
13217  * The following members in ill_t track references to the ill.
13218  *	int             ill_refcnt;     active refcnt
13219  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13220  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13221  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13222  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13223  *
13224  * Reference to an ipif or ill can be obtained in any of the following ways.
13225  *
13226  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13227  * Pointers to ipif / ill from other data structures viz ire and conn.
13228  * Implicit reference to the ipif / ill by holding a reference to the ire.
13229  *
13230  * The ipif/ill lookup functions return a reference held ipif / ill.
13231  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13232  * This is a purely dynamic reference count associated with threads holding
13233  * references to the ipif / ill. Pointers from other structures do not
13234  * count towards this reference count.
13235  *
13236  * ill_ire_cnt is the number of ire's associated with the
13237  * ill. This is incremented whenever a new ire is created referencing the
13238  * ill. This is done atomically inside ire_add_v[46] where the ire is
13239  * actually added to the ire hash table. The count is decremented in
13240  * ire_inactive where the ire is destroyed.
13241  *
13242  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13243  * This is incremented atomically in
13244  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13245  * table. Similarly it is decremented in ncec_inactive() where the ncec
13246  * is destroyed.
13247  *
13248  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13249  * incremented atomically in nce_add() where the nce is actually added to the
13250  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13251  * is destroyed.
13252  *
13253  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13254  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13255  *
13256  * Flow of ioctls involving interface down/up
13257  *
13258  * The following is the sequence of an attempt to set some critical flags on an
13259  * up interface.
13260  * ip_sioctl_flags
13261  * ipif_down
13262  * wait for ipif to be quiescent
13263  * ipif_down_tail
13264  * ip_sioctl_flags_tail
13265  *
13266  * All set ioctls that involve down/up sequence would have a skeleton similar
13267  * to the above. All the *tail functions are called after the refcounts have
13268  * dropped to the appropriate values.
13269  *
13270  * SIOC ioctls during the IPIF_CHANGING interval.
13271  *
13272  * Threads handling SIOC set ioctls serialize on the squeue, but this
13273  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13274  * steps of internal changes to the state, some of which are visible in
13275  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13276  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13277  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13278  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13279  * the current exclusive operation completes. The IPIF_CHANGING check
13280  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13281  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13282  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13283  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13284  * until we release the ipsq_lock, even though the ill/ipif state flags
13285  * can change after we drop the ill_lock.
13286  */
13287 int
13288 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13289 {
13290 	ill_t		*ill = ipif->ipif_ill;
13291 	conn_t		*connp;
13292 	boolean_t	success;
13293 	boolean_t	ipif_was_up = B_FALSE;
13294 	ip_stack_t	*ipst = ill->ill_ipst;
13295 
13296 	ASSERT(IAM_WRITER_IPIF(ipif));
13297 
13298 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13299 
13300 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13301 	    ill_t *, ill, ipif_t *, ipif);
13302 
13303 	if (ipif->ipif_flags & IPIF_UP) {
13304 		mutex_enter(&ill->ill_lock);
13305 		ipif->ipif_flags &= ~IPIF_UP;
13306 		ASSERT(ill->ill_ipif_up_count > 0);
13307 		--ill->ill_ipif_up_count;
13308 		mutex_exit(&ill->ill_lock);
13309 		ipif_was_up = B_TRUE;
13310 		/* Update status in SCTP's list */
13311 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13312 		ill_nic_event_dispatch(ipif->ipif_ill,
13313 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13314 	}
13315 
13316 	/*
13317 	 * Blow away memberships we established in ipif_multicast_up().
13318 	 */
13319 	ipif_multicast_down(ipif);
13320 
13321 	/*
13322 	 * Remove from the mapping for __sin6_src_id. We insert only
13323 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13324 	 * stored as mapped addresses, we need to check for mapped
13325 	 * INADDR_ANY also.
13326 	 */
13327 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13328 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13329 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13330 		int err;
13331 
13332 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13333 		    ipif->ipif_zoneid, ipst);
13334 		if (err != 0) {
13335 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13336 		}
13337 	}
13338 
13339 	if (ipif_was_up) {
13340 		/* only delete if we'd added ire's before */
13341 		if (ipif->ipif_isv6)
13342 			ipif_delete_ires_v6(ipif);
13343 		else
13344 			ipif_delete_ires_v4(ipif);
13345 	}
13346 
13347 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13348 		/*
13349 		 * Since the interface is now down, it may have just become
13350 		 * inactive.  Note that this needs to be done even for a
13351 		 * lll_logical_down(), or ARP entries will not get correctly
13352 		 * restored when the interface comes back up.
13353 		 */
13354 		if (IS_UNDER_IPMP(ill))
13355 			ipmp_ill_refresh_active(ill);
13356 	}
13357 
13358 	/*
13359 	 * neighbor-discovery or arp entries for this interface. The ipif
13360 	 * has to be quiesced, so we walk all the nce's and delete those
13361 	 * that point at the ipif->ipif_ill. At the same time, we also
13362 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13363 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13364 	 * that for ipif_down_tail()
13365 	 */
13366 	ipif_nce_down(ipif);
13367 
13368 	/*
13369 	 * If this is the last ipif on the ill, we also need to remove
13370 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13371 	 * never succeed.
13372 	 */
13373 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13374 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13375 
13376 	/*
13377 	 * Walk all CONNs that can have a reference on an ire for this
13378 	 * ipif (we actually walk all that now have stale references).
13379 	 */
13380 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13381 
13382 	/*
13383 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13384 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13385 	 * and ill_delete -> ipif_free -> ipif_down
13386 	 */
13387 	if (mp == NULL) {
13388 		ASSERT(q == NULL);
13389 		return (0);
13390 	}
13391 
13392 	if (CONN_Q(q)) {
13393 		connp = Q_TO_CONN(q);
13394 		mutex_enter(&connp->conn_lock);
13395 	} else {
13396 		connp = NULL;
13397 	}
13398 	mutex_enter(&ill->ill_lock);
13399 	/*
13400 	 * Are there any ire's pointing to this ipif that are still active ?
13401 	 * If this is the last ipif going down, are there any ire's pointing
13402 	 * to this ill that are still active ?
13403 	 */
13404 	if (ipif_is_quiescent(ipif)) {
13405 		mutex_exit(&ill->ill_lock);
13406 		if (connp != NULL)
13407 			mutex_exit(&connp->conn_lock);
13408 		return (0);
13409 	}
13410 
13411 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13412 	    ill->ill_name, (void *)ill));
13413 	/*
13414 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13415 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13416 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13417 	 */
13418 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13419 	if (!success) {
13420 		/* The conn is closing. So just return */
13421 		ASSERT(connp != NULL);
13422 		mutex_exit(&ill->ill_lock);
13423 		mutex_exit(&connp->conn_lock);
13424 		return (EINTR);
13425 	}
13426 
13427 	mutex_exit(&ill->ill_lock);
13428 	if (connp != NULL)
13429 		mutex_exit(&connp->conn_lock);
13430 	return (EINPROGRESS);
13431 }
13432 
13433 int
13434 ipif_down_tail(ipif_t *ipif)
13435 {
13436 	ill_t	*ill = ipif->ipif_ill;
13437 	int	err = 0;
13438 
13439 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13440 	    ill_t *, ill, ipif_t *, ipif);
13441 
13442 	/*
13443 	 * Skip any loopback interface (null wq).
13444 	 * If this is the last logical interface on the ill
13445 	 * have ill_dl_down tell the driver we are gone (unbind)
13446 	 * Note that lun 0 can ipif_down even though
13447 	 * there are other logical units that are up.
13448 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13449 	 */
13450 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13451 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13452 	    ill->ill_dl_up) {
13453 		ill_dl_down(ill);
13454 	}
13455 	if (!ipif->ipif_isv6)
13456 		err = ipif_arp_down(ipif);
13457 
13458 	ill->ill_logical_down = 0;
13459 
13460 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13461 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13462 	return (err);
13463 }
13464 
13465 /*
13466  * Bring interface logically down without bringing the physical interface
13467  * down e.g. when the netmask is changed. This avoids long lasting link
13468  * negotiations between an ethernet interface and a certain switches.
13469  */
13470 static int
13471 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13472 {
13473 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13474 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13475 
13476 	/*
13477 	 * The ill_logical_down flag is a transient flag. It is set here
13478 	 * and is cleared once the down has completed in ipif_down_tail.
13479 	 * This flag does not indicate whether the ill stream is in the
13480 	 * DL_BOUND state with the driver. Instead this flag is used by
13481 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13482 	 * the driver. The state of the ill stream i.e. whether it is
13483 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13484 	 */
13485 	ipif->ipif_ill->ill_logical_down = 1;
13486 	return (ipif_down(ipif, q, mp));
13487 }
13488 
13489 /*
13490  * Initiate deallocate of an IPIF. Always called as writer. Called by
13491  * ill_delete or ip_sioctl_removeif.
13492  */
13493 static void
13494 ipif_free(ipif_t *ipif)
13495 {
13496 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13497 
13498 	ASSERT(IAM_WRITER_IPIF(ipif));
13499 
13500 	if (ipif->ipif_recovery_id != 0)
13501 		(void) untimeout(ipif->ipif_recovery_id);
13502 	ipif->ipif_recovery_id = 0;
13503 
13504 	/*
13505 	 * Take down the interface. We can be called either from ill_delete
13506 	 * or from ip_sioctl_removeif.
13507 	 */
13508 	(void) ipif_down(ipif, NULL, NULL);
13509 
13510 	/*
13511 	 * Now that the interface is down, there's no chance it can still
13512 	 * become a duplicate.  Cancel any timer that may have been set while
13513 	 * tearing down.
13514 	 */
13515 	if (ipif->ipif_recovery_id != 0)
13516 		(void) untimeout(ipif->ipif_recovery_id);
13517 	ipif->ipif_recovery_id = 0;
13518 
13519 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13520 	/* Remove pointers to this ill in the multicast routing tables */
13521 	reset_mrt_vif_ipif(ipif);
13522 	/* If necessary, clear the cached source ipif rotor. */
13523 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13524 		ipif->ipif_ill->ill_src_ipif = NULL;
13525 	rw_exit(&ipst->ips_ill_g_lock);
13526 }
13527 
13528 static void
13529 ipif_free_tail(ipif_t *ipif)
13530 {
13531 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13532 
13533 	/*
13534 	 * Need to hold both ill_g_lock and ill_lock while
13535 	 * inserting or removing an ipif from the linked list
13536 	 * of ipifs hanging off the ill.
13537 	 */
13538 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13539 
13540 #ifdef DEBUG
13541 	ipif_trace_cleanup(ipif);
13542 #endif
13543 
13544 	/* Ask SCTP to take it out of it list */
13545 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13546 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13547 
13548 	/* Get it out of the ILL interface list. */
13549 	ipif_remove(ipif);
13550 	rw_exit(&ipst->ips_ill_g_lock);
13551 
13552 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13553 	ASSERT(ipif->ipif_recovery_id == 0);
13554 	ASSERT(ipif->ipif_ire_local == NULL);
13555 	ASSERT(ipif->ipif_ire_if == NULL);
13556 
13557 	/* Free the memory. */
13558 	mi_free(ipif);
13559 }
13560 
13561 /*
13562  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13563  * is zero.
13564  */
13565 void
13566 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13567 {
13568 	char	lbuf[LIFNAMSIZ];
13569 	char	*name;
13570 	size_t	name_len;
13571 
13572 	buf[0] = '\0';
13573 	name = ipif->ipif_ill->ill_name;
13574 	name_len = ipif->ipif_ill->ill_name_length;
13575 	if (ipif->ipif_id != 0) {
13576 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13577 		    ipif->ipif_id);
13578 		name = lbuf;
13579 		name_len = mi_strlen(name) + 1;
13580 	}
13581 	len -= 1;
13582 	buf[len] = '\0';
13583 	len = MIN(len, name_len);
13584 	bcopy(name, buf, len);
13585 }
13586 
13587 /*
13588  * Sets `buf' to an ill name.
13589  */
13590 void
13591 ill_get_name(const ill_t *ill, char *buf, int len)
13592 {
13593 	char	*name;
13594 	size_t	name_len;
13595 
13596 	name = ill->ill_name;
13597 	name_len = ill->ill_name_length;
13598 	len -= 1;
13599 	buf[len] = '\0';
13600 	len = MIN(len, name_len);
13601 	bcopy(name, buf, len);
13602 }
13603 
13604 /*
13605  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13606  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13607  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13608  * (May be called as writer.)
13609  */
13610 static ipif_t *
13611 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13612     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13613 {
13614 	char	*cp;
13615 	char	*endp;
13616 	long	id;
13617 	ill_t	*ill;
13618 	ipif_t	*ipif;
13619 	uint_t	ire_type;
13620 	boolean_t did_alloc = B_FALSE;
13621 
13622 	/*
13623 	 * If the caller wants to us to create the ipif, make sure we have a
13624 	 * valid zoneid
13625 	 */
13626 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13627 
13628 	if (namelen == 0) {
13629 		return (NULL);
13630 	}
13631 
13632 	*exists = B_FALSE;
13633 	/* Look for a colon in the name. */
13634 	endp = &name[namelen];
13635 	for (cp = endp; --cp > name; ) {
13636 		if (*cp == IPIF_SEPARATOR_CHAR)
13637 			break;
13638 	}
13639 
13640 	if (*cp == IPIF_SEPARATOR_CHAR) {
13641 		/*
13642 		 * Reject any non-decimal aliases for logical
13643 		 * interfaces. Aliases with leading zeroes
13644 		 * are also rejected as they introduce ambiguity
13645 		 * in the naming of the interfaces.
13646 		 * In order to confirm with existing semantics,
13647 		 * and to not break any programs/script relying
13648 		 * on that behaviour, if<0>:0 is considered to be
13649 		 * a valid interface.
13650 		 *
13651 		 * If alias has two or more digits and the first
13652 		 * is zero, fail.
13653 		 */
13654 		if (&cp[2] < endp && cp[1] == '0') {
13655 			return (NULL);
13656 		}
13657 	}
13658 
13659 	if (cp <= name) {
13660 		cp = endp;
13661 	} else {
13662 		*cp = '\0';
13663 	}
13664 
13665 	/*
13666 	 * Look up the ILL, based on the portion of the name
13667 	 * before the slash. ill_lookup_on_name returns a held ill.
13668 	 * Temporary to check whether ill exists already. If so
13669 	 * ill_lookup_on_name will clear it.
13670 	 */
13671 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13672 	    &did_alloc, ipst);
13673 	if (cp != endp)
13674 		*cp = IPIF_SEPARATOR_CHAR;
13675 	if (ill == NULL)
13676 		return (NULL);
13677 
13678 	/* Establish the unit number in the name. */
13679 	id = 0;
13680 	if (cp < endp && *endp == '\0') {
13681 		/* If there was a colon, the unit number follows. */
13682 		cp++;
13683 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13684 			ill_refrele(ill);
13685 			return (NULL);
13686 		}
13687 	}
13688 
13689 	mutex_enter(&ill->ill_lock);
13690 	/* Now see if there is an IPIF with this unit number. */
13691 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13692 		if (ipif->ipif_id == id) {
13693 			if (zoneid != ALL_ZONES &&
13694 			    zoneid != ipif->ipif_zoneid &&
13695 			    ipif->ipif_zoneid != ALL_ZONES) {
13696 				mutex_exit(&ill->ill_lock);
13697 				ill_refrele(ill);
13698 				return (NULL);
13699 			}
13700 			if (IPIF_CAN_LOOKUP(ipif)) {
13701 				ipif_refhold_locked(ipif);
13702 				mutex_exit(&ill->ill_lock);
13703 				if (!did_alloc)
13704 					*exists = B_TRUE;
13705 				/*
13706 				 * Drop locks before calling ill_refrele
13707 				 * since it can potentially call into
13708 				 * ipif_ill_refrele_tail which can end up
13709 				 * in trying to acquire any lock.
13710 				 */
13711 				ill_refrele(ill);
13712 				return (ipif);
13713 			}
13714 		}
13715 	}
13716 
13717 	if (!do_alloc) {
13718 		mutex_exit(&ill->ill_lock);
13719 		ill_refrele(ill);
13720 		return (NULL);
13721 	}
13722 
13723 	/*
13724 	 * If none found, atomically allocate and return a new one.
13725 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13726 	 * to support "receive only" use of lo0:1 etc. as is still done
13727 	 * below as an initial guess.
13728 	 * However, this is now likely to be overriden later in ipif_up_done()
13729 	 * when we know for sure what address has been configured on the
13730 	 * interface, since we might have more than one loopback interface
13731 	 * with a loopback address, e.g. in the case of zones, and all the
13732 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13733 	 */
13734 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13735 		ire_type = IRE_LOOPBACK;
13736 	else
13737 		ire_type = IRE_LOCAL;
13738 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13739 	if (ipif != NULL)
13740 		ipif_refhold_locked(ipif);
13741 	mutex_exit(&ill->ill_lock);
13742 	ill_refrele(ill);
13743 	return (ipif);
13744 }
13745 
13746 /*
13747  * This routine is called whenever a new address comes up on an ipif.  If
13748  * we are configured to respond to address mask requests, then we are supposed
13749  * to broadcast an address mask reply at this time.  This routine is also
13750  * called if we are already up, but a netmask change is made.  This is legal
13751  * but might not make the system manager very popular.	(May be called
13752  * as writer.)
13753  */
13754 void
13755 ipif_mask_reply(ipif_t *ipif)
13756 {
13757 	icmph_t	*icmph;
13758 	ipha_t	*ipha;
13759 	mblk_t	*mp;
13760 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13761 	ip_xmit_attr_t ixas;
13762 
13763 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13764 
13765 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13766 		return;
13767 
13768 	/* ICMP mask reply is IPv4 only */
13769 	ASSERT(!ipif->ipif_isv6);
13770 	/* ICMP mask reply is not for a loopback interface */
13771 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13772 
13773 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13774 		return;
13775 
13776 	mp = allocb(REPLY_LEN, BPRI_HI);
13777 	if (mp == NULL)
13778 		return;
13779 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13780 
13781 	ipha = (ipha_t *)mp->b_rptr;
13782 	bzero(ipha, REPLY_LEN);
13783 	*ipha = icmp_ipha;
13784 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13785 	ipha->ipha_src = ipif->ipif_lcl_addr;
13786 	ipha->ipha_dst = ipif->ipif_brd_addr;
13787 	ipha->ipha_length = htons(REPLY_LEN);
13788 	ipha->ipha_ident = 0;
13789 
13790 	icmph = (icmph_t *)&ipha[1];
13791 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13792 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13793 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13794 
13795 	bzero(&ixas, sizeof (ixas));
13796 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13797 	ixas.ixa_zoneid = ALL_ZONES;
13798 	ixas.ixa_ifindex = 0;
13799 	ixas.ixa_ipst = ipst;
13800 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13801 	(void) ip_output_simple(mp, &ixas);
13802 	ixa_cleanup(&ixas);
13803 #undef	REPLY_LEN
13804 }
13805 
13806 /*
13807  * Join the ipif specific multicast groups.
13808  * Must be called after a mapping has been set up in the resolver.  (Always
13809  * called as writer.)
13810  */
13811 void
13812 ipif_multicast_up(ipif_t *ipif)
13813 {
13814 	int err;
13815 	ill_t *ill;
13816 	ilm_t *ilm;
13817 
13818 	ASSERT(IAM_WRITER_IPIF(ipif));
13819 
13820 	ill = ipif->ipif_ill;
13821 
13822 	ip1dbg(("ipif_multicast_up\n"));
13823 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13824 	    ipif->ipif_allhosts_ilm != NULL)
13825 		return;
13826 
13827 	if (ipif->ipif_isv6) {
13828 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13829 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13830 
13831 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13832 
13833 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13834 			return;
13835 
13836 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13837 
13838 		/*
13839 		 * Join the all hosts multicast address.  We skip this for
13840 		 * underlying IPMP interfaces since they should be invisible.
13841 		 */
13842 		if (!IS_UNDER_IPMP(ill)) {
13843 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13844 			    &err);
13845 			if (ilm == NULL) {
13846 				ASSERT(err != 0);
13847 				ip0dbg(("ipif_multicast_up: "
13848 				    "all_hosts_mcast failed %d\n", err));
13849 				return;
13850 			}
13851 			ipif->ipif_allhosts_ilm = ilm;
13852 		}
13853 
13854 		/*
13855 		 * Enable multicast for the solicited node multicast address.
13856 		 * If IPMP we need to put the membership on the upper ill.
13857 		 */
13858 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13859 			ill_t *mcast_ill = NULL;
13860 			boolean_t need_refrele;
13861 
13862 			if (IS_UNDER_IPMP(ill) &&
13863 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13864 				need_refrele = B_TRUE;
13865 			} else {
13866 				mcast_ill = ill;
13867 				need_refrele = B_FALSE;
13868 			}
13869 
13870 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13871 			    ipif->ipif_zoneid, &err);
13872 			if (need_refrele)
13873 				ill_refrele(mcast_ill);
13874 
13875 			if (ilm == NULL) {
13876 				ASSERT(err != 0);
13877 				ip0dbg(("ipif_multicast_up: solicited MC"
13878 				    " failed %d\n", err));
13879 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13880 					ipif->ipif_allhosts_ilm = NULL;
13881 					(void) ip_delmulti(ilm);
13882 				}
13883 				return;
13884 			}
13885 			ipif->ipif_solmulti_ilm = ilm;
13886 		}
13887 	} else {
13888 		in6_addr_t v6group;
13889 
13890 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13891 			return;
13892 
13893 		/* Join the all hosts multicast address */
13894 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13895 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13896 
13897 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13898 		if (ilm == NULL) {
13899 			ASSERT(err != 0);
13900 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13901 			return;
13902 		}
13903 		ipif->ipif_allhosts_ilm = ilm;
13904 	}
13905 }
13906 
13907 /*
13908  * Blow away any multicast groups that we joined in ipif_multicast_up().
13909  * (ilms from explicit memberships are handled in conn_update_ill.)
13910  */
13911 void
13912 ipif_multicast_down(ipif_t *ipif)
13913 {
13914 	ASSERT(IAM_WRITER_IPIF(ipif));
13915 
13916 	ip1dbg(("ipif_multicast_down\n"));
13917 
13918 	if (ipif->ipif_allhosts_ilm != NULL) {
13919 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13920 		ipif->ipif_allhosts_ilm = NULL;
13921 	}
13922 	if (ipif->ipif_solmulti_ilm != NULL) {
13923 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13924 		ipif->ipif_solmulti_ilm = NULL;
13925 	}
13926 }
13927 
13928 /*
13929  * Used when an interface comes up to recreate any extra routes on this
13930  * interface.
13931  */
13932 int
13933 ill_recover_saved_ire(ill_t *ill)
13934 {
13935 	mblk_t		*mp;
13936 	ip_stack_t	*ipst = ill->ill_ipst;
13937 
13938 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13939 
13940 	mutex_enter(&ill->ill_saved_ire_lock);
13941 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13942 		ire_t		*ire, *nire;
13943 		ifrt_t		*ifrt;
13944 
13945 		ifrt = (ifrt_t *)mp->b_rptr;
13946 		/*
13947 		 * Create a copy of the IRE with the saved address and netmask.
13948 		 */
13949 		if (ill->ill_isv6) {
13950 			ire = ire_create_v6(
13951 			    &ifrt->ifrt_v6addr,
13952 			    &ifrt->ifrt_v6mask,
13953 			    &ifrt->ifrt_v6gateway_addr,
13954 			    ifrt->ifrt_type,
13955 			    ill,
13956 			    ifrt->ifrt_zoneid,
13957 			    ifrt->ifrt_flags,
13958 			    NULL,
13959 			    ipst);
13960 		} else {
13961 			ire = ire_create(
13962 			    (uint8_t *)&ifrt->ifrt_addr,
13963 			    (uint8_t *)&ifrt->ifrt_mask,
13964 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13965 			    ifrt->ifrt_type,
13966 			    ill,
13967 			    ifrt->ifrt_zoneid,
13968 			    ifrt->ifrt_flags,
13969 			    NULL,
13970 			    ipst);
13971 		}
13972 		if (ire == NULL) {
13973 			mutex_exit(&ill->ill_saved_ire_lock);
13974 			return (ENOMEM);
13975 		}
13976 
13977 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13978 			if (ill->ill_isv6) {
13979 				ire->ire_setsrc_addr_v6 =
13980 				    ifrt->ifrt_v6setsrc_addr;
13981 			} else {
13982 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13983 			}
13984 		}
13985 
13986 		/*
13987 		 * Some software (for example, GateD and Sun Cluster) attempts
13988 		 * to create (what amount to) IRE_PREFIX routes with the
13989 		 * loopback address as the gateway.  This is primarily done to
13990 		 * set up prefixes with the RTF_REJECT flag set (for example,
13991 		 * when generating aggregate routes.)
13992 		 *
13993 		 * If the IRE type (as defined by ill->ill_net_type) is
13994 		 * IRE_LOOPBACK, then we map the request into a
13995 		 * IRE_IF_NORESOLVER.
13996 		 */
13997 		if (ill->ill_net_type == IRE_LOOPBACK)
13998 			ire->ire_type = IRE_IF_NORESOLVER;
13999 
14000 		/*
14001 		 * ire held by ire_add, will be refreled' towards the
14002 		 * the end of ipif_up_done
14003 		 */
14004 		nire = ire_add(ire);
14005 		/*
14006 		 * Check if it was a duplicate entry. This handles
14007 		 * the case of two racing route adds for the same route
14008 		 */
14009 		if (nire == NULL) {
14010 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14011 		} else if (nire != ire) {
14012 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14013 			    (void *)nire));
14014 			ire_delete(nire);
14015 		} else {
14016 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14017 			    (void *)nire));
14018 		}
14019 		if (nire != NULL)
14020 			ire_refrele(nire);
14021 	}
14022 	mutex_exit(&ill->ill_saved_ire_lock);
14023 	return (0);
14024 }
14025 
14026 /*
14027  * Used to set the netmask and broadcast address to default values when the
14028  * interface is brought up.  (Always called as writer.)
14029  */
14030 static void
14031 ipif_set_default(ipif_t *ipif)
14032 {
14033 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14034 
14035 	if (!ipif->ipif_isv6) {
14036 		/*
14037 		 * Interface holds an IPv4 address. Default
14038 		 * mask is the natural netmask.
14039 		 */
14040 		if (!ipif->ipif_net_mask) {
14041 			ipaddr_t	v4mask;
14042 
14043 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14044 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14045 		}
14046 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14047 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14048 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14049 		} else {
14050 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14051 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14052 		}
14053 		/*
14054 		 * NOTE: SunOS 4.X does this even if the broadcast address
14055 		 * has been already set thus we do the same here.
14056 		 */
14057 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14058 			ipaddr_t	v4addr;
14059 
14060 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14061 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14062 		}
14063 	} else {
14064 		/*
14065 		 * Interface holds an IPv6-only address.  Default
14066 		 * mask is all-ones.
14067 		 */
14068 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14069 			ipif->ipif_v6net_mask = ipv6_all_ones;
14070 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14071 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14072 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14073 		} else {
14074 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14075 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14076 		}
14077 	}
14078 }
14079 
14080 /*
14081  * Return 0 if this address can be used as local address without causing
14082  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14083  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14084  * Note that the same IPv6 link-local address is allowed as long as the ills
14085  * are not on the same link.
14086  */
14087 int
14088 ip_addr_availability_check(ipif_t *new_ipif)
14089 {
14090 	in6_addr_t our_v6addr;
14091 	ill_t *ill;
14092 	ipif_t *ipif;
14093 	ill_walk_context_t ctx;
14094 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14095 
14096 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14097 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14098 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14099 
14100 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14101 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14102 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14103 		return (0);
14104 
14105 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14106 
14107 	if (new_ipif->ipif_isv6)
14108 		ill = ILL_START_WALK_V6(&ctx, ipst);
14109 	else
14110 		ill = ILL_START_WALK_V4(&ctx, ipst);
14111 
14112 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14113 		for (ipif = ill->ill_ipif; ipif != NULL;
14114 		    ipif = ipif->ipif_next) {
14115 			if ((ipif == new_ipif) ||
14116 			    !(ipif->ipif_flags & IPIF_UP) ||
14117 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14118 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14119 			    &our_v6addr))
14120 				continue;
14121 
14122 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14123 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14124 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14125 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14126 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14127 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14128 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14129 				continue;
14130 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14131 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14132 				continue;
14133 			else if (new_ipif->ipif_ill == ill)
14134 				return (EADDRINUSE);
14135 			else
14136 				return (EADDRNOTAVAIL);
14137 		}
14138 	}
14139 
14140 	return (0);
14141 }
14142 
14143 /*
14144  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14145  * IREs for the ipif.
14146  * When the routine returns EINPROGRESS then mp has been consumed and
14147  * the ioctl will be acked from ip_rput_dlpi.
14148  */
14149 int
14150 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14151 {
14152 	ill_t		*ill = ipif->ipif_ill;
14153 	boolean_t 	isv6 = ipif->ipif_isv6;
14154 	int		err = 0;
14155 	boolean_t	success;
14156 	uint_t		ipif_orig_id;
14157 	ip_stack_t	*ipst = ill->ill_ipst;
14158 
14159 	ASSERT(IAM_WRITER_IPIF(ipif));
14160 
14161 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14162 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14163 	    ill_t *, ill, ipif_t *, ipif);
14164 
14165 	/* Shouldn't get here if it is already up. */
14166 	if (ipif->ipif_flags & IPIF_UP)
14167 		return (EALREADY);
14168 
14169 	/*
14170 	 * If this is a request to bring up a data address on an interface
14171 	 * under IPMP, then move the address to its IPMP meta-interface and
14172 	 * try to bring it up.  One complication is that the zeroth ipif for
14173 	 * an ill is special, in that every ill always has one, and that code
14174 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14175 	 */
14176 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14177 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14178 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14179 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14180 
14181 		/*
14182 		 * The ipif being brought up should be quiesced.  If it's not,
14183 		 * something has gone amiss and we need to bail out.  (If it's
14184 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14185 		 */
14186 		mutex_enter(&ill->ill_lock);
14187 		if (!ipif_is_quiescent(ipif)) {
14188 			mutex_exit(&ill->ill_lock);
14189 			return (EINVAL);
14190 		}
14191 		mutex_exit(&ill->ill_lock);
14192 
14193 		/*
14194 		 * If we're going to need to allocate ipifs, do it prior
14195 		 * to starting the move (and grabbing locks).
14196 		 */
14197 		if (ipif->ipif_id == 0) {
14198 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14199 			    B_FALSE, &err)) == NULL) {
14200 				return (err);
14201 			}
14202 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14203 			    B_FALSE, &err)) == NULL) {
14204 				mi_free(moveipif);
14205 				return (err);
14206 			}
14207 		}
14208 
14209 		/*
14210 		 * Grab or transfer the ipif to move.  During the move, keep
14211 		 * ill_g_lock held to prevent any ill walker threads from
14212 		 * seeing things in an inconsistent state.
14213 		 */
14214 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14215 		if (ipif->ipif_id != 0) {
14216 			ipif_remove(ipif);
14217 		} else {
14218 			ipif_transfer(ipif, moveipif, stubipif);
14219 			ipif = moveipif;
14220 		}
14221 
14222 		/*
14223 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14224 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14225 		 * replace that one.  Otherwise, pick the next available slot.
14226 		 */
14227 		ipif->ipif_ill = ipmp_ill;
14228 		ipif_orig_id = ipif->ipif_id;
14229 
14230 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14231 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14232 			ipif = ipmp_ill->ill_ipif;
14233 		} else {
14234 			ipif->ipif_id = -1;
14235 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14236 				/*
14237 				 * No more available ipif_id's -- put it back
14238 				 * on the original ill and fail the operation.
14239 				 * Since we're writer on the ill, we can be
14240 				 * sure our old slot is still available.
14241 				 */
14242 				ipif->ipif_id = ipif_orig_id;
14243 				ipif->ipif_ill = ill;
14244 				if (ipif_orig_id == 0) {
14245 					ipif_transfer(ipif, ill->ill_ipif,
14246 					    NULL);
14247 				} else {
14248 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14249 				}
14250 				rw_exit(&ipst->ips_ill_g_lock);
14251 				return (err);
14252 			}
14253 		}
14254 		rw_exit(&ipst->ips_ill_g_lock);
14255 
14256 		/*
14257 		 * Tell SCTP that the ipif has moved.  Note that even if we
14258 		 * had to allocate a new ipif, the original sequence id was
14259 		 * preserved and therefore SCTP won't know.
14260 		 */
14261 		sctp_move_ipif(ipif, ill, ipmp_ill);
14262 
14263 		/*
14264 		 * If the ipif being brought up was on slot zero, then we
14265 		 * first need to bring up the placeholder we stuck there.  In
14266 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14267 		 * call to ipif_up() itself, if we successfully bring up the
14268 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14269 		 */
14270 		if (ipif_orig_id == 0) {
14271 			ASSERT(ill->ill_move_ipif == NULL);
14272 			ill->ill_move_ipif = ipif;
14273 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14274 				ASSERT(ill->ill_move_ipif == NULL);
14275 			if (err != EINPROGRESS)
14276 				ill->ill_move_ipif = NULL;
14277 			return (err);
14278 		}
14279 
14280 		/*
14281 		 * Bring it up on the IPMP ill.
14282 		 */
14283 		return (ipif_up(ipif, q, mp));
14284 	}
14285 
14286 	/* Skip arp/ndp for any loopback interface. */
14287 	if (ill->ill_wq != NULL) {
14288 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14289 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14290 
14291 		if (!ill->ill_dl_up) {
14292 			/*
14293 			 * ill_dl_up is not yet set. i.e. we are yet to
14294 			 * DL_BIND with the driver and this is the first
14295 			 * logical interface on the ill to become "up".
14296 			 * Tell the driver to get going (via DL_BIND_REQ).
14297 			 * Note that changing "significant" IFF_ flags
14298 			 * address/netmask etc cause a down/up dance, but
14299 			 * does not cause an unbind (DL_UNBIND) with the driver
14300 			 */
14301 			return (ill_dl_up(ill, ipif, mp, q));
14302 		}
14303 
14304 		/*
14305 		 * ipif_resolver_up may end up needeing to bind/attach
14306 		 * the ARP stream, which in turn necessitates a
14307 		 * DLPI message exchange with the driver. ioctls are
14308 		 * serialized and so we cannot send more than one
14309 		 * interface up message at a time. If ipif_resolver_up
14310 		 * does need to wait for the DLPI handshake for the ARP stream,
14311 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14312 		 */
14313 
14314 		ASSERT(connp != NULL || !CONN_Q(q));
14315 		if (connp != NULL)
14316 			mutex_enter(&connp->conn_lock);
14317 		mutex_enter(&ill->ill_lock);
14318 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14319 		mutex_exit(&ill->ill_lock);
14320 		if (connp != NULL)
14321 			mutex_exit(&connp->conn_lock);
14322 		if (!success)
14323 			return (EINTR);
14324 
14325 		/*
14326 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14327 		 * complete when ipif_ndp_up returns.
14328 		 */
14329 		err = ipif_resolver_up(ipif, Res_act_initial);
14330 		if (err == EINPROGRESS) {
14331 			/* We will complete it in arp_bringup_done() */
14332 			return (err);
14333 		}
14334 
14335 		if (isv6 && err == 0)
14336 			err = ipif_ndp_up(ipif, B_TRUE);
14337 
14338 		ASSERT(err != EINPROGRESS);
14339 		mp = ipsq_pending_mp_get(ipsq, &connp);
14340 		ASSERT(mp != NULL);
14341 		if (err != 0)
14342 			return (err);
14343 	} else {
14344 		/*
14345 		 * Interfaces without underlying hardware don't do duplicate
14346 		 * address detection.
14347 		 */
14348 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14349 		ipif->ipif_addr_ready = 1;
14350 		err = ill_add_ires(ill);
14351 		/* allocation failure? */
14352 		if (err != 0)
14353 			return (err);
14354 	}
14355 
14356 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14357 	if (err == 0 && ill->ill_move_ipif != NULL) {
14358 		ipif = ill->ill_move_ipif;
14359 		ill->ill_move_ipif = NULL;
14360 		return (ipif_up(ipif, q, mp));
14361 	}
14362 	return (err);
14363 }
14364 
14365 /*
14366  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14367  * The identical set of IREs need to be removed in ill_delete_ires().
14368  */
14369 int
14370 ill_add_ires(ill_t *ill)
14371 {
14372 	ire_t	*ire;
14373 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14374 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14375 
14376 	if (ill->ill_ire_multicast != NULL)
14377 		return (0);
14378 
14379 	/*
14380 	 * provide some dummy ire_addr for creating the ire.
14381 	 */
14382 	if (ill->ill_isv6) {
14383 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14384 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14385 	} else {
14386 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14387 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14388 	}
14389 	if (ire == NULL)
14390 		return (ENOMEM);
14391 
14392 	ill->ill_ire_multicast = ire;
14393 	return (0);
14394 }
14395 
14396 void
14397 ill_delete_ires(ill_t *ill)
14398 {
14399 	if (ill->ill_ire_multicast != NULL) {
14400 		/*
14401 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14402 		 * which was taken without any th_tracing enabled.
14403 		 * We also mark it as condemned (note that it was never added)
14404 		 * so that caching conn's can move off of it.
14405 		 */
14406 		ire_make_condemned(ill->ill_ire_multicast);
14407 		ire_refrele_notr(ill->ill_ire_multicast);
14408 		ill->ill_ire_multicast = NULL;
14409 	}
14410 }
14411 
14412 /*
14413  * Perform a bind for the physical device.
14414  * When the routine returns EINPROGRESS then mp has been consumed and
14415  * the ioctl will be acked from ip_rput_dlpi.
14416  * Allocate an unbind message and save it until ipif_down.
14417  */
14418 static int
14419 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14420 {
14421 	mblk_t	*bind_mp = NULL;
14422 	mblk_t	*unbind_mp = NULL;
14423 	conn_t	*connp;
14424 	boolean_t success;
14425 	int	err;
14426 
14427 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14428 
14429 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14430 	ASSERT(IAM_WRITER_ILL(ill));
14431 	ASSERT(mp != NULL);
14432 
14433 	/*
14434 	 * Make sure we have an IRE_MULTICAST in case we immediately
14435 	 * start receiving packets.
14436 	 */
14437 	err = ill_add_ires(ill);
14438 	if (err != 0)
14439 		goto bad;
14440 
14441 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14442 	    DL_BIND_REQ);
14443 	if (bind_mp == NULL)
14444 		goto bad;
14445 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14446 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14447 
14448 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14449 	if (unbind_mp == NULL)
14450 		goto bad;
14451 
14452 	/*
14453 	 * Record state needed to complete this operation when the
14454 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14455 	 */
14456 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14457 	ASSERT(connp != NULL || !CONN_Q(q));
14458 	GRAB_CONN_LOCK(q);
14459 	mutex_enter(&ipif->ipif_ill->ill_lock);
14460 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14461 	mutex_exit(&ipif->ipif_ill->ill_lock);
14462 	RELEASE_CONN_LOCK(q);
14463 	if (!success)
14464 		goto bad;
14465 
14466 	/*
14467 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14468 	 * the interface goes down.
14469 	 */
14470 	ASSERT(ill->ill_unbind_mp == NULL);
14471 	ill->ill_unbind_mp = unbind_mp;
14472 
14473 	ill_dlpi_send(ill, bind_mp);
14474 	/* Send down link-layer capabilities probe if not already done. */
14475 	ill_capability_probe(ill);
14476 
14477 	/*
14478 	 * Sysid used to rely on the fact that netboots set domainname
14479 	 * and the like. Now that miniroot boots aren't strictly netboots
14480 	 * and miniroot network configuration is driven from userland
14481 	 * these things still need to be set. This situation can be detected
14482 	 * by comparing the interface being configured here to the one
14483 	 * dhcifname was set to reference by the boot loader. Once sysid is
14484 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14485 	 */
14486 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14487 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14488 	    (strlen(srpc_domain) == 0)) {
14489 		if (dhcpinit() != 0)
14490 			cmn_err(CE_WARN, "no cached dhcp response");
14491 	}
14492 
14493 	/*
14494 	 * This operation will complete in ip_rput_dlpi with either
14495 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14496 	 */
14497 	return (EINPROGRESS);
14498 bad:
14499 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14500 
14501 	freemsg(bind_mp);
14502 	freemsg(unbind_mp);
14503 	return (ENOMEM);
14504 }
14505 
14506 /* Add room for tcp+ip headers */
14507 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14508 
14509 /*
14510  * DLPI and ARP is up.
14511  * Create all the IREs associated with an interface. Bring up multicast.
14512  * Set the interface flag and finish other initialization
14513  * that potentially had to be deferred to after DL_BIND_ACK.
14514  */
14515 int
14516 ipif_up_done(ipif_t *ipif)
14517 {
14518 	ill_t		*ill = ipif->ipif_ill;
14519 	int		err = 0;
14520 	boolean_t	loopback = B_FALSE;
14521 	boolean_t	update_src_selection = B_TRUE;
14522 	ipif_t		*tmp_ipif;
14523 
14524 	ip1dbg(("ipif_up_done(%s:%u)\n",
14525 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14526 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14527 	    ill_t *, ill, ipif_t *, ipif);
14528 
14529 	/* Check if this is a loopback interface */
14530 	if (ipif->ipif_ill->ill_wq == NULL)
14531 		loopback = B_TRUE;
14532 
14533 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14534 
14535 	/*
14536 	 * If all other interfaces for this ill are down or DEPRECATED,
14537 	 * or otherwise unsuitable for source address selection,
14538 	 * reset the src generation numbers to make sure source
14539 	 * address selection gets to take this new ipif into account.
14540 	 * No need to hold ill_lock while traversing the ipif list since
14541 	 * we are writer
14542 	 */
14543 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14544 	    tmp_ipif = tmp_ipif->ipif_next) {
14545 		if (((tmp_ipif->ipif_flags &
14546 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14547 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14548 		    (tmp_ipif == ipif))
14549 			continue;
14550 		/* first useable pre-existing interface */
14551 		update_src_selection = B_FALSE;
14552 		break;
14553 	}
14554 	if (update_src_selection)
14555 		ip_update_source_selection(ill->ill_ipst);
14556 
14557 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14558 		nce_t *loop_nce = NULL;
14559 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14560 
14561 		/*
14562 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14563 		 * ipif_lookup_on_name(), but in the case of zones we can have
14564 		 * several loopback addresses on lo0. So all the interfaces with
14565 		 * loopback addresses need to be marked IRE_LOOPBACK.
14566 		 */
14567 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14568 		    htonl(INADDR_LOOPBACK))
14569 			ipif->ipif_ire_type = IRE_LOOPBACK;
14570 		else
14571 			ipif->ipif_ire_type = IRE_LOCAL;
14572 		if (ill->ill_net_type != IRE_LOOPBACK)
14573 			flags |= NCE_F_PUBLISH;
14574 
14575 		/* add unicast nce for the local addr */
14576 		err = nce_lookup_then_add_v4(ill, NULL,
14577 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14578 		    ND_REACHABLE, &loop_nce);
14579 		/* A shared-IP zone sees EEXIST for lo0:N */
14580 		if (err == 0 || err == EEXIST) {
14581 			ipif->ipif_added_nce = 1;
14582 			loop_nce->nce_ipif_cnt++;
14583 			nce_refrele(loop_nce);
14584 			err = 0;
14585 		} else {
14586 			ASSERT(loop_nce == NULL);
14587 			return (err);
14588 		}
14589 	}
14590 
14591 	/* Create all the IREs associated with this interface */
14592 	err = ipif_add_ires_v4(ipif, loopback);
14593 	if (err != 0) {
14594 		/*
14595 		 * see comments about return value from
14596 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14597 		 */
14598 		if (err != EADDRINUSE) {
14599 			(void) ipif_arp_down(ipif);
14600 		} else {
14601 			/*
14602 			 * Make IPMP aware of the deleted ipif so that
14603 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14604 			 * can be completed. Note that we do not want to
14605 			 * destroy the nce that was created on the ipmp_ill
14606 			 * for the active copy of the duplicate address in
14607 			 * use.
14608 			 */
14609 			if (IS_IPMP(ill))
14610 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14611 			err = EADDRNOTAVAIL;
14612 		}
14613 		return (err);
14614 	}
14615 
14616 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14617 		/* Recover any additional IREs entries for this ill */
14618 		(void) ill_recover_saved_ire(ill);
14619 	}
14620 
14621 	if (ill->ill_need_recover_multicast) {
14622 		/*
14623 		 * Need to recover all multicast memberships in the driver.
14624 		 * This had to be deferred until we had attached.  The same
14625 		 * code exists in ipif_up_done_v6() to recover IPv6
14626 		 * memberships.
14627 		 *
14628 		 * Note that it would be preferable to unconditionally do the
14629 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14630 		 * that since ill_join_allmulti() depends on ill_dl_up being
14631 		 * set, and it is not set until we receive a DL_BIND_ACK after
14632 		 * having called ill_dl_up().
14633 		 */
14634 		ill_recover_multicast(ill);
14635 	}
14636 
14637 	if (ill->ill_ipif_up_count == 1) {
14638 		/*
14639 		 * Since the interface is now up, it may now be active.
14640 		 */
14641 		if (IS_UNDER_IPMP(ill))
14642 			ipmp_ill_refresh_active(ill);
14643 
14644 		/*
14645 		 * If this is an IPMP interface, we may now be able to
14646 		 * establish ARP entries.
14647 		 */
14648 		if (IS_IPMP(ill))
14649 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14650 	}
14651 
14652 	/* Join the allhosts multicast address */
14653 	ipif_multicast_up(ipif);
14654 
14655 	if (!loopback && !update_src_selection &&
14656 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14657 		ip_update_source_selection(ill->ill_ipst);
14658 
14659 	if (!loopback && ipif->ipif_addr_ready) {
14660 		/* Broadcast an address mask reply. */
14661 		ipif_mask_reply(ipif);
14662 	}
14663 	/* Perhaps ilgs should use this ill */
14664 	update_conn_ill(NULL, ill->ill_ipst);
14665 
14666 	/*
14667 	 * This had to be deferred until we had bound.  Tell routing sockets and
14668 	 * others that this interface is up if it looks like the address has
14669 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14670 	 * duplicate address detection to do its thing.
14671 	 */
14672 	if (ipif->ipif_addr_ready)
14673 		ipif_up_notify(ipif);
14674 	return (0);
14675 }
14676 
14677 /*
14678  * Add the IREs associated with the ipif.
14679  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14680  */
14681 static int
14682 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14683 {
14684 	ill_t		*ill = ipif->ipif_ill;
14685 	ip_stack_t	*ipst = ill->ill_ipst;
14686 	ire_t		*ire_array[20];
14687 	ire_t		**irep = ire_array;
14688 	ire_t		**irep1;
14689 	ipaddr_t	net_mask = 0;
14690 	ipaddr_t	subnet_mask, route_mask;
14691 	int		err;
14692 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14693 	ire_t		*ire_if = NULL;
14694 	uchar_t		*gw;
14695 
14696 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14697 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14698 		/*
14699 		 * If we're on a labeled system then make sure that zone-
14700 		 * private addresses have proper remote host database entries.
14701 		 */
14702 		if (is_system_labeled() &&
14703 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14704 		    !tsol_check_interface_address(ipif))
14705 			return (EINVAL);
14706 
14707 		/* Register the source address for __sin6_src_id */
14708 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14709 		    ipif->ipif_zoneid, ipst);
14710 		if (err != 0) {
14711 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14712 			return (err);
14713 		}
14714 
14715 		if (loopback)
14716 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14717 		else
14718 			gw = NULL;
14719 
14720 		/* If the interface address is set, create the local IRE. */
14721 		ire_local = ire_create(
14722 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14723 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14724 		    gw,					/* gateway */
14725 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14726 		    ipif->ipif_ill,
14727 		    ipif->ipif_zoneid,
14728 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14729 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14730 		    NULL,
14731 		    ipst);
14732 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14733 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14734 		    ipif->ipif_ire_type,
14735 		    ntohl(ipif->ipif_lcl_addr)));
14736 		if (ire_local == NULL) {
14737 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14738 			err = ENOMEM;
14739 			goto bad;
14740 		}
14741 	} else {
14742 		ip1dbg((
14743 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14744 		    ipif->ipif_ire_type,
14745 		    ntohl(ipif->ipif_lcl_addr),
14746 		    (uint_t)ipif->ipif_flags));
14747 	}
14748 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14749 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14750 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14751 	} else {
14752 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14753 	}
14754 
14755 	subnet_mask = ipif->ipif_net_mask;
14756 
14757 	/*
14758 	 * If mask was not specified, use natural netmask of
14759 	 * interface address. Also, store this mask back into the
14760 	 * ipif struct.
14761 	 */
14762 	if (subnet_mask == 0) {
14763 		subnet_mask = net_mask;
14764 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14765 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14766 		    ipif->ipif_v6subnet);
14767 	}
14768 
14769 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14770 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14771 	    ipif->ipif_subnet != INADDR_ANY) {
14772 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14773 
14774 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14775 			route_mask = IP_HOST_MASK;
14776 		} else {
14777 			route_mask = subnet_mask;
14778 		}
14779 
14780 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14781 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14782 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14783 		    ntohl(ipif->ipif_subnet)));
14784 		ire_if = ire_create(
14785 		    (uchar_t *)&ipif->ipif_subnet,
14786 		    (uchar_t *)&route_mask,
14787 		    (uchar_t *)&ipif->ipif_lcl_addr,
14788 		    ill->ill_net_type,
14789 		    ill,
14790 		    ipif->ipif_zoneid,
14791 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14792 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14793 		    NULL,
14794 		    ipst);
14795 		if (ire_if == NULL) {
14796 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14797 			err = ENOMEM;
14798 			goto bad;
14799 		}
14800 	}
14801 
14802 	/*
14803 	 * Create any necessary broadcast IREs.
14804 	 */
14805 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14806 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14807 		irep = ipif_create_bcast_ires(ipif, irep);
14808 
14809 	/* If an earlier ire_create failed, get out now */
14810 	for (irep1 = irep; irep1 > ire_array; ) {
14811 		irep1--;
14812 		if (*irep1 == NULL) {
14813 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14814 			err = ENOMEM;
14815 			goto bad;
14816 		}
14817 	}
14818 
14819 	/*
14820 	 * Need to atomically check for IP address availability under
14821 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14822 	 * ills or new ipifs can be added while we are checking availability.
14823 	 */
14824 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14825 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14826 	/* Mark it up, and increment counters. */
14827 	ipif->ipif_flags |= IPIF_UP;
14828 	ill->ill_ipif_up_count++;
14829 	err = ip_addr_availability_check(ipif);
14830 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14831 	rw_exit(&ipst->ips_ill_g_lock);
14832 
14833 	if (err != 0) {
14834 		/*
14835 		 * Our address may already be up on the same ill. In this case,
14836 		 * the ARP entry for our ipif replaced the one for the other
14837 		 * ipif. So we don't want to delete it (otherwise the other ipif
14838 		 * would be unable to send packets).
14839 		 * ip_addr_availability_check() identifies this case for us and
14840 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14841 		 * which is the expected error code.
14842 		 */
14843 		ill->ill_ipif_up_count--;
14844 		ipif->ipif_flags &= ~IPIF_UP;
14845 		goto bad;
14846 	}
14847 
14848 	/*
14849 	 * Add in all newly created IREs.  ire_create_bcast() has
14850 	 * already checked for duplicates of the IRE_BROADCAST type.
14851 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14852 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14853 	 * a /32 route.
14854 	 */
14855 	if (ire_if != NULL) {
14856 		ire_if = ire_add(ire_if);
14857 		if (ire_if == NULL) {
14858 			err = ENOMEM;
14859 			goto bad2;
14860 		}
14861 #ifdef DEBUG
14862 		ire_refhold_notr(ire_if);
14863 		ire_refrele(ire_if);
14864 #endif
14865 	}
14866 	if (ire_local != NULL) {
14867 		ire_local = ire_add(ire_local);
14868 		if (ire_local == NULL) {
14869 			err = ENOMEM;
14870 			goto bad2;
14871 		}
14872 #ifdef DEBUG
14873 		ire_refhold_notr(ire_local);
14874 		ire_refrele(ire_local);
14875 #endif
14876 	}
14877 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14878 	if (ire_local != NULL)
14879 		ipif->ipif_ire_local = ire_local;
14880 	if (ire_if != NULL)
14881 		ipif->ipif_ire_if = ire_if;
14882 	rw_exit(&ipst->ips_ill_g_lock);
14883 	ire_local = NULL;
14884 	ire_if = NULL;
14885 
14886 	/*
14887 	 * We first add all of them, and if that succeeds we refrele the
14888 	 * bunch. That enables us to delete all of them should any of the
14889 	 * ire_adds fail.
14890 	 */
14891 	for (irep1 = irep; irep1 > ire_array; ) {
14892 		irep1--;
14893 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14894 		*irep1 = ire_add(*irep1);
14895 		if (*irep1 == NULL) {
14896 			err = ENOMEM;
14897 			goto bad2;
14898 		}
14899 	}
14900 
14901 	for (irep1 = irep; irep1 > ire_array; ) {
14902 		irep1--;
14903 		/* refheld by ire_add. */
14904 		if (*irep1 != NULL) {
14905 			ire_refrele(*irep1);
14906 			*irep1 = NULL;
14907 		}
14908 	}
14909 
14910 	if (!loopback) {
14911 		/*
14912 		 * If the broadcast address has been set, make sure it makes
14913 		 * sense based on the interface address.
14914 		 * Only match on ill since we are sharing broadcast addresses.
14915 		 */
14916 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14917 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14918 			ire_t	*ire;
14919 
14920 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14921 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14922 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14923 
14924 			if (ire == NULL) {
14925 				/*
14926 				 * If there isn't a matching broadcast IRE,
14927 				 * revert to the default for this netmask.
14928 				 */
14929 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14930 				mutex_enter(&ipif->ipif_ill->ill_lock);
14931 				ipif_set_default(ipif);
14932 				mutex_exit(&ipif->ipif_ill->ill_lock);
14933 			} else {
14934 				ire_refrele(ire);
14935 			}
14936 		}
14937 
14938 	}
14939 	return (0);
14940 
14941 bad2:
14942 	ill->ill_ipif_up_count--;
14943 	ipif->ipif_flags &= ~IPIF_UP;
14944 
14945 bad:
14946 	ip1dbg(("ipif_add_ires: FAILED \n"));
14947 	if (ire_local != NULL)
14948 		ire_delete(ire_local);
14949 	if (ire_if != NULL)
14950 		ire_delete(ire_if);
14951 
14952 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14953 	ire_local = ipif->ipif_ire_local;
14954 	ipif->ipif_ire_local = NULL;
14955 	ire_if = ipif->ipif_ire_if;
14956 	ipif->ipif_ire_if = NULL;
14957 	rw_exit(&ipst->ips_ill_g_lock);
14958 	if (ire_local != NULL) {
14959 		ire_delete(ire_local);
14960 		ire_refrele_notr(ire_local);
14961 	}
14962 	if (ire_if != NULL) {
14963 		ire_delete(ire_if);
14964 		ire_refrele_notr(ire_if);
14965 	}
14966 
14967 	while (irep > ire_array) {
14968 		irep--;
14969 		if (*irep != NULL) {
14970 			ire_delete(*irep);
14971 		}
14972 	}
14973 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14974 
14975 	return (err);
14976 }
14977 
14978 /* Remove all the IREs created by ipif_add_ires_v4 */
14979 void
14980 ipif_delete_ires_v4(ipif_t *ipif)
14981 {
14982 	ill_t		*ill = ipif->ipif_ill;
14983 	ip_stack_t	*ipst = ill->ill_ipst;
14984 	ire_t		*ire;
14985 
14986 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14987 	ire = ipif->ipif_ire_local;
14988 	ipif->ipif_ire_local = NULL;
14989 	rw_exit(&ipst->ips_ill_g_lock);
14990 	if (ire != NULL) {
14991 		/*
14992 		 * Move count to ipif so we don't loose the count due to
14993 		 * a down/up dance.
14994 		 */
14995 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14996 
14997 		ire_delete(ire);
14998 		ire_refrele_notr(ire);
14999 	}
15000 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15001 	ire = ipif->ipif_ire_if;
15002 	ipif->ipif_ire_if = NULL;
15003 	rw_exit(&ipst->ips_ill_g_lock);
15004 	if (ire != NULL) {
15005 		ire_delete(ire);
15006 		ire_refrele_notr(ire);
15007 	}
15008 
15009 	/*
15010 	 * Delete the broadcast IREs.
15011 	 */
15012 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15013 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15014 		ipif_delete_bcast_ires(ipif);
15015 }
15016 
15017 /*
15018  * Checks for availbility of a usable source address (if there is one) when the
15019  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15020  * this selection is done regardless of the destination.
15021  */
15022 boolean_t
15023 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15024     ip_stack_t *ipst)
15025 {
15026 	ipif_t		*ipif = NULL;
15027 	ill_t		*uill;
15028 
15029 	ASSERT(ifindex != 0);
15030 
15031 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15032 	if (uill == NULL)
15033 		return (B_FALSE);
15034 
15035 	mutex_enter(&uill->ill_lock);
15036 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15037 		if (IPIF_IS_CONDEMNED(ipif))
15038 			continue;
15039 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15040 			continue;
15041 		if (!(ipif->ipif_flags & IPIF_UP))
15042 			continue;
15043 		if (ipif->ipif_zoneid != zoneid)
15044 			continue;
15045 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15046 		    ipif->ipif_lcl_addr == INADDR_ANY)
15047 			continue;
15048 		mutex_exit(&uill->ill_lock);
15049 		ill_refrele(uill);
15050 		return (B_TRUE);
15051 	}
15052 	mutex_exit(&uill->ill_lock);
15053 	ill_refrele(uill);
15054 	return (B_FALSE);
15055 }
15056 
15057 /*
15058  * Find an ipif with a good local address on the ill+zoneid.
15059  */
15060 ipif_t *
15061 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15062 {
15063 	ipif_t		*ipif;
15064 
15065 	mutex_enter(&ill->ill_lock);
15066 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15067 		if (IPIF_IS_CONDEMNED(ipif))
15068 			continue;
15069 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15070 			continue;
15071 		if (!(ipif->ipif_flags & IPIF_UP))
15072 			continue;
15073 		if (ipif->ipif_zoneid != zoneid &&
15074 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15075 			continue;
15076 		if (ill->ill_isv6 ?
15077 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15078 		    ipif->ipif_lcl_addr == INADDR_ANY)
15079 			continue;
15080 		ipif_refhold_locked(ipif);
15081 		mutex_exit(&ill->ill_lock);
15082 		return (ipif);
15083 	}
15084 	mutex_exit(&ill->ill_lock);
15085 	return (NULL);
15086 }
15087 
15088 /*
15089  * IP source address type, sorted from worst to best.  For a given type,
15090  * always prefer IP addresses on the same subnet.  All-zones addresses are
15091  * suboptimal because they pose problems with unlabeled destinations.
15092  */
15093 typedef enum {
15094 	IPIF_NONE,
15095 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
15096 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
15097 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15098 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15099 	IPIF_DIFFNET,			/* normal and different subnet */
15100 	IPIF_SAMENET,			/* normal and same subnet */
15101 	IPIF_LOCALADDR			/* local loopback */
15102 } ipif_type_t;
15103 
15104 /*
15105  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15106  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15107  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15108  * the first one, unless IPMP is used in which case we round-robin among them;
15109  * see below for more.
15110  *
15111  * Returns NULL if there is no suitable source address for the ill.
15112  * This only occurs when there is no valid source address for the ill.
15113  */
15114 ipif_t *
15115 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15116     boolean_t allow_usesrc, boolean_t *notreadyp)
15117 {
15118 	ill_t	*usill = NULL;
15119 	ill_t	*ipmp_ill = NULL;
15120 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15121 	ipif_type_t type, best_type;
15122 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15123 	ip_stack_t *ipst = ill->ill_ipst;
15124 	boolean_t samenet;
15125 
15126 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15127 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15128 		    B_FALSE, ipst);
15129 		if (usill != NULL)
15130 			ill = usill;	/* Select source from usesrc ILL */
15131 		else
15132 			return (NULL);
15133 	}
15134 
15135 	/*
15136 	 * Test addresses should never be used for source address selection,
15137 	 * so if we were passed one, switch to the IPMP meta-interface.
15138 	 */
15139 	if (IS_UNDER_IPMP(ill)) {
15140 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15141 			ill = ipmp_ill;	/* Select source from IPMP ill */
15142 		else
15143 			return (NULL);
15144 	}
15145 
15146 	/*
15147 	 * If we're dealing with an unlabeled destination on a labeled system,
15148 	 * make sure that we ignore source addresses that are incompatible with
15149 	 * the destination's default label.  That destination's default label
15150 	 * must dominate the minimum label on the source address.
15151 	 */
15152 	dst_rhtp = NULL;
15153 	if (is_system_labeled()) {
15154 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15155 		if (dst_rhtp == NULL)
15156 			return (NULL);
15157 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15158 			TPC_RELE(dst_rhtp);
15159 			dst_rhtp = NULL;
15160 		}
15161 	}
15162 
15163 	/*
15164 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15165 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15166 	 * After selecting the right ipif, under ill_lock make sure ipif is
15167 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15168 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15169 	 * but not under a lock.
15170 	 */
15171 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15172 retry:
15173 	/*
15174 	 * For source address selection, we treat the ipif list as circular
15175 	 * and continue until we get back to where we started.  This allows
15176 	 * IPMP to vary source address selection (which improves inbound load
15177 	 * spreading) by caching its last ending point and starting from
15178 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15179 	 * ills since that can't happen on the IPMP ill.
15180 	 */
15181 	start_ipif = ill->ill_ipif;
15182 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15183 		start_ipif = ill->ill_src_ipif;
15184 
15185 	ipif = start_ipif;
15186 	best_ipif = NULL;
15187 	best_type = IPIF_NONE;
15188 	do {
15189 		if ((next_ipif = ipif->ipif_next) == NULL)
15190 			next_ipif = ill->ill_ipif;
15191 
15192 		if (IPIF_IS_CONDEMNED(ipif))
15193 			continue;
15194 		/* Always skip NOLOCAL and ANYCAST interfaces */
15195 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15196 			continue;
15197 		/* Always skip NOACCEPT interfaces */
15198 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15199 			continue;
15200 		if (!(ipif->ipif_flags & IPIF_UP))
15201 			continue;
15202 
15203 		if (!ipif->ipif_addr_ready) {
15204 			if (notreadyp != NULL)
15205 				*notreadyp = B_TRUE;
15206 			continue;
15207 		}
15208 
15209 		if (zoneid != ALL_ZONES &&
15210 		    ipif->ipif_zoneid != zoneid &&
15211 		    ipif->ipif_zoneid != ALL_ZONES)
15212 			continue;
15213 
15214 		/*
15215 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15216 		 * are not valid as source addresses.
15217 		 */
15218 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15219 			continue;
15220 
15221 		/*
15222 		 * Check compatibility of local address for destination's
15223 		 * default label if we're on a labeled system.	Incompatible
15224 		 * addresses can't be used at all.
15225 		 */
15226 		if (dst_rhtp != NULL) {
15227 			boolean_t incompat;
15228 
15229 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15230 			    IPV4_VERSION, B_FALSE);
15231 			if (src_rhtp == NULL)
15232 				continue;
15233 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15234 			    src_rhtp->tpc_tp.tp_doi !=
15235 			    dst_rhtp->tpc_tp.tp_doi ||
15236 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15237 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15238 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15239 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15240 			TPC_RELE(src_rhtp);
15241 			if (incompat)
15242 				continue;
15243 		}
15244 
15245 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15246 
15247 		if (ipif->ipif_lcl_addr == dst) {
15248 			type = IPIF_LOCALADDR;
15249 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15250 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15251 			    IPIF_DIFFNET_DEPRECATED;
15252 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15253 			type = samenet ? IPIF_SAMENET_ALLZONES :
15254 			    IPIF_DIFFNET_ALLZONES;
15255 		} else {
15256 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15257 		}
15258 
15259 		if (type > best_type) {
15260 			best_type = type;
15261 			best_ipif = ipif;
15262 			if (best_type == IPIF_LOCALADDR)
15263 				break; /* can't get better */
15264 		}
15265 	} while ((ipif = next_ipif) != start_ipif);
15266 
15267 	if ((ipif = best_ipif) != NULL) {
15268 		mutex_enter(&ipif->ipif_ill->ill_lock);
15269 		if (IPIF_IS_CONDEMNED(ipif)) {
15270 			mutex_exit(&ipif->ipif_ill->ill_lock);
15271 			goto retry;
15272 		}
15273 		ipif_refhold_locked(ipif);
15274 
15275 		/*
15276 		 * For IPMP, update the source ipif rotor to the next ipif,
15277 		 * provided we can look it up.  (We must not use it if it's
15278 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15279 		 * ipif_free() checked ill_src_ipif.)
15280 		 */
15281 		if (IS_IPMP(ill) && ipif != NULL) {
15282 			next_ipif = ipif->ipif_next;
15283 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15284 				ill->ill_src_ipif = next_ipif;
15285 			else
15286 				ill->ill_src_ipif = NULL;
15287 		}
15288 		mutex_exit(&ipif->ipif_ill->ill_lock);
15289 	}
15290 
15291 	rw_exit(&ipst->ips_ill_g_lock);
15292 	if (usill != NULL)
15293 		ill_refrele(usill);
15294 	if (ipmp_ill != NULL)
15295 		ill_refrele(ipmp_ill);
15296 	if (dst_rhtp != NULL)
15297 		TPC_RELE(dst_rhtp);
15298 
15299 #ifdef DEBUG
15300 	if (ipif == NULL) {
15301 		char buf1[INET6_ADDRSTRLEN];
15302 
15303 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15304 		    ill->ill_name,
15305 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15306 	} else {
15307 		char buf1[INET6_ADDRSTRLEN];
15308 		char buf2[INET6_ADDRSTRLEN];
15309 
15310 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15311 		    ipif->ipif_ill->ill_name,
15312 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15313 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15314 		    buf2, sizeof (buf2))));
15315 	}
15316 #endif /* DEBUG */
15317 	return (ipif);
15318 }
15319 
15320 /*
15321  * Pick a source address based on the destination ill and an optional setsrc
15322  * address.
15323  * The result is stored in srcp. If generation is set, then put the source
15324  * generation number there before we look for the source address (to avoid
15325  * missing changes in the set of source addresses.
15326  * If flagsp is set, then us it to pass back ipif_flags.
15327  *
15328  * If the caller wants to cache the returned source address and detect when
15329  * that might be stale, the caller should pass in a generation argument,
15330  * which the caller can later compare against ips_src_generation
15331  *
15332  * The precedence order for selecting an IPv4 source address is:
15333  *  - RTF_SETSRC on the offlink ire always wins.
15334  *  - If usrsrc is set, swap the ill to be the usesrc one.
15335  *  - If IPMP is used on the ill, select a random address from the most
15336  *    preferred ones below:
15337  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15338  * 2. Not deprecated, not ALL_ZONES
15339  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15340  * 4. Not deprecated, ALL_ZONES
15341  * 5. If onlink destination, same subnet and deprecated
15342  * 6. Deprecated.
15343  *
15344  * We have lower preference for ALL_ZONES IP addresses,
15345  * as they pose problems with unlabeled destinations.
15346  *
15347  * Note that when multiple IP addresses match e.g., #1 we pick
15348  * the first one if IPMP is not in use. With IPMP we randomize.
15349  */
15350 int
15351 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15352     ipaddr_t multicast_ifaddr,
15353     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15354     uint32_t *generation, uint64_t *flagsp)
15355 {
15356 	ipif_t *ipif;
15357 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15358 
15359 	if (flagsp != NULL)
15360 		*flagsp = 0;
15361 
15362 	/*
15363 	 * Need to grab the generation number before we check to
15364 	 * avoid a race with a change to the set of local addresses.
15365 	 * No lock needed since the thread which updates the set of local
15366 	 * addresses use ipif/ill locks and exit those (hence a store memory
15367 	 * barrier) before doing the atomic increase of ips_src_generation.
15368 	 */
15369 	if (generation != NULL) {
15370 		*generation = ipst->ips_src_generation;
15371 	}
15372 
15373 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15374 		*srcp = multicast_ifaddr;
15375 		return (0);
15376 	}
15377 
15378 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15379 	if (setsrc != INADDR_ANY) {
15380 		*srcp = setsrc;
15381 		return (0);
15382 	}
15383 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15384 	if (ipif == NULL) {
15385 		if (notready)
15386 			return (ENETDOWN);
15387 		else
15388 			return (EADDRNOTAVAIL);
15389 	}
15390 	*srcp = ipif->ipif_lcl_addr;
15391 	if (flagsp != NULL)
15392 		*flagsp = ipif->ipif_flags;
15393 	ipif_refrele(ipif);
15394 	return (0);
15395 }
15396 
15397 /* ARGSUSED */
15398 int
15399 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15400 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15401 {
15402 	/*
15403 	 * ill_phyint_reinit merged the v4 and v6 into a single
15404 	 * ipsq.  We might not have been able to complete the
15405 	 * operation in ipif_set_values, if we could not become
15406 	 * exclusive.  If so restart it here.
15407 	 */
15408 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15409 }
15410 
15411 /*
15412  * Can operate on either a module or a driver queue.
15413  * Returns an error if not a module queue.
15414  */
15415 /* ARGSUSED */
15416 int
15417 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15418     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15419 {
15420 	queue_t		*q1 = q;
15421 	char 		*cp;
15422 	char		interf_name[LIFNAMSIZ];
15423 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15424 
15425 	if (q->q_next == NULL) {
15426 		ip1dbg((
15427 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15428 		return (EINVAL);
15429 	}
15430 
15431 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15432 		return (EALREADY);
15433 
15434 	do {
15435 		q1 = q1->q_next;
15436 	} while (q1->q_next);
15437 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15438 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15439 
15440 	/*
15441 	 * Here we are not going to delay the ioack until after
15442 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15443 	 * original ioctl message before sending the requests.
15444 	 */
15445 	return (ipif_set_values(q, mp, interf_name, &ppa));
15446 }
15447 
15448 /* ARGSUSED */
15449 int
15450 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15451     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15452 {
15453 	return (ENXIO);
15454 }
15455 
15456 /*
15457  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15458  * `irep'.  Returns a pointer to the next free `irep' entry
15459  * A mirror exists in ipif_delete_bcast_ires().
15460  *
15461  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15462  * done in ire_add.
15463  */
15464 static ire_t **
15465 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15466 {
15467 	ipaddr_t addr;
15468 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15469 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15470 	ill_t *ill = ipif->ipif_ill;
15471 	zoneid_t zoneid = ipif->ipif_zoneid;
15472 
15473 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15474 
15475 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15476 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15477 
15478 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15479 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15480 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15481 
15482 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15483 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15484 
15485 	/*
15486 	 * For backward compatibility, we create net broadcast IREs based on
15487 	 * the old "IP address class system", since some old machines only
15488 	 * respond to these class derived net broadcast.  However, we must not
15489 	 * create these net broadcast IREs if the subnetmask is shorter than
15490 	 * the IP address class based derived netmask.  Otherwise, we may
15491 	 * create a net broadcast address which is the same as an IP address
15492 	 * on the subnet -- and then TCP will refuse to talk to that address.
15493 	 */
15494 	if (netmask < subnetmask) {
15495 		addr = netmask & ipif->ipif_subnet;
15496 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15497 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15498 	}
15499 
15500 	/*
15501 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15502 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15503 	 * created.  Creating these broadcast IREs will only create confusion
15504 	 * as `addr' will be the same as the IP address.
15505 	 */
15506 	if (subnetmask != 0xFFFFFFFF) {
15507 		addr = ipif->ipif_subnet;
15508 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15509 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15510 	}
15511 
15512 	return (irep);
15513 }
15514 
15515 /*
15516  * Mirror of ipif_create_bcast_ires()
15517  */
15518 static void
15519 ipif_delete_bcast_ires(ipif_t *ipif)
15520 {
15521 	ipaddr_t	addr;
15522 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15523 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15524 	ill_t		*ill = ipif->ipif_ill;
15525 	zoneid_t	zoneid = ipif->ipif_zoneid;
15526 	ire_t		*ire;
15527 
15528 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15529 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15530 
15531 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15532 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15533 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15534 
15535 	ire = ire_lookup_bcast(ill, 0, zoneid);
15536 	ASSERT(ire != NULL);
15537 	ire_delete(ire); ire_refrele(ire);
15538 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15539 	ASSERT(ire != NULL);
15540 	ire_delete(ire); ire_refrele(ire);
15541 
15542 	/*
15543 	 * For backward compatibility, we create net broadcast IREs based on
15544 	 * the old "IP address class system", since some old machines only
15545 	 * respond to these class derived net broadcast.  However, we must not
15546 	 * create these net broadcast IREs if the subnetmask is shorter than
15547 	 * the IP address class based derived netmask.  Otherwise, we may
15548 	 * create a net broadcast address which is the same as an IP address
15549 	 * on the subnet -- and then TCP will refuse to talk to that address.
15550 	 */
15551 	if (netmask < subnetmask) {
15552 		addr = netmask & ipif->ipif_subnet;
15553 		ire = ire_lookup_bcast(ill, addr, zoneid);
15554 		ASSERT(ire != NULL);
15555 		ire_delete(ire); ire_refrele(ire);
15556 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15557 		ASSERT(ire != NULL);
15558 		ire_delete(ire); ire_refrele(ire);
15559 	}
15560 
15561 	/*
15562 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15563 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15564 	 * created.  Creating these broadcast IREs will only create confusion
15565 	 * as `addr' will be the same as the IP address.
15566 	 */
15567 	if (subnetmask != 0xFFFFFFFF) {
15568 		addr = ipif->ipif_subnet;
15569 		ire = ire_lookup_bcast(ill, addr, zoneid);
15570 		ASSERT(ire != NULL);
15571 		ire_delete(ire); ire_refrele(ire);
15572 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15573 		ASSERT(ire != NULL);
15574 		ire_delete(ire); ire_refrele(ire);
15575 	}
15576 }
15577 
15578 /*
15579  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15580  * from lifr_flags and the name from lifr_name.
15581  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15582  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15583  * Returns EINPROGRESS when mp has been consumed by queueing it on
15584  * ipx_pending_mp and the ioctl will complete in ip_rput.
15585  *
15586  * Can operate on either a module or a driver queue.
15587  * Returns an error if not a module queue.
15588  */
15589 /* ARGSUSED */
15590 int
15591 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15592     ip_ioctl_cmd_t *ipip, void *if_req)
15593 {
15594 	ill_t	*ill = q->q_ptr;
15595 	phyint_t *phyi;
15596 	ip_stack_t *ipst;
15597 	struct lifreq *lifr = if_req;
15598 	uint64_t new_flags;
15599 
15600 	ASSERT(ipif != NULL);
15601 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15602 
15603 	if (q->q_next == NULL) {
15604 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15605 		return (EINVAL);
15606 	}
15607 
15608 	/*
15609 	 * If we are not writer on 'q' then this interface exists already
15610 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15611 	 * so return EALREADY.
15612 	 */
15613 	if (ill != ipif->ipif_ill)
15614 		return (EALREADY);
15615 
15616 	if (ill->ill_name[0] != '\0')
15617 		return (EALREADY);
15618 
15619 	/*
15620 	 * If there's another ill already with the requested name, ensure
15621 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15622 	 * fuse together two unrelated ills, which will cause chaos.
15623 	 */
15624 	ipst = ill->ill_ipst;
15625 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15626 	    lifr->lifr_name, NULL);
15627 	if (phyi != NULL) {
15628 		ill_t *ill_mate = phyi->phyint_illv4;
15629 
15630 		if (ill_mate == NULL)
15631 			ill_mate = phyi->phyint_illv6;
15632 		ASSERT(ill_mate != NULL);
15633 
15634 		if (ill_mate->ill_media->ip_m_mac_type !=
15635 		    ill->ill_media->ip_m_mac_type) {
15636 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15637 			    "use the same ill name on differing media\n"));
15638 			return (EINVAL);
15639 		}
15640 	}
15641 
15642 	/*
15643 	 * We start off as IFF_IPV4 in ipif_allocate and become
15644 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15645 	 * The only flags that we read from user space are IFF_IPV4,
15646 	 * IFF_IPV6, and IFF_BROADCAST.
15647 	 *
15648 	 * This ill has not been inserted into the global list.
15649 	 * So we are still single threaded and don't need any lock
15650 	 *
15651 	 * Saniy check the flags.
15652 	 */
15653 
15654 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15655 	    ((lifr->lifr_flags & IFF_IPV6) ||
15656 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15657 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15658 		    "or IPv6 i.e., no broadcast \n"));
15659 		return (EINVAL);
15660 	}
15661 
15662 	new_flags =
15663 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15664 
15665 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15666 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15667 		    "IFF_IPV4 or IFF_IPV6\n"));
15668 		return (EINVAL);
15669 	}
15670 
15671 	/*
15672 	 * We always start off as IPv4, so only need to check for IPv6.
15673 	 */
15674 	if ((new_flags & IFF_IPV6) != 0) {
15675 		ill->ill_flags |= ILLF_IPV6;
15676 		ill->ill_flags &= ~ILLF_IPV4;
15677 
15678 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15679 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15680 	}
15681 
15682 	if ((new_flags & IFF_BROADCAST) != 0)
15683 		ipif->ipif_flags |= IPIF_BROADCAST;
15684 	else
15685 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15686 
15687 	/* We started off as V4. */
15688 	if (ill->ill_flags & ILLF_IPV6) {
15689 		ill->ill_phyint->phyint_illv6 = ill;
15690 		ill->ill_phyint->phyint_illv4 = NULL;
15691 	}
15692 
15693 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15694 }
15695 
15696 /* ARGSUSED */
15697 int
15698 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15699     ip_ioctl_cmd_t *ipip, void *if_req)
15700 {
15701 	/*
15702 	 * ill_phyint_reinit merged the v4 and v6 into a single
15703 	 * ipsq.  We might not have been able to complete the
15704 	 * slifname in ipif_set_values, if we could not become
15705 	 * exclusive.  If so restart it here
15706 	 */
15707 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15708 }
15709 
15710 /*
15711  * Return a pointer to the ipif which matches the index, IP version type and
15712  * zoneid.
15713  */
15714 ipif_t *
15715 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15716     ip_stack_t *ipst)
15717 {
15718 	ill_t	*ill;
15719 	ipif_t	*ipif = NULL;
15720 
15721 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15722 	if (ill != NULL) {
15723 		mutex_enter(&ill->ill_lock);
15724 		for (ipif = ill->ill_ipif; ipif != NULL;
15725 		    ipif = ipif->ipif_next) {
15726 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15727 			    zoneid == ipif->ipif_zoneid ||
15728 			    ipif->ipif_zoneid == ALL_ZONES)) {
15729 				ipif_refhold_locked(ipif);
15730 				break;
15731 			}
15732 		}
15733 		mutex_exit(&ill->ill_lock);
15734 		ill_refrele(ill);
15735 	}
15736 	return (ipif);
15737 }
15738 
15739 /*
15740  * Change an existing physical interface's index. If the new index
15741  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15742  * Finally, we update other systems which may have a dependence on the
15743  * index value.
15744  */
15745 /* ARGSUSED */
15746 int
15747 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15748     ip_ioctl_cmd_t *ipip, void *ifreq)
15749 {
15750 	ill_t		*ill;
15751 	phyint_t	*phyi;
15752 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15753 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15754 	uint_t	old_index, index;
15755 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15756 	avl_index_t	where;
15757 
15758 	if (ipip->ipi_cmd_type == IF_CMD)
15759 		index = ifr->ifr_index;
15760 	else
15761 		index = lifr->lifr_index;
15762 
15763 	/*
15764 	 * Only allow on physical interface. Also, index zero is illegal.
15765 	 */
15766 	ill = ipif->ipif_ill;
15767 	phyi = ill->ill_phyint;
15768 	if (ipif->ipif_id != 0 || index == 0) {
15769 		return (EINVAL);
15770 	}
15771 
15772 	/* If the index is not changing, no work to do */
15773 	if (phyi->phyint_ifindex == index)
15774 		return (0);
15775 
15776 	/*
15777 	 * Use phyint_exists() to determine if the new interface index
15778 	 * is already in use. If the index is unused then we need to
15779 	 * change the phyint's position in the phyint_list_avl_by_index
15780 	 * tree. If we do not do this, subsequent lookups (using the new
15781 	 * index value) will not find the phyint.
15782 	 */
15783 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15784 	if (phyint_exists(index, ipst)) {
15785 		rw_exit(&ipst->ips_ill_g_lock);
15786 		return (EEXIST);
15787 	}
15788 
15789 	/*
15790 	 * The new index is unused. Set it in the phyint. However we must not
15791 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15792 	 * changes. The event must be bound to old ifindex value.
15793 	 */
15794 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15795 	    &index, sizeof (index));
15796 
15797 	old_index = phyi->phyint_ifindex;
15798 	phyi->phyint_ifindex = index;
15799 
15800 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15801 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15802 	    &index, &where);
15803 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15804 	    phyi, where);
15805 	rw_exit(&ipst->ips_ill_g_lock);
15806 
15807 	/* Update SCTP's ILL list */
15808 	sctp_ill_reindex(ill, old_index);
15809 
15810 	/* Send the routing sockets message */
15811 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15812 	if (ILL_OTHER(ill))
15813 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15814 
15815 	/* Perhaps ilgs should use this ill */
15816 	update_conn_ill(NULL, ill->ill_ipst);
15817 	return (0);
15818 }
15819 
15820 /* ARGSUSED */
15821 int
15822 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15823     ip_ioctl_cmd_t *ipip, void *ifreq)
15824 {
15825 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15826 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15827 
15828 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15829 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15830 	/* Get the interface index */
15831 	if (ipip->ipi_cmd_type == IF_CMD) {
15832 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15833 	} else {
15834 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15835 	}
15836 	return (0);
15837 }
15838 
15839 /* ARGSUSED */
15840 int
15841 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15842     ip_ioctl_cmd_t *ipip, void *ifreq)
15843 {
15844 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15845 
15846 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15847 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15848 	/* Get the interface zone */
15849 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15850 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15851 	return (0);
15852 }
15853 
15854 /*
15855  * Set the zoneid of an interface.
15856  */
15857 /* ARGSUSED */
15858 int
15859 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15860     ip_ioctl_cmd_t *ipip, void *ifreq)
15861 {
15862 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15863 	int err = 0;
15864 	boolean_t need_up = B_FALSE;
15865 	zone_t *zptr;
15866 	zone_status_t status;
15867 	zoneid_t zoneid;
15868 
15869 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15870 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15871 		if (!is_system_labeled())
15872 			return (ENOTSUP);
15873 		zoneid = GLOBAL_ZONEID;
15874 	}
15875 
15876 	/* cannot assign instance zero to a non-global zone */
15877 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15878 		return (ENOTSUP);
15879 
15880 	/*
15881 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15882 	 * the event of a race with the zone shutdown processing, since IP
15883 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15884 	 * interface will be cleaned up even if the zone is shut down
15885 	 * immediately after the status check. If the interface can't be brought
15886 	 * down right away, and the zone is shut down before the restart
15887 	 * function is called, we resolve the possible races by rechecking the
15888 	 * zone status in the restart function.
15889 	 */
15890 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15891 		return (EINVAL);
15892 	status = zone_status_get(zptr);
15893 	zone_rele(zptr);
15894 
15895 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15896 		return (EINVAL);
15897 
15898 	if (ipif->ipif_flags & IPIF_UP) {
15899 		/*
15900 		 * If the interface is already marked up,
15901 		 * we call ipif_down which will take care
15902 		 * of ditching any IREs that have been set
15903 		 * up based on the old interface address.
15904 		 */
15905 		err = ipif_logical_down(ipif, q, mp);
15906 		if (err == EINPROGRESS)
15907 			return (err);
15908 		(void) ipif_down_tail(ipif);
15909 		need_up = B_TRUE;
15910 	}
15911 
15912 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15913 	return (err);
15914 }
15915 
15916 static int
15917 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15918     queue_t *q, mblk_t *mp, boolean_t need_up)
15919 {
15920 	int	err = 0;
15921 	ip_stack_t	*ipst;
15922 
15923 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15924 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15925 
15926 	if (CONN_Q(q))
15927 		ipst = CONNQ_TO_IPST(q);
15928 	else
15929 		ipst = ILLQ_TO_IPST(q);
15930 
15931 	/*
15932 	 * For exclusive stacks we don't allow a different zoneid than
15933 	 * global.
15934 	 */
15935 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15936 	    zoneid != GLOBAL_ZONEID)
15937 		return (EINVAL);
15938 
15939 	/* Set the new zone id. */
15940 	ipif->ipif_zoneid = zoneid;
15941 
15942 	/* Update sctp list */
15943 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15944 
15945 	/* The default multicast interface might have changed */
15946 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15947 
15948 	if (need_up) {
15949 		/*
15950 		 * Now bring the interface back up.  If this
15951 		 * is the only IPIF for the ILL, ipif_up
15952 		 * will have to re-bind to the device, so
15953 		 * we may get back EINPROGRESS, in which
15954 		 * case, this IOCTL will get completed in
15955 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15956 		 */
15957 		err = ipif_up(ipif, q, mp);
15958 	}
15959 	return (err);
15960 }
15961 
15962 /* ARGSUSED */
15963 int
15964 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15965     ip_ioctl_cmd_t *ipip, void *if_req)
15966 {
15967 	struct lifreq *lifr = (struct lifreq *)if_req;
15968 	zoneid_t zoneid;
15969 	zone_t *zptr;
15970 	zone_status_t status;
15971 
15972 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15973 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15974 		zoneid = GLOBAL_ZONEID;
15975 
15976 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15977 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15978 
15979 	/*
15980 	 * We recheck the zone status to resolve the following race condition:
15981 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15982 	 * 2) hme0:1 is up and can't be brought down right away;
15983 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15984 	 * 3) zone "myzone" is halted; the zone status switches to
15985 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15986 	 * the interfaces to remove - hme0:1 is not returned because it's not
15987 	 * yet in "myzone", so it won't be removed;
15988 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15989 	 * status check here, we would have hme0:1 in "myzone" after it's been
15990 	 * destroyed.
15991 	 * Note that if the status check fails, we need to bring the interface
15992 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15993 	 * ipif_up_done[_v6]().
15994 	 */
15995 	status = ZONE_IS_UNINITIALIZED;
15996 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15997 		status = zone_status_get(zptr);
15998 		zone_rele(zptr);
15999 	}
16000 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16001 		if (ipif->ipif_isv6) {
16002 			(void) ipif_up_done_v6(ipif);
16003 		} else {
16004 			(void) ipif_up_done(ipif);
16005 		}
16006 		return (EINVAL);
16007 	}
16008 
16009 	(void) ipif_down_tail(ipif);
16010 
16011 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16012 	    B_TRUE));
16013 }
16014 
16015 /*
16016  * Return the number of addresses on `ill' with one or more of the values
16017  * in `set' set and all of the values in `clear' clear.
16018  */
16019 static uint_t
16020 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16021 {
16022 	ipif_t	*ipif;
16023 	uint_t	cnt = 0;
16024 
16025 	ASSERT(IAM_WRITER_ILL(ill));
16026 
16027 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16028 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16029 			cnt++;
16030 
16031 	return (cnt);
16032 }
16033 
16034 /*
16035  * Return the number of migratable addresses on `ill' that are under
16036  * application control.
16037  */
16038 uint_t
16039 ill_appaddr_cnt(const ill_t *ill)
16040 {
16041 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16042 	    IPIF_NOFAILOVER));
16043 }
16044 
16045 /*
16046  * Return the number of point-to-point addresses on `ill'.
16047  */
16048 uint_t
16049 ill_ptpaddr_cnt(const ill_t *ill)
16050 {
16051 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16052 }
16053 
16054 /* ARGSUSED */
16055 int
16056 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16057 	ip_ioctl_cmd_t *ipip, void *ifreq)
16058 {
16059 	struct lifreq	*lifr = ifreq;
16060 
16061 	ASSERT(q->q_next == NULL);
16062 	ASSERT(CONN_Q(q));
16063 
16064 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16065 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16066 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16067 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16068 
16069 	return (0);
16070 }
16071 
16072 /* Find the previous ILL in this usesrc group */
16073 static ill_t *
16074 ill_prev_usesrc(ill_t *uill)
16075 {
16076 	ill_t *ill;
16077 
16078 	for (ill = uill->ill_usesrc_grp_next;
16079 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16080 	    ill = ill->ill_usesrc_grp_next)
16081 		/* do nothing */;
16082 	return (ill);
16083 }
16084 
16085 /*
16086  * Release all members of the usesrc group. This routine is called
16087  * from ill_delete when the interface being unplumbed is the
16088  * group head.
16089  *
16090  * This silently clears the usesrc that ifconfig setup.
16091  * An alternative would be to keep that ifindex, and drop packets on the floor
16092  * since no source address can be selected.
16093  * Even if we keep the current semantics, don't need a lock and a linked list.
16094  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16095  * the one that is being removed. Issue is how we return the usesrc users
16096  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16097  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16098  * ill walk, but the walker would need to insert in the ioctl response.
16099  */
16100 static void
16101 ill_disband_usesrc_group(ill_t *uill)
16102 {
16103 	ill_t *next_ill, *tmp_ill;
16104 	ip_stack_t	*ipst = uill->ill_ipst;
16105 
16106 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16107 	next_ill = uill->ill_usesrc_grp_next;
16108 
16109 	do {
16110 		ASSERT(next_ill != NULL);
16111 		tmp_ill = next_ill->ill_usesrc_grp_next;
16112 		ASSERT(tmp_ill != NULL);
16113 		next_ill->ill_usesrc_grp_next = NULL;
16114 		next_ill->ill_usesrc_ifindex = 0;
16115 		next_ill = tmp_ill;
16116 	} while (next_ill->ill_usesrc_ifindex != 0);
16117 	uill->ill_usesrc_grp_next = NULL;
16118 }
16119 
16120 /*
16121  * Remove the client usesrc ILL from the list and relink to a new list
16122  */
16123 int
16124 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16125 {
16126 	ill_t *ill, *tmp_ill;
16127 	ip_stack_t	*ipst = ucill->ill_ipst;
16128 
16129 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16130 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16131 
16132 	/*
16133 	 * Check if the usesrc client ILL passed in is not already
16134 	 * in use as a usesrc ILL i.e one whose source address is
16135 	 * in use OR a usesrc ILL is not already in use as a usesrc
16136 	 * client ILL
16137 	 */
16138 	if ((ucill->ill_usesrc_ifindex == 0) ||
16139 	    (uill->ill_usesrc_ifindex != 0)) {
16140 		return (-1);
16141 	}
16142 
16143 	ill = ill_prev_usesrc(ucill);
16144 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16145 
16146 	/* Remove from the current list */
16147 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16148 		/* Only two elements in the list */
16149 		ASSERT(ill->ill_usesrc_ifindex == 0);
16150 		ill->ill_usesrc_grp_next = NULL;
16151 	} else {
16152 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16153 	}
16154 
16155 	if (ifindex == 0) {
16156 		ucill->ill_usesrc_ifindex = 0;
16157 		ucill->ill_usesrc_grp_next = NULL;
16158 		return (0);
16159 	}
16160 
16161 	ucill->ill_usesrc_ifindex = ifindex;
16162 	tmp_ill = uill->ill_usesrc_grp_next;
16163 	uill->ill_usesrc_grp_next = ucill;
16164 	ucill->ill_usesrc_grp_next =
16165 	    (tmp_ill != NULL) ? tmp_ill : uill;
16166 	return (0);
16167 }
16168 
16169 /*
16170  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16171  * ip.c for locking details.
16172  */
16173 /* ARGSUSED */
16174 int
16175 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16176     ip_ioctl_cmd_t *ipip, void *ifreq)
16177 {
16178 	struct lifreq *lifr = (struct lifreq *)ifreq;
16179 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16180 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16181 	int err = 0, ret;
16182 	uint_t ifindex;
16183 	ipsq_t *ipsq = NULL;
16184 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16185 
16186 	ASSERT(IAM_WRITER_IPIF(ipif));
16187 	ASSERT(q->q_next == NULL);
16188 	ASSERT(CONN_Q(q));
16189 
16190 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16191 
16192 	ifindex = lifr->lifr_index;
16193 	if (ifindex == 0) {
16194 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16195 			/* non usesrc group interface, nothing to reset */
16196 			return (0);
16197 		}
16198 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16199 		/* valid reset request */
16200 		reset_flg = B_TRUE;
16201 	}
16202 
16203 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16204 	if (usesrc_ill == NULL)
16205 		return (ENXIO);
16206 	if (usesrc_ill == ipif->ipif_ill) {
16207 		ill_refrele(usesrc_ill);
16208 		return (EINVAL);
16209 	}
16210 
16211 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16212 	    NEW_OP, B_TRUE);
16213 	if (ipsq == NULL) {
16214 		err = EINPROGRESS;
16215 		/* Operation enqueued on the ipsq of the usesrc ILL */
16216 		goto done;
16217 	}
16218 
16219 	/* USESRC isn't currently supported with IPMP */
16220 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16221 		err = ENOTSUP;
16222 		goto done;
16223 	}
16224 
16225 	/*
16226 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16227 	 * used by IPMP underlying interfaces, but someone might think it's
16228 	 * more general and try to use it independently with VNI.)
16229 	 */
16230 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16231 		err = ENOTSUP;
16232 		goto done;
16233 	}
16234 
16235 	/*
16236 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16237 	 * already a client then return EINVAL
16238 	 */
16239 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16240 		err = EINVAL;
16241 		goto done;
16242 	}
16243 
16244 	/*
16245 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16246 	 * be then this is a duplicate operation.
16247 	 */
16248 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16249 		err = 0;
16250 		goto done;
16251 	}
16252 
16253 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16254 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16255 	    usesrc_ill->ill_isv6));
16256 
16257 	/*
16258 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16259 	 * and the ill_usesrc_ifindex fields
16260 	 */
16261 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16262 
16263 	if (reset_flg) {
16264 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16265 		if (ret != 0) {
16266 			err = EINVAL;
16267 		}
16268 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16269 		goto done;
16270 	}
16271 
16272 	/*
16273 	 * Four possibilities to consider:
16274 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16275 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16276 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16277 	 * 4. Both are part of their respective usesrc groups
16278 	 */
16279 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16280 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16281 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16282 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16283 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16284 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16285 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16286 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16287 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16288 		/* Insert at head of list */
16289 		usesrc_cli_ill->ill_usesrc_grp_next =
16290 		    usesrc_ill->ill_usesrc_grp_next;
16291 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16292 	} else {
16293 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16294 		    ifindex);
16295 		if (ret != 0)
16296 			err = EINVAL;
16297 	}
16298 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16299 
16300 done:
16301 	if (ipsq != NULL)
16302 		ipsq_exit(ipsq);
16303 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16304 	ill_refrele(usesrc_ill);
16305 
16306 	/* Let conn_ixa caching know that source address selection changed */
16307 	ip_update_source_selection(ipst);
16308 
16309 	return (err);
16310 }
16311 
16312 /* ARGSUSED */
16313 int
16314 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16315     ip_ioctl_cmd_t *ipip, void *if_req)
16316 {
16317 	struct lifreq	*lifr = (struct lifreq *)if_req;
16318 	ill_t		*ill = ipif->ipif_ill;
16319 
16320 	/*
16321 	 * Need a lock since IFF_UP can be set even when there are
16322 	 * references to the ipif.
16323 	 */
16324 	mutex_enter(&ill->ill_lock);
16325 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16326 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16327 	else
16328 		lifr->lifr_dadstate = DAD_DONE;
16329 	mutex_exit(&ill->ill_lock);
16330 	return (0);
16331 }
16332 
16333 /*
16334  * comparison function used by avl.
16335  */
16336 static int
16337 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16338 {
16339 
16340 	uint_t index;
16341 
16342 	ASSERT(phyip != NULL && index_ptr != NULL);
16343 
16344 	index = *((uint_t *)index_ptr);
16345 	/*
16346 	 * let the phyint with the lowest index be on top.
16347 	 */
16348 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16349 		return (1);
16350 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16351 		return (-1);
16352 	return (0);
16353 }
16354 
16355 /*
16356  * comparison function used by avl.
16357  */
16358 static int
16359 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16360 {
16361 	ill_t *ill;
16362 	int res = 0;
16363 
16364 	ASSERT(phyip != NULL && name_ptr != NULL);
16365 
16366 	if (((phyint_t *)phyip)->phyint_illv4)
16367 		ill = ((phyint_t *)phyip)->phyint_illv4;
16368 	else
16369 		ill = ((phyint_t *)phyip)->phyint_illv6;
16370 	ASSERT(ill != NULL);
16371 
16372 	res = strcmp(ill->ill_name, (char *)name_ptr);
16373 	if (res > 0)
16374 		return (1);
16375 	else if (res < 0)
16376 		return (-1);
16377 	return (0);
16378 }
16379 
16380 /*
16381  * This function is called on the unplumb path via ill_glist_delete() when
16382  * there are no ills left on the phyint and thus the phyint can be freed.
16383  */
16384 static void
16385 phyint_free(phyint_t *phyi)
16386 {
16387 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16388 
16389 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16390 
16391 	/*
16392 	 * If this phyint was an IPMP meta-interface, blow away the group.
16393 	 * This is safe to do because all of the illgrps have already been
16394 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16395 	 * If we're cleaning up as a result of failed initialization,
16396 	 * phyint_grp may be NULL.
16397 	 */
16398 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16399 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16400 		ipmp_grp_destroy(phyi->phyint_grp);
16401 		phyi->phyint_grp = NULL;
16402 		rw_exit(&ipst->ips_ipmp_lock);
16403 	}
16404 
16405 	/*
16406 	 * If this interface was under IPMP, take it out of the group.
16407 	 */
16408 	if (phyi->phyint_grp != NULL)
16409 		ipmp_phyint_leave_grp(phyi);
16410 
16411 	/*
16412 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16413 	 * will be freed in ipsq_exit().
16414 	 */
16415 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16416 	phyi->phyint_name[0] = '\0';
16417 
16418 	mi_free(phyi);
16419 }
16420 
16421 /*
16422  * Attach the ill to the phyint structure which can be shared by both
16423  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16424  * function is called from ipif_set_values and ill_lookup_on_name (for
16425  * loopback) where we know the name of the ill. We lookup the ill and if
16426  * there is one present already with the name use that phyint. Otherwise
16427  * reuse the one allocated by ill_init.
16428  */
16429 static void
16430 ill_phyint_reinit(ill_t *ill)
16431 {
16432 	boolean_t isv6 = ill->ill_isv6;
16433 	phyint_t *phyi_old;
16434 	phyint_t *phyi;
16435 	avl_index_t where = 0;
16436 	ill_t	*ill_other = NULL;
16437 	ip_stack_t	*ipst = ill->ill_ipst;
16438 
16439 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16440 
16441 	phyi_old = ill->ill_phyint;
16442 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16443 	    phyi_old->phyint_illv6 == NULL));
16444 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16445 	    phyi_old->phyint_illv4 == NULL));
16446 	ASSERT(phyi_old->phyint_ifindex == 0);
16447 
16448 	/*
16449 	 * Now that our ill has a name, set it in the phyint.
16450 	 */
16451 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16452 
16453 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16454 	    ill->ill_name, &where);
16455 
16456 	/*
16457 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16458 	 *    the global list of ills. So no other thread could have located
16459 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16460 	 * 2. Now locate the other protocol instance of this ill.
16461 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16462 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16463 	 *    of neither ill can change.
16464 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16465 	 *    other ill.
16466 	 * 5. Release all locks.
16467 	 */
16468 
16469 	/*
16470 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16471 	 * we are initializing IPv4.
16472 	 */
16473 	if (phyi != NULL) {
16474 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16475 		ASSERT(ill_other->ill_phyint != NULL);
16476 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16477 		    (!isv6 && ill_other->ill_isv6));
16478 		GRAB_ILL_LOCKS(ill, ill_other);
16479 		/*
16480 		 * We are potentially throwing away phyint_flags which
16481 		 * could be different from the one that we obtain from
16482 		 * ill_other->ill_phyint. But it is okay as we are assuming
16483 		 * that the state maintained within IP is correct.
16484 		 */
16485 		mutex_enter(&phyi->phyint_lock);
16486 		if (isv6) {
16487 			ASSERT(phyi->phyint_illv6 == NULL);
16488 			phyi->phyint_illv6 = ill;
16489 		} else {
16490 			ASSERT(phyi->phyint_illv4 == NULL);
16491 			phyi->phyint_illv4 = ill;
16492 		}
16493 
16494 		/*
16495 		 * Delete the old phyint and make its ipsq eligible
16496 		 * to be freed in ipsq_exit().
16497 		 */
16498 		phyi_old->phyint_illv4 = NULL;
16499 		phyi_old->phyint_illv6 = NULL;
16500 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16501 		phyi_old->phyint_name[0] = '\0';
16502 		mi_free(phyi_old);
16503 	} else {
16504 		mutex_enter(&ill->ill_lock);
16505 		/*
16506 		 * We don't need to acquire any lock, since
16507 		 * the ill is not yet visible globally  and we
16508 		 * have not yet released the ill_g_lock.
16509 		 */
16510 		phyi = phyi_old;
16511 		mutex_enter(&phyi->phyint_lock);
16512 		/* XXX We need a recovery strategy here. */
16513 		if (!phyint_assign_ifindex(phyi, ipst))
16514 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16515 
16516 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16517 		    (void *)phyi, where);
16518 
16519 		(void) avl_find(&ipst->ips_phyint_g_list->
16520 		    phyint_list_avl_by_index,
16521 		    &phyi->phyint_ifindex, &where);
16522 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16523 		    (void *)phyi, where);
16524 	}
16525 
16526 	/*
16527 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16528 	 * pending mp is not affected because that is per ill basis.
16529 	 */
16530 	ill->ill_phyint = phyi;
16531 
16532 	/*
16533 	 * Now that the phyint's ifindex has been assigned, complete the
16534 	 * remaining
16535 	 */
16536 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16537 	if (ill->ill_isv6) {
16538 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16539 		    ill->ill_phyint->phyint_ifindex;
16540 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16541 	} else {
16542 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16543 	}
16544 
16545 	/*
16546 	 * Generate an event within the hooks framework to indicate that
16547 	 * a new interface has just been added to IP.  For this event to
16548 	 * be generated, the network interface must, at least, have an
16549 	 * ifindex assigned to it.  (We don't generate the event for
16550 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16551 	 *
16552 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16553 	 * that the ordering of delivered events to listeners matches the
16554 	 * order of them in the kernel.
16555 	 */
16556 	if (!IS_LOOPBACK(ill)) {
16557 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16558 		    ill->ill_name_length);
16559 	}
16560 	RELEASE_ILL_LOCKS(ill, ill_other);
16561 	mutex_exit(&phyi->phyint_lock);
16562 }
16563 
16564 /*
16565  * Notify any downstream modules of the name of this interface.
16566  * An M_IOCTL is used even though we don't expect a successful reply.
16567  * Any reply message from the driver (presumably an M_IOCNAK) will
16568  * eventually get discarded somewhere upstream.  The message format is
16569  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16570  * to IP.
16571  */
16572 static void
16573 ip_ifname_notify(ill_t *ill, queue_t *q)
16574 {
16575 	mblk_t *mp1, *mp2;
16576 	struct iocblk *iocp;
16577 	struct lifreq *lifr;
16578 
16579 	mp1 = mkiocb(SIOCSLIFNAME);
16580 	if (mp1 == NULL)
16581 		return;
16582 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16583 	if (mp2 == NULL) {
16584 		freeb(mp1);
16585 		return;
16586 	}
16587 
16588 	mp1->b_cont = mp2;
16589 	iocp = (struct iocblk *)mp1->b_rptr;
16590 	iocp->ioc_count = sizeof (struct lifreq);
16591 
16592 	lifr = (struct lifreq *)mp2->b_rptr;
16593 	mp2->b_wptr += sizeof (struct lifreq);
16594 	bzero(lifr, sizeof (struct lifreq));
16595 
16596 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16597 	lifr->lifr_ppa = ill->ill_ppa;
16598 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16599 
16600 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16601 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16602 	putnext(q, mp1);
16603 }
16604 
16605 static int
16606 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16607 {
16608 	int		err;
16609 	ip_stack_t	*ipst = ill->ill_ipst;
16610 	phyint_t	*phyi = ill->ill_phyint;
16611 
16612 	/*
16613 	 * Now that ill_name is set, the configuration for the IPMP
16614 	 * meta-interface can be performed.
16615 	 */
16616 	if (IS_IPMP(ill)) {
16617 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16618 		/*
16619 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16620 		 * meta-interface and we need to create the IPMP group.
16621 		 */
16622 		if (phyi->phyint_grp == NULL) {
16623 			/*
16624 			 * If someone has renamed another IPMP group to have
16625 			 * the same name as our interface, bail.
16626 			 */
16627 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16628 				rw_exit(&ipst->ips_ipmp_lock);
16629 				return (EEXIST);
16630 			}
16631 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16632 			if (phyi->phyint_grp == NULL) {
16633 				rw_exit(&ipst->ips_ipmp_lock);
16634 				return (ENOMEM);
16635 			}
16636 		}
16637 		rw_exit(&ipst->ips_ipmp_lock);
16638 	}
16639 
16640 	/* Tell downstream modules where they are. */
16641 	ip_ifname_notify(ill, q);
16642 
16643 	/*
16644 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16645 	 * Error cases are ENOMEM ...
16646 	 */
16647 	err = ill_dl_phys(ill, ipif, mp, q);
16648 
16649 	if (ill->ill_isv6) {
16650 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16651 		if (ipst->ips_mld_slowtimeout_id == 0) {
16652 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16653 			    (void *)ipst,
16654 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16655 		}
16656 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16657 	} else {
16658 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16659 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16660 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16661 			    (void *)ipst,
16662 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16663 		}
16664 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16665 	}
16666 
16667 	return (err);
16668 }
16669 
16670 /*
16671  * Common routine for ppa and ifname setting. Should be called exclusive.
16672  *
16673  * Returns EINPROGRESS when mp has been consumed by queueing it on
16674  * ipx_pending_mp and the ioctl will complete in ip_rput.
16675  *
16676  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16677  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16678  * For SLIFNAME, we pass these values back to the userland.
16679  */
16680 static int
16681 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16682 {
16683 	ill_t	*ill;
16684 	ipif_t	*ipif;
16685 	ipsq_t	*ipsq;
16686 	char	*ppa_ptr;
16687 	char	*old_ptr;
16688 	char	old_char;
16689 	int	error;
16690 	ip_stack_t	*ipst;
16691 
16692 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16693 	ASSERT(q->q_next != NULL);
16694 	ASSERT(interf_name != NULL);
16695 
16696 	ill = (ill_t *)q->q_ptr;
16697 	ipst = ill->ill_ipst;
16698 
16699 	ASSERT(ill->ill_ipst != NULL);
16700 	ASSERT(ill->ill_name[0] == '\0');
16701 	ASSERT(IAM_WRITER_ILL(ill));
16702 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16703 	ASSERT(ill->ill_ppa == UINT_MAX);
16704 
16705 	ill->ill_defend_start = ill->ill_defend_count = 0;
16706 	/* The ppa is sent down by ifconfig or is chosen */
16707 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16708 		return (EINVAL);
16709 	}
16710 
16711 	/*
16712 	 * make sure ppa passed in is same as ppa in the name.
16713 	 * This check is not made when ppa == UINT_MAX in that case ppa
16714 	 * in the name could be anything. System will choose a ppa and
16715 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16716 	 */
16717 	if (*new_ppa_ptr != UINT_MAX) {
16718 		/* stoi changes the pointer */
16719 		old_ptr = ppa_ptr;
16720 		/*
16721 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16722 		 * (they don't have an externally visible ppa).  We assign one
16723 		 * here so that we can manage the interface.  Note that in
16724 		 * the past this value was always 0 for DLPI 1 drivers.
16725 		 */
16726 		if (*new_ppa_ptr == 0)
16727 			*new_ppa_ptr = stoi(&old_ptr);
16728 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16729 			return (EINVAL);
16730 	}
16731 	/*
16732 	 * terminate string before ppa
16733 	 * save char at that location.
16734 	 */
16735 	old_char = ppa_ptr[0];
16736 	ppa_ptr[0] = '\0';
16737 
16738 	ill->ill_ppa = *new_ppa_ptr;
16739 	/*
16740 	 * Finish as much work now as possible before calling ill_glist_insert
16741 	 * which makes the ill globally visible and also merges it with the
16742 	 * other protocol instance of this phyint. The remaining work is
16743 	 * done after entering the ipsq which may happen sometime later.
16744 	 */
16745 	ipif = ill->ill_ipif;
16746 
16747 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16748 	ipif_assign_seqid(ipif);
16749 
16750 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16751 		ill->ill_flags |= ILLF_IPV4;
16752 
16753 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16754 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16755 
16756 	if (ill->ill_flags & ILLF_IPV6) {
16757 
16758 		ill->ill_isv6 = B_TRUE;
16759 		ill_set_inputfn(ill);
16760 		if (ill->ill_rq != NULL) {
16761 			ill->ill_rq->q_qinfo = &iprinitv6;
16762 		}
16763 
16764 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16765 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16766 		ipif->ipif_v6subnet = ipv6_all_zeros;
16767 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16768 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16769 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16770 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16771 		/*
16772 		 * point-to-point or Non-mulicast capable
16773 		 * interfaces won't do NUD unless explicitly
16774 		 * configured to do so.
16775 		 */
16776 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16777 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16778 			ill->ill_flags |= ILLF_NONUD;
16779 		}
16780 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16781 		if (ill->ill_flags & ILLF_NOARP) {
16782 			/*
16783 			 * Note: xresolv interfaces will eventually need
16784 			 * NOARP set here as well, but that will require
16785 			 * those external resolvers to have some
16786 			 * knowledge of that flag and act appropriately.
16787 			 * Not to be changed at present.
16788 			 */
16789 			ill->ill_flags &= ~ILLF_NOARP;
16790 		}
16791 		/*
16792 		 * Set the ILLF_ROUTER flag according to the global
16793 		 * IPv6 forwarding policy.
16794 		 */
16795 		if (ipst->ips_ipv6_forwarding != 0)
16796 			ill->ill_flags |= ILLF_ROUTER;
16797 	} else if (ill->ill_flags & ILLF_IPV4) {
16798 		ill->ill_isv6 = B_FALSE;
16799 		ill_set_inputfn(ill);
16800 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16801 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16802 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16803 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16804 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16805 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16806 		/*
16807 		 * Set the ILLF_ROUTER flag according to the global
16808 		 * IPv4 forwarding policy.
16809 		 */
16810 		if (ipst->ips_ip_forwarding != 0)
16811 			ill->ill_flags |= ILLF_ROUTER;
16812 	}
16813 
16814 	ASSERT(ill->ill_phyint != NULL);
16815 
16816 	/*
16817 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16818 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16819 	 */
16820 	if (!ill_allocate_mibs(ill))
16821 		return (ENOMEM);
16822 
16823 	/*
16824 	 * Pick a default sap until we get the DL_INFO_ACK back from
16825 	 * the driver.
16826 	 */
16827 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16828 	    ill->ill_media->ip_m_ipv4sap;
16829 
16830 	ill->ill_ifname_pending = 1;
16831 	ill->ill_ifname_pending_err = 0;
16832 
16833 	/*
16834 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16835 	 * that were joined while this ill was not bound to the DLPI link need
16836 	 * to be recovered by ill_recover_multicast().
16837 	 */
16838 	ill->ill_need_recover_multicast = 1;
16839 
16840 	ill_refhold(ill);
16841 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16842 	if ((error = ill_glist_insert(ill, interf_name,
16843 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16844 		ill->ill_ppa = UINT_MAX;
16845 		ill->ill_name[0] = '\0';
16846 		/*
16847 		 * undo null termination done above.
16848 		 */
16849 		ppa_ptr[0] = old_char;
16850 		rw_exit(&ipst->ips_ill_g_lock);
16851 		ill_refrele(ill);
16852 		return (error);
16853 	}
16854 
16855 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16856 
16857 	/*
16858 	 * When we return the buffer pointed to by interf_name should contain
16859 	 * the same name as in ill_name.
16860 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16861 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16862 	 * so copy full name and update the ppa ptr.
16863 	 * When ppa passed in != UINT_MAX all values are correct just undo
16864 	 * null termination, this saves a bcopy.
16865 	 */
16866 	if (*new_ppa_ptr == UINT_MAX) {
16867 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16868 		*new_ppa_ptr = ill->ill_ppa;
16869 	} else {
16870 		/*
16871 		 * undo null termination done above.
16872 		 */
16873 		ppa_ptr[0] = old_char;
16874 	}
16875 
16876 	/* Let SCTP know about this ILL */
16877 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16878 
16879 	/*
16880 	 * ill_glist_insert has made the ill visible globally, and
16881 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16882 	 * we need to hold the ips_ill_g_lock across the call to enter the
16883 	 * ipsq to enforce atomicity and prevent reordering. In the event
16884 	 * the ipsq has changed, and if the new ipsq is currently busy,
16885 	 * we need to make sure that this half-completed ioctl is ahead of
16886 	 * any subsequent ioctl. We achieve this by not dropping the
16887 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16888 	 * ensuring that new ioctls can't start.
16889 	 */
16890 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16891 	    B_TRUE);
16892 
16893 	rw_exit(&ipst->ips_ill_g_lock);
16894 	ill_refrele(ill);
16895 	if (ipsq == NULL)
16896 		return (EINPROGRESS);
16897 
16898 	/*
16899 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16900 	 */
16901 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16902 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16903 	else
16904 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16905 
16906 	error = ipif_set_values_tail(ill, ipif, mp, q);
16907 	ipsq_exit(ipsq);
16908 	if (error != 0 && error != EINPROGRESS) {
16909 		/*
16910 		 * restore previous values
16911 		 */
16912 		ill->ill_isv6 = B_FALSE;
16913 		ill_set_inputfn(ill);
16914 	}
16915 	return (error);
16916 }
16917 
16918 void
16919 ipif_init(ip_stack_t *ipst)
16920 {
16921 	int i;
16922 
16923 	for (i = 0; i < MAX_G_HEADS; i++) {
16924 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16925 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16926 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16927 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16928 	}
16929 
16930 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16931 	    ill_phyint_compare_index,
16932 	    sizeof (phyint_t),
16933 	    offsetof(struct phyint, phyint_avl_by_index));
16934 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16935 	    ill_phyint_compare_name,
16936 	    sizeof (phyint_t),
16937 	    offsetof(struct phyint, phyint_avl_by_name));
16938 }
16939 
16940 /*
16941  * Save enough information so that we can recreate the IRE if
16942  * the interface goes down and then up.
16943  */
16944 void
16945 ill_save_ire(ill_t *ill, ire_t *ire)
16946 {
16947 	mblk_t	*save_mp;
16948 
16949 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16950 	if (save_mp != NULL) {
16951 		ifrt_t	*ifrt;
16952 
16953 		save_mp->b_wptr += sizeof (ifrt_t);
16954 		ifrt = (ifrt_t *)save_mp->b_rptr;
16955 		bzero(ifrt, sizeof (ifrt_t));
16956 		ifrt->ifrt_type = ire->ire_type;
16957 		if (ire->ire_ipversion == IPV4_VERSION) {
16958 			ASSERT(!ill->ill_isv6);
16959 			ifrt->ifrt_addr = ire->ire_addr;
16960 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16961 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16962 			ifrt->ifrt_mask = ire->ire_mask;
16963 		} else {
16964 			ASSERT(ill->ill_isv6);
16965 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16966 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16967 			mutex_enter(&ire->ire_lock);
16968 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16969 			mutex_exit(&ire->ire_lock);
16970 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16971 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16972 		}
16973 		ifrt->ifrt_flags = ire->ire_flags;
16974 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16975 		mutex_enter(&ill->ill_saved_ire_lock);
16976 		save_mp->b_cont = ill->ill_saved_ire_mp;
16977 		ill->ill_saved_ire_mp = save_mp;
16978 		ill->ill_saved_ire_cnt++;
16979 		mutex_exit(&ill->ill_saved_ire_lock);
16980 	}
16981 }
16982 
16983 /*
16984  * Remove one entry from ill_saved_ire_mp.
16985  */
16986 void
16987 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16988 {
16989 	mblk_t	**mpp;
16990 	mblk_t	*mp;
16991 	ifrt_t	*ifrt;
16992 
16993 	/* Remove from ill_saved_ire_mp list if it is there */
16994 	mutex_enter(&ill->ill_saved_ire_lock);
16995 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16996 	    mpp = &(*mpp)->b_cont) {
16997 		in6_addr_t	gw_addr_v6;
16998 
16999 		/*
17000 		 * On a given ill, the tuple of address, gateway, mask,
17001 		 * ire_type, and zoneid is unique for each saved IRE.
17002 		 */
17003 		mp = *mpp;
17004 		ifrt = (ifrt_t *)mp->b_rptr;
17005 		/* ire_gateway_addr_v6 can change - need lock */
17006 		mutex_enter(&ire->ire_lock);
17007 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17008 		mutex_exit(&ire->ire_lock);
17009 
17010 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17011 		    ifrt->ifrt_type != ire->ire_type)
17012 			continue;
17013 
17014 		if (ill->ill_isv6 ?
17015 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17016 		    &ire->ire_addr_v6) &&
17017 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17018 		    &gw_addr_v6) &&
17019 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17020 		    &ire->ire_mask_v6)) :
17021 		    (ifrt->ifrt_addr == ire->ire_addr &&
17022 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17023 		    ifrt->ifrt_mask == ire->ire_mask)) {
17024 			*mpp = mp->b_cont;
17025 			ill->ill_saved_ire_cnt--;
17026 			freeb(mp);
17027 			break;
17028 		}
17029 	}
17030 	mutex_exit(&ill->ill_saved_ire_lock);
17031 }
17032 
17033 /*
17034  * IP multirouting broadcast routes handling
17035  * Append CGTP broadcast IREs to regular ones created
17036  * at ifconfig time.
17037  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17038  * the destination and the gateway are broadcast addresses.
17039  * The caller has verified that the destination is an IRE_BROADCAST and that
17040  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17041  * we create a MULTIRT IRE_BROADCAST.
17042  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17043  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17044  */
17045 static void
17046 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17047 {
17048 	ire_t *ire_prim;
17049 
17050 	ASSERT(ire != NULL);
17051 
17052 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17053 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17054 	    NULL);
17055 	if (ire_prim != NULL) {
17056 		/*
17057 		 * We are in the special case of broadcasts for
17058 		 * CGTP. We add an IRE_BROADCAST that holds
17059 		 * the RTF_MULTIRT flag, the destination
17060 		 * address and the low level
17061 		 * info of ire_prim. In other words, CGTP
17062 		 * broadcast is added to the redundant ipif.
17063 		 */
17064 		ill_t *ill_prim;
17065 		ire_t  *bcast_ire;
17066 
17067 		ill_prim = ire_prim->ire_ill;
17068 
17069 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17070 		    (void *)ire_prim, (void *)ill_prim));
17071 
17072 		bcast_ire = ire_create(
17073 		    (uchar_t *)&ire->ire_addr,
17074 		    (uchar_t *)&ip_g_all_ones,
17075 		    (uchar_t *)&ire->ire_gateway_addr,
17076 		    IRE_BROADCAST,
17077 		    ill_prim,
17078 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17079 		    ire->ire_flags | RTF_KERNEL,
17080 		    NULL,
17081 		    ipst);
17082 
17083 		/*
17084 		 * Here we assume that ire_add does head insertion so that
17085 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17086 		 */
17087 		if (bcast_ire != NULL) {
17088 			if (ire->ire_flags & RTF_SETSRC) {
17089 				bcast_ire->ire_setsrc_addr =
17090 				    ire->ire_setsrc_addr;
17091 			}
17092 			bcast_ire = ire_add(bcast_ire);
17093 			if (bcast_ire != NULL) {
17094 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17095 				    "added bcast_ire %p\n",
17096 				    (void *)bcast_ire));
17097 
17098 				ill_save_ire(ill_prim, bcast_ire);
17099 				ire_refrele(bcast_ire);
17100 			}
17101 		}
17102 		ire_refrele(ire_prim);
17103 	}
17104 }
17105 
17106 /*
17107  * IP multirouting broadcast routes handling
17108  * Remove the broadcast ire.
17109  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17110  * the destination and the gateway are broadcast addresses.
17111  * The caller has only verified that RTF_MULTIRT was set. We check
17112  * that the destination is broadcast and that the gateway is a broadcast
17113  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17114  */
17115 static void
17116 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17117 {
17118 	ASSERT(ire != NULL);
17119 
17120 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17121 		ire_t *ire_prim;
17122 
17123 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17124 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17125 		    ipst, NULL);
17126 		if (ire_prim != NULL) {
17127 			ill_t *ill_prim;
17128 			ire_t  *bcast_ire;
17129 
17130 			ill_prim = ire_prim->ire_ill;
17131 
17132 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17133 			    "ire_prim %p, ill_prim %p\n",
17134 			    (void *)ire_prim, (void *)ill_prim));
17135 
17136 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17137 			    ire->ire_gateway_addr, IRE_BROADCAST,
17138 			    ill_prim, ALL_ZONES, NULL,
17139 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17140 			    MATCH_IRE_MASK, 0, ipst, NULL);
17141 
17142 			if (bcast_ire != NULL) {
17143 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17144 				    "looked up bcast_ire %p\n",
17145 				    (void *)bcast_ire));
17146 				ill_remove_saved_ire(bcast_ire->ire_ill,
17147 				    bcast_ire);
17148 				ire_delete(bcast_ire);
17149 				ire_refrele(bcast_ire);
17150 			}
17151 			ire_refrele(ire_prim);
17152 		}
17153 	}
17154 }
17155 
17156 /*
17157  * Derive an interface id from the link layer address.
17158  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17159  */
17160 static void
17161 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17162 {
17163 	char		*addr;
17164 
17165 	/*
17166 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17167 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17168 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17169 	 * interface ID on IPv6 interfaces above links that actually have real
17170 	 * Ethernet addresses.
17171 	 */
17172 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17173 		/* Form EUI-64 like address */
17174 		addr = (char *)&v6addr->s6_addr32[2];
17175 		bcopy(ill->ill_phys_addr, addr, 3);
17176 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17177 		addr[3] = (char)0xff;
17178 		addr[4] = (char)0xfe;
17179 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17180 	}
17181 }
17182 
17183 /* ARGSUSED */
17184 static void
17185 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17186 {
17187 }
17188 
17189 typedef struct ipmp_ifcookie {
17190 	uint32_t	ic_hostid;
17191 	char		ic_ifname[LIFNAMSIZ];
17192 	char		ic_zonename[ZONENAME_MAX];
17193 } ipmp_ifcookie_t;
17194 
17195 /*
17196  * Construct a pseudo-random interface ID for the IPMP interface that's both
17197  * predictable and (almost) guaranteed to be unique.
17198  */
17199 static void
17200 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17201 {
17202 	zone_t		*zp;
17203 	uint8_t		*addr;
17204 	uchar_t		hash[16];
17205 	ulong_t 	hostid;
17206 	MD5_CTX		ctx;
17207 	ipmp_ifcookie_t	ic = { 0 };
17208 
17209 	ASSERT(IS_IPMP(ill));
17210 
17211 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17212 	ic.ic_hostid = htonl((uint32_t)hostid);
17213 
17214 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17215 
17216 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17217 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17218 		zone_rele(zp);
17219 	}
17220 
17221 	MD5Init(&ctx);
17222 	MD5Update(&ctx, &ic, sizeof (ic));
17223 	MD5Final(hash, &ctx);
17224 
17225 	/*
17226 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17227 	 */
17228 	addr = &v6addr->s6_addr8[8];
17229 	bcopy(hash + 8, addr, sizeof (uint64_t));
17230 	addr[0] &= ~0x2;				/* set local bit */
17231 }
17232 
17233 /*
17234  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17235  */
17236 static void
17237 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17238 {
17239 	phyint_t *phyi = ill->ill_phyint;
17240 
17241 	/*
17242 	 * Check PHYI_MULTI_BCAST and length of physical
17243 	 * address to determine if we use the mapping or the
17244 	 * broadcast address.
17245 	 */
17246 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17247 	    ill->ill_phys_addr_length != ETHERADDRL) {
17248 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17249 		return;
17250 	}
17251 	m_physaddr[0] = 0x33;
17252 	m_physaddr[1] = 0x33;
17253 	m_physaddr[2] = m_ip6addr[12];
17254 	m_physaddr[3] = m_ip6addr[13];
17255 	m_physaddr[4] = m_ip6addr[14];
17256 	m_physaddr[5] = m_ip6addr[15];
17257 }
17258 
17259 /*
17260  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17261  */
17262 static void
17263 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17264 {
17265 	phyint_t *phyi = ill->ill_phyint;
17266 
17267 	/*
17268 	 * Check PHYI_MULTI_BCAST and length of physical
17269 	 * address to determine if we use the mapping or the
17270 	 * broadcast address.
17271 	 */
17272 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17273 	    ill->ill_phys_addr_length != ETHERADDRL) {
17274 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17275 		return;
17276 	}
17277 	m_physaddr[0] = 0x01;
17278 	m_physaddr[1] = 0x00;
17279 	m_physaddr[2] = 0x5e;
17280 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17281 	m_physaddr[4] = m_ipaddr[2];
17282 	m_physaddr[5] = m_ipaddr[3];
17283 }
17284 
17285 /* ARGSUSED */
17286 static void
17287 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17288 {
17289 	/*
17290 	 * for the MULTI_BCAST case and other cases when we want to
17291 	 * use the link-layer broadcast address for multicast.
17292 	 */
17293 	uint8_t	*bphys_addr;
17294 	dl_unitdata_req_t *dlur;
17295 
17296 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17297 	if (ill->ill_sap_length < 0) {
17298 		bphys_addr = (uchar_t *)dlur +
17299 		    dlur->dl_dest_addr_offset;
17300 	} else  {
17301 		bphys_addr = (uchar_t *)dlur +
17302 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17303 	}
17304 
17305 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17306 }
17307 
17308 /*
17309  * Derive IPoIB interface id from the link layer address.
17310  */
17311 static void
17312 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17313 {
17314 	char		*addr;
17315 
17316 	ASSERT(ill->ill_phys_addr_length == 20);
17317 	addr = (char *)&v6addr->s6_addr32[2];
17318 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17319 	/*
17320 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17321 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17322 	 * rules. In these cases, the IBA considers these GUIDs to be in
17323 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17324 	 * required; vendors are required not to assign global EUI-64's
17325 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17326 	 * of the interface identifier. Whether the GUID is in modified
17327 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17328 	 * bit set to 1.
17329 	 */
17330 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17331 }
17332 
17333 /*
17334  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17335  * Note on mapping from multicast IP addresses to IPoIB multicast link
17336  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17337  * The format of an IPoIB multicast address is:
17338  *
17339  *  4 byte QPN      Scope Sign.  Pkey
17340  * +--------------------------------------------+
17341  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17342  * +--------------------------------------------+
17343  *
17344  * The Scope and Pkey components are properties of the IBA port and
17345  * network interface. They can be ascertained from the broadcast address.
17346  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17347  */
17348 static void
17349 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17350 {
17351 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17352 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17353 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17354 	uint8_t	*bphys_addr;
17355 	dl_unitdata_req_t *dlur;
17356 
17357 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17358 
17359 	/*
17360 	 * RFC 4391: IPv4 MGID is 28-bit long.
17361 	 */
17362 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17363 	m_physaddr[17] = m_ipaddr[1];
17364 	m_physaddr[18] = m_ipaddr[2];
17365 	m_physaddr[19] = m_ipaddr[3];
17366 
17367 
17368 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17369 	if (ill->ill_sap_length < 0) {
17370 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17371 	} else  {
17372 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17373 		    ill->ill_sap_length;
17374 	}
17375 	/*
17376 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17377 	 */
17378 	m_physaddr[5] = bphys_addr[5];
17379 	m_physaddr[8] = bphys_addr[8];
17380 	m_physaddr[9] = bphys_addr[9];
17381 }
17382 
17383 static void
17384 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17385 {
17386 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17387 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17388 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17389 	uint8_t	*bphys_addr;
17390 	dl_unitdata_req_t *dlur;
17391 
17392 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17393 
17394 	/*
17395 	 * RFC 4391: IPv4 MGID is 80-bit long.
17396 	 */
17397 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17398 
17399 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17400 	if (ill->ill_sap_length < 0) {
17401 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17402 	} else  {
17403 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17404 		    ill->ill_sap_length;
17405 	}
17406 	/*
17407 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17408 	 */
17409 	m_physaddr[5] = bphys_addr[5];
17410 	m_physaddr[8] = bphys_addr[8];
17411 	m_physaddr[9] = bphys_addr[9];
17412 }
17413 
17414 /*
17415  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17416  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17417  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17418  * of RFC4213.
17419  */
17420 static void
17421 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17422 {
17423 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17424 	v6addr->s6_addr32[2] = 0;
17425 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17426 }
17427 
17428 /*
17429  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17430  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17431  * id.
17432  */
17433 static void
17434 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17435 {
17436 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17437 
17438 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17439 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17440 }
17441 
17442 static void
17443 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17444 {
17445 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17446 }
17447 
17448 static void
17449 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17450 {
17451 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17452 }
17453 
17454 static void
17455 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17456 {
17457 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17458 }
17459 
17460 static void
17461 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17462 {
17463 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17464 }
17465 
17466 /*
17467  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17468  * Returns an held ill, or NULL.
17469  */
17470 ill_t *
17471 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17472     ip_stack_t *ipst)
17473 {
17474 	ill_t	*ill;
17475 	ipif_t	*ipif;
17476 
17477 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17478 	if (ill == NULL)
17479 		return (NULL);
17480 
17481 	mutex_enter(&ill->ill_lock);
17482 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17483 		if (IPIF_IS_CONDEMNED(ipif))
17484 			continue;
17485 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17486 		    ipif->ipif_zoneid != ALL_ZONES)
17487 			continue;
17488 
17489 		mutex_exit(&ill->ill_lock);
17490 		return (ill);
17491 	}
17492 	mutex_exit(&ill->ill_lock);
17493 	ill_refrele(ill);
17494 	return (NULL);
17495 }
17496 
17497 /*
17498  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17499  * If a pointer to an ipif_t is returned then the caller will need to do
17500  * an ill_refrele().
17501  */
17502 ipif_t *
17503 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17504     ip_stack_t *ipst)
17505 {
17506 	ipif_t *ipif;
17507 	ill_t *ill;
17508 
17509 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17510 	if (ill == NULL)
17511 		return (NULL);
17512 
17513 	mutex_enter(&ill->ill_lock);
17514 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17515 		mutex_exit(&ill->ill_lock);
17516 		ill_refrele(ill);
17517 		return (NULL);
17518 	}
17519 
17520 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17521 		if (!IPIF_CAN_LOOKUP(ipif))
17522 			continue;
17523 		if (lifidx == ipif->ipif_id) {
17524 			ipif_refhold_locked(ipif);
17525 			break;
17526 		}
17527 	}
17528 
17529 	mutex_exit(&ill->ill_lock);
17530 	ill_refrele(ill);
17531 	return (ipif);
17532 }
17533 
17534 /*
17535  * Set ill_inputfn based on the current know state.
17536  * This needs to be called when any of the factors taken into
17537  * account changes.
17538  */
17539 void
17540 ill_set_inputfn(ill_t *ill)
17541 {
17542 	ip_stack_t	*ipst = ill->ill_ipst;
17543 
17544 	if (ill->ill_isv6) {
17545 		if (is_system_labeled())
17546 			ill->ill_inputfn = ill_input_full_v6;
17547 		else
17548 			ill->ill_inputfn = ill_input_short_v6;
17549 	} else {
17550 		if (is_system_labeled())
17551 			ill->ill_inputfn = ill_input_full_v4;
17552 		else if (ill->ill_dhcpinit != 0)
17553 			ill->ill_inputfn = ill_input_full_v4;
17554 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17555 		    != NULL)
17556 			ill->ill_inputfn = ill_input_full_v4;
17557 		else if (ipst->ips_ip_cgtp_filter &&
17558 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17559 			ill->ill_inputfn = ill_input_full_v4;
17560 		else
17561 			ill->ill_inputfn = ill_input_short_v4;
17562 	}
17563 }
17564 
17565 /*
17566  * Re-evaluate ill_inputfn for all the IPv4 ills.
17567  * Used when RSVP and CGTP comes and goes.
17568  */
17569 void
17570 ill_set_inputfn_all(ip_stack_t *ipst)
17571 {
17572 	ill_walk_context_t	ctx;
17573 	ill_t			*ill;
17574 
17575 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17576 	ill = ILL_START_WALK_V4(&ctx, ipst);
17577 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17578 		ill_set_inputfn(ill);
17579 
17580 	rw_exit(&ipst->ips_ill_g_lock);
17581 }
17582 
17583 /*
17584  * Set the physical address information for `ill' to the contents of the
17585  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17586  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17587  * EINPROGRESS will be returned.
17588  */
17589 int
17590 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17591 {
17592 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17593 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17594 
17595 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17596 
17597 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17598 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17599 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17600 		/* Changing DL_IPV6_TOKEN is not yet supported */
17601 		return (0);
17602 	}
17603 
17604 	/*
17605 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17606 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17607 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17608 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17609 	 */
17610 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17611 		freemsg(mp);
17612 		return (ENOMEM);
17613 	}
17614 
17615 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17616 	mutex_enter(&ill->ill_lock);
17617 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17618 	/* no more nce addition allowed */
17619 	mutex_exit(&ill->ill_lock);
17620 
17621 	/*
17622 	 * If we can quiesce the ill, then set the address.  If not, then
17623 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17624 	 */
17625 	ill_down_ipifs(ill, B_TRUE);
17626 	mutex_enter(&ill->ill_lock);
17627 	if (!ill_is_quiescent(ill)) {
17628 		/* call cannot fail since `conn_t *' argument is NULL */
17629 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17630 		    mp, ILL_DOWN);
17631 		mutex_exit(&ill->ill_lock);
17632 		return (EINPROGRESS);
17633 	}
17634 	mutex_exit(&ill->ill_lock);
17635 
17636 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17637 	return (0);
17638 }
17639 
17640 /*
17641  * Once the ill associated with `q' has quiesced, set its physical address
17642  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17643  * are passed (linked by b_cont), since we sometimes need to save two distinct
17644  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17645  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17646  * is quiesced, we know any stale nce's with the old address information have
17647  * already been removed, so we don't need to call nce_flush().
17648  */
17649 /* ARGSUSED */
17650 static void
17651 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17652 {
17653 	ill_t		*ill = q->q_ptr;
17654 	mblk_t		*addrmp2 = unlinkb(addrmp);
17655 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17656 	uint_t		addrlen, addroff;
17657 	int		status;
17658 
17659 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17660 
17661 	addroff	= dlindp->dl_addr_offset;
17662 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17663 
17664 	switch (dlindp->dl_data) {
17665 	case DL_IPV6_LINK_LAYER_ADDR:
17666 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17667 		freemsg(addrmp2);
17668 		break;
17669 
17670 	case DL_CURR_DEST_ADDR:
17671 		freemsg(ill->ill_dest_addr_mp);
17672 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17673 		ill->ill_dest_addr_mp = addrmp;
17674 		if (ill->ill_isv6) {
17675 			ill_setdesttoken(ill);
17676 			ipif_setdestlinklocal(ill->ill_ipif);
17677 		}
17678 		freemsg(addrmp2);
17679 		break;
17680 
17681 	case DL_CURR_PHYS_ADDR:
17682 		freemsg(ill->ill_phys_addr_mp);
17683 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17684 		ill->ill_phys_addr_mp = addrmp;
17685 		ill->ill_phys_addr_length = addrlen;
17686 		if (ill->ill_isv6)
17687 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17688 		else
17689 			freemsg(addrmp2);
17690 		if (ill->ill_isv6) {
17691 			ill_setdefaulttoken(ill);
17692 			ipif_setlinklocal(ill->ill_ipif);
17693 		}
17694 		break;
17695 	default:
17696 		ASSERT(0);
17697 	}
17698 
17699 	/*
17700 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17701 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17702 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17703 	 * brought up.
17704 	 */
17705 	status = ill_up_ipifs(ill, q, addrmp);
17706 	mutex_enter(&ill->ill_lock);
17707 	if (ill->ill_dl_up)
17708 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17709 	mutex_exit(&ill->ill_lock);
17710 	if (status != EINPROGRESS)
17711 		ipsq_current_finish(ipsq);
17712 }
17713 
17714 /*
17715  * Helper routine for setting the ill_nd_lla fields.
17716  */
17717 void
17718 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17719 {
17720 	freemsg(ill->ill_nd_lla_mp);
17721 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17722 	ill->ill_nd_lla_mp = ndmp;
17723 	ill->ill_nd_lla_len = addrlen;
17724 }
17725 
17726 /*
17727  * Replumb the ill.
17728  */
17729 int
17730 ill_replumb(ill_t *ill, mblk_t *mp)
17731 {
17732 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17733 
17734 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17735 
17736 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17737 
17738 	mutex_enter(&ill->ill_lock);
17739 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17740 	/* no more nce addition allowed */
17741 	mutex_exit(&ill->ill_lock);
17742 
17743 	/*
17744 	 * If we can quiesce the ill, then continue.  If not, then
17745 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17746 	 */
17747 	ill_down_ipifs(ill, B_FALSE);
17748 
17749 	mutex_enter(&ill->ill_lock);
17750 	if (!ill_is_quiescent(ill)) {
17751 		/* call cannot fail since `conn_t *' argument is NULL */
17752 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17753 		    mp, ILL_DOWN);
17754 		mutex_exit(&ill->ill_lock);
17755 		return (EINPROGRESS);
17756 	}
17757 	mutex_exit(&ill->ill_lock);
17758 
17759 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17760 	return (0);
17761 }
17762 
17763 /* ARGSUSED */
17764 static void
17765 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17766 {
17767 	ill_t *ill = q->q_ptr;
17768 	int err;
17769 	conn_t *connp = NULL;
17770 
17771 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17772 	freemsg(ill->ill_replumb_mp);
17773 	ill->ill_replumb_mp = copyb(mp);
17774 
17775 	if (ill->ill_replumb_mp == NULL) {
17776 		/* out of memory */
17777 		ipsq_current_finish(ipsq);
17778 		return;
17779 	}
17780 
17781 	mutex_enter(&ill->ill_lock);
17782 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17783 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17784 	mutex_exit(&ill->ill_lock);
17785 
17786 	if (!ill->ill_up_ipifs) {
17787 		/* already closing */
17788 		ipsq_current_finish(ipsq);
17789 		return;
17790 	}
17791 	ill->ill_replumbing = 1;
17792 	err = ill_down_ipifs_tail(ill);
17793 
17794 	/*
17795 	 * Successfully quiesced and brought down the interface, now we send
17796 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17797 	 * DL_NOTE_REPLUMB message.
17798 	 */
17799 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17800 	    DL_NOTIFY_CONF);
17801 	ASSERT(mp != NULL);
17802 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17803 	    DL_NOTE_REPLUMB_DONE;
17804 	ill_dlpi_send(ill, mp);
17805 
17806 	/*
17807 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17808 	 * streams have to be unbound. When all the DLPI exchanges are done,
17809 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17810 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17811 	 * arp_bringup_done().
17812 	 */
17813 	ASSERT(ill->ill_replumb_mp != NULL);
17814 	if (err == EINPROGRESS)
17815 		return;
17816 	else
17817 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17818 	ASSERT(connp == NULL);
17819 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17820 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17821 		return;
17822 	}
17823 	ipsq_current_finish(ipsq);
17824 }
17825 
17826 /*
17827  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17828  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17829  * as per the ioctl.  On failure, an errno is returned.
17830  */
17831 static int
17832 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17833 {
17834 	int rval;
17835 	struct strioctl iocb;
17836 
17837 	iocb.ic_cmd = cmd;
17838 	iocb.ic_timout = 15;
17839 	iocb.ic_len = bufsize;
17840 	iocb.ic_dp = buf;
17841 
17842 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17843 }
17844 
17845 /*
17846  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17847  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17848  */
17849 static int
17850 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17851     uint_t *bufsizep, cred_t *cr)
17852 {
17853 	int err;
17854 	struct lifnum lifn;
17855 
17856 	bzero(&lifn, sizeof (lifn));
17857 	lifn.lifn_family = af;
17858 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17859 
17860 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17861 		return (err);
17862 
17863 	/*
17864 	 * Pad the interface count to account for additional interfaces that
17865 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17866 	 */
17867 	lifn.lifn_count += 4;
17868 	bzero(lifcp, sizeof (*lifcp));
17869 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17870 	lifcp->lifc_family = af;
17871 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17872 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17873 
17874 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17875 	if (err != 0) {
17876 		kmem_free(lifcp->lifc_buf, *bufsizep);
17877 		return (err);
17878 	}
17879 
17880 	return (0);
17881 }
17882 
17883 /*
17884  * Helper for ip_interface_cleanup() that removes the loopback interface.
17885  */
17886 static void
17887 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17888 {
17889 	int err;
17890 	struct lifreq lifr;
17891 
17892 	bzero(&lifr, sizeof (lifr));
17893 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17894 
17895 	/*
17896 	 * Attempt to remove the interface.  It may legitimately not exist
17897 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
17898 	 */
17899 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17900 	if (err != 0 && err != ENXIO) {
17901 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17902 		    "error %d\n", isv6 ? "v6" : "v4", err));
17903 	}
17904 }
17905 
17906 /*
17907  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17908  * groups and that IPMP data addresses are down.  These conditions must be met
17909  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17910  */
17911 static void
17912 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17913 {
17914 	int af = isv6 ? AF_INET6 : AF_INET;
17915 	int i, nifs;
17916 	int err;
17917 	uint_t bufsize;
17918 	uint_t lifrsize = sizeof (struct lifreq);
17919 	struct lifconf lifc;
17920 	struct lifreq *lifrp;
17921 
17922 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17923 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17924 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17925 		return;
17926 	}
17927 
17928 	nifs = lifc.lifc_len / lifrsize;
17929 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17930 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17931 		if (err != 0) {
17932 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17933 			    "flags: error %d", lifrp->lifr_name, err);
17934 			continue;
17935 		}
17936 
17937 		if (lifrp->lifr_flags & IFF_IPMP) {
17938 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17939 				continue;
17940 
17941 			lifrp->lifr_flags &= ~IFF_UP;
17942 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17943 			if (err != 0) {
17944 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17945 				    "bring down (error %d); IPMP interface may "
17946 				    "not be shutdown", lifrp->lifr_name, err);
17947 			}
17948 
17949 			/*
17950 			 * Check if IFF_DUPLICATE is still set -- and if so,
17951 			 * reset the address to clear it.
17952 			 */
17953 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17954 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17955 				continue;
17956 
17957 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17958 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17959 			    lifrp, lifrsize, cr)) != 0) {
17960 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17961 				    "reset DAD (error %d); IPMP interface may "
17962 				    "not be shutdown", lifrp->lifr_name, err);
17963 			}
17964 			continue;
17965 		}
17966 
17967 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
17968 			lifrp->lifr_groupname[0] = '\0';
17969 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
17970 			    lifrsize, cr)) != 0) {
17971 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17972 				    "leave IPMP group (error %d); associated "
17973 				    "IPMP interface may not be shutdown",
17974 				    lifrp->lifr_name, err);
17975 				continue;
17976 			}
17977 		}
17978 	}
17979 
17980 	kmem_free(lifc.lifc_buf, bufsize);
17981 }
17982 
17983 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17984 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17985 
17986 /*
17987  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17988  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17989  * when the user-level processes in the zone are killed and the latter are
17990  * cleaned up by str_stack_shutdown().
17991  */
17992 void
17993 ip_interface_cleanup(ip_stack_t *ipst)
17994 {
17995 	ldi_handle_t	lh;
17996 	ldi_ident_t	li;
17997 	cred_t		*cr;
17998 	int		err;
17999 	int		i;
18000 	char		*devs[] = { UDP6DEV, UDPDEV };
18001 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18002 
18003 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18004 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18005 		    " error %d", err);
18006 		return;
18007 	}
18008 
18009 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18010 	ASSERT(cr != NULL);
18011 
18012 	/*
18013 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18014 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18015 	 * the loop.)
18016 	 */
18017 	for (i = 0; i < 2; i++) {
18018 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18019 		if (err != 0) {
18020 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18021 			    " error %d", devs[i], err);
18022 			continue;
18023 		}
18024 
18025 		ip_loopback_removeif(lh, i == 0, cr);
18026 		ip_ipmp_cleanup(lh, i == 0, cr);
18027 
18028 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18029 	}
18030 
18031 	ldi_ident_release(li);
18032 	crfree(cr);
18033 }
18034 
18035 /*
18036  * This needs to be in-sync with nic_event_t definition
18037  */
18038 static const char *
18039 ill_hook_event2str(nic_event_t event)
18040 {
18041 	switch (event) {
18042 	case NE_PLUMB:
18043 		return ("PLUMB");
18044 	case NE_UNPLUMB:
18045 		return ("UNPLUMB");
18046 	case NE_UP:
18047 		return ("UP");
18048 	case NE_DOWN:
18049 		return ("DOWN");
18050 	case NE_ADDRESS_CHANGE:
18051 		return ("ADDRESS_CHANGE");
18052 	case NE_LIF_UP:
18053 		return ("LIF_UP");
18054 	case NE_LIF_DOWN:
18055 		return ("LIF_DOWN");
18056 	case NE_IFINDEX_CHANGE:
18057 		return ("IFINDEX_CHANGE");
18058 	default:
18059 		return ("UNKNOWN");
18060 	}
18061 }
18062 
18063 void
18064 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18065     nic_event_data_t data, size_t datalen)
18066 {
18067 	ip_stack_t		*ipst = ill->ill_ipst;
18068 	hook_nic_event_int_t	*info;
18069 	const char		*str = NULL;
18070 
18071 	/* create a new nic event info */
18072 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18073 		goto fail;
18074 
18075 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18076 	info->hnei_event.hne_lif = lif;
18077 	info->hnei_event.hne_event = event;
18078 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18079 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18080 	info->hnei_event.hne_data = NULL;
18081 	info->hnei_event.hne_datalen = 0;
18082 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18083 
18084 	if (data != NULL && datalen != 0) {
18085 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18086 		if (info->hnei_event.hne_data == NULL)
18087 			goto fail;
18088 		bcopy(data, info->hnei_event.hne_data, datalen);
18089 		info->hnei_event.hne_datalen = datalen;
18090 	}
18091 
18092 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18093 	    DDI_NOSLEEP) == DDI_SUCCESS)
18094 		return;
18095 
18096 fail:
18097 	if (info != NULL) {
18098 		if (info->hnei_event.hne_data != NULL) {
18099 			kmem_free(info->hnei_event.hne_data,
18100 			    info->hnei_event.hne_datalen);
18101 		}
18102 		kmem_free(info, sizeof (hook_nic_event_t));
18103 	}
18104 	str = ill_hook_event2str(event);
18105 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18106 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18107 }
18108 
18109 static int
18110 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18111 {
18112 	int		err = 0;
18113 	const in_addr_t	*addr = NULL;
18114 	nce_t		*nce = NULL;
18115 	ill_t		*ill = ipif->ipif_ill;
18116 	ill_t		*bound_ill;
18117 	boolean_t	added_ipif = B_FALSE;
18118 	uint16_t	state;
18119 	uint16_t	flags;
18120 
18121 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18122 	    ill_t *, ill, ipif_t *, ipif);
18123 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18124 		addr = &ipif->ipif_lcl_addr;
18125 	}
18126 
18127 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18128 		if (res_act != Res_act_initial)
18129 			return (EINVAL);
18130 	}
18131 
18132 	if (addr != NULL) {
18133 		ipmp_illgrp_t	*illg = ill->ill_grp;
18134 
18135 		/* add unicast nce for the local addr */
18136 
18137 		if (IS_IPMP(ill)) {
18138 			/*
18139 			 * If we're here via ipif_up(), then the ipif
18140 			 * won't be bound yet -- add it to the group,
18141 			 * which will bind it if possible. (We would
18142 			 * add it in ipif_up(), but deleting on failure
18143 			 * there is gruesome.)  If we're here via
18144 			 * ipmp_ill_bind_ipif(), then the ipif has
18145 			 * already been added to the group and we
18146 			 * just need to use the binding.
18147 			 */
18148 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18149 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18150 				if (bound_ill == NULL) {
18151 					/*
18152 					 * We couldn't bind the ipif to an ill
18153 					 * yet, so we have nothing to publish.
18154 					 * Mark the address as ready and return.
18155 					 */
18156 					ipif->ipif_addr_ready = 1;
18157 					return (0);
18158 				}
18159 				added_ipif = B_TRUE;
18160 			}
18161 		} else {
18162 			bound_ill = ill;
18163 		}
18164 
18165 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18166 		    NCE_F_NONUD);
18167 		/*
18168 		 * If this is an initial bring-up (or the ipif was never
18169 		 * completely brought up), do DAD.  Otherwise, we're here
18170 		 * because IPMP has rebound an address to this ill: send
18171 		 * unsolicited advertisements (ARP announcements) to
18172 		 * inform others.
18173 		 */
18174 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18175 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18176 		} else {
18177 			state = ND_REACHABLE;
18178 			flags |= NCE_F_UNSOL_ADV;
18179 		}
18180 
18181 retry:
18182 		err = nce_lookup_then_add_v4(ill,
18183 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18184 		    addr, flags, state, &nce);
18185 
18186 		/*
18187 		 * note that we may encounter EEXIST if we are moving
18188 		 * the nce as a result of a rebind operation.
18189 		 */
18190 		switch (err) {
18191 		case 0:
18192 			ipif->ipif_added_nce = 1;
18193 			nce->nce_ipif_cnt++;
18194 			break;
18195 		case EEXIST:
18196 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18197 			    ill->ill_name));
18198 			if (!NCE_MYADDR(nce->nce_common)) {
18199 				/*
18200 				 * A leftover nce from before this address
18201 				 * existed
18202 				 */
18203 				ncec_delete(nce->nce_common);
18204 				nce_refrele(nce);
18205 				nce = NULL;
18206 				goto retry;
18207 			}
18208 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18209 				nce_refrele(nce);
18210 				nce = NULL;
18211 				ip1dbg(("ipif_arp_up: NCE already exists "
18212 				    "for %s:%u\n", ill->ill_name,
18213 				    ipif->ipif_id));
18214 				goto arp_up_done;
18215 			}
18216 			/*
18217 			 * Duplicate local addresses are permissible for
18218 			 * IPIF_POINTOPOINT interfaces which will get marked
18219 			 * IPIF_UNNUMBERED later in
18220 			 * ip_addr_availability_check().
18221 			 *
18222 			 * The nce_ipif_cnt field tracks the number of
18223 			 * ipifs that have nce_addr as their local address.
18224 			 */
18225 			ipif->ipif_addr_ready = 1;
18226 			ipif->ipif_added_nce = 1;
18227 			nce->nce_ipif_cnt++;
18228 			err = 0;
18229 			break;
18230 		default:
18231 			ASSERT(nce == NULL);
18232 			goto arp_up_done;
18233 		}
18234 		if (arp_no_defense) {
18235 			if ((ipif->ipif_flags & IPIF_UP) &&
18236 			    !ipif->ipif_addr_ready)
18237 				ipif_up_notify(ipif);
18238 			ipif->ipif_addr_ready = 1;
18239 		}
18240 	} else {
18241 		/* zero address. nothing to publish */
18242 		ipif->ipif_addr_ready = 1;
18243 	}
18244 	if (nce != NULL)
18245 		nce_refrele(nce);
18246 arp_up_done:
18247 	if (added_ipif && err != 0)
18248 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18249 	return (err);
18250 }
18251 
18252 int
18253 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18254 {
18255 	int 		err = 0;
18256 	ill_t 		*ill = ipif->ipif_ill;
18257 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18258 
18259 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18260 	    ill_t *, ill, ipif_t *, ipif);
18261 
18262 	/*
18263 	 * need to bring up ARP or setup mcast mapping only
18264 	 * when the first interface is coming UP.
18265 	 */
18266 	first_interface = (ill->ill_ipif_up_count == 0 &&
18267 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18268 
18269 	if (res_act == Res_act_initial && first_interface) {
18270 		/*
18271 		 * Send ATTACH + BIND
18272 		 */
18273 		err = arp_ll_up(ill);
18274 		if (err != EINPROGRESS && err != 0)
18275 			return (err);
18276 
18277 		/*
18278 		 * Add NCE for local address. Start DAD.
18279 		 * we'll wait to hear that DAD has finished
18280 		 * before using the interface.
18281 		 */
18282 		if (err == EINPROGRESS)
18283 			wait_for_dlpi = B_TRUE;
18284 	}
18285 
18286 	if (!wait_for_dlpi)
18287 		(void) ipif_arp_up_done_tail(ipif, res_act);
18288 
18289 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18290 }
18291 
18292 /*
18293  * Finish processing of "arp_up" after all the DLPI message
18294  * exchanges have completed between arp and the driver.
18295  */
18296 void
18297 arp_bringup_done(ill_t *ill, int err)
18298 {
18299 	mblk_t	*mp1;
18300 	ipif_t  *ipif;
18301 	conn_t *connp = NULL;
18302 	ipsq_t	*ipsq;
18303 	queue_t *q;
18304 
18305 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18306 
18307 	ASSERT(IAM_WRITER_ILL(ill));
18308 
18309 	ipsq = ill->ill_phyint->phyint_ipsq;
18310 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18311 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18312 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18313 	if (mp1 == NULL) /* bringup was aborted by the user */
18314 		return;
18315 
18316 	/*
18317 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18318 	 * must have an associated conn_t.  Otherwise, we're bringing this
18319 	 * interface back up as part of handling an asynchronous event (e.g.,
18320 	 * physical address change).
18321 	 */
18322 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18323 		ASSERT(connp != NULL);
18324 		q = CONNP_TO_WQ(connp);
18325 	} else {
18326 		ASSERT(connp == NULL);
18327 		q = ill->ill_rq;
18328 	}
18329 	if (err == 0) {
18330 		if (ipif->ipif_isv6) {
18331 			if ((err = ipif_up_done_v6(ipif)) != 0)
18332 				ip0dbg(("arp_bringup_done: init failed\n"));
18333 		} else {
18334 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18335 			if (err != 0 ||
18336 			    (err = ipif_up_done(ipif)) != 0) {
18337 				ip0dbg(("arp_bringup_done: "
18338 				    "init failed err %x\n", err));
18339 				(void) ipif_arp_down(ipif);
18340 			}
18341 
18342 		}
18343 	} else {
18344 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18345 	}
18346 
18347 	if ((err == 0) && (ill->ill_up_ipifs)) {
18348 		err = ill_up_ipifs(ill, q, mp1);
18349 		if (err == EINPROGRESS)
18350 			return;
18351 	}
18352 
18353 	/*
18354 	 * If we have a moved ipif to bring up, and everything has succeeded
18355 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18356 	 * down -- the admin can try to bring it up by hand if need be.
18357 	 */
18358 	if (ill->ill_move_ipif != NULL) {
18359 		ipif = ill->ill_move_ipif;
18360 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18361 		    ipif->ipif_ill->ill_name));
18362 		ill->ill_move_ipif = NULL;
18363 		if (err == 0) {
18364 			err = ipif_up(ipif, q, mp1);
18365 			if (err == EINPROGRESS)
18366 				return;
18367 		}
18368 	}
18369 
18370 	/*
18371 	 * The operation must complete without EINPROGRESS since
18372 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18373 	 * Otherwise, the operation will be stuck forever in the ipsq.
18374 	 */
18375 	ASSERT(err != EINPROGRESS);
18376 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18377 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18378 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18379 		    ill_t *, ill, ipif_t *, ipif);
18380 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18381 	} else {
18382 		ipsq_current_finish(ipsq);
18383 	}
18384 }
18385 
18386 /*
18387  * Finish processing of arp replumb after all the DLPI message
18388  * exchanges have completed between arp and the driver.
18389  */
18390 void
18391 arp_replumb_done(ill_t *ill, int err)
18392 {
18393 	mblk_t	*mp1;
18394 	ipif_t  *ipif;
18395 	conn_t *connp = NULL;
18396 	ipsq_t	*ipsq;
18397 	queue_t *q;
18398 
18399 	ASSERT(IAM_WRITER_ILL(ill));
18400 
18401 	ipsq = ill->ill_phyint->phyint_ipsq;
18402 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18403 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18404 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18405 	if (mp1 == NULL) {
18406 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18407 		    ipsq->ipsq_xop->ipx_current_ioctl));
18408 		/* bringup was aborted by the user */
18409 		return;
18410 	}
18411 	/*
18412 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18413 	 * must have an associated conn_t.  Otherwise, we're bringing this
18414 	 * interface back up as part of handling an asynchronous event (e.g.,
18415 	 * physical address change).
18416 	 */
18417 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18418 		ASSERT(connp != NULL);
18419 		q = CONNP_TO_WQ(connp);
18420 	} else {
18421 		ASSERT(connp == NULL);
18422 		q = ill->ill_rq;
18423 	}
18424 	if ((err == 0) && (ill->ill_up_ipifs)) {
18425 		err = ill_up_ipifs(ill, q, mp1);
18426 		if (err == EINPROGRESS)
18427 			return;
18428 	}
18429 	/*
18430 	 * The operation must complete without EINPROGRESS since
18431 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18432 	 * Otherwise, the operation will be stuck forever in the ipsq.
18433 	 */
18434 	ASSERT(err != EINPROGRESS);
18435 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18436 		DTRACE_PROBE4(ipif__ioctl, char *,
18437 		    "arp_replumb_done finish",
18438 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18439 		    ill_t *, ill, ipif_t *, ipif);
18440 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18441 	} else {
18442 		ipsq_current_finish(ipsq);
18443 	}
18444 }
18445 
18446 void
18447 ipif_up_notify(ipif_t *ipif)
18448 {
18449 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18450 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18451 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18452 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18453 	    NE_LIF_UP, NULL, 0);
18454 }
18455 
18456 /*
18457  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18458  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18459  * TPI end points with STREAMS modules pushed above.  This is assured by not
18460  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18461  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18462  * while unwinding from the ispq and that could be a thread from the bottom.
18463  */
18464 /* ARGSUSED */
18465 int
18466 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18467     ip_ioctl_cmd_t *ipip, void *arg)
18468 {
18469 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18470 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18471 	int ret = 0;
18472 	int i;
18473 	size_t size;
18474 	ip_stack_t *ipst;
18475 	zoneid_t zoneid;
18476 	ilb_stack_t *ilbs;
18477 
18478 	ipst = CONNQ_TO_IPST(q);
18479 	ilbs = ipst->ips_netstack->netstack_ilb;
18480 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18481 
18482 	switch (command) {
18483 	case ILB_CREATE_RULE: {
18484 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18485 
18486 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18487 			ret = EINVAL;
18488 			break;
18489 		}
18490 
18491 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18492 		break;
18493 	}
18494 	case ILB_DESTROY_RULE:
18495 	case ILB_ENABLE_RULE:
18496 	case ILB_DISABLE_RULE: {
18497 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18498 
18499 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18500 			ret = EINVAL;
18501 			break;
18502 		}
18503 
18504 		if (cmd->flags & ILB_RULE_ALLRULES) {
18505 			if (command == ILB_DESTROY_RULE) {
18506 				ilb_rule_del_all(ilbs, zoneid);
18507 				break;
18508 			} else if (command == ILB_ENABLE_RULE) {
18509 				ilb_rule_enable_all(ilbs, zoneid);
18510 				break;
18511 			} else if (command == ILB_DISABLE_RULE) {
18512 				ilb_rule_disable_all(ilbs, zoneid);
18513 				break;
18514 			}
18515 		} else {
18516 			if (command == ILB_DESTROY_RULE) {
18517 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18518 			} else if (command == ILB_ENABLE_RULE) {
18519 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18520 				    NULL);
18521 			} else if (command == ILB_DISABLE_RULE) {
18522 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18523 				    NULL);
18524 			}
18525 		}
18526 		break;
18527 	}
18528 	case ILB_NUM_RULES: {
18529 		ilb_num_rules_cmd_t *cmd;
18530 
18531 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18532 			ret = EINVAL;
18533 			break;
18534 		}
18535 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18536 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18537 		break;
18538 	}
18539 	case ILB_RULE_NAMES: {
18540 		ilb_rule_names_cmd_t *cmd;
18541 
18542 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18543 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18544 		    cmd->num_names == 0) {
18545 			ret = EINVAL;
18546 			break;
18547 		}
18548 		size = cmd->num_names * ILB_RULE_NAMESZ;
18549 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18550 		    size != cmd_mp->b_wptr) {
18551 			ret = EINVAL;
18552 			break;
18553 		}
18554 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18555 		break;
18556 	}
18557 	case ILB_NUM_SERVERS: {
18558 		ilb_num_servers_cmd_t *cmd;
18559 
18560 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18561 			ret = EINVAL;
18562 			break;
18563 		}
18564 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18565 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18566 		    &(cmd->num));
18567 		break;
18568 	}
18569 	case ILB_LIST_RULE: {
18570 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18571 
18572 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18573 			ret = EINVAL;
18574 			break;
18575 		}
18576 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18577 		break;
18578 	}
18579 	case ILB_LIST_SERVERS: {
18580 		ilb_servers_info_cmd_t *cmd;
18581 
18582 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18583 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18584 		    cmd->num_servers == 0) {
18585 			ret = EINVAL;
18586 			break;
18587 		}
18588 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18589 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18590 		    size != cmd_mp->b_wptr) {
18591 			ret = EINVAL;
18592 			break;
18593 		}
18594 
18595 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18596 		    &cmd->num_servers);
18597 		break;
18598 	}
18599 	case ILB_ADD_SERVERS: {
18600 		ilb_servers_info_cmd_t *cmd;
18601 		ilb_rule_t *rule;
18602 
18603 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18604 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18605 			ret = EINVAL;
18606 			break;
18607 		}
18608 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18609 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18610 		    size != cmd_mp->b_wptr) {
18611 			ret = EINVAL;
18612 			break;
18613 		}
18614 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18615 		if (rule == NULL) {
18616 			ASSERT(ret != 0);
18617 			break;
18618 		}
18619 		for (i = 0; i < cmd->num_servers; i++) {
18620 			ilb_server_info_t *s;
18621 
18622 			s = &cmd->servers[i];
18623 			s->err = ilb_server_add(ilbs, rule, s);
18624 		}
18625 		ILB_RULE_REFRELE(rule);
18626 		break;
18627 	}
18628 	case ILB_DEL_SERVERS:
18629 	case ILB_ENABLE_SERVERS:
18630 	case ILB_DISABLE_SERVERS: {
18631 		ilb_servers_cmd_t *cmd;
18632 		ilb_rule_t *rule;
18633 		int (*f)();
18634 
18635 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18636 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18637 			ret = EINVAL;
18638 			break;
18639 		}
18640 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18641 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18642 		    size != cmd_mp->b_wptr) {
18643 			ret = EINVAL;
18644 			break;
18645 		}
18646 
18647 		if (command == ILB_DEL_SERVERS)
18648 			f = ilb_server_del;
18649 		else if (command == ILB_ENABLE_SERVERS)
18650 			f = ilb_server_enable;
18651 		else if (command == ILB_DISABLE_SERVERS)
18652 			f = ilb_server_disable;
18653 
18654 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18655 		if (rule == NULL) {
18656 			ASSERT(ret != 0);
18657 			break;
18658 		}
18659 
18660 		for (i = 0; i < cmd->num_servers; i++) {
18661 			ilb_server_arg_t *s;
18662 
18663 			s = &cmd->servers[i];
18664 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18665 		}
18666 		ILB_RULE_REFRELE(rule);
18667 		break;
18668 	}
18669 	case ILB_LIST_NAT_TABLE: {
18670 		ilb_list_nat_cmd_t *cmd;
18671 
18672 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18673 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18674 			ret = EINVAL;
18675 			break;
18676 		}
18677 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18678 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18679 		    size != cmd_mp->b_wptr) {
18680 			ret = EINVAL;
18681 			break;
18682 		}
18683 
18684 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18685 		    &cmd->flags);
18686 		break;
18687 	}
18688 	case ILB_LIST_STICKY_TABLE: {
18689 		ilb_list_sticky_cmd_t *cmd;
18690 
18691 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18692 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18693 			ret = EINVAL;
18694 			break;
18695 		}
18696 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18697 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18698 		    size != cmd_mp->b_wptr) {
18699 			ret = EINVAL;
18700 			break;
18701 		}
18702 
18703 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18704 		    &cmd->num_sticky, &cmd->flags);
18705 		break;
18706 	}
18707 	default:
18708 		ret = EINVAL;
18709 		break;
18710 	}
18711 done:
18712 	return (ret);
18713 }
18714 
18715 /* Remove all cache entries for this logical interface */
18716 void
18717 ipif_nce_down(ipif_t *ipif)
18718 {
18719 	ill_t *ill = ipif->ipif_ill;
18720 	nce_t *nce;
18721 
18722 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18723 	    ill_t *, ill, ipif_t *, ipif);
18724 	if (ipif->ipif_added_nce) {
18725 		if (ipif->ipif_isv6)
18726 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18727 		else
18728 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18729 		if (nce != NULL) {
18730 			if (--nce->nce_ipif_cnt == 0)
18731 				ncec_delete(nce->nce_common);
18732 			ipif->ipif_added_nce = 0;
18733 			nce_refrele(nce);
18734 		} else {
18735 			/*
18736 			 * nce may already be NULL because it was already
18737 			 * flushed, e.g., due to a call to nce_flush
18738 			 */
18739 			ipif->ipif_added_nce = 0;
18740 		}
18741 	}
18742 	/*
18743 	 * Make IPMP aware of the deleted data address.
18744 	 */
18745 	if (IS_IPMP(ill))
18746 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18747 
18748 	/*
18749 	 * Remove all other nces dependent on this ill when the last ipif
18750 	 * is going away.
18751 	 */
18752 	if (ill->ill_ipif_up_count == 0) {
18753 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18754 		    (uchar_t *)ill, ill->ill_ipst);
18755 		if (IS_UNDER_IPMP(ill))
18756 			nce_flush(ill, B_TRUE);
18757 	}
18758 }
18759 
18760 /*
18761  * find the first interface that uses usill for its source address.
18762  */
18763 ill_t *
18764 ill_lookup_usesrc(ill_t *usill)
18765 {
18766 	ip_stack_t *ipst = usill->ill_ipst;
18767 	ill_t *ill;
18768 
18769 	ASSERT(usill != NULL);
18770 
18771 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18772 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18773 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18774 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18775 	    ill = ill->ill_usesrc_grp_next) {
18776 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18777 		    !ILL_IS_CONDEMNED(ill)) {
18778 			ill_refhold(ill);
18779 			break;
18780 		}
18781 	}
18782 	rw_exit(&ipst->ips_ill_g_lock);
18783 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18784 	return (ill);
18785 }
18786