xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision 0c4606f05172c66c6c65847c0f940871a0ea1c15)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
118 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
119 		    char *value, caddr_t cp, cred_t *ioc_cr);
120 
121 static boolean_t ill_is_quiescent(ill_t *);
122 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
123 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
124 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
125     mblk_t *mp, boolean_t need_up);
126 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
127     mblk_t *mp, boolean_t need_up);
128 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
129     queue_t *q, mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
131     mblk_t *mp);
132 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp);
134 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
137     int ioccmd, struct linkblk *li);
138 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
139 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
140 static void	ipsq_flush(ill_t *ill);
141 
142 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static void	ipsq_delete(ipsq_t *);
145 
146 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
147     boolean_t initialize, boolean_t insert, int *errorp);
148 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
149 static void	ipif_delete_bcast_ires(ipif_t *ipif);
150 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
151 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
152 		    boolean_t isv6);
153 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
154 static void	ipif_free(ipif_t *ipif);
155 static void	ipif_free_tail(ipif_t *ipif);
156 static void	ipif_set_default(ipif_t *ipif);
157 static int	ipif_set_values(queue_t *q, mblk_t *mp,
158     char *interf_name, uint_t *ppa);
159 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
160     queue_t *q);
161 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
162     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
163     ip_stack_t *);
164 
165 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
166 static void	ill_delete_interface_type(ill_if_t *);
167 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
168 static void	ill_dl_down(ill_t *ill);
169 static void	ill_down(ill_t *ill);
170 static void	ill_down_ipifs(ill_t *, boolean_t);
171 static void	ill_free_mib(ill_t *ill);
172 static void	ill_glist_delete(ill_t *);
173 static void	ill_phyint_reinit(ill_t *ill);
174 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
175 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
177 
178 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
179 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
180 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
181 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
182 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
183 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
184 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
185 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
186 static ip_v4mapinfo_func_t ip_mbcast_mapping;
187 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
188 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
189 static void	phyint_free(phyint_t *);
190 
191 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
194 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
196 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
197     dl_capability_sub_t *);
198 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
199 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
200 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
201 		    dl_capability_sub_t *);
202 static void	ill_capability_dld_enable(ill_t *);
203 static void	ill_capability_ack_thr(void *);
204 static void	ill_capability_lso_enable(ill_t *);
205 
206 static ill_t	*ill_prev_usesrc(ill_t *);
207 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
208 static void	ill_disband_usesrc_group(ill_t *);
209 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
210 
211 #ifdef DEBUG
212 static	void	ill_trace_cleanup(const ill_t *);
213 static	void	ipif_trace_cleanup(const ipif_t *);
214 #endif
215 
216 static	void	ill_dlpi_clear_deferred(ill_t *ill);
217 
218 /*
219  * if we go over the memory footprint limit more than once in this msec
220  * interval, we'll start pruning aggressively.
221  */
222 int ip_min_frag_prune_time = 0;
223 
224 static ipft_t	ip_ioctl_ftbl[] = {
225 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
226 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
227 		IPFT_F_NO_REPLY },
228 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
229 	{ 0 }
230 };
231 
232 /* Simple ICMP IP Header Template */
233 static ipha_t icmp_ipha = {
234 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
235 };
236 
237 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
238 
239 static ip_m_t   ip_m_tbl[] = {
240 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
241 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
242 	    ip_nodef_v6intfid },
243 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
244 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
245 	    ip_nodef_v6intfid },
246 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
259 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
260 	    ip_ipv4_v6destintfid },
261 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
262 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
263 	    ip_ipv6_v6destintfid },
264 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_nodef_v6intfid },
267 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
269 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
271 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
272 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
273 	    ip_nodef_v6intfid }
274 };
275 
276 static ill_t	ill_null;		/* Empty ILL for init. */
277 char	ipif_loopback_name[] = "lo0";
278 static char *ipv4_forward_suffix = ":ip_forwarding";
279 static char *ipv6_forward_suffix = ":ip6_forwarding";
280 static	sin6_t	sin6_null;	/* Zero address for quick clears */
281 static	sin_t	sin_null;	/* Zero address for quick clears */
282 
283 /* When set search for unused ipif_seqid */
284 static ipif_t	ipif_zero;
285 
286 /*
287  * ppa arena is created after these many
288  * interfaces have been plumbed.
289  */
290 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
291 
292 /*
293  * Allocate per-interface mibs.
294  * Returns true if ok. False otherwise.
295  *  ipsq  may not yet be allocated (loopback case ).
296  */
297 static boolean_t
298 ill_allocate_mibs(ill_t *ill)
299 {
300 	/* Already allocated? */
301 	if (ill->ill_ip_mib != NULL) {
302 		if (ill->ill_isv6)
303 			ASSERT(ill->ill_icmp6_mib != NULL);
304 		return (B_TRUE);
305 	}
306 
307 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
308 	    KM_NOSLEEP);
309 	if (ill->ill_ip_mib == NULL) {
310 		return (B_FALSE);
311 	}
312 
313 	/* Setup static information */
314 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
315 	    sizeof (mib2_ipIfStatsEntry_t));
316 	if (ill->ill_isv6) {
317 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
318 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
319 		    sizeof (mib2_ipv6AddrEntry_t));
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
321 		    sizeof (mib2_ipv6RouteEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
323 		    sizeof (mib2_ipv6NetToMediaEntry_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
325 		    sizeof (ipv6_member_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
327 		    sizeof (ipv6_grpsrc_t));
328 	} else {
329 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
331 		    sizeof (mib2_ipAddrEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
333 		    sizeof (mib2_ipRouteEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
335 		    sizeof (mib2_ipNetToMediaEntry_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
337 		    sizeof (ip_member_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
339 		    sizeof (ip_grpsrc_t));
340 
341 		/*
342 		 * For a v4 ill, we are done at this point, because per ill
343 		 * icmp mibs are only used for v6.
344 		 */
345 		return (B_TRUE);
346 	}
347 
348 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
349 	    KM_NOSLEEP);
350 	if (ill->ill_icmp6_mib == NULL) {
351 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
352 		ill->ill_ip_mib = NULL;
353 		return (B_FALSE);
354 	}
355 	/* static icmp info */
356 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
357 	    sizeof (mib2_ipv6IfIcmpEntry_t);
358 	/*
359 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
360 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
361 	 * -> ill_phyint_reinit
362 	 */
363 	return (B_TRUE);
364 }
365 
366 /*
367  * Completely vaporize a lower level tap and all associated interfaces.
368  * ill_delete is called only out of ip_close when the device control
369  * stream is being closed.
370  */
371 void
372 ill_delete(ill_t *ill)
373 {
374 	ipif_t	*ipif;
375 	ill_t	*prev_ill;
376 	ip_stack_t	*ipst = ill->ill_ipst;
377 
378 	/*
379 	 * ill_delete may be forcibly entering the ipsq. The previous
380 	 * ioctl may not have completed and may need to be aborted.
381 	 * ipsq_flush takes care of it. If we don't need to enter the
382 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
383 	 * ill_delete_tail is sufficient.
384 	 */
385 	ipsq_flush(ill);
386 
387 	/*
388 	 * Nuke all interfaces.  ipif_free will take down the interface,
389 	 * remove it from the list, and free the data structure.
390 	 * Walk down the ipif list and remove the logical interfaces
391 	 * first before removing the main ipif. We can't unplumb
392 	 * zeroth interface first in the case of IPv6 as update_conn_ill
393 	 * -> ip_ll_multireq de-references ill_ipif for checking
394 	 * POINTOPOINT.
395 	 *
396 	 * If ill_ipif was not properly initialized (i.e low on memory),
397 	 * then no interfaces to clean up. In this case just clean up the
398 	 * ill.
399 	 */
400 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
401 		ipif_free(ipif);
402 
403 	/*
404 	 * clean out all the nce_t entries that depend on this
405 	 * ill for the ill_phys_addr.
406 	 */
407 	nce_flush(ill, B_TRUE);
408 
409 	/* Clean up msgs on pending upcalls for mrouted */
410 	reset_mrt_ill(ill);
411 
412 	update_conn_ill(ill, ipst);
413 
414 	/*
415 	 * Remove multicast references added as a result of calls to
416 	 * ip_join_allmulti().
417 	 */
418 	ip_purge_allmulti(ill);
419 
420 	/*
421 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
422 	 */
423 	if (IS_UNDER_IPMP(ill))
424 		ipmp_ill_leave_illgrp(ill);
425 
426 	/*
427 	 * ill_down will arrange to blow off any IRE's dependent on this
428 	 * ILL, and shut down fragmentation reassembly.
429 	 */
430 	ill_down(ill);
431 
432 	/* Let SCTP know, so that it can remove this from its list. */
433 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
434 
435 	/*
436 	 * Walk all CONNs that can have a reference on an ire or nce for this
437 	 * ill (we actually walk all that now have stale references).
438 	 */
439 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
440 
441 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
442 	if (ill->ill_isv6)
443 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
444 
445 	/*
446 	 * If an address on this ILL is being used as a source address then
447 	 * clear out the pointers in other ILLs that point to this ILL.
448 	 */
449 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
450 	if (ill->ill_usesrc_grp_next != NULL) {
451 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
452 			ill_disband_usesrc_group(ill);
453 		} else {	/* consumer of the usesrc ILL */
454 			prev_ill = ill_prev_usesrc(ill);
455 			prev_ill->ill_usesrc_grp_next =
456 			    ill->ill_usesrc_grp_next;
457 		}
458 	}
459 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
460 }
461 
462 static void
463 ipif_non_duplicate(ipif_t *ipif)
464 {
465 	ill_t *ill = ipif->ipif_ill;
466 	mutex_enter(&ill->ill_lock);
467 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
468 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
469 		ASSERT(ill->ill_ipif_dup_count > 0);
470 		ill->ill_ipif_dup_count--;
471 	}
472 	mutex_exit(&ill->ill_lock);
473 }
474 
475 /*
476  * ill_delete_tail is called from ip_modclose after all references
477  * to the closing ill are gone. The wait is done in ip_modclose
478  */
479 void
480 ill_delete_tail(ill_t *ill)
481 {
482 	mblk_t	**mpp;
483 	ipif_t	*ipif;
484 	ip_stack_t	*ipst = ill->ill_ipst;
485 
486 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
487 		ipif_non_duplicate(ipif);
488 		(void) ipif_down_tail(ipif);
489 	}
490 
491 	ASSERT(ill->ill_ipif_dup_count == 0);
492 
493 	/*
494 	 * If polling capability is enabled (which signifies direct
495 	 * upcall into IP and driver has ill saved as a handle),
496 	 * we need to make sure that unbind has completed before we
497 	 * let the ill disappear and driver no longer has any reference
498 	 * to this ill.
499 	 */
500 	mutex_enter(&ill->ill_lock);
501 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
502 		cv_wait(&ill->ill_cv, &ill->ill_lock);
503 	mutex_exit(&ill->ill_lock);
504 	ASSERT(!(ill->ill_capabilities &
505 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
506 
507 	if (ill->ill_net_type != IRE_LOOPBACK)
508 		qprocsoff(ill->ill_rq);
509 
510 	/*
511 	 * We do an ipsq_flush once again now. New messages could have
512 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
513 	 * could also have landed up if an ioctl thread had looked up
514 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
515 	 * enqueued the ioctl when we did the ipsq_flush last time.
516 	 */
517 	ipsq_flush(ill);
518 
519 	/*
520 	 * Free capabilities.
521 	 */
522 	if (ill->ill_hcksum_capab != NULL) {
523 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
524 		ill->ill_hcksum_capab = NULL;
525 	}
526 
527 	if (ill->ill_zerocopy_capab != NULL) {
528 		kmem_free(ill->ill_zerocopy_capab,
529 		    sizeof (ill_zerocopy_capab_t));
530 		ill->ill_zerocopy_capab = NULL;
531 	}
532 
533 	if (ill->ill_lso_capab != NULL) {
534 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
535 		ill->ill_lso_capab = NULL;
536 	}
537 
538 	if (ill->ill_dld_capab != NULL) {
539 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
540 		ill->ill_dld_capab = NULL;
541 	}
542 
543 	while (ill->ill_ipif != NULL)
544 		ipif_free_tail(ill->ill_ipif);
545 
546 	/*
547 	 * We have removed all references to ilm from conn and the ones joined
548 	 * within the kernel.
549 	 *
550 	 * We don't walk conns, mrts and ires because
551 	 *
552 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
553 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
554 	 *    ill references.
555 	 */
556 
557 	/*
558 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
559 	 * is safe to do because the illgrp has already been unlinked from the
560 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
561 	 */
562 	if (IS_IPMP(ill)) {
563 		ipmp_illgrp_destroy(ill->ill_grp);
564 		ill->ill_grp = NULL;
565 	}
566 
567 	/*
568 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
569 	 * could free the phyint. No more reference to the phyint after this
570 	 * point.
571 	 */
572 	(void) ill_glist_delete(ill);
573 
574 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
575 	if (ill->ill_ndd_name != NULL)
576 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
577 	rw_exit(&ipst->ips_ip_g_nd_lock);
578 
579 	if (ill->ill_frag_ptr != NULL) {
580 		uint_t count;
581 
582 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
583 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
584 		}
585 		mi_free(ill->ill_frag_ptr);
586 		ill->ill_frag_ptr = NULL;
587 		ill->ill_frag_hash_tbl = NULL;
588 	}
589 
590 	freemsg(ill->ill_nd_lla_mp);
591 	/* Free all retained control messages. */
592 	mpp = &ill->ill_first_mp_to_free;
593 	do {
594 		while (mpp[0]) {
595 			mblk_t  *mp;
596 			mblk_t  *mp1;
597 
598 			mp = mpp[0];
599 			mpp[0] = mp->b_next;
600 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
601 				mp1->b_next = NULL;
602 				mp1->b_prev = NULL;
603 			}
604 			freemsg(mp);
605 		}
606 	} while (mpp++ != &ill->ill_last_mp_to_free);
607 
608 	ill_free_mib(ill);
609 
610 #ifdef DEBUG
611 	ill_trace_cleanup(ill);
612 #endif
613 
614 	/* The default multicast interface might have changed */
615 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
616 
617 	/* Drop refcnt here */
618 	netstack_rele(ill->ill_ipst->ips_netstack);
619 	ill->ill_ipst = NULL;
620 }
621 
622 static void
623 ill_free_mib(ill_t *ill)
624 {
625 	ip_stack_t *ipst = ill->ill_ipst;
626 
627 	/*
628 	 * MIB statistics must not be lost, so when an interface
629 	 * goes away the counter values will be added to the global
630 	 * MIBs.
631 	 */
632 	if (ill->ill_ip_mib != NULL) {
633 		if (ill->ill_isv6) {
634 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
635 			    ill->ill_ip_mib);
636 		} else {
637 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
638 			    ill->ill_ip_mib);
639 		}
640 
641 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
642 		ill->ill_ip_mib = NULL;
643 	}
644 	if (ill->ill_icmp6_mib != NULL) {
645 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
646 		    ill->ill_icmp6_mib);
647 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
648 		ill->ill_icmp6_mib = NULL;
649 	}
650 }
651 
652 /*
653  * Concatenate together a physical address and a sap.
654  *
655  * Sap_lengths are interpreted as follows:
656  *   sap_length == 0	==>	no sap
657  *   sap_length > 0	==>	sap is at the head of the dlpi address
658  *   sap_length < 0	==>	sap is at the tail of the dlpi address
659  */
660 static void
661 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
662     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
663 {
664 	uint16_t sap_addr = (uint16_t)sap_src;
665 
666 	if (sap_length == 0) {
667 		if (phys_src == NULL)
668 			bzero(dst, phys_length);
669 		else
670 			bcopy(phys_src, dst, phys_length);
671 	} else if (sap_length < 0) {
672 		if (phys_src == NULL)
673 			bzero(dst, phys_length);
674 		else
675 			bcopy(phys_src, dst, phys_length);
676 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
677 	} else {
678 		bcopy(&sap_addr, dst, sizeof (sap_addr));
679 		if (phys_src == NULL)
680 			bzero((char *)dst + sap_length, phys_length);
681 		else
682 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
683 	}
684 }
685 
686 /*
687  * Generate a dl_unitdata_req mblk for the device and address given.
688  * addr_length is the length of the physical portion of the address.
689  * If addr is NULL include an all zero address of the specified length.
690  * TRUE? In any case, addr_length is taken to be the entire length of the
691  * dlpi address, including the absolute value of sap_length.
692  */
693 mblk_t *
694 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
695 		t_scalar_t sap_length)
696 {
697 	dl_unitdata_req_t *dlur;
698 	mblk_t	*mp;
699 	t_scalar_t	abs_sap_length;		/* absolute value */
700 
701 	abs_sap_length = ABS(sap_length);
702 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
703 	    DL_UNITDATA_REQ);
704 	if (mp == NULL)
705 		return (NULL);
706 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
707 	/* HACK: accomodate incompatible DLPI drivers */
708 	if (addr_length == 8)
709 		addr_length = 6;
710 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
711 	dlur->dl_dest_addr_offset = sizeof (*dlur);
712 	dlur->dl_priority.dl_min = 0;
713 	dlur->dl_priority.dl_max = 0;
714 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
715 	    (uchar_t *)&dlur[1]);
716 	return (mp);
717 }
718 
719 /*
720  * Add the pending mp to the list. There can be only 1 pending mp
721  * in the list. Any exclusive ioctl that needs to wait for a response
722  * from another module or driver needs to use this function to set
723  * the ipx_pending_mp to the ioctl mblk and wait for the response from
724  * the other module/driver. This is also used while waiting for the
725  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
726  */
727 boolean_t
728 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
729     int waitfor)
730 {
731 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
732 
733 	ASSERT(IAM_WRITER_IPIF(ipif));
734 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
735 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
736 	ASSERT(ipx->ipx_pending_mp == NULL);
737 	/*
738 	 * The caller may be using a different ipif than the one passed into
739 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
740 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
741 	 * that `ipx_current_ipif == ipif'.
742 	 */
743 	ASSERT(ipx->ipx_current_ipif != NULL);
744 
745 	/*
746 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
747 	 * driver.
748 	 */
749 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
750 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
751 	    (DB_TYPE(add_mp) == M_PCPROTO));
752 
753 	if (connp != NULL) {
754 		ASSERT(MUTEX_HELD(&connp->conn_lock));
755 		/*
756 		 * Return error if the conn has started closing. The conn
757 		 * could have finished cleaning up the pending mp list,
758 		 * If so we should not add another mp to the list negating
759 		 * the cleanup.
760 		 */
761 		if (connp->conn_state_flags & CONN_CLOSING)
762 			return (B_FALSE);
763 	}
764 	mutex_enter(&ipx->ipx_lock);
765 	ipx->ipx_pending_ipif = ipif;
766 	/*
767 	 * Note down the queue in b_queue. This will be returned by
768 	 * ipsq_pending_mp_get. Caller will then use these values to restart
769 	 * the processing
770 	 */
771 	add_mp->b_next = NULL;
772 	add_mp->b_queue = q;
773 	ipx->ipx_pending_mp = add_mp;
774 	ipx->ipx_waitfor = waitfor;
775 	mutex_exit(&ipx->ipx_lock);
776 
777 	if (connp != NULL)
778 		connp->conn_oper_pending_ill = ipif->ipif_ill;
779 
780 	return (B_TRUE);
781 }
782 
783 /*
784  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
785  * queued in the list.
786  */
787 mblk_t *
788 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
789 {
790 	mblk_t	*curr = NULL;
791 	ipxop_t	*ipx = ipsq->ipsq_xop;
792 
793 	*connpp = NULL;
794 	mutex_enter(&ipx->ipx_lock);
795 	if (ipx->ipx_pending_mp == NULL) {
796 		mutex_exit(&ipx->ipx_lock);
797 		return (NULL);
798 	}
799 
800 	/* There can be only 1 such excl message */
801 	curr = ipx->ipx_pending_mp;
802 	ASSERT(curr->b_next == NULL);
803 	ipx->ipx_pending_ipif = NULL;
804 	ipx->ipx_pending_mp = NULL;
805 	ipx->ipx_waitfor = 0;
806 	mutex_exit(&ipx->ipx_lock);
807 
808 	if (CONN_Q(curr->b_queue)) {
809 		/*
810 		 * This mp did a refhold on the conn, at the start of the ioctl.
811 		 * So we can safely return a pointer to the conn to the caller.
812 		 */
813 		*connpp = Q_TO_CONN(curr->b_queue);
814 	} else {
815 		*connpp = NULL;
816 	}
817 	curr->b_next = NULL;
818 	curr->b_prev = NULL;
819 	return (curr);
820 }
821 
822 /*
823  * Cleanup the ioctl mp queued in ipx_pending_mp
824  * - Called in the ill_delete path
825  * - Called in the M_ERROR or M_HANGUP path on the ill.
826  * - Called in the conn close path.
827  *
828  * Returns success on finding the pending mblk associated with the ioctl or
829  * exclusive operation in progress, failure otherwise.
830  */
831 boolean_t
832 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
833 {
834 	mblk_t	*mp;
835 	ipxop_t	*ipx;
836 	queue_t	*q;
837 	ipif_t	*ipif;
838 	int	cmd;
839 
840 	ASSERT(IAM_WRITER_ILL(ill));
841 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
842 
843 	/*
844 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
845 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
846 	 * even if it is meant for another ill, since we have to enqueue
847 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
848 	 * If connp is non-null we are called from the conn close path.
849 	 */
850 	mutex_enter(&ipx->ipx_lock);
851 	mp = ipx->ipx_pending_mp;
852 	if ((connp != NULL) &&
853 	    (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp))) {
854 		mutex_exit(&ipx->ipx_lock);
855 		return (B_FALSE);
856 	}
857 
858 	/* Now remove from the ipx_pending_mp */
859 	ipx->ipx_pending_mp = NULL;
860 	ipif = ipx->ipx_pending_ipif;
861 	ipx->ipx_pending_ipif = NULL;
862 	ipx->ipx_waitfor = 0;
863 	ipx->ipx_current_ipif = NULL;
864 	cmd = ipx->ipx_current_ioctl;
865 	ipx->ipx_current_ioctl = 0;
866 	ipx->ipx_current_done = B_TRUE;
867 	mutex_exit(&ipx->ipx_lock);
868 
869 	if (mp == NULL)
870 		return (B_FALSE);
871 
872 	q = mp->b_queue;
873 	mp->b_next = NULL;
874 	mp->b_prev = NULL;
875 	mp->b_queue = NULL;
876 
877 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
878 		DTRACE_PROBE4(ipif__ioctl,
879 		    char *, "ipsq_pending_mp_cleanup",
880 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
881 		    ipif_t *, ipif);
882 		if (connp == NULL) {
883 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
884 		} else {
885 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
886 			mutex_enter(&ipif->ipif_ill->ill_lock);
887 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
888 			mutex_exit(&ipif->ipif_ill->ill_lock);
889 		}
890 	} else {
891 		inet_freemsg(mp);
892 	}
893 	return (B_TRUE);
894 }
895 
896 /*
897  * Called in the conn close path and ill delete path
898  */
899 static void
900 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
901 {
902 	ipsq_t	*ipsq;
903 	mblk_t	*prev;
904 	mblk_t	*curr;
905 	mblk_t	*next;
906 	queue_t	*wq, *rq = NULL;
907 	mblk_t	*tmp_list = NULL;
908 
909 	ASSERT(IAM_WRITER_ILL(ill));
910 	if (connp != NULL)
911 		wq = CONNP_TO_WQ(connp);
912 	else
913 		wq = ill->ill_wq;
914 
915 	/*
916 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
917 	 * against this here.
918 	 */
919 	if (wq != NULL)
920 		rq = RD(wq);
921 
922 	ipsq = ill->ill_phyint->phyint_ipsq;
923 	/*
924 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
925 	 * In the case of ioctl from a conn, there can be only 1 mp
926 	 * queued on the ipsq. If an ill is being unplumbed, only messages
927 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
928 	 * ioctls meant for this ill form conn's are not flushed. They will
929 	 * be processed during ipsq_exit and will not find the ill and will
930 	 * return error.
931 	 */
932 	mutex_enter(&ipsq->ipsq_lock);
933 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
934 	    curr = next) {
935 		next = curr->b_next;
936 		if (curr->b_queue == wq || curr->b_queue == rq) {
937 			/* Unlink the mblk from the pending mp list */
938 			if (prev != NULL) {
939 				prev->b_next = curr->b_next;
940 			} else {
941 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
942 				ipsq->ipsq_xopq_mphead = curr->b_next;
943 			}
944 			if (ipsq->ipsq_xopq_mptail == curr)
945 				ipsq->ipsq_xopq_mptail = prev;
946 			/*
947 			 * Create a temporary list and release the ipsq lock
948 			 * New elements are added to the head of the tmp_list
949 			 */
950 			curr->b_next = tmp_list;
951 			tmp_list = curr;
952 		} else {
953 			prev = curr;
954 		}
955 	}
956 	mutex_exit(&ipsq->ipsq_lock);
957 
958 	while (tmp_list != NULL) {
959 		curr = tmp_list;
960 		tmp_list = curr->b_next;
961 		curr->b_next = NULL;
962 		curr->b_prev = NULL;
963 		curr->b_queue = NULL;
964 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
965 			DTRACE_PROBE4(ipif__ioctl,
966 			    char *, "ipsq_xopq_mp_cleanup",
967 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
968 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
969 			    CONN_CLOSE : NO_COPYOUT, NULL);
970 		} else {
971 			/*
972 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
973 			 * this can't be just inet_freemsg. we have to
974 			 * restart it otherwise the thread will be stuck.
975 			 */
976 			inet_freemsg(curr);
977 		}
978 	}
979 }
980 
981 /*
982  * This conn has started closing. Cleanup any pending ioctl from this conn.
983  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
984  */
985 void
986 conn_ioctl_cleanup(conn_t *connp)
987 {
988 	ipsq_t	*ipsq;
989 	ill_t	*ill;
990 	boolean_t refheld;
991 
992 	/*
993 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
994 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
995 	 * started the mp could be present in ipx_pending_mp. Note that if
996 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
997 	 * not yet queued anywhere. In this case, the conn close code will wait
998 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
999 	 * tcp_close will wait first until all ioctls have completed for this
1000 	 * conn.
1001 	 */
1002 	mutex_enter(&connp->conn_lock);
1003 	ill = connp->conn_oper_pending_ill;
1004 	if (ill == NULL) {
1005 		mutex_exit(&connp->conn_lock);
1006 		return;
1007 	}
1008 
1009 	/*
1010 	 * We may not be able to refhold the ill if the ill/ipif
1011 	 * is changing. But we need to make sure that the ill will
1012 	 * not vanish. So we just bump up the ill_waiter count.
1013 	 */
1014 	refheld = ill_waiter_inc(ill);
1015 	mutex_exit(&connp->conn_lock);
1016 	if (refheld) {
1017 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1018 			ill_waiter_dcr(ill);
1019 			/*
1020 			 * Check whether this ioctl has started and is
1021 			 * pending. If it is not found there then check
1022 			 * whether this ioctl has not even started and is in
1023 			 * the ipsq_xopq list.
1024 			 */
1025 			if (!ipsq_pending_mp_cleanup(ill, connp))
1026 				ipsq_xopq_mp_cleanup(ill, connp);
1027 			ipsq = ill->ill_phyint->phyint_ipsq;
1028 			ipsq_exit(ipsq);
1029 			return;
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * The ill is also closing and we could not bump up the
1035 	 * ill_waiter_count or we could not enter the ipsq. Leave
1036 	 * the cleanup to ill_delete
1037 	 */
1038 	mutex_enter(&connp->conn_lock);
1039 	while (connp->conn_oper_pending_ill != NULL)
1040 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1041 	mutex_exit(&connp->conn_lock);
1042 	if (refheld)
1043 		ill_waiter_dcr(ill);
1044 }
1045 
1046 /*
1047  * ipcl_walk function for cleaning up conn_*_ill fields.
1048  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1049  * conn_bound_if in place. We prefer dropping
1050  * packets instead of sending them out the wrong interface, or accepting
1051  * packets from the wrong ifindex.
1052  */
1053 static void
1054 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1055 {
1056 	ill_t	*ill = (ill_t *)arg;
1057 
1058 	mutex_enter(&connp->conn_lock);
1059 	if (connp->conn_dhcpinit_ill == ill) {
1060 		connp->conn_dhcpinit_ill = NULL;
1061 		ASSERT(ill->ill_dhcpinit != 0);
1062 		atomic_dec_32(&ill->ill_dhcpinit);
1063 		ill_set_inputfn(ill);
1064 	}
1065 	mutex_exit(&connp->conn_lock);
1066 }
1067 
1068 static int
1069 ill_down_ipifs_tail(ill_t *ill)
1070 {
1071 	ipif_t	*ipif;
1072 	int err;
1073 
1074 	ASSERT(IAM_WRITER_ILL(ill));
1075 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1076 		ipif_non_duplicate(ipif);
1077 		/*
1078 		 * ipif_down_tail will call arp_ll_down on the last ipif
1079 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1080 		 */
1081 		if ((err = ipif_down_tail(ipif)) != 0)
1082 			return (err);
1083 	}
1084 	return (0);
1085 }
1086 
1087 /* ARGSUSED */
1088 void
1089 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1090 {
1091 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1092 	(void) ill_down_ipifs_tail(q->q_ptr);
1093 	freemsg(mp);
1094 	ipsq_current_finish(ipsq);
1095 }
1096 
1097 /*
1098  * ill_down_start is called when we want to down this ill and bring it up again
1099  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1100  * all interfaces, but don't tear down any plumbing.
1101  */
1102 boolean_t
1103 ill_down_start(queue_t *q, mblk_t *mp)
1104 {
1105 	ill_t	*ill = q->q_ptr;
1106 	ipif_t	*ipif;
1107 
1108 	ASSERT(IAM_WRITER_ILL(ill));
1109 	mutex_enter(&ill->ill_lock);
1110 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1111 	/* no more nce addition allowed */
1112 	mutex_exit(&ill->ill_lock);
1113 
1114 	/*
1115 	 * It is possible that some ioctl is already in progress while we
1116 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1117 	 * the ioctl. (ill_down_start() is being processed as CUR_OP since
1118 	 * the cause of the M_ERROR / M_HANGUP may prevent the in progress
1119 	 * ioctl from completion.)
1120 	 */
1121 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1122 	ill_dlpi_clear_deferred(ill);
1123 
1124 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1125 		(void) ipif_down(ipif, NULL, NULL);
1126 
1127 	ill_down(ill);
1128 
1129 	/*
1130 	 * Walk all CONNs that can have a reference on an ire or nce for this
1131 	 * ill (we actually walk all that now have stale references).
1132 	 */
1133 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1134 
1135 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1136 	if (ill->ill_isv6)
1137 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1138 
1139 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1140 
1141 	/*
1142 	 * Atomically test and add the pending mp if references are active.
1143 	 */
1144 	mutex_enter(&ill->ill_lock);
1145 	if (!ill_is_quiescent(ill)) {
1146 		/* call cannot fail since `conn_t *' argument is NULL */
1147 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1148 		    mp, ILL_DOWN);
1149 		mutex_exit(&ill->ill_lock);
1150 		return (B_FALSE);
1151 	}
1152 	mutex_exit(&ill->ill_lock);
1153 	return (B_TRUE);
1154 }
1155 
1156 static void
1157 ill_down(ill_t *ill)
1158 {
1159 	mblk_t	*mp;
1160 	ip_stack_t	*ipst = ill->ill_ipst;
1161 
1162 	/*
1163 	 * Blow off any IREs dependent on this ILL.
1164 	 * The caller needs to handle conn_ixa_cleanup
1165 	 */
1166 	ill_delete_ires(ill);
1167 
1168 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1169 
1170 	/* Remove any conn_*_ill depending on this ill */
1171 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1172 
1173 	/*
1174 	 * Free state for additional IREs.
1175 	 */
1176 	mutex_enter(&ill->ill_saved_ire_lock);
1177 	mp = ill->ill_saved_ire_mp;
1178 	ill->ill_saved_ire_mp = NULL;
1179 	ill->ill_saved_ire_cnt = 0;
1180 	mutex_exit(&ill->ill_saved_ire_lock);
1181 	freemsg(mp);
1182 }
1183 
1184 /*
1185  * ire_walk routine used to delete every IRE that depends on
1186  * 'ill'.  (Always called as writer.)
1187  *
1188  * Note: since the routes added by the kernel are deleted separately,
1189  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1190  *
1191  * We also remove references on ire_nce_cache entries that refer to the ill.
1192  */
1193 void
1194 ill_downi(ire_t *ire, char *ill_arg)
1195 {
1196 	ill_t	*ill = (ill_t *)ill_arg;
1197 	nce_t	*nce;
1198 
1199 	mutex_enter(&ire->ire_lock);
1200 	nce = ire->ire_nce_cache;
1201 	if (nce != NULL && nce->nce_ill == ill)
1202 		ire->ire_nce_cache = NULL;
1203 	else
1204 		nce = NULL;
1205 	mutex_exit(&ire->ire_lock);
1206 	if (nce != NULL)
1207 		nce_refrele(nce);
1208 	if (ire->ire_ill == ill)
1209 		ire_delete(ire);
1210 }
1211 
1212 /* Remove IRE_IF_CLONE on this ill */
1213 void
1214 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1215 {
1216 	ill_t	*ill = (ill_t *)ill_arg;
1217 
1218 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1219 	if (ire->ire_ill == ill)
1220 		ire_delete(ire);
1221 }
1222 
1223 /* Consume an M_IOCACK of the fastpath probe. */
1224 void
1225 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1226 {
1227 	mblk_t	*mp1 = mp;
1228 
1229 	/*
1230 	 * If this was the first attempt turn on the fastpath probing.
1231 	 */
1232 	mutex_enter(&ill->ill_lock);
1233 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1234 		ill->ill_dlpi_fastpath_state = IDS_OK;
1235 	mutex_exit(&ill->ill_lock);
1236 
1237 	/* Free the M_IOCACK mblk, hold on to the data */
1238 	mp = mp->b_cont;
1239 	freeb(mp1);
1240 	if (mp == NULL)
1241 		return;
1242 	if (mp->b_cont != NULL)
1243 		nce_fastpath_update(ill, mp);
1244 	else
1245 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1246 	freemsg(mp);
1247 }
1248 
1249 /*
1250  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1251  * The data portion of the request is a dl_unitdata_req_t template for
1252  * what we would send downstream in the absence of a fastpath confirmation.
1253  */
1254 int
1255 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1256 {
1257 	struct iocblk	*ioc;
1258 	mblk_t	*mp;
1259 
1260 	if (dlur_mp == NULL)
1261 		return (EINVAL);
1262 
1263 	mutex_enter(&ill->ill_lock);
1264 	switch (ill->ill_dlpi_fastpath_state) {
1265 	case IDS_FAILED:
1266 		/*
1267 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1268 		 * support it.
1269 		 */
1270 		mutex_exit(&ill->ill_lock);
1271 		return (ENOTSUP);
1272 	case IDS_UNKNOWN:
1273 		/* This is the first probe */
1274 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1275 		break;
1276 	default:
1277 		break;
1278 	}
1279 	mutex_exit(&ill->ill_lock);
1280 
1281 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1282 		return (EAGAIN);
1283 
1284 	mp->b_cont = copyb(dlur_mp);
1285 	if (mp->b_cont == NULL) {
1286 		freeb(mp);
1287 		return (EAGAIN);
1288 	}
1289 
1290 	ioc = (struct iocblk *)mp->b_rptr;
1291 	ioc->ioc_count = msgdsize(mp->b_cont);
1292 
1293 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1294 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1295 	putnext(ill->ill_wq, mp);
1296 	return (0);
1297 }
1298 
1299 void
1300 ill_capability_probe(ill_t *ill)
1301 {
1302 	mblk_t	*mp;
1303 
1304 	ASSERT(IAM_WRITER_ILL(ill));
1305 
1306 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1307 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1308 		return;
1309 
1310 	/*
1311 	 * We are starting a new cycle of capability negotiation.
1312 	 * Free up the capab reset messages of any previous incarnation.
1313 	 * We will do a fresh allocation when we get the response to our probe
1314 	 */
1315 	if (ill->ill_capab_reset_mp != NULL) {
1316 		freemsg(ill->ill_capab_reset_mp);
1317 		ill->ill_capab_reset_mp = NULL;
1318 	}
1319 
1320 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1321 
1322 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1323 	if (mp == NULL)
1324 		return;
1325 
1326 	ill_capability_send(ill, mp);
1327 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1328 }
1329 
1330 void
1331 ill_capability_reset(ill_t *ill, boolean_t reneg)
1332 {
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1336 		return;
1337 
1338 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1339 
1340 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1341 	ill->ill_capab_reset_mp = NULL;
1342 	/*
1343 	 * We turn off all capabilities except those pertaining to
1344 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1345 	 * which will be turned off by the corresponding reset functions.
1346 	 */
1347 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1348 }
1349 
1350 static void
1351 ill_capability_reset_alloc(ill_t *ill)
1352 {
1353 	mblk_t *mp;
1354 	size_t	size = 0;
1355 	int	err;
1356 	dl_capability_req_t	*capb;
1357 
1358 	ASSERT(IAM_WRITER_ILL(ill));
1359 	ASSERT(ill->ill_capab_reset_mp == NULL);
1360 
1361 	if (ILL_HCKSUM_CAPABLE(ill)) {
1362 		size += sizeof (dl_capability_sub_t) +
1363 		    sizeof (dl_capab_hcksum_t);
1364 	}
1365 
1366 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1367 		size += sizeof (dl_capability_sub_t) +
1368 		    sizeof (dl_capab_zerocopy_t);
1369 	}
1370 
1371 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1372 		size += sizeof (dl_capability_sub_t) +
1373 		    sizeof (dl_capab_dld_t);
1374 	}
1375 
1376 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1377 	    STR_NOSIG, &err);
1378 
1379 	mp->b_datap->db_type = M_PROTO;
1380 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1381 
1382 	capb = (dl_capability_req_t *)mp->b_rptr;
1383 	capb->dl_primitive = DL_CAPABILITY_REQ;
1384 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1385 	capb->dl_sub_length = size;
1386 
1387 	mp->b_wptr += sizeof (dl_capability_req_t);
1388 
1389 	/*
1390 	 * Each handler fills in the corresponding dl_capability_sub_t
1391 	 * inside the mblk,
1392 	 */
1393 	ill_capability_hcksum_reset_fill(ill, mp);
1394 	ill_capability_zerocopy_reset_fill(ill, mp);
1395 	ill_capability_dld_reset_fill(ill, mp);
1396 
1397 	ill->ill_capab_reset_mp = mp;
1398 }
1399 
1400 static void
1401 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1402 {
1403 	dl_capab_id_t *id_ic;
1404 	uint_t sub_dl_cap = outers->dl_cap;
1405 	dl_capability_sub_t *inners;
1406 	uint8_t *capend;
1407 
1408 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1409 
1410 	/*
1411 	 * Note: range checks here are not absolutely sufficient to
1412 	 * make us robust against malformed messages sent by drivers;
1413 	 * this is in keeping with the rest of IP's dlpi handling.
1414 	 * (Remember, it's coming from something else in the kernel
1415 	 * address space)
1416 	 */
1417 
1418 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1419 	if (capend > mp->b_wptr) {
1420 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1421 		    "malformed sub-capability too long for mblk");
1422 		return;
1423 	}
1424 
1425 	id_ic = (dl_capab_id_t *)(outers + 1);
1426 
1427 	if (outers->dl_length < sizeof (*id_ic) ||
1428 	    (inners = &id_ic->id_subcap,
1429 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1430 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1431 		    "encapsulated capab type %d too long for mblk",
1432 		    inners->dl_cap);
1433 		return;
1434 	}
1435 
1436 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1437 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1438 		    "isn't as expected; pass-thru module(s) detected, "
1439 		    "discarding capability\n", inners->dl_cap));
1440 		return;
1441 	}
1442 
1443 	/* Process the encapsulated sub-capability */
1444 	ill_capability_dispatch(ill, mp, inners);
1445 }
1446 
1447 static void
1448 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1449 {
1450 	dl_capability_sub_t *dl_subcap;
1451 
1452 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1453 		return;
1454 
1455 	/*
1456 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1457 	 * initialized below since it is not used by DLD.
1458 	 */
1459 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1460 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1461 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1462 
1463 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1464 }
1465 
1466 static void
1467 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1468 {
1469 	/*
1470 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1471 	 * is only to get the VRRP capability.
1472 	 *
1473 	 * Note that we cannot check ill_ipif_up_count here since
1474 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1475 	 * That is done asynchronously, and can race with this function.
1476 	 */
1477 	if (!ill->ill_dl_up) {
1478 		if (subp->dl_cap == DL_CAPAB_VRRP)
1479 			ill_capability_vrrp_ack(ill, mp, subp);
1480 		return;
1481 	}
1482 
1483 	switch (subp->dl_cap) {
1484 	case DL_CAPAB_HCKSUM:
1485 		ill_capability_hcksum_ack(ill, mp, subp);
1486 		break;
1487 	case DL_CAPAB_ZEROCOPY:
1488 		ill_capability_zerocopy_ack(ill, mp, subp);
1489 		break;
1490 	case DL_CAPAB_DLD:
1491 		ill_capability_dld_ack(ill, mp, subp);
1492 		break;
1493 	case DL_CAPAB_VRRP:
1494 		break;
1495 	default:
1496 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1497 		    subp->dl_cap));
1498 	}
1499 }
1500 
1501 /*
1502  * Process the vrrp capability received from a DLS Provider. isub must point
1503  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1504  */
1505 static void
1506 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1507 {
1508 	dl_capab_vrrp_t	*vrrp;
1509 	uint_t		sub_dl_cap = isub->dl_cap;
1510 	uint8_t		*capend;
1511 
1512 	ASSERT(IAM_WRITER_ILL(ill));
1513 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1514 
1515 	/*
1516 	 * Note: range checks here are not absolutely sufficient to
1517 	 * make us robust against malformed messages sent by drivers;
1518 	 * this is in keeping with the rest of IP's dlpi handling.
1519 	 * (Remember, it's coming from something else in the kernel
1520 	 * address space)
1521 	 */
1522 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1523 	if (capend > mp->b_wptr) {
1524 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1525 		    "malformed sub-capability too long for mblk");
1526 		return;
1527 	}
1528 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1529 
1530 	/*
1531 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1532 	 */
1533 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1534 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1535 		ill->ill_flags |= ILLF_VRRP;
1536 	}
1537 }
1538 
1539 /*
1540  * Process a hardware checksum offload capability negotiation ack received
1541  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1542  * of a DL_CAPABILITY_ACK message.
1543  */
1544 static void
1545 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1546 {
1547 	dl_capability_req_t	*ocap;
1548 	dl_capab_hcksum_t	*ihck, *ohck;
1549 	ill_hcksum_capab_t	**ill_hcksum;
1550 	mblk_t			*nmp = NULL;
1551 	uint_t			sub_dl_cap = isub->dl_cap;
1552 	uint8_t			*capend;
1553 
1554 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1555 
1556 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1557 
1558 	/*
1559 	 * Note: range checks here are not absolutely sufficient to
1560 	 * make us robust against malformed messages sent by drivers;
1561 	 * this is in keeping with the rest of IP's dlpi handling.
1562 	 * (Remember, it's coming from something else in the kernel
1563 	 * address space)
1564 	 */
1565 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1566 	if (capend > mp->b_wptr) {
1567 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1568 		    "malformed sub-capability too long for mblk");
1569 		return;
1570 	}
1571 
1572 	/*
1573 	 * There are two types of acks we process here:
1574 	 * 1. acks in reply to a (first form) generic capability req
1575 	 *    (no ENABLE flag set)
1576 	 * 2. acks in reply to a ENABLE capability req.
1577 	 *    (ENABLE flag set)
1578 	 */
1579 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1580 
1581 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1582 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1583 		    "unsupported hardware checksum "
1584 		    "sub-capability (version %d, expected %d)",
1585 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1586 		return;
1587 	}
1588 
1589 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1590 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1591 		    "checksum capability isn't as expected; pass-thru "
1592 		    "module(s) detected, discarding capability\n"));
1593 		return;
1594 	}
1595 
1596 #define	CURR_HCKSUM_CAPAB				\
1597 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1598 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1599 
1600 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1601 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1602 		/* do ENABLE processing */
1603 		if (*ill_hcksum == NULL) {
1604 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1605 			    KM_NOSLEEP);
1606 
1607 			if (*ill_hcksum == NULL) {
1608 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1609 				    "could not enable hcksum version %d "
1610 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1611 				    ill->ill_name);
1612 				return;
1613 			}
1614 		}
1615 
1616 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1617 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1618 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1619 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1620 		    "has enabled hardware checksumming\n ",
1621 		    ill->ill_name));
1622 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1623 		/*
1624 		 * Enabling hardware checksum offload
1625 		 * Currently IP supports {TCP,UDP}/IPv4
1626 		 * partial and full cksum offload and
1627 		 * IPv4 header checksum offload.
1628 		 * Allocate new mblk which will
1629 		 * contain a new capability request
1630 		 * to enable hardware checksum offload.
1631 		 */
1632 		uint_t	size;
1633 		uchar_t	*rptr;
1634 
1635 		size = sizeof (dl_capability_req_t) +
1636 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1637 
1638 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1639 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1640 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1641 			    ill->ill_name);
1642 			return;
1643 		}
1644 
1645 		rptr = nmp->b_rptr;
1646 		/* initialize dl_capability_req_t */
1647 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1648 		ocap->dl_sub_offset =
1649 		    sizeof (dl_capability_req_t);
1650 		ocap->dl_sub_length =
1651 		    sizeof (dl_capability_sub_t) +
1652 		    isub->dl_length;
1653 		nmp->b_rptr += sizeof (dl_capability_req_t);
1654 
1655 		/* initialize dl_capability_sub_t */
1656 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1657 		nmp->b_rptr += sizeof (*isub);
1658 
1659 		/* initialize dl_capab_hcksum_t */
1660 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1661 		bcopy(ihck, ohck, sizeof (*ihck));
1662 
1663 		nmp->b_rptr = rptr;
1664 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1665 
1666 		/* Set ENABLE flag */
1667 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1668 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1669 
1670 		/*
1671 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1672 		 * hardware checksum acceleration.
1673 		 */
1674 		ill_capability_send(ill, nmp);
1675 	} else {
1676 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1677 		    "advertised %x hardware checksum capability flags\n",
1678 		    ill->ill_name, ihck->hcksum_txflags));
1679 	}
1680 }
1681 
1682 static void
1683 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1684 {
1685 	dl_capab_hcksum_t *hck_subcap;
1686 	dl_capability_sub_t *dl_subcap;
1687 
1688 	if (!ILL_HCKSUM_CAPABLE(ill))
1689 		return;
1690 
1691 	ASSERT(ill->ill_hcksum_capab != NULL);
1692 
1693 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1694 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1695 	dl_subcap->dl_length = sizeof (*hck_subcap);
1696 
1697 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1698 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1699 	hck_subcap->hcksum_txflags = 0;
1700 
1701 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1702 }
1703 
1704 static void
1705 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1706 {
1707 	mblk_t *nmp = NULL;
1708 	dl_capability_req_t *oc;
1709 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1710 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1711 	uint_t sub_dl_cap = isub->dl_cap;
1712 	uint8_t *capend;
1713 
1714 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1715 
1716 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1717 
1718 	/*
1719 	 * Note: range checks here are not absolutely sufficient to
1720 	 * make us robust against malformed messages sent by drivers;
1721 	 * this is in keeping with the rest of IP's dlpi handling.
1722 	 * (Remember, it's coming from something else in the kernel
1723 	 * address space)
1724 	 */
1725 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1726 	if (capend > mp->b_wptr) {
1727 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1728 		    "malformed sub-capability too long for mblk");
1729 		return;
1730 	}
1731 
1732 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1733 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1734 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1735 		    "unsupported ZEROCOPY sub-capability (version %d, "
1736 		    "expected %d)", zc_ic->zerocopy_version,
1737 		    ZEROCOPY_VERSION_1);
1738 		return;
1739 	}
1740 
1741 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1742 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1743 		    "capability isn't as expected; pass-thru module(s) "
1744 		    "detected, discarding capability\n"));
1745 		return;
1746 	}
1747 
1748 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1749 		if (*ill_zerocopy_capab == NULL) {
1750 			*ill_zerocopy_capab =
1751 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1752 			    KM_NOSLEEP);
1753 
1754 			if (*ill_zerocopy_capab == NULL) {
1755 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1756 				    "could not enable Zero-copy version %d "
1757 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1758 				    ill->ill_name);
1759 				return;
1760 			}
1761 		}
1762 
1763 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1764 		    "supports Zero-copy version %d\n", ill->ill_name,
1765 		    ZEROCOPY_VERSION_1));
1766 
1767 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1768 		    zc_ic->zerocopy_version;
1769 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1770 		    zc_ic->zerocopy_flags;
1771 
1772 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1773 	} else {
1774 		uint_t size;
1775 		uchar_t *rptr;
1776 
1777 		size = sizeof (dl_capability_req_t) +
1778 		    sizeof (dl_capability_sub_t) +
1779 		    sizeof (dl_capab_zerocopy_t);
1780 
1781 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1782 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1783 			    "could not enable zerocopy for %s (ENOMEM)\n",
1784 			    ill->ill_name);
1785 			return;
1786 		}
1787 
1788 		rptr = nmp->b_rptr;
1789 		/* initialize dl_capability_req_t */
1790 		oc = (dl_capability_req_t *)rptr;
1791 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1792 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1793 		    sizeof (dl_capab_zerocopy_t);
1794 		rptr += sizeof (dl_capability_req_t);
1795 
1796 		/* initialize dl_capability_sub_t */
1797 		bcopy(isub, rptr, sizeof (*isub));
1798 		rptr += sizeof (*isub);
1799 
1800 		/* initialize dl_capab_zerocopy_t */
1801 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1802 		*zc_oc = *zc_ic;
1803 
1804 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1805 		    "to enable zero-copy version %d\n", ill->ill_name,
1806 		    ZEROCOPY_VERSION_1));
1807 
1808 		/* set VMSAFE_MEM flag */
1809 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1810 
1811 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1812 		ill_capability_send(ill, nmp);
1813 	}
1814 }
1815 
1816 static void
1817 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1818 {
1819 	dl_capab_zerocopy_t *zerocopy_subcap;
1820 	dl_capability_sub_t *dl_subcap;
1821 
1822 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1823 		return;
1824 
1825 	ASSERT(ill->ill_zerocopy_capab != NULL);
1826 
1827 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1828 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1829 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1830 
1831 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1832 	zerocopy_subcap->zerocopy_version =
1833 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1834 	zerocopy_subcap->zerocopy_flags = 0;
1835 
1836 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1837 }
1838 
1839 /*
1840  * DLD capability
1841  * Refer to dld.h for more information regarding the purpose and usage
1842  * of this capability.
1843  */
1844 static void
1845 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1846 {
1847 	dl_capab_dld_t		*dld_ic, dld;
1848 	uint_t			sub_dl_cap = isub->dl_cap;
1849 	uint8_t			*capend;
1850 	ill_dld_capab_t		*idc;
1851 
1852 	ASSERT(IAM_WRITER_ILL(ill));
1853 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1854 
1855 	/*
1856 	 * Note: range checks here are not absolutely sufficient to
1857 	 * make us robust against malformed messages sent by drivers;
1858 	 * this is in keeping with the rest of IP's dlpi handling.
1859 	 * (Remember, it's coming from something else in the kernel
1860 	 * address space)
1861 	 */
1862 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1863 	if (capend > mp->b_wptr) {
1864 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1865 		    "malformed sub-capability too long for mblk");
1866 		return;
1867 	}
1868 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1869 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1870 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1871 		    "unsupported DLD sub-capability (version %d, "
1872 		    "expected %d)", dld_ic->dld_version,
1873 		    DLD_CURRENT_VERSION);
1874 		return;
1875 	}
1876 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1877 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1878 		    "capability isn't as expected; pass-thru module(s) "
1879 		    "detected, discarding capability\n"));
1880 		return;
1881 	}
1882 
1883 	/*
1884 	 * Copy locally to ensure alignment.
1885 	 */
1886 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1887 
1888 	if ((idc = ill->ill_dld_capab) == NULL) {
1889 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1890 		if (idc == NULL) {
1891 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1892 			    "could not enable DLD version %d "
1893 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1894 			    ill->ill_name);
1895 			return;
1896 		}
1897 		ill->ill_dld_capab = idc;
1898 	}
1899 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1900 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1901 	ip1dbg(("ill_capability_dld_ack: interface %s "
1902 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1903 
1904 	ill_capability_dld_enable(ill);
1905 }
1906 
1907 /*
1908  * Typically capability negotiation between IP and the driver happens via
1909  * DLPI message exchange. However GLD also offers a direct function call
1910  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1911  * But arbitrary function calls into IP or GLD are not permitted, since both
1912  * of them are protected by their own perimeter mechanism. The perimeter can
1913  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1914  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1915  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1916  * to enter the mac perimeter and then do the direct function calls into
1917  * GLD to enable squeue polling. The ring related callbacks from the mac into
1918  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1919  * protected by the mac perimeter.
1920  */
1921 static void
1922 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1923 {
1924 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1925 	int			err;
1926 
1927 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1928 	    DLD_ENABLE);
1929 	ASSERT(err == 0);
1930 }
1931 
1932 static void
1933 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1934 {
1935 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1936 	int			err;
1937 
1938 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1939 	    DLD_DISABLE);
1940 	ASSERT(err == 0);
1941 }
1942 
1943 boolean_t
1944 ill_mac_perim_held(ill_t *ill)
1945 {
1946 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1947 
1948 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1949 	    DLD_QUERY));
1950 }
1951 
1952 static void
1953 ill_capability_direct_enable(ill_t *ill)
1954 {
1955 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1956 	ill_dld_direct_t	*idd = &idc->idc_direct;
1957 	dld_capab_direct_t	direct;
1958 	int			rc;
1959 
1960 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1961 
1962 	bzero(&direct, sizeof (direct));
1963 	direct.di_rx_cf = (uintptr_t)ip_input;
1964 	direct.di_rx_ch = ill;
1965 
1966 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1967 	    DLD_ENABLE);
1968 	if (rc == 0) {
1969 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1970 		idd->idd_tx_dh = direct.di_tx_dh;
1971 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1972 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1973 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1974 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1975 		ASSERT(idd->idd_tx_cb_df != NULL);
1976 		ASSERT(idd->idd_tx_fctl_df != NULL);
1977 		ASSERT(idd->idd_tx_df != NULL);
1978 		/*
1979 		 * One time registration of flow enable callback function
1980 		 */
1981 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
1982 		    ill_flow_enable, ill);
1983 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
1984 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
1985 	} else {
1986 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
1987 		    "capability, rc = %d\n", rc);
1988 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
1989 	}
1990 }
1991 
1992 static void
1993 ill_capability_poll_enable(ill_t *ill)
1994 {
1995 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1996 	dld_capab_poll_t	poll;
1997 	int			rc;
1998 
1999 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2000 
2001 	bzero(&poll, sizeof (poll));
2002 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2003 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2004 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2005 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2006 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2007 	poll.poll_ring_ch = ill;
2008 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2009 	    DLD_ENABLE);
2010 	if (rc == 0) {
2011 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2012 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2013 	} else {
2014 		ip1dbg(("warning: could not enable POLL "
2015 		    "capability, rc = %d\n", rc));
2016 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2017 	}
2018 }
2019 
2020 /*
2021  * Enable the LSO capability.
2022  */
2023 static void
2024 ill_capability_lso_enable(ill_t *ill)
2025 {
2026 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2027 	dld_capab_lso_t	lso;
2028 	int rc;
2029 
2030 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2031 
2032 	if (ill->ill_lso_capab == NULL) {
2033 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2034 		    KM_NOSLEEP);
2035 		if (ill->ill_lso_capab == NULL) {
2036 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2037 			    "could not enable LSO for %s (ENOMEM)\n",
2038 			    ill->ill_name);
2039 			return;
2040 		}
2041 	}
2042 
2043 	bzero(&lso, sizeof (lso));
2044 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2045 	    DLD_ENABLE)) == 0) {
2046 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2047 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2048 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2049 		ip1dbg(("ill_capability_lso_enable: interface %s "
2050 		    "has enabled LSO\n ", ill->ill_name));
2051 	} else {
2052 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2053 		ill->ill_lso_capab = NULL;
2054 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2055 	}
2056 }
2057 
2058 static void
2059 ill_capability_dld_enable(ill_t *ill)
2060 {
2061 	mac_perim_handle_t mph;
2062 
2063 	ASSERT(IAM_WRITER_ILL(ill));
2064 
2065 	if (ill->ill_isv6)
2066 		return;
2067 
2068 	ill_mac_perim_enter(ill, &mph);
2069 	if (!ill->ill_isv6) {
2070 		ill_capability_direct_enable(ill);
2071 		ill_capability_poll_enable(ill);
2072 		ill_capability_lso_enable(ill);
2073 	}
2074 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2075 	ill_mac_perim_exit(ill, mph);
2076 }
2077 
2078 static void
2079 ill_capability_dld_disable(ill_t *ill)
2080 {
2081 	ill_dld_capab_t	*idc;
2082 	ill_dld_direct_t *idd;
2083 	mac_perim_handle_t	mph;
2084 
2085 	ASSERT(IAM_WRITER_ILL(ill));
2086 
2087 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2088 		return;
2089 
2090 	ill_mac_perim_enter(ill, &mph);
2091 
2092 	idc = ill->ill_dld_capab;
2093 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2094 		/*
2095 		 * For performance we avoid locks in the transmit data path
2096 		 * and don't maintain a count of the number of threads using
2097 		 * direct calls. Thus some threads could be using direct
2098 		 * transmit calls to GLD, even after the capability mechanism
2099 		 * turns it off. This is still safe since the handles used in
2100 		 * the direct calls continue to be valid until the unplumb is
2101 		 * completed. Remove the callback that was added (1-time) at
2102 		 * capab enable time.
2103 		 */
2104 		mutex_enter(&ill->ill_lock);
2105 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2106 		mutex_exit(&ill->ill_lock);
2107 		if (ill->ill_flownotify_mh != NULL) {
2108 			idd = &idc->idc_direct;
2109 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2110 			    ill->ill_flownotify_mh);
2111 			ill->ill_flownotify_mh = NULL;
2112 		}
2113 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2114 		    NULL, DLD_DISABLE);
2115 	}
2116 
2117 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2118 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2119 		ip_squeue_clean_all(ill);
2120 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2121 		    NULL, DLD_DISABLE);
2122 	}
2123 
2124 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2125 		ASSERT(ill->ill_lso_capab != NULL);
2126 		/*
2127 		 * Clear the capability flag for LSO but retain the
2128 		 * ill_lso_capab structure since it's possible that another
2129 		 * thread is still referring to it.  The structure only gets
2130 		 * deallocated when we destroy the ill.
2131 		 */
2132 
2133 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2134 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2135 		    NULL, DLD_DISABLE);
2136 	}
2137 
2138 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2139 	ill_mac_perim_exit(ill, mph);
2140 }
2141 
2142 /*
2143  * Capability Negotiation protocol
2144  *
2145  * We don't wait for DLPI capability operations to finish during interface
2146  * bringup or teardown. Doing so would introduce more asynchrony and the
2147  * interface up/down operations will need multiple return and restarts.
2148  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2149  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2150  * exclusive operation won't start until the DLPI operations of the previous
2151  * exclusive operation complete.
2152  *
2153  * The capability state machine is shown below.
2154  *
2155  * state		next state		event, action
2156  *
2157  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2158  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2159  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2160  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2161  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2162  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2163  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2164  *						    ill_capability_probe.
2165  */
2166 
2167 /*
2168  * Dedicated thread started from ip_stack_init that handles capability
2169  * disable. This thread ensures the taskq dispatch does not fail by waiting
2170  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2171  * that direct calls to DLD are done in a cv_waitable context.
2172  */
2173 void
2174 ill_taskq_dispatch(ip_stack_t *ipst)
2175 {
2176 	callb_cpr_t cprinfo;
2177 	char 	name[64];
2178 	mblk_t	*mp;
2179 
2180 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2181 	    ipst->ips_netstack->netstack_stackid);
2182 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2183 	    name);
2184 	mutex_enter(&ipst->ips_capab_taskq_lock);
2185 
2186 	for (;;) {
2187 		mp = ipst->ips_capab_taskq_head;
2188 		while (mp != NULL) {
2189 			ipst->ips_capab_taskq_head = mp->b_next;
2190 			if (ipst->ips_capab_taskq_head == NULL)
2191 				ipst->ips_capab_taskq_tail = NULL;
2192 			mutex_exit(&ipst->ips_capab_taskq_lock);
2193 			mp->b_next = NULL;
2194 
2195 			VERIFY(taskq_dispatch(system_taskq,
2196 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2197 			mutex_enter(&ipst->ips_capab_taskq_lock);
2198 			mp = ipst->ips_capab_taskq_head;
2199 		}
2200 
2201 		if (ipst->ips_capab_taskq_quit)
2202 			break;
2203 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2204 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2205 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2206 	}
2207 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2208 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2209 	CALLB_CPR_EXIT(&cprinfo);
2210 	thread_exit();
2211 }
2212 
2213 /*
2214  * Consume a new-style hardware capabilities negotiation ack.
2215  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2216  */
2217 static void
2218 ill_capability_ack_thr(void *arg)
2219 {
2220 	mblk_t	*mp = arg;
2221 	dl_capability_ack_t *capp;
2222 	dl_capability_sub_t *subp, *endp;
2223 	ill_t	*ill;
2224 	boolean_t reneg;
2225 
2226 	ill = (ill_t *)mp->b_prev;
2227 	mp->b_prev = NULL;
2228 
2229 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2230 
2231 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2232 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2233 		/*
2234 		 * We have received the ack for our DL_CAPAB reset request.
2235 		 * There isnt' anything in the message that needs processing.
2236 		 * All message based capabilities have been disabled, now
2237 		 * do the function call based capability disable.
2238 		 */
2239 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2240 		ill_capability_dld_disable(ill);
2241 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2242 		if (reneg)
2243 			ill_capability_probe(ill);
2244 		goto done;
2245 	}
2246 
2247 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2248 		ill->ill_dlpi_capab_state = IDCS_OK;
2249 
2250 	capp = (dl_capability_ack_t *)mp->b_rptr;
2251 
2252 	if (capp->dl_sub_length == 0) {
2253 		/* no new-style capabilities */
2254 		goto done;
2255 	}
2256 
2257 	/* make sure the driver supplied correct dl_sub_length */
2258 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2259 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2260 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2261 		goto done;
2262 	}
2263 
2264 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2265 	/*
2266 	 * There are sub-capabilities. Process the ones we know about.
2267 	 * Loop until we don't have room for another sub-cap header..
2268 	 */
2269 	for (subp = SC(capp, capp->dl_sub_offset),
2270 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2271 	    subp <= endp;
2272 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2273 
2274 		switch (subp->dl_cap) {
2275 		case DL_CAPAB_ID_WRAPPER:
2276 			ill_capability_id_ack(ill, mp, subp);
2277 			break;
2278 		default:
2279 			ill_capability_dispatch(ill, mp, subp);
2280 			break;
2281 		}
2282 	}
2283 #undef SC
2284 done:
2285 	inet_freemsg(mp);
2286 	ill_capability_done(ill);
2287 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2288 }
2289 
2290 /*
2291  * This needs to be started in a taskq thread to provide a cv_waitable
2292  * context.
2293  */
2294 void
2295 ill_capability_ack(ill_t *ill, mblk_t *mp)
2296 {
2297 	ip_stack_t	*ipst = ill->ill_ipst;
2298 
2299 	mp->b_prev = (mblk_t *)ill;
2300 	ASSERT(mp->b_next == NULL);
2301 
2302 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2303 	    TQ_NOSLEEP) != 0)
2304 		return;
2305 
2306 	/*
2307 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2308 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2309 	 */
2310 	mutex_enter(&ipst->ips_capab_taskq_lock);
2311 	if (ipst->ips_capab_taskq_head == NULL) {
2312 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2313 		ipst->ips_capab_taskq_head = mp;
2314 	} else {
2315 		ipst->ips_capab_taskq_tail->b_next = mp;
2316 	}
2317 	ipst->ips_capab_taskq_tail = mp;
2318 
2319 	cv_signal(&ipst->ips_capab_taskq_cv);
2320 	mutex_exit(&ipst->ips_capab_taskq_lock);
2321 }
2322 
2323 /*
2324  * This routine is called to scan the fragmentation reassembly table for
2325  * the specified ILL for any packets that are starting to smell.
2326  * dead_interval is the maximum time in seconds that will be tolerated.  It
2327  * will either be the value specified in ip_g_frag_timeout, or zero if the
2328  * ILL is shutting down and it is time to blow everything off.
2329  *
2330  * It returns the number of seconds (as a time_t) that the next frag timer
2331  * should be scheduled for, 0 meaning that the timer doesn't need to be
2332  * re-started.  Note that the method of calculating next_timeout isn't
2333  * entirely accurate since time will flow between the time we grab
2334  * current_time and the time we schedule the next timeout.  This isn't a
2335  * big problem since this is the timer for sending an ICMP reassembly time
2336  * exceeded messages, and it doesn't have to be exactly accurate.
2337  *
2338  * This function is
2339  * sometimes called as writer, although this is not required.
2340  */
2341 time_t
2342 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2343 {
2344 	ipfb_t	*ipfb;
2345 	ipfb_t	*endp;
2346 	ipf_t	*ipf;
2347 	ipf_t	*ipfnext;
2348 	mblk_t	*mp;
2349 	time_t	current_time = gethrestime_sec();
2350 	time_t	next_timeout = 0;
2351 	uint32_t	hdr_length;
2352 	mblk_t	*send_icmp_head;
2353 	mblk_t	*send_icmp_head_v6;
2354 	ip_stack_t *ipst = ill->ill_ipst;
2355 	ip_recv_attr_t iras;
2356 
2357 	bzero(&iras, sizeof (iras));
2358 	iras.ira_flags = 0;
2359 	iras.ira_ill = iras.ira_rill = ill;
2360 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2361 	iras.ira_rifindex = iras.ira_ruifindex;
2362 
2363 	ipfb = ill->ill_frag_hash_tbl;
2364 	if (ipfb == NULL)
2365 		return (B_FALSE);
2366 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2367 	/* Walk the frag hash table. */
2368 	for (; ipfb < endp; ipfb++) {
2369 		send_icmp_head = NULL;
2370 		send_icmp_head_v6 = NULL;
2371 		mutex_enter(&ipfb->ipfb_lock);
2372 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2373 			time_t frag_time = current_time - ipf->ipf_timestamp;
2374 			time_t frag_timeout;
2375 
2376 			if (frag_time < dead_interval) {
2377 				/*
2378 				 * There are some outstanding fragments
2379 				 * that will timeout later.  Make note of
2380 				 * the time so that we can reschedule the
2381 				 * next timeout appropriately.
2382 				 */
2383 				frag_timeout = dead_interval - frag_time;
2384 				if (next_timeout == 0 ||
2385 				    frag_timeout < next_timeout) {
2386 					next_timeout = frag_timeout;
2387 				}
2388 				break;
2389 			}
2390 			/* Time's up.  Get it out of here. */
2391 			hdr_length = ipf->ipf_nf_hdr_len;
2392 			ipfnext = ipf->ipf_hash_next;
2393 			if (ipfnext)
2394 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2395 			*ipf->ipf_ptphn = ipfnext;
2396 			mp = ipf->ipf_mp->b_cont;
2397 			for (; mp; mp = mp->b_cont) {
2398 				/* Extra points for neatness. */
2399 				IP_REASS_SET_START(mp, 0);
2400 				IP_REASS_SET_END(mp, 0);
2401 			}
2402 			mp = ipf->ipf_mp->b_cont;
2403 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2404 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2405 			ipfb->ipfb_count -= ipf->ipf_count;
2406 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2407 			ipfb->ipfb_frag_pkts--;
2408 			/*
2409 			 * We do not send any icmp message from here because
2410 			 * we currently are holding the ipfb_lock for this
2411 			 * hash chain. If we try and send any icmp messages
2412 			 * from here we may end up via a put back into ip
2413 			 * trying to get the same lock, causing a recursive
2414 			 * mutex panic. Instead we build a list and send all
2415 			 * the icmp messages after we have dropped the lock.
2416 			 */
2417 			if (ill->ill_isv6) {
2418 				if (hdr_length != 0) {
2419 					mp->b_next = send_icmp_head_v6;
2420 					send_icmp_head_v6 = mp;
2421 				} else {
2422 					freemsg(mp);
2423 				}
2424 			} else {
2425 				if (hdr_length != 0) {
2426 					mp->b_next = send_icmp_head;
2427 					send_icmp_head = mp;
2428 				} else {
2429 					freemsg(mp);
2430 				}
2431 			}
2432 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2433 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2434 			freeb(ipf->ipf_mp);
2435 		}
2436 		mutex_exit(&ipfb->ipfb_lock);
2437 		/*
2438 		 * Now need to send any icmp messages that we delayed from
2439 		 * above.
2440 		 */
2441 		while (send_icmp_head_v6 != NULL) {
2442 			ip6_t *ip6h;
2443 
2444 			mp = send_icmp_head_v6;
2445 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2446 			mp->b_next = NULL;
2447 			ip6h = (ip6_t *)mp->b_rptr;
2448 			iras.ira_flags = 0;
2449 			/*
2450 			 * This will result in an incorrect ALL_ZONES zoneid
2451 			 * for multicast packets, but we
2452 			 * don't send ICMP errors for those in any case.
2453 			 */
2454 			iras.ira_zoneid =
2455 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2456 			    ill, ipst);
2457 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2458 			icmp_time_exceeded_v6(mp,
2459 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2460 			    &iras);
2461 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2462 		}
2463 		while (send_icmp_head != NULL) {
2464 			ipaddr_t dst;
2465 
2466 			mp = send_icmp_head;
2467 			send_icmp_head = send_icmp_head->b_next;
2468 			mp->b_next = NULL;
2469 
2470 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2471 
2472 			iras.ira_flags = IRAF_IS_IPV4;
2473 			/*
2474 			 * This will result in an incorrect ALL_ZONES zoneid
2475 			 * for broadcast and multicast packets, but we
2476 			 * don't send ICMP errors for those in any case.
2477 			 */
2478 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2479 			    ill, ipst);
2480 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2481 			icmp_time_exceeded(mp,
2482 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2483 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2484 		}
2485 	}
2486 	/*
2487 	 * A non-dying ILL will use the return value to decide whether to
2488 	 * restart the frag timer, and for how long.
2489 	 */
2490 	return (next_timeout);
2491 }
2492 
2493 /*
2494  * This routine is called when the approximate count of mblk memory used
2495  * for the specified ILL has exceeded max_count.
2496  */
2497 void
2498 ill_frag_prune(ill_t *ill, uint_t max_count)
2499 {
2500 	ipfb_t	*ipfb;
2501 	ipf_t	*ipf;
2502 	size_t	count;
2503 	clock_t now;
2504 
2505 	/*
2506 	 * If we are here within ip_min_frag_prune_time msecs remove
2507 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2508 	 * ill_frag_free_num_pkts.
2509 	 */
2510 	mutex_enter(&ill->ill_lock);
2511 	now = ddi_get_lbolt();
2512 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2513 	    (ip_min_frag_prune_time != 0 ?
2514 	    ip_min_frag_prune_time : msec_per_tick)) {
2515 
2516 		ill->ill_frag_free_num_pkts++;
2517 
2518 	} else {
2519 		ill->ill_frag_free_num_pkts = 0;
2520 	}
2521 	ill->ill_last_frag_clean_time = now;
2522 	mutex_exit(&ill->ill_lock);
2523 
2524 	/*
2525 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2526 	 */
2527 	if (ill->ill_frag_free_num_pkts != 0) {
2528 		int ix;
2529 
2530 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2531 			ipfb = &ill->ill_frag_hash_tbl[ix];
2532 			mutex_enter(&ipfb->ipfb_lock);
2533 			if (ipfb->ipfb_ipf != NULL) {
2534 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2535 				    ill->ill_frag_free_num_pkts);
2536 			}
2537 			mutex_exit(&ipfb->ipfb_lock);
2538 		}
2539 	}
2540 	/*
2541 	 * While the reassembly list for this ILL is too big, prune a fragment
2542 	 * queue by age, oldest first.
2543 	 */
2544 	while (ill->ill_frag_count > max_count) {
2545 		int	ix;
2546 		ipfb_t	*oipfb = NULL;
2547 		uint_t	oldest = UINT_MAX;
2548 
2549 		count = 0;
2550 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2551 			ipfb = &ill->ill_frag_hash_tbl[ix];
2552 			mutex_enter(&ipfb->ipfb_lock);
2553 			ipf = ipfb->ipfb_ipf;
2554 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2555 				oldest = ipf->ipf_gen;
2556 				oipfb = ipfb;
2557 			}
2558 			count += ipfb->ipfb_count;
2559 			mutex_exit(&ipfb->ipfb_lock);
2560 		}
2561 		if (oipfb == NULL)
2562 			break;
2563 
2564 		if (count <= max_count)
2565 			return;	/* Somebody beat us to it, nothing to do */
2566 		mutex_enter(&oipfb->ipfb_lock);
2567 		ipf = oipfb->ipfb_ipf;
2568 		if (ipf != NULL) {
2569 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2570 		}
2571 		mutex_exit(&oipfb->ipfb_lock);
2572 	}
2573 }
2574 
2575 /*
2576  * free 'free_cnt' fragmented packets starting at ipf.
2577  */
2578 void
2579 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2580 {
2581 	size_t	count;
2582 	mblk_t	*mp;
2583 	mblk_t	*tmp;
2584 	ipf_t **ipfp = ipf->ipf_ptphn;
2585 
2586 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2587 	ASSERT(ipfp != NULL);
2588 	ASSERT(ipf != NULL);
2589 
2590 	while (ipf != NULL && free_cnt-- > 0) {
2591 		count = ipf->ipf_count;
2592 		mp = ipf->ipf_mp;
2593 		ipf = ipf->ipf_hash_next;
2594 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2595 			IP_REASS_SET_START(tmp, 0);
2596 			IP_REASS_SET_END(tmp, 0);
2597 		}
2598 		atomic_add_32(&ill->ill_frag_count, -count);
2599 		ASSERT(ipfb->ipfb_count >= count);
2600 		ipfb->ipfb_count -= count;
2601 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2602 		ipfb->ipfb_frag_pkts--;
2603 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2604 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2605 		freemsg(mp);
2606 	}
2607 
2608 	if (ipf)
2609 		ipf->ipf_ptphn = ipfp;
2610 	ipfp[0] = ipf;
2611 }
2612 
2613 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2614 	"obsolete and may be removed in a future release of Solaris.  Use " \
2615 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2616 
2617 /*
2618  * For obsolete per-interface forwarding configuration;
2619  * called in response to ND_GET.
2620  */
2621 /* ARGSUSED */
2622 static int
2623 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2624 {
2625 	ill_t *ill = (ill_t *)cp;
2626 
2627 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2628 
2629 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2630 	return (0);
2631 }
2632 
2633 /*
2634  * For obsolete per-interface forwarding configuration;
2635  * called in response to ND_SET.
2636  */
2637 /* ARGSUSED */
2638 static int
2639 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2640     cred_t *ioc_cr)
2641 {
2642 	long value;
2643 	int retval;
2644 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2645 
2646 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2647 
2648 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2649 	    value < 0 || value > 1) {
2650 		return (EINVAL);
2651 	}
2652 
2653 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2654 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2655 	rw_exit(&ipst->ips_ill_g_lock);
2656 	return (retval);
2657 }
2658 
2659 /*
2660  * Helper function for ill_forward_set().
2661  */
2662 static void
2663 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2664 {
2665 	ip_stack_t	*ipst = ill->ill_ipst;
2666 
2667 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2668 
2669 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2670 	    (enable ? "Enabling" : "Disabling"),
2671 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2672 	mutex_enter(&ill->ill_lock);
2673 	if (enable)
2674 		ill->ill_flags |= ILLF_ROUTER;
2675 	else
2676 		ill->ill_flags &= ~ILLF_ROUTER;
2677 	mutex_exit(&ill->ill_lock);
2678 	if (ill->ill_isv6)
2679 		ill_set_nce_router_flags(ill, enable);
2680 	/* Notify routing socket listeners of this change. */
2681 	if (ill->ill_ipif != NULL)
2682 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2683 }
2684 
2685 /*
2686  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2687  * socket messages for each interface whose flags we change.
2688  */
2689 int
2690 ill_forward_set(ill_t *ill, boolean_t enable)
2691 {
2692 	ipmp_illgrp_t *illg;
2693 	ip_stack_t *ipst = ill->ill_ipst;
2694 
2695 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2696 
2697 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2698 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2699 		return (0);
2700 
2701 	if (IS_LOOPBACK(ill))
2702 		return (EINVAL);
2703 
2704 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2705 		/*
2706 		 * Update all of the interfaces in the group.
2707 		 */
2708 		illg = ill->ill_grp;
2709 		ill = list_head(&illg->ig_if);
2710 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2711 			ill_forward_set_on_ill(ill, enable);
2712 
2713 		/*
2714 		 * Update the IPMP meta-interface.
2715 		 */
2716 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2717 		return (0);
2718 	}
2719 
2720 	ill_forward_set_on_ill(ill, enable);
2721 	return (0);
2722 }
2723 
2724 /*
2725  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2726  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2727  * set or clear.
2728  */
2729 static void
2730 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2731 {
2732 	ipif_t *ipif;
2733 	ncec_t *ncec;
2734 	nce_t *nce;
2735 
2736 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2737 		/*
2738 		 * NOTE: we match across the illgrp because nce's for
2739 		 * addresses on IPMP interfaces have an nce_ill that points to
2740 		 * the bound underlying ill.
2741 		 */
2742 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2743 		if (nce != NULL) {
2744 			ncec = nce->nce_common;
2745 			mutex_enter(&ncec->ncec_lock);
2746 			if (enable)
2747 				ncec->ncec_flags |= NCE_F_ISROUTER;
2748 			else
2749 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2750 			mutex_exit(&ncec->ncec_lock);
2751 			nce_refrele(nce);
2752 		}
2753 	}
2754 }
2755 
2756 /*
2757  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2758  * for this ill.  Make sure the v6/v4 question has been answered about this
2759  * ill.  The creation of this ndd variable is only for backwards compatibility.
2760  * The preferred way to control per-interface IP forwarding is through the
2761  * ILLF_ROUTER interface flag.
2762  */
2763 static int
2764 ill_set_ndd_name(ill_t *ill)
2765 {
2766 	char *suffix;
2767 	ip_stack_t	*ipst = ill->ill_ipst;
2768 
2769 	ASSERT(IAM_WRITER_ILL(ill));
2770 
2771 	if (ill->ill_isv6)
2772 		suffix = ipv6_forward_suffix;
2773 	else
2774 		suffix = ipv4_forward_suffix;
2775 
2776 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2777 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2778 	/*
2779 	 * Copies over the '\0'.
2780 	 * Note that strlen(suffix) is always bounded.
2781 	 */
2782 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2783 	    strlen(suffix) + 1);
2784 
2785 	/*
2786 	 * Use of the nd table requires holding the reader lock.
2787 	 * Modifying the nd table thru nd_load/nd_unload requires
2788 	 * the writer lock.
2789 	 */
2790 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2791 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2792 	    nd_ill_forward_set, (caddr_t)ill)) {
2793 		/*
2794 		 * If the nd_load failed, it only meant that it could not
2795 		 * allocate a new bunch of room for further NDD expansion.
2796 		 * Because of that, the ill_ndd_name will be set to 0, and
2797 		 * this interface is at the mercy of the global ip_forwarding
2798 		 * variable.
2799 		 */
2800 		rw_exit(&ipst->ips_ip_g_nd_lock);
2801 		ill->ill_ndd_name = NULL;
2802 		return (ENOMEM);
2803 	}
2804 	rw_exit(&ipst->ips_ip_g_nd_lock);
2805 	return (0);
2806 }
2807 
2808 /*
2809  * Intializes the context structure and returns the first ill in the list
2810  * cuurently start_list and end_list can have values:
2811  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2812  * IP_V4_G_HEAD		Traverse IPV4 list only.
2813  * IP_V6_G_HEAD		Traverse IPV6 list only.
2814  */
2815 
2816 /*
2817  * We don't check for CONDEMNED ills here. Caller must do that if
2818  * necessary under the ill lock.
2819  */
2820 ill_t *
2821 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2822     ip_stack_t *ipst)
2823 {
2824 	ill_if_t *ifp;
2825 	ill_t *ill;
2826 	avl_tree_t *avl_tree;
2827 
2828 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2829 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2830 
2831 	/*
2832 	 * setup the lists to search
2833 	 */
2834 	if (end_list != MAX_G_HEADS) {
2835 		ctx->ctx_current_list = start_list;
2836 		ctx->ctx_last_list = end_list;
2837 	} else {
2838 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2839 		ctx->ctx_current_list = 0;
2840 	}
2841 
2842 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2843 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2844 		if (ifp != (ill_if_t *)
2845 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2846 			avl_tree = &ifp->illif_avl_by_ppa;
2847 			ill = avl_first(avl_tree);
2848 			/*
2849 			 * ill is guaranteed to be non NULL or ifp should have
2850 			 * not existed.
2851 			 */
2852 			ASSERT(ill != NULL);
2853 			return (ill);
2854 		}
2855 		ctx->ctx_current_list++;
2856 	}
2857 
2858 	return (NULL);
2859 }
2860 
2861 /*
2862  * returns the next ill in the list. ill_first() must have been called
2863  * before calling ill_next() or bad things will happen.
2864  */
2865 
2866 /*
2867  * We don't check for CONDEMNED ills here. Caller must do that if
2868  * necessary under the ill lock.
2869  */
2870 ill_t *
2871 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2872 {
2873 	ill_if_t *ifp;
2874 	ill_t *ill;
2875 	ip_stack_t	*ipst = lastill->ill_ipst;
2876 
2877 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2878 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2879 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2880 	    AVL_AFTER)) != NULL) {
2881 		return (ill);
2882 	}
2883 
2884 	/* goto next ill_ifp in the list. */
2885 	ifp = lastill->ill_ifptr->illif_next;
2886 
2887 	/* make sure not at end of circular list */
2888 	while (ifp ==
2889 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2890 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2891 			return (NULL);
2892 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2893 	}
2894 
2895 	return (avl_first(&ifp->illif_avl_by_ppa));
2896 }
2897 
2898 /*
2899  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2900  * The final number (PPA) must not have any leading zeros.  Upon success, a
2901  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2902  */
2903 static char *
2904 ill_get_ppa_ptr(char *name)
2905 {
2906 	int namelen = strlen(name);
2907 	int end_ndx = namelen - 1;
2908 	int ppa_ndx, i;
2909 
2910 	/*
2911 	 * Check that the first character is [a-zA-Z], and that the last
2912 	 * character is [0-9].
2913 	 */
2914 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2915 		return (NULL);
2916 
2917 	/*
2918 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2919 	 */
2920 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2921 		if (!isdigit(name[ppa_ndx - 1]))
2922 			break;
2923 
2924 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2925 		return (NULL);
2926 
2927 	/*
2928 	 * Check that the intermediate characters are [a-z0-9.]
2929 	 */
2930 	for (i = 1; i < ppa_ndx; i++) {
2931 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2932 		    name[i] != '.' && name[i] != '_') {
2933 			return (NULL);
2934 		}
2935 	}
2936 
2937 	return (name + ppa_ndx);
2938 }
2939 
2940 /*
2941  * use avl tree to locate the ill.
2942  */
2943 static ill_t *
2944 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2945 {
2946 	char *ppa_ptr = NULL;
2947 	int len;
2948 	uint_t ppa;
2949 	ill_t *ill = NULL;
2950 	ill_if_t *ifp;
2951 	int list;
2952 
2953 	/*
2954 	 * get ppa ptr
2955 	 */
2956 	if (isv6)
2957 		list = IP_V6_G_HEAD;
2958 	else
2959 		list = IP_V4_G_HEAD;
2960 
2961 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2962 		return (NULL);
2963 	}
2964 
2965 	len = ppa_ptr - name + 1;
2966 
2967 	ppa = stoi(&ppa_ptr);
2968 
2969 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2970 
2971 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2972 		/*
2973 		 * match is done on len - 1 as the name is not null
2974 		 * terminated it contains ppa in addition to the interface
2975 		 * name.
2976 		 */
2977 		if ((ifp->illif_name_len == len) &&
2978 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2979 			break;
2980 		} else {
2981 			ifp = ifp->illif_next;
2982 		}
2983 	}
2984 
2985 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2986 		/*
2987 		 * Even the interface type does not exist.
2988 		 */
2989 		return (NULL);
2990 	}
2991 
2992 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2993 	if (ill != NULL) {
2994 		mutex_enter(&ill->ill_lock);
2995 		if (ILL_CAN_LOOKUP(ill)) {
2996 			ill_refhold_locked(ill);
2997 			mutex_exit(&ill->ill_lock);
2998 			return (ill);
2999 		}
3000 		mutex_exit(&ill->ill_lock);
3001 	}
3002 	return (NULL);
3003 }
3004 
3005 /*
3006  * comparison function for use with avl.
3007  */
3008 static int
3009 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
3010 {
3011 	uint_t ppa;
3012 	uint_t ill_ppa;
3013 
3014 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
3015 
3016 	ppa = *((uint_t *)ppa_ptr);
3017 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
3018 	/*
3019 	 * We want the ill with the lowest ppa to be on the
3020 	 * top.
3021 	 */
3022 	if (ill_ppa < ppa)
3023 		return (1);
3024 	if (ill_ppa > ppa)
3025 		return (-1);
3026 	return (0);
3027 }
3028 
3029 /*
3030  * remove an interface type from the global list.
3031  */
3032 static void
3033 ill_delete_interface_type(ill_if_t *interface)
3034 {
3035 	ASSERT(interface != NULL);
3036 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3037 
3038 	avl_destroy(&interface->illif_avl_by_ppa);
3039 	if (interface->illif_ppa_arena != NULL)
3040 		vmem_destroy(interface->illif_ppa_arena);
3041 
3042 	remque(interface);
3043 
3044 	mi_free(interface);
3045 }
3046 
3047 /*
3048  * remove ill from the global list.
3049  */
3050 static void
3051 ill_glist_delete(ill_t *ill)
3052 {
3053 	ip_stack_t	*ipst;
3054 	phyint_t	*phyi;
3055 
3056 	if (ill == NULL)
3057 		return;
3058 	ipst = ill->ill_ipst;
3059 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3060 
3061 	/*
3062 	 * If the ill was never inserted into the AVL tree
3063 	 * we skip the if branch.
3064 	 */
3065 	if (ill->ill_ifptr != NULL) {
3066 		/*
3067 		 * remove from AVL tree and free ppa number
3068 		 */
3069 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3070 
3071 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3072 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3073 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3074 		}
3075 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3076 			ill_delete_interface_type(ill->ill_ifptr);
3077 		}
3078 
3079 		/*
3080 		 * Indicate ill is no longer in the list.
3081 		 */
3082 		ill->ill_ifptr = NULL;
3083 		ill->ill_name_length = 0;
3084 		ill->ill_name[0] = '\0';
3085 		ill->ill_ppa = UINT_MAX;
3086 	}
3087 
3088 	/* Generate one last event for this ill. */
3089 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3090 	    ill->ill_name_length);
3091 
3092 	ASSERT(ill->ill_phyint != NULL);
3093 	phyi = ill->ill_phyint;
3094 	ill->ill_phyint = NULL;
3095 
3096 	/*
3097 	 * ill_init allocates a phyint always to store the copy
3098 	 * of flags relevant to phyint. At that point in time, we could
3099 	 * not assign the name and hence phyint_illv4/v6 could not be
3100 	 * initialized. Later in ipif_set_values, we assign the name to
3101 	 * the ill, at which point in time we assign phyint_illv4/v6.
3102 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3103 	 */
3104 	if (ill->ill_flags & ILLF_IPV6)
3105 		phyi->phyint_illv6 = NULL;
3106 	else
3107 		phyi->phyint_illv4 = NULL;
3108 
3109 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3110 		rw_exit(&ipst->ips_ill_g_lock);
3111 		return;
3112 	}
3113 
3114 	/*
3115 	 * There are no ills left on this phyint; pull it out of the phyint
3116 	 * avl trees, and free it.
3117 	 */
3118 	if (phyi->phyint_ifindex > 0) {
3119 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3120 		    phyi);
3121 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3122 		    phyi);
3123 	}
3124 	rw_exit(&ipst->ips_ill_g_lock);
3125 
3126 	phyint_free(phyi);
3127 }
3128 
3129 /*
3130  * allocate a ppa, if the number of plumbed interfaces of this type are
3131  * less than ill_no_arena do a linear search to find a unused ppa.
3132  * When the number goes beyond ill_no_arena switch to using an arena.
3133  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3134  * is the return value for an error condition, so allocation starts at one
3135  * and is decremented by one.
3136  */
3137 static int
3138 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3139 {
3140 	ill_t *tmp_ill;
3141 	uint_t start, end;
3142 	int ppa;
3143 
3144 	if (ifp->illif_ppa_arena == NULL &&
3145 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3146 		/*
3147 		 * Create an arena.
3148 		 */
3149 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3150 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3151 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3152 			/* allocate what has already been assigned */
3153 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3154 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3155 		    tmp_ill, AVL_AFTER)) {
3156 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3157 			    1,		/* size */
3158 			    1,		/* align/quantum */
3159 			    0,		/* phase */
3160 			    0,		/* nocross */
3161 			    /* minaddr */
3162 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3163 			    /* maxaddr */
3164 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3165 			    VM_NOSLEEP|VM_FIRSTFIT);
3166 			if (ppa == 0) {
3167 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3168 				    " failed while switching"));
3169 				vmem_destroy(ifp->illif_ppa_arena);
3170 				ifp->illif_ppa_arena = NULL;
3171 				break;
3172 			}
3173 		}
3174 	}
3175 
3176 	if (ifp->illif_ppa_arena != NULL) {
3177 		if (ill->ill_ppa == UINT_MAX) {
3178 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3179 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3180 			if (ppa == 0)
3181 				return (EAGAIN);
3182 			ill->ill_ppa = --ppa;
3183 		} else {
3184 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3185 			    1, 		/* size */
3186 			    1, 		/* align/quantum */
3187 			    0, 		/* phase */
3188 			    0, 		/* nocross */
3189 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3190 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3191 			    VM_NOSLEEP|VM_FIRSTFIT);
3192 			/*
3193 			 * Most likely the allocation failed because
3194 			 * the requested ppa was in use.
3195 			 */
3196 			if (ppa == 0)
3197 				return (EEXIST);
3198 		}
3199 		return (0);
3200 	}
3201 
3202 	/*
3203 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3204 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3205 	 */
3206 	if (ill->ill_ppa == UINT_MAX) {
3207 		end = UINT_MAX - 1;
3208 		start = 0;
3209 	} else {
3210 		end = start = ill->ill_ppa;
3211 	}
3212 
3213 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3214 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3215 		if (start++ >= end) {
3216 			if (ill->ill_ppa == UINT_MAX)
3217 				return (EAGAIN);
3218 			else
3219 				return (EEXIST);
3220 		}
3221 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3222 	}
3223 	ill->ill_ppa = start;
3224 	return (0);
3225 }
3226 
3227 /*
3228  * Insert ill into the list of configured ill's. Once this function completes,
3229  * the ill is globally visible and is available through lookups. More precisely
3230  * this happens after the caller drops the ill_g_lock.
3231  */
3232 static int
3233 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3234 {
3235 	ill_if_t *ill_interface;
3236 	avl_index_t where = 0;
3237 	int error;
3238 	int name_length;
3239 	int index;
3240 	boolean_t check_length = B_FALSE;
3241 	ip_stack_t	*ipst = ill->ill_ipst;
3242 
3243 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3244 
3245 	name_length = mi_strlen(name) + 1;
3246 
3247 	if (isv6)
3248 		index = IP_V6_G_HEAD;
3249 	else
3250 		index = IP_V4_G_HEAD;
3251 
3252 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3253 	/*
3254 	 * Search for interface type based on name
3255 	 */
3256 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3257 		if ((ill_interface->illif_name_len == name_length) &&
3258 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3259 			break;
3260 		}
3261 		ill_interface = ill_interface->illif_next;
3262 	}
3263 
3264 	/*
3265 	 * Interface type not found, create one.
3266 	 */
3267 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3268 		ill_g_head_t ghead;
3269 
3270 		/*
3271 		 * allocate ill_if_t structure
3272 		 */
3273 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3274 		if (ill_interface == NULL) {
3275 			return (ENOMEM);
3276 		}
3277 
3278 		(void) strcpy(ill_interface->illif_name, name);
3279 		ill_interface->illif_name_len = name_length;
3280 
3281 		avl_create(&ill_interface->illif_avl_by_ppa,
3282 		    ill_compare_ppa, sizeof (ill_t),
3283 		    offsetof(struct ill_s, ill_avl_byppa));
3284 
3285 		/*
3286 		 * link the structure in the back to maintain order
3287 		 * of configuration for ifconfig output.
3288 		 */
3289 		ghead = ipst->ips_ill_g_heads[index];
3290 		insque(ill_interface, ghead.ill_g_list_tail);
3291 	}
3292 
3293 	if (ill->ill_ppa == UINT_MAX)
3294 		check_length = B_TRUE;
3295 
3296 	error = ill_alloc_ppa(ill_interface, ill);
3297 	if (error != 0) {
3298 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3299 			ill_delete_interface_type(ill->ill_ifptr);
3300 		return (error);
3301 	}
3302 
3303 	/*
3304 	 * When the ppa is choosen by the system, check that there is
3305 	 * enough space to insert ppa. if a specific ppa was passed in this
3306 	 * check is not required as the interface name passed in will have
3307 	 * the right ppa in it.
3308 	 */
3309 	if (check_length) {
3310 		/*
3311 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3312 		 */
3313 		char buf[sizeof (uint_t) * 3];
3314 
3315 		/*
3316 		 * convert ppa to string to calculate the amount of space
3317 		 * required for it in the name.
3318 		 */
3319 		numtos(ill->ill_ppa, buf);
3320 
3321 		/* Do we have enough space to insert ppa ? */
3322 
3323 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3324 			/* Free ppa and interface type struct */
3325 			if (ill_interface->illif_ppa_arena != NULL) {
3326 				vmem_free(ill_interface->illif_ppa_arena,
3327 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3328 			}
3329 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3330 				ill_delete_interface_type(ill->ill_ifptr);
3331 
3332 			return (EINVAL);
3333 		}
3334 	}
3335 
3336 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3337 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3338 
3339 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3340 	    &where);
3341 	ill->ill_ifptr = ill_interface;
3342 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3343 
3344 	ill_phyint_reinit(ill);
3345 	return (0);
3346 }
3347 
3348 /* Initialize the per phyint ipsq used for serialization */
3349 static boolean_t
3350 ipsq_init(ill_t *ill, boolean_t enter)
3351 {
3352 	ipsq_t  *ipsq;
3353 	ipxop_t	*ipx;
3354 
3355 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3356 		return (B_FALSE);
3357 
3358 	ill->ill_phyint->phyint_ipsq = ipsq;
3359 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3360 	ipx->ipx_ipsq = ipsq;
3361 	ipsq->ipsq_next = ipsq;
3362 	ipsq->ipsq_phyint = ill->ill_phyint;
3363 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3364 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3365 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3366 	if (enter) {
3367 		ipx->ipx_writer = curthread;
3368 		ipx->ipx_forced = B_FALSE;
3369 		ipx->ipx_reentry_cnt = 1;
3370 #ifdef DEBUG
3371 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3372 #endif
3373 	}
3374 	return (B_TRUE);
3375 }
3376 
3377 /*
3378  * ill_init is called by ip_open when a device control stream is opened.
3379  * It does a few initializations, and shoots a DL_INFO_REQ message down
3380  * to the driver.  The response is later picked up in ip_rput_dlpi and
3381  * used to set up default mechanisms for talking to the driver.  (Always
3382  * called as writer.)
3383  *
3384  * If this function returns error, ip_open will call ip_close which in
3385  * turn will call ill_delete to clean up any memory allocated here that
3386  * is not yet freed.
3387  */
3388 int
3389 ill_init(queue_t *q, ill_t *ill)
3390 {
3391 	int	count;
3392 	dl_info_req_t	*dlir;
3393 	mblk_t	*info_mp;
3394 	uchar_t *frag_ptr;
3395 
3396 	/*
3397 	 * The ill is initialized to zero by mi_alloc*(). In addition
3398 	 * some fields already contain valid values, initialized in
3399 	 * ip_open(), before we reach here.
3400 	 */
3401 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3402 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3403 	ill->ill_saved_ire_cnt = 0;
3404 
3405 	ill->ill_rq = q;
3406 	ill->ill_wq = WR(q);
3407 
3408 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3409 	    BPRI_HI);
3410 	if (info_mp == NULL)
3411 		return (ENOMEM);
3412 
3413 	/*
3414 	 * Allocate sufficient space to contain our fragment hash table and
3415 	 * the device name.
3416 	 */
3417 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3418 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3419 	if (frag_ptr == NULL) {
3420 		freemsg(info_mp);
3421 		return (ENOMEM);
3422 	}
3423 	ill->ill_frag_ptr = frag_ptr;
3424 	ill->ill_frag_free_num_pkts = 0;
3425 	ill->ill_last_frag_clean_time = 0;
3426 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3427 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3428 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3429 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3430 		    NULL, MUTEX_DEFAULT, NULL);
3431 	}
3432 
3433 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3434 	if (ill->ill_phyint == NULL) {
3435 		freemsg(info_mp);
3436 		mi_free(frag_ptr);
3437 		return (ENOMEM);
3438 	}
3439 
3440 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3441 	/*
3442 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3443 	 * at this point because of the following reason. If we can't
3444 	 * enter the ipsq at some point and cv_wait, the writer that
3445 	 * wakes us up tries to locate us using the list of all phyints
3446 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3447 	 * If we don't set it now, we risk a missed wakeup.
3448 	 */
3449 	ill->ill_phyint->phyint_illv4 = ill;
3450 	ill->ill_ppa = UINT_MAX;
3451 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3452 
3453 	ill_set_inputfn(ill);
3454 
3455 	if (!ipsq_init(ill, B_TRUE)) {
3456 		freemsg(info_mp);
3457 		mi_free(frag_ptr);
3458 		mi_free(ill->ill_phyint);
3459 		return (ENOMEM);
3460 	}
3461 
3462 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3463 
3464 	/* Frag queue limit stuff */
3465 	ill->ill_frag_count = 0;
3466 	ill->ill_ipf_gen = 0;
3467 
3468 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3469 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3470 	ill->ill_global_timer = INFINITY;
3471 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3472 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3473 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3474 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3475 
3476 	/*
3477 	 * Initialize IPv6 configuration variables.  The IP module is always
3478 	 * opened as an IPv4 module.  Instead tracking down the cases where
3479 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3480 	 * here for convenience, this has no effect until the ill is set to do
3481 	 * IPv6.
3482 	 */
3483 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3484 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3485 	ill->ill_max_buf = ND_MAX_Q;
3486 	ill->ill_refcnt = 0;
3487 
3488 	/* Send down the Info Request to the driver. */
3489 	info_mp->b_datap->db_type = M_PCPROTO;
3490 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3491 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3492 	dlir->dl_primitive = DL_INFO_REQ;
3493 
3494 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3495 
3496 	qprocson(q);
3497 	ill_dlpi_send(ill, info_mp);
3498 
3499 	return (0);
3500 }
3501 
3502 /*
3503  * ill_dls_info
3504  * creates datalink socket info from the device.
3505  */
3506 int
3507 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3508 {
3509 	size_t	len;
3510 
3511 	sdl->sdl_family = AF_LINK;
3512 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3513 	sdl->sdl_type = ill->ill_type;
3514 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3515 	len = strlen(sdl->sdl_data);
3516 	ASSERT(len < 256);
3517 	sdl->sdl_nlen = (uchar_t)len;
3518 	sdl->sdl_alen = ill->ill_phys_addr_length;
3519 	sdl->sdl_slen = 0;
3520 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3521 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3522 
3523 	return (sizeof (struct sockaddr_dl));
3524 }
3525 
3526 /*
3527  * ill_xarp_info
3528  * creates xarp info from the device.
3529  */
3530 static int
3531 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3532 {
3533 	sdl->sdl_family = AF_LINK;
3534 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3535 	sdl->sdl_type = ill->ill_type;
3536 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3537 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3538 	sdl->sdl_alen = ill->ill_phys_addr_length;
3539 	sdl->sdl_slen = 0;
3540 	return (sdl->sdl_nlen);
3541 }
3542 
3543 static int
3544 loopback_kstat_update(kstat_t *ksp, int rw)
3545 {
3546 	kstat_named_t *kn;
3547 	netstackid_t	stackid;
3548 	netstack_t	*ns;
3549 	ip_stack_t	*ipst;
3550 
3551 	if (ksp == NULL || ksp->ks_data == NULL)
3552 		return (EIO);
3553 
3554 	if (rw == KSTAT_WRITE)
3555 		return (EACCES);
3556 
3557 	kn = KSTAT_NAMED_PTR(ksp);
3558 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3559 
3560 	ns = netstack_find_by_stackid(stackid);
3561 	if (ns == NULL)
3562 		return (-1);
3563 
3564 	ipst = ns->netstack_ip;
3565 	if (ipst == NULL) {
3566 		netstack_rele(ns);
3567 		return (-1);
3568 	}
3569 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3570 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3571 	netstack_rele(ns);
3572 	return (0);
3573 }
3574 
3575 /*
3576  * Has ifindex been plumbed already?
3577  */
3578 static boolean_t
3579 phyint_exists(uint_t index, ip_stack_t *ipst)
3580 {
3581 	ASSERT(index != 0);
3582 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3583 
3584 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3585 	    &index, NULL) != NULL);
3586 }
3587 
3588 /* Pick a unique ifindex */
3589 boolean_t
3590 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3591 {
3592 	uint_t starting_index;
3593 
3594 	if (!ipst->ips_ill_index_wrap) {
3595 		*indexp = ipst->ips_ill_index++;
3596 		if (ipst->ips_ill_index == 0) {
3597 			/* Reached the uint_t limit Next time wrap  */
3598 			ipst->ips_ill_index_wrap = B_TRUE;
3599 		}
3600 		return (B_TRUE);
3601 	}
3602 
3603 	/*
3604 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3605 	 * at this point and don't want to call any function that attempts
3606 	 * to get the lock again.
3607 	 */
3608 	starting_index = ipst->ips_ill_index++;
3609 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3610 		if (ipst->ips_ill_index != 0 &&
3611 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3612 			/* found unused index - use it */
3613 			*indexp = ipst->ips_ill_index;
3614 			return (B_TRUE);
3615 		}
3616 	}
3617 
3618 	/*
3619 	 * all interface indicies are inuse.
3620 	 */
3621 	return (B_FALSE);
3622 }
3623 
3624 /*
3625  * Assign a unique interface index for the phyint.
3626  */
3627 static boolean_t
3628 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3629 {
3630 	ASSERT(phyi->phyint_ifindex == 0);
3631 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3632 }
3633 
3634 /*
3635  * Initialize the flags on `phyi' as per the provided mactype.
3636  */
3637 static void
3638 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3639 {
3640 	uint64_t flags = 0;
3641 
3642 	/*
3643 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3644 	 * we always presume the underlying hardware is working and set
3645 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3646 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3647 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3648 	 */
3649 	if (mactype == SUNW_DL_IPMP)
3650 		flags |= PHYI_FAILED;
3651 	else
3652 		flags |= PHYI_RUNNING;
3653 
3654 	switch (mactype) {
3655 	case SUNW_DL_VNI:
3656 		flags |= PHYI_VIRTUAL;
3657 		break;
3658 	case SUNW_DL_IPMP:
3659 		flags |= PHYI_IPMP;
3660 		break;
3661 	case DL_LOOP:
3662 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3663 		break;
3664 	}
3665 
3666 	mutex_enter(&phyi->phyint_lock);
3667 	phyi->phyint_flags |= flags;
3668 	mutex_exit(&phyi->phyint_lock);
3669 }
3670 
3671 /*
3672  * Return a pointer to the ill which matches the supplied name.  Note that
3673  * the ill name length includes the null termination character.  (May be
3674  * called as writer.)
3675  * If do_alloc and the interface is "lo0" it will be automatically created.
3676  * Cannot bump up reference on condemned ills. So dup detect can't be done
3677  * using this func.
3678  */
3679 ill_t *
3680 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3681     boolean_t *did_alloc, ip_stack_t *ipst)
3682 {
3683 	ill_t	*ill;
3684 	ipif_t	*ipif;
3685 	ipsq_t	*ipsq;
3686 	kstat_named_t	*kn;
3687 	boolean_t isloopback;
3688 	in6_addr_t ov6addr;
3689 
3690 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3691 
3692 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3693 	ill = ill_find_by_name(name, isv6, ipst);
3694 	rw_exit(&ipst->ips_ill_g_lock);
3695 	if (ill != NULL)
3696 		return (ill);
3697 
3698 	/*
3699 	 * Couldn't find it.  Does this happen to be a lookup for the
3700 	 * loopback device and are we allowed to allocate it?
3701 	 */
3702 	if (!isloopback || !do_alloc)
3703 		return (NULL);
3704 
3705 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3706 	ill = ill_find_by_name(name, isv6, ipst);
3707 	if (ill != NULL) {
3708 		rw_exit(&ipst->ips_ill_g_lock);
3709 		return (ill);
3710 	}
3711 
3712 	/* Create the loopback device on demand */
3713 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3714 	    sizeof (ipif_loopback_name), BPRI_MED));
3715 	if (ill == NULL)
3716 		goto done;
3717 
3718 	*ill = ill_null;
3719 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3720 	ill->ill_ipst = ipst;
3721 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3722 	netstack_hold(ipst->ips_netstack);
3723 	/*
3724 	 * For exclusive stacks we set the zoneid to zero
3725 	 * to make IP operate as if in the global zone.
3726 	 */
3727 	ill->ill_zoneid = GLOBAL_ZONEID;
3728 
3729 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3730 	if (ill->ill_phyint == NULL)
3731 		goto done;
3732 
3733 	if (isv6)
3734 		ill->ill_phyint->phyint_illv6 = ill;
3735 	else
3736 		ill->ill_phyint->phyint_illv4 = ill;
3737 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3738 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3739 
3740 	if (isv6) {
3741 		ill->ill_isv6 = B_TRUE;
3742 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3743 	} else {
3744 		ill->ill_max_frag = ip_loopback_mtuplus;
3745 	}
3746 	if (!ill_allocate_mibs(ill))
3747 		goto done;
3748 	ill->ill_current_frag = ill->ill_max_frag;
3749 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3750 	/*
3751 	 * ipif_loopback_name can't be pointed at directly because its used
3752 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3753 	 * from the glist, ill_glist_delete() sets the first character of
3754 	 * ill_name to '\0'.
3755 	 */
3756 	ill->ill_name = (char *)ill + sizeof (*ill);
3757 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3758 	ill->ill_name_length = sizeof (ipif_loopback_name);
3759 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3760 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3761 
3762 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3763 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3764 	ill->ill_global_timer = INFINITY;
3765 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3766 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3767 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3768 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3769 
3770 	/* No resolver here. */
3771 	ill->ill_net_type = IRE_LOOPBACK;
3772 
3773 	/* Initialize the ipsq */
3774 	if (!ipsq_init(ill, B_FALSE))
3775 		goto done;
3776 
3777 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3778 	if (ipif == NULL)
3779 		goto done;
3780 
3781 	ill->ill_flags = ILLF_MULTICAST;
3782 
3783 	ov6addr = ipif->ipif_v6lcl_addr;
3784 	/* Set up default loopback address and mask. */
3785 	if (!isv6) {
3786 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3787 
3788 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3789 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3790 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3791 		    ipif->ipif_v6subnet);
3792 		ill->ill_flags |= ILLF_IPV4;
3793 	} else {
3794 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3795 		ipif->ipif_v6net_mask = ipv6_all_ones;
3796 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3797 		    ipif->ipif_v6subnet);
3798 		ill->ill_flags |= ILLF_IPV6;
3799 	}
3800 
3801 	/*
3802 	 * Chain us in at the end of the ill list. hold the ill
3803 	 * before we make it globally visible. 1 for the lookup.
3804 	 */
3805 	ill->ill_refcnt = 0;
3806 	ill_refhold(ill);
3807 
3808 	ill->ill_frag_count = 0;
3809 	ill->ill_frag_free_num_pkts = 0;
3810 	ill->ill_last_frag_clean_time = 0;
3811 
3812 	ipsq = ill->ill_phyint->phyint_ipsq;
3813 
3814 	ill_set_inputfn(ill);
3815 
3816 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3817 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3818 
3819 	/* Let SCTP know so that it can add this to its list */
3820 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3821 
3822 	/*
3823 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3824 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3825 	 * requires to be after ill_glist_insert() since we need the
3826 	 * ill_index set. Pass on ipv6_loopback as the old address.
3827 	 */
3828 	sctp_update_ipif_addr(ipif, ov6addr);
3829 
3830 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3831 
3832 	/*
3833 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3834 	 * If so, free our original one.
3835 	 */
3836 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3837 		ipsq_delete(ipsq);
3838 
3839 	if (ipst->ips_loopback_ksp == NULL) {
3840 		/* Export loopback interface statistics */
3841 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3842 		    ipif_loopback_name, "net",
3843 		    KSTAT_TYPE_NAMED, 2, 0,
3844 		    ipst->ips_netstack->netstack_stackid);
3845 		if (ipst->ips_loopback_ksp != NULL) {
3846 			ipst->ips_loopback_ksp->ks_update =
3847 			    loopback_kstat_update;
3848 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3849 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3850 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3851 			ipst->ips_loopback_ksp->ks_private =
3852 			    (void *)(uintptr_t)ipst->ips_netstack->
3853 			    netstack_stackid;
3854 			kstat_install(ipst->ips_loopback_ksp);
3855 		}
3856 	}
3857 
3858 	*did_alloc = B_TRUE;
3859 	rw_exit(&ipst->ips_ill_g_lock);
3860 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3861 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3862 	return (ill);
3863 done:
3864 	if (ill != NULL) {
3865 		if (ill->ill_phyint != NULL) {
3866 			ipsq = ill->ill_phyint->phyint_ipsq;
3867 			if (ipsq != NULL) {
3868 				ipsq->ipsq_phyint = NULL;
3869 				ipsq_delete(ipsq);
3870 			}
3871 			mi_free(ill->ill_phyint);
3872 		}
3873 		ill_free_mib(ill);
3874 		if (ill->ill_ipst != NULL)
3875 			netstack_rele(ill->ill_ipst->ips_netstack);
3876 		mi_free(ill);
3877 	}
3878 	rw_exit(&ipst->ips_ill_g_lock);
3879 	return (NULL);
3880 }
3881 
3882 /*
3883  * For IPP calls - use the ip_stack_t for global stack.
3884  */
3885 ill_t *
3886 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3887 {
3888 	ip_stack_t	*ipst;
3889 	ill_t		*ill;
3890 
3891 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3892 	if (ipst == NULL) {
3893 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3894 		return (NULL);
3895 	}
3896 
3897 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3898 	netstack_rele(ipst->ips_netstack);
3899 	return (ill);
3900 }
3901 
3902 /*
3903  * Return a pointer to the ill which matches the index and IP version type.
3904  */
3905 ill_t *
3906 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3907 {
3908 	ill_t	*ill;
3909 	phyint_t *phyi;
3910 
3911 	/*
3912 	 * Indexes are stored in the phyint - a common structure
3913 	 * to both IPv4 and IPv6.
3914 	 */
3915 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3916 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3917 	    (void *) &index, NULL);
3918 	if (phyi != NULL) {
3919 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3920 		if (ill != NULL) {
3921 			mutex_enter(&ill->ill_lock);
3922 			if (!ILL_IS_CONDEMNED(ill)) {
3923 				ill_refhold_locked(ill);
3924 				mutex_exit(&ill->ill_lock);
3925 				rw_exit(&ipst->ips_ill_g_lock);
3926 				return (ill);
3927 			}
3928 			mutex_exit(&ill->ill_lock);
3929 		}
3930 	}
3931 	rw_exit(&ipst->ips_ill_g_lock);
3932 	return (NULL);
3933 }
3934 
3935 /*
3936  * Verify whether or not an interface index is valid for the specified zoneid
3937  * to transmit packets.
3938  * It can be zero (meaning "reset") or an interface index assigned
3939  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3940  */
3941 boolean_t
3942 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3943     ip_stack_t *ipst)
3944 {
3945 	ill_t		*ill;
3946 
3947 	if (ifindex == 0)
3948 		return (B_TRUE);
3949 
3950 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3951 	if (ill == NULL)
3952 		return (B_FALSE);
3953 	if (IS_VNI(ill)) {
3954 		ill_refrele(ill);
3955 		return (B_FALSE);
3956 	}
3957 	ill_refrele(ill);
3958 	return (B_TRUE);
3959 }
3960 
3961 /*
3962  * Return the ifindex next in sequence after the passed in ifindex.
3963  * If there is no next ifindex for the given protocol, return 0.
3964  */
3965 uint_t
3966 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3967 {
3968 	phyint_t *phyi;
3969 	phyint_t *phyi_initial;
3970 	uint_t   ifindex;
3971 
3972 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3973 
3974 	if (index == 0) {
3975 		phyi = avl_first(
3976 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3977 	} else {
3978 		phyi = phyi_initial = avl_find(
3979 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3980 		    (void *) &index, NULL);
3981 	}
3982 
3983 	for (; phyi != NULL;
3984 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3985 	    phyi, AVL_AFTER)) {
3986 		/*
3987 		 * If we're not returning the first interface in the tree
3988 		 * and we still haven't moved past the phyint_t that
3989 		 * corresponds to index, avl_walk needs to be called again
3990 		 */
3991 		if (!((index != 0) && (phyi == phyi_initial))) {
3992 			if (isv6) {
3993 				if ((phyi->phyint_illv6) &&
3994 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3995 				    (phyi->phyint_illv6->ill_isv6 == 1))
3996 					break;
3997 			} else {
3998 				if ((phyi->phyint_illv4) &&
3999 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
4000 				    (phyi->phyint_illv4->ill_isv6 == 0))
4001 					break;
4002 			}
4003 		}
4004 	}
4005 
4006 	rw_exit(&ipst->ips_ill_g_lock);
4007 
4008 	if (phyi != NULL)
4009 		ifindex = phyi->phyint_ifindex;
4010 	else
4011 		ifindex = 0;
4012 
4013 	return (ifindex);
4014 }
4015 
4016 /*
4017  * Return the ifindex for the named interface.
4018  * If there is no next ifindex for the interface, return 0.
4019  */
4020 uint_t
4021 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
4022 {
4023 	phyint_t	*phyi;
4024 	avl_index_t	where = 0;
4025 	uint_t		ifindex;
4026 
4027 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4028 
4029 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4030 	    name, &where)) == NULL) {
4031 		rw_exit(&ipst->ips_ill_g_lock);
4032 		return (0);
4033 	}
4034 
4035 	ifindex = phyi->phyint_ifindex;
4036 
4037 	rw_exit(&ipst->ips_ill_g_lock);
4038 
4039 	return (ifindex);
4040 }
4041 
4042 /*
4043  * Return the ifindex to be used by upper layer protocols for instance
4044  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4045  */
4046 uint_t
4047 ill_get_upper_ifindex(const ill_t *ill)
4048 {
4049 	if (IS_UNDER_IPMP(ill))
4050 		return (ipmp_ill_get_ipmp_ifindex(ill));
4051 	else
4052 		return (ill->ill_phyint->phyint_ifindex);
4053 }
4054 
4055 
4056 /*
4057  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4058  * that gives a running thread a reference to the ill. This reference must be
4059  * released by the thread when it is done accessing the ill and related
4060  * objects. ill_refcnt can not be used to account for static references
4061  * such as other structures pointing to an ill. Callers must generally
4062  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4063  * or be sure that the ill is not being deleted or changing state before
4064  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4065  * ill won't change any of its critical state such as address, netmask etc.
4066  */
4067 void
4068 ill_refhold(ill_t *ill)
4069 {
4070 	mutex_enter(&ill->ill_lock);
4071 	ill->ill_refcnt++;
4072 	ILL_TRACE_REF(ill);
4073 	mutex_exit(&ill->ill_lock);
4074 }
4075 
4076 void
4077 ill_refhold_locked(ill_t *ill)
4078 {
4079 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4080 	ill->ill_refcnt++;
4081 	ILL_TRACE_REF(ill);
4082 }
4083 
4084 /* Returns true if we managed to get a refhold */
4085 boolean_t
4086 ill_check_and_refhold(ill_t *ill)
4087 {
4088 	mutex_enter(&ill->ill_lock);
4089 	if (!ILL_IS_CONDEMNED(ill)) {
4090 		ill_refhold_locked(ill);
4091 		mutex_exit(&ill->ill_lock);
4092 		return (B_TRUE);
4093 	}
4094 	mutex_exit(&ill->ill_lock);
4095 	return (B_FALSE);
4096 }
4097 
4098 /*
4099  * Must not be called while holding any locks. Otherwise if this is
4100  * the last reference to be released, there is a chance of recursive mutex
4101  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4102  * to restart an ioctl.
4103  */
4104 void
4105 ill_refrele(ill_t *ill)
4106 {
4107 	mutex_enter(&ill->ill_lock);
4108 	ASSERT(ill->ill_refcnt != 0);
4109 	ill->ill_refcnt--;
4110 	ILL_UNTRACE_REF(ill);
4111 	if (ill->ill_refcnt != 0) {
4112 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4113 		mutex_exit(&ill->ill_lock);
4114 		return;
4115 	}
4116 
4117 	/* Drops the ill_lock */
4118 	ipif_ill_refrele_tail(ill);
4119 }
4120 
4121 /*
4122  * Obtain a weak reference count on the ill. This reference ensures the
4123  * ill won't be freed, but the ill may change any of its critical state
4124  * such as netmask, address etc. Returns an error if the ill has started
4125  * closing.
4126  */
4127 boolean_t
4128 ill_waiter_inc(ill_t *ill)
4129 {
4130 	mutex_enter(&ill->ill_lock);
4131 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4132 		mutex_exit(&ill->ill_lock);
4133 		return (B_FALSE);
4134 	}
4135 	ill->ill_waiters++;
4136 	mutex_exit(&ill->ill_lock);
4137 	return (B_TRUE);
4138 }
4139 
4140 void
4141 ill_waiter_dcr(ill_t *ill)
4142 {
4143 	mutex_enter(&ill->ill_lock);
4144 	ill->ill_waiters--;
4145 	if (ill->ill_waiters == 0)
4146 		cv_broadcast(&ill->ill_cv);
4147 	mutex_exit(&ill->ill_lock);
4148 }
4149 
4150 /*
4151  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4152  * driver.  We construct best guess defaults for lower level information that
4153  * we need.  If an interface is brought up without injection of any overriding
4154  * information from outside, we have to be ready to go with these defaults.
4155  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4156  * we primarely want the dl_provider_style.
4157  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4158  * at which point we assume the other part of the information is valid.
4159  */
4160 void
4161 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4162 {
4163 	uchar_t		*brdcst_addr;
4164 	uint_t		brdcst_addr_length, phys_addr_length;
4165 	t_scalar_t	sap_length;
4166 	dl_info_ack_t	*dlia;
4167 	ip_m_t		*ipm;
4168 	dl_qos_cl_sel1_t *sel1;
4169 	int		min_mtu;
4170 
4171 	ASSERT(IAM_WRITER_ILL(ill));
4172 
4173 	/*
4174 	 * Till the ill is fully up  the ill is not globally visible.
4175 	 * So no need for a lock.
4176 	 */
4177 	dlia = (dl_info_ack_t *)mp->b_rptr;
4178 	ill->ill_mactype = dlia->dl_mac_type;
4179 
4180 	ipm = ip_m_lookup(dlia->dl_mac_type);
4181 	if (ipm == NULL) {
4182 		ipm = ip_m_lookup(DL_OTHER);
4183 		ASSERT(ipm != NULL);
4184 	}
4185 	ill->ill_media = ipm;
4186 
4187 	/*
4188 	 * When the new DLPI stuff is ready we'll pull lengths
4189 	 * from dlia.
4190 	 */
4191 	if (dlia->dl_version == DL_VERSION_2) {
4192 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4193 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4194 		    brdcst_addr_length);
4195 		if (brdcst_addr == NULL) {
4196 			brdcst_addr_length = 0;
4197 		}
4198 		sap_length = dlia->dl_sap_length;
4199 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4200 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4201 		    brdcst_addr_length, sap_length, phys_addr_length));
4202 	} else {
4203 		brdcst_addr_length = 6;
4204 		brdcst_addr = ip_six_byte_all_ones;
4205 		sap_length = -2;
4206 		phys_addr_length = brdcst_addr_length;
4207 	}
4208 
4209 	ill->ill_bcast_addr_length = brdcst_addr_length;
4210 	ill->ill_phys_addr_length = phys_addr_length;
4211 	ill->ill_sap_length = sap_length;
4212 
4213 	/*
4214 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4215 	 * but we must ensure a minimum IP MTU is used since other bits of
4216 	 * IP will fly apart otherwise.
4217 	 */
4218 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4219 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4220 	ill->ill_current_frag = ill->ill_max_frag;
4221 	ill->ill_mtu = ill->ill_max_frag;
4222 
4223 	ill->ill_type = ipm->ip_m_type;
4224 
4225 	if (!ill->ill_dlpi_style_set) {
4226 		if (dlia->dl_provider_style == DL_STYLE2)
4227 			ill->ill_needs_attach = 1;
4228 
4229 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4230 
4231 		/*
4232 		 * Allocate the first ipif on this ill.  We don't delay it
4233 		 * further as ioctl handling assumes at least one ipif exists.
4234 		 *
4235 		 * At this point we don't know whether the ill is v4 or v6.
4236 		 * We will know this whan the SIOCSLIFNAME happens and
4237 		 * the correct value for ill_isv6 will be assigned in
4238 		 * ipif_set_values(). We need to hold the ill lock and
4239 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4240 		 * the wakeup.
4241 		 */
4242 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4243 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4244 		mutex_enter(&ill->ill_lock);
4245 		ASSERT(ill->ill_dlpi_style_set == 0);
4246 		ill->ill_dlpi_style_set = 1;
4247 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4248 		cv_broadcast(&ill->ill_cv);
4249 		mutex_exit(&ill->ill_lock);
4250 		freemsg(mp);
4251 		return;
4252 	}
4253 	ASSERT(ill->ill_ipif != NULL);
4254 	/*
4255 	 * We know whether it is IPv4 or IPv6 now, as this is the
4256 	 * second DL_INFO_ACK we are recieving in response to the
4257 	 * DL_INFO_REQ sent in ipif_set_values.
4258 	 */
4259 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4260 	/*
4261 	 * Clear all the flags that were set based on ill_bcast_addr_length
4262 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4263 	 * changed now and we need to re-evaluate.
4264 	 */
4265 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4266 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4267 
4268 	/*
4269 	 * Free ill_bcast_mp as things could have changed now.
4270 	 *
4271 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4272 	 * with no underlying interfaces (and thus an unknown broadcast
4273 	 * address length), but we enforce that an interface is broadcast-
4274 	 * capable as part of allowing it to join a group.
4275 	 */
4276 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4277 		if (ill->ill_bcast_mp != NULL)
4278 			freemsg(ill->ill_bcast_mp);
4279 		ill->ill_net_type = IRE_IF_NORESOLVER;
4280 
4281 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4282 		    ill->ill_phys_addr_length,
4283 		    ill->ill_sap,
4284 		    ill->ill_sap_length);
4285 
4286 		if (ill->ill_isv6)
4287 			/*
4288 			 * Note: xresolv interfaces will eventually need NOARP
4289 			 * set here as well, but that will require those
4290 			 * external resolvers to have some knowledge of
4291 			 * that flag and act appropriately. Not to be changed
4292 			 * at present.
4293 			 */
4294 			ill->ill_flags |= ILLF_NONUD;
4295 		else
4296 			ill->ill_flags |= ILLF_NOARP;
4297 
4298 		if (ill->ill_mactype == SUNW_DL_VNI) {
4299 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4300 		} else if (ill->ill_phys_addr_length == 0 ||
4301 		    ill->ill_mactype == DL_IPV4 ||
4302 		    ill->ill_mactype == DL_IPV6) {
4303 			/*
4304 			 * The underying link is point-to-point, so mark the
4305 			 * interface as such.  We can do IP multicast over
4306 			 * such a link since it transmits all network-layer
4307 			 * packets to the remote side the same way.
4308 			 */
4309 			ill->ill_flags |= ILLF_MULTICAST;
4310 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4311 		}
4312 	} else {
4313 		ill->ill_net_type = IRE_IF_RESOLVER;
4314 		if (ill->ill_bcast_mp != NULL)
4315 			freemsg(ill->ill_bcast_mp);
4316 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4317 		    ill->ill_bcast_addr_length, ill->ill_sap,
4318 		    ill->ill_sap_length);
4319 		/*
4320 		 * Later detect lack of DLPI driver multicast
4321 		 * capability by catching DL_ENABMULTI errors in
4322 		 * ip_rput_dlpi.
4323 		 */
4324 		ill->ill_flags |= ILLF_MULTICAST;
4325 		if (!ill->ill_isv6)
4326 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4327 	}
4328 
4329 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4330 	if (ill->ill_mactype == SUNW_DL_IPMP)
4331 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4332 
4333 	/* By default an interface does not support any CoS marking */
4334 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4335 
4336 	/*
4337 	 * If we get QoS information in DL_INFO_ACK, the device supports
4338 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4339 	 */
4340 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4341 	    dlia->dl_qos_length);
4342 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4343 		ill->ill_flags |= ILLF_COS_ENABLED;
4344 	}
4345 
4346 	/* Clear any previous error indication. */
4347 	ill->ill_error = 0;
4348 	freemsg(mp);
4349 }
4350 
4351 /*
4352  * Perform various checks to verify that an address would make sense as a
4353  * local, remote, or subnet interface address.
4354  */
4355 static boolean_t
4356 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4357 {
4358 	ipaddr_t	net_mask;
4359 
4360 	/*
4361 	 * Don't allow all zeroes, or all ones, but allow
4362 	 * all ones netmask.
4363 	 */
4364 	if ((net_mask = ip_net_mask(addr)) == 0)
4365 		return (B_FALSE);
4366 	/* A given netmask overrides the "guess" netmask */
4367 	if (subnet_mask != 0)
4368 		net_mask = subnet_mask;
4369 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4370 	    (addr == (addr | ~net_mask)))) {
4371 		return (B_FALSE);
4372 	}
4373 
4374 	/*
4375 	 * Even if the netmask is all ones, we do not allow address to be
4376 	 * 255.255.255.255
4377 	 */
4378 	if (addr == INADDR_BROADCAST)
4379 		return (B_FALSE);
4380 
4381 	if (CLASSD(addr))
4382 		return (B_FALSE);
4383 
4384 	return (B_TRUE);
4385 }
4386 
4387 #define	V6_IPIF_LINKLOCAL(p)	\
4388 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4389 
4390 /*
4391  * Compare two given ipifs and check if the second one is better than
4392  * the first one using the order of preference (not taking deprecated
4393  * into acount) specified in ipif_lookup_multicast().
4394  */
4395 static boolean_t
4396 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4397 {
4398 	/* Check the least preferred first. */
4399 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4400 		/* If both ipifs are the same, use the first one. */
4401 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4402 			return (B_FALSE);
4403 		else
4404 			return (B_TRUE);
4405 	}
4406 
4407 	/* For IPv6, check for link local address. */
4408 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4409 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4410 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4411 			/* The second one is equal or less preferred. */
4412 			return (B_FALSE);
4413 		} else {
4414 			return (B_TRUE);
4415 		}
4416 	}
4417 
4418 	/* Then check for point to point interface. */
4419 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4420 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4421 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4422 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4423 			return (B_FALSE);
4424 		} else {
4425 			return (B_TRUE);
4426 		}
4427 	}
4428 
4429 	/* old_ipif is a normal interface, so no need to use the new one. */
4430 	return (B_FALSE);
4431 }
4432 
4433 /*
4434  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4435  * The ipif must be up, and its ill must multicast-capable, not
4436  * condemned, not an underlying interface in an IPMP group, and
4437  * not a VNI interface.  Order of preference:
4438  *
4439  * 	1a. normal
4440  * 	1b. normal, but deprecated
4441  * 	2a. point to point
4442  * 	2b. point to point, but deprecated
4443  * 	3a. link local
4444  * 	3b. link local, but deprecated
4445  * 	4. loopback.
4446  */
4447 static ipif_t *
4448 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4449 {
4450 	ill_t			*ill;
4451 	ill_walk_context_t	ctx;
4452 	ipif_t			*ipif;
4453 	ipif_t			*saved_ipif = NULL;
4454 	ipif_t			*dep_ipif = NULL;
4455 
4456 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4457 	if (isv6)
4458 		ill = ILL_START_WALK_V6(&ctx, ipst);
4459 	else
4460 		ill = ILL_START_WALK_V4(&ctx, ipst);
4461 
4462 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4463 		mutex_enter(&ill->ill_lock);
4464 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4465 		    ILL_IS_CONDEMNED(ill) ||
4466 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4467 			mutex_exit(&ill->ill_lock);
4468 			continue;
4469 		}
4470 		for (ipif = ill->ill_ipif; ipif != NULL;
4471 		    ipif = ipif->ipif_next) {
4472 			if (zoneid != ipif->ipif_zoneid &&
4473 			    zoneid != ALL_ZONES &&
4474 			    ipif->ipif_zoneid != ALL_ZONES) {
4475 				continue;
4476 			}
4477 			if (!(ipif->ipif_flags & IPIF_UP) ||
4478 			    IPIF_IS_CONDEMNED(ipif)) {
4479 				continue;
4480 			}
4481 
4482 			/*
4483 			 * Found one candidate.  If it is deprecated,
4484 			 * remember it in dep_ipif.  If it is not deprecated,
4485 			 * remember it in saved_ipif.
4486 			 */
4487 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4488 				if (dep_ipif == NULL) {
4489 					dep_ipif = ipif;
4490 				} else if (ipif_comp_multi(dep_ipif, ipif,
4491 				    isv6)) {
4492 					/*
4493 					 * If the previous dep_ipif does not
4494 					 * belong to the same ill, we've done
4495 					 * a ipif_refhold() on it.  So we need
4496 					 * to release it.
4497 					 */
4498 					if (dep_ipif->ipif_ill != ill)
4499 						ipif_refrele(dep_ipif);
4500 					dep_ipif = ipif;
4501 				}
4502 				continue;
4503 			}
4504 			if (saved_ipif == NULL) {
4505 				saved_ipif = ipif;
4506 			} else {
4507 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4508 					if (saved_ipif->ipif_ill != ill)
4509 						ipif_refrele(saved_ipif);
4510 					saved_ipif = ipif;
4511 				}
4512 			}
4513 		}
4514 		/*
4515 		 * Before going to the next ill, do a ipif_refhold() on the
4516 		 * saved ones.
4517 		 */
4518 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4519 			ipif_refhold_locked(saved_ipif);
4520 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4521 			ipif_refhold_locked(dep_ipif);
4522 		mutex_exit(&ill->ill_lock);
4523 	}
4524 	rw_exit(&ipst->ips_ill_g_lock);
4525 
4526 	/*
4527 	 * If we have only the saved_ipif, return it.  But if we have both
4528 	 * saved_ipif and dep_ipif, check to see which one is better.
4529 	 */
4530 	if (saved_ipif != NULL) {
4531 		if (dep_ipif != NULL) {
4532 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4533 				ipif_refrele(saved_ipif);
4534 				return (dep_ipif);
4535 			} else {
4536 				ipif_refrele(dep_ipif);
4537 				return (saved_ipif);
4538 			}
4539 		}
4540 		return (saved_ipif);
4541 	} else {
4542 		return (dep_ipif);
4543 	}
4544 }
4545 
4546 ill_t *
4547 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4548 {
4549 	ipif_t *ipif;
4550 	ill_t *ill;
4551 
4552 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4553 	if (ipif == NULL)
4554 		return (NULL);
4555 
4556 	ill = ipif->ipif_ill;
4557 	ill_refhold(ill);
4558 	ipif_refrele(ipif);
4559 	return (ill);
4560 }
4561 
4562 /*
4563  * This function is called when an application does not specify an interface
4564  * to be used for multicast traffic (joining a group/sending data).  It
4565  * calls ire_lookup_multi() to look for an interface route for the
4566  * specified multicast group.  Doing this allows the administrator to add
4567  * prefix routes for multicast to indicate which interface to be used for
4568  * multicast traffic in the above scenario.  The route could be for all
4569  * multicast (224.0/4), for a single multicast group (a /32 route) or
4570  * anything in between.  If there is no such multicast route, we just find
4571  * any multicast capable interface and return it.  The returned ipif
4572  * is refhold'ed.
4573  *
4574  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4575  * unicast table. This is used by CGTP.
4576  */
4577 ill_t *
4578 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4579     boolean_t *multirtp, ipaddr_t *setsrcp)
4580 {
4581 	ill_t			*ill;
4582 
4583 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4584 	if (ill != NULL)
4585 		return (ill);
4586 
4587 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4588 }
4589 
4590 /*
4591  * Look for an ipif with the specified interface address and destination.
4592  * The destination address is used only for matching point-to-point interfaces.
4593  */
4594 ipif_t *
4595 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4596 {
4597 	ipif_t	*ipif;
4598 	ill_t	*ill;
4599 	ill_walk_context_t ctx;
4600 
4601 	/*
4602 	 * First match all the point-to-point interfaces
4603 	 * before looking at non-point-to-point interfaces.
4604 	 * This is done to avoid returning non-point-to-point
4605 	 * ipif instead of unnumbered point-to-point ipif.
4606 	 */
4607 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4608 	ill = ILL_START_WALK_V4(&ctx, ipst);
4609 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4610 		mutex_enter(&ill->ill_lock);
4611 		for (ipif = ill->ill_ipif; ipif != NULL;
4612 		    ipif = ipif->ipif_next) {
4613 			/* Allow the ipif to be down */
4614 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4615 			    (ipif->ipif_lcl_addr == if_addr) &&
4616 			    (ipif->ipif_pp_dst_addr == dst)) {
4617 				if (!IPIF_IS_CONDEMNED(ipif)) {
4618 					ipif_refhold_locked(ipif);
4619 					mutex_exit(&ill->ill_lock);
4620 					rw_exit(&ipst->ips_ill_g_lock);
4621 					return (ipif);
4622 				}
4623 			}
4624 		}
4625 		mutex_exit(&ill->ill_lock);
4626 	}
4627 	rw_exit(&ipst->ips_ill_g_lock);
4628 
4629 	/* lookup the ipif based on interface address */
4630 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4631 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4632 	return (ipif);
4633 }
4634 
4635 /*
4636  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4637  */
4638 static ipif_t *
4639 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4640     zoneid_t zoneid, ip_stack_t *ipst)
4641 {
4642 	ipif_t  *ipif;
4643 	ill_t   *ill;
4644 	boolean_t ptp = B_FALSE;
4645 	ill_walk_context_t	ctx;
4646 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4647 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4648 
4649 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4650 	/*
4651 	 * Repeat twice, first based on local addresses and
4652 	 * next time for pointopoint.
4653 	 */
4654 repeat:
4655 	ill = ILL_START_WALK_V4(&ctx, ipst);
4656 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4657 		if (match_ill != NULL && ill != match_ill &&
4658 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4659 			continue;
4660 		}
4661 		mutex_enter(&ill->ill_lock);
4662 		for (ipif = ill->ill_ipif; ipif != NULL;
4663 		    ipif = ipif->ipif_next) {
4664 			if (zoneid != ALL_ZONES &&
4665 			    zoneid != ipif->ipif_zoneid &&
4666 			    ipif->ipif_zoneid != ALL_ZONES)
4667 				continue;
4668 
4669 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4670 				continue;
4671 
4672 			/* Allow the ipif to be down */
4673 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4674 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4675 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4676 			    (ipif->ipif_pp_dst_addr == addr))) {
4677 				if (!IPIF_IS_CONDEMNED(ipif)) {
4678 					ipif_refhold_locked(ipif);
4679 					mutex_exit(&ill->ill_lock);
4680 					rw_exit(&ipst->ips_ill_g_lock);
4681 					return (ipif);
4682 				}
4683 			}
4684 		}
4685 		mutex_exit(&ill->ill_lock);
4686 	}
4687 
4688 	/* If we already did the ptp case, then we are done */
4689 	if (ptp) {
4690 		rw_exit(&ipst->ips_ill_g_lock);
4691 		return (NULL);
4692 	}
4693 	ptp = B_TRUE;
4694 	goto repeat;
4695 }
4696 
4697 /*
4698  * Lookup an ipif with the specified address.  For point-to-point links we
4699  * look for matches on either the destination address or the local address,
4700  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4701  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4702  * (or illgrp if `match_ill' is in an IPMP group).
4703  */
4704 ipif_t *
4705 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4706     ip_stack_t *ipst)
4707 {
4708 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4709 	    zoneid, ipst));
4710 }
4711 
4712 /*
4713  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4714  * except that we will only return an address if it is not marked as
4715  * IPIF_DUPLICATE
4716  */
4717 ipif_t *
4718 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4719     ip_stack_t *ipst)
4720 {
4721 	return (ipif_lookup_addr_common(addr, match_ill,
4722 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4723 	    zoneid, ipst));
4724 }
4725 
4726 /*
4727  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4728  * `match_ill' across the IPMP group.  This function is only needed in some
4729  * corner-cases; almost everything should use ipif_lookup_addr().
4730  */
4731 ipif_t *
4732 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4733 {
4734 	ASSERT(match_ill != NULL);
4735 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4736 	    ipst));
4737 }
4738 
4739 /*
4740  * Look for an ipif with the specified address. For point-point links
4741  * we look for matches on either the destination address and the local
4742  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4743  * is set.
4744  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4745  * ill (or illgrp if `match_ill' is in an IPMP group).
4746  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4747  */
4748 zoneid_t
4749 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4750 {
4751 	zoneid_t zoneid;
4752 	ipif_t  *ipif;
4753 	ill_t   *ill;
4754 	boolean_t ptp = B_FALSE;
4755 	ill_walk_context_t	ctx;
4756 
4757 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4758 	/*
4759 	 * Repeat twice, first based on local addresses and
4760 	 * next time for pointopoint.
4761 	 */
4762 repeat:
4763 	ill = ILL_START_WALK_V4(&ctx, ipst);
4764 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4765 		if (match_ill != NULL && ill != match_ill &&
4766 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4767 			continue;
4768 		}
4769 		mutex_enter(&ill->ill_lock);
4770 		for (ipif = ill->ill_ipif; ipif != NULL;
4771 		    ipif = ipif->ipif_next) {
4772 			/* Allow the ipif to be down */
4773 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4774 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4775 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4776 			    (ipif->ipif_pp_dst_addr == addr)) &&
4777 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4778 				zoneid = ipif->ipif_zoneid;
4779 				mutex_exit(&ill->ill_lock);
4780 				rw_exit(&ipst->ips_ill_g_lock);
4781 				/*
4782 				 * If ipif_zoneid was ALL_ZONES then we have
4783 				 * a trusted extensions shared IP address.
4784 				 * In that case GLOBAL_ZONEID works to send.
4785 				 */
4786 				if (zoneid == ALL_ZONES)
4787 					zoneid = GLOBAL_ZONEID;
4788 				return (zoneid);
4789 			}
4790 		}
4791 		mutex_exit(&ill->ill_lock);
4792 	}
4793 
4794 	/* If we already did the ptp case, then we are done */
4795 	if (ptp) {
4796 		rw_exit(&ipst->ips_ill_g_lock);
4797 		return (ALL_ZONES);
4798 	}
4799 	ptp = B_TRUE;
4800 	goto repeat;
4801 }
4802 
4803 /*
4804  * Look for an ipif that matches the specified remote address i.e. the
4805  * ipif that would receive the specified packet.
4806  * First look for directly connected interfaces and then do a recursive
4807  * IRE lookup and pick the first ipif corresponding to the source address in the
4808  * ire.
4809  * Returns: held ipif
4810  *
4811  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4812  */
4813 ipif_t *
4814 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4815 {
4816 	ipif_t	*ipif;
4817 
4818 	ASSERT(!ill->ill_isv6);
4819 
4820 	/*
4821 	 * Someone could be changing this ipif currently or change it
4822 	 * after we return this. Thus  a few packets could use the old
4823 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4824 	 * will atomically be updated or cleaned up with the new value
4825 	 * Thus we don't need a lock to check the flags or other attrs below.
4826 	 */
4827 	mutex_enter(&ill->ill_lock);
4828 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4829 		if (IPIF_IS_CONDEMNED(ipif))
4830 			continue;
4831 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4832 		    ipif->ipif_zoneid != ALL_ZONES)
4833 			continue;
4834 		/* Allow the ipif to be down */
4835 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4836 			if ((ipif->ipif_pp_dst_addr == addr) ||
4837 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4838 			    ipif->ipif_lcl_addr == addr)) {
4839 				ipif_refhold_locked(ipif);
4840 				mutex_exit(&ill->ill_lock);
4841 				return (ipif);
4842 			}
4843 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4844 			ipif_refhold_locked(ipif);
4845 			mutex_exit(&ill->ill_lock);
4846 			return (ipif);
4847 		}
4848 	}
4849 	mutex_exit(&ill->ill_lock);
4850 	/*
4851 	 * For a remote destination it isn't possible to nail down a particular
4852 	 * ipif.
4853 	 */
4854 
4855 	/* Pick the first interface */
4856 	ipif = ipif_get_next_ipif(NULL, ill);
4857 	return (ipif);
4858 }
4859 
4860 /*
4861  * This func does not prevent refcnt from increasing. But if
4862  * the caller has taken steps to that effect, then this func
4863  * can be used to determine whether the ill has become quiescent
4864  */
4865 static boolean_t
4866 ill_is_quiescent(ill_t *ill)
4867 {
4868 	ipif_t	*ipif;
4869 
4870 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4871 
4872 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4873 		if (ipif->ipif_refcnt != 0)
4874 			return (B_FALSE);
4875 	}
4876 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4877 		return (B_FALSE);
4878 	}
4879 	return (B_TRUE);
4880 }
4881 
4882 boolean_t
4883 ill_is_freeable(ill_t *ill)
4884 {
4885 	ipif_t	*ipif;
4886 
4887 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4888 
4889 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4890 		if (ipif->ipif_refcnt != 0) {
4891 			return (B_FALSE);
4892 		}
4893 	}
4894 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4895 		return (B_FALSE);
4896 	}
4897 	return (B_TRUE);
4898 }
4899 
4900 /*
4901  * This func does not prevent refcnt from increasing. But if
4902  * the caller has taken steps to that effect, then this func
4903  * can be used to determine whether the ipif has become quiescent
4904  */
4905 static boolean_t
4906 ipif_is_quiescent(ipif_t *ipif)
4907 {
4908 	ill_t *ill;
4909 
4910 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4911 
4912 	if (ipif->ipif_refcnt != 0)
4913 		return (B_FALSE);
4914 
4915 	ill = ipif->ipif_ill;
4916 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4917 	    ill->ill_logical_down) {
4918 		return (B_TRUE);
4919 	}
4920 
4921 	/* This is the last ipif going down or being deleted on this ill */
4922 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4923 		return (B_FALSE);
4924 	}
4925 
4926 	return (B_TRUE);
4927 }
4928 
4929 /*
4930  * return true if the ipif can be destroyed: the ipif has to be quiescent
4931  * with zero references from ire/ilm to it.
4932  */
4933 static boolean_t
4934 ipif_is_freeable(ipif_t *ipif)
4935 {
4936 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4937 	ASSERT(ipif->ipif_id != 0);
4938 	return (ipif->ipif_refcnt == 0);
4939 }
4940 
4941 /*
4942  * The ipif/ill/ire has been refreled. Do the tail processing.
4943  * Determine if the ipif or ill in question has become quiescent and if so
4944  * wakeup close and/or restart any queued pending ioctl that is waiting
4945  * for the ipif_down (or ill_down)
4946  */
4947 void
4948 ipif_ill_refrele_tail(ill_t *ill)
4949 {
4950 	mblk_t	*mp;
4951 	conn_t	*connp;
4952 	ipsq_t	*ipsq;
4953 	ipxop_t	*ipx;
4954 	ipif_t	*ipif;
4955 	dl_notify_ind_t *dlindp;
4956 
4957 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4958 
4959 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4960 		/* ip_modclose() may be waiting */
4961 		cv_broadcast(&ill->ill_cv);
4962 	}
4963 
4964 	ipsq = ill->ill_phyint->phyint_ipsq;
4965 	mutex_enter(&ipsq->ipsq_lock);
4966 	ipx = ipsq->ipsq_xop;
4967 	mutex_enter(&ipx->ipx_lock);
4968 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4969 		goto unlock;
4970 
4971 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4972 
4973 	ipif = ipx->ipx_pending_ipif;
4974 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4975 		goto unlock;
4976 
4977 	switch (ipx->ipx_waitfor) {
4978 	case IPIF_DOWN:
4979 		if (!ipif_is_quiescent(ipif))
4980 			goto unlock;
4981 		break;
4982 	case IPIF_FREE:
4983 		if (!ipif_is_freeable(ipif))
4984 			goto unlock;
4985 		break;
4986 	case ILL_DOWN:
4987 		if (!ill_is_quiescent(ill))
4988 			goto unlock;
4989 		break;
4990 	case ILL_FREE:
4991 		/*
4992 		 * ILL_FREE is only for loopback; normal ill teardown waits
4993 		 * synchronously in ip_modclose() without using ipx_waitfor,
4994 		 * handled by the cv_broadcast() at the top of this function.
4995 		 */
4996 		if (!ill_is_freeable(ill))
4997 			goto unlock;
4998 		break;
4999 	default:
5000 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
5001 		    (void *)ipsq, ipx->ipx_waitfor);
5002 	}
5003 
5004 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
5005 	mutex_exit(&ipx->ipx_lock);
5006 	mp = ipsq_pending_mp_get(ipsq, &connp);
5007 	mutex_exit(&ipsq->ipsq_lock);
5008 	mutex_exit(&ill->ill_lock);
5009 
5010 	ASSERT(mp != NULL);
5011 	/*
5012 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
5013 	 * we can only get here when the current operation decides it
5014 	 * it needs to quiesce via ipsq_pending_mp_add().
5015 	 */
5016 	switch (mp->b_datap->db_type) {
5017 	case M_PCPROTO:
5018 	case M_PROTO:
5019 		/*
5020 		 * For now, only DL_NOTIFY_IND messages can use this facility.
5021 		 */
5022 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
5023 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
5024 
5025 		switch (dlindp->dl_notification) {
5026 		case DL_NOTE_PHYS_ADDR:
5027 			qwriter_ip(ill, ill->ill_rq, mp,
5028 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5029 			return;
5030 		case DL_NOTE_REPLUMB:
5031 			qwriter_ip(ill, ill->ill_rq, mp,
5032 			    ill_replumb_tail, CUR_OP, B_TRUE);
5033 			return;
5034 		default:
5035 			ASSERT(0);
5036 			ill_refrele(ill);
5037 		}
5038 		break;
5039 
5040 	case M_ERROR:
5041 	case M_HANGUP:
5042 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5043 		    B_TRUE);
5044 		return;
5045 
5046 	case M_IOCTL:
5047 	case M_IOCDATA:
5048 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5049 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5050 		return;
5051 
5052 	default:
5053 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5054 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5055 	}
5056 	return;
5057 unlock:
5058 	mutex_exit(&ipsq->ipsq_lock);
5059 	mutex_exit(&ipx->ipx_lock);
5060 	mutex_exit(&ill->ill_lock);
5061 }
5062 
5063 #ifdef DEBUG
5064 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5065 static void
5066 th_trace_rrecord(th_trace_t *th_trace)
5067 {
5068 	tr_buf_t *tr_buf;
5069 	uint_t lastref;
5070 
5071 	lastref = th_trace->th_trace_lastref;
5072 	lastref++;
5073 	if (lastref == TR_BUF_MAX)
5074 		lastref = 0;
5075 	th_trace->th_trace_lastref = lastref;
5076 	tr_buf = &th_trace->th_trbuf[lastref];
5077 	tr_buf->tr_time = ddi_get_lbolt();
5078 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5079 }
5080 
5081 static void
5082 th_trace_free(void *value)
5083 {
5084 	th_trace_t *th_trace = value;
5085 
5086 	ASSERT(th_trace->th_refcnt == 0);
5087 	kmem_free(th_trace, sizeof (*th_trace));
5088 }
5089 
5090 /*
5091  * Find or create the per-thread hash table used to track object references.
5092  * The ipst argument is NULL if we shouldn't allocate.
5093  *
5094  * Accesses per-thread data, so there's no need to lock here.
5095  */
5096 static mod_hash_t *
5097 th_trace_gethash(ip_stack_t *ipst)
5098 {
5099 	th_hash_t *thh;
5100 
5101 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5102 		mod_hash_t *mh;
5103 		char name[256];
5104 		size_t objsize, rshift;
5105 		int retv;
5106 
5107 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5108 			return (NULL);
5109 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5110 		    (void *)curthread);
5111 
5112 		/*
5113 		 * We use mod_hash_create_extended here rather than the more
5114 		 * obvious mod_hash_create_ptrhash because the latter has a
5115 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5116 		 * block.
5117 		 */
5118 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5119 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5120 		rshift = highbit(objsize);
5121 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5122 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5123 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5124 		if (mh == NULL) {
5125 			kmem_free(thh, sizeof (*thh));
5126 			return (NULL);
5127 		}
5128 		thh->thh_hash = mh;
5129 		thh->thh_ipst = ipst;
5130 		/*
5131 		 * We trace ills, ipifs, ires, and nces.  All of these are
5132 		 * per-IP-stack, so the lock on the thread list is as well.
5133 		 */
5134 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5135 		list_insert_tail(&ip_thread_list, thh);
5136 		rw_exit(&ip_thread_rwlock);
5137 		retv = tsd_set(ip_thread_data, thh);
5138 		ASSERT(retv == 0);
5139 	}
5140 	return (thh != NULL ? thh->thh_hash : NULL);
5141 }
5142 
5143 boolean_t
5144 th_trace_ref(const void *obj, ip_stack_t *ipst)
5145 {
5146 	th_trace_t *th_trace;
5147 	mod_hash_t *mh;
5148 	mod_hash_val_t val;
5149 
5150 	if ((mh = th_trace_gethash(ipst)) == NULL)
5151 		return (B_FALSE);
5152 
5153 	/*
5154 	 * Attempt to locate the trace buffer for this obj and thread.
5155 	 * If it does not exist, then allocate a new trace buffer and
5156 	 * insert into the hash.
5157 	 */
5158 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5159 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5160 		if (th_trace == NULL)
5161 			return (B_FALSE);
5162 
5163 		th_trace->th_id = curthread;
5164 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5165 		    (mod_hash_val_t)th_trace) != 0) {
5166 			kmem_free(th_trace, sizeof (th_trace_t));
5167 			return (B_FALSE);
5168 		}
5169 	} else {
5170 		th_trace = (th_trace_t *)val;
5171 	}
5172 
5173 	ASSERT(th_trace->th_refcnt >= 0 &&
5174 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5175 
5176 	th_trace->th_refcnt++;
5177 	th_trace_rrecord(th_trace);
5178 	return (B_TRUE);
5179 }
5180 
5181 /*
5182  * For the purpose of tracing a reference release, we assume that global
5183  * tracing is always on and that the same thread initiated the reference hold
5184  * is releasing.
5185  */
5186 void
5187 th_trace_unref(const void *obj)
5188 {
5189 	int retv;
5190 	mod_hash_t *mh;
5191 	th_trace_t *th_trace;
5192 	mod_hash_val_t val;
5193 
5194 	mh = th_trace_gethash(NULL);
5195 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5196 	ASSERT(retv == 0);
5197 	th_trace = (th_trace_t *)val;
5198 
5199 	ASSERT(th_trace->th_refcnt > 0);
5200 	th_trace->th_refcnt--;
5201 	th_trace_rrecord(th_trace);
5202 }
5203 
5204 /*
5205  * If tracing has been disabled, then we assume that the reference counts are
5206  * now useless, and we clear them out before destroying the entries.
5207  */
5208 void
5209 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5210 {
5211 	th_hash_t	*thh;
5212 	mod_hash_t	*mh;
5213 	mod_hash_val_t	val;
5214 	th_trace_t	*th_trace;
5215 	int		retv;
5216 
5217 	rw_enter(&ip_thread_rwlock, RW_READER);
5218 	for (thh = list_head(&ip_thread_list); thh != NULL;
5219 	    thh = list_next(&ip_thread_list, thh)) {
5220 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5221 		    &val) == 0) {
5222 			th_trace = (th_trace_t *)val;
5223 			if (trace_disable)
5224 				th_trace->th_refcnt = 0;
5225 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5226 			ASSERT(retv == 0);
5227 		}
5228 	}
5229 	rw_exit(&ip_thread_rwlock);
5230 }
5231 
5232 void
5233 ipif_trace_ref(ipif_t *ipif)
5234 {
5235 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5236 
5237 	if (ipif->ipif_trace_disable)
5238 		return;
5239 
5240 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5241 		ipif->ipif_trace_disable = B_TRUE;
5242 		ipif_trace_cleanup(ipif);
5243 	}
5244 }
5245 
5246 void
5247 ipif_untrace_ref(ipif_t *ipif)
5248 {
5249 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5250 
5251 	if (!ipif->ipif_trace_disable)
5252 		th_trace_unref(ipif);
5253 }
5254 
5255 void
5256 ill_trace_ref(ill_t *ill)
5257 {
5258 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5259 
5260 	if (ill->ill_trace_disable)
5261 		return;
5262 
5263 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5264 		ill->ill_trace_disable = B_TRUE;
5265 		ill_trace_cleanup(ill);
5266 	}
5267 }
5268 
5269 void
5270 ill_untrace_ref(ill_t *ill)
5271 {
5272 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5273 
5274 	if (!ill->ill_trace_disable)
5275 		th_trace_unref(ill);
5276 }
5277 
5278 /*
5279  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5280  * failure, ipif_trace_disable is set.
5281  */
5282 static void
5283 ipif_trace_cleanup(const ipif_t *ipif)
5284 {
5285 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5286 }
5287 
5288 /*
5289  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5290  * failure, ill_trace_disable is set.
5291  */
5292 static void
5293 ill_trace_cleanup(const ill_t *ill)
5294 {
5295 	th_trace_cleanup(ill, ill->ill_trace_disable);
5296 }
5297 #endif /* DEBUG */
5298 
5299 void
5300 ipif_refhold_locked(ipif_t *ipif)
5301 {
5302 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5303 	ipif->ipif_refcnt++;
5304 	IPIF_TRACE_REF(ipif);
5305 }
5306 
5307 void
5308 ipif_refhold(ipif_t *ipif)
5309 {
5310 	ill_t	*ill;
5311 
5312 	ill = ipif->ipif_ill;
5313 	mutex_enter(&ill->ill_lock);
5314 	ipif->ipif_refcnt++;
5315 	IPIF_TRACE_REF(ipif);
5316 	mutex_exit(&ill->ill_lock);
5317 }
5318 
5319 /*
5320  * Must not be called while holding any locks. Otherwise if this is
5321  * the last reference to be released there is a chance of recursive mutex
5322  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5323  * to restart an ioctl.
5324  */
5325 void
5326 ipif_refrele(ipif_t *ipif)
5327 {
5328 	ill_t	*ill;
5329 
5330 	ill = ipif->ipif_ill;
5331 
5332 	mutex_enter(&ill->ill_lock);
5333 	ASSERT(ipif->ipif_refcnt != 0);
5334 	ipif->ipif_refcnt--;
5335 	IPIF_UNTRACE_REF(ipif);
5336 	if (ipif->ipif_refcnt != 0) {
5337 		mutex_exit(&ill->ill_lock);
5338 		return;
5339 	}
5340 
5341 	/* Drops the ill_lock */
5342 	ipif_ill_refrele_tail(ill);
5343 }
5344 
5345 ipif_t *
5346 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5347 {
5348 	ipif_t	*ipif;
5349 
5350 	mutex_enter(&ill->ill_lock);
5351 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5352 	    ipif != NULL; ipif = ipif->ipif_next) {
5353 		if (IPIF_IS_CONDEMNED(ipif))
5354 			continue;
5355 		ipif_refhold_locked(ipif);
5356 		mutex_exit(&ill->ill_lock);
5357 		return (ipif);
5358 	}
5359 	mutex_exit(&ill->ill_lock);
5360 	return (NULL);
5361 }
5362 
5363 /*
5364  * TODO: make this table extendible at run time
5365  * Return a pointer to the mac type info for 'mac_type'
5366  */
5367 static ip_m_t *
5368 ip_m_lookup(t_uscalar_t mac_type)
5369 {
5370 	ip_m_t	*ipm;
5371 
5372 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5373 		if (ipm->ip_m_mac_type == mac_type)
5374 			return (ipm);
5375 	return (NULL);
5376 }
5377 
5378 /*
5379  * Make a link layer address from the multicast IP address *addr.
5380  * To form the link layer address, invoke the ip_m_v*mapping function
5381  * associated with the link-layer type.
5382  */
5383 void
5384 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5385 {
5386 	ip_m_t *ipm;
5387 
5388 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5389 		return;
5390 
5391 	ASSERT(addr != NULL);
5392 
5393 	ipm = ip_m_lookup(ill->ill_mactype);
5394 	if (ipm == NULL ||
5395 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5396 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5397 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5398 		    ill->ill_name, ill->ill_mactype));
5399 		return;
5400 	}
5401 	if (ill->ill_isv6)
5402 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5403 	else
5404 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5405 }
5406 
5407 /*
5408  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5409  * ill is passed in to associate it with the correct interface.
5410  * If ire_arg is set, then we return the held IRE in that location.
5411  */
5412 int
5413 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5414     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5415     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5416 {
5417 	ire_t	*ire, *nire;
5418 	ire_t	*gw_ire = NULL;
5419 	ipif_t	*ipif = NULL;
5420 	uint_t	type;
5421 	int	match_flags = MATCH_IRE_TYPE;
5422 	tsol_gc_t *gc = NULL;
5423 	tsol_gcgrp_t *gcgrp = NULL;
5424 	boolean_t gcgrp_xtraref = B_FALSE;
5425 	boolean_t cgtp_broadcast;
5426 
5427 	ip1dbg(("ip_rt_add:"));
5428 
5429 	if (ire_arg != NULL)
5430 		*ire_arg = NULL;
5431 
5432 	/*
5433 	 * If this is the case of RTF_HOST being set, then we set the netmask
5434 	 * to all ones (regardless if one was supplied).
5435 	 */
5436 	if (flags & RTF_HOST)
5437 		mask = IP_HOST_MASK;
5438 
5439 	/*
5440 	 * Prevent routes with a zero gateway from being created (since
5441 	 * interfaces can currently be plumbed and brought up no assigned
5442 	 * address).
5443 	 */
5444 	if (gw_addr == 0)
5445 		return (ENETUNREACH);
5446 	/*
5447 	 * Get the ipif, if any, corresponding to the gw_addr
5448 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5449 	 * we match on the gatway and destination to handle unnumbered pt-pt
5450 	 * interfaces.
5451 	 */
5452 	if (ill != NULL)
5453 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5454 	else
5455 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5456 	if (ipif != NULL) {
5457 		if (IS_VNI(ipif->ipif_ill)) {
5458 			ipif_refrele(ipif);
5459 			return (EINVAL);
5460 		}
5461 	}
5462 
5463 	/*
5464 	 * GateD will attempt to create routes with a loopback interface
5465 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5466 	 * these routes to be added, but create them as interface routes
5467 	 * since the gateway is an interface address.
5468 	 */
5469 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5470 		flags &= ~RTF_GATEWAY;
5471 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5472 		    mask == IP_HOST_MASK) {
5473 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5474 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5475 			    NULL);
5476 			if (ire != NULL) {
5477 				ire_refrele(ire);
5478 				ipif_refrele(ipif);
5479 				return (EEXIST);
5480 			}
5481 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5482 			    "for 0x%x\n", (void *)ipif,
5483 			    ipif->ipif_ire_type,
5484 			    ntohl(ipif->ipif_lcl_addr)));
5485 			ire = ire_create(
5486 			    (uchar_t *)&dst_addr,	/* dest address */
5487 			    (uchar_t *)&mask,		/* mask */
5488 			    NULL,			/* no gateway */
5489 			    ipif->ipif_ire_type,	/* LOOPBACK */
5490 			    ipif->ipif_ill,
5491 			    zoneid,
5492 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5493 			    NULL,
5494 			    ipst);
5495 
5496 			if (ire == NULL) {
5497 				ipif_refrele(ipif);
5498 				return (ENOMEM);
5499 			}
5500 			/* src address assigned by the caller? */
5501 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5502 				ire->ire_setsrc_addr = src_addr;
5503 
5504 			nire = ire_add(ire);
5505 			if (nire == NULL) {
5506 				/*
5507 				 * In the result of failure, ire_add() will have
5508 				 * already deleted the ire in question, so there
5509 				 * is no need to do that here.
5510 				 */
5511 				ipif_refrele(ipif);
5512 				return (ENOMEM);
5513 			}
5514 			/*
5515 			 * Check if it was a duplicate entry. This handles
5516 			 * the case of two racing route adds for the same route
5517 			 */
5518 			if (nire != ire) {
5519 				ASSERT(nire->ire_identical_ref > 1);
5520 				ire_delete(nire);
5521 				ire_refrele(nire);
5522 				ipif_refrele(ipif);
5523 				return (EEXIST);
5524 			}
5525 			ire = nire;
5526 			goto save_ire;
5527 		}
5528 	}
5529 
5530 	/*
5531 	 * The routes for multicast with CGTP are quite special in that
5532 	 * the gateway is the local interface address, yet RTF_GATEWAY
5533 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5534 	 * this undocumented and unusual use of multicast routes.
5535 	 */
5536 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5537 		flags &= ~RTF_GATEWAY;
5538 
5539 	/*
5540 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5541 	 * and the gateway address provided is one of the system's interface
5542 	 * addresses.  By using the routing socket interface and supplying an
5543 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5544 	 * specifying an interface route to be created is available which uses
5545 	 * the interface index that specifies the outgoing interface rather than
5546 	 * the address of an outgoing interface (which may not be able to
5547 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5548 	 * flag, routes can be specified which not only specify the next-hop to
5549 	 * be used when routing to a certain prefix, but also which outgoing
5550 	 * interface should be used.
5551 	 *
5552 	 * Previously, interfaces would have unique addresses assigned to them
5553 	 * and so the address assigned to a particular interface could be used
5554 	 * to identify a particular interface.  One exception to this was the
5555 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5556 	 *
5557 	 * With the advent of IPv6 and its link-local addresses, this
5558 	 * restriction was relaxed and interfaces could share addresses between
5559 	 * themselves.  In fact, typically all of the link-local interfaces on
5560 	 * an IPv6 node or router will have the same link-local address.  In
5561 	 * order to differentiate between these interfaces, the use of an
5562 	 * interface index is necessary and this index can be carried inside a
5563 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5564 	 * of using the interface index, however, is that all of the ipif's that
5565 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5566 	 * cannot be used to differentiate between ipif's (or logical
5567 	 * interfaces) that belong to the same ill (physical interface).
5568 	 *
5569 	 * For example, in the following case involving IPv4 interfaces and
5570 	 * logical interfaces
5571 	 *
5572 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5573 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5574 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5575 	 *
5576 	 * the ipif's corresponding to each of these interface routes can be
5577 	 * uniquely identified by the "gateway" (actually interface address).
5578 	 *
5579 	 * In this case involving multiple IPv6 default routes to a particular
5580 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5581 	 * default route is of interest:
5582 	 *
5583 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5584 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5585 	 */
5586 
5587 	/* RTF_GATEWAY not set */
5588 	if (!(flags & RTF_GATEWAY)) {
5589 		if (sp != NULL) {
5590 			ip2dbg(("ip_rt_add: gateway security attributes "
5591 			    "cannot be set with interface route\n"));
5592 			if (ipif != NULL)
5593 				ipif_refrele(ipif);
5594 			return (EINVAL);
5595 		}
5596 
5597 		/*
5598 		 * Whether or not ill (RTA_IFP) is set, we require that
5599 		 * the gateway is one of our local addresses.
5600 		 */
5601 		if (ipif == NULL)
5602 			return (ENETUNREACH);
5603 
5604 		/*
5605 		 * We use MATCH_IRE_ILL here. If the caller specified an
5606 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5607 		 * we use the ill derived from the gateway address.
5608 		 * We can always match the gateway address since we record it
5609 		 * in ire_gateway_addr.
5610 		 * We don't allow RTA_IFP to specify a different ill than the
5611 		 * one matching the ipif to make sure we can delete the route.
5612 		 */
5613 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5614 		if (ill == NULL) {
5615 			ill = ipif->ipif_ill;
5616 		} else if (ill != ipif->ipif_ill) {
5617 			ipif_refrele(ipif);
5618 			return (EINVAL);
5619 		}
5620 
5621 		/*
5622 		 * We check for an existing entry at this point.
5623 		 *
5624 		 * Since a netmask isn't passed in via the ioctl interface
5625 		 * (SIOCADDRT), we don't check for a matching netmask in that
5626 		 * case.
5627 		 */
5628 		if (!ioctl_msg)
5629 			match_flags |= MATCH_IRE_MASK;
5630 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5631 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5632 		    NULL);
5633 		if (ire != NULL) {
5634 			ire_refrele(ire);
5635 			ipif_refrele(ipif);
5636 			return (EEXIST);
5637 		}
5638 
5639 		/*
5640 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5641 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5642 		 * gateway.
5643 		 */
5644 		ire = ire_create(
5645 		    (uchar_t *)&dst_addr,
5646 		    (uint8_t *)&mask,
5647 		    (uint8_t *)&gw_addr,
5648 		    ill->ill_net_type,
5649 		    ill,
5650 		    zoneid,
5651 		    flags,
5652 		    NULL,
5653 		    ipst);
5654 		if (ire == NULL) {
5655 			ipif_refrele(ipif);
5656 			return (ENOMEM);
5657 		}
5658 
5659 		/*
5660 		 * Some software (for example, GateD and Sun Cluster) attempts
5661 		 * to create (what amount to) IRE_PREFIX routes with the
5662 		 * loopback address as the gateway.  This is primarily done to
5663 		 * set up prefixes with the RTF_REJECT flag set (for example,
5664 		 * when generating aggregate routes.)
5665 		 *
5666 		 * If the IRE type (as defined by ill->ill_net_type) is
5667 		 * IRE_LOOPBACK, then we map the request into a
5668 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5669 		 * these interface routes, by definition, can only be that.
5670 		 *
5671 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5672 		 * routine, but rather using ire_create() directly.
5673 		 *
5674 		 */
5675 		if (ill->ill_net_type == IRE_LOOPBACK) {
5676 			ire->ire_type = IRE_IF_NORESOLVER;
5677 			ire->ire_flags |= RTF_BLACKHOLE;
5678 		}
5679 
5680 		/* src address assigned by the caller? */
5681 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5682 			ire->ire_setsrc_addr = src_addr;
5683 
5684 		nire = ire_add(ire);
5685 		if (nire == NULL) {
5686 			/*
5687 			 * In the result of failure, ire_add() will have
5688 			 * already deleted the ire in question, so there
5689 			 * is no need to do that here.
5690 			 */
5691 			ipif_refrele(ipif);
5692 			return (ENOMEM);
5693 		}
5694 		/*
5695 		 * Check if it was a duplicate entry. This handles
5696 		 * the case of two racing route adds for the same route
5697 		 */
5698 		if (nire != ire) {
5699 			ire_delete(nire);
5700 			ire_refrele(nire);
5701 			ipif_refrele(ipif);
5702 			return (EEXIST);
5703 		}
5704 		ire = nire;
5705 		goto save_ire;
5706 	}
5707 
5708 	/*
5709 	 * Get an interface IRE for the specified gateway.
5710 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5711 	 * gateway, it is currently unreachable and we fail the request
5712 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5713 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5714 	 * If RTA_IFP was specified we look on that particular ill.
5715 	 */
5716 	if (ill != NULL)
5717 		match_flags |= MATCH_IRE_ILL;
5718 
5719 	/* Check whether the gateway is reachable. */
5720 again:
5721 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5722 	if (flags & RTF_INDIRECT)
5723 		type |= IRE_OFFLINK;
5724 
5725 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5726 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5727 	if (gw_ire == NULL) {
5728 		/*
5729 		 * With IPMP, we allow host routes to influence in.mpathd's
5730 		 * target selection.  However, if the test addresses are on
5731 		 * their own network, the above lookup will fail since the
5732 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5733 		 * hidden test IREs to be found and try again.
5734 		 */
5735 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5736 			match_flags |= MATCH_IRE_TESTHIDDEN;
5737 			goto again;
5738 		}
5739 		if (ipif != NULL)
5740 			ipif_refrele(ipif);
5741 		return (ENETUNREACH);
5742 	}
5743 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5744 		ire_refrele(gw_ire);
5745 		if (ipif != NULL)
5746 			ipif_refrele(ipif);
5747 		return (ENETUNREACH);
5748 	}
5749 
5750 	/*
5751 	 * We create one of three types of IREs as a result of this request
5752 	 * based on the netmask.  A netmask of all ones (which is automatically
5753 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5754 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5755 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5756 	 * destination prefix.
5757 	 */
5758 	if (mask == IP_HOST_MASK)
5759 		type = IRE_HOST;
5760 	else if (mask == 0)
5761 		type = IRE_DEFAULT;
5762 	else
5763 		type = IRE_PREFIX;
5764 
5765 	/* check for a duplicate entry */
5766 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5767 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5768 	    0, ipst, NULL);
5769 	if (ire != NULL) {
5770 		if (ipif != NULL)
5771 			ipif_refrele(ipif);
5772 		ire_refrele(gw_ire);
5773 		ire_refrele(ire);
5774 		return (EEXIST);
5775 	}
5776 
5777 	/* Security attribute exists */
5778 	if (sp != NULL) {
5779 		tsol_gcgrp_addr_t ga;
5780 
5781 		/* find or create the gateway credentials group */
5782 		ga.ga_af = AF_INET;
5783 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5784 
5785 		/* we hold reference to it upon success */
5786 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5787 		if (gcgrp == NULL) {
5788 			if (ipif != NULL)
5789 				ipif_refrele(ipif);
5790 			ire_refrele(gw_ire);
5791 			return (ENOMEM);
5792 		}
5793 
5794 		/*
5795 		 * Create and add the security attribute to the group; a
5796 		 * reference to the group is made upon allocating a new
5797 		 * entry successfully.  If it finds an already-existing
5798 		 * entry for the security attribute in the group, it simply
5799 		 * returns it and no new reference is made to the group.
5800 		 */
5801 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5802 		if (gc == NULL) {
5803 			if (ipif != NULL)
5804 				ipif_refrele(ipif);
5805 			/* release reference held by gcgrp_lookup */
5806 			GCGRP_REFRELE(gcgrp);
5807 			ire_refrele(gw_ire);
5808 			return (ENOMEM);
5809 		}
5810 	}
5811 
5812 	/* Create the IRE. */
5813 	ire = ire_create(
5814 	    (uchar_t *)&dst_addr,		/* dest address */
5815 	    (uchar_t *)&mask,			/* mask */
5816 	    (uchar_t *)&gw_addr,		/* gateway address */
5817 	    (ushort_t)type,			/* IRE type */
5818 	    ill,
5819 	    zoneid,
5820 	    flags,
5821 	    gc,					/* security attribute */
5822 	    ipst);
5823 
5824 	/*
5825 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5826 	 * reference to the 'gcgrp'. We can now release the extra reference
5827 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5828 	 */
5829 	if (gcgrp_xtraref)
5830 		GCGRP_REFRELE(gcgrp);
5831 	if (ire == NULL) {
5832 		if (gc != NULL)
5833 			GC_REFRELE(gc);
5834 		if (ipif != NULL)
5835 			ipif_refrele(ipif);
5836 		ire_refrele(gw_ire);
5837 		return (ENOMEM);
5838 	}
5839 
5840 	/* Before we add, check if an extra CGTP broadcast is needed */
5841 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5842 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5843 
5844 	/* src address assigned by the caller? */
5845 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5846 		ire->ire_setsrc_addr = src_addr;
5847 
5848 	/*
5849 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5850 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5851 	 */
5852 
5853 	/* Add the new IRE. */
5854 	nire = ire_add(ire);
5855 	if (nire == NULL) {
5856 		/*
5857 		 * In the result of failure, ire_add() will have
5858 		 * already deleted the ire in question, so there
5859 		 * is no need to do that here.
5860 		 */
5861 		if (ipif != NULL)
5862 			ipif_refrele(ipif);
5863 		ire_refrele(gw_ire);
5864 		return (ENOMEM);
5865 	}
5866 	/*
5867 	 * Check if it was a duplicate entry. This handles
5868 	 * the case of two racing route adds for the same route
5869 	 */
5870 	if (nire != ire) {
5871 		ire_delete(nire);
5872 		ire_refrele(nire);
5873 		if (ipif != NULL)
5874 			ipif_refrele(ipif);
5875 		ire_refrele(gw_ire);
5876 		return (EEXIST);
5877 	}
5878 	ire = nire;
5879 
5880 	if (flags & RTF_MULTIRT) {
5881 		/*
5882 		 * Invoke the CGTP (multirouting) filtering module
5883 		 * to add the dst address in the filtering database.
5884 		 * Replicated inbound packets coming from that address
5885 		 * will be filtered to discard the duplicates.
5886 		 * It is not necessary to call the CGTP filter hook
5887 		 * when the dst address is a broadcast or multicast,
5888 		 * because an IP source address cannot be a broadcast
5889 		 * or a multicast.
5890 		 */
5891 		if (cgtp_broadcast) {
5892 			ip_cgtp_bcast_add(ire, ipst);
5893 			goto save_ire;
5894 		}
5895 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5896 		    !CLASSD(ire->ire_addr)) {
5897 			int res;
5898 			ipif_t *src_ipif;
5899 
5900 			/* Find the source address corresponding to gw_ire */
5901 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5902 			    NULL, zoneid, ipst);
5903 			if (src_ipif != NULL) {
5904 				res = ipst->ips_ip_cgtp_filter_ops->
5905 				    cfo_add_dest_v4(
5906 				    ipst->ips_netstack->netstack_stackid,
5907 				    ire->ire_addr,
5908 				    ire->ire_gateway_addr,
5909 				    ire->ire_setsrc_addr,
5910 				    src_ipif->ipif_lcl_addr);
5911 				ipif_refrele(src_ipif);
5912 			} else {
5913 				res = EADDRNOTAVAIL;
5914 			}
5915 			if (res != 0) {
5916 				if (ipif != NULL)
5917 					ipif_refrele(ipif);
5918 				ire_refrele(gw_ire);
5919 				ire_delete(ire);
5920 				ire_refrele(ire);	/* Held in ire_add */
5921 				return (res);
5922 			}
5923 		}
5924 	}
5925 
5926 save_ire:
5927 	if (gw_ire != NULL) {
5928 		ire_refrele(gw_ire);
5929 		gw_ire = NULL;
5930 	}
5931 	if (ill != NULL) {
5932 		/*
5933 		 * Save enough information so that we can recreate the IRE if
5934 		 * the interface goes down and then up.  The metrics associated
5935 		 * with the route will be saved as well when rts_setmetrics() is
5936 		 * called after the IRE has been created.  In the case where
5937 		 * memory cannot be allocated, none of this information will be
5938 		 * saved.
5939 		 */
5940 		ill_save_ire(ill, ire);
5941 	}
5942 	if (ioctl_msg)
5943 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5944 	if (ire_arg != NULL) {
5945 		/*
5946 		 * Store the ire that was successfully added into where ire_arg
5947 		 * points to so that callers don't have to look it up
5948 		 * themselves (but they are responsible for ire_refrele()ing
5949 		 * the ire when they are finished with it).
5950 		 */
5951 		*ire_arg = ire;
5952 	} else {
5953 		ire_refrele(ire);		/* Held in ire_add */
5954 	}
5955 	if (ipif != NULL)
5956 		ipif_refrele(ipif);
5957 	return (0);
5958 }
5959 
5960 /*
5961  * ip_rt_delete is called to delete an IPv4 route.
5962  * ill is passed in to associate it with the correct interface.
5963  */
5964 /* ARGSUSED4 */
5965 int
5966 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5967     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5968     ip_stack_t *ipst, zoneid_t zoneid)
5969 {
5970 	ire_t	*ire = NULL;
5971 	ipif_t	*ipif;
5972 	uint_t	type;
5973 	uint_t	match_flags = MATCH_IRE_TYPE;
5974 	int	err = 0;
5975 
5976 	ip1dbg(("ip_rt_delete:"));
5977 	/*
5978 	 * If this is the case of RTF_HOST being set, then we set the netmask
5979 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5980 	 */
5981 	if (flags & RTF_HOST) {
5982 		mask = IP_HOST_MASK;
5983 		match_flags |= MATCH_IRE_MASK;
5984 	} else if (rtm_addrs & RTA_NETMASK) {
5985 		match_flags |= MATCH_IRE_MASK;
5986 	}
5987 
5988 	/*
5989 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5990 	 * we check if the gateway address is one of our interfaces first,
5991 	 * and fall back on RTF_GATEWAY routes.
5992 	 *
5993 	 * This makes it possible to delete an original
5994 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5995 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5996 	 * and those can not be deleted here.
5997 	 *
5998 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5999 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6000 	 * otherwise we use the ill derived from the gateway address.
6001 	 * We can always match the gateway address since we record it
6002 	 * in ire_gateway_addr.
6003 	 *
6004 	 * For more detail on specifying routes by gateway address and by
6005 	 * interface index, see the comments in ip_rt_add().
6006 	 */
6007 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6008 	if (ipif != NULL) {
6009 		ill_t	*ill_match;
6010 
6011 		if (ill != NULL)
6012 			ill_match = ill;
6013 		else
6014 			ill_match = ipif->ipif_ill;
6015 
6016 		match_flags |= MATCH_IRE_ILL;
6017 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6018 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
6019 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
6020 			    NULL);
6021 		}
6022 		if (ire == NULL) {
6023 			match_flags |= MATCH_IRE_GW;
6024 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6025 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6026 			    match_flags, 0, ipst, NULL);
6027 		}
6028 		/* Avoid deleting routes created by kernel from an ipif */
6029 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6030 			ire_refrele(ire);
6031 			ire = NULL;
6032 		}
6033 
6034 		/* Restore in case we didn't find a match */
6035 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6036 	}
6037 
6038 	if (ire == NULL) {
6039 		/*
6040 		 * At this point, the gateway address is not one of our own
6041 		 * addresses or a matching interface route was not found.  We
6042 		 * set the IRE type to lookup based on whether
6043 		 * this is a host route, a default route or just a prefix.
6044 		 *
6045 		 * If an ill was passed in, then the lookup is based on an
6046 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6047 		 */
6048 		match_flags |= MATCH_IRE_GW;
6049 		if (ill != NULL)
6050 			match_flags |= MATCH_IRE_ILL;
6051 		if (mask == IP_HOST_MASK)
6052 			type = IRE_HOST;
6053 		else if (mask == 0)
6054 			type = IRE_DEFAULT;
6055 		else
6056 			type = IRE_PREFIX;
6057 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6058 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6059 	}
6060 
6061 	if (ipif != NULL) {
6062 		ipif_refrele(ipif);
6063 		ipif = NULL;
6064 	}
6065 
6066 	if (ire == NULL)
6067 		return (ESRCH);
6068 
6069 	if (ire->ire_flags & RTF_MULTIRT) {
6070 		/*
6071 		 * Invoke the CGTP (multirouting) filtering module
6072 		 * to remove the dst address from the filtering database.
6073 		 * Packets coming from that address will no longer be
6074 		 * filtered to remove duplicates.
6075 		 */
6076 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6077 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6078 			    ipst->ips_netstack->netstack_stackid,
6079 			    ire->ire_addr, ire->ire_gateway_addr);
6080 		}
6081 		ip_cgtp_bcast_delete(ire, ipst);
6082 	}
6083 
6084 	ill = ire->ire_ill;
6085 	if (ill != NULL)
6086 		ill_remove_saved_ire(ill, ire);
6087 	if (ioctl_msg)
6088 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6089 	ire_delete(ire);
6090 	ire_refrele(ire);
6091 	return (err);
6092 }
6093 
6094 /*
6095  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6096  */
6097 /* ARGSUSED */
6098 int
6099 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6100     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6101 {
6102 	ipaddr_t dst_addr;
6103 	ipaddr_t gw_addr;
6104 	ipaddr_t mask;
6105 	int error = 0;
6106 	mblk_t *mp1;
6107 	struct rtentry *rt;
6108 	ipif_t *ipif = NULL;
6109 	ip_stack_t	*ipst;
6110 
6111 	ASSERT(q->q_next == NULL);
6112 	ipst = CONNQ_TO_IPST(q);
6113 
6114 	ip1dbg(("ip_siocaddrt:"));
6115 	/* Existence of mp1 verified in ip_wput_nondata */
6116 	mp1 = mp->b_cont->b_cont;
6117 	rt = (struct rtentry *)mp1->b_rptr;
6118 
6119 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6120 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6121 
6122 	/*
6123 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6124 	 * to a particular host address.  In this case, we set the netmask to
6125 	 * all ones for the particular destination address.  Otherwise,
6126 	 * determine the netmask to be used based on dst_addr and the interfaces
6127 	 * in use.
6128 	 */
6129 	if (rt->rt_flags & RTF_HOST) {
6130 		mask = IP_HOST_MASK;
6131 	} else {
6132 		/*
6133 		 * Note that ip_subnet_mask returns a zero mask in the case of
6134 		 * default (an all-zeroes address).
6135 		 */
6136 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6137 	}
6138 
6139 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6140 	    B_TRUE, NULL, ipst, ALL_ZONES);
6141 	if (ipif != NULL)
6142 		ipif_refrele(ipif);
6143 	return (error);
6144 }
6145 
6146 /*
6147  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6148  */
6149 /* ARGSUSED */
6150 int
6151 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6152     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6153 {
6154 	ipaddr_t dst_addr;
6155 	ipaddr_t gw_addr;
6156 	ipaddr_t mask;
6157 	int error;
6158 	mblk_t *mp1;
6159 	struct rtentry *rt;
6160 	ipif_t *ipif = NULL;
6161 	ip_stack_t	*ipst;
6162 
6163 	ASSERT(q->q_next == NULL);
6164 	ipst = CONNQ_TO_IPST(q);
6165 
6166 	ip1dbg(("ip_siocdelrt:"));
6167 	/* Existence of mp1 verified in ip_wput_nondata */
6168 	mp1 = mp->b_cont->b_cont;
6169 	rt = (struct rtentry *)mp1->b_rptr;
6170 
6171 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6172 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6173 
6174 	/*
6175 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6176 	 * to a particular host address.  In this case, we set the netmask to
6177 	 * all ones for the particular destination address.  Otherwise,
6178 	 * determine the netmask to be used based on dst_addr and the interfaces
6179 	 * in use.
6180 	 */
6181 	if (rt->rt_flags & RTF_HOST) {
6182 		mask = IP_HOST_MASK;
6183 	} else {
6184 		/*
6185 		 * Note that ip_subnet_mask returns a zero mask in the case of
6186 		 * default (an all-zeroes address).
6187 		 */
6188 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6189 	}
6190 
6191 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6192 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6193 	    ipst, ALL_ZONES);
6194 	if (ipif != NULL)
6195 		ipif_refrele(ipif);
6196 	return (error);
6197 }
6198 
6199 /*
6200  * Enqueue the mp onto the ipsq, chained by b_next.
6201  * b_prev stores the function to be executed later, and b_queue the queue
6202  * where this mp originated.
6203  */
6204 void
6205 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6206     ill_t *pending_ill)
6207 {
6208 	conn_t	*connp;
6209 	ipxop_t *ipx = ipsq->ipsq_xop;
6210 
6211 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6212 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6213 	ASSERT(func != NULL);
6214 
6215 	mp->b_queue = q;
6216 	mp->b_prev = (void *)func;
6217 	mp->b_next = NULL;
6218 
6219 	switch (type) {
6220 	case CUR_OP:
6221 		if (ipx->ipx_mptail != NULL) {
6222 			ASSERT(ipx->ipx_mphead != NULL);
6223 			ipx->ipx_mptail->b_next = mp;
6224 		} else {
6225 			ASSERT(ipx->ipx_mphead == NULL);
6226 			ipx->ipx_mphead = mp;
6227 		}
6228 		ipx->ipx_mptail = mp;
6229 		break;
6230 
6231 	case NEW_OP:
6232 		if (ipsq->ipsq_xopq_mptail != NULL) {
6233 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6234 			ipsq->ipsq_xopq_mptail->b_next = mp;
6235 		} else {
6236 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6237 			ipsq->ipsq_xopq_mphead = mp;
6238 		}
6239 		ipsq->ipsq_xopq_mptail = mp;
6240 		ipx->ipx_ipsq_queued = B_TRUE;
6241 		break;
6242 
6243 	case SWITCH_OP:
6244 		ASSERT(ipsq->ipsq_swxop != NULL);
6245 		/* only one switch operation is currently allowed */
6246 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6247 		ipsq->ipsq_switch_mp = mp;
6248 		ipx->ipx_ipsq_queued = B_TRUE;
6249 		break;
6250 	default:
6251 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6252 	}
6253 
6254 	if (CONN_Q(q) && pending_ill != NULL) {
6255 		connp = Q_TO_CONN(q);
6256 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6257 		connp->conn_oper_pending_ill = pending_ill;
6258 	}
6259 }
6260 
6261 /*
6262  * Dequeue the next message that requested exclusive access to this IPSQ's
6263  * xop.  Specifically:
6264  *
6265  *  1. If we're still processing the current operation on `ipsq', then
6266  *     dequeue the next message for the operation (from ipx_mphead), or
6267  *     return NULL if there are no queued messages for the operation.
6268  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6269  *
6270  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6271  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6272  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6273  *     leaving IPMP groups and require a careful dance -- see the comments
6274  *     in-line below for details.  If we're leaving a group xop or if we're
6275  *     joining a group xop and become writer on it, then we proceed to (3).
6276  *     Otherwise, we return NULL and exit the xop.
6277  *
6278  *  3. For each IPSQ in the xop, return any switch operation stored on
6279  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6280  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6281  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6282  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6283  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6284  *     each phyint in the group, including the IPMP meta-interface phyint.
6285  */
6286 static mblk_t *
6287 ipsq_dq(ipsq_t *ipsq)
6288 {
6289 	ill_t	*illv4, *illv6;
6290 	mblk_t	*mp;
6291 	ipsq_t	*xopipsq;
6292 	ipsq_t	*leftipsq = NULL;
6293 	ipxop_t *ipx;
6294 	phyint_t *phyi = ipsq->ipsq_phyint;
6295 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6296 	boolean_t emptied = B_FALSE;
6297 
6298 	/*
6299 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6300 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6301 	 */
6302 	rw_enter(&ipst->ips_ill_g_lock,
6303 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6304 	mutex_enter(&ipsq->ipsq_lock);
6305 	ipx = ipsq->ipsq_xop;
6306 	mutex_enter(&ipx->ipx_lock);
6307 
6308 	/*
6309 	 * Dequeue the next message associated with the current exclusive
6310 	 * operation, if any.
6311 	 */
6312 	if ((mp = ipx->ipx_mphead) != NULL) {
6313 		ipx->ipx_mphead = mp->b_next;
6314 		if (ipx->ipx_mphead == NULL)
6315 			ipx->ipx_mptail = NULL;
6316 		mp->b_next = (void *)ipsq;
6317 		goto out;
6318 	}
6319 
6320 	if (ipx->ipx_current_ipif != NULL)
6321 		goto empty;
6322 
6323 	if (ipsq->ipsq_swxop != NULL) {
6324 		/*
6325 		 * The exclusive operation that is now being completed has
6326 		 * requested a switch to a different xop.  This happens
6327 		 * when an interface joins or leaves an IPMP group.  Joins
6328 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6329 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6330 		 * (phyint_free()), or interface plumb for an ill type
6331 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6332 		 *
6333 		 * Xop switches are not allowed on the IPMP meta-interface.
6334 		 */
6335 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6336 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6337 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6338 
6339 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6340 			/*
6341 			 * We're switching back to our own xop, so we have two
6342 			 * xop's to drain/exit: our own, and the group xop
6343 			 * that we are leaving.
6344 			 *
6345 			 * First, pull ourselves out of the group ipsq list.
6346 			 * This is safe since we're writer on ill_g_lock.
6347 			 */
6348 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6349 
6350 			xopipsq = ipx->ipx_ipsq;
6351 			while (xopipsq->ipsq_next != ipsq)
6352 				xopipsq = xopipsq->ipsq_next;
6353 
6354 			xopipsq->ipsq_next = ipsq->ipsq_next;
6355 			ipsq->ipsq_next = ipsq;
6356 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6357 			ipsq->ipsq_swxop = NULL;
6358 
6359 			/*
6360 			 * Second, prepare to exit the group xop.  The actual
6361 			 * ipsq_exit() is done at the end of this function
6362 			 * since we cannot hold any locks across ipsq_exit().
6363 			 * Note that although we drop the group's ipx_lock, no
6364 			 * threads can proceed since we're still ipx_writer.
6365 			 */
6366 			leftipsq = xopipsq;
6367 			mutex_exit(&ipx->ipx_lock);
6368 
6369 			/*
6370 			 * Third, set ipx to point to our own xop (which was
6371 			 * inactive and therefore can be entered).
6372 			 */
6373 			ipx = ipsq->ipsq_xop;
6374 			mutex_enter(&ipx->ipx_lock);
6375 			ASSERT(ipx->ipx_writer == NULL);
6376 			ASSERT(ipx->ipx_current_ipif == NULL);
6377 		} else {
6378 			/*
6379 			 * We're switching from our own xop to a group xop.
6380 			 * The requestor of the switch must ensure that the
6381 			 * group xop cannot go away (e.g. by ensuring the
6382 			 * phyint associated with the xop cannot go away).
6383 			 *
6384 			 * If we can become writer on our new xop, then we'll
6385 			 * do the drain.  Otherwise, the current writer of our
6386 			 * new xop will do the drain when it exits.
6387 			 *
6388 			 * First, splice ourselves into the group IPSQ list.
6389 			 * This is safe since we're writer on ill_g_lock.
6390 			 */
6391 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6392 
6393 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6394 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6395 				xopipsq = xopipsq->ipsq_next;
6396 
6397 			xopipsq->ipsq_next = ipsq;
6398 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6399 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6400 			ipsq->ipsq_swxop = NULL;
6401 
6402 			/*
6403 			 * Second, exit our own xop, since it's now unused.
6404 			 * This is safe since we've got the only reference.
6405 			 */
6406 			ASSERT(ipx->ipx_writer == curthread);
6407 			ipx->ipx_writer = NULL;
6408 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6409 			ipx->ipx_ipsq_queued = B_FALSE;
6410 			mutex_exit(&ipx->ipx_lock);
6411 
6412 			/*
6413 			 * Third, set ipx to point to our new xop, and check
6414 			 * if we can become writer on it.  If we cannot, then
6415 			 * the current writer will drain the IPSQ group when
6416 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6417 			 * because we're still holding ipsq_lock.
6418 			 */
6419 			ipx = ipsq->ipsq_xop;
6420 			mutex_enter(&ipx->ipx_lock);
6421 			if (ipx->ipx_writer != NULL ||
6422 			    ipx->ipx_current_ipif != NULL) {
6423 				goto out;
6424 			}
6425 		}
6426 
6427 		/*
6428 		 * Fourth, become writer on our new ipx before we continue
6429 		 * with the drain.  Note that we never dropped ipsq_lock
6430 		 * above, so no other thread could've raced with us to
6431 		 * become writer first.  Also, we're holding ipx_lock, so
6432 		 * no other thread can examine the ipx right now.
6433 		 */
6434 		ASSERT(ipx->ipx_current_ipif == NULL);
6435 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6436 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6437 		ipx->ipx_writer = curthread;
6438 		ipx->ipx_forced = B_FALSE;
6439 #ifdef DEBUG
6440 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6441 #endif
6442 	}
6443 
6444 	xopipsq = ipsq;
6445 	do {
6446 		/*
6447 		 * So that other operations operate on a consistent and
6448 		 * complete phyint, a switch message on an IPSQ must be
6449 		 * handled prior to any other operations on that IPSQ.
6450 		 */
6451 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6452 			xopipsq->ipsq_switch_mp = NULL;
6453 			ASSERT(mp->b_next == NULL);
6454 			mp->b_next = (void *)xopipsq;
6455 			goto out;
6456 		}
6457 
6458 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6459 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6460 			if (xopipsq->ipsq_xopq_mphead == NULL)
6461 				xopipsq->ipsq_xopq_mptail = NULL;
6462 			mp->b_next = (void *)xopipsq;
6463 			goto out;
6464 		}
6465 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6466 empty:
6467 	/*
6468 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6469 	 * new messages can end up on any IPSQ in the xop.
6470 	 */
6471 	ipx->ipx_writer = NULL;
6472 	ipx->ipx_forced = B_FALSE;
6473 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6474 	ipx->ipx_ipsq_queued = B_FALSE;
6475 	emptied = B_TRUE;
6476 #ifdef	DEBUG
6477 	ipx->ipx_depth = 0;
6478 #endif
6479 out:
6480 	mutex_exit(&ipx->ipx_lock);
6481 	mutex_exit(&ipsq->ipsq_lock);
6482 
6483 	/*
6484 	 * If we completely emptied the xop, then wake up any threads waiting
6485 	 * to enter any of the IPSQ's associated with it.
6486 	 */
6487 	if (emptied) {
6488 		xopipsq = ipsq;
6489 		do {
6490 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6491 				continue;
6492 
6493 			illv4 = phyi->phyint_illv4;
6494 			illv6 = phyi->phyint_illv6;
6495 
6496 			GRAB_ILL_LOCKS(illv4, illv6);
6497 			if (illv4 != NULL)
6498 				cv_broadcast(&illv4->ill_cv);
6499 			if (illv6 != NULL)
6500 				cv_broadcast(&illv6->ill_cv);
6501 			RELEASE_ILL_LOCKS(illv4, illv6);
6502 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6503 	}
6504 	rw_exit(&ipst->ips_ill_g_lock);
6505 
6506 	/*
6507 	 * Now that all locks are dropped, exit the IPSQ we left.
6508 	 */
6509 	if (leftipsq != NULL)
6510 		ipsq_exit(leftipsq);
6511 
6512 	return (mp);
6513 }
6514 
6515 /*
6516  * Return completion status of previously initiated DLPI operations on
6517  * ills in the purview of an ipsq.
6518  */
6519 static boolean_t
6520 ipsq_dlpi_done(ipsq_t *ipsq)
6521 {
6522 	ipsq_t		*ipsq_start;
6523 	phyint_t	*phyi;
6524 	ill_t		*ill;
6525 
6526 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6527 	ipsq_start = ipsq;
6528 
6529 	do {
6530 		/*
6531 		 * The only current users of this function are ipsq_try_enter
6532 		 * and ipsq_enter which have made sure that ipsq_writer is
6533 		 * NULL before we reach here. ill_dlpi_pending is modified
6534 		 * only by an ipsq writer
6535 		 */
6536 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6537 		phyi = ipsq->ipsq_phyint;
6538 		/*
6539 		 * phyi could be NULL if a phyint that is part of an
6540 		 * IPMP group is being unplumbed. A more detailed
6541 		 * comment is in ipmp_grp_update_kstats()
6542 		 */
6543 		if (phyi != NULL) {
6544 			ill = phyi->phyint_illv4;
6545 			if (ill != NULL &&
6546 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6547 			    ill->ill_arl_dlpi_pending))
6548 				return (B_FALSE);
6549 
6550 			ill = phyi->phyint_illv6;
6551 			if (ill != NULL &&
6552 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6553 				return (B_FALSE);
6554 		}
6555 
6556 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6557 
6558 	return (B_TRUE);
6559 }
6560 
6561 /*
6562  * Enter the ipsq corresponding to ill, by waiting synchronously till
6563  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6564  * will have to drain completely before ipsq_enter returns success.
6565  * ipx_current_ipif will be set if some exclusive op is in progress,
6566  * and the ipsq_exit logic will start the next enqueued op after
6567  * completion of the current op. If 'force' is used, we don't wait
6568  * for the enqueued ops. This is needed when a conn_close wants to
6569  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6570  * of an ill can also use this option. But we dont' use it currently.
6571  */
6572 #define	ENTER_SQ_WAIT_TICKS 100
6573 boolean_t
6574 ipsq_enter(ill_t *ill, boolean_t force, int type)
6575 {
6576 	ipsq_t	*ipsq;
6577 	ipxop_t *ipx;
6578 	boolean_t waited_enough = B_FALSE;
6579 	ip_stack_t *ipst = ill->ill_ipst;
6580 
6581 	/*
6582 	 * Note that the relationship between ill and ipsq is fixed as long as
6583 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6584 	 * relationship between the IPSQ and xop cannot change.  However,
6585 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6586 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6587 	 * waking up all ills in the xop when it becomes available.
6588 	 */
6589 	for (;;) {
6590 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6591 		mutex_enter(&ill->ill_lock);
6592 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6593 			mutex_exit(&ill->ill_lock);
6594 			rw_exit(&ipst->ips_ill_g_lock);
6595 			return (B_FALSE);
6596 		}
6597 
6598 		ipsq = ill->ill_phyint->phyint_ipsq;
6599 		mutex_enter(&ipsq->ipsq_lock);
6600 		ipx = ipsq->ipsq_xop;
6601 		mutex_enter(&ipx->ipx_lock);
6602 
6603 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6604 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6605 		    waited_enough))
6606 			break;
6607 
6608 		rw_exit(&ipst->ips_ill_g_lock);
6609 
6610 		if (!force || ipx->ipx_writer != NULL) {
6611 			mutex_exit(&ipx->ipx_lock);
6612 			mutex_exit(&ipsq->ipsq_lock);
6613 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6614 		} else {
6615 			mutex_exit(&ipx->ipx_lock);
6616 			mutex_exit(&ipsq->ipsq_lock);
6617 			(void) cv_reltimedwait(&ill->ill_cv,
6618 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6619 			waited_enough = B_TRUE;
6620 		}
6621 		mutex_exit(&ill->ill_lock);
6622 	}
6623 
6624 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6625 	ASSERT(ipx->ipx_reentry_cnt == 0);
6626 	ipx->ipx_writer = curthread;
6627 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6628 	ipx->ipx_reentry_cnt++;
6629 #ifdef DEBUG
6630 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6631 #endif
6632 	mutex_exit(&ipx->ipx_lock);
6633 	mutex_exit(&ipsq->ipsq_lock);
6634 	mutex_exit(&ill->ill_lock);
6635 	rw_exit(&ipst->ips_ill_g_lock);
6636 
6637 	return (B_TRUE);
6638 }
6639 
6640 /*
6641  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6642  * across the call to the core interface ipsq_try_enter() and hence calls this
6643  * function directly. This is explained more fully in ipif_set_values().
6644  * In order to support the above constraint, ipsq_try_enter is implemented as
6645  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6646  */
6647 static ipsq_t *
6648 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6649     int type, boolean_t reentry_ok)
6650 {
6651 	ipsq_t	*ipsq;
6652 	ipxop_t	*ipx;
6653 	ip_stack_t *ipst = ill->ill_ipst;
6654 
6655 	/*
6656 	 * lock ordering:
6657 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6658 	 *
6659 	 * ipx of an ipsq can't change when ipsq_lock is held.
6660 	 */
6661 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6662 	GRAB_CONN_LOCK(q);
6663 	mutex_enter(&ill->ill_lock);
6664 	ipsq = ill->ill_phyint->phyint_ipsq;
6665 	mutex_enter(&ipsq->ipsq_lock);
6666 	ipx = ipsq->ipsq_xop;
6667 	mutex_enter(&ipx->ipx_lock);
6668 
6669 	/*
6670 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6671 	 *    (Note: If the caller does not specify reentry_ok then neither
6672 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6673 	 *    again. Otherwise it can lead to an infinite loop
6674 	 * 2. Enter the ipsq if there is no current writer and this attempted
6675 	 *    entry is part of the current operation
6676 	 * 3. Enter the ipsq if there is no current writer and this is a new
6677 	 *    operation and the operation queue is empty and there is no
6678 	 *    operation currently in progress and if all previously initiated
6679 	 *    DLPI operations have completed.
6680 	 */
6681 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6682 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6683 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6684 	    ipsq_dlpi_done(ipsq))))) {
6685 		/* Success. */
6686 		ipx->ipx_reentry_cnt++;
6687 		ipx->ipx_writer = curthread;
6688 		ipx->ipx_forced = B_FALSE;
6689 		mutex_exit(&ipx->ipx_lock);
6690 		mutex_exit(&ipsq->ipsq_lock);
6691 		mutex_exit(&ill->ill_lock);
6692 		RELEASE_CONN_LOCK(q);
6693 #ifdef DEBUG
6694 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6695 #endif
6696 		return (ipsq);
6697 	}
6698 
6699 	if (func != NULL)
6700 		ipsq_enq(ipsq, q, mp, func, type, ill);
6701 
6702 	mutex_exit(&ipx->ipx_lock);
6703 	mutex_exit(&ipsq->ipsq_lock);
6704 	mutex_exit(&ill->ill_lock);
6705 	RELEASE_CONN_LOCK(q);
6706 	return (NULL);
6707 }
6708 
6709 /*
6710  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6711  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6712  * There is one ipsq per phyint. The ipsq
6713  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6714  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6715  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6716  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6717  * up the interface) and are enqueued in ipx_mphead.
6718  *
6719  * If a thread does not want to reenter the ipsq when it is already writer,
6720  * it must make sure that the specified reentry point to be called later
6721  * when the ipsq is empty, nor any code path starting from the specified reentry
6722  * point must never ever try to enter the ipsq again. Otherwise it can lead
6723  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6724  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6725  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6726  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6727  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6728  * ioctl if the current ioctl has completed. If the current ioctl is still
6729  * in progress it simply returns. The current ioctl could be waiting for
6730  * a response from another module (the driver or could be waiting for
6731  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6732  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6733  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6734  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6735  * all associated DLPI operations have completed.
6736  */
6737 
6738 /*
6739  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6740  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6741  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6742  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6743  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6744  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6745  */
6746 ipsq_t *
6747 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6748     ipsq_func_t func, int type, boolean_t reentry_ok)
6749 {
6750 	ip_stack_t	*ipst;
6751 	ipsq_t		*ipsq;
6752 
6753 	/* Only 1 of ipif or ill can be specified */
6754 	ASSERT((ipif != NULL) ^ (ill != NULL));
6755 
6756 	if (ipif != NULL)
6757 		ill = ipif->ipif_ill;
6758 	ipst = ill->ill_ipst;
6759 
6760 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6761 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6762 	rw_exit(&ipst->ips_ill_g_lock);
6763 
6764 	return (ipsq);
6765 }
6766 
6767 /*
6768  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6769  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6770  * cannot be entered, the mp is queued for completion.
6771  */
6772 void
6773 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6774     boolean_t reentry_ok)
6775 {
6776 	ipsq_t	*ipsq;
6777 
6778 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6779 
6780 	/*
6781 	 * Drop the caller's refhold on the ill.  This is safe since we either
6782 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6783 	 * IPSQ, in which case we return without accessing ill anymore.  This
6784 	 * is needed because func needs to see the correct refcount.
6785 	 * e.g. removeif can work only then.
6786 	 */
6787 	ill_refrele(ill);
6788 	if (ipsq != NULL) {
6789 		(*func)(ipsq, q, mp, NULL);
6790 		ipsq_exit(ipsq);
6791 	}
6792 }
6793 
6794 /*
6795  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6796  * prior to exiting.  Caller must be writer on the specified IPSQ.
6797  */
6798 void
6799 ipsq_exit(ipsq_t *ipsq)
6800 {
6801 	mblk_t *mp;
6802 	ipsq_t *mp_ipsq;
6803 	queue_t	*q;
6804 	phyint_t *phyi;
6805 	ipsq_func_t func;
6806 
6807 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6808 
6809 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6810 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6811 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6812 		return;
6813 	}
6814 
6815 	for (;;) {
6816 		phyi = ipsq->ipsq_phyint;
6817 		mp = ipsq_dq(ipsq);
6818 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6819 
6820 		/*
6821 		 * If we've changed to a new IPSQ, and the phyint associated
6822 		 * with the old one has gone away, free the old IPSQ.  Note
6823 		 * that this cannot happen while the IPSQ is in a group.
6824 		 */
6825 		if (mp_ipsq != ipsq && phyi == NULL) {
6826 			ASSERT(ipsq->ipsq_next == ipsq);
6827 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6828 			ipsq_delete(ipsq);
6829 		}
6830 
6831 		if (mp == NULL)
6832 			break;
6833 
6834 		q = mp->b_queue;
6835 		func = (ipsq_func_t)mp->b_prev;
6836 		ipsq = mp_ipsq;
6837 		mp->b_next = mp->b_prev = NULL;
6838 		mp->b_queue = NULL;
6839 
6840 		/*
6841 		 * If 'q' is an conn queue, it is valid, since we did a
6842 		 * a refhold on the conn at the start of the ioctl.
6843 		 * If 'q' is an ill queue, it is valid, since close of an
6844 		 * ill will clean up its IPSQ.
6845 		 */
6846 		(*func)(ipsq, q, mp, NULL);
6847 	}
6848 }
6849 
6850 /*
6851  * Used to start any igmp or mld timers that could not be started
6852  * while holding ill_mcast_lock. The timers can't be started while holding
6853  * the lock, since mld/igmp_start_timers may need to call untimeout()
6854  * which can't be done while holding the lock which the timeout handler
6855  * acquires. Otherwise
6856  * there could be a deadlock since the timeout handlers
6857  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6858  * ill_mcast_lock.
6859  */
6860 void
6861 ill_mcast_timer_start(ip_stack_t *ipst)
6862 {
6863 	int		next;
6864 
6865 	mutex_enter(&ipst->ips_igmp_timer_lock);
6866 	next = ipst->ips_igmp_deferred_next;
6867 	ipst->ips_igmp_deferred_next = INFINITY;
6868 	mutex_exit(&ipst->ips_igmp_timer_lock);
6869 
6870 	if (next != INFINITY)
6871 		igmp_start_timers(next, ipst);
6872 
6873 	mutex_enter(&ipst->ips_mld_timer_lock);
6874 	next = ipst->ips_mld_deferred_next;
6875 	ipst->ips_mld_deferred_next = INFINITY;
6876 	mutex_exit(&ipst->ips_mld_timer_lock);
6877 
6878 	if (next != INFINITY)
6879 		mld_start_timers(next, ipst);
6880 }
6881 
6882 /*
6883  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6884  * and `ioccmd'.
6885  */
6886 void
6887 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6888 {
6889 	ill_t *ill = ipif->ipif_ill;
6890 	ipxop_t *ipx = ipsq->ipsq_xop;
6891 
6892 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6893 	ASSERT(ipx->ipx_current_ipif == NULL);
6894 	ASSERT(ipx->ipx_current_ioctl == 0);
6895 
6896 	ipx->ipx_current_done = B_FALSE;
6897 	ipx->ipx_current_ioctl = ioccmd;
6898 	mutex_enter(&ipx->ipx_lock);
6899 	ipx->ipx_current_ipif = ipif;
6900 	mutex_exit(&ipx->ipx_lock);
6901 
6902 	/*
6903 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6904 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6905 	 * references to the ipif (so that the references will eventually
6906 	 * drop to zero) and also prevents any "get" operations (e.g.,
6907 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6908 	 * operation has completed and the ipif is again in a stable state.
6909 	 *
6910 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6911 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6912 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6913 	 * ipifs will be affected.
6914 	 *
6915 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6916 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6917 	 * operate on.
6918 	 */
6919 	switch (ioccmd) {
6920 	case SIOCLIFREMOVEIF:
6921 		break;
6922 	case 0:
6923 		mutex_enter(&ill->ill_lock);
6924 		ipif = ipif->ipif_ill->ill_ipif;
6925 		for (; ipif != NULL; ipif = ipif->ipif_next)
6926 			ipif->ipif_state_flags |= IPIF_CHANGING;
6927 		mutex_exit(&ill->ill_lock);
6928 		break;
6929 	default:
6930 		mutex_enter(&ill->ill_lock);
6931 		ipif->ipif_state_flags |= IPIF_CHANGING;
6932 		mutex_exit(&ill->ill_lock);
6933 	}
6934 }
6935 
6936 /*
6937  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6938  * the next exclusive operation to begin once we ipsq_exit().  However, if
6939  * pending DLPI operations remain, then we will wait for the queue to drain
6940  * before allowing the next exclusive operation to begin.  This ensures that
6941  * DLPI operations from one exclusive operation are never improperly processed
6942  * as part of a subsequent exclusive operation.
6943  */
6944 void
6945 ipsq_current_finish(ipsq_t *ipsq)
6946 {
6947 	ipxop_t	*ipx = ipsq->ipsq_xop;
6948 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6949 	ipif_t	*ipif = ipx->ipx_current_ipif;
6950 
6951 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6952 
6953 	/*
6954 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6955 	 * (but in that case, IPIF_CHANGING will already be clear and no
6956 	 * pending DLPI messages can remain).
6957 	 */
6958 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6959 		ill_t *ill = ipif->ipif_ill;
6960 
6961 		mutex_enter(&ill->ill_lock);
6962 		dlpi_pending = ill->ill_dlpi_pending;
6963 		if (ipx->ipx_current_ioctl == 0) {
6964 			ipif = ill->ill_ipif;
6965 			for (; ipif != NULL; ipif = ipif->ipif_next)
6966 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6967 		} else {
6968 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6969 		}
6970 		mutex_exit(&ill->ill_lock);
6971 	}
6972 
6973 	ASSERT(!ipx->ipx_current_done);
6974 	ipx->ipx_current_done = B_TRUE;
6975 	ipx->ipx_current_ioctl = 0;
6976 	if (dlpi_pending == DL_PRIM_INVAL) {
6977 		mutex_enter(&ipx->ipx_lock);
6978 		ipx->ipx_current_ipif = NULL;
6979 		mutex_exit(&ipx->ipx_lock);
6980 	}
6981 }
6982 
6983 /*
6984  * The ill is closing. Flush all messages on the ipsq that originated
6985  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6986  * for this ill since ipsq_enter could not have entered until then.
6987  * New messages can't be queued since the CONDEMNED flag is set.
6988  */
6989 static void
6990 ipsq_flush(ill_t *ill)
6991 {
6992 	queue_t	*q;
6993 	mblk_t	*prev;
6994 	mblk_t	*mp;
6995 	mblk_t	*mp_next;
6996 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6997 
6998 	ASSERT(IAM_WRITER_ILL(ill));
6999 
7000 	/*
7001 	 * Flush any messages sent up by the driver.
7002 	 */
7003 	mutex_enter(&ipx->ipx_lock);
7004 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7005 		mp_next = mp->b_next;
7006 		q = mp->b_queue;
7007 		if (q == ill->ill_rq || q == ill->ill_wq) {
7008 			/* dequeue mp */
7009 			if (prev == NULL)
7010 				ipx->ipx_mphead = mp->b_next;
7011 			else
7012 				prev->b_next = mp->b_next;
7013 			if (ipx->ipx_mptail == mp) {
7014 				ASSERT(mp_next == NULL);
7015 				ipx->ipx_mptail = prev;
7016 			}
7017 			inet_freemsg(mp);
7018 		} else {
7019 			prev = mp;
7020 		}
7021 	}
7022 	mutex_exit(&ipx->ipx_lock);
7023 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7024 	ipsq_xopq_mp_cleanup(ill, NULL);
7025 }
7026 
7027 /*
7028  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7029  * and return the associated ipif.
7030  * Return value:
7031  *	Non zero: An error has occurred. ci may not be filled out.
7032  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7033  *	a held ipif in ci.ci_ipif.
7034  */
7035 int
7036 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7037     cmd_info_t *ci)
7038 {
7039 	char		*name;
7040 	struct ifreq    *ifr;
7041 	struct lifreq    *lifr;
7042 	ipif_t		*ipif = NULL;
7043 	ill_t		*ill;
7044 	conn_t		*connp;
7045 	boolean_t	isv6;
7046 	boolean_t	exists;
7047 	mblk_t		*mp1;
7048 	zoneid_t	zoneid;
7049 	ip_stack_t	*ipst;
7050 
7051 	if (q->q_next != NULL) {
7052 		ill = (ill_t *)q->q_ptr;
7053 		isv6 = ill->ill_isv6;
7054 		connp = NULL;
7055 		zoneid = ALL_ZONES;
7056 		ipst = ill->ill_ipst;
7057 	} else {
7058 		ill = NULL;
7059 		connp = Q_TO_CONN(q);
7060 		isv6 = (connp->conn_family == AF_INET6);
7061 		zoneid = connp->conn_zoneid;
7062 		if (zoneid == GLOBAL_ZONEID) {
7063 			/* global zone can access ipifs in all zones */
7064 			zoneid = ALL_ZONES;
7065 		}
7066 		ipst = connp->conn_netstack->netstack_ip;
7067 	}
7068 
7069 	/* Has been checked in ip_wput_nondata */
7070 	mp1 = mp->b_cont->b_cont;
7071 
7072 	if (ipip->ipi_cmd_type == IF_CMD) {
7073 		/* This a old style SIOC[GS]IF* command */
7074 		ifr = (struct ifreq *)mp1->b_rptr;
7075 		/*
7076 		 * Null terminate the string to protect against buffer
7077 		 * overrun. String was generated by user code and may not
7078 		 * be trusted.
7079 		 */
7080 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7081 		name = ifr->ifr_name;
7082 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7083 		ci->ci_sin6 = NULL;
7084 		ci->ci_lifr = (struct lifreq *)ifr;
7085 	} else {
7086 		/* This a new style SIOC[GS]LIF* command */
7087 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7088 		lifr = (struct lifreq *)mp1->b_rptr;
7089 		/*
7090 		 * Null terminate the string to protect against buffer
7091 		 * overrun. String was generated by user code and may not
7092 		 * be trusted.
7093 		 */
7094 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7095 		name = lifr->lifr_name;
7096 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7097 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7098 		ci->ci_lifr = lifr;
7099 	}
7100 
7101 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7102 		/*
7103 		 * The ioctl will be failed if the ioctl comes down
7104 		 * an conn stream
7105 		 */
7106 		if (ill == NULL) {
7107 			/*
7108 			 * Not an ill queue, return EINVAL same as the
7109 			 * old error code.
7110 			 */
7111 			return (ENXIO);
7112 		}
7113 		ipif = ill->ill_ipif;
7114 		ipif_refhold(ipif);
7115 	} else {
7116 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7117 		    &exists, isv6, zoneid, ipst);
7118 
7119 		/*
7120 		 * Ensure that get ioctls don't see any internal state changes
7121 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7122 		 * set.
7123 		 */
7124 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7125 		    !IAM_WRITER_IPIF(ipif)) {
7126 			ipsq_t	*ipsq;
7127 
7128 			if (connp != NULL)
7129 				mutex_enter(&connp->conn_lock);
7130 			mutex_enter(&ipif->ipif_ill->ill_lock);
7131 			if (IPIF_IS_CHANGING(ipif) &&
7132 			    !IPIF_IS_CONDEMNED(ipif)) {
7133 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7134 				mutex_enter(&ipsq->ipsq_lock);
7135 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7136 				mutex_exit(&ipif->ipif_ill->ill_lock);
7137 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7138 				    NEW_OP, ipif->ipif_ill);
7139 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7140 				mutex_exit(&ipsq->ipsq_lock);
7141 				if (connp != NULL)
7142 					mutex_exit(&connp->conn_lock);
7143 				ipif_refrele(ipif);
7144 				return (EINPROGRESS);
7145 			}
7146 			mutex_exit(&ipif->ipif_ill->ill_lock);
7147 			if (connp != NULL)
7148 				mutex_exit(&connp->conn_lock);
7149 		}
7150 	}
7151 
7152 	/*
7153 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7154 	 */
7155 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7156 		ipif_refrele(ipif);
7157 		return (ENXIO);
7158 	}
7159 
7160 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7161 	    name[0] == '\0') {
7162 		/*
7163 		 * Handle a or a SIOC?IF* with a null name
7164 		 * during plumb (on the ill queue before the I_PLINK).
7165 		 */
7166 		ipif = ill->ill_ipif;
7167 		ipif_refhold(ipif);
7168 	}
7169 
7170 	if (ipif == NULL)
7171 		return (ENXIO);
7172 
7173 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7174 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7175 
7176 	ci->ci_ipif = ipif;
7177 	return (0);
7178 }
7179 
7180 /*
7181  * Return the total number of ipifs.
7182  */
7183 static uint_t
7184 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7185 {
7186 	uint_t numifs = 0;
7187 	ill_t	*ill;
7188 	ill_walk_context_t	ctx;
7189 	ipif_t	*ipif;
7190 
7191 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7192 	ill = ILL_START_WALK_V4(&ctx, ipst);
7193 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7194 		if (IS_UNDER_IPMP(ill))
7195 			continue;
7196 		for (ipif = ill->ill_ipif; ipif != NULL;
7197 		    ipif = ipif->ipif_next) {
7198 			if (ipif->ipif_zoneid == zoneid ||
7199 			    ipif->ipif_zoneid == ALL_ZONES)
7200 				numifs++;
7201 		}
7202 	}
7203 	rw_exit(&ipst->ips_ill_g_lock);
7204 	return (numifs);
7205 }
7206 
7207 /*
7208  * Return the total number of ipifs.
7209  */
7210 static uint_t
7211 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7212 {
7213 	uint_t numifs = 0;
7214 	ill_t	*ill;
7215 	ipif_t	*ipif;
7216 	ill_walk_context_t	ctx;
7217 
7218 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7219 
7220 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7221 	if (family == AF_INET)
7222 		ill = ILL_START_WALK_V4(&ctx, ipst);
7223 	else if (family == AF_INET6)
7224 		ill = ILL_START_WALK_V6(&ctx, ipst);
7225 	else
7226 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7227 
7228 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7229 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7230 			continue;
7231 
7232 		for (ipif = ill->ill_ipif; ipif != NULL;
7233 		    ipif = ipif->ipif_next) {
7234 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7235 			    !(lifn_flags & LIFC_NOXMIT))
7236 				continue;
7237 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7238 			    !(lifn_flags & LIFC_TEMPORARY))
7239 				continue;
7240 			if (((ipif->ipif_flags &
7241 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7242 			    IPIF_DEPRECATED)) ||
7243 			    IS_LOOPBACK(ill) ||
7244 			    !(ipif->ipif_flags & IPIF_UP)) &&
7245 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7246 				continue;
7247 
7248 			if (zoneid != ipif->ipif_zoneid &&
7249 			    ipif->ipif_zoneid != ALL_ZONES &&
7250 			    (zoneid != GLOBAL_ZONEID ||
7251 			    !(lifn_flags & LIFC_ALLZONES)))
7252 				continue;
7253 
7254 			numifs++;
7255 		}
7256 	}
7257 	rw_exit(&ipst->ips_ill_g_lock);
7258 	return (numifs);
7259 }
7260 
7261 uint_t
7262 ip_get_lifsrcofnum(ill_t *ill)
7263 {
7264 	uint_t numifs = 0;
7265 	ill_t	*ill_head = ill;
7266 	ip_stack_t	*ipst = ill->ill_ipst;
7267 
7268 	/*
7269 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7270 	 * other thread may be trying to relink the ILLs in this usesrc group
7271 	 * and adjusting the ill_usesrc_grp_next pointers
7272 	 */
7273 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7274 	if ((ill->ill_usesrc_ifindex == 0) &&
7275 	    (ill->ill_usesrc_grp_next != NULL)) {
7276 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7277 		    ill = ill->ill_usesrc_grp_next)
7278 			numifs++;
7279 	}
7280 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7281 
7282 	return (numifs);
7283 }
7284 
7285 /* Null values are passed in for ipif, sin, and ifreq */
7286 /* ARGSUSED */
7287 int
7288 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7289     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7290 {
7291 	int *nump;
7292 	conn_t *connp = Q_TO_CONN(q);
7293 
7294 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7295 
7296 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7297 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7298 
7299 	*nump = ip_get_numifs(connp->conn_zoneid,
7300 	    connp->conn_netstack->netstack_ip);
7301 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7302 	return (0);
7303 }
7304 
7305 /* Null values are passed in for ipif, sin, and ifreq */
7306 /* ARGSUSED */
7307 int
7308 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7309     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7310 {
7311 	struct lifnum *lifn;
7312 	mblk_t	*mp1;
7313 	conn_t *connp = Q_TO_CONN(q);
7314 
7315 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7316 
7317 	/* Existence checked in ip_wput_nondata */
7318 	mp1 = mp->b_cont->b_cont;
7319 
7320 	lifn = (struct lifnum *)mp1->b_rptr;
7321 	switch (lifn->lifn_family) {
7322 	case AF_UNSPEC:
7323 	case AF_INET:
7324 	case AF_INET6:
7325 		break;
7326 	default:
7327 		return (EAFNOSUPPORT);
7328 	}
7329 
7330 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7331 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7332 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7333 	return (0);
7334 }
7335 
7336 /* ARGSUSED */
7337 int
7338 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7339     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7340 {
7341 	STRUCT_HANDLE(ifconf, ifc);
7342 	mblk_t *mp1;
7343 	struct iocblk *iocp;
7344 	struct ifreq *ifr;
7345 	ill_walk_context_t	ctx;
7346 	ill_t	*ill;
7347 	ipif_t	*ipif;
7348 	struct sockaddr_in *sin;
7349 	int32_t	ifclen;
7350 	zoneid_t zoneid;
7351 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7352 
7353 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7354 
7355 	ip1dbg(("ip_sioctl_get_ifconf"));
7356 	/* Existence verified in ip_wput_nondata */
7357 	mp1 = mp->b_cont->b_cont;
7358 	iocp = (struct iocblk *)mp->b_rptr;
7359 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7360 
7361 	/*
7362 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7363 	 * the user buffer address and length into which the list of struct
7364 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7365 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7366 	 * the SIOCGIFCONF operation was redefined to simply provide
7367 	 * a large output buffer into which we are supposed to jam the ifreq
7368 	 * array.  The same ioctl command code was used, despite the fact that
7369 	 * both the applications and the kernel code had to change, thus making
7370 	 * it impossible to support both interfaces.
7371 	 *
7372 	 * For reasons not good enough to try to explain, the following
7373 	 * algorithm is used for deciding what to do with one of these:
7374 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7375 	 * form with the output buffer coming down as the continuation message.
7376 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7377 	 * and we have to copy in the ifconf structure to find out how big the
7378 	 * output buffer is and where to copy out to.  Sure no problem...
7379 	 *
7380 	 */
7381 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7382 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7383 		int numifs = 0;
7384 		size_t ifc_bufsize;
7385 
7386 		/*
7387 		 * Must be (better be!) continuation of a TRANSPARENT
7388 		 * IOCTL.  We just copied in the ifconf structure.
7389 		 */
7390 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7391 		    (struct ifconf *)mp1->b_rptr);
7392 
7393 		/*
7394 		 * Allocate a buffer to hold requested information.
7395 		 *
7396 		 * If ifc_len is larger than what is needed, we only
7397 		 * allocate what we will use.
7398 		 *
7399 		 * If ifc_len is smaller than what is needed, return
7400 		 * EINVAL.
7401 		 *
7402 		 * XXX: the ill_t structure can hava 2 counters, for
7403 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7404 		 * number of interfaces for a device, so we don't need
7405 		 * to count them here...
7406 		 */
7407 		numifs = ip_get_numifs(zoneid, ipst);
7408 
7409 		ifclen = STRUCT_FGET(ifc, ifc_len);
7410 		ifc_bufsize = numifs * sizeof (struct ifreq);
7411 		if (ifc_bufsize > ifclen) {
7412 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7413 				/* old behaviour */
7414 				return (EINVAL);
7415 			} else {
7416 				ifc_bufsize = ifclen;
7417 			}
7418 		}
7419 
7420 		mp1 = mi_copyout_alloc(q, mp,
7421 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7422 		if (mp1 == NULL)
7423 			return (ENOMEM);
7424 
7425 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7426 	}
7427 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7428 	/*
7429 	 * the SIOCGIFCONF ioctl only knows about
7430 	 * IPv4 addresses, so don't try to tell
7431 	 * it about interfaces with IPv6-only
7432 	 * addresses. (Last parm 'isv6' is B_FALSE)
7433 	 */
7434 
7435 	ifr = (struct ifreq *)mp1->b_rptr;
7436 
7437 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7438 	ill = ILL_START_WALK_V4(&ctx, ipst);
7439 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7440 		if (IS_UNDER_IPMP(ill))
7441 			continue;
7442 		for (ipif = ill->ill_ipif; ipif != NULL;
7443 		    ipif = ipif->ipif_next) {
7444 			if (zoneid != ipif->ipif_zoneid &&
7445 			    ipif->ipif_zoneid != ALL_ZONES)
7446 				continue;
7447 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7448 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7449 					/* old behaviour */
7450 					rw_exit(&ipst->ips_ill_g_lock);
7451 					return (EINVAL);
7452 				} else {
7453 					goto if_copydone;
7454 				}
7455 			}
7456 			ipif_get_name(ipif, ifr->ifr_name,
7457 			    sizeof (ifr->ifr_name));
7458 			sin = (sin_t *)&ifr->ifr_addr;
7459 			*sin = sin_null;
7460 			sin->sin_family = AF_INET;
7461 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7462 			ifr++;
7463 		}
7464 	}
7465 if_copydone:
7466 	rw_exit(&ipst->ips_ill_g_lock);
7467 	mp1->b_wptr = (uchar_t *)ifr;
7468 
7469 	if (STRUCT_BUF(ifc) != NULL) {
7470 		STRUCT_FSET(ifc, ifc_len,
7471 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7472 	}
7473 	return (0);
7474 }
7475 
7476 /*
7477  * Get the interfaces using the address hosted on the interface passed in,
7478  * as a source adddress
7479  */
7480 /* ARGSUSED */
7481 int
7482 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7483     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7484 {
7485 	mblk_t *mp1;
7486 	ill_t	*ill, *ill_head;
7487 	ipif_t	*ipif, *orig_ipif;
7488 	int	numlifs = 0;
7489 	size_t	lifs_bufsize, lifsmaxlen;
7490 	struct	lifreq *lifr;
7491 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7492 	uint_t	ifindex;
7493 	zoneid_t zoneid;
7494 	boolean_t isv6 = B_FALSE;
7495 	struct	sockaddr_in	*sin;
7496 	struct	sockaddr_in6	*sin6;
7497 	STRUCT_HANDLE(lifsrcof, lifs);
7498 	ip_stack_t		*ipst;
7499 
7500 	ipst = CONNQ_TO_IPST(q);
7501 
7502 	ASSERT(q->q_next == NULL);
7503 
7504 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7505 
7506 	/* Existence verified in ip_wput_nondata */
7507 	mp1 = mp->b_cont->b_cont;
7508 
7509 	/*
7510 	 * Must be (better be!) continuation of a TRANSPARENT
7511 	 * IOCTL.  We just copied in the lifsrcof structure.
7512 	 */
7513 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7514 	    (struct lifsrcof *)mp1->b_rptr);
7515 
7516 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7517 		return (EINVAL);
7518 
7519 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7520 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7521 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7522 	if (ipif == NULL) {
7523 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7524 		    ifindex));
7525 		return (ENXIO);
7526 	}
7527 
7528 	/* Allocate a buffer to hold requested information */
7529 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7530 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7531 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7532 	/* The actual size needed is always returned in lifs_len */
7533 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7534 
7535 	/* If the amount we need is more than what is passed in, abort */
7536 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7537 		ipif_refrele(ipif);
7538 		return (0);
7539 	}
7540 
7541 	mp1 = mi_copyout_alloc(q, mp,
7542 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7543 	if (mp1 == NULL) {
7544 		ipif_refrele(ipif);
7545 		return (ENOMEM);
7546 	}
7547 
7548 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7549 	bzero(mp1->b_rptr, lifs_bufsize);
7550 
7551 	lifr = (struct lifreq *)mp1->b_rptr;
7552 
7553 	ill = ill_head = ipif->ipif_ill;
7554 	orig_ipif = ipif;
7555 
7556 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7557 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7558 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7559 
7560 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7561 	for (; (ill != NULL) && (ill != ill_head);
7562 	    ill = ill->ill_usesrc_grp_next) {
7563 
7564 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7565 			break;
7566 
7567 		ipif = ill->ill_ipif;
7568 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7569 		if (ipif->ipif_isv6) {
7570 			sin6 = (sin6_t *)&lifr->lifr_addr;
7571 			*sin6 = sin6_null;
7572 			sin6->sin6_family = AF_INET6;
7573 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7574 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7575 			    &ipif->ipif_v6net_mask);
7576 		} else {
7577 			sin = (sin_t *)&lifr->lifr_addr;
7578 			*sin = sin_null;
7579 			sin->sin_family = AF_INET;
7580 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7581 			lifr->lifr_addrlen = ip_mask_to_plen(
7582 			    ipif->ipif_net_mask);
7583 		}
7584 		lifr++;
7585 	}
7586 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7587 	rw_exit(&ipst->ips_ill_g_lock);
7588 	ipif_refrele(orig_ipif);
7589 	mp1->b_wptr = (uchar_t *)lifr;
7590 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7591 
7592 	return (0);
7593 }
7594 
7595 /* ARGSUSED */
7596 int
7597 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7598     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7599 {
7600 	mblk_t *mp1;
7601 	int	list;
7602 	ill_t	*ill;
7603 	ipif_t	*ipif;
7604 	int	flags;
7605 	int	numlifs = 0;
7606 	size_t	lifc_bufsize;
7607 	struct	lifreq *lifr;
7608 	sa_family_t	family;
7609 	struct	sockaddr_in	*sin;
7610 	struct	sockaddr_in6	*sin6;
7611 	ill_walk_context_t	ctx;
7612 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7613 	int32_t	lifclen;
7614 	zoneid_t zoneid;
7615 	STRUCT_HANDLE(lifconf, lifc);
7616 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7617 
7618 	ip1dbg(("ip_sioctl_get_lifconf"));
7619 
7620 	ASSERT(q->q_next == NULL);
7621 
7622 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7623 
7624 	/* Existence verified in ip_wput_nondata */
7625 	mp1 = mp->b_cont->b_cont;
7626 
7627 	/*
7628 	 * An extended version of SIOCGIFCONF that takes an
7629 	 * additional address family and flags field.
7630 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7631 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7632 	 * interfaces are omitted.
7633 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7634 	 * unless LIFC_TEMPORARY is specified.
7635 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7636 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7637 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7638 	 * has priority over LIFC_NOXMIT.
7639 	 */
7640 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7641 
7642 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7643 		return (EINVAL);
7644 
7645 	/*
7646 	 * Must be (better be!) continuation of a TRANSPARENT
7647 	 * IOCTL.  We just copied in the lifconf structure.
7648 	 */
7649 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7650 
7651 	family = STRUCT_FGET(lifc, lifc_family);
7652 	flags = STRUCT_FGET(lifc, lifc_flags);
7653 
7654 	switch (family) {
7655 	case AF_UNSPEC:
7656 		/*
7657 		 * walk all ILL's.
7658 		 */
7659 		list = MAX_G_HEADS;
7660 		break;
7661 	case AF_INET:
7662 		/*
7663 		 * walk only IPV4 ILL's.
7664 		 */
7665 		list = IP_V4_G_HEAD;
7666 		break;
7667 	case AF_INET6:
7668 		/*
7669 		 * walk only IPV6 ILL's.
7670 		 */
7671 		list = IP_V6_G_HEAD;
7672 		break;
7673 	default:
7674 		return (EAFNOSUPPORT);
7675 	}
7676 
7677 	/*
7678 	 * Allocate a buffer to hold requested information.
7679 	 *
7680 	 * If lifc_len is larger than what is needed, we only
7681 	 * allocate what we will use.
7682 	 *
7683 	 * If lifc_len is smaller than what is needed, return
7684 	 * EINVAL.
7685 	 */
7686 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7687 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7688 	lifclen = STRUCT_FGET(lifc, lifc_len);
7689 	if (lifc_bufsize > lifclen) {
7690 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7691 			return (EINVAL);
7692 		else
7693 			lifc_bufsize = lifclen;
7694 	}
7695 
7696 	mp1 = mi_copyout_alloc(q, mp,
7697 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7698 	if (mp1 == NULL)
7699 		return (ENOMEM);
7700 
7701 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7702 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7703 
7704 	lifr = (struct lifreq *)mp1->b_rptr;
7705 
7706 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7707 	ill = ill_first(list, list, &ctx, ipst);
7708 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7709 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7710 			continue;
7711 
7712 		for (ipif = ill->ill_ipif; ipif != NULL;
7713 		    ipif = ipif->ipif_next) {
7714 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7715 			    !(flags & LIFC_NOXMIT))
7716 				continue;
7717 
7718 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7719 			    !(flags & LIFC_TEMPORARY))
7720 				continue;
7721 
7722 			if (((ipif->ipif_flags &
7723 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7724 			    IPIF_DEPRECATED)) ||
7725 			    IS_LOOPBACK(ill) ||
7726 			    !(ipif->ipif_flags & IPIF_UP)) &&
7727 			    (flags & LIFC_EXTERNAL_SOURCE))
7728 				continue;
7729 
7730 			if (zoneid != ipif->ipif_zoneid &&
7731 			    ipif->ipif_zoneid != ALL_ZONES &&
7732 			    (zoneid != GLOBAL_ZONEID ||
7733 			    !(flags & LIFC_ALLZONES)))
7734 				continue;
7735 
7736 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7737 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7738 					rw_exit(&ipst->ips_ill_g_lock);
7739 					return (EINVAL);
7740 				} else {
7741 					goto lif_copydone;
7742 				}
7743 			}
7744 
7745 			ipif_get_name(ipif, lifr->lifr_name,
7746 			    sizeof (lifr->lifr_name));
7747 			lifr->lifr_type = ill->ill_type;
7748 			if (ipif->ipif_isv6) {
7749 				sin6 = (sin6_t *)&lifr->lifr_addr;
7750 				*sin6 = sin6_null;
7751 				sin6->sin6_family = AF_INET6;
7752 				sin6->sin6_addr =
7753 				    ipif->ipif_v6lcl_addr;
7754 				lifr->lifr_addrlen =
7755 				    ip_mask_to_plen_v6(
7756 				    &ipif->ipif_v6net_mask);
7757 			} else {
7758 				sin = (sin_t *)&lifr->lifr_addr;
7759 				*sin = sin_null;
7760 				sin->sin_family = AF_INET;
7761 				sin->sin_addr.s_addr =
7762 				    ipif->ipif_lcl_addr;
7763 				lifr->lifr_addrlen =
7764 				    ip_mask_to_plen(
7765 				    ipif->ipif_net_mask);
7766 			}
7767 			lifr++;
7768 		}
7769 	}
7770 lif_copydone:
7771 	rw_exit(&ipst->ips_ill_g_lock);
7772 
7773 	mp1->b_wptr = (uchar_t *)lifr;
7774 	if (STRUCT_BUF(lifc) != NULL) {
7775 		STRUCT_FSET(lifc, lifc_len,
7776 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7777 	}
7778 	return (0);
7779 }
7780 
7781 static void
7782 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7783 {
7784 	ip6_asp_t *table;
7785 	size_t table_size;
7786 	mblk_t *data_mp;
7787 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7788 	ip_stack_t	*ipst;
7789 
7790 	if (q->q_next == NULL)
7791 		ipst = CONNQ_TO_IPST(q);
7792 	else
7793 		ipst = ILLQ_TO_IPST(q);
7794 
7795 	/* These two ioctls are I_STR only */
7796 	if (iocp->ioc_count == TRANSPARENT) {
7797 		miocnak(q, mp, 0, EINVAL);
7798 		return;
7799 	}
7800 
7801 	data_mp = mp->b_cont;
7802 	if (data_mp == NULL) {
7803 		/* The user passed us a NULL argument */
7804 		table = NULL;
7805 		table_size = iocp->ioc_count;
7806 	} else {
7807 		/*
7808 		 * The user provided a table.  The stream head
7809 		 * may have copied in the user data in chunks,
7810 		 * so make sure everything is pulled up
7811 		 * properly.
7812 		 */
7813 		if (MBLKL(data_mp) < iocp->ioc_count) {
7814 			mblk_t *new_data_mp;
7815 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7816 			    NULL) {
7817 				miocnak(q, mp, 0, ENOMEM);
7818 				return;
7819 			}
7820 			freemsg(data_mp);
7821 			data_mp = new_data_mp;
7822 			mp->b_cont = data_mp;
7823 		}
7824 		table = (ip6_asp_t *)data_mp->b_rptr;
7825 		table_size = iocp->ioc_count;
7826 	}
7827 
7828 	switch (iocp->ioc_cmd) {
7829 	case SIOCGIP6ADDRPOLICY:
7830 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7831 		if (iocp->ioc_rval == -1)
7832 			iocp->ioc_error = EINVAL;
7833 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7834 		else if (table != NULL &&
7835 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7836 			ip6_asp_t *src = table;
7837 			ip6_asp32_t *dst = (void *)table;
7838 			int count = table_size / sizeof (ip6_asp_t);
7839 			int i;
7840 
7841 			/*
7842 			 * We need to do an in-place shrink of the array
7843 			 * to match the alignment attributes of the
7844 			 * 32-bit ABI looking at it.
7845 			 */
7846 			/* LINTED: logical expression always true: op "||" */
7847 			ASSERT(sizeof (*src) > sizeof (*dst));
7848 			for (i = 1; i < count; i++)
7849 				bcopy(src + i, dst + i, sizeof (*dst));
7850 		}
7851 #endif
7852 		break;
7853 
7854 	case SIOCSIP6ADDRPOLICY:
7855 		ASSERT(mp->b_prev == NULL);
7856 		mp->b_prev = (void *)q;
7857 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7858 		/*
7859 		 * We pass in the datamodel here so that the ip6_asp_replace()
7860 		 * routine can handle converting from 32-bit to native formats
7861 		 * where necessary.
7862 		 *
7863 		 * A better way to handle this might be to convert the inbound
7864 		 * data structure here, and hang it off a new 'mp'; thus the
7865 		 * ip6_asp_replace() logic would always be dealing with native
7866 		 * format data structures..
7867 		 *
7868 		 * (An even simpler way to handle these ioctls is to just
7869 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7870 		 * and just recompile everything that depends on it.)
7871 		 */
7872 #endif
7873 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7874 		    iocp->ioc_flag & IOC_MODELS);
7875 		return;
7876 	}
7877 
7878 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7879 	qreply(q, mp);
7880 }
7881 
7882 static void
7883 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7884 {
7885 	mblk_t 		*data_mp;
7886 	struct dstinforeq	*dir;
7887 	uint8_t		*end, *cur;
7888 	in6_addr_t	*daddr, *saddr;
7889 	ipaddr_t	v4daddr;
7890 	ire_t		*ire;
7891 	ipaddr_t	v4setsrc;
7892 	in6_addr_t	v6setsrc;
7893 	char		*slabel, *dlabel;
7894 	boolean_t	isipv4;
7895 	int		match_ire;
7896 	ill_t		*dst_ill;
7897 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7898 	conn_t		*connp = Q_TO_CONN(q);
7899 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7900 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7901 	uint64_t	ipif_flags;
7902 
7903 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7904 
7905 	/*
7906 	 * This ioctl is I_STR only, and must have a
7907 	 * data mblk following the M_IOCTL mblk.
7908 	 */
7909 	data_mp = mp->b_cont;
7910 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7911 		miocnak(q, mp, 0, EINVAL);
7912 		return;
7913 	}
7914 
7915 	if (MBLKL(data_mp) < iocp->ioc_count) {
7916 		mblk_t *new_data_mp;
7917 
7918 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7919 			miocnak(q, mp, 0, ENOMEM);
7920 			return;
7921 		}
7922 		freemsg(data_mp);
7923 		data_mp = new_data_mp;
7924 		mp->b_cont = data_mp;
7925 	}
7926 	match_ire = MATCH_IRE_DSTONLY;
7927 
7928 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7929 	    end - cur >= sizeof (struct dstinforeq);
7930 	    cur += sizeof (struct dstinforeq)) {
7931 		dir = (struct dstinforeq *)cur;
7932 		daddr = &dir->dir_daddr;
7933 		saddr = &dir->dir_saddr;
7934 
7935 		/*
7936 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7937 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7938 		 * and ip_select_source_v6() do not.
7939 		 */
7940 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7941 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7942 
7943 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7944 		if (isipv4) {
7945 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7946 			v4setsrc = INADDR_ANY;
7947 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7948 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7949 			    NULL, NULL);
7950 		} else {
7951 			v6setsrc = ipv6_all_zeros;
7952 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7953 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7954 			    NULL, NULL);
7955 		}
7956 		ASSERT(ire != NULL);
7957 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7958 			ire_refrele(ire);
7959 			dir->dir_dreachable = 0;
7960 
7961 			/* move on to next dst addr */
7962 			continue;
7963 		}
7964 		dir->dir_dreachable = 1;
7965 
7966 		dst_ill = ire_nexthop_ill(ire);
7967 		if (dst_ill == NULL) {
7968 			ire_refrele(ire);
7969 			continue;
7970 		}
7971 
7972 		/* With ipmp we most likely look at the ipmp ill here */
7973 		dir->dir_dmactype = dst_ill->ill_mactype;
7974 
7975 		if (isipv4) {
7976 			ipaddr_t v4saddr;
7977 
7978 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7979 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7980 			    &v4saddr, NULL, &ipif_flags) != 0) {
7981 				v4saddr = INADDR_ANY;
7982 				ipif_flags = 0;
7983 			}
7984 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7985 		} else {
7986 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7987 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7988 			    saddr, NULL, &ipif_flags) != 0) {
7989 				*saddr = ipv6_all_zeros;
7990 				ipif_flags = 0;
7991 			}
7992 		}
7993 
7994 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7995 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7996 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7997 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7998 		ire_refrele(ire);
7999 		ill_refrele(dst_ill);
8000 	}
8001 	miocack(q, mp, iocp->ioc_count, 0);
8002 }
8003 
8004 /*
8005  * Check if this is an address assigned to this machine.
8006  * Skips interfaces that are down by using ire checks.
8007  * Translates mapped addresses to v4 addresses and then
8008  * treats them as such, returning true if the v4 address
8009  * associated with this mapped address is configured.
8010  * Note: Applications will have to be careful what they do
8011  * with the response; use of mapped addresses limits
8012  * what can be done with the socket, especially with
8013  * respect to socket options and ioctls - neither IPv4
8014  * options nor IPv6 sticky options/ancillary data options
8015  * may be used.
8016  */
8017 /* ARGSUSED */
8018 int
8019 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8020     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8021 {
8022 	struct sioc_addrreq *sia;
8023 	sin_t *sin;
8024 	ire_t *ire;
8025 	mblk_t *mp1;
8026 	zoneid_t zoneid;
8027 	ip_stack_t	*ipst;
8028 
8029 	ip1dbg(("ip_sioctl_tmyaddr"));
8030 
8031 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8032 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8033 	ipst = CONNQ_TO_IPST(q);
8034 
8035 	/* Existence verified in ip_wput_nondata */
8036 	mp1 = mp->b_cont->b_cont;
8037 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8038 	sin = (sin_t *)&sia->sa_addr;
8039 	switch (sin->sin_family) {
8040 	case AF_INET6: {
8041 		sin6_t *sin6 = (sin6_t *)sin;
8042 
8043 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8044 			ipaddr_t v4_addr;
8045 
8046 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8047 			    v4_addr);
8048 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8049 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8050 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8051 		} else {
8052 			in6_addr_t v6addr;
8053 
8054 			v6addr = sin6->sin6_addr;
8055 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8056 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8057 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8058 		}
8059 		break;
8060 	}
8061 	case AF_INET: {
8062 		ipaddr_t v4addr;
8063 
8064 		v4addr = sin->sin_addr.s_addr;
8065 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8066 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8067 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8068 		break;
8069 	}
8070 	default:
8071 		return (EAFNOSUPPORT);
8072 	}
8073 	if (ire != NULL) {
8074 		sia->sa_res = 1;
8075 		ire_refrele(ire);
8076 	} else {
8077 		sia->sa_res = 0;
8078 	}
8079 	return (0);
8080 }
8081 
8082 /*
8083  * Check if this is an address assigned on-link i.e. neighbor,
8084  * and makes sure it's reachable from the current zone.
8085  * Returns true for my addresses as well.
8086  * Translates mapped addresses to v4 addresses and then
8087  * treats them as such, returning true if the v4 address
8088  * associated with this mapped address is configured.
8089  * Note: Applications will have to be careful what they do
8090  * with the response; use of mapped addresses limits
8091  * what can be done with the socket, especially with
8092  * respect to socket options and ioctls - neither IPv4
8093  * options nor IPv6 sticky options/ancillary data options
8094  * may be used.
8095  */
8096 /* ARGSUSED */
8097 int
8098 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8099     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8100 {
8101 	struct sioc_addrreq *sia;
8102 	sin_t *sin;
8103 	mblk_t	*mp1;
8104 	ire_t *ire = NULL;
8105 	zoneid_t zoneid;
8106 	ip_stack_t	*ipst;
8107 
8108 	ip1dbg(("ip_sioctl_tonlink"));
8109 
8110 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8111 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8112 	ipst = CONNQ_TO_IPST(q);
8113 
8114 	/* Existence verified in ip_wput_nondata */
8115 	mp1 = mp->b_cont->b_cont;
8116 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8117 	sin = (sin_t *)&sia->sa_addr;
8118 
8119 	/*
8120 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8121 	 * to make sure we only look at on-link unicast address.
8122 	 */
8123 	switch (sin->sin_family) {
8124 	case AF_INET6: {
8125 		sin6_t *sin6 = (sin6_t *)sin;
8126 
8127 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8128 			ipaddr_t v4_addr;
8129 
8130 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8131 			    v4_addr);
8132 			if (!CLASSD(v4_addr)) {
8133 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8134 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8135 				    0, ipst, NULL);
8136 			}
8137 		} else {
8138 			in6_addr_t v6addr;
8139 
8140 			v6addr = sin6->sin6_addr;
8141 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8142 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8143 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8144 				    ipst, NULL);
8145 			}
8146 		}
8147 		break;
8148 	}
8149 	case AF_INET: {
8150 		ipaddr_t v4addr;
8151 
8152 		v4addr = sin->sin_addr.s_addr;
8153 		if (!CLASSD(v4addr)) {
8154 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8155 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8156 		}
8157 		break;
8158 	}
8159 	default:
8160 		return (EAFNOSUPPORT);
8161 	}
8162 	sia->sa_res = 0;
8163 	if (ire != NULL) {
8164 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8165 
8166 		if ((ire->ire_type & IRE_ONLINK) &&
8167 		    !(ire->ire_type & IRE_BROADCAST))
8168 			sia->sa_res = 1;
8169 		ire_refrele(ire);
8170 	}
8171 	return (0);
8172 }
8173 
8174 /*
8175  * TBD: implement when kernel maintaines a list of site prefixes.
8176  */
8177 /* ARGSUSED */
8178 int
8179 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8180     ip_ioctl_cmd_t *ipip, void *ifreq)
8181 {
8182 	return (ENXIO);
8183 }
8184 
8185 /* ARP IOCTLs. */
8186 /* ARGSUSED */
8187 int
8188 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8189     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8190 {
8191 	int		err;
8192 	ipaddr_t	ipaddr;
8193 	struct iocblk	*iocp;
8194 	conn_t		*connp;
8195 	struct arpreq	*ar;
8196 	struct xarpreq	*xar;
8197 	int		arp_flags, flags, alength;
8198 	uchar_t		*lladdr;
8199 	ip_stack_t	*ipst;
8200 	ill_t		*ill = ipif->ipif_ill;
8201 	ill_t		*proxy_ill = NULL;
8202 	ipmp_arpent_t	*entp = NULL;
8203 	boolean_t	proxyarp = B_FALSE;
8204 	boolean_t	if_arp_ioctl = B_FALSE;
8205 	ncec_t		*ncec = NULL;
8206 	nce_t		*nce;
8207 
8208 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8209 	connp = Q_TO_CONN(q);
8210 	ipst = connp->conn_netstack->netstack_ip;
8211 	iocp = (struct iocblk *)mp->b_rptr;
8212 
8213 	if (ipip->ipi_cmd_type == XARP_CMD) {
8214 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8215 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8216 		ar = NULL;
8217 
8218 		arp_flags = xar->xarp_flags;
8219 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8220 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8221 		/*
8222 		 * Validate against user's link layer address length
8223 		 * input and name and addr length limits.
8224 		 */
8225 		alength = ill->ill_phys_addr_length;
8226 		if (ipip->ipi_cmd == SIOCSXARP) {
8227 			if (alength != xar->xarp_ha.sdl_alen ||
8228 			    (alength + xar->xarp_ha.sdl_nlen >
8229 			    sizeof (xar->xarp_ha.sdl_data)))
8230 				return (EINVAL);
8231 		}
8232 	} else {
8233 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8234 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8235 		xar = NULL;
8236 
8237 		arp_flags = ar->arp_flags;
8238 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8239 		/*
8240 		 * Theoretically, the sa_family could tell us what link
8241 		 * layer type this operation is trying to deal with. By
8242 		 * common usage AF_UNSPEC means ethernet. We'll assume
8243 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8244 		 * for now. Our new SIOC*XARP ioctls can be used more
8245 		 * generally.
8246 		 *
8247 		 * If the underlying media happens to have a non 6 byte
8248 		 * address, arp module will fail set/get, but the del
8249 		 * operation will succeed.
8250 		 */
8251 		alength = 6;
8252 		if ((ipip->ipi_cmd != SIOCDARP) &&
8253 		    (alength != ill->ill_phys_addr_length)) {
8254 			return (EINVAL);
8255 		}
8256 	}
8257 
8258 	/* Translate ATF* flags to NCE* flags */
8259 	flags = 0;
8260 	if (arp_flags & ATF_AUTHORITY)
8261 		flags |= NCE_F_AUTHORITY;
8262 	if (arp_flags & ATF_PERM)
8263 		flags |= NCE_F_NONUD; /* not subject to aging */
8264 	if (arp_flags & ATF_PUBL)
8265 		flags |= NCE_F_PUBLISH;
8266 
8267 	/*
8268 	 * IPMP ARP special handling:
8269 	 *
8270 	 * 1. Since ARP mappings must appear consistent across the group,
8271 	 *    prohibit changing ARP mappings on the underlying interfaces.
8272 	 *
8273 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8274 	 *    IP itself, prohibit changing them.
8275 	 *
8276 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8277 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8278 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8279 	 */
8280 	if (IS_UNDER_IPMP(ill)) {
8281 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8282 			return (EPERM);
8283 	}
8284 	if (IS_IPMP(ill)) {
8285 		ipmp_illgrp_t *illg = ill->ill_grp;
8286 
8287 		switch (ipip->ipi_cmd) {
8288 		case SIOCSARP:
8289 		case SIOCSXARP:
8290 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8291 			if (proxy_ill != NULL) {
8292 				proxyarp = B_TRUE;
8293 				if (!ipmp_ill_is_active(proxy_ill))
8294 					proxy_ill = ipmp_illgrp_next_ill(illg);
8295 				if (proxy_ill != NULL)
8296 					lladdr = proxy_ill->ill_phys_addr;
8297 			}
8298 			/* FALLTHRU */
8299 		}
8300 	}
8301 
8302 	ipaddr = sin->sin_addr.s_addr;
8303 	/*
8304 	 * don't match across illgrp per case (1) and (2).
8305 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8306 	 */
8307 	nce = nce_lookup_v4(ill, &ipaddr);
8308 	if (nce != NULL)
8309 		ncec = nce->nce_common;
8310 
8311 	switch (iocp->ioc_cmd) {
8312 	case SIOCDARP:
8313 	case SIOCDXARP: {
8314 		/*
8315 		 * Delete the NCE if any.
8316 		 */
8317 		if (ncec == NULL) {
8318 			iocp->ioc_error = ENXIO;
8319 			break;
8320 		}
8321 		/* Don't allow changes to arp mappings of local addresses. */
8322 		if (NCE_MYADDR(ncec)) {
8323 			nce_refrele(nce);
8324 			return (ENOTSUP);
8325 		}
8326 		iocp->ioc_error = 0;
8327 
8328 		/*
8329 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8330 		 * This will delete all the nce entries on the under_ills.
8331 		 */
8332 		ncec_delete(ncec);
8333 		/*
8334 		 * Once the NCE has been deleted, then the ire_dep* consistency
8335 		 * mechanism will find any IRE which depended on the now
8336 		 * condemned NCE (as part of sending packets).
8337 		 * That mechanism handles redirects by deleting redirects
8338 		 * that refer to UNREACHABLE nces.
8339 		 */
8340 		break;
8341 	}
8342 	case SIOCGARP:
8343 	case SIOCGXARP:
8344 		if (ncec != NULL) {
8345 			lladdr = ncec->ncec_lladdr;
8346 			flags = ncec->ncec_flags;
8347 			iocp->ioc_error = 0;
8348 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8349 		} else {
8350 			iocp->ioc_error = ENXIO;
8351 		}
8352 		break;
8353 	case SIOCSARP:
8354 	case SIOCSXARP:
8355 		/* Don't allow changes to arp mappings of local addresses. */
8356 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8357 			nce_refrele(nce);
8358 			return (ENOTSUP);
8359 		}
8360 
8361 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8362 		flags |= NCE_F_STATIC;
8363 		if (!if_arp_ioctl) {
8364 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8365 			    lladdr, alength, flags);
8366 		} else {
8367 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8368 			if (ipif != NULL) {
8369 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8370 				    lladdr, alength, flags);
8371 				ipif_refrele(ipif);
8372 			}
8373 		}
8374 		if (nce != NULL) {
8375 			nce_refrele(nce);
8376 			nce = NULL;
8377 		}
8378 		/*
8379 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8380 		 * by nce_add_common()
8381 		 */
8382 		err = nce_lookup_then_add_v4(ill, lladdr,
8383 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8384 		    &nce);
8385 		if (err == EEXIST) {
8386 			ncec = nce->nce_common;
8387 			mutex_enter(&ncec->ncec_lock);
8388 			ncec->ncec_state = ND_REACHABLE;
8389 			ncec->ncec_flags = flags;
8390 			nce_update(ncec, ND_UNCHANGED, lladdr);
8391 			mutex_exit(&ncec->ncec_lock);
8392 			err = 0;
8393 		}
8394 		if (nce != NULL) {
8395 			nce_refrele(nce);
8396 			nce = NULL;
8397 		}
8398 		if (IS_IPMP(ill) && err == 0) {
8399 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8400 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8401 			    flags);
8402 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8403 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8404 				break;
8405 			}
8406 		}
8407 		iocp->ioc_error = err;
8408 	}
8409 
8410 	if (nce != NULL) {
8411 		nce_refrele(nce);
8412 	}
8413 
8414 	/*
8415 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8416 	 */
8417 	if (entp != NULL)
8418 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8419 
8420 	return (iocp->ioc_error);
8421 }
8422 
8423 /*
8424  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8425  * the associated sin and refhold and return the associated ipif via `ci'.
8426  */
8427 int
8428 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8429     cmd_info_t *ci)
8430 {
8431 	mblk_t	*mp1;
8432 	sin_t	*sin;
8433 	conn_t	*connp;
8434 	ipif_t	*ipif;
8435 	ire_t	*ire = NULL;
8436 	ill_t	*ill = NULL;
8437 	boolean_t exists;
8438 	ip_stack_t *ipst;
8439 	struct arpreq *ar;
8440 	struct xarpreq *xar;
8441 	struct sockaddr_dl *sdl;
8442 
8443 	/* ioctl comes down on a conn */
8444 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8445 	connp = Q_TO_CONN(q);
8446 	if (connp->conn_family == AF_INET6)
8447 		return (ENXIO);
8448 
8449 	ipst = connp->conn_netstack->netstack_ip;
8450 
8451 	/* Verified in ip_wput_nondata */
8452 	mp1 = mp->b_cont->b_cont;
8453 
8454 	if (ipip->ipi_cmd_type == XARP_CMD) {
8455 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8456 		xar = (struct xarpreq *)mp1->b_rptr;
8457 		sin = (sin_t *)&xar->xarp_pa;
8458 		sdl = &xar->xarp_ha;
8459 
8460 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8461 			return (ENXIO);
8462 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8463 			return (EINVAL);
8464 	} else {
8465 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8466 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8467 		ar = (struct arpreq *)mp1->b_rptr;
8468 		sin = (sin_t *)&ar->arp_pa;
8469 	}
8470 
8471 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8472 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8473 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8474 		if (ipif == NULL)
8475 			return (ENXIO);
8476 		if (ipif->ipif_id != 0) {
8477 			ipif_refrele(ipif);
8478 			return (ENXIO);
8479 		}
8480 	} else {
8481 		/*
8482 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8483 		 * of 0: use the IP address to find the ipif.  If the IP
8484 		 * address is an IPMP test address, ire_ftable_lookup() will
8485 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8486 		 */
8487 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8488 		    ipst);
8489 		if (ipif == NULL) {
8490 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8491 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8492 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8493 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8494 				if (ire != NULL)
8495 					ire_refrele(ire);
8496 				return (ENXIO);
8497 			}
8498 			ASSERT(ire != NULL && ill != NULL);
8499 			ipif = ill->ill_ipif;
8500 			ipif_refhold(ipif);
8501 			ire_refrele(ire);
8502 		}
8503 	}
8504 
8505 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8506 		ipif_refrele(ipif);
8507 		return (ENXIO);
8508 	}
8509 
8510 	ci->ci_sin = sin;
8511 	ci->ci_ipif = ipif;
8512 	return (0);
8513 }
8514 
8515 /*
8516  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8517  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8518  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8519  * up and thus an ill can join that illgrp.
8520  *
8521  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8522  * open()/close() primarily because close() is not allowed to fail or block
8523  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8524  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8525  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8526  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8527  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8528  * state if I_UNLINK didn't occur.
8529  *
8530  * Note that for each plumb/unplumb operation, we may end up here more than
8531  * once because of the way ifconfig works.  However, it's OK to link the same
8532  * illgrp more than once, or unlink an illgrp that's already unlinked.
8533  */
8534 static int
8535 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8536 {
8537 	int err;
8538 	ip_stack_t *ipst = ill->ill_ipst;
8539 
8540 	ASSERT(IS_IPMP(ill));
8541 	ASSERT(IAM_WRITER_ILL(ill));
8542 
8543 	switch (ioccmd) {
8544 	case I_LINK:
8545 		return (ENOTSUP);
8546 
8547 	case I_PLINK:
8548 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8549 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8550 		rw_exit(&ipst->ips_ipmp_lock);
8551 		break;
8552 
8553 	case I_PUNLINK:
8554 		/*
8555 		 * Require all UP ipifs be brought down prior to unlinking the
8556 		 * illgrp so any associated IREs (and other state) is torched.
8557 		 */
8558 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8559 			return (EBUSY);
8560 
8561 		/*
8562 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8563 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8564 		 * join this group.  Specifically: ills trying to join grab
8565 		 * ipmp_lock and bump a "pending join" counter checked by
8566 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8567 		 * joins can occur (since we have ipmp_lock).  Once we drop
8568 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8569 		 * find the illgrp (since we unlinked it) and will return
8570 		 * EAFNOSUPPORT.  This will then take them back through the
8571 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8572 		 * back through I_PLINK above.
8573 		 */
8574 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8575 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8576 		rw_exit(&ipst->ips_ipmp_lock);
8577 		return (err);
8578 	default:
8579 		break;
8580 	}
8581 	return (0);
8582 }
8583 
8584 /*
8585  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8586  * atomically set/clear the muxids. Also complete the ioctl by acking or
8587  * naking it.  Note that the code is structured such that the link type,
8588  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8589  * its clones use the persistent link, while pppd(1M) and perhaps many
8590  * other daemons may use non-persistent link.  When combined with some
8591  * ill_t states, linking and unlinking lower streams may be used as
8592  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8593  */
8594 /* ARGSUSED */
8595 void
8596 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8597 {
8598 	mblk_t		*mp1;
8599 	struct linkblk	*li;
8600 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8601 	int		err = 0;
8602 
8603 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8604 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8605 
8606 	mp1 = mp->b_cont;	/* This is the linkblk info */
8607 	li = (struct linkblk *)mp1->b_rptr;
8608 
8609 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8610 	if (err == EINPROGRESS)
8611 		return;
8612 done:
8613 	if (err == 0)
8614 		miocack(q, mp, 0, 0);
8615 	else
8616 		miocnak(q, mp, 0, err);
8617 
8618 	/* Conn was refheld in ip_sioctl_copyin_setup */
8619 	if (CONN_Q(q))
8620 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8621 }
8622 
8623 /*
8624  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8625  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8626  * module stream).  If `doconsist' is set, then do the extended consistency
8627  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8628  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8629  * an error code on failure.
8630  */
8631 static int
8632 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8633     struct linkblk *li)
8634 {
8635 	int		err = 0;
8636 	ill_t  		*ill;
8637 	queue_t		*ipwq, *dwq;
8638 	const char	*name;
8639 	struct qinit	*qinfo;
8640 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8641 	boolean_t	entered_ipsq = B_FALSE;
8642 	boolean_t	is_ip = B_FALSE;
8643 	arl_t		*arl;
8644 
8645 	/*
8646 	 * Walk the lower stream to verify it's the IP module stream.
8647 	 * The IP module is identified by its name, wput function,
8648 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8649 	 * (li->l_qbot) will not vanish until this ioctl completes.
8650 	 */
8651 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8652 		qinfo = ipwq->q_qinfo;
8653 		name = qinfo->qi_minfo->mi_idname;
8654 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8655 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8656 			is_ip = B_TRUE;
8657 			break;
8658 		}
8659 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8660 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8661 			break;
8662 		}
8663 	}
8664 
8665 	/*
8666 	 * If this isn't an IP module stream, bail.
8667 	 */
8668 	if (ipwq == NULL)
8669 		return (0);
8670 
8671 	if (!is_ip) {
8672 		arl = (arl_t *)ipwq->q_ptr;
8673 		ill = arl_to_ill(arl);
8674 		if (ill == NULL)
8675 			return (0);
8676 	} else {
8677 		ill = ipwq->q_ptr;
8678 	}
8679 	ASSERT(ill != NULL);
8680 
8681 	if (ipsq == NULL) {
8682 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8683 		    NEW_OP, B_FALSE);
8684 		if (ipsq == NULL) {
8685 			if (!is_ip)
8686 				ill_refrele(ill);
8687 			return (EINPROGRESS);
8688 		}
8689 		entered_ipsq = B_TRUE;
8690 	}
8691 	ASSERT(IAM_WRITER_ILL(ill));
8692 	mutex_enter(&ill->ill_lock);
8693 	if (!is_ip) {
8694 		if (islink && ill->ill_muxid == 0) {
8695 			/*
8696 			 * Plumbing has to be done with IP plumbed first, arp
8697 			 * second, but here we have arp being plumbed first.
8698 			 */
8699 			mutex_exit(&ill->ill_lock);
8700 			ipsq_exit(ipsq);
8701 			ill_refrele(ill);
8702 			return (EINVAL);
8703 		}
8704 	}
8705 	mutex_exit(&ill->ill_lock);
8706 	if (!is_ip) {
8707 		arl->arl_muxid = islink ? li->l_index : 0;
8708 		ill_refrele(ill);
8709 		goto done;
8710 	}
8711 
8712 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8713 		goto done;
8714 
8715 	/*
8716 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8717 	 * the read queue of the module immediately below IP in the ill.
8718 	 * These are used during the capability negotiation below.
8719 	 */
8720 	ill->ill_lmod_rq = NULL;
8721 	ill->ill_lmod_cnt = 0;
8722 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8723 		ill->ill_lmod_rq = RD(dwq);
8724 		for (; dwq != NULL; dwq = dwq->q_next)
8725 			ill->ill_lmod_cnt++;
8726 	}
8727 
8728 	ill->ill_muxid = islink ? li->l_index : 0;
8729 
8730 	/*
8731 	 * Mark the ipsq busy until the capability operations initiated below
8732 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8733 	 * returns, but the capability operation may complete asynchronously
8734 	 * much later.
8735 	 */
8736 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8737 	/*
8738 	 * If there's at least one up ipif on this ill, then we're bound to
8739 	 * the underlying driver via DLPI.  In that case, renegotiate
8740 	 * capabilities to account for any possible change in modules
8741 	 * interposed between IP and the driver.
8742 	 */
8743 	if (ill->ill_ipif_up_count > 0) {
8744 		if (islink)
8745 			ill_capability_probe(ill);
8746 		else
8747 			ill_capability_reset(ill, B_FALSE);
8748 	}
8749 	ipsq_current_finish(ipsq);
8750 done:
8751 	if (entered_ipsq)
8752 		ipsq_exit(ipsq);
8753 
8754 	return (err);
8755 }
8756 
8757 /*
8758  * Search the ioctl command in the ioctl tables and return a pointer
8759  * to the ioctl command information. The ioctl command tables are
8760  * static and fully populated at compile time.
8761  */
8762 ip_ioctl_cmd_t *
8763 ip_sioctl_lookup(int ioc_cmd)
8764 {
8765 	int index;
8766 	ip_ioctl_cmd_t *ipip;
8767 	ip_ioctl_cmd_t *ipip_end;
8768 
8769 	if (ioc_cmd == IPI_DONTCARE)
8770 		return (NULL);
8771 
8772 	/*
8773 	 * Do a 2 step search. First search the indexed table
8774 	 * based on the least significant byte of the ioctl cmd.
8775 	 * If we don't find a match, then search the misc table
8776 	 * serially.
8777 	 */
8778 	index = ioc_cmd & 0xFF;
8779 	if (index < ip_ndx_ioctl_count) {
8780 		ipip = &ip_ndx_ioctl_table[index];
8781 		if (ipip->ipi_cmd == ioc_cmd) {
8782 			/* Found a match in the ndx table */
8783 			return (ipip);
8784 		}
8785 	}
8786 
8787 	/* Search the misc table */
8788 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8789 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8790 		if (ipip->ipi_cmd == ioc_cmd)
8791 			/* Found a match in the misc table */
8792 			return (ipip);
8793 	}
8794 
8795 	return (NULL);
8796 }
8797 
8798 /*
8799  * Wrapper function for resuming deferred ioctl processing
8800  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8801  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8802  */
8803 /* ARGSUSED */
8804 void
8805 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8806     void *dummy_arg)
8807 {
8808 	ip_sioctl_copyin_setup(q, mp);
8809 }
8810 
8811 /*
8812  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8813  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8814  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8815  * We establish here the size of the block to be copied in.  mi_copyin
8816  * arranges for this to happen, an processing continues in ip_wput_nondata with
8817  * an M_IOCDATA message.
8818  */
8819 void
8820 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8821 {
8822 	int	copyin_size;
8823 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8824 	ip_ioctl_cmd_t *ipip;
8825 	cred_t *cr;
8826 	ip_stack_t	*ipst;
8827 
8828 	if (CONN_Q(q))
8829 		ipst = CONNQ_TO_IPST(q);
8830 	else
8831 		ipst = ILLQ_TO_IPST(q);
8832 
8833 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8834 	if (ipip == NULL) {
8835 		/*
8836 		 * The ioctl is not one we understand or own.
8837 		 * Pass it along to be processed down stream,
8838 		 * if this is a module instance of IP, else nak
8839 		 * the ioctl.
8840 		 */
8841 		if (q->q_next == NULL) {
8842 			goto nak;
8843 		} else {
8844 			putnext(q, mp);
8845 			return;
8846 		}
8847 	}
8848 
8849 	/*
8850 	 * If this is deferred, then we will do all the checks when we
8851 	 * come back.
8852 	 */
8853 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8854 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8855 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8856 		return;
8857 	}
8858 
8859 	/*
8860 	 * Only allow a very small subset of IP ioctls on this stream if
8861 	 * IP is a module and not a driver. Allowing ioctls to be processed
8862 	 * in this case may cause assert failures or data corruption.
8863 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8864 	 * ioctls allowed on an IP module stream, after which this stream
8865 	 * normally becomes a multiplexor (at which time the stream head
8866 	 * will fail all ioctls).
8867 	 */
8868 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8869 		goto nak;
8870 	}
8871 
8872 	/* Make sure we have ioctl data to process. */
8873 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8874 		goto nak;
8875 
8876 	/*
8877 	 * Prefer dblk credential over ioctl credential; some synthesized
8878 	 * ioctls have kcred set because there's no way to crhold()
8879 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8880 	 * the framework; the caller of ioctl needs to hold the reference
8881 	 * for the duration of the call).
8882 	 */
8883 	cr = msg_getcred(mp, NULL);
8884 	if (cr == NULL)
8885 		cr = iocp->ioc_cr;
8886 
8887 	/* Make sure normal users don't send down privileged ioctls */
8888 	if ((ipip->ipi_flags & IPI_PRIV) &&
8889 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8890 		/* We checked the privilege earlier but log it here */
8891 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8892 		return;
8893 	}
8894 
8895 	/*
8896 	 * The ioctl command tables can only encode fixed length
8897 	 * ioctl data. If the length is variable, the table will
8898 	 * encode the length as zero. Such special cases are handled
8899 	 * below in the switch.
8900 	 */
8901 	if (ipip->ipi_copyin_size != 0) {
8902 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8903 		return;
8904 	}
8905 
8906 	switch (iocp->ioc_cmd) {
8907 	case O_SIOCGIFCONF:
8908 	case SIOCGIFCONF:
8909 		/*
8910 		 * This IOCTL is hilarious.  See comments in
8911 		 * ip_sioctl_get_ifconf for the story.
8912 		 */
8913 		if (iocp->ioc_count == TRANSPARENT)
8914 			copyin_size = SIZEOF_STRUCT(ifconf,
8915 			    iocp->ioc_flag);
8916 		else
8917 			copyin_size = iocp->ioc_count;
8918 		mi_copyin(q, mp, NULL, copyin_size);
8919 		return;
8920 
8921 	case O_SIOCGLIFCONF:
8922 	case SIOCGLIFCONF:
8923 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8924 		mi_copyin(q, mp, NULL, copyin_size);
8925 		return;
8926 
8927 	case SIOCGLIFSRCOF:
8928 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8929 		mi_copyin(q, mp, NULL, copyin_size);
8930 		return;
8931 	case SIOCGIP6ADDRPOLICY:
8932 		ip_sioctl_ip6addrpolicy(q, mp);
8933 		ip6_asp_table_refrele(ipst);
8934 		return;
8935 
8936 	case SIOCSIP6ADDRPOLICY:
8937 		ip_sioctl_ip6addrpolicy(q, mp);
8938 		return;
8939 
8940 	case SIOCGDSTINFO:
8941 		ip_sioctl_dstinfo(q, mp);
8942 		ip6_asp_table_refrele(ipst);
8943 		return;
8944 
8945 	case I_PLINK:
8946 	case I_PUNLINK:
8947 	case I_LINK:
8948 	case I_UNLINK:
8949 		/*
8950 		 * We treat non-persistent link similarly as the persistent
8951 		 * link case, in terms of plumbing/unplumbing, as well as
8952 		 * dynamic re-plumbing events indicator.  See comments
8953 		 * in ip_sioctl_plink() for more.
8954 		 *
8955 		 * Request can be enqueued in the 'ipsq' while waiting
8956 		 * to become exclusive. So bump up the conn ref.
8957 		 */
8958 		if (CONN_Q(q))
8959 			CONN_INC_REF(Q_TO_CONN(q));
8960 		ip_sioctl_plink(NULL, q, mp, NULL);
8961 		return;
8962 
8963 	case ND_GET:
8964 	case ND_SET:
8965 		/*
8966 		 * Use of the nd table requires holding the reader lock.
8967 		 * Modifying the nd table thru nd_load/nd_unload requires
8968 		 * the writer lock.
8969 		 */
8970 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8971 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8972 			rw_exit(&ipst->ips_ip_g_nd_lock);
8973 
8974 			if (iocp->ioc_error)
8975 				iocp->ioc_count = 0;
8976 			mp->b_datap->db_type = M_IOCACK;
8977 			qreply(q, mp);
8978 			return;
8979 		}
8980 		rw_exit(&ipst->ips_ip_g_nd_lock);
8981 		/*
8982 		 * We don't understand this subioctl of ND_GET / ND_SET.
8983 		 * Maybe intended for some driver / module below us
8984 		 */
8985 		if (q->q_next) {
8986 			putnext(q, mp);
8987 		} else {
8988 			iocp->ioc_error = ENOENT;
8989 			mp->b_datap->db_type = M_IOCNAK;
8990 			iocp->ioc_count = 0;
8991 			qreply(q, mp);
8992 		}
8993 		return;
8994 
8995 	case IP_IOCTL:
8996 		ip_wput_ioctl(q, mp);
8997 		return;
8998 
8999 	case SIOCILB:
9000 		/* The ioctl length varies depending on the ILB command. */
9001 		copyin_size = iocp->ioc_count;
9002 		if (copyin_size < sizeof (ilb_cmd_t))
9003 			goto nak;
9004 		mi_copyin(q, mp, NULL, copyin_size);
9005 		return;
9006 
9007 	default:
9008 		cmn_err(CE_PANIC, "should not happen ");
9009 	}
9010 nak:
9011 	if (mp->b_cont != NULL) {
9012 		freemsg(mp->b_cont);
9013 		mp->b_cont = NULL;
9014 	}
9015 	iocp->ioc_error = EINVAL;
9016 	mp->b_datap->db_type = M_IOCNAK;
9017 	iocp->ioc_count = 0;
9018 	qreply(q, mp);
9019 }
9020 
9021 static void
9022 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9023 {
9024 	struct arpreq *ar;
9025 	struct xarpreq *xar;
9026 	mblk_t	*tmp;
9027 	struct iocblk *iocp;
9028 	int x_arp_ioctl = B_FALSE;
9029 	int *flagsp;
9030 	char *storage = NULL;
9031 
9032 	ASSERT(ill != NULL);
9033 
9034 	iocp = (struct iocblk *)mp->b_rptr;
9035 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9036 
9037 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9038 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9039 	    (iocp->ioc_cmd == SIOCSXARP)) {
9040 		x_arp_ioctl = B_TRUE;
9041 		xar = (struct xarpreq *)tmp->b_rptr;
9042 		flagsp = &xar->xarp_flags;
9043 		storage = xar->xarp_ha.sdl_data;
9044 	} else {
9045 		ar = (struct arpreq *)tmp->b_rptr;
9046 		flagsp = &ar->arp_flags;
9047 		storage = ar->arp_ha.sa_data;
9048 	}
9049 
9050 	/*
9051 	 * We're done if this is not an SIOCG{X}ARP
9052 	 */
9053 	if (x_arp_ioctl) {
9054 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9055 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9056 		    sizeof (xar->xarp_ha.sdl_data)) {
9057 			iocp->ioc_error = EINVAL;
9058 			return;
9059 		}
9060 	}
9061 	*flagsp = ATF_INUSE;
9062 	/*
9063 	 * If /sbin/arp told us we are the authority using the "permanent"
9064 	 * flag, or if this is one of my addresses print "permanent"
9065 	 * in the /sbin/arp output.
9066 	 */
9067 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9068 		*flagsp |= ATF_AUTHORITY;
9069 	if (flags & NCE_F_NONUD)
9070 		*flagsp |= ATF_PERM; /* not subject to aging */
9071 	if (flags & NCE_F_PUBLISH)
9072 		*flagsp |= ATF_PUBL;
9073 	if (hwaddr != NULL) {
9074 		*flagsp |= ATF_COM;
9075 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9076 	}
9077 }
9078 
9079 /*
9080  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9081  * interface) create the next available logical interface for this
9082  * physical interface.
9083  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9084  * ipif with the specified name.
9085  *
9086  * If the address family is not AF_UNSPEC then set the address as well.
9087  *
9088  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9089  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9090  *
9091  * Executed as a writer on the ill.
9092  * So no lock is needed to traverse the ipif chain, or examine the
9093  * phyint flags.
9094  */
9095 /* ARGSUSED */
9096 int
9097 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9098     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9099 {
9100 	mblk_t	*mp1;
9101 	struct lifreq *lifr;
9102 	boolean_t	isv6;
9103 	boolean_t	exists;
9104 	char 	*name;
9105 	char	*endp;
9106 	char	*cp;
9107 	int	namelen;
9108 	ipif_t	*ipif;
9109 	long	id;
9110 	ipsq_t	*ipsq;
9111 	ill_t	*ill;
9112 	sin_t	*sin;
9113 	int	err = 0;
9114 	boolean_t found_sep = B_FALSE;
9115 	conn_t	*connp;
9116 	zoneid_t zoneid;
9117 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9118 
9119 	ASSERT(q->q_next == NULL);
9120 	ip1dbg(("ip_sioctl_addif\n"));
9121 	/* Existence of mp1 has been checked in ip_wput_nondata */
9122 	mp1 = mp->b_cont->b_cont;
9123 	/*
9124 	 * Null terminate the string to protect against buffer
9125 	 * overrun. String was generated by user code and may not
9126 	 * be trusted.
9127 	 */
9128 	lifr = (struct lifreq *)mp1->b_rptr;
9129 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9130 	name = lifr->lifr_name;
9131 	ASSERT(CONN_Q(q));
9132 	connp = Q_TO_CONN(q);
9133 	isv6 = (connp->conn_family == AF_INET6);
9134 	zoneid = connp->conn_zoneid;
9135 	namelen = mi_strlen(name);
9136 	if (namelen == 0)
9137 		return (EINVAL);
9138 
9139 	exists = B_FALSE;
9140 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9141 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9142 		/*
9143 		 * Allow creating lo0 using SIOCLIFADDIF.
9144 		 * can't be any other writer thread. So can pass null below
9145 		 * for the last 4 args to ipif_lookup_name.
9146 		 */
9147 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9148 		    &exists, isv6, zoneid, ipst);
9149 		/* Prevent any further action */
9150 		if (ipif == NULL) {
9151 			return (ENOBUFS);
9152 		} else if (!exists) {
9153 			/* We created the ipif now and as writer */
9154 			ipif_refrele(ipif);
9155 			return (0);
9156 		} else {
9157 			ill = ipif->ipif_ill;
9158 			ill_refhold(ill);
9159 			ipif_refrele(ipif);
9160 		}
9161 	} else {
9162 		/* Look for a colon in the name. */
9163 		endp = &name[namelen];
9164 		for (cp = endp; --cp > name; ) {
9165 			if (*cp == IPIF_SEPARATOR_CHAR) {
9166 				found_sep = B_TRUE;
9167 				/*
9168 				 * Reject any non-decimal aliases for plumbing
9169 				 * of logical interfaces. Aliases with leading
9170 				 * zeroes are also rejected as they introduce
9171 				 * ambiguity in the naming of the interfaces.
9172 				 * Comparing with "0" takes care of all such
9173 				 * cases.
9174 				 */
9175 				if ((strncmp("0", cp+1, 1)) == 0)
9176 					return (EINVAL);
9177 
9178 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9179 				    id <= 0 || *endp != '\0') {
9180 					return (EINVAL);
9181 				}
9182 				*cp = '\0';
9183 				break;
9184 			}
9185 		}
9186 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9187 		if (found_sep)
9188 			*cp = IPIF_SEPARATOR_CHAR;
9189 		if (ill == NULL)
9190 			return (ENXIO);
9191 	}
9192 
9193 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9194 	    B_TRUE);
9195 
9196 	/*
9197 	 * Release the refhold due to the lookup, now that we are excl
9198 	 * or we are just returning
9199 	 */
9200 	ill_refrele(ill);
9201 
9202 	if (ipsq == NULL)
9203 		return (EINPROGRESS);
9204 
9205 	/* We are now exclusive on the IPSQ */
9206 	ASSERT(IAM_WRITER_ILL(ill));
9207 
9208 	if (found_sep) {
9209 		/* Now see if there is an IPIF with this unit number. */
9210 		for (ipif = ill->ill_ipif; ipif != NULL;
9211 		    ipif = ipif->ipif_next) {
9212 			if (ipif->ipif_id == id) {
9213 				err = EEXIST;
9214 				goto done;
9215 			}
9216 		}
9217 	}
9218 
9219 	/*
9220 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9221 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9222 	 * instead.
9223 	 */
9224 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9225 	    B_TRUE, B_TRUE, &err)) == NULL) {
9226 		goto done;
9227 	}
9228 
9229 	/* Return created name with ioctl */
9230 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9231 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9232 	ip1dbg(("created %s\n", lifr->lifr_name));
9233 
9234 	/* Set address */
9235 	sin = (sin_t *)&lifr->lifr_addr;
9236 	if (sin->sin_family != AF_UNSPEC) {
9237 		err = ip_sioctl_addr(ipif, sin, q, mp,
9238 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9239 	}
9240 
9241 done:
9242 	ipsq_exit(ipsq);
9243 	return (err);
9244 }
9245 
9246 /*
9247  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9248  * interface) delete it based on the IP address (on this physical interface).
9249  * Otherwise delete it based on the ipif_id.
9250  * Also, special handling to allow a removeif of lo0.
9251  */
9252 /* ARGSUSED */
9253 int
9254 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9255     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9256 {
9257 	conn_t		*connp;
9258 	ill_t		*ill = ipif->ipif_ill;
9259 	boolean_t	 success;
9260 	ip_stack_t	*ipst;
9261 
9262 	ipst = CONNQ_TO_IPST(q);
9263 
9264 	ASSERT(q->q_next == NULL);
9265 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9266 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9267 	ASSERT(IAM_WRITER_IPIF(ipif));
9268 
9269 	connp = Q_TO_CONN(q);
9270 	/*
9271 	 * Special case for unplumbing lo0 (the loopback physical interface).
9272 	 * If unplumbing lo0, the incoming address structure has been
9273 	 * initialized to all zeros. When unplumbing lo0, all its logical
9274 	 * interfaces must be removed too.
9275 	 *
9276 	 * Note that this interface may be called to remove a specific
9277 	 * loopback logical interface (eg, lo0:1). But in that case
9278 	 * ipif->ipif_id != 0 so that the code path for that case is the
9279 	 * same as any other interface (meaning it skips the code directly
9280 	 * below).
9281 	 */
9282 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9283 		if (sin->sin_family == AF_UNSPEC &&
9284 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9285 			/*
9286 			 * Mark it condemned. No new ref. will be made to ill.
9287 			 */
9288 			mutex_enter(&ill->ill_lock);
9289 			ill->ill_state_flags |= ILL_CONDEMNED;
9290 			for (ipif = ill->ill_ipif; ipif != NULL;
9291 			    ipif = ipif->ipif_next) {
9292 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9293 			}
9294 			mutex_exit(&ill->ill_lock);
9295 
9296 			ipif = ill->ill_ipif;
9297 			/* unplumb the loopback interface */
9298 			ill_delete(ill);
9299 			mutex_enter(&connp->conn_lock);
9300 			mutex_enter(&ill->ill_lock);
9301 
9302 			/* Are any references to this ill active */
9303 			if (ill_is_freeable(ill)) {
9304 				mutex_exit(&ill->ill_lock);
9305 				mutex_exit(&connp->conn_lock);
9306 				ill_delete_tail(ill);
9307 				mi_free(ill);
9308 				return (0);
9309 			}
9310 			success = ipsq_pending_mp_add(connp, ipif,
9311 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9312 			mutex_exit(&connp->conn_lock);
9313 			mutex_exit(&ill->ill_lock);
9314 			if (success)
9315 				return (EINPROGRESS);
9316 			else
9317 				return (EINTR);
9318 		}
9319 	}
9320 
9321 	if (ipif->ipif_id == 0) {
9322 		ipsq_t *ipsq;
9323 
9324 		/* Find based on address */
9325 		if (ipif->ipif_isv6) {
9326 			sin6_t *sin6;
9327 
9328 			if (sin->sin_family != AF_INET6)
9329 				return (EAFNOSUPPORT);
9330 
9331 			sin6 = (sin6_t *)sin;
9332 			/* We are a writer, so we should be able to lookup */
9333 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9334 			    ipst);
9335 		} else {
9336 			if (sin->sin_family != AF_INET)
9337 				return (EAFNOSUPPORT);
9338 
9339 			/* We are a writer, so we should be able to lookup */
9340 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9341 			    ipst);
9342 		}
9343 		if (ipif == NULL) {
9344 			return (EADDRNOTAVAIL);
9345 		}
9346 
9347 		/*
9348 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9349 		 * lifr_name of the physical interface but with an ip address
9350 		 * lifr_addr of a logical interface plumbed over it.
9351 		 * So update ipx_current_ipif now that ipif points to the
9352 		 * correct one.
9353 		 */
9354 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9355 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9356 
9357 		/* This is a writer */
9358 		ipif_refrele(ipif);
9359 	}
9360 
9361 	/*
9362 	 * Can not delete instance zero since it is tied to the ill.
9363 	 */
9364 	if (ipif->ipif_id == 0)
9365 		return (EBUSY);
9366 
9367 	mutex_enter(&ill->ill_lock);
9368 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9369 	mutex_exit(&ill->ill_lock);
9370 
9371 	ipif_free(ipif);
9372 
9373 	mutex_enter(&connp->conn_lock);
9374 	mutex_enter(&ill->ill_lock);
9375 
9376 	/* Are any references to this ipif active */
9377 	if (ipif_is_freeable(ipif)) {
9378 		mutex_exit(&ill->ill_lock);
9379 		mutex_exit(&connp->conn_lock);
9380 		ipif_non_duplicate(ipif);
9381 		(void) ipif_down_tail(ipif);
9382 		ipif_free_tail(ipif); /* frees ipif */
9383 		return (0);
9384 	}
9385 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9386 	    IPIF_FREE);
9387 	mutex_exit(&ill->ill_lock);
9388 	mutex_exit(&connp->conn_lock);
9389 	if (success)
9390 		return (EINPROGRESS);
9391 	else
9392 		return (EINTR);
9393 }
9394 
9395 /*
9396  * Restart the removeif ioctl. The refcnt has gone down to 0.
9397  * The ipif is already condemned. So can't find it thru lookups.
9398  */
9399 /* ARGSUSED */
9400 int
9401 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9402     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9403 {
9404 	ill_t *ill = ipif->ipif_ill;
9405 
9406 	ASSERT(IAM_WRITER_IPIF(ipif));
9407 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9408 
9409 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9410 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9411 
9412 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9413 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9414 		ill_delete_tail(ill);
9415 		mi_free(ill);
9416 		return (0);
9417 	}
9418 
9419 	ipif_non_duplicate(ipif);
9420 	(void) ipif_down_tail(ipif);
9421 	ipif_free_tail(ipif);
9422 
9423 	return (0);
9424 }
9425 
9426 /*
9427  * Set the local interface address.
9428  * Allow an address of all zero when the interface is down.
9429  */
9430 /* ARGSUSED */
9431 int
9432 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9433     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9434 {
9435 	int err = 0;
9436 	in6_addr_t v6addr;
9437 	boolean_t need_up = B_FALSE;
9438 
9439 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9440 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9441 
9442 	ASSERT(IAM_WRITER_IPIF(ipif));
9443 
9444 	if (ipif->ipif_isv6) {
9445 		sin6_t *sin6;
9446 		ill_t *ill;
9447 		phyint_t *phyi;
9448 
9449 		if (sin->sin_family != AF_INET6)
9450 			return (EAFNOSUPPORT);
9451 
9452 		sin6 = (sin6_t *)sin;
9453 		v6addr = sin6->sin6_addr;
9454 		ill = ipif->ipif_ill;
9455 		phyi = ill->ill_phyint;
9456 
9457 		/*
9458 		 * Enforce that true multicast interfaces have a link-local
9459 		 * address for logical unit 0.
9460 		 */
9461 		if (ipif->ipif_id == 0 &&
9462 		    (ill->ill_flags & ILLF_MULTICAST) &&
9463 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9464 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9465 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9466 			return (EADDRNOTAVAIL);
9467 		}
9468 
9469 		/*
9470 		 * up interfaces shouldn't have the unspecified address
9471 		 * unless they also have the IPIF_NOLOCAL flags set and
9472 		 * have a subnet assigned.
9473 		 */
9474 		if ((ipif->ipif_flags & IPIF_UP) &&
9475 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9476 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9477 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9478 			return (EADDRNOTAVAIL);
9479 		}
9480 
9481 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9482 			return (EADDRNOTAVAIL);
9483 	} else {
9484 		ipaddr_t addr;
9485 
9486 		if (sin->sin_family != AF_INET)
9487 			return (EAFNOSUPPORT);
9488 
9489 		addr = sin->sin_addr.s_addr;
9490 
9491 		/* Allow 0 as the local address. */
9492 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9493 			return (EADDRNOTAVAIL);
9494 
9495 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9496 	}
9497 
9498 	/*
9499 	 * Even if there is no change we redo things just to rerun
9500 	 * ipif_set_default.
9501 	 */
9502 	if (ipif->ipif_flags & IPIF_UP) {
9503 		/*
9504 		 * Setting a new local address, make sure
9505 		 * we have net and subnet bcast ire's for
9506 		 * the old address if we need them.
9507 		 */
9508 		/*
9509 		 * If the interface is already marked up,
9510 		 * we call ipif_down which will take care
9511 		 * of ditching any IREs that have been set
9512 		 * up based on the old interface address.
9513 		 */
9514 		err = ipif_logical_down(ipif, q, mp);
9515 		if (err == EINPROGRESS)
9516 			return (err);
9517 		(void) ipif_down_tail(ipif);
9518 		need_up = 1;
9519 	}
9520 
9521 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9522 	return (err);
9523 }
9524 
9525 int
9526 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9527     boolean_t need_up)
9528 {
9529 	in6_addr_t v6addr;
9530 	in6_addr_t ov6addr;
9531 	ipaddr_t addr;
9532 	sin6_t	*sin6;
9533 	int	sinlen;
9534 	int	err = 0;
9535 	ill_t	*ill = ipif->ipif_ill;
9536 	boolean_t need_dl_down;
9537 	boolean_t need_arp_down;
9538 	struct iocblk *iocp;
9539 
9540 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9541 
9542 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9543 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9544 	ASSERT(IAM_WRITER_IPIF(ipif));
9545 
9546 	/* Must cancel any pending timer before taking the ill_lock */
9547 	if (ipif->ipif_recovery_id != 0)
9548 		(void) untimeout(ipif->ipif_recovery_id);
9549 	ipif->ipif_recovery_id = 0;
9550 
9551 	if (ipif->ipif_isv6) {
9552 		sin6 = (sin6_t *)sin;
9553 		v6addr = sin6->sin6_addr;
9554 		sinlen = sizeof (struct sockaddr_in6);
9555 	} else {
9556 		addr = sin->sin_addr.s_addr;
9557 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9558 		sinlen = sizeof (struct sockaddr_in);
9559 	}
9560 	mutex_enter(&ill->ill_lock);
9561 	ov6addr = ipif->ipif_v6lcl_addr;
9562 	ipif->ipif_v6lcl_addr = v6addr;
9563 	sctp_update_ipif_addr(ipif, ov6addr);
9564 	ipif->ipif_addr_ready = 0;
9565 
9566 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9567 
9568 	/*
9569 	 * If the interface was previously marked as a duplicate, then since
9570 	 * we've now got a "new" address, it should no longer be considered a
9571 	 * duplicate -- even if the "new" address is the same as the old one.
9572 	 * Note that if all ipifs are down, we may have a pending ARP down
9573 	 * event to handle.  This is because we want to recover from duplicates
9574 	 * and thus delay tearing down ARP until the duplicates have been
9575 	 * removed or disabled.
9576 	 */
9577 	need_dl_down = need_arp_down = B_FALSE;
9578 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9579 		need_arp_down = !need_up;
9580 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9581 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9582 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9583 			need_dl_down = B_TRUE;
9584 		}
9585 	}
9586 
9587 	ipif_set_default(ipif);
9588 
9589 	/*
9590 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9591 	 * tag the ill so that future updates to the interface ID don't result
9592 	 * in this address getting automatically reconfigured from under the
9593 	 * administrator.
9594 	 */
9595 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9596 		ill->ill_manual_linklocal = 1;
9597 
9598 	/*
9599 	 * When publishing an interface address change event, we only notify
9600 	 * the event listeners of the new address.  It is assumed that if they
9601 	 * actively care about the addresses assigned that they will have
9602 	 * already discovered the previous address assigned (if there was one.)
9603 	 *
9604 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9605 	 */
9606 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9607 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9608 		    NE_ADDRESS_CHANGE, sin, sinlen);
9609 	}
9610 
9611 	mutex_exit(&ill->ill_lock);
9612 
9613 	if (need_up) {
9614 		/*
9615 		 * Now bring the interface back up.  If this
9616 		 * is the only IPIF for the ILL, ipif_up
9617 		 * will have to re-bind to the device, so
9618 		 * we may get back EINPROGRESS, in which
9619 		 * case, this IOCTL will get completed in
9620 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9621 		 */
9622 		err = ipif_up(ipif, q, mp);
9623 	} else {
9624 		/* Perhaps ilgs should use this ill */
9625 		update_conn_ill(NULL, ill->ill_ipst);
9626 	}
9627 
9628 	if (need_dl_down)
9629 		ill_dl_down(ill);
9630 
9631 	if (need_arp_down && !ill->ill_isv6)
9632 		(void) ipif_arp_down(ipif);
9633 
9634 	/*
9635 	 * The default multicast interface might have changed (for
9636 	 * instance if the IPv6 scope of the address changed)
9637 	 */
9638 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9639 
9640 	return (err);
9641 }
9642 
9643 /*
9644  * Restart entry point to restart the address set operation after the
9645  * refcounts have dropped to zero.
9646  */
9647 /* ARGSUSED */
9648 int
9649 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9650     ip_ioctl_cmd_t *ipip, void *ifreq)
9651 {
9652 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9653 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9654 	ASSERT(IAM_WRITER_IPIF(ipif));
9655 	(void) ipif_down_tail(ipif);
9656 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9657 }
9658 
9659 /* ARGSUSED */
9660 int
9661 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9662     ip_ioctl_cmd_t *ipip, void *if_req)
9663 {
9664 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9665 	struct lifreq *lifr = (struct lifreq *)if_req;
9666 
9667 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9668 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9669 	/*
9670 	 * The net mask and address can't change since we have a
9671 	 * reference to the ipif. So no lock is necessary.
9672 	 */
9673 	if (ipif->ipif_isv6) {
9674 		*sin6 = sin6_null;
9675 		sin6->sin6_family = AF_INET6;
9676 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9677 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9678 		lifr->lifr_addrlen =
9679 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9680 	} else {
9681 		*sin = sin_null;
9682 		sin->sin_family = AF_INET;
9683 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9684 		if (ipip->ipi_cmd_type == LIF_CMD) {
9685 			lifr->lifr_addrlen =
9686 			    ip_mask_to_plen(ipif->ipif_net_mask);
9687 		}
9688 	}
9689 	return (0);
9690 }
9691 
9692 /*
9693  * Set the destination address for a pt-pt interface.
9694  */
9695 /* ARGSUSED */
9696 int
9697 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9698     ip_ioctl_cmd_t *ipip, void *if_req)
9699 {
9700 	int err = 0;
9701 	in6_addr_t v6addr;
9702 	boolean_t need_up = B_FALSE;
9703 
9704 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9705 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9706 	ASSERT(IAM_WRITER_IPIF(ipif));
9707 
9708 	if (ipif->ipif_isv6) {
9709 		sin6_t *sin6;
9710 
9711 		if (sin->sin_family != AF_INET6)
9712 			return (EAFNOSUPPORT);
9713 
9714 		sin6 = (sin6_t *)sin;
9715 		v6addr = sin6->sin6_addr;
9716 
9717 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9718 			return (EADDRNOTAVAIL);
9719 	} else {
9720 		ipaddr_t addr;
9721 
9722 		if (sin->sin_family != AF_INET)
9723 			return (EAFNOSUPPORT);
9724 
9725 		addr = sin->sin_addr.s_addr;
9726 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9727 			return (EADDRNOTAVAIL);
9728 
9729 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9730 	}
9731 
9732 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9733 		return (0);	/* No change */
9734 
9735 	if (ipif->ipif_flags & IPIF_UP) {
9736 		/*
9737 		 * If the interface is already marked up,
9738 		 * we call ipif_down which will take care
9739 		 * of ditching any IREs that have been set
9740 		 * up based on the old pp dst address.
9741 		 */
9742 		err = ipif_logical_down(ipif, q, mp);
9743 		if (err == EINPROGRESS)
9744 			return (err);
9745 		(void) ipif_down_tail(ipif);
9746 		need_up = B_TRUE;
9747 	}
9748 	/*
9749 	 * could return EINPROGRESS. If so ioctl will complete in
9750 	 * ip_rput_dlpi_writer
9751 	 */
9752 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9753 	return (err);
9754 }
9755 
9756 static int
9757 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9758     boolean_t need_up)
9759 {
9760 	in6_addr_t v6addr;
9761 	ill_t	*ill = ipif->ipif_ill;
9762 	int	err = 0;
9763 	boolean_t need_dl_down;
9764 	boolean_t need_arp_down;
9765 
9766 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9767 	    ipif->ipif_id, (void *)ipif));
9768 
9769 	/* Must cancel any pending timer before taking the ill_lock */
9770 	if (ipif->ipif_recovery_id != 0)
9771 		(void) untimeout(ipif->ipif_recovery_id);
9772 	ipif->ipif_recovery_id = 0;
9773 
9774 	if (ipif->ipif_isv6) {
9775 		sin6_t *sin6;
9776 
9777 		sin6 = (sin6_t *)sin;
9778 		v6addr = sin6->sin6_addr;
9779 	} else {
9780 		ipaddr_t addr;
9781 
9782 		addr = sin->sin_addr.s_addr;
9783 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9784 	}
9785 	mutex_enter(&ill->ill_lock);
9786 	/* Set point to point destination address. */
9787 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9788 		/*
9789 		 * Allow this as a means of creating logical
9790 		 * pt-pt interfaces on top of e.g. an Ethernet.
9791 		 * XXX Undocumented HACK for testing.
9792 		 * pt-pt interfaces are created with NUD disabled.
9793 		 */
9794 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9795 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9796 		if (ipif->ipif_isv6)
9797 			ill->ill_flags |= ILLF_NONUD;
9798 	}
9799 
9800 	/*
9801 	 * If the interface was previously marked as a duplicate, then since
9802 	 * we've now got a "new" address, it should no longer be considered a
9803 	 * duplicate -- even if the "new" address is the same as the old one.
9804 	 * Note that if all ipifs are down, we may have a pending ARP down
9805 	 * event to handle.
9806 	 */
9807 	need_dl_down = need_arp_down = B_FALSE;
9808 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9809 		need_arp_down = !need_up;
9810 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9811 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9812 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9813 			need_dl_down = B_TRUE;
9814 		}
9815 	}
9816 
9817 	/*
9818 	 * If we've just manually set the IPv6 destination link-local address
9819 	 * (0th ipif), tag the ill so that future updates to the destination
9820 	 * interface ID (as can happen with interfaces over IP tunnels) don't
9821 	 * result in this address getting automatically reconfigured from
9822 	 * under the administrator.
9823 	 */
9824 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9825 		ill->ill_manual_dst_linklocal = 1;
9826 
9827 	/* Set the new address. */
9828 	ipif->ipif_v6pp_dst_addr = v6addr;
9829 	/* Make sure subnet tracks pp_dst */
9830 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9831 	mutex_exit(&ill->ill_lock);
9832 
9833 	if (need_up) {
9834 		/*
9835 		 * Now bring the interface back up.  If this
9836 		 * is the only IPIF for the ILL, ipif_up
9837 		 * will have to re-bind to the device, so
9838 		 * we may get back EINPROGRESS, in which
9839 		 * case, this IOCTL will get completed in
9840 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9841 		 */
9842 		err = ipif_up(ipif, q, mp);
9843 	}
9844 
9845 	if (need_dl_down)
9846 		ill_dl_down(ill);
9847 	if (need_arp_down && !ipif->ipif_isv6)
9848 		(void) ipif_arp_down(ipif);
9849 
9850 	return (err);
9851 }
9852 
9853 /*
9854  * Restart entry point to restart the dstaddress set operation after the
9855  * refcounts have dropped to zero.
9856  */
9857 /* ARGSUSED */
9858 int
9859 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9860     ip_ioctl_cmd_t *ipip, void *ifreq)
9861 {
9862 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9863 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9864 	(void) ipif_down_tail(ipif);
9865 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9866 }
9867 
9868 /* ARGSUSED */
9869 int
9870 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9871     ip_ioctl_cmd_t *ipip, void *if_req)
9872 {
9873 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9874 
9875 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9876 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9877 	/*
9878 	 * Get point to point destination address. The addresses can't
9879 	 * change since we hold a reference to the ipif.
9880 	 */
9881 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9882 		return (EADDRNOTAVAIL);
9883 
9884 	if (ipif->ipif_isv6) {
9885 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9886 		*sin6 = sin6_null;
9887 		sin6->sin6_family = AF_INET6;
9888 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9889 	} else {
9890 		*sin = sin_null;
9891 		sin->sin_family = AF_INET;
9892 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9893 	}
9894 	return (0);
9895 }
9896 
9897 /*
9898  * Check which flags will change by the given flags being set
9899  * silently ignore flags which userland is not allowed to control.
9900  * (Because these flags may change between SIOCGLIFFLAGS and
9901  * SIOCSLIFFLAGS, and that's outside of userland's control,
9902  * we need to silently ignore them rather than fail.)
9903  */
9904 static void
9905 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9906     uint64_t *offp)
9907 {
9908 	ill_t		*ill = ipif->ipif_ill;
9909 	phyint_t 	*phyi = ill->ill_phyint;
9910 	uint64_t	cantchange_flags, intf_flags;
9911 	uint64_t	turn_on, turn_off;
9912 
9913 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9914 	cantchange_flags = IFF_CANTCHANGE;
9915 	if (IS_IPMP(ill))
9916 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9917 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9918 	turn_off = intf_flags & turn_on;
9919 	turn_on ^= turn_off;
9920 	*onp = turn_on;
9921 	*offp = turn_off;
9922 }
9923 
9924 /*
9925  * Set interface flags.  Many flags require special handling (e.g.,
9926  * bringing the interface down); see below for details.
9927  *
9928  * NOTE : We really don't enforce that ipif_id zero should be used
9929  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9930  *	  is because applications generally does SICGLIFFLAGS and
9931  *	  ORs in the new flags (that affects the logical) and does a
9932  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9933  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9934  *	  flags that will be turned on is correct with respect to
9935  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9936  */
9937 /* ARGSUSED */
9938 int
9939 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9940     ip_ioctl_cmd_t *ipip, void *if_req)
9941 {
9942 	uint64_t turn_on;
9943 	uint64_t turn_off;
9944 	int	err = 0;
9945 	phyint_t *phyi;
9946 	ill_t *ill;
9947 	conn_t *connp;
9948 	uint64_t intf_flags;
9949 	boolean_t phyint_flags_modified = B_FALSE;
9950 	uint64_t flags;
9951 	struct ifreq *ifr;
9952 	struct lifreq *lifr;
9953 	boolean_t set_linklocal = B_FALSE;
9954 
9955 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9956 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9957 
9958 	ASSERT(IAM_WRITER_IPIF(ipif));
9959 
9960 	ill = ipif->ipif_ill;
9961 	phyi = ill->ill_phyint;
9962 
9963 	if (ipip->ipi_cmd_type == IF_CMD) {
9964 		ifr = (struct ifreq *)if_req;
9965 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9966 	} else {
9967 		lifr = (struct lifreq *)if_req;
9968 		flags = lifr->lifr_flags;
9969 	}
9970 
9971 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9972 
9973 	/*
9974 	 * Have the flags been set correctly until now?
9975 	 */
9976 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9977 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9978 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9979 	/*
9980 	 * Compare the new flags to the old, and partition
9981 	 * into those coming on and those going off.
9982 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9983 	 */
9984 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9985 		flags |= intf_flags & ~0xFFFF;
9986 
9987 	/*
9988 	 * Explicitly fail attempts to change flags that are always invalid on
9989 	 * an IPMP meta-interface.
9990 	 */
9991 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9992 		return (EINVAL);
9993 
9994 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
9995 	if ((turn_on|turn_off) == 0)
9996 		return (0);	/* No change */
9997 
9998 	/*
9999 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10000 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10001 	 * allow it to be turned off.
10002 	 */
10003 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10004 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10005 		return (EINVAL);
10006 
10007 	if ((connp = Q_TO_CONN(q)) == NULL)
10008 		return (EINVAL);
10009 
10010 	/*
10011 	 * Only vrrp control socket is allowed to change IFF_UP and
10012 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10013 	 */
10014 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10015 		if (!connp->conn_isvrrp)
10016 			return (EINVAL);
10017 	}
10018 
10019 	/*
10020 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10021 	 * VRRP control socket.
10022 	 */
10023 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10024 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10025 			return (EINVAL);
10026 	}
10027 
10028 	if (turn_on & IFF_NOFAILOVER) {
10029 		turn_on |= IFF_DEPRECATED;
10030 		flags |= IFF_DEPRECATED;
10031 	}
10032 
10033 	/*
10034 	 * On underlying interfaces, only allow applications to manage test
10035 	 * addresses -- otherwise, they may get confused when the address
10036 	 * moves as part of being brought up.  Likewise, prevent an
10037 	 * application-managed test address from being converted to a data
10038 	 * address.  To prevent migration of administratively up addresses in
10039 	 * the kernel, we don't allow them to be converted either.
10040 	 */
10041 	if (IS_UNDER_IPMP(ill)) {
10042 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10043 
10044 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10045 			return (EINVAL);
10046 
10047 		if ((turn_off & IFF_NOFAILOVER) &&
10048 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10049 			return (EINVAL);
10050 	}
10051 
10052 	/*
10053 	 * Only allow IFF_TEMPORARY flag to be set on
10054 	 * IPv6 interfaces.
10055 	 */
10056 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10057 		return (EINVAL);
10058 
10059 	/*
10060 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10061 	 */
10062 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10063 		return (EINVAL);
10064 
10065 	/*
10066 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10067 	 * interfaces.  It makes no sense in that context.
10068 	 */
10069 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10070 		return (EINVAL);
10071 
10072 	/*
10073 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10074 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10075 	 * If the link local address isn't set, and can be set, it will get
10076 	 * set later on in this function.
10077 	 */
10078 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10079 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10080 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10081 		if (ipif_cant_setlinklocal(ipif))
10082 			return (EINVAL);
10083 		set_linklocal = B_TRUE;
10084 	}
10085 
10086 	/*
10087 	 * If we modify physical interface flags, we'll potentially need to
10088 	 * send up two routing socket messages for the changes (one for the
10089 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10090 	 */
10091 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10092 		phyint_flags_modified = B_TRUE;
10093 
10094 	/*
10095 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10096 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10097 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10098 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10099 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10100 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10101 	 * will not be honored.
10102 	 */
10103 	if (turn_on & PHYI_STANDBY) {
10104 		/*
10105 		 * No need to grab ill_g_usesrc_lock here; see the
10106 		 * synchronization notes in ip.c.
10107 		 */
10108 		if (ill->ill_usesrc_grp_next != NULL ||
10109 		    intf_flags & PHYI_INACTIVE)
10110 			return (EINVAL);
10111 		if (!(flags & PHYI_FAILED)) {
10112 			flags |= PHYI_INACTIVE;
10113 			turn_on |= PHYI_INACTIVE;
10114 		}
10115 	}
10116 
10117 	if (turn_off & PHYI_STANDBY) {
10118 		flags &= ~PHYI_INACTIVE;
10119 		turn_off |= PHYI_INACTIVE;
10120 	}
10121 
10122 	/*
10123 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10124 	 * would end up on.
10125 	 */
10126 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10127 	    (PHYI_FAILED | PHYI_INACTIVE))
10128 		return (EINVAL);
10129 
10130 	/*
10131 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10132 	 * status of the interface.
10133 	 */
10134 	if ((turn_on | turn_off) & ILLF_ROUTER)
10135 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10136 
10137 	/*
10138 	 * If the interface is not UP and we are not going to
10139 	 * bring it UP, record the flags and return. When the
10140 	 * interface comes UP later, the right actions will be
10141 	 * taken.
10142 	 */
10143 	if (!(ipif->ipif_flags & IPIF_UP) &&
10144 	    !(turn_on & IPIF_UP)) {
10145 		/* Record new flags in their respective places. */
10146 		mutex_enter(&ill->ill_lock);
10147 		mutex_enter(&ill->ill_phyint->phyint_lock);
10148 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10149 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10150 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10151 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10152 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10153 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10154 		mutex_exit(&ill->ill_lock);
10155 		mutex_exit(&ill->ill_phyint->phyint_lock);
10156 
10157 		/*
10158 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10159 		 * same to the kernel: if any of them has been set by
10160 		 * userland, the interface cannot be used for data traffic.
10161 		 */
10162 		if ((turn_on|turn_off) &
10163 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10164 			ASSERT(!IS_IPMP(ill));
10165 			/*
10166 			 * It's possible the ill is part of an "anonymous"
10167 			 * IPMP group rather than a real group.  In that case,
10168 			 * there are no other interfaces in the group and thus
10169 			 * no need to call ipmp_phyint_refresh_active().
10170 			 */
10171 			if (IS_UNDER_IPMP(ill))
10172 				ipmp_phyint_refresh_active(phyi);
10173 		}
10174 
10175 		if (phyint_flags_modified) {
10176 			if (phyi->phyint_illv4 != NULL) {
10177 				ip_rts_ifmsg(phyi->phyint_illv4->
10178 				    ill_ipif, RTSQ_DEFAULT);
10179 			}
10180 			if (phyi->phyint_illv6 != NULL) {
10181 				ip_rts_ifmsg(phyi->phyint_illv6->
10182 				    ill_ipif, RTSQ_DEFAULT);
10183 			}
10184 		}
10185 		/* The default multicast interface might have changed */
10186 		ire_increment_multicast_generation(ill->ill_ipst,
10187 		    ill->ill_isv6);
10188 
10189 		return (0);
10190 	} else if (set_linklocal) {
10191 		mutex_enter(&ill->ill_lock);
10192 		if (set_linklocal)
10193 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10194 		mutex_exit(&ill->ill_lock);
10195 	}
10196 
10197 	/*
10198 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10199 	 * or point-to-point interfaces with an unspecified destination. We do
10200 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10201 	 * have a subnet assigned, which is how in.ndpd currently manages its
10202 	 * onlink prefix list when no addresses are configured with those
10203 	 * prefixes.
10204 	 */
10205 	if (ipif->ipif_isv6 &&
10206 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10207 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10208 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10209 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10210 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10211 		return (EINVAL);
10212 	}
10213 
10214 	/*
10215 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10216 	 * from being brought up.
10217 	 */
10218 	if (!ipif->ipif_isv6 &&
10219 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10220 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10221 		return (EINVAL);
10222 	}
10223 
10224 	/*
10225 	 * If we are going to change one or more of the flags that are
10226 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10227 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10228 	 * IPIF_NOFAILOVER, we will take special action.  This is
10229 	 * done by bring the ipif down, changing the flags and bringing
10230 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10231 	 * back up will trigger the address to be moved.
10232 	 *
10233 	 * If we are going to change IFF_NOACCEPT, we need to bring
10234 	 * all the ipifs down then bring them up again.	 The act of
10235 	 * bringing all the ipifs back up will trigger the local
10236 	 * ires being recreated with "no_accept" set/cleared.
10237 	 *
10238 	 * Note that ILLF_NOACCEPT is always set separately from the
10239 	 * other flags.
10240 	 */
10241 	if ((turn_on|turn_off) &
10242 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10243 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10244 	    IPIF_NOFAILOVER)) {
10245 		/*
10246 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10247 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10248 		 * entries shared between multiple ipifs on the same subnet.
10249 		 */
10250 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10251 		    !(turn_off & IPIF_UP)) {
10252 			if (ipif->ipif_flags & IPIF_UP)
10253 				ill->ill_logical_down = 1;
10254 			turn_on &= ~IPIF_UP;
10255 		}
10256 		err = ipif_down(ipif, q, mp);
10257 		ip1dbg(("ipif_down returns %d err ", err));
10258 		if (err == EINPROGRESS)
10259 			return (err);
10260 		(void) ipif_down_tail(ipif);
10261 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10262 		/*
10263 		 * If we can quiesce the ill, then continue.  If not, then
10264 		 * ip_sioctl_flags_tail() will be called from
10265 		 * ipif_ill_refrele_tail().
10266 		 */
10267 		ill_down_ipifs(ill, B_TRUE);
10268 
10269 		mutex_enter(&connp->conn_lock);
10270 		mutex_enter(&ill->ill_lock);
10271 		if (!ill_is_quiescent(ill)) {
10272 			boolean_t success;
10273 
10274 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10275 			    q, mp, ILL_DOWN);
10276 			mutex_exit(&ill->ill_lock);
10277 			mutex_exit(&connp->conn_lock);
10278 			return (success ? EINPROGRESS : EINTR);
10279 		}
10280 		mutex_exit(&ill->ill_lock);
10281 		mutex_exit(&connp->conn_lock);
10282 	}
10283 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10284 }
10285 
10286 static int
10287 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10288 {
10289 	ill_t	*ill;
10290 	phyint_t *phyi;
10291 	uint64_t turn_on, turn_off;
10292 	boolean_t phyint_flags_modified = B_FALSE;
10293 	int	err = 0;
10294 	boolean_t set_linklocal = B_FALSE;
10295 
10296 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10297 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10298 
10299 	ASSERT(IAM_WRITER_IPIF(ipif));
10300 
10301 	ill = ipif->ipif_ill;
10302 	phyi = ill->ill_phyint;
10303 
10304 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10305 
10306 	/*
10307 	 * IFF_UP is handled separately.
10308 	 */
10309 	turn_on &= ~IFF_UP;
10310 	turn_off &= ~IFF_UP;
10311 
10312 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10313 		phyint_flags_modified = B_TRUE;
10314 
10315 	/*
10316 	 * Now we change the flags. Track current value of
10317 	 * other flags in their respective places.
10318 	 */
10319 	mutex_enter(&ill->ill_lock);
10320 	mutex_enter(&phyi->phyint_lock);
10321 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10322 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10323 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10324 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10325 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10326 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10327 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10328 		set_linklocal = B_TRUE;
10329 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10330 	}
10331 
10332 	mutex_exit(&ill->ill_lock);
10333 	mutex_exit(&phyi->phyint_lock);
10334 
10335 	if (set_linklocal)
10336 		(void) ipif_setlinklocal(ipif);
10337 
10338 	/*
10339 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10340 	 * the kernel: if any of them has been set by userland, the interface
10341 	 * cannot be used for data traffic.
10342 	 */
10343 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10344 		ASSERT(!IS_IPMP(ill));
10345 		/*
10346 		 * It's possible the ill is part of an "anonymous" IPMP group
10347 		 * rather than a real group.  In that case, there are no other
10348 		 * interfaces in the group and thus no need for us to call
10349 		 * ipmp_phyint_refresh_active().
10350 		 */
10351 		if (IS_UNDER_IPMP(ill))
10352 			ipmp_phyint_refresh_active(phyi);
10353 	}
10354 
10355 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10356 		/*
10357 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10358 		 * ipifs that were brought down.
10359 		 *
10360 		 * The routing sockets messages are sent as the result
10361 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10362 		 * as well.
10363 		 */
10364 		err = ill_up_ipifs(ill, q, mp);
10365 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10366 		/*
10367 		 * XXX ipif_up really does not know whether a phyint flags
10368 		 * was modified or not. So, it sends up information on
10369 		 * only one routing sockets message. As we don't bring up
10370 		 * the interface and also set PHYI_ flags simultaneously
10371 		 * it should be okay.
10372 		 */
10373 		err = ipif_up(ipif, q, mp);
10374 	} else {
10375 		/*
10376 		 * Make sure routing socket sees all changes to the flags.
10377 		 * ipif_up_done* handles this when we use ipif_up.
10378 		 */
10379 		if (phyint_flags_modified) {
10380 			if (phyi->phyint_illv4 != NULL) {
10381 				ip_rts_ifmsg(phyi->phyint_illv4->
10382 				    ill_ipif, RTSQ_DEFAULT);
10383 			}
10384 			if (phyi->phyint_illv6 != NULL) {
10385 				ip_rts_ifmsg(phyi->phyint_illv6->
10386 				    ill_ipif, RTSQ_DEFAULT);
10387 			}
10388 		} else {
10389 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10390 		}
10391 		/*
10392 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10393 		 * this in need_up case.
10394 		 */
10395 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10396 	}
10397 
10398 	/* The default multicast interface might have changed */
10399 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10400 	return (err);
10401 }
10402 
10403 /*
10404  * Restart the flags operation now that the refcounts have dropped to zero.
10405  */
10406 /* ARGSUSED */
10407 int
10408 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10409     ip_ioctl_cmd_t *ipip, void *if_req)
10410 {
10411 	uint64_t flags;
10412 	struct ifreq *ifr = if_req;
10413 	struct lifreq *lifr = if_req;
10414 	uint64_t turn_on, turn_off;
10415 
10416 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10417 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10418 
10419 	if (ipip->ipi_cmd_type == IF_CMD) {
10420 		/* cast to uint16_t prevents unwanted sign extension */
10421 		flags = (uint16_t)ifr->ifr_flags;
10422 	} else {
10423 		flags = lifr->lifr_flags;
10424 	}
10425 
10426 	/*
10427 	 * If this function call is a result of the ILLF_NOACCEPT flag
10428 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10429 	 */
10430 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10431 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10432 		(void) ipif_down_tail(ipif);
10433 
10434 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10435 }
10436 
10437 /*
10438  * Can operate on either a module or a driver queue.
10439  */
10440 /* ARGSUSED */
10441 int
10442 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10443     ip_ioctl_cmd_t *ipip, void *if_req)
10444 {
10445 	/*
10446 	 * Has the flags been set correctly till now ?
10447 	 */
10448 	ill_t *ill = ipif->ipif_ill;
10449 	phyint_t *phyi = ill->ill_phyint;
10450 
10451 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10452 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10453 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10454 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10455 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10456 
10457 	/*
10458 	 * Need a lock since some flags can be set even when there are
10459 	 * references to the ipif.
10460 	 */
10461 	mutex_enter(&ill->ill_lock);
10462 	if (ipip->ipi_cmd_type == IF_CMD) {
10463 		struct ifreq *ifr = (struct ifreq *)if_req;
10464 
10465 		/* Get interface flags (low 16 only). */
10466 		ifr->ifr_flags = ((ipif->ipif_flags |
10467 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10468 	} else {
10469 		struct lifreq *lifr = (struct lifreq *)if_req;
10470 
10471 		/* Get interface flags. */
10472 		lifr->lifr_flags = ipif->ipif_flags |
10473 		    ill->ill_flags | phyi->phyint_flags;
10474 	}
10475 	mutex_exit(&ill->ill_lock);
10476 	return (0);
10477 }
10478 
10479 /*
10480  * We allow the MTU to be set on an ILL, but not have it be different
10481  * for different IPIFs since we don't actually send packets on IPIFs.
10482  */
10483 /* ARGSUSED */
10484 int
10485 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10486     ip_ioctl_cmd_t *ipip, void *if_req)
10487 {
10488 	int mtu;
10489 	int ip_min_mtu;
10490 	struct ifreq	*ifr;
10491 	struct lifreq *lifr;
10492 	ill_t	*ill;
10493 
10494 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10495 	    ipif->ipif_id, (void *)ipif));
10496 	if (ipip->ipi_cmd_type == IF_CMD) {
10497 		ifr = (struct ifreq *)if_req;
10498 		mtu = ifr->ifr_metric;
10499 	} else {
10500 		lifr = (struct lifreq *)if_req;
10501 		mtu = lifr->lifr_mtu;
10502 	}
10503 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10504 	if (ipif->ipif_id != 0)
10505 		return (EINVAL);
10506 
10507 	ill = ipif->ipif_ill;
10508 	if (ipif->ipif_isv6)
10509 		ip_min_mtu = IPV6_MIN_MTU;
10510 	else
10511 		ip_min_mtu = IP_MIN_MTU;
10512 
10513 	mutex_enter(&ill->ill_lock);
10514 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10515 		mutex_exit(&ill->ill_lock);
10516 		return (EINVAL);
10517 	}
10518 	/*
10519 	 * The dce and fragmentation code can handle changes to ill_mtu
10520 	 * concurrent with sending/fragmenting packets.
10521 	 */
10522 	ill->ill_mtu = mtu;
10523 	ill->ill_flags |= ILLF_FIXEDMTU;
10524 	mutex_exit(&ill->ill_lock);
10525 
10526 	/*
10527 	 * Make sure all dce_generation checks find out
10528 	 * that ill_mtu has changed.
10529 	 */
10530 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10531 
10532 	/* Update the MTU in SCTP's list */
10533 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10534 	return (0);
10535 }
10536 
10537 /* Get interface MTU. */
10538 /* ARGSUSED */
10539 int
10540 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10541 	ip_ioctl_cmd_t *ipip, void *if_req)
10542 {
10543 	struct ifreq	*ifr;
10544 	struct lifreq	*lifr;
10545 
10546 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10547 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10548 
10549 	/*
10550 	 * We allow a get on any logical interface even though the set
10551 	 * can only be done on logical unit 0.
10552 	 */
10553 	if (ipip->ipi_cmd_type == IF_CMD) {
10554 		ifr = (struct ifreq *)if_req;
10555 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10556 	} else {
10557 		lifr = (struct lifreq *)if_req;
10558 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10559 	}
10560 	return (0);
10561 }
10562 
10563 /* Set interface broadcast address. */
10564 /* ARGSUSED2 */
10565 int
10566 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10567 	ip_ioctl_cmd_t *ipip, void *if_req)
10568 {
10569 	ipaddr_t addr;
10570 	ire_t	*ire;
10571 	ill_t		*ill = ipif->ipif_ill;
10572 	ip_stack_t	*ipst = ill->ill_ipst;
10573 
10574 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10575 	    ipif->ipif_id));
10576 
10577 	ASSERT(IAM_WRITER_IPIF(ipif));
10578 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10579 		return (EADDRNOTAVAIL);
10580 
10581 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10582 
10583 	if (sin->sin_family != AF_INET)
10584 		return (EAFNOSUPPORT);
10585 
10586 	addr = sin->sin_addr.s_addr;
10587 	if (ipif->ipif_flags & IPIF_UP) {
10588 		/*
10589 		 * If we are already up, make sure the new
10590 		 * broadcast address makes sense.  If it does,
10591 		 * there should be an IRE for it already.
10592 		 */
10593 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10594 		    ill, ipif->ipif_zoneid, NULL,
10595 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10596 		if (ire == NULL) {
10597 			return (EINVAL);
10598 		} else {
10599 			ire_refrele(ire);
10600 		}
10601 	}
10602 	/*
10603 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10604 	 * needs to already exist we never need to change the set of
10605 	 * IRE_BROADCASTs when we are UP.
10606 	 */
10607 	if (addr != ipif->ipif_brd_addr)
10608 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10609 
10610 	return (0);
10611 }
10612 
10613 /* Get interface broadcast address. */
10614 /* ARGSUSED */
10615 int
10616 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10617     ip_ioctl_cmd_t *ipip, void *if_req)
10618 {
10619 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10620 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10621 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10622 		return (EADDRNOTAVAIL);
10623 
10624 	/* IPIF_BROADCAST not possible with IPv6 */
10625 	ASSERT(!ipif->ipif_isv6);
10626 	*sin = sin_null;
10627 	sin->sin_family = AF_INET;
10628 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10629 	return (0);
10630 }
10631 
10632 /*
10633  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10634  */
10635 /* ARGSUSED */
10636 int
10637 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10638     ip_ioctl_cmd_t *ipip, void *if_req)
10639 {
10640 	int err = 0;
10641 	in6_addr_t v6mask;
10642 
10643 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10644 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10645 
10646 	ASSERT(IAM_WRITER_IPIF(ipif));
10647 
10648 	if (ipif->ipif_isv6) {
10649 		sin6_t *sin6;
10650 
10651 		if (sin->sin_family != AF_INET6)
10652 			return (EAFNOSUPPORT);
10653 
10654 		sin6 = (sin6_t *)sin;
10655 		v6mask = sin6->sin6_addr;
10656 	} else {
10657 		ipaddr_t mask;
10658 
10659 		if (sin->sin_family != AF_INET)
10660 			return (EAFNOSUPPORT);
10661 
10662 		mask = sin->sin_addr.s_addr;
10663 		V4MASK_TO_V6(mask, v6mask);
10664 	}
10665 
10666 	/*
10667 	 * No big deal if the interface isn't already up, or the mask
10668 	 * isn't really changing, or this is pt-pt.
10669 	 */
10670 	if (!(ipif->ipif_flags & IPIF_UP) ||
10671 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10672 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10673 		ipif->ipif_v6net_mask = v6mask;
10674 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10675 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10676 			    ipif->ipif_v6net_mask,
10677 			    ipif->ipif_v6subnet);
10678 		}
10679 		return (0);
10680 	}
10681 	/*
10682 	 * Make sure we have valid net and subnet broadcast ire's
10683 	 * for the old netmask, if needed by other logical interfaces.
10684 	 */
10685 	err = ipif_logical_down(ipif, q, mp);
10686 	if (err == EINPROGRESS)
10687 		return (err);
10688 	(void) ipif_down_tail(ipif);
10689 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10690 	return (err);
10691 }
10692 
10693 static int
10694 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10695 {
10696 	in6_addr_t v6mask;
10697 	int err = 0;
10698 
10699 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10700 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10701 
10702 	if (ipif->ipif_isv6) {
10703 		sin6_t *sin6;
10704 
10705 		sin6 = (sin6_t *)sin;
10706 		v6mask = sin6->sin6_addr;
10707 	} else {
10708 		ipaddr_t mask;
10709 
10710 		mask = sin->sin_addr.s_addr;
10711 		V4MASK_TO_V6(mask, v6mask);
10712 	}
10713 
10714 	ipif->ipif_v6net_mask = v6mask;
10715 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10716 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10717 		    ipif->ipif_v6subnet);
10718 	}
10719 	err = ipif_up(ipif, q, mp);
10720 
10721 	if (err == 0 || err == EINPROGRESS) {
10722 		/*
10723 		 * The interface must be DL_BOUND if this packet has to
10724 		 * go out on the wire. Since we only go through a logical
10725 		 * down and are bound with the driver during an internal
10726 		 * down/up that is satisfied.
10727 		 */
10728 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10729 			/* Potentially broadcast an address mask reply. */
10730 			ipif_mask_reply(ipif);
10731 		}
10732 	}
10733 	return (err);
10734 }
10735 
10736 /* ARGSUSED */
10737 int
10738 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10739     ip_ioctl_cmd_t *ipip, void *if_req)
10740 {
10741 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10742 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10743 	(void) ipif_down_tail(ipif);
10744 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10745 }
10746 
10747 /* Get interface net mask. */
10748 /* ARGSUSED */
10749 int
10750 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10751     ip_ioctl_cmd_t *ipip, void *if_req)
10752 {
10753 	struct lifreq *lifr = (struct lifreq *)if_req;
10754 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10755 
10756 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10757 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10758 
10759 	/*
10760 	 * net mask can't change since we have a reference to the ipif.
10761 	 */
10762 	if (ipif->ipif_isv6) {
10763 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10764 		*sin6 = sin6_null;
10765 		sin6->sin6_family = AF_INET6;
10766 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10767 		lifr->lifr_addrlen =
10768 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10769 	} else {
10770 		*sin = sin_null;
10771 		sin->sin_family = AF_INET;
10772 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10773 		if (ipip->ipi_cmd_type == LIF_CMD) {
10774 			lifr->lifr_addrlen =
10775 			    ip_mask_to_plen(ipif->ipif_net_mask);
10776 		}
10777 	}
10778 	return (0);
10779 }
10780 
10781 /* ARGSUSED */
10782 int
10783 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10784     ip_ioctl_cmd_t *ipip, void *if_req)
10785 {
10786 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10787 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10788 
10789 	/*
10790 	 * Since no applications should ever be setting metrics on underlying
10791 	 * interfaces, we explicitly fail to smoke 'em out.
10792 	 */
10793 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10794 		return (EINVAL);
10795 
10796 	/*
10797 	 * Set interface metric.  We don't use this for
10798 	 * anything but we keep track of it in case it is
10799 	 * important to routing applications or such.
10800 	 */
10801 	if (ipip->ipi_cmd_type == IF_CMD) {
10802 		struct ifreq    *ifr;
10803 
10804 		ifr = (struct ifreq *)if_req;
10805 		ipif->ipif_metric = ifr->ifr_metric;
10806 	} else {
10807 		struct lifreq   *lifr;
10808 
10809 		lifr = (struct lifreq *)if_req;
10810 		ipif->ipif_metric = lifr->lifr_metric;
10811 	}
10812 	return (0);
10813 }
10814 
10815 /* ARGSUSED */
10816 int
10817 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10818     ip_ioctl_cmd_t *ipip, void *if_req)
10819 {
10820 	/* Get interface metric. */
10821 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10822 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10823 
10824 	if (ipip->ipi_cmd_type == IF_CMD) {
10825 		struct ifreq    *ifr;
10826 
10827 		ifr = (struct ifreq *)if_req;
10828 		ifr->ifr_metric = ipif->ipif_metric;
10829 	} else {
10830 		struct lifreq   *lifr;
10831 
10832 		lifr = (struct lifreq *)if_req;
10833 		lifr->lifr_metric = ipif->ipif_metric;
10834 	}
10835 
10836 	return (0);
10837 }
10838 
10839 /* ARGSUSED */
10840 int
10841 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10842     ip_ioctl_cmd_t *ipip, void *if_req)
10843 {
10844 	int	arp_muxid;
10845 
10846 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10847 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10848 	/*
10849 	 * Set the muxid returned from I_PLINK.
10850 	 */
10851 	if (ipip->ipi_cmd_type == IF_CMD) {
10852 		struct ifreq *ifr = (struct ifreq *)if_req;
10853 
10854 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10855 		arp_muxid = ifr->ifr_arp_muxid;
10856 	} else {
10857 		struct lifreq *lifr = (struct lifreq *)if_req;
10858 
10859 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10860 		arp_muxid = lifr->lifr_arp_muxid;
10861 	}
10862 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10863 	return (0);
10864 }
10865 
10866 /* ARGSUSED */
10867 int
10868 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10869     ip_ioctl_cmd_t *ipip, void *if_req)
10870 {
10871 	int	arp_muxid = 0;
10872 
10873 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10874 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10875 	/*
10876 	 * Get the muxid saved in ill for I_PUNLINK.
10877 	 */
10878 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10879 	if (ipip->ipi_cmd_type == IF_CMD) {
10880 		struct ifreq *ifr = (struct ifreq *)if_req;
10881 
10882 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10883 		ifr->ifr_arp_muxid = arp_muxid;
10884 	} else {
10885 		struct lifreq *lifr = (struct lifreq *)if_req;
10886 
10887 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10888 		lifr->lifr_arp_muxid = arp_muxid;
10889 	}
10890 	return (0);
10891 }
10892 
10893 /*
10894  * Set the subnet prefix. Does not modify the broadcast address.
10895  */
10896 /* ARGSUSED */
10897 int
10898 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10899     ip_ioctl_cmd_t *ipip, void *if_req)
10900 {
10901 	int err = 0;
10902 	in6_addr_t v6addr;
10903 	in6_addr_t v6mask;
10904 	boolean_t need_up = B_FALSE;
10905 	int addrlen;
10906 
10907 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10908 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10909 
10910 	ASSERT(IAM_WRITER_IPIF(ipif));
10911 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10912 
10913 	if (ipif->ipif_isv6) {
10914 		sin6_t *sin6;
10915 
10916 		if (sin->sin_family != AF_INET6)
10917 			return (EAFNOSUPPORT);
10918 
10919 		sin6 = (sin6_t *)sin;
10920 		v6addr = sin6->sin6_addr;
10921 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10922 			return (EADDRNOTAVAIL);
10923 	} else {
10924 		ipaddr_t addr;
10925 
10926 		if (sin->sin_family != AF_INET)
10927 			return (EAFNOSUPPORT);
10928 
10929 		addr = sin->sin_addr.s_addr;
10930 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10931 			return (EADDRNOTAVAIL);
10932 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10933 		/* Add 96 bits */
10934 		addrlen += IPV6_ABITS - IP_ABITS;
10935 	}
10936 
10937 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10938 		return (EINVAL);
10939 
10940 	/* Check if bits in the address is set past the mask */
10941 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10942 		return (EINVAL);
10943 
10944 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10945 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10946 		return (0);	/* No change */
10947 
10948 	if (ipif->ipif_flags & IPIF_UP) {
10949 		/*
10950 		 * If the interface is already marked up,
10951 		 * we call ipif_down which will take care
10952 		 * of ditching any IREs that have been set
10953 		 * up based on the old interface address.
10954 		 */
10955 		err = ipif_logical_down(ipif, q, mp);
10956 		if (err == EINPROGRESS)
10957 			return (err);
10958 		(void) ipif_down_tail(ipif);
10959 		need_up = B_TRUE;
10960 	}
10961 
10962 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10963 	return (err);
10964 }
10965 
10966 static int
10967 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10968     queue_t *q, mblk_t *mp, boolean_t need_up)
10969 {
10970 	ill_t	*ill = ipif->ipif_ill;
10971 	int	err = 0;
10972 
10973 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10974 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10975 
10976 	/* Set the new address. */
10977 	mutex_enter(&ill->ill_lock);
10978 	ipif->ipif_v6net_mask = v6mask;
10979 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10980 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10981 		    ipif->ipif_v6subnet);
10982 	}
10983 	mutex_exit(&ill->ill_lock);
10984 
10985 	if (need_up) {
10986 		/*
10987 		 * Now bring the interface back up.  If this
10988 		 * is the only IPIF for the ILL, ipif_up
10989 		 * will have to re-bind to the device, so
10990 		 * we may get back EINPROGRESS, in which
10991 		 * case, this IOCTL will get completed in
10992 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10993 		 */
10994 		err = ipif_up(ipif, q, mp);
10995 		if (err == EINPROGRESS)
10996 			return (err);
10997 	}
10998 	return (err);
10999 }
11000 
11001 /* ARGSUSED */
11002 int
11003 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11004     ip_ioctl_cmd_t *ipip, void *if_req)
11005 {
11006 	int	addrlen;
11007 	in6_addr_t v6addr;
11008 	in6_addr_t v6mask;
11009 	struct lifreq *lifr = (struct lifreq *)if_req;
11010 
11011 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11012 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11013 	(void) ipif_down_tail(ipif);
11014 
11015 	addrlen = lifr->lifr_addrlen;
11016 	if (ipif->ipif_isv6) {
11017 		sin6_t *sin6;
11018 
11019 		sin6 = (sin6_t *)sin;
11020 		v6addr = sin6->sin6_addr;
11021 	} else {
11022 		ipaddr_t addr;
11023 
11024 		addr = sin->sin_addr.s_addr;
11025 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11026 		addrlen += IPV6_ABITS - IP_ABITS;
11027 	}
11028 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11029 
11030 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11031 }
11032 
11033 /* ARGSUSED */
11034 int
11035 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11036     ip_ioctl_cmd_t *ipip, void *if_req)
11037 {
11038 	struct lifreq *lifr = (struct lifreq *)if_req;
11039 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11040 
11041 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11042 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11043 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11044 
11045 	if (ipif->ipif_isv6) {
11046 		*sin6 = sin6_null;
11047 		sin6->sin6_family = AF_INET6;
11048 		sin6->sin6_addr = ipif->ipif_v6subnet;
11049 		lifr->lifr_addrlen =
11050 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11051 	} else {
11052 		*sin = sin_null;
11053 		sin->sin_family = AF_INET;
11054 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11055 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11056 	}
11057 	return (0);
11058 }
11059 
11060 /*
11061  * Set the IPv6 address token.
11062  */
11063 /* ARGSUSED */
11064 int
11065 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11066     ip_ioctl_cmd_t *ipi, void *if_req)
11067 {
11068 	ill_t *ill = ipif->ipif_ill;
11069 	int err;
11070 	in6_addr_t v6addr;
11071 	in6_addr_t v6mask;
11072 	boolean_t need_up = B_FALSE;
11073 	int i;
11074 	sin6_t *sin6 = (sin6_t *)sin;
11075 	struct lifreq *lifr = (struct lifreq *)if_req;
11076 	int addrlen;
11077 
11078 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11079 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11080 	ASSERT(IAM_WRITER_IPIF(ipif));
11081 
11082 	addrlen = lifr->lifr_addrlen;
11083 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11084 	if (ipif->ipif_id != 0)
11085 		return (EINVAL);
11086 
11087 	if (!ipif->ipif_isv6)
11088 		return (EINVAL);
11089 
11090 	if (addrlen > IPV6_ABITS)
11091 		return (EINVAL);
11092 
11093 	v6addr = sin6->sin6_addr;
11094 
11095 	/*
11096 	 * The length of the token is the length from the end.  To get
11097 	 * the proper mask for this, compute the mask of the bits not
11098 	 * in the token; ie. the prefix, and then xor to get the mask.
11099 	 */
11100 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11101 		return (EINVAL);
11102 	for (i = 0; i < 4; i++) {
11103 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11104 	}
11105 
11106 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11107 	    ill->ill_token_length == addrlen)
11108 		return (0);	/* No change */
11109 
11110 	if (ipif->ipif_flags & IPIF_UP) {
11111 		err = ipif_logical_down(ipif, q, mp);
11112 		if (err == EINPROGRESS)
11113 			return (err);
11114 		(void) ipif_down_tail(ipif);
11115 		need_up = B_TRUE;
11116 	}
11117 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11118 	return (err);
11119 }
11120 
11121 static int
11122 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11123     mblk_t *mp, boolean_t need_up)
11124 {
11125 	in6_addr_t v6addr;
11126 	in6_addr_t v6mask;
11127 	ill_t	*ill = ipif->ipif_ill;
11128 	int	i;
11129 	int	err = 0;
11130 
11131 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11132 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11133 	v6addr = sin6->sin6_addr;
11134 	/*
11135 	 * The length of the token is the length from the end.  To get
11136 	 * the proper mask for this, compute the mask of the bits not
11137 	 * in the token; ie. the prefix, and then xor to get the mask.
11138 	 */
11139 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11140 	for (i = 0; i < 4; i++)
11141 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11142 
11143 	mutex_enter(&ill->ill_lock);
11144 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11145 	ill->ill_token_length = addrlen;
11146 	ill->ill_manual_token = 1;
11147 
11148 	/* Reconfigure the link-local address based on this new token */
11149 	ipif_setlinklocal(ill->ill_ipif);
11150 
11151 	mutex_exit(&ill->ill_lock);
11152 
11153 	if (need_up) {
11154 		/*
11155 		 * Now bring the interface back up.  If this
11156 		 * is the only IPIF for the ILL, ipif_up
11157 		 * will have to re-bind to the device, so
11158 		 * we may get back EINPROGRESS, in which
11159 		 * case, this IOCTL will get completed in
11160 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11161 		 */
11162 		err = ipif_up(ipif, q, mp);
11163 		if (err == EINPROGRESS)
11164 			return (err);
11165 	}
11166 	return (err);
11167 }
11168 
11169 /* ARGSUSED */
11170 int
11171 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11172     ip_ioctl_cmd_t *ipi, void *if_req)
11173 {
11174 	ill_t *ill;
11175 	sin6_t *sin6 = (sin6_t *)sin;
11176 	struct lifreq *lifr = (struct lifreq *)if_req;
11177 
11178 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11179 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11180 	if (ipif->ipif_id != 0)
11181 		return (EINVAL);
11182 
11183 	ill = ipif->ipif_ill;
11184 	if (!ill->ill_isv6)
11185 		return (ENXIO);
11186 
11187 	*sin6 = sin6_null;
11188 	sin6->sin6_family = AF_INET6;
11189 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11190 	sin6->sin6_addr = ill->ill_token;
11191 	lifr->lifr_addrlen = ill->ill_token_length;
11192 	return (0);
11193 }
11194 
11195 /*
11196  * Set (hardware) link specific information that might override
11197  * what was acquired through the DL_INFO_ACK.
11198  */
11199 /* ARGSUSED */
11200 int
11201 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11202     ip_ioctl_cmd_t *ipi, void *if_req)
11203 {
11204 	ill_t		*ill = ipif->ipif_ill;
11205 	int		ip_min_mtu;
11206 	struct lifreq	*lifr = (struct lifreq *)if_req;
11207 	lif_ifinfo_req_t *lir;
11208 
11209 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11210 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11211 	lir = &lifr->lifr_ifinfo;
11212 	ASSERT(IAM_WRITER_IPIF(ipif));
11213 
11214 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11215 	if (ipif->ipif_id != 0)
11216 		return (EINVAL);
11217 
11218 	/* Set interface MTU. */
11219 	if (ipif->ipif_isv6)
11220 		ip_min_mtu = IPV6_MIN_MTU;
11221 	else
11222 		ip_min_mtu = IP_MIN_MTU;
11223 
11224 	/*
11225 	 * Verify values before we set anything. Allow zero to
11226 	 * mean unspecified.
11227 	 *
11228 	 * XXX We should be able to set the user-defined lir_mtu to some value
11229 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11230 	 * ill_max_frag value tells us the max MTU that can be handled by the
11231 	 * datalink, whereas the ill_current_frag is dynamically computed for
11232 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11233 	 * since there is currently no way of distinguishing between
11234 	 * administratively fixed link mtu values (e.g., those set via
11235 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11236 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11237 	 * upper-bound.
11238 	 */
11239 	if (lir->lir_maxmtu != 0 &&
11240 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11241 	    lir->lir_maxmtu < ip_min_mtu))
11242 		return (EINVAL);
11243 	if (lir->lir_reachtime != 0 &&
11244 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11245 		return (EINVAL);
11246 	if (lir->lir_reachretrans != 0 &&
11247 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11248 		return (EINVAL);
11249 
11250 	mutex_enter(&ill->ill_lock);
11251 	/*
11252 	 * The dce and fragmentation code can handle changes to ill_mtu
11253 	 * concurrent with sending/fragmenting packets.
11254 	 */
11255 	if (lir->lir_maxmtu != 0)
11256 		ill->ill_user_mtu = lir->lir_maxmtu;
11257 
11258 	if (lir->lir_reachtime != 0)
11259 		ill->ill_reachable_time = lir->lir_reachtime;
11260 
11261 	if (lir->lir_reachretrans != 0)
11262 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11263 
11264 	ill->ill_max_hops = lir->lir_maxhops;
11265 	ill->ill_max_buf = ND_MAX_Q;
11266 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11267 		/*
11268 		 * ill_mtu is the actual interface MTU, obtained as the min
11269 		 * of user-configured mtu and the value announced by the
11270 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11271 		 * we have already made the choice of requiring
11272 		 * ill_user_mtu < ill_current_frag by the time we get here,
11273 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11274 		 * here.
11275 		 */
11276 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11277 	}
11278 	mutex_exit(&ill->ill_lock);
11279 
11280 	/*
11281 	 * Make sure all dce_generation checks find out
11282 	 * that ill_mtu has changed.
11283 	 */
11284 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11285 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11286 
11287 	/*
11288 	 * Refresh IPMP meta-interface MTU if necessary.
11289 	 */
11290 	if (IS_UNDER_IPMP(ill))
11291 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11292 
11293 	return (0);
11294 }
11295 
11296 /* ARGSUSED */
11297 int
11298 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11299     ip_ioctl_cmd_t *ipi, void *if_req)
11300 {
11301 	struct lif_ifinfo_req *lir;
11302 	ill_t *ill = ipif->ipif_ill;
11303 
11304 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11305 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11306 	if (ipif->ipif_id != 0)
11307 		return (EINVAL);
11308 
11309 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11310 	lir->lir_maxhops = ill->ill_max_hops;
11311 	lir->lir_reachtime = ill->ill_reachable_time;
11312 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11313 	lir->lir_maxmtu = ill->ill_mtu;
11314 
11315 	return (0);
11316 }
11317 
11318 /*
11319  * Return best guess as to the subnet mask for the specified address.
11320  * Based on the subnet masks for all the configured interfaces.
11321  *
11322  * We end up returning a zero mask in the case of default, multicast or
11323  * experimental.
11324  */
11325 static ipaddr_t
11326 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11327 {
11328 	ipaddr_t net_mask;
11329 	ill_t	*ill;
11330 	ipif_t	*ipif;
11331 	ill_walk_context_t ctx;
11332 	ipif_t	*fallback_ipif = NULL;
11333 
11334 	net_mask = ip_net_mask(addr);
11335 	if (net_mask == 0) {
11336 		*ipifp = NULL;
11337 		return (0);
11338 	}
11339 
11340 	/* Let's check to see if this is maybe a local subnet route. */
11341 	/* this function only applies to IPv4 interfaces */
11342 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11343 	ill = ILL_START_WALK_V4(&ctx, ipst);
11344 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11345 		mutex_enter(&ill->ill_lock);
11346 		for (ipif = ill->ill_ipif; ipif != NULL;
11347 		    ipif = ipif->ipif_next) {
11348 			if (IPIF_IS_CONDEMNED(ipif))
11349 				continue;
11350 			if (!(ipif->ipif_flags & IPIF_UP))
11351 				continue;
11352 			if ((ipif->ipif_subnet & net_mask) ==
11353 			    (addr & net_mask)) {
11354 				/*
11355 				 * Don't trust pt-pt interfaces if there are
11356 				 * other interfaces.
11357 				 */
11358 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11359 					if (fallback_ipif == NULL) {
11360 						ipif_refhold_locked(ipif);
11361 						fallback_ipif = ipif;
11362 					}
11363 					continue;
11364 				}
11365 
11366 				/*
11367 				 * Fine. Just assume the same net mask as the
11368 				 * directly attached subnet interface is using.
11369 				 */
11370 				ipif_refhold_locked(ipif);
11371 				mutex_exit(&ill->ill_lock);
11372 				rw_exit(&ipst->ips_ill_g_lock);
11373 				if (fallback_ipif != NULL)
11374 					ipif_refrele(fallback_ipif);
11375 				*ipifp = ipif;
11376 				return (ipif->ipif_net_mask);
11377 			}
11378 		}
11379 		mutex_exit(&ill->ill_lock);
11380 	}
11381 	rw_exit(&ipst->ips_ill_g_lock);
11382 
11383 	*ipifp = fallback_ipif;
11384 	return ((fallback_ipif != NULL) ?
11385 	    fallback_ipif->ipif_net_mask : net_mask);
11386 }
11387 
11388 /*
11389  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11390  */
11391 static void
11392 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11393 {
11394 	IOCP	iocp;
11395 	ipft_t	*ipft;
11396 	ipllc_t	*ipllc;
11397 	mblk_t	*mp1;
11398 	cred_t	*cr;
11399 	int	error = 0;
11400 	conn_t	*connp;
11401 
11402 	ip1dbg(("ip_wput_ioctl"));
11403 	iocp = (IOCP)mp->b_rptr;
11404 	mp1 = mp->b_cont;
11405 	if (mp1 == NULL) {
11406 		iocp->ioc_error = EINVAL;
11407 		mp->b_datap->db_type = M_IOCNAK;
11408 		iocp->ioc_count = 0;
11409 		qreply(q, mp);
11410 		return;
11411 	}
11412 
11413 	/*
11414 	 * These IOCTLs provide various control capabilities to
11415 	 * upstream agents such as ULPs and processes.	There
11416 	 * are currently two such IOCTLs implemented.  They
11417 	 * are used by TCP to provide update information for
11418 	 * existing IREs and to forcibly delete an IRE for a
11419 	 * host that is not responding, thereby forcing an
11420 	 * attempt at a new route.
11421 	 */
11422 	iocp->ioc_error = EINVAL;
11423 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11424 		goto done;
11425 
11426 	ipllc = (ipllc_t *)mp1->b_rptr;
11427 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11428 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11429 			break;
11430 	}
11431 	/*
11432 	 * prefer credential from mblk over ioctl;
11433 	 * see ip_sioctl_copyin_setup
11434 	 */
11435 	cr = msg_getcred(mp, NULL);
11436 	if (cr == NULL)
11437 		cr = iocp->ioc_cr;
11438 
11439 	/*
11440 	 * Refhold the conn in case the request gets queued up in some lookup
11441 	 */
11442 	ASSERT(CONN_Q(q));
11443 	connp = Q_TO_CONN(q);
11444 	CONN_INC_REF(connp);
11445 	if (ipft->ipft_pfi &&
11446 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11447 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11448 		error = (*ipft->ipft_pfi)(q,
11449 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11450 	}
11451 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11452 		/*
11453 		 * CONN_OPER_PENDING_DONE happens in the function called
11454 		 * through ipft_pfi above.
11455 		 */
11456 		return;
11457 	}
11458 
11459 	CONN_OPER_PENDING_DONE(connp);
11460 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11461 		freemsg(mp);
11462 		return;
11463 	}
11464 	iocp->ioc_error = error;
11465 
11466 done:
11467 	mp->b_datap->db_type = M_IOCACK;
11468 	if (iocp->ioc_error)
11469 		iocp->ioc_count = 0;
11470 	qreply(q, mp);
11471 }
11472 
11473 /*
11474  * Assign a unique id for the ipif. This is used by sctp_addr.c
11475  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11476  */
11477 static void
11478 ipif_assign_seqid(ipif_t *ipif)
11479 {
11480 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11481 
11482 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11483 }
11484 
11485 /*
11486  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11487  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11488  * that the clone is complete -- including the seqid -- and the expectation is
11489  * that the caller will either free or overwrite `sipif' before it's unlocked.
11490  */
11491 static void
11492 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11493 {
11494 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11495 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11496 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11497 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11498 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11499 
11500 	dipif->ipif_flags = sipif->ipif_flags;
11501 	dipif->ipif_metric = sipif->ipif_metric;
11502 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11503 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11504 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11505 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11506 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11507 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11508 
11509 	/*
11510 	 * As per the comment atop the function, we assume that these sipif
11511 	 * fields will be changed before sipif is unlocked.
11512 	 */
11513 	dipif->ipif_seqid = sipif->ipif_seqid;
11514 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11515 }
11516 
11517 /*
11518  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11519  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11520  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11521  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11522  * down (i.e., no DAD), of the same type, and unlocked.
11523  */
11524 static void
11525 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11526 {
11527 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11528 	ipxop_t *ipx = ipsq->ipsq_xop;
11529 
11530 	ASSERT(sipif != dipif);
11531 	ASSERT(sipif != virgipif);
11532 
11533 	/*
11534 	 * Grab all of the locks that protect the ipif in a defined order.
11535 	 */
11536 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11537 
11538 	ipif_clone(sipif, dipif);
11539 	if (virgipif != NULL) {
11540 		ipif_clone(virgipif, sipif);
11541 		mi_free(virgipif);
11542 	}
11543 
11544 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11545 
11546 	/*
11547 	 * Transfer ownership of the current xop, if necessary.
11548 	 */
11549 	if (ipx->ipx_current_ipif == sipif) {
11550 		ASSERT(ipx->ipx_pending_ipif == NULL);
11551 		mutex_enter(&ipx->ipx_lock);
11552 		ipx->ipx_current_ipif = dipif;
11553 		mutex_exit(&ipx->ipx_lock);
11554 	}
11555 
11556 	if (virgipif == NULL)
11557 		mi_free(sipif);
11558 }
11559 
11560 /*
11561  * checks if:
11562  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11563  *	- logical interface is within the allowed range
11564  */
11565 static int
11566 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11567 {
11568 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11569 		return (ENAMETOOLONG);
11570 
11571 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11572 		return (ERANGE);
11573 	return (0);
11574 }
11575 
11576 /*
11577  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11578  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11579  * be inserted into the first space available in the list. The value of
11580  * ipif_id will then be set to the appropriate value for its position.
11581  */
11582 static int
11583 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11584 {
11585 	ill_t *ill;
11586 	ipif_t *tipif;
11587 	ipif_t **tipifp;
11588 	int id, err;
11589 	ip_stack_t	*ipst;
11590 
11591 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11592 	    IAM_WRITER_IPIF(ipif));
11593 
11594 	ill = ipif->ipif_ill;
11595 	ASSERT(ill != NULL);
11596 	ipst = ill->ill_ipst;
11597 
11598 	/*
11599 	 * In the case of lo0:0 we already hold the ill_g_lock.
11600 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11601 	 * ipif_insert.
11602 	 */
11603 	if (acquire_g_lock)
11604 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11605 	mutex_enter(&ill->ill_lock);
11606 	id = ipif->ipif_id;
11607 	tipifp = &(ill->ill_ipif);
11608 	if (id == -1) {	/* need to find a real id */
11609 		id = 0;
11610 		while ((tipif = *tipifp) != NULL) {
11611 			ASSERT(tipif->ipif_id >= id);
11612 			if (tipif->ipif_id != id)
11613 				break; /* non-consecutive id */
11614 			id++;
11615 			tipifp = &(tipif->ipif_next);
11616 		}
11617 		if ((err = is_lifname_valid(ill, id)) != 0) {
11618 			mutex_exit(&ill->ill_lock);
11619 			if (acquire_g_lock)
11620 				rw_exit(&ipst->ips_ill_g_lock);
11621 			return (err);
11622 		}
11623 		ipif->ipif_id = id; /* assign new id */
11624 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11625 		/* we have a real id; insert ipif in the right place */
11626 		while ((tipif = *tipifp) != NULL) {
11627 			ASSERT(tipif->ipif_id != id);
11628 			if (tipif->ipif_id > id)
11629 				break; /* found correct location */
11630 			tipifp = &(tipif->ipif_next);
11631 		}
11632 	} else {
11633 		mutex_exit(&ill->ill_lock);
11634 		if (acquire_g_lock)
11635 			rw_exit(&ipst->ips_ill_g_lock);
11636 		return (err);
11637 	}
11638 
11639 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11640 
11641 	ipif->ipif_next = tipif;
11642 	*tipifp = ipif;
11643 	mutex_exit(&ill->ill_lock);
11644 	if (acquire_g_lock)
11645 		rw_exit(&ipst->ips_ill_g_lock);
11646 
11647 	return (0);
11648 }
11649 
11650 static void
11651 ipif_remove(ipif_t *ipif)
11652 {
11653 	ipif_t	**ipifp;
11654 	ill_t	*ill = ipif->ipif_ill;
11655 
11656 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11657 
11658 	mutex_enter(&ill->ill_lock);
11659 	ipifp = &ill->ill_ipif;
11660 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11661 		if (*ipifp == ipif) {
11662 			*ipifp = ipif->ipif_next;
11663 			break;
11664 		}
11665 	}
11666 	mutex_exit(&ill->ill_lock);
11667 }
11668 
11669 /*
11670  * Allocate and initialize a new interface control structure.  (Always
11671  * called as writer.)
11672  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11673  * is not part of the global linked list of ills. ipif_seqid is unique
11674  * in the system and to preserve the uniqueness, it is assigned only
11675  * when ill becomes part of the global list. At that point ill will
11676  * have a name. If it doesn't get assigned here, it will get assigned
11677  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11678  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11679  * the interface flags or any other information from the DL_INFO_ACK for
11680  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11681  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11682  * second DL_INFO_ACK comes in from the driver.
11683  */
11684 static ipif_t *
11685 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11686     boolean_t insert, int *errorp)
11687 {
11688 	int err;
11689 	ipif_t	*ipif;
11690 	ip_stack_t *ipst = ill->ill_ipst;
11691 
11692 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11693 	    ill->ill_name, id, (void *)ill));
11694 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11695 
11696 	if (errorp != NULL)
11697 		*errorp = 0;
11698 
11699 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11700 		if (errorp != NULL)
11701 			*errorp = ENOMEM;
11702 		return (NULL);
11703 	}
11704 	*ipif = ipif_zero;	/* start clean */
11705 
11706 	ipif->ipif_ill = ill;
11707 	ipif->ipif_id = id;	/* could be -1 */
11708 	/*
11709 	 * Inherit the zoneid from the ill; for the shared stack instance
11710 	 * this is always the global zone
11711 	 */
11712 	ipif->ipif_zoneid = ill->ill_zoneid;
11713 
11714 	ipif->ipif_refcnt = 0;
11715 
11716 	if (insert) {
11717 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11718 			mi_free(ipif);
11719 			if (errorp != NULL)
11720 				*errorp = err;
11721 			return (NULL);
11722 		}
11723 		/* -1 id should have been replaced by real id */
11724 		id = ipif->ipif_id;
11725 		ASSERT(id >= 0);
11726 	}
11727 
11728 	if (ill->ill_name[0] != '\0')
11729 		ipif_assign_seqid(ipif);
11730 
11731 	/*
11732 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11733 	 * (which must not exist yet because the zeroth ipif is created once
11734 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11735 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11736 	 */
11737 	if (id == 0 && IS_IPMP(ill)) {
11738 		if (ipmp_illgrp_create(ill) == NULL) {
11739 			if (insert) {
11740 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11741 				ipif_remove(ipif);
11742 				rw_exit(&ipst->ips_ill_g_lock);
11743 			}
11744 			mi_free(ipif);
11745 			if (errorp != NULL)
11746 				*errorp = ENOMEM;
11747 			return (NULL);
11748 		}
11749 	}
11750 
11751 	/*
11752 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11753 	 * not up and can't be looked up until the ioctl completes and the
11754 	 * IPIF_CHANGING flag is cleared.
11755 	 */
11756 	mutex_enter(&ill->ill_lock);
11757 
11758 	ipif->ipif_ire_type = ire_type;
11759 
11760 	if (ipif->ipif_isv6) {
11761 		ill->ill_flags |= ILLF_IPV6;
11762 	} else {
11763 		ipaddr_t inaddr_any = INADDR_ANY;
11764 
11765 		ill->ill_flags |= ILLF_IPV4;
11766 
11767 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11768 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11769 		    &ipif->ipif_v6lcl_addr);
11770 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11771 		    &ipif->ipif_v6subnet);
11772 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11773 		    &ipif->ipif_v6net_mask);
11774 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11775 		    &ipif->ipif_v6brd_addr);
11776 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11777 		    &ipif->ipif_v6pp_dst_addr);
11778 	}
11779 
11780 	/*
11781 	 * Don't set the interface flags etc. now, will do it in
11782 	 * ip_ll_subnet_defaults.
11783 	 */
11784 	if (!initialize)
11785 		goto out;
11786 
11787 	/*
11788 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11789 	 * with no underlying interfaces (and thus an unknown broadcast
11790 	 * address length), but all interfaces that can be placed into an IPMP
11791 	 * group are required to be broadcast-capable.
11792 	 */
11793 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11794 		/*
11795 		 * Later detect lack of DLPI driver multicast capability by
11796 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11797 		 */
11798 		ill->ill_flags |= ILLF_MULTICAST;
11799 		if (!ipif->ipif_isv6)
11800 			ipif->ipif_flags |= IPIF_BROADCAST;
11801 	} else {
11802 		if (ill->ill_net_type != IRE_LOOPBACK) {
11803 			if (ipif->ipif_isv6)
11804 				/*
11805 				 * Note: xresolv interfaces will eventually need
11806 				 * NOARP set here as well, but that will require
11807 				 * those external resolvers to have some
11808 				 * knowledge of that flag and act appropriately.
11809 				 * Not to be changed at present.
11810 				 */
11811 				ill->ill_flags |= ILLF_NONUD;
11812 			else
11813 				ill->ill_flags |= ILLF_NOARP;
11814 		}
11815 		if (ill->ill_phys_addr_length == 0) {
11816 			if (IS_VNI(ill)) {
11817 				ipif->ipif_flags |= IPIF_NOXMIT;
11818 			} else {
11819 				/* pt-pt supports multicast. */
11820 				ill->ill_flags |= ILLF_MULTICAST;
11821 				if (ill->ill_net_type != IRE_LOOPBACK)
11822 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11823 			}
11824 		}
11825 	}
11826 out:
11827 	mutex_exit(&ill->ill_lock);
11828 	return (ipif);
11829 }
11830 
11831 /*
11832  * Remove the neighbor cache entries associated with this logical
11833  * interface.
11834  */
11835 int
11836 ipif_arp_down(ipif_t *ipif)
11837 {
11838 	ill_t	*ill = ipif->ipif_ill;
11839 	int	err = 0;
11840 
11841 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11842 	ASSERT(IAM_WRITER_IPIF(ipif));
11843 
11844 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11845 	    ill_t *, ill, ipif_t *, ipif);
11846 	ipif_nce_down(ipif);
11847 
11848 	/*
11849 	 * If this is the last ipif that is going down and there are no
11850 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11851 	 * clean up ARP completely.
11852 	 */
11853 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11854 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11855 		/*
11856 		 * If this was the last ipif on an IPMP interface, purge any
11857 		 * static ARP entries associated with it.
11858 		 */
11859 		if (IS_IPMP(ill))
11860 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11861 
11862 		/* UNBIND, DETACH */
11863 		err = arp_ll_down(ill);
11864 	}
11865 
11866 	return (err);
11867 }
11868 
11869 /*
11870  * Get the resolver set up for a new IP address.  (Always called as writer.)
11871  * Called both for IPv4 and IPv6 interfaces, though it only does some
11872  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11873  *
11874  * The enumerated value res_act tunes the behavior:
11875  * 	* Res_act_initial: set up all the resolver structures for a new
11876  *	  IP address.
11877  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11878  *	  ARP message in defense of the address.
11879  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11880  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11881  *
11882  * Returns zero on success, or an errno upon failure.
11883  */
11884 int
11885 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11886 {
11887 	ill_t		*ill = ipif->ipif_ill;
11888 	int		err;
11889 	boolean_t	was_dup;
11890 
11891 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11892 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11893 	ASSERT(IAM_WRITER_IPIF(ipif));
11894 
11895 	was_dup = B_FALSE;
11896 	if (res_act == Res_act_initial) {
11897 		ipif->ipif_addr_ready = 0;
11898 		/*
11899 		 * We're bringing an interface up here.  There's no way that we
11900 		 * should need to shut down ARP now.
11901 		 */
11902 		mutex_enter(&ill->ill_lock);
11903 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11904 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11905 			ill->ill_ipif_dup_count--;
11906 			was_dup = B_TRUE;
11907 		}
11908 		mutex_exit(&ill->ill_lock);
11909 	}
11910 	if (ipif->ipif_recovery_id != 0)
11911 		(void) untimeout(ipif->ipif_recovery_id);
11912 	ipif->ipif_recovery_id = 0;
11913 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11914 		ipif->ipif_addr_ready = 1;
11915 		return (0);
11916 	}
11917 	/* NDP will set the ipif_addr_ready flag when it's ready */
11918 	if (ill->ill_isv6)
11919 		return (0);
11920 
11921 	err = ipif_arp_up(ipif, res_act, was_dup);
11922 	return (err);
11923 }
11924 
11925 /*
11926  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11927  * when a link has just gone back up.
11928  */
11929 static void
11930 ipif_nce_start_dad(ipif_t *ipif)
11931 {
11932 	ncec_t *ncec;
11933 	ill_t *ill = ipif->ipif_ill;
11934 	boolean_t isv6 = ill->ill_isv6;
11935 
11936 	if (isv6) {
11937 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11938 		    &ipif->ipif_v6lcl_addr);
11939 	} else {
11940 		ipaddr_t v4addr;
11941 
11942 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11943 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11944 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11945 			/*
11946 			 * If we can't contact ARP for some reason,
11947 			 * that's not really a problem.  Just send
11948 			 * out the routing socket notification that
11949 			 * DAD completion would have done, and continue.
11950 			 */
11951 			ipif_mask_reply(ipif);
11952 			ipif_up_notify(ipif);
11953 			ipif->ipif_addr_ready = 1;
11954 			return;
11955 		}
11956 
11957 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11958 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11959 	}
11960 
11961 	if (ncec == NULL) {
11962 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11963 		    (void *)ipif));
11964 		return;
11965 	}
11966 	if (!nce_restart_dad(ncec)) {
11967 		/*
11968 		 * If we can't restart DAD for some reason, that's not really a
11969 		 * problem.  Just send out the routing socket notification that
11970 		 * DAD completion would have done, and continue.
11971 		 */
11972 		ipif_up_notify(ipif);
11973 		ipif->ipif_addr_ready = 1;
11974 	}
11975 	ncec_refrele(ncec);
11976 }
11977 
11978 /*
11979  * Restart duplicate address detection on all interfaces on the given ill.
11980  *
11981  * This is called when an interface transitions from down to up
11982  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11983  *
11984  * Note that since the underlying physical link has transitioned, we must cause
11985  * at least one routing socket message to be sent here, either via DAD
11986  * completion or just by default on the first ipif.  (If we don't do this, then
11987  * in.mpathd will see long delays when doing link-based failure recovery.)
11988  */
11989 void
11990 ill_restart_dad(ill_t *ill, boolean_t went_up)
11991 {
11992 	ipif_t *ipif;
11993 
11994 	if (ill == NULL)
11995 		return;
11996 
11997 	/*
11998 	 * If layer two doesn't support duplicate address detection, then just
11999 	 * send the routing socket message now and be done with it.
12000 	 */
12001 	if (!ill->ill_isv6 && arp_no_defense) {
12002 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12003 		return;
12004 	}
12005 
12006 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12007 		if (went_up) {
12008 
12009 			if (ipif->ipif_flags & IPIF_UP) {
12010 				ipif_nce_start_dad(ipif);
12011 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12012 				/*
12013 				 * kick off the bring-up process now.
12014 				 */
12015 				ipif_do_recovery(ipif);
12016 			} else {
12017 				/*
12018 				 * Unfortunately, the first ipif is "special"
12019 				 * and represents the underlying ill in the
12020 				 * routing socket messages.  Thus, when this
12021 				 * one ipif is down, we must still notify so
12022 				 * that the user knows the IFF_RUNNING status
12023 				 * change.  (If the first ipif is up, then
12024 				 * we'll handle eventual routing socket
12025 				 * notification via DAD completion.)
12026 				 */
12027 				if (ipif == ill->ill_ipif) {
12028 					ip_rts_ifmsg(ill->ill_ipif,
12029 					    RTSQ_DEFAULT);
12030 				}
12031 			}
12032 		} else {
12033 			/*
12034 			 * After link down, we'll need to send a new routing
12035 			 * message when the link comes back, so clear
12036 			 * ipif_addr_ready.
12037 			 */
12038 			ipif->ipif_addr_ready = 0;
12039 		}
12040 	}
12041 
12042 	/*
12043 	 * If we've torn down links, then notify the user right away.
12044 	 */
12045 	if (!went_up)
12046 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12047 }
12048 
12049 static void
12050 ipsq_delete(ipsq_t *ipsq)
12051 {
12052 	ipxop_t *ipx = ipsq->ipsq_xop;
12053 
12054 	ipsq->ipsq_ipst = NULL;
12055 	ASSERT(ipsq->ipsq_phyint == NULL);
12056 	ASSERT(ipsq->ipsq_xop != NULL);
12057 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12058 	ASSERT(ipx->ipx_pending_mp == NULL);
12059 	kmem_free(ipsq, sizeof (ipsq_t));
12060 }
12061 
12062 static int
12063 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12064 {
12065 	int err = 0;
12066 	ipif_t *ipif;
12067 
12068 	if (ill == NULL)
12069 		return (0);
12070 
12071 	ASSERT(IAM_WRITER_ILL(ill));
12072 	ill->ill_up_ipifs = B_TRUE;
12073 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12074 		if (ipif->ipif_was_up) {
12075 			if (!(ipif->ipif_flags & IPIF_UP))
12076 				err = ipif_up(ipif, q, mp);
12077 			ipif->ipif_was_up = B_FALSE;
12078 			if (err != 0) {
12079 				ASSERT(err == EINPROGRESS);
12080 				return (err);
12081 			}
12082 		}
12083 	}
12084 	ill->ill_up_ipifs = B_FALSE;
12085 	return (0);
12086 }
12087 
12088 /*
12089  * This function is called to bring up all the ipifs that were up before
12090  * bringing the ill down via ill_down_ipifs().
12091  */
12092 int
12093 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12094 {
12095 	int err;
12096 
12097 	ASSERT(IAM_WRITER_ILL(ill));
12098 
12099 	if (ill->ill_replumbing) {
12100 		ill->ill_replumbing = 0;
12101 		/*
12102 		 * Send down REPLUMB_DONE notification followed by the
12103 		 * BIND_REQ on the arp stream.
12104 		 */
12105 		if (!ill->ill_isv6)
12106 			arp_send_replumb_conf(ill);
12107 	}
12108 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12109 	if (err != 0)
12110 		return (err);
12111 
12112 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12113 }
12114 
12115 /*
12116  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12117  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12118  */
12119 static void
12120 ill_down_ipifs(ill_t *ill, boolean_t logical)
12121 {
12122 	ipif_t *ipif;
12123 
12124 	ASSERT(IAM_WRITER_ILL(ill));
12125 
12126 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12127 		/*
12128 		 * We go through the ipif_down logic even if the ipif
12129 		 * is already down, since routes can be added based
12130 		 * on down ipifs. Going through ipif_down once again
12131 		 * will delete any IREs created based on these routes.
12132 		 */
12133 		if (ipif->ipif_flags & IPIF_UP)
12134 			ipif->ipif_was_up = B_TRUE;
12135 
12136 		if (logical) {
12137 			(void) ipif_logical_down(ipif, NULL, NULL);
12138 			ipif_non_duplicate(ipif);
12139 			(void) ipif_down_tail(ipif);
12140 		} else {
12141 			(void) ipif_down(ipif, NULL, NULL);
12142 		}
12143 	}
12144 }
12145 
12146 /*
12147  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12148  * a look again at valid source addresses.
12149  * This should be called each time after the set of source addresses has been
12150  * changed.
12151  */
12152 void
12153 ip_update_source_selection(ip_stack_t *ipst)
12154 {
12155 	/* We skip past SRC_GENERATION_VERIFY */
12156 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12157 	    SRC_GENERATION_VERIFY)
12158 		atomic_add_32(&ipst->ips_src_generation, 1);
12159 }
12160 
12161 /*
12162  * Finish the group join started in ip_sioctl_groupname().
12163  */
12164 /* ARGSUSED */
12165 static void
12166 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12167 {
12168 	ill_t		*ill = q->q_ptr;
12169 	phyint_t	*phyi = ill->ill_phyint;
12170 	ipmp_grp_t	*grp = phyi->phyint_grp;
12171 	ip_stack_t	*ipst = ill->ill_ipst;
12172 
12173 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12174 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12175 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12176 
12177 	if (phyi->phyint_illv4 != NULL) {
12178 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12179 		VERIFY(grp->gr_pendv4-- > 0);
12180 		rw_exit(&ipst->ips_ipmp_lock);
12181 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12182 	}
12183 	if (phyi->phyint_illv6 != NULL) {
12184 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12185 		VERIFY(grp->gr_pendv6-- > 0);
12186 		rw_exit(&ipst->ips_ipmp_lock);
12187 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12188 	}
12189 	freemsg(mp);
12190 }
12191 
12192 /*
12193  * Process an SIOCSLIFGROUPNAME request.
12194  */
12195 /* ARGSUSED */
12196 int
12197 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12198     ip_ioctl_cmd_t *ipip, void *ifreq)
12199 {
12200 	struct lifreq	*lifr = ifreq;
12201 	ill_t		*ill = ipif->ipif_ill;
12202 	ip_stack_t	*ipst = ill->ill_ipst;
12203 	phyint_t	*phyi = ill->ill_phyint;
12204 	ipmp_grp_t	*grp = phyi->phyint_grp;
12205 	mblk_t		*ipsq_mp;
12206 	int		err = 0;
12207 
12208 	/*
12209 	 * Note that phyint_grp can only change here, where we're exclusive.
12210 	 */
12211 	ASSERT(IAM_WRITER_ILL(ill));
12212 
12213 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12214 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12215 		return (EINVAL);
12216 
12217 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12218 
12219 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12220 
12221 	/*
12222 	 * If the name hasn't changed, there's nothing to do.
12223 	 */
12224 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12225 		goto unlock;
12226 
12227 	/*
12228 	 * Handle requests to rename an IPMP meta-interface.
12229 	 *
12230 	 * Note that creation of the IPMP meta-interface is handled in
12231 	 * userland through the standard plumbing sequence.  As part of the
12232 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12233 	 * the name of the interface (see ipif_set_values_tail()).
12234 	 */
12235 	if (IS_IPMP(ill)) {
12236 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12237 		goto unlock;
12238 	}
12239 
12240 	/*
12241 	 * Handle requests to add or remove an IP interface from a group.
12242 	 */
12243 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12244 		/*
12245 		 * Moves are handled by first removing the interface from
12246 		 * its existing group, and then adding it to another group.
12247 		 * So, fail if it's already in a group.
12248 		 */
12249 		if (IS_UNDER_IPMP(ill)) {
12250 			err = EALREADY;
12251 			goto unlock;
12252 		}
12253 
12254 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12255 		if (grp == NULL) {
12256 			err = ENOENT;
12257 			goto unlock;
12258 		}
12259 
12260 		/*
12261 		 * Check if the phyint and its ills are suitable for
12262 		 * inclusion into the group.
12263 		 */
12264 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12265 			goto unlock;
12266 
12267 		/*
12268 		 * Checks pass; join the group, and enqueue the remaining
12269 		 * illgrp joins for when we've become part of the group xop
12270 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12271 		 * requires an mblk_t to scribble on, and since `mp' will be
12272 		 * freed as part of completing the ioctl, allocate another.
12273 		 */
12274 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12275 			err = ENOMEM;
12276 			goto unlock;
12277 		}
12278 
12279 		/*
12280 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12281 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12282 		 * before ip_join_illgrps() is called back.  See the comments
12283 		 * in ip_sioctl_plink_ipmp() for more.
12284 		 */
12285 		if (phyi->phyint_illv4 != NULL)
12286 			grp->gr_pendv4++;
12287 		if (phyi->phyint_illv6 != NULL)
12288 			grp->gr_pendv6++;
12289 
12290 		rw_exit(&ipst->ips_ipmp_lock);
12291 
12292 		ipmp_phyint_join_grp(phyi, grp);
12293 		ill_refhold(ill);
12294 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12295 		    SWITCH_OP, B_FALSE);
12296 		return (0);
12297 	} else {
12298 		/*
12299 		 * Request to remove the interface from a group.  If the
12300 		 * interface is not in a group, this trivially succeeds.
12301 		 */
12302 		rw_exit(&ipst->ips_ipmp_lock);
12303 		if (IS_UNDER_IPMP(ill))
12304 			ipmp_phyint_leave_grp(phyi);
12305 		return (0);
12306 	}
12307 unlock:
12308 	rw_exit(&ipst->ips_ipmp_lock);
12309 	return (err);
12310 }
12311 
12312 /*
12313  * Process an SIOCGLIFBINDING request.
12314  */
12315 /* ARGSUSED */
12316 int
12317 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12318     ip_ioctl_cmd_t *ipip, void *ifreq)
12319 {
12320 	ill_t		*ill;
12321 	struct lifreq	*lifr = ifreq;
12322 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12323 
12324 	if (!IS_IPMP(ipif->ipif_ill))
12325 		return (EINVAL);
12326 
12327 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12328 	if ((ill = ipif->ipif_bound_ill) == NULL)
12329 		lifr->lifr_binding[0] = '\0';
12330 	else
12331 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12332 	rw_exit(&ipst->ips_ipmp_lock);
12333 	return (0);
12334 }
12335 
12336 /*
12337  * Process an SIOCGLIFGROUPNAME request.
12338  */
12339 /* ARGSUSED */
12340 int
12341 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12342     ip_ioctl_cmd_t *ipip, void *ifreq)
12343 {
12344 	ipmp_grp_t	*grp;
12345 	struct lifreq	*lifr = ifreq;
12346 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12347 
12348 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12349 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12350 		lifr->lifr_groupname[0] = '\0';
12351 	else
12352 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12353 	rw_exit(&ipst->ips_ipmp_lock);
12354 	return (0);
12355 }
12356 
12357 /*
12358  * Process an SIOCGLIFGROUPINFO request.
12359  */
12360 /* ARGSUSED */
12361 int
12362 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12363     ip_ioctl_cmd_t *ipip, void *dummy)
12364 {
12365 	ipmp_grp_t	*grp;
12366 	lifgroupinfo_t	*lifgr;
12367 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12368 
12369 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12370 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12371 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12372 
12373 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12374 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12375 		rw_exit(&ipst->ips_ipmp_lock);
12376 		return (ENOENT);
12377 	}
12378 	ipmp_grp_info(grp, lifgr);
12379 	rw_exit(&ipst->ips_ipmp_lock);
12380 	return (0);
12381 }
12382 
12383 static void
12384 ill_dl_down(ill_t *ill)
12385 {
12386 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12387 
12388 	/*
12389 	 * The ill is down; unbind but stay attached since we're still
12390 	 * associated with a PPA. If we have negotiated DLPI capabilites
12391 	 * with the data link service provider (IDS_OK) then reset them.
12392 	 * The interval between unbinding and rebinding is potentially
12393 	 * unbounded hence we cannot assume things will be the same.
12394 	 * The DLPI capabilities will be probed again when the data link
12395 	 * is brought up.
12396 	 */
12397 	mblk_t	*mp = ill->ill_unbind_mp;
12398 
12399 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12400 
12401 	if (!ill->ill_replumbing) {
12402 		/* Free all ilms for this ill */
12403 		update_conn_ill(ill, ill->ill_ipst);
12404 	} else {
12405 		ill_leave_multicast(ill);
12406 	}
12407 
12408 	ill->ill_unbind_mp = NULL;
12409 	if (mp != NULL) {
12410 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12411 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12412 		    ill->ill_name));
12413 		mutex_enter(&ill->ill_lock);
12414 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12415 		mutex_exit(&ill->ill_lock);
12416 		/*
12417 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12418 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12419 		 * ill_capability_dld_disable disable rightaway. If this is not
12420 		 * an unplumb operation then the disable happens on receipt of
12421 		 * the capab ack via ip_rput_dlpi_writer ->
12422 		 * ill_capability_ack_thr. In both cases the order of
12423 		 * the operations seen by DLD is capability disable followed
12424 		 * by DL_UNBIND. Also the DLD capability disable needs a
12425 		 * cv_wait'able context.
12426 		 */
12427 		if (ill->ill_state_flags & ILL_CONDEMNED)
12428 			ill_capability_dld_disable(ill);
12429 		ill_capability_reset(ill, B_FALSE);
12430 		ill_dlpi_send(ill, mp);
12431 	}
12432 	mutex_enter(&ill->ill_lock);
12433 	ill->ill_dl_up = 0;
12434 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12435 	mutex_exit(&ill->ill_lock);
12436 }
12437 
12438 void
12439 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12440 {
12441 	union DL_primitives *dlp;
12442 	t_uscalar_t prim;
12443 	boolean_t waitack = B_FALSE;
12444 
12445 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12446 
12447 	dlp = (union DL_primitives *)mp->b_rptr;
12448 	prim = dlp->dl_primitive;
12449 
12450 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12451 	    dl_primstr(prim), prim, ill->ill_name));
12452 
12453 	switch (prim) {
12454 	case DL_PHYS_ADDR_REQ:
12455 	{
12456 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12457 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12458 		break;
12459 	}
12460 	case DL_BIND_REQ:
12461 		mutex_enter(&ill->ill_lock);
12462 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12463 		mutex_exit(&ill->ill_lock);
12464 		break;
12465 	}
12466 
12467 	/*
12468 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12469 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12470 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12471 	 */
12472 	mutex_enter(&ill->ill_lock);
12473 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12474 	    (prim == DL_UNBIND_REQ)) {
12475 		ill->ill_dlpi_pending = prim;
12476 		waitack = B_TRUE;
12477 	}
12478 
12479 	mutex_exit(&ill->ill_lock);
12480 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12481 	    char *, dl_primstr(prim), ill_t *, ill);
12482 	putnext(ill->ill_wq, mp);
12483 
12484 	/*
12485 	 * There is no ack for DL_NOTIFY_CONF messages
12486 	 */
12487 	if (waitack && prim == DL_NOTIFY_CONF)
12488 		ill_dlpi_done(ill, prim);
12489 }
12490 
12491 /*
12492  * Helper function for ill_dlpi_send().
12493  */
12494 /* ARGSUSED */
12495 static void
12496 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12497 {
12498 	ill_dlpi_send(q->q_ptr, mp);
12499 }
12500 
12501 /*
12502  * Send a DLPI control message to the driver but make sure there
12503  * is only one outstanding message. Uses ill_dlpi_pending to tell
12504  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12505  * when an ACK or a NAK is received to process the next queued message.
12506  */
12507 void
12508 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12509 {
12510 	mblk_t **mpp;
12511 
12512 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12513 
12514 	/*
12515 	 * To ensure that any DLPI requests for current exclusive operation
12516 	 * are always completely sent before any DLPI messages for other
12517 	 * operations, require writer access before enqueuing.
12518 	 */
12519 	if (!IAM_WRITER_ILL(ill)) {
12520 		ill_refhold(ill);
12521 		/* qwriter_ip() does the ill_refrele() */
12522 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12523 		    NEW_OP, B_TRUE);
12524 		return;
12525 	}
12526 
12527 	mutex_enter(&ill->ill_lock);
12528 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12529 		/* Must queue message. Tail insertion */
12530 		mpp = &ill->ill_dlpi_deferred;
12531 		while (*mpp != NULL)
12532 			mpp = &((*mpp)->b_next);
12533 
12534 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12535 		    "while %s pending\n", ill->ill_name,
12536 		    dl_primstr(ill->ill_dlpi_pending)));
12537 
12538 		*mpp = mp;
12539 		mutex_exit(&ill->ill_lock);
12540 		return;
12541 	}
12542 	mutex_exit(&ill->ill_lock);
12543 	ill_dlpi_dispatch(ill, mp);
12544 }
12545 
12546 void
12547 ill_capability_send(ill_t *ill, mblk_t *mp)
12548 {
12549 	ill->ill_capab_pending_cnt++;
12550 	ill_dlpi_send(ill, mp);
12551 }
12552 
12553 void
12554 ill_capability_done(ill_t *ill)
12555 {
12556 	ASSERT(ill->ill_capab_pending_cnt != 0);
12557 
12558 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12559 
12560 	ill->ill_capab_pending_cnt--;
12561 	if (ill->ill_capab_pending_cnt == 0 &&
12562 	    ill->ill_dlpi_capab_state == IDCS_OK)
12563 		ill_capability_reset_alloc(ill);
12564 }
12565 
12566 /*
12567  * Send all deferred DLPI messages without waiting for their ACKs.
12568  */
12569 void
12570 ill_dlpi_send_deferred(ill_t *ill)
12571 {
12572 	mblk_t *mp, *nextmp;
12573 
12574 	/*
12575 	 * Clear ill_dlpi_pending so that the message is not queued in
12576 	 * ill_dlpi_send().
12577 	 */
12578 	mutex_enter(&ill->ill_lock);
12579 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12580 	mp = ill->ill_dlpi_deferred;
12581 	ill->ill_dlpi_deferred = NULL;
12582 	mutex_exit(&ill->ill_lock);
12583 
12584 	for (; mp != NULL; mp = nextmp) {
12585 		nextmp = mp->b_next;
12586 		mp->b_next = NULL;
12587 		ill_dlpi_send(ill, mp);
12588 	}
12589 }
12590 
12591 /*
12592  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12593  * or M_HANGUP
12594  */
12595 static void
12596 ill_dlpi_clear_deferred(ill_t *ill)
12597 {
12598 	mblk_t	*mp, *nextmp;
12599 
12600 	mutex_enter(&ill->ill_lock);
12601 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12602 	mp = ill->ill_dlpi_deferred;
12603 	ill->ill_dlpi_deferred = NULL;
12604 	mutex_exit(&ill->ill_lock);
12605 
12606 	for (; mp != NULL; mp = nextmp) {
12607 		nextmp = mp->b_next;
12608 		inet_freemsg(mp);
12609 	}
12610 }
12611 
12612 /*
12613  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12614  */
12615 boolean_t
12616 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12617 {
12618 	t_uscalar_t pending;
12619 
12620 	mutex_enter(&ill->ill_lock);
12621 	if (ill->ill_dlpi_pending == prim) {
12622 		mutex_exit(&ill->ill_lock);
12623 		return (B_TRUE);
12624 	}
12625 
12626 	/*
12627 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12628 	 * without waiting, so don't print any warnings in that case.
12629 	 */
12630 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12631 		mutex_exit(&ill->ill_lock);
12632 		return (B_FALSE);
12633 	}
12634 	pending = ill->ill_dlpi_pending;
12635 	mutex_exit(&ill->ill_lock);
12636 
12637 	if (pending == DL_PRIM_INVAL) {
12638 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12639 		    "received unsolicited ack for %s on %s\n",
12640 		    dl_primstr(prim), ill->ill_name);
12641 	} else {
12642 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12643 		    "received unexpected ack for %s on %s (expecting %s)\n",
12644 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12645 	}
12646 	return (B_FALSE);
12647 }
12648 
12649 /*
12650  * Complete the current DLPI operation associated with `prim' on `ill' and
12651  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12652  * operations and the ill's current exclusive IPSQ operation has finished
12653  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12654  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12655  * the comments above ipsq_current_finish() for details.
12656  */
12657 void
12658 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12659 {
12660 	mblk_t *mp;
12661 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12662 	ipxop_t *ipx = ipsq->ipsq_xop;
12663 
12664 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12665 	mutex_enter(&ill->ill_lock);
12666 
12667 	ASSERT(prim != DL_PRIM_INVAL);
12668 	ASSERT(ill->ill_dlpi_pending == prim);
12669 
12670 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12671 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12672 
12673 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12674 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12675 		if (ipx->ipx_current_done) {
12676 			mutex_enter(&ipx->ipx_lock);
12677 			ipx->ipx_current_ipif = NULL;
12678 			mutex_exit(&ipx->ipx_lock);
12679 		}
12680 		cv_signal(&ill->ill_cv);
12681 		mutex_exit(&ill->ill_lock);
12682 		return;
12683 	}
12684 
12685 	ill->ill_dlpi_deferred = mp->b_next;
12686 	mp->b_next = NULL;
12687 	mutex_exit(&ill->ill_lock);
12688 
12689 	ill_dlpi_dispatch(ill, mp);
12690 }
12691 
12692 /*
12693  * Queue a (multicast) DLPI control message to be sent to the driver by
12694  * later calling ill_dlpi_send_queued.
12695  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12696  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12697  * for the same group to race.
12698  * We send DLPI control messages in order using ill_lock.
12699  * For IPMP we should be called on the cast_ill.
12700  */
12701 void
12702 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12703 {
12704 	mblk_t **mpp;
12705 
12706 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12707 
12708 	mutex_enter(&ill->ill_lock);
12709 	/* Must queue message. Tail insertion */
12710 	mpp = &ill->ill_dlpi_deferred;
12711 	while (*mpp != NULL)
12712 		mpp = &((*mpp)->b_next);
12713 
12714 	*mpp = mp;
12715 	mutex_exit(&ill->ill_lock);
12716 }
12717 
12718 /*
12719  * Send the messages that were queued. Make sure there is only
12720  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12721  * when an ACK or a NAK is received to process the next queued message.
12722  * For IPMP we are called on the upper ill, but when send what is queued
12723  * on the cast_ill.
12724  */
12725 void
12726 ill_dlpi_send_queued(ill_t *ill)
12727 {
12728 	mblk_t	*mp;
12729 	union DL_primitives *dlp;
12730 	t_uscalar_t prim;
12731 	ill_t *release_ill = NULL;
12732 
12733 	if (IS_IPMP(ill)) {
12734 		/* On the upper IPMP ill. */
12735 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12736 		if (release_ill == NULL) {
12737 			/* Avoid ever sending anything down to the ipmpstub */
12738 			return;
12739 		}
12740 		ill = release_ill;
12741 	}
12742 	mutex_enter(&ill->ill_lock);
12743 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12744 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12745 			/* Can't send. Somebody else will send it */
12746 			mutex_exit(&ill->ill_lock);
12747 			goto done;
12748 		}
12749 		ill->ill_dlpi_deferred = mp->b_next;
12750 		mp->b_next = NULL;
12751 		if (!ill->ill_dl_up) {
12752 			/*
12753 			 * Nobody there. All multicast addresses will be
12754 			 * re-joined when we get the DL_BIND_ACK bringing the
12755 			 * interface up.
12756 			 */
12757 			freemsg(mp);
12758 			continue;
12759 		}
12760 		dlp = (union DL_primitives *)mp->b_rptr;
12761 		prim = dlp->dl_primitive;
12762 
12763 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12764 		    (prim == DL_UNBIND_REQ)) {
12765 			ill->ill_dlpi_pending = prim;
12766 		}
12767 		mutex_exit(&ill->ill_lock);
12768 
12769 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12770 		    char *, dl_primstr(prim), ill_t *, ill);
12771 		putnext(ill->ill_wq, mp);
12772 		mutex_enter(&ill->ill_lock);
12773 	}
12774 	mutex_exit(&ill->ill_lock);
12775 done:
12776 	if (release_ill != NULL)
12777 		ill_refrele(release_ill);
12778 }
12779 
12780 /*
12781  * Queue an IP (IGMP/MLD) message to be sent by IP from
12782  * ill_mcast_send_queued
12783  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12784  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12785  * group to race.
12786  * We send them in order using ill_lock.
12787  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12788  */
12789 void
12790 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12791 {
12792 	mblk_t **mpp;
12793 	ill_t *release_ill = NULL;
12794 
12795 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12796 
12797 	if (IS_IPMP(ill)) {
12798 		/* On the upper IPMP ill. */
12799 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12800 		if (release_ill == NULL) {
12801 			/* Discard instead of queuing for the ipmp interface */
12802 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12803 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12804 			    mp, ill);
12805 			freemsg(mp);
12806 			return;
12807 		}
12808 		ill = release_ill;
12809 	}
12810 
12811 	mutex_enter(&ill->ill_lock);
12812 	/* Must queue message. Tail insertion */
12813 	mpp = &ill->ill_mcast_deferred;
12814 	while (*mpp != NULL)
12815 		mpp = &((*mpp)->b_next);
12816 
12817 	*mpp = mp;
12818 	mutex_exit(&ill->ill_lock);
12819 	if (release_ill != NULL)
12820 		ill_refrele(release_ill);
12821 }
12822 
12823 /*
12824  * Send the IP packets that were queued by ill_mcast_queue.
12825  * These are IGMP/MLD packets.
12826  *
12827  * For IPMP we are called on the upper ill, but when send what is queued
12828  * on the cast_ill.
12829  *
12830  * Request loopback of the report if we are acting as a multicast
12831  * router, so that the process-level routing demon can hear it.
12832  * This will run multiple times for the same group if there are members
12833  * on the same group for multiple ipif's on the same ill. The
12834  * igmp_input/mld_input code will suppress this due to the loopback thus we
12835  * always loopback membership report.
12836  *
12837  * We also need to make sure that this does not get load balanced
12838  * by IPMP. We do this by passing an ill to ip_output_simple.
12839  */
12840 void
12841 ill_mcast_send_queued(ill_t *ill)
12842 {
12843 	mblk_t	*mp;
12844 	ip_xmit_attr_t ixas;
12845 	ill_t *release_ill = NULL;
12846 
12847 	if (IS_IPMP(ill)) {
12848 		/* On the upper IPMP ill. */
12849 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12850 		if (release_ill == NULL) {
12851 			/*
12852 			 * We should have no messages on the ipmp interface
12853 			 * but no point in trying to send them.
12854 			 */
12855 			return;
12856 		}
12857 		ill = release_ill;
12858 	}
12859 	bzero(&ixas, sizeof (ixas));
12860 	ixas.ixa_zoneid = ALL_ZONES;
12861 	ixas.ixa_cred = kcred;
12862 	ixas.ixa_cpid = NOPID;
12863 	ixas.ixa_tsl = NULL;
12864 	/*
12865 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12866 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12867 	 * That is necessary to handle IGMP/MLD snooping switches.
12868 	 */
12869 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12870 	ixas.ixa_ipst = ill->ill_ipst;
12871 
12872 	mutex_enter(&ill->ill_lock);
12873 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12874 		ill->ill_mcast_deferred = mp->b_next;
12875 		mp->b_next = NULL;
12876 		if (!ill->ill_dl_up) {
12877 			/*
12878 			 * Nobody there. Just drop the ip packets.
12879 			 * IGMP/MLD will resend later, if this is a replumb.
12880 			 */
12881 			freemsg(mp);
12882 			continue;
12883 		}
12884 		mutex_enter(&ill->ill_phyint->phyint_lock);
12885 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12886 			/*
12887 			 * When the ill is getting deactivated, we only want to
12888 			 * send the DLPI messages, so drop IGMP/MLD packets.
12889 			 * DLPI messages are handled by ill_dlpi_send_queued()
12890 			 */
12891 			mutex_exit(&ill->ill_phyint->phyint_lock);
12892 			freemsg(mp);
12893 			continue;
12894 		}
12895 		mutex_exit(&ill->ill_phyint->phyint_lock);
12896 		mutex_exit(&ill->ill_lock);
12897 
12898 		/* Check whether we are sending IPv4 or IPv6. */
12899 		if (ill->ill_isv6) {
12900 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12901 
12902 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12903 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12904 		} else {
12905 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12906 
12907 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12908 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12909 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12910 		}
12911 
12912 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12913 		(void) ip_output_simple(mp, &ixas);
12914 		ixa_cleanup(&ixas);
12915 
12916 		mutex_enter(&ill->ill_lock);
12917 	}
12918 	mutex_exit(&ill->ill_lock);
12919 
12920 done:
12921 	if (release_ill != NULL)
12922 		ill_refrele(release_ill);
12923 }
12924 
12925 /*
12926  * Take down a specific interface, but don't lose any information about it.
12927  * (Always called as writer.)
12928  * This function goes through the down sequence even if the interface is
12929  * already down. There are 2 reasons.
12930  * a. Currently we permit interface routes that depend on down interfaces
12931  *    to be added. This behaviour itself is questionable. However it appears
12932  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12933  *    time. We go thru the cleanup in order to remove these routes.
12934  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12935  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12936  *    down, but we need to cleanup i.e. do ill_dl_down and
12937  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12938  *
12939  * IP-MT notes:
12940  *
12941  * Model of reference to interfaces.
12942  *
12943  * The following members in ipif_t track references to the ipif.
12944  *	int     ipif_refcnt;    Active reference count
12945  *
12946  * The following members in ill_t track references to the ill.
12947  *	int             ill_refcnt;     active refcnt
12948  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12949  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12950  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12951  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12952  *
12953  * Reference to an ipif or ill can be obtained in any of the following ways.
12954  *
12955  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12956  * Pointers to ipif / ill from other data structures viz ire and conn.
12957  * Implicit reference to the ipif / ill by holding a reference to the ire.
12958  *
12959  * The ipif/ill lookup functions return a reference held ipif / ill.
12960  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12961  * This is a purely dynamic reference count associated with threads holding
12962  * references to the ipif / ill. Pointers from other structures do not
12963  * count towards this reference count.
12964  *
12965  * ill_ire_cnt is the number of ire's associated with the
12966  * ill. This is incremented whenever a new ire is created referencing the
12967  * ill. This is done atomically inside ire_add_v[46] where the ire is
12968  * actually added to the ire hash table. The count is decremented in
12969  * ire_inactive where the ire is destroyed.
12970  *
12971  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12972  * This is incremented atomically in
12973  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12974  * table. Similarly it is decremented in ncec_inactive() where the ncec
12975  * is destroyed.
12976  *
12977  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12978  * incremented atomically in nce_add() where the nce is actually added to the
12979  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12980  * is destroyed.
12981  *
12982  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12983  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12984  *
12985  * Flow of ioctls involving interface down/up
12986  *
12987  * The following is the sequence of an attempt to set some critical flags on an
12988  * up interface.
12989  * ip_sioctl_flags
12990  * ipif_down
12991  * wait for ipif to be quiescent
12992  * ipif_down_tail
12993  * ip_sioctl_flags_tail
12994  *
12995  * All set ioctls that involve down/up sequence would have a skeleton similar
12996  * to the above. All the *tail functions are called after the refcounts have
12997  * dropped to the appropriate values.
12998  *
12999  * SIOC ioctls during the IPIF_CHANGING interval.
13000  *
13001  * Threads handling SIOC set ioctls serialize on the squeue, but this
13002  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13003  * steps of internal changes to the state, some of which are visible in
13004  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13005  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13006  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13007  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13008  * the current exclusive operation completes. The IPIF_CHANGING check
13009  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13010  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13011  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13012  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13013  * until we release the ipsq_lock, even though the ill/ipif state flags
13014  * can change after we drop the ill_lock.
13015  */
13016 int
13017 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13018 {
13019 	ill_t		*ill = ipif->ipif_ill;
13020 	conn_t		*connp;
13021 	boolean_t	success;
13022 	boolean_t	ipif_was_up = B_FALSE;
13023 	ip_stack_t	*ipst = ill->ill_ipst;
13024 
13025 	ASSERT(IAM_WRITER_IPIF(ipif));
13026 
13027 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13028 
13029 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13030 	    ill_t *, ill, ipif_t *, ipif);
13031 
13032 	if (ipif->ipif_flags & IPIF_UP) {
13033 		mutex_enter(&ill->ill_lock);
13034 		ipif->ipif_flags &= ~IPIF_UP;
13035 		ASSERT(ill->ill_ipif_up_count > 0);
13036 		--ill->ill_ipif_up_count;
13037 		mutex_exit(&ill->ill_lock);
13038 		ipif_was_up = B_TRUE;
13039 		/* Update status in SCTP's list */
13040 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13041 		ill_nic_event_dispatch(ipif->ipif_ill,
13042 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13043 	}
13044 
13045 	/*
13046 	 * Blow away memberships we established in ipif_multicast_up().
13047 	 */
13048 	ipif_multicast_down(ipif);
13049 
13050 	/*
13051 	 * Remove from the mapping for __sin6_src_id. We insert only
13052 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13053 	 * stored as mapped addresses, we need to check for mapped
13054 	 * INADDR_ANY also.
13055 	 */
13056 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13057 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13058 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13059 		int err;
13060 
13061 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13062 		    ipif->ipif_zoneid, ipst);
13063 		if (err != 0) {
13064 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13065 		}
13066 	}
13067 
13068 	if (ipif_was_up) {
13069 		/* only delete if we'd added ire's before */
13070 		if (ipif->ipif_isv6)
13071 			ipif_delete_ires_v6(ipif);
13072 		else
13073 			ipif_delete_ires_v4(ipif);
13074 	}
13075 
13076 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13077 		/*
13078 		 * Since the interface is now down, it may have just become
13079 		 * inactive.  Note that this needs to be done even for a
13080 		 * lll_logical_down(), or ARP entries will not get correctly
13081 		 * restored when the interface comes back up.
13082 		 */
13083 		if (IS_UNDER_IPMP(ill))
13084 			ipmp_ill_refresh_active(ill);
13085 	}
13086 
13087 	/*
13088 	 * neighbor-discovery or arp entries for this interface. The ipif
13089 	 * has to be quiesced, so we walk all the nce's and delete those
13090 	 * that point at the ipif->ipif_ill. At the same time, we also
13091 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13092 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13093 	 * that for ipif_down_tail()
13094 	 */
13095 	ipif_nce_down(ipif);
13096 
13097 	/*
13098 	 * If this is the last ipif on the ill, we also need to remove
13099 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13100 	 * never succeed.
13101 	 */
13102 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13103 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13104 
13105 	/*
13106 	 * Walk all CONNs that can have a reference on an ire for this
13107 	 * ipif (we actually walk all that now have stale references).
13108 	 */
13109 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13110 
13111 	/*
13112 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13113 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13114 	 * and ill_delete -> ipif_free -> ipif_down
13115 	 */
13116 	if (mp == NULL) {
13117 		ASSERT(q == NULL);
13118 		return (0);
13119 	}
13120 
13121 	if (CONN_Q(q)) {
13122 		connp = Q_TO_CONN(q);
13123 		mutex_enter(&connp->conn_lock);
13124 	} else {
13125 		connp = NULL;
13126 	}
13127 	mutex_enter(&ill->ill_lock);
13128 	/*
13129 	 * Are there any ire's pointing to this ipif that are still active ?
13130 	 * If this is the last ipif going down, are there any ire's pointing
13131 	 * to this ill that are still active ?
13132 	 */
13133 	if (ipif_is_quiescent(ipif)) {
13134 		mutex_exit(&ill->ill_lock);
13135 		if (connp != NULL)
13136 			mutex_exit(&connp->conn_lock);
13137 		return (0);
13138 	}
13139 
13140 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13141 	    ill->ill_name, (void *)ill));
13142 	/*
13143 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13144 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13145 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13146 	 */
13147 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13148 	if (!success) {
13149 		/* The conn is closing. So just return */
13150 		ASSERT(connp != NULL);
13151 		mutex_exit(&ill->ill_lock);
13152 		mutex_exit(&connp->conn_lock);
13153 		return (EINTR);
13154 	}
13155 
13156 	mutex_exit(&ill->ill_lock);
13157 	if (connp != NULL)
13158 		mutex_exit(&connp->conn_lock);
13159 	return (EINPROGRESS);
13160 }
13161 
13162 int
13163 ipif_down_tail(ipif_t *ipif)
13164 {
13165 	ill_t	*ill = ipif->ipif_ill;
13166 	int	err = 0;
13167 
13168 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13169 	    ill_t *, ill, ipif_t *, ipif);
13170 
13171 	/*
13172 	 * Skip any loopback interface (null wq).
13173 	 * If this is the last logical interface on the ill
13174 	 * have ill_dl_down tell the driver we are gone (unbind)
13175 	 * Note that lun 0 can ipif_down even though
13176 	 * there are other logical units that are up.
13177 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13178 	 */
13179 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13180 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13181 	    ill->ill_dl_up) {
13182 		ill_dl_down(ill);
13183 	}
13184 	if (!ipif->ipif_isv6)
13185 		err = ipif_arp_down(ipif);
13186 
13187 	ill->ill_logical_down = 0;
13188 
13189 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13190 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13191 	return (err);
13192 }
13193 
13194 /*
13195  * Bring interface logically down without bringing the physical interface
13196  * down e.g. when the netmask is changed. This avoids long lasting link
13197  * negotiations between an ethernet interface and a certain switches.
13198  */
13199 static int
13200 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13201 {
13202 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13203 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13204 
13205 	/*
13206 	 * The ill_logical_down flag is a transient flag. It is set here
13207 	 * and is cleared once the down has completed in ipif_down_tail.
13208 	 * This flag does not indicate whether the ill stream is in the
13209 	 * DL_BOUND state with the driver. Instead this flag is used by
13210 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13211 	 * the driver. The state of the ill stream i.e. whether it is
13212 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13213 	 */
13214 	ipif->ipif_ill->ill_logical_down = 1;
13215 	return (ipif_down(ipif, q, mp));
13216 }
13217 
13218 /*
13219  * Initiate deallocate of an IPIF. Always called as writer. Called by
13220  * ill_delete or ip_sioctl_removeif.
13221  */
13222 static void
13223 ipif_free(ipif_t *ipif)
13224 {
13225 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13226 
13227 	ASSERT(IAM_WRITER_IPIF(ipif));
13228 
13229 	if (ipif->ipif_recovery_id != 0)
13230 		(void) untimeout(ipif->ipif_recovery_id);
13231 	ipif->ipif_recovery_id = 0;
13232 
13233 	/*
13234 	 * Take down the interface. We can be called either from ill_delete
13235 	 * or from ip_sioctl_removeif.
13236 	 */
13237 	(void) ipif_down(ipif, NULL, NULL);
13238 
13239 	/*
13240 	 * Now that the interface is down, there's no chance it can still
13241 	 * become a duplicate.  Cancel any timer that may have been set while
13242 	 * tearing down.
13243 	 */
13244 	if (ipif->ipif_recovery_id != 0)
13245 		(void) untimeout(ipif->ipif_recovery_id);
13246 	ipif->ipif_recovery_id = 0;
13247 
13248 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13249 	/* Remove pointers to this ill in the multicast routing tables */
13250 	reset_mrt_vif_ipif(ipif);
13251 	/* If necessary, clear the cached source ipif rotor. */
13252 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13253 		ipif->ipif_ill->ill_src_ipif = NULL;
13254 	rw_exit(&ipst->ips_ill_g_lock);
13255 }
13256 
13257 static void
13258 ipif_free_tail(ipif_t *ipif)
13259 {
13260 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13261 
13262 	/*
13263 	 * Need to hold both ill_g_lock and ill_lock while
13264 	 * inserting or removing an ipif from the linked list
13265 	 * of ipifs hanging off the ill.
13266 	 */
13267 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13268 
13269 #ifdef DEBUG
13270 	ipif_trace_cleanup(ipif);
13271 #endif
13272 
13273 	/* Ask SCTP to take it out of it list */
13274 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13275 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13276 
13277 	/* Get it out of the ILL interface list. */
13278 	ipif_remove(ipif);
13279 	rw_exit(&ipst->ips_ill_g_lock);
13280 
13281 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13282 	ASSERT(ipif->ipif_recovery_id == 0);
13283 	ASSERT(ipif->ipif_ire_local == NULL);
13284 	ASSERT(ipif->ipif_ire_if == NULL);
13285 
13286 	/* Free the memory. */
13287 	mi_free(ipif);
13288 }
13289 
13290 /*
13291  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13292  * is zero.
13293  */
13294 void
13295 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13296 {
13297 	char	lbuf[LIFNAMSIZ];
13298 	char	*name;
13299 	size_t	name_len;
13300 
13301 	buf[0] = '\0';
13302 	name = ipif->ipif_ill->ill_name;
13303 	name_len = ipif->ipif_ill->ill_name_length;
13304 	if (ipif->ipif_id != 0) {
13305 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13306 		    ipif->ipif_id);
13307 		name = lbuf;
13308 		name_len = mi_strlen(name) + 1;
13309 	}
13310 	len -= 1;
13311 	buf[len] = '\0';
13312 	len = MIN(len, name_len);
13313 	bcopy(name, buf, len);
13314 }
13315 
13316 /*
13317  * Sets `buf' to an ill name.
13318  */
13319 void
13320 ill_get_name(const ill_t *ill, char *buf, int len)
13321 {
13322 	char	*name;
13323 	size_t	name_len;
13324 
13325 	name = ill->ill_name;
13326 	name_len = ill->ill_name_length;
13327 	len -= 1;
13328 	buf[len] = '\0';
13329 	len = MIN(len, name_len);
13330 	bcopy(name, buf, len);
13331 }
13332 
13333 /*
13334  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13335  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13336  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13337  * (May be called as writer.)
13338  */
13339 static ipif_t *
13340 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13341     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13342 {
13343 	char	*cp;
13344 	char	*endp;
13345 	long	id;
13346 	ill_t	*ill;
13347 	ipif_t	*ipif;
13348 	uint_t	ire_type;
13349 	boolean_t did_alloc = B_FALSE;
13350 
13351 	/*
13352 	 * If the caller wants to us to create the ipif, make sure we have a
13353 	 * valid zoneid
13354 	 */
13355 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13356 
13357 	if (namelen == 0) {
13358 		return (NULL);
13359 	}
13360 
13361 	*exists = B_FALSE;
13362 	/* Look for a colon in the name. */
13363 	endp = &name[namelen];
13364 	for (cp = endp; --cp > name; ) {
13365 		if (*cp == IPIF_SEPARATOR_CHAR)
13366 			break;
13367 	}
13368 
13369 	if (*cp == IPIF_SEPARATOR_CHAR) {
13370 		/*
13371 		 * Reject any non-decimal aliases for logical
13372 		 * interfaces. Aliases with leading zeroes
13373 		 * are also rejected as they introduce ambiguity
13374 		 * in the naming of the interfaces.
13375 		 * In order to confirm with existing semantics,
13376 		 * and to not break any programs/script relying
13377 		 * on that behaviour, if<0>:0 is considered to be
13378 		 * a valid interface.
13379 		 *
13380 		 * If alias has two or more digits and the first
13381 		 * is zero, fail.
13382 		 */
13383 		if (&cp[2] < endp && cp[1] == '0') {
13384 			return (NULL);
13385 		}
13386 	}
13387 
13388 	if (cp <= name) {
13389 		cp = endp;
13390 	} else {
13391 		*cp = '\0';
13392 	}
13393 
13394 	/*
13395 	 * Look up the ILL, based on the portion of the name
13396 	 * before the slash. ill_lookup_on_name returns a held ill.
13397 	 * Temporary to check whether ill exists already. If so
13398 	 * ill_lookup_on_name will clear it.
13399 	 */
13400 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13401 	    &did_alloc, ipst);
13402 	if (cp != endp)
13403 		*cp = IPIF_SEPARATOR_CHAR;
13404 	if (ill == NULL)
13405 		return (NULL);
13406 
13407 	/* Establish the unit number in the name. */
13408 	id = 0;
13409 	if (cp < endp && *endp == '\0') {
13410 		/* If there was a colon, the unit number follows. */
13411 		cp++;
13412 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13413 			ill_refrele(ill);
13414 			return (NULL);
13415 		}
13416 	}
13417 
13418 	mutex_enter(&ill->ill_lock);
13419 	/* Now see if there is an IPIF with this unit number. */
13420 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13421 		if (ipif->ipif_id == id) {
13422 			if (zoneid != ALL_ZONES &&
13423 			    zoneid != ipif->ipif_zoneid &&
13424 			    ipif->ipif_zoneid != ALL_ZONES) {
13425 				mutex_exit(&ill->ill_lock);
13426 				ill_refrele(ill);
13427 				return (NULL);
13428 			}
13429 			if (IPIF_CAN_LOOKUP(ipif)) {
13430 				ipif_refhold_locked(ipif);
13431 				mutex_exit(&ill->ill_lock);
13432 				if (!did_alloc)
13433 					*exists = B_TRUE;
13434 				/*
13435 				 * Drop locks before calling ill_refrele
13436 				 * since it can potentially call into
13437 				 * ipif_ill_refrele_tail which can end up
13438 				 * in trying to acquire any lock.
13439 				 */
13440 				ill_refrele(ill);
13441 				return (ipif);
13442 			}
13443 		}
13444 	}
13445 
13446 	if (!do_alloc) {
13447 		mutex_exit(&ill->ill_lock);
13448 		ill_refrele(ill);
13449 		return (NULL);
13450 	}
13451 
13452 	/*
13453 	 * If none found, atomically allocate and return a new one.
13454 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13455 	 * to support "receive only" use of lo0:1 etc. as is still done
13456 	 * below as an initial guess.
13457 	 * However, this is now likely to be overriden later in ipif_up_done()
13458 	 * when we know for sure what address has been configured on the
13459 	 * interface, since we might have more than one loopback interface
13460 	 * with a loopback address, e.g. in the case of zones, and all the
13461 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13462 	 */
13463 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13464 		ire_type = IRE_LOOPBACK;
13465 	else
13466 		ire_type = IRE_LOCAL;
13467 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13468 	if (ipif != NULL)
13469 		ipif_refhold_locked(ipif);
13470 	mutex_exit(&ill->ill_lock);
13471 	ill_refrele(ill);
13472 	return (ipif);
13473 }
13474 
13475 /*
13476  * This routine is called whenever a new address comes up on an ipif.  If
13477  * we are configured to respond to address mask requests, then we are supposed
13478  * to broadcast an address mask reply at this time.  This routine is also
13479  * called if we are already up, but a netmask change is made.  This is legal
13480  * but might not make the system manager very popular.	(May be called
13481  * as writer.)
13482  */
13483 void
13484 ipif_mask_reply(ipif_t *ipif)
13485 {
13486 	icmph_t	*icmph;
13487 	ipha_t	*ipha;
13488 	mblk_t	*mp;
13489 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13490 	ip_xmit_attr_t ixas;
13491 
13492 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13493 
13494 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13495 		return;
13496 
13497 	/* ICMP mask reply is IPv4 only */
13498 	ASSERT(!ipif->ipif_isv6);
13499 	/* ICMP mask reply is not for a loopback interface */
13500 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13501 
13502 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13503 		return;
13504 
13505 	mp = allocb(REPLY_LEN, BPRI_HI);
13506 	if (mp == NULL)
13507 		return;
13508 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13509 
13510 	ipha = (ipha_t *)mp->b_rptr;
13511 	bzero(ipha, REPLY_LEN);
13512 	*ipha = icmp_ipha;
13513 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13514 	ipha->ipha_src = ipif->ipif_lcl_addr;
13515 	ipha->ipha_dst = ipif->ipif_brd_addr;
13516 	ipha->ipha_length = htons(REPLY_LEN);
13517 	ipha->ipha_ident = 0;
13518 
13519 	icmph = (icmph_t *)&ipha[1];
13520 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13521 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13522 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13523 
13524 	bzero(&ixas, sizeof (ixas));
13525 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13526 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13527 	ixas.ixa_zoneid = ALL_ZONES;
13528 	ixas.ixa_ifindex = 0;
13529 	ixas.ixa_ipst = ipst;
13530 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13531 	(void) ip_output_simple(mp, &ixas);
13532 	ixa_cleanup(&ixas);
13533 #undef	REPLY_LEN
13534 }
13535 
13536 /*
13537  * Join the ipif specific multicast groups.
13538  * Must be called after a mapping has been set up in the resolver.  (Always
13539  * called as writer.)
13540  */
13541 void
13542 ipif_multicast_up(ipif_t *ipif)
13543 {
13544 	int err;
13545 	ill_t *ill;
13546 	ilm_t *ilm;
13547 
13548 	ASSERT(IAM_WRITER_IPIF(ipif));
13549 
13550 	ill = ipif->ipif_ill;
13551 
13552 	ip1dbg(("ipif_multicast_up\n"));
13553 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13554 	    ipif->ipif_allhosts_ilm != NULL)
13555 		return;
13556 
13557 	if (ipif->ipif_isv6) {
13558 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13559 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13560 
13561 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13562 
13563 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13564 			return;
13565 
13566 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13567 
13568 		/*
13569 		 * Join the all hosts multicast address.  We skip this for
13570 		 * underlying IPMP interfaces since they should be invisible.
13571 		 */
13572 		if (!IS_UNDER_IPMP(ill)) {
13573 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13574 			    &err);
13575 			if (ilm == NULL) {
13576 				ASSERT(err != 0);
13577 				ip0dbg(("ipif_multicast_up: "
13578 				    "all_hosts_mcast failed %d\n", err));
13579 				return;
13580 			}
13581 			ipif->ipif_allhosts_ilm = ilm;
13582 		}
13583 
13584 		/*
13585 		 * Enable multicast for the solicited node multicast address.
13586 		 * If IPMP we need to put the membership on the upper ill.
13587 		 */
13588 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13589 			ill_t *mcast_ill = NULL;
13590 			boolean_t need_refrele;
13591 
13592 			if (IS_UNDER_IPMP(ill) &&
13593 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13594 				need_refrele = B_TRUE;
13595 			} else {
13596 				mcast_ill = ill;
13597 				need_refrele = B_FALSE;
13598 			}
13599 
13600 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13601 			    ipif->ipif_zoneid, &err);
13602 			if (need_refrele)
13603 				ill_refrele(mcast_ill);
13604 
13605 			if (ilm == NULL) {
13606 				ASSERT(err != 0);
13607 				ip0dbg(("ipif_multicast_up: solicited MC"
13608 				    " failed %d\n", err));
13609 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13610 					ipif->ipif_allhosts_ilm = NULL;
13611 					(void) ip_delmulti(ilm);
13612 				}
13613 				return;
13614 			}
13615 			ipif->ipif_solmulti_ilm = ilm;
13616 		}
13617 	} else {
13618 		in6_addr_t v6group;
13619 
13620 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13621 			return;
13622 
13623 		/* Join the all hosts multicast address */
13624 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13625 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13626 
13627 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13628 		if (ilm == NULL) {
13629 			ASSERT(err != 0);
13630 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13631 			return;
13632 		}
13633 		ipif->ipif_allhosts_ilm = ilm;
13634 	}
13635 }
13636 
13637 /*
13638  * Blow away any multicast groups that we joined in ipif_multicast_up().
13639  * (ilms from explicit memberships are handled in conn_update_ill.)
13640  */
13641 void
13642 ipif_multicast_down(ipif_t *ipif)
13643 {
13644 	ASSERT(IAM_WRITER_IPIF(ipif));
13645 
13646 	ip1dbg(("ipif_multicast_down\n"));
13647 
13648 	if (ipif->ipif_allhosts_ilm != NULL) {
13649 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13650 		ipif->ipif_allhosts_ilm = NULL;
13651 	}
13652 	if (ipif->ipif_solmulti_ilm != NULL) {
13653 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13654 		ipif->ipif_solmulti_ilm = NULL;
13655 	}
13656 }
13657 
13658 /*
13659  * Used when an interface comes up to recreate any extra routes on this
13660  * interface.
13661  */
13662 int
13663 ill_recover_saved_ire(ill_t *ill)
13664 {
13665 	mblk_t		*mp;
13666 	ip_stack_t	*ipst = ill->ill_ipst;
13667 
13668 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13669 
13670 	mutex_enter(&ill->ill_saved_ire_lock);
13671 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13672 		ire_t		*ire, *nire;
13673 		ifrt_t		*ifrt;
13674 
13675 		ifrt = (ifrt_t *)mp->b_rptr;
13676 		/*
13677 		 * Create a copy of the IRE with the saved address and netmask.
13678 		 */
13679 		if (ill->ill_isv6) {
13680 			ire = ire_create_v6(
13681 			    &ifrt->ifrt_v6addr,
13682 			    &ifrt->ifrt_v6mask,
13683 			    &ifrt->ifrt_v6gateway_addr,
13684 			    ifrt->ifrt_type,
13685 			    ill,
13686 			    ifrt->ifrt_zoneid,
13687 			    ifrt->ifrt_flags,
13688 			    NULL,
13689 			    ipst);
13690 		} else {
13691 			ire = ire_create(
13692 			    (uint8_t *)&ifrt->ifrt_addr,
13693 			    (uint8_t *)&ifrt->ifrt_mask,
13694 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13695 			    ifrt->ifrt_type,
13696 			    ill,
13697 			    ifrt->ifrt_zoneid,
13698 			    ifrt->ifrt_flags,
13699 			    NULL,
13700 			    ipst);
13701 		}
13702 		if (ire == NULL) {
13703 			mutex_exit(&ill->ill_saved_ire_lock);
13704 			return (ENOMEM);
13705 		}
13706 
13707 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13708 			if (ill->ill_isv6) {
13709 				ire->ire_setsrc_addr_v6 =
13710 				    ifrt->ifrt_v6setsrc_addr;
13711 			} else {
13712 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13713 			}
13714 		}
13715 
13716 		/*
13717 		 * Some software (for example, GateD and Sun Cluster) attempts
13718 		 * to create (what amount to) IRE_PREFIX routes with the
13719 		 * loopback address as the gateway.  This is primarily done to
13720 		 * set up prefixes with the RTF_REJECT flag set (for example,
13721 		 * when generating aggregate routes.)
13722 		 *
13723 		 * If the IRE type (as defined by ill->ill_net_type) is
13724 		 * IRE_LOOPBACK, then we map the request into a
13725 		 * IRE_IF_NORESOLVER.
13726 		 */
13727 		if (ill->ill_net_type == IRE_LOOPBACK)
13728 			ire->ire_type = IRE_IF_NORESOLVER;
13729 
13730 		/*
13731 		 * ire held by ire_add, will be refreled' towards the
13732 		 * the end of ipif_up_done
13733 		 */
13734 		nire = ire_add(ire);
13735 		/*
13736 		 * Check if it was a duplicate entry. This handles
13737 		 * the case of two racing route adds for the same route
13738 		 */
13739 		if (nire == NULL) {
13740 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13741 		} else if (nire != ire) {
13742 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13743 			    (void *)nire));
13744 			ire_delete(nire);
13745 		} else {
13746 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13747 			    (void *)nire));
13748 		}
13749 		if (nire != NULL)
13750 			ire_refrele(nire);
13751 	}
13752 	mutex_exit(&ill->ill_saved_ire_lock);
13753 	return (0);
13754 }
13755 
13756 /*
13757  * Used to set the netmask and broadcast address to default values when the
13758  * interface is brought up.  (Always called as writer.)
13759  */
13760 static void
13761 ipif_set_default(ipif_t *ipif)
13762 {
13763 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13764 
13765 	if (!ipif->ipif_isv6) {
13766 		/*
13767 		 * Interface holds an IPv4 address. Default
13768 		 * mask is the natural netmask.
13769 		 */
13770 		if (!ipif->ipif_net_mask) {
13771 			ipaddr_t	v4mask;
13772 
13773 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13774 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13775 		}
13776 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13777 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13778 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13779 		} else {
13780 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13781 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13782 		}
13783 		/*
13784 		 * NOTE: SunOS 4.X does this even if the broadcast address
13785 		 * has been already set thus we do the same here.
13786 		 */
13787 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13788 			ipaddr_t	v4addr;
13789 
13790 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13791 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13792 		}
13793 	} else {
13794 		/*
13795 		 * Interface holds an IPv6-only address.  Default
13796 		 * mask is all-ones.
13797 		 */
13798 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13799 			ipif->ipif_v6net_mask = ipv6_all_ones;
13800 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13801 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13802 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13803 		} else {
13804 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13805 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13806 		}
13807 	}
13808 }
13809 
13810 /*
13811  * Return 0 if this address can be used as local address without causing
13812  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13813  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13814  * Note that the same IPv6 link-local address is allowed as long as the ills
13815  * are not on the same link.
13816  */
13817 int
13818 ip_addr_availability_check(ipif_t *new_ipif)
13819 {
13820 	in6_addr_t our_v6addr;
13821 	ill_t *ill;
13822 	ipif_t *ipif;
13823 	ill_walk_context_t ctx;
13824 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13825 
13826 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13827 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13828 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13829 
13830 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13831 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13832 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13833 		return (0);
13834 
13835 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13836 
13837 	if (new_ipif->ipif_isv6)
13838 		ill = ILL_START_WALK_V6(&ctx, ipst);
13839 	else
13840 		ill = ILL_START_WALK_V4(&ctx, ipst);
13841 
13842 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13843 		for (ipif = ill->ill_ipif; ipif != NULL;
13844 		    ipif = ipif->ipif_next) {
13845 			if ((ipif == new_ipif) ||
13846 			    !(ipif->ipif_flags & IPIF_UP) ||
13847 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13848 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13849 			    &our_v6addr))
13850 				continue;
13851 
13852 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13853 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13854 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13855 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13856 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13857 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13858 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13859 				continue;
13860 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13861 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13862 				continue;
13863 			else if (new_ipif->ipif_ill == ill)
13864 				return (EADDRINUSE);
13865 			else
13866 				return (EADDRNOTAVAIL);
13867 		}
13868 	}
13869 
13870 	return (0);
13871 }
13872 
13873 /*
13874  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13875  * IREs for the ipif.
13876  * When the routine returns EINPROGRESS then mp has been consumed and
13877  * the ioctl will be acked from ip_rput_dlpi.
13878  */
13879 int
13880 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13881 {
13882 	ill_t		*ill = ipif->ipif_ill;
13883 	boolean_t 	isv6 = ipif->ipif_isv6;
13884 	int		err = 0;
13885 	boolean_t	success;
13886 	uint_t		ipif_orig_id;
13887 	ip_stack_t	*ipst = ill->ill_ipst;
13888 
13889 	ASSERT(IAM_WRITER_IPIF(ipif));
13890 
13891 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13892 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13893 	    ill_t *, ill, ipif_t *, ipif);
13894 
13895 	/* Shouldn't get here if it is already up. */
13896 	if (ipif->ipif_flags & IPIF_UP)
13897 		return (EALREADY);
13898 
13899 	/*
13900 	 * If this is a request to bring up a data address on an interface
13901 	 * under IPMP, then move the address to its IPMP meta-interface and
13902 	 * try to bring it up.  One complication is that the zeroth ipif for
13903 	 * an ill is special, in that every ill always has one, and that code
13904 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13905 	 */
13906 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13907 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13908 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13909 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13910 
13911 		/*
13912 		 * The ipif being brought up should be quiesced.  If it's not,
13913 		 * something has gone amiss and we need to bail out.  (If it's
13914 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13915 		 */
13916 		mutex_enter(&ill->ill_lock);
13917 		if (!ipif_is_quiescent(ipif)) {
13918 			mutex_exit(&ill->ill_lock);
13919 			return (EINVAL);
13920 		}
13921 		mutex_exit(&ill->ill_lock);
13922 
13923 		/*
13924 		 * If we're going to need to allocate ipifs, do it prior
13925 		 * to starting the move (and grabbing locks).
13926 		 */
13927 		if (ipif->ipif_id == 0) {
13928 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13929 			    B_FALSE, &err)) == NULL) {
13930 				return (err);
13931 			}
13932 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13933 			    B_FALSE, &err)) == NULL) {
13934 				mi_free(moveipif);
13935 				return (err);
13936 			}
13937 		}
13938 
13939 		/*
13940 		 * Grab or transfer the ipif to move.  During the move, keep
13941 		 * ill_g_lock held to prevent any ill walker threads from
13942 		 * seeing things in an inconsistent state.
13943 		 */
13944 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13945 		if (ipif->ipif_id != 0) {
13946 			ipif_remove(ipif);
13947 		} else {
13948 			ipif_transfer(ipif, moveipif, stubipif);
13949 			ipif = moveipif;
13950 		}
13951 
13952 		/*
13953 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13954 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13955 		 * replace that one.  Otherwise, pick the next available slot.
13956 		 */
13957 		ipif->ipif_ill = ipmp_ill;
13958 		ipif_orig_id = ipif->ipif_id;
13959 
13960 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13961 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13962 			ipif = ipmp_ill->ill_ipif;
13963 		} else {
13964 			ipif->ipif_id = -1;
13965 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
13966 				/*
13967 				 * No more available ipif_id's -- put it back
13968 				 * on the original ill and fail the operation.
13969 				 * Since we're writer on the ill, we can be
13970 				 * sure our old slot is still available.
13971 				 */
13972 				ipif->ipif_id = ipif_orig_id;
13973 				ipif->ipif_ill = ill;
13974 				if (ipif_orig_id == 0) {
13975 					ipif_transfer(ipif, ill->ill_ipif,
13976 					    NULL);
13977 				} else {
13978 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13979 				}
13980 				rw_exit(&ipst->ips_ill_g_lock);
13981 				return (err);
13982 			}
13983 		}
13984 		rw_exit(&ipst->ips_ill_g_lock);
13985 
13986 		/*
13987 		 * Tell SCTP that the ipif has moved.  Note that even if we
13988 		 * had to allocate a new ipif, the original sequence id was
13989 		 * preserved and therefore SCTP won't know.
13990 		 */
13991 		sctp_move_ipif(ipif, ill, ipmp_ill);
13992 
13993 		/*
13994 		 * If the ipif being brought up was on slot zero, then we
13995 		 * first need to bring up the placeholder we stuck there.  In
13996 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13997 		 * call to ipif_up() itself, if we successfully bring up the
13998 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13999 		 */
14000 		if (ipif_orig_id == 0) {
14001 			ASSERT(ill->ill_move_ipif == NULL);
14002 			ill->ill_move_ipif = ipif;
14003 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14004 				ASSERT(ill->ill_move_ipif == NULL);
14005 			if (err != EINPROGRESS)
14006 				ill->ill_move_ipif = NULL;
14007 			return (err);
14008 		}
14009 
14010 		/*
14011 		 * Bring it up on the IPMP ill.
14012 		 */
14013 		return (ipif_up(ipif, q, mp));
14014 	}
14015 
14016 	/* Skip arp/ndp for any loopback interface. */
14017 	if (ill->ill_wq != NULL) {
14018 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14019 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14020 
14021 		if (!ill->ill_dl_up) {
14022 			/*
14023 			 * ill_dl_up is not yet set. i.e. we are yet to
14024 			 * DL_BIND with the driver and this is the first
14025 			 * logical interface on the ill to become "up".
14026 			 * Tell the driver to get going (via DL_BIND_REQ).
14027 			 * Note that changing "significant" IFF_ flags
14028 			 * address/netmask etc cause a down/up dance, but
14029 			 * does not cause an unbind (DL_UNBIND) with the driver
14030 			 */
14031 			return (ill_dl_up(ill, ipif, mp, q));
14032 		}
14033 
14034 		/*
14035 		 * ipif_resolver_up may end up needeing to bind/attach
14036 		 * the ARP stream, which in turn necessitates a
14037 		 * DLPI message exchange with the driver. ioctls are
14038 		 * serialized and so we cannot send more than one
14039 		 * interface up message at a time. If ipif_resolver_up
14040 		 * does need to wait for the DLPI handshake for the ARP stream,
14041 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14042 		 */
14043 
14044 		ASSERT(connp != NULL || !CONN_Q(q));
14045 		if (connp != NULL)
14046 			mutex_enter(&connp->conn_lock);
14047 		mutex_enter(&ill->ill_lock);
14048 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14049 		mutex_exit(&ill->ill_lock);
14050 		if (connp != NULL)
14051 			mutex_exit(&connp->conn_lock);
14052 		if (!success)
14053 			return (EINTR);
14054 
14055 		/*
14056 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14057 		 * complete when ipif_ndp_up returns.
14058 		 */
14059 		err = ipif_resolver_up(ipif, Res_act_initial);
14060 		if (err == EINPROGRESS) {
14061 			/* We will complete it in arp_bringup_done() */
14062 			return (err);
14063 		}
14064 
14065 		if (isv6 && err == 0)
14066 			err = ipif_ndp_up(ipif, B_TRUE);
14067 
14068 		ASSERT(err != EINPROGRESS);
14069 		mp = ipsq_pending_mp_get(ipsq, &connp);
14070 		ASSERT(mp != NULL);
14071 		if (err != 0)
14072 			return (err);
14073 	} else {
14074 		/*
14075 		 * Interfaces without underlying hardware don't do duplicate
14076 		 * address detection.
14077 		 */
14078 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14079 		ipif->ipif_addr_ready = 1;
14080 		err = ill_add_ires(ill);
14081 		/* allocation failure? */
14082 		if (err != 0)
14083 			return (err);
14084 	}
14085 
14086 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14087 	if (err == 0 && ill->ill_move_ipif != NULL) {
14088 		ipif = ill->ill_move_ipif;
14089 		ill->ill_move_ipif = NULL;
14090 		return (ipif_up(ipif, q, mp));
14091 	}
14092 	return (err);
14093 }
14094 
14095 /*
14096  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14097  * The identical set of IREs need to be removed in ill_delete_ires().
14098  */
14099 int
14100 ill_add_ires(ill_t *ill)
14101 {
14102 	ire_t	*ire;
14103 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14104 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14105 
14106 	if (ill->ill_ire_multicast != NULL)
14107 		return (0);
14108 
14109 	/*
14110 	 * provide some dummy ire_addr for creating the ire.
14111 	 */
14112 	if (ill->ill_isv6) {
14113 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14114 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14115 	} else {
14116 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14117 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14118 	}
14119 	if (ire == NULL)
14120 		return (ENOMEM);
14121 
14122 	ill->ill_ire_multicast = ire;
14123 	return (0);
14124 }
14125 
14126 void
14127 ill_delete_ires(ill_t *ill)
14128 {
14129 	if (ill->ill_ire_multicast != NULL) {
14130 		/*
14131 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14132 		 * which was taken without any th_tracing enabled.
14133 		 * We also mark it as condemned (note that it was never added)
14134 		 * so that caching conn's can move off of it.
14135 		 */
14136 		ire_make_condemned(ill->ill_ire_multicast);
14137 		ire_refrele_notr(ill->ill_ire_multicast);
14138 		ill->ill_ire_multicast = NULL;
14139 	}
14140 }
14141 
14142 /*
14143  * Perform a bind for the physical device.
14144  * When the routine returns EINPROGRESS then mp has been consumed and
14145  * the ioctl will be acked from ip_rput_dlpi.
14146  * Allocate an unbind message and save it until ipif_down.
14147  */
14148 static int
14149 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14150 {
14151 	mblk_t	*bind_mp = NULL;
14152 	mblk_t	*unbind_mp = NULL;
14153 	conn_t	*connp;
14154 	boolean_t success;
14155 	int	err;
14156 
14157 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14158 
14159 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14160 	ASSERT(IAM_WRITER_ILL(ill));
14161 	ASSERT(mp != NULL);
14162 
14163 	/*
14164 	 * Make sure we have an IRE_MULTICAST in case we immediately
14165 	 * start receiving packets.
14166 	 */
14167 	err = ill_add_ires(ill);
14168 	if (err != 0)
14169 		goto bad;
14170 
14171 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14172 	    DL_BIND_REQ);
14173 	if (bind_mp == NULL)
14174 		goto bad;
14175 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14176 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14177 
14178 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14179 	if (unbind_mp == NULL)
14180 		goto bad;
14181 
14182 	/*
14183 	 * Record state needed to complete this operation when the
14184 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14185 	 */
14186 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14187 	ASSERT(connp != NULL || !CONN_Q(q));
14188 	GRAB_CONN_LOCK(q);
14189 	mutex_enter(&ipif->ipif_ill->ill_lock);
14190 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14191 	mutex_exit(&ipif->ipif_ill->ill_lock);
14192 	RELEASE_CONN_LOCK(q);
14193 	if (!success)
14194 		goto bad;
14195 
14196 	/*
14197 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14198 	 * the interface goes down.
14199 	 */
14200 	ASSERT(ill->ill_unbind_mp == NULL);
14201 	ill->ill_unbind_mp = unbind_mp;
14202 
14203 	ill_dlpi_send(ill, bind_mp);
14204 	/* Send down link-layer capabilities probe if not already done. */
14205 	ill_capability_probe(ill);
14206 
14207 	/*
14208 	 * Sysid used to rely on the fact that netboots set domainname
14209 	 * and the like. Now that miniroot boots aren't strictly netboots
14210 	 * and miniroot network configuration is driven from userland
14211 	 * these things still need to be set. This situation can be detected
14212 	 * by comparing the interface being configured here to the one
14213 	 * dhcifname was set to reference by the boot loader. Once sysid is
14214 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14215 	 */
14216 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14217 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14218 	    (strlen(srpc_domain) == 0)) {
14219 		if (dhcpinit() != 0)
14220 			cmn_err(CE_WARN, "no cached dhcp response");
14221 	}
14222 
14223 	/*
14224 	 * This operation will complete in ip_rput_dlpi with either
14225 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14226 	 */
14227 	return (EINPROGRESS);
14228 bad:
14229 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14230 
14231 	freemsg(bind_mp);
14232 	freemsg(unbind_mp);
14233 	return (ENOMEM);
14234 }
14235 
14236 /* Add room for tcp+ip headers */
14237 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14238 
14239 /*
14240  * DLPI and ARP is up.
14241  * Create all the IREs associated with an interface. Bring up multicast.
14242  * Set the interface flag and finish other initialization
14243  * that potentially had to be deferred to after DL_BIND_ACK.
14244  */
14245 int
14246 ipif_up_done(ipif_t *ipif)
14247 {
14248 	ill_t		*ill = ipif->ipif_ill;
14249 	int		err = 0;
14250 	boolean_t	loopback = B_FALSE;
14251 	boolean_t	update_src_selection = B_TRUE;
14252 	ipif_t		*tmp_ipif;
14253 
14254 	ip1dbg(("ipif_up_done(%s:%u)\n",
14255 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14256 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14257 	    ill_t *, ill, ipif_t *, ipif);
14258 
14259 	/* Check if this is a loopback interface */
14260 	if (ipif->ipif_ill->ill_wq == NULL)
14261 		loopback = B_TRUE;
14262 
14263 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14264 
14265 	/*
14266 	 * If all other interfaces for this ill are down or DEPRECATED,
14267 	 * or otherwise unsuitable for source address selection,
14268 	 * reset the src generation numbers to make sure source
14269 	 * address selection gets to take this new ipif into account.
14270 	 * No need to hold ill_lock while traversing the ipif list since
14271 	 * we are writer
14272 	 */
14273 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14274 	    tmp_ipif = tmp_ipif->ipif_next) {
14275 		if (((tmp_ipif->ipif_flags &
14276 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14277 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14278 		    (tmp_ipif == ipif))
14279 			continue;
14280 		/* first useable pre-existing interface */
14281 		update_src_selection = B_FALSE;
14282 		break;
14283 	}
14284 	if (update_src_selection)
14285 		ip_update_source_selection(ill->ill_ipst);
14286 
14287 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14288 		nce_t *loop_nce = NULL;
14289 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14290 
14291 		/*
14292 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14293 		 * ipif_lookup_on_name(), but in the case of zones we can have
14294 		 * several loopback addresses on lo0. So all the interfaces with
14295 		 * loopback addresses need to be marked IRE_LOOPBACK.
14296 		 */
14297 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14298 		    htonl(INADDR_LOOPBACK))
14299 			ipif->ipif_ire_type = IRE_LOOPBACK;
14300 		else
14301 			ipif->ipif_ire_type = IRE_LOCAL;
14302 		if (ill->ill_net_type != IRE_LOOPBACK)
14303 			flags |= NCE_F_PUBLISH;
14304 
14305 		/* add unicast nce for the local addr */
14306 		err = nce_lookup_then_add_v4(ill, NULL,
14307 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14308 		    ND_REACHABLE, &loop_nce);
14309 		/* A shared-IP zone sees EEXIST for lo0:N */
14310 		if (err == 0 || err == EEXIST) {
14311 			ipif->ipif_added_nce = 1;
14312 			loop_nce->nce_ipif_cnt++;
14313 			nce_refrele(loop_nce);
14314 			err = 0;
14315 		} else {
14316 			ASSERT(loop_nce == NULL);
14317 			return (err);
14318 		}
14319 	}
14320 
14321 	/* Create all the IREs associated with this interface */
14322 	err = ipif_add_ires_v4(ipif, loopback);
14323 	if (err != 0) {
14324 		/*
14325 		 * see comments about return value from
14326 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14327 		 */
14328 		if (err != EADDRINUSE) {
14329 			(void) ipif_arp_down(ipif);
14330 		} else {
14331 			/*
14332 			 * Make IPMP aware of the deleted ipif so that
14333 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14334 			 * can be completed. Note that we do not want to
14335 			 * destroy the nce that was created on the ipmp_ill
14336 			 * for the active copy of the duplicate address in
14337 			 * use.
14338 			 */
14339 			if (IS_IPMP(ill))
14340 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14341 			err = EADDRNOTAVAIL;
14342 		}
14343 		return (err);
14344 	}
14345 
14346 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14347 		/* Recover any additional IREs entries for this ill */
14348 		(void) ill_recover_saved_ire(ill);
14349 	}
14350 
14351 	if (ill->ill_need_recover_multicast) {
14352 		/*
14353 		 * Need to recover all multicast memberships in the driver.
14354 		 * This had to be deferred until we had attached.  The same
14355 		 * code exists in ipif_up_done_v6() to recover IPv6
14356 		 * memberships.
14357 		 *
14358 		 * Note that it would be preferable to unconditionally do the
14359 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14360 		 * that since ill_join_allmulti() depends on ill_dl_up being
14361 		 * set, and it is not set until we receive a DL_BIND_ACK after
14362 		 * having called ill_dl_up().
14363 		 */
14364 		ill_recover_multicast(ill);
14365 	}
14366 
14367 	if (ill->ill_ipif_up_count == 1) {
14368 		/*
14369 		 * Since the interface is now up, it may now be active.
14370 		 */
14371 		if (IS_UNDER_IPMP(ill))
14372 			ipmp_ill_refresh_active(ill);
14373 
14374 		/*
14375 		 * If this is an IPMP interface, we may now be able to
14376 		 * establish ARP entries.
14377 		 */
14378 		if (IS_IPMP(ill))
14379 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14380 	}
14381 
14382 	/* Join the allhosts multicast address */
14383 	ipif_multicast_up(ipif);
14384 
14385 	if (!loopback && !update_src_selection &&
14386 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14387 		ip_update_source_selection(ill->ill_ipst);
14388 
14389 	if (!loopback && ipif->ipif_addr_ready) {
14390 		/* Broadcast an address mask reply. */
14391 		ipif_mask_reply(ipif);
14392 	}
14393 	/* Perhaps ilgs should use this ill */
14394 	update_conn_ill(NULL, ill->ill_ipst);
14395 
14396 	/*
14397 	 * This had to be deferred until we had bound.  Tell routing sockets and
14398 	 * others that this interface is up if it looks like the address has
14399 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14400 	 * duplicate address detection to do its thing.
14401 	 */
14402 	if (ipif->ipif_addr_ready)
14403 		ipif_up_notify(ipif);
14404 	return (0);
14405 }
14406 
14407 /*
14408  * Add the IREs associated with the ipif.
14409  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14410  */
14411 static int
14412 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14413 {
14414 	ill_t		*ill = ipif->ipif_ill;
14415 	ip_stack_t	*ipst = ill->ill_ipst;
14416 	ire_t		*ire_array[20];
14417 	ire_t		**irep = ire_array;
14418 	ire_t		**irep1;
14419 	ipaddr_t	net_mask = 0;
14420 	ipaddr_t	subnet_mask, route_mask;
14421 	int		err;
14422 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14423 	ire_t		*ire_if = NULL;
14424 
14425 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14426 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14427 		/*
14428 		 * If we're on a labeled system then make sure that zone-
14429 		 * private addresses have proper remote host database entries.
14430 		 */
14431 		if (is_system_labeled() &&
14432 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14433 		    !tsol_check_interface_address(ipif))
14434 			return (EINVAL);
14435 
14436 		/* Register the source address for __sin6_src_id */
14437 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14438 		    ipif->ipif_zoneid, ipst);
14439 		if (err != 0) {
14440 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14441 			return (err);
14442 		}
14443 
14444 		/* If the interface address is set, create the local IRE. */
14445 		ire_local = ire_create(
14446 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14447 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14448 		    NULL,				/* no gateway */
14449 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14450 		    ipif->ipif_ill,
14451 		    ipif->ipif_zoneid,
14452 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14453 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14454 		    NULL,
14455 		    ipst);
14456 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14457 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14458 		    ipif->ipif_ire_type,
14459 		    ntohl(ipif->ipif_lcl_addr)));
14460 		if (ire_local == NULL) {
14461 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14462 			err = ENOMEM;
14463 			goto bad;
14464 		}
14465 	} else {
14466 		ip1dbg((
14467 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14468 		    ipif->ipif_ire_type,
14469 		    ntohl(ipif->ipif_lcl_addr),
14470 		    (uint_t)ipif->ipif_flags));
14471 	}
14472 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14473 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14474 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14475 	} else {
14476 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14477 	}
14478 
14479 	subnet_mask = ipif->ipif_net_mask;
14480 
14481 	/*
14482 	 * If mask was not specified, use natural netmask of
14483 	 * interface address. Also, store this mask back into the
14484 	 * ipif struct.
14485 	 */
14486 	if (subnet_mask == 0) {
14487 		subnet_mask = net_mask;
14488 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14489 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14490 		    ipif->ipif_v6subnet);
14491 	}
14492 
14493 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14494 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14495 	    ipif->ipif_subnet != INADDR_ANY) {
14496 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14497 
14498 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14499 			route_mask = IP_HOST_MASK;
14500 		} else {
14501 			route_mask = subnet_mask;
14502 		}
14503 
14504 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14505 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14506 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14507 		    ntohl(ipif->ipif_subnet)));
14508 		ire_if = ire_create(
14509 		    (uchar_t *)&ipif->ipif_subnet,
14510 		    (uchar_t *)&route_mask,
14511 		    (uchar_t *)&ipif->ipif_lcl_addr,
14512 		    ill->ill_net_type,
14513 		    ill,
14514 		    ipif->ipif_zoneid,
14515 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14516 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14517 		    NULL,
14518 		    ipst);
14519 		if (ire_if == NULL) {
14520 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14521 			err = ENOMEM;
14522 			goto bad;
14523 		}
14524 	}
14525 
14526 	/*
14527 	 * Create any necessary broadcast IREs.
14528 	 */
14529 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14530 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14531 		irep = ipif_create_bcast_ires(ipif, irep);
14532 
14533 	/* If an earlier ire_create failed, get out now */
14534 	for (irep1 = irep; irep1 > ire_array; ) {
14535 		irep1--;
14536 		if (*irep1 == NULL) {
14537 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14538 			err = ENOMEM;
14539 			goto bad;
14540 		}
14541 	}
14542 
14543 	/*
14544 	 * Need to atomically check for IP address availability under
14545 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14546 	 * ills or new ipifs can be added while we are checking availability.
14547 	 */
14548 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14549 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14550 	/* Mark it up, and increment counters. */
14551 	ipif->ipif_flags |= IPIF_UP;
14552 	ill->ill_ipif_up_count++;
14553 	err = ip_addr_availability_check(ipif);
14554 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14555 	rw_exit(&ipst->ips_ill_g_lock);
14556 
14557 	if (err != 0) {
14558 		/*
14559 		 * Our address may already be up on the same ill. In this case,
14560 		 * the ARP entry for our ipif replaced the one for the other
14561 		 * ipif. So we don't want to delete it (otherwise the other ipif
14562 		 * would be unable to send packets).
14563 		 * ip_addr_availability_check() identifies this case for us and
14564 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14565 		 * which is the expected error code.
14566 		 */
14567 		ill->ill_ipif_up_count--;
14568 		ipif->ipif_flags &= ~IPIF_UP;
14569 		goto bad;
14570 	}
14571 
14572 	/*
14573 	 * Add in all newly created IREs.  ire_create_bcast() has
14574 	 * already checked for duplicates of the IRE_BROADCAST type.
14575 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14576 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14577 	 * a /32 route.
14578 	 */
14579 	if (ire_if != NULL) {
14580 		ire_if = ire_add(ire_if);
14581 		if (ire_if == NULL) {
14582 			err = ENOMEM;
14583 			goto bad2;
14584 		}
14585 #ifdef DEBUG
14586 		ire_refhold_notr(ire_if);
14587 		ire_refrele(ire_if);
14588 #endif
14589 	}
14590 	if (ire_local != NULL) {
14591 		ire_local = ire_add(ire_local);
14592 		if (ire_local == NULL) {
14593 			err = ENOMEM;
14594 			goto bad2;
14595 		}
14596 #ifdef DEBUG
14597 		ire_refhold_notr(ire_local);
14598 		ire_refrele(ire_local);
14599 #endif
14600 	}
14601 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14602 	if (ire_local != NULL)
14603 		ipif->ipif_ire_local = ire_local;
14604 	if (ire_if != NULL)
14605 		ipif->ipif_ire_if = ire_if;
14606 	rw_exit(&ipst->ips_ill_g_lock);
14607 	ire_local = NULL;
14608 	ire_if = NULL;
14609 
14610 	/*
14611 	 * We first add all of them, and if that succeeds we refrele the
14612 	 * bunch. That enables us to delete all of them should any of the
14613 	 * ire_adds fail.
14614 	 */
14615 	for (irep1 = irep; irep1 > ire_array; ) {
14616 		irep1--;
14617 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14618 		*irep1 = ire_add(*irep1);
14619 		if (*irep1 == NULL) {
14620 			err = ENOMEM;
14621 			goto bad2;
14622 		}
14623 	}
14624 
14625 	for (irep1 = irep; irep1 > ire_array; ) {
14626 		irep1--;
14627 		/* refheld by ire_add. */
14628 		if (*irep1 != NULL) {
14629 			ire_refrele(*irep1);
14630 			*irep1 = NULL;
14631 		}
14632 	}
14633 
14634 	if (!loopback) {
14635 		/*
14636 		 * If the broadcast address has been set, make sure it makes
14637 		 * sense based on the interface address.
14638 		 * Only match on ill since we are sharing broadcast addresses.
14639 		 */
14640 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14641 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14642 			ire_t	*ire;
14643 
14644 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14645 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14646 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14647 
14648 			if (ire == NULL) {
14649 				/*
14650 				 * If there isn't a matching broadcast IRE,
14651 				 * revert to the default for this netmask.
14652 				 */
14653 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14654 				mutex_enter(&ipif->ipif_ill->ill_lock);
14655 				ipif_set_default(ipif);
14656 				mutex_exit(&ipif->ipif_ill->ill_lock);
14657 			} else {
14658 				ire_refrele(ire);
14659 			}
14660 		}
14661 
14662 	}
14663 	return (0);
14664 
14665 bad2:
14666 	ill->ill_ipif_up_count--;
14667 	ipif->ipif_flags &= ~IPIF_UP;
14668 
14669 bad:
14670 	ip1dbg(("ipif_add_ires: FAILED \n"));
14671 	if (ire_local != NULL)
14672 		ire_delete(ire_local);
14673 	if (ire_if != NULL)
14674 		ire_delete(ire_if);
14675 
14676 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14677 	ire_local = ipif->ipif_ire_local;
14678 	ipif->ipif_ire_local = NULL;
14679 	ire_if = ipif->ipif_ire_if;
14680 	ipif->ipif_ire_if = NULL;
14681 	rw_exit(&ipst->ips_ill_g_lock);
14682 	if (ire_local != NULL) {
14683 		ire_delete(ire_local);
14684 		ire_refrele_notr(ire_local);
14685 	}
14686 	if (ire_if != NULL) {
14687 		ire_delete(ire_if);
14688 		ire_refrele_notr(ire_if);
14689 	}
14690 
14691 	while (irep > ire_array) {
14692 		irep--;
14693 		if (*irep != NULL) {
14694 			ire_delete(*irep);
14695 		}
14696 	}
14697 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14698 
14699 	return (err);
14700 }
14701 
14702 /* Remove all the IREs created by ipif_add_ires_v4 */
14703 void
14704 ipif_delete_ires_v4(ipif_t *ipif)
14705 {
14706 	ill_t		*ill = ipif->ipif_ill;
14707 	ip_stack_t	*ipst = ill->ill_ipst;
14708 	ire_t		*ire;
14709 
14710 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14711 	ire = ipif->ipif_ire_local;
14712 	ipif->ipif_ire_local = NULL;
14713 	rw_exit(&ipst->ips_ill_g_lock);
14714 	if (ire != NULL) {
14715 		/*
14716 		 * Move count to ipif so we don't loose the count due to
14717 		 * a down/up dance.
14718 		 */
14719 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14720 
14721 		ire_delete(ire);
14722 		ire_refrele_notr(ire);
14723 	}
14724 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14725 	ire = ipif->ipif_ire_if;
14726 	ipif->ipif_ire_if = NULL;
14727 	rw_exit(&ipst->ips_ill_g_lock);
14728 	if (ire != NULL) {
14729 		ire_delete(ire);
14730 		ire_refrele_notr(ire);
14731 	}
14732 
14733 	/*
14734 	 * Delete the broadcast IREs.
14735 	 */
14736 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14737 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14738 		ipif_delete_bcast_ires(ipif);
14739 }
14740 
14741 /*
14742  * Checks for availbility of a usable source address (if there is one) when the
14743  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14744  * this selection is done regardless of the destination.
14745  */
14746 boolean_t
14747 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14748     ip_stack_t *ipst)
14749 {
14750 	ipif_t		*ipif = NULL;
14751 	ill_t		*uill;
14752 
14753 	ASSERT(ifindex != 0);
14754 
14755 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14756 	if (uill == NULL)
14757 		return (B_FALSE);
14758 
14759 	mutex_enter(&uill->ill_lock);
14760 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14761 		if (IPIF_IS_CONDEMNED(ipif))
14762 			continue;
14763 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14764 			continue;
14765 		if (!(ipif->ipif_flags & IPIF_UP))
14766 			continue;
14767 		if (ipif->ipif_zoneid != zoneid)
14768 			continue;
14769 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14770 		    ipif->ipif_lcl_addr == INADDR_ANY)
14771 			continue;
14772 		mutex_exit(&uill->ill_lock);
14773 		ill_refrele(uill);
14774 		return (B_TRUE);
14775 	}
14776 	mutex_exit(&uill->ill_lock);
14777 	ill_refrele(uill);
14778 	return (B_FALSE);
14779 }
14780 
14781 /*
14782  * Find an ipif with a good local address on the ill+zoneid.
14783  */
14784 ipif_t *
14785 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14786 {
14787 	ipif_t		*ipif;
14788 
14789 	mutex_enter(&ill->ill_lock);
14790 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14791 		if (IPIF_IS_CONDEMNED(ipif))
14792 			continue;
14793 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14794 			continue;
14795 		if (!(ipif->ipif_flags & IPIF_UP))
14796 			continue;
14797 		if (ipif->ipif_zoneid != zoneid &&
14798 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14799 			continue;
14800 		if (ill->ill_isv6 ?
14801 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14802 		    ipif->ipif_lcl_addr == INADDR_ANY)
14803 			continue;
14804 		ipif_refhold_locked(ipif);
14805 		mutex_exit(&ill->ill_lock);
14806 		return (ipif);
14807 	}
14808 	mutex_exit(&ill->ill_lock);
14809 	return (NULL);
14810 }
14811 
14812 /*
14813  * IP source address type, sorted from worst to best.  For a given type,
14814  * always prefer IP addresses on the same subnet.  All-zones addresses are
14815  * suboptimal because they pose problems with unlabeled destinations.
14816  */
14817 typedef enum {
14818 	IPIF_NONE,
14819 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14820 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14821 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14822 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14823 	IPIF_DIFFNET,			/* normal and different subnet */
14824 	IPIF_SAMENET,			/* normal and same subnet */
14825 	IPIF_LOCALADDR			/* local loopback */
14826 } ipif_type_t;
14827 
14828 /*
14829  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14830  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14831  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14832  * the first one, unless IPMP is used in which case we round-robin among them;
14833  * see below for more.
14834  *
14835  * Returns NULL if there is no suitable source address for the ill.
14836  * This only occurs when there is no valid source address for the ill.
14837  */
14838 ipif_t *
14839 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14840     boolean_t allow_usesrc, boolean_t *notreadyp)
14841 {
14842 	ill_t	*usill = NULL;
14843 	ill_t	*ipmp_ill = NULL;
14844 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14845 	ipif_type_t type, best_type;
14846 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14847 	ip_stack_t *ipst = ill->ill_ipst;
14848 	boolean_t samenet;
14849 
14850 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14851 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14852 		    B_FALSE, ipst);
14853 		if (usill != NULL)
14854 			ill = usill;	/* Select source from usesrc ILL */
14855 		else
14856 			return (NULL);
14857 	}
14858 
14859 	/*
14860 	 * Test addresses should never be used for source address selection,
14861 	 * so if we were passed one, switch to the IPMP meta-interface.
14862 	 */
14863 	if (IS_UNDER_IPMP(ill)) {
14864 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14865 			ill = ipmp_ill;	/* Select source from IPMP ill */
14866 		else
14867 			return (NULL);
14868 	}
14869 
14870 	/*
14871 	 * If we're dealing with an unlabeled destination on a labeled system,
14872 	 * make sure that we ignore source addresses that are incompatible with
14873 	 * the destination's default label.  That destination's default label
14874 	 * must dominate the minimum label on the source address.
14875 	 */
14876 	dst_rhtp = NULL;
14877 	if (is_system_labeled()) {
14878 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14879 		if (dst_rhtp == NULL)
14880 			return (NULL);
14881 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14882 			TPC_RELE(dst_rhtp);
14883 			dst_rhtp = NULL;
14884 		}
14885 	}
14886 
14887 	/*
14888 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14889 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14890 	 * After selecting the right ipif, under ill_lock make sure ipif is
14891 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14892 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14893 	 * but not under a lock.
14894 	 */
14895 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14896 retry:
14897 	/*
14898 	 * For source address selection, we treat the ipif list as circular
14899 	 * and continue until we get back to where we started.  This allows
14900 	 * IPMP to vary source address selection (which improves inbound load
14901 	 * spreading) by caching its last ending point and starting from
14902 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14903 	 * ills since that can't happen on the IPMP ill.
14904 	 */
14905 	start_ipif = ill->ill_ipif;
14906 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14907 		start_ipif = ill->ill_src_ipif;
14908 
14909 	ipif = start_ipif;
14910 	best_ipif = NULL;
14911 	best_type = IPIF_NONE;
14912 	do {
14913 		if ((next_ipif = ipif->ipif_next) == NULL)
14914 			next_ipif = ill->ill_ipif;
14915 
14916 		if (IPIF_IS_CONDEMNED(ipif))
14917 			continue;
14918 		/* Always skip NOLOCAL and ANYCAST interfaces */
14919 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14920 			continue;
14921 		/* Always skip NOACCEPT interfaces */
14922 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14923 			continue;
14924 		if (!(ipif->ipif_flags & IPIF_UP))
14925 			continue;
14926 
14927 		if (!ipif->ipif_addr_ready) {
14928 			if (notreadyp != NULL)
14929 				*notreadyp = B_TRUE;
14930 			continue;
14931 		}
14932 
14933 		if (zoneid != ALL_ZONES &&
14934 		    ipif->ipif_zoneid != zoneid &&
14935 		    ipif->ipif_zoneid != ALL_ZONES)
14936 			continue;
14937 
14938 		/*
14939 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14940 		 * are not valid as source addresses.
14941 		 */
14942 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14943 			continue;
14944 
14945 		/*
14946 		 * Check compatibility of local address for destination's
14947 		 * default label if we're on a labeled system.	Incompatible
14948 		 * addresses can't be used at all.
14949 		 */
14950 		if (dst_rhtp != NULL) {
14951 			boolean_t incompat;
14952 
14953 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14954 			    IPV4_VERSION, B_FALSE);
14955 			if (src_rhtp == NULL)
14956 				continue;
14957 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14958 			    src_rhtp->tpc_tp.tp_doi !=
14959 			    dst_rhtp->tpc_tp.tp_doi ||
14960 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14961 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14962 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14963 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14964 			TPC_RELE(src_rhtp);
14965 			if (incompat)
14966 				continue;
14967 		}
14968 
14969 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14970 
14971 		if (ipif->ipif_lcl_addr == dst) {
14972 			type = IPIF_LOCALADDR;
14973 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14974 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14975 			    IPIF_DIFFNET_DEPRECATED;
14976 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14977 			type = samenet ? IPIF_SAMENET_ALLZONES :
14978 			    IPIF_DIFFNET_ALLZONES;
14979 		} else {
14980 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14981 		}
14982 
14983 		if (type > best_type) {
14984 			best_type = type;
14985 			best_ipif = ipif;
14986 			if (best_type == IPIF_LOCALADDR)
14987 				break; /* can't get better */
14988 		}
14989 	} while ((ipif = next_ipif) != start_ipif);
14990 
14991 	if ((ipif = best_ipif) != NULL) {
14992 		mutex_enter(&ipif->ipif_ill->ill_lock);
14993 		if (IPIF_IS_CONDEMNED(ipif)) {
14994 			mutex_exit(&ipif->ipif_ill->ill_lock);
14995 			goto retry;
14996 		}
14997 		ipif_refhold_locked(ipif);
14998 
14999 		/*
15000 		 * For IPMP, update the source ipif rotor to the next ipif,
15001 		 * provided we can look it up.  (We must not use it if it's
15002 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15003 		 * ipif_free() checked ill_src_ipif.)
15004 		 */
15005 		if (IS_IPMP(ill) && ipif != NULL) {
15006 			next_ipif = ipif->ipif_next;
15007 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15008 				ill->ill_src_ipif = next_ipif;
15009 			else
15010 				ill->ill_src_ipif = NULL;
15011 		}
15012 		mutex_exit(&ipif->ipif_ill->ill_lock);
15013 	}
15014 
15015 	rw_exit(&ipst->ips_ill_g_lock);
15016 	if (usill != NULL)
15017 		ill_refrele(usill);
15018 	if (ipmp_ill != NULL)
15019 		ill_refrele(ipmp_ill);
15020 	if (dst_rhtp != NULL)
15021 		TPC_RELE(dst_rhtp);
15022 
15023 #ifdef DEBUG
15024 	if (ipif == NULL) {
15025 		char buf1[INET6_ADDRSTRLEN];
15026 
15027 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15028 		    ill->ill_name,
15029 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15030 	} else {
15031 		char buf1[INET6_ADDRSTRLEN];
15032 		char buf2[INET6_ADDRSTRLEN];
15033 
15034 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15035 		    ipif->ipif_ill->ill_name,
15036 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15037 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15038 		    buf2, sizeof (buf2))));
15039 	}
15040 #endif /* DEBUG */
15041 	return (ipif);
15042 }
15043 
15044 /*
15045  * Pick a source address based on the destination ill and an optional setsrc
15046  * address.
15047  * The result is stored in srcp. If generation is set, then put the source
15048  * generation number there before we look for the source address (to avoid
15049  * missing changes in the set of source addresses.
15050  * If flagsp is set, then us it to pass back ipif_flags.
15051  *
15052  * If the caller wants to cache the returned source address and detect when
15053  * that might be stale, the caller should pass in a generation argument,
15054  * which the caller can later compare against ips_src_generation
15055  *
15056  * The precedence order for selecting an IPv4 source address is:
15057  *  - RTF_SETSRC on the offlink ire always wins.
15058  *  - If usrsrc is set, swap the ill to be the usesrc one.
15059  *  - If IPMP is used on the ill, select a random address from the most
15060  *    preferred ones below:
15061  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15062  * 2. Not deprecated, not ALL_ZONES
15063  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15064  * 4. Not deprecated, ALL_ZONES
15065  * 5. If onlink destination, same subnet and deprecated
15066  * 6. Deprecated.
15067  *
15068  * We have lower preference for ALL_ZONES IP addresses,
15069  * as they pose problems with unlabeled destinations.
15070  *
15071  * Note that when multiple IP addresses match e.g., #1 we pick
15072  * the first one if IPMP is not in use. With IPMP we randomize.
15073  */
15074 int
15075 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15076     ipaddr_t multicast_ifaddr,
15077     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15078     uint32_t *generation, uint64_t *flagsp)
15079 {
15080 	ipif_t *ipif;
15081 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15082 
15083 	if (flagsp != NULL)
15084 		*flagsp = 0;
15085 
15086 	/*
15087 	 * Need to grab the generation number before we check to
15088 	 * avoid a race with a change to the set of local addresses.
15089 	 * No lock needed since the thread which updates the set of local
15090 	 * addresses use ipif/ill locks and exit those (hence a store memory
15091 	 * barrier) before doing the atomic increase of ips_src_generation.
15092 	 */
15093 	if (generation != NULL) {
15094 		*generation = ipst->ips_src_generation;
15095 	}
15096 
15097 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15098 		*srcp = multicast_ifaddr;
15099 		return (0);
15100 	}
15101 
15102 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15103 	if (setsrc != INADDR_ANY) {
15104 		*srcp = setsrc;
15105 		return (0);
15106 	}
15107 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15108 	if (ipif == NULL) {
15109 		if (notready)
15110 			return (ENETDOWN);
15111 		else
15112 			return (EADDRNOTAVAIL);
15113 	}
15114 	*srcp = ipif->ipif_lcl_addr;
15115 	if (flagsp != NULL)
15116 		*flagsp = ipif->ipif_flags;
15117 	ipif_refrele(ipif);
15118 	return (0);
15119 }
15120 
15121 /* ARGSUSED */
15122 int
15123 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15124 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15125 {
15126 	/*
15127 	 * ill_phyint_reinit merged the v4 and v6 into a single
15128 	 * ipsq.  We might not have been able to complete the
15129 	 * operation in ipif_set_values, if we could not become
15130 	 * exclusive.  If so restart it here.
15131 	 */
15132 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15133 }
15134 
15135 /*
15136  * Can operate on either a module or a driver queue.
15137  * Returns an error if not a module queue.
15138  */
15139 /* ARGSUSED */
15140 int
15141 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15142     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15143 {
15144 	queue_t		*q1 = q;
15145 	char 		*cp;
15146 	char		interf_name[LIFNAMSIZ];
15147 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15148 
15149 	if (q->q_next == NULL) {
15150 		ip1dbg((
15151 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15152 		return (EINVAL);
15153 	}
15154 
15155 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15156 		return (EALREADY);
15157 
15158 	do {
15159 		q1 = q1->q_next;
15160 	} while (q1->q_next);
15161 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15162 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15163 
15164 	/*
15165 	 * Here we are not going to delay the ioack until after
15166 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15167 	 * original ioctl message before sending the requests.
15168 	 */
15169 	return (ipif_set_values(q, mp, interf_name, &ppa));
15170 }
15171 
15172 /* ARGSUSED */
15173 int
15174 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15175     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15176 {
15177 	return (ENXIO);
15178 }
15179 
15180 /*
15181  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15182  * `irep'.  Returns a pointer to the next free `irep' entry
15183  * A mirror exists in ipif_delete_bcast_ires().
15184  *
15185  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15186  * done in ire_add.
15187  */
15188 static ire_t **
15189 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15190 {
15191 	ipaddr_t addr;
15192 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15193 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15194 	ill_t *ill = ipif->ipif_ill;
15195 	zoneid_t zoneid = ipif->ipif_zoneid;
15196 
15197 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15198 
15199 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15200 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15201 
15202 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15203 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15204 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15205 
15206 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15207 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15208 
15209 	/*
15210 	 * For backward compatibility, we create net broadcast IREs based on
15211 	 * the old "IP address class system", since some old machines only
15212 	 * respond to these class derived net broadcast.  However, we must not
15213 	 * create these net broadcast IREs if the subnetmask is shorter than
15214 	 * the IP address class based derived netmask.  Otherwise, we may
15215 	 * create a net broadcast address which is the same as an IP address
15216 	 * on the subnet -- and then TCP will refuse to talk to that address.
15217 	 */
15218 	if (netmask < subnetmask) {
15219 		addr = netmask & ipif->ipif_subnet;
15220 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15221 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15222 	}
15223 
15224 	/*
15225 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15226 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15227 	 * created.  Creating these broadcast IREs will only create confusion
15228 	 * as `addr' will be the same as the IP address.
15229 	 */
15230 	if (subnetmask != 0xFFFFFFFF) {
15231 		addr = ipif->ipif_subnet;
15232 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15233 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15234 	}
15235 
15236 	return (irep);
15237 }
15238 
15239 /*
15240  * Mirror of ipif_create_bcast_ires()
15241  */
15242 static void
15243 ipif_delete_bcast_ires(ipif_t *ipif)
15244 {
15245 	ipaddr_t	addr;
15246 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15247 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15248 	ill_t		*ill = ipif->ipif_ill;
15249 	zoneid_t	zoneid = ipif->ipif_zoneid;
15250 	ire_t		*ire;
15251 
15252 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15253 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15254 
15255 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15256 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15257 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15258 
15259 	ire = ire_lookup_bcast(ill, 0, zoneid);
15260 	ASSERT(ire != NULL);
15261 	ire_delete(ire); ire_refrele(ire);
15262 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15263 	ASSERT(ire != NULL);
15264 	ire_delete(ire); ire_refrele(ire);
15265 
15266 	/*
15267 	 * For backward compatibility, we create net broadcast IREs based on
15268 	 * the old "IP address class system", since some old machines only
15269 	 * respond to these class derived net broadcast.  However, we must not
15270 	 * create these net broadcast IREs if the subnetmask is shorter than
15271 	 * the IP address class based derived netmask.  Otherwise, we may
15272 	 * create a net broadcast address which is the same as an IP address
15273 	 * on the subnet -- and then TCP will refuse to talk to that address.
15274 	 */
15275 	if (netmask < subnetmask) {
15276 		addr = netmask & ipif->ipif_subnet;
15277 		ire = ire_lookup_bcast(ill, addr, zoneid);
15278 		ASSERT(ire != NULL);
15279 		ire_delete(ire); ire_refrele(ire);
15280 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15281 		ASSERT(ire != NULL);
15282 		ire_delete(ire); ire_refrele(ire);
15283 	}
15284 
15285 	/*
15286 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15287 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15288 	 * created.  Creating these broadcast IREs will only create confusion
15289 	 * as `addr' will be the same as the IP address.
15290 	 */
15291 	if (subnetmask != 0xFFFFFFFF) {
15292 		addr = ipif->ipif_subnet;
15293 		ire = ire_lookup_bcast(ill, addr, zoneid);
15294 		ASSERT(ire != NULL);
15295 		ire_delete(ire); ire_refrele(ire);
15296 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15297 		ASSERT(ire != NULL);
15298 		ire_delete(ire); ire_refrele(ire);
15299 	}
15300 }
15301 
15302 /*
15303  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15304  * from lifr_flags and the name from lifr_name.
15305  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15306  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15307  * Returns EINPROGRESS when mp has been consumed by queueing it on
15308  * ipx_pending_mp and the ioctl will complete in ip_rput.
15309  *
15310  * Can operate on either a module or a driver queue.
15311  * Returns an error if not a module queue.
15312  */
15313 /* ARGSUSED */
15314 int
15315 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15316     ip_ioctl_cmd_t *ipip, void *if_req)
15317 {
15318 	ill_t	*ill = q->q_ptr;
15319 	phyint_t *phyi;
15320 	ip_stack_t *ipst;
15321 	struct lifreq *lifr = if_req;
15322 	uint64_t new_flags;
15323 
15324 	ASSERT(ipif != NULL);
15325 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15326 
15327 	if (q->q_next == NULL) {
15328 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15329 		return (EINVAL);
15330 	}
15331 
15332 	/*
15333 	 * If we are not writer on 'q' then this interface exists already
15334 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15335 	 * so return EALREADY.
15336 	 */
15337 	if (ill != ipif->ipif_ill)
15338 		return (EALREADY);
15339 
15340 	if (ill->ill_name[0] != '\0')
15341 		return (EALREADY);
15342 
15343 	/*
15344 	 * If there's another ill already with the requested name, ensure
15345 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15346 	 * fuse together two unrelated ills, which will cause chaos.
15347 	 */
15348 	ipst = ill->ill_ipst;
15349 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15350 	    lifr->lifr_name, NULL);
15351 	if (phyi != NULL) {
15352 		ill_t *ill_mate = phyi->phyint_illv4;
15353 
15354 		if (ill_mate == NULL)
15355 			ill_mate = phyi->phyint_illv6;
15356 		ASSERT(ill_mate != NULL);
15357 
15358 		if (ill_mate->ill_media->ip_m_mac_type !=
15359 		    ill->ill_media->ip_m_mac_type) {
15360 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15361 			    "use the same ill name on differing media\n"));
15362 			return (EINVAL);
15363 		}
15364 	}
15365 
15366 	/*
15367 	 * We start off as IFF_IPV4 in ipif_allocate and become
15368 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15369 	 * The only flags that we read from user space are IFF_IPV4,
15370 	 * IFF_IPV6, and IFF_BROADCAST.
15371 	 *
15372 	 * This ill has not been inserted into the global list.
15373 	 * So we are still single threaded and don't need any lock
15374 	 *
15375 	 * Saniy check the flags.
15376 	 */
15377 
15378 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15379 	    ((lifr->lifr_flags & IFF_IPV6) ||
15380 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15381 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15382 		    "or IPv6 i.e., no broadcast \n"));
15383 		return (EINVAL);
15384 	}
15385 
15386 	new_flags =
15387 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15388 
15389 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15390 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15391 		    "IFF_IPV4 or IFF_IPV6\n"));
15392 		return (EINVAL);
15393 	}
15394 
15395 	/*
15396 	 * We always start off as IPv4, so only need to check for IPv6.
15397 	 */
15398 	if ((new_flags & IFF_IPV6) != 0) {
15399 		ill->ill_flags |= ILLF_IPV6;
15400 		ill->ill_flags &= ~ILLF_IPV4;
15401 	}
15402 
15403 	if ((new_flags & IFF_BROADCAST) != 0)
15404 		ipif->ipif_flags |= IPIF_BROADCAST;
15405 	else
15406 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15407 
15408 	/* We started off as V4. */
15409 	if (ill->ill_flags & ILLF_IPV6) {
15410 		ill->ill_phyint->phyint_illv6 = ill;
15411 		ill->ill_phyint->phyint_illv4 = NULL;
15412 	}
15413 
15414 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15415 }
15416 
15417 /* ARGSUSED */
15418 int
15419 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15420     ip_ioctl_cmd_t *ipip, void *if_req)
15421 {
15422 	/*
15423 	 * ill_phyint_reinit merged the v4 and v6 into a single
15424 	 * ipsq.  We might not have been able to complete the
15425 	 * slifname in ipif_set_values, if we could not become
15426 	 * exclusive.  If so restart it here
15427 	 */
15428 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15429 }
15430 
15431 /*
15432  * Return a pointer to the ipif which matches the index, IP version type and
15433  * zoneid.
15434  */
15435 ipif_t *
15436 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15437     ip_stack_t *ipst)
15438 {
15439 	ill_t	*ill;
15440 	ipif_t	*ipif = NULL;
15441 
15442 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15443 	if (ill != NULL) {
15444 		mutex_enter(&ill->ill_lock);
15445 		for (ipif = ill->ill_ipif; ipif != NULL;
15446 		    ipif = ipif->ipif_next) {
15447 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15448 			    zoneid == ipif->ipif_zoneid ||
15449 			    ipif->ipif_zoneid == ALL_ZONES)) {
15450 				ipif_refhold_locked(ipif);
15451 				break;
15452 			}
15453 		}
15454 		mutex_exit(&ill->ill_lock);
15455 		ill_refrele(ill);
15456 	}
15457 	return (ipif);
15458 }
15459 
15460 /*
15461  * Change an existing physical interface's index. If the new index
15462  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15463  * Finally, we update other systems which may have a dependence on the
15464  * index value.
15465  */
15466 /* ARGSUSED */
15467 int
15468 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15469     ip_ioctl_cmd_t *ipip, void *ifreq)
15470 {
15471 	ill_t		*ill;
15472 	phyint_t	*phyi;
15473 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15474 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15475 	uint_t	old_index, index;
15476 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15477 	avl_index_t	where;
15478 
15479 	if (ipip->ipi_cmd_type == IF_CMD)
15480 		index = ifr->ifr_index;
15481 	else
15482 		index = lifr->lifr_index;
15483 
15484 	/*
15485 	 * Only allow on physical interface. Also, index zero is illegal.
15486 	 */
15487 	ill = ipif->ipif_ill;
15488 	phyi = ill->ill_phyint;
15489 	if (ipif->ipif_id != 0 || index == 0) {
15490 		return (EINVAL);
15491 	}
15492 
15493 	/* If the index is not changing, no work to do */
15494 	if (phyi->phyint_ifindex == index)
15495 		return (0);
15496 
15497 	/*
15498 	 * Use phyint_exists() to determine if the new interface index
15499 	 * is already in use. If the index is unused then we need to
15500 	 * change the phyint's position in the phyint_list_avl_by_index
15501 	 * tree. If we do not do this, subsequent lookups (using the new
15502 	 * index value) will not find the phyint.
15503 	 */
15504 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15505 	if (phyint_exists(index, ipst)) {
15506 		rw_exit(&ipst->ips_ill_g_lock);
15507 		return (EEXIST);
15508 	}
15509 
15510 	/*
15511 	 * The new index is unused. Set it in the phyint. However we must not
15512 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15513 	 * changes. The event must be bound to old ifindex value.
15514 	 */
15515 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15516 	    &index, sizeof (index));
15517 
15518 	old_index = phyi->phyint_ifindex;
15519 	phyi->phyint_ifindex = index;
15520 
15521 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15522 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15523 	    &index, &where);
15524 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15525 	    phyi, where);
15526 	rw_exit(&ipst->ips_ill_g_lock);
15527 
15528 	/* Update SCTP's ILL list */
15529 	sctp_ill_reindex(ill, old_index);
15530 
15531 	/* Send the routing sockets message */
15532 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15533 	if (ILL_OTHER(ill))
15534 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15535 
15536 	/* Perhaps ilgs should use this ill */
15537 	update_conn_ill(NULL, ill->ill_ipst);
15538 	return (0);
15539 }
15540 
15541 /* ARGSUSED */
15542 int
15543 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15544     ip_ioctl_cmd_t *ipip, void *ifreq)
15545 {
15546 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15547 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15548 
15549 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15550 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15551 	/* Get the interface index */
15552 	if (ipip->ipi_cmd_type == IF_CMD) {
15553 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15554 	} else {
15555 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15556 	}
15557 	return (0);
15558 }
15559 
15560 /* ARGSUSED */
15561 int
15562 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15563     ip_ioctl_cmd_t *ipip, void *ifreq)
15564 {
15565 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15566 
15567 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15568 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15569 	/* Get the interface zone */
15570 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15571 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15572 	return (0);
15573 }
15574 
15575 /*
15576  * Set the zoneid of an interface.
15577  */
15578 /* ARGSUSED */
15579 int
15580 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15581     ip_ioctl_cmd_t *ipip, void *ifreq)
15582 {
15583 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15584 	int err = 0;
15585 	boolean_t need_up = B_FALSE;
15586 	zone_t *zptr;
15587 	zone_status_t status;
15588 	zoneid_t zoneid;
15589 
15590 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15591 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15592 		if (!is_system_labeled())
15593 			return (ENOTSUP);
15594 		zoneid = GLOBAL_ZONEID;
15595 	}
15596 
15597 	/* cannot assign instance zero to a non-global zone */
15598 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15599 		return (ENOTSUP);
15600 
15601 	/*
15602 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15603 	 * the event of a race with the zone shutdown processing, since IP
15604 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15605 	 * interface will be cleaned up even if the zone is shut down
15606 	 * immediately after the status check. If the interface can't be brought
15607 	 * down right away, and the zone is shut down before the restart
15608 	 * function is called, we resolve the possible races by rechecking the
15609 	 * zone status in the restart function.
15610 	 */
15611 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15612 		return (EINVAL);
15613 	status = zone_status_get(zptr);
15614 	zone_rele(zptr);
15615 
15616 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15617 		return (EINVAL);
15618 
15619 	if (ipif->ipif_flags & IPIF_UP) {
15620 		/*
15621 		 * If the interface is already marked up,
15622 		 * we call ipif_down which will take care
15623 		 * of ditching any IREs that have been set
15624 		 * up based on the old interface address.
15625 		 */
15626 		err = ipif_logical_down(ipif, q, mp);
15627 		if (err == EINPROGRESS)
15628 			return (err);
15629 		(void) ipif_down_tail(ipif);
15630 		need_up = B_TRUE;
15631 	}
15632 
15633 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15634 	return (err);
15635 }
15636 
15637 static int
15638 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15639     queue_t *q, mblk_t *mp, boolean_t need_up)
15640 {
15641 	int	err = 0;
15642 	ip_stack_t	*ipst;
15643 
15644 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15645 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15646 
15647 	if (CONN_Q(q))
15648 		ipst = CONNQ_TO_IPST(q);
15649 	else
15650 		ipst = ILLQ_TO_IPST(q);
15651 
15652 	/*
15653 	 * For exclusive stacks we don't allow a different zoneid than
15654 	 * global.
15655 	 */
15656 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15657 	    zoneid != GLOBAL_ZONEID)
15658 		return (EINVAL);
15659 
15660 	/* Set the new zone id. */
15661 	ipif->ipif_zoneid = zoneid;
15662 
15663 	/* Update sctp list */
15664 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15665 
15666 	/* The default multicast interface might have changed */
15667 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15668 
15669 	if (need_up) {
15670 		/*
15671 		 * Now bring the interface back up.  If this
15672 		 * is the only IPIF for the ILL, ipif_up
15673 		 * will have to re-bind to the device, so
15674 		 * we may get back EINPROGRESS, in which
15675 		 * case, this IOCTL will get completed in
15676 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15677 		 */
15678 		err = ipif_up(ipif, q, mp);
15679 	}
15680 	return (err);
15681 }
15682 
15683 /* ARGSUSED */
15684 int
15685 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15686     ip_ioctl_cmd_t *ipip, void *if_req)
15687 {
15688 	struct lifreq *lifr = (struct lifreq *)if_req;
15689 	zoneid_t zoneid;
15690 	zone_t *zptr;
15691 	zone_status_t status;
15692 
15693 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15694 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15695 		zoneid = GLOBAL_ZONEID;
15696 
15697 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15698 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15699 
15700 	/*
15701 	 * We recheck the zone status to resolve the following race condition:
15702 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15703 	 * 2) hme0:1 is up and can't be brought down right away;
15704 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15705 	 * 3) zone "myzone" is halted; the zone status switches to
15706 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15707 	 * the interfaces to remove - hme0:1 is not returned because it's not
15708 	 * yet in "myzone", so it won't be removed;
15709 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15710 	 * status check here, we would have hme0:1 in "myzone" after it's been
15711 	 * destroyed.
15712 	 * Note that if the status check fails, we need to bring the interface
15713 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15714 	 * ipif_up_done[_v6]().
15715 	 */
15716 	status = ZONE_IS_UNINITIALIZED;
15717 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15718 		status = zone_status_get(zptr);
15719 		zone_rele(zptr);
15720 	}
15721 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15722 		if (ipif->ipif_isv6) {
15723 			(void) ipif_up_done_v6(ipif);
15724 		} else {
15725 			(void) ipif_up_done(ipif);
15726 		}
15727 		return (EINVAL);
15728 	}
15729 
15730 	(void) ipif_down_tail(ipif);
15731 
15732 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15733 	    B_TRUE));
15734 }
15735 
15736 /*
15737  * Return the number of addresses on `ill' with one or more of the values
15738  * in `set' set and all of the values in `clear' clear.
15739  */
15740 static uint_t
15741 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15742 {
15743 	ipif_t	*ipif;
15744 	uint_t	cnt = 0;
15745 
15746 	ASSERT(IAM_WRITER_ILL(ill));
15747 
15748 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15749 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15750 			cnt++;
15751 
15752 	return (cnt);
15753 }
15754 
15755 /*
15756  * Return the number of migratable addresses on `ill' that are under
15757  * application control.
15758  */
15759 uint_t
15760 ill_appaddr_cnt(const ill_t *ill)
15761 {
15762 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15763 	    IPIF_NOFAILOVER));
15764 }
15765 
15766 /*
15767  * Return the number of point-to-point addresses on `ill'.
15768  */
15769 uint_t
15770 ill_ptpaddr_cnt(const ill_t *ill)
15771 {
15772 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15773 }
15774 
15775 /* ARGSUSED */
15776 int
15777 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15778 	ip_ioctl_cmd_t *ipip, void *ifreq)
15779 {
15780 	struct lifreq	*lifr = ifreq;
15781 
15782 	ASSERT(q->q_next == NULL);
15783 	ASSERT(CONN_Q(q));
15784 
15785 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15786 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15787 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15788 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15789 
15790 	return (0);
15791 }
15792 
15793 /* Find the previous ILL in this usesrc group */
15794 static ill_t *
15795 ill_prev_usesrc(ill_t *uill)
15796 {
15797 	ill_t *ill;
15798 
15799 	for (ill = uill->ill_usesrc_grp_next;
15800 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15801 	    ill = ill->ill_usesrc_grp_next)
15802 		/* do nothing */;
15803 	return (ill);
15804 }
15805 
15806 /*
15807  * Release all members of the usesrc group. This routine is called
15808  * from ill_delete when the interface being unplumbed is the
15809  * group head.
15810  *
15811  * This silently clears the usesrc that ifconfig setup.
15812  * An alternative would be to keep that ifindex, and drop packets on the floor
15813  * since no source address can be selected.
15814  * Even if we keep the current semantics, don't need a lock and a linked list.
15815  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15816  * the one that is being removed. Issue is how we return the usesrc users
15817  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15818  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15819  * ill walk, but the walker would need to insert in the ioctl response.
15820  */
15821 static void
15822 ill_disband_usesrc_group(ill_t *uill)
15823 {
15824 	ill_t *next_ill, *tmp_ill;
15825 	ip_stack_t	*ipst = uill->ill_ipst;
15826 
15827 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15828 	next_ill = uill->ill_usesrc_grp_next;
15829 
15830 	do {
15831 		ASSERT(next_ill != NULL);
15832 		tmp_ill = next_ill->ill_usesrc_grp_next;
15833 		ASSERT(tmp_ill != NULL);
15834 		next_ill->ill_usesrc_grp_next = NULL;
15835 		next_ill->ill_usesrc_ifindex = 0;
15836 		next_ill = tmp_ill;
15837 	} while (next_ill->ill_usesrc_ifindex != 0);
15838 	uill->ill_usesrc_grp_next = NULL;
15839 }
15840 
15841 /*
15842  * Remove the client usesrc ILL from the list and relink to a new list
15843  */
15844 int
15845 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15846 {
15847 	ill_t *ill, *tmp_ill;
15848 	ip_stack_t	*ipst = ucill->ill_ipst;
15849 
15850 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15851 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15852 
15853 	/*
15854 	 * Check if the usesrc client ILL passed in is not already
15855 	 * in use as a usesrc ILL i.e one whose source address is
15856 	 * in use OR a usesrc ILL is not already in use as a usesrc
15857 	 * client ILL
15858 	 */
15859 	if ((ucill->ill_usesrc_ifindex == 0) ||
15860 	    (uill->ill_usesrc_ifindex != 0)) {
15861 		return (-1);
15862 	}
15863 
15864 	ill = ill_prev_usesrc(ucill);
15865 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15866 
15867 	/* Remove from the current list */
15868 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15869 		/* Only two elements in the list */
15870 		ASSERT(ill->ill_usesrc_ifindex == 0);
15871 		ill->ill_usesrc_grp_next = NULL;
15872 	} else {
15873 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15874 	}
15875 
15876 	if (ifindex == 0) {
15877 		ucill->ill_usesrc_ifindex = 0;
15878 		ucill->ill_usesrc_grp_next = NULL;
15879 		return (0);
15880 	}
15881 
15882 	ucill->ill_usesrc_ifindex = ifindex;
15883 	tmp_ill = uill->ill_usesrc_grp_next;
15884 	uill->ill_usesrc_grp_next = ucill;
15885 	ucill->ill_usesrc_grp_next =
15886 	    (tmp_ill != NULL) ? tmp_ill : uill;
15887 	return (0);
15888 }
15889 
15890 /*
15891  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15892  * ip.c for locking details.
15893  */
15894 /* ARGSUSED */
15895 int
15896 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15897     ip_ioctl_cmd_t *ipip, void *ifreq)
15898 {
15899 	struct lifreq *lifr = (struct lifreq *)ifreq;
15900 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15901 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15902 	int err = 0, ret;
15903 	uint_t ifindex;
15904 	ipsq_t *ipsq = NULL;
15905 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15906 
15907 	ASSERT(IAM_WRITER_IPIF(ipif));
15908 	ASSERT(q->q_next == NULL);
15909 	ASSERT(CONN_Q(q));
15910 
15911 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15912 
15913 	ifindex = lifr->lifr_index;
15914 	if (ifindex == 0) {
15915 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15916 			/* non usesrc group interface, nothing to reset */
15917 			return (0);
15918 		}
15919 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15920 		/* valid reset request */
15921 		reset_flg = B_TRUE;
15922 	}
15923 
15924 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15925 	if (usesrc_ill == NULL) {
15926 		return (ENXIO);
15927 	}
15928 
15929 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15930 	    NEW_OP, B_TRUE);
15931 	if (ipsq == NULL) {
15932 		err = EINPROGRESS;
15933 		/* Operation enqueued on the ipsq of the usesrc ILL */
15934 		goto done;
15935 	}
15936 
15937 	/* USESRC isn't currently supported with IPMP */
15938 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15939 		err = ENOTSUP;
15940 		goto done;
15941 	}
15942 
15943 	/*
15944 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15945 	 * used by IPMP underlying interfaces, but someone might think it's
15946 	 * more general and try to use it independently with VNI.)
15947 	 */
15948 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15949 		err = ENOTSUP;
15950 		goto done;
15951 	}
15952 
15953 	/*
15954 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15955 	 * already a client then return EINVAL
15956 	 */
15957 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15958 		err = EINVAL;
15959 		goto done;
15960 	}
15961 
15962 	/*
15963 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15964 	 * be then this is a duplicate operation.
15965 	 */
15966 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15967 		err = 0;
15968 		goto done;
15969 	}
15970 
15971 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15972 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15973 	    usesrc_ill->ill_isv6));
15974 
15975 	/*
15976 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15977 	 * and the ill_usesrc_ifindex fields
15978 	 */
15979 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15980 
15981 	if (reset_flg) {
15982 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15983 		if (ret != 0) {
15984 			err = EINVAL;
15985 		}
15986 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15987 		goto done;
15988 	}
15989 
15990 	/*
15991 	 * Four possibilities to consider:
15992 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15993 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15994 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15995 	 * 4. Both are part of their respective usesrc groups
15996 	 */
15997 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15998 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15999 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16000 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16001 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16002 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16003 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16004 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16005 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16006 		/* Insert at head of list */
16007 		usesrc_cli_ill->ill_usesrc_grp_next =
16008 		    usesrc_ill->ill_usesrc_grp_next;
16009 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16010 	} else {
16011 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16012 		    ifindex);
16013 		if (ret != 0)
16014 			err = EINVAL;
16015 	}
16016 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16017 
16018 done:
16019 	if (ipsq != NULL)
16020 		ipsq_exit(ipsq);
16021 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16022 	ill_refrele(usesrc_ill);
16023 
16024 	/* Let conn_ixa caching know that source address selection changed */
16025 	ip_update_source_selection(ipst);
16026 
16027 	return (err);
16028 }
16029 
16030 /*
16031  * comparison function used by avl.
16032  */
16033 static int
16034 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16035 {
16036 
16037 	uint_t index;
16038 
16039 	ASSERT(phyip != NULL && index_ptr != NULL);
16040 
16041 	index = *((uint_t *)index_ptr);
16042 	/*
16043 	 * let the phyint with the lowest index be on top.
16044 	 */
16045 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16046 		return (1);
16047 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16048 		return (-1);
16049 	return (0);
16050 }
16051 
16052 /*
16053  * comparison function used by avl.
16054  */
16055 static int
16056 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16057 {
16058 	ill_t *ill;
16059 	int res = 0;
16060 
16061 	ASSERT(phyip != NULL && name_ptr != NULL);
16062 
16063 	if (((phyint_t *)phyip)->phyint_illv4)
16064 		ill = ((phyint_t *)phyip)->phyint_illv4;
16065 	else
16066 		ill = ((phyint_t *)phyip)->phyint_illv6;
16067 	ASSERT(ill != NULL);
16068 
16069 	res = strcmp(ill->ill_name, (char *)name_ptr);
16070 	if (res > 0)
16071 		return (1);
16072 	else if (res < 0)
16073 		return (-1);
16074 	return (0);
16075 }
16076 
16077 /*
16078  * This function is called on the unplumb path via ill_glist_delete() when
16079  * there are no ills left on the phyint and thus the phyint can be freed.
16080  */
16081 static void
16082 phyint_free(phyint_t *phyi)
16083 {
16084 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16085 
16086 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16087 
16088 	/*
16089 	 * If this phyint was an IPMP meta-interface, blow away the group.
16090 	 * This is safe to do because all of the illgrps have already been
16091 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16092 	 * If we're cleaning up as a result of failed initialization,
16093 	 * phyint_grp may be NULL.
16094 	 */
16095 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16096 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16097 		ipmp_grp_destroy(phyi->phyint_grp);
16098 		phyi->phyint_grp = NULL;
16099 		rw_exit(&ipst->ips_ipmp_lock);
16100 	}
16101 
16102 	/*
16103 	 * If this interface was under IPMP, take it out of the group.
16104 	 */
16105 	if (phyi->phyint_grp != NULL)
16106 		ipmp_phyint_leave_grp(phyi);
16107 
16108 	/*
16109 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16110 	 * will be freed in ipsq_exit().
16111 	 */
16112 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16113 	phyi->phyint_name[0] = '\0';
16114 
16115 	mi_free(phyi);
16116 }
16117 
16118 /*
16119  * Attach the ill to the phyint structure which can be shared by both
16120  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16121  * function is called from ipif_set_values and ill_lookup_on_name (for
16122  * loopback) where we know the name of the ill. We lookup the ill and if
16123  * there is one present already with the name use that phyint. Otherwise
16124  * reuse the one allocated by ill_init.
16125  */
16126 static void
16127 ill_phyint_reinit(ill_t *ill)
16128 {
16129 	boolean_t isv6 = ill->ill_isv6;
16130 	phyint_t *phyi_old;
16131 	phyint_t *phyi;
16132 	avl_index_t where = 0;
16133 	ill_t	*ill_other = NULL;
16134 	ip_stack_t	*ipst = ill->ill_ipst;
16135 
16136 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16137 
16138 	phyi_old = ill->ill_phyint;
16139 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16140 	    phyi_old->phyint_illv6 == NULL));
16141 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16142 	    phyi_old->phyint_illv4 == NULL));
16143 	ASSERT(phyi_old->phyint_ifindex == 0);
16144 
16145 	/*
16146 	 * Now that our ill has a name, set it in the phyint.
16147 	 */
16148 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16149 
16150 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16151 	    ill->ill_name, &where);
16152 
16153 	/*
16154 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16155 	 *    the global list of ills. So no other thread could have located
16156 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16157 	 * 2. Now locate the other protocol instance of this ill.
16158 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16159 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16160 	 *    of neither ill can change.
16161 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16162 	 *    other ill.
16163 	 * 5. Release all locks.
16164 	 */
16165 
16166 	/*
16167 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16168 	 * we are initializing IPv4.
16169 	 */
16170 	if (phyi != NULL) {
16171 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16172 		ASSERT(ill_other->ill_phyint != NULL);
16173 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16174 		    (!isv6 && ill_other->ill_isv6));
16175 		GRAB_ILL_LOCKS(ill, ill_other);
16176 		/*
16177 		 * We are potentially throwing away phyint_flags which
16178 		 * could be different from the one that we obtain from
16179 		 * ill_other->ill_phyint. But it is okay as we are assuming
16180 		 * that the state maintained within IP is correct.
16181 		 */
16182 		mutex_enter(&phyi->phyint_lock);
16183 		if (isv6) {
16184 			ASSERT(phyi->phyint_illv6 == NULL);
16185 			phyi->phyint_illv6 = ill;
16186 		} else {
16187 			ASSERT(phyi->phyint_illv4 == NULL);
16188 			phyi->phyint_illv4 = ill;
16189 		}
16190 
16191 		/*
16192 		 * Delete the old phyint and make its ipsq eligible
16193 		 * to be freed in ipsq_exit().
16194 		 */
16195 		phyi_old->phyint_illv4 = NULL;
16196 		phyi_old->phyint_illv6 = NULL;
16197 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16198 		phyi_old->phyint_name[0] = '\0';
16199 		mi_free(phyi_old);
16200 	} else {
16201 		mutex_enter(&ill->ill_lock);
16202 		/*
16203 		 * We don't need to acquire any lock, since
16204 		 * the ill is not yet visible globally  and we
16205 		 * have not yet released the ill_g_lock.
16206 		 */
16207 		phyi = phyi_old;
16208 		mutex_enter(&phyi->phyint_lock);
16209 		/* XXX We need a recovery strategy here. */
16210 		if (!phyint_assign_ifindex(phyi, ipst))
16211 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16212 
16213 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16214 		    (void *)phyi, where);
16215 
16216 		(void) avl_find(&ipst->ips_phyint_g_list->
16217 		    phyint_list_avl_by_index,
16218 		    &phyi->phyint_ifindex, &where);
16219 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16220 		    (void *)phyi, where);
16221 	}
16222 
16223 	/*
16224 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16225 	 * pending mp is not affected because that is per ill basis.
16226 	 */
16227 	ill->ill_phyint = phyi;
16228 
16229 	/*
16230 	 * Now that the phyint's ifindex has been assigned, complete the
16231 	 * remaining
16232 	 */
16233 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16234 	if (ill->ill_isv6) {
16235 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16236 		    ill->ill_phyint->phyint_ifindex;
16237 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16238 	} else {
16239 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16240 	}
16241 
16242 	/*
16243 	 * Generate an event within the hooks framework to indicate that
16244 	 * a new interface has just been added to IP.  For this event to
16245 	 * be generated, the network interface must, at least, have an
16246 	 * ifindex assigned to it.  (We don't generate the event for
16247 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16248 	 *
16249 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16250 	 * that the ordering of delivered events to listeners matches the
16251 	 * order of them in the kernel.
16252 	 */
16253 	if (!IS_LOOPBACK(ill)) {
16254 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16255 		    ill->ill_name_length);
16256 	}
16257 	RELEASE_ILL_LOCKS(ill, ill_other);
16258 	mutex_exit(&phyi->phyint_lock);
16259 }
16260 
16261 /*
16262  * Notify any downstream modules of the name of this interface.
16263  * An M_IOCTL is used even though we don't expect a successful reply.
16264  * Any reply message from the driver (presumably an M_IOCNAK) will
16265  * eventually get discarded somewhere upstream.  The message format is
16266  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16267  * to IP.
16268  */
16269 static void
16270 ip_ifname_notify(ill_t *ill, queue_t *q)
16271 {
16272 	mblk_t *mp1, *mp2;
16273 	struct iocblk *iocp;
16274 	struct lifreq *lifr;
16275 
16276 	mp1 = mkiocb(SIOCSLIFNAME);
16277 	if (mp1 == NULL)
16278 		return;
16279 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16280 	if (mp2 == NULL) {
16281 		freeb(mp1);
16282 		return;
16283 	}
16284 
16285 	mp1->b_cont = mp2;
16286 	iocp = (struct iocblk *)mp1->b_rptr;
16287 	iocp->ioc_count = sizeof (struct lifreq);
16288 
16289 	lifr = (struct lifreq *)mp2->b_rptr;
16290 	mp2->b_wptr += sizeof (struct lifreq);
16291 	bzero(lifr, sizeof (struct lifreq));
16292 
16293 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16294 	lifr->lifr_ppa = ill->ill_ppa;
16295 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16296 
16297 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16298 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16299 	putnext(q, mp1);
16300 }
16301 
16302 static int
16303 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16304 {
16305 	int		err;
16306 	ip_stack_t	*ipst = ill->ill_ipst;
16307 	phyint_t	*phyi = ill->ill_phyint;
16308 
16309 	/* Set the obsolete NDD per-interface forwarding name. */
16310 	err = ill_set_ndd_name(ill);
16311 	if (err != 0) {
16312 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16313 		    err);
16314 	}
16315 
16316 	/*
16317 	 * Now that ill_name is set, the configuration for the IPMP
16318 	 * meta-interface can be performed.
16319 	 */
16320 	if (IS_IPMP(ill)) {
16321 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16322 		/*
16323 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16324 		 * meta-interface and we need to create the IPMP group.
16325 		 */
16326 		if (phyi->phyint_grp == NULL) {
16327 			/*
16328 			 * If someone has renamed another IPMP group to have
16329 			 * the same name as our interface, bail.
16330 			 */
16331 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16332 				rw_exit(&ipst->ips_ipmp_lock);
16333 				return (EEXIST);
16334 			}
16335 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16336 			if (phyi->phyint_grp == NULL) {
16337 				rw_exit(&ipst->ips_ipmp_lock);
16338 				return (ENOMEM);
16339 			}
16340 		}
16341 		rw_exit(&ipst->ips_ipmp_lock);
16342 	}
16343 
16344 	/* Tell downstream modules where they are. */
16345 	ip_ifname_notify(ill, q);
16346 
16347 	/*
16348 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16349 	 * Error cases are ENOMEM ...
16350 	 */
16351 	err = ill_dl_phys(ill, ipif, mp, q);
16352 
16353 	if (ill->ill_isv6) {
16354 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16355 		if (ipst->ips_mld_slowtimeout_id == 0) {
16356 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16357 			    (void *)ipst,
16358 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16359 		}
16360 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16361 	} else {
16362 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16363 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16364 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16365 			    (void *)ipst,
16366 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16367 		}
16368 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16369 	}
16370 
16371 	return (err);
16372 }
16373 
16374 /*
16375  * Common routine for ppa and ifname setting. Should be called exclusive.
16376  *
16377  * Returns EINPROGRESS when mp has been consumed by queueing it on
16378  * ipx_pending_mp and the ioctl will complete in ip_rput.
16379  *
16380  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16381  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16382  * For SLIFNAME, we pass these values back to the userland.
16383  */
16384 static int
16385 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16386 {
16387 	ill_t	*ill;
16388 	ipif_t	*ipif;
16389 	ipsq_t	*ipsq;
16390 	char	*ppa_ptr;
16391 	char	*old_ptr;
16392 	char	old_char;
16393 	int	error;
16394 	ip_stack_t	*ipst;
16395 
16396 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16397 	ASSERT(q->q_next != NULL);
16398 	ASSERT(interf_name != NULL);
16399 
16400 	ill = (ill_t *)q->q_ptr;
16401 	ipst = ill->ill_ipst;
16402 
16403 	ASSERT(ill->ill_ipst != NULL);
16404 	ASSERT(ill->ill_name[0] == '\0');
16405 	ASSERT(IAM_WRITER_ILL(ill));
16406 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16407 	ASSERT(ill->ill_ppa == UINT_MAX);
16408 
16409 	ill->ill_defend_start = ill->ill_defend_count = 0;
16410 	/* The ppa is sent down by ifconfig or is chosen */
16411 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16412 		return (EINVAL);
16413 	}
16414 
16415 	/*
16416 	 * make sure ppa passed in is same as ppa in the name.
16417 	 * This check is not made when ppa == UINT_MAX in that case ppa
16418 	 * in the name could be anything. System will choose a ppa and
16419 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16420 	 */
16421 	if (*new_ppa_ptr != UINT_MAX) {
16422 		/* stoi changes the pointer */
16423 		old_ptr = ppa_ptr;
16424 		/*
16425 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16426 		 * (they don't have an externally visible ppa).  We assign one
16427 		 * here so that we can manage the interface.  Note that in
16428 		 * the past this value was always 0 for DLPI 1 drivers.
16429 		 */
16430 		if (*new_ppa_ptr == 0)
16431 			*new_ppa_ptr = stoi(&old_ptr);
16432 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16433 			return (EINVAL);
16434 	}
16435 	/*
16436 	 * terminate string before ppa
16437 	 * save char at that location.
16438 	 */
16439 	old_char = ppa_ptr[0];
16440 	ppa_ptr[0] = '\0';
16441 
16442 	ill->ill_ppa = *new_ppa_ptr;
16443 	/*
16444 	 * Finish as much work now as possible before calling ill_glist_insert
16445 	 * which makes the ill globally visible and also merges it with the
16446 	 * other protocol instance of this phyint. The remaining work is
16447 	 * done after entering the ipsq which may happen sometime later.
16448 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16449 	 */
16450 	ipif = ill->ill_ipif;
16451 
16452 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16453 	ipif_assign_seqid(ipif);
16454 
16455 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16456 		ill->ill_flags |= ILLF_IPV4;
16457 
16458 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16459 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16460 
16461 	if (ill->ill_flags & ILLF_IPV6) {
16462 
16463 		ill->ill_isv6 = B_TRUE;
16464 		ill_set_inputfn(ill);
16465 		if (ill->ill_rq != NULL) {
16466 			ill->ill_rq->q_qinfo = &iprinitv6;
16467 		}
16468 
16469 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16470 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16471 		ipif->ipif_v6subnet = ipv6_all_zeros;
16472 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16473 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16474 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16475 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16476 		/*
16477 		 * point-to-point or Non-mulicast capable
16478 		 * interfaces won't do NUD unless explicitly
16479 		 * configured to do so.
16480 		 */
16481 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16482 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16483 			ill->ill_flags |= ILLF_NONUD;
16484 		}
16485 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16486 		if (ill->ill_flags & ILLF_NOARP) {
16487 			/*
16488 			 * Note: xresolv interfaces will eventually need
16489 			 * NOARP set here as well, but that will require
16490 			 * those external resolvers to have some
16491 			 * knowledge of that flag and act appropriately.
16492 			 * Not to be changed at present.
16493 			 */
16494 			ill->ill_flags &= ~ILLF_NOARP;
16495 		}
16496 		/*
16497 		 * Set the ILLF_ROUTER flag according to the global
16498 		 * IPv6 forwarding policy.
16499 		 */
16500 		if (ipst->ips_ipv6_forward != 0)
16501 			ill->ill_flags |= ILLF_ROUTER;
16502 	} else if (ill->ill_flags & ILLF_IPV4) {
16503 		ill->ill_isv6 = B_FALSE;
16504 		ill_set_inputfn(ill);
16505 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16506 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16507 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16508 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16509 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16510 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16511 		/*
16512 		 * Set the ILLF_ROUTER flag according to the global
16513 		 * IPv4 forwarding policy.
16514 		 */
16515 		if (ipst->ips_ip_g_forward != 0)
16516 			ill->ill_flags |= ILLF_ROUTER;
16517 	}
16518 
16519 	ASSERT(ill->ill_phyint != NULL);
16520 
16521 	/*
16522 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16523 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16524 	 */
16525 	if (!ill_allocate_mibs(ill))
16526 		return (ENOMEM);
16527 
16528 	/*
16529 	 * Pick a default sap until we get the DL_INFO_ACK back from
16530 	 * the driver.
16531 	 */
16532 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16533 	    ill->ill_media->ip_m_ipv4sap;
16534 
16535 	ill->ill_ifname_pending = 1;
16536 	ill->ill_ifname_pending_err = 0;
16537 
16538 	/*
16539 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16540 	 * that were joined while this ill was not bound to the DLPI link need
16541 	 * to be recovered by ill_recover_multicast().
16542 	 */
16543 	ill->ill_need_recover_multicast = 1;
16544 
16545 	ill_refhold(ill);
16546 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16547 	if ((error = ill_glist_insert(ill, interf_name,
16548 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16549 		ill->ill_ppa = UINT_MAX;
16550 		ill->ill_name[0] = '\0';
16551 		/*
16552 		 * undo null termination done above.
16553 		 */
16554 		ppa_ptr[0] = old_char;
16555 		rw_exit(&ipst->ips_ill_g_lock);
16556 		ill_refrele(ill);
16557 		return (error);
16558 	}
16559 
16560 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16561 
16562 	/*
16563 	 * When we return the buffer pointed to by interf_name should contain
16564 	 * the same name as in ill_name.
16565 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16566 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16567 	 * so copy full name and update the ppa ptr.
16568 	 * When ppa passed in != UINT_MAX all values are correct just undo
16569 	 * null termination, this saves a bcopy.
16570 	 */
16571 	if (*new_ppa_ptr == UINT_MAX) {
16572 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16573 		*new_ppa_ptr = ill->ill_ppa;
16574 	} else {
16575 		/*
16576 		 * undo null termination done above.
16577 		 */
16578 		ppa_ptr[0] = old_char;
16579 	}
16580 
16581 	/* Let SCTP know about this ILL */
16582 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16583 
16584 	/*
16585 	 * ill_glist_insert has made the ill visible globally, and
16586 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16587 	 * we need to hold the ips_ill_g_lock across the call to enter the
16588 	 * ipsq to enforce atomicity and prevent reordering. In the event
16589 	 * the ipsq has changed, and if the new ipsq is currently busy,
16590 	 * we need to make sure that this half-completed ioctl is ahead of
16591 	 * any subsequent ioctl. We achieve this by not dropping the
16592 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16593 	 * ensuring that new ioctls can't start.
16594 	 */
16595 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16596 	    B_TRUE);
16597 
16598 	rw_exit(&ipst->ips_ill_g_lock);
16599 	ill_refrele(ill);
16600 	if (ipsq == NULL)
16601 		return (EINPROGRESS);
16602 
16603 	/*
16604 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16605 	 */
16606 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16607 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16608 	else
16609 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16610 
16611 	error = ipif_set_values_tail(ill, ipif, mp, q);
16612 	ipsq_exit(ipsq);
16613 	if (error != 0 && error != EINPROGRESS) {
16614 		/*
16615 		 * restore previous values
16616 		 */
16617 		ill->ill_isv6 = B_FALSE;
16618 		ill_set_inputfn(ill);
16619 	}
16620 	return (error);
16621 }
16622 
16623 void
16624 ipif_init(ip_stack_t *ipst)
16625 {
16626 	int i;
16627 
16628 	for (i = 0; i < MAX_G_HEADS; i++) {
16629 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16630 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16631 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16632 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16633 	}
16634 
16635 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16636 	    ill_phyint_compare_index,
16637 	    sizeof (phyint_t),
16638 	    offsetof(struct phyint, phyint_avl_by_index));
16639 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16640 	    ill_phyint_compare_name,
16641 	    sizeof (phyint_t),
16642 	    offsetof(struct phyint, phyint_avl_by_name));
16643 }
16644 
16645 /*
16646  * Save enough information so that we can recreate the IRE if
16647  * the interface goes down and then up.
16648  */
16649 void
16650 ill_save_ire(ill_t *ill, ire_t *ire)
16651 {
16652 	mblk_t	*save_mp;
16653 
16654 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16655 	if (save_mp != NULL) {
16656 		ifrt_t	*ifrt;
16657 
16658 		save_mp->b_wptr += sizeof (ifrt_t);
16659 		ifrt = (ifrt_t *)save_mp->b_rptr;
16660 		bzero(ifrt, sizeof (ifrt_t));
16661 		ifrt->ifrt_type = ire->ire_type;
16662 		if (ire->ire_ipversion == IPV4_VERSION) {
16663 			ASSERT(!ill->ill_isv6);
16664 			ifrt->ifrt_addr = ire->ire_addr;
16665 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16666 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16667 			ifrt->ifrt_mask = ire->ire_mask;
16668 		} else {
16669 			ASSERT(ill->ill_isv6);
16670 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16671 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16672 			mutex_enter(&ire->ire_lock);
16673 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16674 			mutex_exit(&ire->ire_lock);
16675 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16676 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16677 		}
16678 		ifrt->ifrt_flags = ire->ire_flags;
16679 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16680 		mutex_enter(&ill->ill_saved_ire_lock);
16681 		save_mp->b_cont = ill->ill_saved_ire_mp;
16682 		ill->ill_saved_ire_mp = save_mp;
16683 		ill->ill_saved_ire_cnt++;
16684 		mutex_exit(&ill->ill_saved_ire_lock);
16685 	}
16686 }
16687 
16688 /*
16689  * Remove one entry from ill_saved_ire_mp.
16690  */
16691 void
16692 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16693 {
16694 	mblk_t	**mpp;
16695 	mblk_t	*mp;
16696 	ifrt_t	*ifrt;
16697 
16698 	/* Remove from ill_saved_ire_mp list if it is there */
16699 	mutex_enter(&ill->ill_saved_ire_lock);
16700 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16701 	    mpp = &(*mpp)->b_cont) {
16702 		in6_addr_t	gw_addr_v6;
16703 
16704 		/*
16705 		 * On a given ill, the tuple of address, gateway, mask,
16706 		 * ire_type, and zoneid is unique for each saved IRE.
16707 		 */
16708 		mp = *mpp;
16709 		ifrt = (ifrt_t *)mp->b_rptr;
16710 		/* ire_gateway_addr_v6 can change - need lock */
16711 		mutex_enter(&ire->ire_lock);
16712 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16713 		mutex_exit(&ire->ire_lock);
16714 
16715 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16716 		    ifrt->ifrt_type != ire->ire_type)
16717 			continue;
16718 
16719 		if (ill->ill_isv6 ?
16720 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16721 		    &ire->ire_addr_v6) &&
16722 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16723 		    &gw_addr_v6) &&
16724 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16725 		    &ire->ire_mask_v6)) :
16726 		    (ifrt->ifrt_addr == ire->ire_addr &&
16727 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16728 		    ifrt->ifrt_mask == ire->ire_mask)) {
16729 			*mpp = mp->b_cont;
16730 			ill->ill_saved_ire_cnt--;
16731 			freeb(mp);
16732 			break;
16733 		}
16734 	}
16735 	mutex_exit(&ill->ill_saved_ire_lock);
16736 }
16737 
16738 /*
16739  * IP multirouting broadcast routes handling
16740  * Append CGTP broadcast IREs to regular ones created
16741  * at ifconfig time.
16742  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16743  * the destination and the gateway are broadcast addresses.
16744  * The caller has verified that the destination is an IRE_BROADCAST and that
16745  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16746  * we create a MULTIRT IRE_BROADCAST.
16747  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16748  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16749  */
16750 static void
16751 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16752 {
16753 	ire_t *ire_prim;
16754 
16755 	ASSERT(ire != NULL);
16756 
16757 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16758 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16759 	    NULL);
16760 	if (ire_prim != NULL) {
16761 		/*
16762 		 * We are in the special case of broadcasts for
16763 		 * CGTP. We add an IRE_BROADCAST that holds
16764 		 * the RTF_MULTIRT flag, the destination
16765 		 * address and the low level
16766 		 * info of ire_prim. In other words, CGTP
16767 		 * broadcast is added to the redundant ipif.
16768 		 */
16769 		ill_t *ill_prim;
16770 		ire_t  *bcast_ire;
16771 
16772 		ill_prim = ire_prim->ire_ill;
16773 
16774 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16775 		    (void *)ire_prim, (void *)ill_prim));
16776 
16777 		bcast_ire = ire_create(
16778 		    (uchar_t *)&ire->ire_addr,
16779 		    (uchar_t *)&ip_g_all_ones,
16780 		    (uchar_t *)&ire->ire_gateway_addr,
16781 		    IRE_BROADCAST,
16782 		    ill_prim,
16783 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16784 		    ire->ire_flags | RTF_KERNEL,
16785 		    NULL,
16786 		    ipst);
16787 
16788 		/*
16789 		 * Here we assume that ire_add does head insertion so that
16790 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16791 		 */
16792 		if (bcast_ire != NULL) {
16793 			if (ire->ire_flags & RTF_SETSRC) {
16794 				bcast_ire->ire_setsrc_addr =
16795 				    ire->ire_setsrc_addr;
16796 			}
16797 			bcast_ire = ire_add(bcast_ire);
16798 			if (bcast_ire != NULL) {
16799 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16800 				    "added bcast_ire %p\n",
16801 				    (void *)bcast_ire));
16802 
16803 				ill_save_ire(ill_prim, bcast_ire);
16804 				ire_refrele(bcast_ire);
16805 			}
16806 		}
16807 		ire_refrele(ire_prim);
16808 	}
16809 }
16810 
16811 /*
16812  * IP multirouting broadcast routes handling
16813  * Remove the broadcast ire.
16814  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16815  * the destination and the gateway are broadcast addresses.
16816  * The caller has only verified that RTF_MULTIRT was set. We check
16817  * that the destination is broadcast and that the gateway is a broadcast
16818  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16819  */
16820 static void
16821 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16822 {
16823 	ASSERT(ire != NULL);
16824 
16825 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16826 		ire_t *ire_prim;
16827 
16828 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16829 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16830 		    ipst, NULL);
16831 		if (ire_prim != NULL) {
16832 			ill_t *ill_prim;
16833 			ire_t  *bcast_ire;
16834 
16835 			ill_prim = ire_prim->ire_ill;
16836 
16837 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16838 			    "ire_prim %p, ill_prim %p\n",
16839 			    (void *)ire_prim, (void *)ill_prim));
16840 
16841 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16842 			    ire->ire_gateway_addr, IRE_BROADCAST,
16843 			    ill_prim, ALL_ZONES, NULL,
16844 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16845 			    MATCH_IRE_MASK, 0, ipst, NULL);
16846 
16847 			if (bcast_ire != NULL) {
16848 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16849 				    "looked up bcast_ire %p\n",
16850 				    (void *)bcast_ire));
16851 				ill_remove_saved_ire(bcast_ire->ire_ill,
16852 				    bcast_ire);
16853 				ire_delete(bcast_ire);
16854 				ire_refrele(bcast_ire);
16855 			}
16856 			ire_refrele(ire_prim);
16857 		}
16858 	}
16859 }
16860 
16861 /*
16862  * Derive an interface id from the link layer address.
16863  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16864  */
16865 static void
16866 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16867 {
16868 	char		*addr;
16869 
16870 	/*
16871 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16872 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16873 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16874 	 * interface ID on IPv6 interfaces above links that actually have real
16875 	 * Ethernet addresses.
16876 	 */
16877 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16878 		/* Form EUI-64 like address */
16879 		addr = (char *)&v6addr->s6_addr32[2];
16880 		bcopy(ill->ill_phys_addr, addr, 3);
16881 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16882 		addr[3] = (char)0xff;
16883 		addr[4] = (char)0xfe;
16884 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16885 	}
16886 }
16887 
16888 /* ARGSUSED */
16889 static void
16890 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16891 {
16892 }
16893 
16894 typedef struct ipmp_ifcookie {
16895 	uint32_t	ic_hostid;
16896 	char		ic_ifname[LIFNAMSIZ];
16897 	char		ic_zonename[ZONENAME_MAX];
16898 } ipmp_ifcookie_t;
16899 
16900 /*
16901  * Construct a pseudo-random interface ID for the IPMP interface that's both
16902  * predictable and (almost) guaranteed to be unique.
16903  */
16904 static void
16905 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16906 {
16907 	zone_t		*zp;
16908 	uint8_t		*addr;
16909 	uchar_t		hash[16];
16910 	ulong_t 	hostid;
16911 	MD5_CTX		ctx;
16912 	ipmp_ifcookie_t	ic = { 0 };
16913 
16914 	ASSERT(IS_IPMP(ill));
16915 
16916 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16917 	ic.ic_hostid = htonl((uint32_t)hostid);
16918 
16919 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16920 
16921 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16922 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16923 		zone_rele(zp);
16924 	}
16925 
16926 	MD5Init(&ctx);
16927 	MD5Update(&ctx, &ic, sizeof (ic));
16928 	MD5Final(hash, &ctx);
16929 
16930 	/*
16931 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16932 	 */
16933 	addr = &v6addr->s6_addr8[8];
16934 	bcopy(hash + 8, addr, sizeof (uint64_t));
16935 	addr[0] &= ~0x2;				/* set local bit */
16936 }
16937 
16938 /*
16939  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16940  */
16941 static void
16942 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16943 {
16944 	phyint_t *phyi = ill->ill_phyint;
16945 
16946 	/*
16947 	 * Check PHYI_MULTI_BCAST and length of physical
16948 	 * address to determine if we use the mapping or the
16949 	 * broadcast address.
16950 	 */
16951 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16952 	    ill->ill_phys_addr_length != ETHERADDRL) {
16953 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16954 		return;
16955 	}
16956 	m_physaddr[0] = 0x33;
16957 	m_physaddr[1] = 0x33;
16958 	m_physaddr[2] = m_ip6addr[12];
16959 	m_physaddr[3] = m_ip6addr[13];
16960 	m_physaddr[4] = m_ip6addr[14];
16961 	m_physaddr[5] = m_ip6addr[15];
16962 }
16963 
16964 /*
16965  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16966  */
16967 static void
16968 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16969 {
16970 	phyint_t *phyi = ill->ill_phyint;
16971 
16972 	/*
16973 	 * Check PHYI_MULTI_BCAST and length of physical
16974 	 * address to determine if we use the mapping or the
16975 	 * broadcast address.
16976 	 */
16977 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16978 	    ill->ill_phys_addr_length != ETHERADDRL) {
16979 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16980 		return;
16981 	}
16982 	m_physaddr[0] = 0x01;
16983 	m_physaddr[1] = 0x00;
16984 	m_physaddr[2] = 0x5e;
16985 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16986 	m_physaddr[4] = m_ipaddr[2];
16987 	m_physaddr[5] = m_ipaddr[3];
16988 }
16989 
16990 /* ARGSUSED */
16991 static void
16992 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16993 {
16994 	/*
16995 	 * for the MULTI_BCAST case and other cases when we want to
16996 	 * use the link-layer broadcast address for multicast.
16997 	 */
16998 	uint8_t	*bphys_addr;
16999 	dl_unitdata_req_t *dlur;
17000 
17001 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17002 	if (ill->ill_sap_length < 0) {
17003 		bphys_addr = (uchar_t *)dlur +
17004 		    dlur->dl_dest_addr_offset;
17005 	} else  {
17006 		bphys_addr = (uchar_t *)dlur +
17007 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17008 	}
17009 
17010 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17011 }
17012 
17013 /*
17014  * Derive IPoIB interface id from the link layer address.
17015  */
17016 static void
17017 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17018 {
17019 	char		*addr;
17020 
17021 	ASSERT(ill->ill_phys_addr_length == 20);
17022 	addr = (char *)&v6addr->s6_addr32[2];
17023 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17024 	/*
17025 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17026 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17027 	 * rules. In these cases, the IBA considers these GUIDs to be in
17028 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17029 	 * required; vendors are required not to assign global EUI-64's
17030 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17031 	 * of the interface identifier. Whether the GUID is in modified
17032 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17033 	 * bit set to 1.
17034 	 */
17035 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17036 }
17037 
17038 /*
17039  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17040  * Note on mapping from multicast IP addresses to IPoIB multicast link
17041  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17042  * The format of an IPoIB multicast address is:
17043  *
17044  *  4 byte QPN      Scope Sign.  Pkey
17045  * +--------------------------------------------+
17046  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17047  * +--------------------------------------------+
17048  *
17049  * The Scope and Pkey components are properties of the IBA port and
17050  * network interface. They can be ascertained from the broadcast address.
17051  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17052  */
17053 static void
17054 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17055 {
17056 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17057 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17058 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17059 	uint8_t	*bphys_addr;
17060 	dl_unitdata_req_t *dlur;
17061 
17062 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17063 
17064 	/*
17065 	 * RFC 4391: IPv4 MGID is 28-bit long.
17066 	 */
17067 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17068 	m_physaddr[17] = m_ipaddr[1];
17069 	m_physaddr[18] = m_ipaddr[2];
17070 	m_physaddr[19] = m_ipaddr[3];
17071 
17072 
17073 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17074 	if (ill->ill_sap_length < 0) {
17075 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17076 	} else  {
17077 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17078 		    ill->ill_sap_length;
17079 	}
17080 	/*
17081 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17082 	 */
17083 	m_physaddr[5] = bphys_addr[5];
17084 	m_physaddr[8] = bphys_addr[8];
17085 	m_physaddr[9] = bphys_addr[9];
17086 }
17087 
17088 static void
17089 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17090 {
17091 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17092 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17093 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17094 	uint8_t	*bphys_addr;
17095 	dl_unitdata_req_t *dlur;
17096 
17097 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17098 
17099 	/*
17100 	 * RFC 4391: IPv4 MGID is 80-bit long.
17101 	 */
17102 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17103 
17104 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17105 	if (ill->ill_sap_length < 0) {
17106 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17107 	} else  {
17108 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17109 		    ill->ill_sap_length;
17110 	}
17111 	/*
17112 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17113 	 */
17114 	m_physaddr[5] = bphys_addr[5];
17115 	m_physaddr[8] = bphys_addr[8];
17116 	m_physaddr[9] = bphys_addr[9];
17117 }
17118 
17119 /*
17120  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17121  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17122  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17123  * of RFC4213.
17124  */
17125 static void
17126 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17127 {
17128 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17129 	v6addr->s6_addr32[2] = 0;
17130 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17131 }
17132 
17133 /*
17134  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17135  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17136  * id.
17137  */
17138 static void
17139 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17140 {
17141 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17142 
17143 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17144 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17145 }
17146 
17147 static void
17148 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17149 {
17150 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17151 }
17152 
17153 static void
17154 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17155 {
17156 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17157 }
17158 
17159 static void
17160 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17161 {
17162 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17163 }
17164 
17165 static void
17166 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17167 {
17168 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17169 }
17170 
17171 /*
17172  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17173  * Returns an held ill, or NULL.
17174  */
17175 ill_t *
17176 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17177     ip_stack_t *ipst)
17178 {
17179 	ill_t	*ill;
17180 	ipif_t	*ipif;
17181 
17182 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17183 	if (ill == NULL)
17184 		return (NULL);
17185 
17186 	mutex_enter(&ill->ill_lock);
17187 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17188 		if (IPIF_IS_CONDEMNED(ipif))
17189 			continue;
17190 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17191 		    ipif->ipif_zoneid != ALL_ZONES)
17192 			continue;
17193 
17194 		mutex_exit(&ill->ill_lock);
17195 		return (ill);
17196 	}
17197 	mutex_exit(&ill->ill_lock);
17198 	ill_refrele(ill);
17199 	return (NULL);
17200 }
17201 
17202 /*
17203  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17204  * If a pointer to an ipif_t is returned then the caller will need to do
17205  * an ill_refrele().
17206  */
17207 ipif_t *
17208 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17209     ip_stack_t *ipst)
17210 {
17211 	ipif_t *ipif;
17212 	ill_t *ill;
17213 
17214 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17215 	if (ill == NULL)
17216 		return (NULL);
17217 
17218 	mutex_enter(&ill->ill_lock);
17219 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17220 		mutex_exit(&ill->ill_lock);
17221 		ill_refrele(ill);
17222 		return (NULL);
17223 	}
17224 
17225 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17226 		if (!IPIF_CAN_LOOKUP(ipif))
17227 			continue;
17228 		if (lifidx == ipif->ipif_id) {
17229 			ipif_refhold_locked(ipif);
17230 			break;
17231 		}
17232 	}
17233 
17234 	mutex_exit(&ill->ill_lock);
17235 	ill_refrele(ill);
17236 	return (ipif);
17237 }
17238 
17239 /*
17240  * Set ill_inputfn based on the current know state.
17241  * This needs to be called when any of the factors taken into
17242  * account changes.
17243  */
17244 void
17245 ill_set_inputfn(ill_t *ill)
17246 {
17247 	ip_stack_t	*ipst = ill->ill_ipst;
17248 
17249 	if (ill->ill_isv6) {
17250 		if (is_system_labeled())
17251 			ill->ill_inputfn = ill_input_full_v6;
17252 		else
17253 			ill->ill_inputfn = ill_input_short_v6;
17254 	} else {
17255 		if (is_system_labeled())
17256 			ill->ill_inputfn = ill_input_full_v4;
17257 		else if (ill->ill_dhcpinit != 0)
17258 			ill->ill_inputfn = ill_input_full_v4;
17259 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17260 		    != NULL)
17261 			ill->ill_inputfn = ill_input_full_v4;
17262 		else if (ipst->ips_ip_cgtp_filter &&
17263 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17264 			ill->ill_inputfn = ill_input_full_v4;
17265 		else
17266 			ill->ill_inputfn = ill_input_short_v4;
17267 	}
17268 }
17269 
17270 /*
17271  * Re-evaluate ill_inputfn for all the IPv4 ills.
17272  * Used when RSVP and CGTP comes and goes.
17273  */
17274 void
17275 ill_set_inputfn_all(ip_stack_t *ipst)
17276 {
17277 	ill_walk_context_t	ctx;
17278 	ill_t			*ill;
17279 
17280 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17281 	ill = ILL_START_WALK_V4(&ctx, ipst);
17282 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17283 		ill_set_inputfn(ill);
17284 
17285 	rw_exit(&ipst->ips_ill_g_lock);
17286 }
17287 
17288 /*
17289  * Set the physical address information for `ill' to the contents of the
17290  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17291  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17292  * EINPROGRESS will be returned.
17293  */
17294 int
17295 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17296 {
17297 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17298 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17299 
17300 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17301 
17302 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17303 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17304 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17305 		/* Changing DL_IPV6_TOKEN is not yet supported */
17306 		return (0);
17307 	}
17308 
17309 	/*
17310 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17311 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17312 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17313 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17314 	 */
17315 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17316 		freemsg(mp);
17317 		return (ENOMEM);
17318 	}
17319 
17320 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17321 	mutex_enter(&ill->ill_lock);
17322 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17323 	/* no more nce addition allowed */
17324 	mutex_exit(&ill->ill_lock);
17325 
17326 	/*
17327 	 * If we can quiesce the ill, then set the address.  If not, then
17328 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17329 	 */
17330 	ill_down_ipifs(ill, B_TRUE);
17331 	mutex_enter(&ill->ill_lock);
17332 	if (!ill_is_quiescent(ill)) {
17333 		/* call cannot fail since `conn_t *' argument is NULL */
17334 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17335 		    mp, ILL_DOWN);
17336 		mutex_exit(&ill->ill_lock);
17337 		return (EINPROGRESS);
17338 	}
17339 	mutex_exit(&ill->ill_lock);
17340 
17341 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17342 	return (0);
17343 }
17344 
17345 /*
17346  * Once the ill associated with `q' has quiesced, set its physical address
17347  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17348  * are passed (linked by b_cont), since we sometimes need to save two distinct
17349  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17350  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17351  * is quiesced, we know any stale nce's with the old address information have
17352  * already been removed, so we don't need to call nce_flush().
17353  */
17354 /* ARGSUSED */
17355 static void
17356 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17357 {
17358 	ill_t		*ill = q->q_ptr;
17359 	mblk_t		*addrmp2 = unlinkb(addrmp);
17360 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17361 	uint_t		addrlen, addroff;
17362 	int		status;
17363 
17364 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17365 
17366 	addroff	= dlindp->dl_addr_offset;
17367 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17368 
17369 	switch (dlindp->dl_data) {
17370 	case DL_IPV6_LINK_LAYER_ADDR:
17371 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17372 		freemsg(addrmp2);
17373 		break;
17374 
17375 	case DL_CURR_DEST_ADDR:
17376 		freemsg(ill->ill_dest_addr_mp);
17377 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17378 		ill->ill_dest_addr_mp = addrmp;
17379 		if (ill->ill_isv6) {
17380 			ill_setdesttoken(ill);
17381 			ipif_setdestlinklocal(ill->ill_ipif);
17382 		}
17383 		freemsg(addrmp2);
17384 		break;
17385 
17386 	case DL_CURR_PHYS_ADDR:
17387 		freemsg(ill->ill_phys_addr_mp);
17388 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17389 		ill->ill_phys_addr_mp = addrmp;
17390 		ill->ill_phys_addr_length = addrlen;
17391 		if (ill->ill_isv6)
17392 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17393 		else
17394 			freemsg(addrmp2);
17395 		if (ill->ill_isv6) {
17396 			ill_setdefaulttoken(ill);
17397 			ipif_setlinklocal(ill->ill_ipif);
17398 		}
17399 		break;
17400 	default:
17401 		ASSERT(0);
17402 	}
17403 
17404 	/*
17405 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17406 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17407 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17408 	 * brought up.
17409 	 */
17410 	status = ill_up_ipifs(ill, q, addrmp);
17411 	mutex_enter(&ill->ill_lock);
17412 	if (ill->ill_dl_up)
17413 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17414 	mutex_exit(&ill->ill_lock);
17415 	if (status != EINPROGRESS)
17416 		ipsq_current_finish(ipsq);
17417 }
17418 
17419 /*
17420  * Helper routine for setting the ill_nd_lla fields.
17421  */
17422 void
17423 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17424 {
17425 	freemsg(ill->ill_nd_lla_mp);
17426 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17427 	ill->ill_nd_lla_mp = ndmp;
17428 	ill->ill_nd_lla_len = addrlen;
17429 }
17430 
17431 /*
17432  * Replumb the ill.
17433  */
17434 int
17435 ill_replumb(ill_t *ill, mblk_t *mp)
17436 {
17437 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17438 
17439 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17440 
17441 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17442 
17443 	mutex_enter(&ill->ill_lock);
17444 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17445 	/* no more nce addition allowed */
17446 	mutex_exit(&ill->ill_lock);
17447 
17448 	/*
17449 	 * If we can quiesce the ill, then continue.  If not, then
17450 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17451 	 */
17452 	ill_down_ipifs(ill, B_FALSE);
17453 
17454 	mutex_enter(&ill->ill_lock);
17455 	if (!ill_is_quiescent(ill)) {
17456 		/* call cannot fail since `conn_t *' argument is NULL */
17457 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17458 		    mp, ILL_DOWN);
17459 		mutex_exit(&ill->ill_lock);
17460 		return (EINPROGRESS);
17461 	}
17462 	mutex_exit(&ill->ill_lock);
17463 
17464 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17465 	return (0);
17466 }
17467 
17468 /* ARGSUSED */
17469 static void
17470 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17471 {
17472 	ill_t *ill = q->q_ptr;
17473 	int err;
17474 	conn_t *connp = NULL;
17475 
17476 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17477 	freemsg(ill->ill_replumb_mp);
17478 	ill->ill_replumb_mp = copyb(mp);
17479 
17480 	if (ill->ill_replumb_mp == NULL) {
17481 		/* out of memory */
17482 		ipsq_current_finish(ipsq);
17483 		return;
17484 	}
17485 
17486 	mutex_enter(&ill->ill_lock);
17487 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17488 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17489 	mutex_exit(&ill->ill_lock);
17490 
17491 	if (!ill->ill_up_ipifs) {
17492 		/* already closing */
17493 		ipsq_current_finish(ipsq);
17494 		return;
17495 	}
17496 	ill->ill_replumbing = 1;
17497 	err = ill_down_ipifs_tail(ill);
17498 
17499 	/*
17500 	 * Successfully quiesced and brought down the interface, now we send
17501 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17502 	 * DL_NOTE_REPLUMB message.
17503 	 */
17504 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17505 	    DL_NOTIFY_CONF);
17506 	ASSERT(mp != NULL);
17507 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17508 	    DL_NOTE_REPLUMB_DONE;
17509 	ill_dlpi_send(ill, mp);
17510 
17511 	/*
17512 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17513 	 * streams have to be unbound. When all the DLPI exchanges are done,
17514 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17515 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17516 	 * arp_bringup_done().
17517 	 */
17518 	ASSERT(ill->ill_replumb_mp != NULL);
17519 	if (err == EINPROGRESS)
17520 		return;
17521 	else
17522 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17523 	ASSERT(connp == NULL);
17524 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17525 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17526 		return;
17527 	}
17528 	ipsq_current_finish(ipsq);
17529 }
17530 
17531 /*
17532  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17533  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17534  * as per the ioctl.  On failure, an errno is returned.
17535  */
17536 static int
17537 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17538 {
17539 	int rval;
17540 	struct strioctl iocb;
17541 
17542 	iocb.ic_cmd = cmd;
17543 	iocb.ic_timout = 15;
17544 	iocb.ic_len = bufsize;
17545 	iocb.ic_dp = buf;
17546 
17547 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17548 }
17549 
17550 /*
17551  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17552  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17553  */
17554 static int
17555 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17556     uint_t *bufsizep, cred_t *cr)
17557 {
17558 	int err;
17559 	struct lifnum lifn;
17560 
17561 	bzero(&lifn, sizeof (lifn));
17562 	lifn.lifn_family = af;
17563 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17564 
17565 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17566 		return (err);
17567 
17568 	/*
17569 	 * Pad the interface count to account for additional interfaces that
17570 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17571 	 */
17572 	lifn.lifn_count += 4;
17573 	bzero(lifcp, sizeof (*lifcp));
17574 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17575 	lifcp->lifc_family = af;
17576 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17577 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17578 
17579 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17580 	if (err != 0) {
17581 		kmem_free(lifcp->lifc_buf, *bufsizep);
17582 		return (err);
17583 	}
17584 
17585 	return (0);
17586 }
17587 
17588 /*
17589  * Helper for ip_interface_cleanup() that removes the loopback interface.
17590  */
17591 static void
17592 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17593 {
17594 	int err;
17595 	struct lifreq lifr;
17596 
17597 	bzero(&lifr, sizeof (lifr));
17598 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17599 
17600 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17601 	if (err != 0) {
17602 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17603 		    "error %d\n", isv6 ? "v6" : "v4", err));
17604 	}
17605 }
17606 
17607 /*
17608  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17609  * groups and that IPMP data addresses are down.  These conditions must be met
17610  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17611  */
17612 static void
17613 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17614 {
17615 	int af = isv6 ? AF_INET6 : AF_INET;
17616 	int i, nifs;
17617 	int err;
17618 	uint_t bufsize;
17619 	uint_t lifrsize = sizeof (struct lifreq);
17620 	struct lifconf lifc;
17621 	struct lifreq *lifrp;
17622 
17623 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17624 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17625 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17626 		return;
17627 	}
17628 
17629 	nifs = lifc.lifc_len / lifrsize;
17630 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17631 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17632 		if (err != 0) {
17633 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17634 			    "flags: error %d", lifrp->lifr_name, err);
17635 			continue;
17636 		}
17637 
17638 		if (lifrp->lifr_flags & IFF_IPMP) {
17639 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17640 				continue;
17641 
17642 			lifrp->lifr_flags &= ~IFF_UP;
17643 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17644 			if (err != 0) {
17645 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17646 				    "bring down (error %d); IPMP interface may "
17647 				    "not be shutdown", lifrp->lifr_name, err);
17648 			}
17649 
17650 			/*
17651 			 * Check if IFF_DUPLICATE is still set -- and if so,
17652 			 * reset the address to clear it.
17653 			 */
17654 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17655 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17656 				continue;
17657 
17658 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17659 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17660 			    lifrp, lifrsize, cr)) != 0) {
17661 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17662 				    "reset DAD (error %d); IPMP interface may "
17663 				    "not be shutdown", lifrp->lifr_name, err);
17664 			}
17665 			continue;
17666 		}
17667 
17668 		lifrp->lifr_groupname[0] = '\0';
17669 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17670 		if (err != 0) {
17671 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17672 			    "IPMP group (error %d); associated IPMP interface "
17673 			    "may not be shutdown", lifrp->lifr_name, err);
17674 			continue;
17675 		}
17676 	}
17677 
17678 	kmem_free(lifc.lifc_buf, bufsize);
17679 }
17680 
17681 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17682 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17683 
17684 /*
17685  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17686  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17687  * when the user-level processes in the zone are killed and the latter are
17688  * cleaned up by str_stack_shutdown().
17689  */
17690 void
17691 ip_interface_cleanup(ip_stack_t *ipst)
17692 {
17693 	ldi_handle_t	lh;
17694 	ldi_ident_t	li;
17695 	cred_t		*cr;
17696 	int		err;
17697 	int		i;
17698 	char		*devs[] = { UDP6DEV, UDPDEV };
17699 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17700 
17701 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17702 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17703 		    " error %d", err);
17704 		return;
17705 	}
17706 
17707 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17708 	ASSERT(cr != NULL);
17709 
17710 	/*
17711 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17712 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17713 	 * the loop.)
17714 	 */
17715 	for (i = 0; i < 2; i++) {
17716 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17717 		if (err != 0) {
17718 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17719 			    " error %d", devs[i], err);
17720 			continue;
17721 		}
17722 
17723 		ip_loopback_removeif(lh, i == 0, cr);
17724 		ip_ipmp_cleanup(lh, i == 0, cr);
17725 
17726 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17727 	}
17728 
17729 	ldi_ident_release(li);
17730 	crfree(cr);
17731 }
17732 
17733 /*
17734  * This needs to be in-sync with nic_event_t definition
17735  */
17736 static const char *
17737 ill_hook_event2str(nic_event_t event)
17738 {
17739 	switch (event) {
17740 	case NE_PLUMB:
17741 		return ("PLUMB");
17742 	case NE_UNPLUMB:
17743 		return ("UNPLUMB");
17744 	case NE_UP:
17745 		return ("UP");
17746 	case NE_DOWN:
17747 		return ("DOWN");
17748 	case NE_ADDRESS_CHANGE:
17749 		return ("ADDRESS_CHANGE");
17750 	case NE_LIF_UP:
17751 		return ("LIF_UP");
17752 	case NE_LIF_DOWN:
17753 		return ("LIF_DOWN");
17754 	case NE_IFINDEX_CHANGE:
17755 		return ("IFINDEX_CHANGE");
17756 	default:
17757 		return ("UNKNOWN");
17758 	}
17759 }
17760 
17761 void
17762 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17763     nic_event_data_t data, size_t datalen)
17764 {
17765 	ip_stack_t		*ipst = ill->ill_ipst;
17766 	hook_nic_event_int_t	*info;
17767 	const char		*str = NULL;
17768 
17769 	/* create a new nic event info */
17770 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17771 		goto fail;
17772 
17773 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17774 	info->hnei_event.hne_lif = lif;
17775 	info->hnei_event.hne_event = event;
17776 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17777 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17778 	info->hnei_event.hne_data = NULL;
17779 	info->hnei_event.hne_datalen = 0;
17780 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17781 
17782 	if (data != NULL && datalen != 0) {
17783 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17784 		if (info->hnei_event.hne_data == NULL)
17785 			goto fail;
17786 		bcopy(data, info->hnei_event.hne_data, datalen);
17787 		info->hnei_event.hne_datalen = datalen;
17788 	}
17789 
17790 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17791 	    DDI_NOSLEEP) == DDI_SUCCESS)
17792 		return;
17793 
17794 fail:
17795 	if (info != NULL) {
17796 		if (info->hnei_event.hne_data != NULL) {
17797 			kmem_free(info->hnei_event.hne_data,
17798 			    info->hnei_event.hne_datalen);
17799 		}
17800 		kmem_free(info, sizeof (hook_nic_event_t));
17801 	}
17802 	str = ill_hook_event2str(event);
17803 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17804 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17805 }
17806 
17807 static int
17808 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17809 {
17810 	int		err = 0;
17811 	const in_addr_t	*addr = NULL;
17812 	nce_t		*nce = NULL;
17813 	ill_t		*ill = ipif->ipif_ill;
17814 	ill_t		*bound_ill;
17815 	boolean_t	added_ipif = B_FALSE;
17816 	uint16_t	state;
17817 	uint16_t	flags;
17818 
17819 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17820 	    ill_t *, ill, ipif_t *, ipif);
17821 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17822 		addr = &ipif->ipif_lcl_addr;
17823 	}
17824 
17825 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17826 		if (res_act != Res_act_initial)
17827 			return (EINVAL);
17828 	}
17829 
17830 	if (addr != NULL) {
17831 		ipmp_illgrp_t	*illg = ill->ill_grp;
17832 
17833 		/* add unicast nce for the local addr */
17834 
17835 		if (IS_IPMP(ill)) {
17836 			/*
17837 			 * If we're here via ipif_up(), then the ipif
17838 			 * won't be bound yet -- add it to the group,
17839 			 * which will bind it if possible. (We would
17840 			 * add it in ipif_up(), but deleting on failure
17841 			 * there is gruesome.)  If we're here via
17842 			 * ipmp_ill_bind_ipif(), then the ipif has
17843 			 * already been added to the group and we
17844 			 * just need to use the binding.
17845 			 */
17846 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17847 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17848 				if (bound_ill == NULL) {
17849 					/*
17850 					 * We couldn't bind the ipif to an ill
17851 					 * yet, so we have nothing to publish.
17852 					 * Mark the address as ready and return.
17853 					 */
17854 					ipif->ipif_addr_ready = 1;
17855 					return (0);
17856 				}
17857 				added_ipif = B_TRUE;
17858 			}
17859 		} else {
17860 			bound_ill = ill;
17861 		}
17862 
17863 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17864 		    NCE_F_NONUD);
17865 		/*
17866 		 * If this is an initial bring-up (or the ipif was never
17867 		 * completely brought up), do DAD.  Otherwise, we're here
17868 		 * because IPMP has rebound an address to this ill: send
17869 		 * unsolicited advertisements (ARP announcements) to
17870 		 * inform others.
17871 		 */
17872 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17873 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17874 		} else {
17875 			state = ND_REACHABLE;
17876 			flags |= NCE_F_UNSOL_ADV;
17877 		}
17878 
17879 retry:
17880 		err = nce_lookup_then_add_v4(ill,
17881 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17882 		    addr, flags, state, &nce);
17883 
17884 		/*
17885 		 * note that we may encounter EEXIST if we are moving
17886 		 * the nce as a result of a rebind operation.
17887 		 */
17888 		switch (err) {
17889 		case 0:
17890 			ipif->ipif_added_nce = 1;
17891 			nce->nce_ipif_cnt++;
17892 			break;
17893 		case EEXIST:
17894 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17895 			    ill->ill_name));
17896 			if (!NCE_MYADDR(nce->nce_common)) {
17897 				/*
17898 				 * A leftover nce from before this address
17899 				 * existed
17900 				 */
17901 				ncec_delete(nce->nce_common);
17902 				nce_refrele(nce);
17903 				nce = NULL;
17904 				goto retry;
17905 			}
17906 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17907 				nce_refrele(nce);
17908 				nce = NULL;
17909 				ip1dbg(("ipif_arp_up: NCE already exists "
17910 				    "for %s:%u\n", ill->ill_name,
17911 				    ipif->ipif_id));
17912 				goto arp_up_done;
17913 			}
17914 			/*
17915 			 * Duplicate local addresses are permissible for
17916 			 * IPIF_POINTOPOINT interfaces which will get marked
17917 			 * IPIF_UNNUMBERED later in
17918 			 * ip_addr_availability_check().
17919 			 *
17920 			 * The nce_ipif_cnt field tracks the number of
17921 			 * ipifs that have nce_addr as their local address.
17922 			 */
17923 			ipif->ipif_addr_ready = 1;
17924 			ipif->ipif_added_nce = 1;
17925 			nce->nce_ipif_cnt++;
17926 			err = 0;
17927 			break;
17928 		default:
17929 			ASSERT(nce == NULL);
17930 			goto arp_up_done;
17931 		}
17932 		if (arp_no_defense) {
17933 			if ((ipif->ipif_flags & IPIF_UP) &&
17934 			    !ipif->ipif_addr_ready)
17935 				ipif_up_notify(ipif);
17936 			ipif->ipif_addr_ready = 1;
17937 		}
17938 	} else {
17939 		/* zero address. nothing to publish */
17940 		ipif->ipif_addr_ready = 1;
17941 	}
17942 	if (nce != NULL)
17943 		nce_refrele(nce);
17944 arp_up_done:
17945 	if (added_ipif && err != 0)
17946 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17947 	return (err);
17948 }
17949 
17950 int
17951 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17952 {
17953 	int 		err = 0;
17954 	ill_t 		*ill = ipif->ipif_ill;
17955 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17956 
17957 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17958 	    ill_t *, ill, ipif_t *, ipif);
17959 
17960 	/*
17961 	 * need to bring up ARP or setup mcast mapping only
17962 	 * when the first interface is coming UP.
17963 	 */
17964 	first_interface = (ill->ill_ipif_up_count == 0 &&
17965 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17966 
17967 	if (res_act == Res_act_initial && first_interface) {
17968 		/*
17969 		 * Send ATTACH + BIND
17970 		 */
17971 		err = arp_ll_up(ill);
17972 		if (err != EINPROGRESS && err != 0)
17973 			return (err);
17974 
17975 		/*
17976 		 * Add NCE for local address. Start DAD.
17977 		 * we'll wait to hear that DAD has finished
17978 		 * before using the interface.
17979 		 */
17980 		if (err == EINPROGRESS)
17981 			wait_for_dlpi = B_TRUE;
17982 	}
17983 
17984 	if (!wait_for_dlpi)
17985 		(void) ipif_arp_up_done_tail(ipif, res_act);
17986 
17987 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17988 }
17989 
17990 /*
17991  * Finish processing of "arp_up" after all the DLPI message
17992  * exchanges have completed between arp and the driver.
17993  */
17994 void
17995 arp_bringup_done(ill_t *ill, int err)
17996 {
17997 	mblk_t	*mp1;
17998 	ipif_t  *ipif;
17999 	conn_t *connp = NULL;
18000 	ipsq_t	*ipsq;
18001 	queue_t *q;
18002 
18003 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18004 
18005 	ASSERT(IAM_WRITER_ILL(ill));
18006 
18007 	ipsq = ill->ill_phyint->phyint_ipsq;
18008 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18009 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18010 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18011 	if (mp1 == NULL) /* bringup was aborted by the user */
18012 		return;
18013 
18014 	/*
18015 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18016 	 * must have an associated conn_t.  Otherwise, we're bringing this
18017 	 * interface back up as part of handling an asynchronous event (e.g.,
18018 	 * physical address change).
18019 	 */
18020 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18021 		ASSERT(connp != NULL);
18022 		q = CONNP_TO_WQ(connp);
18023 	} else {
18024 		ASSERT(connp == NULL);
18025 		q = ill->ill_rq;
18026 	}
18027 	if (err == 0) {
18028 		if (ipif->ipif_isv6) {
18029 			if ((err = ipif_up_done_v6(ipif)) != 0)
18030 				ip0dbg(("arp_bringup_done: init failed\n"));
18031 		} else {
18032 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18033 			if (err != 0 ||
18034 			    (err = ipif_up_done(ipif)) != 0) {
18035 				ip0dbg(("arp_bringup_done: "
18036 				    "init failed err %x\n", err));
18037 				(void) ipif_arp_down(ipif);
18038 			}
18039 
18040 		}
18041 	} else {
18042 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18043 	}
18044 
18045 	if ((err == 0) && (ill->ill_up_ipifs)) {
18046 		err = ill_up_ipifs(ill, q, mp1);
18047 		if (err == EINPROGRESS)
18048 			return;
18049 	}
18050 
18051 	/*
18052 	 * If we have a moved ipif to bring up, and everything has succeeded
18053 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18054 	 * down -- the admin can try to bring it up by hand if need be.
18055 	 */
18056 	if (ill->ill_move_ipif != NULL) {
18057 		ipif = ill->ill_move_ipif;
18058 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18059 		    ipif->ipif_ill->ill_name));
18060 		ill->ill_move_ipif = NULL;
18061 		if (err == 0) {
18062 			err = ipif_up(ipif, q, mp1);
18063 			if (err == EINPROGRESS)
18064 				return;
18065 		}
18066 	}
18067 
18068 	/*
18069 	 * The operation must complete without EINPROGRESS since
18070 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18071 	 * Otherwise, the operation will be stuck forever in the ipsq.
18072 	 */
18073 	ASSERT(err != EINPROGRESS);
18074 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18075 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18076 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18077 		    ill_t *, ill, ipif_t *, ipif);
18078 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18079 	} else {
18080 		ipsq_current_finish(ipsq);
18081 	}
18082 }
18083 
18084 /*
18085  * Finish processing of arp replumb after all the DLPI message
18086  * exchanges have completed between arp and the driver.
18087  */
18088 void
18089 arp_replumb_done(ill_t *ill, int err)
18090 {
18091 	mblk_t	*mp1;
18092 	ipif_t  *ipif;
18093 	conn_t *connp = NULL;
18094 	ipsq_t	*ipsq;
18095 	queue_t *q;
18096 
18097 	ASSERT(IAM_WRITER_ILL(ill));
18098 
18099 	ipsq = ill->ill_phyint->phyint_ipsq;
18100 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18101 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18102 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18103 	if (mp1 == NULL) {
18104 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18105 		    ipsq->ipsq_xop->ipx_current_ioctl));
18106 		/* bringup was aborted by the user */
18107 		return;
18108 	}
18109 	/*
18110 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18111 	 * must have an associated conn_t.  Otherwise, we're bringing this
18112 	 * interface back up as part of handling an asynchronous event (e.g.,
18113 	 * physical address change).
18114 	 */
18115 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18116 		ASSERT(connp != NULL);
18117 		q = CONNP_TO_WQ(connp);
18118 	} else {
18119 		ASSERT(connp == NULL);
18120 		q = ill->ill_rq;
18121 	}
18122 	if ((err == 0) && (ill->ill_up_ipifs)) {
18123 		err = ill_up_ipifs(ill, q, mp1);
18124 		if (err == EINPROGRESS)
18125 			return;
18126 	}
18127 	/*
18128 	 * The operation must complete without EINPROGRESS since
18129 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18130 	 * Otherwise, the operation will be stuck forever in the ipsq.
18131 	 */
18132 	ASSERT(err != EINPROGRESS);
18133 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18134 		DTRACE_PROBE4(ipif__ioctl, char *,
18135 		    "arp_replumb_done finish",
18136 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18137 		    ill_t *, ill, ipif_t *, ipif);
18138 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18139 	} else {
18140 		ipsq_current_finish(ipsq);
18141 	}
18142 }
18143 
18144 void
18145 ipif_up_notify(ipif_t *ipif)
18146 {
18147 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18148 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18149 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18150 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18151 	    NE_LIF_UP, NULL, 0);
18152 }
18153 
18154 /*
18155  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18156  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18157  * TPI end points with STREAMS modules pushed above.  This is assured by not
18158  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18159  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18160  * while unwinding from the ispq and that could be a thread from the bottom.
18161  */
18162 /* ARGSUSED */
18163 int
18164 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18165     ip_ioctl_cmd_t *ipip, void *arg)
18166 {
18167 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18168 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18169 	int ret = 0;
18170 	int i;
18171 	size_t size;
18172 	ip_stack_t *ipst;
18173 	zoneid_t zoneid;
18174 	ilb_stack_t *ilbs;
18175 
18176 	ipst = CONNQ_TO_IPST(q);
18177 	ilbs = ipst->ips_netstack->netstack_ilb;
18178 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18179 
18180 	switch (command) {
18181 	case ILB_CREATE_RULE: {
18182 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18183 
18184 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18185 			ret = EINVAL;
18186 			break;
18187 		}
18188 
18189 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18190 		break;
18191 	}
18192 	case ILB_DESTROY_RULE:
18193 	case ILB_ENABLE_RULE:
18194 	case ILB_DISABLE_RULE: {
18195 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18196 
18197 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18198 			ret = EINVAL;
18199 			break;
18200 		}
18201 
18202 		if (cmd->flags & ILB_RULE_ALLRULES) {
18203 			if (command == ILB_DESTROY_RULE) {
18204 				ilb_rule_del_all(ilbs, zoneid);
18205 				break;
18206 			} else if (command == ILB_ENABLE_RULE) {
18207 				ilb_rule_enable_all(ilbs, zoneid);
18208 				break;
18209 			} else if (command == ILB_DISABLE_RULE) {
18210 				ilb_rule_disable_all(ilbs, zoneid);
18211 				break;
18212 			}
18213 		} else {
18214 			if (command == ILB_DESTROY_RULE) {
18215 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18216 			} else if (command == ILB_ENABLE_RULE) {
18217 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18218 				    NULL);
18219 			} else if (command == ILB_DISABLE_RULE) {
18220 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18221 				    NULL);
18222 			}
18223 		}
18224 		break;
18225 	}
18226 	case ILB_NUM_RULES: {
18227 		ilb_num_rules_cmd_t *cmd;
18228 
18229 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18230 			ret = EINVAL;
18231 			break;
18232 		}
18233 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18234 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18235 		break;
18236 	}
18237 	case ILB_RULE_NAMES: {
18238 		ilb_rule_names_cmd_t *cmd;
18239 
18240 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18241 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18242 		    cmd->num_names == 0) {
18243 			ret = EINVAL;
18244 			break;
18245 		}
18246 		size = cmd->num_names * ILB_RULE_NAMESZ;
18247 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18248 		    size != cmd_mp->b_wptr) {
18249 			ret = EINVAL;
18250 			break;
18251 		}
18252 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18253 		break;
18254 	}
18255 	case ILB_NUM_SERVERS: {
18256 		ilb_num_servers_cmd_t *cmd;
18257 
18258 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18259 			ret = EINVAL;
18260 			break;
18261 		}
18262 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18263 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18264 		    &(cmd->num));
18265 		break;
18266 	}
18267 	case ILB_LIST_RULE: {
18268 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18269 
18270 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18271 			ret = EINVAL;
18272 			break;
18273 		}
18274 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18275 		break;
18276 	}
18277 	case ILB_LIST_SERVERS: {
18278 		ilb_servers_info_cmd_t *cmd;
18279 
18280 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18281 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18282 		    cmd->num_servers == 0) {
18283 			ret = EINVAL;
18284 			break;
18285 		}
18286 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18287 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18288 		    size != cmd_mp->b_wptr) {
18289 			ret = EINVAL;
18290 			break;
18291 		}
18292 
18293 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18294 		    &cmd->num_servers);
18295 		break;
18296 	}
18297 	case ILB_ADD_SERVERS: {
18298 		ilb_servers_info_cmd_t *cmd;
18299 		ilb_rule_t *rule;
18300 
18301 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18302 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18303 			ret = EINVAL;
18304 			break;
18305 		}
18306 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18307 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18308 		    size != cmd_mp->b_wptr) {
18309 			ret = EINVAL;
18310 			break;
18311 		}
18312 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18313 		if (rule == NULL) {
18314 			ASSERT(ret != 0);
18315 			break;
18316 		}
18317 		for (i = 0; i < cmd->num_servers; i++) {
18318 			ilb_server_info_t *s;
18319 
18320 			s = &cmd->servers[i];
18321 			s->err = ilb_server_add(ilbs, rule, s);
18322 		}
18323 		ILB_RULE_REFRELE(rule);
18324 		break;
18325 	}
18326 	case ILB_DEL_SERVERS:
18327 	case ILB_ENABLE_SERVERS:
18328 	case ILB_DISABLE_SERVERS: {
18329 		ilb_servers_cmd_t *cmd;
18330 		ilb_rule_t *rule;
18331 		int (*f)();
18332 
18333 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18334 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18335 			ret = EINVAL;
18336 			break;
18337 		}
18338 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18339 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18340 		    size != cmd_mp->b_wptr) {
18341 			ret = EINVAL;
18342 			break;
18343 		}
18344 
18345 		if (command == ILB_DEL_SERVERS)
18346 			f = ilb_server_del;
18347 		else if (command == ILB_ENABLE_SERVERS)
18348 			f = ilb_server_enable;
18349 		else if (command == ILB_DISABLE_SERVERS)
18350 			f = ilb_server_disable;
18351 
18352 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18353 		if (rule == NULL) {
18354 			ASSERT(ret != 0);
18355 			break;
18356 		}
18357 
18358 		for (i = 0; i < cmd->num_servers; i++) {
18359 			ilb_server_arg_t *s;
18360 
18361 			s = &cmd->servers[i];
18362 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18363 		}
18364 		ILB_RULE_REFRELE(rule);
18365 		break;
18366 	}
18367 	case ILB_LIST_NAT_TABLE: {
18368 		ilb_list_nat_cmd_t *cmd;
18369 
18370 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18371 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18372 			ret = EINVAL;
18373 			break;
18374 		}
18375 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18376 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18377 		    size != cmd_mp->b_wptr) {
18378 			ret = EINVAL;
18379 			break;
18380 		}
18381 
18382 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18383 		    &cmd->flags);
18384 		break;
18385 	}
18386 	case ILB_LIST_STICKY_TABLE: {
18387 		ilb_list_sticky_cmd_t *cmd;
18388 
18389 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18390 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18391 			ret = EINVAL;
18392 			break;
18393 		}
18394 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18395 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18396 		    size != cmd_mp->b_wptr) {
18397 			ret = EINVAL;
18398 			break;
18399 		}
18400 
18401 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18402 		    &cmd->num_sticky, &cmd->flags);
18403 		break;
18404 	}
18405 	default:
18406 		ret = EINVAL;
18407 		break;
18408 	}
18409 done:
18410 	return (ret);
18411 }
18412 
18413 /* Remove all cache entries for this logical interface */
18414 void
18415 ipif_nce_down(ipif_t *ipif)
18416 {
18417 	ill_t *ill = ipif->ipif_ill;
18418 	nce_t *nce;
18419 
18420 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18421 	    ill_t *, ill, ipif_t *, ipif);
18422 	if (ipif->ipif_added_nce) {
18423 		if (ipif->ipif_isv6)
18424 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18425 		else
18426 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18427 		if (nce != NULL) {
18428 			if (--nce->nce_ipif_cnt == 0)
18429 				ncec_delete(nce->nce_common);
18430 			ipif->ipif_added_nce = 0;
18431 			nce_refrele(nce);
18432 		} else {
18433 			/*
18434 			 * nce may already be NULL because it was already
18435 			 * flushed, e.g., due to a call to nce_flush
18436 			 */
18437 			ipif->ipif_added_nce = 0;
18438 		}
18439 	}
18440 	/*
18441 	 * Make IPMP aware of the deleted data address.
18442 	 */
18443 	if (IS_IPMP(ill))
18444 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18445 
18446 	/*
18447 	 * Remove all other nces dependent on this ill when the last ipif
18448 	 * is going away.
18449 	 */
18450 	if (ill->ill_ipif_up_count == 0) {
18451 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18452 		    (uchar_t *)ill, ill->ill_ipst);
18453 		if (IS_UNDER_IPMP(ill))
18454 			nce_flush(ill, B_TRUE);
18455 	}
18456 }
18457