xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 1d7382f724e745e00a005bae397cdb54a0179ceb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
118 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
119 		    char *value, caddr_t cp, cred_t *ioc_cr);
120 
121 static boolean_t ill_is_quiescent(ill_t *);
122 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
123 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
124 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
125     mblk_t *mp, boolean_t need_up);
126 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
127     mblk_t *mp, boolean_t need_up);
128 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
129     queue_t *q, mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
131     mblk_t *mp);
132 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp);
134 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
137     int ioccmd, struct linkblk *li);
138 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
139 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
140 static void	ipsq_flush(ill_t *ill);
141 
142 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static void	ipsq_delete(ipsq_t *);
145 
146 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
147     boolean_t initialize, boolean_t insert, int *errorp);
148 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
149 static void	ipif_delete_bcast_ires(ipif_t *ipif);
150 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
151 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
152 		    boolean_t isv6);
153 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
154 static void	ipif_free(ipif_t *ipif);
155 static void	ipif_free_tail(ipif_t *ipif);
156 static void	ipif_set_default(ipif_t *ipif);
157 static int	ipif_set_values(queue_t *q, mblk_t *mp,
158     char *interf_name, uint_t *ppa);
159 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
160     queue_t *q);
161 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
162     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
163     ip_stack_t *);
164 
165 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
166 static void	ill_delete_interface_type(ill_if_t *);
167 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
168 static void	ill_dl_down(ill_t *ill);
169 static void	ill_down(ill_t *ill);
170 static void	ill_down_ipifs(ill_t *, boolean_t);
171 static void	ill_free_mib(ill_t *ill);
172 static void	ill_glist_delete(ill_t *);
173 static void	ill_phyint_reinit(ill_t *ill);
174 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
175 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
177 
178 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
179 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
180 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
181 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
182 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
183 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
184 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
185 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
186 static ip_v4mapinfo_func_t ip_mbcast_mapping;
187 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
188 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
189 static void	phyint_free(phyint_t *);
190 
191 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
194 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
196 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
197     dl_capability_sub_t *);
198 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
199 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
200 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
201 		    dl_capability_sub_t *);
202 static void	ill_capability_dld_enable(ill_t *);
203 static void	ill_capability_ack_thr(void *);
204 static void	ill_capability_lso_enable(ill_t *);
205 
206 static ill_t	*ill_prev_usesrc(ill_t *);
207 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
208 static void	ill_disband_usesrc_group(ill_t *);
209 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
210 
211 #ifdef DEBUG
212 static	void	ill_trace_cleanup(const ill_t *);
213 static	void	ipif_trace_cleanup(const ipif_t *);
214 #endif
215 
216 static	void	ill_dlpi_clear_deferred(ill_t *ill);
217 
218 /*
219  * if we go over the memory footprint limit more than once in this msec
220  * interval, we'll start pruning aggressively.
221  */
222 int ip_min_frag_prune_time = 0;
223 
224 static ipft_t	ip_ioctl_ftbl[] = {
225 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
226 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
227 		IPFT_F_NO_REPLY },
228 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
229 	{ 0 }
230 };
231 
232 /* Simple ICMP IP Header Template */
233 static ipha_t icmp_ipha = {
234 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
235 };
236 
237 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
238 
239 static ip_m_t   ip_m_tbl[] = {
240 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
241 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
242 	    ip_nodef_v6intfid },
243 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
259 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
260 	    ip_ipv4_v6destintfid },
261 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
263 	    ip_ipv6_v6destintfid },
264 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_nodef_v6intfid },
267 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
269 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
271 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
272 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
273 	    ip_nodef_v6intfid }
274 };
275 
276 static ill_t	ill_null;		/* Empty ILL for init. */
277 char	ipif_loopback_name[] = "lo0";
278 static char *ipv4_forward_suffix = ":ip_forwarding";
279 static char *ipv6_forward_suffix = ":ip6_forwarding";
280 
281 /* These are used by all IP network modules. */
282 sin6_t	sin6_null;	/* Zero address for quick clears */
283 sin_t	sin_null;	/* Zero address for quick clears */
284 
285 /* When set search for unused ipif_seqid */
286 static ipif_t	ipif_zero;
287 
288 /*
289  * ppa arena is created after these many
290  * interfaces have been plumbed.
291  */
292 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
293 
294 /*
295  * Allocate per-interface mibs.
296  * Returns true if ok. False otherwise.
297  *  ipsq  may not yet be allocated (loopback case ).
298  */
299 static boolean_t
300 ill_allocate_mibs(ill_t *ill)
301 {
302 	/* Already allocated? */
303 	if (ill->ill_ip_mib != NULL) {
304 		if (ill->ill_isv6)
305 			ASSERT(ill->ill_icmp6_mib != NULL);
306 		return (B_TRUE);
307 	}
308 
309 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
310 	    KM_NOSLEEP);
311 	if (ill->ill_ip_mib == NULL) {
312 		return (B_FALSE);
313 	}
314 
315 	/* Setup static information */
316 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
317 	    sizeof (mib2_ipIfStatsEntry_t));
318 	if (ill->ill_isv6) {
319 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
321 		    sizeof (mib2_ipv6AddrEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
323 		    sizeof (mib2_ipv6RouteEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
325 		    sizeof (mib2_ipv6NetToMediaEntry_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
327 		    sizeof (ipv6_member_t));
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
329 		    sizeof (ipv6_grpsrc_t));
330 	} else {
331 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
333 		    sizeof (mib2_ipAddrEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
335 		    sizeof (mib2_ipRouteEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
337 		    sizeof (mib2_ipNetToMediaEntry_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
339 		    sizeof (ip_member_t));
340 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
341 		    sizeof (ip_grpsrc_t));
342 
343 		/*
344 		 * For a v4 ill, we are done at this point, because per ill
345 		 * icmp mibs are only used for v6.
346 		 */
347 		return (B_TRUE);
348 	}
349 
350 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
351 	    KM_NOSLEEP);
352 	if (ill->ill_icmp6_mib == NULL) {
353 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
354 		ill->ill_ip_mib = NULL;
355 		return (B_FALSE);
356 	}
357 	/* static icmp info */
358 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
359 	    sizeof (mib2_ipv6IfIcmpEntry_t);
360 	/*
361 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
362 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
363 	 * -> ill_phyint_reinit
364 	 */
365 	return (B_TRUE);
366 }
367 
368 /*
369  * Completely vaporize a lower level tap and all associated interfaces.
370  * ill_delete is called only out of ip_close when the device control
371  * stream is being closed.
372  */
373 void
374 ill_delete(ill_t *ill)
375 {
376 	ipif_t	*ipif;
377 	ill_t	*prev_ill;
378 	ip_stack_t	*ipst = ill->ill_ipst;
379 
380 	/*
381 	 * ill_delete may be forcibly entering the ipsq. The previous
382 	 * ioctl may not have completed and may need to be aborted.
383 	 * ipsq_flush takes care of it. If we don't need to enter the
384 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
385 	 * ill_delete_tail is sufficient.
386 	 */
387 	ipsq_flush(ill);
388 
389 	/*
390 	 * Nuke all interfaces.  ipif_free will take down the interface,
391 	 * remove it from the list, and free the data structure.
392 	 * Walk down the ipif list and remove the logical interfaces
393 	 * first before removing the main ipif. We can't unplumb
394 	 * zeroth interface first in the case of IPv6 as update_conn_ill
395 	 * -> ip_ll_multireq de-references ill_ipif for checking
396 	 * POINTOPOINT.
397 	 *
398 	 * If ill_ipif was not properly initialized (i.e low on memory),
399 	 * then no interfaces to clean up. In this case just clean up the
400 	 * ill.
401 	 */
402 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
403 		ipif_free(ipif);
404 
405 	/*
406 	 * clean out all the nce_t entries that depend on this
407 	 * ill for the ill_phys_addr.
408 	 */
409 	nce_flush(ill, B_TRUE);
410 
411 	/* Clean up msgs on pending upcalls for mrouted */
412 	reset_mrt_ill(ill);
413 
414 	update_conn_ill(ill, ipst);
415 
416 	/*
417 	 * Remove multicast references added as a result of calls to
418 	 * ip_join_allmulti().
419 	 */
420 	ip_purge_allmulti(ill);
421 
422 	/*
423 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
424 	 */
425 	if (IS_UNDER_IPMP(ill))
426 		ipmp_ill_leave_illgrp(ill);
427 
428 	/*
429 	 * ill_down will arrange to blow off any IRE's dependent on this
430 	 * ILL, and shut down fragmentation reassembly.
431 	 */
432 	ill_down(ill);
433 
434 	/* Let SCTP know, so that it can remove this from its list. */
435 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
436 
437 	/*
438 	 * Walk all CONNs that can have a reference on an ire or nce for this
439 	 * ill (we actually walk all that now have stale references).
440 	 */
441 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
442 
443 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
444 	if (ill->ill_isv6)
445 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
446 
447 	/*
448 	 * If an address on this ILL is being used as a source address then
449 	 * clear out the pointers in other ILLs that point to this ILL.
450 	 */
451 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
452 	if (ill->ill_usesrc_grp_next != NULL) {
453 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
454 			ill_disband_usesrc_group(ill);
455 		} else {	/* consumer of the usesrc ILL */
456 			prev_ill = ill_prev_usesrc(ill);
457 			prev_ill->ill_usesrc_grp_next =
458 			    ill->ill_usesrc_grp_next;
459 		}
460 	}
461 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
462 }
463 
464 static void
465 ipif_non_duplicate(ipif_t *ipif)
466 {
467 	ill_t *ill = ipif->ipif_ill;
468 	mutex_enter(&ill->ill_lock);
469 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
470 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
471 		ASSERT(ill->ill_ipif_dup_count > 0);
472 		ill->ill_ipif_dup_count--;
473 	}
474 	mutex_exit(&ill->ill_lock);
475 }
476 
477 /*
478  * ill_delete_tail is called from ip_modclose after all references
479  * to the closing ill are gone. The wait is done in ip_modclose
480  */
481 void
482 ill_delete_tail(ill_t *ill)
483 {
484 	mblk_t	**mpp;
485 	ipif_t	*ipif;
486 	ip_stack_t	*ipst = ill->ill_ipst;
487 
488 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
489 		ipif_non_duplicate(ipif);
490 		(void) ipif_down_tail(ipif);
491 	}
492 
493 	ASSERT(ill->ill_ipif_dup_count == 0);
494 
495 	/*
496 	 * If polling capability is enabled (which signifies direct
497 	 * upcall into IP and driver has ill saved as a handle),
498 	 * we need to make sure that unbind has completed before we
499 	 * let the ill disappear and driver no longer has any reference
500 	 * to this ill.
501 	 */
502 	mutex_enter(&ill->ill_lock);
503 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
504 		cv_wait(&ill->ill_cv, &ill->ill_lock);
505 	mutex_exit(&ill->ill_lock);
506 	ASSERT(!(ill->ill_capabilities &
507 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
508 
509 	if (ill->ill_net_type != IRE_LOOPBACK)
510 		qprocsoff(ill->ill_rq);
511 
512 	/*
513 	 * We do an ipsq_flush once again now. New messages could have
514 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
515 	 * could also have landed up if an ioctl thread had looked up
516 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
517 	 * enqueued the ioctl when we did the ipsq_flush last time.
518 	 */
519 	ipsq_flush(ill);
520 
521 	/*
522 	 * Free capabilities.
523 	 */
524 	if (ill->ill_hcksum_capab != NULL) {
525 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
526 		ill->ill_hcksum_capab = NULL;
527 	}
528 
529 	if (ill->ill_zerocopy_capab != NULL) {
530 		kmem_free(ill->ill_zerocopy_capab,
531 		    sizeof (ill_zerocopy_capab_t));
532 		ill->ill_zerocopy_capab = NULL;
533 	}
534 
535 	if (ill->ill_lso_capab != NULL) {
536 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
537 		ill->ill_lso_capab = NULL;
538 	}
539 
540 	if (ill->ill_dld_capab != NULL) {
541 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
542 		ill->ill_dld_capab = NULL;
543 	}
544 
545 	while (ill->ill_ipif != NULL)
546 		ipif_free_tail(ill->ill_ipif);
547 
548 	/*
549 	 * We have removed all references to ilm from conn and the ones joined
550 	 * within the kernel.
551 	 *
552 	 * We don't walk conns, mrts and ires because
553 	 *
554 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
555 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
556 	 *    ill references.
557 	 */
558 
559 	/*
560 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
561 	 * is safe to do because the illgrp has already been unlinked from the
562 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
563 	 */
564 	if (IS_IPMP(ill)) {
565 		ipmp_illgrp_destroy(ill->ill_grp);
566 		ill->ill_grp = NULL;
567 	}
568 
569 	/*
570 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
571 	 * could free the phyint. No more reference to the phyint after this
572 	 * point.
573 	 */
574 	(void) ill_glist_delete(ill);
575 
576 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
577 	if (ill->ill_ndd_name != NULL)
578 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
579 	rw_exit(&ipst->ips_ip_g_nd_lock);
580 
581 	if (ill->ill_frag_ptr != NULL) {
582 		uint_t count;
583 
584 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
585 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
586 		}
587 		mi_free(ill->ill_frag_ptr);
588 		ill->ill_frag_ptr = NULL;
589 		ill->ill_frag_hash_tbl = NULL;
590 	}
591 
592 	freemsg(ill->ill_nd_lla_mp);
593 	/* Free all retained control messages. */
594 	mpp = &ill->ill_first_mp_to_free;
595 	do {
596 		while (mpp[0]) {
597 			mblk_t  *mp;
598 			mblk_t  *mp1;
599 
600 			mp = mpp[0];
601 			mpp[0] = mp->b_next;
602 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
603 				mp1->b_next = NULL;
604 				mp1->b_prev = NULL;
605 			}
606 			freemsg(mp);
607 		}
608 	} while (mpp++ != &ill->ill_last_mp_to_free);
609 
610 	ill_free_mib(ill);
611 
612 #ifdef DEBUG
613 	ill_trace_cleanup(ill);
614 #endif
615 
616 	/* The default multicast interface might have changed */
617 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
618 
619 	/* Drop refcnt here */
620 	netstack_rele(ill->ill_ipst->ips_netstack);
621 	ill->ill_ipst = NULL;
622 }
623 
624 static void
625 ill_free_mib(ill_t *ill)
626 {
627 	ip_stack_t *ipst = ill->ill_ipst;
628 
629 	/*
630 	 * MIB statistics must not be lost, so when an interface
631 	 * goes away the counter values will be added to the global
632 	 * MIBs.
633 	 */
634 	if (ill->ill_ip_mib != NULL) {
635 		if (ill->ill_isv6) {
636 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
637 			    ill->ill_ip_mib);
638 		} else {
639 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
640 			    ill->ill_ip_mib);
641 		}
642 
643 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
644 		ill->ill_ip_mib = NULL;
645 	}
646 	if (ill->ill_icmp6_mib != NULL) {
647 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
648 		    ill->ill_icmp6_mib);
649 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
650 		ill->ill_icmp6_mib = NULL;
651 	}
652 }
653 
654 /*
655  * Concatenate together a physical address and a sap.
656  *
657  * Sap_lengths are interpreted as follows:
658  *   sap_length == 0	==>	no sap
659  *   sap_length > 0	==>	sap is at the head of the dlpi address
660  *   sap_length < 0	==>	sap is at the tail of the dlpi address
661  */
662 static void
663 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
664     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
665 {
666 	uint16_t sap_addr = (uint16_t)sap_src;
667 
668 	if (sap_length == 0) {
669 		if (phys_src == NULL)
670 			bzero(dst, phys_length);
671 		else
672 			bcopy(phys_src, dst, phys_length);
673 	} else if (sap_length < 0) {
674 		if (phys_src == NULL)
675 			bzero(dst, phys_length);
676 		else
677 			bcopy(phys_src, dst, phys_length);
678 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
679 	} else {
680 		bcopy(&sap_addr, dst, sizeof (sap_addr));
681 		if (phys_src == NULL)
682 			bzero((char *)dst + sap_length, phys_length);
683 		else
684 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
685 	}
686 }
687 
688 /*
689  * Generate a dl_unitdata_req mblk for the device and address given.
690  * addr_length is the length of the physical portion of the address.
691  * If addr is NULL include an all zero address of the specified length.
692  * TRUE? In any case, addr_length is taken to be the entire length of the
693  * dlpi address, including the absolute value of sap_length.
694  */
695 mblk_t *
696 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
697 		t_scalar_t sap_length)
698 {
699 	dl_unitdata_req_t *dlur;
700 	mblk_t	*mp;
701 	t_scalar_t	abs_sap_length;		/* absolute value */
702 
703 	abs_sap_length = ABS(sap_length);
704 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
705 	    DL_UNITDATA_REQ);
706 	if (mp == NULL)
707 		return (NULL);
708 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
709 	/* HACK: accomodate incompatible DLPI drivers */
710 	if (addr_length == 8)
711 		addr_length = 6;
712 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
713 	dlur->dl_dest_addr_offset = sizeof (*dlur);
714 	dlur->dl_priority.dl_min = 0;
715 	dlur->dl_priority.dl_max = 0;
716 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
717 	    (uchar_t *)&dlur[1]);
718 	return (mp);
719 }
720 
721 /*
722  * Add the pending mp to the list. There can be only 1 pending mp
723  * in the list. Any exclusive ioctl that needs to wait for a response
724  * from another module or driver needs to use this function to set
725  * the ipx_pending_mp to the ioctl mblk and wait for the response from
726  * the other module/driver. This is also used while waiting for the
727  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
728  */
729 boolean_t
730 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
731     int waitfor)
732 {
733 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
734 
735 	ASSERT(IAM_WRITER_IPIF(ipif));
736 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
737 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
738 	ASSERT(ipx->ipx_pending_mp == NULL);
739 	/*
740 	 * The caller may be using a different ipif than the one passed into
741 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
742 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
743 	 * that `ipx_current_ipif == ipif'.
744 	 */
745 	ASSERT(ipx->ipx_current_ipif != NULL);
746 
747 	/*
748 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
749 	 * driver.
750 	 */
751 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
752 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
753 	    (DB_TYPE(add_mp) == M_PCPROTO));
754 
755 	if (connp != NULL) {
756 		ASSERT(MUTEX_HELD(&connp->conn_lock));
757 		/*
758 		 * Return error if the conn has started closing. The conn
759 		 * could have finished cleaning up the pending mp list,
760 		 * If so we should not add another mp to the list negating
761 		 * the cleanup.
762 		 */
763 		if (connp->conn_state_flags & CONN_CLOSING)
764 			return (B_FALSE);
765 	}
766 	mutex_enter(&ipx->ipx_lock);
767 	ipx->ipx_pending_ipif = ipif;
768 	/*
769 	 * Note down the queue in b_queue. This will be returned by
770 	 * ipsq_pending_mp_get. Caller will then use these values to restart
771 	 * the processing
772 	 */
773 	add_mp->b_next = NULL;
774 	add_mp->b_queue = q;
775 	ipx->ipx_pending_mp = add_mp;
776 	ipx->ipx_waitfor = waitfor;
777 	mutex_exit(&ipx->ipx_lock);
778 
779 	if (connp != NULL)
780 		connp->conn_oper_pending_ill = ipif->ipif_ill;
781 
782 	return (B_TRUE);
783 }
784 
785 /*
786  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
787  * queued in the list.
788  */
789 mblk_t *
790 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
791 {
792 	mblk_t	*curr = NULL;
793 	ipxop_t	*ipx = ipsq->ipsq_xop;
794 
795 	*connpp = NULL;
796 	mutex_enter(&ipx->ipx_lock);
797 	if (ipx->ipx_pending_mp == NULL) {
798 		mutex_exit(&ipx->ipx_lock);
799 		return (NULL);
800 	}
801 
802 	/* There can be only 1 such excl message */
803 	curr = ipx->ipx_pending_mp;
804 	ASSERT(curr->b_next == NULL);
805 	ipx->ipx_pending_ipif = NULL;
806 	ipx->ipx_pending_mp = NULL;
807 	ipx->ipx_waitfor = 0;
808 	mutex_exit(&ipx->ipx_lock);
809 
810 	if (CONN_Q(curr->b_queue)) {
811 		/*
812 		 * This mp did a refhold on the conn, at the start of the ioctl.
813 		 * So we can safely return a pointer to the conn to the caller.
814 		 */
815 		*connpp = Q_TO_CONN(curr->b_queue);
816 	} else {
817 		*connpp = NULL;
818 	}
819 	curr->b_next = NULL;
820 	curr->b_prev = NULL;
821 	return (curr);
822 }
823 
824 /*
825  * Cleanup the ioctl mp queued in ipx_pending_mp
826  * - Called in the ill_delete path
827  * - Called in the M_ERROR or M_HANGUP path on the ill.
828  * - Called in the conn close path.
829  *
830  * Returns success on finding the pending mblk associated with the ioctl or
831  * exclusive operation in progress, failure otherwise.
832  */
833 boolean_t
834 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
835 {
836 	mblk_t	*mp;
837 	ipxop_t	*ipx;
838 	queue_t	*q;
839 	ipif_t	*ipif;
840 	int	cmd;
841 
842 	ASSERT(IAM_WRITER_ILL(ill));
843 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
844 
845 	mutex_enter(&ipx->ipx_lock);
846 	mp = ipx->ipx_pending_mp;
847 	if (connp != NULL) {
848 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
849 			/*
850 			 * Nothing to clean since the conn that is closing
851 			 * does not have a matching pending mblk in
852 			 * ipx_pending_mp.
853 			 */
854 			mutex_exit(&ipx->ipx_lock);
855 			return (B_FALSE);
856 		}
857 	} else {
858 		/*
859 		 * A non-zero ill_error signifies we are called in the
860 		 * M_ERROR or M_HANGUP path and we need to unconditionally
861 		 * abort any current ioctl and do the corresponding cleanup.
862 		 * A zero ill_error means we are in the ill_delete path and
863 		 * we do the cleanup only if there is a pending mp.
864 		 */
865 		if (mp == NULL && ill->ill_error == 0) {
866 			mutex_exit(&ipx->ipx_lock);
867 			return (B_FALSE);
868 		}
869 	}
870 
871 	/* Now remove from the ipx_pending_mp */
872 	ipx->ipx_pending_mp = NULL;
873 	ipif = ipx->ipx_pending_ipif;
874 	ipx->ipx_pending_ipif = NULL;
875 	ipx->ipx_waitfor = 0;
876 	ipx->ipx_current_ipif = NULL;
877 	cmd = ipx->ipx_current_ioctl;
878 	ipx->ipx_current_ioctl = 0;
879 	ipx->ipx_current_done = B_TRUE;
880 	mutex_exit(&ipx->ipx_lock);
881 
882 	if (mp == NULL)
883 		return (B_FALSE);
884 
885 	q = mp->b_queue;
886 	mp->b_next = NULL;
887 	mp->b_prev = NULL;
888 	mp->b_queue = NULL;
889 
890 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
891 		DTRACE_PROBE4(ipif__ioctl,
892 		    char *, "ipsq_pending_mp_cleanup",
893 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
894 		    ipif_t *, ipif);
895 		if (connp == NULL) {
896 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
897 		} else {
898 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
899 			mutex_enter(&ipif->ipif_ill->ill_lock);
900 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
901 			mutex_exit(&ipif->ipif_ill->ill_lock);
902 		}
903 	} else {
904 		inet_freemsg(mp);
905 	}
906 	return (B_TRUE);
907 }
908 
909 /*
910  * Called in the conn close path and ill delete path
911  */
912 static void
913 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
914 {
915 	ipsq_t	*ipsq;
916 	mblk_t	*prev;
917 	mblk_t	*curr;
918 	mblk_t	*next;
919 	queue_t	*wq, *rq = NULL;
920 	mblk_t	*tmp_list = NULL;
921 
922 	ASSERT(IAM_WRITER_ILL(ill));
923 	if (connp != NULL)
924 		wq = CONNP_TO_WQ(connp);
925 	else
926 		wq = ill->ill_wq;
927 
928 	/*
929 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
930 	 * against this here.
931 	 */
932 	if (wq != NULL)
933 		rq = RD(wq);
934 
935 	ipsq = ill->ill_phyint->phyint_ipsq;
936 	/*
937 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
938 	 * In the case of ioctl from a conn, there can be only 1 mp
939 	 * queued on the ipsq. If an ill is being unplumbed flush all
940 	 * the messages.
941 	 */
942 	mutex_enter(&ipsq->ipsq_lock);
943 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
944 	    curr = next) {
945 		next = curr->b_next;
946 		if (connp == NULL ||
947 		    (curr->b_queue == wq || curr->b_queue == rq)) {
948 			/* Unlink the mblk from the pending mp list */
949 			if (prev != NULL) {
950 				prev->b_next = curr->b_next;
951 			} else {
952 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
953 				ipsq->ipsq_xopq_mphead = curr->b_next;
954 			}
955 			if (ipsq->ipsq_xopq_mptail == curr)
956 				ipsq->ipsq_xopq_mptail = prev;
957 			/*
958 			 * Create a temporary list and release the ipsq lock
959 			 * New elements are added to the head of the tmp_list
960 			 */
961 			curr->b_next = tmp_list;
962 			tmp_list = curr;
963 		} else {
964 			prev = curr;
965 		}
966 	}
967 	mutex_exit(&ipsq->ipsq_lock);
968 
969 	while (tmp_list != NULL) {
970 		curr = tmp_list;
971 		tmp_list = curr->b_next;
972 		curr->b_next = NULL;
973 		curr->b_prev = NULL;
974 		wq = curr->b_queue;
975 		curr->b_queue = NULL;
976 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
977 			DTRACE_PROBE4(ipif__ioctl,
978 			    char *, "ipsq_xopq_mp_cleanup",
979 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
980 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
981 			    CONN_CLOSE : NO_COPYOUT, NULL);
982 		} else {
983 			/*
984 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
985 			 * this can't be just inet_freemsg. we have to
986 			 * restart it otherwise the thread will be stuck.
987 			 */
988 			inet_freemsg(curr);
989 		}
990 	}
991 }
992 
993 /*
994  * This conn has started closing. Cleanup any pending ioctl from this conn.
995  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
996  */
997 void
998 conn_ioctl_cleanup(conn_t *connp)
999 {
1000 	ipsq_t	*ipsq;
1001 	ill_t	*ill;
1002 	boolean_t refheld;
1003 
1004 	/*
1005 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1006 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1007 	 * started the mp could be present in ipx_pending_mp. Note that if
1008 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1009 	 * not yet queued anywhere. In this case, the conn close code will wait
1010 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1011 	 * tcp_close will wait first until all ioctls have completed for this
1012 	 * conn.
1013 	 */
1014 	mutex_enter(&connp->conn_lock);
1015 	ill = connp->conn_oper_pending_ill;
1016 	if (ill == NULL) {
1017 		mutex_exit(&connp->conn_lock);
1018 		return;
1019 	}
1020 
1021 	/*
1022 	 * We may not be able to refhold the ill if the ill/ipif
1023 	 * is changing. But we need to make sure that the ill will
1024 	 * not vanish. So we just bump up the ill_waiter count.
1025 	 */
1026 	refheld = ill_waiter_inc(ill);
1027 	mutex_exit(&connp->conn_lock);
1028 	if (refheld) {
1029 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1030 			ill_waiter_dcr(ill);
1031 			/*
1032 			 * Check whether this ioctl has started and is
1033 			 * pending. If it is not found there then check
1034 			 * whether this ioctl has not even started and is in
1035 			 * the ipsq_xopq list.
1036 			 */
1037 			if (!ipsq_pending_mp_cleanup(ill, connp))
1038 				ipsq_xopq_mp_cleanup(ill, connp);
1039 			ipsq = ill->ill_phyint->phyint_ipsq;
1040 			ipsq_exit(ipsq);
1041 			return;
1042 		}
1043 	}
1044 
1045 	/*
1046 	 * The ill is also closing and we could not bump up the
1047 	 * ill_waiter_count or we could not enter the ipsq. Leave
1048 	 * the cleanup to ill_delete
1049 	 */
1050 	mutex_enter(&connp->conn_lock);
1051 	while (connp->conn_oper_pending_ill != NULL)
1052 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1053 	mutex_exit(&connp->conn_lock);
1054 	if (refheld)
1055 		ill_waiter_dcr(ill);
1056 }
1057 
1058 /*
1059  * ipcl_walk function for cleaning up conn_*_ill fields.
1060  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1061  * conn_bound_if in place. We prefer dropping
1062  * packets instead of sending them out the wrong interface, or accepting
1063  * packets from the wrong ifindex.
1064  */
1065 static void
1066 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1067 {
1068 	ill_t	*ill = (ill_t *)arg;
1069 
1070 	mutex_enter(&connp->conn_lock);
1071 	if (connp->conn_dhcpinit_ill == ill) {
1072 		connp->conn_dhcpinit_ill = NULL;
1073 		ASSERT(ill->ill_dhcpinit != 0);
1074 		atomic_dec_32(&ill->ill_dhcpinit);
1075 		ill_set_inputfn(ill);
1076 	}
1077 	mutex_exit(&connp->conn_lock);
1078 }
1079 
1080 static int
1081 ill_down_ipifs_tail(ill_t *ill)
1082 {
1083 	ipif_t	*ipif;
1084 	int err;
1085 
1086 	ASSERT(IAM_WRITER_ILL(ill));
1087 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1088 		ipif_non_duplicate(ipif);
1089 		/*
1090 		 * ipif_down_tail will call arp_ll_down on the last ipif
1091 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1092 		 */
1093 		if ((err = ipif_down_tail(ipif)) != 0)
1094 			return (err);
1095 	}
1096 	return (0);
1097 }
1098 
1099 /* ARGSUSED */
1100 void
1101 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1102 {
1103 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1104 	(void) ill_down_ipifs_tail(q->q_ptr);
1105 	freemsg(mp);
1106 	ipsq_current_finish(ipsq);
1107 }
1108 
1109 /*
1110  * ill_down_start is called when we want to down this ill and bring it up again
1111  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1112  * all interfaces, but don't tear down any plumbing.
1113  */
1114 boolean_t
1115 ill_down_start(queue_t *q, mblk_t *mp)
1116 {
1117 	ill_t	*ill = q->q_ptr;
1118 	ipif_t	*ipif;
1119 
1120 	ASSERT(IAM_WRITER_ILL(ill));
1121 	mutex_enter(&ill->ill_lock);
1122 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1123 	/* no more nce addition allowed */
1124 	mutex_exit(&ill->ill_lock);
1125 
1126 	/*
1127 	 * It is possible that some ioctl is already in progress while we
1128 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1129 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1130 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1131 	 * the in progress ioctl from ever completing.
1132 	 *
1133 	 * The thread that started the ioctl (if any) must have returned,
1134 	 * since we are now executing as writer. After the 2 calls below,
1135 	 * the state of the ipsq and the ill would reflect no trace of any
1136 	 * pending operation. Subsequently if there is any response to the
1137 	 * original ioctl from the driver, it would be discarded as an
1138 	 * unsolicited message from the driver.
1139 	 */
1140 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1141 	ill_dlpi_clear_deferred(ill);
1142 
1143 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1144 		(void) ipif_down(ipif, NULL, NULL);
1145 
1146 	ill_down(ill);
1147 
1148 	/*
1149 	 * Walk all CONNs that can have a reference on an ire or nce for this
1150 	 * ill (we actually walk all that now have stale references).
1151 	 */
1152 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1153 
1154 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1155 	if (ill->ill_isv6)
1156 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1157 
1158 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1159 
1160 	/*
1161 	 * Atomically test and add the pending mp if references are active.
1162 	 */
1163 	mutex_enter(&ill->ill_lock);
1164 	if (!ill_is_quiescent(ill)) {
1165 		/* call cannot fail since `conn_t *' argument is NULL */
1166 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1167 		    mp, ILL_DOWN);
1168 		mutex_exit(&ill->ill_lock);
1169 		return (B_FALSE);
1170 	}
1171 	mutex_exit(&ill->ill_lock);
1172 	return (B_TRUE);
1173 }
1174 
1175 static void
1176 ill_down(ill_t *ill)
1177 {
1178 	mblk_t	*mp;
1179 	ip_stack_t	*ipst = ill->ill_ipst;
1180 
1181 	/*
1182 	 * Blow off any IREs dependent on this ILL.
1183 	 * The caller needs to handle conn_ixa_cleanup
1184 	 */
1185 	ill_delete_ires(ill);
1186 
1187 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1188 
1189 	/* Remove any conn_*_ill depending on this ill */
1190 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1191 
1192 	/*
1193 	 * Free state for additional IREs.
1194 	 */
1195 	mutex_enter(&ill->ill_saved_ire_lock);
1196 	mp = ill->ill_saved_ire_mp;
1197 	ill->ill_saved_ire_mp = NULL;
1198 	ill->ill_saved_ire_cnt = 0;
1199 	mutex_exit(&ill->ill_saved_ire_lock);
1200 	freemsg(mp);
1201 }
1202 
1203 /*
1204  * ire_walk routine used to delete every IRE that depends on
1205  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1206  *
1207  * Note: since the routes added by the kernel are deleted separately,
1208  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1209  *
1210  * We also remove references on ire_nce_cache entries that refer to the ill.
1211  */
1212 void
1213 ill_downi(ire_t *ire, char *ill_arg)
1214 {
1215 	ill_t	*ill = (ill_t *)ill_arg;
1216 	nce_t	*nce;
1217 
1218 	mutex_enter(&ire->ire_lock);
1219 	nce = ire->ire_nce_cache;
1220 	if (nce != NULL && nce->nce_ill == ill)
1221 		ire->ire_nce_cache = NULL;
1222 	else
1223 		nce = NULL;
1224 	mutex_exit(&ire->ire_lock);
1225 	if (nce != NULL)
1226 		nce_refrele(nce);
1227 	if (ire->ire_ill == ill) {
1228 		/*
1229 		 * The existing interface binding for ire must be
1230 		 * deleted before trying to bind the route to another
1231 		 * interface. However, since we are using the contents of the
1232 		 * ire after ire_delete, the caller has to ensure that
1233 		 * CONDEMNED (deleted) ire's are not removed from the list
1234 		 * when ire_delete() returns. Currently ill_downi() is
1235 		 * only called as part of ire_walk*() routines, so that
1236 		 * the irb_refhold() done by ire_walk*() will ensure that
1237 		 * ire_delete() does not lead to ire_inactive().
1238 		 */
1239 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1240 		ire_delete(ire);
1241 		if (ire->ire_unbound)
1242 			ire_rebind(ire);
1243 	}
1244 }
1245 
1246 /* Remove IRE_IF_CLONE on this ill */
1247 void
1248 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1249 {
1250 	ill_t	*ill = (ill_t *)ill_arg;
1251 
1252 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1253 	if (ire->ire_ill == ill)
1254 		ire_delete(ire);
1255 }
1256 
1257 /* Consume an M_IOCACK of the fastpath probe. */
1258 void
1259 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1260 {
1261 	mblk_t	*mp1 = mp;
1262 
1263 	/*
1264 	 * If this was the first attempt turn on the fastpath probing.
1265 	 */
1266 	mutex_enter(&ill->ill_lock);
1267 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1268 		ill->ill_dlpi_fastpath_state = IDS_OK;
1269 	mutex_exit(&ill->ill_lock);
1270 
1271 	/* Free the M_IOCACK mblk, hold on to the data */
1272 	mp = mp->b_cont;
1273 	freeb(mp1);
1274 	if (mp == NULL)
1275 		return;
1276 	if (mp->b_cont != NULL)
1277 		nce_fastpath_update(ill, mp);
1278 	else
1279 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1280 	freemsg(mp);
1281 }
1282 
1283 /*
1284  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1285  * The data portion of the request is a dl_unitdata_req_t template for
1286  * what we would send downstream in the absence of a fastpath confirmation.
1287  */
1288 int
1289 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1290 {
1291 	struct iocblk	*ioc;
1292 	mblk_t	*mp;
1293 
1294 	if (dlur_mp == NULL)
1295 		return (EINVAL);
1296 
1297 	mutex_enter(&ill->ill_lock);
1298 	switch (ill->ill_dlpi_fastpath_state) {
1299 	case IDS_FAILED:
1300 		/*
1301 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1302 		 * support it.
1303 		 */
1304 		mutex_exit(&ill->ill_lock);
1305 		return (ENOTSUP);
1306 	case IDS_UNKNOWN:
1307 		/* This is the first probe */
1308 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1309 		break;
1310 	default:
1311 		break;
1312 	}
1313 	mutex_exit(&ill->ill_lock);
1314 
1315 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1316 		return (EAGAIN);
1317 
1318 	mp->b_cont = copyb(dlur_mp);
1319 	if (mp->b_cont == NULL) {
1320 		freeb(mp);
1321 		return (EAGAIN);
1322 	}
1323 
1324 	ioc = (struct iocblk *)mp->b_rptr;
1325 	ioc->ioc_count = msgdsize(mp->b_cont);
1326 
1327 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1328 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1329 	putnext(ill->ill_wq, mp);
1330 	return (0);
1331 }
1332 
1333 void
1334 ill_capability_probe(ill_t *ill)
1335 {
1336 	mblk_t	*mp;
1337 
1338 	ASSERT(IAM_WRITER_ILL(ill));
1339 
1340 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1341 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1342 		return;
1343 
1344 	/*
1345 	 * We are starting a new cycle of capability negotiation.
1346 	 * Free up the capab reset messages of any previous incarnation.
1347 	 * We will do a fresh allocation when we get the response to our probe
1348 	 */
1349 	if (ill->ill_capab_reset_mp != NULL) {
1350 		freemsg(ill->ill_capab_reset_mp);
1351 		ill->ill_capab_reset_mp = NULL;
1352 	}
1353 
1354 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1355 
1356 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1357 	if (mp == NULL)
1358 		return;
1359 
1360 	ill_capability_send(ill, mp);
1361 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1362 }
1363 
1364 void
1365 ill_capability_reset(ill_t *ill, boolean_t reneg)
1366 {
1367 	ASSERT(IAM_WRITER_ILL(ill));
1368 
1369 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1370 		return;
1371 
1372 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1373 
1374 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1375 	ill->ill_capab_reset_mp = NULL;
1376 	/*
1377 	 * We turn off all capabilities except those pertaining to
1378 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1379 	 * which will be turned off by the corresponding reset functions.
1380 	 */
1381 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1382 }
1383 
1384 static void
1385 ill_capability_reset_alloc(ill_t *ill)
1386 {
1387 	mblk_t *mp;
1388 	size_t	size = 0;
1389 	int	err;
1390 	dl_capability_req_t	*capb;
1391 
1392 	ASSERT(IAM_WRITER_ILL(ill));
1393 	ASSERT(ill->ill_capab_reset_mp == NULL);
1394 
1395 	if (ILL_HCKSUM_CAPABLE(ill)) {
1396 		size += sizeof (dl_capability_sub_t) +
1397 		    sizeof (dl_capab_hcksum_t);
1398 	}
1399 
1400 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1401 		size += sizeof (dl_capability_sub_t) +
1402 		    sizeof (dl_capab_zerocopy_t);
1403 	}
1404 
1405 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1406 		size += sizeof (dl_capability_sub_t) +
1407 		    sizeof (dl_capab_dld_t);
1408 	}
1409 
1410 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1411 	    STR_NOSIG, &err);
1412 
1413 	mp->b_datap->db_type = M_PROTO;
1414 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1415 
1416 	capb = (dl_capability_req_t *)mp->b_rptr;
1417 	capb->dl_primitive = DL_CAPABILITY_REQ;
1418 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1419 	capb->dl_sub_length = size;
1420 
1421 	mp->b_wptr += sizeof (dl_capability_req_t);
1422 
1423 	/*
1424 	 * Each handler fills in the corresponding dl_capability_sub_t
1425 	 * inside the mblk,
1426 	 */
1427 	ill_capability_hcksum_reset_fill(ill, mp);
1428 	ill_capability_zerocopy_reset_fill(ill, mp);
1429 	ill_capability_dld_reset_fill(ill, mp);
1430 
1431 	ill->ill_capab_reset_mp = mp;
1432 }
1433 
1434 static void
1435 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1436 {
1437 	dl_capab_id_t *id_ic;
1438 	uint_t sub_dl_cap = outers->dl_cap;
1439 	dl_capability_sub_t *inners;
1440 	uint8_t *capend;
1441 
1442 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1443 
1444 	/*
1445 	 * Note: range checks here are not absolutely sufficient to
1446 	 * make us robust against malformed messages sent by drivers;
1447 	 * this is in keeping with the rest of IP's dlpi handling.
1448 	 * (Remember, it's coming from something else in the kernel
1449 	 * address space)
1450 	 */
1451 
1452 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1453 	if (capend > mp->b_wptr) {
1454 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1455 		    "malformed sub-capability too long for mblk");
1456 		return;
1457 	}
1458 
1459 	id_ic = (dl_capab_id_t *)(outers + 1);
1460 
1461 	if (outers->dl_length < sizeof (*id_ic) ||
1462 	    (inners = &id_ic->id_subcap,
1463 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1464 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1465 		    "encapsulated capab type %d too long for mblk",
1466 		    inners->dl_cap);
1467 		return;
1468 	}
1469 
1470 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1471 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1472 		    "isn't as expected; pass-thru module(s) detected, "
1473 		    "discarding capability\n", inners->dl_cap));
1474 		return;
1475 	}
1476 
1477 	/* Process the encapsulated sub-capability */
1478 	ill_capability_dispatch(ill, mp, inners);
1479 }
1480 
1481 static void
1482 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1483 {
1484 	dl_capability_sub_t *dl_subcap;
1485 
1486 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1487 		return;
1488 
1489 	/*
1490 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1491 	 * initialized below since it is not used by DLD.
1492 	 */
1493 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1494 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1495 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1496 
1497 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1498 }
1499 
1500 static void
1501 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1502 {
1503 	/*
1504 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1505 	 * is only to get the VRRP capability.
1506 	 *
1507 	 * Note that we cannot check ill_ipif_up_count here since
1508 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1509 	 * That is done asynchronously, and can race with this function.
1510 	 */
1511 	if (!ill->ill_dl_up) {
1512 		if (subp->dl_cap == DL_CAPAB_VRRP)
1513 			ill_capability_vrrp_ack(ill, mp, subp);
1514 		return;
1515 	}
1516 
1517 	switch (subp->dl_cap) {
1518 	case DL_CAPAB_HCKSUM:
1519 		ill_capability_hcksum_ack(ill, mp, subp);
1520 		break;
1521 	case DL_CAPAB_ZEROCOPY:
1522 		ill_capability_zerocopy_ack(ill, mp, subp);
1523 		break;
1524 	case DL_CAPAB_DLD:
1525 		ill_capability_dld_ack(ill, mp, subp);
1526 		break;
1527 	case DL_CAPAB_VRRP:
1528 		break;
1529 	default:
1530 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1531 		    subp->dl_cap));
1532 	}
1533 }
1534 
1535 /*
1536  * Process the vrrp capability received from a DLS Provider. isub must point
1537  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1538  */
1539 static void
1540 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1541 {
1542 	dl_capab_vrrp_t	*vrrp;
1543 	uint_t		sub_dl_cap = isub->dl_cap;
1544 	uint8_t		*capend;
1545 
1546 	ASSERT(IAM_WRITER_ILL(ill));
1547 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1548 
1549 	/*
1550 	 * Note: range checks here are not absolutely sufficient to
1551 	 * make us robust against malformed messages sent by drivers;
1552 	 * this is in keeping with the rest of IP's dlpi handling.
1553 	 * (Remember, it's coming from something else in the kernel
1554 	 * address space)
1555 	 */
1556 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1557 	if (capend > mp->b_wptr) {
1558 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1559 		    "malformed sub-capability too long for mblk");
1560 		return;
1561 	}
1562 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1563 
1564 	/*
1565 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1566 	 */
1567 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1568 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1569 		ill->ill_flags |= ILLF_VRRP;
1570 	}
1571 }
1572 
1573 /*
1574  * Process a hardware checksum offload capability negotiation ack received
1575  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1576  * of a DL_CAPABILITY_ACK message.
1577  */
1578 static void
1579 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1580 {
1581 	dl_capability_req_t	*ocap;
1582 	dl_capab_hcksum_t	*ihck, *ohck;
1583 	ill_hcksum_capab_t	**ill_hcksum;
1584 	mblk_t			*nmp = NULL;
1585 	uint_t			sub_dl_cap = isub->dl_cap;
1586 	uint8_t			*capend;
1587 
1588 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1589 
1590 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1591 
1592 	/*
1593 	 * Note: range checks here are not absolutely sufficient to
1594 	 * make us robust against malformed messages sent by drivers;
1595 	 * this is in keeping with the rest of IP's dlpi handling.
1596 	 * (Remember, it's coming from something else in the kernel
1597 	 * address space)
1598 	 */
1599 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1600 	if (capend > mp->b_wptr) {
1601 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1602 		    "malformed sub-capability too long for mblk");
1603 		return;
1604 	}
1605 
1606 	/*
1607 	 * There are two types of acks we process here:
1608 	 * 1. acks in reply to a (first form) generic capability req
1609 	 *    (no ENABLE flag set)
1610 	 * 2. acks in reply to a ENABLE capability req.
1611 	 *    (ENABLE flag set)
1612 	 */
1613 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1614 
1615 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1616 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1617 		    "unsupported hardware checksum "
1618 		    "sub-capability (version %d, expected %d)",
1619 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1620 		return;
1621 	}
1622 
1623 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1624 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1625 		    "checksum capability isn't as expected; pass-thru "
1626 		    "module(s) detected, discarding capability\n"));
1627 		return;
1628 	}
1629 
1630 #define	CURR_HCKSUM_CAPAB				\
1631 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1632 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1633 
1634 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1635 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1636 		/* do ENABLE processing */
1637 		if (*ill_hcksum == NULL) {
1638 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1639 			    KM_NOSLEEP);
1640 
1641 			if (*ill_hcksum == NULL) {
1642 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1643 				    "could not enable hcksum version %d "
1644 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1645 				    ill->ill_name);
1646 				return;
1647 			}
1648 		}
1649 
1650 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1651 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1652 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1653 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1654 		    "has enabled hardware checksumming\n ",
1655 		    ill->ill_name));
1656 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1657 		/*
1658 		 * Enabling hardware checksum offload
1659 		 * Currently IP supports {TCP,UDP}/IPv4
1660 		 * partial and full cksum offload and
1661 		 * IPv4 header checksum offload.
1662 		 * Allocate new mblk which will
1663 		 * contain a new capability request
1664 		 * to enable hardware checksum offload.
1665 		 */
1666 		uint_t	size;
1667 		uchar_t	*rptr;
1668 
1669 		size = sizeof (dl_capability_req_t) +
1670 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1671 
1672 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1673 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1674 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1675 			    ill->ill_name);
1676 			return;
1677 		}
1678 
1679 		rptr = nmp->b_rptr;
1680 		/* initialize dl_capability_req_t */
1681 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1682 		ocap->dl_sub_offset =
1683 		    sizeof (dl_capability_req_t);
1684 		ocap->dl_sub_length =
1685 		    sizeof (dl_capability_sub_t) +
1686 		    isub->dl_length;
1687 		nmp->b_rptr += sizeof (dl_capability_req_t);
1688 
1689 		/* initialize dl_capability_sub_t */
1690 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1691 		nmp->b_rptr += sizeof (*isub);
1692 
1693 		/* initialize dl_capab_hcksum_t */
1694 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1695 		bcopy(ihck, ohck, sizeof (*ihck));
1696 
1697 		nmp->b_rptr = rptr;
1698 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1699 
1700 		/* Set ENABLE flag */
1701 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1702 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1703 
1704 		/*
1705 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1706 		 * hardware checksum acceleration.
1707 		 */
1708 		ill_capability_send(ill, nmp);
1709 	} else {
1710 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1711 		    "advertised %x hardware checksum capability flags\n",
1712 		    ill->ill_name, ihck->hcksum_txflags));
1713 	}
1714 }
1715 
1716 static void
1717 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1718 {
1719 	dl_capab_hcksum_t *hck_subcap;
1720 	dl_capability_sub_t *dl_subcap;
1721 
1722 	if (!ILL_HCKSUM_CAPABLE(ill))
1723 		return;
1724 
1725 	ASSERT(ill->ill_hcksum_capab != NULL);
1726 
1727 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1728 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1729 	dl_subcap->dl_length = sizeof (*hck_subcap);
1730 
1731 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1732 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1733 	hck_subcap->hcksum_txflags = 0;
1734 
1735 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1736 }
1737 
1738 static void
1739 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1740 {
1741 	mblk_t *nmp = NULL;
1742 	dl_capability_req_t *oc;
1743 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1744 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1745 	uint_t sub_dl_cap = isub->dl_cap;
1746 	uint8_t *capend;
1747 
1748 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1749 
1750 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1751 
1752 	/*
1753 	 * Note: range checks here are not absolutely sufficient to
1754 	 * make us robust against malformed messages sent by drivers;
1755 	 * this is in keeping with the rest of IP's dlpi handling.
1756 	 * (Remember, it's coming from something else in the kernel
1757 	 * address space)
1758 	 */
1759 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1760 	if (capend > mp->b_wptr) {
1761 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1762 		    "malformed sub-capability too long for mblk");
1763 		return;
1764 	}
1765 
1766 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1767 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1768 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1769 		    "unsupported ZEROCOPY sub-capability (version %d, "
1770 		    "expected %d)", zc_ic->zerocopy_version,
1771 		    ZEROCOPY_VERSION_1);
1772 		return;
1773 	}
1774 
1775 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1776 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1777 		    "capability isn't as expected; pass-thru module(s) "
1778 		    "detected, discarding capability\n"));
1779 		return;
1780 	}
1781 
1782 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1783 		if (*ill_zerocopy_capab == NULL) {
1784 			*ill_zerocopy_capab =
1785 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1786 			    KM_NOSLEEP);
1787 
1788 			if (*ill_zerocopy_capab == NULL) {
1789 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1790 				    "could not enable Zero-copy version %d "
1791 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1792 				    ill->ill_name);
1793 				return;
1794 			}
1795 		}
1796 
1797 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1798 		    "supports Zero-copy version %d\n", ill->ill_name,
1799 		    ZEROCOPY_VERSION_1));
1800 
1801 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1802 		    zc_ic->zerocopy_version;
1803 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1804 		    zc_ic->zerocopy_flags;
1805 
1806 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1807 	} else {
1808 		uint_t size;
1809 		uchar_t *rptr;
1810 
1811 		size = sizeof (dl_capability_req_t) +
1812 		    sizeof (dl_capability_sub_t) +
1813 		    sizeof (dl_capab_zerocopy_t);
1814 
1815 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1816 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1817 			    "could not enable zerocopy for %s (ENOMEM)\n",
1818 			    ill->ill_name);
1819 			return;
1820 		}
1821 
1822 		rptr = nmp->b_rptr;
1823 		/* initialize dl_capability_req_t */
1824 		oc = (dl_capability_req_t *)rptr;
1825 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1826 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1827 		    sizeof (dl_capab_zerocopy_t);
1828 		rptr += sizeof (dl_capability_req_t);
1829 
1830 		/* initialize dl_capability_sub_t */
1831 		bcopy(isub, rptr, sizeof (*isub));
1832 		rptr += sizeof (*isub);
1833 
1834 		/* initialize dl_capab_zerocopy_t */
1835 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1836 		*zc_oc = *zc_ic;
1837 
1838 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1839 		    "to enable zero-copy version %d\n", ill->ill_name,
1840 		    ZEROCOPY_VERSION_1));
1841 
1842 		/* set VMSAFE_MEM flag */
1843 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1844 
1845 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1846 		ill_capability_send(ill, nmp);
1847 	}
1848 }
1849 
1850 static void
1851 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1852 {
1853 	dl_capab_zerocopy_t *zerocopy_subcap;
1854 	dl_capability_sub_t *dl_subcap;
1855 
1856 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1857 		return;
1858 
1859 	ASSERT(ill->ill_zerocopy_capab != NULL);
1860 
1861 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1862 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1863 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1864 
1865 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1866 	zerocopy_subcap->zerocopy_version =
1867 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1868 	zerocopy_subcap->zerocopy_flags = 0;
1869 
1870 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1871 }
1872 
1873 /*
1874  * DLD capability
1875  * Refer to dld.h for more information regarding the purpose and usage
1876  * of this capability.
1877  */
1878 static void
1879 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1880 {
1881 	dl_capab_dld_t		*dld_ic, dld;
1882 	uint_t			sub_dl_cap = isub->dl_cap;
1883 	uint8_t			*capend;
1884 	ill_dld_capab_t		*idc;
1885 
1886 	ASSERT(IAM_WRITER_ILL(ill));
1887 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1888 
1889 	/*
1890 	 * Note: range checks here are not absolutely sufficient to
1891 	 * make us robust against malformed messages sent by drivers;
1892 	 * this is in keeping with the rest of IP's dlpi handling.
1893 	 * (Remember, it's coming from something else in the kernel
1894 	 * address space)
1895 	 */
1896 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1897 	if (capend > mp->b_wptr) {
1898 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1899 		    "malformed sub-capability too long for mblk");
1900 		return;
1901 	}
1902 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1903 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1904 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1905 		    "unsupported DLD sub-capability (version %d, "
1906 		    "expected %d)", dld_ic->dld_version,
1907 		    DLD_CURRENT_VERSION);
1908 		return;
1909 	}
1910 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1911 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1912 		    "capability isn't as expected; pass-thru module(s) "
1913 		    "detected, discarding capability\n"));
1914 		return;
1915 	}
1916 
1917 	/*
1918 	 * Copy locally to ensure alignment.
1919 	 */
1920 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1921 
1922 	if ((idc = ill->ill_dld_capab) == NULL) {
1923 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1924 		if (idc == NULL) {
1925 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1926 			    "could not enable DLD version %d "
1927 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1928 			    ill->ill_name);
1929 			return;
1930 		}
1931 		ill->ill_dld_capab = idc;
1932 	}
1933 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1934 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1935 	ip1dbg(("ill_capability_dld_ack: interface %s "
1936 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1937 
1938 	ill_capability_dld_enable(ill);
1939 }
1940 
1941 /*
1942  * Typically capability negotiation between IP and the driver happens via
1943  * DLPI message exchange. However GLD also offers a direct function call
1944  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1945  * But arbitrary function calls into IP or GLD are not permitted, since both
1946  * of them are protected by their own perimeter mechanism. The perimeter can
1947  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1948  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1949  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1950  * to enter the mac perimeter and then do the direct function calls into
1951  * GLD to enable squeue polling. The ring related callbacks from the mac into
1952  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1953  * protected by the mac perimeter.
1954  */
1955 static void
1956 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1957 {
1958 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1959 	int			err;
1960 
1961 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1962 	    DLD_ENABLE);
1963 	ASSERT(err == 0);
1964 }
1965 
1966 static void
1967 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1968 {
1969 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1970 	int			err;
1971 
1972 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1973 	    DLD_DISABLE);
1974 	ASSERT(err == 0);
1975 }
1976 
1977 boolean_t
1978 ill_mac_perim_held(ill_t *ill)
1979 {
1980 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1981 
1982 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1983 	    DLD_QUERY));
1984 }
1985 
1986 static void
1987 ill_capability_direct_enable(ill_t *ill)
1988 {
1989 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1990 	ill_dld_direct_t	*idd = &idc->idc_direct;
1991 	dld_capab_direct_t	direct;
1992 	int			rc;
1993 
1994 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1995 
1996 	bzero(&direct, sizeof (direct));
1997 	direct.di_rx_cf = (uintptr_t)ip_input;
1998 	direct.di_rx_ch = ill;
1999 
2000 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2001 	    DLD_ENABLE);
2002 	if (rc == 0) {
2003 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2004 		idd->idd_tx_dh = direct.di_tx_dh;
2005 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2006 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2007 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2008 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2009 		ASSERT(idd->idd_tx_cb_df != NULL);
2010 		ASSERT(idd->idd_tx_fctl_df != NULL);
2011 		ASSERT(idd->idd_tx_df != NULL);
2012 		/*
2013 		 * One time registration of flow enable callback function
2014 		 */
2015 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2016 		    ill_flow_enable, ill);
2017 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2018 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2019 	} else {
2020 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2021 		    "capability, rc = %d\n", rc);
2022 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2023 	}
2024 }
2025 
2026 static void
2027 ill_capability_poll_enable(ill_t *ill)
2028 {
2029 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2030 	dld_capab_poll_t	poll;
2031 	int			rc;
2032 
2033 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2034 
2035 	bzero(&poll, sizeof (poll));
2036 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2037 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2038 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2039 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2040 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2041 	poll.poll_ring_ch = ill;
2042 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2043 	    DLD_ENABLE);
2044 	if (rc == 0) {
2045 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2046 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2047 	} else {
2048 		ip1dbg(("warning: could not enable POLL "
2049 		    "capability, rc = %d\n", rc));
2050 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2051 	}
2052 }
2053 
2054 /*
2055  * Enable the LSO capability.
2056  */
2057 static void
2058 ill_capability_lso_enable(ill_t *ill)
2059 {
2060 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2061 	dld_capab_lso_t	lso;
2062 	int rc;
2063 
2064 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2065 
2066 	if (ill->ill_lso_capab == NULL) {
2067 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2068 		    KM_NOSLEEP);
2069 		if (ill->ill_lso_capab == NULL) {
2070 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2071 			    "could not enable LSO for %s (ENOMEM)\n",
2072 			    ill->ill_name);
2073 			return;
2074 		}
2075 	}
2076 
2077 	bzero(&lso, sizeof (lso));
2078 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2079 	    DLD_ENABLE)) == 0) {
2080 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2081 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2082 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2083 		ip1dbg(("ill_capability_lso_enable: interface %s "
2084 		    "has enabled LSO\n ", ill->ill_name));
2085 	} else {
2086 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2087 		ill->ill_lso_capab = NULL;
2088 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2089 	}
2090 }
2091 
2092 static void
2093 ill_capability_dld_enable(ill_t *ill)
2094 {
2095 	mac_perim_handle_t mph;
2096 
2097 	ASSERT(IAM_WRITER_ILL(ill));
2098 
2099 	if (ill->ill_isv6)
2100 		return;
2101 
2102 	ill_mac_perim_enter(ill, &mph);
2103 	if (!ill->ill_isv6) {
2104 		ill_capability_direct_enable(ill);
2105 		ill_capability_poll_enable(ill);
2106 		ill_capability_lso_enable(ill);
2107 	}
2108 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2109 	ill_mac_perim_exit(ill, mph);
2110 }
2111 
2112 static void
2113 ill_capability_dld_disable(ill_t *ill)
2114 {
2115 	ill_dld_capab_t	*idc;
2116 	ill_dld_direct_t *idd;
2117 	mac_perim_handle_t	mph;
2118 
2119 	ASSERT(IAM_WRITER_ILL(ill));
2120 
2121 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2122 		return;
2123 
2124 	ill_mac_perim_enter(ill, &mph);
2125 
2126 	idc = ill->ill_dld_capab;
2127 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2128 		/*
2129 		 * For performance we avoid locks in the transmit data path
2130 		 * and don't maintain a count of the number of threads using
2131 		 * direct calls. Thus some threads could be using direct
2132 		 * transmit calls to GLD, even after the capability mechanism
2133 		 * turns it off. This is still safe since the handles used in
2134 		 * the direct calls continue to be valid until the unplumb is
2135 		 * completed. Remove the callback that was added (1-time) at
2136 		 * capab enable time.
2137 		 */
2138 		mutex_enter(&ill->ill_lock);
2139 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2140 		mutex_exit(&ill->ill_lock);
2141 		if (ill->ill_flownotify_mh != NULL) {
2142 			idd = &idc->idc_direct;
2143 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2144 			    ill->ill_flownotify_mh);
2145 			ill->ill_flownotify_mh = NULL;
2146 		}
2147 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2148 		    NULL, DLD_DISABLE);
2149 	}
2150 
2151 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2152 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2153 		ip_squeue_clean_all(ill);
2154 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2155 		    NULL, DLD_DISABLE);
2156 	}
2157 
2158 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2159 		ASSERT(ill->ill_lso_capab != NULL);
2160 		/*
2161 		 * Clear the capability flag for LSO but retain the
2162 		 * ill_lso_capab structure since it's possible that another
2163 		 * thread is still referring to it.  The structure only gets
2164 		 * deallocated when we destroy the ill.
2165 		 */
2166 
2167 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2168 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2169 		    NULL, DLD_DISABLE);
2170 	}
2171 
2172 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2173 	ill_mac_perim_exit(ill, mph);
2174 }
2175 
2176 /*
2177  * Capability Negotiation protocol
2178  *
2179  * We don't wait for DLPI capability operations to finish during interface
2180  * bringup or teardown. Doing so would introduce more asynchrony and the
2181  * interface up/down operations will need multiple return and restarts.
2182  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2183  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2184  * exclusive operation won't start until the DLPI operations of the previous
2185  * exclusive operation complete.
2186  *
2187  * The capability state machine is shown below.
2188  *
2189  * state		next state		event, action
2190  *
2191  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2192  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2193  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2194  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2195  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2196  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2197  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2198  *						    ill_capability_probe.
2199  */
2200 
2201 /*
2202  * Dedicated thread started from ip_stack_init that handles capability
2203  * disable. This thread ensures the taskq dispatch does not fail by waiting
2204  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2205  * that direct calls to DLD are done in a cv_waitable context.
2206  */
2207 void
2208 ill_taskq_dispatch(ip_stack_t *ipst)
2209 {
2210 	callb_cpr_t cprinfo;
2211 	char 	name[64];
2212 	mblk_t	*mp;
2213 
2214 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2215 	    ipst->ips_netstack->netstack_stackid);
2216 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2217 	    name);
2218 	mutex_enter(&ipst->ips_capab_taskq_lock);
2219 
2220 	for (;;) {
2221 		mp = ipst->ips_capab_taskq_head;
2222 		while (mp != NULL) {
2223 			ipst->ips_capab_taskq_head = mp->b_next;
2224 			if (ipst->ips_capab_taskq_head == NULL)
2225 				ipst->ips_capab_taskq_tail = NULL;
2226 			mutex_exit(&ipst->ips_capab_taskq_lock);
2227 			mp->b_next = NULL;
2228 
2229 			VERIFY(taskq_dispatch(system_taskq,
2230 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2231 			mutex_enter(&ipst->ips_capab_taskq_lock);
2232 			mp = ipst->ips_capab_taskq_head;
2233 		}
2234 
2235 		if (ipst->ips_capab_taskq_quit)
2236 			break;
2237 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2238 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2239 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2240 	}
2241 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2242 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2243 	CALLB_CPR_EXIT(&cprinfo);
2244 	thread_exit();
2245 }
2246 
2247 /*
2248  * Consume a new-style hardware capabilities negotiation ack.
2249  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2250  */
2251 static void
2252 ill_capability_ack_thr(void *arg)
2253 {
2254 	mblk_t	*mp = arg;
2255 	dl_capability_ack_t *capp;
2256 	dl_capability_sub_t *subp, *endp;
2257 	ill_t	*ill;
2258 	boolean_t reneg;
2259 
2260 	ill = (ill_t *)mp->b_prev;
2261 	mp->b_prev = NULL;
2262 
2263 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2264 
2265 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2266 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2267 		/*
2268 		 * We have received the ack for our DL_CAPAB reset request.
2269 		 * There isnt' anything in the message that needs processing.
2270 		 * All message based capabilities have been disabled, now
2271 		 * do the function call based capability disable.
2272 		 */
2273 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2274 		ill_capability_dld_disable(ill);
2275 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2276 		if (reneg)
2277 			ill_capability_probe(ill);
2278 		goto done;
2279 	}
2280 
2281 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2282 		ill->ill_dlpi_capab_state = IDCS_OK;
2283 
2284 	capp = (dl_capability_ack_t *)mp->b_rptr;
2285 
2286 	if (capp->dl_sub_length == 0) {
2287 		/* no new-style capabilities */
2288 		goto done;
2289 	}
2290 
2291 	/* make sure the driver supplied correct dl_sub_length */
2292 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2293 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2294 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2295 		goto done;
2296 	}
2297 
2298 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2299 	/*
2300 	 * There are sub-capabilities. Process the ones we know about.
2301 	 * Loop until we don't have room for another sub-cap header..
2302 	 */
2303 	for (subp = SC(capp, capp->dl_sub_offset),
2304 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2305 	    subp <= endp;
2306 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2307 
2308 		switch (subp->dl_cap) {
2309 		case DL_CAPAB_ID_WRAPPER:
2310 			ill_capability_id_ack(ill, mp, subp);
2311 			break;
2312 		default:
2313 			ill_capability_dispatch(ill, mp, subp);
2314 			break;
2315 		}
2316 	}
2317 #undef SC
2318 done:
2319 	inet_freemsg(mp);
2320 	ill_capability_done(ill);
2321 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2322 }
2323 
2324 /*
2325  * This needs to be started in a taskq thread to provide a cv_waitable
2326  * context.
2327  */
2328 void
2329 ill_capability_ack(ill_t *ill, mblk_t *mp)
2330 {
2331 	ip_stack_t	*ipst = ill->ill_ipst;
2332 
2333 	mp->b_prev = (mblk_t *)ill;
2334 	ASSERT(mp->b_next == NULL);
2335 
2336 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2337 	    TQ_NOSLEEP) != 0)
2338 		return;
2339 
2340 	/*
2341 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2342 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2343 	 */
2344 	mutex_enter(&ipst->ips_capab_taskq_lock);
2345 	if (ipst->ips_capab_taskq_head == NULL) {
2346 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2347 		ipst->ips_capab_taskq_head = mp;
2348 	} else {
2349 		ipst->ips_capab_taskq_tail->b_next = mp;
2350 	}
2351 	ipst->ips_capab_taskq_tail = mp;
2352 
2353 	cv_signal(&ipst->ips_capab_taskq_cv);
2354 	mutex_exit(&ipst->ips_capab_taskq_lock);
2355 }
2356 
2357 /*
2358  * This routine is called to scan the fragmentation reassembly table for
2359  * the specified ILL for any packets that are starting to smell.
2360  * dead_interval is the maximum time in seconds that will be tolerated.  It
2361  * will either be the value specified in ip_g_frag_timeout, or zero if the
2362  * ILL is shutting down and it is time to blow everything off.
2363  *
2364  * It returns the number of seconds (as a time_t) that the next frag timer
2365  * should be scheduled for, 0 meaning that the timer doesn't need to be
2366  * re-started.  Note that the method of calculating next_timeout isn't
2367  * entirely accurate since time will flow between the time we grab
2368  * current_time and the time we schedule the next timeout.  This isn't a
2369  * big problem since this is the timer for sending an ICMP reassembly time
2370  * exceeded messages, and it doesn't have to be exactly accurate.
2371  *
2372  * This function is
2373  * sometimes called as writer, although this is not required.
2374  */
2375 time_t
2376 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2377 {
2378 	ipfb_t	*ipfb;
2379 	ipfb_t	*endp;
2380 	ipf_t	*ipf;
2381 	ipf_t	*ipfnext;
2382 	mblk_t	*mp;
2383 	time_t	current_time = gethrestime_sec();
2384 	time_t	next_timeout = 0;
2385 	uint32_t	hdr_length;
2386 	mblk_t	*send_icmp_head;
2387 	mblk_t	*send_icmp_head_v6;
2388 	ip_stack_t *ipst = ill->ill_ipst;
2389 	ip_recv_attr_t iras;
2390 
2391 	bzero(&iras, sizeof (iras));
2392 	iras.ira_flags = 0;
2393 	iras.ira_ill = iras.ira_rill = ill;
2394 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2395 	iras.ira_rifindex = iras.ira_ruifindex;
2396 
2397 	ipfb = ill->ill_frag_hash_tbl;
2398 	if (ipfb == NULL)
2399 		return (B_FALSE);
2400 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2401 	/* Walk the frag hash table. */
2402 	for (; ipfb < endp; ipfb++) {
2403 		send_icmp_head = NULL;
2404 		send_icmp_head_v6 = NULL;
2405 		mutex_enter(&ipfb->ipfb_lock);
2406 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2407 			time_t frag_time = current_time - ipf->ipf_timestamp;
2408 			time_t frag_timeout;
2409 
2410 			if (frag_time < dead_interval) {
2411 				/*
2412 				 * There are some outstanding fragments
2413 				 * that will timeout later.  Make note of
2414 				 * the time so that we can reschedule the
2415 				 * next timeout appropriately.
2416 				 */
2417 				frag_timeout = dead_interval - frag_time;
2418 				if (next_timeout == 0 ||
2419 				    frag_timeout < next_timeout) {
2420 					next_timeout = frag_timeout;
2421 				}
2422 				break;
2423 			}
2424 			/* Time's up.  Get it out of here. */
2425 			hdr_length = ipf->ipf_nf_hdr_len;
2426 			ipfnext = ipf->ipf_hash_next;
2427 			if (ipfnext)
2428 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2429 			*ipf->ipf_ptphn = ipfnext;
2430 			mp = ipf->ipf_mp->b_cont;
2431 			for (; mp; mp = mp->b_cont) {
2432 				/* Extra points for neatness. */
2433 				IP_REASS_SET_START(mp, 0);
2434 				IP_REASS_SET_END(mp, 0);
2435 			}
2436 			mp = ipf->ipf_mp->b_cont;
2437 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2438 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2439 			ipfb->ipfb_count -= ipf->ipf_count;
2440 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2441 			ipfb->ipfb_frag_pkts--;
2442 			/*
2443 			 * We do not send any icmp message from here because
2444 			 * we currently are holding the ipfb_lock for this
2445 			 * hash chain. If we try and send any icmp messages
2446 			 * from here we may end up via a put back into ip
2447 			 * trying to get the same lock, causing a recursive
2448 			 * mutex panic. Instead we build a list and send all
2449 			 * the icmp messages after we have dropped the lock.
2450 			 */
2451 			if (ill->ill_isv6) {
2452 				if (hdr_length != 0) {
2453 					mp->b_next = send_icmp_head_v6;
2454 					send_icmp_head_v6 = mp;
2455 				} else {
2456 					freemsg(mp);
2457 				}
2458 			} else {
2459 				if (hdr_length != 0) {
2460 					mp->b_next = send_icmp_head;
2461 					send_icmp_head = mp;
2462 				} else {
2463 					freemsg(mp);
2464 				}
2465 			}
2466 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2467 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2468 			freeb(ipf->ipf_mp);
2469 		}
2470 		mutex_exit(&ipfb->ipfb_lock);
2471 		/*
2472 		 * Now need to send any icmp messages that we delayed from
2473 		 * above.
2474 		 */
2475 		while (send_icmp_head_v6 != NULL) {
2476 			ip6_t *ip6h;
2477 
2478 			mp = send_icmp_head_v6;
2479 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2480 			mp->b_next = NULL;
2481 			ip6h = (ip6_t *)mp->b_rptr;
2482 			iras.ira_flags = 0;
2483 			/*
2484 			 * This will result in an incorrect ALL_ZONES zoneid
2485 			 * for multicast packets, but we
2486 			 * don't send ICMP errors for those in any case.
2487 			 */
2488 			iras.ira_zoneid =
2489 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2490 			    ill, ipst);
2491 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2492 			icmp_time_exceeded_v6(mp,
2493 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2494 			    &iras);
2495 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2496 		}
2497 		while (send_icmp_head != NULL) {
2498 			ipaddr_t dst;
2499 
2500 			mp = send_icmp_head;
2501 			send_icmp_head = send_icmp_head->b_next;
2502 			mp->b_next = NULL;
2503 
2504 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2505 
2506 			iras.ira_flags = IRAF_IS_IPV4;
2507 			/*
2508 			 * This will result in an incorrect ALL_ZONES zoneid
2509 			 * for broadcast and multicast packets, but we
2510 			 * don't send ICMP errors for those in any case.
2511 			 */
2512 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2513 			    ill, ipst);
2514 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2515 			icmp_time_exceeded(mp,
2516 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2517 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2518 		}
2519 	}
2520 	/*
2521 	 * A non-dying ILL will use the return value to decide whether to
2522 	 * restart the frag timer, and for how long.
2523 	 */
2524 	return (next_timeout);
2525 }
2526 
2527 /*
2528  * This routine is called when the approximate count of mblk memory used
2529  * for the specified ILL has exceeded max_count.
2530  */
2531 void
2532 ill_frag_prune(ill_t *ill, uint_t max_count)
2533 {
2534 	ipfb_t	*ipfb;
2535 	ipf_t	*ipf;
2536 	size_t	count;
2537 	clock_t now;
2538 
2539 	/*
2540 	 * If we are here within ip_min_frag_prune_time msecs remove
2541 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2542 	 * ill_frag_free_num_pkts.
2543 	 */
2544 	mutex_enter(&ill->ill_lock);
2545 	now = ddi_get_lbolt();
2546 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2547 	    (ip_min_frag_prune_time != 0 ?
2548 	    ip_min_frag_prune_time : msec_per_tick)) {
2549 
2550 		ill->ill_frag_free_num_pkts++;
2551 
2552 	} else {
2553 		ill->ill_frag_free_num_pkts = 0;
2554 	}
2555 	ill->ill_last_frag_clean_time = now;
2556 	mutex_exit(&ill->ill_lock);
2557 
2558 	/*
2559 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2560 	 */
2561 	if (ill->ill_frag_free_num_pkts != 0) {
2562 		int ix;
2563 
2564 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2565 			ipfb = &ill->ill_frag_hash_tbl[ix];
2566 			mutex_enter(&ipfb->ipfb_lock);
2567 			if (ipfb->ipfb_ipf != NULL) {
2568 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2569 				    ill->ill_frag_free_num_pkts);
2570 			}
2571 			mutex_exit(&ipfb->ipfb_lock);
2572 		}
2573 	}
2574 	/*
2575 	 * While the reassembly list for this ILL is too big, prune a fragment
2576 	 * queue by age, oldest first.
2577 	 */
2578 	while (ill->ill_frag_count > max_count) {
2579 		int	ix;
2580 		ipfb_t	*oipfb = NULL;
2581 		uint_t	oldest = UINT_MAX;
2582 
2583 		count = 0;
2584 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2585 			ipfb = &ill->ill_frag_hash_tbl[ix];
2586 			mutex_enter(&ipfb->ipfb_lock);
2587 			ipf = ipfb->ipfb_ipf;
2588 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2589 				oldest = ipf->ipf_gen;
2590 				oipfb = ipfb;
2591 			}
2592 			count += ipfb->ipfb_count;
2593 			mutex_exit(&ipfb->ipfb_lock);
2594 		}
2595 		if (oipfb == NULL)
2596 			break;
2597 
2598 		if (count <= max_count)
2599 			return;	/* Somebody beat us to it, nothing to do */
2600 		mutex_enter(&oipfb->ipfb_lock);
2601 		ipf = oipfb->ipfb_ipf;
2602 		if (ipf != NULL) {
2603 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2604 		}
2605 		mutex_exit(&oipfb->ipfb_lock);
2606 	}
2607 }
2608 
2609 /*
2610  * free 'free_cnt' fragmented packets starting at ipf.
2611  */
2612 void
2613 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2614 {
2615 	size_t	count;
2616 	mblk_t	*mp;
2617 	mblk_t	*tmp;
2618 	ipf_t **ipfp = ipf->ipf_ptphn;
2619 
2620 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2621 	ASSERT(ipfp != NULL);
2622 	ASSERT(ipf != NULL);
2623 
2624 	while (ipf != NULL && free_cnt-- > 0) {
2625 		count = ipf->ipf_count;
2626 		mp = ipf->ipf_mp;
2627 		ipf = ipf->ipf_hash_next;
2628 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2629 			IP_REASS_SET_START(tmp, 0);
2630 			IP_REASS_SET_END(tmp, 0);
2631 		}
2632 		atomic_add_32(&ill->ill_frag_count, -count);
2633 		ASSERT(ipfb->ipfb_count >= count);
2634 		ipfb->ipfb_count -= count;
2635 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2636 		ipfb->ipfb_frag_pkts--;
2637 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2638 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2639 		freemsg(mp);
2640 	}
2641 
2642 	if (ipf)
2643 		ipf->ipf_ptphn = ipfp;
2644 	ipfp[0] = ipf;
2645 }
2646 
2647 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2648 	"obsolete and may be removed in a future release of Solaris.  Use " \
2649 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2650 
2651 /*
2652  * For obsolete per-interface forwarding configuration;
2653  * called in response to ND_GET.
2654  */
2655 /* ARGSUSED */
2656 static int
2657 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2658 {
2659 	ill_t *ill = (ill_t *)cp;
2660 
2661 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2662 
2663 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2664 	return (0);
2665 }
2666 
2667 /*
2668  * For obsolete per-interface forwarding configuration;
2669  * called in response to ND_SET.
2670  */
2671 /* ARGSUSED */
2672 static int
2673 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2674     cred_t *ioc_cr)
2675 {
2676 	long value;
2677 	int retval;
2678 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2679 
2680 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2681 
2682 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2683 	    value < 0 || value > 1) {
2684 		return (EINVAL);
2685 	}
2686 
2687 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2688 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2689 	rw_exit(&ipst->ips_ill_g_lock);
2690 	return (retval);
2691 }
2692 
2693 /*
2694  * Helper function for ill_forward_set().
2695  */
2696 static void
2697 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2698 {
2699 	ip_stack_t	*ipst = ill->ill_ipst;
2700 
2701 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2702 
2703 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2704 	    (enable ? "Enabling" : "Disabling"),
2705 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2706 	mutex_enter(&ill->ill_lock);
2707 	if (enable)
2708 		ill->ill_flags |= ILLF_ROUTER;
2709 	else
2710 		ill->ill_flags &= ~ILLF_ROUTER;
2711 	mutex_exit(&ill->ill_lock);
2712 	if (ill->ill_isv6)
2713 		ill_set_nce_router_flags(ill, enable);
2714 	/* Notify routing socket listeners of this change. */
2715 	if (ill->ill_ipif != NULL)
2716 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2717 }
2718 
2719 /*
2720  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2721  * socket messages for each interface whose flags we change.
2722  */
2723 int
2724 ill_forward_set(ill_t *ill, boolean_t enable)
2725 {
2726 	ipmp_illgrp_t *illg;
2727 	ip_stack_t *ipst = ill->ill_ipst;
2728 
2729 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2730 
2731 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2732 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2733 		return (0);
2734 
2735 	if (IS_LOOPBACK(ill))
2736 		return (EINVAL);
2737 
2738 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2739 		/*
2740 		 * Update all of the interfaces in the group.
2741 		 */
2742 		illg = ill->ill_grp;
2743 		ill = list_head(&illg->ig_if);
2744 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2745 			ill_forward_set_on_ill(ill, enable);
2746 
2747 		/*
2748 		 * Update the IPMP meta-interface.
2749 		 */
2750 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2751 		return (0);
2752 	}
2753 
2754 	ill_forward_set_on_ill(ill, enable);
2755 	return (0);
2756 }
2757 
2758 /*
2759  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2760  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2761  * set or clear.
2762  */
2763 static void
2764 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2765 {
2766 	ipif_t *ipif;
2767 	ncec_t *ncec;
2768 	nce_t *nce;
2769 
2770 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2771 		/*
2772 		 * NOTE: we match across the illgrp because nce's for
2773 		 * addresses on IPMP interfaces have an nce_ill that points to
2774 		 * the bound underlying ill.
2775 		 */
2776 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2777 		if (nce != NULL) {
2778 			ncec = nce->nce_common;
2779 			mutex_enter(&ncec->ncec_lock);
2780 			if (enable)
2781 				ncec->ncec_flags |= NCE_F_ISROUTER;
2782 			else
2783 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2784 			mutex_exit(&ncec->ncec_lock);
2785 			nce_refrele(nce);
2786 		}
2787 	}
2788 }
2789 
2790 /*
2791  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2792  * for this ill.  Make sure the v6/v4 question has been answered about this
2793  * ill.  The creation of this ndd variable is only for backwards compatibility.
2794  * The preferred way to control per-interface IP forwarding is through the
2795  * ILLF_ROUTER interface flag.
2796  */
2797 static int
2798 ill_set_ndd_name(ill_t *ill)
2799 {
2800 	char *suffix;
2801 	ip_stack_t	*ipst = ill->ill_ipst;
2802 
2803 	ASSERT(IAM_WRITER_ILL(ill));
2804 
2805 	if (ill->ill_isv6)
2806 		suffix = ipv6_forward_suffix;
2807 	else
2808 		suffix = ipv4_forward_suffix;
2809 
2810 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2811 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2812 	/*
2813 	 * Copies over the '\0'.
2814 	 * Note that strlen(suffix) is always bounded.
2815 	 */
2816 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2817 	    strlen(suffix) + 1);
2818 
2819 	/*
2820 	 * Use of the nd table requires holding the reader lock.
2821 	 * Modifying the nd table thru nd_load/nd_unload requires
2822 	 * the writer lock.
2823 	 */
2824 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2825 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2826 	    nd_ill_forward_set, (caddr_t)ill)) {
2827 		/*
2828 		 * If the nd_load failed, it only meant that it could not
2829 		 * allocate a new bunch of room for further NDD expansion.
2830 		 * Because of that, the ill_ndd_name will be set to 0, and
2831 		 * this interface is at the mercy of the global ip_forwarding
2832 		 * variable.
2833 		 */
2834 		rw_exit(&ipst->ips_ip_g_nd_lock);
2835 		ill->ill_ndd_name = NULL;
2836 		return (ENOMEM);
2837 	}
2838 	rw_exit(&ipst->ips_ip_g_nd_lock);
2839 	return (0);
2840 }
2841 
2842 /*
2843  * Intializes the context structure and returns the first ill in the list
2844  * cuurently start_list and end_list can have values:
2845  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2846  * IP_V4_G_HEAD		Traverse IPV4 list only.
2847  * IP_V6_G_HEAD		Traverse IPV6 list only.
2848  */
2849 
2850 /*
2851  * We don't check for CONDEMNED ills here. Caller must do that if
2852  * necessary under the ill lock.
2853  */
2854 ill_t *
2855 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2856     ip_stack_t *ipst)
2857 {
2858 	ill_if_t *ifp;
2859 	ill_t *ill;
2860 	avl_tree_t *avl_tree;
2861 
2862 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2863 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2864 
2865 	/*
2866 	 * setup the lists to search
2867 	 */
2868 	if (end_list != MAX_G_HEADS) {
2869 		ctx->ctx_current_list = start_list;
2870 		ctx->ctx_last_list = end_list;
2871 	} else {
2872 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2873 		ctx->ctx_current_list = 0;
2874 	}
2875 
2876 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2877 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2878 		if (ifp != (ill_if_t *)
2879 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2880 			avl_tree = &ifp->illif_avl_by_ppa;
2881 			ill = avl_first(avl_tree);
2882 			/*
2883 			 * ill is guaranteed to be non NULL or ifp should have
2884 			 * not existed.
2885 			 */
2886 			ASSERT(ill != NULL);
2887 			return (ill);
2888 		}
2889 		ctx->ctx_current_list++;
2890 	}
2891 
2892 	return (NULL);
2893 }
2894 
2895 /*
2896  * returns the next ill in the list. ill_first() must have been called
2897  * before calling ill_next() or bad things will happen.
2898  */
2899 
2900 /*
2901  * We don't check for CONDEMNED ills here. Caller must do that if
2902  * necessary under the ill lock.
2903  */
2904 ill_t *
2905 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2906 {
2907 	ill_if_t *ifp;
2908 	ill_t *ill;
2909 	ip_stack_t	*ipst = lastill->ill_ipst;
2910 
2911 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2912 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2913 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2914 	    AVL_AFTER)) != NULL) {
2915 		return (ill);
2916 	}
2917 
2918 	/* goto next ill_ifp in the list. */
2919 	ifp = lastill->ill_ifptr->illif_next;
2920 
2921 	/* make sure not at end of circular list */
2922 	while (ifp ==
2923 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2924 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2925 			return (NULL);
2926 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2927 	}
2928 
2929 	return (avl_first(&ifp->illif_avl_by_ppa));
2930 }
2931 
2932 /*
2933  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2934  * The final number (PPA) must not have any leading zeros.  Upon success, a
2935  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2936  */
2937 static char *
2938 ill_get_ppa_ptr(char *name)
2939 {
2940 	int namelen = strlen(name);
2941 	int end_ndx = namelen - 1;
2942 	int ppa_ndx, i;
2943 
2944 	/*
2945 	 * Check that the first character is [a-zA-Z], and that the last
2946 	 * character is [0-9].
2947 	 */
2948 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2949 		return (NULL);
2950 
2951 	/*
2952 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2953 	 */
2954 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2955 		if (!isdigit(name[ppa_ndx - 1]))
2956 			break;
2957 
2958 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2959 		return (NULL);
2960 
2961 	/*
2962 	 * Check that the intermediate characters are [a-z0-9.]
2963 	 */
2964 	for (i = 1; i < ppa_ndx; i++) {
2965 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2966 		    name[i] != '.' && name[i] != '_') {
2967 			return (NULL);
2968 		}
2969 	}
2970 
2971 	return (name + ppa_ndx);
2972 }
2973 
2974 /*
2975  * use avl tree to locate the ill.
2976  */
2977 static ill_t *
2978 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2979 {
2980 	char *ppa_ptr = NULL;
2981 	int len;
2982 	uint_t ppa;
2983 	ill_t *ill = NULL;
2984 	ill_if_t *ifp;
2985 	int list;
2986 
2987 	/*
2988 	 * get ppa ptr
2989 	 */
2990 	if (isv6)
2991 		list = IP_V6_G_HEAD;
2992 	else
2993 		list = IP_V4_G_HEAD;
2994 
2995 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2996 		return (NULL);
2997 	}
2998 
2999 	len = ppa_ptr - name + 1;
3000 
3001 	ppa = stoi(&ppa_ptr);
3002 
3003 	ifp = IP_VX_ILL_G_LIST(list, ipst);
3004 
3005 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3006 		/*
3007 		 * match is done on len - 1 as the name is not null
3008 		 * terminated it contains ppa in addition to the interface
3009 		 * name.
3010 		 */
3011 		if ((ifp->illif_name_len == len) &&
3012 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
3013 			break;
3014 		} else {
3015 			ifp = ifp->illif_next;
3016 		}
3017 	}
3018 
3019 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
3020 		/*
3021 		 * Even the interface type does not exist.
3022 		 */
3023 		return (NULL);
3024 	}
3025 
3026 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
3027 	if (ill != NULL) {
3028 		mutex_enter(&ill->ill_lock);
3029 		if (ILL_CAN_LOOKUP(ill)) {
3030 			ill_refhold_locked(ill);
3031 			mutex_exit(&ill->ill_lock);
3032 			return (ill);
3033 		}
3034 		mutex_exit(&ill->ill_lock);
3035 	}
3036 	return (NULL);
3037 }
3038 
3039 /*
3040  * comparison function for use with avl.
3041  */
3042 static int
3043 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3044 {
3045 	uint_t ppa;
3046 	uint_t ill_ppa;
3047 
3048 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3049 
3050 	ppa = *((uint_t *)ppa_ptr);
3051 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3052 	/*
3053 	 * We want the ill with the lowest ppa to be on the
3054 	 * top.
3055 	 */
3056 	if (ill_ppa < ppa)
3057 		return (1);
3058 	if (ill_ppa > ppa)
3059 		return (-1);
3060 	return (0);
3061 }
3062 
3063 /*
3064  * remove an interface type from the global list.
3065  */
3066 static void
3067 ill_delete_interface_type(ill_if_t *interface)
3068 {
3069 	ASSERT(interface != NULL);
3070 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3071 
3072 	avl_destroy(&interface->illif_avl_by_ppa);
3073 	if (interface->illif_ppa_arena != NULL)
3074 		vmem_destroy(interface->illif_ppa_arena);
3075 
3076 	remque(interface);
3077 
3078 	mi_free(interface);
3079 }
3080 
3081 /*
3082  * remove ill from the global list.
3083  */
3084 static void
3085 ill_glist_delete(ill_t *ill)
3086 {
3087 	ip_stack_t	*ipst;
3088 	phyint_t	*phyi;
3089 
3090 	if (ill == NULL)
3091 		return;
3092 	ipst = ill->ill_ipst;
3093 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3094 
3095 	/*
3096 	 * If the ill was never inserted into the AVL tree
3097 	 * we skip the if branch.
3098 	 */
3099 	if (ill->ill_ifptr != NULL) {
3100 		/*
3101 		 * remove from AVL tree and free ppa number
3102 		 */
3103 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3104 
3105 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3106 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3107 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3108 		}
3109 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3110 			ill_delete_interface_type(ill->ill_ifptr);
3111 		}
3112 
3113 		/*
3114 		 * Indicate ill is no longer in the list.
3115 		 */
3116 		ill->ill_ifptr = NULL;
3117 		ill->ill_name_length = 0;
3118 		ill->ill_name[0] = '\0';
3119 		ill->ill_ppa = UINT_MAX;
3120 	}
3121 
3122 	/* Generate one last event for this ill. */
3123 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3124 	    ill->ill_name_length);
3125 
3126 	ASSERT(ill->ill_phyint != NULL);
3127 	phyi = ill->ill_phyint;
3128 	ill->ill_phyint = NULL;
3129 
3130 	/*
3131 	 * ill_init allocates a phyint always to store the copy
3132 	 * of flags relevant to phyint. At that point in time, we could
3133 	 * not assign the name and hence phyint_illv4/v6 could not be
3134 	 * initialized. Later in ipif_set_values, we assign the name to
3135 	 * the ill, at which point in time we assign phyint_illv4/v6.
3136 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3137 	 */
3138 	if (ill->ill_flags & ILLF_IPV6)
3139 		phyi->phyint_illv6 = NULL;
3140 	else
3141 		phyi->phyint_illv4 = NULL;
3142 
3143 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3144 		rw_exit(&ipst->ips_ill_g_lock);
3145 		return;
3146 	}
3147 
3148 	/*
3149 	 * There are no ills left on this phyint; pull it out of the phyint
3150 	 * avl trees, and free it.
3151 	 */
3152 	if (phyi->phyint_ifindex > 0) {
3153 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3154 		    phyi);
3155 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3156 		    phyi);
3157 	}
3158 	rw_exit(&ipst->ips_ill_g_lock);
3159 
3160 	phyint_free(phyi);
3161 }
3162 
3163 /*
3164  * allocate a ppa, if the number of plumbed interfaces of this type are
3165  * less than ill_no_arena do a linear search to find a unused ppa.
3166  * When the number goes beyond ill_no_arena switch to using an arena.
3167  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3168  * is the return value for an error condition, so allocation starts at one
3169  * and is decremented by one.
3170  */
3171 static int
3172 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3173 {
3174 	ill_t *tmp_ill;
3175 	uint_t start, end;
3176 	int ppa;
3177 
3178 	if (ifp->illif_ppa_arena == NULL &&
3179 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3180 		/*
3181 		 * Create an arena.
3182 		 */
3183 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3184 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3185 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3186 			/* allocate what has already been assigned */
3187 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3188 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3189 		    tmp_ill, AVL_AFTER)) {
3190 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3191 			    1,		/* size */
3192 			    1,		/* align/quantum */
3193 			    0,		/* phase */
3194 			    0,		/* nocross */
3195 			    /* minaddr */
3196 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3197 			    /* maxaddr */
3198 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3199 			    VM_NOSLEEP|VM_FIRSTFIT);
3200 			if (ppa == 0) {
3201 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3202 				    " failed while switching"));
3203 				vmem_destroy(ifp->illif_ppa_arena);
3204 				ifp->illif_ppa_arena = NULL;
3205 				break;
3206 			}
3207 		}
3208 	}
3209 
3210 	if (ifp->illif_ppa_arena != NULL) {
3211 		if (ill->ill_ppa == UINT_MAX) {
3212 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3213 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3214 			if (ppa == 0)
3215 				return (EAGAIN);
3216 			ill->ill_ppa = --ppa;
3217 		} else {
3218 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3219 			    1, 		/* size */
3220 			    1, 		/* align/quantum */
3221 			    0, 		/* phase */
3222 			    0, 		/* nocross */
3223 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3224 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3225 			    VM_NOSLEEP|VM_FIRSTFIT);
3226 			/*
3227 			 * Most likely the allocation failed because
3228 			 * the requested ppa was in use.
3229 			 */
3230 			if (ppa == 0)
3231 				return (EEXIST);
3232 		}
3233 		return (0);
3234 	}
3235 
3236 	/*
3237 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3238 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3239 	 */
3240 	if (ill->ill_ppa == UINT_MAX) {
3241 		end = UINT_MAX - 1;
3242 		start = 0;
3243 	} else {
3244 		end = start = ill->ill_ppa;
3245 	}
3246 
3247 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3248 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3249 		if (start++ >= end) {
3250 			if (ill->ill_ppa == UINT_MAX)
3251 				return (EAGAIN);
3252 			else
3253 				return (EEXIST);
3254 		}
3255 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3256 	}
3257 	ill->ill_ppa = start;
3258 	return (0);
3259 }
3260 
3261 /*
3262  * Insert ill into the list of configured ill's. Once this function completes,
3263  * the ill is globally visible and is available through lookups. More precisely
3264  * this happens after the caller drops the ill_g_lock.
3265  */
3266 static int
3267 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3268 {
3269 	ill_if_t *ill_interface;
3270 	avl_index_t where = 0;
3271 	int error;
3272 	int name_length;
3273 	int index;
3274 	boolean_t check_length = B_FALSE;
3275 	ip_stack_t	*ipst = ill->ill_ipst;
3276 
3277 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3278 
3279 	name_length = mi_strlen(name) + 1;
3280 
3281 	if (isv6)
3282 		index = IP_V6_G_HEAD;
3283 	else
3284 		index = IP_V4_G_HEAD;
3285 
3286 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3287 	/*
3288 	 * Search for interface type based on name
3289 	 */
3290 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3291 		if ((ill_interface->illif_name_len == name_length) &&
3292 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3293 			break;
3294 		}
3295 		ill_interface = ill_interface->illif_next;
3296 	}
3297 
3298 	/*
3299 	 * Interface type not found, create one.
3300 	 */
3301 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3302 		ill_g_head_t ghead;
3303 
3304 		/*
3305 		 * allocate ill_if_t structure
3306 		 */
3307 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3308 		if (ill_interface == NULL) {
3309 			return (ENOMEM);
3310 		}
3311 
3312 		(void) strcpy(ill_interface->illif_name, name);
3313 		ill_interface->illif_name_len = name_length;
3314 
3315 		avl_create(&ill_interface->illif_avl_by_ppa,
3316 		    ill_compare_ppa, sizeof (ill_t),
3317 		    offsetof(struct ill_s, ill_avl_byppa));
3318 
3319 		/*
3320 		 * link the structure in the back to maintain order
3321 		 * of configuration for ifconfig output.
3322 		 */
3323 		ghead = ipst->ips_ill_g_heads[index];
3324 		insque(ill_interface, ghead.ill_g_list_tail);
3325 	}
3326 
3327 	if (ill->ill_ppa == UINT_MAX)
3328 		check_length = B_TRUE;
3329 
3330 	error = ill_alloc_ppa(ill_interface, ill);
3331 	if (error != 0) {
3332 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3333 			ill_delete_interface_type(ill->ill_ifptr);
3334 		return (error);
3335 	}
3336 
3337 	/*
3338 	 * When the ppa is choosen by the system, check that there is
3339 	 * enough space to insert ppa. if a specific ppa was passed in this
3340 	 * check is not required as the interface name passed in will have
3341 	 * the right ppa in it.
3342 	 */
3343 	if (check_length) {
3344 		/*
3345 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3346 		 */
3347 		char buf[sizeof (uint_t) * 3];
3348 
3349 		/*
3350 		 * convert ppa to string to calculate the amount of space
3351 		 * required for it in the name.
3352 		 */
3353 		numtos(ill->ill_ppa, buf);
3354 
3355 		/* Do we have enough space to insert ppa ? */
3356 
3357 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3358 			/* Free ppa and interface type struct */
3359 			if (ill_interface->illif_ppa_arena != NULL) {
3360 				vmem_free(ill_interface->illif_ppa_arena,
3361 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3362 			}
3363 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3364 				ill_delete_interface_type(ill->ill_ifptr);
3365 
3366 			return (EINVAL);
3367 		}
3368 	}
3369 
3370 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3371 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3372 
3373 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3374 	    &where);
3375 	ill->ill_ifptr = ill_interface;
3376 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3377 
3378 	ill_phyint_reinit(ill);
3379 	return (0);
3380 }
3381 
3382 /* Initialize the per phyint ipsq used for serialization */
3383 static boolean_t
3384 ipsq_init(ill_t *ill, boolean_t enter)
3385 {
3386 	ipsq_t  *ipsq;
3387 	ipxop_t	*ipx;
3388 
3389 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3390 		return (B_FALSE);
3391 
3392 	ill->ill_phyint->phyint_ipsq = ipsq;
3393 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3394 	ipx->ipx_ipsq = ipsq;
3395 	ipsq->ipsq_next = ipsq;
3396 	ipsq->ipsq_phyint = ill->ill_phyint;
3397 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3398 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3399 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3400 	if (enter) {
3401 		ipx->ipx_writer = curthread;
3402 		ipx->ipx_forced = B_FALSE;
3403 		ipx->ipx_reentry_cnt = 1;
3404 #ifdef DEBUG
3405 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3406 #endif
3407 	}
3408 	return (B_TRUE);
3409 }
3410 
3411 /*
3412  * ill_init is called by ip_open when a device control stream is opened.
3413  * It does a few initializations, and shoots a DL_INFO_REQ message down
3414  * to the driver.  The response is later picked up in ip_rput_dlpi and
3415  * used to set up default mechanisms for talking to the driver.  (Always
3416  * called as writer.)
3417  *
3418  * If this function returns error, ip_open will call ip_close which in
3419  * turn will call ill_delete to clean up any memory allocated here that
3420  * is not yet freed.
3421  */
3422 int
3423 ill_init(queue_t *q, ill_t *ill)
3424 {
3425 	int	count;
3426 	dl_info_req_t	*dlir;
3427 	mblk_t	*info_mp;
3428 	uchar_t *frag_ptr;
3429 
3430 	/*
3431 	 * The ill is initialized to zero by mi_alloc*(). In addition
3432 	 * some fields already contain valid values, initialized in
3433 	 * ip_open(), before we reach here.
3434 	 */
3435 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3436 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3437 	ill->ill_saved_ire_cnt = 0;
3438 
3439 	ill->ill_rq = q;
3440 	ill->ill_wq = WR(q);
3441 
3442 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3443 	    BPRI_HI);
3444 	if (info_mp == NULL)
3445 		return (ENOMEM);
3446 
3447 	/*
3448 	 * Allocate sufficient space to contain our fragment hash table and
3449 	 * the device name.
3450 	 */
3451 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3452 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3453 	if (frag_ptr == NULL) {
3454 		freemsg(info_mp);
3455 		return (ENOMEM);
3456 	}
3457 	ill->ill_frag_ptr = frag_ptr;
3458 	ill->ill_frag_free_num_pkts = 0;
3459 	ill->ill_last_frag_clean_time = 0;
3460 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3461 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3462 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3463 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3464 		    NULL, MUTEX_DEFAULT, NULL);
3465 	}
3466 
3467 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3468 	if (ill->ill_phyint == NULL) {
3469 		freemsg(info_mp);
3470 		mi_free(frag_ptr);
3471 		return (ENOMEM);
3472 	}
3473 
3474 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3475 	/*
3476 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3477 	 * at this point because of the following reason. If we can't
3478 	 * enter the ipsq at some point and cv_wait, the writer that
3479 	 * wakes us up tries to locate us using the list of all phyints
3480 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3481 	 * If we don't set it now, we risk a missed wakeup.
3482 	 */
3483 	ill->ill_phyint->phyint_illv4 = ill;
3484 	ill->ill_ppa = UINT_MAX;
3485 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3486 
3487 	ill_set_inputfn(ill);
3488 
3489 	if (!ipsq_init(ill, B_TRUE)) {
3490 		freemsg(info_mp);
3491 		mi_free(frag_ptr);
3492 		mi_free(ill->ill_phyint);
3493 		return (ENOMEM);
3494 	}
3495 
3496 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3497 
3498 	/* Frag queue limit stuff */
3499 	ill->ill_frag_count = 0;
3500 	ill->ill_ipf_gen = 0;
3501 
3502 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3503 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3504 	ill->ill_global_timer = INFINITY;
3505 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3506 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3507 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3508 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3509 
3510 	/*
3511 	 * Initialize IPv6 configuration variables.  The IP module is always
3512 	 * opened as an IPv4 module.  Instead tracking down the cases where
3513 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3514 	 * here for convenience, this has no effect until the ill is set to do
3515 	 * IPv6.
3516 	 */
3517 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3518 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3519 	ill->ill_max_buf = ND_MAX_Q;
3520 	ill->ill_refcnt = 0;
3521 
3522 	/* Send down the Info Request to the driver. */
3523 	info_mp->b_datap->db_type = M_PCPROTO;
3524 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3525 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3526 	dlir->dl_primitive = DL_INFO_REQ;
3527 
3528 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3529 
3530 	qprocson(q);
3531 	ill_dlpi_send(ill, info_mp);
3532 
3533 	return (0);
3534 }
3535 
3536 /*
3537  * ill_dls_info
3538  * creates datalink socket info from the device.
3539  */
3540 int
3541 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3542 {
3543 	size_t	len;
3544 
3545 	sdl->sdl_family = AF_LINK;
3546 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3547 	sdl->sdl_type = ill->ill_type;
3548 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3549 	len = strlen(sdl->sdl_data);
3550 	ASSERT(len < 256);
3551 	sdl->sdl_nlen = (uchar_t)len;
3552 	sdl->sdl_alen = ill->ill_phys_addr_length;
3553 	sdl->sdl_slen = 0;
3554 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3555 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3556 
3557 	return (sizeof (struct sockaddr_dl));
3558 }
3559 
3560 /*
3561  * ill_xarp_info
3562  * creates xarp info from the device.
3563  */
3564 static int
3565 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3566 {
3567 	sdl->sdl_family = AF_LINK;
3568 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3569 	sdl->sdl_type = ill->ill_type;
3570 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3571 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3572 	sdl->sdl_alen = ill->ill_phys_addr_length;
3573 	sdl->sdl_slen = 0;
3574 	return (sdl->sdl_nlen);
3575 }
3576 
3577 static int
3578 loopback_kstat_update(kstat_t *ksp, int rw)
3579 {
3580 	kstat_named_t *kn;
3581 	netstackid_t	stackid;
3582 	netstack_t	*ns;
3583 	ip_stack_t	*ipst;
3584 
3585 	if (ksp == NULL || ksp->ks_data == NULL)
3586 		return (EIO);
3587 
3588 	if (rw == KSTAT_WRITE)
3589 		return (EACCES);
3590 
3591 	kn = KSTAT_NAMED_PTR(ksp);
3592 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3593 
3594 	ns = netstack_find_by_stackid(stackid);
3595 	if (ns == NULL)
3596 		return (-1);
3597 
3598 	ipst = ns->netstack_ip;
3599 	if (ipst == NULL) {
3600 		netstack_rele(ns);
3601 		return (-1);
3602 	}
3603 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3604 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3605 	netstack_rele(ns);
3606 	return (0);
3607 }
3608 
3609 /*
3610  * Has ifindex been plumbed already?
3611  */
3612 static boolean_t
3613 phyint_exists(uint_t index, ip_stack_t *ipst)
3614 {
3615 	ASSERT(index != 0);
3616 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3617 
3618 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3619 	    &index, NULL) != NULL);
3620 }
3621 
3622 /* Pick a unique ifindex */
3623 boolean_t
3624 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3625 {
3626 	uint_t starting_index;
3627 
3628 	if (!ipst->ips_ill_index_wrap) {
3629 		*indexp = ipst->ips_ill_index++;
3630 		if (ipst->ips_ill_index == 0) {
3631 			/* Reached the uint_t limit Next time wrap  */
3632 			ipst->ips_ill_index_wrap = B_TRUE;
3633 		}
3634 		return (B_TRUE);
3635 	}
3636 
3637 	/*
3638 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3639 	 * at this point and don't want to call any function that attempts
3640 	 * to get the lock again.
3641 	 */
3642 	starting_index = ipst->ips_ill_index++;
3643 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3644 		if (ipst->ips_ill_index != 0 &&
3645 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3646 			/* found unused index - use it */
3647 			*indexp = ipst->ips_ill_index;
3648 			return (B_TRUE);
3649 		}
3650 	}
3651 
3652 	/*
3653 	 * all interface indicies are inuse.
3654 	 */
3655 	return (B_FALSE);
3656 }
3657 
3658 /*
3659  * Assign a unique interface index for the phyint.
3660  */
3661 static boolean_t
3662 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3663 {
3664 	ASSERT(phyi->phyint_ifindex == 0);
3665 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3666 }
3667 
3668 /*
3669  * Initialize the flags on `phyi' as per the provided mactype.
3670  */
3671 static void
3672 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3673 {
3674 	uint64_t flags = 0;
3675 
3676 	/*
3677 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3678 	 * we always presume the underlying hardware is working and set
3679 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3680 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3681 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3682 	 */
3683 	if (mactype == SUNW_DL_IPMP)
3684 		flags |= PHYI_FAILED;
3685 	else
3686 		flags |= PHYI_RUNNING;
3687 
3688 	switch (mactype) {
3689 	case SUNW_DL_VNI:
3690 		flags |= PHYI_VIRTUAL;
3691 		break;
3692 	case SUNW_DL_IPMP:
3693 		flags |= PHYI_IPMP;
3694 		break;
3695 	case DL_LOOP:
3696 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3697 		break;
3698 	}
3699 
3700 	mutex_enter(&phyi->phyint_lock);
3701 	phyi->phyint_flags |= flags;
3702 	mutex_exit(&phyi->phyint_lock);
3703 }
3704 
3705 /*
3706  * Return a pointer to the ill which matches the supplied name.  Note that
3707  * the ill name length includes the null termination character.  (May be
3708  * called as writer.)
3709  * If do_alloc and the interface is "lo0" it will be automatically created.
3710  * Cannot bump up reference on condemned ills. So dup detect can't be done
3711  * using this func.
3712  */
3713 ill_t *
3714 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3715     boolean_t *did_alloc, ip_stack_t *ipst)
3716 {
3717 	ill_t	*ill;
3718 	ipif_t	*ipif;
3719 	ipsq_t	*ipsq;
3720 	kstat_named_t	*kn;
3721 	boolean_t isloopback;
3722 	in6_addr_t ov6addr;
3723 
3724 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3725 
3726 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3727 	ill = ill_find_by_name(name, isv6, ipst);
3728 	rw_exit(&ipst->ips_ill_g_lock);
3729 	if (ill != NULL)
3730 		return (ill);
3731 
3732 	/*
3733 	 * Couldn't find it.  Does this happen to be a lookup for the
3734 	 * loopback device and are we allowed to allocate it?
3735 	 */
3736 	if (!isloopback || !do_alloc)
3737 		return (NULL);
3738 
3739 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3740 	ill = ill_find_by_name(name, isv6, ipst);
3741 	if (ill != NULL) {
3742 		rw_exit(&ipst->ips_ill_g_lock);
3743 		return (ill);
3744 	}
3745 
3746 	/* Create the loopback device on demand */
3747 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3748 	    sizeof (ipif_loopback_name), BPRI_MED));
3749 	if (ill == NULL)
3750 		goto done;
3751 
3752 	*ill = ill_null;
3753 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3754 	ill->ill_ipst = ipst;
3755 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3756 	netstack_hold(ipst->ips_netstack);
3757 	/*
3758 	 * For exclusive stacks we set the zoneid to zero
3759 	 * to make IP operate as if in the global zone.
3760 	 */
3761 	ill->ill_zoneid = GLOBAL_ZONEID;
3762 
3763 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3764 	if (ill->ill_phyint == NULL)
3765 		goto done;
3766 
3767 	if (isv6)
3768 		ill->ill_phyint->phyint_illv6 = ill;
3769 	else
3770 		ill->ill_phyint->phyint_illv4 = ill;
3771 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3772 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3773 
3774 	if (isv6) {
3775 		ill->ill_isv6 = B_TRUE;
3776 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3777 	} else {
3778 		ill->ill_max_frag = ip_loopback_mtuplus;
3779 	}
3780 	if (!ill_allocate_mibs(ill))
3781 		goto done;
3782 	ill->ill_current_frag = ill->ill_max_frag;
3783 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3784 	/*
3785 	 * ipif_loopback_name can't be pointed at directly because its used
3786 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3787 	 * from the glist, ill_glist_delete() sets the first character of
3788 	 * ill_name to '\0'.
3789 	 */
3790 	ill->ill_name = (char *)ill + sizeof (*ill);
3791 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3792 	ill->ill_name_length = sizeof (ipif_loopback_name);
3793 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3794 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3795 
3796 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3797 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3798 	ill->ill_global_timer = INFINITY;
3799 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3800 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3801 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3802 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3803 
3804 	/* No resolver here. */
3805 	ill->ill_net_type = IRE_LOOPBACK;
3806 
3807 	/* Initialize the ipsq */
3808 	if (!ipsq_init(ill, B_FALSE))
3809 		goto done;
3810 
3811 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3812 	if (ipif == NULL)
3813 		goto done;
3814 
3815 	ill->ill_flags = ILLF_MULTICAST;
3816 
3817 	ov6addr = ipif->ipif_v6lcl_addr;
3818 	/* Set up default loopback address and mask. */
3819 	if (!isv6) {
3820 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3821 
3822 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3823 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3824 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3825 		    ipif->ipif_v6subnet);
3826 		ill->ill_flags |= ILLF_IPV4;
3827 	} else {
3828 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3829 		ipif->ipif_v6net_mask = ipv6_all_ones;
3830 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3831 		    ipif->ipif_v6subnet);
3832 		ill->ill_flags |= ILLF_IPV6;
3833 	}
3834 
3835 	/*
3836 	 * Chain us in at the end of the ill list. hold the ill
3837 	 * before we make it globally visible. 1 for the lookup.
3838 	 */
3839 	ill->ill_refcnt = 0;
3840 	ill_refhold(ill);
3841 
3842 	ill->ill_frag_count = 0;
3843 	ill->ill_frag_free_num_pkts = 0;
3844 	ill->ill_last_frag_clean_time = 0;
3845 
3846 	ipsq = ill->ill_phyint->phyint_ipsq;
3847 
3848 	ill_set_inputfn(ill);
3849 
3850 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3851 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3852 
3853 	/* Let SCTP know so that it can add this to its list */
3854 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3855 
3856 	/*
3857 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3858 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3859 	 * requires to be after ill_glist_insert() since we need the
3860 	 * ill_index set. Pass on ipv6_loopback as the old address.
3861 	 */
3862 	sctp_update_ipif_addr(ipif, ov6addr);
3863 
3864 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3865 
3866 	/*
3867 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3868 	 * If so, free our original one.
3869 	 */
3870 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3871 		ipsq_delete(ipsq);
3872 
3873 	if (ipst->ips_loopback_ksp == NULL) {
3874 		/* Export loopback interface statistics */
3875 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3876 		    ipif_loopback_name, "net",
3877 		    KSTAT_TYPE_NAMED, 2, 0,
3878 		    ipst->ips_netstack->netstack_stackid);
3879 		if (ipst->ips_loopback_ksp != NULL) {
3880 			ipst->ips_loopback_ksp->ks_update =
3881 			    loopback_kstat_update;
3882 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3883 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3884 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3885 			ipst->ips_loopback_ksp->ks_private =
3886 			    (void *)(uintptr_t)ipst->ips_netstack->
3887 			    netstack_stackid;
3888 			kstat_install(ipst->ips_loopback_ksp);
3889 		}
3890 	}
3891 
3892 	*did_alloc = B_TRUE;
3893 	rw_exit(&ipst->ips_ill_g_lock);
3894 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3895 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3896 	return (ill);
3897 done:
3898 	if (ill != NULL) {
3899 		if (ill->ill_phyint != NULL) {
3900 			ipsq = ill->ill_phyint->phyint_ipsq;
3901 			if (ipsq != NULL) {
3902 				ipsq->ipsq_phyint = NULL;
3903 				ipsq_delete(ipsq);
3904 			}
3905 			mi_free(ill->ill_phyint);
3906 		}
3907 		ill_free_mib(ill);
3908 		if (ill->ill_ipst != NULL)
3909 			netstack_rele(ill->ill_ipst->ips_netstack);
3910 		mi_free(ill);
3911 	}
3912 	rw_exit(&ipst->ips_ill_g_lock);
3913 	return (NULL);
3914 }
3915 
3916 /*
3917  * For IPP calls - use the ip_stack_t for global stack.
3918  */
3919 ill_t *
3920 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3921 {
3922 	ip_stack_t	*ipst;
3923 	ill_t		*ill;
3924 
3925 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3926 	if (ipst == NULL) {
3927 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3928 		return (NULL);
3929 	}
3930 
3931 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3932 	netstack_rele(ipst->ips_netstack);
3933 	return (ill);
3934 }
3935 
3936 /*
3937  * Return a pointer to the ill which matches the index and IP version type.
3938  */
3939 ill_t *
3940 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3941 {
3942 	ill_t	*ill;
3943 	phyint_t *phyi;
3944 
3945 	/*
3946 	 * Indexes are stored in the phyint - a common structure
3947 	 * to both IPv4 and IPv6.
3948 	 */
3949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3950 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3951 	    (void *) &index, NULL);
3952 	if (phyi != NULL) {
3953 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3954 		if (ill != NULL) {
3955 			mutex_enter(&ill->ill_lock);
3956 			if (!ILL_IS_CONDEMNED(ill)) {
3957 				ill_refhold_locked(ill);
3958 				mutex_exit(&ill->ill_lock);
3959 				rw_exit(&ipst->ips_ill_g_lock);
3960 				return (ill);
3961 			}
3962 			mutex_exit(&ill->ill_lock);
3963 		}
3964 	}
3965 	rw_exit(&ipst->ips_ill_g_lock);
3966 	return (NULL);
3967 }
3968 
3969 /*
3970  * Verify whether or not an interface index is valid for the specified zoneid
3971  * to transmit packets.
3972  * It can be zero (meaning "reset") or an interface index assigned
3973  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3974  */
3975 boolean_t
3976 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3977     ip_stack_t *ipst)
3978 {
3979 	ill_t		*ill;
3980 
3981 	if (ifindex == 0)
3982 		return (B_TRUE);
3983 
3984 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3985 	if (ill == NULL)
3986 		return (B_FALSE);
3987 	if (IS_VNI(ill)) {
3988 		ill_refrele(ill);
3989 		return (B_FALSE);
3990 	}
3991 	ill_refrele(ill);
3992 	return (B_TRUE);
3993 }
3994 
3995 /*
3996  * Return the ifindex next in sequence after the passed in ifindex.
3997  * If there is no next ifindex for the given protocol, return 0.
3998  */
3999 uint_t
4000 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
4001 {
4002 	phyint_t *phyi;
4003 	phyint_t *phyi_initial;
4004 	uint_t   ifindex;
4005 
4006 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4007 
4008 	if (index == 0) {
4009 		phyi = avl_first(
4010 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4011 	} else {
4012 		phyi = phyi_initial = avl_find(
4013 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4014 		    (void *) &index, NULL);
4015 	}
4016 
4017 	for (; phyi != NULL;
4018 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4019 	    phyi, AVL_AFTER)) {
4020 		/*
4021 		 * If we're not returning the first interface in the tree
4022 		 * and we still haven't moved past the phyint_t that
4023 		 * corresponds to index, avl_walk needs to be called again
4024 		 */
4025 		if (!((index != 0) && (phyi == phyi_initial))) {
4026 			if (isv6) {
4027 				if ((phyi->phyint_illv6) &&
4028 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
4029 				    (phyi->phyint_illv6->ill_isv6 == 1))
4030 					break;
4031 			} else {
4032 				if ((phyi->phyint_illv4) &&
4033 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
4034 				    (phyi->phyint_illv4->ill_isv6 == 0))
4035 					break;
4036 			}
4037 		}
4038 	}
4039 
4040 	rw_exit(&ipst->ips_ill_g_lock);
4041 
4042 	if (phyi != NULL)
4043 		ifindex = phyi->phyint_ifindex;
4044 	else
4045 		ifindex = 0;
4046 
4047 	return (ifindex);
4048 }
4049 
4050 /*
4051  * Return the ifindex for the named interface.
4052  * If there is no next ifindex for the interface, return 0.
4053  */
4054 uint_t
4055 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4056 {
4057 	phyint_t	*phyi;
4058 	avl_index_t	where = 0;
4059 	uint_t		ifindex;
4060 
4061 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4062 
4063 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4064 	    name, &where)) == NULL) {
4065 		rw_exit(&ipst->ips_ill_g_lock);
4066 		return (0);
4067 	}
4068 
4069 	ifindex = phyi->phyint_ifindex;
4070 
4071 	rw_exit(&ipst->ips_ill_g_lock);
4072 
4073 	return (ifindex);
4074 }
4075 
4076 /*
4077  * Return the ifindex to be used by upper layer protocols for instance
4078  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4079  */
4080 uint_t
4081 ill_get_upper_ifindex(const ill_t *ill)
4082 {
4083 	if (IS_UNDER_IPMP(ill))
4084 		return (ipmp_ill_get_ipmp_ifindex(ill));
4085 	else
4086 		return (ill->ill_phyint->phyint_ifindex);
4087 }
4088 
4089 
4090 /*
4091  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4092  * that gives a running thread a reference to the ill. This reference must be
4093  * released by the thread when it is done accessing the ill and related
4094  * objects. ill_refcnt can not be used to account for static references
4095  * such as other structures pointing to an ill. Callers must generally
4096  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4097  * or be sure that the ill is not being deleted or changing state before
4098  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4099  * ill won't change any of its critical state such as address, netmask etc.
4100  */
4101 void
4102 ill_refhold(ill_t *ill)
4103 {
4104 	mutex_enter(&ill->ill_lock);
4105 	ill->ill_refcnt++;
4106 	ILL_TRACE_REF(ill);
4107 	mutex_exit(&ill->ill_lock);
4108 }
4109 
4110 void
4111 ill_refhold_locked(ill_t *ill)
4112 {
4113 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4114 	ill->ill_refcnt++;
4115 	ILL_TRACE_REF(ill);
4116 }
4117 
4118 /* Returns true if we managed to get a refhold */
4119 boolean_t
4120 ill_check_and_refhold(ill_t *ill)
4121 {
4122 	mutex_enter(&ill->ill_lock);
4123 	if (!ILL_IS_CONDEMNED(ill)) {
4124 		ill_refhold_locked(ill);
4125 		mutex_exit(&ill->ill_lock);
4126 		return (B_TRUE);
4127 	}
4128 	mutex_exit(&ill->ill_lock);
4129 	return (B_FALSE);
4130 }
4131 
4132 /*
4133  * Must not be called while holding any locks. Otherwise if this is
4134  * the last reference to be released, there is a chance of recursive mutex
4135  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4136  * to restart an ioctl.
4137  */
4138 void
4139 ill_refrele(ill_t *ill)
4140 {
4141 	mutex_enter(&ill->ill_lock);
4142 	ASSERT(ill->ill_refcnt != 0);
4143 	ill->ill_refcnt--;
4144 	ILL_UNTRACE_REF(ill);
4145 	if (ill->ill_refcnt != 0) {
4146 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4147 		mutex_exit(&ill->ill_lock);
4148 		return;
4149 	}
4150 
4151 	/* Drops the ill_lock */
4152 	ipif_ill_refrele_tail(ill);
4153 }
4154 
4155 /*
4156  * Obtain a weak reference count on the ill. This reference ensures the
4157  * ill won't be freed, but the ill may change any of its critical state
4158  * such as netmask, address etc. Returns an error if the ill has started
4159  * closing.
4160  */
4161 boolean_t
4162 ill_waiter_inc(ill_t *ill)
4163 {
4164 	mutex_enter(&ill->ill_lock);
4165 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4166 		mutex_exit(&ill->ill_lock);
4167 		return (B_FALSE);
4168 	}
4169 	ill->ill_waiters++;
4170 	mutex_exit(&ill->ill_lock);
4171 	return (B_TRUE);
4172 }
4173 
4174 void
4175 ill_waiter_dcr(ill_t *ill)
4176 {
4177 	mutex_enter(&ill->ill_lock);
4178 	ill->ill_waiters--;
4179 	if (ill->ill_waiters == 0)
4180 		cv_broadcast(&ill->ill_cv);
4181 	mutex_exit(&ill->ill_lock);
4182 }
4183 
4184 /*
4185  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4186  * driver.  We construct best guess defaults for lower level information that
4187  * we need.  If an interface is brought up without injection of any overriding
4188  * information from outside, we have to be ready to go with these defaults.
4189  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4190  * we primarely want the dl_provider_style.
4191  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4192  * at which point we assume the other part of the information is valid.
4193  */
4194 void
4195 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4196 {
4197 	uchar_t		*brdcst_addr;
4198 	uint_t		brdcst_addr_length, phys_addr_length;
4199 	t_scalar_t	sap_length;
4200 	dl_info_ack_t	*dlia;
4201 	ip_m_t		*ipm;
4202 	dl_qos_cl_sel1_t *sel1;
4203 	int		min_mtu;
4204 
4205 	ASSERT(IAM_WRITER_ILL(ill));
4206 
4207 	/*
4208 	 * Till the ill is fully up  the ill is not globally visible.
4209 	 * So no need for a lock.
4210 	 */
4211 	dlia = (dl_info_ack_t *)mp->b_rptr;
4212 	ill->ill_mactype = dlia->dl_mac_type;
4213 
4214 	ipm = ip_m_lookup(dlia->dl_mac_type);
4215 	if (ipm == NULL) {
4216 		ipm = ip_m_lookup(DL_OTHER);
4217 		ASSERT(ipm != NULL);
4218 	}
4219 	ill->ill_media = ipm;
4220 
4221 	/*
4222 	 * When the new DLPI stuff is ready we'll pull lengths
4223 	 * from dlia.
4224 	 */
4225 	if (dlia->dl_version == DL_VERSION_2) {
4226 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4227 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4228 		    brdcst_addr_length);
4229 		if (brdcst_addr == NULL) {
4230 			brdcst_addr_length = 0;
4231 		}
4232 		sap_length = dlia->dl_sap_length;
4233 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4234 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4235 		    brdcst_addr_length, sap_length, phys_addr_length));
4236 	} else {
4237 		brdcst_addr_length = 6;
4238 		brdcst_addr = ip_six_byte_all_ones;
4239 		sap_length = -2;
4240 		phys_addr_length = brdcst_addr_length;
4241 	}
4242 
4243 	ill->ill_bcast_addr_length = brdcst_addr_length;
4244 	ill->ill_phys_addr_length = phys_addr_length;
4245 	ill->ill_sap_length = sap_length;
4246 
4247 	/*
4248 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4249 	 * but we must ensure a minimum IP MTU is used since other bits of
4250 	 * IP will fly apart otherwise.
4251 	 */
4252 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4253 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4254 	ill->ill_current_frag = ill->ill_max_frag;
4255 	ill->ill_mtu = ill->ill_max_frag;
4256 
4257 	ill->ill_type = ipm->ip_m_type;
4258 
4259 	if (!ill->ill_dlpi_style_set) {
4260 		if (dlia->dl_provider_style == DL_STYLE2)
4261 			ill->ill_needs_attach = 1;
4262 
4263 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4264 
4265 		/*
4266 		 * Allocate the first ipif on this ill.  We don't delay it
4267 		 * further as ioctl handling assumes at least one ipif exists.
4268 		 *
4269 		 * At this point we don't know whether the ill is v4 or v6.
4270 		 * We will know this whan the SIOCSLIFNAME happens and
4271 		 * the correct value for ill_isv6 will be assigned in
4272 		 * ipif_set_values(). We need to hold the ill lock and
4273 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4274 		 * the wakeup.
4275 		 */
4276 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4277 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4278 		mutex_enter(&ill->ill_lock);
4279 		ASSERT(ill->ill_dlpi_style_set == 0);
4280 		ill->ill_dlpi_style_set = 1;
4281 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4282 		cv_broadcast(&ill->ill_cv);
4283 		mutex_exit(&ill->ill_lock);
4284 		freemsg(mp);
4285 		return;
4286 	}
4287 	ASSERT(ill->ill_ipif != NULL);
4288 	/*
4289 	 * We know whether it is IPv4 or IPv6 now, as this is the
4290 	 * second DL_INFO_ACK we are recieving in response to the
4291 	 * DL_INFO_REQ sent in ipif_set_values.
4292 	 */
4293 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4294 	/*
4295 	 * Clear all the flags that were set based on ill_bcast_addr_length
4296 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4297 	 * changed now and we need to re-evaluate.
4298 	 */
4299 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4300 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4301 
4302 	/*
4303 	 * Free ill_bcast_mp as things could have changed now.
4304 	 *
4305 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4306 	 * with no underlying interfaces (and thus an unknown broadcast
4307 	 * address length), but we enforce that an interface is broadcast-
4308 	 * capable as part of allowing it to join a group.
4309 	 */
4310 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4311 		if (ill->ill_bcast_mp != NULL)
4312 			freemsg(ill->ill_bcast_mp);
4313 		ill->ill_net_type = IRE_IF_NORESOLVER;
4314 
4315 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4316 		    ill->ill_phys_addr_length,
4317 		    ill->ill_sap,
4318 		    ill->ill_sap_length);
4319 
4320 		if (ill->ill_isv6)
4321 			/*
4322 			 * Note: xresolv interfaces will eventually need NOARP
4323 			 * set here as well, but that will require those
4324 			 * external resolvers to have some knowledge of
4325 			 * that flag and act appropriately. Not to be changed
4326 			 * at present.
4327 			 */
4328 			ill->ill_flags |= ILLF_NONUD;
4329 		else
4330 			ill->ill_flags |= ILLF_NOARP;
4331 
4332 		if (ill->ill_mactype == SUNW_DL_VNI) {
4333 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4334 		} else if (ill->ill_phys_addr_length == 0 ||
4335 		    ill->ill_mactype == DL_IPV4 ||
4336 		    ill->ill_mactype == DL_IPV6) {
4337 			/*
4338 			 * The underying link is point-to-point, so mark the
4339 			 * interface as such.  We can do IP multicast over
4340 			 * such a link since it transmits all network-layer
4341 			 * packets to the remote side the same way.
4342 			 */
4343 			ill->ill_flags |= ILLF_MULTICAST;
4344 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4345 		}
4346 	} else {
4347 		ill->ill_net_type = IRE_IF_RESOLVER;
4348 		if (ill->ill_bcast_mp != NULL)
4349 			freemsg(ill->ill_bcast_mp);
4350 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4351 		    ill->ill_bcast_addr_length, ill->ill_sap,
4352 		    ill->ill_sap_length);
4353 		/*
4354 		 * Later detect lack of DLPI driver multicast
4355 		 * capability by catching DL_ENABMULTI errors in
4356 		 * ip_rput_dlpi.
4357 		 */
4358 		ill->ill_flags |= ILLF_MULTICAST;
4359 		if (!ill->ill_isv6)
4360 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4361 	}
4362 
4363 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4364 	if (ill->ill_mactype == SUNW_DL_IPMP)
4365 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4366 
4367 	/* By default an interface does not support any CoS marking */
4368 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4369 
4370 	/*
4371 	 * If we get QoS information in DL_INFO_ACK, the device supports
4372 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4373 	 */
4374 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4375 	    dlia->dl_qos_length);
4376 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4377 		ill->ill_flags |= ILLF_COS_ENABLED;
4378 	}
4379 
4380 	/* Clear any previous error indication. */
4381 	ill->ill_error = 0;
4382 	freemsg(mp);
4383 }
4384 
4385 /*
4386  * Perform various checks to verify that an address would make sense as a
4387  * local, remote, or subnet interface address.
4388  */
4389 static boolean_t
4390 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4391 {
4392 	ipaddr_t	net_mask;
4393 
4394 	/*
4395 	 * Don't allow all zeroes, or all ones, but allow
4396 	 * all ones netmask.
4397 	 */
4398 	if ((net_mask = ip_net_mask(addr)) == 0)
4399 		return (B_FALSE);
4400 	/* A given netmask overrides the "guess" netmask */
4401 	if (subnet_mask != 0)
4402 		net_mask = subnet_mask;
4403 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4404 	    (addr == (addr | ~net_mask)))) {
4405 		return (B_FALSE);
4406 	}
4407 
4408 	/*
4409 	 * Even if the netmask is all ones, we do not allow address to be
4410 	 * 255.255.255.255
4411 	 */
4412 	if (addr == INADDR_BROADCAST)
4413 		return (B_FALSE);
4414 
4415 	if (CLASSD(addr))
4416 		return (B_FALSE);
4417 
4418 	return (B_TRUE);
4419 }
4420 
4421 #define	V6_IPIF_LINKLOCAL(p)	\
4422 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4423 
4424 /*
4425  * Compare two given ipifs and check if the second one is better than
4426  * the first one using the order of preference (not taking deprecated
4427  * into acount) specified in ipif_lookup_multicast().
4428  */
4429 static boolean_t
4430 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4431 {
4432 	/* Check the least preferred first. */
4433 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4434 		/* If both ipifs are the same, use the first one. */
4435 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4436 			return (B_FALSE);
4437 		else
4438 			return (B_TRUE);
4439 	}
4440 
4441 	/* For IPv6, check for link local address. */
4442 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4443 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4444 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4445 			/* The second one is equal or less preferred. */
4446 			return (B_FALSE);
4447 		} else {
4448 			return (B_TRUE);
4449 		}
4450 	}
4451 
4452 	/* Then check for point to point interface. */
4453 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4454 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4455 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4456 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4457 			return (B_FALSE);
4458 		} else {
4459 			return (B_TRUE);
4460 		}
4461 	}
4462 
4463 	/* old_ipif is a normal interface, so no need to use the new one. */
4464 	return (B_FALSE);
4465 }
4466 
4467 /*
4468  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4469  * The ipif must be up, and its ill must multicast-capable, not
4470  * condemned, not an underlying interface in an IPMP group, and
4471  * not a VNI interface.  Order of preference:
4472  *
4473  * 	1a. normal
4474  * 	1b. normal, but deprecated
4475  * 	2a. point to point
4476  * 	2b. point to point, but deprecated
4477  * 	3a. link local
4478  * 	3b. link local, but deprecated
4479  * 	4. loopback.
4480  */
4481 static ipif_t *
4482 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4483 {
4484 	ill_t			*ill;
4485 	ill_walk_context_t	ctx;
4486 	ipif_t			*ipif;
4487 	ipif_t			*saved_ipif = NULL;
4488 	ipif_t			*dep_ipif = NULL;
4489 
4490 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4491 	if (isv6)
4492 		ill = ILL_START_WALK_V6(&ctx, ipst);
4493 	else
4494 		ill = ILL_START_WALK_V4(&ctx, ipst);
4495 
4496 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4497 		mutex_enter(&ill->ill_lock);
4498 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4499 		    ILL_IS_CONDEMNED(ill) ||
4500 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4501 			mutex_exit(&ill->ill_lock);
4502 			continue;
4503 		}
4504 		for (ipif = ill->ill_ipif; ipif != NULL;
4505 		    ipif = ipif->ipif_next) {
4506 			if (zoneid != ipif->ipif_zoneid &&
4507 			    zoneid != ALL_ZONES &&
4508 			    ipif->ipif_zoneid != ALL_ZONES) {
4509 				continue;
4510 			}
4511 			if (!(ipif->ipif_flags & IPIF_UP) ||
4512 			    IPIF_IS_CONDEMNED(ipif)) {
4513 				continue;
4514 			}
4515 
4516 			/*
4517 			 * Found one candidate.  If it is deprecated,
4518 			 * remember it in dep_ipif.  If it is not deprecated,
4519 			 * remember it in saved_ipif.
4520 			 */
4521 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4522 				if (dep_ipif == NULL) {
4523 					dep_ipif = ipif;
4524 				} else if (ipif_comp_multi(dep_ipif, ipif,
4525 				    isv6)) {
4526 					/*
4527 					 * If the previous dep_ipif does not
4528 					 * belong to the same ill, we've done
4529 					 * a ipif_refhold() on it.  So we need
4530 					 * to release it.
4531 					 */
4532 					if (dep_ipif->ipif_ill != ill)
4533 						ipif_refrele(dep_ipif);
4534 					dep_ipif = ipif;
4535 				}
4536 				continue;
4537 			}
4538 			if (saved_ipif == NULL) {
4539 				saved_ipif = ipif;
4540 			} else {
4541 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4542 					if (saved_ipif->ipif_ill != ill)
4543 						ipif_refrele(saved_ipif);
4544 					saved_ipif = ipif;
4545 				}
4546 			}
4547 		}
4548 		/*
4549 		 * Before going to the next ill, do a ipif_refhold() on the
4550 		 * saved ones.
4551 		 */
4552 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4553 			ipif_refhold_locked(saved_ipif);
4554 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4555 			ipif_refhold_locked(dep_ipif);
4556 		mutex_exit(&ill->ill_lock);
4557 	}
4558 	rw_exit(&ipst->ips_ill_g_lock);
4559 
4560 	/*
4561 	 * If we have only the saved_ipif, return it.  But if we have both
4562 	 * saved_ipif and dep_ipif, check to see which one is better.
4563 	 */
4564 	if (saved_ipif != NULL) {
4565 		if (dep_ipif != NULL) {
4566 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4567 				ipif_refrele(saved_ipif);
4568 				return (dep_ipif);
4569 			} else {
4570 				ipif_refrele(dep_ipif);
4571 				return (saved_ipif);
4572 			}
4573 		}
4574 		return (saved_ipif);
4575 	} else {
4576 		return (dep_ipif);
4577 	}
4578 }
4579 
4580 ill_t *
4581 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4582 {
4583 	ipif_t *ipif;
4584 	ill_t *ill;
4585 
4586 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4587 	if (ipif == NULL)
4588 		return (NULL);
4589 
4590 	ill = ipif->ipif_ill;
4591 	ill_refhold(ill);
4592 	ipif_refrele(ipif);
4593 	return (ill);
4594 }
4595 
4596 /*
4597  * This function is called when an application does not specify an interface
4598  * to be used for multicast traffic (joining a group/sending data).  It
4599  * calls ire_lookup_multi() to look for an interface route for the
4600  * specified multicast group.  Doing this allows the administrator to add
4601  * prefix routes for multicast to indicate which interface to be used for
4602  * multicast traffic in the above scenario.  The route could be for all
4603  * multicast (224.0/4), for a single multicast group (a /32 route) or
4604  * anything in between.  If there is no such multicast route, we just find
4605  * any multicast capable interface and return it.  The returned ipif
4606  * is refhold'ed.
4607  *
4608  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4609  * unicast table. This is used by CGTP.
4610  */
4611 ill_t *
4612 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4613     boolean_t *multirtp, ipaddr_t *setsrcp)
4614 {
4615 	ill_t			*ill;
4616 
4617 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4618 	if (ill != NULL)
4619 		return (ill);
4620 
4621 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4622 }
4623 
4624 /*
4625  * Look for an ipif with the specified interface address and destination.
4626  * The destination address is used only for matching point-to-point interfaces.
4627  */
4628 ipif_t *
4629 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4630 {
4631 	ipif_t	*ipif;
4632 	ill_t	*ill;
4633 	ill_walk_context_t ctx;
4634 
4635 	/*
4636 	 * First match all the point-to-point interfaces
4637 	 * before looking at non-point-to-point interfaces.
4638 	 * This is done to avoid returning non-point-to-point
4639 	 * ipif instead of unnumbered point-to-point ipif.
4640 	 */
4641 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4642 	ill = ILL_START_WALK_V4(&ctx, ipst);
4643 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4644 		mutex_enter(&ill->ill_lock);
4645 		for (ipif = ill->ill_ipif; ipif != NULL;
4646 		    ipif = ipif->ipif_next) {
4647 			/* Allow the ipif to be down */
4648 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4649 			    (ipif->ipif_lcl_addr == if_addr) &&
4650 			    (ipif->ipif_pp_dst_addr == dst)) {
4651 				if (!IPIF_IS_CONDEMNED(ipif)) {
4652 					ipif_refhold_locked(ipif);
4653 					mutex_exit(&ill->ill_lock);
4654 					rw_exit(&ipst->ips_ill_g_lock);
4655 					return (ipif);
4656 				}
4657 			}
4658 		}
4659 		mutex_exit(&ill->ill_lock);
4660 	}
4661 	rw_exit(&ipst->ips_ill_g_lock);
4662 
4663 	/* lookup the ipif based on interface address */
4664 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4665 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4666 	return (ipif);
4667 }
4668 
4669 /*
4670  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4671  */
4672 static ipif_t *
4673 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4674     zoneid_t zoneid, ip_stack_t *ipst)
4675 {
4676 	ipif_t  *ipif;
4677 	ill_t   *ill;
4678 	boolean_t ptp = B_FALSE;
4679 	ill_walk_context_t	ctx;
4680 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4681 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4682 
4683 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4684 	/*
4685 	 * Repeat twice, first based on local addresses and
4686 	 * next time for pointopoint.
4687 	 */
4688 repeat:
4689 	ill = ILL_START_WALK_V4(&ctx, ipst);
4690 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4691 		if (match_ill != NULL && ill != match_ill &&
4692 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4693 			continue;
4694 		}
4695 		mutex_enter(&ill->ill_lock);
4696 		for (ipif = ill->ill_ipif; ipif != NULL;
4697 		    ipif = ipif->ipif_next) {
4698 			if (zoneid != ALL_ZONES &&
4699 			    zoneid != ipif->ipif_zoneid &&
4700 			    ipif->ipif_zoneid != ALL_ZONES)
4701 				continue;
4702 
4703 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4704 				continue;
4705 
4706 			/* Allow the ipif to be down */
4707 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4708 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4709 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4710 			    (ipif->ipif_pp_dst_addr == addr))) {
4711 				if (!IPIF_IS_CONDEMNED(ipif)) {
4712 					ipif_refhold_locked(ipif);
4713 					mutex_exit(&ill->ill_lock);
4714 					rw_exit(&ipst->ips_ill_g_lock);
4715 					return (ipif);
4716 				}
4717 			}
4718 		}
4719 		mutex_exit(&ill->ill_lock);
4720 	}
4721 
4722 	/* If we already did the ptp case, then we are done */
4723 	if (ptp) {
4724 		rw_exit(&ipst->ips_ill_g_lock);
4725 		return (NULL);
4726 	}
4727 	ptp = B_TRUE;
4728 	goto repeat;
4729 }
4730 
4731 /*
4732  * Lookup an ipif with the specified address.  For point-to-point links we
4733  * look for matches on either the destination address or the local address,
4734  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4735  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4736  * (or illgrp if `match_ill' is in an IPMP group).
4737  */
4738 ipif_t *
4739 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4740     ip_stack_t *ipst)
4741 {
4742 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4743 	    zoneid, ipst));
4744 }
4745 
4746 /*
4747  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4748  * except that we will only return an address if it is not marked as
4749  * IPIF_DUPLICATE
4750  */
4751 ipif_t *
4752 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4753     ip_stack_t *ipst)
4754 {
4755 	return (ipif_lookup_addr_common(addr, match_ill,
4756 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4757 	    zoneid, ipst));
4758 }
4759 
4760 /*
4761  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4762  * `match_ill' across the IPMP group.  This function is only needed in some
4763  * corner-cases; almost everything should use ipif_lookup_addr().
4764  */
4765 ipif_t *
4766 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4767 {
4768 	ASSERT(match_ill != NULL);
4769 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4770 	    ipst));
4771 }
4772 
4773 /*
4774  * Look for an ipif with the specified address. For point-point links
4775  * we look for matches on either the destination address and the local
4776  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4777  * is set.
4778  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4779  * ill (or illgrp if `match_ill' is in an IPMP group).
4780  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4781  */
4782 zoneid_t
4783 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4784 {
4785 	zoneid_t zoneid;
4786 	ipif_t  *ipif;
4787 	ill_t   *ill;
4788 	boolean_t ptp = B_FALSE;
4789 	ill_walk_context_t	ctx;
4790 
4791 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4792 	/*
4793 	 * Repeat twice, first based on local addresses and
4794 	 * next time for pointopoint.
4795 	 */
4796 repeat:
4797 	ill = ILL_START_WALK_V4(&ctx, ipst);
4798 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4799 		if (match_ill != NULL && ill != match_ill &&
4800 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4801 			continue;
4802 		}
4803 		mutex_enter(&ill->ill_lock);
4804 		for (ipif = ill->ill_ipif; ipif != NULL;
4805 		    ipif = ipif->ipif_next) {
4806 			/* Allow the ipif to be down */
4807 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4808 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4809 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4810 			    (ipif->ipif_pp_dst_addr == addr)) &&
4811 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4812 				zoneid = ipif->ipif_zoneid;
4813 				mutex_exit(&ill->ill_lock);
4814 				rw_exit(&ipst->ips_ill_g_lock);
4815 				/*
4816 				 * If ipif_zoneid was ALL_ZONES then we have
4817 				 * a trusted extensions shared IP address.
4818 				 * In that case GLOBAL_ZONEID works to send.
4819 				 */
4820 				if (zoneid == ALL_ZONES)
4821 					zoneid = GLOBAL_ZONEID;
4822 				return (zoneid);
4823 			}
4824 		}
4825 		mutex_exit(&ill->ill_lock);
4826 	}
4827 
4828 	/* If we already did the ptp case, then we are done */
4829 	if (ptp) {
4830 		rw_exit(&ipst->ips_ill_g_lock);
4831 		return (ALL_ZONES);
4832 	}
4833 	ptp = B_TRUE;
4834 	goto repeat;
4835 }
4836 
4837 /*
4838  * Look for an ipif that matches the specified remote address i.e. the
4839  * ipif that would receive the specified packet.
4840  * First look for directly connected interfaces and then do a recursive
4841  * IRE lookup and pick the first ipif corresponding to the source address in the
4842  * ire.
4843  * Returns: held ipif
4844  *
4845  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4846  */
4847 ipif_t *
4848 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4849 {
4850 	ipif_t	*ipif;
4851 
4852 	ASSERT(!ill->ill_isv6);
4853 
4854 	/*
4855 	 * Someone could be changing this ipif currently or change it
4856 	 * after we return this. Thus  a few packets could use the old
4857 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4858 	 * will atomically be updated or cleaned up with the new value
4859 	 * Thus we don't need a lock to check the flags or other attrs below.
4860 	 */
4861 	mutex_enter(&ill->ill_lock);
4862 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4863 		if (IPIF_IS_CONDEMNED(ipif))
4864 			continue;
4865 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4866 		    ipif->ipif_zoneid != ALL_ZONES)
4867 			continue;
4868 		/* Allow the ipif to be down */
4869 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4870 			if ((ipif->ipif_pp_dst_addr == addr) ||
4871 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4872 			    ipif->ipif_lcl_addr == addr)) {
4873 				ipif_refhold_locked(ipif);
4874 				mutex_exit(&ill->ill_lock);
4875 				return (ipif);
4876 			}
4877 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4878 			ipif_refhold_locked(ipif);
4879 			mutex_exit(&ill->ill_lock);
4880 			return (ipif);
4881 		}
4882 	}
4883 	mutex_exit(&ill->ill_lock);
4884 	/*
4885 	 * For a remote destination it isn't possible to nail down a particular
4886 	 * ipif.
4887 	 */
4888 
4889 	/* Pick the first interface */
4890 	ipif = ipif_get_next_ipif(NULL, ill);
4891 	return (ipif);
4892 }
4893 
4894 /*
4895  * This func does not prevent refcnt from increasing. But if
4896  * the caller has taken steps to that effect, then this func
4897  * can be used to determine whether the ill has become quiescent
4898  */
4899 static boolean_t
4900 ill_is_quiescent(ill_t *ill)
4901 {
4902 	ipif_t	*ipif;
4903 
4904 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4905 
4906 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4907 		if (ipif->ipif_refcnt != 0)
4908 			return (B_FALSE);
4909 	}
4910 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4911 		return (B_FALSE);
4912 	}
4913 	return (B_TRUE);
4914 }
4915 
4916 boolean_t
4917 ill_is_freeable(ill_t *ill)
4918 {
4919 	ipif_t	*ipif;
4920 
4921 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4922 
4923 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4924 		if (ipif->ipif_refcnt != 0) {
4925 			return (B_FALSE);
4926 		}
4927 	}
4928 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4929 		return (B_FALSE);
4930 	}
4931 	return (B_TRUE);
4932 }
4933 
4934 /*
4935  * This func does not prevent refcnt from increasing. But if
4936  * the caller has taken steps to that effect, then this func
4937  * can be used to determine whether the ipif has become quiescent
4938  */
4939 static boolean_t
4940 ipif_is_quiescent(ipif_t *ipif)
4941 {
4942 	ill_t *ill;
4943 
4944 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4945 
4946 	if (ipif->ipif_refcnt != 0)
4947 		return (B_FALSE);
4948 
4949 	ill = ipif->ipif_ill;
4950 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4951 	    ill->ill_logical_down) {
4952 		return (B_TRUE);
4953 	}
4954 
4955 	/* This is the last ipif going down or being deleted on this ill */
4956 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4957 		return (B_FALSE);
4958 	}
4959 
4960 	return (B_TRUE);
4961 }
4962 
4963 /*
4964  * return true if the ipif can be destroyed: the ipif has to be quiescent
4965  * with zero references from ire/ilm to it.
4966  */
4967 static boolean_t
4968 ipif_is_freeable(ipif_t *ipif)
4969 {
4970 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4971 	ASSERT(ipif->ipif_id != 0);
4972 	return (ipif->ipif_refcnt == 0);
4973 }
4974 
4975 /*
4976  * The ipif/ill/ire has been refreled. Do the tail processing.
4977  * Determine if the ipif or ill in question has become quiescent and if so
4978  * wakeup close and/or restart any queued pending ioctl that is waiting
4979  * for the ipif_down (or ill_down)
4980  */
4981 void
4982 ipif_ill_refrele_tail(ill_t *ill)
4983 {
4984 	mblk_t	*mp;
4985 	conn_t	*connp;
4986 	ipsq_t	*ipsq;
4987 	ipxop_t	*ipx;
4988 	ipif_t	*ipif;
4989 	dl_notify_ind_t *dlindp;
4990 
4991 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4992 
4993 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4994 		/* ip_modclose() may be waiting */
4995 		cv_broadcast(&ill->ill_cv);
4996 	}
4997 
4998 	ipsq = ill->ill_phyint->phyint_ipsq;
4999 	mutex_enter(&ipsq->ipsq_lock);
5000 	ipx = ipsq->ipsq_xop;
5001 	mutex_enter(&ipx->ipx_lock);
5002 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
5003 		goto unlock;
5004 
5005 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
5006 
5007 	ipif = ipx->ipx_pending_ipif;
5008 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
5009 		goto unlock;
5010 
5011 	switch (ipx->ipx_waitfor) {
5012 	case IPIF_DOWN:
5013 		if (!ipif_is_quiescent(ipif))
5014 			goto unlock;
5015 		break;
5016 	case IPIF_FREE:
5017 		if (!ipif_is_freeable(ipif))
5018 			goto unlock;
5019 		break;
5020 	case ILL_DOWN:
5021 		if (!ill_is_quiescent(ill))
5022 			goto unlock;
5023 		break;
5024 	case ILL_FREE:
5025 		/*
5026 		 * ILL_FREE is only for loopback; normal ill teardown waits
5027 		 * synchronously in ip_modclose() without using ipx_waitfor,
5028 		 * handled by the cv_broadcast() at the top of this function.
5029 		 */
5030 		if (!ill_is_freeable(ill))
5031 			goto unlock;
5032 		break;
5033 	default:
5034 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
5035 		    (void *)ipsq, ipx->ipx_waitfor);
5036 	}
5037 
5038 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
5039 	mutex_exit(&ipx->ipx_lock);
5040 	mp = ipsq_pending_mp_get(ipsq, &connp);
5041 	mutex_exit(&ipsq->ipsq_lock);
5042 	mutex_exit(&ill->ill_lock);
5043 
5044 	ASSERT(mp != NULL);
5045 	/*
5046 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5047 	 * we can only get here when the current operation decides it
5048 	 * it needs to quiesce via ipsq_pending_mp_add().
5049 	 */
5050 	switch (mp->b_datap->db_type) {
5051 	case M_PCPROTO:
5052 	case M_PROTO:
5053 		/*
5054 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5055 		 */
5056 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5057 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5058 
5059 		switch (dlindp->dl_notification) {
5060 		case DL_NOTE_PHYS_ADDR:
5061 			qwriter_ip(ill, ill->ill_rq, mp,
5062 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5063 			return;
5064 		case DL_NOTE_REPLUMB:
5065 			qwriter_ip(ill, ill->ill_rq, mp,
5066 			    ill_replumb_tail, CUR_OP, B_TRUE);
5067 			return;
5068 		default:
5069 			ASSERT(0);
5070 			ill_refrele(ill);
5071 		}
5072 		break;
5073 
5074 	case M_ERROR:
5075 	case M_HANGUP:
5076 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5077 		    B_TRUE);
5078 		return;
5079 
5080 	case M_IOCTL:
5081 	case M_IOCDATA:
5082 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5083 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5084 		return;
5085 
5086 	default:
5087 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5088 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5089 	}
5090 	return;
5091 unlock:
5092 	mutex_exit(&ipsq->ipsq_lock);
5093 	mutex_exit(&ipx->ipx_lock);
5094 	mutex_exit(&ill->ill_lock);
5095 }
5096 
5097 #ifdef DEBUG
5098 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5099 static void
5100 th_trace_rrecord(th_trace_t *th_trace)
5101 {
5102 	tr_buf_t *tr_buf;
5103 	uint_t lastref;
5104 
5105 	lastref = th_trace->th_trace_lastref;
5106 	lastref++;
5107 	if (lastref == TR_BUF_MAX)
5108 		lastref = 0;
5109 	th_trace->th_trace_lastref = lastref;
5110 	tr_buf = &th_trace->th_trbuf[lastref];
5111 	tr_buf->tr_time = ddi_get_lbolt();
5112 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5113 }
5114 
5115 static void
5116 th_trace_free(void *value)
5117 {
5118 	th_trace_t *th_trace = value;
5119 
5120 	ASSERT(th_trace->th_refcnt == 0);
5121 	kmem_free(th_trace, sizeof (*th_trace));
5122 }
5123 
5124 /*
5125  * Find or create the per-thread hash table used to track object references.
5126  * The ipst argument is NULL if we shouldn't allocate.
5127  *
5128  * Accesses per-thread data, so there's no need to lock here.
5129  */
5130 static mod_hash_t *
5131 th_trace_gethash(ip_stack_t *ipst)
5132 {
5133 	th_hash_t *thh;
5134 
5135 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5136 		mod_hash_t *mh;
5137 		char name[256];
5138 		size_t objsize, rshift;
5139 		int retv;
5140 
5141 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5142 			return (NULL);
5143 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5144 		    (void *)curthread);
5145 
5146 		/*
5147 		 * We use mod_hash_create_extended here rather than the more
5148 		 * obvious mod_hash_create_ptrhash because the latter has a
5149 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5150 		 * block.
5151 		 */
5152 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5153 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5154 		rshift = highbit(objsize);
5155 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5156 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5157 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5158 		if (mh == NULL) {
5159 			kmem_free(thh, sizeof (*thh));
5160 			return (NULL);
5161 		}
5162 		thh->thh_hash = mh;
5163 		thh->thh_ipst = ipst;
5164 		/*
5165 		 * We trace ills, ipifs, ires, and nces.  All of these are
5166 		 * per-IP-stack, so the lock on the thread list is as well.
5167 		 */
5168 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5169 		list_insert_tail(&ip_thread_list, thh);
5170 		rw_exit(&ip_thread_rwlock);
5171 		retv = tsd_set(ip_thread_data, thh);
5172 		ASSERT(retv == 0);
5173 	}
5174 	return (thh != NULL ? thh->thh_hash : NULL);
5175 }
5176 
5177 boolean_t
5178 th_trace_ref(const void *obj, ip_stack_t *ipst)
5179 {
5180 	th_trace_t *th_trace;
5181 	mod_hash_t *mh;
5182 	mod_hash_val_t val;
5183 
5184 	if ((mh = th_trace_gethash(ipst)) == NULL)
5185 		return (B_FALSE);
5186 
5187 	/*
5188 	 * Attempt to locate the trace buffer for this obj and thread.
5189 	 * If it does not exist, then allocate a new trace buffer and
5190 	 * insert into the hash.
5191 	 */
5192 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5193 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5194 		if (th_trace == NULL)
5195 			return (B_FALSE);
5196 
5197 		th_trace->th_id = curthread;
5198 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5199 		    (mod_hash_val_t)th_trace) != 0) {
5200 			kmem_free(th_trace, sizeof (th_trace_t));
5201 			return (B_FALSE);
5202 		}
5203 	} else {
5204 		th_trace = (th_trace_t *)val;
5205 	}
5206 
5207 	ASSERT(th_trace->th_refcnt >= 0 &&
5208 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5209 
5210 	th_trace->th_refcnt++;
5211 	th_trace_rrecord(th_trace);
5212 	return (B_TRUE);
5213 }
5214 
5215 /*
5216  * For the purpose of tracing a reference release, we assume that global
5217  * tracing is always on and that the same thread initiated the reference hold
5218  * is releasing.
5219  */
5220 void
5221 th_trace_unref(const void *obj)
5222 {
5223 	int retv;
5224 	mod_hash_t *mh;
5225 	th_trace_t *th_trace;
5226 	mod_hash_val_t val;
5227 
5228 	mh = th_trace_gethash(NULL);
5229 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5230 	ASSERT(retv == 0);
5231 	th_trace = (th_trace_t *)val;
5232 
5233 	ASSERT(th_trace->th_refcnt > 0);
5234 	th_trace->th_refcnt--;
5235 	th_trace_rrecord(th_trace);
5236 }
5237 
5238 /*
5239  * If tracing has been disabled, then we assume that the reference counts are
5240  * now useless, and we clear them out before destroying the entries.
5241  */
5242 void
5243 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5244 {
5245 	th_hash_t	*thh;
5246 	mod_hash_t	*mh;
5247 	mod_hash_val_t	val;
5248 	th_trace_t	*th_trace;
5249 	int		retv;
5250 
5251 	rw_enter(&ip_thread_rwlock, RW_READER);
5252 	for (thh = list_head(&ip_thread_list); thh != NULL;
5253 	    thh = list_next(&ip_thread_list, thh)) {
5254 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5255 		    &val) == 0) {
5256 			th_trace = (th_trace_t *)val;
5257 			if (trace_disable)
5258 				th_trace->th_refcnt = 0;
5259 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5260 			ASSERT(retv == 0);
5261 		}
5262 	}
5263 	rw_exit(&ip_thread_rwlock);
5264 }
5265 
5266 void
5267 ipif_trace_ref(ipif_t *ipif)
5268 {
5269 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5270 
5271 	if (ipif->ipif_trace_disable)
5272 		return;
5273 
5274 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5275 		ipif->ipif_trace_disable = B_TRUE;
5276 		ipif_trace_cleanup(ipif);
5277 	}
5278 }
5279 
5280 void
5281 ipif_untrace_ref(ipif_t *ipif)
5282 {
5283 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5284 
5285 	if (!ipif->ipif_trace_disable)
5286 		th_trace_unref(ipif);
5287 }
5288 
5289 void
5290 ill_trace_ref(ill_t *ill)
5291 {
5292 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5293 
5294 	if (ill->ill_trace_disable)
5295 		return;
5296 
5297 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5298 		ill->ill_trace_disable = B_TRUE;
5299 		ill_trace_cleanup(ill);
5300 	}
5301 }
5302 
5303 void
5304 ill_untrace_ref(ill_t *ill)
5305 {
5306 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5307 
5308 	if (!ill->ill_trace_disable)
5309 		th_trace_unref(ill);
5310 }
5311 
5312 /*
5313  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5314  * failure, ipif_trace_disable is set.
5315  */
5316 static void
5317 ipif_trace_cleanup(const ipif_t *ipif)
5318 {
5319 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5320 }
5321 
5322 /*
5323  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5324  * failure, ill_trace_disable is set.
5325  */
5326 static void
5327 ill_trace_cleanup(const ill_t *ill)
5328 {
5329 	th_trace_cleanup(ill, ill->ill_trace_disable);
5330 }
5331 #endif /* DEBUG */
5332 
5333 void
5334 ipif_refhold_locked(ipif_t *ipif)
5335 {
5336 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5337 	ipif->ipif_refcnt++;
5338 	IPIF_TRACE_REF(ipif);
5339 }
5340 
5341 void
5342 ipif_refhold(ipif_t *ipif)
5343 {
5344 	ill_t	*ill;
5345 
5346 	ill = ipif->ipif_ill;
5347 	mutex_enter(&ill->ill_lock);
5348 	ipif->ipif_refcnt++;
5349 	IPIF_TRACE_REF(ipif);
5350 	mutex_exit(&ill->ill_lock);
5351 }
5352 
5353 /*
5354  * Must not be called while holding any locks. Otherwise if this is
5355  * the last reference to be released there is a chance of recursive mutex
5356  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5357  * to restart an ioctl.
5358  */
5359 void
5360 ipif_refrele(ipif_t *ipif)
5361 {
5362 	ill_t	*ill;
5363 
5364 	ill = ipif->ipif_ill;
5365 
5366 	mutex_enter(&ill->ill_lock);
5367 	ASSERT(ipif->ipif_refcnt != 0);
5368 	ipif->ipif_refcnt--;
5369 	IPIF_UNTRACE_REF(ipif);
5370 	if (ipif->ipif_refcnt != 0) {
5371 		mutex_exit(&ill->ill_lock);
5372 		return;
5373 	}
5374 
5375 	/* Drops the ill_lock */
5376 	ipif_ill_refrele_tail(ill);
5377 }
5378 
5379 ipif_t *
5380 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5381 {
5382 	ipif_t	*ipif;
5383 
5384 	mutex_enter(&ill->ill_lock);
5385 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5386 	    ipif != NULL; ipif = ipif->ipif_next) {
5387 		if (IPIF_IS_CONDEMNED(ipif))
5388 			continue;
5389 		ipif_refhold_locked(ipif);
5390 		mutex_exit(&ill->ill_lock);
5391 		return (ipif);
5392 	}
5393 	mutex_exit(&ill->ill_lock);
5394 	return (NULL);
5395 }
5396 
5397 /*
5398  * TODO: make this table extendible at run time
5399  * Return a pointer to the mac type info for 'mac_type'
5400  */
5401 static ip_m_t *
5402 ip_m_lookup(t_uscalar_t mac_type)
5403 {
5404 	ip_m_t	*ipm;
5405 
5406 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5407 		if (ipm->ip_m_mac_type == mac_type)
5408 			return (ipm);
5409 	return (NULL);
5410 }
5411 
5412 /*
5413  * Make a link layer address from the multicast IP address *addr.
5414  * To form the link layer address, invoke the ip_m_v*mapping function
5415  * associated with the link-layer type.
5416  */
5417 void
5418 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5419 {
5420 	ip_m_t *ipm;
5421 
5422 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5423 		return;
5424 
5425 	ASSERT(addr != NULL);
5426 
5427 	ipm = ip_m_lookup(ill->ill_mactype);
5428 	if (ipm == NULL ||
5429 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5430 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5431 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5432 		    ill->ill_name, ill->ill_mactype));
5433 		return;
5434 	}
5435 	if (ill->ill_isv6)
5436 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5437 	else
5438 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5439 }
5440 
5441 /*
5442  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5443  * ill is passed in to associate it with the correct interface.
5444  * If ire_arg is set, then we return the held IRE in that location.
5445  */
5446 int
5447 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5448     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5449     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5450 {
5451 	ire_t	*ire, *nire;
5452 	ire_t	*gw_ire = NULL;
5453 	ipif_t	*ipif = NULL;
5454 	uint_t	type;
5455 	int	match_flags = MATCH_IRE_TYPE;
5456 	tsol_gc_t *gc = NULL;
5457 	tsol_gcgrp_t *gcgrp = NULL;
5458 	boolean_t gcgrp_xtraref = B_FALSE;
5459 	boolean_t cgtp_broadcast;
5460 	boolean_t unbound = B_FALSE;
5461 
5462 	ip1dbg(("ip_rt_add:"));
5463 
5464 	if (ire_arg != NULL)
5465 		*ire_arg = NULL;
5466 
5467 	/*
5468 	 * If this is the case of RTF_HOST being set, then we set the netmask
5469 	 * to all ones (regardless if one was supplied).
5470 	 */
5471 	if (flags & RTF_HOST)
5472 		mask = IP_HOST_MASK;
5473 
5474 	/*
5475 	 * Prevent routes with a zero gateway from being created (since
5476 	 * interfaces can currently be plumbed and brought up no assigned
5477 	 * address).
5478 	 */
5479 	if (gw_addr == 0)
5480 		return (ENETUNREACH);
5481 	/*
5482 	 * Get the ipif, if any, corresponding to the gw_addr
5483 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5484 	 * we match on the gatway and destination to handle unnumbered pt-pt
5485 	 * interfaces.
5486 	 */
5487 	if (ill != NULL)
5488 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5489 	else
5490 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5491 	if (ipif != NULL) {
5492 		if (IS_VNI(ipif->ipif_ill)) {
5493 			ipif_refrele(ipif);
5494 			return (EINVAL);
5495 		}
5496 	}
5497 
5498 	/*
5499 	 * GateD will attempt to create routes with a loopback interface
5500 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5501 	 * these routes to be added, but create them as interface routes
5502 	 * since the gateway is an interface address.
5503 	 */
5504 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5505 		flags &= ~RTF_GATEWAY;
5506 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5507 		    mask == IP_HOST_MASK) {
5508 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5509 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5510 			    NULL);
5511 			if (ire != NULL) {
5512 				ire_refrele(ire);
5513 				ipif_refrele(ipif);
5514 				return (EEXIST);
5515 			}
5516 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5517 			    "for 0x%x\n", (void *)ipif,
5518 			    ipif->ipif_ire_type,
5519 			    ntohl(ipif->ipif_lcl_addr)));
5520 			ire = ire_create(
5521 			    (uchar_t *)&dst_addr,	/* dest address */
5522 			    (uchar_t *)&mask,		/* mask */
5523 			    NULL,			/* no gateway */
5524 			    ipif->ipif_ire_type,	/* LOOPBACK */
5525 			    ipif->ipif_ill,
5526 			    zoneid,
5527 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5528 			    NULL,
5529 			    ipst);
5530 
5531 			if (ire == NULL) {
5532 				ipif_refrele(ipif);
5533 				return (ENOMEM);
5534 			}
5535 			/* src address assigned by the caller? */
5536 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5537 				ire->ire_setsrc_addr = src_addr;
5538 
5539 			nire = ire_add(ire);
5540 			if (nire == NULL) {
5541 				/*
5542 				 * In the result of failure, ire_add() will have
5543 				 * already deleted the ire in question, so there
5544 				 * is no need to do that here.
5545 				 */
5546 				ipif_refrele(ipif);
5547 				return (ENOMEM);
5548 			}
5549 			/*
5550 			 * Check if it was a duplicate entry. This handles
5551 			 * the case of two racing route adds for the same route
5552 			 */
5553 			if (nire != ire) {
5554 				ASSERT(nire->ire_identical_ref > 1);
5555 				ire_delete(nire);
5556 				ire_refrele(nire);
5557 				ipif_refrele(ipif);
5558 				return (EEXIST);
5559 			}
5560 			ire = nire;
5561 			goto save_ire;
5562 		}
5563 	}
5564 
5565 	/*
5566 	 * The routes for multicast with CGTP are quite special in that
5567 	 * the gateway is the local interface address, yet RTF_GATEWAY
5568 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5569 	 * this undocumented and unusual use of multicast routes.
5570 	 */
5571 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5572 		flags &= ~RTF_GATEWAY;
5573 
5574 	/*
5575 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5576 	 * and the gateway address provided is one of the system's interface
5577 	 * addresses.  By using the routing socket interface and supplying an
5578 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5579 	 * specifying an interface route to be created is available which uses
5580 	 * the interface index that specifies the outgoing interface rather than
5581 	 * the address of an outgoing interface (which may not be able to
5582 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5583 	 * flag, routes can be specified which not only specify the next-hop to
5584 	 * be used when routing to a certain prefix, but also which outgoing
5585 	 * interface should be used.
5586 	 *
5587 	 * Previously, interfaces would have unique addresses assigned to them
5588 	 * and so the address assigned to a particular interface could be used
5589 	 * to identify a particular interface.  One exception to this was the
5590 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5591 	 *
5592 	 * With the advent of IPv6 and its link-local addresses, this
5593 	 * restriction was relaxed and interfaces could share addresses between
5594 	 * themselves.  In fact, typically all of the link-local interfaces on
5595 	 * an IPv6 node or router will have the same link-local address.  In
5596 	 * order to differentiate between these interfaces, the use of an
5597 	 * interface index is necessary and this index can be carried inside a
5598 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5599 	 * of using the interface index, however, is that all of the ipif's that
5600 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5601 	 * cannot be used to differentiate between ipif's (or logical
5602 	 * interfaces) that belong to the same ill (physical interface).
5603 	 *
5604 	 * For example, in the following case involving IPv4 interfaces and
5605 	 * logical interfaces
5606 	 *
5607 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5608 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5609 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5610 	 *
5611 	 * the ipif's corresponding to each of these interface routes can be
5612 	 * uniquely identified by the "gateway" (actually interface address).
5613 	 *
5614 	 * In this case involving multiple IPv6 default routes to a particular
5615 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5616 	 * default route is of interest:
5617 	 *
5618 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5619 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5620 	 */
5621 
5622 	/* RTF_GATEWAY not set */
5623 	if (!(flags & RTF_GATEWAY)) {
5624 		if (sp != NULL) {
5625 			ip2dbg(("ip_rt_add: gateway security attributes "
5626 			    "cannot be set with interface route\n"));
5627 			if (ipif != NULL)
5628 				ipif_refrele(ipif);
5629 			return (EINVAL);
5630 		}
5631 
5632 		/*
5633 		 * Whether or not ill (RTA_IFP) is set, we require that
5634 		 * the gateway is one of our local addresses.
5635 		 */
5636 		if (ipif == NULL)
5637 			return (ENETUNREACH);
5638 
5639 		/*
5640 		 * We use MATCH_IRE_ILL here. If the caller specified an
5641 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5642 		 * we use the ill derived from the gateway address.
5643 		 * We can always match the gateway address since we record it
5644 		 * in ire_gateway_addr.
5645 		 * We don't allow RTA_IFP to specify a different ill than the
5646 		 * one matching the ipif to make sure we can delete the route.
5647 		 */
5648 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5649 		if (ill == NULL) {
5650 			ill = ipif->ipif_ill;
5651 		} else if (ill != ipif->ipif_ill) {
5652 			ipif_refrele(ipif);
5653 			return (EINVAL);
5654 		}
5655 
5656 		/*
5657 		 * We check for an existing entry at this point.
5658 		 *
5659 		 * Since a netmask isn't passed in via the ioctl interface
5660 		 * (SIOCADDRT), we don't check for a matching netmask in that
5661 		 * case.
5662 		 */
5663 		if (!ioctl_msg)
5664 			match_flags |= MATCH_IRE_MASK;
5665 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5666 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5667 		    NULL);
5668 		if (ire != NULL) {
5669 			ire_refrele(ire);
5670 			ipif_refrele(ipif);
5671 			return (EEXIST);
5672 		}
5673 
5674 		/*
5675 		 * Some software (for example, GateD and Sun Cluster) attempts
5676 		 * to create (what amount to) IRE_PREFIX routes with the
5677 		 * loopback address as the gateway.  This is primarily done to
5678 		 * set up prefixes with the RTF_REJECT flag set (for example,
5679 		 * when generating aggregate routes.)
5680 		 *
5681 		 * If the IRE type (as defined by ill->ill_net_type) would be
5682 		 * IRE_LOOPBACK, then we map the request into a
5683 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5684 		 * these interface routes, by definition, can only be that.
5685 		 *
5686 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5687 		 * routine, but rather using ire_create() directly.
5688 		 *
5689 		 */
5690 		type = ill->ill_net_type;
5691 		if (type == IRE_LOOPBACK) {
5692 			type = IRE_IF_NORESOLVER;
5693 			flags |= RTF_BLACKHOLE;
5694 		}
5695 
5696 		/*
5697 		 * Create a copy of the IRE_IF_NORESOLVER or
5698 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5699 		 * gateway.
5700 		 */
5701 		ire = ire_create(
5702 		    (uchar_t *)&dst_addr,
5703 		    (uint8_t *)&mask,
5704 		    (uint8_t *)&gw_addr,
5705 		    type,
5706 		    ill,
5707 		    zoneid,
5708 		    flags,
5709 		    NULL,
5710 		    ipst);
5711 		if (ire == NULL) {
5712 			ipif_refrele(ipif);
5713 			return (ENOMEM);
5714 		}
5715 
5716 		/* src address assigned by the caller? */
5717 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5718 			ire->ire_setsrc_addr = src_addr;
5719 
5720 		nire = ire_add(ire);
5721 		if (nire == NULL) {
5722 			/*
5723 			 * In the result of failure, ire_add() will have
5724 			 * already deleted the ire in question, so there
5725 			 * is no need to do that here.
5726 			 */
5727 			ipif_refrele(ipif);
5728 			return (ENOMEM);
5729 		}
5730 		/*
5731 		 * Check if it was a duplicate entry. This handles
5732 		 * the case of two racing route adds for the same route
5733 		 */
5734 		if (nire != ire) {
5735 			ire_delete(nire);
5736 			ire_refrele(nire);
5737 			ipif_refrele(ipif);
5738 			return (EEXIST);
5739 		}
5740 		ire = nire;
5741 		goto save_ire;
5742 	}
5743 
5744 	/*
5745 	 * Get an interface IRE for the specified gateway.
5746 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5747 	 * gateway, it is currently unreachable and we fail the request
5748 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5749 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5750 	 * If RTA_IFP was specified we look on that particular ill.
5751 	 */
5752 	if (ill != NULL)
5753 		match_flags |= MATCH_IRE_ILL;
5754 
5755 	/* Check whether the gateway is reachable. */
5756 again:
5757 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5758 	if (flags & RTF_INDIRECT)
5759 		type |= IRE_OFFLINK;
5760 
5761 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5762 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5763 	if (gw_ire == NULL) {
5764 		/*
5765 		 * With IPMP, we allow host routes to influence in.mpathd's
5766 		 * target selection.  However, if the test addresses are on
5767 		 * their own network, the above lookup will fail since the
5768 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5769 		 * hidden test IREs to be found and try again.
5770 		 */
5771 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5772 			match_flags |= MATCH_IRE_TESTHIDDEN;
5773 			goto again;
5774 		}
5775 		if (ipif != NULL)
5776 			ipif_refrele(ipif);
5777 		return (ENETUNREACH);
5778 	}
5779 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5780 		ire_refrele(gw_ire);
5781 		if (ipif != NULL)
5782 			ipif_refrele(ipif);
5783 		return (ENETUNREACH);
5784 	}
5785 
5786 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5787 		unbound = B_TRUE;
5788 		if (ipst->ips_ip_strict_src_multihoming > 0)
5789 			ill = gw_ire->ire_ill;
5790 	}
5791 
5792 	/*
5793 	 * We create one of three types of IREs as a result of this request
5794 	 * based on the netmask.  A netmask of all ones (which is automatically
5795 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5796 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5797 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5798 	 * destination prefix.
5799 	 */
5800 	if (mask == IP_HOST_MASK)
5801 		type = IRE_HOST;
5802 	else if (mask == 0)
5803 		type = IRE_DEFAULT;
5804 	else
5805 		type = IRE_PREFIX;
5806 
5807 	/* check for a duplicate entry */
5808 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5809 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5810 	    0, ipst, NULL);
5811 	if (ire != NULL) {
5812 		if (ipif != NULL)
5813 			ipif_refrele(ipif);
5814 		ire_refrele(gw_ire);
5815 		ire_refrele(ire);
5816 		return (EEXIST);
5817 	}
5818 
5819 	/* Security attribute exists */
5820 	if (sp != NULL) {
5821 		tsol_gcgrp_addr_t ga;
5822 
5823 		/* find or create the gateway credentials group */
5824 		ga.ga_af = AF_INET;
5825 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5826 
5827 		/* we hold reference to it upon success */
5828 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5829 		if (gcgrp == NULL) {
5830 			if (ipif != NULL)
5831 				ipif_refrele(ipif);
5832 			ire_refrele(gw_ire);
5833 			return (ENOMEM);
5834 		}
5835 
5836 		/*
5837 		 * Create and add the security attribute to the group; a
5838 		 * reference to the group is made upon allocating a new
5839 		 * entry successfully.  If it finds an already-existing
5840 		 * entry for the security attribute in the group, it simply
5841 		 * returns it and no new reference is made to the group.
5842 		 */
5843 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5844 		if (gc == NULL) {
5845 			if (ipif != NULL)
5846 				ipif_refrele(ipif);
5847 			/* release reference held by gcgrp_lookup */
5848 			GCGRP_REFRELE(gcgrp);
5849 			ire_refrele(gw_ire);
5850 			return (ENOMEM);
5851 		}
5852 	}
5853 
5854 	/* Create the IRE. */
5855 	ire = ire_create(
5856 	    (uchar_t *)&dst_addr,		/* dest address */
5857 	    (uchar_t *)&mask,			/* mask */
5858 	    (uchar_t *)&gw_addr,		/* gateway address */
5859 	    (ushort_t)type,			/* IRE type */
5860 	    ill,
5861 	    zoneid,
5862 	    flags,
5863 	    gc,					/* security attribute */
5864 	    ipst);
5865 
5866 	/*
5867 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5868 	 * reference to the 'gcgrp'. We can now release the extra reference
5869 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5870 	 */
5871 	if (gcgrp_xtraref)
5872 		GCGRP_REFRELE(gcgrp);
5873 	if (ire == NULL) {
5874 		if (gc != NULL)
5875 			GC_REFRELE(gc);
5876 		if (ipif != NULL)
5877 			ipif_refrele(ipif);
5878 		ire_refrele(gw_ire);
5879 		return (ENOMEM);
5880 	}
5881 
5882 	/* Before we add, check if an extra CGTP broadcast is needed */
5883 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5884 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5885 
5886 	/* src address assigned by the caller? */
5887 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5888 		ire->ire_setsrc_addr = src_addr;
5889 
5890 	ire->ire_unbound = unbound;
5891 
5892 	/*
5893 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5894 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5895 	 */
5896 
5897 	/* Add the new IRE. */
5898 	nire = ire_add(ire);
5899 	if (nire == NULL) {
5900 		/*
5901 		 * In the result of failure, ire_add() will have
5902 		 * already deleted the ire in question, so there
5903 		 * is no need to do that here.
5904 		 */
5905 		if (ipif != NULL)
5906 			ipif_refrele(ipif);
5907 		ire_refrele(gw_ire);
5908 		return (ENOMEM);
5909 	}
5910 	/*
5911 	 * Check if it was a duplicate entry. This handles
5912 	 * the case of two racing route adds for the same route
5913 	 */
5914 	if (nire != ire) {
5915 		ire_delete(nire);
5916 		ire_refrele(nire);
5917 		if (ipif != NULL)
5918 			ipif_refrele(ipif);
5919 		ire_refrele(gw_ire);
5920 		return (EEXIST);
5921 	}
5922 	ire = nire;
5923 
5924 	if (flags & RTF_MULTIRT) {
5925 		/*
5926 		 * Invoke the CGTP (multirouting) filtering module
5927 		 * to add the dst address in the filtering database.
5928 		 * Replicated inbound packets coming from that address
5929 		 * will be filtered to discard the duplicates.
5930 		 * It is not necessary to call the CGTP filter hook
5931 		 * when the dst address is a broadcast or multicast,
5932 		 * because an IP source address cannot be a broadcast
5933 		 * or a multicast.
5934 		 */
5935 		if (cgtp_broadcast) {
5936 			ip_cgtp_bcast_add(ire, ipst);
5937 			goto save_ire;
5938 		}
5939 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5940 		    !CLASSD(ire->ire_addr)) {
5941 			int res;
5942 			ipif_t *src_ipif;
5943 
5944 			/* Find the source address corresponding to gw_ire */
5945 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5946 			    NULL, zoneid, ipst);
5947 			if (src_ipif != NULL) {
5948 				res = ipst->ips_ip_cgtp_filter_ops->
5949 				    cfo_add_dest_v4(
5950 				    ipst->ips_netstack->netstack_stackid,
5951 				    ire->ire_addr,
5952 				    ire->ire_gateway_addr,
5953 				    ire->ire_setsrc_addr,
5954 				    src_ipif->ipif_lcl_addr);
5955 				ipif_refrele(src_ipif);
5956 			} else {
5957 				res = EADDRNOTAVAIL;
5958 			}
5959 			if (res != 0) {
5960 				if (ipif != NULL)
5961 					ipif_refrele(ipif);
5962 				ire_refrele(gw_ire);
5963 				ire_delete(ire);
5964 				ire_refrele(ire);	/* Held in ire_add */
5965 				return (res);
5966 			}
5967 		}
5968 	}
5969 
5970 save_ire:
5971 	if (gw_ire != NULL) {
5972 		ire_refrele(gw_ire);
5973 		gw_ire = NULL;
5974 	}
5975 	if (ill != NULL) {
5976 		/*
5977 		 * Save enough information so that we can recreate the IRE if
5978 		 * the interface goes down and then up.  The metrics associated
5979 		 * with the route will be saved as well when rts_setmetrics() is
5980 		 * called after the IRE has been created.  In the case where
5981 		 * memory cannot be allocated, none of this information will be
5982 		 * saved.
5983 		 */
5984 		ill_save_ire(ill, ire);
5985 	}
5986 	if (ioctl_msg)
5987 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5988 	if (ire_arg != NULL) {
5989 		/*
5990 		 * Store the ire that was successfully added into where ire_arg
5991 		 * points to so that callers don't have to look it up
5992 		 * themselves (but they are responsible for ire_refrele()ing
5993 		 * the ire when they are finished with it).
5994 		 */
5995 		*ire_arg = ire;
5996 	} else {
5997 		ire_refrele(ire);		/* Held in ire_add */
5998 	}
5999 	if (ipif != NULL)
6000 		ipif_refrele(ipif);
6001 	return (0);
6002 }
6003 
6004 /*
6005  * ip_rt_delete is called to delete an IPv4 route.
6006  * ill is passed in to associate it with the correct interface.
6007  */
6008 /* ARGSUSED4 */
6009 int
6010 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6011     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
6012     ip_stack_t *ipst, zoneid_t zoneid)
6013 {
6014 	ire_t	*ire = NULL;
6015 	ipif_t	*ipif;
6016 	uint_t	type;
6017 	uint_t	match_flags = MATCH_IRE_TYPE;
6018 	int	err = 0;
6019 
6020 	ip1dbg(("ip_rt_delete:"));
6021 	/*
6022 	 * If this is the case of RTF_HOST being set, then we set the netmask
6023 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6024 	 */
6025 	if (flags & RTF_HOST) {
6026 		mask = IP_HOST_MASK;
6027 		match_flags |= MATCH_IRE_MASK;
6028 	} else if (rtm_addrs & RTA_NETMASK) {
6029 		match_flags |= MATCH_IRE_MASK;
6030 	}
6031 
6032 	/*
6033 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6034 	 * we check if the gateway address is one of our interfaces first,
6035 	 * and fall back on RTF_GATEWAY routes.
6036 	 *
6037 	 * This makes it possible to delete an original
6038 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6039 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6040 	 * and those can not be deleted here.
6041 	 *
6042 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6043 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6044 	 * otherwise we use the ill derived from the gateway address.
6045 	 * We can always match the gateway address since we record it
6046 	 * in ire_gateway_addr.
6047 	 *
6048 	 * For more detail on specifying routes by gateway address and by
6049 	 * interface index, see the comments in ip_rt_add().
6050 	 */
6051 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6052 	if (ipif != NULL) {
6053 		ill_t	*ill_match;
6054 
6055 		if (ill != NULL)
6056 			ill_match = ill;
6057 		else
6058 			ill_match = ipif->ipif_ill;
6059 
6060 		match_flags |= MATCH_IRE_ILL;
6061 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6062 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6063 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6064 			    match_flags, 0, ipst, NULL);
6065 		}
6066 		if (ire == NULL) {
6067 			match_flags |= MATCH_IRE_GW;
6068 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6069 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6070 			    match_flags, 0, ipst, NULL);
6071 		}
6072 		/* Avoid deleting routes created by kernel from an ipif */
6073 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6074 			ire_refrele(ire);
6075 			ire = NULL;
6076 		}
6077 
6078 		/* Restore in case we didn't find a match */
6079 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6080 	}
6081 
6082 	if (ire == NULL) {
6083 		/*
6084 		 * At this point, the gateway address is not one of our own
6085 		 * addresses or a matching interface route was not found.  We
6086 		 * set the IRE type to lookup based on whether
6087 		 * this is a host route, a default route or just a prefix.
6088 		 *
6089 		 * If an ill was passed in, then the lookup is based on an
6090 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6091 		 */
6092 		match_flags |= MATCH_IRE_GW;
6093 		if (ill != NULL)
6094 			match_flags |= MATCH_IRE_ILL;
6095 		if (mask == IP_HOST_MASK)
6096 			type = IRE_HOST;
6097 		else if (mask == 0)
6098 			type = IRE_DEFAULT;
6099 		else
6100 			type = IRE_PREFIX;
6101 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6102 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6103 	}
6104 
6105 	if (ipif != NULL) {
6106 		ipif_refrele(ipif);
6107 		ipif = NULL;
6108 	}
6109 
6110 	if (ire == NULL)
6111 		return (ESRCH);
6112 
6113 	if (ire->ire_flags & RTF_MULTIRT) {
6114 		/*
6115 		 * Invoke the CGTP (multirouting) filtering module
6116 		 * to remove the dst address from the filtering database.
6117 		 * Packets coming from that address will no longer be
6118 		 * filtered to remove duplicates.
6119 		 */
6120 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6121 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6122 			    ipst->ips_netstack->netstack_stackid,
6123 			    ire->ire_addr, ire->ire_gateway_addr);
6124 		}
6125 		ip_cgtp_bcast_delete(ire, ipst);
6126 	}
6127 
6128 	ill = ire->ire_ill;
6129 	if (ill != NULL)
6130 		ill_remove_saved_ire(ill, ire);
6131 	if (ioctl_msg)
6132 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6133 	ire_delete(ire);
6134 	ire_refrele(ire);
6135 	return (err);
6136 }
6137 
6138 /*
6139  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6140  */
6141 /* ARGSUSED */
6142 int
6143 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6144     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6145 {
6146 	ipaddr_t dst_addr;
6147 	ipaddr_t gw_addr;
6148 	ipaddr_t mask;
6149 	int error = 0;
6150 	mblk_t *mp1;
6151 	struct rtentry *rt;
6152 	ipif_t *ipif = NULL;
6153 	ip_stack_t	*ipst;
6154 
6155 	ASSERT(q->q_next == NULL);
6156 	ipst = CONNQ_TO_IPST(q);
6157 
6158 	ip1dbg(("ip_siocaddrt:"));
6159 	/* Existence of mp1 verified in ip_wput_nondata */
6160 	mp1 = mp->b_cont->b_cont;
6161 	rt = (struct rtentry *)mp1->b_rptr;
6162 
6163 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6164 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6165 
6166 	/*
6167 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6168 	 * to a particular host address.  In this case, we set the netmask to
6169 	 * all ones for the particular destination address.  Otherwise,
6170 	 * determine the netmask to be used based on dst_addr and the interfaces
6171 	 * in use.
6172 	 */
6173 	if (rt->rt_flags & RTF_HOST) {
6174 		mask = IP_HOST_MASK;
6175 	} else {
6176 		/*
6177 		 * Note that ip_subnet_mask returns a zero mask in the case of
6178 		 * default (an all-zeroes address).
6179 		 */
6180 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6181 	}
6182 
6183 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6184 	    B_TRUE, NULL, ipst, ALL_ZONES);
6185 	if (ipif != NULL)
6186 		ipif_refrele(ipif);
6187 	return (error);
6188 }
6189 
6190 /*
6191  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6192  */
6193 /* ARGSUSED */
6194 int
6195 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6196     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6197 {
6198 	ipaddr_t dst_addr;
6199 	ipaddr_t gw_addr;
6200 	ipaddr_t mask;
6201 	int error;
6202 	mblk_t *mp1;
6203 	struct rtentry *rt;
6204 	ipif_t *ipif = NULL;
6205 	ip_stack_t	*ipst;
6206 
6207 	ASSERT(q->q_next == NULL);
6208 	ipst = CONNQ_TO_IPST(q);
6209 
6210 	ip1dbg(("ip_siocdelrt:"));
6211 	/* Existence of mp1 verified in ip_wput_nondata */
6212 	mp1 = mp->b_cont->b_cont;
6213 	rt = (struct rtentry *)mp1->b_rptr;
6214 
6215 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6216 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6217 
6218 	/*
6219 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6220 	 * to a particular host address.  In this case, we set the netmask to
6221 	 * all ones for the particular destination address.  Otherwise,
6222 	 * determine the netmask to be used based on dst_addr and the interfaces
6223 	 * in use.
6224 	 */
6225 	if (rt->rt_flags & RTF_HOST) {
6226 		mask = IP_HOST_MASK;
6227 	} else {
6228 		/*
6229 		 * Note that ip_subnet_mask returns a zero mask in the case of
6230 		 * default (an all-zeroes address).
6231 		 */
6232 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6233 	}
6234 
6235 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6236 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6237 	    ipst, ALL_ZONES);
6238 	if (ipif != NULL)
6239 		ipif_refrele(ipif);
6240 	return (error);
6241 }
6242 
6243 /*
6244  * Enqueue the mp onto the ipsq, chained by b_next.
6245  * b_prev stores the function to be executed later, and b_queue the queue
6246  * where this mp originated.
6247  */
6248 void
6249 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6250     ill_t *pending_ill)
6251 {
6252 	conn_t	*connp;
6253 	ipxop_t *ipx = ipsq->ipsq_xop;
6254 
6255 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6256 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6257 	ASSERT(func != NULL);
6258 
6259 	mp->b_queue = q;
6260 	mp->b_prev = (void *)func;
6261 	mp->b_next = NULL;
6262 
6263 	switch (type) {
6264 	case CUR_OP:
6265 		if (ipx->ipx_mptail != NULL) {
6266 			ASSERT(ipx->ipx_mphead != NULL);
6267 			ipx->ipx_mptail->b_next = mp;
6268 		} else {
6269 			ASSERT(ipx->ipx_mphead == NULL);
6270 			ipx->ipx_mphead = mp;
6271 		}
6272 		ipx->ipx_mptail = mp;
6273 		break;
6274 
6275 	case NEW_OP:
6276 		if (ipsq->ipsq_xopq_mptail != NULL) {
6277 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6278 			ipsq->ipsq_xopq_mptail->b_next = mp;
6279 		} else {
6280 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6281 			ipsq->ipsq_xopq_mphead = mp;
6282 		}
6283 		ipsq->ipsq_xopq_mptail = mp;
6284 		ipx->ipx_ipsq_queued = B_TRUE;
6285 		break;
6286 
6287 	case SWITCH_OP:
6288 		ASSERT(ipsq->ipsq_swxop != NULL);
6289 		/* only one switch operation is currently allowed */
6290 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6291 		ipsq->ipsq_switch_mp = mp;
6292 		ipx->ipx_ipsq_queued = B_TRUE;
6293 		break;
6294 	default:
6295 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6296 	}
6297 
6298 	if (CONN_Q(q) && pending_ill != NULL) {
6299 		connp = Q_TO_CONN(q);
6300 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6301 		connp->conn_oper_pending_ill = pending_ill;
6302 	}
6303 }
6304 
6305 /*
6306  * Dequeue the next message that requested exclusive access to this IPSQ's
6307  * xop.  Specifically:
6308  *
6309  *  1. If we're still processing the current operation on `ipsq', then
6310  *     dequeue the next message for the operation (from ipx_mphead), or
6311  *     return NULL if there are no queued messages for the operation.
6312  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6313  *
6314  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6315  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6316  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6317  *     leaving IPMP groups and require a careful dance -- see the comments
6318  *     in-line below for details.  If we're leaving a group xop or if we're
6319  *     joining a group xop and become writer on it, then we proceed to (3).
6320  *     Otherwise, we return NULL and exit the xop.
6321  *
6322  *  3. For each IPSQ in the xop, return any switch operation stored on
6323  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6324  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6325  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6326  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6327  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6328  *     each phyint in the group, including the IPMP meta-interface phyint.
6329  */
6330 static mblk_t *
6331 ipsq_dq(ipsq_t *ipsq)
6332 {
6333 	ill_t	*illv4, *illv6;
6334 	mblk_t	*mp;
6335 	ipsq_t	*xopipsq;
6336 	ipsq_t	*leftipsq = NULL;
6337 	ipxop_t *ipx;
6338 	phyint_t *phyi = ipsq->ipsq_phyint;
6339 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6340 	boolean_t emptied = B_FALSE;
6341 
6342 	/*
6343 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6344 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6345 	 */
6346 	rw_enter(&ipst->ips_ill_g_lock,
6347 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6348 	mutex_enter(&ipsq->ipsq_lock);
6349 	ipx = ipsq->ipsq_xop;
6350 	mutex_enter(&ipx->ipx_lock);
6351 
6352 	/*
6353 	 * Dequeue the next message associated with the current exclusive
6354 	 * operation, if any.
6355 	 */
6356 	if ((mp = ipx->ipx_mphead) != NULL) {
6357 		ipx->ipx_mphead = mp->b_next;
6358 		if (ipx->ipx_mphead == NULL)
6359 			ipx->ipx_mptail = NULL;
6360 		mp->b_next = (void *)ipsq;
6361 		goto out;
6362 	}
6363 
6364 	if (ipx->ipx_current_ipif != NULL)
6365 		goto empty;
6366 
6367 	if (ipsq->ipsq_swxop != NULL) {
6368 		/*
6369 		 * The exclusive operation that is now being completed has
6370 		 * requested a switch to a different xop.  This happens
6371 		 * when an interface joins or leaves an IPMP group.  Joins
6372 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6373 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6374 		 * (phyint_free()), or interface plumb for an ill type
6375 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6376 		 *
6377 		 * Xop switches are not allowed on the IPMP meta-interface.
6378 		 */
6379 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6380 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6381 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6382 
6383 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6384 			/*
6385 			 * We're switching back to our own xop, so we have two
6386 			 * xop's to drain/exit: our own, and the group xop
6387 			 * that we are leaving.
6388 			 *
6389 			 * First, pull ourselves out of the group ipsq list.
6390 			 * This is safe since we're writer on ill_g_lock.
6391 			 */
6392 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6393 
6394 			xopipsq = ipx->ipx_ipsq;
6395 			while (xopipsq->ipsq_next != ipsq)
6396 				xopipsq = xopipsq->ipsq_next;
6397 
6398 			xopipsq->ipsq_next = ipsq->ipsq_next;
6399 			ipsq->ipsq_next = ipsq;
6400 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6401 			ipsq->ipsq_swxop = NULL;
6402 
6403 			/*
6404 			 * Second, prepare to exit the group xop.  The actual
6405 			 * ipsq_exit() is done at the end of this function
6406 			 * since we cannot hold any locks across ipsq_exit().
6407 			 * Note that although we drop the group's ipx_lock, no
6408 			 * threads can proceed since we're still ipx_writer.
6409 			 */
6410 			leftipsq = xopipsq;
6411 			mutex_exit(&ipx->ipx_lock);
6412 
6413 			/*
6414 			 * Third, set ipx to point to our own xop (which was
6415 			 * inactive and therefore can be entered).
6416 			 */
6417 			ipx = ipsq->ipsq_xop;
6418 			mutex_enter(&ipx->ipx_lock);
6419 			ASSERT(ipx->ipx_writer == NULL);
6420 			ASSERT(ipx->ipx_current_ipif == NULL);
6421 		} else {
6422 			/*
6423 			 * We're switching from our own xop to a group xop.
6424 			 * The requestor of the switch must ensure that the
6425 			 * group xop cannot go away (e.g. by ensuring the
6426 			 * phyint associated with the xop cannot go away).
6427 			 *
6428 			 * If we can become writer on our new xop, then we'll
6429 			 * do the drain.  Otherwise, the current writer of our
6430 			 * new xop will do the drain when it exits.
6431 			 *
6432 			 * First, splice ourselves into the group IPSQ list.
6433 			 * This is safe since we're writer on ill_g_lock.
6434 			 */
6435 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6436 
6437 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6438 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6439 				xopipsq = xopipsq->ipsq_next;
6440 
6441 			xopipsq->ipsq_next = ipsq;
6442 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6443 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6444 			ipsq->ipsq_swxop = NULL;
6445 
6446 			/*
6447 			 * Second, exit our own xop, since it's now unused.
6448 			 * This is safe since we've got the only reference.
6449 			 */
6450 			ASSERT(ipx->ipx_writer == curthread);
6451 			ipx->ipx_writer = NULL;
6452 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6453 			ipx->ipx_ipsq_queued = B_FALSE;
6454 			mutex_exit(&ipx->ipx_lock);
6455 
6456 			/*
6457 			 * Third, set ipx to point to our new xop, and check
6458 			 * if we can become writer on it.  If we cannot, then
6459 			 * the current writer will drain the IPSQ group when
6460 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6461 			 * because we're still holding ipsq_lock.
6462 			 */
6463 			ipx = ipsq->ipsq_xop;
6464 			mutex_enter(&ipx->ipx_lock);
6465 			if (ipx->ipx_writer != NULL ||
6466 			    ipx->ipx_current_ipif != NULL) {
6467 				goto out;
6468 			}
6469 		}
6470 
6471 		/*
6472 		 * Fourth, become writer on our new ipx before we continue
6473 		 * with the drain.  Note that we never dropped ipsq_lock
6474 		 * above, so no other thread could've raced with us to
6475 		 * become writer first.  Also, we're holding ipx_lock, so
6476 		 * no other thread can examine the ipx right now.
6477 		 */
6478 		ASSERT(ipx->ipx_current_ipif == NULL);
6479 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6480 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6481 		ipx->ipx_writer = curthread;
6482 		ipx->ipx_forced = B_FALSE;
6483 #ifdef DEBUG
6484 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6485 #endif
6486 	}
6487 
6488 	xopipsq = ipsq;
6489 	do {
6490 		/*
6491 		 * So that other operations operate on a consistent and
6492 		 * complete phyint, a switch message on an IPSQ must be
6493 		 * handled prior to any other operations on that IPSQ.
6494 		 */
6495 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6496 			xopipsq->ipsq_switch_mp = NULL;
6497 			ASSERT(mp->b_next == NULL);
6498 			mp->b_next = (void *)xopipsq;
6499 			goto out;
6500 		}
6501 
6502 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6503 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6504 			if (xopipsq->ipsq_xopq_mphead == NULL)
6505 				xopipsq->ipsq_xopq_mptail = NULL;
6506 			mp->b_next = (void *)xopipsq;
6507 			goto out;
6508 		}
6509 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6510 empty:
6511 	/*
6512 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6513 	 * new messages can end up on any IPSQ in the xop.
6514 	 */
6515 	ipx->ipx_writer = NULL;
6516 	ipx->ipx_forced = B_FALSE;
6517 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6518 	ipx->ipx_ipsq_queued = B_FALSE;
6519 	emptied = B_TRUE;
6520 #ifdef	DEBUG
6521 	ipx->ipx_depth = 0;
6522 #endif
6523 out:
6524 	mutex_exit(&ipx->ipx_lock);
6525 	mutex_exit(&ipsq->ipsq_lock);
6526 
6527 	/*
6528 	 * If we completely emptied the xop, then wake up any threads waiting
6529 	 * to enter any of the IPSQ's associated with it.
6530 	 */
6531 	if (emptied) {
6532 		xopipsq = ipsq;
6533 		do {
6534 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6535 				continue;
6536 
6537 			illv4 = phyi->phyint_illv4;
6538 			illv6 = phyi->phyint_illv6;
6539 
6540 			GRAB_ILL_LOCKS(illv4, illv6);
6541 			if (illv4 != NULL)
6542 				cv_broadcast(&illv4->ill_cv);
6543 			if (illv6 != NULL)
6544 				cv_broadcast(&illv6->ill_cv);
6545 			RELEASE_ILL_LOCKS(illv4, illv6);
6546 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6547 	}
6548 	rw_exit(&ipst->ips_ill_g_lock);
6549 
6550 	/*
6551 	 * Now that all locks are dropped, exit the IPSQ we left.
6552 	 */
6553 	if (leftipsq != NULL)
6554 		ipsq_exit(leftipsq);
6555 
6556 	return (mp);
6557 }
6558 
6559 /*
6560  * Return completion status of previously initiated DLPI operations on
6561  * ills in the purview of an ipsq.
6562  */
6563 static boolean_t
6564 ipsq_dlpi_done(ipsq_t *ipsq)
6565 {
6566 	ipsq_t		*ipsq_start;
6567 	phyint_t	*phyi;
6568 	ill_t		*ill;
6569 
6570 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6571 	ipsq_start = ipsq;
6572 
6573 	do {
6574 		/*
6575 		 * The only current users of this function are ipsq_try_enter
6576 		 * and ipsq_enter which have made sure that ipsq_writer is
6577 		 * NULL before we reach here. ill_dlpi_pending is modified
6578 		 * only by an ipsq writer
6579 		 */
6580 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6581 		phyi = ipsq->ipsq_phyint;
6582 		/*
6583 		 * phyi could be NULL if a phyint that is part of an
6584 		 * IPMP group is being unplumbed. A more detailed
6585 		 * comment is in ipmp_grp_update_kstats()
6586 		 */
6587 		if (phyi != NULL) {
6588 			ill = phyi->phyint_illv4;
6589 			if (ill != NULL &&
6590 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6591 			    ill->ill_arl_dlpi_pending))
6592 				return (B_FALSE);
6593 
6594 			ill = phyi->phyint_illv6;
6595 			if (ill != NULL &&
6596 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6597 				return (B_FALSE);
6598 		}
6599 
6600 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6601 
6602 	return (B_TRUE);
6603 }
6604 
6605 /*
6606  * Enter the ipsq corresponding to ill, by waiting synchronously till
6607  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6608  * will have to drain completely before ipsq_enter returns success.
6609  * ipx_current_ipif will be set if some exclusive op is in progress,
6610  * and the ipsq_exit logic will start the next enqueued op after
6611  * completion of the current op. If 'force' is used, we don't wait
6612  * for the enqueued ops. This is needed when a conn_close wants to
6613  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6614  * of an ill can also use this option. But we dont' use it currently.
6615  */
6616 #define	ENTER_SQ_WAIT_TICKS 100
6617 boolean_t
6618 ipsq_enter(ill_t *ill, boolean_t force, int type)
6619 {
6620 	ipsq_t	*ipsq;
6621 	ipxop_t *ipx;
6622 	boolean_t waited_enough = B_FALSE;
6623 	ip_stack_t *ipst = ill->ill_ipst;
6624 
6625 	/*
6626 	 * Note that the relationship between ill and ipsq is fixed as long as
6627 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6628 	 * relationship between the IPSQ and xop cannot change.  However,
6629 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6630 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6631 	 * waking up all ills in the xop when it becomes available.
6632 	 */
6633 	for (;;) {
6634 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6635 		mutex_enter(&ill->ill_lock);
6636 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6637 			mutex_exit(&ill->ill_lock);
6638 			rw_exit(&ipst->ips_ill_g_lock);
6639 			return (B_FALSE);
6640 		}
6641 
6642 		ipsq = ill->ill_phyint->phyint_ipsq;
6643 		mutex_enter(&ipsq->ipsq_lock);
6644 		ipx = ipsq->ipsq_xop;
6645 		mutex_enter(&ipx->ipx_lock);
6646 
6647 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6648 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6649 		    waited_enough))
6650 			break;
6651 
6652 		rw_exit(&ipst->ips_ill_g_lock);
6653 
6654 		if (!force || ipx->ipx_writer != NULL) {
6655 			mutex_exit(&ipx->ipx_lock);
6656 			mutex_exit(&ipsq->ipsq_lock);
6657 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6658 		} else {
6659 			mutex_exit(&ipx->ipx_lock);
6660 			mutex_exit(&ipsq->ipsq_lock);
6661 			(void) cv_reltimedwait(&ill->ill_cv,
6662 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6663 			waited_enough = B_TRUE;
6664 		}
6665 		mutex_exit(&ill->ill_lock);
6666 	}
6667 
6668 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6669 	ASSERT(ipx->ipx_reentry_cnt == 0);
6670 	ipx->ipx_writer = curthread;
6671 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6672 	ipx->ipx_reentry_cnt++;
6673 #ifdef DEBUG
6674 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6675 #endif
6676 	mutex_exit(&ipx->ipx_lock);
6677 	mutex_exit(&ipsq->ipsq_lock);
6678 	mutex_exit(&ill->ill_lock);
6679 	rw_exit(&ipst->ips_ill_g_lock);
6680 
6681 	return (B_TRUE);
6682 }
6683 
6684 /*
6685  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6686  * across the call to the core interface ipsq_try_enter() and hence calls this
6687  * function directly. This is explained more fully in ipif_set_values().
6688  * In order to support the above constraint, ipsq_try_enter is implemented as
6689  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6690  */
6691 static ipsq_t *
6692 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6693     int type, boolean_t reentry_ok)
6694 {
6695 	ipsq_t	*ipsq;
6696 	ipxop_t	*ipx;
6697 	ip_stack_t *ipst = ill->ill_ipst;
6698 
6699 	/*
6700 	 * lock ordering:
6701 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6702 	 *
6703 	 * ipx of an ipsq can't change when ipsq_lock is held.
6704 	 */
6705 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6706 	GRAB_CONN_LOCK(q);
6707 	mutex_enter(&ill->ill_lock);
6708 	ipsq = ill->ill_phyint->phyint_ipsq;
6709 	mutex_enter(&ipsq->ipsq_lock);
6710 	ipx = ipsq->ipsq_xop;
6711 	mutex_enter(&ipx->ipx_lock);
6712 
6713 	/*
6714 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6715 	 *    (Note: If the caller does not specify reentry_ok then neither
6716 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6717 	 *    again. Otherwise it can lead to an infinite loop
6718 	 * 2. Enter the ipsq if there is no current writer and this attempted
6719 	 *    entry is part of the current operation
6720 	 * 3. Enter the ipsq if there is no current writer and this is a new
6721 	 *    operation and the operation queue is empty and there is no
6722 	 *    operation currently in progress and if all previously initiated
6723 	 *    DLPI operations have completed.
6724 	 */
6725 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6726 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6727 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6728 	    ipsq_dlpi_done(ipsq))))) {
6729 		/* Success. */
6730 		ipx->ipx_reentry_cnt++;
6731 		ipx->ipx_writer = curthread;
6732 		ipx->ipx_forced = B_FALSE;
6733 		mutex_exit(&ipx->ipx_lock);
6734 		mutex_exit(&ipsq->ipsq_lock);
6735 		mutex_exit(&ill->ill_lock);
6736 		RELEASE_CONN_LOCK(q);
6737 #ifdef DEBUG
6738 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6739 #endif
6740 		return (ipsq);
6741 	}
6742 
6743 	if (func != NULL)
6744 		ipsq_enq(ipsq, q, mp, func, type, ill);
6745 
6746 	mutex_exit(&ipx->ipx_lock);
6747 	mutex_exit(&ipsq->ipsq_lock);
6748 	mutex_exit(&ill->ill_lock);
6749 	RELEASE_CONN_LOCK(q);
6750 	return (NULL);
6751 }
6752 
6753 /*
6754  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6755  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6756  * There is one ipsq per phyint. The ipsq
6757  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6758  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6759  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6760  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6761  * up the interface) and are enqueued in ipx_mphead.
6762  *
6763  * If a thread does not want to reenter the ipsq when it is already writer,
6764  * it must make sure that the specified reentry point to be called later
6765  * when the ipsq is empty, nor any code path starting from the specified reentry
6766  * point must never ever try to enter the ipsq again. Otherwise it can lead
6767  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6768  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6769  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6770  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6771  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6772  * ioctl if the current ioctl has completed. If the current ioctl is still
6773  * in progress it simply returns. The current ioctl could be waiting for
6774  * a response from another module (the driver or could be waiting for
6775  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6776  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6777  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6778  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6779  * all associated DLPI operations have completed.
6780  */
6781 
6782 /*
6783  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6784  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6785  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6786  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6787  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6788  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6789  */
6790 ipsq_t *
6791 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6792     ipsq_func_t func, int type, boolean_t reentry_ok)
6793 {
6794 	ip_stack_t	*ipst;
6795 	ipsq_t		*ipsq;
6796 
6797 	/* Only 1 of ipif or ill can be specified */
6798 	ASSERT((ipif != NULL) ^ (ill != NULL));
6799 
6800 	if (ipif != NULL)
6801 		ill = ipif->ipif_ill;
6802 	ipst = ill->ill_ipst;
6803 
6804 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6805 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6806 	rw_exit(&ipst->ips_ill_g_lock);
6807 
6808 	return (ipsq);
6809 }
6810 
6811 /*
6812  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6813  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6814  * cannot be entered, the mp is queued for completion.
6815  */
6816 void
6817 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6818     boolean_t reentry_ok)
6819 {
6820 	ipsq_t	*ipsq;
6821 
6822 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6823 
6824 	/*
6825 	 * Drop the caller's refhold on the ill.  This is safe since we either
6826 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6827 	 * IPSQ, in which case we return without accessing ill anymore.  This
6828 	 * is needed because func needs to see the correct refcount.
6829 	 * e.g. removeif can work only then.
6830 	 */
6831 	ill_refrele(ill);
6832 	if (ipsq != NULL) {
6833 		(*func)(ipsq, q, mp, NULL);
6834 		ipsq_exit(ipsq);
6835 	}
6836 }
6837 
6838 /*
6839  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6840  * prior to exiting.  Caller must be writer on the specified IPSQ.
6841  */
6842 void
6843 ipsq_exit(ipsq_t *ipsq)
6844 {
6845 	mblk_t *mp;
6846 	ipsq_t *mp_ipsq;
6847 	queue_t	*q;
6848 	phyint_t *phyi;
6849 	ipsq_func_t func;
6850 
6851 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6852 
6853 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6854 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6855 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6856 		return;
6857 	}
6858 
6859 	for (;;) {
6860 		phyi = ipsq->ipsq_phyint;
6861 		mp = ipsq_dq(ipsq);
6862 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6863 
6864 		/*
6865 		 * If we've changed to a new IPSQ, and the phyint associated
6866 		 * with the old one has gone away, free the old IPSQ.  Note
6867 		 * that this cannot happen while the IPSQ is in a group.
6868 		 */
6869 		if (mp_ipsq != ipsq && phyi == NULL) {
6870 			ASSERT(ipsq->ipsq_next == ipsq);
6871 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6872 			ipsq_delete(ipsq);
6873 		}
6874 
6875 		if (mp == NULL)
6876 			break;
6877 
6878 		q = mp->b_queue;
6879 		func = (ipsq_func_t)mp->b_prev;
6880 		ipsq = mp_ipsq;
6881 		mp->b_next = mp->b_prev = NULL;
6882 		mp->b_queue = NULL;
6883 
6884 		/*
6885 		 * If 'q' is an conn queue, it is valid, since we did a
6886 		 * a refhold on the conn at the start of the ioctl.
6887 		 * If 'q' is an ill queue, it is valid, since close of an
6888 		 * ill will clean up its IPSQ.
6889 		 */
6890 		(*func)(ipsq, q, mp, NULL);
6891 	}
6892 }
6893 
6894 /*
6895  * Used to start any igmp or mld timers that could not be started
6896  * while holding ill_mcast_lock. The timers can't be started while holding
6897  * the lock, since mld/igmp_start_timers may need to call untimeout()
6898  * which can't be done while holding the lock which the timeout handler
6899  * acquires. Otherwise
6900  * there could be a deadlock since the timeout handlers
6901  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6902  * ill_mcast_lock.
6903  */
6904 void
6905 ill_mcast_timer_start(ip_stack_t *ipst)
6906 {
6907 	int		next;
6908 
6909 	mutex_enter(&ipst->ips_igmp_timer_lock);
6910 	next = ipst->ips_igmp_deferred_next;
6911 	ipst->ips_igmp_deferred_next = INFINITY;
6912 	mutex_exit(&ipst->ips_igmp_timer_lock);
6913 
6914 	if (next != INFINITY)
6915 		igmp_start_timers(next, ipst);
6916 
6917 	mutex_enter(&ipst->ips_mld_timer_lock);
6918 	next = ipst->ips_mld_deferred_next;
6919 	ipst->ips_mld_deferred_next = INFINITY;
6920 	mutex_exit(&ipst->ips_mld_timer_lock);
6921 
6922 	if (next != INFINITY)
6923 		mld_start_timers(next, ipst);
6924 }
6925 
6926 /*
6927  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6928  * and `ioccmd'.
6929  */
6930 void
6931 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6932 {
6933 	ill_t *ill = ipif->ipif_ill;
6934 	ipxop_t *ipx = ipsq->ipsq_xop;
6935 
6936 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6937 	ASSERT(ipx->ipx_current_ipif == NULL);
6938 	ASSERT(ipx->ipx_current_ioctl == 0);
6939 
6940 	ipx->ipx_current_done = B_FALSE;
6941 	ipx->ipx_current_ioctl = ioccmd;
6942 	mutex_enter(&ipx->ipx_lock);
6943 	ipx->ipx_current_ipif = ipif;
6944 	mutex_exit(&ipx->ipx_lock);
6945 
6946 	/*
6947 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6948 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6949 	 * references to the ipif (so that the references will eventually
6950 	 * drop to zero) and also prevents any "get" operations (e.g.,
6951 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6952 	 * operation has completed and the ipif is again in a stable state.
6953 	 *
6954 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6955 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6956 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6957 	 * ipifs will be affected.
6958 	 *
6959 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6960 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6961 	 * operate on.
6962 	 */
6963 	switch (ioccmd) {
6964 	case SIOCLIFREMOVEIF:
6965 		break;
6966 	case 0:
6967 		mutex_enter(&ill->ill_lock);
6968 		ipif = ipif->ipif_ill->ill_ipif;
6969 		for (; ipif != NULL; ipif = ipif->ipif_next)
6970 			ipif->ipif_state_flags |= IPIF_CHANGING;
6971 		mutex_exit(&ill->ill_lock);
6972 		break;
6973 	default:
6974 		mutex_enter(&ill->ill_lock);
6975 		ipif->ipif_state_flags |= IPIF_CHANGING;
6976 		mutex_exit(&ill->ill_lock);
6977 	}
6978 }
6979 
6980 /*
6981  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6982  * the next exclusive operation to begin once we ipsq_exit().  However, if
6983  * pending DLPI operations remain, then we will wait for the queue to drain
6984  * before allowing the next exclusive operation to begin.  This ensures that
6985  * DLPI operations from one exclusive operation are never improperly processed
6986  * as part of a subsequent exclusive operation.
6987  */
6988 void
6989 ipsq_current_finish(ipsq_t *ipsq)
6990 {
6991 	ipxop_t	*ipx = ipsq->ipsq_xop;
6992 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6993 	ipif_t	*ipif = ipx->ipx_current_ipif;
6994 
6995 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6996 
6997 	/*
6998 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6999 	 * (but in that case, IPIF_CHANGING will already be clear and no
7000 	 * pending DLPI messages can remain).
7001 	 */
7002 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
7003 		ill_t *ill = ipif->ipif_ill;
7004 
7005 		mutex_enter(&ill->ill_lock);
7006 		dlpi_pending = ill->ill_dlpi_pending;
7007 		if (ipx->ipx_current_ioctl == 0) {
7008 			ipif = ill->ill_ipif;
7009 			for (; ipif != NULL; ipif = ipif->ipif_next)
7010 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
7011 		} else {
7012 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
7013 		}
7014 		mutex_exit(&ill->ill_lock);
7015 	}
7016 
7017 	ASSERT(!ipx->ipx_current_done);
7018 	ipx->ipx_current_done = B_TRUE;
7019 	ipx->ipx_current_ioctl = 0;
7020 	if (dlpi_pending == DL_PRIM_INVAL) {
7021 		mutex_enter(&ipx->ipx_lock);
7022 		ipx->ipx_current_ipif = NULL;
7023 		mutex_exit(&ipx->ipx_lock);
7024 	}
7025 }
7026 
7027 /*
7028  * The ill is closing. Flush all messages on the ipsq that originated
7029  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7030  * for this ill since ipsq_enter could not have entered until then.
7031  * New messages can't be queued since the CONDEMNED flag is set.
7032  */
7033 static void
7034 ipsq_flush(ill_t *ill)
7035 {
7036 	queue_t	*q;
7037 	mblk_t	*prev;
7038 	mblk_t	*mp;
7039 	mblk_t	*mp_next;
7040 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7041 
7042 	ASSERT(IAM_WRITER_ILL(ill));
7043 
7044 	/*
7045 	 * Flush any messages sent up by the driver.
7046 	 */
7047 	mutex_enter(&ipx->ipx_lock);
7048 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7049 		mp_next = mp->b_next;
7050 		q = mp->b_queue;
7051 		if (q == ill->ill_rq || q == ill->ill_wq) {
7052 			/* dequeue mp */
7053 			if (prev == NULL)
7054 				ipx->ipx_mphead = mp->b_next;
7055 			else
7056 				prev->b_next = mp->b_next;
7057 			if (ipx->ipx_mptail == mp) {
7058 				ASSERT(mp_next == NULL);
7059 				ipx->ipx_mptail = prev;
7060 			}
7061 			inet_freemsg(mp);
7062 		} else {
7063 			prev = mp;
7064 		}
7065 	}
7066 	mutex_exit(&ipx->ipx_lock);
7067 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7068 	ipsq_xopq_mp_cleanup(ill, NULL);
7069 }
7070 
7071 /*
7072  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7073  * and return the associated ipif.
7074  * Return value:
7075  *	Non zero: An error has occurred. ci may not be filled out.
7076  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7077  *	a held ipif in ci.ci_ipif.
7078  */
7079 int
7080 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7081     cmd_info_t *ci)
7082 {
7083 	char		*name;
7084 	struct ifreq    *ifr;
7085 	struct lifreq    *lifr;
7086 	ipif_t		*ipif = NULL;
7087 	ill_t		*ill;
7088 	conn_t		*connp;
7089 	boolean_t	isv6;
7090 	boolean_t	exists;
7091 	mblk_t		*mp1;
7092 	zoneid_t	zoneid;
7093 	ip_stack_t	*ipst;
7094 
7095 	if (q->q_next != NULL) {
7096 		ill = (ill_t *)q->q_ptr;
7097 		isv6 = ill->ill_isv6;
7098 		connp = NULL;
7099 		zoneid = ALL_ZONES;
7100 		ipst = ill->ill_ipst;
7101 	} else {
7102 		ill = NULL;
7103 		connp = Q_TO_CONN(q);
7104 		isv6 = (connp->conn_family == AF_INET6);
7105 		zoneid = connp->conn_zoneid;
7106 		if (zoneid == GLOBAL_ZONEID) {
7107 			/* global zone can access ipifs in all zones */
7108 			zoneid = ALL_ZONES;
7109 		}
7110 		ipst = connp->conn_netstack->netstack_ip;
7111 	}
7112 
7113 	/* Has been checked in ip_wput_nondata */
7114 	mp1 = mp->b_cont->b_cont;
7115 
7116 	if (ipip->ipi_cmd_type == IF_CMD) {
7117 		/* This a old style SIOC[GS]IF* command */
7118 		ifr = (struct ifreq *)mp1->b_rptr;
7119 		/*
7120 		 * Null terminate the string to protect against buffer
7121 		 * overrun. String was generated by user code and may not
7122 		 * be trusted.
7123 		 */
7124 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7125 		name = ifr->ifr_name;
7126 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7127 		ci->ci_sin6 = NULL;
7128 		ci->ci_lifr = (struct lifreq *)ifr;
7129 	} else {
7130 		/* This a new style SIOC[GS]LIF* command */
7131 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7132 		lifr = (struct lifreq *)mp1->b_rptr;
7133 		/*
7134 		 * Null terminate the string to protect against buffer
7135 		 * overrun. String was generated by user code and may not
7136 		 * be trusted.
7137 		 */
7138 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7139 		name = lifr->lifr_name;
7140 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7141 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7142 		ci->ci_lifr = lifr;
7143 	}
7144 
7145 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7146 		/*
7147 		 * The ioctl will be failed if the ioctl comes down
7148 		 * an conn stream
7149 		 */
7150 		if (ill == NULL) {
7151 			/*
7152 			 * Not an ill queue, return EINVAL same as the
7153 			 * old error code.
7154 			 */
7155 			return (ENXIO);
7156 		}
7157 		ipif = ill->ill_ipif;
7158 		ipif_refhold(ipif);
7159 	} else {
7160 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7161 		    &exists, isv6, zoneid, ipst);
7162 
7163 		/*
7164 		 * Ensure that get ioctls don't see any internal state changes
7165 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7166 		 * set.
7167 		 */
7168 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7169 		    !IAM_WRITER_IPIF(ipif)) {
7170 			ipsq_t	*ipsq;
7171 
7172 			if (connp != NULL)
7173 				mutex_enter(&connp->conn_lock);
7174 			mutex_enter(&ipif->ipif_ill->ill_lock);
7175 			if (IPIF_IS_CHANGING(ipif) &&
7176 			    !IPIF_IS_CONDEMNED(ipif)) {
7177 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7178 				mutex_enter(&ipsq->ipsq_lock);
7179 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7180 				mutex_exit(&ipif->ipif_ill->ill_lock);
7181 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7182 				    NEW_OP, ipif->ipif_ill);
7183 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7184 				mutex_exit(&ipsq->ipsq_lock);
7185 				if (connp != NULL)
7186 					mutex_exit(&connp->conn_lock);
7187 				ipif_refrele(ipif);
7188 				return (EINPROGRESS);
7189 			}
7190 			mutex_exit(&ipif->ipif_ill->ill_lock);
7191 			if (connp != NULL)
7192 				mutex_exit(&connp->conn_lock);
7193 		}
7194 	}
7195 
7196 	/*
7197 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7198 	 */
7199 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7200 		ipif_refrele(ipif);
7201 		return (ENXIO);
7202 	}
7203 
7204 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7205 	    name[0] == '\0') {
7206 		/*
7207 		 * Handle a or a SIOC?IF* with a null name
7208 		 * during plumb (on the ill queue before the I_PLINK).
7209 		 */
7210 		ipif = ill->ill_ipif;
7211 		ipif_refhold(ipif);
7212 	}
7213 
7214 	if (ipif == NULL)
7215 		return (ENXIO);
7216 
7217 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7218 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7219 
7220 	ci->ci_ipif = ipif;
7221 	return (0);
7222 }
7223 
7224 /*
7225  * Return the total number of ipifs.
7226  */
7227 static uint_t
7228 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7229 {
7230 	uint_t numifs = 0;
7231 	ill_t	*ill;
7232 	ill_walk_context_t	ctx;
7233 	ipif_t	*ipif;
7234 
7235 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7236 	ill = ILL_START_WALK_V4(&ctx, ipst);
7237 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7238 		if (IS_UNDER_IPMP(ill))
7239 			continue;
7240 		for (ipif = ill->ill_ipif; ipif != NULL;
7241 		    ipif = ipif->ipif_next) {
7242 			if (ipif->ipif_zoneid == zoneid ||
7243 			    ipif->ipif_zoneid == ALL_ZONES)
7244 				numifs++;
7245 		}
7246 	}
7247 	rw_exit(&ipst->ips_ill_g_lock);
7248 	return (numifs);
7249 }
7250 
7251 /*
7252  * Return the total number of ipifs.
7253  */
7254 static uint_t
7255 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7256 {
7257 	uint_t numifs = 0;
7258 	ill_t	*ill;
7259 	ipif_t	*ipif;
7260 	ill_walk_context_t	ctx;
7261 
7262 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7263 
7264 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7265 	if (family == AF_INET)
7266 		ill = ILL_START_WALK_V4(&ctx, ipst);
7267 	else if (family == AF_INET6)
7268 		ill = ILL_START_WALK_V6(&ctx, ipst);
7269 	else
7270 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7271 
7272 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7273 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7274 			continue;
7275 
7276 		for (ipif = ill->ill_ipif; ipif != NULL;
7277 		    ipif = ipif->ipif_next) {
7278 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7279 			    !(lifn_flags & LIFC_NOXMIT))
7280 				continue;
7281 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7282 			    !(lifn_flags & LIFC_TEMPORARY))
7283 				continue;
7284 			if (((ipif->ipif_flags &
7285 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7286 			    IPIF_DEPRECATED)) ||
7287 			    IS_LOOPBACK(ill) ||
7288 			    !(ipif->ipif_flags & IPIF_UP)) &&
7289 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7290 				continue;
7291 
7292 			if (zoneid != ipif->ipif_zoneid &&
7293 			    ipif->ipif_zoneid != ALL_ZONES &&
7294 			    (zoneid != GLOBAL_ZONEID ||
7295 			    !(lifn_flags & LIFC_ALLZONES)))
7296 				continue;
7297 
7298 			numifs++;
7299 		}
7300 	}
7301 	rw_exit(&ipst->ips_ill_g_lock);
7302 	return (numifs);
7303 }
7304 
7305 uint_t
7306 ip_get_lifsrcofnum(ill_t *ill)
7307 {
7308 	uint_t numifs = 0;
7309 	ill_t	*ill_head = ill;
7310 	ip_stack_t	*ipst = ill->ill_ipst;
7311 
7312 	/*
7313 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7314 	 * other thread may be trying to relink the ILLs in this usesrc group
7315 	 * and adjusting the ill_usesrc_grp_next pointers
7316 	 */
7317 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7318 	if ((ill->ill_usesrc_ifindex == 0) &&
7319 	    (ill->ill_usesrc_grp_next != NULL)) {
7320 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7321 		    ill = ill->ill_usesrc_grp_next)
7322 			numifs++;
7323 	}
7324 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7325 
7326 	return (numifs);
7327 }
7328 
7329 /* Null values are passed in for ipif, sin, and ifreq */
7330 /* ARGSUSED */
7331 int
7332 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7333     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7334 {
7335 	int *nump;
7336 	conn_t *connp = Q_TO_CONN(q);
7337 
7338 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7339 
7340 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7341 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7342 
7343 	*nump = ip_get_numifs(connp->conn_zoneid,
7344 	    connp->conn_netstack->netstack_ip);
7345 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7346 	return (0);
7347 }
7348 
7349 /* Null values are passed in for ipif, sin, and ifreq */
7350 /* ARGSUSED */
7351 int
7352 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7353     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7354 {
7355 	struct lifnum *lifn;
7356 	mblk_t	*mp1;
7357 	conn_t *connp = Q_TO_CONN(q);
7358 
7359 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7360 
7361 	/* Existence checked in ip_wput_nondata */
7362 	mp1 = mp->b_cont->b_cont;
7363 
7364 	lifn = (struct lifnum *)mp1->b_rptr;
7365 	switch (lifn->lifn_family) {
7366 	case AF_UNSPEC:
7367 	case AF_INET:
7368 	case AF_INET6:
7369 		break;
7370 	default:
7371 		return (EAFNOSUPPORT);
7372 	}
7373 
7374 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7375 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7376 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7377 	return (0);
7378 }
7379 
7380 /* ARGSUSED */
7381 int
7382 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7383     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7384 {
7385 	STRUCT_HANDLE(ifconf, ifc);
7386 	mblk_t *mp1;
7387 	struct iocblk *iocp;
7388 	struct ifreq *ifr;
7389 	ill_walk_context_t	ctx;
7390 	ill_t	*ill;
7391 	ipif_t	*ipif;
7392 	struct sockaddr_in *sin;
7393 	int32_t	ifclen;
7394 	zoneid_t zoneid;
7395 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7396 
7397 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7398 
7399 	ip1dbg(("ip_sioctl_get_ifconf"));
7400 	/* Existence verified in ip_wput_nondata */
7401 	mp1 = mp->b_cont->b_cont;
7402 	iocp = (struct iocblk *)mp->b_rptr;
7403 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7404 
7405 	/*
7406 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7407 	 * the user buffer address and length into which the list of struct
7408 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7409 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7410 	 * the SIOCGIFCONF operation was redefined to simply provide
7411 	 * a large output buffer into which we are supposed to jam the ifreq
7412 	 * array.  The same ioctl command code was used, despite the fact that
7413 	 * both the applications and the kernel code had to change, thus making
7414 	 * it impossible to support both interfaces.
7415 	 *
7416 	 * For reasons not good enough to try to explain, the following
7417 	 * algorithm is used for deciding what to do with one of these:
7418 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7419 	 * form with the output buffer coming down as the continuation message.
7420 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7421 	 * and we have to copy in the ifconf structure to find out how big the
7422 	 * output buffer is and where to copy out to.  Sure no problem...
7423 	 *
7424 	 */
7425 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7426 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7427 		int numifs = 0;
7428 		size_t ifc_bufsize;
7429 
7430 		/*
7431 		 * Must be (better be!) continuation of a TRANSPARENT
7432 		 * IOCTL.  We just copied in the ifconf structure.
7433 		 */
7434 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7435 		    (struct ifconf *)mp1->b_rptr);
7436 
7437 		/*
7438 		 * Allocate a buffer to hold requested information.
7439 		 *
7440 		 * If ifc_len is larger than what is needed, we only
7441 		 * allocate what we will use.
7442 		 *
7443 		 * If ifc_len is smaller than what is needed, return
7444 		 * EINVAL.
7445 		 *
7446 		 * XXX: the ill_t structure can hava 2 counters, for
7447 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7448 		 * number of interfaces for a device, so we don't need
7449 		 * to count them here...
7450 		 */
7451 		numifs = ip_get_numifs(zoneid, ipst);
7452 
7453 		ifclen = STRUCT_FGET(ifc, ifc_len);
7454 		ifc_bufsize = numifs * sizeof (struct ifreq);
7455 		if (ifc_bufsize > ifclen) {
7456 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7457 				/* old behaviour */
7458 				return (EINVAL);
7459 			} else {
7460 				ifc_bufsize = ifclen;
7461 			}
7462 		}
7463 
7464 		mp1 = mi_copyout_alloc(q, mp,
7465 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7466 		if (mp1 == NULL)
7467 			return (ENOMEM);
7468 
7469 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7470 	}
7471 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7472 	/*
7473 	 * the SIOCGIFCONF ioctl only knows about
7474 	 * IPv4 addresses, so don't try to tell
7475 	 * it about interfaces with IPv6-only
7476 	 * addresses. (Last parm 'isv6' is B_FALSE)
7477 	 */
7478 
7479 	ifr = (struct ifreq *)mp1->b_rptr;
7480 
7481 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7482 	ill = ILL_START_WALK_V4(&ctx, ipst);
7483 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7484 		if (IS_UNDER_IPMP(ill))
7485 			continue;
7486 		for (ipif = ill->ill_ipif; ipif != NULL;
7487 		    ipif = ipif->ipif_next) {
7488 			if (zoneid != ipif->ipif_zoneid &&
7489 			    ipif->ipif_zoneid != ALL_ZONES)
7490 				continue;
7491 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7492 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7493 					/* old behaviour */
7494 					rw_exit(&ipst->ips_ill_g_lock);
7495 					return (EINVAL);
7496 				} else {
7497 					goto if_copydone;
7498 				}
7499 			}
7500 			ipif_get_name(ipif, ifr->ifr_name,
7501 			    sizeof (ifr->ifr_name));
7502 			sin = (sin_t *)&ifr->ifr_addr;
7503 			*sin = sin_null;
7504 			sin->sin_family = AF_INET;
7505 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7506 			ifr++;
7507 		}
7508 	}
7509 if_copydone:
7510 	rw_exit(&ipst->ips_ill_g_lock);
7511 	mp1->b_wptr = (uchar_t *)ifr;
7512 
7513 	if (STRUCT_BUF(ifc) != NULL) {
7514 		STRUCT_FSET(ifc, ifc_len,
7515 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7516 	}
7517 	return (0);
7518 }
7519 
7520 /*
7521  * Get the interfaces using the address hosted on the interface passed in,
7522  * as a source adddress
7523  */
7524 /* ARGSUSED */
7525 int
7526 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7527     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7528 {
7529 	mblk_t *mp1;
7530 	ill_t	*ill, *ill_head;
7531 	ipif_t	*ipif, *orig_ipif;
7532 	int	numlifs = 0;
7533 	size_t	lifs_bufsize, lifsmaxlen;
7534 	struct	lifreq *lifr;
7535 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7536 	uint_t	ifindex;
7537 	zoneid_t zoneid;
7538 	boolean_t isv6 = B_FALSE;
7539 	struct	sockaddr_in	*sin;
7540 	struct	sockaddr_in6	*sin6;
7541 	STRUCT_HANDLE(lifsrcof, lifs);
7542 	ip_stack_t		*ipst;
7543 
7544 	ipst = CONNQ_TO_IPST(q);
7545 
7546 	ASSERT(q->q_next == NULL);
7547 
7548 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7549 
7550 	/* Existence verified in ip_wput_nondata */
7551 	mp1 = mp->b_cont->b_cont;
7552 
7553 	/*
7554 	 * Must be (better be!) continuation of a TRANSPARENT
7555 	 * IOCTL.  We just copied in the lifsrcof structure.
7556 	 */
7557 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7558 	    (struct lifsrcof *)mp1->b_rptr);
7559 
7560 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7561 		return (EINVAL);
7562 
7563 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7564 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7565 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7566 	if (ipif == NULL) {
7567 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7568 		    ifindex));
7569 		return (ENXIO);
7570 	}
7571 
7572 	/* Allocate a buffer to hold requested information */
7573 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7574 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7575 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7576 	/* The actual size needed is always returned in lifs_len */
7577 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7578 
7579 	/* If the amount we need is more than what is passed in, abort */
7580 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7581 		ipif_refrele(ipif);
7582 		return (0);
7583 	}
7584 
7585 	mp1 = mi_copyout_alloc(q, mp,
7586 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7587 	if (mp1 == NULL) {
7588 		ipif_refrele(ipif);
7589 		return (ENOMEM);
7590 	}
7591 
7592 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7593 	bzero(mp1->b_rptr, lifs_bufsize);
7594 
7595 	lifr = (struct lifreq *)mp1->b_rptr;
7596 
7597 	ill = ill_head = ipif->ipif_ill;
7598 	orig_ipif = ipif;
7599 
7600 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7601 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7602 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7603 
7604 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7605 	for (; (ill != NULL) && (ill != ill_head);
7606 	    ill = ill->ill_usesrc_grp_next) {
7607 
7608 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7609 			break;
7610 
7611 		ipif = ill->ill_ipif;
7612 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7613 		if (ipif->ipif_isv6) {
7614 			sin6 = (sin6_t *)&lifr->lifr_addr;
7615 			*sin6 = sin6_null;
7616 			sin6->sin6_family = AF_INET6;
7617 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7618 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7619 			    &ipif->ipif_v6net_mask);
7620 		} else {
7621 			sin = (sin_t *)&lifr->lifr_addr;
7622 			*sin = sin_null;
7623 			sin->sin_family = AF_INET;
7624 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7625 			lifr->lifr_addrlen = ip_mask_to_plen(
7626 			    ipif->ipif_net_mask);
7627 		}
7628 		lifr++;
7629 	}
7630 	rw_exit(&ipst->ips_ill_g_lock);
7631 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7632 	ipif_refrele(orig_ipif);
7633 	mp1->b_wptr = (uchar_t *)lifr;
7634 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7635 
7636 	return (0);
7637 }
7638 
7639 /* ARGSUSED */
7640 int
7641 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7642     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7643 {
7644 	mblk_t *mp1;
7645 	int	list;
7646 	ill_t	*ill;
7647 	ipif_t	*ipif;
7648 	int	flags;
7649 	int	numlifs = 0;
7650 	size_t	lifc_bufsize;
7651 	struct	lifreq *lifr;
7652 	sa_family_t	family;
7653 	struct	sockaddr_in	*sin;
7654 	struct	sockaddr_in6	*sin6;
7655 	ill_walk_context_t	ctx;
7656 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7657 	int32_t	lifclen;
7658 	zoneid_t zoneid;
7659 	STRUCT_HANDLE(lifconf, lifc);
7660 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7661 
7662 	ip1dbg(("ip_sioctl_get_lifconf"));
7663 
7664 	ASSERT(q->q_next == NULL);
7665 
7666 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7667 
7668 	/* Existence verified in ip_wput_nondata */
7669 	mp1 = mp->b_cont->b_cont;
7670 
7671 	/*
7672 	 * An extended version of SIOCGIFCONF that takes an
7673 	 * additional address family and flags field.
7674 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7675 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7676 	 * interfaces are omitted.
7677 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7678 	 * unless LIFC_TEMPORARY is specified.
7679 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7680 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7681 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7682 	 * has priority over LIFC_NOXMIT.
7683 	 */
7684 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7685 
7686 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7687 		return (EINVAL);
7688 
7689 	/*
7690 	 * Must be (better be!) continuation of a TRANSPARENT
7691 	 * IOCTL.  We just copied in the lifconf structure.
7692 	 */
7693 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7694 
7695 	family = STRUCT_FGET(lifc, lifc_family);
7696 	flags = STRUCT_FGET(lifc, lifc_flags);
7697 
7698 	switch (family) {
7699 	case AF_UNSPEC:
7700 		/*
7701 		 * walk all ILL's.
7702 		 */
7703 		list = MAX_G_HEADS;
7704 		break;
7705 	case AF_INET:
7706 		/*
7707 		 * walk only IPV4 ILL's.
7708 		 */
7709 		list = IP_V4_G_HEAD;
7710 		break;
7711 	case AF_INET6:
7712 		/*
7713 		 * walk only IPV6 ILL's.
7714 		 */
7715 		list = IP_V6_G_HEAD;
7716 		break;
7717 	default:
7718 		return (EAFNOSUPPORT);
7719 	}
7720 
7721 	/*
7722 	 * Allocate a buffer to hold requested information.
7723 	 *
7724 	 * If lifc_len is larger than what is needed, we only
7725 	 * allocate what we will use.
7726 	 *
7727 	 * If lifc_len is smaller than what is needed, return
7728 	 * EINVAL.
7729 	 */
7730 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7731 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7732 	lifclen = STRUCT_FGET(lifc, lifc_len);
7733 	if (lifc_bufsize > lifclen) {
7734 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7735 			return (EINVAL);
7736 		else
7737 			lifc_bufsize = lifclen;
7738 	}
7739 
7740 	mp1 = mi_copyout_alloc(q, mp,
7741 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7742 	if (mp1 == NULL)
7743 		return (ENOMEM);
7744 
7745 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7746 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7747 
7748 	lifr = (struct lifreq *)mp1->b_rptr;
7749 
7750 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7751 	ill = ill_first(list, list, &ctx, ipst);
7752 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7753 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7754 			continue;
7755 
7756 		for (ipif = ill->ill_ipif; ipif != NULL;
7757 		    ipif = ipif->ipif_next) {
7758 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7759 			    !(flags & LIFC_NOXMIT))
7760 				continue;
7761 
7762 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7763 			    !(flags & LIFC_TEMPORARY))
7764 				continue;
7765 
7766 			if (((ipif->ipif_flags &
7767 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7768 			    IPIF_DEPRECATED)) ||
7769 			    IS_LOOPBACK(ill) ||
7770 			    !(ipif->ipif_flags & IPIF_UP)) &&
7771 			    (flags & LIFC_EXTERNAL_SOURCE))
7772 				continue;
7773 
7774 			if (zoneid != ipif->ipif_zoneid &&
7775 			    ipif->ipif_zoneid != ALL_ZONES &&
7776 			    (zoneid != GLOBAL_ZONEID ||
7777 			    !(flags & LIFC_ALLZONES)))
7778 				continue;
7779 
7780 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7781 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7782 					rw_exit(&ipst->ips_ill_g_lock);
7783 					return (EINVAL);
7784 				} else {
7785 					goto lif_copydone;
7786 				}
7787 			}
7788 
7789 			ipif_get_name(ipif, lifr->lifr_name,
7790 			    sizeof (lifr->lifr_name));
7791 			lifr->lifr_type = ill->ill_type;
7792 			if (ipif->ipif_isv6) {
7793 				sin6 = (sin6_t *)&lifr->lifr_addr;
7794 				*sin6 = sin6_null;
7795 				sin6->sin6_family = AF_INET6;
7796 				sin6->sin6_addr =
7797 				    ipif->ipif_v6lcl_addr;
7798 				lifr->lifr_addrlen =
7799 				    ip_mask_to_plen_v6(
7800 				    &ipif->ipif_v6net_mask);
7801 			} else {
7802 				sin = (sin_t *)&lifr->lifr_addr;
7803 				*sin = sin_null;
7804 				sin->sin_family = AF_INET;
7805 				sin->sin_addr.s_addr =
7806 				    ipif->ipif_lcl_addr;
7807 				lifr->lifr_addrlen =
7808 				    ip_mask_to_plen(
7809 				    ipif->ipif_net_mask);
7810 			}
7811 			lifr++;
7812 		}
7813 	}
7814 lif_copydone:
7815 	rw_exit(&ipst->ips_ill_g_lock);
7816 
7817 	mp1->b_wptr = (uchar_t *)lifr;
7818 	if (STRUCT_BUF(lifc) != NULL) {
7819 		STRUCT_FSET(lifc, lifc_len,
7820 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7821 	}
7822 	return (0);
7823 }
7824 
7825 static void
7826 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7827 {
7828 	ip6_asp_t *table;
7829 	size_t table_size;
7830 	mblk_t *data_mp;
7831 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7832 	ip_stack_t	*ipst;
7833 
7834 	if (q->q_next == NULL)
7835 		ipst = CONNQ_TO_IPST(q);
7836 	else
7837 		ipst = ILLQ_TO_IPST(q);
7838 
7839 	/* These two ioctls are I_STR only */
7840 	if (iocp->ioc_count == TRANSPARENT) {
7841 		miocnak(q, mp, 0, EINVAL);
7842 		return;
7843 	}
7844 
7845 	data_mp = mp->b_cont;
7846 	if (data_mp == NULL) {
7847 		/* The user passed us a NULL argument */
7848 		table = NULL;
7849 		table_size = iocp->ioc_count;
7850 	} else {
7851 		/*
7852 		 * The user provided a table.  The stream head
7853 		 * may have copied in the user data in chunks,
7854 		 * so make sure everything is pulled up
7855 		 * properly.
7856 		 */
7857 		if (MBLKL(data_mp) < iocp->ioc_count) {
7858 			mblk_t *new_data_mp;
7859 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7860 			    NULL) {
7861 				miocnak(q, mp, 0, ENOMEM);
7862 				return;
7863 			}
7864 			freemsg(data_mp);
7865 			data_mp = new_data_mp;
7866 			mp->b_cont = data_mp;
7867 		}
7868 		table = (ip6_asp_t *)data_mp->b_rptr;
7869 		table_size = iocp->ioc_count;
7870 	}
7871 
7872 	switch (iocp->ioc_cmd) {
7873 	case SIOCGIP6ADDRPOLICY:
7874 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7875 		if (iocp->ioc_rval == -1)
7876 			iocp->ioc_error = EINVAL;
7877 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7878 		else if (table != NULL &&
7879 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7880 			ip6_asp_t *src = table;
7881 			ip6_asp32_t *dst = (void *)table;
7882 			int count = table_size / sizeof (ip6_asp_t);
7883 			int i;
7884 
7885 			/*
7886 			 * We need to do an in-place shrink of the array
7887 			 * to match the alignment attributes of the
7888 			 * 32-bit ABI looking at it.
7889 			 */
7890 			/* LINTED: logical expression always true: op "||" */
7891 			ASSERT(sizeof (*src) > sizeof (*dst));
7892 			for (i = 1; i < count; i++)
7893 				bcopy(src + i, dst + i, sizeof (*dst));
7894 		}
7895 #endif
7896 		break;
7897 
7898 	case SIOCSIP6ADDRPOLICY:
7899 		ASSERT(mp->b_prev == NULL);
7900 		mp->b_prev = (void *)q;
7901 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7902 		/*
7903 		 * We pass in the datamodel here so that the ip6_asp_replace()
7904 		 * routine can handle converting from 32-bit to native formats
7905 		 * where necessary.
7906 		 *
7907 		 * A better way to handle this might be to convert the inbound
7908 		 * data structure here, and hang it off a new 'mp'; thus the
7909 		 * ip6_asp_replace() logic would always be dealing with native
7910 		 * format data structures..
7911 		 *
7912 		 * (An even simpler way to handle these ioctls is to just
7913 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7914 		 * and just recompile everything that depends on it.)
7915 		 */
7916 #endif
7917 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7918 		    iocp->ioc_flag & IOC_MODELS);
7919 		return;
7920 	}
7921 
7922 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7923 	qreply(q, mp);
7924 }
7925 
7926 static void
7927 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7928 {
7929 	mblk_t 		*data_mp;
7930 	struct dstinforeq	*dir;
7931 	uint8_t		*end, *cur;
7932 	in6_addr_t	*daddr, *saddr;
7933 	ipaddr_t	v4daddr;
7934 	ire_t		*ire;
7935 	ipaddr_t	v4setsrc;
7936 	in6_addr_t	v6setsrc;
7937 	char		*slabel, *dlabel;
7938 	boolean_t	isipv4;
7939 	int		match_ire;
7940 	ill_t		*dst_ill;
7941 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7942 	conn_t		*connp = Q_TO_CONN(q);
7943 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7944 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7945 	uint64_t	ipif_flags;
7946 
7947 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7948 
7949 	/*
7950 	 * This ioctl is I_STR only, and must have a
7951 	 * data mblk following the M_IOCTL mblk.
7952 	 */
7953 	data_mp = mp->b_cont;
7954 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7955 		miocnak(q, mp, 0, EINVAL);
7956 		return;
7957 	}
7958 
7959 	if (MBLKL(data_mp) < iocp->ioc_count) {
7960 		mblk_t *new_data_mp;
7961 
7962 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7963 			miocnak(q, mp, 0, ENOMEM);
7964 			return;
7965 		}
7966 		freemsg(data_mp);
7967 		data_mp = new_data_mp;
7968 		mp->b_cont = data_mp;
7969 	}
7970 	match_ire = MATCH_IRE_DSTONLY;
7971 
7972 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7973 	    end - cur >= sizeof (struct dstinforeq);
7974 	    cur += sizeof (struct dstinforeq)) {
7975 		dir = (struct dstinforeq *)cur;
7976 		daddr = &dir->dir_daddr;
7977 		saddr = &dir->dir_saddr;
7978 
7979 		/*
7980 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7981 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7982 		 * and ip_select_source_v6() do not.
7983 		 */
7984 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7985 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7986 
7987 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7988 		if (isipv4) {
7989 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7990 			v4setsrc = INADDR_ANY;
7991 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7992 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7993 			    NULL, NULL);
7994 		} else {
7995 			v6setsrc = ipv6_all_zeros;
7996 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7997 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7998 			    NULL, NULL);
7999 		}
8000 		ASSERT(ire != NULL);
8001 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
8002 			ire_refrele(ire);
8003 			dir->dir_dreachable = 0;
8004 
8005 			/* move on to next dst addr */
8006 			continue;
8007 		}
8008 		dir->dir_dreachable = 1;
8009 
8010 		dst_ill = ire_nexthop_ill(ire);
8011 		if (dst_ill == NULL) {
8012 			ire_refrele(ire);
8013 			continue;
8014 		}
8015 
8016 		/* With ipmp we most likely look at the ipmp ill here */
8017 		dir->dir_dmactype = dst_ill->ill_mactype;
8018 
8019 		if (isipv4) {
8020 			ipaddr_t v4saddr;
8021 
8022 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
8023 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
8024 			    &v4saddr, NULL, &ipif_flags) != 0) {
8025 				v4saddr = INADDR_ANY;
8026 				ipif_flags = 0;
8027 			}
8028 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
8029 		} else {
8030 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
8031 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8032 			    saddr, NULL, &ipif_flags) != 0) {
8033 				*saddr = ipv6_all_zeros;
8034 				ipif_flags = 0;
8035 			}
8036 		}
8037 
8038 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8039 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
8040 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8041 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8042 		ire_refrele(ire);
8043 		ill_refrele(dst_ill);
8044 	}
8045 	miocack(q, mp, iocp->ioc_count, 0);
8046 }
8047 
8048 /*
8049  * Check if this is an address assigned to this machine.
8050  * Skips interfaces that are down by using ire checks.
8051  * Translates mapped addresses to v4 addresses and then
8052  * treats them as such, returning true if the v4 address
8053  * associated with this mapped address is configured.
8054  * Note: Applications will have to be careful what they do
8055  * with the response; use of mapped addresses limits
8056  * what can be done with the socket, especially with
8057  * respect to socket options and ioctls - neither IPv4
8058  * options nor IPv6 sticky options/ancillary data options
8059  * may be used.
8060  */
8061 /* ARGSUSED */
8062 int
8063 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8064     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8065 {
8066 	struct sioc_addrreq *sia;
8067 	sin_t *sin;
8068 	ire_t *ire;
8069 	mblk_t *mp1;
8070 	zoneid_t zoneid;
8071 	ip_stack_t	*ipst;
8072 
8073 	ip1dbg(("ip_sioctl_tmyaddr"));
8074 
8075 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8076 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8077 	ipst = CONNQ_TO_IPST(q);
8078 
8079 	/* Existence verified in ip_wput_nondata */
8080 	mp1 = mp->b_cont->b_cont;
8081 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8082 	sin = (sin_t *)&sia->sa_addr;
8083 	switch (sin->sin_family) {
8084 	case AF_INET6: {
8085 		sin6_t *sin6 = (sin6_t *)sin;
8086 
8087 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8088 			ipaddr_t v4_addr;
8089 
8090 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8091 			    v4_addr);
8092 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8093 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8094 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8095 		} else {
8096 			in6_addr_t v6addr;
8097 
8098 			v6addr = sin6->sin6_addr;
8099 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8100 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8101 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8102 		}
8103 		break;
8104 	}
8105 	case AF_INET: {
8106 		ipaddr_t v4addr;
8107 
8108 		v4addr = sin->sin_addr.s_addr;
8109 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8110 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8111 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8112 		break;
8113 	}
8114 	default:
8115 		return (EAFNOSUPPORT);
8116 	}
8117 	if (ire != NULL) {
8118 		sia->sa_res = 1;
8119 		ire_refrele(ire);
8120 	} else {
8121 		sia->sa_res = 0;
8122 	}
8123 	return (0);
8124 }
8125 
8126 /*
8127  * Check if this is an address assigned on-link i.e. neighbor,
8128  * and makes sure it's reachable from the current zone.
8129  * Returns true for my addresses as well.
8130  * Translates mapped addresses to v4 addresses and then
8131  * treats them as such, returning true if the v4 address
8132  * associated with this mapped address is configured.
8133  * Note: Applications will have to be careful what they do
8134  * with the response; use of mapped addresses limits
8135  * what can be done with the socket, especially with
8136  * respect to socket options and ioctls - neither IPv4
8137  * options nor IPv6 sticky options/ancillary data options
8138  * may be used.
8139  */
8140 /* ARGSUSED */
8141 int
8142 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8143     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8144 {
8145 	struct sioc_addrreq *sia;
8146 	sin_t *sin;
8147 	mblk_t	*mp1;
8148 	ire_t *ire = NULL;
8149 	zoneid_t zoneid;
8150 	ip_stack_t	*ipst;
8151 
8152 	ip1dbg(("ip_sioctl_tonlink"));
8153 
8154 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8155 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8156 	ipst = CONNQ_TO_IPST(q);
8157 
8158 	/* Existence verified in ip_wput_nondata */
8159 	mp1 = mp->b_cont->b_cont;
8160 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8161 	sin = (sin_t *)&sia->sa_addr;
8162 
8163 	/*
8164 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8165 	 * to make sure we only look at on-link unicast address.
8166 	 */
8167 	switch (sin->sin_family) {
8168 	case AF_INET6: {
8169 		sin6_t *sin6 = (sin6_t *)sin;
8170 
8171 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8172 			ipaddr_t v4_addr;
8173 
8174 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8175 			    v4_addr);
8176 			if (!CLASSD(v4_addr)) {
8177 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8178 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8179 				    0, ipst, NULL);
8180 			}
8181 		} else {
8182 			in6_addr_t v6addr;
8183 
8184 			v6addr = sin6->sin6_addr;
8185 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8186 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8187 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8188 				    ipst, NULL);
8189 			}
8190 		}
8191 		break;
8192 	}
8193 	case AF_INET: {
8194 		ipaddr_t v4addr;
8195 
8196 		v4addr = sin->sin_addr.s_addr;
8197 		if (!CLASSD(v4addr)) {
8198 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8199 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8200 		}
8201 		break;
8202 	}
8203 	default:
8204 		return (EAFNOSUPPORT);
8205 	}
8206 	sia->sa_res = 0;
8207 	if (ire != NULL) {
8208 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8209 
8210 		if ((ire->ire_type & IRE_ONLINK) &&
8211 		    !(ire->ire_type & IRE_BROADCAST))
8212 			sia->sa_res = 1;
8213 		ire_refrele(ire);
8214 	}
8215 	return (0);
8216 }
8217 
8218 /*
8219  * TBD: implement when kernel maintaines a list of site prefixes.
8220  */
8221 /* ARGSUSED */
8222 int
8223 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8224     ip_ioctl_cmd_t *ipip, void *ifreq)
8225 {
8226 	return (ENXIO);
8227 }
8228 
8229 /* ARP IOCTLs. */
8230 /* ARGSUSED */
8231 int
8232 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8233     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8234 {
8235 	int		err;
8236 	ipaddr_t	ipaddr;
8237 	struct iocblk	*iocp;
8238 	conn_t		*connp;
8239 	struct arpreq	*ar;
8240 	struct xarpreq	*xar;
8241 	int		arp_flags, flags, alength;
8242 	uchar_t		*lladdr;
8243 	ip_stack_t	*ipst;
8244 	ill_t		*ill = ipif->ipif_ill;
8245 	ill_t		*proxy_ill = NULL;
8246 	ipmp_arpent_t	*entp = NULL;
8247 	boolean_t	proxyarp = B_FALSE;
8248 	boolean_t	if_arp_ioctl = B_FALSE;
8249 	ncec_t		*ncec = NULL;
8250 	nce_t		*nce;
8251 
8252 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8253 	connp = Q_TO_CONN(q);
8254 	ipst = connp->conn_netstack->netstack_ip;
8255 	iocp = (struct iocblk *)mp->b_rptr;
8256 
8257 	if (ipip->ipi_cmd_type == XARP_CMD) {
8258 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8259 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8260 		ar = NULL;
8261 
8262 		arp_flags = xar->xarp_flags;
8263 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8264 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8265 		/*
8266 		 * Validate against user's link layer address length
8267 		 * input and name and addr length limits.
8268 		 */
8269 		alength = ill->ill_phys_addr_length;
8270 		if (ipip->ipi_cmd == SIOCSXARP) {
8271 			if (alength != xar->xarp_ha.sdl_alen ||
8272 			    (alength + xar->xarp_ha.sdl_nlen >
8273 			    sizeof (xar->xarp_ha.sdl_data)))
8274 				return (EINVAL);
8275 		}
8276 	} else {
8277 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8278 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8279 		xar = NULL;
8280 
8281 		arp_flags = ar->arp_flags;
8282 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8283 		/*
8284 		 * Theoretically, the sa_family could tell us what link
8285 		 * layer type this operation is trying to deal with. By
8286 		 * common usage AF_UNSPEC means ethernet. We'll assume
8287 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8288 		 * for now. Our new SIOC*XARP ioctls can be used more
8289 		 * generally.
8290 		 *
8291 		 * If the underlying media happens to have a non 6 byte
8292 		 * address, arp module will fail set/get, but the del
8293 		 * operation will succeed.
8294 		 */
8295 		alength = 6;
8296 		if ((ipip->ipi_cmd != SIOCDARP) &&
8297 		    (alength != ill->ill_phys_addr_length)) {
8298 			return (EINVAL);
8299 		}
8300 	}
8301 
8302 	/* Translate ATF* flags to NCE* flags */
8303 	flags = 0;
8304 	if (arp_flags & ATF_AUTHORITY)
8305 		flags |= NCE_F_AUTHORITY;
8306 	if (arp_flags & ATF_PERM)
8307 		flags |= NCE_F_NONUD; /* not subject to aging */
8308 	if (arp_flags & ATF_PUBL)
8309 		flags |= NCE_F_PUBLISH;
8310 
8311 	/*
8312 	 * IPMP ARP special handling:
8313 	 *
8314 	 * 1. Since ARP mappings must appear consistent across the group,
8315 	 *    prohibit changing ARP mappings on the underlying interfaces.
8316 	 *
8317 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8318 	 *    IP itself, prohibit changing them.
8319 	 *
8320 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8321 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8322 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8323 	 */
8324 	if (IS_UNDER_IPMP(ill)) {
8325 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8326 			return (EPERM);
8327 	}
8328 	if (IS_IPMP(ill)) {
8329 		ipmp_illgrp_t *illg = ill->ill_grp;
8330 
8331 		switch (ipip->ipi_cmd) {
8332 		case SIOCSARP:
8333 		case SIOCSXARP:
8334 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8335 			if (proxy_ill != NULL) {
8336 				proxyarp = B_TRUE;
8337 				if (!ipmp_ill_is_active(proxy_ill))
8338 					proxy_ill = ipmp_illgrp_next_ill(illg);
8339 				if (proxy_ill != NULL)
8340 					lladdr = proxy_ill->ill_phys_addr;
8341 			}
8342 			/* FALLTHRU */
8343 		}
8344 	}
8345 
8346 	ipaddr = sin->sin_addr.s_addr;
8347 	/*
8348 	 * don't match across illgrp per case (1) and (2).
8349 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8350 	 */
8351 	nce = nce_lookup_v4(ill, &ipaddr);
8352 	if (nce != NULL)
8353 		ncec = nce->nce_common;
8354 
8355 	switch (iocp->ioc_cmd) {
8356 	case SIOCDARP:
8357 	case SIOCDXARP: {
8358 		/*
8359 		 * Delete the NCE if any.
8360 		 */
8361 		if (ncec == NULL) {
8362 			iocp->ioc_error = ENXIO;
8363 			break;
8364 		}
8365 		/* Don't allow changes to arp mappings of local addresses. */
8366 		if (NCE_MYADDR(ncec)) {
8367 			nce_refrele(nce);
8368 			return (ENOTSUP);
8369 		}
8370 		iocp->ioc_error = 0;
8371 
8372 		/*
8373 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8374 		 * This will delete all the nce entries on the under_ills.
8375 		 */
8376 		ncec_delete(ncec);
8377 		/*
8378 		 * Once the NCE has been deleted, then the ire_dep* consistency
8379 		 * mechanism will find any IRE which depended on the now
8380 		 * condemned NCE (as part of sending packets).
8381 		 * That mechanism handles redirects by deleting redirects
8382 		 * that refer to UNREACHABLE nces.
8383 		 */
8384 		break;
8385 	}
8386 	case SIOCGARP:
8387 	case SIOCGXARP:
8388 		if (ncec != NULL) {
8389 			lladdr = ncec->ncec_lladdr;
8390 			flags = ncec->ncec_flags;
8391 			iocp->ioc_error = 0;
8392 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8393 		} else {
8394 			iocp->ioc_error = ENXIO;
8395 		}
8396 		break;
8397 	case SIOCSARP:
8398 	case SIOCSXARP:
8399 		/* Don't allow changes to arp mappings of local addresses. */
8400 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8401 			nce_refrele(nce);
8402 			return (ENOTSUP);
8403 		}
8404 
8405 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8406 		flags |= NCE_F_STATIC;
8407 		if (!if_arp_ioctl) {
8408 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8409 			    lladdr, alength, flags);
8410 		} else {
8411 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8412 			if (ipif != NULL) {
8413 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8414 				    lladdr, alength, flags);
8415 				ipif_refrele(ipif);
8416 			}
8417 		}
8418 		if (nce != NULL) {
8419 			nce_refrele(nce);
8420 			nce = NULL;
8421 		}
8422 		/*
8423 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8424 		 * by nce_add_common()
8425 		 */
8426 		err = nce_lookup_then_add_v4(ill, lladdr,
8427 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8428 		    &nce);
8429 		if (err == EEXIST) {
8430 			ncec = nce->nce_common;
8431 			mutex_enter(&ncec->ncec_lock);
8432 			ncec->ncec_state = ND_REACHABLE;
8433 			ncec->ncec_flags = flags;
8434 			nce_update(ncec, ND_UNCHANGED, lladdr);
8435 			mutex_exit(&ncec->ncec_lock);
8436 			err = 0;
8437 		}
8438 		if (nce != NULL) {
8439 			nce_refrele(nce);
8440 			nce = NULL;
8441 		}
8442 		if (IS_IPMP(ill) && err == 0) {
8443 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8444 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8445 			    flags);
8446 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8447 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8448 				break;
8449 			}
8450 		}
8451 		iocp->ioc_error = err;
8452 	}
8453 
8454 	if (nce != NULL) {
8455 		nce_refrele(nce);
8456 	}
8457 
8458 	/*
8459 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8460 	 */
8461 	if (entp != NULL)
8462 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8463 
8464 	return (iocp->ioc_error);
8465 }
8466 
8467 /*
8468  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8469  * the associated sin and refhold and return the associated ipif via `ci'.
8470  */
8471 int
8472 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8473     cmd_info_t *ci)
8474 {
8475 	mblk_t	*mp1;
8476 	sin_t	*sin;
8477 	conn_t	*connp;
8478 	ipif_t	*ipif;
8479 	ire_t	*ire = NULL;
8480 	ill_t	*ill = NULL;
8481 	boolean_t exists;
8482 	ip_stack_t *ipst;
8483 	struct arpreq *ar;
8484 	struct xarpreq *xar;
8485 	struct sockaddr_dl *sdl;
8486 
8487 	/* ioctl comes down on a conn */
8488 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8489 	connp = Q_TO_CONN(q);
8490 	if (connp->conn_family == AF_INET6)
8491 		return (ENXIO);
8492 
8493 	ipst = connp->conn_netstack->netstack_ip;
8494 
8495 	/* Verified in ip_wput_nondata */
8496 	mp1 = mp->b_cont->b_cont;
8497 
8498 	if (ipip->ipi_cmd_type == XARP_CMD) {
8499 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8500 		xar = (struct xarpreq *)mp1->b_rptr;
8501 		sin = (sin_t *)&xar->xarp_pa;
8502 		sdl = &xar->xarp_ha;
8503 
8504 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8505 			return (ENXIO);
8506 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8507 			return (EINVAL);
8508 	} else {
8509 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8510 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8511 		ar = (struct arpreq *)mp1->b_rptr;
8512 		sin = (sin_t *)&ar->arp_pa;
8513 	}
8514 
8515 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8516 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8517 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8518 		if (ipif == NULL)
8519 			return (ENXIO);
8520 		if (ipif->ipif_id != 0) {
8521 			ipif_refrele(ipif);
8522 			return (ENXIO);
8523 		}
8524 	} else {
8525 		/*
8526 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8527 		 * of 0: use the IP address to find the ipif.  If the IP
8528 		 * address is an IPMP test address, ire_ftable_lookup() will
8529 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8530 		 */
8531 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8532 		    ipst);
8533 		if (ipif == NULL) {
8534 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8535 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8536 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8537 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8538 				if (ire != NULL)
8539 					ire_refrele(ire);
8540 				return (ENXIO);
8541 			}
8542 			ASSERT(ire != NULL && ill != NULL);
8543 			ipif = ill->ill_ipif;
8544 			ipif_refhold(ipif);
8545 			ire_refrele(ire);
8546 		}
8547 	}
8548 
8549 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8550 		ipif_refrele(ipif);
8551 		return (ENXIO);
8552 	}
8553 
8554 	ci->ci_sin = sin;
8555 	ci->ci_ipif = ipif;
8556 	return (0);
8557 }
8558 
8559 /*
8560  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8561  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8562  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8563  * up and thus an ill can join that illgrp.
8564  *
8565  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8566  * open()/close() primarily because close() is not allowed to fail or block
8567  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8568  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8569  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8570  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8571  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8572  * state if I_UNLINK didn't occur.
8573  *
8574  * Note that for each plumb/unplumb operation, we may end up here more than
8575  * once because of the way ifconfig works.  However, it's OK to link the same
8576  * illgrp more than once, or unlink an illgrp that's already unlinked.
8577  */
8578 static int
8579 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8580 {
8581 	int err;
8582 	ip_stack_t *ipst = ill->ill_ipst;
8583 
8584 	ASSERT(IS_IPMP(ill));
8585 	ASSERT(IAM_WRITER_ILL(ill));
8586 
8587 	switch (ioccmd) {
8588 	case I_LINK:
8589 		return (ENOTSUP);
8590 
8591 	case I_PLINK:
8592 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8593 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8594 		rw_exit(&ipst->ips_ipmp_lock);
8595 		break;
8596 
8597 	case I_PUNLINK:
8598 		/*
8599 		 * Require all UP ipifs be brought down prior to unlinking the
8600 		 * illgrp so any associated IREs (and other state) is torched.
8601 		 */
8602 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8603 			return (EBUSY);
8604 
8605 		/*
8606 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8607 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8608 		 * join this group.  Specifically: ills trying to join grab
8609 		 * ipmp_lock and bump a "pending join" counter checked by
8610 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8611 		 * joins can occur (since we have ipmp_lock).  Once we drop
8612 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8613 		 * find the illgrp (since we unlinked it) and will return
8614 		 * EAFNOSUPPORT.  This will then take them back through the
8615 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8616 		 * back through I_PLINK above.
8617 		 */
8618 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8619 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8620 		rw_exit(&ipst->ips_ipmp_lock);
8621 		return (err);
8622 	default:
8623 		break;
8624 	}
8625 	return (0);
8626 }
8627 
8628 /*
8629  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8630  * atomically set/clear the muxids. Also complete the ioctl by acking or
8631  * naking it.  Note that the code is structured such that the link type,
8632  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8633  * its clones use the persistent link, while pppd(1M) and perhaps many
8634  * other daemons may use non-persistent link.  When combined with some
8635  * ill_t states, linking and unlinking lower streams may be used as
8636  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8637  */
8638 /* ARGSUSED */
8639 void
8640 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8641 {
8642 	mblk_t		*mp1;
8643 	struct linkblk	*li;
8644 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8645 	int		err = 0;
8646 
8647 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8648 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8649 
8650 	mp1 = mp->b_cont;	/* This is the linkblk info */
8651 	li = (struct linkblk *)mp1->b_rptr;
8652 
8653 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8654 	if (err == EINPROGRESS)
8655 		return;
8656 done:
8657 	if (err == 0)
8658 		miocack(q, mp, 0, 0);
8659 	else
8660 		miocnak(q, mp, 0, err);
8661 
8662 	/* Conn was refheld in ip_sioctl_copyin_setup */
8663 	if (CONN_Q(q))
8664 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8665 }
8666 
8667 /*
8668  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8669  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8670  * module stream).  If `doconsist' is set, then do the extended consistency
8671  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8672  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8673  * an error code on failure.
8674  */
8675 static int
8676 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8677     struct linkblk *li)
8678 {
8679 	int		err = 0;
8680 	ill_t  		*ill;
8681 	queue_t		*ipwq, *dwq;
8682 	const char	*name;
8683 	struct qinit	*qinfo;
8684 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8685 	boolean_t	entered_ipsq = B_FALSE;
8686 	boolean_t	is_ip = B_FALSE;
8687 	arl_t		*arl;
8688 
8689 	/*
8690 	 * Walk the lower stream to verify it's the IP module stream.
8691 	 * The IP module is identified by its name, wput function,
8692 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8693 	 * (li->l_qbot) will not vanish until this ioctl completes.
8694 	 */
8695 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8696 		qinfo = ipwq->q_qinfo;
8697 		name = qinfo->qi_minfo->mi_idname;
8698 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8699 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8700 			is_ip = B_TRUE;
8701 			break;
8702 		}
8703 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8704 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8705 			break;
8706 		}
8707 	}
8708 
8709 	/*
8710 	 * If this isn't an IP module stream, bail.
8711 	 */
8712 	if (ipwq == NULL)
8713 		return (0);
8714 
8715 	if (!is_ip) {
8716 		arl = (arl_t *)ipwq->q_ptr;
8717 		ill = arl_to_ill(arl);
8718 		if (ill == NULL)
8719 			return (0);
8720 	} else {
8721 		ill = ipwq->q_ptr;
8722 	}
8723 	ASSERT(ill != NULL);
8724 
8725 	if (ipsq == NULL) {
8726 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8727 		    NEW_OP, B_FALSE);
8728 		if (ipsq == NULL) {
8729 			if (!is_ip)
8730 				ill_refrele(ill);
8731 			return (EINPROGRESS);
8732 		}
8733 		entered_ipsq = B_TRUE;
8734 	}
8735 	ASSERT(IAM_WRITER_ILL(ill));
8736 	mutex_enter(&ill->ill_lock);
8737 	if (!is_ip) {
8738 		if (islink && ill->ill_muxid == 0) {
8739 			/*
8740 			 * Plumbing has to be done with IP plumbed first, arp
8741 			 * second, but here we have arp being plumbed first.
8742 			 */
8743 			mutex_exit(&ill->ill_lock);
8744 			ipsq_exit(ipsq);
8745 			ill_refrele(ill);
8746 			return (EINVAL);
8747 		}
8748 	}
8749 	mutex_exit(&ill->ill_lock);
8750 	if (!is_ip) {
8751 		arl->arl_muxid = islink ? li->l_index : 0;
8752 		ill_refrele(ill);
8753 		goto done;
8754 	}
8755 
8756 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8757 		goto done;
8758 
8759 	/*
8760 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8761 	 * the read queue of the module immediately below IP in the ill.
8762 	 * These are used during the capability negotiation below.
8763 	 */
8764 	ill->ill_lmod_rq = NULL;
8765 	ill->ill_lmod_cnt = 0;
8766 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8767 		ill->ill_lmod_rq = RD(dwq);
8768 		for (; dwq != NULL; dwq = dwq->q_next)
8769 			ill->ill_lmod_cnt++;
8770 	}
8771 
8772 	ill->ill_muxid = islink ? li->l_index : 0;
8773 
8774 	/*
8775 	 * Mark the ipsq busy until the capability operations initiated below
8776 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8777 	 * returns, but the capability operation may complete asynchronously
8778 	 * much later.
8779 	 */
8780 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8781 	/*
8782 	 * If there's at least one up ipif on this ill, then we're bound to
8783 	 * the underlying driver via DLPI.  In that case, renegotiate
8784 	 * capabilities to account for any possible change in modules
8785 	 * interposed between IP and the driver.
8786 	 */
8787 	if (ill->ill_ipif_up_count > 0) {
8788 		if (islink)
8789 			ill_capability_probe(ill);
8790 		else
8791 			ill_capability_reset(ill, B_FALSE);
8792 	}
8793 	ipsq_current_finish(ipsq);
8794 done:
8795 	if (entered_ipsq)
8796 		ipsq_exit(ipsq);
8797 
8798 	return (err);
8799 }
8800 
8801 /*
8802  * Search the ioctl command in the ioctl tables and return a pointer
8803  * to the ioctl command information. The ioctl command tables are
8804  * static and fully populated at compile time.
8805  */
8806 ip_ioctl_cmd_t *
8807 ip_sioctl_lookup(int ioc_cmd)
8808 {
8809 	int index;
8810 	ip_ioctl_cmd_t *ipip;
8811 	ip_ioctl_cmd_t *ipip_end;
8812 
8813 	if (ioc_cmd == IPI_DONTCARE)
8814 		return (NULL);
8815 
8816 	/*
8817 	 * Do a 2 step search. First search the indexed table
8818 	 * based on the least significant byte of the ioctl cmd.
8819 	 * If we don't find a match, then search the misc table
8820 	 * serially.
8821 	 */
8822 	index = ioc_cmd & 0xFF;
8823 	if (index < ip_ndx_ioctl_count) {
8824 		ipip = &ip_ndx_ioctl_table[index];
8825 		if (ipip->ipi_cmd == ioc_cmd) {
8826 			/* Found a match in the ndx table */
8827 			return (ipip);
8828 		}
8829 	}
8830 
8831 	/* Search the misc table */
8832 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8833 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8834 		if (ipip->ipi_cmd == ioc_cmd)
8835 			/* Found a match in the misc table */
8836 			return (ipip);
8837 	}
8838 
8839 	return (NULL);
8840 }
8841 
8842 /*
8843  * Wrapper function for resuming deferred ioctl processing
8844  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8845  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8846  */
8847 /* ARGSUSED */
8848 void
8849 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8850     void *dummy_arg)
8851 {
8852 	ip_sioctl_copyin_setup(q, mp);
8853 }
8854 
8855 /*
8856  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8857  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8858  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8859  * We establish here the size of the block to be copied in.  mi_copyin
8860  * arranges for this to happen, an processing continues in ip_wput_nondata with
8861  * an M_IOCDATA message.
8862  */
8863 void
8864 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8865 {
8866 	int	copyin_size;
8867 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8868 	ip_ioctl_cmd_t *ipip;
8869 	cred_t *cr;
8870 	ip_stack_t	*ipst;
8871 
8872 	if (CONN_Q(q))
8873 		ipst = CONNQ_TO_IPST(q);
8874 	else
8875 		ipst = ILLQ_TO_IPST(q);
8876 
8877 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8878 	if (ipip == NULL) {
8879 		/*
8880 		 * The ioctl is not one we understand or own.
8881 		 * Pass it along to be processed down stream,
8882 		 * if this is a module instance of IP, else nak
8883 		 * the ioctl.
8884 		 */
8885 		if (q->q_next == NULL) {
8886 			goto nak;
8887 		} else {
8888 			putnext(q, mp);
8889 			return;
8890 		}
8891 	}
8892 
8893 	/*
8894 	 * If this is deferred, then we will do all the checks when we
8895 	 * come back.
8896 	 */
8897 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8898 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8899 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8900 		return;
8901 	}
8902 
8903 	/*
8904 	 * Only allow a very small subset of IP ioctls on this stream if
8905 	 * IP is a module and not a driver. Allowing ioctls to be processed
8906 	 * in this case may cause assert failures or data corruption.
8907 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8908 	 * ioctls allowed on an IP module stream, after which this stream
8909 	 * normally becomes a multiplexor (at which time the stream head
8910 	 * will fail all ioctls).
8911 	 */
8912 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8913 		goto nak;
8914 	}
8915 
8916 	/* Make sure we have ioctl data to process. */
8917 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8918 		goto nak;
8919 
8920 	/*
8921 	 * Prefer dblk credential over ioctl credential; some synthesized
8922 	 * ioctls have kcred set because there's no way to crhold()
8923 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8924 	 * the framework; the caller of ioctl needs to hold the reference
8925 	 * for the duration of the call).
8926 	 */
8927 	cr = msg_getcred(mp, NULL);
8928 	if (cr == NULL)
8929 		cr = iocp->ioc_cr;
8930 
8931 	/* Make sure normal users don't send down privileged ioctls */
8932 	if ((ipip->ipi_flags & IPI_PRIV) &&
8933 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8934 		/* We checked the privilege earlier but log it here */
8935 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8936 		return;
8937 	}
8938 
8939 	/*
8940 	 * The ioctl command tables can only encode fixed length
8941 	 * ioctl data. If the length is variable, the table will
8942 	 * encode the length as zero. Such special cases are handled
8943 	 * below in the switch.
8944 	 */
8945 	if (ipip->ipi_copyin_size != 0) {
8946 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8947 		return;
8948 	}
8949 
8950 	switch (iocp->ioc_cmd) {
8951 	case O_SIOCGIFCONF:
8952 	case SIOCGIFCONF:
8953 		/*
8954 		 * This IOCTL is hilarious.  See comments in
8955 		 * ip_sioctl_get_ifconf for the story.
8956 		 */
8957 		if (iocp->ioc_count == TRANSPARENT)
8958 			copyin_size = SIZEOF_STRUCT(ifconf,
8959 			    iocp->ioc_flag);
8960 		else
8961 			copyin_size = iocp->ioc_count;
8962 		mi_copyin(q, mp, NULL, copyin_size);
8963 		return;
8964 
8965 	case O_SIOCGLIFCONF:
8966 	case SIOCGLIFCONF:
8967 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8968 		mi_copyin(q, mp, NULL, copyin_size);
8969 		return;
8970 
8971 	case SIOCGLIFSRCOF:
8972 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8973 		mi_copyin(q, mp, NULL, copyin_size);
8974 		return;
8975 	case SIOCGIP6ADDRPOLICY:
8976 		ip_sioctl_ip6addrpolicy(q, mp);
8977 		ip6_asp_table_refrele(ipst);
8978 		return;
8979 
8980 	case SIOCSIP6ADDRPOLICY:
8981 		ip_sioctl_ip6addrpolicy(q, mp);
8982 		return;
8983 
8984 	case SIOCGDSTINFO:
8985 		ip_sioctl_dstinfo(q, mp);
8986 		ip6_asp_table_refrele(ipst);
8987 		return;
8988 
8989 	case I_PLINK:
8990 	case I_PUNLINK:
8991 	case I_LINK:
8992 	case I_UNLINK:
8993 		/*
8994 		 * We treat non-persistent link similarly as the persistent
8995 		 * link case, in terms of plumbing/unplumbing, as well as
8996 		 * dynamic re-plumbing events indicator.  See comments
8997 		 * in ip_sioctl_plink() for more.
8998 		 *
8999 		 * Request can be enqueued in the 'ipsq' while waiting
9000 		 * to become exclusive. So bump up the conn ref.
9001 		 */
9002 		if (CONN_Q(q))
9003 			CONN_INC_REF(Q_TO_CONN(q));
9004 		ip_sioctl_plink(NULL, q, mp, NULL);
9005 		return;
9006 
9007 	case ND_GET:
9008 	case ND_SET:
9009 		/*
9010 		 * Use of the nd table requires holding the reader lock.
9011 		 * Modifying the nd table thru nd_load/nd_unload requires
9012 		 * the writer lock.
9013 		 */
9014 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
9015 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
9016 			rw_exit(&ipst->ips_ip_g_nd_lock);
9017 
9018 			if (iocp->ioc_error)
9019 				iocp->ioc_count = 0;
9020 			mp->b_datap->db_type = M_IOCACK;
9021 			qreply(q, mp);
9022 			return;
9023 		}
9024 		rw_exit(&ipst->ips_ip_g_nd_lock);
9025 		/*
9026 		 * We don't understand this subioctl of ND_GET / ND_SET.
9027 		 * Maybe intended for some driver / module below us
9028 		 */
9029 		if (q->q_next) {
9030 			putnext(q, mp);
9031 		} else {
9032 			iocp->ioc_error = ENOENT;
9033 			mp->b_datap->db_type = M_IOCNAK;
9034 			iocp->ioc_count = 0;
9035 			qreply(q, mp);
9036 		}
9037 		return;
9038 
9039 	case IP_IOCTL:
9040 		ip_wput_ioctl(q, mp);
9041 		return;
9042 
9043 	case SIOCILB:
9044 		/* The ioctl length varies depending on the ILB command. */
9045 		copyin_size = iocp->ioc_count;
9046 		if (copyin_size < sizeof (ilb_cmd_t))
9047 			goto nak;
9048 		mi_copyin(q, mp, NULL, copyin_size);
9049 		return;
9050 
9051 	default:
9052 		cmn_err(CE_PANIC, "should not happen ");
9053 	}
9054 nak:
9055 	if (mp->b_cont != NULL) {
9056 		freemsg(mp->b_cont);
9057 		mp->b_cont = NULL;
9058 	}
9059 	iocp->ioc_error = EINVAL;
9060 	mp->b_datap->db_type = M_IOCNAK;
9061 	iocp->ioc_count = 0;
9062 	qreply(q, mp);
9063 }
9064 
9065 static void
9066 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9067 {
9068 	struct arpreq *ar;
9069 	struct xarpreq *xar;
9070 	mblk_t	*tmp;
9071 	struct iocblk *iocp;
9072 	int x_arp_ioctl = B_FALSE;
9073 	int *flagsp;
9074 	char *storage = NULL;
9075 
9076 	ASSERT(ill != NULL);
9077 
9078 	iocp = (struct iocblk *)mp->b_rptr;
9079 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9080 
9081 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9082 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9083 	    (iocp->ioc_cmd == SIOCSXARP)) {
9084 		x_arp_ioctl = B_TRUE;
9085 		xar = (struct xarpreq *)tmp->b_rptr;
9086 		flagsp = &xar->xarp_flags;
9087 		storage = xar->xarp_ha.sdl_data;
9088 	} else {
9089 		ar = (struct arpreq *)tmp->b_rptr;
9090 		flagsp = &ar->arp_flags;
9091 		storage = ar->arp_ha.sa_data;
9092 	}
9093 
9094 	/*
9095 	 * We're done if this is not an SIOCG{X}ARP
9096 	 */
9097 	if (x_arp_ioctl) {
9098 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9099 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9100 		    sizeof (xar->xarp_ha.sdl_data)) {
9101 			iocp->ioc_error = EINVAL;
9102 			return;
9103 		}
9104 	}
9105 	*flagsp = ATF_INUSE;
9106 	/*
9107 	 * If /sbin/arp told us we are the authority using the "permanent"
9108 	 * flag, or if this is one of my addresses print "permanent"
9109 	 * in the /sbin/arp output.
9110 	 */
9111 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9112 		*flagsp |= ATF_AUTHORITY;
9113 	if (flags & NCE_F_NONUD)
9114 		*flagsp |= ATF_PERM; /* not subject to aging */
9115 	if (flags & NCE_F_PUBLISH)
9116 		*flagsp |= ATF_PUBL;
9117 	if (hwaddr != NULL) {
9118 		*flagsp |= ATF_COM;
9119 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9120 	}
9121 }
9122 
9123 /*
9124  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9125  * interface) create the next available logical interface for this
9126  * physical interface.
9127  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9128  * ipif with the specified name.
9129  *
9130  * If the address family is not AF_UNSPEC then set the address as well.
9131  *
9132  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9133  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9134  *
9135  * Executed as a writer on the ill.
9136  * So no lock is needed to traverse the ipif chain, or examine the
9137  * phyint flags.
9138  */
9139 /* ARGSUSED */
9140 int
9141 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9142     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9143 {
9144 	mblk_t	*mp1;
9145 	struct lifreq *lifr;
9146 	boolean_t	isv6;
9147 	boolean_t	exists;
9148 	char 	*name;
9149 	char	*endp;
9150 	char	*cp;
9151 	int	namelen;
9152 	ipif_t	*ipif;
9153 	long	id;
9154 	ipsq_t	*ipsq;
9155 	ill_t	*ill;
9156 	sin_t	*sin;
9157 	int	err = 0;
9158 	boolean_t found_sep = B_FALSE;
9159 	conn_t	*connp;
9160 	zoneid_t zoneid;
9161 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9162 
9163 	ASSERT(q->q_next == NULL);
9164 	ip1dbg(("ip_sioctl_addif\n"));
9165 	/* Existence of mp1 has been checked in ip_wput_nondata */
9166 	mp1 = mp->b_cont->b_cont;
9167 	/*
9168 	 * Null terminate the string to protect against buffer
9169 	 * overrun. String was generated by user code and may not
9170 	 * be trusted.
9171 	 */
9172 	lifr = (struct lifreq *)mp1->b_rptr;
9173 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9174 	name = lifr->lifr_name;
9175 	ASSERT(CONN_Q(q));
9176 	connp = Q_TO_CONN(q);
9177 	isv6 = (connp->conn_family == AF_INET6);
9178 	zoneid = connp->conn_zoneid;
9179 	namelen = mi_strlen(name);
9180 	if (namelen == 0)
9181 		return (EINVAL);
9182 
9183 	exists = B_FALSE;
9184 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9185 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9186 		/*
9187 		 * Allow creating lo0 using SIOCLIFADDIF.
9188 		 * can't be any other writer thread. So can pass null below
9189 		 * for the last 4 args to ipif_lookup_name.
9190 		 */
9191 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9192 		    &exists, isv6, zoneid, ipst);
9193 		/* Prevent any further action */
9194 		if (ipif == NULL) {
9195 			return (ENOBUFS);
9196 		} else if (!exists) {
9197 			/* We created the ipif now and as writer */
9198 			ipif_refrele(ipif);
9199 			return (0);
9200 		} else {
9201 			ill = ipif->ipif_ill;
9202 			ill_refhold(ill);
9203 			ipif_refrele(ipif);
9204 		}
9205 	} else {
9206 		/* Look for a colon in the name. */
9207 		endp = &name[namelen];
9208 		for (cp = endp; --cp > name; ) {
9209 			if (*cp == IPIF_SEPARATOR_CHAR) {
9210 				found_sep = B_TRUE;
9211 				/*
9212 				 * Reject any non-decimal aliases for plumbing
9213 				 * of logical interfaces. Aliases with leading
9214 				 * zeroes are also rejected as they introduce
9215 				 * ambiguity in the naming of the interfaces.
9216 				 * Comparing with "0" takes care of all such
9217 				 * cases.
9218 				 */
9219 				if ((strncmp("0", cp+1, 1)) == 0)
9220 					return (EINVAL);
9221 
9222 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9223 				    id <= 0 || *endp != '\0') {
9224 					return (EINVAL);
9225 				}
9226 				*cp = '\0';
9227 				break;
9228 			}
9229 		}
9230 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9231 		if (found_sep)
9232 			*cp = IPIF_SEPARATOR_CHAR;
9233 		if (ill == NULL)
9234 			return (ENXIO);
9235 	}
9236 
9237 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9238 	    B_TRUE);
9239 
9240 	/*
9241 	 * Release the refhold due to the lookup, now that we are excl
9242 	 * or we are just returning
9243 	 */
9244 	ill_refrele(ill);
9245 
9246 	if (ipsq == NULL)
9247 		return (EINPROGRESS);
9248 
9249 	/* We are now exclusive on the IPSQ */
9250 	ASSERT(IAM_WRITER_ILL(ill));
9251 
9252 	if (found_sep) {
9253 		/* Now see if there is an IPIF with this unit number. */
9254 		for (ipif = ill->ill_ipif; ipif != NULL;
9255 		    ipif = ipif->ipif_next) {
9256 			if (ipif->ipif_id == id) {
9257 				err = EEXIST;
9258 				goto done;
9259 			}
9260 		}
9261 	}
9262 
9263 	/*
9264 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9265 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9266 	 * instead.
9267 	 */
9268 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9269 	    B_TRUE, B_TRUE, &err)) == NULL) {
9270 		goto done;
9271 	}
9272 
9273 	/* Return created name with ioctl */
9274 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9275 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9276 	ip1dbg(("created %s\n", lifr->lifr_name));
9277 
9278 	/* Set address */
9279 	sin = (sin_t *)&lifr->lifr_addr;
9280 	if (sin->sin_family != AF_UNSPEC) {
9281 		err = ip_sioctl_addr(ipif, sin, q, mp,
9282 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9283 	}
9284 
9285 done:
9286 	ipsq_exit(ipsq);
9287 	return (err);
9288 }
9289 
9290 /*
9291  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9292  * interface) delete it based on the IP address (on this physical interface).
9293  * Otherwise delete it based on the ipif_id.
9294  * Also, special handling to allow a removeif of lo0.
9295  */
9296 /* ARGSUSED */
9297 int
9298 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9299     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9300 {
9301 	conn_t		*connp;
9302 	ill_t		*ill = ipif->ipif_ill;
9303 	boolean_t	 success;
9304 	ip_stack_t	*ipst;
9305 
9306 	ipst = CONNQ_TO_IPST(q);
9307 
9308 	ASSERT(q->q_next == NULL);
9309 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9310 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9311 	ASSERT(IAM_WRITER_IPIF(ipif));
9312 
9313 	connp = Q_TO_CONN(q);
9314 	/*
9315 	 * Special case for unplumbing lo0 (the loopback physical interface).
9316 	 * If unplumbing lo0, the incoming address structure has been
9317 	 * initialized to all zeros. When unplumbing lo0, all its logical
9318 	 * interfaces must be removed too.
9319 	 *
9320 	 * Note that this interface may be called to remove a specific
9321 	 * loopback logical interface (eg, lo0:1). But in that case
9322 	 * ipif->ipif_id != 0 so that the code path for that case is the
9323 	 * same as any other interface (meaning it skips the code directly
9324 	 * below).
9325 	 */
9326 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9327 		if (sin->sin_family == AF_UNSPEC &&
9328 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9329 			/*
9330 			 * Mark it condemned. No new ref. will be made to ill.
9331 			 */
9332 			mutex_enter(&ill->ill_lock);
9333 			ill->ill_state_flags |= ILL_CONDEMNED;
9334 			for (ipif = ill->ill_ipif; ipif != NULL;
9335 			    ipif = ipif->ipif_next) {
9336 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9337 			}
9338 			mutex_exit(&ill->ill_lock);
9339 
9340 			ipif = ill->ill_ipif;
9341 			/* unplumb the loopback interface */
9342 			ill_delete(ill);
9343 			mutex_enter(&connp->conn_lock);
9344 			mutex_enter(&ill->ill_lock);
9345 
9346 			/* Are any references to this ill active */
9347 			if (ill_is_freeable(ill)) {
9348 				mutex_exit(&ill->ill_lock);
9349 				mutex_exit(&connp->conn_lock);
9350 				ill_delete_tail(ill);
9351 				mi_free(ill);
9352 				return (0);
9353 			}
9354 			success = ipsq_pending_mp_add(connp, ipif,
9355 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9356 			mutex_exit(&connp->conn_lock);
9357 			mutex_exit(&ill->ill_lock);
9358 			if (success)
9359 				return (EINPROGRESS);
9360 			else
9361 				return (EINTR);
9362 		}
9363 	}
9364 
9365 	if (ipif->ipif_id == 0) {
9366 		ipsq_t *ipsq;
9367 
9368 		/* Find based on address */
9369 		if (ipif->ipif_isv6) {
9370 			sin6_t *sin6;
9371 
9372 			if (sin->sin_family != AF_INET6)
9373 				return (EAFNOSUPPORT);
9374 
9375 			sin6 = (sin6_t *)sin;
9376 			/* We are a writer, so we should be able to lookup */
9377 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9378 			    ipst);
9379 		} else {
9380 			if (sin->sin_family != AF_INET)
9381 				return (EAFNOSUPPORT);
9382 
9383 			/* We are a writer, so we should be able to lookup */
9384 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9385 			    ipst);
9386 		}
9387 		if (ipif == NULL) {
9388 			return (EADDRNOTAVAIL);
9389 		}
9390 
9391 		/*
9392 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9393 		 * lifr_name of the physical interface but with an ip address
9394 		 * lifr_addr of a logical interface plumbed over it.
9395 		 * So update ipx_current_ipif now that ipif points to the
9396 		 * correct one.
9397 		 */
9398 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9399 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9400 
9401 		/* This is a writer */
9402 		ipif_refrele(ipif);
9403 	}
9404 
9405 	/*
9406 	 * Can not delete instance zero since it is tied to the ill.
9407 	 */
9408 	if (ipif->ipif_id == 0)
9409 		return (EBUSY);
9410 
9411 	mutex_enter(&ill->ill_lock);
9412 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9413 	mutex_exit(&ill->ill_lock);
9414 
9415 	ipif_free(ipif);
9416 
9417 	mutex_enter(&connp->conn_lock);
9418 	mutex_enter(&ill->ill_lock);
9419 
9420 	/* Are any references to this ipif active */
9421 	if (ipif_is_freeable(ipif)) {
9422 		mutex_exit(&ill->ill_lock);
9423 		mutex_exit(&connp->conn_lock);
9424 		ipif_non_duplicate(ipif);
9425 		(void) ipif_down_tail(ipif);
9426 		ipif_free_tail(ipif); /* frees ipif */
9427 		return (0);
9428 	}
9429 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9430 	    IPIF_FREE);
9431 	mutex_exit(&ill->ill_lock);
9432 	mutex_exit(&connp->conn_lock);
9433 	if (success)
9434 		return (EINPROGRESS);
9435 	else
9436 		return (EINTR);
9437 }
9438 
9439 /*
9440  * Restart the removeif ioctl. The refcnt has gone down to 0.
9441  * The ipif is already condemned. So can't find it thru lookups.
9442  */
9443 /* ARGSUSED */
9444 int
9445 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9446     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9447 {
9448 	ill_t *ill = ipif->ipif_ill;
9449 
9450 	ASSERT(IAM_WRITER_IPIF(ipif));
9451 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9452 
9453 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9454 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9455 
9456 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9457 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9458 		ill_delete_tail(ill);
9459 		mi_free(ill);
9460 		return (0);
9461 	}
9462 
9463 	ipif_non_duplicate(ipif);
9464 	(void) ipif_down_tail(ipif);
9465 	ipif_free_tail(ipif);
9466 
9467 	return (0);
9468 }
9469 
9470 /*
9471  * Set the local interface address.
9472  * Allow an address of all zero when the interface is down.
9473  */
9474 /* ARGSUSED */
9475 int
9476 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9477     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9478 {
9479 	int err = 0;
9480 	in6_addr_t v6addr;
9481 	boolean_t need_up = B_FALSE;
9482 
9483 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9484 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9485 
9486 	ASSERT(IAM_WRITER_IPIF(ipif));
9487 
9488 	if (ipif->ipif_isv6) {
9489 		sin6_t *sin6;
9490 		ill_t *ill;
9491 		phyint_t *phyi;
9492 
9493 		if (sin->sin_family != AF_INET6)
9494 			return (EAFNOSUPPORT);
9495 
9496 		sin6 = (sin6_t *)sin;
9497 		v6addr = sin6->sin6_addr;
9498 		ill = ipif->ipif_ill;
9499 		phyi = ill->ill_phyint;
9500 
9501 		/*
9502 		 * Enforce that true multicast interfaces have a link-local
9503 		 * address for logical unit 0.
9504 		 */
9505 		if (ipif->ipif_id == 0 &&
9506 		    (ill->ill_flags & ILLF_MULTICAST) &&
9507 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9508 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9509 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9510 			return (EADDRNOTAVAIL);
9511 		}
9512 
9513 		/*
9514 		 * up interfaces shouldn't have the unspecified address
9515 		 * unless they also have the IPIF_NOLOCAL flags set and
9516 		 * have a subnet assigned.
9517 		 */
9518 		if ((ipif->ipif_flags & IPIF_UP) &&
9519 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9520 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9521 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9522 			return (EADDRNOTAVAIL);
9523 		}
9524 
9525 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9526 			return (EADDRNOTAVAIL);
9527 	} else {
9528 		ipaddr_t addr;
9529 
9530 		if (sin->sin_family != AF_INET)
9531 			return (EAFNOSUPPORT);
9532 
9533 		addr = sin->sin_addr.s_addr;
9534 
9535 		/* Allow 0 as the local address. */
9536 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9537 			return (EADDRNOTAVAIL);
9538 
9539 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9540 	}
9541 
9542 	/*
9543 	 * Even if there is no change we redo things just to rerun
9544 	 * ipif_set_default.
9545 	 */
9546 	if (ipif->ipif_flags & IPIF_UP) {
9547 		/*
9548 		 * Setting a new local address, make sure
9549 		 * we have net and subnet bcast ire's for
9550 		 * the old address if we need them.
9551 		 */
9552 		/*
9553 		 * If the interface is already marked up,
9554 		 * we call ipif_down which will take care
9555 		 * of ditching any IREs that have been set
9556 		 * up based on the old interface address.
9557 		 */
9558 		err = ipif_logical_down(ipif, q, mp);
9559 		if (err == EINPROGRESS)
9560 			return (err);
9561 		(void) ipif_down_tail(ipif);
9562 		need_up = 1;
9563 	}
9564 
9565 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9566 	return (err);
9567 }
9568 
9569 int
9570 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9571     boolean_t need_up)
9572 {
9573 	in6_addr_t v6addr;
9574 	in6_addr_t ov6addr;
9575 	ipaddr_t addr;
9576 	sin6_t	*sin6;
9577 	int	sinlen;
9578 	int	err = 0;
9579 	ill_t	*ill = ipif->ipif_ill;
9580 	boolean_t need_dl_down;
9581 	boolean_t need_arp_down;
9582 	struct iocblk *iocp;
9583 
9584 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9585 
9586 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9587 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9588 	ASSERT(IAM_WRITER_IPIF(ipif));
9589 
9590 	/* Must cancel any pending timer before taking the ill_lock */
9591 	if (ipif->ipif_recovery_id != 0)
9592 		(void) untimeout(ipif->ipif_recovery_id);
9593 	ipif->ipif_recovery_id = 0;
9594 
9595 	if (ipif->ipif_isv6) {
9596 		sin6 = (sin6_t *)sin;
9597 		v6addr = sin6->sin6_addr;
9598 		sinlen = sizeof (struct sockaddr_in6);
9599 	} else {
9600 		addr = sin->sin_addr.s_addr;
9601 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9602 		sinlen = sizeof (struct sockaddr_in);
9603 	}
9604 	mutex_enter(&ill->ill_lock);
9605 	ov6addr = ipif->ipif_v6lcl_addr;
9606 	ipif->ipif_v6lcl_addr = v6addr;
9607 	sctp_update_ipif_addr(ipif, ov6addr);
9608 	ipif->ipif_addr_ready = 0;
9609 
9610 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9611 
9612 	/*
9613 	 * If the interface was previously marked as a duplicate, then since
9614 	 * we've now got a "new" address, it should no longer be considered a
9615 	 * duplicate -- even if the "new" address is the same as the old one.
9616 	 * Note that if all ipifs are down, we may have a pending ARP down
9617 	 * event to handle.  This is because we want to recover from duplicates
9618 	 * and thus delay tearing down ARP until the duplicates have been
9619 	 * removed or disabled.
9620 	 */
9621 	need_dl_down = need_arp_down = B_FALSE;
9622 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9623 		need_arp_down = !need_up;
9624 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9625 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9626 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9627 			need_dl_down = B_TRUE;
9628 		}
9629 	}
9630 
9631 	ipif_set_default(ipif);
9632 
9633 	/*
9634 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9635 	 * tag the ill so that future updates to the interface ID don't result
9636 	 * in this address getting automatically reconfigured from under the
9637 	 * administrator.
9638 	 */
9639 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9640 		ill->ill_manual_linklocal = 1;
9641 
9642 	/*
9643 	 * When publishing an interface address change event, we only notify
9644 	 * the event listeners of the new address.  It is assumed that if they
9645 	 * actively care about the addresses assigned that they will have
9646 	 * already discovered the previous address assigned (if there was one.)
9647 	 *
9648 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9649 	 */
9650 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9651 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9652 		    NE_ADDRESS_CHANGE, sin, sinlen);
9653 	}
9654 
9655 	mutex_exit(&ill->ill_lock);
9656 
9657 	if (need_up) {
9658 		/*
9659 		 * Now bring the interface back up.  If this
9660 		 * is the only IPIF for the ILL, ipif_up
9661 		 * will have to re-bind to the device, so
9662 		 * we may get back EINPROGRESS, in which
9663 		 * case, this IOCTL will get completed in
9664 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9665 		 */
9666 		err = ipif_up(ipif, q, mp);
9667 	} else {
9668 		/* Perhaps ilgs should use this ill */
9669 		update_conn_ill(NULL, ill->ill_ipst);
9670 	}
9671 
9672 	if (need_dl_down)
9673 		ill_dl_down(ill);
9674 
9675 	if (need_arp_down && !ill->ill_isv6)
9676 		(void) ipif_arp_down(ipif);
9677 
9678 	/*
9679 	 * The default multicast interface might have changed (for
9680 	 * instance if the IPv6 scope of the address changed)
9681 	 */
9682 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9683 
9684 	return (err);
9685 }
9686 
9687 /*
9688  * Restart entry point to restart the address set operation after the
9689  * refcounts have dropped to zero.
9690  */
9691 /* ARGSUSED */
9692 int
9693 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9694     ip_ioctl_cmd_t *ipip, void *ifreq)
9695 {
9696 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9697 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9698 	ASSERT(IAM_WRITER_IPIF(ipif));
9699 	(void) ipif_down_tail(ipif);
9700 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9701 }
9702 
9703 /* ARGSUSED */
9704 int
9705 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9706     ip_ioctl_cmd_t *ipip, void *if_req)
9707 {
9708 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9709 	struct lifreq *lifr = (struct lifreq *)if_req;
9710 
9711 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9712 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9713 	/*
9714 	 * The net mask and address can't change since we have a
9715 	 * reference to the ipif. So no lock is necessary.
9716 	 */
9717 	if (ipif->ipif_isv6) {
9718 		*sin6 = sin6_null;
9719 		sin6->sin6_family = AF_INET6;
9720 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9721 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9722 		lifr->lifr_addrlen =
9723 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9724 	} else {
9725 		*sin = sin_null;
9726 		sin->sin_family = AF_INET;
9727 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9728 		if (ipip->ipi_cmd_type == LIF_CMD) {
9729 			lifr->lifr_addrlen =
9730 			    ip_mask_to_plen(ipif->ipif_net_mask);
9731 		}
9732 	}
9733 	return (0);
9734 }
9735 
9736 /*
9737  * Set the destination address for a pt-pt interface.
9738  */
9739 /* ARGSUSED */
9740 int
9741 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9742     ip_ioctl_cmd_t *ipip, void *if_req)
9743 {
9744 	int err = 0;
9745 	in6_addr_t v6addr;
9746 	boolean_t need_up = B_FALSE;
9747 
9748 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9749 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9750 	ASSERT(IAM_WRITER_IPIF(ipif));
9751 
9752 	if (ipif->ipif_isv6) {
9753 		sin6_t *sin6;
9754 
9755 		if (sin->sin_family != AF_INET6)
9756 			return (EAFNOSUPPORT);
9757 
9758 		sin6 = (sin6_t *)sin;
9759 		v6addr = sin6->sin6_addr;
9760 
9761 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9762 			return (EADDRNOTAVAIL);
9763 	} else {
9764 		ipaddr_t addr;
9765 
9766 		if (sin->sin_family != AF_INET)
9767 			return (EAFNOSUPPORT);
9768 
9769 		addr = sin->sin_addr.s_addr;
9770 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9771 			return (EADDRNOTAVAIL);
9772 
9773 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9774 	}
9775 
9776 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9777 		return (0);	/* No change */
9778 
9779 	if (ipif->ipif_flags & IPIF_UP) {
9780 		/*
9781 		 * If the interface is already marked up,
9782 		 * we call ipif_down which will take care
9783 		 * of ditching any IREs that have been set
9784 		 * up based on the old pp dst address.
9785 		 */
9786 		err = ipif_logical_down(ipif, q, mp);
9787 		if (err == EINPROGRESS)
9788 			return (err);
9789 		(void) ipif_down_tail(ipif);
9790 		need_up = B_TRUE;
9791 	}
9792 	/*
9793 	 * could return EINPROGRESS. If so ioctl will complete in
9794 	 * ip_rput_dlpi_writer
9795 	 */
9796 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9797 	return (err);
9798 }
9799 
9800 static int
9801 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9802     boolean_t need_up)
9803 {
9804 	in6_addr_t v6addr;
9805 	ill_t	*ill = ipif->ipif_ill;
9806 	int	err = 0;
9807 	boolean_t need_dl_down;
9808 	boolean_t need_arp_down;
9809 
9810 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9811 	    ipif->ipif_id, (void *)ipif));
9812 
9813 	/* Must cancel any pending timer before taking the ill_lock */
9814 	if (ipif->ipif_recovery_id != 0)
9815 		(void) untimeout(ipif->ipif_recovery_id);
9816 	ipif->ipif_recovery_id = 0;
9817 
9818 	if (ipif->ipif_isv6) {
9819 		sin6_t *sin6;
9820 
9821 		sin6 = (sin6_t *)sin;
9822 		v6addr = sin6->sin6_addr;
9823 	} else {
9824 		ipaddr_t addr;
9825 
9826 		addr = sin->sin_addr.s_addr;
9827 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9828 	}
9829 	mutex_enter(&ill->ill_lock);
9830 	/* Set point to point destination address. */
9831 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9832 		/*
9833 		 * Allow this as a means of creating logical
9834 		 * pt-pt interfaces on top of e.g. an Ethernet.
9835 		 * XXX Undocumented HACK for testing.
9836 		 * pt-pt interfaces are created with NUD disabled.
9837 		 */
9838 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9839 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9840 		if (ipif->ipif_isv6)
9841 			ill->ill_flags |= ILLF_NONUD;
9842 	}
9843 
9844 	/*
9845 	 * If the interface was previously marked as a duplicate, then since
9846 	 * we've now got a "new" address, it should no longer be considered a
9847 	 * duplicate -- even if the "new" address is the same as the old one.
9848 	 * Note that if all ipifs are down, we may have a pending ARP down
9849 	 * event to handle.
9850 	 */
9851 	need_dl_down = need_arp_down = B_FALSE;
9852 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9853 		need_arp_down = !need_up;
9854 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9855 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9856 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9857 			need_dl_down = B_TRUE;
9858 		}
9859 	}
9860 
9861 	/*
9862 	 * If we've just manually set the IPv6 destination link-local address
9863 	 * (0th ipif), tag the ill so that future updates to the destination
9864 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9865 	 * result in this address getting automatically reconfigured from
9866 	 * under the administrator.
9867 	 */
9868 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9869 		ill->ill_manual_dst_linklocal = 1;
9870 
9871 	/* Set the new address. */
9872 	ipif->ipif_v6pp_dst_addr = v6addr;
9873 	/* Make sure subnet tracks pp_dst */
9874 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9875 	mutex_exit(&ill->ill_lock);
9876 
9877 	if (need_up) {
9878 		/*
9879 		 * Now bring the interface back up.  If this
9880 		 * is the only IPIF for the ILL, ipif_up
9881 		 * will have to re-bind to the device, so
9882 		 * we may get back EINPROGRESS, in which
9883 		 * case, this IOCTL will get completed in
9884 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9885 		 */
9886 		err = ipif_up(ipif, q, mp);
9887 	}
9888 
9889 	if (need_dl_down)
9890 		ill_dl_down(ill);
9891 	if (need_arp_down && !ipif->ipif_isv6)
9892 		(void) ipif_arp_down(ipif);
9893 
9894 	return (err);
9895 }
9896 
9897 /*
9898  * Restart entry point to restart the dstaddress set operation after the
9899  * refcounts have dropped to zero.
9900  */
9901 /* ARGSUSED */
9902 int
9903 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9904     ip_ioctl_cmd_t *ipip, void *ifreq)
9905 {
9906 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9907 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9908 	(void) ipif_down_tail(ipif);
9909 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9910 }
9911 
9912 /* ARGSUSED */
9913 int
9914 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9915     ip_ioctl_cmd_t *ipip, void *if_req)
9916 {
9917 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9918 
9919 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9920 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9921 	/*
9922 	 * Get point to point destination address. The addresses can't
9923 	 * change since we hold a reference to the ipif.
9924 	 */
9925 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9926 		return (EADDRNOTAVAIL);
9927 
9928 	if (ipif->ipif_isv6) {
9929 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9930 		*sin6 = sin6_null;
9931 		sin6->sin6_family = AF_INET6;
9932 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9933 	} else {
9934 		*sin = sin_null;
9935 		sin->sin_family = AF_INET;
9936 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9937 	}
9938 	return (0);
9939 }
9940 
9941 /*
9942  * Check which flags will change by the given flags being set
9943  * silently ignore flags which userland is not allowed to control.
9944  * (Because these flags may change between SIOCGLIFFLAGS and
9945  * SIOCSLIFFLAGS, and that's outside of userland's control,
9946  * we need to silently ignore them rather than fail.)
9947  */
9948 static void
9949 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9950     uint64_t *offp)
9951 {
9952 	ill_t		*ill = ipif->ipif_ill;
9953 	phyint_t 	*phyi = ill->ill_phyint;
9954 	uint64_t	cantchange_flags, intf_flags;
9955 	uint64_t	turn_on, turn_off;
9956 
9957 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9958 	cantchange_flags = IFF_CANTCHANGE;
9959 	if (IS_IPMP(ill))
9960 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9961 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9962 	turn_off = intf_flags & turn_on;
9963 	turn_on ^= turn_off;
9964 	*onp = turn_on;
9965 	*offp = turn_off;
9966 }
9967 
9968 /*
9969  * Set interface flags.  Many flags require special handling (e.g.,
9970  * bringing the interface down); see below for details.
9971  *
9972  * NOTE : We really don't enforce that ipif_id zero should be used
9973  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9974  *	  is because applications generally does SICGLIFFLAGS and
9975  *	  ORs in the new flags (that affects the logical) and does a
9976  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9977  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9978  *	  flags that will be turned on is correct with respect to
9979  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9980  */
9981 /* ARGSUSED */
9982 int
9983 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9984     ip_ioctl_cmd_t *ipip, void *if_req)
9985 {
9986 	uint64_t turn_on;
9987 	uint64_t turn_off;
9988 	int	err = 0;
9989 	phyint_t *phyi;
9990 	ill_t *ill;
9991 	conn_t *connp;
9992 	uint64_t intf_flags;
9993 	boolean_t phyint_flags_modified = B_FALSE;
9994 	uint64_t flags;
9995 	struct ifreq *ifr;
9996 	struct lifreq *lifr;
9997 	boolean_t set_linklocal = B_FALSE;
9998 
9999 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10000 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10001 
10002 	ASSERT(IAM_WRITER_IPIF(ipif));
10003 
10004 	ill = ipif->ipif_ill;
10005 	phyi = ill->ill_phyint;
10006 
10007 	if (ipip->ipi_cmd_type == IF_CMD) {
10008 		ifr = (struct ifreq *)if_req;
10009 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10010 	} else {
10011 		lifr = (struct lifreq *)if_req;
10012 		flags = lifr->lifr_flags;
10013 	}
10014 
10015 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10016 
10017 	/*
10018 	 * Have the flags been set correctly until now?
10019 	 */
10020 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10021 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10022 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10023 	/*
10024 	 * Compare the new flags to the old, and partition
10025 	 * into those coming on and those going off.
10026 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10027 	 */
10028 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10029 		flags |= intf_flags & ~0xFFFF;
10030 
10031 	/*
10032 	 * Explicitly fail attempts to change flags that are always invalid on
10033 	 * an IPMP meta-interface.
10034 	 */
10035 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10036 		return (EINVAL);
10037 
10038 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10039 	if ((turn_on|turn_off) == 0)
10040 		return (0);	/* No change */
10041 
10042 	/*
10043 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10044 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10045 	 * allow it to be turned off.
10046 	 */
10047 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10048 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10049 		return (EINVAL);
10050 
10051 	if ((connp = Q_TO_CONN(q)) == NULL)
10052 		return (EINVAL);
10053 
10054 	/*
10055 	 * Only vrrp control socket is allowed to change IFF_UP and
10056 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10057 	 */
10058 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10059 		if (!connp->conn_isvrrp)
10060 			return (EINVAL);
10061 	}
10062 
10063 	/*
10064 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10065 	 * VRRP control socket.
10066 	 */
10067 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10068 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10069 			return (EINVAL);
10070 	}
10071 
10072 	if (turn_on & IFF_NOFAILOVER) {
10073 		turn_on |= IFF_DEPRECATED;
10074 		flags |= IFF_DEPRECATED;
10075 	}
10076 
10077 	/*
10078 	 * On underlying interfaces, only allow applications to manage test
10079 	 * addresses -- otherwise, they may get confused when the address
10080 	 * moves as part of being brought up.  Likewise, prevent an
10081 	 * application-managed test address from being converted to a data
10082 	 * address.  To prevent migration of administratively up addresses in
10083 	 * the kernel, we don't allow them to be converted either.
10084 	 */
10085 	if (IS_UNDER_IPMP(ill)) {
10086 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10087 
10088 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10089 			return (EINVAL);
10090 
10091 		if ((turn_off & IFF_NOFAILOVER) &&
10092 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10093 			return (EINVAL);
10094 	}
10095 
10096 	/*
10097 	 * Only allow IFF_TEMPORARY flag to be set on
10098 	 * IPv6 interfaces.
10099 	 */
10100 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10101 		return (EINVAL);
10102 
10103 	/*
10104 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10105 	 */
10106 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10107 		return (EINVAL);
10108 
10109 	/*
10110 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10111 	 * interfaces.  It makes no sense in that context.
10112 	 */
10113 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10114 		return (EINVAL);
10115 
10116 	/*
10117 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10118 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10119 	 * If the link local address isn't set, and can be set, it will get
10120 	 * set later on in this function.
10121 	 */
10122 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10123 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10124 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10125 		if (ipif_cant_setlinklocal(ipif))
10126 			return (EINVAL);
10127 		set_linklocal = B_TRUE;
10128 	}
10129 
10130 	/*
10131 	 * If we modify physical interface flags, we'll potentially need to
10132 	 * send up two routing socket messages for the changes (one for the
10133 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10134 	 */
10135 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10136 		phyint_flags_modified = B_TRUE;
10137 
10138 	/*
10139 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10140 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10141 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10142 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10143 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10144 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10145 	 * will not be honored.
10146 	 */
10147 	if (turn_on & PHYI_STANDBY) {
10148 		/*
10149 		 * No need to grab ill_g_usesrc_lock here; see the
10150 		 * synchronization notes in ip.c.
10151 		 */
10152 		if (ill->ill_usesrc_grp_next != NULL ||
10153 		    intf_flags & PHYI_INACTIVE)
10154 			return (EINVAL);
10155 		if (!(flags & PHYI_FAILED)) {
10156 			flags |= PHYI_INACTIVE;
10157 			turn_on |= PHYI_INACTIVE;
10158 		}
10159 	}
10160 
10161 	if (turn_off & PHYI_STANDBY) {
10162 		flags &= ~PHYI_INACTIVE;
10163 		turn_off |= PHYI_INACTIVE;
10164 	}
10165 
10166 	/*
10167 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10168 	 * would end up on.
10169 	 */
10170 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10171 	    (PHYI_FAILED | PHYI_INACTIVE))
10172 		return (EINVAL);
10173 
10174 	/*
10175 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10176 	 * status of the interface.
10177 	 */
10178 	if ((turn_on | turn_off) & ILLF_ROUTER)
10179 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10180 
10181 	/*
10182 	 * If the interface is not UP and we are not going to
10183 	 * bring it UP, record the flags and return. When the
10184 	 * interface comes UP later, the right actions will be
10185 	 * taken.
10186 	 */
10187 	if (!(ipif->ipif_flags & IPIF_UP) &&
10188 	    !(turn_on & IPIF_UP)) {
10189 		/* Record new flags in their respective places. */
10190 		mutex_enter(&ill->ill_lock);
10191 		mutex_enter(&ill->ill_phyint->phyint_lock);
10192 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10193 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10194 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10195 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10196 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10197 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10198 		mutex_exit(&ill->ill_lock);
10199 		mutex_exit(&ill->ill_phyint->phyint_lock);
10200 
10201 		/*
10202 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10203 		 * same to the kernel: if any of them has been set by
10204 		 * userland, the interface cannot be used for data traffic.
10205 		 */
10206 		if ((turn_on|turn_off) &
10207 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10208 			ASSERT(!IS_IPMP(ill));
10209 			/*
10210 			 * It's possible the ill is part of an "anonymous"
10211 			 * IPMP group rather than a real group.  In that case,
10212 			 * there are no other interfaces in the group and thus
10213 			 * no need to call ipmp_phyint_refresh_active().
10214 			 */
10215 			if (IS_UNDER_IPMP(ill))
10216 				ipmp_phyint_refresh_active(phyi);
10217 		}
10218 
10219 		if (phyint_flags_modified) {
10220 			if (phyi->phyint_illv4 != NULL) {
10221 				ip_rts_ifmsg(phyi->phyint_illv4->
10222 				    ill_ipif, RTSQ_DEFAULT);
10223 			}
10224 			if (phyi->phyint_illv6 != NULL) {
10225 				ip_rts_ifmsg(phyi->phyint_illv6->
10226 				    ill_ipif, RTSQ_DEFAULT);
10227 			}
10228 		}
10229 		/* The default multicast interface might have changed */
10230 		ire_increment_multicast_generation(ill->ill_ipst,
10231 		    ill->ill_isv6);
10232 
10233 		return (0);
10234 	} else if (set_linklocal) {
10235 		mutex_enter(&ill->ill_lock);
10236 		if (set_linklocal)
10237 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10238 		mutex_exit(&ill->ill_lock);
10239 	}
10240 
10241 	/*
10242 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10243 	 * or point-to-point interfaces with an unspecified destination. We do
10244 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10245 	 * have a subnet assigned, which is how in.ndpd currently manages its
10246 	 * onlink prefix list when no addresses are configured with those
10247 	 * prefixes.
10248 	 */
10249 	if (ipif->ipif_isv6 &&
10250 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10251 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10252 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10253 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10254 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10255 		return (EINVAL);
10256 	}
10257 
10258 	/*
10259 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10260 	 * from being brought up.
10261 	 */
10262 	if (!ipif->ipif_isv6 &&
10263 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10264 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10265 		return (EINVAL);
10266 	}
10267 
10268 	/*
10269 	 * If we are going to change one or more of the flags that are
10270 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10271 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10272 	 * IPIF_NOFAILOVER, we will take special action.  This is
10273 	 * done by bring the ipif down, changing the flags and bringing
10274 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10275 	 * back up will trigger the address to be moved.
10276 	 *
10277 	 * If we are going to change IFF_NOACCEPT, we need to bring
10278 	 * all the ipifs down then bring them up again.	 The act of
10279 	 * bringing all the ipifs back up will trigger the local
10280 	 * ires being recreated with "no_accept" set/cleared.
10281 	 *
10282 	 * Note that ILLF_NOACCEPT is always set separately from the
10283 	 * other flags.
10284 	 */
10285 	if ((turn_on|turn_off) &
10286 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10287 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10288 	    IPIF_NOFAILOVER)) {
10289 		/*
10290 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10291 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10292 		 * entries shared between multiple ipifs on the same subnet.
10293 		 */
10294 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10295 		    !(turn_off & IPIF_UP)) {
10296 			if (ipif->ipif_flags & IPIF_UP)
10297 				ill->ill_logical_down = 1;
10298 			turn_on &= ~IPIF_UP;
10299 		}
10300 		err = ipif_down(ipif, q, mp);
10301 		ip1dbg(("ipif_down returns %d err ", err));
10302 		if (err == EINPROGRESS)
10303 			return (err);
10304 		(void) ipif_down_tail(ipif);
10305 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10306 		/*
10307 		 * If we can quiesce the ill, then continue.  If not, then
10308 		 * ip_sioctl_flags_tail() will be called from
10309 		 * ipif_ill_refrele_tail().
10310 		 */
10311 		ill_down_ipifs(ill, B_TRUE);
10312 
10313 		mutex_enter(&connp->conn_lock);
10314 		mutex_enter(&ill->ill_lock);
10315 		if (!ill_is_quiescent(ill)) {
10316 			boolean_t success;
10317 
10318 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10319 			    q, mp, ILL_DOWN);
10320 			mutex_exit(&ill->ill_lock);
10321 			mutex_exit(&connp->conn_lock);
10322 			return (success ? EINPROGRESS : EINTR);
10323 		}
10324 		mutex_exit(&ill->ill_lock);
10325 		mutex_exit(&connp->conn_lock);
10326 	}
10327 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10328 }
10329 
10330 static int
10331 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10332 {
10333 	ill_t	*ill;
10334 	phyint_t *phyi;
10335 	uint64_t turn_on, turn_off;
10336 	boolean_t phyint_flags_modified = B_FALSE;
10337 	int	err = 0;
10338 	boolean_t set_linklocal = B_FALSE;
10339 
10340 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10341 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10342 
10343 	ASSERT(IAM_WRITER_IPIF(ipif));
10344 
10345 	ill = ipif->ipif_ill;
10346 	phyi = ill->ill_phyint;
10347 
10348 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10349 
10350 	/*
10351 	 * IFF_UP is handled separately.
10352 	 */
10353 	turn_on &= ~IFF_UP;
10354 	turn_off &= ~IFF_UP;
10355 
10356 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10357 		phyint_flags_modified = B_TRUE;
10358 
10359 	/*
10360 	 * Now we change the flags. Track current value of
10361 	 * other flags in their respective places.
10362 	 */
10363 	mutex_enter(&ill->ill_lock);
10364 	mutex_enter(&phyi->phyint_lock);
10365 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10366 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10367 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10368 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10369 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10370 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10371 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10372 		set_linklocal = B_TRUE;
10373 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10374 	}
10375 
10376 	mutex_exit(&ill->ill_lock);
10377 	mutex_exit(&phyi->phyint_lock);
10378 
10379 	if (set_linklocal)
10380 		(void) ipif_setlinklocal(ipif);
10381 
10382 	/*
10383 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10384 	 * the kernel: if any of them has been set by userland, the interface
10385 	 * cannot be used for data traffic.
10386 	 */
10387 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10388 		ASSERT(!IS_IPMP(ill));
10389 		/*
10390 		 * It's possible the ill is part of an "anonymous" IPMP group
10391 		 * rather than a real group.  In that case, there are no other
10392 		 * interfaces in the group and thus no need for us to call
10393 		 * ipmp_phyint_refresh_active().
10394 		 */
10395 		if (IS_UNDER_IPMP(ill))
10396 			ipmp_phyint_refresh_active(phyi);
10397 	}
10398 
10399 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10400 		/*
10401 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10402 		 * ipifs that were brought down.
10403 		 *
10404 		 * The routing sockets messages are sent as the result
10405 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10406 		 * as well.
10407 		 */
10408 		err = ill_up_ipifs(ill, q, mp);
10409 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10410 		/*
10411 		 * XXX ipif_up really does not know whether a phyint flags
10412 		 * was modified or not. So, it sends up information on
10413 		 * only one routing sockets message. As we don't bring up
10414 		 * the interface and also set PHYI_ flags simultaneously
10415 		 * it should be okay.
10416 		 */
10417 		err = ipif_up(ipif, q, mp);
10418 	} else {
10419 		/*
10420 		 * Make sure routing socket sees all changes to the flags.
10421 		 * ipif_up_done* handles this when we use ipif_up.
10422 		 */
10423 		if (phyint_flags_modified) {
10424 			if (phyi->phyint_illv4 != NULL) {
10425 				ip_rts_ifmsg(phyi->phyint_illv4->
10426 				    ill_ipif, RTSQ_DEFAULT);
10427 			}
10428 			if (phyi->phyint_illv6 != NULL) {
10429 				ip_rts_ifmsg(phyi->phyint_illv6->
10430 				    ill_ipif, RTSQ_DEFAULT);
10431 			}
10432 		} else {
10433 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10434 		}
10435 		/*
10436 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10437 		 * this in need_up case.
10438 		 */
10439 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10440 	}
10441 
10442 	/* The default multicast interface might have changed */
10443 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10444 	return (err);
10445 }
10446 
10447 /*
10448  * Restart the flags operation now that the refcounts have dropped to zero.
10449  */
10450 /* ARGSUSED */
10451 int
10452 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10453     ip_ioctl_cmd_t *ipip, void *if_req)
10454 {
10455 	uint64_t flags;
10456 	struct ifreq *ifr = if_req;
10457 	struct lifreq *lifr = if_req;
10458 	uint64_t turn_on, turn_off;
10459 
10460 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10461 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10462 
10463 	if (ipip->ipi_cmd_type == IF_CMD) {
10464 		/* cast to uint16_t prevents unwanted sign extension */
10465 		flags = (uint16_t)ifr->ifr_flags;
10466 	} else {
10467 		flags = lifr->lifr_flags;
10468 	}
10469 
10470 	/*
10471 	 * If this function call is a result of the ILLF_NOACCEPT flag
10472 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10473 	 */
10474 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10475 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10476 		(void) ipif_down_tail(ipif);
10477 
10478 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10479 }
10480 
10481 /*
10482  * Can operate on either a module or a driver queue.
10483  */
10484 /* ARGSUSED */
10485 int
10486 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10487     ip_ioctl_cmd_t *ipip, void *if_req)
10488 {
10489 	/*
10490 	 * Has the flags been set correctly till now ?
10491 	 */
10492 	ill_t *ill = ipif->ipif_ill;
10493 	phyint_t *phyi = ill->ill_phyint;
10494 
10495 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10496 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10497 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10498 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10499 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10500 
10501 	/*
10502 	 * Need a lock since some flags can be set even when there are
10503 	 * references to the ipif.
10504 	 */
10505 	mutex_enter(&ill->ill_lock);
10506 	if (ipip->ipi_cmd_type == IF_CMD) {
10507 		struct ifreq *ifr = (struct ifreq *)if_req;
10508 
10509 		/* Get interface flags (low 16 only). */
10510 		ifr->ifr_flags = ((ipif->ipif_flags |
10511 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10512 	} else {
10513 		struct lifreq *lifr = (struct lifreq *)if_req;
10514 
10515 		/* Get interface flags. */
10516 		lifr->lifr_flags = ipif->ipif_flags |
10517 		    ill->ill_flags | phyi->phyint_flags;
10518 	}
10519 	mutex_exit(&ill->ill_lock);
10520 	return (0);
10521 }
10522 
10523 /*
10524  * We allow the MTU to be set on an ILL, but not have it be different
10525  * for different IPIFs since we don't actually send packets on IPIFs.
10526  */
10527 /* ARGSUSED */
10528 int
10529 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10530     ip_ioctl_cmd_t *ipip, void *if_req)
10531 {
10532 	int mtu;
10533 	int ip_min_mtu;
10534 	struct ifreq	*ifr;
10535 	struct lifreq *lifr;
10536 	ill_t	*ill;
10537 
10538 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10539 	    ipif->ipif_id, (void *)ipif));
10540 	if (ipip->ipi_cmd_type == IF_CMD) {
10541 		ifr = (struct ifreq *)if_req;
10542 		mtu = ifr->ifr_metric;
10543 	} else {
10544 		lifr = (struct lifreq *)if_req;
10545 		mtu = lifr->lifr_mtu;
10546 	}
10547 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10548 	if (ipif->ipif_id != 0)
10549 		return (EINVAL);
10550 
10551 	ill = ipif->ipif_ill;
10552 	if (ipif->ipif_isv6)
10553 		ip_min_mtu = IPV6_MIN_MTU;
10554 	else
10555 		ip_min_mtu = IP_MIN_MTU;
10556 
10557 	mutex_enter(&ill->ill_lock);
10558 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10559 		mutex_exit(&ill->ill_lock);
10560 		return (EINVAL);
10561 	}
10562 	/*
10563 	 * The dce and fragmentation code can handle changes to ill_mtu
10564 	 * concurrent with sending/fragmenting packets.
10565 	 */
10566 	ill->ill_mtu = mtu;
10567 	ill->ill_flags |= ILLF_FIXEDMTU;
10568 	mutex_exit(&ill->ill_lock);
10569 
10570 	/*
10571 	 * Make sure all dce_generation checks find out
10572 	 * that ill_mtu has changed.
10573 	 */
10574 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10575 
10576 	/* Update the MTU in SCTP's list */
10577 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10578 	return (0);
10579 }
10580 
10581 /* Get interface MTU. */
10582 /* ARGSUSED */
10583 int
10584 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10585 	ip_ioctl_cmd_t *ipip, void *if_req)
10586 {
10587 	struct ifreq	*ifr;
10588 	struct lifreq	*lifr;
10589 
10590 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10591 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10592 
10593 	/*
10594 	 * We allow a get on any logical interface even though the set
10595 	 * can only be done on logical unit 0.
10596 	 */
10597 	if (ipip->ipi_cmd_type == IF_CMD) {
10598 		ifr = (struct ifreq *)if_req;
10599 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10600 	} else {
10601 		lifr = (struct lifreq *)if_req;
10602 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10603 	}
10604 	return (0);
10605 }
10606 
10607 /* Set interface broadcast address. */
10608 /* ARGSUSED2 */
10609 int
10610 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10611 	ip_ioctl_cmd_t *ipip, void *if_req)
10612 {
10613 	ipaddr_t addr;
10614 	ire_t	*ire;
10615 	ill_t		*ill = ipif->ipif_ill;
10616 	ip_stack_t	*ipst = ill->ill_ipst;
10617 
10618 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10619 	    ipif->ipif_id));
10620 
10621 	ASSERT(IAM_WRITER_IPIF(ipif));
10622 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10623 		return (EADDRNOTAVAIL);
10624 
10625 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10626 
10627 	if (sin->sin_family != AF_INET)
10628 		return (EAFNOSUPPORT);
10629 
10630 	addr = sin->sin_addr.s_addr;
10631 	if (ipif->ipif_flags & IPIF_UP) {
10632 		/*
10633 		 * If we are already up, make sure the new
10634 		 * broadcast address makes sense.  If it does,
10635 		 * there should be an IRE for it already.
10636 		 */
10637 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10638 		    ill, ipif->ipif_zoneid, NULL,
10639 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10640 		if (ire == NULL) {
10641 			return (EINVAL);
10642 		} else {
10643 			ire_refrele(ire);
10644 		}
10645 	}
10646 	/*
10647 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10648 	 * needs to already exist we never need to change the set of
10649 	 * IRE_BROADCASTs when we are UP.
10650 	 */
10651 	if (addr != ipif->ipif_brd_addr)
10652 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10653 
10654 	return (0);
10655 }
10656 
10657 /* Get interface broadcast address. */
10658 /* ARGSUSED */
10659 int
10660 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10661     ip_ioctl_cmd_t *ipip, void *if_req)
10662 {
10663 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10664 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10665 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10666 		return (EADDRNOTAVAIL);
10667 
10668 	/* IPIF_BROADCAST not possible with IPv6 */
10669 	ASSERT(!ipif->ipif_isv6);
10670 	*sin = sin_null;
10671 	sin->sin_family = AF_INET;
10672 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10673 	return (0);
10674 }
10675 
10676 /*
10677  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10678  */
10679 /* ARGSUSED */
10680 int
10681 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10682     ip_ioctl_cmd_t *ipip, void *if_req)
10683 {
10684 	int err = 0;
10685 	in6_addr_t v6mask;
10686 
10687 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10688 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10689 
10690 	ASSERT(IAM_WRITER_IPIF(ipif));
10691 
10692 	if (ipif->ipif_isv6) {
10693 		sin6_t *sin6;
10694 
10695 		if (sin->sin_family != AF_INET6)
10696 			return (EAFNOSUPPORT);
10697 
10698 		sin6 = (sin6_t *)sin;
10699 		v6mask = sin6->sin6_addr;
10700 	} else {
10701 		ipaddr_t mask;
10702 
10703 		if (sin->sin_family != AF_INET)
10704 			return (EAFNOSUPPORT);
10705 
10706 		mask = sin->sin_addr.s_addr;
10707 		V4MASK_TO_V6(mask, v6mask);
10708 	}
10709 
10710 	/*
10711 	 * No big deal if the interface isn't already up, or the mask
10712 	 * isn't really changing, or this is pt-pt.
10713 	 */
10714 	if (!(ipif->ipif_flags & IPIF_UP) ||
10715 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10716 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10717 		ipif->ipif_v6net_mask = v6mask;
10718 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10719 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10720 			    ipif->ipif_v6net_mask,
10721 			    ipif->ipif_v6subnet);
10722 		}
10723 		return (0);
10724 	}
10725 	/*
10726 	 * Make sure we have valid net and subnet broadcast ire's
10727 	 * for the old netmask, if needed by other logical interfaces.
10728 	 */
10729 	err = ipif_logical_down(ipif, q, mp);
10730 	if (err == EINPROGRESS)
10731 		return (err);
10732 	(void) ipif_down_tail(ipif);
10733 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10734 	return (err);
10735 }
10736 
10737 static int
10738 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10739 {
10740 	in6_addr_t v6mask;
10741 	int err = 0;
10742 
10743 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10744 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10745 
10746 	if (ipif->ipif_isv6) {
10747 		sin6_t *sin6;
10748 
10749 		sin6 = (sin6_t *)sin;
10750 		v6mask = sin6->sin6_addr;
10751 	} else {
10752 		ipaddr_t mask;
10753 
10754 		mask = sin->sin_addr.s_addr;
10755 		V4MASK_TO_V6(mask, v6mask);
10756 	}
10757 
10758 	ipif->ipif_v6net_mask = v6mask;
10759 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10760 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10761 		    ipif->ipif_v6subnet);
10762 	}
10763 	err = ipif_up(ipif, q, mp);
10764 
10765 	if (err == 0 || err == EINPROGRESS) {
10766 		/*
10767 		 * The interface must be DL_BOUND if this packet has to
10768 		 * go out on the wire. Since we only go through a logical
10769 		 * down and are bound with the driver during an internal
10770 		 * down/up that is satisfied.
10771 		 */
10772 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10773 			/* Potentially broadcast an address mask reply. */
10774 			ipif_mask_reply(ipif);
10775 		}
10776 	}
10777 	return (err);
10778 }
10779 
10780 /* ARGSUSED */
10781 int
10782 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10783     ip_ioctl_cmd_t *ipip, void *if_req)
10784 {
10785 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10786 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10787 	(void) ipif_down_tail(ipif);
10788 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10789 }
10790 
10791 /* Get interface net mask. */
10792 /* ARGSUSED */
10793 int
10794 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10795     ip_ioctl_cmd_t *ipip, void *if_req)
10796 {
10797 	struct lifreq *lifr = (struct lifreq *)if_req;
10798 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10799 
10800 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10801 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10802 
10803 	/*
10804 	 * net mask can't change since we have a reference to the ipif.
10805 	 */
10806 	if (ipif->ipif_isv6) {
10807 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10808 		*sin6 = sin6_null;
10809 		sin6->sin6_family = AF_INET6;
10810 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10811 		lifr->lifr_addrlen =
10812 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10813 	} else {
10814 		*sin = sin_null;
10815 		sin->sin_family = AF_INET;
10816 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10817 		if (ipip->ipi_cmd_type == LIF_CMD) {
10818 			lifr->lifr_addrlen =
10819 			    ip_mask_to_plen(ipif->ipif_net_mask);
10820 		}
10821 	}
10822 	return (0);
10823 }
10824 
10825 /* ARGSUSED */
10826 int
10827 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10828     ip_ioctl_cmd_t *ipip, void *if_req)
10829 {
10830 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10831 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10832 
10833 	/*
10834 	 * Since no applications should ever be setting metrics on underlying
10835 	 * interfaces, we explicitly fail to smoke 'em out.
10836 	 */
10837 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10838 		return (EINVAL);
10839 
10840 	/*
10841 	 * Set interface metric.  We don't use this for
10842 	 * anything but we keep track of it in case it is
10843 	 * important to routing applications or such.
10844 	 */
10845 	if (ipip->ipi_cmd_type == IF_CMD) {
10846 		struct ifreq    *ifr;
10847 
10848 		ifr = (struct ifreq *)if_req;
10849 		ipif->ipif_metric = ifr->ifr_metric;
10850 	} else {
10851 		struct lifreq   *lifr;
10852 
10853 		lifr = (struct lifreq *)if_req;
10854 		ipif->ipif_metric = lifr->lifr_metric;
10855 	}
10856 	return (0);
10857 }
10858 
10859 /* ARGSUSED */
10860 int
10861 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10862     ip_ioctl_cmd_t *ipip, void *if_req)
10863 {
10864 	/* Get interface metric. */
10865 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10866 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10867 
10868 	if (ipip->ipi_cmd_type == IF_CMD) {
10869 		struct ifreq    *ifr;
10870 
10871 		ifr = (struct ifreq *)if_req;
10872 		ifr->ifr_metric = ipif->ipif_metric;
10873 	} else {
10874 		struct lifreq   *lifr;
10875 
10876 		lifr = (struct lifreq *)if_req;
10877 		lifr->lifr_metric = ipif->ipif_metric;
10878 	}
10879 
10880 	return (0);
10881 }
10882 
10883 /* ARGSUSED */
10884 int
10885 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10886     ip_ioctl_cmd_t *ipip, void *if_req)
10887 {
10888 	int	arp_muxid;
10889 
10890 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10891 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10892 	/*
10893 	 * Set the muxid returned from I_PLINK.
10894 	 */
10895 	if (ipip->ipi_cmd_type == IF_CMD) {
10896 		struct ifreq *ifr = (struct ifreq *)if_req;
10897 
10898 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10899 		arp_muxid = ifr->ifr_arp_muxid;
10900 	} else {
10901 		struct lifreq *lifr = (struct lifreq *)if_req;
10902 
10903 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10904 		arp_muxid = lifr->lifr_arp_muxid;
10905 	}
10906 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10907 	return (0);
10908 }
10909 
10910 /* ARGSUSED */
10911 int
10912 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10913     ip_ioctl_cmd_t *ipip, void *if_req)
10914 {
10915 	int	arp_muxid = 0;
10916 
10917 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10918 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10919 	/*
10920 	 * Get the muxid saved in ill for I_PUNLINK.
10921 	 */
10922 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10923 	if (ipip->ipi_cmd_type == IF_CMD) {
10924 		struct ifreq *ifr = (struct ifreq *)if_req;
10925 
10926 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10927 		ifr->ifr_arp_muxid = arp_muxid;
10928 	} else {
10929 		struct lifreq *lifr = (struct lifreq *)if_req;
10930 
10931 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10932 		lifr->lifr_arp_muxid = arp_muxid;
10933 	}
10934 	return (0);
10935 }
10936 
10937 /*
10938  * Set the subnet prefix. Does not modify the broadcast address.
10939  */
10940 /* ARGSUSED */
10941 int
10942 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10943     ip_ioctl_cmd_t *ipip, void *if_req)
10944 {
10945 	int err = 0;
10946 	in6_addr_t v6addr;
10947 	in6_addr_t v6mask;
10948 	boolean_t need_up = B_FALSE;
10949 	int addrlen;
10950 
10951 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10952 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10953 
10954 	ASSERT(IAM_WRITER_IPIF(ipif));
10955 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10956 
10957 	if (ipif->ipif_isv6) {
10958 		sin6_t *sin6;
10959 
10960 		if (sin->sin_family != AF_INET6)
10961 			return (EAFNOSUPPORT);
10962 
10963 		sin6 = (sin6_t *)sin;
10964 		v6addr = sin6->sin6_addr;
10965 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10966 			return (EADDRNOTAVAIL);
10967 	} else {
10968 		ipaddr_t addr;
10969 
10970 		if (sin->sin_family != AF_INET)
10971 			return (EAFNOSUPPORT);
10972 
10973 		addr = sin->sin_addr.s_addr;
10974 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10975 			return (EADDRNOTAVAIL);
10976 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10977 		/* Add 96 bits */
10978 		addrlen += IPV6_ABITS - IP_ABITS;
10979 	}
10980 
10981 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10982 		return (EINVAL);
10983 
10984 	/* Check if bits in the address is set past the mask */
10985 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10986 		return (EINVAL);
10987 
10988 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10989 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10990 		return (0);	/* No change */
10991 
10992 	if (ipif->ipif_flags & IPIF_UP) {
10993 		/*
10994 		 * If the interface is already marked up,
10995 		 * we call ipif_down which will take care
10996 		 * of ditching any IREs that have been set
10997 		 * up based on the old interface address.
10998 		 */
10999 		err = ipif_logical_down(ipif, q, mp);
11000 		if (err == EINPROGRESS)
11001 			return (err);
11002 		(void) ipif_down_tail(ipif);
11003 		need_up = B_TRUE;
11004 	}
11005 
11006 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11007 	return (err);
11008 }
11009 
11010 static int
11011 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11012     queue_t *q, mblk_t *mp, boolean_t need_up)
11013 {
11014 	ill_t	*ill = ipif->ipif_ill;
11015 	int	err = 0;
11016 
11017 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11018 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11019 
11020 	/* Set the new address. */
11021 	mutex_enter(&ill->ill_lock);
11022 	ipif->ipif_v6net_mask = v6mask;
11023 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11024 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11025 		    ipif->ipif_v6subnet);
11026 	}
11027 	mutex_exit(&ill->ill_lock);
11028 
11029 	if (need_up) {
11030 		/*
11031 		 * Now bring the interface back up.  If this
11032 		 * is the only IPIF for the ILL, ipif_up
11033 		 * will have to re-bind to the device, so
11034 		 * we may get back EINPROGRESS, in which
11035 		 * case, this IOCTL will get completed in
11036 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11037 		 */
11038 		err = ipif_up(ipif, q, mp);
11039 		if (err == EINPROGRESS)
11040 			return (err);
11041 	}
11042 	return (err);
11043 }
11044 
11045 /* ARGSUSED */
11046 int
11047 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11048     ip_ioctl_cmd_t *ipip, void *if_req)
11049 {
11050 	int	addrlen;
11051 	in6_addr_t v6addr;
11052 	in6_addr_t v6mask;
11053 	struct lifreq *lifr = (struct lifreq *)if_req;
11054 
11055 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11056 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11057 	(void) ipif_down_tail(ipif);
11058 
11059 	addrlen = lifr->lifr_addrlen;
11060 	if (ipif->ipif_isv6) {
11061 		sin6_t *sin6;
11062 
11063 		sin6 = (sin6_t *)sin;
11064 		v6addr = sin6->sin6_addr;
11065 	} else {
11066 		ipaddr_t addr;
11067 
11068 		addr = sin->sin_addr.s_addr;
11069 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11070 		addrlen += IPV6_ABITS - IP_ABITS;
11071 	}
11072 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11073 
11074 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11075 }
11076 
11077 /* ARGSUSED */
11078 int
11079 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11080     ip_ioctl_cmd_t *ipip, void *if_req)
11081 {
11082 	struct lifreq *lifr = (struct lifreq *)if_req;
11083 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11084 
11085 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11086 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11087 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11088 
11089 	if (ipif->ipif_isv6) {
11090 		*sin6 = sin6_null;
11091 		sin6->sin6_family = AF_INET6;
11092 		sin6->sin6_addr = ipif->ipif_v6subnet;
11093 		lifr->lifr_addrlen =
11094 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11095 	} else {
11096 		*sin = sin_null;
11097 		sin->sin_family = AF_INET;
11098 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11099 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11100 	}
11101 	return (0);
11102 }
11103 
11104 /*
11105  * Set the IPv6 address token.
11106  */
11107 /* ARGSUSED */
11108 int
11109 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11110     ip_ioctl_cmd_t *ipi, void *if_req)
11111 {
11112 	ill_t *ill = ipif->ipif_ill;
11113 	int err;
11114 	in6_addr_t v6addr;
11115 	in6_addr_t v6mask;
11116 	boolean_t need_up = B_FALSE;
11117 	int i;
11118 	sin6_t *sin6 = (sin6_t *)sin;
11119 	struct lifreq *lifr = (struct lifreq *)if_req;
11120 	int addrlen;
11121 
11122 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11123 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11124 	ASSERT(IAM_WRITER_IPIF(ipif));
11125 
11126 	addrlen = lifr->lifr_addrlen;
11127 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11128 	if (ipif->ipif_id != 0)
11129 		return (EINVAL);
11130 
11131 	if (!ipif->ipif_isv6)
11132 		return (EINVAL);
11133 
11134 	if (addrlen > IPV6_ABITS)
11135 		return (EINVAL);
11136 
11137 	v6addr = sin6->sin6_addr;
11138 
11139 	/*
11140 	 * The length of the token is the length from the end.  To get
11141 	 * the proper mask for this, compute the mask of the bits not
11142 	 * in the token; ie. the prefix, and then xor to get the mask.
11143 	 */
11144 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11145 		return (EINVAL);
11146 	for (i = 0; i < 4; i++) {
11147 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11148 	}
11149 
11150 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11151 	    ill->ill_token_length == addrlen)
11152 		return (0);	/* No change */
11153 
11154 	if (ipif->ipif_flags & IPIF_UP) {
11155 		err = ipif_logical_down(ipif, q, mp);
11156 		if (err == EINPROGRESS)
11157 			return (err);
11158 		(void) ipif_down_tail(ipif);
11159 		need_up = B_TRUE;
11160 	}
11161 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11162 	return (err);
11163 }
11164 
11165 static int
11166 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11167     mblk_t *mp, boolean_t need_up)
11168 {
11169 	in6_addr_t v6addr;
11170 	in6_addr_t v6mask;
11171 	ill_t	*ill = ipif->ipif_ill;
11172 	int	i;
11173 	int	err = 0;
11174 
11175 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11176 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11177 	v6addr = sin6->sin6_addr;
11178 	/*
11179 	 * The length of the token is the length from the end.  To get
11180 	 * the proper mask for this, compute the mask of the bits not
11181 	 * in the token; ie. the prefix, and then xor to get the mask.
11182 	 */
11183 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11184 	for (i = 0; i < 4; i++)
11185 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11186 
11187 	mutex_enter(&ill->ill_lock);
11188 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11189 	ill->ill_token_length = addrlen;
11190 	ill->ill_manual_token = 1;
11191 
11192 	/* Reconfigure the link-local address based on this new token */
11193 	ipif_setlinklocal(ill->ill_ipif);
11194 
11195 	mutex_exit(&ill->ill_lock);
11196 
11197 	if (need_up) {
11198 		/*
11199 		 * Now bring the interface back up.  If this
11200 		 * is the only IPIF for the ILL, ipif_up
11201 		 * will have to re-bind to the device, so
11202 		 * we may get back EINPROGRESS, in which
11203 		 * case, this IOCTL will get completed in
11204 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11205 		 */
11206 		err = ipif_up(ipif, q, mp);
11207 		if (err == EINPROGRESS)
11208 			return (err);
11209 	}
11210 	return (err);
11211 }
11212 
11213 /* ARGSUSED */
11214 int
11215 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11216     ip_ioctl_cmd_t *ipi, void *if_req)
11217 {
11218 	ill_t *ill;
11219 	sin6_t *sin6 = (sin6_t *)sin;
11220 	struct lifreq *lifr = (struct lifreq *)if_req;
11221 
11222 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11223 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11224 	if (ipif->ipif_id != 0)
11225 		return (EINVAL);
11226 
11227 	ill = ipif->ipif_ill;
11228 	if (!ill->ill_isv6)
11229 		return (ENXIO);
11230 
11231 	*sin6 = sin6_null;
11232 	sin6->sin6_family = AF_INET6;
11233 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11234 	sin6->sin6_addr = ill->ill_token;
11235 	lifr->lifr_addrlen = ill->ill_token_length;
11236 	return (0);
11237 }
11238 
11239 /*
11240  * Set (hardware) link specific information that might override
11241  * what was acquired through the DL_INFO_ACK.
11242  */
11243 /* ARGSUSED */
11244 int
11245 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11246     ip_ioctl_cmd_t *ipi, void *if_req)
11247 {
11248 	ill_t		*ill = ipif->ipif_ill;
11249 	int		ip_min_mtu;
11250 	struct lifreq	*lifr = (struct lifreq *)if_req;
11251 	lif_ifinfo_req_t *lir;
11252 
11253 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11254 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11255 	lir = &lifr->lifr_ifinfo;
11256 	ASSERT(IAM_WRITER_IPIF(ipif));
11257 
11258 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11259 	if (ipif->ipif_id != 0)
11260 		return (EINVAL);
11261 
11262 	/* Set interface MTU. */
11263 	if (ipif->ipif_isv6)
11264 		ip_min_mtu = IPV6_MIN_MTU;
11265 	else
11266 		ip_min_mtu = IP_MIN_MTU;
11267 
11268 	/*
11269 	 * Verify values before we set anything. Allow zero to
11270 	 * mean unspecified.
11271 	 *
11272 	 * XXX We should be able to set the user-defined lir_mtu to some value
11273 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11274 	 * ill_max_frag value tells us the max MTU that can be handled by the
11275 	 * datalink, whereas the ill_current_frag is dynamically computed for
11276 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11277 	 * since there is currently no way of distinguishing between
11278 	 * administratively fixed link mtu values (e.g., those set via
11279 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11280 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11281 	 * upper-bound.
11282 	 */
11283 	if (lir->lir_maxmtu != 0 &&
11284 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11285 	    lir->lir_maxmtu < ip_min_mtu))
11286 		return (EINVAL);
11287 	if (lir->lir_reachtime != 0 &&
11288 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11289 		return (EINVAL);
11290 	if (lir->lir_reachretrans != 0 &&
11291 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11292 		return (EINVAL);
11293 
11294 	mutex_enter(&ill->ill_lock);
11295 	/*
11296 	 * The dce and fragmentation code can handle changes to ill_mtu
11297 	 * concurrent with sending/fragmenting packets.
11298 	 */
11299 	if (lir->lir_maxmtu != 0)
11300 		ill->ill_user_mtu = lir->lir_maxmtu;
11301 
11302 	if (lir->lir_reachtime != 0)
11303 		ill->ill_reachable_time = lir->lir_reachtime;
11304 
11305 	if (lir->lir_reachretrans != 0)
11306 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11307 
11308 	ill->ill_max_hops = lir->lir_maxhops;
11309 	ill->ill_max_buf = ND_MAX_Q;
11310 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11311 		/*
11312 		 * ill_mtu is the actual interface MTU, obtained as the min
11313 		 * of user-configured mtu and the value announced by the
11314 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11315 		 * we have already made the choice of requiring
11316 		 * ill_user_mtu < ill_current_frag by the time we get here,
11317 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11318 		 * here.
11319 		 */
11320 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11321 	}
11322 	mutex_exit(&ill->ill_lock);
11323 
11324 	/*
11325 	 * Make sure all dce_generation checks find out
11326 	 * that ill_mtu has changed.
11327 	 */
11328 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11329 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11330 
11331 	/*
11332 	 * Refresh IPMP meta-interface MTU if necessary.
11333 	 */
11334 	if (IS_UNDER_IPMP(ill))
11335 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11336 
11337 	return (0);
11338 }
11339 
11340 /* ARGSUSED */
11341 int
11342 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11343     ip_ioctl_cmd_t *ipi, void *if_req)
11344 {
11345 	struct lif_ifinfo_req *lir;
11346 	ill_t *ill = ipif->ipif_ill;
11347 
11348 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11349 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11350 	if (ipif->ipif_id != 0)
11351 		return (EINVAL);
11352 
11353 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11354 	lir->lir_maxhops = ill->ill_max_hops;
11355 	lir->lir_reachtime = ill->ill_reachable_time;
11356 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11357 	lir->lir_maxmtu = ill->ill_mtu;
11358 
11359 	return (0);
11360 }
11361 
11362 /*
11363  * Return best guess as to the subnet mask for the specified address.
11364  * Based on the subnet masks for all the configured interfaces.
11365  *
11366  * We end up returning a zero mask in the case of default, multicast or
11367  * experimental.
11368  */
11369 static ipaddr_t
11370 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11371 {
11372 	ipaddr_t net_mask;
11373 	ill_t	*ill;
11374 	ipif_t	*ipif;
11375 	ill_walk_context_t ctx;
11376 	ipif_t	*fallback_ipif = NULL;
11377 
11378 	net_mask = ip_net_mask(addr);
11379 	if (net_mask == 0) {
11380 		*ipifp = NULL;
11381 		return (0);
11382 	}
11383 
11384 	/* Let's check to see if this is maybe a local subnet route. */
11385 	/* this function only applies to IPv4 interfaces */
11386 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11387 	ill = ILL_START_WALK_V4(&ctx, ipst);
11388 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11389 		mutex_enter(&ill->ill_lock);
11390 		for (ipif = ill->ill_ipif; ipif != NULL;
11391 		    ipif = ipif->ipif_next) {
11392 			if (IPIF_IS_CONDEMNED(ipif))
11393 				continue;
11394 			if (!(ipif->ipif_flags & IPIF_UP))
11395 				continue;
11396 			if ((ipif->ipif_subnet & net_mask) ==
11397 			    (addr & net_mask)) {
11398 				/*
11399 				 * Don't trust pt-pt interfaces if there are
11400 				 * other interfaces.
11401 				 */
11402 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11403 					if (fallback_ipif == NULL) {
11404 						ipif_refhold_locked(ipif);
11405 						fallback_ipif = ipif;
11406 					}
11407 					continue;
11408 				}
11409 
11410 				/*
11411 				 * Fine. Just assume the same net mask as the
11412 				 * directly attached subnet interface is using.
11413 				 */
11414 				ipif_refhold_locked(ipif);
11415 				mutex_exit(&ill->ill_lock);
11416 				rw_exit(&ipst->ips_ill_g_lock);
11417 				if (fallback_ipif != NULL)
11418 					ipif_refrele(fallback_ipif);
11419 				*ipifp = ipif;
11420 				return (ipif->ipif_net_mask);
11421 			}
11422 		}
11423 		mutex_exit(&ill->ill_lock);
11424 	}
11425 	rw_exit(&ipst->ips_ill_g_lock);
11426 
11427 	*ipifp = fallback_ipif;
11428 	return ((fallback_ipif != NULL) ?
11429 	    fallback_ipif->ipif_net_mask : net_mask);
11430 }
11431 
11432 /*
11433  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11434  */
11435 static void
11436 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11437 {
11438 	IOCP	iocp;
11439 	ipft_t	*ipft;
11440 	ipllc_t	*ipllc;
11441 	mblk_t	*mp1;
11442 	cred_t	*cr;
11443 	int	error = 0;
11444 	conn_t	*connp;
11445 
11446 	ip1dbg(("ip_wput_ioctl"));
11447 	iocp = (IOCP)mp->b_rptr;
11448 	mp1 = mp->b_cont;
11449 	if (mp1 == NULL) {
11450 		iocp->ioc_error = EINVAL;
11451 		mp->b_datap->db_type = M_IOCNAK;
11452 		iocp->ioc_count = 0;
11453 		qreply(q, mp);
11454 		return;
11455 	}
11456 
11457 	/*
11458 	 * These IOCTLs provide various control capabilities to
11459 	 * upstream agents such as ULPs and processes.	There
11460 	 * are currently two such IOCTLs implemented.  They
11461 	 * are used by TCP to provide update information for
11462 	 * existing IREs and to forcibly delete an IRE for a
11463 	 * host that is not responding, thereby forcing an
11464 	 * attempt at a new route.
11465 	 */
11466 	iocp->ioc_error = EINVAL;
11467 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11468 		goto done;
11469 
11470 	ipllc = (ipllc_t *)mp1->b_rptr;
11471 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11472 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11473 			break;
11474 	}
11475 	/*
11476 	 * prefer credential from mblk over ioctl;
11477 	 * see ip_sioctl_copyin_setup
11478 	 */
11479 	cr = msg_getcred(mp, NULL);
11480 	if (cr == NULL)
11481 		cr = iocp->ioc_cr;
11482 
11483 	/*
11484 	 * Refhold the conn in case the request gets queued up in some lookup
11485 	 */
11486 	ASSERT(CONN_Q(q));
11487 	connp = Q_TO_CONN(q);
11488 	CONN_INC_REF(connp);
11489 	if (ipft->ipft_pfi &&
11490 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11491 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11492 		error = (*ipft->ipft_pfi)(q,
11493 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11494 	}
11495 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11496 		/*
11497 		 * CONN_OPER_PENDING_DONE happens in the function called
11498 		 * through ipft_pfi above.
11499 		 */
11500 		return;
11501 	}
11502 
11503 	CONN_OPER_PENDING_DONE(connp);
11504 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11505 		freemsg(mp);
11506 		return;
11507 	}
11508 	iocp->ioc_error = error;
11509 
11510 done:
11511 	mp->b_datap->db_type = M_IOCACK;
11512 	if (iocp->ioc_error)
11513 		iocp->ioc_count = 0;
11514 	qreply(q, mp);
11515 }
11516 
11517 /*
11518  * Assign a unique id for the ipif. This is used by sctp_addr.c
11519  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11520  */
11521 static void
11522 ipif_assign_seqid(ipif_t *ipif)
11523 {
11524 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11525 
11526 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11527 }
11528 
11529 /*
11530  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11531  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11532  * that the clone is complete -- including the seqid -- and the expectation is
11533  * that the caller will either free or overwrite `sipif' before it's unlocked.
11534  */
11535 static void
11536 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11537 {
11538 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11539 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11540 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11541 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11542 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11543 
11544 	dipif->ipif_flags = sipif->ipif_flags;
11545 	dipif->ipif_metric = sipif->ipif_metric;
11546 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11547 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11548 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11549 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11550 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11551 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11552 
11553 	/*
11554 	 * As per the comment atop the function, we assume that these sipif
11555 	 * fields will be changed before sipif is unlocked.
11556 	 */
11557 	dipif->ipif_seqid = sipif->ipif_seqid;
11558 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11559 }
11560 
11561 /*
11562  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11563  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11564  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11565  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11566  * down (i.e., no DAD), of the same type, and unlocked.
11567  */
11568 static void
11569 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11570 {
11571 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11572 	ipxop_t *ipx = ipsq->ipsq_xop;
11573 
11574 	ASSERT(sipif != dipif);
11575 	ASSERT(sipif != virgipif);
11576 
11577 	/*
11578 	 * Grab all of the locks that protect the ipif in a defined order.
11579 	 */
11580 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11581 
11582 	ipif_clone(sipif, dipif);
11583 	if (virgipif != NULL) {
11584 		ipif_clone(virgipif, sipif);
11585 		mi_free(virgipif);
11586 	}
11587 
11588 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11589 
11590 	/*
11591 	 * Transfer ownership of the current xop, if necessary.
11592 	 */
11593 	if (ipx->ipx_current_ipif == sipif) {
11594 		ASSERT(ipx->ipx_pending_ipif == NULL);
11595 		mutex_enter(&ipx->ipx_lock);
11596 		ipx->ipx_current_ipif = dipif;
11597 		mutex_exit(&ipx->ipx_lock);
11598 	}
11599 
11600 	if (virgipif == NULL)
11601 		mi_free(sipif);
11602 }
11603 
11604 /*
11605  * checks if:
11606  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11607  *	- logical interface is within the allowed range
11608  */
11609 static int
11610 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11611 {
11612 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11613 		return (ENAMETOOLONG);
11614 
11615 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11616 		return (ERANGE);
11617 	return (0);
11618 }
11619 
11620 /*
11621  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11622  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11623  * be inserted into the first space available in the list. The value of
11624  * ipif_id will then be set to the appropriate value for its position.
11625  */
11626 static int
11627 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11628 {
11629 	ill_t *ill;
11630 	ipif_t *tipif;
11631 	ipif_t **tipifp;
11632 	int id, err;
11633 	ip_stack_t	*ipst;
11634 
11635 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11636 	    IAM_WRITER_IPIF(ipif));
11637 
11638 	ill = ipif->ipif_ill;
11639 	ASSERT(ill != NULL);
11640 	ipst = ill->ill_ipst;
11641 
11642 	/*
11643 	 * In the case of lo0:0 we already hold the ill_g_lock.
11644 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11645 	 * ipif_insert.
11646 	 */
11647 	if (acquire_g_lock)
11648 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11649 	mutex_enter(&ill->ill_lock);
11650 	id = ipif->ipif_id;
11651 	tipifp = &(ill->ill_ipif);
11652 	if (id == -1) {	/* need to find a real id */
11653 		id = 0;
11654 		while ((tipif = *tipifp) != NULL) {
11655 			ASSERT(tipif->ipif_id >= id);
11656 			if (tipif->ipif_id != id)
11657 				break; /* non-consecutive id */
11658 			id++;
11659 			tipifp = &(tipif->ipif_next);
11660 		}
11661 		if ((err = is_lifname_valid(ill, id)) != 0) {
11662 			mutex_exit(&ill->ill_lock);
11663 			if (acquire_g_lock)
11664 				rw_exit(&ipst->ips_ill_g_lock);
11665 			return (err);
11666 		}
11667 		ipif->ipif_id = id; /* assign new id */
11668 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11669 		/* we have a real id; insert ipif in the right place */
11670 		while ((tipif = *tipifp) != NULL) {
11671 			ASSERT(tipif->ipif_id != id);
11672 			if (tipif->ipif_id > id)
11673 				break; /* found correct location */
11674 			tipifp = &(tipif->ipif_next);
11675 		}
11676 	} else {
11677 		mutex_exit(&ill->ill_lock);
11678 		if (acquire_g_lock)
11679 			rw_exit(&ipst->ips_ill_g_lock);
11680 		return (err);
11681 	}
11682 
11683 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11684 
11685 	ipif->ipif_next = tipif;
11686 	*tipifp = ipif;
11687 	mutex_exit(&ill->ill_lock);
11688 	if (acquire_g_lock)
11689 		rw_exit(&ipst->ips_ill_g_lock);
11690 
11691 	return (0);
11692 }
11693 
11694 static void
11695 ipif_remove(ipif_t *ipif)
11696 {
11697 	ipif_t	**ipifp;
11698 	ill_t	*ill = ipif->ipif_ill;
11699 
11700 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11701 
11702 	mutex_enter(&ill->ill_lock);
11703 	ipifp = &ill->ill_ipif;
11704 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11705 		if (*ipifp == ipif) {
11706 			*ipifp = ipif->ipif_next;
11707 			break;
11708 		}
11709 	}
11710 	mutex_exit(&ill->ill_lock);
11711 }
11712 
11713 /*
11714  * Allocate and initialize a new interface control structure.  (Always
11715  * called as writer.)
11716  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11717  * is not part of the global linked list of ills. ipif_seqid is unique
11718  * in the system and to preserve the uniqueness, it is assigned only
11719  * when ill becomes part of the global list. At that point ill will
11720  * have a name. If it doesn't get assigned here, it will get assigned
11721  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11722  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11723  * the interface flags or any other information from the DL_INFO_ACK for
11724  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11725  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11726  * second DL_INFO_ACK comes in from the driver.
11727  */
11728 static ipif_t *
11729 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11730     boolean_t insert, int *errorp)
11731 {
11732 	int err;
11733 	ipif_t	*ipif;
11734 	ip_stack_t *ipst = ill->ill_ipst;
11735 
11736 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11737 	    ill->ill_name, id, (void *)ill));
11738 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11739 
11740 	if (errorp != NULL)
11741 		*errorp = 0;
11742 
11743 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11744 		if (errorp != NULL)
11745 			*errorp = ENOMEM;
11746 		return (NULL);
11747 	}
11748 	*ipif = ipif_zero;	/* start clean */
11749 
11750 	ipif->ipif_ill = ill;
11751 	ipif->ipif_id = id;	/* could be -1 */
11752 	/*
11753 	 * Inherit the zoneid from the ill; for the shared stack instance
11754 	 * this is always the global zone
11755 	 */
11756 	ipif->ipif_zoneid = ill->ill_zoneid;
11757 
11758 	ipif->ipif_refcnt = 0;
11759 
11760 	if (insert) {
11761 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11762 			mi_free(ipif);
11763 			if (errorp != NULL)
11764 				*errorp = err;
11765 			return (NULL);
11766 		}
11767 		/* -1 id should have been replaced by real id */
11768 		id = ipif->ipif_id;
11769 		ASSERT(id >= 0);
11770 	}
11771 
11772 	if (ill->ill_name[0] != '\0')
11773 		ipif_assign_seqid(ipif);
11774 
11775 	/*
11776 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11777 	 * (which must not exist yet because the zeroth ipif is created once
11778 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11779 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11780 	 */
11781 	if (id == 0 && IS_IPMP(ill)) {
11782 		if (ipmp_illgrp_create(ill) == NULL) {
11783 			if (insert) {
11784 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11785 				ipif_remove(ipif);
11786 				rw_exit(&ipst->ips_ill_g_lock);
11787 			}
11788 			mi_free(ipif);
11789 			if (errorp != NULL)
11790 				*errorp = ENOMEM;
11791 			return (NULL);
11792 		}
11793 	}
11794 
11795 	/*
11796 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11797 	 * not up and can't be looked up until the ioctl completes and the
11798 	 * IPIF_CHANGING flag is cleared.
11799 	 */
11800 	mutex_enter(&ill->ill_lock);
11801 
11802 	ipif->ipif_ire_type = ire_type;
11803 
11804 	if (ipif->ipif_isv6) {
11805 		ill->ill_flags |= ILLF_IPV6;
11806 	} else {
11807 		ipaddr_t inaddr_any = INADDR_ANY;
11808 
11809 		ill->ill_flags |= ILLF_IPV4;
11810 
11811 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11812 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11813 		    &ipif->ipif_v6lcl_addr);
11814 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11815 		    &ipif->ipif_v6subnet);
11816 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11817 		    &ipif->ipif_v6net_mask);
11818 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11819 		    &ipif->ipif_v6brd_addr);
11820 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11821 		    &ipif->ipif_v6pp_dst_addr);
11822 	}
11823 
11824 	/*
11825 	 * Don't set the interface flags etc. now, will do it in
11826 	 * ip_ll_subnet_defaults.
11827 	 */
11828 	if (!initialize)
11829 		goto out;
11830 
11831 	/*
11832 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11833 	 * with no underlying interfaces (and thus an unknown broadcast
11834 	 * address length), but all interfaces that can be placed into an IPMP
11835 	 * group are required to be broadcast-capable.
11836 	 */
11837 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11838 		/*
11839 		 * Later detect lack of DLPI driver multicast capability by
11840 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11841 		 */
11842 		ill->ill_flags |= ILLF_MULTICAST;
11843 		if (!ipif->ipif_isv6)
11844 			ipif->ipif_flags |= IPIF_BROADCAST;
11845 	} else {
11846 		if (ill->ill_net_type != IRE_LOOPBACK) {
11847 			if (ipif->ipif_isv6)
11848 				/*
11849 				 * Note: xresolv interfaces will eventually need
11850 				 * NOARP set here as well, but that will require
11851 				 * those external resolvers to have some
11852 				 * knowledge of that flag and act appropriately.
11853 				 * Not to be changed at present.
11854 				 */
11855 				ill->ill_flags |= ILLF_NONUD;
11856 			else
11857 				ill->ill_flags |= ILLF_NOARP;
11858 		}
11859 		if (ill->ill_phys_addr_length == 0) {
11860 			if (IS_VNI(ill)) {
11861 				ipif->ipif_flags |= IPIF_NOXMIT;
11862 			} else {
11863 				/* pt-pt supports multicast. */
11864 				ill->ill_flags |= ILLF_MULTICAST;
11865 				if (ill->ill_net_type != IRE_LOOPBACK)
11866 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11867 			}
11868 		}
11869 	}
11870 out:
11871 	mutex_exit(&ill->ill_lock);
11872 	return (ipif);
11873 }
11874 
11875 /*
11876  * Remove the neighbor cache entries associated with this logical
11877  * interface.
11878  */
11879 int
11880 ipif_arp_down(ipif_t *ipif)
11881 {
11882 	ill_t	*ill = ipif->ipif_ill;
11883 	int	err = 0;
11884 
11885 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11886 	ASSERT(IAM_WRITER_IPIF(ipif));
11887 
11888 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11889 	    ill_t *, ill, ipif_t *, ipif);
11890 	ipif_nce_down(ipif);
11891 
11892 	/*
11893 	 * If this is the last ipif that is going down and there are no
11894 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11895 	 * clean up ARP completely.
11896 	 */
11897 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11898 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11899 		/*
11900 		 * If this was the last ipif on an IPMP interface, purge any
11901 		 * static ARP entries associated with it.
11902 		 */
11903 		if (IS_IPMP(ill))
11904 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11905 
11906 		/* UNBIND, DETACH */
11907 		err = arp_ll_down(ill);
11908 	}
11909 
11910 	return (err);
11911 }
11912 
11913 /*
11914  * Get the resolver set up for a new IP address.  (Always called as writer.)
11915  * Called both for IPv4 and IPv6 interfaces, though it only does some
11916  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11917  *
11918  * The enumerated value res_act tunes the behavior:
11919  * 	* Res_act_initial: set up all the resolver structures for a new
11920  *	  IP address.
11921  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11922  *	  ARP message in defense of the address.
11923  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11924  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11925  *
11926  * Returns zero on success, or an errno upon failure.
11927  */
11928 int
11929 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11930 {
11931 	ill_t		*ill = ipif->ipif_ill;
11932 	int		err;
11933 	boolean_t	was_dup;
11934 
11935 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11936 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11937 	ASSERT(IAM_WRITER_IPIF(ipif));
11938 
11939 	was_dup = B_FALSE;
11940 	if (res_act == Res_act_initial) {
11941 		ipif->ipif_addr_ready = 0;
11942 		/*
11943 		 * We're bringing an interface up here.  There's no way that we
11944 		 * should need to shut down ARP now.
11945 		 */
11946 		mutex_enter(&ill->ill_lock);
11947 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11948 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11949 			ill->ill_ipif_dup_count--;
11950 			was_dup = B_TRUE;
11951 		}
11952 		mutex_exit(&ill->ill_lock);
11953 	}
11954 	if (ipif->ipif_recovery_id != 0)
11955 		(void) untimeout(ipif->ipif_recovery_id);
11956 	ipif->ipif_recovery_id = 0;
11957 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11958 		ipif->ipif_addr_ready = 1;
11959 		return (0);
11960 	}
11961 	/* NDP will set the ipif_addr_ready flag when it's ready */
11962 	if (ill->ill_isv6)
11963 		return (0);
11964 
11965 	err = ipif_arp_up(ipif, res_act, was_dup);
11966 	return (err);
11967 }
11968 
11969 /*
11970  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11971  * when a link has just gone back up.
11972  */
11973 static void
11974 ipif_nce_start_dad(ipif_t *ipif)
11975 {
11976 	ncec_t *ncec;
11977 	ill_t *ill = ipif->ipif_ill;
11978 	boolean_t isv6 = ill->ill_isv6;
11979 
11980 	if (isv6) {
11981 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11982 		    &ipif->ipif_v6lcl_addr);
11983 	} else {
11984 		ipaddr_t v4addr;
11985 
11986 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11987 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11988 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11989 			/*
11990 			 * If we can't contact ARP for some reason,
11991 			 * that's not really a problem.  Just send
11992 			 * out the routing socket notification that
11993 			 * DAD completion would have done, and continue.
11994 			 */
11995 			ipif_mask_reply(ipif);
11996 			ipif_up_notify(ipif);
11997 			ipif->ipif_addr_ready = 1;
11998 			return;
11999 		}
12000 
12001 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12002 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12003 	}
12004 
12005 	if (ncec == NULL) {
12006 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12007 		    (void *)ipif));
12008 		return;
12009 	}
12010 	if (!nce_restart_dad(ncec)) {
12011 		/*
12012 		 * If we can't restart DAD for some reason, that's not really a
12013 		 * problem.  Just send out the routing socket notification that
12014 		 * DAD completion would have done, and continue.
12015 		 */
12016 		ipif_up_notify(ipif);
12017 		ipif->ipif_addr_ready = 1;
12018 	}
12019 	ncec_refrele(ncec);
12020 }
12021 
12022 /*
12023  * Restart duplicate address detection on all interfaces on the given ill.
12024  *
12025  * This is called when an interface transitions from down to up
12026  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12027  *
12028  * Note that since the underlying physical link has transitioned, we must cause
12029  * at least one routing socket message to be sent here, either via DAD
12030  * completion or just by default on the first ipif.  (If we don't do this, then
12031  * in.mpathd will see long delays when doing link-based failure recovery.)
12032  */
12033 void
12034 ill_restart_dad(ill_t *ill, boolean_t went_up)
12035 {
12036 	ipif_t *ipif;
12037 
12038 	if (ill == NULL)
12039 		return;
12040 
12041 	/*
12042 	 * If layer two doesn't support duplicate address detection, then just
12043 	 * send the routing socket message now and be done with it.
12044 	 */
12045 	if (!ill->ill_isv6 && arp_no_defense) {
12046 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12047 		return;
12048 	}
12049 
12050 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12051 		if (went_up) {
12052 
12053 			if (ipif->ipif_flags & IPIF_UP) {
12054 				ipif_nce_start_dad(ipif);
12055 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12056 				/*
12057 				 * kick off the bring-up process now.
12058 				 */
12059 				ipif_do_recovery(ipif);
12060 			} else {
12061 				/*
12062 				 * Unfortunately, the first ipif is "special"
12063 				 * and represents the underlying ill in the
12064 				 * routing socket messages.  Thus, when this
12065 				 * one ipif is down, we must still notify so
12066 				 * that the user knows the IFF_RUNNING status
12067 				 * change.  (If the first ipif is up, then
12068 				 * we'll handle eventual routing socket
12069 				 * notification via DAD completion.)
12070 				 */
12071 				if (ipif == ill->ill_ipif) {
12072 					ip_rts_ifmsg(ill->ill_ipif,
12073 					    RTSQ_DEFAULT);
12074 				}
12075 			}
12076 		} else {
12077 			/*
12078 			 * After link down, we'll need to send a new routing
12079 			 * message when the link comes back, so clear
12080 			 * ipif_addr_ready.
12081 			 */
12082 			ipif->ipif_addr_ready = 0;
12083 		}
12084 	}
12085 
12086 	/*
12087 	 * If we've torn down links, then notify the user right away.
12088 	 */
12089 	if (!went_up)
12090 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12091 }
12092 
12093 static void
12094 ipsq_delete(ipsq_t *ipsq)
12095 {
12096 	ipxop_t *ipx = ipsq->ipsq_xop;
12097 
12098 	ipsq->ipsq_ipst = NULL;
12099 	ASSERT(ipsq->ipsq_phyint == NULL);
12100 	ASSERT(ipsq->ipsq_xop != NULL);
12101 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12102 	ASSERT(ipx->ipx_pending_mp == NULL);
12103 	kmem_free(ipsq, sizeof (ipsq_t));
12104 }
12105 
12106 static int
12107 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12108 {
12109 	int err = 0;
12110 	ipif_t *ipif;
12111 
12112 	if (ill == NULL)
12113 		return (0);
12114 
12115 	ASSERT(IAM_WRITER_ILL(ill));
12116 	ill->ill_up_ipifs = B_TRUE;
12117 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12118 		if (ipif->ipif_was_up) {
12119 			if (!(ipif->ipif_flags & IPIF_UP))
12120 				err = ipif_up(ipif, q, mp);
12121 			ipif->ipif_was_up = B_FALSE;
12122 			if (err != 0) {
12123 				ASSERT(err == EINPROGRESS);
12124 				return (err);
12125 			}
12126 		}
12127 	}
12128 	ill->ill_up_ipifs = B_FALSE;
12129 	return (0);
12130 }
12131 
12132 /*
12133  * This function is called to bring up all the ipifs that were up before
12134  * bringing the ill down via ill_down_ipifs().
12135  */
12136 int
12137 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12138 {
12139 	int err;
12140 
12141 	ASSERT(IAM_WRITER_ILL(ill));
12142 
12143 	if (ill->ill_replumbing) {
12144 		ill->ill_replumbing = 0;
12145 		/*
12146 		 * Send down REPLUMB_DONE notification followed by the
12147 		 * BIND_REQ on the arp stream.
12148 		 */
12149 		if (!ill->ill_isv6)
12150 			arp_send_replumb_conf(ill);
12151 	}
12152 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12153 	if (err != 0)
12154 		return (err);
12155 
12156 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12157 }
12158 
12159 /*
12160  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12161  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12162  */
12163 static void
12164 ill_down_ipifs(ill_t *ill, boolean_t logical)
12165 {
12166 	ipif_t *ipif;
12167 
12168 	ASSERT(IAM_WRITER_ILL(ill));
12169 
12170 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12171 		/*
12172 		 * We go through the ipif_down logic even if the ipif
12173 		 * is already down, since routes can be added based
12174 		 * on down ipifs. Going through ipif_down once again
12175 		 * will delete any IREs created based on these routes.
12176 		 */
12177 		if (ipif->ipif_flags & IPIF_UP)
12178 			ipif->ipif_was_up = B_TRUE;
12179 
12180 		if (logical) {
12181 			(void) ipif_logical_down(ipif, NULL, NULL);
12182 			ipif_non_duplicate(ipif);
12183 			(void) ipif_down_tail(ipif);
12184 		} else {
12185 			(void) ipif_down(ipif, NULL, NULL);
12186 		}
12187 	}
12188 }
12189 
12190 /*
12191  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12192  * a look again at valid source addresses.
12193  * This should be called each time after the set of source addresses has been
12194  * changed.
12195  */
12196 void
12197 ip_update_source_selection(ip_stack_t *ipst)
12198 {
12199 	/* We skip past SRC_GENERATION_VERIFY */
12200 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12201 	    SRC_GENERATION_VERIFY)
12202 		atomic_add_32(&ipst->ips_src_generation, 1);
12203 }
12204 
12205 /*
12206  * Finish the group join started in ip_sioctl_groupname().
12207  */
12208 /* ARGSUSED */
12209 static void
12210 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12211 {
12212 	ill_t		*ill = q->q_ptr;
12213 	phyint_t	*phyi = ill->ill_phyint;
12214 	ipmp_grp_t	*grp = phyi->phyint_grp;
12215 	ip_stack_t	*ipst = ill->ill_ipst;
12216 
12217 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12218 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12219 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12220 
12221 	if (phyi->phyint_illv4 != NULL) {
12222 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12223 		VERIFY(grp->gr_pendv4-- > 0);
12224 		rw_exit(&ipst->ips_ipmp_lock);
12225 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12226 	}
12227 	if (phyi->phyint_illv6 != NULL) {
12228 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12229 		VERIFY(grp->gr_pendv6-- > 0);
12230 		rw_exit(&ipst->ips_ipmp_lock);
12231 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12232 	}
12233 	freemsg(mp);
12234 }
12235 
12236 /*
12237  * Process an SIOCSLIFGROUPNAME request.
12238  */
12239 /* ARGSUSED */
12240 int
12241 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12242     ip_ioctl_cmd_t *ipip, void *ifreq)
12243 {
12244 	struct lifreq	*lifr = ifreq;
12245 	ill_t		*ill = ipif->ipif_ill;
12246 	ip_stack_t	*ipst = ill->ill_ipst;
12247 	phyint_t	*phyi = ill->ill_phyint;
12248 	ipmp_grp_t	*grp = phyi->phyint_grp;
12249 	mblk_t		*ipsq_mp;
12250 	int		err = 0;
12251 
12252 	/*
12253 	 * Note that phyint_grp can only change here, where we're exclusive.
12254 	 */
12255 	ASSERT(IAM_WRITER_ILL(ill));
12256 
12257 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12258 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12259 		return (EINVAL);
12260 
12261 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12262 
12263 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12264 
12265 	/*
12266 	 * If the name hasn't changed, there's nothing to do.
12267 	 */
12268 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12269 		goto unlock;
12270 
12271 	/*
12272 	 * Handle requests to rename an IPMP meta-interface.
12273 	 *
12274 	 * Note that creation of the IPMP meta-interface is handled in
12275 	 * userland through the standard plumbing sequence.  As part of the
12276 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12277 	 * the name of the interface (see ipif_set_values_tail()).
12278 	 */
12279 	if (IS_IPMP(ill)) {
12280 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12281 		goto unlock;
12282 	}
12283 
12284 	/*
12285 	 * Handle requests to add or remove an IP interface from a group.
12286 	 */
12287 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12288 		/*
12289 		 * Moves are handled by first removing the interface from
12290 		 * its existing group, and then adding it to another group.
12291 		 * So, fail if it's already in a group.
12292 		 */
12293 		if (IS_UNDER_IPMP(ill)) {
12294 			err = EALREADY;
12295 			goto unlock;
12296 		}
12297 
12298 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12299 		if (grp == NULL) {
12300 			err = ENOENT;
12301 			goto unlock;
12302 		}
12303 
12304 		/*
12305 		 * Check if the phyint and its ills are suitable for
12306 		 * inclusion into the group.
12307 		 */
12308 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12309 			goto unlock;
12310 
12311 		/*
12312 		 * Checks pass; join the group, and enqueue the remaining
12313 		 * illgrp joins for when we've become part of the group xop
12314 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12315 		 * requires an mblk_t to scribble on, and since `mp' will be
12316 		 * freed as part of completing the ioctl, allocate another.
12317 		 */
12318 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12319 			err = ENOMEM;
12320 			goto unlock;
12321 		}
12322 
12323 		/*
12324 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12325 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12326 		 * before ip_join_illgrps() is called back.  See the comments
12327 		 * in ip_sioctl_plink_ipmp() for more.
12328 		 */
12329 		if (phyi->phyint_illv4 != NULL)
12330 			grp->gr_pendv4++;
12331 		if (phyi->phyint_illv6 != NULL)
12332 			grp->gr_pendv6++;
12333 
12334 		rw_exit(&ipst->ips_ipmp_lock);
12335 
12336 		ipmp_phyint_join_grp(phyi, grp);
12337 		ill_refhold(ill);
12338 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12339 		    SWITCH_OP, B_FALSE);
12340 		return (0);
12341 	} else {
12342 		/*
12343 		 * Request to remove the interface from a group.  If the
12344 		 * interface is not in a group, this trivially succeeds.
12345 		 */
12346 		rw_exit(&ipst->ips_ipmp_lock);
12347 		if (IS_UNDER_IPMP(ill))
12348 			ipmp_phyint_leave_grp(phyi);
12349 		return (0);
12350 	}
12351 unlock:
12352 	rw_exit(&ipst->ips_ipmp_lock);
12353 	return (err);
12354 }
12355 
12356 /*
12357  * Process an SIOCGLIFBINDING request.
12358  */
12359 /* ARGSUSED */
12360 int
12361 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12362     ip_ioctl_cmd_t *ipip, void *ifreq)
12363 {
12364 	ill_t		*ill;
12365 	struct lifreq	*lifr = ifreq;
12366 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12367 
12368 	if (!IS_IPMP(ipif->ipif_ill))
12369 		return (EINVAL);
12370 
12371 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12372 	if ((ill = ipif->ipif_bound_ill) == NULL)
12373 		lifr->lifr_binding[0] = '\0';
12374 	else
12375 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12376 	rw_exit(&ipst->ips_ipmp_lock);
12377 	return (0);
12378 }
12379 
12380 /*
12381  * Process an SIOCGLIFGROUPNAME request.
12382  */
12383 /* ARGSUSED */
12384 int
12385 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12386     ip_ioctl_cmd_t *ipip, void *ifreq)
12387 {
12388 	ipmp_grp_t	*grp;
12389 	struct lifreq	*lifr = ifreq;
12390 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12391 
12392 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12393 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12394 		lifr->lifr_groupname[0] = '\0';
12395 	else
12396 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12397 	rw_exit(&ipst->ips_ipmp_lock);
12398 	return (0);
12399 }
12400 
12401 /*
12402  * Process an SIOCGLIFGROUPINFO request.
12403  */
12404 /* ARGSUSED */
12405 int
12406 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12407     ip_ioctl_cmd_t *ipip, void *dummy)
12408 {
12409 	ipmp_grp_t	*grp;
12410 	lifgroupinfo_t	*lifgr;
12411 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12412 
12413 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12414 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12415 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12416 
12417 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12418 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12419 		rw_exit(&ipst->ips_ipmp_lock);
12420 		return (ENOENT);
12421 	}
12422 	ipmp_grp_info(grp, lifgr);
12423 	rw_exit(&ipst->ips_ipmp_lock);
12424 	return (0);
12425 }
12426 
12427 static void
12428 ill_dl_down(ill_t *ill)
12429 {
12430 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12431 
12432 	/*
12433 	 * The ill is down; unbind but stay attached since we're still
12434 	 * associated with a PPA. If we have negotiated DLPI capabilites
12435 	 * with the data link service provider (IDS_OK) then reset them.
12436 	 * The interval between unbinding and rebinding is potentially
12437 	 * unbounded hence we cannot assume things will be the same.
12438 	 * The DLPI capabilities will be probed again when the data link
12439 	 * is brought up.
12440 	 */
12441 	mblk_t	*mp = ill->ill_unbind_mp;
12442 
12443 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12444 
12445 	if (!ill->ill_replumbing) {
12446 		/* Free all ilms for this ill */
12447 		update_conn_ill(ill, ill->ill_ipst);
12448 	} else {
12449 		ill_leave_multicast(ill);
12450 	}
12451 
12452 	ill->ill_unbind_mp = NULL;
12453 	if (mp != NULL) {
12454 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12455 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12456 		    ill->ill_name));
12457 		mutex_enter(&ill->ill_lock);
12458 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12459 		mutex_exit(&ill->ill_lock);
12460 		/*
12461 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12462 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12463 		 * ill_capability_dld_disable disable rightaway. If this is not
12464 		 * an unplumb operation then the disable happens on receipt of
12465 		 * the capab ack via ip_rput_dlpi_writer ->
12466 		 * ill_capability_ack_thr. In both cases the order of
12467 		 * the operations seen by DLD is capability disable followed
12468 		 * by DL_UNBIND. Also the DLD capability disable needs a
12469 		 * cv_wait'able context.
12470 		 */
12471 		if (ill->ill_state_flags & ILL_CONDEMNED)
12472 			ill_capability_dld_disable(ill);
12473 		ill_capability_reset(ill, B_FALSE);
12474 		ill_dlpi_send(ill, mp);
12475 	}
12476 	mutex_enter(&ill->ill_lock);
12477 	ill->ill_dl_up = 0;
12478 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12479 	mutex_exit(&ill->ill_lock);
12480 }
12481 
12482 void
12483 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12484 {
12485 	union DL_primitives *dlp;
12486 	t_uscalar_t prim;
12487 	boolean_t waitack = B_FALSE;
12488 
12489 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12490 
12491 	dlp = (union DL_primitives *)mp->b_rptr;
12492 	prim = dlp->dl_primitive;
12493 
12494 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12495 	    dl_primstr(prim), prim, ill->ill_name));
12496 
12497 	switch (prim) {
12498 	case DL_PHYS_ADDR_REQ:
12499 	{
12500 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12501 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12502 		break;
12503 	}
12504 	case DL_BIND_REQ:
12505 		mutex_enter(&ill->ill_lock);
12506 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12507 		mutex_exit(&ill->ill_lock);
12508 		break;
12509 	}
12510 
12511 	/*
12512 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12513 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12514 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12515 	 */
12516 	mutex_enter(&ill->ill_lock);
12517 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12518 	    (prim == DL_UNBIND_REQ)) {
12519 		ill->ill_dlpi_pending = prim;
12520 		waitack = B_TRUE;
12521 	}
12522 
12523 	mutex_exit(&ill->ill_lock);
12524 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12525 	    char *, dl_primstr(prim), ill_t *, ill);
12526 	putnext(ill->ill_wq, mp);
12527 
12528 	/*
12529 	 * There is no ack for DL_NOTIFY_CONF messages
12530 	 */
12531 	if (waitack && prim == DL_NOTIFY_CONF)
12532 		ill_dlpi_done(ill, prim);
12533 }
12534 
12535 /*
12536  * Helper function for ill_dlpi_send().
12537  */
12538 /* ARGSUSED */
12539 static void
12540 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12541 {
12542 	ill_dlpi_send(q->q_ptr, mp);
12543 }
12544 
12545 /*
12546  * Send a DLPI control message to the driver but make sure there
12547  * is only one outstanding message. Uses ill_dlpi_pending to tell
12548  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12549  * when an ACK or a NAK is received to process the next queued message.
12550  */
12551 void
12552 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12553 {
12554 	mblk_t **mpp;
12555 
12556 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12557 
12558 	/*
12559 	 * To ensure that any DLPI requests for current exclusive operation
12560 	 * are always completely sent before any DLPI messages for other
12561 	 * operations, require writer access before enqueuing.
12562 	 */
12563 	if (!IAM_WRITER_ILL(ill)) {
12564 		ill_refhold(ill);
12565 		/* qwriter_ip() does the ill_refrele() */
12566 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12567 		    NEW_OP, B_TRUE);
12568 		return;
12569 	}
12570 
12571 	mutex_enter(&ill->ill_lock);
12572 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12573 		/* Must queue message. Tail insertion */
12574 		mpp = &ill->ill_dlpi_deferred;
12575 		while (*mpp != NULL)
12576 			mpp = &((*mpp)->b_next);
12577 
12578 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12579 		    "while %s pending\n", ill->ill_name,
12580 		    dl_primstr(ill->ill_dlpi_pending)));
12581 
12582 		*mpp = mp;
12583 		mutex_exit(&ill->ill_lock);
12584 		return;
12585 	}
12586 	mutex_exit(&ill->ill_lock);
12587 	ill_dlpi_dispatch(ill, mp);
12588 }
12589 
12590 void
12591 ill_capability_send(ill_t *ill, mblk_t *mp)
12592 {
12593 	ill->ill_capab_pending_cnt++;
12594 	ill_dlpi_send(ill, mp);
12595 }
12596 
12597 void
12598 ill_capability_done(ill_t *ill)
12599 {
12600 	ASSERT(ill->ill_capab_pending_cnt != 0);
12601 
12602 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12603 
12604 	ill->ill_capab_pending_cnt--;
12605 	if (ill->ill_capab_pending_cnt == 0 &&
12606 	    ill->ill_dlpi_capab_state == IDCS_OK)
12607 		ill_capability_reset_alloc(ill);
12608 }
12609 
12610 /*
12611  * Send all deferred DLPI messages without waiting for their ACKs.
12612  */
12613 void
12614 ill_dlpi_send_deferred(ill_t *ill)
12615 {
12616 	mblk_t *mp, *nextmp;
12617 
12618 	/*
12619 	 * Clear ill_dlpi_pending so that the message is not queued in
12620 	 * ill_dlpi_send().
12621 	 */
12622 	mutex_enter(&ill->ill_lock);
12623 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12624 	mp = ill->ill_dlpi_deferred;
12625 	ill->ill_dlpi_deferred = NULL;
12626 	mutex_exit(&ill->ill_lock);
12627 
12628 	for (; mp != NULL; mp = nextmp) {
12629 		nextmp = mp->b_next;
12630 		mp->b_next = NULL;
12631 		ill_dlpi_send(ill, mp);
12632 	}
12633 }
12634 
12635 /*
12636  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12637  * or M_HANGUP
12638  */
12639 static void
12640 ill_dlpi_clear_deferred(ill_t *ill)
12641 {
12642 	mblk_t	*mp, *nextmp;
12643 
12644 	mutex_enter(&ill->ill_lock);
12645 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12646 	mp = ill->ill_dlpi_deferred;
12647 	ill->ill_dlpi_deferred = NULL;
12648 	mutex_exit(&ill->ill_lock);
12649 
12650 	for (; mp != NULL; mp = nextmp) {
12651 		nextmp = mp->b_next;
12652 		inet_freemsg(mp);
12653 	}
12654 }
12655 
12656 /*
12657  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12658  */
12659 boolean_t
12660 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12661 {
12662 	t_uscalar_t pending;
12663 
12664 	mutex_enter(&ill->ill_lock);
12665 	if (ill->ill_dlpi_pending == prim) {
12666 		mutex_exit(&ill->ill_lock);
12667 		return (B_TRUE);
12668 	}
12669 
12670 	/*
12671 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12672 	 * without waiting, so don't print any warnings in that case.
12673 	 */
12674 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12675 		mutex_exit(&ill->ill_lock);
12676 		return (B_FALSE);
12677 	}
12678 	pending = ill->ill_dlpi_pending;
12679 	mutex_exit(&ill->ill_lock);
12680 
12681 	if (pending == DL_PRIM_INVAL) {
12682 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12683 		    "received unsolicited ack for %s on %s\n",
12684 		    dl_primstr(prim), ill->ill_name);
12685 	} else {
12686 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12687 		    "received unexpected ack for %s on %s (expecting %s)\n",
12688 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12689 	}
12690 	return (B_FALSE);
12691 }
12692 
12693 /*
12694  * Complete the current DLPI operation associated with `prim' on `ill' and
12695  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12696  * operations and the ill's current exclusive IPSQ operation has finished
12697  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12698  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12699  * the comments above ipsq_current_finish() for details.
12700  */
12701 void
12702 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12703 {
12704 	mblk_t *mp;
12705 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12706 	ipxop_t *ipx = ipsq->ipsq_xop;
12707 
12708 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12709 	mutex_enter(&ill->ill_lock);
12710 
12711 	ASSERT(prim != DL_PRIM_INVAL);
12712 	ASSERT(ill->ill_dlpi_pending == prim);
12713 
12714 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12715 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12716 
12717 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12718 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12719 		if (ipx->ipx_current_done) {
12720 			mutex_enter(&ipx->ipx_lock);
12721 			ipx->ipx_current_ipif = NULL;
12722 			mutex_exit(&ipx->ipx_lock);
12723 		}
12724 		cv_signal(&ill->ill_cv);
12725 		mutex_exit(&ill->ill_lock);
12726 		return;
12727 	}
12728 
12729 	ill->ill_dlpi_deferred = mp->b_next;
12730 	mp->b_next = NULL;
12731 	mutex_exit(&ill->ill_lock);
12732 
12733 	ill_dlpi_dispatch(ill, mp);
12734 }
12735 
12736 /*
12737  * Queue a (multicast) DLPI control message to be sent to the driver by
12738  * later calling ill_dlpi_send_queued.
12739  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12740  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12741  * for the same group to race.
12742  * We send DLPI control messages in order using ill_lock.
12743  * For IPMP we should be called on the cast_ill.
12744  */
12745 void
12746 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12747 {
12748 	mblk_t **mpp;
12749 
12750 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12751 
12752 	mutex_enter(&ill->ill_lock);
12753 	/* Must queue message. Tail insertion */
12754 	mpp = &ill->ill_dlpi_deferred;
12755 	while (*mpp != NULL)
12756 		mpp = &((*mpp)->b_next);
12757 
12758 	*mpp = mp;
12759 	mutex_exit(&ill->ill_lock);
12760 }
12761 
12762 /*
12763  * Send the messages that were queued. Make sure there is only
12764  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12765  * when an ACK or a NAK is received to process the next queued message.
12766  * For IPMP we are called on the upper ill, but when send what is queued
12767  * on the cast_ill.
12768  */
12769 void
12770 ill_dlpi_send_queued(ill_t *ill)
12771 {
12772 	mblk_t	*mp;
12773 	union DL_primitives *dlp;
12774 	t_uscalar_t prim;
12775 	ill_t *release_ill = NULL;
12776 
12777 	if (IS_IPMP(ill)) {
12778 		/* On the upper IPMP ill. */
12779 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12780 		if (release_ill == NULL) {
12781 			/* Avoid ever sending anything down to the ipmpstub */
12782 			return;
12783 		}
12784 		ill = release_ill;
12785 	}
12786 	mutex_enter(&ill->ill_lock);
12787 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12788 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12789 			/* Can't send. Somebody else will send it */
12790 			mutex_exit(&ill->ill_lock);
12791 			goto done;
12792 		}
12793 		ill->ill_dlpi_deferred = mp->b_next;
12794 		mp->b_next = NULL;
12795 		if (!ill->ill_dl_up) {
12796 			/*
12797 			 * Nobody there. All multicast addresses will be
12798 			 * re-joined when we get the DL_BIND_ACK bringing the
12799 			 * interface up.
12800 			 */
12801 			freemsg(mp);
12802 			continue;
12803 		}
12804 		dlp = (union DL_primitives *)mp->b_rptr;
12805 		prim = dlp->dl_primitive;
12806 
12807 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12808 		    (prim == DL_UNBIND_REQ)) {
12809 			ill->ill_dlpi_pending = prim;
12810 		}
12811 		mutex_exit(&ill->ill_lock);
12812 
12813 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12814 		    char *, dl_primstr(prim), ill_t *, ill);
12815 		putnext(ill->ill_wq, mp);
12816 		mutex_enter(&ill->ill_lock);
12817 	}
12818 	mutex_exit(&ill->ill_lock);
12819 done:
12820 	if (release_ill != NULL)
12821 		ill_refrele(release_ill);
12822 }
12823 
12824 /*
12825  * Queue an IP (IGMP/MLD) message to be sent by IP from
12826  * ill_mcast_send_queued
12827  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12828  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12829  * group to race.
12830  * We send them in order using ill_lock.
12831  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12832  */
12833 void
12834 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12835 {
12836 	mblk_t **mpp;
12837 	ill_t *release_ill = NULL;
12838 
12839 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12840 
12841 	if (IS_IPMP(ill)) {
12842 		/* On the upper IPMP ill. */
12843 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12844 		if (release_ill == NULL) {
12845 			/* Discard instead of queuing for the ipmp interface */
12846 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12847 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12848 			    mp, ill);
12849 			freemsg(mp);
12850 			return;
12851 		}
12852 		ill = release_ill;
12853 	}
12854 
12855 	mutex_enter(&ill->ill_lock);
12856 	/* Must queue message. Tail insertion */
12857 	mpp = &ill->ill_mcast_deferred;
12858 	while (*mpp != NULL)
12859 		mpp = &((*mpp)->b_next);
12860 
12861 	*mpp = mp;
12862 	mutex_exit(&ill->ill_lock);
12863 	if (release_ill != NULL)
12864 		ill_refrele(release_ill);
12865 }
12866 
12867 /*
12868  * Send the IP packets that were queued by ill_mcast_queue.
12869  * These are IGMP/MLD packets.
12870  *
12871  * For IPMP we are called on the upper ill, but when send what is queued
12872  * on the cast_ill.
12873  *
12874  * Request loopback of the report if we are acting as a multicast
12875  * router, so that the process-level routing demon can hear it.
12876  * This will run multiple times for the same group if there are members
12877  * on the same group for multiple ipif's on the same ill. The
12878  * igmp_input/mld_input code will suppress this due to the loopback thus we
12879  * always loopback membership report.
12880  *
12881  * We also need to make sure that this does not get load balanced
12882  * by IPMP. We do this by passing an ill to ip_output_simple.
12883  */
12884 void
12885 ill_mcast_send_queued(ill_t *ill)
12886 {
12887 	mblk_t	*mp;
12888 	ip_xmit_attr_t ixas;
12889 	ill_t *release_ill = NULL;
12890 
12891 	if (IS_IPMP(ill)) {
12892 		/* On the upper IPMP ill. */
12893 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12894 		if (release_ill == NULL) {
12895 			/*
12896 			 * We should have no messages on the ipmp interface
12897 			 * but no point in trying to send them.
12898 			 */
12899 			return;
12900 		}
12901 		ill = release_ill;
12902 	}
12903 	bzero(&ixas, sizeof (ixas));
12904 	ixas.ixa_zoneid = ALL_ZONES;
12905 	ixas.ixa_cred = kcred;
12906 	ixas.ixa_cpid = NOPID;
12907 	ixas.ixa_tsl = NULL;
12908 	/*
12909 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12910 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12911 	 * That is necessary to handle IGMP/MLD snooping switches.
12912 	 */
12913 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12914 	ixas.ixa_ipst = ill->ill_ipst;
12915 
12916 	mutex_enter(&ill->ill_lock);
12917 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12918 		ill->ill_mcast_deferred = mp->b_next;
12919 		mp->b_next = NULL;
12920 		if (!ill->ill_dl_up) {
12921 			/*
12922 			 * Nobody there. Just drop the ip packets.
12923 			 * IGMP/MLD will resend later, if this is a replumb.
12924 			 */
12925 			freemsg(mp);
12926 			continue;
12927 		}
12928 		mutex_enter(&ill->ill_phyint->phyint_lock);
12929 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12930 			/*
12931 			 * When the ill is getting deactivated, we only want to
12932 			 * send the DLPI messages, so drop IGMP/MLD packets.
12933 			 * DLPI messages are handled by ill_dlpi_send_queued()
12934 			 */
12935 			mutex_exit(&ill->ill_phyint->phyint_lock);
12936 			freemsg(mp);
12937 			continue;
12938 		}
12939 		mutex_exit(&ill->ill_phyint->phyint_lock);
12940 		mutex_exit(&ill->ill_lock);
12941 
12942 		/* Check whether we are sending IPv4 or IPv6. */
12943 		if (ill->ill_isv6) {
12944 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12945 
12946 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12947 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12948 		} else {
12949 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12950 
12951 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12952 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12953 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12954 		}
12955 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
12956 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12957 		(void) ip_output_simple(mp, &ixas);
12958 		ixa_cleanup(&ixas);
12959 
12960 		mutex_enter(&ill->ill_lock);
12961 	}
12962 	mutex_exit(&ill->ill_lock);
12963 
12964 done:
12965 	if (release_ill != NULL)
12966 		ill_refrele(release_ill);
12967 }
12968 
12969 /*
12970  * Take down a specific interface, but don't lose any information about it.
12971  * (Always called as writer.)
12972  * This function goes through the down sequence even if the interface is
12973  * already down. There are 2 reasons.
12974  * a. Currently we permit interface routes that depend on down interfaces
12975  *    to be added. This behaviour itself is questionable. However it appears
12976  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12977  *    time. We go thru the cleanup in order to remove these routes.
12978  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12979  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12980  *    down, but we need to cleanup i.e. do ill_dl_down and
12981  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12982  *
12983  * IP-MT notes:
12984  *
12985  * Model of reference to interfaces.
12986  *
12987  * The following members in ipif_t track references to the ipif.
12988  *	int     ipif_refcnt;    Active reference count
12989  *
12990  * The following members in ill_t track references to the ill.
12991  *	int             ill_refcnt;     active refcnt
12992  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12993  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12994  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12995  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12996  *
12997  * Reference to an ipif or ill can be obtained in any of the following ways.
12998  *
12999  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13000  * Pointers to ipif / ill from other data structures viz ire and conn.
13001  * Implicit reference to the ipif / ill by holding a reference to the ire.
13002  *
13003  * The ipif/ill lookup functions return a reference held ipif / ill.
13004  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13005  * This is a purely dynamic reference count associated with threads holding
13006  * references to the ipif / ill. Pointers from other structures do not
13007  * count towards this reference count.
13008  *
13009  * ill_ire_cnt is the number of ire's associated with the
13010  * ill. This is incremented whenever a new ire is created referencing the
13011  * ill. This is done atomically inside ire_add_v[46] where the ire is
13012  * actually added to the ire hash table. The count is decremented in
13013  * ire_inactive where the ire is destroyed.
13014  *
13015  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13016  * This is incremented atomically in
13017  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13018  * table. Similarly it is decremented in ncec_inactive() where the ncec
13019  * is destroyed.
13020  *
13021  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13022  * incremented atomically in nce_add() where the nce is actually added to the
13023  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13024  * is destroyed.
13025  *
13026  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13027  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13028  *
13029  * Flow of ioctls involving interface down/up
13030  *
13031  * The following is the sequence of an attempt to set some critical flags on an
13032  * up interface.
13033  * ip_sioctl_flags
13034  * ipif_down
13035  * wait for ipif to be quiescent
13036  * ipif_down_tail
13037  * ip_sioctl_flags_tail
13038  *
13039  * All set ioctls that involve down/up sequence would have a skeleton similar
13040  * to the above. All the *tail functions are called after the refcounts have
13041  * dropped to the appropriate values.
13042  *
13043  * SIOC ioctls during the IPIF_CHANGING interval.
13044  *
13045  * Threads handling SIOC set ioctls serialize on the squeue, but this
13046  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13047  * steps of internal changes to the state, some of which are visible in
13048  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13049  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13050  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13051  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13052  * the current exclusive operation completes. The IPIF_CHANGING check
13053  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13054  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13055  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13056  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13057  * until we release the ipsq_lock, even though the ill/ipif state flags
13058  * can change after we drop the ill_lock.
13059  */
13060 int
13061 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13062 {
13063 	ill_t		*ill = ipif->ipif_ill;
13064 	conn_t		*connp;
13065 	boolean_t	success;
13066 	boolean_t	ipif_was_up = B_FALSE;
13067 	ip_stack_t	*ipst = ill->ill_ipst;
13068 
13069 	ASSERT(IAM_WRITER_IPIF(ipif));
13070 
13071 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13072 
13073 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13074 	    ill_t *, ill, ipif_t *, ipif);
13075 
13076 	if (ipif->ipif_flags & IPIF_UP) {
13077 		mutex_enter(&ill->ill_lock);
13078 		ipif->ipif_flags &= ~IPIF_UP;
13079 		ASSERT(ill->ill_ipif_up_count > 0);
13080 		--ill->ill_ipif_up_count;
13081 		mutex_exit(&ill->ill_lock);
13082 		ipif_was_up = B_TRUE;
13083 		/* Update status in SCTP's list */
13084 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13085 		ill_nic_event_dispatch(ipif->ipif_ill,
13086 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13087 	}
13088 
13089 	/*
13090 	 * Blow away memberships we established in ipif_multicast_up().
13091 	 */
13092 	ipif_multicast_down(ipif);
13093 
13094 	/*
13095 	 * Remove from the mapping for __sin6_src_id. We insert only
13096 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13097 	 * stored as mapped addresses, we need to check for mapped
13098 	 * INADDR_ANY also.
13099 	 */
13100 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13101 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13102 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13103 		int err;
13104 
13105 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13106 		    ipif->ipif_zoneid, ipst);
13107 		if (err != 0) {
13108 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13109 		}
13110 	}
13111 
13112 	if (ipif_was_up) {
13113 		/* only delete if we'd added ire's before */
13114 		if (ipif->ipif_isv6)
13115 			ipif_delete_ires_v6(ipif);
13116 		else
13117 			ipif_delete_ires_v4(ipif);
13118 	}
13119 
13120 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13121 		/*
13122 		 * Since the interface is now down, it may have just become
13123 		 * inactive.  Note that this needs to be done even for a
13124 		 * lll_logical_down(), or ARP entries will not get correctly
13125 		 * restored when the interface comes back up.
13126 		 */
13127 		if (IS_UNDER_IPMP(ill))
13128 			ipmp_ill_refresh_active(ill);
13129 	}
13130 
13131 	/*
13132 	 * neighbor-discovery or arp entries for this interface. The ipif
13133 	 * has to be quiesced, so we walk all the nce's and delete those
13134 	 * that point at the ipif->ipif_ill. At the same time, we also
13135 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13136 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13137 	 * that for ipif_down_tail()
13138 	 */
13139 	ipif_nce_down(ipif);
13140 
13141 	/*
13142 	 * If this is the last ipif on the ill, we also need to remove
13143 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13144 	 * never succeed.
13145 	 */
13146 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13147 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13148 
13149 	/*
13150 	 * Walk all CONNs that can have a reference on an ire for this
13151 	 * ipif (we actually walk all that now have stale references).
13152 	 */
13153 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13154 
13155 	/*
13156 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13157 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13158 	 * and ill_delete -> ipif_free -> ipif_down
13159 	 */
13160 	if (mp == NULL) {
13161 		ASSERT(q == NULL);
13162 		return (0);
13163 	}
13164 
13165 	if (CONN_Q(q)) {
13166 		connp = Q_TO_CONN(q);
13167 		mutex_enter(&connp->conn_lock);
13168 	} else {
13169 		connp = NULL;
13170 	}
13171 	mutex_enter(&ill->ill_lock);
13172 	/*
13173 	 * Are there any ire's pointing to this ipif that are still active ?
13174 	 * If this is the last ipif going down, are there any ire's pointing
13175 	 * to this ill that are still active ?
13176 	 */
13177 	if (ipif_is_quiescent(ipif)) {
13178 		mutex_exit(&ill->ill_lock);
13179 		if (connp != NULL)
13180 			mutex_exit(&connp->conn_lock);
13181 		return (0);
13182 	}
13183 
13184 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13185 	    ill->ill_name, (void *)ill));
13186 	/*
13187 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13188 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13189 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13190 	 */
13191 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13192 	if (!success) {
13193 		/* The conn is closing. So just return */
13194 		ASSERT(connp != NULL);
13195 		mutex_exit(&ill->ill_lock);
13196 		mutex_exit(&connp->conn_lock);
13197 		return (EINTR);
13198 	}
13199 
13200 	mutex_exit(&ill->ill_lock);
13201 	if (connp != NULL)
13202 		mutex_exit(&connp->conn_lock);
13203 	return (EINPROGRESS);
13204 }
13205 
13206 int
13207 ipif_down_tail(ipif_t *ipif)
13208 {
13209 	ill_t	*ill = ipif->ipif_ill;
13210 	int	err = 0;
13211 
13212 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13213 	    ill_t *, ill, ipif_t *, ipif);
13214 
13215 	/*
13216 	 * Skip any loopback interface (null wq).
13217 	 * If this is the last logical interface on the ill
13218 	 * have ill_dl_down tell the driver we are gone (unbind)
13219 	 * Note that lun 0 can ipif_down even though
13220 	 * there are other logical units that are up.
13221 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13222 	 */
13223 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13224 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13225 	    ill->ill_dl_up) {
13226 		ill_dl_down(ill);
13227 	}
13228 	if (!ipif->ipif_isv6)
13229 		err = ipif_arp_down(ipif);
13230 
13231 	ill->ill_logical_down = 0;
13232 
13233 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13234 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13235 	return (err);
13236 }
13237 
13238 /*
13239  * Bring interface logically down without bringing the physical interface
13240  * down e.g. when the netmask is changed. This avoids long lasting link
13241  * negotiations between an ethernet interface and a certain switches.
13242  */
13243 static int
13244 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13245 {
13246 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13247 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13248 
13249 	/*
13250 	 * The ill_logical_down flag is a transient flag. It is set here
13251 	 * and is cleared once the down has completed in ipif_down_tail.
13252 	 * This flag does not indicate whether the ill stream is in the
13253 	 * DL_BOUND state with the driver. Instead this flag is used by
13254 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13255 	 * the driver. The state of the ill stream i.e. whether it is
13256 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13257 	 */
13258 	ipif->ipif_ill->ill_logical_down = 1;
13259 	return (ipif_down(ipif, q, mp));
13260 }
13261 
13262 /*
13263  * Initiate deallocate of an IPIF. Always called as writer. Called by
13264  * ill_delete or ip_sioctl_removeif.
13265  */
13266 static void
13267 ipif_free(ipif_t *ipif)
13268 {
13269 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13270 
13271 	ASSERT(IAM_WRITER_IPIF(ipif));
13272 
13273 	if (ipif->ipif_recovery_id != 0)
13274 		(void) untimeout(ipif->ipif_recovery_id);
13275 	ipif->ipif_recovery_id = 0;
13276 
13277 	/*
13278 	 * Take down the interface. We can be called either from ill_delete
13279 	 * or from ip_sioctl_removeif.
13280 	 */
13281 	(void) ipif_down(ipif, NULL, NULL);
13282 
13283 	/*
13284 	 * Now that the interface is down, there's no chance it can still
13285 	 * become a duplicate.  Cancel any timer that may have been set while
13286 	 * tearing down.
13287 	 */
13288 	if (ipif->ipif_recovery_id != 0)
13289 		(void) untimeout(ipif->ipif_recovery_id);
13290 	ipif->ipif_recovery_id = 0;
13291 
13292 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13293 	/* Remove pointers to this ill in the multicast routing tables */
13294 	reset_mrt_vif_ipif(ipif);
13295 	/* If necessary, clear the cached source ipif rotor. */
13296 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13297 		ipif->ipif_ill->ill_src_ipif = NULL;
13298 	rw_exit(&ipst->ips_ill_g_lock);
13299 }
13300 
13301 static void
13302 ipif_free_tail(ipif_t *ipif)
13303 {
13304 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13305 
13306 	/*
13307 	 * Need to hold both ill_g_lock and ill_lock while
13308 	 * inserting or removing an ipif from the linked list
13309 	 * of ipifs hanging off the ill.
13310 	 */
13311 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13312 
13313 #ifdef DEBUG
13314 	ipif_trace_cleanup(ipif);
13315 #endif
13316 
13317 	/* Ask SCTP to take it out of it list */
13318 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13319 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13320 
13321 	/* Get it out of the ILL interface list. */
13322 	ipif_remove(ipif);
13323 	rw_exit(&ipst->ips_ill_g_lock);
13324 
13325 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13326 	ASSERT(ipif->ipif_recovery_id == 0);
13327 	ASSERT(ipif->ipif_ire_local == NULL);
13328 	ASSERT(ipif->ipif_ire_if == NULL);
13329 
13330 	/* Free the memory. */
13331 	mi_free(ipif);
13332 }
13333 
13334 /*
13335  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13336  * is zero.
13337  */
13338 void
13339 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13340 {
13341 	char	lbuf[LIFNAMSIZ];
13342 	char	*name;
13343 	size_t	name_len;
13344 
13345 	buf[0] = '\0';
13346 	name = ipif->ipif_ill->ill_name;
13347 	name_len = ipif->ipif_ill->ill_name_length;
13348 	if (ipif->ipif_id != 0) {
13349 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13350 		    ipif->ipif_id);
13351 		name = lbuf;
13352 		name_len = mi_strlen(name) + 1;
13353 	}
13354 	len -= 1;
13355 	buf[len] = '\0';
13356 	len = MIN(len, name_len);
13357 	bcopy(name, buf, len);
13358 }
13359 
13360 /*
13361  * Sets `buf' to an ill name.
13362  */
13363 void
13364 ill_get_name(const ill_t *ill, char *buf, int len)
13365 {
13366 	char	*name;
13367 	size_t	name_len;
13368 
13369 	name = ill->ill_name;
13370 	name_len = ill->ill_name_length;
13371 	len -= 1;
13372 	buf[len] = '\0';
13373 	len = MIN(len, name_len);
13374 	bcopy(name, buf, len);
13375 }
13376 
13377 /*
13378  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13379  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13380  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13381  * (May be called as writer.)
13382  */
13383 static ipif_t *
13384 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13385     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13386 {
13387 	char	*cp;
13388 	char	*endp;
13389 	long	id;
13390 	ill_t	*ill;
13391 	ipif_t	*ipif;
13392 	uint_t	ire_type;
13393 	boolean_t did_alloc = B_FALSE;
13394 
13395 	/*
13396 	 * If the caller wants to us to create the ipif, make sure we have a
13397 	 * valid zoneid
13398 	 */
13399 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13400 
13401 	if (namelen == 0) {
13402 		return (NULL);
13403 	}
13404 
13405 	*exists = B_FALSE;
13406 	/* Look for a colon in the name. */
13407 	endp = &name[namelen];
13408 	for (cp = endp; --cp > name; ) {
13409 		if (*cp == IPIF_SEPARATOR_CHAR)
13410 			break;
13411 	}
13412 
13413 	if (*cp == IPIF_SEPARATOR_CHAR) {
13414 		/*
13415 		 * Reject any non-decimal aliases for logical
13416 		 * interfaces. Aliases with leading zeroes
13417 		 * are also rejected as they introduce ambiguity
13418 		 * in the naming of the interfaces.
13419 		 * In order to confirm with existing semantics,
13420 		 * and to not break any programs/script relying
13421 		 * on that behaviour, if<0>:0 is considered to be
13422 		 * a valid interface.
13423 		 *
13424 		 * If alias has two or more digits and the first
13425 		 * is zero, fail.
13426 		 */
13427 		if (&cp[2] < endp && cp[1] == '0') {
13428 			return (NULL);
13429 		}
13430 	}
13431 
13432 	if (cp <= name) {
13433 		cp = endp;
13434 	} else {
13435 		*cp = '\0';
13436 	}
13437 
13438 	/*
13439 	 * Look up the ILL, based on the portion of the name
13440 	 * before the slash. ill_lookup_on_name returns a held ill.
13441 	 * Temporary to check whether ill exists already. If so
13442 	 * ill_lookup_on_name will clear it.
13443 	 */
13444 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13445 	    &did_alloc, ipst);
13446 	if (cp != endp)
13447 		*cp = IPIF_SEPARATOR_CHAR;
13448 	if (ill == NULL)
13449 		return (NULL);
13450 
13451 	/* Establish the unit number in the name. */
13452 	id = 0;
13453 	if (cp < endp && *endp == '\0') {
13454 		/* If there was a colon, the unit number follows. */
13455 		cp++;
13456 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13457 			ill_refrele(ill);
13458 			return (NULL);
13459 		}
13460 	}
13461 
13462 	mutex_enter(&ill->ill_lock);
13463 	/* Now see if there is an IPIF with this unit number. */
13464 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13465 		if (ipif->ipif_id == id) {
13466 			if (zoneid != ALL_ZONES &&
13467 			    zoneid != ipif->ipif_zoneid &&
13468 			    ipif->ipif_zoneid != ALL_ZONES) {
13469 				mutex_exit(&ill->ill_lock);
13470 				ill_refrele(ill);
13471 				return (NULL);
13472 			}
13473 			if (IPIF_CAN_LOOKUP(ipif)) {
13474 				ipif_refhold_locked(ipif);
13475 				mutex_exit(&ill->ill_lock);
13476 				if (!did_alloc)
13477 					*exists = B_TRUE;
13478 				/*
13479 				 * Drop locks before calling ill_refrele
13480 				 * since it can potentially call into
13481 				 * ipif_ill_refrele_tail which can end up
13482 				 * in trying to acquire any lock.
13483 				 */
13484 				ill_refrele(ill);
13485 				return (ipif);
13486 			}
13487 		}
13488 	}
13489 
13490 	if (!do_alloc) {
13491 		mutex_exit(&ill->ill_lock);
13492 		ill_refrele(ill);
13493 		return (NULL);
13494 	}
13495 
13496 	/*
13497 	 * If none found, atomically allocate and return a new one.
13498 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13499 	 * to support "receive only" use of lo0:1 etc. as is still done
13500 	 * below as an initial guess.
13501 	 * However, this is now likely to be overriden later in ipif_up_done()
13502 	 * when we know for sure what address has been configured on the
13503 	 * interface, since we might have more than one loopback interface
13504 	 * with a loopback address, e.g. in the case of zones, and all the
13505 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13506 	 */
13507 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13508 		ire_type = IRE_LOOPBACK;
13509 	else
13510 		ire_type = IRE_LOCAL;
13511 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13512 	if (ipif != NULL)
13513 		ipif_refhold_locked(ipif);
13514 	mutex_exit(&ill->ill_lock);
13515 	ill_refrele(ill);
13516 	return (ipif);
13517 }
13518 
13519 /*
13520  * This routine is called whenever a new address comes up on an ipif.  If
13521  * we are configured to respond to address mask requests, then we are supposed
13522  * to broadcast an address mask reply at this time.  This routine is also
13523  * called if we are already up, but a netmask change is made.  This is legal
13524  * but might not make the system manager very popular.	(May be called
13525  * as writer.)
13526  */
13527 void
13528 ipif_mask_reply(ipif_t *ipif)
13529 {
13530 	icmph_t	*icmph;
13531 	ipha_t	*ipha;
13532 	mblk_t	*mp;
13533 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13534 	ip_xmit_attr_t ixas;
13535 
13536 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13537 
13538 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13539 		return;
13540 
13541 	/* ICMP mask reply is IPv4 only */
13542 	ASSERT(!ipif->ipif_isv6);
13543 	/* ICMP mask reply is not for a loopback interface */
13544 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13545 
13546 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13547 		return;
13548 
13549 	mp = allocb(REPLY_LEN, BPRI_HI);
13550 	if (mp == NULL)
13551 		return;
13552 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13553 
13554 	ipha = (ipha_t *)mp->b_rptr;
13555 	bzero(ipha, REPLY_LEN);
13556 	*ipha = icmp_ipha;
13557 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13558 	ipha->ipha_src = ipif->ipif_lcl_addr;
13559 	ipha->ipha_dst = ipif->ipif_brd_addr;
13560 	ipha->ipha_length = htons(REPLY_LEN);
13561 	ipha->ipha_ident = 0;
13562 
13563 	icmph = (icmph_t *)&ipha[1];
13564 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13565 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13566 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13567 
13568 	bzero(&ixas, sizeof (ixas));
13569 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13570 	ixas.ixa_zoneid = ALL_ZONES;
13571 	ixas.ixa_ifindex = 0;
13572 	ixas.ixa_ipst = ipst;
13573 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13574 	(void) ip_output_simple(mp, &ixas);
13575 	ixa_cleanup(&ixas);
13576 #undef	REPLY_LEN
13577 }
13578 
13579 /*
13580  * Join the ipif specific multicast groups.
13581  * Must be called after a mapping has been set up in the resolver.  (Always
13582  * called as writer.)
13583  */
13584 void
13585 ipif_multicast_up(ipif_t *ipif)
13586 {
13587 	int err;
13588 	ill_t *ill;
13589 	ilm_t *ilm;
13590 
13591 	ASSERT(IAM_WRITER_IPIF(ipif));
13592 
13593 	ill = ipif->ipif_ill;
13594 
13595 	ip1dbg(("ipif_multicast_up\n"));
13596 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13597 	    ipif->ipif_allhosts_ilm != NULL)
13598 		return;
13599 
13600 	if (ipif->ipif_isv6) {
13601 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13602 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13603 
13604 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13605 
13606 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13607 			return;
13608 
13609 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13610 
13611 		/*
13612 		 * Join the all hosts multicast address.  We skip this for
13613 		 * underlying IPMP interfaces since they should be invisible.
13614 		 */
13615 		if (!IS_UNDER_IPMP(ill)) {
13616 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13617 			    &err);
13618 			if (ilm == NULL) {
13619 				ASSERT(err != 0);
13620 				ip0dbg(("ipif_multicast_up: "
13621 				    "all_hosts_mcast failed %d\n", err));
13622 				return;
13623 			}
13624 			ipif->ipif_allhosts_ilm = ilm;
13625 		}
13626 
13627 		/*
13628 		 * Enable multicast for the solicited node multicast address.
13629 		 * If IPMP we need to put the membership on the upper ill.
13630 		 */
13631 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13632 			ill_t *mcast_ill = NULL;
13633 			boolean_t need_refrele;
13634 
13635 			if (IS_UNDER_IPMP(ill) &&
13636 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13637 				need_refrele = B_TRUE;
13638 			} else {
13639 				mcast_ill = ill;
13640 				need_refrele = B_FALSE;
13641 			}
13642 
13643 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13644 			    ipif->ipif_zoneid, &err);
13645 			if (need_refrele)
13646 				ill_refrele(mcast_ill);
13647 
13648 			if (ilm == NULL) {
13649 				ASSERT(err != 0);
13650 				ip0dbg(("ipif_multicast_up: solicited MC"
13651 				    " failed %d\n", err));
13652 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13653 					ipif->ipif_allhosts_ilm = NULL;
13654 					(void) ip_delmulti(ilm);
13655 				}
13656 				return;
13657 			}
13658 			ipif->ipif_solmulti_ilm = ilm;
13659 		}
13660 	} else {
13661 		in6_addr_t v6group;
13662 
13663 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13664 			return;
13665 
13666 		/* Join the all hosts multicast address */
13667 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13668 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13669 
13670 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13671 		if (ilm == NULL) {
13672 			ASSERT(err != 0);
13673 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13674 			return;
13675 		}
13676 		ipif->ipif_allhosts_ilm = ilm;
13677 	}
13678 }
13679 
13680 /*
13681  * Blow away any multicast groups that we joined in ipif_multicast_up().
13682  * (ilms from explicit memberships are handled in conn_update_ill.)
13683  */
13684 void
13685 ipif_multicast_down(ipif_t *ipif)
13686 {
13687 	ASSERT(IAM_WRITER_IPIF(ipif));
13688 
13689 	ip1dbg(("ipif_multicast_down\n"));
13690 
13691 	if (ipif->ipif_allhosts_ilm != NULL) {
13692 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13693 		ipif->ipif_allhosts_ilm = NULL;
13694 	}
13695 	if (ipif->ipif_solmulti_ilm != NULL) {
13696 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13697 		ipif->ipif_solmulti_ilm = NULL;
13698 	}
13699 }
13700 
13701 /*
13702  * Used when an interface comes up to recreate any extra routes on this
13703  * interface.
13704  */
13705 int
13706 ill_recover_saved_ire(ill_t *ill)
13707 {
13708 	mblk_t		*mp;
13709 	ip_stack_t	*ipst = ill->ill_ipst;
13710 
13711 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13712 
13713 	mutex_enter(&ill->ill_saved_ire_lock);
13714 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13715 		ire_t		*ire, *nire;
13716 		ifrt_t		*ifrt;
13717 
13718 		ifrt = (ifrt_t *)mp->b_rptr;
13719 		/*
13720 		 * Create a copy of the IRE with the saved address and netmask.
13721 		 */
13722 		if (ill->ill_isv6) {
13723 			ire = ire_create_v6(
13724 			    &ifrt->ifrt_v6addr,
13725 			    &ifrt->ifrt_v6mask,
13726 			    &ifrt->ifrt_v6gateway_addr,
13727 			    ifrt->ifrt_type,
13728 			    ill,
13729 			    ifrt->ifrt_zoneid,
13730 			    ifrt->ifrt_flags,
13731 			    NULL,
13732 			    ipst);
13733 		} else {
13734 			ire = ire_create(
13735 			    (uint8_t *)&ifrt->ifrt_addr,
13736 			    (uint8_t *)&ifrt->ifrt_mask,
13737 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13738 			    ifrt->ifrt_type,
13739 			    ill,
13740 			    ifrt->ifrt_zoneid,
13741 			    ifrt->ifrt_flags,
13742 			    NULL,
13743 			    ipst);
13744 		}
13745 		if (ire == NULL) {
13746 			mutex_exit(&ill->ill_saved_ire_lock);
13747 			return (ENOMEM);
13748 		}
13749 
13750 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13751 			if (ill->ill_isv6) {
13752 				ire->ire_setsrc_addr_v6 =
13753 				    ifrt->ifrt_v6setsrc_addr;
13754 			} else {
13755 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13756 			}
13757 		}
13758 
13759 		/*
13760 		 * Some software (for example, GateD and Sun Cluster) attempts
13761 		 * to create (what amount to) IRE_PREFIX routes with the
13762 		 * loopback address as the gateway.  This is primarily done to
13763 		 * set up prefixes with the RTF_REJECT flag set (for example,
13764 		 * when generating aggregate routes.)
13765 		 *
13766 		 * If the IRE type (as defined by ill->ill_net_type) is
13767 		 * IRE_LOOPBACK, then we map the request into a
13768 		 * IRE_IF_NORESOLVER.
13769 		 */
13770 		if (ill->ill_net_type == IRE_LOOPBACK)
13771 			ire->ire_type = IRE_IF_NORESOLVER;
13772 
13773 		/*
13774 		 * ire held by ire_add, will be refreled' towards the
13775 		 * the end of ipif_up_done
13776 		 */
13777 		nire = ire_add(ire);
13778 		/*
13779 		 * Check if it was a duplicate entry. This handles
13780 		 * the case of two racing route adds for the same route
13781 		 */
13782 		if (nire == NULL) {
13783 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13784 		} else if (nire != ire) {
13785 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13786 			    (void *)nire));
13787 			ire_delete(nire);
13788 		} else {
13789 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13790 			    (void *)nire));
13791 		}
13792 		if (nire != NULL)
13793 			ire_refrele(nire);
13794 	}
13795 	mutex_exit(&ill->ill_saved_ire_lock);
13796 	return (0);
13797 }
13798 
13799 /*
13800  * Used to set the netmask and broadcast address to default values when the
13801  * interface is brought up.  (Always called as writer.)
13802  */
13803 static void
13804 ipif_set_default(ipif_t *ipif)
13805 {
13806 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13807 
13808 	if (!ipif->ipif_isv6) {
13809 		/*
13810 		 * Interface holds an IPv4 address. Default
13811 		 * mask is the natural netmask.
13812 		 */
13813 		if (!ipif->ipif_net_mask) {
13814 			ipaddr_t	v4mask;
13815 
13816 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13817 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13818 		}
13819 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13820 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13821 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13822 		} else {
13823 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13824 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13825 		}
13826 		/*
13827 		 * NOTE: SunOS 4.X does this even if the broadcast address
13828 		 * has been already set thus we do the same here.
13829 		 */
13830 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13831 			ipaddr_t	v4addr;
13832 
13833 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13834 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13835 		}
13836 	} else {
13837 		/*
13838 		 * Interface holds an IPv6-only address.  Default
13839 		 * mask is all-ones.
13840 		 */
13841 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13842 			ipif->ipif_v6net_mask = ipv6_all_ones;
13843 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13844 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13845 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13846 		} else {
13847 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13848 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13849 		}
13850 	}
13851 }
13852 
13853 /*
13854  * Return 0 if this address can be used as local address without causing
13855  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13856  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13857  * Note that the same IPv6 link-local address is allowed as long as the ills
13858  * are not on the same link.
13859  */
13860 int
13861 ip_addr_availability_check(ipif_t *new_ipif)
13862 {
13863 	in6_addr_t our_v6addr;
13864 	ill_t *ill;
13865 	ipif_t *ipif;
13866 	ill_walk_context_t ctx;
13867 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13868 
13869 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13870 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13871 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13872 
13873 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13874 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13875 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13876 		return (0);
13877 
13878 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13879 
13880 	if (new_ipif->ipif_isv6)
13881 		ill = ILL_START_WALK_V6(&ctx, ipst);
13882 	else
13883 		ill = ILL_START_WALK_V4(&ctx, ipst);
13884 
13885 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13886 		for (ipif = ill->ill_ipif; ipif != NULL;
13887 		    ipif = ipif->ipif_next) {
13888 			if ((ipif == new_ipif) ||
13889 			    !(ipif->ipif_flags & IPIF_UP) ||
13890 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13891 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13892 			    &our_v6addr))
13893 				continue;
13894 
13895 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13896 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13897 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13898 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13899 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13900 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13901 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13902 				continue;
13903 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13904 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13905 				continue;
13906 			else if (new_ipif->ipif_ill == ill)
13907 				return (EADDRINUSE);
13908 			else
13909 				return (EADDRNOTAVAIL);
13910 		}
13911 	}
13912 
13913 	return (0);
13914 }
13915 
13916 /*
13917  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13918  * IREs for the ipif.
13919  * When the routine returns EINPROGRESS then mp has been consumed and
13920  * the ioctl will be acked from ip_rput_dlpi.
13921  */
13922 int
13923 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13924 {
13925 	ill_t		*ill = ipif->ipif_ill;
13926 	boolean_t 	isv6 = ipif->ipif_isv6;
13927 	int		err = 0;
13928 	boolean_t	success;
13929 	uint_t		ipif_orig_id;
13930 	ip_stack_t	*ipst = ill->ill_ipst;
13931 
13932 	ASSERT(IAM_WRITER_IPIF(ipif));
13933 
13934 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13935 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13936 	    ill_t *, ill, ipif_t *, ipif);
13937 
13938 	/* Shouldn't get here if it is already up. */
13939 	if (ipif->ipif_flags & IPIF_UP)
13940 		return (EALREADY);
13941 
13942 	/*
13943 	 * If this is a request to bring up a data address on an interface
13944 	 * under IPMP, then move the address to its IPMP meta-interface and
13945 	 * try to bring it up.  One complication is that the zeroth ipif for
13946 	 * an ill is special, in that every ill always has one, and that code
13947 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13948 	 */
13949 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13950 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13951 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13952 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13953 
13954 		/*
13955 		 * The ipif being brought up should be quiesced.  If it's not,
13956 		 * something has gone amiss and we need to bail out.  (If it's
13957 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13958 		 */
13959 		mutex_enter(&ill->ill_lock);
13960 		if (!ipif_is_quiescent(ipif)) {
13961 			mutex_exit(&ill->ill_lock);
13962 			return (EINVAL);
13963 		}
13964 		mutex_exit(&ill->ill_lock);
13965 
13966 		/*
13967 		 * If we're going to need to allocate ipifs, do it prior
13968 		 * to starting the move (and grabbing locks).
13969 		 */
13970 		if (ipif->ipif_id == 0) {
13971 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13972 			    B_FALSE, &err)) == NULL) {
13973 				return (err);
13974 			}
13975 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13976 			    B_FALSE, &err)) == NULL) {
13977 				mi_free(moveipif);
13978 				return (err);
13979 			}
13980 		}
13981 
13982 		/*
13983 		 * Grab or transfer the ipif to move.  During the move, keep
13984 		 * ill_g_lock held to prevent any ill walker threads from
13985 		 * seeing things in an inconsistent state.
13986 		 */
13987 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13988 		if (ipif->ipif_id != 0) {
13989 			ipif_remove(ipif);
13990 		} else {
13991 			ipif_transfer(ipif, moveipif, stubipif);
13992 			ipif = moveipif;
13993 		}
13994 
13995 		/*
13996 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13997 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13998 		 * replace that one.  Otherwise, pick the next available slot.
13999 		 */
14000 		ipif->ipif_ill = ipmp_ill;
14001 		ipif_orig_id = ipif->ipif_id;
14002 
14003 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14004 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14005 			ipif = ipmp_ill->ill_ipif;
14006 		} else {
14007 			ipif->ipif_id = -1;
14008 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14009 				/*
14010 				 * No more available ipif_id's -- put it back
14011 				 * on the original ill and fail the operation.
14012 				 * Since we're writer on the ill, we can be
14013 				 * sure our old slot is still available.
14014 				 */
14015 				ipif->ipif_id = ipif_orig_id;
14016 				ipif->ipif_ill = ill;
14017 				if (ipif_orig_id == 0) {
14018 					ipif_transfer(ipif, ill->ill_ipif,
14019 					    NULL);
14020 				} else {
14021 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14022 				}
14023 				rw_exit(&ipst->ips_ill_g_lock);
14024 				return (err);
14025 			}
14026 		}
14027 		rw_exit(&ipst->ips_ill_g_lock);
14028 
14029 		/*
14030 		 * Tell SCTP that the ipif has moved.  Note that even if we
14031 		 * had to allocate a new ipif, the original sequence id was
14032 		 * preserved and therefore SCTP won't know.
14033 		 */
14034 		sctp_move_ipif(ipif, ill, ipmp_ill);
14035 
14036 		/*
14037 		 * If the ipif being brought up was on slot zero, then we
14038 		 * first need to bring up the placeholder we stuck there.  In
14039 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14040 		 * call to ipif_up() itself, if we successfully bring up the
14041 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14042 		 */
14043 		if (ipif_orig_id == 0) {
14044 			ASSERT(ill->ill_move_ipif == NULL);
14045 			ill->ill_move_ipif = ipif;
14046 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14047 				ASSERT(ill->ill_move_ipif == NULL);
14048 			if (err != EINPROGRESS)
14049 				ill->ill_move_ipif = NULL;
14050 			return (err);
14051 		}
14052 
14053 		/*
14054 		 * Bring it up on the IPMP ill.
14055 		 */
14056 		return (ipif_up(ipif, q, mp));
14057 	}
14058 
14059 	/* Skip arp/ndp for any loopback interface. */
14060 	if (ill->ill_wq != NULL) {
14061 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14062 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14063 
14064 		if (!ill->ill_dl_up) {
14065 			/*
14066 			 * ill_dl_up is not yet set. i.e. we are yet to
14067 			 * DL_BIND with the driver and this is the first
14068 			 * logical interface on the ill to become "up".
14069 			 * Tell the driver to get going (via DL_BIND_REQ).
14070 			 * Note that changing "significant" IFF_ flags
14071 			 * address/netmask etc cause a down/up dance, but
14072 			 * does not cause an unbind (DL_UNBIND) with the driver
14073 			 */
14074 			return (ill_dl_up(ill, ipif, mp, q));
14075 		}
14076 
14077 		/*
14078 		 * ipif_resolver_up may end up needeing to bind/attach
14079 		 * the ARP stream, which in turn necessitates a
14080 		 * DLPI message exchange with the driver. ioctls are
14081 		 * serialized and so we cannot send more than one
14082 		 * interface up message at a time. If ipif_resolver_up
14083 		 * does need to wait for the DLPI handshake for the ARP stream,
14084 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14085 		 */
14086 
14087 		ASSERT(connp != NULL || !CONN_Q(q));
14088 		if (connp != NULL)
14089 			mutex_enter(&connp->conn_lock);
14090 		mutex_enter(&ill->ill_lock);
14091 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14092 		mutex_exit(&ill->ill_lock);
14093 		if (connp != NULL)
14094 			mutex_exit(&connp->conn_lock);
14095 		if (!success)
14096 			return (EINTR);
14097 
14098 		/*
14099 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14100 		 * complete when ipif_ndp_up returns.
14101 		 */
14102 		err = ipif_resolver_up(ipif, Res_act_initial);
14103 		if (err == EINPROGRESS) {
14104 			/* We will complete it in arp_bringup_done() */
14105 			return (err);
14106 		}
14107 
14108 		if (isv6 && err == 0)
14109 			err = ipif_ndp_up(ipif, B_TRUE);
14110 
14111 		ASSERT(err != EINPROGRESS);
14112 		mp = ipsq_pending_mp_get(ipsq, &connp);
14113 		ASSERT(mp != NULL);
14114 		if (err != 0)
14115 			return (err);
14116 	} else {
14117 		/*
14118 		 * Interfaces without underlying hardware don't do duplicate
14119 		 * address detection.
14120 		 */
14121 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14122 		ipif->ipif_addr_ready = 1;
14123 		err = ill_add_ires(ill);
14124 		/* allocation failure? */
14125 		if (err != 0)
14126 			return (err);
14127 	}
14128 
14129 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14130 	if (err == 0 && ill->ill_move_ipif != NULL) {
14131 		ipif = ill->ill_move_ipif;
14132 		ill->ill_move_ipif = NULL;
14133 		return (ipif_up(ipif, q, mp));
14134 	}
14135 	return (err);
14136 }
14137 
14138 /*
14139  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14140  * The identical set of IREs need to be removed in ill_delete_ires().
14141  */
14142 int
14143 ill_add_ires(ill_t *ill)
14144 {
14145 	ire_t	*ire;
14146 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14147 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14148 
14149 	if (ill->ill_ire_multicast != NULL)
14150 		return (0);
14151 
14152 	/*
14153 	 * provide some dummy ire_addr for creating the ire.
14154 	 */
14155 	if (ill->ill_isv6) {
14156 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14157 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14158 	} else {
14159 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14160 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14161 	}
14162 	if (ire == NULL)
14163 		return (ENOMEM);
14164 
14165 	ill->ill_ire_multicast = ire;
14166 	return (0);
14167 }
14168 
14169 void
14170 ill_delete_ires(ill_t *ill)
14171 {
14172 	if (ill->ill_ire_multicast != NULL) {
14173 		/*
14174 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14175 		 * which was taken without any th_tracing enabled.
14176 		 * We also mark it as condemned (note that it was never added)
14177 		 * so that caching conn's can move off of it.
14178 		 */
14179 		ire_make_condemned(ill->ill_ire_multicast);
14180 		ire_refrele_notr(ill->ill_ire_multicast);
14181 		ill->ill_ire_multicast = NULL;
14182 	}
14183 }
14184 
14185 /*
14186  * Perform a bind for the physical device.
14187  * When the routine returns EINPROGRESS then mp has been consumed and
14188  * the ioctl will be acked from ip_rput_dlpi.
14189  * Allocate an unbind message and save it until ipif_down.
14190  */
14191 static int
14192 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14193 {
14194 	mblk_t	*bind_mp = NULL;
14195 	mblk_t	*unbind_mp = NULL;
14196 	conn_t	*connp;
14197 	boolean_t success;
14198 	int	err;
14199 
14200 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14201 
14202 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14203 	ASSERT(IAM_WRITER_ILL(ill));
14204 	ASSERT(mp != NULL);
14205 
14206 	/*
14207 	 * Make sure we have an IRE_MULTICAST in case we immediately
14208 	 * start receiving packets.
14209 	 */
14210 	err = ill_add_ires(ill);
14211 	if (err != 0)
14212 		goto bad;
14213 
14214 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14215 	    DL_BIND_REQ);
14216 	if (bind_mp == NULL)
14217 		goto bad;
14218 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14219 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14220 
14221 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14222 	if (unbind_mp == NULL)
14223 		goto bad;
14224 
14225 	/*
14226 	 * Record state needed to complete this operation when the
14227 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14228 	 */
14229 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14230 	ASSERT(connp != NULL || !CONN_Q(q));
14231 	GRAB_CONN_LOCK(q);
14232 	mutex_enter(&ipif->ipif_ill->ill_lock);
14233 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14234 	mutex_exit(&ipif->ipif_ill->ill_lock);
14235 	RELEASE_CONN_LOCK(q);
14236 	if (!success)
14237 		goto bad;
14238 
14239 	/*
14240 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14241 	 * the interface goes down.
14242 	 */
14243 	ASSERT(ill->ill_unbind_mp == NULL);
14244 	ill->ill_unbind_mp = unbind_mp;
14245 
14246 	ill_dlpi_send(ill, bind_mp);
14247 	/* Send down link-layer capabilities probe if not already done. */
14248 	ill_capability_probe(ill);
14249 
14250 	/*
14251 	 * Sysid used to rely on the fact that netboots set domainname
14252 	 * and the like. Now that miniroot boots aren't strictly netboots
14253 	 * and miniroot network configuration is driven from userland
14254 	 * these things still need to be set. This situation can be detected
14255 	 * by comparing the interface being configured here to the one
14256 	 * dhcifname was set to reference by the boot loader. Once sysid is
14257 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14258 	 */
14259 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14260 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14261 	    (strlen(srpc_domain) == 0)) {
14262 		if (dhcpinit() != 0)
14263 			cmn_err(CE_WARN, "no cached dhcp response");
14264 	}
14265 
14266 	/*
14267 	 * This operation will complete in ip_rput_dlpi with either
14268 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14269 	 */
14270 	return (EINPROGRESS);
14271 bad:
14272 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14273 
14274 	freemsg(bind_mp);
14275 	freemsg(unbind_mp);
14276 	return (ENOMEM);
14277 }
14278 
14279 /* Add room for tcp+ip headers */
14280 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14281 
14282 /*
14283  * DLPI and ARP is up.
14284  * Create all the IREs associated with an interface. Bring up multicast.
14285  * Set the interface flag and finish other initialization
14286  * that potentially had to be deferred to after DL_BIND_ACK.
14287  */
14288 int
14289 ipif_up_done(ipif_t *ipif)
14290 {
14291 	ill_t		*ill = ipif->ipif_ill;
14292 	int		err = 0;
14293 	boolean_t	loopback = B_FALSE;
14294 	boolean_t	update_src_selection = B_TRUE;
14295 	ipif_t		*tmp_ipif;
14296 
14297 	ip1dbg(("ipif_up_done(%s:%u)\n",
14298 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14299 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14300 	    ill_t *, ill, ipif_t *, ipif);
14301 
14302 	/* Check if this is a loopback interface */
14303 	if (ipif->ipif_ill->ill_wq == NULL)
14304 		loopback = B_TRUE;
14305 
14306 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14307 
14308 	/*
14309 	 * If all other interfaces for this ill are down or DEPRECATED,
14310 	 * or otherwise unsuitable for source address selection,
14311 	 * reset the src generation numbers to make sure source
14312 	 * address selection gets to take this new ipif into account.
14313 	 * No need to hold ill_lock while traversing the ipif list since
14314 	 * we are writer
14315 	 */
14316 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14317 	    tmp_ipif = tmp_ipif->ipif_next) {
14318 		if (((tmp_ipif->ipif_flags &
14319 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14320 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14321 		    (tmp_ipif == ipif))
14322 			continue;
14323 		/* first useable pre-existing interface */
14324 		update_src_selection = B_FALSE;
14325 		break;
14326 	}
14327 	if (update_src_selection)
14328 		ip_update_source_selection(ill->ill_ipst);
14329 
14330 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14331 		nce_t *loop_nce = NULL;
14332 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14333 
14334 		/*
14335 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14336 		 * ipif_lookup_on_name(), but in the case of zones we can have
14337 		 * several loopback addresses on lo0. So all the interfaces with
14338 		 * loopback addresses need to be marked IRE_LOOPBACK.
14339 		 */
14340 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14341 		    htonl(INADDR_LOOPBACK))
14342 			ipif->ipif_ire_type = IRE_LOOPBACK;
14343 		else
14344 			ipif->ipif_ire_type = IRE_LOCAL;
14345 		if (ill->ill_net_type != IRE_LOOPBACK)
14346 			flags |= NCE_F_PUBLISH;
14347 
14348 		/* add unicast nce for the local addr */
14349 		err = nce_lookup_then_add_v4(ill, NULL,
14350 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14351 		    ND_REACHABLE, &loop_nce);
14352 		/* A shared-IP zone sees EEXIST for lo0:N */
14353 		if (err == 0 || err == EEXIST) {
14354 			ipif->ipif_added_nce = 1;
14355 			loop_nce->nce_ipif_cnt++;
14356 			nce_refrele(loop_nce);
14357 			err = 0;
14358 		} else {
14359 			ASSERT(loop_nce == NULL);
14360 			return (err);
14361 		}
14362 	}
14363 
14364 	/* Create all the IREs associated with this interface */
14365 	err = ipif_add_ires_v4(ipif, loopback);
14366 	if (err != 0) {
14367 		/*
14368 		 * see comments about return value from
14369 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14370 		 */
14371 		if (err != EADDRINUSE) {
14372 			(void) ipif_arp_down(ipif);
14373 		} else {
14374 			/*
14375 			 * Make IPMP aware of the deleted ipif so that
14376 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14377 			 * can be completed. Note that we do not want to
14378 			 * destroy the nce that was created on the ipmp_ill
14379 			 * for the active copy of the duplicate address in
14380 			 * use.
14381 			 */
14382 			if (IS_IPMP(ill))
14383 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14384 			err = EADDRNOTAVAIL;
14385 		}
14386 		return (err);
14387 	}
14388 
14389 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14390 		/* Recover any additional IREs entries for this ill */
14391 		(void) ill_recover_saved_ire(ill);
14392 	}
14393 
14394 	if (ill->ill_need_recover_multicast) {
14395 		/*
14396 		 * Need to recover all multicast memberships in the driver.
14397 		 * This had to be deferred until we had attached.  The same
14398 		 * code exists in ipif_up_done_v6() to recover IPv6
14399 		 * memberships.
14400 		 *
14401 		 * Note that it would be preferable to unconditionally do the
14402 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14403 		 * that since ill_join_allmulti() depends on ill_dl_up being
14404 		 * set, and it is not set until we receive a DL_BIND_ACK after
14405 		 * having called ill_dl_up().
14406 		 */
14407 		ill_recover_multicast(ill);
14408 	}
14409 
14410 	if (ill->ill_ipif_up_count == 1) {
14411 		/*
14412 		 * Since the interface is now up, it may now be active.
14413 		 */
14414 		if (IS_UNDER_IPMP(ill))
14415 			ipmp_ill_refresh_active(ill);
14416 
14417 		/*
14418 		 * If this is an IPMP interface, we may now be able to
14419 		 * establish ARP entries.
14420 		 */
14421 		if (IS_IPMP(ill))
14422 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14423 	}
14424 
14425 	/* Join the allhosts multicast address */
14426 	ipif_multicast_up(ipif);
14427 
14428 	if (!loopback && !update_src_selection &&
14429 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14430 		ip_update_source_selection(ill->ill_ipst);
14431 
14432 	if (!loopback && ipif->ipif_addr_ready) {
14433 		/* Broadcast an address mask reply. */
14434 		ipif_mask_reply(ipif);
14435 	}
14436 	/* Perhaps ilgs should use this ill */
14437 	update_conn_ill(NULL, ill->ill_ipst);
14438 
14439 	/*
14440 	 * This had to be deferred until we had bound.  Tell routing sockets and
14441 	 * others that this interface is up if it looks like the address has
14442 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14443 	 * duplicate address detection to do its thing.
14444 	 */
14445 	if (ipif->ipif_addr_ready)
14446 		ipif_up_notify(ipif);
14447 	return (0);
14448 }
14449 
14450 /*
14451  * Add the IREs associated with the ipif.
14452  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14453  */
14454 static int
14455 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14456 {
14457 	ill_t		*ill = ipif->ipif_ill;
14458 	ip_stack_t	*ipst = ill->ill_ipst;
14459 	ire_t		*ire_array[20];
14460 	ire_t		**irep = ire_array;
14461 	ire_t		**irep1;
14462 	ipaddr_t	net_mask = 0;
14463 	ipaddr_t	subnet_mask, route_mask;
14464 	int		err;
14465 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14466 	ire_t		*ire_if = NULL;
14467 	uchar_t		*gw;
14468 
14469 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14470 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14471 		/*
14472 		 * If we're on a labeled system then make sure that zone-
14473 		 * private addresses have proper remote host database entries.
14474 		 */
14475 		if (is_system_labeled() &&
14476 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14477 		    !tsol_check_interface_address(ipif))
14478 			return (EINVAL);
14479 
14480 		/* Register the source address for __sin6_src_id */
14481 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14482 		    ipif->ipif_zoneid, ipst);
14483 		if (err != 0) {
14484 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14485 			return (err);
14486 		}
14487 
14488 		if (loopback)
14489 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14490 		else
14491 			gw = NULL;
14492 
14493 		/* If the interface address is set, create the local IRE. */
14494 		ire_local = ire_create(
14495 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14496 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14497 		    gw,					/* gateway */
14498 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14499 		    ipif->ipif_ill,
14500 		    ipif->ipif_zoneid,
14501 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14502 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14503 		    NULL,
14504 		    ipst);
14505 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14506 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14507 		    ipif->ipif_ire_type,
14508 		    ntohl(ipif->ipif_lcl_addr)));
14509 		if (ire_local == NULL) {
14510 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14511 			err = ENOMEM;
14512 			goto bad;
14513 		}
14514 	} else {
14515 		ip1dbg((
14516 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14517 		    ipif->ipif_ire_type,
14518 		    ntohl(ipif->ipif_lcl_addr),
14519 		    (uint_t)ipif->ipif_flags));
14520 	}
14521 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14522 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14523 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14524 	} else {
14525 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14526 	}
14527 
14528 	subnet_mask = ipif->ipif_net_mask;
14529 
14530 	/*
14531 	 * If mask was not specified, use natural netmask of
14532 	 * interface address. Also, store this mask back into the
14533 	 * ipif struct.
14534 	 */
14535 	if (subnet_mask == 0) {
14536 		subnet_mask = net_mask;
14537 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14538 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14539 		    ipif->ipif_v6subnet);
14540 	}
14541 
14542 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14543 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14544 	    ipif->ipif_subnet != INADDR_ANY) {
14545 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14546 
14547 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14548 			route_mask = IP_HOST_MASK;
14549 		} else {
14550 			route_mask = subnet_mask;
14551 		}
14552 
14553 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14554 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14555 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14556 		    ntohl(ipif->ipif_subnet)));
14557 		ire_if = ire_create(
14558 		    (uchar_t *)&ipif->ipif_subnet,
14559 		    (uchar_t *)&route_mask,
14560 		    (uchar_t *)&ipif->ipif_lcl_addr,
14561 		    ill->ill_net_type,
14562 		    ill,
14563 		    ipif->ipif_zoneid,
14564 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14565 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14566 		    NULL,
14567 		    ipst);
14568 		if (ire_if == NULL) {
14569 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14570 			err = ENOMEM;
14571 			goto bad;
14572 		}
14573 	}
14574 
14575 	/*
14576 	 * Create any necessary broadcast IREs.
14577 	 */
14578 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14579 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14580 		irep = ipif_create_bcast_ires(ipif, irep);
14581 
14582 	/* If an earlier ire_create failed, get out now */
14583 	for (irep1 = irep; irep1 > ire_array; ) {
14584 		irep1--;
14585 		if (*irep1 == NULL) {
14586 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14587 			err = ENOMEM;
14588 			goto bad;
14589 		}
14590 	}
14591 
14592 	/*
14593 	 * Need to atomically check for IP address availability under
14594 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14595 	 * ills or new ipifs can be added while we are checking availability.
14596 	 */
14597 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14598 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14599 	/* Mark it up, and increment counters. */
14600 	ipif->ipif_flags |= IPIF_UP;
14601 	ill->ill_ipif_up_count++;
14602 	err = ip_addr_availability_check(ipif);
14603 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14604 	rw_exit(&ipst->ips_ill_g_lock);
14605 
14606 	if (err != 0) {
14607 		/*
14608 		 * Our address may already be up on the same ill. In this case,
14609 		 * the ARP entry for our ipif replaced the one for the other
14610 		 * ipif. So we don't want to delete it (otherwise the other ipif
14611 		 * would be unable to send packets).
14612 		 * ip_addr_availability_check() identifies this case for us and
14613 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14614 		 * which is the expected error code.
14615 		 */
14616 		ill->ill_ipif_up_count--;
14617 		ipif->ipif_flags &= ~IPIF_UP;
14618 		goto bad;
14619 	}
14620 
14621 	/*
14622 	 * Add in all newly created IREs.  ire_create_bcast() has
14623 	 * already checked for duplicates of the IRE_BROADCAST type.
14624 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14625 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14626 	 * a /32 route.
14627 	 */
14628 	if (ire_if != NULL) {
14629 		ire_if = ire_add(ire_if);
14630 		if (ire_if == NULL) {
14631 			err = ENOMEM;
14632 			goto bad2;
14633 		}
14634 #ifdef DEBUG
14635 		ire_refhold_notr(ire_if);
14636 		ire_refrele(ire_if);
14637 #endif
14638 	}
14639 	if (ire_local != NULL) {
14640 		ire_local = ire_add(ire_local);
14641 		if (ire_local == NULL) {
14642 			err = ENOMEM;
14643 			goto bad2;
14644 		}
14645 #ifdef DEBUG
14646 		ire_refhold_notr(ire_local);
14647 		ire_refrele(ire_local);
14648 #endif
14649 	}
14650 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14651 	if (ire_local != NULL)
14652 		ipif->ipif_ire_local = ire_local;
14653 	if (ire_if != NULL)
14654 		ipif->ipif_ire_if = ire_if;
14655 	rw_exit(&ipst->ips_ill_g_lock);
14656 	ire_local = NULL;
14657 	ire_if = NULL;
14658 
14659 	/*
14660 	 * We first add all of them, and if that succeeds we refrele the
14661 	 * bunch. That enables us to delete all of them should any of the
14662 	 * ire_adds fail.
14663 	 */
14664 	for (irep1 = irep; irep1 > ire_array; ) {
14665 		irep1--;
14666 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14667 		*irep1 = ire_add(*irep1);
14668 		if (*irep1 == NULL) {
14669 			err = ENOMEM;
14670 			goto bad2;
14671 		}
14672 	}
14673 
14674 	for (irep1 = irep; irep1 > ire_array; ) {
14675 		irep1--;
14676 		/* refheld by ire_add. */
14677 		if (*irep1 != NULL) {
14678 			ire_refrele(*irep1);
14679 			*irep1 = NULL;
14680 		}
14681 	}
14682 
14683 	if (!loopback) {
14684 		/*
14685 		 * If the broadcast address has been set, make sure it makes
14686 		 * sense based on the interface address.
14687 		 * Only match on ill since we are sharing broadcast addresses.
14688 		 */
14689 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14690 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14691 			ire_t	*ire;
14692 
14693 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14694 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14695 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14696 
14697 			if (ire == NULL) {
14698 				/*
14699 				 * If there isn't a matching broadcast IRE,
14700 				 * revert to the default for this netmask.
14701 				 */
14702 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14703 				mutex_enter(&ipif->ipif_ill->ill_lock);
14704 				ipif_set_default(ipif);
14705 				mutex_exit(&ipif->ipif_ill->ill_lock);
14706 			} else {
14707 				ire_refrele(ire);
14708 			}
14709 		}
14710 
14711 	}
14712 	return (0);
14713 
14714 bad2:
14715 	ill->ill_ipif_up_count--;
14716 	ipif->ipif_flags &= ~IPIF_UP;
14717 
14718 bad:
14719 	ip1dbg(("ipif_add_ires: FAILED \n"));
14720 	if (ire_local != NULL)
14721 		ire_delete(ire_local);
14722 	if (ire_if != NULL)
14723 		ire_delete(ire_if);
14724 
14725 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14726 	ire_local = ipif->ipif_ire_local;
14727 	ipif->ipif_ire_local = NULL;
14728 	ire_if = ipif->ipif_ire_if;
14729 	ipif->ipif_ire_if = NULL;
14730 	rw_exit(&ipst->ips_ill_g_lock);
14731 	if (ire_local != NULL) {
14732 		ire_delete(ire_local);
14733 		ire_refrele_notr(ire_local);
14734 	}
14735 	if (ire_if != NULL) {
14736 		ire_delete(ire_if);
14737 		ire_refrele_notr(ire_if);
14738 	}
14739 
14740 	while (irep > ire_array) {
14741 		irep--;
14742 		if (*irep != NULL) {
14743 			ire_delete(*irep);
14744 		}
14745 	}
14746 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14747 
14748 	return (err);
14749 }
14750 
14751 /* Remove all the IREs created by ipif_add_ires_v4 */
14752 void
14753 ipif_delete_ires_v4(ipif_t *ipif)
14754 {
14755 	ill_t		*ill = ipif->ipif_ill;
14756 	ip_stack_t	*ipst = ill->ill_ipst;
14757 	ire_t		*ire;
14758 
14759 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14760 	ire = ipif->ipif_ire_local;
14761 	ipif->ipif_ire_local = NULL;
14762 	rw_exit(&ipst->ips_ill_g_lock);
14763 	if (ire != NULL) {
14764 		/*
14765 		 * Move count to ipif so we don't loose the count due to
14766 		 * a down/up dance.
14767 		 */
14768 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14769 
14770 		ire_delete(ire);
14771 		ire_refrele_notr(ire);
14772 	}
14773 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14774 	ire = ipif->ipif_ire_if;
14775 	ipif->ipif_ire_if = NULL;
14776 	rw_exit(&ipst->ips_ill_g_lock);
14777 	if (ire != NULL) {
14778 		ire_delete(ire);
14779 		ire_refrele_notr(ire);
14780 	}
14781 
14782 	/*
14783 	 * Delete the broadcast IREs.
14784 	 */
14785 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14786 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14787 		ipif_delete_bcast_ires(ipif);
14788 }
14789 
14790 /*
14791  * Checks for availbility of a usable source address (if there is one) when the
14792  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14793  * this selection is done regardless of the destination.
14794  */
14795 boolean_t
14796 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14797     ip_stack_t *ipst)
14798 {
14799 	ipif_t		*ipif = NULL;
14800 	ill_t		*uill;
14801 
14802 	ASSERT(ifindex != 0);
14803 
14804 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14805 	if (uill == NULL)
14806 		return (B_FALSE);
14807 
14808 	mutex_enter(&uill->ill_lock);
14809 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14810 		if (IPIF_IS_CONDEMNED(ipif))
14811 			continue;
14812 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14813 			continue;
14814 		if (!(ipif->ipif_flags & IPIF_UP))
14815 			continue;
14816 		if (ipif->ipif_zoneid != zoneid)
14817 			continue;
14818 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14819 		    ipif->ipif_lcl_addr == INADDR_ANY)
14820 			continue;
14821 		mutex_exit(&uill->ill_lock);
14822 		ill_refrele(uill);
14823 		return (B_TRUE);
14824 	}
14825 	mutex_exit(&uill->ill_lock);
14826 	ill_refrele(uill);
14827 	return (B_FALSE);
14828 }
14829 
14830 /*
14831  * Find an ipif with a good local address on the ill+zoneid.
14832  */
14833 ipif_t *
14834 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14835 {
14836 	ipif_t		*ipif;
14837 
14838 	mutex_enter(&ill->ill_lock);
14839 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14840 		if (IPIF_IS_CONDEMNED(ipif))
14841 			continue;
14842 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14843 			continue;
14844 		if (!(ipif->ipif_flags & IPIF_UP))
14845 			continue;
14846 		if (ipif->ipif_zoneid != zoneid &&
14847 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14848 			continue;
14849 		if (ill->ill_isv6 ?
14850 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14851 		    ipif->ipif_lcl_addr == INADDR_ANY)
14852 			continue;
14853 		ipif_refhold_locked(ipif);
14854 		mutex_exit(&ill->ill_lock);
14855 		return (ipif);
14856 	}
14857 	mutex_exit(&ill->ill_lock);
14858 	return (NULL);
14859 }
14860 
14861 /*
14862  * IP source address type, sorted from worst to best.  For a given type,
14863  * always prefer IP addresses on the same subnet.  All-zones addresses are
14864  * suboptimal because they pose problems with unlabeled destinations.
14865  */
14866 typedef enum {
14867 	IPIF_NONE,
14868 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14869 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14870 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14871 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14872 	IPIF_DIFFNET,			/* normal and different subnet */
14873 	IPIF_SAMENET,			/* normal and same subnet */
14874 	IPIF_LOCALADDR			/* local loopback */
14875 } ipif_type_t;
14876 
14877 /*
14878  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14879  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14880  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14881  * the first one, unless IPMP is used in which case we round-robin among them;
14882  * see below for more.
14883  *
14884  * Returns NULL if there is no suitable source address for the ill.
14885  * This only occurs when there is no valid source address for the ill.
14886  */
14887 ipif_t *
14888 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14889     boolean_t allow_usesrc, boolean_t *notreadyp)
14890 {
14891 	ill_t	*usill = NULL;
14892 	ill_t	*ipmp_ill = NULL;
14893 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14894 	ipif_type_t type, best_type;
14895 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14896 	ip_stack_t *ipst = ill->ill_ipst;
14897 	boolean_t samenet;
14898 
14899 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14900 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14901 		    B_FALSE, ipst);
14902 		if (usill != NULL)
14903 			ill = usill;	/* Select source from usesrc ILL */
14904 		else
14905 			return (NULL);
14906 	}
14907 
14908 	/*
14909 	 * Test addresses should never be used for source address selection,
14910 	 * so if we were passed one, switch to the IPMP meta-interface.
14911 	 */
14912 	if (IS_UNDER_IPMP(ill)) {
14913 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14914 			ill = ipmp_ill;	/* Select source from IPMP ill */
14915 		else
14916 			return (NULL);
14917 	}
14918 
14919 	/*
14920 	 * If we're dealing with an unlabeled destination on a labeled system,
14921 	 * make sure that we ignore source addresses that are incompatible with
14922 	 * the destination's default label.  That destination's default label
14923 	 * must dominate the minimum label on the source address.
14924 	 */
14925 	dst_rhtp = NULL;
14926 	if (is_system_labeled()) {
14927 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14928 		if (dst_rhtp == NULL)
14929 			return (NULL);
14930 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14931 			TPC_RELE(dst_rhtp);
14932 			dst_rhtp = NULL;
14933 		}
14934 	}
14935 
14936 	/*
14937 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14938 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14939 	 * After selecting the right ipif, under ill_lock make sure ipif is
14940 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14941 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14942 	 * but not under a lock.
14943 	 */
14944 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14945 retry:
14946 	/*
14947 	 * For source address selection, we treat the ipif list as circular
14948 	 * and continue until we get back to where we started.  This allows
14949 	 * IPMP to vary source address selection (which improves inbound load
14950 	 * spreading) by caching its last ending point and starting from
14951 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14952 	 * ills since that can't happen on the IPMP ill.
14953 	 */
14954 	start_ipif = ill->ill_ipif;
14955 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14956 		start_ipif = ill->ill_src_ipif;
14957 
14958 	ipif = start_ipif;
14959 	best_ipif = NULL;
14960 	best_type = IPIF_NONE;
14961 	do {
14962 		if ((next_ipif = ipif->ipif_next) == NULL)
14963 			next_ipif = ill->ill_ipif;
14964 
14965 		if (IPIF_IS_CONDEMNED(ipif))
14966 			continue;
14967 		/* Always skip NOLOCAL and ANYCAST interfaces */
14968 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14969 			continue;
14970 		/* Always skip NOACCEPT interfaces */
14971 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14972 			continue;
14973 		if (!(ipif->ipif_flags & IPIF_UP))
14974 			continue;
14975 
14976 		if (!ipif->ipif_addr_ready) {
14977 			if (notreadyp != NULL)
14978 				*notreadyp = B_TRUE;
14979 			continue;
14980 		}
14981 
14982 		if (zoneid != ALL_ZONES &&
14983 		    ipif->ipif_zoneid != zoneid &&
14984 		    ipif->ipif_zoneid != ALL_ZONES)
14985 			continue;
14986 
14987 		/*
14988 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14989 		 * are not valid as source addresses.
14990 		 */
14991 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14992 			continue;
14993 
14994 		/*
14995 		 * Check compatibility of local address for destination's
14996 		 * default label if we're on a labeled system.	Incompatible
14997 		 * addresses can't be used at all.
14998 		 */
14999 		if (dst_rhtp != NULL) {
15000 			boolean_t incompat;
15001 
15002 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15003 			    IPV4_VERSION, B_FALSE);
15004 			if (src_rhtp == NULL)
15005 				continue;
15006 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15007 			    src_rhtp->tpc_tp.tp_doi !=
15008 			    dst_rhtp->tpc_tp.tp_doi ||
15009 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15010 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15011 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15012 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15013 			TPC_RELE(src_rhtp);
15014 			if (incompat)
15015 				continue;
15016 		}
15017 
15018 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15019 
15020 		if (ipif->ipif_lcl_addr == dst) {
15021 			type = IPIF_LOCALADDR;
15022 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15023 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15024 			    IPIF_DIFFNET_DEPRECATED;
15025 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15026 			type = samenet ? IPIF_SAMENET_ALLZONES :
15027 			    IPIF_DIFFNET_ALLZONES;
15028 		} else {
15029 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15030 		}
15031 
15032 		if (type > best_type) {
15033 			best_type = type;
15034 			best_ipif = ipif;
15035 			if (best_type == IPIF_LOCALADDR)
15036 				break; /* can't get better */
15037 		}
15038 	} while ((ipif = next_ipif) != start_ipif);
15039 
15040 	if ((ipif = best_ipif) != NULL) {
15041 		mutex_enter(&ipif->ipif_ill->ill_lock);
15042 		if (IPIF_IS_CONDEMNED(ipif)) {
15043 			mutex_exit(&ipif->ipif_ill->ill_lock);
15044 			goto retry;
15045 		}
15046 		ipif_refhold_locked(ipif);
15047 
15048 		/*
15049 		 * For IPMP, update the source ipif rotor to the next ipif,
15050 		 * provided we can look it up.  (We must not use it if it's
15051 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15052 		 * ipif_free() checked ill_src_ipif.)
15053 		 */
15054 		if (IS_IPMP(ill) && ipif != NULL) {
15055 			next_ipif = ipif->ipif_next;
15056 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15057 				ill->ill_src_ipif = next_ipif;
15058 			else
15059 				ill->ill_src_ipif = NULL;
15060 		}
15061 		mutex_exit(&ipif->ipif_ill->ill_lock);
15062 	}
15063 
15064 	rw_exit(&ipst->ips_ill_g_lock);
15065 	if (usill != NULL)
15066 		ill_refrele(usill);
15067 	if (ipmp_ill != NULL)
15068 		ill_refrele(ipmp_ill);
15069 	if (dst_rhtp != NULL)
15070 		TPC_RELE(dst_rhtp);
15071 
15072 #ifdef DEBUG
15073 	if (ipif == NULL) {
15074 		char buf1[INET6_ADDRSTRLEN];
15075 
15076 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15077 		    ill->ill_name,
15078 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15079 	} else {
15080 		char buf1[INET6_ADDRSTRLEN];
15081 		char buf2[INET6_ADDRSTRLEN];
15082 
15083 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15084 		    ipif->ipif_ill->ill_name,
15085 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15086 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15087 		    buf2, sizeof (buf2))));
15088 	}
15089 #endif /* DEBUG */
15090 	return (ipif);
15091 }
15092 
15093 /*
15094  * Pick a source address based on the destination ill and an optional setsrc
15095  * address.
15096  * The result is stored in srcp. If generation is set, then put the source
15097  * generation number there before we look for the source address (to avoid
15098  * missing changes in the set of source addresses.
15099  * If flagsp is set, then us it to pass back ipif_flags.
15100  *
15101  * If the caller wants to cache the returned source address and detect when
15102  * that might be stale, the caller should pass in a generation argument,
15103  * which the caller can later compare against ips_src_generation
15104  *
15105  * The precedence order for selecting an IPv4 source address is:
15106  *  - RTF_SETSRC on the offlink ire always wins.
15107  *  - If usrsrc is set, swap the ill to be the usesrc one.
15108  *  - If IPMP is used on the ill, select a random address from the most
15109  *    preferred ones below:
15110  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15111  * 2. Not deprecated, not ALL_ZONES
15112  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15113  * 4. Not deprecated, ALL_ZONES
15114  * 5. If onlink destination, same subnet and deprecated
15115  * 6. Deprecated.
15116  *
15117  * We have lower preference for ALL_ZONES IP addresses,
15118  * as they pose problems with unlabeled destinations.
15119  *
15120  * Note that when multiple IP addresses match e.g., #1 we pick
15121  * the first one if IPMP is not in use. With IPMP we randomize.
15122  */
15123 int
15124 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15125     ipaddr_t multicast_ifaddr,
15126     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15127     uint32_t *generation, uint64_t *flagsp)
15128 {
15129 	ipif_t *ipif;
15130 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15131 
15132 	if (flagsp != NULL)
15133 		*flagsp = 0;
15134 
15135 	/*
15136 	 * Need to grab the generation number before we check to
15137 	 * avoid a race with a change to the set of local addresses.
15138 	 * No lock needed since the thread which updates the set of local
15139 	 * addresses use ipif/ill locks and exit those (hence a store memory
15140 	 * barrier) before doing the atomic increase of ips_src_generation.
15141 	 */
15142 	if (generation != NULL) {
15143 		*generation = ipst->ips_src_generation;
15144 	}
15145 
15146 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15147 		*srcp = multicast_ifaddr;
15148 		return (0);
15149 	}
15150 
15151 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15152 	if (setsrc != INADDR_ANY) {
15153 		*srcp = setsrc;
15154 		return (0);
15155 	}
15156 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15157 	if (ipif == NULL) {
15158 		if (notready)
15159 			return (ENETDOWN);
15160 		else
15161 			return (EADDRNOTAVAIL);
15162 	}
15163 	*srcp = ipif->ipif_lcl_addr;
15164 	if (flagsp != NULL)
15165 		*flagsp = ipif->ipif_flags;
15166 	ipif_refrele(ipif);
15167 	return (0);
15168 }
15169 
15170 /* ARGSUSED */
15171 int
15172 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15173 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15174 {
15175 	/*
15176 	 * ill_phyint_reinit merged the v4 and v6 into a single
15177 	 * ipsq.  We might not have been able to complete the
15178 	 * operation in ipif_set_values, if we could not become
15179 	 * exclusive.  If so restart it here.
15180 	 */
15181 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15182 }
15183 
15184 /*
15185  * Can operate on either a module or a driver queue.
15186  * Returns an error if not a module queue.
15187  */
15188 /* ARGSUSED */
15189 int
15190 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15191     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15192 {
15193 	queue_t		*q1 = q;
15194 	char 		*cp;
15195 	char		interf_name[LIFNAMSIZ];
15196 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15197 
15198 	if (q->q_next == NULL) {
15199 		ip1dbg((
15200 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15201 		return (EINVAL);
15202 	}
15203 
15204 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15205 		return (EALREADY);
15206 
15207 	do {
15208 		q1 = q1->q_next;
15209 	} while (q1->q_next);
15210 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15211 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15212 
15213 	/*
15214 	 * Here we are not going to delay the ioack until after
15215 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15216 	 * original ioctl message before sending the requests.
15217 	 */
15218 	return (ipif_set_values(q, mp, interf_name, &ppa));
15219 }
15220 
15221 /* ARGSUSED */
15222 int
15223 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15224     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15225 {
15226 	return (ENXIO);
15227 }
15228 
15229 /*
15230  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15231  * `irep'.  Returns a pointer to the next free `irep' entry
15232  * A mirror exists in ipif_delete_bcast_ires().
15233  *
15234  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15235  * done in ire_add.
15236  */
15237 static ire_t **
15238 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15239 {
15240 	ipaddr_t addr;
15241 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15242 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15243 	ill_t *ill = ipif->ipif_ill;
15244 	zoneid_t zoneid = ipif->ipif_zoneid;
15245 
15246 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15247 
15248 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15249 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15250 
15251 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15252 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15253 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15254 
15255 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15256 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15257 
15258 	/*
15259 	 * For backward compatibility, we create net broadcast IREs based on
15260 	 * the old "IP address class system", since some old machines only
15261 	 * respond to these class derived net broadcast.  However, we must not
15262 	 * create these net broadcast IREs if the subnetmask is shorter than
15263 	 * the IP address class based derived netmask.  Otherwise, we may
15264 	 * create a net broadcast address which is the same as an IP address
15265 	 * on the subnet -- and then TCP will refuse to talk to that address.
15266 	 */
15267 	if (netmask < subnetmask) {
15268 		addr = netmask & ipif->ipif_subnet;
15269 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15270 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15271 	}
15272 
15273 	/*
15274 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15275 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15276 	 * created.  Creating these broadcast IREs will only create confusion
15277 	 * as `addr' will be the same as the IP address.
15278 	 */
15279 	if (subnetmask != 0xFFFFFFFF) {
15280 		addr = ipif->ipif_subnet;
15281 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15282 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15283 	}
15284 
15285 	return (irep);
15286 }
15287 
15288 /*
15289  * Mirror of ipif_create_bcast_ires()
15290  */
15291 static void
15292 ipif_delete_bcast_ires(ipif_t *ipif)
15293 {
15294 	ipaddr_t	addr;
15295 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15296 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15297 	ill_t		*ill = ipif->ipif_ill;
15298 	zoneid_t	zoneid = ipif->ipif_zoneid;
15299 	ire_t		*ire;
15300 
15301 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15302 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15303 
15304 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15305 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15306 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15307 
15308 	ire = ire_lookup_bcast(ill, 0, zoneid);
15309 	ASSERT(ire != NULL);
15310 	ire_delete(ire); ire_refrele(ire);
15311 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15312 	ASSERT(ire != NULL);
15313 	ire_delete(ire); ire_refrele(ire);
15314 
15315 	/*
15316 	 * For backward compatibility, we create net broadcast IREs based on
15317 	 * the old "IP address class system", since some old machines only
15318 	 * respond to these class derived net broadcast.  However, we must not
15319 	 * create these net broadcast IREs if the subnetmask is shorter than
15320 	 * the IP address class based derived netmask.  Otherwise, we may
15321 	 * create a net broadcast address which is the same as an IP address
15322 	 * on the subnet -- and then TCP will refuse to talk to that address.
15323 	 */
15324 	if (netmask < subnetmask) {
15325 		addr = netmask & ipif->ipif_subnet;
15326 		ire = ire_lookup_bcast(ill, addr, zoneid);
15327 		ASSERT(ire != NULL);
15328 		ire_delete(ire); ire_refrele(ire);
15329 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15330 		ASSERT(ire != NULL);
15331 		ire_delete(ire); ire_refrele(ire);
15332 	}
15333 
15334 	/*
15335 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15336 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15337 	 * created.  Creating these broadcast IREs will only create confusion
15338 	 * as `addr' will be the same as the IP address.
15339 	 */
15340 	if (subnetmask != 0xFFFFFFFF) {
15341 		addr = ipif->ipif_subnet;
15342 		ire = ire_lookup_bcast(ill, addr, zoneid);
15343 		ASSERT(ire != NULL);
15344 		ire_delete(ire); ire_refrele(ire);
15345 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15346 		ASSERT(ire != NULL);
15347 		ire_delete(ire); ire_refrele(ire);
15348 	}
15349 }
15350 
15351 /*
15352  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15353  * from lifr_flags and the name from lifr_name.
15354  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15355  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15356  * Returns EINPROGRESS when mp has been consumed by queueing it on
15357  * ipx_pending_mp and the ioctl will complete in ip_rput.
15358  *
15359  * Can operate on either a module or a driver queue.
15360  * Returns an error if not a module queue.
15361  */
15362 /* ARGSUSED */
15363 int
15364 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15365     ip_ioctl_cmd_t *ipip, void *if_req)
15366 {
15367 	ill_t	*ill = q->q_ptr;
15368 	phyint_t *phyi;
15369 	ip_stack_t *ipst;
15370 	struct lifreq *lifr = if_req;
15371 	uint64_t new_flags;
15372 
15373 	ASSERT(ipif != NULL);
15374 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15375 
15376 	if (q->q_next == NULL) {
15377 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15378 		return (EINVAL);
15379 	}
15380 
15381 	/*
15382 	 * If we are not writer on 'q' then this interface exists already
15383 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15384 	 * so return EALREADY.
15385 	 */
15386 	if (ill != ipif->ipif_ill)
15387 		return (EALREADY);
15388 
15389 	if (ill->ill_name[0] != '\0')
15390 		return (EALREADY);
15391 
15392 	/*
15393 	 * If there's another ill already with the requested name, ensure
15394 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15395 	 * fuse together two unrelated ills, which will cause chaos.
15396 	 */
15397 	ipst = ill->ill_ipst;
15398 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15399 	    lifr->lifr_name, NULL);
15400 	if (phyi != NULL) {
15401 		ill_t *ill_mate = phyi->phyint_illv4;
15402 
15403 		if (ill_mate == NULL)
15404 			ill_mate = phyi->phyint_illv6;
15405 		ASSERT(ill_mate != NULL);
15406 
15407 		if (ill_mate->ill_media->ip_m_mac_type !=
15408 		    ill->ill_media->ip_m_mac_type) {
15409 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15410 			    "use the same ill name on differing media\n"));
15411 			return (EINVAL);
15412 		}
15413 	}
15414 
15415 	/*
15416 	 * We start off as IFF_IPV4 in ipif_allocate and become
15417 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15418 	 * The only flags that we read from user space are IFF_IPV4,
15419 	 * IFF_IPV6, and IFF_BROADCAST.
15420 	 *
15421 	 * This ill has not been inserted into the global list.
15422 	 * So we are still single threaded and don't need any lock
15423 	 *
15424 	 * Saniy check the flags.
15425 	 */
15426 
15427 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15428 	    ((lifr->lifr_flags & IFF_IPV6) ||
15429 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15430 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15431 		    "or IPv6 i.e., no broadcast \n"));
15432 		return (EINVAL);
15433 	}
15434 
15435 	new_flags =
15436 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15437 
15438 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15439 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15440 		    "IFF_IPV4 or IFF_IPV6\n"));
15441 		return (EINVAL);
15442 	}
15443 
15444 	/*
15445 	 * We always start off as IPv4, so only need to check for IPv6.
15446 	 */
15447 	if ((new_flags & IFF_IPV6) != 0) {
15448 		ill->ill_flags |= ILLF_IPV6;
15449 		ill->ill_flags &= ~ILLF_IPV4;
15450 	}
15451 
15452 	if ((new_flags & IFF_BROADCAST) != 0)
15453 		ipif->ipif_flags |= IPIF_BROADCAST;
15454 	else
15455 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15456 
15457 	/* We started off as V4. */
15458 	if (ill->ill_flags & ILLF_IPV6) {
15459 		ill->ill_phyint->phyint_illv6 = ill;
15460 		ill->ill_phyint->phyint_illv4 = NULL;
15461 	}
15462 
15463 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15464 }
15465 
15466 /* ARGSUSED */
15467 int
15468 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15469     ip_ioctl_cmd_t *ipip, void *if_req)
15470 {
15471 	/*
15472 	 * ill_phyint_reinit merged the v4 and v6 into a single
15473 	 * ipsq.  We might not have been able to complete the
15474 	 * slifname in ipif_set_values, if we could not become
15475 	 * exclusive.  If so restart it here
15476 	 */
15477 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15478 }
15479 
15480 /*
15481  * Return a pointer to the ipif which matches the index, IP version type and
15482  * zoneid.
15483  */
15484 ipif_t *
15485 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15486     ip_stack_t *ipst)
15487 {
15488 	ill_t	*ill;
15489 	ipif_t	*ipif = NULL;
15490 
15491 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15492 	if (ill != NULL) {
15493 		mutex_enter(&ill->ill_lock);
15494 		for (ipif = ill->ill_ipif; ipif != NULL;
15495 		    ipif = ipif->ipif_next) {
15496 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15497 			    zoneid == ipif->ipif_zoneid ||
15498 			    ipif->ipif_zoneid == ALL_ZONES)) {
15499 				ipif_refhold_locked(ipif);
15500 				break;
15501 			}
15502 		}
15503 		mutex_exit(&ill->ill_lock);
15504 		ill_refrele(ill);
15505 	}
15506 	return (ipif);
15507 }
15508 
15509 /*
15510  * Change an existing physical interface's index. If the new index
15511  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15512  * Finally, we update other systems which may have a dependence on the
15513  * index value.
15514  */
15515 /* ARGSUSED */
15516 int
15517 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15518     ip_ioctl_cmd_t *ipip, void *ifreq)
15519 {
15520 	ill_t		*ill;
15521 	phyint_t	*phyi;
15522 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15523 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15524 	uint_t	old_index, index;
15525 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15526 	avl_index_t	where;
15527 
15528 	if (ipip->ipi_cmd_type == IF_CMD)
15529 		index = ifr->ifr_index;
15530 	else
15531 		index = lifr->lifr_index;
15532 
15533 	/*
15534 	 * Only allow on physical interface. Also, index zero is illegal.
15535 	 */
15536 	ill = ipif->ipif_ill;
15537 	phyi = ill->ill_phyint;
15538 	if (ipif->ipif_id != 0 || index == 0) {
15539 		return (EINVAL);
15540 	}
15541 
15542 	/* If the index is not changing, no work to do */
15543 	if (phyi->phyint_ifindex == index)
15544 		return (0);
15545 
15546 	/*
15547 	 * Use phyint_exists() to determine if the new interface index
15548 	 * is already in use. If the index is unused then we need to
15549 	 * change the phyint's position in the phyint_list_avl_by_index
15550 	 * tree. If we do not do this, subsequent lookups (using the new
15551 	 * index value) will not find the phyint.
15552 	 */
15553 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15554 	if (phyint_exists(index, ipst)) {
15555 		rw_exit(&ipst->ips_ill_g_lock);
15556 		return (EEXIST);
15557 	}
15558 
15559 	/*
15560 	 * The new index is unused. Set it in the phyint. However we must not
15561 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15562 	 * changes. The event must be bound to old ifindex value.
15563 	 */
15564 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15565 	    &index, sizeof (index));
15566 
15567 	old_index = phyi->phyint_ifindex;
15568 	phyi->phyint_ifindex = index;
15569 
15570 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15571 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15572 	    &index, &where);
15573 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15574 	    phyi, where);
15575 	rw_exit(&ipst->ips_ill_g_lock);
15576 
15577 	/* Update SCTP's ILL list */
15578 	sctp_ill_reindex(ill, old_index);
15579 
15580 	/* Send the routing sockets message */
15581 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15582 	if (ILL_OTHER(ill))
15583 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15584 
15585 	/* Perhaps ilgs should use this ill */
15586 	update_conn_ill(NULL, ill->ill_ipst);
15587 	return (0);
15588 }
15589 
15590 /* ARGSUSED */
15591 int
15592 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15593     ip_ioctl_cmd_t *ipip, void *ifreq)
15594 {
15595 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15596 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15597 
15598 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15599 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15600 	/* Get the interface index */
15601 	if (ipip->ipi_cmd_type == IF_CMD) {
15602 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15603 	} else {
15604 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15605 	}
15606 	return (0);
15607 }
15608 
15609 /* ARGSUSED */
15610 int
15611 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15612     ip_ioctl_cmd_t *ipip, void *ifreq)
15613 {
15614 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15615 
15616 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15617 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15618 	/* Get the interface zone */
15619 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15620 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15621 	return (0);
15622 }
15623 
15624 /*
15625  * Set the zoneid of an interface.
15626  */
15627 /* ARGSUSED */
15628 int
15629 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15630     ip_ioctl_cmd_t *ipip, void *ifreq)
15631 {
15632 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15633 	int err = 0;
15634 	boolean_t need_up = B_FALSE;
15635 	zone_t *zptr;
15636 	zone_status_t status;
15637 	zoneid_t zoneid;
15638 
15639 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15640 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15641 		if (!is_system_labeled())
15642 			return (ENOTSUP);
15643 		zoneid = GLOBAL_ZONEID;
15644 	}
15645 
15646 	/* cannot assign instance zero to a non-global zone */
15647 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15648 		return (ENOTSUP);
15649 
15650 	/*
15651 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15652 	 * the event of a race with the zone shutdown processing, since IP
15653 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15654 	 * interface will be cleaned up even if the zone is shut down
15655 	 * immediately after the status check. If the interface can't be brought
15656 	 * down right away, and the zone is shut down before the restart
15657 	 * function is called, we resolve the possible races by rechecking the
15658 	 * zone status in the restart function.
15659 	 */
15660 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15661 		return (EINVAL);
15662 	status = zone_status_get(zptr);
15663 	zone_rele(zptr);
15664 
15665 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15666 		return (EINVAL);
15667 
15668 	if (ipif->ipif_flags & IPIF_UP) {
15669 		/*
15670 		 * If the interface is already marked up,
15671 		 * we call ipif_down which will take care
15672 		 * of ditching any IREs that have been set
15673 		 * up based on the old interface address.
15674 		 */
15675 		err = ipif_logical_down(ipif, q, mp);
15676 		if (err == EINPROGRESS)
15677 			return (err);
15678 		(void) ipif_down_tail(ipif);
15679 		need_up = B_TRUE;
15680 	}
15681 
15682 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15683 	return (err);
15684 }
15685 
15686 static int
15687 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15688     queue_t *q, mblk_t *mp, boolean_t need_up)
15689 {
15690 	int	err = 0;
15691 	ip_stack_t	*ipst;
15692 
15693 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15694 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15695 
15696 	if (CONN_Q(q))
15697 		ipst = CONNQ_TO_IPST(q);
15698 	else
15699 		ipst = ILLQ_TO_IPST(q);
15700 
15701 	/*
15702 	 * For exclusive stacks we don't allow a different zoneid than
15703 	 * global.
15704 	 */
15705 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15706 	    zoneid != GLOBAL_ZONEID)
15707 		return (EINVAL);
15708 
15709 	/* Set the new zone id. */
15710 	ipif->ipif_zoneid = zoneid;
15711 
15712 	/* Update sctp list */
15713 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15714 
15715 	/* The default multicast interface might have changed */
15716 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15717 
15718 	if (need_up) {
15719 		/*
15720 		 * Now bring the interface back up.  If this
15721 		 * is the only IPIF for the ILL, ipif_up
15722 		 * will have to re-bind to the device, so
15723 		 * we may get back EINPROGRESS, in which
15724 		 * case, this IOCTL will get completed in
15725 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15726 		 */
15727 		err = ipif_up(ipif, q, mp);
15728 	}
15729 	return (err);
15730 }
15731 
15732 /* ARGSUSED */
15733 int
15734 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15735     ip_ioctl_cmd_t *ipip, void *if_req)
15736 {
15737 	struct lifreq *lifr = (struct lifreq *)if_req;
15738 	zoneid_t zoneid;
15739 	zone_t *zptr;
15740 	zone_status_t status;
15741 
15742 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15743 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15744 		zoneid = GLOBAL_ZONEID;
15745 
15746 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15747 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15748 
15749 	/*
15750 	 * We recheck the zone status to resolve the following race condition:
15751 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15752 	 * 2) hme0:1 is up and can't be brought down right away;
15753 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15754 	 * 3) zone "myzone" is halted; the zone status switches to
15755 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15756 	 * the interfaces to remove - hme0:1 is not returned because it's not
15757 	 * yet in "myzone", so it won't be removed;
15758 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15759 	 * status check here, we would have hme0:1 in "myzone" after it's been
15760 	 * destroyed.
15761 	 * Note that if the status check fails, we need to bring the interface
15762 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15763 	 * ipif_up_done[_v6]().
15764 	 */
15765 	status = ZONE_IS_UNINITIALIZED;
15766 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15767 		status = zone_status_get(zptr);
15768 		zone_rele(zptr);
15769 	}
15770 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15771 		if (ipif->ipif_isv6) {
15772 			(void) ipif_up_done_v6(ipif);
15773 		} else {
15774 			(void) ipif_up_done(ipif);
15775 		}
15776 		return (EINVAL);
15777 	}
15778 
15779 	(void) ipif_down_tail(ipif);
15780 
15781 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15782 	    B_TRUE));
15783 }
15784 
15785 /*
15786  * Return the number of addresses on `ill' with one or more of the values
15787  * in `set' set and all of the values in `clear' clear.
15788  */
15789 static uint_t
15790 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15791 {
15792 	ipif_t	*ipif;
15793 	uint_t	cnt = 0;
15794 
15795 	ASSERT(IAM_WRITER_ILL(ill));
15796 
15797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15798 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15799 			cnt++;
15800 
15801 	return (cnt);
15802 }
15803 
15804 /*
15805  * Return the number of migratable addresses on `ill' that are under
15806  * application control.
15807  */
15808 uint_t
15809 ill_appaddr_cnt(const ill_t *ill)
15810 {
15811 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15812 	    IPIF_NOFAILOVER));
15813 }
15814 
15815 /*
15816  * Return the number of point-to-point addresses on `ill'.
15817  */
15818 uint_t
15819 ill_ptpaddr_cnt(const ill_t *ill)
15820 {
15821 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15822 }
15823 
15824 /* ARGSUSED */
15825 int
15826 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15827 	ip_ioctl_cmd_t *ipip, void *ifreq)
15828 {
15829 	struct lifreq	*lifr = ifreq;
15830 
15831 	ASSERT(q->q_next == NULL);
15832 	ASSERT(CONN_Q(q));
15833 
15834 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15835 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15836 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15837 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15838 
15839 	return (0);
15840 }
15841 
15842 /* Find the previous ILL in this usesrc group */
15843 static ill_t *
15844 ill_prev_usesrc(ill_t *uill)
15845 {
15846 	ill_t *ill;
15847 
15848 	for (ill = uill->ill_usesrc_grp_next;
15849 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15850 	    ill = ill->ill_usesrc_grp_next)
15851 		/* do nothing */;
15852 	return (ill);
15853 }
15854 
15855 /*
15856  * Release all members of the usesrc group. This routine is called
15857  * from ill_delete when the interface being unplumbed is the
15858  * group head.
15859  *
15860  * This silently clears the usesrc that ifconfig setup.
15861  * An alternative would be to keep that ifindex, and drop packets on the floor
15862  * since no source address can be selected.
15863  * Even if we keep the current semantics, don't need a lock and a linked list.
15864  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15865  * the one that is being removed. Issue is how we return the usesrc users
15866  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15867  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15868  * ill walk, but the walker would need to insert in the ioctl response.
15869  */
15870 static void
15871 ill_disband_usesrc_group(ill_t *uill)
15872 {
15873 	ill_t *next_ill, *tmp_ill;
15874 	ip_stack_t	*ipst = uill->ill_ipst;
15875 
15876 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15877 	next_ill = uill->ill_usesrc_grp_next;
15878 
15879 	do {
15880 		ASSERT(next_ill != NULL);
15881 		tmp_ill = next_ill->ill_usesrc_grp_next;
15882 		ASSERT(tmp_ill != NULL);
15883 		next_ill->ill_usesrc_grp_next = NULL;
15884 		next_ill->ill_usesrc_ifindex = 0;
15885 		next_ill = tmp_ill;
15886 	} while (next_ill->ill_usesrc_ifindex != 0);
15887 	uill->ill_usesrc_grp_next = NULL;
15888 }
15889 
15890 /*
15891  * Remove the client usesrc ILL from the list and relink to a new list
15892  */
15893 int
15894 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15895 {
15896 	ill_t *ill, *tmp_ill;
15897 	ip_stack_t	*ipst = ucill->ill_ipst;
15898 
15899 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15900 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15901 
15902 	/*
15903 	 * Check if the usesrc client ILL passed in is not already
15904 	 * in use as a usesrc ILL i.e one whose source address is
15905 	 * in use OR a usesrc ILL is not already in use as a usesrc
15906 	 * client ILL
15907 	 */
15908 	if ((ucill->ill_usesrc_ifindex == 0) ||
15909 	    (uill->ill_usesrc_ifindex != 0)) {
15910 		return (-1);
15911 	}
15912 
15913 	ill = ill_prev_usesrc(ucill);
15914 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15915 
15916 	/* Remove from the current list */
15917 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15918 		/* Only two elements in the list */
15919 		ASSERT(ill->ill_usesrc_ifindex == 0);
15920 		ill->ill_usesrc_grp_next = NULL;
15921 	} else {
15922 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15923 	}
15924 
15925 	if (ifindex == 0) {
15926 		ucill->ill_usesrc_ifindex = 0;
15927 		ucill->ill_usesrc_grp_next = NULL;
15928 		return (0);
15929 	}
15930 
15931 	ucill->ill_usesrc_ifindex = ifindex;
15932 	tmp_ill = uill->ill_usesrc_grp_next;
15933 	uill->ill_usesrc_grp_next = ucill;
15934 	ucill->ill_usesrc_grp_next =
15935 	    (tmp_ill != NULL) ? tmp_ill : uill;
15936 	return (0);
15937 }
15938 
15939 /*
15940  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15941  * ip.c for locking details.
15942  */
15943 /* ARGSUSED */
15944 int
15945 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15946     ip_ioctl_cmd_t *ipip, void *ifreq)
15947 {
15948 	struct lifreq *lifr = (struct lifreq *)ifreq;
15949 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15950 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15951 	int err = 0, ret;
15952 	uint_t ifindex;
15953 	ipsq_t *ipsq = NULL;
15954 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15955 
15956 	ASSERT(IAM_WRITER_IPIF(ipif));
15957 	ASSERT(q->q_next == NULL);
15958 	ASSERT(CONN_Q(q));
15959 
15960 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15961 
15962 	ifindex = lifr->lifr_index;
15963 	if (ifindex == 0) {
15964 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15965 			/* non usesrc group interface, nothing to reset */
15966 			return (0);
15967 		}
15968 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15969 		/* valid reset request */
15970 		reset_flg = B_TRUE;
15971 	}
15972 
15973 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15974 	if (usesrc_ill == NULL) {
15975 		return (ENXIO);
15976 	}
15977 
15978 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15979 	    NEW_OP, B_TRUE);
15980 	if (ipsq == NULL) {
15981 		err = EINPROGRESS;
15982 		/* Operation enqueued on the ipsq of the usesrc ILL */
15983 		goto done;
15984 	}
15985 
15986 	/* USESRC isn't currently supported with IPMP */
15987 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15988 		err = ENOTSUP;
15989 		goto done;
15990 	}
15991 
15992 	/*
15993 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15994 	 * used by IPMP underlying interfaces, but someone might think it's
15995 	 * more general and try to use it independently with VNI.)
15996 	 */
15997 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15998 		err = ENOTSUP;
15999 		goto done;
16000 	}
16001 
16002 	/*
16003 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16004 	 * already a client then return EINVAL
16005 	 */
16006 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16007 		err = EINVAL;
16008 		goto done;
16009 	}
16010 
16011 	/*
16012 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16013 	 * be then this is a duplicate operation.
16014 	 */
16015 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16016 		err = 0;
16017 		goto done;
16018 	}
16019 
16020 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16021 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16022 	    usesrc_ill->ill_isv6));
16023 
16024 	/*
16025 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16026 	 * and the ill_usesrc_ifindex fields
16027 	 */
16028 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16029 
16030 	if (reset_flg) {
16031 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16032 		if (ret != 0) {
16033 			err = EINVAL;
16034 		}
16035 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16036 		goto done;
16037 	}
16038 
16039 	/*
16040 	 * Four possibilities to consider:
16041 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16042 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16043 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16044 	 * 4. Both are part of their respective usesrc groups
16045 	 */
16046 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16047 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16048 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16049 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16050 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16051 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16052 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16053 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16054 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16055 		/* Insert at head of list */
16056 		usesrc_cli_ill->ill_usesrc_grp_next =
16057 		    usesrc_ill->ill_usesrc_grp_next;
16058 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16059 	} else {
16060 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16061 		    ifindex);
16062 		if (ret != 0)
16063 			err = EINVAL;
16064 	}
16065 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16066 
16067 done:
16068 	if (ipsq != NULL)
16069 		ipsq_exit(ipsq);
16070 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16071 	ill_refrele(usesrc_ill);
16072 
16073 	/* Let conn_ixa caching know that source address selection changed */
16074 	ip_update_source_selection(ipst);
16075 
16076 	return (err);
16077 }
16078 
16079 /*
16080  * comparison function used by avl.
16081  */
16082 static int
16083 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16084 {
16085 
16086 	uint_t index;
16087 
16088 	ASSERT(phyip != NULL && index_ptr != NULL);
16089 
16090 	index = *((uint_t *)index_ptr);
16091 	/*
16092 	 * let the phyint with the lowest index be on top.
16093 	 */
16094 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16095 		return (1);
16096 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16097 		return (-1);
16098 	return (0);
16099 }
16100 
16101 /*
16102  * comparison function used by avl.
16103  */
16104 static int
16105 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16106 {
16107 	ill_t *ill;
16108 	int res = 0;
16109 
16110 	ASSERT(phyip != NULL && name_ptr != NULL);
16111 
16112 	if (((phyint_t *)phyip)->phyint_illv4)
16113 		ill = ((phyint_t *)phyip)->phyint_illv4;
16114 	else
16115 		ill = ((phyint_t *)phyip)->phyint_illv6;
16116 	ASSERT(ill != NULL);
16117 
16118 	res = strcmp(ill->ill_name, (char *)name_ptr);
16119 	if (res > 0)
16120 		return (1);
16121 	else if (res < 0)
16122 		return (-1);
16123 	return (0);
16124 }
16125 
16126 /*
16127  * This function is called on the unplumb path via ill_glist_delete() when
16128  * there are no ills left on the phyint and thus the phyint can be freed.
16129  */
16130 static void
16131 phyint_free(phyint_t *phyi)
16132 {
16133 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16134 
16135 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16136 
16137 	/*
16138 	 * If this phyint was an IPMP meta-interface, blow away the group.
16139 	 * This is safe to do because all of the illgrps have already been
16140 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16141 	 * If we're cleaning up as a result of failed initialization,
16142 	 * phyint_grp may be NULL.
16143 	 */
16144 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16145 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16146 		ipmp_grp_destroy(phyi->phyint_grp);
16147 		phyi->phyint_grp = NULL;
16148 		rw_exit(&ipst->ips_ipmp_lock);
16149 	}
16150 
16151 	/*
16152 	 * If this interface was under IPMP, take it out of the group.
16153 	 */
16154 	if (phyi->phyint_grp != NULL)
16155 		ipmp_phyint_leave_grp(phyi);
16156 
16157 	/*
16158 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16159 	 * will be freed in ipsq_exit().
16160 	 */
16161 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16162 	phyi->phyint_name[0] = '\0';
16163 
16164 	mi_free(phyi);
16165 }
16166 
16167 /*
16168  * Attach the ill to the phyint structure which can be shared by both
16169  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16170  * function is called from ipif_set_values and ill_lookup_on_name (for
16171  * loopback) where we know the name of the ill. We lookup the ill and if
16172  * there is one present already with the name use that phyint. Otherwise
16173  * reuse the one allocated by ill_init.
16174  */
16175 static void
16176 ill_phyint_reinit(ill_t *ill)
16177 {
16178 	boolean_t isv6 = ill->ill_isv6;
16179 	phyint_t *phyi_old;
16180 	phyint_t *phyi;
16181 	avl_index_t where = 0;
16182 	ill_t	*ill_other = NULL;
16183 	ip_stack_t	*ipst = ill->ill_ipst;
16184 
16185 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16186 
16187 	phyi_old = ill->ill_phyint;
16188 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16189 	    phyi_old->phyint_illv6 == NULL));
16190 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16191 	    phyi_old->phyint_illv4 == NULL));
16192 	ASSERT(phyi_old->phyint_ifindex == 0);
16193 
16194 	/*
16195 	 * Now that our ill has a name, set it in the phyint.
16196 	 */
16197 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16198 
16199 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16200 	    ill->ill_name, &where);
16201 
16202 	/*
16203 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16204 	 *    the global list of ills. So no other thread could have located
16205 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16206 	 * 2. Now locate the other protocol instance of this ill.
16207 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16208 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16209 	 *    of neither ill can change.
16210 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16211 	 *    other ill.
16212 	 * 5. Release all locks.
16213 	 */
16214 
16215 	/*
16216 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16217 	 * we are initializing IPv4.
16218 	 */
16219 	if (phyi != NULL) {
16220 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16221 		ASSERT(ill_other->ill_phyint != NULL);
16222 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16223 		    (!isv6 && ill_other->ill_isv6));
16224 		GRAB_ILL_LOCKS(ill, ill_other);
16225 		/*
16226 		 * We are potentially throwing away phyint_flags which
16227 		 * could be different from the one that we obtain from
16228 		 * ill_other->ill_phyint. But it is okay as we are assuming
16229 		 * that the state maintained within IP is correct.
16230 		 */
16231 		mutex_enter(&phyi->phyint_lock);
16232 		if (isv6) {
16233 			ASSERT(phyi->phyint_illv6 == NULL);
16234 			phyi->phyint_illv6 = ill;
16235 		} else {
16236 			ASSERT(phyi->phyint_illv4 == NULL);
16237 			phyi->phyint_illv4 = ill;
16238 		}
16239 
16240 		/*
16241 		 * Delete the old phyint and make its ipsq eligible
16242 		 * to be freed in ipsq_exit().
16243 		 */
16244 		phyi_old->phyint_illv4 = NULL;
16245 		phyi_old->phyint_illv6 = NULL;
16246 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16247 		phyi_old->phyint_name[0] = '\0';
16248 		mi_free(phyi_old);
16249 	} else {
16250 		mutex_enter(&ill->ill_lock);
16251 		/*
16252 		 * We don't need to acquire any lock, since
16253 		 * the ill is not yet visible globally  and we
16254 		 * have not yet released the ill_g_lock.
16255 		 */
16256 		phyi = phyi_old;
16257 		mutex_enter(&phyi->phyint_lock);
16258 		/* XXX We need a recovery strategy here. */
16259 		if (!phyint_assign_ifindex(phyi, ipst))
16260 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16261 
16262 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16263 		    (void *)phyi, where);
16264 
16265 		(void) avl_find(&ipst->ips_phyint_g_list->
16266 		    phyint_list_avl_by_index,
16267 		    &phyi->phyint_ifindex, &where);
16268 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16269 		    (void *)phyi, where);
16270 	}
16271 
16272 	/*
16273 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16274 	 * pending mp is not affected because that is per ill basis.
16275 	 */
16276 	ill->ill_phyint = phyi;
16277 
16278 	/*
16279 	 * Now that the phyint's ifindex has been assigned, complete the
16280 	 * remaining
16281 	 */
16282 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16283 	if (ill->ill_isv6) {
16284 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16285 		    ill->ill_phyint->phyint_ifindex;
16286 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16287 	} else {
16288 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16289 	}
16290 
16291 	/*
16292 	 * Generate an event within the hooks framework to indicate that
16293 	 * a new interface has just been added to IP.  For this event to
16294 	 * be generated, the network interface must, at least, have an
16295 	 * ifindex assigned to it.  (We don't generate the event for
16296 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16297 	 *
16298 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16299 	 * that the ordering of delivered events to listeners matches the
16300 	 * order of them in the kernel.
16301 	 */
16302 	if (!IS_LOOPBACK(ill)) {
16303 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16304 		    ill->ill_name_length);
16305 	}
16306 	RELEASE_ILL_LOCKS(ill, ill_other);
16307 	mutex_exit(&phyi->phyint_lock);
16308 }
16309 
16310 /*
16311  * Notify any downstream modules of the name of this interface.
16312  * An M_IOCTL is used even though we don't expect a successful reply.
16313  * Any reply message from the driver (presumably an M_IOCNAK) will
16314  * eventually get discarded somewhere upstream.  The message format is
16315  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16316  * to IP.
16317  */
16318 static void
16319 ip_ifname_notify(ill_t *ill, queue_t *q)
16320 {
16321 	mblk_t *mp1, *mp2;
16322 	struct iocblk *iocp;
16323 	struct lifreq *lifr;
16324 
16325 	mp1 = mkiocb(SIOCSLIFNAME);
16326 	if (mp1 == NULL)
16327 		return;
16328 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16329 	if (mp2 == NULL) {
16330 		freeb(mp1);
16331 		return;
16332 	}
16333 
16334 	mp1->b_cont = mp2;
16335 	iocp = (struct iocblk *)mp1->b_rptr;
16336 	iocp->ioc_count = sizeof (struct lifreq);
16337 
16338 	lifr = (struct lifreq *)mp2->b_rptr;
16339 	mp2->b_wptr += sizeof (struct lifreq);
16340 	bzero(lifr, sizeof (struct lifreq));
16341 
16342 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16343 	lifr->lifr_ppa = ill->ill_ppa;
16344 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16345 
16346 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16347 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16348 	putnext(q, mp1);
16349 }
16350 
16351 static int
16352 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16353 {
16354 	int		err;
16355 	ip_stack_t	*ipst = ill->ill_ipst;
16356 	phyint_t	*phyi = ill->ill_phyint;
16357 
16358 	/* Set the obsolete NDD per-interface forwarding name. */
16359 	err = ill_set_ndd_name(ill);
16360 	if (err != 0) {
16361 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16362 		    err);
16363 	}
16364 
16365 	/*
16366 	 * Now that ill_name is set, the configuration for the IPMP
16367 	 * meta-interface can be performed.
16368 	 */
16369 	if (IS_IPMP(ill)) {
16370 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16371 		/*
16372 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16373 		 * meta-interface and we need to create the IPMP group.
16374 		 */
16375 		if (phyi->phyint_grp == NULL) {
16376 			/*
16377 			 * If someone has renamed another IPMP group to have
16378 			 * the same name as our interface, bail.
16379 			 */
16380 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16381 				rw_exit(&ipst->ips_ipmp_lock);
16382 				return (EEXIST);
16383 			}
16384 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16385 			if (phyi->phyint_grp == NULL) {
16386 				rw_exit(&ipst->ips_ipmp_lock);
16387 				return (ENOMEM);
16388 			}
16389 		}
16390 		rw_exit(&ipst->ips_ipmp_lock);
16391 	}
16392 
16393 	/* Tell downstream modules where they are. */
16394 	ip_ifname_notify(ill, q);
16395 
16396 	/*
16397 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16398 	 * Error cases are ENOMEM ...
16399 	 */
16400 	err = ill_dl_phys(ill, ipif, mp, q);
16401 
16402 	if (ill->ill_isv6) {
16403 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16404 		if (ipst->ips_mld_slowtimeout_id == 0) {
16405 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16406 			    (void *)ipst,
16407 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16408 		}
16409 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16410 	} else {
16411 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16412 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16413 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16414 			    (void *)ipst,
16415 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16416 		}
16417 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16418 	}
16419 
16420 	return (err);
16421 }
16422 
16423 /*
16424  * Common routine for ppa and ifname setting. Should be called exclusive.
16425  *
16426  * Returns EINPROGRESS when mp has been consumed by queueing it on
16427  * ipx_pending_mp and the ioctl will complete in ip_rput.
16428  *
16429  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16430  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16431  * For SLIFNAME, we pass these values back to the userland.
16432  */
16433 static int
16434 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16435 {
16436 	ill_t	*ill;
16437 	ipif_t	*ipif;
16438 	ipsq_t	*ipsq;
16439 	char	*ppa_ptr;
16440 	char	*old_ptr;
16441 	char	old_char;
16442 	int	error;
16443 	ip_stack_t	*ipst;
16444 
16445 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16446 	ASSERT(q->q_next != NULL);
16447 	ASSERT(interf_name != NULL);
16448 
16449 	ill = (ill_t *)q->q_ptr;
16450 	ipst = ill->ill_ipst;
16451 
16452 	ASSERT(ill->ill_ipst != NULL);
16453 	ASSERT(ill->ill_name[0] == '\0');
16454 	ASSERT(IAM_WRITER_ILL(ill));
16455 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16456 	ASSERT(ill->ill_ppa == UINT_MAX);
16457 
16458 	ill->ill_defend_start = ill->ill_defend_count = 0;
16459 	/* The ppa is sent down by ifconfig or is chosen */
16460 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16461 		return (EINVAL);
16462 	}
16463 
16464 	/*
16465 	 * make sure ppa passed in is same as ppa in the name.
16466 	 * This check is not made when ppa == UINT_MAX in that case ppa
16467 	 * in the name could be anything. System will choose a ppa and
16468 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16469 	 */
16470 	if (*new_ppa_ptr != UINT_MAX) {
16471 		/* stoi changes the pointer */
16472 		old_ptr = ppa_ptr;
16473 		/*
16474 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16475 		 * (they don't have an externally visible ppa).  We assign one
16476 		 * here so that we can manage the interface.  Note that in
16477 		 * the past this value was always 0 for DLPI 1 drivers.
16478 		 */
16479 		if (*new_ppa_ptr == 0)
16480 			*new_ppa_ptr = stoi(&old_ptr);
16481 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16482 			return (EINVAL);
16483 	}
16484 	/*
16485 	 * terminate string before ppa
16486 	 * save char at that location.
16487 	 */
16488 	old_char = ppa_ptr[0];
16489 	ppa_ptr[0] = '\0';
16490 
16491 	ill->ill_ppa = *new_ppa_ptr;
16492 	/*
16493 	 * Finish as much work now as possible before calling ill_glist_insert
16494 	 * which makes the ill globally visible and also merges it with the
16495 	 * other protocol instance of this phyint. The remaining work is
16496 	 * done after entering the ipsq which may happen sometime later.
16497 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16498 	 */
16499 	ipif = ill->ill_ipif;
16500 
16501 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16502 	ipif_assign_seqid(ipif);
16503 
16504 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16505 		ill->ill_flags |= ILLF_IPV4;
16506 
16507 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16508 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16509 
16510 	if (ill->ill_flags & ILLF_IPV6) {
16511 
16512 		ill->ill_isv6 = B_TRUE;
16513 		ill_set_inputfn(ill);
16514 		if (ill->ill_rq != NULL) {
16515 			ill->ill_rq->q_qinfo = &iprinitv6;
16516 		}
16517 
16518 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16519 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16520 		ipif->ipif_v6subnet = ipv6_all_zeros;
16521 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16522 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16523 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16524 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16525 		/*
16526 		 * point-to-point or Non-mulicast capable
16527 		 * interfaces won't do NUD unless explicitly
16528 		 * configured to do so.
16529 		 */
16530 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16531 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16532 			ill->ill_flags |= ILLF_NONUD;
16533 		}
16534 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16535 		if (ill->ill_flags & ILLF_NOARP) {
16536 			/*
16537 			 * Note: xresolv interfaces will eventually need
16538 			 * NOARP set here as well, but that will require
16539 			 * those external resolvers to have some
16540 			 * knowledge of that flag and act appropriately.
16541 			 * Not to be changed at present.
16542 			 */
16543 			ill->ill_flags &= ~ILLF_NOARP;
16544 		}
16545 		/*
16546 		 * Set the ILLF_ROUTER flag according to the global
16547 		 * IPv6 forwarding policy.
16548 		 */
16549 		if (ipst->ips_ipv6_forward != 0)
16550 			ill->ill_flags |= ILLF_ROUTER;
16551 	} else if (ill->ill_flags & ILLF_IPV4) {
16552 		ill->ill_isv6 = B_FALSE;
16553 		ill_set_inputfn(ill);
16554 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16555 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16556 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16557 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16558 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16559 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16560 		/*
16561 		 * Set the ILLF_ROUTER flag according to the global
16562 		 * IPv4 forwarding policy.
16563 		 */
16564 		if (ipst->ips_ip_g_forward != 0)
16565 			ill->ill_flags |= ILLF_ROUTER;
16566 	}
16567 
16568 	ASSERT(ill->ill_phyint != NULL);
16569 
16570 	/*
16571 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16572 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16573 	 */
16574 	if (!ill_allocate_mibs(ill))
16575 		return (ENOMEM);
16576 
16577 	/*
16578 	 * Pick a default sap until we get the DL_INFO_ACK back from
16579 	 * the driver.
16580 	 */
16581 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16582 	    ill->ill_media->ip_m_ipv4sap;
16583 
16584 	ill->ill_ifname_pending = 1;
16585 	ill->ill_ifname_pending_err = 0;
16586 
16587 	/*
16588 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16589 	 * that were joined while this ill was not bound to the DLPI link need
16590 	 * to be recovered by ill_recover_multicast().
16591 	 */
16592 	ill->ill_need_recover_multicast = 1;
16593 
16594 	ill_refhold(ill);
16595 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16596 	if ((error = ill_glist_insert(ill, interf_name,
16597 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16598 		ill->ill_ppa = UINT_MAX;
16599 		ill->ill_name[0] = '\0';
16600 		/*
16601 		 * undo null termination done above.
16602 		 */
16603 		ppa_ptr[0] = old_char;
16604 		rw_exit(&ipst->ips_ill_g_lock);
16605 		ill_refrele(ill);
16606 		return (error);
16607 	}
16608 
16609 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16610 
16611 	/*
16612 	 * When we return the buffer pointed to by interf_name should contain
16613 	 * the same name as in ill_name.
16614 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16615 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16616 	 * so copy full name and update the ppa ptr.
16617 	 * When ppa passed in != UINT_MAX all values are correct just undo
16618 	 * null termination, this saves a bcopy.
16619 	 */
16620 	if (*new_ppa_ptr == UINT_MAX) {
16621 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16622 		*new_ppa_ptr = ill->ill_ppa;
16623 	} else {
16624 		/*
16625 		 * undo null termination done above.
16626 		 */
16627 		ppa_ptr[0] = old_char;
16628 	}
16629 
16630 	/* Let SCTP know about this ILL */
16631 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16632 
16633 	/*
16634 	 * ill_glist_insert has made the ill visible globally, and
16635 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16636 	 * we need to hold the ips_ill_g_lock across the call to enter the
16637 	 * ipsq to enforce atomicity and prevent reordering. In the event
16638 	 * the ipsq has changed, and if the new ipsq is currently busy,
16639 	 * we need to make sure that this half-completed ioctl is ahead of
16640 	 * any subsequent ioctl. We achieve this by not dropping the
16641 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16642 	 * ensuring that new ioctls can't start.
16643 	 */
16644 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16645 	    B_TRUE);
16646 
16647 	rw_exit(&ipst->ips_ill_g_lock);
16648 	ill_refrele(ill);
16649 	if (ipsq == NULL)
16650 		return (EINPROGRESS);
16651 
16652 	/*
16653 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16654 	 */
16655 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16656 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16657 	else
16658 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16659 
16660 	error = ipif_set_values_tail(ill, ipif, mp, q);
16661 	ipsq_exit(ipsq);
16662 	if (error != 0 && error != EINPROGRESS) {
16663 		/*
16664 		 * restore previous values
16665 		 */
16666 		ill->ill_isv6 = B_FALSE;
16667 		ill_set_inputfn(ill);
16668 	}
16669 	return (error);
16670 }
16671 
16672 void
16673 ipif_init(ip_stack_t *ipst)
16674 {
16675 	int i;
16676 
16677 	for (i = 0; i < MAX_G_HEADS; i++) {
16678 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16679 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16680 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16681 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16682 	}
16683 
16684 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16685 	    ill_phyint_compare_index,
16686 	    sizeof (phyint_t),
16687 	    offsetof(struct phyint, phyint_avl_by_index));
16688 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16689 	    ill_phyint_compare_name,
16690 	    sizeof (phyint_t),
16691 	    offsetof(struct phyint, phyint_avl_by_name));
16692 }
16693 
16694 /*
16695  * Save enough information so that we can recreate the IRE if
16696  * the interface goes down and then up.
16697  */
16698 void
16699 ill_save_ire(ill_t *ill, ire_t *ire)
16700 {
16701 	mblk_t	*save_mp;
16702 
16703 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16704 	if (save_mp != NULL) {
16705 		ifrt_t	*ifrt;
16706 
16707 		save_mp->b_wptr += sizeof (ifrt_t);
16708 		ifrt = (ifrt_t *)save_mp->b_rptr;
16709 		bzero(ifrt, sizeof (ifrt_t));
16710 		ifrt->ifrt_type = ire->ire_type;
16711 		if (ire->ire_ipversion == IPV4_VERSION) {
16712 			ASSERT(!ill->ill_isv6);
16713 			ifrt->ifrt_addr = ire->ire_addr;
16714 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16715 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16716 			ifrt->ifrt_mask = ire->ire_mask;
16717 		} else {
16718 			ASSERT(ill->ill_isv6);
16719 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16720 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16721 			mutex_enter(&ire->ire_lock);
16722 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16723 			mutex_exit(&ire->ire_lock);
16724 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16725 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16726 		}
16727 		ifrt->ifrt_flags = ire->ire_flags;
16728 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16729 		mutex_enter(&ill->ill_saved_ire_lock);
16730 		save_mp->b_cont = ill->ill_saved_ire_mp;
16731 		ill->ill_saved_ire_mp = save_mp;
16732 		ill->ill_saved_ire_cnt++;
16733 		mutex_exit(&ill->ill_saved_ire_lock);
16734 	}
16735 }
16736 
16737 /*
16738  * Remove one entry from ill_saved_ire_mp.
16739  */
16740 void
16741 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16742 {
16743 	mblk_t	**mpp;
16744 	mblk_t	*mp;
16745 	ifrt_t	*ifrt;
16746 
16747 	/* Remove from ill_saved_ire_mp list if it is there */
16748 	mutex_enter(&ill->ill_saved_ire_lock);
16749 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16750 	    mpp = &(*mpp)->b_cont) {
16751 		in6_addr_t	gw_addr_v6;
16752 
16753 		/*
16754 		 * On a given ill, the tuple of address, gateway, mask,
16755 		 * ire_type, and zoneid is unique for each saved IRE.
16756 		 */
16757 		mp = *mpp;
16758 		ifrt = (ifrt_t *)mp->b_rptr;
16759 		/* ire_gateway_addr_v6 can change - need lock */
16760 		mutex_enter(&ire->ire_lock);
16761 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16762 		mutex_exit(&ire->ire_lock);
16763 
16764 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16765 		    ifrt->ifrt_type != ire->ire_type)
16766 			continue;
16767 
16768 		if (ill->ill_isv6 ?
16769 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16770 		    &ire->ire_addr_v6) &&
16771 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16772 		    &gw_addr_v6) &&
16773 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16774 		    &ire->ire_mask_v6)) :
16775 		    (ifrt->ifrt_addr == ire->ire_addr &&
16776 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16777 		    ifrt->ifrt_mask == ire->ire_mask)) {
16778 			*mpp = mp->b_cont;
16779 			ill->ill_saved_ire_cnt--;
16780 			freeb(mp);
16781 			break;
16782 		}
16783 	}
16784 	mutex_exit(&ill->ill_saved_ire_lock);
16785 }
16786 
16787 /*
16788  * IP multirouting broadcast routes handling
16789  * Append CGTP broadcast IREs to regular ones created
16790  * at ifconfig time.
16791  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16792  * the destination and the gateway are broadcast addresses.
16793  * The caller has verified that the destination is an IRE_BROADCAST and that
16794  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16795  * we create a MULTIRT IRE_BROADCAST.
16796  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16797  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16798  */
16799 static void
16800 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16801 {
16802 	ire_t *ire_prim;
16803 
16804 	ASSERT(ire != NULL);
16805 
16806 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16807 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16808 	    NULL);
16809 	if (ire_prim != NULL) {
16810 		/*
16811 		 * We are in the special case of broadcasts for
16812 		 * CGTP. We add an IRE_BROADCAST that holds
16813 		 * the RTF_MULTIRT flag, the destination
16814 		 * address and the low level
16815 		 * info of ire_prim. In other words, CGTP
16816 		 * broadcast is added to the redundant ipif.
16817 		 */
16818 		ill_t *ill_prim;
16819 		ire_t  *bcast_ire;
16820 
16821 		ill_prim = ire_prim->ire_ill;
16822 
16823 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16824 		    (void *)ire_prim, (void *)ill_prim));
16825 
16826 		bcast_ire = ire_create(
16827 		    (uchar_t *)&ire->ire_addr,
16828 		    (uchar_t *)&ip_g_all_ones,
16829 		    (uchar_t *)&ire->ire_gateway_addr,
16830 		    IRE_BROADCAST,
16831 		    ill_prim,
16832 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16833 		    ire->ire_flags | RTF_KERNEL,
16834 		    NULL,
16835 		    ipst);
16836 
16837 		/*
16838 		 * Here we assume that ire_add does head insertion so that
16839 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16840 		 */
16841 		if (bcast_ire != NULL) {
16842 			if (ire->ire_flags & RTF_SETSRC) {
16843 				bcast_ire->ire_setsrc_addr =
16844 				    ire->ire_setsrc_addr;
16845 			}
16846 			bcast_ire = ire_add(bcast_ire);
16847 			if (bcast_ire != NULL) {
16848 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16849 				    "added bcast_ire %p\n",
16850 				    (void *)bcast_ire));
16851 
16852 				ill_save_ire(ill_prim, bcast_ire);
16853 				ire_refrele(bcast_ire);
16854 			}
16855 		}
16856 		ire_refrele(ire_prim);
16857 	}
16858 }
16859 
16860 /*
16861  * IP multirouting broadcast routes handling
16862  * Remove the broadcast ire.
16863  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16864  * the destination and the gateway are broadcast addresses.
16865  * The caller has only verified that RTF_MULTIRT was set. We check
16866  * that the destination is broadcast and that the gateway is a broadcast
16867  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16868  */
16869 static void
16870 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16871 {
16872 	ASSERT(ire != NULL);
16873 
16874 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16875 		ire_t *ire_prim;
16876 
16877 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16878 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16879 		    ipst, NULL);
16880 		if (ire_prim != NULL) {
16881 			ill_t *ill_prim;
16882 			ire_t  *bcast_ire;
16883 
16884 			ill_prim = ire_prim->ire_ill;
16885 
16886 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16887 			    "ire_prim %p, ill_prim %p\n",
16888 			    (void *)ire_prim, (void *)ill_prim));
16889 
16890 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16891 			    ire->ire_gateway_addr, IRE_BROADCAST,
16892 			    ill_prim, ALL_ZONES, NULL,
16893 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16894 			    MATCH_IRE_MASK, 0, ipst, NULL);
16895 
16896 			if (bcast_ire != NULL) {
16897 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16898 				    "looked up bcast_ire %p\n",
16899 				    (void *)bcast_ire));
16900 				ill_remove_saved_ire(bcast_ire->ire_ill,
16901 				    bcast_ire);
16902 				ire_delete(bcast_ire);
16903 				ire_refrele(bcast_ire);
16904 			}
16905 			ire_refrele(ire_prim);
16906 		}
16907 	}
16908 }
16909 
16910 /*
16911  * Derive an interface id from the link layer address.
16912  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16913  */
16914 static void
16915 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16916 {
16917 	char		*addr;
16918 
16919 	/*
16920 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16921 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16922 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16923 	 * interface ID on IPv6 interfaces above links that actually have real
16924 	 * Ethernet addresses.
16925 	 */
16926 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16927 		/* Form EUI-64 like address */
16928 		addr = (char *)&v6addr->s6_addr32[2];
16929 		bcopy(ill->ill_phys_addr, addr, 3);
16930 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16931 		addr[3] = (char)0xff;
16932 		addr[4] = (char)0xfe;
16933 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16934 	}
16935 }
16936 
16937 /* ARGSUSED */
16938 static void
16939 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16940 {
16941 }
16942 
16943 typedef struct ipmp_ifcookie {
16944 	uint32_t	ic_hostid;
16945 	char		ic_ifname[LIFNAMSIZ];
16946 	char		ic_zonename[ZONENAME_MAX];
16947 } ipmp_ifcookie_t;
16948 
16949 /*
16950  * Construct a pseudo-random interface ID for the IPMP interface that's both
16951  * predictable and (almost) guaranteed to be unique.
16952  */
16953 static void
16954 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16955 {
16956 	zone_t		*zp;
16957 	uint8_t		*addr;
16958 	uchar_t		hash[16];
16959 	ulong_t 	hostid;
16960 	MD5_CTX		ctx;
16961 	ipmp_ifcookie_t	ic = { 0 };
16962 
16963 	ASSERT(IS_IPMP(ill));
16964 
16965 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16966 	ic.ic_hostid = htonl((uint32_t)hostid);
16967 
16968 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16969 
16970 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16971 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16972 		zone_rele(zp);
16973 	}
16974 
16975 	MD5Init(&ctx);
16976 	MD5Update(&ctx, &ic, sizeof (ic));
16977 	MD5Final(hash, &ctx);
16978 
16979 	/*
16980 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16981 	 */
16982 	addr = &v6addr->s6_addr8[8];
16983 	bcopy(hash + 8, addr, sizeof (uint64_t));
16984 	addr[0] &= ~0x2;				/* set local bit */
16985 }
16986 
16987 /*
16988  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16989  */
16990 static void
16991 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16992 {
16993 	phyint_t *phyi = ill->ill_phyint;
16994 
16995 	/*
16996 	 * Check PHYI_MULTI_BCAST and length of physical
16997 	 * address to determine if we use the mapping or the
16998 	 * broadcast address.
16999 	 */
17000 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17001 	    ill->ill_phys_addr_length != ETHERADDRL) {
17002 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17003 		return;
17004 	}
17005 	m_physaddr[0] = 0x33;
17006 	m_physaddr[1] = 0x33;
17007 	m_physaddr[2] = m_ip6addr[12];
17008 	m_physaddr[3] = m_ip6addr[13];
17009 	m_physaddr[4] = m_ip6addr[14];
17010 	m_physaddr[5] = m_ip6addr[15];
17011 }
17012 
17013 /*
17014  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17015  */
17016 static void
17017 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17018 {
17019 	phyint_t *phyi = ill->ill_phyint;
17020 
17021 	/*
17022 	 * Check PHYI_MULTI_BCAST and length of physical
17023 	 * address to determine if we use the mapping or the
17024 	 * broadcast address.
17025 	 */
17026 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17027 	    ill->ill_phys_addr_length != ETHERADDRL) {
17028 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17029 		return;
17030 	}
17031 	m_physaddr[0] = 0x01;
17032 	m_physaddr[1] = 0x00;
17033 	m_physaddr[2] = 0x5e;
17034 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17035 	m_physaddr[4] = m_ipaddr[2];
17036 	m_physaddr[5] = m_ipaddr[3];
17037 }
17038 
17039 /* ARGSUSED */
17040 static void
17041 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17042 {
17043 	/*
17044 	 * for the MULTI_BCAST case and other cases when we want to
17045 	 * use the link-layer broadcast address for multicast.
17046 	 */
17047 	uint8_t	*bphys_addr;
17048 	dl_unitdata_req_t *dlur;
17049 
17050 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17051 	if (ill->ill_sap_length < 0) {
17052 		bphys_addr = (uchar_t *)dlur +
17053 		    dlur->dl_dest_addr_offset;
17054 	} else  {
17055 		bphys_addr = (uchar_t *)dlur +
17056 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17057 	}
17058 
17059 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17060 }
17061 
17062 /*
17063  * Derive IPoIB interface id from the link layer address.
17064  */
17065 static void
17066 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17067 {
17068 	char		*addr;
17069 
17070 	ASSERT(ill->ill_phys_addr_length == 20);
17071 	addr = (char *)&v6addr->s6_addr32[2];
17072 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17073 	/*
17074 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17075 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17076 	 * rules. In these cases, the IBA considers these GUIDs to be in
17077 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17078 	 * required; vendors are required not to assign global EUI-64's
17079 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17080 	 * of the interface identifier. Whether the GUID is in modified
17081 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17082 	 * bit set to 1.
17083 	 */
17084 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17085 }
17086 
17087 /*
17088  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17089  * Note on mapping from multicast IP addresses to IPoIB multicast link
17090  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17091  * The format of an IPoIB multicast address is:
17092  *
17093  *  4 byte QPN      Scope Sign.  Pkey
17094  * +--------------------------------------------+
17095  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17096  * +--------------------------------------------+
17097  *
17098  * The Scope and Pkey components are properties of the IBA port and
17099  * network interface. They can be ascertained from the broadcast address.
17100  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17101  */
17102 static void
17103 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17104 {
17105 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17106 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17107 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17108 	uint8_t	*bphys_addr;
17109 	dl_unitdata_req_t *dlur;
17110 
17111 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17112 
17113 	/*
17114 	 * RFC 4391: IPv4 MGID is 28-bit long.
17115 	 */
17116 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17117 	m_physaddr[17] = m_ipaddr[1];
17118 	m_physaddr[18] = m_ipaddr[2];
17119 	m_physaddr[19] = m_ipaddr[3];
17120 
17121 
17122 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17123 	if (ill->ill_sap_length < 0) {
17124 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17125 	} else  {
17126 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17127 		    ill->ill_sap_length;
17128 	}
17129 	/*
17130 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17131 	 */
17132 	m_physaddr[5] = bphys_addr[5];
17133 	m_physaddr[8] = bphys_addr[8];
17134 	m_physaddr[9] = bphys_addr[9];
17135 }
17136 
17137 static void
17138 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17139 {
17140 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17141 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17142 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17143 	uint8_t	*bphys_addr;
17144 	dl_unitdata_req_t *dlur;
17145 
17146 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17147 
17148 	/*
17149 	 * RFC 4391: IPv4 MGID is 80-bit long.
17150 	 */
17151 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17152 
17153 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17154 	if (ill->ill_sap_length < 0) {
17155 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17156 	} else  {
17157 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17158 		    ill->ill_sap_length;
17159 	}
17160 	/*
17161 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17162 	 */
17163 	m_physaddr[5] = bphys_addr[5];
17164 	m_physaddr[8] = bphys_addr[8];
17165 	m_physaddr[9] = bphys_addr[9];
17166 }
17167 
17168 /*
17169  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17170  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17171  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17172  * of RFC4213.
17173  */
17174 static void
17175 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17176 {
17177 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17178 	v6addr->s6_addr32[2] = 0;
17179 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17180 }
17181 
17182 /*
17183  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17184  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17185  * id.
17186  */
17187 static void
17188 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17189 {
17190 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17191 
17192 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17193 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17194 }
17195 
17196 static void
17197 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17198 {
17199 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17200 }
17201 
17202 static void
17203 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17204 {
17205 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17206 }
17207 
17208 static void
17209 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17210 {
17211 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17212 }
17213 
17214 static void
17215 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17216 {
17217 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17218 }
17219 
17220 /*
17221  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17222  * Returns an held ill, or NULL.
17223  */
17224 ill_t *
17225 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17226     ip_stack_t *ipst)
17227 {
17228 	ill_t	*ill;
17229 	ipif_t	*ipif;
17230 
17231 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17232 	if (ill == NULL)
17233 		return (NULL);
17234 
17235 	mutex_enter(&ill->ill_lock);
17236 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17237 		if (IPIF_IS_CONDEMNED(ipif))
17238 			continue;
17239 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17240 		    ipif->ipif_zoneid != ALL_ZONES)
17241 			continue;
17242 
17243 		mutex_exit(&ill->ill_lock);
17244 		return (ill);
17245 	}
17246 	mutex_exit(&ill->ill_lock);
17247 	ill_refrele(ill);
17248 	return (NULL);
17249 }
17250 
17251 /*
17252  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17253  * If a pointer to an ipif_t is returned then the caller will need to do
17254  * an ill_refrele().
17255  */
17256 ipif_t *
17257 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17258     ip_stack_t *ipst)
17259 {
17260 	ipif_t *ipif;
17261 	ill_t *ill;
17262 
17263 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17264 	if (ill == NULL)
17265 		return (NULL);
17266 
17267 	mutex_enter(&ill->ill_lock);
17268 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17269 		mutex_exit(&ill->ill_lock);
17270 		ill_refrele(ill);
17271 		return (NULL);
17272 	}
17273 
17274 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17275 		if (!IPIF_CAN_LOOKUP(ipif))
17276 			continue;
17277 		if (lifidx == ipif->ipif_id) {
17278 			ipif_refhold_locked(ipif);
17279 			break;
17280 		}
17281 	}
17282 
17283 	mutex_exit(&ill->ill_lock);
17284 	ill_refrele(ill);
17285 	return (ipif);
17286 }
17287 
17288 /*
17289  * Set ill_inputfn based on the current know state.
17290  * This needs to be called when any of the factors taken into
17291  * account changes.
17292  */
17293 void
17294 ill_set_inputfn(ill_t *ill)
17295 {
17296 	ip_stack_t	*ipst = ill->ill_ipst;
17297 
17298 	if (ill->ill_isv6) {
17299 		if (is_system_labeled())
17300 			ill->ill_inputfn = ill_input_full_v6;
17301 		else
17302 			ill->ill_inputfn = ill_input_short_v6;
17303 	} else {
17304 		if (is_system_labeled())
17305 			ill->ill_inputfn = ill_input_full_v4;
17306 		else if (ill->ill_dhcpinit != 0)
17307 			ill->ill_inputfn = ill_input_full_v4;
17308 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17309 		    != NULL)
17310 			ill->ill_inputfn = ill_input_full_v4;
17311 		else if (ipst->ips_ip_cgtp_filter &&
17312 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17313 			ill->ill_inputfn = ill_input_full_v4;
17314 		else
17315 			ill->ill_inputfn = ill_input_short_v4;
17316 	}
17317 }
17318 
17319 /*
17320  * Re-evaluate ill_inputfn for all the IPv4 ills.
17321  * Used when RSVP and CGTP comes and goes.
17322  */
17323 void
17324 ill_set_inputfn_all(ip_stack_t *ipst)
17325 {
17326 	ill_walk_context_t	ctx;
17327 	ill_t			*ill;
17328 
17329 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17330 	ill = ILL_START_WALK_V4(&ctx, ipst);
17331 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17332 		ill_set_inputfn(ill);
17333 
17334 	rw_exit(&ipst->ips_ill_g_lock);
17335 }
17336 
17337 /*
17338  * Set the physical address information for `ill' to the contents of the
17339  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17340  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17341  * EINPROGRESS will be returned.
17342  */
17343 int
17344 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17345 {
17346 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17347 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17348 
17349 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17350 
17351 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17352 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17353 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17354 		/* Changing DL_IPV6_TOKEN is not yet supported */
17355 		return (0);
17356 	}
17357 
17358 	/*
17359 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17360 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17361 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17362 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17363 	 */
17364 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17365 		freemsg(mp);
17366 		return (ENOMEM);
17367 	}
17368 
17369 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17370 	mutex_enter(&ill->ill_lock);
17371 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17372 	/* no more nce addition allowed */
17373 	mutex_exit(&ill->ill_lock);
17374 
17375 	/*
17376 	 * If we can quiesce the ill, then set the address.  If not, then
17377 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17378 	 */
17379 	ill_down_ipifs(ill, B_TRUE);
17380 	mutex_enter(&ill->ill_lock);
17381 	if (!ill_is_quiescent(ill)) {
17382 		/* call cannot fail since `conn_t *' argument is NULL */
17383 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17384 		    mp, ILL_DOWN);
17385 		mutex_exit(&ill->ill_lock);
17386 		return (EINPROGRESS);
17387 	}
17388 	mutex_exit(&ill->ill_lock);
17389 
17390 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17391 	return (0);
17392 }
17393 
17394 /*
17395  * Once the ill associated with `q' has quiesced, set its physical address
17396  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17397  * are passed (linked by b_cont), since we sometimes need to save two distinct
17398  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17399  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17400  * is quiesced, we know any stale nce's with the old address information have
17401  * already been removed, so we don't need to call nce_flush().
17402  */
17403 /* ARGSUSED */
17404 static void
17405 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17406 {
17407 	ill_t		*ill = q->q_ptr;
17408 	mblk_t		*addrmp2 = unlinkb(addrmp);
17409 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17410 	uint_t		addrlen, addroff;
17411 	int		status;
17412 
17413 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17414 
17415 	addroff	= dlindp->dl_addr_offset;
17416 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17417 
17418 	switch (dlindp->dl_data) {
17419 	case DL_IPV6_LINK_LAYER_ADDR:
17420 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17421 		freemsg(addrmp2);
17422 		break;
17423 
17424 	case DL_CURR_DEST_ADDR:
17425 		freemsg(ill->ill_dest_addr_mp);
17426 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17427 		ill->ill_dest_addr_mp = addrmp;
17428 		if (ill->ill_isv6) {
17429 			ill_setdesttoken(ill);
17430 			ipif_setdestlinklocal(ill->ill_ipif);
17431 		}
17432 		freemsg(addrmp2);
17433 		break;
17434 
17435 	case DL_CURR_PHYS_ADDR:
17436 		freemsg(ill->ill_phys_addr_mp);
17437 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17438 		ill->ill_phys_addr_mp = addrmp;
17439 		ill->ill_phys_addr_length = addrlen;
17440 		if (ill->ill_isv6)
17441 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17442 		else
17443 			freemsg(addrmp2);
17444 		if (ill->ill_isv6) {
17445 			ill_setdefaulttoken(ill);
17446 			ipif_setlinklocal(ill->ill_ipif);
17447 		}
17448 		break;
17449 	default:
17450 		ASSERT(0);
17451 	}
17452 
17453 	/*
17454 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17455 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17456 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17457 	 * brought up.
17458 	 */
17459 	status = ill_up_ipifs(ill, q, addrmp);
17460 	mutex_enter(&ill->ill_lock);
17461 	if (ill->ill_dl_up)
17462 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17463 	mutex_exit(&ill->ill_lock);
17464 	if (status != EINPROGRESS)
17465 		ipsq_current_finish(ipsq);
17466 }
17467 
17468 /*
17469  * Helper routine for setting the ill_nd_lla fields.
17470  */
17471 void
17472 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17473 {
17474 	freemsg(ill->ill_nd_lla_mp);
17475 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17476 	ill->ill_nd_lla_mp = ndmp;
17477 	ill->ill_nd_lla_len = addrlen;
17478 }
17479 
17480 /*
17481  * Replumb the ill.
17482  */
17483 int
17484 ill_replumb(ill_t *ill, mblk_t *mp)
17485 {
17486 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17487 
17488 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17489 
17490 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17491 
17492 	mutex_enter(&ill->ill_lock);
17493 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17494 	/* no more nce addition allowed */
17495 	mutex_exit(&ill->ill_lock);
17496 
17497 	/*
17498 	 * If we can quiesce the ill, then continue.  If not, then
17499 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17500 	 */
17501 	ill_down_ipifs(ill, B_FALSE);
17502 
17503 	mutex_enter(&ill->ill_lock);
17504 	if (!ill_is_quiescent(ill)) {
17505 		/* call cannot fail since `conn_t *' argument is NULL */
17506 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17507 		    mp, ILL_DOWN);
17508 		mutex_exit(&ill->ill_lock);
17509 		return (EINPROGRESS);
17510 	}
17511 	mutex_exit(&ill->ill_lock);
17512 
17513 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17514 	return (0);
17515 }
17516 
17517 /* ARGSUSED */
17518 static void
17519 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17520 {
17521 	ill_t *ill = q->q_ptr;
17522 	int err;
17523 	conn_t *connp = NULL;
17524 
17525 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17526 	freemsg(ill->ill_replumb_mp);
17527 	ill->ill_replumb_mp = copyb(mp);
17528 
17529 	if (ill->ill_replumb_mp == NULL) {
17530 		/* out of memory */
17531 		ipsq_current_finish(ipsq);
17532 		return;
17533 	}
17534 
17535 	mutex_enter(&ill->ill_lock);
17536 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17537 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17538 	mutex_exit(&ill->ill_lock);
17539 
17540 	if (!ill->ill_up_ipifs) {
17541 		/* already closing */
17542 		ipsq_current_finish(ipsq);
17543 		return;
17544 	}
17545 	ill->ill_replumbing = 1;
17546 	err = ill_down_ipifs_tail(ill);
17547 
17548 	/*
17549 	 * Successfully quiesced and brought down the interface, now we send
17550 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17551 	 * DL_NOTE_REPLUMB message.
17552 	 */
17553 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17554 	    DL_NOTIFY_CONF);
17555 	ASSERT(mp != NULL);
17556 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17557 	    DL_NOTE_REPLUMB_DONE;
17558 	ill_dlpi_send(ill, mp);
17559 
17560 	/*
17561 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17562 	 * streams have to be unbound. When all the DLPI exchanges are done,
17563 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17564 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17565 	 * arp_bringup_done().
17566 	 */
17567 	ASSERT(ill->ill_replumb_mp != NULL);
17568 	if (err == EINPROGRESS)
17569 		return;
17570 	else
17571 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17572 	ASSERT(connp == NULL);
17573 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17574 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17575 		return;
17576 	}
17577 	ipsq_current_finish(ipsq);
17578 }
17579 
17580 /*
17581  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17582  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17583  * as per the ioctl.  On failure, an errno is returned.
17584  */
17585 static int
17586 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17587 {
17588 	int rval;
17589 	struct strioctl iocb;
17590 
17591 	iocb.ic_cmd = cmd;
17592 	iocb.ic_timout = 15;
17593 	iocb.ic_len = bufsize;
17594 	iocb.ic_dp = buf;
17595 
17596 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17597 }
17598 
17599 /*
17600  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17601  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17602  */
17603 static int
17604 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17605     uint_t *bufsizep, cred_t *cr)
17606 {
17607 	int err;
17608 	struct lifnum lifn;
17609 
17610 	bzero(&lifn, sizeof (lifn));
17611 	lifn.lifn_family = af;
17612 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17613 
17614 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17615 		return (err);
17616 
17617 	/*
17618 	 * Pad the interface count to account for additional interfaces that
17619 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17620 	 */
17621 	lifn.lifn_count += 4;
17622 	bzero(lifcp, sizeof (*lifcp));
17623 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17624 	lifcp->lifc_family = af;
17625 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17626 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17627 
17628 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17629 	if (err != 0) {
17630 		kmem_free(lifcp->lifc_buf, *bufsizep);
17631 		return (err);
17632 	}
17633 
17634 	return (0);
17635 }
17636 
17637 /*
17638  * Helper for ip_interface_cleanup() that removes the loopback interface.
17639  */
17640 static void
17641 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17642 {
17643 	int err;
17644 	struct lifreq lifr;
17645 
17646 	bzero(&lifr, sizeof (lifr));
17647 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17648 
17649 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17650 	if (err != 0) {
17651 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17652 		    "error %d\n", isv6 ? "v6" : "v4", err));
17653 	}
17654 }
17655 
17656 /*
17657  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17658  * groups and that IPMP data addresses are down.  These conditions must be met
17659  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17660  */
17661 static void
17662 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17663 {
17664 	int af = isv6 ? AF_INET6 : AF_INET;
17665 	int i, nifs;
17666 	int err;
17667 	uint_t bufsize;
17668 	uint_t lifrsize = sizeof (struct lifreq);
17669 	struct lifconf lifc;
17670 	struct lifreq *lifrp;
17671 
17672 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17673 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17674 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17675 		return;
17676 	}
17677 
17678 	nifs = lifc.lifc_len / lifrsize;
17679 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17680 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17681 		if (err != 0) {
17682 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17683 			    "flags: error %d", lifrp->lifr_name, err);
17684 			continue;
17685 		}
17686 
17687 		if (lifrp->lifr_flags & IFF_IPMP) {
17688 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17689 				continue;
17690 
17691 			lifrp->lifr_flags &= ~IFF_UP;
17692 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17693 			if (err != 0) {
17694 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17695 				    "bring down (error %d); IPMP interface may "
17696 				    "not be shutdown", lifrp->lifr_name, err);
17697 			}
17698 
17699 			/*
17700 			 * Check if IFF_DUPLICATE is still set -- and if so,
17701 			 * reset the address to clear it.
17702 			 */
17703 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17704 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17705 				continue;
17706 
17707 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17708 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17709 			    lifrp, lifrsize, cr)) != 0) {
17710 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17711 				    "reset DAD (error %d); IPMP interface may "
17712 				    "not be shutdown", lifrp->lifr_name, err);
17713 			}
17714 			continue;
17715 		}
17716 
17717 		lifrp->lifr_groupname[0] = '\0';
17718 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17719 		if (err != 0) {
17720 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17721 			    "IPMP group (error %d); associated IPMP interface "
17722 			    "may not be shutdown", lifrp->lifr_name, err);
17723 			continue;
17724 		}
17725 	}
17726 
17727 	kmem_free(lifc.lifc_buf, bufsize);
17728 }
17729 
17730 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17731 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17732 
17733 /*
17734  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17735  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17736  * when the user-level processes in the zone are killed and the latter are
17737  * cleaned up by str_stack_shutdown().
17738  */
17739 void
17740 ip_interface_cleanup(ip_stack_t *ipst)
17741 {
17742 	ldi_handle_t	lh;
17743 	ldi_ident_t	li;
17744 	cred_t		*cr;
17745 	int		err;
17746 	int		i;
17747 	char		*devs[] = { UDP6DEV, UDPDEV };
17748 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17749 
17750 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17751 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17752 		    " error %d", err);
17753 		return;
17754 	}
17755 
17756 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17757 	ASSERT(cr != NULL);
17758 
17759 	/*
17760 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17761 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17762 	 * the loop.)
17763 	 */
17764 	for (i = 0; i < 2; i++) {
17765 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17766 		if (err != 0) {
17767 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17768 			    " error %d", devs[i], err);
17769 			continue;
17770 		}
17771 
17772 		ip_loopback_removeif(lh, i == 0, cr);
17773 		ip_ipmp_cleanup(lh, i == 0, cr);
17774 
17775 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17776 	}
17777 
17778 	ldi_ident_release(li);
17779 	crfree(cr);
17780 }
17781 
17782 /*
17783  * This needs to be in-sync with nic_event_t definition
17784  */
17785 static const char *
17786 ill_hook_event2str(nic_event_t event)
17787 {
17788 	switch (event) {
17789 	case NE_PLUMB:
17790 		return ("PLUMB");
17791 	case NE_UNPLUMB:
17792 		return ("UNPLUMB");
17793 	case NE_UP:
17794 		return ("UP");
17795 	case NE_DOWN:
17796 		return ("DOWN");
17797 	case NE_ADDRESS_CHANGE:
17798 		return ("ADDRESS_CHANGE");
17799 	case NE_LIF_UP:
17800 		return ("LIF_UP");
17801 	case NE_LIF_DOWN:
17802 		return ("LIF_DOWN");
17803 	case NE_IFINDEX_CHANGE:
17804 		return ("IFINDEX_CHANGE");
17805 	default:
17806 		return ("UNKNOWN");
17807 	}
17808 }
17809 
17810 void
17811 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17812     nic_event_data_t data, size_t datalen)
17813 {
17814 	ip_stack_t		*ipst = ill->ill_ipst;
17815 	hook_nic_event_int_t	*info;
17816 	const char		*str = NULL;
17817 
17818 	/* create a new nic event info */
17819 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17820 		goto fail;
17821 
17822 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17823 	info->hnei_event.hne_lif = lif;
17824 	info->hnei_event.hne_event = event;
17825 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17826 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17827 	info->hnei_event.hne_data = NULL;
17828 	info->hnei_event.hne_datalen = 0;
17829 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17830 
17831 	if (data != NULL && datalen != 0) {
17832 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17833 		if (info->hnei_event.hne_data == NULL)
17834 			goto fail;
17835 		bcopy(data, info->hnei_event.hne_data, datalen);
17836 		info->hnei_event.hne_datalen = datalen;
17837 	}
17838 
17839 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17840 	    DDI_NOSLEEP) == DDI_SUCCESS)
17841 		return;
17842 
17843 fail:
17844 	if (info != NULL) {
17845 		if (info->hnei_event.hne_data != NULL) {
17846 			kmem_free(info->hnei_event.hne_data,
17847 			    info->hnei_event.hne_datalen);
17848 		}
17849 		kmem_free(info, sizeof (hook_nic_event_t));
17850 	}
17851 	str = ill_hook_event2str(event);
17852 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17853 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17854 }
17855 
17856 static int
17857 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17858 {
17859 	int		err = 0;
17860 	const in_addr_t	*addr = NULL;
17861 	nce_t		*nce = NULL;
17862 	ill_t		*ill = ipif->ipif_ill;
17863 	ill_t		*bound_ill;
17864 	boolean_t	added_ipif = B_FALSE;
17865 	uint16_t	state;
17866 	uint16_t	flags;
17867 
17868 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17869 	    ill_t *, ill, ipif_t *, ipif);
17870 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17871 		addr = &ipif->ipif_lcl_addr;
17872 	}
17873 
17874 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17875 		if (res_act != Res_act_initial)
17876 			return (EINVAL);
17877 	}
17878 
17879 	if (addr != NULL) {
17880 		ipmp_illgrp_t	*illg = ill->ill_grp;
17881 
17882 		/* add unicast nce for the local addr */
17883 
17884 		if (IS_IPMP(ill)) {
17885 			/*
17886 			 * If we're here via ipif_up(), then the ipif
17887 			 * won't be bound yet -- add it to the group,
17888 			 * which will bind it if possible. (We would
17889 			 * add it in ipif_up(), but deleting on failure
17890 			 * there is gruesome.)  If we're here via
17891 			 * ipmp_ill_bind_ipif(), then the ipif has
17892 			 * already been added to the group and we
17893 			 * just need to use the binding.
17894 			 */
17895 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17896 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17897 				if (bound_ill == NULL) {
17898 					/*
17899 					 * We couldn't bind the ipif to an ill
17900 					 * yet, so we have nothing to publish.
17901 					 * Mark the address as ready and return.
17902 					 */
17903 					ipif->ipif_addr_ready = 1;
17904 					return (0);
17905 				}
17906 				added_ipif = B_TRUE;
17907 			}
17908 		} else {
17909 			bound_ill = ill;
17910 		}
17911 
17912 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17913 		    NCE_F_NONUD);
17914 		/*
17915 		 * If this is an initial bring-up (or the ipif was never
17916 		 * completely brought up), do DAD.  Otherwise, we're here
17917 		 * because IPMP has rebound an address to this ill: send
17918 		 * unsolicited advertisements (ARP announcements) to
17919 		 * inform others.
17920 		 */
17921 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17922 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17923 		} else {
17924 			state = ND_REACHABLE;
17925 			flags |= NCE_F_UNSOL_ADV;
17926 		}
17927 
17928 retry:
17929 		err = nce_lookup_then_add_v4(ill,
17930 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17931 		    addr, flags, state, &nce);
17932 
17933 		/*
17934 		 * note that we may encounter EEXIST if we are moving
17935 		 * the nce as a result of a rebind operation.
17936 		 */
17937 		switch (err) {
17938 		case 0:
17939 			ipif->ipif_added_nce = 1;
17940 			nce->nce_ipif_cnt++;
17941 			break;
17942 		case EEXIST:
17943 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17944 			    ill->ill_name));
17945 			if (!NCE_MYADDR(nce->nce_common)) {
17946 				/*
17947 				 * A leftover nce from before this address
17948 				 * existed
17949 				 */
17950 				ncec_delete(nce->nce_common);
17951 				nce_refrele(nce);
17952 				nce = NULL;
17953 				goto retry;
17954 			}
17955 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17956 				nce_refrele(nce);
17957 				nce = NULL;
17958 				ip1dbg(("ipif_arp_up: NCE already exists "
17959 				    "for %s:%u\n", ill->ill_name,
17960 				    ipif->ipif_id));
17961 				goto arp_up_done;
17962 			}
17963 			/*
17964 			 * Duplicate local addresses are permissible for
17965 			 * IPIF_POINTOPOINT interfaces which will get marked
17966 			 * IPIF_UNNUMBERED later in
17967 			 * ip_addr_availability_check().
17968 			 *
17969 			 * The nce_ipif_cnt field tracks the number of
17970 			 * ipifs that have nce_addr as their local address.
17971 			 */
17972 			ipif->ipif_addr_ready = 1;
17973 			ipif->ipif_added_nce = 1;
17974 			nce->nce_ipif_cnt++;
17975 			err = 0;
17976 			break;
17977 		default:
17978 			ASSERT(nce == NULL);
17979 			goto arp_up_done;
17980 		}
17981 		if (arp_no_defense) {
17982 			if ((ipif->ipif_flags & IPIF_UP) &&
17983 			    !ipif->ipif_addr_ready)
17984 				ipif_up_notify(ipif);
17985 			ipif->ipif_addr_ready = 1;
17986 		}
17987 	} else {
17988 		/* zero address. nothing to publish */
17989 		ipif->ipif_addr_ready = 1;
17990 	}
17991 	if (nce != NULL)
17992 		nce_refrele(nce);
17993 arp_up_done:
17994 	if (added_ipif && err != 0)
17995 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17996 	return (err);
17997 }
17998 
17999 int
18000 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18001 {
18002 	int 		err = 0;
18003 	ill_t 		*ill = ipif->ipif_ill;
18004 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18005 
18006 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18007 	    ill_t *, ill, ipif_t *, ipif);
18008 
18009 	/*
18010 	 * need to bring up ARP or setup mcast mapping only
18011 	 * when the first interface is coming UP.
18012 	 */
18013 	first_interface = (ill->ill_ipif_up_count == 0 &&
18014 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18015 
18016 	if (res_act == Res_act_initial && first_interface) {
18017 		/*
18018 		 * Send ATTACH + BIND
18019 		 */
18020 		err = arp_ll_up(ill);
18021 		if (err != EINPROGRESS && err != 0)
18022 			return (err);
18023 
18024 		/*
18025 		 * Add NCE for local address. Start DAD.
18026 		 * we'll wait to hear that DAD has finished
18027 		 * before using the interface.
18028 		 */
18029 		if (err == EINPROGRESS)
18030 			wait_for_dlpi = B_TRUE;
18031 	}
18032 
18033 	if (!wait_for_dlpi)
18034 		(void) ipif_arp_up_done_tail(ipif, res_act);
18035 
18036 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18037 }
18038 
18039 /*
18040  * Finish processing of "arp_up" after all the DLPI message
18041  * exchanges have completed between arp and the driver.
18042  */
18043 void
18044 arp_bringup_done(ill_t *ill, int err)
18045 {
18046 	mblk_t	*mp1;
18047 	ipif_t  *ipif;
18048 	conn_t *connp = NULL;
18049 	ipsq_t	*ipsq;
18050 	queue_t *q;
18051 
18052 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18053 
18054 	ASSERT(IAM_WRITER_ILL(ill));
18055 
18056 	ipsq = ill->ill_phyint->phyint_ipsq;
18057 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18058 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18059 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18060 	if (mp1 == NULL) /* bringup was aborted by the user */
18061 		return;
18062 
18063 	/*
18064 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18065 	 * must have an associated conn_t.  Otherwise, we're bringing this
18066 	 * interface back up as part of handling an asynchronous event (e.g.,
18067 	 * physical address change).
18068 	 */
18069 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18070 		ASSERT(connp != NULL);
18071 		q = CONNP_TO_WQ(connp);
18072 	} else {
18073 		ASSERT(connp == NULL);
18074 		q = ill->ill_rq;
18075 	}
18076 	if (err == 0) {
18077 		if (ipif->ipif_isv6) {
18078 			if ((err = ipif_up_done_v6(ipif)) != 0)
18079 				ip0dbg(("arp_bringup_done: init failed\n"));
18080 		} else {
18081 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18082 			if (err != 0 ||
18083 			    (err = ipif_up_done(ipif)) != 0) {
18084 				ip0dbg(("arp_bringup_done: "
18085 				    "init failed err %x\n", err));
18086 				(void) ipif_arp_down(ipif);
18087 			}
18088 
18089 		}
18090 	} else {
18091 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18092 	}
18093 
18094 	if ((err == 0) && (ill->ill_up_ipifs)) {
18095 		err = ill_up_ipifs(ill, q, mp1);
18096 		if (err == EINPROGRESS)
18097 			return;
18098 	}
18099 
18100 	/*
18101 	 * If we have a moved ipif to bring up, and everything has succeeded
18102 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18103 	 * down -- the admin can try to bring it up by hand if need be.
18104 	 */
18105 	if (ill->ill_move_ipif != NULL) {
18106 		ipif = ill->ill_move_ipif;
18107 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18108 		    ipif->ipif_ill->ill_name));
18109 		ill->ill_move_ipif = NULL;
18110 		if (err == 0) {
18111 			err = ipif_up(ipif, q, mp1);
18112 			if (err == EINPROGRESS)
18113 				return;
18114 		}
18115 	}
18116 
18117 	/*
18118 	 * The operation must complete without EINPROGRESS since
18119 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18120 	 * Otherwise, the operation will be stuck forever in the ipsq.
18121 	 */
18122 	ASSERT(err != EINPROGRESS);
18123 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18124 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18125 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18126 		    ill_t *, ill, ipif_t *, ipif);
18127 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18128 	} else {
18129 		ipsq_current_finish(ipsq);
18130 	}
18131 }
18132 
18133 /*
18134  * Finish processing of arp replumb after all the DLPI message
18135  * exchanges have completed between arp and the driver.
18136  */
18137 void
18138 arp_replumb_done(ill_t *ill, int err)
18139 {
18140 	mblk_t	*mp1;
18141 	ipif_t  *ipif;
18142 	conn_t *connp = NULL;
18143 	ipsq_t	*ipsq;
18144 	queue_t *q;
18145 
18146 	ASSERT(IAM_WRITER_ILL(ill));
18147 
18148 	ipsq = ill->ill_phyint->phyint_ipsq;
18149 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18150 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18151 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18152 	if (mp1 == NULL) {
18153 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18154 		    ipsq->ipsq_xop->ipx_current_ioctl));
18155 		/* bringup was aborted by the user */
18156 		return;
18157 	}
18158 	/*
18159 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18160 	 * must have an associated conn_t.  Otherwise, we're bringing this
18161 	 * interface back up as part of handling an asynchronous event (e.g.,
18162 	 * physical address change).
18163 	 */
18164 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18165 		ASSERT(connp != NULL);
18166 		q = CONNP_TO_WQ(connp);
18167 	} else {
18168 		ASSERT(connp == NULL);
18169 		q = ill->ill_rq;
18170 	}
18171 	if ((err == 0) && (ill->ill_up_ipifs)) {
18172 		err = ill_up_ipifs(ill, q, mp1);
18173 		if (err == EINPROGRESS)
18174 			return;
18175 	}
18176 	/*
18177 	 * The operation must complete without EINPROGRESS since
18178 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18179 	 * Otherwise, the operation will be stuck forever in the ipsq.
18180 	 */
18181 	ASSERT(err != EINPROGRESS);
18182 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18183 		DTRACE_PROBE4(ipif__ioctl, char *,
18184 		    "arp_replumb_done finish",
18185 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18186 		    ill_t *, ill, ipif_t *, ipif);
18187 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18188 	} else {
18189 		ipsq_current_finish(ipsq);
18190 	}
18191 }
18192 
18193 void
18194 ipif_up_notify(ipif_t *ipif)
18195 {
18196 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18197 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18198 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18199 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18200 	    NE_LIF_UP, NULL, 0);
18201 }
18202 
18203 /*
18204  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18205  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18206  * TPI end points with STREAMS modules pushed above.  This is assured by not
18207  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18208  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18209  * while unwinding from the ispq and that could be a thread from the bottom.
18210  */
18211 /* ARGSUSED */
18212 int
18213 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18214     ip_ioctl_cmd_t *ipip, void *arg)
18215 {
18216 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18217 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18218 	int ret = 0;
18219 	int i;
18220 	size_t size;
18221 	ip_stack_t *ipst;
18222 	zoneid_t zoneid;
18223 	ilb_stack_t *ilbs;
18224 
18225 	ipst = CONNQ_TO_IPST(q);
18226 	ilbs = ipst->ips_netstack->netstack_ilb;
18227 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18228 
18229 	switch (command) {
18230 	case ILB_CREATE_RULE: {
18231 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18232 
18233 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18234 			ret = EINVAL;
18235 			break;
18236 		}
18237 
18238 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18239 		break;
18240 	}
18241 	case ILB_DESTROY_RULE:
18242 	case ILB_ENABLE_RULE:
18243 	case ILB_DISABLE_RULE: {
18244 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18245 
18246 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18247 			ret = EINVAL;
18248 			break;
18249 		}
18250 
18251 		if (cmd->flags & ILB_RULE_ALLRULES) {
18252 			if (command == ILB_DESTROY_RULE) {
18253 				ilb_rule_del_all(ilbs, zoneid);
18254 				break;
18255 			} else if (command == ILB_ENABLE_RULE) {
18256 				ilb_rule_enable_all(ilbs, zoneid);
18257 				break;
18258 			} else if (command == ILB_DISABLE_RULE) {
18259 				ilb_rule_disable_all(ilbs, zoneid);
18260 				break;
18261 			}
18262 		} else {
18263 			if (command == ILB_DESTROY_RULE) {
18264 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18265 			} else if (command == ILB_ENABLE_RULE) {
18266 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18267 				    NULL);
18268 			} else if (command == ILB_DISABLE_RULE) {
18269 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18270 				    NULL);
18271 			}
18272 		}
18273 		break;
18274 	}
18275 	case ILB_NUM_RULES: {
18276 		ilb_num_rules_cmd_t *cmd;
18277 
18278 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18279 			ret = EINVAL;
18280 			break;
18281 		}
18282 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18283 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18284 		break;
18285 	}
18286 	case ILB_RULE_NAMES: {
18287 		ilb_rule_names_cmd_t *cmd;
18288 
18289 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18290 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18291 		    cmd->num_names == 0) {
18292 			ret = EINVAL;
18293 			break;
18294 		}
18295 		size = cmd->num_names * ILB_RULE_NAMESZ;
18296 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18297 		    size != cmd_mp->b_wptr) {
18298 			ret = EINVAL;
18299 			break;
18300 		}
18301 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18302 		break;
18303 	}
18304 	case ILB_NUM_SERVERS: {
18305 		ilb_num_servers_cmd_t *cmd;
18306 
18307 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18308 			ret = EINVAL;
18309 			break;
18310 		}
18311 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18312 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18313 		    &(cmd->num));
18314 		break;
18315 	}
18316 	case ILB_LIST_RULE: {
18317 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18318 
18319 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18320 			ret = EINVAL;
18321 			break;
18322 		}
18323 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18324 		break;
18325 	}
18326 	case ILB_LIST_SERVERS: {
18327 		ilb_servers_info_cmd_t *cmd;
18328 
18329 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18330 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18331 		    cmd->num_servers == 0) {
18332 			ret = EINVAL;
18333 			break;
18334 		}
18335 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18336 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18337 		    size != cmd_mp->b_wptr) {
18338 			ret = EINVAL;
18339 			break;
18340 		}
18341 
18342 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18343 		    &cmd->num_servers);
18344 		break;
18345 	}
18346 	case ILB_ADD_SERVERS: {
18347 		ilb_servers_info_cmd_t *cmd;
18348 		ilb_rule_t *rule;
18349 
18350 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18351 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18352 			ret = EINVAL;
18353 			break;
18354 		}
18355 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18356 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18357 		    size != cmd_mp->b_wptr) {
18358 			ret = EINVAL;
18359 			break;
18360 		}
18361 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18362 		if (rule == NULL) {
18363 			ASSERT(ret != 0);
18364 			break;
18365 		}
18366 		for (i = 0; i < cmd->num_servers; i++) {
18367 			ilb_server_info_t *s;
18368 
18369 			s = &cmd->servers[i];
18370 			s->err = ilb_server_add(ilbs, rule, s);
18371 		}
18372 		ILB_RULE_REFRELE(rule);
18373 		break;
18374 	}
18375 	case ILB_DEL_SERVERS:
18376 	case ILB_ENABLE_SERVERS:
18377 	case ILB_DISABLE_SERVERS: {
18378 		ilb_servers_cmd_t *cmd;
18379 		ilb_rule_t *rule;
18380 		int (*f)();
18381 
18382 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18383 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18384 			ret = EINVAL;
18385 			break;
18386 		}
18387 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18388 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18389 		    size != cmd_mp->b_wptr) {
18390 			ret = EINVAL;
18391 			break;
18392 		}
18393 
18394 		if (command == ILB_DEL_SERVERS)
18395 			f = ilb_server_del;
18396 		else if (command == ILB_ENABLE_SERVERS)
18397 			f = ilb_server_enable;
18398 		else if (command == ILB_DISABLE_SERVERS)
18399 			f = ilb_server_disable;
18400 
18401 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18402 		if (rule == NULL) {
18403 			ASSERT(ret != 0);
18404 			break;
18405 		}
18406 
18407 		for (i = 0; i < cmd->num_servers; i++) {
18408 			ilb_server_arg_t *s;
18409 
18410 			s = &cmd->servers[i];
18411 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18412 		}
18413 		ILB_RULE_REFRELE(rule);
18414 		break;
18415 	}
18416 	case ILB_LIST_NAT_TABLE: {
18417 		ilb_list_nat_cmd_t *cmd;
18418 
18419 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18420 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18421 			ret = EINVAL;
18422 			break;
18423 		}
18424 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18425 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18426 		    size != cmd_mp->b_wptr) {
18427 			ret = EINVAL;
18428 			break;
18429 		}
18430 
18431 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18432 		    &cmd->flags);
18433 		break;
18434 	}
18435 	case ILB_LIST_STICKY_TABLE: {
18436 		ilb_list_sticky_cmd_t *cmd;
18437 
18438 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18439 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18440 			ret = EINVAL;
18441 			break;
18442 		}
18443 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18444 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18445 		    size != cmd_mp->b_wptr) {
18446 			ret = EINVAL;
18447 			break;
18448 		}
18449 
18450 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18451 		    &cmd->num_sticky, &cmd->flags);
18452 		break;
18453 	}
18454 	default:
18455 		ret = EINVAL;
18456 		break;
18457 	}
18458 done:
18459 	return (ret);
18460 }
18461 
18462 /* Remove all cache entries for this logical interface */
18463 void
18464 ipif_nce_down(ipif_t *ipif)
18465 {
18466 	ill_t *ill = ipif->ipif_ill;
18467 	nce_t *nce;
18468 
18469 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18470 	    ill_t *, ill, ipif_t *, ipif);
18471 	if (ipif->ipif_added_nce) {
18472 		if (ipif->ipif_isv6)
18473 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18474 		else
18475 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18476 		if (nce != NULL) {
18477 			if (--nce->nce_ipif_cnt == 0)
18478 				ncec_delete(nce->nce_common);
18479 			ipif->ipif_added_nce = 0;
18480 			nce_refrele(nce);
18481 		} else {
18482 			/*
18483 			 * nce may already be NULL because it was already
18484 			 * flushed, e.g., due to a call to nce_flush
18485 			 */
18486 			ipif->ipif_added_nce = 0;
18487 		}
18488 	}
18489 	/*
18490 	 * Make IPMP aware of the deleted data address.
18491 	 */
18492 	if (IS_IPMP(ill))
18493 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18494 
18495 	/*
18496 	 * Remove all other nces dependent on this ill when the last ipif
18497 	 * is going away.
18498 	 */
18499 	if (ill->ill_ipif_up_count == 0) {
18500 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18501 		    (uchar_t *)ill, ill->ill_ipst);
18502 		if (IS_UNDER_IPMP(ill))
18503 			nce_flush(ill, B_TRUE);
18504 	}
18505 }
18506 
18507 /*
18508  * find the first interface that uses usill for its source address.
18509  */
18510 ill_t *
18511 ill_lookup_usesrc(ill_t *usill)
18512 {
18513 	ip_stack_t *ipst = usill->ill_ipst;
18514 	ill_t *ill;
18515 
18516 	ASSERT(usill != NULL);
18517 
18518 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18519 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18520 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18521 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18522 	    ill = ill->ill_usesrc_grp_next) {
18523 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18524 		    !ILL_IS_CONDEMNED(ill)) {
18525 			ill_refhold(ill);
18526 			break;
18527 		}
18528 	}
18529 	rw_exit(&ipst->ips_ill_g_lock);
18530 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18531 	return (ill);
18532 }
18533