xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision ccd81fdd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 1990 Mentat Inc.
24  */
25 
26 /*
27  * This file contains the interface control functions for IP.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/dlpi.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/sysmacros.h>
36 #include <sys/strsubr.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/ctype.h>
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.h>
68 #include <sys/md5.h>
69 
70 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
71 #include <inet/mi.h>
72 #include <inet/nd.h>
73 #include <inet/tunables.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
105 #include <inet/udp_impl.h> /* needed for udp_stack_t */
106 
107 /* The character which tells where the ill_name ends */
108 #define	IPIF_SEPARATOR_CHAR	':'
109 
110 /* IP ioctl function table entry */
111 typedef struct ipft_s {
112 	int	ipft_cmd;
113 	pfi_t	ipft_pfi;
114 	int	ipft_min_size;
115 	int	ipft_flags;
116 } ipft_t;
117 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
118 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
119 
120 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
121 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
122 		    char *value, caddr_t cp, cred_t *ioc_cr);
123 
124 static boolean_t ill_is_quiescent(ill_t *);
125 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
126 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
127 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
128     mblk_t *mp, boolean_t need_up);
129 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
130     mblk_t *mp, boolean_t need_up);
131 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
132     queue_t *q, mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
134     mblk_t *mp);
135 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
136     mblk_t *mp);
137 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
138     queue_t *q, mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
140     int ioccmd, struct linkblk *li);
141 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
142 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
143 static void	ipsq_flush(ill_t *ill);
144 
145 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
146     queue_t *q, mblk_t *mp, boolean_t need_up);
147 static void	ipsq_delete(ipsq_t *);
148 
149 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
150     boolean_t initialize, boolean_t insert, int *errorp);
151 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
152 static void	ipif_delete_bcast_ires(ipif_t *ipif);
153 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
154 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
155 		    boolean_t isv6);
156 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
157 static void	ipif_free(ipif_t *ipif);
158 static void	ipif_free_tail(ipif_t *ipif);
159 static void	ipif_set_default(ipif_t *ipif);
160 static int	ipif_set_values(queue_t *q, mblk_t *mp,
161     char *interf_name, uint_t *ppa);
162 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
163     queue_t *q);
164 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
165     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
166     ip_stack_t *);
167 static ipif_t	*ipif_lookup_on_name_async(char *name, size_t namelen,
168     boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
169     int *error, ip_stack_t *);
170 
171 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
172 static void	ill_delete_interface_type(ill_if_t *);
173 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
174 static void	ill_dl_down(ill_t *ill);
175 static void	ill_down(ill_t *ill);
176 static void	ill_down_ipifs(ill_t *, boolean_t);
177 static void	ill_free_mib(ill_t *ill);
178 static void	ill_glist_delete(ill_t *);
179 static void	ill_phyint_reinit(ill_t *ill);
180 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
181 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
182 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
183 
184 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
185 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
186 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
187 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
188 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
189 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
190 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
191 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
192 static ip_v4mapinfo_func_t ip_mbcast_mapping;
193 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
194 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
195 static void	phyint_free(phyint_t *);
196 
197 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
198 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
199 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
200 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
201 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
202 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
203     dl_capability_sub_t *);
204 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
205 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
206 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
207 		    dl_capability_sub_t *);
208 static void	ill_capability_dld_enable(ill_t *);
209 static void	ill_capability_ack_thr(void *);
210 static void	ill_capability_lso_enable(ill_t *);
211 
212 static ill_t	*ill_prev_usesrc(ill_t *);
213 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
214 static void	ill_disband_usesrc_group(ill_t *);
215 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
216 
217 #ifdef DEBUG
218 static	void	ill_trace_cleanup(const ill_t *);
219 static	void	ipif_trace_cleanup(const ipif_t *);
220 #endif
221 
222 static	void	ill_dlpi_clear_deferred(ill_t *ill);
223 
224 /*
225  * if we go over the memory footprint limit more than once in this msec
226  * interval, we'll start pruning aggressively.
227  */
228 int ip_min_frag_prune_time = 0;
229 
230 static ipft_t	ip_ioctl_ftbl[] = {
231 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
232 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
233 		IPFT_F_NO_REPLY },
234 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
235 	{ 0 }
236 };
237 
238 /* Simple ICMP IP Header Template */
239 static ipha_t icmp_ipha = {
240 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
241 };
242 
243 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
244 
245 static ip_m_t   ip_m_tbl[] = {
246 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
247 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
248 	    ip_nodef_v6intfid },
249 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
251 	    ip_nodef_v6intfid },
252 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
260 	    ip_nodef_v6intfid },
261 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
262 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
263 	    ip_nodef_v6intfid },
264 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
265 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
266 	    ip_ipv4_v6destintfid },
267 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
268 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
269 	    ip_ipv6_v6destintfid },
270 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
271 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
272 	    ip_nodef_v6intfid },
273 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
274 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
275 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
276 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
277 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
278 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
279 	    ip_nodef_v6intfid }
280 };
281 
282 static ill_t	ill_null;		/* Empty ILL for init. */
283 char	ipif_loopback_name[] = "lo0";
284 
285 /* These are used by all IP network modules. */
286 sin6_t	sin6_null;	/* Zero address for quick clears */
287 sin_t	sin_null;	/* Zero address for quick clears */
288 
289 /* When set search for unused ipif_seqid */
290 static ipif_t	ipif_zero;
291 
292 /*
293  * ppa arena is created after these many
294  * interfaces have been plumbed.
295  */
296 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
297 
298 /*
299  * Allocate per-interface mibs.
300  * Returns true if ok. False otherwise.
301  *  ipsq  may not yet be allocated (loopback case ).
302  */
303 static boolean_t
304 ill_allocate_mibs(ill_t *ill)
305 {
306 	/* Already allocated? */
307 	if (ill->ill_ip_mib != NULL) {
308 		if (ill->ill_isv6)
309 			ASSERT(ill->ill_icmp6_mib != NULL);
310 		return (B_TRUE);
311 	}
312 
313 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
314 	    KM_NOSLEEP);
315 	if (ill->ill_ip_mib == NULL) {
316 		return (B_FALSE);
317 	}
318 
319 	/* Setup static information */
320 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
321 	    sizeof (mib2_ipIfStatsEntry_t));
322 	if (ill->ill_isv6) {
323 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
325 		    sizeof (mib2_ipv6AddrEntry_t));
326 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
327 		    sizeof (mib2_ipv6RouteEntry_t));
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
329 		    sizeof (mib2_ipv6NetToMediaEntry_t));
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
331 		    sizeof (ipv6_member_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
333 		    sizeof (ipv6_grpsrc_t));
334 	} else {
335 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
337 		    sizeof (mib2_ipAddrEntry_t));
338 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
339 		    sizeof (mib2_ipRouteEntry_t));
340 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
341 		    sizeof (mib2_ipNetToMediaEntry_t));
342 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
343 		    sizeof (ip_member_t));
344 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
345 		    sizeof (ip_grpsrc_t));
346 
347 		/*
348 		 * For a v4 ill, we are done at this point, because per ill
349 		 * icmp mibs are only used for v6.
350 		 */
351 		return (B_TRUE);
352 	}
353 
354 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
355 	    KM_NOSLEEP);
356 	if (ill->ill_icmp6_mib == NULL) {
357 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
358 		ill->ill_ip_mib = NULL;
359 		return (B_FALSE);
360 	}
361 	/* static icmp info */
362 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
363 	    sizeof (mib2_ipv6IfIcmpEntry_t);
364 	/*
365 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
366 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
367 	 * -> ill_phyint_reinit
368 	 */
369 	return (B_TRUE);
370 }
371 
372 /*
373  * Completely vaporize a lower level tap and all associated interfaces.
374  * ill_delete is called only out of ip_close when the device control
375  * stream is being closed.
376  */
377 void
378 ill_delete(ill_t *ill)
379 {
380 	ipif_t	*ipif;
381 	ill_t	*prev_ill;
382 	ip_stack_t	*ipst = ill->ill_ipst;
383 
384 	/*
385 	 * ill_delete may be forcibly entering the ipsq. The previous
386 	 * ioctl may not have completed and may need to be aborted.
387 	 * ipsq_flush takes care of it. If we don't need to enter the
388 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
389 	 * ill_delete_tail is sufficient.
390 	 */
391 	ipsq_flush(ill);
392 
393 	/*
394 	 * Nuke all interfaces.  ipif_free will take down the interface,
395 	 * remove it from the list, and free the data structure.
396 	 * Walk down the ipif list and remove the logical interfaces
397 	 * first before removing the main ipif. We can't unplumb
398 	 * zeroth interface first in the case of IPv6 as update_conn_ill
399 	 * -> ip_ll_multireq de-references ill_ipif for checking
400 	 * POINTOPOINT.
401 	 *
402 	 * If ill_ipif was not properly initialized (i.e low on memory),
403 	 * then no interfaces to clean up. In this case just clean up the
404 	 * ill.
405 	 */
406 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
407 		ipif_free(ipif);
408 
409 	/*
410 	 * clean out all the nce_t entries that depend on this
411 	 * ill for the ill_phys_addr.
412 	 */
413 	nce_flush(ill, B_TRUE);
414 
415 	/* Clean up msgs on pending upcalls for mrouted */
416 	reset_mrt_ill(ill);
417 
418 	update_conn_ill(ill, ipst);
419 
420 	/*
421 	 * Remove multicast references added as a result of calls to
422 	 * ip_join_allmulti().
423 	 */
424 	ip_purge_allmulti(ill);
425 
426 	/*
427 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
428 	 */
429 	if (IS_UNDER_IPMP(ill))
430 		ipmp_ill_leave_illgrp(ill);
431 
432 	/*
433 	 * ill_down will arrange to blow off any IRE's dependent on this
434 	 * ILL, and shut down fragmentation reassembly.
435 	 */
436 	ill_down(ill);
437 
438 	/* Let SCTP know, so that it can remove this from its list. */
439 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
440 
441 	/*
442 	 * Walk all CONNs that can have a reference on an ire or nce for this
443 	 * ill (we actually walk all that now have stale references).
444 	 */
445 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
446 
447 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
448 	if (ill->ill_isv6)
449 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
450 
451 	/*
452 	 * If an address on this ILL is being used as a source address then
453 	 * clear out the pointers in other ILLs that point to this ILL.
454 	 */
455 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
456 	if (ill->ill_usesrc_grp_next != NULL) {
457 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
458 			ill_disband_usesrc_group(ill);
459 		} else {	/* consumer of the usesrc ILL */
460 			prev_ill = ill_prev_usesrc(ill);
461 			prev_ill->ill_usesrc_grp_next =
462 			    ill->ill_usesrc_grp_next;
463 		}
464 	}
465 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
466 }
467 
468 static void
469 ipif_non_duplicate(ipif_t *ipif)
470 {
471 	ill_t *ill = ipif->ipif_ill;
472 	mutex_enter(&ill->ill_lock);
473 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
474 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
475 		ASSERT(ill->ill_ipif_dup_count > 0);
476 		ill->ill_ipif_dup_count--;
477 	}
478 	mutex_exit(&ill->ill_lock);
479 }
480 
481 /*
482  * ill_delete_tail is called from ip_modclose after all references
483  * to the closing ill are gone. The wait is done in ip_modclose
484  */
485 void
486 ill_delete_tail(ill_t *ill)
487 {
488 	mblk_t	**mpp;
489 	ipif_t	*ipif;
490 	ip_stack_t *ipst = ill->ill_ipst;
491 
492 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
493 		ipif_non_duplicate(ipif);
494 		(void) ipif_down_tail(ipif);
495 	}
496 
497 	ASSERT(ill->ill_ipif_dup_count == 0);
498 
499 	/*
500 	 * If polling capability is enabled (which signifies direct
501 	 * upcall into IP and driver has ill saved as a handle),
502 	 * we need to make sure that unbind has completed before we
503 	 * let the ill disappear and driver no longer has any reference
504 	 * to this ill.
505 	 */
506 	mutex_enter(&ill->ill_lock);
507 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
508 		cv_wait(&ill->ill_cv, &ill->ill_lock);
509 	mutex_exit(&ill->ill_lock);
510 	ASSERT(!(ill->ill_capabilities &
511 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
512 
513 	if (ill->ill_net_type != IRE_LOOPBACK)
514 		qprocsoff(ill->ill_rq);
515 
516 	/*
517 	 * We do an ipsq_flush once again now. New messages could have
518 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
519 	 * could also have landed up if an ioctl thread had looked up
520 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
521 	 * enqueued the ioctl when we did the ipsq_flush last time.
522 	 */
523 	ipsq_flush(ill);
524 
525 	/*
526 	 * Free capabilities.
527 	 */
528 	if (ill->ill_hcksum_capab != NULL) {
529 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
530 		ill->ill_hcksum_capab = NULL;
531 	}
532 
533 	if (ill->ill_zerocopy_capab != NULL) {
534 		kmem_free(ill->ill_zerocopy_capab,
535 		    sizeof (ill_zerocopy_capab_t));
536 		ill->ill_zerocopy_capab = NULL;
537 	}
538 
539 	if (ill->ill_lso_capab != NULL) {
540 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
541 		ill->ill_lso_capab = NULL;
542 	}
543 
544 	if (ill->ill_dld_capab != NULL) {
545 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
546 		ill->ill_dld_capab = NULL;
547 	}
548 
549 	while (ill->ill_ipif != NULL)
550 		ipif_free_tail(ill->ill_ipif);
551 
552 	/*
553 	 * We have removed all references to ilm from conn and the ones joined
554 	 * within the kernel.
555 	 *
556 	 * We don't walk conns, mrts and ires because
557 	 *
558 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
559 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
560 	 *    ill references.
561 	 */
562 
563 	/*
564 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
565 	 * is safe to do because the illgrp has already been unlinked from the
566 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
567 	 */
568 	if (IS_IPMP(ill)) {
569 		ipmp_illgrp_destroy(ill->ill_grp);
570 		ill->ill_grp = NULL;
571 	}
572 
573 	/*
574 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
575 	 * could free the phyint. No more reference to the phyint after this
576 	 * point.
577 	 */
578 	(void) ill_glist_delete(ill);
579 
580 	if (ill->ill_frag_ptr != NULL) {
581 		uint_t count;
582 
583 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
584 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
585 		}
586 		mi_free(ill->ill_frag_ptr);
587 		ill->ill_frag_ptr = NULL;
588 		ill->ill_frag_hash_tbl = NULL;
589 	}
590 
591 	freemsg(ill->ill_nd_lla_mp);
592 	/* Free all retained control messages. */
593 	mpp = &ill->ill_first_mp_to_free;
594 	do {
595 		while (mpp[0]) {
596 			mblk_t  *mp;
597 			mblk_t  *mp1;
598 
599 			mp = mpp[0];
600 			mpp[0] = mp->b_next;
601 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
602 				mp1->b_next = NULL;
603 				mp1->b_prev = NULL;
604 			}
605 			freemsg(mp);
606 		}
607 	} while (mpp++ != &ill->ill_last_mp_to_free);
608 
609 	ill_free_mib(ill);
610 
611 #ifdef DEBUG
612 	ill_trace_cleanup(ill);
613 #endif
614 
615 	/* The default multicast interface might have changed */
616 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
617 
618 	/* Drop refcnt here */
619 	netstack_rele(ill->ill_ipst->ips_netstack);
620 	ill->ill_ipst = NULL;
621 }
622 
623 static void
624 ill_free_mib(ill_t *ill)
625 {
626 	ip_stack_t *ipst = ill->ill_ipst;
627 
628 	/*
629 	 * MIB statistics must not be lost, so when an interface
630 	 * goes away the counter values will be added to the global
631 	 * MIBs.
632 	 */
633 	if (ill->ill_ip_mib != NULL) {
634 		if (ill->ill_isv6) {
635 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
636 			    ill->ill_ip_mib);
637 		} else {
638 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
639 			    ill->ill_ip_mib);
640 		}
641 
642 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
643 		ill->ill_ip_mib = NULL;
644 	}
645 	if (ill->ill_icmp6_mib != NULL) {
646 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
647 		    ill->ill_icmp6_mib);
648 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
649 		ill->ill_icmp6_mib = NULL;
650 	}
651 }
652 
653 /*
654  * Concatenate together a physical address and a sap.
655  *
656  * Sap_lengths are interpreted as follows:
657  *   sap_length == 0	==>	no sap
658  *   sap_length > 0	==>	sap is at the head of the dlpi address
659  *   sap_length < 0	==>	sap is at the tail of the dlpi address
660  */
661 static void
662 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
663     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
664 {
665 	uint16_t sap_addr = (uint16_t)sap_src;
666 
667 	if (sap_length == 0) {
668 		if (phys_src == NULL)
669 			bzero(dst, phys_length);
670 		else
671 			bcopy(phys_src, dst, phys_length);
672 	} else if (sap_length < 0) {
673 		if (phys_src == NULL)
674 			bzero(dst, phys_length);
675 		else
676 			bcopy(phys_src, dst, phys_length);
677 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
678 	} else {
679 		bcopy(&sap_addr, dst, sizeof (sap_addr));
680 		if (phys_src == NULL)
681 			bzero((char *)dst + sap_length, phys_length);
682 		else
683 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
684 	}
685 }
686 
687 /*
688  * Generate a dl_unitdata_req mblk for the device and address given.
689  * addr_length is the length of the physical portion of the address.
690  * If addr is NULL include an all zero address of the specified length.
691  * TRUE? In any case, addr_length is taken to be the entire length of the
692  * dlpi address, including the absolute value of sap_length.
693  */
694 mblk_t *
695 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
696 		t_scalar_t sap_length)
697 {
698 	dl_unitdata_req_t *dlur;
699 	mblk_t	*mp;
700 	t_scalar_t	abs_sap_length;		/* absolute value */
701 
702 	abs_sap_length = ABS(sap_length);
703 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
704 	    DL_UNITDATA_REQ);
705 	if (mp == NULL)
706 		return (NULL);
707 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
708 	/* HACK: accomodate incompatible DLPI drivers */
709 	if (addr_length == 8)
710 		addr_length = 6;
711 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
712 	dlur->dl_dest_addr_offset = sizeof (*dlur);
713 	dlur->dl_priority.dl_min = 0;
714 	dlur->dl_priority.dl_max = 0;
715 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
716 	    (uchar_t *)&dlur[1]);
717 	return (mp);
718 }
719 
720 /*
721  * Add the pending mp to the list. There can be only 1 pending mp
722  * in the list. Any exclusive ioctl that needs to wait for a response
723  * from another module or driver needs to use this function to set
724  * the ipx_pending_mp to the ioctl mblk and wait for the response from
725  * the other module/driver. This is also used while waiting for the
726  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
727  */
728 boolean_t
729 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
730     int waitfor)
731 {
732 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
733 
734 	ASSERT(IAM_WRITER_IPIF(ipif));
735 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
736 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
737 	ASSERT(ipx->ipx_pending_mp == NULL);
738 	/*
739 	 * The caller may be using a different ipif than the one passed into
740 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
741 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
742 	 * that `ipx_current_ipif == ipif'.
743 	 */
744 	ASSERT(ipx->ipx_current_ipif != NULL);
745 
746 	/*
747 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
748 	 * driver.
749 	 */
750 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
751 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
752 	    (DB_TYPE(add_mp) == M_PCPROTO));
753 
754 	if (connp != NULL) {
755 		ASSERT(MUTEX_HELD(&connp->conn_lock));
756 		/*
757 		 * Return error if the conn has started closing. The conn
758 		 * could have finished cleaning up the pending mp list,
759 		 * If so we should not add another mp to the list negating
760 		 * the cleanup.
761 		 */
762 		if (connp->conn_state_flags & CONN_CLOSING)
763 			return (B_FALSE);
764 	}
765 	mutex_enter(&ipx->ipx_lock);
766 	ipx->ipx_pending_ipif = ipif;
767 	/*
768 	 * Note down the queue in b_queue. This will be returned by
769 	 * ipsq_pending_mp_get. Caller will then use these values to restart
770 	 * the processing
771 	 */
772 	add_mp->b_next = NULL;
773 	add_mp->b_queue = q;
774 	ipx->ipx_pending_mp = add_mp;
775 	ipx->ipx_waitfor = waitfor;
776 	mutex_exit(&ipx->ipx_lock);
777 
778 	if (connp != NULL)
779 		connp->conn_oper_pending_ill = ipif->ipif_ill;
780 
781 	return (B_TRUE);
782 }
783 
784 /*
785  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
786  * queued in the list.
787  */
788 mblk_t *
789 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
790 {
791 	mblk_t	*curr = NULL;
792 	ipxop_t	*ipx = ipsq->ipsq_xop;
793 
794 	*connpp = NULL;
795 	mutex_enter(&ipx->ipx_lock);
796 	if (ipx->ipx_pending_mp == NULL) {
797 		mutex_exit(&ipx->ipx_lock);
798 		return (NULL);
799 	}
800 
801 	/* There can be only 1 such excl message */
802 	curr = ipx->ipx_pending_mp;
803 	ASSERT(curr->b_next == NULL);
804 	ipx->ipx_pending_ipif = NULL;
805 	ipx->ipx_pending_mp = NULL;
806 	ipx->ipx_waitfor = 0;
807 	mutex_exit(&ipx->ipx_lock);
808 
809 	if (CONN_Q(curr->b_queue)) {
810 		/*
811 		 * This mp did a refhold on the conn, at the start of the ioctl.
812 		 * So we can safely return a pointer to the conn to the caller.
813 		 */
814 		*connpp = Q_TO_CONN(curr->b_queue);
815 	} else {
816 		*connpp = NULL;
817 	}
818 	curr->b_next = NULL;
819 	curr->b_prev = NULL;
820 	return (curr);
821 }
822 
823 /*
824  * Cleanup the ioctl mp queued in ipx_pending_mp
825  * - Called in the ill_delete path
826  * - Called in the M_ERROR or M_HANGUP path on the ill.
827  * - Called in the conn close path.
828  *
829  * Returns success on finding the pending mblk associated with the ioctl or
830  * exclusive operation in progress, failure otherwise.
831  */
832 boolean_t
833 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
834 {
835 	mblk_t	*mp;
836 	ipxop_t	*ipx;
837 	queue_t	*q;
838 	ipif_t	*ipif;
839 	int	cmd;
840 
841 	ASSERT(IAM_WRITER_ILL(ill));
842 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
843 
844 	mutex_enter(&ipx->ipx_lock);
845 	mp = ipx->ipx_pending_mp;
846 	if (connp != NULL) {
847 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
848 			/*
849 			 * Nothing to clean since the conn that is closing
850 			 * does not have a matching pending mblk in
851 			 * ipx_pending_mp.
852 			 */
853 			mutex_exit(&ipx->ipx_lock);
854 			return (B_FALSE);
855 		}
856 	} else {
857 		/*
858 		 * A non-zero ill_error signifies we are called in the
859 		 * M_ERROR or M_HANGUP path and we need to unconditionally
860 		 * abort any current ioctl and do the corresponding cleanup.
861 		 * A zero ill_error means we are in the ill_delete path and
862 		 * we do the cleanup only if there is a pending mp.
863 		 */
864 		if (mp == NULL && ill->ill_error == 0) {
865 			mutex_exit(&ipx->ipx_lock);
866 			return (B_FALSE);
867 		}
868 	}
869 
870 	/* Now remove from the ipx_pending_mp */
871 	ipx->ipx_pending_mp = NULL;
872 	ipif = ipx->ipx_pending_ipif;
873 	ipx->ipx_pending_ipif = NULL;
874 	ipx->ipx_waitfor = 0;
875 	ipx->ipx_current_ipif = NULL;
876 	cmd = ipx->ipx_current_ioctl;
877 	ipx->ipx_current_ioctl = 0;
878 	ipx->ipx_current_done = B_TRUE;
879 	mutex_exit(&ipx->ipx_lock);
880 
881 	if (mp == NULL)
882 		return (B_FALSE);
883 
884 	q = mp->b_queue;
885 	mp->b_next = NULL;
886 	mp->b_prev = NULL;
887 	mp->b_queue = NULL;
888 
889 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
890 		DTRACE_PROBE4(ipif__ioctl,
891 		    char *, "ipsq_pending_mp_cleanup",
892 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
893 		    ipif_t *, ipif);
894 		if (connp == NULL) {
895 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
896 		} else {
897 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
898 			mutex_enter(&ipif->ipif_ill->ill_lock);
899 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
900 			mutex_exit(&ipif->ipif_ill->ill_lock);
901 		}
902 	} else {
903 		inet_freemsg(mp);
904 	}
905 	return (B_TRUE);
906 }
907 
908 /*
909  * Called in the conn close path and ill delete path
910  */
911 static void
912 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
913 {
914 	ipsq_t	*ipsq;
915 	mblk_t	*prev;
916 	mblk_t	*curr;
917 	mblk_t	*next;
918 	queue_t	*wq, *rq = NULL;
919 	mblk_t	*tmp_list = NULL;
920 
921 	ASSERT(IAM_WRITER_ILL(ill));
922 	if (connp != NULL)
923 		wq = CONNP_TO_WQ(connp);
924 	else
925 		wq = ill->ill_wq;
926 
927 	/*
928 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
929 	 * against this here.
930 	 */
931 	if (wq != NULL)
932 		rq = RD(wq);
933 
934 	ipsq = ill->ill_phyint->phyint_ipsq;
935 	/*
936 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
937 	 * In the case of ioctl from a conn, there can be only 1 mp
938 	 * queued on the ipsq. If an ill is being unplumbed flush all
939 	 * the messages.
940 	 */
941 	mutex_enter(&ipsq->ipsq_lock);
942 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
943 	    curr = next) {
944 		next = curr->b_next;
945 		if (connp == NULL ||
946 		    (curr->b_queue == wq || curr->b_queue == rq)) {
947 			/* Unlink the mblk from the pending mp list */
948 			if (prev != NULL) {
949 				prev->b_next = curr->b_next;
950 			} else {
951 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
952 				ipsq->ipsq_xopq_mphead = curr->b_next;
953 			}
954 			if (ipsq->ipsq_xopq_mptail == curr)
955 				ipsq->ipsq_xopq_mptail = prev;
956 			/*
957 			 * Create a temporary list and release the ipsq lock
958 			 * New elements are added to the head of the tmp_list
959 			 */
960 			curr->b_next = tmp_list;
961 			tmp_list = curr;
962 		} else {
963 			prev = curr;
964 		}
965 	}
966 	mutex_exit(&ipsq->ipsq_lock);
967 
968 	while (tmp_list != NULL) {
969 		curr = tmp_list;
970 		tmp_list = curr->b_next;
971 		curr->b_next = NULL;
972 		curr->b_prev = NULL;
973 		wq = curr->b_queue;
974 		curr->b_queue = NULL;
975 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
976 			DTRACE_PROBE4(ipif__ioctl,
977 			    char *, "ipsq_xopq_mp_cleanup",
978 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
979 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
980 			    CONN_CLOSE : NO_COPYOUT, NULL);
981 		} else {
982 			/*
983 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
984 			 * this can't be just inet_freemsg. we have to
985 			 * restart it otherwise the thread will be stuck.
986 			 */
987 			inet_freemsg(curr);
988 		}
989 	}
990 }
991 
992 /*
993  * This conn has started closing. Cleanup any pending ioctl from this conn.
994  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
995  */
996 void
997 conn_ioctl_cleanup(conn_t *connp)
998 {
999 	ipsq_t	*ipsq;
1000 	ill_t	*ill;
1001 	boolean_t refheld;
1002 
1003 	/*
1004 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1005 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1006 	 * started the mp could be present in ipx_pending_mp. Note that if
1007 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1008 	 * not yet queued anywhere. In this case, the conn close code will wait
1009 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1010 	 * tcp_close will wait first until all ioctls have completed for this
1011 	 * conn.
1012 	 */
1013 	mutex_enter(&connp->conn_lock);
1014 	ill = connp->conn_oper_pending_ill;
1015 	if (ill == NULL) {
1016 		mutex_exit(&connp->conn_lock);
1017 		return;
1018 	}
1019 
1020 	/*
1021 	 * We may not be able to refhold the ill if the ill/ipif
1022 	 * is changing. But we need to make sure that the ill will
1023 	 * not vanish. So we just bump up the ill_waiter count.
1024 	 */
1025 	refheld = ill_waiter_inc(ill);
1026 	mutex_exit(&connp->conn_lock);
1027 	if (refheld) {
1028 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1029 			ill_waiter_dcr(ill);
1030 			/*
1031 			 * Check whether this ioctl has started and is
1032 			 * pending. If it is not found there then check
1033 			 * whether this ioctl has not even started and is in
1034 			 * the ipsq_xopq list.
1035 			 */
1036 			if (!ipsq_pending_mp_cleanup(ill, connp))
1037 				ipsq_xopq_mp_cleanup(ill, connp);
1038 			ipsq = ill->ill_phyint->phyint_ipsq;
1039 			ipsq_exit(ipsq);
1040 			return;
1041 		}
1042 	}
1043 
1044 	/*
1045 	 * The ill is also closing and we could not bump up the
1046 	 * ill_waiter_count or we could not enter the ipsq. Leave
1047 	 * the cleanup to ill_delete
1048 	 */
1049 	mutex_enter(&connp->conn_lock);
1050 	while (connp->conn_oper_pending_ill != NULL)
1051 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1052 	mutex_exit(&connp->conn_lock);
1053 	if (refheld)
1054 		ill_waiter_dcr(ill);
1055 }
1056 
1057 /*
1058  * ipcl_walk function for cleaning up conn_*_ill fields.
1059  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1060  * conn_bound_if in place. We prefer dropping
1061  * packets instead of sending them out the wrong interface, or accepting
1062  * packets from the wrong ifindex.
1063  */
1064 static void
1065 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1066 {
1067 	ill_t	*ill = (ill_t *)arg;
1068 
1069 	mutex_enter(&connp->conn_lock);
1070 	if (connp->conn_dhcpinit_ill == ill) {
1071 		connp->conn_dhcpinit_ill = NULL;
1072 		ASSERT(ill->ill_dhcpinit != 0);
1073 		atomic_dec_32(&ill->ill_dhcpinit);
1074 		ill_set_inputfn(ill);
1075 	}
1076 	mutex_exit(&connp->conn_lock);
1077 }
1078 
1079 static int
1080 ill_down_ipifs_tail(ill_t *ill)
1081 {
1082 	ipif_t	*ipif;
1083 	int err;
1084 
1085 	ASSERT(IAM_WRITER_ILL(ill));
1086 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1087 		ipif_non_duplicate(ipif);
1088 		/*
1089 		 * ipif_down_tail will call arp_ll_down on the last ipif
1090 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1091 		 */
1092 		if ((err = ipif_down_tail(ipif)) != 0)
1093 			return (err);
1094 	}
1095 	return (0);
1096 }
1097 
1098 /* ARGSUSED */
1099 void
1100 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1101 {
1102 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1103 	(void) ill_down_ipifs_tail(q->q_ptr);
1104 	freemsg(mp);
1105 	ipsq_current_finish(ipsq);
1106 }
1107 
1108 /*
1109  * ill_down_start is called when we want to down this ill and bring it up again
1110  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1111  * all interfaces, but don't tear down any plumbing.
1112  */
1113 boolean_t
1114 ill_down_start(queue_t *q, mblk_t *mp)
1115 {
1116 	ill_t	*ill = q->q_ptr;
1117 	ipif_t	*ipif;
1118 
1119 	ASSERT(IAM_WRITER_ILL(ill));
1120 	/*
1121 	 * It is possible that some ioctl is already in progress while we
1122 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1123 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1124 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1125 	 * the in progress ioctl from ever completing.
1126 	 *
1127 	 * The thread that started the ioctl (if any) must have returned,
1128 	 * since we are now executing as writer. After the 2 calls below,
1129 	 * the state of the ipsq and the ill would reflect no trace of any
1130 	 * pending operation. Subsequently if there is any response to the
1131 	 * original ioctl from the driver, it would be discarded as an
1132 	 * unsolicited message from the driver.
1133 	 */
1134 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1135 	ill_dlpi_clear_deferred(ill);
1136 
1137 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1138 		(void) ipif_down(ipif, NULL, NULL);
1139 
1140 	ill_down(ill);
1141 
1142 	/*
1143 	 * Walk all CONNs that can have a reference on an ire or nce for this
1144 	 * ill (we actually walk all that now have stale references).
1145 	 */
1146 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1147 
1148 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1149 	if (ill->ill_isv6)
1150 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1151 
1152 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1153 
1154 	/*
1155 	 * Atomically test and add the pending mp if references are active.
1156 	 */
1157 	mutex_enter(&ill->ill_lock);
1158 	if (!ill_is_quiescent(ill)) {
1159 		/* call cannot fail since `conn_t *' argument is NULL */
1160 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1161 		    mp, ILL_DOWN);
1162 		mutex_exit(&ill->ill_lock);
1163 		return (B_FALSE);
1164 	}
1165 	mutex_exit(&ill->ill_lock);
1166 	return (B_TRUE);
1167 }
1168 
1169 static void
1170 ill_down(ill_t *ill)
1171 {
1172 	mblk_t	*mp;
1173 	ip_stack_t	*ipst = ill->ill_ipst;
1174 
1175 	/*
1176 	 * Blow off any IREs dependent on this ILL.
1177 	 * The caller needs to handle conn_ixa_cleanup
1178 	 */
1179 	ill_delete_ires(ill);
1180 
1181 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1182 
1183 	/* Remove any conn_*_ill depending on this ill */
1184 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1185 
1186 	/*
1187 	 * Free state for additional IREs.
1188 	 */
1189 	mutex_enter(&ill->ill_saved_ire_lock);
1190 	mp = ill->ill_saved_ire_mp;
1191 	ill->ill_saved_ire_mp = NULL;
1192 	ill->ill_saved_ire_cnt = 0;
1193 	mutex_exit(&ill->ill_saved_ire_lock);
1194 	freemsg(mp);
1195 }
1196 
1197 /*
1198  * ire_walk routine used to delete every IRE that depends on
1199  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1200  *
1201  * Note: since the routes added by the kernel are deleted separately,
1202  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1203  *
1204  * We also remove references on ire_nce_cache entries that refer to the ill.
1205  */
1206 void
1207 ill_downi(ire_t *ire, char *ill_arg)
1208 {
1209 	ill_t	*ill = (ill_t *)ill_arg;
1210 	nce_t	*nce;
1211 
1212 	mutex_enter(&ire->ire_lock);
1213 	nce = ire->ire_nce_cache;
1214 	if (nce != NULL && nce->nce_ill == ill)
1215 		ire->ire_nce_cache = NULL;
1216 	else
1217 		nce = NULL;
1218 	mutex_exit(&ire->ire_lock);
1219 	if (nce != NULL)
1220 		nce_refrele(nce);
1221 	if (ire->ire_ill == ill) {
1222 		/*
1223 		 * The existing interface binding for ire must be
1224 		 * deleted before trying to bind the route to another
1225 		 * interface. However, since we are using the contents of the
1226 		 * ire after ire_delete, the caller has to ensure that
1227 		 * CONDEMNED (deleted) ire's are not removed from the list
1228 		 * when ire_delete() returns. Currently ill_downi() is
1229 		 * only called as part of ire_walk*() routines, so that
1230 		 * the irb_refhold() done by ire_walk*() will ensure that
1231 		 * ire_delete() does not lead to ire_inactive().
1232 		 */
1233 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1234 		ire_delete(ire);
1235 		if (ire->ire_unbound)
1236 			ire_rebind(ire);
1237 	}
1238 }
1239 
1240 /* Remove IRE_IF_CLONE on this ill */
1241 void
1242 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1243 {
1244 	ill_t	*ill = (ill_t *)ill_arg;
1245 
1246 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1247 	if (ire->ire_ill == ill)
1248 		ire_delete(ire);
1249 }
1250 
1251 /* Consume an M_IOCACK of the fastpath probe. */
1252 void
1253 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1254 {
1255 	mblk_t	*mp1 = mp;
1256 
1257 	/*
1258 	 * If this was the first attempt turn on the fastpath probing.
1259 	 */
1260 	mutex_enter(&ill->ill_lock);
1261 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1262 		ill->ill_dlpi_fastpath_state = IDS_OK;
1263 	mutex_exit(&ill->ill_lock);
1264 
1265 	/* Free the M_IOCACK mblk, hold on to the data */
1266 	mp = mp->b_cont;
1267 	freeb(mp1);
1268 	if (mp == NULL)
1269 		return;
1270 	if (mp->b_cont != NULL)
1271 		nce_fastpath_update(ill, mp);
1272 	else
1273 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1274 	freemsg(mp);
1275 }
1276 
1277 /*
1278  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1279  * The data portion of the request is a dl_unitdata_req_t template for
1280  * what we would send downstream in the absence of a fastpath confirmation.
1281  */
1282 int
1283 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1284 {
1285 	struct iocblk	*ioc;
1286 	mblk_t	*mp;
1287 
1288 	if (dlur_mp == NULL)
1289 		return (EINVAL);
1290 
1291 	mutex_enter(&ill->ill_lock);
1292 	switch (ill->ill_dlpi_fastpath_state) {
1293 	case IDS_FAILED:
1294 		/*
1295 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1296 		 * support it.
1297 		 */
1298 		mutex_exit(&ill->ill_lock);
1299 		return (ENOTSUP);
1300 	case IDS_UNKNOWN:
1301 		/* This is the first probe */
1302 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1303 		break;
1304 	default:
1305 		break;
1306 	}
1307 	mutex_exit(&ill->ill_lock);
1308 
1309 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1310 		return (EAGAIN);
1311 
1312 	mp->b_cont = copyb(dlur_mp);
1313 	if (mp->b_cont == NULL) {
1314 		freeb(mp);
1315 		return (EAGAIN);
1316 	}
1317 
1318 	ioc = (struct iocblk *)mp->b_rptr;
1319 	ioc->ioc_count = msgdsize(mp->b_cont);
1320 
1321 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1322 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1323 	putnext(ill->ill_wq, mp);
1324 	return (0);
1325 }
1326 
1327 void
1328 ill_capability_probe(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 
1332 	ASSERT(IAM_WRITER_ILL(ill));
1333 
1334 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1335 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1336 		return;
1337 
1338 	/*
1339 	 * We are starting a new cycle of capability negotiation.
1340 	 * Free up the capab reset messages of any previous incarnation.
1341 	 * We will do a fresh allocation when we get the response to our probe
1342 	 */
1343 	if (ill->ill_capab_reset_mp != NULL) {
1344 		freemsg(ill->ill_capab_reset_mp);
1345 		ill->ill_capab_reset_mp = NULL;
1346 	}
1347 
1348 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1349 
1350 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1351 	if (mp == NULL)
1352 		return;
1353 
1354 	ill_capability_send(ill, mp);
1355 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1356 }
1357 
1358 void
1359 ill_capability_reset(ill_t *ill, boolean_t reneg)
1360 {
1361 	ASSERT(IAM_WRITER_ILL(ill));
1362 
1363 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1364 		return;
1365 
1366 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1367 
1368 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1369 	ill->ill_capab_reset_mp = NULL;
1370 	/*
1371 	 * We turn off all capabilities except those pertaining to
1372 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1373 	 * which will be turned off by the corresponding reset functions.
1374 	 */
1375 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1376 }
1377 
1378 static void
1379 ill_capability_reset_alloc(ill_t *ill)
1380 {
1381 	mblk_t *mp;
1382 	size_t	size = 0;
1383 	int	err;
1384 	dl_capability_req_t	*capb;
1385 
1386 	ASSERT(IAM_WRITER_ILL(ill));
1387 	ASSERT(ill->ill_capab_reset_mp == NULL);
1388 
1389 	if (ILL_HCKSUM_CAPABLE(ill)) {
1390 		size += sizeof (dl_capability_sub_t) +
1391 		    sizeof (dl_capab_hcksum_t);
1392 	}
1393 
1394 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1395 		size += sizeof (dl_capability_sub_t) +
1396 		    sizeof (dl_capab_zerocopy_t);
1397 	}
1398 
1399 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1400 		size += sizeof (dl_capability_sub_t) +
1401 		    sizeof (dl_capab_dld_t);
1402 	}
1403 
1404 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1405 	    STR_NOSIG, &err);
1406 
1407 	mp->b_datap->db_type = M_PROTO;
1408 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1409 
1410 	capb = (dl_capability_req_t *)mp->b_rptr;
1411 	capb->dl_primitive = DL_CAPABILITY_REQ;
1412 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1413 	capb->dl_sub_length = size;
1414 
1415 	mp->b_wptr += sizeof (dl_capability_req_t);
1416 
1417 	/*
1418 	 * Each handler fills in the corresponding dl_capability_sub_t
1419 	 * inside the mblk,
1420 	 */
1421 	ill_capability_hcksum_reset_fill(ill, mp);
1422 	ill_capability_zerocopy_reset_fill(ill, mp);
1423 	ill_capability_dld_reset_fill(ill, mp);
1424 
1425 	ill->ill_capab_reset_mp = mp;
1426 }
1427 
1428 static void
1429 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1430 {
1431 	dl_capab_id_t *id_ic;
1432 	uint_t sub_dl_cap = outers->dl_cap;
1433 	dl_capability_sub_t *inners;
1434 	uint8_t *capend;
1435 
1436 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1437 
1438 	/*
1439 	 * Note: range checks here are not absolutely sufficient to
1440 	 * make us robust against malformed messages sent by drivers;
1441 	 * this is in keeping with the rest of IP's dlpi handling.
1442 	 * (Remember, it's coming from something else in the kernel
1443 	 * address space)
1444 	 */
1445 
1446 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1447 	if (capend > mp->b_wptr) {
1448 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1449 		    "malformed sub-capability too long for mblk");
1450 		return;
1451 	}
1452 
1453 	id_ic = (dl_capab_id_t *)(outers + 1);
1454 
1455 	if (outers->dl_length < sizeof (*id_ic) ||
1456 	    (inners = &id_ic->id_subcap,
1457 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1458 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1459 		    "encapsulated capab type %d too long for mblk",
1460 		    inners->dl_cap);
1461 		return;
1462 	}
1463 
1464 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1465 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1466 		    "isn't as expected; pass-thru module(s) detected, "
1467 		    "discarding capability\n", inners->dl_cap));
1468 		return;
1469 	}
1470 
1471 	/* Process the encapsulated sub-capability */
1472 	ill_capability_dispatch(ill, mp, inners);
1473 }
1474 
1475 static void
1476 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1477 {
1478 	dl_capability_sub_t *dl_subcap;
1479 
1480 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1481 		return;
1482 
1483 	/*
1484 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1485 	 * initialized below since it is not used by DLD.
1486 	 */
1487 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1488 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1489 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1490 
1491 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1492 }
1493 
1494 static void
1495 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1496 {
1497 	/*
1498 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1499 	 * is only to get the VRRP capability.
1500 	 *
1501 	 * Note that we cannot check ill_ipif_up_count here since
1502 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1503 	 * That is done asynchronously, and can race with this function.
1504 	 */
1505 	if (!ill->ill_dl_up) {
1506 		if (subp->dl_cap == DL_CAPAB_VRRP)
1507 			ill_capability_vrrp_ack(ill, mp, subp);
1508 		return;
1509 	}
1510 
1511 	switch (subp->dl_cap) {
1512 	case DL_CAPAB_HCKSUM:
1513 		ill_capability_hcksum_ack(ill, mp, subp);
1514 		break;
1515 	case DL_CAPAB_ZEROCOPY:
1516 		ill_capability_zerocopy_ack(ill, mp, subp);
1517 		break;
1518 	case DL_CAPAB_DLD:
1519 		ill_capability_dld_ack(ill, mp, subp);
1520 		break;
1521 	case DL_CAPAB_VRRP:
1522 		break;
1523 	default:
1524 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1525 		    subp->dl_cap));
1526 	}
1527 }
1528 
1529 /*
1530  * Process the vrrp capability received from a DLS Provider. isub must point
1531  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1532  */
1533 static void
1534 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1535 {
1536 	dl_capab_vrrp_t	*vrrp;
1537 	uint_t		sub_dl_cap = isub->dl_cap;
1538 	uint8_t		*capend;
1539 
1540 	ASSERT(IAM_WRITER_ILL(ill));
1541 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1542 
1543 	/*
1544 	 * Note: range checks here are not absolutely sufficient to
1545 	 * make us robust against malformed messages sent by drivers;
1546 	 * this is in keeping with the rest of IP's dlpi handling.
1547 	 * (Remember, it's coming from something else in the kernel
1548 	 * address space)
1549 	 */
1550 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1551 	if (capend > mp->b_wptr) {
1552 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1553 		    "malformed sub-capability too long for mblk");
1554 		return;
1555 	}
1556 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1557 
1558 	/*
1559 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1560 	 */
1561 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1562 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1563 		ill->ill_flags |= ILLF_VRRP;
1564 	}
1565 }
1566 
1567 /*
1568  * Process a hardware checksum offload capability negotiation ack received
1569  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1570  * of a DL_CAPABILITY_ACK message.
1571  */
1572 static void
1573 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1574 {
1575 	dl_capability_req_t	*ocap;
1576 	dl_capab_hcksum_t	*ihck, *ohck;
1577 	ill_hcksum_capab_t	**ill_hcksum;
1578 	mblk_t			*nmp = NULL;
1579 	uint_t			sub_dl_cap = isub->dl_cap;
1580 	uint8_t			*capend;
1581 
1582 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1583 
1584 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1585 
1586 	/*
1587 	 * Note: range checks here are not absolutely sufficient to
1588 	 * make us robust against malformed messages sent by drivers;
1589 	 * this is in keeping with the rest of IP's dlpi handling.
1590 	 * (Remember, it's coming from something else in the kernel
1591 	 * address space)
1592 	 */
1593 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1594 	if (capend > mp->b_wptr) {
1595 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1596 		    "malformed sub-capability too long for mblk");
1597 		return;
1598 	}
1599 
1600 	/*
1601 	 * There are two types of acks we process here:
1602 	 * 1. acks in reply to a (first form) generic capability req
1603 	 *    (no ENABLE flag set)
1604 	 * 2. acks in reply to a ENABLE capability req.
1605 	 *    (ENABLE flag set)
1606 	 */
1607 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1608 
1609 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1610 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1611 		    "unsupported hardware checksum "
1612 		    "sub-capability (version %d, expected %d)",
1613 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1614 		return;
1615 	}
1616 
1617 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1618 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1619 		    "checksum capability isn't as expected; pass-thru "
1620 		    "module(s) detected, discarding capability\n"));
1621 		return;
1622 	}
1623 
1624 #define	CURR_HCKSUM_CAPAB				\
1625 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1626 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1627 
1628 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1629 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1630 		/* do ENABLE processing */
1631 		if (*ill_hcksum == NULL) {
1632 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1633 			    KM_NOSLEEP);
1634 
1635 			if (*ill_hcksum == NULL) {
1636 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1637 				    "could not enable hcksum version %d "
1638 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1639 				    ill->ill_name);
1640 				return;
1641 			}
1642 		}
1643 
1644 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1645 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1646 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1647 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1648 		    "has enabled hardware checksumming\n ",
1649 		    ill->ill_name));
1650 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1651 		/*
1652 		 * Enabling hardware checksum offload
1653 		 * Currently IP supports {TCP,UDP}/IPv4
1654 		 * partial and full cksum offload and
1655 		 * IPv4 header checksum offload.
1656 		 * Allocate new mblk which will
1657 		 * contain a new capability request
1658 		 * to enable hardware checksum offload.
1659 		 */
1660 		uint_t	size;
1661 		uchar_t	*rptr;
1662 
1663 		size = sizeof (dl_capability_req_t) +
1664 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1665 
1666 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1667 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1668 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1669 			    ill->ill_name);
1670 			return;
1671 		}
1672 
1673 		rptr = nmp->b_rptr;
1674 		/* initialize dl_capability_req_t */
1675 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1676 		ocap->dl_sub_offset =
1677 		    sizeof (dl_capability_req_t);
1678 		ocap->dl_sub_length =
1679 		    sizeof (dl_capability_sub_t) +
1680 		    isub->dl_length;
1681 		nmp->b_rptr += sizeof (dl_capability_req_t);
1682 
1683 		/* initialize dl_capability_sub_t */
1684 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1685 		nmp->b_rptr += sizeof (*isub);
1686 
1687 		/* initialize dl_capab_hcksum_t */
1688 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1689 		bcopy(ihck, ohck, sizeof (*ihck));
1690 
1691 		nmp->b_rptr = rptr;
1692 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1693 
1694 		/* Set ENABLE flag */
1695 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1696 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1697 
1698 		/*
1699 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1700 		 * hardware checksum acceleration.
1701 		 */
1702 		ill_capability_send(ill, nmp);
1703 	} else {
1704 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1705 		    "advertised %x hardware checksum capability flags\n",
1706 		    ill->ill_name, ihck->hcksum_txflags));
1707 	}
1708 }
1709 
1710 static void
1711 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1712 {
1713 	dl_capab_hcksum_t *hck_subcap;
1714 	dl_capability_sub_t *dl_subcap;
1715 
1716 	if (!ILL_HCKSUM_CAPABLE(ill))
1717 		return;
1718 
1719 	ASSERT(ill->ill_hcksum_capab != NULL);
1720 
1721 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1722 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1723 	dl_subcap->dl_length = sizeof (*hck_subcap);
1724 
1725 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1726 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1727 	hck_subcap->hcksum_txflags = 0;
1728 
1729 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1730 }
1731 
1732 static void
1733 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1734 {
1735 	mblk_t *nmp = NULL;
1736 	dl_capability_req_t *oc;
1737 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1738 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1739 	uint_t sub_dl_cap = isub->dl_cap;
1740 	uint8_t *capend;
1741 
1742 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1743 
1744 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1745 
1746 	/*
1747 	 * Note: range checks here are not absolutely sufficient to
1748 	 * make us robust against malformed messages sent by drivers;
1749 	 * this is in keeping with the rest of IP's dlpi handling.
1750 	 * (Remember, it's coming from something else in the kernel
1751 	 * address space)
1752 	 */
1753 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1754 	if (capend > mp->b_wptr) {
1755 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1756 		    "malformed sub-capability too long for mblk");
1757 		return;
1758 	}
1759 
1760 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1761 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1762 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1763 		    "unsupported ZEROCOPY sub-capability (version %d, "
1764 		    "expected %d)", zc_ic->zerocopy_version,
1765 		    ZEROCOPY_VERSION_1);
1766 		return;
1767 	}
1768 
1769 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1770 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1771 		    "capability isn't as expected; pass-thru module(s) "
1772 		    "detected, discarding capability\n"));
1773 		return;
1774 	}
1775 
1776 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1777 		if (*ill_zerocopy_capab == NULL) {
1778 			*ill_zerocopy_capab =
1779 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1780 			    KM_NOSLEEP);
1781 
1782 			if (*ill_zerocopy_capab == NULL) {
1783 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1784 				    "could not enable Zero-copy version %d "
1785 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1786 				    ill->ill_name);
1787 				return;
1788 			}
1789 		}
1790 
1791 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1792 		    "supports Zero-copy version %d\n", ill->ill_name,
1793 		    ZEROCOPY_VERSION_1));
1794 
1795 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1796 		    zc_ic->zerocopy_version;
1797 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1798 		    zc_ic->zerocopy_flags;
1799 
1800 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1801 	} else {
1802 		uint_t size;
1803 		uchar_t *rptr;
1804 
1805 		size = sizeof (dl_capability_req_t) +
1806 		    sizeof (dl_capability_sub_t) +
1807 		    sizeof (dl_capab_zerocopy_t);
1808 
1809 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1810 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1811 			    "could not enable zerocopy for %s (ENOMEM)\n",
1812 			    ill->ill_name);
1813 			return;
1814 		}
1815 
1816 		rptr = nmp->b_rptr;
1817 		/* initialize dl_capability_req_t */
1818 		oc = (dl_capability_req_t *)rptr;
1819 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1820 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1821 		    sizeof (dl_capab_zerocopy_t);
1822 		rptr += sizeof (dl_capability_req_t);
1823 
1824 		/* initialize dl_capability_sub_t */
1825 		bcopy(isub, rptr, sizeof (*isub));
1826 		rptr += sizeof (*isub);
1827 
1828 		/* initialize dl_capab_zerocopy_t */
1829 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1830 		*zc_oc = *zc_ic;
1831 
1832 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1833 		    "to enable zero-copy version %d\n", ill->ill_name,
1834 		    ZEROCOPY_VERSION_1));
1835 
1836 		/* set VMSAFE_MEM flag */
1837 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1838 
1839 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1840 		ill_capability_send(ill, nmp);
1841 	}
1842 }
1843 
1844 static void
1845 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1846 {
1847 	dl_capab_zerocopy_t *zerocopy_subcap;
1848 	dl_capability_sub_t *dl_subcap;
1849 
1850 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1851 		return;
1852 
1853 	ASSERT(ill->ill_zerocopy_capab != NULL);
1854 
1855 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1856 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1857 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1858 
1859 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1860 	zerocopy_subcap->zerocopy_version =
1861 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1862 	zerocopy_subcap->zerocopy_flags = 0;
1863 
1864 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1865 }
1866 
1867 /*
1868  * DLD capability
1869  * Refer to dld.h for more information regarding the purpose and usage
1870  * of this capability.
1871  */
1872 static void
1873 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1874 {
1875 	dl_capab_dld_t		*dld_ic, dld;
1876 	uint_t			sub_dl_cap = isub->dl_cap;
1877 	uint8_t			*capend;
1878 	ill_dld_capab_t		*idc;
1879 
1880 	ASSERT(IAM_WRITER_ILL(ill));
1881 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1882 
1883 	/*
1884 	 * Note: range checks here are not absolutely sufficient to
1885 	 * make us robust against malformed messages sent by drivers;
1886 	 * this is in keeping with the rest of IP's dlpi handling.
1887 	 * (Remember, it's coming from something else in the kernel
1888 	 * address space)
1889 	 */
1890 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1891 	if (capend > mp->b_wptr) {
1892 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1893 		    "malformed sub-capability too long for mblk");
1894 		return;
1895 	}
1896 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1897 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1898 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1899 		    "unsupported DLD sub-capability (version %d, "
1900 		    "expected %d)", dld_ic->dld_version,
1901 		    DLD_CURRENT_VERSION);
1902 		return;
1903 	}
1904 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1905 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1906 		    "capability isn't as expected; pass-thru module(s) "
1907 		    "detected, discarding capability\n"));
1908 		return;
1909 	}
1910 
1911 	/*
1912 	 * Copy locally to ensure alignment.
1913 	 */
1914 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1915 
1916 	if ((idc = ill->ill_dld_capab) == NULL) {
1917 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1918 		if (idc == NULL) {
1919 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1920 			    "could not enable DLD version %d "
1921 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1922 			    ill->ill_name);
1923 			return;
1924 		}
1925 		ill->ill_dld_capab = idc;
1926 	}
1927 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1928 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1929 	ip1dbg(("ill_capability_dld_ack: interface %s "
1930 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1931 
1932 	ill_capability_dld_enable(ill);
1933 }
1934 
1935 /*
1936  * Typically capability negotiation between IP and the driver happens via
1937  * DLPI message exchange. However GLD also offers a direct function call
1938  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1939  * But arbitrary function calls into IP or GLD are not permitted, since both
1940  * of them are protected by their own perimeter mechanism. The perimeter can
1941  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1942  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1943  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1944  * to enter the mac perimeter and then do the direct function calls into
1945  * GLD to enable squeue polling. The ring related callbacks from the mac into
1946  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1947  * protected by the mac perimeter.
1948  */
1949 static void
1950 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1951 {
1952 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1953 	int			err;
1954 
1955 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1956 	    DLD_ENABLE);
1957 	ASSERT(err == 0);
1958 }
1959 
1960 static void
1961 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1962 {
1963 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1964 	int			err;
1965 
1966 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1967 	    DLD_DISABLE);
1968 	ASSERT(err == 0);
1969 }
1970 
1971 boolean_t
1972 ill_mac_perim_held(ill_t *ill)
1973 {
1974 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1975 
1976 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1977 	    DLD_QUERY));
1978 }
1979 
1980 static void
1981 ill_capability_direct_enable(ill_t *ill)
1982 {
1983 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1984 	ill_dld_direct_t	*idd = &idc->idc_direct;
1985 	dld_capab_direct_t	direct;
1986 	int			rc;
1987 
1988 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1989 
1990 	bzero(&direct, sizeof (direct));
1991 	direct.di_rx_cf = (uintptr_t)ip_input;
1992 	direct.di_rx_ch = ill;
1993 
1994 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1995 	    DLD_ENABLE);
1996 	if (rc == 0) {
1997 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1998 		idd->idd_tx_dh = direct.di_tx_dh;
1999 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2000 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2001 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2002 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2003 		ASSERT(idd->idd_tx_cb_df != NULL);
2004 		ASSERT(idd->idd_tx_fctl_df != NULL);
2005 		ASSERT(idd->idd_tx_df != NULL);
2006 		/*
2007 		 * One time registration of flow enable callback function
2008 		 */
2009 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2010 		    ill_flow_enable, ill);
2011 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2012 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2013 	} else {
2014 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2015 		    "capability, rc = %d\n", rc);
2016 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2017 	}
2018 }
2019 
2020 static void
2021 ill_capability_poll_enable(ill_t *ill)
2022 {
2023 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2024 	dld_capab_poll_t	poll;
2025 	int			rc;
2026 
2027 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2028 
2029 	bzero(&poll, sizeof (poll));
2030 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2031 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2032 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2033 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2034 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2035 	poll.poll_ring_ch = ill;
2036 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2037 	    DLD_ENABLE);
2038 	if (rc == 0) {
2039 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2040 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2041 	} else {
2042 		ip1dbg(("warning: could not enable POLL "
2043 		    "capability, rc = %d\n", rc));
2044 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2045 	}
2046 }
2047 
2048 /*
2049  * Enable the LSO capability.
2050  */
2051 static void
2052 ill_capability_lso_enable(ill_t *ill)
2053 {
2054 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2055 	dld_capab_lso_t	lso;
2056 	int rc;
2057 
2058 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2059 
2060 	if (ill->ill_lso_capab == NULL) {
2061 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2062 		    KM_NOSLEEP);
2063 		if (ill->ill_lso_capab == NULL) {
2064 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2065 			    "could not enable LSO for %s (ENOMEM)\n",
2066 			    ill->ill_name);
2067 			return;
2068 		}
2069 	}
2070 
2071 	bzero(&lso, sizeof (lso));
2072 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2073 	    DLD_ENABLE)) == 0) {
2074 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2075 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2076 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2077 		ip1dbg(("ill_capability_lso_enable: interface %s "
2078 		    "has enabled LSO\n ", ill->ill_name));
2079 	} else {
2080 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2081 		ill->ill_lso_capab = NULL;
2082 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2083 	}
2084 }
2085 
2086 static void
2087 ill_capability_dld_enable(ill_t *ill)
2088 {
2089 	mac_perim_handle_t mph;
2090 
2091 	ASSERT(IAM_WRITER_ILL(ill));
2092 
2093 	if (ill->ill_isv6)
2094 		return;
2095 
2096 	ill_mac_perim_enter(ill, &mph);
2097 	if (!ill->ill_isv6) {
2098 		ill_capability_direct_enable(ill);
2099 		ill_capability_poll_enable(ill);
2100 		ill_capability_lso_enable(ill);
2101 	}
2102 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2103 	ill_mac_perim_exit(ill, mph);
2104 }
2105 
2106 static void
2107 ill_capability_dld_disable(ill_t *ill)
2108 {
2109 	ill_dld_capab_t	*idc;
2110 	ill_dld_direct_t *idd;
2111 	mac_perim_handle_t	mph;
2112 
2113 	ASSERT(IAM_WRITER_ILL(ill));
2114 
2115 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2116 		return;
2117 
2118 	ill_mac_perim_enter(ill, &mph);
2119 
2120 	idc = ill->ill_dld_capab;
2121 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2122 		/*
2123 		 * For performance we avoid locks in the transmit data path
2124 		 * and don't maintain a count of the number of threads using
2125 		 * direct calls. Thus some threads could be using direct
2126 		 * transmit calls to GLD, even after the capability mechanism
2127 		 * turns it off. This is still safe since the handles used in
2128 		 * the direct calls continue to be valid until the unplumb is
2129 		 * completed. Remove the callback that was added (1-time) at
2130 		 * capab enable time.
2131 		 */
2132 		mutex_enter(&ill->ill_lock);
2133 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2134 		mutex_exit(&ill->ill_lock);
2135 		if (ill->ill_flownotify_mh != NULL) {
2136 			idd = &idc->idc_direct;
2137 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2138 			    ill->ill_flownotify_mh);
2139 			ill->ill_flownotify_mh = NULL;
2140 		}
2141 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2142 		    NULL, DLD_DISABLE);
2143 	}
2144 
2145 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2146 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2147 		ip_squeue_clean_all(ill);
2148 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2149 		    NULL, DLD_DISABLE);
2150 	}
2151 
2152 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2153 		ASSERT(ill->ill_lso_capab != NULL);
2154 		/*
2155 		 * Clear the capability flag for LSO but retain the
2156 		 * ill_lso_capab structure since it's possible that another
2157 		 * thread is still referring to it.  The structure only gets
2158 		 * deallocated when we destroy the ill.
2159 		 */
2160 
2161 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2162 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2163 		    NULL, DLD_DISABLE);
2164 	}
2165 
2166 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2167 	ill_mac_perim_exit(ill, mph);
2168 }
2169 
2170 /*
2171  * Capability Negotiation protocol
2172  *
2173  * We don't wait for DLPI capability operations to finish during interface
2174  * bringup or teardown. Doing so would introduce more asynchrony and the
2175  * interface up/down operations will need multiple return and restarts.
2176  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2177  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2178  * exclusive operation won't start until the DLPI operations of the previous
2179  * exclusive operation complete.
2180  *
2181  * The capability state machine is shown below.
2182  *
2183  * state		next state		event, action
2184  *
2185  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2186  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2187  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2188  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2189  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2190  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2191  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2192  *						    ill_capability_probe.
2193  */
2194 
2195 /*
2196  * Dedicated thread started from ip_stack_init that handles capability
2197  * disable. This thread ensures the taskq dispatch does not fail by waiting
2198  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2199  * that direct calls to DLD are done in a cv_waitable context.
2200  */
2201 void
2202 ill_taskq_dispatch(ip_stack_t *ipst)
2203 {
2204 	callb_cpr_t cprinfo;
2205 	char 	name[64];
2206 	mblk_t	*mp;
2207 
2208 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2209 	    ipst->ips_netstack->netstack_stackid);
2210 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2211 	    name);
2212 	mutex_enter(&ipst->ips_capab_taskq_lock);
2213 
2214 	for (;;) {
2215 		mp = ipst->ips_capab_taskq_head;
2216 		while (mp != NULL) {
2217 			ipst->ips_capab_taskq_head = mp->b_next;
2218 			if (ipst->ips_capab_taskq_head == NULL)
2219 				ipst->ips_capab_taskq_tail = NULL;
2220 			mutex_exit(&ipst->ips_capab_taskq_lock);
2221 			mp->b_next = NULL;
2222 
2223 			VERIFY(taskq_dispatch(system_taskq,
2224 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2225 			mutex_enter(&ipst->ips_capab_taskq_lock);
2226 			mp = ipst->ips_capab_taskq_head;
2227 		}
2228 
2229 		if (ipst->ips_capab_taskq_quit)
2230 			break;
2231 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2232 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2233 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2234 	}
2235 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2236 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2237 	CALLB_CPR_EXIT(&cprinfo);
2238 	thread_exit();
2239 }
2240 
2241 /*
2242  * Consume a new-style hardware capabilities negotiation ack.
2243  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2244  */
2245 static void
2246 ill_capability_ack_thr(void *arg)
2247 {
2248 	mblk_t	*mp = arg;
2249 	dl_capability_ack_t *capp;
2250 	dl_capability_sub_t *subp, *endp;
2251 	ill_t	*ill;
2252 	boolean_t reneg;
2253 
2254 	ill = (ill_t *)mp->b_prev;
2255 	mp->b_prev = NULL;
2256 
2257 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2258 
2259 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2260 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2261 		/*
2262 		 * We have received the ack for our DL_CAPAB reset request.
2263 		 * There isnt' anything in the message that needs processing.
2264 		 * All message based capabilities have been disabled, now
2265 		 * do the function call based capability disable.
2266 		 */
2267 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2268 		ill_capability_dld_disable(ill);
2269 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2270 		if (reneg)
2271 			ill_capability_probe(ill);
2272 		goto done;
2273 	}
2274 
2275 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2276 		ill->ill_dlpi_capab_state = IDCS_OK;
2277 
2278 	capp = (dl_capability_ack_t *)mp->b_rptr;
2279 
2280 	if (capp->dl_sub_length == 0) {
2281 		/* no new-style capabilities */
2282 		goto done;
2283 	}
2284 
2285 	/* make sure the driver supplied correct dl_sub_length */
2286 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2287 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2288 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2289 		goto done;
2290 	}
2291 
2292 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2293 	/*
2294 	 * There are sub-capabilities. Process the ones we know about.
2295 	 * Loop until we don't have room for another sub-cap header..
2296 	 */
2297 	for (subp = SC(capp, capp->dl_sub_offset),
2298 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2299 	    subp <= endp;
2300 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2301 
2302 		switch (subp->dl_cap) {
2303 		case DL_CAPAB_ID_WRAPPER:
2304 			ill_capability_id_ack(ill, mp, subp);
2305 			break;
2306 		default:
2307 			ill_capability_dispatch(ill, mp, subp);
2308 			break;
2309 		}
2310 	}
2311 #undef SC
2312 done:
2313 	inet_freemsg(mp);
2314 	ill_capability_done(ill);
2315 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2316 }
2317 
2318 /*
2319  * This needs to be started in a taskq thread to provide a cv_waitable
2320  * context.
2321  */
2322 void
2323 ill_capability_ack(ill_t *ill, mblk_t *mp)
2324 {
2325 	ip_stack_t	*ipst = ill->ill_ipst;
2326 
2327 	mp->b_prev = (mblk_t *)ill;
2328 	ASSERT(mp->b_next == NULL);
2329 
2330 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2331 	    TQ_NOSLEEP) != 0)
2332 		return;
2333 
2334 	/*
2335 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2336 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2337 	 */
2338 	mutex_enter(&ipst->ips_capab_taskq_lock);
2339 	if (ipst->ips_capab_taskq_head == NULL) {
2340 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2341 		ipst->ips_capab_taskq_head = mp;
2342 	} else {
2343 		ipst->ips_capab_taskq_tail->b_next = mp;
2344 	}
2345 	ipst->ips_capab_taskq_tail = mp;
2346 
2347 	cv_signal(&ipst->ips_capab_taskq_cv);
2348 	mutex_exit(&ipst->ips_capab_taskq_lock);
2349 }
2350 
2351 /*
2352  * This routine is called to scan the fragmentation reassembly table for
2353  * the specified ILL for any packets that are starting to smell.
2354  * dead_interval is the maximum time in seconds that will be tolerated.  It
2355  * will either be the value specified in ip_g_frag_timeout, or zero if the
2356  * ILL is shutting down and it is time to blow everything off.
2357  *
2358  * It returns the number of seconds (as a time_t) that the next frag timer
2359  * should be scheduled for, 0 meaning that the timer doesn't need to be
2360  * re-started.  Note that the method of calculating next_timeout isn't
2361  * entirely accurate since time will flow between the time we grab
2362  * current_time and the time we schedule the next timeout.  This isn't a
2363  * big problem since this is the timer for sending an ICMP reassembly time
2364  * exceeded messages, and it doesn't have to be exactly accurate.
2365  *
2366  * This function is
2367  * sometimes called as writer, although this is not required.
2368  */
2369 time_t
2370 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2371 {
2372 	ipfb_t	*ipfb;
2373 	ipfb_t	*endp;
2374 	ipf_t	*ipf;
2375 	ipf_t	*ipfnext;
2376 	mblk_t	*mp;
2377 	time_t	current_time = gethrestime_sec();
2378 	time_t	next_timeout = 0;
2379 	uint32_t	hdr_length;
2380 	mblk_t	*send_icmp_head;
2381 	mblk_t	*send_icmp_head_v6;
2382 	ip_stack_t *ipst = ill->ill_ipst;
2383 	ip_recv_attr_t iras;
2384 
2385 	bzero(&iras, sizeof (iras));
2386 	iras.ira_flags = 0;
2387 	iras.ira_ill = iras.ira_rill = ill;
2388 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2389 	iras.ira_rifindex = iras.ira_ruifindex;
2390 
2391 	ipfb = ill->ill_frag_hash_tbl;
2392 	if (ipfb == NULL)
2393 		return (B_FALSE);
2394 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2395 	/* Walk the frag hash table. */
2396 	for (; ipfb < endp; ipfb++) {
2397 		send_icmp_head = NULL;
2398 		send_icmp_head_v6 = NULL;
2399 		mutex_enter(&ipfb->ipfb_lock);
2400 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2401 			time_t frag_time = current_time - ipf->ipf_timestamp;
2402 			time_t frag_timeout;
2403 
2404 			if (frag_time < dead_interval) {
2405 				/*
2406 				 * There are some outstanding fragments
2407 				 * that will timeout later.  Make note of
2408 				 * the time so that we can reschedule the
2409 				 * next timeout appropriately.
2410 				 */
2411 				frag_timeout = dead_interval - frag_time;
2412 				if (next_timeout == 0 ||
2413 				    frag_timeout < next_timeout) {
2414 					next_timeout = frag_timeout;
2415 				}
2416 				break;
2417 			}
2418 			/* Time's up.  Get it out of here. */
2419 			hdr_length = ipf->ipf_nf_hdr_len;
2420 			ipfnext = ipf->ipf_hash_next;
2421 			if (ipfnext)
2422 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2423 			*ipf->ipf_ptphn = ipfnext;
2424 			mp = ipf->ipf_mp->b_cont;
2425 			for (; mp; mp = mp->b_cont) {
2426 				/* Extra points for neatness. */
2427 				IP_REASS_SET_START(mp, 0);
2428 				IP_REASS_SET_END(mp, 0);
2429 			}
2430 			mp = ipf->ipf_mp->b_cont;
2431 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2432 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2433 			ipfb->ipfb_count -= ipf->ipf_count;
2434 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2435 			ipfb->ipfb_frag_pkts--;
2436 			/*
2437 			 * We do not send any icmp message from here because
2438 			 * we currently are holding the ipfb_lock for this
2439 			 * hash chain. If we try and send any icmp messages
2440 			 * from here we may end up via a put back into ip
2441 			 * trying to get the same lock, causing a recursive
2442 			 * mutex panic. Instead we build a list and send all
2443 			 * the icmp messages after we have dropped the lock.
2444 			 */
2445 			if (ill->ill_isv6) {
2446 				if (hdr_length != 0) {
2447 					mp->b_next = send_icmp_head_v6;
2448 					send_icmp_head_v6 = mp;
2449 				} else {
2450 					freemsg(mp);
2451 				}
2452 			} else {
2453 				if (hdr_length != 0) {
2454 					mp->b_next = send_icmp_head;
2455 					send_icmp_head = mp;
2456 				} else {
2457 					freemsg(mp);
2458 				}
2459 			}
2460 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2461 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2462 			freeb(ipf->ipf_mp);
2463 		}
2464 		mutex_exit(&ipfb->ipfb_lock);
2465 		/*
2466 		 * Now need to send any icmp messages that we delayed from
2467 		 * above.
2468 		 */
2469 		while (send_icmp_head_v6 != NULL) {
2470 			ip6_t *ip6h;
2471 
2472 			mp = send_icmp_head_v6;
2473 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2474 			mp->b_next = NULL;
2475 			ip6h = (ip6_t *)mp->b_rptr;
2476 			iras.ira_flags = 0;
2477 			/*
2478 			 * This will result in an incorrect ALL_ZONES zoneid
2479 			 * for multicast packets, but we
2480 			 * don't send ICMP errors for those in any case.
2481 			 */
2482 			iras.ira_zoneid =
2483 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2484 			    ill, ipst);
2485 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2486 			icmp_time_exceeded_v6(mp,
2487 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2488 			    &iras);
2489 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2490 		}
2491 		while (send_icmp_head != NULL) {
2492 			ipaddr_t dst;
2493 
2494 			mp = send_icmp_head;
2495 			send_icmp_head = send_icmp_head->b_next;
2496 			mp->b_next = NULL;
2497 
2498 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2499 
2500 			iras.ira_flags = IRAF_IS_IPV4;
2501 			/*
2502 			 * This will result in an incorrect ALL_ZONES zoneid
2503 			 * for broadcast and multicast packets, but we
2504 			 * don't send ICMP errors for those in any case.
2505 			 */
2506 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2507 			    ill, ipst);
2508 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2509 			icmp_time_exceeded(mp,
2510 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2511 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2512 		}
2513 	}
2514 	/*
2515 	 * A non-dying ILL will use the return value to decide whether to
2516 	 * restart the frag timer, and for how long.
2517 	 */
2518 	return (next_timeout);
2519 }
2520 
2521 /*
2522  * This routine is called when the approximate count of mblk memory used
2523  * for the specified ILL has exceeded max_count.
2524  */
2525 void
2526 ill_frag_prune(ill_t *ill, uint_t max_count)
2527 {
2528 	ipfb_t	*ipfb;
2529 	ipf_t	*ipf;
2530 	size_t	count;
2531 	clock_t now;
2532 
2533 	/*
2534 	 * If we are here within ip_min_frag_prune_time msecs remove
2535 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2536 	 * ill_frag_free_num_pkts.
2537 	 */
2538 	mutex_enter(&ill->ill_lock);
2539 	now = ddi_get_lbolt();
2540 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2541 	    (ip_min_frag_prune_time != 0 ?
2542 	    ip_min_frag_prune_time : msec_per_tick)) {
2543 
2544 		ill->ill_frag_free_num_pkts++;
2545 
2546 	} else {
2547 		ill->ill_frag_free_num_pkts = 0;
2548 	}
2549 	ill->ill_last_frag_clean_time = now;
2550 	mutex_exit(&ill->ill_lock);
2551 
2552 	/*
2553 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2554 	 */
2555 	if (ill->ill_frag_free_num_pkts != 0) {
2556 		int ix;
2557 
2558 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2559 			ipfb = &ill->ill_frag_hash_tbl[ix];
2560 			mutex_enter(&ipfb->ipfb_lock);
2561 			if (ipfb->ipfb_ipf != NULL) {
2562 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2563 				    ill->ill_frag_free_num_pkts);
2564 			}
2565 			mutex_exit(&ipfb->ipfb_lock);
2566 		}
2567 	}
2568 	/*
2569 	 * While the reassembly list for this ILL is too big, prune a fragment
2570 	 * queue by age, oldest first.
2571 	 */
2572 	while (ill->ill_frag_count > max_count) {
2573 		int	ix;
2574 		ipfb_t	*oipfb = NULL;
2575 		uint_t	oldest = UINT_MAX;
2576 
2577 		count = 0;
2578 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2579 			ipfb = &ill->ill_frag_hash_tbl[ix];
2580 			mutex_enter(&ipfb->ipfb_lock);
2581 			ipf = ipfb->ipfb_ipf;
2582 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2583 				oldest = ipf->ipf_gen;
2584 				oipfb = ipfb;
2585 			}
2586 			count += ipfb->ipfb_count;
2587 			mutex_exit(&ipfb->ipfb_lock);
2588 		}
2589 		if (oipfb == NULL)
2590 			break;
2591 
2592 		if (count <= max_count)
2593 			return;	/* Somebody beat us to it, nothing to do */
2594 		mutex_enter(&oipfb->ipfb_lock);
2595 		ipf = oipfb->ipfb_ipf;
2596 		if (ipf != NULL) {
2597 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2598 		}
2599 		mutex_exit(&oipfb->ipfb_lock);
2600 	}
2601 }
2602 
2603 /*
2604  * free 'free_cnt' fragmented packets starting at ipf.
2605  */
2606 void
2607 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2608 {
2609 	size_t	count;
2610 	mblk_t	*mp;
2611 	mblk_t	*tmp;
2612 	ipf_t **ipfp = ipf->ipf_ptphn;
2613 
2614 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2615 	ASSERT(ipfp != NULL);
2616 	ASSERT(ipf != NULL);
2617 
2618 	while (ipf != NULL && free_cnt-- > 0) {
2619 		count = ipf->ipf_count;
2620 		mp = ipf->ipf_mp;
2621 		ipf = ipf->ipf_hash_next;
2622 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2623 			IP_REASS_SET_START(tmp, 0);
2624 			IP_REASS_SET_END(tmp, 0);
2625 		}
2626 		atomic_add_32(&ill->ill_frag_count, -count);
2627 		ASSERT(ipfb->ipfb_count >= count);
2628 		ipfb->ipfb_count -= count;
2629 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2630 		ipfb->ipfb_frag_pkts--;
2631 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2632 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2633 		freemsg(mp);
2634 	}
2635 
2636 	if (ipf)
2637 		ipf->ipf_ptphn = ipfp;
2638 	ipfp[0] = ipf;
2639 }
2640 
2641 /*
2642  * Helper function for ill_forward_set().
2643  */
2644 static void
2645 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2646 {
2647 	ip_stack_t	*ipst = ill->ill_ipst;
2648 
2649 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2650 
2651 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2652 	    (enable ? "Enabling" : "Disabling"),
2653 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2654 	mutex_enter(&ill->ill_lock);
2655 	if (enable)
2656 		ill->ill_flags |= ILLF_ROUTER;
2657 	else
2658 		ill->ill_flags &= ~ILLF_ROUTER;
2659 	mutex_exit(&ill->ill_lock);
2660 	if (ill->ill_isv6)
2661 		ill_set_nce_router_flags(ill, enable);
2662 	/* Notify routing socket listeners of this change. */
2663 	if (ill->ill_ipif != NULL)
2664 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2665 }
2666 
2667 /*
2668  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2669  * socket messages for each interface whose flags we change.
2670  */
2671 int
2672 ill_forward_set(ill_t *ill, boolean_t enable)
2673 {
2674 	ipmp_illgrp_t *illg;
2675 	ip_stack_t *ipst = ill->ill_ipst;
2676 
2677 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2678 
2679 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2680 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2681 		return (0);
2682 
2683 	if (IS_LOOPBACK(ill))
2684 		return (EINVAL);
2685 
2686 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2687 		/*
2688 		 * Update all of the interfaces in the group.
2689 		 */
2690 		illg = ill->ill_grp;
2691 		ill = list_head(&illg->ig_if);
2692 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2693 			ill_forward_set_on_ill(ill, enable);
2694 
2695 		/*
2696 		 * Update the IPMP meta-interface.
2697 		 */
2698 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2699 		return (0);
2700 	}
2701 
2702 	ill_forward_set_on_ill(ill, enable);
2703 	return (0);
2704 }
2705 
2706 /*
2707  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2708  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2709  * set or clear.
2710  */
2711 static void
2712 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2713 {
2714 	ipif_t *ipif;
2715 	ncec_t *ncec;
2716 	nce_t *nce;
2717 
2718 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2719 		/*
2720 		 * NOTE: we match across the illgrp because nce's for
2721 		 * addresses on IPMP interfaces have an nce_ill that points to
2722 		 * the bound underlying ill.
2723 		 */
2724 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2725 		if (nce != NULL) {
2726 			ncec = nce->nce_common;
2727 			mutex_enter(&ncec->ncec_lock);
2728 			if (enable)
2729 				ncec->ncec_flags |= NCE_F_ISROUTER;
2730 			else
2731 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2732 			mutex_exit(&ncec->ncec_lock);
2733 			nce_refrele(nce);
2734 		}
2735 	}
2736 }
2737 
2738 /*
2739  * Intializes the context structure and returns the first ill in the list
2740  * cuurently start_list and end_list can have values:
2741  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2742  * IP_V4_G_HEAD		Traverse IPV4 list only.
2743  * IP_V6_G_HEAD		Traverse IPV6 list only.
2744  */
2745 
2746 /*
2747  * We don't check for CONDEMNED ills here. Caller must do that if
2748  * necessary under the ill lock.
2749  */
2750 ill_t *
2751 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2752     ip_stack_t *ipst)
2753 {
2754 	ill_if_t *ifp;
2755 	ill_t *ill;
2756 	avl_tree_t *avl_tree;
2757 
2758 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2759 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2760 
2761 	/*
2762 	 * setup the lists to search
2763 	 */
2764 	if (end_list != MAX_G_HEADS) {
2765 		ctx->ctx_current_list = start_list;
2766 		ctx->ctx_last_list = end_list;
2767 	} else {
2768 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2769 		ctx->ctx_current_list = 0;
2770 	}
2771 
2772 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2773 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2774 		if (ifp != (ill_if_t *)
2775 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2776 			avl_tree = &ifp->illif_avl_by_ppa;
2777 			ill = avl_first(avl_tree);
2778 			/*
2779 			 * ill is guaranteed to be non NULL or ifp should have
2780 			 * not existed.
2781 			 */
2782 			ASSERT(ill != NULL);
2783 			return (ill);
2784 		}
2785 		ctx->ctx_current_list++;
2786 	}
2787 
2788 	return (NULL);
2789 }
2790 
2791 /*
2792  * returns the next ill in the list. ill_first() must have been called
2793  * before calling ill_next() or bad things will happen.
2794  */
2795 
2796 /*
2797  * We don't check for CONDEMNED ills here. Caller must do that if
2798  * necessary under the ill lock.
2799  */
2800 ill_t *
2801 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2802 {
2803 	ill_if_t *ifp;
2804 	ill_t *ill;
2805 	ip_stack_t	*ipst = lastill->ill_ipst;
2806 
2807 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2808 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2809 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2810 	    AVL_AFTER)) != NULL) {
2811 		return (ill);
2812 	}
2813 
2814 	/* goto next ill_ifp in the list. */
2815 	ifp = lastill->ill_ifptr->illif_next;
2816 
2817 	/* make sure not at end of circular list */
2818 	while (ifp ==
2819 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2820 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2821 			return (NULL);
2822 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2823 	}
2824 
2825 	return (avl_first(&ifp->illif_avl_by_ppa));
2826 }
2827 
2828 /*
2829  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2830  * The final number (PPA) must not have any leading zeros.  Upon success, a
2831  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2832  */
2833 static char *
2834 ill_get_ppa_ptr(char *name)
2835 {
2836 	int namelen = strlen(name);
2837 	int end_ndx = namelen - 1;
2838 	int ppa_ndx, i;
2839 
2840 	/*
2841 	 * Check that the first character is [a-zA-Z], and that the last
2842 	 * character is [0-9].
2843 	 */
2844 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2845 		return (NULL);
2846 
2847 	/*
2848 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2849 	 */
2850 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2851 		if (!isdigit(name[ppa_ndx - 1]))
2852 			break;
2853 
2854 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2855 		return (NULL);
2856 
2857 	/*
2858 	 * Check that the intermediate characters are [a-z0-9.]
2859 	 */
2860 	for (i = 1; i < ppa_ndx; i++) {
2861 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2862 		    name[i] != '.' && name[i] != '_') {
2863 			return (NULL);
2864 		}
2865 	}
2866 
2867 	return (name + ppa_ndx);
2868 }
2869 
2870 /*
2871  * use avl tree to locate the ill.
2872  */
2873 static ill_t *
2874 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2875 {
2876 	char *ppa_ptr = NULL;
2877 	int len;
2878 	uint_t ppa;
2879 	ill_t *ill = NULL;
2880 	ill_if_t *ifp;
2881 	int list;
2882 
2883 	/*
2884 	 * get ppa ptr
2885 	 */
2886 	if (isv6)
2887 		list = IP_V6_G_HEAD;
2888 	else
2889 		list = IP_V4_G_HEAD;
2890 
2891 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2892 		return (NULL);
2893 	}
2894 
2895 	len = ppa_ptr - name + 1;
2896 
2897 	ppa = stoi(&ppa_ptr);
2898 
2899 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2900 
2901 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2902 		/*
2903 		 * match is done on len - 1 as the name is not null
2904 		 * terminated it contains ppa in addition to the interface
2905 		 * name.
2906 		 */
2907 		if ((ifp->illif_name_len == len) &&
2908 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2909 			break;
2910 		} else {
2911 			ifp = ifp->illif_next;
2912 		}
2913 	}
2914 
2915 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2916 		/*
2917 		 * Even the interface type does not exist.
2918 		 */
2919 		return (NULL);
2920 	}
2921 
2922 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2923 	if (ill != NULL) {
2924 		mutex_enter(&ill->ill_lock);
2925 		if (ILL_CAN_LOOKUP(ill)) {
2926 			ill_refhold_locked(ill);
2927 			mutex_exit(&ill->ill_lock);
2928 			return (ill);
2929 		}
2930 		mutex_exit(&ill->ill_lock);
2931 	}
2932 	return (NULL);
2933 }
2934 
2935 /*
2936  * comparison function for use with avl.
2937  */
2938 static int
2939 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2940 {
2941 	uint_t ppa;
2942 	uint_t ill_ppa;
2943 
2944 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2945 
2946 	ppa = *((uint_t *)ppa_ptr);
2947 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2948 	/*
2949 	 * We want the ill with the lowest ppa to be on the
2950 	 * top.
2951 	 */
2952 	if (ill_ppa < ppa)
2953 		return (1);
2954 	if (ill_ppa > ppa)
2955 		return (-1);
2956 	return (0);
2957 }
2958 
2959 /*
2960  * remove an interface type from the global list.
2961  */
2962 static void
2963 ill_delete_interface_type(ill_if_t *interface)
2964 {
2965 	ASSERT(interface != NULL);
2966 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2967 
2968 	avl_destroy(&interface->illif_avl_by_ppa);
2969 	if (interface->illif_ppa_arena != NULL)
2970 		vmem_destroy(interface->illif_ppa_arena);
2971 
2972 	remque(interface);
2973 
2974 	mi_free(interface);
2975 }
2976 
2977 /*
2978  * remove ill from the global list.
2979  */
2980 static void
2981 ill_glist_delete(ill_t *ill)
2982 {
2983 	ip_stack_t	*ipst;
2984 	phyint_t	*phyi;
2985 
2986 	if (ill == NULL)
2987 		return;
2988 	ipst = ill->ill_ipst;
2989 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
2990 
2991 	/*
2992 	 * If the ill was never inserted into the AVL tree
2993 	 * we skip the if branch.
2994 	 */
2995 	if (ill->ill_ifptr != NULL) {
2996 		/*
2997 		 * remove from AVL tree and free ppa number
2998 		 */
2999 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3000 
3001 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3002 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3003 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3004 		}
3005 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3006 			ill_delete_interface_type(ill->ill_ifptr);
3007 		}
3008 
3009 		/*
3010 		 * Indicate ill is no longer in the list.
3011 		 */
3012 		ill->ill_ifptr = NULL;
3013 		ill->ill_name_length = 0;
3014 		ill->ill_name[0] = '\0';
3015 		ill->ill_ppa = UINT_MAX;
3016 	}
3017 
3018 	/* Generate one last event for this ill. */
3019 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3020 	    ill->ill_name_length);
3021 
3022 	ASSERT(ill->ill_phyint != NULL);
3023 	phyi = ill->ill_phyint;
3024 	ill->ill_phyint = NULL;
3025 
3026 	/*
3027 	 * ill_init allocates a phyint always to store the copy
3028 	 * of flags relevant to phyint. At that point in time, we could
3029 	 * not assign the name and hence phyint_illv4/v6 could not be
3030 	 * initialized. Later in ipif_set_values, we assign the name to
3031 	 * the ill, at which point in time we assign phyint_illv4/v6.
3032 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3033 	 */
3034 	if (ill->ill_flags & ILLF_IPV6)
3035 		phyi->phyint_illv6 = NULL;
3036 	else
3037 		phyi->phyint_illv4 = NULL;
3038 
3039 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3040 		rw_exit(&ipst->ips_ill_g_lock);
3041 		return;
3042 	}
3043 
3044 	/*
3045 	 * There are no ills left on this phyint; pull it out of the phyint
3046 	 * avl trees, and free it.
3047 	 */
3048 	if (phyi->phyint_ifindex > 0) {
3049 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3050 		    phyi);
3051 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3052 		    phyi);
3053 	}
3054 	rw_exit(&ipst->ips_ill_g_lock);
3055 
3056 	phyint_free(phyi);
3057 }
3058 
3059 /*
3060  * allocate a ppa, if the number of plumbed interfaces of this type are
3061  * less than ill_no_arena do a linear search to find a unused ppa.
3062  * When the number goes beyond ill_no_arena switch to using an arena.
3063  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3064  * is the return value for an error condition, so allocation starts at one
3065  * and is decremented by one.
3066  */
3067 static int
3068 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3069 {
3070 	ill_t *tmp_ill;
3071 	uint_t start, end;
3072 	int ppa;
3073 
3074 	if (ifp->illif_ppa_arena == NULL &&
3075 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3076 		/*
3077 		 * Create an arena.
3078 		 */
3079 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3080 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3081 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3082 			/* allocate what has already been assigned */
3083 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3084 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3085 		    tmp_ill, AVL_AFTER)) {
3086 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3087 			    1,		/* size */
3088 			    1,		/* align/quantum */
3089 			    0,		/* phase */
3090 			    0,		/* nocross */
3091 			    /* minaddr */
3092 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3093 			    /* maxaddr */
3094 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3095 			    VM_NOSLEEP|VM_FIRSTFIT);
3096 			if (ppa == 0) {
3097 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3098 				    " failed while switching"));
3099 				vmem_destroy(ifp->illif_ppa_arena);
3100 				ifp->illif_ppa_arena = NULL;
3101 				break;
3102 			}
3103 		}
3104 	}
3105 
3106 	if (ifp->illif_ppa_arena != NULL) {
3107 		if (ill->ill_ppa == UINT_MAX) {
3108 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3109 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3110 			if (ppa == 0)
3111 				return (EAGAIN);
3112 			ill->ill_ppa = --ppa;
3113 		} else {
3114 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3115 			    1, 		/* size */
3116 			    1, 		/* align/quantum */
3117 			    0, 		/* phase */
3118 			    0, 		/* nocross */
3119 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3120 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3121 			    VM_NOSLEEP|VM_FIRSTFIT);
3122 			/*
3123 			 * Most likely the allocation failed because
3124 			 * the requested ppa was in use.
3125 			 */
3126 			if (ppa == 0)
3127 				return (EEXIST);
3128 		}
3129 		return (0);
3130 	}
3131 
3132 	/*
3133 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3134 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3135 	 */
3136 	if (ill->ill_ppa == UINT_MAX) {
3137 		end = UINT_MAX - 1;
3138 		start = 0;
3139 	} else {
3140 		end = start = ill->ill_ppa;
3141 	}
3142 
3143 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3144 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3145 		if (start++ >= end) {
3146 			if (ill->ill_ppa == UINT_MAX)
3147 				return (EAGAIN);
3148 			else
3149 				return (EEXIST);
3150 		}
3151 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3152 	}
3153 	ill->ill_ppa = start;
3154 	return (0);
3155 }
3156 
3157 /*
3158  * Insert ill into the list of configured ill's. Once this function completes,
3159  * the ill is globally visible and is available through lookups. More precisely
3160  * this happens after the caller drops the ill_g_lock.
3161  */
3162 static int
3163 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3164 {
3165 	ill_if_t *ill_interface;
3166 	avl_index_t where = 0;
3167 	int error;
3168 	int name_length;
3169 	int index;
3170 	boolean_t check_length = B_FALSE;
3171 	ip_stack_t	*ipst = ill->ill_ipst;
3172 
3173 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3174 
3175 	name_length = mi_strlen(name) + 1;
3176 
3177 	if (isv6)
3178 		index = IP_V6_G_HEAD;
3179 	else
3180 		index = IP_V4_G_HEAD;
3181 
3182 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3183 	/*
3184 	 * Search for interface type based on name
3185 	 */
3186 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3187 		if ((ill_interface->illif_name_len == name_length) &&
3188 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3189 			break;
3190 		}
3191 		ill_interface = ill_interface->illif_next;
3192 	}
3193 
3194 	/*
3195 	 * Interface type not found, create one.
3196 	 */
3197 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3198 		ill_g_head_t ghead;
3199 
3200 		/*
3201 		 * allocate ill_if_t structure
3202 		 */
3203 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3204 		if (ill_interface == NULL) {
3205 			return (ENOMEM);
3206 		}
3207 
3208 		(void) strcpy(ill_interface->illif_name, name);
3209 		ill_interface->illif_name_len = name_length;
3210 
3211 		avl_create(&ill_interface->illif_avl_by_ppa,
3212 		    ill_compare_ppa, sizeof (ill_t),
3213 		    offsetof(struct ill_s, ill_avl_byppa));
3214 
3215 		/*
3216 		 * link the structure in the back to maintain order
3217 		 * of configuration for ifconfig output.
3218 		 */
3219 		ghead = ipst->ips_ill_g_heads[index];
3220 		insque(ill_interface, ghead.ill_g_list_tail);
3221 	}
3222 
3223 	if (ill->ill_ppa == UINT_MAX)
3224 		check_length = B_TRUE;
3225 
3226 	error = ill_alloc_ppa(ill_interface, ill);
3227 	if (error != 0) {
3228 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3229 			ill_delete_interface_type(ill->ill_ifptr);
3230 		return (error);
3231 	}
3232 
3233 	/*
3234 	 * When the ppa is choosen by the system, check that there is
3235 	 * enough space to insert ppa. if a specific ppa was passed in this
3236 	 * check is not required as the interface name passed in will have
3237 	 * the right ppa in it.
3238 	 */
3239 	if (check_length) {
3240 		/*
3241 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3242 		 */
3243 		char buf[sizeof (uint_t) * 3];
3244 
3245 		/*
3246 		 * convert ppa to string to calculate the amount of space
3247 		 * required for it in the name.
3248 		 */
3249 		numtos(ill->ill_ppa, buf);
3250 
3251 		/* Do we have enough space to insert ppa ? */
3252 
3253 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3254 			/* Free ppa and interface type struct */
3255 			if (ill_interface->illif_ppa_arena != NULL) {
3256 				vmem_free(ill_interface->illif_ppa_arena,
3257 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3258 			}
3259 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3260 				ill_delete_interface_type(ill->ill_ifptr);
3261 
3262 			return (EINVAL);
3263 		}
3264 	}
3265 
3266 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3267 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3268 
3269 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3270 	    &where);
3271 	ill->ill_ifptr = ill_interface;
3272 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3273 
3274 	ill_phyint_reinit(ill);
3275 	return (0);
3276 }
3277 
3278 /* Initialize the per phyint ipsq used for serialization */
3279 static boolean_t
3280 ipsq_init(ill_t *ill, boolean_t enter)
3281 {
3282 	ipsq_t  *ipsq;
3283 	ipxop_t	*ipx;
3284 
3285 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3286 		return (B_FALSE);
3287 
3288 	ill->ill_phyint->phyint_ipsq = ipsq;
3289 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3290 	ipx->ipx_ipsq = ipsq;
3291 	ipsq->ipsq_next = ipsq;
3292 	ipsq->ipsq_phyint = ill->ill_phyint;
3293 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3294 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3295 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3296 	if (enter) {
3297 		ipx->ipx_writer = curthread;
3298 		ipx->ipx_forced = B_FALSE;
3299 		ipx->ipx_reentry_cnt = 1;
3300 #ifdef DEBUG
3301 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3302 #endif
3303 	}
3304 	return (B_TRUE);
3305 }
3306 
3307 /*
3308  * ill_init is called by ip_open when a device control stream is opened.
3309  * It does a few initializations, and shoots a DL_INFO_REQ message down
3310  * to the driver.  The response is later picked up in ip_rput_dlpi and
3311  * used to set up default mechanisms for talking to the driver.  (Always
3312  * called as writer.)
3313  *
3314  * If this function returns error, ip_open will call ip_close which in
3315  * turn will call ill_delete to clean up any memory allocated here that
3316  * is not yet freed.
3317  */
3318 int
3319 ill_init(queue_t *q, ill_t *ill)
3320 {
3321 	int	count;
3322 	dl_info_req_t	*dlir;
3323 	mblk_t	*info_mp;
3324 	uchar_t *frag_ptr;
3325 
3326 	/*
3327 	 * The ill is initialized to zero by mi_alloc*(). In addition
3328 	 * some fields already contain valid values, initialized in
3329 	 * ip_open(), before we reach here.
3330 	 */
3331 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3332 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3333 	ill->ill_saved_ire_cnt = 0;
3334 
3335 	ill->ill_rq = q;
3336 	ill->ill_wq = WR(q);
3337 
3338 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3339 	    BPRI_HI);
3340 	if (info_mp == NULL)
3341 		return (ENOMEM);
3342 
3343 	/*
3344 	 * Allocate sufficient space to contain our fragment hash table and
3345 	 * the device name.
3346 	 */
3347 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3348 	if (frag_ptr == NULL) {
3349 		freemsg(info_mp);
3350 		return (ENOMEM);
3351 	}
3352 	ill->ill_frag_ptr = frag_ptr;
3353 	ill->ill_frag_free_num_pkts = 0;
3354 	ill->ill_last_frag_clean_time = 0;
3355 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3356 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3357 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3358 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3359 		    NULL, MUTEX_DEFAULT, NULL);
3360 	}
3361 
3362 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3363 	if (ill->ill_phyint == NULL) {
3364 		freemsg(info_mp);
3365 		mi_free(frag_ptr);
3366 		return (ENOMEM);
3367 	}
3368 
3369 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3370 	/*
3371 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3372 	 * at this point because of the following reason. If we can't
3373 	 * enter the ipsq at some point and cv_wait, the writer that
3374 	 * wakes us up tries to locate us using the list of all phyints
3375 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3376 	 * If we don't set it now, we risk a missed wakeup.
3377 	 */
3378 	ill->ill_phyint->phyint_illv4 = ill;
3379 	ill->ill_ppa = UINT_MAX;
3380 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3381 
3382 	ill_set_inputfn(ill);
3383 
3384 	if (!ipsq_init(ill, B_TRUE)) {
3385 		freemsg(info_mp);
3386 		mi_free(frag_ptr);
3387 		mi_free(ill->ill_phyint);
3388 		return (ENOMEM);
3389 	}
3390 
3391 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3392 
3393 	/* Frag queue limit stuff */
3394 	ill->ill_frag_count = 0;
3395 	ill->ill_ipf_gen = 0;
3396 
3397 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3398 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3399 	ill->ill_global_timer = INFINITY;
3400 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3401 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3402 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3403 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3404 
3405 	/*
3406 	 * Initialize IPv6 configuration variables.  The IP module is always
3407 	 * opened as an IPv4 module.  Instead tracking down the cases where
3408 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3409 	 * here for convenience, this has no effect until the ill is set to do
3410 	 * IPv6.
3411 	 */
3412 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3413 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3414 	ill->ill_max_buf = ND_MAX_Q;
3415 	ill->ill_refcnt = 0;
3416 
3417 	/* Send down the Info Request to the driver. */
3418 	info_mp->b_datap->db_type = M_PCPROTO;
3419 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3420 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3421 	dlir->dl_primitive = DL_INFO_REQ;
3422 
3423 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3424 
3425 	qprocson(q);
3426 	ill_dlpi_send(ill, info_mp);
3427 
3428 	return (0);
3429 }
3430 
3431 /*
3432  * ill_dls_info
3433  * creates datalink socket info from the device.
3434  */
3435 int
3436 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3437 {
3438 	size_t	len;
3439 
3440 	sdl->sdl_family = AF_LINK;
3441 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3442 	sdl->sdl_type = ill->ill_type;
3443 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3444 	len = strlen(sdl->sdl_data);
3445 	ASSERT(len < 256);
3446 	sdl->sdl_nlen = (uchar_t)len;
3447 	sdl->sdl_alen = ill->ill_phys_addr_length;
3448 	sdl->sdl_slen = 0;
3449 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3450 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3451 
3452 	return (sizeof (struct sockaddr_dl));
3453 }
3454 
3455 /*
3456  * ill_xarp_info
3457  * creates xarp info from the device.
3458  */
3459 static int
3460 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3461 {
3462 	sdl->sdl_family = AF_LINK;
3463 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3464 	sdl->sdl_type = ill->ill_type;
3465 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3466 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3467 	sdl->sdl_alen = ill->ill_phys_addr_length;
3468 	sdl->sdl_slen = 0;
3469 	return (sdl->sdl_nlen);
3470 }
3471 
3472 static int
3473 loopback_kstat_update(kstat_t *ksp, int rw)
3474 {
3475 	kstat_named_t *kn;
3476 	netstackid_t	stackid;
3477 	netstack_t	*ns;
3478 	ip_stack_t	*ipst;
3479 
3480 	if (ksp == NULL || ksp->ks_data == NULL)
3481 		return (EIO);
3482 
3483 	if (rw == KSTAT_WRITE)
3484 		return (EACCES);
3485 
3486 	kn = KSTAT_NAMED_PTR(ksp);
3487 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3488 
3489 	ns = netstack_find_by_stackid(stackid);
3490 	if (ns == NULL)
3491 		return (-1);
3492 
3493 	ipst = ns->netstack_ip;
3494 	if (ipst == NULL) {
3495 		netstack_rele(ns);
3496 		return (-1);
3497 	}
3498 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3499 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3500 	netstack_rele(ns);
3501 	return (0);
3502 }
3503 
3504 /*
3505  * Has ifindex been plumbed already?
3506  */
3507 static boolean_t
3508 phyint_exists(uint_t index, ip_stack_t *ipst)
3509 {
3510 	ASSERT(index != 0);
3511 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3512 
3513 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3514 	    &index, NULL) != NULL);
3515 }
3516 
3517 /* Pick a unique ifindex */
3518 boolean_t
3519 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3520 {
3521 	uint_t starting_index;
3522 
3523 	if (!ipst->ips_ill_index_wrap) {
3524 		*indexp = ipst->ips_ill_index++;
3525 		if (ipst->ips_ill_index == 0) {
3526 			/* Reached the uint_t limit Next time wrap  */
3527 			ipst->ips_ill_index_wrap = B_TRUE;
3528 		}
3529 		return (B_TRUE);
3530 	}
3531 
3532 	/*
3533 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3534 	 * at this point and don't want to call any function that attempts
3535 	 * to get the lock again.
3536 	 */
3537 	starting_index = ipst->ips_ill_index++;
3538 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3539 		if (ipst->ips_ill_index != 0 &&
3540 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3541 			/* found unused index - use it */
3542 			*indexp = ipst->ips_ill_index;
3543 			return (B_TRUE);
3544 		}
3545 	}
3546 
3547 	/*
3548 	 * all interface indicies are inuse.
3549 	 */
3550 	return (B_FALSE);
3551 }
3552 
3553 /*
3554  * Assign a unique interface index for the phyint.
3555  */
3556 static boolean_t
3557 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3558 {
3559 	ASSERT(phyi->phyint_ifindex == 0);
3560 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3561 }
3562 
3563 /*
3564  * Initialize the flags on `phyi' as per the provided mactype.
3565  */
3566 static void
3567 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3568 {
3569 	uint64_t flags = 0;
3570 
3571 	/*
3572 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3573 	 * we always presume the underlying hardware is working and set
3574 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3575 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3576 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3577 	 */
3578 	if (mactype == SUNW_DL_IPMP)
3579 		flags |= PHYI_FAILED;
3580 	else
3581 		flags |= PHYI_RUNNING;
3582 
3583 	switch (mactype) {
3584 	case SUNW_DL_VNI:
3585 		flags |= PHYI_VIRTUAL;
3586 		break;
3587 	case SUNW_DL_IPMP:
3588 		flags |= PHYI_IPMP;
3589 		break;
3590 	case DL_LOOP:
3591 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3592 		break;
3593 	}
3594 
3595 	mutex_enter(&phyi->phyint_lock);
3596 	phyi->phyint_flags |= flags;
3597 	mutex_exit(&phyi->phyint_lock);
3598 }
3599 
3600 /*
3601  * Return a pointer to the ill which matches the supplied name.  Note that
3602  * the ill name length includes the null termination character.  (May be
3603  * called as writer.)
3604  * If do_alloc and the interface is "lo0" it will be automatically created.
3605  * Cannot bump up reference on condemned ills. So dup detect can't be done
3606  * using this func.
3607  */
3608 ill_t *
3609 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3610     boolean_t *did_alloc, ip_stack_t *ipst)
3611 {
3612 	ill_t	*ill;
3613 	ipif_t	*ipif;
3614 	ipsq_t	*ipsq;
3615 	kstat_named_t	*kn;
3616 	boolean_t isloopback;
3617 	in6_addr_t ov6addr;
3618 
3619 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3620 
3621 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3622 	ill = ill_find_by_name(name, isv6, ipst);
3623 	rw_exit(&ipst->ips_ill_g_lock);
3624 	if (ill != NULL)
3625 		return (ill);
3626 
3627 	/*
3628 	 * Couldn't find it.  Does this happen to be a lookup for the
3629 	 * loopback device and are we allowed to allocate it?
3630 	 */
3631 	if (!isloopback || !do_alloc)
3632 		return (NULL);
3633 
3634 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3635 	ill = ill_find_by_name(name, isv6, ipst);
3636 	if (ill != NULL) {
3637 		rw_exit(&ipst->ips_ill_g_lock);
3638 		return (ill);
3639 	}
3640 
3641 	/* Create the loopback device on demand */
3642 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3643 	    sizeof (ipif_loopback_name), BPRI_MED));
3644 	if (ill == NULL)
3645 		goto done;
3646 
3647 	*ill = ill_null;
3648 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3649 	ill->ill_ipst = ipst;
3650 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3651 	netstack_hold(ipst->ips_netstack);
3652 	/*
3653 	 * For exclusive stacks we set the zoneid to zero
3654 	 * to make IP operate as if in the global zone.
3655 	 */
3656 	ill->ill_zoneid = GLOBAL_ZONEID;
3657 
3658 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3659 	if (ill->ill_phyint == NULL)
3660 		goto done;
3661 
3662 	if (isv6)
3663 		ill->ill_phyint->phyint_illv6 = ill;
3664 	else
3665 		ill->ill_phyint->phyint_illv4 = ill;
3666 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3667 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3668 
3669 	if (isv6) {
3670 		ill->ill_isv6 = B_TRUE;
3671 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3672 	} else {
3673 		ill->ill_max_frag = ip_loopback_mtuplus;
3674 	}
3675 	if (!ill_allocate_mibs(ill))
3676 		goto done;
3677 	ill->ill_current_frag = ill->ill_max_frag;
3678 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3679 	/*
3680 	 * ipif_loopback_name can't be pointed at directly because its used
3681 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3682 	 * from the glist, ill_glist_delete() sets the first character of
3683 	 * ill_name to '\0'.
3684 	 */
3685 	ill->ill_name = (char *)ill + sizeof (*ill);
3686 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3687 	ill->ill_name_length = sizeof (ipif_loopback_name);
3688 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3689 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3690 
3691 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3692 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3693 	ill->ill_global_timer = INFINITY;
3694 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3695 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3696 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3697 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3698 
3699 	/* No resolver here. */
3700 	ill->ill_net_type = IRE_LOOPBACK;
3701 
3702 	/* Initialize the ipsq */
3703 	if (!ipsq_init(ill, B_FALSE))
3704 		goto done;
3705 
3706 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3707 	if (ipif == NULL)
3708 		goto done;
3709 
3710 	ill->ill_flags = ILLF_MULTICAST;
3711 
3712 	ov6addr = ipif->ipif_v6lcl_addr;
3713 	/* Set up default loopback address and mask. */
3714 	if (!isv6) {
3715 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3716 
3717 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3718 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3719 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3720 		    ipif->ipif_v6subnet);
3721 		ill->ill_flags |= ILLF_IPV4;
3722 	} else {
3723 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3724 		ipif->ipif_v6net_mask = ipv6_all_ones;
3725 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3726 		    ipif->ipif_v6subnet);
3727 		ill->ill_flags |= ILLF_IPV6;
3728 	}
3729 
3730 	/*
3731 	 * Chain us in at the end of the ill list. hold the ill
3732 	 * before we make it globally visible. 1 for the lookup.
3733 	 */
3734 	ill->ill_refcnt = 0;
3735 	ill_refhold(ill);
3736 
3737 	ill->ill_frag_count = 0;
3738 	ill->ill_frag_free_num_pkts = 0;
3739 	ill->ill_last_frag_clean_time = 0;
3740 
3741 	ipsq = ill->ill_phyint->phyint_ipsq;
3742 
3743 	ill_set_inputfn(ill);
3744 
3745 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3746 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3747 
3748 	/* Let SCTP know so that it can add this to its list */
3749 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3750 
3751 	/*
3752 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3753 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3754 	 * requires to be after ill_glist_insert() since we need the
3755 	 * ill_index set. Pass on ipv6_loopback as the old address.
3756 	 */
3757 	sctp_update_ipif_addr(ipif, ov6addr);
3758 
3759 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3760 
3761 	/*
3762 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3763 	 * If so, free our original one.
3764 	 */
3765 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3766 		ipsq_delete(ipsq);
3767 
3768 	if (ipst->ips_loopback_ksp == NULL) {
3769 		/* Export loopback interface statistics */
3770 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3771 		    ipif_loopback_name, "net",
3772 		    KSTAT_TYPE_NAMED, 2, 0,
3773 		    ipst->ips_netstack->netstack_stackid);
3774 		if (ipst->ips_loopback_ksp != NULL) {
3775 			ipst->ips_loopback_ksp->ks_update =
3776 			    loopback_kstat_update;
3777 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3778 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3779 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3780 			ipst->ips_loopback_ksp->ks_private =
3781 			    (void *)(uintptr_t)ipst->ips_netstack->
3782 			    netstack_stackid;
3783 			kstat_install(ipst->ips_loopback_ksp);
3784 		}
3785 	}
3786 
3787 	*did_alloc = B_TRUE;
3788 	rw_exit(&ipst->ips_ill_g_lock);
3789 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3790 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3791 	return (ill);
3792 done:
3793 	if (ill != NULL) {
3794 		if (ill->ill_phyint != NULL) {
3795 			ipsq = ill->ill_phyint->phyint_ipsq;
3796 			if (ipsq != NULL) {
3797 				ipsq->ipsq_phyint = NULL;
3798 				ipsq_delete(ipsq);
3799 			}
3800 			mi_free(ill->ill_phyint);
3801 		}
3802 		ill_free_mib(ill);
3803 		if (ill->ill_ipst != NULL)
3804 			netstack_rele(ill->ill_ipst->ips_netstack);
3805 		mi_free(ill);
3806 	}
3807 	rw_exit(&ipst->ips_ill_g_lock);
3808 	return (NULL);
3809 }
3810 
3811 /*
3812  * For IPP calls - use the ip_stack_t for global stack.
3813  */
3814 ill_t *
3815 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3816 {
3817 	ip_stack_t	*ipst;
3818 	ill_t		*ill;
3819 
3820 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3821 	if (ipst == NULL) {
3822 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3823 		return (NULL);
3824 	}
3825 
3826 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3827 	netstack_rele(ipst->ips_netstack);
3828 	return (ill);
3829 }
3830 
3831 /*
3832  * Return a pointer to the ill which matches the index and IP version type.
3833  */
3834 ill_t *
3835 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3836 {
3837 	ill_t	*ill;
3838 	phyint_t *phyi;
3839 
3840 	/*
3841 	 * Indexes are stored in the phyint - a common structure
3842 	 * to both IPv4 and IPv6.
3843 	 */
3844 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3845 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3846 	    (void *) &index, NULL);
3847 	if (phyi != NULL) {
3848 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3849 		if (ill != NULL) {
3850 			mutex_enter(&ill->ill_lock);
3851 			if (!ILL_IS_CONDEMNED(ill)) {
3852 				ill_refhold_locked(ill);
3853 				mutex_exit(&ill->ill_lock);
3854 				rw_exit(&ipst->ips_ill_g_lock);
3855 				return (ill);
3856 			}
3857 			mutex_exit(&ill->ill_lock);
3858 		}
3859 	}
3860 	rw_exit(&ipst->ips_ill_g_lock);
3861 	return (NULL);
3862 }
3863 
3864 /*
3865  * Verify whether or not an interface index is valid for the specified zoneid
3866  * to transmit packets.
3867  * It can be zero (meaning "reset") or an interface index assigned
3868  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3869  */
3870 boolean_t
3871 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3872     ip_stack_t *ipst)
3873 {
3874 	ill_t		*ill;
3875 
3876 	if (ifindex == 0)
3877 		return (B_TRUE);
3878 
3879 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3880 	if (ill == NULL)
3881 		return (B_FALSE);
3882 	if (IS_VNI(ill)) {
3883 		ill_refrele(ill);
3884 		return (B_FALSE);
3885 	}
3886 	ill_refrele(ill);
3887 	return (B_TRUE);
3888 }
3889 
3890 /*
3891  * Return the ifindex next in sequence after the passed in ifindex.
3892  * If there is no next ifindex for the given protocol, return 0.
3893  */
3894 uint_t
3895 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3896 {
3897 	phyint_t *phyi;
3898 	phyint_t *phyi_initial;
3899 	uint_t   ifindex;
3900 
3901 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3902 
3903 	if (index == 0) {
3904 		phyi = avl_first(
3905 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3906 	} else {
3907 		phyi = phyi_initial = avl_find(
3908 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3909 		    (void *) &index, NULL);
3910 	}
3911 
3912 	for (; phyi != NULL;
3913 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3914 	    phyi, AVL_AFTER)) {
3915 		/*
3916 		 * If we're not returning the first interface in the tree
3917 		 * and we still haven't moved past the phyint_t that
3918 		 * corresponds to index, avl_walk needs to be called again
3919 		 */
3920 		if (!((index != 0) && (phyi == phyi_initial))) {
3921 			if (isv6) {
3922 				if ((phyi->phyint_illv6) &&
3923 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3924 				    (phyi->phyint_illv6->ill_isv6 == 1))
3925 					break;
3926 			} else {
3927 				if ((phyi->phyint_illv4) &&
3928 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3929 				    (phyi->phyint_illv4->ill_isv6 == 0))
3930 					break;
3931 			}
3932 		}
3933 	}
3934 
3935 	rw_exit(&ipst->ips_ill_g_lock);
3936 
3937 	if (phyi != NULL)
3938 		ifindex = phyi->phyint_ifindex;
3939 	else
3940 		ifindex = 0;
3941 
3942 	return (ifindex);
3943 }
3944 
3945 /*
3946  * Return the ifindex for the named interface.
3947  * If there is no next ifindex for the interface, return 0.
3948  */
3949 uint_t
3950 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3951 {
3952 	phyint_t	*phyi;
3953 	avl_index_t	where = 0;
3954 	uint_t		ifindex;
3955 
3956 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3957 
3958 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3959 	    name, &where)) == NULL) {
3960 		rw_exit(&ipst->ips_ill_g_lock);
3961 		return (0);
3962 	}
3963 
3964 	ifindex = phyi->phyint_ifindex;
3965 
3966 	rw_exit(&ipst->ips_ill_g_lock);
3967 
3968 	return (ifindex);
3969 }
3970 
3971 /*
3972  * Return the ifindex to be used by upper layer protocols for instance
3973  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
3974  */
3975 uint_t
3976 ill_get_upper_ifindex(const ill_t *ill)
3977 {
3978 	if (IS_UNDER_IPMP(ill))
3979 		return (ipmp_ill_get_ipmp_ifindex(ill));
3980 	else
3981 		return (ill->ill_phyint->phyint_ifindex);
3982 }
3983 
3984 
3985 /*
3986  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
3987  * that gives a running thread a reference to the ill. This reference must be
3988  * released by the thread when it is done accessing the ill and related
3989  * objects. ill_refcnt can not be used to account for static references
3990  * such as other structures pointing to an ill. Callers must generally
3991  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
3992  * or be sure that the ill is not being deleted or changing state before
3993  * calling the refhold functions. A non-zero ill_refcnt ensures that the
3994  * ill won't change any of its critical state such as address, netmask etc.
3995  */
3996 void
3997 ill_refhold(ill_t *ill)
3998 {
3999 	mutex_enter(&ill->ill_lock);
4000 	ill->ill_refcnt++;
4001 	ILL_TRACE_REF(ill);
4002 	mutex_exit(&ill->ill_lock);
4003 }
4004 
4005 void
4006 ill_refhold_locked(ill_t *ill)
4007 {
4008 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4009 	ill->ill_refcnt++;
4010 	ILL_TRACE_REF(ill);
4011 }
4012 
4013 /* Returns true if we managed to get a refhold */
4014 boolean_t
4015 ill_check_and_refhold(ill_t *ill)
4016 {
4017 	mutex_enter(&ill->ill_lock);
4018 	if (!ILL_IS_CONDEMNED(ill)) {
4019 		ill_refhold_locked(ill);
4020 		mutex_exit(&ill->ill_lock);
4021 		return (B_TRUE);
4022 	}
4023 	mutex_exit(&ill->ill_lock);
4024 	return (B_FALSE);
4025 }
4026 
4027 /*
4028  * Must not be called while holding any locks. Otherwise if this is
4029  * the last reference to be released, there is a chance of recursive mutex
4030  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4031  * to restart an ioctl.
4032  */
4033 void
4034 ill_refrele(ill_t *ill)
4035 {
4036 	mutex_enter(&ill->ill_lock);
4037 	ASSERT(ill->ill_refcnt != 0);
4038 	ill->ill_refcnt--;
4039 	ILL_UNTRACE_REF(ill);
4040 	if (ill->ill_refcnt != 0) {
4041 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4042 		mutex_exit(&ill->ill_lock);
4043 		return;
4044 	}
4045 
4046 	/* Drops the ill_lock */
4047 	ipif_ill_refrele_tail(ill);
4048 }
4049 
4050 /*
4051  * Obtain a weak reference count on the ill. This reference ensures the
4052  * ill won't be freed, but the ill may change any of its critical state
4053  * such as netmask, address etc. Returns an error if the ill has started
4054  * closing.
4055  */
4056 boolean_t
4057 ill_waiter_inc(ill_t *ill)
4058 {
4059 	mutex_enter(&ill->ill_lock);
4060 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4061 		mutex_exit(&ill->ill_lock);
4062 		return (B_FALSE);
4063 	}
4064 	ill->ill_waiters++;
4065 	mutex_exit(&ill->ill_lock);
4066 	return (B_TRUE);
4067 }
4068 
4069 void
4070 ill_waiter_dcr(ill_t *ill)
4071 {
4072 	mutex_enter(&ill->ill_lock);
4073 	ill->ill_waiters--;
4074 	if (ill->ill_waiters == 0)
4075 		cv_broadcast(&ill->ill_cv);
4076 	mutex_exit(&ill->ill_lock);
4077 }
4078 
4079 /*
4080  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4081  * driver.  We construct best guess defaults for lower level information that
4082  * we need.  If an interface is brought up without injection of any overriding
4083  * information from outside, we have to be ready to go with these defaults.
4084  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4085  * we primarely want the dl_provider_style.
4086  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4087  * at which point we assume the other part of the information is valid.
4088  */
4089 void
4090 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4091 {
4092 	uchar_t		*brdcst_addr;
4093 	uint_t		brdcst_addr_length, phys_addr_length;
4094 	t_scalar_t	sap_length;
4095 	dl_info_ack_t	*dlia;
4096 	ip_m_t		*ipm;
4097 	dl_qos_cl_sel1_t *sel1;
4098 	int		min_mtu;
4099 
4100 	ASSERT(IAM_WRITER_ILL(ill));
4101 
4102 	/*
4103 	 * Till the ill is fully up  the ill is not globally visible.
4104 	 * So no need for a lock.
4105 	 */
4106 	dlia = (dl_info_ack_t *)mp->b_rptr;
4107 	ill->ill_mactype = dlia->dl_mac_type;
4108 
4109 	ipm = ip_m_lookup(dlia->dl_mac_type);
4110 	if (ipm == NULL) {
4111 		ipm = ip_m_lookup(DL_OTHER);
4112 		ASSERT(ipm != NULL);
4113 	}
4114 	ill->ill_media = ipm;
4115 
4116 	/*
4117 	 * When the new DLPI stuff is ready we'll pull lengths
4118 	 * from dlia.
4119 	 */
4120 	if (dlia->dl_version == DL_VERSION_2) {
4121 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4122 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4123 		    brdcst_addr_length);
4124 		if (brdcst_addr == NULL) {
4125 			brdcst_addr_length = 0;
4126 		}
4127 		sap_length = dlia->dl_sap_length;
4128 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4129 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4130 		    brdcst_addr_length, sap_length, phys_addr_length));
4131 	} else {
4132 		brdcst_addr_length = 6;
4133 		brdcst_addr = ip_six_byte_all_ones;
4134 		sap_length = -2;
4135 		phys_addr_length = brdcst_addr_length;
4136 	}
4137 
4138 	ill->ill_bcast_addr_length = brdcst_addr_length;
4139 	ill->ill_phys_addr_length = phys_addr_length;
4140 	ill->ill_sap_length = sap_length;
4141 
4142 	/*
4143 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4144 	 * but we must ensure a minimum IP MTU is used since other bits of
4145 	 * IP will fly apart otherwise.
4146 	 */
4147 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4148 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4149 	ill->ill_current_frag = ill->ill_max_frag;
4150 	ill->ill_mtu = ill->ill_max_frag;
4151 
4152 	ill->ill_type = ipm->ip_m_type;
4153 
4154 	if (!ill->ill_dlpi_style_set) {
4155 		if (dlia->dl_provider_style == DL_STYLE2)
4156 			ill->ill_needs_attach = 1;
4157 
4158 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4159 
4160 		/*
4161 		 * Allocate the first ipif on this ill.  We don't delay it
4162 		 * further as ioctl handling assumes at least one ipif exists.
4163 		 *
4164 		 * At this point we don't know whether the ill is v4 or v6.
4165 		 * We will know this whan the SIOCSLIFNAME happens and
4166 		 * the correct value for ill_isv6 will be assigned in
4167 		 * ipif_set_values(). We need to hold the ill lock and
4168 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4169 		 * the wakeup.
4170 		 */
4171 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4172 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4173 		mutex_enter(&ill->ill_lock);
4174 		ASSERT(ill->ill_dlpi_style_set == 0);
4175 		ill->ill_dlpi_style_set = 1;
4176 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4177 		cv_broadcast(&ill->ill_cv);
4178 		mutex_exit(&ill->ill_lock);
4179 		freemsg(mp);
4180 		return;
4181 	}
4182 	ASSERT(ill->ill_ipif != NULL);
4183 	/*
4184 	 * We know whether it is IPv4 or IPv6 now, as this is the
4185 	 * second DL_INFO_ACK we are recieving in response to the
4186 	 * DL_INFO_REQ sent in ipif_set_values.
4187 	 */
4188 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4189 	/*
4190 	 * Clear all the flags that were set based on ill_bcast_addr_length
4191 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4192 	 * changed now and we need to re-evaluate.
4193 	 */
4194 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4195 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4196 
4197 	/*
4198 	 * Free ill_bcast_mp as things could have changed now.
4199 	 *
4200 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4201 	 * with no underlying interfaces (and thus an unknown broadcast
4202 	 * address length), but we enforce that an interface is broadcast-
4203 	 * capable as part of allowing it to join a group.
4204 	 */
4205 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4206 		if (ill->ill_bcast_mp != NULL)
4207 			freemsg(ill->ill_bcast_mp);
4208 		ill->ill_net_type = IRE_IF_NORESOLVER;
4209 
4210 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4211 		    ill->ill_phys_addr_length,
4212 		    ill->ill_sap,
4213 		    ill->ill_sap_length);
4214 
4215 		if (ill->ill_isv6)
4216 			/*
4217 			 * Note: xresolv interfaces will eventually need NOARP
4218 			 * set here as well, but that will require those
4219 			 * external resolvers to have some knowledge of
4220 			 * that flag and act appropriately. Not to be changed
4221 			 * at present.
4222 			 */
4223 			ill->ill_flags |= ILLF_NONUD;
4224 		else
4225 			ill->ill_flags |= ILLF_NOARP;
4226 
4227 		if (ill->ill_mactype == SUNW_DL_VNI) {
4228 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4229 		} else if (ill->ill_phys_addr_length == 0 ||
4230 		    ill->ill_mactype == DL_IPV4 ||
4231 		    ill->ill_mactype == DL_IPV6) {
4232 			/*
4233 			 * The underying link is point-to-point, so mark the
4234 			 * interface as such.  We can do IP multicast over
4235 			 * such a link since it transmits all network-layer
4236 			 * packets to the remote side the same way.
4237 			 */
4238 			ill->ill_flags |= ILLF_MULTICAST;
4239 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4240 		}
4241 	} else {
4242 		ill->ill_net_type = IRE_IF_RESOLVER;
4243 		if (ill->ill_bcast_mp != NULL)
4244 			freemsg(ill->ill_bcast_mp);
4245 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4246 		    ill->ill_bcast_addr_length, ill->ill_sap,
4247 		    ill->ill_sap_length);
4248 		/*
4249 		 * Later detect lack of DLPI driver multicast
4250 		 * capability by catching DL_ENABMULTI errors in
4251 		 * ip_rput_dlpi.
4252 		 */
4253 		ill->ill_flags |= ILLF_MULTICAST;
4254 		if (!ill->ill_isv6)
4255 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4256 	}
4257 
4258 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4259 	if (ill->ill_mactype == SUNW_DL_IPMP)
4260 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4261 
4262 	/* By default an interface does not support any CoS marking */
4263 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4264 
4265 	/*
4266 	 * If we get QoS information in DL_INFO_ACK, the device supports
4267 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4268 	 */
4269 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4270 	    dlia->dl_qos_length);
4271 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4272 		ill->ill_flags |= ILLF_COS_ENABLED;
4273 	}
4274 
4275 	/* Clear any previous error indication. */
4276 	ill->ill_error = 0;
4277 	freemsg(mp);
4278 }
4279 
4280 /*
4281  * Perform various checks to verify that an address would make sense as a
4282  * local, remote, or subnet interface address.
4283  */
4284 static boolean_t
4285 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4286 {
4287 	ipaddr_t	net_mask;
4288 
4289 	/*
4290 	 * Don't allow all zeroes, or all ones, but allow
4291 	 * all ones netmask.
4292 	 */
4293 	if ((net_mask = ip_net_mask(addr)) == 0)
4294 		return (B_FALSE);
4295 	/* A given netmask overrides the "guess" netmask */
4296 	if (subnet_mask != 0)
4297 		net_mask = subnet_mask;
4298 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4299 	    (addr == (addr | ~net_mask)))) {
4300 		return (B_FALSE);
4301 	}
4302 
4303 	/*
4304 	 * Even if the netmask is all ones, we do not allow address to be
4305 	 * 255.255.255.255
4306 	 */
4307 	if (addr == INADDR_BROADCAST)
4308 		return (B_FALSE);
4309 
4310 	if (CLASSD(addr))
4311 		return (B_FALSE);
4312 
4313 	return (B_TRUE);
4314 }
4315 
4316 #define	V6_IPIF_LINKLOCAL(p)	\
4317 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4318 
4319 /*
4320  * Compare two given ipifs and check if the second one is better than
4321  * the first one using the order of preference (not taking deprecated
4322  * into acount) specified in ipif_lookup_multicast().
4323  */
4324 static boolean_t
4325 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4326 {
4327 	/* Check the least preferred first. */
4328 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4329 		/* If both ipifs are the same, use the first one. */
4330 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4331 			return (B_FALSE);
4332 		else
4333 			return (B_TRUE);
4334 	}
4335 
4336 	/* For IPv6, check for link local address. */
4337 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4338 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4339 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4340 			/* The second one is equal or less preferred. */
4341 			return (B_FALSE);
4342 		} else {
4343 			return (B_TRUE);
4344 		}
4345 	}
4346 
4347 	/* Then check for point to point interface. */
4348 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4349 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4350 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4351 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4352 			return (B_FALSE);
4353 		} else {
4354 			return (B_TRUE);
4355 		}
4356 	}
4357 
4358 	/* old_ipif is a normal interface, so no need to use the new one. */
4359 	return (B_FALSE);
4360 }
4361 
4362 /*
4363  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4364  * The ipif must be up, and its ill must multicast-capable, not
4365  * condemned, not an underlying interface in an IPMP group, and
4366  * not a VNI interface.  Order of preference:
4367  *
4368  * 	1a. normal
4369  * 	1b. normal, but deprecated
4370  * 	2a. point to point
4371  * 	2b. point to point, but deprecated
4372  * 	3a. link local
4373  * 	3b. link local, but deprecated
4374  * 	4. loopback.
4375  */
4376 static ipif_t *
4377 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4378 {
4379 	ill_t			*ill;
4380 	ill_walk_context_t	ctx;
4381 	ipif_t			*ipif;
4382 	ipif_t			*saved_ipif = NULL;
4383 	ipif_t			*dep_ipif = NULL;
4384 
4385 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4386 	if (isv6)
4387 		ill = ILL_START_WALK_V6(&ctx, ipst);
4388 	else
4389 		ill = ILL_START_WALK_V4(&ctx, ipst);
4390 
4391 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4392 		mutex_enter(&ill->ill_lock);
4393 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4394 		    ILL_IS_CONDEMNED(ill) ||
4395 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4396 			mutex_exit(&ill->ill_lock);
4397 			continue;
4398 		}
4399 		for (ipif = ill->ill_ipif; ipif != NULL;
4400 		    ipif = ipif->ipif_next) {
4401 			if (zoneid != ipif->ipif_zoneid &&
4402 			    zoneid != ALL_ZONES &&
4403 			    ipif->ipif_zoneid != ALL_ZONES) {
4404 				continue;
4405 			}
4406 			if (!(ipif->ipif_flags & IPIF_UP) ||
4407 			    IPIF_IS_CONDEMNED(ipif)) {
4408 				continue;
4409 			}
4410 
4411 			/*
4412 			 * Found one candidate.  If it is deprecated,
4413 			 * remember it in dep_ipif.  If it is not deprecated,
4414 			 * remember it in saved_ipif.
4415 			 */
4416 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4417 				if (dep_ipif == NULL) {
4418 					dep_ipif = ipif;
4419 				} else if (ipif_comp_multi(dep_ipif, ipif,
4420 				    isv6)) {
4421 					/*
4422 					 * If the previous dep_ipif does not
4423 					 * belong to the same ill, we've done
4424 					 * a ipif_refhold() on it.  So we need
4425 					 * to release it.
4426 					 */
4427 					if (dep_ipif->ipif_ill != ill)
4428 						ipif_refrele(dep_ipif);
4429 					dep_ipif = ipif;
4430 				}
4431 				continue;
4432 			}
4433 			if (saved_ipif == NULL) {
4434 				saved_ipif = ipif;
4435 			} else {
4436 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4437 					if (saved_ipif->ipif_ill != ill)
4438 						ipif_refrele(saved_ipif);
4439 					saved_ipif = ipif;
4440 				}
4441 			}
4442 		}
4443 		/*
4444 		 * Before going to the next ill, do a ipif_refhold() on the
4445 		 * saved ones.
4446 		 */
4447 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4448 			ipif_refhold_locked(saved_ipif);
4449 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4450 			ipif_refhold_locked(dep_ipif);
4451 		mutex_exit(&ill->ill_lock);
4452 	}
4453 	rw_exit(&ipst->ips_ill_g_lock);
4454 
4455 	/*
4456 	 * If we have only the saved_ipif, return it.  But if we have both
4457 	 * saved_ipif and dep_ipif, check to see which one is better.
4458 	 */
4459 	if (saved_ipif != NULL) {
4460 		if (dep_ipif != NULL) {
4461 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4462 				ipif_refrele(saved_ipif);
4463 				return (dep_ipif);
4464 			} else {
4465 				ipif_refrele(dep_ipif);
4466 				return (saved_ipif);
4467 			}
4468 		}
4469 		return (saved_ipif);
4470 	} else {
4471 		return (dep_ipif);
4472 	}
4473 }
4474 
4475 ill_t *
4476 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4477 {
4478 	ipif_t *ipif;
4479 	ill_t *ill;
4480 
4481 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4482 	if (ipif == NULL)
4483 		return (NULL);
4484 
4485 	ill = ipif->ipif_ill;
4486 	ill_refhold(ill);
4487 	ipif_refrele(ipif);
4488 	return (ill);
4489 }
4490 
4491 /*
4492  * This function is called when an application does not specify an interface
4493  * to be used for multicast traffic (joining a group/sending data).  It
4494  * calls ire_lookup_multi() to look for an interface route for the
4495  * specified multicast group.  Doing this allows the administrator to add
4496  * prefix routes for multicast to indicate which interface to be used for
4497  * multicast traffic in the above scenario.  The route could be for all
4498  * multicast (224.0/4), for a single multicast group (a /32 route) or
4499  * anything in between.  If there is no such multicast route, we just find
4500  * any multicast capable interface and return it.  The returned ipif
4501  * is refhold'ed.
4502  *
4503  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4504  * unicast table. This is used by CGTP.
4505  */
4506 ill_t *
4507 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4508     boolean_t *multirtp, ipaddr_t *setsrcp)
4509 {
4510 	ill_t			*ill;
4511 
4512 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4513 	if (ill != NULL)
4514 		return (ill);
4515 
4516 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4517 }
4518 
4519 /*
4520  * Look for an ipif with the specified interface address and destination.
4521  * The destination address is used only for matching point-to-point interfaces.
4522  */
4523 ipif_t *
4524 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4525 {
4526 	ipif_t	*ipif;
4527 	ill_t	*ill;
4528 	ill_walk_context_t ctx;
4529 
4530 	/*
4531 	 * First match all the point-to-point interfaces
4532 	 * before looking at non-point-to-point interfaces.
4533 	 * This is done to avoid returning non-point-to-point
4534 	 * ipif instead of unnumbered point-to-point ipif.
4535 	 */
4536 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4537 	ill = ILL_START_WALK_V4(&ctx, ipst);
4538 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4539 		mutex_enter(&ill->ill_lock);
4540 		for (ipif = ill->ill_ipif; ipif != NULL;
4541 		    ipif = ipif->ipif_next) {
4542 			/* Allow the ipif to be down */
4543 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4544 			    (ipif->ipif_lcl_addr == if_addr) &&
4545 			    (ipif->ipif_pp_dst_addr == dst)) {
4546 				if (!IPIF_IS_CONDEMNED(ipif)) {
4547 					ipif_refhold_locked(ipif);
4548 					mutex_exit(&ill->ill_lock);
4549 					rw_exit(&ipst->ips_ill_g_lock);
4550 					return (ipif);
4551 				}
4552 			}
4553 		}
4554 		mutex_exit(&ill->ill_lock);
4555 	}
4556 	rw_exit(&ipst->ips_ill_g_lock);
4557 
4558 	/* lookup the ipif based on interface address */
4559 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4560 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4561 	return (ipif);
4562 }
4563 
4564 /*
4565  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4566  */
4567 static ipif_t *
4568 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4569     zoneid_t zoneid, ip_stack_t *ipst)
4570 {
4571 	ipif_t  *ipif;
4572 	ill_t   *ill;
4573 	boolean_t ptp = B_FALSE;
4574 	ill_walk_context_t	ctx;
4575 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4576 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4577 
4578 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4579 	/*
4580 	 * Repeat twice, first based on local addresses and
4581 	 * next time for pointopoint.
4582 	 */
4583 repeat:
4584 	ill = ILL_START_WALK_V4(&ctx, ipst);
4585 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4586 		if (match_ill != NULL && ill != match_ill &&
4587 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4588 			continue;
4589 		}
4590 		mutex_enter(&ill->ill_lock);
4591 		for (ipif = ill->ill_ipif; ipif != NULL;
4592 		    ipif = ipif->ipif_next) {
4593 			if (zoneid != ALL_ZONES &&
4594 			    zoneid != ipif->ipif_zoneid &&
4595 			    ipif->ipif_zoneid != ALL_ZONES)
4596 				continue;
4597 
4598 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4599 				continue;
4600 
4601 			/* Allow the ipif to be down */
4602 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4603 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4604 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4605 			    (ipif->ipif_pp_dst_addr == addr))) {
4606 				if (!IPIF_IS_CONDEMNED(ipif)) {
4607 					ipif_refhold_locked(ipif);
4608 					mutex_exit(&ill->ill_lock);
4609 					rw_exit(&ipst->ips_ill_g_lock);
4610 					return (ipif);
4611 				}
4612 			}
4613 		}
4614 		mutex_exit(&ill->ill_lock);
4615 	}
4616 
4617 	/* If we already did the ptp case, then we are done */
4618 	if (ptp) {
4619 		rw_exit(&ipst->ips_ill_g_lock);
4620 		return (NULL);
4621 	}
4622 	ptp = B_TRUE;
4623 	goto repeat;
4624 }
4625 
4626 /*
4627  * Lookup an ipif with the specified address.  For point-to-point links we
4628  * look for matches on either the destination address or the local address,
4629  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4630  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4631  * (or illgrp if `match_ill' is in an IPMP group).
4632  */
4633 ipif_t *
4634 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4635     ip_stack_t *ipst)
4636 {
4637 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4638 	    zoneid, ipst));
4639 }
4640 
4641 /*
4642  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4643  * except that we will only return an address if it is not marked as
4644  * IPIF_DUPLICATE
4645  */
4646 ipif_t *
4647 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4648     ip_stack_t *ipst)
4649 {
4650 	return (ipif_lookup_addr_common(addr, match_ill,
4651 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4652 	    zoneid, ipst));
4653 }
4654 
4655 /*
4656  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4657  * `match_ill' across the IPMP group.  This function is only needed in some
4658  * corner-cases; almost everything should use ipif_lookup_addr().
4659  */
4660 ipif_t *
4661 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4662 {
4663 	ASSERT(match_ill != NULL);
4664 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4665 	    ipst));
4666 }
4667 
4668 /*
4669  * Look for an ipif with the specified address. For point-point links
4670  * we look for matches on either the destination address and the local
4671  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4672  * is set.
4673  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4674  * ill (or illgrp if `match_ill' is in an IPMP group).
4675  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4676  */
4677 zoneid_t
4678 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4679 {
4680 	zoneid_t zoneid;
4681 	ipif_t  *ipif;
4682 	ill_t   *ill;
4683 	boolean_t ptp = B_FALSE;
4684 	ill_walk_context_t	ctx;
4685 
4686 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4687 	/*
4688 	 * Repeat twice, first based on local addresses and
4689 	 * next time for pointopoint.
4690 	 */
4691 repeat:
4692 	ill = ILL_START_WALK_V4(&ctx, ipst);
4693 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4694 		if (match_ill != NULL && ill != match_ill &&
4695 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4696 			continue;
4697 		}
4698 		mutex_enter(&ill->ill_lock);
4699 		for (ipif = ill->ill_ipif; ipif != NULL;
4700 		    ipif = ipif->ipif_next) {
4701 			/* Allow the ipif to be down */
4702 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4703 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4704 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4705 			    (ipif->ipif_pp_dst_addr == addr)) &&
4706 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4707 				zoneid = ipif->ipif_zoneid;
4708 				mutex_exit(&ill->ill_lock);
4709 				rw_exit(&ipst->ips_ill_g_lock);
4710 				/*
4711 				 * If ipif_zoneid was ALL_ZONES then we have
4712 				 * a trusted extensions shared IP address.
4713 				 * In that case GLOBAL_ZONEID works to send.
4714 				 */
4715 				if (zoneid == ALL_ZONES)
4716 					zoneid = GLOBAL_ZONEID;
4717 				return (zoneid);
4718 			}
4719 		}
4720 		mutex_exit(&ill->ill_lock);
4721 	}
4722 
4723 	/* If we already did the ptp case, then we are done */
4724 	if (ptp) {
4725 		rw_exit(&ipst->ips_ill_g_lock);
4726 		return (ALL_ZONES);
4727 	}
4728 	ptp = B_TRUE;
4729 	goto repeat;
4730 }
4731 
4732 /*
4733  * Look for an ipif that matches the specified remote address i.e. the
4734  * ipif that would receive the specified packet.
4735  * First look for directly connected interfaces and then do a recursive
4736  * IRE lookup and pick the first ipif corresponding to the source address in the
4737  * ire.
4738  * Returns: held ipif
4739  *
4740  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4741  */
4742 ipif_t *
4743 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4744 {
4745 	ipif_t	*ipif;
4746 
4747 	ASSERT(!ill->ill_isv6);
4748 
4749 	/*
4750 	 * Someone could be changing this ipif currently or change it
4751 	 * after we return this. Thus  a few packets could use the old
4752 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4753 	 * will atomically be updated or cleaned up with the new value
4754 	 * Thus we don't need a lock to check the flags or other attrs below.
4755 	 */
4756 	mutex_enter(&ill->ill_lock);
4757 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4758 		if (IPIF_IS_CONDEMNED(ipif))
4759 			continue;
4760 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4761 		    ipif->ipif_zoneid != ALL_ZONES)
4762 			continue;
4763 		/* Allow the ipif to be down */
4764 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4765 			if ((ipif->ipif_pp_dst_addr == addr) ||
4766 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4767 			    ipif->ipif_lcl_addr == addr)) {
4768 				ipif_refhold_locked(ipif);
4769 				mutex_exit(&ill->ill_lock);
4770 				return (ipif);
4771 			}
4772 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4773 			ipif_refhold_locked(ipif);
4774 			mutex_exit(&ill->ill_lock);
4775 			return (ipif);
4776 		}
4777 	}
4778 	mutex_exit(&ill->ill_lock);
4779 	/*
4780 	 * For a remote destination it isn't possible to nail down a particular
4781 	 * ipif.
4782 	 */
4783 
4784 	/* Pick the first interface */
4785 	ipif = ipif_get_next_ipif(NULL, ill);
4786 	return (ipif);
4787 }
4788 
4789 /*
4790  * This func does not prevent refcnt from increasing. But if
4791  * the caller has taken steps to that effect, then this func
4792  * can be used to determine whether the ill has become quiescent
4793  */
4794 static boolean_t
4795 ill_is_quiescent(ill_t *ill)
4796 {
4797 	ipif_t	*ipif;
4798 
4799 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4800 
4801 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4802 		if (ipif->ipif_refcnt != 0)
4803 			return (B_FALSE);
4804 	}
4805 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4806 		return (B_FALSE);
4807 	}
4808 	return (B_TRUE);
4809 }
4810 
4811 boolean_t
4812 ill_is_freeable(ill_t *ill)
4813 {
4814 	ipif_t	*ipif;
4815 
4816 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4817 
4818 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4819 		if (ipif->ipif_refcnt != 0) {
4820 			return (B_FALSE);
4821 		}
4822 	}
4823 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4824 		return (B_FALSE);
4825 	}
4826 	return (B_TRUE);
4827 }
4828 
4829 /*
4830  * This func does not prevent refcnt from increasing. But if
4831  * the caller has taken steps to that effect, then this func
4832  * can be used to determine whether the ipif has become quiescent
4833  */
4834 static boolean_t
4835 ipif_is_quiescent(ipif_t *ipif)
4836 {
4837 	ill_t *ill;
4838 
4839 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4840 
4841 	if (ipif->ipif_refcnt != 0)
4842 		return (B_FALSE);
4843 
4844 	ill = ipif->ipif_ill;
4845 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4846 	    ill->ill_logical_down) {
4847 		return (B_TRUE);
4848 	}
4849 
4850 	/* This is the last ipif going down or being deleted on this ill */
4851 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4852 		return (B_FALSE);
4853 	}
4854 
4855 	return (B_TRUE);
4856 }
4857 
4858 /*
4859  * return true if the ipif can be destroyed: the ipif has to be quiescent
4860  * with zero references from ire/ilm to it.
4861  */
4862 static boolean_t
4863 ipif_is_freeable(ipif_t *ipif)
4864 {
4865 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4866 	ASSERT(ipif->ipif_id != 0);
4867 	return (ipif->ipif_refcnt == 0);
4868 }
4869 
4870 /*
4871  * The ipif/ill/ire has been refreled. Do the tail processing.
4872  * Determine if the ipif or ill in question has become quiescent and if so
4873  * wakeup close and/or restart any queued pending ioctl that is waiting
4874  * for the ipif_down (or ill_down)
4875  */
4876 void
4877 ipif_ill_refrele_tail(ill_t *ill)
4878 {
4879 	mblk_t	*mp;
4880 	conn_t	*connp;
4881 	ipsq_t	*ipsq;
4882 	ipxop_t	*ipx;
4883 	ipif_t	*ipif;
4884 	dl_notify_ind_t *dlindp;
4885 
4886 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4887 
4888 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4889 		/* ip_modclose() may be waiting */
4890 		cv_broadcast(&ill->ill_cv);
4891 	}
4892 
4893 	ipsq = ill->ill_phyint->phyint_ipsq;
4894 	mutex_enter(&ipsq->ipsq_lock);
4895 	ipx = ipsq->ipsq_xop;
4896 	mutex_enter(&ipx->ipx_lock);
4897 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4898 		goto unlock;
4899 
4900 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4901 
4902 	ipif = ipx->ipx_pending_ipif;
4903 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4904 		goto unlock;
4905 
4906 	switch (ipx->ipx_waitfor) {
4907 	case IPIF_DOWN:
4908 		if (!ipif_is_quiescent(ipif))
4909 			goto unlock;
4910 		break;
4911 	case IPIF_FREE:
4912 		if (!ipif_is_freeable(ipif))
4913 			goto unlock;
4914 		break;
4915 	case ILL_DOWN:
4916 		if (!ill_is_quiescent(ill))
4917 			goto unlock;
4918 		break;
4919 	case ILL_FREE:
4920 		/*
4921 		 * ILL_FREE is only for loopback; normal ill teardown waits
4922 		 * synchronously in ip_modclose() without using ipx_waitfor,
4923 		 * handled by the cv_broadcast() at the top of this function.
4924 		 */
4925 		if (!ill_is_freeable(ill))
4926 			goto unlock;
4927 		break;
4928 	default:
4929 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4930 		    (void *)ipsq, ipx->ipx_waitfor);
4931 	}
4932 
4933 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4934 	mutex_exit(&ipx->ipx_lock);
4935 	mp = ipsq_pending_mp_get(ipsq, &connp);
4936 	mutex_exit(&ipsq->ipsq_lock);
4937 	mutex_exit(&ill->ill_lock);
4938 
4939 	ASSERT(mp != NULL);
4940 	/*
4941 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4942 	 * we can only get here when the current operation decides it
4943 	 * it needs to quiesce via ipsq_pending_mp_add().
4944 	 */
4945 	switch (mp->b_datap->db_type) {
4946 	case M_PCPROTO:
4947 	case M_PROTO:
4948 		/*
4949 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4950 		 */
4951 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4952 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4953 
4954 		switch (dlindp->dl_notification) {
4955 		case DL_NOTE_PHYS_ADDR:
4956 			qwriter_ip(ill, ill->ill_rq, mp,
4957 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
4958 			return;
4959 		case DL_NOTE_REPLUMB:
4960 			qwriter_ip(ill, ill->ill_rq, mp,
4961 			    ill_replumb_tail, CUR_OP, B_TRUE);
4962 			return;
4963 		default:
4964 			ASSERT(0);
4965 			ill_refrele(ill);
4966 		}
4967 		break;
4968 
4969 	case M_ERROR:
4970 	case M_HANGUP:
4971 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
4972 		    B_TRUE);
4973 		return;
4974 
4975 	case M_IOCTL:
4976 	case M_IOCDATA:
4977 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
4978 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
4979 		return;
4980 
4981 	default:
4982 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
4983 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
4984 	}
4985 	return;
4986 unlock:
4987 	mutex_exit(&ipsq->ipsq_lock);
4988 	mutex_exit(&ipx->ipx_lock);
4989 	mutex_exit(&ill->ill_lock);
4990 }
4991 
4992 #ifdef DEBUG
4993 /* Reuse trace buffer from beginning (if reached the end) and record trace */
4994 static void
4995 th_trace_rrecord(th_trace_t *th_trace)
4996 {
4997 	tr_buf_t *tr_buf;
4998 	uint_t lastref;
4999 
5000 	lastref = th_trace->th_trace_lastref;
5001 	lastref++;
5002 	if (lastref == TR_BUF_MAX)
5003 		lastref = 0;
5004 	th_trace->th_trace_lastref = lastref;
5005 	tr_buf = &th_trace->th_trbuf[lastref];
5006 	tr_buf->tr_time = ddi_get_lbolt();
5007 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5008 }
5009 
5010 static void
5011 th_trace_free(void *value)
5012 {
5013 	th_trace_t *th_trace = value;
5014 
5015 	ASSERT(th_trace->th_refcnt == 0);
5016 	kmem_free(th_trace, sizeof (*th_trace));
5017 }
5018 
5019 /*
5020  * Find or create the per-thread hash table used to track object references.
5021  * The ipst argument is NULL if we shouldn't allocate.
5022  *
5023  * Accesses per-thread data, so there's no need to lock here.
5024  */
5025 static mod_hash_t *
5026 th_trace_gethash(ip_stack_t *ipst)
5027 {
5028 	th_hash_t *thh;
5029 
5030 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5031 		mod_hash_t *mh;
5032 		char name[256];
5033 		size_t objsize, rshift;
5034 		int retv;
5035 
5036 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5037 			return (NULL);
5038 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5039 		    (void *)curthread);
5040 
5041 		/*
5042 		 * We use mod_hash_create_extended here rather than the more
5043 		 * obvious mod_hash_create_ptrhash because the latter has a
5044 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5045 		 * block.
5046 		 */
5047 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5048 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5049 		rshift = highbit(objsize);
5050 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5051 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5052 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5053 		if (mh == NULL) {
5054 			kmem_free(thh, sizeof (*thh));
5055 			return (NULL);
5056 		}
5057 		thh->thh_hash = mh;
5058 		thh->thh_ipst = ipst;
5059 		/*
5060 		 * We trace ills, ipifs, ires, and nces.  All of these are
5061 		 * per-IP-stack, so the lock on the thread list is as well.
5062 		 */
5063 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5064 		list_insert_tail(&ip_thread_list, thh);
5065 		rw_exit(&ip_thread_rwlock);
5066 		retv = tsd_set(ip_thread_data, thh);
5067 		ASSERT(retv == 0);
5068 	}
5069 	return (thh != NULL ? thh->thh_hash : NULL);
5070 }
5071 
5072 boolean_t
5073 th_trace_ref(const void *obj, ip_stack_t *ipst)
5074 {
5075 	th_trace_t *th_trace;
5076 	mod_hash_t *mh;
5077 	mod_hash_val_t val;
5078 
5079 	if ((mh = th_trace_gethash(ipst)) == NULL)
5080 		return (B_FALSE);
5081 
5082 	/*
5083 	 * Attempt to locate the trace buffer for this obj and thread.
5084 	 * If it does not exist, then allocate a new trace buffer and
5085 	 * insert into the hash.
5086 	 */
5087 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5088 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5089 		if (th_trace == NULL)
5090 			return (B_FALSE);
5091 
5092 		th_trace->th_id = curthread;
5093 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5094 		    (mod_hash_val_t)th_trace) != 0) {
5095 			kmem_free(th_trace, sizeof (th_trace_t));
5096 			return (B_FALSE);
5097 		}
5098 	} else {
5099 		th_trace = (th_trace_t *)val;
5100 	}
5101 
5102 	ASSERT(th_trace->th_refcnt >= 0 &&
5103 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5104 
5105 	th_trace->th_refcnt++;
5106 	th_trace_rrecord(th_trace);
5107 	return (B_TRUE);
5108 }
5109 
5110 /*
5111  * For the purpose of tracing a reference release, we assume that global
5112  * tracing is always on and that the same thread initiated the reference hold
5113  * is releasing.
5114  */
5115 void
5116 th_trace_unref(const void *obj)
5117 {
5118 	int retv;
5119 	mod_hash_t *mh;
5120 	th_trace_t *th_trace;
5121 	mod_hash_val_t val;
5122 
5123 	mh = th_trace_gethash(NULL);
5124 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5125 	ASSERT(retv == 0);
5126 	th_trace = (th_trace_t *)val;
5127 
5128 	ASSERT(th_trace->th_refcnt > 0);
5129 	th_trace->th_refcnt--;
5130 	th_trace_rrecord(th_trace);
5131 }
5132 
5133 /*
5134  * If tracing has been disabled, then we assume that the reference counts are
5135  * now useless, and we clear them out before destroying the entries.
5136  */
5137 void
5138 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5139 {
5140 	th_hash_t	*thh;
5141 	mod_hash_t	*mh;
5142 	mod_hash_val_t	val;
5143 	th_trace_t	*th_trace;
5144 	int		retv;
5145 
5146 	rw_enter(&ip_thread_rwlock, RW_READER);
5147 	for (thh = list_head(&ip_thread_list); thh != NULL;
5148 	    thh = list_next(&ip_thread_list, thh)) {
5149 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5150 		    &val) == 0) {
5151 			th_trace = (th_trace_t *)val;
5152 			if (trace_disable)
5153 				th_trace->th_refcnt = 0;
5154 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5155 			ASSERT(retv == 0);
5156 		}
5157 	}
5158 	rw_exit(&ip_thread_rwlock);
5159 }
5160 
5161 void
5162 ipif_trace_ref(ipif_t *ipif)
5163 {
5164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5165 
5166 	if (ipif->ipif_trace_disable)
5167 		return;
5168 
5169 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5170 		ipif->ipif_trace_disable = B_TRUE;
5171 		ipif_trace_cleanup(ipif);
5172 	}
5173 }
5174 
5175 void
5176 ipif_untrace_ref(ipif_t *ipif)
5177 {
5178 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5179 
5180 	if (!ipif->ipif_trace_disable)
5181 		th_trace_unref(ipif);
5182 }
5183 
5184 void
5185 ill_trace_ref(ill_t *ill)
5186 {
5187 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5188 
5189 	if (ill->ill_trace_disable)
5190 		return;
5191 
5192 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5193 		ill->ill_trace_disable = B_TRUE;
5194 		ill_trace_cleanup(ill);
5195 	}
5196 }
5197 
5198 void
5199 ill_untrace_ref(ill_t *ill)
5200 {
5201 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5202 
5203 	if (!ill->ill_trace_disable)
5204 		th_trace_unref(ill);
5205 }
5206 
5207 /*
5208  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5209  * failure, ipif_trace_disable is set.
5210  */
5211 static void
5212 ipif_trace_cleanup(const ipif_t *ipif)
5213 {
5214 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5215 }
5216 
5217 /*
5218  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5219  * failure, ill_trace_disable is set.
5220  */
5221 static void
5222 ill_trace_cleanup(const ill_t *ill)
5223 {
5224 	th_trace_cleanup(ill, ill->ill_trace_disable);
5225 }
5226 #endif /* DEBUG */
5227 
5228 void
5229 ipif_refhold_locked(ipif_t *ipif)
5230 {
5231 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5232 	ipif->ipif_refcnt++;
5233 	IPIF_TRACE_REF(ipif);
5234 }
5235 
5236 void
5237 ipif_refhold(ipif_t *ipif)
5238 {
5239 	ill_t	*ill;
5240 
5241 	ill = ipif->ipif_ill;
5242 	mutex_enter(&ill->ill_lock);
5243 	ipif->ipif_refcnt++;
5244 	IPIF_TRACE_REF(ipif);
5245 	mutex_exit(&ill->ill_lock);
5246 }
5247 
5248 /*
5249  * Must not be called while holding any locks. Otherwise if this is
5250  * the last reference to be released there is a chance of recursive mutex
5251  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5252  * to restart an ioctl.
5253  */
5254 void
5255 ipif_refrele(ipif_t *ipif)
5256 {
5257 	ill_t	*ill;
5258 
5259 	ill = ipif->ipif_ill;
5260 
5261 	mutex_enter(&ill->ill_lock);
5262 	ASSERT(ipif->ipif_refcnt != 0);
5263 	ipif->ipif_refcnt--;
5264 	IPIF_UNTRACE_REF(ipif);
5265 	if (ipif->ipif_refcnt != 0) {
5266 		mutex_exit(&ill->ill_lock);
5267 		return;
5268 	}
5269 
5270 	/* Drops the ill_lock */
5271 	ipif_ill_refrele_tail(ill);
5272 }
5273 
5274 ipif_t *
5275 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5276 {
5277 	ipif_t	*ipif;
5278 
5279 	mutex_enter(&ill->ill_lock);
5280 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5281 	    ipif != NULL; ipif = ipif->ipif_next) {
5282 		if (IPIF_IS_CONDEMNED(ipif))
5283 			continue;
5284 		ipif_refhold_locked(ipif);
5285 		mutex_exit(&ill->ill_lock);
5286 		return (ipif);
5287 	}
5288 	mutex_exit(&ill->ill_lock);
5289 	return (NULL);
5290 }
5291 
5292 /*
5293  * TODO: make this table extendible at run time
5294  * Return a pointer to the mac type info for 'mac_type'
5295  */
5296 static ip_m_t *
5297 ip_m_lookup(t_uscalar_t mac_type)
5298 {
5299 	ip_m_t	*ipm;
5300 
5301 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5302 		if (ipm->ip_m_mac_type == mac_type)
5303 			return (ipm);
5304 	return (NULL);
5305 }
5306 
5307 /*
5308  * Make a link layer address from the multicast IP address *addr.
5309  * To form the link layer address, invoke the ip_m_v*mapping function
5310  * associated with the link-layer type.
5311  */
5312 void
5313 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5314 {
5315 	ip_m_t *ipm;
5316 
5317 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5318 		return;
5319 
5320 	ASSERT(addr != NULL);
5321 
5322 	ipm = ip_m_lookup(ill->ill_mactype);
5323 	if (ipm == NULL ||
5324 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5325 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5326 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5327 		    ill->ill_name, ill->ill_mactype));
5328 		return;
5329 	}
5330 	if (ill->ill_isv6)
5331 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5332 	else
5333 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5334 }
5335 
5336 /*
5337  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5338  * Otherwise returns B_TRUE.
5339  *
5340  * The netmask can be verified to be contiguous with 32 shifts and or
5341  * operations. Take the contiguous mask (in host byte order) and compute
5342  * 	mask | mask << 1 | mask << 2 | ... | mask << 31
5343  * the result will be the same as the 'mask' for contiguous mask.
5344  */
5345 static boolean_t
5346 ip_contiguous_mask(uint32_t mask)
5347 {
5348 	uint32_t	m = mask;
5349 	int		i;
5350 
5351 	for (i = 1; i < 32; i++)
5352 		m |= (mask << i);
5353 
5354 	return (m == mask);
5355 }
5356 
5357 /*
5358  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5359  * ill is passed in to associate it with the correct interface.
5360  * If ire_arg is set, then we return the held IRE in that location.
5361  */
5362 int
5363 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5364     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5365     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5366 {
5367 	ire_t	*ire, *nire;
5368 	ire_t	*gw_ire = NULL;
5369 	ipif_t	*ipif = NULL;
5370 	uint_t	type;
5371 	int	match_flags = MATCH_IRE_TYPE;
5372 	tsol_gc_t *gc = NULL;
5373 	tsol_gcgrp_t *gcgrp = NULL;
5374 	boolean_t gcgrp_xtraref = B_FALSE;
5375 	boolean_t cgtp_broadcast;
5376 	boolean_t unbound = B_FALSE;
5377 
5378 	ip1dbg(("ip_rt_add:"));
5379 
5380 	if (ire_arg != NULL)
5381 		*ire_arg = NULL;
5382 
5383 	/* disallow non-contiguous netmasks */
5384 	if (!ip_contiguous_mask(ntohl(mask)))
5385 		return (ENOTSUP);
5386 
5387 	/*
5388 	 * If this is the case of RTF_HOST being set, then we set the netmask
5389 	 * to all ones (regardless if one was supplied).
5390 	 */
5391 	if (flags & RTF_HOST)
5392 		mask = IP_HOST_MASK;
5393 
5394 	/*
5395 	 * Prevent routes with a zero gateway from being created (since
5396 	 * interfaces can currently be plumbed and brought up no assigned
5397 	 * address).
5398 	 */
5399 	if (gw_addr == 0)
5400 		return (ENETUNREACH);
5401 	/*
5402 	 * Get the ipif, if any, corresponding to the gw_addr
5403 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5404 	 * we match on the gatway and destination to handle unnumbered pt-pt
5405 	 * interfaces.
5406 	 */
5407 	if (ill != NULL)
5408 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5409 	else
5410 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5411 	if (ipif != NULL) {
5412 		if (IS_VNI(ipif->ipif_ill)) {
5413 			ipif_refrele(ipif);
5414 			return (EINVAL);
5415 		}
5416 	}
5417 
5418 	/*
5419 	 * GateD will attempt to create routes with a loopback interface
5420 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5421 	 * these routes to be added, but create them as interface routes
5422 	 * since the gateway is an interface address.
5423 	 */
5424 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5425 		flags &= ~RTF_GATEWAY;
5426 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5427 		    mask == IP_HOST_MASK) {
5428 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5429 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5430 			    NULL);
5431 			if (ire != NULL) {
5432 				ire_refrele(ire);
5433 				ipif_refrele(ipif);
5434 				return (EEXIST);
5435 			}
5436 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5437 			    "for 0x%x\n", (void *)ipif,
5438 			    ipif->ipif_ire_type,
5439 			    ntohl(ipif->ipif_lcl_addr)));
5440 			ire = ire_create(
5441 			    (uchar_t *)&dst_addr,	/* dest address */
5442 			    (uchar_t *)&mask,		/* mask */
5443 			    NULL,			/* no gateway */
5444 			    ipif->ipif_ire_type,	/* LOOPBACK */
5445 			    ipif->ipif_ill,
5446 			    zoneid,
5447 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5448 			    NULL,
5449 			    ipst);
5450 
5451 			if (ire == NULL) {
5452 				ipif_refrele(ipif);
5453 				return (ENOMEM);
5454 			}
5455 			/* src address assigned by the caller? */
5456 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5457 				ire->ire_setsrc_addr = src_addr;
5458 
5459 			nire = ire_add(ire);
5460 			if (nire == NULL) {
5461 				/*
5462 				 * In the result of failure, ire_add() will have
5463 				 * already deleted the ire in question, so there
5464 				 * is no need to do that here.
5465 				 */
5466 				ipif_refrele(ipif);
5467 				return (ENOMEM);
5468 			}
5469 			/*
5470 			 * Check if it was a duplicate entry. This handles
5471 			 * the case of two racing route adds for the same route
5472 			 */
5473 			if (nire != ire) {
5474 				ASSERT(nire->ire_identical_ref > 1);
5475 				ire_delete(nire);
5476 				ire_refrele(nire);
5477 				ipif_refrele(ipif);
5478 				return (EEXIST);
5479 			}
5480 			ire = nire;
5481 			goto save_ire;
5482 		}
5483 	}
5484 
5485 	/*
5486 	 * The routes for multicast with CGTP are quite special in that
5487 	 * the gateway is the local interface address, yet RTF_GATEWAY
5488 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5489 	 * this undocumented and unusual use of multicast routes.
5490 	 */
5491 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5492 		flags &= ~RTF_GATEWAY;
5493 
5494 	/*
5495 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5496 	 * and the gateway address provided is one of the system's interface
5497 	 * addresses.  By using the routing socket interface and supplying an
5498 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5499 	 * specifying an interface route to be created is available which uses
5500 	 * the interface index that specifies the outgoing interface rather than
5501 	 * the address of an outgoing interface (which may not be able to
5502 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5503 	 * flag, routes can be specified which not only specify the next-hop to
5504 	 * be used when routing to a certain prefix, but also which outgoing
5505 	 * interface should be used.
5506 	 *
5507 	 * Previously, interfaces would have unique addresses assigned to them
5508 	 * and so the address assigned to a particular interface could be used
5509 	 * to identify a particular interface.  One exception to this was the
5510 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5511 	 *
5512 	 * With the advent of IPv6 and its link-local addresses, this
5513 	 * restriction was relaxed and interfaces could share addresses between
5514 	 * themselves.  In fact, typically all of the link-local interfaces on
5515 	 * an IPv6 node or router will have the same link-local address.  In
5516 	 * order to differentiate between these interfaces, the use of an
5517 	 * interface index is necessary and this index can be carried inside a
5518 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5519 	 * of using the interface index, however, is that all of the ipif's that
5520 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5521 	 * cannot be used to differentiate between ipif's (or logical
5522 	 * interfaces) that belong to the same ill (physical interface).
5523 	 *
5524 	 * For example, in the following case involving IPv4 interfaces and
5525 	 * logical interfaces
5526 	 *
5527 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5528 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5529 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5530 	 *
5531 	 * the ipif's corresponding to each of these interface routes can be
5532 	 * uniquely identified by the "gateway" (actually interface address).
5533 	 *
5534 	 * In this case involving multiple IPv6 default routes to a particular
5535 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5536 	 * default route is of interest:
5537 	 *
5538 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5539 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5540 	 */
5541 
5542 	/* RTF_GATEWAY not set */
5543 	if (!(flags & RTF_GATEWAY)) {
5544 		if (sp != NULL) {
5545 			ip2dbg(("ip_rt_add: gateway security attributes "
5546 			    "cannot be set with interface route\n"));
5547 			if (ipif != NULL)
5548 				ipif_refrele(ipif);
5549 			return (EINVAL);
5550 		}
5551 
5552 		/*
5553 		 * Whether or not ill (RTA_IFP) is set, we require that
5554 		 * the gateway is one of our local addresses.
5555 		 */
5556 		if (ipif == NULL)
5557 			return (ENETUNREACH);
5558 
5559 		/*
5560 		 * We use MATCH_IRE_ILL here. If the caller specified an
5561 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5562 		 * we use the ill derived from the gateway address.
5563 		 * We can always match the gateway address since we record it
5564 		 * in ire_gateway_addr.
5565 		 * We don't allow RTA_IFP to specify a different ill than the
5566 		 * one matching the ipif to make sure we can delete the route.
5567 		 */
5568 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5569 		if (ill == NULL) {
5570 			ill = ipif->ipif_ill;
5571 		} else if (ill != ipif->ipif_ill) {
5572 			ipif_refrele(ipif);
5573 			return (EINVAL);
5574 		}
5575 
5576 		/*
5577 		 * We check for an existing entry at this point.
5578 		 *
5579 		 * Since a netmask isn't passed in via the ioctl interface
5580 		 * (SIOCADDRT), we don't check for a matching netmask in that
5581 		 * case.
5582 		 */
5583 		if (!ioctl_msg)
5584 			match_flags |= MATCH_IRE_MASK;
5585 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5586 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5587 		    NULL);
5588 		if (ire != NULL) {
5589 			ire_refrele(ire);
5590 			ipif_refrele(ipif);
5591 			return (EEXIST);
5592 		}
5593 
5594 		/*
5595 		 * Some software (for example, GateD and Sun Cluster) attempts
5596 		 * to create (what amount to) IRE_PREFIX routes with the
5597 		 * loopback address as the gateway.  This is primarily done to
5598 		 * set up prefixes with the RTF_REJECT flag set (for example,
5599 		 * when generating aggregate routes.)
5600 		 *
5601 		 * If the IRE type (as defined by ill->ill_net_type) would be
5602 		 * IRE_LOOPBACK, then we map the request into a
5603 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5604 		 * these interface routes, by definition, can only be that.
5605 		 *
5606 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5607 		 * routine, but rather using ire_create() directly.
5608 		 *
5609 		 */
5610 		type = ill->ill_net_type;
5611 		if (type == IRE_LOOPBACK) {
5612 			type = IRE_IF_NORESOLVER;
5613 			flags |= RTF_BLACKHOLE;
5614 		}
5615 
5616 		/*
5617 		 * Create a copy of the IRE_IF_NORESOLVER or
5618 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5619 		 * gateway.
5620 		 */
5621 		ire = ire_create(
5622 		    (uchar_t *)&dst_addr,
5623 		    (uint8_t *)&mask,
5624 		    (uint8_t *)&gw_addr,
5625 		    type,
5626 		    ill,
5627 		    zoneid,
5628 		    flags,
5629 		    NULL,
5630 		    ipst);
5631 		if (ire == NULL) {
5632 			ipif_refrele(ipif);
5633 			return (ENOMEM);
5634 		}
5635 
5636 		/* src address assigned by the caller? */
5637 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5638 			ire->ire_setsrc_addr = src_addr;
5639 
5640 		nire = ire_add(ire);
5641 		if (nire == NULL) {
5642 			/*
5643 			 * In the result of failure, ire_add() will have
5644 			 * already deleted the ire in question, so there
5645 			 * is no need to do that here.
5646 			 */
5647 			ipif_refrele(ipif);
5648 			return (ENOMEM);
5649 		}
5650 		/*
5651 		 * Check if it was a duplicate entry. This handles
5652 		 * the case of two racing route adds for the same route
5653 		 */
5654 		if (nire != ire) {
5655 			ire_delete(nire);
5656 			ire_refrele(nire);
5657 			ipif_refrele(ipif);
5658 			return (EEXIST);
5659 		}
5660 		ire = nire;
5661 		goto save_ire;
5662 	}
5663 
5664 	/*
5665 	 * Get an interface IRE for the specified gateway.
5666 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5667 	 * gateway, it is currently unreachable and we fail the request
5668 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5669 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5670 	 * If RTA_IFP was specified we look on that particular ill.
5671 	 */
5672 	if (ill != NULL)
5673 		match_flags |= MATCH_IRE_ILL;
5674 
5675 	/* Check whether the gateway is reachable. */
5676 again:
5677 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5678 	if (flags & RTF_INDIRECT)
5679 		type |= IRE_OFFLINK;
5680 
5681 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5682 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5683 	if (gw_ire == NULL) {
5684 		/*
5685 		 * With IPMP, we allow host routes to influence in.mpathd's
5686 		 * target selection.  However, if the test addresses are on
5687 		 * their own network, the above lookup will fail since the
5688 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5689 		 * hidden test IREs to be found and try again.
5690 		 */
5691 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5692 			match_flags |= MATCH_IRE_TESTHIDDEN;
5693 			goto again;
5694 		}
5695 		if (ipif != NULL)
5696 			ipif_refrele(ipif);
5697 		return (ENETUNREACH);
5698 	}
5699 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5700 		ire_refrele(gw_ire);
5701 		if (ipif != NULL)
5702 			ipif_refrele(ipif);
5703 		return (ENETUNREACH);
5704 	}
5705 
5706 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5707 		unbound = B_TRUE;
5708 		if (ipst->ips_ip_strict_src_multihoming > 0)
5709 			ill = gw_ire->ire_ill;
5710 	}
5711 
5712 	/*
5713 	 * We create one of three types of IREs as a result of this request
5714 	 * based on the netmask.  A netmask of all ones (which is automatically
5715 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5716 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5717 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5718 	 * destination prefix.
5719 	 */
5720 	if (mask == IP_HOST_MASK)
5721 		type = IRE_HOST;
5722 	else if (mask == 0)
5723 		type = IRE_DEFAULT;
5724 	else
5725 		type = IRE_PREFIX;
5726 
5727 	/* check for a duplicate entry */
5728 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5729 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5730 	    0, ipst, NULL);
5731 	if (ire != NULL) {
5732 		if (ipif != NULL)
5733 			ipif_refrele(ipif);
5734 		ire_refrele(gw_ire);
5735 		ire_refrele(ire);
5736 		return (EEXIST);
5737 	}
5738 
5739 	/* Security attribute exists */
5740 	if (sp != NULL) {
5741 		tsol_gcgrp_addr_t ga;
5742 
5743 		/* find or create the gateway credentials group */
5744 		ga.ga_af = AF_INET;
5745 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5746 
5747 		/* we hold reference to it upon success */
5748 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5749 		if (gcgrp == NULL) {
5750 			if (ipif != NULL)
5751 				ipif_refrele(ipif);
5752 			ire_refrele(gw_ire);
5753 			return (ENOMEM);
5754 		}
5755 
5756 		/*
5757 		 * Create and add the security attribute to the group; a
5758 		 * reference to the group is made upon allocating a new
5759 		 * entry successfully.  If it finds an already-existing
5760 		 * entry for the security attribute in the group, it simply
5761 		 * returns it and no new reference is made to the group.
5762 		 */
5763 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5764 		if (gc == NULL) {
5765 			if (ipif != NULL)
5766 				ipif_refrele(ipif);
5767 			/* release reference held by gcgrp_lookup */
5768 			GCGRP_REFRELE(gcgrp);
5769 			ire_refrele(gw_ire);
5770 			return (ENOMEM);
5771 		}
5772 	}
5773 
5774 	/* Create the IRE. */
5775 	ire = ire_create(
5776 	    (uchar_t *)&dst_addr,		/* dest address */
5777 	    (uchar_t *)&mask,			/* mask */
5778 	    (uchar_t *)&gw_addr,		/* gateway address */
5779 	    (ushort_t)type,			/* IRE type */
5780 	    ill,
5781 	    zoneid,
5782 	    flags,
5783 	    gc,					/* security attribute */
5784 	    ipst);
5785 
5786 	/*
5787 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5788 	 * reference to the 'gcgrp'. We can now release the extra reference
5789 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5790 	 */
5791 	if (gcgrp_xtraref)
5792 		GCGRP_REFRELE(gcgrp);
5793 	if (ire == NULL) {
5794 		if (gc != NULL)
5795 			GC_REFRELE(gc);
5796 		if (ipif != NULL)
5797 			ipif_refrele(ipif);
5798 		ire_refrele(gw_ire);
5799 		return (ENOMEM);
5800 	}
5801 
5802 	/* Before we add, check if an extra CGTP broadcast is needed */
5803 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5804 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5805 
5806 	/* src address assigned by the caller? */
5807 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5808 		ire->ire_setsrc_addr = src_addr;
5809 
5810 	ire->ire_unbound = unbound;
5811 
5812 	/*
5813 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5814 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5815 	 */
5816 
5817 	/* Add the new IRE. */
5818 	nire = ire_add(ire);
5819 	if (nire == NULL) {
5820 		/*
5821 		 * In the result of failure, ire_add() will have
5822 		 * already deleted the ire in question, so there
5823 		 * is no need to do that here.
5824 		 */
5825 		if (ipif != NULL)
5826 			ipif_refrele(ipif);
5827 		ire_refrele(gw_ire);
5828 		return (ENOMEM);
5829 	}
5830 	/*
5831 	 * Check if it was a duplicate entry. This handles
5832 	 * the case of two racing route adds for the same route
5833 	 */
5834 	if (nire != ire) {
5835 		ire_delete(nire);
5836 		ire_refrele(nire);
5837 		if (ipif != NULL)
5838 			ipif_refrele(ipif);
5839 		ire_refrele(gw_ire);
5840 		return (EEXIST);
5841 	}
5842 	ire = nire;
5843 
5844 	if (flags & RTF_MULTIRT) {
5845 		/*
5846 		 * Invoke the CGTP (multirouting) filtering module
5847 		 * to add the dst address in the filtering database.
5848 		 * Replicated inbound packets coming from that address
5849 		 * will be filtered to discard the duplicates.
5850 		 * It is not necessary to call the CGTP filter hook
5851 		 * when the dst address is a broadcast or multicast,
5852 		 * because an IP source address cannot be a broadcast
5853 		 * or a multicast.
5854 		 */
5855 		if (cgtp_broadcast) {
5856 			ip_cgtp_bcast_add(ire, ipst);
5857 			goto save_ire;
5858 		}
5859 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5860 		    !CLASSD(ire->ire_addr)) {
5861 			int res;
5862 			ipif_t *src_ipif;
5863 
5864 			/* Find the source address corresponding to gw_ire */
5865 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5866 			    NULL, zoneid, ipst);
5867 			if (src_ipif != NULL) {
5868 				res = ipst->ips_ip_cgtp_filter_ops->
5869 				    cfo_add_dest_v4(
5870 				    ipst->ips_netstack->netstack_stackid,
5871 				    ire->ire_addr,
5872 				    ire->ire_gateway_addr,
5873 				    ire->ire_setsrc_addr,
5874 				    src_ipif->ipif_lcl_addr);
5875 				ipif_refrele(src_ipif);
5876 			} else {
5877 				res = EADDRNOTAVAIL;
5878 			}
5879 			if (res != 0) {
5880 				if (ipif != NULL)
5881 					ipif_refrele(ipif);
5882 				ire_refrele(gw_ire);
5883 				ire_delete(ire);
5884 				ire_refrele(ire);	/* Held in ire_add */
5885 				return (res);
5886 			}
5887 		}
5888 	}
5889 
5890 save_ire:
5891 	if (gw_ire != NULL) {
5892 		ire_refrele(gw_ire);
5893 		gw_ire = NULL;
5894 	}
5895 	if (ill != NULL) {
5896 		/*
5897 		 * Save enough information so that we can recreate the IRE if
5898 		 * the interface goes down and then up.  The metrics associated
5899 		 * with the route will be saved as well when rts_setmetrics() is
5900 		 * called after the IRE has been created.  In the case where
5901 		 * memory cannot be allocated, none of this information will be
5902 		 * saved.
5903 		 */
5904 		ill_save_ire(ill, ire);
5905 	}
5906 	if (ioctl_msg)
5907 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5908 	if (ire_arg != NULL) {
5909 		/*
5910 		 * Store the ire that was successfully added into where ire_arg
5911 		 * points to so that callers don't have to look it up
5912 		 * themselves (but they are responsible for ire_refrele()ing
5913 		 * the ire when they are finished with it).
5914 		 */
5915 		*ire_arg = ire;
5916 	} else {
5917 		ire_refrele(ire);		/* Held in ire_add */
5918 	}
5919 	if (ipif != NULL)
5920 		ipif_refrele(ipif);
5921 	return (0);
5922 }
5923 
5924 /*
5925  * ip_rt_delete is called to delete an IPv4 route.
5926  * ill is passed in to associate it with the correct interface.
5927  */
5928 /* ARGSUSED4 */
5929 int
5930 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5931     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5932     ip_stack_t *ipst, zoneid_t zoneid)
5933 {
5934 	ire_t	*ire = NULL;
5935 	ipif_t	*ipif;
5936 	uint_t	type;
5937 	uint_t	match_flags = MATCH_IRE_TYPE;
5938 	int	err = 0;
5939 
5940 	ip1dbg(("ip_rt_delete:"));
5941 	/*
5942 	 * If this is the case of RTF_HOST being set, then we set the netmask
5943 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5944 	 */
5945 	if (flags & RTF_HOST) {
5946 		mask = IP_HOST_MASK;
5947 		match_flags |= MATCH_IRE_MASK;
5948 	} else if (rtm_addrs & RTA_NETMASK) {
5949 		match_flags |= MATCH_IRE_MASK;
5950 	}
5951 
5952 	/*
5953 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5954 	 * we check if the gateway address is one of our interfaces first,
5955 	 * and fall back on RTF_GATEWAY routes.
5956 	 *
5957 	 * This makes it possible to delete an original
5958 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5959 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5960 	 * and those can not be deleted here.
5961 	 *
5962 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5963 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5964 	 * otherwise we use the ill derived from the gateway address.
5965 	 * We can always match the gateway address since we record it
5966 	 * in ire_gateway_addr.
5967 	 *
5968 	 * For more detail on specifying routes by gateway address and by
5969 	 * interface index, see the comments in ip_rt_add().
5970 	 */
5971 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5972 	if (ipif != NULL) {
5973 		ill_t	*ill_match;
5974 
5975 		if (ill != NULL)
5976 			ill_match = ill;
5977 		else
5978 			ill_match = ipif->ipif_ill;
5979 
5980 		match_flags |= MATCH_IRE_ILL;
5981 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5982 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
5983 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
5984 			    match_flags, 0, ipst, NULL);
5985 		}
5986 		if (ire == NULL) {
5987 			match_flags |= MATCH_IRE_GW;
5988 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5989 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5990 			    match_flags, 0, ipst, NULL);
5991 		}
5992 		/* Avoid deleting routes created by kernel from an ipif */
5993 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5994 			ire_refrele(ire);
5995 			ire = NULL;
5996 		}
5997 
5998 		/* Restore in case we didn't find a match */
5999 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6000 	}
6001 
6002 	if (ire == NULL) {
6003 		/*
6004 		 * At this point, the gateway address is not one of our own
6005 		 * addresses or a matching interface route was not found.  We
6006 		 * set the IRE type to lookup based on whether
6007 		 * this is a host route, a default route or just a prefix.
6008 		 *
6009 		 * If an ill was passed in, then the lookup is based on an
6010 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6011 		 */
6012 		match_flags |= MATCH_IRE_GW;
6013 		if (ill != NULL)
6014 			match_flags |= MATCH_IRE_ILL;
6015 		if (mask == IP_HOST_MASK)
6016 			type = IRE_HOST;
6017 		else if (mask == 0)
6018 			type = IRE_DEFAULT;
6019 		else
6020 			type = IRE_PREFIX;
6021 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6022 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6023 	}
6024 
6025 	if (ipif != NULL) {
6026 		ipif_refrele(ipif);
6027 		ipif = NULL;
6028 	}
6029 
6030 	if (ire == NULL)
6031 		return (ESRCH);
6032 
6033 	if (ire->ire_flags & RTF_MULTIRT) {
6034 		/*
6035 		 * Invoke the CGTP (multirouting) filtering module
6036 		 * to remove the dst address from the filtering database.
6037 		 * Packets coming from that address will no longer be
6038 		 * filtered to remove duplicates.
6039 		 */
6040 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6041 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6042 			    ipst->ips_netstack->netstack_stackid,
6043 			    ire->ire_addr, ire->ire_gateway_addr);
6044 		}
6045 		ip_cgtp_bcast_delete(ire, ipst);
6046 	}
6047 
6048 	ill = ire->ire_ill;
6049 	if (ill != NULL)
6050 		ill_remove_saved_ire(ill, ire);
6051 	if (ioctl_msg)
6052 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6053 	ire_delete(ire);
6054 	ire_refrele(ire);
6055 	return (err);
6056 }
6057 
6058 /*
6059  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6060  */
6061 /* ARGSUSED */
6062 int
6063 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6064     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6065 {
6066 	ipaddr_t dst_addr;
6067 	ipaddr_t gw_addr;
6068 	ipaddr_t mask;
6069 	int error = 0;
6070 	mblk_t *mp1;
6071 	struct rtentry *rt;
6072 	ipif_t *ipif = NULL;
6073 	ip_stack_t	*ipst;
6074 
6075 	ASSERT(q->q_next == NULL);
6076 	ipst = CONNQ_TO_IPST(q);
6077 
6078 	ip1dbg(("ip_siocaddrt:"));
6079 	/* Existence of mp1 verified in ip_wput_nondata */
6080 	mp1 = mp->b_cont->b_cont;
6081 	rt = (struct rtentry *)mp1->b_rptr;
6082 
6083 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6084 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6085 
6086 	/*
6087 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6088 	 * to a particular host address.  In this case, we set the netmask to
6089 	 * all ones for the particular destination address.  Otherwise,
6090 	 * determine the netmask to be used based on dst_addr and the interfaces
6091 	 * in use.
6092 	 */
6093 	if (rt->rt_flags & RTF_HOST) {
6094 		mask = IP_HOST_MASK;
6095 	} else {
6096 		/*
6097 		 * Note that ip_subnet_mask returns a zero mask in the case of
6098 		 * default (an all-zeroes address).
6099 		 */
6100 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6101 	}
6102 
6103 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6104 	    B_TRUE, NULL, ipst, ALL_ZONES);
6105 	if (ipif != NULL)
6106 		ipif_refrele(ipif);
6107 	return (error);
6108 }
6109 
6110 /*
6111  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6112  */
6113 /* ARGSUSED */
6114 int
6115 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6116     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6117 {
6118 	ipaddr_t dst_addr;
6119 	ipaddr_t gw_addr;
6120 	ipaddr_t mask;
6121 	int error;
6122 	mblk_t *mp1;
6123 	struct rtentry *rt;
6124 	ipif_t *ipif = NULL;
6125 	ip_stack_t	*ipst;
6126 
6127 	ASSERT(q->q_next == NULL);
6128 	ipst = CONNQ_TO_IPST(q);
6129 
6130 	ip1dbg(("ip_siocdelrt:"));
6131 	/* Existence of mp1 verified in ip_wput_nondata */
6132 	mp1 = mp->b_cont->b_cont;
6133 	rt = (struct rtentry *)mp1->b_rptr;
6134 
6135 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6136 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6137 
6138 	/*
6139 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6140 	 * to a particular host address.  In this case, we set the netmask to
6141 	 * all ones for the particular destination address.  Otherwise,
6142 	 * determine the netmask to be used based on dst_addr and the interfaces
6143 	 * in use.
6144 	 */
6145 	if (rt->rt_flags & RTF_HOST) {
6146 		mask = IP_HOST_MASK;
6147 	} else {
6148 		/*
6149 		 * Note that ip_subnet_mask returns a zero mask in the case of
6150 		 * default (an all-zeroes address).
6151 		 */
6152 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6153 	}
6154 
6155 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6156 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6157 	    ipst, ALL_ZONES);
6158 	if (ipif != NULL)
6159 		ipif_refrele(ipif);
6160 	return (error);
6161 }
6162 
6163 /*
6164  * Enqueue the mp onto the ipsq, chained by b_next.
6165  * b_prev stores the function to be executed later, and b_queue the queue
6166  * where this mp originated.
6167  */
6168 void
6169 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6170     ill_t *pending_ill)
6171 {
6172 	conn_t	*connp;
6173 	ipxop_t *ipx = ipsq->ipsq_xop;
6174 
6175 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6176 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6177 	ASSERT(func != NULL);
6178 
6179 	mp->b_queue = q;
6180 	mp->b_prev = (void *)func;
6181 	mp->b_next = NULL;
6182 
6183 	switch (type) {
6184 	case CUR_OP:
6185 		if (ipx->ipx_mptail != NULL) {
6186 			ASSERT(ipx->ipx_mphead != NULL);
6187 			ipx->ipx_mptail->b_next = mp;
6188 		} else {
6189 			ASSERT(ipx->ipx_mphead == NULL);
6190 			ipx->ipx_mphead = mp;
6191 		}
6192 		ipx->ipx_mptail = mp;
6193 		break;
6194 
6195 	case NEW_OP:
6196 		if (ipsq->ipsq_xopq_mptail != NULL) {
6197 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6198 			ipsq->ipsq_xopq_mptail->b_next = mp;
6199 		} else {
6200 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6201 			ipsq->ipsq_xopq_mphead = mp;
6202 		}
6203 		ipsq->ipsq_xopq_mptail = mp;
6204 		ipx->ipx_ipsq_queued = B_TRUE;
6205 		break;
6206 
6207 	case SWITCH_OP:
6208 		ASSERT(ipsq->ipsq_swxop != NULL);
6209 		/* only one switch operation is currently allowed */
6210 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6211 		ipsq->ipsq_switch_mp = mp;
6212 		ipx->ipx_ipsq_queued = B_TRUE;
6213 		break;
6214 	default:
6215 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6216 	}
6217 
6218 	if (CONN_Q(q) && pending_ill != NULL) {
6219 		connp = Q_TO_CONN(q);
6220 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6221 		connp->conn_oper_pending_ill = pending_ill;
6222 	}
6223 }
6224 
6225 /*
6226  * Dequeue the next message that requested exclusive access to this IPSQ's
6227  * xop.  Specifically:
6228  *
6229  *  1. If we're still processing the current operation on `ipsq', then
6230  *     dequeue the next message for the operation (from ipx_mphead), or
6231  *     return NULL if there are no queued messages for the operation.
6232  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6233  *
6234  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6235  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6236  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6237  *     leaving IPMP groups and require a careful dance -- see the comments
6238  *     in-line below for details.  If we're leaving a group xop or if we're
6239  *     joining a group xop and become writer on it, then we proceed to (3).
6240  *     Otherwise, we return NULL and exit the xop.
6241  *
6242  *  3. For each IPSQ in the xop, return any switch operation stored on
6243  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6244  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6245  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6246  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6247  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6248  *     each phyint in the group, including the IPMP meta-interface phyint.
6249  */
6250 static mblk_t *
6251 ipsq_dq(ipsq_t *ipsq)
6252 {
6253 	ill_t	*illv4, *illv6;
6254 	mblk_t	*mp;
6255 	ipsq_t	*xopipsq;
6256 	ipsq_t	*leftipsq = NULL;
6257 	ipxop_t *ipx;
6258 	phyint_t *phyi = ipsq->ipsq_phyint;
6259 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6260 	boolean_t emptied = B_FALSE;
6261 
6262 	/*
6263 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6264 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6265 	 */
6266 	rw_enter(&ipst->ips_ill_g_lock,
6267 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6268 	mutex_enter(&ipsq->ipsq_lock);
6269 	ipx = ipsq->ipsq_xop;
6270 	mutex_enter(&ipx->ipx_lock);
6271 
6272 	/*
6273 	 * Dequeue the next message associated with the current exclusive
6274 	 * operation, if any.
6275 	 */
6276 	if ((mp = ipx->ipx_mphead) != NULL) {
6277 		ipx->ipx_mphead = mp->b_next;
6278 		if (ipx->ipx_mphead == NULL)
6279 			ipx->ipx_mptail = NULL;
6280 		mp->b_next = (void *)ipsq;
6281 		goto out;
6282 	}
6283 
6284 	if (ipx->ipx_current_ipif != NULL)
6285 		goto empty;
6286 
6287 	if (ipsq->ipsq_swxop != NULL) {
6288 		/*
6289 		 * The exclusive operation that is now being completed has
6290 		 * requested a switch to a different xop.  This happens
6291 		 * when an interface joins or leaves an IPMP group.  Joins
6292 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6293 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6294 		 * (phyint_free()), or interface plumb for an ill type
6295 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6296 		 *
6297 		 * Xop switches are not allowed on the IPMP meta-interface.
6298 		 */
6299 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6300 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6301 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6302 
6303 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6304 			/*
6305 			 * We're switching back to our own xop, so we have two
6306 			 * xop's to drain/exit: our own, and the group xop
6307 			 * that we are leaving.
6308 			 *
6309 			 * First, pull ourselves out of the group ipsq list.
6310 			 * This is safe since we're writer on ill_g_lock.
6311 			 */
6312 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6313 
6314 			xopipsq = ipx->ipx_ipsq;
6315 			while (xopipsq->ipsq_next != ipsq)
6316 				xopipsq = xopipsq->ipsq_next;
6317 
6318 			xopipsq->ipsq_next = ipsq->ipsq_next;
6319 			ipsq->ipsq_next = ipsq;
6320 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6321 			ipsq->ipsq_swxop = NULL;
6322 
6323 			/*
6324 			 * Second, prepare to exit the group xop.  The actual
6325 			 * ipsq_exit() is done at the end of this function
6326 			 * since we cannot hold any locks across ipsq_exit().
6327 			 * Note that although we drop the group's ipx_lock, no
6328 			 * threads can proceed since we're still ipx_writer.
6329 			 */
6330 			leftipsq = xopipsq;
6331 			mutex_exit(&ipx->ipx_lock);
6332 
6333 			/*
6334 			 * Third, set ipx to point to our own xop (which was
6335 			 * inactive and therefore can be entered).
6336 			 */
6337 			ipx = ipsq->ipsq_xop;
6338 			mutex_enter(&ipx->ipx_lock);
6339 			ASSERT(ipx->ipx_writer == NULL);
6340 			ASSERT(ipx->ipx_current_ipif == NULL);
6341 		} else {
6342 			/*
6343 			 * We're switching from our own xop to a group xop.
6344 			 * The requestor of the switch must ensure that the
6345 			 * group xop cannot go away (e.g. by ensuring the
6346 			 * phyint associated with the xop cannot go away).
6347 			 *
6348 			 * If we can become writer on our new xop, then we'll
6349 			 * do the drain.  Otherwise, the current writer of our
6350 			 * new xop will do the drain when it exits.
6351 			 *
6352 			 * First, splice ourselves into the group IPSQ list.
6353 			 * This is safe since we're writer on ill_g_lock.
6354 			 */
6355 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6356 
6357 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6358 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6359 				xopipsq = xopipsq->ipsq_next;
6360 
6361 			xopipsq->ipsq_next = ipsq;
6362 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6363 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6364 			ipsq->ipsq_swxop = NULL;
6365 
6366 			/*
6367 			 * Second, exit our own xop, since it's now unused.
6368 			 * This is safe since we've got the only reference.
6369 			 */
6370 			ASSERT(ipx->ipx_writer == curthread);
6371 			ipx->ipx_writer = NULL;
6372 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6373 			ipx->ipx_ipsq_queued = B_FALSE;
6374 			mutex_exit(&ipx->ipx_lock);
6375 
6376 			/*
6377 			 * Third, set ipx to point to our new xop, and check
6378 			 * if we can become writer on it.  If we cannot, then
6379 			 * the current writer will drain the IPSQ group when
6380 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6381 			 * because we're still holding ipsq_lock.
6382 			 */
6383 			ipx = ipsq->ipsq_xop;
6384 			mutex_enter(&ipx->ipx_lock);
6385 			if (ipx->ipx_writer != NULL ||
6386 			    ipx->ipx_current_ipif != NULL) {
6387 				goto out;
6388 			}
6389 		}
6390 
6391 		/*
6392 		 * Fourth, become writer on our new ipx before we continue
6393 		 * with the drain.  Note that we never dropped ipsq_lock
6394 		 * above, so no other thread could've raced with us to
6395 		 * become writer first.  Also, we're holding ipx_lock, so
6396 		 * no other thread can examine the ipx right now.
6397 		 */
6398 		ASSERT(ipx->ipx_current_ipif == NULL);
6399 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6400 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6401 		ipx->ipx_writer = curthread;
6402 		ipx->ipx_forced = B_FALSE;
6403 #ifdef DEBUG
6404 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6405 #endif
6406 	}
6407 
6408 	xopipsq = ipsq;
6409 	do {
6410 		/*
6411 		 * So that other operations operate on a consistent and
6412 		 * complete phyint, a switch message on an IPSQ must be
6413 		 * handled prior to any other operations on that IPSQ.
6414 		 */
6415 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6416 			xopipsq->ipsq_switch_mp = NULL;
6417 			ASSERT(mp->b_next == NULL);
6418 			mp->b_next = (void *)xopipsq;
6419 			goto out;
6420 		}
6421 
6422 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6423 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6424 			if (xopipsq->ipsq_xopq_mphead == NULL)
6425 				xopipsq->ipsq_xopq_mptail = NULL;
6426 			mp->b_next = (void *)xopipsq;
6427 			goto out;
6428 		}
6429 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6430 empty:
6431 	/*
6432 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6433 	 * new messages can end up on any IPSQ in the xop.
6434 	 */
6435 	ipx->ipx_writer = NULL;
6436 	ipx->ipx_forced = B_FALSE;
6437 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6438 	ipx->ipx_ipsq_queued = B_FALSE;
6439 	emptied = B_TRUE;
6440 #ifdef	DEBUG
6441 	ipx->ipx_depth = 0;
6442 #endif
6443 out:
6444 	mutex_exit(&ipx->ipx_lock);
6445 	mutex_exit(&ipsq->ipsq_lock);
6446 
6447 	/*
6448 	 * If we completely emptied the xop, then wake up any threads waiting
6449 	 * to enter any of the IPSQ's associated with it.
6450 	 */
6451 	if (emptied) {
6452 		xopipsq = ipsq;
6453 		do {
6454 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6455 				continue;
6456 
6457 			illv4 = phyi->phyint_illv4;
6458 			illv6 = phyi->phyint_illv6;
6459 
6460 			GRAB_ILL_LOCKS(illv4, illv6);
6461 			if (illv4 != NULL)
6462 				cv_broadcast(&illv4->ill_cv);
6463 			if (illv6 != NULL)
6464 				cv_broadcast(&illv6->ill_cv);
6465 			RELEASE_ILL_LOCKS(illv4, illv6);
6466 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6467 	}
6468 	rw_exit(&ipst->ips_ill_g_lock);
6469 
6470 	/*
6471 	 * Now that all locks are dropped, exit the IPSQ we left.
6472 	 */
6473 	if (leftipsq != NULL)
6474 		ipsq_exit(leftipsq);
6475 
6476 	return (mp);
6477 }
6478 
6479 /*
6480  * Return completion status of previously initiated DLPI operations on
6481  * ills in the purview of an ipsq.
6482  */
6483 static boolean_t
6484 ipsq_dlpi_done(ipsq_t *ipsq)
6485 {
6486 	ipsq_t		*ipsq_start;
6487 	phyint_t	*phyi;
6488 	ill_t		*ill;
6489 
6490 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6491 	ipsq_start = ipsq;
6492 
6493 	do {
6494 		/*
6495 		 * The only current users of this function are ipsq_try_enter
6496 		 * and ipsq_enter which have made sure that ipsq_writer is
6497 		 * NULL before we reach here. ill_dlpi_pending is modified
6498 		 * only by an ipsq writer
6499 		 */
6500 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6501 		phyi = ipsq->ipsq_phyint;
6502 		/*
6503 		 * phyi could be NULL if a phyint that is part of an
6504 		 * IPMP group is being unplumbed. A more detailed
6505 		 * comment is in ipmp_grp_update_kstats()
6506 		 */
6507 		if (phyi != NULL) {
6508 			ill = phyi->phyint_illv4;
6509 			if (ill != NULL &&
6510 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6511 			    ill->ill_arl_dlpi_pending))
6512 				return (B_FALSE);
6513 
6514 			ill = phyi->phyint_illv6;
6515 			if (ill != NULL &&
6516 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6517 				return (B_FALSE);
6518 		}
6519 
6520 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6521 
6522 	return (B_TRUE);
6523 }
6524 
6525 /*
6526  * Enter the ipsq corresponding to ill, by waiting synchronously till
6527  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6528  * will have to drain completely before ipsq_enter returns success.
6529  * ipx_current_ipif will be set if some exclusive op is in progress,
6530  * and the ipsq_exit logic will start the next enqueued op after
6531  * completion of the current op. If 'force' is used, we don't wait
6532  * for the enqueued ops. This is needed when a conn_close wants to
6533  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6534  * of an ill can also use this option. But we dont' use it currently.
6535  */
6536 #define	ENTER_SQ_WAIT_TICKS 100
6537 boolean_t
6538 ipsq_enter(ill_t *ill, boolean_t force, int type)
6539 {
6540 	ipsq_t	*ipsq;
6541 	ipxop_t *ipx;
6542 	boolean_t waited_enough = B_FALSE;
6543 	ip_stack_t *ipst = ill->ill_ipst;
6544 
6545 	/*
6546 	 * Note that the relationship between ill and ipsq is fixed as long as
6547 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6548 	 * relationship between the IPSQ and xop cannot change.  However,
6549 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6550 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6551 	 * waking up all ills in the xop when it becomes available.
6552 	 */
6553 	for (;;) {
6554 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6555 		mutex_enter(&ill->ill_lock);
6556 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6557 			mutex_exit(&ill->ill_lock);
6558 			rw_exit(&ipst->ips_ill_g_lock);
6559 			return (B_FALSE);
6560 		}
6561 
6562 		ipsq = ill->ill_phyint->phyint_ipsq;
6563 		mutex_enter(&ipsq->ipsq_lock);
6564 		ipx = ipsq->ipsq_xop;
6565 		mutex_enter(&ipx->ipx_lock);
6566 
6567 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6568 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6569 		    waited_enough))
6570 			break;
6571 
6572 		rw_exit(&ipst->ips_ill_g_lock);
6573 
6574 		if (!force || ipx->ipx_writer != NULL) {
6575 			mutex_exit(&ipx->ipx_lock);
6576 			mutex_exit(&ipsq->ipsq_lock);
6577 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6578 		} else {
6579 			mutex_exit(&ipx->ipx_lock);
6580 			mutex_exit(&ipsq->ipsq_lock);
6581 			(void) cv_reltimedwait(&ill->ill_cv,
6582 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6583 			waited_enough = B_TRUE;
6584 		}
6585 		mutex_exit(&ill->ill_lock);
6586 	}
6587 
6588 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6589 	ASSERT(ipx->ipx_reentry_cnt == 0);
6590 	ipx->ipx_writer = curthread;
6591 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6592 	ipx->ipx_reentry_cnt++;
6593 #ifdef DEBUG
6594 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6595 #endif
6596 	mutex_exit(&ipx->ipx_lock);
6597 	mutex_exit(&ipsq->ipsq_lock);
6598 	mutex_exit(&ill->ill_lock);
6599 	rw_exit(&ipst->ips_ill_g_lock);
6600 
6601 	return (B_TRUE);
6602 }
6603 
6604 /*
6605  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6606  * across the call to the core interface ipsq_try_enter() and hence calls this
6607  * function directly. This is explained more fully in ipif_set_values().
6608  * In order to support the above constraint, ipsq_try_enter is implemented as
6609  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6610  */
6611 static ipsq_t *
6612 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6613     int type, boolean_t reentry_ok)
6614 {
6615 	ipsq_t	*ipsq;
6616 	ipxop_t	*ipx;
6617 	ip_stack_t *ipst = ill->ill_ipst;
6618 
6619 	/*
6620 	 * lock ordering:
6621 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6622 	 *
6623 	 * ipx of an ipsq can't change when ipsq_lock is held.
6624 	 */
6625 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6626 	GRAB_CONN_LOCK(q);
6627 	mutex_enter(&ill->ill_lock);
6628 	ipsq = ill->ill_phyint->phyint_ipsq;
6629 	mutex_enter(&ipsq->ipsq_lock);
6630 	ipx = ipsq->ipsq_xop;
6631 	mutex_enter(&ipx->ipx_lock);
6632 
6633 	/*
6634 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6635 	 *    (Note: If the caller does not specify reentry_ok then neither
6636 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6637 	 *    again. Otherwise it can lead to an infinite loop
6638 	 * 2. Enter the ipsq if there is no current writer and this attempted
6639 	 *    entry is part of the current operation
6640 	 * 3. Enter the ipsq if there is no current writer and this is a new
6641 	 *    operation and the operation queue is empty and there is no
6642 	 *    operation currently in progress and if all previously initiated
6643 	 *    DLPI operations have completed.
6644 	 */
6645 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6646 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6647 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6648 	    ipsq_dlpi_done(ipsq))))) {
6649 		/* Success. */
6650 		ipx->ipx_reentry_cnt++;
6651 		ipx->ipx_writer = curthread;
6652 		ipx->ipx_forced = B_FALSE;
6653 		mutex_exit(&ipx->ipx_lock);
6654 		mutex_exit(&ipsq->ipsq_lock);
6655 		mutex_exit(&ill->ill_lock);
6656 		RELEASE_CONN_LOCK(q);
6657 #ifdef DEBUG
6658 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6659 #endif
6660 		return (ipsq);
6661 	}
6662 
6663 	if (func != NULL)
6664 		ipsq_enq(ipsq, q, mp, func, type, ill);
6665 
6666 	mutex_exit(&ipx->ipx_lock);
6667 	mutex_exit(&ipsq->ipsq_lock);
6668 	mutex_exit(&ill->ill_lock);
6669 	RELEASE_CONN_LOCK(q);
6670 	return (NULL);
6671 }
6672 
6673 /*
6674  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6675  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6676  * There is one ipsq per phyint. The ipsq
6677  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6678  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6679  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6680  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6681  * up the interface) and are enqueued in ipx_mphead.
6682  *
6683  * If a thread does not want to reenter the ipsq when it is already writer,
6684  * it must make sure that the specified reentry point to be called later
6685  * when the ipsq is empty, nor any code path starting from the specified reentry
6686  * point must never ever try to enter the ipsq again. Otherwise it can lead
6687  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6688  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6689  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6690  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6691  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6692  * ioctl if the current ioctl has completed. If the current ioctl is still
6693  * in progress it simply returns. The current ioctl could be waiting for
6694  * a response from another module (the driver or could be waiting for
6695  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6696  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6697  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6698  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6699  * all associated DLPI operations have completed.
6700  */
6701 
6702 /*
6703  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6704  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6705  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6706  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6707  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6708  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6709  */
6710 ipsq_t *
6711 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6712     ipsq_func_t func, int type, boolean_t reentry_ok)
6713 {
6714 	ip_stack_t	*ipst;
6715 	ipsq_t		*ipsq;
6716 
6717 	/* Only 1 of ipif or ill can be specified */
6718 	ASSERT((ipif != NULL) ^ (ill != NULL));
6719 
6720 	if (ipif != NULL)
6721 		ill = ipif->ipif_ill;
6722 	ipst = ill->ill_ipst;
6723 
6724 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6725 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6726 	rw_exit(&ipst->ips_ill_g_lock);
6727 
6728 	return (ipsq);
6729 }
6730 
6731 /*
6732  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6733  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6734  * cannot be entered, the mp is queued for completion.
6735  */
6736 void
6737 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6738     boolean_t reentry_ok)
6739 {
6740 	ipsq_t	*ipsq;
6741 
6742 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6743 
6744 	/*
6745 	 * Drop the caller's refhold on the ill.  This is safe since we either
6746 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6747 	 * IPSQ, in which case we return without accessing ill anymore.  This
6748 	 * is needed because func needs to see the correct refcount.
6749 	 * e.g. removeif can work only then.
6750 	 */
6751 	ill_refrele(ill);
6752 	if (ipsq != NULL) {
6753 		(*func)(ipsq, q, mp, NULL);
6754 		ipsq_exit(ipsq);
6755 	}
6756 }
6757 
6758 /*
6759  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6760  * prior to exiting.  Caller must be writer on the specified IPSQ.
6761  */
6762 void
6763 ipsq_exit(ipsq_t *ipsq)
6764 {
6765 	mblk_t *mp;
6766 	ipsq_t *mp_ipsq;
6767 	queue_t	*q;
6768 	phyint_t *phyi;
6769 	ipsq_func_t func;
6770 
6771 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6772 
6773 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6774 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6775 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6776 		return;
6777 	}
6778 
6779 	for (;;) {
6780 		phyi = ipsq->ipsq_phyint;
6781 		mp = ipsq_dq(ipsq);
6782 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6783 
6784 		/*
6785 		 * If we've changed to a new IPSQ, and the phyint associated
6786 		 * with the old one has gone away, free the old IPSQ.  Note
6787 		 * that this cannot happen while the IPSQ is in a group.
6788 		 */
6789 		if (mp_ipsq != ipsq && phyi == NULL) {
6790 			ASSERT(ipsq->ipsq_next == ipsq);
6791 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6792 			ipsq_delete(ipsq);
6793 		}
6794 
6795 		if (mp == NULL)
6796 			break;
6797 
6798 		q = mp->b_queue;
6799 		func = (ipsq_func_t)mp->b_prev;
6800 		ipsq = mp_ipsq;
6801 		mp->b_next = mp->b_prev = NULL;
6802 		mp->b_queue = NULL;
6803 
6804 		/*
6805 		 * If 'q' is an conn queue, it is valid, since we did a
6806 		 * a refhold on the conn at the start of the ioctl.
6807 		 * If 'q' is an ill queue, it is valid, since close of an
6808 		 * ill will clean up its IPSQ.
6809 		 */
6810 		(*func)(ipsq, q, mp, NULL);
6811 	}
6812 }
6813 
6814 /*
6815  * Used to start any igmp or mld timers that could not be started
6816  * while holding ill_mcast_lock. The timers can't be started while holding
6817  * the lock, since mld/igmp_start_timers may need to call untimeout()
6818  * which can't be done while holding the lock which the timeout handler
6819  * acquires. Otherwise
6820  * there could be a deadlock since the timeout handlers
6821  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6822  * ill_mcast_lock.
6823  */
6824 void
6825 ill_mcast_timer_start(ip_stack_t *ipst)
6826 {
6827 	int		next;
6828 
6829 	mutex_enter(&ipst->ips_igmp_timer_lock);
6830 	next = ipst->ips_igmp_deferred_next;
6831 	ipst->ips_igmp_deferred_next = INFINITY;
6832 	mutex_exit(&ipst->ips_igmp_timer_lock);
6833 
6834 	if (next != INFINITY)
6835 		igmp_start_timers(next, ipst);
6836 
6837 	mutex_enter(&ipst->ips_mld_timer_lock);
6838 	next = ipst->ips_mld_deferred_next;
6839 	ipst->ips_mld_deferred_next = INFINITY;
6840 	mutex_exit(&ipst->ips_mld_timer_lock);
6841 
6842 	if (next != INFINITY)
6843 		mld_start_timers(next, ipst);
6844 }
6845 
6846 /*
6847  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6848  * and `ioccmd'.
6849  */
6850 void
6851 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6852 {
6853 	ill_t *ill = ipif->ipif_ill;
6854 	ipxop_t *ipx = ipsq->ipsq_xop;
6855 
6856 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6857 	ASSERT(ipx->ipx_current_ipif == NULL);
6858 	ASSERT(ipx->ipx_current_ioctl == 0);
6859 
6860 	ipx->ipx_current_done = B_FALSE;
6861 	ipx->ipx_current_ioctl = ioccmd;
6862 	mutex_enter(&ipx->ipx_lock);
6863 	ipx->ipx_current_ipif = ipif;
6864 	mutex_exit(&ipx->ipx_lock);
6865 
6866 	/*
6867 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6868 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6869 	 * references to the ipif (so that the references will eventually
6870 	 * drop to zero) and also prevents any "get" operations (e.g.,
6871 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6872 	 * operation has completed and the ipif is again in a stable state.
6873 	 *
6874 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6875 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6876 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6877 	 * ipifs will be affected.
6878 	 *
6879 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6880 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6881 	 * operate on.
6882 	 */
6883 	switch (ioccmd) {
6884 	case SIOCLIFREMOVEIF:
6885 		break;
6886 	case 0:
6887 		mutex_enter(&ill->ill_lock);
6888 		ipif = ipif->ipif_ill->ill_ipif;
6889 		for (; ipif != NULL; ipif = ipif->ipif_next)
6890 			ipif->ipif_state_flags |= IPIF_CHANGING;
6891 		mutex_exit(&ill->ill_lock);
6892 		break;
6893 	default:
6894 		mutex_enter(&ill->ill_lock);
6895 		ipif->ipif_state_flags |= IPIF_CHANGING;
6896 		mutex_exit(&ill->ill_lock);
6897 	}
6898 }
6899 
6900 /*
6901  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6902  * the next exclusive operation to begin once we ipsq_exit().  However, if
6903  * pending DLPI operations remain, then we will wait for the queue to drain
6904  * before allowing the next exclusive operation to begin.  This ensures that
6905  * DLPI operations from one exclusive operation are never improperly processed
6906  * as part of a subsequent exclusive operation.
6907  */
6908 void
6909 ipsq_current_finish(ipsq_t *ipsq)
6910 {
6911 	ipxop_t	*ipx = ipsq->ipsq_xop;
6912 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6913 	ipif_t	*ipif = ipx->ipx_current_ipif;
6914 
6915 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6916 
6917 	/*
6918 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6919 	 * (but in that case, IPIF_CHANGING will already be clear and no
6920 	 * pending DLPI messages can remain).
6921 	 */
6922 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6923 		ill_t *ill = ipif->ipif_ill;
6924 
6925 		mutex_enter(&ill->ill_lock);
6926 		dlpi_pending = ill->ill_dlpi_pending;
6927 		if (ipx->ipx_current_ioctl == 0) {
6928 			ipif = ill->ill_ipif;
6929 			for (; ipif != NULL; ipif = ipif->ipif_next)
6930 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6931 		} else {
6932 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6933 		}
6934 		mutex_exit(&ill->ill_lock);
6935 	}
6936 
6937 	ASSERT(!ipx->ipx_current_done);
6938 	ipx->ipx_current_done = B_TRUE;
6939 	ipx->ipx_current_ioctl = 0;
6940 	if (dlpi_pending == DL_PRIM_INVAL) {
6941 		mutex_enter(&ipx->ipx_lock);
6942 		ipx->ipx_current_ipif = NULL;
6943 		mutex_exit(&ipx->ipx_lock);
6944 	}
6945 }
6946 
6947 /*
6948  * The ill is closing. Flush all messages on the ipsq that originated
6949  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6950  * for this ill since ipsq_enter could not have entered until then.
6951  * New messages can't be queued since the CONDEMNED flag is set.
6952  */
6953 static void
6954 ipsq_flush(ill_t *ill)
6955 {
6956 	queue_t	*q;
6957 	mblk_t	*prev;
6958 	mblk_t	*mp;
6959 	mblk_t	*mp_next;
6960 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6961 
6962 	ASSERT(IAM_WRITER_ILL(ill));
6963 
6964 	/*
6965 	 * Flush any messages sent up by the driver.
6966 	 */
6967 	mutex_enter(&ipx->ipx_lock);
6968 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6969 		mp_next = mp->b_next;
6970 		q = mp->b_queue;
6971 		if (q == ill->ill_rq || q == ill->ill_wq) {
6972 			/* dequeue mp */
6973 			if (prev == NULL)
6974 				ipx->ipx_mphead = mp->b_next;
6975 			else
6976 				prev->b_next = mp->b_next;
6977 			if (ipx->ipx_mptail == mp) {
6978 				ASSERT(mp_next == NULL);
6979 				ipx->ipx_mptail = prev;
6980 			}
6981 			inet_freemsg(mp);
6982 		} else {
6983 			prev = mp;
6984 		}
6985 	}
6986 	mutex_exit(&ipx->ipx_lock);
6987 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6988 	ipsq_xopq_mp_cleanup(ill, NULL);
6989 }
6990 
6991 /*
6992  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6993  * and return the associated ipif.
6994  * Return value:
6995  *	Non zero: An error has occurred. ci may not be filled out.
6996  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
6997  *	a held ipif in ci.ci_ipif.
6998  */
6999 int
7000 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7001     cmd_info_t *ci)
7002 {
7003 	char		*name;
7004 	struct ifreq    *ifr;
7005 	struct lifreq    *lifr;
7006 	ipif_t		*ipif = NULL;
7007 	ill_t		*ill;
7008 	conn_t		*connp;
7009 	boolean_t	isv6;
7010 	int		err;
7011 	mblk_t		*mp1;
7012 	zoneid_t	zoneid;
7013 	ip_stack_t	*ipst;
7014 
7015 	if (q->q_next != NULL) {
7016 		ill = (ill_t *)q->q_ptr;
7017 		isv6 = ill->ill_isv6;
7018 		connp = NULL;
7019 		zoneid = ALL_ZONES;
7020 		ipst = ill->ill_ipst;
7021 	} else {
7022 		ill = NULL;
7023 		connp = Q_TO_CONN(q);
7024 		isv6 = (connp->conn_family == AF_INET6);
7025 		zoneid = connp->conn_zoneid;
7026 		if (zoneid == GLOBAL_ZONEID) {
7027 			/* global zone can access ipifs in all zones */
7028 			zoneid = ALL_ZONES;
7029 		}
7030 		ipst = connp->conn_netstack->netstack_ip;
7031 	}
7032 
7033 	/* Has been checked in ip_wput_nondata */
7034 	mp1 = mp->b_cont->b_cont;
7035 
7036 	if (ipip->ipi_cmd_type == IF_CMD) {
7037 		/* This a old style SIOC[GS]IF* command */
7038 		ifr = (struct ifreq *)mp1->b_rptr;
7039 		/*
7040 		 * Null terminate the string to protect against buffer
7041 		 * overrun. String was generated by user code and may not
7042 		 * be trusted.
7043 		 */
7044 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7045 		name = ifr->ifr_name;
7046 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7047 		ci->ci_sin6 = NULL;
7048 		ci->ci_lifr = (struct lifreq *)ifr;
7049 	} else {
7050 		/* This a new style SIOC[GS]LIF* command */
7051 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7052 		lifr = (struct lifreq *)mp1->b_rptr;
7053 		/*
7054 		 * Null terminate the string to protect against buffer
7055 		 * overrun. String was generated by user code and may not
7056 		 * be trusted.
7057 		 */
7058 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7059 		name = lifr->lifr_name;
7060 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7061 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7062 		ci->ci_lifr = lifr;
7063 	}
7064 
7065 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7066 		/*
7067 		 * The ioctl will be failed if the ioctl comes down
7068 		 * an conn stream
7069 		 */
7070 		if (ill == NULL) {
7071 			/*
7072 			 * Not an ill queue, return EINVAL same as the
7073 			 * old error code.
7074 			 */
7075 			return (ENXIO);
7076 		}
7077 		ipif = ill->ill_ipif;
7078 		ipif_refhold(ipif);
7079 	} else {
7080 		/*
7081 		 * Ensure that ioctls don't see any internal state changes
7082 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7083 		 * set.
7084 		 */
7085 		ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7086 		    isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7087 		if (ipif == NULL) {
7088 			if (err == EINPROGRESS)
7089 				return (err);
7090 			err = 0;	/* Ensure we don't use it below */
7091 		}
7092 	}
7093 
7094 	/*
7095 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7096 	 */
7097 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7098 		ipif_refrele(ipif);
7099 		return (ENXIO);
7100 	}
7101 
7102 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7103 	    name[0] == '\0') {
7104 		/*
7105 		 * Handle a or a SIOC?IF* with a null name
7106 		 * during plumb (on the ill queue before the I_PLINK).
7107 		 */
7108 		ipif = ill->ill_ipif;
7109 		ipif_refhold(ipif);
7110 	}
7111 
7112 	if (ipif == NULL)
7113 		return (ENXIO);
7114 
7115 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7116 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7117 
7118 	ci->ci_ipif = ipif;
7119 	return (0);
7120 }
7121 
7122 /*
7123  * Return the total number of ipifs.
7124  */
7125 static uint_t
7126 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7127 {
7128 	uint_t numifs = 0;
7129 	ill_t	*ill;
7130 	ill_walk_context_t	ctx;
7131 	ipif_t	*ipif;
7132 
7133 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7134 	ill = ILL_START_WALK_V4(&ctx, ipst);
7135 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7136 		if (IS_UNDER_IPMP(ill))
7137 			continue;
7138 		for (ipif = ill->ill_ipif; ipif != NULL;
7139 		    ipif = ipif->ipif_next) {
7140 			if (ipif->ipif_zoneid == zoneid ||
7141 			    ipif->ipif_zoneid == ALL_ZONES)
7142 				numifs++;
7143 		}
7144 	}
7145 	rw_exit(&ipst->ips_ill_g_lock);
7146 	return (numifs);
7147 }
7148 
7149 /*
7150  * Return the total number of ipifs.
7151  */
7152 static uint_t
7153 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7154 {
7155 	uint_t numifs = 0;
7156 	ill_t	*ill;
7157 	ipif_t	*ipif;
7158 	ill_walk_context_t	ctx;
7159 
7160 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7161 
7162 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7163 	if (family == AF_INET)
7164 		ill = ILL_START_WALK_V4(&ctx, ipst);
7165 	else if (family == AF_INET6)
7166 		ill = ILL_START_WALK_V6(&ctx, ipst);
7167 	else
7168 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7169 
7170 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7171 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7172 			continue;
7173 
7174 		for (ipif = ill->ill_ipif; ipif != NULL;
7175 		    ipif = ipif->ipif_next) {
7176 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7177 			    !(lifn_flags & LIFC_NOXMIT))
7178 				continue;
7179 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7180 			    !(lifn_flags & LIFC_TEMPORARY))
7181 				continue;
7182 			if (((ipif->ipif_flags &
7183 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7184 			    IPIF_DEPRECATED)) ||
7185 			    IS_LOOPBACK(ill) ||
7186 			    !(ipif->ipif_flags & IPIF_UP)) &&
7187 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7188 				continue;
7189 
7190 			if (zoneid != ipif->ipif_zoneid &&
7191 			    ipif->ipif_zoneid != ALL_ZONES &&
7192 			    (zoneid != GLOBAL_ZONEID ||
7193 			    !(lifn_flags & LIFC_ALLZONES)))
7194 				continue;
7195 
7196 			numifs++;
7197 		}
7198 	}
7199 	rw_exit(&ipst->ips_ill_g_lock);
7200 	return (numifs);
7201 }
7202 
7203 uint_t
7204 ip_get_lifsrcofnum(ill_t *ill)
7205 {
7206 	uint_t numifs = 0;
7207 	ill_t	*ill_head = ill;
7208 	ip_stack_t	*ipst = ill->ill_ipst;
7209 
7210 	/*
7211 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7212 	 * other thread may be trying to relink the ILLs in this usesrc group
7213 	 * and adjusting the ill_usesrc_grp_next pointers
7214 	 */
7215 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7216 	if ((ill->ill_usesrc_ifindex == 0) &&
7217 	    (ill->ill_usesrc_grp_next != NULL)) {
7218 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7219 		    ill = ill->ill_usesrc_grp_next)
7220 			numifs++;
7221 	}
7222 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7223 
7224 	return (numifs);
7225 }
7226 
7227 /* Null values are passed in for ipif, sin, and ifreq */
7228 /* ARGSUSED */
7229 int
7230 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7231     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7232 {
7233 	int *nump;
7234 	conn_t *connp = Q_TO_CONN(q);
7235 
7236 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7237 
7238 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7239 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7240 
7241 	*nump = ip_get_numifs(connp->conn_zoneid,
7242 	    connp->conn_netstack->netstack_ip);
7243 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7244 	return (0);
7245 }
7246 
7247 /* Null values are passed in for ipif, sin, and ifreq */
7248 /* ARGSUSED */
7249 int
7250 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7251     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7252 {
7253 	struct lifnum *lifn;
7254 	mblk_t	*mp1;
7255 	conn_t *connp = Q_TO_CONN(q);
7256 
7257 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7258 
7259 	/* Existence checked in ip_wput_nondata */
7260 	mp1 = mp->b_cont->b_cont;
7261 
7262 	lifn = (struct lifnum *)mp1->b_rptr;
7263 	switch (lifn->lifn_family) {
7264 	case AF_UNSPEC:
7265 	case AF_INET:
7266 	case AF_INET6:
7267 		break;
7268 	default:
7269 		return (EAFNOSUPPORT);
7270 	}
7271 
7272 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7273 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7274 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7275 	return (0);
7276 }
7277 
7278 /* ARGSUSED */
7279 int
7280 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7281     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7282 {
7283 	STRUCT_HANDLE(ifconf, ifc);
7284 	mblk_t *mp1;
7285 	struct iocblk *iocp;
7286 	struct ifreq *ifr;
7287 	ill_walk_context_t	ctx;
7288 	ill_t	*ill;
7289 	ipif_t	*ipif;
7290 	struct sockaddr_in *sin;
7291 	int32_t	ifclen;
7292 	zoneid_t zoneid;
7293 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7294 
7295 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7296 
7297 	ip1dbg(("ip_sioctl_get_ifconf"));
7298 	/* Existence verified in ip_wput_nondata */
7299 	mp1 = mp->b_cont->b_cont;
7300 	iocp = (struct iocblk *)mp->b_rptr;
7301 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7302 
7303 	/*
7304 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7305 	 * the user buffer address and length into which the list of struct
7306 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7307 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7308 	 * the SIOCGIFCONF operation was redefined to simply provide
7309 	 * a large output buffer into which we are supposed to jam the ifreq
7310 	 * array.  The same ioctl command code was used, despite the fact that
7311 	 * both the applications and the kernel code had to change, thus making
7312 	 * it impossible to support both interfaces.
7313 	 *
7314 	 * For reasons not good enough to try to explain, the following
7315 	 * algorithm is used for deciding what to do with one of these:
7316 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7317 	 * form with the output buffer coming down as the continuation message.
7318 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7319 	 * and we have to copy in the ifconf structure to find out how big the
7320 	 * output buffer is and where to copy out to.  Sure no problem...
7321 	 *
7322 	 */
7323 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7324 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7325 		int numifs = 0;
7326 		size_t ifc_bufsize;
7327 
7328 		/*
7329 		 * Must be (better be!) continuation of a TRANSPARENT
7330 		 * IOCTL.  We just copied in the ifconf structure.
7331 		 */
7332 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7333 		    (struct ifconf *)mp1->b_rptr);
7334 
7335 		/*
7336 		 * Allocate a buffer to hold requested information.
7337 		 *
7338 		 * If ifc_len is larger than what is needed, we only
7339 		 * allocate what we will use.
7340 		 *
7341 		 * If ifc_len is smaller than what is needed, return
7342 		 * EINVAL.
7343 		 *
7344 		 * XXX: the ill_t structure can hava 2 counters, for
7345 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7346 		 * number of interfaces for a device, so we don't need
7347 		 * to count them here...
7348 		 */
7349 		numifs = ip_get_numifs(zoneid, ipst);
7350 
7351 		ifclen = STRUCT_FGET(ifc, ifc_len);
7352 		ifc_bufsize = numifs * sizeof (struct ifreq);
7353 		if (ifc_bufsize > ifclen) {
7354 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7355 				/* old behaviour */
7356 				return (EINVAL);
7357 			} else {
7358 				ifc_bufsize = ifclen;
7359 			}
7360 		}
7361 
7362 		mp1 = mi_copyout_alloc(q, mp,
7363 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7364 		if (mp1 == NULL)
7365 			return (ENOMEM);
7366 
7367 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7368 	}
7369 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7370 	/*
7371 	 * the SIOCGIFCONF ioctl only knows about
7372 	 * IPv4 addresses, so don't try to tell
7373 	 * it about interfaces with IPv6-only
7374 	 * addresses. (Last parm 'isv6' is B_FALSE)
7375 	 */
7376 
7377 	ifr = (struct ifreq *)mp1->b_rptr;
7378 
7379 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7380 	ill = ILL_START_WALK_V4(&ctx, ipst);
7381 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7382 		if (IS_UNDER_IPMP(ill))
7383 			continue;
7384 		for (ipif = ill->ill_ipif; ipif != NULL;
7385 		    ipif = ipif->ipif_next) {
7386 			if (zoneid != ipif->ipif_zoneid &&
7387 			    ipif->ipif_zoneid != ALL_ZONES)
7388 				continue;
7389 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7390 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7391 					/* old behaviour */
7392 					rw_exit(&ipst->ips_ill_g_lock);
7393 					return (EINVAL);
7394 				} else {
7395 					goto if_copydone;
7396 				}
7397 			}
7398 			ipif_get_name(ipif, ifr->ifr_name,
7399 			    sizeof (ifr->ifr_name));
7400 			sin = (sin_t *)&ifr->ifr_addr;
7401 			*sin = sin_null;
7402 			sin->sin_family = AF_INET;
7403 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7404 			ifr++;
7405 		}
7406 	}
7407 if_copydone:
7408 	rw_exit(&ipst->ips_ill_g_lock);
7409 	mp1->b_wptr = (uchar_t *)ifr;
7410 
7411 	if (STRUCT_BUF(ifc) != NULL) {
7412 		STRUCT_FSET(ifc, ifc_len,
7413 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7414 	}
7415 	return (0);
7416 }
7417 
7418 /*
7419  * Get the interfaces using the address hosted on the interface passed in,
7420  * as a source adddress
7421  */
7422 /* ARGSUSED */
7423 int
7424 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7425     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7426 {
7427 	mblk_t *mp1;
7428 	ill_t	*ill, *ill_head;
7429 	ipif_t	*ipif, *orig_ipif;
7430 	int	numlifs = 0;
7431 	size_t	lifs_bufsize, lifsmaxlen;
7432 	struct	lifreq *lifr;
7433 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7434 	uint_t	ifindex;
7435 	zoneid_t zoneid;
7436 	boolean_t isv6 = B_FALSE;
7437 	struct	sockaddr_in	*sin;
7438 	struct	sockaddr_in6	*sin6;
7439 	STRUCT_HANDLE(lifsrcof, lifs);
7440 	ip_stack_t		*ipst;
7441 
7442 	ipst = CONNQ_TO_IPST(q);
7443 
7444 	ASSERT(q->q_next == NULL);
7445 
7446 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7447 
7448 	/* Existence verified in ip_wput_nondata */
7449 	mp1 = mp->b_cont->b_cont;
7450 
7451 	/*
7452 	 * Must be (better be!) continuation of a TRANSPARENT
7453 	 * IOCTL.  We just copied in the lifsrcof structure.
7454 	 */
7455 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7456 	    (struct lifsrcof *)mp1->b_rptr);
7457 
7458 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7459 		return (EINVAL);
7460 
7461 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7462 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7463 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7464 	if (ipif == NULL) {
7465 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7466 		    ifindex));
7467 		return (ENXIO);
7468 	}
7469 
7470 	/* Allocate a buffer to hold requested information */
7471 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7472 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7473 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7474 	/* The actual size needed is always returned in lifs_len */
7475 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7476 
7477 	/* If the amount we need is more than what is passed in, abort */
7478 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7479 		ipif_refrele(ipif);
7480 		return (0);
7481 	}
7482 
7483 	mp1 = mi_copyout_alloc(q, mp,
7484 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7485 	if (mp1 == NULL) {
7486 		ipif_refrele(ipif);
7487 		return (ENOMEM);
7488 	}
7489 
7490 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7491 	bzero(mp1->b_rptr, lifs_bufsize);
7492 
7493 	lifr = (struct lifreq *)mp1->b_rptr;
7494 
7495 	ill = ill_head = ipif->ipif_ill;
7496 	orig_ipif = ipif;
7497 
7498 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7499 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7500 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7501 
7502 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7503 	for (; (ill != NULL) && (ill != ill_head);
7504 	    ill = ill->ill_usesrc_grp_next) {
7505 
7506 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7507 			break;
7508 
7509 		ipif = ill->ill_ipif;
7510 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7511 		if (ipif->ipif_isv6) {
7512 			sin6 = (sin6_t *)&lifr->lifr_addr;
7513 			*sin6 = sin6_null;
7514 			sin6->sin6_family = AF_INET6;
7515 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7516 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7517 			    &ipif->ipif_v6net_mask);
7518 		} else {
7519 			sin = (sin_t *)&lifr->lifr_addr;
7520 			*sin = sin_null;
7521 			sin->sin_family = AF_INET;
7522 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7523 			lifr->lifr_addrlen = ip_mask_to_plen(
7524 			    ipif->ipif_net_mask);
7525 		}
7526 		lifr++;
7527 	}
7528 	rw_exit(&ipst->ips_ill_g_lock);
7529 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7530 	ipif_refrele(orig_ipif);
7531 	mp1->b_wptr = (uchar_t *)lifr;
7532 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7533 
7534 	return (0);
7535 }
7536 
7537 /* ARGSUSED */
7538 int
7539 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7540     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7541 {
7542 	mblk_t *mp1;
7543 	int	list;
7544 	ill_t	*ill;
7545 	ipif_t	*ipif;
7546 	int	flags;
7547 	int	numlifs = 0;
7548 	size_t	lifc_bufsize;
7549 	struct	lifreq *lifr;
7550 	sa_family_t	family;
7551 	struct	sockaddr_in	*sin;
7552 	struct	sockaddr_in6	*sin6;
7553 	ill_walk_context_t	ctx;
7554 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7555 	int32_t	lifclen;
7556 	zoneid_t zoneid;
7557 	STRUCT_HANDLE(lifconf, lifc);
7558 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7559 
7560 	ip1dbg(("ip_sioctl_get_lifconf"));
7561 
7562 	ASSERT(q->q_next == NULL);
7563 
7564 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7565 
7566 	/* Existence verified in ip_wput_nondata */
7567 	mp1 = mp->b_cont->b_cont;
7568 
7569 	/*
7570 	 * An extended version of SIOCGIFCONF that takes an
7571 	 * additional address family and flags field.
7572 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7573 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7574 	 * interfaces are omitted.
7575 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7576 	 * unless LIFC_TEMPORARY is specified.
7577 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7578 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7579 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7580 	 * has priority over LIFC_NOXMIT.
7581 	 */
7582 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7583 
7584 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7585 		return (EINVAL);
7586 
7587 	/*
7588 	 * Must be (better be!) continuation of a TRANSPARENT
7589 	 * IOCTL.  We just copied in the lifconf structure.
7590 	 */
7591 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7592 
7593 	family = STRUCT_FGET(lifc, lifc_family);
7594 	flags = STRUCT_FGET(lifc, lifc_flags);
7595 
7596 	switch (family) {
7597 	case AF_UNSPEC:
7598 		/*
7599 		 * walk all ILL's.
7600 		 */
7601 		list = MAX_G_HEADS;
7602 		break;
7603 	case AF_INET:
7604 		/*
7605 		 * walk only IPV4 ILL's.
7606 		 */
7607 		list = IP_V4_G_HEAD;
7608 		break;
7609 	case AF_INET6:
7610 		/*
7611 		 * walk only IPV6 ILL's.
7612 		 */
7613 		list = IP_V6_G_HEAD;
7614 		break;
7615 	default:
7616 		return (EAFNOSUPPORT);
7617 	}
7618 
7619 	/*
7620 	 * Allocate a buffer to hold requested information.
7621 	 *
7622 	 * If lifc_len is larger than what is needed, we only
7623 	 * allocate what we will use.
7624 	 *
7625 	 * If lifc_len is smaller than what is needed, return
7626 	 * EINVAL.
7627 	 */
7628 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7629 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7630 	lifclen = STRUCT_FGET(lifc, lifc_len);
7631 	if (lifc_bufsize > lifclen) {
7632 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7633 			return (EINVAL);
7634 		else
7635 			lifc_bufsize = lifclen;
7636 	}
7637 
7638 	mp1 = mi_copyout_alloc(q, mp,
7639 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7640 	if (mp1 == NULL)
7641 		return (ENOMEM);
7642 
7643 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7644 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7645 
7646 	lifr = (struct lifreq *)mp1->b_rptr;
7647 
7648 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7649 	ill = ill_first(list, list, &ctx, ipst);
7650 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7651 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7652 			continue;
7653 
7654 		for (ipif = ill->ill_ipif; ipif != NULL;
7655 		    ipif = ipif->ipif_next) {
7656 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7657 			    !(flags & LIFC_NOXMIT))
7658 				continue;
7659 
7660 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7661 			    !(flags & LIFC_TEMPORARY))
7662 				continue;
7663 
7664 			if (((ipif->ipif_flags &
7665 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7666 			    IPIF_DEPRECATED)) ||
7667 			    IS_LOOPBACK(ill) ||
7668 			    !(ipif->ipif_flags & IPIF_UP)) &&
7669 			    (flags & LIFC_EXTERNAL_SOURCE))
7670 				continue;
7671 
7672 			if (zoneid != ipif->ipif_zoneid &&
7673 			    ipif->ipif_zoneid != ALL_ZONES &&
7674 			    (zoneid != GLOBAL_ZONEID ||
7675 			    !(flags & LIFC_ALLZONES)))
7676 				continue;
7677 
7678 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7679 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7680 					rw_exit(&ipst->ips_ill_g_lock);
7681 					return (EINVAL);
7682 				} else {
7683 					goto lif_copydone;
7684 				}
7685 			}
7686 
7687 			ipif_get_name(ipif, lifr->lifr_name,
7688 			    sizeof (lifr->lifr_name));
7689 			lifr->lifr_type = ill->ill_type;
7690 			if (ipif->ipif_isv6) {
7691 				sin6 = (sin6_t *)&lifr->lifr_addr;
7692 				*sin6 = sin6_null;
7693 				sin6->sin6_family = AF_INET6;
7694 				sin6->sin6_addr =
7695 				    ipif->ipif_v6lcl_addr;
7696 				lifr->lifr_addrlen =
7697 				    ip_mask_to_plen_v6(
7698 				    &ipif->ipif_v6net_mask);
7699 			} else {
7700 				sin = (sin_t *)&lifr->lifr_addr;
7701 				*sin = sin_null;
7702 				sin->sin_family = AF_INET;
7703 				sin->sin_addr.s_addr =
7704 				    ipif->ipif_lcl_addr;
7705 				lifr->lifr_addrlen =
7706 				    ip_mask_to_plen(
7707 				    ipif->ipif_net_mask);
7708 			}
7709 			lifr++;
7710 		}
7711 	}
7712 lif_copydone:
7713 	rw_exit(&ipst->ips_ill_g_lock);
7714 
7715 	mp1->b_wptr = (uchar_t *)lifr;
7716 	if (STRUCT_BUF(lifc) != NULL) {
7717 		STRUCT_FSET(lifc, lifc_len,
7718 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7719 	}
7720 	return (0);
7721 }
7722 
7723 static void
7724 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7725 {
7726 	ip6_asp_t *table;
7727 	size_t table_size;
7728 	mblk_t *data_mp;
7729 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7730 	ip_stack_t	*ipst;
7731 
7732 	if (q->q_next == NULL)
7733 		ipst = CONNQ_TO_IPST(q);
7734 	else
7735 		ipst = ILLQ_TO_IPST(q);
7736 
7737 	/* These two ioctls are I_STR only */
7738 	if (iocp->ioc_count == TRANSPARENT) {
7739 		miocnak(q, mp, 0, EINVAL);
7740 		return;
7741 	}
7742 
7743 	data_mp = mp->b_cont;
7744 	if (data_mp == NULL) {
7745 		/* The user passed us a NULL argument */
7746 		table = NULL;
7747 		table_size = iocp->ioc_count;
7748 	} else {
7749 		/*
7750 		 * The user provided a table.  The stream head
7751 		 * may have copied in the user data in chunks,
7752 		 * so make sure everything is pulled up
7753 		 * properly.
7754 		 */
7755 		if (MBLKL(data_mp) < iocp->ioc_count) {
7756 			mblk_t *new_data_mp;
7757 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7758 			    NULL) {
7759 				miocnak(q, mp, 0, ENOMEM);
7760 				return;
7761 			}
7762 			freemsg(data_mp);
7763 			data_mp = new_data_mp;
7764 			mp->b_cont = data_mp;
7765 		}
7766 		table = (ip6_asp_t *)data_mp->b_rptr;
7767 		table_size = iocp->ioc_count;
7768 	}
7769 
7770 	switch (iocp->ioc_cmd) {
7771 	case SIOCGIP6ADDRPOLICY:
7772 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7773 		if (iocp->ioc_rval == -1)
7774 			iocp->ioc_error = EINVAL;
7775 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7776 		else if (table != NULL &&
7777 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7778 			ip6_asp_t *src = table;
7779 			ip6_asp32_t *dst = (void *)table;
7780 			int count = table_size / sizeof (ip6_asp_t);
7781 			int i;
7782 
7783 			/*
7784 			 * We need to do an in-place shrink of the array
7785 			 * to match the alignment attributes of the
7786 			 * 32-bit ABI looking at it.
7787 			 */
7788 			/* LINTED: logical expression always true: op "||" */
7789 			ASSERT(sizeof (*src) > sizeof (*dst));
7790 			for (i = 1; i < count; i++)
7791 				bcopy(src + i, dst + i, sizeof (*dst));
7792 		}
7793 #endif
7794 		break;
7795 
7796 	case SIOCSIP6ADDRPOLICY:
7797 		ASSERT(mp->b_prev == NULL);
7798 		mp->b_prev = (void *)q;
7799 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7800 		/*
7801 		 * We pass in the datamodel here so that the ip6_asp_replace()
7802 		 * routine can handle converting from 32-bit to native formats
7803 		 * where necessary.
7804 		 *
7805 		 * A better way to handle this might be to convert the inbound
7806 		 * data structure here, and hang it off a new 'mp'; thus the
7807 		 * ip6_asp_replace() logic would always be dealing with native
7808 		 * format data structures..
7809 		 *
7810 		 * (An even simpler way to handle these ioctls is to just
7811 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7812 		 * and just recompile everything that depends on it.)
7813 		 */
7814 #endif
7815 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7816 		    iocp->ioc_flag & IOC_MODELS);
7817 		return;
7818 	}
7819 
7820 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7821 	qreply(q, mp);
7822 }
7823 
7824 static void
7825 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7826 {
7827 	mblk_t 		*data_mp;
7828 	struct dstinforeq	*dir;
7829 	uint8_t		*end, *cur;
7830 	in6_addr_t	*daddr, *saddr;
7831 	ipaddr_t	v4daddr;
7832 	ire_t		*ire;
7833 	ipaddr_t	v4setsrc;
7834 	in6_addr_t	v6setsrc;
7835 	char		*slabel, *dlabel;
7836 	boolean_t	isipv4;
7837 	int		match_ire;
7838 	ill_t		*dst_ill;
7839 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7840 	conn_t		*connp = Q_TO_CONN(q);
7841 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7842 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7843 	uint64_t	ipif_flags;
7844 
7845 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7846 
7847 	/*
7848 	 * This ioctl is I_STR only, and must have a
7849 	 * data mblk following the M_IOCTL mblk.
7850 	 */
7851 	data_mp = mp->b_cont;
7852 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7853 		miocnak(q, mp, 0, EINVAL);
7854 		return;
7855 	}
7856 
7857 	if (MBLKL(data_mp) < iocp->ioc_count) {
7858 		mblk_t *new_data_mp;
7859 
7860 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7861 			miocnak(q, mp, 0, ENOMEM);
7862 			return;
7863 		}
7864 		freemsg(data_mp);
7865 		data_mp = new_data_mp;
7866 		mp->b_cont = data_mp;
7867 	}
7868 	match_ire = MATCH_IRE_DSTONLY;
7869 
7870 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7871 	    end - cur >= sizeof (struct dstinforeq);
7872 	    cur += sizeof (struct dstinforeq)) {
7873 		dir = (struct dstinforeq *)cur;
7874 		daddr = &dir->dir_daddr;
7875 		saddr = &dir->dir_saddr;
7876 
7877 		/*
7878 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7879 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7880 		 * and ip_select_source_v6() do not.
7881 		 */
7882 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7883 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7884 
7885 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7886 		if (isipv4) {
7887 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7888 			v4setsrc = INADDR_ANY;
7889 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7890 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7891 			    NULL, NULL);
7892 		} else {
7893 			v6setsrc = ipv6_all_zeros;
7894 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7895 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7896 			    NULL, NULL);
7897 		}
7898 		ASSERT(ire != NULL);
7899 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7900 			ire_refrele(ire);
7901 			dir->dir_dreachable = 0;
7902 
7903 			/* move on to next dst addr */
7904 			continue;
7905 		}
7906 		dir->dir_dreachable = 1;
7907 
7908 		dst_ill = ire_nexthop_ill(ire);
7909 		if (dst_ill == NULL) {
7910 			ire_refrele(ire);
7911 			continue;
7912 		}
7913 
7914 		/* With ipmp we most likely look at the ipmp ill here */
7915 		dir->dir_dmactype = dst_ill->ill_mactype;
7916 
7917 		if (isipv4) {
7918 			ipaddr_t v4saddr;
7919 
7920 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7921 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7922 			    &v4saddr, NULL, &ipif_flags) != 0) {
7923 				v4saddr = INADDR_ANY;
7924 				ipif_flags = 0;
7925 			}
7926 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7927 		} else {
7928 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7929 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7930 			    saddr, NULL, &ipif_flags) != 0) {
7931 				*saddr = ipv6_all_zeros;
7932 				ipif_flags = 0;
7933 			}
7934 		}
7935 
7936 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7937 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7938 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7939 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7940 		ire_refrele(ire);
7941 		ill_refrele(dst_ill);
7942 	}
7943 	miocack(q, mp, iocp->ioc_count, 0);
7944 }
7945 
7946 /*
7947  * Check if this is an address assigned to this machine.
7948  * Skips interfaces that are down by using ire checks.
7949  * Translates mapped addresses to v4 addresses and then
7950  * treats them as such, returning true if the v4 address
7951  * associated with this mapped address is configured.
7952  * Note: Applications will have to be careful what they do
7953  * with the response; use of mapped addresses limits
7954  * what can be done with the socket, especially with
7955  * respect to socket options and ioctls - neither IPv4
7956  * options nor IPv6 sticky options/ancillary data options
7957  * may be used.
7958  */
7959 /* ARGSUSED */
7960 int
7961 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7962     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7963 {
7964 	struct sioc_addrreq *sia;
7965 	sin_t *sin;
7966 	ire_t *ire;
7967 	mblk_t *mp1;
7968 	zoneid_t zoneid;
7969 	ip_stack_t	*ipst;
7970 
7971 	ip1dbg(("ip_sioctl_tmyaddr"));
7972 
7973 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7974 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7975 	ipst = CONNQ_TO_IPST(q);
7976 
7977 	/* Existence verified in ip_wput_nondata */
7978 	mp1 = mp->b_cont->b_cont;
7979 	sia = (struct sioc_addrreq *)mp1->b_rptr;
7980 	sin = (sin_t *)&sia->sa_addr;
7981 	switch (sin->sin_family) {
7982 	case AF_INET6: {
7983 		sin6_t *sin6 = (sin6_t *)sin;
7984 
7985 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7986 			ipaddr_t v4_addr;
7987 
7988 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
7989 			    v4_addr);
7990 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
7991 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7992 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
7993 		} else {
7994 			in6_addr_t v6addr;
7995 
7996 			v6addr = sin6->sin6_addr;
7997 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
7998 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
7999 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8000 		}
8001 		break;
8002 	}
8003 	case AF_INET: {
8004 		ipaddr_t v4addr;
8005 
8006 		v4addr = sin->sin_addr.s_addr;
8007 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8008 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8009 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8010 		break;
8011 	}
8012 	default:
8013 		return (EAFNOSUPPORT);
8014 	}
8015 	if (ire != NULL) {
8016 		sia->sa_res = 1;
8017 		ire_refrele(ire);
8018 	} else {
8019 		sia->sa_res = 0;
8020 	}
8021 	return (0);
8022 }
8023 
8024 /*
8025  * Check if this is an address assigned on-link i.e. neighbor,
8026  * and makes sure it's reachable from the current zone.
8027  * Returns true for my addresses as well.
8028  * Translates mapped addresses to v4 addresses and then
8029  * treats them as such, returning true if the v4 address
8030  * associated with this mapped address is configured.
8031  * Note: Applications will have to be careful what they do
8032  * with the response; use of mapped addresses limits
8033  * what can be done with the socket, especially with
8034  * respect to socket options and ioctls - neither IPv4
8035  * options nor IPv6 sticky options/ancillary data options
8036  * may be used.
8037  */
8038 /* ARGSUSED */
8039 int
8040 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8041     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8042 {
8043 	struct sioc_addrreq *sia;
8044 	sin_t *sin;
8045 	mblk_t	*mp1;
8046 	ire_t *ire = NULL;
8047 	zoneid_t zoneid;
8048 	ip_stack_t	*ipst;
8049 
8050 	ip1dbg(("ip_sioctl_tonlink"));
8051 
8052 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8053 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8054 	ipst = CONNQ_TO_IPST(q);
8055 
8056 	/* Existence verified in ip_wput_nondata */
8057 	mp1 = mp->b_cont->b_cont;
8058 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8059 	sin = (sin_t *)&sia->sa_addr;
8060 
8061 	/*
8062 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8063 	 * to make sure we only look at on-link unicast address.
8064 	 */
8065 	switch (sin->sin_family) {
8066 	case AF_INET6: {
8067 		sin6_t *sin6 = (sin6_t *)sin;
8068 
8069 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8070 			ipaddr_t v4_addr;
8071 
8072 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8073 			    v4_addr);
8074 			if (!CLASSD(v4_addr)) {
8075 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8076 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8077 				    0, ipst, NULL);
8078 			}
8079 		} else {
8080 			in6_addr_t v6addr;
8081 
8082 			v6addr = sin6->sin6_addr;
8083 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8084 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8085 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8086 				    ipst, NULL);
8087 			}
8088 		}
8089 		break;
8090 	}
8091 	case AF_INET: {
8092 		ipaddr_t v4addr;
8093 
8094 		v4addr = sin->sin_addr.s_addr;
8095 		if (!CLASSD(v4addr)) {
8096 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8097 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8098 		}
8099 		break;
8100 	}
8101 	default:
8102 		return (EAFNOSUPPORT);
8103 	}
8104 	sia->sa_res = 0;
8105 	if (ire != NULL) {
8106 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8107 
8108 		if ((ire->ire_type & IRE_ONLINK) &&
8109 		    !(ire->ire_type & IRE_BROADCAST))
8110 			sia->sa_res = 1;
8111 		ire_refrele(ire);
8112 	}
8113 	return (0);
8114 }
8115 
8116 /*
8117  * TBD: implement when kernel maintaines a list of site prefixes.
8118  */
8119 /* ARGSUSED */
8120 int
8121 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8122     ip_ioctl_cmd_t *ipip, void *ifreq)
8123 {
8124 	return (ENXIO);
8125 }
8126 
8127 /* ARP IOCTLs. */
8128 /* ARGSUSED */
8129 int
8130 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8131     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8132 {
8133 	int		err;
8134 	ipaddr_t	ipaddr;
8135 	struct iocblk	*iocp;
8136 	conn_t		*connp;
8137 	struct arpreq	*ar;
8138 	struct xarpreq	*xar;
8139 	int		arp_flags, flags, alength;
8140 	uchar_t		*lladdr;
8141 	ip_stack_t	*ipst;
8142 	ill_t		*ill = ipif->ipif_ill;
8143 	ill_t		*proxy_ill = NULL;
8144 	ipmp_arpent_t	*entp = NULL;
8145 	boolean_t	proxyarp = B_FALSE;
8146 	boolean_t	if_arp_ioctl = B_FALSE;
8147 	ncec_t		*ncec = NULL;
8148 	nce_t		*nce;
8149 
8150 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8151 	connp = Q_TO_CONN(q);
8152 	ipst = connp->conn_netstack->netstack_ip;
8153 	iocp = (struct iocblk *)mp->b_rptr;
8154 
8155 	if (ipip->ipi_cmd_type == XARP_CMD) {
8156 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8157 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8158 		ar = NULL;
8159 
8160 		arp_flags = xar->xarp_flags;
8161 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8162 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8163 		/*
8164 		 * Validate against user's link layer address length
8165 		 * input and name and addr length limits.
8166 		 */
8167 		alength = ill->ill_phys_addr_length;
8168 		if (ipip->ipi_cmd == SIOCSXARP) {
8169 			if (alength != xar->xarp_ha.sdl_alen ||
8170 			    (alength + xar->xarp_ha.sdl_nlen >
8171 			    sizeof (xar->xarp_ha.sdl_data)))
8172 				return (EINVAL);
8173 		}
8174 	} else {
8175 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8176 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8177 		xar = NULL;
8178 
8179 		arp_flags = ar->arp_flags;
8180 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8181 		/*
8182 		 * Theoretically, the sa_family could tell us what link
8183 		 * layer type this operation is trying to deal with. By
8184 		 * common usage AF_UNSPEC means ethernet. We'll assume
8185 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8186 		 * for now. Our new SIOC*XARP ioctls can be used more
8187 		 * generally.
8188 		 *
8189 		 * If the underlying media happens to have a non 6 byte
8190 		 * address, arp module will fail set/get, but the del
8191 		 * operation will succeed.
8192 		 */
8193 		alength = 6;
8194 		if ((ipip->ipi_cmd != SIOCDARP) &&
8195 		    (alength != ill->ill_phys_addr_length)) {
8196 			return (EINVAL);
8197 		}
8198 	}
8199 
8200 	/* Translate ATF* flags to NCE* flags */
8201 	flags = 0;
8202 	if (arp_flags & ATF_AUTHORITY)
8203 		flags |= NCE_F_AUTHORITY;
8204 	if (arp_flags & ATF_PERM)
8205 		flags |= NCE_F_NONUD; /* not subject to aging */
8206 	if (arp_flags & ATF_PUBL)
8207 		flags |= NCE_F_PUBLISH;
8208 
8209 	/*
8210 	 * IPMP ARP special handling:
8211 	 *
8212 	 * 1. Since ARP mappings must appear consistent across the group,
8213 	 *    prohibit changing ARP mappings on the underlying interfaces.
8214 	 *
8215 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8216 	 *    IP itself, prohibit changing them.
8217 	 *
8218 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8219 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8220 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8221 	 */
8222 	if (IS_UNDER_IPMP(ill)) {
8223 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8224 			return (EPERM);
8225 	}
8226 	if (IS_IPMP(ill)) {
8227 		ipmp_illgrp_t *illg = ill->ill_grp;
8228 
8229 		switch (ipip->ipi_cmd) {
8230 		case SIOCSARP:
8231 		case SIOCSXARP:
8232 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8233 			if (proxy_ill != NULL) {
8234 				proxyarp = B_TRUE;
8235 				if (!ipmp_ill_is_active(proxy_ill))
8236 					proxy_ill = ipmp_illgrp_next_ill(illg);
8237 				if (proxy_ill != NULL)
8238 					lladdr = proxy_ill->ill_phys_addr;
8239 			}
8240 			/* FALLTHRU */
8241 		}
8242 	}
8243 
8244 	ipaddr = sin->sin_addr.s_addr;
8245 	/*
8246 	 * don't match across illgrp per case (1) and (2).
8247 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8248 	 */
8249 	nce = nce_lookup_v4(ill, &ipaddr);
8250 	if (nce != NULL)
8251 		ncec = nce->nce_common;
8252 
8253 	switch (iocp->ioc_cmd) {
8254 	case SIOCDARP:
8255 	case SIOCDXARP: {
8256 		/*
8257 		 * Delete the NCE if any.
8258 		 */
8259 		if (ncec == NULL) {
8260 			iocp->ioc_error = ENXIO;
8261 			break;
8262 		}
8263 		/* Don't allow changes to arp mappings of local addresses. */
8264 		if (NCE_MYADDR(ncec)) {
8265 			nce_refrele(nce);
8266 			return (ENOTSUP);
8267 		}
8268 		iocp->ioc_error = 0;
8269 
8270 		/*
8271 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8272 		 * This will delete all the nce entries on the under_ills.
8273 		 */
8274 		ncec_delete(ncec);
8275 		/*
8276 		 * Once the NCE has been deleted, then the ire_dep* consistency
8277 		 * mechanism will find any IRE which depended on the now
8278 		 * condemned NCE (as part of sending packets).
8279 		 * That mechanism handles redirects by deleting redirects
8280 		 * that refer to UNREACHABLE nces.
8281 		 */
8282 		break;
8283 	}
8284 	case SIOCGARP:
8285 	case SIOCGXARP:
8286 		if (ncec != NULL) {
8287 			lladdr = ncec->ncec_lladdr;
8288 			flags = ncec->ncec_flags;
8289 			iocp->ioc_error = 0;
8290 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8291 		} else {
8292 			iocp->ioc_error = ENXIO;
8293 		}
8294 		break;
8295 	case SIOCSARP:
8296 	case SIOCSXARP:
8297 		/* Don't allow changes to arp mappings of local addresses. */
8298 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8299 			nce_refrele(nce);
8300 			return (ENOTSUP);
8301 		}
8302 
8303 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8304 		flags |= NCE_F_STATIC;
8305 		if (!if_arp_ioctl) {
8306 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8307 			    lladdr, alength, flags);
8308 		} else {
8309 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8310 			if (ipif != NULL) {
8311 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8312 				    lladdr, alength, flags);
8313 				ipif_refrele(ipif);
8314 			}
8315 		}
8316 		if (nce != NULL) {
8317 			nce_refrele(nce);
8318 			nce = NULL;
8319 		}
8320 		/*
8321 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8322 		 * by nce_add_common()
8323 		 */
8324 		err = nce_lookup_then_add_v4(ill, lladdr,
8325 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8326 		    &nce);
8327 		if (err == EEXIST) {
8328 			ncec = nce->nce_common;
8329 			mutex_enter(&ncec->ncec_lock);
8330 			ncec->ncec_state = ND_REACHABLE;
8331 			ncec->ncec_flags = flags;
8332 			nce_update(ncec, ND_UNCHANGED, lladdr);
8333 			mutex_exit(&ncec->ncec_lock);
8334 			err = 0;
8335 		}
8336 		if (nce != NULL) {
8337 			nce_refrele(nce);
8338 			nce = NULL;
8339 		}
8340 		if (IS_IPMP(ill) && err == 0) {
8341 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8342 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8343 			    flags);
8344 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8345 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8346 				break;
8347 			}
8348 		}
8349 		iocp->ioc_error = err;
8350 	}
8351 
8352 	if (nce != NULL) {
8353 		nce_refrele(nce);
8354 	}
8355 
8356 	/*
8357 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8358 	 */
8359 	if (entp != NULL)
8360 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8361 
8362 	return (iocp->ioc_error);
8363 }
8364 
8365 /*
8366  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8367  * the associated sin and refhold and return the associated ipif via `ci'.
8368  */
8369 int
8370 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8371     cmd_info_t *ci)
8372 {
8373 	mblk_t	*mp1;
8374 	sin_t	*sin;
8375 	conn_t	*connp;
8376 	ipif_t	*ipif;
8377 	ire_t	*ire = NULL;
8378 	ill_t	*ill = NULL;
8379 	boolean_t exists;
8380 	ip_stack_t *ipst;
8381 	struct arpreq *ar;
8382 	struct xarpreq *xar;
8383 	struct sockaddr_dl *sdl;
8384 
8385 	/* ioctl comes down on a conn */
8386 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8387 	connp = Q_TO_CONN(q);
8388 	if (connp->conn_family == AF_INET6)
8389 		return (ENXIO);
8390 
8391 	ipst = connp->conn_netstack->netstack_ip;
8392 
8393 	/* Verified in ip_wput_nondata */
8394 	mp1 = mp->b_cont->b_cont;
8395 
8396 	if (ipip->ipi_cmd_type == XARP_CMD) {
8397 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8398 		xar = (struct xarpreq *)mp1->b_rptr;
8399 		sin = (sin_t *)&xar->xarp_pa;
8400 		sdl = &xar->xarp_ha;
8401 
8402 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8403 			return (ENXIO);
8404 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8405 			return (EINVAL);
8406 	} else {
8407 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8408 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8409 		ar = (struct arpreq *)mp1->b_rptr;
8410 		sin = (sin_t *)&ar->arp_pa;
8411 	}
8412 
8413 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8414 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8415 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8416 		if (ipif == NULL)
8417 			return (ENXIO);
8418 		if (ipif->ipif_id != 0) {
8419 			ipif_refrele(ipif);
8420 			return (ENXIO);
8421 		}
8422 	} else {
8423 		/*
8424 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8425 		 * of 0: use the IP address to find the ipif.  If the IP
8426 		 * address is an IPMP test address, ire_ftable_lookup() will
8427 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8428 		 */
8429 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8430 		    ipst);
8431 		if (ipif == NULL) {
8432 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8433 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8434 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8435 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8436 				if (ire != NULL)
8437 					ire_refrele(ire);
8438 				return (ENXIO);
8439 			}
8440 			ASSERT(ire != NULL && ill != NULL);
8441 			ipif = ill->ill_ipif;
8442 			ipif_refhold(ipif);
8443 			ire_refrele(ire);
8444 		}
8445 	}
8446 
8447 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8448 		ipif_refrele(ipif);
8449 		return (ENXIO);
8450 	}
8451 
8452 	ci->ci_sin = sin;
8453 	ci->ci_ipif = ipif;
8454 	return (0);
8455 }
8456 
8457 /*
8458  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8459  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8460  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8461  * up and thus an ill can join that illgrp.
8462  *
8463  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8464  * open()/close() primarily because close() is not allowed to fail or block
8465  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8466  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8467  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8468  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8469  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8470  * state if I_UNLINK didn't occur.
8471  *
8472  * Note that for each plumb/unplumb operation, we may end up here more than
8473  * once because of the way ifconfig works.  However, it's OK to link the same
8474  * illgrp more than once, or unlink an illgrp that's already unlinked.
8475  */
8476 static int
8477 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8478 {
8479 	int err;
8480 	ip_stack_t *ipst = ill->ill_ipst;
8481 
8482 	ASSERT(IS_IPMP(ill));
8483 	ASSERT(IAM_WRITER_ILL(ill));
8484 
8485 	switch (ioccmd) {
8486 	case I_LINK:
8487 		return (ENOTSUP);
8488 
8489 	case I_PLINK:
8490 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8491 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8492 		rw_exit(&ipst->ips_ipmp_lock);
8493 		break;
8494 
8495 	case I_PUNLINK:
8496 		/*
8497 		 * Require all UP ipifs be brought down prior to unlinking the
8498 		 * illgrp so any associated IREs (and other state) is torched.
8499 		 */
8500 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8501 			return (EBUSY);
8502 
8503 		/*
8504 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8505 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8506 		 * join this group.  Specifically: ills trying to join grab
8507 		 * ipmp_lock and bump a "pending join" counter checked by
8508 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8509 		 * joins can occur (since we have ipmp_lock).  Once we drop
8510 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8511 		 * find the illgrp (since we unlinked it) and will return
8512 		 * EAFNOSUPPORT.  This will then take them back through the
8513 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8514 		 * back through I_PLINK above.
8515 		 */
8516 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8517 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8518 		rw_exit(&ipst->ips_ipmp_lock);
8519 		return (err);
8520 	default:
8521 		break;
8522 	}
8523 	return (0);
8524 }
8525 
8526 /*
8527  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8528  * atomically set/clear the muxids. Also complete the ioctl by acking or
8529  * naking it.  Note that the code is structured such that the link type,
8530  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8531  * its clones use the persistent link, while pppd(1M) and perhaps many
8532  * other daemons may use non-persistent link.  When combined with some
8533  * ill_t states, linking and unlinking lower streams may be used as
8534  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8535  */
8536 /* ARGSUSED */
8537 void
8538 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8539 {
8540 	mblk_t		*mp1;
8541 	struct linkblk	*li;
8542 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8543 	int		err = 0;
8544 
8545 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8546 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8547 
8548 	mp1 = mp->b_cont;	/* This is the linkblk info */
8549 	li = (struct linkblk *)mp1->b_rptr;
8550 
8551 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8552 	if (err == EINPROGRESS)
8553 		return;
8554 done:
8555 	if (err == 0)
8556 		miocack(q, mp, 0, 0);
8557 	else
8558 		miocnak(q, mp, 0, err);
8559 
8560 	/* Conn was refheld in ip_sioctl_copyin_setup */
8561 	if (CONN_Q(q))
8562 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8563 }
8564 
8565 /*
8566  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8567  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8568  * module stream).  If `doconsist' is set, then do the extended consistency
8569  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8570  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8571  * an error code on failure.
8572  */
8573 static int
8574 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8575     struct linkblk *li)
8576 {
8577 	int		err = 0;
8578 	ill_t  		*ill;
8579 	queue_t		*ipwq, *dwq;
8580 	const char	*name;
8581 	struct qinit	*qinfo;
8582 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8583 	boolean_t	entered_ipsq = B_FALSE;
8584 	boolean_t	is_ip = B_FALSE;
8585 	arl_t		*arl;
8586 
8587 	/*
8588 	 * Walk the lower stream to verify it's the IP module stream.
8589 	 * The IP module is identified by its name, wput function,
8590 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8591 	 * (li->l_qbot) will not vanish until this ioctl completes.
8592 	 */
8593 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8594 		qinfo = ipwq->q_qinfo;
8595 		name = qinfo->qi_minfo->mi_idname;
8596 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8597 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8598 			is_ip = B_TRUE;
8599 			break;
8600 		}
8601 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8602 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8603 			break;
8604 		}
8605 	}
8606 
8607 	/*
8608 	 * If this isn't an IP module stream, bail.
8609 	 */
8610 	if (ipwq == NULL)
8611 		return (0);
8612 
8613 	if (!is_ip) {
8614 		arl = (arl_t *)ipwq->q_ptr;
8615 		ill = arl_to_ill(arl);
8616 		if (ill == NULL)
8617 			return (0);
8618 	} else {
8619 		ill = ipwq->q_ptr;
8620 	}
8621 	ASSERT(ill != NULL);
8622 
8623 	if (ipsq == NULL) {
8624 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8625 		    NEW_OP, B_FALSE);
8626 		if (ipsq == NULL) {
8627 			if (!is_ip)
8628 				ill_refrele(ill);
8629 			return (EINPROGRESS);
8630 		}
8631 		entered_ipsq = B_TRUE;
8632 	}
8633 	ASSERT(IAM_WRITER_ILL(ill));
8634 	mutex_enter(&ill->ill_lock);
8635 	if (!is_ip) {
8636 		if (islink && ill->ill_muxid == 0) {
8637 			/*
8638 			 * Plumbing has to be done with IP plumbed first, arp
8639 			 * second, but here we have arp being plumbed first.
8640 			 */
8641 			mutex_exit(&ill->ill_lock);
8642 			ipsq_exit(ipsq);
8643 			ill_refrele(ill);
8644 			return (EINVAL);
8645 		}
8646 	}
8647 	mutex_exit(&ill->ill_lock);
8648 	if (!is_ip) {
8649 		arl->arl_muxid = islink ? li->l_index : 0;
8650 		ill_refrele(ill);
8651 		goto done;
8652 	}
8653 
8654 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8655 		goto done;
8656 
8657 	/*
8658 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8659 	 * the read queue of the module immediately below IP in the ill.
8660 	 * These are used during the capability negotiation below.
8661 	 */
8662 	ill->ill_lmod_rq = NULL;
8663 	ill->ill_lmod_cnt = 0;
8664 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8665 		ill->ill_lmod_rq = RD(dwq);
8666 		for (; dwq != NULL; dwq = dwq->q_next)
8667 			ill->ill_lmod_cnt++;
8668 	}
8669 
8670 	ill->ill_muxid = islink ? li->l_index : 0;
8671 
8672 	/*
8673 	 * Mark the ipsq busy until the capability operations initiated below
8674 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8675 	 * returns, but the capability operation may complete asynchronously
8676 	 * much later.
8677 	 */
8678 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8679 	/*
8680 	 * If there's at least one up ipif on this ill, then we're bound to
8681 	 * the underlying driver via DLPI.  In that case, renegotiate
8682 	 * capabilities to account for any possible change in modules
8683 	 * interposed between IP and the driver.
8684 	 */
8685 	if (ill->ill_ipif_up_count > 0) {
8686 		if (islink)
8687 			ill_capability_probe(ill);
8688 		else
8689 			ill_capability_reset(ill, B_FALSE);
8690 	}
8691 	ipsq_current_finish(ipsq);
8692 done:
8693 	if (entered_ipsq)
8694 		ipsq_exit(ipsq);
8695 
8696 	return (err);
8697 }
8698 
8699 /*
8700  * Search the ioctl command in the ioctl tables and return a pointer
8701  * to the ioctl command information. The ioctl command tables are
8702  * static and fully populated at compile time.
8703  */
8704 ip_ioctl_cmd_t *
8705 ip_sioctl_lookup(int ioc_cmd)
8706 {
8707 	int index;
8708 	ip_ioctl_cmd_t *ipip;
8709 	ip_ioctl_cmd_t *ipip_end;
8710 
8711 	if (ioc_cmd == IPI_DONTCARE)
8712 		return (NULL);
8713 
8714 	/*
8715 	 * Do a 2 step search. First search the indexed table
8716 	 * based on the least significant byte of the ioctl cmd.
8717 	 * If we don't find a match, then search the misc table
8718 	 * serially.
8719 	 */
8720 	index = ioc_cmd & 0xFF;
8721 	if (index < ip_ndx_ioctl_count) {
8722 		ipip = &ip_ndx_ioctl_table[index];
8723 		if (ipip->ipi_cmd == ioc_cmd) {
8724 			/* Found a match in the ndx table */
8725 			return (ipip);
8726 		}
8727 	}
8728 
8729 	/* Search the misc table */
8730 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8731 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8732 		if (ipip->ipi_cmd == ioc_cmd)
8733 			/* Found a match in the misc table */
8734 			return (ipip);
8735 	}
8736 
8737 	return (NULL);
8738 }
8739 
8740 /*
8741  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8742  */
8743 static boolean_t
8744 getset_ioctl_checks(mblk_t *mp)
8745 {
8746 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8747 	mblk_t		*mp1 = mp->b_cont;
8748 	mod_ioc_prop_t	*pioc;
8749 	uint_t		flags;
8750 	uint_t		pioc_size;
8751 
8752 	/* do sanity checks on various arguments */
8753 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8754 	    iocp->ioc_count == TRANSPARENT) {
8755 		return (B_FALSE);
8756 	}
8757 	if (msgdsize(mp1) < iocp->ioc_count) {
8758 		if (!pullupmsg(mp1, iocp->ioc_count))
8759 			return (B_FALSE);
8760 	}
8761 
8762 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8763 
8764 	/* sanity checks on mpr_valsize */
8765 	pioc_size = sizeof (mod_ioc_prop_t);
8766 	if (pioc->mpr_valsize != 0)
8767 		pioc_size += pioc->mpr_valsize - 1;
8768 
8769 	if (iocp->ioc_count != pioc_size)
8770 		return (B_FALSE);
8771 
8772 	flags = pioc->mpr_flags;
8773 	if (iocp->ioc_cmd == SIOCSETPROP) {
8774 		/*
8775 		 * One can either reset the value to it's default value or
8776 		 * change the current value or append/remove the value from
8777 		 * a multi-valued properties.
8778 		 */
8779 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8780 		    flags != MOD_PROP_ACTIVE &&
8781 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8782 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8783 			return (B_FALSE);
8784 	} else {
8785 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8786 
8787 		/*
8788 		 * One can retrieve only one kind of property information
8789 		 * at a time.
8790 		 */
8791 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8792 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8793 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8794 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8795 			return (B_FALSE);
8796 	}
8797 
8798 	return (B_TRUE);
8799 }
8800 
8801 /*
8802  * process the SIOC{SET|GET}PROP ioctl's
8803  */
8804 /* ARGSUSED */
8805 static void
8806 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8807 {
8808 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8809 	mblk_t		*mp1 = mp->b_cont;
8810 	mod_ioc_prop_t	*pioc;
8811 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8812 	ip_stack_t	*ipst;
8813 	icmp_stack_t	*is;
8814 	tcp_stack_t	*tcps;
8815 	sctp_stack_t	*sctps;
8816 	udp_stack_t	*us;
8817 	netstack_t	*stack;
8818 	void		*cbarg;
8819 	cred_t		*cr;
8820 	boolean_t 	set;
8821 	int		err;
8822 
8823 	ASSERT(q->q_next == NULL);
8824 	ASSERT(CONN_Q(q));
8825 
8826 	if (!getset_ioctl_checks(mp)) {
8827 		miocnak(q, mp, 0, EINVAL);
8828 		return;
8829 	}
8830 	ipst = CONNQ_TO_IPST(q);
8831 	stack = ipst->ips_netstack;
8832 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8833 
8834 	switch (pioc->mpr_proto) {
8835 	case MOD_PROTO_IP:
8836 	case MOD_PROTO_IPV4:
8837 	case MOD_PROTO_IPV6:
8838 		ptbl = ipst->ips_propinfo_tbl;
8839 		cbarg = ipst;
8840 		break;
8841 	case MOD_PROTO_RAWIP:
8842 		is = stack->netstack_icmp;
8843 		ptbl = is->is_propinfo_tbl;
8844 		cbarg = is;
8845 		break;
8846 	case MOD_PROTO_TCP:
8847 		tcps = stack->netstack_tcp;
8848 		ptbl = tcps->tcps_propinfo_tbl;
8849 		cbarg = tcps;
8850 		break;
8851 	case MOD_PROTO_UDP:
8852 		us = stack->netstack_udp;
8853 		ptbl = us->us_propinfo_tbl;
8854 		cbarg = us;
8855 		break;
8856 	case MOD_PROTO_SCTP:
8857 		sctps = stack->netstack_sctp;
8858 		ptbl = sctps->sctps_propinfo_tbl;
8859 		cbarg = sctps;
8860 		break;
8861 	default:
8862 		miocnak(q, mp, 0, EINVAL);
8863 		return;
8864 	}
8865 
8866 	/* search for given property in respective protocol propinfo table */
8867 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8868 		if (strcmp(pinfo->mpi_name, pioc->mpr_name) == 0 &&
8869 		    pinfo->mpi_proto == pioc->mpr_proto)
8870 			break;
8871 	}
8872 	if (pinfo->mpi_name == NULL) {
8873 		miocnak(q, mp, 0, ENOENT);
8874 		return;
8875 	}
8876 
8877 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8878 	if (set && pinfo->mpi_setf != NULL) {
8879 		cr = msg_getcred(mp, NULL);
8880 		if (cr == NULL)
8881 			cr = iocp->ioc_cr;
8882 		err = pinfo->mpi_setf(cbarg, cr, pinfo, pioc->mpr_ifname,
8883 		    pioc->mpr_val, pioc->mpr_flags);
8884 	} else if (!set && pinfo->mpi_getf != NULL) {
8885 		err = pinfo->mpi_getf(cbarg, pinfo, pioc->mpr_ifname,
8886 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8887 	} else {
8888 		err = EPERM;
8889 	}
8890 
8891 	if (err != 0) {
8892 		miocnak(q, mp, 0, err);
8893 	} else {
8894 		if (set)
8895 			miocack(q, mp, 0, 0);
8896 		else    /* For get, we need to return back the data */
8897 			miocack(q, mp, iocp->ioc_count, 0);
8898 	}
8899 }
8900 
8901 /*
8902  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8903  * as several routing daemons have unfortunately used this 'unpublished'
8904  * but well-known ioctls.
8905  */
8906 /* ARGSUSED */
8907 static void
8908 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8909 {
8910 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8911 	mblk_t		*mp1 = mp->b_cont;
8912 	char		*pname, *pval, *buf;
8913 	uint_t		bufsize, proto;
8914 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8915 	ip_stack_t	*ipst;
8916 	int		err = 0;
8917 
8918 	ASSERT(CONN_Q(q));
8919 	ipst = CONNQ_TO_IPST(q);
8920 
8921 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8922 		miocnak(q, mp, 0, EINVAL);
8923 		return;
8924 	}
8925 
8926 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8927 	pval = buf = pname = (char *)mp1->b_rptr;
8928 	bufsize = MBLKL(mp1);
8929 
8930 	if (strcmp(pname, "ip_forwarding") == 0) {
8931 		pname = "forwarding";
8932 		proto = MOD_PROTO_IPV4;
8933 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8934 		pname = "forwarding";
8935 		proto = MOD_PROTO_IPV6;
8936 	} else {
8937 		miocnak(q, mp, 0, EINVAL);
8938 		return;
8939 	}
8940 
8941 	ptbl = ipst->ips_propinfo_tbl;
8942 	for (pinfo = ptbl; pinfo->mpi_name != NULL; pinfo++) {
8943 		if (strcmp(pinfo->mpi_name, pname) == 0 &&
8944 		    pinfo->mpi_proto == proto)
8945 			break;
8946 	}
8947 
8948 	ASSERT(pinfo->mpi_name != NULL);
8949 
8950 	switch (iocp->ioc_cmd) {
8951 	case ND_GET:
8952 		if ((err = pinfo->mpi_getf(ipst, pinfo, NULL, buf, bufsize,
8953 		    0)) == 0) {
8954 			miocack(q, mp, iocp->ioc_count, 0);
8955 			return;
8956 		}
8957 		break;
8958 	case ND_SET:
8959 		/*
8960 		 * buffer will have property name and value in the following
8961 		 * format,
8962 		 * <property name>'\0'<property value>'\0', extract them;
8963 		 */
8964 		while (*pval++)
8965 			noop;
8966 
8967 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8968 			err = EINVAL;
8969 		} else if ((err = pinfo->mpi_setf(ipst, NULL, pinfo, NULL,
8970 		    pval, 0)) == 0) {
8971 			miocack(q, mp, 0, 0);
8972 			return;
8973 		}
8974 		break;
8975 	default:
8976 		err = EINVAL;
8977 		break;
8978 	}
8979 	miocnak(q, mp, 0, err);
8980 }
8981 
8982 /*
8983  * Wrapper function for resuming deferred ioctl processing
8984  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8985  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8986  */
8987 /* ARGSUSED */
8988 void
8989 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8990     void *dummy_arg)
8991 {
8992 	ip_sioctl_copyin_setup(q, mp);
8993 }
8994 
8995 /*
8996  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8997  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8998  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8999  * We establish here the size of the block to be copied in.  mi_copyin
9000  * arranges for this to happen, an processing continues in ip_wput_nondata with
9001  * an M_IOCDATA message.
9002  */
9003 void
9004 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9005 {
9006 	int	copyin_size;
9007 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9008 	ip_ioctl_cmd_t *ipip;
9009 	cred_t *cr;
9010 	ip_stack_t	*ipst;
9011 
9012 	if (CONN_Q(q))
9013 		ipst = CONNQ_TO_IPST(q);
9014 	else
9015 		ipst = ILLQ_TO_IPST(q);
9016 
9017 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9018 	if (ipip == NULL) {
9019 		/*
9020 		 * The ioctl is not one we understand or own.
9021 		 * Pass it along to be processed down stream,
9022 		 * if this is a module instance of IP, else nak
9023 		 * the ioctl.
9024 		 */
9025 		if (q->q_next == NULL) {
9026 			goto nak;
9027 		} else {
9028 			putnext(q, mp);
9029 			return;
9030 		}
9031 	}
9032 
9033 	/*
9034 	 * If this is deferred, then we will do all the checks when we
9035 	 * come back.
9036 	 */
9037 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9038 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9039 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9040 		return;
9041 	}
9042 
9043 	/*
9044 	 * Only allow a very small subset of IP ioctls on this stream if
9045 	 * IP is a module and not a driver. Allowing ioctls to be processed
9046 	 * in this case may cause assert failures or data corruption.
9047 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9048 	 * ioctls allowed on an IP module stream, after which this stream
9049 	 * normally becomes a multiplexor (at which time the stream head
9050 	 * will fail all ioctls).
9051 	 */
9052 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9053 		goto nak;
9054 	}
9055 
9056 	/* Make sure we have ioctl data to process. */
9057 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9058 		goto nak;
9059 
9060 	/*
9061 	 * Prefer dblk credential over ioctl credential; some synthesized
9062 	 * ioctls have kcred set because there's no way to crhold()
9063 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9064 	 * the framework; the caller of ioctl needs to hold the reference
9065 	 * for the duration of the call).
9066 	 */
9067 	cr = msg_getcred(mp, NULL);
9068 	if (cr == NULL)
9069 		cr = iocp->ioc_cr;
9070 
9071 	/* Make sure normal users don't send down privileged ioctls */
9072 	if ((ipip->ipi_flags & IPI_PRIV) &&
9073 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9074 		/* We checked the privilege earlier but log it here */
9075 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9076 		return;
9077 	}
9078 
9079 	/*
9080 	 * The ioctl command tables can only encode fixed length
9081 	 * ioctl data. If the length is variable, the table will
9082 	 * encode the length as zero. Such special cases are handled
9083 	 * below in the switch.
9084 	 */
9085 	if (ipip->ipi_copyin_size != 0) {
9086 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9087 		return;
9088 	}
9089 
9090 	switch (iocp->ioc_cmd) {
9091 	case O_SIOCGIFCONF:
9092 	case SIOCGIFCONF:
9093 		/*
9094 		 * This IOCTL is hilarious.  See comments in
9095 		 * ip_sioctl_get_ifconf for the story.
9096 		 */
9097 		if (iocp->ioc_count == TRANSPARENT)
9098 			copyin_size = SIZEOF_STRUCT(ifconf,
9099 			    iocp->ioc_flag);
9100 		else
9101 			copyin_size = iocp->ioc_count;
9102 		mi_copyin(q, mp, NULL, copyin_size);
9103 		return;
9104 
9105 	case O_SIOCGLIFCONF:
9106 	case SIOCGLIFCONF:
9107 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9108 		mi_copyin(q, mp, NULL, copyin_size);
9109 		return;
9110 
9111 	case SIOCGLIFSRCOF:
9112 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9113 		mi_copyin(q, mp, NULL, copyin_size);
9114 		return;
9115 
9116 	case SIOCGIP6ADDRPOLICY:
9117 		ip_sioctl_ip6addrpolicy(q, mp);
9118 		ip6_asp_table_refrele(ipst);
9119 		return;
9120 
9121 	case SIOCSIP6ADDRPOLICY:
9122 		ip_sioctl_ip6addrpolicy(q, mp);
9123 		return;
9124 
9125 	case SIOCGDSTINFO:
9126 		ip_sioctl_dstinfo(q, mp);
9127 		ip6_asp_table_refrele(ipst);
9128 		return;
9129 
9130 	case ND_SET:
9131 	case ND_GET:
9132 		ip_process_legacy_nddprop(q, mp);
9133 		return;
9134 
9135 	case SIOCSETPROP:
9136 	case SIOCGETPROP:
9137 		ip_sioctl_getsetprop(q, mp);
9138 		return;
9139 
9140 	case I_PLINK:
9141 	case I_PUNLINK:
9142 	case I_LINK:
9143 	case I_UNLINK:
9144 		/*
9145 		 * We treat non-persistent link similarly as the persistent
9146 		 * link case, in terms of plumbing/unplumbing, as well as
9147 		 * dynamic re-plumbing events indicator.  See comments
9148 		 * in ip_sioctl_plink() for more.
9149 		 *
9150 		 * Request can be enqueued in the 'ipsq' while waiting
9151 		 * to become exclusive. So bump up the conn ref.
9152 		 */
9153 		if (CONN_Q(q))
9154 			CONN_INC_REF(Q_TO_CONN(q));
9155 		ip_sioctl_plink(NULL, q, mp, NULL);
9156 		return;
9157 
9158 	case IP_IOCTL:
9159 		ip_wput_ioctl(q, mp);
9160 		return;
9161 
9162 	case SIOCILB:
9163 		/* The ioctl length varies depending on the ILB command. */
9164 		copyin_size = iocp->ioc_count;
9165 		if (copyin_size < sizeof (ilb_cmd_t))
9166 			goto nak;
9167 		mi_copyin(q, mp, NULL, copyin_size);
9168 		return;
9169 
9170 	default:
9171 		cmn_err(CE_PANIC, "should not happen ");
9172 	}
9173 nak:
9174 	if (mp->b_cont != NULL) {
9175 		freemsg(mp->b_cont);
9176 		mp->b_cont = NULL;
9177 	}
9178 	iocp->ioc_error = EINVAL;
9179 	mp->b_datap->db_type = M_IOCNAK;
9180 	iocp->ioc_count = 0;
9181 	qreply(q, mp);
9182 }
9183 
9184 static void
9185 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9186 {
9187 	struct arpreq *ar;
9188 	struct xarpreq *xar;
9189 	mblk_t	*tmp;
9190 	struct iocblk *iocp;
9191 	int x_arp_ioctl = B_FALSE;
9192 	int *flagsp;
9193 	char *storage = NULL;
9194 
9195 	ASSERT(ill != NULL);
9196 
9197 	iocp = (struct iocblk *)mp->b_rptr;
9198 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9199 
9200 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9201 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9202 	    (iocp->ioc_cmd == SIOCSXARP)) {
9203 		x_arp_ioctl = B_TRUE;
9204 		xar = (struct xarpreq *)tmp->b_rptr;
9205 		flagsp = &xar->xarp_flags;
9206 		storage = xar->xarp_ha.sdl_data;
9207 	} else {
9208 		ar = (struct arpreq *)tmp->b_rptr;
9209 		flagsp = &ar->arp_flags;
9210 		storage = ar->arp_ha.sa_data;
9211 	}
9212 
9213 	/*
9214 	 * We're done if this is not an SIOCG{X}ARP
9215 	 */
9216 	if (x_arp_ioctl) {
9217 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9218 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9219 		    sizeof (xar->xarp_ha.sdl_data)) {
9220 			iocp->ioc_error = EINVAL;
9221 			return;
9222 		}
9223 	}
9224 	*flagsp = ATF_INUSE;
9225 	/*
9226 	 * If /sbin/arp told us we are the authority using the "permanent"
9227 	 * flag, or if this is one of my addresses print "permanent"
9228 	 * in the /sbin/arp output.
9229 	 */
9230 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9231 		*flagsp |= ATF_AUTHORITY;
9232 	if (flags & NCE_F_NONUD)
9233 		*flagsp |= ATF_PERM; /* not subject to aging */
9234 	if (flags & NCE_F_PUBLISH)
9235 		*flagsp |= ATF_PUBL;
9236 	if (hwaddr != NULL) {
9237 		*flagsp |= ATF_COM;
9238 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9239 	}
9240 }
9241 
9242 /*
9243  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9244  * interface) create the next available logical interface for this
9245  * physical interface.
9246  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9247  * ipif with the specified name.
9248  *
9249  * If the address family is not AF_UNSPEC then set the address as well.
9250  *
9251  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9252  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9253  *
9254  * Executed as a writer on the ill.
9255  * So no lock is needed to traverse the ipif chain, or examine the
9256  * phyint flags.
9257  */
9258 /* ARGSUSED */
9259 int
9260 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9261     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9262 {
9263 	mblk_t	*mp1;
9264 	struct lifreq *lifr;
9265 	boolean_t	isv6;
9266 	boolean_t	exists;
9267 	char 	*name;
9268 	char	*endp;
9269 	char	*cp;
9270 	int	namelen;
9271 	ipif_t	*ipif;
9272 	long	id;
9273 	ipsq_t	*ipsq;
9274 	ill_t	*ill;
9275 	sin_t	*sin;
9276 	int	err = 0;
9277 	boolean_t found_sep = B_FALSE;
9278 	conn_t	*connp;
9279 	zoneid_t zoneid;
9280 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9281 
9282 	ASSERT(q->q_next == NULL);
9283 	ip1dbg(("ip_sioctl_addif\n"));
9284 	/* Existence of mp1 has been checked in ip_wput_nondata */
9285 	mp1 = mp->b_cont->b_cont;
9286 	/*
9287 	 * Null terminate the string to protect against buffer
9288 	 * overrun. String was generated by user code and may not
9289 	 * be trusted.
9290 	 */
9291 	lifr = (struct lifreq *)mp1->b_rptr;
9292 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9293 	name = lifr->lifr_name;
9294 	ASSERT(CONN_Q(q));
9295 	connp = Q_TO_CONN(q);
9296 	isv6 = (connp->conn_family == AF_INET6);
9297 	zoneid = connp->conn_zoneid;
9298 	namelen = mi_strlen(name);
9299 	if (namelen == 0)
9300 		return (EINVAL);
9301 
9302 	exists = B_FALSE;
9303 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9304 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9305 		/*
9306 		 * Allow creating lo0 using SIOCLIFADDIF.
9307 		 * can't be any other writer thread. So can pass null below
9308 		 * for the last 4 args to ipif_lookup_name.
9309 		 */
9310 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9311 		    &exists, isv6, zoneid, ipst);
9312 		/* Prevent any further action */
9313 		if (ipif == NULL) {
9314 			return (ENOBUFS);
9315 		} else if (!exists) {
9316 			/* We created the ipif now and as writer */
9317 			ipif_refrele(ipif);
9318 			return (0);
9319 		} else {
9320 			ill = ipif->ipif_ill;
9321 			ill_refhold(ill);
9322 			ipif_refrele(ipif);
9323 		}
9324 	} else {
9325 		/* Look for a colon in the name. */
9326 		endp = &name[namelen];
9327 		for (cp = endp; --cp > name; ) {
9328 			if (*cp == IPIF_SEPARATOR_CHAR) {
9329 				found_sep = B_TRUE;
9330 				/*
9331 				 * Reject any non-decimal aliases for plumbing
9332 				 * of logical interfaces. Aliases with leading
9333 				 * zeroes are also rejected as they introduce
9334 				 * ambiguity in the naming of the interfaces.
9335 				 * Comparing with "0" takes care of all such
9336 				 * cases.
9337 				 */
9338 				if ((strncmp("0", cp+1, 1)) == 0)
9339 					return (EINVAL);
9340 
9341 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9342 				    id <= 0 || *endp != '\0') {
9343 					return (EINVAL);
9344 				}
9345 				*cp = '\0';
9346 				break;
9347 			}
9348 		}
9349 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9350 		if (found_sep)
9351 			*cp = IPIF_SEPARATOR_CHAR;
9352 		if (ill == NULL)
9353 			return (ENXIO);
9354 	}
9355 
9356 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9357 	    B_TRUE);
9358 
9359 	/*
9360 	 * Release the refhold due to the lookup, now that we are excl
9361 	 * or we are just returning
9362 	 */
9363 	ill_refrele(ill);
9364 
9365 	if (ipsq == NULL)
9366 		return (EINPROGRESS);
9367 
9368 	/* We are now exclusive on the IPSQ */
9369 	ASSERT(IAM_WRITER_ILL(ill));
9370 
9371 	if (found_sep) {
9372 		/* Now see if there is an IPIF with this unit number. */
9373 		for (ipif = ill->ill_ipif; ipif != NULL;
9374 		    ipif = ipif->ipif_next) {
9375 			if (ipif->ipif_id == id) {
9376 				err = EEXIST;
9377 				goto done;
9378 			}
9379 		}
9380 	}
9381 
9382 	/*
9383 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9384 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9385 	 * instead.
9386 	 */
9387 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9388 	    B_TRUE, B_TRUE, &err)) == NULL) {
9389 		goto done;
9390 	}
9391 
9392 	/* Return created name with ioctl */
9393 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9394 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9395 	ip1dbg(("created %s\n", lifr->lifr_name));
9396 
9397 	/* Set address */
9398 	sin = (sin_t *)&lifr->lifr_addr;
9399 	if (sin->sin_family != AF_UNSPEC) {
9400 		err = ip_sioctl_addr(ipif, sin, q, mp,
9401 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9402 	}
9403 
9404 done:
9405 	ipsq_exit(ipsq);
9406 	return (err);
9407 }
9408 
9409 /*
9410  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9411  * interface) delete it based on the IP address (on this physical interface).
9412  * Otherwise delete it based on the ipif_id.
9413  * Also, special handling to allow a removeif of lo0.
9414  */
9415 /* ARGSUSED */
9416 int
9417 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9418     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9419 {
9420 	conn_t		*connp;
9421 	ill_t		*ill = ipif->ipif_ill;
9422 	boolean_t	 success;
9423 	ip_stack_t	*ipst;
9424 
9425 	ipst = CONNQ_TO_IPST(q);
9426 
9427 	ASSERT(q->q_next == NULL);
9428 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9429 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9430 	ASSERT(IAM_WRITER_IPIF(ipif));
9431 
9432 	connp = Q_TO_CONN(q);
9433 	/*
9434 	 * Special case for unplumbing lo0 (the loopback physical interface).
9435 	 * If unplumbing lo0, the incoming address structure has been
9436 	 * initialized to all zeros. When unplumbing lo0, all its logical
9437 	 * interfaces must be removed too.
9438 	 *
9439 	 * Note that this interface may be called to remove a specific
9440 	 * loopback logical interface (eg, lo0:1). But in that case
9441 	 * ipif->ipif_id != 0 so that the code path for that case is the
9442 	 * same as any other interface (meaning it skips the code directly
9443 	 * below).
9444 	 */
9445 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9446 		if (sin->sin_family == AF_UNSPEC &&
9447 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9448 			/*
9449 			 * Mark it condemned. No new ref. will be made to ill.
9450 			 */
9451 			mutex_enter(&ill->ill_lock);
9452 			ill->ill_state_flags |= ILL_CONDEMNED;
9453 			for (ipif = ill->ill_ipif; ipif != NULL;
9454 			    ipif = ipif->ipif_next) {
9455 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9456 			}
9457 			mutex_exit(&ill->ill_lock);
9458 
9459 			ipif = ill->ill_ipif;
9460 			/* unplumb the loopback interface */
9461 			ill_delete(ill);
9462 			mutex_enter(&connp->conn_lock);
9463 			mutex_enter(&ill->ill_lock);
9464 
9465 			/* Are any references to this ill active */
9466 			if (ill_is_freeable(ill)) {
9467 				mutex_exit(&ill->ill_lock);
9468 				mutex_exit(&connp->conn_lock);
9469 				ill_delete_tail(ill);
9470 				mi_free(ill);
9471 				return (0);
9472 			}
9473 			success = ipsq_pending_mp_add(connp, ipif,
9474 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9475 			mutex_exit(&connp->conn_lock);
9476 			mutex_exit(&ill->ill_lock);
9477 			if (success)
9478 				return (EINPROGRESS);
9479 			else
9480 				return (EINTR);
9481 		}
9482 	}
9483 
9484 	if (ipif->ipif_id == 0) {
9485 		ipsq_t *ipsq;
9486 
9487 		/* Find based on address */
9488 		if (ipif->ipif_isv6) {
9489 			sin6_t *sin6;
9490 
9491 			if (sin->sin_family != AF_INET6)
9492 				return (EAFNOSUPPORT);
9493 
9494 			sin6 = (sin6_t *)sin;
9495 			/* We are a writer, so we should be able to lookup */
9496 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9497 			    ipst);
9498 		} else {
9499 			if (sin->sin_family != AF_INET)
9500 				return (EAFNOSUPPORT);
9501 
9502 			/* We are a writer, so we should be able to lookup */
9503 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9504 			    ipst);
9505 		}
9506 		if (ipif == NULL) {
9507 			return (EADDRNOTAVAIL);
9508 		}
9509 
9510 		/*
9511 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9512 		 * lifr_name of the physical interface but with an ip address
9513 		 * lifr_addr of a logical interface plumbed over it.
9514 		 * So update ipx_current_ipif now that ipif points to the
9515 		 * correct one.
9516 		 */
9517 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9518 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9519 
9520 		/* This is a writer */
9521 		ipif_refrele(ipif);
9522 	}
9523 
9524 	/*
9525 	 * Can not delete instance zero since it is tied to the ill.
9526 	 */
9527 	if (ipif->ipif_id == 0)
9528 		return (EBUSY);
9529 
9530 	mutex_enter(&ill->ill_lock);
9531 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9532 	mutex_exit(&ill->ill_lock);
9533 
9534 	ipif_free(ipif);
9535 
9536 	mutex_enter(&connp->conn_lock);
9537 	mutex_enter(&ill->ill_lock);
9538 
9539 	/* Are any references to this ipif active */
9540 	if (ipif_is_freeable(ipif)) {
9541 		mutex_exit(&ill->ill_lock);
9542 		mutex_exit(&connp->conn_lock);
9543 		ipif_non_duplicate(ipif);
9544 		(void) ipif_down_tail(ipif);
9545 		ipif_free_tail(ipif); /* frees ipif */
9546 		return (0);
9547 	}
9548 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9549 	    IPIF_FREE);
9550 	mutex_exit(&ill->ill_lock);
9551 	mutex_exit(&connp->conn_lock);
9552 	if (success)
9553 		return (EINPROGRESS);
9554 	else
9555 		return (EINTR);
9556 }
9557 
9558 /*
9559  * Restart the removeif ioctl. The refcnt has gone down to 0.
9560  * The ipif is already condemned. So can't find it thru lookups.
9561  */
9562 /* ARGSUSED */
9563 int
9564 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9565     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9566 {
9567 	ill_t *ill = ipif->ipif_ill;
9568 
9569 	ASSERT(IAM_WRITER_IPIF(ipif));
9570 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9571 
9572 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9573 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9574 
9575 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9576 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9577 		ill_delete_tail(ill);
9578 		mi_free(ill);
9579 		return (0);
9580 	}
9581 
9582 	ipif_non_duplicate(ipif);
9583 	(void) ipif_down_tail(ipif);
9584 	ipif_free_tail(ipif);
9585 
9586 	return (0);
9587 }
9588 
9589 /*
9590  * Set the local interface address using the given prefix and ill_token.
9591  */
9592 /* ARGSUSED */
9593 int
9594 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9595     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9596 {
9597 	int err;
9598 	in6_addr_t v6addr;
9599 	sin6_t *sin6;
9600 	ill_t *ill;
9601 	int i;
9602 
9603 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9604 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9605 
9606 	ASSERT(IAM_WRITER_IPIF(ipif));
9607 
9608 	if (!ipif->ipif_isv6)
9609 		return (EINVAL);
9610 
9611 	if (sin->sin_family != AF_INET6)
9612 		return (EAFNOSUPPORT);
9613 
9614 	sin6 = (sin6_t *)sin;
9615 	v6addr = sin6->sin6_addr;
9616 	ill = ipif->ipif_ill;
9617 
9618 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9619 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9620 		return (EADDRNOTAVAIL);
9621 
9622 	for (i = 0; i < 4; i++)
9623 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9624 
9625 	err = ip_sioctl_addr(ipif, sin, q, mp,
9626 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9627 	return (err);
9628 }
9629 
9630 /*
9631  * Restart entry point to restart the address set operation after the
9632  * refcounts have dropped to zero.
9633  */
9634 /* ARGSUSED */
9635 int
9636 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9637     ip_ioctl_cmd_t *ipip, void *ifreq)
9638 {
9639 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9640 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9641 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9642 }
9643 
9644 /*
9645  * Set the local interface address.
9646  * Allow an address of all zero when the interface is down.
9647  */
9648 /* ARGSUSED */
9649 int
9650 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9651     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9652 {
9653 	int err = 0;
9654 	in6_addr_t v6addr;
9655 	boolean_t need_up = B_FALSE;
9656 
9657 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9658 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9659 
9660 	ASSERT(IAM_WRITER_IPIF(ipif));
9661 
9662 	if (ipif->ipif_isv6) {
9663 		sin6_t *sin6;
9664 		ill_t *ill;
9665 		phyint_t *phyi;
9666 
9667 		if (sin->sin_family != AF_INET6)
9668 			return (EAFNOSUPPORT);
9669 
9670 		sin6 = (sin6_t *)sin;
9671 		v6addr = sin6->sin6_addr;
9672 		ill = ipif->ipif_ill;
9673 		phyi = ill->ill_phyint;
9674 
9675 		/*
9676 		 * Enforce that true multicast interfaces have a link-local
9677 		 * address for logical unit 0.
9678 		 *
9679 		 * However for those ipif's for which link-local address was
9680 		 * not created by default, also allow setting :: as the address.
9681 		 * This scenario would arise, when we delete an address on ipif
9682 		 * with logical unit 0, we would want to set :: as the address.
9683 		 */
9684 		if (ipif->ipif_id == 0 &&
9685 		    (ill->ill_flags & ILLF_MULTICAST) &&
9686 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9687 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9688 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9689 
9690 			/*
9691 			 * if default link-local was not created by kernel for
9692 			 * this ill, allow setting :: as the address on ipif:0.
9693 			 */
9694 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9695 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9696 					return (EADDRNOTAVAIL);
9697 			} else {
9698 				return (EADDRNOTAVAIL);
9699 			}
9700 		}
9701 
9702 		/*
9703 		 * up interfaces shouldn't have the unspecified address
9704 		 * unless they also have the IPIF_NOLOCAL flags set and
9705 		 * have a subnet assigned.
9706 		 */
9707 		if ((ipif->ipif_flags & IPIF_UP) &&
9708 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9709 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9710 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9711 			return (EADDRNOTAVAIL);
9712 		}
9713 
9714 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9715 			return (EADDRNOTAVAIL);
9716 	} else {
9717 		ipaddr_t addr;
9718 
9719 		if (sin->sin_family != AF_INET)
9720 			return (EAFNOSUPPORT);
9721 
9722 		addr = sin->sin_addr.s_addr;
9723 
9724 		/* Allow INADDR_ANY as the local address. */
9725 		if (addr != INADDR_ANY &&
9726 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9727 			return (EADDRNOTAVAIL);
9728 
9729 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9730 	}
9731 
9732 	/*
9733 	 * Even if there is no change we redo things just to rerun
9734 	 * ipif_set_default.
9735 	 */
9736 	if (ipif->ipif_flags & IPIF_UP) {
9737 		/*
9738 		 * Setting a new local address, make sure
9739 		 * we have net and subnet bcast ire's for
9740 		 * the old address if we need them.
9741 		 */
9742 		/*
9743 		 * If the interface is already marked up,
9744 		 * we call ipif_down which will take care
9745 		 * of ditching any IREs that have been set
9746 		 * up based on the old interface address.
9747 		 */
9748 		err = ipif_logical_down(ipif, q, mp);
9749 		if (err == EINPROGRESS)
9750 			return (err);
9751 		(void) ipif_down_tail(ipif);
9752 		need_up = 1;
9753 	}
9754 
9755 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9756 	return (err);
9757 }
9758 
9759 int
9760 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9761     boolean_t need_up)
9762 {
9763 	in6_addr_t v6addr;
9764 	in6_addr_t ov6addr;
9765 	ipaddr_t addr;
9766 	sin6_t	*sin6;
9767 	int	sinlen;
9768 	int	err = 0;
9769 	ill_t	*ill = ipif->ipif_ill;
9770 	boolean_t need_dl_down;
9771 	boolean_t need_arp_down;
9772 	struct iocblk *iocp;
9773 
9774 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9775 
9776 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9777 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9778 	ASSERT(IAM_WRITER_IPIF(ipif));
9779 
9780 	/* Must cancel any pending timer before taking the ill_lock */
9781 	if (ipif->ipif_recovery_id != 0)
9782 		(void) untimeout(ipif->ipif_recovery_id);
9783 	ipif->ipif_recovery_id = 0;
9784 
9785 	if (ipif->ipif_isv6) {
9786 		sin6 = (sin6_t *)sin;
9787 		v6addr = sin6->sin6_addr;
9788 		sinlen = sizeof (struct sockaddr_in6);
9789 	} else {
9790 		addr = sin->sin_addr.s_addr;
9791 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9792 		sinlen = sizeof (struct sockaddr_in);
9793 	}
9794 	mutex_enter(&ill->ill_lock);
9795 	ov6addr = ipif->ipif_v6lcl_addr;
9796 	ipif->ipif_v6lcl_addr = v6addr;
9797 	sctp_update_ipif_addr(ipif, ov6addr);
9798 	ipif->ipif_addr_ready = 0;
9799 
9800 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9801 
9802 	/*
9803 	 * If the interface was previously marked as a duplicate, then since
9804 	 * we've now got a "new" address, it should no longer be considered a
9805 	 * duplicate -- even if the "new" address is the same as the old one.
9806 	 * Note that if all ipifs are down, we may have a pending ARP down
9807 	 * event to handle.  This is because we want to recover from duplicates
9808 	 * and thus delay tearing down ARP until the duplicates have been
9809 	 * removed or disabled.
9810 	 */
9811 	need_dl_down = need_arp_down = B_FALSE;
9812 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9813 		need_arp_down = !need_up;
9814 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9815 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9816 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9817 			need_dl_down = B_TRUE;
9818 		}
9819 	}
9820 
9821 	ipif_set_default(ipif);
9822 
9823 	/*
9824 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9825 	 * tag the ill so that future updates to the interface ID don't result
9826 	 * in this address getting automatically reconfigured from under the
9827 	 * administrator.
9828 	 */
9829 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9830 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9831 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9832 			ill->ill_manual_linklocal = 1;
9833 	}
9834 
9835 	/*
9836 	 * When publishing an interface address change event, we only notify
9837 	 * the event listeners of the new address.  It is assumed that if they
9838 	 * actively care about the addresses assigned that they will have
9839 	 * already discovered the previous address assigned (if there was one.)
9840 	 *
9841 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9842 	 */
9843 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9844 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9845 		    NE_ADDRESS_CHANGE, sin, sinlen);
9846 	}
9847 
9848 	mutex_exit(&ill->ill_lock);
9849 
9850 	if (need_up) {
9851 		/*
9852 		 * Now bring the interface back up.  If this
9853 		 * is the only IPIF for the ILL, ipif_up
9854 		 * will have to re-bind to the device, so
9855 		 * we may get back EINPROGRESS, in which
9856 		 * case, this IOCTL will get completed in
9857 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9858 		 */
9859 		err = ipif_up(ipif, q, mp);
9860 	} else {
9861 		/* Perhaps ilgs should use this ill */
9862 		update_conn_ill(NULL, ill->ill_ipst);
9863 	}
9864 
9865 	if (need_dl_down)
9866 		ill_dl_down(ill);
9867 
9868 	if (need_arp_down && !ill->ill_isv6)
9869 		(void) ipif_arp_down(ipif);
9870 
9871 	/*
9872 	 * The default multicast interface might have changed (for
9873 	 * instance if the IPv6 scope of the address changed)
9874 	 */
9875 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9876 
9877 	return (err);
9878 }
9879 
9880 /*
9881  * Restart entry point to restart the address set operation after the
9882  * refcounts have dropped to zero.
9883  */
9884 /* ARGSUSED */
9885 int
9886 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9887     ip_ioctl_cmd_t *ipip, void *ifreq)
9888 {
9889 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9890 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9891 	ASSERT(IAM_WRITER_IPIF(ipif));
9892 	(void) ipif_down_tail(ipif);
9893 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9894 }
9895 
9896 /* ARGSUSED */
9897 int
9898 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9899     ip_ioctl_cmd_t *ipip, void *if_req)
9900 {
9901 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9902 	struct lifreq *lifr = (struct lifreq *)if_req;
9903 
9904 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9905 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9906 	/*
9907 	 * The net mask and address can't change since we have a
9908 	 * reference to the ipif. So no lock is necessary.
9909 	 */
9910 	if (ipif->ipif_isv6) {
9911 		*sin6 = sin6_null;
9912 		sin6->sin6_family = AF_INET6;
9913 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9914 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9915 		lifr->lifr_addrlen =
9916 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9917 	} else {
9918 		*sin = sin_null;
9919 		sin->sin_family = AF_INET;
9920 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9921 		if (ipip->ipi_cmd_type == LIF_CMD) {
9922 			lifr->lifr_addrlen =
9923 			    ip_mask_to_plen(ipif->ipif_net_mask);
9924 		}
9925 	}
9926 	return (0);
9927 }
9928 
9929 /*
9930  * Set the destination address for a pt-pt interface.
9931  */
9932 /* ARGSUSED */
9933 int
9934 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9935     ip_ioctl_cmd_t *ipip, void *if_req)
9936 {
9937 	int err = 0;
9938 	in6_addr_t v6addr;
9939 	boolean_t need_up = B_FALSE;
9940 
9941 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9942 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9943 	ASSERT(IAM_WRITER_IPIF(ipif));
9944 
9945 	if (ipif->ipif_isv6) {
9946 		sin6_t *sin6;
9947 
9948 		if (sin->sin_family != AF_INET6)
9949 			return (EAFNOSUPPORT);
9950 
9951 		sin6 = (sin6_t *)sin;
9952 		v6addr = sin6->sin6_addr;
9953 
9954 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9955 			return (EADDRNOTAVAIL);
9956 	} else {
9957 		ipaddr_t addr;
9958 
9959 		if (sin->sin_family != AF_INET)
9960 			return (EAFNOSUPPORT);
9961 
9962 		addr = sin->sin_addr.s_addr;
9963 		if (addr != INADDR_ANY &&
9964 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
9965 			return (EADDRNOTAVAIL);
9966 		}
9967 
9968 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9969 	}
9970 
9971 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9972 		return (0);	/* No change */
9973 
9974 	if (ipif->ipif_flags & IPIF_UP) {
9975 		/*
9976 		 * If the interface is already marked up,
9977 		 * we call ipif_down which will take care
9978 		 * of ditching any IREs that have been set
9979 		 * up based on the old pp dst address.
9980 		 */
9981 		err = ipif_logical_down(ipif, q, mp);
9982 		if (err == EINPROGRESS)
9983 			return (err);
9984 		(void) ipif_down_tail(ipif);
9985 		need_up = B_TRUE;
9986 	}
9987 	/*
9988 	 * could return EINPROGRESS. If so ioctl will complete in
9989 	 * ip_rput_dlpi_writer
9990 	 */
9991 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9992 	return (err);
9993 }
9994 
9995 static int
9996 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9997     boolean_t need_up)
9998 {
9999 	in6_addr_t v6addr;
10000 	ill_t	*ill = ipif->ipif_ill;
10001 	int	err = 0;
10002 	boolean_t need_dl_down;
10003 	boolean_t need_arp_down;
10004 
10005 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10006 	    ipif->ipif_id, (void *)ipif));
10007 
10008 	/* Must cancel any pending timer before taking the ill_lock */
10009 	if (ipif->ipif_recovery_id != 0)
10010 		(void) untimeout(ipif->ipif_recovery_id);
10011 	ipif->ipif_recovery_id = 0;
10012 
10013 	if (ipif->ipif_isv6) {
10014 		sin6_t *sin6;
10015 
10016 		sin6 = (sin6_t *)sin;
10017 		v6addr = sin6->sin6_addr;
10018 	} else {
10019 		ipaddr_t addr;
10020 
10021 		addr = sin->sin_addr.s_addr;
10022 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10023 	}
10024 	mutex_enter(&ill->ill_lock);
10025 	/* Set point to point destination address. */
10026 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10027 		/*
10028 		 * Allow this as a means of creating logical
10029 		 * pt-pt interfaces on top of e.g. an Ethernet.
10030 		 * XXX Undocumented HACK for testing.
10031 		 * pt-pt interfaces are created with NUD disabled.
10032 		 */
10033 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10034 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10035 		if (ipif->ipif_isv6)
10036 			ill->ill_flags |= ILLF_NONUD;
10037 	}
10038 
10039 	/*
10040 	 * If the interface was previously marked as a duplicate, then since
10041 	 * we've now got a "new" address, it should no longer be considered a
10042 	 * duplicate -- even if the "new" address is the same as the old one.
10043 	 * Note that if all ipifs are down, we may have a pending ARP down
10044 	 * event to handle.
10045 	 */
10046 	need_dl_down = need_arp_down = B_FALSE;
10047 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10048 		need_arp_down = !need_up;
10049 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10050 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10051 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10052 			need_dl_down = B_TRUE;
10053 		}
10054 	}
10055 
10056 	/*
10057 	 * If we've just manually set the IPv6 destination link-local address
10058 	 * (0th ipif), tag the ill so that future updates to the destination
10059 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10060 	 * result in this address getting automatically reconfigured from
10061 	 * under the administrator.
10062 	 */
10063 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10064 		ill->ill_manual_dst_linklocal = 1;
10065 
10066 	/* Set the new address. */
10067 	ipif->ipif_v6pp_dst_addr = v6addr;
10068 	/* Make sure subnet tracks pp_dst */
10069 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10070 	mutex_exit(&ill->ill_lock);
10071 
10072 	if (need_up) {
10073 		/*
10074 		 * Now bring the interface back up.  If this
10075 		 * is the only IPIF for the ILL, ipif_up
10076 		 * will have to re-bind to the device, so
10077 		 * we may get back EINPROGRESS, in which
10078 		 * case, this IOCTL will get completed in
10079 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10080 		 */
10081 		err = ipif_up(ipif, q, mp);
10082 	}
10083 
10084 	if (need_dl_down)
10085 		ill_dl_down(ill);
10086 	if (need_arp_down && !ipif->ipif_isv6)
10087 		(void) ipif_arp_down(ipif);
10088 
10089 	return (err);
10090 }
10091 
10092 /*
10093  * Restart entry point to restart the dstaddress set operation after the
10094  * refcounts have dropped to zero.
10095  */
10096 /* ARGSUSED */
10097 int
10098 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10099     ip_ioctl_cmd_t *ipip, void *ifreq)
10100 {
10101 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10102 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10103 	(void) ipif_down_tail(ipif);
10104 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10105 }
10106 
10107 /* ARGSUSED */
10108 int
10109 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10110     ip_ioctl_cmd_t *ipip, void *if_req)
10111 {
10112 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10113 
10114 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10115 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10116 	/*
10117 	 * Get point to point destination address. The addresses can't
10118 	 * change since we hold a reference to the ipif.
10119 	 */
10120 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10121 		return (EADDRNOTAVAIL);
10122 
10123 	if (ipif->ipif_isv6) {
10124 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10125 		*sin6 = sin6_null;
10126 		sin6->sin6_family = AF_INET6;
10127 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10128 	} else {
10129 		*sin = sin_null;
10130 		sin->sin_family = AF_INET;
10131 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10132 	}
10133 	return (0);
10134 }
10135 
10136 /*
10137  * Check which flags will change by the given flags being set
10138  * silently ignore flags which userland is not allowed to control.
10139  * (Because these flags may change between SIOCGLIFFLAGS and
10140  * SIOCSLIFFLAGS, and that's outside of userland's control,
10141  * we need to silently ignore them rather than fail.)
10142  */
10143 static void
10144 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10145     uint64_t *offp)
10146 {
10147 	ill_t		*ill = ipif->ipif_ill;
10148 	phyint_t 	*phyi = ill->ill_phyint;
10149 	uint64_t	cantchange_flags, intf_flags;
10150 	uint64_t	turn_on, turn_off;
10151 
10152 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10153 	cantchange_flags = IFF_CANTCHANGE;
10154 	if (IS_IPMP(ill))
10155 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10156 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10157 	turn_off = intf_flags & turn_on;
10158 	turn_on ^= turn_off;
10159 	*onp = turn_on;
10160 	*offp = turn_off;
10161 }
10162 
10163 /*
10164  * Set interface flags.  Many flags require special handling (e.g.,
10165  * bringing the interface down); see below for details.
10166  *
10167  * NOTE : We really don't enforce that ipif_id zero should be used
10168  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10169  *	  is because applications generally does SICGLIFFLAGS and
10170  *	  ORs in the new flags (that affects the logical) and does a
10171  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10172  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10173  *	  flags that will be turned on is correct with respect to
10174  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10175  */
10176 /* ARGSUSED */
10177 int
10178 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10179     ip_ioctl_cmd_t *ipip, void *if_req)
10180 {
10181 	uint64_t turn_on;
10182 	uint64_t turn_off;
10183 	int	err = 0;
10184 	phyint_t *phyi;
10185 	ill_t *ill;
10186 	conn_t *connp;
10187 	uint64_t intf_flags;
10188 	boolean_t phyint_flags_modified = B_FALSE;
10189 	uint64_t flags;
10190 	struct ifreq *ifr;
10191 	struct lifreq *lifr;
10192 	boolean_t set_linklocal = B_FALSE;
10193 
10194 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10195 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10196 
10197 	ASSERT(IAM_WRITER_IPIF(ipif));
10198 
10199 	ill = ipif->ipif_ill;
10200 	phyi = ill->ill_phyint;
10201 
10202 	if (ipip->ipi_cmd_type == IF_CMD) {
10203 		ifr = (struct ifreq *)if_req;
10204 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10205 	} else {
10206 		lifr = (struct lifreq *)if_req;
10207 		flags = lifr->lifr_flags;
10208 	}
10209 
10210 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10211 
10212 	/*
10213 	 * Have the flags been set correctly until now?
10214 	 */
10215 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10216 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10217 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10218 	/*
10219 	 * Compare the new flags to the old, and partition
10220 	 * into those coming on and those going off.
10221 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10222 	 */
10223 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10224 		flags |= intf_flags & ~0xFFFF;
10225 
10226 	/*
10227 	 * Explicitly fail attempts to change flags that are always invalid on
10228 	 * an IPMP meta-interface.
10229 	 */
10230 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10231 		return (EINVAL);
10232 
10233 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10234 	if ((turn_on|turn_off) == 0)
10235 		return (0);	/* No change */
10236 
10237 	/*
10238 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10239 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10240 	 * allow it to be turned off.
10241 	 */
10242 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10243 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10244 		return (EINVAL);
10245 
10246 	if ((connp = Q_TO_CONN(q)) == NULL)
10247 		return (EINVAL);
10248 
10249 	/*
10250 	 * Only vrrp control socket is allowed to change IFF_UP and
10251 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10252 	 */
10253 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10254 		if (!connp->conn_isvrrp)
10255 			return (EINVAL);
10256 	}
10257 
10258 	/*
10259 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10260 	 * VRRP control socket.
10261 	 */
10262 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10263 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10264 			return (EINVAL);
10265 	}
10266 
10267 	if (turn_on & IFF_NOFAILOVER) {
10268 		turn_on |= IFF_DEPRECATED;
10269 		flags |= IFF_DEPRECATED;
10270 	}
10271 
10272 	/*
10273 	 * On underlying interfaces, only allow applications to manage test
10274 	 * addresses -- otherwise, they may get confused when the address
10275 	 * moves as part of being brought up.  Likewise, prevent an
10276 	 * application-managed test address from being converted to a data
10277 	 * address.  To prevent migration of administratively up addresses in
10278 	 * the kernel, we don't allow them to be converted either.
10279 	 */
10280 	if (IS_UNDER_IPMP(ill)) {
10281 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10282 
10283 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10284 			return (EINVAL);
10285 
10286 		if ((turn_off & IFF_NOFAILOVER) &&
10287 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10288 			return (EINVAL);
10289 	}
10290 
10291 	/*
10292 	 * Only allow IFF_TEMPORARY flag to be set on
10293 	 * IPv6 interfaces.
10294 	 */
10295 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10296 		return (EINVAL);
10297 
10298 	/*
10299 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10300 	 */
10301 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10302 		return (EINVAL);
10303 
10304 	/*
10305 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10306 	 * interfaces.  It makes no sense in that context.
10307 	 */
10308 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10309 		return (EINVAL);
10310 
10311 	/*
10312 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10313 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10314 	 * If the link local address isn't set, and can be set, it will get
10315 	 * set later on in this function.
10316 	 */
10317 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10318 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10319 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10320 		if (ipif_cant_setlinklocal(ipif))
10321 			return (EINVAL);
10322 		set_linklocal = B_TRUE;
10323 	}
10324 
10325 	/*
10326 	 * If we modify physical interface flags, we'll potentially need to
10327 	 * send up two routing socket messages for the changes (one for the
10328 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10329 	 */
10330 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10331 		phyint_flags_modified = B_TRUE;
10332 
10333 	/*
10334 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10335 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10336 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10337 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10338 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10339 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10340 	 * will not be honored.
10341 	 */
10342 	if (turn_on & PHYI_STANDBY) {
10343 		/*
10344 		 * No need to grab ill_g_usesrc_lock here; see the
10345 		 * synchronization notes in ip.c.
10346 		 */
10347 		if (ill->ill_usesrc_grp_next != NULL ||
10348 		    intf_flags & PHYI_INACTIVE)
10349 			return (EINVAL);
10350 		if (!(flags & PHYI_FAILED)) {
10351 			flags |= PHYI_INACTIVE;
10352 			turn_on |= PHYI_INACTIVE;
10353 		}
10354 	}
10355 
10356 	if (turn_off & PHYI_STANDBY) {
10357 		flags &= ~PHYI_INACTIVE;
10358 		turn_off |= PHYI_INACTIVE;
10359 	}
10360 
10361 	/*
10362 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10363 	 * would end up on.
10364 	 */
10365 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10366 	    (PHYI_FAILED | PHYI_INACTIVE))
10367 		return (EINVAL);
10368 
10369 	/*
10370 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10371 	 * status of the interface.
10372 	 */
10373 	if ((turn_on | turn_off) & ILLF_ROUTER)
10374 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10375 
10376 	/*
10377 	 * If the interface is not UP and we are not going to
10378 	 * bring it UP, record the flags and return. When the
10379 	 * interface comes UP later, the right actions will be
10380 	 * taken.
10381 	 */
10382 	if (!(ipif->ipif_flags & IPIF_UP) &&
10383 	    !(turn_on & IPIF_UP)) {
10384 		/* Record new flags in their respective places. */
10385 		mutex_enter(&ill->ill_lock);
10386 		mutex_enter(&ill->ill_phyint->phyint_lock);
10387 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10388 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10389 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10390 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10391 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10392 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10393 		mutex_exit(&ill->ill_lock);
10394 		mutex_exit(&ill->ill_phyint->phyint_lock);
10395 
10396 		/*
10397 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10398 		 * same to the kernel: if any of them has been set by
10399 		 * userland, the interface cannot be used for data traffic.
10400 		 */
10401 		if ((turn_on|turn_off) &
10402 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10403 			ASSERT(!IS_IPMP(ill));
10404 			/*
10405 			 * It's possible the ill is part of an "anonymous"
10406 			 * IPMP group rather than a real group.  In that case,
10407 			 * there are no other interfaces in the group and thus
10408 			 * no need to call ipmp_phyint_refresh_active().
10409 			 */
10410 			if (IS_UNDER_IPMP(ill))
10411 				ipmp_phyint_refresh_active(phyi);
10412 		}
10413 
10414 		if (phyint_flags_modified) {
10415 			if (phyi->phyint_illv4 != NULL) {
10416 				ip_rts_ifmsg(phyi->phyint_illv4->
10417 				    ill_ipif, RTSQ_DEFAULT);
10418 			}
10419 			if (phyi->phyint_illv6 != NULL) {
10420 				ip_rts_ifmsg(phyi->phyint_illv6->
10421 				    ill_ipif, RTSQ_DEFAULT);
10422 			}
10423 		}
10424 		/* The default multicast interface might have changed */
10425 		ire_increment_multicast_generation(ill->ill_ipst,
10426 		    ill->ill_isv6);
10427 
10428 		return (0);
10429 	} else if (set_linklocal) {
10430 		mutex_enter(&ill->ill_lock);
10431 		if (set_linklocal)
10432 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10433 		mutex_exit(&ill->ill_lock);
10434 	}
10435 
10436 	/*
10437 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10438 	 * or point-to-point interfaces with an unspecified destination. We do
10439 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10440 	 * have a subnet assigned, which is how in.ndpd currently manages its
10441 	 * onlink prefix list when no addresses are configured with those
10442 	 * prefixes.
10443 	 */
10444 	if (ipif->ipif_isv6 &&
10445 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10446 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10447 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10448 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10449 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10450 		return (EINVAL);
10451 	}
10452 
10453 	/*
10454 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10455 	 * from being brought up.
10456 	 */
10457 	if (!ipif->ipif_isv6 &&
10458 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10459 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10460 		return (EINVAL);
10461 	}
10462 
10463 	/*
10464 	 * If we are going to change one or more of the flags that are
10465 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10466 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10467 	 * IPIF_NOFAILOVER, we will take special action.  This is
10468 	 * done by bring the ipif down, changing the flags and bringing
10469 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10470 	 * back up will trigger the address to be moved.
10471 	 *
10472 	 * If we are going to change IFF_NOACCEPT, we need to bring
10473 	 * all the ipifs down then bring them up again.	 The act of
10474 	 * bringing all the ipifs back up will trigger the local
10475 	 * ires being recreated with "no_accept" set/cleared.
10476 	 *
10477 	 * Note that ILLF_NOACCEPT is always set separately from the
10478 	 * other flags.
10479 	 */
10480 	if ((turn_on|turn_off) &
10481 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10482 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10483 	    IPIF_NOFAILOVER)) {
10484 		/*
10485 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10486 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10487 		 * entries shared between multiple ipifs on the same subnet.
10488 		 */
10489 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10490 		    !(turn_off & IPIF_UP)) {
10491 			if (ipif->ipif_flags & IPIF_UP)
10492 				ill->ill_logical_down = 1;
10493 			turn_on &= ~IPIF_UP;
10494 		}
10495 		err = ipif_down(ipif, q, mp);
10496 		ip1dbg(("ipif_down returns %d err ", err));
10497 		if (err == EINPROGRESS)
10498 			return (err);
10499 		(void) ipif_down_tail(ipif);
10500 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10501 		/*
10502 		 * If we can quiesce the ill, then continue.  If not, then
10503 		 * ip_sioctl_flags_tail() will be called from
10504 		 * ipif_ill_refrele_tail().
10505 		 */
10506 		ill_down_ipifs(ill, B_TRUE);
10507 
10508 		mutex_enter(&connp->conn_lock);
10509 		mutex_enter(&ill->ill_lock);
10510 		if (!ill_is_quiescent(ill)) {
10511 			boolean_t success;
10512 
10513 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10514 			    q, mp, ILL_DOWN);
10515 			mutex_exit(&ill->ill_lock);
10516 			mutex_exit(&connp->conn_lock);
10517 			return (success ? EINPROGRESS : EINTR);
10518 		}
10519 		mutex_exit(&ill->ill_lock);
10520 		mutex_exit(&connp->conn_lock);
10521 	}
10522 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10523 }
10524 
10525 static int
10526 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10527 {
10528 	ill_t	*ill;
10529 	phyint_t *phyi;
10530 	uint64_t turn_on, turn_off;
10531 	boolean_t phyint_flags_modified = B_FALSE;
10532 	int	err = 0;
10533 	boolean_t set_linklocal = B_FALSE;
10534 
10535 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10536 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10537 
10538 	ASSERT(IAM_WRITER_IPIF(ipif));
10539 
10540 	ill = ipif->ipif_ill;
10541 	phyi = ill->ill_phyint;
10542 
10543 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10544 
10545 	/*
10546 	 * IFF_UP is handled separately.
10547 	 */
10548 	turn_on &= ~IFF_UP;
10549 	turn_off &= ~IFF_UP;
10550 
10551 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10552 		phyint_flags_modified = B_TRUE;
10553 
10554 	/*
10555 	 * Now we change the flags. Track current value of
10556 	 * other flags in their respective places.
10557 	 */
10558 	mutex_enter(&ill->ill_lock);
10559 	mutex_enter(&phyi->phyint_lock);
10560 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10561 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10562 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10563 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10564 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10565 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10566 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10567 		set_linklocal = B_TRUE;
10568 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10569 	}
10570 
10571 	mutex_exit(&ill->ill_lock);
10572 	mutex_exit(&phyi->phyint_lock);
10573 
10574 	if (set_linklocal)
10575 		(void) ipif_setlinklocal(ipif);
10576 
10577 	/*
10578 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10579 	 * the kernel: if any of them has been set by userland, the interface
10580 	 * cannot be used for data traffic.
10581 	 */
10582 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10583 		ASSERT(!IS_IPMP(ill));
10584 		/*
10585 		 * It's possible the ill is part of an "anonymous" IPMP group
10586 		 * rather than a real group.  In that case, there are no other
10587 		 * interfaces in the group and thus no need for us to call
10588 		 * ipmp_phyint_refresh_active().
10589 		 */
10590 		if (IS_UNDER_IPMP(ill))
10591 			ipmp_phyint_refresh_active(phyi);
10592 	}
10593 
10594 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10595 		/*
10596 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10597 		 * ipifs that were brought down.
10598 		 *
10599 		 * The routing sockets messages are sent as the result
10600 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10601 		 * as well.
10602 		 */
10603 		err = ill_up_ipifs(ill, q, mp);
10604 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10605 		/*
10606 		 * XXX ipif_up really does not know whether a phyint flags
10607 		 * was modified or not. So, it sends up information on
10608 		 * only one routing sockets message. As we don't bring up
10609 		 * the interface and also set PHYI_ flags simultaneously
10610 		 * it should be okay.
10611 		 */
10612 		err = ipif_up(ipif, q, mp);
10613 	} else {
10614 		/*
10615 		 * Make sure routing socket sees all changes to the flags.
10616 		 * ipif_up_done* handles this when we use ipif_up.
10617 		 */
10618 		if (phyint_flags_modified) {
10619 			if (phyi->phyint_illv4 != NULL) {
10620 				ip_rts_ifmsg(phyi->phyint_illv4->
10621 				    ill_ipif, RTSQ_DEFAULT);
10622 			}
10623 			if (phyi->phyint_illv6 != NULL) {
10624 				ip_rts_ifmsg(phyi->phyint_illv6->
10625 				    ill_ipif, RTSQ_DEFAULT);
10626 			}
10627 		} else {
10628 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10629 		}
10630 		/*
10631 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10632 		 * this in need_up case.
10633 		 */
10634 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10635 	}
10636 
10637 	/* The default multicast interface might have changed */
10638 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10639 	return (err);
10640 }
10641 
10642 /*
10643  * Restart the flags operation now that the refcounts have dropped to zero.
10644  */
10645 /* ARGSUSED */
10646 int
10647 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10648     ip_ioctl_cmd_t *ipip, void *if_req)
10649 {
10650 	uint64_t flags;
10651 	struct ifreq *ifr = if_req;
10652 	struct lifreq *lifr = if_req;
10653 	uint64_t turn_on, turn_off;
10654 
10655 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10656 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10657 
10658 	if (ipip->ipi_cmd_type == IF_CMD) {
10659 		/* cast to uint16_t prevents unwanted sign extension */
10660 		flags = (uint16_t)ifr->ifr_flags;
10661 	} else {
10662 		flags = lifr->lifr_flags;
10663 	}
10664 
10665 	/*
10666 	 * If this function call is a result of the ILLF_NOACCEPT flag
10667 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10668 	 */
10669 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10670 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10671 		(void) ipif_down_tail(ipif);
10672 
10673 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10674 }
10675 
10676 /*
10677  * Can operate on either a module or a driver queue.
10678  */
10679 /* ARGSUSED */
10680 int
10681 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10682     ip_ioctl_cmd_t *ipip, void *if_req)
10683 {
10684 	/*
10685 	 * Has the flags been set correctly till now ?
10686 	 */
10687 	ill_t *ill = ipif->ipif_ill;
10688 	phyint_t *phyi = ill->ill_phyint;
10689 
10690 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10691 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10692 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10693 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10694 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10695 
10696 	/*
10697 	 * Need a lock since some flags can be set even when there are
10698 	 * references to the ipif.
10699 	 */
10700 	mutex_enter(&ill->ill_lock);
10701 	if (ipip->ipi_cmd_type == IF_CMD) {
10702 		struct ifreq *ifr = (struct ifreq *)if_req;
10703 
10704 		/* Get interface flags (low 16 only). */
10705 		ifr->ifr_flags = ((ipif->ipif_flags |
10706 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10707 	} else {
10708 		struct lifreq *lifr = (struct lifreq *)if_req;
10709 
10710 		/* Get interface flags. */
10711 		lifr->lifr_flags = ipif->ipif_flags |
10712 		    ill->ill_flags | phyi->phyint_flags;
10713 	}
10714 	mutex_exit(&ill->ill_lock);
10715 	return (0);
10716 }
10717 
10718 /*
10719  * We allow the MTU to be set on an ILL, but not have it be different
10720  * for different IPIFs since we don't actually send packets on IPIFs.
10721  */
10722 /* ARGSUSED */
10723 int
10724 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10725     ip_ioctl_cmd_t *ipip, void *if_req)
10726 {
10727 	int mtu;
10728 	int ip_min_mtu;
10729 	struct ifreq	*ifr;
10730 	struct lifreq *lifr;
10731 	ill_t	*ill;
10732 
10733 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10734 	    ipif->ipif_id, (void *)ipif));
10735 	if (ipip->ipi_cmd_type == IF_CMD) {
10736 		ifr = (struct ifreq *)if_req;
10737 		mtu = ifr->ifr_metric;
10738 	} else {
10739 		lifr = (struct lifreq *)if_req;
10740 		mtu = lifr->lifr_mtu;
10741 	}
10742 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10743 	if (ipif->ipif_id != 0)
10744 		return (EINVAL);
10745 
10746 	ill = ipif->ipif_ill;
10747 	if (ipif->ipif_isv6)
10748 		ip_min_mtu = IPV6_MIN_MTU;
10749 	else
10750 		ip_min_mtu = IP_MIN_MTU;
10751 
10752 	mutex_enter(&ill->ill_lock);
10753 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10754 		mutex_exit(&ill->ill_lock);
10755 		return (EINVAL);
10756 	}
10757 	/*
10758 	 * The dce and fragmentation code can handle changes to ill_mtu
10759 	 * concurrent with sending/fragmenting packets.
10760 	 */
10761 	ill->ill_mtu = mtu;
10762 	ill->ill_flags |= ILLF_FIXEDMTU;
10763 	mutex_exit(&ill->ill_lock);
10764 
10765 	/*
10766 	 * Make sure all dce_generation checks find out
10767 	 * that ill_mtu has changed.
10768 	 */
10769 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10770 
10771 	/*
10772 	 * Refresh IPMP meta-interface MTU if necessary.
10773 	 */
10774 	if (IS_UNDER_IPMP(ill))
10775 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10776 
10777 	/* Update the MTU in SCTP's list */
10778 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10779 	return (0);
10780 }
10781 
10782 /* Get interface MTU. */
10783 /* ARGSUSED */
10784 int
10785 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10786 	ip_ioctl_cmd_t *ipip, void *if_req)
10787 {
10788 	struct ifreq	*ifr;
10789 	struct lifreq	*lifr;
10790 
10791 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10792 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10793 
10794 	/*
10795 	 * We allow a get on any logical interface even though the set
10796 	 * can only be done on logical unit 0.
10797 	 */
10798 	if (ipip->ipi_cmd_type == IF_CMD) {
10799 		ifr = (struct ifreq *)if_req;
10800 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10801 	} else {
10802 		lifr = (struct lifreq *)if_req;
10803 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10804 	}
10805 	return (0);
10806 }
10807 
10808 /* Set interface broadcast address. */
10809 /* ARGSUSED2 */
10810 int
10811 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10812 	ip_ioctl_cmd_t *ipip, void *if_req)
10813 {
10814 	ipaddr_t addr;
10815 	ire_t	*ire;
10816 	ill_t		*ill = ipif->ipif_ill;
10817 	ip_stack_t	*ipst = ill->ill_ipst;
10818 
10819 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10820 	    ipif->ipif_id));
10821 
10822 	ASSERT(IAM_WRITER_IPIF(ipif));
10823 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10824 		return (EADDRNOTAVAIL);
10825 
10826 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10827 
10828 	if (sin->sin_family != AF_INET)
10829 		return (EAFNOSUPPORT);
10830 
10831 	addr = sin->sin_addr.s_addr;
10832 
10833 	if (ipif->ipif_flags & IPIF_UP) {
10834 		/*
10835 		 * If we are already up, make sure the new
10836 		 * broadcast address makes sense.  If it does,
10837 		 * there should be an IRE for it already.
10838 		 */
10839 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10840 		    ill, ipif->ipif_zoneid, NULL,
10841 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10842 		if (ire == NULL) {
10843 			return (EINVAL);
10844 		} else {
10845 			ire_refrele(ire);
10846 		}
10847 	}
10848 	/*
10849 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10850 	 * needs to already exist we never need to change the set of
10851 	 * IRE_BROADCASTs when we are UP.
10852 	 */
10853 	if (addr != ipif->ipif_brd_addr)
10854 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10855 
10856 	return (0);
10857 }
10858 
10859 /* Get interface broadcast address. */
10860 /* ARGSUSED */
10861 int
10862 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10863     ip_ioctl_cmd_t *ipip, void *if_req)
10864 {
10865 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10866 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10867 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10868 		return (EADDRNOTAVAIL);
10869 
10870 	/* IPIF_BROADCAST not possible with IPv6 */
10871 	ASSERT(!ipif->ipif_isv6);
10872 	*sin = sin_null;
10873 	sin->sin_family = AF_INET;
10874 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10875 	return (0);
10876 }
10877 
10878 /*
10879  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10880  */
10881 /* ARGSUSED */
10882 int
10883 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10884     ip_ioctl_cmd_t *ipip, void *if_req)
10885 {
10886 	int err = 0;
10887 	in6_addr_t v6mask;
10888 
10889 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10890 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10891 
10892 	ASSERT(IAM_WRITER_IPIF(ipif));
10893 
10894 	if (ipif->ipif_isv6) {
10895 		sin6_t *sin6;
10896 
10897 		if (sin->sin_family != AF_INET6)
10898 			return (EAFNOSUPPORT);
10899 
10900 		sin6 = (sin6_t *)sin;
10901 		v6mask = sin6->sin6_addr;
10902 	} else {
10903 		ipaddr_t mask;
10904 
10905 		if (sin->sin_family != AF_INET)
10906 			return (EAFNOSUPPORT);
10907 
10908 		mask = sin->sin_addr.s_addr;
10909 		if (!ip_contiguous_mask(ntohl(mask)))
10910 			return (ENOTSUP);
10911 		V4MASK_TO_V6(mask, v6mask);
10912 	}
10913 
10914 	/*
10915 	 * No big deal if the interface isn't already up, or the mask
10916 	 * isn't really changing, or this is pt-pt.
10917 	 */
10918 	if (!(ipif->ipif_flags & IPIF_UP) ||
10919 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10920 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10921 		ipif->ipif_v6net_mask = v6mask;
10922 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10923 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10924 			    ipif->ipif_v6net_mask,
10925 			    ipif->ipif_v6subnet);
10926 		}
10927 		return (0);
10928 	}
10929 	/*
10930 	 * Make sure we have valid net and subnet broadcast ire's
10931 	 * for the old netmask, if needed by other logical interfaces.
10932 	 */
10933 	err = ipif_logical_down(ipif, q, mp);
10934 	if (err == EINPROGRESS)
10935 		return (err);
10936 	(void) ipif_down_tail(ipif);
10937 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10938 	return (err);
10939 }
10940 
10941 static int
10942 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10943 {
10944 	in6_addr_t v6mask;
10945 	int err = 0;
10946 
10947 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10948 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10949 
10950 	if (ipif->ipif_isv6) {
10951 		sin6_t *sin6;
10952 
10953 		sin6 = (sin6_t *)sin;
10954 		v6mask = sin6->sin6_addr;
10955 	} else {
10956 		ipaddr_t mask;
10957 
10958 		mask = sin->sin_addr.s_addr;
10959 		V4MASK_TO_V6(mask, v6mask);
10960 	}
10961 
10962 	ipif->ipif_v6net_mask = v6mask;
10963 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10964 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10965 		    ipif->ipif_v6subnet);
10966 	}
10967 	err = ipif_up(ipif, q, mp);
10968 
10969 	if (err == 0 || err == EINPROGRESS) {
10970 		/*
10971 		 * The interface must be DL_BOUND if this packet has to
10972 		 * go out on the wire. Since we only go through a logical
10973 		 * down and are bound with the driver during an internal
10974 		 * down/up that is satisfied.
10975 		 */
10976 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10977 			/* Potentially broadcast an address mask reply. */
10978 			ipif_mask_reply(ipif);
10979 		}
10980 	}
10981 	return (err);
10982 }
10983 
10984 /* ARGSUSED */
10985 int
10986 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10987     ip_ioctl_cmd_t *ipip, void *if_req)
10988 {
10989 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10990 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10991 	(void) ipif_down_tail(ipif);
10992 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10993 }
10994 
10995 /* Get interface net mask. */
10996 /* ARGSUSED */
10997 int
10998 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10999     ip_ioctl_cmd_t *ipip, void *if_req)
11000 {
11001 	struct lifreq *lifr = (struct lifreq *)if_req;
11002 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11003 
11004 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11005 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11006 
11007 	/*
11008 	 * net mask can't change since we have a reference to the ipif.
11009 	 */
11010 	if (ipif->ipif_isv6) {
11011 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11012 		*sin6 = sin6_null;
11013 		sin6->sin6_family = AF_INET6;
11014 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11015 		lifr->lifr_addrlen =
11016 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11017 	} else {
11018 		*sin = sin_null;
11019 		sin->sin_family = AF_INET;
11020 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11021 		if (ipip->ipi_cmd_type == LIF_CMD) {
11022 			lifr->lifr_addrlen =
11023 			    ip_mask_to_plen(ipif->ipif_net_mask);
11024 		}
11025 	}
11026 	return (0);
11027 }
11028 
11029 /* ARGSUSED */
11030 int
11031 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11032     ip_ioctl_cmd_t *ipip, void *if_req)
11033 {
11034 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11035 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11036 
11037 	/*
11038 	 * Since no applications should ever be setting metrics on underlying
11039 	 * interfaces, we explicitly fail to smoke 'em out.
11040 	 */
11041 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11042 		return (EINVAL);
11043 
11044 	/*
11045 	 * Set interface metric.  We don't use this for
11046 	 * anything but we keep track of it in case it is
11047 	 * important to routing applications or such.
11048 	 */
11049 	if (ipip->ipi_cmd_type == IF_CMD) {
11050 		struct ifreq    *ifr;
11051 
11052 		ifr = (struct ifreq *)if_req;
11053 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11054 	} else {
11055 		struct lifreq   *lifr;
11056 
11057 		lifr = (struct lifreq *)if_req;
11058 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11059 	}
11060 	return (0);
11061 }
11062 
11063 /* ARGSUSED */
11064 int
11065 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11066     ip_ioctl_cmd_t *ipip, void *if_req)
11067 {
11068 	/* Get interface metric. */
11069 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11070 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11071 
11072 	if (ipip->ipi_cmd_type == IF_CMD) {
11073 		struct ifreq    *ifr;
11074 
11075 		ifr = (struct ifreq *)if_req;
11076 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11077 	} else {
11078 		struct lifreq   *lifr;
11079 
11080 		lifr = (struct lifreq *)if_req;
11081 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11082 	}
11083 
11084 	return (0);
11085 }
11086 
11087 /* ARGSUSED */
11088 int
11089 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11090     ip_ioctl_cmd_t *ipip, void *if_req)
11091 {
11092 	int	arp_muxid;
11093 
11094 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11095 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11096 	/*
11097 	 * Set the muxid returned from I_PLINK.
11098 	 */
11099 	if (ipip->ipi_cmd_type == IF_CMD) {
11100 		struct ifreq *ifr = (struct ifreq *)if_req;
11101 
11102 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11103 		arp_muxid = ifr->ifr_arp_muxid;
11104 	} else {
11105 		struct lifreq *lifr = (struct lifreq *)if_req;
11106 
11107 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11108 		arp_muxid = lifr->lifr_arp_muxid;
11109 	}
11110 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11111 	return (0);
11112 }
11113 
11114 /* ARGSUSED */
11115 int
11116 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11117     ip_ioctl_cmd_t *ipip, void *if_req)
11118 {
11119 	int	arp_muxid = 0;
11120 
11121 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11122 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11123 	/*
11124 	 * Get the muxid saved in ill for I_PUNLINK.
11125 	 */
11126 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11127 	if (ipip->ipi_cmd_type == IF_CMD) {
11128 		struct ifreq *ifr = (struct ifreq *)if_req;
11129 
11130 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11131 		ifr->ifr_arp_muxid = arp_muxid;
11132 	} else {
11133 		struct lifreq *lifr = (struct lifreq *)if_req;
11134 
11135 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11136 		lifr->lifr_arp_muxid = arp_muxid;
11137 	}
11138 	return (0);
11139 }
11140 
11141 /*
11142  * Set the subnet prefix. Does not modify the broadcast address.
11143  */
11144 /* ARGSUSED */
11145 int
11146 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11147     ip_ioctl_cmd_t *ipip, void *if_req)
11148 {
11149 	int err = 0;
11150 	in6_addr_t v6addr;
11151 	in6_addr_t v6mask;
11152 	boolean_t need_up = B_FALSE;
11153 	int addrlen;
11154 
11155 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11156 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11157 
11158 	ASSERT(IAM_WRITER_IPIF(ipif));
11159 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11160 
11161 	if (ipif->ipif_isv6) {
11162 		sin6_t *sin6;
11163 
11164 		if (sin->sin_family != AF_INET6)
11165 			return (EAFNOSUPPORT);
11166 
11167 		sin6 = (sin6_t *)sin;
11168 		v6addr = sin6->sin6_addr;
11169 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11170 			return (EADDRNOTAVAIL);
11171 	} else {
11172 		ipaddr_t addr;
11173 
11174 		if (sin->sin_family != AF_INET)
11175 			return (EAFNOSUPPORT);
11176 
11177 		addr = sin->sin_addr.s_addr;
11178 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11179 			return (EADDRNOTAVAIL);
11180 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11181 		/* Add 96 bits */
11182 		addrlen += IPV6_ABITS - IP_ABITS;
11183 	}
11184 
11185 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11186 		return (EINVAL);
11187 
11188 	/* Check if bits in the address is set past the mask */
11189 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11190 		return (EINVAL);
11191 
11192 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11193 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11194 		return (0);	/* No change */
11195 
11196 	if (ipif->ipif_flags & IPIF_UP) {
11197 		/*
11198 		 * If the interface is already marked up,
11199 		 * we call ipif_down which will take care
11200 		 * of ditching any IREs that have been set
11201 		 * up based on the old interface address.
11202 		 */
11203 		err = ipif_logical_down(ipif, q, mp);
11204 		if (err == EINPROGRESS)
11205 			return (err);
11206 		(void) ipif_down_tail(ipif);
11207 		need_up = B_TRUE;
11208 	}
11209 
11210 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11211 	return (err);
11212 }
11213 
11214 static int
11215 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11216     queue_t *q, mblk_t *mp, boolean_t need_up)
11217 {
11218 	ill_t	*ill = ipif->ipif_ill;
11219 	int	err = 0;
11220 
11221 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11222 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11223 
11224 	/* Set the new address. */
11225 	mutex_enter(&ill->ill_lock);
11226 	ipif->ipif_v6net_mask = v6mask;
11227 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11228 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11229 		    ipif->ipif_v6subnet);
11230 	}
11231 	mutex_exit(&ill->ill_lock);
11232 
11233 	if (need_up) {
11234 		/*
11235 		 * Now bring the interface back up.  If this
11236 		 * is the only IPIF for the ILL, ipif_up
11237 		 * will have to re-bind to the device, so
11238 		 * we may get back EINPROGRESS, in which
11239 		 * case, this IOCTL will get completed in
11240 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11241 		 */
11242 		err = ipif_up(ipif, q, mp);
11243 		if (err == EINPROGRESS)
11244 			return (err);
11245 	}
11246 	return (err);
11247 }
11248 
11249 /* ARGSUSED */
11250 int
11251 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11252     ip_ioctl_cmd_t *ipip, void *if_req)
11253 {
11254 	int	addrlen;
11255 	in6_addr_t v6addr;
11256 	in6_addr_t v6mask;
11257 	struct lifreq *lifr = (struct lifreq *)if_req;
11258 
11259 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11260 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11261 	(void) ipif_down_tail(ipif);
11262 
11263 	addrlen = lifr->lifr_addrlen;
11264 	if (ipif->ipif_isv6) {
11265 		sin6_t *sin6;
11266 
11267 		sin6 = (sin6_t *)sin;
11268 		v6addr = sin6->sin6_addr;
11269 	} else {
11270 		ipaddr_t addr;
11271 
11272 		addr = sin->sin_addr.s_addr;
11273 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11274 		addrlen += IPV6_ABITS - IP_ABITS;
11275 	}
11276 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11277 
11278 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11279 }
11280 
11281 /* ARGSUSED */
11282 int
11283 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11284     ip_ioctl_cmd_t *ipip, void *if_req)
11285 {
11286 	struct lifreq *lifr = (struct lifreq *)if_req;
11287 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11288 
11289 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11290 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11291 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11292 
11293 	if (ipif->ipif_isv6) {
11294 		*sin6 = sin6_null;
11295 		sin6->sin6_family = AF_INET6;
11296 		sin6->sin6_addr = ipif->ipif_v6subnet;
11297 		lifr->lifr_addrlen =
11298 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11299 	} else {
11300 		*sin = sin_null;
11301 		sin->sin_family = AF_INET;
11302 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11303 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11304 	}
11305 	return (0);
11306 }
11307 
11308 /*
11309  * Set the IPv6 address token.
11310  */
11311 /* ARGSUSED */
11312 int
11313 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11314     ip_ioctl_cmd_t *ipi, void *if_req)
11315 {
11316 	ill_t *ill = ipif->ipif_ill;
11317 	int err;
11318 	in6_addr_t v6addr;
11319 	in6_addr_t v6mask;
11320 	boolean_t need_up = B_FALSE;
11321 	int i;
11322 	sin6_t *sin6 = (sin6_t *)sin;
11323 	struct lifreq *lifr = (struct lifreq *)if_req;
11324 	int addrlen;
11325 
11326 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11327 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11328 	ASSERT(IAM_WRITER_IPIF(ipif));
11329 
11330 	addrlen = lifr->lifr_addrlen;
11331 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11332 	if (ipif->ipif_id != 0)
11333 		return (EINVAL);
11334 
11335 	if (!ipif->ipif_isv6)
11336 		return (EINVAL);
11337 
11338 	if (addrlen > IPV6_ABITS)
11339 		return (EINVAL);
11340 
11341 	v6addr = sin6->sin6_addr;
11342 
11343 	/*
11344 	 * The length of the token is the length from the end.  To get
11345 	 * the proper mask for this, compute the mask of the bits not
11346 	 * in the token; ie. the prefix, and then xor to get the mask.
11347 	 */
11348 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11349 		return (EINVAL);
11350 	for (i = 0; i < 4; i++) {
11351 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11352 	}
11353 
11354 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11355 	    ill->ill_token_length == addrlen)
11356 		return (0);	/* No change */
11357 
11358 	if (ipif->ipif_flags & IPIF_UP) {
11359 		err = ipif_logical_down(ipif, q, mp);
11360 		if (err == EINPROGRESS)
11361 			return (err);
11362 		(void) ipif_down_tail(ipif);
11363 		need_up = B_TRUE;
11364 	}
11365 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11366 	return (err);
11367 }
11368 
11369 static int
11370 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11371     mblk_t *mp, boolean_t need_up)
11372 {
11373 	in6_addr_t v6addr;
11374 	in6_addr_t v6mask;
11375 	ill_t	*ill = ipif->ipif_ill;
11376 	int	i;
11377 	int	err = 0;
11378 
11379 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11380 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11381 	v6addr = sin6->sin6_addr;
11382 	/*
11383 	 * The length of the token is the length from the end.  To get
11384 	 * the proper mask for this, compute the mask of the bits not
11385 	 * in the token; ie. the prefix, and then xor to get the mask.
11386 	 */
11387 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11388 	for (i = 0; i < 4; i++)
11389 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11390 
11391 	mutex_enter(&ill->ill_lock);
11392 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11393 	ill->ill_token_length = addrlen;
11394 	ill->ill_manual_token = 1;
11395 
11396 	/* Reconfigure the link-local address based on this new token */
11397 	ipif_setlinklocal(ill->ill_ipif);
11398 
11399 	mutex_exit(&ill->ill_lock);
11400 
11401 	if (need_up) {
11402 		/*
11403 		 * Now bring the interface back up.  If this
11404 		 * is the only IPIF for the ILL, ipif_up
11405 		 * will have to re-bind to the device, so
11406 		 * we may get back EINPROGRESS, in which
11407 		 * case, this IOCTL will get completed in
11408 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11409 		 */
11410 		err = ipif_up(ipif, q, mp);
11411 		if (err == EINPROGRESS)
11412 			return (err);
11413 	}
11414 	return (err);
11415 }
11416 
11417 /* ARGSUSED */
11418 int
11419 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11420     ip_ioctl_cmd_t *ipi, void *if_req)
11421 {
11422 	ill_t *ill;
11423 	sin6_t *sin6 = (sin6_t *)sin;
11424 	struct lifreq *lifr = (struct lifreq *)if_req;
11425 
11426 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11427 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11428 	if (ipif->ipif_id != 0)
11429 		return (EINVAL);
11430 
11431 	ill = ipif->ipif_ill;
11432 	if (!ill->ill_isv6)
11433 		return (ENXIO);
11434 
11435 	*sin6 = sin6_null;
11436 	sin6->sin6_family = AF_INET6;
11437 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11438 	sin6->sin6_addr = ill->ill_token;
11439 	lifr->lifr_addrlen = ill->ill_token_length;
11440 	return (0);
11441 }
11442 
11443 /*
11444  * Set (hardware) link specific information that might override
11445  * what was acquired through the DL_INFO_ACK.
11446  */
11447 /* ARGSUSED */
11448 int
11449 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11450     ip_ioctl_cmd_t *ipi, void *if_req)
11451 {
11452 	ill_t		*ill = ipif->ipif_ill;
11453 	int		ip_min_mtu;
11454 	struct lifreq	*lifr = (struct lifreq *)if_req;
11455 	lif_ifinfo_req_t *lir;
11456 
11457 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11458 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11459 	lir = &lifr->lifr_ifinfo;
11460 	ASSERT(IAM_WRITER_IPIF(ipif));
11461 
11462 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11463 	if (ipif->ipif_id != 0)
11464 		return (EINVAL);
11465 
11466 	/* Set interface MTU. */
11467 	if (ipif->ipif_isv6)
11468 		ip_min_mtu = IPV6_MIN_MTU;
11469 	else
11470 		ip_min_mtu = IP_MIN_MTU;
11471 
11472 	/*
11473 	 * Verify values before we set anything. Allow zero to
11474 	 * mean unspecified.
11475 	 *
11476 	 * XXX We should be able to set the user-defined lir_mtu to some value
11477 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11478 	 * ill_max_frag value tells us the max MTU that can be handled by the
11479 	 * datalink, whereas the ill_current_frag is dynamically computed for
11480 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11481 	 * since there is currently no way of distinguishing between
11482 	 * administratively fixed link mtu values (e.g., those set via
11483 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11484 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11485 	 * upper-bound.
11486 	 */
11487 	if (lir->lir_maxmtu != 0 &&
11488 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11489 	    lir->lir_maxmtu < ip_min_mtu))
11490 		return (EINVAL);
11491 	if (lir->lir_reachtime != 0 &&
11492 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11493 		return (EINVAL);
11494 	if (lir->lir_reachretrans != 0 &&
11495 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11496 		return (EINVAL);
11497 
11498 	mutex_enter(&ill->ill_lock);
11499 	/*
11500 	 * The dce and fragmentation code can handle changes to ill_mtu
11501 	 * concurrent with sending/fragmenting packets.
11502 	 */
11503 	if (lir->lir_maxmtu != 0)
11504 		ill->ill_user_mtu = lir->lir_maxmtu;
11505 
11506 	if (lir->lir_reachtime != 0)
11507 		ill->ill_reachable_time = lir->lir_reachtime;
11508 
11509 	if (lir->lir_reachretrans != 0)
11510 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11511 
11512 	ill->ill_max_hops = lir->lir_maxhops;
11513 	ill->ill_max_buf = ND_MAX_Q;
11514 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11515 		/*
11516 		 * ill_mtu is the actual interface MTU, obtained as the min
11517 		 * of user-configured mtu and the value announced by the
11518 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11519 		 * we have already made the choice of requiring
11520 		 * ill_user_mtu < ill_current_frag by the time we get here,
11521 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11522 		 * here.
11523 		 */
11524 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11525 	}
11526 	mutex_exit(&ill->ill_lock);
11527 
11528 	/*
11529 	 * Make sure all dce_generation checks find out
11530 	 * that ill_mtu has changed.
11531 	 */
11532 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11533 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11534 
11535 	/*
11536 	 * Refresh IPMP meta-interface MTU if necessary.
11537 	 */
11538 	if (IS_UNDER_IPMP(ill))
11539 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11540 
11541 	return (0);
11542 }
11543 
11544 /* ARGSUSED */
11545 int
11546 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11547     ip_ioctl_cmd_t *ipi, void *if_req)
11548 {
11549 	struct lif_ifinfo_req *lir;
11550 	ill_t *ill = ipif->ipif_ill;
11551 
11552 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11553 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11554 	if (ipif->ipif_id != 0)
11555 		return (EINVAL);
11556 
11557 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11558 	lir->lir_maxhops = ill->ill_max_hops;
11559 	lir->lir_reachtime = ill->ill_reachable_time;
11560 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11561 	lir->lir_maxmtu = ill->ill_mtu;
11562 
11563 	return (0);
11564 }
11565 
11566 /*
11567  * Return best guess as to the subnet mask for the specified address.
11568  * Based on the subnet masks for all the configured interfaces.
11569  *
11570  * We end up returning a zero mask in the case of default, multicast or
11571  * experimental.
11572  */
11573 static ipaddr_t
11574 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11575 {
11576 	ipaddr_t net_mask;
11577 	ill_t	*ill;
11578 	ipif_t	*ipif;
11579 	ill_walk_context_t ctx;
11580 	ipif_t	*fallback_ipif = NULL;
11581 
11582 	net_mask = ip_net_mask(addr);
11583 	if (net_mask == 0) {
11584 		*ipifp = NULL;
11585 		return (0);
11586 	}
11587 
11588 	/* Let's check to see if this is maybe a local subnet route. */
11589 	/* this function only applies to IPv4 interfaces */
11590 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11591 	ill = ILL_START_WALK_V4(&ctx, ipst);
11592 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11593 		mutex_enter(&ill->ill_lock);
11594 		for (ipif = ill->ill_ipif; ipif != NULL;
11595 		    ipif = ipif->ipif_next) {
11596 			if (IPIF_IS_CONDEMNED(ipif))
11597 				continue;
11598 			if (!(ipif->ipif_flags & IPIF_UP))
11599 				continue;
11600 			if ((ipif->ipif_subnet & net_mask) ==
11601 			    (addr & net_mask)) {
11602 				/*
11603 				 * Don't trust pt-pt interfaces if there are
11604 				 * other interfaces.
11605 				 */
11606 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11607 					if (fallback_ipif == NULL) {
11608 						ipif_refhold_locked(ipif);
11609 						fallback_ipif = ipif;
11610 					}
11611 					continue;
11612 				}
11613 
11614 				/*
11615 				 * Fine. Just assume the same net mask as the
11616 				 * directly attached subnet interface is using.
11617 				 */
11618 				ipif_refhold_locked(ipif);
11619 				mutex_exit(&ill->ill_lock);
11620 				rw_exit(&ipst->ips_ill_g_lock);
11621 				if (fallback_ipif != NULL)
11622 					ipif_refrele(fallback_ipif);
11623 				*ipifp = ipif;
11624 				return (ipif->ipif_net_mask);
11625 			}
11626 		}
11627 		mutex_exit(&ill->ill_lock);
11628 	}
11629 	rw_exit(&ipst->ips_ill_g_lock);
11630 
11631 	*ipifp = fallback_ipif;
11632 	return ((fallback_ipif != NULL) ?
11633 	    fallback_ipif->ipif_net_mask : net_mask);
11634 }
11635 
11636 /*
11637  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11638  */
11639 static void
11640 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11641 {
11642 	IOCP	iocp;
11643 	ipft_t	*ipft;
11644 	ipllc_t	*ipllc;
11645 	mblk_t	*mp1;
11646 	cred_t	*cr;
11647 	int	error = 0;
11648 	conn_t	*connp;
11649 
11650 	ip1dbg(("ip_wput_ioctl"));
11651 	iocp = (IOCP)mp->b_rptr;
11652 	mp1 = mp->b_cont;
11653 	if (mp1 == NULL) {
11654 		iocp->ioc_error = EINVAL;
11655 		mp->b_datap->db_type = M_IOCNAK;
11656 		iocp->ioc_count = 0;
11657 		qreply(q, mp);
11658 		return;
11659 	}
11660 
11661 	/*
11662 	 * These IOCTLs provide various control capabilities to
11663 	 * upstream agents such as ULPs and processes.	There
11664 	 * are currently two such IOCTLs implemented.  They
11665 	 * are used by TCP to provide update information for
11666 	 * existing IREs and to forcibly delete an IRE for a
11667 	 * host that is not responding, thereby forcing an
11668 	 * attempt at a new route.
11669 	 */
11670 	iocp->ioc_error = EINVAL;
11671 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11672 		goto done;
11673 
11674 	ipllc = (ipllc_t *)mp1->b_rptr;
11675 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11676 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11677 			break;
11678 	}
11679 	/*
11680 	 * prefer credential from mblk over ioctl;
11681 	 * see ip_sioctl_copyin_setup
11682 	 */
11683 	cr = msg_getcred(mp, NULL);
11684 	if (cr == NULL)
11685 		cr = iocp->ioc_cr;
11686 
11687 	/*
11688 	 * Refhold the conn in case the request gets queued up in some lookup
11689 	 */
11690 	ASSERT(CONN_Q(q));
11691 	connp = Q_TO_CONN(q);
11692 	CONN_INC_REF(connp);
11693 	if (ipft->ipft_pfi &&
11694 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11695 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11696 		error = (*ipft->ipft_pfi)(q,
11697 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11698 	}
11699 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11700 		/*
11701 		 * CONN_OPER_PENDING_DONE happens in the function called
11702 		 * through ipft_pfi above.
11703 		 */
11704 		return;
11705 	}
11706 
11707 	CONN_OPER_PENDING_DONE(connp);
11708 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11709 		freemsg(mp);
11710 		return;
11711 	}
11712 	iocp->ioc_error = error;
11713 
11714 done:
11715 	mp->b_datap->db_type = M_IOCACK;
11716 	if (iocp->ioc_error)
11717 		iocp->ioc_count = 0;
11718 	qreply(q, mp);
11719 }
11720 
11721 /*
11722  * Assign a unique id for the ipif. This is used by sctp_addr.c
11723  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11724  */
11725 static void
11726 ipif_assign_seqid(ipif_t *ipif)
11727 {
11728 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11729 
11730 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11731 }
11732 
11733 /*
11734  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11735  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11736  * that the clone is complete -- including the seqid -- and the expectation is
11737  * that the caller will either free or overwrite `sipif' before it's unlocked.
11738  */
11739 static void
11740 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11741 {
11742 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11743 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11744 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11745 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11746 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11747 
11748 	dipif->ipif_flags = sipif->ipif_flags;
11749 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11750 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11751 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11752 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11753 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11754 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11755 
11756 	/*
11757 	 * As per the comment atop the function, we assume that these sipif
11758 	 * fields will be changed before sipif is unlocked.
11759 	 */
11760 	dipif->ipif_seqid = sipif->ipif_seqid;
11761 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11762 }
11763 
11764 /*
11765  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11766  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11767  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11768  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11769  * down (i.e., no DAD), of the same type, and unlocked.
11770  */
11771 static void
11772 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11773 {
11774 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11775 	ipxop_t *ipx = ipsq->ipsq_xop;
11776 
11777 	ASSERT(sipif != dipif);
11778 	ASSERT(sipif != virgipif);
11779 
11780 	/*
11781 	 * Grab all of the locks that protect the ipif in a defined order.
11782 	 */
11783 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11784 
11785 	ipif_clone(sipif, dipif);
11786 	if (virgipif != NULL) {
11787 		ipif_clone(virgipif, sipif);
11788 		mi_free(virgipif);
11789 	}
11790 
11791 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11792 
11793 	/*
11794 	 * Transfer ownership of the current xop, if necessary.
11795 	 */
11796 	if (ipx->ipx_current_ipif == sipif) {
11797 		ASSERT(ipx->ipx_pending_ipif == NULL);
11798 		mutex_enter(&ipx->ipx_lock);
11799 		ipx->ipx_current_ipif = dipif;
11800 		mutex_exit(&ipx->ipx_lock);
11801 	}
11802 
11803 	if (virgipif == NULL)
11804 		mi_free(sipif);
11805 }
11806 
11807 /*
11808  * checks if:
11809  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11810  *	- logical interface is within the allowed range
11811  */
11812 static int
11813 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11814 {
11815 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11816 		return (ENAMETOOLONG);
11817 
11818 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11819 		return (ERANGE);
11820 	return (0);
11821 }
11822 
11823 /*
11824  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11825  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11826  * be inserted into the first space available in the list. The value of
11827  * ipif_id will then be set to the appropriate value for its position.
11828  */
11829 static int
11830 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11831 {
11832 	ill_t *ill;
11833 	ipif_t *tipif;
11834 	ipif_t **tipifp;
11835 	int id, err;
11836 	ip_stack_t	*ipst;
11837 
11838 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11839 	    IAM_WRITER_IPIF(ipif));
11840 
11841 	ill = ipif->ipif_ill;
11842 	ASSERT(ill != NULL);
11843 	ipst = ill->ill_ipst;
11844 
11845 	/*
11846 	 * In the case of lo0:0 we already hold the ill_g_lock.
11847 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11848 	 * ipif_insert.
11849 	 */
11850 	if (acquire_g_lock)
11851 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11852 	mutex_enter(&ill->ill_lock);
11853 	id = ipif->ipif_id;
11854 	tipifp = &(ill->ill_ipif);
11855 	if (id == -1) {	/* need to find a real id */
11856 		id = 0;
11857 		while ((tipif = *tipifp) != NULL) {
11858 			ASSERT(tipif->ipif_id >= id);
11859 			if (tipif->ipif_id != id)
11860 				break; /* non-consecutive id */
11861 			id++;
11862 			tipifp = &(tipif->ipif_next);
11863 		}
11864 		if ((err = is_lifname_valid(ill, id)) != 0) {
11865 			mutex_exit(&ill->ill_lock);
11866 			if (acquire_g_lock)
11867 				rw_exit(&ipst->ips_ill_g_lock);
11868 			return (err);
11869 		}
11870 		ipif->ipif_id = id; /* assign new id */
11871 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11872 		/* we have a real id; insert ipif in the right place */
11873 		while ((tipif = *tipifp) != NULL) {
11874 			ASSERT(tipif->ipif_id != id);
11875 			if (tipif->ipif_id > id)
11876 				break; /* found correct location */
11877 			tipifp = &(tipif->ipif_next);
11878 		}
11879 	} else {
11880 		mutex_exit(&ill->ill_lock);
11881 		if (acquire_g_lock)
11882 			rw_exit(&ipst->ips_ill_g_lock);
11883 		return (err);
11884 	}
11885 
11886 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11887 
11888 	ipif->ipif_next = tipif;
11889 	*tipifp = ipif;
11890 	mutex_exit(&ill->ill_lock);
11891 	if (acquire_g_lock)
11892 		rw_exit(&ipst->ips_ill_g_lock);
11893 
11894 	return (0);
11895 }
11896 
11897 static void
11898 ipif_remove(ipif_t *ipif)
11899 {
11900 	ipif_t	**ipifp;
11901 	ill_t	*ill = ipif->ipif_ill;
11902 
11903 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11904 
11905 	mutex_enter(&ill->ill_lock);
11906 	ipifp = &ill->ill_ipif;
11907 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11908 		if (*ipifp == ipif) {
11909 			*ipifp = ipif->ipif_next;
11910 			break;
11911 		}
11912 	}
11913 	mutex_exit(&ill->ill_lock);
11914 }
11915 
11916 /*
11917  * Allocate and initialize a new interface control structure.  (Always
11918  * called as writer.)
11919  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11920  * is not part of the global linked list of ills. ipif_seqid is unique
11921  * in the system and to preserve the uniqueness, it is assigned only
11922  * when ill becomes part of the global list. At that point ill will
11923  * have a name. If it doesn't get assigned here, it will get assigned
11924  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11925  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11926  * the interface flags or any other information from the DL_INFO_ACK for
11927  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11928  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11929  * second DL_INFO_ACK comes in from the driver.
11930  */
11931 static ipif_t *
11932 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11933     boolean_t insert, int *errorp)
11934 {
11935 	int err;
11936 	ipif_t	*ipif;
11937 	ip_stack_t *ipst = ill->ill_ipst;
11938 
11939 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11940 	    ill->ill_name, id, (void *)ill));
11941 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11942 
11943 	if (errorp != NULL)
11944 		*errorp = 0;
11945 
11946 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11947 		if (errorp != NULL)
11948 			*errorp = ENOMEM;
11949 		return (NULL);
11950 	}
11951 	*ipif = ipif_zero;	/* start clean */
11952 
11953 	ipif->ipif_ill = ill;
11954 	ipif->ipif_id = id;	/* could be -1 */
11955 	/*
11956 	 * Inherit the zoneid from the ill; for the shared stack instance
11957 	 * this is always the global zone
11958 	 */
11959 	ipif->ipif_zoneid = ill->ill_zoneid;
11960 
11961 	ipif->ipif_refcnt = 0;
11962 
11963 	if (insert) {
11964 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11965 			mi_free(ipif);
11966 			if (errorp != NULL)
11967 				*errorp = err;
11968 			return (NULL);
11969 		}
11970 		/* -1 id should have been replaced by real id */
11971 		id = ipif->ipif_id;
11972 		ASSERT(id >= 0);
11973 	}
11974 
11975 	if (ill->ill_name[0] != '\0')
11976 		ipif_assign_seqid(ipif);
11977 
11978 	/*
11979 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11980 	 * (which must not exist yet because the zeroth ipif is created once
11981 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11982 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11983 	 */
11984 	if (id == 0 && IS_IPMP(ill)) {
11985 		if (ipmp_illgrp_create(ill) == NULL) {
11986 			if (insert) {
11987 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11988 				ipif_remove(ipif);
11989 				rw_exit(&ipst->ips_ill_g_lock);
11990 			}
11991 			mi_free(ipif);
11992 			if (errorp != NULL)
11993 				*errorp = ENOMEM;
11994 			return (NULL);
11995 		}
11996 	}
11997 
11998 	/*
11999 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12000 	 * not up and can't be looked up until the ioctl completes and the
12001 	 * IPIF_CHANGING flag is cleared.
12002 	 */
12003 	mutex_enter(&ill->ill_lock);
12004 
12005 	ipif->ipif_ire_type = ire_type;
12006 
12007 	if (ipif->ipif_isv6) {
12008 		ill->ill_flags |= ILLF_IPV6;
12009 	} else {
12010 		ipaddr_t inaddr_any = INADDR_ANY;
12011 
12012 		ill->ill_flags |= ILLF_IPV4;
12013 
12014 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12015 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12016 		    &ipif->ipif_v6lcl_addr);
12017 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12018 		    &ipif->ipif_v6subnet);
12019 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12020 		    &ipif->ipif_v6net_mask);
12021 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12022 		    &ipif->ipif_v6brd_addr);
12023 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12024 		    &ipif->ipif_v6pp_dst_addr);
12025 	}
12026 
12027 	/*
12028 	 * Don't set the interface flags etc. now, will do it in
12029 	 * ip_ll_subnet_defaults.
12030 	 */
12031 	if (!initialize)
12032 		goto out;
12033 
12034 	/*
12035 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12036 	 * with no underlying interfaces (and thus an unknown broadcast
12037 	 * address length), but all interfaces that can be placed into an IPMP
12038 	 * group are required to be broadcast-capable.
12039 	 */
12040 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12041 		/*
12042 		 * Later detect lack of DLPI driver multicast capability by
12043 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12044 		 */
12045 		ill->ill_flags |= ILLF_MULTICAST;
12046 		if (!ipif->ipif_isv6)
12047 			ipif->ipif_flags |= IPIF_BROADCAST;
12048 	} else {
12049 		if (ill->ill_net_type != IRE_LOOPBACK) {
12050 			if (ipif->ipif_isv6)
12051 				/*
12052 				 * Note: xresolv interfaces will eventually need
12053 				 * NOARP set here as well, but that will require
12054 				 * those external resolvers to have some
12055 				 * knowledge of that flag and act appropriately.
12056 				 * Not to be changed at present.
12057 				 */
12058 				ill->ill_flags |= ILLF_NONUD;
12059 			else
12060 				ill->ill_flags |= ILLF_NOARP;
12061 		}
12062 		if (ill->ill_phys_addr_length == 0) {
12063 			if (IS_VNI(ill)) {
12064 				ipif->ipif_flags |= IPIF_NOXMIT;
12065 			} else {
12066 				/* pt-pt supports multicast. */
12067 				ill->ill_flags |= ILLF_MULTICAST;
12068 				if (ill->ill_net_type != IRE_LOOPBACK)
12069 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12070 			}
12071 		}
12072 	}
12073 out:
12074 	mutex_exit(&ill->ill_lock);
12075 	return (ipif);
12076 }
12077 
12078 /*
12079  * Remove the neighbor cache entries associated with this logical
12080  * interface.
12081  */
12082 int
12083 ipif_arp_down(ipif_t *ipif)
12084 {
12085 	ill_t	*ill = ipif->ipif_ill;
12086 	int	err = 0;
12087 
12088 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12089 	ASSERT(IAM_WRITER_IPIF(ipif));
12090 
12091 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12092 	    ill_t *, ill, ipif_t *, ipif);
12093 	ipif_nce_down(ipif);
12094 
12095 	/*
12096 	 * If this is the last ipif that is going down and there are no
12097 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12098 	 * clean up ARP completely.
12099 	 */
12100 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12101 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12102 		/*
12103 		 * If this was the last ipif on an IPMP interface, purge any
12104 		 * static ARP entries associated with it.
12105 		 */
12106 		if (IS_IPMP(ill))
12107 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12108 
12109 		/* UNBIND, DETACH */
12110 		err = arp_ll_down(ill);
12111 	}
12112 
12113 	return (err);
12114 }
12115 
12116 /*
12117  * Get the resolver set up for a new IP address.  (Always called as writer.)
12118  * Called both for IPv4 and IPv6 interfaces, though it only does some
12119  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12120  *
12121  * The enumerated value res_act tunes the behavior:
12122  * 	* Res_act_initial: set up all the resolver structures for a new
12123  *	  IP address.
12124  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12125  *	  ARP message in defense of the address.
12126  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12127  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12128  *
12129  * Returns zero on success, or an errno upon failure.
12130  */
12131 int
12132 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12133 {
12134 	ill_t		*ill = ipif->ipif_ill;
12135 	int		err;
12136 	boolean_t	was_dup;
12137 
12138 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12139 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12140 	ASSERT(IAM_WRITER_IPIF(ipif));
12141 
12142 	was_dup = B_FALSE;
12143 	if (res_act == Res_act_initial) {
12144 		ipif->ipif_addr_ready = 0;
12145 		/*
12146 		 * We're bringing an interface up here.  There's no way that we
12147 		 * should need to shut down ARP now.
12148 		 */
12149 		mutex_enter(&ill->ill_lock);
12150 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12151 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12152 			ill->ill_ipif_dup_count--;
12153 			was_dup = B_TRUE;
12154 		}
12155 		mutex_exit(&ill->ill_lock);
12156 	}
12157 	if (ipif->ipif_recovery_id != 0)
12158 		(void) untimeout(ipif->ipif_recovery_id);
12159 	ipif->ipif_recovery_id = 0;
12160 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12161 		ipif->ipif_addr_ready = 1;
12162 		return (0);
12163 	}
12164 	/* NDP will set the ipif_addr_ready flag when it's ready */
12165 	if (ill->ill_isv6)
12166 		return (0);
12167 
12168 	err = ipif_arp_up(ipif, res_act, was_dup);
12169 	return (err);
12170 }
12171 
12172 /*
12173  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12174  * when a link has just gone back up.
12175  */
12176 static void
12177 ipif_nce_start_dad(ipif_t *ipif)
12178 {
12179 	ncec_t *ncec;
12180 	ill_t *ill = ipif->ipif_ill;
12181 	boolean_t isv6 = ill->ill_isv6;
12182 
12183 	if (isv6) {
12184 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12185 		    &ipif->ipif_v6lcl_addr);
12186 	} else {
12187 		ipaddr_t v4addr;
12188 
12189 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12190 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12191 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12192 			/*
12193 			 * If we can't contact ARP for some reason,
12194 			 * that's not really a problem.  Just send
12195 			 * out the routing socket notification that
12196 			 * DAD completion would have done, and continue.
12197 			 */
12198 			ipif_mask_reply(ipif);
12199 			ipif_up_notify(ipif);
12200 			ipif->ipif_addr_ready = 1;
12201 			return;
12202 		}
12203 
12204 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12205 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12206 	}
12207 
12208 	if (ncec == NULL) {
12209 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12210 		    (void *)ipif));
12211 		return;
12212 	}
12213 	if (!nce_restart_dad(ncec)) {
12214 		/*
12215 		 * If we can't restart DAD for some reason, that's not really a
12216 		 * problem.  Just send out the routing socket notification that
12217 		 * DAD completion would have done, and continue.
12218 		 */
12219 		ipif_up_notify(ipif);
12220 		ipif->ipif_addr_ready = 1;
12221 	}
12222 	ncec_refrele(ncec);
12223 }
12224 
12225 /*
12226  * Restart duplicate address detection on all interfaces on the given ill.
12227  *
12228  * This is called when an interface transitions from down to up
12229  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12230  *
12231  * Note that since the underlying physical link has transitioned, we must cause
12232  * at least one routing socket message to be sent here, either via DAD
12233  * completion or just by default on the first ipif.  (If we don't do this, then
12234  * in.mpathd will see long delays when doing link-based failure recovery.)
12235  */
12236 void
12237 ill_restart_dad(ill_t *ill, boolean_t went_up)
12238 {
12239 	ipif_t *ipif;
12240 
12241 	if (ill == NULL)
12242 		return;
12243 
12244 	/*
12245 	 * If layer two doesn't support duplicate address detection, then just
12246 	 * send the routing socket message now and be done with it.
12247 	 */
12248 	if (!ill->ill_isv6 && arp_no_defense) {
12249 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12250 		return;
12251 	}
12252 
12253 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12254 		if (went_up) {
12255 
12256 			if (ipif->ipif_flags & IPIF_UP) {
12257 				ipif_nce_start_dad(ipif);
12258 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12259 				/*
12260 				 * kick off the bring-up process now.
12261 				 */
12262 				ipif_do_recovery(ipif);
12263 			} else {
12264 				/*
12265 				 * Unfortunately, the first ipif is "special"
12266 				 * and represents the underlying ill in the
12267 				 * routing socket messages.  Thus, when this
12268 				 * one ipif is down, we must still notify so
12269 				 * that the user knows the IFF_RUNNING status
12270 				 * change.  (If the first ipif is up, then
12271 				 * we'll handle eventual routing socket
12272 				 * notification via DAD completion.)
12273 				 */
12274 				if (ipif == ill->ill_ipif) {
12275 					ip_rts_ifmsg(ill->ill_ipif,
12276 					    RTSQ_DEFAULT);
12277 				}
12278 			}
12279 		} else {
12280 			/*
12281 			 * After link down, we'll need to send a new routing
12282 			 * message when the link comes back, so clear
12283 			 * ipif_addr_ready.
12284 			 */
12285 			ipif->ipif_addr_ready = 0;
12286 		}
12287 	}
12288 
12289 	/*
12290 	 * If we've torn down links, then notify the user right away.
12291 	 */
12292 	if (!went_up)
12293 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12294 }
12295 
12296 static void
12297 ipsq_delete(ipsq_t *ipsq)
12298 {
12299 	ipxop_t *ipx = ipsq->ipsq_xop;
12300 
12301 	ipsq->ipsq_ipst = NULL;
12302 	ASSERT(ipsq->ipsq_phyint == NULL);
12303 	ASSERT(ipsq->ipsq_xop != NULL);
12304 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12305 	ASSERT(ipx->ipx_pending_mp == NULL);
12306 	kmem_free(ipsq, sizeof (ipsq_t));
12307 }
12308 
12309 static int
12310 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12311 {
12312 	int err = 0;
12313 	ipif_t *ipif;
12314 
12315 	if (ill == NULL)
12316 		return (0);
12317 
12318 	ASSERT(IAM_WRITER_ILL(ill));
12319 	ill->ill_up_ipifs = B_TRUE;
12320 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12321 		if (ipif->ipif_was_up) {
12322 			if (!(ipif->ipif_flags & IPIF_UP))
12323 				err = ipif_up(ipif, q, mp);
12324 			ipif->ipif_was_up = B_FALSE;
12325 			if (err != 0) {
12326 				ASSERT(err == EINPROGRESS);
12327 				return (err);
12328 			}
12329 		}
12330 	}
12331 	ill->ill_up_ipifs = B_FALSE;
12332 	return (0);
12333 }
12334 
12335 /*
12336  * This function is called to bring up all the ipifs that were up before
12337  * bringing the ill down via ill_down_ipifs().
12338  */
12339 int
12340 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12341 {
12342 	int err;
12343 
12344 	ASSERT(IAM_WRITER_ILL(ill));
12345 
12346 	if (ill->ill_replumbing) {
12347 		ill->ill_replumbing = 0;
12348 		/*
12349 		 * Send down REPLUMB_DONE notification followed by the
12350 		 * BIND_REQ on the arp stream.
12351 		 */
12352 		if (!ill->ill_isv6)
12353 			arp_send_replumb_conf(ill);
12354 	}
12355 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12356 	if (err != 0)
12357 		return (err);
12358 
12359 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12360 }
12361 
12362 /*
12363  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12364  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12365  */
12366 static void
12367 ill_down_ipifs(ill_t *ill, boolean_t logical)
12368 {
12369 	ipif_t *ipif;
12370 
12371 	ASSERT(IAM_WRITER_ILL(ill));
12372 
12373 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12374 		/*
12375 		 * We go through the ipif_down logic even if the ipif
12376 		 * is already down, since routes can be added based
12377 		 * on down ipifs. Going through ipif_down once again
12378 		 * will delete any IREs created based on these routes.
12379 		 */
12380 		if (ipif->ipif_flags & IPIF_UP)
12381 			ipif->ipif_was_up = B_TRUE;
12382 
12383 		if (logical) {
12384 			(void) ipif_logical_down(ipif, NULL, NULL);
12385 			ipif_non_duplicate(ipif);
12386 			(void) ipif_down_tail(ipif);
12387 		} else {
12388 			(void) ipif_down(ipif, NULL, NULL);
12389 		}
12390 	}
12391 }
12392 
12393 /*
12394  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12395  * a look again at valid source addresses.
12396  * This should be called each time after the set of source addresses has been
12397  * changed.
12398  */
12399 void
12400 ip_update_source_selection(ip_stack_t *ipst)
12401 {
12402 	/* We skip past SRC_GENERATION_VERIFY */
12403 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12404 	    SRC_GENERATION_VERIFY)
12405 		atomic_add_32(&ipst->ips_src_generation, 1);
12406 }
12407 
12408 /*
12409  * Finish the group join started in ip_sioctl_groupname().
12410  */
12411 /* ARGSUSED */
12412 static void
12413 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12414 {
12415 	ill_t		*ill = q->q_ptr;
12416 	phyint_t	*phyi = ill->ill_phyint;
12417 	ipmp_grp_t	*grp = phyi->phyint_grp;
12418 	ip_stack_t	*ipst = ill->ill_ipst;
12419 
12420 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12421 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12422 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12423 
12424 	if (phyi->phyint_illv4 != NULL) {
12425 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12426 		VERIFY(grp->gr_pendv4-- > 0);
12427 		rw_exit(&ipst->ips_ipmp_lock);
12428 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12429 	}
12430 	if (phyi->phyint_illv6 != NULL) {
12431 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12432 		VERIFY(grp->gr_pendv6-- > 0);
12433 		rw_exit(&ipst->ips_ipmp_lock);
12434 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12435 	}
12436 	freemsg(mp);
12437 }
12438 
12439 /*
12440  * Process an SIOCSLIFGROUPNAME request.
12441  */
12442 /* ARGSUSED */
12443 int
12444 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12445     ip_ioctl_cmd_t *ipip, void *ifreq)
12446 {
12447 	struct lifreq	*lifr = ifreq;
12448 	ill_t		*ill = ipif->ipif_ill;
12449 	ip_stack_t	*ipst = ill->ill_ipst;
12450 	phyint_t	*phyi = ill->ill_phyint;
12451 	ipmp_grp_t	*grp = phyi->phyint_grp;
12452 	mblk_t		*ipsq_mp;
12453 	int		err = 0;
12454 
12455 	/*
12456 	 * Note that phyint_grp can only change here, where we're exclusive.
12457 	 */
12458 	ASSERT(IAM_WRITER_ILL(ill));
12459 
12460 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12461 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12462 		return (EINVAL);
12463 
12464 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12465 
12466 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12467 
12468 	/*
12469 	 * If the name hasn't changed, there's nothing to do.
12470 	 */
12471 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12472 		goto unlock;
12473 
12474 	/*
12475 	 * Handle requests to rename an IPMP meta-interface.
12476 	 *
12477 	 * Note that creation of the IPMP meta-interface is handled in
12478 	 * userland through the standard plumbing sequence.  As part of the
12479 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12480 	 * the name of the interface (see ipif_set_values_tail()).
12481 	 */
12482 	if (IS_IPMP(ill)) {
12483 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12484 		goto unlock;
12485 	}
12486 
12487 	/*
12488 	 * Handle requests to add or remove an IP interface from a group.
12489 	 */
12490 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12491 		/*
12492 		 * Moves are handled by first removing the interface from
12493 		 * its existing group, and then adding it to another group.
12494 		 * So, fail if it's already in a group.
12495 		 */
12496 		if (IS_UNDER_IPMP(ill)) {
12497 			err = EALREADY;
12498 			goto unlock;
12499 		}
12500 
12501 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12502 		if (grp == NULL) {
12503 			err = ENOENT;
12504 			goto unlock;
12505 		}
12506 
12507 		/*
12508 		 * Check if the phyint and its ills are suitable for
12509 		 * inclusion into the group.
12510 		 */
12511 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12512 			goto unlock;
12513 
12514 		/*
12515 		 * Checks pass; join the group, and enqueue the remaining
12516 		 * illgrp joins for when we've become part of the group xop
12517 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12518 		 * requires an mblk_t to scribble on, and since `mp' will be
12519 		 * freed as part of completing the ioctl, allocate another.
12520 		 */
12521 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12522 			err = ENOMEM;
12523 			goto unlock;
12524 		}
12525 
12526 		/*
12527 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12528 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12529 		 * before ip_join_illgrps() is called back.  See the comments
12530 		 * in ip_sioctl_plink_ipmp() for more.
12531 		 */
12532 		if (phyi->phyint_illv4 != NULL)
12533 			grp->gr_pendv4++;
12534 		if (phyi->phyint_illv6 != NULL)
12535 			grp->gr_pendv6++;
12536 
12537 		rw_exit(&ipst->ips_ipmp_lock);
12538 
12539 		ipmp_phyint_join_grp(phyi, grp);
12540 		ill_refhold(ill);
12541 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12542 		    SWITCH_OP, B_FALSE);
12543 		return (0);
12544 	} else {
12545 		/*
12546 		 * Request to remove the interface from a group.  If the
12547 		 * interface is not in a group, this trivially succeeds.
12548 		 */
12549 		rw_exit(&ipst->ips_ipmp_lock);
12550 		if (IS_UNDER_IPMP(ill))
12551 			ipmp_phyint_leave_grp(phyi);
12552 		return (0);
12553 	}
12554 unlock:
12555 	rw_exit(&ipst->ips_ipmp_lock);
12556 	return (err);
12557 }
12558 
12559 /*
12560  * Process an SIOCGLIFBINDING request.
12561  */
12562 /* ARGSUSED */
12563 int
12564 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12565     ip_ioctl_cmd_t *ipip, void *ifreq)
12566 {
12567 	ill_t		*ill;
12568 	struct lifreq	*lifr = ifreq;
12569 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12570 
12571 	if (!IS_IPMP(ipif->ipif_ill))
12572 		return (EINVAL);
12573 
12574 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12575 	if ((ill = ipif->ipif_bound_ill) == NULL)
12576 		lifr->lifr_binding[0] = '\0';
12577 	else
12578 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12579 	rw_exit(&ipst->ips_ipmp_lock);
12580 	return (0);
12581 }
12582 
12583 /*
12584  * Process an SIOCGLIFGROUPNAME request.
12585  */
12586 /* ARGSUSED */
12587 int
12588 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12589     ip_ioctl_cmd_t *ipip, void *ifreq)
12590 {
12591 	ipmp_grp_t	*grp;
12592 	struct lifreq	*lifr = ifreq;
12593 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12594 
12595 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12596 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12597 		lifr->lifr_groupname[0] = '\0';
12598 	else
12599 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12600 	rw_exit(&ipst->ips_ipmp_lock);
12601 	return (0);
12602 }
12603 
12604 /*
12605  * Process an SIOCGLIFGROUPINFO request.
12606  */
12607 /* ARGSUSED */
12608 int
12609 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12610     ip_ioctl_cmd_t *ipip, void *dummy)
12611 {
12612 	ipmp_grp_t	*grp;
12613 	lifgroupinfo_t	*lifgr;
12614 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12615 
12616 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12617 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12618 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12619 
12620 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12621 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12622 		rw_exit(&ipst->ips_ipmp_lock);
12623 		return (ENOENT);
12624 	}
12625 	ipmp_grp_info(grp, lifgr);
12626 	rw_exit(&ipst->ips_ipmp_lock);
12627 	return (0);
12628 }
12629 
12630 static void
12631 ill_dl_down(ill_t *ill)
12632 {
12633 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12634 
12635 	/*
12636 	 * The ill is down; unbind but stay attached since we're still
12637 	 * associated with a PPA. If we have negotiated DLPI capabilites
12638 	 * with the data link service provider (IDS_OK) then reset them.
12639 	 * The interval between unbinding and rebinding is potentially
12640 	 * unbounded hence we cannot assume things will be the same.
12641 	 * The DLPI capabilities will be probed again when the data link
12642 	 * is brought up.
12643 	 */
12644 	mblk_t	*mp = ill->ill_unbind_mp;
12645 
12646 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12647 
12648 	if (!ill->ill_replumbing) {
12649 		/* Free all ilms for this ill */
12650 		update_conn_ill(ill, ill->ill_ipst);
12651 	} else {
12652 		ill_leave_multicast(ill);
12653 	}
12654 
12655 	ill->ill_unbind_mp = NULL;
12656 	if (mp != NULL) {
12657 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12658 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12659 		    ill->ill_name));
12660 		mutex_enter(&ill->ill_lock);
12661 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12662 		mutex_exit(&ill->ill_lock);
12663 		/*
12664 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12665 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12666 		 * ill_capability_dld_disable disable rightaway. If this is not
12667 		 * an unplumb operation then the disable happens on receipt of
12668 		 * the capab ack via ip_rput_dlpi_writer ->
12669 		 * ill_capability_ack_thr. In both cases the order of
12670 		 * the operations seen by DLD is capability disable followed
12671 		 * by DL_UNBIND. Also the DLD capability disable needs a
12672 		 * cv_wait'able context.
12673 		 */
12674 		if (ill->ill_state_flags & ILL_CONDEMNED)
12675 			ill_capability_dld_disable(ill);
12676 		ill_capability_reset(ill, B_FALSE);
12677 		ill_dlpi_send(ill, mp);
12678 	}
12679 	mutex_enter(&ill->ill_lock);
12680 	ill->ill_dl_up = 0;
12681 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12682 	mutex_exit(&ill->ill_lock);
12683 }
12684 
12685 void
12686 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12687 {
12688 	union DL_primitives *dlp;
12689 	t_uscalar_t prim;
12690 	boolean_t waitack = B_FALSE;
12691 
12692 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12693 
12694 	dlp = (union DL_primitives *)mp->b_rptr;
12695 	prim = dlp->dl_primitive;
12696 
12697 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12698 	    dl_primstr(prim), prim, ill->ill_name));
12699 
12700 	switch (prim) {
12701 	case DL_PHYS_ADDR_REQ:
12702 	{
12703 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12704 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12705 		break;
12706 	}
12707 	case DL_BIND_REQ:
12708 		mutex_enter(&ill->ill_lock);
12709 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12710 		mutex_exit(&ill->ill_lock);
12711 		break;
12712 	}
12713 
12714 	/*
12715 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12716 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12717 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12718 	 */
12719 	mutex_enter(&ill->ill_lock);
12720 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12721 	    (prim == DL_UNBIND_REQ)) {
12722 		ill->ill_dlpi_pending = prim;
12723 		waitack = B_TRUE;
12724 	}
12725 
12726 	mutex_exit(&ill->ill_lock);
12727 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12728 	    char *, dl_primstr(prim), ill_t *, ill);
12729 	putnext(ill->ill_wq, mp);
12730 
12731 	/*
12732 	 * There is no ack for DL_NOTIFY_CONF messages
12733 	 */
12734 	if (waitack && prim == DL_NOTIFY_CONF)
12735 		ill_dlpi_done(ill, prim);
12736 }
12737 
12738 /*
12739  * Helper function for ill_dlpi_send().
12740  */
12741 /* ARGSUSED */
12742 static void
12743 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12744 {
12745 	ill_dlpi_send(q->q_ptr, mp);
12746 }
12747 
12748 /*
12749  * Send a DLPI control message to the driver but make sure there
12750  * is only one outstanding message. Uses ill_dlpi_pending to tell
12751  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12752  * when an ACK or a NAK is received to process the next queued message.
12753  */
12754 void
12755 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12756 {
12757 	mblk_t **mpp;
12758 
12759 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12760 
12761 	/*
12762 	 * To ensure that any DLPI requests for current exclusive operation
12763 	 * are always completely sent before any DLPI messages for other
12764 	 * operations, require writer access before enqueuing.
12765 	 */
12766 	if (!IAM_WRITER_ILL(ill)) {
12767 		ill_refhold(ill);
12768 		/* qwriter_ip() does the ill_refrele() */
12769 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12770 		    NEW_OP, B_TRUE);
12771 		return;
12772 	}
12773 
12774 	mutex_enter(&ill->ill_lock);
12775 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12776 		/* Must queue message. Tail insertion */
12777 		mpp = &ill->ill_dlpi_deferred;
12778 		while (*mpp != NULL)
12779 			mpp = &((*mpp)->b_next);
12780 
12781 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12782 		    "while %s pending\n", ill->ill_name,
12783 		    dl_primstr(ill->ill_dlpi_pending)));
12784 
12785 		*mpp = mp;
12786 		mutex_exit(&ill->ill_lock);
12787 		return;
12788 	}
12789 	mutex_exit(&ill->ill_lock);
12790 	ill_dlpi_dispatch(ill, mp);
12791 }
12792 
12793 void
12794 ill_capability_send(ill_t *ill, mblk_t *mp)
12795 {
12796 	ill->ill_capab_pending_cnt++;
12797 	ill_dlpi_send(ill, mp);
12798 }
12799 
12800 void
12801 ill_capability_done(ill_t *ill)
12802 {
12803 	ASSERT(ill->ill_capab_pending_cnt != 0);
12804 
12805 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12806 
12807 	ill->ill_capab_pending_cnt--;
12808 	if (ill->ill_capab_pending_cnt == 0 &&
12809 	    ill->ill_dlpi_capab_state == IDCS_OK)
12810 		ill_capability_reset_alloc(ill);
12811 }
12812 
12813 /*
12814  * Send all deferred DLPI messages without waiting for their ACKs.
12815  */
12816 void
12817 ill_dlpi_send_deferred(ill_t *ill)
12818 {
12819 	mblk_t *mp, *nextmp;
12820 
12821 	/*
12822 	 * Clear ill_dlpi_pending so that the message is not queued in
12823 	 * ill_dlpi_send().
12824 	 */
12825 	mutex_enter(&ill->ill_lock);
12826 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12827 	mp = ill->ill_dlpi_deferred;
12828 	ill->ill_dlpi_deferred = NULL;
12829 	mutex_exit(&ill->ill_lock);
12830 
12831 	for (; mp != NULL; mp = nextmp) {
12832 		nextmp = mp->b_next;
12833 		mp->b_next = NULL;
12834 		ill_dlpi_send(ill, mp);
12835 	}
12836 }
12837 
12838 /*
12839  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12840  * or M_HANGUP
12841  */
12842 static void
12843 ill_dlpi_clear_deferred(ill_t *ill)
12844 {
12845 	mblk_t	*mp, *nextmp;
12846 
12847 	mutex_enter(&ill->ill_lock);
12848 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12849 	mp = ill->ill_dlpi_deferred;
12850 	ill->ill_dlpi_deferred = NULL;
12851 	mutex_exit(&ill->ill_lock);
12852 
12853 	for (; mp != NULL; mp = nextmp) {
12854 		nextmp = mp->b_next;
12855 		inet_freemsg(mp);
12856 	}
12857 }
12858 
12859 /*
12860  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12861  */
12862 boolean_t
12863 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12864 {
12865 	t_uscalar_t pending;
12866 
12867 	mutex_enter(&ill->ill_lock);
12868 	if (ill->ill_dlpi_pending == prim) {
12869 		mutex_exit(&ill->ill_lock);
12870 		return (B_TRUE);
12871 	}
12872 
12873 	/*
12874 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12875 	 * without waiting, so don't print any warnings in that case.
12876 	 */
12877 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12878 		mutex_exit(&ill->ill_lock);
12879 		return (B_FALSE);
12880 	}
12881 	pending = ill->ill_dlpi_pending;
12882 	mutex_exit(&ill->ill_lock);
12883 
12884 	if (pending == DL_PRIM_INVAL) {
12885 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12886 		    "received unsolicited ack for %s on %s\n",
12887 		    dl_primstr(prim), ill->ill_name);
12888 	} else {
12889 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12890 		    "received unexpected ack for %s on %s (expecting %s)\n",
12891 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12892 	}
12893 	return (B_FALSE);
12894 }
12895 
12896 /*
12897  * Complete the current DLPI operation associated with `prim' on `ill' and
12898  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12899  * operations and the ill's current exclusive IPSQ operation has finished
12900  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12901  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12902  * the comments above ipsq_current_finish() for details.
12903  */
12904 void
12905 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12906 {
12907 	mblk_t *mp;
12908 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12909 	ipxop_t *ipx = ipsq->ipsq_xop;
12910 
12911 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12912 	mutex_enter(&ill->ill_lock);
12913 
12914 	ASSERT(prim != DL_PRIM_INVAL);
12915 	ASSERT(ill->ill_dlpi_pending == prim);
12916 
12917 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12918 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12919 
12920 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12921 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12922 		if (ipx->ipx_current_done) {
12923 			mutex_enter(&ipx->ipx_lock);
12924 			ipx->ipx_current_ipif = NULL;
12925 			mutex_exit(&ipx->ipx_lock);
12926 		}
12927 		cv_signal(&ill->ill_cv);
12928 		mutex_exit(&ill->ill_lock);
12929 		return;
12930 	}
12931 
12932 	ill->ill_dlpi_deferred = mp->b_next;
12933 	mp->b_next = NULL;
12934 	mutex_exit(&ill->ill_lock);
12935 
12936 	ill_dlpi_dispatch(ill, mp);
12937 }
12938 
12939 /*
12940  * Queue a (multicast) DLPI control message to be sent to the driver by
12941  * later calling ill_dlpi_send_queued.
12942  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12943  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12944  * for the same group to race.
12945  * We send DLPI control messages in order using ill_lock.
12946  * For IPMP we should be called on the cast_ill.
12947  */
12948 void
12949 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12950 {
12951 	mblk_t **mpp;
12952 
12953 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12954 
12955 	mutex_enter(&ill->ill_lock);
12956 	/* Must queue message. Tail insertion */
12957 	mpp = &ill->ill_dlpi_deferred;
12958 	while (*mpp != NULL)
12959 		mpp = &((*mpp)->b_next);
12960 
12961 	*mpp = mp;
12962 	mutex_exit(&ill->ill_lock);
12963 }
12964 
12965 /*
12966  * Send the messages that were queued. Make sure there is only
12967  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12968  * when an ACK or a NAK is received to process the next queued message.
12969  * For IPMP we are called on the upper ill, but when send what is queued
12970  * on the cast_ill.
12971  */
12972 void
12973 ill_dlpi_send_queued(ill_t *ill)
12974 {
12975 	mblk_t	*mp;
12976 	union DL_primitives *dlp;
12977 	t_uscalar_t prim;
12978 	ill_t *release_ill = NULL;
12979 
12980 	if (IS_IPMP(ill)) {
12981 		/* On the upper IPMP ill. */
12982 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12983 		if (release_ill == NULL) {
12984 			/* Avoid ever sending anything down to the ipmpstub */
12985 			return;
12986 		}
12987 		ill = release_ill;
12988 	}
12989 	mutex_enter(&ill->ill_lock);
12990 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12991 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12992 			/* Can't send. Somebody else will send it */
12993 			mutex_exit(&ill->ill_lock);
12994 			goto done;
12995 		}
12996 		ill->ill_dlpi_deferred = mp->b_next;
12997 		mp->b_next = NULL;
12998 		if (!ill->ill_dl_up) {
12999 			/*
13000 			 * Nobody there. All multicast addresses will be
13001 			 * re-joined when we get the DL_BIND_ACK bringing the
13002 			 * interface up.
13003 			 */
13004 			freemsg(mp);
13005 			continue;
13006 		}
13007 		dlp = (union DL_primitives *)mp->b_rptr;
13008 		prim = dlp->dl_primitive;
13009 
13010 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13011 		    (prim == DL_UNBIND_REQ)) {
13012 			ill->ill_dlpi_pending = prim;
13013 		}
13014 		mutex_exit(&ill->ill_lock);
13015 
13016 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13017 		    char *, dl_primstr(prim), ill_t *, ill);
13018 		putnext(ill->ill_wq, mp);
13019 		mutex_enter(&ill->ill_lock);
13020 	}
13021 	mutex_exit(&ill->ill_lock);
13022 done:
13023 	if (release_ill != NULL)
13024 		ill_refrele(release_ill);
13025 }
13026 
13027 /*
13028  * Queue an IP (IGMP/MLD) message to be sent by IP from
13029  * ill_mcast_send_queued
13030  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13031  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13032  * group to race.
13033  * We send them in order using ill_lock.
13034  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13035  */
13036 void
13037 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13038 {
13039 	mblk_t **mpp;
13040 	ill_t *release_ill = NULL;
13041 
13042 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13043 
13044 	if (IS_IPMP(ill)) {
13045 		/* On the upper IPMP ill. */
13046 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13047 		if (release_ill == NULL) {
13048 			/* Discard instead of queuing for the ipmp interface */
13049 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13050 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13051 			    mp, ill);
13052 			freemsg(mp);
13053 			return;
13054 		}
13055 		ill = release_ill;
13056 	}
13057 
13058 	mutex_enter(&ill->ill_lock);
13059 	/* Must queue message. Tail insertion */
13060 	mpp = &ill->ill_mcast_deferred;
13061 	while (*mpp != NULL)
13062 		mpp = &((*mpp)->b_next);
13063 
13064 	*mpp = mp;
13065 	mutex_exit(&ill->ill_lock);
13066 	if (release_ill != NULL)
13067 		ill_refrele(release_ill);
13068 }
13069 
13070 /*
13071  * Send the IP packets that were queued by ill_mcast_queue.
13072  * These are IGMP/MLD packets.
13073  *
13074  * For IPMP we are called on the upper ill, but when send what is queued
13075  * on the cast_ill.
13076  *
13077  * Request loopback of the report if we are acting as a multicast
13078  * router, so that the process-level routing demon can hear it.
13079  * This will run multiple times for the same group if there are members
13080  * on the same group for multiple ipif's on the same ill. The
13081  * igmp_input/mld_input code will suppress this due to the loopback thus we
13082  * always loopback membership report.
13083  *
13084  * We also need to make sure that this does not get load balanced
13085  * by IPMP. We do this by passing an ill to ip_output_simple.
13086  */
13087 void
13088 ill_mcast_send_queued(ill_t *ill)
13089 {
13090 	mblk_t	*mp;
13091 	ip_xmit_attr_t ixas;
13092 	ill_t *release_ill = NULL;
13093 
13094 	if (IS_IPMP(ill)) {
13095 		/* On the upper IPMP ill. */
13096 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13097 		if (release_ill == NULL) {
13098 			/*
13099 			 * We should have no messages on the ipmp interface
13100 			 * but no point in trying to send them.
13101 			 */
13102 			return;
13103 		}
13104 		ill = release_ill;
13105 	}
13106 	bzero(&ixas, sizeof (ixas));
13107 	ixas.ixa_zoneid = ALL_ZONES;
13108 	ixas.ixa_cred = kcred;
13109 	ixas.ixa_cpid = NOPID;
13110 	ixas.ixa_tsl = NULL;
13111 	/*
13112 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13113 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13114 	 * That is necessary to handle IGMP/MLD snooping switches.
13115 	 */
13116 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13117 	ixas.ixa_ipst = ill->ill_ipst;
13118 
13119 	mutex_enter(&ill->ill_lock);
13120 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13121 		ill->ill_mcast_deferred = mp->b_next;
13122 		mp->b_next = NULL;
13123 		if (!ill->ill_dl_up) {
13124 			/*
13125 			 * Nobody there. Just drop the ip packets.
13126 			 * IGMP/MLD will resend later, if this is a replumb.
13127 			 */
13128 			freemsg(mp);
13129 			continue;
13130 		}
13131 		mutex_enter(&ill->ill_phyint->phyint_lock);
13132 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13133 			/*
13134 			 * When the ill is getting deactivated, we only want to
13135 			 * send the DLPI messages, so drop IGMP/MLD packets.
13136 			 * DLPI messages are handled by ill_dlpi_send_queued()
13137 			 */
13138 			mutex_exit(&ill->ill_phyint->phyint_lock);
13139 			freemsg(mp);
13140 			continue;
13141 		}
13142 		mutex_exit(&ill->ill_phyint->phyint_lock);
13143 		mutex_exit(&ill->ill_lock);
13144 
13145 		/* Check whether we are sending IPv4 or IPv6. */
13146 		if (ill->ill_isv6) {
13147 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13148 
13149 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13150 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13151 		} else {
13152 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13153 
13154 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13155 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13156 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13157 		}
13158 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13159 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13160 		(void) ip_output_simple(mp, &ixas);
13161 		ixa_cleanup(&ixas);
13162 
13163 		mutex_enter(&ill->ill_lock);
13164 	}
13165 	mutex_exit(&ill->ill_lock);
13166 
13167 done:
13168 	if (release_ill != NULL)
13169 		ill_refrele(release_ill);
13170 }
13171 
13172 /*
13173  * Take down a specific interface, but don't lose any information about it.
13174  * (Always called as writer.)
13175  * This function goes through the down sequence even if the interface is
13176  * already down. There are 2 reasons.
13177  * a. Currently we permit interface routes that depend on down interfaces
13178  *    to be added. This behaviour itself is questionable. However it appears
13179  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13180  *    time. We go thru the cleanup in order to remove these routes.
13181  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13182  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13183  *    down, but we need to cleanup i.e. do ill_dl_down and
13184  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13185  *
13186  * IP-MT notes:
13187  *
13188  * Model of reference to interfaces.
13189  *
13190  * The following members in ipif_t track references to the ipif.
13191  *	int     ipif_refcnt;    Active reference count
13192  *
13193  * The following members in ill_t track references to the ill.
13194  *	int             ill_refcnt;     active refcnt
13195  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13196  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13197  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13198  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13199  *
13200  * Reference to an ipif or ill can be obtained in any of the following ways.
13201  *
13202  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13203  * Pointers to ipif / ill from other data structures viz ire and conn.
13204  * Implicit reference to the ipif / ill by holding a reference to the ire.
13205  *
13206  * The ipif/ill lookup functions return a reference held ipif / ill.
13207  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13208  * This is a purely dynamic reference count associated with threads holding
13209  * references to the ipif / ill. Pointers from other structures do not
13210  * count towards this reference count.
13211  *
13212  * ill_ire_cnt is the number of ire's associated with the
13213  * ill. This is incremented whenever a new ire is created referencing the
13214  * ill. This is done atomically inside ire_add_v[46] where the ire is
13215  * actually added to the ire hash table. The count is decremented in
13216  * ire_inactive where the ire is destroyed.
13217  *
13218  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13219  * This is incremented atomically in
13220  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13221  * table. Similarly it is decremented in ncec_inactive() where the ncec
13222  * is destroyed.
13223  *
13224  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13225  * incremented atomically in nce_add() where the nce is actually added to the
13226  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13227  * is destroyed.
13228  *
13229  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13230  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13231  *
13232  * Flow of ioctls involving interface down/up
13233  *
13234  * The following is the sequence of an attempt to set some critical flags on an
13235  * up interface.
13236  * ip_sioctl_flags
13237  * ipif_down
13238  * wait for ipif to be quiescent
13239  * ipif_down_tail
13240  * ip_sioctl_flags_tail
13241  *
13242  * All set ioctls that involve down/up sequence would have a skeleton similar
13243  * to the above. All the *tail functions are called after the refcounts have
13244  * dropped to the appropriate values.
13245  *
13246  * SIOC ioctls during the IPIF_CHANGING interval.
13247  *
13248  * Threads handling SIOC set ioctls serialize on the squeue, but this
13249  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13250  * steps of internal changes to the state, some of which are visible in
13251  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13252  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13253  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13254  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13255  * the current exclusive operation completes. The IPIF_CHANGING check
13256  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13257  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13258  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13259  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13260  * until we release the ipsq_lock, even though the ill/ipif state flags
13261  * can change after we drop the ill_lock.
13262  */
13263 int
13264 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13265 {
13266 	ill_t		*ill = ipif->ipif_ill;
13267 	conn_t		*connp;
13268 	boolean_t	success;
13269 	boolean_t	ipif_was_up = B_FALSE;
13270 	ip_stack_t	*ipst = ill->ill_ipst;
13271 
13272 	ASSERT(IAM_WRITER_IPIF(ipif));
13273 
13274 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13275 
13276 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13277 	    ill_t *, ill, ipif_t *, ipif);
13278 
13279 	if (ipif->ipif_flags & IPIF_UP) {
13280 		mutex_enter(&ill->ill_lock);
13281 		ipif->ipif_flags &= ~IPIF_UP;
13282 		ASSERT(ill->ill_ipif_up_count > 0);
13283 		--ill->ill_ipif_up_count;
13284 		mutex_exit(&ill->ill_lock);
13285 		ipif_was_up = B_TRUE;
13286 		/* Update status in SCTP's list */
13287 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13288 		ill_nic_event_dispatch(ipif->ipif_ill,
13289 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13290 	}
13291 
13292 	/*
13293 	 * Removal of the last ipif from an ill may result in a DL_UNBIND
13294 	 * being sent to the driver, and we must not send any data packets to
13295 	 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13296 	 * ire and nce entries used in the data path will be cleaned
13297 	 * up, and we also set  the ILL_DOWN_IN_PROGRESS bit to make
13298 	 * sure on new entries will be added until the ill is bound
13299 	 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13300 	 * receipt of a DL_BIND_ACK.
13301 	 */
13302 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13303 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13304 	    ill->ill_dl_up) {
13305 		ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13306 	}
13307 
13308 	/*
13309 	 * Blow away memberships we established in ipif_multicast_up().
13310 	 */
13311 	ipif_multicast_down(ipif);
13312 
13313 	/*
13314 	 * Remove from the mapping for __sin6_src_id. We insert only
13315 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13316 	 * stored as mapped addresses, we need to check for mapped
13317 	 * INADDR_ANY also.
13318 	 */
13319 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13320 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13321 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13322 		int err;
13323 
13324 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13325 		    ipif->ipif_zoneid, ipst);
13326 		if (err != 0) {
13327 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13328 		}
13329 	}
13330 
13331 	if (ipif_was_up) {
13332 		/* only delete if we'd added ire's before */
13333 		if (ipif->ipif_isv6)
13334 			ipif_delete_ires_v6(ipif);
13335 		else
13336 			ipif_delete_ires_v4(ipif);
13337 	}
13338 
13339 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13340 		/*
13341 		 * Since the interface is now down, it may have just become
13342 		 * inactive.  Note that this needs to be done even for a
13343 		 * lll_logical_down(), or ARP entries will not get correctly
13344 		 * restored when the interface comes back up.
13345 		 */
13346 		if (IS_UNDER_IPMP(ill))
13347 			ipmp_ill_refresh_active(ill);
13348 	}
13349 
13350 	/*
13351 	 * neighbor-discovery or arp entries for this interface. The ipif
13352 	 * has to be quiesced, so we walk all the nce's and delete those
13353 	 * that point at the ipif->ipif_ill. At the same time, we also
13354 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13355 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13356 	 * that for ipif_down_tail()
13357 	 */
13358 	ipif_nce_down(ipif);
13359 
13360 	/*
13361 	 * If this is the last ipif on the ill, we also need to remove
13362 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13363 	 * never succeed.
13364 	 */
13365 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13366 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13367 
13368 	/*
13369 	 * Walk all CONNs that can have a reference on an ire for this
13370 	 * ipif (we actually walk all that now have stale references).
13371 	 */
13372 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13373 
13374 	/*
13375 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13376 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13377 	 * and ill_delete -> ipif_free -> ipif_down
13378 	 */
13379 	if (mp == NULL) {
13380 		ASSERT(q == NULL);
13381 		return (0);
13382 	}
13383 
13384 	if (CONN_Q(q)) {
13385 		connp = Q_TO_CONN(q);
13386 		mutex_enter(&connp->conn_lock);
13387 	} else {
13388 		connp = NULL;
13389 	}
13390 	mutex_enter(&ill->ill_lock);
13391 	/*
13392 	 * Are there any ire's pointing to this ipif that are still active ?
13393 	 * If this is the last ipif going down, are there any ire's pointing
13394 	 * to this ill that are still active ?
13395 	 */
13396 	if (ipif_is_quiescent(ipif)) {
13397 		mutex_exit(&ill->ill_lock);
13398 		if (connp != NULL)
13399 			mutex_exit(&connp->conn_lock);
13400 		return (0);
13401 	}
13402 
13403 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13404 	    ill->ill_name, (void *)ill));
13405 	/*
13406 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13407 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13408 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13409 	 */
13410 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13411 	if (!success) {
13412 		/* The conn is closing. So just return */
13413 		ASSERT(connp != NULL);
13414 		mutex_exit(&ill->ill_lock);
13415 		mutex_exit(&connp->conn_lock);
13416 		return (EINTR);
13417 	}
13418 
13419 	mutex_exit(&ill->ill_lock);
13420 	if (connp != NULL)
13421 		mutex_exit(&connp->conn_lock);
13422 	return (EINPROGRESS);
13423 }
13424 
13425 int
13426 ipif_down_tail(ipif_t *ipif)
13427 {
13428 	ill_t	*ill = ipif->ipif_ill;
13429 	int	err = 0;
13430 
13431 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13432 	    ill_t *, ill, ipif_t *, ipif);
13433 
13434 	/*
13435 	 * Skip any loopback interface (null wq).
13436 	 * If this is the last logical interface on the ill
13437 	 * have ill_dl_down tell the driver we are gone (unbind)
13438 	 * Note that lun 0 can ipif_down even though
13439 	 * there are other logical units that are up.
13440 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13441 	 */
13442 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13443 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13444 	    ill->ill_dl_up) {
13445 		ill_dl_down(ill);
13446 	}
13447 	if (!ipif->ipif_isv6)
13448 		err = ipif_arp_down(ipif);
13449 
13450 	ill->ill_logical_down = 0;
13451 
13452 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13453 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13454 	return (err);
13455 }
13456 
13457 /*
13458  * Bring interface logically down without bringing the physical interface
13459  * down e.g. when the netmask is changed. This avoids long lasting link
13460  * negotiations between an ethernet interface and a certain switches.
13461  */
13462 static int
13463 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13464 {
13465 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13466 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13467 
13468 	/*
13469 	 * The ill_logical_down flag is a transient flag. It is set here
13470 	 * and is cleared once the down has completed in ipif_down_tail.
13471 	 * This flag does not indicate whether the ill stream is in the
13472 	 * DL_BOUND state with the driver. Instead this flag is used by
13473 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13474 	 * the driver. The state of the ill stream i.e. whether it is
13475 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13476 	 */
13477 	ipif->ipif_ill->ill_logical_down = 1;
13478 	return (ipif_down(ipif, q, mp));
13479 }
13480 
13481 /*
13482  * Initiate deallocate of an IPIF. Always called as writer. Called by
13483  * ill_delete or ip_sioctl_removeif.
13484  */
13485 static void
13486 ipif_free(ipif_t *ipif)
13487 {
13488 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13489 
13490 	ASSERT(IAM_WRITER_IPIF(ipif));
13491 
13492 	if (ipif->ipif_recovery_id != 0)
13493 		(void) untimeout(ipif->ipif_recovery_id);
13494 	ipif->ipif_recovery_id = 0;
13495 
13496 	/*
13497 	 * Take down the interface. We can be called either from ill_delete
13498 	 * or from ip_sioctl_removeif.
13499 	 */
13500 	(void) ipif_down(ipif, NULL, NULL);
13501 
13502 	/*
13503 	 * Now that the interface is down, there's no chance it can still
13504 	 * become a duplicate.  Cancel any timer that may have been set while
13505 	 * tearing down.
13506 	 */
13507 	if (ipif->ipif_recovery_id != 0)
13508 		(void) untimeout(ipif->ipif_recovery_id);
13509 	ipif->ipif_recovery_id = 0;
13510 
13511 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13512 	/* Remove pointers to this ill in the multicast routing tables */
13513 	reset_mrt_vif_ipif(ipif);
13514 	/* If necessary, clear the cached source ipif rotor. */
13515 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13516 		ipif->ipif_ill->ill_src_ipif = NULL;
13517 	rw_exit(&ipst->ips_ill_g_lock);
13518 }
13519 
13520 static void
13521 ipif_free_tail(ipif_t *ipif)
13522 {
13523 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13524 
13525 	/*
13526 	 * Need to hold both ill_g_lock and ill_lock while
13527 	 * inserting or removing an ipif from the linked list
13528 	 * of ipifs hanging off the ill.
13529 	 */
13530 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13531 
13532 #ifdef DEBUG
13533 	ipif_trace_cleanup(ipif);
13534 #endif
13535 
13536 	/* Ask SCTP to take it out of it list */
13537 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13538 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13539 
13540 	/* Get it out of the ILL interface list. */
13541 	ipif_remove(ipif);
13542 	rw_exit(&ipst->ips_ill_g_lock);
13543 
13544 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13545 	ASSERT(ipif->ipif_recovery_id == 0);
13546 	ASSERT(ipif->ipif_ire_local == NULL);
13547 	ASSERT(ipif->ipif_ire_if == NULL);
13548 
13549 	/* Free the memory. */
13550 	mi_free(ipif);
13551 }
13552 
13553 /*
13554  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13555  * is zero.
13556  */
13557 void
13558 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13559 {
13560 	char	lbuf[LIFNAMSIZ];
13561 	char	*name;
13562 	size_t	name_len;
13563 
13564 	buf[0] = '\0';
13565 	name = ipif->ipif_ill->ill_name;
13566 	name_len = ipif->ipif_ill->ill_name_length;
13567 	if (ipif->ipif_id != 0) {
13568 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13569 		    ipif->ipif_id);
13570 		name = lbuf;
13571 		name_len = mi_strlen(name) + 1;
13572 	}
13573 	len -= 1;
13574 	buf[len] = '\0';
13575 	len = MIN(len, name_len);
13576 	bcopy(name, buf, len);
13577 }
13578 
13579 /*
13580  * Sets `buf' to an ill name.
13581  */
13582 void
13583 ill_get_name(const ill_t *ill, char *buf, int len)
13584 {
13585 	char	*name;
13586 	size_t	name_len;
13587 
13588 	name = ill->ill_name;
13589 	name_len = ill->ill_name_length;
13590 	len -= 1;
13591 	buf[len] = '\0';
13592 	len = MIN(len, name_len);
13593 	bcopy(name, buf, len);
13594 }
13595 
13596 /*
13597  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13598  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13599  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13600  * (May be called as writer.)
13601  */
13602 static ipif_t *
13603 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13604     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13605 {
13606 	char	*cp;
13607 	char	*endp;
13608 	long	id;
13609 	ill_t	*ill;
13610 	ipif_t	*ipif;
13611 	uint_t	ire_type;
13612 	boolean_t did_alloc = B_FALSE;
13613 	char	last;
13614 
13615 	/*
13616 	 * If the caller wants to us to create the ipif, make sure we have a
13617 	 * valid zoneid
13618 	 */
13619 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13620 
13621 	if (namelen == 0) {
13622 		return (NULL);
13623 	}
13624 
13625 	*exists = B_FALSE;
13626 	/* Look for a colon in the name. */
13627 	endp = &name[namelen];
13628 	for (cp = endp; --cp > name; ) {
13629 		if (*cp == IPIF_SEPARATOR_CHAR)
13630 			break;
13631 	}
13632 
13633 	if (*cp == IPIF_SEPARATOR_CHAR) {
13634 		/*
13635 		 * Reject any non-decimal aliases for logical
13636 		 * interfaces. Aliases with leading zeroes
13637 		 * are also rejected as they introduce ambiguity
13638 		 * in the naming of the interfaces.
13639 		 * In order to confirm with existing semantics,
13640 		 * and to not break any programs/script relying
13641 		 * on that behaviour, if<0>:0 is considered to be
13642 		 * a valid interface.
13643 		 *
13644 		 * If alias has two or more digits and the first
13645 		 * is zero, fail.
13646 		 */
13647 		if (&cp[2] < endp && cp[1] == '0') {
13648 			return (NULL);
13649 		}
13650 	}
13651 
13652 	if (cp <= name) {
13653 		cp = endp;
13654 	}
13655 	last = *cp;
13656 	*cp = '\0';
13657 
13658 	/*
13659 	 * Look up the ILL, based on the portion of the name
13660 	 * before the slash. ill_lookup_on_name returns a held ill.
13661 	 * Temporary to check whether ill exists already. If so
13662 	 * ill_lookup_on_name will clear it.
13663 	 */
13664 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13665 	    &did_alloc, ipst);
13666 	*cp = last;
13667 	if (ill == NULL)
13668 		return (NULL);
13669 
13670 	/* Establish the unit number in the name. */
13671 	id = 0;
13672 	if (cp < endp && *endp == '\0') {
13673 		/* If there was a colon, the unit number follows. */
13674 		cp++;
13675 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13676 			ill_refrele(ill);
13677 			return (NULL);
13678 		}
13679 	}
13680 
13681 	mutex_enter(&ill->ill_lock);
13682 	/* Now see if there is an IPIF with this unit number. */
13683 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13684 		if (ipif->ipif_id == id) {
13685 			if (zoneid != ALL_ZONES &&
13686 			    zoneid != ipif->ipif_zoneid &&
13687 			    ipif->ipif_zoneid != ALL_ZONES) {
13688 				mutex_exit(&ill->ill_lock);
13689 				ill_refrele(ill);
13690 				return (NULL);
13691 			}
13692 			if (IPIF_CAN_LOOKUP(ipif)) {
13693 				ipif_refhold_locked(ipif);
13694 				mutex_exit(&ill->ill_lock);
13695 				if (!did_alloc)
13696 					*exists = B_TRUE;
13697 				/*
13698 				 * Drop locks before calling ill_refrele
13699 				 * since it can potentially call into
13700 				 * ipif_ill_refrele_tail which can end up
13701 				 * in trying to acquire any lock.
13702 				 */
13703 				ill_refrele(ill);
13704 				return (ipif);
13705 			}
13706 		}
13707 	}
13708 
13709 	if (!do_alloc) {
13710 		mutex_exit(&ill->ill_lock);
13711 		ill_refrele(ill);
13712 		return (NULL);
13713 	}
13714 
13715 	/*
13716 	 * If none found, atomically allocate and return a new one.
13717 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13718 	 * to support "receive only" use of lo0:1 etc. as is still done
13719 	 * below as an initial guess.
13720 	 * However, this is now likely to be overriden later in ipif_up_done()
13721 	 * when we know for sure what address has been configured on the
13722 	 * interface, since we might have more than one loopback interface
13723 	 * with a loopback address, e.g. in the case of zones, and all the
13724 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13725 	 */
13726 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13727 		ire_type = IRE_LOOPBACK;
13728 	else
13729 		ire_type = IRE_LOCAL;
13730 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13731 	if (ipif != NULL)
13732 		ipif_refhold_locked(ipif);
13733 	mutex_exit(&ill->ill_lock);
13734 	ill_refrele(ill);
13735 	return (ipif);
13736 }
13737 
13738 /*
13739  * Variant of the above that queues the request on the ipsq when
13740  * IPIF_CHANGING is set.
13741  */
13742 static ipif_t *
13743 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13744     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13745     ip_stack_t *ipst)
13746 {
13747 	char	*cp;
13748 	char	*endp;
13749 	long	id;
13750 	ill_t	*ill;
13751 	ipif_t	*ipif;
13752 	boolean_t did_alloc = B_FALSE;
13753 	ipsq_t	*ipsq;
13754 
13755 	if (error != NULL)
13756 		*error = 0;
13757 
13758 	if (namelen == 0) {
13759 		if (error != NULL)
13760 			*error = ENXIO;
13761 		return (NULL);
13762 	}
13763 
13764 	/* Look for a colon in the name. */
13765 	endp = &name[namelen];
13766 	for (cp = endp; --cp > name; ) {
13767 		if (*cp == IPIF_SEPARATOR_CHAR)
13768 			break;
13769 	}
13770 
13771 	if (*cp == IPIF_SEPARATOR_CHAR) {
13772 		/*
13773 		 * Reject any non-decimal aliases for logical
13774 		 * interfaces. Aliases with leading zeroes
13775 		 * are also rejected as they introduce ambiguity
13776 		 * in the naming of the interfaces.
13777 		 * In order to confirm with existing semantics,
13778 		 * and to not break any programs/script relying
13779 		 * on that behaviour, if<0>:0 is considered to be
13780 		 * a valid interface.
13781 		 *
13782 		 * If alias has two or more digits and the first
13783 		 * is zero, fail.
13784 		 */
13785 		if (&cp[2] < endp && cp[1] == '0') {
13786 			if (error != NULL)
13787 				*error = EINVAL;
13788 			return (NULL);
13789 		}
13790 	}
13791 
13792 	if (cp <= name) {
13793 		cp = endp;
13794 	} else {
13795 		*cp = '\0';
13796 	}
13797 
13798 	/*
13799 	 * Look up the ILL, based on the portion of the name
13800 	 * before the slash. ill_lookup_on_name returns a held ill.
13801 	 * Temporary to check whether ill exists already. If so
13802 	 * ill_lookup_on_name will clear it.
13803 	 */
13804 	ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13805 	if (cp != endp)
13806 		*cp = IPIF_SEPARATOR_CHAR;
13807 	if (ill == NULL)
13808 		return (NULL);
13809 
13810 	/* Establish the unit number in the name. */
13811 	id = 0;
13812 	if (cp < endp && *endp == '\0') {
13813 		/* If there was a colon, the unit number follows. */
13814 		cp++;
13815 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13816 			ill_refrele(ill);
13817 			if (error != NULL)
13818 				*error = ENXIO;
13819 			return (NULL);
13820 		}
13821 	}
13822 
13823 	GRAB_CONN_LOCK(q);
13824 	mutex_enter(&ill->ill_lock);
13825 	/* Now see if there is an IPIF with this unit number. */
13826 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13827 		if (ipif->ipif_id == id) {
13828 			if (zoneid != ALL_ZONES &&
13829 			    zoneid != ipif->ipif_zoneid &&
13830 			    ipif->ipif_zoneid != ALL_ZONES) {
13831 				mutex_exit(&ill->ill_lock);
13832 				RELEASE_CONN_LOCK(q);
13833 				ill_refrele(ill);
13834 				if (error != NULL)
13835 					*error = ENXIO;
13836 				return (NULL);
13837 			}
13838 
13839 			if (!(IPIF_IS_CHANGING(ipif) ||
13840 			    IPIF_IS_CONDEMNED(ipif)) ||
13841 			    IAM_WRITER_IPIF(ipif)) {
13842 				ipif_refhold_locked(ipif);
13843 				mutex_exit(&ill->ill_lock);
13844 				/*
13845 				 * Drop locks before calling ill_refrele
13846 				 * since it can potentially call into
13847 				 * ipif_ill_refrele_tail which can end up
13848 				 * in trying to acquire any lock.
13849 				 */
13850 				RELEASE_CONN_LOCK(q);
13851 				ill_refrele(ill);
13852 				return (ipif);
13853 			} else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13854 				ipsq = ill->ill_phyint->phyint_ipsq;
13855 				mutex_enter(&ipsq->ipsq_lock);
13856 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13857 				mutex_exit(&ill->ill_lock);
13858 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13859 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13860 				mutex_exit(&ipsq->ipsq_lock);
13861 				RELEASE_CONN_LOCK(q);
13862 				ill_refrele(ill);
13863 				if (error != NULL)
13864 					*error = EINPROGRESS;
13865 				return (NULL);
13866 			}
13867 		}
13868 	}
13869 	RELEASE_CONN_LOCK(q);
13870 	mutex_exit(&ill->ill_lock);
13871 	ill_refrele(ill);
13872 	if (error != NULL)
13873 		*error = ENXIO;
13874 	return (NULL);
13875 }
13876 
13877 /*
13878  * This routine is called whenever a new address comes up on an ipif.  If
13879  * we are configured to respond to address mask requests, then we are supposed
13880  * to broadcast an address mask reply at this time.  This routine is also
13881  * called if we are already up, but a netmask change is made.  This is legal
13882  * but might not make the system manager very popular.	(May be called
13883  * as writer.)
13884  */
13885 void
13886 ipif_mask_reply(ipif_t *ipif)
13887 {
13888 	icmph_t	*icmph;
13889 	ipha_t	*ipha;
13890 	mblk_t	*mp;
13891 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13892 	ip_xmit_attr_t ixas;
13893 
13894 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13895 
13896 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13897 		return;
13898 
13899 	/* ICMP mask reply is IPv4 only */
13900 	ASSERT(!ipif->ipif_isv6);
13901 	/* ICMP mask reply is not for a loopback interface */
13902 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13903 
13904 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13905 		return;
13906 
13907 	mp = allocb(REPLY_LEN, BPRI_HI);
13908 	if (mp == NULL)
13909 		return;
13910 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13911 
13912 	ipha = (ipha_t *)mp->b_rptr;
13913 	bzero(ipha, REPLY_LEN);
13914 	*ipha = icmp_ipha;
13915 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13916 	ipha->ipha_src = ipif->ipif_lcl_addr;
13917 	ipha->ipha_dst = ipif->ipif_brd_addr;
13918 	ipha->ipha_length = htons(REPLY_LEN);
13919 	ipha->ipha_ident = 0;
13920 
13921 	icmph = (icmph_t *)&ipha[1];
13922 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13923 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13924 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13925 
13926 	bzero(&ixas, sizeof (ixas));
13927 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13928 	ixas.ixa_zoneid = ALL_ZONES;
13929 	ixas.ixa_ifindex = 0;
13930 	ixas.ixa_ipst = ipst;
13931 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13932 	(void) ip_output_simple(mp, &ixas);
13933 	ixa_cleanup(&ixas);
13934 #undef	REPLY_LEN
13935 }
13936 
13937 /*
13938  * Join the ipif specific multicast groups.
13939  * Must be called after a mapping has been set up in the resolver.  (Always
13940  * called as writer.)
13941  */
13942 void
13943 ipif_multicast_up(ipif_t *ipif)
13944 {
13945 	int err;
13946 	ill_t *ill;
13947 	ilm_t *ilm;
13948 
13949 	ASSERT(IAM_WRITER_IPIF(ipif));
13950 
13951 	ill = ipif->ipif_ill;
13952 
13953 	ip1dbg(("ipif_multicast_up\n"));
13954 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13955 	    ipif->ipif_allhosts_ilm != NULL)
13956 		return;
13957 
13958 	if (ipif->ipif_isv6) {
13959 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13960 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13961 
13962 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13963 
13964 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13965 			return;
13966 
13967 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13968 
13969 		/*
13970 		 * Join the all hosts multicast address.  We skip this for
13971 		 * underlying IPMP interfaces since they should be invisible.
13972 		 */
13973 		if (!IS_UNDER_IPMP(ill)) {
13974 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13975 			    &err);
13976 			if (ilm == NULL) {
13977 				ASSERT(err != 0);
13978 				ip0dbg(("ipif_multicast_up: "
13979 				    "all_hosts_mcast failed %d\n", err));
13980 				return;
13981 			}
13982 			ipif->ipif_allhosts_ilm = ilm;
13983 		}
13984 
13985 		/*
13986 		 * Enable multicast for the solicited node multicast address.
13987 		 * If IPMP we need to put the membership on the upper ill.
13988 		 */
13989 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13990 			ill_t *mcast_ill = NULL;
13991 			boolean_t need_refrele;
13992 
13993 			if (IS_UNDER_IPMP(ill) &&
13994 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13995 				need_refrele = B_TRUE;
13996 			} else {
13997 				mcast_ill = ill;
13998 				need_refrele = B_FALSE;
13999 			}
14000 
14001 			ilm = ip_addmulti(&v6solmc, mcast_ill,
14002 			    ipif->ipif_zoneid, &err);
14003 			if (need_refrele)
14004 				ill_refrele(mcast_ill);
14005 
14006 			if (ilm == NULL) {
14007 				ASSERT(err != 0);
14008 				ip0dbg(("ipif_multicast_up: solicited MC"
14009 				    " failed %d\n", err));
14010 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14011 					ipif->ipif_allhosts_ilm = NULL;
14012 					(void) ip_delmulti(ilm);
14013 				}
14014 				return;
14015 			}
14016 			ipif->ipif_solmulti_ilm = ilm;
14017 		}
14018 	} else {
14019 		in6_addr_t v6group;
14020 
14021 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14022 			return;
14023 
14024 		/* Join the all hosts multicast address */
14025 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14026 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14027 
14028 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14029 		if (ilm == NULL) {
14030 			ASSERT(err != 0);
14031 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
14032 			return;
14033 		}
14034 		ipif->ipif_allhosts_ilm = ilm;
14035 	}
14036 }
14037 
14038 /*
14039  * Blow away any multicast groups that we joined in ipif_multicast_up().
14040  * (ilms from explicit memberships are handled in conn_update_ill.)
14041  */
14042 void
14043 ipif_multicast_down(ipif_t *ipif)
14044 {
14045 	ASSERT(IAM_WRITER_IPIF(ipif));
14046 
14047 	ip1dbg(("ipif_multicast_down\n"));
14048 
14049 	if (ipif->ipif_allhosts_ilm != NULL) {
14050 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
14051 		ipif->ipif_allhosts_ilm = NULL;
14052 	}
14053 	if (ipif->ipif_solmulti_ilm != NULL) {
14054 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
14055 		ipif->ipif_solmulti_ilm = NULL;
14056 	}
14057 }
14058 
14059 /*
14060  * Used when an interface comes up to recreate any extra routes on this
14061  * interface.
14062  */
14063 int
14064 ill_recover_saved_ire(ill_t *ill)
14065 {
14066 	mblk_t		*mp;
14067 	ip_stack_t	*ipst = ill->ill_ipst;
14068 
14069 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14070 
14071 	mutex_enter(&ill->ill_saved_ire_lock);
14072 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14073 		ire_t		*ire, *nire;
14074 		ifrt_t		*ifrt;
14075 
14076 		ifrt = (ifrt_t *)mp->b_rptr;
14077 		/*
14078 		 * Create a copy of the IRE with the saved address and netmask.
14079 		 */
14080 		if (ill->ill_isv6) {
14081 			ire = ire_create_v6(
14082 			    &ifrt->ifrt_v6addr,
14083 			    &ifrt->ifrt_v6mask,
14084 			    &ifrt->ifrt_v6gateway_addr,
14085 			    ifrt->ifrt_type,
14086 			    ill,
14087 			    ifrt->ifrt_zoneid,
14088 			    ifrt->ifrt_flags,
14089 			    NULL,
14090 			    ipst);
14091 		} else {
14092 			ire = ire_create(
14093 			    (uint8_t *)&ifrt->ifrt_addr,
14094 			    (uint8_t *)&ifrt->ifrt_mask,
14095 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
14096 			    ifrt->ifrt_type,
14097 			    ill,
14098 			    ifrt->ifrt_zoneid,
14099 			    ifrt->ifrt_flags,
14100 			    NULL,
14101 			    ipst);
14102 		}
14103 		if (ire == NULL) {
14104 			mutex_exit(&ill->ill_saved_ire_lock);
14105 			return (ENOMEM);
14106 		}
14107 
14108 		if (ifrt->ifrt_flags & RTF_SETSRC) {
14109 			if (ill->ill_isv6) {
14110 				ire->ire_setsrc_addr_v6 =
14111 				    ifrt->ifrt_v6setsrc_addr;
14112 			} else {
14113 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14114 			}
14115 		}
14116 
14117 		/*
14118 		 * Some software (for example, GateD and Sun Cluster) attempts
14119 		 * to create (what amount to) IRE_PREFIX routes with the
14120 		 * loopback address as the gateway.  This is primarily done to
14121 		 * set up prefixes with the RTF_REJECT flag set (for example,
14122 		 * when generating aggregate routes.)
14123 		 *
14124 		 * If the IRE type (as defined by ill->ill_net_type) is
14125 		 * IRE_LOOPBACK, then we map the request into a
14126 		 * IRE_IF_NORESOLVER.
14127 		 */
14128 		if (ill->ill_net_type == IRE_LOOPBACK)
14129 			ire->ire_type = IRE_IF_NORESOLVER;
14130 
14131 		/*
14132 		 * ire held by ire_add, will be refreled' towards the
14133 		 * the end of ipif_up_done
14134 		 */
14135 		nire = ire_add(ire);
14136 		/*
14137 		 * Check if it was a duplicate entry. This handles
14138 		 * the case of two racing route adds for the same route
14139 		 */
14140 		if (nire == NULL) {
14141 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14142 		} else if (nire != ire) {
14143 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14144 			    (void *)nire));
14145 			ire_delete(nire);
14146 		} else {
14147 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14148 			    (void *)nire));
14149 		}
14150 		if (nire != NULL)
14151 			ire_refrele(nire);
14152 	}
14153 	mutex_exit(&ill->ill_saved_ire_lock);
14154 	return (0);
14155 }
14156 
14157 /*
14158  * Used to set the netmask and broadcast address to default values when the
14159  * interface is brought up.  (Always called as writer.)
14160  */
14161 static void
14162 ipif_set_default(ipif_t *ipif)
14163 {
14164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14165 
14166 	if (!ipif->ipif_isv6) {
14167 		/*
14168 		 * Interface holds an IPv4 address. Default
14169 		 * mask is the natural netmask.
14170 		 */
14171 		if (!ipif->ipif_net_mask) {
14172 			ipaddr_t	v4mask;
14173 
14174 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14175 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14176 		}
14177 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14178 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14179 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14180 		} else {
14181 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14182 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14183 		}
14184 		/*
14185 		 * NOTE: SunOS 4.X does this even if the broadcast address
14186 		 * has been already set thus we do the same here.
14187 		 */
14188 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14189 			ipaddr_t	v4addr;
14190 
14191 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14192 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14193 		}
14194 	} else {
14195 		/*
14196 		 * Interface holds an IPv6-only address.  Default
14197 		 * mask is all-ones.
14198 		 */
14199 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14200 			ipif->ipif_v6net_mask = ipv6_all_ones;
14201 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14202 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14203 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14204 		} else {
14205 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14206 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14207 		}
14208 	}
14209 }
14210 
14211 /*
14212  * Return 0 if this address can be used as local address without causing
14213  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14214  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14215  * Note that the same IPv6 link-local address is allowed as long as the ills
14216  * are not on the same link.
14217  */
14218 int
14219 ip_addr_availability_check(ipif_t *new_ipif)
14220 {
14221 	in6_addr_t our_v6addr;
14222 	ill_t *ill;
14223 	ipif_t *ipif;
14224 	ill_walk_context_t ctx;
14225 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14226 
14227 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14228 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14229 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14230 
14231 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14232 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14233 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14234 		return (0);
14235 
14236 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14237 
14238 	if (new_ipif->ipif_isv6)
14239 		ill = ILL_START_WALK_V6(&ctx, ipst);
14240 	else
14241 		ill = ILL_START_WALK_V4(&ctx, ipst);
14242 
14243 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14244 		for (ipif = ill->ill_ipif; ipif != NULL;
14245 		    ipif = ipif->ipif_next) {
14246 			if ((ipif == new_ipif) ||
14247 			    !(ipif->ipif_flags & IPIF_UP) ||
14248 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14249 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14250 			    &our_v6addr))
14251 				continue;
14252 
14253 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14254 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14255 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14256 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14257 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14258 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14259 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14260 				continue;
14261 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14262 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14263 				continue;
14264 			else if (new_ipif->ipif_ill == ill)
14265 				return (EADDRINUSE);
14266 			else
14267 				return (EADDRNOTAVAIL);
14268 		}
14269 	}
14270 
14271 	return (0);
14272 }
14273 
14274 /*
14275  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14276  * IREs for the ipif.
14277  * When the routine returns EINPROGRESS then mp has been consumed and
14278  * the ioctl will be acked from ip_rput_dlpi.
14279  */
14280 int
14281 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14282 {
14283 	ill_t		*ill = ipif->ipif_ill;
14284 	boolean_t 	isv6 = ipif->ipif_isv6;
14285 	int		err = 0;
14286 	boolean_t	success;
14287 	uint_t		ipif_orig_id;
14288 	ip_stack_t	*ipst = ill->ill_ipst;
14289 
14290 	ASSERT(IAM_WRITER_IPIF(ipif));
14291 
14292 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14293 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14294 	    ill_t *, ill, ipif_t *, ipif);
14295 
14296 	/* Shouldn't get here if it is already up. */
14297 	if (ipif->ipif_flags & IPIF_UP)
14298 		return (EALREADY);
14299 
14300 	/*
14301 	 * If this is a request to bring up a data address on an interface
14302 	 * under IPMP, then move the address to its IPMP meta-interface and
14303 	 * try to bring it up.  One complication is that the zeroth ipif for
14304 	 * an ill is special, in that every ill always has one, and that code
14305 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14306 	 */
14307 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14308 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14309 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14310 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14311 
14312 		/*
14313 		 * The ipif being brought up should be quiesced.  If it's not,
14314 		 * something has gone amiss and we need to bail out.  (If it's
14315 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14316 		 */
14317 		mutex_enter(&ill->ill_lock);
14318 		if (!ipif_is_quiescent(ipif)) {
14319 			mutex_exit(&ill->ill_lock);
14320 			return (EINVAL);
14321 		}
14322 		mutex_exit(&ill->ill_lock);
14323 
14324 		/*
14325 		 * If we're going to need to allocate ipifs, do it prior
14326 		 * to starting the move (and grabbing locks).
14327 		 */
14328 		if (ipif->ipif_id == 0) {
14329 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14330 			    B_FALSE, &err)) == NULL) {
14331 				return (err);
14332 			}
14333 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14334 			    B_FALSE, &err)) == NULL) {
14335 				mi_free(moveipif);
14336 				return (err);
14337 			}
14338 		}
14339 
14340 		/*
14341 		 * Grab or transfer the ipif to move.  During the move, keep
14342 		 * ill_g_lock held to prevent any ill walker threads from
14343 		 * seeing things in an inconsistent state.
14344 		 */
14345 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14346 		if (ipif->ipif_id != 0) {
14347 			ipif_remove(ipif);
14348 		} else {
14349 			ipif_transfer(ipif, moveipif, stubipif);
14350 			ipif = moveipif;
14351 		}
14352 
14353 		/*
14354 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14355 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14356 		 * replace that one.  Otherwise, pick the next available slot.
14357 		 */
14358 		ipif->ipif_ill = ipmp_ill;
14359 		ipif_orig_id = ipif->ipif_id;
14360 
14361 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14362 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14363 			ipif = ipmp_ill->ill_ipif;
14364 		} else {
14365 			ipif->ipif_id = -1;
14366 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14367 				/*
14368 				 * No more available ipif_id's -- put it back
14369 				 * on the original ill and fail the operation.
14370 				 * Since we're writer on the ill, we can be
14371 				 * sure our old slot is still available.
14372 				 */
14373 				ipif->ipif_id = ipif_orig_id;
14374 				ipif->ipif_ill = ill;
14375 				if (ipif_orig_id == 0) {
14376 					ipif_transfer(ipif, ill->ill_ipif,
14377 					    NULL);
14378 				} else {
14379 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14380 				}
14381 				rw_exit(&ipst->ips_ill_g_lock);
14382 				return (err);
14383 			}
14384 		}
14385 		rw_exit(&ipst->ips_ill_g_lock);
14386 
14387 		/*
14388 		 * Tell SCTP that the ipif has moved.  Note that even if we
14389 		 * had to allocate a new ipif, the original sequence id was
14390 		 * preserved and therefore SCTP won't know.
14391 		 */
14392 		sctp_move_ipif(ipif, ill, ipmp_ill);
14393 
14394 		/*
14395 		 * If the ipif being brought up was on slot zero, then we
14396 		 * first need to bring up the placeholder we stuck there.  In
14397 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14398 		 * call to ipif_up() itself, if we successfully bring up the
14399 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14400 		 */
14401 		if (ipif_orig_id == 0) {
14402 			ASSERT(ill->ill_move_ipif == NULL);
14403 			ill->ill_move_ipif = ipif;
14404 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14405 				ASSERT(ill->ill_move_ipif == NULL);
14406 			if (err != EINPROGRESS)
14407 				ill->ill_move_ipif = NULL;
14408 			return (err);
14409 		}
14410 
14411 		/*
14412 		 * Bring it up on the IPMP ill.
14413 		 */
14414 		return (ipif_up(ipif, q, mp));
14415 	}
14416 
14417 	/* Skip arp/ndp for any loopback interface. */
14418 	if (ill->ill_wq != NULL) {
14419 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14420 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14421 
14422 		if (!ill->ill_dl_up) {
14423 			/*
14424 			 * ill_dl_up is not yet set. i.e. we are yet to
14425 			 * DL_BIND with the driver and this is the first
14426 			 * logical interface on the ill to become "up".
14427 			 * Tell the driver to get going (via DL_BIND_REQ).
14428 			 * Note that changing "significant" IFF_ flags
14429 			 * address/netmask etc cause a down/up dance, but
14430 			 * does not cause an unbind (DL_UNBIND) with the driver
14431 			 */
14432 			return (ill_dl_up(ill, ipif, mp, q));
14433 		}
14434 
14435 		/*
14436 		 * ipif_resolver_up may end up needeing to bind/attach
14437 		 * the ARP stream, which in turn necessitates a
14438 		 * DLPI message exchange with the driver. ioctls are
14439 		 * serialized and so we cannot send more than one
14440 		 * interface up message at a time. If ipif_resolver_up
14441 		 * does need to wait for the DLPI handshake for the ARP stream,
14442 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14443 		 */
14444 
14445 		ASSERT(connp != NULL || !CONN_Q(q));
14446 		if (connp != NULL)
14447 			mutex_enter(&connp->conn_lock);
14448 		mutex_enter(&ill->ill_lock);
14449 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14450 		mutex_exit(&ill->ill_lock);
14451 		if (connp != NULL)
14452 			mutex_exit(&connp->conn_lock);
14453 		if (!success)
14454 			return (EINTR);
14455 
14456 		/*
14457 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14458 		 * complete when ipif_ndp_up returns.
14459 		 */
14460 		err = ipif_resolver_up(ipif, Res_act_initial);
14461 		if (err == EINPROGRESS) {
14462 			/* We will complete it in arp_bringup_done() */
14463 			return (err);
14464 		}
14465 
14466 		if (isv6 && err == 0)
14467 			err = ipif_ndp_up(ipif, B_TRUE);
14468 
14469 		ASSERT(err != EINPROGRESS);
14470 		mp = ipsq_pending_mp_get(ipsq, &connp);
14471 		ASSERT(mp != NULL);
14472 		if (err != 0)
14473 			return (err);
14474 	} else {
14475 		/*
14476 		 * Interfaces without underlying hardware don't do duplicate
14477 		 * address detection.
14478 		 */
14479 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14480 		ipif->ipif_addr_ready = 1;
14481 		err = ill_add_ires(ill);
14482 		/* allocation failure? */
14483 		if (err != 0)
14484 			return (err);
14485 	}
14486 
14487 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14488 	if (err == 0 && ill->ill_move_ipif != NULL) {
14489 		ipif = ill->ill_move_ipif;
14490 		ill->ill_move_ipif = NULL;
14491 		return (ipif_up(ipif, q, mp));
14492 	}
14493 	return (err);
14494 }
14495 
14496 /*
14497  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14498  * The identical set of IREs need to be removed in ill_delete_ires().
14499  */
14500 int
14501 ill_add_ires(ill_t *ill)
14502 {
14503 	ire_t	*ire;
14504 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14505 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14506 
14507 	if (ill->ill_ire_multicast != NULL)
14508 		return (0);
14509 
14510 	/*
14511 	 * provide some dummy ire_addr for creating the ire.
14512 	 */
14513 	if (ill->ill_isv6) {
14514 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14515 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14516 	} else {
14517 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14518 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14519 	}
14520 	if (ire == NULL)
14521 		return (ENOMEM);
14522 
14523 	ill->ill_ire_multicast = ire;
14524 	return (0);
14525 }
14526 
14527 void
14528 ill_delete_ires(ill_t *ill)
14529 {
14530 	if (ill->ill_ire_multicast != NULL) {
14531 		/*
14532 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14533 		 * which was taken without any th_tracing enabled.
14534 		 * We also mark it as condemned (note that it was never added)
14535 		 * so that caching conn's can move off of it.
14536 		 */
14537 		ire_make_condemned(ill->ill_ire_multicast);
14538 		ire_refrele_notr(ill->ill_ire_multicast);
14539 		ill->ill_ire_multicast = NULL;
14540 	}
14541 }
14542 
14543 /*
14544  * Perform a bind for the physical device.
14545  * When the routine returns EINPROGRESS then mp has been consumed and
14546  * the ioctl will be acked from ip_rput_dlpi.
14547  * Allocate an unbind message and save it until ipif_down.
14548  */
14549 static int
14550 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14551 {
14552 	mblk_t	*bind_mp = NULL;
14553 	mblk_t	*unbind_mp = NULL;
14554 	conn_t	*connp;
14555 	boolean_t success;
14556 	int	err;
14557 
14558 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14559 
14560 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14561 	ASSERT(IAM_WRITER_ILL(ill));
14562 	ASSERT(mp != NULL);
14563 
14564 	/*
14565 	 * Make sure we have an IRE_MULTICAST in case we immediately
14566 	 * start receiving packets.
14567 	 */
14568 	err = ill_add_ires(ill);
14569 	if (err != 0)
14570 		goto bad;
14571 
14572 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14573 	    DL_BIND_REQ);
14574 	if (bind_mp == NULL)
14575 		goto bad;
14576 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14577 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14578 
14579 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14580 	if (unbind_mp == NULL)
14581 		goto bad;
14582 
14583 	/*
14584 	 * Record state needed to complete this operation when the
14585 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14586 	 */
14587 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14588 	ASSERT(connp != NULL || !CONN_Q(q));
14589 	GRAB_CONN_LOCK(q);
14590 	mutex_enter(&ipif->ipif_ill->ill_lock);
14591 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14592 	mutex_exit(&ipif->ipif_ill->ill_lock);
14593 	RELEASE_CONN_LOCK(q);
14594 	if (!success)
14595 		goto bad;
14596 
14597 	/*
14598 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14599 	 * the interface goes down.
14600 	 */
14601 	ASSERT(ill->ill_unbind_mp == NULL);
14602 	ill->ill_unbind_mp = unbind_mp;
14603 
14604 	ill_dlpi_send(ill, bind_mp);
14605 	/* Send down link-layer capabilities probe if not already done. */
14606 	ill_capability_probe(ill);
14607 
14608 	/*
14609 	 * Sysid used to rely on the fact that netboots set domainname
14610 	 * and the like. Now that miniroot boots aren't strictly netboots
14611 	 * and miniroot network configuration is driven from userland
14612 	 * these things still need to be set. This situation can be detected
14613 	 * by comparing the interface being configured here to the one
14614 	 * dhcifname was set to reference by the boot loader. Once sysid is
14615 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14616 	 */
14617 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14618 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14619 	    (strlen(srpc_domain) == 0)) {
14620 		if (dhcpinit() != 0)
14621 			cmn_err(CE_WARN, "no cached dhcp response");
14622 	}
14623 
14624 	/*
14625 	 * This operation will complete in ip_rput_dlpi with either
14626 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14627 	 */
14628 	return (EINPROGRESS);
14629 bad:
14630 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14631 
14632 	freemsg(bind_mp);
14633 	freemsg(unbind_mp);
14634 	return (ENOMEM);
14635 }
14636 
14637 /* Add room for tcp+ip headers */
14638 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14639 
14640 /*
14641  * DLPI and ARP is up.
14642  * Create all the IREs associated with an interface. Bring up multicast.
14643  * Set the interface flag and finish other initialization
14644  * that potentially had to be deferred to after DL_BIND_ACK.
14645  */
14646 int
14647 ipif_up_done(ipif_t *ipif)
14648 {
14649 	ill_t		*ill = ipif->ipif_ill;
14650 	int		err = 0;
14651 	boolean_t	loopback = B_FALSE;
14652 	boolean_t	update_src_selection = B_TRUE;
14653 	ipif_t		*tmp_ipif;
14654 
14655 	ip1dbg(("ipif_up_done(%s:%u)\n",
14656 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14657 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14658 	    ill_t *, ill, ipif_t *, ipif);
14659 
14660 	/* Check if this is a loopback interface */
14661 	if (ipif->ipif_ill->ill_wq == NULL)
14662 		loopback = B_TRUE;
14663 
14664 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14665 
14666 	/*
14667 	 * If all other interfaces for this ill are down or DEPRECATED,
14668 	 * or otherwise unsuitable for source address selection,
14669 	 * reset the src generation numbers to make sure source
14670 	 * address selection gets to take this new ipif into account.
14671 	 * No need to hold ill_lock while traversing the ipif list since
14672 	 * we are writer
14673 	 */
14674 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14675 	    tmp_ipif = tmp_ipif->ipif_next) {
14676 		if (((tmp_ipif->ipif_flags &
14677 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14678 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14679 		    (tmp_ipif == ipif))
14680 			continue;
14681 		/* first useable pre-existing interface */
14682 		update_src_selection = B_FALSE;
14683 		break;
14684 	}
14685 	if (update_src_selection)
14686 		ip_update_source_selection(ill->ill_ipst);
14687 
14688 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14689 		nce_t *loop_nce = NULL;
14690 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14691 
14692 		/*
14693 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14694 		 * ipif_lookup_on_name(), but in the case of zones we can have
14695 		 * several loopback addresses on lo0. So all the interfaces with
14696 		 * loopback addresses need to be marked IRE_LOOPBACK.
14697 		 */
14698 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14699 		    htonl(INADDR_LOOPBACK))
14700 			ipif->ipif_ire_type = IRE_LOOPBACK;
14701 		else
14702 			ipif->ipif_ire_type = IRE_LOCAL;
14703 		if (ill->ill_net_type != IRE_LOOPBACK)
14704 			flags |= NCE_F_PUBLISH;
14705 
14706 		/* add unicast nce for the local addr */
14707 		err = nce_lookup_then_add_v4(ill, NULL,
14708 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14709 		    ND_REACHABLE, &loop_nce);
14710 		/* A shared-IP zone sees EEXIST for lo0:N */
14711 		if (err == 0 || err == EEXIST) {
14712 			ipif->ipif_added_nce = 1;
14713 			loop_nce->nce_ipif_cnt++;
14714 			nce_refrele(loop_nce);
14715 			err = 0;
14716 		} else {
14717 			ASSERT(loop_nce == NULL);
14718 			return (err);
14719 		}
14720 	}
14721 
14722 	/* Create all the IREs associated with this interface */
14723 	err = ipif_add_ires_v4(ipif, loopback);
14724 	if (err != 0) {
14725 		/*
14726 		 * see comments about return value from
14727 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14728 		 */
14729 		if (err != EADDRINUSE) {
14730 			(void) ipif_arp_down(ipif);
14731 		} else {
14732 			/*
14733 			 * Make IPMP aware of the deleted ipif so that
14734 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14735 			 * can be completed. Note that we do not want to
14736 			 * destroy the nce that was created on the ipmp_ill
14737 			 * for the active copy of the duplicate address in
14738 			 * use.
14739 			 */
14740 			if (IS_IPMP(ill))
14741 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14742 			err = EADDRNOTAVAIL;
14743 		}
14744 		return (err);
14745 	}
14746 
14747 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14748 		/* Recover any additional IREs entries for this ill */
14749 		(void) ill_recover_saved_ire(ill);
14750 	}
14751 
14752 	if (ill->ill_need_recover_multicast) {
14753 		/*
14754 		 * Need to recover all multicast memberships in the driver.
14755 		 * This had to be deferred until we had attached.  The same
14756 		 * code exists in ipif_up_done_v6() to recover IPv6
14757 		 * memberships.
14758 		 *
14759 		 * Note that it would be preferable to unconditionally do the
14760 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14761 		 * that since ill_join_allmulti() depends on ill_dl_up being
14762 		 * set, and it is not set until we receive a DL_BIND_ACK after
14763 		 * having called ill_dl_up().
14764 		 */
14765 		ill_recover_multicast(ill);
14766 	}
14767 
14768 	if (ill->ill_ipif_up_count == 1) {
14769 		/*
14770 		 * Since the interface is now up, it may now be active.
14771 		 */
14772 		if (IS_UNDER_IPMP(ill))
14773 			ipmp_ill_refresh_active(ill);
14774 
14775 		/*
14776 		 * If this is an IPMP interface, we may now be able to
14777 		 * establish ARP entries.
14778 		 */
14779 		if (IS_IPMP(ill))
14780 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14781 	}
14782 
14783 	/* Join the allhosts multicast address */
14784 	ipif_multicast_up(ipif);
14785 
14786 	if (!loopback && !update_src_selection &&
14787 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14788 		ip_update_source_selection(ill->ill_ipst);
14789 
14790 	if (!loopback && ipif->ipif_addr_ready) {
14791 		/* Broadcast an address mask reply. */
14792 		ipif_mask_reply(ipif);
14793 	}
14794 	/* Perhaps ilgs should use this ill */
14795 	update_conn_ill(NULL, ill->ill_ipst);
14796 
14797 	/*
14798 	 * This had to be deferred until we had bound.  Tell routing sockets and
14799 	 * others that this interface is up if it looks like the address has
14800 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14801 	 * duplicate address detection to do its thing.
14802 	 */
14803 	if (ipif->ipif_addr_ready)
14804 		ipif_up_notify(ipif);
14805 	return (0);
14806 }
14807 
14808 /*
14809  * Add the IREs associated with the ipif.
14810  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14811  */
14812 static int
14813 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14814 {
14815 	ill_t		*ill = ipif->ipif_ill;
14816 	ip_stack_t	*ipst = ill->ill_ipst;
14817 	ire_t		*ire_array[20];
14818 	ire_t		**irep = ire_array;
14819 	ire_t		**irep1;
14820 	ipaddr_t	net_mask = 0;
14821 	ipaddr_t	subnet_mask, route_mask;
14822 	int		err;
14823 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14824 	ire_t		*ire_if = NULL;
14825 	uchar_t		*gw;
14826 
14827 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14828 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14829 		/*
14830 		 * If we're on a labeled system then make sure that zone-
14831 		 * private addresses have proper remote host database entries.
14832 		 */
14833 		if (is_system_labeled() &&
14834 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14835 		    !tsol_check_interface_address(ipif))
14836 			return (EINVAL);
14837 
14838 		/* Register the source address for __sin6_src_id */
14839 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14840 		    ipif->ipif_zoneid, ipst);
14841 		if (err != 0) {
14842 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14843 			return (err);
14844 		}
14845 
14846 		if (loopback)
14847 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14848 		else
14849 			gw = NULL;
14850 
14851 		/* If the interface address is set, create the local IRE. */
14852 		ire_local = ire_create(
14853 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14854 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14855 		    gw,					/* gateway */
14856 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14857 		    ipif->ipif_ill,
14858 		    ipif->ipif_zoneid,
14859 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14860 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14861 		    NULL,
14862 		    ipst);
14863 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14864 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14865 		    ipif->ipif_ire_type,
14866 		    ntohl(ipif->ipif_lcl_addr)));
14867 		if (ire_local == NULL) {
14868 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14869 			err = ENOMEM;
14870 			goto bad;
14871 		}
14872 	} else {
14873 		ip1dbg((
14874 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14875 		    ipif->ipif_ire_type,
14876 		    ntohl(ipif->ipif_lcl_addr),
14877 		    (uint_t)ipif->ipif_flags));
14878 	}
14879 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14880 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14881 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14882 	} else {
14883 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14884 	}
14885 
14886 	subnet_mask = ipif->ipif_net_mask;
14887 
14888 	/*
14889 	 * If mask was not specified, use natural netmask of
14890 	 * interface address. Also, store this mask back into the
14891 	 * ipif struct.
14892 	 */
14893 	if (subnet_mask == 0) {
14894 		subnet_mask = net_mask;
14895 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14896 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14897 		    ipif->ipif_v6subnet);
14898 	}
14899 
14900 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14901 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14902 	    ipif->ipif_subnet != INADDR_ANY) {
14903 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14904 
14905 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14906 			route_mask = IP_HOST_MASK;
14907 		} else {
14908 			route_mask = subnet_mask;
14909 		}
14910 
14911 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14912 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14913 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14914 		    ntohl(ipif->ipif_subnet)));
14915 		ire_if = ire_create(
14916 		    (uchar_t *)&ipif->ipif_subnet,
14917 		    (uchar_t *)&route_mask,
14918 		    (uchar_t *)&ipif->ipif_lcl_addr,
14919 		    ill->ill_net_type,
14920 		    ill,
14921 		    ipif->ipif_zoneid,
14922 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14923 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14924 		    NULL,
14925 		    ipst);
14926 		if (ire_if == NULL) {
14927 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14928 			err = ENOMEM;
14929 			goto bad;
14930 		}
14931 	}
14932 
14933 	/*
14934 	 * Create any necessary broadcast IREs.
14935 	 */
14936 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14937 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14938 		irep = ipif_create_bcast_ires(ipif, irep);
14939 
14940 	/* If an earlier ire_create failed, get out now */
14941 	for (irep1 = irep; irep1 > ire_array; ) {
14942 		irep1--;
14943 		if (*irep1 == NULL) {
14944 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14945 			err = ENOMEM;
14946 			goto bad;
14947 		}
14948 	}
14949 
14950 	/*
14951 	 * Need to atomically check for IP address availability under
14952 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14953 	 * ills or new ipifs can be added while we are checking availability.
14954 	 */
14955 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14956 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14957 	/* Mark it up, and increment counters. */
14958 	ipif->ipif_flags |= IPIF_UP;
14959 	ill->ill_ipif_up_count++;
14960 	err = ip_addr_availability_check(ipif);
14961 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14962 	rw_exit(&ipst->ips_ill_g_lock);
14963 
14964 	if (err != 0) {
14965 		/*
14966 		 * Our address may already be up on the same ill. In this case,
14967 		 * the ARP entry for our ipif replaced the one for the other
14968 		 * ipif. So we don't want to delete it (otherwise the other ipif
14969 		 * would be unable to send packets).
14970 		 * ip_addr_availability_check() identifies this case for us and
14971 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14972 		 * which is the expected error code.
14973 		 */
14974 		ill->ill_ipif_up_count--;
14975 		ipif->ipif_flags &= ~IPIF_UP;
14976 		goto bad;
14977 	}
14978 
14979 	/*
14980 	 * Add in all newly created IREs.  ire_create_bcast() has
14981 	 * already checked for duplicates of the IRE_BROADCAST type.
14982 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14983 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14984 	 * a /32 route.
14985 	 */
14986 	if (ire_if != NULL) {
14987 		ire_if = ire_add(ire_if);
14988 		if (ire_if == NULL) {
14989 			err = ENOMEM;
14990 			goto bad2;
14991 		}
14992 #ifdef DEBUG
14993 		ire_refhold_notr(ire_if);
14994 		ire_refrele(ire_if);
14995 #endif
14996 	}
14997 	if (ire_local != NULL) {
14998 		ire_local = ire_add(ire_local);
14999 		if (ire_local == NULL) {
15000 			err = ENOMEM;
15001 			goto bad2;
15002 		}
15003 #ifdef DEBUG
15004 		ire_refhold_notr(ire_local);
15005 		ire_refrele(ire_local);
15006 #endif
15007 	}
15008 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15009 	if (ire_local != NULL)
15010 		ipif->ipif_ire_local = ire_local;
15011 	if (ire_if != NULL)
15012 		ipif->ipif_ire_if = ire_if;
15013 	rw_exit(&ipst->ips_ill_g_lock);
15014 	ire_local = NULL;
15015 	ire_if = NULL;
15016 
15017 	/*
15018 	 * We first add all of them, and if that succeeds we refrele the
15019 	 * bunch. That enables us to delete all of them should any of the
15020 	 * ire_adds fail.
15021 	 */
15022 	for (irep1 = irep; irep1 > ire_array; ) {
15023 		irep1--;
15024 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15025 		*irep1 = ire_add(*irep1);
15026 		if (*irep1 == NULL) {
15027 			err = ENOMEM;
15028 			goto bad2;
15029 		}
15030 	}
15031 
15032 	for (irep1 = irep; irep1 > ire_array; ) {
15033 		irep1--;
15034 		/* refheld by ire_add. */
15035 		if (*irep1 != NULL) {
15036 			ire_refrele(*irep1);
15037 			*irep1 = NULL;
15038 		}
15039 	}
15040 
15041 	if (!loopback) {
15042 		/*
15043 		 * If the broadcast address has been set, make sure it makes
15044 		 * sense based on the interface address.
15045 		 * Only match on ill since we are sharing broadcast addresses.
15046 		 */
15047 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15048 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
15049 			ire_t	*ire;
15050 
15051 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15052 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15053 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15054 
15055 			if (ire == NULL) {
15056 				/*
15057 				 * If there isn't a matching broadcast IRE,
15058 				 * revert to the default for this netmask.
15059 				 */
15060 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
15061 				mutex_enter(&ipif->ipif_ill->ill_lock);
15062 				ipif_set_default(ipif);
15063 				mutex_exit(&ipif->ipif_ill->ill_lock);
15064 			} else {
15065 				ire_refrele(ire);
15066 			}
15067 		}
15068 
15069 	}
15070 	return (0);
15071 
15072 bad2:
15073 	ill->ill_ipif_up_count--;
15074 	ipif->ipif_flags &= ~IPIF_UP;
15075 
15076 bad:
15077 	ip1dbg(("ipif_add_ires: FAILED \n"));
15078 	if (ire_local != NULL)
15079 		ire_delete(ire_local);
15080 	if (ire_if != NULL)
15081 		ire_delete(ire_if);
15082 
15083 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15084 	ire_local = ipif->ipif_ire_local;
15085 	ipif->ipif_ire_local = NULL;
15086 	ire_if = ipif->ipif_ire_if;
15087 	ipif->ipif_ire_if = NULL;
15088 	rw_exit(&ipst->ips_ill_g_lock);
15089 	if (ire_local != NULL) {
15090 		ire_delete(ire_local);
15091 		ire_refrele_notr(ire_local);
15092 	}
15093 	if (ire_if != NULL) {
15094 		ire_delete(ire_if);
15095 		ire_refrele_notr(ire_if);
15096 	}
15097 
15098 	while (irep > ire_array) {
15099 		irep--;
15100 		if (*irep != NULL) {
15101 			ire_delete(*irep);
15102 		}
15103 	}
15104 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15105 
15106 	return (err);
15107 }
15108 
15109 /* Remove all the IREs created by ipif_add_ires_v4 */
15110 void
15111 ipif_delete_ires_v4(ipif_t *ipif)
15112 {
15113 	ill_t		*ill = ipif->ipif_ill;
15114 	ip_stack_t	*ipst = ill->ill_ipst;
15115 	ire_t		*ire;
15116 
15117 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15118 	ire = ipif->ipif_ire_local;
15119 	ipif->ipif_ire_local = NULL;
15120 	rw_exit(&ipst->ips_ill_g_lock);
15121 	if (ire != NULL) {
15122 		/*
15123 		 * Move count to ipif so we don't loose the count due to
15124 		 * a down/up dance.
15125 		 */
15126 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15127 
15128 		ire_delete(ire);
15129 		ire_refrele_notr(ire);
15130 	}
15131 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15132 	ire = ipif->ipif_ire_if;
15133 	ipif->ipif_ire_if = NULL;
15134 	rw_exit(&ipst->ips_ill_g_lock);
15135 	if (ire != NULL) {
15136 		ire_delete(ire);
15137 		ire_refrele_notr(ire);
15138 	}
15139 
15140 	/*
15141 	 * Delete the broadcast IREs.
15142 	 */
15143 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15144 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15145 		ipif_delete_bcast_ires(ipif);
15146 }
15147 
15148 /*
15149  * Checks for availbility of a usable source address (if there is one) when the
15150  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15151  * this selection is done regardless of the destination.
15152  */
15153 boolean_t
15154 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15155     ip_stack_t *ipst)
15156 {
15157 	ipif_t		*ipif = NULL;
15158 	ill_t		*uill;
15159 
15160 	ASSERT(ifindex != 0);
15161 
15162 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15163 	if (uill == NULL)
15164 		return (B_FALSE);
15165 
15166 	mutex_enter(&uill->ill_lock);
15167 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15168 		if (IPIF_IS_CONDEMNED(ipif))
15169 			continue;
15170 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15171 			continue;
15172 		if (!(ipif->ipif_flags & IPIF_UP))
15173 			continue;
15174 		if (ipif->ipif_zoneid != zoneid)
15175 			continue;
15176 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15177 		    ipif->ipif_lcl_addr == INADDR_ANY)
15178 			continue;
15179 		mutex_exit(&uill->ill_lock);
15180 		ill_refrele(uill);
15181 		return (B_TRUE);
15182 	}
15183 	mutex_exit(&uill->ill_lock);
15184 	ill_refrele(uill);
15185 	return (B_FALSE);
15186 }
15187 
15188 /*
15189  * Find an ipif with a good local address on the ill+zoneid.
15190  */
15191 ipif_t *
15192 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15193 {
15194 	ipif_t		*ipif;
15195 
15196 	mutex_enter(&ill->ill_lock);
15197 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15198 		if (IPIF_IS_CONDEMNED(ipif))
15199 			continue;
15200 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15201 			continue;
15202 		if (!(ipif->ipif_flags & IPIF_UP))
15203 			continue;
15204 		if (ipif->ipif_zoneid != zoneid &&
15205 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15206 			continue;
15207 		if (ill->ill_isv6 ?
15208 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15209 		    ipif->ipif_lcl_addr == INADDR_ANY)
15210 			continue;
15211 		ipif_refhold_locked(ipif);
15212 		mutex_exit(&ill->ill_lock);
15213 		return (ipif);
15214 	}
15215 	mutex_exit(&ill->ill_lock);
15216 	return (NULL);
15217 }
15218 
15219 /*
15220  * IP source address type, sorted from worst to best.  For a given type,
15221  * always prefer IP addresses on the same subnet.  All-zones addresses are
15222  * suboptimal because they pose problems with unlabeled destinations.
15223  */
15224 typedef enum {
15225 	IPIF_NONE,
15226 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
15227 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
15228 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15229 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15230 	IPIF_DIFFNET,			/* normal and different subnet */
15231 	IPIF_SAMENET,			/* normal and same subnet */
15232 	IPIF_LOCALADDR			/* local loopback */
15233 } ipif_type_t;
15234 
15235 /*
15236  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15237  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15238  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15239  * the first one, unless IPMP is used in which case we round-robin among them;
15240  * see below for more.
15241  *
15242  * Returns NULL if there is no suitable source address for the ill.
15243  * This only occurs when there is no valid source address for the ill.
15244  */
15245 ipif_t *
15246 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15247     boolean_t allow_usesrc, boolean_t *notreadyp)
15248 {
15249 	ill_t	*usill = NULL;
15250 	ill_t	*ipmp_ill = NULL;
15251 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15252 	ipif_type_t type, best_type;
15253 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15254 	ip_stack_t *ipst = ill->ill_ipst;
15255 	boolean_t samenet;
15256 
15257 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15258 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15259 		    B_FALSE, ipst);
15260 		if (usill != NULL)
15261 			ill = usill;	/* Select source from usesrc ILL */
15262 		else
15263 			return (NULL);
15264 	}
15265 
15266 	/*
15267 	 * Test addresses should never be used for source address selection,
15268 	 * so if we were passed one, switch to the IPMP meta-interface.
15269 	 */
15270 	if (IS_UNDER_IPMP(ill)) {
15271 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15272 			ill = ipmp_ill;	/* Select source from IPMP ill */
15273 		else
15274 			return (NULL);
15275 	}
15276 
15277 	/*
15278 	 * If we're dealing with an unlabeled destination on a labeled system,
15279 	 * make sure that we ignore source addresses that are incompatible with
15280 	 * the destination's default label.  That destination's default label
15281 	 * must dominate the minimum label on the source address.
15282 	 */
15283 	dst_rhtp = NULL;
15284 	if (is_system_labeled()) {
15285 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15286 		if (dst_rhtp == NULL)
15287 			return (NULL);
15288 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15289 			TPC_RELE(dst_rhtp);
15290 			dst_rhtp = NULL;
15291 		}
15292 	}
15293 
15294 	/*
15295 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15296 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15297 	 * After selecting the right ipif, under ill_lock make sure ipif is
15298 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15299 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15300 	 * but not under a lock.
15301 	 */
15302 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15303 retry:
15304 	/*
15305 	 * For source address selection, we treat the ipif list as circular
15306 	 * and continue until we get back to where we started.  This allows
15307 	 * IPMP to vary source address selection (which improves inbound load
15308 	 * spreading) by caching its last ending point and starting from
15309 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15310 	 * ills since that can't happen on the IPMP ill.
15311 	 */
15312 	start_ipif = ill->ill_ipif;
15313 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15314 		start_ipif = ill->ill_src_ipif;
15315 
15316 	ipif = start_ipif;
15317 	best_ipif = NULL;
15318 	best_type = IPIF_NONE;
15319 	do {
15320 		if ((next_ipif = ipif->ipif_next) == NULL)
15321 			next_ipif = ill->ill_ipif;
15322 
15323 		if (IPIF_IS_CONDEMNED(ipif))
15324 			continue;
15325 		/* Always skip NOLOCAL and ANYCAST interfaces */
15326 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15327 			continue;
15328 		/* Always skip NOACCEPT interfaces */
15329 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15330 			continue;
15331 		if (!(ipif->ipif_flags & IPIF_UP))
15332 			continue;
15333 
15334 		if (!ipif->ipif_addr_ready) {
15335 			if (notreadyp != NULL)
15336 				*notreadyp = B_TRUE;
15337 			continue;
15338 		}
15339 
15340 		if (zoneid != ALL_ZONES &&
15341 		    ipif->ipif_zoneid != zoneid &&
15342 		    ipif->ipif_zoneid != ALL_ZONES)
15343 			continue;
15344 
15345 		/*
15346 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15347 		 * are not valid as source addresses.
15348 		 */
15349 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15350 			continue;
15351 
15352 		/*
15353 		 * Check compatibility of local address for destination's
15354 		 * default label if we're on a labeled system.	Incompatible
15355 		 * addresses can't be used at all.
15356 		 */
15357 		if (dst_rhtp != NULL) {
15358 			boolean_t incompat;
15359 
15360 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15361 			    IPV4_VERSION, B_FALSE);
15362 			if (src_rhtp == NULL)
15363 				continue;
15364 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15365 			    src_rhtp->tpc_tp.tp_doi !=
15366 			    dst_rhtp->tpc_tp.tp_doi ||
15367 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15368 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15369 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15370 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15371 			TPC_RELE(src_rhtp);
15372 			if (incompat)
15373 				continue;
15374 		}
15375 
15376 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15377 
15378 		if (ipif->ipif_lcl_addr == dst) {
15379 			type = IPIF_LOCALADDR;
15380 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15381 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15382 			    IPIF_DIFFNET_DEPRECATED;
15383 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15384 			type = samenet ? IPIF_SAMENET_ALLZONES :
15385 			    IPIF_DIFFNET_ALLZONES;
15386 		} else {
15387 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15388 		}
15389 
15390 		if (type > best_type) {
15391 			best_type = type;
15392 			best_ipif = ipif;
15393 			if (best_type == IPIF_LOCALADDR)
15394 				break; /* can't get better */
15395 		}
15396 	} while ((ipif = next_ipif) != start_ipif);
15397 
15398 	if ((ipif = best_ipif) != NULL) {
15399 		mutex_enter(&ipif->ipif_ill->ill_lock);
15400 		if (IPIF_IS_CONDEMNED(ipif)) {
15401 			mutex_exit(&ipif->ipif_ill->ill_lock);
15402 			goto retry;
15403 		}
15404 		ipif_refhold_locked(ipif);
15405 
15406 		/*
15407 		 * For IPMP, update the source ipif rotor to the next ipif,
15408 		 * provided we can look it up.  (We must not use it if it's
15409 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15410 		 * ipif_free() checked ill_src_ipif.)
15411 		 */
15412 		if (IS_IPMP(ill) && ipif != NULL) {
15413 			next_ipif = ipif->ipif_next;
15414 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15415 				ill->ill_src_ipif = next_ipif;
15416 			else
15417 				ill->ill_src_ipif = NULL;
15418 		}
15419 		mutex_exit(&ipif->ipif_ill->ill_lock);
15420 	}
15421 
15422 	rw_exit(&ipst->ips_ill_g_lock);
15423 	if (usill != NULL)
15424 		ill_refrele(usill);
15425 	if (ipmp_ill != NULL)
15426 		ill_refrele(ipmp_ill);
15427 	if (dst_rhtp != NULL)
15428 		TPC_RELE(dst_rhtp);
15429 
15430 #ifdef DEBUG
15431 	if (ipif == NULL) {
15432 		char buf1[INET6_ADDRSTRLEN];
15433 
15434 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15435 		    ill->ill_name,
15436 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15437 	} else {
15438 		char buf1[INET6_ADDRSTRLEN];
15439 		char buf2[INET6_ADDRSTRLEN];
15440 
15441 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15442 		    ipif->ipif_ill->ill_name,
15443 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15444 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15445 		    buf2, sizeof (buf2))));
15446 	}
15447 #endif /* DEBUG */
15448 	return (ipif);
15449 }
15450 
15451 /*
15452  * Pick a source address based on the destination ill and an optional setsrc
15453  * address.
15454  * The result is stored in srcp. If generation is set, then put the source
15455  * generation number there before we look for the source address (to avoid
15456  * missing changes in the set of source addresses.
15457  * If flagsp is set, then us it to pass back ipif_flags.
15458  *
15459  * If the caller wants to cache the returned source address and detect when
15460  * that might be stale, the caller should pass in a generation argument,
15461  * which the caller can later compare against ips_src_generation
15462  *
15463  * The precedence order for selecting an IPv4 source address is:
15464  *  - RTF_SETSRC on the offlink ire always wins.
15465  *  - If usrsrc is set, swap the ill to be the usesrc one.
15466  *  - If IPMP is used on the ill, select a random address from the most
15467  *    preferred ones below:
15468  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15469  * 2. Not deprecated, not ALL_ZONES
15470  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15471  * 4. Not deprecated, ALL_ZONES
15472  * 5. If onlink destination, same subnet and deprecated
15473  * 6. Deprecated.
15474  *
15475  * We have lower preference for ALL_ZONES IP addresses,
15476  * as they pose problems with unlabeled destinations.
15477  *
15478  * Note that when multiple IP addresses match e.g., #1 we pick
15479  * the first one if IPMP is not in use. With IPMP we randomize.
15480  */
15481 int
15482 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15483     ipaddr_t multicast_ifaddr,
15484     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15485     uint32_t *generation, uint64_t *flagsp)
15486 {
15487 	ipif_t *ipif;
15488 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15489 
15490 	if (flagsp != NULL)
15491 		*flagsp = 0;
15492 
15493 	/*
15494 	 * Need to grab the generation number before we check to
15495 	 * avoid a race with a change to the set of local addresses.
15496 	 * No lock needed since the thread which updates the set of local
15497 	 * addresses use ipif/ill locks and exit those (hence a store memory
15498 	 * barrier) before doing the atomic increase of ips_src_generation.
15499 	 */
15500 	if (generation != NULL) {
15501 		*generation = ipst->ips_src_generation;
15502 	}
15503 
15504 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15505 		*srcp = multicast_ifaddr;
15506 		return (0);
15507 	}
15508 
15509 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15510 	if (setsrc != INADDR_ANY) {
15511 		*srcp = setsrc;
15512 		return (0);
15513 	}
15514 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15515 	if (ipif == NULL) {
15516 		if (notready)
15517 			return (ENETDOWN);
15518 		else
15519 			return (EADDRNOTAVAIL);
15520 	}
15521 	*srcp = ipif->ipif_lcl_addr;
15522 	if (flagsp != NULL)
15523 		*flagsp = ipif->ipif_flags;
15524 	ipif_refrele(ipif);
15525 	return (0);
15526 }
15527 
15528 /* ARGSUSED */
15529 int
15530 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15531 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15532 {
15533 	/*
15534 	 * ill_phyint_reinit merged the v4 and v6 into a single
15535 	 * ipsq.  We might not have been able to complete the
15536 	 * operation in ipif_set_values, if we could not become
15537 	 * exclusive.  If so restart it here.
15538 	 */
15539 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15540 }
15541 
15542 /*
15543  * Can operate on either a module or a driver queue.
15544  * Returns an error if not a module queue.
15545  */
15546 /* ARGSUSED */
15547 int
15548 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15549     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15550 {
15551 	queue_t		*q1 = q;
15552 	char 		*cp;
15553 	char		interf_name[LIFNAMSIZ];
15554 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15555 
15556 	if (q->q_next == NULL) {
15557 		ip1dbg((
15558 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15559 		return (EINVAL);
15560 	}
15561 
15562 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15563 		return (EALREADY);
15564 
15565 	do {
15566 		q1 = q1->q_next;
15567 	} while (q1->q_next);
15568 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15569 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15570 
15571 	/*
15572 	 * Here we are not going to delay the ioack until after
15573 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15574 	 * original ioctl message before sending the requests.
15575 	 */
15576 	return (ipif_set_values(q, mp, interf_name, &ppa));
15577 }
15578 
15579 /* ARGSUSED */
15580 int
15581 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15582     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15583 {
15584 	return (ENXIO);
15585 }
15586 
15587 /*
15588  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15589  * `irep'.  Returns a pointer to the next free `irep' entry
15590  * A mirror exists in ipif_delete_bcast_ires().
15591  *
15592  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15593  * done in ire_add.
15594  */
15595 static ire_t **
15596 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15597 {
15598 	ipaddr_t addr;
15599 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15600 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15601 	ill_t *ill = ipif->ipif_ill;
15602 	zoneid_t zoneid = ipif->ipif_zoneid;
15603 
15604 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15605 
15606 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15607 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15608 
15609 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15610 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15611 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15612 
15613 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15614 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15615 
15616 	/*
15617 	 * For backward compatibility, we create net broadcast IREs based on
15618 	 * the old "IP address class system", since some old machines only
15619 	 * respond to these class derived net broadcast.  However, we must not
15620 	 * create these net broadcast IREs if the subnetmask is shorter than
15621 	 * the IP address class based derived netmask.  Otherwise, we may
15622 	 * create a net broadcast address which is the same as an IP address
15623 	 * on the subnet -- and then TCP will refuse to talk to that address.
15624 	 */
15625 	if (netmask < subnetmask) {
15626 		addr = netmask & ipif->ipif_subnet;
15627 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15628 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15629 	}
15630 
15631 	/*
15632 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15633 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15634 	 * created.  Creating these broadcast IREs will only create confusion
15635 	 * as `addr' will be the same as the IP address.
15636 	 */
15637 	if (subnetmask != 0xFFFFFFFF) {
15638 		addr = ipif->ipif_subnet;
15639 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15640 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15641 	}
15642 
15643 	return (irep);
15644 }
15645 
15646 /*
15647  * Mirror of ipif_create_bcast_ires()
15648  */
15649 static void
15650 ipif_delete_bcast_ires(ipif_t *ipif)
15651 {
15652 	ipaddr_t	addr;
15653 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15654 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15655 	ill_t		*ill = ipif->ipif_ill;
15656 	zoneid_t	zoneid = ipif->ipif_zoneid;
15657 	ire_t		*ire;
15658 
15659 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15660 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15661 
15662 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15663 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15664 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15665 
15666 	ire = ire_lookup_bcast(ill, 0, zoneid);
15667 	ASSERT(ire != NULL);
15668 	ire_delete(ire); ire_refrele(ire);
15669 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15670 	ASSERT(ire != NULL);
15671 	ire_delete(ire); ire_refrele(ire);
15672 
15673 	/*
15674 	 * For backward compatibility, we create net broadcast IREs based on
15675 	 * the old "IP address class system", since some old machines only
15676 	 * respond to these class derived net broadcast.  However, we must not
15677 	 * create these net broadcast IREs if the subnetmask is shorter than
15678 	 * the IP address class based derived netmask.  Otherwise, we may
15679 	 * create a net broadcast address which is the same as an IP address
15680 	 * on the subnet -- and then TCP will refuse to talk to that address.
15681 	 */
15682 	if (netmask < subnetmask) {
15683 		addr = netmask & ipif->ipif_subnet;
15684 		ire = ire_lookup_bcast(ill, addr, zoneid);
15685 		ASSERT(ire != NULL);
15686 		ire_delete(ire); ire_refrele(ire);
15687 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15688 		ASSERT(ire != NULL);
15689 		ire_delete(ire); ire_refrele(ire);
15690 	}
15691 
15692 	/*
15693 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15694 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15695 	 * created.  Creating these broadcast IREs will only create confusion
15696 	 * as `addr' will be the same as the IP address.
15697 	 */
15698 	if (subnetmask != 0xFFFFFFFF) {
15699 		addr = ipif->ipif_subnet;
15700 		ire = ire_lookup_bcast(ill, addr, zoneid);
15701 		ASSERT(ire != NULL);
15702 		ire_delete(ire); ire_refrele(ire);
15703 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15704 		ASSERT(ire != NULL);
15705 		ire_delete(ire); ire_refrele(ire);
15706 	}
15707 }
15708 
15709 /*
15710  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15711  * from lifr_flags and the name from lifr_name.
15712  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15713  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15714  * Returns EINPROGRESS when mp has been consumed by queueing it on
15715  * ipx_pending_mp and the ioctl will complete in ip_rput.
15716  *
15717  * Can operate on either a module or a driver queue.
15718  * Returns an error if not a module queue.
15719  */
15720 /* ARGSUSED */
15721 int
15722 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15723     ip_ioctl_cmd_t *ipip, void *if_req)
15724 {
15725 	ill_t	*ill = q->q_ptr;
15726 	phyint_t *phyi;
15727 	ip_stack_t *ipst;
15728 	struct lifreq *lifr = if_req;
15729 	uint64_t new_flags;
15730 
15731 	ASSERT(ipif != NULL);
15732 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15733 
15734 	if (q->q_next == NULL) {
15735 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15736 		return (EINVAL);
15737 	}
15738 
15739 	/*
15740 	 * If we are not writer on 'q' then this interface exists already
15741 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15742 	 * so return EALREADY.
15743 	 */
15744 	if (ill != ipif->ipif_ill)
15745 		return (EALREADY);
15746 
15747 	if (ill->ill_name[0] != '\0')
15748 		return (EALREADY);
15749 
15750 	/*
15751 	 * If there's another ill already with the requested name, ensure
15752 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15753 	 * fuse together two unrelated ills, which will cause chaos.
15754 	 */
15755 	ipst = ill->ill_ipst;
15756 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15757 	    lifr->lifr_name, NULL);
15758 	if (phyi != NULL) {
15759 		ill_t *ill_mate = phyi->phyint_illv4;
15760 
15761 		if (ill_mate == NULL)
15762 			ill_mate = phyi->phyint_illv6;
15763 		ASSERT(ill_mate != NULL);
15764 
15765 		if (ill_mate->ill_media->ip_m_mac_type !=
15766 		    ill->ill_media->ip_m_mac_type) {
15767 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15768 			    "use the same ill name on differing media\n"));
15769 			return (EINVAL);
15770 		}
15771 	}
15772 
15773 	/*
15774 	 * We start off as IFF_IPV4 in ipif_allocate and become
15775 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15776 	 * The only flags that we read from user space are IFF_IPV4,
15777 	 * IFF_IPV6, and IFF_BROADCAST.
15778 	 *
15779 	 * This ill has not been inserted into the global list.
15780 	 * So we are still single threaded and don't need any lock
15781 	 *
15782 	 * Saniy check the flags.
15783 	 */
15784 
15785 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15786 	    ((lifr->lifr_flags & IFF_IPV6) ||
15787 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15788 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15789 		    "or IPv6 i.e., no broadcast \n"));
15790 		return (EINVAL);
15791 	}
15792 
15793 	new_flags =
15794 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15795 
15796 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15797 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15798 		    "IFF_IPV4 or IFF_IPV6\n"));
15799 		return (EINVAL);
15800 	}
15801 
15802 	/*
15803 	 * We always start off as IPv4, so only need to check for IPv6.
15804 	 */
15805 	if ((new_flags & IFF_IPV6) != 0) {
15806 		ill->ill_flags |= ILLF_IPV6;
15807 		ill->ill_flags &= ~ILLF_IPV4;
15808 
15809 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15810 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15811 	}
15812 
15813 	if ((new_flags & IFF_BROADCAST) != 0)
15814 		ipif->ipif_flags |= IPIF_BROADCAST;
15815 	else
15816 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15817 
15818 	/* We started off as V4. */
15819 	if (ill->ill_flags & ILLF_IPV6) {
15820 		ill->ill_phyint->phyint_illv6 = ill;
15821 		ill->ill_phyint->phyint_illv4 = NULL;
15822 	}
15823 
15824 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15825 }
15826 
15827 /* ARGSUSED */
15828 int
15829 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15830     ip_ioctl_cmd_t *ipip, void *if_req)
15831 {
15832 	/*
15833 	 * ill_phyint_reinit merged the v4 and v6 into a single
15834 	 * ipsq.  We might not have been able to complete the
15835 	 * slifname in ipif_set_values, if we could not become
15836 	 * exclusive.  If so restart it here
15837 	 */
15838 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15839 }
15840 
15841 /*
15842  * Return a pointer to the ipif which matches the index, IP version type and
15843  * zoneid.
15844  */
15845 ipif_t *
15846 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15847     ip_stack_t *ipst)
15848 {
15849 	ill_t	*ill;
15850 	ipif_t	*ipif = NULL;
15851 
15852 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15853 	if (ill != NULL) {
15854 		mutex_enter(&ill->ill_lock);
15855 		for (ipif = ill->ill_ipif; ipif != NULL;
15856 		    ipif = ipif->ipif_next) {
15857 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15858 			    zoneid == ipif->ipif_zoneid ||
15859 			    ipif->ipif_zoneid == ALL_ZONES)) {
15860 				ipif_refhold_locked(ipif);
15861 				break;
15862 			}
15863 		}
15864 		mutex_exit(&ill->ill_lock);
15865 		ill_refrele(ill);
15866 	}
15867 	return (ipif);
15868 }
15869 
15870 /*
15871  * Change an existing physical interface's index. If the new index
15872  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15873  * Finally, we update other systems which may have a dependence on the
15874  * index value.
15875  */
15876 /* ARGSUSED */
15877 int
15878 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15879     ip_ioctl_cmd_t *ipip, void *ifreq)
15880 {
15881 	ill_t		*ill;
15882 	phyint_t	*phyi;
15883 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15884 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15885 	uint_t	old_index, index;
15886 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15887 	avl_index_t	where;
15888 
15889 	if (ipip->ipi_cmd_type == IF_CMD)
15890 		index = ifr->ifr_index;
15891 	else
15892 		index = lifr->lifr_index;
15893 
15894 	/*
15895 	 * Only allow on physical interface. Also, index zero is illegal.
15896 	 */
15897 	ill = ipif->ipif_ill;
15898 	phyi = ill->ill_phyint;
15899 	if (ipif->ipif_id != 0 || index == 0) {
15900 		return (EINVAL);
15901 	}
15902 
15903 	/* If the index is not changing, no work to do */
15904 	if (phyi->phyint_ifindex == index)
15905 		return (0);
15906 
15907 	/*
15908 	 * Use phyint_exists() to determine if the new interface index
15909 	 * is already in use. If the index is unused then we need to
15910 	 * change the phyint's position in the phyint_list_avl_by_index
15911 	 * tree. If we do not do this, subsequent lookups (using the new
15912 	 * index value) will not find the phyint.
15913 	 */
15914 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15915 	if (phyint_exists(index, ipst)) {
15916 		rw_exit(&ipst->ips_ill_g_lock);
15917 		return (EEXIST);
15918 	}
15919 
15920 	/*
15921 	 * The new index is unused. Set it in the phyint. However we must not
15922 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15923 	 * changes. The event must be bound to old ifindex value.
15924 	 */
15925 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15926 	    &index, sizeof (index));
15927 
15928 	old_index = phyi->phyint_ifindex;
15929 	phyi->phyint_ifindex = index;
15930 
15931 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15932 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15933 	    &index, &where);
15934 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15935 	    phyi, where);
15936 	rw_exit(&ipst->ips_ill_g_lock);
15937 
15938 	/* Update SCTP's ILL list */
15939 	sctp_ill_reindex(ill, old_index);
15940 
15941 	/* Send the routing sockets message */
15942 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15943 	if (ILL_OTHER(ill))
15944 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15945 
15946 	/* Perhaps ilgs should use this ill */
15947 	update_conn_ill(NULL, ill->ill_ipst);
15948 	return (0);
15949 }
15950 
15951 /* ARGSUSED */
15952 int
15953 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15954     ip_ioctl_cmd_t *ipip, void *ifreq)
15955 {
15956 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15957 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15958 
15959 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15960 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15961 	/* Get the interface index */
15962 	if (ipip->ipi_cmd_type == IF_CMD) {
15963 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15964 	} else {
15965 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15966 	}
15967 	return (0);
15968 }
15969 
15970 /* ARGSUSED */
15971 int
15972 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15973     ip_ioctl_cmd_t *ipip, void *ifreq)
15974 {
15975 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15976 
15977 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15978 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15979 	/* Get the interface zone */
15980 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15981 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15982 	return (0);
15983 }
15984 
15985 /*
15986  * Set the zoneid of an interface.
15987  */
15988 /* ARGSUSED */
15989 int
15990 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15991     ip_ioctl_cmd_t *ipip, void *ifreq)
15992 {
15993 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15994 	int err = 0;
15995 	boolean_t need_up = B_FALSE;
15996 	zone_t *zptr;
15997 	zone_status_t status;
15998 	zoneid_t zoneid;
15999 
16000 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16001 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16002 		if (!is_system_labeled())
16003 			return (ENOTSUP);
16004 		zoneid = GLOBAL_ZONEID;
16005 	}
16006 
16007 	/* cannot assign instance zero to a non-global zone */
16008 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16009 		return (ENOTSUP);
16010 
16011 	/*
16012 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
16013 	 * the event of a race with the zone shutdown processing, since IP
16014 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16015 	 * interface will be cleaned up even if the zone is shut down
16016 	 * immediately after the status check. If the interface can't be brought
16017 	 * down right away, and the zone is shut down before the restart
16018 	 * function is called, we resolve the possible races by rechecking the
16019 	 * zone status in the restart function.
16020 	 */
16021 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
16022 		return (EINVAL);
16023 	status = zone_status_get(zptr);
16024 	zone_rele(zptr);
16025 
16026 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16027 		return (EINVAL);
16028 
16029 	if (ipif->ipif_flags & IPIF_UP) {
16030 		/*
16031 		 * If the interface is already marked up,
16032 		 * we call ipif_down which will take care
16033 		 * of ditching any IREs that have been set
16034 		 * up based on the old interface address.
16035 		 */
16036 		err = ipif_logical_down(ipif, q, mp);
16037 		if (err == EINPROGRESS)
16038 			return (err);
16039 		(void) ipif_down_tail(ipif);
16040 		need_up = B_TRUE;
16041 	}
16042 
16043 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16044 	return (err);
16045 }
16046 
16047 static int
16048 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16049     queue_t *q, mblk_t *mp, boolean_t need_up)
16050 {
16051 	int	err = 0;
16052 	ip_stack_t	*ipst;
16053 
16054 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16055 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16056 
16057 	if (CONN_Q(q))
16058 		ipst = CONNQ_TO_IPST(q);
16059 	else
16060 		ipst = ILLQ_TO_IPST(q);
16061 
16062 	/*
16063 	 * For exclusive stacks we don't allow a different zoneid than
16064 	 * global.
16065 	 */
16066 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16067 	    zoneid != GLOBAL_ZONEID)
16068 		return (EINVAL);
16069 
16070 	/* Set the new zone id. */
16071 	ipif->ipif_zoneid = zoneid;
16072 
16073 	/* Update sctp list */
16074 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16075 
16076 	/* The default multicast interface might have changed */
16077 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16078 
16079 	if (need_up) {
16080 		/*
16081 		 * Now bring the interface back up.  If this
16082 		 * is the only IPIF for the ILL, ipif_up
16083 		 * will have to re-bind to the device, so
16084 		 * we may get back EINPROGRESS, in which
16085 		 * case, this IOCTL will get completed in
16086 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
16087 		 */
16088 		err = ipif_up(ipif, q, mp);
16089 	}
16090 	return (err);
16091 }
16092 
16093 /* ARGSUSED */
16094 int
16095 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16096     ip_ioctl_cmd_t *ipip, void *if_req)
16097 {
16098 	struct lifreq *lifr = (struct lifreq *)if_req;
16099 	zoneid_t zoneid;
16100 	zone_t *zptr;
16101 	zone_status_t status;
16102 
16103 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16104 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16105 		zoneid = GLOBAL_ZONEID;
16106 
16107 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16108 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16109 
16110 	/*
16111 	 * We recheck the zone status to resolve the following race condition:
16112 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16113 	 * 2) hme0:1 is up and can't be brought down right away;
16114 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16115 	 * 3) zone "myzone" is halted; the zone status switches to
16116 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16117 	 * the interfaces to remove - hme0:1 is not returned because it's not
16118 	 * yet in "myzone", so it won't be removed;
16119 	 * 4) the restart function for SIOCSLIFZONE is called; without the
16120 	 * status check here, we would have hme0:1 in "myzone" after it's been
16121 	 * destroyed.
16122 	 * Note that if the status check fails, we need to bring the interface
16123 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16124 	 * ipif_up_done[_v6]().
16125 	 */
16126 	status = ZONE_IS_UNINITIALIZED;
16127 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16128 		status = zone_status_get(zptr);
16129 		zone_rele(zptr);
16130 	}
16131 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16132 		if (ipif->ipif_isv6) {
16133 			(void) ipif_up_done_v6(ipif);
16134 		} else {
16135 			(void) ipif_up_done(ipif);
16136 		}
16137 		return (EINVAL);
16138 	}
16139 
16140 	(void) ipif_down_tail(ipif);
16141 
16142 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16143 	    B_TRUE));
16144 }
16145 
16146 /*
16147  * Return the number of addresses on `ill' with one or more of the values
16148  * in `set' set and all of the values in `clear' clear.
16149  */
16150 static uint_t
16151 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16152 {
16153 	ipif_t	*ipif;
16154 	uint_t	cnt = 0;
16155 
16156 	ASSERT(IAM_WRITER_ILL(ill));
16157 
16158 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16159 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16160 			cnt++;
16161 
16162 	return (cnt);
16163 }
16164 
16165 /*
16166  * Return the number of migratable addresses on `ill' that are under
16167  * application control.
16168  */
16169 uint_t
16170 ill_appaddr_cnt(const ill_t *ill)
16171 {
16172 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16173 	    IPIF_NOFAILOVER));
16174 }
16175 
16176 /*
16177  * Return the number of point-to-point addresses on `ill'.
16178  */
16179 uint_t
16180 ill_ptpaddr_cnt(const ill_t *ill)
16181 {
16182 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16183 }
16184 
16185 /* ARGSUSED */
16186 int
16187 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16188 	ip_ioctl_cmd_t *ipip, void *ifreq)
16189 {
16190 	struct lifreq	*lifr = ifreq;
16191 
16192 	ASSERT(q->q_next == NULL);
16193 	ASSERT(CONN_Q(q));
16194 
16195 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16196 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16197 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16198 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16199 
16200 	return (0);
16201 }
16202 
16203 /* Find the previous ILL in this usesrc group */
16204 static ill_t *
16205 ill_prev_usesrc(ill_t *uill)
16206 {
16207 	ill_t *ill;
16208 
16209 	for (ill = uill->ill_usesrc_grp_next;
16210 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16211 	    ill = ill->ill_usesrc_grp_next)
16212 		/* do nothing */;
16213 	return (ill);
16214 }
16215 
16216 /*
16217  * Release all members of the usesrc group. This routine is called
16218  * from ill_delete when the interface being unplumbed is the
16219  * group head.
16220  *
16221  * This silently clears the usesrc that ifconfig setup.
16222  * An alternative would be to keep that ifindex, and drop packets on the floor
16223  * since no source address can be selected.
16224  * Even if we keep the current semantics, don't need a lock and a linked list.
16225  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16226  * the one that is being removed. Issue is how we return the usesrc users
16227  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16228  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16229  * ill walk, but the walker would need to insert in the ioctl response.
16230  */
16231 static void
16232 ill_disband_usesrc_group(ill_t *uill)
16233 {
16234 	ill_t *next_ill, *tmp_ill;
16235 	ip_stack_t	*ipst = uill->ill_ipst;
16236 
16237 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16238 	next_ill = uill->ill_usesrc_grp_next;
16239 
16240 	do {
16241 		ASSERT(next_ill != NULL);
16242 		tmp_ill = next_ill->ill_usesrc_grp_next;
16243 		ASSERT(tmp_ill != NULL);
16244 		next_ill->ill_usesrc_grp_next = NULL;
16245 		next_ill->ill_usesrc_ifindex = 0;
16246 		next_ill = tmp_ill;
16247 	} while (next_ill->ill_usesrc_ifindex != 0);
16248 	uill->ill_usesrc_grp_next = NULL;
16249 }
16250 
16251 /*
16252  * Remove the client usesrc ILL from the list and relink to a new list
16253  */
16254 int
16255 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16256 {
16257 	ill_t *ill, *tmp_ill;
16258 	ip_stack_t	*ipst = ucill->ill_ipst;
16259 
16260 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16261 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16262 
16263 	/*
16264 	 * Check if the usesrc client ILL passed in is not already
16265 	 * in use as a usesrc ILL i.e one whose source address is
16266 	 * in use OR a usesrc ILL is not already in use as a usesrc
16267 	 * client ILL
16268 	 */
16269 	if ((ucill->ill_usesrc_ifindex == 0) ||
16270 	    (uill->ill_usesrc_ifindex != 0)) {
16271 		return (-1);
16272 	}
16273 
16274 	ill = ill_prev_usesrc(ucill);
16275 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16276 
16277 	/* Remove from the current list */
16278 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16279 		/* Only two elements in the list */
16280 		ASSERT(ill->ill_usesrc_ifindex == 0);
16281 		ill->ill_usesrc_grp_next = NULL;
16282 	} else {
16283 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16284 	}
16285 
16286 	if (ifindex == 0) {
16287 		ucill->ill_usesrc_ifindex = 0;
16288 		ucill->ill_usesrc_grp_next = NULL;
16289 		return (0);
16290 	}
16291 
16292 	ucill->ill_usesrc_ifindex = ifindex;
16293 	tmp_ill = uill->ill_usesrc_grp_next;
16294 	uill->ill_usesrc_grp_next = ucill;
16295 	ucill->ill_usesrc_grp_next =
16296 	    (tmp_ill != NULL) ? tmp_ill : uill;
16297 	return (0);
16298 }
16299 
16300 /*
16301  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16302  * ip.c for locking details.
16303  */
16304 /* ARGSUSED */
16305 int
16306 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16307     ip_ioctl_cmd_t *ipip, void *ifreq)
16308 {
16309 	struct lifreq *lifr = (struct lifreq *)ifreq;
16310 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16311 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16312 	int err = 0, ret;
16313 	uint_t ifindex;
16314 	ipsq_t *ipsq = NULL;
16315 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16316 
16317 	ASSERT(IAM_WRITER_IPIF(ipif));
16318 	ASSERT(q->q_next == NULL);
16319 	ASSERT(CONN_Q(q));
16320 
16321 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16322 
16323 	ifindex = lifr->lifr_index;
16324 	if (ifindex == 0) {
16325 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16326 			/* non usesrc group interface, nothing to reset */
16327 			return (0);
16328 		}
16329 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16330 		/* valid reset request */
16331 		reset_flg = B_TRUE;
16332 	}
16333 
16334 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16335 	if (usesrc_ill == NULL)
16336 		return (ENXIO);
16337 	if (usesrc_ill == ipif->ipif_ill) {
16338 		ill_refrele(usesrc_ill);
16339 		return (EINVAL);
16340 	}
16341 
16342 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16343 	    NEW_OP, B_TRUE);
16344 	if (ipsq == NULL) {
16345 		err = EINPROGRESS;
16346 		/* Operation enqueued on the ipsq of the usesrc ILL */
16347 		goto done;
16348 	}
16349 
16350 	/* USESRC isn't currently supported with IPMP */
16351 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16352 		err = ENOTSUP;
16353 		goto done;
16354 	}
16355 
16356 	/*
16357 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16358 	 * used by IPMP underlying interfaces, but someone might think it's
16359 	 * more general and try to use it independently with VNI.)
16360 	 */
16361 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16362 		err = ENOTSUP;
16363 		goto done;
16364 	}
16365 
16366 	/*
16367 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16368 	 * already a client then return EINVAL
16369 	 */
16370 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16371 		err = EINVAL;
16372 		goto done;
16373 	}
16374 
16375 	/*
16376 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16377 	 * be then this is a duplicate operation.
16378 	 */
16379 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16380 		err = 0;
16381 		goto done;
16382 	}
16383 
16384 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16385 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16386 	    usesrc_ill->ill_isv6));
16387 
16388 	/*
16389 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16390 	 * and the ill_usesrc_ifindex fields
16391 	 */
16392 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16393 
16394 	if (reset_flg) {
16395 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16396 		if (ret != 0) {
16397 			err = EINVAL;
16398 		}
16399 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16400 		goto done;
16401 	}
16402 
16403 	/*
16404 	 * Four possibilities to consider:
16405 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16406 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16407 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16408 	 * 4. Both are part of their respective usesrc groups
16409 	 */
16410 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16411 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16412 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16413 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16414 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16415 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16416 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16417 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16418 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16419 		/* Insert at head of list */
16420 		usesrc_cli_ill->ill_usesrc_grp_next =
16421 		    usesrc_ill->ill_usesrc_grp_next;
16422 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16423 	} else {
16424 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16425 		    ifindex);
16426 		if (ret != 0)
16427 			err = EINVAL;
16428 	}
16429 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16430 
16431 done:
16432 	if (ipsq != NULL)
16433 		ipsq_exit(ipsq);
16434 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16435 	ill_refrele(usesrc_ill);
16436 
16437 	/* Let conn_ixa caching know that source address selection changed */
16438 	ip_update_source_selection(ipst);
16439 
16440 	return (err);
16441 }
16442 
16443 /* ARGSUSED */
16444 int
16445 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16446     ip_ioctl_cmd_t *ipip, void *if_req)
16447 {
16448 	struct lifreq	*lifr = (struct lifreq *)if_req;
16449 	ill_t		*ill = ipif->ipif_ill;
16450 
16451 	/*
16452 	 * Need a lock since IFF_UP can be set even when there are
16453 	 * references to the ipif.
16454 	 */
16455 	mutex_enter(&ill->ill_lock);
16456 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16457 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16458 	else
16459 		lifr->lifr_dadstate = DAD_DONE;
16460 	mutex_exit(&ill->ill_lock);
16461 	return (0);
16462 }
16463 
16464 /*
16465  * comparison function used by avl.
16466  */
16467 static int
16468 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16469 {
16470 
16471 	uint_t index;
16472 
16473 	ASSERT(phyip != NULL && index_ptr != NULL);
16474 
16475 	index = *((uint_t *)index_ptr);
16476 	/*
16477 	 * let the phyint with the lowest index be on top.
16478 	 */
16479 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16480 		return (1);
16481 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16482 		return (-1);
16483 	return (0);
16484 }
16485 
16486 /*
16487  * comparison function used by avl.
16488  */
16489 static int
16490 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16491 {
16492 	ill_t *ill;
16493 	int res = 0;
16494 
16495 	ASSERT(phyip != NULL && name_ptr != NULL);
16496 
16497 	if (((phyint_t *)phyip)->phyint_illv4)
16498 		ill = ((phyint_t *)phyip)->phyint_illv4;
16499 	else
16500 		ill = ((phyint_t *)phyip)->phyint_illv6;
16501 	ASSERT(ill != NULL);
16502 
16503 	res = strcmp(ill->ill_name, (char *)name_ptr);
16504 	if (res > 0)
16505 		return (1);
16506 	else if (res < 0)
16507 		return (-1);
16508 	return (0);
16509 }
16510 
16511 /*
16512  * This function is called on the unplumb path via ill_glist_delete() when
16513  * there are no ills left on the phyint and thus the phyint can be freed.
16514  */
16515 static void
16516 phyint_free(phyint_t *phyi)
16517 {
16518 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16519 
16520 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16521 
16522 	/*
16523 	 * If this phyint was an IPMP meta-interface, blow away the group.
16524 	 * This is safe to do because all of the illgrps have already been
16525 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16526 	 * If we're cleaning up as a result of failed initialization,
16527 	 * phyint_grp may be NULL.
16528 	 */
16529 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16530 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16531 		ipmp_grp_destroy(phyi->phyint_grp);
16532 		phyi->phyint_grp = NULL;
16533 		rw_exit(&ipst->ips_ipmp_lock);
16534 	}
16535 
16536 	/*
16537 	 * If this interface was under IPMP, take it out of the group.
16538 	 */
16539 	if (phyi->phyint_grp != NULL)
16540 		ipmp_phyint_leave_grp(phyi);
16541 
16542 	/*
16543 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16544 	 * will be freed in ipsq_exit().
16545 	 */
16546 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16547 	phyi->phyint_name[0] = '\0';
16548 
16549 	mi_free(phyi);
16550 }
16551 
16552 /*
16553  * Attach the ill to the phyint structure which can be shared by both
16554  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16555  * function is called from ipif_set_values and ill_lookup_on_name (for
16556  * loopback) where we know the name of the ill. We lookup the ill and if
16557  * there is one present already with the name use that phyint. Otherwise
16558  * reuse the one allocated by ill_init.
16559  */
16560 static void
16561 ill_phyint_reinit(ill_t *ill)
16562 {
16563 	boolean_t isv6 = ill->ill_isv6;
16564 	phyint_t *phyi_old;
16565 	phyint_t *phyi;
16566 	avl_index_t where = 0;
16567 	ill_t	*ill_other = NULL;
16568 	ip_stack_t	*ipst = ill->ill_ipst;
16569 
16570 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16571 
16572 	phyi_old = ill->ill_phyint;
16573 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16574 	    phyi_old->phyint_illv6 == NULL));
16575 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16576 	    phyi_old->phyint_illv4 == NULL));
16577 	ASSERT(phyi_old->phyint_ifindex == 0);
16578 
16579 	/*
16580 	 * Now that our ill has a name, set it in the phyint.
16581 	 */
16582 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16583 
16584 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16585 	    ill->ill_name, &where);
16586 
16587 	/*
16588 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16589 	 *    the global list of ills. So no other thread could have located
16590 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16591 	 * 2. Now locate the other protocol instance of this ill.
16592 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16593 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16594 	 *    of neither ill can change.
16595 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16596 	 *    other ill.
16597 	 * 5. Release all locks.
16598 	 */
16599 
16600 	/*
16601 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16602 	 * we are initializing IPv4.
16603 	 */
16604 	if (phyi != NULL) {
16605 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16606 		ASSERT(ill_other->ill_phyint != NULL);
16607 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16608 		    (!isv6 && ill_other->ill_isv6));
16609 		GRAB_ILL_LOCKS(ill, ill_other);
16610 		/*
16611 		 * We are potentially throwing away phyint_flags which
16612 		 * could be different from the one that we obtain from
16613 		 * ill_other->ill_phyint. But it is okay as we are assuming
16614 		 * that the state maintained within IP is correct.
16615 		 */
16616 		mutex_enter(&phyi->phyint_lock);
16617 		if (isv6) {
16618 			ASSERT(phyi->phyint_illv6 == NULL);
16619 			phyi->phyint_illv6 = ill;
16620 		} else {
16621 			ASSERT(phyi->phyint_illv4 == NULL);
16622 			phyi->phyint_illv4 = ill;
16623 		}
16624 
16625 		/*
16626 		 * Delete the old phyint and make its ipsq eligible
16627 		 * to be freed in ipsq_exit().
16628 		 */
16629 		phyi_old->phyint_illv4 = NULL;
16630 		phyi_old->phyint_illv6 = NULL;
16631 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16632 		phyi_old->phyint_name[0] = '\0';
16633 		mi_free(phyi_old);
16634 	} else {
16635 		mutex_enter(&ill->ill_lock);
16636 		/*
16637 		 * We don't need to acquire any lock, since
16638 		 * the ill is not yet visible globally  and we
16639 		 * have not yet released the ill_g_lock.
16640 		 */
16641 		phyi = phyi_old;
16642 		mutex_enter(&phyi->phyint_lock);
16643 		/* XXX We need a recovery strategy here. */
16644 		if (!phyint_assign_ifindex(phyi, ipst))
16645 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16646 
16647 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16648 		    (void *)phyi, where);
16649 
16650 		(void) avl_find(&ipst->ips_phyint_g_list->
16651 		    phyint_list_avl_by_index,
16652 		    &phyi->phyint_ifindex, &where);
16653 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16654 		    (void *)phyi, where);
16655 	}
16656 
16657 	/*
16658 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16659 	 * pending mp is not affected because that is per ill basis.
16660 	 */
16661 	ill->ill_phyint = phyi;
16662 
16663 	/*
16664 	 * Now that the phyint's ifindex has been assigned, complete the
16665 	 * remaining
16666 	 */
16667 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16668 	if (ill->ill_isv6) {
16669 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16670 		    ill->ill_phyint->phyint_ifindex;
16671 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16672 	} else {
16673 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16674 	}
16675 
16676 	/*
16677 	 * Generate an event within the hooks framework to indicate that
16678 	 * a new interface has just been added to IP.  For this event to
16679 	 * be generated, the network interface must, at least, have an
16680 	 * ifindex assigned to it.  (We don't generate the event for
16681 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16682 	 *
16683 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16684 	 * that the ordering of delivered events to listeners matches the
16685 	 * order of them in the kernel.
16686 	 */
16687 	if (!IS_LOOPBACK(ill)) {
16688 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16689 		    ill->ill_name_length);
16690 	}
16691 	RELEASE_ILL_LOCKS(ill, ill_other);
16692 	mutex_exit(&phyi->phyint_lock);
16693 }
16694 
16695 /*
16696  * Notify any downstream modules of the name of this interface.
16697  * An M_IOCTL is used even though we don't expect a successful reply.
16698  * Any reply message from the driver (presumably an M_IOCNAK) will
16699  * eventually get discarded somewhere upstream.  The message format is
16700  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16701  * to IP.
16702  */
16703 static void
16704 ip_ifname_notify(ill_t *ill, queue_t *q)
16705 {
16706 	mblk_t *mp1, *mp2;
16707 	struct iocblk *iocp;
16708 	struct lifreq *lifr;
16709 
16710 	mp1 = mkiocb(SIOCSLIFNAME);
16711 	if (mp1 == NULL)
16712 		return;
16713 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16714 	if (mp2 == NULL) {
16715 		freeb(mp1);
16716 		return;
16717 	}
16718 
16719 	mp1->b_cont = mp2;
16720 	iocp = (struct iocblk *)mp1->b_rptr;
16721 	iocp->ioc_count = sizeof (struct lifreq);
16722 
16723 	lifr = (struct lifreq *)mp2->b_rptr;
16724 	mp2->b_wptr += sizeof (struct lifreq);
16725 	bzero(lifr, sizeof (struct lifreq));
16726 
16727 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16728 	lifr->lifr_ppa = ill->ill_ppa;
16729 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16730 
16731 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16732 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16733 	putnext(q, mp1);
16734 }
16735 
16736 static int
16737 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16738 {
16739 	int		err;
16740 	ip_stack_t	*ipst = ill->ill_ipst;
16741 	phyint_t	*phyi = ill->ill_phyint;
16742 
16743 	/*
16744 	 * Now that ill_name is set, the configuration for the IPMP
16745 	 * meta-interface can be performed.
16746 	 */
16747 	if (IS_IPMP(ill)) {
16748 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16749 		/*
16750 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16751 		 * meta-interface and we need to create the IPMP group.
16752 		 */
16753 		if (phyi->phyint_grp == NULL) {
16754 			/*
16755 			 * If someone has renamed another IPMP group to have
16756 			 * the same name as our interface, bail.
16757 			 */
16758 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16759 				rw_exit(&ipst->ips_ipmp_lock);
16760 				return (EEXIST);
16761 			}
16762 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16763 			if (phyi->phyint_grp == NULL) {
16764 				rw_exit(&ipst->ips_ipmp_lock);
16765 				return (ENOMEM);
16766 			}
16767 		}
16768 		rw_exit(&ipst->ips_ipmp_lock);
16769 	}
16770 
16771 	/* Tell downstream modules where they are. */
16772 	ip_ifname_notify(ill, q);
16773 
16774 	/*
16775 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16776 	 * Error cases are ENOMEM ...
16777 	 */
16778 	err = ill_dl_phys(ill, ipif, mp, q);
16779 
16780 	if (ill->ill_isv6) {
16781 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16782 		if (ipst->ips_mld_slowtimeout_id == 0) {
16783 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16784 			    (void *)ipst,
16785 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16786 		}
16787 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16788 	} else {
16789 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16790 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16791 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16792 			    (void *)ipst,
16793 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16794 		}
16795 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16796 	}
16797 
16798 	return (err);
16799 }
16800 
16801 /*
16802  * Common routine for ppa and ifname setting. Should be called exclusive.
16803  *
16804  * Returns EINPROGRESS when mp has been consumed by queueing it on
16805  * ipx_pending_mp and the ioctl will complete in ip_rput.
16806  *
16807  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16808  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16809  * For SLIFNAME, we pass these values back to the userland.
16810  */
16811 static int
16812 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16813 {
16814 	ill_t	*ill;
16815 	ipif_t	*ipif;
16816 	ipsq_t	*ipsq;
16817 	char	*ppa_ptr;
16818 	char	*old_ptr;
16819 	char	old_char;
16820 	int	error;
16821 	ip_stack_t	*ipst;
16822 
16823 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16824 	ASSERT(q->q_next != NULL);
16825 	ASSERT(interf_name != NULL);
16826 
16827 	ill = (ill_t *)q->q_ptr;
16828 	ipst = ill->ill_ipst;
16829 
16830 	ASSERT(ill->ill_ipst != NULL);
16831 	ASSERT(ill->ill_name[0] == '\0');
16832 	ASSERT(IAM_WRITER_ILL(ill));
16833 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16834 	ASSERT(ill->ill_ppa == UINT_MAX);
16835 
16836 	ill->ill_defend_start = ill->ill_defend_count = 0;
16837 	/* The ppa is sent down by ifconfig or is chosen */
16838 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16839 		return (EINVAL);
16840 	}
16841 
16842 	/*
16843 	 * make sure ppa passed in is same as ppa in the name.
16844 	 * This check is not made when ppa == UINT_MAX in that case ppa
16845 	 * in the name could be anything. System will choose a ppa and
16846 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16847 	 */
16848 	if (*new_ppa_ptr != UINT_MAX) {
16849 		/* stoi changes the pointer */
16850 		old_ptr = ppa_ptr;
16851 		/*
16852 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16853 		 * (they don't have an externally visible ppa).  We assign one
16854 		 * here so that we can manage the interface.  Note that in
16855 		 * the past this value was always 0 for DLPI 1 drivers.
16856 		 */
16857 		if (*new_ppa_ptr == 0)
16858 			*new_ppa_ptr = stoi(&old_ptr);
16859 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16860 			return (EINVAL);
16861 	}
16862 	/*
16863 	 * terminate string before ppa
16864 	 * save char at that location.
16865 	 */
16866 	old_char = ppa_ptr[0];
16867 	ppa_ptr[0] = '\0';
16868 
16869 	ill->ill_ppa = *new_ppa_ptr;
16870 	/*
16871 	 * Finish as much work now as possible before calling ill_glist_insert
16872 	 * which makes the ill globally visible and also merges it with the
16873 	 * other protocol instance of this phyint. The remaining work is
16874 	 * done after entering the ipsq which may happen sometime later.
16875 	 */
16876 	ipif = ill->ill_ipif;
16877 
16878 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16879 	ipif_assign_seqid(ipif);
16880 
16881 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16882 		ill->ill_flags |= ILLF_IPV4;
16883 
16884 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16885 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16886 
16887 	if (ill->ill_flags & ILLF_IPV6) {
16888 
16889 		ill->ill_isv6 = B_TRUE;
16890 		ill_set_inputfn(ill);
16891 		if (ill->ill_rq != NULL) {
16892 			ill->ill_rq->q_qinfo = &iprinitv6;
16893 		}
16894 
16895 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16896 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16897 		ipif->ipif_v6subnet = ipv6_all_zeros;
16898 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16899 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16900 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16901 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16902 		/*
16903 		 * point-to-point or Non-mulicast capable
16904 		 * interfaces won't do NUD unless explicitly
16905 		 * configured to do so.
16906 		 */
16907 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16908 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16909 			ill->ill_flags |= ILLF_NONUD;
16910 		}
16911 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16912 		if (ill->ill_flags & ILLF_NOARP) {
16913 			/*
16914 			 * Note: xresolv interfaces will eventually need
16915 			 * NOARP set here as well, but that will require
16916 			 * those external resolvers to have some
16917 			 * knowledge of that flag and act appropriately.
16918 			 * Not to be changed at present.
16919 			 */
16920 			ill->ill_flags &= ~ILLF_NOARP;
16921 		}
16922 		/*
16923 		 * Set the ILLF_ROUTER flag according to the global
16924 		 * IPv6 forwarding policy.
16925 		 */
16926 		if (ipst->ips_ipv6_forwarding != 0)
16927 			ill->ill_flags |= ILLF_ROUTER;
16928 	} else if (ill->ill_flags & ILLF_IPV4) {
16929 		ill->ill_isv6 = B_FALSE;
16930 		ill_set_inputfn(ill);
16931 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16932 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16933 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16934 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16935 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16936 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16937 		/*
16938 		 * Set the ILLF_ROUTER flag according to the global
16939 		 * IPv4 forwarding policy.
16940 		 */
16941 		if (ipst->ips_ip_forwarding != 0)
16942 			ill->ill_flags |= ILLF_ROUTER;
16943 	}
16944 
16945 	ASSERT(ill->ill_phyint != NULL);
16946 
16947 	/*
16948 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16949 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16950 	 */
16951 	if (!ill_allocate_mibs(ill))
16952 		return (ENOMEM);
16953 
16954 	/*
16955 	 * Pick a default sap until we get the DL_INFO_ACK back from
16956 	 * the driver.
16957 	 */
16958 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16959 	    ill->ill_media->ip_m_ipv4sap;
16960 
16961 	ill->ill_ifname_pending = 1;
16962 	ill->ill_ifname_pending_err = 0;
16963 
16964 	/*
16965 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16966 	 * that were joined while this ill was not bound to the DLPI link need
16967 	 * to be recovered by ill_recover_multicast().
16968 	 */
16969 	ill->ill_need_recover_multicast = 1;
16970 
16971 	ill_refhold(ill);
16972 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16973 	if ((error = ill_glist_insert(ill, interf_name,
16974 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16975 		ill->ill_ppa = UINT_MAX;
16976 		ill->ill_name[0] = '\0';
16977 		/*
16978 		 * undo null termination done above.
16979 		 */
16980 		ppa_ptr[0] = old_char;
16981 		rw_exit(&ipst->ips_ill_g_lock);
16982 		ill_refrele(ill);
16983 		return (error);
16984 	}
16985 
16986 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16987 
16988 	/*
16989 	 * When we return the buffer pointed to by interf_name should contain
16990 	 * the same name as in ill_name.
16991 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16992 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16993 	 * so copy full name and update the ppa ptr.
16994 	 * When ppa passed in != UINT_MAX all values are correct just undo
16995 	 * null termination, this saves a bcopy.
16996 	 */
16997 	if (*new_ppa_ptr == UINT_MAX) {
16998 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16999 		*new_ppa_ptr = ill->ill_ppa;
17000 	} else {
17001 		/*
17002 		 * undo null termination done above.
17003 		 */
17004 		ppa_ptr[0] = old_char;
17005 	}
17006 
17007 	/* Let SCTP know about this ILL */
17008 	sctp_update_ill(ill, SCTP_ILL_INSERT);
17009 
17010 	/*
17011 	 * ill_glist_insert has made the ill visible globally, and
17012 	 * ill_phyint_reinit could have changed the ipsq. At this point,
17013 	 * we need to hold the ips_ill_g_lock across the call to enter the
17014 	 * ipsq to enforce atomicity and prevent reordering. In the event
17015 	 * the ipsq has changed, and if the new ipsq is currently busy,
17016 	 * we need to make sure that this half-completed ioctl is ahead of
17017 	 * any subsequent ioctl. We achieve this by not dropping the
17018 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
17019 	 * ensuring that new ioctls can't start.
17020 	 */
17021 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17022 	    B_TRUE);
17023 
17024 	rw_exit(&ipst->ips_ill_g_lock);
17025 	ill_refrele(ill);
17026 	if (ipsq == NULL)
17027 		return (EINPROGRESS);
17028 
17029 	/*
17030 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17031 	 */
17032 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17033 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17034 	else
17035 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17036 
17037 	error = ipif_set_values_tail(ill, ipif, mp, q);
17038 	ipsq_exit(ipsq);
17039 	if (error != 0 && error != EINPROGRESS) {
17040 		/*
17041 		 * restore previous values
17042 		 */
17043 		ill->ill_isv6 = B_FALSE;
17044 		ill_set_inputfn(ill);
17045 	}
17046 	return (error);
17047 }
17048 
17049 void
17050 ipif_init(ip_stack_t *ipst)
17051 {
17052 	int i;
17053 
17054 	for (i = 0; i < MAX_G_HEADS; i++) {
17055 		ipst->ips_ill_g_heads[i].ill_g_list_head =
17056 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17057 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
17058 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17059 	}
17060 
17061 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17062 	    ill_phyint_compare_index,
17063 	    sizeof (phyint_t),
17064 	    offsetof(struct phyint, phyint_avl_by_index));
17065 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17066 	    ill_phyint_compare_name,
17067 	    sizeof (phyint_t),
17068 	    offsetof(struct phyint, phyint_avl_by_name));
17069 }
17070 
17071 /*
17072  * Save enough information so that we can recreate the IRE if
17073  * the interface goes down and then up.
17074  */
17075 void
17076 ill_save_ire(ill_t *ill, ire_t *ire)
17077 {
17078 	mblk_t	*save_mp;
17079 
17080 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17081 	if (save_mp != NULL) {
17082 		ifrt_t	*ifrt;
17083 
17084 		save_mp->b_wptr += sizeof (ifrt_t);
17085 		ifrt = (ifrt_t *)save_mp->b_rptr;
17086 		bzero(ifrt, sizeof (ifrt_t));
17087 		ifrt->ifrt_type = ire->ire_type;
17088 		if (ire->ire_ipversion == IPV4_VERSION) {
17089 			ASSERT(!ill->ill_isv6);
17090 			ifrt->ifrt_addr = ire->ire_addr;
17091 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17092 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17093 			ifrt->ifrt_mask = ire->ire_mask;
17094 		} else {
17095 			ASSERT(ill->ill_isv6);
17096 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
17097 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17098 			mutex_enter(&ire->ire_lock);
17099 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17100 			mutex_exit(&ire->ire_lock);
17101 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17102 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
17103 		}
17104 		ifrt->ifrt_flags = ire->ire_flags;
17105 		ifrt->ifrt_zoneid = ire->ire_zoneid;
17106 		mutex_enter(&ill->ill_saved_ire_lock);
17107 		save_mp->b_cont = ill->ill_saved_ire_mp;
17108 		ill->ill_saved_ire_mp = save_mp;
17109 		ill->ill_saved_ire_cnt++;
17110 		mutex_exit(&ill->ill_saved_ire_lock);
17111 	}
17112 }
17113 
17114 /*
17115  * Remove one entry from ill_saved_ire_mp.
17116  */
17117 void
17118 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17119 {
17120 	mblk_t	**mpp;
17121 	mblk_t	*mp;
17122 	ifrt_t	*ifrt;
17123 
17124 	/* Remove from ill_saved_ire_mp list if it is there */
17125 	mutex_enter(&ill->ill_saved_ire_lock);
17126 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17127 	    mpp = &(*mpp)->b_cont) {
17128 		in6_addr_t	gw_addr_v6;
17129 
17130 		/*
17131 		 * On a given ill, the tuple of address, gateway, mask,
17132 		 * ire_type, and zoneid is unique for each saved IRE.
17133 		 */
17134 		mp = *mpp;
17135 		ifrt = (ifrt_t *)mp->b_rptr;
17136 		/* ire_gateway_addr_v6 can change - need lock */
17137 		mutex_enter(&ire->ire_lock);
17138 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17139 		mutex_exit(&ire->ire_lock);
17140 
17141 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17142 		    ifrt->ifrt_type != ire->ire_type)
17143 			continue;
17144 
17145 		if (ill->ill_isv6 ?
17146 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17147 		    &ire->ire_addr_v6) &&
17148 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17149 		    &gw_addr_v6) &&
17150 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17151 		    &ire->ire_mask_v6)) :
17152 		    (ifrt->ifrt_addr == ire->ire_addr &&
17153 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17154 		    ifrt->ifrt_mask == ire->ire_mask)) {
17155 			*mpp = mp->b_cont;
17156 			ill->ill_saved_ire_cnt--;
17157 			freeb(mp);
17158 			break;
17159 		}
17160 	}
17161 	mutex_exit(&ill->ill_saved_ire_lock);
17162 }
17163 
17164 /*
17165  * IP multirouting broadcast routes handling
17166  * Append CGTP broadcast IREs to regular ones created
17167  * at ifconfig time.
17168  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17169  * the destination and the gateway are broadcast addresses.
17170  * The caller has verified that the destination is an IRE_BROADCAST and that
17171  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17172  * we create a MULTIRT IRE_BROADCAST.
17173  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17174  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17175  */
17176 static void
17177 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17178 {
17179 	ire_t *ire_prim;
17180 
17181 	ASSERT(ire != NULL);
17182 
17183 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17184 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17185 	    NULL);
17186 	if (ire_prim != NULL) {
17187 		/*
17188 		 * We are in the special case of broadcasts for
17189 		 * CGTP. We add an IRE_BROADCAST that holds
17190 		 * the RTF_MULTIRT flag, the destination
17191 		 * address and the low level
17192 		 * info of ire_prim. In other words, CGTP
17193 		 * broadcast is added to the redundant ipif.
17194 		 */
17195 		ill_t *ill_prim;
17196 		ire_t  *bcast_ire;
17197 
17198 		ill_prim = ire_prim->ire_ill;
17199 
17200 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17201 		    (void *)ire_prim, (void *)ill_prim));
17202 
17203 		bcast_ire = ire_create(
17204 		    (uchar_t *)&ire->ire_addr,
17205 		    (uchar_t *)&ip_g_all_ones,
17206 		    (uchar_t *)&ire->ire_gateway_addr,
17207 		    IRE_BROADCAST,
17208 		    ill_prim,
17209 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17210 		    ire->ire_flags | RTF_KERNEL,
17211 		    NULL,
17212 		    ipst);
17213 
17214 		/*
17215 		 * Here we assume that ire_add does head insertion so that
17216 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17217 		 */
17218 		if (bcast_ire != NULL) {
17219 			if (ire->ire_flags & RTF_SETSRC) {
17220 				bcast_ire->ire_setsrc_addr =
17221 				    ire->ire_setsrc_addr;
17222 			}
17223 			bcast_ire = ire_add(bcast_ire);
17224 			if (bcast_ire != NULL) {
17225 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17226 				    "added bcast_ire %p\n",
17227 				    (void *)bcast_ire));
17228 
17229 				ill_save_ire(ill_prim, bcast_ire);
17230 				ire_refrele(bcast_ire);
17231 			}
17232 		}
17233 		ire_refrele(ire_prim);
17234 	}
17235 }
17236 
17237 /*
17238  * IP multirouting broadcast routes handling
17239  * Remove the broadcast ire.
17240  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17241  * the destination and the gateway are broadcast addresses.
17242  * The caller has only verified that RTF_MULTIRT was set. We check
17243  * that the destination is broadcast and that the gateway is a broadcast
17244  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17245  */
17246 static void
17247 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17248 {
17249 	ASSERT(ire != NULL);
17250 
17251 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17252 		ire_t *ire_prim;
17253 
17254 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17255 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17256 		    ipst, NULL);
17257 		if (ire_prim != NULL) {
17258 			ill_t *ill_prim;
17259 			ire_t  *bcast_ire;
17260 
17261 			ill_prim = ire_prim->ire_ill;
17262 
17263 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17264 			    "ire_prim %p, ill_prim %p\n",
17265 			    (void *)ire_prim, (void *)ill_prim));
17266 
17267 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17268 			    ire->ire_gateway_addr, IRE_BROADCAST,
17269 			    ill_prim, ALL_ZONES, NULL,
17270 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17271 			    MATCH_IRE_MASK, 0, ipst, NULL);
17272 
17273 			if (bcast_ire != NULL) {
17274 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17275 				    "looked up bcast_ire %p\n",
17276 				    (void *)bcast_ire));
17277 				ill_remove_saved_ire(bcast_ire->ire_ill,
17278 				    bcast_ire);
17279 				ire_delete(bcast_ire);
17280 				ire_refrele(bcast_ire);
17281 			}
17282 			ire_refrele(ire_prim);
17283 		}
17284 	}
17285 }
17286 
17287 /*
17288  * Derive an interface id from the link layer address.
17289  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17290  */
17291 static void
17292 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17293 {
17294 	char		*addr;
17295 
17296 	/*
17297 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17298 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17299 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17300 	 * interface ID on IPv6 interfaces above links that actually have real
17301 	 * Ethernet addresses.
17302 	 */
17303 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17304 		/* Form EUI-64 like address */
17305 		addr = (char *)&v6addr->s6_addr32[2];
17306 		bcopy(ill->ill_phys_addr, addr, 3);
17307 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17308 		addr[3] = (char)0xff;
17309 		addr[4] = (char)0xfe;
17310 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17311 	}
17312 }
17313 
17314 /* ARGSUSED */
17315 static void
17316 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17317 {
17318 }
17319 
17320 typedef struct ipmp_ifcookie {
17321 	uint32_t	ic_hostid;
17322 	char		ic_ifname[LIFNAMSIZ];
17323 	char		ic_zonename[ZONENAME_MAX];
17324 } ipmp_ifcookie_t;
17325 
17326 /*
17327  * Construct a pseudo-random interface ID for the IPMP interface that's both
17328  * predictable and (almost) guaranteed to be unique.
17329  */
17330 static void
17331 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17332 {
17333 	zone_t		*zp;
17334 	uint8_t		*addr;
17335 	uchar_t		hash[16];
17336 	ulong_t 	hostid;
17337 	MD5_CTX		ctx;
17338 	ipmp_ifcookie_t	ic = { 0 };
17339 
17340 	ASSERT(IS_IPMP(ill));
17341 
17342 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17343 	ic.ic_hostid = htonl((uint32_t)hostid);
17344 
17345 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17346 
17347 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17348 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17349 		zone_rele(zp);
17350 	}
17351 
17352 	MD5Init(&ctx);
17353 	MD5Update(&ctx, &ic, sizeof (ic));
17354 	MD5Final(hash, &ctx);
17355 
17356 	/*
17357 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17358 	 */
17359 	addr = &v6addr->s6_addr8[8];
17360 	bcopy(hash + 8, addr, sizeof (uint64_t));
17361 	addr[0] &= ~0x2;				/* set local bit */
17362 }
17363 
17364 /*
17365  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17366  */
17367 static void
17368 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17369 {
17370 	phyint_t *phyi = ill->ill_phyint;
17371 
17372 	/*
17373 	 * Check PHYI_MULTI_BCAST and length of physical
17374 	 * address to determine if we use the mapping or the
17375 	 * broadcast address.
17376 	 */
17377 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17378 	    ill->ill_phys_addr_length != ETHERADDRL) {
17379 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17380 		return;
17381 	}
17382 	m_physaddr[0] = 0x33;
17383 	m_physaddr[1] = 0x33;
17384 	m_physaddr[2] = m_ip6addr[12];
17385 	m_physaddr[3] = m_ip6addr[13];
17386 	m_physaddr[4] = m_ip6addr[14];
17387 	m_physaddr[5] = m_ip6addr[15];
17388 }
17389 
17390 /*
17391  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17392  */
17393 static void
17394 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17395 {
17396 	phyint_t *phyi = ill->ill_phyint;
17397 
17398 	/*
17399 	 * Check PHYI_MULTI_BCAST and length of physical
17400 	 * address to determine if we use the mapping or the
17401 	 * broadcast address.
17402 	 */
17403 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17404 	    ill->ill_phys_addr_length != ETHERADDRL) {
17405 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17406 		return;
17407 	}
17408 	m_physaddr[0] = 0x01;
17409 	m_physaddr[1] = 0x00;
17410 	m_physaddr[2] = 0x5e;
17411 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17412 	m_physaddr[4] = m_ipaddr[2];
17413 	m_physaddr[5] = m_ipaddr[3];
17414 }
17415 
17416 /* ARGSUSED */
17417 static void
17418 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17419 {
17420 	/*
17421 	 * for the MULTI_BCAST case and other cases when we want to
17422 	 * use the link-layer broadcast address for multicast.
17423 	 */
17424 	uint8_t	*bphys_addr;
17425 	dl_unitdata_req_t *dlur;
17426 
17427 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17428 	if (ill->ill_sap_length < 0) {
17429 		bphys_addr = (uchar_t *)dlur +
17430 		    dlur->dl_dest_addr_offset;
17431 	} else  {
17432 		bphys_addr = (uchar_t *)dlur +
17433 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17434 	}
17435 
17436 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17437 }
17438 
17439 /*
17440  * Derive IPoIB interface id from the link layer address.
17441  */
17442 static void
17443 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17444 {
17445 	char		*addr;
17446 
17447 	ASSERT(ill->ill_phys_addr_length == 20);
17448 	addr = (char *)&v6addr->s6_addr32[2];
17449 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17450 	/*
17451 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17452 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17453 	 * rules. In these cases, the IBA considers these GUIDs to be in
17454 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17455 	 * required; vendors are required not to assign global EUI-64's
17456 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17457 	 * of the interface identifier. Whether the GUID is in modified
17458 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17459 	 * bit set to 1.
17460 	 */
17461 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17462 }
17463 
17464 /*
17465  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17466  * Note on mapping from multicast IP addresses to IPoIB multicast link
17467  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17468  * The format of an IPoIB multicast address is:
17469  *
17470  *  4 byte QPN      Scope Sign.  Pkey
17471  * +--------------------------------------------+
17472  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17473  * +--------------------------------------------+
17474  *
17475  * The Scope and Pkey components are properties of the IBA port and
17476  * network interface. They can be ascertained from the broadcast address.
17477  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17478  */
17479 static void
17480 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17481 {
17482 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17483 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17484 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17485 	uint8_t	*bphys_addr;
17486 	dl_unitdata_req_t *dlur;
17487 
17488 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17489 
17490 	/*
17491 	 * RFC 4391: IPv4 MGID is 28-bit long.
17492 	 */
17493 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17494 	m_physaddr[17] = m_ipaddr[1];
17495 	m_physaddr[18] = m_ipaddr[2];
17496 	m_physaddr[19] = m_ipaddr[3];
17497 
17498 
17499 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17500 	if (ill->ill_sap_length < 0) {
17501 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17502 	} else  {
17503 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17504 		    ill->ill_sap_length;
17505 	}
17506 	/*
17507 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17508 	 */
17509 	m_physaddr[5] = bphys_addr[5];
17510 	m_physaddr[8] = bphys_addr[8];
17511 	m_physaddr[9] = bphys_addr[9];
17512 }
17513 
17514 static void
17515 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17516 {
17517 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17518 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17519 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17520 	uint8_t	*bphys_addr;
17521 	dl_unitdata_req_t *dlur;
17522 
17523 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17524 
17525 	/*
17526 	 * RFC 4391: IPv4 MGID is 80-bit long.
17527 	 */
17528 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17529 
17530 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17531 	if (ill->ill_sap_length < 0) {
17532 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17533 	} else  {
17534 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17535 		    ill->ill_sap_length;
17536 	}
17537 	/*
17538 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17539 	 */
17540 	m_physaddr[5] = bphys_addr[5];
17541 	m_physaddr[8] = bphys_addr[8];
17542 	m_physaddr[9] = bphys_addr[9];
17543 }
17544 
17545 /*
17546  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17547  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17548  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17549  * of RFC4213.
17550  */
17551 static void
17552 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17553 {
17554 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17555 	v6addr->s6_addr32[2] = 0;
17556 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17557 }
17558 
17559 /*
17560  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17561  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17562  * id.
17563  */
17564 static void
17565 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17566 {
17567 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17568 
17569 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17570 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17571 }
17572 
17573 static void
17574 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17575 {
17576 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17577 }
17578 
17579 static void
17580 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17581 {
17582 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17583 }
17584 
17585 static void
17586 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17587 {
17588 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17589 }
17590 
17591 static void
17592 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17593 {
17594 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17595 }
17596 
17597 /*
17598  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17599  * Returns an held ill, or NULL.
17600  */
17601 ill_t *
17602 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17603     ip_stack_t *ipst)
17604 {
17605 	ill_t	*ill;
17606 	ipif_t	*ipif;
17607 
17608 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17609 	if (ill == NULL)
17610 		return (NULL);
17611 
17612 	mutex_enter(&ill->ill_lock);
17613 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17614 		if (IPIF_IS_CONDEMNED(ipif))
17615 			continue;
17616 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17617 		    ipif->ipif_zoneid != ALL_ZONES)
17618 			continue;
17619 
17620 		mutex_exit(&ill->ill_lock);
17621 		return (ill);
17622 	}
17623 	mutex_exit(&ill->ill_lock);
17624 	ill_refrele(ill);
17625 	return (NULL);
17626 }
17627 
17628 /*
17629  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17630  * If a pointer to an ipif_t is returned then the caller will need to do
17631  * an ill_refrele().
17632  */
17633 ipif_t *
17634 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17635     ip_stack_t *ipst)
17636 {
17637 	ipif_t *ipif;
17638 	ill_t *ill;
17639 
17640 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17641 	if (ill == NULL)
17642 		return (NULL);
17643 
17644 	mutex_enter(&ill->ill_lock);
17645 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17646 		mutex_exit(&ill->ill_lock);
17647 		ill_refrele(ill);
17648 		return (NULL);
17649 	}
17650 
17651 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17652 		if (!IPIF_CAN_LOOKUP(ipif))
17653 			continue;
17654 		if (lifidx == ipif->ipif_id) {
17655 			ipif_refhold_locked(ipif);
17656 			break;
17657 		}
17658 	}
17659 
17660 	mutex_exit(&ill->ill_lock);
17661 	ill_refrele(ill);
17662 	return (ipif);
17663 }
17664 
17665 /*
17666  * Set ill_inputfn based on the current know state.
17667  * This needs to be called when any of the factors taken into
17668  * account changes.
17669  */
17670 void
17671 ill_set_inputfn(ill_t *ill)
17672 {
17673 	ip_stack_t	*ipst = ill->ill_ipst;
17674 
17675 	if (ill->ill_isv6) {
17676 		if (is_system_labeled())
17677 			ill->ill_inputfn = ill_input_full_v6;
17678 		else
17679 			ill->ill_inputfn = ill_input_short_v6;
17680 	} else {
17681 		if (is_system_labeled())
17682 			ill->ill_inputfn = ill_input_full_v4;
17683 		else if (ill->ill_dhcpinit != 0)
17684 			ill->ill_inputfn = ill_input_full_v4;
17685 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17686 		    != NULL)
17687 			ill->ill_inputfn = ill_input_full_v4;
17688 		else if (ipst->ips_ip_cgtp_filter &&
17689 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17690 			ill->ill_inputfn = ill_input_full_v4;
17691 		else
17692 			ill->ill_inputfn = ill_input_short_v4;
17693 	}
17694 }
17695 
17696 /*
17697  * Re-evaluate ill_inputfn for all the IPv4 ills.
17698  * Used when RSVP and CGTP comes and goes.
17699  */
17700 void
17701 ill_set_inputfn_all(ip_stack_t *ipst)
17702 {
17703 	ill_walk_context_t	ctx;
17704 	ill_t			*ill;
17705 
17706 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17707 	ill = ILL_START_WALK_V4(&ctx, ipst);
17708 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17709 		ill_set_inputfn(ill);
17710 
17711 	rw_exit(&ipst->ips_ill_g_lock);
17712 }
17713 
17714 /*
17715  * Set the physical address information for `ill' to the contents of the
17716  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17717  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17718  * EINPROGRESS will be returned.
17719  */
17720 int
17721 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17722 {
17723 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17724 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17725 
17726 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17727 
17728 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17729 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17730 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17731 		/* Changing DL_IPV6_TOKEN is not yet supported */
17732 		return (0);
17733 	}
17734 
17735 	/*
17736 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17737 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17738 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17739 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17740 	 */
17741 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17742 		freemsg(mp);
17743 		return (ENOMEM);
17744 	}
17745 
17746 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17747 
17748 	/*
17749 	 * Since we'll only do a logical down, we can't rely on ipif_down
17750 	 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17751 	 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17752 	 * case, to quiesce ire's and nce's for ill_is_quiescent.
17753 	 */
17754 	mutex_enter(&ill->ill_lock);
17755 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17756 	/* no more ire/nce addition allowed */
17757 	mutex_exit(&ill->ill_lock);
17758 
17759 	/*
17760 	 * If we can quiesce the ill, then set the address.  If not, then
17761 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17762 	 */
17763 	ill_down_ipifs(ill, B_TRUE);
17764 	mutex_enter(&ill->ill_lock);
17765 	if (!ill_is_quiescent(ill)) {
17766 		/* call cannot fail since `conn_t *' argument is NULL */
17767 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17768 		    mp, ILL_DOWN);
17769 		mutex_exit(&ill->ill_lock);
17770 		return (EINPROGRESS);
17771 	}
17772 	mutex_exit(&ill->ill_lock);
17773 
17774 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17775 	return (0);
17776 }
17777 
17778 /*
17779  * Once the ill associated with `q' has quiesced, set its physical address
17780  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17781  * are passed (linked by b_cont), since we sometimes need to save two distinct
17782  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17783  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17784  * is quiesced, we know any stale nce's with the old address information have
17785  * already been removed, so we don't need to call nce_flush().
17786  */
17787 /* ARGSUSED */
17788 static void
17789 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17790 {
17791 	ill_t		*ill = q->q_ptr;
17792 	mblk_t		*addrmp2 = unlinkb(addrmp);
17793 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17794 	uint_t		addrlen, addroff;
17795 	int		status;
17796 
17797 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17798 
17799 	addroff	= dlindp->dl_addr_offset;
17800 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17801 
17802 	switch (dlindp->dl_data) {
17803 	case DL_IPV6_LINK_LAYER_ADDR:
17804 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17805 		freemsg(addrmp2);
17806 		break;
17807 
17808 	case DL_CURR_DEST_ADDR:
17809 		freemsg(ill->ill_dest_addr_mp);
17810 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17811 		ill->ill_dest_addr_mp = addrmp;
17812 		if (ill->ill_isv6) {
17813 			ill_setdesttoken(ill);
17814 			ipif_setdestlinklocal(ill->ill_ipif);
17815 		}
17816 		freemsg(addrmp2);
17817 		break;
17818 
17819 	case DL_CURR_PHYS_ADDR:
17820 		freemsg(ill->ill_phys_addr_mp);
17821 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17822 		ill->ill_phys_addr_mp = addrmp;
17823 		ill->ill_phys_addr_length = addrlen;
17824 		if (ill->ill_isv6)
17825 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17826 		else
17827 			freemsg(addrmp2);
17828 		if (ill->ill_isv6) {
17829 			ill_setdefaulttoken(ill);
17830 			ipif_setlinklocal(ill->ill_ipif);
17831 		}
17832 		break;
17833 	default:
17834 		ASSERT(0);
17835 	}
17836 
17837 	/*
17838 	 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17839 	 * as we bring the ipifs up again.
17840 	 */
17841 	mutex_enter(&ill->ill_lock);
17842 	ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17843 	mutex_exit(&ill->ill_lock);
17844 	/*
17845 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17846 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17847 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17848 	 * brought up.
17849 	 */
17850 	status = ill_up_ipifs(ill, q, addrmp);
17851 	if (status != EINPROGRESS)
17852 		ipsq_current_finish(ipsq);
17853 }
17854 
17855 /*
17856  * Helper routine for setting the ill_nd_lla fields.
17857  */
17858 void
17859 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17860 {
17861 	freemsg(ill->ill_nd_lla_mp);
17862 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17863 	ill->ill_nd_lla_mp = ndmp;
17864 	ill->ill_nd_lla_len = addrlen;
17865 }
17866 
17867 /*
17868  * Replumb the ill.
17869  */
17870 int
17871 ill_replumb(ill_t *ill, mblk_t *mp)
17872 {
17873 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17874 
17875 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17876 
17877 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17878 
17879 	/*
17880 	 * If we can quiesce the ill, then continue.  If not, then
17881 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17882 	 */
17883 	ill_down_ipifs(ill, B_FALSE);
17884 
17885 	mutex_enter(&ill->ill_lock);
17886 	if (!ill_is_quiescent(ill)) {
17887 		/* call cannot fail since `conn_t *' argument is NULL */
17888 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17889 		    mp, ILL_DOWN);
17890 		mutex_exit(&ill->ill_lock);
17891 		return (EINPROGRESS);
17892 	}
17893 	mutex_exit(&ill->ill_lock);
17894 
17895 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17896 	return (0);
17897 }
17898 
17899 /* ARGSUSED */
17900 static void
17901 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17902 {
17903 	ill_t *ill = q->q_ptr;
17904 	int err;
17905 	conn_t *connp = NULL;
17906 
17907 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17908 	freemsg(ill->ill_replumb_mp);
17909 	ill->ill_replumb_mp = copyb(mp);
17910 
17911 	if (ill->ill_replumb_mp == NULL) {
17912 		/* out of memory */
17913 		ipsq_current_finish(ipsq);
17914 		return;
17915 	}
17916 
17917 	mutex_enter(&ill->ill_lock);
17918 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17919 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17920 	mutex_exit(&ill->ill_lock);
17921 
17922 	if (!ill->ill_up_ipifs) {
17923 		/* already closing */
17924 		ipsq_current_finish(ipsq);
17925 		return;
17926 	}
17927 	ill->ill_replumbing = 1;
17928 	err = ill_down_ipifs_tail(ill);
17929 
17930 	/*
17931 	 * Successfully quiesced and brought down the interface, now we send
17932 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17933 	 * DL_NOTE_REPLUMB message.
17934 	 */
17935 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17936 	    DL_NOTIFY_CONF);
17937 	ASSERT(mp != NULL);
17938 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17939 	    DL_NOTE_REPLUMB_DONE;
17940 	ill_dlpi_send(ill, mp);
17941 
17942 	/*
17943 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17944 	 * streams have to be unbound. When all the DLPI exchanges are done,
17945 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17946 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17947 	 * arp_bringup_done().
17948 	 */
17949 	ASSERT(ill->ill_replumb_mp != NULL);
17950 	if (err == EINPROGRESS)
17951 		return;
17952 	else
17953 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17954 	ASSERT(connp == NULL);
17955 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17956 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17957 		return;
17958 	}
17959 	ipsq_current_finish(ipsq);
17960 }
17961 
17962 /*
17963  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17964  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17965  * as per the ioctl.  On failure, an errno is returned.
17966  */
17967 static int
17968 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17969 {
17970 	int rval;
17971 	struct strioctl iocb;
17972 
17973 	iocb.ic_cmd = cmd;
17974 	iocb.ic_timout = 15;
17975 	iocb.ic_len = bufsize;
17976 	iocb.ic_dp = buf;
17977 
17978 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17979 }
17980 
17981 /*
17982  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17983  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17984  */
17985 static int
17986 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17987     uint_t *bufsizep, cred_t *cr)
17988 {
17989 	int err;
17990 	struct lifnum lifn;
17991 
17992 	bzero(&lifn, sizeof (lifn));
17993 	lifn.lifn_family = af;
17994 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17995 
17996 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17997 		return (err);
17998 
17999 	/*
18000 	 * Pad the interface count to account for additional interfaces that
18001 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18002 	 */
18003 	lifn.lifn_count += 4;
18004 	bzero(lifcp, sizeof (*lifcp));
18005 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
18006 	lifcp->lifc_family = af;
18007 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18008 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18009 
18010 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18011 	if (err != 0) {
18012 		kmem_free(lifcp->lifc_buf, *bufsizep);
18013 		return (err);
18014 	}
18015 
18016 	return (0);
18017 }
18018 
18019 /*
18020  * Helper for ip_interface_cleanup() that removes the loopback interface.
18021  */
18022 static void
18023 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18024 {
18025 	int err;
18026 	struct lifreq lifr;
18027 
18028 	bzero(&lifr, sizeof (lifr));
18029 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
18030 
18031 	/*
18032 	 * Attempt to remove the interface.  It may legitimately not exist
18033 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18034 	 */
18035 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18036 	if (err != 0 && err != ENXIO) {
18037 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18038 		    "error %d\n", isv6 ? "v6" : "v4", err));
18039 	}
18040 }
18041 
18042 /*
18043  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18044  * groups and that IPMP data addresses are down.  These conditions must be met
18045  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18046  */
18047 static void
18048 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18049 {
18050 	int af = isv6 ? AF_INET6 : AF_INET;
18051 	int i, nifs;
18052 	int err;
18053 	uint_t bufsize;
18054 	uint_t lifrsize = sizeof (struct lifreq);
18055 	struct lifconf lifc;
18056 	struct lifreq *lifrp;
18057 
18058 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18059 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18060 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
18061 		return;
18062 	}
18063 
18064 	nifs = lifc.lifc_len / lifrsize;
18065 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18066 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18067 		if (err != 0) {
18068 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18069 			    "flags: error %d", lifrp->lifr_name, err);
18070 			continue;
18071 		}
18072 
18073 		if (lifrp->lifr_flags & IFF_IPMP) {
18074 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18075 				continue;
18076 
18077 			lifrp->lifr_flags &= ~IFF_UP;
18078 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18079 			if (err != 0) {
18080 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18081 				    "bring down (error %d); IPMP interface may "
18082 				    "not be shutdown", lifrp->lifr_name, err);
18083 			}
18084 
18085 			/*
18086 			 * Check if IFF_DUPLICATE is still set -- and if so,
18087 			 * reset the address to clear it.
18088 			 */
18089 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18090 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18091 				continue;
18092 
18093 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18094 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18095 			    lifrp, lifrsize, cr)) != 0) {
18096 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18097 				    "reset DAD (error %d); IPMP interface may "
18098 				    "not be shutdown", lifrp->lifr_name, err);
18099 			}
18100 			continue;
18101 		}
18102 
18103 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18104 			lifrp->lifr_groupname[0] = '\0';
18105 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18106 			    lifrsize, cr)) != 0) {
18107 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18108 				    "leave IPMP group (error %d); associated "
18109 				    "IPMP interface may not be shutdown",
18110 				    lifrp->lifr_name, err);
18111 				continue;
18112 			}
18113 		}
18114 	}
18115 
18116 	kmem_free(lifc.lifc_buf, bufsize);
18117 }
18118 
18119 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
18120 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
18121 
18122 /*
18123  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18124  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18125  * when the user-level processes in the zone are killed and the latter are
18126  * cleaned up by str_stack_shutdown().
18127  */
18128 void
18129 ip_interface_cleanup(ip_stack_t *ipst)
18130 {
18131 	ldi_handle_t	lh;
18132 	ldi_ident_t	li;
18133 	cred_t		*cr;
18134 	int		err;
18135 	int		i;
18136 	char		*devs[] = { UDP6DEV, UDPDEV };
18137 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18138 
18139 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18140 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18141 		    " error %d", err);
18142 		return;
18143 	}
18144 
18145 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18146 	ASSERT(cr != NULL);
18147 
18148 	/*
18149 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18150 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18151 	 * the loop.)
18152 	 */
18153 	for (i = 0; i < 2; i++) {
18154 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18155 		if (err != 0) {
18156 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18157 			    " error %d", devs[i], err);
18158 			continue;
18159 		}
18160 
18161 		ip_loopback_removeif(lh, i == 0, cr);
18162 		ip_ipmp_cleanup(lh, i == 0, cr);
18163 
18164 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18165 	}
18166 
18167 	ldi_ident_release(li);
18168 	crfree(cr);
18169 }
18170 
18171 /*
18172  * This needs to be in-sync with nic_event_t definition
18173  */
18174 static const char *
18175 ill_hook_event2str(nic_event_t event)
18176 {
18177 	switch (event) {
18178 	case NE_PLUMB:
18179 		return ("PLUMB");
18180 	case NE_UNPLUMB:
18181 		return ("UNPLUMB");
18182 	case NE_UP:
18183 		return ("UP");
18184 	case NE_DOWN:
18185 		return ("DOWN");
18186 	case NE_ADDRESS_CHANGE:
18187 		return ("ADDRESS_CHANGE");
18188 	case NE_LIF_UP:
18189 		return ("LIF_UP");
18190 	case NE_LIF_DOWN:
18191 		return ("LIF_DOWN");
18192 	case NE_IFINDEX_CHANGE:
18193 		return ("IFINDEX_CHANGE");
18194 	default:
18195 		return ("UNKNOWN");
18196 	}
18197 }
18198 
18199 void
18200 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18201     nic_event_data_t data, size_t datalen)
18202 {
18203 	ip_stack_t		*ipst = ill->ill_ipst;
18204 	hook_nic_event_int_t	*info;
18205 	const char		*str = NULL;
18206 
18207 	/* create a new nic event info */
18208 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18209 		goto fail;
18210 
18211 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18212 	info->hnei_event.hne_lif = lif;
18213 	info->hnei_event.hne_event = event;
18214 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18215 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18216 	info->hnei_event.hne_data = NULL;
18217 	info->hnei_event.hne_datalen = 0;
18218 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18219 
18220 	if (data != NULL && datalen != 0) {
18221 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18222 		if (info->hnei_event.hne_data == NULL)
18223 			goto fail;
18224 		bcopy(data, info->hnei_event.hne_data, datalen);
18225 		info->hnei_event.hne_datalen = datalen;
18226 	}
18227 
18228 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18229 	    DDI_NOSLEEP) == DDI_SUCCESS)
18230 		return;
18231 
18232 fail:
18233 	if (info != NULL) {
18234 		if (info->hnei_event.hne_data != NULL) {
18235 			kmem_free(info->hnei_event.hne_data,
18236 			    info->hnei_event.hne_datalen);
18237 		}
18238 		kmem_free(info, sizeof (hook_nic_event_t));
18239 	}
18240 	str = ill_hook_event2str(event);
18241 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18242 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18243 }
18244 
18245 static int
18246 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18247 {
18248 	int		err = 0;
18249 	const in_addr_t	*addr = NULL;
18250 	nce_t		*nce = NULL;
18251 	ill_t		*ill = ipif->ipif_ill;
18252 	ill_t		*bound_ill;
18253 	boolean_t	added_ipif = B_FALSE;
18254 	uint16_t	state;
18255 	uint16_t	flags;
18256 
18257 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18258 	    ill_t *, ill, ipif_t *, ipif);
18259 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18260 		addr = &ipif->ipif_lcl_addr;
18261 	}
18262 
18263 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18264 		if (res_act != Res_act_initial)
18265 			return (EINVAL);
18266 	}
18267 
18268 	if (addr != NULL) {
18269 		ipmp_illgrp_t	*illg = ill->ill_grp;
18270 
18271 		/* add unicast nce for the local addr */
18272 
18273 		if (IS_IPMP(ill)) {
18274 			/*
18275 			 * If we're here via ipif_up(), then the ipif
18276 			 * won't be bound yet -- add it to the group,
18277 			 * which will bind it if possible. (We would
18278 			 * add it in ipif_up(), but deleting on failure
18279 			 * there is gruesome.)  If we're here via
18280 			 * ipmp_ill_bind_ipif(), then the ipif has
18281 			 * already been added to the group and we
18282 			 * just need to use the binding.
18283 			 */
18284 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18285 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18286 				if (bound_ill == NULL) {
18287 					/*
18288 					 * We couldn't bind the ipif to an ill
18289 					 * yet, so we have nothing to publish.
18290 					 * Mark the address as ready and return.
18291 					 */
18292 					ipif->ipif_addr_ready = 1;
18293 					return (0);
18294 				}
18295 				added_ipif = B_TRUE;
18296 			}
18297 		} else {
18298 			bound_ill = ill;
18299 		}
18300 
18301 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18302 		    NCE_F_NONUD);
18303 		/*
18304 		 * If this is an initial bring-up (or the ipif was never
18305 		 * completely brought up), do DAD.  Otherwise, we're here
18306 		 * because IPMP has rebound an address to this ill: send
18307 		 * unsolicited advertisements (ARP announcements) to
18308 		 * inform others.
18309 		 */
18310 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18311 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18312 		} else {
18313 			state = ND_REACHABLE;
18314 			flags |= NCE_F_UNSOL_ADV;
18315 		}
18316 
18317 retry:
18318 		err = nce_lookup_then_add_v4(ill,
18319 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18320 		    addr, flags, state, &nce);
18321 
18322 		/*
18323 		 * note that we may encounter EEXIST if we are moving
18324 		 * the nce as a result of a rebind operation.
18325 		 */
18326 		switch (err) {
18327 		case 0:
18328 			ipif->ipif_added_nce = 1;
18329 			nce->nce_ipif_cnt++;
18330 			break;
18331 		case EEXIST:
18332 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18333 			    ill->ill_name));
18334 			if (!NCE_MYADDR(nce->nce_common)) {
18335 				/*
18336 				 * A leftover nce from before this address
18337 				 * existed
18338 				 */
18339 				ncec_delete(nce->nce_common);
18340 				nce_refrele(nce);
18341 				nce = NULL;
18342 				goto retry;
18343 			}
18344 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18345 				nce_refrele(nce);
18346 				nce = NULL;
18347 				ip1dbg(("ipif_arp_up: NCE already exists "
18348 				    "for %s:%u\n", ill->ill_name,
18349 				    ipif->ipif_id));
18350 				goto arp_up_done;
18351 			}
18352 			/*
18353 			 * Duplicate local addresses are permissible for
18354 			 * IPIF_POINTOPOINT interfaces which will get marked
18355 			 * IPIF_UNNUMBERED later in
18356 			 * ip_addr_availability_check().
18357 			 *
18358 			 * The nce_ipif_cnt field tracks the number of
18359 			 * ipifs that have nce_addr as their local address.
18360 			 */
18361 			ipif->ipif_addr_ready = 1;
18362 			ipif->ipif_added_nce = 1;
18363 			nce->nce_ipif_cnt++;
18364 			err = 0;
18365 			break;
18366 		default:
18367 			ASSERT(nce == NULL);
18368 			goto arp_up_done;
18369 		}
18370 		if (arp_no_defense) {
18371 			if ((ipif->ipif_flags & IPIF_UP) &&
18372 			    !ipif->ipif_addr_ready)
18373 				ipif_up_notify(ipif);
18374 			ipif->ipif_addr_ready = 1;
18375 		}
18376 	} else {
18377 		/* zero address. nothing to publish */
18378 		ipif->ipif_addr_ready = 1;
18379 	}
18380 	if (nce != NULL)
18381 		nce_refrele(nce);
18382 arp_up_done:
18383 	if (added_ipif && err != 0)
18384 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18385 	return (err);
18386 }
18387 
18388 int
18389 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18390 {
18391 	int 		err = 0;
18392 	ill_t 		*ill = ipif->ipif_ill;
18393 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18394 
18395 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18396 	    ill_t *, ill, ipif_t *, ipif);
18397 
18398 	/*
18399 	 * need to bring up ARP or setup mcast mapping only
18400 	 * when the first interface is coming UP.
18401 	 */
18402 	first_interface = (ill->ill_ipif_up_count == 0 &&
18403 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18404 
18405 	if (res_act == Res_act_initial && first_interface) {
18406 		/*
18407 		 * Send ATTACH + BIND
18408 		 */
18409 		err = arp_ll_up(ill);
18410 		if (err != EINPROGRESS && err != 0)
18411 			return (err);
18412 
18413 		/*
18414 		 * Add NCE for local address. Start DAD.
18415 		 * we'll wait to hear that DAD has finished
18416 		 * before using the interface.
18417 		 */
18418 		if (err == EINPROGRESS)
18419 			wait_for_dlpi = B_TRUE;
18420 	}
18421 
18422 	if (!wait_for_dlpi)
18423 		(void) ipif_arp_up_done_tail(ipif, res_act);
18424 
18425 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18426 }
18427 
18428 /*
18429  * Finish processing of "arp_up" after all the DLPI message
18430  * exchanges have completed between arp and the driver.
18431  */
18432 void
18433 arp_bringup_done(ill_t *ill, int err)
18434 {
18435 	mblk_t	*mp1;
18436 	ipif_t  *ipif;
18437 	conn_t *connp = NULL;
18438 	ipsq_t	*ipsq;
18439 	queue_t *q;
18440 
18441 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18442 
18443 	ASSERT(IAM_WRITER_ILL(ill));
18444 
18445 	ipsq = ill->ill_phyint->phyint_ipsq;
18446 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18447 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18448 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18449 	if (mp1 == NULL) /* bringup was aborted by the user */
18450 		return;
18451 
18452 	/*
18453 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18454 	 * must have an associated conn_t.  Otherwise, we're bringing this
18455 	 * interface back up as part of handling an asynchronous event (e.g.,
18456 	 * physical address change).
18457 	 */
18458 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18459 		ASSERT(connp != NULL);
18460 		q = CONNP_TO_WQ(connp);
18461 	} else {
18462 		ASSERT(connp == NULL);
18463 		q = ill->ill_rq;
18464 	}
18465 	if (err == 0) {
18466 		if (ipif->ipif_isv6) {
18467 			if ((err = ipif_up_done_v6(ipif)) != 0)
18468 				ip0dbg(("arp_bringup_done: init failed\n"));
18469 		} else {
18470 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18471 			if (err != 0 ||
18472 			    (err = ipif_up_done(ipif)) != 0) {
18473 				ip0dbg(("arp_bringup_done: "
18474 				    "init failed err %x\n", err));
18475 				(void) ipif_arp_down(ipif);
18476 			}
18477 
18478 		}
18479 	} else {
18480 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18481 	}
18482 
18483 	if ((err == 0) && (ill->ill_up_ipifs)) {
18484 		err = ill_up_ipifs(ill, q, mp1);
18485 		if (err == EINPROGRESS)
18486 			return;
18487 	}
18488 
18489 	/*
18490 	 * If we have a moved ipif to bring up, and everything has succeeded
18491 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18492 	 * down -- the admin can try to bring it up by hand if need be.
18493 	 */
18494 	if (ill->ill_move_ipif != NULL) {
18495 		ipif = ill->ill_move_ipif;
18496 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18497 		    ipif->ipif_ill->ill_name));
18498 		ill->ill_move_ipif = NULL;
18499 		if (err == 0) {
18500 			err = ipif_up(ipif, q, mp1);
18501 			if (err == EINPROGRESS)
18502 				return;
18503 		}
18504 	}
18505 
18506 	/*
18507 	 * The operation must complete without EINPROGRESS since
18508 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18509 	 * Otherwise, the operation will be stuck forever in the ipsq.
18510 	 */
18511 	ASSERT(err != EINPROGRESS);
18512 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18513 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18514 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18515 		    ill_t *, ill, ipif_t *, ipif);
18516 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18517 	} else {
18518 		ipsq_current_finish(ipsq);
18519 	}
18520 }
18521 
18522 /*
18523  * Finish processing of arp replumb after all the DLPI message
18524  * exchanges have completed between arp and the driver.
18525  */
18526 void
18527 arp_replumb_done(ill_t *ill, int err)
18528 {
18529 	mblk_t	*mp1;
18530 	ipif_t  *ipif;
18531 	conn_t *connp = NULL;
18532 	ipsq_t	*ipsq;
18533 	queue_t *q;
18534 
18535 	ASSERT(IAM_WRITER_ILL(ill));
18536 
18537 	ipsq = ill->ill_phyint->phyint_ipsq;
18538 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18539 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18540 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18541 	if (mp1 == NULL) {
18542 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18543 		    ipsq->ipsq_xop->ipx_current_ioctl));
18544 		/* bringup was aborted by the user */
18545 		return;
18546 	}
18547 	/*
18548 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18549 	 * must have an associated conn_t.  Otherwise, we're bringing this
18550 	 * interface back up as part of handling an asynchronous event (e.g.,
18551 	 * physical address change).
18552 	 */
18553 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18554 		ASSERT(connp != NULL);
18555 		q = CONNP_TO_WQ(connp);
18556 	} else {
18557 		ASSERT(connp == NULL);
18558 		q = ill->ill_rq;
18559 	}
18560 	if ((err == 0) && (ill->ill_up_ipifs)) {
18561 		err = ill_up_ipifs(ill, q, mp1);
18562 		if (err == EINPROGRESS)
18563 			return;
18564 	}
18565 	/*
18566 	 * The operation must complete without EINPROGRESS since
18567 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18568 	 * Otherwise, the operation will be stuck forever in the ipsq.
18569 	 */
18570 	ASSERT(err != EINPROGRESS);
18571 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18572 		DTRACE_PROBE4(ipif__ioctl, char *,
18573 		    "arp_replumb_done finish",
18574 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18575 		    ill_t *, ill, ipif_t *, ipif);
18576 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18577 	} else {
18578 		ipsq_current_finish(ipsq);
18579 	}
18580 }
18581 
18582 void
18583 ipif_up_notify(ipif_t *ipif)
18584 {
18585 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18586 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18587 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18588 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18589 	    NE_LIF_UP, NULL, 0);
18590 }
18591 
18592 /*
18593  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18594  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18595  * TPI end points with STREAMS modules pushed above.  This is assured by not
18596  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18597  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18598  * while unwinding from the ispq and that could be a thread from the bottom.
18599  */
18600 /* ARGSUSED */
18601 int
18602 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18603     ip_ioctl_cmd_t *ipip, void *arg)
18604 {
18605 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18606 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18607 	int ret = 0;
18608 	int i;
18609 	size_t size;
18610 	ip_stack_t *ipst;
18611 	zoneid_t zoneid;
18612 	ilb_stack_t *ilbs;
18613 
18614 	ipst = CONNQ_TO_IPST(q);
18615 	ilbs = ipst->ips_netstack->netstack_ilb;
18616 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18617 
18618 	switch (command) {
18619 	case ILB_CREATE_RULE: {
18620 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18621 
18622 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18623 			ret = EINVAL;
18624 			break;
18625 		}
18626 
18627 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18628 		break;
18629 	}
18630 	case ILB_DESTROY_RULE:
18631 	case ILB_ENABLE_RULE:
18632 	case ILB_DISABLE_RULE: {
18633 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18634 
18635 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18636 			ret = EINVAL;
18637 			break;
18638 		}
18639 
18640 		if (cmd->flags & ILB_RULE_ALLRULES) {
18641 			if (command == ILB_DESTROY_RULE) {
18642 				ilb_rule_del_all(ilbs, zoneid);
18643 				break;
18644 			} else if (command == ILB_ENABLE_RULE) {
18645 				ilb_rule_enable_all(ilbs, zoneid);
18646 				break;
18647 			} else if (command == ILB_DISABLE_RULE) {
18648 				ilb_rule_disable_all(ilbs, zoneid);
18649 				break;
18650 			}
18651 		} else {
18652 			if (command == ILB_DESTROY_RULE) {
18653 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18654 			} else if (command == ILB_ENABLE_RULE) {
18655 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18656 				    NULL);
18657 			} else if (command == ILB_DISABLE_RULE) {
18658 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18659 				    NULL);
18660 			}
18661 		}
18662 		break;
18663 	}
18664 	case ILB_NUM_RULES: {
18665 		ilb_num_rules_cmd_t *cmd;
18666 
18667 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18668 			ret = EINVAL;
18669 			break;
18670 		}
18671 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18672 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18673 		break;
18674 	}
18675 	case ILB_RULE_NAMES: {
18676 		ilb_rule_names_cmd_t *cmd;
18677 
18678 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18679 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18680 		    cmd->num_names == 0) {
18681 			ret = EINVAL;
18682 			break;
18683 		}
18684 		size = cmd->num_names * ILB_RULE_NAMESZ;
18685 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18686 		    size != cmd_mp->b_wptr) {
18687 			ret = EINVAL;
18688 			break;
18689 		}
18690 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18691 		break;
18692 	}
18693 	case ILB_NUM_SERVERS: {
18694 		ilb_num_servers_cmd_t *cmd;
18695 
18696 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18697 			ret = EINVAL;
18698 			break;
18699 		}
18700 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18701 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18702 		    &(cmd->num));
18703 		break;
18704 	}
18705 	case ILB_LIST_RULE: {
18706 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18707 
18708 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18709 			ret = EINVAL;
18710 			break;
18711 		}
18712 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18713 		break;
18714 	}
18715 	case ILB_LIST_SERVERS: {
18716 		ilb_servers_info_cmd_t *cmd;
18717 
18718 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18719 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18720 		    cmd->num_servers == 0) {
18721 			ret = EINVAL;
18722 			break;
18723 		}
18724 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18725 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18726 		    size != cmd_mp->b_wptr) {
18727 			ret = EINVAL;
18728 			break;
18729 		}
18730 
18731 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18732 		    &cmd->num_servers);
18733 		break;
18734 	}
18735 	case ILB_ADD_SERVERS: {
18736 		ilb_servers_info_cmd_t *cmd;
18737 		ilb_rule_t *rule;
18738 
18739 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18740 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18741 			ret = EINVAL;
18742 			break;
18743 		}
18744 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18745 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18746 		    size != cmd_mp->b_wptr) {
18747 			ret = EINVAL;
18748 			break;
18749 		}
18750 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18751 		if (rule == NULL) {
18752 			ASSERT(ret != 0);
18753 			break;
18754 		}
18755 		for (i = 0; i < cmd->num_servers; i++) {
18756 			ilb_server_info_t *s;
18757 
18758 			s = &cmd->servers[i];
18759 			s->err = ilb_server_add(ilbs, rule, s);
18760 		}
18761 		ILB_RULE_REFRELE(rule);
18762 		break;
18763 	}
18764 	case ILB_DEL_SERVERS:
18765 	case ILB_ENABLE_SERVERS:
18766 	case ILB_DISABLE_SERVERS: {
18767 		ilb_servers_cmd_t *cmd;
18768 		ilb_rule_t *rule;
18769 		int (*f)();
18770 
18771 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18772 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18773 			ret = EINVAL;
18774 			break;
18775 		}
18776 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18777 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18778 		    size != cmd_mp->b_wptr) {
18779 			ret = EINVAL;
18780 			break;
18781 		}
18782 
18783 		if (command == ILB_DEL_SERVERS)
18784 			f = ilb_server_del;
18785 		else if (command == ILB_ENABLE_SERVERS)
18786 			f = ilb_server_enable;
18787 		else if (command == ILB_DISABLE_SERVERS)
18788 			f = ilb_server_disable;
18789 
18790 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18791 		if (rule == NULL) {
18792 			ASSERT(ret != 0);
18793 			break;
18794 		}
18795 
18796 		for (i = 0; i < cmd->num_servers; i++) {
18797 			ilb_server_arg_t *s;
18798 
18799 			s = &cmd->servers[i];
18800 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18801 		}
18802 		ILB_RULE_REFRELE(rule);
18803 		break;
18804 	}
18805 	case ILB_LIST_NAT_TABLE: {
18806 		ilb_list_nat_cmd_t *cmd;
18807 
18808 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18809 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18810 			ret = EINVAL;
18811 			break;
18812 		}
18813 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18814 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18815 		    size != cmd_mp->b_wptr) {
18816 			ret = EINVAL;
18817 			break;
18818 		}
18819 
18820 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18821 		    &cmd->flags);
18822 		break;
18823 	}
18824 	case ILB_LIST_STICKY_TABLE: {
18825 		ilb_list_sticky_cmd_t *cmd;
18826 
18827 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18828 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18829 			ret = EINVAL;
18830 			break;
18831 		}
18832 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18833 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18834 		    size != cmd_mp->b_wptr) {
18835 			ret = EINVAL;
18836 			break;
18837 		}
18838 
18839 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18840 		    &cmd->num_sticky, &cmd->flags);
18841 		break;
18842 	}
18843 	default:
18844 		ret = EINVAL;
18845 		break;
18846 	}
18847 done:
18848 	return (ret);
18849 }
18850 
18851 /* Remove all cache entries for this logical interface */
18852 void
18853 ipif_nce_down(ipif_t *ipif)
18854 {
18855 	ill_t *ill = ipif->ipif_ill;
18856 	nce_t *nce;
18857 
18858 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18859 	    ill_t *, ill, ipif_t *, ipif);
18860 	if (ipif->ipif_added_nce) {
18861 		if (ipif->ipif_isv6)
18862 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18863 		else
18864 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18865 		if (nce != NULL) {
18866 			if (--nce->nce_ipif_cnt == 0)
18867 				ncec_delete(nce->nce_common);
18868 			ipif->ipif_added_nce = 0;
18869 			nce_refrele(nce);
18870 		} else {
18871 			/*
18872 			 * nce may already be NULL because it was already
18873 			 * flushed, e.g., due to a call to nce_flush
18874 			 */
18875 			ipif->ipif_added_nce = 0;
18876 		}
18877 	}
18878 	/*
18879 	 * Make IPMP aware of the deleted data address.
18880 	 */
18881 	if (IS_IPMP(ill))
18882 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18883 
18884 	/*
18885 	 * Remove all other nces dependent on this ill when the last ipif
18886 	 * is going away.
18887 	 */
18888 	if (ill->ill_ipif_up_count == 0) {
18889 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18890 		    (uchar_t *)ill, ill->ill_ipst);
18891 		if (IS_UNDER_IPMP(ill))
18892 			nce_flush(ill, B_TRUE);
18893 	}
18894 }
18895 
18896 /*
18897  * find the first interface that uses usill for its source address.
18898  */
18899 ill_t *
18900 ill_lookup_usesrc(ill_t *usill)
18901 {
18902 	ip_stack_t *ipst = usill->ill_ipst;
18903 	ill_t *ill;
18904 
18905 	ASSERT(usill != NULL);
18906 
18907 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
18908 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
18909 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
18910 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
18911 	    ill = ill->ill_usesrc_grp_next) {
18912 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
18913 		    !ILL_IS_CONDEMNED(ill)) {
18914 			ill_refhold(ill);
18915 			break;
18916 		}
18917 	}
18918 	rw_exit(&ipst->ips_ill_g_lock);
18919 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
18920 	return (ill);
18921 }
18922